af_rose.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638
  1. /*
  2. * This program is free software; you can redistribute it and/or modify
  3. * it under the terms of the GNU General Public License as published by
  4. * the Free Software Foundation; either version 2 of the License, or
  5. * (at your option) any later version.
  6. *
  7. * Copyright (C) Jonathan Naylor G4KLX (g4klx@g4klx.demon.co.uk)
  8. * Copyright (C) Alan Cox GW4PTS (alan@lxorguk.ukuu.org.uk)
  9. * Copyright (C) Terry Dawson VK2KTJ (terry@animats.net)
  10. * Copyright (C) Tomi Manninen OH2BNS (oh2bns@sral.fi)
  11. */
  12. #include <linux/capability.h>
  13. #include <linux/module.h>
  14. #include <linux/moduleparam.h>
  15. #include <linux/init.h>
  16. #include <linux/errno.h>
  17. #include <linux/types.h>
  18. #include <linux/socket.h>
  19. #include <linux/in.h>
  20. #include <linux/slab.h>
  21. #include <linux/kernel.h>
  22. #include <linux/sched.h>
  23. #include <linux/spinlock.h>
  24. #include <linux/timer.h>
  25. #include <linux/string.h>
  26. #include <linux/sockios.h>
  27. #include <linux/net.h>
  28. #include <linux/stat.h>
  29. #include <net/net_namespace.h>
  30. #include <net/ax25.h>
  31. #include <linux/inet.h>
  32. #include <linux/netdevice.h>
  33. #include <linux/if_arp.h>
  34. #include <linux/skbuff.h>
  35. #include <net/sock.h>
  36. #include <asm/uaccess.h>
  37. #include <linux/fcntl.h>
  38. #include <linux/termios.h>
  39. #include <linux/mm.h>
  40. #include <linux/interrupt.h>
  41. #include <linux/notifier.h>
  42. #include <net/rose.h>
  43. #include <linux/proc_fs.h>
  44. #include <linux/seq_file.h>
  45. #include <net/tcp_states.h>
  46. #include <net/ip.h>
  47. #include <net/arp.h>
  48. static int rose_ndevs = 10;
  49. int sysctl_rose_restart_request_timeout = ROSE_DEFAULT_T0;
  50. int sysctl_rose_call_request_timeout = ROSE_DEFAULT_T1;
  51. int sysctl_rose_reset_request_timeout = ROSE_DEFAULT_T2;
  52. int sysctl_rose_clear_request_timeout = ROSE_DEFAULT_T3;
  53. int sysctl_rose_no_activity_timeout = ROSE_DEFAULT_IDLE;
  54. int sysctl_rose_ack_hold_back_timeout = ROSE_DEFAULT_HB;
  55. int sysctl_rose_routing_control = ROSE_DEFAULT_ROUTING;
  56. int sysctl_rose_link_fail_timeout = ROSE_DEFAULT_FAIL_TIMEOUT;
  57. int sysctl_rose_maximum_vcs = ROSE_DEFAULT_MAXVC;
  58. int sysctl_rose_window_size = ROSE_DEFAULT_WINDOW_SIZE;
  59. static HLIST_HEAD(rose_list);
  60. static DEFINE_SPINLOCK(rose_list_lock);
  61. static const struct proto_ops rose_proto_ops;
  62. ax25_address rose_callsign;
  63. /*
  64. * ROSE network devices are virtual network devices encapsulating ROSE
  65. * frames into AX.25 which will be sent through an AX.25 device, so form a
  66. * special "super class" of normal net devices; split their locks off into a
  67. * separate class since they always nest.
  68. */
  69. static struct lock_class_key rose_netdev_xmit_lock_key;
  70. static struct lock_class_key rose_netdev_addr_lock_key;
  71. static void rose_set_lockdep_one(struct net_device *dev,
  72. struct netdev_queue *txq,
  73. void *_unused)
  74. {
  75. lockdep_set_class(&txq->_xmit_lock, &rose_netdev_xmit_lock_key);
  76. }
  77. static void rose_set_lockdep_key(struct net_device *dev)
  78. {
  79. lockdep_set_class(&dev->addr_list_lock, &rose_netdev_addr_lock_key);
  80. netdev_for_each_tx_queue(dev, rose_set_lockdep_one, NULL);
  81. }
  82. /*
  83. * Convert a ROSE address into text.
  84. */
  85. char *rose2asc(char *buf, const rose_address *addr)
  86. {
  87. if (addr->rose_addr[0] == 0x00 && addr->rose_addr[1] == 0x00 &&
  88. addr->rose_addr[2] == 0x00 && addr->rose_addr[3] == 0x00 &&
  89. addr->rose_addr[4] == 0x00) {
  90. strcpy(buf, "*");
  91. } else {
  92. sprintf(buf, "%02X%02X%02X%02X%02X", addr->rose_addr[0] & 0xFF,
  93. addr->rose_addr[1] & 0xFF,
  94. addr->rose_addr[2] & 0xFF,
  95. addr->rose_addr[3] & 0xFF,
  96. addr->rose_addr[4] & 0xFF);
  97. }
  98. return buf;
  99. }
  100. /*
  101. * Compare two ROSE addresses, 0 == equal.
  102. */
  103. int rosecmp(rose_address *addr1, rose_address *addr2)
  104. {
  105. int i;
  106. for (i = 0; i < 5; i++)
  107. if (addr1->rose_addr[i] != addr2->rose_addr[i])
  108. return 1;
  109. return 0;
  110. }
  111. /*
  112. * Compare two ROSE addresses for only mask digits, 0 == equal.
  113. */
  114. int rosecmpm(rose_address *addr1, rose_address *addr2, unsigned short mask)
  115. {
  116. unsigned int i, j;
  117. if (mask > 10)
  118. return 1;
  119. for (i = 0; i < mask; i++) {
  120. j = i / 2;
  121. if ((i % 2) != 0) {
  122. if ((addr1->rose_addr[j] & 0x0F) != (addr2->rose_addr[j] & 0x0F))
  123. return 1;
  124. } else {
  125. if ((addr1->rose_addr[j] & 0xF0) != (addr2->rose_addr[j] & 0xF0))
  126. return 1;
  127. }
  128. }
  129. return 0;
  130. }
  131. /*
  132. * Socket removal during an interrupt is now safe.
  133. */
  134. static void rose_remove_socket(struct sock *sk)
  135. {
  136. spin_lock_bh(&rose_list_lock);
  137. sk_del_node_init(sk);
  138. spin_unlock_bh(&rose_list_lock);
  139. }
  140. /*
  141. * Kill all bound sockets on a broken link layer connection to a
  142. * particular neighbour.
  143. */
  144. void rose_kill_by_neigh(struct rose_neigh *neigh)
  145. {
  146. struct sock *s;
  147. spin_lock_bh(&rose_list_lock);
  148. sk_for_each(s, &rose_list) {
  149. struct rose_sock *rose = rose_sk(s);
  150. if (rose->neighbour == neigh) {
  151. rose_disconnect(s, ENETUNREACH, ROSE_OUT_OF_ORDER, 0);
  152. rose->neighbour->use--;
  153. rose->neighbour = NULL;
  154. }
  155. }
  156. spin_unlock_bh(&rose_list_lock);
  157. }
  158. /*
  159. * Kill all bound sockets on a dropped device.
  160. */
  161. static void rose_kill_by_device(struct net_device *dev)
  162. {
  163. struct sock *s;
  164. spin_lock_bh(&rose_list_lock);
  165. sk_for_each(s, &rose_list) {
  166. struct rose_sock *rose = rose_sk(s);
  167. if (rose->device == dev) {
  168. rose_disconnect(s, ENETUNREACH, ROSE_OUT_OF_ORDER, 0);
  169. if (rose->neighbour)
  170. rose->neighbour->use--;
  171. rose->device = NULL;
  172. }
  173. }
  174. spin_unlock_bh(&rose_list_lock);
  175. }
  176. /*
  177. * Handle device status changes.
  178. */
  179. static int rose_device_event(struct notifier_block *this,
  180. unsigned long event, void *ptr)
  181. {
  182. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  183. if (!net_eq(dev_net(dev), &init_net))
  184. return NOTIFY_DONE;
  185. if (event != NETDEV_DOWN)
  186. return NOTIFY_DONE;
  187. switch (dev->type) {
  188. case ARPHRD_ROSE:
  189. rose_kill_by_device(dev);
  190. break;
  191. case ARPHRD_AX25:
  192. rose_link_device_down(dev);
  193. rose_rt_device_down(dev);
  194. break;
  195. }
  196. return NOTIFY_DONE;
  197. }
  198. /*
  199. * Add a socket to the bound sockets list.
  200. */
  201. static void rose_insert_socket(struct sock *sk)
  202. {
  203. spin_lock_bh(&rose_list_lock);
  204. sk_add_node(sk, &rose_list);
  205. spin_unlock_bh(&rose_list_lock);
  206. }
  207. /*
  208. * Find a socket that wants to accept the Call Request we just
  209. * received.
  210. */
  211. static struct sock *rose_find_listener(rose_address *addr, ax25_address *call)
  212. {
  213. struct sock *s;
  214. spin_lock_bh(&rose_list_lock);
  215. sk_for_each(s, &rose_list) {
  216. struct rose_sock *rose = rose_sk(s);
  217. if (!rosecmp(&rose->source_addr, addr) &&
  218. !ax25cmp(&rose->source_call, call) &&
  219. !rose->source_ndigis && s->sk_state == TCP_LISTEN)
  220. goto found;
  221. }
  222. sk_for_each(s, &rose_list) {
  223. struct rose_sock *rose = rose_sk(s);
  224. if (!rosecmp(&rose->source_addr, addr) &&
  225. !ax25cmp(&rose->source_call, &null_ax25_address) &&
  226. s->sk_state == TCP_LISTEN)
  227. goto found;
  228. }
  229. s = NULL;
  230. found:
  231. spin_unlock_bh(&rose_list_lock);
  232. return s;
  233. }
  234. /*
  235. * Find a connected ROSE socket given my LCI and device.
  236. */
  237. struct sock *rose_find_socket(unsigned int lci, struct rose_neigh *neigh)
  238. {
  239. struct sock *s;
  240. spin_lock_bh(&rose_list_lock);
  241. sk_for_each(s, &rose_list) {
  242. struct rose_sock *rose = rose_sk(s);
  243. if (rose->lci == lci && rose->neighbour == neigh)
  244. goto found;
  245. }
  246. s = NULL;
  247. found:
  248. spin_unlock_bh(&rose_list_lock);
  249. return s;
  250. }
  251. /*
  252. * Find a unique LCI for a given device.
  253. */
  254. unsigned int rose_new_lci(struct rose_neigh *neigh)
  255. {
  256. int lci;
  257. if (neigh->dce_mode) {
  258. for (lci = 1; lci <= sysctl_rose_maximum_vcs; lci++)
  259. if (rose_find_socket(lci, neigh) == NULL && rose_route_free_lci(lci, neigh) == NULL)
  260. return lci;
  261. } else {
  262. for (lci = sysctl_rose_maximum_vcs; lci > 0; lci--)
  263. if (rose_find_socket(lci, neigh) == NULL && rose_route_free_lci(lci, neigh) == NULL)
  264. return lci;
  265. }
  266. return 0;
  267. }
  268. /*
  269. * Deferred destroy.
  270. */
  271. void rose_destroy_socket(struct sock *);
  272. /*
  273. * Handler for deferred kills.
  274. */
  275. static void rose_destroy_timer(unsigned long data)
  276. {
  277. rose_destroy_socket((struct sock *)data);
  278. }
  279. /*
  280. * This is called from user mode and the timers. Thus it protects itself
  281. * against interrupt users but doesn't worry about being called during
  282. * work. Once it is removed from the queue no interrupt or bottom half
  283. * will touch it and we are (fairly 8-) ) safe.
  284. */
  285. void rose_destroy_socket(struct sock *sk)
  286. {
  287. struct sk_buff *skb;
  288. rose_remove_socket(sk);
  289. rose_stop_heartbeat(sk);
  290. rose_stop_idletimer(sk);
  291. rose_stop_timer(sk);
  292. rose_clear_queues(sk); /* Flush the queues */
  293. while ((skb = skb_dequeue(&sk->sk_receive_queue)) != NULL) {
  294. if (skb->sk != sk) { /* A pending connection */
  295. /* Queue the unaccepted socket for death */
  296. sock_set_flag(skb->sk, SOCK_DEAD);
  297. rose_start_heartbeat(skb->sk);
  298. rose_sk(skb->sk)->state = ROSE_STATE_0;
  299. }
  300. kfree_skb(skb);
  301. }
  302. if (sk_has_allocations(sk)) {
  303. /* Defer: outstanding buffers */
  304. setup_timer(&sk->sk_timer, rose_destroy_timer,
  305. (unsigned long)sk);
  306. sk->sk_timer.expires = jiffies + 10 * HZ;
  307. add_timer(&sk->sk_timer);
  308. } else
  309. sock_put(sk);
  310. }
  311. /*
  312. * Handling for system calls applied via the various interfaces to a
  313. * ROSE socket object.
  314. */
  315. static int rose_setsockopt(struct socket *sock, int level, int optname,
  316. char __user *optval, unsigned int optlen)
  317. {
  318. struct sock *sk = sock->sk;
  319. struct rose_sock *rose = rose_sk(sk);
  320. int opt;
  321. if (level != SOL_ROSE)
  322. return -ENOPROTOOPT;
  323. if (optlen < sizeof(int))
  324. return -EINVAL;
  325. if (get_user(opt, (int __user *)optval))
  326. return -EFAULT;
  327. switch (optname) {
  328. case ROSE_DEFER:
  329. rose->defer = opt ? 1 : 0;
  330. return 0;
  331. case ROSE_T1:
  332. if (opt < 1)
  333. return -EINVAL;
  334. rose->t1 = opt * HZ;
  335. return 0;
  336. case ROSE_T2:
  337. if (opt < 1)
  338. return -EINVAL;
  339. rose->t2 = opt * HZ;
  340. return 0;
  341. case ROSE_T3:
  342. if (opt < 1)
  343. return -EINVAL;
  344. rose->t3 = opt * HZ;
  345. return 0;
  346. case ROSE_HOLDBACK:
  347. if (opt < 1)
  348. return -EINVAL;
  349. rose->hb = opt * HZ;
  350. return 0;
  351. case ROSE_IDLE:
  352. if (opt < 0)
  353. return -EINVAL;
  354. rose->idle = opt * 60 * HZ;
  355. return 0;
  356. case ROSE_QBITINCL:
  357. rose->qbitincl = opt ? 1 : 0;
  358. return 0;
  359. default:
  360. return -ENOPROTOOPT;
  361. }
  362. }
  363. static int rose_getsockopt(struct socket *sock, int level, int optname,
  364. char __user *optval, int __user *optlen)
  365. {
  366. struct sock *sk = sock->sk;
  367. struct rose_sock *rose = rose_sk(sk);
  368. int val = 0;
  369. int len;
  370. if (level != SOL_ROSE)
  371. return -ENOPROTOOPT;
  372. if (get_user(len, optlen))
  373. return -EFAULT;
  374. if (len < 0)
  375. return -EINVAL;
  376. switch (optname) {
  377. case ROSE_DEFER:
  378. val = rose->defer;
  379. break;
  380. case ROSE_T1:
  381. val = rose->t1 / HZ;
  382. break;
  383. case ROSE_T2:
  384. val = rose->t2 / HZ;
  385. break;
  386. case ROSE_T3:
  387. val = rose->t3 / HZ;
  388. break;
  389. case ROSE_HOLDBACK:
  390. val = rose->hb / HZ;
  391. break;
  392. case ROSE_IDLE:
  393. val = rose->idle / (60 * HZ);
  394. break;
  395. case ROSE_QBITINCL:
  396. val = rose->qbitincl;
  397. break;
  398. default:
  399. return -ENOPROTOOPT;
  400. }
  401. len = min_t(unsigned int, len, sizeof(int));
  402. if (put_user(len, optlen))
  403. return -EFAULT;
  404. return copy_to_user(optval, &val, len) ? -EFAULT : 0;
  405. }
  406. static int rose_listen(struct socket *sock, int backlog)
  407. {
  408. struct sock *sk = sock->sk;
  409. if (sk->sk_state != TCP_LISTEN) {
  410. struct rose_sock *rose = rose_sk(sk);
  411. rose->dest_ndigis = 0;
  412. memset(&rose->dest_addr, 0, ROSE_ADDR_LEN);
  413. memset(&rose->dest_call, 0, AX25_ADDR_LEN);
  414. memset(rose->dest_digis, 0, AX25_ADDR_LEN * ROSE_MAX_DIGIS);
  415. sk->sk_max_ack_backlog = backlog;
  416. sk->sk_state = TCP_LISTEN;
  417. return 0;
  418. }
  419. return -EOPNOTSUPP;
  420. }
  421. static struct proto rose_proto = {
  422. .name = "ROSE",
  423. .owner = THIS_MODULE,
  424. .obj_size = sizeof(struct rose_sock),
  425. };
  426. static int rose_create(struct net *net, struct socket *sock, int protocol,
  427. int kern)
  428. {
  429. struct sock *sk;
  430. struct rose_sock *rose;
  431. if (!net_eq(net, &init_net))
  432. return -EAFNOSUPPORT;
  433. if (sock->type != SOCK_SEQPACKET || protocol != 0)
  434. return -ESOCKTNOSUPPORT;
  435. sk = sk_alloc(net, PF_ROSE, GFP_ATOMIC, &rose_proto, kern);
  436. if (sk == NULL)
  437. return -ENOMEM;
  438. rose = rose_sk(sk);
  439. sock_init_data(sock, sk);
  440. skb_queue_head_init(&rose->ack_queue);
  441. #ifdef M_BIT
  442. skb_queue_head_init(&rose->frag_queue);
  443. rose->fraglen = 0;
  444. #endif
  445. sock->ops = &rose_proto_ops;
  446. sk->sk_protocol = protocol;
  447. init_timer(&rose->timer);
  448. init_timer(&rose->idletimer);
  449. rose->t1 = msecs_to_jiffies(sysctl_rose_call_request_timeout);
  450. rose->t2 = msecs_to_jiffies(sysctl_rose_reset_request_timeout);
  451. rose->t3 = msecs_to_jiffies(sysctl_rose_clear_request_timeout);
  452. rose->hb = msecs_to_jiffies(sysctl_rose_ack_hold_back_timeout);
  453. rose->idle = msecs_to_jiffies(sysctl_rose_no_activity_timeout);
  454. rose->state = ROSE_STATE_0;
  455. return 0;
  456. }
  457. static struct sock *rose_make_new(struct sock *osk)
  458. {
  459. struct sock *sk;
  460. struct rose_sock *rose, *orose;
  461. if (osk->sk_type != SOCK_SEQPACKET)
  462. return NULL;
  463. sk = sk_alloc(sock_net(osk), PF_ROSE, GFP_ATOMIC, &rose_proto, 0);
  464. if (sk == NULL)
  465. return NULL;
  466. rose = rose_sk(sk);
  467. sock_init_data(NULL, sk);
  468. skb_queue_head_init(&rose->ack_queue);
  469. #ifdef M_BIT
  470. skb_queue_head_init(&rose->frag_queue);
  471. rose->fraglen = 0;
  472. #endif
  473. sk->sk_type = osk->sk_type;
  474. sk->sk_priority = osk->sk_priority;
  475. sk->sk_protocol = osk->sk_protocol;
  476. sk->sk_rcvbuf = osk->sk_rcvbuf;
  477. sk->sk_sndbuf = osk->sk_sndbuf;
  478. sk->sk_state = TCP_ESTABLISHED;
  479. sock_copy_flags(sk, osk);
  480. init_timer(&rose->timer);
  481. init_timer(&rose->idletimer);
  482. orose = rose_sk(osk);
  483. rose->t1 = orose->t1;
  484. rose->t2 = orose->t2;
  485. rose->t3 = orose->t3;
  486. rose->hb = orose->hb;
  487. rose->idle = orose->idle;
  488. rose->defer = orose->defer;
  489. rose->device = orose->device;
  490. rose->qbitincl = orose->qbitincl;
  491. return sk;
  492. }
  493. static int rose_release(struct socket *sock)
  494. {
  495. struct sock *sk = sock->sk;
  496. struct rose_sock *rose;
  497. if (sk == NULL) return 0;
  498. sock_hold(sk);
  499. sock_orphan(sk);
  500. lock_sock(sk);
  501. rose = rose_sk(sk);
  502. switch (rose->state) {
  503. case ROSE_STATE_0:
  504. release_sock(sk);
  505. rose_disconnect(sk, 0, -1, -1);
  506. lock_sock(sk);
  507. rose_destroy_socket(sk);
  508. break;
  509. case ROSE_STATE_2:
  510. rose->neighbour->use--;
  511. release_sock(sk);
  512. rose_disconnect(sk, 0, -1, -1);
  513. lock_sock(sk);
  514. rose_destroy_socket(sk);
  515. break;
  516. case ROSE_STATE_1:
  517. case ROSE_STATE_3:
  518. case ROSE_STATE_4:
  519. case ROSE_STATE_5:
  520. rose_clear_queues(sk);
  521. rose_stop_idletimer(sk);
  522. rose_write_internal(sk, ROSE_CLEAR_REQUEST);
  523. rose_start_t3timer(sk);
  524. rose->state = ROSE_STATE_2;
  525. sk->sk_state = TCP_CLOSE;
  526. sk->sk_shutdown |= SEND_SHUTDOWN;
  527. sk->sk_state_change(sk);
  528. sock_set_flag(sk, SOCK_DEAD);
  529. sock_set_flag(sk, SOCK_DESTROY);
  530. break;
  531. default:
  532. break;
  533. }
  534. sock->sk = NULL;
  535. release_sock(sk);
  536. sock_put(sk);
  537. return 0;
  538. }
  539. static int rose_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
  540. {
  541. struct sock *sk = sock->sk;
  542. struct rose_sock *rose = rose_sk(sk);
  543. struct sockaddr_rose *addr = (struct sockaddr_rose *)uaddr;
  544. struct net_device *dev;
  545. ax25_address *source;
  546. ax25_uid_assoc *user;
  547. int n;
  548. if (!sock_flag(sk, SOCK_ZAPPED))
  549. return -EINVAL;
  550. if (addr_len != sizeof(struct sockaddr_rose) && addr_len != sizeof(struct full_sockaddr_rose))
  551. return -EINVAL;
  552. if (addr->srose_family != AF_ROSE)
  553. return -EINVAL;
  554. if (addr_len == sizeof(struct sockaddr_rose) && addr->srose_ndigis > 1)
  555. return -EINVAL;
  556. if ((unsigned int) addr->srose_ndigis > ROSE_MAX_DIGIS)
  557. return -EINVAL;
  558. if ((dev = rose_dev_get(&addr->srose_addr)) == NULL)
  559. return -EADDRNOTAVAIL;
  560. source = &addr->srose_call;
  561. user = ax25_findbyuid(current_euid());
  562. if (user) {
  563. rose->source_call = user->call;
  564. ax25_uid_put(user);
  565. } else {
  566. if (ax25_uid_policy && !capable(CAP_NET_BIND_SERVICE))
  567. return -EACCES;
  568. rose->source_call = *source;
  569. }
  570. rose->source_addr = addr->srose_addr;
  571. rose->device = dev;
  572. rose->source_ndigis = addr->srose_ndigis;
  573. if (addr_len == sizeof(struct full_sockaddr_rose)) {
  574. struct full_sockaddr_rose *full_addr = (struct full_sockaddr_rose *)uaddr;
  575. for (n = 0 ; n < addr->srose_ndigis ; n++)
  576. rose->source_digis[n] = full_addr->srose_digis[n];
  577. } else {
  578. if (rose->source_ndigis == 1) {
  579. rose->source_digis[0] = addr->srose_digi;
  580. }
  581. }
  582. rose_insert_socket(sk);
  583. sock_reset_flag(sk, SOCK_ZAPPED);
  584. return 0;
  585. }
  586. static int rose_connect(struct socket *sock, struct sockaddr *uaddr, int addr_len, int flags)
  587. {
  588. struct sock *sk = sock->sk;
  589. struct rose_sock *rose = rose_sk(sk);
  590. struct sockaddr_rose *addr = (struct sockaddr_rose *)uaddr;
  591. unsigned char cause, diagnostic;
  592. struct net_device *dev;
  593. ax25_uid_assoc *user;
  594. int n, err = 0;
  595. if (addr_len != sizeof(struct sockaddr_rose) && addr_len != sizeof(struct full_sockaddr_rose))
  596. return -EINVAL;
  597. if (addr->srose_family != AF_ROSE)
  598. return -EINVAL;
  599. if (addr_len == sizeof(struct sockaddr_rose) && addr->srose_ndigis > 1)
  600. return -EINVAL;
  601. if ((unsigned int) addr->srose_ndigis > ROSE_MAX_DIGIS)
  602. return -EINVAL;
  603. /* Source + Destination digis should not exceed ROSE_MAX_DIGIS */
  604. if ((rose->source_ndigis + addr->srose_ndigis) > ROSE_MAX_DIGIS)
  605. return -EINVAL;
  606. lock_sock(sk);
  607. if (sk->sk_state == TCP_ESTABLISHED && sock->state == SS_CONNECTING) {
  608. /* Connect completed during a ERESTARTSYS event */
  609. sock->state = SS_CONNECTED;
  610. goto out_release;
  611. }
  612. if (sk->sk_state == TCP_CLOSE && sock->state == SS_CONNECTING) {
  613. sock->state = SS_UNCONNECTED;
  614. err = -ECONNREFUSED;
  615. goto out_release;
  616. }
  617. if (sk->sk_state == TCP_ESTABLISHED) {
  618. /* No reconnect on a seqpacket socket */
  619. err = -EISCONN;
  620. goto out_release;
  621. }
  622. sk->sk_state = TCP_CLOSE;
  623. sock->state = SS_UNCONNECTED;
  624. rose->neighbour = rose_get_neigh(&addr->srose_addr, &cause,
  625. &diagnostic, 0);
  626. if (!rose->neighbour) {
  627. err = -ENETUNREACH;
  628. goto out_release;
  629. }
  630. rose->lci = rose_new_lci(rose->neighbour);
  631. if (!rose->lci) {
  632. err = -ENETUNREACH;
  633. goto out_release;
  634. }
  635. if (sock_flag(sk, SOCK_ZAPPED)) { /* Must bind first - autobinding in this may or may not work */
  636. sock_reset_flag(sk, SOCK_ZAPPED);
  637. if ((dev = rose_dev_first()) == NULL) {
  638. err = -ENETUNREACH;
  639. goto out_release;
  640. }
  641. user = ax25_findbyuid(current_euid());
  642. if (!user) {
  643. err = -EINVAL;
  644. goto out_release;
  645. }
  646. memcpy(&rose->source_addr, dev->dev_addr, ROSE_ADDR_LEN);
  647. rose->source_call = user->call;
  648. rose->device = dev;
  649. ax25_uid_put(user);
  650. rose_insert_socket(sk); /* Finish the bind */
  651. }
  652. rose->dest_addr = addr->srose_addr;
  653. rose->dest_call = addr->srose_call;
  654. rose->rand = ((long)rose & 0xFFFF) + rose->lci;
  655. rose->dest_ndigis = addr->srose_ndigis;
  656. if (addr_len == sizeof(struct full_sockaddr_rose)) {
  657. struct full_sockaddr_rose *full_addr = (struct full_sockaddr_rose *)uaddr;
  658. for (n = 0 ; n < addr->srose_ndigis ; n++)
  659. rose->dest_digis[n] = full_addr->srose_digis[n];
  660. } else {
  661. if (rose->dest_ndigis == 1) {
  662. rose->dest_digis[0] = addr->srose_digi;
  663. }
  664. }
  665. /* Move to connecting socket, start sending Connect Requests */
  666. sock->state = SS_CONNECTING;
  667. sk->sk_state = TCP_SYN_SENT;
  668. rose->state = ROSE_STATE_1;
  669. rose->neighbour->use++;
  670. rose_write_internal(sk, ROSE_CALL_REQUEST);
  671. rose_start_heartbeat(sk);
  672. rose_start_t1timer(sk);
  673. /* Now the loop */
  674. if (sk->sk_state != TCP_ESTABLISHED && (flags & O_NONBLOCK)) {
  675. err = -EINPROGRESS;
  676. goto out_release;
  677. }
  678. /*
  679. * A Connect Ack with Choke or timeout or failed routing will go to
  680. * closed.
  681. */
  682. if (sk->sk_state == TCP_SYN_SENT) {
  683. DEFINE_WAIT(wait);
  684. for (;;) {
  685. prepare_to_wait(sk_sleep(sk), &wait,
  686. TASK_INTERRUPTIBLE);
  687. if (sk->sk_state != TCP_SYN_SENT)
  688. break;
  689. if (!signal_pending(current)) {
  690. release_sock(sk);
  691. schedule();
  692. lock_sock(sk);
  693. continue;
  694. }
  695. err = -ERESTARTSYS;
  696. break;
  697. }
  698. finish_wait(sk_sleep(sk), &wait);
  699. if (err)
  700. goto out_release;
  701. }
  702. if (sk->sk_state != TCP_ESTABLISHED) {
  703. sock->state = SS_UNCONNECTED;
  704. err = sock_error(sk); /* Always set at this point */
  705. goto out_release;
  706. }
  707. sock->state = SS_CONNECTED;
  708. out_release:
  709. release_sock(sk);
  710. return err;
  711. }
  712. static int rose_accept(struct socket *sock, struct socket *newsock, int flags)
  713. {
  714. struct sk_buff *skb;
  715. struct sock *newsk;
  716. DEFINE_WAIT(wait);
  717. struct sock *sk;
  718. int err = 0;
  719. if ((sk = sock->sk) == NULL)
  720. return -EINVAL;
  721. lock_sock(sk);
  722. if (sk->sk_type != SOCK_SEQPACKET) {
  723. err = -EOPNOTSUPP;
  724. goto out_release;
  725. }
  726. if (sk->sk_state != TCP_LISTEN) {
  727. err = -EINVAL;
  728. goto out_release;
  729. }
  730. /*
  731. * The write queue this time is holding sockets ready to use
  732. * hooked into the SABM we saved
  733. */
  734. for (;;) {
  735. prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
  736. skb = skb_dequeue(&sk->sk_receive_queue);
  737. if (skb)
  738. break;
  739. if (flags & O_NONBLOCK) {
  740. err = -EWOULDBLOCK;
  741. break;
  742. }
  743. if (!signal_pending(current)) {
  744. release_sock(sk);
  745. schedule();
  746. lock_sock(sk);
  747. continue;
  748. }
  749. err = -ERESTARTSYS;
  750. break;
  751. }
  752. finish_wait(sk_sleep(sk), &wait);
  753. if (err)
  754. goto out_release;
  755. newsk = skb->sk;
  756. sock_graft(newsk, newsock);
  757. /* Now attach up the new socket */
  758. skb->sk = NULL;
  759. kfree_skb(skb);
  760. sk->sk_ack_backlog--;
  761. out_release:
  762. release_sock(sk);
  763. return err;
  764. }
  765. static int rose_getname(struct socket *sock, struct sockaddr *uaddr,
  766. int *uaddr_len, int peer)
  767. {
  768. struct full_sockaddr_rose *srose = (struct full_sockaddr_rose *)uaddr;
  769. struct sock *sk = sock->sk;
  770. struct rose_sock *rose = rose_sk(sk);
  771. int n;
  772. memset(srose, 0, sizeof(*srose));
  773. if (peer != 0) {
  774. if (sk->sk_state != TCP_ESTABLISHED)
  775. return -ENOTCONN;
  776. srose->srose_family = AF_ROSE;
  777. srose->srose_addr = rose->dest_addr;
  778. srose->srose_call = rose->dest_call;
  779. srose->srose_ndigis = rose->dest_ndigis;
  780. for (n = 0; n < rose->dest_ndigis; n++)
  781. srose->srose_digis[n] = rose->dest_digis[n];
  782. } else {
  783. srose->srose_family = AF_ROSE;
  784. srose->srose_addr = rose->source_addr;
  785. srose->srose_call = rose->source_call;
  786. srose->srose_ndigis = rose->source_ndigis;
  787. for (n = 0; n < rose->source_ndigis; n++)
  788. srose->srose_digis[n] = rose->source_digis[n];
  789. }
  790. *uaddr_len = sizeof(struct full_sockaddr_rose);
  791. return 0;
  792. }
  793. int rose_rx_call_request(struct sk_buff *skb, struct net_device *dev, struct rose_neigh *neigh, unsigned int lci)
  794. {
  795. struct sock *sk;
  796. struct sock *make;
  797. struct rose_sock *make_rose;
  798. struct rose_facilities_struct facilities;
  799. int n;
  800. skb->sk = NULL; /* Initially we don't know who it's for */
  801. /*
  802. * skb->data points to the rose frame start
  803. */
  804. memset(&facilities, 0x00, sizeof(struct rose_facilities_struct));
  805. if (!rose_parse_facilities(skb->data + ROSE_CALL_REQ_FACILITIES_OFF,
  806. skb->len - ROSE_CALL_REQ_FACILITIES_OFF,
  807. &facilities)) {
  808. rose_transmit_clear_request(neigh, lci, ROSE_INVALID_FACILITY, 76);
  809. return 0;
  810. }
  811. sk = rose_find_listener(&facilities.source_addr, &facilities.source_call);
  812. /*
  813. * We can't accept the Call Request.
  814. */
  815. if (sk == NULL || sk_acceptq_is_full(sk) ||
  816. (make = rose_make_new(sk)) == NULL) {
  817. rose_transmit_clear_request(neigh, lci, ROSE_NETWORK_CONGESTION, 120);
  818. return 0;
  819. }
  820. skb->sk = make;
  821. make->sk_state = TCP_ESTABLISHED;
  822. make_rose = rose_sk(make);
  823. make_rose->lci = lci;
  824. make_rose->dest_addr = facilities.dest_addr;
  825. make_rose->dest_call = facilities.dest_call;
  826. make_rose->dest_ndigis = facilities.dest_ndigis;
  827. for (n = 0 ; n < facilities.dest_ndigis ; n++)
  828. make_rose->dest_digis[n] = facilities.dest_digis[n];
  829. make_rose->source_addr = facilities.source_addr;
  830. make_rose->source_call = facilities.source_call;
  831. make_rose->source_ndigis = facilities.source_ndigis;
  832. for (n = 0 ; n < facilities.source_ndigis ; n++)
  833. make_rose->source_digis[n] = facilities.source_digis[n];
  834. make_rose->neighbour = neigh;
  835. make_rose->device = dev;
  836. make_rose->facilities = facilities;
  837. make_rose->neighbour->use++;
  838. if (rose_sk(sk)->defer) {
  839. make_rose->state = ROSE_STATE_5;
  840. } else {
  841. rose_write_internal(make, ROSE_CALL_ACCEPTED);
  842. make_rose->state = ROSE_STATE_3;
  843. rose_start_idletimer(make);
  844. }
  845. make_rose->condition = 0x00;
  846. make_rose->vs = 0;
  847. make_rose->va = 0;
  848. make_rose->vr = 0;
  849. make_rose->vl = 0;
  850. sk->sk_ack_backlog++;
  851. rose_insert_socket(make);
  852. skb_queue_head(&sk->sk_receive_queue, skb);
  853. rose_start_heartbeat(make);
  854. if (!sock_flag(sk, SOCK_DEAD))
  855. sk->sk_data_ready(sk);
  856. return 1;
  857. }
  858. static int rose_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
  859. {
  860. struct sock *sk = sock->sk;
  861. struct rose_sock *rose = rose_sk(sk);
  862. DECLARE_SOCKADDR(struct sockaddr_rose *, usrose, msg->msg_name);
  863. int err;
  864. struct full_sockaddr_rose srose;
  865. struct sk_buff *skb;
  866. unsigned char *asmptr;
  867. int n, size, qbit = 0;
  868. if (msg->msg_flags & ~(MSG_DONTWAIT|MSG_EOR|MSG_CMSG_COMPAT))
  869. return -EINVAL;
  870. if (sock_flag(sk, SOCK_ZAPPED))
  871. return -EADDRNOTAVAIL;
  872. if (sk->sk_shutdown & SEND_SHUTDOWN) {
  873. send_sig(SIGPIPE, current, 0);
  874. return -EPIPE;
  875. }
  876. if (rose->neighbour == NULL || rose->device == NULL)
  877. return -ENETUNREACH;
  878. if (usrose != NULL) {
  879. if (msg->msg_namelen != sizeof(struct sockaddr_rose) && msg->msg_namelen != sizeof(struct full_sockaddr_rose))
  880. return -EINVAL;
  881. memset(&srose, 0, sizeof(struct full_sockaddr_rose));
  882. memcpy(&srose, usrose, msg->msg_namelen);
  883. if (rosecmp(&rose->dest_addr, &srose.srose_addr) != 0 ||
  884. ax25cmp(&rose->dest_call, &srose.srose_call) != 0)
  885. return -EISCONN;
  886. if (srose.srose_ndigis != rose->dest_ndigis)
  887. return -EISCONN;
  888. if (srose.srose_ndigis == rose->dest_ndigis) {
  889. for (n = 0 ; n < srose.srose_ndigis ; n++)
  890. if (ax25cmp(&rose->dest_digis[n],
  891. &srose.srose_digis[n]))
  892. return -EISCONN;
  893. }
  894. if (srose.srose_family != AF_ROSE)
  895. return -EINVAL;
  896. } else {
  897. if (sk->sk_state != TCP_ESTABLISHED)
  898. return -ENOTCONN;
  899. srose.srose_family = AF_ROSE;
  900. srose.srose_addr = rose->dest_addr;
  901. srose.srose_call = rose->dest_call;
  902. srose.srose_ndigis = rose->dest_ndigis;
  903. for (n = 0 ; n < rose->dest_ndigis ; n++)
  904. srose.srose_digis[n] = rose->dest_digis[n];
  905. }
  906. /* Build a packet */
  907. /* Sanity check the packet size */
  908. if (len > 65535)
  909. return -EMSGSIZE;
  910. size = len + AX25_BPQ_HEADER_LEN + AX25_MAX_HEADER_LEN + ROSE_MIN_LEN;
  911. if ((skb = sock_alloc_send_skb(sk, size, msg->msg_flags & MSG_DONTWAIT, &err)) == NULL)
  912. return err;
  913. skb_reserve(skb, AX25_BPQ_HEADER_LEN + AX25_MAX_HEADER_LEN + ROSE_MIN_LEN);
  914. /*
  915. * Put the data on the end
  916. */
  917. skb_reset_transport_header(skb);
  918. skb_put(skb, len);
  919. err = memcpy_from_msg(skb_transport_header(skb), msg, len);
  920. if (err) {
  921. kfree_skb(skb);
  922. return err;
  923. }
  924. /*
  925. * If the Q BIT Include socket option is in force, the first
  926. * byte of the user data is the logical value of the Q Bit.
  927. */
  928. if (rose->qbitincl) {
  929. qbit = skb->data[0];
  930. skb_pull(skb, 1);
  931. }
  932. /*
  933. * Push down the ROSE header
  934. */
  935. asmptr = skb_push(skb, ROSE_MIN_LEN);
  936. /* Build a ROSE Network header */
  937. asmptr[0] = ((rose->lci >> 8) & 0x0F) | ROSE_GFI;
  938. asmptr[1] = (rose->lci >> 0) & 0xFF;
  939. asmptr[2] = ROSE_DATA;
  940. if (qbit)
  941. asmptr[0] |= ROSE_Q_BIT;
  942. if (sk->sk_state != TCP_ESTABLISHED) {
  943. kfree_skb(skb);
  944. return -ENOTCONN;
  945. }
  946. #ifdef M_BIT
  947. #define ROSE_PACLEN (256-ROSE_MIN_LEN)
  948. if (skb->len - ROSE_MIN_LEN > ROSE_PACLEN) {
  949. unsigned char header[ROSE_MIN_LEN];
  950. struct sk_buff *skbn;
  951. int frontlen;
  952. int lg;
  953. /* Save a copy of the Header */
  954. skb_copy_from_linear_data(skb, header, ROSE_MIN_LEN);
  955. skb_pull(skb, ROSE_MIN_LEN);
  956. frontlen = skb_headroom(skb);
  957. while (skb->len > 0) {
  958. if ((skbn = sock_alloc_send_skb(sk, frontlen + ROSE_PACLEN, 0, &err)) == NULL) {
  959. kfree_skb(skb);
  960. return err;
  961. }
  962. skbn->sk = sk;
  963. skbn->free = 1;
  964. skbn->arp = 1;
  965. skb_reserve(skbn, frontlen);
  966. lg = (ROSE_PACLEN > skb->len) ? skb->len : ROSE_PACLEN;
  967. /* Copy the user data */
  968. skb_copy_from_linear_data(skb, skb_put(skbn, lg), lg);
  969. skb_pull(skb, lg);
  970. /* Duplicate the Header */
  971. skb_push(skbn, ROSE_MIN_LEN);
  972. skb_copy_to_linear_data(skbn, header, ROSE_MIN_LEN);
  973. if (skb->len > 0)
  974. skbn->data[2] |= M_BIT;
  975. skb_queue_tail(&sk->sk_write_queue, skbn); /* Throw it on the queue */
  976. }
  977. skb->free = 1;
  978. kfree_skb(skb);
  979. } else {
  980. skb_queue_tail(&sk->sk_write_queue, skb); /* Throw it on the queue */
  981. }
  982. #else
  983. skb_queue_tail(&sk->sk_write_queue, skb); /* Shove it onto the queue */
  984. #endif
  985. rose_kick(sk);
  986. return len;
  987. }
  988. static int rose_recvmsg(struct socket *sock, struct msghdr *msg, size_t size,
  989. int flags)
  990. {
  991. struct sock *sk = sock->sk;
  992. struct rose_sock *rose = rose_sk(sk);
  993. size_t copied;
  994. unsigned char *asmptr;
  995. struct sk_buff *skb;
  996. int n, er, qbit;
  997. /*
  998. * This works for seqpacket too. The receiver has ordered the queue for
  999. * us! We do one quick check first though
  1000. */
  1001. if (sk->sk_state != TCP_ESTABLISHED)
  1002. return -ENOTCONN;
  1003. /* Now we can treat all alike */
  1004. if ((skb = skb_recv_datagram(sk, flags & ~MSG_DONTWAIT, flags & MSG_DONTWAIT, &er)) == NULL)
  1005. return er;
  1006. qbit = (skb->data[0] & ROSE_Q_BIT) == ROSE_Q_BIT;
  1007. skb_pull(skb, ROSE_MIN_LEN);
  1008. if (rose->qbitincl) {
  1009. asmptr = skb_push(skb, 1);
  1010. *asmptr = qbit;
  1011. }
  1012. skb_reset_transport_header(skb);
  1013. copied = skb->len;
  1014. if (copied > size) {
  1015. copied = size;
  1016. msg->msg_flags |= MSG_TRUNC;
  1017. }
  1018. skb_copy_datagram_msg(skb, 0, msg, copied);
  1019. if (msg->msg_name) {
  1020. struct sockaddr_rose *srose;
  1021. DECLARE_SOCKADDR(struct full_sockaddr_rose *, full_srose,
  1022. msg->msg_name);
  1023. memset(msg->msg_name, 0, sizeof(struct full_sockaddr_rose));
  1024. srose = msg->msg_name;
  1025. srose->srose_family = AF_ROSE;
  1026. srose->srose_addr = rose->dest_addr;
  1027. srose->srose_call = rose->dest_call;
  1028. srose->srose_ndigis = rose->dest_ndigis;
  1029. for (n = 0 ; n < rose->dest_ndigis ; n++)
  1030. full_srose->srose_digis[n] = rose->dest_digis[n];
  1031. msg->msg_namelen = sizeof(struct full_sockaddr_rose);
  1032. }
  1033. skb_free_datagram(sk, skb);
  1034. return copied;
  1035. }
  1036. static int rose_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
  1037. {
  1038. struct sock *sk = sock->sk;
  1039. struct rose_sock *rose = rose_sk(sk);
  1040. void __user *argp = (void __user *)arg;
  1041. switch (cmd) {
  1042. case TIOCOUTQ: {
  1043. long amount;
  1044. amount = sk->sk_sndbuf - sk_wmem_alloc_get(sk);
  1045. if (amount < 0)
  1046. amount = 0;
  1047. return put_user(amount, (unsigned int __user *) argp);
  1048. }
  1049. case TIOCINQ: {
  1050. struct sk_buff *skb;
  1051. long amount = 0L;
  1052. /* These two are safe on a single CPU system as only user tasks fiddle here */
  1053. if ((skb = skb_peek(&sk->sk_receive_queue)) != NULL)
  1054. amount = skb->len;
  1055. return put_user(amount, (unsigned int __user *) argp);
  1056. }
  1057. case SIOCGSTAMP:
  1058. return sock_get_timestamp(sk, (struct timeval __user *) argp);
  1059. case SIOCGSTAMPNS:
  1060. return sock_get_timestampns(sk, (struct timespec __user *) argp);
  1061. case SIOCGIFADDR:
  1062. case SIOCSIFADDR:
  1063. case SIOCGIFDSTADDR:
  1064. case SIOCSIFDSTADDR:
  1065. case SIOCGIFBRDADDR:
  1066. case SIOCSIFBRDADDR:
  1067. case SIOCGIFNETMASK:
  1068. case SIOCSIFNETMASK:
  1069. case SIOCGIFMETRIC:
  1070. case SIOCSIFMETRIC:
  1071. return -EINVAL;
  1072. case SIOCADDRT:
  1073. case SIOCDELRT:
  1074. case SIOCRSCLRRT:
  1075. if (!capable(CAP_NET_ADMIN))
  1076. return -EPERM;
  1077. return rose_rt_ioctl(cmd, argp);
  1078. case SIOCRSGCAUSE: {
  1079. struct rose_cause_struct rose_cause;
  1080. rose_cause.cause = rose->cause;
  1081. rose_cause.diagnostic = rose->diagnostic;
  1082. return copy_to_user(argp, &rose_cause, sizeof(struct rose_cause_struct)) ? -EFAULT : 0;
  1083. }
  1084. case SIOCRSSCAUSE: {
  1085. struct rose_cause_struct rose_cause;
  1086. if (copy_from_user(&rose_cause, argp, sizeof(struct rose_cause_struct)))
  1087. return -EFAULT;
  1088. rose->cause = rose_cause.cause;
  1089. rose->diagnostic = rose_cause.diagnostic;
  1090. return 0;
  1091. }
  1092. case SIOCRSSL2CALL:
  1093. if (!capable(CAP_NET_ADMIN)) return -EPERM;
  1094. if (ax25cmp(&rose_callsign, &null_ax25_address) != 0)
  1095. ax25_listen_release(&rose_callsign, NULL);
  1096. if (copy_from_user(&rose_callsign, argp, sizeof(ax25_address)))
  1097. return -EFAULT;
  1098. if (ax25cmp(&rose_callsign, &null_ax25_address) != 0)
  1099. return ax25_listen_register(&rose_callsign, NULL);
  1100. return 0;
  1101. case SIOCRSGL2CALL:
  1102. return copy_to_user(argp, &rose_callsign, sizeof(ax25_address)) ? -EFAULT : 0;
  1103. case SIOCRSACCEPT:
  1104. if (rose->state == ROSE_STATE_5) {
  1105. rose_write_internal(sk, ROSE_CALL_ACCEPTED);
  1106. rose_start_idletimer(sk);
  1107. rose->condition = 0x00;
  1108. rose->vs = 0;
  1109. rose->va = 0;
  1110. rose->vr = 0;
  1111. rose->vl = 0;
  1112. rose->state = ROSE_STATE_3;
  1113. }
  1114. return 0;
  1115. default:
  1116. return -ENOIOCTLCMD;
  1117. }
  1118. return 0;
  1119. }
  1120. #ifdef CONFIG_PROC_FS
  1121. static void *rose_info_start(struct seq_file *seq, loff_t *pos)
  1122. __acquires(rose_list_lock)
  1123. {
  1124. spin_lock_bh(&rose_list_lock);
  1125. return seq_hlist_start_head(&rose_list, *pos);
  1126. }
  1127. static void *rose_info_next(struct seq_file *seq, void *v, loff_t *pos)
  1128. {
  1129. return seq_hlist_next(v, &rose_list, pos);
  1130. }
  1131. static void rose_info_stop(struct seq_file *seq, void *v)
  1132. __releases(rose_list_lock)
  1133. {
  1134. spin_unlock_bh(&rose_list_lock);
  1135. }
  1136. static int rose_info_show(struct seq_file *seq, void *v)
  1137. {
  1138. char buf[11], rsbuf[11];
  1139. if (v == SEQ_START_TOKEN)
  1140. seq_puts(seq,
  1141. "dest_addr dest_call src_addr src_call dev lci neigh st vs vr va t t1 t2 t3 hb idle Snd-Q Rcv-Q inode\n");
  1142. else {
  1143. struct sock *s = sk_entry(v);
  1144. struct rose_sock *rose = rose_sk(s);
  1145. const char *devname, *callsign;
  1146. const struct net_device *dev = rose->device;
  1147. if (!dev)
  1148. devname = "???";
  1149. else
  1150. devname = dev->name;
  1151. seq_printf(seq, "%-10s %-9s ",
  1152. rose2asc(rsbuf, &rose->dest_addr),
  1153. ax2asc(buf, &rose->dest_call));
  1154. if (ax25cmp(&rose->source_call, &null_ax25_address) == 0)
  1155. callsign = "??????-?";
  1156. else
  1157. callsign = ax2asc(buf, &rose->source_call);
  1158. seq_printf(seq,
  1159. "%-10s %-9s %-5s %3.3X %05d %d %d %d %d %3lu %3lu %3lu %3lu %3lu %3lu/%03lu %5d %5d %ld\n",
  1160. rose2asc(rsbuf, &rose->source_addr),
  1161. callsign,
  1162. devname,
  1163. rose->lci & 0x0FFF,
  1164. (rose->neighbour) ? rose->neighbour->number : 0,
  1165. rose->state,
  1166. rose->vs,
  1167. rose->vr,
  1168. rose->va,
  1169. ax25_display_timer(&rose->timer) / HZ,
  1170. rose->t1 / HZ,
  1171. rose->t2 / HZ,
  1172. rose->t3 / HZ,
  1173. rose->hb / HZ,
  1174. ax25_display_timer(&rose->idletimer) / (60 * HZ),
  1175. rose->idle / (60 * HZ),
  1176. sk_wmem_alloc_get(s),
  1177. sk_rmem_alloc_get(s),
  1178. s->sk_socket ? SOCK_INODE(s->sk_socket)->i_ino : 0L);
  1179. }
  1180. return 0;
  1181. }
  1182. static const struct seq_operations rose_info_seqops = {
  1183. .start = rose_info_start,
  1184. .next = rose_info_next,
  1185. .stop = rose_info_stop,
  1186. .show = rose_info_show,
  1187. };
  1188. static int rose_info_open(struct inode *inode, struct file *file)
  1189. {
  1190. return seq_open(file, &rose_info_seqops);
  1191. }
  1192. static const struct file_operations rose_info_fops = {
  1193. .owner = THIS_MODULE,
  1194. .open = rose_info_open,
  1195. .read = seq_read,
  1196. .llseek = seq_lseek,
  1197. .release = seq_release,
  1198. };
  1199. #endif /* CONFIG_PROC_FS */
  1200. static const struct net_proto_family rose_family_ops = {
  1201. .family = PF_ROSE,
  1202. .create = rose_create,
  1203. .owner = THIS_MODULE,
  1204. };
  1205. static const struct proto_ops rose_proto_ops = {
  1206. .family = PF_ROSE,
  1207. .owner = THIS_MODULE,
  1208. .release = rose_release,
  1209. .bind = rose_bind,
  1210. .connect = rose_connect,
  1211. .socketpair = sock_no_socketpair,
  1212. .accept = rose_accept,
  1213. .getname = rose_getname,
  1214. .poll = datagram_poll,
  1215. .ioctl = rose_ioctl,
  1216. .listen = rose_listen,
  1217. .shutdown = sock_no_shutdown,
  1218. .setsockopt = rose_setsockopt,
  1219. .getsockopt = rose_getsockopt,
  1220. .sendmsg = rose_sendmsg,
  1221. .recvmsg = rose_recvmsg,
  1222. .mmap = sock_no_mmap,
  1223. .sendpage = sock_no_sendpage,
  1224. };
  1225. static struct notifier_block rose_dev_notifier = {
  1226. .notifier_call = rose_device_event,
  1227. };
  1228. static struct net_device **dev_rose;
  1229. static struct ax25_protocol rose_pid = {
  1230. .pid = AX25_P_ROSE,
  1231. .func = rose_route_frame
  1232. };
  1233. static struct ax25_linkfail rose_linkfail_notifier = {
  1234. .func = rose_link_failed
  1235. };
  1236. static int __init rose_proto_init(void)
  1237. {
  1238. int i;
  1239. int rc;
  1240. if (rose_ndevs > 0x7FFFFFFF/sizeof(struct net_device *)) {
  1241. printk(KERN_ERR "ROSE: rose_proto_init - rose_ndevs parameter to large\n");
  1242. rc = -EINVAL;
  1243. goto out;
  1244. }
  1245. rc = proto_register(&rose_proto, 0);
  1246. if (rc != 0)
  1247. goto out;
  1248. rose_callsign = null_ax25_address;
  1249. dev_rose = kzalloc(rose_ndevs * sizeof(struct net_device *), GFP_KERNEL);
  1250. if (dev_rose == NULL) {
  1251. printk(KERN_ERR "ROSE: rose_proto_init - unable to allocate device structure\n");
  1252. rc = -ENOMEM;
  1253. goto out_proto_unregister;
  1254. }
  1255. for (i = 0; i < rose_ndevs; i++) {
  1256. struct net_device *dev;
  1257. char name[IFNAMSIZ];
  1258. sprintf(name, "rose%d", i);
  1259. dev = alloc_netdev(0, name, NET_NAME_UNKNOWN, rose_setup);
  1260. if (!dev) {
  1261. printk(KERN_ERR "ROSE: rose_proto_init - unable to allocate memory\n");
  1262. rc = -ENOMEM;
  1263. goto fail;
  1264. }
  1265. rc = register_netdev(dev);
  1266. if (rc) {
  1267. printk(KERN_ERR "ROSE: netdevice registration failed\n");
  1268. free_netdev(dev);
  1269. goto fail;
  1270. }
  1271. rose_set_lockdep_key(dev);
  1272. dev_rose[i] = dev;
  1273. }
  1274. sock_register(&rose_family_ops);
  1275. register_netdevice_notifier(&rose_dev_notifier);
  1276. ax25_register_pid(&rose_pid);
  1277. ax25_linkfail_register(&rose_linkfail_notifier);
  1278. #ifdef CONFIG_SYSCTL
  1279. rose_register_sysctl();
  1280. #endif
  1281. rose_loopback_init();
  1282. rose_add_loopback_neigh();
  1283. proc_create("rose", S_IRUGO, init_net.proc_net, &rose_info_fops);
  1284. proc_create("rose_neigh", S_IRUGO, init_net.proc_net,
  1285. &rose_neigh_fops);
  1286. proc_create("rose_nodes", S_IRUGO, init_net.proc_net,
  1287. &rose_nodes_fops);
  1288. proc_create("rose_routes", S_IRUGO, init_net.proc_net,
  1289. &rose_routes_fops);
  1290. out:
  1291. return rc;
  1292. fail:
  1293. while (--i >= 0) {
  1294. unregister_netdev(dev_rose[i]);
  1295. free_netdev(dev_rose[i]);
  1296. }
  1297. kfree(dev_rose);
  1298. out_proto_unregister:
  1299. proto_unregister(&rose_proto);
  1300. goto out;
  1301. }
  1302. module_init(rose_proto_init);
  1303. module_param(rose_ndevs, int, 0);
  1304. MODULE_PARM_DESC(rose_ndevs, "number of ROSE devices");
  1305. MODULE_AUTHOR("Jonathan Naylor G4KLX <g4klx@g4klx.demon.co.uk>");
  1306. MODULE_DESCRIPTION("The amateur radio ROSE network layer protocol");
  1307. MODULE_LICENSE("GPL");
  1308. MODULE_ALIAS_NETPROTO(PF_ROSE);
  1309. static void __exit rose_exit(void)
  1310. {
  1311. int i;
  1312. remove_proc_entry("rose", init_net.proc_net);
  1313. remove_proc_entry("rose_neigh", init_net.proc_net);
  1314. remove_proc_entry("rose_nodes", init_net.proc_net);
  1315. remove_proc_entry("rose_routes", init_net.proc_net);
  1316. rose_loopback_clear();
  1317. rose_rt_free();
  1318. ax25_protocol_release(AX25_P_ROSE);
  1319. ax25_linkfail_release(&rose_linkfail_notifier);
  1320. if (ax25cmp(&rose_callsign, &null_ax25_address) != 0)
  1321. ax25_listen_release(&rose_callsign, NULL);
  1322. #ifdef CONFIG_SYSCTL
  1323. rose_unregister_sysctl();
  1324. #endif
  1325. unregister_netdevice_notifier(&rose_dev_notifier);
  1326. sock_unregister(PF_ROSE);
  1327. for (i = 0; i < rose_ndevs; i++) {
  1328. struct net_device *dev = dev_rose[i];
  1329. if (dev) {
  1330. unregister_netdev(dev);
  1331. free_netdev(dev);
  1332. }
  1333. }
  1334. kfree(dev_rose);
  1335. proto_unregister(&rose_proto);
  1336. }
  1337. module_exit(rose_exit);