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