x25_dev.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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/config.h>
  20. #include <linux/kernel.h>
  21. #include <linux/netdevice.h>
  22. #include <linux/skbuff.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->h.raw = skb->data;
  47. bh_lock_sock(sk);
  48. if (!sock_owned_by_user(sk)) {
  49. queued = x25_process_rx_frame(sk, skb);
  50. } else {
  51. sk_add_backlog(sk, skb);
  52. }
  53. bh_unlock_sock(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. * Let caller throw it away.
  64. */
  65. /*
  66. x25_transmit_clear_request(nb, lci, 0x0D);
  67. */
  68. if (frametype != X25_CLEAR_CONFIRMATION)
  69. printk(KERN_DEBUG "x25_receive_data(): unknown frame type %2x\n",frametype);
  70. return 0;
  71. }
  72. int x25_lapb_receive_frame(struct sk_buff *skb, struct net_device *dev,
  73. struct packet_type *ptype, struct net_device *orig_dev)
  74. {
  75. struct sk_buff *nskb;
  76. struct x25_neigh *nb;
  77. nskb = skb_copy(skb, GFP_ATOMIC);
  78. if (!nskb)
  79. goto drop;
  80. kfree_skb(skb);
  81. skb = nskb;
  82. /*
  83. * Packet received from unrecognised device, throw it away.
  84. */
  85. nb = x25_get_neigh(dev);
  86. if (!nb) {
  87. printk(KERN_DEBUG "X.25: unknown neighbour - %s\n", dev->name);
  88. goto drop;
  89. }
  90. switch (skb->data[0]) {
  91. case 0x00:
  92. skb_pull(skb, 1);
  93. if (x25_receive_data(skb, nb)) {
  94. x25_neigh_put(nb);
  95. goto out;
  96. }
  97. break;
  98. case 0x01:
  99. x25_link_established(nb);
  100. break;
  101. case 0x02:
  102. x25_link_terminated(nb);
  103. break;
  104. }
  105. x25_neigh_put(nb);
  106. drop:
  107. kfree_skb(skb);
  108. out:
  109. return 0;
  110. }
  111. void x25_establish_link(struct x25_neigh *nb)
  112. {
  113. struct sk_buff *skb;
  114. unsigned char *ptr;
  115. switch (nb->dev->type) {
  116. case ARPHRD_X25:
  117. if ((skb = alloc_skb(1, GFP_ATOMIC)) == NULL) {
  118. printk(KERN_ERR "x25_dev: out of memory\n");
  119. return;
  120. }
  121. ptr = skb_put(skb, 1);
  122. *ptr = 0x01;
  123. break;
  124. #if defined(CONFIG_LLC) || defined(CONFIG_LLC_MODULE)
  125. case ARPHRD_ETHER:
  126. return;
  127. #endif
  128. default:
  129. return;
  130. }
  131. skb->protocol = htons(ETH_P_X25);
  132. skb->dev = nb->dev;
  133. dev_queue_xmit(skb);
  134. }
  135. void x25_terminate_link(struct x25_neigh *nb)
  136. {
  137. struct sk_buff *skb;
  138. unsigned char *ptr;
  139. #if defined(CONFIG_LLC) || defined(CONFIG_LLC_MODULE)
  140. if (nb->dev->type == ARPHRD_ETHER)
  141. return;
  142. #endif
  143. if (nb->dev->type != ARPHRD_X25)
  144. return;
  145. skb = alloc_skb(1, GFP_ATOMIC);
  146. if (!skb) {
  147. printk(KERN_ERR "x25_dev: out of memory\n");
  148. return;
  149. }
  150. ptr = skb_put(skb, 1);
  151. *ptr = 0x02;
  152. skb->protocol = htons(ETH_P_X25);
  153. skb->dev = nb->dev;
  154. dev_queue_xmit(skb);
  155. }
  156. void x25_send_frame(struct sk_buff *skb, struct x25_neigh *nb)
  157. {
  158. unsigned char *dptr;
  159. skb->nh.raw = skb->data;
  160. switch (nb->dev->type) {
  161. case ARPHRD_X25:
  162. dptr = skb_push(skb, 1);
  163. *dptr = 0x00;
  164. break;
  165. #if defined(CONFIG_LLC) || defined(CONFIG_LLC_MODULE)
  166. case ARPHRD_ETHER:
  167. kfree_skb(skb);
  168. return;
  169. #endif
  170. default:
  171. kfree_skb(skb);
  172. return;
  173. }
  174. skb->protocol = htons(ETH_P_X25);
  175. skb->dev = nb->dev;
  176. dev_queue_xmit(skb);
  177. }