hdlc_cisco.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  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. u32 txseq; /* TX sequence number, 0 = none */
  52. u32 rxseq; /* RX sequence number */
  53. };
  54. static int cisco_ioctl(struct net_device *dev, struct ifreq *ifr);
  55. static inline struct cisco_state* state(hdlc_device *hdlc)
  56. {
  57. return (struct cisco_state *)hdlc->state;
  58. }
  59. static int cisco_hard_header(struct sk_buff *skb, struct net_device *dev,
  60. u16 type, const void *daddr, const void *saddr,
  61. unsigned int len)
  62. {
  63. struct hdlc_header *data;
  64. #ifdef DEBUG_HARD_HEADER
  65. printk(KERN_DEBUG "%s: cisco_hard_header called\n", dev->name);
  66. #endif
  67. skb_push(skb, sizeof(struct hdlc_header));
  68. data = (struct hdlc_header*)skb->data;
  69. if (type == CISCO_KEEPALIVE)
  70. data->address = CISCO_MULTICAST;
  71. else
  72. data->address = CISCO_UNICAST;
  73. data->control = 0;
  74. data->protocol = htons(type);
  75. return sizeof(struct hdlc_header);
  76. }
  77. static void cisco_keepalive_send(struct net_device *dev, u32 type,
  78. __be32 par1, __be32 par2)
  79. {
  80. struct sk_buff *skb;
  81. struct cisco_packet *data;
  82. skb = dev_alloc_skb(sizeof(struct hdlc_header) +
  83. sizeof(struct cisco_packet));
  84. if (!skb) {
  85. printk(KERN_WARNING
  86. "%s: Memory squeeze on cisco_keepalive_send()\n",
  87. dev->name);
  88. return;
  89. }
  90. skb_reserve(skb, 4);
  91. cisco_hard_header(skb, dev, CISCO_KEEPALIVE, NULL, NULL, 0);
  92. data = (struct cisco_packet*)(skb->data + 4);
  93. data->type = htonl(type);
  94. data->par1 = par1;
  95. data->par2 = par2;
  96. data->rel = cpu_to_be16(0xFFFF);
  97. /* we will need do_div here if 1000 % HZ != 0 */
  98. data->time = htonl((jiffies - INITIAL_JIFFIES) * (1000 / HZ));
  99. skb_put(skb, sizeof(struct cisco_packet));
  100. skb->priority = TC_PRIO_CONTROL;
  101. skb->dev = dev;
  102. skb_reset_network_header(skb);
  103. dev_queue_xmit(skb);
  104. }
  105. static __be16 cisco_type_trans(struct sk_buff *skb, struct net_device *dev)
  106. {
  107. struct hdlc_header *data = (struct hdlc_header*)skb->data;
  108. if (skb->len < sizeof(struct hdlc_header))
  109. return cpu_to_be16(ETH_P_HDLC);
  110. if (data->address != CISCO_MULTICAST &&
  111. data->address != CISCO_UNICAST)
  112. return cpu_to_be16(ETH_P_HDLC);
  113. switch(data->protocol) {
  114. case cpu_to_be16(ETH_P_IP):
  115. case cpu_to_be16(ETH_P_IPX):
  116. case cpu_to_be16(ETH_P_IPV6):
  117. skb_pull(skb, sizeof(struct hdlc_header));
  118. return data->protocol;
  119. default:
  120. return cpu_to_be16(ETH_P_HDLC);
  121. }
  122. }
  123. static int cisco_rx(struct sk_buff *skb)
  124. {
  125. struct net_device *dev = skb->dev;
  126. hdlc_device *hdlc = dev_to_hdlc(dev);
  127. struct cisco_state *st = state(hdlc);
  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. u32 ack;
  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. ack = ntohl(cisco_data->par2);
  183. if (ack && (ack == st->txseq ||
  184. /* our current REQ may be in transit */
  185. ack == st->txseq - 1)) {
  186. st->last_poll = jiffies;
  187. if (!st->up) {
  188. u32 sec, min, hrs, days;
  189. sec = ntohl(cisco_data->time) / 1000;
  190. min = sec / 60; sec -= min * 60;
  191. hrs = min / 60; min -= hrs * 60;
  192. days = hrs / 24; hrs -= days * 24;
  193. printk(KERN_INFO "%s: Link up (peer "
  194. "uptime %ud%uh%um%us)\n",
  195. dev->name, days, hrs, min, sec);
  196. netif_dormant_off(dev);
  197. st->up = 1;
  198. }
  199. }
  200. spin_unlock(&st->lock);
  201. dev_kfree_skb_any(skb);
  202. return NET_RX_SUCCESS;
  203. } /* switch(keepalive type) */
  204. } /* switch(protocol) */
  205. printk(KERN_INFO "%s: Unsupported protocol %x\n", dev->name,
  206. ntohs(data->protocol));
  207. dev_kfree_skb_any(skb);
  208. return NET_RX_DROP;
  209. rx_error:
  210. dev->stats.rx_errors++; /* Mark error */
  211. dev_kfree_skb_any(skb);
  212. return NET_RX_DROP;
  213. }
  214. static void cisco_timer(unsigned long arg)
  215. {
  216. struct net_device *dev = (struct net_device *)arg;
  217. hdlc_device *hdlc = dev_to_hdlc(dev);
  218. struct cisco_state *st = state(hdlc);
  219. spin_lock(&st->lock);
  220. if (st->up &&
  221. time_after(jiffies, st->last_poll + st->settings.timeout * HZ)) {
  222. st->up = 0;
  223. printk(KERN_INFO "%s: Link down\n", dev->name);
  224. netif_dormant_on(dev);
  225. }
  226. cisco_keepalive_send(dev, CISCO_KEEPALIVE_REQ, htonl(++st->txseq),
  227. htonl(st->rxseq));
  228. spin_unlock(&st->lock);
  229. st->timer.expires = jiffies + st->settings.interval * HZ;
  230. st->timer.function = cisco_timer;
  231. st->timer.data = arg;
  232. add_timer(&st->timer);
  233. }
  234. static void cisco_start(struct net_device *dev)
  235. {
  236. hdlc_device *hdlc = dev_to_hdlc(dev);
  237. struct cisco_state *st = state(hdlc);
  238. unsigned long flags;
  239. spin_lock_irqsave(&st->lock, flags);
  240. st->up = st->txseq = st->rxseq = 0;
  241. spin_unlock_irqrestore(&st->lock, flags);
  242. init_timer(&st->timer);
  243. st->timer.expires = jiffies + HZ; /* First poll after 1 s */
  244. st->timer.function = cisco_timer;
  245. st->timer.data = (unsigned long)dev;
  246. add_timer(&st->timer);
  247. }
  248. static void cisco_stop(struct net_device *dev)
  249. {
  250. hdlc_device *hdlc = dev_to_hdlc(dev);
  251. struct cisco_state *st = state(hdlc);
  252. unsigned long flags;
  253. del_timer_sync(&st->timer);
  254. spin_lock_irqsave(&st->lock, flags);
  255. netif_dormant_on(dev);
  256. st->up = st->txseq = 0;
  257. spin_unlock_irqrestore(&st->lock, flags);
  258. }
  259. static struct hdlc_proto proto = {
  260. .start = cisco_start,
  261. .stop = cisco_stop,
  262. .type_trans = cisco_type_trans,
  263. .ioctl = cisco_ioctl,
  264. .netif_rx = cisco_rx,
  265. .module = THIS_MODULE,
  266. };
  267. static const struct header_ops cisco_header_ops = {
  268. .create = cisco_hard_header,
  269. };
  270. static int cisco_ioctl(struct net_device *dev, struct ifreq *ifr)
  271. {
  272. cisco_proto __user *cisco_s = ifr->ifr_settings.ifs_ifsu.cisco;
  273. const size_t size = sizeof(cisco_proto);
  274. cisco_proto new_settings;
  275. hdlc_device *hdlc = dev_to_hdlc(dev);
  276. int result;
  277. switch (ifr->ifr_settings.type) {
  278. case IF_GET_PROTO:
  279. if (dev_to_hdlc(dev)->proto != &proto)
  280. return -EINVAL;
  281. ifr->ifr_settings.type = IF_PROTO_CISCO;
  282. if (ifr->ifr_settings.size < size) {
  283. ifr->ifr_settings.size = size; /* data size wanted */
  284. return -ENOBUFS;
  285. }
  286. if (copy_to_user(cisco_s, &state(hdlc)->settings, size))
  287. return -EFAULT;
  288. return 0;
  289. case IF_PROTO_CISCO:
  290. if (!capable(CAP_NET_ADMIN))
  291. return -EPERM;
  292. if (dev->flags & IFF_UP)
  293. return -EBUSY;
  294. if (copy_from_user(&new_settings, cisco_s, size))
  295. return -EFAULT;
  296. if (new_settings.interval < 1 ||
  297. new_settings.timeout < 2)
  298. return -EINVAL;
  299. result = hdlc->attach(dev, ENCODING_NRZ,PARITY_CRC16_PR1_CCITT);
  300. if (result)
  301. return result;
  302. result = attach_hdlc_protocol(dev, &proto,
  303. sizeof(struct cisco_state));
  304. if (result)
  305. return result;
  306. memcpy(&state(hdlc)->settings, &new_settings, size);
  307. spin_lock_init(&state(hdlc)->lock);
  308. dev->header_ops = &cisco_header_ops;
  309. dev->type = ARPHRD_CISCO;
  310. netif_dormant_on(dev);
  311. return 0;
  312. }
  313. return -EINVAL;
  314. }
  315. static int __init mod_init(void)
  316. {
  317. register_hdlc_protocol(&proto);
  318. return 0;
  319. }
  320. static void __exit mod_exit(void)
  321. {
  322. unregister_hdlc_protocol(&proto);
  323. }
  324. module_init(mod_init);
  325. module_exit(mod_exit);
  326. MODULE_AUTHOR("Krzysztof Halasa <khc@pm.waw.pl>");
  327. MODULE_DESCRIPTION("Cisco HDLC protocol support for generic HDLC");
  328. MODULE_LICENSE("GPL v2");