⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 lec.c

📁 Linux Kernel 2.6.9 for OMAP1710
💻 C
📖 第 1 页 / 共 5 页
字号:
/* * lec.c: Lan Emulation driver  * Marko Kiiskila mkiiskila@yahoo.com * */#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>#include <linux/spinlock.h>#include <linux/proc_fs.h>#include <linux/seq_file.h>/* 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};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);#endif/* Modular too */#include <linux/module.h>#include <linux/init.h>#include "lec.h"#include "lec_arpc.h"#include "resources.h"#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);static int lec_start_xmit(struct sk_buff *skb, struct net_device *dev);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);static struct lec_arp_table* lec_arp_find(struct lec_priv *priv,                                                     unsigned char *mac_addr);static int lec_arp_remove(struct lec_priv *priv,				     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 */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;}#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);                skb_queue_tail(&priv->lecd->sk->sk_receive_queue, skb2);                priv->lecd->sk->sk_data_ready(priv->lecd->sk, skb2->len);        }        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_TRunsigned 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;}static __inline__ voidlec_send(struct atm_vcc *vcc, struct sk_buff *skb, struct lec_priv *priv){	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) {		priv->stats.tx_dropped++;		return;	}	priv->stats.tx_packets++;	priv->stats.tx_bytes += skb->len;}static voidlec_tx_timeout(struct net_device *dev){	printk(KERN_INFO "%s: tx timeout\n", dev->name);	dev->trans_start = jiffies;	netif_wake_queue(dev);}static int lec_start_xmit(struct sk_buff *skb, struct net_device *dev){        struct sk_buff *skb2;        struct lec_priv *priv = (struct lec_priv *)dev->priv;        struct lecdatahdr_8023 *lec_h;        struct atm_vcc *vcc;	struct lec_arp_table *entry;        unsigned char *dst;	int min_frame_size;#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 */                DPRINTK("lec_start_xmit called\n");          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) {                DPRINTK("lec_start_xmit: reallocating skb\n");                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 */#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) {                if ((skb->len + skb_tailroom(skb)) < min_frame_size) {                        skb2 = skb_copy_expand(skb, 0,                            min_frame_size - skb->truesize, GFP_ATOMIC);                                dev_kfree_skb(skb);                        if (skb2 == NULL) {                                priv->stats.tx_dropped++;                                return 0;                        }                        skb = skb2;                }		skb_put(skb, min_frame_size - skb->len);        }                /* 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;        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)) {                    if (entry && (entry->tx_wait.qlen < LEC_UNRES_QUE_LEN)) {                        DPRINTK("%s:lec_start_xmit: queuing packet, ", dev->name);                        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 {                        DPRINTK("%s:lec_start_xmit: tx queue full or no arp entry, dropping, ", dev->name);                        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,               vcc->vpi, vcc->vci);       #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]);		lec_send(vcc, skb2, priv);        }	lec_send(vcc, skb, priv);	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);	}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -