br2684.c 22.8 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-only
L
Linus Torvalds 已提交
2
/*
3 4 5 6 7 8
 * Ethernet netdevice using ATM AAL5 as underlying carrier
 * (RFC1483 obsoleted by RFC2684) for Linux
 *
 * Authors: Marcell GAL, 2000, XDSL Ltd, Hungary
 *          Eric Kinzie, 2006-2007, US Naval Research Laboratory
 */
L
Linus Torvalds 已提交
9

10 11
#define pr_fmt(fmt) KBUILD_MODNAME ":%s: " fmt, __func__

L
Linus Torvalds 已提交
12 13 14 15 16 17 18 19 20
#include <linux/module.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/list.h>
#include <linux/netdevice.h>
#include <linux/skbuff.h>
#include <linux/etherdevice.h>
#include <linux/rtnetlink.h>
#include <linux/ip.h>
21
#include <linux/uaccess.h>
22
#include <linux/slab.h>
L
Linus Torvalds 已提交
23 24 25
#include <net/arp.h>
#include <linux/atm.h>
#include <linux/atmdev.h>
26
#include <linux/capability.h>
L
Linus Torvalds 已提交
27 28 29 30 31 32 33 34
#include <linux/seq_file.h>

#include <linux/atmbr2684.h>

#include "common.h"

static void skb_debug(const struct sk_buff *skb)
{
35
#ifdef SKB_DEBUG
L
Linus Torvalds 已提交
36
#define NUM2PRINT 50
37 38
	print_hex_dump(KERN_DEBUG, "br2684: skb: ", DUMP_OFFSET,
		       16, 1, skb->data, min(NUM2PRINT, skb->len), true);
L
Linus Torvalds 已提交
39
#endif
40
}
L
Linus Torvalds 已提交
41

E
Eric Kinzie 已提交
42 43 44 45 46 47 48 49 50 51 52
#define BR2684_ETHERTYPE_LEN	2
#define BR2684_PAD_LEN		2

#define LLC		0xaa, 0xaa, 0x03
#define SNAP_BRIDGED	0x00, 0x80, 0xc2
#define SNAP_ROUTED	0x00, 0x00, 0x00
#define PID_ETHERNET	0x00, 0x07
#define ETHERTYPE_IPV4	0x08, 0x00
#define ETHERTYPE_IPV6	0x86, 0xdd
#define PAD_BRIDGED	0x00, 0x00

53 54 55
static const unsigned char ethertype_ipv4[] = { ETHERTYPE_IPV4 };
static const unsigned char ethertype_ipv6[] = { ETHERTYPE_IPV6 };
static const unsigned char llc_oui_pid_pad[] =
56
			{ LLC, SNAP_BRIDGED, PID_ETHERNET, PAD_BRIDGED };
57
static const unsigned char pad[] = { PAD_BRIDGED };
58 59
static const unsigned char llc_oui_ipv4[] = { LLC, SNAP_ROUTED, ETHERTYPE_IPV4 };
static const unsigned char llc_oui_ipv6[] = { LLC, SNAP_ROUTED, ETHERTYPE_IPV6 };
L
Linus Torvalds 已提交
60 61

enum br2684_encaps {
62
	e_vc = BR2684_ENCAPS_VC,
L
Linus Torvalds 已提交
63 64 65 66
	e_llc = BR2684_ENCAPS_LLC,
};

struct br2684_vcc {
67
	struct atm_vcc *atmvcc;
L
Linus Torvalds 已提交
68
	struct net_device *device;
69
	/* keep old push, pop functions for chaining */
70
	void (*old_push)(struct atm_vcc *vcc, struct sk_buff *skb);
71
	void (*old_pop)(struct atm_vcc *vcc, struct sk_buff *skb);
72
	void (*old_release_cb)(struct atm_vcc *vcc);
D
David Woodhouse 已提交
73
	struct module *old_owner;
L
Linus Torvalds 已提交
74 75 76 77 78
	enum br2684_encaps encaps;
	struct list_head brvccs;
#ifdef CONFIG_ATM_BR2684_IPFILTER
	struct br2684_filter filter;
#endif /* CONFIG_ATM_BR2684_IPFILTER */
79
	unsigned int copies_needed, copies_failed;
80
	atomic_t qspace;
L
Linus Torvalds 已提交
81 82 83 84 85 86
};

struct br2684_dev {
	struct net_device *net_dev;
	struct list_head br2684_devs;
	int number;
87
	struct list_head brvccs;	/* one device <=> one vcc (before xmas) */
L
Linus Torvalds 已提交
88
	int mac_was_set;
E
Eric Kinzie 已提交
89
	enum br2684_payload payload;
L
Linus Torvalds 已提交
90 91 92 93 94 95
};

/*
 * This lock should be held for writing any time the list of devices or
 * their attached vcc's could be altered.  It should be held for reading
 * any time these are being queried.  Note that we sometimes need to
96 97
 * do read-locking under interrupting context, so write locking must block
 * the current CPU's interrupts.
L
Linus Torvalds 已提交
98 99 100 101 102 103 104
 */
static DEFINE_RWLOCK(devs_lock);

static LIST_HEAD(br2684_devs);

