x25_dev.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. * 2000-09-04 Henner Eisen Prevent freeing a dangling skb.
  18. */
  19. #include <linux/kernel.h>
  20. #include <linux/netdevice.h>
  21. #include <linux/skbuff.h>
  22. #include <linux/slab.h>
  23. #include <net/sock.h>
  24. #include <linux/if_arp.h>
  25. #include <net/x25.h>
  26. #include <net/x25device.h>
  27. static int x25_receive_data(struct sk_buff *skb, struct x25_neigh *nb)
  28. {
  29. struct sock *sk;
  30. unsigned short frametype;
  31. unsigned int lci;
  32. frametype = skb->data[2];
  33. lci = ((skb->data[0] << 8) & 0xF00) + ((skb->data[1] << 0) & 0x0FF);
  34. /*
  35. * LCI of zero is always for us, and its always a link control
  36. * frame.
  37. */
  38. if (lci == 0) {
  39. x25_link_control(skb, nb, frametype);
  40. return 0;
  41. }
  42. /*
  43. * Find an existing socket.
  44. */
  45. if ((sk = x25_find_socket(lci, nb)) != NULL) {
  46. int queued = 1;
  47. skb_reset_transport_header(skb);
  48. bh_lock_sock(sk);
  49. if (!sock_owned_by_user(sk)) {
  50. queued = x25_process_rx_frame(sk, skb);
  51. } else {
  52. queued = !sk_add_backlog(sk, skb);
  53. }
  54. bh_unlock_sock(sk);
  55. sock_put(sk);
  56. return queued;
  57. }
  58. /*
  59. * Is is a Call Request ? if so process it.
  60. */
  61. if (frametype == X25_CALL_REQUEST)
  62. return x25_rx_call_request(skb, nb, lci);
  63. /*
  64. * Its not a Call Request, nor is it a control frame.
  65. * Can we forward it?
  66. */
  67. if (x25_forward_data(lci, nb, skb)) {
  68. if (frametype == X25_CLEAR_CONFIRMATION) {
  69. x25_clear_forward_by_lci(lci);
  70. }
  71. kfree_skb(skb);
  72. return 1;
  73. }
  74. /*
  75. x25_transmit_clear_request(nb, lci, 0x0D);
  76. */
  77. if (frametype != X25_CLEAR_CONFIRMATION)
  78. printk(KERN_DEBUG "x25_receive_data(): unknown frame type %2x\n",frametype);
  79. return 0;
  80. }
  81. int x25_lapb_receive_frame(struct sk_buff *skb, struct net_device *dev,
  82. struct packet_type *ptype, struct net_device *orig_dev)
  83. {
  84. struct sk_buff *nskb;
  85. struct x25_neigh *nb;
  86. if (!net_eq(dev_net(dev), &init_net))
  87. goto drop;
  88. nskb = skb_copy(skb, GFP_ATOMIC);
  89. if (!nskb)
  90. goto drop;
  91. kfree_skb(skb);
  92. skb = nskb;
  93. /*
  94. * Packet received from unrecognised device, throw it away.
  95. */
  96. nb = x25_get_neigh(dev);
  97. if (!nb) {
  98. printk(KERN_DEBUG "X.25: unknown neighbour - %s\n", dev->name);
  99. goto drop;
  100. }
  101. switch (skb->data[0]) {
  102. case X25_IFACE_DATA:
  103. skb_pull(skb, 1);
  104. if (x25_receive_data(skb, nb)) {
  105. x25_neigh_put(nb);
  106. goto out;
  107. }
  108. break;
  109. case X25_IFACE_CONNECT:
  110. x25_link_established(nb);
  111. break;
  112. case X25_IFACE_DISCONNECT:
  113. x25_link_terminated(nb);
  114. break;
  115. }
  116. x25_neigh_put(nb);
  117. drop:
  118. kfree_skb(skb);
  119. out:
  120. return 0;
  121. }
  122. void x25_establish_link(struct x25_neigh *nb)
  123. {
  124. struct sk_buff *skb;
  125. unsigned char *ptr;
  126. switch (nb->dev->type) {
  127. case ARPHRD_X25:
  128. if ((skb = alloc_skb(1, GFP_ATOMIC)) == NULL) {
  129. printk(KERN_ERR "x25_dev: out of memory\n");
  130. return;
  131. }
  132. ptr = skb_put(skb, 1);
  133. *ptr = X25_IFACE_CONNECT;
  134. break;
  135. #if defined(CONFIG_LLC) || defined(CONFIG_LLC_MODULE)
  136. case ARPHRD_ETHER:
  137. return;
  138. #endif
  139. default:
  140. return;
  141. }
  142. skb->protocol = htons(ETH_P_X25);
  143. skb->dev = nb->dev;
  144. dev_queue_xmit(skb);
  145. }
  146. void x25_terminate_link(struct x25_neigh *nb)
  147. {
  148. struct sk_buff *skb;
  149. unsigned char *ptr;
  150. #if defined(CONFIG_LLC) || defined(CONFIG_LLC_MODULE)
  151. if (nb->dev->type == ARPHRD_ETHER)
  152. return;
  153. #endif
  154. if (nb->dev->type != ARPHRD_X25)
  155. return;
  156. skb = alloc_skb(1, GFP_ATOMIC);
  157. if (!skb) {
  158. printk(KERN_ERR "x25_dev: out of memory\n");
  159. return;
  160. }
  161. ptr = skb_put(skb, 1);
  162. *ptr = X25_IFACE_DISCONNECT;
  163. skb->protocol = htons(ETH_P_X25);
  164. skb->dev = nb->dev;
  165. dev_queue_xmit(skb);
  166. }
  167. void x25_send_frame(struct sk_buff *skb, struct x25_neigh *nb)
  168. {
  169. unsigned char *dptr;
  170. skb_reset_network_header(skb);
  171. switch (nb->dev->type) {
  172. case ARPHRD_X25:
  173. dptr = skb_push(skb, 1);
  174. *dptr = X25_IFACE_DATA;
  175. break;
  176. #if defined(CONFIG_LLC) || defined(CONFIG_LLC_MODULE)
  177. case ARPHRD_ETHER:
  178. kfree_skb(skb);
  179. return;
  180. #endif
  181. default:
  182. kfree_skb(skb);
  183. return;
  184. }
  185. skb->protocol = htons(ETH_P_X25);
  186. skb->dev = nb->dev;
  187. dev_queue_xmit(skb);
  188. }