diff options
author | Krzysztof Halasa <khc@pm.waw.pl> | 2006-09-26 17:23:45 -0400 |
---|---|---|
committer | Jeff Garzik <jeff@garzik.org> | 2006-09-26 17:40:24 -0400 |
commit | eb2a2fd91f7c8a53b15063d6f08cf22b9a56cbfb (patch) | |
tree | 1d910a9460b76fd85ed02e8b9131270e4977f6f7 /drivers/net/wan/hdlc_cisco.c | |
parent | c226951b93f7cd7c3a10b17384535b617bd43fd0 (diff) |
[PATCH] Modularize generic HDLC
This patch enables building of individual WAN protocol support
routines (parts of generic HDLC) as separate modules.
All protocol-private definitions are moved from hdlc.h file
to protocol drivers. User-space interface and interface
between generic HDLC and underlying low-level HDLC drivers
are unchanged.
Signed-off-by: Krzysztof Halasa <khc@pm.waw.pl>
Signed-off-by: Jeff Garzik <jeff@garzik.org>
Diffstat (limited to 'drivers/net/wan/hdlc_cisco.c')
-rw-r--r-- | drivers/net/wan/hdlc_cisco.c | 198 |
1 files changed, 134 insertions, 64 deletions
diff --git a/drivers/net/wan/hdlc_cisco.c b/drivers/net/wan/hdlc_cisco.c index f289daba0c7b..7ec2b2f9b7ee 100644 --- a/drivers/net/wan/hdlc_cisco.c +++ b/drivers/net/wan/hdlc_cisco.c | |||
@@ -2,7 +2,7 @@ | |||
2 | * Generic HDLC support routines for Linux | 2 | * Generic HDLC support routines for Linux |
3 | * Cisco HDLC support | 3 | * Cisco HDLC support |
4 | * | 4 | * |
5 | * Copyright (C) 2000 - 2003 Krzysztof Halasa <khc@pm.waw.pl> | 5 | * Copyright (C) 2000 - 2006 Krzysztof Halasa <khc@pm.waw.pl> |
6 | * | 6 | * |
7 | * This program is free software; you can redistribute it and/or modify it | 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 | 8 | * under the terms of version 2 of the GNU General Public License |
@@ -34,17 +34,56 @@ | |||
34 | #define CISCO_KEEPALIVE_REQ 2 /* Cisco keepalive request */ | 34 | #define CISCO_KEEPALIVE_REQ 2 /* Cisco keepalive request */ |
35 | 35 | ||
36 | 36 | ||
37 | struct hdlc_header { | ||
38 | u8 address; | ||
39 | u8 control; | ||
40 | u16 protocol; | ||
41 | }__attribute__ ((packed)); | ||
42 | |||
43 | |||
44 | struct cisco_packet { | ||
45 | u32 type; /* code */ | ||
46 | u32 par1; | ||
47 | u32 par2; | ||
48 | u16 rel; /* reliability */ | ||
49 | u32 time; | ||
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; | ||
59 | unsigned long last_poll; | ||
60 | int up; | ||
61 | int request_sent; | ||
62 | u32 txseq; /* TX sequence number */ | ||
63 | u32 rxseq; /* RX sequence number */ | ||
64 | }; | ||
65 | |||
66 | |||
67 | static int cisco_ioctl(struct net_device *dev, struct ifreq *ifr); | ||
68 | |||
69 | |||
70 | static inline struct cisco_state * state(hdlc_device *hdlc) | ||
71 | { | ||
72 | return(struct cisco_state *)(hdlc->state); | ||
73 | } | ||
74 | |||
75 | |||
37 | static int cisco_hard_header(struct sk_buff *skb, struct net_device *dev, | 76 | static int cisco_hard_header(struct sk_buff *skb, struct net_device *dev, |
38 | u16 type, void *daddr, void *saddr, | 77 | u16 type, void *daddr, void *saddr, |
39 | unsigned int len) | 78 | unsigned int len) |
40 | { | 79 | { |
41 | hdlc_header *data; | 80 | struct hdlc_header *data; |
42 | #ifdef DEBUG_HARD_HEADER | 81 | #ifdef DEBUG_HARD_HEADER |
43 | printk(KERN_DEBUG "%s: cisco_hard_header called\n", dev->name); | 82 | printk(KERN_DEBUG "%s: cisco_hard_header called\n", dev->name); |
44 | #endif | 83 | #endif |
45 | 84 | ||
46 | skb_push(skb, sizeof(hdlc_header)); | 85 | skb_push(skb, sizeof(struct hdlc_header)); |
47 | data = (hdlc_header*)skb->data; | 86 | data = (struct hdlc_header*)skb->data; |
48 | if (type == CISCO_KEEPALIVE) | 87 | if (type == CISCO_KEEPALIVE) |
49 | data->address = CISCO_MULTICAST; | 88 | data->address = CISCO_MULTICAST; |
50 | else | 89 | else |
@@ -52,7 +91,7 @@ static int cisco_hard_header(struct sk_buff *skb, struct net_device *dev, | |||
52 | data->control = 0; | 91 | data->control = 0; |
53 | data->protocol = htons(type); | 92 | data->protocol = htons(type); |
54 | 93 | ||
55 | return sizeof(hdlc_header); | 94 | return sizeof(struct hdlc_header); |
56 | } | 95 | } |
57 | 96 | ||
58 | 97 | ||
@@ -61,9 +100,10 @@ static void cisco_keepalive_send(struct net_device *dev, u32 type, | |||
61 | u32 par1, u32 par2) | 100 | u32 par1, u32 par2) |
62 | { | 101 | { |
63 | struct sk_buff *skb; | 102 | struct sk_buff *skb; |
64 | cisco_packet *data; | 103 | struct cisco_packet *data; |
65 | 104 | ||
66 | skb = dev_alloc_skb(sizeof(hdlc_header) + sizeof(cisco_packet)); | 105 | skb = dev_alloc_skb(sizeof(struct hdlc_header) + |
106 | sizeof(struct cisco_packet)); | ||
67 | if (!skb) { | 107 | if (!skb) { |
68 | printk(KERN_WARNING | 108 | printk(KERN_WARNING |
69 | "%s: Memory squeeze on cisco_keepalive_send()\n", | 109 | "%s: Memory squeeze on cisco_keepalive_send()\n", |
@@ -72,7 +112,7 @@ static void cisco_keepalive_send(struct net_device *dev, u32 type, | |||
72 | } | 112 | } |
73 | skb_reserve(skb, 4); | 113 | skb_reserve(skb, 4); |
74 | cisco_hard_header(skb, dev, CISCO_KEEPALIVE, NULL, NULL, 0); | 114 | cisco_hard_header(skb, dev, CISCO_KEEPALIVE, NULL, NULL, 0); |
75 | data = (cisco_packet*)(skb->data + 4); | 115 | data = (struct cisco_packet*)(skb->data + 4); |
76 | 116 | ||
77 | data->type = htonl(type); | 117 | data->type = htonl(type); |
78 | data->par1 = htonl(par1); | 118 | data->par1 = htonl(par1); |
@@ -81,7 +121,7 @@ static void cisco_keepalive_send(struct net_device *dev, u32 type, | |||
81 | /* we will need do_div here if 1000 % HZ != 0 */ | 121 | /* we will need do_div here if 1000 % HZ != 0 */ |
82 | data->time = htonl((jiffies - INITIAL_JIFFIES) * (1000 / HZ)); | 122 | data->time = htonl((jiffies - INITIAL_JIFFIES) * (1000 / HZ)); |
83 | 123 | ||
84 | skb_put(skb, sizeof(cisco_packet)); | 124 | skb_put(skb, sizeof(struct cisco_packet)); |
85 | skb->priority = TC_PRIO_CONTROL; | 125 | skb->priority = TC_PRIO_CONTROL; |
86 | skb->dev = dev; | 126 | skb->dev = dev; |
87 | skb->nh.raw = skb->data; | 127 | skb->nh.raw = skb->data; |
@@ -93,9 +133,9 @@ static void cisco_keepalive_send(struct net_device *dev, u32 type, | |||
93 | 133 | ||
94 | static __be16 cisco_type_trans(struct sk_buff *skb, struct net_device *dev) | 134 | static __be16 cisco_type_trans(struct sk_buff *skb, struct net_device *dev) |
95 | { | 135 | { |
96 | hdlc_header *data = (hdlc_header*)skb->data; | 136 | struct hdlc_header *data = (struct hdlc_header*)skb->data; |
97 | 137 | ||
98 | if (skb->len < sizeof(hdlc_header)) | 138 | if (skb->len < sizeof(struct hdlc_header)) |
99 | return __constant_htons(ETH_P_HDLC); | 139 | return __constant_htons(ETH_P_HDLC); |
100 | 140 | ||
101 | if (data->address != CISCO_MULTICAST && | 141 | if (data->address != CISCO_MULTICAST && |
@@ -106,7 +146,7 @@ static __be16 cisco_type_trans(struct sk_buff *skb, struct net_device *dev) | |||
106 | case __constant_htons(ETH_P_IP): | 146 | case __constant_htons(ETH_P_IP): |
107 | case __constant_htons(ETH_P_IPX): | 147 | case __constant_htons(ETH_P_IPX): |
108 | case __constant_htons(ETH_P_IPV6): | 148 | case __constant_htons(ETH_P_IPV6): |
109 | skb_pull(skb, sizeof(hdlc_header)); | 149 | skb_pull(skb, sizeof(struct hdlc_header)); |
110 | return data->protocol; | 150 | return data->protocol; |
111 | default: | 151 | default: |
112 | return __constant_htons(ETH_P_HDLC); | 152 | return __constant_htons(ETH_P_HDLC); |
@@ -118,12 +158,12 @@ static int cisco_rx(struct sk_buff *skb) | |||
118 | { | 158 | { |
119 | struct net_device *dev = skb->dev; | 159 | struct net_device *dev = skb->dev; |
120 | hdlc_device *hdlc = dev_to_hdlc(dev); | 160 | hdlc_device *hdlc = dev_to_hdlc(dev); |
121 | hdlc_header *data = (hdlc_header*)skb->data; | 161 | struct hdlc_header *data = (struct hdlc_header*)skb->data; |
122 | cisco_packet *cisco_data; | 162 | struct cisco_packet *cisco_data; |
123 | struct in_device *in_dev; | 163 | struct in_device *in_dev; |
124 | u32 addr, mask; | 164 | u32 addr, mask; |
125 | 165 | ||
126 | if (skb->len < sizeof(hdlc_header)) | 166 | if (skb->len < sizeof(struct hdlc_header)) |
127 | goto rx_error; | 167 | goto rx_error; |
128 | 168 | ||
129 | if (data->address != CISCO_MULTICAST && | 169 | if (data->address != CISCO_MULTICAST && |
@@ -137,15 +177,17 @@ static int cisco_rx(struct sk_buff *skb) | |||
137 | return NET_RX_SUCCESS; | 177 | return NET_RX_SUCCESS; |
138 | 178 | ||
139 | case CISCO_KEEPALIVE: | 179 | case CISCO_KEEPALIVE: |
140 | if (skb->len != sizeof(hdlc_header) + CISCO_PACKET_LEN && | 180 | if ((skb->len != sizeof(struct hdlc_header) + |
141 | skb->len != sizeof(hdlc_header) + CISCO_BIG_PACKET_LEN) { | 181 | CISCO_PACKET_LEN) && |
142 | printk(KERN_INFO "%s: Invalid length of Cisco " | 182 | (skb->len != sizeof(struct hdlc_header) + |
143 | "control packet (%d bytes)\n", | 183 | CISCO_BIG_PACKET_LEN)) { |
144 | dev->name, skb->len); | 184 | printk(KERN_INFO "%s: Invalid length of Cisco control" |
185 | " packet (%d bytes)\n", dev->name, skb->len); | ||
145 | goto rx_error; | 186 | goto rx_error; |
146 | } | 187 | } |
147 | 188 | ||
148 | cisco_data = (cisco_packet*)(skb->data + sizeof(hdlc_header)); | 189 | cisco_data = (struct cisco_packet*)(skb->data + sizeof |
190 | (struct hdlc_header)); | ||
149 | 191 | ||
150 | switch(ntohl (cisco_data->type)) { | 192 | switch(ntohl (cisco_data->type)) { |
151 | case CISCO_ADDR_REQ: /* Stolen from syncppp.c :-) */ | 193 | case CISCO_ADDR_REQ: /* Stolen from syncppp.c :-) */ |
@@ -178,11 +220,11 @@ static int cisco_rx(struct sk_buff *skb) | |||
178 | goto rx_error; | 220 | goto rx_error; |
179 | 221 | ||
180 | case CISCO_KEEPALIVE_REQ: | 222 | case CISCO_KEEPALIVE_REQ: |
181 | hdlc->state.cisco.rxseq = ntohl(cisco_data->par1); | 223 | state(hdlc)->rxseq = ntohl(cisco_data->par1); |
182 | if (hdlc->state.cisco.request_sent && | 224 | if (state(hdlc)->request_sent && |
183 | ntohl(cisco_data->par2)==hdlc->state.cisco.txseq) { | 225 | ntohl(cisco_data->par2) == state(hdlc)->txseq) { |
184 | hdlc->state.cisco.last_poll = jiffies; | 226 | state(hdlc)->last_poll = jiffies; |
185 | if (!hdlc->state.cisco.up) { | 227 | if (!state(hdlc)->up) { |
186 | u32 sec, min, hrs, days; | 228 | u32 sec, min, hrs, days; |
187 | sec = ntohl(cisco_data->time) / 1000; | 229 | sec = ntohl(cisco_data->time) / 1000; |
188 | min = sec / 60; sec -= min * 60; | 230 | min = sec / 60; sec -= min * 60; |
@@ -193,7 +235,7 @@ static int cisco_rx(struct sk_buff *skb) | |||
193 | dev->name, days, hrs, | 235 | dev->name, days, hrs, |
194 | min, sec); | 236 | min, sec); |
195 | netif_dormant_off(dev); | 237 | netif_dormant_off(dev); |
196 | hdlc->state.cisco.up = 1; | 238 | state(hdlc)->up = 1; |
197 | } | 239 | } |
198 | } | 240 | } |
199 | 241 | ||
@@ -208,7 +250,7 @@ static int cisco_rx(struct sk_buff *skb) | |||
208 | return NET_RX_DROP; | 250 | return NET_RX_DROP; |
209 | 251 | ||
210 | rx_error: | 252 | rx_error: |
211 | hdlc->stats.rx_errors++; /* Mark error */ | 253 | dev_to_desc(dev)->stats.rx_errors++; /* Mark error */ |
212 | dev_kfree_skb_any(skb); | 254 | dev_kfree_skb_any(skb); |
213 | return NET_RX_DROP; | 255 | return NET_RX_DROP; |
214 | } | 256 | } |
@@ -220,23 +262,22 @@ static void cisco_timer(unsigned long arg) | |||
220 | struct net_device *dev = (struct net_device *)arg; | 262 | struct net_device *dev = (struct net_device *)arg; |
221 | hdlc_device *hdlc = dev_to_hdlc(dev); | 263 | hdlc_device *hdlc = dev_to_hdlc(dev); |
222 | 264 | ||
223 | if (hdlc->state.cisco.up && | 265 | if (state(hdlc)->up && |
224 | time_after(jiffies, hdlc->state.cisco.last_poll + | 266 | time_after(jiffies, state(hdlc)->last_poll + |
225 | hdlc->state.cisco.settings.timeout * HZ)) { | 267 | state(hdlc)->settings.timeout * HZ)) { |
226 | hdlc->state.cisco.up = 0; | 268 | state(hdlc)->up = 0; |
227 | printk(KERN_INFO "%s: Link down\n", dev->name); | 269 | printk(KERN_INFO "%s: Link down\n", dev->name); |
228 | netif_dormant_on(dev); | 270 | netif_dormant_on(dev); |
229 | } | 271 | } |
230 | 272 | ||
231 | cisco_keepalive_send(dev, CISCO_KEEPALIVE_REQ, | 273 | cisco_keepalive_send(dev, CISCO_KEEPALIVE_REQ, ++state(hdlc)->txseq, |
232 | ++hdlc->state.cisco.txseq, | 274 | state(hdlc)->rxseq); |
233 | hdlc->state.cisco.rxseq); | 275 | state(hdlc)->request_sent = 1; |
234 | hdlc->state.cisco.request_sent = 1; | 276 | state(hdlc)->timer.expires = jiffies + |
235 | hdlc->state.cisco.timer.expires = jiffies + | 277 | state(hdlc)->settings.interval * HZ; |
236 | hdlc->state.cisco.settings.interval * HZ; | 278 | state(hdlc)->timer.function = cisco_timer; |
237 | hdlc->state.cisco.timer.function = cisco_timer; | 279 | state(hdlc)->timer.data = arg; |
238 | hdlc->state.cisco.timer.data = arg; | 280 | add_timer(&state(hdlc)->timer); |
239 | add_timer(&hdlc->state.cisco.timer); | ||
240 | } | 281 | } |
241 | 282 | ||
242 | 283 | ||
@@ -244,15 +285,15 @@ static void cisco_timer(unsigned long arg) | |||
244 | static void cisco_start(struct net_device *dev) | 285 | static void cisco_start(struct net_device *dev) |
245 | { | 286 | { |
246 | hdlc_device *hdlc = dev_to_hdlc(dev); | 287 | hdlc_device *hdlc = dev_to_hdlc(dev); |
247 | hdlc->state.cisco.up = 0; | 288 | state(hdlc)->up = 0; |
248 | hdlc->state.cisco.request_sent = 0; | 289 | state(hdlc)->request_sent = 0; |
249 | hdlc->state.cisco.txseq = hdlc->state.cisco.rxseq = 0; | 290 | state(hdlc)->txseq = state(hdlc)->rxseq = 0; |
250 | 291 | ||
251 | init_timer(&hdlc->state.cisco.timer); | 292 | init_timer(&state(hdlc)->timer); |
252 | hdlc->state.cisco.timer.expires = jiffies + HZ; /*First poll after 1s*/ | 293 | state(hdlc)->timer.expires = jiffies + HZ; /*First poll after 1s*/ |
253 | hdlc->state.cisco.timer.function = cisco_timer; | 294 | state(hdlc)->timer.function = cisco_timer; |
254 | hdlc->state.cisco.timer.data = (unsigned long)dev; | 295 | state(hdlc)->timer.data = (unsigned long)dev; |
255 | add_timer(&hdlc->state.cisco.timer); | 296 | add_timer(&state(hdlc)->timer); |
256 | } | 297 | } |
257 | 298 | ||
258 | 299 | ||
@@ -260,15 +301,24 @@ static void cisco_start(struct net_device *dev) | |||
260 | static void cisco_stop(struct net_device *dev) | 301 | static void cisco_stop(struct net_device *dev) |
261 | { | 302 | { |
262 | hdlc_device *hdlc = dev_to_hdlc(dev); | 303 | hdlc_device *hdlc = dev_to_hdlc(dev); |
263 | del_timer_sync(&hdlc->state.cisco.timer); | 304 | del_timer_sync(&state(hdlc)->timer); |
264 | netif_dormant_on(dev); | 305 | netif_dormant_on(dev); |
265 | hdlc->state.cisco.up = 0; | 306 | state(hdlc)->up = 0; |
266 | hdlc->state.cisco.request_sent = 0; | 307 | state(hdlc)->request_sent = 0; |
267 | } | 308 | } |
268 | 309 | ||
269 | 310 | ||
270 | 311 | ||
271 | int hdlc_cisco_ioctl(struct net_device *dev, struct ifreq *ifr) | 312 | static struct hdlc_proto proto = { |
313 | .start = cisco_start, | ||
314 | .stop = cisco_stop, | ||
315 | .type_trans = cisco_type_trans, | ||
316 | .ioctl = cisco_ioctl, | ||
317 | .module = THIS_MODULE, | ||
318 | }; | ||
319 | |||
320 | |||
321 | static int cisco_ioctl(struct net_device *dev, struct ifreq *ifr) | ||
272 | { | 322 | { |
273 | cisco_proto __user *cisco_s = ifr->ifr_settings.ifs_ifsu.cisco; | 323 | cisco_proto __user *cisco_s = ifr->ifr_settings.ifs_ifsu.cisco; |
274 | const size_t size = sizeof(cisco_proto); | 324 | const size_t size = sizeof(cisco_proto); |
@@ -278,12 +328,14 @@ int hdlc_cisco_ioctl(struct net_device *dev, struct ifreq *ifr) | |||
278 | 328 | ||
279 | switch (ifr->ifr_settings.type) { | 329 | switch (ifr->ifr_settings.type) { |
280 | case IF_GET_PROTO: | 330 | case IF_GET_PROTO: |
331 | if (dev_to_hdlc(dev)->proto != &proto) | ||
332 | return -EINVAL; | ||
281 | ifr->ifr_settings.type = IF_PROTO_CISCO; | 333 | ifr->ifr_settings.type = IF_PROTO_CISCO; |
282 | if (ifr->ifr_settings.size < size) { | 334 | if (ifr->ifr_settings.size < size) { |
283 | ifr->ifr_settings.size = size; /* data size wanted */ | 335 | ifr->ifr_settings.size = size; /* data size wanted */ |
284 | return -ENOBUFS; | 336 | return -ENOBUFS; |
285 | } | 337 | } |
286 | if (copy_to_user(cisco_s, &hdlc->state.cisco.settings, size)) | 338 | if (copy_to_user(cisco_s, &state(hdlc)->settings, size)) |
287 | return -EFAULT; | 339 | return -EFAULT; |
288 | return 0; | 340 | return 0; |
289 | 341 | ||
@@ -302,19 +354,15 @@ int hdlc_cisco_ioctl(struct net_device *dev, struct ifreq *ifr) | |||
302 | return -EINVAL; | 354 | return -EINVAL; |
303 | 355 | ||
304 | result=hdlc->attach(dev, ENCODING_NRZ,PARITY_CRC16_PR1_CCITT); | 356 | result=hdlc->attach(dev, ENCODING_NRZ,PARITY_CRC16_PR1_CCITT); |
305 | |||
306 | if (result) | 357 | if (result) |
307 | return result; | 358 | return result; |
308 | 359 | ||
309 | hdlc_proto_detach(hdlc); | 360 | result = attach_hdlc_protocol(dev, &proto, cisco_rx, |
310 | memcpy(&hdlc->state.cisco.settings, &new_settings, size); | 361 | sizeof(struct cisco_state)); |
311 | memset(&hdlc->proto, 0, sizeof(hdlc->proto)); | 362 | if (result) |
363 | return result; | ||
312 | 364 | ||
313 | hdlc->proto.start = cisco_start; | 365 | memcpy(&state(hdlc)->settings, &new_settings, size); |
314 | hdlc->proto.stop = cisco_stop; | ||
315 | hdlc->proto.netif_rx = cisco_rx; | ||
316 | hdlc->proto.type_trans = cisco_type_trans; | ||
317 | hdlc->proto.id = IF_PROTO_CISCO; | ||
318 | dev->hard_start_xmit = hdlc->xmit; | 366 | dev->hard_start_xmit = hdlc->xmit; |
319 | dev->hard_header = cisco_hard_header; | 367 | dev->hard_header = cisco_hard_header; |
320 | dev->hard_header_cache = NULL; | 368 | dev->hard_header_cache = NULL; |
@@ -327,3 +375,25 @@ int hdlc_cisco_ioctl(struct net_device *dev, struct ifreq *ifr) | |||
327 | 375 | ||
328 | return -EINVAL; | 376 | return -EINVAL; |
329 | } | 377 | } |
378 | |||
379 | |||
380 | static int __init mod_init(void) | ||
381 | { | ||
382 | register_hdlc_protocol(&proto); | ||
383 | return 0; | ||
384 | } | ||
385 | |||
386 | |||
387 | |||
388 | static void __exit mod_exit(void) | ||
389 | { | ||
390 | unregister_hdlc_protocol(&proto); | ||
391 | } | ||
392 | |||
393 | |||
394 | module_init(mod_init); | ||
395 | module_exit(mod_exit); | ||
396 | |||
397 | MODULE_AUTHOR("Krzysztof Halasa <khc@pm.waw.pl>"); | ||
398 | MODULE_DESCRIPTION("Cisco HDLC protocol support for generic HDLC"); | ||
399 | MODULE_LICENSE("GPL v2"); | ||