hdlc_generic.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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/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.18";
  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, struct net_device *orig_dev)
  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. printk(KERN_INFO "%s: Carrier detected\n", dev->name);
  102. __hdlc_set_carrier_on(dev);
  103. } else {
  104. printk(KERN_INFO "%s: Carrier lost\n", dev->name);
  105. __hdlc_set_carrier_off(dev);
  106. }
  107. carrier_exit:
  108. spin_unlock_irqrestore(&hdlc->state_lock, flags);
  109. }
  110. /* Must be called by hardware driver when HDLC device is being opened */
  111. int hdlc_open(struct net_device *dev)
  112. {
  113. hdlc_device *hdlc = dev_to_hdlc(dev);
  114. #ifdef DEBUG_LINK
  115. printk(KERN_DEBUG "hdlc_open() carrier %i open %i\n",
  116. hdlc->carrier, hdlc->open);
  117. #endif
  118. if (hdlc->proto.id == -1)
  119. return -ENOSYS; /* no protocol attached */
  120. if (hdlc->proto.open) {
  121. int result = hdlc->proto.open(dev);
  122. if (result)
  123. return result;
  124. }
  125. spin_lock_irq(&hdlc->state_lock);
  126. if (hdlc->carrier) {
  127. printk(KERN_INFO "%s: Carrier detected\n", dev->name);
  128. __hdlc_set_carrier_on(dev);
  129. } else
  130. printk(KERN_INFO "%s: No carrier\n", dev->name);
  131. hdlc->open = 1;
  132. spin_unlock_irq(&hdlc->state_lock);
  133. return 0;
  134. }
  135. /* Must be called by hardware driver when HDLC device is being closed */
  136. void hdlc_close(struct net_device *dev)
  137. {
  138. hdlc_device *hdlc = dev_to_hdlc(dev);
  139. #ifdef DEBUG_LINK
  140. printk(KERN_DEBUG "hdlc_close() carrier %i open %i\n",
  141. hdlc->carrier, hdlc->open);
  142. #endif
  143. spin_lock_irq(&hdlc->state_lock);
  144. hdlc->open = 0;
  145. if (hdlc->carrier)
  146. __hdlc_set_carrier_off(dev);
  147. spin_unlock_irq(&hdlc->state_lock);
  148. if (hdlc->proto.close)
  149. hdlc->proto.close(dev);
  150. }
  151. #ifndef CONFIG_HDLC_RAW
  152. #define hdlc_raw_ioctl(dev, ifr) -ENOSYS
  153. #endif
  154. #ifndef CONFIG_HDLC_RAW_ETH
  155. #define hdlc_raw_eth_ioctl(dev, ifr) -ENOSYS
  156. #endif
  157. #ifndef CONFIG_HDLC_PPP
  158. #define hdlc_ppp_ioctl(dev, ifr) -ENOSYS
  159. #endif
  160. #ifndef CONFIG_HDLC_CISCO
  161. #define hdlc_cisco_ioctl(dev, ifr) -ENOSYS
  162. #endif
  163. #ifndef CONFIG_HDLC_FR
  164. #define hdlc_fr_ioctl(dev, ifr) -ENOSYS
  165. #endif
  166. #ifndef CONFIG_HDLC_X25
  167. #define hdlc_x25_ioctl(dev, ifr) -ENOSYS
  168. #endif
  169. int hdlc_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  170. {
  171. hdlc_device *hdlc = dev_to_hdlc(dev);
  172. unsigned int proto;
  173. if (cmd != SIOCWANDEV)
  174. return -EINVAL;
  175. switch(ifr->ifr_settings.type) {
  176. case IF_PROTO_HDLC:
  177. case IF_PROTO_HDLC_ETH:
  178. case IF_PROTO_PPP:
  179. case IF_PROTO_CISCO:
  180. case IF_PROTO_FR:
  181. case IF_PROTO_X25:
  182. proto = ifr->ifr_settings.type;
  183. break;
  184. default:
  185. proto = hdlc->proto.id;
  186. }
  187. switch(proto) {
  188. case IF_PROTO_HDLC: return hdlc_raw_ioctl(dev, ifr);
  189. case IF_PROTO_HDLC_ETH: return hdlc_raw_eth_ioctl(dev, ifr);
  190. case IF_PROTO_PPP: return hdlc_ppp_ioctl(dev, ifr);
  191. case IF_PROTO_CISCO: return hdlc_cisco_ioctl(dev, ifr);
  192. case IF_PROTO_FR: return hdlc_fr_ioctl(dev, ifr);
  193. case IF_PROTO_X25: return hdlc_x25_ioctl(dev, ifr);
  194. default: return -EINVAL;
  195. }
  196. }
  197. static void hdlc_setup(struct net_device *dev)
  198. {
  199. hdlc_device *hdlc = dev_to_hdlc(dev);
  200. dev->get_stats = hdlc_get_stats;
  201. dev->change_mtu = hdlc_change_mtu;
  202. dev->mtu = HDLC_MAX_MTU;
  203. dev->type = ARPHRD_RAWHDLC;
  204. dev->hard_header_len = 16;
  205. dev->flags = IFF_POINTOPOINT | IFF_NOARP;
  206. hdlc->proto.id = -1;
  207. hdlc->proto.detach = NULL;
  208. hdlc->carrier = 1;
  209. hdlc->open = 0;
  210. spin_lock_init(&hdlc->state_lock);
  211. }
  212. struct net_device *alloc_hdlcdev(void *priv)
  213. {
  214. struct net_device *dev;
  215. dev = alloc_netdev(sizeof(hdlc_device), "hdlc%d", hdlc_setup);
  216. if (dev)
  217. dev_to_hdlc(dev)->priv = priv;
  218. return dev;
  219. }
  220. int register_hdlc_device(struct net_device *dev)
  221. {
  222. int result = dev_alloc_name(dev, "hdlc%d");
  223. if (result < 0)
  224. return result;
  225. result = register_netdev(dev);
  226. if (result != 0)
  227. return -EIO;
  228. if (netif_carrier_ok(dev))
  229. netif_carrier_off(dev); /* no carrier until DCD goes up */
  230. return 0;
  231. }
  232. void unregister_hdlc_device(struct net_device *dev)
  233. {
  234. rtnl_lock();
  235. hdlc_proto_detach(dev_to_hdlc(dev));
  236. unregister_netdevice(dev);
  237. rtnl_unlock();
  238. }
  239. MODULE_AUTHOR("Krzysztof Halasa <khc@pm.waw.pl>");
  240. MODULE_DESCRIPTION("HDLC support module");
  241. MODULE_LICENSE("GPL v2");
  242. EXPORT_SYMBOL(hdlc_open);
  243. EXPORT_SYMBOL(hdlc_close);
  244. EXPORT_SYMBOL(hdlc_set_carrier);
  245. EXPORT_SYMBOL(hdlc_ioctl);
  246. EXPORT_SYMBOL(alloc_hdlcdev);
  247. EXPORT_SYMBOL(register_hdlc_device);
  248. EXPORT_SYMBOL(unregister_hdlc_device);
  249. static struct packet_type hdlc_packet_type = {
  250. .type = __constant_htons(ETH_P_HDLC),
  251. .func = hdlc_rcv,
  252. };
  253. static int __init hdlc_module_init(void)
  254. {
  255. printk(KERN_INFO "%s\n", version);
  256. dev_add_pack(&hdlc_packet_type);
  257. return 0;
  258. }
  259. static void __exit hdlc_module_exit(void)
  260. {
  261. dev_remove_pack(&hdlc_packet_type);
  262. }
  263. module_init(hdlc_module_init);
  264. module_exit(hdlc_module_exit);