mpc.c 38.2 KB
Newer Older
1 2
#define pr_fmt(fmt) KBUILD_MODNAME ":%s: " fmt, __func__

L
Linus Torvalds 已提交
3 4
#include <linux/kernel.h>
#include <linux/string.h>
5
#include <linux/slab.h>
L
Linus Torvalds 已提交
6 7 8
#include <linux/timer.h>
#include <linux/init.h>
#include <linux/bitops.h>
9
#include <linux/capability.h>
L
Linus Torvalds 已提交
10 11 12 13 14 15 16 17 18
#include <linux/seq_file.h>

/* We are an ethernet device */
#include <linux/if_ether.h>
#include <linux/netdevice.h>
#include <linux/etherdevice.h>
#include <net/sock.h>
#include <linux/skbuff.h>
#include <linux/ip.h>
J
Joe Perches 已提交
19
#include <linux/uaccess.h>
L
Linus Torvalds 已提交
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
#include <asm/byteorder.h>
#include <net/checksum.h>   /* for ip_fast_csum() */
#include <net/arp.h>
#include <net/dst.h>
#include <linux/proc_fs.h>

/* And atm device */
#include <linux/atmdev.h>
#include <linux/atmlec.h>
#include <linux/atmmpc.h>
/* Modular too */
#include <linux/module.h>

#include "lec.h"
#include "mpc.h"
#include "resources.h"

/*
38
 * mpc.c: Implementation of MPOA client kernel part
L
Linus Torvalds 已提交
39 40 41
 */

