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