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