x25_dev.c 4.2 KB

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