hdlc.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. /*
  2. * Generic HDLC support routines for Linux
  3. *
  4. * Copyright (C) 1999 - 2008 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/notifier.h>
  37. #include <linux/hdlc.h>
  38. #include <net/net_namespace.h>
  39. static const char* version = "HDLC support module revision 1.22";
  40. #undef DEBUG_LINK
  41. static struct hdlc_proto *first_proto = NULL;
  42. static int hdlc_change_mtu(struct net_device *dev, int new_mtu)
  43. {
  44. if ((new_mtu < 68) || (new_mtu > HDLC_MAX_MTU))
  45. return -EINVAL;
  46. dev->mtu = new_mtu;
  47. return 0;
  48. }
  49. static struct net_device_stats *hdlc_get_stats(struct net_device *dev)
  50. {
  51. return hdlc_stats(dev);
  52. }
  53. static int hdlc_rcv(struct sk_buff *skb, struct net_device *dev,
  54. struct packet_type *p, struct net_device *orig_dev)
  55. {
  56. struct hdlc_device *hdlc = dev_to_hdlc(dev);
  57. if (dev_net(dev) != &init_net) {
  58. kfree_skb(skb);
  59. return 0;
  60. }
  61. BUG_ON(!hdlc->proto->netif_rx);
  62. return hdlc->proto->netif_rx(skb);
  63. }
  64. static inline void hdlc_proto_start(struct net_device *dev)
  65. {
  66. hdlc_device *hdlc = dev_to_hdlc(dev);
  67. if (hdlc->proto->start)
  68. hdlc->proto->start(dev);
  69. }
  70. static inline void hdlc_proto_stop(struct net_device *dev)
  71. {
  72. hdlc_device *hdlc = dev_to_hdlc(dev);
  73. if (hdlc->proto->stop)
  74. hdlc->proto->stop(dev);
  75. }
  76. static int hdlc_device_event(struct notifier_block *this, unsigned long event,
  77. void *ptr)
  78. {
  79. struct net_device *dev = ptr;
  80. hdlc_device *hdlc;
  81. unsigned long flags;
  82. int on;
  83. if (dev_net(dev) != &init_net)
  84. return NOTIFY_DONE;
  85. if (dev->get_stats != hdlc_get_stats)
  86. return NOTIFY_DONE; /* not an HDLC device */
  87. if (event != NETDEV_CHANGE)
  88. return NOTIFY_DONE; /* Only interrested in carrier changes */
  89. on = netif_carrier_ok(dev);
  90. #ifdef DEBUG_LINK
  91. printk(KERN_DEBUG "%s: hdlc_device_event NETDEV_CHANGE, carrier %i\n",
  92. dev->name, on);
  93. #endif
  94. hdlc = dev_to_hdlc(dev);
  95. spin_lock_irqsave(&hdlc->state_lock, flags);
  96. if (hdlc->carrier == on)
  97. goto carrier_exit; /* no change in DCD line level */
  98. hdlc->carrier = on;
  99. if (!hdlc->open)
  100. goto carrier_exit;
  101. if (hdlc->carrier) {
  102. printk(KERN_INFO "%s: Carrier detected\n", dev->name);
  103. hdlc_proto_start(dev);
  104. } else {
  105. printk(KERN_INFO "%s: Carrier lost\n", dev->name);
  106. hdlc_proto_stop(dev);
  107. }
  108. carrier_exit:
  109. spin_unlock_irqrestore(&hdlc->state_lock, flags);
  110. return NOTIFY_DONE;
  111. }
  112. /* Must be called by hardware driver when HDLC device is being opened */
  113. int hdlc_open(struct net_device *dev)
  114. {
  115. hdlc_device *hdlc = dev_to_hdlc(dev);
  116. #ifdef DEBUG_LINK
  117. printk(KERN_DEBUG "%s: hdlc_open() carrier %i open %i\n", dev->name,
  118. hdlc->carrier, hdlc->open);
  119. #endif
  120. if (hdlc->proto == NULL)
  121. return -ENOSYS; /* no protocol attached */
  122. if (hdlc->proto->open) {
  123. int result = hdlc->proto->open(dev);
  124. if (result)
  125. return result;
  126. }
  127. spin_lock_irq(&hdlc->state_lock);
  128. if (hdlc->carrier) {
  129. printk(KERN_INFO "%s: Carrier detected\n", dev->name);
  130. hdlc_proto_start(dev);
  131. } else
  132. printk(KERN_INFO "%s: No carrier\n", dev->name);
  133. hdlc->open = 1;
  134. spin_unlock_irq(&hdlc->state_lock);
  135. return 0;
  136. }
  137. /* Must be called by hardware driver when HDLC device is being closed */
  138. void hdlc_close(struct net_device *dev)
  139. {
  140. hdlc_device *hdlc = dev_to_hdlc(dev);
  141. #ifdef DEBUG_LINK
  142. printk(KERN_DEBUG "%s: hdlc_close() carrier %i open %i\n", dev->name,
  143. hdlc->carrier, hdlc->open);
  144. #endif
  145. spin_lock_irq(&hdlc->state_lock);
  146. hdlc->open = 0;
  147. if (hdlc->carrier)
  148. hdlc_proto_stop(dev);
  149. spin_unlock_irq(&hdlc->state_lock);
  150. if (hdlc->proto->close)
  151. hdlc->proto->close(dev);
  152. }
  153. int hdlc_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  154. {
  155. struct hdlc_proto *proto = first_proto;
  156. int result;
  157. if (cmd != SIOCWANDEV)
  158. return -EINVAL;
  159. if (dev_to_hdlc(dev)->proto) {
  160. result = dev_to_hdlc(dev)->proto->ioctl(dev, ifr);
  161. if (result != -EINVAL)
  162. return result;
  163. }
  164. /* Not handled by currently attached protocol (if any) */
  165. while (proto) {
  166. if ((result = proto->ioctl(dev, ifr)) != -EINVAL)
  167. return result;
  168. proto = proto->next;
  169. }
  170. return -EINVAL;
  171. }
  172. static const struct header_ops hdlc_null_ops;
  173. static void hdlc_setup_dev(struct net_device *dev)
  174. {
  175. /* Re-init all variables changed by HDLC protocol drivers,
  176. * including ether_setup() called from hdlc_raw_eth.c.
  177. */
  178. dev->get_stats = hdlc_get_stats;
  179. dev->flags = IFF_POINTOPOINT | IFF_NOARP;
  180. dev->mtu = HDLC_MAX_MTU;
  181. dev->type = ARPHRD_RAWHDLC;
  182. dev->hard_header_len = 16;
  183. dev->addr_len = 0;
  184. dev->header_ops = &hdlc_null_ops;
  185. dev->change_mtu = hdlc_change_mtu;
  186. }
  187. static void hdlc_setup(struct net_device *dev)
  188. {
  189. hdlc_device *hdlc = dev_to_hdlc(dev);
  190. hdlc_setup_dev(dev);
  191. hdlc->carrier = 1;
  192. hdlc->open = 0;
  193. spin_lock_init(&hdlc->state_lock);
  194. }
  195. struct net_device *alloc_hdlcdev(void *priv)
  196. {
  197. struct net_device *dev;
  198. dev = alloc_netdev(sizeof(struct hdlc_device), "hdlc%d", hdlc_setup);
  199. if (dev)
  200. dev_to_hdlc(dev)->priv = priv;
  201. return dev;
  202. }
  203. void unregister_hdlc_device(struct net_device *dev)
  204. {
  205. rtnl_lock();
  206. unregister_netdevice(dev);
  207. detach_hdlc_protocol(dev);
  208. rtnl_unlock();
  209. }
  210. int attach_hdlc_protocol(struct net_device *dev, struct hdlc_proto *proto,
  211. size_t size)
  212. {
  213. detach_hdlc_protocol(dev);
  214. if (!try_module_get(proto->module))
  215. return -ENOSYS;
  216. if (size)
  217. if ((dev_to_hdlc(dev)->state = kmalloc(size,
  218. GFP_KERNEL)) == NULL) {
  219. printk(KERN_WARNING "Memory squeeze on"
  220. " hdlc_proto_attach()\n");
  221. module_put(proto->module);
  222. return -ENOBUFS;
  223. }
  224. dev_to_hdlc(dev)->proto = proto;
  225. return 0;
  226. }
  227. void detach_hdlc_protocol(struct net_device *dev)
  228. {
  229. hdlc_device *hdlc = dev_to_hdlc(dev);
  230. if (hdlc->proto) {
  231. if (hdlc->proto->detach)
  232. hdlc->proto->detach(dev);
  233. module_put(hdlc->proto->module);
  234. hdlc->proto = NULL;
  235. }
  236. kfree(hdlc->state);
  237. hdlc->state = NULL;
  238. hdlc_setup_dev(dev);
  239. }
  240. void register_hdlc_protocol(struct hdlc_proto *proto)
  241. {
  242. proto->next = first_proto;
  243. first_proto = proto;
  244. }
  245. void unregister_hdlc_protocol(struct hdlc_proto *proto)
  246. {
  247. struct hdlc_proto **p = &first_proto;
  248. while (*p) {
  249. if (*p == proto) {
  250. *p = proto->next;
  251. return;
  252. }
  253. p = &((*p)->next);
  254. }
  255. }
  256. MODULE_AUTHOR("Krzysztof Halasa <khc@pm.waw.pl>");
  257. MODULE_DESCRIPTION("HDLC support module");
  258. MODULE_LICENSE("GPL v2");
  259. EXPORT_SYMBOL(hdlc_open);
  260. EXPORT_SYMBOL(hdlc_close);
  261. EXPORT_SYMBOL(hdlc_ioctl);
  262. EXPORT_SYMBOL(alloc_hdlcdev);
  263. EXPORT_SYMBOL(unregister_hdlc_device);
  264. EXPORT_SYMBOL(register_hdlc_protocol);
  265. EXPORT_SYMBOL(unregister_hdlc_protocol);
  266. EXPORT_SYMBOL(attach_hdlc_protocol);
  267. EXPORT_SYMBOL(detach_hdlc_protocol);
  268. static struct packet_type hdlc_packet_type = {
  269. .type = __constant_htons(ETH_P_HDLC),
  270. .func = hdlc_rcv,
  271. };
  272. static struct notifier_block hdlc_notifier = {
  273. .notifier_call = hdlc_device_event,
  274. };
  275. static int __init hdlc_module_init(void)
  276. {
  277. int result;
  278. printk(KERN_INFO "%s\n", version);
  279. if ((result = register_netdevice_notifier(&hdlc_notifier)) != 0)
  280. return result;
  281. dev_add_pack(&hdlc_packet_type);
  282. return 0;
  283. }
  284. static void __exit hdlc_module_exit(void)
  285. {
  286. dev_remove_pack(&hdlc_packet_type);
  287. unregister_netdevice_notifier(&hdlc_notifier);
  288. }
  289. module_init(hdlc_module_init);
  290. module_exit(hdlc_module_exit);