hdlc_fr.c 29.4 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4
/*
 * Generic HDLC support routines for Linux
 * Frame Relay support
 *
5
 * Copyright (C) 1999 - 2006 Krzysztof Halasa <khc@pm.waw.pl>
L
Linus Torvalds 已提交
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of version 2 of the GNU General Public License
 * as published by the Free Software Foundation.
 *

            Theory of PVC state

 DCE mode:

 (exist,new) -> 0,0 when "PVC create" or if "link unreliable"
         0,x -> 1,1 if "link reliable" when sending FULL STATUS
         1,1 -> 1,0 if received FULL STATUS ACK

 (active)    -> 0 when "ifconfig PVC down" or "link unreliable" or "PVC create"
             -> 1 when "PVC up" and (exist,new) = 1,0

 DTE mode:
 (exist,new,active) = FULL STATUS if "link reliable"
		    = 0, 0, 0 if "link unreliable"
 No LMI:
 active = open and "link reliable"
 exist = new = not used

K
Krzysztof Halasa 已提交
30 31 32 33
 CCITT LMI: ITU-T Q.933 Annex A
 ANSI LMI: ANSI T1.617 Annex D
 CISCO LMI: the original, aka "Gang of Four" LMI

L
Linus Torvalds 已提交
34 35 36
*/

#include <linux/errno.h>
37 38
#include <linux/etherdevice.h>
#include <linux/hdlc.h>
L
Linus Torvalds 已提交
39
#include <linux/if_arp.h>
40
#include <linux/inetdevice.h>
L
Linus Torvalds 已提交
41
#include <linux/init.h>
42 43
#include <linux/kernel.h>
#include <linux/module.h>
L
Linus Torvalds 已提交
44
#include <linux/pkt_sched.h>
45
#include <linux/poll.h>
L
Linus Torvalds 已提交
46
#include <linux/rtnetlink.h>
47 48
#include <linux/skbuff.h>
#include <linux/slab.h>
L
Linus Torvalds 已提交
49 50 51 52

#undef DEBUG_PKT
#undef DEBUG_ECN
#undef DEBUG_LINK
53 54
#undef DEBUG_PROTO
#undef DEBUG_PVC
L
Linus Torvalds 已提交
55

K
Krzysztof Halasa 已提交
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
#define FR_UI			0x03
#define FR_PAD			0x00

#define NLPID_IP		0xCC
#define NLPID_IPV6		0x8E
#define NLPID_SNAP		0x80
#define NLPID_PAD		0x00
#define NLPID_CCITT_ANSI_LMI	0x08
#define NLPID_CISCO_LMI		0x09


#define LMI_CCITT_ANSI_DLCI	   0 /* LMI DLCI */
#define LMI_CISCO_DLCI		1023

#define LMI_CALLREF		0x00 /* Call Reference */
#define LMI_ANSI_LOCKSHIFT	0x95 /* ANSI locking shift */
#define LMI_ANSI_CISCO_REPTYPE	0x01 /* report type */
#define LMI_CCITT_REPTYPE	0x51
#define LMI_ANSI_CISCO_ALIVE	0x03 /* keep alive */
#define LMI_CCITT_ALIVE		0x53
#define LMI_ANSI_CISCO_PVCSTAT	0x07 /* PVC status */
#define LMI_CCITT_PVCSTAT	0x57

#define LMI_FULLREP		0x00 /* full report  */
#define LMI_INTEGRITY		0x01 /* link integrity report */
#define LMI_SINGLE		0x02 /* single PVC report */

L
Linus Torvalds 已提交
83 84 85 86 87 88
#define LMI_STATUS_ENQUIRY      0x75
#define LMI_STATUS              0x7D /* reply */

#define LMI_REPT_LEN               1 /* report type element length */
#define LMI_INTEG_LEN              2 /* link integrity element length */

K
Krzysztof Halasa 已提交
89 90
#define LMI_CCITT_CISCO_LENGTH	  13 /* LMI frame lengths */
#define LMI_ANSI_LENGTH		  14
L
Linus Torvalds 已提交
91 92


93
struct fr_hdr {
L
Linus Torvalds 已提交
94 95 96 97
#if defined(__LITTLE_ENDIAN_BITFIELD)
	unsigned ea1:	1;
	unsigned cr:	1;
	unsigned dlcih:	6;
98

L
Linus Torvalds 已提交
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
	unsigned ea2:	1;
	unsigned de:	1;
	unsigned becn:	1;
	unsigned fecn:	1;
	unsigned dlcil:	4;
#else
	unsigned dlcih:	6;
	unsigned cr:	1;
	unsigned ea1:	1;

	unsigned dlcil:	4;
	unsigned fecn:	1;
	unsigned becn:	1;
	unsigned de:	1;
	unsigned ea2:	1;
#endif
115
} __packed;
L
Linus Torvalds 已提交
116 117


118
struct pvc_device {
119 120 121
	struct net_device *frad;
	struct net_device *main;
	struct net_device *ether;	/* bridged Ethernet interface	*/
122
	struct pvc_device *next;	/* Sorted in ascending DLCI order */
123 124 125 126 127 128 129 130 131 132 133 134
	int dlci;
	int open_count;

	struct {
		unsigned int new: 1;
		unsigned int active: 1;
		unsigned int exist: 1;
		unsigned int deleted: 1;
		unsigned int fecn: 1;
		unsigned int becn: 1;
		unsigned int bandwidth;	/* Cisco LMI reporting only */
	}state;
135
};
136 137 138

struct frad_state {
	fr_proto settings;
139
	struct pvc_device *first_pvc;
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
	int dce_pvc_count;

	struct timer_list timer;
	unsigned long last_poll;
	int reliable;
	int dce_changed;
	int request;
	int fullrep_sent;
	u32 last_errors; /* last errors bit list */
	u8 n391cnt;
	u8 txseq; /* TX sequence number */
	u8 rxseq; /* RX sequence number */
};


static int fr_ioctl(struct net_device *dev, struct ifreq *ifr);


L
Linus Torvalds 已提交
158 159 160 161 162 163 164 165 166 167 168 169 170
static inline u16 q922_to_dlci(u8 *hdr)
{
	return ((hdr[0] & 0xFC) << 2) | ((hdr[1] & 0xF0) >> 4);
}


static inline void dlci_to_q922(u8 *hdr, u16 dlci)
{
	hdr[0] = (dlci >> 2) & 0xFC;
	hdr[1] = ((dlci << 4) & 0xF0) | 0x01;
}


171
static inline struct frad_state* state(hdlc_device *hdlc)
172 173 174 175 176
{
	return(struct frad_state *)(hdlc->state);
}


177
static inline struct pvc_device *find_pvc(hdlc_device *hdlc, u16 dlci)
L
Linus Torvalds 已提交
178
{
179
	struct pvc_device *pvc = state(hdlc)->first_pvc;
L
Linus Torvalds 已提交
180 181 182 183 184

	while (pvc) {
		if (pvc->dlci == dlci)
			return pvc;
		if (pvc->dlci > dlci)
185
			return NULL; /* the list is sorted */
L
Linus Torvalds 已提交
186 187 188 189 190 191 192
		pvc = pvc->next;
	}

	return NULL;
}


