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