xref: /linux/net/atm/lec.c (revision abacaf559950eec0d99d37ff6b92049409af5943)
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_obj(struct lec_vcc_priv);
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 		if (vpriv) {
1264 			vcc->pop = vpriv->old_pop;
1265 			if (vpriv->xoff)
1266 				netif_wake_queue(dev);
1267 			kfree(vpriv);
1268 			vcc->user_back = NULL;
1269 			vcc->push = entry->old_push;
1270 			vcc_release_async(vcc, -EPIPE);
1271 		}
1272 		entry->vcc = NULL;
1273 	}
1274 	if (entry->recv_vcc) {
1275 		struct atm_vcc *vcc = entry->recv_vcc;
1276 		struct lec_vcc_priv *vpriv = LEC_VCC_PRIV(vcc);
1277 
1278 		if (vpriv) {
1279 			kfree(vpriv);
1280 			vcc->user_back = NULL;
1281 
1282 			entry->recv_vcc->push = entry->old_recv_push;
1283 			vcc_release_async(entry->recv_vcc, -EPIPE);
1284 		}
1285 		entry->recv_vcc = NULL;
1286 	}
1287 }
1288 
1289 /*
1290  * Insert entry to lec_arp_table
1291  * LANE2: Add to the end of the list to satisfy 8.1.13
1292  */
1293 static inline void
lec_arp_add(struct lec_priv * priv,struct lec_arp_table * entry)1294 lec_arp_add(struct lec_priv *priv, struct lec_arp_table *entry)
1295 {
1296 	struct hlist_head *tmp;
1297 
1298 	tmp = &priv->lec_arp_tables[HASH(entry->mac_addr[ETH_ALEN - 1])];
1299 	hlist_add_head(&entry->next, tmp);
1300 
1301 	pr_debug("Added entry:%pM\n", entry->mac_addr);
1302 }
1303 
1304 /*
1305  * Remove entry from lec_arp_table
1306  */
1307 static int
lec_arp_remove(struct lec_priv * priv,struct lec_arp_table * to_remove)1308 lec_arp_remove(struct lec_priv *priv, struct lec_arp_table *to_remove)
1309 {
1310 	struct lec_arp_table *entry;
1311 	int i, remove_vcc = 1;
1312 
1313 	if (!to_remove)
1314 		return -1;
1315 
1316 	hlist_del(&to_remove->next);
1317 	timer_delete(&to_remove->timer);
1318 
1319 	/*
1320 	 * If this is the only MAC connected to this VCC,
1321 	 * also tear down the VCC
1322 	 */
1323 	if (to_remove->status >= ESI_FLUSH_PENDING) {
1324 		/*
1325 		 * ESI_FLUSH_PENDING, ESI_FORWARD_DIRECT
1326 		 */
1327 		for (i = 0; i < LEC_ARP_TABLE_SIZE; i++) {
1328 			hlist_for_each_entry(entry,
1329 					     &priv->lec_arp_tables[i], next) {
1330 				if (memcmp(to_remove->atm_addr,
1331 					   entry->atm_addr, ATM_ESA_LEN) == 0) {
1332 					remove_vcc = 0;
1333 					break;
1334 				}
1335 			}
1336 		}
1337 		if (remove_vcc)
1338 			lec_arp_clear_vccs(to_remove);
1339 	}
1340 	skb_queue_purge(&to_remove->tx_wait);	/* FIXME: good place for this? */
1341 
1342 	pr_debug("Removed entry:%pM\n", to_remove->mac_addr);
1343 	return 0;
1344 }
1345 
1346 #if DEBUG_ARP_TABLE
get_status_string(unsigned char st)1347 static const char *get_status_string(unsigned char st)
1348 {
1349 	switch (st) {
1350 	case ESI_UNKNOWN:
1351 		return "ESI_UNKNOWN";
1352 	case ESI_ARP_PENDING:
1353 		return "ESI_ARP_PENDING";
1354 	case ESI_VC_PENDING:
1355 		return "ESI_VC_PENDING";
1356 	case ESI_FLUSH_PENDING:
1357 		return "ESI_FLUSH_PENDING";
1358 	case ESI_FORWARD_DIRECT:
1359 		return "ESI_FORWARD_DIRECT";
1360 	}
1361 	return "<UNKNOWN>";
1362 }
1363 
dump_arp_table(struct lec_priv * priv)1364 static void dump_arp_table(struct lec_priv *priv)
1365 {
1366 	struct lec_arp_table *rulla;
1367 	char buf[256];
1368 	int i, offset;
1369 
1370 	pr_info("Dump %p:\n", priv);
1371 	for (i = 0; i < LEC_ARP_TABLE_SIZE; i++) {
1372 		hlist_for_each_entry(rulla,
1373 				     &priv->lec_arp_tables[i], next) {
1374 			offset = 0;
1375 			offset += sprintf(buf, "%d: %p\n", i, rulla);
1376 			offset += sprintf(buf + offset, "Mac: %pM ",
1377 					  rulla->mac_addr);
1378 			offset += sprintf(buf + offset, "Atm: %*ph ", ATM_ESA_LEN,
1379 					  rulla->atm_addr);
1380 			offset += sprintf(buf + offset,
1381 					  "Vcc vpi:%d vci:%d, Recv_vcc vpi:%d vci:%d Last_used:%lx, Timestamp:%lx, No_tries:%d ",
1382 					  rulla->vcc ? rulla->vcc->vpi : 0,
1383 					  rulla->vcc ? rulla->vcc->vci : 0,
1384 					  rulla->recv_vcc ? rulla->recv_vcc->
1385 					  vpi : 0,
1386 					  rulla->recv_vcc ? rulla->recv_vcc->
1387 					  vci : 0, rulla->last_used,
1388 					  rulla->timestamp, rulla->no_tries);
1389 			offset +=
1390 			    sprintf(buf + offset,
1391 				    "Flags:%x, Packets_flooded:%x, Status: %s ",
1392 				    rulla->flags, rulla->packets_flooded,
1393 				    get_status_string(rulla->status));
1394 			pr_info("%s\n", buf);
1395 		}
1396 	}
1397 
1398 	if (!hlist_empty(&priv->lec_no_forward))
1399 		pr_info("No forward\n");
1400 	hlist_for_each_entry(rulla, &priv->lec_no_forward, next) {
1401 		offset = 0;
1402 		offset += sprintf(buf + offset, "Mac: %pM ", rulla->mac_addr);
1403 		offset += sprintf(buf + offset, "Atm: %*ph ", ATM_ESA_LEN,
1404 				  rulla->atm_addr);
1405 		offset += sprintf(buf + offset,
1406 				  "Vcc vpi:%d vci:%d, Recv_vcc vpi:%d vci:%d Last_used:%lx, Timestamp:%lx, No_tries:%d ",
1407 				  rulla->vcc ? rulla->vcc->vpi : 0,
1408 				  rulla->vcc ? rulla->vcc->vci : 0,
1409 				  rulla->recv_vcc ? rulla->recv_vcc->vpi : 0,
1410 				  rulla->recv_vcc ? rulla->recv_vcc->vci : 0,
1411 				  rulla->last_used,
1412 				  rulla->timestamp, rulla->no_tries);
1413 		offset += sprintf(buf + offset,
1414 				  "Flags:%x, Packets_flooded:%x, Status: %s ",
1415 				  rulla->flags, rulla->packets_flooded,
1416 				  get_status_string(rulla->status));
1417 		pr_info("%s\n", buf);
1418 	}
1419 
1420 	if (!hlist_empty(&priv->lec_arp_empty_ones))
1421 		pr_info("Empty ones\n");
1422 	hlist_for_each_entry(rulla, &priv->lec_arp_empty_ones, 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", buf);
1440 	}
1441 
1442 	if (!hlist_empty(&priv->mcast_fwds))
1443 		pr_info("Multicast Forward VCCs\n");
1444 	hlist_for_each_entry(rulla, &priv->mcast_fwds, 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\n", buf);
1462 	}
1463 
1464 }
1465 #else
1466 #define dump_arp_table(priv) do { } while (0)
1467 #endif
1468 
1469 /*
1470  * Destruction of arp-cache
1471  */
lec_arp_destroy(struct lec_priv * priv)1472 static void lec_arp_destroy(struct lec_priv *priv)
1473 {
1474 	unsigned long flags;
1475 	struct hlist_node *next;
1476 	struct lec_arp_table *entry;
1477 	int i;
1478 
1479 	cancel_delayed_work_sync(&priv->lec_arp_work);
1480 
1481 	/*
1482 	 * Remove all entries
1483 	 */
1484 
1485 	spin_lock_irqsave(&priv->lec_arp_lock, flags);
1486 	for (i = 0; i < LEC_ARP_TABLE_SIZE; i++) {
1487 		hlist_for_each_entry_safe(entry, next,
1488 					  &priv->lec_arp_tables[i], next) {
1489 			lec_arp_remove(priv, entry);
1490 			lec_arp_put(entry);
1491 		}
1492 		INIT_HLIST_HEAD(&priv->lec_arp_tables[i]);
1493 	}
1494 
1495 	hlist_for_each_entry_safe(entry, next,
1496 				  &priv->lec_arp_empty_ones, next) {
1497 		timer_delete_sync(&entry->timer);
1498 		lec_arp_clear_vccs(entry);
1499 		hlist_del(&entry->next);
1500 		lec_arp_put(entry);
1501 	}
1502 	INIT_HLIST_HEAD(&priv->lec_arp_empty_ones);
1503 
1504 	hlist_for_each_entry_safe(entry, next,
1505 				  &priv->lec_no_forward, next) {
1506 		timer_delete_sync(&entry->timer);
1507 		lec_arp_clear_vccs(entry);
1508 		hlist_del(&entry->next);
1509 		lec_arp_put(entry);
1510 	}
1511 	INIT_HLIST_HEAD(&priv->lec_no_forward);
1512 
1513 	hlist_for_each_entry_safe(entry, next, &priv->mcast_fwds, next) {
1514 		/* No timer, LANEv2 7.1.20 and 2.3.5.3 */
1515 		lec_arp_clear_vccs(entry);
1516 		hlist_del(&entry->next);
1517 		lec_arp_put(entry);
1518 	}
1519 	INIT_HLIST_HEAD(&priv->mcast_fwds);
1520 	priv->mcast_vcc = NULL;
1521 	spin_unlock_irqrestore(&priv->lec_arp_lock, flags);
1522 }
1523 
1524 /*
1525  * Find entry by mac_address
1526  */
lec_arp_find(struct lec_priv * priv,const unsigned char * mac_addr)1527 static struct lec_arp_table *lec_arp_find(struct lec_priv *priv,
1528 					  const unsigned char *mac_addr)
1529 {
1530 	struct hlist_head *head;
1531 	struct lec_arp_table *entry;
1532 
1533 	pr_debug("%pM\n", mac_addr);
1534 
1535 	head = &priv->lec_arp_tables[HASH(mac_addr[ETH_ALEN - 1])];
1536 	hlist_for_each_entry(entry, head, next) {
1537 		if (ether_addr_equal(mac_addr, entry->mac_addr))
1538 			return entry;
1539 	}
1540 	return NULL;
1541 }
1542 
make_entry(struct lec_priv * priv,const unsigned char * mac_addr)1543 static struct lec_arp_table *make_entry(struct lec_priv *priv,
1544 					const unsigned char *mac_addr)
1545 {
1546 	struct lec_arp_table *to_return;
1547 
1548 	to_return = kzalloc_obj(struct lec_arp_table, GFP_ATOMIC);
1549 	if (!to_return)
1550 		return NULL;
1551 	ether_addr_copy(to_return->mac_addr, mac_addr);
1552 	INIT_HLIST_NODE(&to_return->next);
1553 	timer_setup(&to_return->timer, lec_arp_expire_arp, 0);
1554 	to_return->last_used = jiffies;
1555 	to_return->priv = priv;
1556 	skb_queue_head_init(&to_return->tx_wait);
1557 	refcount_set(&to_return->usage, 1);
1558 	return to_return;
1559 }
1560 
1561 /* Arp sent timer expired */
lec_arp_expire_arp(struct timer_list * t)1562 static void lec_arp_expire_arp(struct timer_list *t)
1563 {
1564 	struct lec_arp_table *entry;
1565 
1566 	entry = timer_container_of(entry, t, timer);
1567 
1568 	pr_debug("\n");
1569 	if (entry->status == ESI_ARP_PENDING) {
1570 		if (entry->no_tries <= entry->priv->max_retry_count) {
1571 			if (entry->is_rdesc)
1572 				send_to_lecd(entry->priv, l_rdesc_arp_xmt,
1573 					     entry->mac_addr, NULL, NULL);
1574 			else
1575 				send_to_lecd(entry->priv, l_arp_xmt,
1576 					     entry->mac_addr, NULL, NULL);
1577 			entry->no_tries++;
1578 		}
1579 		mod_timer(&entry->timer, jiffies + (1 * HZ));
1580 	}
1581 }
1582 
1583 /* Unknown/unused vcc expire, remove associated entry */
lec_arp_expire_vcc(struct timer_list * t)1584 static void lec_arp_expire_vcc(struct timer_list *t)
1585 {
1586 	unsigned long flags;
1587 	struct lec_arp_table *to_remove = timer_container_of(to_remove, t,
1588 							     timer);
1589 	struct lec_priv *priv = to_remove->priv;
1590 
1591 	timer_delete(&to_remove->timer);
1592 
1593 	pr_debug("%p %p: vpi:%d vci:%d\n",
1594 		 to_remove, priv,
1595 		 to_remove->vcc ? to_remove->recv_vcc->vpi : 0,
1596 		 to_remove->vcc ? to_remove->recv_vcc->vci : 0);
1597 
1598 	spin_lock_irqsave(&priv->lec_arp_lock, flags);
1599 	hlist_del(&to_remove->next);
1600 	spin_unlock_irqrestore(&priv->lec_arp_lock, flags);
1601 
1602 	lec_arp_clear_vccs(to_remove);
1603 	lec_arp_put(to_remove);
1604 }
1605 
__lec_arp_check_expire(struct lec_arp_table * entry,unsigned long now,struct lec_priv * priv)1606 static bool __lec_arp_check_expire(struct lec_arp_table *entry,
1607 				   unsigned long now,
1608 				   struct lec_priv *priv)
1609 {
1610 	unsigned long time_to_check;
1611 
1612 	if ((entry->flags) & LEC_REMOTE_FLAG && priv->topology_change)
1613 		time_to_check = priv->forward_delay_time;
1614 	else
1615 		time_to_check = priv->aging_time;
1616 
1617 	pr_debug("About to expire: %lx - %lx > %lx\n",
1618 		 now, entry->last_used, time_to_check);
1619 	if (time_after(now, entry->last_used + time_to_check) &&
1620 	    !(entry->flags & LEC_PERMANENT_FLAG) &&
1621 	    !(entry->mac_addr[0] & 0x01)) {	/* LANE2: 7.1.20 */
1622 		/* Remove entry */
1623 		pr_debug("Entry timed out\n");
1624 		lec_arp_remove(priv, entry);
1625 		lec_arp_put(entry);
1626 	} else {
1627 		/* Something else */
1628 		if ((entry->status == ESI_VC_PENDING ||
1629 		     entry->status == ESI_ARP_PENDING) &&
1630 		    time_after_eq(now, entry->timestamp +
1631 				       priv->max_unknown_frame_time)) {
1632 			entry->timestamp = jiffies;
1633 			entry->packets_flooded = 0;
1634 			if (entry->status == ESI_VC_PENDING)
1635 				send_to_lecd(priv, l_svc_setup,
1636 					     entry->mac_addr,
1637 					     entry->atm_addr,
1638 					     NULL);
1639 		}
1640 		if (entry->status == ESI_FLUSH_PENDING &&
1641 		    time_after_eq(now, entry->timestamp +
1642 				       priv->path_switching_delay)) {
1643 			lec_arp_hold(entry);
1644 			return true;
1645 		}
1646 	}
1647 
1648 	return false;
1649 }
1650 /*
1651  * Expire entries.
1652  * 1. Re-set timer
1653  * 2. For each entry, delete entries that have aged past the age limit.
1654  * 3. For each entry, depending on the status of the entry, perform
1655  *    the following maintenance.
1656  *    a. If status is ESI_VC_PENDING or ESI_ARP_PENDING then if the
1657  *       tick_count is above the max_unknown_frame_time, clear
1658  *       the tick_count to zero and clear the packets_flooded counter
1659  *       to zero. This supports the packet rate limit per address
1660  *       while flooding unknowns.
1661  *    b. If the status is ESI_FLUSH_PENDING and the tick_count is greater
1662  *       than or equal to the path_switching_delay, change the status
1663  *       to ESI_FORWARD_DIRECT. This causes the flush period to end
1664  *       regardless of the progress of the flush protocol.
1665  */
lec_arp_check_expire(struct work_struct * work)1666 static void lec_arp_check_expire(struct work_struct *work)
1667 {
1668 	unsigned long flags;
1669 	struct lec_priv *priv =
1670 		container_of(work, struct lec_priv, lec_arp_work.work);
1671 	struct hlist_node *next;
1672 	struct lec_arp_table *entry;
1673 	unsigned long now;
1674 	int i;
1675 
1676 	pr_debug("%p\n", priv);
1677 	now = jiffies;
1678 restart:
1679 	spin_lock_irqsave(&priv->lec_arp_lock, flags);
1680 	for (i = 0; i < LEC_ARP_TABLE_SIZE; i++) {
1681 		hlist_for_each_entry_safe(entry, next,
1682 					  &priv->lec_arp_tables[i], next) {
1683 			if (__lec_arp_check_expire(entry, now, priv)) {
1684 				struct sk_buff *skb;
1685 				struct atm_vcc *vcc = entry->vcc;
1686 
1687 				spin_unlock_irqrestore(&priv->lec_arp_lock,
1688 						       flags);
1689 				while ((skb = skb_dequeue(&entry->tx_wait)))
1690 					lec_send(vcc, skb);
1691 				entry->last_used = jiffies;
1692 				entry->status = ESI_FORWARD_DIRECT;
1693 				lec_arp_put(entry);
1694 
1695 				goto restart;
1696 			}
1697 		}
1698 	}
1699 	spin_unlock_irqrestore(&priv->lec_arp_lock, flags);
1700 
1701 	schedule_delayed_work(&priv->lec_arp_work, LEC_ARP_REFRESH_INTERVAL);
1702 }
1703 
1704 /*
1705  * Try to find vcc where mac_address is attached.
1706  *
1707  */
lec_arp_resolve(struct lec_priv * priv,const unsigned char * mac_to_find,int is_rdesc,struct lec_arp_table ** ret_entry)1708 static struct atm_vcc *lec_arp_resolve(struct lec_priv *priv,
1709 				       const unsigned char *mac_to_find,
1710 				       int is_rdesc,
1711 				       struct lec_arp_table **ret_entry)
1712 {
1713 	unsigned long flags;
1714 	struct lec_arp_table *entry;
1715 	struct atm_vcc *found;
1716 
1717 	if (mac_to_find[0] & 0x01) {
1718 		switch (priv->lane_version) {
1719 		case 1:
1720 			return priv->mcast_vcc;
1721 		case 2:	/* LANE2 wants arp for multicast addresses */
1722 			if (ether_addr_equal(mac_to_find, bus_mac))
1723 				return priv->mcast_vcc;
1724 			break;
1725 		default:
1726 			break;
1727 		}
1728 	}
1729 
1730 	spin_lock_irqsave(&priv->lec_arp_lock, flags);
1731 	entry = lec_arp_find(priv, mac_to_find);
1732 
1733 	if (entry) {
1734 		if (entry->status == ESI_FORWARD_DIRECT) {
1735 			/* Connection Ok */
1736 			entry->last_used = jiffies;
1737 			lec_arp_hold(entry);
1738 			*ret_entry = entry;
1739 			found = entry->vcc;
1740 			goto out;
1741 		}
1742 		/*
1743 		 * If the LE_ARP cache entry is still pending, reset count to 0
1744 		 * so another LE_ARP request can be made for this frame.
1745 		 */
1746 		if (entry->status == ESI_ARP_PENDING)
1747 			entry->no_tries = 0;
1748 		/*
1749 		 * Data direct VC not yet set up, check to see if the unknown
1750 		 * frame count is greater than the limit. If the limit has
1751 		 * not been reached, allow the caller to send packet to
1752 		 * BUS.
1753 		 */
1754 		if (entry->status != ESI_FLUSH_PENDING &&
1755 		    entry->packets_flooded <
1756 		    priv->maximum_unknown_frame_count) {
1757 			entry->packets_flooded++;
1758 			pr_debug("Flooding..\n");
1759 			found = priv->mcast_vcc;
1760 			goto out;
1761 		}
1762 		/*
1763 		 * We got here because entry->status == ESI_FLUSH_PENDING
1764 		 * or BUS flood limit was reached for an entry which is
1765 		 * in ESI_ARP_PENDING or ESI_VC_PENDING state.
1766 		 */
1767 		lec_arp_hold(entry);
1768 		*ret_entry = entry;
1769 		pr_debug("entry->status %d entry->vcc %p\n", entry->status,
1770 			 entry->vcc);
1771 		found = NULL;
1772 	} else {
1773 		/* No matching entry was found */
1774 		entry = make_entry(priv, mac_to_find);
1775 		pr_debug("Making entry\n");
1776 		if (!entry) {
1777 			found = priv->mcast_vcc;
1778 			goto out;
1779 		}
1780 		lec_arp_add(priv, entry);
1781 		/* We want arp-request(s) to be sent */
1782 		entry->packets_flooded = 1;
1783 		entry->status = ESI_ARP_PENDING;
1784 		entry->no_tries = 1;
1785 		entry->last_used = entry->timestamp = jiffies;
1786 		entry->is_rdesc = is_rdesc;
1787 		if (entry->is_rdesc)
1788 			send_to_lecd(priv, l_rdesc_arp_xmt, mac_to_find, NULL,
1789 				     NULL);
1790 		else
1791 			send_to_lecd(priv, l_arp_xmt, mac_to_find, NULL, NULL);
1792 		entry->timer.expires = jiffies + (1 * HZ);
1793 		entry->timer.function = lec_arp_expire_arp;
1794 		add_timer(&entry->timer);
1795 		found = priv->mcast_vcc;
1796 	}
1797 
1798 out:
1799 	spin_unlock_irqrestore(&priv->lec_arp_lock, flags);
1800 	return found;
1801 }
1802 
1803 static int
lec_addr_delete(struct lec_priv * priv,const unsigned char * atm_addr,unsigned long permanent)1804 lec_addr_delete(struct lec_priv *priv, const unsigned char *atm_addr,
1805 		unsigned long permanent)
1806 {
1807 	unsigned long flags;
1808 	struct hlist_node *next;
1809 	struct lec_arp_table *entry;
1810 	int i;
1811 
1812 	pr_debug("\n");
1813 	spin_lock_irqsave(&priv->lec_arp_lock, flags);
1814 	for (i = 0; i < LEC_ARP_TABLE_SIZE; i++) {
1815 		hlist_for_each_entry_safe(entry, next,
1816 					  &priv->lec_arp_tables[i], next) {
1817 			if (!memcmp(atm_addr, entry->atm_addr, ATM_ESA_LEN) &&
1818 			    (permanent ||
1819 			     !(entry->flags & LEC_PERMANENT_FLAG))) {
1820 				lec_arp_remove(priv, entry);
1821 				lec_arp_put(entry);
1822 			}
1823 			spin_unlock_irqrestore(&priv->lec_arp_lock, flags);
1824 			return 0;
1825 		}
1826 	}
1827 	spin_unlock_irqrestore(&priv->lec_arp_lock, flags);
1828 	return -1;
1829 }
1830 
1831 /*
1832  * Notifies:  Response to arp_request (atm_addr != NULL)
1833  */
1834 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)1835 lec_arp_update(struct lec_priv *priv, const unsigned char *mac_addr,
1836 	       const unsigned char *atm_addr, unsigned long remoteflag,
1837 	       unsigned int targetless_le_arp)
1838 {
1839 	unsigned long flags;
1840 	struct hlist_node *next;
1841 	struct lec_arp_table *entry, *tmp;
1842 	int i;
1843 
1844 	pr_debug("%smac:%pM\n",
1845 		 (targetless_le_arp) ? "targetless " : "", mac_addr);
1846 
1847 	spin_lock_irqsave(&priv->lec_arp_lock, flags);
1848 	entry = lec_arp_find(priv, mac_addr);
1849 	if (entry == NULL && targetless_le_arp)
1850 		goto out;	/*
1851 				 * LANE2: ignore targetless LE_ARPs for which
1852 				 * we have no entry in the cache. 7.1.30
1853 				 */
1854 	if (!hlist_empty(&priv->lec_arp_empty_ones)) {
1855 		hlist_for_each_entry_safe(entry, next,
1856 					  &priv->lec_arp_empty_ones, next) {
1857 			if (memcmp(entry->atm_addr, atm_addr, ATM_ESA_LEN) == 0) {
1858 				hlist_del(&entry->next);
1859 				timer_delete(&entry->timer);
1860 				tmp = lec_arp_find(priv, mac_addr);
1861 				if (tmp) {
1862 					timer_delete(&tmp->timer);
1863 					tmp->status = ESI_FORWARD_DIRECT;
1864 					memcpy(tmp->atm_addr, atm_addr, ATM_ESA_LEN);
1865 					tmp->vcc = entry->vcc;
1866 					tmp->old_push = entry->old_push;
1867 					tmp->last_used = jiffies;
1868 					timer_delete(&entry->timer);
1869 					lec_arp_put(entry);
1870 					entry = tmp;
1871 				} else {
1872 					entry->status = ESI_FORWARD_DIRECT;
1873 					ether_addr_copy(entry->mac_addr,
1874 							mac_addr);
1875 					entry->last_used = jiffies;
1876 					lec_arp_add(priv, entry);
1877 				}
1878 				if (remoteflag)
1879 					entry->flags |= LEC_REMOTE_FLAG;
1880 				else
1881 					entry->flags &= ~LEC_REMOTE_FLAG;
1882 				pr_debug("After update\n");
1883 				dump_arp_table(priv);
1884 				goto out;
1885 			}
1886 		}
1887 	}
1888 
1889 	entry = lec_arp_find(priv, mac_addr);
1890 	if (!entry) {
1891 		entry = make_entry(priv, mac_addr);
1892 		if (!entry)
1893 			goto out;
1894 		entry->status = ESI_UNKNOWN;
1895 		lec_arp_add(priv, entry);
1896 		/* Temporary, changes before end of function */
1897 	}
1898 	memcpy(entry->atm_addr, atm_addr, ATM_ESA_LEN);
1899 	timer_delete(&entry->timer);
1900 	for (i = 0; i < LEC_ARP_TABLE_SIZE; i++) {
1901 		hlist_for_each_entry(tmp,
1902 				     &priv->lec_arp_tables[i], next) {
1903 			if (entry != tmp &&
1904 			    !memcmp(tmp->atm_addr, atm_addr, ATM_ESA_LEN)) {
1905 				/* Vcc to this host exists */
1906 				if (tmp->status > ESI_VC_PENDING) {
1907 					/*
1908 					 * ESI_FLUSH_PENDING,
1909 					 * ESI_FORWARD_DIRECT
1910 					 */
1911 					entry->vcc = tmp->vcc;
1912 					entry->old_push = tmp->old_push;
1913 				}
1914 				entry->status = tmp->status;
1915 				break;
1916 			}
1917 		}
1918 	}
1919 	if (remoteflag)
1920 		entry->flags |= LEC_REMOTE_FLAG;
1921 	else
1922 		entry->flags &= ~LEC_REMOTE_FLAG;
1923 	if (entry->status == ESI_ARP_PENDING || entry->status == ESI_UNKNOWN) {
1924 		entry->status = ESI_VC_PENDING;
1925 		send_to_lecd(priv, l_svc_setup, entry->mac_addr, atm_addr, NULL);
1926 	}
1927 	pr_debug("After update2\n");
1928 	dump_arp_table(priv);
1929 out:
1930 	spin_unlock_irqrestore(&priv->lec_arp_lock, flags);
1931 }
1932 
1933 /*
1934  * Notifies: Vcc setup ready
1935  */
1936 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))1937 lec_vcc_added(struct lec_priv *priv, const struct atmlec_ioc *ioc_data,
1938 	      struct atm_vcc *vcc,
1939 	      void (*old_push) (struct atm_vcc *vcc, struct sk_buff *skb))
1940 {
1941 	unsigned long flags;
1942 	struct lec_arp_table *entry;
1943 	int i, found_entry = 0;
1944 
1945 	spin_lock_irqsave(&priv->lec_arp_lock, flags);
1946 	/* Vcc for Multicast Forward. No timer, LANEv2 7.1.20 and 2.3.5.3 */
1947 	if (ioc_data->receive == 2) {
1948 		pr_debug("LEC_ARP: Attaching mcast forward\n");
1949 #if 0
1950 		entry = lec_arp_find(priv, bus_mac);
1951 		if (!entry) {
1952 			pr_info("LEC_ARP: Multicast entry not found!\n");
1953 			goto out;
1954 		}
1955 		memcpy(entry->atm_addr, ioc_data->atm_addr, ATM_ESA_LEN);
1956 		entry->recv_vcc = vcc;
1957 		entry->old_recv_push = old_push;
1958 #endif
1959 		entry = make_entry(priv, bus_mac);
1960 		if (entry == NULL)
1961 			goto out;
1962 		timer_delete(&entry->timer);
1963 		memcpy(entry->atm_addr, ioc_data->atm_addr, ATM_ESA_LEN);
1964 		entry->recv_vcc = vcc;
1965 		entry->old_recv_push = old_push;
1966 		hlist_add_head(&entry->next, &priv->mcast_fwds);
1967 		goto out;
1968 	} else if (ioc_data->receive == 1) {
1969 		/*
1970 		 * Vcc which we don't want to make default vcc,
1971 		 * attach it anyway.
1972 		 */
1973 		pr_debug("LEC_ARP:Attaching data direct, not default: %*phN\n",
1974 			 ATM_ESA_LEN, ioc_data->atm_addr);
1975 		entry = make_entry(priv, bus_mac);
1976 		if (entry == NULL)
1977 			goto out;
1978 		memcpy(entry->atm_addr, ioc_data->atm_addr, ATM_ESA_LEN);
1979 		eth_zero_addr(entry->mac_addr);
1980 		entry->recv_vcc = vcc;
1981 		entry->old_recv_push = old_push;
1982 		entry->status = ESI_UNKNOWN;
1983 		entry->timer.expires = jiffies + priv->vcc_timeout_period;
1984 		entry->timer.function = lec_arp_expire_vcc;
1985 		hlist_add_head(&entry->next, &priv->lec_no_forward);
1986 		add_timer(&entry->timer);
1987 		dump_arp_table(priv);
1988 		goto out;
1989 	}
1990 	pr_debug("LEC_ARP:Attaching data direct, default: %*phN\n",
1991 		 ATM_ESA_LEN, ioc_data->atm_addr);
1992 	for (i = 0; i < LEC_ARP_TABLE_SIZE; i++) {
1993 		hlist_for_each_entry(entry,
1994 				     &priv->lec_arp_tables[i], next) {
1995 			if (memcmp
1996 			    (ioc_data->atm_addr, entry->atm_addr,
1997 			     ATM_ESA_LEN) == 0) {
1998 				pr_debug("LEC_ARP: Attaching data direct\n");
1999 				pr_debug("Currently -> Vcc: %d, Rvcc:%d\n",
2000 					 entry->vcc ? entry->vcc->vci : 0,
2001 					 entry->recv_vcc ? entry->recv_vcc->
2002 					 vci : 0);
2003 				found_entry = 1;
2004 				timer_delete(&entry->timer);
2005 				entry->vcc = vcc;
2006 				entry->old_push = old_push;
2007 				if (entry->status == ESI_VC_PENDING) {
2008 					if (priv->maximum_unknown_frame_count
2009 					    == 0)
2010 						entry->status =
2011 						    ESI_FORWARD_DIRECT;
2012 					else {
2013 						entry->timestamp = jiffies;
2014 						entry->status =
2015 						    ESI_FLUSH_PENDING;
2016 #if 0
2017 						send_to_lecd(priv, l_flush_xmt,
2018 							     NULL,
2019 							     entry->atm_addr,
2020 							     NULL);
2021 #endif
2022 					}
2023 				} else {
2024 					/*
2025 					 * They were forming a connection
2026 					 * to us, and we to them. Our
2027 					 * ATM address is numerically lower
2028 					 * than theirs, so we make connection
2029 					 * we formed into default VCC (8.1.11).
2030 					 * Connection they made gets torn
2031 					 * down. This might confuse some
2032 					 * clients. Can be changed if
2033 					 * someone reports trouble...
2034 					 */
2035 					;
2036 				}
2037 			}
2038 		}
2039 	}
2040 	if (found_entry) {
2041 		pr_debug("After vcc was added\n");
2042 		dump_arp_table(priv);
2043 		goto out;
2044 	}
2045 	/*
2046 	 * Not found, snatch address from first data packet that arrives
2047 	 * from this vcc
2048 	 */
2049 	entry = make_entry(priv, bus_mac);
2050 	if (!entry)
2051 		goto out;
2052 	entry->vcc = vcc;
2053 	entry->old_push = old_push;
2054 	memcpy(entry->atm_addr, ioc_data->atm_addr, ATM_ESA_LEN);
2055 	eth_zero_addr(entry->mac_addr);
2056 	entry->status = ESI_UNKNOWN;
2057 	hlist_add_head(&entry->next, &priv->lec_arp_empty_ones);
2058 	entry->timer.expires = jiffies + priv->vcc_timeout_period;
2059 	entry->timer.function = lec_arp_expire_vcc;
2060 	add_timer(&entry->timer);
2061 	pr_debug("After vcc was added\n");
2062 	dump_arp_table(priv);
2063 out:
2064 	spin_unlock_irqrestore(&priv->lec_arp_lock, flags);
2065 }
2066 
lec_flush_complete(struct lec_priv * priv,unsigned long tran_id)2067 static void lec_flush_complete(struct lec_priv *priv, unsigned long tran_id)
2068 {
2069 	unsigned long flags;
2070 	struct lec_arp_table *entry;
2071 	int i;
2072 
2073 	pr_debug("%lx\n", tran_id);
2074 restart:
2075 	spin_lock_irqsave(&priv->lec_arp_lock, flags);
2076 	for (i = 0; i < LEC_ARP_TABLE_SIZE; i++) {
2077 		hlist_for_each_entry(entry,
2078 				     &priv->lec_arp_tables[i], next) {
2079 			if (entry->flush_tran_id == tran_id &&
2080 			    entry->status == ESI_FLUSH_PENDING) {
2081 				struct sk_buff *skb;
2082 				struct atm_vcc *vcc = entry->vcc;
2083 
2084 				lec_arp_hold(entry);
2085 				spin_unlock_irqrestore(&priv->lec_arp_lock,
2086 						       flags);
2087 				while ((skb = skb_dequeue(&entry->tx_wait)))
2088 					lec_send(vcc, skb);
2089 				entry->last_used = jiffies;
2090 				entry->status = ESI_FORWARD_DIRECT;
2091 				lec_arp_put(entry);
2092 				pr_debug("LEC_ARP: Flushed\n");
2093 				goto restart;
2094 			}
2095 		}
2096 	}
2097 	spin_unlock_irqrestore(&priv->lec_arp_lock, flags);
2098 	dump_arp_table(priv);
2099 }
2100 
2101 static void
lec_set_flush_tran_id(struct lec_priv * priv,const unsigned char * atm_addr,unsigned long tran_id)2102 lec_set_flush_tran_id(struct lec_priv *priv,
2103 		      const unsigned char *atm_addr, unsigned long tran_id)
2104 {
2105 	unsigned long flags;
2106 	struct lec_arp_table *entry;
2107 	int i;
2108 
2109 	spin_lock_irqsave(&priv->lec_arp_lock, flags);
2110 	for (i = 0; i < LEC_ARP_TABLE_SIZE; i++)
2111 		hlist_for_each_entry(entry,
2112 				     &priv->lec_arp_tables[i], next) {
2113 			if (!memcmp(atm_addr, entry->atm_addr, ATM_ESA_LEN)) {
2114 				entry->flush_tran_id = tran_id;
2115 				pr_debug("Set flush transaction id to %lx for %p\n",
2116 					 tran_id, entry);
2117 			}
2118 		}
2119 	spin_unlock_irqrestore(&priv->lec_arp_lock, flags);
2120 }
2121 
lec_mcast_make(struct lec_priv * priv,struct atm_vcc * vcc)2122 static int lec_mcast_make(struct lec_priv *priv, struct atm_vcc *vcc)
2123 {
2124 	unsigned long flags;
2125 	unsigned char mac_addr[] = {
2126 		0xff, 0xff, 0xff, 0xff, 0xff, 0xff
2127 	};
2128 	struct lec_arp_table *to_add;
2129 	struct lec_vcc_priv *vpriv;
2130 	int err = 0;
2131 
2132 	vpriv = kmalloc_obj(struct lec_vcc_priv);
2133 	if (!vpriv)
2134 		return -ENOMEM;
2135 	vpriv->xoff = 0;
2136 	vpriv->old_pop = vcc->pop;
2137 	vcc->user_back = vpriv;
2138 	vcc->pop = lec_pop;
2139 	spin_lock_irqsave(&priv->lec_arp_lock, flags);
2140 	to_add = make_entry(priv, mac_addr);
2141 	if (!to_add) {
2142 		vcc->pop = vpriv->old_pop;
2143 		kfree(vpriv);
2144 		err = -ENOMEM;
2145 		goto out;
2146 	}
2147 	memcpy(to_add->atm_addr, vcc->remote.sas_addr.prv, ATM_ESA_LEN);
2148 	to_add->status = ESI_FORWARD_DIRECT;
2149 	to_add->flags |= LEC_PERMANENT_FLAG;
2150 	to_add->vcc = vcc;
2151 	to_add->old_push = vcc->push;
2152 	vcc->push = lec_push;
2153 	priv->mcast_vcc = vcc;
2154 	lec_arp_add(priv, to_add);
2155 out:
2156 	spin_unlock_irqrestore(&priv->lec_arp_lock, flags);
2157 	return err;
2158 }
2159 
lec_vcc_close(struct lec_priv * priv,struct atm_vcc * vcc)2160 static void lec_vcc_close(struct lec_priv *priv, struct atm_vcc *vcc)
2161 {
2162 	unsigned long flags;
2163 	struct hlist_node *next;
2164 	struct lec_arp_table *entry;
2165 	int i;
2166 
2167 	pr_debug("LEC_ARP: lec_vcc_close vpi:%d vci:%d\n", vcc->vpi, vcc->vci);
2168 	dump_arp_table(priv);
2169 
2170 	spin_lock_irqsave(&priv->lec_arp_lock, flags);
2171 
2172 	for (i = 0; i < LEC_ARP_TABLE_SIZE; i++) {
2173 		hlist_for_each_entry_safe(entry, next,
2174 					  &priv->lec_arp_tables[i], next) {
2175 			if (vcc == entry->vcc) {
2176 				lec_arp_remove(priv, entry);
2177 				lec_arp_put(entry);
2178 				if (priv->mcast_vcc == vcc)
2179 					priv->mcast_vcc = NULL;
2180 			}
2181 		}
2182 	}
2183 
2184 	hlist_for_each_entry_safe(entry, next,
2185 				  &priv->lec_arp_empty_ones, next) {
2186 		if (entry->vcc == vcc) {
2187 			lec_arp_clear_vccs(entry);
2188 			timer_delete(&entry->timer);
2189 			hlist_del(&entry->next);
2190 			lec_arp_put(entry);
2191 		}
2192 	}
2193 
2194 	hlist_for_each_entry_safe(entry, next,
2195 				  &priv->lec_no_forward, next) {
2196 		if (entry->recv_vcc == vcc) {
2197 			lec_arp_clear_vccs(entry);
2198 			timer_delete(&entry->timer);
2199 			hlist_del(&entry->next);
2200 			lec_arp_put(entry);
2201 		}
2202 	}
2203 
2204 	hlist_for_each_entry_safe(entry, next, &priv->mcast_fwds, next) {
2205 		if (entry->recv_vcc == vcc) {
2206 			lec_arp_clear_vccs(entry);
2207 			/* No timer, LANEv2 7.1.20 and 2.3.5.3 */
2208 			hlist_del(&entry->next);
2209 			lec_arp_put(entry);
2210 		}
2211 	}
2212 
2213 	spin_unlock_irqrestore(&priv->lec_arp_lock, flags);
2214 	dump_arp_table(priv);
2215 }
2216 
2217 static void
lec_arp_check_empties(struct lec_priv * priv,struct atm_vcc * vcc,struct sk_buff * skb)2218 lec_arp_check_empties(struct lec_priv *priv,
2219 		      struct atm_vcc *vcc, struct sk_buff *skb)
2220 {
2221 	unsigned long flags;
2222 	struct hlist_node *next;
2223 	struct lec_arp_table *entry, *tmp;
2224 	struct lecdatahdr_8023 *hdr = (struct lecdatahdr_8023 *)skb->data;
2225 	unsigned char *src = hdr->h_source;
2226 
2227 	spin_lock_irqsave(&priv->lec_arp_lock, flags);
2228 	hlist_for_each_entry_safe(entry, next,
2229 				  &priv->lec_arp_empty_ones, next) {
2230 		if (vcc == entry->vcc) {
2231 			timer_delete(&entry->timer);
2232 			ether_addr_copy(entry->mac_addr, src);
2233 			entry->status = ESI_FORWARD_DIRECT;
2234 			entry->last_used = jiffies;
2235 			/* We might have got an entry */
2236 			tmp = lec_arp_find(priv, src);
2237 			if (tmp) {
2238 				lec_arp_remove(priv, tmp);
2239 				lec_arp_put(tmp);
2240 			}
2241 			hlist_del(&entry->next);
2242 			lec_arp_add(priv, entry);
2243 			goto out;
2244 		}
2245 	}
2246 	pr_debug("LEC_ARP: Arp_check_empties: entry not found!\n");
2247 out:
2248 	spin_unlock_irqrestore(&priv->lec_arp_lock, flags);
2249 }
2250 
2251 MODULE_DESCRIPTION("ATM LAN Emulation (LANE) support");
2252 MODULE_LICENSE("GPL");
2253