193
static struct pvc_device *add_pvc(struct net_device *dev, u16 dlci)
L
Linus Torvalds 已提交
194 195
{
	hdlc_device *hdlc = dev_to_hdlc(dev);
196
	struct pvc_device *pvc, **pvc_p = &state(hdlc)->first_pvc;
L
Linus Torvalds 已提交
197 198 199 200 201 202 203 204 205

	while (*pvc_p) {
		if ((*pvc_p)->dlci == dlci)
			return *pvc_p;
		if ((*pvc_p)->dlci > dlci)
			break;	/* the list is sorted */
		pvc_p = &(*pvc_p)->next;
	}

206
	pvc = kzalloc(sizeof(*pvc), GFP_ATOMIC);
207 208 209
#ifdef DEBUG_PVC
	printk(KERN_DEBUG "add_pvc: allocated pvc %p, frad %p\n", pvc, dev);
#endif
L
Linus Torvalds 已提交
210 211 212 213
	if (!pvc)
		return NULL;

	pvc->dlci = dlci;
214
	pvc->frad = dev;
L
Linus Torvalds 已提交
215 216 217 218 219 220
	pvc->next = *pvc_p;	/* Put it in the chain */
	*pvc_p = pvc;
	return pvc;
}


221
static inline int pvc_is_used(struct pvc_device *pvc)
L
Linus Torvalds 已提交
222
{
223
	return pvc->main || pvc->ether;
L
Linus Torvalds 已提交
224 225 226
}


227
static inline void pvc_carrier(int on, struct pvc_device *pvc)
L
Linus Torvalds 已提交
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
{
	if (on) {
		if (pvc->main)
			if (!netif_carrier_ok(pvc->main))
				netif_carrier_on(pvc->main);
		if (pvc->ether)
			if (!netif_carrier_ok(pvc->ether))
				netif_carrier_on(pvc->ether);
	} else {
		if (pvc->main)
			if (netif_carrier_ok(pvc->main))
				netif_carrier_off(pvc->main);
		if (pvc->ether)
			if (netif_carrier_ok(pvc->ether))
				netif_carrier_off(pvc->ether);
	}
}


static inline void delete_unused_pvcs(hdlc_device *hdlc)
{
249
	struct pvc_device **pvc_p = &state(hdlc)->first_pvc;
L
Linus Torvalds 已提交
250 251 252

	while (*pvc_p) {
		if (!pvc_is_used(*pvc_p)) {
253
			struct pvc_device *pvc = *pvc_p;
254 255 256
#ifdef DEBUG_PVC
			printk(KERN_DEBUG "freeing unused pvc: %p\n", pvc);
#endif
L
Linus Torvalds 已提交
257 258 259 260 261 262 263 264 265
			*pvc_p = pvc->next;
			kfree(pvc);
			continue;
		}
		pvc_p = &(*pvc_p)->next;
	}
}


266 267
static inline struct net_device **get_dev_p(struct pvc_device *pvc,
					    int type)
L
Linus Torvalds 已提交
268 269 270 271 272 273 274 275 276 277 278 279 280 281
{
	if (type == ARPHRD_ETHER)
		return &pvc->ether;
	else
		return &pvc->main;
}


static int fr_hard_header(struct sk_buff **skb_p, u16 dlci)
{
	u16 head_len;
	struct sk_buff *skb = *skb_p;

	switch (skb->protocol) {
282
	case cpu_to_be16(NLPID_CCITT_ANSI_LMI):
L
Linus Torvalds 已提交
283 284
		head_len = 4;
		skb_push(skb, head_len);
K
Krzysztof Halasa 已提交
285
		skb->data[3] = NLPID_CCITT_ANSI_LMI;
L
Linus Torvalds 已提交
286 287
		break;

288
	case cpu_to_be16(NLPID_CISCO_LMI):
L
Linus Torvalds 已提交
289 290
		head_len = 4;
		skb_push(skb, head_len);
K
Krzysztof Halasa 已提交
291
		skb->data[3] = NLPID_CISCO_LMI;
L
Linus Torvalds 已提交
292 293
		break;

294
	case cpu_to_be16(ETH_P_IP):
K
Krzysztof Halasa 已提交
295 296 297 298 299
		head_len = 4;
		skb_push(skb, head_len);
		skb->data[3] = NLPID_IP;
		break;

300
	case cpu_to_be16(ETH_P_IPV6):
L
Linus Torvalds 已提交
301 302
		head_len = 4;
		skb_push(skb, head_len);
K
Krzysztof Halasa 已提交
303
		skb->data[3] = NLPID_IPV6;
L
Linus Torvalds 已提交
304 305
		break;

306
	case cpu_to_be16(ETH_P_802_3):
L
Linus Torvalds 已提交
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
		head_len = 10;
		if (skb_headroom(skb) < head_len) {
			struct sk_buff *skb2 = skb_realloc_headroom(skb,
								    head_len);
			if (!skb2)
				return -ENOBUFS;
			dev_kfree_skb(skb);
			skb = *skb_p = skb2;
		}
		skb_push(skb, head_len);
		skb->data[3] = FR_PAD;
		skb->data[4] = NLPID_SNAP;
		skb->data[5] = FR_PAD;
		skb->data[6] = 0x80;
		skb->data[7] = 0xC2;
		skb->data[8] = 0x00;
		skb->data[9] = 0x07; /* bridged Ethernet frame w/out FCS */
		break;

	default:
		head_len = 10;
		skb_push(skb, head_len);
		skb->data[3] = FR_PAD;
		skb->data[4] = NLPID_SNAP;
		skb->data[5] = FR_PAD;
		skb->data[6] = FR_PAD;
		skb->data[7] = FR_PAD;
334
		*(__be16*)(skb->data + 8) = skb->protocol;
L
Linus Torvalds 已提交
335 336 337 338 339 340 341 342 343 344 345
	}

	dlci_to_q922(skb->data, dlci);
	skb->data[2] = FR_UI;
	return 0;
}



static int pvc_open(struct net_device *dev)
{
346
	struct pvc_device *pvc = dev->ml_priv;
L
Linus Torvalds 已提交
347

348 349
	if ((pvc->frad->flags & IFF_UP) == 0)
		return -EIO;  /* Frad must be UP in order to activate PVC */
L
Linus Torvalds 已提交
350 351

	if (pvc->open_count++ == 0) {
352 353 354
		hdlc_device *hdlc = dev_to_hdlc(pvc->frad);
		if (state(hdlc)->settings.lmi == LMI_NONE)
			pvc->state.active = netif_carrier_ok(pvc->frad);
L
Linus Torvalds 已提交
355 356

		pvc_carrier(pvc->state.active, pvc);
357
		state(hdlc)->dce_changed = 1;
L
Linus Torvalds 已提交
358 359 360 361 362 363 364 365
	}
	return 0;
}



