hdlc_x25.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /*
  2. * Generic HDLC support routines for Linux
  3. * X.25 support
  4. *
  5. * Copyright (C) 1999 - 2006 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/errno.h>
  12. #include <linux/gfp.h>
  13. #include <linux/hdlc.h>
  14. #include <linux/if_arp.h>
  15. #include <linux/inetdevice.h>
  16. #include <linux/init.h>
  17. #include <linux/kernel.h>
  18. #include <linux/lapb.h>
  19. #include <linux/module.h>
  20. #include <linux/pkt_sched.h>
  21. #include <linux/poll.h>
  22. #include <linux/rtnetlink.h>
  23. #include <linux/skbuff.h>
  24. #include <net/x25device.h>
  25. static int x25_ioctl(struct net_device *dev, struct ifreq *ifr);
  26. /* These functions are callbacks called by LAPB layer */
  27. static void x25_connect_disconnect(struct net_device *dev, int reason, int code)
  28. {
  29. struct sk_buff *skb;
  30. unsigned char *ptr;
  31. if ((skb = dev_alloc_skb(1)) == NULL) {
  32. netdev_err(dev, "out of memory\n");
  33. return;
  34. }
  35. ptr = skb_put(skb, 1);
  36. *ptr = code;
  37. skb->protocol = x25_type_trans(skb, dev);
  38. netif_rx(skb);
  39. }
  40. static void x25_connected(struct net_device *dev, int reason)
  41. {
  42. x25_connect_disconnect(dev, reason, X25_IFACE_CONNECT);
  43. }
  44. static void x25_disconnected(struct net_device *dev, int reason)
  45. {
  46. x25_connect_disconnect(dev, reason, X25_IFACE_DISCONNECT);
  47. }
  48. static int x25_data_indication(struct net_device *dev, struct sk_buff *skb)
  49. {
  50. unsigned char *ptr;
  51. skb_push(skb, 1);
  52. if (skb_cow(skb, 1))
  53. return NET_RX_DROP;
  54. ptr = skb->data;
  55. *ptr = X25_IFACE_DATA;
  56. skb->protocol = x25_type_trans(skb, dev);
  57. return netif_rx(skb);
  58. }
  59. static void x25_data_transmit(struct net_device *dev, struct sk_buff *skb)
  60. {
  61. hdlc_device *hdlc = dev_to_hdlc(dev);
  62. hdlc->xmit(skb, dev); /* Ignore return value :-( */
  63. }
  64. static netdev_tx_t x25_xmit(struct sk_buff *skb, struct net_device *dev)
  65. {
  66. int result;
  67. /* X.25 to LAPB */
  68. switch (skb->data[0]) {
  69. case X25_IFACE_DATA: /* Data to be transmitted */
  70. skb_pull(skb, 1);
  71. if ((result = lapb_data_request(dev, skb)) != LAPB_OK)
  72. dev_kfree_skb(skb);
  73. return NETDEV_TX_OK;
  74. case X25_IFACE_CONNECT:
  75. if ((result = lapb_connect_request(dev))!= LAPB_OK) {
  76. if (result == LAPB_CONNECTED)
  77. /* Send connect confirm. msg to level 3 */
  78. x25_connected(dev, 0);
  79. else
  80. netdev_err(dev, "LAPB connect request failed, error code = %i\n",
  81. result);
  82. }
  83. break;
  84. case X25_IFACE_DISCONNECT:
  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. netdev_err(dev, "LAPB disconnect request failed, error code = %i\n",
  91. result);
  92. }
  93. break;
  94. default: /* to be defined */
  95. break;
  96. }
  97. dev_kfree_skb(skb);
  98. return NETDEV_TX_OK;
  99. }
  100. static int x25_open(struct net_device *dev)
  101. {
  102. struct lapb_register_struct cb;
  103. int result;
  104. cb.connect_confirmation = x25_connected;
  105. cb.connect_indication = x25_connected;
  106. cb.disconnect_confirmation = x25_disconnected;
  107. cb.disconnect_indication = x25_disconnected;
  108. cb.data_indication = x25_data_indication;
  109. cb.data_transmit = x25_data_transmit;
  110. result = lapb_register(dev, &cb);
  111. if (result != LAPB_OK)
  112. return result;
  113. return 0;
  114. }
  115. static void x25_close(struct net_device *dev)
  116. {
  117. lapb_unregister(dev);
  118. }
  119. static int x25_rx(struct sk_buff *skb)
  120. {
  121. struct net_device *dev = skb->dev;
  122. if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL) {
  123. dev->stats.rx_dropped++;
  124. return NET_RX_DROP;
  125. }
  126. if (lapb_data_received(dev, skb) == LAPB_OK)
  127. return NET_RX_SUCCESS;
  128. dev->stats.rx_errors++;
  129. dev_kfree_skb_any(skb);
  130. return NET_RX_DROP;
  131. }
  132. static struct hdlc_proto proto = {
  133. .open = x25_open,
  134. .close = x25_close,
  135. .ioctl = x25_ioctl,
  136. .netif_rx = x25_rx,
  137. .xmit = x25_xmit,
  138. .module = THIS_MODULE,
  139. };
  140. static int x25_ioctl(struct net_device *dev, struct ifreq *ifr)
  141. {
  142. hdlc_device *hdlc = dev_to_hdlc(dev);
  143. int result;
  144. switch (ifr->ifr_settings.type) {
  145. case IF_GET_PROTO:
  146. if (dev_to_hdlc(dev)->proto != &proto)
  147. return -EINVAL;
  148. ifr->ifr_settings.type = IF_PROTO_X25;
  149. return 0; /* return protocol only, no settable parameters */
  150. case IF_PROTO_X25:
  151. if (!capable(CAP_NET_ADMIN))
  152. return -EPERM;
  153. if (dev->flags & IFF_UP)
  154. return -EBUSY;
  155. result=hdlc->attach(dev, ENCODING_NRZ,PARITY_CRC16_PR1_CCITT);
  156. if (result)
  157. return result;
  158. if ((result = attach_hdlc_protocol(dev, &proto, 0)))
  159. return result;
  160. dev->type = ARPHRD_X25;
  161. netif_dormant_off(dev);
  162. return 0;
  163. }
  164. return -EINVAL;
  165. }
  166. static int __init mod_init(void)
  167. {
  168. register_hdlc_protocol(&proto);
  169. return 0;
  170. }
  171. static void __exit mod_exit(void)
  172. {
  173. unregister_hdlc_protocol(&proto);
  174. }
  175. module_init(mod_init);
  176. module_exit(mod_exit);
  177. MODULE_AUTHOR("Krzysztof Halasa <khc@pm.waw.pl>");
  178. MODULE_DESCRIPTION("X.25 protocol support for generic HDLC");
  179. MODULE_LICENSE("GPL v2");