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

📄 vlan.c

📁 嵌入式系统设计与实验教材二源码linux内核移植与编译
💻 C
📖 第 1 页 / 共 2 页
字号:
/* * INET		802.1Q VLAN *		Ethernet-type device handling. * * Authors:	Ben Greear <greearb@candelatech.com> *              Please send support related email to: vlan@scry.wanfear.com *              VLAN Home Page: http://www.candelatech.com/~greear/vlan.html *  * Fixes: *              Fix for packet capture - Nick Eggleston <nick@dccinc.com>; * * *		This program is free software; you can redistribute it and/or *		modify it under the terms of the GNU General Public License *		as published by the Free Software Foundation; either version *		2 of the License, or (at your option) any later version. */#include <asm/uaccess.h> /* for copy_from_user */#include <linux/module.h>#include <linux/netdevice.h>#include <linux/skbuff.h>#include <net/datalink.h>#include <linux/mm.h>#include <linux/in.h>#include <linux/init.h>#include <net/p8022.h>#include <net/arp.h>#include <linux/rtnetlink.h>#include <linux/brlock.h>#include <linux/notifier.h>#include <linux/if_vlan.h>#include "vlan.h"#include "vlanproc.h"/* Global VLAN variables *//* Our listing of VLAN group(s) */struct vlan_group *p802_1Q_vlan_list;static char vlan_fullname[] = "802.1Q VLAN Support";static unsigned int vlan_version = 1;static unsigned int vlan_release = 6;static char vlan_copyright[] = " Ben Greear <greearb@candelatech.com>";static int vlan_device_event(struct notifier_block *, unsigned long, void *);struct notifier_block vlan_notifier_block = {	notifier_call: vlan_device_event,};/* These may be changed at run-time through IOCTLs *//* Determines interface naming scheme. */unsigned short vlan_name_type = VLAN_NAME_TYPE_RAW_PLUS_VID_NO_PAD;/* Counter for how many NON-VLAN protos we've received on a VLAN. */unsigned long vlan_bad_proto_recvd = 0;/* DO reorder the header by default */unsigned short vlan_default_dev_flags = 1;static struct packet_type vlan_packet_type = {	type: __constant_htons(ETH_P_8021Q),	dev:  NULL,	func: vlan_skb_recv, /* VLAN receive method */	data: (void *)(-1),  /* Set here '(void *)1' when this code can SHARE SKBs */	next: NULL};/* End of global variables definitions. *//* * Function vlan_proto_init (pro) * *    Initialize VLAN protocol layer,  * */static int __init vlan_proto_init(void){	int err;	printk(VLAN_INF "%s v%u.%u %s\n",	       vlan_fullname, vlan_version, vlan_release, vlan_copyright);	/* proc file system initialization */	err = vlan_proc_init();	if (err < 0) {		printk(KERN_ERR __FUNCTION__		       "%s: can't create entry in proc filesystem!\n",		       VLAN_NAME);		return 1;	}	dev_add_pack(&vlan_packet_type);	/* Register us to receive netdevice events */	register_netdevice_notifier(&vlan_notifier_block);	vlan_ioctl_hook = vlan_ioctl_handler;	printk(VLAN_INF "%s Initialization complete.\n", VLAN_NAME);	return 0;}/* * Cleanup of groups before exit */static void vlan_group_cleanup(void){	struct vlan_group *grp = NULL;	struct vlan_group *nextgroup;	for (grp = p802_1Q_vlan_list; (grp != NULL);) {		nextgroup = grp->next;		kfree(grp);		grp = nextgroup;	}	p802_1Q_vlan_list = NULL;}/* *     Module 'remove' entry point. *     o delete /proc/net/router directory and static entries. */ static void __exit vlan_cleanup_module(void){	/* Un-register us from receiving netdevice events */	unregister_netdevice_notifier(&vlan_notifier_block);	dev_remove_pack(&vlan_packet_type);	vlan_proc_cleanup();	vlan_group_cleanup();	vlan_ioctl_hook = NULL;}module_init(vlan_proto_init);module_exit(vlan_cleanup_module);/**  Will search linearly for now, based on device index.  Could * hash, or directly link, this some day. --Ben * TODO:  Potential performance issue here.  Linear search where N is *        the number of 'real' devices used by VLANs. */struct vlan_group* vlan_find_group(int real_dev_ifindex){	struct vlan_group *grp = NULL;	br_read_lock_bh(BR_NETPROTO_LOCK);	for (grp = p802_1Q_vlan_list;	     ((grp != NULL) && (grp->real_dev_ifindex != real_dev_ifindex));	     grp = grp->next) {		/* nothing */ ;	}	br_read_unlock_bh(BR_NETPROTO_LOCK);	return grp;}/*  Find the protocol handler.  Assumes VID < 0xFFF. */struct net_device *find_802_1Q_vlan_dev(struct net_device *real_dev,                                        unsigned short VID){	struct vlan_group *grp = vlan_find_group(real_dev->ifindex);	if (grp)                return grp->vlan_devices[VID];	return NULL;}/** This method will explicitly do a dev_put on the device if do_dev_put * is TRUE.  This gets around a difficulty with reference counting, and * the unregister-by-name (below).  If do_locks is true, it will grab * a lock before un-registering.  If do_locks is false, it is assumed that * the lock has already been grabbed externally...  --Ben */int unregister_802_1Q_vlan_dev(int real_dev_ifindex, unsigned short vlan_id,			       int do_dev_put, int do_locks){	struct net_device *dev = NULL;	struct vlan_group *grp;#ifdef VLAN_DEBUG	printk(VLAN_DBG __FUNCTION__ ": VID: %i\n", vlan_id);#endif	/* sanity check */	if ((vlan_id >= 0xFFF) || (vlan_id <= 0))		return -EINVAL;	grp = vlan_find_group(real_dev_ifindex);	if (grp) {		dev = grp->vlan_devices[vlan_id];		if (dev) {			/* Remove proc entry */			vlan_proc_rem_dev(dev);			/* Take it out of our own structures */			grp->vlan_devices[vlan_id] = NULL;			/* Take it out of the global list of devices.			 *  NOTE:  This deletes dev, don't access it again!!			 */			if (do_dev_put)				dev_put(dev);			/* TODO:  Please review this code. */			if (do_locks) {				rtnl_lock();				unregister_netdevice(dev);				rtnl_unlock();			} else {				unregister_netdevice(dev);			}			MOD_DEC_USE_COUNT;		}	}                return 0;}int unregister_802_1Q_vlan_device(const char *vlan_IF_name){	struct net_device *dev = NULL;#ifdef VLAN_DEBUG	printk(VLAN_DBG __FUNCTION__ ": unregister VLAN by name, name -:%s:-\n",	       vlan_IF_name);#endif	dev = dev_get_by_name(vlan_IF_name);	if (dev) {		if (dev->priv_flags & IFF_802_1Q_VLAN) {			return unregister_802_1Q_vlan_dev(				VLAN_DEV_INFO(dev)->real_dev->ifindex,				(unsigned short)(VLAN_DEV_INFO(dev)->vlan_id),				1 /* do dev_put */, 1 /* do locking */);		} else {			printk(VLAN_ERR __FUNCTION__			       ": ERROR:	Tried to remove a non-vlan device "			       "with VLAN code, name: %s  priv_flags: %hX\n",			       dev->name, dev->priv_flags);			dev_put(dev);			return -EPERM;		}	} else {#ifdef VLAN_DEBUG		printk(VLAN_DBG __FUNCTION__ ": WARNING: Could not find dev.\n");#endif		return -EINVAL;	}}/*  Attach a VLAN device to a mac address (ie Ethernet Card). *  Returns the device that was created, or NULL if there was *  an error of some kind. */struct net_device *register_802_1Q_vlan_device(const char* eth_IF_name,					       unsigned short VLAN_ID){	struct vlan_group *grp;	struct net_device *new_dev;	struct net_device *real_dev; /* the ethernet device */	int malloc_size = 0;#ifdef VLAN_DEBUG	printk(VLAN_DBG __FUNCTION__ ": if_name -:%s:-	vid: %i\n",	       eth_IF_name, VLAN_ID);#endif	if (VLAN_ID >= 0xfff)		goto out_ret_null;	/* find the device relating to eth_IF_name. */	real_dev = dev_get_by_name(eth_IF_name);	if (!real_dev)		goto out_ret_null;	/* TODO:  Make sure this device can really handle having a VLAN attached	 * to it...	 */	if (find_802_1Q_vlan_dev(real_dev, VLAN_ID)) {		/* was already registered. */		printk(VLAN_DBG __FUNCTION__ ": ALREADY had VLAN registered\n");		dev_put(real_dev);		return NULL;	}	malloc_size = (sizeof(struct net_device));	new_dev = (struct net_device *) kmalloc(malloc_size, GFP_KERNEL);	VLAN_MEM_DBG("net_device malloc, addr: %p  size: %i\n",		     new_dev, malloc_size);	if (new_dev == NULL)		goto out_put_dev;	memset(new_dev, 0, malloc_size);	/* set us up to not use a Qdisc, as the underlying Hardware device	 * can do all the queueing we could want.	 */	/* new_dev->qdisc_sleeping = &noqueue_qdisc;   Not needed it seems. */	new_dev->tx_queue_len = 0; /* This should effectively give us no queue. */	/* Gotta set up the fields for the device. */#ifdef VLAN_DEBUG	printk(VLAN_DBG "About to allocate name, vlan_name_type: %i\n",	       vlan_name_type);#endif	switch (vlan_name_type) {	case VLAN_NAME_TYPE_RAW_PLUS_VID:		/* name will look like:	 eth1.0005 */		sprintf(new_dev->name, "%s.%.4i", real_dev->name, VLAN_ID);		break;

⌨️ 快捷键说明

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