static int pvc_close(struct net_device *dev)
{
366
	struct pvc_device *pvc = dev->ml_priv;
L
Linus Torvalds 已提交
367 368

	if (--pvc->open_count == 0) {
369 370
		hdlc_device *hdlc = dev_to_hdlc(pvc->frad);
		if (state(hdlc)->settings.lmi == LMI_NONE)
L
Linus Torvalds 已提交
371 372
			pvc->state.active = 0;

373 374
		if (state(hdlc)->settings.dce) {
			state(hdlc)->dce_changed = 1;
L
Linus Torvalds 已提交
375 376 377 378 379 380 381 382
			pvc->state.active = 0;
		}
	}
	return 0;
}



383
static int pvc_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
L
Linus Torvalds 已提交
384
{
385
	struct pvc_device *pvc = dev->ml_priv;
L
Linus Torvalds 已提交
386 387 388 389 390 391 392 393 394 395 396 397 398 399 400
	fr_proto_pvc_info info;

	if (ifr->ifr_settings.type == IF_GET_PROTO) {
		if (dev->type == ARPHRD_ETHER)
			ifr->ifr_settings.type = IF_PROTO_FR_ETH_PVC;
		else
			ifr->ifr_settings.type = IF_PROTO_FR_PVC;

		if (ifr->ifr_settings.size < sizeof(info)) {
			/* data size wanted */
			ifr->ifr_settings.size = sizeof(info);
			return -ENOBUFS;
		}

		info.dlci = pvc->dlci;
401
		memcpy(info.master, pvc->frad->name, IFNAMSIZ);
L
Linus Torvalds 已提交
402 403 404 405 406 407 408 409 410
		if (copy_to_user(ifr->ifr_settings.ifs_ifsu.fr_pvc_info,
				 &info, sizeof(info)))
			return -EFAULT;
		return 0;
	}

	return -EINVAL;
}

411
static netdev_tx_t pvc_xmit(struct sk_buff *skb, struct net_device *dev)
L
Linus Torvalds 已提交
412
{
413
	struct pvc_device *pvc = dev->ml_priv;
L
Linus Torvalds 已提交
414 415 416 417 418 419 420 421 422

	if (pvc->state.active) {
		if (dev->type == ARPHRD_ETHER) {
			int pad = ETH_ZLEN - skb->len;
			if (pad > 0) { /* Pad the frame with zeros */
				int len = skb->len;
				if (skb_tailroom(skb) < pad)
					if (pskb_expand_head(skb, 0, pad,
							     GFP_ATOMIC)) {
423
						dev->stats.tx_dropped++;
L
Linus Torvalds 已提交
424
						dev_kfree_skb(skb);
425
						return NETDEV_TX_OK;
L
Linus Torvalds 已提交
426 427 428 429
					}
				skb_put(skb, pad);
				memset(skb->data + len, 0, pad);
			}
430
			skb->protocol = cpu_to_be16(ETH_P_802_3);
L
Linus Torvalds 已提交
431 432
		}
		if (!fr_hard_header(&skb, pvc->dlci)) {
433 434
			dev->stats.tx_bytes += skb->len;
			dev->stats.tx_packets++;
L
Linus Torvalds 已提交
435
			if (pvc->state.fecn) /* TX Congestion counter */
436
				dev->stats.tx_compressed++;
437
			skb->dev = pvc->frad;
L
Linus Torvalds 已提交
438
			dev_queue_xmit(skb);
439
			return NETDEV_TX_OK;
L
Linus Torvalds 已提交
440 441 442
		}
	}

443
	dev->stats.tx_dropped++;
L
Linus Torvalds 已提交
444
	dev_kfree_skb(skb);
445
	return NETDEV_TX_OK;
L
Linus Torvalds 已提交
446 447
}

448
static inline void fr_log_dlci_active(struct pvc_device *pvc)
L
Linus Torvalds 已提交
449
{
450 451 452 453 454 455 456 457
	netdev_info(pvc->frad, "DLCI %d [%s%s%s]%s %s\n",
		    pvc->dlci,
		    pvc->main ? pvc->main->name : "",
		    pvc->main && pvc->ether ? " " : "",
		    pvc->ether ? pvc->ether->name : "",
		    pvc->state.new ? " new" : "",
		    !pvc->state.exist ? "deleted" :
		    pvc->state.active ? "active" : "inactive");
L
Linus Torvalds 已提交
458 459 460 461 462 463 464 465 466 467 468 469 470 471 472
}



static inline u8 fr_lmi_nextseq(u8 x)
{
	x++;
	return x ? x : 1;
}


static void fr_lmi_send(struct net_device *dev, int fullrep)
{
	hdlc_device *hdlc = dev_to_hdlc(dev);
	struct sk_buff *skb;
473
	struct pvc_device *pvc = state(hdlc)->first_pvc;
474 475
	int lmi = state(hdlc)->settings.lmi;
	int dce = state(hdlc)->settings.dce;
K
Krzysztof Halasa 已提交
476 477
	int len = lmi == LMI_ANSI ? LMI_ANSI_LENGTH : LMI_CCITT_CISCO_LENGTH;
	int stat_len = (lmi == LMI_CISCO) ? 6 : 3;
L
Linus Torvalds 已提交
478 479 480
	u8 *data;
	int i = 0;

K
Krzysztof Halasa 已提交
481
	if (dce && fullrep) {
482
		len += state(hdlc)->dce_pvc_count * (2 + stat_len);
L
Linus Torvalds 已提交
483
		if (len > HDLC_MAX_MRU) {
484
			netdev_warn(dev, "Too many PVCs while sending LMI full report\n");
L
Linus Torvalds 已提交
485 486 487 488 489 490
			return;
		}
	}

	skb = dev_alloc_skb(len);
	if (!skb) {
491
		netdev_warn(dev, "Memory squeeze on fr_lmi_send()\n");
L
Linus Torvalds 已提交
492 493 494 495
		return;
	}
	memset(skb->data, 0, len);
	skb_reserve(skb, 4);
K
Krzysztof Halasa 已提交
496
	if (lmi == LMI_CISCO) {
497
		skb->protocol = cpu_to_be16(NLPID_CISCO_LMI);
K
Krzysztof Halasa 已提交
498 499
		fr_hard_header(&skb, LMI_CISCO_DLCI);
	} else {
500
		skb->protocol = cpu_to_be16(NLPID_CCITT_ANSI_LMI);
K
Krzysztof Halasa 已提交
501 502
		fr_hard_header(&skb, LMI_CCITT_ANSI_DLCI);
	}
503
	data = skb_tail_pointer(skb);
L
Linus Torvalds 已提交
504
	data[i++] = LMI_CALLREF;
K
Krzysztof Halasa 已提交
505 506
	data[i++] = dce ? LMI_STATUS : LMI_STATUS_ENQUIRY;
	if (lmi == LMI_ANSI)
L
Linus Torvalds 已提交
507
		data[i++] = LMI_ANSI_LOCKSHIFT;
K
Krzysztof Halasa 已提交
508 509
	data[i++] = lmi == LMI_CCITT ? LMI_CCITT_REPTYPE :
		LMI_ANSI_CISCO_REPTYPE;
L
Linus Torvalds 已提交
510 511
	data[i++] = LMI_REPT_LEN;
	data[i++] = fullrep ? LMI_FULLREP : LMI_INTEGRITY;
K
Krzysztof Halasa 已提交
512
	data[i++] = lmi == LMI_CCITT ? LMI_CCITT_ALIVE : LMI_ANSI_CISCO_ALIVE;
L
Linus Torvalds 已提交
513
	data[i++] = LMI_INTEG_LEN;
514 515 516
	data[i++] = state(hdlc)->txseq =
		fr_lmi_nextseq(state(hdlc)->txseq);
	data[i++] = state(hdlc)->rxseq;
L
Linus Torvalds 已提交
517

K
Krzysztof Halasa 已提交
518
	if (dce && fullrep) {
L
Linus Torvalds 已提交
519
		while (pvc) {
K
Krzysztof Halasa 已提交
520 521
			data[i++] = lmi == LMI_CCITT ? LMI_CCITT_PVCSTAT :
				LMI_ANSI_CISCO_PVCSTAT;
L
Linus Torvalds 已提交
522 523 524
			data[i++] = stat_len;

			/* LMI start/restart */
525
			if (state(hdlc)->reliable && !pvc->state.exist) {
L
Linus Torvalds 已提交
526 527 528 529 530 531 532 533 534 535 536 537
				pvc->state.exist = pvc->state.new = 1;
				fr_log_dlci_active(pvc);
			}

			/* ifconfig PVC up */
			if (pvc->open_count && !pvc->state.active &&
			    pvc->state.exist && !pvc->state.new) {
				pvc_carrier(1, pvc);
				pvc->state.active = 1;
				fr_log_dlci_active(pvc);
			}

K
Krzysztof Halasa 已提交
538 539 540 541 542 543 544 545 546 547 548 549 550 551
			if (lmi == LMI_CISCO) {
				data[i] = pvc->dlci >> 8;
				data[i + 1] = pvc->dlci & 0xFF;
			} else {
				data[i] = (pvc->dlci >> 4) & 0x3F;
				data[i + 1] = ((pvc->dlci << 3) & 0x78) | 0x80;
				data[i + 2] = 0x80;
			}

			if (pvc->state.new)
				data[i + 2] |= 0x08;
			else if (pvc->state.active)
				data[i + 2] |= 0x02;

L
Linus Torvalds 已提交
552 553 554 555 556 557 558 559
			i += stat_len;
			pvc = pvc->next;
		}
	}

	skb_put(skb, i);
	skb->priority = TC_PRIO_CONTROL;
	skb->dev = dev;
560
	skb_reset_network_header(skb);
L
Linus Torvalds 已提交
561 562 563 564 565 566 567 568 569

	dev_queue_xmit(skb);
}



