af_rose.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627
  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/signal.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 <linux/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(struct timer_list *t)
  276. {
  277. struct sock *sk = from_timer(sk, t, sk_timer);
  278. rose_destroy_socket(sk);
  279. }
  280. /*
  281. * This is called from user mode and the timers. Thus it protects itself
  282. * against interrupt users but doesn't worry about being called during
  283. * work. Once it is removed from the queue no interrupt or bottom half
  284. * will touch it and we are (fairly 8-) ) safe.
  285. */
  286. void rose_destroy_socket(struct sock *sk)
  287. {
  288. struct sk_buff *skb;
  289. rose_remove_socket(sk);
  290. rose_stop_heartbeat(sk);
  291. rose_stop_idletimer(sk);
  292. rose_stop_timer(sk);
  293. rose_clear_queues(sk); /* Flush the queues */
  294. while ((skb = skb_dequeue(&sk->sk_receive_queue)) != NULL) {
  295. if (skb->sk != sk) { /* A pending connection */
  296. /* Queue the unaccepted socket for death */
  297. sock_set_flag(skb->sk, SOCK_DEAD);
  298. rose_start_heartbeat(skb->sk);
  299. rose_sk(skb->sk)->state = ROSE_STATE_0;
  300. }
  301. kfree_skb(skb);
  302. }
  303. if (sk_has_allocations(sk)) {
  304. /* Defer: outstanding buffers */
  305. timer_setup(&sk->sk_timer, rose_destroy_timer, 0);
  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. timer_setup(&rose->timer, NULL, 0);
  448. timer_setup(&rose->idletimer, NULL, 0);
  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. timer_setup(&rose->timer, NULL, 0);
  481. timer_setup(&rose->idletimer, NULL, 0);
  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. bool kern)
  714. {
  715. struct sk_buff *skb;
  716. struct sock *newsk;
  717. DEFINE_WAIT(wait);
  718. struct sock *sk;
  719. int err = 0;
  720. if ((sk = sock->sk) == NULL)
  721. return -EINVAL;
  722. lock_sock(sk);
  723. if (sk->sk_type != SOCK_SEQPACKET) {
  724. err = -EOPNOTSUPP;
  725. goto out_release;
  726. }
  727. if (sk->sk_state != TCP_LISTEN) {
  728. err = -EINVAL;
  729. goto out_release;
  730. }
  731. /*
  732. * The write queue this time is holding sockets ready to use
  733. * hooked into the SABM we saved
  734. */
  735. for (;;) {
  736. prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
  737. skb = skb_dequeue(&sk->sk_receive_queue);
  738. if (skb)
  739. break;
  740. if (flags & O_NONBLOCK) {
  741. err = -EWOULDBLOCK;
  742. break;
  743. }
  744. if (!signal_pending(current)) {
  745. release_sock(sk);
  746. schedule();
  747. lock_sock(sk);
  748. continue;
  749. }
  750. err = -ERESTARTSYS;
  751. break;
  752. }
  753. finish_wait(sk_sleep(sk), &wait);
  754. if (err)
  755. goto out_release;
  756. newsk = skb->sk;
  757. sock_graft(newsk, newsock);
  758. /* Now attach up the new socket */
  759. skb->sk = NULL;
  760. kfree_skb(skb);
  761. sk->sk_ack_backlog--;
  762. out_release:
  763. release_sock(sk);
  764. return err;
  765. }
  766. static int rose_getname(struct socket *sock, struct sockaddr *uaddr,
  767. int peer)
  768. {
  769. struct full_sockaddr_rose *srose = (struct full_sockaddr_rose *)uaddr;
  770. struct sock *sk = sock->sk;
  771. struct rose_sock *rose = rose_sk(sk);
  772. int n;
  773. memset(srose, 0, sizeof(*srose));
  774. if (peer != 0) {
  775. if (sk->sk_state != TCP_ESTABLISHED)
  776. return -ENOTCONN;
  777. srose->srose_family = AF_ROSE;
  778. srose->srose_addr = rose->dest_addr;
  779. srose->srose_call = rose->dest_call;
  780. srose->srose_ndigis = rose->dest_ndigis;
  781. for (n = 0; n < rose->dest_ndigis; n++)
  782. srose->srose_digis[n] = rose->dest_digis[n];
  783. } else {
  784. srose->srose_family = AF_ROSE;
  785. srose->srose_addr = rose->source_addr;
  786. srose->srose_call = rose->source_call;
  787. srose->srose_ndigis = rose->source_ndigis;
  788. for (n = 0; n < rose->source_ndigis; n++)
  789. srose->srose_digis[n] = rose->source_digis[n];
  790. }
  791. return sizeof(struct full_sockaddr_rose);
  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. #endif /* CONFIG_PROC_FS */
  1189. static const struct net_proto_family rose_family_ops = {
  1190. .family = PF_ROSE,
  1191. .create = rose_create,
  1192. .owner = THIS_MODULE,
  1193. };
  1194. static const struct proto_ops rose_proto_ops = {
  1195. .family = PF_ROSE,
  1196. .owner = THIS_MODULE,
  1197. .release = rose_release,
  1198. .bind = rose_bind,
  1199. .connect = rose_connect,
  1200. .socketpair = sock_no_socketpair,
  1201. .accept = rose_accept,
  1202. .getname = rose_getname,
  1203. .poll = datagram_poll,
  1204. .ioctl = rose_ioctl,
  1205. .listen = rose_listen,
  1206. .shutdown = sock_no_shutdown,
  1207. .setsockopt = rose_setsockopt,
  1208. .getsockopt = rose_getsockopt,
  1209. .sendmsg = rose_sendmsg,
  1210. .recvmsg = rose_recvmsg,
  1211. .mmap = sock_no_mmap,
  1212. .sendpage = sock_no_sendpage,
  1213. };
  1214. static struct notifier_block rose_dev_notifier = {
  1215. .notifier_call = rose_device_event,
  1216. };
  1217. static struct net_device **dev_rose;
  1218. static struct ax25_protocol rose_pid = {
  1219. .pid = AX25_P_ROSE,
  1220. .func = rose_route_frame
  1221. };
  1222. static struct ax25_linkfail rose_linkfail_notifier = {
  1223. .func = rose_link_failed
  1224. };
  1225. static int __init rose_proto_init(void)
  1226. {
  1227. int i;
  1228. int rc;
  1229. if (rose_ndevs > 0x7FFFFFFF/sizeof(struct net_device *)) {
  1230. printk(KERN_ERR "ROSE: rose_proto_init - rose_ndevs parameter to large\n");
  1231. rc = -EINVAL;
  1232. goto out;
  1233. }
  1234. rc = proto_register(&rose_proto, 0);
  1235. if (rc != 0)
  1236. goto out;
  1237. rose_callsign = null_ax25_address;
  1238. dev_rose = kcalloc(rose_ndevs, sizeof(struct net_device *),
  1239. GFP_KERNEL);
  1240. if (dev_rose == NULL) {
  1241. printk(KERN_ERR "ROSE: rose_proto_init - unable to allocate device structure\n");
  1242. rc = -ENOMEM;
  1243. goto out_proto_unregister;
  1244. }
  1245. for (i = 0; i < rose_ndevs; i++) {
  1246. struct net_device *dev;
  1247. char name[IFNAMSIZ];
  1248. sprintf(name, "rose%d", i);
  1249. dev = alloc_netdev(0, name, NET_NAME_UNKNOWN, rose_setup);
  1250. if (!dev) {
  1251. printk(KERN_ERR "ROSE: rose_proto_init - unable to allocate memory\n");
  1252. rc = -ENOMEM;
  1253. goto fail;
  1254. }
  1255. rc = register_netdev(dev);
  1256. if (rc) {
  1257. printk(KERN_ERR "ROSE: netdevice registration failed\n");
  1258. free_netdev(dev);
  1259. goto fail;
  1260. }
  1261. rose_set_lockdep_key(dev);
  1262. dev_rose[i] = dev;
  1263. }
  1264. sock_register(&rose_family_ops);
  1265. register_netdevice_notifier(&rose_dev_notifier);
  1266. ax25_register_pid(&rose_pid);
  1267. ax25_linkfail_register(&rose_linkfail_notifier);
  1268. #ifdef CONFIG_SYSCTL
  1269. rose_register_sysctl();
  1270. #endif
  1271. rose_loopback_init();
  1272. rose_add_loopback_neigh();
  1273. proc_create_seq("rose", 0444, init_net.proc_net, &rose_info_seqops);
  1274. proc_create_seq("rose_neigh", 0444, init_net.proc_net,
  1275. &rose_neigh_seqops);
  1276. proc_create_seq("rose_nodes", 0444, init_net.proc_net,
  1277. &rose_node_seqops);
  1278. proc_create_seq("rose_routes", 0444, init_net.proc_net,
  1279. &rose_route_seqops);
  1280. out:
  1281. return rc;
  1282. fail:
  1283. while (--i >= 0) {
  1284. unregister_netdev(dev_rose[i]);
  1285. free_netdev(dev_rose[i]);
  1286. }
  1287. kfree(dev_rose);
  1288. out_proto_unregister:
  1289. proto_unregister(&rose_proto);
  1290. goto out;
  1291. }
  1292. module_init(rose_proto_init);
  1293. module_param(rose_ndevs, int, 0);
  1294. MODULE_PARM_DESC(rose_ndevs, "number of ROSE devices");
  1295. MODULE_AUTHOR("Jonathan Naylor G4KLX <g4klx@g4klx.demon.co.uk>");
  1296. MODULE_DESCRIPTION("The amateur radio ROSE network layer protocol");
  1297. MODULE_LICENSE("GPL");
  1298. MODULE_ALIAS_NETPROTO(PF_ROSE);
  1299. static void __exit rose_exit(void)
  1300. {
  1301. int i;
  1302. remove_proc_entry("rose", init_net.proc_net);
  1303. remove_proc_entry("rose_neigh", init_net.proc_net);
  1304. remove_proc_entry("rose_nodes", init_net.proc_net);
  1305. remove_proc_entry("rose_routes", init_net.proc_net);
  1306. rose_loopback_clear();
  1307. rose_rt_free();
  1308. ax25_protocol_release(AX25_P_ROSE);
  1309. ax25_linkfail_release(&rose_linkfail_notifier);
  1310. if (ax25cmp(&rose_callsign, &null_ax25_address) != 0)
  1311. ax25_listen_release(&rose_callsign, NULL);
  1312. #ifdef CONFIG_SYSCTL
  1313. rose_unregister_sysctl();
  1314. #endif
  1315. unregister_netdevice_notifier(&rose_dev_notifier);
  1316. sock_unregister(PF_ROSE);
  1317. for (i = 0; i < rose_ndevs; i++) {
  1318. struct net_device *dev = dev_rose[i];
  1319. if (dev) {
  1320. unregister_netdev(dev);
  1321. free_netdev(dev);
  1322. }
  1323. }
  1324. kfree(dev_rose);
  1325. proto_unregister(&rose_proto);
  1326. }
  1327. module_exit(rose_exit);