hdlc_cisco.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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/errno.h>
  12. #include <linux/hdlc.h>
  13. #include <linux/if_arp.h>
  14. #include <linux/inetdevice.h>
  15. #include <linux/init.h>
  16. #include <linux/kernel.h>
  17. #include <linux/module.h>
  18. #include <linux/pkt_sched.h>
  19. #include <linux/poll.h>
  20. #include <linux/rtnetlink.h>
  21. #include <linux/skbuff.h>
  22. #include <linux/slab.h>
  23. #undef DEBUG_HARD_HEADER
  24. #define CISCO_MULTICAST 0x8F /* Cisco multicast address */
  25. #define CISCO_UNICAST 0x0F /* Cisco unicast address */
  26. #define CISCO_KEEPALIVE 0x8035 /* Cisco keepalive protocol */
  27. #define CISCO_SYS_INFO 0x2000 /* Cisco interface/system info */
  28. #define CISCO_ADDR_REQ 0 /* Cisco address request */
  29. #define CISCO_ADDR_REPLY 1 /* Cisco address reply */
  30. #define CISCO_KEEPALIVE_REQ 2 /* Cisco keepalive request */
  31. struct hdlc_header {
  32. u8 address;
  33. u8 control;
  34. __be16 protocol;
  35. }__attribute__ ((packed));
  36. struct cisco_packet {
  37. __be32 type; /* code */
  38. __be32 par1;
  39. __be32 par2;
  40. __be16 rel; /* reliability */
  41. __be32 time;
  42. }__attribute__ ((packed));
  43. #define CISCO_PACKET_LEN 18
  44. #define CISCO_BIG_PACKET_LEN 20
  45. struct cisco_state {
  46. cisco_proto settings;
  47. struct timer_list timer;
  48. spinlock_t lock;
  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, const void *daddr, const 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 = cpu_to_be16(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 cpu_to_be16(ETH_P_HDLC);
  111. if (data->address != CISCO_MULTICAST &&
  112. data->address != CISCO_UNICAST)
  113. return cpu_to_be16(ETH_P_HDLC);
  114. switch(data->protocol) {
  115. case cpu_to_be16(ETH_P_IP):
  116. case cpu_to_be16(ETH_P_IPX):
  117. case cpu_to_be16(ETH_P_IPV6):
  118. skb_pull(skb, sizeof(struct hdlc_header));
  119. return data->protocol;
  120. default:
  121. return cpu_to_be16(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 cisco_state *st = state(hdlc);
  129. struct hdlc_header *data = (struct hdlc_header*)skb->data;
  130. struct cisco_packet *cisco_data;
  131. struct in_device *in_dev;
  132. __be32 addr, mask;
  133. if (skb->len < sizeof(struct hdlc_header))
  134. goto rx_error;
  135. if (data->address != CISCO_MULTICAST &&
  136. data->address != CISCO_UNICAST)
  137. goto rx_error;
  138. switch (ntohs(data->protocol)) {
  139. case CISCO_SYS_INFO:
  140. /* Packet is not needed, drop it. */
  141. dev_kfree_skb_any(skb);
  142. return NET_RX_SUCCESS;
  143. case CISCO_KEEPALIVE:
  144. if ((skb->len != sizeof(struct hdlc_header) +
  145. CISCO_PACKET_LEN) &&
  146. (skb->len != sizeof(struct hdlc_header) +
  147. CISCO_BIG_PACKET_LEN)) {
  148. printk(KERN_INFO "%s: Invalid length of Cisco control"
  149. " packet (%d bytes)\n", dev->name, skb->len);
  150. goto rx_error;
  151. }
  152. cisco_data = (struct cisco_packet*)(skb->data + sizeof
  153. (struct hdlc_header));
  154. switch(ntohl (cisco_data->type)) {
  155. case CISCO_ADDR_REQ: /* Stolen from syncppp.c :-) */
  156. in_dev = dev->ip_ptr;
  157. addr = 0;
  158. mask = ~cpu_to_be32(0); /* is the mask correct? */
  159. if (in_dev != NULL) {
  160. struct in_ifaddr **ifap = &in_dev->ifa_list;
  161. while (*ifap != NULL) {
  162. if (strcmp(dev->name,
  163. (*ifap)->ifa_label) == 0) {
  164. addr = (*ifap)->ifa_local;
  165. mask = (*ifap)->ifa_mask;
  166. break;
  167. }
  168. ifap = &(*ifap)->ifa_next;
  169. }
  170. cisco_keepalive_send(dev, CISCO_ADDR_REPLY,
  171. addr, mask);
  172. }
  173. dev_kfree_skb_any(skb);
  174. return NET_RX_SUCCESS;
  175. case CISCO_ADDR_REPLY:
  176. printk(KERN_INFO "%s: Unexpected Cisco IP address "
  177. "reply\n", dev->name);
  178. goto rx_error;
  179. case CISCO_KEEPALIVE_REQ:
  180. spin_lock(&st->lock);
  181. st->rxseq = ntohl(cisco_data->par1);
  182. if (st->request_sent &&
  183. ntohl(cisco_data->par2) == st->txseq) {
  184. st->last_poll = jiffies;
  185. if (!st->up) {
  186. u32 sec, min, hrs, days;
  187. sec = ntohl(cisco_data->time) / 1000;
  188. min = sec / 60; sec -= min * 60;
  189. hrs = min / 60; min -= hrs * 60;
  190. days = hrs / 24; hrs -= days * 24;
  191. printk(KERN_INFO "%s: Link up (peer "
  192. "uptime %ud%uh%um%us)\n",
  193. dev->name, days, hrs, min, sec);
  194. netif_dormant_off(dev);
  195. st->up = 1;
  196. }
  197. }
  198. spin_unlock(&st->lock);
  199. dev_kfree_skb_any(skb);
  200. return NET_RX_SUCCESS;
  201. } /* switch(keepalive type) */
  202. } /* switch(protocol) */
  203. printk(KERN_INFO "%s: Unsupported protocol %x\n", dev->name,
  204. ntohs(data->protocol));
  205. dev_kfree_skb_any(skb);
  206. return NET_RX_DROP;
  207. rx_error:
  208. dev->stats.rx_errors++; /* Mark error */
  209. dev_kfree_skb_any(skb);
  210. return NET_RX_DROP;
  211. }
  212. static void cisco_timer(unsigned long arg)
  213. {
  214. struct net_device *dev = (struct net_device *)arg;
  215. hdlc_device *hdlc = dev_to_hdlc(dev);
  216. struct cisco_state *st = state(hdlc);
  217. spin_lock(&st->lock);
  218. if (st->up &&
  219. time_after(jiffies, st->last_poll + st->settings.timeout * HZ)) {
  220. st->up = 0;
  221. printk(KERN_INFO "%s: Link down\n", dev->name);
  222. netif_dormant_on(dev);
  223. }
  224. cisco_keepalive_send(dev, CISCO_KEEPALIVE_REQ, htonl(++st->txseq),
  225. htonl(st->rxseq));
  226. st->request_sent = 1;
  227. spin_unlock(&st->lock);
  228. st->timer.expires = jiffies + st->settings.interval * HZ;
  229. st->timer.function = cisco_timer;
  230. st->timer.data = arg;
  231. add_timer(&st->timer);
  232. }
  233. static void cisco_start(struct net_device *dev)
  234. {
  235. hdlc_device *hdlc = dev_to_hdlc(dev);
  236. struct cisco_state *st = state(hdlc);
  237. unsigned long flags;
  238. spin_lock_irqsave(&st->lock, flags);
  239. st->up = 0;
  240. st->request_sent = 0;
  241. st->txseq = st->rxseq = 0;
  242. spin_unlock_irqrestore(&st->lock, flags);
  243. init_timer(&st->timer);
  244. st->timer.expires = jiffies + HZ; /* First poll after 1 s */
  245. st->timer.function = cisco_timer;
  246. st->timer.data = (unsigned long)dev;
  247. add_timer(&st->timer);
  248. }
  249. static void cisco_stop(struct net_device *dev)
  250. {
  251. hdlc_device *hdlc = dev_to_hdlc(dev);
  252. struct cisco_state *st = state(hdlc);
  253. unsigned long flags;
  254. del_timer_sync(&st->timer);
  255. spin_lock_irqsave(&st->lock, flags);
  256. netif_dormant_on(dev);
  257. st->up = 0;
  258. st->request_sent = 0;
  259. spin_unlock_irqrestore(&st->lock, flags);
  260. }
  261. static struct hdlc_proto proto = {
  262. .start = cisco_start,
  263. .stop = cisco_stop,
  264. .type_trans = cisco_type_trans,
  265. .ioctl = cisco_ioctl,
  266. .netif_rx = cisco_rx,
  267. .module = THIS_MODULE,
  268. };
  269. static const struct header_ops cisco_header_ops = {
  270. .create = cisco_hard_header,
  271. };
  272. static int cisco_ioctl(struct net_device *dev, struct ifreq *ifr)
  273. {
  274. cisco_proto __user *cisco_s = ifr->ifr_settings.ifs_ifsu.cisco;
  275. const size_t size = sizeof(cisco_proto);
  276. cisco_proto new_settings;
  277. hdlc_device *hdlc = dev_to_hdlc(dev);
  278. int result;
  279. switch (ifr->ifr_settings.type) {
  280. case IF_GET_PROTO:
  281. if (dev_to_hdlc(dev)->proto != &proto)
  282. return -EINVAL;
  283. ifr->ifr_settings.type = IF_PROTO_CISCO;
  284. if (ifr->ifr_settings.size < size) {
  285. ifr->ifr_settings.size = size; /* data size wanted */
  286. return -ENOBUFS;
  287. }
  288. if (copy_to_user(cisco_s, &state(hdlc)->settings, size))
  289. return -EFAULT;
  290. return 0;
  291. case IF_PROTO_CISCO:
  292. if (!capable(CAP_NET_ADMIN))
  293. return -EPERM;
  294. if (dev->flags & IFF_UP)
  295. return -EBUSY;
  296. if (copy_from_user(&new_settings, cisco_s, size))
  297. return -EFAULT;
  298. if (new_settings.interval < 1 ||
  299. new_settings.timeout < 2)
  300. return -EINVAL;
  301. result = hdlc->attach(dev, ENCODING_NRZ,PARITY_CRC16_PR1_CCITT);
  302. if (result)
  303. return result;
  304. result = attach_hdlc_protocol(dev, &proto,
  305. sizeof(struct cisco_state));
  306. if (result)
  307. return result;
  308. memcpy(&state(hdlc)->settings, &new_settings, size);
  309. spin_lock_init(&state(hdlc)->lock);
  310. dev->header_ops = &cisco_header_ops;
  311. dev->type = ARPHRD_CISCO;
  312. netif_dormant_on(dev);
  313. return 0;
  314. }
  315. return -EINVAL;
  316. }
  317. static int __init mod_init(void)
  318. {
  319. register_hdlc_protocol(&proto);
  320. return 0;
  321. }
  322. static void __exit mod_exit(void)
  323. {
  324. unregister_hdlc_protocol(&proto);
  325. }
  326. module_init(mod_init);
  327. module_exit(mod_exit);
  328. MODULE_AUTHOR("Krzysztof Halasa <khc@pm.waw.pl>");
  329. MODULE_DESCRIPTION("Cisco HDLC protocol support for generic HDLC");
  330. MODULE_LICENSE("GPL v2");