hdlc_cisco.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Generic HDLC support routines for Linux
  4. * Cisco HDLC support
  5. *
  6. * Copyright (C) 2000 - 2006 Krzysztof Halasa <khc@pm.waw.pl>
  7. */
  8. #include <linux/errno.h>
  9. #include <linux/hdlc.h>
  10. #include <linux/if_arp.h>
  11. #include <linux/inetdevice.h>
  12. #include <linux/init.h>
  13. #include <linux/kernel.h>
  14. #include <linux/module.h>
  15. #include <linux/pkt_sched.h>
  16. #include <linux/poll.h>
  17. #include <linux/rtnetlink.h>
  18. #include <linux/skbuff.h>
  19. #undef DEBUG_HARD_HEADER
  20. #define CISCO_MULTICAST 0x8F /* Cisco multicast address */
  21. #define CISCO_UNICAST 0x0F /* Cisco unicast address */
  22. #define CISCO_KEEPALIVE 0x8035 /* Cisco keepalive protocol */
  23. #define CISCO_SYS_INFO 0x2000 /* Cisco interface/system info */
  24. #define CISCO_ADDR_REQ 0 /* Cisco address request */
  25. #define CISCO_ADDR_REPLY 1 /* Cisco address reply */
  26. #define CISCO_KEEPALIVE_REQ 2 /* Cisco keepalive request */
  27. struct hdlc_header {
  28. u8 address;
  29. u8 control;
  30. __be16 protocol;
  31. }__packed;
  32. struct cisco_packet {
  33. __be32 type; /* code */
  34. __be32 par1;
  35. __be32 par2;
  36. __be16 rel; /* reliability */
  37. __be32 time;
  38. }__packed;
  39. #define CISCO_PACKET_LEN 18
  40. #define CISCO_BIG_PACKET_LEN 20
  41. struct cisco_state {
  42. cisco_proto settings;
  43. struct timer_list timer;
  44. struct net_device *dev;
  45. spinlock_t lock;
  46. unsigned long last_poll;
  47. int up;
  48. u32 txseq; /* TX sequence number, 0 = none */
  49. u32 rxseq; /* RX sequence number */
  50. };
  51. static int cisco_ioctl(struct net_device *dev, struct ifreq *ifr);
  52. static inline struct cisco_state* state(hdlc_device *hdlc)
  53. {
  54. return (struct cisco_state *)hdlc->state;
  55. }
  56. static int cisco_hard_header(struct sk_buff *skb, struct net_device *dev,
  57. u16 type, const void *daddr, const void *saddr,
  58. unsigned int len)
  59. {
  60. struct hdlc_header *data;
  61. #ifdef DEBUG_HARD_HEADER
  62. printk(KERN_DEBUG "%s: cisco_hard_header called\n", dev->name);
  63. #endif
  64. skb_push(skb, sizeof(struct hdlc_header));
  65. data = (struct hdlc_header*)skb->data;
  66. if (type == CISCO_KEEPALIVE)
  67. data->address = CISCO_MULTICAST;
  68. else
  69. data->address = CISCO_UNICAST;
  70. data->control = 0;
  71. data->protocol = htons(type);
  72. return sizeof(struct hdlc_header);
  73. }
  74. static void cisco_keepalive_send(struct net_device *dev, u32 type,
  75. __be32 par1, __be32 par2)
  76. {
  77. struct sk_buff *skb;
  78. struct cisco_packet *data;
  79. skb = dev_alloc_skb(sizeof(struct hdlc_header) +
  80. sizeof(struct cisco_packet));
  81. if (!skb) {
  82. netdev_warn(dev, "Memory squeeze on cisco_keepalive_send()\n");
  83. return;
  84. }
  85. skb_reserve(skb, 4);
  86. cisco_hard_header(skb, dev, CISCO_KEEPALIVE, NULL, NULL, 0);
  87. data = (struct cisco_packet*)(skb->data + 4);
  88. data->type = htonl(type);
  89. data->par1 = par1;
  90. data->par2 = par2;
  91. data->rel = cpu_to_be16(0xFFFF);
  92. /* we will need do_div here if 1000 % HZ != 0 */
  93. data->time = htonl((jiffies - INITIAL_JIFFIES) * (1000 / HZ));
  94. skb_put(skb, sizeof(struct cisco_packet));
  95. skb->priority = TC_PRIO_CONTROL;
  96. skb->dev = dev;
  97. skb->protocol = htons(ETH_P_HDLC);
  98. skb_reset_network_header(skb);
  99. dev_queue_xmit(skb);
  100. }
  101. static __be16 cisco_type_trans(struct sk_buff *skb, struct net_device *dev)
  102. {
  103. struct hdlc_header *data = (struct hdlc_header*)skb->data;
  104. if (skb->len < sizeof(struct hdlc_header))
  105. return cpu_to_be16(ETH_P_HDLC);
  106. if (data->address != CISCO_MULTICAST &&
  107. data->address != CISCO_UNICAST)
  108. return cpu_to_be16(ETH_P_HDLC);
  109. switch (data->protocol) {
  110. case cpu_to_be16(ETH_P_IP):
  111. case cpu_to_be16(ETH_P_IPX):
  112. case cpu_to_be16(ETH_P_IPV6):
  113. skb_pull(skb, sizeof(struct hdlc_header));
  114. return data->protocol;
  115. default:
  116. return cpu_to_be16(ETH_P_HDLC);
  117. }
  118. }
  119. static int cisco_rx(struct sk_buff *skb)
  120. {
  121. struct net_device *dev = skb->dev;
  122. hdlc_device *hdlc = dev_to_hdlc(dev);
  123. struct cisco_state *st = state(hdlc);
  124. struct hdlc_header *data = (struct hdlc_header*)skb->data;
  125. struct cisco_packet *cisco_data;
  126. struct in_device *in_dev;
  127. __be32 addr, mask;
  128. u32 ack;
  129. if (skb->len < sizeof(struct hdlc_header))
  130. goto rx_error;
  131. if (data->address != CISCO_MULTICAST &&
  132. data->address != CISCO_UNICAST)
  133. goto rx_error;
  134. switch (ntohs(data->protocol)) {
  135. case CISCO_SYS_INFO:
  136. /* Packet is not needed, drop it. */
  137. dev_kfree_skb_any(skb);
  138. return NET_RX_SUCCESS;
  139. case CISCO_KEEPALIVE:
  140. if ((skb->len != sizeof(struct hdlc_header) +
  141. CISCO_PACKET_LEN) &&
  142. (skb->len != sizeof(struct hdlc_header) +
  143. CISCO_BIG_PACKET_LEN)) {
  144. netdev_info(dev, "Invalid length of Cisco control packet (%d bytes)\n",
  145. skb->len);
  146. goto rx_error;
  147. }
  148. cisco_data = (struct cisco_packet*)(skb->data + sizeof
  149. (struct hdlc_header));
  150. switch (ntohl (cisco_data->type)) {
  151. case CISCO_ADDR_REQ: /* Stolen from syncppp.c :-) */
  152. rcu_read_lock();
  153. in_dev = __in_dev_get_rcu(dev);
  154. addr = 0;
  155. mask = ~cpu_to_be32(0); /* is the mask correct? */
  156. if (in_dev != NULL) {
  157. const struct in_ifaddr *ifa;
  158. in_dev_for_each_ifa_rcu(ifa, in_dev) {
  159. if (strcmp(dev->name,
  160. ifa->ifa_label) == 0) {
  161. addr = ifa->ifa_local;
  162. mask = ifa->ifa_mask;
  163. break;
  164. }
  165. }
  166. cisco_keepalive_send(dev, CISCO_ADDR_REPLY,
  167. addr, mask);
  168. }
  169. rcu_read_unlock();
  170. dev_kfree_skb_any(skb);
  171. return NET_RX_SUCCESS;
  172. case CISCO_ADDR_REPLY:
  173. netdev_info(dev, "Unexpected Cisco IP address reply\n");
  174. goto rx_error;
  175. case CISCO_KEEPALIVE_REQ:
  176. spin_lock(&st->lock);
  177. st->rxseq = ntohl(cisco_data->par1);
  178. ack = ntohl(cisco_data->par2);
  179. if (ack && (ack == st->txseq ||
  180. /* our current REQ may be in transit */
  181. ack == st->txseq - 1)) {
  182. st->last_poll = jiffies;
  183. if (!st->up) {
  184. u32 sec, min, hrs, days;
  185. sec = ntohl(cisco_data->time) / 1000;
  186. min = sec / 60; sec -= min * 60;
  187. hrs = min / 60; min -= hrs * 60;
  188. days = hrs / 24; hrs -= days * 24;
  189. netdev_info(dev, "Link up (peer uptime %ud%uh%um%us)\n",
  190. days, hrs, min, sec);
  191. netif_dormant_off(dev);
  192. st->up = 1;
  193. }
  194. }
  195. spin_unlock(&st->lock);
  196. dev_kfree_skb_any(skb);
  197. return NET_RX_SUCCESS;
  198. } /* switch (keepalive type) */
  199. } /* switch (protocol) */
  200. netdev_info(dev, "Unsupported protocol %x\n", ntohs(data->protocol));
  201. dev_kfree_skb_any(skb);
  202. return NET_RX_DROP;
  203. rx_error:
  204. dev->stats.rx_errors++; /* Mark error */
  205. dev_kfree_skb_any(skb);
  206. return NET_RX_DROP;
  207. }
  208. static void cisco_timer(struct timer_list *t)
  209. {
  210. struct cisco_state *st = from_timer(st, t, timer);
  211. struct net_device *dev = st->dev;
  212. spin_lock(&st->lock);
  213. if (st->up &&
  214. time_after(jiffies, st->last_poll + st->settings.timeout * HZ)) {
  215. st->up = 0;
  216. netdev_info(dev, "Link down\n");
  217. netif_dormant_on(dev);
  218. }
  219. cisco_keepalive_send(dev, CISCO_KEEPALIVE_REQ, htonl(++st->txseq),
  220. htonl(st->rxseq));
  221. spin_unlock(&st->lock);
  222. st->timer.expires = jiffies + st->settings.interval * HZ;
  223. add_timer(&st->timer);
  224. }
  225. static void cisco_start(struct net_device *dev)
  226. {
  227. hdlc_device *hdlc = dev_to_hdlc(dev);
  228. struct cisco_state *st = state(hdlc);
  229. unsigned long flags;
  230. spin_lock_irqsave(&st->lock, flags);
  231. st->up = st->txseq = st->rxseq = 0;
  232. spin_unlock_irqrestore(&st->lock, flags);
  233. st->dev = dev;
  234. timer_setup(&st->timer, cisco_timer, 0);
  235. st->timer.expires = jiffies + HZ; /* First poll after 1 s */
  236. add_timer(&st->timer);
  237. }
  238. static void cisco_stop(struct net_device *dev)
  239. {
  240. hdlc_device *hdlc = dev_to_hdlc(dev);
  241. struct cisco_state *st = state(hdlc);
  242. unsigned long flags;
  243. del_timer_sync(&st->timer);
  244. spin_lock_irqsave(&st->lock, flags);
  245. netif_dormant_on(dev);
  246. st->up = st->txseq = 0;
  247. spin_unlock_irqrestore(&st->lock, flags);
  248. }
  249. static struct hdlc_proto proto = {
  250. .start = cisco_start,
  251. .stop = cisco_stop,
  252. .type_trans = cisco_type_trans,
  253. .ioctl = cisco_ioctl,
  254. .netif_rx = cisco_rx,
  255. .module = THIS_MODULE,
  256. };
  257. static const struct header_ops cisco_header_ops = {
  258. .create = cisco_hard_header,
  259. };
  260. static int cisco_ioctl(struct net_device *dev, struct ifreq *ifr)
  261. {
  262. cisco_proto __user *cisco_s = ifr->ifr_settings.ifs_ifsu.cisco;
  263. const size_t size = sizeof(cisco_proto);
  264. cisco_proto new_settings;
  265. hdlc_device *hdlc = dev_to_hdlc(dev);
  266. int result;
  267. switch (ifr->ifr_settings.type) {
  268. case IF_GET_PROTO:
  269. if (dev_to_hdlc(dev)->proto != &proto)
  270. return -EINVAL;
  271. ifr->ifr_settings.type = IF_PROTO_CISCO;
  272. if (ifr->ifr_settings.size < size) {
  273. ifr->ifr_settings.size = size; /* data size wanted */
  274. return -ENOBUFS;
  275. }
  276. if (copy_to_user(cisco_s, &state(hdlc)->settings, size))
  277. return -EFAULT;
  278. return 0;
  279. case IF_PROTO_CISCO:
  280. if (!capable(CAP_NET_ADMIN))
  281. return -EPERM;
  282. if (dev->flags & IFF_UP)
  283. return -EBUSY;
  284. if (copy_from_user(&new_settings, cisco_s, size))
  285. return -EFAULT;
  286. if (new_settings.interval < 1 ||
  287. new_settings.timeout < 2)
  288. return -EINVAL;
  289. result = hdlc->attach(dev, ENCODING_NRZ,PARITY_CRC16_PR1_CCITT);
  290. if (result)
  291. return result;
  292. result = attach_hdlc_protocol(dev, &proto,
  293. sizeof(struct cisco_state));
  294. if (result)
  295. return result;
  296. memcpy(&state(hdlc)->settings, &new_settings, size);
  297. spin_lock_init(&state(hdlc)->lock);
  298. dev->header_ops = &cisco_header_ops;
  299. dev->hard_header_len = sizeof(struct hdlc_header);
  300. dev->type = ARPHRD_CISCO;
  301. call_netdevice_notifiers(NETDEV_POST_TYPE_CHANGE, dev);
  302. netif_dormant_on(dev);
  303. return 0;
  304. }
  305. return -EINVAL;
  306. }
  307. static int __init mod_init(void)
  308. {
  309. register_hdlc_protocol(&proto);
  310. return 0;
  311. }
  312. static void __exit mod_exit(void)
  313. {
  314. unregister_hdlc_protocol(&proto);
  315. }
  316. module_init(mod_init);
  317. module_exit(mod_exit);
  318. MODULE_AUTHOR("Krzysztof Halasa <khc@pm.waw.pl>");
  319. MODULE_DESCRIPTION("Cisco HDLC protocol support for generic HDLC");
  320. MODULE_LICENSE("GPL v2");