static inline struct br2684_dev *BRPRIV(const struct net_device *net_dev)
{
105
	return netdev_priv(net_dev);
L
Linus Torvalds 已提交
106 107 108 109 110 111 112 113 114
}

static inline struct net_device *list_entry_brdev(const struct list_head *le)
{
	return list_entry(le, struct br2684_dev, br2684_devs)->net_dev;
}

static inline struct br2684_vcc *BR2684_VCC(const struct atm_vcc *atmvcc)
{
115
	return (struct br2684_vcc *)(atmvcc->user_back);
L
Linus Torvalds 已提交
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
}

static inline struct br2684_vcc *list_entry_brvcc(const struct list_head *le)
{
	return list_entry(le, struct br2684_vcc, brvccs);
}

/* Caller should hold read_lock(&devs_lock) */
static struct net_device *br2684_find_dev(const struct br2684_if_spec *s)
{
	struct list_head *lh;
	struct net_device *net_dev;
	switch (s->method) {
	case BR2684_FIND_BYNUM:
		list_for_each(lh, &br2684_devs) {
			net_dev = list_entry_brdev(lh);
			if (BRPRIV(net_dev)->number == s->spec.devnum)
				return net_dev;
		}
		break;
	case BR2684_FIND_BYIFNAME:
		list_for_each(lh, &br2684_devs) {
			net_dev = list_entry_brdev(lh);
			if (!strncmp(net_dev->name, s->spec.ifname, IFNAMSIZ))
				return net_dev;
		}
		break;
	}
	return NULL;
}

147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
static int atm_dev_event(struct notifier_block *this, unsigned long event,
		 void *arg)
{
	struct atm_dev *atm_dev = arg;
	struct list_head *lh;
	struct net_device *net_dev;
	struct br2684_vcc *brvcc;
	struct atm_vcc *atm_vcc;
	unsigned long flags;

	pr_debug("event=%ld dev=%p\n", event, atm_dev);

	read_lock_irqsave(&devs_lock, flags);
	list_for_each(lh, &br2684_devs) {
		net_dev = list_entry_brdev(lh);

		list_for_each_entry(brvcc, &BRPRIV(net_dev)->brvccs, brvccs) {
			atm_vcc = brvcc->atmvcc;
			if (atm_vcc && brvcc->atmvcc->dev == atm_dev) {

				if (atm_vcc->dev->signal == ATM_PHY_SIG_LOST)
					netif_carrier_off(net_dev);
				else
					netif_carrier_on(net_dev);

			}
		}
	}
	read_unlock_irqrestore(&devs_lock, flags);

	return NOTIFY_DONE;
}

static struct notifier_block atm_dev_notifier = {
	.notifier_call = atm_dev_event,
};

184 185 186 187 188
/* chained vcc->pop function.  Check if we should wake the netif_queue */
static void br2684_pop(struct atm_vcc *vcc, struct sk_buff *skb)
{
	struct br2684_vcc *brvcc = BR2684_VCC(vcc);

189
	pr_debug("(vcc %p ; net_dev %p )\n", vcc, brvcc->device);
190 191
	brvcc->old_pop(vcc, skb);

192 193 194
	/* If the queue space just went up from zero, wake */
	if (atomic_inc_return(&brvcc->qspace) == 1)
		netif_wake_queue(brvcc->device);
195
}
196

L
Linus Torvalds 已提交
197 198 199 200 201
/*
 * Send a packet out a particular vcc.  Not to useful right now, but paves
 * the way for multiple vcc's per itf.  Returns true if we can send,
 * otherwise false
 */
S
Stephen Hemminger 已提交
202
static int br2684_xmit_vcc(struct sk_buff *skb, struct net_device *dev,
203
			   struct br2684_vcc *brvcc)
