lec.c 85.8 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/*
 * lec.c: Lan Emulation driver 
 * Marko Kiiskila carnil@cs.tut.fi
 *
 */

#include <linux/config.h>
#include <linux/kernel.h>
#include <linux/bitops.h>

/* We are ethernet device */
#include <linux/if_ether.h>
#include <linux/netdevice.h>
#include <linux/etherdevice.h>
#include <net/sock.h>
#include <linux/skbuff.h>
#include <linux/ip.h>
#include <asm/byteorder.h>
#include <asm/uaccess.h>
#include <net/arp.h>
#include <net/dst.h>
#include <linux/proc_fs.h>
Chas Williams's avatar
Chas Williams committed
23
#include <linux/spinlock.h>
24 25
#include <linux/proc_fs.h>
#include <linux/seq_file.h>
Linus Torvalds's avatar
Linus Torvalds committed
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40

/* TokenRing if needed */
#ifdef CONFIG_TR
#include <linux/trdevice.h>
#endif

/* And atm device */
#include <linux/atmdev.h>
#include <linux/atmlec.h>

/* Proxy LEC knows about bridging */
#if defined(CONFIG_BRIDGE) || defined(CONFIG_BRIDGE_MODULE)
#include <linux/if_bridge.h>
#include "../bridge/br_private.h"
static unsigned char bridge_ula_lec[] = {0x01, 0x80, 0xc2, 0x00, 0x00};
41 42 43 44

extern struct net_bridge_fdb_entry *(*br_fdb_get_hook)(struct net_bridge *br,
       unsigned char *addr);
extern void (*br_fdb_put_hook)(struct net_bridge_fdb_entry *ent);
Linus Torvalds's avatar
Linus Torvalds committed
45 46 47 48 49 50 51 52
#endif

/* Modular too */
#include <linux/module.h>
#include <linux/init.h>

#include "lec.h"
#include "lec_arpc.h"
53
#include "resources.h"
Linus Torvalds's avatar
Linus Torvalds committed
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69

#if 0
#define DPRINTK printk
#else
#define DPRINTK(format,args...)
#endif

#define DUMP_PACKETS 0 /* 0 = None,
                        * 1 = 30 first bytes
                        * 2 = Whole packet
                        */

#define LEC_UNRES_QUE_LEN 8 /* number of tx packets to queue for a
                               single destination while waiting for SVC */

static int lec_open(struct net_device *dev);
70
static int lec_start_xmit(struct sk_buff *skb, struct net_device *dev);
Linus Torvalds's avatar
Linus Torvalds committed
71 72 73
static int lec_close(struct net_device *dev);
static struct net_device_stats *lec_get_stats(struct net_device *dev);
static void lec_init(struct net_device *dev);
74
static inline struct lec_arp_table* lec_arp_find(struct lec_priv *priv,
Linus Torvalds's avatar
Linus Torvalds committed
75
                                                     unsigned char *mac_addr);
76
static inline int lec_arp_remove(struct lec_priv *priv,
Linus Torvalds's avatar
Linus Torvalds committed
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
				     struct lec_arp_table *to_remove);
/* LANE2 functions */
static void lane2_associate_ind (struct net_device *dev, u8 *mac_address,
                          u8 *tlvs, u32 sizeoftlvs);
static int lane2_resolve(struct net_device *dev, u8 *dst_mac, int force,
                  u8 **tlvs, u32 *sizeoftlvs);
static int lane2_associate_req (struct net_device *dev, u8 *lan_dst,
                         u8 *tlvs, u32 sizeoftlvs);

static struct lane2_ops lane2_ops = {
	lane2_resolve,         /* resolve,             spec 3.1.3 */
	lane2_associate_req,   /* associate_req,       spec 3.1.4 */
	NULL                  /* associate indicator, spec 3.1.5 */
};

static unsigned char bus_mac[ETH_ALEN] = {0xff,0xff,0xff,0xff,0xff,0xff};

/* Device structures */
static struct net_device *dev_lec[MAX_LEC_ITF];

/* This will be called from proc.c via function pointer */
98 99 100 101 102 103 104 105 106 107 108 109
struct net_device *get_dev_lec(int itf)
{
	struct net_device *dev;

	if (itf >= MAX_LEC_ITF)
		return NULL;
	rtnl_lock();
	dev = dev_lec[itf];
	if (dev)
		dev_hold(dev);
	rtnl_unlock();
	return dev;
Linus Torvalds's avatar
Linus Torvalds committed
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
}

#if defined(CONFIG_BRIDGE) || defined(CONFIG_BRIDGE_MODULE)
static void lec_handle_bridge(struct sk_buff *skb, struct net_device *dev)
{
        struct ethhdr *eth;
        char *buff;
        struct lec_priv *priv;

        /* Check if this is a BPDU. If so, ask zeppelin to send
         * LE_TOPOLOGY_REQUEST with the same value of Topology Change bit
         * as the Config BPDU has */
        eth = (struct ethhdr *)skb->data;
        buff = skb->data + skb->dev->hard_header_len;
        if (*buff++ == 0x42 && *buff++ == 0x42 && *buff++ == 0x03) {
                struct sk_buff *skb2;
                struct atmlec_msg *mesg;

                skb2 = alloc_skb(sizeof(struct atmlec_msg), GFP_ATOMIC);
                if (skb2 == NULL) return;
                skb2->len = sizeof(struct atmlec_msg);
                mesg = (struct atmlec_msg *)skb2->data;
                mesg->type = l_topology_change;
                buff += 4;
                mesg->content.normal.flag = *buff & 0x01; /* 0x01 is topology change */

                priv = (struct lec_priv *)dev->priv;
                atm_force_charge(priv->lecd, skb2->truesize);
138
                skb_queue_tail(&priv->lecd->sk->sk_receive_queue, skb2);
139
                priv->lecd->sk->sk_data_ready(priv->lecd->sk, skb2->len);
Linus Torvalds's avatar
Linus Torvalds committed
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
        }

        return;
}
#endif /* defined(CONFIG_BRIDGE) || defined(CONFIG_BRIDGE_MODULE) */

/*
 * Modelled after tr_type_trans
 * All multicast and ARE or STE frames go to BUS.
 * Non source routed frames go by destination address.
 * Last hop source routed frames go by destination address.
 * Not last hop source routed frames go by _next_ route descriptor.
 * Returns pointer to destination MAC address or fills in rdesc
 * and returns NULL.
 */
#ifdef CONFIG_TR
unsigned char *get_tr_dst(unsigned char *packet, unsigned char *rdesc)
{
        struct trh_hdr *trh;
        int riflen, num_rdsc;
        
        trh = (struct trh_hdr *)packet;
        if (trh->daddr[0] & (uint8_t)0x80)
                return bus_mac; /* multicast */

        if (trh->saddr[0] & TR_RII) {
                riflen = (ntohs(trh->rcf) & TR_RCF_LEN_MASK) >> 8;
                if ((ntohs(trh->rcf) >> 13) != 0)
                        return bus_mac; /* ARE or STE */
        }
        else
                return trh->daddr; /* not source routed */

        if (riflen < 6)
                return trh->daddr; /* last hop, source routed */
                
        /* riflen is 6 or more, packet has more than one route descriptor */
        num_rdsc = (riflen/2) - 1;
        memset(rdesc, 0, ETH_ALEN);
        /* offset 4 comes from LAN destination field in LE control frames */
        if (trh->rcf & htons((uint16_t)TR_RCF_DIR_BIT))
                memcpy(&rdesc[4], &trh->rseg[num_rdsc-2], sizeof(uint16_t));
        else {
                memcpy(&rdesc[4], &trh->rseg[1], sizeof(uint16_t));
                rdesc[5] = ((ntohs(trh->rseg[0]) & 0x000f) | (rdesc[5] & 0xf0));
        }

        return NULL;
}
#endif /* CONFIG_TR */

/*
 * Open/initialize the netdevice. This is called (in the current kernel)
 * sometime after booting when the 'ifconfig' program is run.
 *
 * This routine should set everything up anew at each open, even
 * registers that "should" only need to be set once at boot, so that
 * there is non-reboot way to recover if something goes wrong.
 */

static int 
lec_open(struct net_device *dev)
{
        struct lec_priv *priv = (struct lec_priv *)dev->priv;
        
	netif_start_queue(dev);
        memset(&priv->stats,0,sizeof(struct net_device_stats));
        
        return 0;
}

211 212 213
static __inline__ void
lec_send(struct atm_vcc *vcc, struct sk_buff *skb, struct lec_priv *priv)
{
214 215 216 217 218
	ATM_SKB(skb)->vcc = vcc;
	ATM_SKB(skb)->atm_options = vcc->atm_options;

	atomic_add(skb->truesize, &vcc->sk->sk_wmem_alloc);
	if (vcc->send(vcc, skb) < 0) {
219
		priv->stats.tx_dropped++;
220
		return;
221
	}
222 223 224 225 226 227 228 229 230 231 232

	priv->stats.tx_packets++;
	priv->stats.tx_bytes += skb->len;
}

static void
lec_tx_timeout(struct net_device *dev)
{
	printk(KERN_INFO "%s: tx timeout\n", dev->name);
	dev->trans_start = jiffies;
	netif_wake_queue(dev);
233 234
}

Linus Torvalds's avatar
Linus Torvalds committed
235
static int 
236
lec_start_xmit(struct sk_buff *skb, struct net_device *dev)
Linus Torvalds's avatar
Linus Torvalds committed
237 238 239 240
{
        struct sk_buff *skb2;
        struct lec_priv *priv = (struct lec_priv *)dev->priv;
        struct lecdatahdr_8023 *lec_h;
241
        struct atm_vcc *vcc;
Linus Torvalds's avatar
Linus Torvalds committed
242
	struct lec_arp_table *entry;
243 244
        unsigned char *dst;
	int min_frame_size;
Linus Torvalds's avatar
Linus Torvalds committed
245 246 247 248 249 250 251 252 253
#ifdef CONFIG_TR
        unsigned char rdesc[ETH_ALEN]; /* Token Ring route descriptor */
#endif
        int is_rdesc;
#if DUMP_PACKETS > 0
        char buf[300];
        int i=0;
#endif /* DUMP_PACKETS >0 */
        
254
        DPRINTK("lec_start_xmit called\n");  
Linus Torvalds's avatar
Linus Torvalds committed
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
        if (!priv->lecd) {
                printk("%s:No lecd attached\n",dev->name);
                priv->stats.tx_errors++;
                netif_stop_queue(dev);
                return -EUNATCH;
        } 

        DPRINTK("skbuff head:%lx data:%lx tail:%lx end:%lx\n",
                (long)skb->head, (long)skb->data, (long)skb->tail,
                (long)skb->end);
#if defined(CONFIG_BRIDGE) || defined(CONFIG_BRIDGE_MODULE)
        if (memcmp(skb->data, bridge_ula_lec, sizeof(bridge_ula_lec)) == 0)
                lec_handle_bridge(skb, dev);
#endif

        /* Make sure we have room for lec_id */
        if (skb_headroom(skb) < 2) {

273
                DPRINTK("lec_start_xmit: reallocating skb\n");
Linus Torvalds's avatar
Linus Torvalds committed
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
                skb2 = skb_realloc_headroom(skb, LEC_HEADER_LEN);
                kfree_skb(skb);
                if (skb2 == NULL) return 0;
                skb = skb2;
        }
        skb_push(skb, 2);

        /* Put le header to place, works for TokenRing too */
        lec_h = (struct lecdatahdr_8023*)skb->data;
        lec_h->le_header = htons(priv->lecid); 

#ifdef CONFIG_TR
        /* Ugly. Use this to realign Token Ring packets for
         * e.g. PCA-200E driver. */
        if (priv->is_trdev) {
                skb2 = skb_realloc_headroom(skb, LEC_HEADER_LEN);
                kfree_skb(skb);
                if (skb2 == NULL) return 0;
                skb = skb2;
        }
#endif

#if DUMP_PACKETS > 0
        printk("%s: send datalen:%ld lecid:%4.4x\n", dev->name,
               skb->len, priv->lecid);
#if DUMP_PACKETS >= 2
        for(i=0;i<skb->len && i <99;i++) {
                sprintf(buf+i*3,"%2.2x ",0xff&skb->data[i]);
        }
#elif DUMP_PACKETS >= 1
        for(i=0;i<skb->len && i < 30;i++) {
                sprintf(buf+i*3,"%2.2x ", 0xff&skb->data[i]);
        }
#endif /* DUMP_PACKETS >= 1 */
        if (i==skb->len)
                printk("%s\n",buf);
        else
                printk("%s...\n",buf);
#endif /* DUMP_PACKETS > 0 */

        /* Minimum ethernet-frame size */
315 316 317 318 319 320 321
#ifdef CONFIG_TR
        if (priv->is_trdev)
                min_frame_size = LEC_MINIMUM_8025_SIZE;
	else
#endif
        min_frame_size = LEC_MINIMUM_8023_SIZE;
        if (skb->len < min_frame_size) {
322
                if ((skb->len + skb_tailroom(skb)) < min_frame_size) {
323 324
                        skb2 = skb_copy_expand(skb, 0,
                            min_frame_size - skb->truesize, GFP_ATOMIC);
Linus Torvalds's avatar
Linus Torvalds committed
325
                                dev_kfree_skb(skb);
326 327
                        if (skb2 == NULL) {
                                priv->stats.tx_dropped++;
Linus Torvalds's avatar
Linus Torvalds committed
328 329
                                return 0;
                        }
330
                        skb = skb2;
Linus Torvalds's avatar
Linus Torvalds committed
331
                }
332
		skb_put(skb, min_frame_size - skb->len);
Linus Torvalds's avatar
Linus Torvalds committed
333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
        }
        
        /* Send to right vcc */
        is_rdesc = 0;
        dst = lec_h->h_dest;
#ifdef CONFIG_TR
        if (priv->is_trdev) {
                dst = get_tr_dst(skb->data+2, rdesc);
                if (dst == NULL) {
                        dst = rdesc;
                        is_rdesc = 1;
                }
        }
#endif
        entry = NULL;
348 349 350 351
        vcc = lec_arp_resolve(priv, dst, is_rdesc, &entry);
        DPRINTK("%s:vcc:%p vcc_flags:%x, entry:%p\n", dev->name,
                vcc, vcc?vcc->flags:0, entry);
        if (!vcc || !test_bit(ATM_VF_READY,&vcc->flags)) {    
Linus Torvalds's avatar
Linus Torvalds committed
352
                if (entry && (entry->tx_wait.qlen < LEC_UNRES_QUE_LEN)) {
353
                        DPRINTK("%s:lec_start_xmit: queuing packet, ", dev->name);
Linus Torvalds's avatar
Linus Torvalds committed
354 355 356 357 358
                        DPRINTK("MAC address 0x%02x:%02x:%02x:%02x:%02x:%02x\n",
                                lec_h->h_dest[0], lec_h->h_dest[1], lec_h->h_dest[2],
                                lec_h->h_dest[3], lec_h->h_dest[4], lec_h->h_dest[5]);
                        skb_queue_tail(&entry->tx_wait, skb);
                } else {
359
                        DPRINTK("%s:lec_start_xmit: tx queue full or no arp entry, dropping, ", dev->name);
Linus Torvalds's avatar
Linus Torvalds committed
360 361 362 363 364 365 366 367 368 369 370
                        DPRINTK("MAC address 0x%02x:%02x:%02x:%02x:%02x:%02x\n",
                                lec_h->h_dest[0], lec_h->h_dest[1], lec_h->h_dest[2],
                                lec_h->h_dest[3], lec_h->h_dest[4], lec_h->h_dest[5]);
                        priv->stats.tx_dropped++;
                        dev_kfree_skb(skb);
                }
                return 0;
        }
                
#if DUMP_PACKETS > 0                    
        printk("%s:sending to vpi:%d vci:%d\n", dev->name,
371
               vcc->vpi, vcc->vci);       
Linus Torvalds's avatar
Linus Torvalds committed
372 373 374 375 376 377 378
#endif /* DUMP_PACKETS > 0 */
                
        while (entry && (skb2 = skb_dequeue(&entry->tx_wait))) {
                DPRINTK("lec.c: emptying tx queue, ");
                DPRINTK("MAC address 0x%02x:%02x:%02x:%02x:%02x:%02x\n",
                        lec_h->h_dest[0], lec_h->h_dest[1], lec_h->h_dest[2],
                        lec_h->h_dest[3], lec_h->h_dest[4], lec_h->h_dest[5]);
379
		lec_send(vcc, skb2, priv);
Linus Torvalds's avatar
Linus Torvalds committed
380 381
        }

382
	lec_send(vcc, skb, priv);
Linus Torvalds's avatar
Linus Torvalds committed
383

384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400
	if (!atm_may_send(vcc, 0)) {
		struct lec_vcc_priv *vpriv = LEC_VCC_PRIV(vcc);

		vpriv->xoff = 1;
		netif_stop_queue(dev);

		/*
		 * vcc->pop() might have occurred in between, making
		 * the vcc usuable again.  Since xmit is serialized,
		 * this is the only situation we have to re-test.
		 */

		if (atm_may_send(vcc, 0))
			netif_wake_queue(dev);
	}

	dev->trans_start = jiffies;
Linus Torvalds's avatar
Linus Torvalds committed
401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431
        return 0;
}

