Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * Generic HDLC support routines for Linux | |
3 | * Cisco HDLC support | |
4 | * | |
eb2a2fd9 | 5 | * Copyright (C) 2000 - 2006 Krzysztof Halasa <khc@pm.waw.pl> |
1da177e4 LT |
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 | ||
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 | ||
eb2a2fd9 KH |
37 | struct hdlc_header { |
38 | u8 address; | |
39 | u8 control; | |
abf17ffd | 40 | __be16 protocol; |
eb2a2fd9 KH |
41 | }__attribute__ ((packed)); |
42 | ||
43 | ||
44 | struct cisco_packet { | |
abf17ffd KH |
45 | __be32 type; /* code */ |
46 | __be32 par1; | |
47 | __be32 par2; | |
48 | __be16 rel; /* reliability */ | |
49 | __be32 time; | |
eb2a2fd9 KH |
50 | }__attribute__ ((packed)); |
51 | #define CISCO_PACKET_LEN 18 | |
52 | #define CISCO_BIG_PACKET_LEN 20 | |
53 | ||
54 | ||
55 | struct cisco_state { | |
56 | cisco_proto settings; | |
57 | ||
58 | struct timer_list timer; | |
aff26e2f | 59 | spinlock_t lock; |
eb2a2fd9 KH |
60 | 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 | ||
68 | static int cisco_ioctl(struct net_device *dev, struct ifreq *ifr); | |
69 | ||
70 | ||
71 | static inline struct cisco_state * state(hdlc_device *hdlc) | |
72 | { | |
73 | return(struct cisco_state *)(hdlc->state); | |
74 | } | |
75 | ||
76 | ||
1da177e4 | 77 | static int cisco_hard_header(struct sk_buff *skb, struct net_device *dev, |
3b04ddde | 78 | u16 type, const void *daddr, const void *saddr, |
1da177e4 LT |
79 | unsigned int len) |
80 | { | |
eb2a2fd9 | 81 | struct hdlc_header *data; |
1da177e4 LT |
82 | #ifdef DEBUG_HARD_HEADER |
83 | printk(KERN_DEBUG "%s: cisco_hard_header called\n", dev->name); | |
84 | #endif | |
85 | ||
eb2a2fd9 KH |
86 | skb_push(skb, sizeof(struct hdlc_header)); |
87 | data = (struct hdlc_header*)skb->data; | |
1da177e4 LT |
88 | 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 | ||
eb2a2fd9 | 95 | return sizeof(struct hdlc_header); |
1da177e4 LT |
96 | } |
97 | ||
98 | ||
99 | ||
100 | static void cisco_keepalive_send(struct net_device *dev, u32 type, | |
abf17ffd | 101 | __be32 par1, __be32 par2) |
1da177e4 LT |
102 | { |
103 | struct sk_buff *skb; | |
eb2a2fd9 | 104 | struct cisco_packet *data; |
1da177e4 | 105 | |
eb2a2fd9 KH |
106 | skb = dev_alloc_skb(sizeof(struct hdlc_header) + |
107 | sizeof(struct cisco_packet)); | |
1da177e4 LT |
108 | 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); | |
eb2a2fd9 | 116 | data = (struct cisco_packet*)(skb->data + 4); |
1da177e4 LT |
117 | |
118 | data->type = htonl(type); | |
abf17ffd KH |
119 | data->par1 = par1; |
120 | data->par2 = par2; | |
121 | data->rel = __constant_htons(0xFFFF); | |
1da177e4 LT |
122 | /* we will need do_div here if 1000 % HZ != 0 */ |
123 | data->time = htonl((jiffies - INITIAL_JIFFIES) * (1000 / HZ)); | |
124 | ||
eb2a2fd9 | 125 | skb_put(skb, sizeof(struct cisco_packet)); |
1da177e4 LT |
126 | skb->priority = TC_PRIO_CONTROL; |
127 | skb->dev = dev; | |
c1d2bbe1 | 128 | skb_reset_network_header(skb); |
1da177e4 LT |
129 | |
130 | dev_queue_xmit(skb); | |
131 | } | |
132 | ||
133 | ||
134 | ||
ab611487 | 135 | static __be16 cisco_type_trans(struct sk_buff *skb, struct net_device *dev) |
1da177e4 | 136 | { |
eb2a2fd9 | 137 | struct hdlc_header *data = (struct hdlc_header*)skb->data; |
1da177e4 | 138 | |
eb2a2fd9 | 139 | if (skb->len < sizeof(struct hdlc_header)) |
1da177e4 LT |
140 | 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): | |
eb2a2fd9 | 150 | skb_pull(skb, sizeof(struct hdlc_header)); |
1da177e4 LT |
151 | return data->protocol; |
152 | default: | |
153 | return __constant_htons(ETH_P_HDLC); | |
154 | } | |
155 | } | |
156 | ||
157 | ||
158 | static int cisco_rx(struct sk_buff *skb) | |
159 | { | |
160 | struct net_device *dev = skb->dev; | |
161 | hdlc_device *hdlc = dev_to_hdlc(dev); | |
aff26e2f | 162 | struct cisco_state *st = state(hdlc); |
eb2a2fd9 KH |
163 | struct hdlc_header *data = (struct hdlc_header*)skb->data; |
164 | struct cisco_packet *cisco_data; | |
1da177e4 | 165 | struct in_device *in_dev; |
a144ea4b | 166 | __be32 addr, mask; |
1da177e4 | 167 | |
eb2a2fd9 | 168 | if (skb->len < sizeof(struct hdlc_header)) |
1da177e4 LT |
169 | 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: | |
eb2a2fd9 KH |
182 | 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); | |
1da177e4 LT |
188 | goto rx_error; |
189 | } | |
190 | ||
eb2a2fd9 KH |
191 | cisco_data = (struct cisco_packet*)(skb->data + sizeof |
192 | (struct hdlc_header)); | |
1da177e4 LT |
193 | |
194 | switch(ntohl (cisco_data->type)) { | |
195 | case CISCO_ADDR_REQ: /* Stolen from syncppp.c :-) */ | |
196 | in_dev = dev->ip_ptr; | |
197 | addr = 0; | |
abf17ffd | 198 | mask = __constant_htonl(~0); /* is the mask correct? */ |
1da177e4 LT |
199 | |
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: | |
aff26e2f KH |
225 | 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) { | |
1da177e4 LT |
231 | 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", | |
aff26e2f | 238 | dev->name, days, hrs, min, sec); |
c2ce9204 | 239 | netif_dormant_off(dev); |
aff26e2f | 240 | st->up = 1; |
1da177e4 LT |
241 | } |
242 | } | |
aff26e2f | 243 | spin_unlock(&st->lock); |
1da177e4 LT |
244 | |
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, | |
abf17ffd | 251 | ntohs(data->protocol)); |
1da177e4 LT |
252 | dev_kfree_skb_any(skb); |
253 | return NET_RX_DROP; | |
254 | ||
198191c4 KH |
255 | rx_error: |
256 | dev->stats.rx_errors++; /* Mark error */ | |
1da177e4 LT |
257 | dev_kfree_skb_any(skb); |
258 | return NET_RX_DROP; | |
259 | } | |
260 | ||
261 | ||
262 | ||
263 | static void cisco_timer(unsigned long arg) | |
264 | { | |
265 | struct net_device *dev = (struct net_device *)arg; | |
266 | hdlc_device *hdlc = dev_to_hdlc(dev); | |
aff26e2f | 267 | struct cisco_state *st = state(hdlc); |
1da177e4 | 268 | |
aff26e2f KH |
269 | spin_lock(&st->lock); |
270 | if (st->up && | |
271 | time_after(jiffies, st->last_poll + st->settings.timeout * HZ)) { | |
272 | st->up = 0; | |
1da177e4 | 273 | printk(KERN_INFO "%s: Link down\n", dev->name); |
c2ce9204 | 274 | netif_dormant_on(dev); |
1da177e4 LT |
275 | } |
276 | ||
aff26e2f KH |
277 | 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); | |
1da177e4 LT |
286 | } |
287 | ||
288 | ||
289 | ||
290 | static void cisco_start(struct net_device *dev) | |
291 | { | |
292 | hdlc_device *hdlc = dev_to_hdlc(dev); | |
aff26e2f KH |
293 | struct cisco_state *st = state(hdlc); |
294 | unsigned long flags; | |
295 | ||
296 | 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); | |
1da177e4 LT |
307 | } |
308 | ||
309 | ||
310 | ||
311 | static void cisco_stop(struct net_device *dev) | |
312 | { | |
313 | hdlc_device *hdlc = dev_to_hdlc(dev); | |
aff26e2f KH |
314 | 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); | |
c2ce9204 | 320 | netif_dormant_on(dev); |
aff26e2f KH |
321 | st->up = 0; |
322 | st->request_sent = 0; | |
323 | spin_unlock_irqrestore(&st->lock, flags); | |
1da177e4 LT |
324 | } |
325 | ||
326 | ||
eb2a2fd9 KH |
327 | static struct hdlc_proto proto = { |
328 | .start = cisco_start, | |
329 | .stop = cisco_stop, | |
330 | .type_trans = cisco_type_trans, | |
331 | .ioctl = cisco_ioctl, | |
40d25142 | 332 | .netif_rx = cisco_rx, |
eb2a2fd9 KH |
333 | .module = THIS_MODULE, |
334 | }; | |
3b04ddde SH |
335 | |
336 | static const struct header_ops cisco_header_ops = { | |
337 | .create = cisco_hard_header, | |
338 | }; | |
eb2a2fd9 KH |
339 | |
340 | static int cisco_ioctl(struct net_device *dev, struct ifreq *ifr) | |
1da177e4 LT |
341 | { |
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: | |
eb2a2fd9 KH |
350 | if (dev_to_hdlc(dev)->proto != &proto) |
351 | return -EINVAL; | |
1da177e4 LT |
352 | 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 | } | |
eb2a2fd9 | 357 | if (copy_to_user(cisco_s, &state(hdlc)->settings, size)) |
1da177e4 LT |
358 | 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); | |
1da177e4 LT |
376 | if (result) |
377 | return result; | |
378 | ||
40d25142 | 379 | result = attach_hdlc_protocol(dev, &proto, |
eb2a2fd9 KH |
380 | sizeof(struct cisco_state)); |
381 | if (result) | |
382 | return result; | |
1da177e4 | 383 | |
eb2a2fd9 | 384 | memcpy(&state(hdlc)->settings, &new_settings, size); |
aff26e2f | 385 | spin_lock_init(&state(hdlc)->lock); |
1da177e4 | 386 | dev->hard_start_xmit = hdlc->xmit; |
3b04ddde | 387 | dev->header_ops = &cisco_header_ops; |
1da177e4 | 388 | dev->type = ARPHRD_CISCO; |
c2ce9204 | 389 | netif_dormant_on(dev); |
1da177e4 LT |
390 | return 0; |
391 | } | |
392 | ||
393 | return -EINVAL; | |
394 | } | |
eb2a2fd9 KH |
395 | |
396 | ||
397 | static int __init mod_init(void) | |
398 | { | |
399 | register_hdlc_protocol(&proto); | |
400 | return 0; | |
401 | } | |
402 | ||
403 | ||
404 | ||
405 | static void __exit mod_exit(void) | |
406 | { | |
407 | unregister_hdlc_protocol(&proto); | |
408 | } | |
409 | ||
410 | ||
411 | module_init(mod_init); | |
412 | module_exit(mod_exit); | |
413 | ||
414 | MODULE_AUTHOR("Krzysztof Halasa <khc@pm.waw.pl>"); | |
415 | MODULE_DESCRIPTION("Cisco HDLC protocol support for generic HDLC"); | |
416 | MODULE_LICENSE("GPL v2"); |