L
Linus Torvalds 已提交
204
{
S
Stephen Hemminger 已提交
205
	struct br2684_dev *brdev = BRPRIV(dev);
L
Linus Torvalds 已提交
206
	struct atm_vcc *atmvcc;
207 208 209 210
	int minheadroom = (brvcc->encaps == e_llc) ?
		((brdev->payload == p_bridged) ?
			sizeof(llc_oui_pid_pad) : sizeof(llc_oui_ipv4)) :
		((brdev->payload == p_bridged) ? BR2684_PAD_LEN : 0);
E
Eric Kinzie 已提交
211

L
Linus Torvalds 已提交
212 213 214 215 216 217 218 219 220 221
	if (skb_headroom(skb) < minheadroom) {
		struct sk_buff *skb2 = skb_realloc_headroom(skb, minheadroom);
		brvcc->copies_needed++;
		dev_kfree_skb(skb);
		if (skb2 == NULL) {
			brvcc->copies_failed++;
			return 0;
		}
		skb = skb2;
	}
E
Eric Kinzie 已提交
222 223 224 225

	if (brvcc->encaps == e_llc) {
		if (brdev->payload == p_bridged) {
			skb_push(skb, sizeof(llc_oui_pid_pad));
226 227
			skb_copy_to_linear_data(skb, llc_oui_pid_pad,
						sizeof(llc_oui_pid_pad));
E
Eric Kinzie 已提交
228 229 230 231 232
		} else if (brdev->payload == p_routed) {
			unsigned short prot = ntohs(skb->protocol);

			skb_push(skb, sizeof(llc_oui_ipv4));
			switch (prot) {
233 234 235 236 237 238 239 240 241 242 243
			case ETH_P_IP:
				skb_copy_to_linear_data(skb, llc_oui_ipv4,
							sizeof(llc_oui_ipv4));
				break;
			case ETH_P_IPV6:
				skb_copy_to_linear_data(skb, llc_oui_ipv6,
							sizeof(llc_oui_ipv6));
				break;
			default:
				dev_kfree_skb(skb);
				return 0;
E
Eric Kinzie 已提交
244 245
			}
		}
246 247 248
	} else { /* e_vc */
		if (brdev->payload == p_bridged) {
			skb_push(skb, 2);
E
Eric Kinzie 已提交
249
			memset(skb->data, 0, 2);
250
		}
E
Eric Kinzie 已提交
251
	}
L
Linus Torvalds 已提交
252 253 254
	skb_debug(skb);

	ATM_SKB(skb)->vcc = atmvcc = brvcc->atmvcc;
255
	pr_debug("atm_skb(%p)->vcc(%p)->dev(%p)\n", skb, atmvcc, atmvcc->dev);
256
	atm_account_tx(atmvcc, skb);
S
Stephen Hemminger 已提交
257 258
	dev->stats.tx_packets++;
	dev->stats.tx_bytes += skb->len;
259

260 261
	if (atomic_dec_return(&brvcc->qspace) < 1) {
		/* No more please! */
262
		netif_stop_queue(brvcc->device);
263 264 265
		/* We might have raced with br2684_pop() */
		if (unlikely(atomic_read(&brvcc->qspace) > 0))
			netif_wake_queue(brvcc->device);
266 267
	}

268 269 270 271
	/* If this fails immediately, the skb will be freed and br2684_pop()
	   will wake the queue if appropriate. Just return an error so that
	   the stats are updated correctly */
	return !atmvcc->send(atmvcc, skb);
L
Linus Torvalds 已提交
272 273
}

274 275 276 277 278 279 280 281 282 283 284
static void br2684_release_cb(struct atm_vcc *atmvcc)
{
	struct br2684_vcc *brvcc = BR2684_VCC(atmvcc);

	if (atomic_read(&brvcc->qspace) > 0)
		netif_wake_queue(brvcc->device);

	if (brvcc->old_release_cb)
		brvcc->old_release_cb(atmvcc);
}

285 286
static inline struct br2684_vcc *pick_outgoing_vcc(const struct sk_buff *skb,
						   const struct br2684_dev *brdev)
L
Linus Torvalds 已提交
287
{
288
	return list_empty(&brdev->brvccs) ? NULL : list_entry_brvcc(brdev->brvccs.next);	/* 1 vcc/dev right now */
L
Linus Torvalds 已提交
289 290
}

291 292
static netdev_tx_t br2684_start_xmit(struct sk_buff *skb,
				     struct net_device *dev)
L
Linus Torvalds 已提交
293 294 295
{
	struct br2684_dev *brdev = BRPRIV(dev);
	struct br2684_vcc *brvcc;
296 297
	struct atm_vcc *atmvcc;
	netdev_tx_t ret = NETDEV_TX_OK;
L
Linus Torvalds 已提交
298

299
	pr_debug("skb_dst(skb)=%p\n", skb_dst(skb));
L
Linus Torvalds 已提交
300 301 302
	read_lock(&devs_lock);
	brvcc = pick_outgoing_vcc(skb, brdev);
	if (brvcc == NULL) {
303
		pr_debug("no vcc attached to dev %s\n", dev->name);
S
Stephen Hemminger 已提交
304 305
		dev->stats.tx_errors++;
		dev->stats.tx_carrier_errors++;
L
Linus Torvalds 已提交
306 307
		/* netif_stop_queue(dev); */
		dev_kfree_skb(skb);
308
		goto out_devs;
L
Linus Torvalds 已提交
309
	}
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
	atmvcc = brvcc->atmvcc;

	bh_lock_sock(sk_atm(atmvcc));

	if (test_bit(ATM_VF_RELEASED, &atmvcc->flags) ||
	    test_bit(ATM_VF_CLOSE, &atmvcc->flags) ||
	    !test_bit(ATM_VF_READY, &atmvcc->flags)) {
		dev->stats.tx_dropped++;
		dev_kfree_skb(skb);
		goto out;
	}

	if (sock_owned_by_user(sk_atm(atmvcc))) {
		netif_stop_queue(brvcc->device);
		ret = NETDEV_TX_BUSY;
		goto out;
	}

S
Stephen Hemminger 已提交
328
	if (!br2684_xmit_vcc(skb, dev, brvcc)) {
L
Linus Torvalds 已提交
329 330 331
		/*
		 * We should probably use netif_*_queue() here, but that
		 * involves added complication.  We need to walk before
332 333 334
		 * we can run.
		 *
		 * Don't free here! this pointer might be no longer valid!
L
Linus Torvalds 已提交
335
		 */
S
Stephen Hemminger 已提交
336 337
		dev->stats.tx_errors++;
		dev->stats.tx_fifo_errors++;
L
Linus Torvalds 已提交
338
	}
339 340 341
 out:
	bh_unlock_sock(sk_atm(atmvcc));
 out_devs:
L
Linus Torvalds 已提交
342
	read_unlock(&devs_lock);
343
	return ret;
L
Linus Torvalds 已提交
344 345 346 347 348 349 350 351
}