/* The inverse routine to net_open(). */
static int 
lec_close(struct net_device *dev) 
{
        netif_stop_queue(dev);
        return 0;
}

/*
 * Get the current statistics.
 * This may be called with the card open or closed.
 */
static struct net_device_stats *
lec_get_stats(struct net_device *dev)
{
        return &((struct lec_priv *)dev->priv)->stats;
}

static int 
lec_atm_send(struct atm_vcc *vcc, struct sk_buff *skb)
{
        struct net_device *dev = (struct net_device*)vcc->proto_data;
        struct lec_priv *priv = (struct lec_priv*)dev->priv;
        struct atmlec_msg *mesg;
        struct lec_arp_table *entry;
        int i;
        char *tmp; /* FIXME */

432
	atomic_sub(skb->truesize, &vcc->sk->sk_wmem_alloc);
Linus Torvalds's avatar
Linus Torvalds committed
433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459
        mesg = (struct atmlec_msg *)skb->data;
        tmp = skb->data;
        tmp += sizeof(struct atmlec_msg);
        DPRINTK("%s: msg from zeppelin:%d\n", dev->name, mesg->type);
        switch(mesg->type) {
        case l_set_mac_addr:
                for (i=0;i<6;i++) {
                        dev->dev_addr[i] = mesg->content.normal.mac_addr[i];
                }    
                break;
        case l_del_mac_addr:
                for(i=0;i<6;i++) {
                        dev->dev_addr[i] = 0;
                }
                break;
        case l_addr_delete:
                lec_addr_delete(priv, mesg->content.normal.atm_addr, 
                                mesg->content.normal.flag);
                break;
        case l_topology_change:
                priv->topology_change = mesg->content.normal.flag;  
                break;
        case l_flush_complete:
                lec_flush_complete(priv, mesg->content.normal.flag);
                break;
        case l_narp_req: /* LANE2: see 7.1.35 in the lane2 spec */
                entry = lec_arp_find(priv, mesg->content.normal.mac_addr);
460
                lec_arp_remove(priv, entry);
Linus Torvalds's avatar
Linus Torvalds committed
461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537

                if (mesg->content.normal.no_source_le_narp)
                        break;
                /* FALL THROUGH */
        case l_arp_update:
                lec_arp_update(priv, mesg->content.normal.mac_addr,
                               mesg->content.normal.atm_addr,
                               mesg->content.normal.flag,
                               mesg->content.normal.targetless_le_arp);
                DPRINTK("lec: in l_arp_update\n");
                if (mesg->sizeoftlvs != 0) { /* LANE2 3.1.5 */
                        DPRINTK("lec: LANE2 3.1.5, got tlvs, size %d\n", mesg->sizeoftlvs);
                        lane2_associate_ind(dev,
                                            mesg->content.normal.mac_addr,
                                            tmp, mesg->sizeoftlvs);
                }
                break;
        case l_config:
                priv->maximum_unknown_frame_count = 
                        mesg->content.config.maximum_unknown_frame_count;
                priv->max_unknown_frame_time = 
                        (mesg->content.config.max_unknown_frame_time*HZ);
                priv->max_retry_count = 
                        mesg->content.config.max_retry_count;
                priv->aging_time = (mesg->content.config.aging_time*HZ);
                priv->forward_delay_time = 
                        (mesg->content.config.forward_delay_time*HZ);
                priv->arp_response_time = 
                        (mesg->content.config.arp_response_time*HZ);
                priv->flush_timeout = (mesg->content.config.flush_timeout*HZ);
                priv->path_switching_delay = 
                        (mesg->content.config.path_switching_delay*HZ);
                priv->lane_version = mesg->content.config.lane_version; /* LANE2 */
		priv->lane2_ops = NULL;
		if (priv->lane_version > 1)
			priv->lane2_ops = &lane2_ops;
		if (dev->change_mtu(dev, mesg->content.config.mtu))
			printk("%s: change_mtu to %d failed\n", dev->name,
			    mesg->content.config.mtu);
		priv->is_proxy = mesg->content.config.is_proxy;
                break;
        case l_flush_tran_id:
                lec_set_flush_tran_id(priv, mesg->content.normal.atm_addr,
                                      mesg->content.normal.flag);
                break;
        case l_set_lecid:
                priv->lecid=(unsigned short)(0xffff&mesg->content.normal.flag);
                break;
        case l_should_bridge: {
#if defined(CONFIG_BRIDGE) || defined(CONFIG_BRIDGE_MODULE)
                struct net_bridge_fdb_entry *f;

                DPRINTK("%s: bridge zeppelin asks about 0x%02x:%02x:%02x:%02x:%02x:%02x\n",
                        dev->name,
                        mesg->content.proxy.mac_addr[0], mesg->content.proxy.mac_addr[1],
                        mesg->content.proxy.mac_addr[2], mesg->content.proxy.mac_addr[3],
                        mesg->content.proxy.mac_addr[4], mesg->content.proxy.mac_addr[5]);

                if (br_fdb_get_hook == NULL || dev->br_port == NULL)
                        break;

                f = br_fdb_get_hook(dev->br_port->br, mesg->content.proxy.mac_addr);
                if (f != NULL &&
                    f->dst->dev != dev &&
                    f->dst->state == BR_STATE_FORWARDING) {
                                /* hit from bridge table, send LE_ARP_RESPONSE */
                        struct sk_buff *skb2;

                        DPRINTK("%s: entry found, responding to zeppelin\n", dev->name);
                        skb2 = alloc_skb(sizeof(struct atmlec_msg), GFP_ATOMIC);
                        if (skb2 == NULL) {
                                br_fdb_put_hook(f);
                                break;
                        }
                        skb2->len = sizeof(struct atmlec_msg);
                        memcpy(skb2->data, mesg, sizeof(struct atmlec_msg));
                        atm_force_charge(priv->lecd, skb2->truesize);
538
                        skb_queue_tail(&priv->lecd->sk->sk_receive_queue, skb2);
539
                        priv->lecd->sk->sk_data_ready(priv->lecd->sk, skb2->len);
Linus Torvalds's avatar
Linus Torvalds committed
540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566
                }
                if (f != NULL) br_fdb_put_hook(f);
#endif /* defined(CONFIG_BRIDGE) || defined(CONFIG_BRIDGE_MODULE) */
                }
                break;
        default:
                printk("%s: Unknown message type %d\n", dev->name, mesg->type);
                dev_kfree_skb(skb);
                return -EINVAL;
        }
        dev_kfree_skb(skb);
        return 0;
}

static void 
lec_atm_close(struct atm_vcc *vcc)
{
        struct sk_buff *skb;
        struct net_device *dev = (struct net_device *)vcc->proto_data;
        struct lec_priv *priv = (struct lec_priv *)dev->priv;

        priv->lecd = NULL;
        /* Do something needful? */

        netif_stop_queue(dev);
        lec_arp_destroy(priv);

567
        if (skb_peek(&vcc->sk->sk_receive_queue))
Linus Torvalds's avatar
Linus Torvalds committed
568 569
		printk("%s lec_atm_close: closing with messages pending\n",
                       dev->name);
570
        while ((skb = skb_dequeue(&vcc->sk->sk_receive_queue))) {
Linus Torvalds's avatar
Linus Torvalds committed
571 572 573 574 575
                atm_return(vcc, skb->truesize);
		dev_kfree_skb(skb);
        }
  
	printk("%s: Shut down!\n", dev->name);
576
        module_put(THIS_MODULE);
Linus Torvalds's avatar
Linus Torvalds committed
577 578 579
}

static struct atmdev_ops lecdev_ops = {
580 581
        .close	= lec_atm_close,
        .send	= lec_atm_send
Linus Torvalds's avatar
Linus Torvalds committed
582 583 584
};

static struct atm_dev lecatm_dev = {
585 586 587
	.ops	= &lecdev_ops,
	.type	= "lec",
	.number	= 999,	/* dummy device number */
588
	.lock	= SPIN_LOCK_UNLOCKED
Linus Torvalds's avatar
Linus Torvalds committed
589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622
};

/*
 * LANE2: new argument struct sk_buff *data contains
 * the LE_ARP based TLVs introduced in the LANE2 spec
 */
int 
send_to_lecd(struct lec_priv *priv, atmlec_msg_type type, 
             unsigned char *mac_addr, unsigned char *atm_addr,
             struct sk_buff *data)
{
	struct sk_buff *skb;
	struct atmlec_msg *mesg;

	if (!priv || !priv->lecd) {
		return -1;
	}
	skb = alloc_skb(sizeof(struct atmlec_msg), GFP_ATOMIC);
	if (!skb)
		return -1;
	skb->len = sizeof(struct atmlec_msg);
	mesg = (struct atmlec_msg *)skb->data;
        memset(mesg, 0, sizeof(struct atmlec_msg));
	mesg->type = type;
        if (data != NULL)
                mesg->sizeoftlvs = data->len;
	if (mac_addr)
		memcpy(&mesg->content.normal.mac_addr, mac_addr, ETH_ALEN);
        else
                mesg->content.normal.targetless_le_arp = 1;
	if (atm_addr)
		memcpy(&mesg->content.normal.atm_addr, atm_addr, ATM_ESA_LEN);