static void fr_set_link_state(int reliable, struct net_device *dev)
{
	hdlc_device *hdlc = dev_to_hdlc(dev);
570
	struct pvc_device *pvc = state(hdlc)->first_pvc;
L
Linus Torvalds 已提交
571

572
	state(hdlc)->reliable = reliable;
L
Linus Torvalds 已提交
573
	if (reliable) {
574
		netif_dormant_off(dev);
575 576
		state(hdlc)->n391cnt = 0; /* Request full status */
		state(hdlc)->dce_changed = 1;
L
Linus Torvalds 已提交
577

578
		if (state(hdlc)->settings.lmi == LMI_NONE) {
L
Linus Torvalds 已提交
579 580 581 582 583 584 585 586
			while (pvc) {	/* Activate all PVCs */
				pvc_carrier(1, pvc);
				pvc->state.exist = pvc->state.active = 1;
				pvc->state.new = 0;
				pvc = pvc->next;
			}
		}
	} else {
587
		netif_dormant_on(dev);
L
Linus Torvalds 已提交
588 589 590 591
		while (pvc) {		/* Deactivate all PVCs */
			pvc_carrier(0, pvc);
			pvc->state.exist = pvc->state.active = 0;
			pvc->state.new = 0;
592
			if (!state(hdlc)->settings.dce)
K
Krzysztof Halasa 已提交
593
				pvc->state.bandwidth = 0;
L
Linus Torvalds 已提交
594 595 596 597 598 599 600 601 602 603 604 605 606
			pvc = pvc->next;
		}
	}
}


static void fr_timer(unsigned long arg)
{
	struct net_device *dev = (struct net_device *)arg;
	hdlc_device *hdlc = dev_to_hdlc(dev);
	int i, cnt = 0, reliable;
	u32 list;

607 608 609 610 611
	if (state(hdlc)->settings.dce) {
		reliable = state(hdlc)->request &&
			time_before(jiffies, state(hdlc)->last_poll +
				    state(hdlc)->settings.t392 * HZ);
		state(hdlc)->request = 0;
K
Krzysztof Halasa 已提交
612
	} else {
613 614 615
		state(hdlc)->last_errors <<= 1; /* Shift the list */
		if (state(hdlc)->request) {
			if (state(hdlc)->reliable)
616
				netdev_info(dev, "No LMI status reply received\n");
617
			state(hdlc)->last_errors |= 1;
L
Linus Torvalds 已提交
618 619
		}

620 621
		list = state(hdlc)->last_errors;
		for (i = 0; i < state(hdlc)->settings.n393; i++, list >>= 1)
L
Linus Torvalds 已提交
622 623
			cnt += (list & 1);	/* errors count */

624
		reliable = (cnt < state(hdlc)->settings.n392);
L
Linus Torvalds 已提交
625 626
	}

627
	if (state(hdlc)->reliable != reliable) {
628
		netdev_info(dev, "Link %sreliable\n", reliable ? "" : "un");
L
Linus Torvalds 已提交
629 630 631
		fr_set_link_state(reliable, dev);
	}

632 633 634
	if (state(hdlc)->settings.dce)
		state(hdlc)->timer.expires = jiffies +
			state(hdlc)->settings.t392 * HZ;
L
Linus Torvalds 已提交
635
	else {
636 637
		if (state(hdlc)->n391cnt)
			state(hdlc)->n391cnt--;
L
Linus Torvalds 已提交
638

639
		fr_lmi_send(dev, state(hdlc)->n391cnt == 0);
L
Linus Torvalds 已提交
640

641 642 643 644
		state(hdlc)->last_poll = jiffies;
		state(hdlc)->request = 1;
		state(hdlc)->timer.expires = jiffies +
			state(hdlc)->settings.t391 * HZ;
L
Linus Torvalds 已提交
645 646
	}

647 648 649
	state(hdlc)->timer.function = fr_timer;
	state(hdlc)->timer.data = arg;
	add_timer(&state(hdlc)->timer);
L
Linus Torvalds 已提交
650 651 652 653 654 655
}


