1692 lines · 39.1 KB · C
RawDownload
/* *  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 <linux/slab.h>#include <linux/export.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_POST_OPEN		10	/* After opening: 10 msecs */#define CONF_CARRIER_TIMEOUT	120000	/* Wait for carrier timeout */ /* 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 cpu_to_be32(INADDR_NONE)#define ANY cpu_to_be32(INADDR_ANY) /* * 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_addrservaddr = NONE;	/* IP Address of the IP addresses'server */ __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 */ __be32 ic_dev_xid;		/* Device under configuration */ /* vendor class identifier */static char vendor_class_identifier[253] __initdata; /* 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; /* MTU for boot device */static int ic_dev_mtu __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 bool __init ic_is_init_dev(struct net_device *dev){	if (dev->flags & IFF_LOOPBACK)		return false;	return 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));} static int __init ic_open_devs(void){	struct ic_device *d, **last;	struct net_device *dev;	unsigned short oflags;	unsigned long start, next_msg; 	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)			pr_err("IP-Config: Failed to open %s\n", dev->name);	} 	for_each_netdev(&init_net, dev) {		if (ic_is_init_dev(dev)) {			int able = 0;			if (dev->mtu >= 364)				able |= IC_BOOTP;			else				pr_warn("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) {				pr_err("IP-Config: Failed to open %s\n",				       dev->name);				continue;			}			if (!(d = kmalloc(sizeof(struct ic_device), GFP_KERNEL))) {				rtnl_unlock();				return -ENOMEM;			}			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));		}	} 	/* no point in waiting if we could not bring up at least one device */	if (!ic_first_dev)		goto have_carrier; 	/* wait for a carrier on at least one device */	start = jiffies;	next_msg = start + msecs_to_jiffies(CONF_CARRIER_TIMEOUT/12);	while (jiffies - start < msecs_to_jiffies(CONF_CARRIER_TIMEOUT)) {		int wait, elapsed; 		for_each_netdev(&init_net, dev)			if (ic_is_init_dev(dev) && netif_carrier_ok(dev))				goto have_carrier; 		msleep(1); 		if time_before(jiffies, next_msg)			continue; 		elapsed = jiffies_to_msecs(jiffies - start);		wait = (CONF_CARRIER_TIMEOUT - elapsed + 500)/1000;		pr_info("Waiting up to %d more seconds for network.\n", wait);		next_msg = jiffies + msecs_to_jiffies(CONF_CARRIER_TIMEOUT/12);	}have_carrier:	rtnl_unlock(); 	*last = NULL; 	if (!ic_first_dev) {		if (user_dev_name[0])			pr_err("IP-Config: Device `%s' not found\n",			       user_dev_name);		else			pr_err("IP-Config: No network devices available\n");		return -ENODEV;	}	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_devinet_ioctl(unsigned int cmd, struct ifreq *arg){	int res; 	mm_segment_t oldfs = get_fs();	set_fs(get_ds());	res = devinet_ioctl(&init_net, cmd, (struct ifreq __user *) arg);	set_fs(oldfs);	return res;} 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 = dev_ioctl(&init_net, 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(&init_net, 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_devinet_ioctl(SIOCSIFADDR, &ir)) < 0) {		pr_err("IP-Config: Unable to set interface address (%d)\n",		       err);		return -1;	}	set_sockaddr(sin, ic_netmask, 0);	if ((err = ic_devinet_ioctl(SIOCSIFNETMASK, &ir)) < 0) {		pr_err("IP-Config: Unable to set interface netmask (%d)\n",		       err);		return -1;	}	set_sockaddr(sin, ic_myaddr | ~ic_netmask, 0);	if ((err = ic_devinet_ioctl(SIOCSIFBRDADDR, &ir)) < 0) {		pr_err("IP-Config: Unable to set interface broadcast address (%d)\n",		       err);		return -1;	}	/* Handle the case where we need non-standard MTU on the boot link (a network	 * using jumbo frames, for instance).  If we can't set the mtu, don't error	 * out, we'll try to muddle along.	 */	if (ic_dev_mtu != 0) {		strcpy(ir.ifr_name, ic_dev->name);		ir.ifr_mtu = ic_dev_mtu;		if ((err = ic_dev_ioctl(SIOCSIFMTU, &ir)) < 0)			pr_err("IP-Config: Unable to set interface mtu to %d (%d)\n",			       ic_dev_mtu, err);	}	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) {			pr_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) {			pr_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, "%pI4", &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 {			pr_err("IP-Config: Unable to guess netmask for address %pI4\n",			       &ic_myaddr);			return -1;		}		printk("IP-Config: Guessing netmask %pI4\n", &ic_netmask);	} 	return 0;} /* *	RARP support. */ #ifdef IPCONFIG_RARP static 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 =	cpu_to_be16(ETH_P_RARP),	.func =	ic_rarp_recv,}; static inline void __init ic_rarp_init(void){	dev_add_pack(&rarp_packet_type);} static inline void __init 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;

Lines 1–500 of 1692.

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t