x25_dev.c 4.4 KB

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