blob: 849819c2552d3257b4872a87cc9376c2839b7f5b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Generic HDLC support routines for Linux
3 * Cisco HDLC support
4 *
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +02005 * Copyright (C) 2000 - 2006 Krzysztof Halasa <khc@pm.waw.pl>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
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
12#include <linux/module.h>
13#include <linux/kernel.h>
14#include <linux/slab.h>
15#include <linux/poll.h>
16#include <linux/errno.h>
17#include <linux/if_arp.h>
18#include <linux/init.h>
19#include <linux/skbuff.h>
20#include <linux/pkt_sched.h>
21#include <linux/inetdevice.h>
22#include <linux/lapb.h>
23#include <linux/rtnetlink.h>
24#include <linux/hdlc.h>
25
26#undef DEBUG_HARD_HEADER
27
28#define CISCO_MULTICAST 0x8F /* Cisco multicast address */
29#define CISCO_UNICAST 0x0F /* Cisco unicast address */
30#define CISCO_KEEPALIVE 0x8035 /* Cisco keepalive protocol */
31#define CISCO_SYS_INFO 0x2000 /* Cisco interface/system info */
32#define CISCO_ADDR_REQ 0 /* Cisco address request */
33#define CISCO_ADDR_REPLY 1 /* Cisco address reply */
34#define CISCO_KEEPALIVE_REQ 2 /* Cisco keepalive request */
35
36
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +020037struct hdlc_header {
38 u8 address;
39 u8 control;
Krzysztof Halasaabf17ff2007-04-27 13:13:33 +020040 __be16 protocol;
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +020041}__attribute__ ((packed));
42
43
44struct cisco_packet {
Krzysztof Halasaabf17ff2007-04-27 13:13:33 +020045 __be32 type; /* code */
46 __be32 par1;
47 __be32 par2;
48 __be16 rel; /* reliability */
49 __be32 time;
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +020050}__attribute__ ((packed));
51#define CISCO_PACKET_LEN 18
52#define CISCO_BIG_PACKET_LEN 20
53
54
55struct cisco_state {
56 cisco_proto settings;
57
58 struct timer_list timer;
Krzysztof Halasaaff26e22008-05-19 19:11:08 +020059 spinlock_t lock;
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +020060 unsigned long last_poll;
61 int up;
62 int request_sent;
63 u32 txseq; /* TX sequence number */
64 u32 rxseq; /* RX sequence number */
65};
66
67
68static int cisco_ioctl(struct net_device *dev, struct ifreq *ifr);
69
70
71static inline struct cisco_state * state(hdlc_device *hdlc)
72{
73 return(struct cisco_state *)(hdlc->state);
74}
75
76
Linus Torvalds1da177e2005-04-16 15:20:36 -070077static int cisco_hard_header(struct sk_buff *skb, struct net_device *dev,
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -070078 u16 type, const void *daddr, const void *saddr,
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 unsigned int len)
80{
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +020081 struct hdlc_header *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082#ifdef DEBUG_HARD_HEADER
83 printk(KERN_DEBUG "%s: cisco_hard_header called\n", dev->name);
84#endif
85
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +020086 skb_push(skb, sizeof(struct hdlc_header));
87 data = (struct hdlc_header*)skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 if (type == CISCO_KEEPALIVE)
89 data->address = CISCO_MULTICAST;
90 else
91 data->address = CISCO_UNICAST;
92 data->control = 0;
93 data->protocol = htons(type);
94
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +020095 return sizeof(struct hdlc_header);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096}
97
98
99
100static void cisco_keepalive_send(struct net_device *dev, u32 type,
Krzysztof Halasaabf17ff2007-04-27 13:13:33 +0200101 __be32 par1, __be32 par2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102{
103 struct sk_buff *skb;
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200104 struct cisco_packet *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200106 skb = dev_alloc_skb(sizeof(struct hdlc_header) +
107 sizeof(struct cisco_packet));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 if (!skb) {
109 printk(KERN_WARNING
110 "%s: Memory squeeze on cisco_keepalive_send()\n",
111 dev->name);
112 return;
113 }
114 skb_reserve(skb, 4);
115 cisco_hard_header(skb, dev, CISCO_KEEPALIVE, NULL, NULL, 0);
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200116 data = (struct cisco_packet*)(skb->data + 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
118 data->type = htonl(type);
Krzysztof Halasaabf17ff2007-04-27 13:13:33 +0200119 data->par1 = par1;
120 data->par2 = par2;
121 data->rel = __constant_htons(0xFFFF);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 /* we will need do_div here if 1000 % HZ != 0 */
123 data->time = htonl((jiffies - INITIAL_JIFFIES) * (1000 / HZ));
124
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200125 skb_put(skb, sizeof(struct cisco_packet));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 skb->priority = TC_PRIO_CONTROL;
127 skb->dev = dev;
Arnaldo Carvalho de Meloc1d2bbe2007-04-10 20:45:18 -0700128 skb_reset_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
130 dev_queue_xmit(skb);
131}
132
133
134
Alexey Dobriyanab611482005-07-12 12:08:43 -0700135static __be16 cisco_type_trans(struct sk_buff *skb, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136{
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200137 struct hdlc_header *data = (struct hdlc_header*)skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200139 if (skb->len < sizeof(struct hdlc_header))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 return __constant_htons(ETH_P_HDLC);
141
142 if (data->address != CISCO_MULTICAST &&
143 data->address != CISCO_UNICAST)
144 return __constant_htons(ETH_P_HDLC);
145
146 switch(data->protocol) {
147 case __constant_htons(ETH_P_IP):
148 case __constant_htons(ETH_P_IPX):
149 case __constant_htons(ETH_P_IPV6):
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200150 skb_pull(skb, sizeof(struct hdlc_header));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 return data->protocol;
152 default:
153 return __constant_htons(ETH_P_HDLC);
154 }
155}
156
157
158static int cisco_rx(struct sk_buff *skb)
159{
160 struct net_device *dev = skb->dev;
161 hdlc_device *hdlc = dev_to_hdlc(dev);
Krzysztof Halasaaff26e22008-05-19 19:11:08 +0200162 struct cisco_state *st = state(hdlc);
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200163 struct hdlc_header *data = (struct hdlc_header*)skb->data;
164 struct cisco_packet *cisco_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 struct in_device *in_dev;
Al Viroa144ea42006-09-28 18:00:55 -0700166 __be32 addr, mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200168 if (skb->len < sizeof(struct hdlc_header))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 goto rx_error;
170
171 if (data->address != CISCO_MULTICAST &&
172 data->address != CISCO_UNICAST)
173 goto rx_error;
174
175 switch(ntohs(data->protocol)) {
176 case CISCO_SYS_INFO:
177 /* Packet is not needed, drop it. */
178 dev_kfree_skb_any(skb);
179 return NET_RX_SUCCESS;
180
181 case CISCO_KEEPALIVE:
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200182 if ((skb->len != sizeof(struct hdlc_header) +
183 CISCO_PACKET_LEN) &&
184 (skb->len != sizeof(struct hdlc_header) +
185 CISCO_BIG_PACKET_LEN)) {
186 printk(KERN_INFO "%s: Invalid length of Cisco control"
187 " packet (%d bytes)\n", dev->name, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 goto rx_error;
189 }
190
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200191 cisco_data = (struct cisco_packet*)(skb->data + sizeof
192 (struct hdlc_header));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
194 switch(ntohl (cisco_data->type)) {
195 case CISCO_ADDR_REQ: /* Stolen from syncppp.c :-) */
196 in_dev = dev->ip_ptr;
197 addr = 0;
Krzysztof Halasaabf17ff2007-04-27 13:13:33 +0200198 mask = __constant_htonl(~0); /* is the mask correct? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
200 if (in_dev != NULL) {
201 struct in_ifaddr **ifap = &in_dev->ifa_list;
202
203 while (*ifap != NULL) {
204 if (strcmp(dev->name,
205 (*ifap)->ifa_label) == 0) {
206 addr = (*ifap)->ifa_local;
207 mask = (*ifap)->ifa_mask;
208 break;
209 }
210 ifap = &(*ifap)->ifa_next;
211 }
212
213 cisco_keepalive_send(dev, CISCO_ADDR_REPLY,
214 addr, mask);
215 }
216 dev_kfree_skb_any(skb);
217 return NET_RX_SUCCESS;
218
219 case CISCO_ADDR_REPLY:
220 printk(KERN_INFO "%s: Unexpected Cisco IP address "
221 "reply\n", dev->name);
222 goto rx_error;
223
224 case CISCO_KEEPALIVE_REQ:
Krzysztof Halasaaff26e22008-05-19 19:11:08 +0200225 spin_lock(&st->lock);
226 st->rxseq = ntohl(cisco_data->par1);
227 if (st->request_sent &&
228 ntohl(cisco_data->par2) == st->txseq) {
229 st->last_poll = jiffies;
230 if (!st->up) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 u32 sec, min, hrs, days;
232 sec = ntohl(cisco_data->time) / 1000;
233 min = sec / 60; sec -= min * 60;
234 hrs = min / 60; min -= hrs * 60;
235 days = hrs / 24; hrs -= days * 24;
236 printk(KERN_INFO "%s: Link up (peer "
237 "uptime %ud%uh%um%us)\n",
Krzysztof Halasaaff26e22008-05-19 19:11:08 +0200238 dev->name, days, hrs, min, sec);
Krzysztof Halasac2ce9202006-07-12 13:46:12 -0700239 netif_dormant_off(dev);
Krzysztof Halasaaff26e22008-05-19 19:11:08 +0200240 st->up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 }
242 }
Krzysztof Halasaaff26e22008-05-19 19:11:08 +0200243 spin_unlock(&st->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
245 dev_kfree_skb_any(skb);
246 return NET_RX_SUCCESS;
247 } /* switch(keepalive type) */
248 } /* switch(protocol) */
249
250 printk(KERN_INFO "%s: Unsupported protocol %x\n", dev->name,
Krzysztof Halasaabf17ff2007-04-27 13:13:33 +0200251 ntohs(data->protocol));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 dev_kfree_skb_any(skb);
253 return NET_RX_DROP;
254
Krzysztof Halasa198191c2008-06-30 23:26:53 +0200255rx_error:
256 dev->stats.rx_errors++; /* Mark error */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 dev_kfree_skb_any(skb);
258 return NET_RX_DROP;
259}
260
261
262
263static void cisco_timer(unsigned long arg)
264{
265 struct net_device *dev = (struct net_device *)arg;
266 hdlc_device *hdlc = dev_to_hdlc(dev);
Krzysztof Halasaaff26e22008-05-19 19:11:08 +0200267 struct cisco_state *st = state(hdlc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
Krzysztof Halasaaff26e22008-05-19 19:11:08 +0200269 spin_lock(&st->lock);
270 if (st->up &&
271 time_after(jiffies, st->last_poll + st->settings.timeout * HZ)) {
272 st->up = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 printk(KERN_INFO "%s: Link down\n", dev->name);
Krzysztof Halasac2ce9202006-07-12 13:46:12 -0700274 netif_dormant_on(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 }
276
Krzysztof Halasaaff26e22008-05-19 19:11:08 +0200277 cisco_keepalive_send(dev, CISCO_KEEPALIVE_REQ, htonl(++st->txseq),
278 htonl(st->rxseq));
279 st->request_sent = 1;
280 spin_unlock(&st->lock);
281
282 st->timer.expires = jiffies + st->settings.interval * HZ;
283 st->timer.function = cisco_timer;
284 st->timer.data = arg;
285 add_timer(&st->timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286}
287
288
289
290static void cisco_start(struct net_device *dev)
291{
292 hdlc_device *hdlc = dev_to_hdlc(dev);
Krzysztof Halasaaff26e22008-05-19 19:11:08 +0200293 struct cisco_state *st = state(hdlc);
294 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295
Krzysztof Halasaaff26e22008-05-19 19:11:08 +0200296 spin_lock_irqsave(&st->lock, flags);
297 st->up = 0;
298 st->request_sent = 0;
299 st->txseq = st->rxseq = 0;
300 spin_unlock_irqrestore(&st->lock, flags);
301
302 init_timer(&st->timer);
303 st->timer.expires = jiffies + HZ; /* First poll after 1 s */
304 st->timer.function = cisco_timer;
305 st->timer.data = (unsigned long)dev;
306 add_timer(&st->timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307}
308
309
310
311static void cisco_stop(struct net_device *dev)
312{
313 hdlc_device *hdlc = dev_to_hdlc(dev);
Krzysztof Halasaaff26e22008-05-19 19:11:08 +0200314 struct cisco_state *st = state(hdlc);
315 unsigned long flags;
316
317 del_timer_sync(&st->timer);
318
319 spin_lock_irqsave(&st->lock, flags);
Krzysztof Halasac2ce9202006-07-12 13:46:12 -0700320 netif_dormant_on(dev);
Krzysztof Halasaaff26e22008-05-19 19:11:08 +0200321 st->up = 0;
322 st->request_sent = 0;
323 spin_unlock_irqrestore(&st->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324}
325
326
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200327static struct hdlc_proto proto = {
328 .start = cisco_start,
329 .stop = cisco_stop,
330 .type_trans = cisco_type_trans,
331 .ioctl = cisco_ioctl,
Krzysztof Halasa40d25142008-02-01 22:37:12 +0100332 .netif_rx = cisco_rx,
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200333 .module = THIS_MODULE,
334};
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -0700335
336static const struct header_ops cisco_header_ops = {
337 .create = cisco_hard_header,
338};
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200339
340static int cisco_ioctl(struct net_device *dev, struct ifreq *ifr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341{
342 cisco_proto __user *cisco_s = ifr->ifr_settings.ifs_ifsu.cisco;
343 const size_t size = sizeof(cisco_proto);
344 cisco_proto new_settings;
345 hdlc_device *hdlc = dev_to_hdlc(dev);
346 int result;
347
348 switch (ifr->ifr_settings.type) {
349 case IF_GET_PROTO:
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200350 if (dev_to_hdlc(dev)->proto != &proto)
351 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 ifr->ifr_settings.type = IF_PROTO_CISCO;
353 if (ifr->ifr_settings.size < size) {
354 ifr->ifr_settings.size = size; /* data size wanted */
355 return -ENOBUFS;
356 }
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200357 if (copy_to_user(cisco_s, &state(hdlc)->settings, size))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 return -EFAULT;
359 return 0;
360
361 case IF_PROTO_CISCO:
362 if(!capable(CAP_NET_ADMIN))
363 return -EPERM;
364
365 if(dev->flags & IFF_UP)
366 return -EBUSY;
367
368 if (copy_from_user(&new_settings, cisco_s, size))
369 return -EFAULT;
370
371 if (new_settings.interval < 1 ||
372 new_settings.timeout < 2)
373 return -EINVAL;
374
375 result=hdlc->attach(dev, ENCODING_NRZ,PARITY_CRC16_PR1_CCITT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 if (result)
377 return result;
378
Krzysztof Halasa40d25142008-02-01 22:37:12 +0100379 result = attach_hdlc_protocol(dev, &proto,
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200380 sizeof(struct cisco_state));
381 if (result)
382 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200384 memcpy(&state(hdlc)->settings, &new_settings, size);
Krzysztof Halasaaff26e22008-05-19 19:11:08 +0200385 spin_lock_init(&state(hdlc)->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 dev->hard_start_xmit = hdlc->xmit;
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -0700387 dev->header_ops = &cisco_header_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 dev->type = ARPHRD_CISCO;
Krzysztof Halasac2ce9202006-07-12 13:46:12 -0700389 netif_dormant_on(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 return 0;
391 }
392
393 return -EINVAL;
394}
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200395
396
397static int __init mod_init(void)
398{
399 register_hdlc_protocol(&proto);
400 return 0;
401}
402
403
404
405static void __exit mod_exit(void)
406{
407 unregister_hdlc_protocol(&proto);
408}
409
410
411module_init(mod_init);
412module_exit(mod_exit);
413
414MODULE_AUTHOR("Krzysztof Halasa <khc@pm.waw.pl>");
415MODULE_DESCRIPTION("Cisco HDLC protocol support for generic HDLC");
416MODULE_LICENSE("GPL v2");