static int fr_lmi_recv(struct net_device *dev, struct sk_buff *skb)
{
	hdlc_device *hdlc = dev_to_hdlc(dev);
656
	struct pvc_device *pvc;
L
Linus Torvalds 已提交
657
	u8 rxseq, txseq;
658 659
	int lmi = state(hdlc)->settings.lmi;
	int dce = state(hdlc)->settings.dce;
K
Krzysztof Halasa 已提交
660
	int stat_len = (lmi == LMI_CISCO) ? 6 : 3, reptype, error, no_ram, i;
L
Linus Torvalds 已提交
661

K
Krzysztof Halasa 已提交
662 663
	if (skb->len < (lmi == LMI_ANSI ? LMI_ANSI_LENGTH :
			LMI_CCITT_CISCO_LENGTH)) {
664
		netdev_info(dev, "Short LMI frame\n");
L
Linus Torvalds 已提交
665 666 667
		return 1;
	}

K
Krzysztof Halasa 已提交
668 669
	if (skb->data[3] != (lmi == LMI_CISCO ? NLPID_CISCO_LMI :
			     NLPID_CCITT_ANSI_LMI)) {
670
		netdev_info(dev, "Received non-LMI frame with LMI DLCI\n");
K
Krzysztof Halasa 已提交
671 672 673 674
		return 1;
	}

	if (skb->data[4] != LMI_CALLREF) {
675 676
		netdev_info(dev, "Invalid LMI Call reference (0x%02X)\n",
			    skb->data[4]);
K
Krzysztof Halasa 已提交
677 678 679 680
		return 1;
	}

	if (skb->data[5] != (dce ? LMI_STATUS_ENQUIRY : LMI_STATUS)) {
681 682
		netdev_info(dev, "Invalid LMI Message type (0x%02X)\n",
			    skb->data[5]);
L
Linus Torvalds 已提交
683 684 685
		return 1;
	}

K
Krzysztof Halasa 已提交
686 687
	if (lmi == LMI_ANSI) {
		if (skb->data[6] != LMI_ANSI_LOCKSHIFT) {
688 689
			netdev_info(dev, "Not ANSI locking shift in LMI message (0x%02X)\n",
				    skb->data[6]);
K
Krzysztof Halasa 已提交
690 691 692 693 694
			return 1;
		}
		i = 7;
	} else
		i = 6;
L
Linus Torvalds 已提交
695

K
Krzysztof Halasa 已提交
696 697
	if (skb->data[i] != (lmi == LMI_CCITT ? LMI_CCITT_REPTYPE :
			     LMI_ANSI_CISCO_REPTYPE)) {
698 699
		netdev_info(dev, "Not an LMI Report type IE (0x%02X)\n",
			    skb->data[i]);
L
Linus Torvalds 已提交
700 701 702
		return 1;
	}

K
Krzysztof Halasa 已提交
703
	if (skb->data[++i] != LMI_REPT_LEN) {
704 705
		netdev_info(dev, "Invalid LMI Report type IE length (%u)\n",
			    skb->data[i]);
K
Krzysztof Halasa 已提交
706 707
		return 1;
	}
L
Linus Torvalds 已提交
708

K
Krzysztof Halasa 已提交
709 710
	reptype = skb->data[++i];
	if (reptype != LMI_INTEGRITY && reptype != LMI_FULLREP) {
711 712
		netdev_info(dev, "Unsupported LMI Report type (0x%02X)\n",
			    reptype);
K
Krzysztof Halasa 已提交
713 714
		return 1;
	}
L
Linus Torvalds 已提交
715

K
Krzysztof Halasa 已提交
716 717
	if (skb->data[++i] != (lmi == LMI_CCITT ? LMI_CCITT_ALIVE :
			       LMI_ANSI_CISCO_ALIVE)) {
718 719
		netdev_info(dev, "Not an LMI Link integrity verification IE (0x%02X)\n",
			    skb->data[i]);
L
Linus Torvalds 已提交
720 721 722
		return 1;
	}

K
Krzysztof Halasa 已提交
723
	if (skb->data[++i] != LMI_INTEG_LEN) {
724 725
		netdev_info(dev, "Invalid LMI Link integrity verification IE length (%u)\n",
			    skb->data[i]);
K
Krzysztof Halasa 已提交
726 727 728
		return 1;
	}
	i++;
L
Linus Torvalds 已提交
729

730
	state(hdlc)->rxseq = skb->data[i++]; /* TX sequence from peer */
L
Linus Torvalds 已提交
731 732
	rxseq = skb->data[i++];	/* Should confirm our sequence */

733
	txseq = state(hdlc)->txseq;
L
Linus Torvalds 已提交
734

K
Krzysztof Halasa 已提交
735
	if (dce)
736
		state(hdlc)->last_poll = jiffies;
L
Linus Torvalds 已提交
737 738

	error = 0;
739
	if (!state(hdlc)->reliable)
L
Linus Torvalds 已提交
740 741
		error = 1;

742 743
	if (rxseq == 0 || rxseq != txseq) { /* Ask for full report next time */
		state(hdlc)->n391cnt = 0;
L
Linus Torvalds 已提交
744 745 746
		error = 1;
	}

K
Krzysztof Halasa 已提交
747
	if (dce) {
748
		if (state(hdlc)->fullrep_sent && !error) {
L
Linus Torvalds 已提交
749
/* Stop sending full report - the last one has been confirmed by DTE */
750 751
			state(hdlc)->fullrep_sent = 0;
			pvc = state(hdlc)->first_pvc;
L
Linus Torvalds 已提交
752 753 754 755 756
			while (pvc) {
				if (pvc->state.new) {
					pvc->state.new = 0;

/* Tell DTE that new PVC is now active */
757
					state(hdlc)->dce_changed = 1;
L
Linus Torvalds 已提交
758 759 760 761 762
				}
				pvc = pvc->next;
			}
		}

763
		if (state(hdlc)->dce_changed) {
L
Linus Torvalds 已提交
764
			reptype = LMI_FULLREP;
765 766
			state(hdlc)->fullrep_sent = 1;
			state(hdlc)->dce_changed = 0;
L
Linus Torvalds 已提交
767 768
		}

769
		state(hdlc)->request = 1; /* got request */
L
Linus Torvalds 已提交
770 771 772 773 774 775
		fr_lmi_send(dev, reptype == LMI_FULLREP ? 1 : 0);
		return 0;
	}

	/* DTE */

776
	state(hdlc)->request = 0; /* got response, no request pending */
L
Linus Torvalds 已提交
777 778 779 780 781 782 783

	if (error)
		return 0;

	if (reptype != LMI_FULLREP)
		return 0;

784
	pvc = state(hdlc)->first_pvc;
L
Linus Torvalds 已提交
785 786 787 788 789 790 791 792 793

	while (pvc) {
		pvc->state.deleted = 1;
		pvc = pvc->next;
	}

	no_ram = 0;
	while (skb->len >= i + 2 + stat_len) {
		u16 dlci;
K
Krzysztof Halasa 已提交
794
		u32 bw;
L
Linus Torvalds 已提交
795 796
		unsigned int active, new;

K
Krzysztof Halasa 已提交
797 798
		if (skb->data[i] != (lmi == LMI_CCITT ? LMI_CCITT_PVCSTAT :
				       LMI_ANSI_CISCO_PVCSTAT)) {
799 800
			netdev_info(dev, "Not an LMI PVC status IE (0x%02X)\n",
				    skb->data[i]);
L
Linus Torvalds 已提交
801 802 803
			return 1;
		}

K
Krzysztof Halasa 已提交
804
		if (skb->data[++i] != stat_len) {
805 806
			netdev_info(dev, "Invalid LMI PVC status IE length (%u)\n",
				    skb->data[i]);
L
Linus Torvalds 已提交
807 808 809 810
			return 1;
		}
		i++;

K
Krzysztof Halasa 已提交
811 812 813 814 815 816 817 818 819 820 821 822
		new = !! (skb->data[i + 2] & 0x08);
		active = !! (skb->data[i + 2] & 0x02);
		if (lmi == LMI_CISCO) {
			dlci = (skb->data[i] << 8) | skb->data[i + 1];
			bw = (skb->data[i + 3] << 16) |
				(skb->data[i + 4] << 8) |
				(skb->data[i + 5]);
		} else {
			dlci = ((skb->data[i] & 0x3F) << 4) |
				((skb->data[i + 1] & 0x78) >> 3);
			bw = 0;
		}
L
Linus Torvalds 已提交
823 824 825 826

		pvc = add_pvc(dev, dlci);

		if (!pvc && !no_ram) {
827
			netdev_warn(dev, "Memory squeeze on fr_lmi_recv()\n");
L
Linus Torvalds 已提交
828 829 830 831 832 833 834 835
			no_ram = 1;
		}

		if (pvc) {
			pvc->state.exist = 1;
			pvc->state.deleted = 0;
			if (active != pvc->state.active ||
			    new != pvc->state.new ||
K
Krzysztof Halasa 已提交
836
			    bw != pvc->state.bandwidth ||
L
Linus Torvalds 已提交
837 838 839
			    !pvc->state.exist) {
				pvc->state.new = new;
				pvc->state.active = active;
K
Krzysztof Halasa 已提交
840
				pvc->state.bandwidth = bw;
L
Linus Torvalds 已提交
841 842 843 844 845 846 847 848
				pvc_carrier(active, pvc);
				fr_log_dlci_active(pvc);
			}
		}

		i += stat_len;
	}

849
	pvc = state(hdlc)->first_pvc;
L
Linus Torvalds 已提交
850 851 852 853 854 855

	while (pvc) {
		if (pvc->state.deleted && pvc->state.exist) {
			pvc_carrier(0, pvc);
			pvc->state.active = pvc->state.new = 0;
			pvc->state.exist = 0;
K
Krzysztof Halasa 已提交
856
			pvc->state.bandwidth = 0;
L
Linus Torvalds 已提交
857 858 859 860 861 862
			fr_log_dlci_active(pvc);
		}
		pvc = pvc->next;
	}

	/* Next full report after N391 polls */
863
	state(hdlc)->n391cnt = state(hdlc)->settings.n391;
L
Linus Torvalds 已提交
864 865 866 867 868 869 870

	return 0;
}