/*
 * We remember when the MAC gets set, so we don't override it later with
 * the ESI of the ATM card of the first VC
 */
static int br2684_mac_addr(struct net_device *dev, void *p)
{
352
	int err = eth_mac_addr(dev, p);
L
Linus Torvalds 已提交
353 354 355 356 357 358 359
	if (!err)
		BRPRIV(dev)->mac_was_set = 1;
	return err;
}

#ifdef CONFIG_ATM_BR2684_IPFILTER
/* this IOCTL is experimental. */
360
static int br2684_setfilt(struct atm_vcc *atmvcc, void __user * arg)
L
Linus Torvalds 已提交
361 362 363 364 365 366 367 368 369
{
	struct br2684_vcc *brvcc;
	struct br2684_filter_set fs;

	if (copy_from_user(&fs, arg, sizeof fs))
		return -EFAULT;
	if (fs.ifspec.method != BR2684_FIND_BYNOTHING) {
		/*
		 * This is really a per-vcc thing, but we can also search
370
		 * by device.
L
Linus Torvalds 已提交
371 372 373 374
		 */
		struct br2684_dev *brdev;
		read_lock(&devs_lock);
		brdev = BRPRIV(br2684_find_dev(&fs.ifspec));
375 376
		if (brdev == NULL || list_empty(&brdev->brvccs) ||
		    brdev->brvccs.next != brdev->brvccs.prev)	/* >1 VCC */
L
Linus Torvalds 已提交
377 378 379 380 381 382 383 384 385 386 387 388 389 390
			brvcc = NULL;
		else
			brvcc = list_entry_brvcc(brdev->brvccs.next);
		read_unlock(&devs_lock);
		if (brvcc == NULL)
			return -ESRCH;
	} else
		brvcc = BR2684_VCC(atmvcc);
	memcpy(&brvcc->filter, &fs.filter, sizeof(brvcc->filter));
	return 0;
}

/* Returns 1 if packet should be dropped */
static inline int
A
Al Viro 已提交
391
packet_fails_filter(__be16 type, struct br2684_vcc *brvcc, struct sk_buff *skb)
L
Linus Torvalds 已提交
392 393
{
	if (brvcc->filter.netmask == 0)
394
		return 0;	/* no filter in place */
395
	if (type == htons(ETH_P_IP) &&
396
	    (((struct iphdr *)(skb->data))->daddr & brvcc->filter.
L
Linus Torvalds 已提交
397 398
	     netmask) == brvcc->filter.prefix)
		return 0;
399
	if (type == htons(ETH_P_ARP))
L
Linus Torvalds 已提交
400
		return 0;
401 402 403
	/*
	 * TODO: we should probably filter ARPs too.. don't want to have
	 * them returning values that don't make sense, or is that ok?
L
Linus Torvalds 已提交
404 405 406 407 408 409 410
	 */
	return 1;		/* drop */
}
#endif /* CONFIG_ATM_BR2684_IPFILTER */

static void br2684_close_vcc(struct br2684_vcc *brvcc)
{
411
	pr_debug("removing VCC %p from dev %p\n", brvcc, brvcc->device);
L
Linus Torvalds 已提交
412 413 414 415
	write_lock_irq(&devs_lock);
	list_del(&brvcc->brvccs);
	write_unlock_irq(&devs_lock);
	brvcc->atmvcc->user_back = NULL;	/* what about vcc->recvq ??? */
416
	brvcc->atmvcc->release_cb = brvcc->old_release_cb;
L
Linus Torvalds 已提交
417
	brvcc->old_push(brvcc->atmvcc, NULL);	/* pass on the bad news */
D
David Woodhouse 已提交
418
	module_put(brvcc->old_owner);
L
Linus Torvalds 已提交
419 420 421 422 423 424 425 426 427 428
	kfree(brvcc);
}

