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