hdlc.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. /*
  2. * Generic HDLC support routines for Linux
  3. *
  4. * Copyright (C) 1999 - 2006 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. static const char* version = "HDLC support module revision 1.21";
  39. #undef DEBUG_LINK
  40. static struct hdlc_proto *first_proto = NULL;
  41. static int hdlc_change_mtu(struct net_device *dev, int new_mtu)
  42. {
  43. if ((new_mtu < 68) || (new_mtu > HDLC_MAX_MTU))
  44. return -EINVAL;
  45. dev->mtu = new_mtu;
  46. return 0;
  47. }
  48. static struct net_device_stats *hdlc_get_stats(struct net_device *dev)
  49. {
  50. return hdlc_stats(dev);
  51. }
  52. static int hdlc_rcv(struct sk_buff *skb, struct net_device *dev,
  53. struct packet_type *p, struct net_device *orig_dev)
  54. {
  55. struct hdlc_device_desc *desc = dev_to_desc(dev);
  56. if (desc->netif_rx)
  57. return desc->netif_rx(skb);
  58. desc->stats.rx_dropped++; /* Shouldn't happen */
  59. dev_kfree_skb(skb);
  60. return NET_RX_DROP;
  61. }
  62. static inline void hdlc_proto_start(struct net_device *dev)
  63. {
  64. hdlc_device *hdlc = dev_to_hdlc(dev);
  65. if (hdlc->proto->start)
  66. return hdlc->proto->start(dev);
  67. }
  68. static inline void hdlc_proto_stop(struct net_device *dev)
  69. {
  70. hdlc_device *hdlc = dev_to_hdlc(dev);
  71. if (hdlc->proto->stop)
  72. return hdlc->proto->stop(dev);
  73. }
  74. static int hdlc_device_event(struct notifier_block *this, unsigned long event,
  75. void *ptr)
  76. {
  77. struct net_device *dev = ptr;
  78. hdlc_device *hdlc;
  79. unsigned long flags;
  80. int on;
  81. if (dev->get_stats != hdlc_get_stats)
  82. return NOTIFY_DONE; /* not an HDLC device */
  83. if (event != NETDEV_CHANGE)
  84. return NOTIFY_DONE; /* Only interrested 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. printk(KERN_INFO "%s: Carrier detected\n", dev->name);
  99. hdlc_proto_start(dev);
  100. } else {
  101. printk(KERN_INFO "%s: Carrier lost\n", dev->name);
  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. printk(KERN_INFO "%s: Carrier detected\n", dev->name);
  126. hdlc_proto_start(dev);
  127. } else
  128. printk(KERN_INFO "%s: No carrier\n", dev->name);
  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 void hdlc_setup_dev(struct net_device *dev)
  169. {
  170. /* Re-init all variables changed by HDLC protocol drivers,
  171. * including ether_setup() called from hdlc_raw_eth.c.
  172. */
  173. dev->get_stats = hdlc_get_stats;
  174. dev->flags = IFF_POINTOPOINT | IFF_NOARP;
  175. dev->mtu = HDLC_MAX_MTU;
  176. dev->type = ARPHRD_RAWHDLC;
  177. dev->hard_header_len = 16;
  178. dev->addr_len = 0;
  179. dev->hard_header = NULL;
  180. dev->rebuild_header = NULL;
  181. dev->set_mac_address = NULL;
  182. dev->hard_header_cache = NULL;
  183. dev->header_cache_update = NULL;
  184. dev->change_mtu = hdlc_change_mtu;
  185. dev->hard_header_parse = NULL;
  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_desc) +
  199. sizeof(hdlc_device), "hdlc%d", hdlc_setup);
  200. if (dev)
  201. dev_to_hdlc(dev)->priv = priv;
  202. return dev;
  203. }
  204. void unregister_hdlc_device(struct net_device *dev)
  205. {
  206. rtnl_lock();
  207. unregister_netdevice(dev);
  208. detach_hdlc_protocol(dev);
  209. rtnl_unlock();
  210. }
  211. int attach_hdlc_protocol(struct net_device *dev, struct hdlc_proto *proto,
  212. int (*rx)(struct sk_buff *skb), size_t size)
  213. {
  214. detach_hdlc_protocol(dev);
  215. if (!try_module_get(proto->module))
  216. return -ENOSYS;
  217. if (size)
  218. if ((dev_to_hdlc(dev)->state = kmalloc(size,
  219. GFP_KERNEL)) == NULL) {
  220. printk(KERN_WARNING "Memory squeeze on"
  221. " hdlc_proto_attach()\n");
  222. module_put(proto->module);
  223. return -ENOBUFS;
  224. }
  225. dev_to_hdlc(dev)->proto = proto;
  226. dev_to_desc(dev)->netif_rx = rx;
  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. proto->next = first_proto;
  245. first_proto = proto;
  246. }
  247. void unregister_hdlc_protocol(struct hdlc_proto *proto)
  248. {
  249. struct hdlc_proto **p = &first_proto;
  250. while (*p) {
  251. if (*p == proto) {
  252. *p = proto->next;
  253. return;
  254. }
  255. p = &((*p)->next);
  256. }
  257. }
  258. MODULE_AUTHOR("Krzysztof Halasa <khc@pm.waw.pl>");
  259. MODULE_DESCRIPTION("HDLC support module");
  260. MODULE_LICENSE("GPL v2");
  261. EXPORT_SYMBOL(hdlc_open);
  262. EXPORT_SYMBOL(hdlc_close);
  263. EXPORT_SYMBOL(hdlc_ioctl);
  264. EXPORT_SYMBOL(alloc_hdlcdev);
  265. EXPORT_SYMBOL(unregister_hdlc_device);
  266. EXPORT_SYMBOL(register_hdlc_protocol);
  267. EXPORT_SYMBOL(unregister_hdlc_protocol);
  268. EXPORT_SYMBOL(attach_hdlc_protocol);
  269. EXPORT_SYMBOL(detach_hdlc_protocol);
  270. static struct packet_type hdlc_packet_type = {
  271. .type = __constant_htons(ETH_P_HDLC),
  272. .func = hdlc_rcv,
  273. };
  274. static struct notifier_block hdlc_notifier = {
  275. .notifier_call = hdlc_device_event,
  276. };
  277. static int __init hdlc_module_init(void)
  278. {
  279. int result;
  280. printk(KERN_INFO "%s\n", version);
  281. if ((result = register_netdevice_notifier(&hdlc_notifier)) != 0)
  282. return result;
  283. dev_add_pack(&hdlc_packet_type);
  284. return 0;
  285. }
  286. static void __exit hdlc_module_exit(void)
  287. {
  288. dev_remove_pack(&hdlc_packet_type);
  289. unregister_netdevice_notifier(&hdlc_notifier);
  290. }
  291. module_init(hdlc_module_init);
  292. module_exit(hdlc_module_exit);