blob: 7c6cb4f31798881ab24644a33791775e0ae1daf3 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/errno.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <linux/etherdevice.h>
38#include <linux/hdlc.h>
Krzysztof Hałasa4dfce402008-06-30 19:06:40 +020039#include <linux/if_arp.h>
40#include <linux/inetdevice.h>
41#include <linux/init.h>
42#include <linux/kernel.h>
43#include <linux/module.h>
44#include <linux/pkt_sched.h>
45#include <linux/poll.h>
46#include <linux/rtnetlink.h>
47#include <linux/skbuff.h>
48#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50#undef DEBUG_PKT
51#undef DEBUG_ECN
52#undef DEBUG_LINK
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +020053#undef DEBUG_PROTO
54#undef DEBUG_PVC
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +020056#define FR_UI 0x03
57#define FR_PAD 0x00
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +020059#define NLPID_IP 0xCC
60#define NLPID_IPV6 0x8E
61#define NLPID_SNAP 0x80
62#define NLPID_PAD 0x00
63#define NLPID_CCITT_ANSI_LMI 0x08
64#define NLPID_CISCO_LMI 0x09
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
66
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +020067#define LMI_CCITT_ANSI_DLCI 0 /* LMI DLCI */
68#define LMI_CISCO_DLCI 1023
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +020070#define LMI_CALLREF 0x00 /* Call Reference */
71#define LMI_ANSI_LOCKSHIFT 0x95 /* ANSI locking shift */
72#define LMI_ANSI_CISCO_REPTYPE 0x01 /* report type */
73#define LMI_CCITT_REPTYPE 0x51
74#define LMI_ANSI_CISCO_ALIVE 0x03 /* keep alive */
75#define LMI_CCITT_ALIVE 0x53
76#define LMI_ANSI_CISCO_PVCSTAT 0x07 /* PVC status */
77#define LMI_CCITT_PVCSTAT 0x57
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +020079#define LMI_FULLREP 0x00 /* full report */
80#define LMI_INTEGRITY 0x01 /* link integrity report */
81#define LMI_SINGLE 0x02 /* single PVC report */
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
Linus Torvalds1da177e2005-04-16 15:20:36 -070083#define LMI_STATUS_ENQUIRY 0x75
84#define LMI_STATUS 0x7D /* reply */
85
86#define LMI_REPT_LEN 1 /* report type element length */
87#define LMI_INTEG_LEN 2 /* link integrity element length */
88
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +020089#define LMI_CCITT_CISCO_LENGTH 13 /* LMI frame lengths */
90#define LMI_ANSI_LENGTH 14
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
92
93typedef struct {
94#if defined(__LITTLE_ENDIAN_BITFIELD)
95 unsigned ea1: 1;
96 unsigned cr: 1;
97 unsigned dlcih: 6;
Krzysztof Hałasa4dfce402008-06-30 19:06:40 +020098
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 unsigned ea2: 1;
100 unsigned de: 1;
101 unsigned becn: 1;
102 unsigned fecn: 1;
103 unsigned dlcil: 4;
104#else
105 unsigned dlcih: 6;
106 unsigned cr: 1;
107 unsigned ea1: 1;
108
109 unsigned dlcil: 4;
110 unsigned fecn: 1;
111 unsigned becn: 1;
112 unsigned de: 1;
113 unsigned ea2: 1;
114#endif
Eric Dumazetba2d3582010-06-02 18:10:09 +0000115}__packed fr_hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
117
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200118typedef struct pvc_device_struct {
119 struct net_device *frad;
120 struct net_device *main;
121 struct net_device *ether; /* bridged Ethernet interface */
122 struct pvc_device_struct *next; /* Sorted in ascending DLCI order */
123 int dlci;
124 int open_count;
125
126 struct {
127 unsigned int new: 1;
128 unsigned int active: 1;
129 unsigned int exist: 1;
130 unsigned int deleted: 1;
131 unsigned int fecn: 1;
132 unsigned int becn: 1;
133 unsigned int bandwidth; /* Cisco LMI reporting only */
134 }state;
135}pvc_device;
136
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200137struct frad_state {
138 fr_proto settings;
139 pvc_device *first_pvc;
140 int dce_pvc_count;
141
142 struct timer_list timer;
143 unsigned long last_poll;
144 int reliable;
145 int dce_changed;
146 int request;
147 int fullrep_sent;
148 u32 last_errors; /* last errors bit list */
149 u8 n391cnt;
150 u8 txseq; /* TX sequence number */
151 u8 rxseq; /* RX sequence number */
152};
153
154
155static int fr_ioctl(struct net_device *dev, struct ifreq *ifr);
156
157
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158static inline u16 q922_to_dlci(u8 *hdr)
159{
160 return ((hdr[0] & 0xFC) << 2) | ((hdr[1] & 0xF0) >> 4);
161}
162
163
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164static inline void dlci_to_q922(u8 *hdr, u16 dlci)
165{
166 hdr[0] = (dlci >> 2) & 0xFC;
167 hdr[1] = ((dlci << 4) & 0xF0) | 0x01;
168}
169
170
Krzysztof Halasa983e2302008-02-01 22:34:30 +0100171static inline struct frad_state* state(hdlc_device *hdlc)
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200172{
173 return(struct frad_state *)(hdlc->state);
174}
175
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200176
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177static inline pvc_device* find_pvc(hdlc_device *hdlc, u16 dlci)
178{
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200179 pvc_device *pvc = state(hdlc)->first_pvc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
181 while (pvc) {
182 if (pvc->dlci == dlci)
183 return pvc;
184 if (pvc->dlci > dlci)
Rudy Matela870571a22009-11-25 10:20:20 -0300185 return NULL; /* the list is sorted */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 pvc = pvc->next;
187 }
188
189 return NULL;
190}
191
192
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200193static pvc_device* add_pvc(struct net_device *dev, u16 dlci)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194{
195 hdlc_device *hdlc = dev_to_hdlc(dev);
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200196 pvc_device *pvc, **pvc_p = &state(hdlc)->first_pvc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
198 while (*pvc_p) {
199 if ((*pvc_p)->dlci == dlci)
200 return *pvc_p;
201 if ((*pvc_p)->dlci > dlci)
202 break; /* the list is sorted */
203 pvc_p = &(*pvc_p)->next;
204 }
205
Mariusz Kozlowski1ee32542007-08-10 15:24:50 -0700206 pvc = kzalloc(sizeof(pvc_device), GFP_ATOMIC);
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200207#ifdef DEBUG_PVC
208 printk(KERN_DEBUG "add_pvc: allocated pvc %p, frad %p\n", pvc, dev);
209#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 if (!pvc)
211 return NULL;
212
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 pvc->dlci = dlci;
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200214 pvc->frad = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 pvc->next = *pvc_p; /* Put it in the chain */
216 *pvc_p = pvc;
217 return pvc;
218}
219
220
221static inline int pvc_is_used(pvc_device *pvc)
222{
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200223 return pvc->main || pvc->ether;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224}
225
226
227static inline void pvc_carrier(int on, pvc_device *pvc)
228{
229 if (on) {
230 if (pvc->main)
231 if (!netif_carrier_ok(pvc->main))
232 netif_carrier_on(pvc->main);
233 if (pvc->ether)
234 if (!netif_carrier_ok(pvc->ether))
235 netif_carrier_on(pvc->ether);
236 } else {
237 if (pvc->main)
238 if (netif_carrier_ok(pvc->main))
239 netif_carrier_off(pvc->main);
240 if (pvc->ether)
241 if (netif_carrier_ok(pvc->ether))
242 netif_carrier_off(pvc->ether);
243 }
244}
245
246
247static inline void delete_unused_pvcs(hdlc_device *hdlc)
248{
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200249 pvc_device **pvc_p = &state(hdlc)->first_pvc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
251 while (*pvc_p) {
252 if (!pvc_is_used(*pvc_p)) {
253 pvc_device *pvc = *pvc_p;
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200254#ifdef DEBUG_PVC
255 printk(KERN_DEBUG "freeing unused pvc: %p\n", pvc);
256#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 *pvc_p = pvc->next;
258 kfree(pvc);
259 continue;
260 }
261 pvc_p = &(*pvc_p)->next;
262 }
263}
264
265
266static inline struct net_device** get_dev_p(pvc_device *pvc, int type)
267{
268 if (type == ARPHRD_ETHER)
269 return &pvc->ether;
270 else
271 return &pvc->main;
272}
273
274
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275static int fr_hard_header(struct sk_buff **skb_p, u16 dlci)
276{
277 u16 head_len;
278 struct sk_buff *skb = *skb_p;
279
280 switch (skb->protocol) {
Harvey Harrison09640e62009-02-01 00:45:17 -0800281 case cpu_to_be16(NLPID_CCITT_ANSI_LMI):
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +0200282 head_len = 4;
283 skb_push(skb, head_len);
284 skb->data[3] = NLPID_CCITT_ANSI_LMI;
285 break;
286
Harvey Harrison09640e62009-02-01 00:45:17 -0800287 case cpu_to_be16(NLPID_CISCO_LMI):
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +0200288 head_len = 4;
289 skb_push(skb, head_len);
290 skb->data[3] = NLPID_CISCO_LMI;
291 break;
292
Harvey Harrison09640e62009-02-01 00:45:17 -0800293 case cpu_to_be16(ETH_P_IP):
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 head_len = 4;
295 skb_push(skb, head_len);
296 skb->data[3] = NLPID_IP;
297 break;
298
Harvey Harrison09640e62009-02-01 00:45:17 -0800299 case cpu_to_be16(ETH_P_IPV6):
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 head_len = 4;
301 skb_push(skb, head_len);
302 skb->data[3] = NLPID_IPV6;
303 break;
304
Harvey Harrison09640e62009-02-01 00:45:17 -0800305 case cpu_to_be16(ETH_P_802_3):
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 head_len = 10;
307 if (skb_headroom(skb) < head_len) {
308 struct sk_buff *skb2 = skb_realloc_headroom(skb,
309 head_len);
310 if (!skb2)
311 return -ENOBUFS;
312 dev_kfree_skb(skb);
313 skb = *skb_p = skb2;
314 }
315 skb_push(skb, head_len);
316 skb->data[3] = FR_PAD;
317 skb->data[4] = NLPID_SNAP;
318 skb->data[5] = FR_PAD;
319 skb->data[6] = 0x80;
320 skb->data[7] = 0xC2;
321 skb->data[8] = 0x00;
322 skb->data[9] = 0x07; /* bridged Ethernet frame w/out FCS */
323 break;
324
325 default:
326 head_len = 10;
327 skb_push(skb, head_len);
328 skb->data[3] = FR_PAD;
329 skb->data[4] = NLPID_SNAP;
330 skb->data[5] = FR_PAD;
331 skb->data[6] = FR_PAD;
332 skb->data[7] = FR_PAD;
Krzysztof Halasaabf17ff2007-04-27 13:13:33 +0200333 *(__be16*)(skb->data + 8) = skb->protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 }
335
336 dlci_to_q922(skb->data, dlci);
337 skb->data[2] = FR_UI;
338 return 0;
339}
340
341
342
343static int pvc_open(struct net_device *dev)
344{
Wang Chen2baf8a22008-11-21 16:34:18 -0800345 pvc_device *pvc = dev->ml_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200347 if ((pvc->frad->flags & IFF_UP) == 0)
348 return -EIO; /* Frad must be UP in order to activate PVC */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
350 if (pvc->open_count++ == 0) {
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200351 hdlc_device *hdlc = dev_to_hdlc(pvc->frad);
352 if (state(hdlc)->settings.lmi == LMI_NONE)
353 pvc->state.active = netif_carrier_ok(pvc->frad);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
355 pvc_carrier(pvc->state.active, pvc);
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200356 state(hdlc)->dce_changed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 }
358 return 0;
359}
360
361
362
363static int pvc_close(struct net_device *dev)
364{
Wang Chen2baf8a22008-11-21 16:34:18 -0800365 pvc_device *pvc = dev->ml_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
367 if (--pvc->open_count == 0) {
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200368 hdlc_device *hdlc = dev_to_hdlc(pvc->frad);
369 if (state(hdlc)->settings.lmi == LMI_NONE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 pvc->state.active = 0;
371
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200372 if (state(hdlc)->settings.dce) {
373 state(hdlc)->dce_changed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 pvc->state.active = 0;
375 }
376 }
377 return 0;
378}
379
380
381
Adrian Bunk7665a082005-09-09 23:17:28 -0700382static int pvc_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383{
Wang Chen2baf8a22008-11-21 16:34:18 -0800384 pvc_device *pvc = dev->ml_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 fr_proto_pvc_info info;
386
387 if (ifr->ifr_settings.type == IF_GET_PROTO) {
388 if (dev->type == ARPHRD_ETHER)
389 ifr->ifr_settings.type = IF_PROTO_FR_ETH_PVC;
390 else
391 ifr->ifr_settings.type = IF_PROTO_FR_PVC;
392
393 if (ifr->ifr_settings.size < sizeof(info)) {
394 /* data size wanted */
395 ifr->ifr_settings.size = sizeof(info);
396 return -ENOBUFS;
397 }
398
399 info.dlci = pvc->dlci;
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200400 memcpy(info.master, pvc->frad->name, IFNAMSIZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 if (copy_to_user(ifr->ifr_settings.ifs_ifsu.fr_pvc_info,
402 &info, sizeof(info)))
403 return -EFAULT;
404 return 0;
405 }
406
407 return -EINVAL;
408}
409
Stephen Hemmingerd71a6742009-08-31 19:50:47 +0000410static netdev_tx_t pvc_xmit(struct sk_buff *skb, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411{
Wang Chen2baf8a22008-11-21 16:34:18 -0800412 pvc_device *pvc = dev->ml_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
414 if (pvc->state.active) {
415 if (dev->type == ARPHRD_ETHER) {
416 int pad = ETH_ZLEN - skb->len;
417 if (pad > 0) { /* Pad the frame with zeros */
418 int len = skb->len;
419 if (skb_tailroom(skb) < pad)
420 if (pskb_expand_head(skb, 0, pad,
421 GFP_ATOMIC)) {
Krzysztof Halasa198191c2008-06-30 23:26:53 +0200422 dev->stats.tx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 dev_kfree_skb(skb);
Patrick McHardy6ed10652009-06-23 06:03:08 +0000424 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 }
426 skb_put(skb, pad);
427 memset(skb->data + len, 0, pad);
428 }
Harvey Harrison09640e62009-02-01 00:45:17 -0800429 skb->protocol = cpu_to_be16(ETH_P_802_3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 }
431 if (!fr_hard_header(&skb, pvc->dlci)) {
Krzysztof Halasa198191c2008-06-30 23:26:53 +0200432 dev->stats.tx_bytes += skb->len;
433 dev->stats.tx_packets++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 if (pvc->state.fecn) /* TX Congestion counter */
Krzysztof Halasa198191c2008-06-30 23:26:53 +0200435 dev->stats.tx_compressed++;
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200436 skb->dev = pvc->frad;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 dev_queue_xmit(skb);
Patrick McHardy6ed10652009-06-23 06:03:08 +0000438 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 }
440 }
441
Krzysztof Halasa198191c2008-06-30 23:26:53 +0200442 dev->stats.tx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 dev_kfree_skb(skb);
Patrick McHardy6ed10652009-06-23 06:03:08 +0000444 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445}
446
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447static inline void fr_log_dlci_active(pvc_device *pvc)
448{
Joe Perches12a3bfe2011-06-26 19:01:28 +0000449 netdev_info(pvc->frad, "DLCI %d [%s%s%s]%s %s\n",
450 pvc->dlci,
451 pvc->main ? pvc->main->name : "",
452 pvc->main && pvc->ether ? " " : "",
453 pvc->ether ? pvc->ether->name : "",
454 pvc->state.new ? " new" : "",
455 !pvc->state.exist ? "deleted" :
456 pvc->state.active ? "active" : "inactive");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457}
458
459
460
461static inline u8 fr_lmi_nextseq(u8 x)
462{
463 x++;
464 return x ? x : 1;
465}
466
467
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468static void fr_lmi_send(struct net_device *dev, int fullrep)
469{
470 hdlc_device *hdlc = dev_to_hdlc(dev);
471 struct sk_buff *skb;
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200472 pvc_device *pvc = state(hdlc)->first_pvc;
473 int lmi = state(hdlc)->settings.lmi;
474 int dce = state(hdlc)->settings.dce;
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +0200475 int len = lmi == LMI_ANSI ? LMI_ANSI_LENGTH : LMI_CCITT_CISCO_LENGTH;
476 int stat_len = (lmi == LMI_CISCO) ? 6 : 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 u8 *data;
478 int i = 0;
479
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +0200480 if (dce && fullrep) {
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200481 len += state(hdlc)->dce_pvc_count * (2 + stat_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 if (len > HDLC_MAX_MRU) {
Joe Perches12a3bfe2011-06-26 19:01:28 +0000483 netdev_warn(dev, "Too many PVCs while sending LMI full report\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 return;
485 }
486 }
487
488 skb = dev_alloc_skb(len);
489 if (!skb) {
Joe Perches12a3bfe2011-06-26 19:01:28 +0000490 netdev_warn(dev, "Memory squeeze on fr_lmi_send()\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 return;
492 }
493 memset(skb->data, 0, len);
494 skb_reserve(skb, 4);
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +0200495 if (lmi == LMI_CISCO) {
Harvey Harrison09640e62009-02-01 00:45:17 -0800496 skb->protocol = cpu_to_be16(NLPID_CISCO_LMI);
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +0200497 fr_hard_header(&skb, LMI_CISCO_DLCI);
498 } else {
Harvey Harrison09640e62009-02-01 00:45:17 -0800499 skb->protocol = cpu_to_be16(NLPID_CCITT_ANSI_LMI);
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +0200500 fr_hard_header(&skb, LMI_CCITT_ANSI_DLCI);
501 }
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700502 data = skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 data[i++] = LMI_CALLREF;
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +0200504 data[i++] = dce ? LMI_STATUS : LMI_STATUS_ENQUIRY;
505 if (lmi == LMI_ANSI)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 data[i++] = LMI_ANSI_LOCKSHIFT;
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +0200507 data[i++] = lmi == LMI_CCITT ? LMI_CCITT_REPTYPE :
508 LMI_ANSI_CISCO_REPTYPE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 data[i++] = LMI_REPT_LEN;
510 data[i++] = fullrep ? LMI_FULLREP : LMI_INTEGRITY;
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +0200511 data[i++] = lmi == LMI_CCITT ? LMI_CCITT_ALIVE : LMI_ANSI_CISCO_ALIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 data[i++] = LMI_INTEG_LEN;
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200513 data[i++] = state(hdlc)->txseq =
514 fr_lmi_nextseq(state(hdlc)->txseq);
515 data[i++] = state(hdlc)->rxseq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +0200517 if (dce && fullrep) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 while (pvc) {
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +0200519 data[i++] = lmi == LMI_CCITT ? LMI_CCITT_PVCSTAT :
520 LMI_ANSI_CISCO_PVCSTAT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 data[i++] = stat_len;
522
523 /* LMI start/restart */
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200524 if (state(hdlc)->reliable && !pvc->state.exist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 pvc->state.exist = pvc->state.new = 1;
526 fr_log_dlci_active(pvc);
527 }
528
529 /* ifconfig PVC up */
530 if (pvc->open_count && !pvc->state.active &&
531 pvc->state.exist && !pvc->state.new) {
532 pvc_carrier(1, pvc);
533 pvc->state.active = 1;
534 fr_log_dlci_active(pvc);
535 }
536
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +0200537 if (lmi == LMI_CISCO) {
538 data[i] = pvc->dlci >> 8;
539 data[i + 1] = pvc->dlci & 0xFF;
540 } else {
541 data[i] = (pvc->dlci >> 4) & 0x3F;
542 data[i + 1] = ((pvc->dlci << 3) & 0x78) | 0x80;
543 data[i + 2] = 0x80;
544 }
545
546 if (pvc->state.new)
547 data[i + 2] |= 0x08;
548 else if (pvc->state.active)
549 data[i + 2] |= 0x02;
550
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 i += stat_len;
552 pvc = pvc->next;
553 }
554 }
555
556 skb_put(skb, i);
557 skb->priority = TC_PRIO_CONTROL;
558 skb->dev = dev;
Arnaldo Carvalho de Meloc1d2bbe2007-04-10 20:45:18 -0700559 skb_reset_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
561 dev_queue_xmit(skb);
562}
563
564
565
566static void fr_set_link_state(int reliable, struct net_device *dev)
567{
568 hdlc_device *hdlc = dev_to_hdlc(dev);
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200569 pvc_device *pvc = state(hdlc)->first_pvc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200571 state(hdlc)->reliable = reliable;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 if (reliable) {
Krzysztof Halasac2ce9202006-07-12 13:46:12 -0700573 netif_dormant_off(dev);
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200574 state(hdlc)->n391cnt = 0; /* Request full status */
575 state(hdlc)->dce_changed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200577 if (state(hdlc)->settings.lmi == LMI_NONE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 while (pvc) { /* Activate all PVCs */
579 pvc_carrier(1, pvc);
580 pvc->state.exist = pvc->state.active = 1;
581 pvc->state.new = 0;
582 pvc = pvc->next;
583 }
584 }
585 } else {
Krzysztof Halasac2ce9202006-07-12 13:46:12 -0700586 netif_dormant_on(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 while (pvc) { /* Deactivate all PVCs */
588 pvc_carrier(0, pvc);
589 pvc->state.exist = pvc->state.active = 0;
590 pvc->state.new = 0;
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200591 if (!state(hdlc)->settings.dce)
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +0200592 pvc->state.bandwidth = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 pvc = pvc->next;
594 }
595 }
596}
597
598
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599static void fr_timer(unsigned long arg)
600{
601 struct net_device *dev = (struct net_device *)arg;
602 hdlc_device *hdlc = dev_to_hdlc(dev);
603 int i, cnt = 0, reliable;
604 u32 list;
605
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200606 if (state(hdlc)->settings.dce) {
607 reliable = state(hdlc)->request &&
608 time_before(jiffies, state(hdlc)->last_poll +
609 state(hdlc)->settings.t392 * HZ);
610 state(hdlc)->request = 0;
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +0200611 } else {
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200612 state(hdlc)->last_errors <<= 1; /* Shift the list */
613 if (state(hdlc)->request) {
614 if (state(hdlc)->reliable)
Joe Perches12a3bfe2011-06-26 19:01:28 +0000615 netdev_info(dev, "No LMI status reply received\n");
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200616 state(hdlc)->last_errors |= 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 }
618
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200619 list = state(hdlc)->last_errors;
620 for (i = 0; i < state(hdlc)->settings.n393; i++, list >>= 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 cnt += (list & 1); /* errors count */
622
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200623 reliable = (cnt < state(hdlc)->settings.n392);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 }
625
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200626 if (state(hdlc)->reliable != reliable) {
Joe Perches12a3bfe2011-06-26 19:01:28 +0000627 netdev_info(dev, "Link %sreliable\n", reliable ? "" : "un");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 fr_set_link_state(reliable, dev);
629 }
630
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200631 if (state(hdlc)->settings.dce)
632 state(hdlc)->timer.expires = jiffies +
633 state(hdlc)->settings.t392 * HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 else {
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200635 if (state(hdlc)->n391cnt)
636 state(hdlc)->n391cnt--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200638 fr_lmi_send(dev, state(hdlc)->n391cnt == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200640 state(hdlc)->last_poll = jiffies;
641 state(hdlc)->request = 1;
642 state(hdlc)->timer.expires = jiffies +
643 state(hdlc)->settings.t391 * HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 }
645
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200646 state(hdlc)->timer.function = fr_timer;
647 state(hdlc)->timer.data = arg;
648 add_timer(&state(hdlc)->timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649}
650
651
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652static int fr_lmi_recv(struct net_device *dev, struct sk_buff *skb)
653{
654 hdlc_device *hdlc = dev_to_hdlc(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 pvc_device *pvc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 u8 rxseq, txseq;
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200657 int lmi = state(hdlc)->settings.lmi;
658 int dce = state(hdlc)->settings.dce;
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +0200659 int stat_len = (lmi == LMI_CISCO) ? 6 : 3, reptype, error, no_ram, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +0200661 if (skb->len < (lmi == LMI_ANSI ? LMI_ANSI_LENGTH :
662 LMI_CCITT_CISCO_LENGTH)) {
Joe Perches12a3bfe2011-06-26 19:01:28 +0000663 netdev_info(dev, "Short LMI frame\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 return 1;
665 }
666
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +0200667 if (skb->data[3] != (lmi == LMI_CISCO ? NLPID_CISCO_LMI :
668 NLPID_CCITT_ANSI_LMI)) {
Joe Perches12a3bfe2011-06-26 19:01:28 +0000669 netdev_info(dev, "Received non-LMI frame with LMI DLCI\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 return 1;
671 }
672
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +0200673 if (skb->data[4] != LMI_CALLREF) {
Joe Perches12a3bfe2011-06-26 19:01:28 +0000674 netdev_info(dev, "Invalid LMI Call reference (0x%02X)\n",
675 skb->data[4]);
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +0200676 return 1;
677 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +0200679 if (skb->data[5] != (dce ? LMI_STATUS_ENQUIRY : LMI_STATUS)) {
Joe Perches12a3bfe2011-06-26 19:01:28 +0000680 netdev_info(dev, "Invalid LMI Message type (0x%02X)\n",
681 skb->data[5]);
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +0200682 return 1;
683 }
684
685 if (lmi == LMI_ANSI) {
686 if (skb->data[6] != LMI_ANSI_LOCKSHIFT) {
Joe Perches12a3bfe2011-06-26 19:01:28 +0000687 netdev_info(dev, "Not ANSI locking shift in LMI message (0x%02X)\n",
688 skb->data[6]);
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +0200689 return 1;
690 }
691 i = 7;
692 } else
693 i = 6;
694
695 if (skb->data[i] != (lmi == LMI_CCITT ? LMI_CCITT_REPTYPE :
696 LMI_ANSI_CISCO_REPTYPE)) {
Joe Perches12a3bfe2011-06-26 19:01:28 +0000697 netdev_info(dev, "Not an LMI Report type IE (0x%02X)\n",
698 skb->data[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 return 1;
700 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +0200702 if (skb->data[++i] != LMI_REPT_LEN) {
Joe Perches12a3bfe2011-06-26 19:01:28 +0000703 netdev_info(dev, "Invalid LMI Report type IE length (%u)\n",
704 skb->data[i]);
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +0200705 return 1;
706 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +0200708 reptype = skb->data[++i];
709 if (reptype != LMI_INTEGRITY && reptype != LMI_FULLREP) {
Joe Perches12a3bfe2011-06-26 19:01:28 +0000710 netdev_info(dev, "Unsupported LMI Report type (0x%02X)\n",
711 reptype);
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +0200712 return 1;
713 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +0200715 if (skb->data[++i] != (lmi == LMI_CCITT ? LMI_CCITT_ALIVE :
716 LMI_ANSI_CISCO_ALIVE)) {
Joe Perches12a3bfe2011-06-26 19:01:28 +0000717 netdev_info(dev, "Not an LMI Link integrity verification IE (0x%02X)\n",
718 skb->data[i]);
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +0200719 return 1;
720 }
721
722 if (skb->data[++i] != LMI_INTEG_LEN) {
Joe Perches12a3bfe2011-06-26 19:01:28 +0000723 netdev_info(dev, "Invalid LMI Link integrity verification IE length (%u)\n",
724 skb->data[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 return 1;
726 }
727 i++;
728
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200729 state(hdlc)->rxseq = skb->data[i++]; /* TX sequence from peer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 rxseq = skb->data[i++]; /* Should confirm our sequence */
731
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200732 txseq = state(hdlc)->txseq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +0200734 if (dce)
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200735 state(hdlc)->last_poll = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736
737 error = 0;
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200738 if (!state(hdlc)->reliable)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 error = 1;
740
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200741 if (rxseq == 0 || rxseq != txseq) { /* Ask for full report next time */
742 state(hdlc)->n391cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 error = 1;
744 }
745
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +0200746 if (dce) {
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200747 if (state(hdlc)->fullrep_sent && !error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748/* Stop sending full report - the last one has been confirmed by DTE */
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200749 state(hdlc)->fullrep_sent = 0;
750 pvc = state(hdlc)->first_pvc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 while (pvc) {
752 if (pvc->state.new) {
753 pvc->state.new = 0;
754
755/* Tell DTE that new PVC is now active */
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200756 state(hdlc)->dce_changed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 }
758 pvc = pvc->next;
759 }
760 }
761
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200762 if (state(hdlc)->dce_changed) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 reptype = LMI_FULLREP;
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200764 state(hdlc)->fullrep_sent = 1;
765 state(hdlc)->dce_changed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 }
767
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200768 state(hdlc)->request = 1; /* got request */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 fr_lmi_send(dev, reptype == LMI_FULLREP ? 1 : 0);
770 return 0;
771 }
772
773 /* DTE */
774
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200775 state(hdlc)->request = 0; /* got response, no request pending */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
777 if (error)
778 return 0;
779
780 if (reptype != LMI_FULLREP)
781 return 0;
782
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200783 pvc = state(hdlc)->first_pvc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784
785 while (pvc) {
786 pvc->state.deleted = 1;
787 pvc = pvc->next;
788 }
789
790 no_ram = 0;
791 while (skb->len >= i + 2 + stat_len) {
792 u16 dlci;
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +0200793 u32 bw;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 unsigned int active, new;
795
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +0200796 if (skb->data[i] != (lmi == LMI_CCITT ? LMI_CCITT_PVCSTAT :
797 LMI_ANSI_CISCO_PVCSTAT)) {
Joe Perches12a3bfe2011-06-26 19:01:28 +0000798 netdev_info(dev, "Not an LMI PVC status IE (0x%02X)\n",
799 skb->data[i]);
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +0200800 return 1;
801 }
802
803 if (skb->data[++i] != stat_len) {
Joe Perches12a3bfe2011-06-26 19:01:28 +0000804 netdev_info(dev, "Invalid LMI PVC status IE length (%u)\n",
805 skb->data[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 return 1;
807 }
808 i++;
809
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +0200810 new = !! (skb->data[i + 2] & 0x08);
811 active = !! (skb->data[i + 2] & 0x02);
812 if (lmi == LMI_CISCO) {
813 dlci = (skb->data[i] << 8) | skb->data[i + 1];
814 bw = (skb->data[i + 3] << 16) |
815 (skb->data[i + 4] << 8) |
816 (skb->data[i + 5]);
817 } else {
818 dlci = ((skb->data[i] & 0x3F) << 4) |
819 ((skb->data[i + 1] & 0x78) >> 3);
820 bw = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822
823 pvc = add_pvc(dev, dlci);
824
825 if (!pvc && !no_ram) {
Joe Perches12a3bfe2011-06-26 19:01:28 +0000826 netdev_warn(dev, "Memory squeeze on fr_lmi_recv()\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 no_ram = 1;
828 }
829
830 if (pvc) {
831 pvc->state.exist = 1;
832 pvc->state.deleted = 0;
833 if (active != pvc->state.active ||
834 new != pvc->state.new ||
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +0200835 bw != pvc->state.bandwidth ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 !pvc->state.exist) {
837 pvc->state.new = new;
838 pvc->state.active = active;
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +0200839 pvc->state.bandwidth = bw;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 pvc_carrier(active, pvc);
841 fr_log_dlci_active(pvc);
842 }
843 }
844
845 i += stat_len;
846 }
847
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200848 pvc = state(hdlc)->first_pvc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849
850 while (pvc) {
851 if (pvc->state.deleted && pvc->state.exist) {
852 pvc_carrier(0, pvc);
853 pvc->state.active = pvc->state.new = 0;
854 pvc->state.exist = 0;
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +0200855 pvc->state.bandwidth = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 fr_log_dlci_active(pvc);
857 }
858 pvc = pvc->next;
859 }
860
861 /* Next full report after N391 polls */
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200862 state(hdlc)->n391cnt = state(hdlc)->settings.n391;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863
864 return 0;
865}
866
867
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868static int fr_rx(struct sk_buff *skb)
869{
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200870 struct net_device *frad = skb->dev;
871 hdlc_device *hdlc = dev_to_hdlc(frad);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 fr_hdr *fh = (fr_hdr*)skb->data;
873 u8 *data = skb->data;
874 u16 dlci;
875 pvc_device *pvc;
876 struct net_device *dev = NULL;
877
878 if (skb->len <= 4 || fh->ea1 || data[2] != FR_UI)
879 goto rx_error;
880
881 dlci = q922_to_dlci(skb->data);
882
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +0200883 if ((dlci == LMI_CCITT_ANSI_DLCI &&
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200884 (state(hdlc)->settings.lmi == LMI_ANSI ||
885 state(hdlc)->settings.lmi == LMI_CCITT)) ||
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +0200886 (dlci == LMI_CISCO_DLCI &&
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200887 state(hdlc)->settings.lmi == LMI_CISCO)) {
888 if (fr_lmi_recv(frad, skb))
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +0200889 goto rx_error;
890 dev_kfree_skb_any(skb);
891 return NET_RX_SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 }
893
894 pvc = find_pvc(hdlc, dlci);
895 if (!pvc) {
896#ifdef DEBUG_PKT
Joe Perches12a3bfe2011-06-26 19:01:28 +0000897 netdev_info(frad, "No PVC for received frame's DLCI %d\n",
898 dlci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899#endif
900 dev_kfree_skb_any(skb);
901 return NET_RX_DROP;
902 }
903
904 if (pvc->state.fecn != fh->fecn) {
905#ifdef DEBUG_ECN
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200906 printk(KERN_DEBUG "%s: DLCI %d FECN O%s\n", frad->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 dlci, fh->fecn ? "N" : "FF");
908#endif
909 pvc->state.fecn ^= 1;
910 }
911
912 if (pvc->state.becn != fh->becn) {
913#ifdef DEBUG_ECN
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200914 printk(KERN_DEBUG "%s: DLCI %d BECN O%s\n", frad->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 dlci, fh->becn ? "N" : "FF");
916#endif
917 pvc->state.becn ^= 1;
918 }
919
920
921 if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL) {
Krzysztof Halasa198191c2008-06-30 23:26:53 +0200922 frad->stats.rx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 return NET_RX_DROP;
924 }
925
926 if (data[3] == NLPID_IP) {
927 skb_pull(skb, 4); /* Remove 4-byte header (hdr, UI, NLPID) */
928 dev = pvc->main;
929 skb->protocol = htons(ETH_P_IP);
930
931 } else if (data[3] == NLPID_IPV6) {
932 skb_pull(skb, 4); /* Remove 4-byte header (hdr, UI, NLPID) */
933 dev = pvc->main;
934 skb->protocol = htons(ETH_P_IPV6);
935
936 } else if (skb->len > 10 && data[3] == FR_PAD &&
937 data[4] == NLPID_SNAP && data[5] == FR_PAD) {
Krzysztof Halasaabf17ff2007-04-27 13:13:33 +0200938 u16 oui = ntohs(*(__be16*)(data + 6));
939 u16 pid = ntohs(*(__be16*)(data + 8));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 skb_pull(skb, 10);
941
942 switch ((((u32)oui) << 16) | pid) {
943 case ETH_P_ARP: /* routed frame with SNAP */
944 case ETH_P_IPX:
945 case ETH_P_IP: /* a long variant */
946 case ETH_P_IPV6:
947 dev = pvc->main;
948 skb->protocol = htons(pid);
949 break;
950
951 case 0x80C20007: /* bridged Ethernet frame */
952 if ((dev = pvc->ether) != NULL)
953 skb->protocol = eth_type_trans(skb, dev);
954 break;
955
956 default:
Joe Perches12a3bfe2011-06-26 19:01:28 +0000957 netdev_info(frad, "Unsupported protocol, OUI=%x PID=%x\n",
958 oui, pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 dev_kfree_skb_any(skb);
960 return NET_RX_DROP;
961 }
962 } else {
Joe Perches12a3bfe2011-06-26 19:01:28 +0000963 netdev_info(frad, "Unsupported protocol, NLPID=%x length=%i\n",
964 data[3], skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 dev_kfree_skb_any(skb);
966 return NET_RX_DROP;
967 }
968
969 if (dev) {
Krzysztof Halasa198191c2008-06-30 23:26:53 +0200970 dev->stats.rx_packets++; /* PVC traffic */
971 dev->stats.rx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 if (pvc->state.becn)
Krzysztof Halasa198191c2008-06-30 23:26:53 +0200973 dev->stats.rx_compressed++;
Krzysztof Halasa54364b72008-06-29 21:48:11 +0200974 skb->dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 netif_rx(skb);
976 return NET_RX_SUCCESS;
977 } else {
978 dev_kfree_skb_any(skb);
979 return NET_RX_DROP;
980 }
981
982 rx_error:
Krzysztof Halasa198191c2008-06-30 23:26:53 +0200983 frad->stats.rx_errors++; /* Mark error */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 dev_kfree_skb_any(skb);
985 return NET_RX_DROP;
986}
987
988
989
990static void fr_start(struct net_device *dev)
991{
992 hdlc_device *hdlc = dev_to_hdlc(dev);
993#ifdef DEBUG_LINK
994 printk(KERN_DEBUG "fr_start\n");
995#endif
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200996 if (state(hdlc)->settings.lmi != LMI_NONE) {
997 state(hdlc)->reliable = 0;
998 state(hdlc)->dce_changed = 1;
999 state(hdlc)->request = 0;
1000 state(hdlc)->fullrep_sent = 0;
1001 state(hdlc)->last_errors = 0xFFFFFFFF;
1002 state(hdlc)->n391cnt = 0;
1003 state(hdlc)->txseq = state(hdlc)->rxseq = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +02001005 init_timer(&state(hdlc)->timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 /* First poll after 1 s */
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +02001007 state(hdlc)->timer.expires = jiffies + HZ;
1008 state(hdlc)->timer.function = fr_timer;
1009 state(hdlc)->timer.data = (unsigned long)dev;
1010 add_timer(&state(hdlc)->timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 } else
1012 fr_set_link_state(1, dev);
1013}
1014
1015
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016static void fr_stop(struct net_device *dev)
1017{
1018 hdlc_device *hdlc = dev_to_hdlc(dev);
1019#ifdef DEBUG_LINK
1020 printk(KERN_DEBUG "fr_stop\n");
1021#endif
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +02001022 if (state(hdlc)->settings.lmi != LMI_NONE)
1023 del_timer_sync(&state(hdlc)->timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 fr_set_link_state(0, dev);
1025}
1026
1027
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028static void fr_close(struct net_device *dev)
1029{
1030 hdlc_device *hdlc = dev_to_hdlc(dev);
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +02001031 pvc_device *pvc = state(hdlc)->first_pvc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032
1033 while (pvc) { /* Shutdown all PVCs for this FRAD */
1034 if (pvc->main)
1035 dev_close(pvc->main);
1036 if (pvc->ether)
1037 dev_close(pvc->ether);
1038 pvc = pvc->next;
1039 }
1040}
1041
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +02001042
1043static void pvc_setup(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044{
1045 dev->type = ARPHRD_DLCI;
1046 dev->flags = IFF_POINTOPOINT;
1047 dev->hard_header_len = 10;
1048 dev->addr_len = 2;
Eric Dumazet93f154b2009-05-18 22:19:19 -07001049 dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050}
1051
Krzysztof Hałasa991990a2009-01-08 22:52:11 +01001052static const struct net_device_ops pvc_ops = {
1053 .ndo_open = pvc_open,
1054 .ndo_stop = pvc_close,
1055 .ndo_change_mtu = hdlc_change_mtu,
1056 .ndo_start_xmit = pvc_xmit,
1057 .ndo_do_ioctl = pvc_ioctl,
1058};
1059
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +02001060static int fr_add_pvc(struct net_device *frad, unsigned int dlci, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061{
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +02001062 hdlc_device *hdlc = dev_to_hdlc(frad);
Krzysztof Halasa141766c2008-07-01 14:42:07 +02001063 pvc_device *pvc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 struct net_device *dev;
Jiri Pirko1c5cae82011-04-30 01:21:32 +00001065 int used;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +02001067 if ((pvc = add_pvc(frad, dlci)) == NULL) {
Joe Perches12a3bfe2011-06-26 19:01:28 +00001068 netdev_warn(frad, "Memory squeeze on fr_add_pvc()\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 return -ENOBUFS;
1070 }
1071
1072 if (*get_dev_p(pvc, type))
1073 return -EEXIST;
1074
1075 used = pvc_is_used(pvc);
1076
Neil Horman550fd082011-07-26 06:05:38 +00001077 if (type == ARPHRD_ETHER) {
Krzysztof Halasa141766c2008-07-01 14:42:07 +02001078 dev = alloc_netdev(0, "pvceth%d", ether_setup);
Neil Horman550fd082011-07-26 06:05:38 +00001079 dev->priv_flags &= ~IFF_TX_SKB_SHARING;
1080 } else
Krzysztof Halasa141766c2008-07-01 14:42:07 +02001081 dev = alloc_netdev(0, "pvc%d", pvc_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082
1083 if (!dev) {
Joe Perches12a3bfe2011-06-26 19:01:28 +00001084 netdev_warn(frad, "Memory squeeze on fr_pvc()\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 delete_unused_pvcs(hdlc);
1086 return -ENOBUFS;
1087 }
1088
Krzysztof Halasa47eaa262008-02-01 22:39:50 +01001089 if (type == ARPHRD_ETHER)
Danny Kukawkaf2cedb62012-02-15 06:45:39 +00001090 eth_hw_addr_random(dev);
Krzysztof Halasa47eaa262008-02-01 22:39:50 +01001091 else {
Krzysztof Halasaabf17ff2007-04-27 13:13:33 +02001092 *(__be16*)dev->dev_addr = htons(dlci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 dlci_to_q922(dev->broadcast, dlci);
1094 }
Krzysztof Hałasa991990a2009-01-08 22:52:11 +01001095 dev->netdev_ops = &pvc_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 dev->mtu = HDLC_MAX_MTU;
1097 dev->tx_queue_len = 0;
Wang Chen2baf8a22008-11-21 16:34:18 -08001098 dev->ml_priv = pvc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 if (register_netdevice(dev) != 0) {
1101 free_netdev(dev);
1102 delete_unused_pvcs(hdlc);
1103 return -EIO;
1104 }
1105
1106 dev->destructor = free_netdev;
1107 *get_dev_p(pvc, type) = dev;
1108 if (!used) {
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +02001109 state(hdlc)->dce_changed = 1;
1110 state(hdlc)->dce_pvc_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 }
1112 return 0;
1113}
1114
1115
1116
1117static int fr_del_pvc(hdlc_device *hdlc, unsigned int dlci, int type)
1118{
1119 pvc_device *pvc;
1120 struct net_device *dev;
1121
1122 if ((pvc = find_pvc(hdlc, dlci)) == NULL)
1123 return -ENOENT;
1124
1125 if ((dev = *get_dev_p(pvc, type)) == NULL)
1126 return -ENOENT;
1127
1128 if (dev->flags & IFF_UP)
1129 return -EBUSY; /* PVC in use */
1130
1131 unregister_netdevice(dev); /* the destructor will free_netdev(dev) */
1132 *get_dev_p(pvc, type) = NULL;
1133
1134 if (!pvc_is_used(pvc)) {
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +02001135 state(hdlc)->dce_pvc_count--;
1136 state(hdlc)->dce_changed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 }
1138 delete_unused_pvcs(hdlc);
1139 return 0;
1140}
1141
1142
1143
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +02001144static void fr_destroy(struct net_device *frad)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145{
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +02001146 hdlc_device *hdlc = dev_to_hdlc(frad);
1147 pvc_device *pvc = state(hdlc)->first_pvc;
1148 state(hdlc)->first_pvc = NULL; /* All PVCs destroyed */
1149 state(hdlc)->dce_pvc_count = 0;
1150 state(hdlc)->dce_changed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151
1152 while (pvc) {
1153 pvc_device *next = pvc->next;
1154 /* destructors will free_netdev() main and ether */
1155 if (pvc->main)
1156 unregister_netdevice(pvc->main);
1157
1158 if (pvc->ether)
1159 unregister_netdevice(pvc->ether);
1160
1161 kfree(pvc);
1162 pvc = next;
1163 }
1164}
1165
1166
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +02001167static struct hdlc_proto proto = {
1168 .close = fr_close,
1169 .start = fr_start,
1170 .stop = fr_stop,
1171 .detach = fr_destroy,
1172 .ioctl = fr_ioctl,
Krzysztof Halasa40d25142008-02-01 22:37:12 +01001173 .netif_rx = fr_rx,
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +02001174 .module = THIS_MODULE,
1175};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +02001177
1178static int fr_ioctl(struct net_device *dev, struct ifreq *ifr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179{
1180 fr_proto __user *fr_s = ifr->ifr_settings.ifs_ifsu.fr;
1181 const size_t size = sizeof(fr_proto);
1182 fr_proto new_settings;
1183 hdlc_device *hdlc = dev_to_hdlc(dev);
1184 fr_proto_pvc pvc;
1185 int result;
1186
1187 switch (ifr->ifr_settings.type) {
1188 case IF_GET_PROTO:
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +02001189 if (dev_to_hdlc(dev)->proto != &proto) /* Different proto */
1190 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 ifr->ifr_settings.type = IF_PROTO_FR;
1192 if (ifr->ifr_settings.size < size) {
1193 ifr->ifr_settings.size = size; /* data size wanted */
1194 return -ENOBUFS;
1195 }
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +02001196 if (copy_to_user(fr_s, &state(hdlc)->settings, size))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 return -EFAULT;
1198 return 0;
1199
1200 case IF_PROTO_FR:
Rudy Matela6f7ad1e2009-11-29 23:42:42 -08001201 if (!capable(CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 return -EPERM;
1203
Rudy Matela6f7ad1e2009-11-29 23:42:42 -08001204 if (dev->flags & IFF_UP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 return -EBUSY;
1206
1207 if (copy_from_user(&new_settings, fr_s, size))
1208 return -EFAULT;
1209
1210 if (new_settings.lmi == LMI_DEFAULT)
1211 new_settings.lmi = LMI_ANSI;
1212
1213 if ((new_settings.lmi != LMI_NONE &&
1214 new_settings.lmi != LMI_ANSI &&
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +02001215 new_settings.lmi != LMI_CCITT &&
1216 new_settings.lmi != LMI_CISCO) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 new_settings.t391 < 1 ||
1218 new_settings.t392 < 2 ||
1219 new_settings.n391 < 1 ||
1220 new_settings.n392 < 1 ||
1221 new_settings.n393 < new_settings.n392 ||
1222 new_settings.n393 > 32 ||
1223 (new_settings.dce != 0 &&
1224 new_settings.dce != 1))
1225 return -EINVAL;
1226
1227 result=hdlc->attach(dev, ENCODING_NRZ,PARITY_CRC16_PR1_CCITT);
1228 if (result)
1229 return result;
1230
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +02001231 if (dev_to_hdlc(dev)->proto != &proto) { /* Different proto */
Krzysztof Halasa40d25142008-02-01 22:37:12 +01001232 result = attach_hdlc_protocol(dev, &proto,
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +02001233 sizeof(struct frad_state));
1234 if (result)
1235 return result;
1236 state(hdlc)->first_pvc = NULL;
1237 state(hdlc)->dce_pvc_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 }
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +02001239 memcpy(&state(hdlc)->settings, &new_settings, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 dev->type = ARPHRD_FRAD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 return 0;
1242
1243 case IF_PROTO_FR_ADD_PVC:
1244 case IF_PROTO_FR_DEL_PVC:
1245 case IF_PROTO_FR_ADD_ETH_PVC:
1246 case IF_PROTO_FR_DEL_ETH_PVC:
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +02001247 if (dev_to_hdlc(dev)->proto != &proto) /* Different proto */
1248 return -EINVAL;
1249
Rudy Matela6f7ad1e2009-11-29 23:42:42 -08001250 if (!capable(CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 return -EPERM;
1252
1253 if (copy_from_user(&pvc, ifr->ifr_settings.ifs_ifsu.fr_pvc,
1254 sizeof(fr_proto_pvc)))
1255 return -EFAULT;
1256
1257 if (pvc.dlci <= 0 || pvc.dlci >= 1024)
1258 return -EINVAL; /* Only 10 bits, DLCI 0 reserved */
1259
1260 if (ifr->ifr_settings.type == IF_PROTO_FR_ADD_ETH_PVC ||
1261 ifr->ifr_settings.type == IF_PROTO_FR_DEL_ETH_PVC)
1262 result = ARPHRD_ETHER; /* bridged Ethernet device */
1263 else
1264 result = ARPHRD_DLCI;
1265
1266 if (ifr->ifr_settings.type == IF_PROTO_FR_ADD_PVC ||
1267 ifr->ifr_settings.type == IF_PROTO_FR_ADD_ETH_PVC)
1268 return fr_add_pvc(dev, pvc.dlci, result);
1269 else
1270 return fr_del_pvc(hdlc, pvc.dlci, result);
1271 }
1272
1273 return -EINVAL;
1274}
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +02001275
1276
1277static int __init mod_init(void)
1278{
1279 register_hdlc_protocol(&proto);
1280 return 0;
1281}
1282
1283
1284static void __exit mod_exit(void)
1285{
1286 unregister_hdlc_protocol(&proto);
1287}
1288
1289
1290module_init(mod_init);
1291module_exit(mod_exit);
1292
1293MODULE_AUTHOR("Krzysztof Halasa <khc@pm.waw.pl>");
1294MODULE_DESCRIPTION("Frame-Relay protocol support for generic HDLC");
1295MODULE_LICENSE("GPL v2");