hdlc_x25.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. int result;
  103. static const struct lapb_register_struct cb = {
  104. .connect_confirmation = x25_connected,
  105. .connect_indication = x25_connected,
  106. .disconnect_confirmation = x25_disconnected,
  107. .disconnect_indication = x25_disconnected,
  108. .data_indication = x25_data_indication,
  109. .data_transmit = x25_data_transmit,
  110. };
  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. struct net_device *dev = skb->dev;
  123. if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL) {
  124. dev->stats.rx_dropped++;
  125. return NET_RX_DROP;
  126. }
  127. if (lapb_data_received(dev, skb) == LAPB_OK)
  128. return NET_RX_SUCCESS;
  129. dev->stats.rx_errors++;
  130. dev_kfree_skb_any(skb);
  131. return NET_RX_DROP;
  132. }
  133. static struct hdlc_proto proto = {
  134. .open = x25_open,
  135. .close = x25_close,
  136. .ioctl = x25_ioctl,
  137. .netif_rx = x25_rx,
  138. .xmit = x25_xmit,
  139. .module = THIS_MODULE,
  140. };
  141. static int x25_ioctl(struct net_device *dev, struct ifreq *ifr)
  142. {
  143. hdlc_device *hdlc = dev_to_hdlc(dev);
  144. int result;
  145. switch (ifr->ifr_settings.type) {
  146. case IF_GET_PROTO:
  147. if (dev_to_hdlc(dev)->proto != &proto)
  148. return -EINVAL;
  149. ifr->ifr_settings.type = IF_PROTO_X25;
  150. return 0; /* return protocol only, no settable parameters */
  151. case IF_PROTO_X25:
  152. if (!capable(CAP_NET_ADMIN))
  153. return -EPERM;
  154. if (dev->flags & IFF_UP)
  155. return -EBUSY;
  156. result=hdlc->attach(dev, ENCODING_NRZ,PARITY_CRC16_PR1_CCITT);
  157. if (result)
  158. return result;
  159. if ((result = attach_hdlc_protocol(dev, &proto, 0)))
  160. return result;
  161. dev->type = ARPHRD_X25;
  162. netif_dormant_off(dev);
  163. return 0;
  164. }
  165. return -EINVAL;
  166. }
  167. static int __init mod_init(void)
  168. {
  169. register_hdlc_protocol(&proto);
  170. return 0;
  171. }
  172. static void __exit mod_exit(void)
  173. {
  174. unregister_hdlc_protocol(&proto);
  175. }
  176. module_init(mod_init);
  177. module_exit(mod_exit);
  178. MODULE_AUTHOR("Krzysztof Halasa <khc@pm.waw.pl>");
  179. MODULE_DESCRIPTION("X.25 protocol support for generic HDLC");
  180. MODULE_LICENSE("GPL v2");