hdlc.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Generic HDLC support routines for Linux
  4. *
  5. * Copyright (C) 1999 - 2008 Krzysztof Halasa <[email protected]>
  6. *
  7. * Currently supported:
  8. * * raw IP-in-HDLC
  9. * * Cisco HDLC
  10. * * Frame Relay with ANSI or CCITT LMI (both user and network side)
  11. * * PPP
  12. * * X.25
  13. *
  14. * Use sethdlc utility to set line parameters, protocol and PVCs
  15. *
  16. * How does it work:
  17. * - proto->open(), close(), start(), stop() calls are serialized.
  18. * The order is: open, [ start, stop ... ] close ...
  19. * - proto->start() and stop() are called with spin_lock_irq held.
  20. */
  21. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  22. #include <linux/errno.h>
  23. #include <linux/hdlc.h>
  24. #include <linux/if_arp.h>
  25. #include <linux/inetdevice.h>
  26. #include <linux/init.h>
  27. #include <linux/kernel.h>
  28. #include <linux/module.h>
  29. #include <linux/notifier.h>
  30. #include <linux/pkt_sched.h>
  31. #include <linux/poll.h>
  32. #include <linux/rtnetlink.h>
  33. #include <linux/skbuff.h>
  34. #include <linux/slab.h>
  35. #include <net/net_namespace.h>
  36. static const char *version = "HDLC support module revision 1.22";
  37. #undef DEBUG_LINK
  38. static struct hdlc_proto *first_proto;
  39. static int hdlc_rcv(struct sk_buff *skb, struct net_device *dev,
  40. struct packet_type *p, struct net_device *orig_dev)
  41. {
  42. struct hdlc_device *hdlc;
  43. /* First make sure "dev" is an HDLC device */
  44. if (!(dev->priv_flags & IFF_WAN_HDLC)) {
  45. kfree_skb(skb);
  46. return NET_RX_SUCCESS;
  47. }
  48. hdlc = dev_to_hdlc(dev);
  49. if (!net_eq(dev_net(dev), &init_net)) {
  50. kfree_skb(skb);
  51. return 0;
  52. }
  53. BUG_ON(!hdlc->proto->netif_rx);
  54. return hdlc->proto->netif_rx(skb);
  55. }
  56. netdev_tx_t hdlc_start_xmit(struct sk_buff *skb, struct net_device *dev)
  57. {
  58. hdlc_device *hdlc = dev_to_hdlc(dev);
  59. if (hdlc->proto->xmit)
  60. return hdlc->proto->xmit(skb, dev);
  61. return hdlc->xmit(skb, dev); /* call hardware driver directly */
  62. }
  63. EXPORT_SYMBOL(hdlc_start_xmit);
  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 = netdev_notifier_info_to_dev(ptr);
  80. hdlc_device *hdlc;
  81. unsigned long flags;
  82. int on;
  83. if (!net_eq(dev_net(dev), &init_net))
  84. return NOTIFY_DONE;
  85. if (!(dev->priv_flags & IFF_WAN_HDLC))
  86. return NOTIFY_DONE; /* not an HDLC device */
  87. if (event != NETDEV_CHANGE)
  88. return NOTIFY_DONE; /* Only interested 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. netdev_info(dev, "Carrier detected\n");
  103. hdlc_proto_start(dev);
  104. } else {
  105. netdev_info(dev, "Carrier lost\n");
  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)
  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. netdev_info(dev, "Carrier detected\n");
  130. hdlc_proto_start(dev);
  131. } else {
  132. netdev_info(dev, "No carrier\n");
  133. }
  134. hdlc->open = 1;
  135. spin_unlock_irq(&hdlc->state_lock);
  136. return 0;
  137. }
  138. EXPORT_SYMBOL(hdlc_open);
  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. EXPORT_SYMBOL(hdlc_close);
  156. int hdlc_ioctl(struct net_device *dev, struct if_settings *ifs)
  157. {
  158. struct hdlc_proto *proto = first_proto;
  159. int result;
  160. if (dev_to_hdlc(dev)->proto) {
  161. result = dev_to_hdlc(dev)->proto->ioctl(dev, ifs);
  162. if (result != -EINVAL)
  163. return result;
  164. }
  165. /* Not handled by currently attached protocol (if any) */
  166. while (proto) {
  167. result = proto->ioctl(dev, ifs);
  168. if (result != -EINVAL)
  169. return result;
  170. proto = proto->next;
  171. }
  172. return -EINVAL;
  173. }
  174. EXPORT_SYMBOL(hdlc_ioctl);
  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->min_mtu = 68;
  185. dev->max_mtu = HDLC_MAX_MTU;
  186. dev->type = ARPHRD_RAWHDLC;
  187. dev->hard_header_len = 0;
  188. dev->needed_headroom = 0;
  189. dev->addr_len = 0;
  190. dev->header_ops = &hdlc_null_ops;
  191. }
  192. static void hdlc_setup(struct net_device *dev)
  193. {
  194. hdlc_device *hdlc = dev_to_hdlc(dev);
  195. hdlc_setup_dev(dev);
  196. hdlc->carrier = 1;
  197. hdlc->open = 0;
  198. spin_lock_init(&hdlc->state_lock);
  199. }
  200. struct net_device *alloc_hdlcdev(void *priv)
  201. {
  202. struct net_device *dev;
  203. dev = alloc_netdev(sizeof(struct hdlc_device), "hdlc%d",
  204. NET_NAME_UNKNOWN, hdlc_setup);
  205. if (dev)
  206. dev_to_hdlc(dev)->priv = priv;
  207. return dev;
  208. }
  209. EXPORT_SYMBOL(alloc_hdlcdev);
  210. void unregister_hdlc_device(struct net_device *dev)
  211. {
  212. rtnl_lock();
  213. detach_hdlc_protocol(dev);
  214. unregister_netdevice(dev);
  215. rtnl_unlock();
  216. }
  217. EXPORT_SYMBOL(unregister_hdlc_device);
  218. int attach_hdlc_protocol(struct net_device *dev, struct hdlc_proto *proto,
  219. size_t size)
  220. {
  221. int err;
  222. err = detach_hdlc_protocol(dev);
  223. if (err)
  224. return err;
  225. if (!try_module_get(proto->module))
  226. return -ENOSYS;
  227. if (size) {
  228. dev_to_hdlc(dev)->state = kmalloc(size, GFP_KERNEL);
  229. if (!dev_to_hdlc(dev)->state) {
  230. module_put(proto->module);
  231. return -ENOBUFS;
  232. }
  233. }
  234. dev_to_hdlc(dev)->proto = proto;
  235. return 0;
  236. }
  237. EXPORT_SYMBOL(attach_hdlc_protocol);
  238. int detach_hdlc_protocol(struct net_device *dev)
  239. {
  240. hdlc_device *hdlc = dev_to_hdlc(dev);
  241. int err;
  242. if (hdlc->proto) {
  243. err = call_netdevice_notifiers(NETDEV_PRE_TYPE_CHANGE, dev);
  244. err = notifier_to_errno(err);
  245. if (err) {
  246. netdev_err(dev, "Refused to change device type\n");
  247. return err;
  248. }
  249. if (hdlc->proto->detach)
  250. hdlc->proto->detach(dev);
  251. module_put(hdlc->proto->module);
  252. hdlc->proto = NULL;
  253. }
  254. kfree(hdlc->state);
  255. hdlc->state = NULL;
  256. hdlc_setup_dev(dev);
  257. return 0;
  258. }
  259. EXPORT_SYMBOL(detach_hdlc_protocol);
  260. void register_hdlc_protocol(struct hdlc_proto *proto)
  261. {
  262. rtnl_lock();
  263. proto->next = first_proto;
  264. first_proto = proto;
  265. rtnl_unlock();
  266. }
  267. EXPORT_SYMBOL(register_hdlc_protocol);
  268. void unregister_hdlc_protocol(struct hdlc_proto *proto)
  269. {
  270. struct hdlc_proto **p;
  271. rtnl_lock();
  272. p = &first_proto;
  273. while (*p != proto) {
  274. BUG_ON(!*p);
  275. p = &((*p)->next);
  276. }
  277. *p = proto->next;
  278. rtnl_unlock();
  279. }
  280. EXPORT_SYMBOL(unregister_hdlc_protocol);
  281. MODULE_AUTHOR("Krzysztof Halasa <[email protected]>");
  282. MODULE_DESCRIPTION("HDLC support module");
  283. MODULE_LICENSE("GPL v2");
  284. static struct packet_type hdlc_packet_type __read_mostly = {
  285. .type = cpu_to_be16(ETH_P_HDLC),
  286. .func = hdlc_rcv,
  287. };
  288. static struct notifier_block hdlc_notifier = {
  289. .notifier_call = hdlc_device_event,
  290. };
  291. static int __init hdlc_module_init(void)
  292. {
  293. int result;
  294. pr_info("%s\n", version);
  295. result = register_netdevice_notifier(&hdlc_notifier);
  296. if (result)
  297. return result;
  298. dev_add_pack(&hdlc_packet_type);
  299. return 0;
  300. }
  301. static void __exit hdlc_module_exit(void)
  302. {
  303. dev_remove_pack(&hdlc_packet_type);
  304. unregister_netdevice_notifier(&hdlc_notifier);
  305. }
  306. module_init(hdlc_module_init);
  307. module_exit(hdlc_module_exit);