hdlc_cisco.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. /*
  2. * Generic HDLC support routines for Linux
  3. * Cisco HDLC support
  4. *
  5. * Copyright (C) 2000 - 2003 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. static int cisco_hard_header(struct sk_buff *skb, struct net_device *dev,
  33. u16 type, void *daddr, void *saddr,
  34. unsigned int len)
  35. {
  36. hdlc_header *data;
  37. #ifdef DEBUG_HARD_HEADER
  38. printk(KERN_DEBUG "%s: cisco_hard_header called\n", dev->name);
  39. #endif
  40. skb_push(skb, sizeof(hdlc_header));
  41. data = (hdlc_header*)skb->data;
  42. if (type == CISCO_KEEPALIVE)
  43. data->address = CISCO_MULTICAST;
  44. else
  45. data->address = CISCO_UNICAST;
  46. data->control = 0;
  47. data->protocol = htons(type);
  48. return sizeof(hdlc_header);
  49. }
  50. static void cisco_keepalive_send(struct net_device *dev, u32 type,
  51. u32 par1, u32 par2)
  52. {
  53. struct sk_buff *skb;
  54. cisco_packet *data;
  55. skb = dev_alloc_skb(sizeof(hdlc_header) + sizeof(cisco_packet));
  56. if (!skb) {
  57. printk(KERN_WARNING
  58. "%s: Memory squeeze on cisco_keepalive_send()\n",
  59. dev->name);
  60. return;
  61. }
  62. skb_reserve(skb, 4);
  63. cisco_hard_header(skb, dev, CISCO_KEEPALIVE, NULL, NULL, 0);
  64. data = (cisco_packet*)skb->data;
  65. data->type = htonl(type);
  66. data->par1 = htonl(par1);
  67. data->par2 = htonl(par2);
  68. data->rel = 0xFFFF;
  69. /* we will need do_div here if 1000 % HZ != 0 */
  70. data->time = htonl((jiffies - INITIAL_JIFFIES) * (1000 / HZ));
  71. skb_put(skb, sizeof(cisco_packet));
  72. skb->priority = TC_PRIO_CONTROL;
  73. skb->dev = dev;
  74. skb->nh.raw = skb->data;
  75. dev_queue_xmit(skb);
  76. }
  77. static __be16 cisco_type_trans(struct sk_buff *skb, struct net_device *dev)
  78. {
  79. hdlc_header *data = (hdlc_header*)skb->data;
  80. if (skb->len < sizeof(hdlc_header))
  81. return __constant_htons(ETH_P_HDLC);
  82. if (data->address != CISCO_MULTICAST &&
  83. data->address != CISCO_UNICAST)
  84. return __constant_htons(ETH_P_HDLC);
  85. switch(data->protocol) {
  86. case __constant_htons(ETH_P_IP):
  87. case __constant_htons(ETH_P_IPX):
  88. case __constant_htons(ETH_P_IPV6):
  89. skb_pull(skb, sizeof(hdlc_header));
  90. return data->protocol;
  91. default:
  92. return __constant_htons(ETH_P_HDLC);
  93. }
  94. }
  95. static int cisco_rx(struct sk_buff *skb)
  96. {
  97. struct net_device *dev = skb->dev;
  98. hdlc_device *hdlc = dev_to_hdlc(dev);
  99. hdlc_header *data = (hdlc_header*)skb->data;
  100. cisco_packet *cisco_data;
  101. struct in_device *in_dev;
  102. u32 addr, mask;
  103. if (skb->len < sizeof(hdlc_header))
  104. goto rx_error;
  105. if (data->address != CISCO_MULTICAST &&
  106. data->address != CISCO_UNICAST)
  107. goto rx_error;
  108. switch(ntohs(data->protocol)) {
  109. case CISCO_SYS_INFO:
  110. /* Packet is not needed, drop it. */
  111. dev_kfree_skb_any(skb);
  112. return NET_RX_SUCCESS;
  113. case CISCO_KEEPALIVE:
  114. if (skb->len != sizeof(hdlc_header) + CISCO_PACKET_LEN &&
  115. skb->len != sizeof(hdlc_header) + CISCO_BIG_PACKET_LEN) {
  116. printk(KERN_INFO "%s: Invalid length of Cisco "
  117. "control packet (%d bytes)\n",
  118. dev->name, skb->len);
  119. goto rx_error;
  120. }
  121. cisco_data = (cisco_packet*)(skb->data + sizeof(hdlc_header));
  122. switch(ntohl (cisco_data->type)) {
  123. case CISCO_ADDR_REQ: /* Stolen from syncppp.c :-) */
  124. in_dev = dev->ip_ptr;
  125. addr = 0;
  126. mask = ~0; /* is the mask correct? */
  127. if (in_dev != NULL) {
  128. struct in_ifaddr **ifap = &in_dev->ifa_list;
  129. while (*ifap != NULL) {
  130. if (strcmp(dev->name,
  131. (*ifap)->ifa_label) == 0) {
  132. addr = (*ifap)->ifa_local;
  133. mask = (*ifap)->ifa_mask;
  134. break;
  135. }
  136. ifap = &(*ifap)->ifa_next;
  137. }
  138. cisco_keepalive_send(dev, CISCO_ADDR_REPLY,
  139. addr, mask);
  140. }
  141. dev_kfree_skb_any(skb);
  142. return NET_RX_SUCCESS;
  143. case CISCO_ADDR_REPLY:
  144. printk(KERN_INFO "%s: Unexpected Cisco IP address "
  145. "reply\n", dev->name);
  146. goto rx_error;
  147. case CISCO_KEEPALIVE_REQ:
  148. hdlc->state.cisco.rxseq = ntohl(cisco_data->par1);
  149. if (hdlc->state.cisco.request_sent &&
  150. ntohl(cisco_data->par2)==hdlc->state.cisco.txseq) {
  151. hdlc->state.cisco.last_poll = jiffies;
  152. if (!hdlc->state.cisco.up) {
  153. u32 sec, min, hrs, days;
  154. sec = ntohl(cisco_data->time) / 1000;
  155. min = sec / 60; sec -= min * 60;
  156. hrs = min / 60; min -= hrs * 60;
  157. days = hrs / 24; hrs -= days * 24;
  158. printk(KERN_INFO "%s: Link up (peer "
  159. "uptime %ud%uh%um%us)\n",
  160. dev->name, days, hrs,
  161. min, sec);
  162. netif_carrier_on(dev);
  163. hdlc->state.cisco.up = 1;
  164. }
  165. }
  166. dev_kfree_skb_any(skb);
  167. return NET_RX_SUCCESS;
  168. } /* switch(keepalive type) */
  169. } /* switch(protocol) */
  170. printk(KERN_INFO "%s: Unsupported protocol %x\n", dev->name,
  171. data->protocol);
  172. dev_kfree_skb_any(skb);
  173. return NET_RX_DROP;
  174. rx_error:
  175. hdlc->stats.rx_errors++; /* Mark error */
  176. dev_kfree_skb_any(skb);
  177. return NET_RX_DROP;
  178. }
  179. static void cisco_timer(unsigned long arg)
  180. {
  181. struct net_device *dev = (struct net_device *)arg;
  182. hdlc_device *hdlc = dev_to_hdlc(dev);
  183. if (hdlc->state.cisco.up &&
  184. time_after(jiffies, hdlc->state.cisco.last_poll +
  185. hdlc->state.cisco.settings.timeout * HZ)) {
  186. hdlc->state.cisco.up = 0;
  187. printk(KERN_INFO "%s: Link down\n", dev->name);
  188. netif_carrier_off(dev);
  189. }
  190. cisco_keepalive_send(dev, CISCO_KEEPALIVE_REQ,
  191. ++hdlc->state.cisco.txseq,
  192. hdlc->state.cisco.rxseq);
  193. hdlc->state.cisco.request_sent = 1;
  194. hdlc->state.cisco.timer.expires = jiffies +
  195. hdlc->state.cisco.settings.interval * HZ;
  196. hdlc->state.cisco.timer.function = cisco_timer;
  197. hdlc->state.cisco.timer.data = arg;
  198. add_timer(&hdlc->state.cisco.timer);
  199. }
  200. static void cisco_start(struct net_device *dev)
  201. {
  202. hdlc_device *hdlc = dev_to_hdlc(dev);
  203. hdlc->state.cisco.up = 0;
  204. hdlc->state.cisco.request_sent = 0;
  205. hdlc->state.cisco.txseq = hdlc->state.cisco.rxseq = 0;
  206. init_timer(&hdlc->state.cisco.timer);
  207. hdlc->state.cisco.timer.expires = jiffies + HZ; /*First poll after 1s*/
  208. hdlc->state.cisco.timer.function = cisco_timer;
  209. hdlc->state.cisco.timer.data = (unsigned long)dev;
  210. add_timer(&hdlc->state.cisco.timer);
  211. }
  212. static void cisco_stop(struct net_device *dev)
  213. {
  214. hdlc_device *hdlc = dev_to_hdlc(dev);
  215. del_timer_sync(&hdlc->state.cisco.timer);
  216. if (netif_carrier_ok(dev))
  217. netif_carrier_off(dev);
  218. hdlc->state.cisco.up = 0;
  219. hdlc->state.cisco.request_sent = 0;
  220. }
  221. int hdlc_cisco_ioctl(struct net_device *dev, struct ifreq *ifr)
  222. {
  223. cisco_proto __user *cisco_s = ifr->ifr_settings.ifs_ifsu.cisco;
  224. const size_t size = sizeof(cisco_proto);
  225. cisco_proto new_settings;
  226. hdlc_device *hdlc = dev_to_hdlc(dev);
  227. int result;
  228. switch (ifr->ifr_settings.type) {
  229. case IF_GET_PROTO:
  230. ifr->ifr_settings.type = IF_PROTO_CISCO;
  231. if (ifr->ifr_settings.size < size) {
  232. ifr->ifr_settings.size = size; /* data size wanted */
  233. return -ENOBUFS;
  234. }
  235. if (copy_to_user(cisco_s, &hdlc->state.cisco.settings, size))
  236. return -EFAULT;
  237. return 0;
  238. case IF_PROTO_CISCO:
  239. if(!capable(CAP_NET_ADMIN))
  240. return -EPERM;
  241. if(dev->flags & IFF_UP)
  242. return -EBUSY;
  243. if (copy_from_user(&new_settings, cisco_s, size))
  244. return -EFAULT;
  245. if (new_settings.interval < 1 ||
  246. new_settings.timeout < 2)
  247. return -EINVAL;
  248. result=hdlc->attach(dev, ENCODING_NRZ,PARITY_CRC16_PR1_CCITT);
  249. if (result)
  250. return result;
  251. hdlc_proto_detach(hdlc);
  252. memcpy(&hdlc->state.cisco.settings, &new_settings, size);
  253. memset(&hdlc->proto, 0, sizeof(hdlc->proto));
  254. hdlc->proto.start = cisco_start;
  255. hdlc->proto.stop = cisco_stop;
  256. hdlc->proto.netif_rx = cisco_rx;
  257. hdlc->proto.type_trans = cisco_type_trans;
  258. hdlc->proto.id = IF_PROTO_CISCO;
  259. dev->hard_start_xmit = hdlc->xmit;
  260. dev->hard_header = cisco_hard_header;
  261. dev->hard_header_cache = NULL;
  262. dev->type = ARPHRD_CISCO;
  263. dev->flags = IFF_POINTOPOINT | IFF_NOARP;
  264. dev->addr_len = 0;
  265. return 0;
  266. }
  267. return -EINVAL;
  268. }