x25_dev.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. if (!net_eq(dev_net(dev), &init_net))
  85. goto drop;
  86. nskb = skb_copy(skb, GFP_ATOMIC);
  87. if (!nskb)
  88. goto drop;
  89. kfree_skb(skb);
  90. skb = nskb;
  91. /*
  92. * Packet received from unrecognised device, throw it away.
  93. */
  94. nb = x25_get_neigh(dev);
  95. if (!nb) {
  96. printk(KERN_DEBUG "X.25: unknown neighbour - %s\n", dev->name);
  97. goto drop;
  98. }
  99. switch (skb->data[0]) {
  100. case 0x00:
  101. skb_pull(skb, 1);
  102. if (x25_receive_data(skb, nb)) {
  103. x25_neigh_put(nb);
  104. goto out;
  105. }
  106. break;
  107. case 0x01:
  108. x25_link_established(nb);
  109. break;
  110. case 0x02:
  111. x25_link_terminated(nb);
  112. break;
  113. }
  114. x25_neigh_put(nb);
  115. drop:
  116. kfree_skb(skb);
  117. out:
  118. return 0;
  119. }
  120. void x25_establish_link(struct x25_neigh *nb)
  121. {
  122. struct sk_buff *skb;
  123. unsigned char *ptr;
  124. switch (nb->dev->type) {
  125. case ARPHRD_X25:
  126. if ((skb = alloc_skb(1, GFP_ATOMIC)) == NULL) {
  127. printk(KERN_ERR "x25_dev: out of memory\n");
  128. return;
  129. }
  130. ptr = skb_put(skb, 1);
  131. *ptr = 0x01;
  132. break;
  133. #if defined(CONFIG_LLC) || defined(CONFIG_LLC_MODULE)
  134. case ARPHRD_ETHER:
  135. return;
  136. #endif
  137. default:
  138. return;
  139. }
  140. skb->protocol = htons(ETH_P_X25);
  141. skb->dev = nb->dev;
  142. dev_queue_xmit(skb);
  143. }
  144. void x25_terminate_link(struct x25_neigh *nb)
  145. {
  146. struct sk_buff *skb;
  147. unsigned char *ptr;
  148. #if defined(CONFIG_LLC) || defined(CONFIG_LLC_MODULE)
  149. if (nb->dev->type == ARPHRD_ETHER)
  150. return;
  151. #endif
  152. if (nb->dev->type != ARPHRD_X25)
  153. return;
  154. skb = alloc_skb(1, GFP_ATOMIC);
  155. if (!skb) {
  156. printk(KERN_ERR "x25_dev: out of memory\n");
  157. return;
  158. }
  159. ptr = skb_put(skb, 1);
  160. *ptr = 0x02;
  161. skb->protocol = htons(ETH_P_X25);
  162. skb->dev = nb->dev;
  163. dev_queue_xmit(skb);
  164. }
  165. void x25_send_frame(struct sk_buff *skb, struct x25_neigh *nb)
  166. {
  167. unsigned char *dptr;
  168. skb_reset_network_header(skb);
  169. switch (nb->dev->type) {
  170. case ARPHRD_X25:
  171. dptr = skb_push(skb, 1);
  172. *dptr = 0x00;
  173. break;
  174. #if defined(CONFIG_LLC) || defined(CONFIG_LLC_MODULE)
  175. case ARPHRD_ETHER:
  176. kfree_skb(skb);
  177. return;
  178. #endif
  179. default:
  180. kfree_skb(skb);
  181. return;
  182. }
  183. skb->protocol = htons(ETH_P_X25);
  184. skb->dev = nb->dev;
  185. dev_queue_xmit(skb);
  186. }