/* when AAL5 PDU comes in: */
static void br2684_push(struct atm_vcc *atmvcc, struct sk_buff *skb)
{
	struct br2684_vcc *brvcc = BR2684_VCC(atmvcc);
	struct net_device *net_dev = brvcc->device;
	struct br2684_dev *brdev = BRPRIV(net_dev);

429
	pr_debug("\n");
L
Linus Torvalds 已提交
430 431 432 433 434

	if (unlikely(skb == NULL)) {
		/* skb==NULL means VCC is being destroyed */
		br2684_close_vcc(brvcc);
		if (list_empty(&brdev->brvccs)) {
435
			write_lock_irq(&devs_lock);
L
Linus Torvalds 已提交
436
			list_del(&brdev->br2684_devs);
437
			write_unlock_irq(&devs_lock);
L
Linus Torvalds 已提交
438
			unregister_netdev(net_dev);
439
			free_netdev(net_dev);
L
Linus Torvalds 已提交
440 441 442 443 444 445
		}
		return;
	}

	skb_debug(skb);
	atm_return(atmvcc, skb->truesize);
446
	pr_debug("skb from brdev %p\n", brdev);
L
Linus Torvalds 已提交
447
	if (brvcc->encaps == e_llc) {
E
Eric Kinzie 已提交
448 449 450 451 452

		if (skb->len > 7 && skb->data[7] == 0x01)
			__skb_trim(skb, skb->len - 4);

		/* accept packets that have "ipv[46]" in the snap header */
453 454 455 456 457
		if ((skb->len >= (sizeof(llc_oui_ipv4))) &&
		    (memcmp(skb->data, llc_oui_ipv4,
			    sizeof(llc_oui_ipv4) - BR2684_ETHERTYPE_LEN) == 0)) {
			if (memcmp(skb->data + 6, ethertype_ipv6,
				   sizeof(ethertype_ipv6)) == 0)
458
				skb->protocol = htons(ETH_P_IPV6);
459 460
			else if (memcmp(skb->data + 6, ethertype_ipv4,
					sizeof(ethertype_ipv4)) == 0)
461
				skb->protocol = htons(ETH_P_IP);
462 463
			else
				goto error;
E
Eric Kinzie 已提交
464 465 466
			skb_pull(skb, sizeof(llc_oui_ipv4));
			skb_reset_network_header(skb);
			skb->pkt_type = PACKET_HOST;
467 468 469 470 471
		/*
		 * Let us waste some time for checking the encapsulation.
		 * Note, that only 7 char is checked so frames with a valid FCS
		 * are also accepted (but FCS is not checked of course).
		 */
E
Eric Kinzie 已提交
472
		} else if ((skb->len >= sizeof(llc_oui_pid_pad)) &&
473
			   (memcmp(skb->data, llc_oui_pid_pad, 7) == 0)) {
E
Eric Kinzie 已提交
474 475
			skb_pull(skb, sizeof(llc_oui_pid_pad));
			skb->protocol = eth_type_trans(skb, net_dev);
476 477
		} else
			goto error;
L
Linus Torvalds 已提交
478

479 480 481 482 483 484 485
	} else { /* e_vc */
		if (brdev->payload == p_routed) {
			struct iphdr *iph;

			skb_reset_network_header(skb);
			iph = ip_hdr(skb);
			if (iph->version == 4)
486
				skb->protocol = htons(ETH_P_IP);
487
			else if (iph->version == 6)
488
				skb->protocol = htons(ETH_P_IPV6);
489 490 491 492 493
			else
				goto error;
			skb->pkt_type = PACKET_HOST;
		} else { /* p_bridged */
			/* first 2 chars should be 0 */
494
			if (memcmp(skb->data, pad, BR2684_PAD_LEN) != 0)
495 496 497
				goto error;
			skb_pull(skb, BR2684_PAD_LEN);
			skb->protocol = eth_type_trans(skb, net_dev);
L
Linus Torvalds 已提交
498 499 500 501
		}
	}

#ifdef CONFIG_ATM_BR2684_IPFILTER
502 503
	if (unlikely(packet_fails_filter(skb->protocol, brvcc, skb)))
		goto dropped;
L
Linus Torvalds 已提交
504 505 506
#endif /* CONFIG_ATM_BR2684_IPFILTER */
	skb->dev = net_dev;
	ATM_SKB(skb)->vcc = atmvcc;	/* needed ? */
507
	pr_debug("received packet's protocol: %x\n", ntohs(skb->protocol));
L
Linus Torvalds 已提交
508
	skb_debug(skb);
509 510 511
	/* sigh, interface is down? */
	if (unlikely(!(net_dev->flags & IFF_UP)))
		goto dropped;
S
Stephen Hemminger 已提交
512 513
	net_dev->stats.rx_packets++;
	net_dev->stats.rx_bytes += skb->len;
L
Linus Torvalds 已提交
514 515
	memset(ATM_SKB(skb), 0, sizeof(struct atm_skb_data));
	netif_rx(skb);
516 517 518
	return;

dropped:
S
Stephen Hemminger 已提交
519
	net_dev->stats.rx_dropped++;
520 521
	goto free_skb;
error:
S
Stephen Hemminger 已提交
522
	net_dev->stats.rx_errors++;
523 524
free_skb:
	dev_kfree_skb(skb);
L
Linus Torvalds 已提交
525 526
}

527 528 529 530 531
/*
 * Assign a vcc to a dev
 * Note: we do not have explicit unassign, but look at _push()
 */
