hdlc_x25.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. printk(KERN_ERR "%s: out of memory\n", dev->name);
  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. printk(KERN_ERR "%s: LAPB connect request "
  81. "failed, error code = %i\n",
  82. dev->name, result);
  83. }
  84. break;
  85. case X25_IFACE_DISCONNECT:
  86. if ((result = lapb_disconnect_request(dev)) != LAPB_OK) {
  87. if (result == LAPB_NOTCONNECTED)
  88. /* Send disconnect confirm. msg to level 3 */
  89. x25_disconnected(dev, 0);
  90. else
  91. printk(KERN_ERR "%s: LAPB disconnect request "
  92. "failed, error code = %i\n",
  93. dev->name, result);
  94. }
  95. break;
  96. default: /* to be defined */
  97. break;
  98. }
  99. dev_kfree_skb(skb);
  100. return NETDEV_TX_OK;
  101. }
  102. static int x25_open(struct net_device *dev)
  103. {
  104. struct lapb_register_struct cb;
  105. int result;
  106. cb.connect_confirmation = x25_connected;
  107. cb.connect_indication = x25_connected;
  108. cb.disconnect_confirmation = x25_disconnected;
  109. cb.disconnect_indication = x25_disconnected;
  110. cb.data_indication = x25_data_indication;
  111. cb.data_transmit = x25_data_transmit;
  112. result = lapb_register(dev, &cb);
  113. if (result != LAPB_OK)
  114. return result;
  115. return 0;
  116. }
  117. static void x25_close(struct net_device *dev)
  118. {
  119. lapb_unregister(dev);
  120. }
  121. static int x25_rx(struct sk_buff *skb)
  122. {
  123. struct net_device *dev = skb->dev;
  124. if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL) {
  125. dev->stats.rx_dropped++;
  126. return NET_RX_DROP;
  127. }
  128. if (lapb_data_received(dev, skb) == LAPB_OK)
  129. return NET_RX_SUCCESS;
  130. dev->stats.rx_errors++;
  131. dev_kfree_skb_any(skb);
  132. return NET_RX_DROP;
  133. }
  134. static struct hdlc_proto proto = {
  135. .open = x25_open,
  136. .close = x25_close,
  137. .ioctl = x25_ioctl,
  138. .netif_rx = x25_rx,
  139. .xmit = x25_xmit,
  140. .module = THIS_MODULE,
  141. };
  142. static int x25_ioctl(struct net_device *dev, struct ifreq *ifr)
  143. {
  144. hdlc_device *hdlc = dev_to_hdlc(dev);
  145. int result;
  146. switch (ifr->ifr_settings.type) {
  147. case IF_GET_PROTO:
  148. if (dev_to_hdlc(dev)->proto != &proto)
  149. return -EINVAL;
  150. ifr->ifr_settings.type = IF_PROTO_X25;
  151. return 0; /* return protocol only, no settable parameters */
  152. case IF_PROTO_X25:
  153. if (!capable(CAP_NET_ADMIN))
  154. return -EPERM;
  155. if (dev->flags & IFF_UP)
  156. return -EBUSY;
  157. result=hdlc->attach(dev, ENCODING_NRZ,PARITY_CRC16_PR1_CCITT);
  158. if (result)
  159. return result;
  160. if ((result = attach_hdlc_protocol(dev, &proto, 0)))
  161. return result;
  162. dev->type = ARPHRD_X25;
  163. netif_dormant_off(dev);
  164. return 0;
  165. }
  166. return -EINVAL;
  167. }
  168. static int __init mod_init(void)
  169. {
  170. register_hdlc_protocol(&proto);
  171. return 0;
  172. }
  173. static void __exit mod_exit(void)
  174. {
  175. unregister_hdlc_protocol(&proto);
  176. }
  177. module_init(mod_init);
  178. module_exit(mod_exit);
  179. MODULE_AUTHOR("Krzysztof Halasa <khc@pm.waw.pl>");
  180. MODULE_DESCRIPTION("X.25 protocol support for generic HDLC");
  181. MODULE_LICENSE("GPL v2");