]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/net/wan/hdlc_fr.c
net: use net_eq() in INET_MATCH and INET_TW_MATCH
[net-next-2.6.git] / drivers / net / wan / hdlc_fr.c
CommitLineData
1da177e4
LT
1/*
2 * Generic HDLC support routines for Linux
3 * Frame Relay support
4 *
eb2a2fd9 5 * Copyright (C) 1999 - 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 Theory of PVC state
13
14 DCE mode:
15
16 (exist,new) -> 0,0 when "PVC create" or if "link unreliable"
17 0,x -> 1,1 if "link reliable" when sending FULL STATUS
18 1,1 -> 1,0 if received FULL STATUS ACK
19
20 (active) -> 0 when "ifconfig PVC down" or "link unreliable" or "PVC create"
21 -> 1 when "PVC up" and (exist,new) = 1,0
22
23 DTE mode:
24 (exist,new,active) = FULL STATUS if "link reliable"
25 = 0, 0, 0 if "link unreliable"
26 No LMI:
27 active = open and "link reliable"
28 exist = new = not used
29
b3dd65f9
KH
30 CCITT LMI: ITU-T Q.933 Annex A
31 ANSI LMI: ANSI T1.617 Annex D
32 CISCO LMI: the original, aka "Gang of Four" LMI
33
1da177e4
LT
34*/
35
1da177e4 36#include <linux/errno.h>
4dfce407
KH
37#include <linux/etherdevice.h>
38#include <linux/hdlc.h>
1da177e4 39#include <linux/if_arp.h>
4dfce407 40#include <linux/inetdevice.h>
1da177e4 41#include <linux/init.h>
4dfce407
KH
42#include <linux/kernel.h>
43#include <linux/module.h>
1da177e4 44#include <linux/pkt_sched.h>
4dfce407 45#include <linux/poll.h>
1da177e4 46#include <linux/rtnetlink.h>
4dfce407
KH
47#include <linux/skbuff.h>
48#include <linux/slab.h>
1da177e4
LT
49
50#undef DEBUG_PKT
51#undef DEBUG_ECN
52#undef DEBUG_LINK
eb2a2fd9
KH
53#undef DEBUG_PROTO
54#undef DEBUG_PVC
1da177e4 55
b3dd65f9
KH
56#define FR_UI 0x03
57#define FR_PAD 0x00
58
59#define NLPID_IP 0xCC
60#define NLPID_IPV6 0x8E
61#define NLPID_SNAP 0x80
62#define NLPID_PAD 0x00
63#define NLPID_CCITT_ANSI_LMI 0x08
64#define NLPID_CISCO_LMI 0x09
65
66
67#define LMI_CCITT_ANSI_DLCI 0 /* LMI DLCI */
68#define LMI_CISCO_DLCI 1023
69
70#define LMI_CALLREF 0x00 /* Call Reference */
71#define LMI_ANSI_LOCKSHIFT 0x95 /* ANSI locking shift */
72#define LMI_ANSI_CISCO_REPTYPE 0x01 /* report type */
73#define LMI_CCITT_REPTYPE 0x51
74#define LMI_ANSI_CISCO_ALIVE 0x03 /* keep alive */
75#define LMI_CCITT_ALIVE 0x53
76#define LMI_ANSI_CISCO_PVCSTAT 0x07 /* PVC status */
77#define LMI_CCITT_PVCSTAT 0x57
78
79#define LMI_FULLREP 0x00 /* full report */
80#define LMI_INTEGRITY 0x01 /* link integrity report */
81#define LMI_SINGLE 0x02 /* single PVC report */
82
1da177e4
LT
83#define LMI_STATUS_ENQUIRY 0x75
84#define LMI_STATUS 0x7D /* reply */
85
86#define LMI_REPT_LEN 1 /* report type element length */
87#define LMI_INTEG_LEN 2 /* link integrity element length */
88
b3dd65f9
KH
89#define LMI_CCITT_CISCO_LENGTH 13 /* LMI frame lengths */
90#define LMI_ANSI_LENGTH 14
1da177e4
LT
91
92
93typedef struct {
94#if defined(__LITTLE_ENDIAN_BITFIELD)
95 unsigned ea1: 1;
96 unsigned cr: 1;
97 unsigned dlcih: 6;
4dfce407 98
1da177e4
LT
99 unsigned ea2: 1;
100 unsigned de: 1;
101 unsigned becn: 1;
102 unsigned fecn: 1;
103 unsigned dlcil: 4;
104#else
105 unsigned dlcih: 6;
106 unsigned cr: 1;
107 unsigned ea1: 1;
108
109 unsigned dlcil: 4;
110 unsigned fecn: 1;
111 unsigned becn: 1;
112 unsigned de: 1;
113 unsigned ea2: 1;
114#endif
115}__attribute__ ((packed)) fr_hdr;
116
117
eb2a2fd9
KH
118typedef struct pvc_device_struct {
119 struct net_device *frad;
120 struct net_device *main;
121 struct net_device *ether; /* bridged Ethernet interface */
122 struct pvc_device_struct *next; /* Sorted in ascending DLCI order */
123 int dlci;
124 int open_count;
125
126 struct {
127 unsigned int new: 1;
128 unsigned int active: 1;
129 unsigned int exist: 1;
130 unsigned int deleted: 1;
131 unsigned int fecn: 1;
132 unsigned int becn: 1;
133 unsigned int bandwidth; /* Cisco LMI reporting only */
134 }state;
135}pvc_device;
136
eb2a2fd9
KH
137struct frad_state {
138 fr_proto settings;
139 pvc_device *first_pvc;
140 int dce_pvc_count;
141
142 struct timer_list timer;
143 unsigned long last_poll;
144 int reliable;
145 int dce_changed;
146 int request;
147 int fullrep_sent;
148 u32 last_errors; /* last errors bit list */
149 u8 n391cnt;
150 u8 txseq; /* TX sequence number */
151 u8 rxseq; /* RX sequence number */
152};
153
154
155static int fr_ioctl(struct net_device *dev, struct ifreq *ifr);
156
157
1da177e4
LT
158static inline u16 q922_to_dlci(u8 *hdr)
159{
160 return ((hdr[0] & 0xFC) << 2) | ((hdr[1] & 0xF0) >> 4);
161}
162
163
1da177e4
LT
164static inline void dlci_to_q922(u8 *hdr, u16 dlci)
165{
166 hdr[0] = (dlci >> 2) & 0xFC;
167 hdr[1] = ((dlci << 4) & 0xF0) | 0x01;
168}
169
170
983e2304 171static inline struct frad_state* state(hdlc_device *hdlc)
eb2a2fd9
KH
172{
173 return(struct frad_state *)(hdlc->state);
174}
175
eb2a2fd9 176
1da177e4
LT
177static inline pvc_device* find_pvc(hdlc_device *hdlc, u16 dlci)
178{
eb2a2fd9 179 pvc_device *pvc = state(hdlc)->first_pvc;
1da177e4
LT
180
181 while (pvc) {
182 if (pvc->dlci == dlci)
183 return pvc;
184 if (pvc->dlci > dlci)
185 return NULL; /* the listed is sorted */
186 pvc = pvc->next;
187 }
188
189 return NULL;
190}
191
192
eb2a2fd9 193static pvc_device* add_pvc(struct net_device *dev, u16 dlci)
1da177e4
LT
194{
195 hdlc_device *hdlc = dev_to_hdlc(dev);
eb2a2fd9 196 pvc_device *pvc, **pvc_p = &state(hdlc)->first_pvc;
1da177e4
LT
197
198 while (*pvc_p) {
199 if ((*pvc_p)->dlci == dlci)
200 return *pvc_p;
201 if ((*pvc_p)->dlci > dlci)
202 break; /* the list is sorted */
203 pvc_p = &(*pvc_p)->next;
204 }
205
1ee32543 206 pvc = kzalloc(sizeof(pvc_device), GFP_ATOMIC);
eb2a2fd9
KH
207#ifdef DEBUG_PVC
208 printk(KERN_DEBUG "add_pvc: allocated pvc %p, frad %p\n", pvc, dev);
209#endif
1da177e4
LT
210 if (!pvc)
211 return NULL;
212
1da177e4 213 pvc->dlci = dlci;
eb2a2fd9 214 pvc->frad = dev;
1da177e4
LT
215 pvc->next = *pvc_p; /* Put it in the chain */
216 *pvc_p = pvc;
217 return pvc;
218}
219
220
221static inline int pvc_is_used(pvc_device *pvc)
222{
eb2a2fd9 223 return pvc->main || pvc->ether;
1da177e4
LT
224}
225
226
227static inline void pvc_carrier(int on, pvc_device *pvc)
228{
229 if (on) {
230 if (pvc->main)
231 if (!netif_carrier_ok(pvc->main))
232 netif_carrier_on(pvc->main);
233 if (pvc->ether)
234 if (!netif_carrier_ok(pvc->ether))
235 netif_carrier_on(pvc->ether);
236 } else {
237 if (pvc->main)
238 if (netif_carrier_ok(pvc->main))
239 netif_carrier_off(pvc->main);
240 if (pvc->ether)
241 if (netif_carrier_ok(pvc->ether))
242 netif_carrier_off(pvc->ether);
243 }
244}
245
246
247static inline void delete_unused_pvcs(hdlc_device *hdlc)
248{
eb2a2fd9 249 pvc_device **pvc_p = &state(hdlc)->first_pvc;
1da177e4
LT
250
251 while (*pvc_p) {
252 if (!pvc_is_used(*pvc_p)) {
253 pvc_device *pvc = *pvc_p;
eb2a2fd9
KH
254#ifdef DEBUG_PVC
255 printk(KERN_DEBUG "freeing unused pvc: %p\n", pvc);
256#endif
1da177e4
LT
257 *pvc_p = pvc->next;
258 kfree(pvc);
259 continue;
260 }
261 pvc_p = &(*pvc_p)->next;
262 }
263}
264
265
266static inline struct net_device** get_dev_p(pvc_device *pvc, int type)
267{
268 if (type == ARPHRD_ETHER)
269 return &pvc->ether;
270 else
271 return &pvc->main;
272}
273
274
1da177e4
LT
275static int fr_hard_header(struct sk_buff **skb_p, u16 dlci)
276{
277 u16 head_len;
278 struct sk_buff *skb = *skb_p;
279
280 switch (skb->protocol) {
abf17ffd 281 case __constant_htons(NLPID_CCITT_ANSI_LMI):
1da177e4
LT
282 head_len = 4;
283 skb_push(skb, head_len);
b3dd65f9 284 skb->data[3] = NLPID_CCITT_ANSI_LMI;
1da177e4
LT
285 break;
286
abf17ffd 287 case __constant_htons(NLPID_CISCO_LMI):
1da177e4
LT
288 head_len = 4;
289 skb_push(skb, head_len);
b3dd65f9 290 skb->data[3] = NLPID_CISCO_LMI;
1da177e4
LT
291 break;
292
abf17ffd 293 case __constant_htons(ETH_P_IP):
b3dd65f9
KH
294 head_len = 4;
295 skb_push(skb, head_len);
296 skb->data[3] = NLPID_IP;
297 break;
298
abf17ffd 299 case __constant_htons(ETH_P_IPV6):
1da177e4
LT
300 head_len = 4;
301 skb_push(skb, head_len);
b3dd65f9 302 skb->data[3] = NLPID_IPV6;
1da177e4
LT
303 break;
304
abf17ffd 305 case __constant_htons(ETH_P_802_3):
1da177e4
LT
306 head_len = 10;
307 if (skb_headroom(skb) < head_len) {
308 struct sk_buff *skb2 = skb_realloc_headroom(skb,
309 head_len);
310 if (!skb2)
311 return -ENOBUFS;
312 dev_kfree_skb(skb);
313 skb = *skb_p = skb2;
314 }
315 skb_push(skb, head_len);
316 skb->data[3] = FR_PAD;
317 skb->data[4] = NLPID_SNAP;
318 skb->data[5] = FR_PAD;
319 skb->data[6] = 0x80;
320 skb->data[7] = 0xC2;
321 skb->data[8] = 0x00;
322 skb->data[9] = 0x07; /* bridged Ethernet frame w/out FCS */
323 break;
324
325 default:
326 head_len = 10;
327 skb_push(skb, head_len);
328 skb->data[3] = FR_PAD;
329 skb->data[4] = NLPID_SNAP;
330 skb->data[5] = FR_PAD;
331 skb->data[6] = FR_PAD;
332 skb->data[7] = FR_PAD;
abf17ffd 333 *(__be16*)(skb->data + 8) = skb->protocol;
1da177e4
LT
334 }
335
336 dlci_to_q922(skb->data, dlci);
337 skb->data[2] = FR_UI;
338 return 0;
339}
340
341
342
343static int pvc_open(struct net_device *dev)
344{
141766cf 345 pvc_device *pvc = dev->priv;
1da177e4 346
eb2a2fd9
KH
347 if ((pvc->frad->flags & IFF_UP) == 0)
348 return -EIO; /* Frad must be UP in order to activate PVC */
1da177e4
LT
349
350 if (pvc->open_count++ == 0) {
eb2a2fd9
KH
351 hdlc_device *hdlc = dev_to_hdlc(pvc->frad);
352 if (state(hdlc)->settings.lmi == LMI_NONE)
353 pvc->state.active = netif_carrier_ok(pvc->frad);
1da177e4
LT
354
355 pvc_carrier(pvc->state.active, pvc);
eb2a2fd9 356 state(hdlc)->dce_changed = 1;
1da177e4
LT
357 }
358 return 0;
359}
360
361
362
363static int pvc_close(struct net_device *dev)
364{
141766cf 365 pvc_device *pvc = dev->priv;
1da177e4
LT
366
367 if (--pvc->open_count == 0) {
eb2a2fd9
KH
368 hdlc_device *hdlc = dev_to_hdlc(pvc->frad);
369 if (state(hdlc)->settings.lmi == LMI_NONE)
1da177e4
LT
370 pvc->state.active = 0;
371
eb2a2fd9
KH
372 if (state(hdlc)->settings.dce) {
373 state(hdlc)->dce_changed = 1;
1da177e4
LT
374 pvc->state.active = 0;
375 }
376 }
377 return 0;
378}
379
380
381
7665a089 382static int pvc_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
1da177e4 383{
141766cf 384 pvc_device *pvc = dev->priv;
1da177e4
LT
385 fr_proto_pvc_info info;
386
387 if (ifr->ifr_settings.type == IF_GET_PROTO) {
388 if (dev->type == ARPHRD_ETHER)
389 ifr->ifr_settings.type = IF_PROTO_FR_ETH_PVC;
390 else
391 ifr->ifr_settings.type = IF_PROTO_FR_PVC;
392
393 if (ifr->ifr_settings.size < sizeof(info)) {
394 /* data size wanted */
395 ifr->ifr_settings.size = sizeof(info);
396 return -ENOBUFS;
397 }
398
399 info.dlci = pvc->dlci;
eb2a2fd9 400 memcpy(info.master, pvc->frad->name, IFNAMSIZ);
1da177e4
LT
401 if (copy_to_user(ifr->ifr_settings.ifs_ifsu.fr_pvc_info,
402 &info, sizeof(info)))
403 return -EFAULT;
404 return 0;
405 }
406
407 return -EINVAL;
408}
409
1da177e4
LT
410static int pvc_xmit(struct sk_buff *skb, struct net_device *dev)
411{
141766cf 412 pvc_device *pvc = dev->priv;
1da177e4
LT
413
414 if (pvc->state.active) {
415 if (dev->type == ARPHRD_ETHER) {
416 int pad = ETH_ZLEN - skb->len;
417 if (pad > 0) { /* Pad the frame with zeros */
418 int len = skb->len;
419 if (skb_tailroom(skb) < pad)
420 if (pskb_expand_head(skb, 0, pad,
421 GFP_ATOMIC)) {
198191c4 422 dev->stats.tx_dropped++;
1da177e4
LT
423 dev_kfree_skb(skb);
424 return 0;
425 }
426 skb_put(skb, pad);
427 memset(skb->data + len, 0, pad);
428 }
429 skb->protocol = __constant_htons(ETH_P_802_3);
430 }
431 if (!fr_hard_header(&skb, pvc->dlci)) {
198191c4
KH
432 dev->stats.tx_bytes += skb->len;
433 dev->stats.tx_packets++;
1da177e4 434 if (pvc->state.fecn) /* TX Congestion counter */
198191c4 435 dev->stats.tx_compressed++;
eb2a2fd9 436 skb->dev = pvc->frad;
1da177e4
LT
437 dev_queue_xmit(skb);
438 return 0;
439 }
440 }
441
198191c4 442 dev->stats.tx_dropped++;
1da177e4
LT
443 dev_kfree_skb(skb);
444 return 0;
445}
446
447
448
449static int pvc_change_mtu(struct net_device *dev, int new_mtu)
450{
451 if ((new_mtu < 68) || (new_mtu > HDLC_MAX_MTU))
452 return -EINVAL;
453 dev->mtu = new_mtu;
454 return 0;
455}
456
457
458
459static inline void fr_log_dlci_active(pvc_device *pvc)
460{
461 printk(KERN_INFO "%s: DLCI %d [%s%s%s]%s %s\n",
eb2a2fd9 462 pvc->frad->name,
1da177e4
LT
463 pvc->dlci,
464 pvc->main ? pvc->main->name : "",
465 pvc->main && pvc->ether ? " " : "",
466 pvc->ether ? pvc->ether->name : "",
467 pvc->state.new ? " new" : "",
468 !pvc->state.exist ? "deleted" :
469 pvc->state.active ? "active" : "inactive");
470}
471
472
473
474static inline u8 fr_lmi_nextseq(u8 x)
475{
476 x++;
477 return x ? x : 1;
478}
479
480
1da177e4
LT
481static void fr_lmi_send(struct net_device *dev, int fullrep)
482{
483 hdlc_device *hdlc = dev_to_hdlc(dev);
484 struct sk_buff *skb;
eb2a2fd9
KH
485 pvc_device *pvc = state(hdlc)->first_pvc;
486 int lmi = state(hdlc)->settings.lmi;
487 int dce = state(hdlc)->settings.dce;
b3dd65f9
KH
488 int len = lmi == LMI_ANSI ? LMI_ANSI_LENGTH : LMI_CCITT_CISCO_LENGTH;
489 int stat_len = (lmi == LMI_CISCO) ? 6 : 3;
1da177e4
LT
490 u8 *data;
491 int i = 0;
492
b3dd65f9 493 if (dce && fullrep) {
eb2a2fd9 494 len += state(hdlc)->dce_pvc_count * (2 + stat_len);
1da177e4
LT
495 if (len > HDLC_MAX_MRU) {
496 printk(KERN_WARNING "%s: Too many PVCs while sending "
497 "LMI full report\n", dev->name);
498 return;
499 }
500 }
501
502 skb = dev_alloc_skb(len);
503 if (!skb) {
504 printk(KERN_WARNING "%s: Memory squeeze on fr_lmi_send()\n",
505 dev->name);
506 return;
507 }
508 memset(skb->data, 0, len);
509 skb_reserve(skb, 4);
b3dd65f9
KH
510 if (lmi == LMI_CISCO) {
511 skb->protocol = __constant_htons(NLPID_CISCO_LMI);
512 fr_hard_header(&skb, LMI_CISCO_DLCI);
513 } else {
514 skb->protocol = __constant_htons(NLPID_CCITT_ANSI_LMI);
515 fr_hard_header(&skb, LMI_CCITT_ANSI_DLCI);
516 }
27a884dc 517 data = skb_tail_pointer(skb);
1da177e4 518 data[i++] = LMI_CALLREF;
b3dd65f9
KH
519 data[i++] = dce ? LMI_STATUS : LMI_STATUS_ENQUIRY;
520 if (lmi == LMI_ANSI)
1da177e4 521 data[i++] = LMI_ANSI_LOCKSHIFT;
b3dd65f9
KH
522 data[i++] = lmi == LMI_CCITT ? LMI_CCITT_REPTYPE :
523 LMI_ANSI_CISCO_REPTYPE;
1da177e4
LT
524 data[i++] = LMI_REPT_LEN;
525 data[i++] = fullrep ? LMI_FULLREP : LMI_INTEGRITY;
b3dd65f9 526 data[i++] = lmi == LMI_CCITT ? LMI_CCITT_ALIVE : LMI_ANSI_CISCO_ALIVE;
1da177e4 527 data[i++] = LMI_INTEG_LEN;
eb2a2fd9
KH
528 data[i++] = state(hdlc)->txseq =
529 fr_lmi_nextseq(state(hdlc)->txseq);
530 data[i++] = state(hdlc)->rxseq;
1da177e4 531
b3dd65f9 532 if (dce && fullrep) {
1da177e4 533 while (pvc) {
b3dd65f9
KH
534 data[i++] = lmi == LMI_CCITT ? LMI_CCITT_PVCSTAT :
535 LMI_ANSI_CISCO_PVCSTAT;
1da177e4
LT
536 data[i++] = stat_len;
537
538 /* LMI start/restart */
eb2a2fd9 539 if (state(hdlc)->reliable && !pvc->state.exist) {
1da177e4
LT
540 pvc->state.exist = pvc->state.new = 1;
541 fr_log_dlci_active(pvc);
542 }
543
544 /* ifconfig PVC up */
545 if (pvc->open_count && !pvc->state.active &&
546 pvc->state.exist && !pvc->state.new) {
547 pvc_carrier(1, pvc);
548 pvc->state.active = 1;
549 fr_log_dlci_active(pvc);
550 }
551
b3dd65f9
KH
552 if (lmi == LMI_CISCO) {
553 data[i] = pvc->dlci >> 8;
554 data[i + 1] = pvc->dlci & 0xFF;
555 } else {
556 data[i] = (pvc->dlci >> 4) & 0x3F;
557 data[i + 1] = ((pvc->dlci << 3) & 0x78) | 0x80;
558 data[i + 2] = 0x80;
559 }
560
561 if (pvc->state.new)
562 data[i + 2] |= 0x08;
563 else if (pvc->state.active)
564 data[i + 2] |= 0x02;
565
1da177e4
LT
566 i += stat_len;
567 pvc = pvc->next;
568 }
569 }
570
571 skb_put(skb, i);
572 skb->priority = TC_PRIO_CONTROL;
573 skb->dev = dev;
c1d2bbe1 574 skb_reset_network_header(skb);
1da177e4
LT
575
576 dev_queue_xmit(skb);
577}
578
579
580
581static void fr_set_link_state(int reliable, struct net_device *dev)
582{
583 hdlc_device *hdlc = dev_to_hdlc(dev);
eb2a2fd9 584 pvc_device *pvc = state(hdlc)->first_pvc;
1da177e4 585
eb2a2fd9 586 state(hdlc)->reliable = reliable;
1da177e4 587 if (reliable) {
c2ce9204 588 netif_dormant_off(dev);
eb2a2fd9
KH
589 state(hdlc)->n391cnt = 0; /* Request full status */
590 state(hdlc)->dce_changed = 1;
1da177e4 591
eb2a2fd9 592 if (state(hdlc)->settings.lmi == LMI_NONE) {
1da177e4
LT
593 while (pvc) { /* Activate all PVCs */
594 pvc_carrier(1, pvc);
595 pvc->state.exist = pvc->state.active = 1;
596 pvc->state.new = 0;
597 pvc = pvc->next;
598 }
599 }
600 } else {
c2ce9204 601 netif_dormant_on(dev);
1da177e4
LT
602 while (pvc) { /* Deactivate all PVCs */
603 pvc_carrier(0, pvc);
604 pvc->state.exist = pvc->state.active = 0;
605 pvc->state.new = 0;
eb2a2fd9 606 if (!state(hdlc)->settings.dce)
b3dd65f9 607 pvc->state.bandwidth = 0;
1da177e4
LT
608 pvc = pvc->next;
609 }
610 }
611}
612
613
1da177e4
LT
614static void fr_timer(unsigned long arg)
615{
616 struct net_device *dev = (struct net_device *)arg;
617 hdlc_device *hdlc = dev_to_hdlc(dev);
618 int i, cnt = 0, reliable;
619 u32 list;
620
eb2a2fd9
KH
621 if (state(hdlc)->settings.dce) {
622 reliable = state(hdlc)->request &&
623 time_before(jiffies, state(hdlc)->last_poll +
624 state(hdlc)->settings.t392 * HZ);
625 state(hdlc)->request = 0;
b3dd65f9 626 } else {
eb2a2fd9
KH
627 state(hdlc)->last_errors <<= 1; /* Shift the list */
628 if (state(hdlc)->request) {
629 if (state(hdlc)->reliable)
1da177e4
LT
630 printk(KERN_INFO "%s: No LMI status reply "
631 "received\n", dev->name);
eb2a2fd9 632 state(hdlc)->last_errors |= 1;
1da177e4
LT
633 }
634
eb2a2fd9
KH
635 list = state(hdlc)->last_errors;
636 for (i = 0; i < state(hdlc)->settings.n393; i++, list >>= 1)
1da177e4
LT
637 cnt += (list & 1); /* errors count */
638
eb2a2fd9 639 reliable = (cnt < state(hdlc)->settings.n392);
1da177e4
LT
640 }
641
eb2a2fd9 642 if (state(hdlc)->reliable != reliable) {
1da177e4
LT
643 printk(KERN_INFO "%s: Link %sreliable\n", dev->name,
644 reliable ? "" : "un");
645 fr_set_link_state(reliable, dev);
646 }
647
eb2a2fd9
KH
648 if (state(hdlc)->settings.dce)
649 state(hdlc)->timer.expires = jiffies +
650 state(hdlc)->settings.t392 * HZ;
1da177e4 651 else {
eb2a2fd9
KH
652 if (state(hdlc)->n391cnt)
653 state(hdlc)->n391cnt--;
1da177e4 654
eb2a2fd9 655 fr_lmi_send(dev, state(hdlc)->n391cnt == 0);
1da177e4 656
eb2a2fd9
KH
657 state(hdlc)->last_poll = jiffies;
658 state(hdlc)->request = 1;
659 state(hdlc)->timer.expires = jiffies +
660 state(hdlc)->settings.t391 * HZ;
1da177e4
LT
661 }
662
eb2a2fd9
KH
663 state(hdlc)->timer.function = fr_timer;
664 state(hdlc)->timer.data = arg;
665 add_timer(&state(hdlc)->timer);
1da177e4
LT
666}
667
668
1da177e4
LT
669static int fr_lmi_recv(struct net_device *dev, struct sk_buff *skb)
670{
671 hdlc_device *hdlc = dev_to_hdlc(dev);
1da177e4 672 pvc_device *pvc;
1da177e4 673 u8 rxseq, txseq;
eb2a2fd9
KH
674 int lmi = state(hdlc)->settings.lmi;
675 int dce = state(hdlc)->settings.dce;
b3dd65f9 676 int stat_len = (lmi == LMI_CISCO) ? 6 : 3, reptype, error, no_ram, i;
1da177e4 677
b3dd65f9
KH
678 if (skb->len < (lmi == LMI_ANSI ? LMI_ANSI_LENGTH :
679 LMI_CCITT_CISCO_LENGTH)) {
1da177e4
LT
680 printk(KERN_INFO "%s: Short LMI frame\n", dev->name);
681 return 1;
682 }
683
b3dd65f9
KH
684 if (skb->data[3] != (lmi == LMI_CISCO ? NLPID_CISCO_LMI :
685 NLPID_CCITT_ANSI_LMI)) {
eb2a2fd9
KH
686 printk(KERN_INFO "%s: Received non-LMI frame with LMI DLCI\n",
687 dev->name);
b3dd65f9
KH
688 return 1;
689 }
690
691 if (skb->data[4] != LMI_CALLREF) {
692 printk(KERN_INFO "%s: Invalid LMI Call reference (0x%02X)\n",
693 dev->name, skb->data[4]);
694 return 1;
695 }
696
697 if (skb->data[5] != (dce ? LMI_STATUS_ENQUIRY : LMI_STATUS)) {
698 printk(KERN_INFO "%s: Invalid LMI Message type (0x%02X)\n",
699 dev->name, skb->data[5]);
1da177e4
LT
700 return 1;
701 }
702
b3dd65f9
KH
703 if (lmi == LMI_ANSI) {
704 if (skb->data[6] != LMI_ANSI_LOCKSHIFT) {
705 printk(KERN_INFO "%s: Not ANSI locking shift in LMI"
706 " message (0x%02X)\n", dev->name, skb->data[6]);
707 return 1;
708 }
709 i = 7;
710 } else
711 i = 6;
1da177e4 712
b3dd65f9
KH
713 if (skb->data[i] != (lmi == LMI_CCITT ? LMI_CCITT_REPTYPE :
714 LMI_ANSI_CISCO_REPTYPE)) {
715 printk(KERN_INFO "%s: Not an LMI Report type IE (0x%02X)\n",
1da177e4
LT
716 dev->name, skb->data[i]);
717 return 1;
718 }
1da177e4 719
b3dd65f9
KH
720 if (skb->data[++i] != LMI_REPT_LEN) {
721 printk(KERN_INFO "%s: Invalid LMI Report type IE length"
722 " (%u)\n", dev->name, skb->data[i]);
723 return 1;
724 }
1da177e4 725
b3dd65f9
KH
726 reptype = skb->data[++i];
727 if (reptype != LMI_INTEGRITY && reptype != LMI_FULLREP) {
728 printk(KERN_INFO "%s: Unsupported LMI Report type (0x%02X)\n",
729 dev->name, reptype);
730 return 1;
731 }
1da177e4 732
b3dd65f9
KH
733 if (skb->data[++i] != (lmi == LMI_CCITT ? LMI_CCITT_ALIVE :
734 LMI_ANSI_CISCO_ALIVE)) {
735 printk(KERN_INFO "%s: Not an LMI Link integrity verification"
736 " IE (0x%02X)\n", dev->name, skb->data[i]);
1da177e4
LT
737 return 1;
738 }
1da177e4 739
b3dd65f9
KH
740 if (skb->data[++i] != LMI_INTEG_LEN) {
741 printk(KERN_INFO "%s: Invalid LMI Link integrity verification"
742 " IE length (%u)\n", dev->name, skb->data[i]);
743 return 1;
744 }
745 i++;
1da177e4 746
eb2a2fd9 747 state(hdlc)->rxseq = skb->data[i++]; /* TX sequence from peer */
1da177e4
LT
748 rxseq = skb->data[i++]; /* Should confirm our sequence */
749
eb2a2fd9 750 txseq = state(hdlc)->txseq;
1da177e4 751
b3dd65f9 752 if (dce)
eb2a2fd9 753 state(hdlc)->last_poll = jiffies;
1da177e4
LT
754
755 error = 0;
eb2a2fd9 756 if (!state(hdlc)->reliable)
1da177e4
LT
757 error = 1;
758
eb2a2fd9
KH
759 if (rxseq == 0 || rxseq != txseq) { /* Ask for full report next time */
760 state(hdlc)->n391cnt = 0;
1da177e4
LT
761 error = 1;
762 }
763
b3dd65f9 764 if (dce) {
eb2a2fd9 765 if (state(hdlc)->fullrep_sent && !error) {
1da177e4 766/* Stop sending full report - the last one has been confirmed by DTE */
eb2a2fd9
KH
767 state(hdlc)->fullrep_sent = 0;
768 pvc = state(hdlc)->first_pvc;
1da177e4
LT
769 while (pvc) {
770 if (pvc->state.new) {
771 pvc->state.new = 0;
772
773/* Tell DTE that new PVC is now active */
eb2a2fd9 774 state(hdlc)->dce_changed = 1;
1da177e4
LT
775 }
776 pvc = pvc->next;
777 }
778 }
779
eb2a2fd9 780 if (state(hdlc)->dce_changed) {
1da177e4 781 reptype = LMI_FULLREP;
eb2a2fd9
KH
782 state(hdlc)->fullrep_sent = 1;
783 state(hdlc)->dce_changed = 0;
1da177e4
LT
784 }
785
eb2a2fd9 786 state(hdlc)->request = 1; /* got request */
1da177e4
LT
787 fr_lmi_send(dev, reptype == LMI_FULLREP ? 1 : 0);
788 return 0;
789 }
790
791 /* DTE */
792
eb2a2fd9 793 state(hdlc)->request = 0; /* got response, no request pending */
1da177e4
LT
794
795 if (error)
796 return 0;
797
798 if (reptype != LMI_FULLREP)
799 return 0;
800
eb2a2fd9 801 pvc = state(hdlc)->first_pvc;
1da177e4
LT
802
803 while (pvc) {
804 pvc->state.deleted = 1;
805 pvc = pvc->next;
806 }
807
808 no_ram = 0;
809 while (skb->len >= i + 2 + stat_len) {
810 u16 dlci;
b3dd65f9 811 u32 bw;
1da177e4
LT
812 unsigned int active, new;
813
b3dd65f9
KH
814 if (skb->data[i] != (lmi == LMI_CCITT ? LMI_CCITT_PVCSTAT :
815 LMI_ANSI_CISCO_PVCSTAT)) {
816 printk(KERN_INFO "%s: Not an LMI PVC status IE"
817 " (0x%02X)\n", dev->name, skb->data[i]);
1da177e4
LT
818 return 1;
819 }
1da177e4 820
b3dd65f9
KH
821 if (skb->data[++i] != stat_len) {
822 printk(KERN_INFO "%s: Invalid LMI PVC status IE length"
823 " (%u)\n", dev->name, skb->data[i]);
1da177e4
LT
824 return 1;
825 }
826 i++;
827
b3dd65f9
KH
828 new = !! (skb->data[i + 2] & 0x08);
829 active = !! (skb->data[i + 2] & 0x02);
830 if (lmi == LMI_CISCO) {
831 dlci = (skb->data[i] << 8) | skb->data[i + 1];
832 bw = (skb->data[i + 3] << 16) |
833 (skb->data[i + 4] << 8) |
834 (skb->data[i + 5]);
835 } else {
836 dlci = ((skb->data[i] & 0x3F) << 4) |
837 ((skb->data[i + 1] & 0x78) >> 3);
838 bw = 0;
839 }
1da177e4
LT
840
841 pvc = add_pvc(dev, dlci);
842
843 if (!pvc && !no_ram) {
844 printk(KERN_WARNING
845 "%s: Memory squeeze on fr_lmi_recv()\n",
846 dev->name);
847 no_ram = 1;
848 }
849
850 if (pvc) {
851 pvc->state.exist = 1;
852 pvc->state.deleted = 0;
853 if (active != pvc->state.active ||
854 new != pvc->state.new ||
b3dd65f9 855 bw != pvc->state.bandwidth ||
1da177e4
LT
856 !pvc->state.exist) {
857 pvc->state.new = new;
858 pvc->state.active = active;
b3dd65f9 859 pvc->state.bandwidth = bw;
1da177e4
LT
860 pvc_carrier(active, pvc);
861 fr_log_dlci_active(pvc);
862 }
863 }
864
865 i += stat_len;
866 }
867
eb2a2fd9 868 pvc = state(hdlc)->first_pvc;
1da177e4
LT
869
870 while (pvc) {
871 if (pvc->state.deleted && pvc->state.exist) {
872 pvc_carrier(0, pvc);
873 pvc->state.active = pvc->state.new = 0;
874 pvc->state.exist = 0;
b3dd65f9 875 pvc->state.bandwidth = 0;
1da177e4
LT
876 fr_log_dlci_active(pvc);
877 }
878 pvc = pvc->next;
879 }
880
881 /* Next full report after N391 polls */
eb2a2fd9 882 state(hdlc)->n391cnt = state(hdlc)->settings.n391;
1da177e4
LT
883
884 return 0;
885}
886
887
1da177e4
LT
888static int fr_rx(struct sk_buff *skb)
889{
eb2a2fd9
KH
890 struct net_device *frad = skb->dev;
891 hdlc_device *hdlc = dev_to_hdlc(frad);
1da177e4
LT
892 fr_hdr *fh = (fr_hdr*)skb->data;
893 u8 *data = skb->data;
894 u16 dlci;
895 pvc_device *pvc;
896 struct net_device *dev = NULL;
897
898 if (skb->len <= 4 || fh->ea1 || data[2] != FR_UI)
899 goto rx_error;
900
901 dlci = q922_to_dlci(skb->data);
902
b3dd65f9 903 if ((dlci == LMI_CCITT_ANSI_DLCI &&
eb2a2fd9
KH
904 (state(hdlc)->settings.lmi == LMI_ANSI ||
905 state(hdlc)->settings.lmi == LMI_CCITT)) ||
b3dd65f9 906 (dlci == LMI_CISCO_DLCI &&
eb2a2fd9
KH
907 state(hdlc)->settings.lmi == LMI_CISCO)) {
908 if (fr_lmi_recv(frad, skb))
b3dd65f9
KH
909 goto rx_error;
910 dev_kfree_skb_any(skb);
911 return NET_RX_SUCCESS;
1da177e4
LT
912 }
913
914 pvc = find_pvc(hdlc, dlci);
915 if (!pvc) {
916#ifdef DEBUG_PKT
917 printk(KERN_INFO "%s: No PVC for received frame's DLCI %d\n",
eb2a2fd9 918 frad->name, dlci);
1da177e4
LT
919#endif
920 dev_kfree_skb_any(skb);
921 return NET_RX_DROP;
922 }
923
924 if (pvc->state.fecn != fh->fecn) {
925#ifdef DEBUG_ECN
eb2a2fd9 926 printk(KERN_DEBUG "%s: DLCI %d FECN O%s\n", frad->name,
1da177e4
LT
927 dlci, fh->fecn ? "N" : "FF");
928#endif
929 pvc->state.fecn ^= 1;
930 }
931
932 if (pvc->state.becn != fh->becn) {
933#ifdef DEBUG_ECN
eb2a2fd9 934 printk(KERN_DEBUG "%s: DLCI %d BECN O%s\n", frad->name,
1da177e4
LT
935 dlci, fh->becn ? "N" : "FF");
936#endif
937 pvc->state.becn ^= 1;
938 }
939
940
941 if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL) {
198191c4 942 frad->stats.rx_dropped++;
1da177e4
LT
943 return NET_RX_DROP;
944 }
945
946 if (data[3] == NLPID_IP) {
947 skb_pull(skb, 4); /* Remove 4-byte header (hdr, UI, NLPID) */
948 dev = pvc->main;
949 skb->protocol = htons(ETH_P_IP);
950
951 } else if (data[3] == NLPID_IPV6) {
952 skb_pull(skb, 4); /* Remove 4-byte header (hdr, UI, NLPID) */
953 dev = pvc->main;
954 skb->protocol = htons(ETH_P_IPV6);
955
956 } else if (skb->len > 10 && data[3] == FR_PAD &&
957 data[4] == NLPID_SNAP && data[5] == FR_PAD) {
abf17ffd
KH
958 u16 oui = ntohs(*(__be16*)(data + 6));
959 u16 pid = ntohs(*(__be16*)(data + 8));
1da177e4
LT
960 skb_pull(skb, 10);
961
962 switch ((((u32)oui) << 16) | pid) {
963 case ETH_P_ARP: /* routed frame with SNAP */
964 case ETH_P_IPX:
965 case ETH_P_IP: /* a long variant */
966 case ETH_P_IPV6:
967 dev = pvc->main;
968 skb->protocol = htons(pid);
969 break;
970
971 case 0x80C20007: /* bridged Ethernet frame */
972 if ((dev = pvc->ether) != NULL)
973 skb->protocol = eth_type_trans(skb, dev);
974 break;
975
976 default:
977 printk(KERN_INFO "%s: Unsupported protocol, OUI=%x "
eb2a2fd9 978 "PID=%x\n", frad->name, oui, pid);
1da177e4
LT
979 dev_kfree_skb_any(skb);
980 return NET_RX_DROP;
981 }
982 } else {
983 printk(KERN_INFO "%s: Unsupported protocol, NLPID=%x "
eb2a2fd9 984 "length = %i\n", frad->name, data[3], skb->len);
1da177e4
LT
985 dev_kfree_skb_any(skb);
986 return NET_RX_DROP;
987 }
988
989 if (dev) {
198191c4
KH
990 dev->stats.rx_packets++; /* PVC traffic */
991 dev->stats.rx_bytes += skb->len;
1da177e4 992 if (pvc->state.becn)
198191c4 993 dev->stats.rx_compressed++;
54364b75 994 skb->dev = dev;
1da177e4
LT
995 netif_rx(skb);
996 return NET_RX_SUCCESS;
997 } else {
998 dev_kfree_skb_any(skb);
999 return NET_RX_DROP;
1000 }
1001
1002 rx_error:
198191c4 1003 frad->stats.rx_errors++; /* Mark error */
1da177e4
LT
1004 dev_kfree_skb_any(skb);
1005 return NET_RX_DROP;
1006}
1007
1008
1009
1010static void fr_start(struct net_device *dev)
1011{
1012 hdlc_device *hdlc = dev_to_hdlc(dev);
1013#ifdef DEBUG_LINK
1014 printk(KERN_DEBUG "fr_start\n");
1015#endif
eb2a2fd9
KH
1016 if (state(hdlc)->settings.lmi != LMI_NONE) {
1017 state(hdlc)->reliable = 0;
1018 state(hdlc)->dce_changed = 1;
1019 state(hdlc)->request = 0;
1020 state(hdlc)->fullrep_sent = 0;
1021 state(hdlc)->last_errors = 0xFFFFFFFF;
1022 state(hdlc)->n391cnt = 0;
1023 state(hdlc)->txseq = state(hdlc)->rxseq = 0;
1024
1025 init_timer(&state(hdlc)->timer);
1da177e4 1026 /* First poll after 1 s */
eb2a2fd9
KH
1027 state(hdlc)->timer.expires = jiffies + HZ;
1028 state(hdlc)->timer.function = fr_timer;
1029 state(hdlc)->timer.data = (unsigned long)dev;
1030 add_timer(&state(hdlc)->timer);
1da177e4
LT
1031 } else
1032 fr_set_link_state(1, dev);
1033}
1034
1035
1da177e4
LT
1036static void fr_stop(struct net_device *dev)
1037{
1038 hdlc_device *hdlc = dev_to_hdlc(dev);
1039#ifdef DEBUG_LINK
1040 printk(KERN_DEBUG "fr_stop\n");
1041#endif
eb2a2fd9
KH
1042 if (state(hdlc)->settings.lmi != LMI_NONE)
1043 del_timer_sync(&state(hdlc)->timer);
1da177e4
LT
1044 fr_set_link_state(0, dev);
1045}
1046
1047
1da177e4
LT
1048static void fr_close(struct net_device *dev)
1049{
1050 hdlc_device *hdlc = dev_to_hdlc(dev);
eb2a2fd9 1051 pvc_device *pvc = state(hdlc)->first_pvc;
1da177e4
LT
1052
1053 while (pvc) { /* Shutdown all PVCs for this FRAD */
1054 if (pvc->main)
1055 dev_close(pvc->main);
1056 if (pvc->ether)
1057 dev_close(pvc->ether);
1058 pvc = pvc->next;
1059 }
1060}
1061
eb2a2fd9
KH
1062
1063static void pvc_setup(struct net_device *dev)
1da177e4
LT
1064{
1065 dev->type = ARPHRD_DLCI;
1066 dev->flags = IFF_POINTOPOINT;
1067 dev->hard_header_len = 10;
1068 dev->addr_len = 2;
1069}
1070
eb2a2fd9 1071static int fr_add_pvc(struct net_device *frad, unsigned int dlci, int type)
1da177e4 1072{
eb2a2fd9 1073 hdlc_device *hdlc = dev_to_hdlc(frad);
141766cf 1074 pvc_device *pvc;
1da177e4
LT
1075 struct net_device *dev;
1076 int result, used;
1da177e4 1077
eb2a2fd9 1078 if ((pvc = add_pvc(frad, dlci)) == NULL) {
1da177e4 1079 printk(KERN_WARNING "%s: Memory squeeze on fr_add_pvc()\n",
eb2a2fd9 1080 frad->name);
1da177e4
LT
1081 return -ENOBUFS;
1082 }
1083
1084 if (*get_dev_p(pvc, type))
1085 return -EEXIST;
1086
1087 used = pvc_is_used(pvc);
1088
1089 if (type == ARPHRD_ETHER)
141766cf 1090 dev = alloc_netdev(0, "pvceth%d", ether_setup);
1da177e4 1091 else
141766cf 1092 dev = alloc_netdev(0, "pvc%d", pvc_setup);
1da177e4
LT
1093
1094 if (!dev) {
1095 printk(KERN_WARNING "%s: Memory squeeze on fr_pvc()\n",
eb2a2fd9 1096 frad->name);
1da177e4
LT
1097 delete_unused_pvcs(hdlc);
1098 return -ENOBUFS;
1099 }
1100
47eaa267
KH
1101 if (type == ARPHRD_ETHER)
1102 random_ether_addr(dev->dev_addr);
1103 else {
abf17ffd 1104 *(__be16*)dev->dev_addr = htons(dlci);
1da177e4
LT
1105 dlci_to_q922(dev->broadcast, dlci);
1106 }
1107 dev->hard_start_xmit = pvc_xmit;
1da177e4
LT
1108 dev->open = pvc_open;
1109 dev->stop = pvc_close;
1110 dev->do_ioctl = pvc_ioctl;
1111 dev->change_mtu = pvc_change_mtu;
1112 dev->mtu = HDLC_MAX_MTU;
1113 dev->tx_queue_len = 0;
141766cf 1114 dev->priv = pvc;
1da177e4
LT
1115
1116 result = dev_alloc_name(dev, dev->name);
1117 if (result < 0) {
1118 free_netdev(dev);
1119 delete_unused_pvcs(hdlc);
1120 return result;
1121 }
1122
1123 if (register_netdevice(dev) != 0) {
1124 free_netdev(dev);
1125 delete_unused_pvcs(hdlc);
1126 return -EIO;
1127 }
1128
1129 dev->destructor = free_netdev;
1130 *get_dev_p(pvc, type) = dev;
1131 if (!used) {
eb2a2fd9
KH
1132 state(hdlc)->dce_changed = 1;
1133 state(hdlc)->dce_pvc_count++;
1da177e4
LT
1134 }
1135 return 0;
1136}
1137
1138
1139
1140static int fr_del_pvc(hdlc_device *hdlc, unsigned int dlci, int type)
1141{
1142 pvc_device *pvc;
1143 struct net_device *dev;
1144
1145 if ((pvc = find_pvc(hdlc, dlci)) == NULL)
1146 return -ENOENT;
1147
1148 if ((dev = *get_dev_p(pvc, type)) == NULL)
1149 return -ENOENT;
1150
1151 if (dev->flags & IFF_UP)
1152 return -EBUSY; /* PVC in use */
1153
1154 unregister_netdevice(dev); /* the destructor will free_netdev(dev) */
1155 *get_dev_p(pvc, type) = NULL;
1156
1157 if (!pvc_is_used(pvc)) {
eb2a2fd9
KH
1158 state(hdlc)->dce_pvc_count--;
1159 state(hdlc)->dce_changed = 1;
1da177e4
LT
1160 }
1161 delete_unused_pvcs(hdlc);
1162 return 0;
1163}
1164
1165
1166
eb2a2fd9 1167static void fr_destroy(struct net_device *frad)
1da177e4 1168{
eb2a2fd9
KH
1169 hdlc_device *hdlc = dev_to_hdlc(frad);
1170 pvc_device *pvc = state(hdlc)->first_pvc;
1171 state(hdlc)->first_pvc = NULL; /* All PVCs destroyed */
1172 state(hdlc)->dce_pvc_count = 0;
1173 state(hdlc)->dce_changed = 1;
1da177e4
LT
1174
1175 while (pvc) {
1176 pvc_device *next = pvc->next;
1177 /* destructors will free_netdev() main and ether */
1178 if (pvc->main)
1179 unregister_netdevice(pvc->main);
1180
1181 if (pvc->ether)
1182 unregister_netdevice(pvc->ether);
1183
1184 kfree(pvc);
1185 pvc = next;
1186 }
1187}
1188
1189
eb2a2fd9
KH
1190static struct hdlc_proto proto = {
1191 .close = fr_close,
1192 .start = fr_start,
1193 .stop = fr_stop,
1194 .detach = fr_destroy,
1195 .ioctl = fr_ioctl,
40d25142 1196 .netif_rx = fr_rx,
eb2a2fd9
KH
1197 .module = THIS_MODULE,
1198};
1199
1da177e4 1200
eb2a2fd9 1201static int fr_ioctl(struct net_device *dev, struct ifreq *ifr)
1da177e4
LT
1202{
1203 fr_proto __user *fr_s = ifr->ifr_settings.ifs_ifsu.fr;
1204 const size_t size = sizeof(fr_proto);
1205 fr_proto new_settings;
1206 hdlc_device *hdlc = dev_to_hdlc(dev);
1207 fr_proto_pvc pvc;
1208 int result;
1209
1210 switch (ifr->ifr_settings.type) {
1211 case IF_GET_PROTO:
eb2a2fd9
KH
1212 if (dev_to_hdlc(dev)->proto != &proto) /* Different proto */
1213 return -EINVAL;
1da177e4
LT
1214 ifr->ifr_settings.type = IF_PROTO_FR;
1215 if (ifr->ifr_settings.size < size) {
1216 ifr->ifr_settings.size = size; /* data size wanted */
1217 return -ENOBUFS;
1218 }
eb2a2fd9 1219 if (copy_to_user(fr_s, &state(hdlc)->settings, size))
1da177e4
LT
1220 return -EFAULT;
1221 return 0;
1222
1223 case IF_PROTO_FR:
1224 if(!capable(CAP_NET_ADMIN))
1225 return -EPERM;
1226
1227 if(dev->flags & IFF_UP)
1228 return -EBUSY;
1229
1230 if (copy_from_user(&new_settings, fr_s, size))
1231 return -EFAULT;
1232
1233 if (new_settings.lmi == LMI_DEFAULT)
1234 new_settings.lmi = LMI_ANSI;
1235
1236 if ((new_settings.lmi != LMI_NONE &&
1237 new_settings.lmi != LMI_ANSI &&
b3dd65f9
KH
1238 new_settings.lmi != LMI_CCITT &&
1239 new_settings.lmi != LMI_CISCO) ||
1da177e4
LT
1240 new_settings.t391 < 1 ||
1241 new_settings.t392 < 2 ||
1242 new_settings.n391 < 1 ||
1243 new_settings.n392 < 1 ||
1244 new_settings.n393 < new_settings.n392 ||
1245 new_settings.n393 > 32 ||
1246 (new_settings.dce != 0 &&
1247 new_settings.dce != 1))
1248 return -EINVAL;
1249
1250 result=hdlc->attach(dev, ENCODING_NRZ,PARITY_CRC16_PR1_CCITT);
1251 if (result)
1252 return result;
1253
eb2a2fd9 1254 if (dev_to_hdlc(dev)->proto != &proto) { /* Different proto */
40d25142 1255 result = attach_hdlc_protocol(dev, &proto,
eb2a2fd9
KH
1256 sizeof(struct frad_state));
1257 if (result)
1258 return result;
1259 state(hdlc)->first_pvc = NULL;
1260 state(hdlc)->dce_pvc_count = 0;
1da177e4 1261 }
eb2a2fd9
KH
1262 memcpy(&state(hdlc)->settings, &new_settings, size);
1263
1da177e4 1264 dev->hard_start_xmit = hdlc->xmit;
1da177e4 1265 dev->type = ARPHRD_FRAD;
1da177e4
LT
1266 return 0;
1267
1268 case IF_PROTO_FR_ADD_PVC:
1269 case IF_PROTO_FR_DEL_PVC:
1270 case IF_PROTO_FR_ADD_ETH_PVC:
1271 case IF_PROTO_FR_DEL_ETH_PVC:
eb2a2fd9
KH
1272 if (dev_to_hdlc(dev)->proto != &proto) /* Different proto */
1273 return -EINVAL;
1274
1da177e4
LT
1275 if(!capable(CAP_NET_ADMIN))
1276 return -EPERM;
1277
1278 if (copy_from_user(&pvc, ifr->ifr_settings.ifs_ifsu.fr_pvc,
1279 sizeof(fr_proto_pvc)))
1280 return -EFAULT;
1281
1282 if (pvc.dlci <= 0 || pvc.dlci >= 1024)
1283 return -EINVAL; /* Only 10 bits, DLCI 0 reserved */
1284
1285 if (ifr->ifr_settings.type == IF_PROTO_FR_ADD_ETH_PVC ||
1286 ifr->ifr_settings.type == IF_PROTO_FR_DEL_ETH_PVC)
1287 result = ARPHRD_ETHER; /* bridged Ethernet device */
1288 else
1289 result = ARPHRD_DLCI;
1290
1291 if (ifr->ifr_settings.type == IF_PROTO_FR_ADD_PVC ||
1292 ifr->ifr_settings.type == IF_PROTO_FR_ADD_ETH_PVC)
1293 return fr_add_pvc(dev, pvc.dlci, result);
1294 else
1295 return fr_del_pvc(hdlc, pvc.dlci, result);
1296 }
1297
1298 return -EINVAL;
1299}
eb2a2fd9
KH
1300
1301
1302static int __init mod_init(void)
1303{
1304 register_hdlc_protocol(&proto);
1305 return 0;
1306}
1307
1308
1309static void __exit mod_exit(void)
1310{
1311 unregister_hdlc_protocol(&proto);
1312}
1313
1314
1315module_init(mod_init);
1316module_exit(mod_exit);
1317
1318MODULE_AUTHOR("Krzysztof Halasa <khc@pm.waw.pl>");
1319MODULE_DESCRIPTION("Frame-Relay protocol support for generic HDLC");
1320MODULE_LICENSE("GPL v2");