hdlc.c 8.2 KB

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