lec.c 59.9 KB
Newer Older
L
Linus Torvalds 已提交
1
/*
2
 * lec.c: Lan Emulation driver
L
Linus Torvalds 已提交
3
 *
4
 * Marko Kiiskila <mkiiskila@yahoo.com>
L
Linus Torvalds 已提交
5 6
 */

7 8
#define pr_fmt(fmt) KBUILD_MODNAME ":%s: " fmt, __func__

9
#include <linux/slab.h>
L
Linus Torvalds 已提交
10 11
#include <linux/kernel.h>
#include <linux/bitops.h>
12
#include <linux/capability.h>
L
Linus Torvalds 已提交
13 14 15 16 17 18 19 20 21

/* We are 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>
#include <asm/byteorder.h>
J
Joe Perches 已提交
22
#include <linux/uaccess.h>
L
Linus Torvalds 已提交
23 24 25 26 27 28 29 30 31 32 33
#include <net/arp.h>
#include <net/dst.h>
#include <linux/proc_fs.h>
#include <linux/spinlock.h>
#include <linux/seq_file.h>

/* And atm device */
#include <linux/atmdev.h>
#include <linux/atmlec.h>

/* Proxy LEC knows about bridging */
34
#if IS_ENABLED(CONFIG_BRIDGE)
L
Linus Torvalds 已提交
35 36
#include "../bridge/br_private.h"

37
static unsigned char bridge_ula_lec[] = { 0x01, 0x80, 0xc2, 0x00, 0x00 };
L
Linus Torvalds 已提交
38 39 40 41 42 43 44 45 46 47
#endif

/* Modular too */
#include <linux/module.h>
#include <linux/init.h>

#include "lec.h"
#include "lec_arpc.h"
#include "resources.h"

48 49 50 51 52
#define DUMP_PACKETS 0		/*
				 * 0 = None,
				 * 1 = 30 first bytes
				 * 2 = Whole packet
				 */
L
Linus Torvalds 已提交
53

54 55 56 57
#define LEC_UNRES_QUE_LEN 8	/*
				 * number of tx packets to queue for a
				 * single destination while waiting for SVC
				 */
L
Linus Torvalds 已提交
58 59

static int lec_open(struct net_device *dev);
60 61
static netdev_tx_t lec_start_xmit(struct sk_buff *skb,
				  struct net_device *dev);
L
Linus Torvalds 已提交
62
static int lec_close(struct net_device *dev);
63
static struct lec_arp_table *lec_arp_find(struct lec_priv *priv,
64
					  const unsigned char *mac_addr);
L
Linus Torvalds 已提交
65
static int lec_arp_remove(struct lec_priv *priv,
66
			  struct lec_arp_table *to_remove);
L
Linus Torvalds 已提交
67
/* LANE2 functions */
68 69 70
static void lane2_associate_ind(struct net_device *dev, const u8 *mac_address,
				const u8 *tlvs, u32 sizeoftlvs);
static int lane2_resolve(struct net_device *dev, const u8 *dst_mac, int force,
71
			 u8 **tlvs, u32 *sizeoftlvs);
72 73
static int lane2_associate_req(struct net_device *dev, const u8 *lan_dst,
			       const u8 *tlvs, u32 sizeoftlvs);
L
Linus Torvalds 已提交
74

75
static int lec_addr_delete(struct lec_priv *priv, const unsigned char *atm_addr,
L
Linus Torvalds 已提交
76 77 78 79 80
			   unsigned long permanent);
static void lec_arp_check_empties(struct lec_priv *priv,
				  struct atm_vcc *vcc, struct sk_buff *skb);
static void lec_arp_destroy(struct lec_priv *priv);
static void lec_arp_init(struct lec_priv *priv);
81
static struct atm_vcc *lec_arp_resolve(struct lec_priv *priv,
82
				       const unsigned char *mac_to_find,
L
Linus Torvalds 已提交
83 84
				       int is_rdesc,
				       struct lec_arp_table **ret_entry);
85
static void lec_arp_update(struct lec_priv *priv, const unsigned char *mac_addr,
J
Joe Perches 已提交
86 87
			   const unsigned char *atm_addr,
			   unsigned long remoteflag,
L
Linus Torvalds 已提交
88 89 90 91
			   unsigned int targetless_le_arp);
static void lec_flush_complete(struct lec_priv *priv, unsigned long tran_id);
static int lec_mcast_make(struct lec_priv *priv, struct atm_vcc *vcc);
static void lec_set_flush_tran_id(struct lec_priv *priv,
92
				  const unsigned char *atm_addr,
L
Linus Torvalds 已提交
93
				  unsigned long tran_id);
J
Joe Perches 已提交
94 95
static void lec_vcc_added(struct lec_priv *priv,
			  const struct atmlec_ioc *ioc_data,
L
Linus Torvalds 已提交
96
			  struct atm_vcc *vcc,
J
Joe Perches 已提交
97 98
			  void (*old_push)(struct atm_vcc *vcc,
					   struct sk_buff *skb));
L
Linus Torvalds 已提交
99 100
static void lec_vcc_close(struct lec_priv *priv, struct atm_vcc *vcc);

101 102 103 104 105 106 107 108 109 110 111 112
/* must be done under lec_arp_lock */
static inline void lec_arp_hold(struct lec_arp_table *entry)
{
	atomic_inc(&entry->usage);
}

static inline void lec_arp_put(struct lec_arp_table *entry)
{
	if (atomic_dec_and_test(&entry->usage))
		kfree(entry);
}

L
Linus Torvalds 已提交
113
static struct lane2_ops lane2_ops = {
114 115 116
	lane2_resolve,		/* resolve,             spec 3.1.3 */
	lane2_associate_req,	/* associate_req,       spec 3.1.4 */
	NULL			/* associate indicator, spec 3.1.5 */
L
Linus Torvalds 已提交
117 118
};

