2 * Generic HDLC support routines for Linux
5 * Copyright (C) 2000 - 2006 Krzysztof Halasa <khc@pm.waw.pl>
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.
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>
26 #undef DEBUG_HARD_HEADER
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 */
41 }__attribute__ ((packed));
45 __be32 type; /* code */
48 __be16 rel; /* reliability */
50 }__attribute__ ((packed));
51 #define CISCO_PACKET_LEN 18
52 #define CISCO_BIG_PACKET_LEN 20
58 struct timer_list timer;
60 unsigned long last_poll;
63 u32 txseq; /* TX sequence number */
64 u32 rxseq; /* RX sequence number */
68 static int cisco_ioctl(struct net_device *dev, struct ifreq *ifr);
71 static inline struct cisco_state * state(hdlc_device *hdlc)
73 return(struct cisco_state *)(hdlc->state);
77 static int cisco_hard_header(struct sk_buff *skb, struct net_device *dev,
78 u16 type, const void *daddr, const void *saddr,
81 struct hdlc_header *data;
82 #ifdef DEBUG_HARD_HEADER
83 printk(KERN_DEBUG "%s: cisco_hard_header called\n", dev->name);
86 skb_push(skb, sizeof(struct hdlc_header));
87 data = (struct hdlc_header*)skb->data;
88 if (type == CISCO_KEEPALIVE)
89 data->address = CISCO_MULTICAST;
91 data->address = CISCO_UNICAST;
93 data->protocol = htons(type);
95 return sizeof(struct hdlc_header);
100 static void cisco_keepalive_send(struct net_device *dev, u32 type,
101 __be32 par1, __be32 par2)
104 struct cisco_packet *data;
106 skb = dev_alloc_skb(sizeof(struct hdlc_header) +
107 sizeof(struct cisco_packet));
110 "%s: Memory squeeze on cisco_keepalive_send()\n",
115 cisco_hard_header(skb, dev, CISCO_KEEPALIVE, NULL, NULL, 0);
116 data = (struct cisco_packet*)(skb->data + 4);
118 data->type = htonl(type);
121 data->rel = __constant_htons(0xFFFF);
122 /* we will need do_div here if 1000 % HZ != 0 */
123 data->time = htonl((jiffies - INITIAL_JIFFIES) * (1000 / HZ));
125 skb_put(skb, sizeof(struct cisco_packet));
126 skb->priority = TC_PRIO_CONTROL;
128 skb_reset_network_header(skb);
135 static __be16 cisco_type_trans(struct sk_buff *skb, struct net_device *dev)
137 struct hdlc_header *data = (struct hdlc_header*)skb->data;
139 if (skb->len < sizeof(struct hdlc_header))
140 return __constant_htons(ETH_P_HDLC);
142 if (data->address != CISCO_MULTICAST &&
143 data->address != CISCO_UNICAST)
144 return __constant_htons(ETH_P_HDLC);
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):
150 skb_pull(skb, sizeof(struct hdlc_header));
151 return data->protocol;
153 return __constant_htons(ETH_P_HDLC);
158 static int cisco_rx(struct sk_buff *skb)
160 struct net_device *dev = skb->dev;
161 hdlc_device *hdlc = dev_to_hdlc(dev);
162 struct cisco_state *st = state(hdlc);
163 struct hdlc_header *data = (struct hdlc_header*)skb->data;
164 struct cisco_packet *cisco_data;
165 struct in_device *in_dev;
168 if (skb->len < sizeof(struct hdlc_header))
171 if (data->address != CISCO_MULTICAST &&
172 data->address != CISCO_UNICAST)
175 switch(ntohs(data->protocol)) {
177 /* Packet is not needed, drop it. */
178 dev_kfree_skb_any(skb);
179 return NET_RX_SUCCESS;
181 case CISCO_KEEPALIVE:
182 if ((skb->len != sizeof(struct hdlc_header) +
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);
191 cisco_data = (struct cisco_packet*)(skb->data + sizeof
192 (struct hdlc_header));
194 switch(ntohl (cisco_data->type)) {
195 case CISCO_ADDR_REQ: /* Stolen from syncppp.c :-) */
196 in_dev = dev->ip_ptr;
198 mask = __constant_htonl(~0); /* is the mask correct? */
200 if (in_dev != NULL) {
201 struct in_ifaddr **ifap = &in_dev->ifa_list;
203 while (*ifap != NULL) {
204 if (strcmp(dev->name,
205 (*ifap)->ifa_label) == 0) {
206 addr = (*ifap)->ifa_local;
207 mask = (*ifap)->ifa_mask;
210 ifap = &(*ifap)->ifa_next;
213 cisco_keepalive_send(dev, CISCO_ADDR_REPLY,
216 dev_kfree_skb_any(skb);
217 return NET_RX_SUCCESS;
219 case CISCO_ADDR_REPLY:
220 printk(KERN_INFO "%s: Unexpected Cisco IP address "
221 "reply\n", dev->name);
224 case CISCO_KEEPALIVE_REQ:
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;
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",
238 dev->name, days, hrs, min, sec);
239 netif_dormant_off(dev);
243 spin_unlock(&st->lock);
245 dev_kfree_skb_any(skb);
246 return NET_RX_SUCCESS;
247 } /* switch(keepalive type) */
248 } /* switch(protocol) */
250 printk(KERN_INFO "%s: Unsupported protocol %x\n", dev->name,
251 ntohs(data->protocol));
252 dev_kfree_skb_any(skb);
256 dev->stats.rx_errors++; /* Mark error */
257 dev_kfree_skb_any(skb);
263 static void cisco_timer(unsigned long arg)
265 struct net_device *dev = (struct net_device *)arg;
266 hdlc_device *hdlc = dev_to_hdlc(dev);
267 struct cisco_state *st = state(hdlc);
269 spin_lock(&st->lock);
271 time_after(jiffies, st->last_poll + st->settings.timeout * HZ)) {
273 printk(KERN_INFO "%s: Link down\n", dev->name);
274 netif_dormant_on(dev);
277 cisco_keepalive_send(dev, CISCO_KEEPALIVE_REQ, htonl(++st->txseq),
279 st->request_sent = 1;
280 spin_unlock(&st->lock);
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);
290 static void cisco_start(struct net_device *dev)
292 hdlc_device *hdlc = dev_to_hdlc(dev);
293 struct cisco_state *st = state(hdlc);
296 spin_lock_irqsave(&st->lock, flags);
298 st->request_sent = 0;
299 st->txseq = st->rxseq = 0;
300 spin_unlock_irqrestore(&st->lock, flags);
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);
311 static void cisco_stop(struct net_device *dev)
313 hdlc_device *hdlc = dev_to_hdlc(dev);
314 struct cisco_state *st = state(hdlc);
317 del_timer_sync(&st->timer);
319 spin_lock_irqsave(&st->lock, flags);
320 netif_dormant_on(dev);
322 st->request_sent = 0;
323 spin_unlock_irqrestore(&st->lock, flags);
327 static struct hdlc_proto proto = {
328 .start = cisco_start,
330 .type_trans = cisco_type_trans,
331 .ioctl = cisco_ioctl,
332 .netif_rx = cisco_rx,
333 .module = THIS_MODULE,
336 static const struct header_ops cisco_header_ops = {
337 .create = cisco_hard_header,
340 static int cisco_ioctl(struct net_device *dev, struct ifreq *ifr)
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);
348 switch (ifr->ifr_settings.type) {
350 if (dev_to_hdlc(dev)->proto != &proto)
352 ifr->ifr_settings.type = IF_PROTO_CISCO;
353 if (ifr->ifr_settings.size < size) {
354 ifr->ifr_settings.size = size; /* data size wanted */
357 if (copy_to_user(cisco_s, &state(hdlc)->settings, size))
362 if(!capable(CAP_NET_ADMIN))
365 if(dev->flags & IFF_UP)
368 if (copy_from_user(&new_settings, cisco_s, size))
371 if (new_settings.interval < 1 ||
372 new_settings.timeout < 2)
375 result=hdlc->attach(dev, ENCODING_NRZ,PARITY_CRC16_PR1_CCITT);
379 result = attach_hdlc_protocol(dev, &proto,
380 sizeof(struct cisco_state));
384 memcpy(&state(hdlc)->settings, &new_settings, size);
385 spin_lock_init(&state(hdlc)->lock);
386 dev->hard_start_xmit = hdlc->xmit;
387 dev->header_ops = &cisco_header_ops;
388 dev->type = ARPHRD_CISCO;
389 netif_dormant_on(dev);
397 static int __init mod_init(void)
399 register_hdlc_protocol(&proto);
405 static void __exit mod_exit(void)
407 unregister_hdlc_protocol(&proto);
411 module_init(mod_init);
412 module_exit(mod_exit);
414 MODULE_AUTHOR("Krzysztof Halasa <khc@pm.waw.pl>");
415 MODULE_DESCRIPTION("Cisco HDLC protocol support for generic HDLC");
416 MODULE_LICENSE("GPL v2");