hdlc_generic.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. /*
  2. * Generic HDLC support routines for Linux
  3. *
  4. * Copyright (C) 1999 - 2005 Krzysztof Halasa <khc@pm.waw.pl>
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of version 2 of the GNU General Public License
  8. * as published by the Free Software Foundation.
  9. *
  10. * Currently supported:
  11. * * raw IP-in-HDLC
  12. * * Cisco HDLC
  13. * * Frame Relay with ANSI or CCITT LMI (both user and network side)
  14. * * PPP
  15. * * X.25
  16. *
  17. * Use sethdlc utility to set line parameters, protocol and PVCs
  18. *
  19. * How does it work:
  20. * - proto.open(), close(), start(), stop() calls are serialized.
  21. * The order is: open, [ start, stop ... ] close ...
  22. * - proto.start() and stop() are called with spin_lock_irq held.
  23. */
  24. #include <linux/module.h>
  25. #include <linux/kernel.h>
  26. #include <linux/slab.h>
  27. #include <linux/poll.h>
  28. #include <linux/errno.h>
  29. #include <linux/if_arp.h>
  30. #include <linux/init.h>
  31. #include <linux/skbuff.h>
  32. #include <linux/pkt_sched.h>
  33. #include <linux/inetdevice.h>
  34. #include <linux/lapb.h>
  35. #include <linux/rtnetlink.h>
  36. #include <linux/hdlc.h>
  37. static const char* version = "HDLC support module revision 1.18";
  38. #undef DEBUG_LINK
  39. static int hdlc_change_mtu(struct net_device *dev, int new_mtu)
  40. {
  41. if ((new_mtu < 68) || (new_mtu > HDLC_MAX_MTU))
  42. return -EINVAL;
  43. dev->mtu = new_mtu;
  44. return 0;
  45. }
  46. static struct net_device_stats *hdlc_get_stats(struct net_device *dev)
  47. {
  48. return hdlc_stats(dev);
  49. }
  50. static int hdlc_rcv(struct sk_buff *skb, struct net_device *dev,
  51. struct packet_type *p, struct net_device *orig_dev)
  52. {
  53. hdlc_device *hdlc = dev_to_hdlc(dev);
  54. if (hdlc->proto.netif_rx)
  55. return hdlc->proto.netif_rx(skb);
  56. hdlc->stats.rx_dropped++; /* Shouldn't happen */
  57. dev_kfree_skb(skb);
  58. return NET_RX_DROP;
  59. }
  60. static void __hdlc_set_carrier_on(struct net_device *dev)
  61. {
  62. hdlc_device *hdlc = dev_to_hdlc(dev);
  63. if (hdlc->proto.start)
  64. return hdlc->proto.start(dev);
  65. #if 0
  66. #ifdef DEBUG_LINK
  67. if (netif_carrier_ok(dev))
  68. printk(KERN_ERR "hdlc_set_carrier_on(): already on\n");
  69. #endif
  70. netif_carrier_on(dev);
  71. #endif
  72. }
  73. static void __hdlc_set_carrier_off(struct net_device *dev)
  74. {
  75. hdlc_device *hdlc = dev_to_hdlc(dev);
  76. if (hdlc->proto.stop)
  77. return hdlc->proto.stop(dev);
  78. #if 0
  79. #ifdef DEBUG_LINK
  80. if (!netif_carrier_ok(dev))
  81. printk(KERN_ERR "hdlc_set_carrier_off(): already off\n");
  82. #endif
  83. netif_carrier_off(dev);
  84. #endif
  85. }
  86. void hdlc_set_carrier(int on, struct net_device *dev)
  87. {
  88. hdlc_device *hdlc = dev_to_hdlc(dev);
  89. unsigned long flags;
  90. on = on ? 1 : 0;
  91. #ifdef DEBUG_LINK
  92. printk(KERN_DEBUG "hdlc_set_carrier %i\n", on);
  93. #endif
  94. spin_lock_irqsave(&hdlc->state_lock, flags);
  95. if (hdlc->carrier == on)
  96. goto carrier_exit; /* no change in DCD line level */
  97. #ifdef DEBUG_LINK
  98. printk(KERN_INFO "%s: carrier %s\n", dev->name, on ? "ON" : "off");
  99. #endif
  100. hdlc->carrier = on;
  101. if (!hdlc->open)
  102. goto carrier_exit;
  103. if (hdlc->carrier) {
  104. printk(KERN_INFO "%s: Carrier detected\n", dev->name);
  105. __hdlc_set_carrier_on(dev);
  106. } else {
  107. printk(KERN_INFO "%s: Carrier lost\n", dev->name);
  108. __hdlc_set_carrier_off(dev);
  109. }
  110. carrier_exit:
  111. spin_unlock_irqrestore(&hdlc->state_lock, flags);
  112. }
  113. /* Must be called by hardware driver when HDLC device is being opened */
  114. int hdlc_open(struct net_device *dev)
  115. {
  116. hdlc_device *hdlc = dev_to_hdlc(dev);
  117. #ifdef DEBUG_LINK
  118. printk(KERN_DEBUG "hdlc_open() carrier %i open %i\n",
  119. hdlc->carrier, hdlc->open);
  120. #endif
  121. if (hdlc->proto.id == -1)
  122. return -ENOSYS; /* no protocol attached */
  123. if (hdlc->proto.open) {
  124. int result = hdlc->proto.open(dev);
  125. if (result)
  126. return result;
  127. }
  128. spin_lock_irq(&hdlc->state_lock);
  129. if (hdlc->carrier) {
  130. printk(KERN_INFO "%s: Carrier detected\n", dev->name);
  131. __hdlc_set_carrier_on(dev);
  132. } else
  133. printk(KERN_INFO "%s: No carrier\n", dev->name);
  134. hdlc->open = 1;
  135. spin_unlock_irq(&hdlc->state_lock);
  136. return 0;
  137. }
  138. /* Must be called by hardware driver when HDLC device is being closed */
  139. void hdlc_close(struct net_device *dev)
  140. {
  141. hdlc_device *hdlc = dev_to_hdlc(dev);
  142. #ifdef DEBUG_LINK
  143. printk(KERN_DEBUG "hdlc_close() carrier %i open %i\n",
  144. hdlc->carrier, hdlc->open);
  145. #endif
  146. spin_lock_irq(&hdlc->state_lock);
  147. hdlc->open = 0;
  148. if (hdlc->carrier)
  149. __hdlc_set_carrier_off(dev);
  150. spin_unlock_irq(&hdlc->state_lock);
  151. if (hdlc->proto.close)
  152. hdlc->proto.close(dev);
  153. }
  154. #ifndef CONFIG_HDLC_RAW
  155. #define hdlc_raw_ioctl(dev, ifr) -ENOSYS
  156. #endif
  157. #ifndef CONFIG_HDLC_RAW_ETH
  158. #define hdlc_raw_eth_ioctl(dev, ifr) -ENOSYS
  159. #endif
  160. #ifndef CONFIG_HDLC_PPP
  161. #define hdlc_ppp_ioctl(dev, ifr) -ENOSYS
  162. #endif
  163. #ifndef CONFIG_HDLC_CISCO
  164. #define hdlc_cisco_ioctl(dev, ifr) -ENOSYS
  165. #endif
  166. #ifndef CONFIG_HDLC_FR
  167. #define hdlc_fr_ioctl(dev, ifr) -ENOSYS
  168. #endif
  169. #ifndef CONFIG_HDLC_X25
  170. #define hdlc_x25_ioctl(dev, ifr) -ENOSYS
  171. #endif
  172. int hdlc_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  173. {
  174. hdlc_device *hdlc = dev_to_hdlc(dev);
  175. unsigned int proto;
  176. if (cmd != SIOCWANDEV)
  177. return -EINVAL;
  178. switch(ifr->ifr_settings.type) {
  179. case IF_PROTO_HDLC:
  180. case IF_PROTO_HDLC_ETH:
  181. case IF_PROTO_PPP:
  182. case IF_PROTO_CISCO:
  183. case IF_PROTO_FR:
  184. case IF_PROTO_X25:
  185. proto = ifr->ifr_settings.type;
  186. break;
  187. default:
  188. proto = hdlc->proto.id;
  189. }
  190. switch(proto) {
  191. case IF_PROTO_HDLC: return hdlc_raw_ioctl(dev, ifr);
  192. case IF_PROTO_HDLC_ETH: return hdlc_raw_eth_ioctl(dev, ifr);
  193. case IF_PROTO_PPP: return hdlc_ppp_ioctl(dev, ifr);
  194. case IF_PROTO_CISCO: return hdlc_cisco_ioctl(dev, ifr);
  195. case IF_PROTO_FR: return hdlc_fr_ioctl(dev, ifr);
  196. case IF_PROTO_X25: return hdlc_x25_ioctl(dev, ifr);
  197. default: return -EINVAL;
  198. }
  199. }
  200. void hdlc_setup(struct net_device *dev)
  201. {
  202. hdlc_device *hdlc = dev_to_hdlc(dev);
  203. dev->get_stats = hdlc_get_stats;
  204. dev->change_mtu = hdlc_change_mtu;
  205. dev->mtu = HDLC_MAX_MTU;
  206. dev->type = ARPHRD_RAWHDLC;
  207. dev->hard_header_len = 16;
  208. dev->flags = IFF_POINTOPOINT | IFF_NOARP;
  209. hdlc->proto.id = -1;
  210. hdlc->proto.detach = NULL;
  211. hdlc->carrier = 1;
  212. hdlc->open = 0;
  213. spin_lock_init(&hdlc->state_lock);
  214. }
  215. struct net_device *alloc_hdlcdev(void *priv)
  216. {
  217. struct net_device *dev;
  218. dev = alloc_netdev(sizeof(hdlc_device), "hdlc%d", hdlc_setup);
  219. if (dev)
  220. dev_to_hdlc(dev)->priv = priv;
  221. return dev;
  222. }
  223. void unregister_hdlc_device(struct net_device *dev)
  224. {
  225. rtnl_lock();
  226. hdlc_proto_detach(dev_to_hdlc(dev));
  227. unregister_netdevice(dev);
  228. rtnl_unlock();
  229. }
  230. MODULE_AUTHOR("Krzysztof Halasa <khc@pm.waw.pl>");
  231. MODULE_DESCRIPTION("HDLC support module");
  232. MODULE_LICENSE("GPL v2");
  233. EXPORT_SYMBOL(hdlc_open);
  234. EXPORT_SYMBOL(hdlc_close);
  235. EXPORT_SYMBOL(hdlc_set_carrier);
  236. EXPORT_SYMBOL(hdlc_ioctl);
  237. EXPORT_SYMBOL(hdlc_setup);
  238. EXPORT_SYMBOL(alloc_hdlcdev);
  239. EXPORT_SYMBOL(unregister_hdlc_device);
  240. static struct packet_type hdlc_packet_type = {
  241. .type = __constant_htons(ETH_P_HDLC),
  242. .func = hdlc_rcv,
  243. };
  244. static int __init hdlc_module_init(void)
  245. {
  246. printk(KERN_INFO "%s\n", version);
  247. dev_add_pack(&hdlc_packet_type);
  248. return 0;
  249. }
  250. static void __exit hdlc_module_exit(void)
  251. {
  252. dev_remove_pack(&hdlc_packet_type);
  253. }
  254. module_init(hdlc_module_init);
  255. module_exit(hdlc_module_exit);