x25_link.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:10k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

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