        atm_force_charge(priv->lecd, skb->truesize);
623
	skb_queue_tail(&priv->lecd->sk->sk_receive_queue, skb);
624
        priv->lecd->sk->sk_data_ready(priv->lecd->sk, skb->len);
Linus Torvalds's avatar
Linus Torvalds committed
625 626 627 628

        if (data != NULL) {
                DPRINTK("lec: about to send %d bytes of data\n", data->len);
                atm_force_charge(priv->lecd, data->truesize);
629
                skb_queue_tail(&priv->lecd->sk->sk_receive_queue, data);
630
                priv->lecd->sk->sk_data_ready(priv->lecd->sk, skb->len);
Linus Torvalds's avatar
Linus Torvalds committed
631 632 633 634 635 636 637 638 639 640 641 642 643 644
        }

        return 0;
}

/* shamelessly stolen from drivers/net/net_init.c */
static int lec_change_mtu(struct net_device *dev, int new_mtu)
{
        if ((new_mtu < 68) || (new_mtu > 18190))
                return -EINVAL;
        dev->mtu = new_mtu;
        return 0;
}

645 646 647 648 649 650 651 652
static void lec_set_multicast_list(struct net_device *dev)
{
	/* by default, all multicast frames arrive over the bus.
         * eventually support selective multicast service
         */
        return;
}

Linus Torvalds's avatar
Linus Torvalds committed
653 654 655 656 657 658
static void 
lec_init(struct net_device *dev)
{
        dev->change_mtu = lec_change_mtu;
        dev->open = lec_open;
        dev->stop = lec_close;
659 660
        dev->hard_start_xmit = lec_start_xmit;
	dev->tx_timeout = lec_tx_timeout;
Linus Torvalds's avatar
Linus Torvalds committed
661 662

        dev->get_stats = lec_get_stats;
663
        dev->set_multicast_list = lec_set_multicast_list;
Linus Torvalds's avatar
Linus Torvalds committed
664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711
        dev->do_ioctl  = NULL;
        printk("%s: Initialized!\n",dev->name);
        return;
}

static unsigned char lec_ctrl_magic[] = {
        0xff,
        0x00,
        0x01,
        0x01 };

void 
lec_push(struct atm_vcc *vcc, struct sk_buff *skb)
{
        struct net_device *dev = (struct net_device *)vcc->proto_data;
        struct lec_priv *priv = (struct lec_priv *)dev->priv; 

#if DUMP_PACKETS >0
        int i=0;
        char buf[300];

        printk("%s: lec_push vcc vpi:%d vci:%d\n", dev->name,
               vcc->vpi, vcc->vci);
#endif
        if (!skb) {
                DPRINTK("%s: null skb\n",dev->name);
                lec_vcc_close(priv, vcc);
                return;
        }
#if DUMP_PACKETS > 0
        printk("%s: rcv datalen:%ld lecid:%4.4x\n", dev->name,
               skb->len, priv->lecid);
#if DUMP_PACKETS >= 2
        for(i=0;i<skb->len && i <99;i++) {
                sprintf(buf+i*3,"%2.2x ",0xff&skb->data[i]);
        }
#elif DUMP_PACKETS >= 1
        for(i=0;i<skb->len && i < 30;i++) {
                sprintf(buf+i*3,"%2.2x ", 0xff&skb->data[i]);
        }
#endif /* DUMP_PACKETS >= 1 */
        if (i==skb->len)
                printk("%s\n",buf);
        else
                printk("%s...\n",buf);
#endif /* DUMP_PACKETS > 0 */
        if (memcmp(skb->data, lec_ctrl_magic, 4) ==0) { /* Control frame, to daemon*/
                DPRINTK("%s: To daemon\n",dev->name);
712
                skb_queue_tail(&vcc->sk->sk_receive_queue, skb);
713
                vcc->sk->sk_data_ready(vcc->sk, skb->len);
Linus Torvalds's avatar
Linus Torvalds committed
714 715 716 717 718
        } else { /* Data frame, queue to protocol handlers */
                unsigned char *dst;

                atm_return(vcc,skb->truesize);
                if (*(uint16_t *)skb->data == htons(priv->lecid) ||
719 720
                    !priv->lecd ||
                    !(dev->flags & IFF_UP)) { 
Linus Torvalds's avatar
Linus Torvalds committed
721 722
                        /* Probably looping back, or if lecd is missing,
                           lecd has gone down */
723
                        DPRINTK("Ignoring frame...\n");
Linus Torvalds's avatar
Linus Torvalds committed
724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742
                        dev_kfree_skb(skb);
                        return;
                }
#ifdef CONFIG_TR
                if (priv->is_trdev) dst = ((struct lecdatahdr_8025 *)skb->data)->h_dest;
                else
#endif
                dst = ((struct lecdatahdr_8023 *)skb->data)->h_dest;

                if (!(dst[0]&0x01) &&   /* Never filter Multi/Broadcast */
                    !priv->is_proxy &&  /* Proxy wants all the packets */
		    memcmp(dst, dev->dev_addr, dev->addr_len)) {
                        dev_kfree_skb(skb);
                        return;
                }
                if (priv->lec_arp_empty_ones) {
                        lec_arp_check_empties(priv, vcc, skb);
                }
                skb->dev = dev;
David S. Miller's avatar
David S. Miller committed
743
                skb_pull(skb, 2); /* skip lec_id */
Linus Torvalds's avatar
Linus Torvalds committed
744 745 746 747 748 749 750
#ifdef CONFIG_TR
                if (priv->is_trdev) skb->protocol = tr_type_trans(skb, dev);
                else
#endif
                skb->protocol = eth_type_trans(skb, dev);
                priv->stats.rx_packets++;
                priv->stats.rx_bytes += skb->len;
Chas Williams's avatar
Chas Williams committed
751
                memset(ATM_SKB(skb), 0, sizeof(struct atm_skb_data));
Linus Torvalds's avatar
Linus Torvalds committed
752 753 754 755
                netif_rx(skb);
        }
}

756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775
void
lec_pop(struct atm_vcc *vcc, struct sk_buff *skb)
{
	struct lec_vcc_priv *vpriv = LEC_VCC_PRIV(vcc);
	struct net_device *dev = skb->dev;

	if (vpriv == NULL) {
		printk("lec_pop(): vpriv = NULL!?!?!?\n");
		return;
	}

	vpriv->old_pop(vcc, skb);

	if (vpriv->xoff && atm_may_send(vcc, 0)) {
		vpriv->xoff = 0;
		if (netif_running(dev) && netif_queue_stopped(dev))
			netif_wake_queue(dev);
	}
}

Linus Torvalds's avatar
Linus Torvalds committed
776 777 778
int 
lec_vcc_attach(struct atm_vcc *vcc, void *arg)
{
779
	struct lec_vcc_priv *vpriv;
Linus Torvalds's avatar
Linus Torvalds committed
780 781 782 783 784 785 786 787 788 789 790 791
        int bytes_left;
        struct atmlec_ioc ioc_data;

        /* Lecd must be up in this case */
        bytes_left = copy_from_user(&ioc_data, arg, sizeof(struct atmlec_ioc));
        if (bytes_left != 0) {
                printk("lec: lec_vcc_attach, copy from user failed for %d bytes\n",
                       bytes_left);
        }
        if (ioc_data.dev_num < 0 || ioc_data.dev_num >= MAX_LEC_ITF || 
            !dev_lec[ioc_data.dev_num])
                return -EINVAL;
792 793 794 795 796 797
	if (!(vpriv = kmalloc(sizeof(struct lec_vcc_priv), GFP_KERNEL)))
		return -ENOMEM;
	vpriv->xoff = 0;
	vpriv->old_pop = vcc->pop;
	LEC_VCC_PRIV(vcc) = vpriv;
	vcc->pop = lec_pop;
Linus Torvalds's avatar
Linus Torvalds committed
798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841
        lec_vcc_added(dev_lec[ioc_data.dev_num]->priv, 
                      &ioc_data, vcc, vcc->push);
        vcc->push = lec_push;
        vcc->proto_data = dev_lec[ioc_data.dev_num];
        return 0;
}

int 
lec_mcast_attach(struct atm_vcc *vcc, int arg)
{
        if (arg <0 || arg >= MAX_LEC_ITF || !dev_lec[arg])
                return -EINVAL;
        vcc->proto_data = dev_lec[arg];
        return (lec_mcast_make((struct lec_priv*)dev_lec[arg]->priv, vcc));
}

/* Initialize device. */
int 
lecd_attach(struct atm_vcc *vcc, int arg)
{  
        int i;
        struct lec_priv *priv;

        if (arg<0)
                i = 0;
        else
                i = arg;
#ifdef CONFIG_TR
        if (arg >= MAX_LEC_ITF)
                return -EINVAL;
#else /* Reserve the top NUM_TR_DEVS for TR */
        if (arg >= (MAX_LEC_ITF-NUM_TR_DEVS))
                return -EINVAL;
#endif
        if (!dev_lec[i]) {
                int is_trdev, size;

                is_trdev = 0;
                if (i >= (MAX_LEC_ITF - NUM_TR_DEVS))
                        is_trdev = 1;

                size = sizeof(struct lec_priv);
#ifdef CONFIG_TR
                if (is_trdev)
842
                        dev_lec[i] = alloc_trdev(size);
Linus Torvalds's avatar
Linus Torvalds committed
843 844
                else
#endif
845
                dev_lec[i] = alloc_etherdev(size);
Linus Torvalds's avatar
Linus Torvalds committed
846 847
                if (!dev_lec[i])
                        return -ENOMEM;
848 849
                snprintf(dev_lec[i]->name, IFNAMSIZ, "lec%d", i);
                if (register_netdev(dev_lec[i])) {
850
                        free_netdev(dev_lec[i]);
851 852
                        return -EINVAL;
                }
Linus Torvalds's avatar
Linus Torvalds committed
853 854 855 856 857 858 859 860 861 862 863 864

                priv = dev_lec[i]->priv;
                priv->is_trdev = is_trdev;
                lec_init(dev_lec[i]);
        } else {
                priv = dev_lec[i]->priv;
                if (priv->lecd)
                        return -EADDRINUSE;
        }
        lec_arp_init(priv);
	priv->itfnum = i;  /* LANE2 addition */
        priv->lecd = vcc;
865 866
        vcc->dev = &lecatm_dev;
        vcc_insert_socket(vcc->sk);
Linus Torvalds's avatar
Linus Torvalds committed
867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886
        
        vcc->proto_data = dev_lec[i];
	set_bit(ATM_VF_META,&vcc->flags);
	set_bit(ATM_VF_READY,&vcc->flags);

        /* Set default values to these variables */
        priv->maximum_unknown_frame_count = 1;
        priv->max_unknown_frame_time = (1*HZ);
        priv->vcc_timeout_period = (1200*HZ);
        priv->max_retry_count = 1;
        priv->aging_time = (300*HZ);
        priv->forward_delay_time = (15*HZ);
        priv->topology_change = 0;
        priv->arp_response_time = (1*HZ);
        priv->flush_timeout = (4*HZ);
        priv->path_switching_delay = (6*HZ);

        if (dev_lec[i]->flags & IFF_UP) {
                netif_start_queue(dev_lec[i]);
        }
887
        __module_get(THIS_MODULE);
Linus Torvalds's avatar
Linus Torvalds committed
888 889 890
        return i;
}

891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137
#ifdef CONFIG_PROC_FS
static char* lec_arp_get_status_string(unsigned char status)
{
	static char *lec_arp_status_string[] = {
		"ESI_UNKNOWN       ",
		"ESI_ARP_PENDING   ",
		"ESI_VC_PENDING    ",
		"<Undefined>       ",
		"ESI_FLUSH_PENDING ",
		"ESI_FORWARD_DIRECT"
	};

	if (status > ESI_FORWARD_DIRECT)
		status = 3;	/* ESI_UNDEFINED */
	return lec_arp_status_string[status];
}

static void lec_info(struct seq_file *seq, struct lec_arp_table *entry)
{
	int i;

	for (i = 0; i < ETH_ALEN; i++)
		seq_printf(seq, "%2.2x", entry->mac_addr[i] & 0xff);
	seq_printf(seq, " ");
	for (i = 0; i < ATM_ESA_LEN; i++)
		seq_printf(seq, "%2.2x", entry->atm_addr[i] & 0xff);
	seq_printf(seq, " %s %4.4x", lec_arp_get_status_string(entry->status),
		   entry->flags & 0xffff);
	if (entry->vcc)
		seq_printf(seq, "%3d %3d ", entry->vcc->vpi, entry->vcc->vci);
	else
	        seq_printf(seq, "        ");
	if (entry->recv_vcc) {
		seq_printf(seq, "     %3d %3d", entry->recv_vcc->vpi,
			   entry->recv_vcc->vci);
        }
        seq_putc(seq, '\n');
}


struct lec_state {
	unsigned long flags;
	struct lec_priv *locked;
	struct lec_arp_table *entry;
	struct net_device *dev;
	int itf;
	int arp_table;
	int misc_table;
};

static void *lec_tbl_walk(struct lec_state *state, struct lec_arp_table *tbl,
			  loff_t *l)
{
	struct lec_arp_table *e = state->entry;

	if (!e)
		e = tbl;
	if (e == (void *)1) {
		e = tbl;
		--*l;
	}
	for (; e; e = e->next) {
		if (--*l < 0)
			break;
	}
	state->entry = e;
	return (*l < 0) ? state : NULL;
}

