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