119
static unsigned char bus_mac[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
L
Linus Torvalds 已提交
120 121 122 123

/* Device structures */
static struct net_device *dev_lec[MAX_LEC_ITF];

124
#if IS_ENABLED(CONFIG_BRIDGE)
L
Linus Torvalds 已提交
125 126
static void lec_handle_bridge(struct sk_buff *skb, struct net_device *dev)
{
127 128 129 130 131 132 133 134 135 136
	char *buff;
	struct lec_priv *priv;

	/*
	 * Check if this is a BPDU. If so, ask zeppelin to send
	 * LE_TOPOLOGY_REQUEST with the same value of Topology Change bit
	 * as the Config BPDU has
	 */
	buff = skb->data + skb->dev->hard_header_len;
	if (*buff++ == 0x42 && *buff++ == 0x42 && *buff++ == 0x03) {
L
Linus Torvalds 已提交
137
		struct sock *sk;
138 139 140 141 142 143 144 145 146 147
		struct sk_buff *skb2;
		struct atmlec_msg *mesg;

		skb2 = alloc_skb(sizeof(struct atmlec_msg), GFP_ATOMIC);
		if (skb2 == NULL)
			return;
		skb2->len = sizeof(struct atmlec_msg);
		mesg = (struct atmlec_msg *)skb2->data;
		mesg->type = l_topology_change;
		buff += 4;
J
Joe Perches 已提交
148 149
		mesg->content.normal.flag = *buff & 0x01;
					/* 0x01 is topology change */
150

151
		priv = netdev_priv(dev);
152
		atm_force_charge(priv->lecd, skb2->truesize);
L
Linus Torvalds 已提交
153
		sk = sk_atm(priv->lecd);
154
		skb_queue_tail(&sk->sk_receive_queue, skb2);
155
		sk->sk_data_ready(sk);
156
	}
L
Linus Torvalds 已提交
157
}
158
#endif /* IS_ENABLED(CONFIG_BRIDGE) */
L
Linus Torvalds 已提交
159 160 161 162 163 164 165 166 167 168

/*
 * Open/initialize the netdevice. This is called (in the current kernel)
 * sometime after booting when the 'ifconfig' program is run.
 *
 * This routine should set everything up anew at each open, even
 * registers that "should" only need to be set once at boot, so that
 * there is non-reboot way to recover if something goes wrong.
 */

169
static int lec_open(struct net_device *dev)
L
Linus Torvalds 已提交
170 171
{
	netif_start_queue(dev);
172 173

	return 0;
L
Linus Torvalds 已提交
174 175
}

176 177
static void
lec_send(struct atm_vcc *vcc, struct sk_buff *skb)
L
Linus Torvalds 已提交
178
{
179 180
	struct net_device *dev = skb->dev;

L
Linus Torvalds 已提交
181 182 183 184 185
	ATM_SKB(skb)->vcc = vcc;
	ATM_SKB(skb)->atm_options = vcc->atm_options;

	atomic_add(skb->truesize, &sk_atm(vcc)->sk_wmem_alloc);
	if (vcc->send(vcc, skb) < 0) {
186
		dev->stats.tx_dropped++;
L
Linus Torvalds 已提交
187 188 189
		return;
	}

190 191
	dev->stats.tx_packets++;
	dev->stats.tx_bytes += skb->len;
L
Linus Torvalds 已提交
192 193
}

194
static void lec_tx_timeout(struct net_device *dev)
L
Linus Torvalds 已提交
195
{
196
	pr_info("%s\n", dev->name);
197
	netif_trans_update(dev);
L
Linus Torvalds 已提交
198 199 200
	netif_wake_queue(dev);
}

201 202
static netdev_tx_t lec_start_xmit(struct sk_buff *skb,
				  struct net_device *dev)
L
Linus Torvalds 已提交
203
{
204
	struct sk_buff *skb2;
205
	struct lec_priv *priv = netdev_priv(dev);
206 207
	struct lecdatahdr_8023 *lec_h;
	struct atm_vcc *vcc;
L
Linus Torvalds 已提交
208
	struct lec_arp_table *entry;
209
	unsigned char *dst;
L
Linus Torvalds 已提交
210
	int min_frame_size;
211 212
	int is_rdesc;

213
	pr_debug("called\n");
214
	if (!priv->lecd) {
J
Joe Perches 已提交
215
		pr_info("%s:No lecd attached\n", dev->name);
216
		dev->stats.tx_errors++;
217
		netif_stop_queue(dev);
218 219
		kfree_skb(skb);
		return NETDEV_TX_OK;
220 221
	}

222
	pr_debug("skbuff head:%lx data:%lx tail:%lx end:%lx\n",
223 224
		 (long)skb->head, (long)skb->data, (long)skb_tail_pointer(skb),
		 (long)skb_end_pointer(skb));
225
#if IS_ENABLED(CONFIG_BRIDGE)
226 227
	if (memcmp(skb->data, bridge_ula_lec, sizeof(bridge_ula_lec)) == 0)
		lec_handle_bridge(skb, dev);
L
Linus Torvalds 已提交
228 229
#endif

230 231
	/* Make sure we have room for lec_id */
	if (skb_headroom(skb) < 2) {
232
		pr_debug("reallocating skb\n");
233
		skb2 = skb_realloc_headroom(skb, LEC_HEADER_LEN);
234 235
		if (unlikely(!skb2)) {
			kfree_skb(skb);
236
			return NETDEV_TX_OK;
237 238
		}
		consume_skb(skb);
239 240 241
		skb = skb2;
	}
	skb_push(skb, 2);
L
Linus Torvalds 已提交
242

243
	/* Put le header to place */
244 245
	lec_h = (struct lecdatahdr_8023 *)skb->data;
	lec_h->le_header = htons(priv->lecid);
L
Linus Torvalds 已提交
246 247

#if DUMP_PACKETS >= 2
J
Joe Perches 已提交
248
#define MAX_DUMP_SKB 99
L
Linus Torvalds 已提交
249
#elif DUMP_PACKETS >= 1
J
Joe Perches 已提交
250 251 252 253 254 255 256
#define MAX_DUMP_SKB 30
#endif
#if DUMP_PACKETS >= 1
	printk(KERN_DEBUG "%s: send datalen:%ld lecid:%4.4x\n",
	       dev->name, skb->len, priv->lecid);
	print_hex_dump(KERN_DEBUG, "", DUMP_OFFSET, 16, 1,
		       skb->data, min(skb->len, MAX_DUMP_SKB), true);
L
Linus Torvalds 已提交
257 258
#endif /* DUMP_PACKETS >= 1 */

259
	/* Minimum ethernet-frame size */
260
	min_frame_size = LEC_MINIMUM_8023_SIZE;
261 262 263 264 265 266 267
	if (skb->len < min_frame_size) {
		if ((skb->len + skb_tailroom(skb)) < min_frame_size) {
			skb2 = skb_copy_expand(skb, 0,
					       min_frame_size - skb->truesize,
					       GFP_ATOMIC);
			dev_kfree_skb(skb);
			if (skb2 == NULL) {
268
				dev->stats.tx_dropped++;
269
				return NETDEV_TX_OK;
270 271 272
			}
			skb = skb2;
		}
L
Linus Torvalds 已提交
273
		skb_put(skb, min_frame_size - skb->len);
274 275 276 277 278 279 280
	}

	/* Send to right vcc */
	is_rdesc = 0;
	dst = lec_h->h_dest;
	entry = NULL;
	vcc = lec_arp_resolve(priv, dst, is_rdesc, &entry);
281 282
	pr_debug("%s:vcc:%p vcc_flags:%lx, entry:%p\n",
		 dev->name, vcc, vcc ? vcc->flags : 0, entry);
283 284
	if (!vcc || !test_bit(ATM_VF_READY, &vcc->flags)) {
		if (entry && (entry->tx_wait.qlen < LEC_UNRES_QUE_LEN)) {
285 286
			pr_debug("%s:queuing packet, MAC address %pM\n",
				 dev->name, lec_h->h_dest);
287 288
			skb_queue_tail(&entry->tx_wait, skb);
		} else {
289 290
			pr_debug("%s:tx queue full or no arp entry, dropping, MAC address: %pM\n",
				 dev->name, lec_h->h_dest);
291
			dev->stats.tx_dropped++;
292 293
			dev_kfree_skb(skb);
		}
294
		goto out;
295 296
	}
#if DUMP_PACKETS > 0
J
Joe Perches 已提交
297 298
	printk(KERN_DEBUG "%s:sending to vpi:%d vci:%d\n",
	       dev->name, vcc->vpi, vcc->vci);
L
Linus Torvalds 已提交
299
#endif /* DUMP_PACKETS > 0 */
300 301

	while (entry && (skb2 = skb_dequeue(&entry->tx_wait))) {
302
		pr_debug("emptying tx queue, MAC address %pM\n", lec_h->h_dest);
303
		lec_send(vcc, skb2);
304
	}
L
Linus Torvalds 已提交
305

306
	lec_send(vcc, skb);
L
Linus Torvalds 已提交
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323

	if (!atm_may_send(vcc, 0)) {
		struct lec_vcc_priv *vpriv = LEC_VCC_PRIV(vcc);

		vpriv->xoff = 1;
		netif_stop_queue(dev);

		/*
		 * vcc->pop() might have occurred in between, making
		 * the vcc usuable again.  Since xmit is serialized,
		 * this is the only situation we have to re-test.
		 */

		if (atm_may_send(vcc, 0))
			netif_wake_queue(dev);
	}

324 325 326
out:
	if (entry)
		lec_arp_put(entry);
327
	netif_trans_update(dev);
328
	return NETDEV_TX_OK;
L
Linus Torvalds 已提交
329 330 331
}

/* The inverse routine to net_open(). */
332
static int lec_close(struct net_device *dev)
L
Linus Torvalds 已提交
333
{
334 335
	netif_stop_queue(dev);
	return 0;
L
Linus Torvalds 已提交
336 337
}

338
static int lec_atm_send(struct atm_vcc *vcc, struct sk_buff *skb)
L
Linus Torvalds 已提交
339 340
{
	unsigned long flags;
341
	struct net_device *dev = (struct net_device *)vcc->proto_data;
342
	struct lec_priv *priv = netdev_priv(dev);
343 344 345 346
	struct atmlec_msg *mesg;
	struct lec_arp_table *entry;
	int i;
	char *tmp;		/* FIXME */
L
Linus Torvalds 已提交
347 348

	atomic_sub(skb->truesize, &sk_atm(vcc)->sk_wmem_alloc);
349 350 351
	mesg = (struct atmlec_msg *)skb->data;
	tmp = skb->data;
	tmp += sizeof(struct atmlec_msg);
352
	pr_debug("%s: msg from zeppelin:%d\n", dev->name, mesg->type);
353 354
	switch (mesg->type) {
	case l_set_mac_addr:
J
Joe Perches 已提交
355
		for (i = 0; i < 6; i++)
356 357 358
			dev->dev_addr[i] = mesg->content.normal.mac_addr[i];
		break;
	case l_del_mac_addr:
J
Joe Perches 已提交
359
		for (i = 0; i < 6; i++)
360 361 362 363 364 365 366 367 368 369 370 371 372
			dev->dev_addr[i] = 0;
		break;
	case l_addr_delete:
		lec_addr_delete(priv, mesg->content.normal.atm_addr,
				mesg->content.normal.flag);
		break;
	case l_topology_change:
		priv->topology_change = mesg->content.normal.flag;
		break;
	case l_flush_complete:
		lec_flush_complete(priv, mesg->content.normal.flag);
		break;
	case l_narp_req:	/* LANE2: see 7.1.35 in the lane2 spec */
L
Linus Torvalds 已提交
373
		spin_lock_irqsave(&priv->lec_arp_lock, flags);
374 375
		entry = lec_arp_find(priv, mesg->content.normal.mac_addr);
		lec_arp_remove(priv, entry);
L
Linus Torvalds 已提交
376 377
		spin_unlock_irqrestore(&priv->lec_arp_lock, flags);

378 379 380 381 382 383 384 385
		if (mesg->content.normal.no_source_le_narp)
			break;
		/* FALL THROUGH */
	case l_arp_update:
		lec_arp_update(priv, mesg->content.normal.mac_addr,
			       mesg->content.normal.atm_addr,
			       mesg->content.normal.flag,
			       mesg->content.normal.targetless_le_arp);
386
		pr_debug("in l_arp_update\n");
387
		if (mesg->sizeoftlvs != 0) {	/* LANE2 3.1.5 */
388 389
			pr_debug("LANE2 3.1.5, got tlvs, size %d\n",
				 mesg->sizeoftlvs);
390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407
			lane2_associate_ind(dev, mesg->content.normal.mac_addr,
					    tmp, mesg->sizeoftlvs);
		}
		break;
	case l_config:
		priv->maximum_unknown_frame_count =
		    mesg->content.config.maximum_unknown_frame_count;
		priv->max_unknown_frame_time =
		    (mesg->content.config.max_unknown_frame_time * HZ);
		priv->max_retry_count = mesg->content.config.max_retry_count;
		priv->aging_time = (mesg->content.config.aging_time * HZ);
		priv->forward_delay_time =
		    (mesg->content.config.forward_delay_time * HZ);
		priv->arp_response_time =
		    (mesg->content.config.arp_response_time * HZ);
		priv->flush_timeout = (mesg->content.config.flush_timeout * HZ);
		priv->path_switching_delay =
		    (mesg->content.config.path_switching_delay * HZ);
J
Joe Perches 已提交
408 409
		priv->lane_version = mesg->content.config.lane_version;
					/* LANE2 */
L
Linus Torvalds 已提交
410 411 412
		priv->lane2_ops = NULL;
		if (priv->lane_version > 1)
			priv->lane2_ops = &lane2_ops;
413
		rtnl_lock();
S
Stephen Hemminger 已提交
414
		if (dev_set_mtu(dev, mesg->content.config.mtu))
J
Joe Perches 已提交
415 416
			pr_info("%s: change_mtu to %d failed\n",
				dev->name, mesg->content.config.mtu);
417
		rtnl_unlock();
L
Linus Torvalds 已提交
418
		priv->is_proxy = mesg->content.config.is_proxy;
419 420 421 422 423 424 425 426 427 428
		break;
	case l_flush_tran_id:
		lec_set_flush_tran_id(priv, mesg->content.normal.atm_addr,
				      mesg->content.normal.flag);
		break;
	case l_set_lecid:
		priv->lecid =
		    (unsigned short)(0xffff & mesg->content.normal.flag);
		break;
	case l_should_bridge:
429
#if IS_ENABLED(CONFIG_BRIDGE)
430 431 432
	{
		pr_debug("%s: bridge zeppelin asks about %pM\n",
			 dev->name, mesg->content.proxy.mac_addr);
433

434 435
		if (br_fdb_test_addr_hook == NULL)
			break;
436

437 438 439 440 441 442 443 444 445 446 447 448 449 450 451
		if (br_fdb_test_addr_hook(dev, mesg->content.proxy.mac_addr)) {
			/* hit from bridge table, send LE_ARP_RESPONSE */
			struct sk_buff *skb2;
			struct sock *sk;

			pr_debug("%s: entry found, responding to zeppelin\n",
				 dev->name);
			skb2 = alloc_skb(sizeof(struct atmlec_msg), GFP_ATOMIC);
			if (skb2 == NULL)
				break;
			skb2->len = sizeof(struct atmlec_msg);
			skb_copy_to_linear_data(skb2, mesg, sizeof(*mesg));
			atm_force_charge(priv->lecd, skb2->truesize);
			sk = sk_atm(priv->lecd);
			skb_queue_tail(&sk->sk_receive_queue, skb2);
452
			sk->sk_data_ready(sk);
453
		}
454
	}
455
#endif /* IS_ENABLED(CONFIG_BRIDGE) */
456 457
		break;
	default:
J
Joe Perches 已提交
458
		pr_info("%s: Unknown message type %d\n", dev->name, mesg->type);
459 460 461 462 463
		dev_kfree_skb(skb);
		return -EINVAL;
	}
	dev_kfree_skb(skb);
	return 0;
L
Linus Torvalds 已提交
464 465
}

466
static void lec_atm_close(struct atm_vcc *vcc)
L
Linus Torvalds 已提交
467
{
468 469
	struct sk_buff *skb;
	struct net_device *dev = (struct net_device *)vcc->proto_data;
470
	struct lec_priv *priv = netdev_priv(dev);
L
Linus Torvalds 已提交
471

472 473
	priv->lecd = NULL;
	/* Do something needful? */
L
Linus Torvalds 已提交
474

475 476
	netif_stop_queue(dev);
	lec_arp_destroy(priv);
L
Linus Torvalds 已提交
477

478
	if (skb_peek(&sk_atm(vcc)->sk_receive_queue))
J
Joe Perches 已提交
479
		pr_info("%s closing with messages pending\n", dev->name);
480
	while ((skb = skb_dequeue(&sk_atm(vcc)->sk_receive_queue))) {
481
		atm_return(vcc, skb->truesize);
L
Linus Torvalds 已提交
482
		dev_kfree_skb(skb);
483 484
	}

J
Joe Perches 已提交
485
	pr_info("%s: Shut down!\n", dev->name);
486
	module_put(THIS_MODULE);
L
Linus Torvalds 已提交
487 488 489
}

static struct atmdev_ops lecdev_ops = {
490 491
	.close = lec_atm_close,
	.send = lec_atm_send
L
Linus Torvalds 已提交
492 493 494
};

static struct atm_dev lecatm_dev = {
495 496 497
	.ops = &lecdev_ops,
	.type = "lec",
	.number = 999,		/* dummy device number */
498
	.lock = __SPIN_LOCK_UNLOCKED(lecatm_dev.lock)
L
Linus Torvalds 已提交
499 500 501 502 503 504
};

/*
 * LANE2: new argument struct sk_buff *data contains
 * the LE_ARP based TLVs introduced in the LANE2 spec
 */
505 506
static int
send_to_lecd(struct lec_priv *priv, atmlec_msg_type type,
507
	     const unsigned char *mac_addr, const unsigned char *atm_addr,
508
	     struct sk_buff *data)
L
Linus Torvalds 已提交
509 510 511 512 513
{
	struct sock *sk;
	struct sk_buff *skb;
	struct atmlec_msg *mesg;

J
Joe Perches 已提交
514
	if (!priv || !priv->lecd)
L
Linus Torvalds 已提交
515 516 517 518 519 520
		return -1;
	skb = alloc_skb(sizeof(struct atmlec_msg), GFP_ATOMIC);
	if (!skb)
		return -1;
	skb->len = sizeof(struct atmlec_msg);
	mesg = (struct atmlec_msg *)skb->data;
521
	memset(mesg, 0, sizeof(struct atmlec_msg));
L
Linus Torvalds 已提交
522
	mesg->type = type;
523 524
	if (data != NULL)
		mesg->sizeoftlvs = data->len;
L
Linus Torvalds 已提交
525
	if (mac_addr)
526
		ether_addr_copy(mesg->content.normal.mac_addr, mac_addr);
527 528
	else
		mesg->content.normal.targetless_le_arp = 1;
L
Linus Torvalds 已提交
529 530 531
	if (atm_addr)
		memcpy(&mesg->content.normal.atm_addr, atm_addr, ATM_ESA_LEN);

532
	atm_force_charge(priv->lecd, skb->truesize);
L
Linus Torvalds 已提交
533 534
	sk = sk_atm(priv->lecd);
	skb_queue_tail(&sk->sk_receive_queue, skb);
535
	sk->sk_data_ready(sk);
L
Linus Torvalds 已提交
536

537
	if (data != NULL) {
538
		pr_debug("about to send %d bytes of data\n", data->len);
539 540
		atm_force_charge(priv->lecd, data->truesize);
		skb_queue_tail(&sk->sk_receive_queue, data);
541
		sk->sk_data_ready(sk);
542
	}
L
Linus Torvalds 已提交
543

544
	return 0;
L
Linus Torvalds 已提交
545 546 547 548 549
}

/* shamelessly stolen from drivers/net/net_init.c */
static int lec_change_mtu(struct net_device *dev, int new_mtu)
{
550 551 552 553
	if ((new_mtu < 68) || (new_mtu > 18190))
		return -EINVAL;
	dev->mtu = new_mtu;
	return 0;
L
Linus Torvalds 已提交
554 555 556 557
}

static void lec_set_multicast_list(struct net_device *dev)
{
558 559 560 561
	/*
	 * by default, all multicast frames arrive over the bus.
	 * eventually support selective multicast service
	 */
L
Linus Torvalds 已提交
562 563
}

564 565 566 567 568 569
static const struct net_device_ops lec_netdev_ops = {
	.ndo_open		= lec_open,
	.ndo_stop		= lec_close,
	.ndo_start_xmit		= lec_start_xmit,
	.ndo_change_mtu		= lec_change_mtu,
	.ndo_tx_timeout		= lec_tx_timeout,
570
	.ndo_set_rx_mode	= lec_set_multicast_list,
571 572
};

573
static const unsigned char lec_ctrl_magic[] = {
574 575 576 577 578
	0xff,
	0x00,
	0x01,
	0x01
};
L
Linus Torvalds 已提交
579

580 581 582 583
#define LEC_DATA_DIRECT_8023  2
#define LEC_DATA_DIRECT_8025  3

static int lec_is_data_direct(struct atm_vcc *vcc)
584
{
585 586
	return ((vcc->sap.blli[0].l3.tr9577.snap[4] == LEC_DATA_DIRECT_8023) ||
		(vcc->sap.blli[0].l3.tr9577.snap[4] == LEC_DATA_DIRECT_8025));
587
}
588

589
static void lec_push(struct atm_vcc *vcc, struct sk_buff *skb)
L
Linus Torvalds 已提交
590
{
591
	unsigned long flags;
592
	struct net_device *dev = (struct net_device *)vcc->proto_data;
593
	struct lec_priv *priv = netdev_priv(dev);
L
Linus Torvalds 已提交
594

J
Joe Perches 已提交
595
#if DUMP_PACKETS > 0
596 597
	printk(KERN_DEBUG "%s: vcc vpi:%d vci:%d\n",
	       dev->name, vcc->vpi, vcc->vci);
L
Linus Torvalds 已提交
598
#endif
599
	if (!skb) {
600
		pr_debug("%s: null skb\n", dev->name);
601 602 603
		lec_vcc_close(priv, vcc);
		return;
	}
L
Linus Torvalds 已提交
604
#if DUMP_PACKETS >= 2
605
#define MAX_SKB_DUMP 99
L
Linus Torvalds 已提交
606
#elif DUMP_PACKETS >= 1
607 608 609 610 611 612 613
#define MAX_SKB_DUMP 30
#endif
#if DUMP_PACKETS > 0
	printk(KERN_DEBUG "%s: rcv datalen:%ld lecid:%4.4x\n",
	       dev->name, skb->len, priv->lecid);
	print_hex_dump(KERN_DEBUG, "", DUMP_OFFSET, 16, 1,
		       skb->data, min(MAX_SKB_DUMP, skb->len), true);
L
Linus Torvalds 已提交
614
#endif /* DUMP_PACKETS > 0 */
615 616
	if (memcmp(skb->data, lec_ctrl_magic, 4) == 0) {
				/* Control frame, to daemon */
L
Linus Torvalds 已提交
617 618
		struct sock *sk = sk_atm(vcc);

619
		pr_debug("%s: To daemon\n", dev->name);
620
		skb_queue_tail(&sk->sk_receive_queue, skb);
621
		sk->sk_data_ready(sk);
622
	} else {		/* Data frame, queue to protocol handlers */
623
		struct lec_arp_table *entry;
624 625 626
		unsigned char *src, *dst;

		atm_return(vcc, skb->truesize);
A
Al Viro 已提交
627
		if (*(__be16 *) skb->data == htons(priv->lecid) ||
628 629 630 631 632
		    !priv->lecd || !(dev->flags & IFF_UP)) {
			/*
			 * Probably looping back, or if lecd is missing,
			 * lecd has gone down
			 */
633
			pr_debug("Ignoring frame...\n");
634 635 636
			dev_kfree_skb(skb);
			return;
		}
637
		dst = ((struct lecdatahdr_8023 *)skb->data)->h_dest;
638

639 640
		/*
		 * If this is a Data Direct VCC, and the VCC does not match
641 642 643 644
		 * the LE_ARP cache entry, delete the LE_ARP cache entry.
		 */
		spin_lock_irqsave(&priv->lec_arp_lock, flags);
		if (lec_is_data_direct(vcc)) {
645
			src = ((struct lecdatahdr_8023 *)skb->data)->h_source;
646 647 648
			entry = lec_arp_find(priv, src);
			if (entry && entry->vcc != vcc) {
				lec_arp_remove(priv, entry);
649
				lec_arp_put(entry);
650 651 652
			}
		}
		spin_unlock_irqrestore(&priv->lec_arp_lock, flags);
L
Linus Torvalds 已提交
653

654 655
		if (!(dst[0] & 0x01) &&	/* Never filter Multi/Broadcast */
		    !priv->is_proxy &&	/* Proxy wants all the packets */
L
Linus Torvalds 已提交
656
		    memcmp(dst, dev->dev_addr, dev->addr_len)) {
657 658 659
			dev_kfree_skb(skb);
			return;
		}
J
Joe Perches 已提交
660
		if (!hlist_empty(&priv->lec_arp_empty_ones))
661 662
			lec_arp_check_empties(priv, vcc, skb);
		skb_pull(skb, 2);	/* skip lec_id */
663
		skb->protocol = eth_type_trans(skb, dev);
664 665
		dev->stats.rx_packets++;
		dev->stats.rx_bytes += skb->len;
666 667 668
		memset(ATM_SKB(skb), 0, sizeof(struct atm_skb_data));
		netif_rx(skb);
	}
L
Linus Torvalds 已提交
669 670
}

671
static void lec_pop(struct atm_vcc *vcc, struct sk_buff *skb)
L
Linus Torvalds 已提交
672 673 674 675 676
{
	struct lec_vcc_priv *vpriv = LEC_VCC_PRIV(vcc);
	struct net_device *dev = skb->dev;

	if (vpriv == NULL) {
677
		pr_info("vpriv = NULL!?!?!?\n");
L
Linus Torvalds 已提交
678 679 680 681 682 683 684 685 686 687 688 689
		return;
	}

	vpriv->old_pop(vcc, skb);

	if (vpriv->xoff && atm_may_send(vcc, 0)) {
		vpriv->xoff = 0;
		if (netif_running(dev) && netif_queue_stopped(dev))
			netif_wake_queue(dev);
	}
}

690
static int lec_vcc_attach(struct atm_vcc *vcc, void __user *arg)
L
Linus Torvalds 已提交
691 692
{
	struct lec_vcc_priv *vpriv;
693 694 695 696 697
	int bytes_left;
	struct atmlec_ioc ioc_data;

	/* Lecd must be up in this case */
	bytes_left = copy_from_user(&ioc_data, arg, sizeof(struct atmlec_ioc));
698 699
	if (bytes_left != 0)
		pr_info("copy from user failed for %d bytes\n", bytes_left);
700 701 702
	if (ioc_data.dev_num < 0 || ioc_data.dev_num >= MAX_LEC_ITF ||
	    !dev_lec[ioc_data.dev_num])
		return -EINVAL;
J
Joe Perches 已提交
703 704
	vpriv = kmalloc(sizeof(struct lec_vcc_priv), GFP_KERNEL);
	if (!vpriv)
L
Linus Torvalds 已提交
705 706 707 708 709
		return -ENOMEM;
	vpriv->xoff = 0;
	vpriv->old_pop = vcc->pop;
	vcc->user_back = vpriv;
	vcc->pop = lec_pop;
710
	lec_vcc_added(netdev_priv(dev_lec[ioc_data.dev_num]),
711 712 713 714
		      &ioc_data, vcc, vcc->push);
	vcc->proto_data = dev_lec[ioc_data.dev_num];
	vcc->push = lec_push;
	return 0;
L
Linus Torvalds 已提交
715 716
}

717
static int lec_mcast_attach(struct atm_vcc *vcc, int arg)
L
Linus Torvalds 已提交
718
{
719 720 721
	if (arg < 0 || arg >= MAX_LEC_ITF || !dev_lec[arg])
		return -EINVAL;
	vcc->proto_data = dev_lec[arg];
722
	return lec_mcast_make(netdev_priv(dev_lec[arg]), vcc);
L
Linus Torvalds 已提交
723 724 725
}

/* Initialize device. */
726 727 728 729 730 731 732 733 734 735 736 737
static int lecd_attach(struct atm_vcc *vcc, int arg)
{
	int i;
	struct lec_priv *priv;

	if (arg < 0)
		i = 0;
	else
		i = arg;
	if (arg >= MAX_LEC_ITF)
		return -EINVAL;
	if (!dev_lec[i]) {
738
		int size;
L
Linus Torvalds 已提交
739

740
		size = sizeof(struct lec_priv);
741
		dev_lec[i] = alloc_etherdev(size);
742 743
		if (!dev_lec[i])
			return -ENOMEM;
744
		dev_lec[i]->netdev_ops = &lec_netdev_ops;
745 746 747 748 749 750
		snprintf(dev_lec[i]->name, IFNAMSIZ, "lec%d", i);
		if (register_netdev(dev_lec[i])) {
			free_netdev(dev_lec[i]);
			return -EINVAL;
		}

751
		priv = netdev_priv(dev_lec[i]);
752
	} else {
753
		priv = netdev_priv(dev_lec[i]);
754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778
		if (priv->lecd)
			return -EADDRINUSE;
	}
	lec_arp_init(priv);
	priv->itfnum = i;	/* LANE2 addition */
	priv->lecd = vcc;
	vcc->dev = &lecatm_dev;
	vcc_insert_socket(sk_atm(vcc));

	vcc->proto_data = dev_lec[i];
	set_bit(ATM_VF_META, &vcc->flags);
	set_bit(ATM_VF_READY, &vcc->flags);

	/* Set default values to these variables */
	priv->maximum_unknown_frame_count = 1;
	priv->max_unknown_frame_time = (1 * HZ);
	priv->vcc_timeout_period = (1200 * HZ);
	priv->max_retry_count = 1;
	priv->aging_time = (300 * HZ);
	priv->forward_delay_time = (15 * HZ);
	priv->topology_change = 0;
	priv->arp_response_time = (1 * HZ);
	priv->flush_timeout = (4 * HZ);
	priv->path_switching_delay = (6 * HZ);

J
Joe Perches 已提交
779
	if (dev_lec[i]->flags & IFF_UP)
780 781 782
		netif_start_queue(dev_lec[i]);
	__module_get(THIS_MODULE);
	return i;
L
Linus Torvalds 已提交
783 784 785
}

#ifdef CONFIG_PROC_FS
786
static const char *lec_arp_get_status_string(unsigned char status)
L
Linus Torvalds 已提交
787
{
788
	static const char *const lec_arp_status_string[] = {
L
Linus Torvalds 已提交
789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815
		"ESI_UNKNOWN       ",
		"ESI_ARP_PENDING   ",
		"ESI_VC_PENDING    ",
		"<Undefined>       ",
		"ESI_FLUSH_PENDING ",
		"ESI_FORWARD_DIRECT"
	};

	if (status > ESI_FORWARD_DIRECT)
		status = 3;	/* ESI_UNDEFINED */
	return lec_arp_status_string[status];
}

static void lec_info(struct seq_file *seq, struct lec_arp_table *entry)
{
	int i;

	for (i = 0; i < ETH_ALEN; i++)
		seq_printf(seq, "%2.2x", entry->mac_addr[i] & 0xff);
	seq_printf(seq, " ");
	for (i = 0; i < ATM_ESA_LEN; i++)
		seq_printf(seq, "%2.2x", entry->atm_addr[i] & 0xff);
	seq_printf(seq, " %s %4.4x", lec_arp_get_status_string(entry->status),
		   entry->flags & 0xffff);
	if (entry->vcc)
		seq_printf(seq, "%3d %3d ", entry->vcc->vpi, entry->vcc->vci);
	else
816
		seq_printf(seq, "        ");
L
Linus Torvalds 已提交
817 818 819
	if (entry->recv_vcc) {
		seq_printf(seq, "     %3d %3d", entry->recv_vcc->vpi,
			   entry->recv_vcc->vci);
820 821
	}
	seq_putc(seq, '\n');
L
Linus Torvalds 已提交
822 823 824 825 826
}

struct lec_state {
	unsigned long flags;
	struct lec_priv *locked;
827
	struct hlist_node *node;
L
Linus Torvalds 已提交
828 829 830 831 832 833
	struct net_device *dev;
	int itf;
	int arp_table;
	int misc_table;
};

834
static void *lec_tbl_walk(struct lec_state *state, struct hlist_head *tbl,
L
Linus Torvalds 已提交
835 836
			  loff_t *l)
{
837
	struct hlist_node *e = state->node;
L
Linus Torvalds 已提交
838 839

	if (!e)
840
		e = tbl->first;
J
Joe Perches 已提交
841
	if (e == SEQ_START_TOKEN) {
842
		e = tbl->first;
L
Linus Torvalds 已提交
843 844
		--*l;
	}
845

846
	for (; e; e = e->next) {
L
Linus Torvalds 已提交
847 848 849
		if (--*l < 0)
			break;
	}
850 851
	state->node = e;

L
Linus Torvalds 已提交
852 853 854 855
	return (*l < 0) ? state : NULL;
}

static void *lec_arp_walk(struct lec_state *state, loff_t *l,
856
			  struct lec_priv *priv)
L
Linus Torvalds 已提交
857 858 859 860 861
{
	void *v = NULL;
	int p;

	for (p = state->arp_table; p < LEC_ARP_TABLE_SIZE; p++) {
862
		v = lec_tbl_walk(state, &priv->lec_arp_tables[p], l);
L
Linus Torvalds 已提交
863 864 865 866 867 868 869 870 871 872
		if (v)
			break;
	}
	state->arp_table = p;
	return v;
}

static void *lec_misc_walk(struct lec_state *state, loff_t *l,
			   struct lec_priv *priv)
{
873 874 875 876
	struct hlist_head *lec_misc_tables[] = {
		&priv->lec_arp_empty_ones,
		&priv->lec_no_forward,
		&priv->mcast_fwds
L
Linus Torvalds 已提交
877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896
	};
	void *v = NULL;
	int q;

	for (q = state->misc_table; q < ARRAY_SIZE(lec_misc_tables); q++) {
		v = lec_tbl_walk(state, lec_misc_tables[q], l);
		if (v)
			break;
	}
	state->misc_table = q;
	return v;
}

static void *lec_priv_walk(struct lec_state *state, loff_t *l,
			   struct lec_priv *priv)
{
	if (!state->locked) {
		state->locked = priv;
		spin_lock_irqsave(&priv->lec_arp_lock, state->flags);
	}
897
	if (!lec_arp_walk(state, l, priv) && !lec_misc_walk(state, l, priv)) {
L
Linus Torvalds 已提交
898 899 900 901 902 903 904 905 906 907 908 909 910 911
		spin_unlock_irqrestore(&priv->lec_arp_lock, state->flags);
		state->locked = NULL;
		/* Partial state reset for the next time we get called */
		state->arp_table = state->misc_table = 0;
	}
	return state->locked;
}

static void *lec_itf_walk(struct lec_state *state, loff_t *l)
{
	struct net_device *dev;
	void *v;

	dev = state->dev ? state->dev : dev_lec[state->itf];
912 913
	v = (dev && netdev_priv(dev)) ?
		lec_priv_walk(state, l, netdev_priv(dev)) : NULL;
L
Linus Torvalds 已提交
914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931
	if (!v && dev) {
		dev_put(dev);
		/* Partial state reset for the next time we get called */
		dev = NULL;
	}
	state->dev = dev;
	return v;
}

static void *lec_get_idx(struct lec_state *state, loff_t l)
{
	void *v = NULL;

	for (; state->itf < MAX_LEC_ITF; state->itf++) {
		v = lec_itf_walk(state, &l);
		if (v)
			break;
	}
932
	return v;
L
Linus Torvalds 已提交
933 934 935 936 937 938 939 940 941 942 943
}

static void *lec_seq_start(struct seq_file *seq, loff_t *pos)
{
	struct lec_state *state = seq->private;

	state->itf = 0;
	state->dev = NULL;
	state->locked = NULL;
	state->arp_table = 0;
	state->misc_table = 0;
J
Joe Perches 已提交
944
	state->node = SEQ_START_TOKEN;
L
Linus Torvalds 已提交
945

J
Joe Perches 已提交
946
	return *pos ? lec_get_idx(state, *pos) : SEQ_START_TOKEN;
L
Linus Torvalds 已提交
947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970
}

static void lec_seq_stop(struct seq_file *seq, void *v)
{
	struct lec_state *state = seq->private;

	if (state->dev) {
		spin_unlock_irqrestore(&state->locked->lec_arp_lock,
				       state->flags);
		dev_put(state->dev);
	}
}

static void *lec_seq_next(struct seq_file *seq, void *v, loff_t *pos)
{
	struct lec_state *state = seq->private;

	v = lec_get_idx(state, 1);
	*pos += !!PTR_ERR(v);
	return v;
}

static int lec_seq_show(struct seq_file *seq, void *v)
{
971 972
	static const char lec_banner[] =
	    "Itf  MAC          ATM destination"
973 974
	    "                          Status            Flags "
	    "VPI/VCI Recv VPI/VCI\n";
L
Linus Torvalds 已提交
975

J
Joe Perches 已提交
976
	if (v == SEQ_START_TOKEN)
L
Linus Torvalds 已提交
977 978 979
		seq_puts(seq, lec_banner);
	else {
		struct lec_state *state = seq->private;
980
		struct net_device *dev = state->dev;
J
Joe Perches 已提交
981 982 983
		struct lec_arp_table *entry = hlist_entry(state->node,
							  struct lec_arp_table,
							  next);
L
Linus Torvalds 已提交
984 985

		seq_printf(seq, "%s ", dev->name);
986
		lec_info(seq, entry);
L
Linus Torvalds 已提交
987 988 989 990
	}
	return 0;
}

991
static const struct seq_operations lec_seq_ops = {
992 993 994 995
	.start = lec_seq_start,
	.next = lec_seq_next,
	.stop = lec_seq_stop,
	.show = lec_seq_show,
L
Linus Torvalds 已提交
996 997 998 999
};

static int lec_seq_open(struct inode *inode, struct file *file)
{
1000
	return seq_open_private(file, &lec_seq_ops, sizeof(struct lec_state));
L
Linus Torvalds 已提交
1001 1002
}

1003
static const struct file_operations lec_seq_fops = {
1004 1005 1006 1007
	.owner = THIS_MODULE,
	.open = lec_seq_open,
	.read = seq_read,
	.llseek = seq_lseek,
1008
	.release = seq_release_private,
L
Linus Torvalds 已提交
1009 1010 1011 1012 1013 1014 1015
};
#endif

static int lane_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
{
	struct atm_vcc *vcc = ATM_SD(sock);
	int err = 0;
1016

L
Linus Torvalds 已提交
1017
	switch (cmd) {
1018 1019 1020 1021 1022 1023 1024 1025
	case ATMLEC_CTRL:
	case ATMLEC_MCAST:
	case ATMLEC_DATA:
		if (!capable(CAP_NET_ADMIN))
			return -EPERM;
		break;
	default:
		return -ENOIOCTLCMD;
L
Linus Torvalds 已提交
1026 1027 1028
	}

	switch (cmd) {
1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039
	case ATMLEC_CTRL:
		err = lecd_attach(vcc, (int)arg);
		if (err >= 0)
			sock->state = SS_CONNECTED;
		break;
	case ATMLEC_MCAST:
		err = lec_mcast_attach(vcc, (int)arg);
		break;
	case ATMLEC_DATA:
		err = lec_vcc_attach(vcc, (void __user *)arg);
		break;
L
Linus Torvalds 已提交
1040 1041 1042 1043 1044 1045
	}

	return err;
}

static struct atm_ioctl lane_ioctl_ops = {
1046 1047
	.owner = THIS_MODULE,
	.ioctl = lane_ioctl,
L
Linus Torvalds 已提交
1048 1049 1050 1051 1052 1053 1054
};

static int __init lane_module_init(void)
{
#ifdef CONFIG_PROC_FS
	struct proc_dir_entry *p;

1055
	p = proc_create("lec", S_IRUGO, atm_proc_root, &lec_seq_fops);
1056
	if (!p) {
1057
		pr_err("Unable to initialize /proc/net/atm/lec\n");
1058 1059
		return -ENOMEM;
	}
L
Linus Torvalds 已提交
1060 1061 1062
#endif

	register_atm_ioctl(&lane_ioctl_ops);
M
Michal Marek 已提交
1063
	pr_info("lec.c: initialized\n");
1064
	return 0;
L
Linus Torvalds 已提交
1065 1066 1067 1068
}

static void __exit lane_module_cleanup(void)
{
1069
	int i;
L
Linus Torvalds 已提交
1070 1071 1072 1073 1074

	remove_proc_entry("lec", atm_proc_root);

	deregister_atm_ioctl(&lane_ioctl_ops);

1075 1076
	for (i = 0; i < MAX_LEC_ITF; i++) {
		if (dev_lec[i] != NULL) {
L
Linus Torvalds 已提交
1077
			unregister_netdev(dev_lec[i]);
1078 1079 1080 1081
			free_netdev(dev_lec[i]);
			dev_lec[i] = NULL;
		}
	}
L
Linus Torvalds 已提交
1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093
}

module_init(lane_module_init);
module_exit(lane_module_cleanup);

/*
 * LANE2: 3.1.3, LE_RESOLVE.request
 * Non force allocates memory and fills in *tlvs, fills in *sizeoftlvs.
 * If sizeoftlvs == NULL the default TLVs associated with with this
 * lec will be used.
 * If dst_mac == NULL, targetless LE_ARP will be sent
 */
1094
static int lane2_resolve(struct net_device *dev, const u8 *dst_mac, int force,
1095
			 u8 **tlvs, u32 *sizeoftlvs)
L
Linus Torvalds 已提交
1096 1097
{
	unsigned long flags;
1098
	struct lec_priv *priv = netdev_priv(dev);
1099 1100 1101
	struct lec_arp_table *table;
	struct sk_buff *skb;
	int retval;
L
Linus Torvalds 已提交
1102

1103
	if (force == 0) {
L
Linus Torvalds 已提交
1104
		spin_lock_irqsave(&priv->lec_arp_lock, flags);
1105
		table = lec_arp_find(priv, dst_mac);
L
Linus Torvalds 已提交
1106
		spin_unlock_irqrestore(&priv->lec_arp_lock, flags);
1107 1108 1109
		if (table == NULL)
			return -1;

1110
		*tlvs = kmemdup(table->tlvs, table->sizeoftlvs, GFP_ATOMIC);
1111 1112 1113 1114 1115 1116 1117
		if (*tlvs == NULL)
			return -1;

		*sizeoftlvs = table->sizeoftlvs;

		return 0;
	}
L
Linus Torvalds 已提交
1118 1119 1120

	if (sizeoftlvs == NULL)
		retval = send_to_lecd(priv, l_arp_xmt, dst_mac, NULL, NULL);
1121

L
Linus Torvalds 已提交
1122 1123 1124 1125 1126
	else {
		skb = alloc_skb(*sizeoftlvs, GFP_ATOMIC);
		if (skb == NULL)
			return -1;
		skb->len = *sizeoftlvs;
1127
		skb_copy_to_linear_data(skb, *tlvs, *sizeoftlvs);
L
Linus Torvalds 已提交
1128 1129
		retval = send_to_lecd(priv, l_arp_xmt, dst_mac, NULL, skb);
	}
1130 1131
	return retval;
}
L
Linus Torvalds 已提交
1132 1133 1134 1135 1136 1137 1138 1139

/*
 * LANE2: 3.1.4, LE_ASSOCIATE.request
 * Associate the *tlvs with the *lan_dst address.
 * Will overwrite any previous association
 * Returns 1 for success, 0 for failure (out of memory)
 *
 */
1140 1141
static int lane2_associate_req(struct net_device *dev, const u8 *lan_dst,
			       const u8 *tlvs, u32 sizeoftlvs)
L
Linus Torvalds 已提交
1142
{
1143 1144
	int retval;
	struct sk_buff *skb;
1145
	struct lec_priv *priv = netdev_priv(dev);
1146

1147
	if (!ether_addr_equal(lan_dst, dev->dev_addr))
J
Joe Perches 已提交
1148
		return 0;	/* not our mac address */
1149 1150 1151

	kfree(priv->tlvs);	/* NULL if there was no previous association */

1152
	priv->tlvs = kmemdup(tlvs, sizeoftlvs, GFP_KERNEL);
1153
	if (priv->tlvs == NULL)
J
Joe Perches 已提交
1154
		return 0;
1155 1156 1157 1158 1159 1160
	priv->sizeoftlvs = sizeoftlvs;

	skb = alloc_skb(sizeoftlvs, GFP_ATOMIC);
	if (skb == NULL)
		return 0;
	skb->len = sizeoftlvs;
1161
	skb_copy_to_linear_data(skb, tlvs, sizeoftlvs);
1162 1163
	retval = send_to_lecd(priv, l_associate_req, NULL, NULL, skb);
	if (retval != 0)
J
Joe Perches 已提交
1164
		pr_info("lec.c: lane2_associate_req() failed\n");
1165 1166 1167 1168
	/*
	 * If the previous association has changed we must
	 * somehow notify other LANE entities about the change
	 */
J
Joe Perches 已提交
1169
	return 1;
L
Linus Torvalds 已提交
1170 1171 1172 1173 1174 1175
}

/*
 * LANE2: 3.1.5, LE_ASSOCIATE.indication
 *
 */
1176 1177
static void lane2_associate_ind(struct net_device *dev, const u8 *mac_addr,
				const u8 *tlvs, u32 sizeoftlvs)
L
Linus Torvalds 已提交
1178 1179
{
#if 0
1180
	int i = 0;
L
Linus Torvalds 已提交
1181
#endif
1182
	struct lec_priv *priv = netdev_priv(dev);
1183 1184 1185 1186 1187 1188 1189
#if 0				/*
				 * Why have the TLVs in LE_ARP entries
				 * since we do not use them? When you
				 * uncomment this code, make sure the
				 * TLVs get freed when entry is killed
				 */
	struct lec_arp_table *entry = lec_arp_find(priv, mac_addr);
L
Linus Torvalds 已提交
1190

1191 1192
	if (entry == NULL)
		return;		/* should not happen */
L
Linus Torvalds 已提交
1193

1194
	kfree(entry->tlvs);
L
Linus Torvalds 已提交
1195

1196
	entry->tlvs = kmemdup(tlvs, sizeoftlvs, GFP_KERNEL);
1197 1198 1199
	if (entry->tlvs == NULL)
		return;
	entry->sizeoftlvs = sizeoftlvs;
L
Linus Torvalds 已提交
1200 1201
#endif
#if 0
J
Joe Perches 已提交
1202 1203
	pr_info("\n");
	pr_info("dump of tlvs, sizeoftlvs=%d\n", sizeoftlvs);
1204
	while (i < sizeoftlvs)
J
Joe Perches 已提交
1205
		pr_cont("%02x ", tlvs[i++]);
1206

J
Joe Perches 已提交
1207
	pr_cont("\n");
L
Linus Torvalds 已提交
1208 1209
#endif

1210 1211 1212 1213 1214
	/* tell MPOA about the TLVs we saw */
	if (priv->lane2_ops && priv->lane2_ops->associate_indicator) {
		priv->lane2_ops->associate_indicator(dev, mac_addr,
						     tlvs, sizeoftlvs);
	}
L
Linus Torvalds 已提交
1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225
}

/*
 * Here starts what used to lec_arpc.c
 *
 * lec_arpc.c was added here when making
 * lane client modular. October 1997
 */

#include <linux/types.h>
#include <linux/timer.h>
J
Joe Perches 已提交
1226
#include <linux/param.h>
A
Arun Sharma 已提交
1227
#include <linux/atomic.h>
L
Linus Torvalds 已提交
1228 1229 1230 1231
#include <linux/inetdevice.h>
#include <net/route.h>

#if 0
J
Joe Perches 已提交
1232
#define pr_debug(format, args...)
L
Linus Torvalds 已提交
1233
/*
1234
  #define pr_debug printk
L
Linus Torvalds 已提交
1235 1236 1237 1238 1239 1240
*/
#endif
#define DEBUG_ARP_TABLE 0

#define LEC_ARP_REFRESH_INTERVAL (3*HZ)

D
David Howells 已提交
1241
static void lec_arp_check_expire(struct work_struct *work);
L
Linus Torvalds 已提交
1242 1243
static void lec_arp_expire_arp(unsigned long data);

1244
/*
L
Linus Torvalds 已提交
1245 1246 1247
 * Arp table funcs
 */

J
Joe Perches 已提交
1248
#define HASH(ch) (ch & (LEC_ARP_TABLE_SIZE - 1))
L
Linus Torvalds 已提交
1249 1250 1251 1252

/*
 * Initialization of arp-cache
 */
1253
static void lec_arp_init(struct lec_priv *priv)
L
Linus Torvalds 已提交
1254
{
1255
	unsigned short i;
L
Linus Torvalds 已提交
1256

J
Joe Perches 已提交
1257
	for (i = 0; i < LEC_ARP_TABLE_SIZE; i++)
1258
		INIT_HLIST_HEAD(&priv->lec_arp_tables[i]);
1259 1260 1261
	INIT_HLIST_HEAD(&priv->lec_arp_empty_ones);
	INIT_HLIST_HEAD(&priv->lec_no_forward);
	INIT_HLIST_HEAD(&priv->mcast_fwds);
L
Linus Torvalds 已提交
1262
	spin_lock_init(&priv->lec_arp_lock);
D
David Howells 已提交
1263
	INIT_DELAYED_WORK(&priv->lec_arp_work, lec_arp_check_expire);
1264
	schedule_delayed_work(&priv->lec_arp_work, LEC_ARP_REFRESH_INTERVAL);
L
Linus Torvalds 已提交
1265 1266
}

1267
static void lec_arp_clear_vccs(struct lec_arp_table *entry)
L
Linus Torvalds 已提交
1268
{
1269
	if (entry->vcc) {
L
Linus Torvalds 已提交
1270 1271
		struct atm_vcc *vcc = entry->vcc;
		struct lec_vcc_priv *vpriv = LEC_VCC_PRIV(vcc);
1272
		struct net_device *dev = (struct net_device *)vcc->proto_data;
L
Linus Torvalds 已提交
1273

1274
		vcc->pop = vpriv->old_pop;
L
Linus Torvalds 已提交
1275 1276 1277 1278
		if (vpriv->xoff)
			netif_wake_queue(dev);
		kfree(vpriv);
		vcc->user_back = NULL;
1279
		vcc->push = entry->old_push;
L
Linus Torvalds 已提交
1280
		vcc_release_async(vcc, -EPIPE);
1281
		entry->vcc = NULL;
1282 1283 1284
	}
	if (entry->recv_vcc) {
		entry->recv_vcc->push = entry->old_recv_push;
L
Linus Torvalds 已提交
1285
		vcc_release_async(entry->recv_vcc, -EPIPE);
1286 1287
		entry->recv_vcc = NULL;
	}
L
Linus Torvalds 已提交
1288 1289 1290 1291 1292 1293
}

/*
 * Insert entry to lec_arp_table
 * LANE2: Add to the end of the list to satisfy 8.1.13
 */
1294
static inline void
1295
lec_arp_add(struct lec_priv *priv, struct lec_arp_table *entry)
L
Linus Torvalds 已提交
1296
{
1297
	struct hlist_head *tmp;
1298

1299 1300
	tmp = &priv->lec_arp_tables[HASH(entry->mac_addr[ETH_ALEN - 1])];
	hlist_add_head(&entry->next, tmp);
1301

1302
	pr_debug("Added entry:%pM\n", entry->mac_addr);
L
Linus Torvalds 已提交
1303 1304 1305 1306 1307
}

/*
 * Remove entry from lec_arp_table
 */
1308 1309
static int
lec_arp_remove(struct lec_priv *priv, struct lec_arp_table *to_remove)
L
Linus Torvalds 已提交
1310
{
1311 1312
	struct lec_arp_table *entry;
	int i, remove_vcc = 1;
1313

J
Joe Perches 已提交
1314
	if (!to_remove)
1315
		return -1;
1316 1317

	hlist_del(&to_remove->next);
1318 1319
	del_timer(&to_remove->timer);

J
Joe Perches 已提交
1320 1321 1322 1323
	/*
	 * If this is the only MAC connected to this VCC,
	 * also tear down the VCC
	 */
1324 1325 1326 1327
	if (to_remove->status >= ESI_FLUSH_PENDING) {
		/*
		 * ESI_FLUSH_PENDING, ESI_FORWARD_DIRECT
		 */
1328
		for (i = 0; i < LEC_ARP_TABLE_SIZE; i++) {
1329
			hlist_for_each_entry(entry,
J
Joe Perches 已提交
1330
					     &priv->lec_arp_tables[i], next) {
1331 1332
				if (memcmp(to_remove->atm_addr,
					   entry->atm_addr, ATM_ESA_LEN) == 0) {
1333 1334 1335 1336 1337 1338 1339 1340 1341 1342
					remove_vcc = 0;
					break;
				}
			}
		}
		if (remove_vcc)
			lec_arp_clear_vccs(to_remove);
	}
	skb_queue_purge(&to_remove->tx_wait);	/* FIXME: good place for this? */

1343
	pr_debug("Removed entry:%pM\n", to_remove->mac_addr);
1344
	return 0;
L
Linus Torvalds 已提交
1345 1346 1347
}

#if DEBUG_ARP_TABLE
1348
static const char *get_status_string(unsigned char st)
L
Linus Torvalds 已提交
1349
{
1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361
	switch (st) {
	case ESI_UNKNOWN:
		return "ESI_UNKNOWN";
	case ESI_ARP_PENDING:
		return "ESI_ARP_PENDING";
	case ESI_VC_PENDING:
		return "ESI_VC_PENDING";
	case ESI_FLUSH_PENDING:
		return "ESI_FLUSH_PENDING";
	case ESI_FORWARD_DIRECT:
		return "ESI_FORWARD_DIRECT";
	}
J
Joe Perches 已提交
1362
	return "<UNKNOWN>";
L
Linus Torvalds 已提交
1363 1364
}

1365
static void dump_arp_table(struct lec_priv *priv)
L
Linus Torvalds 已提交
1366
{
1367
	struct lec_arp_table *rulla;
1368 1369
	char buf[256];
	int i, j, offset;
1370

J
Joe Perches 已提交
1371
	pr_info("Dump %p:\n", priv);
1372
	for (i = 0; i < LEC_ARP_TABLE_SIZE; i++) {
1373
		hlist_for_each_entry(rulla,
J
Joe Perches 已提交
1374
				     &priv->lec_arp_tables[i], next) {
1375 1376
			offset = 0;
			offset += sprintf(buf, "%d: %p\n", i, rulla);
J
Joe Perches 已提交
1377 1378 1379
			offset += sprintf(buf + offset, "Mac: %pM",
					  rulla->mac_addr);
			offset += sprintf(buf + offset, " Atm:");
1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398
			for (j = 0; j < ATM_ESA_LEN; j++) {
				offset += sprintf(buf + offset,
						  "%2.2x ",
						  rulla->atm_addr[j] & 0xff);
			}
			offset += sprintf(buf + offset,
					  "Vcc vpi:%d vci:%d, Recv_vcc vpi:%d vci:%d Last_used:%lx, Timestamp:%lx, No_tries:%d ",
					  rulla->vcc ? rulla->vcc->vpi : 0,
					  rulla->vcc ? rulla->vcc->vci : 0,
					  rulla->recv_vcc ? rulla->recv_vcc->
					  vpi : 0,
					  rulla->recv_vcc ? rulla->recv_vcc->
					  vci : 0, rulla->last_used,
					  rulla->timestamp, rulla->no_tries);
			offset +=
			    sprintf(buf + offset,
				    "Flags:%x, Packets_flooded:%x, Status: %s ",
				    rulla->flags, rulla->packets_flooded,
				    get_status_string(rulla->status));
J
Joe Perches 已提交
1399
			pr_info("%s\n", buf);
1400 1401
		}
	}
1402 1403

	if (!hlist_empty(&priv->lec_no_forward))
J
Joe Perches 已提交
1404
		pr_info("No forward\n");
1405
	hlist_for_each_entry(rulla, &priv->lec_no_forward, next) {
1406
		offset = 0;
J
Joe Perches 已提交
1407 1408
		offset += sprintf(buf + offset, "Mac: %pM", rulla->mac_addr);
		offset += sprintf(buf + offset, " Atm:");
1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424
		for (j = 0; j < ATM_ESA_LEN; j++) {
			offset += sprintf(buf + offset, "%2.2x ",
					  rulla->atm_addr[j] & 0xff);
		}
		offset += sprintf(buf + offset,
				  "Vcc vpi:%d vci:%d, Recv_vcc vpi:%d vci:%d Last_used:%lx, Timestamp:%lx, No_tries:%d ",
				  rulla->vcc ? rulla->vcc->vpi : 0,
				  rulla->vcc ? rulla->vcc->vci : 0,
				  rulla->recv_vcc ? rulla->recv_vcc->vpi : 0,
				  rulla->recv_vcc ? rulla->recv_vcc->vci : 0,
				  rulla->last_used,
				  rulla->timestamp, rulla->no_tries);
		offset += sprintf(buf + offset,
				  "Flags:%x, Packets_flooded:%x, Status: %s ",
				  rulla->flags, rulla->packets_flooded,
				  get_status_string(rulla->status));
J
Joe Perches 已提交
1425
		pr_info("%s\n", buf);
1426
	}
1427 1428

	if (!hlist_empty(&priv->lec_arp_empty_ones))
J
Joe Perches 已提交
1429
		pr_info("Empty ones\n");
1430
	hlist_for_each_entry(rulla, &priv->lec_arp_empty_ones, next) {
1431
		offset = 0;
J
Joe Perches 已提交
1432 1433
		offset += sprintf(buf + offset, "Mac: %pM", rulla->mac_addr);
		offset += sprintf(buf + offset, " Atm:");
1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449
		for (j = 0; j < ATM_ESA_LEN; j++) {
			offset += sprintf(buf + offset, "%2.2x ",
					  rulla->atm_addr[j] & 0xff);
		}
		offset += sprintf(buf + offset,
				  "Vcc vpi:%d vci:%d, Recv_vcc vpi:%d vci:%d Last_used:%lx, Timestamp:%lx, No_tries:%d ",
				  rulla->vcc ? rulla->vcc->vpi : 0,
				  rulla->vcc ? rulla->vcc->vci : 0,
				  rulla->recv_vcc ? rulla->recv_vcc->vpi : 0,
				  rulla->recv_vcc ? rulla->recv_vcc->vci : 0,
				  rulla->last_used,
				  rulla->timestamp, rulla->no_tries);
		offset += sprintf(buf + offset,
				  "Flags:%x, Packets_flooded:%x, Status: %s ",
				  rulla->flags, rulla->packets_flooded,
				  get_status_string(rulla->status));
J
Joe Perches 已提交
1450
		pr_info("%s", buf);
1451 1452
	}

1453
	if (!hlist_empty(&priv->mcast_fwds))
J
Joe Perches 已提交
1454
		pr_info("Multicast Forward VCCs\n");
1455
	hlist_for_each_entry(rulla, &priv->mcast_fwds, next) {
1456
		offset = 0;
J
Joe Perches 已提交
1457 1458
		offset += sprintf(buf + offset, "Mac: %pM", rulla->mac_addr);
		offset += sprintf(buf + offset, " Atm:");
1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474
		for (j = 0; j < ATM_ESA_LEN; j++) {
			offset += sprintf(buf + offset, "%2.2x ",
					  rulla->atm_addr[j] & 0xff);
		}
		offset += sprintf(buf + offset,
				  "Vcc vpi:%d vci:%d, Recv_vcc vpi:%d vci:%d Last_used:%lx, Timestamp:%lx, No_tries:%d ",
				  rulla->vcc ? rulla->vcc->vpi : 0,
				  rulla->vcc ? rulla->vcc->vci : 0,
				  rulla->recv_vcc ? rulla->recv_vcc->vpi : 0,
				  rulla->recv_vcc ? rulla->recv_vcc->vci : 0,
				  rulla->last_used,
				  rulla->timestamp, rulla->no_tries);
		offset += sprintf(buf + offset,
				  "Flags:%x, Packets_flooded:%x, Status: %s ",
				  rulla->flags, rulla->packets_flooded,
				  get_status_string(rulla->status));
J
Joe Perches 已提交
1475
		pr_info("%s\n", buf);
1476
	}
L
Linus Torvalds 已提交
1477 1478

}
1479 1480 1481
#else
#define dump_arp_table(priv) do { } while (0)
#endif
L
Linus Torvalds 已提交
1482 1483 1484 1485

/*
 * Destruction of arp-cache
 */
1486
static void lec_arp_destroy(struct lec_priv *priv)
L
Linus Torvalds 已提交
1487 1488
{
	unsigned long flags;
1489
	struct hlist_node *next;
1490
	struct lec_arp_table *entry;
1491 1492
	int i;

1493
	cancel_delayed_work_sync(&priv->lec_arp_work);
L
Linus Torvalds 已提交
1494

1495 1496 1497
	/*
	 * Remove all entries
	 */
L
Linus Torvalds 已提交
1498 1499

	spin_lock_irqsave(&priv->lec_arp_lock, flags);
1500
	for (i = 0; i < LEC_ARP_TABLE_SIZE; i++) {
1501
		hlist_for_each_entry_safe(entry, next,
J
Joe Perches 已提交
1502
					  &priv->lec_arp_tables[i], next) {
1503
			lec_arp_remove(priv, entry);
1504
			lec_arp_put(entry);
1505
		}
1506
		INIT_HLIST_HEAD(&priv->lec_arp_tables[i]);
1507
	}
1508

1509
	hlist_for_each_entry_safe(entry, next,
J
Joe Perches 已提交
1510
				  &priv->lec_arp_empty_ones, next) {
1511 1512
		del_timer_sync(&entry->timer);
		lec_arp_clear_vccs(entry);
1513
		hlist_del(&entry->next);
1514
		lec_arp_put(entry);
1515
	}
1516 1517
	INIT_HLIST_HEAD(&priv->lec_arp_empty_ones);

1518
	hlist_for_each_entry_safe(entry, next,
J
Joe Perches 已提交
1519
				  &priv->lec_no_forward, next) {
1520 1521
		del_timer_sync(&entry->timer);
		lec_arp_clear_vccs(entry);
1522
		hlist_del(&entry->next);
1523
		lec_arp_put(entry);
1524
	}
1525 1526
	INIT_HLIST_HEAD(&priv->lec_no_forward);

1527
	hlist_for_each_entry_safe(entry, next, &priv->mcast_fwds, next) {
1528 1529
		/* No timer, LANEv2 7.1.20 and 2.3.5.3 */
		lec_arp_clear_vccs(entry);
1530
		hlist_del(&entry->next);
1531
		lec_arp_put(entry);
1532
	}
1533
	INIT_HLIST_HEAD(&priv->mcast_fwds);
1534
	priv->mcast_vcc = NULL;
L
Linus Torvalds 已提交
1535 1536 1537
	spin_unlock_irqrestore(&priv->lec_arp_lock, flags);
}

1538
/*
L
Linus Torvalds 已提交
1539 1540
 * Find entry by mac_address
 */
1541
static struct lec_arp_table *lec_arp_find(struct lec_priv *priv,
1542
					  const unsigned char *mac_addr)
L
Linus Torvalds 已提交
1543
{
1544 1545
	struct hlist_head *head;
	struct lec_arp_table *entry;
1546

1547
	pr_debug("%pM\n", mac_addr);
1548

1549
	head = &priv->lec_arp_tables[HASH(mac_addr[ETH_ALEN - 1])];
1550
	hlist_for_each_entry(entry, head, next) {
1551
		if (ether_addr_equal(mac_addr, entry->mac_addr))
1552
			return entry;
1553 1554
	}
	return NULL;
L
Linus Torvalds 已提交
1555 1556
}

1557
static struct lec_arp_table *make_entry(struct lec_priv *priv,
1558
					const unsigned char *mac_addr)
L
Linus Torvalds 已提交
1559
{
1560 1561 1562 1563
	struct lec_arp_table *to_return;

	to_return = kzalloc(sizeof(struct lec_arp_table), GFP_ATOMIC);
	if (!to_return) {
J
Joe Perches 已提交
1564
		pr_info("LEC: Arp entry kmalloc failed\n");
1565 1566
		return NULL;
	}
J
Joe Perches 已提交
1567
	ether_addr_copy(to_return->mac_addr, mac_addr);
1568
	INIT_HLIST_NODE(&to_return->next);
1569 1570
	setup_timer(&to_return->timer, lec_arp_expire_arp,
			(unsigned long)to_return);
1571 1572 1573
	to_return->last_used = jiffies;
	to_return->priv = priv;
	skb_queue_head_init(&to_return->tx_wait);
1574
	atomic_set(&to_return->usage, 1);
1575
	return to_return;
L
Linus Torvalds 已提交
1576 1577
}

1578 1579
/* Arp sent timer expired */
static void lec_arp_expire_arp(unsigned long data)
L
Linus Torvalds 已提交
1580
{
1581 1582 1583 1584
	struct lec_arp_table *entry;

	entry = (struct lec_arp_table *)data;

1585
	pr_debug("\n");
1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597
	if (entry->status == ESI_ARP_PENDING) {
		if (entry->no_tries <= entry->priv->max_retry_count) {
			if (entry->is_rdesc)
				send_to_lecd(entry->priv, l_rdesc_arp_xmt,
					     entry->mac_addr, NULL, NULL);
			else
				send_to_lecd(entry->priv, l_arp_xmt,
					     entry->mac_addr, NULL, NULL);
			entry->no_tries++;
		}
		mod_timer(&entry->timer, jiffies + (1 * HZ));
	}
L
Linus Torvalds 已提交
1598 1599
}

1600 1601
/* Unknown/unused vcc expire, remove associated entry */
static void lec_arp_expire_vcc(unsigned long data)
L
Linus Torvalds 已提交
1602 1603
{
	unsigned long flags;
1604
	struct lec_arp_table *to_remove = (struct lec_arp_table *)data;
J
Joe Perches 已提交
1605
	struct lec_priv *priv = to_remove->priv;
L
Linus Torvalds 已提交
1606

1607
	del_timer(&to_remove->timer);
L
Linus Torvalds 已提交
1608

1609 1610 1611 1612
	pr_debug("%p %p: vpi:%d vci:%d\n",
		 to_remove, priv,
		 to_remove->vcc ? to_remove->recv_vcc->vpi : 0,
		 to_remove->vcc ? to_remove->recv_vcc->vci : 0);
L
Linus Torvalds 已提交
1613 1614

	spin_lock_irqsave(&priv->lec_arp_lock, flags);
1615
	hlist_del(&to_remove->next);
L
Linus Torvalds 已提交
1616 1617
	spin_unlock_irqrestore(&priv->lec_arp_lock, flags);

1618
	lec_arp_clear_vccs(to_remove);
1619
	lec_arp_put(to_remove);
L
Linus Torvalds 已提交
1620 1621
}

1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665
static bool __lec_arp_check_expire(struct lec_arp_table *entry,
				   unsigned long now,
				   struct lec_priv *priv)
{
	unsigned long time_to_check;

	if ((entry->flags) & LEC_REMOTE_FLAG && priv->topology_change)
		time_to_check = priv->forward_delay_time;
	else
		time_to_check = priv->aging_time;

	pr_debug("About to expire: %lx - %lx > %lx\n",
		 now, entry->last_used, time_to_check);
	if (time_after(now, entry->last_used + time_to_check) &&
	    !(entry->flags & LEC_PERMANENT_FLAG) &&
	    !(entry->mac_addr[0] & 0x01)) {	/* LANE2: 7.1.20 */
		/* Remove entry */
		pr_debug("Entry timed out\n");
		lec_arp_remove(priv, entry);
		lec_arp_put(entry);
	} else {
		/* Something else */
		if ((entry->status == ESI_VC_PENDING ||
		     entry->status == ESI_ARP_PENDING) &&
		    time_after_eq(now, entry->timestamp +
				       priv->max_unknown_frame_time)) {
			entry->timestamp = jiffies;
			entry->packets_flooded = 0;
			if (entry->status == ESI_VC_PENDING)
				send_to_lecd(priv, l_svc_setup,
					     entry->mac_addr,
					     entry->atm_addr,
					     NULL);
		}
		if (entry->status == ESI_FLUSH_PENDING &&
		    time_after_eq(now, entry->timestamp +
				       priv->path_switching_delay)) {
			lec_arp_hold(entry);
			return true;
		}
	}

	return false;
}
L
Linus Torvalds 已提交
1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681
/*
 * Expire entries.
 * 1. Re-set timer
 * 2. For each entry, delete entries that have aged past the age limit.
 * 3. For each entry, depending on the status of the entry, perform
 *    the following maintenance.
 *    a. If status is ESI_VC_PENDING or ESI_ARP_PENDING then if the
 *       tick_count is above the max_unknown_frame_time, clear
 *       the tick_count to zero and clear the packets_flooded counter
 *       to zero. This supports the packet rate limit per address
 *       while flooding unknowns.
 *    b. If the status is ESI_FLUSH_PENDING and the tick_count is greater
 *       than or equal to the path_switching_delay, change the status
 *       to ESI_FORWARD_DIRECT. This causes the flush period to end
 *       regardless of the progress of the flush protocol.
 */
D
David Howells 已提交
1682
static void lec_arp_check_expire(struct work_struct *work)
L
Linus Torvalds 已提交
1683 1684
{
	unsigned long flags;
D
David Howells 已提交
1685 1686
	struct lec_priv *priv =
		container_of(work, struct lec_priv, lec_arp_work.work);
1687
	struct hlist_node *next;
1688
	struct lec_arp_table *entry;
1689 1690 1691
	unsigned long now;
	int i;

1692
	pr_debug("%p\n", priv);
L
Linus Torvalds 已提交
1693
	now = jiffies;
1694
restart:
L
Linus Torvalds 已提交
1695
	spin_lock_irqsave(&priv->lec_arp_lock, flags);
1696
	for (i = 0; i < LEC_ARP_TABLE_SIZE; i++) {
1697
		hlist_for_each_entry_safe(entry, next,
J
Joe Perches 已提交
1698
					  &priv->lec_arp_tables[i], next) {
1699 1700 1701 1702 1703 1704 1705 1706 1707 1708
			if (__lec_arp_check_expire(entry, now, priv)) {
				struct sk_buff *skb;
				struct atm_vcc *vcc = entry->vcc;

				spin_unlock_irqrestore(&priv->lec_arp_lock,
						       flags);
				while ((skb = skb_dequeue(&entry->tx_wait)))
					lec_send(vcc, skb);
				entry->last_used = jiffies;
				entry->status = ESI_FORWARD_DIRECT;
1709
				lec_arp_put(entry);
1710 1711

				goto restart;
L
Linus Torvalds 已提交
1712 1713 1714 1715 1716
			}
		}
	}
	spin_unlock_irqrestore(&priv->lec_arp_lock, flags);

1717
	schedule_delayed_work(&priv->lec_arp_work, LEC_ARP_REFRESH_INTERVAL);
L
Linus Torvalds 已提交
1718
}
1719

L
Linus Torvalds 已提交
1720 1721
/*
 * Try to find vcc where mac_address is attached.
1722
 *
L
Linus Torvalds 已提交
1723
 */
1724
static struct atm_vcc *lec_arp_resolve(struct lec_priv *priv,
J
Joe Perches 已提交
1725 1726
				       const unsigned char *mac_to_find,
				       int is_rdesc,
1727
				       struct lec_arp_table **ret_entry)
L
Linus Torvalds 已提交
1728 1729
{
	unsigned long flags;
1730
	struct lec_arp_table *entry;
L
Linus Torvalds 已提交
1731 1732
	struct atm_vcc *found;

1733 1734 1735 1736 1737
	if (mac_to_find[0] & 0x01) {
		switch (priv->lane_version) {
		case 1:
			return priv->mcast_vcc;
		case 2:	/* LANE2 wants arp for multicast addresses */
1738
			if (ether_addr_equal(mac_to_find, bus_mac))
1739 1740 1741 1742 1743 1744
				return priv->mcast_vcc;
			break;
		default:
			break;
		}
	}
L
Linus Torvalds 已提交
1745 1746

	spin_lock_irqsave(&priv->lec_arp_lock, flags);
1747 1748 1749 1750 1751 1752
	entry = lec_arp_find(priv, mac_to_find);

	if (entry) {
		if (entry->status == ESI_FORWARD_DIRECT) {
			/* Connection Ok */
			entry->last_used = jiffies;
1753
			lec_arp_hold(entry);
1754 1755
			*ret_entry = entry;
			found = entry->vcc;
L
Linus Torvalds 已提交
1756
			goto out;
1757 1758 1759
		}
		/*
		 * If the LE_ARP cache entry is still pending, reset count to 0
1760 1761
		 * so another LE_ARP request can be made for this frame.
		 */
J
Joe Perches 已提交
1762
		if (entry->status == ESI_ARP_PENDING)
1763
			entry->no_tries = 0;
1764 1765 1766 1767 1768 1769 1770 1771 1772 1773
		/*
		 * Data direct VC not yet set up, check to see if the unknown
		 * frame count is greater than the limit. If the limit has
		 * not been reached, allow the caller to send packet to
		 * BUS.
		 */
		if (entry->status != ESI_FLUSH_PENDING &&
		    entry->packets_flooded <
		    priv->maximum_unknown_frame_count) {
			entry->packets_flooded++;
1774
			pr_debug("Flooding..\n");
1775
			found = priv->mcast_vcc;
L
Linus Torvalds 已提交
1776
			goto out;
1777 1778 1779
		}
		/*
		 * We got here because entry->status == ESI_FLUSH_PENDING
L
Linus Torvalds 已提交
1780 1781 1782
		 * or BUS flood limit was reached for an entry which is
		 * in ESI_ARP_PENDING or ESI_VC_PENDING state.
		 */
1783
		lec_arp_hold(entry);
1784
		*ret_entry = entry;
1785 1786
		pr_debug("entry->status %d entry->vcc %p\n", entry->status,
			 entry->vcc);
1787 1788 1789 1790
		found = NULL;
	} else {
		/* No matching entry was found */
		entry = make_entry(priv, mac_to_find);
1791
		pr_debug("Making entry\n");
1792 1793
		if (!entry) {
			found = priv->mcast_vcc;
L
Linus Torvalds 已提交
1794
			goto out;
1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812
		}
		lec_arp_add(priv, entry);
		/* We want arp-request(s) to be sent */
		entry->packets_flooded = 1;
		entry->status = ESI_ARP_PENDING;
		entry->no_tries = 1;
		entry->last_used = entry->timestamp = jiffies;
		entry->is_rdesc = is_rdesc;
		if (entry->is_rdesc)
			send_to_lecd(priv, l_rdesc_arp_xmt, mac_to_find, NULL,
				     NULL);
		else
			send_to_lecd(priv, l_arp_xmt, mac_to_find, NULL, NULL);
		entry->timer.expires = jiffies + (1 * HZ);
		entry->timer.function = lec_arp_expire_arp;
		add_timer(&entry->timer);
		found = priv->mcast_vcc;
	}
L
Linus Torvalds 已提交
1813 1814 1815 1816 1817 1818 1819

out:
	spin_unlock_irqrestore(&priv->lec_arp_lock, flags);
	return found;
}

static int
1820
lec_addr_delete(struct lec_priv *priv, const unsigned char *atm_addr,
1821
		unsigned long permanent)
L
Linus Torvalds 已提交
1822 1823
{
	unsigned long flags;
1824
	struct hlist_node *next;
1825
	struct lec_arp_table *entry;
1826
	int i;
L
Linus Torvalds 已提交
1827

1828
	pr_debug("\n");
L
Linus Torvalds 已提交
1829
	spin_lock_irqsave(&priv->lec_arp_lock, flags);
1830
	for (i = 0; i < LEC_ARP_TABLE_SIZE; i++) {
1831
		hlist_for_each_entry_safe(entry, next,
J
Joe Perches 已提交
1832 1833 1834 1835
					  &priv->lec_arp_tables[i], next) {
			if (!memcmp(atm_addr, entry->atm_addr, ATM_ESA_LEN) &&
			    (permanent ||
			     !(entry->flags & LEC_PERMANENT_FLAG))) {
L
Linus Torvalds 已提交
1836
				lec_arp_remove(priv, entry);
1837
				lec_arp_put(entry);
1838
			}
L
Linus Torvalds 已提交
1839
			spin_unlock_irqrestore(&priv->lec_arp_lock, flags);
1840 1841 1842
			return 0;
		}
	}
L
Linus Torvalds 已提交
1843
	spin_unlock_irqrestore(&priv->lec_arp_lock, flags);
1844
	return -1;
L
Linus Torvalds 已提交
1845 1846 1847
}

/*
1848
 * Notifies:  Response to arp_request (atm_addr != NULL)
L
Linus Torvalds 已提交
1849 1850
 */
static void
1851 1852
lec_arp_update(struct lec_priv *priv, const unsigned char *mac_addr,
	       const unsigned char *atm_addr, unsigned long remoteflag,
1853
	       unsigned int targetless_le_arp)
L
Linus Torvalds 已提交
1854 1855
{
	unsigned long flags;
1856
	struct hlist_node *next;
1857 1858
	struct lec_arp_table *entry, *tmp;
	int i;
L
Linus Torvalds 已提交
1859

1860 1861
	pr_debug("%smac:%pM\n",
		 (targetless_le_arp) ? "targetless " : "", mac_addr);
L
Linus Torvalds 已提交
1862 1863

	spin_lock_irqsave(&priv->lec_arp_lock, flags);
1864 1865 1866 1867 1868 1869
	entry = lec_arp_find(priv, mac_addr);
	if (entry == NULL && targetless_le_arp)
		goto out;	/*
				 * LANE2: ignore targetless LE_ARPs for which
				 * we have no entry in the cache. 7.1.30
				 */
1870
	if (!hlist_empty(&priv->lec_arp_empty_ones)) {
1871
		hlist_for_each_entry_safe(entry, next,
J
Joe Perches 已提交
1872
					  &priv->lec_arp_empty_ones, next) {
1873 1874
			if (memcmp(entry->atm_addr, atm_addr, ATM_ESA_LEN) == 0) {
				hlist_del(&entry->next);
1875
				del_timer(&entry->timer);
1876 1877 1878 1879 1880 1881 1882 1883 1884
				tmp = lec_arp_find(priv, mac_addr);
				if (tmp) {
					del_timer(&tmp->timer);
					tmp->status = ESI_FORWARD_DIRECT;
					memcpy(tmp->atm_addr, atm_addr, ATM_ESA_LEN);
					tmp->vcc = entry->vcc;
					tmp->old_push = entry->old_push;
					tmp->last_used = jiffies;
					del_timer(&entry->timer);
1885
					lec_arp_put(entry);
1886 1887 1888
					entry = tmp;
				} else {
					entry->status = ESI_FORWARD_DIRECT;
J
Joe Perches 已提交
1889 1890
					ether_addr_copy(entry->mac_addr,
							mac_addr);
1891 1892 1893 1894 1895 1896 1897
					entry->last_used = jiffies;
					lec_arp_add(priv, entry);
				}
				if (remoteflag)
					entry->flags |= LEC_REMOTE_FLAG;
				else
					entry->flags &= ~LEC_REMOTE_FLAG;
1898
				pr_debug("After update\n");
1899 1900
				dump_arp_table(priv);
				goto out;
1901 1902 1903
			}
		}
	}
1904

1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916
	entry = lec_arp_find(priv, mac_addr);
	if (!entry) {
		entry = make_entry(priv, mac_addr);
		if (!entry)
			goto out;
		entry->status = ESI_UNKNOWN;
		lec_arp_add(priv, entry);
		/* Temporary, changes before end of function */
	}
	memcpy(entry->atm_addr, atm_addr, ATM_ESA_LEN);
	del_timer(&entry->timer);
	for (i = 0; i < LEC_ARP_TABLE_SIZE; i++) {
1917
		hlist_for_each_entry(tmp,
J
Joe Perches 已提交
1918
				     &priv->lec_arp_tables[i], next) {
1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940
			if (entry != tmp &&
			    !memcmp(tmp->atm_addr, atm_addr, ATM_ESA_LEN)) {
				/* Vcc to this host exists */
				if (tmp->status > ESI_VC_PENDING) {
					/*
					 * ESI_FLUSH_PENDING,
					 * ESI_FORWARD_DIRECT
					 */
					entry->vcc = tmp->vcc;
					entry->old_push = tmp->old_push;
				}
				entry->status = tmp->status;
				break;
			}
		}
	}
	if (remoteflag)
		entry->flags |= LEC_REMOTE_FLAG;
	else
		entry->flags &= ~LEC_REMOTE_FLAG;
	if (entry->status == ESI_ARP_PENDING || entry->status == ESI_UNKNOWN) {
		entry->status = ESI_VC_PENDING;
1941
		send_to_lecd(priv, l_svc_setup, entry->mac_addr, atm_addr, NULL);
1942
	}
1943
	pr_debug("After update2\n");
1944
	dump_arp_table(priv);
L
Linus Torvalds 已提交
1945 1946 1947 1948 1949
out:
	spin_unlock_irqrestore(&priv->lec_arp_lock, flags);
}

/*
1950
 * Notifies: Vcc setup ready
L
Linus Torvalds 已提交
1951 1952
 */
static void
1953
lec_vcc_added(struct lec_priv *priv, const struct atmlec_ioc *ioc_data,
1954 1955
	      struct atm_vcc *vcc,
	      void (*old_push) (struct atm_vcc *vcc, struct sk_buff *skb))
L
Linus Torvalds 已提交
1956 1957
{
	unsigned long flags;
1958 1959
	struct lec_arp_table *entry;
	int i, found_entry = 0;
L
Linus Torvalds 已提交
1960 1961

	spin_lock_irqsave(&priv->lec_arp_lock, flags);
J
Joe Perches 已提交
1962
	/* Vcc for Multicast Forward. No timer, LANEv2 7.1.20 and 2.3.5.3 */
1963
	if (ioc_data->receive == 2) {
1964
		pr_debug("LEC_ARP: Attaching mcast forward\n");
L
Linus Torvalds 已提交
1965
#if 0
1966 1967
		entry = lec_arp_find(priv, bus_mac);
		if (!entry) {
J
Joe Perches 已提交
1968
			pr_info("LEC_ARP: Multicast entry not found!\n");
L
Linus Torvalds 已提交
1969
			goto out;
1970 1971 1972 1973
		}
		memcpy(entry->atm_addr, ioc_data->atm_addr, ATM_ESA_LEN);
		entry->recv_vcc = vcc;
		entry->old_recv_push = old_push;
L
Linus Torvalds 已提交
1974
#endif
1975 1976
		entry = make_entry(priv, bus_mac);
		if (entry == NULL)
L
Linus Torvalds 已提交
1977
			goto out;
1978 1979 1980 1981
		del_timer(&entry->timer);
		memcpy(entry->atm_addr, ioc_data->atm_addr, ATM_ESA_LEN);
		entry->recv_vcc = vcc;
		entry->old_recv_push = old_push;
1982
		hlist_add_head(&entry->next, &priv->mcast_fwds);
1983 1984 1985 1986 1987 1988
		goto out;
	} else if (ioc_data->receive == 1) {
		/*
		 * Vcc which we don't want to make default vcc,
		 * attach it anyway.
		 */
1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999
		pr_debug("LEC_ARP:Attaching data direct, not default: %2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x\n",
			 ioc_data->atm_addr[0], ioc_data->atm_addr[1],
			 ioc_data->atm_addr[2], ioc_data->atm_addr[3],
			 ioc_data->atm_addr[4], ioc_data->atm_addr[5],
			 ioc_data->atm_addr[6], ioc_data->atm_addr[7],
			 ioc_data->atm_addr[8], ioc_data->atm_addr[9],
			 ioc_data->atm_addr[10], ioc_data->atm_addr[11],
			 ioc_data->atm_addr[12], ioc_data->atm_addr[13],
			 ioc_data->atm_addr[14], ioc_data->atm_addr[15],
			 ioc_data->atm_addr[16], ioc_data->atm_addr[17],
			 ioc_data->atm_addr[18], ioc_data->atm_addr[19]);
2000 2001
		entry = make_entry(priv, bus_mac);
		if (entry == NULL)
L
Linus Torvalds 已提交
2002
			goto out;
2003
		memcpy(entry->atm_addr, ioc_data->atm_addr, ATM_ESA_LEN);
2004
		eth_zero_addr(entry->mac_addr);
2005 2006 2007 2008 2009
		entry->recv_vcc = vcc;
		entry->old_recv_push = old_push;
		entry->status = ESI_UNKNOWN;
		entry->timer.expires = jiffies + priv->vcc_timeout_period;
		entry->timer.function = lec_arp_expire_vcc;
2010
		hlist_add_head(&entry->next, &priv->lec_no_forward);
2011
		add_timer(&entry->timer);
L
Linus Torvalds 已提交
2012 2013
		dump_arp_table(priv);
		goto out;
2014
	}
2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025
	pr_debug("LEC_ARP:Attaching data direct, default: %2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x\n",
		 ioc_data->atm_addr[0], ioc_data->atm_addr[1],
		 ioc_data->atm_addr[2], ioc_data->atm_addr[3],
		 ioc_data->atm_addr[4], ioc_data->atm_addr[5],
		 ioc_data->atm_addr[6], ioc_data->atm_addr[7],
		 ioc_data->atm_addr[8], ioc_data->atm_addr[9],
		 ioc_data->atm_addr[10], ioc_data->atm_addr[11],
		 ioc_data->atm_addr[12], ioc_data->atm_addr[13],
		 ioc_data->atm_addr[14], ioc_data->atm_addr[15],
		 ioc_data->atm_addr[16], ioc_data->atm_addr[17],
		 ioc_data->atm_addr[18], ioc_data->atm_addr[19]);
2026
	for (i = 0; i < LEC_ARP_TABLE_SIZE; i++) {
2027
		hlist_for_each_entry(entry,
J
Joe Perches 已提交
2028
				     &priv->lec_arp_tables[i], next) {
2029 2030 2031
			if (memcmp
			    (ioc_data->atm_addr, entry->atm_addr,
			     ATM_ESA_LEN) == 0) {
2032 2033
				pr_debug("LEC_ARP: Attaching data direct\n");
				pr_debug("Currently -> Vcc: %d, Rvcc:%d\n",
2034 2035 2036
					 entry->vcc ? entry->vcc->vci : 0,
					 entry->recv_vcc ? entry->recv_vcc->
					 vci : 0);
2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049
				found_entry = 1;
				del_timer(&entry->timer);
				entry->vcc = vcc;
				entry->old_push = old_push;
				if (entry->status == ESI_VC_PENDING) {
					if (priv->maximum_unknown_frame_count
					    == 0)
						entry->status =
						    ESI_FORWARD_DIRECT;
					else {
						entry->timestamp = jiffies;
						entry->status =
						    ESI_FLUSH_PENDING;
L
Linus Torvalds 已提交
2050
#if 0
2051 2052 2053 2054
						send_to_lecd(priv, l_flush_xmt,
							     NULL,
							     entry->atm_addr,
							     NULL);
L
Linus Torvalds 已提交
2055
#endif
2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074
					}
				} else {
					/*
					 * They were forming a connection
					 * to us, and we to them. Our
					 * ATM address is numerically lower
					 * than theirs, so we make connection
					 * we formed into default VCC (8.1.11).
					 * Connection they made gets torn
					 * down. This might confuse some
					 * clients. Can be changed if
					 * someone reports trouble...
					 */
					;
				}
			}
		}
	}
	if (found_entry) {
2075
		pr_debug("After vcc was added\n");
2076
		dump_arp_table(priv);
L
Linus Torvalds 已提交
2077
		goto out;
2078 2079 2080 2081 2082 2083 2084
	}
	/*
	 * Not found, snatch address from first data packet that arrives
	 * from this vcc
	 */
	entry = make_entry(priv, bus_mac);
	if (!entry)
L
Linus Torvalds 已提交
2085
		goto out;
2086 2087 2088
	entry->vcc = vcc;
	entry->old_push = old_push;
	memcpy(entry->atm_addr, ioc_data->atm_addr, ATM_ESA_LEN);
2089
	eth_zero_addr(entry->mac_addr);
2090
	entry->status = ESI_UNKNOWN;
2091
	hlist_add_head(&entry->next, &priv->lec_arp_empty_ones);
2092 2093 2094
	entry->timer.expires = jiffies + priv->vcc_timeout_period;
	entry->timer.function = lec_arp_expire_vcc;
	add_timer(&entry->timer);
2095
	pr_debug("After vcc was added\n");
L
Linus Torvalds 已提交
2096 2097 2098 2099 2100
	dump_arp_table(priv);
out:
	spin_unlock_irqrestore(&priv->lec_arp_lock, flags);
}

2101
static void lec_flush_complete(struct lec_priv *priv, unsigned long tran_id)
L
Linus Torvalds 已提交
2102 2103
{
	unsigned long flags;
2104 2105
	struct lec_arp_table *entry;
	int i;
L
Linus Torvalds 已提交
2106

2107
	pr_debug("%lx\n", tran_id);
2108
restart:
2109 2110
	spin_lock_irqsave(&priv->lec_arp_lock, flags);
	for (i = 0; i < LEC_ARP_TABLE_SIZE; i++) {
2111
		hlist_for_each_entry(entry,
J
Joe Perches 已提交
2112 2113 2114
				     &priv->lec_arp_tables[i], next) {
			if (entry->flush_tran_id == tran_id &&
			    entry->status == ESI_FLUSH_PENDING) {
2115
				struct sk_buff *skb;
2116
				struct atm_vcc *vcc = entry->vcc;
2117

2118
				lec_arp_hold(entry);
J
Joe Perches 已提交
2119 2120
				spin_unlock_irqrestore(&priv->lec_arp_lock,
						       flags);
2121
				while ((skb = skb_dequeue(&entry->tx_wait)))
2122
					lec_send(vcc, skb);
2123
				entry->last_used = jiffies;
2124
				entry->status = ESI_FORWARD_DIRECT;
2125
				lec_arp_put(entry);
2126
				pr_debug("LEC_ARP: Flushed\n");
2127
				goto restart;
2128 2129 2130
			}
		}
	}
L
Linus Torvalds 已提交
2131
	spin_unlock_irqrestore(&priv->lec_arp_lock, flags);
2132
	dump_arp_table(priv);
L
Linus Torvalds 已提交
2133 2134 2135 2136
}

static void
lec_set_flush_tran_id(struct lec_priv *priv,
2137
		      const unsigned char *atm_addr, unsigned long tran_id)
L
Linus Torvalds 已提交
2138 2139
{
	unsigned long flags;
2140 2141
	struct lec_arp_table *entry;
	int i;
L
Linus Torvalds 已提交
2142 2143

	spin_lock_irqsave(&priv->lec_arp_lock, flags);
2144
	for (i = 0; i < LEC_ARP_TABLE_SIZE; i++)
2145
		hlist_for_each_entry(entry,
J
Joe Perches 已提交
2146
				     &priv->lec_arp_tables[i], next) {
2147 2148
			if (!memcmp(atm_addr, entry->atm_addr, ATM_ESA_LEN)) {
				entry->flush_tran_id = tran_id;
2149
				pr_debug("Set flush transaction id to %lx for %p\n",
2150
					 tran_id, entry);
2151
			}
2152
		}
L
Linus Torvalds 已提交
2153 2154 2155
	spin_unlock_irqrestore(&priv->lec_arp_lock, flags);
}

2156
static int lec_mcast_make(struct lec_priv *priv, struct atm_vcc *vcc)
L
Linus Torvalds 已提交
2157 2158
{
	unsigned long flags;
2159 2160 2161 2162
	unsigned char mac_addr[] = {
		0xff, 0xff, 0xff, 0xff, 0xff, 0xff
	};
	struct lec_arp_table *to_add;
L
Linus Torvalds 已提交
2163 2164
	struct lec_vcc_priv *vpriv;
	int err = 0;
2165

J
Joe Perches 已提交
2166 2167
	vpriv = kmalloc(sizeof(struct lec_vcc_priv), GFP_KERNEL);
	if (!vpriv)
L
Linus Torvalds 已提交
2168 2169 2170 2171
		return -ENOMEM;
	vpriv->xoff = 0;
	vpriv->old_pop = vcc->pop;
	vcc->user_back = vpriv;
2172
	vcc->pop = lec_pop;
L
Linus Torvalds 已提交
2173
	spin_lock_irqsave(&priv->lec_arp_lock, flags);
2174 2175
	to_add = make_entry(priv, mac_addr);
	if (!to_add) {
L
Linus Torvalds 已提交
2176 2177
		vcc->pop = vpriv->old_pop;
		kfree(vpriv);
2178
		err = -ENOMEM;
L
Linus Torvalds 已提交
2179
		goto out;
2180 2181 2182 2183 2184 2185 2186 2187 2188
	}
	memcpy(to_add->atm_addr, vcc->remote.sas_addr.prv, ATM_ESA_LEN);
	to_add->status = ESI_FORWARD_DIRECT;
	to_add->flags |= LEC_PERMANENT_FLAG;
	to_add->vcc = vcc;
	to_add->old_push = vcc->push;
	vcc->push = lec_push;
	priv->mcast_vcc = vcc;
	lec_arp_add(priv, to_add);
L
Linus Torvalds 已提交
2189 2190
out:
	spin_unlock_irqrestore(&priv->lec_arp_lock, flags);
2191
	return err;
L
Linus Torvalds 已提交
2192 2193
}

2194
static void lec_vcc_close(struct lec_priv *priv, struct atm_vcc *vcc)
L
Linus Torvalds 已提交
2195 2196
{
	unsigned long flags;
2197
	struct hlist_node *next;
2198
	struct lec_arp_table *entry;
2199
	int i;
L
Linus Torvalds 已提交
2200

2201
	pr_debug("LEC_ARP: lec_vcc_close vpi:%d vci:%d\n", vcc->vpi, vcc->vci);
2202
	dump_arp_table(priv);
2203

L
Linus Torvalds 已提交
2204
	spin_lock_irqsave(&priv->lec_arp_lock, flags);
2205

2206
	for (i = 0; i < LEC_ARP_TABLE_SIZE; i++) {
2207
		hlist_for_each_entry_safe(entry, next,
J
Joe Perches 已提交
2208
					  &priv->lec_arp_tables[i], next) {
2209 2210
			if (vcc == entry->vcc) {
				lec_arp_remove(priv, entry);
2211
				lec_arp_put(entry);
J
Joe Perches 已提交
2212
				if (priv->mcast_vcc == vcc)
2213 2214 2215 2216 2217
					priv->mcast_vcc = NULL;
			}
		}
	}

2218
	hlist_for_each_entry_safe(entry, next,
J
Joe Perches 已提交
2219
				  &priv->lec_arp_empty_ones, next) {
2220
		if (entry->vcc == vcc) {
2221 2222
			lec_arp_clear_vccs(entry);
			del_timer(&entry->timer);
2223
			hlist_del(&entry->next);
2224
			lec_arp_put(entry);
2225 2226 2227
		}
	}

2228
	hlist_for_each_entry_safe(entry, next,
J
Joe Perches 已提交
2229
				  &priv->lec_no_forward, next) {
2230 2231 2232
		if (entry->recv_vcc == vcc) {
			lec_arp_clear_vccs(entry);
			del_timer(&entry->timer);
2233
			hlist_del(&entry->next);
2234
			lec_arp_put(entry);
2235 2236 2237
		}
	}

2238
	hlist_for_each_entry_safe(entry, next, &priv->mcast_fwds, next) {
2239 2240 2241
		if (entry->recv_vcc == vcc) {
			lec_arp_clear_vccs(entry);
			/* No timer, LANEv2 7.1.20 and 2.3.5.3 */
2242
			hlist_del(&entry->next);
2243
			lec_arp_put(entry);
2244 2245
		}
	}
L
Linus Torvalds 已提交
2246 2247 2248 2249 2250 2251 2252

	spin_unlock_irqrestore(&priv->lec_arp_lock, flags);
	dump_arp_table(priv);
}

static void
lec_arp_check_empties(struct lec_priv *priv,
2253
		      struct atm_vcc *vcc, struct sk_buff *skb)
L
Linus Torvalds 已提交
2254
{
2255
	unsigned long flags;
2256
	struct hlist_node *next;
2257
	struct lec_arp_table *entry, *tmp;
2258
	struct lecdatahdr_8023 *hdr = (struct lecdatahdr_8023 *)skb->data;
2259
	unsigned char *src = hdr->h_source;
L
Linus Torvalds 已提交
2260 2261

	spin_lock_irqsave(&priv->lec_arp_lock, flags);
2262
	hlist_for_each_entry_safe(entry, next,
J
Joe Perches 已提交
2263
				  &priv->lec_arp_empty_ones, next) {
2264 2265
		if (vcc == entry->vcc) {
			del_timer(&entry->timer);
J
Joe Perches 已提交
2266
			ether_addr_copy(entry->mac_addr, src);
2267 2268 2269
			entry->status = ESI_FORWARD_DIRECT;
			entry->last_used = jiffies;
			/* We might have got an entry */
J
Joe Perches 已提交
2270 2271
			tmp = lec_arp_find(priv, src);
			if (tmp) {
2272
				lec_arp_remove(priv, tmp);
2273
				lec_arp_put(tmp);
2274 2275 2276 2277
			}
			hlist_del(&entry->next);
			lec_arp_add(priv, entry);
			goto out;
2278 2279
		}
	}
2280
	pr_debug("LEC_ARP: Arp_check_empties: entry not found!\n");
L
Linus Torvalds 已提交
2281 2282 2283
out:
	spin_unlock_irqrestore(&priv->lec_arp_lock, flags);
}
2284

L
Linus Torvalds 已提交
2285
MODULE_LICENSE("GPL");