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

📄 ipconfig.c

📁 linux 内核源代码
💻 C
📖 第 1 页 / 共 3 页
字号:
/* *  $Id: ipconfig.c,v 1.46 2002/02/01 22:01:04 davem Exp $ * *  Automatic Configuration of IP -- use DHCP, BOOTP, RARP, or *  user-supplied information to configure own IP address and routes. * *  Copyright (C) 1996-1998 Martin Mares <mj@atrey.karlin.mff.cuni.cz> * *  Derived from network configuration code in fs/nfs/nfsroot.c, *  originally Copyright (C) 1995, 1996 Gero Kuhlmann and me. * *  BOOTP rewritten to construct and analyse packets itself instead *  of misusing the IP layer. num_bugs_causing_wrong_arp_replies--; *					     -- MJ, December 1998 * *  Fixed ip_auto_config_setup calling at startup in the new "Linker Magic" *  initialization scheme. *	- Arnaldo Carvalho de Melo <acme@conectiva.com.br>, 08/11/1999 * *  DHCP support added.  To users this looks like a whole separate *  protocol, but we know it's just a bag on the side of BOOTP. *		-- Chip Salzenberg <chip@valinux.com>, May 2000 * *  Ported DHCP support from 2.2.16 to 2.4.0-test4 *              -- Eric Biederman <ebiederman@lnxi.com>, 30 Aug 2000 * *  Merged changes from 2.2.19 into 2.4.3 *              -- Eric Biederman <ebiederman@lnxi.com>, 22 April Aug 2001 * *  Multiple Nameservers in /proc/net/pnp *              --  Josef Siemes <jsiemes@web.de>, Aug 2002 */#include <linux/types.h>#include <linux/string.h>#include <linux/kernel.h>#include <linux/jiffies.h>#include <linux/random.h>#include <linux/init.h>#include <linux/utsname.h>#include <linux/in.h>#include <linux/if.h>#include <linux/inet.h>#include <linux/inetdevice.h>#include <linux/netdevice.h>#include <linux/if_arp.h>#include <linux/skbuff.h>#include <linux/ip.h>#include <linux/socket.h>#include <linux/route.h>#include <linux/udp.h>#include <linux/proc_fs.h>#include <linux/seq_file.h>#include <linux/major.h>#include <linux/root_dev.h>#include <linux/delay.h>#include <linux/nfs_fs.h>#include <net/net_namespace.h>#include <net/arp.h>#include <net/ip.h>#include <net/ipconfig.h>#include <net/route.h>#include <asm/uaccess.h>#include <net/checksum.h>#include <asm/processor.h>/* Define this to allow debugging output */#undef IPCONFIG_DEBUG#ifdef IPCONFIG_DEBUG#define DBG(x) printk x#else#define DBG(x) do { } while(0)#endif#if defined(CONFIG_IP_PNP_DHCP)#define IPCONFIG_DHCP#endif#if defined(CONFIG_IP_PNP_BOOTP) || defined(CONFIG_IP_PNP_DHCP)#define IPCONFIG_BOOTP#endif#if defined(CONFIG_IP_PNP_RARP)#define IPCONFIG_RARP#endif#if defined(IPCONFIG_BOOTP) || defined(IPCONFIG_RARP)#define IPCONFIG_DYNAMIC#endif/* Define the friendly delay before and after opening net devices */#define CONF_PRE_OPEN		500	/* Before opening: 1/2 second */#define CONF_POST_OPEN		1	/* After opening: 1 second *//* Define the timeout for waiting for a DHCP/BOOTP/RARP reply */#define CONF_OPEN_RETRIES 	2	/* (Re)open devices twice */#define CONF_SEND_RETRIES 	6	/* Send six requests per open */#define CONF_INTER_TIMEOUT	(HZ/2)	/* Inter-device timeout: 1/2 second */#define CONF_BASE_TIMEOUT	(HZ*2)	/* Initial timeout: 2 seconds */#define CONF_TIMEOUT_RANDOM	(HZ)	/* Maximum amount of randomization */#define CONF_TIMEOUT_MULT	*7/4	/* Rate of timeout growth */#define CONF_TIMEOUT_MAX	(HZ*30)	/* Maximum allowed timeout */#define CONF_NAMESERVERS_MAX   3       /* Maximum number of nameservers					   - '3' from resolv.h */#define NONE __constant_htonl(INADDR_NONE)/* * Public IP configuration *//* This is used by platforms which might be able to set the ipconfig * variables using firmware environment vars.  If this is set, it will * ignore such firmware variables. */int ic_set_manually __initdata = 0;		/* IPconfig parameters set manually */static int ic_enable __initdata = 0;		/* IP config enabled? *//* Protocol choice */int ic_proto_enabled __initdata = 0#ifdef IPCONFIG_BOOTP			| IC_BOOTP#endif#ifdef CONFIG_IP_PNP_DHCP			| IC_USE_DHCP#endif#ifdef IPCONFIG_RARP			| IC_RARP#endif			;static int ic_host_name_set __initdata = 0;	/* Host name set by us? */__be32 ic_myaddr = NONE;		/* My IP address */static __be32 ic_netmask = NONE;	/* Netmask for local subnet */__be32 ic_gateway = NONE;	/* Gateway IP address */__be32 ic_servaddr = NONE;	/* Boot server IP address */__be32 root_server_addr = NONE;	/* Address of NFS server */u8 root_server_path[256] = { 0, };	/* Path to mount as root *//* Persistent data: */static int ic_proto_used;			/* Protocol used, if any */static __be32 ic_nameservers[CONF_NAMESERVERS_MAX]; /* DNS Server IP addresses */static u8 ic_domain[64];		/* DNS (not NIS) domain name *//* * Private state. *//* Name of user-selected boot device */static char user_dev_name[IFNAMSIZ] __initdata = { 0, };/* Protocols supported by available interfaces */static int ic_proto_have_if __initdata = 0;#ifdef IPCONFIG_DYNAMICstatic DEFINE_SPINLOCK(ic_recv_lock);static volatile int ic_got_reply __initdata = 0;    /* Proto(s) that replied */#endif#ifdef IPCONFIG_DHCPstatic int ic_dhcp_msgtype __initdata = 0;	/* DHCP msg type received */#endif/* *	Network devices */struct ic_device {	struct ic_device *next;	struct net_device *dev;	unsigned short flags;	short able;	__be32 xid;};static struct ic_device *ic_first_dev __initdata = NULL;/* List of open device */static struct net_device *ic_dev __initdata = NULL;	/* Selected device */static int __init ic_open_devs(void){	struct ic_device *d, **last;	struct net_device *dev;	unsigned short oflags;	last = &ic_first_dev;	rtnl_lock();	/* bring loopback device up first */	for_each_netdev(&init_net, dev) {		if (!(dev->flags & IFF_LOOPBACK))			continue;		if (dev_change_flags(dev, dev->flags | IFF_UP) < 0)			printk(KERN_ERR "IP-Config: Failed to open %s\n", dev->name);	}	for_each_netdev(&init_net, dev) {		if (dev->flags & IFF_LOOPBACK)			continue;		if (user_dev_name[0] ? !strcmp(dev->name, user_dev_name) :		    (!(dev->flags & IFF_LOOPBACK) &&		     (dev->flags & (IFF_POINTOPOINT|IFF_BROADCAST)) &&		     strncmp(dev->name, "dummy", 5))) {			int able = 0;			if (dev->mtu >= 364)				able |= IC_BOOTP;			else				printk(KERN_WARNING "DHCP/BOOTP: Ignoring device %s, MTU %d too small", dev->name, dev->mtu);			if (!(dev->flags & IFF_NOARP))				able |= IC_RARP;			able &= ic_proto_enabled;			if (ic_proto_enabled && !able)				continue;			oflags = dev->flags;			if (dev_change_flags(dev, oflags | IFF_UP) < 0) {				printk(KERN_ERR "IP-Config: Failed to open %s\n", dev->name);				continue;			}			if (!(d = kmalloc(sizeof(struct ic_device), GFP_KERNEL))) {				rtnl_unlock();				return -1;			}			d->dev = dev;			*last = d;			last = &d->next;			d->flags = oflags;			d->able = able;			if (able & IC_BOOTP)				get_random_bytes(&d->xid, sizeof(__be32));			else				d->xid = 0;			ic_proto_have_if |= able;			DBG(("IP-Config: %s UP (able=%d, xid=%08x)\n",				dev->name, able, d->xid));		}	}	rtnl_unlock();	*last = NULL;	if (!ic_first_dev) {		if (user_dev_name[0])			printk(KERN_ERR "IP-Config: Device `%s' not found.\n", user_dev_name);		else			printk(KERN_ERR "IP-Config: No network devices available.\n");		return -1;	}	return 0;}static void __init ic_close_devs(void){	struct ic_device *d, *next;	struct net_device *dev;	rtnl_lock();	next = ic_first_dev;	while ((d = next)) {		next = d->next;		dev = d->dev;		if (dev != ic_dev) {			DBG(("IP-Config: Downing %s\n", dev->name));			dev_change_flags(dev, d->flags);		}		kfree(d);	}	rtnl_unlock();}/* *	Interface to various network functions. */static inline voidset_sockaddr(struct sockaddr_in *sin, __be32 addr, __be16 port){	sin->sin_family = AF_INET;	sin->sin_addr.s_addr = addr;	sin->sin_port = port;}static int __init ic_dev_ioctl(unsigned int cmd, struct ifreq *arg){	int res;	mm_segment_t oldfs = get_fs();	set_fs(get_ds());	res = devinet_ioctl(cmd, (struct ifreq __user *) arg);	set_fs(oldfs);	return res;}static int __init ic_route_ioctl(unsigned int cmd, struct rtentry *arg){	int res;	mm_segment_t oldfs = get_fs();	set_fs(get_ds());	res = ip_rt_ioctl(cmd, (void __user *) arg);	set_fs(oldfs);	return res;}/* *	Set up interface addresses and routes. */static int __init ic_setup_if(void){	struct ifreq ir;	struct sockaddr_in *sin = (void *) &ir.ifr_ifru.ifru_addr;	int err;	memset(&ir, 0, sizeof(ir));	strcpy(ir.ifr_ifrn.ifrn_name, ic_dev->name);	set_sockaddr(sin, ic_myaddr, 0);	if ((err = ic_dev_ioctl(SIOCSIFADDR, &ir)) < 0) {		printk(KERN_ERR "IP-Config: Unable to set interface address (%d).\n", err);		return -1;	}	set_sockaddr(sin, ic_netmask, 0);	if ((err = ic_dev_ioctl(SIOCSIFNETMASK, &ir)) < 0) {		printk(KERN_ERR "IP-Config: Unable to set interface netmask (%d).\n", err);		return -1;	}	set_sockaddr(sin, ic_myaddr | ~ic_netmask, 0);	if ((err = ic_dev_ioctl(SIOCSIFBRDADDR, &ir)) < 0) {		printk(KERN_ERR "IP-Config: Unable to set interface broadcast address (%d).\n", err);		return -1;	}	return 0;}static int __init ic_setup_routes(void){	/* No need to setup device routes, only the default route... */	if (ic_gateway != NONE) {		struct rtentry rm;		int err;		memset(&rm, 0, sizeof(rm));		if ((ic_gateway ^ ic_myaddr) & ic_netmask) {			printk(KERN_ERR "IP-Config: Gateway not on directly connected network.\n");			return -1;		}		set_sockaddr((struct sockaddr_in *) &rm.rt_dst, 0, 0);		set_sockaddr((struct sockaddr_in *) &rm.rt_genmask, 0, 0);		set_sockaddr((struct sockaddr_in *) &rm.rt_gateway, ic_gateway, 0);		rm.rt_flags = RTF_UP | RTF_GATEWAY;		if ((err = ic_route_ioctl(SIOCADDRT, &rm)) < 0) {			printk(KERN_ERR "IP-Config: Cannot add default route (%d).\n", err);			return -1;		}	}	return 0;}/* *	Fill in default values for all missing parameters. */static int __init ic_defaults(void){	/*	 *	At this point we have no userspace running so need not	 *	claim locks on system_utsname	 */	if (!ic_host_name_set)		sprintf(init_utsname()->nodename, "%u.%u.%u.%u", NIPQUAD(ic_myaddr));	if (root_server_addr == NONE)		root_server_addr = ic_servaddr;	if (ic_netmask == NONE) {		if (IN_CLASSA(ntohl(ic_myaddr)))			ic_netmask = htonl(IN_CLASSA_NET);		else if (IN_CLASSB(ntohl(ic_myaddr)))			ic_netmask = htonl(IN_CLASSB_NET);		else if (IN_CLASSC(ntohl(ic_myaddr)))			ic_netmask = htonl(IN_CLASSC_NET);		else {			printk(KERN_ERR "IP-Config: Unable to guess netmask for address %u.%u.%u.%u\n",				NIPQUAD(ic_myaddr));			return -1;		}		printk("IP-Config: Guessing netmask %u.%u.%u.%u\n", NIPQUAD(ic_netmask));	}	return 0;}/* *	RARP support. */#ifdef IPCONFIG_RARPstatic int ic_rarp_recv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev);static struct packet_type rarp_packet_type __initdata = {	.type =	__constant_htons(ETH_P_RARP),	.func =	ic_rarp_recv,};static inline void ic_rarp_init(void){	dev_add_pack(&rarp_packet_type);}static inline void ic_rarp_cleanup(void){	dev_remove_pack(&rarp_packet_type);}/* *  Process received RARP packet. */static int __initic_rarp_recv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev){	struct arphdr *rarp;	unsigned char *rarp_ptr;	__be32 sip, tip;	unsigned char *sha, *tha;		/* s for "source", t for "target" */	struct ic_device *d;	if (dev->nd_net != &init_net)		goto drop;	if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL)		return NET_RX_DROP;	if (!pskb_may_pull(skb, sizeof(struct arphdr)))		goto drop;	/* Basic sanity checks can be done without the lock.  */	rarp = (struct arphdr *)skb_transport_header(skb);	/* If this test doesn't pass, it's not IP, or we should	 * ignore it anyway.	 */	if (rarp->ar_hln != dev->addr_len || dev->type != ntohs(rarp->ar_hrd))		goto drop;	/* If it's not a RARP reply, delete it. */	if (rarp->ar_op != htons(ARPOP_RREPLY))		goto drop;	/* If it's not Ethernet, delete it. */	if (rarp->ar_pro != htons(ETH_P_IP))		goto drop;	if (!pskb_may_pull(skb,			   sizeof(struct arphdr) +			   (2 * dev->addr_len) +			   (2 * 4)))		goto drop;	/* OK, it is all there and looks valid, process... */	rarp = (struct arphdr *)skb_transport_header(skb);	rarp_ptr = (unsigned char *) (rarp + 1);	/* One reply at a time, please. */	spin_lock(&ic_recv_lock);	/* If we already have a reply, just drop the packet */	if (ic_got_reply)		goto drop_unlock;	/* Find the ic_device that the packet arrived on */	d = ic_first_dev;	while (d && d->dev != dev)		d = d->next;	if (!d)		goto drop_unlock;	/* should never happen */	/* Extract variable-width fields */	sha = rarp_ptr;	rarp_ptr += dev->addr_len;	memcpy(&sip, rarp_ptr, 4);	rarp_ptr += 4;	tha = rarp_ptr;	rarp_ptr += dev->addr_len;	memcpy(&tip, rarp_ptr, 4);	/* Discard packets which are not meant for us. */	if (memcmp(tha, dev->dev_addr, dev->addr_len))		goto drop_unlock;	/* Discard packets which are not from specified server. */	if (ic_servaddr != NONE && ic_servaddr != sip)		goto drop_unlock;	/* We have a winner! */	ic_dev = dev;	if (ic_myaddr == NONE)		ic_myaddr = tip;	ic_servaddr = sip;	ic_got_reply = IC_RARP;

⌨️ 快捷键说明

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