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