hdlc_cisco.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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. struct net_device *dev;
  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. netdev_warn(dev, "Memory squeeze on cisco_keepalive_send()\n");
  86. return;
  87. }
  88. skb_reserve(skb, 4);
  89. cisco_hard_header(skb, dev, CISCO_KEEPALIVE, NULL, NULL, 0);
  90. data = (struct cisco_packet*)(skb->data + 4);
  91. data->type = htonl(type);
  92. data->par1 = par1;
  93. data->par2 = par2;
  94. data->rel = cpu_to_be16(0xFFFF);
  95. /* we will need do_div here if 1000 % HZ != 0 */
  96. data->time = htonl((jiffies - INITIAL_JIFFIES) * (1000 / HZ));
  97. skb_put(skb, sizeof(struct cisco_packet));
  98. skb->priority = TC_PRIO_CONTROL;
  99. skb->dev = dev;
  100. skb_reset_network_header(skb);
  101. dev_queue_xmit(skb);
  102. }
  103. static __be16 cisco_type_trans(struct sk_buff *skb, struct net_device *dev)
  104. {
  105. struct hdlc_header *data = (struct hdlc_header*)skb->data;
  106. if (skb->len < sizeof(struct hdlc_header))
  107. return cpu_to_be16(ETH_P_HDLC);
  108. if (data->address != CISCO_MULTICAST &&
  109. data->address != CISCO_UNICAST)
  110. return cpu_to_be16(ETH_P_HDLC);
  111. switch (data->protocol) {
  112. case cpu_to_be16(ETH_P_IP):
  113. case cpu_to_be16(ETH_P_IPX):
  114. case cpu_to_be16(ETH_P_IPV6):
  115. skb_pull(skb, sizeof(struct hdlc_header));
  116. return data->protocol;
  117. default:
  118. return cpu_to_be16(ETH_P_HDLC);
  119. }
  120. }
  121. static int cisco_rx(struct sk_buff *skb)
  122. {
  123. struct net_device *dev = skb->dev;
  124. hdlc_device *hdlc = dev_to_hdlc(dev);
  125. struct cisco_state *st = state(hdlc);
  126. struct hdlc_header *data = (struct hdlc_header*)skb->data;
  127. struct cisco_packet *cisco_data;
  128. struct in_device *in_dev;
  129. __be32 addr, mask;
  130. u32 ack;
  131. if (skb->len < sizeof(struct hdlc_header))
  132. goto rx_error;
  133. if (data->address != CISCO_MULTICAST &&
  134. data->address != CISCO_UNICAST)
  135. goto rx_error;
  136. switch (ntohs(data->protocol)) {
  137. case CISCO_SYS_INFO:
  138. /* Packet is not needed, drop it. */
  139. dev_kfree_skb_any(skb);
  140. return NET_RX_SUCCESS;
  141. case CISCO_KEEPALIVE:
  142. if ((skb->len != sizeof(struct hdlc_header) +
  143. CISCO_PACKET_LEN) &&
  144. (skb->len != sizeof(struct hdlc_header) +
  145. CISCO_BIG_PACKET_LEN)) {
  146. netdev_info(dev, "Invalid length of Cisco control packet (%d bytes)\n",
  147. skb->len);
  148. goto rx_error;
  149. }
  150. cisco_data = (struct cisco_packet*)(skb->data + sizeof
  151. (struct hdlc_header));
  152. switch (ntohl (cisco_data->type)) {
  153. case CISCO_ADDR_REQ: /* Stolen from syncppp.c :-) */
  154. rcu_read_lock();
  155. in_dev = __in_dev_get_rcu(dev);
  156. addr = 0;
  157. mask = ~cpu_to_be32(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. rcu_read_unlock();
  173. dev_kfree_skb_any(skb);
  174. return NET_RX_SUCCESS;
  175. case CISCO_ADDR_REPLY:
  176. netdev_info(dev, "Unexpected Cisco IP address reply\n");
  177. goto rx_error;
  178. case CISCO_KEEPALIVE_REQ:
  179. spin_lock(&st->lock);
  180. st->rxseq = ntohl(cisco_data->par1);
  181. ack = ntohl(cisco_data->par2);
  182. if (ack && (ack == st->txseq ||
  183. /* our current REQ may be in transit */
  184. ack == st->txseq - 1)) {
  185. st->last_poll = jiffies;
  186. if (!st->up) {
  187. u32 sec, min, hrs, days;
  188. sec = ntohl(cisco_data->time) / 1000;
  189. min = sec / 60; sec -= min * 60;
  190. hrs = min / 60; min -= hrs * 60;
  191. days = hrs / 24; hrs -= days * 24;
  192. netdev_info(dev, "Link up (peer uptime %ud%uh%um%us)\n",
  193. 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. netdev_info(dev, "Unsupported protocol %x\n", ntohs(data->protocol));
  204. dev_kfree_skb_any(skb);
  205. return NET_RX_DROP;
  206. rx_error:
  207. dev->stats.rx_errors++; /* Mark error */
  208. dev_kfree_skb_any(skb);
  209. return NET_RX_DROP;
  210. }
  211. static void cisco_timer(struct timer_list *t)
  212. {
  213. struct cisco_state *st = from_timer(st, t, timer);
  214. struct net_device *dev = st->dev;
  215. spin_lock(&st->lock);
  216. if (st->up &&
  217. time_after(jiffies, st->last_poll + st->settings.timeout * HZ)) {
  218. st->up = 0;
  219. netdev_info(dev, "Link down\n");
  220. netif_dormant_on(dev);
  221. }
  222. cisco_keepalive_send(dev, CISCO_KEEPALIVE_REQ, htonl(++st->txseq),
  223. htonl(st->rxseq));
  224. spin_unlock(&st->lock);
  225. st->timer.expires = jiffies + st->settings.interval * HZ;
  226. add_timer(&st->timer);
  227. }
  228. static void cisco_start(struct net_device *dev)
  229. {
  230. hdlc_device *hdlc = dev_to_hdlc(dev);
  231. struct cisco_state *st = state(hdlc);
  232. unsigned long flags;
  233. spin_lock_irqsave(&st->lock, flags);
  234. st->up = st->txseq = st->rxseq = 0;
  235. spin_unlock_irqrestore(&st->lock, flags);
  236. st->dev = dev;
  237. timer_setup(&st->timer, cisco_timer, 0);
  238. st->timer.expires = jiffies + HZ; /* First poll after 1 s */
  239. add_timer(&st->timer);
  240. }
  241. static void cisco_stop(struct net_device *dev)
  242. {
  243. hdlc_device *hdlc = dev_to_hdlc(dev);
  244. struct cisco_state *st = state(hdlc);
  245. unsigned long flags;
  246. del_timer_sync(&st->timer);
  247. spin_lock_irqsave(&st->lock, flags);
  248. netif_dormant_on(dev);
  249. st->up = st->txseq = 0;
  250. spin_unlock_irqrestore(&st->lock, flags);
  251. }
  252. static struct hdlc_proto proto = {
  253. .start = cisco_start,
  254. .stop = cisco_stop,
  255. .type_trans = cisco_type_trans,
  256. .ioctl = cisco_ioctl,
  257. .netif_rx = cisco_rx,
  258. .module = THIS_MODULE,
  259. };
  260. static const struct header_ops cisco_header_ops = {
  261. .create = cisco_hard_header,
  262. };
  263. static int cisco_ioctl(struct net_device *dev, struct ifreq *ifr)
  264. {
  265. cisco_proto __user *cisco_s = ifr->ifr_settings.ifs_ifsu.cisco;
  266. const size_t size = sizeof(cisco_proto);
  267. cisco_proto new_settings;
  268. hdlc_device *hdlc = dev_to_hdlc(dev);
  269. int result;
  270. switch (ifr->ifr_settings.type) {
  271. case IF_GET_PROTO:
  272. if (dev_to_hdlc(dev)->proto != &proto)
  273. return -EINVAL;
  274. ifr->ifr_settings.type = IF_PROTO_CISCO;
  275. if (ifr->ifr_settings.size < size) {
  276. ifr->ifr_settings.size = size; /* data size wanted */
  277. return -ENOBUFS;
  278. }
  279. if (copy_to_user(cisco_s, &state(hdlc)->settings, size))
  280. return -EFAULT;
  281. return 0;
  282. case IF_PROTO_CISCO:
  283. if (!capable(CAP_NET_ADMIN))
  284. return -EPERM;
  285. if (dev->flags & IFF_UP)
  286. return -EBUSY;
  287. if (copy_from_user(&new_settings, cisco_s, size))
  288. return -EFAULT;
  289. if (new_settings.interval < 1 ||
  290. new_settings.timeout < 2)
  291. return -EINVAL;
  292. result = hdlc->attach(dev, ENCODING_NRZ,PARITY_CRC16_PR1_CCITT);
  293. if (result)
  294. return result;
  295. result = attach_hdlc_protocol(dev, &proto,
  296. sizeof(struct cisco_state));
  297. if (result)
  298. return result;
  299. memcpy(&state(hdlc)->settings, &new_settings, size);
  300. spin_lock_init(&state(hdlc)->lock);
  301. dev->header_ops = &cisco_header_ops;
  302. dev->type = ARPHRD_CISCO;
  303. call_netdevice_notifiers(NETDEV_POST_TYPE_CHANGE, dev);
  304. netif_dormant_on(dev);
  305. return 0;
  306. }
  307. return -EINVAL;
  308. }
  309. static int __init mod_init(void)
  310. {
  311. register_hdlc_protocol(&proto);
  312. return 0;
  313. }
  314. static void __exit mod_exit(void)
  315. {
  316. unregister_hdlc_protocol(&proto);
  317. }
  318. module_init(mod_init);
  319. module_exit(mod_exit);
  320. MODULE_AUTHOR("Krzysztof Halasa <khc@pm.waw.pl>");
  321. MODULE_DESCRIPTION("Cisco HDLC protocol support for generic HDLC");
  322. MODULE_LICENSE("GPL v2");