af_netrom.c 32 KB

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