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