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