blob: 51296c2b8b89f0b274ab098dd8cad1d72f26e6bc [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Generic HDLC support routines for Linux
3 * Frame Relay support
4 *
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +02005 * Copyright (C) 1999 - 2006 Krzysztof Halasa <khc@pm.waw.pl>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
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
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +020030 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070034*/
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
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +020055#undef DEBUG_PROTO
56#undef DEBUG_PVC
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +020058#define FR_UI 0x03
59#define FR_PAD 0x00
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +020061#define NLPID_IP 0xCC
62#define NLPID_IPV6 0x8E
63#define NLPID_SNAP 0x80
64#define NLPID_PAD 0x00
65#define NLPID_CCITT_ANSI_LMI 0x08
66#define NLPID_CISCO_LMI 0x09
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
68
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +020069#define LMI_CCITT_ANSI_DLCI 0 /* LMI DLCI */
70#define LMI_CISCO_DLCI 1023
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +020072#define LMI_CALLREF 0x00 /* Call Reference */
73#define LMI_ANSI_LOCKSHIFT 0x95 /* ANSI locking shift */
74#define LMI_ANSI_CISCO_REPTYPE 0x01 /* report type */
75#define LMI_CCITT_REPTYPE 0x51
76#define LMI_ANSI_CISCO_ALIVE 0x03 /* keep alive */
77#define LMI_CCITT_ALIVE 0x53
78#define LMI_ANSI_CISCO_PVCSTAT 0x07 /* PVC status */
79#define LMI_CCITT_PVCSTAT 0x57
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +020081#define LMI_FULLREP 0x00 /* full report */
82#define LMI_INTEGRITY 0x01 /* link integrity report */
83#define LMI_SINGLE 0x02 /* single PVC report */
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
Linus Torvalds1da177e2005-04-16 15:20:36 -070085#define LMI_STATUS_ENQUIRY 0x75
86#define LMI_STATUS 0x7D /* reply */
87
88#define LMI_REPT_LEN 1 /* report type element length */
89#define LMI_INTEG_LEN 2 /* link integrity element length */
90
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +020091#define LMI_CCITT_CISCO_LENGTH 13 /* LMI frame lengths */
92#define LMI_ANSI_LENGTH 14
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
94
95typedef struct {
96#if defined(__LITTLE_ENDIAN_BITFIELD)
97 unsigned ea1: 1;
98 unsigned cr: 1;
99 unsigned dlcih: 6;
100
101 unsigned ea2: 1;
102 unsigned de: 1;
103 unsigned becn: 1;
104 unsigned fecn: 1;
105 unsigned dlcil: 4;
106#else
107 unsigned dlcih: 6;
108 unsigned cr: 1;
109 unsigned ea1: 1;
110
111 unsigned dlcil: 4;
112 unsigned fecn: 1;
113 unsigned becn: 1;
114 unsigned de: 1;
115 unsigned ea2: 1;
116#endif
117}__attribute__ ((packed)) fr_hdr;
118
119
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200120typedef struct pvc_device_struct {
121 struct net_device *frad;
122 struct net_device *main;
123 struct net_device *ether; /* bridged Ethernet interface */
124 struct pvc_device_struct *next; /* Sorted in ascending DLCI order */
125 int dlci;
126 int open_count;
127
128 struct {
129 unsigned int new: 1;
130 unsigned int active: 1;
131 unsigned int exist: 1;
132 unsigned int deleted: 1;
133 unsigned int fecn: 1;
134 unsigned int becn: 1;
135 unsigned int bandwidth; /* Cisco LMI reporting only */
136 }state;
137}pvc_device;
138
Krzysztof Halasa983e2302008-02-01 22:34:30 +0100139struct pvc_desc {
140 struct net_device_stats stats;
141 pvc_device *pvc;
142};
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200143
144struct frad_state {
145 fr_proto settings;
146 pvc_device *first_pvc;
147 int dce_pvc_count;
148
149 struct timer_list timer;
150 unsigned long last_poll;
151 int reliable;
152 int dce_changed;
153 int request;
154 int fullrep_sent;
155 u32 last_errors; /* last errors bit list */
156 u8 n391cnt;
157 u8 txseq; /* TX sequence number */
158 u8 rxseq; /* RX sequence number */
159};
160
161
162static int fr_ioctl(struct net_device *dev, struct ifreq *ifr);
163
164
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165static inline u16 q922_to_dlci(u8 *hdr)
166{
167 return ((hdr[0] & 0xFC) << 2) | ((hdr[1] & 0xF0) >> 4);
168}
169
170
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171static inline void dlci_to_q922(u8 *hdr, u16 dlci)
172{
173 hdr[0] = (dlci >> 2) & 0xFC;
174 hdr[1] = ((dlci << 4) & 0xF0) | 0x01;
175}
176
177
Krzysztof Halasa983e2302008-02-01 22:34:30 +0100178static inline struct frad_state* state(hdlc_device *hdlc)
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200179{
180 return(struct frad_state *)(hdlc->state);
181}
182
Krzysztof Halasa983e2302008-02-01 22:34:30 +0100183static inline struct pvc_desc* pvcdev_to_desc(struct net_device *dev)
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200184{
185 return dev->priv;
186}
187
Krzysztof Halasa983e2302008-02-01 22:34:30 +0100188static inline struct net_device_stats* pvc_get_stats(struct net_device *dev)
189{
190 return &pvcdev_to_desc(dev)->stats;
191}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
193static inline pvc_device* find_pvc(hdlc_device *hdlc, u16 dlci)
194{
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200195 pvc_device *pvc = state(hdlc)->first_pvc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
197 while (pvc) {
198 if (pvc->dlci == dlci)
199 return pvc;
200 if (pvc->dlci > dlci)
201 return NULL; /* the listed is sorted */
202 pvc = pvc->next;
203 }
204
205 return NULL;
206}
207
208
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200209static pvc_device* add_pvc(struct net_device *dev, u16 dlci)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210{
211 hdlc_device *hdlc = dev_to_hdlc(dev);
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200212 pvc_device *pvc, **pvc_p = &state(hdlc)->first_pvc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
214 while (*pvc_p) {
215 if ((*pvc_p)->dlci == dlci)
216 return *pvc_p;
217 if ((*pvc_p)->dlci > dlci)
218 break; /* the list is sorted */
219 pvc_p = &(*pvc_p)->next;
220 }
221
Mariusz Kozlowski1ee32542007-08-10 15:24:50 -0700222 pvc = kzalloc(sizeof(pvc_device), GFP_ATOMIC);
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200223#ifdef DEBUG_PVC
224 printk(KERN_DEBUG "add_pvc: allocated pvc %p, frad %p\n", pvc, dev);
225#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 if (!pvc)
227 return NULL;
228
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 pvc->dlci = dlci;
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200230 pvc->frad = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 pvc->next = *pvc_p; /* Put it in the chain */
232 *pvc_p = pvc;
233 return pvc;
234}
235
236
237static inline int pvc_is_used(pvc_device *pvc)
238{
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200239 return pvc->main || pvc->ether;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240}
241
242
243static inline void pvc_carrier(int on, pvc_device *pvc)
244{
245 if (on) {
246 if (pvc->main)
247 if (!netif_carrier_ok(pvc->main))
248 netif_carrier_on(pvc->main);
249 if (pvc->ether)
250 if (!netif_carrier_ok(pvc->ether))
251 netif_carrier_on(pvc->ether);
252 } else {
253 if (pvc->main)
254 if (netif_carrier_ok(pvc->main))
255 netif_carrier_off(pvc->main);
256 if (pvc->ether)
257 if (netif_carrier_ok(pvc->ether))
258 netif_carrier_off(pvc->ether);
259 }
260}
261
262
263static inline void delete_unused_pvcs(hdlc_device *hdlc)
264{
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200265 pvc_device **pvc_p = &state(hdlc)->first_pvc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
267 while (*pvc_p) {
268 if (!pvc_is_used(*pvc_p)) {
269 pvc_device *pvc = *pvc_p;
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200270#ifdef DEBUG_PVC
271 printk(KERN_DEBUG "freeing unused pvc: %p\n", pvc);
272#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 *pvc_p = pvc->next;
274 kfree(pvc);
275 continue;
276 }
277 pvc_p = &(*pvc_p)->next;
278 }
279}
280
281
282static inline struct net_device** get_dev_p(pvc_device *pvc, int type)
283{
284 if (type == ARPHRD_ETHER)
285 return &pvc->ether;
286 else
287 return &pvc->main;
288}
289
290
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291static int fr_hard_header(struct sk_buff **skb_p, u16 dlci)
292{
293 u16 head_len;
294 struct sk_buff *skb = *skb_p;
295
296 switch (skb->protocol) {
Krzysztof Halasaabf17ff2007-04-27 13:13:33 +0200297 case __constant_htons(NLPID_CCITT_ANSI_LMI):
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +0200298 head_len = 4;
299 skb_push(skb, head_len);
300 skb->data[3] = NLPID_CCITT_ANSI_LMI;
301 break;
302
Krzysztof Halasaabf17ff2007-04-27 13:13:33 +0200303 case __constant_htons(NLPID_CISCO_LMI):
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +0200304 head_len = 4;
305 skb_push(skb, head_len);
306 skb->data[3] = NLPID_CISCO_LMI;
307 break;
308
Krzysztof Halasaabf17ff2007-04-27 13:13:33 +0200309 case __constant_htons(ETH_P_IP):
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 head_len = 4;
311 skb_push(skb, head_len);
312 skb->data[3] = NLPID_IP;
313 break;
314
Krzysztof Halasaabf17ff2007-04-27 13:13:33 +0200315 case __constant_htons(ETH_P_IPV6):
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 head_len = 4;
317 skb_push(skb, head_len);
318 skb->data[3] = NLPID_IPV6;
319 break;
320
Krzysztof Halasaabf17ff2007-04-27 13:13:33 +0200321 case __constant_htons(ETH_P_802_3):
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 head_len = 10;
323 if (skb_headroom(skb) < head_len) {
324 struct sk_buff *skb2 = skb_realloc_headroom(skb,
325 head_len);
326 if (!skb2)
327 return -ENOBUFS;
328 dev_kfree_skb(skb);
329 skb = *skb_p = skb2;
330 }
331 skb_push(skb, head_len);
332 skb->data[3] = FR_PAD;
333 skb->data[4] = NLPID_SNAP;
334 skb->data[5] = FR_PAD;
335 skb->data[6] = 0x80;
336 skb->data[7] = 0xC2;
337 skb->data[8] = 0x00;
338 skb->data[9] = 0x07; /* bridged Ethernet frame w/out FCS */
339 break;
340
341 default:
342 head_len = 10;
343 skb_push(skb, head_len);
344 skb->data[3] = FR_PAD;
345 skb->data[4] = NLPID_SNAP;
346 skb->data[5] = FR_PAD;
347 skb->data[6] = FR_PAD;
348 skb->data[7] = FR_PAD;
Krzysztof Halasaabf17ff2007-04-27 13:13:33 +0200349 *(__be16*)(skb->data + 8) = skb->protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 }
351
352 dlci_to_q922(skb->data, dlci);
353 skb->data[2] = FR_UI;
354 return 0;
355}
356
357
358
359static int pvc_open(struct net_device *dev)
360{
Krzysztof Halasa983e2302008-02-01 22:34:30 +0100361 pvc_device *pvc = pvcdev_to_desc(dev)->pvc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200363 if ((pvc->frad->flags & IFF_UP) == 0)
364 return -EIO; /* Frad must be UP in order to activate PVC */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
366 if (pvc->open_count++ == 0) {
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200367 hdlc_device *hdlc = dev_to_hdlc(pvc->frad);
368 if (state(hdlc)->settings.lmi == LMI_NONE)
369 pvc->state.active = netif_carrier_ok(pvc->frad);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
371 pvc_carrier(pvc->state.active, pvc);
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200372 state(hdlc)->dce_changed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 }
374 return 0;
375}
376
377
378
379static int pvc_close(struct net_device *dev)
380{
Krzysztof Halasa983e2302008-02-01 22:34:30 +0100381 pvc_device *pvc = pvcdev_to_desc(dev)->pvc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
383 if (--pvc->open_count == 0) {
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200384 hdlc_device *hdlc = dev_to_hdlc(pvc->frad);
385 if (state(hdlc)->settings.lmi == LMI_NONE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 pvc->state.active = 0;
387
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200388 if (state(hdlc)->settings.dce) {
389 state(hdlc)->dce_changed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 pvc->state.active = 0;
391 }
392 }
393 return 0;
394}
395
396
397
Adrian Bunk7665a082005-09-09 23:17:28 -0700398static int pvc_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399{
Krzysztof Halasa983e2302008-02-01 22:34:30 +0100400 pvc_device *pvc = pvcdev_to_desc(dev)->pvc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 fr_proto_pvc_info info;
402
403 if (ifr->ifr_settings.type == IF_GET_PROTO) {
404 if (dev->type == ARPHRD_ETHER)
405 ifr->ifr_settings.type = IF_PROTO_FR_ETH_PVC;
406 else
407 ifr->ifr_settings.type = IF_PROTO_FR_PVC;
408
409 if (ifr->ifr_settings.size < sizeof(info)) {
410 /* data size wanted */
411 ifr->ifr_settings.size = sizeof(info);
412 return -ENOBUFS;
413 }
414
415 info.dlci = pvc->dlci;
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200416 memcpy(info.master, pvc->frad->name, IFNAMSIZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 if (copy_to_user(ifr->ifr_settings.ifs_ifsu.fr_pvc_info,
418 &info, sizeof(info)))
419 return -EFAULT;
420 return 0;
421 }
422
423 return -EINVAL;
424}
425
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426static int pvc_xmit(struct sk_buff *skb, struct net_device *dev)
427{
Krzysztof Halasa983e2302008-02-01 22:34:30 +0100428 pvc_device *pvc = pvcdev_to_desc(dev)->pvc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 struct net_device_stats *stats = pvc_get_stats(dev);
430
431 if (pvc->state.active) {
432 if (dev->type == ARPHRD_ETHER) {
433 int pad = ETH_ZLEN - skb->len;
434 if (pad > 0) { /* Pad the frame with zeros */
435 int len = skb->len;
436 if (skb_tailroom(skb) < pad)
437 if (pskb_expand_head(skb, 0, pad,
438 GFP_ATOMIC)) {
439 stats->tx_dropped++;
440 dev_kfree_skb(skb);
441 return 0;
442 }
443 skb_put(skb, pad);
444 memset(skb->data + len, 0, pad);
445 }
446 skb->protocol = __constant_htons(ETH_P_802_3);
447 }
448 if (!fr_hard_header(&skb, pvc->dlci)) {
449 stats->tx_bytes += skb->len;
450 stats->tx_packets++;
451 if (pvc->state.fecn) /* TX Congestion counter */
452 stats->tx_compressed++;
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200453 skb->dev = pvc->frad;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 dev_queue_xmit(skb);
455 return 0;
456 }
457 }
458
459 stats->tx_dropped++;
460 dev_kfree_skb(skb);
461 return 0;
462}
463
464
465
466static int pvc_change_mtu(struct net_device *dev, int new_mtu)
467{
468 if ((new_mtu < 68) || (new_mtu > HDLC_MAX_MTU))
469 return -EINVAL;
470 dev->mtu = new_mtu;
471 return 0;
472}
473
474
475
476static inline void fr_log_dlci_active(pvc_device *pvc)
477{
478 printk(KERN_INFO "%s: DLCI %d [%s%s%s]%s %s\n",
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200479 pvc->frad->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 pvc->dlci,
481 pvc->main ? pvc->main->name : "",
482 pvc->main && pvc->ether ? " " : "",
483 pvc->ether ? pvc->ether->name : "",
484 pvc->state.new ? " new" : "",
485 !pvc->state.exist ? "deleted" :
486 pvc->state.active ? "active" : "inactive");
487}
488
489
490
491static inline u8 fr_lmi_nextseq(u8 x)
492{
493 x++;
494 return x ? x : 1;
495}
496
497
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498static void fr_lmi_send(struct net_device *dev, int fullrep)
499{
500 hdlc_device *hdlc = dev_to_hdlc(dev);
501 struct sk_buff *skb;
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200502 pvc_device *pvc = state(hdlc)->first_pvc;
503 int lmi = state(hdlc)->settings.lmi;
504 int dce = state(hdlc)->settings.dce;
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +0200505 int len = lmi == LMI_ANSI ? LMI_ANSI_LENGTH : LMI_CCITT_CISCO_LENGTH;
506 int stat_len = (lmi == LMI_CISCO) ? 6 : 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 u8 *data;
508 int i = 0;
509
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +0200510 if (dce && fullrep) {
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200511 len += state(hdlc)->dce_pvc_count * (2 + stat_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 if (len > HDLC_MAX_MRU) {
513 printk(KERN_WARNING "%s: Too many PVCs while sending "
514 "LMI full report\n", dev->name);
515 return;
516 }
517 }
518
519 skb = dev_alloc_skb(len);
520 if (!skb) {
521 printk(KERN_WARNING "%s: Memory squeeze on fr_lmi_send()\n",
522 dev->name);
523 return;
524 }
525 memset(skb->data, 0, len);
526 skb_reserve(skb, 4);
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +0200527 if (lmi == LMI_CISCO) {
528 skb->protocol = __constant_htons(NLPID_CISCO_LMI);
529 fr_hard_header(&skb, LMI_CISCO_DLCI);
530 } else {
531 skb->protocol = __constant_htons(NLPID_CCITT_ANSI_LMI);
532 fr_hard_header(&skb, LMI_CCITT_ANSI_DLCI);
533 }
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700534 data = skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 data[i++] = LMI_CALLREF;
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +0200536 data[i++] = dce ? LMI_STATUS : LMI_STATUS_ENQUIRY;
537 if (lmi == LMI_ANSI)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 data[i++] = LMI_ANSI_LOCKSHIFT;
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +0200539 data[i++] = lmi == LMI_CCITT ? LMI_CCITT_REPTYPE :
540 LMI_ANSI_CISCO_REPTYPE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 data[i++] = LMI_REPT_LEN;
542 data[i++] = fullrep ? LMI_FULLREP : LMI_INTEGRITY;
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +0200543 data[i++] = lmi == LMI_CCITT ? LMI_CCITT_ALIVE : LMI_ANSI_CISCO_ALIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 data[i++] = LMI_INTEG_LEN;
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200545 data[i++] = state(hdlc)->txseq =
546 fr_lmi_nextseq(state(hdlc)->txseq);
547 data[i++] = state(hdlc)->rxseq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +0200549 if (dce && fullrep) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 while (pvc) {
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +0200551 data[i++] = lmi == LMI_CCITT ? LMI_CCITT_PVCSTAT :
552 LMI_ANSI_CISCO_PVCSTAT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 data[i++] = stat_len;
554
555 /* LMI start/restart */
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200556 if (state(hdlc)->reliable && !pvc->state.exist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 pvc->state.exist = pvc->state.new = 1;
558 fr_log_dlci_active(pvc);
559 }
560
561 /* ifconfig PVC up */
562 if (pvc->open_count && !pvc->state.active &&
563 pvc->state.exist && !pvc->state.new) {
564 pvc_carrier(1, pvc);
565 pvc->state.active = 1;
566 fr_log_dlci_active(pvc);
567 }
568
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +0200569 if (lmi == LMI_CISCO) {
570 data[i] = pvc->dlci >> 8;
571 data[i + 1] = pvc->dlci & 0xFF;
572 } else {
573 data[i] = (pvc->dlci >> 4) & 0x3F;
574 data[i + 1] = ((pvc->dlci << 3) & 0x78) | 0x80;
575 data[i + 2] = 0x80;
576 }
577
578 if (pvc->state.new)
579 data[i + 2] |= 0x08;
580 else if (pvc->state.active)
581 data[i + 2] |= 0x02;
582
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 i += stat_len;
584 pvc = pvc->next;
585 }
586 }
587
588 skb_put(skb, i);
589 skb->priority = TC_PRIO_CONTROL;
590 skb->dev = dev;
Arnaldo Carvalho de Meloc1d2bbe2007-04-10 20:45:18 -0700591 skb_reset_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592
593 dev_queue_xmit(skb);
594}
595
596
597
598static void fr_set_link_state(int reliable, struct net_device *dev)
599{
600 hdlc_device *hdlc = dev_to_hdlc(dev);
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200601 pvc_device *pvc = state(hdlc)->first_pvc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200603 state(hdlc)->reliable = reliable;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 if (reliable) {
Krzysztof Halasac2ce9202006-07-12 13:46:12 -0700605 netif_dormant_off(dev);
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200606 state(hdlc)->n391cnt = 0; /* Request full status */
607 state(hdlc)->dce_changed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200609 if (state(hdlc)->settings.lmi == LMI_NONE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 while (pvc) { /* Activate all PVCs */
611 pvc_carrier(1, pvc);
612 pvc->state.exist = pvc->state.active = 1;
613 pvc->state.new = 0;
614 pvc = pvc->next;
615 }
616 }
617 } else {
Krzysztof Halasac2ce9202006-07-12 13:46:12 -0700618 netif_dormant_on(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 while (pvc) { /* Deactivate all PVCs */
620 pvc_carrier(0, pvc);
621 pvc->state.exist = pvc->state.active = 0;
622 pvc->state.new = 0;
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200623 if (!state(hdlc)->settings.dce)
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +0200624 pvc->state.bandwidth = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 pvc = pvc->next;
626 }
627 }
628}
629
630
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631static void fr_timer(unsigned long arg)
632{
633 struct net_device *dev = (struct net_device *)arg;
634 hdlc_device *hdlc = dev_to_hdlc(dev);
635 int i, cnt = 0, reliable;
636 u32 list;
637
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200638 if (state(hdlc)->settings.dce) {
639 reliable = state(hdlc)->request &&
640 time_before(jiffies, state(hdlc)->last_poll +
641 state(hdlc)->settings.t392 * HZ);
642 state(hdlc)->request = 0;
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +0200643 } else {
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200644 state(hdlc)->last_errors <<= 1; /* Shift the list */
645 if (state(hdlc)->request) {
646 if (state(hdlc)->reliable)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 printk(KERN_INFO "%s: No LMI status reply "
648 "received\n", dev->name);
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200649 state(hdlc)->last_errors |= 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 }
651
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200652 list = state(hdlc)->last_errors;
653 for (i = 0; i < state(hdlc)->settings.n393; i++, list >>= 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 cnt += (list & 1); /* errors count */
655
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200656 reliable = (cnt < state(hdlc)->settings.n392);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 }
658
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200659 if (state(hdlc)->reliable != reliable) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 printk(KERN_INFO "%s: Link %sreliable\n", dev->name,
661 reliable ? "" : "un");
662 fr_set_link_state(reliable, dev);
663 }
664
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200665 if (state(hdlc)->settings.dce)
666 state(hdlc)->timer.expires = jiffies +
667 state(hdlc)->settings.t392 * HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 else {
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200669 if (state(hdlc)->n391cnt)
670 state(hdlc)->n391cnt--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200672 fr_lmi_send(dev, state(hdlc)->n391cnt == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200674 state(hdlc)->last_poll = jiffies;
675 state(hdlc)->request = 1;
676 state(hdlc)->timer.expires = jiffies +
677 state(hdlc)->settings.t391 * HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 }
679
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200680 state(hdlc)->timer.function = fr_timer;
681 state(hdlc)->timer.data = arg;
682 add_timer(&state(hdlc)->timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683}
684
685
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686static int fr_lmi_recv(struct net_device *dev, struct sk_buff *skb)
687{
688 hdlc_device *hdlc = dev_to_hdlc(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 pvc_device *pvc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 u8 rxseq, txseq;
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200691 int lmi = state(hdlc)->settings.lmi;
692 int dce = state(hdlc)->settings.dce;
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +0200693 int stat_len = (lmi == LMI_CISCO) ? 6 : 3, reptype, error, no_ram, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +0200695 if (skb->len < (lmi == LMI_ANSI ? LMI_ANSI_LENGTH :
696 LMI_CCITT_CISCO_LENGTH)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 printk(KERN_INFO "%s: Short LMI frame\n", dev->name);
698 return 1;
699 }
700
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +0200701 if (skb->data[3] != (lmi == LMI_CISCO ? NLPID_CISCO_LMI :
702 NLPID_CCITT_ANSI_LMI)) {
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200703 printk(KERN_INFO "%s: Received non-LMI frame with LMI DLCI\n",
704 dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 return 1;
706 }
707
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +0200708 if (skb->data[4] != LMI_CALLREF) {
709 printk(KERN_INFO "%s: Invalid LMI Call reference (0x%02X)\n",
710 dev->name, skb->data[4]);
711 return 1;
712 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +0200714 if (skb->data[5] != (dce ? LMI_STATUS_ENQUIRY : LMI_STATUS)) {
715 printk(KERN_INFO "%s: Invalid LMI Message type (0x%02X)\n",
716 dev->name, skb->data[5]);
717 return 1;
718 }
719
720 if (lmi == LMI_ANSI) {
721 if (skb->data[6] != LMI_ANSI_LOCKSHIFT) {
722 printk(KERN_INFO "%s: Not ANSI locking shift in LMI"
723 " message (0x%02X)\n", dev->name, skb->data[6]);
724 return 1;
725 }
726 i = 7;
727 } else
728 i = 6;
729
730 if (skb->data[i] != (lmi == LMI_CCITT ? LMI_CCITT_REPTYPE :
731 LMI_ANSI_CISCO_REPTYPE)) {
732 printk(KERN_INFO "%s: Not an LMI Report type IE (0x%02X)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 dev->name, skb->data[i]);
734 return 1;
735 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +0200737 if (skb->data[++i] != LMI_REPT_LEN) {
738 printk(KERN_INFO "%s: Invalid LMI Report type IE length"
739 " (%u)\n", dev->name, skb->data[i]);
740 return 1;
741 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +0200743 reptype = skb->data[++i];
744 if (reptype != LMI_INTEGRITY && reptype != LMI_FULLREP) {
745 printk(KERN_INFO "%s: Unsupported LMI Report type (0x%02X)\n",
746 dev->name, reptype);
747 return 1;
748 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +0200750 if (skb->data[++i] != (lmi == LMI_CCITT ? LMI_CCITT_ALIVE :
751 LMI_ANSI_CISCO_ALIVE)) {
752 printk(KERN_INFO "%s: Not an LMI Link integrity verification"
753 " IE (0x%02X)\n", dev->name, skb->data[i]);
754 return 1;
755 }
756
757 if (skb->data[++i] != LMI_INTEG_LEN) {
758 printk(KERN_INFO "%s: Invalid LMI Link integrity verification"
759 " IE length (%u)\n", dev->name, skb->data[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 return 1;
761 }
762 i++;
763
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200764 state(hdlc)->rxseq = skb->data[i++]; /* TX sequence from peer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 rxseq = skb->data[i++]; /* Should confirm our sequence */
766
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200767 txseq = state(hdlc)->txseq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +0200769 if (dce)
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200770 state(hdlc)->last_poll = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
772 error = 0;
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200773 if (!state(hdlc)->reliable)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 error = 1;
775
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200776 if (rxseq == 0 || rxseq != txseq) { /* Ask for full report next time */
777 state(hdlc)->n391cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 error = 1;
779 }
780
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +0200781 if (dce) {
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200782 if (state(hdlc)->fullrep_sent && !error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783/* Stop sending full report - the last one has been confirmed by DTE */
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200784 state(hdlc)->fullrep_sent = 0;
785 pvc = state(hdlc)->first_pvc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 while (pvc) {
787 if (pvc->state.new) {
788 pvc->state.new = 0;
789
790/* Tell DTE that new PVC is now active */
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200791 state(hdlc)->dce_changed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 }
793 pvc = pvc->next;
794 }
795 }
796
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200797 if (state(hdlc)->dce_changed) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 reptype = LMI_FULLREP;
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200799 state(hdlc)->fullrep_sent = 1;
800 state(hdlc)->dce_changed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 }
802
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200803 state(hdlc)->request = 1; /* got request */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 fr_lmi_send(dev, reptype == LMI_FULLREP ? 1 : 0);
805 return 0;
806 }
807
808 /* DTE */
809
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200810 state(hdlc)->request = 0; /* got response, no request pending */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811
812 if (error)
813 return 0;
814
815 if (reptype != LMI_FULLREP)
816 return 0;
817
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200818 pvc = state(hdlc)->first_pvc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819
820 while (pvc) {
821 pvc->state.deleted = 1;
822 pvc = pvc->next;
823 }
824
825 no_ram = 0;
826 while (skb->len >= i + 2 + stat_len) {
827 u16 dlci;
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +0200828 u32 bw;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 unsigned int active, new;
830
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +0200831 if (skb->data[i] != (lmi == LMI_CCITT ? LMI_CCITT_PVCSTAT :
832 LMI_ANSI_CISCO_PVCSTAT)) {
833 printk(KERN_INFO "%s: Not an LMI PVC status IE"
834 " (0x%02X)\n", dev->name, skb->data[i]);
835 return 1;
836 }
837
838 if (skb->data[++i] != stat_len) {
839 printk(KERN_INFO "%s: Invalid LMI PVC status IE length"
840 " (%u)\n", dev->name, skb->data[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 return 1;
842 }
843 i++;
844
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +0200845 new = !! (skb->data[i + 2] & 0x08);
846 active = !! (skb->data[i + 2] & 0x02);
847 if (lmi == LMI_CISCO) {
848 dlci = (skb->data[i] << 8) | skb->data[i + 1];
849 bw = (skb->data[i + 3] << 16) |
850 (skb->data[i + 4] << 8) |
851 (skb->data[i + 5]);
852 } else {
853 dlci = ((skb->data[i] & 0x3F) << 4) |
854 ((skb->data[i + 1] & 0x78) >> 3);
855 bw = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857
858 pvc = add_pvc(dev, dlci);
859
860 if (!pvc && !no_ram) {
861 printk(KERN_WARNING
862 "%s: Memory squeeze on fr_lmi_recv()\n",
863 dev->name);
864 no_ram = 1;
865 }
866
867 if (pvc) {
868 pvc->state.exist = 1;
869 pvc->state.deleted = 0;
870 if (active != pvc->state.active ||
871 new != pvc->state.new ||
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +0200872 bw != pvc->state.bandwidth ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 !pvc->state.exist) {
874 pvc->state.new = new;
875 pvc->state.active = active;
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +0200876 pvc->state.bandwidth = bw;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 pvc_carrier(active, pvc);
878 fr_log_dlci_active(pvc);
879 }
880 }
881
882 i += stat_len;
883 }
884
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200885 pvc = state(hdlc)->first_pvc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886
887 while (pvc) {
888 if (pvc->state.deleted && pvc->state.exist) {
889 pvc_carrier(0, pvc);
890 pvc->state.active = pvc->state.new = 0;
891 pvc->state.exist = 0;
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +0200892 pvc->state.bandwidth = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 fr_log_dlci_active(pvc);
894 }
895 pvc = pvc->next;
896 }
897
898 /* Next full report after N391 polls */
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200899 state(hdlc)->n391cnt = state(hdlc)->settings.n391;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900
901 return 0;
902}
903
904
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905static int fr_rx(struct sk_buff *skb)
906{
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200907 struct net_device *frad = skb->dev;
908 hdlc_device *hdlc = dev_to_hdlc(frad);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 fr_hdr *fh = (fr_hdr*)skb->data;
910 u8 *data = skb->data;
911 u16 dlci;
912 pvc_device *pvc;
913 struct net_device *dev = NULL;
914
915 if (skb->len <= 4 || fh->ea1 || data[2] != FR_UI)
916 goto rx_error;
917
918 dlci = q922_to_dlci(skb->data);
919
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +0200920 if ((dlci == LMI_CCITT_ANSI_DLCI &&
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200921 (state(hdlc)->settings.lmi == LMI_ANSI ||
922 state(hdlc)->settings.lmi == LMI_CCITT)) ||
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +0200923 (dlci == LMI_CISCO_DLCI &&
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200924 state(hdlc)->settings.lmi == LMI_CISCO)) {
925 if (fr_lmi_recv(frad, skb))
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +0200926 goto rx_error;
927 dev_kfree_skb_any(skb);
928 return NET_RX_SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 }
930
931 pvc = find_pvc(hdlc, dlci);
932 if (!pvc) {
933#ifdef DEBUG_PKT
934 printk(KERN_INFO "%s: No PVC for received frame's DLCI %d\n",
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200935 frad->name, dlci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936#endif
937 dev_kfree_skb_any(skb);
938 return NET_RX_DROP;
939 }
940
941 if (pvc->state.fecn != fh->fecn) {
942#ifdef DEBUG_ECN
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200943 printk(KERN_DEBUG "%s: DLCI %d FECN O%s\n", frad->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 dlci, fh->fecn ? "N" : "FF");
945#endif
946 pvc->state.fecn ^= 1;
947 }
948
949 if (pvc->state.becn != fh->becn) {
950#ifdef DEBUG_ECN
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200951 printk(KERN_DEBUG "%s: DLCI %d BECN O%s\n", frad->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 dlci, fh->becn ? "N" : "FF");
953#endif
954 pvc->state.becn ^= 1;
955 }
956
957
958 if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL) {
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200959 dev_to_desc(frad)->stats.rx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 return NET_RX_DROP;
961 }
962
963 if (data[3] == NLPID_IP) {
964 skb_pull(skb, 4); /* Remove 4-byte header (hdr, UI, NLPID) */
965 dev = pvc->main;
966 skb->protocol = htons(ETH_P_IP);
967
968 } else if (data[3] == NLPID_IPV6) {
969 skb_pull(skb, 4); /* Remove 4-byte header (hdr, UI, NLPID) */
970 dev = pvc->main;
971 skb->protocol = htons(ETH_P_IPV6);
972
973 } else if (skb->len > 10 && data[3] == FR_PAD &&
974 data[4] == NLPID_SNAP && data[5] == FR_PAD) {
Krzysztof Halasaabf17ff2007-04-27 13:13:33 +0200975 u16 oui = ntohs(*(__be16*)(data + 6));
976 u16 pid = ntohs(*(__be16*)(data + 8));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 skb_pull(skb, 10);
978
979 switch ((((u32)oui) << 16) | pid) {
980 case ETH_P_ARP: /* routed frame with SNAP */
981 case ETH_P_IPX:
982 case ETH_P_IP: /* a long variant */
983 case ETH_P_IPV6:
984 dev = pvc->main;
985 skb->protocol = htons(pid);
986 break;
987
988 case 0x80C20007: /* bridged Ethernet frame */
989 if ((dev = pvc->ether) != NULL)
990 skb->protocol = eth_type_trans(skb, dev);
991 break;
992
993 default:
994 printk(KERN_INFO "%s: Unsupported protocol, OUI=%x "
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200995 "PID=%x\n", frad->name, oui, pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 dev_kfree_skb_any(skb);
997 return NET_RX_DROP;
998 }
999 } else {
1000 printk(KERN_INFO "%s: Unsupported protocol, NLPID=%x "
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +02001001 "length = %i\n", frad->name, data[3], skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 dev_kfree_skb_any(skb);
1003 return NET_RX_DROP;
1004 }
1005
1006 if (dev) {
1007 struct net_device_stats *stats = pvc_get_stats(dev);
1008 stats->rx_packets++; /* PVC traffic */
1009 stats->rx_bytes += skb->len;
1010 if (pvc->state.becn)
1011 stats->rx_compressed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 netif_rx(skb);
1013 return NET_RX_SUCCESS;
1014 } else {
1015 dev_kfree_skb_any(skb);
1016 return NET_RX_DROP;
1017 }
1018
1019 rx_error:
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +02001020 dev_to_desc(frad)->stats.rx_errors++; /* Mark error */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 dev_kfree_skb_any(skb);
1022 return NET_RX_DROP;
1023}
1024
1025
1026
1027static void fr_start(struct net_device *dev)
1028{
1029 hdlc_device *hdlc = dev_to_hdlc(dev);
1030#ifdef DEBUG_LINK
1031 printk(KERN_DEBUG "fr_start\n");
1032#endif
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +02001033 if (state(hdlc)->settings.lmi != LMI_NONE) {
1034 state(hdlc)->reliable = 0;
1035 state(hdlc)->dce_changed = 1;
1036 state(hdlc)->request = 0;
1037 state(hdlc)->fullrep_sent = 0;
1038 state(hdlc)->last_errors = 0xFFFFFFFF;
1039 state(hdlc)->n391cnt = 0;
1040 state(hdlc)->txseq = state(hdlc)->rxseq = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +02001042 init_timer(&state(hdlc)->timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 /* First poll after 1 s */
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +02001044 state(hdlc)->timer.expires = jiffies + HZ;
1045 state(hdlc)->timer.function = fr_timer;
1046 state(hdlc)->timer.data = (unsigned long)dev;
1047 add_timer(&state(hdlc)->timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 } else
1049 fr_set_link_state(1, dev);
1050}
1051
1052
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053static void fr_stop(struct net_device *dev)
1054{
1055 hdlc_device *hdlc = dev_to_hdlc(dev);
1056#ifdef DEBUG_LINK
1057 printk(KERN_DEBUG "fr_stop\n");
1058#endif
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +02001059 if (state(hdlc)->settings.lmi != LMI_NONE)
1060 del_timer_sync(&state(hdlc)->timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 fr_set_link_state(0, dev);
1062}
1063
1064
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065static void fr_close(struct net_device *dev)
1066{
1067 hdlc_device *hdlc = dev_to_hdlc(dev);
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +02001068 pvc_device *pvc = state(hdlc)->first_pvc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069
1070 while (pvc) { /* Shutdown all PVCs for this FRAD */
1071 if (pvc->main)
1072 dev_close(pvc->main);
1073 if (pvc->ether)
1074 dev_close(pvc->ether);
1075 pvc = pvc->next;
1076 }
1077}
1078
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +02001079
1080static void pvc_setup(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081{
1082 dev->type = ARPHRD_DLCI;
1083 dev->flags = IFF_POINTOPOINT;
1084 dev->hard_header_len = 10;
1085 dev->addr_len = 2;
1086}
1087
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +02001088static int fr_add_pvc(struct net_device *frad, unsigned int dlci, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089{
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +02001090 hdlc_device *hdlc = dev_to_hdlc(frad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 pvc_device *pvc = NULL;
1092 struct net_device *dev;
1093 int result, used;
1094 char * prefix = "pvc%d";
1095
1096 if (type == ARPHRD_ETHER)
1097 prefix = "pvceth%d";
1098
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +02001099 if ((pvc = add_pvc(frad, dlci)) == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 printk(KERN_WARNING "%s: Memory squeeze on fr_add_pvc()\n",
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +02001101 frad->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 return -ENOBUFS;
1103 }
1104
1105 if (*get_dev_p(pvc, type))
1106 return -EEXIST;
1107
1108 used = pvc_is_used(pvc);
1109
1110 if (type == ARPHRD_ETHER)
Krzysztof Halasa983e2302008-02-01 22:34:30 +01001111 dev = alloc_netdev(sizeof(struct pvc_desc), "pvceth%d",
1112 ether_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 else
Krzysztof Halasa983e2302008-02-01 22:34:30 +01001114 dev = alloc_netdev(sizeof(struct pvc_desc), "pvc%d", pvc_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115
1116 if (!dev) {
1117 printk(KERN_WARNING "%s: Memory squeeze on fr_pvc()\n",
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +02001118 frad->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 delete_unused_pvcs(hdlc);
1120 return -ENOBUFS;
1121 }
1122
1123 if (type == ARPHRD_ETHER) {
1124 memcpy(dev->dev_addr, "\x00\x01", 2);
1125 get_random_bytes(dev->dev_addr + 2, ETH_ALEN - 2);
1126 } else {
Krzysztof Halasaabf17ff2007-04-27 13:13:33 +02001127 *(__be16*)dev->dev_addr = htons(dlci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 dlci_to_q922(dev->broadcast, dlci);
1129 }
1130 dev->hard_start_xmit = pvc_xmit;
1131 dev->get_stats = pvc_get_stats;
1132 dev->open = pvc_open;
1133 dev->stop = pvc_close;
1134 dev->do_ioctl = pvc_ioctl;
1135 dev->change_mtu = pvc_change_mtu;
1136 dev->mtu = HDLC_MAX_MTU;
1137 dev->tx_queue_len = 0;
Krzysztof Halasa983e2302008-02-01 22:34:30 +01001138 pvcdev_to_desc(dev)->pvc = pvc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139
1140 result = dev_alloc_name(dev, dev->name);
1141 if (result < 0) {
1142 free_netdev(dev);
1143 delete_unused_pvcs(hdlc);
1144 return result;
1145 }
1146
1147 if (register_netdevice(dev) != 0) {
1148 free_netdev(dev);
1149 delete_unused_pvcs(hdlc);
1150 return -EIO;
1151 }
1152
1153 dev->destructor = free_netdev;
1154 *get_dev_p(pvc, type) = dev;
1155 if (!used) {
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +02001156 state(hdlc)->dce_changed = 1;
1157 state(hdlc)->dce_pvc_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 }
1159 return 0;
1160}
1161
1162
1163
1164static int fr_del_pvc(hdlc_device *hdlc, unsigned int dlci, int type)
1165{
1166 pvc_device *pvc;
1167 struct net_device *dev;
1168
1169 if ((pvc = find_pvc(hdlc, dlci)) == NULL)
1170 return -ENOENT;
1171
1172 if ((dev = *get_dev_p(pvc, type)) == NULL)
1173 return -ENOENT;
1174
1175 if (dev->flags & IFF_UP)
1176 return -EBUSY; /* PVC in use */
1177
1178 unregister_netdevice(dev); /* the destructor will free_netdev(dev) */
1179 *get_dev_p(pvc, type) = NULL;
1180
1181 if (!pvc_is_used(pvc)) {
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +02001182 state(hdlc)->dce_pvc_count--;
1183 state(hdlc)->dce_changed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 }
1185 delete_unused_pvcs(hdlc);
1186 return 0;
1187}
1188
1189
1190
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +02001191static void fr_destroy(struct net_device *frad)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192{
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +02001193 hdlc_device *hdlc = dev_to_hdlc(frad);
1194 pvc_device *pvc = state(hdlc)->first_pvc;
1195 state(hdlc)->first_pvc = NULL; /* All PVCs destroyed */
1196 state(hdlc)->dce_pvc_count = 0;
1197 state(hdlc)->dce_changed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198
1199 while (pvc) {
1200 pvc_device *next = pvc->next;
1201 /* destructors will free_netdev() main and ether */
1202 if (pvc->main)
1203 unregister_netdevice(pvc->main);
1204
1205 if (pvc->ether)
1206 unregister_netdevice(pvc->ether);
1207
1208 kfree(pvc);
1209 pvc = next;
1210 }
1211}
1212
1213
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +02001214static struct hdlc_proto proto = {
1215 .close = fr_close,
1216 .start = fr_start,
1217 .stop = fr_stop,
1218 .detach = fr_destroy,
1219 .ioctl = fr_ioctl,
1220 .module = THIS_MODULE,
1221};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +02001223
1224static int fr_ioctl(struct net_device *dev, struct ifreq *ifr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225{
1226 fr_proto __user *fr_s = ifr->ifr_settings.ifs_ifsu.fr;
1227 const size_t size = sizeof(fr_proto);
1228 fr_proto new_settings;
1229 hdlc_device *hdlc = dev_to_hdlc(dev);
1230 fr_proto_pvc pvc;
1231 int result;
1232
1233 switch (ifr->ifr_settings.type) {
1234 case IF_GET_PROTO:
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +02001235 if (dev_to_hdlc(dev)->proto != &proto) /* Different proto */
1236 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 ifr->ifr_settings.type = IF_PROTO_FR;
1238 if (ifr->ifr_settings.size < size) {
1239 ifr->ifr_settings.size = size; /* data size wanted */
1240 return -ENOBUFS;
1241 }
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +02001242 if (copy_to_user(fr_s, &state(hdlc)->settings, size))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 return -EFAULT;
1244 return 0;
1245
1246 case IF_PROTO_FR:
1247 if(!capable(CAP_NET_ADMIN))
1248 return -EPERM;
1249
1250 if(dev->flags & IFF_UP)
1251 return -EBUSY;
1252
1253 if (copy_from_user(&new_settings, fr_s, size))
1254 return -EFAULT;
1255
1256 if (new_settings.lmi == LMI_DEFAULT)
1257 new_settings.lmi = LMI_ANSI;
1258
1259 if ((new_settings.lmi != LMI_NONE &&
1260 new_settings.lmi != LMI_ANSI &&
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +02001261 new_settings.lmi != LMI_CCITT &&
1262 new_settings.lmi != LMI_CISCO) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 new_settings.t391 < 1 ||
1264 new_settings.t392 < 2 ||
1265 new_settings.n391 < 1 ||
1266 new_settings.n392 < 1 ||
1267 new_settings.n393 < new_settings.n392 ||
1268 new_settings.n393 > 32 ||
1269 (new_settings.dce != 0 &&
1270 new_settings.dce != 1))
1271 return -EINVAL;
1272
1273 result=hdlc->attach(dev, ENCODING_NRZ,PARITY_CRC16_PR1_CCITT);
1274 if (result)
1275 return result;
1276
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +02001277 if (dev_to_hdlc(dev)->proto != &proto) { /* Different proto */
1278 result = attach_hdlc_protocol(dev, &proto, fr_rx,
1279 sizeof(struct frad_state));
1280 if (result)
1281 return result;
1282 state(hdlc)->first_pvc = NULL;
1283 state(hdlc)->dce_pvc_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 }
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +02001285 memcpy(&state(hdlc)->settings, &new_settings, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 dev->hard_start_xmit = hdlc->xmit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 dev->type = ARPHRD_FRAD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 return 0;
1290
1291 case IF_PROTO_FR_ADD_PVC:
1292 case IF_PROTO_FR_DEL_PVC:
1293 case IF_PROTO_FR_ADD_ETH_PVC:
1294 case IF_PROTO_FR_DEL_ETH_PVC:
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +02001295 if (dev_to_hdlc(dev)->proto != &proto) /* Different proto */
1296 return -EINVAL;
1297
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 if(!capable(CAP_NET_ADMIN))
1299 return -EPERM;
1300
1301 if (copy_from_user(&pvc, ifr->ifr_settings.ifs_ifsu.fr_pvc,
1302 sizeof(fr_proto_pvc)))
1303 return -EFAULT;
1304
1305 if (pvc.dlci <= 0 || pvc.dlci >= 1024)
1306 return -EINVAL; /* Only 10 bits, DLCI 0 reserved */
1307
1308 if (ifr->ifr_settings.type == IF_PROTO_FR_ADD_ETH_PVC ||
1309 ifr->ifr_settings.type == IF_PROTO_FR_DEL_ETH_PVC)
1310 result = ARPHRD_ETHER; /* bridged Ethernet device */
1311 else
1312 result = ARPHRD_DLCI;
1313
1314 if (ifr->ifr_settings.type == IF_PROTO_FR_ADD_PVC ||
1315 ifr->ifr_settings.type == IF_PROTO_FR_ADD_ETH_PVC)
1316 return fr_add_pvc(dev, pvc.dlci, result);
1317 else
1318 return fr_del_pvc(hdlc, pvc.dlci, result);
1319 }
1320
1321 return -EINVAL;
1322}
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +02001323
1324
1325static int __init mod_init(void)
1326{
1327 register_hdlc_protocol(&proto);
1328 return 0;
1329}
1330
1331
1332static void __exit mod_exit(void)
1333{
1334 unregister_hdlc_protocol(&proto);
1335}
1336
1337
1338module_init(mod_init);
1339module_exit(mod_exit);
1340
1341MODULE_AUTHOR("Krzysztof Halasa <khc@pm.waw.pl>");
1342MODULE_DESCRIPTION("Frame-Relay protocol support for generic HDLC");
1343MODULE_LICENSE("GPL v2");