hdlc_cisco.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. /*
  2. * Generic HDLC support routines for Linux
  3. * Cisco HDLC support
  4. *
  5. * Copyright (C) 2000 - 2006 Krzysztof Halasa <khc@pm.waw.pl>
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of version 2 of the GNU General Public License
  9. * as published by the Free Software Foundation.
  10. */
  11. #include <linux/module.h>
  12. #include <linux/kernel.h>
  13. #include <linux/slab.h>
  14. #include <linux/poll.h>
  15. #include <linux/errno.h>
  16. #include <linux/if_arp.h>
  17. #include <linux/init.h>
  18. #include <linux/skbuff.h>
  19. #include <linux/pkt_sched.h>
  20. #include <linux/inetdevice.h>
  21. #include <linux/lapb.h>
  22. #include <linux/rtnetlink.h>
  23. #include <linux/hdlc.h>
  24. #undef DEBUG_HARD_HEADER
  25. #define CISCO_MULTICAST 0x8F /* Cisco multicast address */
  26. #define CISCO_UNICAST 0x0F /* Cisco unicast address */
  27. #define CISCO_KEEPALIVE 0x8035 /* Cisco keepalive protocol */
  28. #define CISCO_SYS_INFO 0x2000 /* Cisco interface/system info */
  29. #define CISCO_ADDR_REQ 0 /* Cisco address request */
  30. #define CISCO_ADDR_REPLY 1 /* Cisco address reply */
  31. #define CISCO_KEEPALIVE_REQ 2 /* Cisco keepalive request */
  32. struct hdlc_header {
  33. u8 address;
  34. u8 control;
  35. __be16 protocol;
  36. }__attribute__ ((packed));
  37. struct cisco_packet {
  38. __be32 type; /* code */
  39. __be32 par1;
  40. __be32 par2;
  41. __be16 rel; /* reliability */
  42. __be32 time;
  43. }__attribute__ ((packed));
  44. #define CISCO_PACKET_LEN 18
  45. #define CISCO_BIG_PACKET_LEN 20
  46. struct cisco_state {
  47. cisco_proto settings;
  48. struct timer_list timer;
  49. unsigned long last_poll;
  50. int up;
  51. int request_sent;
  52. u32 txseq; /* TX sequence number */
  53. u32 rxseq; /* RX sequence number */
  54. };
  55. static int cisco_ioctl(struct net_device *dev, struct ifreq *ifr);
  56. static inline struct cisco_state * state(hdlc_device *hdlc)
  57. {
  58. return(struct cisco_state *)(hdlc->state);
  59. }
  60. static int cisco_hard_header(struct sk_buff *skb, struct net_device *dev,
  61. u16 type, void *daddr, void *saddr,
  62. unsigned int len)
  63. {
  64. struct hdlc_header *data;
  65. #ifdef DEBUG_HARD_HEADER
  66. printk(KERN_DEBUG "%s: cisco_hard_header called\n", dev->name);
  67. #endif
  68. skb_push(skb, sizeof(struct hdlc_header));
  69. data = (struct hdlc_header*)skb->data;
  70. if (type == CISCO_KEEPALIVE)
  71. data->address = CISCO_MULTICAST;
  72. else
  73. data->address = CISCO_UNICAST;
  74. data->control = 0;
  75. data->protocol = htons(type);
  76. return sizeof(struct hdlc_header);
  77. }
  78. static void cisco_keepalive_send(struct net_device *dev, u32 type,
  79. __be32 par1, __be32 par2)
  80. {
  81. struct sk_buff *skb;
  82. struct cisco_packet *data;
  83. skb = dev_alloc_skb(sizeof(struct hdlc_header) +
  84. sizeof(struct cisco_packet));
  85. if (!skb) {
  86. printk(KERN_WARNING
  87. "%s: Memory squeeze on cisco_keepalive_send()\n",
  88. dev->name);
  89. return;
  90. }
  91. skb_reserve(skb, 4);
  92. cisco_hard_header(skb, dev, CISCO_KEEPALIVE, NULL, NULL, 0);
  93. data = (struct cisco_packet*)(skb->data + 4);
  94. data->type = htonl(type);
  95. data->par1 = par1;
  96. data->par2 = par2;
  97. data->rel = __constant_htons(0xFFFF);
  98. /* we will need do_div here if 1000 % HZ != 0 */
  99. data->time = htonl((jiffies - INITIAL_JIFFIES) * (1000 / HZ));
  100. skb_put(skb, sizeof(struct cisco_packet));
  101. skb->priority = TC_PRIO_CONTROL;
  102. skb->dev = dev;
  103. skb_reset_network_header(skb);
  104. dev_queue_xmit(skb);
  105. }
  106. static __be16 cisco_type_trans(struct sk_buff *skb, struct net_device *dev)
  107. {
  108. struct hdlc_header *data = (struct hdlc_header*)skb->data;
  109. if (skb->len < sizeof(struct hdlc_header))
  110. return __constant_htons(ETH_P_HDLC);
  111. if (data->address != CISCO_MULTICAST &&
  112. data->address != CISCO_UNICAST)
  113. return __constant_htons(ETH_P_HDLC);
  114. switch(data->protocol) {
  115. case __constant_htons(ETH_P_IP):
  116. case __constant_htons(ETH_P_IPX):
  117. case __constant_htons(ETH_P_IPV6):
  118. skb_pull(skb, sizeof(struct hdlc_header));
  119. return data->protocol;
  120. default:
  121. return __constant_htons(ETH_P_HDLC);
  122. }
  123. }
  124. static int cisco_rx(struct sk_buff *skb)
  125. {
  126. struct net_device *dev = skb->dev;
  127. hdlc_device *hdlc = dev_to_hdlc(dev);
  128. struct hdlc_header *data = (struct hdlc_header*)skb->data;
  129. struct cisco_packet *cisco_data;
  130. struct in_device *in_dev;
  131. __be32 addr, mask;
  132. if (skb->len < sizeof(struct hdlc_header))
  133. goto rx_error;
  134. if (data->address != CISCO_MULTICAST &&
  135. data->address != CISCO_UNICAST)
  136. goto rx_error;
  137. switch(ntohs(data->protocol)) {
  138. case CISCO_SYS_INFO:
  139. /* Packet is not needed, drop it. */
  140. dev_kfree_skb_any(skb);
  141. return NET_RX_SUCCESS;
  142. case CISCO_KEEPALIVE:
  143. if ((skb->len != sizeof(struct hdlc_header) +
  144. CISCO_PACKET_LEN) &&
  145. (skb->len != sizeof(struct hdlc_header) +
  146. CISCO_BIG_PACKET_LEN)) {
  147. printk(KERN_INFO "%s: Invalid length of Cisco control"
  148. " packet (%d bytes)\n", dev->name, skb->len);
  149. goto rx_error;
  150. }
  151. cisco_data = (struct cisco_packet*)(skb->data + sizeof
  152. (struct hdlc_header));
  153. switch(ntohl (cisco_data->type)) {
  154. case CISCO_ADDR_REQ: /* Stolen from syncppp.c :-) */
  155. in_dev = dev->ip_ptr;
  156. addr = 0;
  157. mask = __constant_htonl(~0); /* is the mask correct? */
  158. if (in_dev != NULL) {
  159. struct in_ifaddr **ifap = &in_dev->ifa_list;
  160. while (*ifap != NULL) {
  161. if (strcmp(dev->name,
  162. (*ifap)->ifa_label) == 0) {
  163. addr = (*ifap)->ifa_local;
  164. mask = (*ifap)->ifa_mask;
  165. break;
  166. }
  167. ifap = &(*ifap)->ifa_next;
  168. }
  169. cisco_keepalive_send(dev, CISCO_ADDR_REPLY,
  170. addr, mask);
  171. }
  172. dev_kfree_skb_any(skb);
  173. return NET_RX_SUCCESS;
  174. case CISCO_ADDR_REPLY:
  175. printk(KERN_INFO "%s: Unexpected Cisco IP address "
  176. "reply\n", dev->name);
  177. goto rx_error;
  178. case CISCO_KEEPALIVE_REQ:
  179. state(hdlc)->rxseq = ntohl(cisco_data->par1);
  180. if (state(hdlc)->request_sent &&
  181. ntohl(cisco_data->par2) == state(hdlc)->txseq) {
  182. state(hdlc)->last_poll = jiffies;
  183. if (!state(hdlc)->up) {
  184. u32 sec, min, hrs, days;
  185. sec = ntohl(cisco_data->time) / 1000;
  186. min = sec / 60; sec -= min * 60;
  187. hrs = min / 60; min -= hrs * 60;
  188. days = hrs / 24; hrs -= days * 24;
  189. printk(KERN_INFO "%s: Link up (peer "
  190. "uptime %ud%uh%um%us)\n",
  191. dev->name, days, hrs,
  192. min, sec);
  193. netif_dormant_off(dev);
  194. state(hdlc)->up = 1;
  195. }
  196. }
  197. dev_kfree_skb_any(skb);
  198. return NET_RX_SUCCESS;
  199. } /* switch(keepalive type) */
  200. } /* switch(protocol) */
  201. printk(KERN_INFO "%s: Unsupported protocol %x\n", dev->name,
  202. ntohs(data->protocol));
  203. dev_kfree_skb_any(skb);
  204. return NET_RX_DROP;
  205. rx_error:
  206. dev_to_desc(dev)->stats.rx_errors++; /* Mark error */
  207. dev_kfree_skb_any(skb);
  208. return NET_RX_DROP;
  209. }
  210. static void cisco_timer(unsigned long arg)
  211. {
  212. struct net_device *dev = (struct net_device *)arg;
  213. hdlc_device *hdlc = dev_to_hdlc(dev);
  214. if (state(hdlc)->up &&
  215. time_after(jiffies, state(hdlc)->last_poll +
  216. state(hdlc)->settings.timeout * HZ)) {
  217. state(hdlc)->up = 0;
  218. printk(KERN_INFO "%s: Link down\n", dev->name);
  219. netif_dormant_on(dev);
  220. }
  221. cisco_keepalive_send(dev, CISCO_KEEPALIVE_REQ,
  222. htonl(++state(hdlc)->txseq),
  223. htonl(state(hdlc)->rxseq));
  224. state(hdlc)->request_sent = 1;
  225. state(hdlc)->timer.expires = jiffies +
  226. state(hdlc)->settings.interval * HZ;
  227. state(hdlc)->timer.function = cisco_timer;
  228. state(hdlc)->timer.data = arg;
  229. add_timer(&state(hdlc)->timer);
  230. }
  231. static void cisco_start(struct net_device *dev)
  232. {
  233. hdlc_device *hdlc = dev_to_hdlc(dev);
  234. state(hdlc)->up = 0;
  235. state(hdlc)->request_sent = 0;
  236. state(hdlc)->txseq = state(hdlc)->rxseq = 0;
  237. init_timer(&state(hdlc)->timer);
  238. state(hdlc)->timer.expires = jiffies + HZ; /*First poll after 1s*/
  239. state(hdlc)->timer.function = cisco_timer;
  240. state(hdlc)->timer.data = (unsigned long)dev;
  241. add_timer(&state(hdlc)->timer);
  242. }
  243. static void cisco_stop(struct net_device *dev)
  244. {
  245. hdlc_device *hdlc = dev_to_hdlc(dev);
  246. del_timer_sync(&state(hdlc)->timer);
  247. netif_dormant_on(dev);
  248. state(hdlc)->up = 0;
  249. state(hdlc)->request_sent = 0;
  250. }
  251. static struct hdlc_proto proto = {
  252. .start = cisco_start,
  253. .stop = cisco_stop,
  254. .type_trans = cisco_type_trans,
  255. .ioctl = cisco_ioctl,
  256. .module = THIS_MODULE,
  257. };
  258. static int cisco_ioctl(struct net_device *dev, struct ifreq *ifr)
  259. {
  260. cisco_proto __user *cisco_s = ifr->ifr_settings.ifs_ifsu.cisco;
  261. const size_t size = sizeof(cisco_proto);
  262. cisco_proto new_settings;
  263. hdlc_device *hdlc = dev_to_hdlc(dev);
  264. int result;
  265. switch (ifr->ifr_settings.type) {
  266. case IF_GET_PROTO:
  267. if (dev_to_hdlc(dev)->proto != &proto)
  268. return -EINVAL;
  269. ifr->ifr_settings.type = IF_PROTO_CISCO;
  270. if (ifr->ifr_settings.size < size) {
  271. ifr->ifr_settings.size = size; /* data size wanted */
  272. return -ENOBUFS;
  273. }
  274. if (copy_to_user(cisco_s, &state(hdlc)->settings, size))
  275. return -EFAULT;
  276. return 0;
  277. case IF_PROTO_CISCO:
  278. if(!capable(CAP_NET_ADMIN))
  279. return -EPERM;
  280. if(dev->flags & IFF_UP)
  281. return -EBUSY;
  282. if (copy_from_user(&new_settings, cisco_s, size))
  283. return -EFAULT;
  284. if (new_settings.interval < 1 ||
  285. new_settings.timeout < 2)
  286. return -EINVAL;
  287. result=hdlc->attach(dev, ENCODING_NRZ,PARITY_CRC16_PR1_CCITT);
  288. if (result)
  289. return result;
  290. result = attach_hdlc_protocol(dev, &proto, cisco_rx,
  291. sizeof(struct cisco_state));
  292. if (result)
  293. return result;
  294. memcpy(&state(hdlc)->settings, &new_settings, size);
  295. dev->hard_start_xmit = hdlc->xmit;
  296. dev->hard_header = cisco_hard_header;
  297. dev->type = ARPHRD_CISCO;
  298. netif_dormant_on(dev);
  299. return 0;
  300. }
  301. return -EINVAL;
  302. }
  303. static int __init mod_init(void)
  304. {
  305. register_hdlc_protocol(&proto);
  306. return 0;
  307. }
  308. static void __exit mod_exit(void)
  309. {
  310. unregister_hdlc_protocol(&proto);
  311. }
  312. module_init(mod_init);
  313. module_exit(mod_exit);
  314. MODULE_AUTHOR("Krzysztof Halasa <khc@pm.waw.pl>");
  315. MODULE_DESCRIPTION("Cisco HDLC protocol support for generic HDLC");
  316. MODULE_LICENSE("GPL v2");