hdlc_cisco.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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. spinlock_t lock;
  50. unsigned long last_poll;
  51. int up;
  52. int request_sent;
  53. u32 txseq; /* TX sequence number */
  54. u32 rxseq; /* RX sequence number */
  55. };
  56. static int cisco_ioctl(struct net_device *dev, struct ifreq *ifr);
  57. static inline struct cisco_state * state(hdlc_device *hdlc)
  58. {
  59. return(struct cisco_state *)(hdlc->state);
  60. }
  61. static int cisco_hard_header(struct sk_buff *skb, struct net_device *dev,
  62. u16 type, const void *daddr, const void *saddr,
  63. unsigned int len)
  64. {
  65. struct hdlc_header *data;
  66. #ifdef DEBUG_HARD_HEADER
  67. printk(KERN_DEBUG "%s: cisco_hard_header called\n", dev->name);
  68. #endif
  69. skb_push(skb, sizeof(struct hdlc_header));
  70. data = (struct hdlc_header*)skb->data;
  71. if (type == CISCO_KEEPALIVE)
  72. data->address = CISCO_MULTICAST;
  73. else
  74. data->address = CISCO_UNICAST;
  75. data->control = 0;
  76. data->protocol = htons(type);
  77. return sizeof(struct hdlc_header);
  78. }
  79. static void cisco_keepalive_send(struct net_device *dev, u32 type,
  80. __be32 par1, __be32 par2)
  81. {
  82. struct sk_buff *skb;
  83. struct cisco_packet *data;
  84. skb = dev_alloc_skb(sizeof(struct hdlc_header) +
  85. sizeof(struct cisco_packet));
  86. if (!skb) {
  87. printk(KERN_WARNING
  88. "%s: Memory squeeze on cisco_keepalive_send()\n",
  89. dev->name);
  90. return;
  91. }
  92. skb_reserve(skb, 4);
  93. cisco_hard_header(skb, dev, CISCO_KEEPALIVE, NULL, NULL, 0);
  94. data = (struct cisco_packet*)(skb->data + 4);
  95. data->type = htonl(type);
  96. data->par1 = par1;
  97. data->par2 = par2;
  98. data->rel = __constant_htons(0xFFFF);
  99. /* we will need do_div here if 1000 % HZ != 0 */
  100. data->time = htonl((jiffies - INITIAL_JIFFIES) * (1000 / HZ));
  101. skb_put(skb, sizeof(struct cisco_packet));
  102. skb->priority = TC_PRIO_CONTROL;
  103. skb->dev = dev;
  104. skb_reset_network_header(skb);
  105. dev_queue_xmit(skb);
  106. }
  107. static __be16 cisco_type_trans(struct sk_buff *skb, struct net_device *dev)
  108. {
  109. struct hdlc_header *data = (struct hdlc_header*)skb->data;
  110. if (skb->len < sizeof(struct hdlc_header))
  111. return __constant_htons(ETH_P_HDLC);
  112. if (data->address != CISCO_MULTICAST &&
  113. data->address != CISCO_UNICAST)
  114. return __constant_htons(ETH_P_HDLC);
  115. switch(data->protocol) {
  116. case __constant_htons(ETH_P_IP):
  117. case __constant_htons(ETH_P_IPX):
  118. case __constant_htons(ETH_P_IPV6):
  119. skb_pull(skb, sizeof(struct hdlc_header));
  120. return data->protocol;
  121. default:
  122. return __constant_htons(ETH_P_HDLC);
  123. }
  124. }
  125. static int cisco_rx(struct sk_buff *skb)
  126. {
  127. struct net_device *dev = skb->dev;
  128. hdlc_device *hdlc = dev_to_hdlc(dev);
  129. struct cisco_state *st = state(hdlc);
  130. struct hdlc_header *data = (struct hdlc_header*)skb->data;
  131. struct cisco_packet *cisco_data;
  132. struct in_device *in_dev;
  133. __be32 addr, mask;
  134. if (skb->len < sizeof(struct hdlc_header))
  135. goto rx_error;
  136. if (data->address != CISCO_MULTICAST &&
  137. data->address != CISCO_UNICAST)
  138. goto rx_error;
  139. switch(ntohs(data->protocol)) {
  140. case CISCO_SYS_INFO:
  141. /* Packet is not needed, drop it. */
  142. dev_kfree_skb_any(skb);
  143. return NET_RX_SUCCESS;
  144. case CISCO_KEEPALIVE:
  145. if ((skb->len != sizeof(struct hdlc_header) +
  146. CISCO_PACKET_LEN) &&
  147. (skb->len != sizeof(struct hdlc_header) +
  148. CISCO_BIG_PACKET_LEN)) {
  149. printk(KERN_INFO "%s: Invalid length of Cisco control"
  150. " packet (%d bytes)\n", dev->name, skb->len);
  151. goto rx_error;
  152. }
  153. cisco_data = (struct cisco_packet*)(skb->data + sizeof
  154. (struct hdlc_header));
  155. switch(ntohl (cisco_data->type)) {
  156. case CISCO_ADDR_REQ: /* Stolen from syncppp.c :-) */
  157. in_dev = dev->ip_ptr;
  158. addr = 0;
  159. mask = __constant_htonl(~0); /* is the mask correct? */
  160. if (in_dev != NULL) {
  161. struct in_ifaddr **ifap = &in_dev->ifa_list;
  162. while (*ifap != NULL) {
  163. if (strcmp(dev->name,
  164. (*ifap)->ifa_label) == 0) {
  165. addr = (*ifap)->ifa_local;
  166. mask = (*ifap)->ifa_mask;
  167. break;
  168. }
  169. ifap = &(*ifap)->ifa_next;
  170. }
  171. cisco_keepalive_send(dev, CISCO_ADDR_REPLY,
  172. addr, mask);
  173. }
  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. if (st->request_sent &&
  184. ntohl(cisco_data->par2) == st->txseq) {
  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. printk(KERN_INFO "%s: Link up (peer "
  193. "uptime %ud%uh%um%us)\n",
  194. dev->name, days, hrs, min, sec);
  195. netif_dormant_off(dev);
  196. st->up = 1;
  197. }
  198. }
  199. spin_unlock(&st->lock);
  200. dev_kfree_skb_any(skb);
  201. return NET_RX_SUCCESS;
  202. } /* switch(keepalive type) */
  203. } /* switch(protocol) */
  204. printk(KERN_INFO "%s: Unsupported protocol %x\n", dev->name,
  205. ntohs(data->protocol));
  206. dev_kfree_skb_any(skb);
  207. return NET_RX_DROP;
  208. rx_error:
  209. dev_to_hdlc(dev)->stats.rx_errors++; /* Mark error */
  210. dev_kfree_skb_any(skb);
  211. return NET_RX_DROP;
  212. }
  213. static void cisco_timer(unsigned long arg)
  214. {
  215. struct net_device *dev = (struct net_device *)arg;
  216. hdlc_device *hdlc = dev_to_hdlc(dev);
  217. struct cisco_state *st = state(hdlc);
  218. spin_lock(&st->lock);
  219. if (st->up &&
  220. time_after(jiffies, st->last_poll + st->settings.timeout * HZ)) {
  221. st->up = 0;
  222. printk(KERN_INFO "%s: Link down\n", dev->name);
  223. netif_dormant_on(dev);
  224. }
  225. cisco_keepalive_send(dev, CISCO_KEEPALIVE_REQ, htonl(++st->txseq),
  226. htonl(st->rxseq));
  227. st->request_sent = 1;
  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 = 0;
  241. st->request_sent = 0;
  242. st->txseq = st->rxseq = 0;
  243. spin_unlock_irqrestore(&st->lock, flags);
  244. init_timer(&st->timer);
  245. st->timer.expires = jiffies + HZ; /* First poll after 1 s */
  246. st->timer.function = cisco_timer;
  247. st->timer.data = (unsigned long)dev;
  248. add_timer(&st->timer);
  249. }
  250. static void cisco_stop(struct net_device *dev)
  251. {
  252. hdlc_device *hdlc = dev_to_hdlc(dev);
  253. struct cisco_state *st = state(hdlc);
  254. unsigned long flags;
  255. del_timer_sync(&st->timer);
  256. spin_lock_irqsave(&st->lock, flags);
  257. netif_dormant_on(dev);
  258. st->up = 0;
  259. st->request_sent = 0;
  260. spin_unlock_irqrestore(&st->lock, flags);
  261. }
  262. static struct hdlc_proto proto = {
  263. .start = cisco_start,
  264. .stop = cisco_stop,
  265. .type_trans = cisco_type_trans,
  266. .ioctl = cisco_ioctl,
  267. .netif_rx = cisco_rx,
  268. .module = THIS_MODULE,
  269. };
  270. static const struct header_ops cisco_header_ops = {
  271. .create = cisco_hard_header,
  272. };
  273. static int cisco_ioctl(struct net_device *dev, struct ifreq *ifr)
  274. {
  275. cisco_proto __user *cisco_s = ifr->ifr_settings.ifs_ifsu.cisco;
  276. const size_t size = sizeof(cisco_proto);
  277. cisco_proto new_settings;
  278. hdlc_device *hdlc = dev_to_hdlc(dev);
  279. int result;
  280. switch (ifr->ifr_settings.type) {
  281. case IF_GET_PROTO:
  282. if (dev_to_hdlc(dev)->proto != &proto)
  283. return -EINVAL;
  284. ifr->ifr_settings.type = IF_PROTO_CISCO;
  285. if (ifr->ifr_settings.size < size) {
  286. ifr->ifr_settings.size = size; /* data size wanted */
  287. return -ENOBUFS;
  288. }
  289. if (copy_to_user(cisco_s, &state(hdlc)->settings, size))
  290. return -EFAULT;
  291. return 0;
  292. case IF_PROTO_CISCO:
  293. if(!capable(CAP_NET_ADMIN))
  294. return -EPERM;
  295. if(dev->flags & IFF_UP)
  296. return -EBUSY;
  297. if (copy_from_user(&new_settings, cisco_s, size))
  298. return -EFAULT;
  299. if (new_settings.interval < 1 ||
  300. new_settings.timeout < 2)
  301. return -EINVAL;
  302. result=hdlc->attach(dev, ENCODING_NRZ,PARITY_CRC16_PR1_CCITT);
  303. if (result)
  304. return result;
  305. result = attach_hdlc_protocol(dev, &proto,
  306. sizeof(struct cisco_state));
  307. if (result)
  308. return result;
  309. memcpy(&state(hdlc)->settings, &new_settings, size);
  310. spin_lock_init(&state(hdlc)->lock);
  311. dev->hard_start_xmit = hdlc->xmit;
  312. dev->header_ops = &cisco_header_ops;
  313. dev->type = ARPHRD_CISCO;
  314. netif_dormant_on(dev);
  315. return 0;
  316. }
  317. return -EINVAL;
  318. }
  319. static int __init mod_init(void)
  320. {
  321. register_hdlc_protocol(&proto);
  322. return 0;
  323. }
  324. static void __exit mod_exit(void)
  325. {
  326. unregister_hdlc_protocol(&proto);
  327. }
  328. module_init(mod_init);
  329. module_exit(mod_exit);
  330. MODULE_AUTHOR("Krzysztof Halasa <khc@pm.waw.pl>");
  331. MODULE_DESCRIPTION("Cisco HDLC protocol support for generic HDLC");
  332. MODULE_LICENSE("GPL v2");