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
+ 4);
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 __be16
cisco_type_trans(struct sk_buff
*skb
, struct net_device
*dev
)
96 hdlc_header
*data
= (hdlc_header
*)skb
->data
;
98 if (skb
->len
< sizeof(hdlc_header
))
99 return __constant_htons(ETH_P_HDLC
);
101 if (data
->address
!= CISCO_MULTICAST
&&
102 data
->address
!= CISCO_UNICAST
)
103 return __constant_htons(ETH_P_HDLC
);
105 switch(data
->protocol
) {
106 case __constant_htons(ETH_P_IP
):
107 case __constant_htons(ETH_P_IPX
):
108 case __constant_htons(ETH_P_IPV6
):
109 skb_pull(skb
, sizeof(hdlc_header
));
110 return data
->protocol
;
112 return __constant_htons(ETH_P_HDLC
);
117 static int cisco_rx(struct sk_buff
*skb
)
119 struct net_device
*dev
= skb
->dev
;
120 hdlc_device
*hdlc
= dev_to_hdlc(dev
);
121 hdlc_header
*data
= (hdlc_header
*)skb
->data
;
122 cisco_packet
*cisco_data
;
123 struct in_device
*in_dev
;
126 if (skb
->len
< sizeof(hdlc_header
))
129 if (data
->address
!= CISCO_MULTICAST
&&
130 data
->address
!= CISCO_UNICAST
)
133 switch(ntohs(data
->protocol
)) {
135 /* Packet is not needed, drop it. */
136 dev_kfree_skb_any(skb
);
137 return NET_RX_SUCCESS
;
139 case CISCO_KEEPALIVE
:
140 if (skb
->len
!= sizeof(hdlc_header
) + CISCO_PACKET_LEN
&&
141 skb
->len
!= sizeof(hdlc_header
) + CISCO_BIG_PACKET_LEN
) {
142 printk(KERN_INFO
"%s: Invalid length of Cisco "
143 "control packet (%d bytes)\n",
144 dev
->name
, skb
->len
);
148 cisco_data
= (cisco_packet
*)(skb
->data
+ sizeof(hdlc_header
));
150 switch(ntohl (cisco_data
->type
)) {
151 case CISCO_ADDR_REQ
: /* Stolen from syncppp.c :-) */
152 in_dev
= dev
->ip_ptr
;
154 mask
= ~0; /* is the mask correct? */
156 if (in_dev
!= NULL
) {
157 struct in_ifaddr
**ifap
= &in_dev
->ifa_list
;
159 while (*ifap
!= NULL
) {
160 if (strcmp(dev
->name
,
161 (*ifap
)->ifa_label
) == 0) {
162 addr
= (*ifap
)->ifa_local
;
163 mask
= (*ifap
)->ifa_mask
;
166 ifap
= &(*ifap
)->ifa_next
;
169 cisco_keepalive_send(dev
, CISCO_ADDR_REPLY
,
172 dev_kfree_skb_any(skb
);
173 return NET_RX_SUCCESS
;
175 case CISCO_ADDR_REPLY
:
176 printk(KERN_INFO
"%s: Unexpected Cisco IP address "
177 "reply\n", dev
->name
);
180 case CISCO_KEEPALIVE_REQ
:
181 hdlc
->state
.cisco
.rxseq
= ntohl(cisco_data
->par1
);
182 if (hdlc
->state
.cisco
.request_sent
&&
183 ntohl(cisco_data
->par2
)==hdlc
->state
.cisco
.txseq
) {
184 hdlc
->state
.cisco
.last_poll
= jiffies
;
185 if (!hdlc
->state
.cisco
.up
) {
186 u32 sec
, min
, hrs
, days
;
187 sec
= ntohl(cisco_data
->time
) / 1000;
188 min
= sec
/ 60; sec
-= min
* 60;
189 hrs
= min
/ 60; min
-= hrs
* 60;
190 days
= hrs
/ 24; hrs
-= days
* 24;
191 printk(KERN_INFO
"%s: Link up (peer "
192 "uptime %ud%uh%um%us)\n",
193 dev
->name
, days
, hrs
,
196 netif_carrier_on(dev
);
198 hdlc
->state
.cisco
.up
= 1;
202 dev_kfree_skb_any(skb
);
203 return NET_RX_SUCCESS
;
204 } /* switch(keepalive type) */
205 } /* switch(protocol) */
207 printk(KERN_INFO
"%s: Unsupported protocol %x\n", dev
->name
,
209 dev_kfree_skb_any(skb
);
213 hdlc
->stats
.rx_errors
++; /* Mark error */
214 dev_kfree_skb_any(skb
);
220 static void cisco_timer(unsigned long arg
)
222 struct net_device
*dev
= (struct net_device
*)arg
;
223 hdlc_device
*hdlc
= dev_to_hdlc(dev
);
225 if (hdlc
->state
.cisco
.up
&&
226 time_after(jiffies
, hdlc
->state
.cisco
.last_poll
+
227 hdlc
->state
.cisco
.settings
.timeout
* HZ
)) {
228 hdlc
->state
.cisco
.up
= 0;
229 printk(KERN_INFO
"%s: Link down\n", dev
->name
);
231 netif_carrier_off(dev
);
235 cisco_keepalive_send(dev
, CISCO_KEEPALIVE_REQ
,
236 ++hdlc
->state
.cisco
.txseq
,
237 hdlc
->state
.cisco
.rxseq
);
238 hdlc
->state
.cisco
.request_sent
= 1;
239 hdlc
->state
.cisco
.timer
.expires
= jiffies
+
240 hdlc
->state
.cisco
.settings
.interval
* HZ
;
241 hdlc
->state
.cisco
.timer
.function
= cisco_timer
;
242 hdlc
->state
.cisco
.timer
.data
= arg
;
243 add_timer(&hdlc
->state
.cisco
.timer
);
248 static void cisco_start(struct net_device
*dev
)
250 hdlc_device
*hdlc
= dev_to_hdlc(dev
);
251 hdlc
->state
.cisco
.up
= 0;
252 hdlc
->state
.cisco
.request_sent
= 0;
253 hdlc
->state
.cisco
.txseq
= hdlc
->state
.cisco
.rxseq
= 0;
255 init_timer(&hdlc
->state
.cisco
.timer
);
256 hdlc
->state
.cisco
.timer
.expires
= jiffies
+ HZ
; /*First poll after 1s*/
257 hdlc
->state
.cisco
.timer
.function
= cisco_timer
;
258 hdlc
->state
.cisco
.timer
.data
= (unsigned long)dev
;
259 add_timer(&hdlc
->state
.cisco
.timer
);
264 static void cisco_stop(struct net_device
*dev
)
266 hdlc_device
*hdlc
= dev_to_hdlc(dev
);
267 del_timer_sync(&hdlc
->state
.cisco
.timer
);
269 if (netif_carrier_ok(dev
))
270 netif_carrier_off(dev
);
272 hdlc
->state
.cisco
.up
= 0;
273 hdlc
->state
.cisco
.request_sent
= 0;
278 int hdlc_cisco_ioctl(struct net_device
*dev
, struct ifreq
*ifr
)
280 cisco_proto __user
*cisco_s
= ifr
->ifr_settings
.ifs_ifsu
.cisco
;
281 const size_t size
= sizeof(cisco_proto
);
282 cisco_proto new_settings
;
283 hdlc_device
*hdlc
= dev_to_hdlc(dev
);
286 switch (ifr
->ifr_settings
.type
) {
288 ifr
->ifr_settings
.type
= IF_PROTO_CISCO
;
289 if (ifr
->ifr_settings
.size
< size
) {
290 ifr
->ifr_settings
.size
= size
; /* data size wanted */
293 if (copy_to_user(cisco_s
, &hdlc
->state
.cisco
.settings
, size
))
298 if(!capable(CAP_NET_ADMIN
))
301 if(dev
->flags
& IFF_UP
)
304 if (copy_from_user(&new_settings
, cisco_s
, size
))
307 if (new_settings
.interval
< 1 ||
308 new_settings
.timeout
< 2)
311 result
=hdlc
->attach(dev
, ENCODING_NRZ
,PARITY_CRC16_PR1_CCITT
);
316 hdlc_proto_detach(hdlc
);
317 memcpy(&hdlc
->state
.cisco
.settings
, &new_settings
, size
);
318 memset(&hdlc
->proto
, 0, sizeof(hdlc
->proto
));
320 hdlc
->proto
.start
= cisco_start
;
321 hdlc
->proto
.stop
= cisco_stop
;
322 hdlc
->proto
.netif_rx
= cisco_rx
;
323 hdlc
->proto
.type_trans
= cisco_type_trans
;
324 hdlc
->proto
.id
= IF_PROTO_CISCO
;
325 dev
->hard_start_xmit
= hdlc
->xmit
;
326 dev
->hard_header
= cisco_hard_header
;
327 dev
->hard_header_cache
= NULL
;
328 dev
->type
= ARPHRD_CISCO
;
329 dev
->flags
= IFF_POINTOPOINT
| IFF_NOARP
;