hdlc_cisco.c 8.4 KB

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