hdlc.c 8.1 KB

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