static void *lec_arp_walk(struct lec_state *state, loff_t *l,
			      struct lec_priv *priv)
{
	void *v = NULL;
	int p;

	for (p = state->arp_table; p < LEC_ARP_TABLE_SIZE; p++) {
		v = lec_tbl_walk(state, priv->lec_arp_tables[p], l);
		if (v)
			break;
	}
	state->arp_table = p;
	return v;
}

static void *lec_misc_walk(struct lec_state *state, loff_t *l,
			   struct lec_priv *priv)
{
	struct lec_arp_table *lec_misc_tables[] = {
		priv->lec_arp_empty_ones,
		priv->lec_no_forward,
		priv->mcast_fwds
	};
	void *v = NULL;
	int q;

	for (q = state->misc_table; q < ARRAY_SIZE(lec_misc_tables); q++) {
		v = lec_tbl_walk(state, lec_misc_tables[q], l);
		if (v)
			break;
	}
	state->misc_table = q;
	return v;
}

static void *lec_priv_walk(struct lec_state *state, loff_t *l,
			   struct lec_priv *priv)
{
	if (!state->locked) {
		state->locked = priv;
		spin_lock_irqsave(&priv->lec_arp_lock, state->flags);
	}
	if (!lec_arp_walk(state, l, priv) &&
	    !lec_misc_walk(state, l, priv)) {
		spin_unlock_irqrestore(&priv->lec_arp_lock, state->flags);
		state->locked = NULL;
		/* Partial state reset for the next time we get called */
		state->arp_table = state->misc_table = 0;
	}
	return state->locked;
}

static void *lec_itf_walk(struct lec_state *state, loff_t *l)
{
	struct net_device *dev;
	void *v;

	dev = state->dev ? state->dev : dev_lec[state->itf];
	v = (dev && dev->priv) ? lec_priv_walk(state, l, dev->priv) : NULL;
	if (!v && dev) {
		dev_put(dev);
		/* Partial state reset for the next time we get called */
		dev = NULL;
	}
	state->dev = dev;
	return v;
}

static void *lec_get_idx(struct lec_state *state, loff_t l)
{
	void *v = NULL;

	for (; state->itf < MAX_LEC_ITF; state->itf++) {
		v = lec_itf_walk(state, &l);
		if (v)
			break;
	}
	return v; 
}

static void *lec_seq_start(struct seq_file *seq, loff_t *pos)
{
	struct lec_state *state = seq->private;

	state->itf = 0;
	state->dev = NULL;
	state->locked = NULL;
	state->arp_table = 0;
	state->misc_table = 0;
	state->entry = (void *)1;

	return *pos ? lec_get_idx(state, *pos) : (void*)1;
}

static void lec_seq_stop(struct seq_file *seq, void *v)
{
	struct lec_state *state = seq->private;

	if (state->dev) {
		spin_unlock_irqrestore(&state->locked->lec_arp_lock,
				       state->flags);
		dev_put(state->dev);
	}
}

static void *lec_seq_next(struct seq_file *seq, void *v, loff_t *pos)
{
	struct lec_state *state = seq->private;

	v = lec_get_idx(state, 1);
	*pos += !!PTR_ERR(v);
	return v;
}

static int lec_seq_show(struct seq_file *seq, void *v)
{
	static char lec_banner[] = "Itf  MAC          ATM destination" 
		"                          Status            Flags "
		"VPI/VCI Recv VPI/VCI\n";

	if (v == (void *)1)
		seq_puts(seq, lec_banner);
	else {
		struct lec_state *state = seq->private;
		struct net_device *dev = state->dev; 

		seq_printf(seq, "%s ", dev->name);
		lec_info(seq, state->entry);
	}
	return 0;
}

static struct seq_operations lec_seq_ops = {
	.start	= lec_seq_start,
	.next	= lec_seq_next,
	.stop	= lec_seq_stop,
	.show	= lec_seq_show,
};

static int lec_seq_open(struct inode *inode, struct file *file)
{
	struct lec_state *state;
	struct seq_file *seq;
	int rc = -EAGAIN;

	state = kmalloc(sizeof(*state), GFP_KERNEL);
	if (!state) {
		rc = -ENOMEM;
		goto out;
	}

	rc = seq_open(file, &lec_seq_ops);
	if (rc)
		goto out_kfree;
	seq = file->private_data;
	seq->private = state;
out:
	return rc;

out_kfree:
	kfree(state);
	goto out;
}

static int lec_seq_release(struct inode *inode, struct file *file)
{
	return seq_release_private(inode, file);
}

static struct file_operations lec_seq_fops = {
	.owner		= THIS_MODULE,
	.open		= lec_seq_open,
	.read		= seq_read,
	.llseek		= seq_lseek,
	.release	= lec_seq_release,
};
#endif

1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175
static int lane_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
{
	struct atm_vcc *vcc = ATM_SD(sock);
	int err = 0;
	
	switch (cmd) {
		case ATMLEC_CTRL: 
		case ATMLEC_MCAST:
		case ATMLEC_DATA:
			if (!capable(CAP_NET_ADMIN))
				return -EPERM;
			break;
		default:
			return -ENOIOCTLCMD;
	}

	switch (cmd) {
		case ATMLEC_CTRL:
			err = lecd_attach(vcc, (int) arg);
			if (err >= 0)
				sock->state = SS_CONNECTED;
			break;
		case ATMLEC_MCAST:
			err = lec_mcast_attach(vcc, (int) arg);
			break;
		case ATMLEC_DATA:
			err = lec_vcc_attach(vcc, (void *) arg);
			break;
	}

	return err;
}

static struct atm_ioctl lane_ioctl_ops = {
	.owner  = THIS_MODULE,
	.ioctl  = lane_ioctl,
};

Linus Torvalds's avatar
Linus Torvalds committed
1176 1177
static int __init lane_module_init(void)
{
1178 1179 1180 1181 1182 1183 1184 1185
#ifdef CONFIG_PROC_FS
	struct proc_dir_entry *p;

	p = create_proc_entry("lec", S_IRUGO, atm_proc_root);
	if (p)
		p->proc_fops = &lec_seq_fops;
#endif

1186
	register_atm_ioctl(&lane_ioctl_ops);
1187
        printk("lec.c: " __DATE__ " " __TIME__ " initialized\n");
Linus Torvalds's avatar
Linus Torvalds committed
1188 1189 1190 1191 1192 1193 1194 1195
        return 0;
}

static void __exit lane_module_cleanup(void)
{
        int i;
        struct lec_priv *priv;

1196 1197
	remove_proc_entry("lec", atm_proc_root);

1198
	deregister_atm_ioctl(&lane_ioctl_ops);
Linus Torvalds's avatar
Linus Torvalds committed
1199 1200 1201 1202

        for (i = 0; i < MAX_LEC_ITF; i++) {
                if (dev_lec[i] != NULL) {
                        priv = (struct lec_priv *)dev_lec[i]->priv;
1203
			unregister_netdev(dev_lec[i]);
1204
                        free_netdev(dev_lec[i]);
Linus Torvalds's avatar
Linus Torvalds committed
1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370
                        dev_lec[i] = NULL;
                }
        }

        return;                                    
}

module_init(lane_module_init);
module_exit(lane_module_cleanup);

/*
 * LANE2: 3.1.3, LE_RESOLVE.request
 * Non force allocates memory and fills in *tlvs, fills in *sizeoftlvs.
 * If sizeoftlvs == NULL the default TLVs associated with with this
 * lec will be used.
 * If dst_mac == NULL, targetless LE_ARP will be sent
 */
static int lane2_resolve(struct net_device *dev, u8 *dst_mac, int force,
    u8 **tlvs, u32 *sizeoftlvs)
{
        struct lec_priv *priv = (struct lec_priv *)dev->priv;
        struct lec_arp_table *table;
        struct sk_buff *skb;
        int retval;

        if (force == 0) {
                table = lec_arp_find(priv, dst_mac);
                if(table == NULL)
                        return -1;
                
                *tlvs = kmalloc(table->sizeoftlvs, GFP_KERNEL);
                if (*tlvs == NULL)
                        return -1;
                
                memcpy(*tlvs, table->tlvs, table->sizeoftlvs);
                *sizeoftlvs = table->sizeoftlvs;
                
                return 0;
        }

	if (sizeoftlvs == NULL)
		retval = send_to_lecd(priv, l_arp_xmt, dst_mac, NULL, NULL);
		
	else {
		skb = alloc_skb(*sizeoftlvs, GFP_ATOMIC);
		if (skb == NULL)
			return -1;
		skb->len = *sizeoftlvs;
		memcpy(skb->data, *tlvs, *sizeoftlvs);
		retval = send_to_lecd(priv, l_arp_xmt, dst_mac, NULL, skb);
	}
        return retval;
}        


/*
 * LANE2: 3.1.4, LE_ASSOCIATE.request
 * Associate the *tlvs with the *lan_dst address.
 * Will overwrite any previous association
 * Returns 1 for success, 0 for failure (out of memory)
 *
 */
static int lane2_associate_req (struct net_device *dev, u8 *lan_dst,
                         u8 *tlvs, u32 sizeoftlvs)
{
        int retval;
        struct sk_buff *skb;
        struct lec_priv *priv = (struct lec_priv*)dev->priv;

        if ( memcmp(lan_dst, dev->dev_addr, ETH_ALEN) != 0 )
                return (0);       /* not our mac address */

        kfree(priv->tlvs); /* NULL if there was no previous association */

        priv->tlvs = kmalloc(sizeoftlvs, GFP_KERNEL);
        if (priv->tlvs == NULL)
                return (0);
        priv->sizeoftlvs = sizeoftlvs;
        memcpy(priv->tlvs, tlvs, sizeoftlvs);

        skb = alloc_skb(sizeoftlvs, GFP_ATOMIC);
        if (skb == NULL)
                return 0;
        skb->len = sizeoftlvs;
        memcpy(skb->data, tlvs, sizeoftlvs);
        retval = send_to_lecd(priv, l_associate_req, NULL, NULL, skb);
        if (retval != 0)
                printk("lec.c: lane2_associate_req() failed\n");
        /* If the previous association has changed we must
         * somehow notify other LANE entities about the change
         */
        return (1);
}

/*
 * LANE2: 3.1.5, LE_ASSOCIATE.indication
 *
 */
static void lane2_associate_ind (struct net_device *dev, u8 *mac_addr,
    u8 *tlvs, u32 sizeoftlvs)
{
#if 0
        int i = 0;
#endif
	struct lec_priv *priv = (struct lec_priv *)dev->priv;
#if 0 /* Why have the TLVs in LE_ARP entries since we do not use them? When you
         uncomment this code, make sure the TLVs get freed when entry is killed */
        struct lec_arp_table *entry = lec_arp_find(priv, mac_addr);

        if (entry == NULL)
                return;     /* should not happen */

        kfree(entry->tlvs);

        entry->tlvs = kmalloc(sizeoftlvs, GFP_KERNEL);
        if (entry->tlvs == NULL)
                return;

        entry->sizeoftlvs = sizeoftlvs;
        memcpy(entry->tlvs, tlvs, sizeoftlvs);
#endif
#if 0
        printk("lec.c: lane2_associate_ind()\n");
        printk("dump of tlvs, sizeoftlvs=%d\n", sizeoftlvs);
        while (i < sizeoftlvs)
                printk("%02x ", tlvs[i++]);
        
        printk("\n");
#endif

        /* tell MPOA about the TLVs we saw */
        if (priv->lane2_ops && priv->lane2_ops->associate_indicator) {
                priv->lane2_ops->associate_indicator(dev, mac_addr,
                                                     tlvs, sizeoftlvs);
        }
        return;
}

/*
 * Here starts what used to lec_arpc.c
 *
 * lec_arpc.c was added here when making
 * lane client modular. October 1997
 *
 */

#include <linux/types.h>
#include <linux/sched.h>
#include <linux/timer.h>
#include <asm/param.h>
#include <asm/atomic.h>
#include <linux/inetdevice.h>
#include <net/route.h>


#if 0
#define DPRINTK(format,args...)
/*
#define DPRINTK printk
*/
#endif
#define DEBUG_ARP_TABLE 0

#define LEC_ARP_REFRESH_INTERVAL (3*HZ)

static void lec_arp_check_expire(unsigned long data);
1371
static void lec_arp_expire_arp(unsigned long data);
Linus Torvalds's avatar
Linus Torvalds committed
1372 1373 1374 1375 1376 1377 1378 1379 1380
void dump_arp_table(struct lec_priv *priv);

/* 
 * Arp table funcs
 */

#define HASH(ch) (ch & (LEC_ARP_TABLE_SIZE -1))

static __inline__ void 
Chas Williams's avatar
Chas Williams committed
1381
lec_arp_get(struct lec_priv *priv)
Linus Torvalds's avatar
Linus Torvalds committed
1382
{
Chas Williams's avatar
Chas Williams committed
1383
        atomic_inc(&priv->lec_arp_users);
Linus Torvalds's avatar
Linus Torvalds committed
1384 1385 1386
}

static __inline__ void 
Chas Williams's avatar
Chas Williams committed
1387
lec_arp_put(struct lec_priv *priv)
Linus Torvalds's avatar
Linus Torvalds committed
1388
{
Chas Williams's avatar
Chas Williams committed
1389
        atomic_dec(&priv->lec_arp_users);
Linus Torvalds's avatar
Linus Torvalds committed
1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402
}