static int fr_rx(struct sk_buff *skb)
{
871 872
	struct net_device *frad = skb->dev;
	hdlc_device *hdlc = dev_to_hdlc(frad);
873
	struct fr_hdr *fh = (struct fr_hdr *)skb->data;
L
Linus Torvalds 已提交
874 875
	u8 *data = skb->data;
	u16 dlci;
876
	struct pvc_device *pvc;
L
Linus Torvalds 已提交
877 878 879 880 881 882 883
	struct net_device *dev = NULL;

	if (skb->len <= 4 || fh->ea1 || data[2] != FR_UI)
		goto rx_error;

	dlci = q922_to_dlci(skb->data);

K
Krzysztof Halasa 已提交
884
	if ((dlci == LMI_CCITT_ANSI_DLCI &&
885 886
	     (state(hdlc)->settings.lmi == LMI_ANSI ||
	      state(hdlc)->settings.lmi == LMI_CCITT)) ||
K
Krzysztof Halasa 已提交
887
	    (dlci == LMI_CISCO_DLCI &&
888 889
	     state(hdlc)->settings.lmi == LMI_CISCO)) {
		if (fr_lmi_recv(frad, skb))
K
Krzysztof Halasa 已提交
890 891 892
			goto rx_error;
		dev_kfree_skb_any(skb);
		return NET_RX_SUCCESS;
L
Linus Torvalds 已提交
893 894 895 896 897
	}

	pvc = find_pvc(hdlc, dlci);
	if (!pvc) {
#ifdef DEBUG_PKT
898 899
		netdev_info(frad, "No PVC for received frame's DLCI %d\n",
			    dlci);
L
Linus Torvalds 已提交
900 901 902 903 904 905 906
#endif
		dev_kfree_skb_any(skb);
		return NET_RX_DROP;
	}

	if (pvc->state.fecn != fh->fecn) {
#ifdef DEBUG_ECN
907
		printk(KERN_DEBUG "%s: DLCI %d FECN O%s\n", frad->name,
L
Linus Torvalds 已提交
908 909 910 911 912 913 914
		       dlci, fh->fecn ? "N" : "FF");
#endif
		pvc->state.fecn ^= 1;
	}

	if (pvc->state.becn != fh->becn) {
#ifdef DEBUG_ECN
915
		printk(KERN_DEBUG "%s: DLCI %d BECN O%s\n", frad->name,
L
Linus Torvalds 已提交
916 917 918 919 920 921 922
		       dlci, fh->becn ? "N" : "FF");
#endif
		pvc->state.becn ^= 1;
	}


	if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL) {
923
		frad->stats.rx_dropped++;
L
Linus Torvalds 已提交
924 925 926 927 928 929 930 931 932 933 934 935 936 937 938
		return NET_RX_DROP;
	}

	if (data[3] == NLPID_IP) {
		skb_pull(skb, 4); /* Remove 4-byte header (hdr, UI, NLPID) */
		dev = pvc->main;
		skb->protocol = htons(ETH_P_IP);

	} else if (data[3] == NLPID_IPV6) {
		skb_pull(skb, 4); /* Remove 4-byte header (hdr, UI, NLPID) */
		dev = pvc->main;
		skb->protocol = htons(ETH_P_IPV6);

	} else if (skb->len > 10 && data[3] == FR_PAD &&
		   data[4] == NLPID_SNAP && data[5] == FR_PAD) {
939 940
		u16 oui = ntohs(*(__be16*)(data + 6));
		u16 pid = ntohs(*(__be16*)(data + 8));
L
Linus Torvalds 已提交
941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957
		skb_pull(skb, 10);

		switch ((((u32)oui) << 16) | pid) {
		case ETH_P_ARP: /* routed frame with SNAP */
		case ETH_P_IPX:
		case ETH_P_IP:	/* a long variant */
		case ETH_P_IPV6:
			dev = pvc->main;
			skb->protocol = htons(pid);
			break;

		case 0x80C20007: /* bridged Ethernet frame */
			if ((dev = pvc->ether) != NULL)
				skb->protocol = eth_type_trans(skb, dev);
			break;

		default:
958 959
			netdev_info(frad, "Unsupported protocol, OUI=%x PID=%x\n",
				    oui, pid);
L
Linus Torvalds 已提交
960 961 962 963
			dev_kfree_skb_any(skb);
			return NET_RX_DROP;
		}
	} else {
964 965
		netdev_info(frad, "Unsupported protocol, NLPID=%x length=%i\n",
			    data[3], skb->len);
L
Linus Torvalds 已提交
966 967 968 969 970
		dev_kfree_skb_any(skb);
		return NET_RX_DROP;
	}

	if (dev) {
971 972
		dev->stats.rx_packets++; /* PVC traffic */
		dev->stats.rx_bytes += skb->len;
L
Linus Torvalds 已提交
973
		if (pvc->state.becn)
974
			dev->stats.rx_compressed++;
975
		skb->dev = dev;
L
Linus Torvalds 已提交
976 977 978 979 980 981 982 983
		netif_rx(skb);
		return NET_RX_SUCCESS;
	} else {
		dev_kfree_skb_any(skb);
		return NET_RX_DROP;
	}

 rx_error:
984
	frad->stats.rx_errors++; /* Mark error */
L
Linus Torvalds 已提交
985 986 987 988 989 990 991 992 993 994 995 996
	dev_kfree_skb_any(skb);
	return NET_RX_DROP;
}



static void fr_start(struct net_device *dev)
{
	hdlc_device *hdlc = dev_to_hdlc(dev);
#ifdef DEBUG_LINK
	printk(KERN_DEBUG "fr_start\n");
#endif
997 998 999 1000 1001 1002 1003 1004 1005 1006
	if (state(hdlc)->settings.lmi != LMI_NONE) {
		state(hdlc)->reliable = 0;
		state(hdlc)->dce_changed = 1;
		state(hdlc)->request = 0;
		state(hdlc)->fullrep_sent = 0;
		state(hdlc)->last_errors = 0xFFFFFFFF;
		state(hdlc)->n391cnt = 0;
		state(hdlc)->txseq = state(hdlc)->rxseq = 0;

		init_timer(&state(hdlc)->timer);
L
Linus Torvalds 已提交
1007
		/* First poll after 1 s */
1008 1009 1010 1011
		state(hdlc)->timer.expires = jiffies + HZ;
		state(hdlc)->timer.function = fr_timer;
		state(hdlc)->timer.data = (unsigned long)dev;
		add_timer(&state(hdlc)->timer);
L
Linus Torvalds 已提交
1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022
	} else
		fr_set_link_state(1, dev);
}


static void fr_stop(struct net_device *dev)
{
	hdlc_device *hdlc = dev_to_hdlc(dev);
#ifdef DEBUG_LINK
	printk(KERN_DEBUG "fr_stop\n");
#endif
1023 1024
	if (state(hdlc)->settings.lmi != LMI_NONE)
		del_timer_sync(&state(hdlc)->timer);
L
Linus Torvalds 已提交
1025 1026 1027 1028 1029 1030 1031
	fr_set_link_state(0, dev);
}


static void fr_close(struct net_device *dev)
{
	hdlc_device *hdlc = dev_to_hdlc(dev);
1032
	struct pvc_device *pvc = state(hdlc)->first_pvc;
L
Linus Torvalds 已提交
1033 1034 1035 1036 1037 1038 1039 1040 1041 1042

	while (pvc) {		/* Shutdown all PVCs for this FRAD */
		if (pvc->main)
			dev_close(pvc->main);
		if (pvc->ether)
			dev_close(pvc->ether);
		pvc = pvc->next;
	}
}

1043 1044

static void pvc_setup(struct net_device *dev)
L
Linus Torvalds 已提交
1045 1046 1047 1048 1049
{
	dev->type = ARPHRD_DLCI;
	dev->flags = IFF_POINTOPOINT;
	dev->hard_header_len = 10;
	dev->addr_len = 2;
1050
	netif_keep_dst(dev);
L
Linus Torvalds 已提交
1051 1052
}

1053 1054 1055 1056 1057 1058 1059
static const struct net_device_ops pvc_ops = {
	.ndo_open       = pvc_open,
	.ndo_stop       = pvc_close,
	.ndo_start_xmit = pvc_xmit,
	.ndo_do_ioctl   = pvc_ioctl,
};

1060
static int fr_add_pvc(struct net_device *frad, unsigned int dlci, int type)
L
Linus Torvalds 已提交
1061
{
1062
	hdlc_device *hdlc = dev_to_hdlc(frad);
1063
	struct pvc_device *pvc;
L
Linus Torvalds 已提交
1064
	struct net_device *dev;
1065
	int used;
L
Linus Torvalds 已提交
1066

1067
	if ((pvc = add_pvc(frad, dlci)) == NULL) {
1068
		netdev_warn(frad, "Memory squeeze on fr_add_pvc()\n");
L
Linus Torvalds 已提交
1069 1070 1071 1072 1073 1074 1075 1076
		return -ENOBUFS;
	}

	if (*get_dev_p(pvc, type))
		return -EEXIST;

	used = pvc_is_used(pvc);

1077
	if (type == ARPHRD_ETHER)
1078 1079
		dev = alloc_netdev(0, "pvceth%d", NET_NAME_UNKNOWN,
				   ether_setup);
1080
	else
1081
		dev = alloc_netdev(0, "pvc%d", NET_NAME_UNKNOWN, pvc_setup);
L
Linus Torvalds 已提交
1082 1083

	if (!dev) {
1084
		netdev_warn(frad, "Memory squeeze on fr_pvc()\n");
L
Linus Torvalds 已提交
1085 1086 1087 1088
		delete_unused_pvcs(hdlc);
		return -ENOBUFS;
	}

1089 1090
	if (type == ARPHRD_ETHER) {
		dev->priv_flags &= ~IFF_TX_SKB_SHARING;
1091
		eth_hw_addr_random(dev);
1092
	} else {
1093
		*(__be16*)dev->dev_addr = htons(dlci);
L
Linus Torvalds 已提交
1094 1095
		dlci_to_q922(dev->broadcast, dlci);
	}
1096
	dev->netdev_ops = &pvc_ops;
L
Linus Torvalds 已提交
1097
	dev->mtu = HDLC_MAX_MTU;
1098 1099
	dev->min_mtu = 68;
	dev->max_mtu = HDLC_MAX_MTU;
1100
	dev->priv_flags |= IFF_NO_QUEUE;
1101
	dev->ml_priv = pvc;
L
Linus Torvalds 已提交
1102 1103 1104 1105 1106 1107 1108 1109 1110 1111

	if (register_netdevice(dev) != 0) {
		free_netdev(dev);
		delete_unused_pvcs(hdlc);
		return -EIO;
	}

	dev->destructor = free_netdev;
	*get_dev_p(pvc, type) = dev;
	if (!used) {
1112 1113
		state(hdlc)->dce_changed = 1;
		state(hdlc)->dce_pvc_count++;
L
Linus Torvalds 已提交
1114 1115 1116 1117 1118 1119 1120 1121
	}
	return 0;
}