#if 0
42 43 44
#define dprintk(format, args...) \
	printk(KERN_DEBUG "mpoa:%s: " format, __func__, ##args)
#define dprintk_cont(format, args...) printk(KERN_CONT format, ##args)
L
Linus Torvalds 已提交
45
#else
46 47 48 49 50 51
#define dprintk(format, args...)					\
	do { if (0)							\
		printk(KERN_DEBUG "mpoa:%s: " format, __func__, ##args);\
	} while (0)
#define dprintk_cont(format, args...)			\
	do { if (0) printk(KERN_CONT format, ##args); } while (0)
L
Linus Torvalds 已提交
52 53 54
#endif

#if 0
55 56 57
#define ddprintk(format, args...) \
	printk(KERN_DEBUG "mpoa:%s: " format, __func__, ##args)
#define ddprintk_cont(format, args...) printk(KERN_CONT format, ##args)
L
Linus Torvalds 已提交
58
#else
59 60 61 62 63 64
#define ddprintk(format, args...)					\
	do { if (0)							\
		printk(KERN_DEBUG "mpoa:%s: " format, __func__, ##args);\
	} while (0)
#define ddprintk_cont(format, args...)			\
	do { if (0) printk(KERN_CONT format, ##args); } while (0)
L
Linus Torvalds 已提交
65 66 67
#endif

/* mpc_daemon -> kernel */
J
Joe Perches 已提交
68
static void MPOA_trigger_rcvd(struct k_message *msg, struct mpoa_client *mpc);
L
Linus Torvalds 已提交
69 70 71 72
static void MPOA_res_reply_rcvd(struct k_message *msg, struct mpoa_client *mpc);
static void ingress_purge_rcvd(struct k_message *msg, struct mpoa_client *mpc);
static void egress_purge_rcvd(struct k_message *msg, struct mpoa_client *mpc);
static void mps_death(struct k_message *msg, struct mpoa_client *mpc);
J
Joe Perches 已提交
73 74 75 76 77 78 79 80
static void clean_up(struct k_message *msg, struct mpoa_client *mpc,
		     int action);
static void MPOA_cache_impos_rcvd(struct k_message *msg,
				  struct mpoa_client *mpc);
static void set_mpc_ctrl_addr_rcvd(struct k_message *mesg,
				   struct mpoa_client *mpc);
static void set_mps_mac_addr_rcvd(struct k_message *mesg,
				  struct mpoa_client *mpc);
L
Linus Torvalds 已提交
81

82 83 84 85
static const uint8_t *copy_macs(struct mpoa_client *mpc,
				const uint8_t *router_mac,
				const uint8_t *tlvs, uint8_t mps_macs,
				uint8_t device_type);
L
Linus Torvalds 已提交
86 87
static void purge_egress_shortcut(struct atm_vcc *vcc, eg_cache_entry *entry);

88
static void send_set_mps_ctrl_addr(const char *addr, struct mpoa_client *mpc);
L
Linus Torvalds 已提交
89 90 91 92
static void mpoad_close(struct atm_vcc *vcc);
static int msg_from_mpoad(struct atm_vcc *vcc, struct sk_buff *skb);

static void mpc_push(struct atm_vcc *vcc, struct sk_buff *skb);
93
static netdev_tx_t mpc_send_packet(struct sk_buff *skb,
J
Joe Perches 已提交
94 95 96
				   struct net_device *dev);
static int mpoa_event_listener(struct notifier_block *mpoa_notifier,
			       unsigned long event, void *dev);
L
Linus Torvalds 已提交
97
static void mpc_timer_refresh(void);
J
Joe Perches 已提交
98
static void mpc_cache_check(unsigned long checking_time);
L
Linus Torvalds 已提交
99 100 101 102 103

static struct llc_snap_hdr llc_snap_mpoa_ctrl = {
	0xaa, 0xaa, 0x03,
	{0x00, 0x00, 0x5e},
	{0x00, 0x03}         /* For MPOA control PDUs */
104
};
L
Linus Torvalds 已提交
105 106 107 108
static struct llc_snap_hdr llc_snap_mpoa_data = {
	0xaa, 0xaa, 0x03,
	{0x00, 0x00, 0x00},
	{0x08, 0x00}         /* This is for IP PDUs only */
109
};
L
Linus Torvalds 已提交
110 111 112 113
static struct llc_snap_hdr llc_snap_mpoa_data_tagged = {
	0xaa, 0xaa, 0x03,
	{0x00, 0x00, 0x00},
	{0x88, 0x4c}         /* This is for tagged data PDUs */
114
};
L
Linus Torvalds 已提交
115 116 117 118 119 120 121 122 123

static struct notifier_block mpoa_notifier = {
	mpoa_event_listener,
	NULL,
	0
};

struct mpoa_client *mpcs = NULL; /* FIXME */
static struct atm_mpoa_qos *qos_head = NULL;
124
static DEFINE_TIMER(mpc_timer, NULL);
L
Linus Torvalds 已提交
125 126 127 128 129


static struct mpoa_client *find_mpc_by_itfnum(int itf)
{
	struct mpoa_client *mpc;
130

L
Linus Torvalds 已提交
131 132 133 134
	mpc = mpcs;  /* our global linked list */
	while (mpc != NULL) {
		if (mpc->dev_num == itf)
			return mpc;
135
		mpc = mpc->next;
L
Linus Torvalds 已提交
136 137 138 139 140 141 142 143
	}

	return NULL;   /* not found */
}

static struct mpoa_client *find_mpc_by_vcc(struct atm_vcc *vcc)
{
	struct mpoa_client *mpc;
144

L
Linus Torvalds 已提交
145 146 147 148 149 150 151 152 153 154 155 156 157
	mpc = mpcs;  /* our global linked list */
	while (mpc != NULL) {
		if (mpc->mpoad_vcc == vcc)
			return mpc;
		mpc = mpc->next;
	}

	return NULL;   /* not found */
}

static struct mpoa_client *find_mpc_by_lec(struct net_device *dev)
{
	struct mpoa_client *mpc;
158

L
Linus Torvalds 已提交
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
	mpc = mpcs;  /* our global linked list */
	while (mpc != NULL) {
		if (mpc->dev == dev)
			return mpc;
		mpc = mpc->next;
	}

	return NULL;   /* not found */
}

/*
 * Functions for managing QoS list
 */

/*
 * Overwrites the old entry or makes a new one.
 */
A
Al Viro 已提交
176
struct atm_mpoa_qos *atm_mpoa_add_qos(__be32 dst_ip, struct atm_qos *qos)
L
Linus Torvalds 已提交
177 178 179 180 181 182 183 184 185 186 187
{
	struct atm_mpoa_qos *entry;

	entry = atm_mpoa_search_qos(dst_ip);
	if (entry != NULL) {
		entry->qos = *qos;
		return entry;
	}

	entry = kmalloc(sizeof(struct atm_mpoa_qos), GFP_KERNEL);
	if (entry == NULL) {
J
Joe Perches 已提交
188
		pr_info("mpoa: out of memory\n");
L
Linus Torvalds 已提交
189 190 191 192 193 194 195 196 197 198 199 200
		return entry;
	}

	entry->ipaddr = dst_ip;
	entry->qos = *qos;

	entry->next = qos_head;
	qos_head = entry;

	return entry;
}

A
Al Viro 已提交
201
struct atm_mpoa_qos *atm_mpoa_search_qos(__be32 dst_ip)
L
Linus Torvalds 已提交
202 203 204 205
{
	struct atm_mpoa_qos *qos;

	qos = qos_head;
J
Joe Perches 已提交
206 207
	while (qos) {
		if (qos->ipaddr == dst_ip)
L
Linus Torvalds 已提交
208 209 210 211 212
			break;
		qos = qos->next;
	}

	return qos;
213
}
L
Linus Torvalds 已提交
214 215 216 217 218 219 220 221

/*
 * Returns 0 for failure
 */
int atm_mpoa_delete_qos(struct atm_mpoa_qos *entry)
{
	struct atm_mpoa_qos *curr;

J
Joe Perches 已提交
222 223
	if (entry == NULL)
		return 0;
L
Linus Torvalds 已提交
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
	if (entry == qos_head) {
		qos_head = qos_head->next;
		kfree(entry);
		return 1;
	}

	curr = qos_head;
	while (curr != NULL) {
		if (curr->next == entry) {
			curr->next = entry->next;
			kfree(entry);
			return 1;
		}
		curr = curr->next;
	}

	return 0;
}

/* this is buggered - we need locking for qos_head */
void atm_mpoa_disp_qos(struct seq_file *m)
{
	struct atm_mpoa_qos *qos;

	qos = qos_head;
	seq_printf(m, "QoS entries for shortcuts:\n");
	seq_printf(m, "IP address\n  TX:max_pcr pcr     min_pcr max_cdv max_sdu\n  RX:max_pcr pcr     min_pcr max_cdv max_sdu\n");

	while (qos != NULL) {
H
Harvey Harrison 已提交
253
		seq_printf(m, "%pI4\n     %-7d %-7d %-7d %-7d %-7d\n     %-7d %-7d %-7d %-7d %-7d\n",
J
Joe Perches 已提交
254 255 256 257 258 259 260 261 262 263 264
			   &qos->ipaddr,
			   qos->qos.txtp.max_pcr,
			   qos->qos.txtp.pcr,
			   qos->qos.txtp.min_pcr,
			   qos->qos.txtp.max_cdv,
			   qos->qos.txtp.max_sdu,
			   qos->qos.rxtp.max_pcr,
			   qos->qos.rxtp.pcr,
			   qos->qos.rxtp.min_pcr,
			   qos->qos.rxtp.max_cdv,
			   qos->qos.rxtp.max_sdu);
L
Linus Torvalds 已提交
265 266 267 268 269 270 271 272 273 274
		qos = qos->next;
	}
}

static struct net_device *find_lec_by_itfnum(int itf)
{
	struct net_device *dev;
	char name[IFNAMSIZ];

	sprintf(name, "lec%d", itf);
275
	dev = dev_get_by_name(&init_net, name);
276

L
Linus Torvalds 已提交
277 278 279 280 281 282 283
	return dev;
}

static struct mpoa_client *alloc_mpc(void)
{
	struct mpoa_client *mpc;

J
Joe Perches 已提交
284
	mpc = kzalloc(sizeof(struct mpoa_client), GFP_KERNEL);
L
Linus Torvalds 已提交
285 286 287 288 289 290 291 292 293
	if (mpc == NULL)
		return NULL;
	rwlock_init(&mpc->ingress_lock);
	rwlock_init(&mpc->egress_lock);
	mpc->next = mpcs;
	atm_mpoa_init_cache(mpc);

	mpc->parameters.mpc_p1 = MPC_P1;
	mpc->parameters.mpc_p2 = MPC_P2;
J
Joe Perches 已提交
294
	memset(mpc->parameters.mpc_p3, 0, sizeof(mpc->parameters.mpc_p3));
L
Linus Torvalds 已提交
295
	mpc->parameters.mpc_p4 = MPC_P4;
296
	mpc->parameters.mpc_p5 = MPC_P5;
L
Linus Torvalds 已提交
297
	mpc->parameters.mpc_p6 = MPC_P6;
298

L
Linus Torvalds 已提交
299
	mpcs = mpc;
300

L
Linus Torvalds 已提交
301 302 303 304 305 306
	return mpc;
}

/*
 *
 * start_mpc() puts the MPC on line. All the packets destined
307
 * to the lec underneath us are now being monitored and
L
Linus Torvalds 已提交
308 309 310 311 312
 * shortcuts will be established.
 *
 */
static void start_mpc(struct mpoa_client *mpc, struct net_device *dev)
{
313

314
	dprintk("(%s)\n", mpc->dev->name);
315
	if (!dev->netdev_ops)
J
Joe Perches 已提交
316
		pr_info("(%s) not starting\n", dev->name);
317 318 319 320 321
	else {
		mpc->old_ops = dev->netdev_ops;
		mpc->new_ops = *mpc->old_ops;
		mpc->new_ops.ndo_start_xmit = mpc_send_packet;
		dev->netdev_ops = &mpc->new_ops;
L
Linus Torvalds 已提交
322 323 324 325 326
	}
}

static void stop_mpc(struct mpoa_client *mpc)
{
327
	struct net_device *dev = mpc->dev;
328
	dprintk("(%s)", mpc->dev->name);
L
Linus Torvalds 已提交
329 330

	/* Lets not nullify lec device's dev->hard_start_xmit */
331
	if (dev->netdev_ops != &mpc->new_ops) {
332
		dprintk_cont(" mpc already stopped, not fatal\n");
L
Linus Torvalds 已提交
333 334
		return;
	}
335
	dprintk_cont("\n");
336

337 338 339 340
	dev->netdev_ops = mpc->old_ops;
	mpc->old_ops = NULL;

	/* close_shortcuts(mpc);    ??? FIXME */
L
Linus Torvalds 已提交
341 342 343 344 345 346
}

static const char *mpoa_device_type_string(char type) __attribute__ ((unused));

static const char *mpoa_device_type_string(char type)
{
J
Joe Perches 已提交
347
	switch (type) {
L
Linus Torvalds 已提交
348 349 350 351 352 353 354 355 356 357
	case NON_MPOA:
		return "non-MPOA device";
	case MPS:
		return "MPS";
	case MPC:
		return "MPC";
	case MPS_AND_MPC:
		return "both MPS and MPC";
	}

J
Joe Perches 已提交
358
	return "unspecified (non-MPOA) device";
L
Linus Torvalds 已提交
359 360 361
}

/*
W
Wang Chen 已提交
362 363
 * lec device calls this via its netdev_priv(dev)->lane2_ops
 * ->associate_indicator() when it sees a TLV in LE_ARP packet.
L
Linus Torvalds 已提交
364 365 366 367 368 369 370 371 372 373
 * We fill in the pointer above when we see a LANE2 lec initializing
 * See LANE2 spec 3.1.5
 *
 * Quite a big and ugly function but when you look at it
 * all it does is to try to locate and parse MPOA Device
 * Type TLV.
 * We give our lec a pointer to this function and when the
 * lec sees a TLV it uses the pointer to call this function.
 *
 */
374 375
static void lane2_assoc_ind(struct net_device *dev, const u8 *mac_addr,
			    const u8 *tlvs, u32 sizeoftlvs)
L
Linus Torvalds 已提交
376 377 378
{
	uint32_t type;
	uint8_t length, mpoa_device_type, number_of_mps_macs;
379
	const uint8_t *end_of_tlvs;
L
Linus Torvalds 已提交
380
	struct mpoa_client *mpc;
381

L
Linus Torvalds 已提交
382
	mpoa_device_type = number_of_mps_macs = 0; /* silence gcc */
383
	dprintk("(%s) received TLV(s), ", dev->name);
L
Linus Torvalds 已提交
384 385 386
	dprintk("total length of all TLVs %d\n", sizeoftlvs);
	mpc = find_mpc_by_lec(dev); /* Sampo-Fix: moved here from below */
	if (mpc == NULL) {
J
Joe Perches 已提交
387
		pr_info("(%s) no mpc\n", dev->name);
L
Linus Torvalds 已提交
388 389 390 391
		return;
	}
	end_of_tlvs = tlvs + sizeoftlvs;
	while (end_of_tlvs - tlvs >= 5) {
J
Joe Perches 已提交
392 393
		type = ((tlvs[0] << 24) | (tlvs[1] << 16) |
			(tlvs[2] << 8) | tlvs[3]);
L
Linus Torvalds 已提交
394 395 396 397
		length = tlvs[4];
		tlvs += 5;
		dprintk("    type 0x%x length %02x\n", type, length);
		if (tlvs + length > end_of_tlvs) {
J
Joe Perches 已提交
398
			pr_info("TLV value extends past its buffer, aborting parse\n");
L
Linus Torvalds 已提交
399 400
			return;
		}
401

L
Linus Torvalds 已提交
402
		if (type == 0) {
J
Joe Perches 已提交
403 404
			pr_info("mpoa: (%s) TLV type was 0, returning\n",
				dev->name);
L
Linus Torvalds 已提交
405 406 407 408 409 410 411 412 413
			return;
		}

		if (type != TLV_MPOA_DEVICE_TYPE) {
			tlvs += length;
			continue;  /* skip other TLVs */
		}
		mpoa_device_type = *tlvs++;
		number_of_mps_macs = *tlvs++;
414
		dprintk("(%s) MPOA device type '%s', ",
J
Joe Perches 已提交
415
			dev->name, mpoa_device_type_string(mpoa_device_type));
L
Linus Torvalds 已提交
416 417
		if (mpoa_device_type == MPS_AND_MPC &&
		    length < (42 + number_of_mps_macs*ETH_ALEN)) { /* :) */
J
Joe Perches 已提交
418 419
			pr_info("(%s) short MPOA Device Type TLV\n",
				dev->name);
L
Linus Torvalds 已提交
420 421
			continue;
		}
J
Joe Perches 已提交
422 423 424
		if ((mpoa_device_type == MPS || mpoa_device_type == MPC) &&
		    length < 22 + number_of_mps_macs*ETH_ALEN) {
			pr_info("(%s) short MPOA Device Type TLV\n", dev->name);
L
Linus Torvalds 已提交
425 426
			continue;
		}
J
Joe Perches 已提交
427 428
		if (mpoa_device_type != MPS &&
		    mpoa_device_type != MPS_AND_MPC) {
429
			dprintk("ignoring non-MPS device ");
J
Joe Perches 已提交
430 431
			if (mpoa_device_type == MPC)
				tlvs += 20;
L
Linus Torvalds 已提交
432 433
			continue;  /* we are only interested in MPSs */
		}
J
Joe Perches 已提交
434 435 436
		if (number_of_mps_macs == 0 &&
		    mpoa_device_type == MPS_AND_MPC) {
			pr_info("(%s) MPS_AND_MPC has zero MACs\n", dev->name);
L
Linus Torvalds 已提交
437 438
			continue;  /* someone should read the spec */
		}
439 440
		dprintk_cont("this MPS has %d MAC addresses\n",
			     number_of_mps_macs);
441

J
Joe Perches 已提交
442 443 444 445
		/*
		 * ok, now we can go and tell our daemon
		 * the control address of MPS
		 */
L
Linus Torvalds 已提交
446
		send_set_mps_ctrl_addr(tlvs, mpc);
447

J
Joe Perches 已提交
448 449 450 451
		tlvs = copy_macs(mpc, mac_addr, tlvs,
				 number_of_mps_macs, mpoa_device_type);
		if (tlvs == NULL)
			return;
L
Linus Torvalds 已提交
452 453
	}
	if (end_of_tlvs - tlvs != 0)
454
		pr_info("(%s) ignoring %zd bytes of trailing TLV garbage\n",
J
Joe Perches 已提交
455
			dev->name, end_of_tlvs - tlvs);
L
Linus Torvalds 已提交
456 457 458 459 460 461 462
}

/*
 * Store at least advertizing router's MAC address
 * plus the possible MAC address(es) to mpc->mps_macs.
 * For a freshly allocated MPOA client mpc->mps_macs == 0.
 */
463 464 465 466
static const uint8_t *copy_macs(struct mpoa_client *mpc,
				const uint8_t *router_mac,
				const uint8_t *tlvs, uint8_t mps_macs,
				uint8_t device_type)
L
Linus Torvalds 已提交
467 468 469 470 471
{
	int num_macs;
	num_macs = (mps_macs > 1) ? mps_macs : 1;

	if (mpc->number_of_mps_macs != num_macs) { /* need to reallocate? */
J
Joe Perches 已提交
472 473
		if (mpc->number_of_mps_macs != 0)
			kfree(mpc->mps_macs);
L
Linus Torvalds 已提交
474
		mpc->number_of_mps_macs = 0;
J
Joe Perches 已提交
475
		mpc->mps_macs = kmalloc(num_macs * ETH_ALEN, GFP_KERNEL);
L
Linus Torvalds 已提交
476
		if (mpc->mps_macs == NULL) {
J
Joe Perches 已提交
477
			pr_info("(%s) out of mem\n", mpc->dev->name);
L
Linus Torvalds 已提交
478 479 480
			return NULL;
		}
	}
J
Joe Perches 已提交
481
	ether_addr_copy(mpc->mps_macs, router_mac);
L
Linus Torvalds 已提交
482 483 484 485 486 487 488 489 490 491 492 493 494 495
	tlvs += 20; if (device_type == MPS_AND_MPC) tlvs += 20;
	if (mps_macs > 0)
		memcpy(mpc->mps_macs, tlvs, mps_macs*ETH_ALEN);
	tlvs += mps_macs*ETH_ALEN;
	mpc->number_of_mps_macs = num_macs;

	return tlvs;
}

static int send_via_shortcut(struct sk_buff *skb, struct mpoa_client *mpc)
{
	in_cache_entry *entry;
	struct iphdr *iph;
	char *buff;
A
Al Viro 已提交
496
	__be32 ipaddr = 0;
L
Linus Torvalds 已提交
497 498 499

	static struct {
		struct llc_snap_hdr hdr;
A
Al Viro 已提交
500
		__be32 tag;
L
Linus Torvalds 已提交
501 502 503 504 505 506 507 508 509
	} tagged_llc_snap_hdr = {
		{0xaa, 0xaa, 0x03, {0x00, 0x00, 0x00}, {0x88, 0x4c}},
		0
	};

	buff = skb->data + mpc->dev->hard_header_len;
	iph = (struct iphdr *)buff;
	ipaddr = iph->daddr;

510
	ddprintk("(%s) ipaddr 0x%x\n",
J
Joe Perches 已提交
511
		 mpc->dev->name, ipaddr);
L
Linus Torvalds 已提交
512 513 514 515

	entry = mpc->in_ops->get(ipaddr, mpc);
	if (entry == NULL) {
		entry = mpc->in_ops->add_entry(ipaddr, mpc);
J
Joe Perches 已提交
516 517
		if (entry != NULL)
			mpc->in_ops->put(entry);
L
Linus Torvalds 已提交
518 519
		return 1;
	}
J
Joe Perches 已提交
520 521
	/* threshold not exceeded or VCC not ready */
	if (mpc->in_ops->cache_hit(entry, mpc) != OPEN) {
522
		ddprintk("(%s) cache_hit: returns != OPEN\n",
J
Joe Perches 已提交
523
			 mpc->dev->name);
L
Linus Torvalds 已提交
524 525 526 527
		mpc->in_ops->put(entry);
		return 1;
	}

528
	ddprintk("(%s) using shortcut\n",
J
Joe Perches 已提交
529
		 mpc->dev->name);
L
Linus Torvalds 已提交
530 531
	/* MPOA spec A.1.4, MPOA client must decrement IP ttl at least by one */
	if (iph->ttl <= 1) {
532
		ddprintk("(%s) IP ttl = %u, using LANE\n",
J
Joe Perches 已提交
533
			 mpc->dev->name, iph->ttl);
L
Linus Torvalds 已提交
534 535 536 537 538 539 540 541
		mpc->in_ops->put(entry);
		return 1;
	}
	iph->ttl--;
	iph->check = 0;
	iph->check = ip_fast_csum((unsigned char *)iph, iph->ihl);

	if (entry->ctrl_info.tag != 0) {
542
		ddprintk("(%s) adding tag 0x%x\n",
J
Joe Perches 已提交
543
			 mpc->dev->name, entry->ctrl_info.tag);
L
Linus Torvalds 已提交
544
		tagged_llc_snap_hdr.tag = entry->ctrl_info.tag;
J
Joe Perches 已提交
545 546 547
		skb_pull(skb, ETH_HLEN);	/* get rid of Eth header */
		skb_push(skb, sizeof(tagged_llc_snap_hdr));
						/* add LLC/SNAP header   */
548 549
		skb_copy_to_linear_data(skb, &tagged_llc_snap_hdr,
					sizeof(tagged_llc_snap_hdr));
L
Linus Torvalds 已提交
550
	} else {
J
Joe Perches 已提交
551 552 553
		skb_pull(skb, ETH_HLEN);	/* get rid of Eth header */
		skb_push(skb, sizeof(struct llc_snap_hdr));
						/* add LLC/SNAP header + tag  */
554 555
		skb_copy_to_linear_data(skb, &llc_snap_mpoa_data,
					sizeof(struct llc_snap_hdr));
L
Linus Torvalds 已提交
556 557
	}

558
	refcount_add(skb->truesize, &sk_atm(entry->shortcut)->sk_wmem_alloc);
L
Linus Torvalds 已提交
559 560 561 562 563 564 565 566 567 568 569
	ATM_SKB(skb)->atm_options = entry->shortcut->atm_options;
	entry->shortcut->send(entry->shortcut, skb);
	entry->packets_fwded++;
	mpc->in_ops->put(entry);

	return 0;
}

/*
 * Probably needs some error checks and locking, not sure...
 */
570 571
static netdev_tx_t mpc_send_packet(struct sk_buff *skb,
					 struct net_device *dev)
L
Linus Torvalds 已提交
572 573 574 575
{
	struct mpoa_client *mpc;
	struct ethhdr *eth;
	int i = 0;
576

L
Linus Torvalds 已提交
577
	mpc = find_mpc_by_lec(dev); /* this should NEVER fail */
J
Joe Perches 已提交
578 579
	if (mpc == NULL) {
		pr_info("(%s) no MPC found\n", dev->name);
L
Linus Torvalds 已提交
580 581 582 583 584 585 586
		goto non_ip;
	}

	eth = (struct ethhdr *)skb->data;
	if (eth->h_proto != htons(ETH_P_IP))
		goto non_ip; /* Multi-Protocol Over ATM :-) */

587 588 589 590 591 592 593
	/* Weed out funny packets (e.g., AF_PACKET or raw). */
	if (skb->len < ETH_HLEN + sizeof(struct iphdr))
		goto non_ip;
	skb_set_network_header(skb, ETH_HLEN);
	if (skb->len < ETH_HLEN + ip_hdr(skb)->ihl * 4 || ip_hdr(skb)->ihl < 5)
		goto non_ip;

L
Linus Torvalds 已提交
594
	while (i < mpc->number_of_mps_macs) {
595
		if (ether_addr_equal(eth->h_dest, mpc->mps_macs + i * ETH_ALEN))
J
Joe Perches 已提交
596 597
			if (send_via_shortcut(skb, mpc) == 0) /* try shortcut */
				return NETDEV_TX_OK;
L
Linus Torvalds 已提交
598 599 600
		i++;
	}

J
Joe Perches 已提交
601
non_ip:
602
	return __netdev_start_xmit(mpc->old_ops, skb, dev, false);
L
Linus Torvalds 已提交
603 604 605 606 607 608 609 610
}

static int atm_mpoa_vcc_attach(struct atm_vcc *vcc, void __user *arg)
{
	int bytes_left;
	struct mpoa_client *mpc;
	struct atmmpc_ioc ioc_data;
	in_cache_entry *in_entry;
A
Al Viro 已提交
611
	__be32  ipaddr;
L
Linus Torvalds 已提交
612 613 614

	bytes_left = copy_from_user(&ioc_data, arg, sizeof(struct atmmpc_ioc));
	if (bytes_left != 0) {
J
Joe Perches 已提交
615 616
		pr_info("mpoa:Short read (missed %d bytes) from userland\n",
			bytes_left);
L
Linus Torvalds 已提交
617 618 619 620 621
		return -EFAULT;
	}
	ipaddr = ioc_data.ipaddr;
	if (ioc_data.dev_num < 0 || ioc_data.dev_num >= MAX_LEC_ITF)
		return -EINVAL;
622

L
Linus Torvalds 已提交
623 624 625
	mpc = find_mpc_by_itfnum(ioc_data.dev_num);
	if (mpc == NULL)
		return -EINVAL;
626

L
Linus Torvalds 已提交
627 628
	if (ioc_data.type == MPC_SOCKET_INGRESS) {
		in_entry = mpc->in_ops->get(ipaddr, mpc);
J
Joe Perches 已提交
629 630 631
		if (in_entry == NULL ||
		    in_entry->entry_state < INGRESS_RESOLVED) {
			pr_info("(%s) did not find RESOLVED entry from ingress cache\n",
L
Linus Torvalds 已提交
632
				mpc->dev->name);
J
Joe Perches 已提交
633 634
			if (in_entry != NULL)
				mpc->in_ops->put(in_entry);
L
Linus Torvalds 已提交
635 636
			return -EINVAL;
		}
J
Joe Perches 已提交
637 638
		pr_info("(%s) attaching ingress SVC, entry = %pI4\n",
			mpc->dev->name, &in_entry->ctrl_info.in_dst_ip);
L
Linus Torvalds 已提交
639 640 641
		in_entry->shortcut = vcc;
		mpc->in_ops->put(in_entry);
	} else {
J
Joe Perches 已提交
642
		pr_info("(%s) attaching egress SVC\n", mpc->dev->name);
L
Linus Torvalds 已提交
643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658
	}

	vcc->proto_data = mpc->dev;
	vcc->push = mpc_push;

	return 0;
}

/*
 *
 */
static void mpc_vcc_close(struct atm_vcc *vcc, struct net_device *dev)
{
	struct mpoa_client *mpc;
	in_cache_entry *in_entry;
	eg_cache_entry *eg_entry;
659

L
Linus Torvalds 已提交
660 661
	mpc = find_mpc_by_lec(dev);
	if (mpc == NULL) {
J
Joe Perches 已提交
662
		pr_info("(%s) close for unknown MPC\n", dev->name);
L
Linus Torvalds 已提交
663 664 665
		return;
	}

666
	dprintk("(%s)\n", dev->name);
L
Linus Torvalds 已提交
667 668
	in_entry = mpc->in_ops->get_by_vcc(vcc, mpc);
	if (in_entry) {
669
		dprintk("(%s) ingress SVC closed ip = %pI4\n",
J
Joe Perches 已提交
670
			mpc->dev->name, &in_entry->ctrl_info.in_dst_ip);
L
Linus Torvalds 已提交
671 672 673 674 675
		in_entry->shortcut = NULL;
		mpc->in_ops->put(in_entry);
	}
	eg_entry = mpc->eg_ops->get_by_vcc(vcc, mpc);
	if (eg_entry) {
676
		dprintk("(%s) egress SVC closed\n", mpc->dev->name);
L
Linus Torvalds 已提交
677 678 679 680 681
		eg_entry->shortcut = NULL;
		mpc->eg_ops->put(eg_entry);
	}

	if (in_entry == NULL && eg_entry == NULL)
682
		dprintk("(%s) unused vcc closed\n", dev->name);
L
Linus Torvalds 已提交
683 684 685 686 687 688 689 690
}

static void mpc_push(struct atm_vcc *vcc, struct sk_buff *skb)
{
	struct net_device *dev = (struct net_device *)vcc->proto_data;
	struct sk_buff *new_skb;
	eg_cache_entry *eg;
	struct mpoa_client *mpc;
A
Al Viro 已提交
691
	__be32 tag;
L
Linus Torvalds 已提交
692
	char *tmp;
693

694
	ddprintk("(%s)\n", dev->name);
L
Linus Torvalds 已提交
695
	if (skb == NULL) {
696
		dprintk("(%s) null skb, closing VCC\n", dev->name);
L
Linus Torvalds 已提交
697 698 699
		mpc_vcc_close(vcc, dev);
		return;
	}
700

L
Linus Torvalds 已提交
701
	skb->dev = dev;
J
Joe Perches 已提交
702 703
	if (memcmp(skb->data, &llc_snap_mpoa_ctrl,
		   sizeof(struct llc_snap_hdr)) == 0) {
L
Linus Torvalds 已提交
704 705
		struct sock *sk = sk_atm(vcc);

706
		dprintk("(%s) control packet arrived\n", dev->name);
L
Linus Torvalds 已提交
707 708
		/* Pass control packets to daemon */
		skb_queue_tail(&sk->sk_receive_queue, skb);
709
		sk->sk_data_ready(sk);
L
Linus Torvalds 已提交
710 711 712 713 714 715 716 717
		return;
	}

	/* data coming over the shortcut */
	atm_return(vcc, skb->truesize);

	mpc = find_mpc_by_lec(dev);
	if (mpc == NULL) {
J
Joe Perches 已提交
718
		pr_info("(%s) unknown MPC\n", dev->name);
L
Linus Torvalds 已提交
719 720 721
		return;
	}

J
Joe Perches 已提交
722 723
	if (memcmp(skb->data, &llc_snap_mpoa_data_tagged,
		   sizeof(struct llc_snap_hdr)) == 0) { /* MPOA tagged data */
724
		ddprintk("(%s) tagged data packet arrived\n", dev->name);
L
Linus Torvalds 已提交
725

J
Joe Perches 已提交
726 727 728 729
	} else if (memcmp(skb->data, &llc_snap_mpoa_data,
			  sizeof(struct llc_snap_hdr)) == 0) { /* MPOA data */
		pr_info("(%s) Unsupported non-tagged data packet arrived.  Purging\n",
			dev->name);
L
Linus Torvalds 已提交
730 731 732
		dev_kfree_skb_any(skb);
		return;
	} else {
J
Joe Perches 已提交
733
		pr_info("(%s) garbage arrived, purging\n", dev->name);
L
Linus Torvalds 已提交
734 735 736 737 738
		dev_kfree_skb_any(skb);
		return;
	}

	tmp = skb->data + sizeof(struct llc_snap_hdr);
A
Al Viro 已提交
739
	tag = *(__be32 *)tmp;
L
Linus Torvalds 已提交
740 741 742

	eg = mpc->eg_ops->get_by_tag(tag, mpc);
	if (eg == NULL) {
J
Joe Perches 已提交
743 744
		pr_info("mpoa: (%s) Didn't find egress cache entry, tag = %u\n",
			dev->name, tag);
L
Linus Torvalds 已提交
745 746 747 748
		purge_egress_shortcut(vcc, NULL);
		dev_kfree_skb_any(skb);
		return;
	}
749

L
Linus Torvalds 已提交
750 751 752
	/*
	 * See if ingress MPC is using shortcut we opened as a return channel.
	 * This means we have a bi-directional vcc opened by us.
753
	 */
L
Linus Torvalds 已提交
754 755
	if (eg->shortcut == NULL) {
		eg->shortcut = vcc;
J
Joe Perches 已提交
756
		pr_info("(%s) egress SVC in use\n", dev->name);
L
Linus Torvalds 已提交
757 758
	}

J
Joe Perches 已提交
759 760 761 762
	skb_pull(skb, sizeof(struct llc_snap_hdr) + sizeof(tag));
					/* get rid of LLC/SNAP header */
	new_skb = skb_realloc_headroom(skb, eg->ctrl_info.DH_length);
					/* LLC/SNAP is shorter than MAC header :( */
L
Linus Torvalds 已提交
763
	dev_kfree_skb_any(skb);
J
Joe Perches 已提交
764
	if (new_skb == NULL) {
L
Linus Torvalds 已提交
765 766 767 768
		mpc->eg_ops->put(eg);
		return;
	}
	skb_push(new_skb, eg->ctrl_info.DH_length);     /* add MAC header */
769 770
	skb_copy_to_linear_data(new_skb, eg->ctrl_info.DLL_header,
				eg->ctrl_info.DH_length);
L
Linus Torvalds 已提交
771
	new_skb->protocol = eth_type_trans(new_skb, dev);
772
	skb_reset_network_header(new_skb);
L
Linus Torvalds 已提交
773

774
	eg->latest_ip_addr = ip_hdr(new_skb)->saddr;
L
Linus Torvalds 已提交
775 776 777
	eg->packets_rcvd++;
	mpc->eg_ops->put(eg);

J
Jiri Slaby 已提交
778
	memset(ATM_SKB(new_skb), 0, sizeof(struct atm_skb_data));
L
Linus Torvalds 已提交
779 780 781
	netif_rx(new_skb);
}

B
Bhumika Goyal 已提交
782
static const struct atmdev_ops mpc_ops = { /* only send is required */
L
Linus Torvalds 已提交
783 784 785 786 787 788 789 790
	.close	= mpoad_close,
	.send	= msg_from_mpoad
};

static struct atm_dev mpc_dev = {
	.ops	= &mpc_ops,
	.type	= "mpc",
	.number	= 42,
791
	.lock	= __SPIN_LOCK_UNLOCKED(mpc_dev.lock)
L
Linus Torvalds 已提交
792 793 794
	/* members not explicitly initialised will be 0 */
};

J
Joe Perches 已提交
795
static int atm_mpoa_mpoad_attach(struct atm_vcc *vcc, int arg)
L
Linus Torvalds 已提交
796 797 798 799
{
	struct mpoa_client *mpc;
	struct lec_priv *priv;
	int err;
800

L
Linus Torvalds 已提交
801 802 803 804 805 806 807 808 809 810 811
	if (mpcs == NULL) {
		init_timer(&mpc_timer);
		mpc_timer_refresh();

		/* This lets us now how our LECs are doing */
		err = register_netdevice_notifier(&mpoa_notifier);
		if (err < 0) {
			del_timer(&mpc_timer);
			return err;
		}
	}
812

L
Linus Torvalds 已提交
813 814
	mpc = find_mpc_by_itfnum(arg);
	if (mpc == NULL) {
815
		dprintk("allocating new mpc for itf %d\n", arg);
L
Linus Torvalds 已提交
816 817 818 819
		mpc = alloc_mpc();
		if (mpc == NULL)
			return -ENOMEM;
		mpc->dev_num = arg;
J
Joe Perches 已提交
820 821
		mpc->dev = find_lec_by_itfnum(arg);
					/* NULL if there was no lec */
L
Linus Torvalds 已提交
822 823
	}
	if (mpc->mpoad_vcc) {
J
Joe Perches 已提交
824
		pr_info("mpoad is already present for itf %d\n", arg);
L
Linus Torvalds 已提交
825 826 827 828
		return -EADDRINUSE;
	}

	if (mpc->dev) { /* check if the lec is LANE2 capable */
829
		priv = netdev_priv(mpc->dev);
L
Linus Torvalds 已提交
830 831 832 833
		if (priv->lane_version < 2) {
			dev_put(mpc->dev);
			mpc->dev = NULL;
		} else
834
			priv->lane2_ops->associate_indicator = lane2_assoc_ind;
L
Linus Torvalds 已提交
835 836 837 838 839
	}

	mpc->mpoad_vcc = vcc;
	vcc->dev = &mpc_dev;
	vcc_insert_socket(sk_atm(vcc));
J
Joe Perches 已提交
840 841
	set_bit(ATM_VF_META, &vcc->flags);
	set_bit(ATM_VF_READY, &vcc->flags);
L
Linus Torvalds 已提交
842 843 844 845

	if (mpc->dev) {
		char empty[ATM_ESA_LEN];
		memset(empty, 0, ATM_ESA_LEN);
846

L
Linus Torvalds 已提交
847 848 849 850
		start_mpc(mpc, mpc->dev);
		/* set address if mpcd e.g. gets killed and restarted.
		 * If we do not do it now we have to wait for the next LE_ARP
		 */
J
Joe Perches 已提交
851
		if (memcmp(mpc->mps_ctrl_addr, empty, ATM_ESA_LEN) != 0)
L
Linus Torvalds 已提交
852 853 854 855 856 857 858
			send_set_mps_ctrl_addr(mpc->mps_ctrl_addr, mpc);
	}

	__module_get(THIS_MODULE);
	return arg;
}

859
static void send_set_mps_ctrl_addr(const char *addr, struct mpoa_client *mpc)
L
Linus Torvalds 已提交
860 861 862
{
	struct k_message mesg;

J
Joe Perches 已提交
863
	memcpy(mpc->mps_ctrl_addr, addr, ATM_ESA_LEN);
864

L
Linus Torvalds 已提交
865 866 867 868 869 870 871 872 873 874 875 876
	mesg.type = SET_MPS_CTRL_ADDR;
	memcpy(mesg.MPS_ctrl, addr, ATM_ESA_LEN);
	msg_to_mpoad(&mesg, mpc);
}

static void mpoad_close(struct atm_vcc *vcc)
{
	struct mpoa_client *mpc;
	struct sk_buff *skb;

	mpc = find_mpc_by_vcc(vcc);
	if (mpc == NULL) {
J
Joe Perches 已提交
877
		pr_info("did not find MPC\n");
L
Linus Torvalds 已提交
878 879 880
		return;
	}
	if (!mpc->mpoad_vcc) {
J
Joe Perches 已提交
881
		pr_info("close for non-present mpoad\n");
L
Linus Torvalds 已提交
882 883
		return;
	}
884

L
Linus Torvalds 已提交
885 886
	mpc->mpoad_vcc = NULL;
	if (mpc->dev) {
887
		struct lec_priv *priv = netdev_priv(mpc->dev);
L
Linus Torvalds 已提交
888 889 890 891 892 893 894 895 896 897 898 899
		priv->lane2_ops->associate_indicator = NULL;
		stop_mpc(mpc);
		dev_put(mpc->dev);
	}

	mpc->in_ops->destroy_cache(mpc);
	mpc->eg_ops->destroy_cache(mpc);

	while ((skb = skb_dequeue(&sk_atm(vcc)->sk_receive_queue))) {
		atm_return(vcc, skb->truesize);
		kfree_skb(skb);
	}
900

J
Joe Perches 已提交
901
	pr_info("(%s) going down\n",
L
Linus Torvalds 已提交
902 903 904 905 906 907 908 909 910
		(mpc->dev) ? mpc->dev->name : "<unknown>");
	module_put(THIS_MODULE);
}

/*
 *
 */
static int msg_from_mpoad(struct atm_vcc *vcc, struct sk_buff *skb)
{
911

L
Linus Torvalds 已提交
912
	struct mpoa_client *mpc = find_mpc_by_vcc(vcc);
J
Joe Perches 已提交
913
	struct k_message *mesg = (struct k_message *)skb->data;
914
	WARN_ON(refcount_sub_and_test(skb->truesize, &sk_atm(vcc)->sk_wmem_alloc));
915

L
Linus Torvalds 已提交
916
	if (mpc == NULL) {
J
Joe Perches 已提交
917
		pr_info("no mpc found\n");
L
Linus Torvalds 已提交
918 919
		return 0;
	}
920
	dprintk("(%s)", mpc->dev ? mpc->dev->name : "<unknown>");
J
Joe Perches 已提交
921
	switch (mesg->type) {
L
Linus Torvalds 已提交
922
	case MPOA_RES_REPLY_RCVD:
923
		dprintk_cont("mpoa_res_reply_rcvd\n");
L
Linus Torvalds 已提交
924 925 926
		MPOA_res_reply_rcvd(mesg, mpc);
		break;
	case MPOA_TRIGGER_RCVD:
927
		dprintk_cont("mpoa_trigger_rcvd\n");
L
Linus Torvalds 已提交
928 929 930
		MPOA_trigger_rcvd(mesg, mpc);
		break;
	case INGRESS_PURGE_RCVD:
931
		dprintk_cont("nhrp_purge_rcvd\n");
L
Linus Torvalds 已提交
932 933 934
		ingress_purge_rcvd(mesg, mpc);
		break;
	case EGRESS_PURGE_RCVD:
935
		dprintk_cont("egress_purge_reply_rcvd\n");
L
Linus Torvalds 已提交
936 937 938
		egress_purge_rcvd(mesg, mpc);
		break;
	case MPS_DEATH:
939
		dprintk_cont("mps_death\n");
L
Linus Torvalds 已提交
940 941 942
		mps_death(mesg, mpc);
		break;
	case CACHE_IMPOS_RCVD:
943
		dprintk_cont("cache_impos_rcvd\n");
L
Linus Torvalds 已提交
944 945 946
		MPOA_cache_impos_rcvd(mesg, mpc);
		break;
	case SET_MPC_CTRL_ADDR:
947
		dprintk_cont("set_mpc_ctrl_addr\n");
L
Linus Torvalds 已提交
948 949 950
		set_mpc_ctrl_addr_rcvd(mesg, mpc);
		break;
	case SET_MPS_MAC_ADDR:
951
		dprintk_cont("set_mps_mac_addr\n");
L
Linus Torvalds 已提交
952 953 954
		set_mps_mac_addr_rcvd(mesg, mpc);
		break;
	case CLEAN_UP_AND_EXIT:
955
		dprintk_cont("clean_up_and_exit\n");
L
Linus Torvalds 已提交
956 957 958
		clean_up(mesg, mpc, DIE);
		break;
	case RELOAD:
959
		dprintk_cont("reload\n");
L
Linus Torvalds 已提交
960 961 962
		clean_up(mesg, mpc, RELOAD);
		break;
	case SET_MPC_PARAMS:
963
		dprintk_cont("set_mpc_params\n");
L
Linus Torvalds 已提交
964 965 966
		mpc->parameters = mesg->content.params;
		break;
	default:
967
		dprintk_cont("unknown message %d\n", mesg->type);
L
Linus Torvalds 已提交
968 969 970 971 972 973 974 975 976 977 978 979 980 981
		break;
	}
	kfree_skb(skb);

	return 0;
}

/* Remember that this function may not do things that sleep */
int msg_to_mpoad(struct k_message *mesg, struct mpoa_client *mpc)
{
	struct sk_buff *skb;
	struct sock *sk;

	if (mpc == NULL || !mpc->mpoad_vcc) {
J
Joe Perches 已提交
982
		pr_info("mesg %d to a non-existent mpoad\n", mesg->type);
L
Linus Torvalds 已提交
983 984 985 986 987 988 989
		return -ENXIO;
	}

	skb = alloc_skb(sizeof(struct k_message), GFP_ATOMIC);
	if (skb == NULL)
		return -ENOMEM;
	skb_put(skb, sizeof(struct k_message));
990
	skb_copy_to_linear_data(skb, mesg, sizeof(*mesg));
L
Linus Torvalds 已提交
991
	atm_force_charge(mpc->mpoad_vcc, skb->truesize);
992

L
Linus Torvalds 已提交
993 994
	sk = sk_atm(mpc->mpoad_vcc);
	skb_queue_tail(&sk->sk_receive_queue, skb);
995
	sk->sk_data_ready(sk);
L
Linus Torvalds 已提交
996 997 998 999

	return 0;
}

J
Joe Perches 已提交
1000
static int mpoa_event_listener(struct notifier_block *mpoa_notifier,
1001
			       unsigned long event, void *ptr)
L
Linus Torvalds 已提交
1002
{
1003
	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
L
Linus Torvalds 已提交
1004 1005 1006
	struct mpoa_client *mpc;
	struct lec_priv *priv;

1007
	if (!net_eq(dev_net(dev), &init_net))
1008 1009
		return NOTIFY_DONE;

1010
	if (strncmp(dev->name, "lec", 3))
L
Linus Torvalds 已提交
1011
		return NOTIFY_DONE; /* we are only interested in lec:s */
1012

L
Linus Torvalds 已提交
1013 1014
	switch (event) {
	case NETDEV_REGISTER:       /* a new lec device was allocated */
1015
		priv = netdev_priv(dev);
L
Linus Torvalds 已提交
1016 1017 1018 1019 1020
		if (priv->lane_version < 2)
			break;
		priv->lane2_ops->associate_indicator = lane2_assoc_ind;
		mpc = find_mpc_by_itfnum(priv->itfnum);
		if (mpc == NULL) {
1021
			dprintk("allocating new mpc for %s\n", dev->name);
L
Linus Torvalds 已提交
1022 1023
			mpc = alloc_mpc();
			if (mpc == NULL) {
J
Joe Perches 已提交
1024
				pr_info("no new mpc");
L
Linus Torvalds 已提交
1025 1026 1027 1028 1029 1030
				break;
			}
		}
		mpc->dev_num = priv->itfnum;
		mpc->dev = dev;
		dev_hold(dev);
1031
		dprintk("(%s) was initialized\n", dev->name);
L
Linus Torvalds 已提交
1032 1033 1034 1035 1036 1037
		break;
	case NETDEV_UNREGISTER:
		/* the lec device was deallocated */
		mpc = find_mpc_by_lec(dev);
		if (mpc == NULL)
			break;
1038
		dprintk("device (%s) was deallocated\n", dev->name);
L
Linus Torvalds 已提交
1039 1040 1041 1042 1043 1044 1045 1046 1047
		stop_mpc(mpc);
		dev_put(mpc->dev);
		mpc->dev = NULL;
		break;
	case NETDEV_UP:
		/* the dev was ifconfig'ed up */
		mpc = find_mpc_by_lec(dev);
		if (mpc == NULL)
			break;
J
Joe Perches 已提交
1048
		if (mpc->mpoad_vcc != NULL)
L
Linus Torvalds 已提交
1049 1050 1051 1052 1053 1054 1055 1056 1057 1058
			start_mpc(mpc, dev);
		break;
	case NETDEV_DOWN:
		/* the dev was ifconfig'ed down */
		/* this means that the flow of packets from the
		 * upper layer stops
		 */
		mpc = find_mpc_by_lec(dev);
		if (mpc == NULL)
			break;
J
Joe Perches 已提交
1059
		if (mpc->mpoad_vcc != NULL)
L
Linus Torvalds 已提交
1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082
			stop_mpc(mpc);
		break;
	case NETDEV_REBOOT:
	case NETDEV_CHANGE:
	case NETDEV_CHANGEMTU:
	case NETDEV_CHANGEADDR:
	case NETDEV_GOING_DOWN:
		break;
	default:
		break;
	}

	return NOTIFY_DONE;
}

/*
 * Functions which are called after a message is received from mpcd.
 * Msg is reused on purpose.
 */


static void MPOA_trigger_rcvd(struct k_message *msg, struct mpoa_client *mpc)
{
A
Al Viro 已提交
1083
	__be32 dst_ip = msg->content.in_info.in_dst_ip;
L
Linus Torvalds 已提交
1084 1085 1086
	in_cache_entry *entry;

	entry = mpc->in_ops->get(dst_ip, mpc);
J
Joe Perches 已提交
1087
	if (entry == NULL) {
L
Linus Torvalds 已提交
1088 1089 1090 1091 1092 1093 1094 1095 1096
		entry = mpc->in_ops->add_entry(dst_ip, mpc);
		entry->entry_state = INGRESS_RESOLVING;
		msg->type = SND_MPOA_RES_RQST;
		msg->content.in_info = entry->ctrl_info;
		msg_to_mpoad(msg, mpc);
		do_gettimeofday(&(entry->reply_wait));
		mpc->in_ops->put(entry);
		return;
	}
1097

J
Joe Perches 已提交
1098
	if (entry->entry_state == INGRESS_INVALID) {
L
Linus Torvalds 已提交
1099 1100 1101 1102 1103 1104 1105 1106
		entry->entry_state = INGRESS_RESOLVING;
		msg->type = SND_MPOA_RES_RQST;
		msg->content.in_info = entry->ctrl_info;
		msg_to_mpoad(msg, mpc);
		do_gettimeofday(&(entry->reply_wait));
		mpc->in_ops->put(entry);
		return;
	}
1107

J
Joe Perches 已提交
1108
	pr_info("(%s) entry already in resolving state\n",
L
Linus Torvalds 已提交
1109 1110 1111 1112 1113 1114
		(mpc->dev) ? mpc->dev->name : "<unknown>");
	mpc->in_ops->put(entry);
}

/*
 * Things get complicated because we have to check if there's an egress
1115
 * shortcut with suitable traffic parameters we could use.
L
Linus Torvalds 已提交
1116
 */
J
Joe Perches 已提交
1117 1118 1119
static void check_qos_and_open_shortcut(struct k_message *msg,
					struct mpoa_client *client,
					in_cache_entry *entry)
L
Linus Torvalds 已提交
1120
{
A
Al Viro 已提交
1121
	__be32 dst_ip = msg->content.in_info.in_dst_ip;
L
Linus Torvalds 已提交
1122 1123 1124
	struct atm_mpoa_qos *qos = atm_mpoa_search_qos(dst_ip);
	eg_cache_entry *eg_entry = client->eg_ops->get_by_src_ip(dst_ip, client);

J
Joe Perches 已提交
1125 1126 1127 1128 1129 1130 1131 1132
	if (eg_entry && eg_entry->shortcut) {
		if (eg_entry->shortcut->qos.txtp.traffic_class &
		    msg->qos.txtp.traffic_class &
		    (qos ? qos->qos.txtp.traffic_class : ATM_UBR | ATM_CBR)) {
			if (eg_entry->shortcut->qos.txtp.traffic_class == ATM_UBR)
				entry->shortcut = eg_entry->shortcut;
			else if (eg_entry->shortcut->qos.txtp.max_pcr > 0)
				entry->shortcut = eg_entry->shortcut;
L
Linus Torvalds 已提交
1133
		}
J
Joe Perches 已提交
1134
		if (entry->shortcut) {
1135
			dprintk("(%s) using egress SVC to reach %pI4\n",
H
Harvey Harrison 已提交
1136
				client->dev->name, &dst_ip);
L
Linus Torvalds 已提交
1137 1138 1139 1140 1141 1142 1143 1144 1145
			client->eg_ops->put(eg_entry);
			return;
		}
	}
	if (eg_entry != NULL)
		client->eg_ops->put(eg_entry);

	/* No luck in the egress cache we must open an ingress SVC */
	msg->type = OPEN_INGRESS_SVC;
J
Joe Perches 已提交
1146 1147
	if (qos &&
	    (qos->qos.txtp.traffic_class == msg->qos.txtp.traffic_class)) {
L
Linus Torvalds 已提交
1148
		msg->qos = qos->qos;
J
Joe Perches 已提交
1149 1150 1151 1152
		pr_info("(%s) trying to get a CBR shortcut\n",
			client->dev->name);
	} else
		memset(&msg->qos, 0, sizeof(struct atm_qos));
L
Linus Torvalds 已提交
1153 1154 1155 1156 1157
	msg_to_mpoad(msg, client);
}

static void MPOA_res_reply_rcvd(struct k_message *msg, struct mpoa_client *mpc)
{
A
Al Viro 已提交
1158
	__be32 dst_ip = msg->content.in_info.in_dst_ip;
L
Linus Torvalds 已提交
1159
	in_cache_entry *entry = mpc->in_ops->get(dst_ip, mpc);
A
Andrew Morton 已提交
1160

1161
	dprintk("(%s) ip %pI4\n",
H
Harvey Harrison 已提交
1162
		mpc->dev->name, &dst_ip);
1163
	ddprintk("(%s) entry = %p",
J
Joe Perches 已提交
1164 1165 1166 1167
		 mpc->dev->name, entry);
	if (entry == NULL) {
		pr_info("(%s) ARGH, received res. reply for an entry that doesn't exist.\n",
			mpc->dev->name);
L
Linus Torvalds 已提交
1168 1169
		return;
	}
1170
	ddprintk_cont(" entry_state = %d ", entry->entry_state);
L
Linus Torvalds 已提交
1171 1172

	if (entry->entry_state == INGRESS_RESOLVED) {
J
Joe Perches 已提交
1173
		pr_info("(%s) RESOLVED entry!\n", mpc->dev->name);
L
Linus Torvalds 已提交
1174 1175 1176 1177 1178 1179 1180 1181
		mpc->in_ops->put(entry);
		return;
	}

	entry->ctrl_info = msg->content.in_info;
	do_gettimeofday(&(entry->tv));
	do_gettimeofday(&(entry->reply_wait)); /* Used in refreshing func from now on */
	entry->refresh_time = 0;
1182
	ddprintk_cont("entry->shortcut = %p\n", entry->shortcut);
L
Linus Torvalds 已提交
1183

J
Joe Perches 已提交
1184 1185
	if (entry->entry_state == INGRESS_RESOLVING &&
	    entry->shortcut != NULL) {
1186
		entry->entry_state = INGRESS_RESOLVED;
L
Linus Torvalds 已提交
1187 1188 1189 1190 1191
		mpc->in_ops->put(entry);
		return; /* Shortcut already open... */
	}

	if (entry->shortcut != NULL) {
J
Joe Perches 已提交
1192 1193
		pr_info("(%s) entry->shortcut != NULL, impossible!\n",
			mpc->dev->name);
L
Linus Torvalds 已提交
1194 1195 1196
		mpc->in_ops->put(entry);
		return;
	}
1197

L
Linus Torvalds 已提交
1198 1199 1200 1201 1202 1203 1204 1205 1206 1207
	check_qos_and_open_shortcut(msg, mpc, entry);
	entry->entry_state = INGRESS_RESOLVED;
	mpc->in_ops->put(entry);

	return;

}

static void ingress_purge_rcvd(struct k_message *msg, struct mpoa_client *mpc)
{
A
Al Viro 已提交
1208 1209
	__be32 dst_ip = msg->content.in_info.in_dst_ip;
	__be32 mask = msg->ip_mask;
L
Linus Torvalds 已提交
1210 1211
	in_cache_entry *entry = mpc->in_ops->get_with_mask(dst_ip, mpc, mask);

J
Joe Perches 已提交
1212 1213 1214
	if (entry == NULL) {
		pr_info("(%s) purge for a non-existing entry, ip = %pI4\n",
			mpc->dev->name, &dst_ip);
L
Linus Torvalds 已提交
1215 1216 1217 1218
		return;
	}

	do {
1219
		dprintk("(%s) removing an ingress entry, ip = %pI4\n",
H
Harvey Harrison 已提交
1220
			mpc->dev->name, &dst_ip);
L
Linus Torvalds 已提交
1221 1222 1223 1224 1225 1226
		write_lock_bh(&mpc->ingress_lock);
		mpc->in_ops->remove_entry(entry, mpc);
		write_unlock_bh(&mpc->ingress_lock);
		mpc->in_ops->put(entry);
		entry = mpc->in_ops->get_with_mask(dst_ip, mpc, mask);
	} while (entry != NULL);
1227
}
L
Linus Torvalds 已提交
1228 1229 1230

static void egress_purge_rcvd(struct k_message *msg, struct mpoa_client *mpc)
{
A
Al Viro 已提交
1231
	__be32 cache_id = msg->content.eg_info.cache_id;
L
Linus Torvalds 已提交
1232
	eg_cache_entry *entry = mpc->eg_ops->get_by_cache_id(cache_id, mpc);
1233

L
Linus Torvalds 已提交
1234
	if (entry == NULL) {
1235
		dprintk("(%s) purge for a non-existing entry\n",
J
Joe Perches 已提交
1236
			mpc->dev->name);
L
Linus Torvalds 已提交
1237 1238 1239 1240 1241 1242 1243 1244
		return;
	}

	write_lock_irq(&mpc->egress_lock);
	mpc->eg_ops->remove_entry(entry, mpc);
	write_unlock_irq(&mpc->egress_lock);

	mpc->eg_ops->put(entry);
1245
}
L
Linus Torvalds 已提交
1246 1247 1248 1249 1250 1251 1252

static void purge_egress_shortcut(struct atm_vcc *vcc, eg_cache_entry *entry)
{
	struct sock *sk;
	struct k_message *purge_msg;
	struct sk_buff *skb;

1253
	dprintk("entering\n");
L
Linus Torvalds 已提交
1254
	if (vcc == NULL) {
J
Joe Perches 已提交
1255
		pr_info("vcc == NULL\n");
L
Linus Torvalds 已提交
1256 1257 1258 1259 1260
		return;
	}

	skb = alloc_skb(sizeof(struct k_message), GFP_ATOMIC);
	if (skb == NULL) {
J
Joe Perches 已提交
1261
		pr_info("out of memory\n");
L
Linus Torvalds 已提交
1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275
		return;
	}

	skb_put(skb, sizeof(struct k_message));
	memset(skb->data, 0, sizeof(struct k_message));
	purge_msg = (struct k_message *)skb->data;
	purge_msg->type = DATA_PLANE_PURGE;
	if (entry != NULL)
		purge_msg->content.eg_info = entry->ctrl_info;

	atm_force_charge(vcc, skb->truesize);

	sk = sk_atm(vcc);
	skb_queue_tail(&sk->sk_receive_queue, skb);
1276
	sk->sk_data_ready(sk);
1277
	dprintk("exiting\n");
L
Linus Torvalds 已提交
1278 1279 1280 1281 1282 1283
}

/*
 * Our MPS died. Tell our daemon to send NHRP data plane purge to each
 * of the egress shortcuts we have.
 */
J
Joe Perches 已提交
1284
static void mps_death(struct k_message *msg, struct mpoa_client *mpc)
L
Linus Torvalds 已提交
1285 1286 1287
{
	eg_cache_entry *entry;

1288
	dprintk("(%s)\n", mpc->dev->name);
L
Linus Torvalds 已提交
1289

J
Joe Perches 已提交
1290 1291
	if (memcmp(msg->MPS_ctrl, mpc->mps_ctrl_addr, ATM_ESA_LEN)) {
		pr_info("(%s) wrong MPS\n", mpc->dev->name);
L
Linus Torvalds 已提交
1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307
		return;
	}

	/* FIXME: This knows too much of the cache structure */
	read_lock_irq(&mpc->egress_lock);
	entry = mpc->eg_cache;
	while (entry != NULL) {
		purge_egress_shortcut(entry->shortcut, entry);
		entry = entry->next;
	}
	read_unlock_irq(&mpc->egress_lock);

	mpc->in_ops->destroy_cache(mpc);
	mpc->eg_ops->destroy_cache(mpc);
}

J
Joe Perches 已提交
1308 1309
static void MPOA_cache_impos_rcvd(struct k_message *msg,
				  struct mpoa_client *mpc)
L
Linus Torvalds 已提交
1310 1311 1312
{
	uint16_t holding_time;
	eg_cache_entry *entry = mpc->eg_ops->get_by_cache_id(msg->content.eg_info.cache_id, mpc);
1313

L
Linus Torvalds 已提交
1314
	holding_time = msg->content.eg_info.holding_time;
1315
	dprintk("(%s) entry = %p, holding_time = %u\n",
J
Joe Perches 已提交
1316 1317
		mpc->dev->name, entry, holding_time);
	if (entry == NULL && holding_time) {
L
Linus Torvalds 已提交
1318 1319 1320 1321
		entry = mpc->eg_ops->add_entry(msg, mpc);
		mpc->eg_ops->put(entry);
		return;
	}
J
Joe Perches 已提交
1322
	if (holding_time) {
L
Linus Torvalds 已提交
1323 1324 1325
		mpc->eg_ops->update(entry, holding_time);
		return;
	}
1326

L
Linus Torvalds 已提交
1327 1328 1329 1330 1331 1332 1333
	write_lock_irq(&mpc->egress_lock);
	mpc->eg_ops->remove_entry(entry, mpc);
	write_unlock_irq(&mpc->egress_lock);

	mpc->eg_ops->put(entry);
}

J
Joe Perches 已提交
1334 1335
static void set_mpc_ctrl_addr_rcvd(struct k_message *mesg,
				   struct mpoa_client *mpc)
L
Linus Torvalds 已提交
1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349
{
	struct lec_priv *priv;
	int i, retval ;

	uint8_t tlv[4 + 1 + 1 + 1 + ATM_ESA_LEN];

	tlv[0] = 00; tlv[1] = 0xa0; tlv[2] = 0x3e; tlv[3] = 0x2a; /* type  */
	tlv[4] = 1 + 1 + ATM_ESA_LEN;  /* length                           */
	tlv[5] = 0x02;                 /* MPOA client                      */
	tlv[6] = 0x00;                 /* number of MPS MAC addresses      */

	memcpy(&tlv[7], mesg->MPS_ctrl, ATM_ESA_LEN); /* MPC ctrl ATM addr */
	memcpy(mpc->our_ctrl_addr, mesg->MPS_ctrl, ATM_ESA_LEN);

1350 1351
	dprintk("(%s) setting MPC ctrl ATM address to",
		mpc->dev ? mpc->dev->name : "<unknown>");
L
Linus Torvalds 已提交
1352
	for (i = 7; i < sizeof(tlv); i++)
1353 1354
		dprintk_cont(" %02x", tlv[i]);
	dprintk_cont("\n");
L
Linus Torvalds 已提交
1355 1356

	if (mpc->dev) {
1357
		priv = netdev_priv(mpc->dev);
J
Joe Perches 已提交
1358 1359 1360
		retval = priv->lane2_ops->associate_req(mpc->dev,
							mpc->dev->dev_addr,
							tlv, sizeof(tlv));
L
Linus Torvalds 已提交
1361
		if (retval == 0)
J
Joe Perches 已提交
1362 1363
			pr_info("(%s) MPOA device type TLV association failed\n",
				mpc->dev->name);
L
Linus Torvalds 已提交
1364 1365
		retval = priv->lane2_ops->resolve(mpc->dev, NULL, 1, NULL, NULL);
		if (retval < 0)
J
Joe Perches 已提交
1366 1367
			pr_info("(%s) targetless LE_ARP request failed\n",
				mpc->dev->name);
L
Linus Torvalds 已提交
1368 1369 1370
	}
}

J
Joe Perches 已提交
1371 1372
static void set_mps_mac_addr_rcvd(struct k_message *msg,
				  struct mpoa_client *client)
L
Linus Torvalds 已提交
1373 1374
{

J
Joe Perches 已提交
1375
	if (client->number_of_mps_macs)
L
Linus Torvalds 已提交
1376 1377
		kfree(client->mps_macs);
	client->number_of_mps_macs = 0;
1378
	client->mps_macs = kmemdup(msg->MPS_ctrl, ETH_ALEN, GFP_KERNEL);
L
Linus Torvalds 已提交
1379
	if (client->mps_macs == NULL) {
J
Joe Perches 已提交
1380
		pr_info("out of memory\n");
L
Linus Torvalds 已提交
1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398
		return;
	}
	client->number_of_mps_macs = 1;
}

/*
 * purge egress cache and tell daemon to 'action' (DIE, RELOAD)
 */
static void clean_up(struct k_message *msg, struct mpoa_client *mpc, int action)
{

	eg_cache_entry *entry;
	msg->type = SND_EGRESS_PURGE;


	/* FIXME: This knows too much of the cache structure */
	read_lock_irq(&mpc->egress_lock);
	entry = mpc->eg_cache;
J
Joe Perches 已提交
1399 1400
	while (entry != NULL) {
		msg->content.eg_info = entry->ctrl_info;
1401
		dprintk("cache_id %u\n", entry->ctrl_info.cache_id);
J
Joe Perches 已提交
1402 1403
		msg_to_mpoad(msg, mpc);
		entry = entry->next;
L
Linus Torvalds 已提交
1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418
	}
	read_unlock_irq(&mpc->egress_lock);

	msg->type = action;
	msg_to_mpoad(msg, mpc);
}

static void mpc_timer_refresh(void)
{
	mpc_timer.expires = jiffies + (MPC_P2 * HZ);
	mpc_timer.data = mpc_timer.expires;
	mpc_timer.function = mpc_cache_check;
	add_timer(&mpc_timer);
}

J
Joe Perches 已提交
1419
static void mpc_cache_check(unsigned long checking_time)
L
Linus Torvalds 已提交
1420 1421 1422 1423
{
	struct mpoa_client *mpc = mpcs;
	static unsigned long previous_resolving_check_time;
	static unsigned long previous_refresh_time;
1424

J
Joe Perches 已提交
1425
	while (mpc != NULL) {
L
Linus Torvalds 已提交
1426 1427
		mpc->in_ops->clear_count(mpc);
		mpc->eg_ops->clear_expired(mpc);
J
Joe Perches 已提交
1428 1429
		if (checking_time - previous_resolving_check_time >
		    mpc->parameters.mpc_p4 * HZ) {
L
Linus Torvalds 已提交
1430 1431 1432
			mpc->in_ops->check_resolving(mpc);
			previous_resolving_check_time = checking_time;
		}
J
Joe Perches 已提交
1433 1434
		if (checking_time - previous_refresh_time >
		    mpc->parameters.mpc_p5 * HZ) {
L
Linus Torvalds 已提交
1435 1436 1437 1438 1439 1440 1441 1442
			mpc->in_ops->refresh(mpc);
			previous_refresh_time = checking_time;
		}
		mpc = mpc->next;
	}
	mpc_timer_refresh();
}

J
Joe Perches 已提交
1443 1444
static int atm_mpoa_ioctl(struct socket *sock, unsigned int cmd,
			  unsigned long arg)
L
Linus Torvalds 已提交
1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455
{
	int err = 0;
	struct atm_vcc *vcc = ATM_SD(sock);

	if (cmd != ATMMPC_CTRL && cmd != ATMMPC_DATA)
		return -ENOIOCTLCMD;

	if (!capable(CAP_NET_ADMIN))
		return -EPERM;

	switch (cmd) {
J
Joe Perches 已提交
1456 1457 1458 1459 1460 1461 1462 1463 1464 1465
	case ATMMPC_CTRL:
		err = atm_mpoa_mpoad_attach(vcc, (int)arg);
		if (err >= 0)
			sock->state = SS_CONNECTED;
		break;
	case ATMMPC_DATA:
		err = atm_mpoa_vcc_attach(vcc, (void __user *)arg);
		break;
	default:
		break;
L
Linus Torvalds 已提交
1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479
	}
	return err;
}

static struct atm_ioctl atm_ioctl_ops = {
	.owner	= THIS_MODULE,
	.ioctl	= atm_mpoa_ioctl,
};

static __init int atm_mpoa_init(void)
{
	register_atm_ioctl(&atm_ioctl_ops);

	if (mpc_proc_init() != 0)
1480
		pr_info("failed to initialize /proc/mpoa\n");
L
Linus Torvalds 已提交
1481

M
Michal Marek 已提交
1482
	pr_info("mpc.c: initialized\n");
L
Linus Torvalds 已提交
1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494

	return 0;
}

static void __exit atm_mpoa_cleanup(void)
{
	struct mpoa_client *mpc, *tmp;
	struct atm_mpoa_qos *qos, *nextqos;
	struct lec_priv *priv;

	mpc_proc_clean();

1495
	del_timer_sync(&mpc_timer);
L
Linus Torvalds 已提交
1496 1497 1498 1499 1500 1501 1502 1503 1504
	unregister_netdevice_notifier(&mpoa_notifier);
	deregister_atm_ioctl(&atm_ioctl_ops);

	mpc = mpcs;
	mpcs = NULL;
	while (mpc != NULL) {
		tmp = mpc->next;
		if (mpc->dev != NULL) {
			stop_mpc(mpc);
1505
			priv = netdev_priv(mpc->dev);
L
Linus Torvalds 已提交
1506 1507 1508
			if (priv->lane2_ops != NULL)
				priv->lane2_ops->associate_indicator = NULL;
		}
1509
		ddprintk("about to clear caches\n");
L
Linus Torvalds 已提交
1510 1511
		mpc->in_ops->destroy_cache(mpc);
		mpc->eg_ops->destroy_cache(mpc);
1512
		ddprintk("caches cleared\n");
L
Linus Torvalds 已提交
1513 1514
		kfree(mpc->mps_macs);
		memset(mpc, 0, sizeof(struct mpoa_client));
1515
		ddprintk("about to kfree %p\n", mpc);
L
Linus Torvalds 已提交
1516
		kfree(mpc);
1517
		ddprintk("next mpc is at %p\n", tmp);
L
Linus Torvalds 已提交
1518 1519 1520 1521 1522 1523 1524
		mpc = tmp;
	}

	qos = qos_head;
	qos_head = NULL;
	while (qos != NULL) {
		nextqos = qos->next;
1525
		dprintk("freeing qos entry %p\n", qos);
L
Linus Torvalds 已提交
1526 1527 1528 1529 1530 1531 1532 1533 1534
		kfree(qos);
		qos = nextqos;
	}
}

module_init(atm_mpoa_init);
module_exit(atm_mpoa_cleanup);

MODULE_LICENSE("GPL");