/*
 * Initialization of arp-cache
 */
void 
lec_arp_init(struct lec_priv *priv)
{
        unsigned short i;

        for (i=0;i<LEC_ARP_TABLE_SIZE;i++) {
                priv->lec_arp_tables[i] = NULL;
        }        
1403
	spin_lock_init(&priv->lec_arp_lock);
Linus Torvalds's avatar
Linus Torvalds committed
1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414
        init_timer(&priv->lec_arp_timer);
        priv->lec_arp_timer.expires = jiffies+LEC_ARP_REFRESH_INTERVAL;
        priv->lec_arp_timer.data = (unsigned long)priv;
        priv->lec_arp_timer.function = lec_arp_check_expire;
        add_timer(&priv->lec_arp_timer);
}

void
lec_arp_clear_vccs(struct lec_arp_table *entry)
{
        if (entry->vcc) {
1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426
		struct atm_vcc *vcc = entry->vcc;
		struct lec_vcc_priv *vpriv = LEC_VCC_PRIV(vcc);
		struct net_device *dev = (struct net_device*) vcc->proto_data;

                vcc->pop = vpriv->old_pop;
		if (vpriv->xoff)
			netif_wake_queue(dev);
		kfree(vpriv);
		LEC_VCC_PRIV(vcc) = NULL;
                vcc->push = entry->old_push;
		vcc_release_async(vcc, -EPIPE);
                vcc = NULL;
Linus Torvalds's avatar
Linus Torvalds committed
1427 1428 1429
        }
        if (entry->recv_vcc) {
                entry->recv_vcc->push = entry->old_recv_push;
1430
		vcc_release_async(entry->recv_vcc, -EPIPE);
Linus Torvalds's avatar
Linus Torvalds committed
1431 1432 1433 1434 1435 1436 1437 1438
                entry->recv_vcc = NULL;
        }        
}

/*
 * Insert entry to lec_arp_table
 * LANE2: Add to the end of the list to satisfy 8.1.13
 */
1439 1440
static inline void 
lec_arp_add(struct lec_priv *priv, struct lec_arp_table *to_add)
Linus Torvalds's avatar
Linus Torvalds committed
1441 1442
{
        unsigned long flags;
Chas Williams's avatar
Chas Williams committed
1443
        unsigned short place;
Linus Torvalds's avatar
Linus Torvalds committed
1444 1445
        struct lec_arp_table *tmp;

1446
        spin_lock_irqsave(&priv->lec_arp_lock, flags);
Linus Torvalds's avatar
Linus Torvalds committed
1447

Chas Williams's avatar
Chas Williams committed
1448
        place = HASH(to_add->mac_addr[ETH_ALEN-1]);
1449
        tmp = priv->lec_arp_tables[place];
Chas Williams's avatar
Chas Williams committed
1450
        to_add->next = NULL;
Linus Torvalds's avatar
Linus Torvalds committed
1451
        if (tmp == NULL)
1452
                priv->lec_arp_tables[place] = to_add;
Linus Torvalds's avatar
Linus Torvalds committed
1453 1454 1455 1456
  
        else {  /* add to the end */
                while (tmp->next)
                        tmp = tmp->next;
Chas Williams's avatar
Chas Williams committed
1457
                tmp->next = to_add;
Linus Torvalds's avatar
Linus Torvalds committed
1458 1459
        }

1460
        spin_unlock_irqrestore(&priv->lec_arp_lock, flags);
Chas Williams's avatar
Chas Williams committed
1461

Linus Torvalds's avatar
Linus Torvalds committed
1462
        DPRINTK("LEC_ARP: Added entry:%2.2x %2.2x %2.2x %2.2x %2.2x %2.2x\n",
Chas Williams's avatar
Chas Williams committed
1463 1464 1465
                0xff&to_add->mac_addr[0], 0xff&to_add->mac_addr[1],
                0xff&to_add->mac_addr[2], 0xff&to_add->mac_addr[3],
                0xff&to_add->mac_addr[4], 0xff&to_add->mac_addr[5]);
Linus Torvalds's avatar
Linus Torvalds committed
1466 1467 1468 1469 1470
}

/*
 * Remove entry from lec_arp_table
 */
1471 1472
static inline int 
lec_arp_remove(struct lec_priv *priv,
Linus Torvalds's avatar
Linus Torvalds committed
1473 1474
               struct lec_arp_table *to_remove)
{
Chas Williams's avatar
Chas Williams committed
1475
        unsigned long flags;
Linus Torvalds's avatar
Linus Torvalds committed
1476 1477 1478 1479
        unsigned short place;
        struct lec_arp_table *tmp;
        int remove_vcc=1;

1480
        spin_lock_irqsave(&priv->lec_arp_lock, flags);
Linus Torvalds's avatar
Linus Torvalds committed
1481 1482

        if (!to_remove) {
1483
                spin_unlock_irqrestore(&priv->lec_arp_lock, flags);
Linus Torvalds's avatar
Linus Torvalds committed
1484 1485 1486
                return -1;
        }
        place = HASH(to_remove->mac_addr[ETH_ALEN-1]);
1487
        tmp = priv->lec_arp_tables[place];
Linus Torvalds's avatar
Linus Torvalds committed
1488
        if (tmp == to_remove) {
1489
                priv->lec_arp_tables[place] = tmp->next;
Linus Torvalds's avatar
Linus Torvalds committed
1490 1491 1492 1493 1494
        } else {
                while(tmp && tmp->next != to_remove) {
                        tmp = tmp->next;
                }
                if (!tmp) {/* Entry was not found */
1495
                        spin_unlock_irqrestore(&priv->lec_arp_lock, flags);
Linus Torvalds's avatar
Linus Torvalds committed
1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508
                        return -1;
                }
        }
        tmp->next = to_remove->next;
        del_timer(&to_remove->timer);
  
        /* If this is the only MAC connected to this VCC, also tear down
           the VCC */
        if (to_remove->status >= ESI_FLUSH_PENDING) {
                /*
                 * ESI_FLUSH_PENDING, ESI_FORWARD_DIRECT
                 */
                for(place=0;place<LEC_ARP_TABLE_SIZE;place++) {
1509
                        for(tmp = priv->lec_arp_tables[place]; tmp != NULL; tmp = tmp->next) {
Linus Torvalds's avatar
Linus Torvalds committed
1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520
                                if (memcmp(tmp->atm_addr, to_remove->atm_addr,
                                           ATM_ESA_LEN)==0) {
                                        remove_vcc=0;
                                        break;
                                }
                        }
                }
                if (remove_vcc)
                        lec_arp_clear_vccs(to_remove);
        }
        skb_queue_purge(&to_remove->tx_wait); /* FIXME: good place for this? */
Chas Williams's avatar
Chas Williams committed
1521

1522
        spin_unlock_irqrestore(&priv->lec_arp_lock, flags);
Chas Williams's avatar
Chas Williams committed
1523

Linus Torvalds's avatar
Linus Torvalds committed
1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709
        DPRINTK("LEC_ARP: Removed entry:%2.2x %2.2x %2.2x %2.2x %2.2x %2.2x\n",
                0xff&to_remove->mac_addr[0], 0xff&to_remove->mac_addr[1],
                0xff&to_remove->mac_addr[2], 0xff&to_remove->mac_addr[3],
                0xff&to_remove->mac_addr[4], 0xff&to_remove->mac_addr[5]);
        return 0;
}

#if DEBUG_ARP_TABLE
static char*
get_status_string(unsigned char st)
{
        switch(st) {
        case ESI_UNKNOWN:
                return "ESI_UNKNOWN";
        case ESI_ARP_PENDING:
                return "ESI_ARP_PENDING";
        case ESI_VC_PENDING:
                return "ESI_VC_PENDING";
        case ESI_FLUSH_PENDING:
                return "ESI_FLUSH_PENDING";
        case ESI_FORWARD_DIRECT:
                return "ESI_FORWARD_DIRECT";
        default:
                return "<UNKNOWN>";
        }
}
#endif

void
dump_arp_table(struct lec_priv *priv)
{
#if DEBUG_ARP_TABLE
        int i,j, offset;
        struct lec_arp_table *rulla;
        char buf[1024];
        struct lec_arp_table **lec_arp_tables =
                (struct lec_arp_table **)priv->lec_arp_tables;
        struct lec_arp_table *lec_arp_empty_ones =
                (struct lec_arp_table *)priv->lec_arp_empty_ones;
        struct lec_arp_table *lec_no_forward =
                (struct lec_arp_table *)priv->lec_no_forward;
        struct lec_arp_table *mcast_fwds = priv->mcast_fwds;


        printk("Dump %p:\n",priv);
        for (i=0;i<LEC_ARP_TABLE_SIZE;i++) {
                rulla = lec_arp_tables[i];
                offset = 0;
                offset += sprintf(buf,"%d: %p\n",i, rulla);
                while (rulla) {
                        offset += sprintf(buf+offset,"Mac:");
                        for(j=0;j<ETH_ALEN;j++) {
                                offset+=sprintf(buf+offset,
                                                "%2.2x ",
                                                rulla->mac_addr[j]&0xff);
                        }
                        offset +=sprintf(buf+offset,"Atm:");
                        for(j=0;j<ATM_ESA_LEN;j++) {
                                offset+=sprintf(buf+offset,
                                                "%2.2x ",
                                                rulla->atm_addr[j]&0xff);
                        }      
                        offset+=sprintf(buf+offset,
                                        "Vcc vpi:%d vci:%d, Recv_vcc vpi:%d vci:%d Last_used:%lx, Timestamp:%lx, No_tries:%d ",
                                        rulla->vcc?rulla->vcc->vpi:0, 
                                        rulla->vcc?rulla->vcc->vci:0,
                                        rulla->recv_vcc?rulla->recv_vcc->vpi:0,
                                        rulla->recv_vcc?rulla->recv_vcc->vci:0,
                                        rulla->last_used,
                                        rulla->timestamp, rulla->no_tries);
                        offset+=sprintf(buf+offset,
                                        "Flags:%x, Packets_flooded:%x, Status: %s ",
                                        rulla->flags, rulla->packets_flooded, 
                                        get_status_string(rulla->status));
                        offset+=sprintf(buf+offset,"->%p\n",rulla->next);
                        rulla = rulla->next;
                }
                printk("%s",buf);
        }
        rulla = lec_no_forward;
        if (rulla)
                printk("No forward\n");  
        while(rulla) {
                offset=0;
                offset += sprintf(buf+offset,"Mac:");
                for(j=0;j<ETH_ALEN;j++) {
                        offset+=sprintf(buf+offset,"%2.2x ",
                                        rulla->mac_addr[j]&0xff);
                }
                offset +=sprintf(buf+offset,"Atm:");
                for(j=0;j<ATM_ESA_LEN;j++) {
                        offset+=sprintf(buf+offset,"%2.2x ",
                                        rulla->atm_addr[j]&0xff);
                }      
                offset+=sprintf(buf+offset,
                                "Vcc vpi:%d vci:%d, Recv_vcc vpi:%d vci:%d Last_used:%lx, Timestamp:%lx, No_tries:%d ",
                                rulla->vcc?rulla->vcc->vpi:0, 
                                rulla->vcc?rulla->vcc->vci:0, 
                                rulla->recv_vcc?rulla->recv_vcc->vpi:0,
                                rulla->recv_vcc?rulla->recv_vcc->vci:0,
                                rulla->last_used, 
                                rulla->timestamp, rulla->no_tries);
                offset+=sprintf(buf+offset,
                                "Flags:%x, Packets_flooded:%x, Status: %s ",
                                rulla->flags, rulla->packets_flooded, 
                                get_status_string(rulla->status));
                offset+=sprintf(buf+offset,"->%lx\n",(long)rulla->next);
                rulla = rulla->next;
                printk("%s",buf);
        }
        rulla = lec_arp_empty_ones;
        if (rulla)
                printk("Empty ones\n");  
        while(rulla) {
                offset=0;
                offset += sprintf(buf+offset,"Mac:");
                for(j=0;j<ETH_ALEN;j++) {
                        offset+=sprintf(buf+offset,"%2.2x ",
                                        rulla->mac_addr[j]&0xff);
                }
                offset +=sprintf(buf+offset,"Atm:");
                for(j=0;j<ATM_ESA_LEN;j++) {
                        offset+=sprintf(buf+offset,"%2.2x ",
                                        rulla->atm_addr[j]&0xff);
                }      
                offset+=sprintf(buf+offset,
                                "Vcc vpi:%d vci:%d, Recv_vcc vpi:%d vci:%d Last_used:%lx, Timestamp:%lx, No_tries:%d ",
                                rulla->vcc?rulla->vcc->vpi:0, 
                                rulla->vcc?rulla->vcc->vci:0, 
                                rulla->recv_vcc?rulla->recv_vcc->vpi:0,
                                rulla->recv_vcc?rulla->recv_vcc->vci:0,
                                rulla->last_used, 
                                rulla->timestamp, rulla->no_tries);
                offset+=sprintf(buf+offset,
                                "Flags:%x, Packets_flooded:%x, Status: %s ",
                                rulla->flags, rulla->packets_flooded, 
                                get_status_string(rulla->status));
                offset+=sprintf(buf+offset,"->%lx\n",(long)rulla->next);
                rulla = rulla->next;
                printk("%s",buf);
        }

        rulla = mcast_fwds;
        if (rulla)
                printk("Multicast Forward VCCs\n");  
        while(rulla) {
                offset=0;
                offset += sprintf(buf+offset,"Mac:");
                for(j=0;j<ETH_ALEN;j++) {
                        offset+=sprintf(buf+offset,"%2.2x ",
                                        rulla->mac_addr[j]&0xff);
                }
                offset +=sprintf(buf+offset,"Atm:");
                for(j=0;j<ATM_ESA_LEN;j++) {
                        offset+=sprintf(buf+offset,"%2.2x ",
                                        rulla->atm_addr[j]&0xff);
                }      
                offset+=sprintf(buf+offset,
                                "Vcc vpi:%d vci:%d, Recv_vcc vpi:%d vci:%d Last_used:%lx, Timestamp:%lx, No_tries:%d ",
                                rulla->vcc?rulla->vcc->vpi:0, 
                                rulla->vcc?rulla->vcc->vci:0, 
                                rulla->recv_vcc?rulla->recv_vcc->vpi:0,
                                rulla->recv_vcc?rulla->recv_vcc->vci:0,
                                rulla->last_used, 
                                rulla->timestamp, rulla->no_tries);
                offset+=sprintf(buf+offset,
                                "Flags:%x, Packets_flooded:%x, Status: %s ",
                                rulla->flags, rulla->packets_flooded, 
                                get_status_string(rulla->status));
                offset+=sprintf(buf+offset,"->%lx\n",(long)rulla->next);
                rulla = rulla->next;
                printk("%s",buf);
        }

#endif
}

