x25_link.c 8.7 KB

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