hdlc.c 8.3 KB

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