/*
 * Destruction of arp-cache
 */
void
lec_arp_destroy(struct lec_priv *priv)
{
        struct lec_arp_table *entry, *next;
        int i;

1710
        del_timer_sync(&priv->lec_arp_timer);
Linus Torvalds's avatar
Linus Torvalds committed
1711 1712 1713 1714 1715 1716 1717
        
        /*
         * Remove all entries
         */
        for (i=0;i<LEC_ARP_TABLE_SIZE;i++) {
                for(entry =priv->lec_arp_tables[i];entry != NULL; entry=next) {
                        next = entry->next;
1718
                        lec_arp_remove(priv, entry);
Linus Torvalds's avatar
Linus Torvalds committed
1719 1720 1721 1722 1723 1724
                        kfree(entry);
                }
        }
        entry = priv->lec_arp_empty_ones;
        while(entry) {
                next = entry->next;
1725
                del_timer_sync(&entry->timer);
Linus Torvalds's avatar
Linus Torvalds committed
1726 1727 1728 1729 1730 1731 1732 1733
                lec_arp_clear_vccs(entry);
                kfree(entry);
                entry = next;
        }
        priv->lec_arp_empty_ones = NULL;
        entry = priv->lec_no_forward;
        while(entry) {
                next = entry->next;
1734
                del_timer_sync(&entry->timer);
Linus Torvalds's avatar
Linus Torvalds committed
1735 1736 1737 1738 1739 1740 1741 1742
                lec_arp_clear_vccs(entry);
                kfree(entry);
                entry = next;
        }
        priv->lec_no_forward = NULL;
        entry = priv->mcast_fwds;
        while(entry) {
                next = entry->next;
1743
                /* No timer, LANEv2 7.1.20 and 2.3.5.3 */
Linus Torvalds's avatar
Linus Torvalds committed
1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757
                lec_arp_clear_vccs(entry);
                kfree(entry);
                entry = next;
        }
        priv->mcast_fwds = NULL;
        priv->mcast_vcc = NULL;
        memset(priv->lec_arp_tables, 0, 
               sizeof(struct lec_arp_table*)*LEC_ARP_TABLE_SIZE);
}


/* 
 * Find entry by mac_address
 */
1758
static inline struct lec_arp_table*
Linus Torvalds's avatar
Linus Torvalds committed
1759 1760 1761 1762 1763 1764 1765 1766 1767
lec_arp_find(struct lec_priv *priv,
             unsigned char *mac_addr)
{
        unsigned short place;
        struct lec_arp_table *to_return;

        DPRINTK("LEC_ARP: lec_arp_find :%2.2x %2.2x %2.2x %2.2x %2.2x %2.2x\n",
                mac_addr[0]&0xff, mac_addr[1]&0xff, mac_addr[2]&0xff, 
                mac_addr[3]&0xff, mac_addr[4]&0xff, mac_addr[5]&0xff);
Chas Williams's avatar
Chas Williams committed
1768
        lec_arp_get(priv);
Linus Torvalds's avatar
Linus Torvalds committed
1769 1770 1771 1772 1773
        place = HASH(mac_addr[ETH_ALEN-1]);
  
        to_return = priv->lec_arp_tables[place];
        while(to_return) {
                if (memcmp(mac_addr, to_return->mac_addr, ETH_ALEN) == 0) {
Chas Williams's avatar
Chas Williams committed
1774
                        lec_arp_put(priv);
Linus Torvalds's avatar
Linus Torvalds committed
1775 1776 1777 1778
                        return to_return;
                }
                to_return = to_return->next;
        }
Chas Williams's avatar
Chas Williams committed
1779
        lec_arp_put(priv);
Linus Torvalds's avatar
Linus Torvalds committed
1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825
        return NULL;
}

static struct lec_arp_table*
make_entry(struct lec_priv *priv, unsigned char *mac_addr)
{
        struct lec_arp_table *to_return;

        to_return=(struct lec_arp_table *)kmalloc(sizeof(struct lec_arp_table),
                                                  GFP_ATOMIC);
        if (!to_return) {
                printk("LEC: Arp entry kmalloc failed\n");
                return NULL;
        }
        memset(to_return,0,sizeof(struct lec_arp_table));
        memcpy(to_return->mac_addr, mac_addr, ETH_ALEN);
        init_timer(&to_return->timer);
        to_return->timer.function = lec_arp_expire_arp;
        to_return->timer.data = (unsigned long)to_return;
        to_return->last_used = jiffies;
        to_return->priv = priv;
        skb_queue_head_init(&to_return->tx_wait);
        return to_return;
}

/*
 *
 * Arp sent timer expired
 *
 */
static void
lec_arp_expire_arp(unsigned long data)
{
        struct lec_arp_table *entry;

        entry = (struct lec_arp_table *)data;

        DPRINTK("lec_arp_expire_arp\n");
        if (entry->status == ESI_ARP_PENDING) {
                if (entry->no_tries <= entry->priv->max_retry_count) {
                        if (entry->is_rdesc)
                                send_to_lecd(entry->priv, l_rdesc_arp_xmt, entry->mac_addr, NULL, NULL);
                        else
                                send_to_lecd(entry->priv, l_arp_xmt, entry->mac_addr, NULL, NULL);
                        entry->no_tries++;
                }
1826
                mod_timer(&entry->timer, jiffies + (1*HZ));
Linus Torvalds's avatar
Linus Torvalds committed
1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898
        }
}

/*
 *
 * Unknown/unused vcc expire, remove associated entry
 *
 */
static void
lec_arp_expire_vcc(unsigned long data)
{
        struct lec_arp_table *to_remove = (struct lec_arp_table*)data;
        struct lec_priv *priv = (struct lec_priv *)to_remove->priv;
        struct lec_arp_table *entry = NULL;

        del_timer(&to_remove->timer);

        DPRINTK("LEC_ARP %p %p: lec_arp_expire_vcc vpi:%d vci:%d\n",
                to_remove, priv, 
                to_remove->vcc?to_remove->recv_vcc->vpi:0,
                to_remove->vcc?to_remove->recv_vcc->vci:0);
        DPRINTK("eo:%p nf:%p\n",priv->lec_arp_empty_ones,priv->lec_no_forward);
        if (to_remove == priv->lec_arp_empty_ones)
                priv->lec_arp_empty_ones = to_remove->next;
        else {
                entry = priv->lec_arp_empty_ones;
                while (entry && entry->next != to_remove)
                        entry = entry->next;
                if (entry)
                        entry->next = to_remove->next;
        }
        if (!entry) {
                if (to_remove == priv->lec_no_forward) {
                        priv->lec_no_forward = to_remove->next;
                } else {
                        entry = priv->lec_no_forward;
                        while (entry && entry->next != to_remove)
                                entry = entry->next;
                        if (entry)
                                entry->next = to_remove->next;
                }
	}
        lec_arp_clear_vccs(to_remove);
        kfree(to_remove);
}

/*
 * Expire entries.
 * 1. Re-set timer
 * 2. For each entry, delete entries that have aged past the age limit.
 * 3. For each entry, depending on the status of the entry, perform
 *    the following maintenance.
 *    a. If status is ESI_VC_PENDING or ESI_ARP_PENDING then if the
 *       tick_count is above the max_unknown_frame_time, clear
 *       the tick_count to zero and clear the packets_flooded counter
 *       to zero. This supports the packet rate limit per address
 *       while flooding unknowns.
 *    b. If the status is ESI_FLUSH_PENDING and the tick_count is greater
 *       than or equal to the path_switching_delay, change the status
 *       to ESI_FORWARD_DIRECT. This causes the flush period to end
 *       regardless of the progress of the flush protocol.
 */
static void
lec_arp_check_expire(unsigned long data)
{
        struct lec_priv *priv = (struct lec_priv *)data;
        struct lec_arp_table *entry, *next;
        unsigned long now;
        unsigned long time_to_check;
        int i;

        DPRINTK("lec_arp_check_expire %p,%d\n",priv,
Chas Williams's avatar
Chas Williams committed
1899
                atomic_read(&priv->lec_arp_users));
Linus Torvalds's avatar
Linus Torvalds committed
1900 1901
        DPRINTK("expire: eo:%p nf:%p\n",priv->lec_arp_empty_ones,
                priv->lec_no_forward);
Chas Williams's avatar
Chas Williams committed
1902 1903
        if (!atomic_read(&priv->lec_arp_users)) {
                lec_arp_get(priv);
Linus Torvalds's avatar
Linus Torvalds committed
1904 1905
                now = jiffies;
                for(i=0;i<LEC_ARP_TABLE_SIZE;i++) {
1906
                        for(entry = priv->lec_arp_tables[i]; entry != NULL; ) {
Linus Torvalds's avatar
Linus Torvalds committed
1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921
                                if ((entry->flags) & LEC_REMOTE_FLAG && 
                                    priv->topology_change)
                                        time_to_check=priv->forward_delay_time;
                                else
                                        time_to_check = priv->aging_time;

                                DPRINTK("About to expire: %lx - %lx > %lx\n",
                                        now,entry->last_used, time_to_check);
                                if( time_after(now, entry->last_used+
                                   time_to_check) && 
                                    !(entry->flags & LEC_PERMANENT_FLAG) &&
                                    !(entry->mac_addr[0] & 0x01) ) { /* LANE2: 7.1.20 */
                                        /* Remove entry */
                                        DPRINTK("LEC:Entry timed out\n");
                                        next = entry->next;      
1922
                                        lec_arp_remove(priv, entry);
Linus Torvalds's avatar
Linus Torvalds committed
1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940
                                        kfree(entry);
                                        entry = next;
                                } else {
                                        /* Something else */
                                        if ((entry->status == ESI_VC_PENDING ||
                                             entry->status == ESI_ARP_PENDING) 
                                            && time_after_eq(now,
                                            entry->timestamp +
                                            priv->max_unknown_frame_time)) {
                                                entry->timestamp = jiffies;
                                                entry->packets_flooded = 0;
                                                if (entry->status == ESI_VC_PENDING)
                                                        send_to_lecd(priv, l_svc_setup, entry->mac_addr, entry->atm_addr, NULL);
                                        }
                                        if (entry->status == ESI_FLUSH_PENDING 
                                           &&
                                           time_after_eq(now, entry->timestamp+
                                           priv->path_switching_delay)) {
1941 1942 1943 1944
			                        struct sk_buff *skb;

 				                while ((skb = skb_dequeue(&entry->tx_wait)))
					                lec_send(entry->vcc, skb, entry->priv);
Linus Torvalds's avatar
Linus Torvalds committed
1945 1946 1947 1948 1949 1950 1951 1952
                                                entry->last_used = jiffies;
                                                entry->status = 
                                                        ESI_FORWARD_DIRECT;
                                        }
                                        entry = entry->next;
                                }
                        }
                }
Chas Williams's avatar
Chas Williams committed
1953
                lec_arp_put(priv);
Linus Torvalds's avatar
Linus Torvalds committed
1954
        }
1955 1956

        mod_timer(&priv->lec_arp_timer, jiffies + LEC_ARP_REFRESH_INTERVAL);
Linus Torvalds's avatar
Linus Torvalds committed
1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014
}
/*
 * Try to find vcc where mac_address is attached.
 * 
 */
struct atm_vcc*
lec_arp_resolve(struct lec_priv *priv, unsigned char *mac_to_find, int is_rdesc,
                struct lec_arp_table **ret_entry)
{
        struct lec_arp_table *entry;

        if (mac_to_find[0]&0x01) {
                switch (priv->lane_version) {
                case 1:
                        return priv->mcast_vcc;
                        break;
                case 2:  /* LANE2 wants arp for multicast addresses */
                        if ( memcmp(mac_to_find, bus_mac, ETH_ALEN) == 0)
                                return priv->mcast_vcc;
                        break;
                default:
                        break;
                }
        }

