x25_dev.c 4.3 KB

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