x25_link.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. /*
  2. * X.25 Packet Layer release 002
  3. *
  4. * This is ALPHA test software. This code may break your machine,
  5. * randomly fail to work with new releases, misbehave and/or generally
  6. * screw up. It might even work.
  7. *
  8. * This code REQUIRES 2.1.15 or higher
  9. *
  10. * This module:
  11. * This module is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public License
  13. * as published by the Free Software Foundation; either version
  14. * 2 of the License, or (at your option) any later version.
  15. *
  16. * History
  17. * X.25 001 Jonathan Naylor Started coding.
  18. * X.25 002 Jonathan Naylor New timer architecture.
  19. * mar/20/00 Daniela Squassoni Disabling/enabling of facilities
  20. * negotiation.
  21. * 2000-09-04 Henner Eisen dev_hold() / dev_put() for x25_neigh.
  22. */
  23. #include <linux/kernel.h>
  24. #include <linux/jiffies.h>
  25. #include <linux/timer.h>
  26. #include <linux/slab.h>
  27. #include <linux/netdevice.h>
  28. #include <linux/skbuff.h>
  29. #include <asm/uaccess.h>
  30. #include <linux/init.h>
  31. #include <net/x25.h>
  32. LIST_HEAD(x25_neigh_list);
  33. DEFINE_RWLOCK(x25_neigh_list_lock);
  34. static void x25_t20timer_expiry(unsigned long);
  35. static void x25_transmit_restart_confirmation(struct x25_neigh *nb);
  36. static void x25_transmit_restart_request(struct x25_neigh *nb);
  37. /*
  38. * Linux set/reset timer routines
  39. */
  40. static inline void x25_start_t20timer(struct x25_neigh *nb)
  41. {
  42. mod_timer(&nb->t20timer, jiffies + nb->t20);
  43. }
  44. static void x25_t20timer_expiry(unsigned long param)
  45. {
  46. struct x25_neigh *nb = (struct x25_neigh *)param;
  47. x25_transmit_restart_request(nb);
  48. x25_start_t20timer(nb);
  49. }
  50. static inline void x25_stop_t20timer(struct x25_neigh *nb)
  51. {
  52. del_timer(&nb->t20timer);
  53. }
  54. static inline int x25_t20timer_pending(struct x25_neigh *nb)
  55. {
  56. return timer_pending(&nb->t20timer);
  57. }
  58. /*
  59. * This handles all restart and diagnostic frames.
  60. */
  61. void x25_link_control(struct sk_buff *skb, struct x25_neigh *nb,
  62. unsigned short frametype)
  63. {
  64. struct sk_buff *skbn;
  65. int confirm;
  66. switch (frametype) {
  67. case X25_RESTART_REQUEST:
  68. confirm = !x25_t20timer_pending(nb);
  69. x25_stop_t20timer(nb);
  70. nb->state = X25_LINK_STATE_3;
  71. if (confirm)
  72. x25_transmit_restart_confirmation(nb);
  73. break;
  74. case X25_RESTART_CONFIRMATION:
  75. x25_stop_t20timer(nb);
  76. nb->state = X25_LINK_STATE_3;
  77. break;
  78. case X25_DIAGNOSTIC:
  79. printk(KERN_WARNING "x25: diagnostic #%d - "
  80. "%02X %02X %02X\n",
  81. skb->data[3], skb->data[4],
  82. skb->data[5], skb->data[6]);
  83. break;
  84. default:
  85. printk(KERN_WARNING "x25: received unknown %02X "
  86. "with LCI 000\n", frametype);
  87. break;
  88. }
  89. if (nb->state == X25_LINK_STATE_3)
  90. while ((skbn = skb_dequeue(&nb->queue)) != NULL)
  91. x25_send_frame(skbn, nb);
  92. }
  93. /*
  94. * This routine is called when a Restart Request is needed
  95. */
  96. static void x25_transmit_restart_request(struct x25_neigh *nb)
  97. {
  98. unsigned char *dptr;
  99. int len = X25_MAX_L2_LEN + X25_STD_MIN_LEN + 2;
  100. struct sk_buff *skb = alloc_skb(len, GFP_ATOMIC);
  101. if (!skb)
  102. return;
  103. skb_reserve(skb, X25_MAX_L2_LEN);
  104. dptr = skb_put(skb, X25_STD_MIN_LEN + 2);
  105. *dptr++ = nb->extended ? X25_GFI_EXTSEQ : X25_GFI_STDSEQ;
  106. *dptr++ = 0x00;
  107. *dptr++ = X25_RESTART_REQUEST;
  108. *dptr++ = 0x00;
  109. *dptr++ = 0;
  110. skb->sk = NULL;
  111. x25_send_frame(skb, nb);
  112. }
  113. /*
  114. * This routine is called when a Restart Confirmation is needed
  115. */
  116. static void x25_transmit_restart_confirmation(struct x25_neigh *nb)
  117. {
  118. unsigned char *dptr;
  119. int len = X25_MAX_L2_LEN + X25_STD_MIN_LEN;
  120. struct sk_buff *skb = alloc_skb(len, GFP_ATOMIC);
  121. if (!skb)
  122. return;
  123. skb_reserve(skb, X25_MAX_L2_LEN);
  124. dptr = skb_put(skb, X25_STD_MIN_LEN);
  125. *dptr++ = nb->extended ? X25_GFI_EXTSEQ : X25_GFI_STDSEQ;
  126. *dptr++ = 0x00;
  127. *dptr++ = X25_RESTART_CONFIRMATION;
  128. skb->sk = NULL;
  129. x25_send_frame(skb, nb);
  130. }
  131. /*
  132. * This routine is called when a Clear Request is needed outside of the context
  133. * of a connected socket.
  134. */
  135. void x25_transmit_clear_request(struct x25_neigh *nb, unsigned int lci,
  136. unsigned char cause)
  137. {
  138. unsigned char *dptr;
  139. int len = X25_MAX_L2_LEN + X25_STD_MIN_LEN + 2;
  140. struct sk_buff *skb = alloc_skb(len, GFP_ATOMIC);
  141. if (!skb)
  142. return;
  143. skb_reserve(skb, X25_MAX_L2_LEN);
  144. dptr = skb_put(skb, X25_STD_MIN_LEN + 2);
  145. *dptr++ = ((lci >> 8) & 0x0F) | (nb->extended ?
  146. X25_GFI_EXTSEQ :
  147. X25_GFI_STDSEQ);
  148. *dptr++ = (lci >> 0) & 0xFF;
  149. *dptr++ = X25_CLEAR_REQUEST;
  150. *dptr++ = cause;
  151. *dptr++ = 0x00;
  152. skb->sk = NULL;
  153. x25_send_frame(skb, nb);
  154. }
  155. void x25_transmit_link(struct sk_buff *skb, struct x25_neigh *nb)
  156. {
  157. switch (nb->state) {
  158. case X25_LINK_STATE_0:
  159. skb_queue_tail(&nb->queue, skb);
  160. nb->state = X25_LINK_STATE_1;
  161. x25_establish_link(nb);
  162. break;
  163. case X25_LINK_STATE_1:
  164. case X25_LINK_STATE_2:
  165. skb_queue_tail(&nb->queue, skb);
  166. break;
  167. case X25_LINK_STATE_3:
  168. x25_send_frame(skb, nb);
  169. break;
  170. }
  171. }
  172. /*
  173. * Called when the link layer has become established.
  174. */
  175. void x25_link_established(struct x25_neigh *nb)
  176. {
  177. switch (nb->state) {
  178. case X25_LINK_STATE_0:
  179. nb->state = X25_LINK_STATE_2;
  180. break;
  181. case X25_LINK_STATE_1:
  182. x25_transmit_restart_request(nb);
  183. nb->state = X25_LINK_STATE_2;
  184. x25_start_t20timer(nb);
  185. break;
  186. }
  187. }
  188. /*
  189. * Called when the link layer has terminated, or an establishment
  190. * request has failed.
  191. */
  192. void x25_link_terminated(struct x25_neigh *nb)
  193. {
  194. nb->state = X25_LINK_STATE_0;
  195. /* Out of order: clear existing virtual calls (X.25 03/93 4.6.3) */
  196. x25_kill_by_neigh(nb);
  197. }
  198. /*
  199. * Add a new device.
  200. */
  201. void x25_link_device_up(struct net_device *dev)
  202. {
  203. struct x25_neigh *nb = kmalloc(sizeof(*nb), GFP_ATOMIC);
  204. if (!nb)
  205. return;
  206. skb_queue_head_init(&nb->queue);
  207. setup_timer(&nb->t20timer, x25_t20timer_expiry, (unsigned long)nb);
  208. dev_hold(dev);
  209. nb->dev = dev;
  210. nb->state = X25_LINK_STATE_0;
  211. nb->extended = 0;
  212. /*
  213. * Enables negotiation
  214. */
  215. nb->global_facil_mask = X25_MASK_REVERSE |
  216. X25_MASK_THROUGHPUT |
  217. X25_MASK_PACKET_SIZE |
  218. X25_MASK_WINDOW_SIZE;
  219. nb->t20 = sysctl_x25_restart_request_timeout;
  220. atomic_set(&nb->refcnt, 1);
  221. write_lock_bh(&x25_neigh_list_lock);
  222. list_add(&nb->node, &x25_neigh_list);
  223. write_unlock_bh(&x25_neigh_list_lock);
  224. }
  225. /**
  226. * __x25_remove_neigh - remove neighbour from x25_neigh_list
  227. * @nb - neigh to remove
  228. *
  229. * Remove neighbour from x25_neigh_list. If it was there.
  230. * Caller must hold x25_neigh_list_lock.
  231. */
  232. static void __x25_remove_neigh(struct x25_neigh *nb)
  233. {
  234. skb_queue_purge(&nb->queue);
  235. x25_stop_t20timer(nb);
  236. if (nb->node.next) {
  237. list_del(&nb->node);
  238. x25_neigh_put(nb);
  239. }
  240. }
  241. /*
  242. * A device has been removed, remove its links.
  243. */
  244. void x25_link_device_down(struct net_device *dev)
  245. {
  246. struct x25_neigh *nb;
  247. struct list_head *entry, *tmp;
  248. write_lock_bh(&x25_neigh_list_lock);
  249. list_for_each_safe(entry, tmp, &x25_neigh_list) {
  250. nb = list_entry(entry, struct x25_neigh, node);
  251. if (nb->dev == dev) {
  252. __x25_remove_neigh(nb);
  253. dev_put(dev);
  254. }
  255. }
  256. write_unlock_bh(&x25_neigh_list_lock);
  257. }
  258. /*
  259. * Given a device, return the neighbour address.
  260. */
  261. struct x25_neigh *x25_get_neigh(struct net_device *dev)
  262. {
  263. struct x25_neigh *nb, *use = NULL;
  264. struct list_head *entry;
  265. read_lock_bh(&x25_neigh_list_lock);
  266. list_for_each(entry, &x25_neigh_list) {
  267. nb = list_entry(entry, struct x25_neigh, node);
  268. if (nb->dev == dev) {
  269. use = nb;
  270. break;
  271. }
  272. }
  273. if (use)
  274. x25_neigh_hold(use);
  275. read_unlock_bh(&x25_neigh_list_lock);
  276. return use;
  277. }
  278. /*
  279. * Handle the ioctls that control the subscription functions.
  280. */
  281. int x25_subscr_ioctl(unsigned int cmd, void __user *arg)
  282. {
  283. struct x25_subscrip_struct x25_subscr;
  284. struct x25_neigh *nb;
  285. struct net_device *dev;
  286. int rc = -EINVAL;
  287. if (cmd != SIOCX25GSUBSCRIP && cmd != SIOCX25SSUBSCRIP)
  288. goto out;
  289. rc = -EFAULT;
  290. if (copy_from_user(&x25_subscr, arg, sizeof(x25_subscr)))
  291. goto out;
  292. rc = -EINVAL;
  293. if ((dev = x25_dev_get(x25_subscr.device)) == NULL)
  294. goto out;
  295. if ((nb = x25_get_neigh(dev)) == NULL)
  296. goto out_dev_put;
  297. dev_put(dev);
  298. if (cmd == SIOCX25GSUBSCRIP) {
  299. read_lock_bh(&x25_neigh_list_lock);
  300. x25_subscr.extended = nb->extended;
  301. x25_subscr.global_facil_mask = nb->global_facil_mask;
  302. read_unlock_bh(&x25_neigh_list_lock);
  303. rc = copy_to_user(arg, &x25_subscr,
  304. sizeof(x25_subscr)) ? -EFAULT : 0;
  305. } else {
  306. rc = -EINVAL;
  307. if (!(x25_subscr.extended && x25_subscr.extended != 1)) {
  308. rc = 0;
  309. write_lock_bh(&x25_neigh_list_lock);
  310. nb->extended = x25_subscr.extended;
  311. nb->global_facil_mask = x25_subscr.global_facil_mask;
  312. write_unlock_bh(&x25_neigh_list_lock);
  313. }
  314. }
  315. x25_neigh_put(nb);
  316. out:
  317. return rc;
  318. out_dev_put:
  319. dev_put(dev);
  320. goto out;
  321. }
  322. /*
  323. * Release all memory associated with X.25 neighbour structures.
  324. */
  325. void __exit x25_link_free(void)
  326. {
  327. struct x25_neigh *nb;
  328. struct list_head *entry, *tmp;
  329. write_lock_bh(&x25_neigh_list_lock);
  330. list_for_each_safe(entry, tmp, &x25_neigh_list) {
  331. struct net_device *dev;
  332. nb = list_entry(entry, struct x25_neigh, node);
  333. dev = nb->dev;
  334. __x25_remove_neigh(nb);
  335. dev_put(dev);
  336. }
  337. write_unlock_bh(&x25_neigh_list_lock);
  338. }