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