hdlc_cisco.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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 + 4);
  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. #if 0
  163. netif_carrier_on(dev);
  164. #endif
  165. hdlc->state.cisco.up = 1;
  166. }
  167. }
  168. dev_kfree_skb_any(skb);
  169. return NET_RX_SUCCESS;
  170. } /* switch(keepalive type) */
  171. } /* switch(protocol) */
  172. printk(KERN_INFO "%s: Unsupported protocol %x\n", dev->name,
  173. data->protocol);
  174. dev_kfree_skb_any(skb);
  175. return NET_RX_DROP;
  176. rx_error:
  177. hdlc->stats.rx_errors++; /* Mark error */
  178. dev_kfree_skb_any(skb);
  179. return NET_RX_DROP;
  180. }
  181. static void cisco_timer(unsigned long arg)
  182. {
  183. struct net_device *dev = (struct net_device *)arg;
  184. hdlc_device *hdlc = dev_to_hdlc(dev);
  185. if (hdlc->state.cisco.up &&
  186. time_after(jiffies, hdlc->state.cisco.last_poll +
  187. hdlc->state.cisco.settings.timeout * HZ)) {
  188. hdlc->state.cisco.up = 0;
  189. printk(KERN_INFO "%s: Link down\n", dev->name);
  190. #if 0
  191. netif_carrier_off(dev);
  192. #endif
  193. }
  194. cisco_keepalive_send(dev, CISCO_KEEPALIVE_REQ,
  195. ++hdlc->state.cisco.txseq,
  196. hdlc->state.cisco.rxseq);
  197. hdlc->state.cisco.request_sent = 1;
  198. hdlc->state.cisco.timer.expires = jiffies +
  199. hdlc->state.cisco.settings.interval * HZ;
  200. hdlc->state.cisco.timer.function = cisco_timer;
  201. hdlc->state.cisco.timer.data = arg;
  202. add_timer(&hdlc->state.cisco.timer);
  203. }
  204. static void cisco_start(struct net_device *dev)
  205. {
  206. hdlc_device *hdlc = dev_to_hdlc(dev);
  207. hdlc->state.cisco.up = 0;
  208. hdlc->state.cisco.request_sent = 0;
  209. hdlc->state.cisco.txseq = hdlc->state.cisco.rxseq = 0;
  210. init_timer(&hdlc->state.cisco.timer);
  211. hdlc->state.cisco.timer.expires = jiffies + HZ; /*First poll after 1s*/
  212. hdlc->state.cisco.timer.function = cisco_timer;
  213. hdlc->state.cisco.timer.data = (unsigned long)dev;
  214. add_timer(&hdlc->state.cisco.timer);
  215. }
  216. static void cisco_stop(struct net_device *dev)
  217. {
  218. hdlc_device *hdlc = dev_to_hdlc(dev);
  219. del_timer_sync(&hdlc->state.cisco.timer);
  220. #if 0
  221. if (netif_carrier_ok(dev))
  222. netif_carrier_off(dev);
  223. #endif
  224. hdlc->state.cisco.up = 0;
  225. hdlc->state.cisco.request_sent = 0;
  226. }
  227. int hdlc_cisco_ioctl(struct net_device *dev, struct ifreq *ifr)
  228. {
  229. cisco_proto __user *cisco_s = ifr->ifr_settings.ifs_ifsu.cisco;
  230. const size_t size = sizeof(cisco_proto);
  231. cisco_proto new_settings;
  232. hdlc_device *hdlc = dev_to_hdlc(dev);
  233. int result;
  234. switch (ifr->ifr_settings.type) {
  235. case IF_GET_PROTO:
  236. ifr->ifr_settings.type = IF_PROTO_CISCO;
  237. if (ifr->ifr_settings.size < size) {
  238. ifr->ifr_settings.size = size; /* data size wanted */
  239. return -ENOBUFS;
  240. }
  241. if (copy_to_user(cisco_s, &hdlc->state.cisco.settings, size))
  242. return -EFAULT;
  243. return 0;
  244. case IF_PROTO_CISCO:
  245. if(!capable(CAP_NET_ADMIN))
  246. return -EPERM;
  247. if(dev->flags & IFF_UP)
  248. return -EBUSY;
  249. if (copy_from_user(&new_settings, cisco_s, size))
  250. return -EFAULT;
  251. if (new_settings.interval < 1 ||
  252. new_settings.timeout < 2)
  253. return -EINVAL;
  254. result=hdlc->attach(dev, ENCODING_NRZ,PARITY_CRC16_PR1_CCITT);
  255. if (result)
  256. return result;
  257. hdlc_proto_detach(hdlc);
  258. memcpy(&hdlc->state.cisco.settings, &new_settings, size);
  259. memset(&hdlc->proto, 0, sizeof(hdlc->proto));
  260. hdlc->proto.start = cisco_start;
  261. hdlc->proto.stop = cisco_stop;
  262. hdlc->proto.netif_rx = cisco_rx;
  263. hdlc->proto.type_trans = cisco_type_trans;
  264. hdlc->proto.id = IF_PROTO_CISCO;
  265. dev->hard_start_xmit = hdlc->xmit;
  266. dev->hard_header = cisco_hard_header;
  267. dev->hard_header_cache = NULL;
  268. dev->type = ARPHRD_CISCO;
  269. dev->flags = IFF_POINTOPOINT | IFF_NOARP;
  270. dev->addr_len = 0;
  271. return 0;
  272. }
  273. return -EINVAL;
  274. }