static int fr_del_pvc(hdlc_device *hdlc, unsigned int dlci, int type)
{
1122
	struct pvc_device *pvc;
L
Linus Torvalds 已提交
1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137
	struct net_device *dev;

	if ((pvc = find_pvc(hdlc, dlci)) == NULL)
		return -ENOENT;

	if ((dev = *get_dev_p(pvc, type)) == NULL)
		return -ENOENT;

	if (dev->flags & IFF_UP)
		return -EBUSY;		/* PVC in use */

	unregister_netdevice(dev); /* the destructor will free_netdev(dev) */
	*get_dev_p(pvc, type) = NULL;

	if (!pvc_is_used(pvc)) {
1138 1139
		state(hdlc)->dce_pvc_count--;
		state(hdlc)->dce_changed = 1;
L
Linus Torvalds 已提交
1140 1141 1142 1143 1144 1145 1146
	}
	delete_unused_pvcs(hdlc);
	return 0;
}



1147
static void fr_destroy(struct net_device *frad)
L
Linus Torvalds 已提交
1148
{
1149
	hdlc_device *hdlc = dev_to_hdlc(frad);
1150
	struct pvc_device *pvc = state(hdlc)->first_pvc;
1151 1152 1153
	state(hdlc)->first_pvc = NULL; /* All PVCs destroyed */
	state(hdlc)->dce_pvc_count = 0;
	state(hdlc)->dce_changed = 1;
L
Linus Torvalds 已提交
1154 1155

	while (pvc) {
1156
		struct pvc_device *next = pvc->next;
L
Linus Torvalds 已提交
1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169
		/* destructors will free_netdev() main and ether */
		if (pvc->main)
			unregister_netdevice(pvc->main);

		if (pvc->ether)
			unregister_netdevice(pvc->ether);

		kfree(pvc);
		pvc = next;
	}
}


1170 1171 1172 1173 1174 1175
static struct hdlc_proto proto = {
	.close		= fr_close,
	.start		= fr_start,
	.stop		= fr_stop,
	.detach		= fr_destroy,
	.ioctl		= fr_ioctl,
1176
	.netif_rx	= fr_rx,
1177 1178 1179
	.module		= THIS_MODULE,
};

L
Linus Torvalds 已提交
1180

1181
static int fr_ioctl(struct net_device *dev, struct ifreq *ifr)
L
Linus Torvalds 已提交
1182 1183 1184 1185 1186 1187 1188 1189 1190 1191
{
	fr_proto __user *fr_s = ifr->ifr_settings.ifs_ifsu.fr;
	const size_t size = sizeof(fr_proto);
	fr_proto new_settings;
	hdlc_device *hdlc = dev_to_hdlc(dev);
	fr_proto_pvc pvc;
	int result;

	switch (ifr->ifr_settings.type) {
	case IF_GET_PROTO:
1192 1193
		if (dev_to_hdlc(dev)->proto != &proto) /* Different proto */
			return -EINVAL;
L
Linus Torvalds 已提交
1194 1195 1196 1197 1198
		ifr->ifr_settings.type = IF_PROTO_FR;
		if (ifr->ifr_settings.size < size) {
			ifr->ifr_settings.size = size; /* data size wanted */
			return -ENOBUFS;
		}
1199
		if (copy_to_user(fr_s, &state(hdlc)->settings, size))
L
Linus Torvalds 已提交
1200 1201 1202 1203
			return -EFAULT;
		return 0;

	case IF_PROTO_FR:
1204
		if (!capable(CAP_NET_ADMIN))
L
Linus Torvalds 已提交
1205 1206
			return -EPERM;

1207
		if (dev->flags & IFF_UP)
L
Linus Torvalds 已提交
1208 1209 1210 1211 1212 1213 1214 1215 1216 1217
			return -EBUSY;

		if (copy_from_user(&new_settings, fr_s, size))
			return -EFAULT;

		if (new_settings.lmi == LMI_DEFAULT)
			new_settings.lmi = LMI_ANSI;

		if ((new_settings.lmi != LMI_NONE &&
		     new_settings.lmi != LMI_ANSI &&
K
Krzysztof Halasa 已提交
1218 1219
		     new_settings.lmi != LMI_CCITT &&
		     new_settings.lmi != LMI_CISCO) ||
L
Linus Torvalds 已提交
1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233
		    new_settings.t391 < 1 ||
		    new_settings.t392 < 2 ||
		    new_settings.n391 < 1 ||
		    new_settings.n392 < 1 ||
		    new_settings.n393 < new_settings.n392 ||
		    new_settings.n393 > 32 ||
		    (new_settings.dce != 0 &&
		     new_settings.dce != 1))
			return -EINVAL;

		result=hdlc->attach(dev, ENCODING_NRZ,PARITY_CRC16_PR1_CCITT);
		if (result)
			return result;

1234
		if (dev_to_hdlc(dev)->proto != &proto) { /* Different proto */
1235
			result = attach_hdlc_protocol(dev, &proto,
1236 1237 1238 1239 1240
						      sizeof(struct frad_state));
			if (result)
				return result;
			state(hdlc)->first_pvc = NULL;
			state(hdlc)->dce_pvc_count = 0;
L
Linus Torvalds 已提交
1241
		}
1242
		memcpy(&state(hdlc)->settings, &new_settings, size);
L
Linus Torvalds 已提交
1243
		dev->type = ARPHRD_FRAD;
1244
		call_netdevice_notifiers(NETDEV_POST_TYPE_CHANGE, dev);
L
Linus Torvalds 已提交
1245 1246 1247 1248 1249 1250
		return 0;

	case IF_PROTO_FR_ADD_PVC:
	case IF_PROTO_FR_DEL_PVC:
	case IF_PROTO_FR_ADD_ETH_PVC:
	case IF_PROTO_FR_DEL_ETH_PVC:
1251 1252 1253
		if (dev_to_hdlc(dev)->proto != &proto) /* Different proto */
			return -EINVAL;

1254
		if (!capable(CAP_NET_ADMIN))
L
Linus Torvalds 已提交
1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278
			return -EPERM;

		if (copy_from_user(&pvc, ifr->ifr_settings.ifs_ifsu.fr_pvc,
				   sizeof(fr_proto_pvc)))
			return -EFAULT;

		if (pvc.dlci <= 0 || pvc.dlci >= 1024)
			return -EINVAL;	/* Only 10 bits, DLCI 0 reserved */

		if (ifr->ifr_settings.type == IF_PROTO_FR_ADD_ETH_PVC ||
		    ifr->ifr_settings.type == IF_PROTO_FR_DEL_ETH_PVC)
			result = ARPHRD_ETHER; /* bridged Ethernet device */
		else
			result = ARPHRD_DLCI;

		if (ifr->ifr_settings.type == IF_PROTO_FR_ADD_PVC ||
		    ifr->ifr_settings.type == IF_PROTO_FR_ADD_ETH_PVC)
			return fr_add_pvc(dev, pvc.dlci, result);
		else
			return fr_del_pvc(hdlc, pvc.dlci, result);
	}

	return -EINVAL;
}
1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299


static int __init mod_init(void)
{
	register_hdlc_protocol(&proto);
	return 0;
}


static void __exit mod_exit(void)
{
	unregister_hdlc_protocol(&proto);
}


module_init(mod_init);
module_exit(mod_exit);

MODULE_AUTHOR("Krzysztof Halasa <khc@pm.waw.pl>");
MODULE_DESCRIPTION("Frame-Relay protocol support for generic HDLC");
MODULE_LICENSE("GPL v2");