static int br2684_regvcc(struct atm_vcc *atmvcc, void __user * arg)
L
Linus Torvalds 已提交
532 533 534 535 536
{
	struct br2684_vcc *brvcc;
	struct br2684_dev *brdev;
	struct net_device *net_dev;
	struct atm_backend_br2684 be;
537
	int err;
L
Linus Torvalds 已提交
538 539 540

	if (copy_from_user(&be, arg, sizeof be))
		return -EFAULT;
541
	brvcc = kzalloc(sizeof(struct br2684_vcc), GFP_KERNEL);
L
Linus Torvalds 已提交
542 543
	if (!brvcc)
		return -ENOMEM;
544 545 546 547 548 549 550
	/*
	 * Allow two packets in the ATM queue. One actually being sent, and one
	 * for the ATM 'TX done' handler to send. It shouldn't take long to get
	 * the next one from the netdev queue, when we need it. More than that
	 * would be bufferbloat.
	 */
	atomic_set(&brvcc->qspace, 2);
L
Linus Torvalds 已提交
551 552 553
	write_lock_irq(&devs_lock);
	net_dev = br2684_find_dev(&be.ifspec);
	if (net_dev == NULL) {
L
Lucas De Marchi 已提交
554
		pr_err("tried to attach to non-existent device\n");
L
Linus Torvalds 已提交
555 556 557 558 559 560 561 562 563 564 565 566 567
		err = -ENXIO;
		goto error;
	}
	brdev = BRPRIV(net_dev);
	if (atmvcc->push == NULL) {
		err = -EBADFD;
		goto error;
	}
	if (!list_empty(&brdev->brvccs)) {
		/* Only 1 VCC/dev right now */
		err = -EEXIST;
		goto error;
	}
568 569 570 571 572 573
	if (be.fcs_in != BR2684_FCSIN_NO ||
	    be.fcs_out != BR2684_FCSOUT_NO ||
	    be.fcs_auto || be.has_vpiid || be.send_padding ||
	    (be.encaps != BR2684_ENCAPS_VC &&
	     be.encaps != BR2684_ENCAPS_LLC) ||
	    be.min_size != 0) {
L
Linus Torvalds 已提交
574 575 576
		err = -EINVAL;
		goto error;
	}
577
	pr_debug("vcc=%p, encaps=%d, brvcc=%p\n", atmvcc, be.encaps, brvcc);
L
Linus Torvalds 已提交
578 579
	if (list_empty(&brdev->brvccs) && !brdev->mac_was_set) {
		unsigned char *esi = atmvcc->dev->esi;
580 581
		const u8 one = 1;

L
Linus Torvalds 已提交
582
		if (esi[0] | esi[1] | esi[2] | esi[3] | esi[4] | esi[5])
J
Jakub Kicinski 已提交
583
			dev_addr_set(net_dev, esi);
L
Linus Torvalds 已提交
584
		else
585
			dev_addr_mod(net_dev, 2, &one, 1);
L
Linus Torvalds 已提交
586 587 588 589 590 591
	}
	list_add(&brvcc->brvccs, &brdev->brvccs);
	write_unlock_irq(&devs_lock);
	brvcc->device = net_dev;
	brvcc->atmvcc = atmvcc;
	atmvcc->user_back = brvcc;
592
	brvcc->encaps = (enum br2684_encaps)be.encaps;
L
Linus Torvalds 已提交
593
	brvcc->old_push = atmvcc->push;
594
	brvcc->old_pop = atmvcc->pop;
595
	brvcc->old_release_cb = atmvcc->release_cb;
D
David Woodhouse 已提交
596
	brvcc->old_owner = atmvcc->owner;
L
Linus Torvalds 已提交
597 598
	barrier();
	atmvcc->push = br2684_push;
599
	atmvcc->pop = br2684_pop;
600
	atmvcc->release_cb = br2684_release_cb;
D
David Woodhouse 已提交
601
	atmvcc->owner = THIS_MODULE;
D
David S. Miller 已提交
602

603 604 605 606 607 608
	/* initialize netdev carrier state */
	if (atmvcc->dev->signal == ATM_PHY_SIG_LOST)
		netif_carrier_off(net_dev);
	else
		netif_carrier_on(net_dev);

L
Linus Torvalds 已提交
609
	__module_get(THIS_MODULE);
610 611 612 613

	/* re-process everything received between connection setup and
	   backend setup */
	vcc_process_recv_queue(atmvcc);
L
Linus Torvalds 已提交
614
	return 0;
615 616

error:
L
Linus Torvalds 已提交
617 618 619 620 621
	write_unlock_irq(&devs_lock);
	kfree(brvcc);
	return err;
}

622 623 624 625 626 627
static const struct net_device_ops br2684_netdev_ops = {
	.ndo_start_xmit 	= br2684_start_xmit,
	.ndo_set_mac_address	= br2684_mac_addr,
	.ndo_validate_addr	= eth_validate_addr,
};

628 629 630 631 632
static const struct net_device_ops br2684_netdev_ops_routed = {
	.ndo_start_xmit 	= br2684_start_xmit,
	.ndo_set_mac_address	= br2684_mac_addr,
};

L
Linus Torvalds 已提交
633 634 635 636 637
static void br2684_setup(struct net_device *netdev)
{
	struct br2684_dev *brdev = BRPRIV(netdev);

	ether_setup(netdev);
638
	netdev->hard_header_len += sizeof(llc_oui_pid_pad); /* worst case */
639
	brdev->net_dev = netdev;
L
Linus Torvalds 已提交
640

641
	netdev->netdev_ops = &br2684_netdev_ops;
L
Linus Torvalds 已提交
642 643 644 645

	INIT_LIST_HEAD(&brdev->brvccs);
}

