af_netrom.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540
  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 Jonathan Naylor G4KLX (g4klx@g4klx.demon.co.uk)
  8. * Copyright Alan Cox GW4PTS (alan@lxorguk.ukuu.org.uk)
  9. * Copyright Darryl Miles G7LED (dlm@g7led.demon.co.uk)
  10. */
  11. #include <linux/module.h>
  12. #include <linux/moduleparam.h>
  13. #include <linux/capability.h>
  14. #include <linux/errno.h>
  15. #include <linux/types.h>
  16. #include <linux/socket.h>
  17. #include <linux/in.h>
  18. #include <linux/slab.h>
  19. #include <linux/kernel.h>
  20. #include <linux/sched/signal.h>
  21. #include <linux/timer.h>
  22. #include <linux/string.h>
  23. #include <linux/sockios.h>
  24. #include <linux/net.h>
  25. #include <linux/stat.h>
  26. #include <net/ax25.h>
  27. #include <linux/inet.h>
  28. #include <linux/netdevice.h>
  29. #include <linux/if_arp.h>
  30. #include <linux/skbuff.h>
  31. #include <net/net_namespace.h>
  32. #include <net/sock.h>
  33. #include <linux/uaccess.h>
  34. #include <linux/fcntl.h>
  35. #include <linux/termios.h> /* For TIOCINQ/OUTQ */
  36. #include <linux/mm.h>
  37. #include <linux/interrupt.h>
  38. #include <linux/notifier.h>
  39. #include <net/netrom.h>
  40. #include <linux/proc_fs.h>
  41. #include <linux/seq_file.h>
  42. #include <net/ip.h>
  43. #include <net/tcp_states.h>
  44. #include <net/arp.h>
  45. #include <linux/init.h>
  46. static int nr_ndevs = 4;
  47. int sysctl_netrom_default_path_quality = NR_DEFAULT_QUAL;
  48. int sysctl_netrom_obsolescence_count_initialiser = NR_DEFAULT_OBS;
  49. int sysctl_netrom_network_ttl_initialiser = NR_DEFAULT_TTL;
  50. int sysctl_netrom_transport_timeout = NR_DEFAULT_T1;
  51. int sysctl_netrom_transport_maximum_tries = NR_DEFAULT_N2;
  52. int sysctl_netrom_transport_acknowledge_delay = NR_DEFAULT_T2;
  53. int sysctl_netrom_transport_busy_delay = NR_DEFAULT_T4;
  54. int sysctl_netrom_transport_requested_window_size = NR_DEFAULT_WINDOW;
  55. int sysctl_netrom_transport_no_activity_timeout = NR_DEFAULT_IDLE;
  56. int sysctl_netrom_routing_control = NR_DEFAULT_ROUTING;
  57. int sysctl_netrom_link_fails_count = NR_DEFAULT_FAILS;
  58. int sysctl_netrom_reset_circuit = NR_DEFAULT_RESET;
  59. static unsigned short circuit = 0x101;
  60. static HLIST_HEAD(nr_list);
  61. static DEFINE_SPINLOCK(nr_list_lock);
  62. static const struct proto_ops nr_proto_ops;
  63. /*
  64. * NETROM network devices are virtual network devices encapsulating NETROM
  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 nr_netdev_xmit_lock_key;
  70. static struct lock_class_key nr_netdev_addr_lock_key;
  71. static void nr_set_lockdep_one(struct net_device *dev,
  72. struct netdev_queue *txq,
  73. void *_unused)
  74. {
  75. lockdep_set_class(&txq->_xmit_lock, &nr_netdev_xmit_lock_key);
  76. }
  77. static void nr_set_lockdep_key(struct net_device *dev)
  78. {
  79. lockdep_set_class(&dev->addr_list_lock, &nr_netdev_addr_lock_key);
  80. netdev_for_each_tx_queue(dev, nr_set_lockdep_one, NULL);
  81. }
  82. /*
  83. * Socket removal during an interrupt is now safe.
  84. */
  85. static void nr_remove_socket(struct sock *sk)
  86. {
  87. spin_lock_bh(&nr_list_lock);
  88. sk_del_node_init(sk);
  89. spin_unlock_bh(&nr_list_lock);
  90. }
  91. /*
  92. * Kill all bound sockets on a dropped device.
  93. */
  94. static void nr_kill_by_device(struct net_device *dev)
  95. {
  96. struct sock *s;
  97. spin_lock_bh(&nr_list_lock);
  98. sk_for_each(s, &nr_list)
  99. if (nr_sk(s)->device == dev)
  100. nr_disconnect(s, ENETUNREACH);
  101. spin_unlock_bh(&nr_list_lock);
  102. }
  103. /*
  104. * Handle device status changes.
  105. */
  106. static int nr_device_event(struct notifier_block *this, unsigned long event, void *ptr)
  107. {
  108. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  109. if (!net_eq(dev_net(dev), &init_net))
  110. return NOTIFY_DONE;
  111. if (event != NETDEV_DOWN)
  112. return NOTIFY_DONE;
  113. nr_kill_by_device(dev);
  114. nr_rt_device_down(dev);
  115. return NOTIFY_DONE;
  116. }
  117. /*
  118. * Add a socket to the bound sockets list.
  119. */
  120. static void nr_insert_socket(struct sock *sk)
  121. {
  122. spin_lock_bh(&nr_list_lock);
  123. sk_add_node(sk, &nr_list);
  124. spin_unlock_bh(&nr_list_lock);
  125. }
  126. /*
  127. * Find a socket that wants to accept the Connect Request we just
  128. * received.
  129. */
  130. static struct sock *nr_find_listener(ax25_address *addr)
  131. {
  132. struct sock *s;
  133. spin_lock_bh(&nr_list_lock);
  134. sk_for_each(s, &nr_list)
  135. if (!ax25cmp(&nr_sk(s)->source_addr, addr) &&
  136. s->sk_state == TCP_LISTEN) {
  137. sock_hold(s);
  138. goto found;
  139. }
  140. s = NULL;
  141. found:
  142. spin_unlock_bh(&nr_list_lock);
  143. return s;
  144. }
  145. /*
  146. * Find a connected NET/ROM socket given my circuit IDs.
  147. */
  148. static struct sock *nr_find_socket(unsigned char index, unsigned char id)
  149. {
  150. struct sock *s;
  151. spin_lock_bh(&nr_list_lock);
  152. sk_for_each(s, &nr_list) {
  153. struct nr_sock *nr = nr_sk(s);
  154. if (nr->my_index == index && nr->my_id == id) {
  155. sock_hold(s);
  156. goto found;
  157. }
  158. }
  159. s = NULL;
  160. found:
  161. spin_unlock_bh(&nr_list_lock);
  162. return s;
  163. }
  164. /*
  165. * Find a connected NET/ROM socket given their circuit IDs.
  166. */
  167. static struct sock *nr_find_peer(unsigned char index, unsigned char id,
  168. ax25_address *dest)
  169. {
  170. struct sock *s;
  171. spin_lock_bh(&nr_list_lock);
  172. sk_for_each(s, &nr_list) {
  173. struct nr_sock *nr = nr_sk(s);
  174. if (nr->your_index == index && nr->your_id == id &&
  175. !ax25cmp(&nr->dest_addr, dest)) {
  176. sock_hold(s);
  177. goto found;
  178. }
  179. }
  180. s = NULL;
  181. found:
  182. spin_unlock_bh(&nr_list_lock);
  183. return s;
  184. }
  185. /*
  186. * Find next free circuit ID.
  187. */
  188. static unsigned short nr_find_next_circuit(void)
  189. {
  190. unsigned short id = circuit;
  191. unsigned char i, j;
  192. struct sock *sk;
  193. for (;;) {
  194. i = id / 256;
  195. j = id % 256;
  196. if (i != 0 && j != 0) {
  197. if ((sk=nr_find_socket(i, j)) == NULL)
  198. break;
  199. sock_put(sk);
  200. }
  201. id++;
  202. }
  203. return id;
  204. }
  205. /*
  206. * Deferred destroy.
  207. */
  208. void nr_destroy_socket(struct sock *);
  209. /*
  210. * Handler for deferred kills.
  211. */
  212. static void nr_destroy_timer(struct timer_list *t)
  213. {
  214. struct sock *sk = from_timer(sk, t, sk_timer);
  215. bh_lock_sock(sk);
  216. sock_hold(sk);
  217. nr_destroy_socket(sk);
  218. bh_unlock_sock(sk);
  219. sock_put(sk);
  220. }
  221. /*
  222. * This is called from user mode and the timers. Thus it protects itself
  223. * against interrupt users but doesn't worry about being called during
  224. * work. Once it is removed from the queue no interrupt or bottom half
  225. * will touch it and we are (fairly 8-) ) safe.
  226. */
  227. void nr_destroy_socket(struct sock *sk)
  228. {
  229. struct sk_buff *skb;
  230. nr_remove_socket(sk);
  231. nr_stop_heartbeat(sk);
  232. nr_stop_t1timer(sk);
  233. nr_stop_t2timer(sk);
  234. nr_stop_t4timer(sk);
  235. nr_stop_idletimer(sk);
  236. nr_clear_queues(sk); /* Flush the queues */
  237. while ((skb = skb_dequeue(&sk->sk_receive_queue)) != NULL) {
  238. if (skb->sk != sk) { /* A pending connection */
  239. /* Queue the unaccepted socket for death */
  240. sock_set_flag(skb->sk, SOCK_DEAD);
  241. nr_start_heartbeat(skb->sk);
  242. nr_sk(skb->sk)->state = NR_STATE_0;
  243. }
  244. kfree_skb(skb);
  245. }
  246. if (sk_has_allocations(sk)) {
  247. /* Defer: outstanding buffers */
  248. sk->sk_timer.function = nr_destroy_timer;
  249. sk->sk_timer.expires = jiffies + 2 * HZ;
  250. add_timer(&sk->sk_timer);
  251. } else
  252. sock_put(sk);
  253. }
  254. /*
  255. * Handling for system calls applied via the various interfaces to a
  256. * NET/ROM socket object.
  257. */
  258. static int nr_setsockopt(struct socket *sock, int level, int optname,
  259. char __user *optval, unsigned int optlen)
  260. {
  261. struct sock *sk = sock->sk;
  262. struct nr_sock *nr = nr_sk(sk);
  263. unsigned long opt;
  264. if (level != SOL_NETROM)
  265. return -ENOPROTOOPT;
  266. if (optlen < sizeof(unsigned int))
  267. return -EINVAL;
  268. if (get_user(opt, (unsigned int __user *)optval))
  269. return -EFAULT;
  270. switch (optname) {
  271. case NETROM_T1:
  272. if (opt < 1 || opt > ULONG_MAX / HZ)
  273. return -EINVAL;
  274. nr->t1 = opt * HZ;
  275. return 0;
  276. case NETROM_T2:
  277. if (opt < 1 || opt > ULONG_MAX / HZ)
  278. return -EINVAL;
  279. nr->t2 = opt * HZ;
  280. return 0;
  281. case NETROM_N2:
  282. if (opt < 1 || opt > 31)
  283. return -EINVAL;
  284. nr->n2 = opt;
  285. return 0;
  286. case NETROM_T4:
  287. if (opt < 1 || opt > ULONG_MAX / HZ)
  288. return -EINVAL;
  289. nr->t4 = opt * HZ;
  290. return 0;
  291. case NETROM_IDLE:
  292. if (opt > ULONG_MAX / (60 * HZ))
  293. return -EINVAL;
  294. nr->idle = opt * 60 * HZ;
  295. return 0;
  296. default:
  297. return -ENOPROTOOPT;
  298. }
  299. }
  300. static int nr_getsockopt(struct socket *sock, int level, int optname,
  301. char __user *optval, int __user *optlen)
  302. {
  303. struct sock *sk = sock->sk;
  304. struct nr_sock *nr = nr_sk(sk);
  305. int val = 0;
  306. int len;
  307. if (level != SOL_NETROM)
  308. return -ENOPROTOOPT;
  309. if (get_user(len, optlen))
  310. return -EFAULT;
  311. if (len < 0)
  312. return -EINVAL;
  313. switch (optname) {
  314. case NETROM_T1:
  315. val = nr->t1 / HZ;
  316. break;
  317. case NETROM_T2:
  318. val = nr->t2 / HZ;
  319. break;
  320. case NETROM_N2:
  321. val = nr->n2;
  322. break;
  323. case NETROM_T4:
  324. val = nr->t4 / HZ;
  325. break;
  326. case NETROM_IDLE:
  327. val = nr->idle / (60 * HZ);
  328. break;
  329. default:
  330. return -ENOPROTOOPT;
  331. }
  332. len = min_t(unsigned int, len, sizeof(int));
  333. if (put_user(len, optlen))
  334. return -EFAULT;
  335. return copy_to_user(optval, &val, len) ? -EFAULT : 0;
  336. }
  337. static int nr_listen(struct socket *sock, int backlog)
  338. {
  339. struct sock *sk = sock->sk;
  340. lock_sock(sk);
  341. if (sk->sk_state != TCP_LISTEN) {
  342. memset(&nr_sk(sk)->user_addr, 0, AX25_ADDR_LEN);
  343. sk->sk_max_ack_backlog = backlog;
  344. sk->sk_state = TCP_LISTEN;
  345. release_sock(sk);
  346. return 0;
  347. }
  348. release_sock(sk);
  349. return -EOPNOTSUPP;
  350. }
  351. static struct proto nr_proto = {
  352. .name = "NETROM",
  353. .owner = THIS_MODULE,
  354. .obj_size = sizeof(struct nr_sock),
  355. };
  356. static int nr_create(struct net *net, struct socket *sock, int protocol,
  357. int kern)
  358. {
  359. struct sock *sk;
  360. struct nr_sock *nr;
  361. if (!net_eq(net, &init_net))
  362. return -EAFNOSUPPORT;
  363. if (sock->type != SOCK_SEQPACKET || protocol != 0)
  364. return -ESOCKTNOSUPPORT;
  365. sk = sk_alloc(net, PF_NETROM, GFP_ATOMIC, &nr_proto, kern);
  366. if (sk == NULL)
  367. return -ENOMEM;
  368. nr = nr_sk(sk);
  369. sock_init_data(sock, sk);
  370. sock->ops = &nr_proto_ops;
  371. sk->sk_protocol = protocol;
  372. skb_queue_head_init(&nr->ack_queue);
  373. skb_queue_head_init(&nr->reseq_queue);
  374. skb_queue_head_init(&nr->frag_queue);
  375. nr_init_timers(sk);
  376. nr->t1 =
  377. msecs_to_jiffies(sysctl_netrom_transport_timeout);
  378. nr->t2 =
  379. msecs_to_jiffies(sysctl_netrom_transport_acknowledge_delay);
  380. nr->n2 =
  381. msecs_to_jiffies(sysctl_netrom_transport_maximum_tries);
  382. nr->t4 =
  383. msecs_to_jiffies(sysctl_netrom_transport_busy_delay);
  384. nr->idle =
  385. msecs_to_jiffies(sysctl_netrom_transport_no_activity_timeout);
  386. nr->window = sysctl_netrom_transport_requested_window_size;
  387. nr->bpqext = 1;
  388. nr->state = NR_STATE_0;
  389. return 0;
  390. }
  391. static struct sock *nr_make_new(struct sock *osk)
  392. {
  393. struct sock *sk;
  394. struct nr_sock *nr, *onr;
  395. if (osk->sk_type != SOCK_SEQPACKET)
  396. return NULL;
  397. sk = sk_alloc(sock_net(osk), PF_NETROM, GFP_ATOMIC, osk->sk_prot, 0);
  398. if (sk == NULL)
  399. return NULL;
  400. nr = nr_sk(sk);
  401. sock_init_data(NULL, sk);
  402. sk->sk_type = osk->sk_type;
  403. sk->sk_priority = osk->sk_priority;
  404. sk->sk_protocol = osk->sk_protocol;
  405. sk->sk_rcvbuf = osk->sk_rcvbuf;
  406. sk->sk_sndbuf = osk->sk_sndbuf;
  407. sk->sk_state = TCP_ESTABLISHED;
  408. sock_copy_flags(sk, osk);
  409. skb_queue_head_init(&nr->ack_queue);
  410. skb_queue_head_init(&nr->reseq_queue);
  411. skb_queue_head_init(&nr->frag_queue);
  412. nr_init_timers(sk);
  413. onr = nr_sk(osk);
  414. nr->t1 = onr->t1;
  415. nr->t2 = onr->t2;
  416. nr->n2 = onr->n2;
  417. nr->t4 = onr->t4;
  418. nr->idle = onr->idle;
  419. nr->window = onr->window;
  420. nr->device = onr->device;
  421. nr->bpqext = onr->bpqext;
  422. return sk;
  423. }
  424. static int nr_release(struct socket *sock)
  425. {
  426. struct sock *sk = sock->sk;
  427. struct nr_sock *nr;
  428. if (sk == NULL) return 0;
  429. sock_hold(sk);
  430. sock_orphan(sk);
  431. lock_sock(sk);
  432. nr = nr_sk(sk);
  433. switch (nr->state) {
  434. case NR_STATE_0:
  435. case NR_STATE_1:
  436. case NR_STATE_2:
  437. nr_disconnect(sk, 0);
  438. nr_destroy_socket(sk);
  439. break;
  440. case NR_STATE_3:
  441. nr_clear_queues(sk);
  442. nr->n2count = 0;
  443. nr_write_internal(sk, NR_DISCREQ);
  444. nr_start_t1timer(sk);
  445. nr_stop_t2timer(sk);
  446. nr_stop_t4timer(sk);
  447. nr_stop_idletimer(sk);
  448. nr->state = NR_STATE_2;
  449. sk->sk_state = TCP_CLOSE;
  450. sk->sk_shutdown |= SEND_SHUTDOWN;
  451. sk->sk_state_change(sk);
  452. sock_set_flag(sk, SOCK_DESTROY);
  453. break;
  454. default:
  455. break;
  456. }
  457. sock->sk = NULL;
  458. release_sock(sk);
  459. sock_put(sk);
  460. return 0;
  461. }
  462. static int nr_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
  463. {
  464. struct sock *sk = sock->sk;
  465. struct nr_sock *nr = nr_sk(sk);
  466. struct full_sockaddr_ax25 *addr = (struct full_sockaddr_ax25 *)uaddr;
  467. struct net_device *dev;
  468. ax25_uid_assoc *user;
  469. ax25_address *source;
  470. lock_sock(sk);
  471. if (!sock_flag(sk, SOCK_ZAPPED)) {
  472. release_sock(sk);
  473. return -EINVAL;
  474. }
  475. if (addr_len < sizeof(struct sockaddr_ax25) || addr_len > sizeof(struct full_sockaddr_ax25)) {
  476. release_sock(sk);
  477. return -EINVAL;
  478. }
  479. if (addr_len < (addr->fsa_ax25.sax25_ndigis * sizeof(ax25_address) + sizeof(struct sockaddr_ax25))) {
  480. release_sock(sk);
  481. return -EINVAL;
  482. }
  483. if (addr->fsa_ax25.sax25_family != AF_NETROM) {
  484. release_sock(sk);
  485. return -EINVAL;
  486. }
  487. if ((dev = nr_dev_get(&addr->fsa_ax25.sax25_call)) == NULL) {
  488. release_sock(sk);
  489. return -EADDRNOTAVAIL;
  490. }
  491. /*
  492. * Only the super user can set an arbitrary user callsign.
  493. */
  494. if (addr->fsa_ax25.sax25_ndigis == 1) {
  495. if (!capable(CAP_NET_BIND_SERVICE)) {
  496. dev_put(dev);
  497. release_sock(sk);
  498. return -EPERM;
  499. }
  500. nr->user_addr = addr->fsa_digipeater[0];
  501. nr->source_addr = addr->fsa_ax25.sax25_call;
  502. } else {
  503. source = &addr->fsa_ax25.sax25_call;
  504. user = ax25_findbyuid(current_euid());
  505. if (user) {
  506. nr->user_addr = user->call;
  507. ax25_uid_put(user);
  508. } else {
  509. if (ax25_uid_policy && !capable(CAP_NET_BIND_SERVICE)) {
  510. release_sock(sk);
  511. dev_put(dev);
  512. return -EPERM;
  513. }
  514. nr->user_addr = *source;
  515. }
  516. nr->source_addr = *source;
  517. }
  518. nr->device = dev;
  519. nr_insert_socket(sk);
  520. sock_reset_flag(sk, SOCK_ZAPPED);
  521. dev_put(dev);
  522. release_sock(sk);
  523. return 0;
  524. }
  525. static int nr_connect(struct socket *sock, struct sockaddr *uaddr,
  526. int addr_len, int flags)
  527. {
  528. struct sock *sk = sock->sk;
  529. struct nr_sock *nr = nr_sk(sk);
  530. struct sockaddr_ax25 *addr = (struct sockaddr_ax25 *)uaddr;
  531. ax25_address *source = NULL;
  532. ax25_uid_assoc *user;
  533. struct net_device *dev;
  534. int err = 0;
  535. lock_sock(sk);
  536. if (sk->sk_state == TCP_ESTABLISHED && sock->state == SS_CONNECTING) {
  537. sock->state = SS_CONNECTED;
  538. goto out_release; /* Connect completed during a ERESTARTSYS event */
  539. }
  540. if (sk->sk_state == TCP_CLOSE && sock->state == SS_CONNECTING) {
  541. sock->state = SS_UNCONNECTED;
  542. err = -ECONNREFUSED;
  543. goto out_release;
  544. }
  545. if (sk->sk_state == TCP_ESTABLISHED) {
  546. err = -EISCONN; /* No reconnect on a seqpacket socket */
  547. goto out_release;
  548. }
  549. sk->sk_state = TCP_CLOSE;
  550. sock->state = SS_UNCONNECTED;
  551. if (addr_len != sizeof(struct sockaddr_ax25) && addr_len != sizeof(struct full_sockaddr_ax25)) {
  552. err = -EINVAL;
  553. goto out_release;
  554. }
  555. if (addr->sax25_family != AF_NETROM) {
  556. err = -EINVAL;
  557. goto out_release;
  558. }
  559. if (sock_flag(sk, SOCK_ZAPPED)) { /* Must bind first - autobinding in this may or may not work */
  560. sock_reset_flag(sk, SOCK_ZAPPED);
  561. if ((dev = nr_dev_first()) == NULL) {
  562. err = -ENETUNREACH;
  563. goto out_release;
  564. }
  565. source = (ax25_address *)dev->dev_addr;
  566. user = ax25_findbyuid(current_euid());
  567. if (user) {
  568. nr->user_addr = user->call;
  569. ax25_uid_put(user);
  570. } else {
  571. if (ax25_uid_policy && !capable(CAP_NET_ADMIN)) {
  572. dev_put(dev);
  573. err = -EPERM;
  574. goto out_release;
  575. }
  576. nr->user_addr = *source;
  577. }
  578. nr->source_addr = *source;
  579. nr->device = dev;
  580. dev_put(dev);
  581. nr_insert_socket(sk); /* Finish the bind */
  582. }
  583. nr->dest_addr = addr->sax25_call;
  584. release_sock(sk);
  585. circuit = nr_find_next_circuit();
  586. lock_sock(sk);
  587. nr->my_index = circuit / 256;
  588. nr->my_id = circuit % 256;
  589. circuit++;
  590. /* Move to connecting socket, start sending Connect Requests */
  591. sock->state = SS_CONNECTING;
  592. sk->sk_state = TCP_SYN_SENT;
  593. nr_establish_data_link(sk);
  594. nr->state = NR_STATE_1;
  595. nr_start_heartbeat(sk);
  596. /* Now the loop */
  597. if (sk->sk_state != TCP_ESTABLISHED && (flags & O_NONBLOCK)) {
  598. err = -EINPROGRESS;
  599. goto out_release;
  600. }
  601. /*
  602. * A Connect Ack with Choke or timeout or failed routing will go to
  603. * closed.
  604. */
  605. if (sk->sk_state == TCP_SYN_SENT) {
  606. DEFINE_WAIT(wait);
  607. for (;;) {
  608. prepare_to_wait(sk_sleep(sk), &wait,
  609. TASK_INTERRUPTIBLE);
  610. if (sk->sk_state != TCP_SYN_SENT)
  611. break;
  612. if (!signal_pending(current)) {
  613. release_sock(sk);
  614. schedule();
  615. lock_sock(sk);
  616. continue;
  617. }
  618. err = -ERESTARTSYS;
  619. break;
  620. }
  621. finish_wait(sk_sleep(sk), &wait);
  622. if (err)
  623. goto out_release;
  624. }
  625. if (sk->sk_state != TCP_ESTABLISHED) {
  626. sock->state = SS_UNCONNECTED;
  627. err = sock_error(sk); /* Always set at this point */
  628. goto out_release;
  629. }
  630. sock->state = SS_CONNECTED;
  631. out_release:
  632. release_sock(sk);
  633. return err;
  634. }
  635. static int nr_accept(struct socket *sock, struct socket *newsock, int flags,
  636. bool kern)
  637. {
  638. struct sk_buff *skb;
  639. struct sock *newsk;
  640. DEFINE_WAIT(wait);
  641. struct sock *sk;
  642. int err = 0;
  643. if ((sk = sock->sk) == NULL)
  644. return -EINVAL;
  645. lock_sock(sk);
  646. if (sk->sk_type != SOCK_SEQPACKET) {
  647. err = -EOPNOTSUPP;
  648. goto out_release;
  649. }
  650. if (sk->sk_state != TCP_LISTEN) {
  651. err = -EINVAL;
  652. goto out_release;
  653. }
  654. /*
  655. * The write queue this time is holding sockets ready to use
  656. * hooked into the SABM we saved
  657. */
  658. for (;;) {
  659. prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
  660. skb = skb_dequeue(&sk->sk_receive_queue);
  661. if (skb)
  662. break;
  663. if (flags & O_NONBLOCK) {
  664. err = -EWOULDBLOCK;
  665. break;
  666. }
  667. if (!signal_pending(current)) {
  668. release_sock(sk);
  669. schedule();
  670. lock_sock(sk);
  671. continue;
  672. }
  673. err = -ERESTARTSYS;
  674. break;
  675. }
  676. finish_wait(sk_sleep(sk), &wait);
  677. if (err)
  678. goto out_release;
  679. newsk = skb->sk;
  680. sock_graft(newsk, newsock);
  681. /* Now attach up the new socket */
  682. kfree_skb(skb);
  683. sk_acceptq_removed(sk);
  684. out_release:
  685. release_sock(sk);
  686. return err;
  687. }
  688. static int nr_getname(struct socket *sock, struct sockaddr *uaddr,
  689. int peer)
  690. {
  691. struct full_sockaddr_ax25 *sax = (struct full_sockaddr_ax25 *)uaddr;
  692. struct sock *sk = sock->sk;
  693. struct nr_sock *nr = nr_sk(sk);
  694. int uaddr_len;
  695. memset(&sax->fsa_ax25, 0, sizeof(struct sockaddr_ax25));
  696. lock_sock(sk);
  697. if (peer != 0) {
  698. if (sk->sk_state != TCP_ESTABLISHED) {
  699. release_sock(sk);
  700. return -ENOTCONN;
  701. }
  702. sax->fsa_ax25.sax25_family = AF_NETROM;
  703. sax->fsa_ax25.sax25_ndigis = 1;
  704. sax->fsa_ax25.sax25_call = nr->user_addr;
  705. memset(sax->fsa_digipeater, 0, sizeof(sax->fsa_digipeater));
  706. sax->fsa_digipeater[0] = nr->dest_addr;
  707. uaddr_len = sizeof(struct full_sockaddr_ax25);
  708. } else {
  709. sax->fsa_ax25.sax25_family = AF_NETROM;
  710. sax->fsa_ax25.sax25_ndigis = 0;
  711. sax->fsa_ax25.sax25_call = nr->source_addr;
  712. uaddr_len = sizeof(struct sockaddr_ax25);
  713. }
  714. release_sock(sk);
  715. return uaddr_len;
  716. }
  717. int nr_rx_frame(struct sk_buff *skb, struct net_device *dev)
  718. {
  719. struct sock *sk;
  720. struct sock *make;
  721. struct nr_sock *nr_make;
  722. ax25_address *src, *dest, *user;
  723. unsigned short circuit_index, circuit_id;
  724. unsigned short peer_circuit_index, peer_circuit_id;
  725. unsigned short frametype, flags, window, timeout;
  726. int ret;
  727. skb_orphan(skb);
  728. /*
  729. * skb->data points to the netrom frame start
  730. */
  731. src = (ax25_address *)(skb->data + 0);
  732. dest = (ax25_address *)(skb->data + 7);
  733. circuit_index = skb->data[15];
  734. circuit_id = skb->data[16];
  735. peer_circuit_index = skb->data[17];
  736. peer_circuit_id = skb->data[18];
  737. frametype = skb->data[19] & 0x0F;
  738. flags = skb->data[19] & 0xF0;
  739. /*
  740. * Check for an incoming IP over NET/ROM frame.
  741. */
  742. if (frametype == NR_PROTOEXT &&
  743. circuit_index == NR_PROTO_IP && circuit_id == NR_PROTO_IP) {
  744. skb_pull(skb, NR_NETWORK_LEN + NR_TRANSPORT_LEN);
  745. skb_reset_transport_header(skb);
  746. return nr_rx_ip(skb, dev);
  747. }
  748. /*
  749. * Find an existing socket connection, based on circuit ID, if it's
  750. * a Connect Request base it on their circuit ID.
  751. *
  752. * Circuit ID 0/0 is not valid but it could still be a "reset" for a
  753. * circuit that no longer exists at the other end ...
  754. */
  755. sk = NULL;
  756. if (circuit_index == 0 && circuit_id == 0) {
  757. if (frametype == NR_CONNACK && flags == NR_CHOKE_FLAG)
  758. sk = nr_find_peer(peer_circuit_index, peer_circuit_id, src);
  759. } else {
  760. if (frametype == NR_CONNREQ)
  761. sk = nr_find_peer(circuit_index, circuit_id, src);
  762. else
  763. sk = nr_find_socket(circuit_index, circuit_id);
  764. }
  765. if (sk != NULL) {
  766. bh_lock_sock(sk);
  767. skb_reset_transport_header(skb);
  768. if (frametype == NR_CONNACK && skb->len == 22)
  769. nr_sk(sk)->bpqext = 1;
  770. else
  771. nr_sk(sk)->bpqext = 0;
  772. ret = nr_process_rx_frame(sk, skb);
  773. bh_unlock_sock(sk);
  774. sock_put(sk);
  775. return ret;
  776. }
  777. /*
  778. * Now it should be a CONNREQ.
  779. */
  780. if (frametype != NR_CONNREQ) {
  781. /*
  782. * Here it would be nice to be able to send a reset but
  783. * NET/ROM doesn't have one. We've tried to extend the protocol
  784. * by sending NR_CONNACK | NR_CHOKE_FLAGS replies but that
  785. * apparently kills BPQ boxes... :-(
  786. * So now we try to follow the established behaviour of
  787. * G8PZT's Xrouter which is sending packets with command type 7
  788. * as an extension of the protocol.
  789. */
  790. if (sysctl_netrom_reset_circuit &&
  791. (frametype != NR_RESET || flags != 0))
  792. nr_transmit_reset(skb, 1);
  793. return 0;
  794. }
  795. sk = nr_find_listener(dest);
  796. user = (ax25_address *)(skb->data + 21);
  797. if (sk == NULL || sk_acceptq_is_full(sk) ||
  798. (make = nr_make_new(sk)) == NULL) {
  799. nr_transmit_refusal(skb, 0);
  800. if (sk)
  801. sock_put(sk);
  802. return 0;
  803. }
  804. bh_lock_sock(sk);
  805. window = skb->data[20];
  806. sock_hold(make);
  807. skb->sk = make;
  808. skb->destructor = sock_efree;
  809. make->sk_state = TCP_ESTABLISHED;
  810. /* Fill in his circuit details */
  811. nr_make = nr_sk(make);
  812. nr_make->source_addr = *dest;
  813. nr_make->dest_addr = *src;
  814. nr_make->user_addr = *user;
  815. nr_make->your_index = circuit_index;
  816. nr_make->your_id = circuit_id;
  817. bh_unlock_sock(sk);
  818. circuit = nr_find_next_circuit();
  819. bh_lock_sock(sk);
  820. nr_make->my_index = circuit / 256;
  821. nr_make->my_id = circuit % 256;
  822. circuit++;
  823. /* Window negotiation */
  824. if (window < nr_make->window)
  825. nr_make->window = window;
  826. /* L4 timeout negotiation */
  827. if (skb->len == 37) {
  828. timeout = skb->data[36] * 256 + skb->data[35];
  829. if (timeout * HZ < nr_make->t1)
  830. nr_make->t1 = timeout * HZ;
  831. nr_make->bpqext = 1;
  832. } else {
  833. nr_make->bpqext = 0;
  834. }
  835. nr_write_internal(make, NR_CONNACK);
  836. nr_make->condition = 0x00;
  837. nr_make->vs = 0;
  838. nr_make->va = 0;
  839. nr_make->vr = 0;
  840. nr_make->vl = 0;
  841. nr_make->state = NR_STATE_3;
  842. sk_acceptq_added(sk);
  843. skb_queue_head(&sk->sk_receive_queue, skb);
  844. if (!sock_flag(sk, SOCK_DEAD))
  845. sk->sk_data_ready(sk);
  846. bh_unlock_sock(sk);
  847. sock_put(sk);
  848. nr_insert_socket(make);
  849. nr_start_heartbeat(make);
  850. nr_start_idletimer(make);
  851. return 1;
  852. }
  853. static int nr_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
  854. {
  855. struct sock *sk = sock->sk;
  856. struct nr_sock *nr = nr_sk(sk);
  857. DECLARE_SOCKADDR(struct sockaddr_ax25 *, usax, msg->msg_name);
  858. int err;
  859. struct sockaddr_ax25 sax;
  860. struct sk_buff *skb;
  861. unsigned char *asmptr;
  862. int size;
  863. if (msg->msg_flags & ~(MSG_DONTWAIT|MSG_EOR|MSG_CMSG_COMPAT))
  864. return -EINVAL;
  865. lock_sock(sk);
  866. if (sock_flag(sk, SOCK_ZAPPED)) {
  867. err = -EADDRNOTAVAIL;
  868. goto out;
  869. }
  870. if (sk->sk_shutdown & SEND_SHUTDOWN) {
  871. send_sig(SIGPIPE, current, 0);
  872. err = -EPIPE;
  873. goto out;
  874. }
  875. if (nr->device == NULL) {
  876. err = -ENETUNREACH;
  877. goto out;
  878. }
  879. if (usax) {
  880. if (msg->msg_namelen < sizeof(sax)) {
  881. err = -EINVAL;
  882. goto out;
  883. }
  884. sax = *usax;
  885. if (ax25cmp(&nr->dest_addr, &sax.sax25_call) != 0) {
  886. err = -EISCONN;
  887. goto out;
  888. }
  889. if (sax.sax25_family != AF_NETROM) {
  890. err = -EINVAL;
  891. goto out;
  892. }
  893. } else {
  894. if (sk->sk_state != TCP_ESTABLISHED) {
  895. err = -ENOTCONN;
  896. goto out;
  897. }
  898. sax.sax25_family = AF_NETROM;
  899. sax.sax25_call = nr->dest_addr;
  900. }
  901. /* Build a packet - the conventional user limit is 236 bytes. We can
  902. do ludicrously large NetROM frames but must not overflow */
  903. if (len > 65536) {
  904. err = -EMSGSIZE;
  905. goto out;
  906. }
  907. size = len + NR_NETWORK_LEN + NR_TRANSPORT_LEN;
  908. if ((skb = sock_alloc_send_skb(sk, size, msg->msg_flags & MSG_DONTWAIT, &err)) == NULL)
  909. goto out;
  910. skb_reserve(skb, size - len);
  911. skb_reset_transport_header(skb);
  912. /*
  913. * Push down the NET/ROM header
  914. */
  915. asmptr = skb_push(skb, NR_TRANSPORT_LEN);
  916. /* Build a NET/ROM Transport header */
  917. *asmptr++ = nr->your_index;
  918. *asmptr++ = nr->your_id;
  919. *asmptr++ = 0; /* To be filled in later */
  920. *asmptr++ = 0; /* Ditto */
  921. *asmptr++ = NR_INFO;
  922. /*
  923. * Put the data on the end
  924. */
  925. skb_put(skb, len);
  926. /* User data follows immediately after the NET/ROM transport header */
  927. if (memcpy_from_msg(skb_transport_header(skb), msg, len)) {
  928. kfree_skb(skb);
  929. err = -EFAULT;
  930. goto out;
  931. }
  932. if (sk->sk_state != TCP_ESTABLISHED) {
  933. kfree_skb(skb);
  934. err = -ENOTCONN;
  935. goto out;
  936. }
  937. nr_output(sk, skb); /* Shove it onto the queue */
  938. err = len;
  939. out:
  940. release_sock(sk);
  941. return err;
  942. }
  943. static int nr_recvmsg(struct socket *sock, struct msghdr *msg, size_t size,
  944. int flags)
  945. {
  946. struct sock *sk = sock->sk;
  947. DECLARE_SOCKADDR(struct sockaddr_ax25 *, sax, msg->msg_name);
  948. size_t copied;
  949. struct sk_buff *skb;
  950. int er;
  951. /*
  952. * This works for seqpacket too. The receiver has ordered the queue for
  953. * us! We do one quick check first though
  954. */
  955. lock_sock(sk);
  956. if (sk->sk_state != TCP_ESTABLISHED) {
  957. release_sock(sk);
  958. return -ENOTCONN;
  959. }
  960. /* Now we can treat all alike */
  961. if ((skb = skb_recv_datagram(sk, flags & ~MSG_DONTWAIT, flags & MSG_DONTWAIT, &er)) == NULL) {
  962. release_sock(sk);
  963. return er;
  964. }
  965. skb_reset_transport_header(skb);
  966. copied = skb->len;
  967. if (copied > size) {
  968. copied = size;
  969. msg->msg_flags |= MSG_TRUNC;
  970. }
  971. er = skb_copy_datagram_msg(skb, 0, msg, copied);
  972. if (er < 0) {
  973. skb_free_datagram(sk, skb);
  974. release_sock(sk);
  975. return er;
  976. }
  977. if (sax != NULL) {
  978. memset(sax, 0, sizeof(*sax));
  979. sax->sax25_family = AF_NETROM;
  980. skb_copy_from_linear_data_offset(skb, 7, sax->sax25_call.ax25_call,
  981. AX25_ADDR_LEN);
  982. msg->msg_namelen = sizeof(*sax);
  983. }
  984. skb_free_datagram(sk, skb);
  985. release_sock(sk);
  986. return copied;
  987. }
  988. static int nr_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
  989. {
  990. struct sock *sk = sock->sk;
  991. void __user *argp = (void __user *)arg;
  992. int ret;
  993. switch (cmd) {
  994. case TIOCOUTQ: {
  995. long amount;
  996. lock_sock(sk);
  997. amount = sk->sk_sndbuf - sk_wmem_alloc_get(sk);
  998. if (amount < 0)
  999. amount = 0;
  1000. release_sock(sk);
  1001. return put_user(amount, (int __user *)argp);
  1002. }
  1003. case TIOCINQ: {
  1004. struct sk_buff *skb;
  1005. long amount = 0L;
  1006. lock_sock(sk);
  1007. /* These two are safe on a single CPU system as only user tasks fiddle here */
  1008. if ((skb = skb_peek(&sk->sk_receive_queue)) != NULL)
  1009. amount = skb->len;
  1010. release_sock(sk);
  1011. return put_user(amount, (int __user *)argp);
  1012. }
  1013. case SIOCGSTAMP:
  1014. lock_sock(sk);
  1015. ret = sock_get_timestamp(sk, argp);
  1016. release_sock(sk);
  1017. return ret;
  1018. case SIOCGSTAMPNS:
  1019. lock_sock(sk);
  1020. ret = sock_get_timestampns(sk, argp);
  1021. release_sock(sk);
  1022. return ret;
  1023. case SIOCGIFADDR:
  1024. case SIOCSIFADDR:
  1025. case SIOCGIFDSTADDR:
  1026. case SIOCSIFDSTADDR:
  1027. case SIOCGIFBRDADDR:
  1028. case SIOCSIFBRDADDR:
  1029. case SIOCGIFNETMASK:
  1030. case SIOCSIFNETMASK:
  1031. case SIOCGIFMETRIC:
  1032. case SIOCSIFMETRIC:
  1033. return -EINVAL;
  1034. case SIOCADDRT:
  1035. case SIOCDELRT:
  1036. case SIOCNRDECOBS:
  1037. if (!capable(CAP_NET_ADMIN))
  1038. return -EPERM;
  1039. return nr_rt_ioctl(cmd, argp);
  1040. default:
  1041. return -ENOIOCTLCMD;
  1042. }
  1043. return 0;
  1044. }
  1045. #ifdef CONFIG_PROC_FS
  1046. static void *nr_info_start(struct seq_file *seq, loff_t *pos)
  1047. {
  1048. spin_lock_bh(&nr_list_lock);
  1049. return seq_hlist_start_head(&nr_list, *pos);
  1050. }
  1051. static void *nr_info_next(struct seq_file *seq, void *v, loff_t *pos)
  1052. {
  1053. return seq_hlist_next(v, &nr_list, pos);
  1054. }
  1055. static void nr_info_stop(struct seq_file *seq, void *v)
  1056. {
  1057. spin_unlock_bh(&nr_list_lock);
  1058. }
  1059. static int nr_info_show(struct seq_file *seq, void *v)
  1060. {
  1061. struct sock *s = sk_entry(v);
  1062. struct net_device *dev;
  1063. struct nr_sock *nr;
  1064. const char *devname;
  1065. char buf[11];
  1066. if (v == SEQ_START_TOKEN)
  1067. seq_puts(seq,
  1068. "user_addr dest_node src_node dev my your st vs vr va t1 t2 t4 idle n2 wnd Snd-Q Rcv-Q inode\n");
  1069. else {
  1070. bh_lock_sock(s);
  1071. nr = nr_sk(s);
  1072. if ((dev = nr->device) == NULL)
  1073. devname = "???";
  1074. else
  1075. devname = dev->name;
  1076. seq_printf(seq, "%-9s ", ax2asc(buf, &nr->user_addr));
  1077. seq_printf(seq, "%-9s ", ax2asc(buf, &nr->dest_addr));
  1078. seq_printf(seq,
  1079. "%-9s %-3s %02X/%02X %02X/%02X %2d %3d %3d %3d %3lu/%03lu %2lu/%02lu %3lu/%03lu %3lu/%03lu %2d/%02d %3d %5d %5d %ld\n",
  1080. ax2asc(buf, &nr->source_addr),
  1081. devname,
  1082. nr->my_index,
  1083. nr->my_id,
  1084. nr->your_index,
  1085. nr->your_id,
  1086. nr->state,
  1087. nr->vs,
  1088. nr->vr,
  1089. nr->va,
  1090. ax25_display_timer(&nr->t1timer) / HZ,
  1091. nr->t1 / HZ,
  1092. ax25_display_timer(&nr->t2timer) / HZ,
  1093. nr->t2 / HZ,
  1094. ax25_display_timer(&nr->t4timer) / HZ,
  1095. nr->t4 / HZ,
  1096. ax25_display_timer(&nr->idletimer) / (60 * HZ),
  1097. nr->idle / (60 * HZ),
  1098. nr->n2count,
  1099. nr->n2,
  1100. nr->window,
  1101. sk_wmem_alloc_get(s),
  1102. sk_rmem_alloc_get(s),
  1103. s->sk_socket ? SOCK_INODE(s->sk_socket)->i_ino : 0L);
  1104. bh_unlock_sock(s);
  1105. }
  1106. return 0;
  1107. }
  1108. static const struct seq_operations nr_info_seqops = {
  1109. .start = nr_info_start,
  1110. .next = nr_info_next,
  1111. .stop = nr_info_stop,
  1112. .show = nr_info_show,
  1113. };
  1114. #endif /* CONFIG_PROC_FS */
  1115. static const struct net_proto_family nr_family_ops = {
  1116. .family = PF_NETROM,
  1117. .create = nr_create,
  1118. .owner = THIS_MODULE,
  1119. };
  1120. static const struct proto_ops nr_proto_ops = {
  1121. .family = PF_NETROM,
  1122. .owner = THIS_MODULE,
  1123. .release = nr_release,
  1124. .bind = nr_bind,
  1125. .connect = nr_connect,
  1126. .socketpair = sock_no_socketpair,
  1127. .accept = nr_accept,
  1128. .getname = nr_getname,
  1129. .poll = datagram_poll,
  1130. .ioctl = nr_ioctl,
  1131. .listen = nr_listen,
  1132. .shutdown = sock_no_shutdown,
  1133. .setsockopt = nr_setsockopt,
  1134. .getsockopt = nr_getsockopt,
  1135. .sendmsg = nr_sendmsg,
  1136. .recvmsg = nr_recvmsg,
  1137. .mmap = sock_no_mmap,
  1138. .sendpage = sock_no_sendpage,
  1139. };
  1140. static struct notifier_block nr_dev_notifier = {
  1141. .notifier_call = nr_device_event,
  1142. };
  1143. static struct net_device **dev_nr;
  1144. static struct ax25_protocol nr_pid = {
  1145. .pid = AX25_P_NETROM,
  1146. .func = nr_route_frame
  1147. };
  1148. static struct ax25_linkfail nr_linkfail_notifier = {
  1149. .func = nr_link_failed,
  1150. };
  1151. static int __init nr_proto_init(void)
  1152. {
  1153. int i;
  1154. int rc = proto_register(&nr_proto, 0);
  1155. if (rc)
  1156. return rc;
  1157. if (nr_ndevs > 0x7fffffff/sizeof(struct net_device *)) {
  1158. pr_err("NET/ROM: %s - nr_ndevs parameter too large\n",
  1159. __func__);
  1160. rc = -EINVAL;
  1161. goto unregister_proto;
  1162. }
  1163. dev_nr = kcalloc(nr_ndevs, sizeof(struct net_device *), GFP_KERNEL);
  1164. if (!dev_nr) {
  1165. pr_err("NET/ROM: %s - unable to allocate device array\n",
  1166. __func__);
  1167. rc = -ENOMEM;
  1168. goto unregister_proto;
  1169. }
  1170. for (i = 0; i < nr_ndevs; i++) {
  1171. char name[IFNAMSIZ];
  1172. struct net_device *dev;
  1173. sprintf(name, "nr%d", i);
  1174. dev = alloc_netdev(0, name, NET_NAME_UNKNOWN, nr_setup);
  1175. if (!dev) {
  1176. rc = -ENOMEM;
  1177. goto fail;
  1178. }
  1179. dev->base_addr = i;
  1180. rc = register_netdev(dev);
  1181. if (rc) {
  1182. free_netdev(dev);
  1183. goto fail;
  1184. }
  1185. nr_set_lockdep_key(dev);
  1186. dev_nr[i] = dev;
  1187. }
  1188. rc = sock_register(&nr_family_ops);
  1189. if (rc)
  1190. goto fail;
  1191. rc = register_netdevice_notifier(&nr_dev_notifier);
  1192. if (rc)
  1193. goto out_sock;
  1194. ax25_register_pid(&nr_pid);
  1195. ax25_linkfail_register(&nr_linkfail_notifier);
  1196. #ifdef CONFIG_SYSCTL
  1197. rc = nr_register_sysctl();
  1198. if (rc)
  1199. goto out_sysctl;
  1200. #endif
  1201. nr_loopback_init();
  1202. rc = -ENOMEM;
  1203. if (!proc_create_seq("nr", 0444, init_net.proc_net, &nr_info_seqops))
  1204. goto proc_remove1;
  1205. if (!proc_create_seq("nr_neigh", 0444, init_net.proc_net,
  1206. &nr_neigh_seqops))
  1207. goto proc_remove2;
  1208. if (!proc_create_seq("nr_nodes", 0444, init_net.proc_net,
  1209. &nr_node_seqops))
  1210. goto proc_remove3;
  1211. return 0;
  1212. proc_remove3:
  1213. remove_proc_entry("nr_neigh", init_net.proc_net);
  1214. proc_remove2:
  1215. remove_proc_entry("nr", init_net.proc_net);
  1216. proc_remove1:
  1217. nr_loopback_clear();
  1218. nr_rt_free();
  1219. #ifdef CONFIG_SYSCTL
  1220. nr_unregister_sysctl();
  1221. out_sysctl:
  1222. #endif
  1223. ax25_linkfail_release(&nr_linkfail_notifier);
  1224. ax25_protocol_release(AX25_P_NETROM);
  1225. unregister_netdevice_notifier(&nr_dev_notifier);
  1226. out_sock:
  1227. sock_unregister(PF_NETROM);
  1228. fail:
  1229. while (--i >= 0) {
  1230. unregister_netdev(dev_nr[i]);
  1231. free_netdev(dev_nr[i]);
  1232. }
  1233. kfree(dev_nr);
  1234. unregister_proto:
  1235. proto_unregister(&nr_proto);
  1236. return rc;
  1237. }
  1238. module_init(nr_proto_init);
  1239. module_param(nr_ndevs, int, 0);
  1240. MODULE_PARM_DESC(nr_ndevs, "number of NET/ROM devices");
  1241. MODULE_AUTHOR("Jonathan Naylor G4KLX <g4klx@g4klx.demon.co.uk>");
  1242. MODULE_DESCRIPTION("The amateur radio NET/ROM network and transport layer protocol");
  1243. MODULE_LICENSE("GPL");
  1244. MODULE_ALIAS_NETPROTO(PF_NETROM);
  1245. static void __exit nr_exit(void)
  1246. {
  1247. int i;
  1248. remove_proc_entry("nr", init_net.proc_net);
  1249. remove_proc_entry("nr_neigh", init_net.proc_net);
  1250. remove_proc_entry("nr_nodes", init_net.proc_net);
  1251. nr_loopback_clear();
  1252. nr_rt_free();
  1253. #ifdef CONFIG_SYSCTL
  1254. nr_unregister_sysctl();
  1255. #endif
  1256. ax25_linkfail_release(&nr_linkfail_notifier);
  1257. ax25_protocol_release(AX25_P_NETROM);
  1258. unregister_netdevice_notifier(&nr_dev_notifier);
  1259. sock_unregister(PF_NETROM);
  1260. for (i = 0; i < nr_ndevs; i++) {
  1261. struct net_device *dev = dev_nr[i];
  1262. if (dev) {
  1263. unregister_netdev(dev);
  1264. free_netdev(dev);
  1265. }
  1266. }
  1267. kfree(dev_nr);
  1268. proto_unregister(&nr_proto);
  1269. }
  1270. module_exit(nr_exit);