hdlc_generic.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. /*
  2. * Generic HDLC support routines for Linux
  3. *
  4. * Copyright (C) 1999 - 2005 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.19";
  39. #undef DEBUG_LINK
  40. static int hdlc_change_mtu(struct net_device *dev, int new_mtu)
  41. {
  42. if ((new_mtu < 68) || (new_mtu > HDLC_MAX_MTU))
  43. return -EINVAL;
  44. dev->mtu = new_mtu;
  45. return 0;
  46. }
  47. static struct net_device_stats *hdlc_get_stats(struct net_device *dev)
  48. {
  49. return hdlc_stats(dev);
  50. }
  51. static int hdlc_rcv(struct sk_buff *skb, struct net_device *dev,
  52. struct packet_type *p, struct net_device *orig_dev)
  53. {
  54. hdlc_device *hdlc = dev_to_hdlc(dev);
  55. if (hdlc->proto.netif_rx)
  56. return hdlc->proto.netif_rx(skb);
  57. hdlc->stats.rx_dropped++; /* Shouldn't happen */
  58. dev_kfree_skb(skb);
  59. return NET_RX_DROP;
  60. }
  61. static inline void hdlc_proto_start(struct net_device *dev)
  62. {
  63. hdlc_device *hdlc = dev_to_hdlc(dev);
  64. if (hdlc->proto.start)
  65. return hdlc->proto.start(dev);
  66. }
  67. static inline void hdlc_proto_stop(struct net_device *dev)
  68. {
  69. hdlc_device *hdlc = dev_to_hdlc(dev);
  70. if (hdlc->proto.stop)
  71. return hdlc->proto.stop(dev);
  72. }
  73. static int hdlc_device_event(struct notifier_block *this, unsigned long event,
  74. void *ptr)
  75. {
  76. struct net_device *dev = ptr;
  77. hdlc_device *hdlc;
  78. unsigned long flags;
  79. int on;
  80. if (dev->get_stats != hdlc_get_stats)
  81. return NOTIFY_DONE; /* not an HDLC device */
  82. if (event != NETDEV_CHANGE)
  83. return NOTIFY_DONE; /* Only interrested in carrier changes */
  84. on = netif_carrier_ok(dev);
  85. #ifdef DEBUG_LINK
  86. printk(KERN_DEBUG "%s: hdlc_device_event NETDEV_CHANGE, carrier %i\n",
  87. dev->name, on);
  88. #endif
  89. hdlc = dev_to_hdlc(dev);
  90. spin_lock_irqsave(&hdlc->state_lock, flags);
  91. if (hdlc->carrier == on)
  92. goto carrier_exit; /* no change in DCD line level */
  93. hdlc->carrier = on;
  94. if (!hdlc->open)
  95. goto carrier_exit;
  96. if (hdlc->carrier) {
  97. printk(KERN_INFO "%s: Carrier detected\n", dev->name);
  98. hdlc_proto_start(dev);
  99. } else {
  100. printk(KERN_INFO "%s: Carrier lost\n", dev->name);
  101. hdlc_proto_stop(dev);
  102. }
  103. carrier_exit:
  104. spin_unlock_irqrestore(&hdlc->state_lock, flags);
  105. return NOTIFY_DONE;
  106. }
  107. /* Must be called by hardware driver when HDLC device is being opened */
  108. int hdlc_open(struct net_device *dev)
  109. {
  110. hdlc_device *hdlc = dev_to_hdlc(dev);
  111. #ifdef DEBUG_LINK
  112. printk(KERN_DEBUG "hdlc_open() carrier %i open %i\n",
  113. hdlc->carrier, hdlc->open);
  114. #endif
  115. if (hdlc->proto.id == -1)
  116. return -ENOSYS; /* no protocol attached */
  117. if (hdlc->proto.open) {
  118. int result = hdlc->proto.open(dev);
  119. if (result)
  120. return result;
  121. }
  122. spin_lock_irq(&hdlc->state_lock);
  123. if (hdlc->carrier) {
  124. printk(KERN_INFO "%s: Carrier detected\n", dev->name);
  125. hdlc_proto_start(dev);
  126. } else
  127. printk(KERN_INFO "%s: No carrier\n", dev->name);
  128. hdlc->open = 1;
  129. spin_unlock_irq(&hdlc->state_lock);
  130. return 0;
  131. }
  132. /* Must be called by hardware driver when HDLC device is being closed */
  133. void hdlc_close(struct net_device *dev)
  134. {
  135. hdlc_device *hdlc = dev_to_hdlc(dev);
  136. #ifdef DEBUG_LINK
  137. printk(KERN_DEBUG "hdlc_close() carrier %i open %i\n",
  138. hdlc->carrier, hdlc->open);
  139. #endif
  140. spin_lock_irq(&hdlc->state_lock);
  141. hdlc->open = 0;
  142. if (hdlc->carrier)
  143. hdlc_proto_stop(dev);
  144. spin_unlock_irq(&hdlc->state_lock);
  145. if (hdlc->proto.close)
  146. hdlc->proto.close(dev);
  147. }
  148. #ifndef CONFIG_HDLC_RAW
  149. #define hdlc_raw_ioctl(dev, ifr) -ENOSYS
  150. #endif
  151. #ifndef CONFIG_HDLC_RAW_ETH
  152. #define hdlc_raw_eth_ioctl(dev, ifr) -ENOSYS
  153. #endif
  154. #ifndef CONFIG_HDLC_PPP
  155. #define hdlc_ppp_ioctl(dev, ifr) -ENOSYS
  156. #endif
  157. #ifndef CONFIG_HDLC_CISCO
  158. #define hdlc_cisco_ioctl(dev, ifr) -ENOSYS
  159. #endif
  160. #ifndef CONFIG_HDLC_FR
  161. #define hdlc_fr_ioctl(dev, ifr) -ENOSYS
  162. #endif
  163. #ifndef CONFIG_HDLC_X25
  164. #define hdlc_x25_ioctl(dev, ifr) -ENOSYS
  165. #endif
  166. int hdlc_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  167. {
  168. hdlc_device *hdlc = dev_to_hdlc(dev);
  169. unsigned int proto;
  170. if (cmd != SIOCWANDEV)
  171. return -EINVAL;
  172. switch(ifr->ifr_settings.type) {
  173. case IF_PROTO_HDLC:
  174. case IF_PROTO_HDLC_ETH:
  175. case IF_PROTO_PPP:
  176. case IF_PROTO_CISCO:
  177. case IF_PROTO_FR:
  178. case IF_PROTO_X25:
  179. proto = ifr->ifr_settings.type;
  180. break;
  181. default:
  182. proto = hdlc->proto.id;
  183. }
  184. switch(proto) {
  185. case IF_PROTO_HDLC: return hdlc_raw_ioctl(dev, ifr);
  186. case IF_PROTO_HDLC_ETH: return hdlc_raw_eth_ioctl(dev, ifr);
  187. case IF_PROTO_PPP: return hdlc_ppp_ioctl(dev, ifr);
  188. case IF_PROTO_CISCO: return hdlc_cisco_ioctl(dev, ifr);
  189. case IF_PROTO_FR: return hdlc_fr_ioctl(dev, ifr);
  190. case IF_PROTO_X25: return hdlc_x25_ioctl(dev, ifr);
  191. default: return -EINVAL;
  192. }
  193. }
  194. void hdlc_setup(struct net_device *dev)
  195. {
  196. hdlc_device *hdlc = dev_to_hdlc(dev);
  197. dev->get_stats = hdlc_get_stats;
  198. dev->change_mtu = hdlc_change_mtu;
  199. dev->mtu = HDLC_MAX_MTU;
  200. dev->type = ARPHRD_RAWHDLC;
  201. dev->hard_header_len = 16;
  202. dev->flags = IFF_POINTOPOINT | IFF_NOARP;
  203. hdlc->proto.id = -1;
  204. hdlc->proto.detach = NULL;
  205. hdlc->carrier = 1;
  206. hdlc->open = 0;
  207. spin_lock_init(&hdlc->state_lock);
  208. }
  209. struct net_device *alloc_hdlcdev(void *priv)
  210. {
  211. struct net_device *dev;
  212. dev = alloc_netdev(sizeof(hdlc_device), "hdlc%d", hdlc_setup);
  213. if (dev)
  214. dev_to_hdlc(dev)->priv = priv;
  215. return dev;
  216. }
  217. void unregister_hdlc_device(struct net_device *dev)
  218. {
  219. rtnl_lock();
  220. hdlc_proto_detach(dev_to_hdlc(dev));
  221. unregister_netdevice(dev);
  222. rtnl_unlock();
  223. }
  224. MODULE_AUTHOR("Krzysztof Halasa <khc@pm.waw.pl>");
  225. MODULE_DESCRIPTION("HDLC support module");
  226. MODULE_LICENSE("GPL v2");
  227. EXPORT_SYMBOL(hdlc_open);
  228. EXPORT_SYMBOL(hdlc_close);
  229. EXPORT_SYMBOL(hdlc_ioctl);
  230. EXPORT_SYMBOL(hdlc_setup);
  231. EXPORT_SYMBOL(alloc_hdlcdev);
  232. EXPORT_SYMBOL(unregister_hdlc_device);
  233. static struct packet_type hdlc_packet_type = {
  234. .type = __constant_htons(ETH_P_HDLC),
  235. .func = hdlc_rcv,
  236. };
  237. static struct notifier_block hdlc_notifier = {
  238. .notifier_call = hdlc_device_event,
  239. };
  240. static int __init hdlc_module_init(void)
  241. {
  242. int result;
  243. printk(KERN_INFO "%s\n", version);
  244. if ((result = register_netdevice_notifier(&hdlc_notifier)) != 0)
  245. return result;
  246. dev_add_pack(&hdlc_packet_type);
  247. return 0;
  248. }
  249. static void __exit hdlc_module_exit(void)
  250. {
  251. dev_remove_pack(&hdlc_packet_type);
  252. unregister_netdevice_notifier(&hdlc_notifier);
  253. }
  254. module_init(hdlc_module_init);
  255. module_exit(hdlc_module_exit);