hdlc_generic.c 7.0 KB

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