xref: /linux/net/atm/lec.c (revision 5c8013ae2e86ec36b07500ba4cacb14ab4d6f728)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * lec.c: Lan Emulation driver
4  *
5  * Marko Kiiskila <mkiiskila@yahoo.com>
6  */
7 
8 #define pr_fmt(fmt) KBUILD_MODNAME ":%s: " fmt, __func__
9 
10 #include <linux/slab.h>
11 #include <linux/kernel.h>
12 #include <linux/bitops.h>
13 #include <linux/capability.h>
14 
15 /* We are ethernet device */
16 #include <linux/if_ether.h>
17 #include <linux/netdevice.h>
18 #include <linux/etherdevice.h>
19 #include <net/sock.h>
20 #include <linux/skbuff.h>
21 #include <linux/ip.h>
22 #include <asm/byteorder.h>
23 #include <linux/uaccess.h>
24 #include <net/arp.h>
25 #include <net/dst.h>
26 #include <linux/proc_fs.h>
27 #include <linux/spinlock.h>
28 #include <linux/seq_file.h>
29 
30 /* And atm device */
31 #include <linux/atmdev.h>
32 #include <linux/atmlec.h>
33 
34 /* Proxy LEC knows about bridging */
35 #if IS_ENABLED(CONFIG_BRIDGE)
36 #include "../bridge/br_private.h"
37 
38 static unsigned char bridge_ula_lec[] = { 0x01, 0x80, 0xc2, 0x00, 0x00 };
39 #endif
40 
41 /* Modular too */
42 #include <linux/module.h>
43 #include <linux/init.h>
44 
45 /* Hardening for Spectre-v1 */
46 #include <linux/nospec.h>
47 
48 #include "lec.h"
49 #include "lec_arpc.h"
50 #include "resources.h"
51 
52 #define DUMP_PACKETS 0		/*
53 				 * 0 = None,
54 				 * 1 = 30 first bytes
55 				 * 2 = Whole packet
56 				 */
57 
58 #define LEC_UNRES_QUE_LEN 8	/*
59 				 * number of tx packets to queue for a
60 				 * single destination while waiting for SVC
61 				 */
62 
63 static int lec_open(struct net_device *dev);
64 static netdev_tx_t lec_start_xmit(struct sk_buff *skb,
65 				  struct net_device *dev);
66 static int lec_close(struct net_device *dev);
67 static struct lec_arp_table *lec_arp_find(struct lec_priv *priv,
68 					  const unsigned char *mac_addr);
69 static int lec_arp_remove(struct lec_priv *priv,
70 			  struct lec_arp_table *to_remove);
71 /* LANE2 functions */
72 static void lane2_associate_ind(struct net_device *dev, const u8 *mac_address,
73 				const u8 *tlvs, u32 sizeoftlvs);
74 static int lane2_resolve(struct net_device *dev, const u8 *dst_mac, int force,
75 			 u8 **tlvs, u32 *sizeoftlvs);
76 static int lane2_associate_req(struct net_device *dev, const u8 *lan_dst,
77 			       const u8 *tlvs, u32 sizeoftlvs);
78 
79 static int lec_addr_delete(struct lec_priv *priv, const unsigned char *atm_addr,
80 			   unsigned long permanent);
81 static void lec_arp_check_empties(struct lec_priv *priv,
82 				  struct atm_vcc *vcc, struct sk_buff *skb);
83 static void lec_arp_destroy(struct lec_priv *priv);
84 static void lec_arp_init(struct lec_priv *priv);
85 static struct atm_vcc *lec_arp_resolve(struct lec_priv *priv,
86 				       const unsigned char *mac_to_find,
87 				       int is_rdesc,
88 				       struct lec_arp_table **ret_entry);
89 static void lec_arp_update(struct lec_priv *priv, const unsigned char *mac_addr,
90 			   const unsigned char *atm_addr,
91 			   unsigned long remoteflag,
92 			   unsigned int targetless_le_arp);
93 static void lec_flush_complete(struct lec_priv *priv, unsigned long tran_id);
94 static int lec_mcast_make(struct lec_priv *priv, struct atm_vcc *vcc);
95 static void lec_set_flush_tran_id(struct lec_priv *priv,
96 				  const unsigned char *atm_addr,
97 				  unsigned long tran_id);
98 static void lec_vcc_added(struct lec_priv *priv,
99 			  const struct atmlec_ioc *ioc_data,
100 			  struct atm_vcc *vcc,
101 			  void (*old_push)(struct atm_vcc *vcc,
102 					   struct sk_buff *skb));
103 static void lec_vcc_close(struct lec_priv *priv, struct atm_vcc *vcc);
104 
105 /* must be done under lec_arp_lock */
lec_arp_hold(struct lec_arp_table * entry)106 static inline void lec_arp_hold(struct lec_arp_table *entry)
107 {
108 	refcount_inc(&entry->usage);
109 }
110 
lec_arp_put(struct lec_arp_table * entry)111 static inline void lec_arp_put(struct lec_arp_table *entry)
112 {
113 	if (refcount_dec_and_test(&entry->usage))
114 		kfree(entry);
115 }
116 
117 static struct lane2_ops lane2_ops = {
118 	.resolve = lane2_resolve,		/* spec 3.1.3 */
119 	.associate_req = lane2_associate_req,	/* spec 3.1.4 */
120 	.associate_indicator = NULL             /* spec 3.1.5 */
121 };
122 
123 static unsigned char bus_mac[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
124 
125 /* Device structures */
126 static struct net_device *dev_lec[MAX_LEC_ITF];
127 static DEFINE_MUTEX(lec_mutex);
128 
129 #if IS_ENABLED(CONFIG_BRIDGE)
lec_handle_bridge(struct sk_buff * skb,struct net_device * dev)130 static void lec_handle_bridge(struct sk_buff *skb, struct net_device *dev)
131 {
132 	char *buff;
133 	struct lec_priv *priv;
134 
135 	/*
136 	 * Check if this is a BPDU. If so, ask zeppelin to send
137 	 * LE_TOPOLOGY_REQUEST with the same value of Topology Change bit
138 	 * as the Config BPDU has
139 	 */
140 	buff = skb->data + skb->dev->hard_header_len;
141 	if (*buff++ == 0x42 && *buff++ == 0x42 && *buff++ == 0x03) {
142 		struct sock *sk;
143 		struct sk_buff *skb2;
144 		struct atmlec_msg *mesg;
145 
146 		skb2 = alloc_skb(sizeof(struct atmlec_msg), GFP_ATOMIC);
147 		if (skb2 == NULL)
148 			return;
149 		skb2->len = sizeof(struct atmlec_msg);
150 		mesg = (struct atmlec_msg *)skb2->data;
151 		mesg->type = l_topology_change;
152 		buff += 4;
153 		mesg->content.normal.flag = *buff & 0x01;
154 					/* 0x01 is topology change */
155 
156 		priv = netdev_priv(dev);
157 		atm_force_charge(priv->lecd, skb2->truesize);
158 		sk = sk_atm(priv->lecd);
159 		skb_queue_tail(&sk->sk_receive_queue, skb2);
160 		sk->sk_data_ready(sk);
161 	}
162 }
163 #endif /* IS_ENABLED(CONFIG_BRIDGE) */
164 
165 /*
166  * Open/initialize the netdevice. This is called (in the current kernel)
167  * sometime after booting when the 'ifconfig' program is run.
168  *
169  * This routine should set everything up anew at each open, even
170  * registers that "should" only need to be set once at boot, so that
171  * there is non-reboot way to recover if something goes wrong.
172  */
173 
lec_open(struct net_device * dev)174 static int lec_open(struct net_device *dev)
175 {
176 	netif_start_queue(dev);
177 
178 	return 0;
179 }
180 
181 static void
lec_send(struct atm_vcc * vcc,struct sk_buff * skb)182 lec_send(struct atm_vcc *vcc, struct sk_buff *skb)
183 {
184 	struct net_device *dev = skb->dev;
185 	unsigned int len = skb->len;
186 
187 	ATM_SKB(skb)->vcc = vcc;
188 	atm_account_tx(vcc, skb);
189 
190 	if (vcc->send(vcc, skb) < 0) {
191 		dev->stats.tx_dropped++;
192 		return;
193 	}
194 
195 	dev->stats.tx_packets++;
196 	dev->stats.tx_bytes += len;
197 }
198 
lec_tx_timeout(struct net_device * dev,unsigned int txqueue)199 static void lec_tx_timeout(struct net_device *dev, unsigned int txqueue)
200 {
201 	pr_info("%s\n", dev->name);
202 	netif_trans_update(dev);
203 	netif_wake_queue(dev);
204 }
205 
lec_start_xmit(struct sk_buff * skb,struct net_device * dev)206 static netdev_tx_t lec_start_xmit(struct sk_buff *skb,
207 				  struct net_device *dev)
208 {
209 	struct sk_buff *skb2;
210 	struct lec_priv *priv = netdev_priv(dev);
211 	struct lecdatahdr_8023 *lec_h;
212 	struct atm_vcc *vcc;
213 	struct lec_arp_table *entry;
214 	unsigned char *dst;
215 	int min_frame_size;
216 	int is_rdesc;
217 
218 	pr_debug("called\n");
219 	if (!priv->lecd) {
220 		pr_info("%s:No lecd attached\n", dev->name);
221 		dev->stats.tx_errors++;
222 		netif_stop_queue(dev);
223 		kfree_skb(skb);
224 		return NETDEV_TX_OK;
225 	}
226 
227 	pr_debug("skbuff head:%lx data:%lx tail:%lx end:%lx\n",
228 		 (long)skb->head, (long)skb->data, (long)skb_tail_pointer(skb),
229 		 (long)skb_end_pointer(skb));
230 #if IS_ENABLED(CONFIG_BRIDGE)
231 	if (memcmp(skb->data, bridge_ula_lec, sizeof(bridge_ula_lec)) == 0)
232 		lec_handle_bridge(skb, dev);
233 #endif
234 
235 	/* Make sure we have room for lec_id */
236 	if (skb_headroom(skb) < 2) {
237 		pr_debug("reallocating skb\n");
238 		skb2 = skb_realloc_headroom(skb, LEC_HEADER_LEN);
239 		if (unlikely(!skb2)) {
240 			kfree_skb(skb);
241 			return NETDEV_TX_OK;
242 		}
243 		consume_skb(skb);
244 		skb = skb2;
245 	}
246 	skb_push(skb, 2);
247 
248 	/* Put le header to place */
249 	lec_h = (struct lecdatahdr_8023 *)skb->data;
250 	lec_h->le_header = htons(priv->lecid);
251 
252 #if DUMP_PACKETS >= 2
253 #define MAX_DUMP_SKB 99
254 #elif DUMP_PACKETS >= 1
255 #define MAX_DUMP_SKB 30
256 #endif
257 #if DUMP_PACKETS >= 1
258 	printk(KERN_DEBUG "%s: send datalen:%ld lecid:%4.4x\n",
259 	       dev->name, skb->len, priv->lecid);
260 	print_hex_dump(KERN_DEBUG, "", DUMP_OFFSET, 16, 1,
261 		       skb->data, min(skb->len, MAX_DUMP_SKB), true);
262 #endif /* DUMP_PACKETS >= 1 */
263 
264 	/* Minimum ethernet-frame size */
265 	min_frame_size = LEC_MINIMUM_8023_SIZE;
266 	if (skb->len < min_frame_size) {
267 		if ((skb->len + skb_tailroom(skb)) < min_frame_size) {
268 			skb2 = skb_copy_expand(skb, 0,
269 					       min_frame_size - skb->truesize,
270 					       GFP_ATOMIC);
271 			dev_kfree_skb(skb);
272 			if (skb2 == NULL) {
273 				dev->stats.tx_dropped++;
274 				return NETDEV_TX_OK;
275 			}
276 			skb = skb2;
277 		}
278 		skb_put(skb, min_frame_size - skb->len);
279 	}
280 
281 	/* Send to right vcc */
282 	is_rdesc = 0;
283 	dst = lec_h->h_dest;
284 	entry = NULL;
285 	vcc = lec_arp_resolve(priv, dst, is_rdesc, &entry);
286 	pr_debug("%s:vcc:%p vcc_flags:%lx, entry:%p\n",
287 		 dev->name, vcc, vcc ? vcc->flags : 0, entry);
288 	if (!vcc || !test_bit(ATM_VF_READY, &vcc->flags)) {
289 		if (entry && (entry->tx_wait.qlen < LEC_UNRES_QUE_LEN)) {
290 			pr_debug("%s:queuing packet, MAC address %pM\n",
291 				 dev->name, lec_h->h_dest);
292 			skb_queue_tail(&entry->tx_wait, skb);
293 		} else {
294 			pr_debug("%s:tx queue full or no arp entry, dropping, MAC address: %pM\n",
295 				 dev->name, lec_h->h_dest);
296 			dev->stats.tx_dropped++;
297 			dev_kfree_skb(skb);
298 		}
299 		goto out;
300 	}
301 #if DUMP_PACKETS > 0
302 	printk(KERN_DEBUG "%s:sending to vpi:%d vci:%d\n",
303 	       dev->name, vcc->vpi, vcc->vci);
304 #endif /* DUMP_PACKETS > 0 */
305 
306 	while (entry && (skb2 = skb_dequeue(&entry->tx_wait))) {
307 		pr_debug("emptying tx queue, MAC address %pM\n", lec_h->h_dest);
308 		lec_send(vcc, skb2);
309 	}
310 
311 	lec_send(vcc, skb);
312 
313 	if (!atm_may_send(vcc, 0)) {
314 		struct lec_vcc_priv *vpriv = LEC_VCC_PRIV(vcc);
315 
316 		vpriv->xoff = 1;
317 		netif_stop_queue(dev);
318 
319 		/*
320 		 * vcc->pop() might have occurred in between, making
321 		 * the vcc usuable again.  Since xmit is serialized,
322 		 * this is the only situation we have to re-test.
323 		 */
324 
325 		if (atm_may_send(vcc, 0))
326 			netif_wake_queue(dev);
327 	}
328 
329 out:
330 	if (entry)
331 		lec_arp_put(entry);
332 	netif_trans_update(dev);
333 	return NETDEV_TX_OK;
334 }
335 
336 /* The inverse routine to net_open(). */
lec_close(struct net_device * dev)337 static int lec_close(struct net_device *dev)
338 {
339 	netif_stop_queue(dev);
340 	return 0;
341 }
342 
lec_atm_send(struct atm_vcc * vcc,struct sk_buff * skb)343 static int lec_atm_send(struct atm_vcc *vcc, struct sk_buff *skb)
344 {
345 	static const u8 zero_addr[ETH_ALEN] = {};
346 	unsigned long flags;
347 	struct net_device *dev = (struct net_device *)vcc->proto_data;
348 	struct lec_priv *priv = netdev_priv(dev);
349 	struct atmlec_msg *mesg;
350 	struct lec_arp_table *entry;
351 	char *tmp;		/* FIXME */
352 
353 	WARN_ON(refcount_sub_and_test(skb->truesize, &sk_atm(vcc)->sk_wmem_alloc));
354 	mesg = (struct atmlec_msg *)skb->data;
355 	tmp = skb->data;
356 	tmp += sizeof(struct atmlec_msg);
357 	pr_debug("%s: msg from zeppelin:%d\n", dev->name, mesg->type);
358 	switch (mesg->type) {
359 	case l_set_mac_addr:
360 		eth_hw_addr_set(dev, mesg->content.normal.mac_addr);
361 		break;
362 	case l_del_mac_addr:
363 		eth_hw_addr_set(dev, zero_addr);
364 		break;
365 	case l_addr_delete:
366 		lec_addr_delete(priv, mesg->content.normal.atm_addr,
367 				mesg->content.normal.flag);
368 		break;
369 	case l_topology_change:
370 		priv->topology_change = mesg->content.normal.flag;
371 		break;
372 	case l_flush_complete:
373 		lec_flush_complete(priv, mesg->content.normal.flag);
374 		break;
375 	case l_narp_req:	/* LANE2: see 7.1.35 in the lane2 spec */
376 		spin_lock_irqsave(&priv->lec_arp_lock, flags);
377 		entry = lec_arp_find(priv, mesg->content.normal.mac_addr);
378 		lec_arp_remove(priv, entry);
379 		spin_unlock_irqrestore(&priv->lec_arp_lock, flags);
380 
381 		if (mesg->content.normal.no_source_le_narp)
382 			break;
383 		fallthrough;
384 	case l_arp_update:
385 		lec_arp_update(priv, mesg->content.normal.mac_addr,
386 			       mesg->content.normal.atm_addr,
387 			       mesg->content.normal.flag,
388 			       mesg->content.normal.targetless_le_arp);
389 		pr_debug("in l_arp_update\n");
390 		if (mesg->sizeoftlvs != 0) {	/* LANE2 3.1.5 */
391 			pr_debug("LANE2 3.1.5, got tlvs, size %d\n",
392 				 mesg->sizeoftlvs);
393 			lane2_associate_ind(dev, mesg->content.normal.mac_addr,
394 					    tmp, mesg->sizeoftlvs);
395 		}
396 		break;
397 	case l_config:
398 		priv->maximum_unknown_frame_count =
399 		    mesg->content.config.maximum_unknown_frame_count;
400 		priv->max_unknown_frame_time =
401 		    (mesg->content.config.max_unknown_frame_time * HZ);
402 		priv->max_retry_count = mesg->content.config.max_retry_count;
403 		priv->aging_time = (mesg->content.config.aging_time * HZ);
404 		priv->forward_delay_time =
405 		    (mesg->content.config.forward_delay_time * HZ);
406 		priv->arp_response_time =
407 		    (mesg->content.config.arp_response_time * HZ);
408 		priv->flush_timeout = (mesg->content.config.flush_timeout * HZ);
409 		priv->path_switching_delay =
410 		    (mesg->content.config.path_switching_delay * HZ);
411 		priv->lane_version = mesg->content.config.lane_version;
412 					/* LANE2 */
413 		priv->lane2_ops = NULL;
414 		if (priv->lane_version > 1)
415 			priv->lane2_ops = &lane2_ops;
416 		rtnl_lock();
417 		if (dev_set_mtu(dev, mesg->content.config.mtu))
418 			pr_info("%s: change_mtu to %d failed\n",
419 				dev->name, mesg->content.config.mtu);
420 		rtnl_unlock();
421 		priv->is_proxy = mesg->content.config.is_proxy;
422 		break;
423 	case l_flush_tran_id:
424 		lec_set_flush_tran_id(priv, mesg->content.normal.atm_addr,
425 				      mesg->content.normal.flag);
426 		break;
427 	case l_set_lecid:
428 		priv->lecid =
429 		    (unsigned short)(0xffff & mesg->content.normal.flag);
430 		break;
431 	case l_should_bridge:
432 #if IS_ENABLED(CONFIG_BRIDGE)
433 	{
434 		pr_debug("%s: bridge zeppelin asks about %pM\n",
435 			 dev->name, mesg->content.proxy.mac_addr);
436 
437 		if (br_fdb_test_addr_hook == NULL)
438 			break;
439 
440 		if (br_fdb_test_addr_hook(dev, mesg->content.proxy.mac_addr)) {
441 			/* hit from bridge table, send LE_ARP_RESPONSE */
442 			struct sk_buff *skb2;
443 			struct sock *sk;
444 
445 			pr_debug("%s: entry found, responding to zeppelin\n",
446 				 dev->name);
447 			skb2 = alloc_skb(sizeof(struct atmlec_msg), GFP_ATOMIC);
448 			if (skb2 == NULL)
449 				break;
450 			skb2->len = sizeof(struct atmlec_msg);
451 			skb_copy_to_linear_data(skb2, mesg, sizeof(*mesg));
452 			atm_force_charge(priv->lecd, skb2->truesize);
453 			sk = sk_atm(priv->lecd);
454 			skb_queue_tail(&sk->sk_receive_queue, skb2);
455 			sk->sk_data_ready(sk);
456 		}
457 	}
458 #endif /* IS_ENABLED(CONFIG_BRIDGE) */
459 		break;
460 	default:
461 		pr_info("%s: Unknown message type %d\n", dev->name, mesg->type);
462 		dev_kfree_skb(skb);
463 		return -EINVAL;
464 	}
465 	dev_kfree_skb(skb);
466 	return 0;
467 }
468 
lec_atm_close(struct atm_vcc * vcc)469 static void lec_atm_close(struct atm_vcc *vcc)
470 {
471 	struct sk_buff *skb;
472 	struct net_device *dev = (struct net_device *)vcc->proto_data;
473 	struct lec_priv *priv = netdev_priv(dev);
474 
475 	priv->lecd = NULL;
476 	/* Do something needful? */
477 
478 	netif_stop_queue(dev);
479 	lec_arp_destroy(priv);
480 
481 	if (skb_peek(&sk_atm(vcc)->sk_receive_queue))
482 		pr_info("%s closing with messages pending\n", dev->name);
483 	while ((skb = skb_dequeue(&sk_atm(vcc)->sk_receive_queue))) {
484 		atm_return(vcc, skb->truesize);
485 		dev_kfree_skb(skb);
486 	}
487 
488 	pr_info("%s: Shut down!\n", dev->name);
489 	module_put(THIS_MODULE);
490 }
491 
492 static const struct atmdev_ops lecdev_ops = {
493 	.close = lec_atm_close,
494 	.send = lec_atm_send
495 };
496 
497 static struct atm_dev lecatm_dev = {
498 	.ops = &lecdev_ops,
499 	.type = "lec",
500 	.number = 999,		/* dummy device number */
501 	.lock = __SPIN_LOCK_UNLOCKED(lecatm_dev.lock)
502 };
503 
504 /*
505  * LANE2: new argument struct sk_buff *data contains
506  * the LE_ARP based TLVs introduced in the LANE2 spec
507  */
508 static int
send_to_lecd(struct lec_priv * priv,atmlec_msg_type type,const unsigned char * mac_addr,const unsigned char * atm_addr,struct sk_buff * data)509 send_to_lecd(struct lec_priv *priv, atmlec_msg_type type,
510 	     const unsigned char *mac_addr, const unsigned char *atm_addr,
511 	     struct sk_buff *data)
512 {
513 	struct sock *sk;
514 	struct sk_buff *skb;
515 	struct atmlec_msg *mesg;
516 
517 	if (!priv || !priv->lecd)
518 		return -1;
519 	skb = alloc_skb(sizeof(struct atmlec_msg), GFP_ATOMIC);
520 	if (!skb)
521 		return -1;
522 	skb->len = sizeof(struct atmlec_msg);
523 	mesg = (struct atmlec_msg *)skb->data;
524 	memset(mesg, 0, sizeof(struct atmlec_msg));
525 	mesg->type = type;
526 	if (data != NULL)
527 		mesg->sizeoftlvs = data->len;
528 	if (mac_addr)
529 		ether_addr_copy(mesg->content.normal.mac_addr, mac_addr);
530 	else
531 		mesg->content.normal.targetless_le_arp = 1;
532 	if (atm_addr)
533 		memcpy(&mesg->content.normal.atm_addr, atm_addr, ATM_ESA_LEN);
534 
535 	atm_force_charge(priv->lecd, skb->truesize);
536 	sk = sk_atm(priv->lecd);
537 	skb_queue_tail(&sk->sk_receive_queue, skb);
538 	sk->sk_data_ready(sk);
539 
540 	if (data != NULL) {
541 		pr_debug("about to send %d bytes of data\n", data->len);
542 		atm_force_charge(priv->lecd, data->truesize);
543 		skb_queue_tail(&sk->sk_receive_queue, data);
544 		sk->sk_data_ready(sk);
545 	}
546 
547 	return 0;
548 }
549 
lec_set_multicast_list(struct net_device * dev)550 static void lec_set_multicast_list(struct net_device *dev)
551 {
552 	/*
553 	 * by default, all multicast frames arrive over the bus.
554 	 * eventually support selective multicast service
555 	 */
556 }
557 
558 static const struct net_device_ops lec_netdev_ops = {
559 	.ndo_open		= lec_open,
560 	.ndo_stop		= lec_close,
561 	.ndo_start_xmit		= lec_start_xmit,
562 	.ndo_tx_timeout		= lec_tx_timeout,
563 	.ndo_set_rx_mode	= lec_set_multicast_list,
564 };
565 
566 static const unsigned char lec_ctrl_magic[] = {
567 	0xff,
568 	0x00,
569 	0x01,
570 	0x01
571 };
572 
573 #define LEC_DATA_DIRECT_8023  2
574 #define LEC_DATA_DIRECT_8025  3
575 
lec_is_data_direct(struct atm_vcc * vcc)576 static int lec_is_data_direct(struct atm_vcc *vcc)
577 {
578 	return ((vcc->sap.blli[0].l3.tr9577.snap[4] == LEC_DATA_DIRECT_8023) ||
579 		(vcc->sap.blli[0].l3.tr9577.snap[4] == LEC_DATA_DIRECT_8025));
580 }
581 
lec_push(struct atm_vcc * vcc,struct sk_buff * skb)582 static void lec_push(struct atm_vcc *vcc, struct sk_buff *skb)
583 {
584 	unsigned long flags;
585 	struct net_device *dev = (struct net_device *)vcc->proto_data;
586 	struct lec_priv *priv = netdev_priv(dev);
587 
588 #if DUMP_PACKETS > 0
589 	printk(KERN_DEBUG "%s: vcc vpi:%d vci:%d\n",
590 	       dev->name, vcc->vpi, vcc->vci);
591 #endif
592 	if (!skb) {
593 		pr_debug("%s: null skb\n", dev->name);
594 		lec_vcc_close(priv, vcc);
595 		return;
596 	}
597 #if DUMP_PACKETS >= 2
598 #define MAX_SKB_DUMP 99
599 #elif DUMP_PACKETS >= 1
600 #define MAX_SKB_DUMP 30
601 #endif
602 #if DUMP_PACKETS > 0
603 	printk(KERN_DEBUG "%s: rcv datalen:%ld lecid:%4.4x\n",
604 	       dev->name, skb->len, priv->lecid);
605 	print_hex_dump(KERN_DEBUG, "", DUMP_OFFSET, 16, 1,
606 		       skb->data, min(MAX_SKB_DUMP, skb->len), true);
607 #endif /* DUMP_PACKETS > 0 */
608 	if (memcmp(skb->data, lec_ctrl_magic, 4) == 0) {
609 				/* Control frame, to daemon */
610 		struct sock *sk = sk_atm(vcc);
611 
612 		pr_debug("%s: To daemon\n", dev->name);
613 		skb_queue_tail(&sk->sk_receive_queue, skb);
614 		sk->sk_data_ready(sk);
615 	} else {		/* Data frame, queue to protocol handlers */
616 		struct lec_arp_table *entry;
617 		unsigned char *src, *dst;
618 
619 		atm_return(vcc, skb->truesize);
620 		if (*(__be16 *) skb->data == htons(priv->lecid) ||
621 		    !priv->lecd || !(dev->flags & IFF_UP)) {
622 			/*
623 			 * Probably looping back, or if lecd is missing,
624 			 * lecd has gone down
625 			 */
626 			pr_debug("Ignoring frame...\n");
627 			dev_kfree_skb(skb);
628 			return;
629 		}
630 		dst = ((struct lecdatahdr_8023 *)skb->data)->h_dest;
631 
632 		/*
633 		 * If this is a Data Direct VCC, and the VCC does not match
634 		 * the LE_ARP cache entry, delete the LE_ARP cache entry.
635 		 */
636 		spin_lock_irqsave(&priv->lec_arp_lock, flags);
637 		if (lec_is_data_direct(vcc)) {
638 			src = ((struct lecdatahdr_8023 *)skb->data)->h_source;
639 			entry = lec_arp_find(priv, src);
640 			if (entry && entry->vcc != vcc) {
641 				lec_arp_remove(priv, entry);
642 				lec_arp_put(entry);
643 			}
644 		}
645 		spin_unlock_irqrestore(&priv->lec_arp_lock, flags);
646 
647 		if (!(dst[0] & 0x01) &&	/* Never filter Multi/Broadcast */
648 		    !priv->is_proxy &&	/* Proxy wants all the packets */
649 		    memcmp(dst, dev->dev_addr, dev->addr_len)) {
650 			dev_kfree_skb(skb);
651 			return;
652 		}
653 		if (!hlist_empty(&priv->lec_arp_empty_ones))
654 			lec_arp_check_empties(priv, vcc, skb);
655 		skb_pull(skb, 2);	/* skip lec_id */
656 		skb->protocol = eth_type_trans(skb, dev);
657 		dev->stats.rx_packets++;
658 		dev->stats.rx_bytes += skb->len;
659 		memset(ATM_SKB(skb), 0, sizeof(struct atm_skb_data));
660 		netif_rx(skb);
661 	}
662 }
663 
lec_pop(struct atm_vcc * vcc,struct sk_buff * skb)664 static void lec_pop(struct atm_vcc *vcc, struct sk_buff *skb)
665 {
666 	struct lec_vcc_priv *vpriv = LEC_VCC_PRIV(vcc);
667 	struct net_device *dev = skb->dev;
668 
669 	if (vpriv == NULL) {
670 		pr_info("vpriv = NULL!?!?!?\n");
671 		return;
672 	}
673 
674 	vpriv->old_pop(vcc, skb);
675 
676 	if (vpriv->xoff && atm_may_send(vcc, 0)) {
677 		vpriv->xoff = 0;
678 		if (netif_running(dev) && netif_queue_stopped(dev))
679 			netif_wake_queue(dev);
680 	}
681 }
682 
lec_vcc_attach(struct atm_vcc * vcc,void __user * arg)683 static int lec_vcc_attach(struct atm_vcc *vcc, void __user *arg)
684 {
685 	struct lec_vcc_priv *vpriv;
686 	int bytes_left;
687 	struct atmlec_ioc ioc_data;
688 
689 	lockdep_assert_held(&lec_mutex);
690 	/* Lecd must be up in this case */
691 	bytes_left = copy_from_user(&ioc_data, arg, sizeof(struct atmlec_ioc));
692 	if (bytes_left != 0)
693 		pr_info("copy from user failed for %d bytes\n", bytes_left);
694 	if (ioc_data.dev_num < 0 || ioc_data.dev_num >= MAX_LEC_ITF)
695 		return -EINVAL;
696 	ioc_data.dev_num = array_index_nospec(ioc_data.dev_num, MAX_LEC_ITF);
697 	if (!dev_lec[ioc_data.dev_num])
698 		return -EINVAL;
699 	vpriv = kmalloc(sizeof(struct lec_vcc_priv), GFP_KERNEL);
700 	if (!vpriv)
701 		return -ENOMEM;
702 	vpriv->xoff = 0;
703 	vpriv->old_pop = vcc->pop;
704 	vcc->user_back = vpriv;
705 	vcc->pop = lec_pop;
706 	lec_vcc_added(netdev_priv(dev_lec[ioc_data.dev_num]),
707 		      &ioc_data, vcc, vcc->push);
708 	vcc->proto_data = dev_lec[ioc_data.dev_num];
709 	vcc->push = lec_push;
710 	return 0;
711 }
712 
lec_mcast_attach(struct atm_vcc * vcc,int arg)713 static int lec_mcast_attach(struct atm_vcc *vcc, int arg)
714 {
715 	lockdep_assert_held(&lec_mutex);
716 	if (arg < 0 || arg >= MAX_LEC_ITF)
717 		return -EINVAL;
718 	arg = array_index_nospec(arg, MAX_LEC_ITF);
719 	if (!dev_lec[arg])
720 		return -EINVAL;
721 	vcc->proto_data = dev_lec[arg];
722 	return lec_mcast_make(netdev_priv(dev_lec[arg]), vcc);
723 }
724 
725 /* Initialize device. */
lecd_attach(struct atm_vcc * vcc,int arg)726 static int lecd_attach(struct atm_vcc *vcc, int arg)
727 {
728 	int i;
729 	struct lec_priv *priv;
730 
731 	lockdep_assert_held(&lec_mutex);
732 	if (arg < 0)
733 		arg = 0;
734 	if (arg >= MAX_LEC_ITF)
735 		return -EINVAL;
736 	i = array_index_nospec(arg, MAX_LEC_ITF);
737 	if (!dev_lec[i]) {
738 		int size;
739 
740 		size = sizeof(struct lec_priv);
741 		dev_lec[i] = alloc_etherdev(size);
742 		if (!dev_lec[i])
743 			return -ENOMEM;
744 		dev_lec[i]->netdev_ops = &lec_netdev_ops;
745 		dev_lec[i]->max_mtu = 18190;
746 		snprintf(dev_lec[i]->name, IFNAMSIZ, "lec%d", i);
747 		if (register_netdev(dev_lec[i])) {
748 			free_netdev(dev_lec[i]);
749 			dev_lec[i] = NULL;
750 			return -EINVAL;
751 		}
752 
753 		priv = netdev_priv(dev_lec[i]);
754 	} else {
755 		priv = netdev_priv(dev_lec[i]);
756 		if (priv->lecd)
757 			return -EADDRINUSE;
758 	}
759 	lec_arp_init(priv);
760 	priv->itfnum = i;	/* LANE2 addition */
761 	priv->lecd = vcc;
762 	vcc->dev = &lecatm_dev;
763 	vcc_insert_socket(sk_atm(vcc));
764 
765 	vcc->proto_data = dev_lec[i];
766 	set_bit(ATM_VF_META, &vcc->flags);
767 	set_bit(ATM_VF_READY, &vcc->flags);
768 
769 	/* Set default values to these variables */
770 	priv->maximum_unknown_frame_count = 1;
771 	priv->max_unknown_frame_time = (1 * HZ);
772 	priv->vcc_timeout_period = (1200 * HZ);
773 	priv->max_retry_count = 1;
774 	priv->aging_time = (300 * HZ);
775 	priv->forward_delay_time = (15 * HZ);
776 	priv->topology_change = 0;
777 	priv->arp_response_time = (1 * HZ);
778 	priv->flush_timeout = (4 * HZ);
779 	priv->path_switching_delay = (6 * HZ);
780 
781 	if (dev_lec[i]->flags & IFF_UP)
782 		netif_start_queue(dev_lec[i]);
783 	__module_get(THIS_MODULE);
784 	return i;
785 }
786 
787 #ifdef CONFIG_PROC_FS
lec_arp_get_status_string(unsigned char status)788 static const char *lec_arp_get_status_string(unsigned char status)
789 {
790 	static const char *const lec_arp_status_string[] = {
791 		"ESI_UNKNOWN       ",
792 		"ESI_ARP_PENDING   ",
793 		"ESI_VC_PENDING    ",
794 		"<Undefined>       ",
795 		"ESI_FLUSH_PENDING ",
796 		"ESI_FORWARD_DIRECT"
797 	};
798 
799 	if (status > ESI_FORWARD_DIRECT)
800 		status = 3;	/* ESI_UNDEFINED */
801 	return lec_arp_status_string[status];
802 }
803 
lec_info(struct seq_file * seq,struct lec_arp_table * entry)804 static void lec_info(struct seq_file *seq, struct lec_arp_table *entry)
805 {
806 	seq_printf(seq, "%pM ", entry->mac_addr);
807 	seq_printf(seq, "%*phN ", ATM_ESA_LEN, entry->atm_addr);
808 	seq_printf(seq, "%s %4.4x", lec_arp_get_status_string(entry->status),
809 		   entry->flags & 0xffff);
810 	if (entry->vcc)
811 		seq_printf(seq, "%3d %3d ", entry->vcc->vpi, entry->vcc->vci);
812 	else
813 		seq_printf(seq, "        ");
814 	if (entry->recv_vcc) {
815 		seq_printf(seq, "     %3d %3d", entry->recv_vcc->vpi,
816 			   entry->recv_vcc->vci);
817 	}
818 	seq_putc(seq, '\n');
819 }
820 
821 struct lec_state {
822 	unsigned long flags;
823 	struct lec_priv *locked;
824 	struct hlist_node *node;
825 	struct net_device *dev;
826 	int itf;
827 	int arp_table;
828 	int misc_table;
829 };
830 
lec_tbl_walk(struct lec_state * state,struct hlist_head * tbl,loff_t * l)831 static void *lec_tbl_walk(struct lec_state *state, struct hlist_head *tbl,
832 			  loff_t *l)
833 {
834 	struct hlist_node *e = state->node;
835 
836 	if (!e)
837 		e = tbl->first;
838 	if (e == SEQ_START_TOKEN) {
839 		e = tbl->first;
840 		--*l;
841 	}
842 
843 	for (; e; e = e->next) {
844 		if (--*l < 0)
845 			break;
846 	}
847 	state->node = e;
848 
849 	return (*l < 0) ? state : NULL;
850 }
851 
lec_arp_walk(struct lec_state * state,loff_t * l,struct lec_priv * priv)852 static void *lec_arp_walk(struct lec_state *state, loff_t *l,
853 			  struct lec_priv *priv)
854 {
855 	void *v = NULL;
856 	int p;
857 
858 	for (p = state->arp_table; p < LEC_ARP_TABLE_SIZE; p++) {
859 		v = lec_tbl_walk(state, &priv->lec_arp_tables[p], l);
860 		if (v)
861 			break;
862 	}
863 	state->arp_table = p;
864 	return v;
865 }
866 
lec_misc_walk(struct lec_state * state,loff_t * l,struct lec_priv * priv)867 static void *lec_misc_walk(struct lec_state *state, loff_t *l,
868 			   struct lec_priv *priv)
869 {
870 	struct hlist_head *lec_misc_tables[] = {
871 		&priv->lec_arp_empty_ones,
872 		&priv->lec_no_forward,
873 		&priv->mcast_fwds
874 	};
875 	void *v = NULL;
876 	int q;
877 
878 	for (q = state->misc_table; q < ARRAY_SIZE(lec_misc_tables); q++) {
879 		v = lec_tbl_walk(state, lec_misc_tables[q], l);
880 		if (v)
881 			break;
882 	}
883 	state->misc_table = q;
884 	return v;
885 }
886 
lec_priv_walk(struct lec_state * state,loff_t * l,struct lec_priv * priv)887 static void *lec_priv_walk(struct lec_state *state, loff_t *l,
888 			   struct lec_priv *priv)
889 {
890 	if (!state->locked) {
891 		state->locked = priv;
892 		spin_lock_irqsave(&priv->lec_arp_lock, state->flags);
893 	}
894 	if (!lec_arp_walk(state, l, priv) && !lec_misc_walk(state, l, priv)) {
895 		spin_unlock_irqrestore(&priv->lec_arp_lock, state->flags);
896 		state->locked = NULL;
897 		/* Partial state reset for the next time we get called */
898 		state->arp_table = state->misc_table = 0;
899 	}
900 	return state->locked;
901 }
902 
lec_itf_walk(struct lec_state * state,loff_t * l)903 static void *lec_itf_walk(struct lec_state *state, loff_t *l)
904 {
905 	struct net_device *dev;
906 	void *v;
907 
908 	dev = state->dev ? state->dev : dev_lec[state->itf];
909 	v = (dev && netdev_priv(dev)) ?
910 		lec_priv_walk(state, l, netdev_priv(dev)) : NULL;
911 	if (!v && dev) {
912 		/* Partial state reset for the next time we get called */
913 		dev = NULL;
914 	}
915 	state->dev = dev;
916 	return v;
917 }
918 
lec_get_idx(struct lec_state * state,loff_t l)919 static void *lec_get_idx(struct lec_state *state, loff_t l)
920 {
921 	void *v = NULL;
922 
923 	for (; state->itf < MAX_LEC_ITF; state->itf++) {
924 		v = lec_itf_walk(state, &l);
925 		if (v)
926 			break;
927 	}
928 	return v;
929 }
930 
lec_seq_start(struct seq_file * seq,loff_t * pos)931 static void *lec_seq_start(struct seq_file *seq, loff_t *pos)
932 {
933 	struct lec_state *state = seq->private;
934 
935 	mutex_lock(&lec_mutex);
936 	state->itf = 0;
937 	state->dev = NULL;
938 	state->locked = NULL;
939 	state->arp_table = 0;
940 	state->misc_table = 0;
941 	state->node = SEQ_START_TOKEN;
942 
943 	return *pos ? lec_get_idx(state, *pos) : SEQ_START_TOKEN;
944 }
945 
lec_seq_stop(struct seq_file * seq,void * v)946 static void lec_seq_stop(struct seq_file *seq, void *v)
947 {
948 	struct lec_state *state = seq->private;
949 
950 	if (state->dev) {
951 		spin_unlock_irqrestore(&state->locked->lec_arp_lock,
952 				       state->flags);
953 		state->dev = NULL;
954 	}
955 	mutex_unlock(&lec_mutex);
956 }
957 
lec_seq_next(struct seq_file * seq,void * v,loff_t * pos)958 static void *lec_seq_next(struct seq_file *seq, void *v, loff_t *pos)
959 {
960 	struct lec_state *state = seq->private;
961 
962 	++*pos;
963 	return lec_get_idx(state, 1);
964 }
965 
lec_seq_show(struct seq_file * seq,void * v)966 static int lec_seq_show(struct seq_file *seq, void *v)
967 {
968 	static const char lec_banner[] =
969 	    "Itf  MAC          ATM destination"
970 	    "                          Status            Flags "
971 	    "VPI/VCI Recv VPI/VCI\n";
972 
973 	if (v == SEQ_START_TOKEN)
974 		seq_puts(seq, lec_banner);
975 	else {
976 		struct lec_state *state = seq->private;
977 		struct net_device *dev = state->dev;
978 		struct lec_arp_table *entry = hlist_entry(state->node,
979 							  struct lec_arp_table,
980 							  next);
981 
982 		seq_printf(seq, "%s ", dev->name);
983 		lec_info(seq, entry);
984 	}
985 	return 0;
986 }
987 
988 static const struct seq_operations lec_seq_ops = {
989 	.start = lec_seq_start,
990 	.next = lec_seq_next,
991 	.stop = lec_seq_stop,
992 	.show = lec_seq_show,
993 };
994 #endif
995 
lane_ioctl(struct socket * sock,unsigned int cmd,unsigned long arg)996 static int lane_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
997 {
998 	struct atm_vcc *vcc = ATM_SD(sock);
999 	int err = 0;
1000 
1001 	switch (cmd) {
1002 	case ATMLEC_CTRL:
1003 	case ATMLEC_MCAST:
1004 	case ATMLEC_DATA:
1005 		if (!capable(CAP_NET_ADMIN))
1006 			return -EPERM;
1007 		break;
1008 	default:
1009 		return -ENOIOCTLCMD;
1010 	}
1011 
1012 	mutex_lock(&lec_mutex);
1013 	switch (cmd) {
1014 	case ATMLEC_CTRL:
1015 		err = lecd_attach(vcc, (int)arg);
1016 		if (err >= 0)
1017 			sock->state = SS_CONNECTED;
1018 		break;
1019 	case ATMLEC_MCAST:
1020 		err = lec_mcast_attach(vcc, (int)arg);
1021 		break;
1022 	case ATMLEC_DATA:
1023 		err = lec_vcc_attach(vcc, (void __user *)arg);
1024 		break;
1025 	}
1026 
1027 	mutex_unlock(&lec_mutex);
1028 	return err;
1029 }
1030 
1031 static struct atm_ioctl lane_ioctl_ops = {
1032 	.owner = THIS_MODULE,
1033 	.ioctl = lane_ioctl,
1034 };
1035 
lane_module_init(void)1036 static int __init lane_module_init(void)
1037 {
1038 #ifdef CONFIG_PROC_FS
1039 	struct proc_dir_entry *p;
1040 
1041 	p = proc_create_seq_private("lec", 0444, atm_proc_root, &lec_seq_ops,
1042 			sizeof(struct lec_state), NULL);
1043 	if (!p) {
1044 		pr_err("Unable to initialize /proc/net/atm/lec\n");
1045 		return -ENOMEM;
1046 	}
1047 #endif
1048 
1049 	register_atm_ioctl(&lane_ioctl_ops);
1050 	pr_info("lec.c: initialized\n");
1051 	return 0;
1052 }
1053 
lane_module_cleanup(void)1054 static void __exit lane_module_cleanup(void)
1055 {
1056 	int i;
1057 
1058 #ifdef CONFIG_PROC_FS
1059 	remove_proc_entry("lec", atm_proc_root);
1060 #endif
1061 
1062 	deregister_atm_ioctl(&lane_ioctl_ops);
1063 
1064 	for (i = 0; i < MAX_LEC_ITF; i++) {
1065 		if (dev_lec[i] != NULL) {
1066 			unregister_netdev(dev_lec[i]);
1067 			free_netdev(dev_lec[i]);
1068 			dev_lec[i] = NULL;
1069 		}
1070 	}
1071 }
1072 
1073 module_init(lane_module_init);
1074 module_exit(lane_module_cleanup);
1075 
1076 /*
1077  * LANE2: 3.1.3, LE_RESOLVE.request
1078  * Non force allocates memory and fills in *tlvs, fills in *sizeoftlvs.
1079  * If sizeoftlvs == NULL the default TLVs associated with this
1080  * lec will be used.
1081  * If dst_mac == NULL, targetless LE_ARP will be sent
1082  */
lane2_resolve(struct net_device * dev,const u8 * dst_mac,int force,u8 ** tlvs,u32 * sizeoftlvs)1083 static int lane2_resolve(struct net_device *dev, const u8 *dst_mac, int force,
1084 			 u8 **tlvs, u32 *sizeoftlvs)
1085 {
1086 	unsigned long flags;
1087 	struct lec_priv *priv = netdev_priv(dev);
1088 	struct lec_arp_table *table;
1089 	struct sk_buff *skb;
1090 	int retval;
1091 
1092 	if (force == 0) {
1093 		spin_lock_irqsave(&priv->lec_arp_lock, flags);
1094 		table = lec_arp_find(priv, dst_mac);
1095 		spin_unlock_irqrestore(&priv->lec_arp_lock, flags);
1096 		if (table == NULL)
1097 			return -1;
1098 
1099 		*tlvs = kmemdup(table->tlvs, table->sizeoftlvs, GFP_ATOMIC);
1100 		if (*tlvs == NULL)
1101 			return -1;
1102 
1103 		*sizeoftlvs = table->sizeoftlvs;
1104 
1105 		return 0;
1106 	}
1107 
1108 	if (sizeoftlvs == NULL)
1109 		retval = send_to_lecd(priv, l_arp_xmt, dst_mac, NULL, NULL);
1110 
1111 	else {
1112 		skb = alloc_skb(*sizeoftlvs, GFP_ATOMIC);
1113 		if (skb == NULL)
1114 			return -1;
1115 		skb->len = *sizeoftlvs;
1116 		skb_copy_to_linear_data(skb, *tlvs, *sizeoftlvs);
1117 		retval = send_to_lecd(priv, l_arp_xmt, dst_mac, NULL, skb);
1118 	}
1119 	return retval;
1120 }
1121 
1122 /*
1123  * LANE2: 3.1.4, LE_ASSOCIATE.request
1124  * Associate the *tlvs with the *lan_dst address.
1125  * Will overwrite any previous association
1126  * Returns 1 for success, 0 for failure (out of memory)
1127  *
1128  */
lane2_associate_req(struct net_device * dev,const u8 * lan_dst,const u8 * tlvs,u32 sizeoftlvs)1129 static int lane2_associate_req(struct net_device *dev, const u8 *lan_dst,
1130 			       const u8 *tlvs, u32 sizeoftlvs)
1131 {
1132 	int retval;
1133 	struct sk_buff *skb;
1134 	struct lec_priv *priv = netdev_priv(dev);
1135 
1136 	if (!ether_addr_equal(lan_dst, dev->dev_addr))
1137 		return 0;	/* not our mac address */
1138 
1139 	kfree(priv->tlvs);	/* NULL if there was no previous association */
1140 
1141 	priv->tlvs = kmemdup(tlvs, sizeoftlvs, GFP_KERNEL);
1142 	if (priv->tlvs == NULL)
1143 		return 0;
1144 	priv->sizeoftlvs = sizeoftlvs;
1145 
1146 	skb = alloc_skb(sizeoftlvs, GFP_ATOMIC);
1147 	if (skb == NULL)
1148 		return 0;
1149 	skb->len = sizeoftlvs;
1150 	skb_copy_to_linear_data(skb, tlvs, sizeoftlvs);
1151 	retval = send_to_lecd(priv, l_associate_req, NULL, NULL, skb);
1152 	if (retval != 0)
1153 		pr_info("lec.c: lane2_associate_req() failed\n");
1154 	/*
1155 	 * If the previous association has changed we must
1156 	 * somehow notify other LANE entities about the change
1157 	 */
1158 	return 1;
1159 }
1160 
1161 /*
1162  * LANE2: 3.1.5, LE_ASSOCIATE.indication
1163  *
1164  */
lane2_associate_ind(struct net_device * dev,const u8 * mac_addr,const u8 * tlvs,u32 sizeoftlvs)1165 static void lane2_associate_ind(struct net_device *dev, const u8 *mac_addr,
1166 				const u8 *tlvs, u32 sizeoftlvs)
1167 {
1168 #if 0
1169 	int i = 0;
1170 #endif
1171 	struct lec_priv *priv = netdev_priv(dev);
1172 #if 0				/*
1173 				 * Why have the TLVs in LE_ARP entries
1174 				 * since we do not use them? When you
1175 				 * uncomment this code, make sure the
1176 				 * TLVs get freed when entry is killed
1177 				 */
1178 	struct lec_arp_table *entry = lec_arp_find(priv, mac_addr);
1179 
1180 	if (entry == NULL)
1181 		return;		/* should not happen */
1182 
1183 	kfree(entry->tlvs);
1184 
1185 	entry->tlvs = kmemdup(tlvs, sizeoftlvs, GFP_KERNEL);
1186 	if (entry->tlvs == NULL)
1187 		return;
1188 	entry->sizeoftlvs = sizeoftlvs;
1189 #endif
1190 #if 0
1191 	pr_info("\n");
1192 	pr_info("dump of tlvs, sizeoftlvs=%d\n", sizeoftlvs);
1193 	while (i < sizeoftlvs)
1194 		pr_cont("%02x ", tlvs[i++]);
1195 
1196 	pr_cont("\n");
1197 #endif
1198 
1199 	/* tell MPOA about the TLVs we saw */
1200 	if (priv->lane2_ops && priv->lane2_ops->associate_indicator) {
1201 		priv->lane2_ops->associate_indicator(dev, mac_addr,
1202 						     tlvs, sizeoftlvs);
1203 	}
1204 }
1205 
1206 /*
1207  * Here starts what used to lec_arpc.c
1208  *
1209  * lec_arpc.c was added here when making
1210  * lane client modular. October 1997
1211  */
1212 
1213 #include <linux/types.h>
1214 #include <linux/timer.h>
1215 #include <linux/param.h>
1216 #include <linux/atomic.h>
1217 #include <linux/inetdevice.h>
1218 #include <net/route.h>
1219 
1220 #if 0
1221 #define pr_debug(format, args...)
1222 /*
1223   #define pr_debug printk
1224 */
1225 #endif
1226 #define DEBUG_ARP_TABLE 0
1227 
1228 #define LEC_ARP_REFRESH_INTERVAL (3*HZ)
1229 
1230 static void lec_arp_check_expire(struct work_struct *work);
1231 static void lec_arp_expire_arp(struct timer_list *t);
1232 
1233 /*
1234  * Arp table funcs
1235  */
1236 
1237 #define HASH(ch) (ch & (LEC_ARP_TABLE_SIZE - 1))
1238 
1239 /*
1240  * Initialization of arp-cache
1241  */
lec_arp_init(struct lec_priv * priv)1242 static void lec_arp_init(struct lec_priv *priv)
1243 {
1244 	unsigned short i;
1245 
1246 	for (i = 0; i < LEC_ARP_TABLE_SIZE; i++)
1247 		INIT_HLIST_HEAD(&priv->lec_arp_tables[i]);
1248 	INIT_HLIST_HEAD(&priv->lec_arp_empty_ones);
1249 	INIT_HLIST_HEAD(&priv->lec_no_forward);
1250 	INIT_HLIST_HEAD(&priv->mcast_fwds);
1251 	spin_lock_init(&priv->lec_arp_lock);
1252 	INIT_DELAYED_WORK(&priv->lec_arp_work, lec_arp_check_expire);
1253 	schedule_delayed_work(&priv->lec_arp_work, LEC_ARP_REFRESH_INTERVAL);
1254 }
1255 
lec_arp_clear_vccs(struct lec_arp_table * entry)1256 static void lec_arp_clear_vccs(struct lec_arp_table *entry)
1257 {
1258 	if (entry->vcc) {
1259 		struct atm_vcc *vcc = entry->vcc;
1260 		struct lec_vcc_priv *vpriv = LEC_VCC_PRIV(vcc);
1261 		struct net_device *dev = (struct net_device *)vcc->proto_data;
1262 
1263 		vcc->pop = vpriv->old_pop;
1264 		if (vpriv->xoff)
1265 			netif_wake_queue(dev);
1266 		kfree(vpriv);
1267 		vcc->user_back = NULL;
1268 		vcc->push = entry->old_push;
1269 		vcc_release_async(vcc, -EPIPE);
1270 		entry->vcc = NULL;
1271 	}
1272 	if (entry->recv_vcc) {
1273 		struct atm_vcc *vcc = entry->recv_vcc;
1274 		struct lec_vcc_priv *vpriv = LEC_VCC_PRIV(vcc);
1275 
1276 		kfree(vpriv);
1277 		vcc->user_back = NULL;
1278 
1279 		entry->recv_vcc->push = entry->old_recv_push;
1280 		vcc_release_async(entry->recv_vcc, -EPIPE);
1281 		entry->recv_vcc = NULL;
1282 	}
1283 }
1284 
1285 /*
1286  * Insert entry to lec_arp_table
1287  * LANE2: Add to the end of the list to satisfy 8.1.13
1288  */
1289 static inline void
lec_arp_add(struct lec_priv * priv,struct lec_arp_table * entry)1290 lec_arp_add(struct lec_priv *priv, struct lec_arp_table *entry)
1291 {
1292 	struct hlist_head *tmp;
1293 
1294 	tmp = &priv->lec_arp_tables[HASH(entry->mac_addr[ETH_ALEN - 1])];
1295 	hlist_add_head(&entry->next, tmp);
1296 
1297 	pr_debug("Added entry:%pM\n", entry->mac_addr);
1298 }
1299 
1300 /*
1301  * Remove entry from lec_arp_table
1302  */
1303 static int
lec_arp_remove(struct lec_priv * priv,struct lec_arp_table * to_remove)1304 lec_arp_remove(struct lec_priv *priv, struct lec_arp_table *to_remove)
1305 {
1306 	struct lec_arp_table *entry;
1307 	int i, remove_vcc = 1;
1308 
1309 	if (!to_remove)
1310 		return -1;
1311 
1312 	hlist_del(&to_remove->next);
1313 	timer_delete(&to_remove->timer);
1314 
1315 	/*
1316 	 * If this is the only MAC connected to this VCC,
1317 	 * also tear down the VCC
1318 	 */
1319 	if (to_remove->status >= ESI_FLUSH_PENDING) {
1320 		/*
1321 		 * ESI_FLUSH_PENDING, ESI_FORWARD_DIRECT
1322 		 */
1323 		for (i = 0; i < LEC_ARP_TABLE_SIZE; i++) {
1324 			hlist_for_each_entry(entry,
1325 					     &priv->lec_arp_tables[i], next) {
1326 				if (memcmp(to_remove->atm_addr,
1327 					   entry->atm_addr, ATM_ESA_LEN) == 0) {
1328 					remove_vcc = 0;
1329 					break;
1330 				}
1331 			}
1332 		}
1333 		if (remove_vcc)
1334 			lec_arp_clear_vccs(to_remove);
1335 	}
1336 	skb_queue_purge(&to_remove->tx_wait);	/* FIXME: good place for this? */
1337 
1338 	pr_debug("Removed entry:%pM\n", to_remove->mac_addr);
1339 	return 0;
1340 }
1341 
1342 #if DEBUG_ARP_TABLE
get_status_string(unsigned char st)1343 static const char *get_status_string(unsigned char st)
1344 {
1345 	switch (st) {
1346 	case ESI_UNKNOWN:
1347 		return "ESI_UNKNOWN";
1348 	case ESI_ARP_PENDING:
1349 		return "ESI_ARP_PENDING";
1350 	case ESI_VC_PENDING:
1351 		return "ESI_VC_PENDING";
1352 	case ESI_FLUSH_PENDING:
1353 		return "ESI_FLUSH_PENDING";
1354 	case ESI_FORWARD_DIRECT:
1355 		return "ESI_FORWARD_DIRECT";
1356 	}
1357 	return "<UNKNOWN>";
1358 }
1359 
dump_arp_table(struct lec_priv * priv)1360 static void dump_arp_table(struct lec_priv *priv)
1361 {
1362 	struct lec_arp_table *rulla;
1363 	char buf[256];
1364 	int i, offset;
1365 
1366 	pr_info("Dump %p:\n", priv);
1367 	for (i = 0; i < LEC_ARP_TABLE_SIZE; i++) {
1368 		hlist_for_each_entry(rulla,
1369 				     &priv->lec_arp_tables[i], next) {
1370 			offset = 0;
1371 			offset += sprintf(buf, "%d: %p\n", i, rulla);
1372 			offset += sprintf(buf + offset, "Mac: %pM ",
1373 					  rulla->mac_addr);
1374 			offset += sprintf(buf + offset, "Atm: %*ph ", ATM_ESA_LEN,
1375 					  rulla->atm_addr);
1376 			offset += sprintf(buf + offset,
1377 					  "Vcc vpi:%d vci:%d, Recv_vcc vpi:%d vci:%d Last_used:%lx, Timestamp:%lx, No_tries:%d ",
1378 					  rulla->vcc ? rulla->vcc->vpi : 0,
1379 					  rulla->vcc ? rulla->vcc->vci : 0,
1380 					  rulla->recv_vcc ? rulla->recv_vcc->
1381 					  vpi : 0,
1382 					  rulla->recv_vcc ? rulla->recv_vcc->
1383 					  vci : 0, rulla->last_used,
1384 					  rulla->timestamp, rulla->no_tries);
1385 			offset +=
1386 			    sprintf(buf + offset,
1387 				    "Flags:%x, Packets_flooded:%x, Status: %s ",
1388 				    rulla->flags, rulla->packets_flooded,
1389 				    get_status_string(rulla->status));
1390 			pr_info("%s\n", buf);
1391 		}
1392 	}
1393 
1394 	if (!hlist_empty(&priv->lec_no_forward))
1395 		pr_info("No forward\n");
1396 	hlist_for_each_entry(rulla, &priv->lec_no_forward, next) {
1397 		offset = 0;
1398 		offset += sprintf(buf + offset, "Mac: %pM ", rulla->mac_addr);
1399 		offset += sprintf(buf + offset, "Atm: %*ph ", ATM_ESA_LEN,
1400 				  rulla->atm_addr);
1401 		offset += sprintf(buf + offset,
1402 				  "Vcc vpi:%d vci:%d, Recv_vcc vpi:%d vci:%d Last_used:%lx, Timestamp:%lx, No_tries:%d ",
1403 				  rulla->vcc ? rulla->vcc->vpi : 0,
1404 				  rulla->vcc ? rulla->vcc->vci : 0,
1405 				  rulla->recv_vcc ? rulla->recv_vcc->vpi : 0,
1406 				  rulla->recv_vcc ? rulla->recv_vcc->vci : 0,
1407 				  rulla->last_used,
1408 				  rulla->timestamp, rulla->no_tries);
1409 		offset += sprintf(buf + offset,
1410 				  "Flags:%x, Packets_flooded:%x, Status: %s ",
1411 				  rulla->flags, rulla->packets_flooded,
1412 				  get_status_string(rulla->status));
1413 		pr_info("%s\n", buf);
1414 	}
1415 
1416 	if (!hlist_empty(&priv->lec_arp_empty_ones))
1417 		pr_info("Empty ones\n");
1418 	hlist_for_each_entry(rulla, &priv->lec_arp_empty_ones, next) {
1419 		offset = 0;
1420 		offset += sprintf(buf + offset, "Mac: %pM ", rulla->mac_addr);
1421 		offset += sprintf(buf + offset, "Atm: %*ph ", ATM_ESA_LEN,
1422 				  rulla->atm_addr);
1423 		offset += sprintf(buf + offset,
1424 				  "Vcc vpi:%d vci:%d, Recv_vcc vpi:%d vci:%d Last_used:%lx, Timestamp:%lx, No_tries:%d ",
1425 				  rulla->vcc ? rulla->vcc->vpi : 0,
1426 				  rulla->vcc ? rulla->vcc->vci : 0,
1427 				  rulla->recv_vcc ? rulla->recv_vcc->vpi : 0,
1428 				  rulla->recv_vcc ? rulla->recv_vcc->vci : 0,
1429 				  rulla->last_used,
1430 				  rulla->timestamp, rulla->no_tries);
1431 		offset += sprintf(buf + offset,
1432 				  "Flags:%x, Packets_flooded:%x, Status: %s ",
1433 				  rulla->flags, rulla->packets_flooded,
1434 				  get_status_string(rulla->status));
1435 		pr_info("%s", buf);
1436 	}
1437 
1438 	if (!hlist_empty(&priv->mcast_fwds))
1439 		pr_info("Multicast Forward VCCs\n");
1440 	hlist_for_each_entry(rulla, &priv->mcast_fwds, next) {
1441 		offset = 0;
1442 		offset += sprintf(buf + offset, "Mac: %pM ", rulla->mac_addr);
1443 		offset += sprintf(buf + offset, "Atm: %*ph ", ATM_ESA_LEN,
1444 				  rulla->atm_addr);
1445 		offset += sprintf(buf + offset,
1446 				  "Vcc vpi:%d vci:%d, Recv_vcc vpi:%d vci:%d Last_used:%lx, Timestamp:%lx, No_tries:%d ",
1447 				  rulla->vcc ? rulla->vcc->vpi : 0,
1448 				  rulla->vcc ? rulla->vcc->vci : 0,
1449 				  rulla->recv_vcc ? rulla->recv_vcc->vpi : 0,
1450 				  rulla->recv_vcc ? rulla->recv_vcc->vci : 0,
1451 				  rulla->last_used,
1452 				  rulla->timestamp, rulla->no_tries);
1453 		offset += sprintf(buf + offset,
1454 				  "Flags:%x, Packets_flooded:%x, Status: %s ",
1455 				  rulla->flags, rulla->packets_flooded,
1456 				  get_status_string(rulla->status));
1457 		pr_info("%s\n", buf);
1458 	}
1459 
1460 }
1461 #else
1462 #define dump_arp_table(priv) do { } while (0)
1463 #endif
1464 
1465 /*
1466  * Destruction of arp-cache
1467  */
lec_arp_destroy(struct lec_priv * priv)1468 static void lec_arp_destroy(struct lec_priv *priv)
1469 {
1470 	unsigned long flags;
1471 	struct hlist_node *next;
1472 	struct lec_arp_table *entry;
1473 	int i;
1474 
1475 	cancel_delayed_work_sync(&priv->lec_arp_work);
1476 
1477 	/*
1478 	 * Remove all entries
1479 	 */
1480 
1481 	spin_lock_irqsave(&priv->lec_arp_lock, flags);
1482 	for (i = 0; i < LEC_ARP_TABLE_SIZE; i++) {
1483 		hlist_for_each_entry_safe(entry, next,
1484 					  &priv->lec_arp_tables[i], next) {
1485 			lec_arp_remove(priv, entry);
1486 			lec_arp_put(entry);
1487 		}
1488 		INIT_HLIST_HEAD(&priv->lec_arp_tables[i]);
1489 	}
1490 
1491 	hlist_for_each_entry_safe(entry, next,
1492 				  &priv->lec_arp_empty_ones, next) {
1493 		timer_delete_sync(&entry->timer);
1494 		lec_arp_clear_vccs(entry);
1495 		hlist_del(&entry->next);
1496 		lec_arp_put(entry);
1497 	}
1498 	INIT_HLIST_HEAD(&priv->lec_arp_empty_ones);
1499 
1500 	hlist_for_each_entry_safe(entry, next,
1501 				  &priv->lec_no_forward, next) {
1502 		timer_delete_sync(&entry->timer);
1503 		lec_arp_clear_vccs(entry);
1504 		hlist_del(&entry->next);
1505 		lec_arp_put(entry);
1506 	}
1507 	INIT_HLIST_HEAD(&priv->lec_no_forward);
1508 
1509 	hlist_for_each_entry_safe(entry, next, &priv->mcast_fwds, next) {
1510 		/* No timer, LANEv2 7.1.20 and 2.3.5.3 */
1511 		lec_arp_clear_vccs(entry);
1512 		hlist_del(&entry->next);
1513 		lec_arp_put(entry);
1514 	}
1515 	INIT_HLIST_HEAD(&priv->mcast_fwds);
1516 	priv->mcast_vcc = NULL;
1517 	spin_unlock_irqrestore(&priv->lec_arp_lock, flags);
1518 }
1519 
1520 /*
1521  * Find entry by mac_address
1522  */
lec_arp_find(struct lec_priv * priv,const unsigned char * mac_addr)1523 static struct lec_arp_table *lec_arp_find(struct lec_priv *priv,
1524 					  const unsigned char *mac_addr)
1525 {
1526 	struct hlist_head *head;
1527 	struct lec_arp_table *entry;
1528 
1529 	pr_debug("%pM\n", mac_addr);
1530 
1531 	head = &priv->lec_arp_tables[HASH(mac_addr[ETH_ALEN - 1])];
1532 	hlist_for_each_entry(entry, head, next) {
1533 		if (ether_addr_equal(mac_addr, entry->mac_addr))
1534 			return entry;
1535 	}
1536 	return NULL;
1537 }
1538 
make_entry(struct lec_priv * priv,const unsigned char * mac_addr)1539 static struct lec_arp_table *make_entry(struct lec_priv *priv,
1540 					const unsigned char *mac_addr)
1541 {
1542 	struct lec_arp_table *to_return;
1543 
1544 	to_return = kzalloc(sizeof(struct lec_arp_table), GFP_ATOMIC);
1545 	if (!to_return)
1546 		return NULL;
1547 	ether_addr_copy(to_return->mac_addr, mac_addr);
1548 	INIT_HLIST_NODE(&to_return->next);
1549 	timer_setup(&to_return->timer, lec_arp_expire_arp, 0);
1550 	to_return->last_used = jiffies;
1551 	to_return->priv = priv;
1552 	skb_queue_head_init(&to_return->tx_wait);
1553 	refcount_set(&to_return->usage, 1);
1554 	return to_return;
1555 }
1556 
1557 /* Arp sent timer expired */
lec_arp_expire_arp(struct timer_list * t)1558 static void lec_arp_expire_arp(struct timer_list *t)
1559 {
1560 	struct lec_arp_table *entry;
1561 
1562 	entry = timer_container_of(entry, t, timer);
1563 
1564 	pr_debug("\n");
1565 	if (entry->status == ESI_ARP_PENDING) {
1566 		if (entry->no_tries <= entry->priv->max_retry_count) {
1567 			if (entry->is_rdesc)
1568 				send_to_lecd(entry->priv, l_rdesc_arp_xmt,
1569 					     entry->mac_addr, NULL, NULL);
1570 			else
1571 				send_to_lecd(entry->priv, l_arp_xmt,
1572 					     entry->mac_addr, NULL, NULL);
1573 			entry->no_tries++;
1574 		}
1575 		mod_timer(&entry->timer, jiffies + (1 * HZ));
1576 	}
1577 }
1578 
1579 /* Unknown/unused vcc expire, remove associated entry */
lec_arp_expire_vcc(struct timer_list * t)1580 static void lec_arp_expire_vcc(struct timer_list *t)
1581 {
1582 	unsigned long flags;
1583 	struct lec_arp_table *to_remove = timer_container_of(to_remove, t,
1584 							     timer);
1585 	struct lec_priv *priv = to_remove->priv;
1586 
1587 	timer_delete(&to_remove->timer);
1588 
1589 	pr_debug("%p %p: vpi:%d vci:%d\n",
1590 		 to_remove, priv,
1591 		 to_remove->vcc ? to_remove->recv_vcc->vpi : 0,
1592 		 to_remove->vcc ? to_remove->recv_vcc->vci : 0);
1593 
1594 	spin_lock_irqsave(&priv->lec_arp_lock, flags);
1595 	hlist_del(&to_remove->next);
1596 	spin_unlock_irqrestore(&priv->lec_arp_lock, flags);
1597 
1598 	lec_arp_clear_vccs(to_remove);
1599 	lec_arp_put(to_remove);
1600 }
1601 
__lec_arp_check_expire(struct lec_arp_table * entry,unsigned long now,struct lec_priv * priv)1602 static bool __lec_arp_check_expire(struct lec_arp_table *entry,
1603 				   unsigned long now,
1604 				   struct lec_priv *priv)
1605 {
1606 	unsigned long time_to_check;
1607 
1608 	if ((entry->flags) & LEC_REMOTE_FLAG && priv->topology_change)
1609 		time_to_check = priv->forward_delay_time;
1610 	else
1611 		time_to_check = priv->aging_time;
1612 
1613 	pr_debug("About to expire: %lx - %lx > %lx\n",
1614 		 now, entry->last_used, time_to_check);
1615 	if (time_after(now, entry->last_used + time_to_check) &&
1616 	    !(entry->flags & LEC_PERMANENT_FLAG) &&
1617 	    !(entry->mac_addr[0] & 0x01)) {	/* LANE2: 7.1.20 */
1618 		/* Remove entry */
1619 		pr_debug("Entry timed out\n");
1620 		lec_arp_remove(priv, entry);
1621 		lec_arp_put(entry);
1622 	} else {
1623 		/* Something else */
1624 		if ((entry->status == ESI_VC_PENDING ||
1625 		     entry->status == ESI_ARP_PENDING) &&
1626 		    time_after_eq(now, entry->timestamp +
1627 				       priv->max_unknown_frame_time)) {
1628 			entry->timestamp = jiffies;
1629 			entry->packets_flooded = 0;
1630 			if (entry->status == ESI_VC_PENDING)
1631 				send_to_lecd(priv, l_svc_setup,
1632 					     entry->mac_addr,
1633 					     entry->atm_addr,
1634 					     NULL);
1635 		}
1636 		if (entry->status == ESI_FLUSH_PENDING &&
1637 		    time_after_eq(now, entry->timestamp +
1638 				       priv->path_switching_delay)) {
1639 			lec_arp_hold(entry);
1640 			return true;
1641 		}
1642 	}
1643 
1644 	return false;
1645 }
1646 /*
1647  * Expire entries.
1648  * 1. Re-set timer
1649  * 2. For each entry, delete entries that have aged past the age limit.
1650  * 3. For each entry, depending on the status of the entry, perform
1651  *    the following maintenance.
1652  *    a. If status is ESI_VC_PENDING or ESI_ARP_PENDING then if the
1653  *       tick_count is above the max_unknown_frame_time, clear
1654  *       the tick_count to zero and clear the packets_flooded counter
1655  *       to zero. This supports the packet rate limit per address
1656  *       while flooding unknowns.
1657  *    b. If the status is ESI_FLUSH_PENDING and the tick_count is greater
1658  *       than or equal to the path_switching_delay, change the status
1659  *       to ESI_FORWARD_DIRECT. This causes the flush period to end
1660  *       regardless of the progress of the flush protocol.
1661  */
lec_arp_check_expire(struct work_struct * work)1662 static void lec_arp_check_expire(struct work_struct *work)
1663 {
1664 	unsigned long flags;
1665 	struct lec_priv *priv =
1666 		container_of(work, struct lec_priv, lec_arp_work.work);
1667 	struct hlist_node *next;
1668 	struct lec_arp_table *entry;
1669 	unsigned long now;
1670 	int i;
1671 
1672 	pr_debug("%p\n", priv);
1673 	now = jiffies;
1674 restart:
1675 	spin_lock_irqsave(&priv->lec_arp_lock, flags);
1676 	for (i = 0; i < LEC_ARP_TABLE_SIZE; i++) {
1677 		hlist_for_each_entry_safe(entry, next,
1678 					  &priv->lec_arp_tables[i], next) {
1679 			if (__lec_arp_check_expire(entry, now, priv)) {
1680 				struct sk_buff *skb;
1681 				struct atm_vcc *vcc = entry->vcc;
1682 
1683 				spin_unlock_irqrestore(&priv->lec_arp_lock,
1684 						       flags);
1685 				while ((skb = skb_dequeue(&entry->tx_wait)))
1686 					lec_send(vcc, skb);
1687 				entry->last_used = jiffies;
1688 				entry->status = ESI_FORWARD_DIRECT;
1689 				lec_arp_put(entry);
1690 
1691 				goto restart;
1692 			}
1693 		}
1694 	}
1695 	spin_unlock_irqrestore(&priv->lec_arp_lock, flags);
1696 
1697 	schedule_delayed_work(&priv->lec_arp_work, LEC_ARP_REFRESH_INTERVAL);
1698 }
1699 
1700 /*
1701  * Try to find vcc where mac_address is attached.
1702  *
1703  */
lec_arp_resolve(struct lec_priv * priv,const unsigned char * mac_to_find,int is_rdesc,struct lec_arp_table ** ret_entry)1704 static struct atm_vcc *lec_arp_resolve(struct lec_priv *priv,
1705 				       const unsigned char *mac_to_find,
1706 				       int is_rdesc,
1707 				       struct lec_arp_table **ret_entry)
1708 {
1709 	unsigned long flags;
1710 	struct lec_arp_table *entry;
1711 	struct atm_vcc *found;
1712 
1713 	if (mac_to_find[0] & 0x01) {
1714 		switch (priv->lane_version) {
1715 		case 1:
1716 			return priv->mcast_vcc;
1717 		case 2:	/* LANE2 wants arp for multicast addresses */
1718 			if (ether_addr_equal(mac_to_find, bus_mac))
1719 				return priv->mcast_vcc;
1720 			break;
1721 		default:
1722 			break;
1723 		}
1724 	}
1725 
1726 	spin_lock_irqsave(&priv->lec_arp_lock, flags);
1727 	entry = lec_arp_find(priv, mac_to_find);
1728 
1729 	if (entry) {
1730 		if (entry->status == ESI_FORWARD_DIRECT) {
1731 			/* Connection Ok */
1732 			entry->last_used = jiffies;
1733 			lec_arp_hold(entry);
1734 			*ret_entry = entry;
1735 			found = entry->vcc;
1736 			goto out;
1737 		}
1738 		/*
1739 		 * If the LE_ARP cache entry is still pending, reset count to 0
1740 		 * so another LE_ARP request can be made for this frame.
1741 		 */
1742 		if (entry->status == ESI_ARP_PENDING)
1743 			entry->no_tries = 0;
1744 		/*
1745 		 * Data direct VC not yet set up, check to see if the unknown
1746 		 * frame count is greater than the limit. If the limit has
1747 		 * not been reached, allow the caller to send packet to
1748 		 * BUS.
1749 		 */
1750 		if (entry->status != ESI_FLUSH_PENDING &&
1751 		    entry->packets_flooded <
1752 		    priv->maximum_unknown_frame_count) {
1753 			entry->packets_flooded++;
1754 			pr_debug("Flooding..\n");
1755 			found = priv->mcast_vcc;
1756 			goto out;
1757 		}
1758 		/*
1759 		 * We got here because entry->status == ESI_FLUSH_PENDING
1760 		 * or BUS flood limit was reached for an entry which is
1761 		 * in ESI_ARP_PENDING or ESI_VC_PENDING state.
1762 		 */
1763 		lec_arp_hold(entry);
1764 		*ret_entry = entry;
1765 		pr_debug("entry->status %d entry->vcc %p\n", entry->status,
1766 			 entry->vcc);
1767 		found = NULL;
1768 	} else {
1769 		/* No matching entry was found */
1770 		entry = make_entry(priv, mac_to_find);
1771 		pr_debug("Making entry\n");
1772 		if (!entry) {
1773 			found = priv->mcast_vcc;
1774 			goto out;
1775 		}
1776 		lec_arp_add(priv, entry);
1777 		/* We want arp-request(s) to be sent */
1778 		entry->packets_flooded = 1;
1779 		entry->status = ESI_ARP_PENDING;
1780 		entry->no_tries = 1;
1781 		entry->last_used = entry->timestamp = jiffies;
1782 		entry->is_rdesc = is_rdesc;
1783 		if (entry->is_rdesc)
1784 			send_to_lecd(priv, l_rdesc_arp_xmt, mac_to_find, NULL,
1785 				     NULL);
1786 		else
1787 			send_to_lecd(priv, l_arp_xmt, mac_to_find, NULL, NULL);
1788 		entry->timer.expires = jiffies + (1 * HZ);
1789 		entry->timer.function = lec_arp_expire_arp;
1790 		add_timer(&entry->timer);
1791 		found = priv->mcast_vcc;
1792 	}
1793 
1794 out:
1795 	spin_unlock_irqrestore(&priv->lec_arp_lock, flags);
1796 	return found;
1797 }
1798 
1799 static int
lec_addr_delete(struct lec_priv * priv,const unsigned char * atm_addr,unsigned long permanent)1800 lec_addr_delete(struct lec_priv *priv, const unsigned char *atm_addr,
1801 		unsigned long permanent)
1802 {
1803 	unsigned long flags;
1804 	struct hlist_node *next;
1805 	struct lec_arp_table *entry;
1806 	int i;
1807 
1808 	pr_debug("\n");
1809 	spin_lock_irqsave(&priv->lec_arp_lock, flags);
1810 	for (i = 0; i < LEC_ARP_TABLE_SIZE; i++) {
1811 		hlist_for_each_entry_safe(entry, next,
1812 					  &priv->lec_arp_tables[i], next) {
1813 			if (!memcmp(atm_addr, entry->atm_addr, ATM_ESA_LEN) &&
1814 			    (permanent ||
1815 			     !(entry->flags & LEC_PERMANENT_FLAG))) {
1816 				lec_arp_remove(priv, entry);
1817 				lec_arp_put(entry);
1818 			}
1819 			spin_unlock_irqrestore(&priv->lec_arp_lock, flags);
1820 			return 0;
1821 		}
1822 	}
1823 	spin_unlock_irqrestore(&priv->lec_arp_lock, flags);
1824 	return -1;
1825 }
1826 
1827 /*
1828  * Notifies:  Response to arp_request (atm_addr != NULL)
1829  */
1830 static void
lec_arp_update(struct lec_priv * priv,const unsigned char * mac_addr,const unsigned char * atm_addr,unsigned long remoteflag,unsigned int targetless_le_arp)1831 lec_arp_update(struct lec_priv *priv, const unsigned char *mac_addr,
1832 	       const unsigned char *atm_addr, unsigned long remoteflag,
1833 	       unsigned int targetless_le_arp)
1834 {
1835 	unsigned long flags;
1836 	struct hlist_node *next;
1837 	struct lec_arp_table *entry, *tmp;
1838 	int i;
1839 
1840 	pr_debug("%smac:%pM\n",
1841 		 (targetless_le_arp) ? "targetless " : "", mac_addr);
1842 
1843 	spin_lock_irqsave(&priv->lec_arp_lock, flags);
1844 	entry = lec_arp_find(priv, mac_addr);
1845 	if (entry == NULL && targetless_le_arp)
1846 		goto out;	/*
1847 				 * LANE2: ignore targetless LE_ARPs for which
1848 				 * we have no entry in the cache. 7.1.30
1849 				 */
1850 	if (!hlist_empty(&priv->lec_arp_empty_ones)) {
1851 		hlist_for_each_entry_safe(entry, next,
1852 					  &priv->lec_arp_empty_ones, next) {
1853 			if (memcmp(entry->atm_addr, atm_addr, ATM_ESA_LEN) == 0) {
1854 				hlist_del(&entry->next);
1855 				timer_delete(&entry->timer);
1856 				tmp = lec_arp_find(priv, mac_addr);
1857 				if (tmp) {
1858 					timer_delete(&tmp->timer);
1859 					tmp->status = ESI_FORWARD_DIRECT;
1860 					memcpy(tmp->atm_addr, atm_addr, ATM_ESA_LEN);
1861 					tmp->vcc = entry->vcc;
1862 					tmp->old_push = entry->old_push;
1863 					tmp->last_used = jiffies;
1864 					timer_delete(&entry->timer);
1865 					lec_arp_put(entry);
1866 					entry = tmp;
1867 				} else {
1868 					entry->status = ESI_FORWARD_DIRECT;
1869 					ether_addr_copy(entry->mac_addr,
1870 							mac_addr);
1871 					entry->last_used = jiffies;
1872 					lec_arp_add(priv, entry);
1873 				}
1874 				if (remoteflag)
1875 					entry->flags |= LEC_REMOTE_FLAG;
1876 				else
1877 					entry->flags &= ~LEC_REMOTE_FLAG;
1878 				pr_debug("After update\n");
1879 				dump_arp_table(priv);
1880 				goto out;
1881 			}
1882 		}
1883 	}
1884 
1885 	entry = lec_arp_find(priv, mac_addr);
1886 	if (!entry) {
1887 		entry = make_entry(priv, mac_addr);
1888 		if (!entry)
1889 			goto out;
1890 		entry->status = ESI_UNKNOWN;
1891 		lec_arp_add(priv, entry);
1892 		/* Temporary, changes before end of function */
1893 	}
1894 	memcpy(entry->atm_addr, atm_addr, ATM_ESA_LEN);
1895 	timer_delete(&entry->timer);
1896 	for (i = 0; i < LEC_ARP_TABLE_SIZE; i++) {
1897 		hlist_for_each_entry(tmp,
1898 				     &priv->lec_arp_tables[i], next) {
1899 			if (entry != tmp &&
1900 			    !memcmp(tmp->atm_addr, atm_addr, ATM_ESA_LEN)) {
1901 				/* Vcc to this host exists */
1902 				if (tmp->status > ESI_VC_PENDING) {
1903 					/*
1904 					 * ESI_FLUSH_PENDING,
1905 					 * ESI_FORWARD_DIRECT
1906 					 */
1907 					entry->vcc = tmp->vcc;
1908 					entry->old_push = tmp->old_push;
1909 				}
1910 				entry->status = tmp->status;
1911 				break;
1912 			}
1913 		}
1914 	}
1915 	if (remoteflag)
1916 		entry->flags |= LEC_REMOTE_FLAG;
1917 	else
1918 		entry->flags &= ~LEC_REMOTE_FLAG;
1919 	if (entry->status == ESI_ARP_PENDING || entry->status == ESI_UNKNOWN) {
1920 		entry->status = ESI_VC_PENDING;
1921 		send_to_lecd(priv, l_svc_setup, entry->mac_addr, atm_addr, NULL);
1922 	}
1923 	pr_debug("After update2\n");
1924 	dump_arp_table(priv);
1925 out:
1926 	spin_unlock_irqrestore(&priv->lec_arp_lock, flags);
1927 }
1928 
1929 /*
1930  * Notifies: Vcc setup ready
1931  */
1932 static void
lec_vcc_added(struct lec_priv * priv,const struct atmlec_ioc * ioc_data,struct atm_vcc * vcc,void (* old_push)(struct atm_vcc * vcc,struct sk_buff * skb))1933 lec_vcc_added(struct lec_priv *priv, const struct atmlec_ioc *ioc_data,
1934 	      struct atm_vcc *vcc,
1935 	      void (*old_push) (struct atm_vcc *vcc, struct sk_buff *skb))
1936 {
1937 	unsigned long flags;
1938 	struct lec_arp_table *entry;
1939 	int i, found_entry = 0;
1940 
1941 	spin_lock_irqsave(&priv->lec_arp_lock, flags);
1942 	/* Vcc for Multicast Forward. No timer, LANEv2 7.1.20 and 2.3.5.3 */
1943 	if (ioc_data->receive == 2) {
1944 		pr_debug("LEC_ARP: Attaching mcast forward\n");
1945 #if 0
1946 		entry = lec_arp_find(priv, bus_mac);
1947 		if (!entry) {
1948 			pr_info("LEC_ARP: Multicast entry not found!\n");
1949 			goto out;
1950 		}
1951 		memcpy(entry->atm_addr, ioc_data->atm_addr, ATM_ESA_LEN);
1952 		entry->recv_vcc = vcc;
1953 		entry->old_recv_push = old_push;
1954 #endif
1955 		entry = make_entry(priv, bus_mac);
1956 		if (entry == NULL)
1957 			goto out;
1958 		timer_delete(&entry->timer);
1959 		memcpy(entry->atm_addr, ioc_data->atm_addr, ATM_ESA_LEN);
1960 		entry->recv_vcc = vcc;
1961 		entry->old_recv_push = old_push;
1962 		hlist_add_head(&entry->next, &priv->mcast_fwds);
1963 		goto out;
1964 	} else if (ioc_data->receive == 1) {
1965 		/*
1966 		 * Vcc which we don't want to make default vcc,
1967 		 * attach it anyway.
1968 		 */
1969 		pr_debug("LEC_ARP:Attaching data direct, not default: %*phN\n",
1970 			 ATM_ESA_LEN, ioc_data->atm_addr);
1971 		entry = make_entry(priv, bus_mac);
1972 		if (entry == NULL)
1973 			goto out;
1974 		memcpy(entry->atm_addr, ioc_data->atm_addr, ATM_ESA_LEN);
1975 		eth_zero_addr(entry->mac_addr);
1976 		entry->recv_vcc = vcc;
1977 		entry->old_recv_push = old_push;
1978 		entry->status = ESI_UNKNOWN;
1979 		entry->timer.expires = jiffies + priv->vcc_timeout_period;
1980 		entry->timer.function = lec_arp_expire_vcc;
1981 		hlist_add_head(&entry->next, &priv->lec_no_forward);
1982 		add_timer(&entry->timer);
1983 		dump_arp_table(priv);
1984 		goto out;
1985 	}
1986 	pr_debug("LEC_ARP:Attaching data direct, default: %*phN\n",
1987 		 ATM_ESA_LEN, ioc_data->atm_addr);
1988 	for (i = 0; i < LEC_ARP_TABLE_SIZE; i++) {
1989 		hlist_for_each_entry(entry,
1990 				     &priv->lec_arp_tables[i], next) {
1991 			if (memcmp
1992 			    (ioc_data->atm_addr, entry->atm_addr,
1993 			     ATM_ESA_LEN) == 0) {
1994 				pr_debug("LEC_ARP: Attaching data direct\n");
1995 				pr_debug("Currently -> Vcc: %d, Rvcc:%d\n",
1996 					 entry->vcc ? entry->vcc->vci : 0,
1997 					 entry->recv_vcc ? entry->recv_vcc->
1998 					 vci : 0);
1999 				found_entry = 1;
2000 				timer_delete(&entry->timer);
2001 				entry->vcc = vcc;
2002 				entry->old_push = old_push;
2003 				if (entry->status == ESI_VC_PENDING) {
2004 					if (priv->maximum_unknown_frame_count
2005 					    == 0)
2006 						entry->status =
2007 						    ESI_FORWARD_DIRECT;
2008 					else {
2009 						entry->timestamp = jiffies;
2010 						entry->status =
2011 						    ESI_FLUSH_PENDING;
2012 #if 0
2013 						send_to_lecd(priv, l_flush_xmt,
2014 							     NULL,
2015 							     entry->atm_addr,
2016 							     NULL);
2017 #endif
2018 					}
2019 				} else {
2020 					/*
2021 					 * They were forming a connection
2022 					 * to us, and we to them. Our
2023 					 * ATM address is numerically lower
2024 					 * than theirs, so we make connection
2025 					 * we formed into default VCC (8.1.11).
2026 					 * Connection they made gets torn
2027 					 * down. This might confuse some
2028 					 * clients. Can be changed if
2029 					 * someone reports trouble...
2030 					 */
2031 					;
2032 				}
2033 			}
2034 		}
2035 	}
2036 	if (found_entry) {
2037 		pr_debug("After vcc was added\n");
2038 		dump_arp_table(priv);
2039 		goto out;
2040 	}
2041 	/*
2042 	 * Not found, snatch address from first data packet that arrives
2043 	 * from this vcc
2044 	 */
2045 	entry = make_entry(priv, bus_mac);
2046 	if (!entry)
2047 		goto out;
2048 	entry->vcc = vcc;
2049 	entry->old_push = old_push;
2050 	memcpy(entry->atm_addr, ioc_data->atm_addr, ATM_ESA_LEN);
2051 	eth_zero_addr(entry->mac_addr);
2052 	entry->status = ESI_UNKNOWN;
2053 	hlist_add_head(&entry->next, &priv->lec_arp_empty_ones);
2054 	entry->timer.expires = jiffies + priv->vcc_timeout_period;
2055 	entry->timer.function = lec_arp_expire_vcc;
2056 	add_timer(&entry->timer);
2057 	pr_debug("After vcc was added\n");
2058 	dump_arp_table(priv);
2059 out:
2060 	spin_unlock_irqrestore(&priv->lec_arp_lock, flags);
2061 }
2062 
lec_flush_complete(struct lec_priv * priv,unsigned long tran_id)2063 static void lec_flush_complete(struct lec_priv *priv, unsigned long tran_id)
2064 {
2065 	unsigned long flags;
2066 	struct lec_arp_table *entry;
2067 	int i;
2068 
2069 	pr_debug("%lx\n", tran_id);
2070 restart:
2071 	spin_lock_irqsave(&priv->lec_arp_lock, flags);
2072 	for (i = 0; i < LEC_ARP_TABLE_SIZE; i++) {
2073 		hlist_for_each_entry(entry,
2074 				     &priv->lec_arp_tables[i], next) {
2075 			if (entry->flush_tran_id == tran_id &&
2076 			    entry->status == ESI_FLUSH_PENDING) {
2077 				struct sk_buff *skb;
2078 				struct atm_vcc *vcc = entry->vcc;
2079 
2080 				lec_arp_hold(entry);
2081 				spin_unlock_irqrestore(&priv->lec_arp_lock,
2082 						       flags);
2083 				while ((skb = skb_dequeue(&entry->tx_wait)))
2084 					lec_send(vcc, skb);
2085 				entry->last_used = jiffies;
2086 				entry->status = ESI_FORWARD_DIRECT;
2087 				lec_arp_put(entry);
2088 				pr_debug("LEC_ARP: Flushed\n");
2089 				goto restart;
2090 			}
2091 		}
2092 	}
2093 	spin_unlock_irqrestore(&priv->lec_arp_lock, flags);
2094 	dump_arp_table(priv);
2095 }
2096 
2097 static void
lec_set_flush_tran_id(struct lec_priv * priv,const unsigned char * atm_addr,unsigned long tran_id)2098 lec_set_flush_tran_id(struct lec_priv *priv,
2099 		      const unsigned char *atm_addr, unsigned long tran_id)
2100 {
2101 	unsigned long flags;
2102 	struct lec_arp_table *entry;
2103 	int i;
2104 
2105 	spin_lock_irqsave(&priv->lec_arp_lock, flags);
2106 	for (i = 0; i < LEC_ARP_TABLE_SIZE; i++)
2107 		hlist_for_each_entry(entry,
2108 				     &priv->lec_arp_tables[i], next) {
2109 			if (!memcmp(atm_addr, entry->atm_addr, ATM_ESA_LEN)) {
2110 				entry->flush_tran_id = tran_id;
2111 				pr_debug("Set flush transaction id to %lx for %p\n",
2112 					 tran_id, entry);
2113 			}
2114 		}
2115 	spin_unlock_irqrestore(&priv->lec_arp_lock, flags);
2116 }
2117 
lec_mcast_make(struct lec_priv * priv,struct atm_vcc * vcc)2118 static int lec_mcast_make(struct lec_priv *priv, struct atm_vcc *vcc)
2119 {
2120 	unsigned long flags;
2121 	unsigned char mac_addr[] = {
2122 		0xff, 0xff, 0xff, 0xff, 0xff, 0xff
2123 	};
2124 	struct lec_arp_table *to_add;
2125 	struct lec_vcc_priv *vpriv;
2126 	int err = 0;
2127 
2128 	vpriv = kmalloc(sizeof(struct lec_vcc_priv), GFP_KERNEL);
2129 	if (!vpriv)
2130 		return -ENOMEM;
2131 	vpriv->xoff = 0;
2132 	vpriv->old_pop = vcc->pop;
2133 	vcc->user_back = vpriv;
2134 	vcc->pop = lec_pop;
2135 	spin_lock_irqsave(&priv->lec_arp_lock, flags);
2136 	to_add = make_entry(priv, mac_addr);
2137 	if (!to_add) {
2138 		vcc->pop = vpriv->old_pop;
2139 		kfree(vpriv);
2140 		err = -ENOMEM;
2141 		goto out;
2142 	}
2143 	memcpy(to_add->atm_addr, vcc->remote.sas_addr.prv, ATM_ESA_LEN);
2144 	to_add->status = ESI_FORWARD_DIRECT;
2145 	to_add->flags |= LEC_PERMANENT_FLAG;
2146 	to_add->vcc = vcc;
2147 	to_add->old_push = vcc->push;
2148 	vcc->push = lec_push;
2149 	priv->mcast_vcc = vcc;
2150 	lec_arp_add(priv, to_add);
2151 out:
2152 	spin_unlock_irqrestore(&priv->lec_arp_lock, flags);
2153 	return err;
2154 }
2155 
lec_vcc_close(struct lec_priv * priv,struct atm_vcc * vcc)2156 static void lec_vcc_close(struct lec_priv *priv, struct atm_vcc *vcc)
2157 {
2158 	unsigned long flags;
2159 	struct hlist_node *next;
2160 	struct lec_arp_table *entry;
2161 	int i;
2162 
2163 	pr_debug("LEC_ARP: lec_vcc_close vpi:%d vci:%d\n", vcc->vpi, vcc->vci);
2164 	dump_arp_table(priv);
2165 
2166 	spin_lock_irqsave(&priv->lec_arp_lock, flags);
2167 
2168 	for (i = 0; i < LEC_ARP_TABLE_SIZE; i++) {
2169 		hlist_for_each_entry_safe(entry, next,
2170 					  &priv->lec_arp_tables[i], next) {
2171 			if (vcc == entry->vcc) {
2172 				lec_arp_remove(priv, entry);
2173 				lec_arp_put(entry);
2174 				if (priv->mcast_vcc == vcc)
2175 					priv->mcast_vcc = NULL;
2176 			}
2177 		}
2178 	}
2179 
2180 	hlist_for_each_entry_safe(entry, next,
2181 				  &priv->lec_arp_empty_ones, next) {
2182 		if (entry->vcc == vcc) {
2183 			lec_arp_clear_vccs(entry);
2184 			timer_delete(&entry->timer);
2185 			hlist_del(&entry->next);
2186 			lec_arp_put(entry);
2187 		}
2188 	}
2189 
2190 	hlist_for_each_entry_safe(entry, next,
2191 				  &priv->lec_no_forward, next) {
2192 		if (entry->recv_vcc == vcc) {
2193 			lec_arp_clear_vccs(entry);
2194 			timer_delete(&entry->timer);
2195 			hlist_del(&entry->next);
2196 			lec_arp_put(entry);
2197 		}
2198 	}
2199 
2200 	hlist_for_each_entry_safe(entry, next, &priv->mcast_fwds, next) {
2201 		if (entry->recv_vcc == vcc) {
2202 			lec_arp_clear_vccs(entry);
2203 			/* No timer, LANEv2 7.1.20 and 2.3.5.3 */
2204 			hlist_del(&entry->next);
2205 			lec_arp_put(entry);
2206 		}
2207 	}
2208 
2209 	spin_unlock_irqrestore(&priv->lec_arp_lock, flags);
2210 	dump_arp_table(priv);
2211 }
2212 
2213 static void
lec_arp_check_empties(struct lec_priv * priv,struct atm_vcc * vcc,struct sk_buff * skb)2214 lec_arp_check_empties(struct lec_priv *priv,
2215 		      struct atm_vcc *vcc, struct sk_buff *skb)
2216 {
2217 	unsigned long flags;
2218 	struct hlist_node *next;
2219 	struct lec_arp_table *entry, *tmp;
2220 	struct lecdatahdr_8023 *hdr = (struct lecdatahdr_8023 *)skb->data;
2221 	unsigned char *src = hdr->h_source;
2222 
2223 	spin_lock_irqsave(&priv->lec_arp_lock, flags);
2224 	hlist_for_each_entry_safe(entry, next,
2225 				  &priv->lec_arp_empty_ones, next) {
2226 		if (vcc == entry->vcc) {
2227 			timer_delete(&entry->timer);
2228 			ether_addr_copy(entry->mac_addr, src);
2229 			entry->status = ESI_FORWARD_DIRECT;
2230 			entry->last_used = jiffies;
2231 			/* We might have got an entry */
2232 			tmp = lec_arp_find(priv, src);
2233 			if (tmp) {
2234 				lec_arp_remove(priv, tmp);
2235 				lec_arp_put(tmp);
2236 			}
2237 			hlist_del(&entry->next);
2238 			lec_arp_add(priv, entry);
2239 			goto out;
2240 		}
2241 	}
2242 	pr_debug("LEC_ARP: Arp_check_empties: entry not found!\n");
2243 out:
2244 	spin_unlock_irqrestore(&priv->lec_arp_lock, flags);
2245 }
2246 
2247 MODULE_DESCRIPTION("ATM LAN Emulation (LANE) support");
2248 MODULE_LICENSE("GPL");
2249