x25_dev.c 4.1 KB

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