blob: 071a64cacd5c29cdedfc959c922bea885a7a7cc2 [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
139
140struct frad_state {
141 fr_proto settings;
142 pvc_device *first_pvc;
143 int dce_pvc_count;
144
145 struct timer_list timer;
146 unsigned long last_poll;
147 int reliable;
148 int dce_changed;
149 int request;
150 int fullrep_sent;
151 u32 last_errors; /* last errors bit list */
152 u8 n391cnt;
153 u8 txseq; /* TX sequence number */
154 u8 rxseq; /* RX sequence number */
155};
156
157
158static int fr_ioctl(struct net_device *dev, struct ifreq *ifr);
159
160
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161static inline u16 q922_to_dlci(u8 *hdr)
162{
163 return ((hdr[0] & 0xFC) << 2) | ((hdr[1] & 0xF0) >> 4);
164}
165
166
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167static inline void dlci_to_q922(u8 *hdr, u16 dlci)
168{
169 hdr[0] = (dlci >> 2) & 0xFC;
170 hdr[1] = ((dlci << 4) & 0xF0) | 0x01;
171}
172
173
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200174static inline struct frad_state * state(hdlc_device *hdlc)
175{
176 return(struct frad_state *)(hdlc->state);
177}
178
179
180static __inline__ pvc_device* dev_to_pvc(struct net_device *dev)
181{
182 return dev->priv;
183}
184
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
186static inline pvc_device* find_pvc(hdlc_device *hdlc, u16 dlci)
187{
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200188 pvc_device *pvc = state(hdlc)->first_pvc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
190 while (pvc) {
191 if (pvc->dlci == dlci)
192 return pvc;
193 if (pvc->dlci > dlci)
194 return NULL; /* the listed is sorted */
195 pvc = pvc->next;
196 }
197
198 return NULL;
199}
200
201
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200202static pvc_device* add_pvc(struct net_device *dev, u16 dlci)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203{
204 hdlc_device *hdlc = dev_to_hdlc(dev);
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200205 pvc_device *pvc, **pvc_p = &state(hdlc)->first_pvc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
207 while (*pvc_p) {
208 if ((*pvc_p)->dlci == dlci)
209 return *pvc_p;
210 if ((*pvc_p)->dlci > dlci)
211 break; /* the list is sorted */
212 pvc_p = &(*pvc_p)->next;
213 }
214
Mariusz Kozlowski1ee32542007-08-10 15:24:50 -0700215 pvc = kzalloc(sizeof(pvc_device), GFP_ATOMIC);
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200216#ifdef DEBUG_PVC
217 printk(KERN_DEBUG "add_pvc: allocated pvc %p, frad %p\n", pvc, dev);
218#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 if (!pvc)
220 return NULL;
221
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 pvc->dlci = dlci;
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200223 pvc->frad = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 pvc->next = *pvc_p; /* Put it in the chain */
225 *pvc_p = pvc;
226 return pvc;
227}
228
229
230static inline int pvc_is_used(pvc_device *pvc)
231{
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200232 return pvc->main || pvc->ether;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233}
234
235
236static inline void pvc_carrier(int on, pvc_device *pvc)
237{
238 if (on) {
239 if (pvc->main)
240 if (!netif_carrier_ok(pvc->main))
241 netif_carrier_on(pvc->main);
242 if (pvc->ether)
243 if (!netif_carrier_ok(pvc->ether))
244 netif_carrier_on(pvc->ether);
245 } else {
246 if (pvc->main)
247 if (netif_carrier_ok(pvc->main))
248 netif_carrier_off(pvc->main);
249 if (pvc->ether)
250 if (netif_carrier_ok(pvc->ether))
251 netif_carrier_off(pvc->ether);
252 }
253}
254
255
256static inline void delete_unused_pvcs(hdlc_device *hdlc)
257{
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200258 pvc_device **pvc_p = &state(hdlc)->first_pvc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
260 while (*pvc_p) {
261 if (!pvc_is_used(*pvc_p)) {
262 pvc_device *pvc = *pvc_p;
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200263#ifdef DEBUG_PVC
264 printk(KERN_DEBUG "freeing unused pvc: %p\n", pvc);
265#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 *pvc_p = pvc->next;
267 kfree(pvc);
268 continue;
269 }
270 pvc_p = &(*pvc_p)->next;
271 }
272}
273
274
275static inline struct net_device** get_dev_p(pvc_device *pvc, int type)
276{
277 if (type == ARPHRD_ETHER)
278 return &pvc->ether;
279 else
280 return &pvc->main;
281}
282
283
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284static int fr_hard_header(struct sk_buff **skb_p, u16 dlci)
285{
286 u16 head_len;
287 struct sk_buff *skb = *skb_p;
288
289 switch (skb->protocol) {
Krzysztof Halasaabf17ff2007-04-27 13:13:33 +0200290 case __constant_htons(NLPID_CCITT_ANSI_LMI):
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +0200291 head_len = 4;
292 skb_push(skb, head_len);
293 skb->data[3] = NLPID_CCITT_ANSI_LMI;
294 break;
295
Krzysztof Halasaabf17ff2007-04-27 13:13:33 +0200296 case __constant_htons(NLPID_CISCO_LMI):
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +0200297 head_len = 4;
298 skb_push(skb, head_len);
299 skb->data[3] = NLPID_CISCO_LMI;
300 break;
301
Krzysztof Halasaabf17ff2007-04-27 13:13:33 +0200302 case __constant_htons(ETH_P_IP):
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 head_len = 4;
304 skb_push(skb, head_len);
305 skb->data[3] = NLPID_IP;
306 break;
307
Krzysztof Halasaabf17ff2007-04-27 13:13:33 +0200308 case __constant_htons(ETH_P_IPV6):
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 head_len = 4;
310 skb_push(skb, head_len);
311 skb->data[3] = NLPID_IPV6;
312 break;
313
Krzysztof Halasaabf17ff2007-04-27 13:13:33 +0200314 case __constant_htons(ETH_P_802_3):
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 head_len = 10;
316 if (skb_headroom(skb) < head_len) {
317 struct sk_buff *skb2 = skb_realloc_headroom(skb,
318 head_len);
319 if (!skb2)
320 return -ENOBUFS;
321 dev_kfree_skb(skb);
322 skb = *skb_p = skb2;
323 }
324 skb_push(skb, head_len);
325 skb->data[3] = FR_PAD;
326 skb->data[4] = NLPID_SNAP;
327 skb->data[5] = FR_PAD;
328 skb->data[6] = 0x80;
329 skb->data[7] = 0xC2;
330 skb->data[8] = 0x00;
331 skb->data[9] = 0x07; /* bridged Ethernet frame w/out FCS */
332 break;
333
334 default:
335 head_len = 10;
336 skb_push(skb, head_len);
337 skb->data[3] = FR_PAD;
338 skb->data[4] = NLPID_SNAP;
339 skb->data[5] = FR_PAD;
340 skb->data[6] = FR_PAD;
341 skb->data[7] = FR_PAD;
Krzysztof Halasaabf17ff2007-04-27 13:13:33 +0200342 *(__be16*)(skb->data + 8) = skb->protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 }
344
345 dlci_to_q922(skb->data, dlci);
346 skb->data[2] = FR_UI;
347 return 0;
348}
349
350
351
352static int pvc_open(struct net_device *dev)
353{
354 pvc_device *pvc = dev_to_pvc(dev);
355
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200356 if ((pvc->frad->flags & IFF_UP) == 0)
357 return -EIO; /* Frad must be UP in order to activate PVC */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
359 if (pvc->open_count++ == 0) {
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200360 hdlc_device *hdlc = dev_to_hdlc(pvc->frad);
361 if (state(hdlc)->settings.lmi == LMI_NONE)
362 pvc->state.active = netif_carrier_ok(pvc->frad);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
364 pvc_carrier(pvc->state.active, pvc);
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200365 state(hdlc)->dce_changed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 }
367 return 0;
368}
369
370
371
372static int pvc_close(struct net_device *dev)
373{
374 pvc_device *pvc = dev_to_pvc(dev);
375
376 if (--pvc->open_count == 0) {
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200377 hdlc_device *hdlc = dev_to_hdlc(pvc->frad);
378 if (state(hdlc)->settings.lmi == LMI_NONE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 pvc->state.active = 0;
380
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200381 if (state(hdlc)->settings.dce) {
382 state(hdlc)->dce_changed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 pvc->state.active = 0;
384 }
385 }
386 return 0;
387}
388
389
390
Adrian Bunk7665a082005-09-09 23:17:28 -0700391static int pvc_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392{
393 pvc_device *pvc = dev_to_pvc(dev);
394 fr_proto_pvc_info info;
395
396 if (ifr->ifr_settings.type == IF_GET_PROTO) {
397 if (dev->type == ARPHRD_ETHER)
398 ifr->ifr_settings.type = IF_PROTO_FR_ETH_PVC;
399 else
400 ifr->ifr_settings.type = IF_PROTO_FR_PVC;
401
402 if (ifr->ifr_settings.size < sizeof(info)) {
403 /* data size wanted */
404 ifr->ifr_settings.size = sizeof(info);
405 return -ENOBUFS;
406 }
407
408 info.dlci = pvc->dlci;
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200409 memcpy(info.master, pvc->frad->name, IFNAMSIZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 if (copy_to_user(ifr->ifr_settings.ifs_ifsu.fr_pvc_info,
411 &info, sizeof(info)))
412 return -EFAULT;
413 return 0;
414 }
415
416 return -EINVAL;
417}
418
419
420static inline struct net_device_stats *pvc_get_stats(struct net_device *dev)
421{
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200422 return &dev_to_desc(dev)->stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423}
424
425
426
427static int pvc_xmit(struct sk_buff *skb, struct net_device *dev)
428{
429 pvc_device *pvc = dev_to_pvc(dev);
430 struct net_device_stats *stats = pvc_get_stats(dev);
431
432 if (pvc->state.active) {
433 if (dev->type == ARPHRD_ETHER) {
434 int pad = ETH_ZLEN - skb->len;
435 if (pad > 0) { /* Pad the frame with zeros */
436 int len = skb->len;
437 if (skb_tailroom(skb) < pad)
438 if (pskb_expand_head(skb, 0, pad,
439 GFP_ATOMIC)) {
440 stats->tx_dropped++;
441 dev_kfree_skb(skb);
442 return 0;
443 }
444 skb_put(skb, pad);
445 memset(skb->data + len, 0, pad);
446 }
447 skb->protocol = __constant_htons(ETH_P_802_3);
448 }
449 if (!fr_hard_header(&skb, pvc->dlci)) {
450 stats->tx_bytes += skb->len;
451 stats->tx_packets++;
452 if (pvc->state.fecn) /* TX Congestion counter */
453 stats->tx_compressed++;
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200454 skb->dev = pvc->frad;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 dev_queue_xmit(skb);
456 return 0;
457 }
458 }
459
460 stats->tx_dropped++;
461 dev_kfree_skb(skb);
462 return 0;
463}
464
465
466
467static int pvc_change_mtu(struct net_device *dev, int new_mtu)
468{
469 if ((new_mtu < 68) || (new_mtu > HDLC_MAX_MTU))
470 return -EINVAL;
471 dev->mtu = new_mtu;
472 return 0;
473}
474
475
476
477static inline void fr_log_dlci_active(pvc_device *pvc)
478{
479 printk(KERN_INFO "%s: DLCI %d [%s%s%s]%s %s\n",
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200480 pvc->frad->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 pvc->dlci,
482 pvc->main ? pvc->main->name : "",
483 pvc->main && pvc->ether ? " " : "",
484 pvc->ether ? pvc->ether->name : "",
485 pvc->state.new ? " new" : "",
486 !pvc->state.exist ? "deleted" :
487 pvc->state.active ? "active" : "inactive");
488}
489
490
491
492static inline u8 fr_lmi_nextseq(u8 x)
493{
494 x++;
495 return x ? x : 1;
496}
497
498
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499static void fr_lmi_send(struct net_device *dev, int fullrep)
500{
501 hdlc_device *hdlc = dev_to_hdlc(dev);
502 struct sk_buff *skb;
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200503 pvc_device *pvc = state(hdlc)->first_pvc;
504 int lmi = state(hdlc)->settings.lmi;
505 int dce = state(hdlc)->settings.dce;
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +0200506 int len = lmi == LMI_ANSI ? LMI_ANSI_LENGTH : LMI_CCITT_CISCO_LENGTH;
507 int stat_len = (lmi == LMI_CISCO) ? 6 : 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 u8 *data;
509 int i = 0;
510
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +0200511 if (dce && fullrep) {
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200512 len += state(hdlc)->dce_pvc_count * (2 + stat_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 if (len > HDLC_MAX_MRU) {
514 printk(KERN_WARNING "%s: Too many PVCs while sending "
515 "LMI full report\n", dev->name);
516 return;
517 }
518 }
519
520 skb = dev_alloc_skb(len);
521 if (!skb) {
522 printk(KERN_WARNING "%s: Memory squeeze on fr_lmi_send()\n",
523 dev->name);
524 return;
525 }
526 memset(skb->data, 0, len);
527 skb_reserve(skb, 4);
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +0200528 if (lmi == LMI_CISCO) {
529 skb->protocol = __constant_htons(NLPID_CISCO_LMI);
530 fr_hard_header(&skb, LMI_CISCO_DLCI);
531 } else {
532 skb->protocol = __constant_htons(NLPID_CCITT_ANSI_LMI);
533 fr_hard_header(&skb, LMI_CCITT_ANSI_DLCI);
534 }
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700535 data = skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 data[i++] = LMI_CALLREF;
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +0200537 data[i++] = dce ? LMI_STATUS : LMI_STATUS_ENQUIRY;
538 if (lmi == LMI_ANSI)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 data[i++] = LMI_ANSI_LOCKSHIFT;
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +0200540 data[i++] = lmi == LMI_CCITT ? LMI_CCITT_REPTYPE :
541 LMI_ANSI_CISCO_REPTYPE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 data[i++] = LMI_REPT_LEN;
543 data[i++] = fullrep ? LMI_FULLREP : LMI_INTEGRITY;
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +0200544 data[i++] = lmi == LMI_CCITT ? LMI_CCITT_ALIVE : LMI_ANSI_CISCO_ALIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 data[i++] = LMI_INTEG_LEN;
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200546 data[i++] = state(hdlc)->txseq =
547 fr_lmi_nextseq(state(hdlc)->txseq);
548 data[i++] = state(hdlc)->rxseq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +0200550 if (dce && fullrep) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 while (pvc) {
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +0200552 data[i++] = lmi == LMI_CCITT ? LMI_CCITT_PVCSTAT :
553 LMI_ANSI_CISCO_PVCSTAT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 data[i++] = stat_len;
555
556 /* LMI start/restart */
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200557 if (state(hdlc)->reliable && !pvc->state.exist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 pvc->state.exist = pvc->state.new = 1;
559 fr_log_dlci_active(pvc);
560 }
561
562 /* ifconfig PVC up */
563 if (pvc->open_count && !pvc->state.active &&
564 pvc->state.exist && !pvc->state.new) {
565 pvc_carrier(1, pvc);
566 pvc->state.active = 1;
567 fr_log_dlci_active(pvc);
568 }
569
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +0200570 if (lmi == LMI_CISCO) {
571 data[i] = pvc->dlci >> 8;
572 data[i + 1] = pvc->dlci & 0xFF;
573 } else {
574 data[i] = (pvc->dlci >> 4) & 0x3F;
575 data[i + 1] = ((pvc->dlci << 3) & 0x78) | 0x80;
576 data[i + 2] = 0x80;
577 }
578
579 if (pvc->state.new)
580 data[i + 2] |= 0x08;
581 else if (pvc->state.active)
582 data[i + 2] |= 0x02;
583
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 i += stat_len;
585 pvc = pvc->next;
586 }
587 }
588
589 skb_put(skb, i);
590 skb->priority = TC_PRIO_CONTROL;
591 skb->dev = dev;
Arnaldo Carvalho de Meloc1d2bbe2007-04-10 20:45:18 -0700592 skb_reset_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
594 dev_queue_xmit(skb);
595}
596
597
598
599static void fr_set_link_state(int reliable, struct net_device *dev)
600{
601 hdlc_device *hdlc = dev_to_hdlc(dev);
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200602 pvc_device *pvc = state(hdlc)->first_pvc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200604 state(hdlc)->reliable = reliable;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 if (reliable) {
Krzysztof Halasac2ce9202006-07-12 13:46:12 -0700606 netif_dormant_off(dev);
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200607 state(hdlc)->n391cnt = 0; /* Request full status */
608 state(hdlc)->dce_changed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200610 if (state(hdlc)->settings.lmi == LMI_NONE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 while (pvc) { /* Activate all PVCs */
612 pvc_carrier(1, pvc);
613 pvc->state.exist = pvc->state.active = 1;
614 pvc->state.new = 0;
615 pvc = pvc->next;
616 }
617 }
618 } else {
Krzysztof Halasac2ce9202006-07-12 13:46:12 -0700619 netif_dormant_on(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 while (pvc) { /* Deactivate all PVCs */
621 pvc_carrier(0, pvc);
622 pvc->state.exist = pvc->state.active = 0;
623 pvc->state.new = 0;
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200624 if (!state(hdlc)->settings.dce)
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +0200625 pvc->state.bandwidth = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 pvc = pvc->next;
627 }
628 }
629}
630
631
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632static void fr_timer(unsigned long arg)
633{
634 struct net_device *dev = (struct net_device *)arg;
635 hdlc_device *hdlc = dev_to_hdlc(dev);
636 int i, cnt = 0, reliable;
637 u32 list;
638
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200639 if (state(hdlc)->settings.dce) {
640 reliable = state(hdlc)->request &&
641 time_before(jiffies, state(hdlc)->last_poll +
642 state(hdlc)->settings.t392 * HZ);
643 state(hdlc)->request = 0;
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +0200644 } else {
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200645 state(hdlc)->last_errors <<= 1; /* Shift the list */
646 if (state(hdlc)->request) {
647 if (state(hdlc)->reliable)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 printk(KERN_INFO "%s: No LMI status reply "
649 "received\n", dev->name);
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200650 state(hdlc)->last_errors |= 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 }
652
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200653 list = state(hdlc)->last_errors;
654 for (i = 0; i < state(hdlc)->settings.n393; i++, list >>= 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 cnt += (list & 1); /* errors count */
656
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200657 reliable = (cnt < state(hdlc)->settings.n392);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 }
659
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200660 if (state(hdlc)->reliable != reliable) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 printk(KERN_INFO "%s: Link %sreliable\n", dev->name,
662 reliable ? "" : "un");
663 fr_set_link_state(reliable, dev);
664 }
665
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200666 if (state(hdlc)->settings.dce)
667 state(hdlc)->timer.expires = jiffies +
668 state(hdlc)->settings.t392 * HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 else {
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200670 if (state(hdlc)->n391cnt)
671 state(hdlc)->n391cnt--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200673 fr_lmi_send(dev, state(hdlc)->n391cnt == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200675 state(hdlc)->last_poll = jiffies;
676 state(hdlc)->request = 1;
677 state(hdlc)->timer.expires = jiffies +
678 state(hdlc)->settings.t391 * HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 }
680
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200681 state(hdlc)->timer.function = fr_timer;
682 state(hdlc)->timer.data = arg;
683 add_timer(&state(hdlc)->timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684}
685
686
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687static int fr_lmi_recv(struct net_device *dev, struct sk_buff *skb)
688{
689 hdlc_device *hdlc = dev_to_hdlc(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 pvc_device *pvc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 u8 rxseq, txseq;
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200692 int lmi = state(hdlc)->settings.lmi;
693 int dce = state(hdlc)->settings.dce;
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +0200694 int stat_len = (lmi == LMI_CISCO) ? 6 : 3, reptype, error, no_ram, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +0200696 if (skb->len < (lmi == LMI_ANSI ? LMI_ANSI_LENGTH :
697 LMI_CCITT_CISCO_LENGTH)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 printk(KERN_INFO "%s: Short LMI frame\n", dev->name);
699 return 1;
700 }
701
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +0200702 if (skb->data[3] != (lmi == LMI_CISCO ? NLPID_CISCO_LMI :
703 NLPID_CCITT_ANSI_LMI)) {
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200704 printk(KERN_INFO "%s: Received non-LMI frame with LMI DLCI\n",
705 dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 return 1;
707 }
708
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +0200709 if (skb->data[4] != LMI_CALLREF) {
710 printk(KERN_INFO "%s: Invalid LMI Call reference (0x%02X)\n",
711 dev->name, skb->data[4]);
712 return 1;
713 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +0200715 if (skb->data[5] != (dce ? LMI_STATUS_ENQUIRY : LMI_STATUS)) {
716 printk(KERN_INFO "%s: Invalid LMI Message type (0x%02X)\n",
717 dev->name, skb->data[5]);
718 return 1;
719 }
720
721 if (lmi == LMI_ANSI) {
722 if (skb->data[6] != LMI_ANSI_LOCKSHIFT) {
723 printk(KERN_INFO "%s: Not ANSI locking shift in LMI"
724 " message (0x%02X)\n", dev->name, skb->data[6]);
725 return 1;
726 }
727 i = 7;
728 } else
729 i = 6;
730
731 if (skb->data[i] != (lmi == LMI_CCITT ? LMI_CCITT_REPTYPE :
732 LMI_ANSI_CISCO_REPTYPE)) {
733 printk(KERN_INFO "%s: Not an LMI Report type IE (0x%02X)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 dev->name, skb->data[i]);
735 return 1;
736 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +0200738 if (skb->data[++i] != LMI_REPT_LEN) {
739 printk(KERN_INFO "%s: Invalid LMI Report type IE length"
740 " (%u)\n", dev->name, skb->data[i]);
741 return 1;
742 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +0200744 reptype = skb->data[++i];
745 if (reptype != LMI_INTEGRITY && reptype != LMI_FULLREP) {
746 printk(KERN_INFO "%s: Unsupported LMI Report type (0x%02X)\n",
747 dev->name, reptype);
748 return 1;
749 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +0200751 if (skb->data[++i] != (lmi == LMI_CCITT ? LMI_CCITT_ALIVE :
752 LMI_ANSI_CISCO_ALIVE)) {
753 printk(KERN_INFO "%s: Not an LMI Link integrity verification"
754 " IE (0x%02X)\n", dev->name, skb->data[i]);
755 return 1;
756 }
757
758 if (skb->data[++i] != LMI_INTEG_LEN) {
759 printk(KERN_INFO "%s: Invalid LMI Link integrity verification"
760 " IE length (%u)\n", dev->name, skb->data[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 return 1;
762 }
763 i++;
764
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200765 state(hdlc)->rxseq = skb->data[i++]; /* TX sequence from peer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 rxseq = skb->data[i++]; /* Should confirm our sequence */
767
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200768 txseq = state(hdlc)->txseq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +0200770 if (dce)
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200771 state(hdlc)->last_poll = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772
773 error = 0;
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200774 if (!state(hdlc)->reliable)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 error = 1;
776
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200777 if (rxseq == 0 || rxseq != txseq) { /* Ask for full report next time */
778 state(hdlc)->n391cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 error = 1;
780 }
781
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +0200782 if (dce) {
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200783 if (state(hdlc)->fullrep_sent && !error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784/* Stop sending full report - the last one has been confirmed by DTE */
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200785 state(hdlc)->fullrep_sent = 0;
786 pvc = state(hdlc)->first_pvc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 while (pvc) {
788 if (pvc->state.new) {
789 pvc->state.new = 0;
790
791/* Tell DTE that new PVC is now active */
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200792 state(hdlc)->dce_changed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 }
794 pvc = pvc->next;
795 }
796 }
797
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200798 if (state(hdlc)->dce_changed) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 reptype = LMI_FULLREP;
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200800 state(hdlc)->fullrep_sent = 1;
801 state(hdlc)->dce_changed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 }
803
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200804 state(hdlc)->request = 1; /* got request */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 fr_lmi_send(dev, reptype == LMI_FULLREP ? 1 : 0);
806 return 0;
807 }
808
809 /* DTE */
810
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200811 state(hdlc)->request = 0; /* got response, no request pending */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812
813 if (error)
814 return 0;
815
816 if (reptype != LMI_FULLREP)
817 return 0;
818
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200819 pvc = state(hdlc)->first_pvc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
821 while (pvc) {
822 pvc->state.deleted = 1;
823 pvc = pvc->next;
824 }
825
826 no_ram = 0;
827 while (skb->len >= i + 2 + stat_len) {
828 u16 dlci;
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +0200829 u32 bw;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 unsigned int active, new;
831
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +0200832 if (skb->data[i] != (lmi == LMI_CCITT ? LMI_CCITT_PVCSTAT :
833 LMI_ANSI_CISCO_PVCSTAT)) {
834 printk(KERN_INFO "%s: Not an LMI PVC status IE"
835 " (0x%02X)\n", dev->name, skb->data[i]);
836 return 1;
837 }
838
839 if (skb->data[++i] != stat_len) {
840 printk(KERN_INFO "%s: Invalid LMI PVC status IE length"
841 " (%u)\n", dev->name, skb->data[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 return 1;
843 }
844 i++;
845
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +0200846 new = !! (skb->data[i + 2] & 0x08);
847 active = !! (skb->data[i + 2] & 0x02);
848 if (lmi == LMI_CISCO) {
849 dlci = (skb->data[i] << 8) | skb->data[i + 1];
850 bw = (skb->data[i + 3] << 16) |
851 (skb->data[i + 4] << 8) |
852 (skb->data[i + 5]);
853 } else {
854 dlci = ((skb->data[i] & 0x3F) << 4) |
855 ((skb->data[i + 1] & 0x78) >> 3);
856 bw = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858
859 pvc = add_pvc(dev, dlci);
860
861 if (!pvc && !no_ram) {
862 printk(KERN_WARNING
863 "%s: Memory squeeze on fr_lmi_recv()\n",
864 dev->name);
865 no_ram = 1;
866 }
867
868 if (pvc) {
869 pvc->state.exist = 1;
870 pvc->state.deleted = 0;
871 if (active != pvc->state.active ||
872 new != pvc->state.new ||
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +0200873 bw != pvc->state.bandwidth ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 !pvc->state.exist) {
875 pvc->state.new = new;
876 pvc->state.active = active;
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +0200877 pvc->state.bandwidth = bw;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 pvc_carrier(active, pvc);
879 fr_log_dlci_active(pvc);
880 }
881 }
882
883 i += stat_len;
884 }
885
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200886 pvc = state(hdlc)->first_pvc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887
888 while (pvc) {
889 if (pvc->state.deleted && pvc->state.exist) {
890 pvc_carrier(0, pvc);
891 pvc->state.active = pvc->state.new = 0;
892 pvc->state.exist = 0;
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +0200893 pvc->state.bandwidth = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 fr_log_dlci_active(pvc);
895 }
896 pvc = pvc->next;
897 }
898
899 /* Next full report after N391 polls */
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200900 state(hdlc)->n391cnt = state(hdlc)->settings.n391;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901
902 return 0;
903}
904
905
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906static int fr_rx(struct sk_buff *skb)
907{
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200908 struct net_device *frad = skb->dev;
909 hdlc_device *hdlc = dev_to_hdlc(frad);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 fr_hdr *fh = (fr_hdr*)skb->data;
911 u8 *data = skb->data;
912 u16 dlci;
913 pvc_device *pvc;
914 struct net_device *dev = NULL;
915
916 if (skb->len <= 4 || fh->ea1 || data[2] != FR_UI)
917 goto rx_error;
918
919 dlci = q922_to_dlci(skb->data);
920
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +0200921 if ((dlci == LMI_CCITT_ANSI_DLCI &&
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200922 (state(hdlc)->settings.lmi == LMI_ANSI ||
923 state(hdlc)->settings.lmi == LMI_CCITT)) ||
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +0200924 (dlci == LMI_CISCO_DLCI &&
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200925 state(hdlc)->settings.lmi == LMI_CISCO)) {
926 if (fr_lmi_recv(frad, skb))
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +0200927 goto rx_error;
928 dev_kfree_skb_any(skb);
929 return NET_RX_SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 }
931
932 pvc = find_pvc(hdlc, dlci);
933 if (!pvc) {
934#ifdef DEBUG_PKT
935 printk(KERN_INFO "%s: No PVC for received frame's DLCI %d\n",
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200936 frad->name, dlci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937#endif
938 dev_kfree_skb_any(skb);
939 return NET_RX_DROP;
940 }
941
942 if (pvc->state.fecn != fh->fecn) {
943#ifdef DEBUG_ECN
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200944 printk(KERN_DEBUG "%s: DLCI %d FECN O%s\n", frad->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 dlci, fh->fecn ? "N" : "FF");
946#endif
947 pvc->state.fecn ^= 1;
948 }
949
950 if (pvc->state.becn != fh->becn) {
951#ifdef DEBUG_ECN
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200952 printk(KERN_DEBUG "%s: DLCI %d BECN O%s\n", frad->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 dlci, fh->becn ? "N" : "FF");
954#endif
955 pvc->state.becn ^= 1;
956 }
957
958
959 if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL) {
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200960 dev_to_desc(frad)->stats.rx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 return NET_RX_DROP;
962 }
963
964 if (data[3] == NLPID_IP) {
965 skb_pull(skb, 4); /* Remove 4-byte header (hdr, UI, NLPID) */
966 dev = pvc->main;
967 skb->protocol = htons(ETH_P_IP);
968
969 } else if (data[3] == NLPID_IPV6) {
970 skb_pull(skb, 4); /* Remove 4-byte header (hdr, UI, NLPID) */
971 dev = pvc->main;
972 skb->protocol = htons(ETH_P_IPV6);
973
974 } else if (skb->len > 10 && data[3] == FR_PAD &&
975 data[4] == NLPID_SNAP && data[5] == FR_PAD) {
Krzysztof Halasaabf17ff2007-04-27 13:13:33 +0200976 u16 oui = ntohs(*(__be16*)(data + 6));
977 u16 pid = ntohs(*(__be16*)(data + 8));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 skb_pull(skb, 10);
979
980 switch ((((u32)oui) << 16) | pid) {
981 case ETH_P_ARP: /* routed frame with SNAP */
982 case ETH_P_IPX:
983 case ETH_P_IP: /* a long variant */
984 case ETH_P_IPV6:
985 dev = pvc->main;
986 skb->protocol = htons(pid);
987 break;
988
989 case 0x80C20007: /* bridged Ethernet frame */
990 if ((dev = pvc->ether) != NULL)
991 skb->protocol = eth_type_trans(skb, dev);
992 break;
993
994 default:
995 printk(KERN_INFO "%s: Unsupported protocol, OUI=%x "
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200996 "PID=%x\n", frad->name, oui, pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 dev_kfree_skb_any(skb);
998 return NET_RX_DROP;
999 }
1000 } else {
1001 printk(KERN_INFO "%s: Unsupported protocol, NLPID=%x "
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +02001002 "length = %i\n", frad->name, data[3], skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 dev_kfree_skb_any(skb);
1004 return NET_RX_DROP;
1005 }
1006
1007 if (dev) {
1008 struct net_device_stats *stats = pvc_get_stats(dev);
1009 stats->rx_packets++; /* PVC traffic */
1010 stats->rx_bytes += skb->len;
1011 if (pvc->state.becn)
1012 stats->rx_compressed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 netif_rx(skb);
1014 return NET_RX_SUCCESS;
1015 } else {
1016 dev_kfree_skb_any(skb);
1017 return NET_RX_DROP;
1018 }
1019
1020 rx_error:
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +02001021 dev_to_desc(frad)->stats.rx_errors++; /* Mark error */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 dev_kfree_skb_any(skb);
1023 return NET_RX_DROP;
1024}
1025
1026
1027
1028static void fr_start(struct net_device *dev)
1029{
1030 hdlc_device *hdlc = dev_to_hdlc(dev);
1031#ifdef DEBUG_LINK
1032 printk(KERN_DEBUG "fr_start\n");
1033#endif
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +02001034 if (state(hdlc)->settings.lmi != LMI_NONE) {
1035 state(hdlc)->reliable = 0;
1036 state(hdlc)->dce_changed = 1;
1037 state(hdlc)->request = 0;
1038 state(hdlc)->fullrep_sent = 0;
1039 state(hdlc)->last_errors = 0xFFFFFFFF;
1040 state(hdlc)->n391cnt = 0;
1041 state(hdlc)->txseq = state(hdlc)->rxseq = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +02001043 init_timer(&state(hdlc)->timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 /* First poll after 1 s */
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +02001045 state(hdlc)->timer.expires = jiffies + HZ;
1046 state(hdlc)->timer.function = fr_timer;
1047 state(hdlc)->timer.data = (unsigned long)dev;
1048 add_timer(&state(hdlc)->timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 } else
1050 fr_set_link_state(1, dev);
1051}
1052
1053
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054static void fr_stop(struct net_device *dev)
1055{
1056 hdlc_device *hdlc = dev_to_hdlc(dev);
1057#ifdef DEBUG_LINK
1058 printk(KERN_DEBUG "fr_stop\n");
1059#endif
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +02001060 if (state(hdlc)->settings.lmi != LMI_NONE)
1061 del_timer_sync(&state(hdlc)->timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 fr_set_link_state(0, dev);
1063}
1064
1065
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066static void fr_close(struct net_device *dev)
1067{
1068 hdlc_device *hdlc = dev_to_hdlc(dev);
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +02001069 pvc_device *pvc = state(hdlc)->first_pvc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070
1071 while (pvc) { /* Shutdown all PVCs for this FRAD */
1072 if (pvc->main)
1073 dev_close(pvc->main);
1074 if (pvc->ether)
1075 dev_close(pvc->ether);
1076 pvc = pvc->next;
1077 }
1078}
1079
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +02001080
1081static void pvc_setup(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082{
1083 dev->type = ARPHRD_DLCI;
1084 dev->flags = IFF_POINTOPOINT;
1085 dev->hard_header_len = 10;
1086 dev->addr_len = 2;
1087}
1088
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +02001089static int fr_add_pvc(struct net_device *frad, unsigned int dlci, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090{
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +02001091 hdlc_device *hdlc = dev_to_hdlc(frad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 pvc_device *pvc = NULL;
1093 struct net_device *dev;
1094 int result, used;
1095 char * prefix = "pvc%d";
1096
1097 if (type == ARPHRD_ETHER)
1098 prefix = "pvceth%d";
1099
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +02001100 if ((pvc = add_pvc(frad, dlci)) == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 printk(KERN_WARNING "%s: Memory squeeze on fr_add_pvc()\n",
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +02001102 frad->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 return -ENOBUFS;
1104 }
1105
1106 if (*get_dev_p(pvc, type))
1107 return -EEXIST;
1108
1109 used = pvc_is_used(pvc);
1110
1111 if (type == ARPHRD_ETHER)
1112 dev = alloc_netdev(sizeof(struct net_device_stats),
1113 "pvceth%d", ether_setup);
1114 else
1115 dev = alloc_netdev(sizeof(struct net_device_stats),
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +02001116 "pvc%d", pvc_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117
1118 if (!dev) {
1119 printk(KERN_WARNING "%s: Memory squeeze on fr_pvc()\n",
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +02001120 frad->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 delete_unused_pvcs(hdlc);
1122 return -ENOBUFS;
1123 }
1124
1125 if (type == ARPHRD_ETHER) {
1126 memcpy(dev->dev_addr, "\x00\x01", 2);
1127 get_random_bytes(dev->dev_addr + 2, ETH_ALEN - 2);
1128 } else {
Krzysztof Halasaabf17ff2007-04-27 13:13:33 +02001129 *(__be16*)dev->dev_addr = htons(dlci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 dlci_to_q922(dev->broadcast, dlci);
1131 }
1132 dev->hard_start_xmit = pvc_xmit;
1133 dev->get_stats = pvc_get_stats;
1134 dev->open = pvc_open;
1135 dev->stop = pvc_close;
1136 dev->do_ioctl = pvc_ioctl;
1137 dev->change_mtu = pvc_change_mtu;
1138 dev->mtu = HDLC_MAX_MTU;
1139 dev->tx_queue_len = 0;
1140 dev->priv = pvc;
1141
1142 result = dev_alloc_name(dev, dev->name);
1143 if (result < 0) {
1144 free_netdev(dev);
1145 delete_unused_pvcs(hdlc);
1146 return result;
1147 }
1148
1149 if (register_netdevice(dev) != 0) {
1150 free_netdev(dev);
1151 delete_unused_pvcs(hdlc);
1152 return -EIO;
1153 }
1154
1155 dev->destructor = free_netdev;
1156 *get_dev_p(pvc, type) = dev;
1157 if (!used) {
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +02001158 state(hdlc)->dce_changed = 1;
1159 state(hdlc)->dce_pvc_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 }
1161 return 0;
1162}
1163
1164
1165
1166static int fr_del_pvc(hdlc_device *hdlc, unsigned int dlci, int type)
1167{
1168 pvc_device *pvc;
1169 struct net_device *dev;
1170
1171 if ((pvc = find_pvc(hdlc, dlci)) == NULL)
1172 return -ENOENT;
1173
1174 if ((dev = *get_dev_p(pvc, type)) == NULL)
1175 return -ENOENT;
1176
1177 if (dev->flags & IFF_UP)
1178 return -EBUSY; /* PVC in use */
1179
1180 unregister_netdevice(dev); /* the destructor will free_netdev(dev) */
1181 *get_dev_p(pvc, type) = NULL;
1182
1183 if (!pvc_is_used(pvc)) {
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +02001184 state(hdlc)->dce_pvc_count--;
1185 state(hdlc)->dce_changed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 }
1187 delete_unused_pvcs(hdlc);
1188 return 0;
1189}
1190
1191
1192
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +02001193static void fr_destroy(struct net_device *frad)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194{
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +02001195 hdlc_device *hdlc = dev_to_hdlc(frad);
1196 pvc_device *pvc = state(hdlc)->first_pvc;
1197 state(hdlc)->first_pvc = NULL; /* All PVCs destroyed */
1198 state(hdlc)->dce_pvc_count = 0;
1199 state(hdlc)->dce_changed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200
1201 while (pvc) {
1202 pvc_device *next = pvc->next;
1203 /* destructors will free_netdev() main and ether */
1204 if (pvc->main)
1205 unregister_netdevice(pvc->main);
1206
1207 if (pvc->ether)
1208 unregister_netdevice(pvc->ether);
1209
1210 kfree(pvc);
1211 pvc = next;
1212 }
1213}
1214
1215
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +02001216static struct hdlc_proto proto = {
1217 .close = fr_close,
1218 .start = fr_start,
1219 .stop = fr_stop,
1220 .detach = fr_destroy,
1221 .ioctl = fr_ioctl,
1222 .module = THIS_MODULE,
1223};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +02001225
1226static int fr_ioctl(struct net_device *dev, struct ifreq *ifr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227{
1228 fr_proto __user *fr_s = ifr->ifr_settings.ifs_ifsu.fr;
1229 const size_t size = sizeof(fr_proto);
1230 fr_proto new_settings;
1231 hdlc_device *hdlc = dev_to_hdlc(dev);
1232 fr_proto_pvc pvc;
1233 int result;
1234
1235 switch (ifr->ifr_settings.type) {
1236 case IF_GET_PROTO:
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +02001237 if (dev_to_hdlc(dev)->proto != &proto) /* Different proto */
1238 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 ifr->ifr_settings.type = IF_PROTO_FR;
1240 if (ifr->ifr_settings.size < size) {
1241 ifr->ifr_settings.size = size; /* data size wanted */
1242 return -ENOBUFS;
1243 }
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +02001244 if (copy_to_user(fr_s, &state(hdlc)->settings, size))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 return -EFAULT;
1246 return 0;
1247
1248 case IF_PROTO_FR:
1249 if(!capable(CAP_NET_ADMIN))
1250 return -EPERM;
1251
1252 if(dev->flags & IFF_UP)
1253 return -EBUSY;
1254
1255 if (copy_from_user(&new_settings, fr_s, size))
1256 return -EFAULT;
1257
1258 if (new_settings.lmi == LMI_DEFAULT)
1259 new_settings.lmi = LMI_ANSI;
1260
1261 if ((new_settings.lmi != LMI_NONE &&
1262 new_settings.lmi != LMI_ANSI &&
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +02001263 new_settings.lmi != LMI_CCITT &&
1264 new_settings.lmi != LMI_CISCO) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 new_settings.t391 < 1 ||
1266 new_settings.t392 < 2 ||
1267 new_settings.n391 < 1 ||
1268 new_settings.n392 < 1 ||
1269 new_settings.n393 < new_settings.n392 ||
1270 new_settings.n393 > 32 ||
1271 (new_settings.dce != 0 &&
1272 new_settings.dce != 1))
1273 return -EINVAL;
1274
1275 result=hdlc->attach(dev, ENCODING_NRZ,PARITY_CRC16_PR1_CCITT);
1276 if (result)
1277 return result;
1278
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +02001279 if (dev_to_hdlc(dev)->proto != &proto) { /* Different proto */
1280 result = attach_hdlc_protocol(dev, &proto, fr_rx,
1281 sizeof(struct frad_state));
1282 if (result)
1283 return result;
1284 state(hdlc)->first_pvc = NULL;
1285 state(hdlc)->dce_pvc_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 }
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +02001287 memcpy(&state(hdlc)->settings, &new_settings, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 dev->hard_start_xmit = hdlc->xmit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 dev->type = ARPHRD_FRAD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 return 0;
1292
1293 case IF_PROTO_FR_ADD_PVC:
1294 case IF_PROTO_FR_DEL_PVC:
1295 case IF_PROTO_FR_ADD_ETH_PVC:
1296 case IF_PROTO_FR_DEL_ETH_PVC:
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +02001297 if (dev_to_hdlc(dev)->proto != &proto) /* Different proto */
1298 return -EINVAL;
1299
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 if(!capable(CAP_NET_ADMIN))
1301 return -EPERM;
1302
1303 if (copy_from_user(&pvc, ifr->ifr_settings.ifs_ifsu.fr_pvc,
1304 sizeof(fr_proto_pvc)))
1305 return -EFAULT;
1306
1307 if (pvc.dlci <= 0 || pvc.dlci >= 1024)
1308 return -EINVAL; /* Only 10 bits, DLCI 0 reserved */
1309
1310 if (ifr->ifr_settings.type == IF_PROTO_FR_ADD_ETH_PVC ||
1311 ifr->ifr_settings.type == IF_PROTO_FR_DEL_ETH_PVC)
1312 result = ARPHRD_ETHER; /* bridged Ethernet device */
1313 else
1314 result = ARPHRD_DLCI;
1315
1316 if (ifr->ifr_settings.type == IF_PROTO_FR_ADD_PVC ||
1317 ifr->ifr_settings.type == IF_PROTO_FR_ADD_ETH_PVC)
1318 return fr_add_pvc(dev, pvc.dlci, result);
1319 else
1320 return fr_del_pvc(hdlc, pvc.dlci, result);
1321 }
1322
1323 return -EINVAL;
1324}
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +02001325
1326
1327static int __init mod_init(void)
1328{
1329 register_hdlc_protocol(&proto);
1330 return 0;
1331}
1332
1333
1334static void __exit mod_exit(void)
1335{
1336 unregister_hdlc_protocol(&proto);
1337}
1338
1339
1340module_init(mod_init);
1341module_exit(mod_exit);
1342
1343MODULE_AUTHOR("Krzysztof Halasa <khc@pm.waw.pl>");
1344MODULE_DESCRIPTION("Frame-Relay protocol support for generic HDLC");
1345MODULE_LICENSE("GPL v2");