af_netrom.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516
  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.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(unsigned long data)
  213. {
  214. struct sock *sk=(struct sock *)data;
  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. {
  637. struct sk_buff *skb;
  638. struct sock *newsk;
  639. DEFINE_WAIT(wait);
  640. struct sock *sk;
  641. int err = 0;
  642. if ((sk = sock->sk) == NULL)
  643. return -EINVAL;
  644. lock_sock(sk);
  645. if (sk->sk_type != SOCK_SEQPACKET) {
  646. err = -EOPNOTSUPP;
  647. goto out_release;
  648. }
  649. if (sk->sk_state != TCP_LISTEN) {
  650. err = -EINVAL;
  651. goto out_release;
  652. }
  653. /*
  654. * The write queue this time is holding sockets ready to use
  655. * hooked into the SABM we saved
  656. */
  657. for (;;) {
  658. prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
  659. skb = skb_dequeue(&sk->sk_receive_queue);
  660. if (skb)
  661. break;
  662. if (flags & O_NONBLOCK) {
  663. err = -EWOULDBLOCK;
  664. break;
  665. }
  666. if (!signal_pending(current)) {
  667. release_sock(sk);
  668. schedule();
  669. lock_sock(sk);
  670. continue;
  671. }
  672. err = -ERESTARTSYS;
  673. break;
  674. }
  675. finish_wait(sk_sleep(sk), &wait);
  676. if (err)
  677. goto out_release;
  678. newsk = skb->sk;
  679. sock_graft(newsk, newsock);
  680. /* Now attach up the new socket */
  681. kfree_skb(skb);
  682. sk_acceptq_removed(sk);
  683. out_release:
  684. release_sock(sk);
  685. return err;
  686. }
  687. static int nr_getname(struct socket *sock, struct sockaddr *uaddr,
  688. int *uaddr_len, int peer)
  689. {
  690. struct full_sockaddr_ax25 *sax = (struct full_sockaddr_ax25 *)uaddr;
  691. struct sock *sk = sock->sk;
  692. struct nr_sock *nr = nr_sk(sk);
  693. memset(&sax->fsa_ax25, 0, sizeof(struct sockaddr_ax25));
  694. lock_sock(sk);
  695. if (peer != 0) {
  696. if (sk->sk_state != TCP_ESTABLISHED) {
  697. release_sock(sk);
  698. return -ENOTCONN;
  699. }
  700. sax->fsa_ax25.sax25_family = AF_NETROM;
  701. sax->fsa_ax25.sax25_ndigis = 1;
  702. sax->fsa_ax25.sax25_call = nr->user_addr;
  703. memset(sax->fsa_digipeater, 0, sizeof(sax->fsa_digipeater));
  704. sax->fsa_digipeater[0] = nr->dest_addr;
  705. *uaddr_len = sizeof(struct full_sockaddr_ax25);
  706. } else {
  707. sax->fsa_ax25.sax25_family = AF_NETROM;
  708. sax->fsa_ax25.sax25_ndigis = 0;
  709. sax->fsa_ax25.sax25_call = nr->source_addr;
  710. *uaddr_len = sizeof(struct sockaddr_ax25);
  711. }
  712. release_sock(sk);
  713. return 0;
  714. }
  715. int nr_rx_frame(struct sk_buff *skb, struct net_device *dev)
  716. {
  717. struct sock *sk;
  718. struct sock *make;
  719. struct nr_sock *nr_make;
  720. ax25_address *src, *dest, *user;
  721. unsigned short circuit_index, circuit_id;
  722. unsigned short peer_circuit_index, peer_circuit_id;
  723. unsigned short frametype, flags, window, timeout;
  724. int ret;
  725. skb->sk = NULL; /* Initially we don't know who it's for */
  726. /*
  727. * skb->data points to the netrom frame start
  728. */
  729. src = (ax25_address *)(skb->data + 0);
  730. dest = (ax25_address *)(skb->data + 7);
  731. circuit_index = skb->data[15];
  732. circuit_id = skb->data[16];
  733. peer_circuit_index = skb->data[17];
  734. peer_circuit_id = skb->data[18];
  735. frametype = skb->data[19] & 0x0F;
  736. flags = skb->data[19] & 0xF0;
  737. /*
  738. * Check for an incoming IP over NET/ROM frame.
  739. */
  740. if (frametype == NR_PROTOEXT &&
  741. circuit_index == NR_PROTO_IP && circuit_id == NR_PROTO_IP) {
  742. skb_pull(skb, NR_NETWORK_LEN + NR_TRANSPORT_LEN);
  743. skb_reset_transport_header(skb);
  744. return nr_rx_ip(skb, dev);
  745. }
  746. /*
  747. * Find an existing socket connection, based on circuit ID, if it's
  748. * a Connect Request base it on their circuit ID.
  749. *
  750. * Circuit ID 0/0 is not valid but it could still be a "reset" for a
  751. * circuit that no longer exists at the other end ...
  752. */
  753. sk = NULL;
  754. if (circuit_index == 0 && circuit_id == 0) {
  755. if (frametype == NR_CONNACK && flags == NR_CHOKE_FLAG)
  756. sk = nr_find_peer(peer_circuit_index, peer_circuit_id, src);
  757. } else {
  758. if (frametype == NR_CONNREQ)
  759. sk = nr_find_peer(circuit_index, circuit_id, src);
  760. else
  761. sk = nr_find_socket(circuit_index, circuit_id);
  762. }
  763. if (sk != NULL) {
  764. bh_lock_sock(sk);
  765. skb_reset_transport_header(skb);
  766. if (frametype == NR_CONNACK && skb->len == 22)
  767. nr_sk(sk)->bpqext = 1;
  768. else
  769. nr_sk(sk)->bpqext = 0;
  770. ret = nr_process_rx_frame(sk, skb);
  771. bh_unlock_sock(sk);
  772. sock_put(sk);
  773. return ret;
  774. }
  775. /*
  776. * Now it should be a CONNREQ.
  777. */
  778. if (frametype != NR_CONNREQ) {
  779. /*
  780. * Here it would be nice to be able to send a reset but
  781. * NET/ROM doesn't have one. We've tried to extend the protocol
  782. * by sending NR_CONNACK | NR_CHOKE_FLAGS replies but that
  783. * apparently kills BPQ boxes... :-(
  784. * So now we try to follow the established behaviour of
  785. * G8PZT's Xrouter which is sending packets with command type 7
  786. * as an extension of the protocol.
  787. */
  788. if (sysctl_netrom_reset_circuit &&
  789. (frametype != NR_RESET || flags != 0))
  790. nr_transmit_reset(skb, 1);
  791. return 0;
  792. }
  793. sk = nr_find_listener(dest);
  794. user = (ax25_address *)(skb->data + 21);
  795. if (sk == NULL || sk_acceptq_is_full(sk) ||
  796. (make = nr_make_new(sk)) == NULL) {
  797. nr_transmit_refusal(skb, 0);
  798. if (sk)
  799. sock_put(sk);
  800. return 0;
  801. }
  802. bh_lock_sock(sk);
  803. window = skb->data[20];
  804. skb->sk = make;
  805. make->sk_state = TCP_ESTABLISHED;
  806. /* Fill in his circuit details */
  807. nr_make = nr_sk(make);
  808. nr_make->source_addr = *dest;
  809. nr_make->dest_addr = *src;
  810. nr_make->user_addr = *user;
  811. nr_make->your_index = circuit_index;
  812. nr_make->your_id = circuit_id;
  813. bh_unlock_sock(sk);
  814. circuit = nr_find_next_circuit();
  815. bh_lock_sock(sk);
  816. nr_make->my_index = circuit / 256;
  817. nr_make->my_id = circuit % 256;
  818. circuit++;
  819. /* Window negotiation */
  820. if (window < nr_make->window)
  821. nr_make->window = window;
  822. /* L4 timeout negotiation */
  823. if (skb->len == 37) {
  824. timeout = skb->data[36] * 256 + skb->data[35];
  825. if (timeout * HZ < nr_make->t1)
  826. nr_make->t1 = timeout * HZ;
  827. nr_make->bpqext = 1;
  828. } else {
  829. nr_make->bpqext = 0;
  830. }
  831. nr_write_internal(make, NR_CONNACK);
  832. nr_make->condition = 0x00;
  833. nr_make->vs = 0;
  834. nr_make->va = 0;
  835. nr_make->vr = 0;
  836. nr_make->vl = 0;
  837. nr_make->state = NR_STATE_3;
  838. sk_acceptq_added(sk);
  839. skb_queue_head(&sk->sk_receive_queue, skb);
  840. if (!sock_flag(sk, SOCK_DEAD))
  841. sk->sk_data_ready(sk);
  842. bh_unlock_sock(sk);
  843. sock_put(sk);
  844. nr_insert_socket(make);
  845. nr_start_heartbeat(make);
  846. nr_start_idletimer(make);
  847. return 1;
  848. }
  849. static int nr_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
  850. {
  851. struct sock *sk = sock->sk;
  852. struct nr_sock *nr = nr_sk(sk);
  853. DECLARE_SOCKADDR(struct sockaddr_ax25 *, usax, msg->msg_name);
  854. int err;
  855. struct sockaddr_ax25 sax;
  856. struct sk_buff *skb;
  857. unsigned char *asmptr;
  858. int size;
  859. if (msg->msg_flags & ~(MSG_DONTWAIT|MSG_EOR|MSG_CMSG_COMPAT))
  860. return -EINVAL;
  861. lock_sock(sk);
  862. if (sock_flag(sk, SOCK_ZAPPED)) {
  863. err = -EADDRNOTAVAIL;
  864. goto out;
  865. }
  866. if (sk->sk_shutdown & SEND_SHUTDOWN) {
  867. send_sig(SIGPIPE, current, 0);
  868. err = -EPIPE;
  869. goto out;
  870. }
  871. if (nr->device == NULL) {
  872. err = -ENETUNREACH;
  873. goto out;
  874. }
  875. if (usax) {
  876. if (msg->msg_namelen < sizeof(sax)) {
  877. err = -EINVAL;
  878. goto out;
  879. }
  880. sax = *usax;
  881. if (ax25cmp(&nr->dest_addr, &sax.sax25_call) != 0) {
  882. err = -EISCONN;
  883. goto out;
  884. }
  885. if (sax.sax25_family != AF_NETROM) {
  886. err = -EINVAL;
  887. goto out;
  888. }
  889. } else {
  890. if (sk->sk_state != TCP_ESTABLISHED) {
  891. err = -ENOTCONN;
  892. goto out;
  893. }
  894. sax.sax25_family = AF_NETROM;
  895. sax.sax25_call = nr->dest_addr;
  896. }
  897. /* Build a packet - the conventional user limit is 236 bytes. We can
  898. do ludicrously large NetROM frames but must not overflow */
  899. if (len > 65536) {
  900. err = -EMSGSIZE;
  901. goto out;
  902. }
  903. size = len + NR_NETWORK_LEN + NR_TRANSPORT_LEN;
  904. if ((skb = sock_alloc_send_skb(sk, size, msg->msg_flags & MSG_DONTWAIT, &err)) == NULL)
  905. goto out;
  906. skb_reserve(skb, size - len);
  907. skb_reset_transport_header(skb);
  908. /*
  909. * Push down the NET/ROM header
  910. */
  911. asmptr = skb_push(skb, NR_TRANSPORT_LEN);
  912. /* Build a NET/ROM Transport header */
  913. *asmptr++ = nr->your_index;
  914. *asmptr++ = nr->your_id;
  915. *asmptr++ = 0; /* To be filled in later */
  916. *asmptr++ = 0; /* Ditto */
  917. *asmptr++ = NR_INFO;
  918. /*
  919. * Put the data on the end
  920. */
  921. skb_put(skb, len);
  922. /* User data follows immediately after the NET/ROM transport header */
  923. if (memcpy_from_msg(skb_transport_header(skb), msg, len)) {
  924. kfree_skb(skb);
  925. err = -EFAULT;
  926. goto out;
  927. }
  928. if (sk->sk_state != TCP_ESTABLISHED) {
  929. kfree_skb(skb);
  930. err = -ENOTCONN;
  931. goto out;
  932. }
  933. nr_output(sk, skb); /* Shove it onto the queue */
  934. err = len;
  935. out:
  936. release_sock(sk);
  937. return err;
  938. }
  939. static int nr_recvmsg(struct socket *sock, struct msghdr *msg, size_t size,
  940. int flags)
  941. {
  942. struct sock *sk = sock->sk;
  943. DECLARE_SOCKADDR(struct sockaddr_ax25 *, sax, msg->msg_name);
  944. size_t copied;
  945. struct sk_buff *skb;
  946. int er;
  947. /*
  948. * This works for seqpacket too. The receiver has ordered the queue for
  949. * us! We do one quick check first though
  950. */
  951. lock_sock(sk);
  952. if (sk->sk_state != TCP_ESTABLISHED) {
  953. release_sock(sk);
  954. return -ENOTCONN;
  955. }
  956. /* Now we can treat all alike */
  957. if ((skb = skb_recv_datagram(sk, flags & ~MSG_DONTWAIT, flags & MSG_DONTWAIT, &er)) == NULL) {
  958. release_sock(sk);
  959. return er;
  960. }
  961. skb_reset_transport_header(skb);
  962. copied = skb->len;
  963. if (copied > size) {
  964. copied = size;
  965. msg->msg_flags |= MSG_TRUNC;
  966. }
  967. er = skb_copy_datagram_msg(skb, 0, msg, copied);
  968. if (er < 0) {
  969. skb_free_datagram(sk, skb);
  970. release_sock(sk);
  971. return er;
  972. }
  973. if (sax != NULL) {
  974. memset(sax, 0, sizeof(*sax));
  975. sax->sax25_family = AF_NETROM;
  976. skb_copy_from_linear_data_offset(skb, 7, sax->sax25_call.ax25_call,
  977. AX25_ADDR_LEN);
  978. msg->msg_namelen = sizeof(*sax);
  979. }
  980. skb_free_datagram(sk, skb);
  981. release_sock(sk);
  982. return copied;
  983. }
  984. static int nr_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
  985. {
  986. struct sock *sk = sock->sk;
  987. void __user *argp = (void __user *)arg;
  988. int ret;
  989. switch (cmd) {
  990. case TIOCOUTQ: {
  991. long amount;
  992. lock_sock(sk);
  993. amount = sk->sk_sndbuf - sk_wmem_alloc_get(sk);
  994. if (amount < 0)
  995. amount = 0;
  996. release_sock(sk);
  997. return put_user(amount, (int __user *)argp);
  998. }
  999. case TIOCINQ: {
  1000. struct sk_buff *skb;
  1001. long amount = 0L;
  1002. lock_sock(sk);
  1003. /* These two are safe on a single CPU system as only user tasks fiddle here */
  1004. if ((skb = skb_peek(&sk->sk_receive_queue)) != NULL)
  1005. amount = skb->len;
  1006. release_sock(sk);
  1007. return put_user(amount, (int __user *)argp);
  1008. }
  1009. case SIOCGSTAMP:
  1010. lock_sock(sk);
  1011. ret = sock_get_timestamp(sk, argp);
  1012. release_sock(sk);
  1013. return ret;
  1014. case SIOCGSTAMPNS:
  1015. lock_sock(sk);
  1016. ret = sock_get_timestampns(sk, argp);
  1017. release_sock(sk);
  1018. return ret;
  1019. case SIOCGIFADDR:
  1020. case SIOCSIFADDR:
  1021. case SIOCGIFDSTADDR:
  1022. case SIOCSIFDSTADDR:
  1023. case SIOCGIFBRDADDR:
  1024. case SIOCSIFBRDADDR:
  1025. case SIOCGIFNETMASK:
  1026. case SIOCSIFNETMASK:
  1027. case SIOCGIFMETRIC:
  1028. case SIOCSIFMETRIC:
  1029. return -EINVAL;
  1030. case SIOCADDRT:
  1031. case SIOCDELRT:
  1032. case SIOCNRDECOBS:
  1033. if (!capable(CAP_NET_ADMIN))
  1034. return -EPERM;
  1035. return nr_rt_ioctl(cmd, argp);
  1036. default:
  1037. return -ENOIOCTLCMD;
  1038. }
  1039. return 0;
  1040. }
  1041. #ifdef CONFIG_PROC_FS
  1042. static void *nr_info_start(struct seq_file *seq, loff_t *pos)
  1043. {
  1044. spin_lock_bh(&nr_list_lock);
  1045. return seq_hlist_start_head(&nr_list, *pos);
  1046. }
  1047. static void *nr_info_next(struct seq_file *seq, void *v, loff_t *pos)
  1048. {
  1049. return seq_hlist_next(v, &nr_list, pos);
  1050. }
  1051. static void nr_info_stop(struct seq_file *seq, void *v)
  1052. {
  1053. spin_unlock_bh(&nr_list_lock);
  1054. }
  1055. static int nr_info_show(struct seq_file *seq, void *v)
  1056. {
  1057. struct sock *s = sk_entry(v);
  1058. struct net_device *dev;
  1059. struct nr_sock *nr;
  1060. const char *devname;
  1061. char buf[11];
  1062. if (v == SEQ_START_TOKEN)
  1063. seq_puts(seq,
  1064. "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");
  1065. else {
  1066. bh_lock_sock(s);
  1067. nr = nr_sk(s);
  1068. if ((dev = nr->device) == NULL)
  1069. devname = "???";
  1070. else
  1071. devname = dev->name;
  1072. seq_printf(seq, "%-9s ", ax2asc(buf, &nr->user_addr));
  1073. seq_printf(seq, "%-9s ", ax2asc(buf, &nr->dest_addr));
  1074. seq_printf(seq,
  1075. "%-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",
  1076. ax2asc(buf, &nr->source_addr),
  1077. devname,
  1078. nr->my_index,
  1079. nr->my_id,
  1080. nr->your_index,
  1081. nr->your_id,
  1082. nr->state,
  1083. nr->vs,
  1084. nr->vr,
  1085. nr->va,
  1086. ax25_display_timer(&nr->t1timer) / HZ,
  1087. nr->t1 / HZ,
  1088. ax25_display_timer(&nr->t2timer) / HZ,
  1089. nr->t2 / HZ,
  1090. ax25_display_timer(&nr->t4timer) / HZ,
  1091. nr->t4 / HZ,
  1092. ax25_display_timer(&nr->idletimer) / (60 * HZ),
  1093. nr->idle / (60 * HZ),
  1094. nr->n2count,
  1095. nr->n2,
  1096. nr->window,
  1097. sk_wmem_alloc_get(s),
  1098. sk_rmem_alloc_get(s),
  1099. s->sk_socket ? SOCK_INODE(s->sk_socket)->i_ino : 0L);
  1100. bh_unlock_sock(s);
  1101. }
  1102. return 0;
  1103. }
  1104. static const struct seq_operations nr_info_seqops = {
  1105. .start = nr_info_start,
  1106. .next = nr_info_next,
  1107. .stop = nr_info_stop,
  1108. .show = nr_info_show,
  1109. };
  1110. static int nr_info_open(struct inode *inode, struct file *file)
  1111. {
  1112. return seq_open(file, &nr_info_seqops);
  1113. }
  1114. static const struct file_operations nr_info_fops = {
  1115. .owner = THIS_MODULE,
  1116. .open = nr_info_open,
  1117. .read = seq_read,
  1118. .llseek = seq_lseek,
  1119. .release = seq_release,
  1120. };
  1121. #endif /* CONFIG_PROC_FS */
  1122. static const struct net_proto_family nr_family_ops = {
  1123. .family = PF_NETROM,
  1124. .create = nr_create,
  1125. .owner = THIS_MODULE,
  1126. };
  1127. static const struct proto_ops nr_proto_ops = {
  1128. .family = PF_NETROM,
  1129. .owner = THIS_MODULE,
  1130. .release = nr_release,
  1131. .bind = nr_bind,
  1132. .connect = nr_connect,
  1133. .socketpair = sock_no_socketpair,
  1134. .accept = nr_accept,
  1135. .getname = nr_getname,
  1136. .poll = datagram_poll,
  1137. .ioctl = nr_ioctl,
  1138. .listen = nr_listen,
  1139. .shutdown = sock_no_shutdown,
  1140. .setsockopt = nr_setsockopt,
  1141. .getsockopt = nr_getsockopt,
  1142. .sendmsg = nr_sendmsg,
  1143. .recvmsg = nr_recvmsg,
  1144. .mmap = sock_no_mmap,
  1145. .sendpage = sock_no_sendpage,
  1146. };
  1147. static struct notifier_block nr_dev_notifier = {
  1148. .notifier_call = nr_device_event,
  1149. };
  1150. static struct net_device **dev_nr;
  1151. static struct ax25_protocol nr_pid = {
  1152. .pid = AX25_P_NETROM,
  1153. .func = nr_route_frame
  1154. };
  1155. static struct ax25_linkfail nr_linkfail_notifier = {
  1156. .func = nr_link_failed,
  1157. };
  1158. static int __init nr_proto_init(void)
  1159. {
  1160. int i;
  1161. int rc = proto_register(&nr_proto, 0);
  1162. if (rc != 0)
  1163. goto out;
  1164. if (nr_ndevs > 0x7fffffff/sizeof(struct net_device *)) {
  1165. printk(KERN_ERR "NET/ROM: nr_proto_init - nr_ndevs parameter to large\n");
  1166. return -1;
  1167. }
  1168. dev_nr = kzalloc(nr_ndevs * sizeof(struct net_device *), GFP_KERNEL);
  1169. if (dev_nr == NULL) {
  1170. printk(KERN_ERR "NET/ROM: nr_proto_init - unable to allocate device array\n");
  1171. return -1;
  1172. }
  1173. for (i = 0; i < nr_ndevs; i++) {
  1174. char name[IFNAMSIZ];
  1175. struct net_device *dev;
  1176. sprintf(name, "nr%d", i);
  1177. dev = alloc_netdev(0, name, NET_NAME_UNKNOWN, nr_setup);
  1178. if (!dev) {
  1179. printk(KERN_ERR "NET/ROM: nr_proto_init - unable to allocate device structure\n");
  1180. goto fail;
  1181. }
  1182. dev->base_addr = i;
  1183. if (register_netdev(dev)) {
  1184. printk(KERN_ERR "NET/ROM: nr_proto_init - unable to register network device\n");
  1185. free_netdev(dev);
  1186. goto fail;
  1187. }
  1188. nr_set_lockdep_key(dev);
  1189. dev_nr[i] = dev;
  1190. }
  1191. if (sock_register(&nr_family_ops)) {
  1192. printk(KERN_ERR "NET/ROM: nr_proto_init - unable to register socket family\n");
  1193. goto fail;
  1194. }
  1195. register_netdevice_notifier(&nr_dev_notifier);
  1196. ax25_register_pid(&nr_pid);
  1197. ax25_linkfail_register(&nr_linkfail_notifier);
  1198. #ifdef CONFIG_SYSCTL
  1199. nr_register_sysctl();
  1200. #endif
  1201. nr_loopback_init();
  1202. proc_create("nr", S_IRUGO, init_net.proc_net, &nr_info_fops);
  1203. proc_create("nr_neigh", S_IRUGO, init_net.proc_net, &nr_neigh_fops);
  1204. proc_create("nr_nodes", S_IRUGO, init_net.proc_net, &nr_nodes_fops);
  1205. out:
  1206. return rc;
  1207. fail:
  1208. while (--i >= 0) {
  1209. unregister_netdev(dev_nr[i]);
  1210. free_netdev(dev_nr[i]);
  1211. }
  1212. kfree(dev_nr);
  1213. proto_unregister(&nr_proto);
  1214. rc = -1;
  1215. goto out;
  1216. }
  1217. module_init(nr_proto_init);
  1218. module_param(nr_ndevs, int, 0);
  1219. MODULE_PARM_DESC(nr_ndevs, "number of NET/ROM devices");
  1220. MODULE_AUTHOR("Jonathan Naylor G4KLX <g4klx@g4klx.demon.co.uk>");
  1221. MODULE_DESCRIPTION("The amateur radio NET/ROM network and transport layer protocol");
  1222. MODULE_LICENSE("GPL");
  1223. MODULE_ALIAS_NETPROTO(PF_NETROM);
  1224. static void __exit nr_exit(void)
  1225. {
  1226. int i;
  1227. remove_proc_entry("nr", init_net.proc_net);
  1228. remove_proc_entry("nr_neigh", init_net.proc_net);
  1229. remove_proc_entry("nr_nodes", init_net.proc_net);
  1230. nr_loopback_clear();
  1231. nr_rt_free();
  1232. #ifdef CONFIG_SYSCTL
  1233. nr_unregister_sysctl();
  1234. #endif
  1235. ax25_linkfail_release(&nr_linkfail_notifier);
  1236. ax25_protocol_release(AX25_P_NETROM);
  1237. unregister_netdevice_notifier(&nr_dev_notifier);
  1238. sock_unregister(PF_NETROM);
  1239. for (i = 0; i < nr_ndevs; i++) {
  1240. struct net_device *dev = dev_nr[i];
  1241. if (dev) {
  1242. unregister_netdev(dev);
  1243. free_netdev(dev);
  1244. }
  1245. }
  1246. kfree(dev_nr);
  1247. proto_unregister(&nr_proto);
  1248. }
  1249. module_exit(nr_exit);