E
Eric Kinzie 已提交
646 647 648 649
static void br2684_setup_routed(struct net_device *netdev)
{
	struct br2684_dev *brdev = BRPRIV(netdev);

650
	brdev->net_dev = netdev;
651
	netdev->hard_header_len = sizeof(llc_oui_ipv4); /* worst case */
652
	netdev->netdev_ops = &br2684_netdev_ops_routed;
E
Eric Kinzie 已提交
653
	netdev->addr_len = 0;
654 655 656
	netdev->mtu = ETH_DATA_LEN;
	netdev->min_mtu = 0;
	netdev->max_mtu = ETH_MAX_MTU;
E
Eric Kinzie 已提交
657 658 659 660 661 662
	netdev->type = ARPHRD_PPP;
	netdev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
	netdev->tx_queue_len = 100;
	INIT_LIST_HEAD(&brdev->brvccs);
}

663
static int br2684_create(void __user *arg)
L
Linus Torvalds 已提交
664 665 666 667 668
{
	int err;
	struct net_device *netdev;
	struct br2684_dev *brdev;
	struct atm_newif_br2684 ni;
E
Eric Kinzie 已提交
669
	enum br2684_payload payload;
L
Linus Torvalds 已提交
670

671
	pr_debug("\n");
L
Linus Torvalds 已提交
672

673
	if (copy_from_user(&ni, arg, sizeof ni))
L
Linus Torvalds 已提交
674
		return -EFAULT;
E
Eric Kinzie 已提交
675 676 677 678 679

	if (ni.media & BR2684_FLAG_ROUTED)
		payload = p_routed;
	else
		payload = p_bridged;
680
	ni.media &= 0xffff;	/* strip flags */
E
Eric Kinzie 已提交
681

682
	if (ni.media != BR2684_MEDIA_ETHERNET || ni.mtu != 1500)
L
Linus Torvalds 已提交
683 684 685 686
		return -EINVAL;

	netdev = alloc_netdev(sizeof(struct br2684_dev),
			      ni.ifname[0] ? ni.ifname : "nas%d",
687 688
			      NET_NAME_UNKNOWN,
			      (payload == p_routed) ? br2684_setup_routed : br2684_setup);
L
Linus Torvalds 已提交
689 690 691 692 693
	if (!netdev)
		return -ENOMEM;

	brdev = BRPRIV(netdev);

694
	pr_debug("registered netdev %s\n", netdev->name);
L
Linus Torvalds 已提交
695 696 697
	/* open, stop, do_ioctl ? */
	err = register_netdev(netdev);
	if (err < 0) {
698
		pr_err("register_netdev failed\n");
L
Linus Torvalds 已提交
699 700 701 702 703
		free_netdev(netdev);
		return err;
	}

	write_lock_irq(&devs_lock);
704

E
Eric Kinzie 已提交
705
	brdev->payload = payload;
706 707 708 709 710 711 712

	if (list_empty(&br2684_devs)) {
		/* 1st br2684 device */
		brdev->number = 1;
	} else
		brdev->number = BRPRIV(list_entry_brdev(br2684_devs.prev))->number + 1;

L
Linus Torvalds 已提交
713 714 715 716 717 718 719 720 721 722
	list_add_tail(&brdev->br2684_devs, &br2684_devs);
	write_unlock_irq(&devs_lock);
	return 0;
}

/*
 * This handles ioctls actually performed on our vcc - we must return
 * -ENOIOCTLCMD for any unrecognized ioctl
 */
static int br2684_ioctl(struct socket *sock, unsigned int cmd,
723
			unsigned long arg)
L
Linus Torvalds 已提交
724 725 726
{
	struct atm_vcc *atmvcc = ATM_SD(sock);
	void __user *argp = (void __user *)arg;
727
	atm_backend_t b;
L
Linus Torvalds 已提交
728 729

	int err;
730
	switch (cmd) {
L
Linus Torvalds 已提交
731
	case ATM_SETBACKEND:
732
	case ATM_NEWBACKENDIF:
L
Linus Torvalds 已提交
733 734 735 736 737 738 739
		err = get_user(b, (atm_backend_t __user *) argp);
		if (err)
			return -EFAULT;
		if (b != ATM_BACKEND_BR2684)
			return -ENOIOCTLCMD;
		if (!capable(CAP_NET_ADMIN))
			return -EPERM;
740 741 742
		if (cmd == ATM_SETBACKEND) {
			if (sock->state != SS_CONNECTED)
				return -EINVAL;
L
Linus Torvalds 已提交
743
			return br2684_regvcc(atmvcc, argp);
744
		} else {
L
Linus Torvalds 已提交
745
			return br2684_create(argp);
746
		}
L
Linus Torvalds 已提交
747 748 749 750 751 752 753
#ifdef CONFIG_ATM_BR2684_IPFILTER
	case BR2684_SETFILT:
		if (atmvcc->push != br2684_push)
			return -ENOIOCTLCMD;
		if (!capable(CAP_NET_ADMIN))
			return -EPERM;
		err = br2684_setfilt(atmvcc, argp);
754

L
Linus Torvalds 已提交
755 756 757 758 759 760 761
		return err;
#endif /* CONFIG_ATM_BR2684_IPFILTER */
	}
	return -ENOIOCTLCMD;
}

static struct atm_ioctl br2684_ioctl_ops = {
762 763
	.owner = THIS_MODULE,
	.ioctl = br2684_ioctl,
L
Linus Torvalds 已提交
764 765 766
};

