2 * Generic HDLC support routines for Linux
5 * Copyright (C) 2000 - 2003 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 */
37 static int cisco_hard_header(struct sk_buff *skb, struct net_device *dev,
38 u16 type, void *daddr, void *saddr,
42 #ifdef DEBUG_HARD_HEADER
43 printk(KERN_DEBUG "%s: cisco_hard_header called\n", dev->name);
46 skb_push(skb, sizeof(hdlc_header));
47 data = (hdlc_header*)skb->data;
48 if (type == CISCO_KEEPALIVE)
49 data->address = CISCO_MULTICAST;
51 data->address = CISCO_UNICAST;
53 data->protocol = htons(type);
55 return sizeof(hdlc_header);
60 static void cisco_keepalive_send(struct net_device *dev, u32 type,
66 skb = dev_alloc_skb(sizeof(hdlc_header) + sizeof(cisco_packet));
69 "%s: Memory squeeze on cisco_keepalive_send()\n",
74 cisco_hard_header(skb, dev, CISCO_KEEPALIVE, NULL, NULL, 0);
75 data = (cisco_packet*)skb->data;
77 data->type = htonl(type);
78 data->par1 = htonl(par1);
79 data->par2 = htonl(par2);
81 /* we will need do_div here if 1000 % HZ != 0 */
82 data->time = htonl((jiffies - INITIAL_JIFFIES) * (1000 / HZ));
84 skb_put(skb, sizeof(cisco_packet));
85 skb->priority = TC_PRIO_CONTROL;
87 skb->nh.raw = skb->data;
94 static unsigned short cisco_type_trans(struct sk_buff *skb,
95 struct net_device *dev)
97 hdlc_header *data = (hdlc_header*)skb->data;
99 if (skb->len < sizeof(hdlc_header))
100 return __constant_htons(ETH_P_HDLC);
102 if (data->address != CISCO_MULTICAST &&
103 data->address != CISCO_UNICAST)
104 return __constant_htons(ETH_P_HDLC);
106 switch(data->protocol) {
107 case __constant_htons(ETH_P_IP):
108 case __constant_htons(ETH_P_IPX):
109 case __constant_htons(ETH_P_IPV6):
110 skb_pull(skb, sizeof(hdlc_header));
111 return data->protocol;
113 return __constant_htons(ETH_P_HDLC);
118 static int cisco_rx(struct sk_buff *skb)
120 struct net_device *dev = skb->dev;
121 hdlc_device *hdlc = dev_to_hdlc(dev);
122 hdlc_header *data = (hdlc_header*)skb->data;
123 cisco_packet *cisco_data;
124 struct in_device *in_dev;
127 if (skb->len < sizeof(hdlc_header))
130 if (data->address != CISCO_MULTICAST &&
131 data->address != CISCO_UNICAST)
134 switch(ntohs(data->protocol)) {
136 /* Packet is not needed, drop it. */
137 dev_kfree_skb_any(skb);
138 return NET_RX_SUCCESS;
140 case CISCO_KEEPALIVE:
141 if (skb->len != sizeof(hdlc_header) + CISCO_PACKET_LEN &&
142 skb->len != sizeof(hdlc_header) + CISCO_BIG_PACKET_LEN) {
143 printk(KERN_INFO "%s: Invalid length of Cisco "
144 "control packet (%d bytes)\n",
145 dev->name, skb->len);
149 cisco_data = (cisco_packet*)(skb->data + sizeof(hdlc_header));
151 switch(ntohl (cisco_data->type)) {
152 case CISCO_ADDR_REQ: /* Stolen from syncppp.c :-) */
153 in_dev = dev->ip_ptr;
155 mask = ~0; /* is the mask correct? */
157 if (in_dev != NULL) {
158 struct in_ifaddr **ifap = &in_dev->ifa_list;
160 while (*ifap != NULL) {
161 if (strcmp(dev->name,
162 (*ifap)->ifa_label) == 0) {
163 addr = (*ifap)->ifa_local;
164 mask = (*ifap)->ifa_mask;
167 ifap = &(*ifap)->ifa_next;
170 cisco_keepalive_send(dev, CISCO_ADDR_REPLY,
173 dev_kfree_skb_any(skb);
174 return NET_RX_SUCCESS;
176 case CISCO_ADDR_REPLY:
177 printk(KERN_INFO "%s: Unexpected Cisco IP address "
178 "reply\n", dev->name);
181 case CISCO_KEEPALIVE_REQ:
182 hdlc->state.cisco.rxseq = ntohl(cisco_data->par1);
183 if (hdlc->state.cisco.request_sent &&
184 ntohl(cisco_data->par2)==hdlc->state.cisco.txseq) {
185 hdlc->state.cisco.last_poll = jiffies;
186 if (!hdlc->state.cisco.up) {
187 u32 sec, min, hrs, days;
188 sec = ntohl(cisco_data->time) / 1000;
189 min = sec / 60; sec -= min * 60;
190 hrs = min / 60; min -= hrs * 60;
191 days = hrs / 24; hrs -= days * 24;
192 printk(KERN_INFO "%s: Link up (peer "
193 "uptime %ud%uh%um%us)\n",
194 dev->name, days, hrs,
196 netif_carrier_on(dev);
197 hdlc->state.cisco.up = 1;
201 dev_kfree_skb_any(skb);
202 return NET_RX_SUCCESS;
203 } /* switch(keepalive type) */
204 } /* switch(protocol) */
206 printk(KERN_INFO "%s: Unsupported protocol %x\n", dev->name,
208 dev_kfree_skb_any(skb);
212 hdlc->stats.rx_errors++; /* Mark error */
213 dev_kfree_skb_any(skb);
219 static void cisco_timer(unsigned long arg)
221 struct net_device *dev = (struct net_device *)arg;
222 hdlc_device *hdlc = dev_to_hdlc(dev);
224 if (hdlc->state.cisco.up &&
225 time_after(jiffies, hdlc->state.cisco.last_poll +
226 hdlc->state.cisco.settings.timeout * HZ)) {
227 hdlc->state.cisco.up = 0;
228 printk(KERN_INFO "%s: Link down\n", dev->name);
229 netif_carrier_off(dev);
232 cisco_keepalive_send(dev, CISCO_KEEPALIVE_REQ,
233 ++hdlc->state.cisco.txseq,
234 hdlc->state.cisco.rxseq);
235 hdlc->state.cisco.request_sent = 1;
236 hdlc->state.cisco.timer.expires = jiffies +
237 hdlc->state.cisco.settings.interval * HZ;
238 hdlc->state.cisco.timer.function = cisco_timer;
239 hdlc->state.cisco.timer.data = arg;
240 add_timer(&hdlc->state.cisco.timer);
245 static void cisco_start(struct net_device *dev)
247 hdlc_device *hdlc = dev_to_hdlc(dev);
248 hdlc->state.cisco.up = 0;
249 hdlc->state.cisco.request_sent = 0;
250 hdlc->state.cisco.txseq = hdlc->state.cisco.rxseq = 0;
252 init_timer(&hdlc->state.cisco.timer);
253 hdlc->state.cisco.timer.expires = jiffies + HZ; /*First poll after 1s*/
254 hdlc->state.cisco.timer.function = cisco_timer;
255 hdlc->state.cisco.timer.data = (unsigned long)dev;
256 add_timer(&hdlc->state.cisco.timer);
261 static void cisco_stop(struct net_device *dev)
263 hdlc_device *hdlc = dev_to_hdlc(dev);
264 del_timer_sync(&hdlc->state.cisco.timer);
265 if (netif_carrier_ok(dev))
266 netif_carrier_off(dev);
267 hdlc->state.cisco.up = 0;
268 hdlc->state.cisco.request_sent = 0;
273 int hdlc_cisco_ioctl(struct net_device *dev, struct ifreq *ifr)
275 cisco_proto __user *cisco_s = ifr->ifr_settings.ifs_ifsu.cisco;
276 const size_t size = sizeof(cisco_proto);
277 cisco_proto new_settings;
278 hdlc_device *hdlc = dev_to_hdlc(dev);
281 switch (ifr->ifr_settings.type) {
283 ifr->ifr_settings.type = IF_PROTO_CISCO;
284 if (ifr->ifr_settings.size < size) {
285 ifr->ifr_settings.size = size; /* data size wanted */
288 if (copy_to_user(cisco_s, &hdlc->state.cisco.settings, size))
293 if(!capable(CAP_NET_ADMIN))
296 if(dev->flags & IFF_UP)
299 if (copy_from_user(&new_settings, cisco_s, size))
302 if (new_settings.interval < 1 ||
303 new_settings.timeout < 2)
306 result=hdlc->attach(dev, ENCODING_NRZ,PARITY_CRC16_PR1_CCITT);
311 hdlc_proto_detach(hdlc);
312 memcpy(&hdlc->state.cisco.settings, &new_settings, size);
313 memset(&hdlc->proto, 0, sizeof(hdlc->proto));
315 hdlc->proto.start = cisco_start;
316 hdlc->proto.stop = cisco_stop;
317 hdlc->proto.netif_rx = cisco_rx;
318 hdlc->proto.type_trans = cisco_type_trans;
319 hdlc->proto.id = IF_PROTO_CISCO;
320 dev->hard_start_xmit = hdlc->xmit;
321 dev->hard_header = cisco_hard_header;
322 dev->hard_header_cache = NULL;
323 dev->type = ARPHRD_CISCO;
324 dev->flags = IFF_POINTOPOINT | IFF_NOARP;