hdlc_generic.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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/config.h>
  25. #include <linux/module.h>
  26. #include <linux/kernel.h>
  27. #include <linux/slab.h>
  28. #include <linux/poll.h>
  29. #include <linux/errno.h>
  30. #include <linux/if_arp.h>
  31. #include <linux/init.h>
  32. #include <linux/skbuff.h>
  33. #include <linux/pkt_sched.h>
  34. #include <linux/inetdevice.h>
  35. #include <linux/lapb.h>
  36. #include <linux/rtnetlink.h>
  37. #include <linux/hdlc.h>
  38. static const char* version = "HDLC support module revision 1.18";
  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 void __hdlc_set_carrier_on(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. #if 0
  67. #ifdef DEBUG_LINK
  68. if (netif_carrier_ok(dev))
  69. printk(KERN_ERR "hdlc_set_carrier_on(): already on\n");
  70. #endif
  71. netif_carrier_on(dev);
  72. #endif
  73. }
  74. static void __hdlc_set_carrier_off(struct net_device *dev)
  75. {
  76. hdlc_device *hdlc = dev_to_hdlc(dev);
  77. if (hdlc->proto.stop)
  78. return hdlc->proto.stop(dev);
  79. #if 0
  80. #ifdef DEBUG_LINK
  81. if (!netif_carrier_ok(dev))
  82. printk(KERN_ERR "hdlc_set_carrier_off(): already off\n");
  83. #endif
  84. netif_carrier_off(dev);
  85. #endif
  86. }
  87. void hdlc_set_carrier(int on, struct net_device *dev)
  88. {
  89. hdlc_device *hdlc = dev_to_hdlc(dev);
  90. unsigned long flags;
  91. on = on ? 1 : 0;
  92. #ifdef DEBUG_LINK
  93. printk(KERN_DEBUG "hdlc_set_carrier %i\n", on);
  94. #endif
  95. spin_lock_irqsave(&hdlc->state_lock, flags);
  96. if (hdlc->carrier == on)
  97. goto carrier_exit; /* no change in DCD line level */
  98. #ifdef DEBUG_LINK
  99. printk(KERN_INFO "%s: carrier %s\n", dev->name, on ? "ON" : "off");
  100. #endif
  101. hdlc->carrier = on;
  102. if (!hdlc->open)
  103. goto carrier_exit;
  104. if (hdlc->carrier) {
  105. printk(KERN_INFO "%s: Carrier detected\n", dev->name);
  106. __hdlc_set_carrier_on(dev);
  107. } else {
  108. printk(KERN_INFO "%s: Carrier lost\n", dev->name);
  109. __hdlc_set_carrier_off(dev);
  110. }
  111. carrier_exit:
  112. spin_unlock_irqrestore(&hdlc->state_lock, flags);
  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 "hdlc_open() carrier %i open %i\n",
  120. hdlc->carrier, hdlc->open);
  121. #endif
  122. if (hdlc->proto.id == -1)
  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. printk(KERN_INFO "%s: Carrier detected\n", dev->name);
  132. __hdlc_set_carrier_on(dev);
  133. } else
  134. printk(KERN_INFO "%s: No carrier\n", dev->name);
  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 "hdlc_close() carrier %i open %i\n",
  145. hdlc->carrier, hdlc->open);
  146. #endif
  147. spin_lock_irq(&hdlc->state_lock);
  148. hdlc->open = 0;
  149. if (hdlc->carrier)
  150. __hdlc_set_carrier_off(dev);
  151. spin_unlock_irq(&hdlc->state_lock);
  152. if (hdlc->proto.close)
  153. hdlc->proto.close(dev);
  154. }
  155. #ifndef CONFIG_HDLC_RAW
  156. #define hdlc_raw_ioctl(dev, ifr) -ENOSYS
  157. #endif
  158. #ifndef CONFIG_HDLC_RAW_ETH
  159. #define hdlc_raw_eth_ioctl(dev, ifr) -ENOSYS
  160. #endif
  161. #ifndef CONFIG_HDLC_PPP
  162. #define hdlc_ppp_ioctl(dev, ifr) -ENOSYS
  163. #endif
  164. #ifndef CONFIG_HDLC_CISCO
  165. #define hdlc_cisco_ioctl(dev, ifr) -ENOSYS
  166. #endif
  167. #ifndef CONFIG_HDLC_FR
  168. #define hdlc_fr_ioctl(dev, ifr) -ENOSYS
  169. #endif
  170. #ifndef CONFIG_HDLC_X25
  171. #define hdlc_x25_ioctl(dev, ifr) -ENOSYS
  172. #endif
  173. int hdlc_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  174. {
  175. hdlc_device *hdlc = dev_to_hdlc(dev);
  176. unsigned int proto;
  177. if (cmd != SIOCWANDEV)
  178. return -EINVAL;
  179. switch(ifr->ifr_settings.type) {
  180. case IF_PROTO_HDLC:
  181. case IF_PROTO_HDLC_ETH:
  182. case IF_PROTO_PPP:
  183. case IF_PROTO_CISCO:
  184. case IF_PROTO_FR:
  185. case IF_PROTO_X25:
  186. proto = ifr->ifr_settings.type;
  187. break;
  188. default:
  189. proto = hdlc->proto.id;
  190. }
  191. switch(proto) {
  192. case IF_PROTO_HDLC: return hdlc_raw_ioctl(dev, ifr);
  193. case IF_PROTO_HDLC_ETH: return hdlc_raw_eth_ioctl(dev, ifr);
  194. case IF_PROTO_PPP: return hdlc_ppp_ioctl(dev, ifr);
  195. case IF_PROTO_CISCO: return hdlc_cisco_ioctl(dev, ifr);
  196. case IF_PROTO_FR: return hdlc_fr_ioctl(dev, ifr);
  197. case IF_PROTO_X25: return hdlc_x25_ioctl(dev, ifr);
  198. default: return -EINVAL;
  199. }
  200. }
  201. static void hdlc_setup(struct net_device *dev)
  202. {
  203. hdlc_device *hdlc = dev_to_hdlc(dev);
  204. dev->get_stats = hdlc_get_stats;
  205. dev->change_mtu = hdlc_change_mtu;
  206. dev->mtu = HDLC_MAX_MTU;
  207. dev->type = ARPHRD_RAWHDLC;
  208. dev->hard_header_len = 16;
  209. dev->flags = IFF_POINTOPOINT | IFF_NOARP;
  210. hdlc->proto.id = -1;
  211. hdlc->proto.detach = NULL;
  212. hdlc->carrier = 1;
  213. hdlc->open = 0;
  214. spin_lock_init(&hdlc->state_lock);
  215. }
  216. struct net_device *alloc_hdlcdev(void *priv)
  217. {
  218. struct net_device *dev;
  219. dev = alloc_netdev(sizeof(hdlc_device), "hdlc%d", hdlc_setup);
  220. if (dev)
  221. dev_to_hdlc(dev)->priv = priv;
  222. return dev;
  223. }
  224. int register_hdlc_device(struct net_device *dev)
  225. {
  226. int result = dev_alloc_name(dev, "hdlc%d");
  227. if (result < 0)
  228. return result;
  229. result = register_netdev(dev);
  230. if (result != 0)
  231. return -EIO;
  232. #if 0
  233. if (netif_carrier_ok(dev))
  234. netif_carrier_off(dev); /* no carrier until DCD goes up */
  235. #endif
  236. return 0;
  237. }
  238. void unregister_hdlc_device(struct net_device *dev)
  239. {
  240. rtnl_lock();
  241. hdlc_proto_detach(dev_to_hdlc(dev));
  242. unregister_netdevice(dev);
  243. rtnl_unlock();
  244. }
  245. MODULE_AUTHOR("Krzysztof Halasa <khc@pm.waw.pl>");
  246. MODULE_DESCRIPTION("HDLC support module");
  247. MODULE_LICENSE("GPL v2");
  248. EXPORT_SYMBOL(hdlc_open);
  249. EXPORT_SYMBOL(hdlc_close);
  250. EXPORT_SYMBOL(hdlc_set_carrier);
  251. EXPORT_SYMBOL(hdlc_ioctl);
  252. EXPORT_SYMBOL(alloc_hdlcdev);
  253. EXPORT_SYMBOL(register_hdlc_device);
  254. EXPORT_SYMBOL(unregister_hdlc_device);
  255. static struct packet_type hdlc_packet_type = {
  256. .type = __constant_htons(ETH_P_HDLC),
  257. .func = hdlc_rcv,
  258. };
  259. static int __init hdlc_module_init(void)
  260. {
  261. printk(KERN_INFO "%s\n", version);
  262. dev_add_pack(&hdlc_packet_type);
  263. return 0;
  264. }
  265. static void __exit hdlc_module_exit(void)
  266. {
  267. dev_remove_pack(&hdlc_packet_type);
  268. }
  269. module_init(hdlc_module_init);
  270. module_exit(hdlc_module_exit);