x25_link.c 8.9 KB

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