hdlc_cisco.c 9.7 KB

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