#ifdef CONFIG_PROC_FS
767
static void *br2684_seq_start(struct seq_file *seq, loff_t * pos)
768
	__acquires(devs_lock)
L
Linus Torvalds 已提交
769 770
{
	read_lock(&devs_lock);
771
	return seq_list_start(&br2684_devs, *pos);
L
Linus Torvalds 已提交
772 773
}

774
static void *br2684_seq_next(struct seq_file *seq, void *v, loff_t * pos)
L
Linus Torvalds 已提交
775
{
776
	return seq_list_next(v, &br2684_devs, pos);
L
Linus Torvalds 已提交
777 778 779
}

static void br2684_seq_stop(struct seq_file *seq, void *v)
780
	__releases(devs_lock)
L
Linus Torvalds 已提交
781 782 783 784 785 786
{
	read_unlock(&devs_lock);
}

static int br2684_seq_show(struct seq_file *seq, void *v)
{
787
	const struct br2684_dev *brdev = list_entry(v, struct br2684_dev,
788
						    br2684_devs);
L
Linus Torvalds 已提交
789 790 791
	const struct net_device *net_dev = brdev->net_dev;
	const struct br2684_vcc *brvcc;

J
Johannes Berg 已提交
792
	seq_printf(seq, "dev %.16s: num=%d, mac=%pM (%s)\n",
793 794
		   net_dev->name,
		   brdev->number,
J
Johannes Berg 已提交
795
		   net_dev->dev_addr,
796
		   brdev->mac_was_set ? "set" : "auto");
L
Linus Torvalds 已提交
797 798

	list_for_each_entry(brvcc, &brdev->brvccs, brvccs) {
E
Eric Kinzie 已提交
799
		seq_printf(seq, "  vcc %d.%d.%d: encaps=%s payload=%s"
800 801 802 803 804 805
			   ", failed copies %u/%u"
			   "\n", brvcc->atmvcc->dev->number,
			   brvcc->atmvcc->vpi, brvcc->atmvcc->vci,
			   (brvcc->encaps == e_llc) ? "LLC" : "VC",
			   (brdev->payload == p_bridged) ? "bridged" : "routed",
			   brvcc->copies_failed, brvcc->copies_needed);
L
Linus Torvalds 已提交
806
#ifdef CONFIG_ATM_BR2684_IPFILTER
807
		if (brvcc->filter.netmask != 0)
808 809 810
			seq_printf(seq, "    filter=%pI4/%pI4\n",
				   &brvcc->filter.prefix,
				   &brvcc->filter.netmask);
L
Linus Torvalds 已提交
811 812 813 814 815
#endif /* CONFIG_ATM_BR2684_IPFILTER */
	}
	return 0;
}

816
static const struct seq_operations br2684_seq_ops = {
L
Linus Torvalds 已提交
817
	.start = br2684_seq_start,
818 819 820
	.next = br2684_seq_next,
	.stop = br2684_seq_stop,
	.show = br2684_seq_show,
L
Linus Torvalds 已提交
821 822 823
};

extern struct proc_dir_entry *atm_proc_root;	/* from proc.c */
824
#endif /* CONFIG_PROC_FS */
L
Linus Torvalds 已提交
825 826 827 828 829

static int __init br2684_init(void)
{
#ifdef CONFIG_PROC_FS
	struct proc_dir_entry *p;
830
	p = proc_create_seq("br2684", 0, atm_proc_root, &br2684_seq_ops);
831
	if (p == NULL)
L
Linus Torvalds 已提交
832 833 834
		return -ENOMEM;
#endif
	register_atm_ioctl(&br2684_ioctl_ops);
835
	register_atmdevice_notifier(&atm_dev_notifier);
L
Linus Torvalds 已提交
836 837 838 839 840 841 842 843 844 845 846 847 848 849
	return 0;
}

static void __exit br2684_exit(void)
{
	struct net_device *net_dev;
	struct br2684_dev *brdev;
	struct br2684_vcc *brvcc;
	deregister_atm_ioctl(&br2684_ioctl_ops);

#ifdef CONFIG_PROC_FS
	remove_proc_entry("br2684", atm_proc_root);
#endif

850

851
	unregister_atmdevice_notifier(&atm_dev_notifier);
852

L
Linus Torvalds 已提交
853 854 855 856 857 858 859 860 861 862
	while (!list_empty(&br2684_devs)) {
		net_dev = list_entry_brdev(br2684_devs.next);
		brdev = BRPRIV(net_dev);
		while (!list_empty(&brdev->brvccs)) {
			brvcc = list_entry_brvcc(brdev->brvccs.next);
			br2684_close_vcc(brvcc);
		}

		list_del(&brdev->br2684_devs);
		unregister_netdev(net_dev);
863
		free_netdev(net_dev);
L
Linus Torvalds 已提交
864 865 866 867 868 869 870 871 872
	}
}

module_init(br2684_init);
module_exit(br2684_exit);

MODULE_AUTHOR("Marcell GAL");
MODULE_DESCRIPTION("RFC2684 bridged protocols over ATM/AAL5");
MODULE_LICENSE("GPL");