af_netrom.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515
  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 <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. struct hlist_node *node;
  98. spin_lock_bh(&nr_list_lock);
  99. sk_for_each(s, node, &nr_list)
  100. if (nr_sk(s)->device == dev)
  101. nr_disconnect(s, ENETUNREACH);
  102. spin_unlock_bh(&nr_list_lock);
  103. }
  104. /*
  105. * Handle device status changes.
  106. */
  107. static int nr_device_event(struct notifier_block *this, unsigned long event, void *ptr)
  108. {
  109. struct net_device *dev = (struct net_device *)ptr;
  110. if (!net_eq(dev_net(dev), &init_net))
  111. return NOTIFY_DONE;
  112. if (event != NETDEV_DOWN)
  113. return NOTIFY_DONE;
  114. nr_kill_by_device(dev);
  115. nr_rt_device_down(dev);
  116. return NOTIFY_DONE;
  117. }
  118. /*
  119. * Add a socket to the bound sockets list.
  120. */
  121. static void nr_insert_socket(struct sock *sk)
  122. {
  123. spin_lock_bh(&nr_list_lock);
  124. sk_add_node(sk, &nr_list);
  125. spin_unlock_bh(&nr_list_lock);
  126. }
  127. /*
  128. * Find a socket that wants to accept the Connect Request we just
  129. * received.
  130. */
  131. static struct sock *nr_find_listener(ax25_address *addr)
  132. {
  133. struct sock *s;
  134. struct hlist_node *node;
  135. spin_lock_bh(&nr_list_lock);
  136. sk_for_each(s, node, &nr_list)
  137. if (!ax25cmp(&nr_sk(s)->source_addr, addr) &&
  138. s->sk_state == TCP_LISTEN) {
  139. bh_lock_sock(s);
  140. goto found;
  141. }
  142. s = NULL;
  143. found:
  144. spin_unlock_bh(&nr_list_lock);
  145. return s;
  146. }
  147. /*
  148. * Find a connected NET/ROM socket given my circuit IDs.
  149. */
  150. static struct sock *nr_find_socket(unsigned char index, unsigned char id)
  151. {
  152. struct sock *s;
  153. struct hlist_node *node;
  154. spin_lock_bh(&nr_list_lock);
  155. sk_for_each(s, node, &nr_list) {
  156. struct nr_sock *nr = nr_sk(s);
  157. if (nr->my_index == index && nr->my_id == id) {
  158. bh_lock_sock(s);
  159. goto found;
  160. }
  161. }
  162. s = NULL;
  163. found:
  164. spin_unlock_bh(&nr_list_lock);
  165. return s;
  166. }
  167. /*
  168. * Find a connected NET/ROM socket given their circuit IDs.
  169. */
  170. static struct sock *nr_find_peer(unsigned char index, unsigned char id,
  171. ax25_address *dest)
  172. {
  173. struct sock *s;
  174. struct hlist_node *node;
  175. spin_lock_bh(&nr_list_lock);
  176. sk_for_each(s, node, &nr_list) {
  177. struct nr_sock *nr = nr_sk(s);
  178. if (nr->your_index == index && nr->your_id == id &&
  179. !ax25cmp(&nr->dest_addr, dest)) {
  180. bh_lock_sock(s);
  181. goto found;
  182. }
  183. }
  184. s = NULL;
  185. found:
  186. spin_unlock_bh(&nr_list_lock);
  187. return s;
  188. }
  189. /*
  190. * Find next free circuit ID.
  191. */
  192. static unsigned short nr_find_next_circuit(void)
  193. {
  194. unsigned short id = circuit;
  195. unsigned char i, j;
  196. struct sock *sk;
  197. for (;;) {
  198. i = id / 256;
  199. j = id % 256;
  200. if (i != 0 && j != 0) {
  201. if ((sk=nr_find_socket(i, j)) == NULL)
  202. break;
  203. bh_unlock_sock(sk);
  204. }
  205. id++;
  206. }
  207. return id;
  208. }
  209. /*
  210. * Deferred destroy.
  211. */
  212. void nr_destroy_socket(struct sock *);
  213. /*
  214. * Handler for deferred kills.
  215. */
  216. static void nr_destroy_timer(unsigned long data)
  217. {
  218. struct sock *sk=(struct sock *)data;
  219. bh_lock_sock(sk);
  220. sock_hold(sk);
  221. nr_destroy_socket(sk);
  222. bh_unlock_sock(sk);
  223. sock_put(sk);
  224. }
  225. /*
  226. * This is called from user mode and the timers. Thus it protects itself
  227. * against interrupt users but doesn't worry about being called during
  228. * work. Once it is removed from the queue no interrupt or bottom half
  229. * will touch it and we are (fairly 8-) ) safe.
  230. */
  231. void nr_destroy_socket(struct sock *sk)
  232. {
  233. struct sk_buff *skb;
  234. nr_remove_socket(sk);
  235. nr_stop_heartbeat(sk);
  236. nr_stop_t1timer(sk);
  237. nr_stop_t2timer(sk);
  238. nr_stop_t4timer(sk);
  239. nr_stop_idletimer(sk);
  240. nr_clear_queues(sk); /* Flush the queues */
  241. while ((skb = skb_dequeue(&sk->sk_receive_queue)) != NULL) {
  242. if (skb->sk != sk) { /* A pending connection */
  243. /* Queue the unaccepted socket for death */
  244. sock_set_flag(skb->sk, SOCK_DEAD);
  245. nr_start_heartbeat(skb->sk);
  246. nr_sk(skb->sk)->state = NR_STATE_0;
  247. }
  248. kfree_skb(skb);
  249. }
  250. if (sk_has_allocations(sk)) {
  251. /* Defer: outstanding buffers */
  252. sk->sk_timer.function = nr_destroy_timer;
  253. sk->sk_timer.expires = jiffies + 2 * HZ;
  254. add_timer(&sk->sk_timer);
  255. } else
  256. sock_put(sk);
  257. }
  258. /*
  259. * Handling for system calls applied via the various interfaces to a
  260. * NET/ROM socket object.
  261. */
  262. static int nr_setsockopt(struct socket *sock, int level, int optname,
  263. char __user *optval, unsigned int optlen)
  264. {
  265. struct sock *sk = sock->sk;
  266. struct nr_sock *nr = nr_sk(sk);
  267. unsigned long opt;
  268. if (level != SOL_NETROM)
  269. return -ENOPROTOOPT;
  270. if (optlen < sizeof(unsigned int))
  271. return -EINVAL;
  272. if (get_user(opt, (unsigned int __user *)optval))
  273. return -EFAULT;
  274. switch (optname) {
  275. case NETROM_T1:
  276. if (opt < 1 || opt > ULONG_MAX / HZ)
  277. return -EINVAL;
  278. nr->t1 = opt * HZ;
  279. return 0;
  280. case NETROM_T2:
  281. if (opt < 1 || opt > ULONG_MAX / HZ)
  282. return -EINVAL;
  283. nr->t2 = opt * HZ;
  284. return 0;
  285. case NETROM_N2:
  286. if (opt < 1 || opt > 31)
  287. return -EINVAL;
  288. nr->n2 = opt;
  289. return 0;
  290. case NETROM_T4:
  291. if (opt < 1 || opt > ULONG_MAX / HZ)
  292. return -EINVAL;
  293. nr->t4 = opt * HZ;
  294. return 0;
  295. case NETROM_IDLE:
  296. if (opt > ULONG_MAX / (60 * HZ))
  297. return -EINVAL;
  298. nr->idle = opt * 60 * HZ;
  299. return 0;
  300. default:
  301. return -ENOPROTOOPT;
  302. }
  303. }
  304. static int nr_getsockopt(struct socket *sock, int level, int optname,
  305. char __user *optval, int __user *optlen)
  306. {
  307. struct sock *sk = sock->sk;
  308. struct nr_sock *nr = nr_sk(sk);
  309. int val = 0;
  310. int len;
  311. if (level != SOL_NETROM)
  312. return -ENOPROTOOPT;
  313. if (get_user(len, optlen))
  314. return -EFAULT;
  315. if (len < 0)
  316. return -EINVAL;
  317. switch (optname) {
  318. case NETROM_T1:
  319. val = nr->t1 / HZ;
  320. break;
  321. case NETROM_T2:
  322. val = nr->t2 / HZ;
  323. break;
  324. case NETROM_N2:
  325. val = nr->n2;
  326. break;
  327. case NETROM_T4:
  328. val = nr->t4 / HZ;
  329. break;
  330. case NETROM_IDLE:
  331. val = nr->idle / (60 * HZ);
  332. break;
  333. default:
  334. return -ENOPROTOOPT;
  335. }
  336. len = min_t(unsigned int, len, sizeof(int));
  337. if (put_user(len, optlen))
  338. return -EFAULT;
  339. return copy_to_user(optval, &val, len) ? -EFAULT : 0;
  340. }
  341. static int nr_listen(struct socket *sock, int backlog)
  342. {
  343. struct sock *sk = sock->sk;
  344. lock_sock(sk);
  345. if (sk->sk_state != TCP_LISTEN) {
  346. memset(&nr_sk(sk)->user_addr, 0, AX25_ADDR_LEN);
  347. sk->sk_max_ack_backlog = backlog;
  348. sk->sk_state = TCP_LISTEN;
  349. release_sock(sk);
  350. return 0;
  351. }
  352. release_sock(sk);
  353. return -EOPNOTSUPP;
  354. }
  355. static struct proto nr_proto = {
  356. .name = "NETROM",
  357. .owner = THIS_MODULE,
  358. .obj_size = sizeof(struct nr_sock),
  359. };
  360. static int nr_create(struct net *net, struct socket *sock, int protocol,
  361. int kern)
  362. {
  363. struct sock *sk;
  364. struct nr_sock *nr;
  365. if (!net_eq(net, &init_net))
  366. return -EAFNOSUPPORT;
  367. if (sock->type != SOCK_SEQPACKET || protocol != 0)
  368. return -ESOCKTNOSUPPORT;
  369. sk = sk_alloc(net, PF_NETROM, GFP_ATOMIC, &nr_proto);
  370. if (sk == NULL)
  371. return -ENOMEM;
  372. nr = nr_sk(sk);
  373. sock_init_data(sock, sk);
  374. sock->ops = &nr_proto_ops;
  375. sk->sk_protocol = protocol;
  376. skb_queue_head_init(&nr->ack_queue);
  377. skb_queue_head_init(&nr->reseq_queue);
  378. skb_queue_head_init(&nr->frag_queue);
  379. nr_init_timers(sk);
  380. nr->t1 =
  381. msecs_to_jiffies(sysctl_netrom_transport_timeout);
  382. nr->t2 =
  383. msecs_to_jiffies(sysctl_netrom_transport_acknowledge_delay);
  384. nr->n2 =
  385. msecs_to_jiffies(sysctl_netrom_transport_maximum_tries);
  386. nr->t4 =
  387. msecs_to_jiffies(sysctl_netrom_transport_busy_delay);
  388. nr->idle =
  389. msecs_to_jiffies(sysctl_netrom_transport_no_activity_timeout);
  390. nr->window = sysctl_netrom_transport_requested_window_size;
  391. nr->bpqext = 1;
  392. nr->state = NR_STATE_0;
  393. return 0;
  394. }
  395. static struct sock *nr_make_new(struct sock *osk)
  396. {
  397. struct sock *sk;
  398. struct nr_sock *nr, *onr;
  399. if (osk->sk_type != SOCK_SEQPACKET)
  400. return NULL;
  401. sk = sk_alloc(sock_net(osk), PF_NETROM, GFP_ATOMIC, osk->sk_prot);
  402. if (sk == NULL)
  403. return NULL;
  404. nr = nr_sk(sk);
  405. sock_init_data(NULL, sk);
  406. sk->sk_type = osk->sk_type;
  407. sk->sk_priority = osk->sk_priority;
  408. sk->sk_protocol = osk->sk_protocol;
  409. sk->sk_rcvbuf = osk->sk_rcvbuf;
  410. sk->sk_sndbuf = osk->sk_sndbuf;
  411. sk->sk_state = TCP_ESTABLISHED;
  412. sock_copy_flags(sk, osk);
  413. skb_queue_head_init(&nr->ack_queue);
  414. skb_queue_head_init(&nr->reseq_queue);
  415. skb_queue_head_init(&nr->frag_queue);
  416. nr_init_timers(sk);
  417. onr = nr_sk(osk);
  418. nr->t1 = onr->t1;
  419. nr->t2 = onr->t2;
  420. nr->n2 = onr->n2;
  421. nr->t4 = onr->t4;
  422. nr->idle = onr->idle;
  423. nr->window = onr->window;
  424. nr->device = onr->device;
  425. nr->bpqext = onr->bpqext;
  426. return sk;
  427. }
  428. static int nr_release(struct socket *sock)
  429. {
  430. struct sock *sk = sock->sk;
  431. struct nr_sock *nr;
  432. if (sk == NULL) return 0;
  433. sock_hold(sk);
  434. sock_orphan(sk);
  435. lock_sock(sk);
  436. nr = nr_sk(sk);
  437. switch (nr->state) {
  438. case NR_STATE_0:
  439. case NR_STATE_1:
  440. case NR_STATE_2:
  441. nr_disconnect(sk, 0);
  442. nr_destroy_socket(sk);
  443. break;
  444. case NR_STATE_3:
  445. nr_clear_queues(sk);
  446. nr->n2count = 0;
  447. nr_write_internal(sk, NR_DISCREQ);
  448. nr_start_t1timer(sk);
  449. nr_stop_t2timer(sk);
  450. nr_stop_t4timer(sk);
  451. nr_stop_idletimer(sk);
  452. nr->state = NR_STATE_2;
  453. sk->sk_state = TCP_CLOSE;
  454. sk->sk_shutdown |= SEND_SHUTDOWN;
  455. sk->sk_state_change(sk);
  456. sock_set_flag(sk, SOCK_DESTROY);
  457. break;
  458. default:
  459. break;
  460. }
  461. sock->sk = NULL;
  462. release_sock(sk);
  463. sock_put(sk);
  464. return 0;
  465. }
  466. static int nr_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
  467. {
  468. struct sock *sk = sock->sk;
  469. struct nr_sock *nr = nr_sk(sk);
  470. struct full_sockaddr_ax25 *addr = (struct full_sockaddr_ax25 *)uaddr;
  471. struct net_device *dev;
  472. ax25_uid_assoc *user;
  473. ax25_address *source;
  474. lock_sock(sk);
  475. if (!sock_flag(sk, SOCK_ZAPPED)) {
  476. release_sock(sk);
  477. return -EINVAL;
  478. }
  479. if (addr_len < sizeof(struct sockaddr_ax25) || addr_len > sizeof(struct full_sockaddr_ax25)) {
  480. release_sock(sk);
  481. return -EINVAL;
  482. }
  483. if (addr_len < (addr->fsa_ax25.sax25_ndigis * sizeof(ax25_address) + sizeof(struct sockaddr_ax25))) {
  484. release_sock(sk);
  485. return -EINVAL;
  486. }
  487. if (addr->fsa_ax25.sax25_family != AF_NETROM) {
  488. release_sock(sk);
  489. return -EINVAL;
  490. }
  491. if ((dev = nr_dev_get(&addr->fsa_ax25.sax25_call)) == NULL) {
  492. release_sock(sk);
  493. return -EADDRNOTAVAIL;
  494. }
  495. /*
  496. * Only the super user can set an arbitrary user callsign.
  497. */
  498. if (addr->fsa_ax25.sax25_ndigis == 1) {
  499. if (!capable(CAP_NET_BIND_SERVICE)) {
  500. dev_put(dev);
  501. release_sock(sk);
  502. return -EACCES;
  503. }
  504. nr->user_addr = addr->fsa_digipeater[0];
  505. nr->source_addr = addr->fsa_ax25.sax25_call;
  506. } else {
  507. source = &addr->fsa_ax25.sax25_call;
  508. user = ax25_findbyuid(current_euid());
  509. if (user) {
  510. nr->user_addr = user->call;
  511. ax25_uid_put(user);
  512. } else {
  513. if (ax25_uid_policy && !capable(CAP_NET_BIND_SERVICE)) {
  514. release_sock(sk);
  515. dev_put(dev);
  516. return -EPERM;
  517. }
  518. nr->user_addr = *source;
  519. }
  520. nr->source_addr = *source;
  521. }
  522. nr->device = dev;
  523. nr_insert_socket(sk);
  524. sock_reset_flag(sk, SOCK_ZAPPED);
  525. dev_put(dev);
  526. release_sock(sk);
  527. return 0;
  528. }
  529. static int nr_connect(struct socket *sock, struct sockaddr *uaddr,
  530. int addr_len, int flags)
  531. {
  532. struct sock *sk = sock->sk;
  533. struct nr_sock *nr = nr_sk(sk);
  534. struct sockaddr_ax25 *addr = (struct sockaddr_ax25 *)uaddr;
  535. ax25_address *source = NULL;
  536. ax25_uid_assoc *user;
  537. struct net_device *dev;
  538. int err = 0;
  539. lock_sock(sk);
  540. if (sk->sk_state == TCP_ESTABLISHED && sock->state == SS_CONNECTING) {
  541. sock->state = SS_CONNECTED;
  542. goto out_release; /* Connect completed during a ERESTARTSYS event */
  543. }
  544. if (sk->sk_state == TCP_CLOSE && sock->state == SS_CONNECTING) {
  545. sock->state = SS_UNCONNECTED;
  546. err = -ECONNREFUSED;
  547. goto out_release;
  548. }
  549. if (sk->sk_state == TCP_ESTABLISHED) {
  550. err = -EISCONN; /* No reconnect on a seqpacket socket */
  551. goto out_release;
  552. }
  553. sk->sk_state = TCP_CLOSE;
  554. sock->state = SS_UNCONNECTED;
  555. if (addr_len != sizeof(struct sockaddr_ax25) && addr_len != sizeof(struct full_sockaddr_ax25)) {
  556. err = -EINVAL;
  557. goto out_release;
  558. }
  559. if (addr->sax25_family != AF_NETROM) {
  560. err = -EINVAL;
  561. goto out_release;
  562. }
  563. if (sock_flag(sk, SOCK_ZAPPED)) { /* Must bind first - autobinding in this may or may not work */
  564. sock_reset_flag(sk, SOCK_ZAPPED);
  565. if ((dev = nr_dev_first()) == NULL) {
  566. err = -ENETUNREACH;
  567. goto out_release;
  568. }
  569. source = (ax25_address *)dev->dev_addr;
  570. user = ax25_findbyuid(current_euid());
  571. if (user) {
  572. nr->user_addr = user->call;
  573. ax25_uid_put(user);
  574. } else {
  575. if (ax25_uid_policy && !capable(CAP_NET_ADMIN)) {
  576. dev_put(dev);
  577. err = -EPERM;
  578. goto out_release;
  579. }
  580. nr->user_addr = *source;
  581. }
  582. nr->source_addr = *source;
  583. nr->device = dev;
  584. dev_put(dev);
  585. nr_insert_socket(sk); /* Finish the bind */
  586. }
  587. nr->dest_addr = addr->sax25_call;
  588. release_sock(sk);
  589. circuit = nr_find_next_circuit();
  590. lock_sock(sk);
  591. nr->my_index = circuit / 256;
  592. nr->my_id = circuit % 256;
  593. circuit++;
  594. /* Move to connecting socket, start sending Connect Requests */
  595. sock->state = SS_CONNECTING;
  596. sk->sk_state = TCP_SYN_SENT;
  597. nr_establish_data_link(sk);
  598. nr->state = NR_STATE_1;
  599. nr_start_heartbeat(sk);
  600. /* Now the loop */
  601. if (sk->sk_state != TCP_ESTABLISHED && (flags & O_NONBLOCK)) {
  602. err = -EINPROGRESS;
  603. goto out_release;
  604. }
  605. /*
  606. * A Connect Ack with Choke or timeout or failed routing will go to
  607. * closed.
  608. */
  609. if (sk->sk_state == TCP_SYN_SENT) {
  610. DEFINE_WAIT(wait);
  611. for (;;) {
  612. prepare_to_wait(sk_sleep(sk), &wait,
  613. TASK_INTERRUPTIBLE);
  614. if (sk->sk_state != TCP_SYN_SENT)
  615. break;
  616. if (!signal_pending(current)) {
  617. release_sock(sk);
  618. schedule();
  619. lock_sock(sk);
  620. continue;
  621. }
  622. err = -ERESTARTSYS;
  623. break;
  624. }
  625. finish_wait(sk_sleep(sk), &wait);
  626. if (err)
  627. goto out_release;
  628. }
  629. if (sk->sk_state != TCP_ESTABLISHED) {
  630. sock->state = SS_UNCONNECTED;
  631. err = sock_error(sk); /* Always set at this point */
  632. goto out_release;
  633. }
  634. sock->state = SS_CONNECTED;
  635. out_release:
  636. release_sock(sk);
  637. return err;
  638. }
  639. static int nr_accept(struct socket *sock, struct socket *newsock, int flags)
  640. {
  641. struct sk_buff *skb;
  642. struct sock *newsk;
  643. DEFINE_WAIT(wait);
  644. struct sock *sk;
  645. int err = 0;
  646. if ((sk = sock->sk) == NULL)
  647. return -EINVAL;
  648. lock_sock(sk);
  649. if (sk->sk_type != SOCK_SEQPACKET) {
  650. err = -EOPNOTSUPP;
  651. goto out_release;
  652. }
  653. if (sk->sk_state != TCP_LISTEN) {
  654. err = -EINVAL;
  655. goto out_release;
  656. }
  657. /*
  658. * The write queue this time is holding sockets ready to use
  659. * hooked into the SABM we saved
  660. */
  661. for (;;) {
  662. prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
  663. skb = skb_dequeue(&sk->sk_receive_queue);
  664. if (skb)
  665. break;
  666. if (flags & O_NONBLOCK) {
  667. err = -EWOULDBLOCK;
  668. break;
  669. }
  670. if (!signal_pending(current)) {
  671. release_sock(sk);
  672. schedule();
  673. lock_sock(sk);
  674. continue;
  675. }
  676. err = -ERESTARTSYS;
  677. break;
  678. }
  679. finish_wait(sk_sleep(sk), &wait);
  680. if (err)
  681. goto out_release;
  682. newsk = skb->sk;
  683. sock_graft(newsk, newsock);
  684. /* Now attach up the new socket */
  685. kfree_skb(skb);
  686. sk_acceptq_removed(sk);
  687. out_release:
  688. release_sock(sk);
  689. return err;
  690. }
  691. static int nr_getname(struct socket *sock, struct sockaddr *uaddr,
  692. int *uaddr_len, int peer)
  693. {
  694. struct full_sockaddr_ax25 *sax = (struct full_sockaddr_ax25 *)uaddr;
  695. struct sock *sk = sock->sk;
  696. struct nr_sock *nr = nr_sk(sk);
  697. lock_sock(sk);
  698. if (peer != 0) {
  699. if (sk->sk_state != TCP_ESTABLISHED) {
  700. release_sock(sk);
  701. return -ENOTCONN;
  702. }
  703. sax->fsa_ax25.sax25_family = AF_NETROM;
  704. sax->fsa_ax25.sax25_ndigis = 1;
  705. sax->fsa_ax25.sax25_call = nr->user_addr;
  706. memset(sax->fsa_digipeater, 0, sizeof(sax->fsa_digipeater));
  707. sax->fsa_digipeater[0] = nr->dest_addr;
  708. *uaddr_len = sizeof(struct full_sockaddr_ax25);
  709. } else {
  710. sax->fsa_ax25.sax25_family = AF_NETROM;
  711. sax->fsa_ax25.sax25_ndigis = 0;
  712. sax->fsa_ax25.sax25_call = nr->source_addr;
  713. *uaddr_len = sizeof(struct sockaddr_ax25);
  714. }
  715. release_sock(sk);
  716. return 0;
  717. }
  718. int nr_rx_frame(struct sk_buff *skb, struct net_device *dev)
  719. {
  720. struct sock *sk;
  721. struct sock *make;
  722. struct nr_sock *nr_make;
  723. ax25_address *src, *dest, *user;
  724. unsigned short circuit_index, circuit_id;
  725. unsigned short peer_circuit_index, peer_circuit_id;
  726. unsigned short frametype, flags, window, timeout;
  727. int ret;
  728. skb->sk = NULL; /* Initially we don't know who it's for */
  729. /*
  730. * skb->data points to the netrom frame start
  731. */
  732. src = (ax25_address *)(skb->data + 0);
  733. dest = (ax25_address *)(skb->data + 7);
  734. circuit_index = skb->data[15];
  735. circuit_id = skb->data[16];
  736. peer_circuit_index = skb->data[17];
  737. peer_circuit_id = skb->data[18];
  738. frametype = skb->data[19] & 0x0F;
  739. flags = skb->data[19] & 0xF0;
  740. /*
  741. * Check for an incoming IP over NET/ROM frame.
  742. */
  743. if (frametype == NR_PROTOEXT &&
  744. circuit_index == NR_PROTO_IP && circuit_id == NR_PROTO_IP) {
  745. skb_pull(skb, NR_NETWORK_LEN + NR_TRANSPORT_LEN);
  746. skb_reset_transport_header(skb);
  747. return nr_rx_ip(skb, dev);
  748. }
  749. /*
  750. * Find an existing socket connection, based on circuit ID, if it's
  751. * a Connect Request base it on their circuit ID.
  752. *
  753. * Circuit ID 0/0 is not valid but it could still be a "reset" for a
  754. * circuit that no longer exists at the other end ...
  755. */
  756. sk = NULL;
  757. if (circuit_index == 0 && circuit_id == 0) {
  758. if (frametype == NR_CONNACK && flags == NR_CHOKE_FLAG)
  759. sk = nr_find_peer(peer_circuit_index, peer_circuit_id, src);
  760. } else {
  761. if (frametype == NR_CONNREQ)
  762. sk = nr_find_peer(circuit_index, circuit_id, src);
  763. else
  764. sk = nr_find_socket(circuit_index, circuit_id);
  765. }
  766. if (sk != NULL) {
  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. return ret;
  775. }
  776. /*
  777. * Now it should be a CONNREQ.
  778. */
  779. if (frametype != NR_CONNREQ) {
  780. /*
  781. * Here it would be nice to be able to send a reset but
  782. * NET/ROM doesn't have one. We've tried to extend the protocol
  783. * by sending NR_CONNACK | NR_CHOKE_FLAGS replies but that
  784. * apparently kills BPQ boxes... :-(
  785. * So now we try to follow the established behaviour of
  786. * G8PZT's Xrouter which is sending packets with command type 7
  787. * as an extension of the protocol.
  788. */
  789. if (sysctl_netrom_reset_circuit &&
  790. (frametype != NR_RESET || flags != 0))
  791. nr_transmit_reset(skb, 1);
  792. return 0;
  793. }
  794. sk = nr_find_listener(dest);
  795. user = (ax25_address *)(skb->data + 21);
  796. if (sk == NULL || sk_acceptq_is_full(sk) ||
  797. (make = nr_make_new(sk)) == NULL) {
  798. nr_transmit_refusal(skb, 0);
  799. if (sk)
  800. bh_unlock_sock(sk);
  801. return 0;
  802. }
  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, skb->len);
  842. bh_unlock_sock(sk);
  843. nr_insert_socket(make);
  844. nr_start_heartbeat(make);
  845. nr_start_idletimer(make);
  846. return 1;
  847. }
  848. static int nr_sendmsg(struct kiocb *iocb, struct socket *sock,
  849. struct msghdr *msg, size_t len)
  850. {
  851. struct sock *sk = sock->sk;
  852. struct nr_sock *nr = nr_sk(sk);
  853. struct sockaddr_ax25 *usax = (struct sockaddr_ax25 *)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_fromiovec(skb_transport_header(skb), msg->msg_iov, 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 kiocb *iocb, struct socket *sock,
  940. struct msghdr *msg, size_t size, int flags)
  941. {
  942. struct sock *sk = sock->sk;
  943. struct sockaddr_ax25 *sax = (struct sockaddr_ax25 *)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_iovec(skb, 0, msg->msg_iov, 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, 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_net_fops_create(&init_net, "nr", S_IRUGO, &nr_info_fops);
  1203. proc_net_fops_create(&init_net, "nr_neigh", S_IRUGO, &nr_neigh_fops);
  1204. proc_net_fops_create(&init_net, "nr_nodes", S_IRUGO, &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. proc_net_remove(&init_net, "nr");
  1228. proc_net_remove(&init_net, "nr_neigh");
  1229. proc_net_remove(&init_net, "nr_nodes");
  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);