af_netrom.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518
  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 ([email protected])
  8. * Copyright Alan Cox GW4PTS ([email protected])
  9. * Copyright Darryl Miles G7LED ([email protected])
  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_orphan(skb);
  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. sock_hold(make);
  805. skb->sk = make;
  806. skb->destructor = sock_efree;
  807. make->sk_state = TCP_ESTABLISHED;
  808. /* Fill in his circuit details */
  809. nr_make = nr_sk(make);
  810. nr_make->source_addr = *dest;
  811. nr_make->dest_addr = *src;
  812. nr_make->user_addr = *user;
  813. nr_make->your_index = circuit_index;
  814. nr_make->your_id = circuit_id;
  815. bh_unlock_sock(sk);
  816. circuit = nr_find_next_circuit();
  817. bh_lock_sock(sk);
  818. nr_make->my_index = circuit / 256;
  819. nr_make->my_id = circuit % 256;
  820. circuit++;
  821. /* Window negotiation */
  822. if (window < nr_make->window)
  823. nr_make->window = window;
  824. /* L4 timeout negotiation */
  825. if (skb->len == 37) {
  826. timeout = skb->data[36] * 256 + skb->data[35];
  827. if (timeout * HZ < nr_make->t1)
  828. nr_make->t1 = timeout * HZ;
  829. nr_make->bpqext = 1;
  830. } else {
  831. nr_make->bpqext = 0;
  832. }
  833. nr_write_internal(make, NR_CONNACK);
  834. nr_make->condition = 0x00;
  835. nr_make->vs = 0;
  836. nr_make->va = 0;
  837. nr_make->vr = 0;
  838. nr_make->vl = 0;
  839. nr_make->state = NR_STATE_3;
  840. sk_acceptq_added(sk);
  841. skb_queue_head(&sk->sk_receive_queue, skb);
  842. if (!sock_flag(sk, SOCK_DEAD))
  843. sk->sk_data_ready(sk);
  844. bh_unlock_sock(sk);
  845. sock_put(sk);
  846. nr_insert_socket(make);
  847. nr_start_heartbeat(make);
  848. nr_start_idletimer(make);
  849. return 1;
  850. }
  851. static int nr_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
  852. {
  853. struct sock *sk = sock->sk;
  854. struct nr_sock *nr = nr_sk(sk);
  855. DECLARE_SOCKADDR(struct sockaddr_ax25 *, usax, msg->msg_name);
  856. int err;
  857. struct sockaddr_ax25 sax;
  858. struct sk_buff *skb;
  859. unsigned char *asmptr;
  860. int size;
  861. if (msg->msg_flags & ~(MSG_DONTWAIT|MSG_EOR|MSG_CMSG_COMPAT))
  862. return -EINVAL;
  863. lock_sock(sk);
  864. if (sock_flag(sk, SOCK_ZAPPED)) {
  865. err = -EADDRNOTAVAIL;
  866. goto out;
  867. }
  868. if (sk->sk_shutdown & SEND_SHUTDOWN) {
  869. send_sig(SIGPIPE, current, 0);
  870. err = -EPIPE;
  871. goto out;
  872. }
  873. if (nr->device == NULL) {
  874. err = -ENETUNREACH;
  875. goto out;
  876. }
  877. if (usax) {
  878. if (msg->msg_namelen < sizeof(sax)) {
  879. err = -EINVAL;
  880. goto out;
  881. }
  882. sax = *usax;
  883. if (ax25cmp(&nr->dest_addr, &sax.sax25_call) != 0) {
  884. err = -EISCONN;
  885. goto out;
  886. }
  887. if (sax.sax25_family != AF_NETROM) {
  888. err = -EINVAL;
  889. goto out;
  890. }
  891. } else {
  892. if (sk->sk_state != TCP_ESTABLISHED) {
  893. err = -ENOTCONN;
  894. goto out;
  895. }
  896. sax.sax25_family = AF_NETROM;
  897. sax.sax25_call = nr->dest_addr;
  898. }
  899. /* Build a packet - the conventional user limit is 236 bytes. We can
  900. do ludicrously large NetROM frames but must not overflow */
  901. if (len > 65536) {
  902. err = -EMSGSIZE;
  903. goto out;
  904. }
  905. size = len + NR_NETWORK_LEN + NR_TRANSPORT_LEN;
  906. if ((skb = sock_alloc_send_skb(sk, size, msg->msg_flags & MSG_DONTWAIT, &err)) == NULL)
  907. goto out;
  908. skb_reserve(skb, size - len);
  909. skb_reset_transport_header(skb);
  910. /*
  911. * Push down the NET/ROM header
  912. */
  913. asmptr = skb_push(skb, NR_TRANSPORT_LEN);
  914. /* Build a NET/ROM Transport header */
  915. *asmptr++ = nr->your_index;
  916. *asmptr++ = nr->your_id;
  917. *asmptr++ = 0; /* To be filled in later */
  918. *asmptr++ = 0; /* Ditto */
  919. *asmptr++ = NR_INFO;
  920. /*
  921. * Put the data on the end
  922. */
  923. skb_put(skb, len);
  924. /* User data follows immediately after the NET/ROM transport header */
  925. if (memcpy_from_msg(skb_transport_header(skb), msg, len)) {
  926. kfree_skb(skb);
  927. err = -EFAULT;
  928. goto out;
  929. }
  930. if (sk->sk_state != TCP_ESTABLISHED) {
  931. kfree_skb(skb);
  932. err = -ENOTCONN;
  933. goto out;
  934. }
  935. nr_output(sk, skb); /* Shove it onto the queue */
  936. err = len;
  937. out:
  938. release_sock(sk);
  939. return err;
  940. }
  941. static int nr_recvmsg(struct socket *sock, struct msghdr *msg, size_t size,
  942. int flags)
  943. {
  944. struct sock *sk = sock->sk;
  945. DECLARE_SOCKADDR(struct sockaddr_ax25 *, sax, msg->msg_name);
  946. size_t copied;
  947. struct sk_buff *skb;
  948. int er;
  949. /*
  950. * This works for seqpacket too. The receiver has ordered the queue for
  951. * us! We do one quick check first though
  952. */
  953. lock_sock(sk);
  954. if (sk->sk_state != TCP_ESTABLISHED) {
  955. release_sock(sk);
  956. return -ENOTCONN;
  957. }
  958. /* Now we can treat all alike */
  959. if ((skb = skb_recv_datagram(sk, flags & ~MSG_DONTWAIT, flags & MSG_DONTWAIT, &er)) == NULL) {
  960. release_sock(sk);
  961. return er;
  962. }
  963. skb_reset_transport_header(skb);
  964. copied = skb->len;
  965. if (copied > size) {
  966. copied = size;
  967. msg->msg_flags |= MSG_TRUNC;
  968. }
  969. er = skb_copy_datagram_msg(skb, 0, msg, copied);
  970. if (er < 0) {
  971. skb_free_datagram(sk, skb);
  972. release_sock(sk);
  973. return er;
  974. }
  975. if (sax != NULL) {
  976. memset(sax, 0, sizeof(*sax));
  977. sax->sax25_family = AF_NETROM;
  978. skb_copy_from_linear_data_offset(skb, 7, sax->sax25_call.ax25_call,
  979. AX25_ADDR_LEN);
  980. msg->msg_namelen = sizeof(*sax);
  981. }
  982. skb_free_datagram(sk, skb);
  983. release_sock(sk);
  984. return copied;
  985. }
  986. static int nr_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
  987. {
  988. struct sock *sk = sock->sk;
  989. void __user *argp = (void __user *)arg;
  990. int ret;
  991. switch (cmd) {
  992. case TIOCOUTQ: {
  993. long amount;
  994. lock_sock(sk);
  995. amount = sk->sk_sndbuf - sk_wmem_alloc_get(sk);
  996. if (amount < 0)
  997. amount = 0;
  998. release_sock(sk);
  999. return put_user(amount, (int __user *)argp);
  1000. }
  1001. case TIOCINQ: {
  1002. struct sk_buff *skb;
  1003. long amount = 0L;
  1004. lock_sock(sk);
  1005. /* These two are safe on a single CPU system as only user tasks fiddle here */
  1006. if ((skb = skb_peek(&sk->sk_receive_queue)) != NULL)
  1007. amount = skb->len;
  1008. release_sock(sk);
  1009. return put_user(amount, (int __user *)argp);
  1010. }
  1011. case SIOCGSTAMP:
  1012. lock_sock(sk);
  1013. ret = sock_get_timestamp(sk, argp);
  1014. release_sock(sk);
  1015. return ret;
  1016. case SIOCGSTAMPNS:
  1017. lock_sock(sk);
  1018. ret = sock_get_timestampns(sk, argp);
  1019. release_sock(sk);
  1020. return ret;
  1021. case SIOCGIFADDR:
  1022. case SIOCSIFADDR:
  1023. case SIOCGIFDSTADDR:
  1024. case SIOCSIFDSTADDR:
  1025. case SIOCGIFBRDADDR:
  1026. case SIOCSIFBRDADDR:
  1027. case SIOCGIFNETMASK:
  1028. case SIOCSIFNETMASK:
  1029. case SIOCGIFMETRIC:
  1030. case SIOCSIFMETRIC:
  1031. return -EINVAL;
  1032. case SIOCADDRT:
  1033. case SIOCDELRT:
  1034. case SIOCNRDECOBS:
  1035. if (!capable(CAP_NET_ADMIN))
  1036. return -EPERM;
  1037. return nr_rt_ioctl(cmd, argp);
  1038. default:
  1039. return -ENOIOCTLCMD;
  1040. }
  1041. return 0;
  1042. }
  1043. #ifdef CONFIG_PROC_FS
  1044. static void *nr_info_start(struct seq_file *seq, loff_t *pos)
  1045. {
  1046. spin_lock_bh(&nr_list_lock);
  1047. return seq_hlist_start_head(&nr_list, *pos);
  1048. }
  1049. static void *nr_info_next(struct seq_file *seq, void *v, loff_t *pos)
  1050. {
  1051. return seq_hlist_next(v, &nr_list, pos);
  1052. }
  1053. static void nr_info_stop(struct seq_file *seq, void *v)
  1054. {
  1055. spin_unlock_bh(&nr_list_lock);
  1056. }
  1057. static int nr_info_show(struct seq_file *seq, void *v)
  1058. {
  1059. struct sock *s = sk_entry(v);
  1060. struct net_device *dev;
  1061. struct nr_sock *nr;
  1062. const char *devname;
  1063. char buf[11];
  1064. if (v == SEQ_START_TOKEN)
  1065. seq_puts(seq,
  1066. "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");
  1067. else {
  1068. bh_lock_sock(s);
  1069. nr = nr_sk(s);
  1070. if ((dev = nr->device) == NULL)
  1071. devname = "???";
  1072. else
  1073. devname = dev->name;
  1074. seq_printf(seq, "%-9s ", ax2asc(buf, &nr->user_addr));
  1075. seq_printf(seq, "%-9s ", ax2asc(buf, &nr->dest_addr));
  1076. seq_printf(seq,
  1077. "%-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",
  1078. ax2asc(buf, &nr->source_addr),
  1079. devname,
  1080. nr->my_index,
  1081. nr->my_id,
  1082. nr->your_index,
  1083. nr->your_id,
  1084. nr->state,
  1085. nr->vs,
  1086. nr->vr,
  1087. nr->va,
  1088. ax25_display_timer(&nr->t1timer) / HZ,
  1089. nr->t1 / HZ,
  1090. ax25_display_timer(&nr->t2timer) / HZ,
  1091. nr->t2 / HZ,
  1092. ax25_display_timer(&nr->t4timer) / HZ,
  1093. nr->t4 / HZ,
  1094. ax25_display_timer(&nr->idletimer) / (60 * HZ),
  1095. nr->idle / (60 * HZ),
  1096. nr->n2count,
  1097. nr->n2,
  1098. nr->window,
  1099. sk_wmem_alloc_get(s),
  1100. sk_rmem_alloc_get(s),
  1101. s->sk_socket ? SOCK_INODE(s->sk_socket)->i_ino : 0L);
  1102. bh_unlock_sock(s);
  1103. }
  1104. return 0;
  1105. }
  1106. static const struct seq_operations nr_info_seqops = {
  1107. .start = nr_info_start,
  1108. .next = nr_info_next,
  1109. .stop = nr_info_stop,
  1110. .show = nr_info_show,
  1111. };
  1112. static int nr_info_open(struct inode *inode, struct file *file)
  1113. {
  1114. return seq_open(file, &nr_info_seqops);
  1115. }
  1116. static const struct file_operations nr_info_fops = {
  1117. .owner = THIS_MODULE,
  1118. .open = nr_info_open,
  1119. .read = seq_read,
  1120. .llseek = seq_lseek,
  1121. .release = seq_release,
  1122. };
  1123. #endif /* CONFIG_PROC_FS */
  1124. static const struct net_proto_family nr_family_ops = {
  1125. .family = PF_NETROM,
  1126. .create = nr_create,
  1127. .owner = THIS_MODULE,
  1128. };
  1129. static const struct proto_ops nr_proto_ops = {
  1130. .family = PF_NETROM,
  1131. .owner = THIS_MODULE,
  1132. .release = nr_release,
  1133. .bind = nr_bind,
  1134. .connect = nr_connect,
  1135. .socketpair = sock_no_socketpair,
  1136. .accept = nr_accept,
  1137. .getname = nr_getname,
  1138. .poll = datagram_poll,
  1139. .ioctl = nr_ioctl,
  1140. .listen = nr_listen,
  1141. .shutdown = sock_no_shutdown,
  1142. .setsockopt = nr_setsockopt,
  1143. .getsockopt = nr_getsockopt,
  1144. .sendmsg = nr_sendmsg,
  1145. .recvmsg = nr_recvmsg,
  1146. .mmap = sock_no_mmap,
  1147. .sendpage = sock_no_sendpage,
  1148. };
  1149. static struct notifier_block nr_dev_notifier = {
  1150. .notifier_call = nr_device_event,
  1151. };
  1152. static struct net_device **dev_nr;
  1153. static struct ax25_protocol nr_pid = {
  1154. .pid = AX25_P_NETROM,
  1155. .func = nr_route_frame
  1156. };
  1157. static struct ax25_linkfail nr_linkfail_notifier = {
  1158. .func = nr_link_failed,
  1159. };
  1160. static int __init nr_proto_init(void)
  1161. {
  1162. int i;
  1163. int rc = proto_register(&nr_proto, 0);
  1164. if (rc != 0)
  1165. goto out;
  1166. if (nr_ndevs > 0x7fffffff/sizeof(struct net_device *)) {
  1167. printk(KERN_ERR "NET/ROM: nr_proto_init - nr_ndevs parameter to large\n");
  1168. return -1;
  1169. }
  1170. dev_nr = kzalloc(nr_ndevs * sizeof(struct net_device *), GFP_KERNEL);
  1171. if (dev_nr == NULL) {
  1172. printk(KERN_ERR "NET/ROM: nr_proto_init - unable to allocate device array\n");
  1173. return -1;
  1174. }
  1175. for (i = 0; i < nr_ndevs; i++) {
  1176. char name[IFNAMSIZ];
  1177. struct net_device *dev;
  1178. sprintf(name, "nr%d", i);
  1179. dev = alloc_netdev(0, name, NET_NAME_UNKNOWN, nr_setup);
  1180. if (!dev) {
  1181. printk(KERN_ERR "NET/ROM: nr_proto_init - unable to allocate device structure\n");
  1182. goto fail;
  1183. }
  1184. dev->base_addr = i;
  1185. if (register_netdev(dev)) {
  1186. printk(KERN_ERR "NET/ROM: nr_proto_init - unable to register network device\n");
  1187. free_netdev(dev);
  1188. goto fail;
  1189. }
  1190. nr_set_lockdep_key(dev);
  1191. dev_nr[i] = dev;
  1192. }
  1193. if (sock_register(&nr_family_ops)) {
  1194. printk(KERN_ERR "NET/ROM: nr_proto_init - unable to register socket family\n");
  1195. goto fail;
  1196. }
  1197. register_netdevice_notifier(&nr_dev_notifier);
  1198. ax25_register_pid(&nr_pid);
  1199. ax25_linkfail_register(&nr_linkfail_notifier);
  1200. #ifdef CONFIG_SYSCTL
  1201. nr_register_sysctl();
  1202. #endif
  1203. nr_loopback_init();
  1204. proc_create("nr", S_IRUGO, init_net.proc_net, &nr_info_fops);
  1205. proc_create("nr_neigh", S_IRUGO, init_net.proc_net, &nr_neigh_fops);
  1206. proc_create("nr_nodes", S_IRUGO, init_net.proc_net, &nr_nodes_fops);
  1207. out:
  1208. return rc;
  1209. fail:
  1210. while (--i >= 0) {
  1211. unregister_netdev(dev_nr[i]);
  1212. free_netdev(dev_nr[i]);
  1213. }
  1214. kfree(dev_nr);
  1215. proto_unregister(&nr_proto);
  1216. rc = -1;
  1217. goto out;
  1218. }
  1219. module_init(nr_proto_init);
  1220. module_param(nr_ndevs, int, 0);
  1221. MODULE_PARM_DESC(nr_ndevs, "number of NET/ROM devices");
  1222. MODULE_AUTHOR("Jonathan Naylor G4KLX <[email protected]>");
  1223. MODULE_DESCRIPTION("The amateur radio NET/ROM network and transport layer protocol");
  1224. MODULE_LICENSE("GPL");
  1225. MODULE_ALIAS_NETPROTO(PF_NETROM);
  1226. static void __exit nr_exit(void)
  1227. {
  1228. int i;
  1229. remove_proc_entry("nr", init_net.proc_net);
  1230. remove_proc_entry("nr_neigh", init_net.proc_net);
  1231. remove_proc_entry("nr_nodes", init_net.proc_net);
  1232. nr_loopback_clear();
  1233. nr_rt_free();
  1234. #ifdef CONFIG_SYSCTL
  1235. nr_unregister_sysctl();
  1236. #endif
  1237. ax25_linkfail_release(&nr_linkfail_notifier);
  1238. ax25_protocol_release(AX25_P_NETROM);
  1239. unregister_netdevice_notifier(&nr_dev_notifier);
  1240. sock_unregister(PF_NETROM);
  1241. for (i = 0; i < nr_ndevs; i++) {
  1242. struct net_device *dev = dev_nr[i];
  1243. if (dev) {
  1244. unregister_netdev(dev);
  1245. free_netdev(dev);
  1246. }
  1247. }
  1248. kfree(dev_nr);
  1249. proto_unregister(&nr_proto);
  1250. }
  1251. module_exit(nr_exit);