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