        entry = lec_arp_find(priv, mac_to_find);
  
        if (entry) {
                if (entry->status == ESI_FORWARD_DIRECT) {
                        /* Connection Ok */
                        entry->last_used = jiffies;
                        *ret_entry = entry;
                        return entry->vcc;
                }
                /* Data direct VC not yet set up, check to see if the unknown
                   frame count is greater than the limit. If the limit has
                   not been reached, allow the caller to send packet to
                   BUS. */
                if (entry->status != ESI_FLUSH_PENDING &&
                    entry->packets_flooded<priv->maximum_unknown_frame_count) {
                        entry->packets_flooded++;
                        DPRINTK("LEC_ARP: Flooding..\n");
                        return priv->mcast_vcc;
                }
		/* We got here because entry->status == ESI_FLUSH_PENDING
		 * or BUS flood limit was reached for an entry which is
		 * in ESI_ARP_PENDING or ESI_VC_PENDING state.
		 */
                *ret_entry = entry;
                DPRINTK("lec: entry->status %d entry->vcc %p\n", entry->status, entry->vcc);
                return NULL;
        } else {
                /* No matching entry was found */
                entry = make_entry(priv, mac_to_find);
                DPRINTK("LEC_ARP: Making entry\n");
                if (!entry) {
                        return priv->mcast_vcc;
                }
2015
                lec_arp_add(priv, entry);
Linus Torvalds's avatar
Linus Torvalds committed
2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039
                /* We want arp-request(s) to be sent */
                entry->packets_flooded =1;
                entry->status = ESI_ARP_PENDING;
                entry->no_tries = 1;
                entry->last_used = entry->timestamp = jiffies;
                entry->is_rdesc = is_rdesc;
                if (entry->is_rdesc)
                        send_to_lecd(priv, l_rdesc_arp_xmt, mac_to_find, NULL, NULL);
                else
                        send_to_lecd(priv, l_arp_xmt, mac_to_find, NULL, NULL);
                entry->timer.expires = jiffies + (1*HZ);
                entry->timer.function = lec_arp_expire_arp;
                add_timer(&entry->timer);
                return priv->mcast_vcc;
        }
}

int
lec_addr_delete(struct lec_priv *priv, unsigned char *atm_addr, 
                unsigned long permanent)
{
        struct lec_arp_table *entry, *next;
        int i;

Chas Williams's avatar
Chas Williams committed
2040
        lec_arp_get(priv);
Linus Torvalds's avatar
Linus Torvalds committed
2041 2042 2043 2044 2045 2046 2047
        DPRINTK("lec_addr_delete\n");
        for(i=0;i<LEC_ARP_TABLE_SIZE;i++) {
                for(entry=priv->lec_arp_tables[i];entry != NULL; entry=next) {
                        next = entry->next;
                        if (!memcmp(atm_addr, entry->atm_addr, ATM_ESA_LEN)
                            && (permanent || 
                                !(entry->flags & LEC_PERMANENT_FLAG))) {
2048
                                lec_arp_remove(priv, entry);
Linus Torvalds's avatar
Linus Torvalds committed
2049 2050
                                kfree(entry);
                        }
Chas Williams's avatar
Chas Williams committed
2051
                        lec_arp_put(priv);
Linus Torvalds's avatar
Linus Torvalds committed
2052 2053 2054
                        return 0;
                }
        }
Chas Williams's avatar
Chas Williams committed
2055
        lec_arp_put(priv);
Linus Torvalds's avatar
Linus Torvalds committed
2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079
        return -1;
}

/*
 * Notifies:  Response to arp_request (atm_addr != NULL) 
 */
void
lec_arp_update(struct lec_priv *priv, unsigned char *mac_addr,
               unsigned char *atm_addr, unsigned long remoteflag,
               unsigned int targetless_le_arp)
{
        struct lec_arp_table *entry, *tmp;
        int i;

        DPRINTK("lec:%s", (targetless_le_arp) ? "targetless ": " ");
        DPRINTK("lec_arp_update mac:%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x\n",
                mac_addr[0],mac_addr[1],mac_addr[2],mac_addr[3],
                mac_addr[4],mac_addr[5]);

        entry = lec_arp_find(priv, mac_addr);
        if (entry == NULL && targetless_le_arp)
                return;   /* LANE2: ignore targetless LE_ARPs for which
                           * we have no entry in the cache. 7.1.30
                           */
Chas Williams's avatar
Chas Williams committed
2080
        lec_arp_get(priv);
Linus Torvalds's avatar
Linus Torvalds committed
2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113
        if (priv->lec_arp_empty_ones) {
                entry = priv->lec_arp_empty_ones;
                if (!memcmp(entry->atm_addr, atm_addr, ATM_ESA_LEN)) {
                        priv->lec_arp_empty_ones = entry->next;
                } else {
                        while(entry->next && memcmp(entry->next->atm_addr, 
                                                    atm_addr, ATM_ESA_LEN))
                                entry = entry->next;
                        if (entry->next) {
                                tmp = entry;
                                entry = entry->next;
                                tmp->next = entry->next;
                        } else
                                entry = NULL;
                        
                }
                if (entry) {
                        del_timer(&entry->timer);
                        tmp = lec_arp_find(priv, mac_addr);
                        if (tmp) {
                                del_timer(&tmp->timer);
                                tmp->status = ESI_FORWARD_DIRECT;
                                memcpy(tmp->atm_addr, atm_addr, ATM_ESA_LEN);
                                tmp->vcc = entry->vcc;
                                tmp->old_push = entry->old_push;
                                tmp->last_used = jiffies;
                                del_timer(&entry->timer);
                                kfree(entry);
                                entry=tmp;
                        } else {
                                entry->status = ESI_FORWARD_DIRECT;
                                memcpy(entry->mac_addr, mac_addr, ETH_ALEN);
                                entry->last_used = jiffies;
2114
                                lec_arp_add(priv, entry);
Linus Torvalds's avatar
Linus Torvalds committed
2115 2116 2117 2118 2119
                        }
                        if (remoteflag)
                                entry->flags|=LEC_REMOTE_FLAG;
                        else
                                entry->flags&=~LEC_REMOTE_FLAG;
Chas Williams's avatar
Chas Williams committed
2120
                        lec_arp_put(priv);
Linus Torvalds's avatar
Linus Torvalds committed
2121 2122 2123 2124 2125 2126 2127 2128
                        DPRINTK("After update\n");
                        dump_arp_table(priv);
                        return;
                }
        }
        entry = lec_arp_find(priv, mac_addr);
        if (!entry) {
                entry = make_entry(priv, mac_addr);
Linus Torvalds's avatar
Linus Torvalds committed
2129
                if (!entry) {
Chas Williams's avatar
Chas Williams committed
2130
                        lec_arp_put(priv);
Linus Torvalds's avatar
Linus Torvalds committed
2131 2132
                        return;
                }
Linus Torvalds's avatar
Linus Torvalds committed
2133
                entry->status = ESI_UNKNOWN;
2134
                lec_arp_add(priv, entry);
Linus Torvalds's avatar
Linus Torvalds committed
2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168
                /* Temporary, changes before end of function */
        }
        memcpy(entry->atm_addr, atm_addr, ATM_ESA_LEN);
        del_timer(&entry->timer);
        for(i=0;i<LEC_ARP_TABLE_SIZE;i++) {
                for(tmp=priv->lec_arp_tables[i];tmp;tmp=tmp->next) {
                        if (entry != tmp &&
                            !memcmp(tmp->atm_addr, atm_addr,
                                    ATM_ESA_LEN)) { 
                                /* Vcc to this host exists */
                                if (tmp->status > ESI_VC_PENDING) {
                                        /*
                                         * ESI_FLUSH_PENDING,
                                         * ESI_FORWARD_DIRECT
                                         */
                                        entry->vcc = tmp->vcc;
                                        entry->old_push=tmp->old_push;
                                }
                                entry->status=tmp->status;
                                break;
                        }
                }
        }
        if (remoteflag)
                entry->flags|=LEC_REMOTE_FLAG;
        else
                entry->flags&=~LEC_REMOTE_FLAG;
        if (entry->status == ESI_ARP_PENDING ||
            entry->status == ESI_UNKNOWN) {
                entry->status = ESI_VC_PENDING;
                send_to_lecd(priv, l_svc_setup, entry->mac_addr, atm_addr, NULL);
        }
        DPRINTK("After update2\n");
        dump_arp_table(priv);
Chas Williams's avatar
Chas Williams committed
2169
        lec_arp_put(priv);
Linus Torvalds's avatar
Linus Torvalds committed
2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182
}

/*
 * Notifies: Vcc setup ready 
 */
void
lec_vcc_added(struct lec_priv *priv, struct atmlec_ioc *ioc_data,
              struct atm_vcc *vcc,
              void (*old_push)(struct atm_vcc *vcc, struct sk_buff *skb))
{
        struct lec_arp_table *entry;
        int i, found_entry=0;

Chas Williams's avatar
Chas Williams committed
2183
        lec_arp_get(priv);
Linus Torvalds's avatar
Linus Torvalds committed
2184 2185 2186 2187 2188 2189 2190 2191
        if (ioc_data->receive == 2) {
                /* Vcc for Multicast Forward. No timer, LANEv2 7.1.20 and 2.3.5.3 */

                DPRINTK("LEC_ARP: Attaching mcast forward\n");
#if 0
                entry = lec_arp_find(priv, bus_mac);
                if (!entry) {
                        printk("LEC_ARP: Multicast entry not found!\n");
Chas Williams's avatar
Chas Williams committed
2192
                        lec_arp_put(priv);
Linus Torvalds's avatar
Linus Torvalds committed
2193 2194 2195 2196 2197 2198 2199 2200
                        return;
                }
                memcpy(entry->atm_addr, ioc_data->atm_addr, ATM_ESA_LEN);
                entry->recv_vcc = vcc;
                entry->old_recv_push = old_push;
#endif
                entry = make_entry(priv, bus_mac);
                if (entry == NULL) {
Chas Williams's avatar
Chas Williams committed
2201
                        lec_arp_put(priv);
Linus Torvalds's avatar
Linus Torvalds committed
2202 2203 2204 2205 2206 2207 2208 2209
                        return;
                }
                del_timer(&entry->timer);
                memcpy(entry->atm_addr, ioc_data->atm_addr, ATM_ESA_LEN);
                entry->recv_vcc = vcc;
                entry->old_recv_push = old_push;
                entry->next = priv->mcast_fwds;
                priv->mcast_fwds = entry;
Chas Williams's avatar
Chas Williams committed
2210
                lec_arp_put(priv);
Linus Torvalds's avatar
Linus Torvalds committed
2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226
                return;
        } else if (ioc_data->receive == 1) {
                /* Vcc which we don't want to make default vcc, attach it
                   anyway. */
                DPRINTK("LEC_ARP:Attaching data direct, not default :%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x\n",
                        ioc_data->atm_addr[0],ioc_data->atm_addr[1],
                        ioc_data->atm_addr[2],ioc_data->atm_addr[3],
                        ioc_data->atm_addr[4],ioc_data->atm_addr[5],
                        ioc_data->atm_addr[6],ioc_data->atm_addr[7],
                        ioc_data->atm_addr[8],ioc_data->atm_addr[9],
                        ioc_data->atm_addr[10],ioc_data->atm_addr[11],
                        ioc_data->atm_addr[12],ioc_data->atm_addr[13],
                        ioc_data->atm_addr[14],ioc_data->atm_addr[15],
                        ioc_data->atm_addr[16],ioc_data->atm_addr[17],
                        ioc_data->atm_addr[18],ioc_data->atm_addr[19]);
                entry = make_entry(priv, bus_mac);
Linus Torvalds's avatar
Linus Torvalds committed
2227
                if (entry == NULL) {
Chas Williams's avatar
Chas Williams committed
2228
                        lec_arp_put(priv);
Linus Torvalds's avatar
Linus Torvalds committed
2229 2230
                        return;
                }
Linus Torvalds's avatar
Linus Torvalds committed
2231 2232 2233 2234 2235 2236 2237 2238 2239 2240
                memcpy(entry->atm_addr, ioc_data->atm_addr, ATM_ESA_LEN);
                memset(entry->mac_addr, 0, ETH_ALEN);
                entry->recv_vcc = vcc;
                entry->old_recv_push = old_push;
                entry->status = ESI_UNKNOWN;
                entry->timer.expires = jiffies + priv->vcc_timeout_period;
                entry->timer.function = lec_arp_expire_vcc;
                add_timer(&entry->timer);
                entry->next = priv->lec_no_forward;
                priv->lec_no_forward = entry;
Chas Williams's avatar
Chas Williams committed
2241
                lec_arp_put(priv);
Linus Torvalds's avatar
Linus Torvalds committed
2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299
		dump_arp_table(priv);
                return;
        }
        DPRINTK("LEC_ARP:Attaching data direct, default:%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x\n",
                ioc_data->atm_addr[0],ioc_data->atm_addr[1],
                ioc_data->atm_addr[2],ioc_data->atm_addr[3],
                ioc_data->atm_addr[4],ioc_data->atm_addr[5],
                ioc_data->atm_addr[6],ioc_data->atm_addr[7],
                ioc_data->atm_addr[8],ioc_data->atm_addr[9],
                ioc_data->atm_addr[10],ioc_data->atm_addr[11],
                ioc_data->atm_addr[12],ioc_data->atm_addr[13],
                ioc_data->atm_addr[14],ioc_data->atm_addr[15],
                ioc_data->atm_addr[16],ioc_data->atm_addr[17],
                ioc_data->atm_addr[18],ioc_data->atm_addr[19]);
        for (i=0;i<LEC_ARP_TABLE_SIZE;i++) {
                for (entry = priv->lec_arp_tables[i];entry;entry=entry->next) {
                        if (memcmp(ioc_data->atm_addr, entry->atm_addr, 
                                   ATM_ESA_LEN)==0) {
                                DPRINTK("LEC_ARP: Attaching data direct\n");
                                DPRINTK("Currently -> Vcc: %d, Rvcc:%d\n",
                                        entry->vcc?entry->vcc->vci:0,
                                        entry->recv_vcc?entry->recv_vcc->vci:0);
                                found_entry=1;
                                del_timer(&entry->timer);
                                entry->vcc = vcc;
                                entry->old_push = old_push;
                                if (entry->status == ESI_VC_PENDING) {
                                        if(priv->maximum_unknown_frame_count
                                           ==0)
                                                entry->status = 
                                                        ESI_FORWARD_DIRECT;
                                        else {
                                                entry->timestamp = jiffies;
                                                entry->status = 
                                                        ESI_FLUSH_PENDING;
#if 0
                                                send_to_lecd(priv,l_flush_xmt,
                                                             NULL,
                                                             entry->atm_addr,
                                                             NULL);
#endif
                                        }
                                } else {
                                        /* They were forming a connection
                                           to us, and we to them. Our
                                           ATM address is numerically lower
                                           than theirs, so we make connection
                                           we formed into default VCC (8.1.11).
                                           Connection they made gets torn
                                           down. This might confuse some
                                           clients. Can be changed if
                                           someone reports trouble... */
                                        ;
                                }
                        }
                }
        }
        if (found_entry) {
Chas Williams's avatar
Chas Williams committed
2300
                lec_arp_put(priv);
Linus Torvalds's avatar
Linus Torvalds committed
2301 2302 2303 2304 2305 2306 2307
                DPRINTK("After vcc was added\n");
                dump_arp_table(priv);
                return;
        }
        /* Not found, snatch address from first data packet that arrives from
           this vcc */
        entry = make_entry(priv, bus_mac);
Linus Torvalds's avatar
Linus Torvalds committed
2308
        if (!entry) {
Chas Williams's avatar
Chas Williams committed
2309
                lec_arp_put(priv);
Linus Torvalds's avatar
Linus Torvalds committed
2310 2311
                return;
        }
Linus Torvalds's avatar
Linus Torvalds committed
2312 2313 2314 2315 2316 2317 2318 2319 2320 2321
        entry->vcc = vcc;
        entry->old_push = old_push;
        memcpy(entry->atm_addr, ioc_data->atm_addr, ATM_ESA_LEN);
        memset(entry->mac_addr, 0, ETH_ALEN);
        entry->status = ESI_UNKNOWN;
        entry->next = priv->lec_arp_empty_ones;
        priv->lec_arp_empty_ones = entry;
        entry->timer.expires = jiffies + priv->vcc_timeout_period;
        entry->timer.function = lec_arp_expire_vcc;
        add_timer(&entry->timer);
Chas Williams's avatar
Chas Williams committed
2322
        lec_arp_put(priv);
Linus Torvalds's avatar
Linus Torvalds committed
2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337
        DPRINTK("After vcc was added\n");
	dump_arp_table(priv);
}

