hdlc_x25.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /*
  2. * Generic HDLC support routines for Linux
  3. * X.25 support
  4. *
  5. * Copyright (C) 1999 - 2003 Krzysztof Halasa <khc@pm.waw.pl>
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of version 2 of the GNU General Public License
  9. * as published by the Free Software Foundation.
  10. */
  11. #include <linux/module.h>
  12. #include <linux/kernel.h>
  13. #include <linux/slab.h>
  14. #include <linux/poll.h>
  15. #include <linux/errno.h>
  16. #include <linux/if_arp.h>
  17. #include <linux/init.h>
  18. #include <linux/skbuff.h>
  19. #include <linux/pkt_sched.h>
  20. #include <linux/inetdevice.h>
  21. #include <linux/lapb.h>
  22. #include <linux/rtnetlink.h>
  23. #include <linux/hdlc.h>
  24. #include <net/x25device.h>
  25. /* These functions are callbacks called by LAPB layer */
  26. static void x25_connect_disconnect(struct net_device *dev, int reason, int code)
  27. {
  28. struct sk_buff *skb;
  29. unsigned char *ptr;
  30. if ((skb = dev_alloc_skb(1)) == NULL) {
  31. printk(KERN_ERR "%s: out of memory\n", dev->name);
  32. return;
  33. }
  34. ptr = skb_put(skb, 1);
  35. *ptr = code;
  36. skb->protocol = x25_type_trans(skb, dev);
  37. netif_rx(skb);
  38. }
  39. static void x25_connected(struct net_device *dev, int reason)
  40. {
  41. x25_connect_disconnect(dev, reason, 1);
  42. }
  43. static void x25_disconnected(struct net_device *dev, int reason)
  44. {
  45. x25_connect_disconnect(dev, reason, 2);
  46. }
  47. static int x25_data_indication(struct net_device *dev, struct sk_buff *skb)
  48. {
  49. unsigned char *ptr;
  50. skb_push(skb, 1);
  51. if (skb_cow(skb, 1))
  52. return NET_RX_DROP;
  53. ptr = skb->data;
  54. *ptr = 0;
  55. skb->protocol = x25_type_trans(skb, dev);
  56. return netif_rx(skb);
  57. }
  58. static void x25_data_transmit(struct net_device *dev, struct sk_buff *skb)
  59. {
  60. hdlc_device *hdlc = dev_to_hdlc(dev);
  61. hdlc->xmit(skb, dev); /* Ignore return value :-( */
  62. }
  63. static int x25_xmit(struct sk_buff *skb, struct net_device *dev)
  64. {
  65. int result;
  66. /* X.25 to LAPB */
  67. switch (skb->data[0]) {
  68. case 0: /* Data to be transmitted */
  69. skb_pull(skb, 1);
  70. if ((result = lapb_data_request(dev, skb)) != LAPB_OK)
  71. dev_kfree_skb(skb);
  72. return 0;
  73. case 1:
  74. if ((result = lapb_connect_request(dev))!= LAPB_OK) {
  75. if (result == LAPB_CONNECTED)
  76. /* Send connect confirm. msg to level 3 */
  77. x25_connected(dev, 0);
  78. else
  79. printk(KERN_ERR "%s: LAPB connect request "
  80. "failed, error code = %i\n",
  81. dev->name, result);
  82. }
  83. break;
  84. case 2:
  85. if ((result = lapb_disconnect_request(dev)) != LAPB_OK) {
  86. if (result == LAPB_NOTCONNECTED)
  87. /* Send disconnect confirm. msg to level 3 */
  88. x25_disconnected(dev, 0);
  89. else
  90. printk(KERN_ERR "%s: LAPB disconnect request "
  91. "failed, error code = %i\n",
  92. dev->name, result);
  93. }
  94. break;
  95. default: /* to be defined */
  96. break;
  97. }
  98. dev_kfree_skb(skb);
  99. return 0;
  100. }
  101. static int x25_open(struct net_device *dev)
  102. {
  103. struct lapb_register_struct cb;
  104. int result;
  105. cb.connect_confirmation = x25_connected;
  106. cb.connect_indication = x25_connected;
  107. cb.disconnect_confirmation = x25_disconnected;
  108. cb.disconnect_indication = x25_disconnected;
  109. cb.data_indication = x25_data_indication;
  110. cb.data_transmit = x25_data_transmit;
  111. result = lapb_register(dev, &cb);
  112. if (result != LAPB_OK)
  113. return result;
  114. return 0;
  115. }
  116. static void x25_close(struct net_device *dev)
  117. {
  118. lapb_unregister(dev);
  119. }
  120. static int x25_rx(struct sk_buff *skb)
  121. {
  122. hdlc_device *hdlc = dev_to_hdlc(skb->dev);
  123. if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL) {
  124. hdlc->stats.rx_dropped++;
  125. return NET_RX_DROP;
  126. }
  127. if (lapb_data_received(skb->dev, skb) == LAPB_OK)
  128. return NET_RX_SUCCESS;
  129. hdlc->stats.rx_errors++;
  130. dev_kfree_skb_any(skb);
  131. return NET_RX_DROP;
  132. }
  133. int hdlc_x25_ioctl(struct net_device *dev, struct ifreq *ifr)
  134. {
  135. hdlc_device *hdlc = dev_to_hdlc(dev);
  136. int result;
  137. switch (ifr->ifr_settings.type) {
  138. case IF_GET_PROTO:
  139. ifr->ifr_settings.type = IF_PROTO_X25;
  140. return 0; /* return protocol only, no settable parameters */
  141. case IF_PROTO_X25:
  142. if(!capable(CAP_NET_ADMIN))
  143. return -EPERM;
  144. if(dev->flags & IFF_UP)
  145. return -EBUSY;
  146. result=hdlc->attach(dev, ENCODING_NRZ,PARITY_CRC16_PR1_CCITT);
  147. if (result)
  148. return result;
  149. hdlc_proto_detach(hdlc);
  150. memset(&hdlc->proto, 0, sizeof(hdlc->proto));
  151. hdlc->proto.open = x25_open;
  152. hdlc->proto.close = x25_close;
  153. hdlc->proto.netif_rx = x25_rx;
  154. hdlc->proto.type_trans = NULL;
  155. hdlc->proto.id = IF_PROTO_X25;
  156. dev->hard_start_xmit = x25_xmit;
  157. dev->hard_header = NULL;
  158. dev->type = ARPHRD_X25;
  159. dev->addr_len = 0;
  160. return 0;
  161. }
  162. return -EINVAL;
  163. }