void
lec_flush_complete(struct lec_priv *priv, unsigned long tran_id)
{
        struct lec_arp_table *entry;
        int i;
  
        DPRINTK("LEC:lec_flush_complete %lx\n",tran_id);
        for (i=0;i<LEC_ARP_TABLE_SIZE;i++) {
                for (entry=priv->lec_arp_tables[i];entry;entry=entry->next) {
                        if (entry->flush_tran_id == tran_id &&
                            entry->status == ESI_FLUSH_PENDING) {
2338 2339 2340 2341
			        struct sk_buff *skb;

 				while ((skb = skb_dequeue(&entry->tx_wait)))
					lec_send(entry->vcc, skb, entry->priv);
Linus Torvalds's avatar
Linus Torvalds committed
2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370
                                entry->status = ESI_FORWARD_DIRECT;
                                DPRINTK("LEC_ARP: Flushed\n");
                        }
                }
        }
        dump_arp_table(priv);
}

void
lec_set_flush_tran_id(struct lec_priv *priv,
                      unsigned char *atm_addr, unsigned long tran_id)
{
        struct lec_arp_table *entry;
        int i;

        for (i=0;i<LEC_ARP_TABLE_SIZE;i++)
                for(entry=priv->lec_arp_tables[i];entry;entry=entry->next)
                        if (!memcmp(atm_addr, entry->atm_addr, ATM_ESA_LEN)) {
                                entry->flush_tran_id = tran_id;
                                DPRINTK("Set flush transaction id to %lx for %p\n",tran_id,entry);
                        }
}

int 
lec_mcast_make(struct lec_priv *priv, struct atm_vcc *vcc)
{
        unsigned char mac_addr[] = {
                0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
        struct lec_arp_table *to_add;
2371
	struct lec_vcc_priv *vpriv;
Linus Torvalds's avatar
Linus Torvalds committed
2372
  
2373 2374 2375 2376 2377 2378
	if (!(vpriv = kmalloc(sizeof(struct lec_vcc_priv), GFP_KERNEL)))
		return -ENOMEM;
	vpriv->xoff = 0;
	vpriv->old_pop = vcc->pop;
	LEC_VCC_PRIV(vcc) = vpriv;
        vcc->pop = lec_pop;
Chas Williams's avatar
Chas Williams committed
2379
        lec_arp_get(priv);
Linus Torvalds's avatar
Linus Torvalds committed
2380 2381
        to_add = make_entry(priv, mac_addr);
        if (!to_add) {
Chas Williams's avatar
Chas Williams committed
2382
                lec_arp_put(priv);
2383 2384
		vcc->pop = vpriv->old_pop;
		kfree(vpriv);
Linus Torvalds's avatar
Linus Torvalds committed
2385 2386 2387 2388 2389 2390 2391 2392 2393
                return -ENOMEM;
        }
        memcpy(to_add->atm_addr, vcc->remote.sas_addr.prv, ATM_ESA_LEN);
        to_add->status = ESI_FORWARD_DIRECT;
        to_add->flags |= LEC_PERMANENT_FLAG;
        to_add->vcc = vcc;
        to_add->old_push = vcc->push;
        vcc->push = lec_push;
        priv->mcast_vcc = vcc;
2394
        lec_arp_add(priv, to_add);
Chas Williams's avatar
Chas Williams committed
2395
        lec_arp_put(priv);
Linus Torvalds's avatar
Linus Torvalds committed
2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406
        return 0;
}

void
lec_vcc_close(struct lec_priv *priv, struct atm_vcc *vcc)
{
        struct lec_arp_table *entry, *next;
        int i;

        DPRINTK("LEC_ARP: lec_vcc_close vpi:%d vci:%d\n",vcc->vpi,vcc->vci);
        dump_arp_table(priv);
Chas Williams's avatar
Chas Williams committed
2407
        lec_arp_get(priv);
Linus Torvalds's avatar
Linus Torvalds committed
2408 2409 2410 2411
        for(i=0;i<LEC_ARP_TABLE_SIZE;i++) {
                for(entry = priv->lec_arp_tables[i];entry; entry=next) {
                        next = entry->next;
                        if (vcc == entry->vcc) {
2412
                                lec_arp_remove(priv, entry);
Linus Torvalds's avatar
Linus Torvalds committed
2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468
                                kfree(entry);
                                if (priv->mcast_vcc == vcc) {
                                        priv->mcast_vcc = NULL;
                                }
                        }
                }
        }

        entry = priv->lec_arp_empty_ones;
        priv->lec_arp_empty_ones = NULL;
        while (entry != NULL) {
                next = entry->next;
                if (entry->vcc == vcc) { /* leave it out from the list */
                        lec_arp_clear_vccs(entry);
                        del_timer(&entry->timer);
                        kfree(entry);
                }
                else {              /* put it back to the list */
                        entry->next = priv->lec_arp_empty_ones;
                        priv->lec_arp_empty_ones = entry;
                }
                entry = next;
        }
        
        entry = priv->lec_no_forward;
        priv->lec_no_forward = NULL;
        while (entry != NULL) {
                next = entry->next;
                if (entry->recv_vcc == vcc) {
                        lec_arp_clear_vccs(entry);
                        del_timer(&entry->timer);
                        kfree(entry);
                }
                else {
                        entry->next = priv->lec_no_forward;
                        priv->lec_no_forward = entry;
                }
                entry = next;
        }

        entry = priv->mcast_fwds;
        priv->mcast_fwds = NULL;
        while (entry != NULL) {
                next = entry->next;
                if (entry->recv_vcc == vcc) {
                        lec_arp_clear_vccs(entry);
                        /* No timer, LANEv2 7.1.20 and 2.3.5.3 */
                        kfree(entry);
                }
                else {
                        entry->next = priv->mcast_fwds;
                        priv->mcast_fwds = entry;
                }
                entry = next;
        }

Chas Williams's avatar
Chas Williams committed
2469
        lec_arp_put(priv);
Linus Torvalds's avatar
Linus Torvalds committed
2470 2471 2472 2473 2474 2475 2476
	dump_arp_table(priv);
}

void
lec_arp_check_empties(struct lec_priv *priv,
                      struct atm_vcc *vcc, struct sk_buff *skb)
{
Chas Williams's avatar
Chas Williams committed
2477
        unsigned long flags;
Linus Torvalds's avatar
Linus Torvalds committed
2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488
        struct lec_arp_table *entry, *prev;
        struct lecdatahdr_8023 *hdr = (struct lecdatahdr_8023 *)skb->data;
        unsigned char *src;
#ifdef CONFIG_TR
        struct lecdatahdr_8025 *tr_hdr = (struct lecdatahdr_8025 *)skb->data;

        if (priv->is_trdev) src = tr_hdr->h_source;
        else
#endif
        src = hdr->h_source;

Chas Williams's avatar
Chas Williams committed
2489
        lec_arp_get(priv);
Linus Torvalds's avatar
Linus Torvalds committed
2490 2491
        entry = priv->lec_arp_empty_ones;
        if (vcc == entry->vcc) {
2492
		spin_lock_irqsave(&priv->lec_arp_lock, flags);
Linus Torvalds's avatar
Linus Torvalds committed
2493 2494 2495 2496 2497
                del_timer(&entry->timer);
                memcpy(entry->mac_addr, src, ETH_ALEN);
                entry->status = ESI_FORWARD_DIRECT;
                entry->last_used = jiffies;
                priv->lec_arp_empty_ones = entry->next;
2498
                spin_unlock_irqrestore(&priv->lec_arp_lock, flags);
Linus Torvalds's avatar
Linus Torvalds committed
2499 2500
                /* We might have got an entry */
                if ((prev=lec_arp_find(priv,src))) {
2501
                        lec_arp_remove(priv, prev);
Linus Torvalds's avatar
Linus Torvalds committed
2502 2503
                        kfree(prev);
                }
2504
                lec_arp_add(priv, entry);
Chas Williams's avatar
Chas Williams committed
2505
                lec_arp_put(priv);
Linus Torvalds's avatar
Linus Torvalds committed
2506 2507
                return;
        }
2508
        spin_lock_irqsave(&priv->lec_arp_lock, flags);
Linus Torvalds's avatar
Linus Torvalds committed
2509 2510 2511 2512 2513 2514 2515 2516
        prev = entry;
        entry = entry->next;
        while (entry && entry->vcc != vcc) {
                prev= entry;
                entry = entry->next;
        }
        if (!entry) {
                DPRINTK("LEC_ARP: Arp_check_empties: entry not found!\n");
Chas Williams's avatar
Chas Williams committed
2517
                lec_arp_put(priv);
2518
                spin_unlock_irqrestore(&priv->lec_arp_lock, flags);
Linus Torvalds's avatar
Linus Torvalds committed
2519 2520 2521 2522 2523 2524 2525
                return;
        }
        del_timer(&entry->timer);
        memcpy(entry->mac_addr, src, ETH_ALEN);
        entry->status = ESI_FORWARD_DIRECT;
        entry->last_used = jiffies;
        prev->next = entry->next;
2526
        spin_unlock_irqrestore(&priv->lec_arp_lock, flags);
Linus Torvalds's avatar
Linus Torvalds committed
2527
        if ((prev = lec_arp_find(priv, src))) {
2528
                lec_arp_remove(priv, prev);
Linus Torvalds's avatar
Linus Torvalds committed
2529 2530
                kfree(prev);
        }
2531
        lec_arp_add(priv, entry);
Chas Williams's avatar
Chas Williams committed
2532
        lec_arp_put(priv);  
Linus Torvalds's avatar
Linus Torvalds committed
2533
}
Linus Torvalds's avatar
Linus Torvalds committed
2534
MODULE_LICENSE("GPL");