xref: /linux/net/ipv4/ipconfig.c (revision 6756ae4b4e97aba48c042b4aa6b77a18f507d2cb)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *  $Id: ipconfig.c,v 1.46 2002/02/01 22:01:04 davem Exp $
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  *  Automatic Configuration of IP -- use DHCP, BOOTP, RARP, or
51da177e4SLinus Torvalds  *  user-supplied information to configure own IP address and routes.
61da177e4SLinus Torvalds  *
71da177e4SLinus Torvalds  *  Copyright (C) 1996-1998 Martin Mares <mj@atrey.karlin.mff.cuni.cz>
81da177e4SLinus Torvalds  *
91da177e4SLinus Torvalds  *  Derived from network configuration code in fs/nfs/nfsroot.c,
101da177e4SLinus Torvalds  *  originally Copyright (C) 1995, 1996 Gero Kuhlmann and me.
111da177e4SLinus Torvalds  *
121da177e4SLinus Torvalds  *  BOOTP rewritten to construct and analyse packets itself instead
131da177e4SLinus Torvalds  *  of misusing the IP layer. num_bugs_causing_wrong_arp_replies--;
141da177e4SLinus Torvalds  *					     -- MJ, December 1998
151da177e4SLinus Torvalds  *
161da177e4SLinus Torvalds  *  Fixed ip_auto_config_setup calling at startup in the new "Linker Magic"
171da177e4SLinus Torvalds  *  initialization scheme.
181da177e4SLinus Torvalds  *	- Arnaldo Carvalho de Melo <acme@conectiva.com.br>, 08/11/1999
191da177e4SLinus Torvalds  *
201da177e4SLinus Torvalds  *  DHCP support added.  To users this looks like a whole separate
211da177e4SLinus Torvalds  *  protocol, but we know it's just a bag on the side of BOOTP.
221da177e4SLinus Torvalds  *		-- Chip Salzenberg <chip@valinux.com>, May 2000
231da177e4SLinus Torvalds  *
241da177e4SLinus Torvalds  *  Ported DHCP support from 2.2.16 to 2.4.0-test4
251da177e4SLinus Torvalds  *              -- Eric Biederman <ebiederman@lnxi.com>, 30 Aug 2000
261da177e4SLinus Torvalds  *
271da177e4SLinus Torvalds  *  Merged changes from 2.2.19 into 2.4.3
281da177e4SLinus Torvalds  *              -- Eric Biederman <ebiederman@lnxi.com>, 22 April Aug 2001
291da177e4SLinus Torvalds  *
301da177e4SLinus Torvalds  *  Multiple Nameservers in /proc/net/pnp
311da177e4SLinus Torvalds  *              --  Josef Siemes <jsiemes@web.de>, Aug 2002
321da177e4SLinus Torvalds  */
331da177e4SLinus Torvalds 
341da177e4SLinus Torvalds #include <linux/config.h>
351da177e4SLinus Torvalds #include <linux/types.h>
361da177e4SLinus Torvalds #include <linux/string.h>
371da177e4SLinus Torvalds #include <linux/kernel.h>
381da177e4SLinus Torvalds #include <linux/jiffies.h>
391da177e4SLinus Torvalds #include <linux/random.h>
401da177e4SLinus Torvalds #include <linux/init.h>
411da177e4SLinus Torvalds #include <linux/utsname.h>
421da177e4SLinus Torvalds #include <linux/in.h>
431da177e4SLinus Torvalds #include <linux/if.h>
441da177e4SLinus Torvalds #include <linux/inet.h>
4514c85021SArnaldo Carvalho de Melo #include <linux/inetdevice.h>
461da177e4SLinus Torvalds #include <linux/netdevice.h>
471da177e4SLinus Torvalds #include <linux/if_arp.h>
481da177e4SLinus Torvalds #include <linux/skbuff.h>
491da177e4SLinus Torvalds #include <linux/ip.h>
501da177e4SLinus Torvalds #include <linux/socket.h>
511da177e4SLinus Torvalds #include <linux/route.h>
521da177e4SLinus Torvalds #include <linux/udp.h>
531da177e4SLinus Torvalds #include <linux/proc_fs.h>
541da177e4SLinus Torvalds #include <linux/seq_file.h>
551da177e4SLinus Torvalds #include <linux/major.h>
561da177e4SLinus Torvalds #include <linux/root_dev.h>
571da177e4SLinus Torvalds #include <linux/delay.h>
5843d60661SAdrian Bunk #include <linux/nfs_fs.h>
591da177e4SLinus Torvalds #include <net/arp.h>
601da177e4SLinus Torvalds #include <net/ip.h>
611da177e4SLinus Torvalds #include <net/ipconfig.h>
6214c85021SArnaldo Carvalho de Melo #include <net/route.h>
631da177e4SLinus Torvalds 
641da177e4SLinus Torvalds #include <asm/uaccess.h>
651da177e4SLinus Torvalds #include <net/checksum.h>
661da177e4SLinus Torvalds #include <asm/processor.h>
671da177e4SLinus Torvalds 
681da177e4SLinus Torvalds /* Define this to allow debugging output */
691da177e4SLinus Torvalds #undef IPCONFIG_DEBUG
701da177e4SLinus Torvalds 
711da177e4SLinus Torvalds #ifdef IPCONFIG_DEBUG
721da177e4SLinus Torvalds #define DBG(x) printk x
731da177e4SLinus Torvalds #else
741da177e4SLinus Torvalds #define DBG(x) do { } while(0)
751da177e4SLinus Torvalds #endif
761da177e4SLinus Torvalds 
771da177e4SLinus Torvalds #if defined(CONFIG_IP_PNP_DHCP)
781da177e4SLinus Torvalds #define IPCONFIG_DHCP
791da177e4SLinus Torvalds #endif
801da177e4SLinus Torvalds #if defined(CONFIG_IP_PNP_BOOTP) || defined(CONFIG_IP_PNP_DHCP)
811da177e4SLinus Torvalds #define IPCONFIG_BOOTP
821da177e4SLinus Torvalds #endif
831da177e4SLinus Torvalds #if defined(CONFIG_IP_PNP_RARP)
841da177e4SLinus Torvalds #define IPCONFIG_RARP
851da177e4SLinus Torvalds #endif
861da177e4SLinus Torvalds #if defined(IPCONFIG_BOOTP) || defined(IPCONFIG_RARP)
871da177e4SLinus Torvalds #define IPCONFIG_DYNAMIC
881da177e4SLinus Torvalds #endif
891da177e4SLinus Torvalds 
901da177e4SLinus Torvalds /* Define the friendly delay before and after opening net devices */
911da177e4SLinus Torvalds #define CONF_PRE_OPEN		500	/* Before opening: 1/2 second */
921da177e4SLinus Torvalds #define CONF_POST_OPEN		1	/* After opening: 1 second */
931da177e4SLinus Torvalds 
941da177e4SLinus Torvalds /* Define the timeout for waiting for a DHCP/BOOTP/RARP reply */
951da177e4SLinus Torvalds #define CONF_OPEN_RETRIES 	2	/* (Re)open devices twice */
961da177e4SLinus Torvalds #define CONF_SEND_RETRIES 	6	/* Send six requests per open */
971da177e4SLinus Torvalds #define CONF_INTER_TIMEOUT	(HZ/2)	/* Inter-device timeout: 1/2 second */
981da177e4SLinus Torvalds #define CONF_BASE_TIMEOUT	(HZ*2)	/* Initial timeout: 2 seconds */
991da177e4SLinus Torvalds #define CONF_TIMEOUT_RANDOM	(HZ)	/* Maximum amount of randomization */
1001da177e4SLinus Torvalds #define CONF_TIMEOUT_MULT	*7/4	/* Rate of timeout growth */
1011da177e4SLinus Torvalds #define CONF_TIMEOUT_MAX	(HZ*30)	/* Maximum allowed timeout */
1021da177e4SLinus Torvalds #define CONF_NAMESERVERS_MAX   3       /* Maximum number of nameservers
1031da177e4SLinus Torvalds                                            - '3' from resolv.h */
1041da177e4SLinus Torvalds 
1051da177e4SLinus Torvalds 
1061da177e4SLinus Torvalds /*
1071da177e4SLinus Torvalds  * Public IP configuration
1081da177e4SLinus Torvalds  */
1091da177e4SLinus Torvalds 
1101da177e4SLinus Torvalds /* This is used by platforms which might be able to set the ipconfig
1111da177e4SLinus Torvalds  * variables using firmware environment vars.  If this is set, it will
1121da177e4SLinus Torvalds  * ignore such firmware variables.
1131da177e4SLinus Torvalds  */
1141da177e4SLinus Torvalds int ic_set_manually __initdata = 0;		/* IPconfig parameters set manually */
1151da177e4SLinus Torvalds 
1161da177e4SLinus Torvalds static int ic_enable __initdata = 0;		/* IP config enabled? */
1171da177e4SLinus Torvalds 
1181da177e4SLinus Torvalds /* Protocol choice */
1191da177e4SLinus Torvalds int ic_proto_enabled __initdata = 0
1201da177e4SLinus Torvalds #ifdef IPCONFIG_BOOTP
1211da177e4SLinus Torvalds 			| IC_BOOTP
1221da177e4SLinus Torvalds #endif
1231da177e4SLinus Torvalds #ifdef CONFIG_IP_PNP_DHCP
1241da177e4SLinus Torvalds 			| IC_USE_DHCP
1251da177e4SLinus Torvalds #endif
1261da177e4SLinus Torvalds #ifdef IPCONFIG_RARP
1271da177e4SLinus Torvalds 			| IC_RARP
1281da177e4SLinus Torvalds #endif
1291da177e4SLinus Torvalds 			;
1301da177e4SLinus Torvalds 
1311da177e4SLinus Torvalds static int ic_host_name_set __initdata = 0;	/* Host name set by us? */
1321da177e4SLinus Torvalds 
1331da177e4SLinus Torvalds u32 ic_myaddr = INADDR_NONE;		/* My IP address */
1341da177e4SLinus Torvalds static u32 ic_netmask = INADDR_NONE;	/* Netmask for local subnet */
1351da177e4SLinus Torvalds u32 ic_gateway = INADDR_NONE;	/* Gateway IP address */
1361da177e4SLinus Torvalds 
1371da177e4SLinus Torvalds u32 ic_servaddr = INADDR_NONE;	/* Boot server IP address */
1381da177e4SLinus Torvalds 
1391da177e4SLinus Torvalds u32 root_server_addr = INADDR_NONE;	/* Address of NFS server */
1401da177e4SLinus Torvalds u8 root_server_path[256] = { 0, };	/* Path to mount as root */
1411da177e4SLinus Torvalds 
1421da177e4SLinus Torvalds /* Persistent data: */
1431da177e4SLinus Torvalds 
1441da177e4SLinus Torvalds static int ic_proto_used;			/* Protocol used, if any */
1451da177e4SLinus Torvalds static u32 ic_nameservers[CONF_NAMESERVERS_MAX]; /* DNS Server IP addresses */
1461da177e4SLinus Torvalds static u8 ic_domain[64];		/* DNS (not NIS) domain name */
1471da177e4SLinus Torvalds 
1481da177e4SLinus Torvalds /*
1491da177e4SLinus Torvalds  * Private state.
1501da177e4SLinus Torvalds  */
1511da177e4SLinus Torvalds 
1521da177e4SLinus Torvalds /* Name of user-selected boot device */
1531da177e4SLinus Torvalds static char user_dev_name[IFNAMSIZ] __initdata = { 0, };
1541da177e4SLinus Torvalds 
1551da177e4SLinus Torvalds /* Protocols supported by available interfaces */
1561da177e4SLinus Torvalds static int ic_proto_have_if __initdata = 0;
1571da177e4SLinus Torvalds 
1581da177e4SLinus Torvalds #ifdef IPCONFIG_DYNAMIC
1591da177e4SLinus Torvalds static DEFINE_SPINLOCK(ic_recv_lock);
1601da177e4SLinus Torvalds static volatile int ic_got_reply __initdata = 0;    /* Proto(s) that replied */
1611da177e4SLinus Torvalds #endif
1621da177e4SLinus Torvalds #ifdef IPCONFIG_DHCP
1631da177e4SLinus Torvalds static int ic_dhcp_msgtype __initdata = 0;	/* DHCP msg type received */
1641da177e4SLinus Torvalds #endif
1651da177e4SLinus Torvalds 
1661da177e4SLinus Torvalds 
1671da177e4SLinus Torvalds /*
1681da177e4SLinus Torvalds  *	Network devices
1691da177e4SLinus Torvalds  */
1701da177e4SLinus Torvalds 
1711da177e4SLinus Torvalds struct ic_device {
1721da177e4SLinus Torvalds 	struct ic_device *next;
1731da177e4SLinus Torvalds 	struct net_device *dev;
1741da177e4SLinus Torvalds 	unsigned short flags;
1751da177e4SLinus Torvalds 	short able;
1761da177e4SLinus Torvalds 	u32 xid;
1771da177e4SLinus Torvalds };
1781da177e4SLinus Torvalds 
1791da177e4SLinus Torvalds static struct ic_device *ic_first_dev __initdata = NULL;/* List of open device */
1801da177e4SLinus Torvalds static struct net_device *ic_dev __initdata = NULL;	/* Selected device */
1811da177e4SLinus Torvalds 
1821da177e4SLinus Torvalds static int __init ic_open_devs(void)
1831da177e4SLinus Torvalds {
1841da177e4SLinus Torvalds 	struct ic_device *d, **last;
1851da177e4SLinus Torvalds 	struct net_device *dev;
1861da177e4SLinus Torvalds 	unsigned short oflags;
1871da177e4SLinus Torvalds 
1881da177e4SLinus Torvalds 	last = &ic_first_dev;
189*6756ae4bSStephen Hemminger 	rtnl_lock();
1901da177e4SLinus Torvalds 
1911da177e4SLinus Torvalds 	/* bring loopback device up first */
1921da177e4SLinus Torvalds 	if (dev_change_flags(&loopback_dev, loopback_dev.flags | IFF_UP) < 0)
1931da177e4SLinus Torvalds 		printk(KERN_ERR "IP-Config: Failed to open %s\n", loopback_dev.name);
1941da177e4SLinus Torvalds 
1951da177e4SLinus Torvalds 	for (dev = dev_base; dev; dev = dev->next) {
1961da177e4SLinus Torvalds 		if (dev == &loopback_dev)
1971da177e4SLinus Torvalds 			continue;
1981da177e4SLinus Torvalds 		if (user_dev_name[0] ? !strcmp(dev->name, user_dev_name) :
1991da177e4SLinus Torvalds 		    (!(dev->flags & IFF_LOOPBACK) &&
2001da177e4SLinus Torvalds 		     (dev->flags & (IFF_POINTOPOINT|IFF_BROADCAST)) &&
2011da177e4SLinus Torvalds 		     strncmp(dev->name, "dummy", 5))) {
2021da177e4SLinus Torvalds 			int able = 0;
2031da177e4SLinus Torvalds 			if (dev->mtu >= 364)
2041da177e4SLinus Torvalds 				able |= IC_BOOTP;
2051da177e4SLinus Torvalds 			else
2061da177e4SLinus Torvalds 				printk(KERN_WARNING "DHCP/BOOTP: Ignoring device %s, MTU %d too small", dev->name, dev->mtu);
2071da177e4SLinus Torvalds 			if (!(dev->flags & IFF_NOARP))
2081da177e4SLinus Torvalds 				able |= IC_RARP;
2091da177e4SLinus Torvalds 			able &= ic_proto_enabled;
2101da177e4SLinus Torvalds 			if (ic_proto_enabled && !able)
2111da177e4SLinus Torvalds 				continue;
2121da177e4SLinus Torvalds 			oflags = dev->flags;
2131da177e4SLinus Torvalds 			if (dev_change_flags(dev, oflags | IFF_UP) < 0) {
2141da177e4SLinus Torvalds 				printk(KERN_ERR "IP-Config: Failed to open %s\n", dev->name);
2151da177e4SLinus Torvalds 				continue;
2161da177e4SLinus Torvalds 			}
2171da177e4SLinus Torvalds 			if (!(d = kmalloc(sizeof(struct ic_device), GFP_KERNEL))) {
218*6756ae4bSStephen Hemminger 				rtnl_unlock();
2191da177e4SLinus Torvalds 				return -1;
2201da177e4SLinus Torvalds 			}
2211da177e4SLinus Torvalds 			d->dev = dev;
2221da177e4SLinus Torvalds 			*last = d;
2231da177e4SLinus Torvalds 			last = &d->next;
2241da177e4SLinus Torvalds 			d->flags = oflags;
2251da177e4SLinus Torvalds 			d->able = able;
2261da177e4SLinus Torvalds 			if (able & IC_BOOTP)
2271da177e4SLinus Torvalds 				get_random_bytes(&d->xid, sizeof(u32));
2281da177e4SLinus Torvalds 			else
2291da177e4SLinus Torvalds 				d->xid = 0;
2301da177e4SLinus Torvalds 			ic_proto_have_if |= able;
2311da177e4SLinus Torvalds 			DBG(("IP-Config: %s UP (able=%d, xid=%08x)\n",
2321da177e4SLinus Torvalds 				dev->name, able, d->xid));
2331da177e4SLinus Torvalds 		}
2341da177e4SLinus Torvalds 	}
235*6756ae4bSStephen Hemminger 	rtnl_unlock();
2361da177e4SLinus Torvalds 
2371da177e4SLinus Torvalds 	*last = NULL;
2381da177e4SLinus Torvalds 
2391da177e4SLinus Torvalds 	if (!ic_first_dev) {
2401da177e4SLinus Torvalds 		if (user_dev_name[0])
2411da177e4SLinus Torvalds 			printk(KERN_ERR "IP-Config: Device `%s' not found.\n", user_dev_name);
2421da177e4SLinus Torvalds 		else
2431da177e4SLinus Torvalds 			printk(KERN_ERR "IP-Config: No network devices available.\n");
2441da177e4SLinus Torvalds 		return -1;
2451da177e4SLinus Torvalds 	}
2461da177e4SLinus Torvalds 	return 0;
2471da177e4SLinus Torvalds }
2481da177e4SLinus Torvalds 
2491da177e4SLinus Torvalds static void __init ic_close_devs(void)
2501da177e4SLinus Torvalds {
2511da177e4SLinus Torvalds 	struct ic_device *d, *next;
2521da177e4SLinus Torvalds 	struct net_device *dev;
2531da177e4SLinus Torvalds 
254*6756ae4bSStephen Hemminger 	rtnl_lock();
2551da177e4SLinus Torvalds 	next = ic_first_dev;
2561da177e4SLinus Torvalds 	while ((d = next)) {
2571da177e4SLinus Torvalds 		next = d->next;
2581da177e4SLinus Torvalds 		dev = d->dev;
2591da177e4SLinus Torvalds 		if (dev != ic_dev) {
2601da177e4SLinus Torvalds 			DBG(("IP-Config: Downing %s\n", dev->name));
2611da177e4SLinus Torvalds 			dev_change_flags(dev, d->flags);
2621da177e4SLinus Torvalds 		}
2631da177e4SLinus Torvalds 		kfree(d);
2641da177e4SLinus Torvalds 	}
265*6756ae4bSStephen Hemminger 	rtnl_unlock();
2661da177e4SLinus Torvalds }
2671da177e4SLinus Torvalds 
2681da177e4SLinus Torvalds /*
2691da177e4SLinus Torvalds  *	Interface to various network functions.
2701da177e4SLinus Torvalds  */
2711da177e4SLinus Torvalds 
2721da177e4SLinus Torvalds static inline void
2731da177e4SLinus Torvalds set_sockaddr(struct sockaddr_in *sin, u32 addr, u16 port)
2741da177e4SLinus Torvalds {
2751da177e4SLinus Torvalds 	sin->sin_family = AF_INET;
2761da177e4SLinus Torvalds 	sin->sin_addr.s_addr = addr;
2771da177e4SLinus Torvalds 	sin->sin_port = port;
2781da177e4SLinus Torvalds }
2791da177e4SLinus Torvalds 
2801da177e4SLinus Torvalds static int __init ic_dev_ioctl(unsigned int cmd, struct ifreq *arg)
2811da177e4SLinus Torvalds {
2821da177e4SLinus Torvalds 	int res;
2831da177e4SLinus Torvalds 
2841da177e4SLinus Torvalds 	mm_segment_t oldfs = get_fs();
2851da177e4SLinus Torvalds 	set_fs(get_ds());
2861da177e4SLinus Torvalds 	res = devinet_ioctl(cmd, (struct ifreq __user *) arg);
2871da177e4SLinus Torvalds 	set_fs(oldfs);
2881da177e4SLinus Torvalds 	return res;
2891da177e4SLinus Torvalds }
2901da177e4SLinus Torvalds 
2911da177e4SLinus Torvalds static int __init ic_route_ioctl(unsigned int cmd, struct rtentry *arg)
2921da177e4SLinus Torvalds {
2931da177e4SLinus Torvalds 	int res;
2941da177e4SLinus Torvalds 
2951da177e4SLinus Torvalds 	mm_segment_t oldfs = get_fs();
2961da177e4SLinus Torvalds 	set_fs(get_ds());
2971da177e4SLinus Torvalds 	res = ip_rt_ioctl(cmd, (void __user *) arg);
2981da177e4SLinus Torvalds 	set_fs(oldfs);
2991da177e4SLinus Torvalds 	return res;
3001da177e4SLinus Torvalds }
3011da177e4SLinus Torvalds 
3021da177e4SLinus Torvalds /*
3031da177e4SLinus Torvalds  *	Set up interface addresses and routes.
3041da177e4SLinus Torvalds  */
3051da177e4SLinus Torvalds 
3061da177e4SLinus Torvalds static int __init ic_setup_if(void)
3071da177e4SLinus Torvalds {
3081da177e4SLinus Torvalds 	struct ifreq ir;
3091da177e4SLinus Torvalds 	struct sockaddr_in *sin = (void *) &ir.ifr_ifru.ifru_addr;
3101da177e4SLinus Torvalds 	int err;
3111da177e4SLinus Torvalds 
3121da177e4SLinus Torvalds 	memset(&ir, 0, sizeof(ir));
3131da177e4SLinus Torvalds 	strcpy(ir.ifr_ifrn.ifrn_name, ic_dev->name);
3141da177e4SLinus Torvalds 	set_sockaddr(sin, ic_myaddr, 0);
3151da177e4SLinus Torvalds 	if ((err = ic_dev_ioctl(SIOCSIFADDR, &ir)) < 0) {
3161da177e4SLinus Torvalds 		printk(KERN_ERR "IP-Config: Unable to set interface address (%d).\n", err);
3171da177e4SLinus Torvalds 		return -1;
3181da177e4SLinus Torvalds 	}
3191da177e4SLinus Torvalds 	set_sockaddr(sin, ic_netmask, 0);
3201da177e4SLinus Torvalds 	if ((err = ic_dev_ioctl(SIOCSIFNETMASK, &ir)) < 0) {
3211da177e4SLinus Torvalds 		printk(KERN_ERR "IP-Config: Unable to set interface netmask (%d).\n", err);
3221da177e4SLinus Torvalds 		return -1;
3231da177e4SLinus Torvalds 	}
3241da177e4SLinus Torvalds 	set_sockaddr(sin, ic_myaddr | ~ic_netmask, 0);
3251da177e4SLinus Torvalds 	if ((err = ic_dev_ioctl(SIOCSIFBRDADDR, &ir)) < 0) {
3261da177e4SLinus Torvalds 		printk(KERN_ERR "IP-Config: Unable to set interface broadcast address (%d).\n", err);
3271da177e4SLinus Torvalds 		return -1;
3281da177e4SLinus Torvalds 	}
3291da177e4SLinus Torvalds 	return 0;
3301da177e4SLinus Torvalds }
3311da177e4SLinus Torvalds 
3321da177e4SLinus Torvalds static int __init ic_setup_routes(void)
3331da177e4SLinus Torvalds {
3341da177e4SLinus Torvalds 	/* No need to setup device routes, only the default route... */
3351da177e4SLinus Torvalds 
3361da177e4SLinus Torvalds 	if (ic_gateway != INADDR_NONE) {
3371da177e4SLinus Torvalds 		struct rtentry rm;
3381da177e4SLinus Torvalds 		int err;
3391da177e4SLinus Torvalds 
3401da177e4SLinus Torvalds 		memset(&rm, 0, sizeof(rm));
3411da177e4SLinus Torvalds 		if ((ic_gateway ^ ic_myaddr) & ic_netmask) {
3421da177e4SLinus Torvalds 			printk(KERN_ERR "IP-Config: Gateway not on directly connected network.\n");
3431da177e4SLinus Torvalds 			return -1;
3441da177e4SLinus Torvalds 		}
3451da177e4SLinus Torvalds 		set_sockaddr((struct sockaddr_in *) &rm.rt_dst, 0, 0);
3461da177e4SLinus Torvalds 		set_sockaddr((struct sockaddr_in *) &rm.rt_genmask, 0, 0);
3471da177e4SLinus Torvalds 		set_sockaddr((struct sockaddr_in *) &rm.rt_gateway, ic_gateway, 0);
3481da177e4SLinus Torvalds 		rm.rt_flags = RTF_UP | RTF_GATEWAY;
3491da177e4SLinus Torvalds 		if ((err = ic_route_ioctl(SIOCADDRT, &rm)) < 0) {
3501da177e4SLinus Torvalds 			printk(KERN_ERR "IP-Config: Cannot add default route (%d).\n", err);
3511da177e4SLinus Torvalds 			return -1;
3521da177e4SLinus Torvalds 		}
3531da177e4SLinus Torvalds 	}
3541da177e4SLinus Torvalds 
3551da177e4SLinus Torvalds 	return 0;
3561da177e4SLinus Torvalds }
3571da177e4SLinus Torvalds 
3581da177e4SLinus Torvalds /*
3591da177e4SLinus Torvalds  *	Fill in default values for all missing parameters.
3601da177e4SLinus Torvalds  */
3611da177e4SLinus Torvalds 
3621da177e4SLinus Torvalds static int __init ic_defaults(void)
3631da177e4SLinus Torvalds {
3641da177e4SLinus Torvalds 	/*
3651da177e4SLinus Torvalds 	 *	At this point we have no userspace running so need not
3661da177e4SLinus Torvalds 	 *	claim locks on system_utsname
3671da177e4SLinus Torvalds 	 */
3681da177e4SLinus Torvalds 
3691da177e4SLinus Torvalds 	if (!ic_host_name_set)
3701da177e4SLinus Torvalds 		sprintf(system_utsname.nodename, "%u.%u.%u.%u", NIPQUAD(ic_myaddr));
3711da177e4SLinus Torvalds 
3721da177e4SLinus Torvalds 	if (root_server_addr == INADDR_NONE)
3731da177e4SLinus Torvalds 		root_server_addr = ic_servaddr;
3741da177e4SLinus Torvalds 
3751da177e4SLinus Torvalds 	if (ic_netmask == INADDR_NONE) {
3761da177e4SLinus Torvalds 		if (IN_CLASSA(ntohl(ic_myaddr)))
3771da177e4SLinus Torvalds 			ic_netmask = htonl(IN_CLASSA_NET);
3781da177e4SLinus Torvalds 		else if (IN_CLASSB(ntohl(ic_myaddr)))
3791da177e4SLinus Torvalds 			ic_netmask = htonl(IN_CLASSB_NET);
3801da177e4SLinus Torvalds 		else if (IN_CLASSC(ntohl(ic_myaddr)))
3811da177e4SLinus Torvalds 			ic_netmask = htonl(IN_CLASSC_NET);
3821da177e4SLinus Torvalds 		else {
3831da177e4SLinus Torvalds 			printk(KERN_ERR "IP-Config: Unable to guess netmask for address %u.%u.%u.%u\n",
3841da177e4SLinus Torvalds 				NIPQUAD(ic_myaddr));
3851da177e4SLinus Torvalds 			return -1;
3861da177e4SLinus Torvalds 		}
3871da177e4SLinus Torvalds 		printk("IP-Config: Guessing netmask %u.%u.%u.%u\n", NIPQUAD(ic_netmask));
3881da177e4SLinus Torvalds 	}
3891da177e4SLinus Torvalds 
3901da177e4SLinus Torvalds 	return 0;
3911da177e4SLinus Torvalds }
3921da177e4SLinus Torvalds 
3931da177e4SLinus Torvalds /*
3941da177e4SLinus Torvalds  *	RARP support.
3951da177e4SLinus Torvalds  */
3961da177e4SLinus Torvalds 
3971da177e4SLinus Torvalds #ifdef IPCONFIG_RARP
3981da177e4SLinus Torvalds 
399f2ccd8faSDavid S. Miller static int ic_rarp_recv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev);
4001da177e4SLinus Torvalds 
4011da177e4SLinus Torvalds static struct packet_type rarp_packet_type __initdata = {
4021da177e4SLinus Torvalds 	.type =	__constant_htons(ETH_P_RARP),
4031da177e4SLinus Torvalds 	.func =	ic_rarp_recv,
4041da177e4SLinus Torvalds };
4051da177e4SLinus Torvalds 
4061da177e4SLinus Torvalds static inline void ic_rarp_init(void)
4071da177e4SLinus Torvalds {
4081da177e4SLinus Torvalds 	dev_add_pack(&rarp_packet_type);
4091da177e4SLinus Torvalds }
4101da177e4SLinus Torvalds 
4111da177e4SLinus Torvalds static inline void ic_rarp_cleanup(void)
4121da177e4SLinus Torvalds {
4131da177e4SLinus Torvalds 	dev_remove_pack(&rarp_packet_type);
4141da177e4SLinus Torvalds }
4151da177e4SLinus Torvalds 
4161da177e4SLinus Torvalds /*
4171da177e4SLinus Torvalds  *  Process received RARP packet.
4181da177e4SLinus Torvalds  */
4191da177e4SLinus Torvalds static int __init
420f2ccd8faSDavid S. Miller ic_rarp_recv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev)
4211da177e4SLinus Torvalds {
4221da177e4SLinus Torvalds 	struct arphdr *rarp;
4231da177e4SLinus Torvalds 	unsigned char *rarp_ptr;
4241da177e4SLinus Torvalds 	unsigned long sip, tip;
4251da177e4SLinus Torvalds 	unsigned char *sha, *tha;		/* s for "source", t for "target" */
4261da177e4SLinus Torvalds 	struct ic_device *d;
4271da177e4SLinus Torvalds 
4281da177e4SLinus Torvalds 	if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL)
4291da177e4SLinus Torvalds 		return NET_RX_DROP;
4301da177e4SLinus Torvalds 
4311da177e4SLinus Torvalds 	if (!pskb_may_pull(skb, sizeof(struct arphdr)))
4321da177e4SLinus Torvalds 		goto drop;
4331da177e4SLinus Torvalds 
4341da177e4SLinus Torvalds 	/* Basic sanity checks can be done without the lock.  */
4351da177e4SLinus Torvalds 	rarp = (struct arphdr *)skb->h.raw;
4361da177e4SLinus Torvalds 
4371da177e4SLinus Torvalds 	/* If this test doesn't pass, it's not IP, or we should
4381da177e4SLinus Torvalds 	 * ignore it anyway.
4391da177e4SLinus Torvalds 	 */
4401da177e4SLinus Torvalds 	if (rarp->ar_hln != dev->addr_len || dev->type != ntohs(rarp->ar_hrd))
4411da177e4SLinus Torvalds 		goto drop;
4421da177e4SLinus Torvalds 
4431da177e4SLinus Torvalds 	/* If it's not a RARP reply, delete it. */
4441da177e4SLinus Torvalds 	if (rarp->ar_op != htons(ARPOP_RREPLY))
4451da177e4SLinus Torvalds 		goto drop;
4461da177e4SLinus Torvalds 
4471da177e4SLinus Torvalds 	/* If it's not Ethernet, delete it. */
4481da177e4SLinus Torvalds 	if (rarp->ar_pro != htons(ETH_P_IP))
4491da177e4SLinus Torvalds 		goto drop;
4501da177e4SLinus Torvalds 
4511da177e4SLinus Torvalds 	if (!pskb_may_pull(skb,
4521da177e4SLinus Torvalds 			   sizeof(struct arphdr) +
4531da177e4SLinus Torvalds 			   (2 * dev->addr_len) +
4541da177e4SLinus Torvalds 			   (2 * 4)))
4551da177e4SLinus Torvalds 		goto drop;
4561da177e4SLinus Torvalds 
4571da177e4SLinus Torvalds 	/* OK, it is all there and looks valid, process... */
4581da177e4SLinus Torvalds 	rarp = (struct arphdr *)skb->h.raw;
4591da177e4SLinus Torvalds 	rarp_ptr = (unsigned char *) (rarp + 1);
4601da177e4SLinus Torvalds 
4611da177e4SLinus Torvalds 	/* One reply at a time, please. */
4621da177e4SLinus Torvalds 	spin_lock(&ic_recv_lock);
4631da177e4SLinus Torvalds 
4641da177e4SLinus Torvalds 	/* If we already have a reply, just drop the packet */
4651da177e4SLinus Torvalds 	if (ic_got_reply)
4661da177e4SLinus Torvalds 		goto drop_unlock;
4671da177e4SLinus Torvalds 
4681da177e4SLinus Torvalds 	/* Find the ic_device that the packet arrived on */
4691da177e4SLinus Torvalds 	d = ic_first_dev;
4701da177e4SLinus Torvalds 	while (d && d->dev != dev)
4711da177e4SLinus Torvalds 		d = d->next;
4721da177e4SLinus Torvalds 	if (!d)
4731da177e4SLinus Torvalds 		goto drop_unlock;	/* should never happen */
4741da177e4SLinus Torvalds 
4751da177e4SLinus Torvalds 	/* Extract variable-width fields */
4761da177e4SLinus Torvalds 	sha = rarp_ptr;
4771da177e4SLinus Torvalds 	rarp_ptr += dev->addr_len;
4781da177e4SLinus Torvalds 	memcpy(&sip, rarp_ptr, 4);
4791da177e4SLinus Torvalds 	rarp_ptr += 4;
4801da177e4SLinus Torvalds 	tha = rarp_ptr;
4811da177e4SLinus Torvalds 	rarp_ptr += dev->addr_len;
4821da177e4SLinus Torvalds 	memcpy(&tip, rarp_ptr, 4);
4831da177e4SLinus Torvalds 
4841da177e4SLinus Torvalds 	/* Discard packets which are not meant for us. */
4851da177e4SLinus Torvalds 	if (memcmp(tha, dev->dev_addr, dev->addr_len))
4861da177e4SLinus Torvalds 		goto drop_unlock;
4871da177e4SLinus Torvalds 
4881da177e4SLinus Torvalds 	/* Discard packets which are not from specified server. */
4891da177e4SLinus Torvalds 	if (ic_servaddr != INADDR_NONE && ic_servaddr != sip)
4901da177e4SLinus Torvalds 		goto drop_unlock;
4911da177e4SLinus Torvalds 
4921da177e4SLinus Torvalds 	/* We have a winner! */
4931da177e4SLinus Torvalds 	ic_dev = dev;
4941da177e4SLinus Torvalds 	if (ic_myaddr == INADDR_NONE)
4951da177e4SLinus Torvalds 		ic_myaddr = tip;
4961da177e4SLinus Torvalds 	ic_servaddr = sip;
4971da177e4SLinus Torvalds 	ic_got_reply = IC_RARP;
4981da177e4SLinus Torvalds 
4991da177e4SLinus Torvalds drop_unlock:
5001da177e4SLinus Torvalds 	/* Show's over.  Nothing to see here.  */
5011da177e4SLinus Torvalds 	spin_unlock(&ic_recv_lock);
5021da177e4SLinus Torvalds 
5031da177e4SLinus Torvalds drop:
5041da177e4SLinus Torvalds 	/* Throw the packet out. */
5051da177e4SLinus Torvalds 	kfree_skb(skb);
5061da177e4SLinus Torvalds 	return 0;
5071da177e4SLinus Torvalds }
5081da177e4SLinus Torvalds 
5091da177e4SLinus Torvalds 
5101da177e4SLinus Torvalds /*
5111da177e4SLinus Torvalds  *  Send RARP request packet over a single interface.
5121da177e4SLinus Torvalds  */
5131da177e4SLinus Torvalds static void __init ic_rarp_send_if(struct ic_device *d)
5141da177e4SLinus Torvalds {
5151da177e4SLinus Torvalds 	struct net_device *dev = d->dev;
5161da177e4SLinus Torvalds 	arp_send(ARPOP_RREQUEST, ETH_P_RARP, 0, dev, 0, NULL,
5171da177e4SLinus Torvalds 		 dev->dev_addr, dev->dev_addr);
5181da177e4SLinus Torvalds }
5191da177e4SLinus Torvalds #endif
5201da177e4SLinus Torvalds 
5211da177e4SLinus Torvalds /*
5221da177e4SLinus Torvalds  *	DHCP/BOOTP support.
5231da177e4SLinus Torvalds  */
5241da177e4SLinus Torvalds 
5251da177e4SLinus Torvalds #ifdef IPCONFIG_BOOTP
5261da177e4SLinus Torvalds 
5271da177e4SLinus Torvalds struct bootp_pkt {		/* BOOTP packet format */
5281da177e4SLinus Torvalds 	struct iphdr iph;	/* IP header */
5291da177e4SLinus Torvalds 	struct udphdr udph;	/* UDP header */
5301da177e4SLinus Torvalds 	u8 op;			/* 1=request, 2=reply */
5311da177e4SLinus Torvalds 	u8 htype;		/* HW address type */
5321da177e4SLinus Torvalds 	u8 hlen;		/* HW address length */
5331da177e4SLinus Torvalds 	u8 hops;		/* Used only by gateways */
5341da177e4SLinus Torvalds 	u32 xid;		/* Transaction ID */
5351da177e4SLinus Torvalds 	u16 secs;		/* Seconds since we started */
5361da177e4SLinus Torvalds 	u16 flags;		/* Just what it says */
5371da177e4SLinus Torvalds 	u32 client_ip;		/* Client's IP address if known */
5381da177e4SLinus Torvalds 	u32 your_ip;		/* Assigned IP address */
5391da177e4SLinus Torvalds 	u32 server_ip;		/* (Next, e.g. NFS) Server's IP address */
5401da177e4SLinus Torvalds 	u32 relay_ip;		/* IP address of BOOTP relay */
5411da177e4SLinus Torvalds 	u8 hw_addr[16];		/* Client's HW address */
5421da177e4SLinus Torvalds 	u8 serv_name[64];	/* Server host name */
5431da177e4SLinus Torvalds 	u8 boot_file[128];	/* Name of boot file */
5441da177e4SLinus Torvalds 	u8 exten[312];		/* DHCP options / BOOTP vendor extensions */
5451da177e4SLinus Torvalds };
5461da177e4SLinus Torvalds 
5471da177e4SLinus Torvalds /* packet ops */
5481da177e4SLinus Torvalds #define BOOTP_REQUEST	1
5491da177e4SLinus Torvalds #define BOOTP_REPLY	2
5501da177e4SLinus Torvalds 
5511da177e4SLinus Torvalds /* DHCP message types */
5521da177e4SLinus Torvalds #define DHCPDISCOVER	1
5531da177e4SLinus Torvalds #define DHCPOFFER	2
5541da177e4SLinus Torvalds #define DHCPREQUEST	3
5551da177e4SLinus Torvalds #define DHCPDECLINE	4
5561da177e4SLinus Torvalds #define DHCPACK		5
5571da177e4SLinus Torvalds #define DHCPNAK		6
5581da177e4SLinus Torvalds #define DHCPRELEASE	7
5591da177e4SLinus Torvalds #define DHCPINFORM	8
5601da177e4SLinus Torvalds 
561f2ccd8faSDavid S. Miller static int ic_bootp_recv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev);
5621da177e4SLinus Torvalds 
5631da177e4SLinus Torvalds static struct packet_type bootp_packet_type __initdata = {
5641da177e4SLinus Torvalds 	.type =	__constant_htons(ETH_P_IP),
5651da177e4SLinus Torvalds 	.func =	ic_bootp_recv,
5661da177e4SLinus Torvalds };
5671da177e4SLinus Torvalds 
5681da177e4SLinus Torvalds 
5691da177e4SLinus Torvalds /*
5701da177e4SLinus Torvalds  *  Initialize DHCP/BOOTP extension fields in the request.
5711da177e4SLinus Torvalds  */
5721da177e4SLinus Torvalds 
5731da177e4SLinus Torvalds static const u8 ic_bootp_cookie[4] = { 99, 130, 83, 99 };
5741da177e4SLinus Torvalds 
5751da177e4SLinus Torvalds #ifdef IPCONFIG_DHCP
5761da177e4SLinus Torvalds 
5771da177e4SLinus Torvalds static void __init
5781da177e4SLinus Torvalds ic_dhcp_init_options(u8 *options)
5791da177e4SLinus Torvalds {
5801da177e4SLinus Torvalds 	u8 mt = ((ic_servaddr == INADDR_NONE)
5811da177e4SLinus Torvalds 		 ? DHCPDISCOVER : DHCPREQUEST);
5821da177e4SLinus Torvalds 	u8 *e = options;
5831da177e4SLinus Torvalds 
5841da177e4SLinus Torvalds #ifdef IPCONFIG_DEBUG
5851da177e4SLinus Torvalds 	printk("DHCP: Sending message type %d\n", mt);
5861da177e4SLinus Torvalds #endif
5871da177e4SLinus Torvalds 
5881da177e4SLinus Torvalds 	memcpy(e, ic_bootp_cookie, 4);	/* RFC1048 Magic Cookie */
5891da177e4SLinus Torvalds 	e += 4;
5901da177e4SLinus Torvalds 
5911da177e4SLinus Torvalds 	*e++ = 53;		/* DHCP message type */
5921da177e4SLinus Torvalds 	*e++ = 1;
5931da177e4SLinus Torvalds 	*e++ = mt;
5941da177e4SLinus Torvalds 
5951da177e4SLinus Torvalds 	if (mt == DHCPREQUEST) {
5961da177e4SLinus Torvalds 		*e++ = 54;	/* Server ID (IP address) */
5971da177e4SLinus Torvalds 		*e++ = 4;
5981da177e4SLinus Torvalds 		memcpy(e, &ic_servaddr, 4);
5991da177e4SLinus Torvalds 		e += 4;
6001da177e4SLinus Torvalds 
6011da177e4SLinus Torvalds 		*e++ = 50;	/* Requested IP address */
6021da177e4SLinus Torvalds 		*e++ = 4;
6031da177e4SLinus Torvalds 		memcpy(e, &ic_myaddr, 4);
6041da177e4SLinus Torvalds 		e += 4;
6051da177e4SLinus Torvalds 	}
6061da177e4SLinus Torvalds 
6071da177e4SLinus Torvalds 	/* always? */
6081da177e4SLinus Torvalds 	{
6091da177e4SLinus Torvalds 		static const u8 ic_req_params[] = {
6101da177e4SLinus Torvalds 			1,	/* Subnet mask */
6111da177e4SLinus Torvalds 			3,	/* Default gateway */
6121da177e4SLinus Torvalds 			6,	/* DNS server */
6131da177e4SLinus Torvalds 			12,	/* Host name */
6141da177e4SLinus Torvalds 			15,	/* Domain name */
6151da177e4SLinus Torvalds 			17,	/* Boot path */
6161da177e4SLinus Torvalds 			40,	/* NIS domain name */
6171da177e4SLinus Torvalds 		};
6181da177e4SLinus Torvalds 
6191da177e4SLinus Torvalds 		*e++ = 55;	/* Parameter request list */
6201da177e4SLinus Torvalds 		*e++ = sizeof(ic_req_params);
6211da177e4SLinus Torvalds 		memcpy(e, ic_req_params, sizeof(ic_req_params));
6221da177e4SLinus Torvalds 		e += sizeof(ic_req_params);
6231da177e4SLinus Torvalds 	}
6241da177e4SLinus Torvalds 
6251da177e4SLinus Torvalds 	*e++ = 255;	/* End of the list */
6261da177e4SLinus Torvalds }
6271da177e4SLinus Torvalds 
6281da177e4SLinus Torvalds #endif /* IPCONFIG_DHCP */
6291da177e4SLinus Torvalds 
6301da177e4SLinus Torvalds static void __init ic_bootp_init_ext(u8 *e)
6311da177e4SLinus Torvalds {
6321da177e4SLinus Torvalds 	memcpy(e, ic_bootp_cookie, 4);	/* RFC1048 Magic Cookie */
6331da177e4SLinus Torvalds 	e += 4;
6341da177e4SLinus Torvalds 	*e++ = 1;		/* Subnet mask request */
6351da177e4SLinus Torvalds 	*e++ = 4;
6361da177e4SLinus Torvalds 	e += 4;
6371da177e4SLinus Torvalds 	*e++ = 3;		/* Default gateway request */
6381da177e4SLinus Torvalds 	*e++ = 4;
6391da177e4SLinus Torvalds 	e += 4;
6401da177e4SLinus Torvalds 	*e++ = 5;		/* Name server request */
6411da177e4SLinus Torvalds 	*e++ = 8;
6421da177e4SLinus Torvalds 	e += 8;
6431da177e4SLinus Torvalds 	*e++ = 12;		/* Host name request */
6441da177e4SLinus Torvalds 	*e++ = 32;
6451da177e4SLinus Torvalds 	e += 32;
6461da177e4SLinus Torvalds 	*e++ = 40;		/* NIS Domain name request */
6471da177e4SLinus Torvalds 	*e++ = 32;
6481da177e4SLinus Torvalds 	e += 32;
6491da177e4SLinus Torvalds 	*e++ = 17;		/* Boot path */
6501da177e4SLinus Torvalds 	*e++ = 40;
6511da177e4SLinus Torvalds 	e += 40;
6521da177e4SLinus Torvalds 
6531da177e4SLinus Torvalds 	*e++ = 57;		/* set extension buffer size for reply */
6541da177e4SLinus Torvalds 	*e++ = 2;
6551da177e4SLinus Torvalds 	*e++ = 1;		/* 128+236+8+20+14, see dhcpd sources */
6561da177e4SLinus Torvalds 	*e++ = 150;
6571da177e4SLinus Torvalds 
6581da177e4SLinus Torvalds 	*e++ = 255;		/* End of the list */
6591da177e4SLinus Torvalds }
6601da177e4SLinus Torvalds 
6611da177e4SLinus Torvalds 
6621da177e4SLinus Torvalds /*
6631da177e4SLinus Torvalds  *  Initialize the DHCP/BOOTP mechanism.
6641da177e4SLinus Torvalds  */
6651da177e4SLinus Torvalds static inline void ic_bootp_init(void)
6661da177e4SLinus Torvalds {
6671da177e4SLinus Torvalds 	int i;
6681da177e4SLinus Torvalds 
6691da177e4SLinus Torvalds 	for (i = 0; i < CONF_NAMESERVERS_MAX; i++)
6701da177e4SLinus Torvalds 		ic_nameservers[i] = INADDR_NONE;
6711da177e4SLinus Torvalds 
6721da177e4SLinus Torvalds 	dev_add_pack(&bootp_packet_type);
6731da177e4SLinus Torvalds }
6741da177e4SLinus Torvalds 
6751da177e4SLinus Torvalds 
6761da177e4SLinus Torvalds /*
6771da177e4SLinus Torvalds  *  DHCP/BOOTP cleanup.
6781da177e4SLinus Torvalds  */
6791da177e4SLinus Torvalds static inline void ic_bootp_cleanup(void)
6801da177e4SLinus Torvalds {
6811da177e4SLinus Torvalds 	dev_remove_pack(&bootp_packet_type);
6821da177e4SLinus Torvalds }
6831da177e4SLinus Torvalds 
6841da177e4SLinus Torvalds 
6851da177e4SLinus Torvalds /*
6861da177e4SLinus Torvalds  *  Send DHCP/BOOTP request to single interface.
6871da177e4SLinus Torvalds  */
6881da177e4SLinus Torvalds static void __init ic_bootp_send_if(struct ic_device *d, unsigned long jiffies_diff)
6891da177e4SLinus Torvalds {
6901da177e4SLinus Torvalds 	struct net_device *dev = d->dev;
6911da177e4SLinus Torvalds 	struct sk_buff *skb;
6921da177e4SLinus Torvalds 	struct bootp_pkt *b;
6931da177e4SLinus Torvalds 	int hh_len = LL_RESERVED_SPACE(dev);
6941da177e4SLinus Torvalds 	struct iphdr *h;
6951da177e4SLinus Torvalds 
6961da177e4SLinus Torvalds 	/* Allocate packet */
6971da177e4SLinus Torvalds 	skb = alloc_skb(sizeof(struct bootp_pkt) + hh_len + 15, GFP_KERNEL);
6981da177e4SLinus Torvalds 	if (!skb)
6991da177e4SLinus Torvalds 		return;
7001da177e4SLinus Torvalds 	skb_reserve(skb, hh_len);
7011da177e4SLinus Torvalds 	b = (struct bootp_pkt *) skb_put(skb, sizeof(struct bootp_pkt));
7021da177e4SLinus Torvalds 	memset(b, 0, sizeof(struct bootp_pkt));
7031da177e4SLinus Torvalds 
7041da177e4SLinus Torvalds 	/* Construct IP header */
7051da177e4SLinus Torvalds 	skb->nh.iph = h = &b->iph;
7061da177e4SLinus Torvalds 	h->version = 4;
7071da177e4SLinus Torvalds 	h->ihl = 5;
7081da177e4SLinus Torvalds 	h->tot_len = htons(sizeof(struct bootp_pkt));
7091da177e4SLinus Torvalds 	h->frag_off = htons(IP_DF);
7101da177e4SLinus Torvalds 	h->ttl = 64;
7111da177e4SLinus Torvalds 	h->protocol = IPPROTO_UDP;
7121da177e4SLinus Torvalds 	h->daddr = INADDR_BROADCAST;
7131da177e4SLinus Torvalds 	h->check = ip_fast_csum((unsigned char *) h, h->ihl);
7141da177e4SLinus Torvalds 
7151da177e4SLinus Torvalds 	/* Construct UDP header */
7161da177e4SLinus Torvalds 	b->udph.source = htons(68);
7171da177e4SLinus Torvalds 	b->udph.dest = htons(67);
7181da177e4SLinus Torvalds 	b->udph.len = htons(sizeof(struct bootp_pkt) - sizeof(struct iphdr));
7191da177e4SLinus Torvalds 	/* UDP checksum not calculated -- explicitly allowed in BOOTP RFC */
7201da177e4SLinus Torvalds 
7211da177e4SLinus Torvalds 	/* Construct DHCP/BOOTP header */
7221da177e4SLinus Torvalds 	b->op = BOOTP_REQUEST;
7231da177e4SLinus Torvalds 	if (dev->type < 256) /* check for false types */
7241da177e4SLinus Torvalds 		b->htype = dev->type;
7251da177e4SLinus Torvalds 	else if (dev->type == ARPHRD_IEEE802_TR) /* fix for token ring */
7261da177e4SLinus Torvalds 		b->htype = ARPHRD_IEEE802;
7271da177e4SLinus Torvalds 	else if (dev->type == ARPHRD_FDDI)
7281da177e4SLinus Torvalds 		b->htype = ARPHRD_ETHER;
7291da177e4SLinus Torvalds 	else {
7301da177e4SLinus Torvalds 		printk("Unknown ARP type 0x%04x for device %s\n", dev->type, dev->name);
7311da177e4SLinus Torvalds 		b->htype = dev->type; /* can cause undefined behavior */
7321da177e4SLinus Torvalds 	}
7331da177e4SLinus Torvalds 	b->hlen = dev->addr_len;
7341da177e4SLinus Torvalds 	b->your_ip = INADDR_NONE;
7351da177e4SLinus Torvalds 	b->server_ip = INADDR_NONE;
7361da177e4SLinus Torvalds 	memcpy(b->hw_addr, dev->dev_addr, dev->addr_len);
7371da177e4SLinus Torvalds 	b->secs = htons(jiffies_diff / HZ);
7381da177e4SLinus Torvalds 	b->xid = d->xid;
7391da177e4SLinus Torvalds 
7401da177e4SLinus Torvalds 	/* add DHCP options or BOOTP extensions */
7411da177e4SLinus Torvalds #ifdef IPCONFIG_DHCP
7421da177e4SLinus Torvalds 	if (ic_proto_enabled & IC_USE_DHCP)
7431da177e4SLinus Torvalds 		ic_dhcp_init_options(b->exten);
7441da177e4SLinus Torvalds 	else
7451da177e4SLinus Torvalds #endif
7461da177e4SLinus Torvalds 		ic_bootp_init_ext(b->exten);
7471da177e4SLinus Torvalds 
7481da177e4SLinus Torvalds 	/* Chain packet down the line... */
7491da177e4SLinus Torvalds 	skb->dev = dev;
7501da177e4SLinus Torvalds 	skb->protocol = htons(ETH_P_IP);
7511da177e4SLinus Torvalds 	if ((dev->hard_header &&
7521da177e4SLinus Torvalds 	     dev->hard_header(skb, dev, ntohs(skb->protocol), dev->broadcast, dev->dev_addr, skb->len) < 0) ||
7531da177e4SLinus Torvalds 	    dev_queue_xmit(skb) < 0)
7541da177e4SLinus Torvalds 		printk("E");
7551da177e4SLinus Torvalds }
7561da177e4SLinus Torvalds 
7571da177e4SLinus Torvalds 
7581da177e4SLinus Torvalds /*
7591da177e4SLinus Torvalds  *  Copy BOOTP-supplied string if not already set.
7601da177e4SLinus Torvalds  */
7611da177e4SLinus Torvalds static int __init ic_bootp_string(char *dest, char *src, int len, int max)
7621da177e4SLinus Torvalds {
7631da177e4SLinus Torvalds 	if (!len)
7641da177e4SLinus Torvalds 		return 0;
7651da177e4SLinus Torvalds 	if (len > max-1)
7661da177e4SLinus Torvalds 		len = max-1;
7671da177e4SLinus Torvalds 	memcpy(dest, src, len);
7681da177e4SLinus Torvalds 	dest[len] = '\0';
7691da177e4SLinus Torvalds 	return 1;
7701da177e4SLinus Torvalds }
7711da177e4SLinus Torvalds 
7721da177e4SLinus Torvalds 
7731da177e4SLinus Torvalds /*
7741da177e4SLinus Torvalds  *  Process BOOTP extensions.
7751da177e4SLinus Torvalds  */
7761da177e4SLinus Torvalds static void __init ic_do_bootp_ext(u8 *ext)
7771da177e4SLinus Torvalds {
7781da177e4SLinus Torvalds        u8 servers;
7791da177e4SLinus Torvalds        int i;
7801da177e4SLinus Torvalds 
7811da177e4SLinus Torvalds #ifdef IPCONFIG_DEBUG
7821da177e4SLinus Torvalds 	u8 *c;
7831da177e4SLinus Torvalds 
7841da177e4SLinus Torvalds 	printk("DHCP/BOOTP: Got extension %d:",*ext);
7851da177e4SLinus Torvalds 	for(c=ext+2; c<ext+2+ext[1]; c++)
7861da177e4SLinus Torvalds 		printk(" %02x", *c);
7871da177e4SLinus Torvalds 	printk("\n");
7881da177e4SLinus Torvalds #endif
7891da177e4SLinus Torvalds 
7901da177e4SLinus Torvalds 	switch (*ext++) {
7911da177e4SLinus Torvalds 		case 1:		/* Subnet mask */
7921da177e4SLinus Torvalds 			if (ic_netmask == INADDR_NONE)
7931da177e4SLinus Torvalds 				memcpy(&ic_netmask, ext+1, 4);
7941da177e4SLinus Torvalds 			break;
7951da177e4SLinus Torvalds 		case 3:		/* Default gateway */
7961da177e4SLinus Torvalds 			if (ic_gateway == INADDR_NONE)
7971da177e4SLinus Torvalds 				memcpy(&ic_gateway, ext+1, 4);
7981da177e4SLinus Torvalds 			break;
7991da177e4SLinus Torvalds 		case 6:		/* DNS server */
8001da177e4SLinus Torvalds 			servers= *ext/4;
8011da177e4SLinus Torvalds 			if (servers > CONF_NAMESERVERS_MAX)
8021da177e4SLinus Torvalds 				servers = CONF_NAMESERVERS_MAX;
8031da177e4SLinus Torvalds 			for (i = 0; i < servers; i++) {
8041da177e4SLinus Torvalds 				if (ic_nameservers[i] == INADDR_NONE)
8051da177e4SLinus Torvalds 					memcpy(&ic_nameservers[i], ext+1+4*i, 4);
8061da177e4SLinus Torvalds 			}
8071da177e4SLinus Torvalds 			break;
8081da177e4SLinus Torvalds 		case 12:	/* Host name */
8091da177e4SLinus Torvalds 			ic_bootp_string(system_utsname.nodename, ext+1, *ext, __NEW_UTS_LEN);
8101da177e4SLinus Torvalds 			ic_host_name_set = 1;
8111da177e4SLinus Torvalds 			break;
8121da177e4SLinus Torvalds 		case 15:	/* Domain name (DNS) */
8131da177e4SLinus Torvalds 			ic_bootp_string(ic_domain, ext+1, *ext, sizeof(ic_domain));
8141da177e4SLinus Torvalds 			break;
8151da177e4SLinus Torvalds 		case 17:	/* Root path */
8161da177e4SLinus Torvalds 			if (!root_server_path[0])
8171da177e4SLinus Torvalds 				ic_bootp_string(root_server_path, ext+1, *ext, sizeof(root_server_path));
8181da177e4SLinus Torvalds 			break;
8191da177e4SLinus Torvalds 		case 40:	/* NIS Domain name (_not_ DNS) */
8201da177e4SLinus Torvalds 			ic_bootp_string(system_utsname.domainname, ext+1, *ext, __NEW_UTS_LEN);
8211da177e4SLinus Torvalds 			break;
8221da177e4SLinus Torvalds 	}
8231da177e4SLinus Torvalds }
8241da177e4SLinus Torvalds 
8251da177e4SLinus Torvalds 
8261da177e4SLinus Torvalds /*
8271da177e4SLinus Torvalds  *  Receive BOOTP reply.
8281da177e4SLinus Torvalds  */
829f2ccd8faSDavid S. Miller static int __init ic_bootp_recv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev)
8301da177e4SLinus Torvalds {
8311da177e4SLinus Torvalds 	struct bootp_pkt *b;
8321da177e4SLinus Torvalds 	struct iphdr *h;
8331da177e4SLinus Torvalds 	struct ic_device *d;
8341da177e4SLinus Torvalds 	int len, ext_len;
8351da177e4SLinus Torvalds 
8361da177e4SLinus Torvalds 	/* Perform verifications before taking the lock.  */
8371da177e4SLinus Torvalds 	if (skb->pkt_type == PACKET_OTHERHOST)
8381da177e4SLinus Torvalds 		goto drop;
8391da177e4SLinus Torvalds 
8401da177e4SLinus Torvalds 	if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL)
8411da177e4SLinus Torvalds 		return NET_RX_DROP;
8421da177e4SLinus Torvalds 
8431da177e4SLinus Torvalds 	if (!pskb_may_pull(skb,
8441da177e4SLinus Torvalds 			   sizeof(struct iphdr) +
8451da177e4SLinus Torvalds 			   sizeof(struct udphdr)))
8461da177e4SLinus Torvalds 		goto drop;
8471da177e4SLinus Torvalds 
8481da177e4SLinus Torvalds 	b = (struct bootp_pkt *) skb->nh.iph;
8491da177e4SLinus Torvalds 	h = &b->iph;
8501da177e4SLinus Torvalds 
8511da177e4SLinus Torvalds 	if (h->ihl != 5 || h->version != 4 || h->protocol != IPPROTO_UDP)
8521da177e4SLinus Torvalds 		goto drop;
8531da177e4SLinus Torvalds 
8541da177e4SLinus Torvalds 	/* Fragments are not supported */
8551da177e4SLinus Torvalds 	if (h->frag_off & htons(IP_OFFSET | IP_MF)) {
8561da177e4SLinus Torvalds 		if (net_ratelimit())
8571da177e4SLinus Torvalds 			printk(KERN_ERR "DHCP/BOOTP: Ignoring fragmented "
8581da177e4SLinus Torvalds 			       "reply.\n");
8591da177e4SLinus Torvalds 		goto drop;
8601da177e4SLinus Torvalds 	}
8611da177e4SLinus Torvalds 
8621da177e4SLinus Torvalds 	if (skb->len < ntohs(h->tot_len))
8631da177e4SLinus Torvalds 		goto drop;
8641da177e4SLinus Torvalds 
8651da177e4SLinus Torvalds 	if (ip_fast_csum((char *) h, h->ihl))
8661da177e4SLinus Torvalds 		goto drop;
8671da177e4SLinus Torvalds 
8681da177e4SLinus Torvalds 	if (b->udph.source != htons(67) || b->udph.dest != htons(68))
8691da177e4SLinus Torvalds 		goto drop;
8701da177e4SLinus Torvalds 
8711da177e4SLinus Torvalds 	if (ntohs(h->tot_len) < ntohs(b->udph.len) + sizeof(struct iphdr))
8721da177e4SLinus Torvalds 		goto drop;
8731da177e4SLinus Torvalds 
8741da177e4SLinus Torvalds 	len = ntohs(b->udph.len) - sizeof(struct udphdr);
8751da177e4SLinus Torvalds 	ext_len = len - (sizeof(*b) -
8761da177e4SLinus Torvalds 			 sizeof(struct iphdr) -
8771da177e4SLinus Torvalds 			 sizeof(struct udphdr) -
8781da177e4SLinus Torvalds 			 sizeof(b->exten));
8791da177e4SLinus Torvalds 	if (ext_len < 0)
8801da177e4SLinus Torvalds 		goto drop;
8811da177e4SLinus Torvalds 
8821da177e4SLinus Torvalds 	/* Ok the front looks good, make sure we can get at the rest.  */
8831da177e4SLinus Torvalds 	if (!pskb_may_pull(skb, skb->len))
8841da177e4SLinus Torvalds 		goto drop;
8851da177e4SLinus Torvalds 
8861da177e4SLinus Torvalds 	b = (struct bootp_pkt *) skb->nh.iph;
8871da177e4SLinus Torvalds 	h = &b->iph;
8881da177e4SLinus Torvalds 
8891da177e4SLinus Torvalds 	/* One reply at a time, please. */
8901da177e4SLinus Torvalds 	spin_lock(&ic_recv_lock);
8911da177e4SLinus Torvalds 
8921da177e4SLinus Torvalds 	/* If we already have a reply, just drop the packet */
8931da177e4SLinus Torvalds 	if (ic_got_reply)
8941da177e4SLinus Torvalds 		goto drop_unlock;
8951da177e4SLinus Torvalds 
8961da177e4SLinus Torvalds 	/* Find the ic_device that the packet arrived on */
8971da177e4SLinus Torvalds 	d = ic_first_dev;
8981da177e4SLinus Torvalds 	while (d && d->dev != dev)
8991da177e4SLinus Torvalds 		d = d->next;
9001da177e4SLinus Torvalds 	if (!d)
9011da177e4SLinus Torvalds 		goto drop_unlock;  /* should never happen */
9021da177e4SLinus Torvalds 
9031da177e4SLinus Torvalds 	/* Is it a reply to our BOOTP request? */
9041da177e4SLinus Torvalds 	if (b->op != BOOTP_REPLY ||
9051da177e4SLinus Torvalds 	    b->xid != d->xid) {
9061da177e4SLinus Torvalds 		if (net_ratelimit())
9071da177e4SLinus Torvalds 			printk(KERN_ERR "DHCP/BOOTP: Reply not for us, "
9081da177e4SLinus Torvalds 			       "op[%x] xid[%x]\n",
9091da177e4SLinus Torvalds 			       b->op, b->xid);
9101da177e4SLinus Torvalds 		goto drop_unlock;
9111da177e4SLinus Torvalds 	}
9121da177e4SLinus Torvalds 
9131da177e4SLinus Torvalds 	/* Parse extensions */
9141da177e4SLinus Torvalds 	if (ext_len >= 4 &&
9151da177e4SLinus Torvalds 	    !memcmp(b->exten, ic_bootp_cookie, 4)) { /* Check magic cookie */
9161da177e4SLinus Torvalds                 u8 *end = (u8 *) b + ntohs(b->iph.tot_len);
9171da177e4SLinus Torvalds 		u8 *ext;
9181da177e4SLinus Torvalds 
9191da177e4SLinus Torvalds #ifdef IPCONFIG_DHCP
9201da177e4SLinus Torvalds 		if (ic_proto_enabled & IC_USE_DHCP) {
9211da177e4SLinus Torvalds 			u32 server_id = INADDR_NONE;
9221da177e4SLinus Torvalds 			int mt = 0;
9231da177e4SLinus Torvalds 
9241da177e4SLinus Torvalds 			ext = &b->exten[4];
9251da177e4SLinus Torvalds 			while (ext < end && *ext != 0xff) {
9261da177e4SLinus Torvalds 				u8 *opt = ext++;
9271da177e4SLinus Torvalds 				if (*opt == 0)	/* Padding */
9281da177e4SLinus Torvalds 					continue;
9291da177e4SLinus Torvalds 				ext += *ext + 1;
9301da177e4SLinus Torvalds 				if (ext >= end)
9311da177e4SLinus Torvalds 					break;
9321da177e4SLinus Torvalds 				switch (*opt) {
9331da177e4SLinus Torvalds 				case 53:	/* Message type */
9341da177e4SLinus Torvalds 					if (opt[1])
9351da177e4SLinus Torvalds 						mt = opt[2];
9361da177e4SLinus Torvalds 					break;
9371da177e4SLinus Torvalds 				case 54:	/* Server ID (IP address) */
9381da177e4SLinus Torvalds 					if (opt[1] >= 4)
9391da177e4SLinus Torvalds 						memcpy(&server_id, opt + 2, 4);
9401da177e4SLinus Torvalds 					break;
9411da177e4SLinus Torvalds 				};
9421da177e4SLinus Torvalds 			}
9431da177e4SLinus Torvalds 
9441da177e4SLinus Torvalds #ifdef IPCONFIG_DEBUG
9451da177e4SLinus Torvalds 			printk("DHCP: Got message type %d\n", mt);
9461da177e4SLinus Torvalds #endif
9471da177e4SLinus Torvalds 
9481da177e4SLinus Torvalds 			switch (mt) {
9491da177e4SLinus Torvalds 			case DHCPOFFER:
9501da177e4SLinus Torvalds 				/* While in the process of accepting one offer,
9511da177e4SLinus Torvalds 				 * ignore all others.
9521da177e4SLinus Torvalds 				 */
9531da177e4SLinus Torvalds 				if (ic_myaddr != INADDR_NONE)
9541da177e4SLinus Torvalds 					goto drop_unlock;
9551da177e4SLinus Torvalds 
9561da177e4SLinus Torvalds 				/* Let's accept that offer. */
9571da177e4SLinus Torvalds 				ic_myaddr = b->your_ip;
9581da177e4SLinus Torvalds 				ic_servaddr = server_id;
9591da177e4SLinus Torvalds #ifdef IPCONFIG_DEBUG
9601da177e4SLinus Torvalds 				printk("DHCP: Offered address %u.%u.%u.%u",
9611da177e4SLinus Torvalds 				       NIPQUAD(ic_myaddr));
9621da177e4SLinus Torvalds 				printk(" by server %u.%u.%u.%u\n",
9631da177e4SLinus Torvalds 				       NIPQUAD(ic_servaddr));
9641da177e4SLinus Torvalds #endif
9651da177e4SLinus Torvalds 				/* The DHCP indicated server address takes
9661da177e4SLinus Torvalds 				 * precedence over the bootp header one if
9671da177e4SLinus Torvalds 				 * they are different.
9681da177e4SLinus Torvalds 				 */
9691da177e4SLinus Torvalds 				if ((server_id != INADDR_NONE) &&
9701da177e4SLinus Torvalds 				    (b->server_ip != server_id))
9711da177e4SLinus Torvalds 					b->server_ip = ic_servaddr;
9721da177e4SLinus Torvalds 				break;
9731da177e4SLinus Torvalds 
9741da177e4SLinus Torvalds 			case DHCPACK:
9751da177e4SLinus Torvalds 				if (memcmp(dev->dev_addr, b->hw_addr, dev->addr_len) != 0)
9761da177e4SLinus Torvalds 					goto drop_unlock;
9771da177e4SLinus Torvalds 
9781da177e4SLinus Torvalds 				/* Yeah! */
9791da177e4SLinus Torvalds 				break;
9801da177e4SLinus Torvalds 
9811da177e4SLinus Torvalds 			default:
9821da177e4SLinus Torvalds 				/* Urque.  Forget it*/
9831da177e4SLinus Torvalds 				ic_myaddr = INADDR_NONE;
9841da177e4SLinus Torvalds 				ic_servaddr = INADDR_NONE;
9851da177e4SLinus Torvalds 				goto drop_unlock;
9861da177e4SLinus Torvalds 			};
9871da177e4SLinus Torvalds 
9881da177e4SLinus Torvalds 			ic_dhcp_msgtype = mt;
9891da177e4SLinus Torvalds 
9901da177e4SLinus Torvalds 		}
9911da177e4SLinus Torvalds #endif /* IPCONFIG_DHCP */
9921da177e4SLinus Torvalds 
9931da177e4SLinus Torvalds 		ext = &b->exten[4];
9941da177e4SLinus Torvalds 		while (ext < end && *ext != 0xff) {
9951da177e4SLinus Torvalds 			u8 *opt = ext++;
9961da177e4SLinus Torvalds 			if (*opt == 0)	/* Padding */
9971da177e4SLinus Torvalds 				continue;
9981da177e4SLinus Torvalds 			ext += *ext + 1;
9991da177e4SLinus Torvalds 			if (ext < end)
10001da177e4SLinus Torvalds 				ic_do_bootp_ext(opt);
10011da177e4SLinus Torvalds 		}
10021da177e4SLinus Torvalds 	}
10031da177e4SLinus Torvalds 
10041da177e4SLinus Torvalds 	/* We have a winner! */
10051da177e4SLinus Torvalds 	ic_dev = dev;
10061da177e4SLinus Torvalds 	ic_myaddr = b->your_ip;
10071da177e4SLinus Torvalds 	ic_servaddr = b->server_ip;
10081da177e4SLinus Torvalds 	if (ic_gateway == INADDR_NONE && b->relay_ip)
10091da177e4SLinus Torvalds 		ic_gateway = b->relay_ip;
10101da177e4SLinus Torvalds 	if (ic_nameservers[0] == INADDR_NONE)
10111da177e4SLinus Torvalds 		ic_nameservers[0] = ic_servaddr;
10121da177e4SLinus Torvalds 	ic_got_reply = IC_BOOTP;
10131da177e4SLinus Torvalds 
10141da177e4SLinus Torvalds drop_unlock:
10151da177e4SLinus Torvalds 	/* Show's over.  Nothing to see here.  */
10161da177e4SLinus Torvalds 	spin_unlock(&ic_recv_lock);
10171da177e4SLinus Torvalds 
10181da177e4SLinus Torvalds drop:
10191da177e4SLinus Torvalds 	/* Throw the packet out. */
10201da177e4SLinus Torvalds 	kfree_skb(skb);
10211da177e4SLinus Torvalds 
10221da177e4SLinus Torvalds 	return 0;
10231da177e4SLinus Torvalds }
10241da177e4SLinus Torvalds 
10251da177e4SLinus Torvalds 
10261da177e4SLinus Torvalds #endif
10271da177e4SLinus Torvalds 
10281da177e4SLinus Torvalds 
10291da177e4SLinus Torvalds /*
10301da177e4SLinus Torvalds  *	Dynamic IP configuration -- DHCP, BOOTP, RARP.
10311da177e4SLinus Torvalds  */
10321da177e4SLinus Torvalds 
10331da177e4SLinus Torvalds #ifdef IPCONFIG_DYNAMIC
10341da177e4SLinus Torvalds 
10351da177e4SLinus Torvalds static int __init ic_dynamic(void)
10361da177e4SLinus Torvalds {
10371da177e4SLinus Torvalds 	int retries;
10381da177e4SLinus Torvalds 	struct ic_device *d;
10391da177e4SLinus Torvalds 	unsigned long start_jiffies, timeout, jiff;
10401da177e4SLinus Torvalds 	int do_bootp = ic_proto_have_if & IC_BOOTP;
10411da177e4SLinus Torvalds 	int do_rarp = ic_proto_have_if & IC_RARP;
10421da177e4SLinus Torvalds 
10431da177e4SLinus Torvalds 	/*
10441da177e4SLinus Torvalds 	 * If none of DHCP/BOOTP/RARP was selected, return with an error.
10451da177e4SLinus Torvalds 	 * This routine gets only called when some pieces of information
10461da177e4SLinus Torvalds 	 * are missing, and without DHCP/BOOTP/RARP we are unable to get it.
10471da177e4SLinus Torvalds 	 */
10481da177e4SLinus Torvalds 	if (!ic_proto_enabled) {
10491da177e4SLinus Torvalds 		printk(KERN_ERR "IP-Config: Incomplete network configuration information.\n");
10501da177e4SLinus Torvalds 		return -1;
10511da177e4SLinus Torvalds 	}
10521da177e4SLinus Torvalds 
10531da177e4SLinus Torvalds #ifdef IPCONFIG_BOOTP
10541da177e4SLinus Torvalds 	if ((ic_proto_enabled ^ ic_proto_have_if) & IC_BOOTP)
10551da177e4SLinus Torvalds 		printk(KERN_ERR "DHCP/BOOTP: No suitable device found.\n");
10561da177e4SLinus Torvalds #endif
10571da177e4SLinus Torvalds #ifdef IPCONFIG_RARP
10581da177e4SLinus Torvalds 	if ((ic_proto_enabled ^ ic_proto_have_if) & IC_RARP)
10591da177e4SLinus Torvalds 		printk(KERN_ERR "RARP: No suitable device found.\n");
10601da177e4SLinus Torvalds #endif
10611da177e4SLinus Torvalds 
10621da177e4SLinus Torvalds 	if (!ic_proto_have_if)
10631da177e4SLinus Torvalds 		/* Error message already printed */
10641da177e4SLinus Torvalds 		return -1;
10651da177e4SLinus Torvalds 
10661da177e4SLinus Torvalds 	/*
10671da177e4SLinus Torvalds 	 * Setup protocols
10681da177e4SLinus Torvalds 	 */
10691da177e4SLinus Torvalds #ifdef IPCONFIG_BOOTP
10701da177e4SLinus Torvalds 	if (do_bootp)
10711da177e4SLinus Torvalds 		ic_bootp_init();
10721da177e4SLinus Torvalds #endif
10731da177e4SLinus Torvalds #ifdef IPCONFIG_RARP
10741da177e4SLinus Torvalds 	if (do_rarp)
10751da177e4SLinus Torvalds 		ic_rarp_init();
10761da177e4SLinus Torvalds #endif
10771da177e4SLinus Torvalds 
10781da177e4SLinus Torvalds 	/*
10791da177e4SLinus Torvalds 	 * Send requests and wait, until we get an answer. This loop
10801da177e4SLinus Torvalds 	 * seems to be a terrible waste of CPU time, but actually there is
10811da177e4SLinus Torvalds 	 * only one process running at all, so we don't need to use any
10821da177e4SLinus Torvalds 	 * scheduler functions.
10831da177e4SLinus Torvalds 	 * [Actually we could now, but the nothing else running note still
10841da177e4SLinus Torvalds 	 *  applies.. - AC]
10851da177e4SLinus Torvalds 	 */
10861da177e4SLinus Torvalds 	printk(KERN_NOTICE "Sending %s%s%s requests .",
10871da177e4SLinus Torvalds 	       do_bootp
10881da177e4SLinus Torvalds 		? ((ic_proto_enabled & IC_USE_DHCP) ? "DHCP" : "BOOTP") : "",
10891da177e4SLinus Torvalds 	       (do_bootp && do_rarp) ? " and " : "",
10901da177e4SLinus Torvalds 	       do_rarp ? "RARP" : "");
10911da177e4SLinus Torvalds 
10921da177e4SLinus Torvalds 	start_jiffies = jiffies;
10931da177e4SLinus Torvalds 	d = ic_first_dev;
10941da177e4SLinus Torvalds 	retries = CONF_SEND_RETRIES;
10951da177e4SLinus Torvalds 	get_random_bytes(&timeout, sizeof(timeout));
10961da177e4SLinus Torvalds 	timeout = CONF_BASE_TIMEOUT + (timeout % (unsigned) CONF_TIMEOUT_RANDOM);
10971da177e4SLinus Torvalds 	for(;;) {
10981da177e4SLinus Torvalds #ifdef IPCONFIG_BOOTP
10991da177e4SLinus Torvalds 		if (do_bootp && (d->able & IC_BOOTP))
11001da177e4SLinus Torvalds 			ic_bootp_send_if(d, jiffies - start_jiffies);
11011da177e4SLinus Torvalds #endif
11021da177e4SLinus Torvalds #ifdef IPCONFIG_RARP
11031da177e4SLinus Torvalds 		if (do_rarp && (d->able & IC_RARP))
11041da177e4SLinus Torvalds 			ic_rarp_send_if(d);
11051da177e4SLinus Torvalds #endif
11061da177e4SLinus Torvalds 
11071da177e4SLinus Torvalds 		jiff = jiffies + (d->next ? CONF_INTER_TIMEOUT : timeout);
1108121caf57SNishanth Aravamudan 		while (time_before(jiffies, jiff) && !ic_got_reply)
1109121caf57SNishanth Aravamudan 			schedule_timeout_uninterruptible(1);
11101da177e4SLinus Torvalds #ifdef IPCONFIG_DHCP
11111da177e4SLinus Torvalds 		/* DHCP isn't done until we get a DHCPACK. */
11121da177e4SLinus Torvalds 		if ((ic_got_reply & IC_BOOTP)
11131da177e4SLinus Torvalds 		    && (ic_proto_enabled & IC_USE_DHCP)
11141da177e4SLinus Torvalds 		    && ic_dhcp_msgtype != DHCPACK)
11151da177e4SLinus Torvalds 		{
11161da177e4SLinus Torvalds 			ic_got_reply = 0;
11171da177e4SLinus Torvalds 			printk(",");
11181da177e4SLinus Torvalds 			continue;
11191da177e4SLinus Torvalds 		}
11201da177e4SLinus Torvalds #endif /* IPCONFIG_DHCP */
11211da177e4SLinus Torvalds 
11221da177e4SLinus Torvalds 		if (ic_got_reply) {
11231da177e4SLinus Torvalds 			printk(" OK\n");
11241da177e4SLinus Torvalds 			break;
11251da177e4SLinus Torvalds 		}
11261da177e4SLinus Torvalds 
11271da177e4SLinus Torvalds 		if ((d = d->next))
11281da177e4SLinus Torvalds 			continue;
11291da177e4SLinus Torvalds 
11301da177e4SLinus Torvalds 		if (! --retries) {
11311da177e4SLinus Torvalds 			printk(" timed out!\n");
11321da177e4SLinus Torvalds 			break;
11331da177e4SLinus Torvalds 		}
11341da177e4SLinus Torvalds 
11351da177e4SLinus Torvalds 		d = ic_first_dev;
11361da177e4SLinus Torvalds 
11371da177e4SLinus Torvalds 		timeout = timeout CONF_TIMEOUT_MULT;
11381da177e4SLinus Torvalds 		if (timeout > CONF_TIMEOUT_MAX)
11391da177e4SLinus Torvalds 			timeout = CONF_TIMEOUT_MAX;
11401da177e4SLinus Torvalds 
11411da177e4SLinus Torvalds 		printk(".");
11421da177e4SLinus Torvalds 	}
11431da177e4SLinus Torvalds 
11441da177e4SLinus Torvalds #ifdef IPCONFIG_BOOTP
11451da177e4SLinus Torvalds 	if (do_bootp)
11461da177e4SLinus Torvalds 		ic_bootp_cleanup();
11471da177e4SLinus Torvalds #endif
11481da177e4SLinus Torvalds #ifdef IPCONFIG_RARP
11491da177e4SLinus Torvalds 	if (do_rarp)
11501da177e4SLinus Torvalds 		ic_rarp_cleanup();
11511da177e4SLinus Torvalds #endif
11521da177e4SLinus Torvalds 
11537a1af5d7SMaxime Bizon 	if (!ic_got_reply) {
11547a1af5d7SMaxime Bizon 		ic_myaddr = INADDR_NONE;
11551da177e4SLinus Torvalds 		return -1;
11567a1af5d7SMaxime Bizon 	}
11571da177e4SLinus Torvalds 
11581da177e4SLinus Torvalds 	printk("IP-Config: Got %s answer from %u.%u.%u.%u, ",
11591da177e4SLinus Torvalds 		((ic_got_reply & IC_RARP) ? "RARP"
11601da177e4SLinus Torvalds 		 : (ic_proto_enabled & IC_USE_DHCP) ? "DHCP" : "BOOTP"),
11611da177e4SLinus Torvalds 		NIPQUAD(ic_servaddr));
11621da177e4SLinus Torvalds 	printk("my address is %u.%u.%u.%u\n", NIPQUAD(ic_myaddr));
11631da177e4SLinus Torvalds 
11641da177e4SLinus Torvalds 	return 0;
11651da177e4SLinus Torvalds }
11661da177e4SLinus Torvalds 
11671da177e4SLinus Torvalds #endif /* IPCONFIG_DYNAMIC */
11681da177e4SLinus Torvalds 
11691da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
11701da177e4SLinus Torvalds 
11711da177e4SLinus Torvalds static int pnp_seq_show(struct seq_file *seq, void *v)
11721da177e4SLinus Torvalds {
11731da177e4SLinus Torvalds 	int i;
11741da177e4SLinus Torvalds 
11751da177e4SLinus Torvalds 	if (ic_proto_used & IC_PROTO)
11761da177e4SLinus Torvalds 		seq_printf(seq, "#PROTO: %s\n",
11771da177e4SLinus Torvalds 			   (ic_proto_used & IC_RARP) ? "RARP"
11781da177e4SLinus Torvalds 			   : (ic_proto_used & IC_USE_DHCP) ? "DHCP" : "BOOTP");
11791da177e4SLinus Torvalds 	else
11801da177e4SLinus Torvalds 		seq_puts(seq, "#MANUAL\n");
11811da177e4SLinus Torvalds 
11821da177e4SLinus Torvalds 	if (ic_domain[0])
11831da177e4SLinus Torvalds 		seq_printf(seq,
11841da177e4SLinus Torvalds 			   "domain %s\n", ic_domain);
11851da177e4SLinus Torvalds 	for (i = 0; i < CONF_NAMESERVERS_MAX; i++) {
11861da177e4SLinus Torvalds 		if (ic_nameservers[i] != INADDR_NONE)
11871da177e4SLinus Torvalds 			seq_printf(seq,
11881da177e4SLinus Torvalds 				   "nameserver %u.%u.%u.%u\n",
11891da177e4SLinus Torvalds 				   NIPQUAD(ic_nameservers[i]));
11901da177e4SLinus Torvalds 	}
11911da177e4SLinus Torvalds 	if (ic_servaddr != INADDR_NONE)
11921da177e4SLinus Torvalds 		seq_printf(seq,
11931da177e4SLinus Torvalds 			   "bootserver %u.%u.%u.%u\n",
11941da177e4SLinus Torvalds 			   NIPQUAD(ic_servaddr));
11951da177e4SLinus Torvalds 	return 0;
11961da177e4SLinus Torvalds }
11971da177e4SLinus Torvalds 
11981da177e4SLinus Torvalds static int pnp_seq_open(struct inode *indoe, struct file *file)
11991da177e4SLinus Torvalds {
12001da177e4SLinus Torvalds 	return single_open(file, pnp_seq_show, NULL);
12011da177e4SLinus Torvalds }
12021da177e4SLinus Torvalds 
12031da177e4SLinus Torvalds static struct file_operations pnp_seq_fops = {
12041da177e4SLinus Torvalds 	.owner		= THIS_MODULE,
12051da177e4SLinus Torvalds 	.open		= pnp_seq_open,
12061da177e4SLinus Torvalds 	.read		= seq_read,
12071da177e4SLinus Torvalds 	.llseek		= seq_lseek,
12081da177e4SLinus Torvalds 	.release	= single_release,
12091da177e4SLinus Torvalds };
12101da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */
12111da177e4SLinus Torvalds 
12121da177e4SLinus Torvalds /*
12131da177e4SLinus Torvalds  *  Extract IP address from the parameter string if needed. Note that we
12141da177e4SLinus Torvalds  *  need to have root_server_addr set _before_ IPConfig gets called as it
12151da177e4SLinus Torvalds  *  can override it.
12161da177e4SLinus Torvalds  */
12171da177e4SLinus Torvalds u32 __init root_nfs_parse_addr(char *name)
12181da177e4SLinus Torvalds {
12191da177e4SLinus Torvalds 	u32 addr;
12201da177e4SLinus Torvalds 	int octets = 0;
12211da177e4SLinus Torvalds 	char *cp, *cq;
12221da177e4SLinus Torvalds 
12231da177e4SLinus Torvalds 	cp = cq = name;
12241da177e4SLinus Torvalds 	while (octets < 4) {
12251da177e4SLinus Torvalds 		while (*cp >= '0' && *cp <= '9')
12261da177e4SLinus Torvalds 			cp++;
12271da177e4SLinus Torvalds 		if (cp == cq || cp - cq > 3)
12281da177e4SLinus Torvalds 			break;
12291da177e4SLinus Torvalds 		if (*cp == '.' || octets == 3)
12301da177e4SLinus Torvalds 			octets++;
12311da177e4SLinus Torvalds 		if (octets < 4)
12321da177e4SLinus Torvalds 			cp++;
12331da177e4SLinus Torvalds 		cq = cp;
12341da177e4SLinus Torvalds 	}
12351da177e4SLinus Torvalds 	if (octets == 4 && (*cp == ':' || *cp == '\0')) {
12361da177e4SLinus Torvalds 		if (*cp == ':')
12371da177e4SLinus Torvalds 			*cp++ = '\0';
12381da177e4SLinus Torvalds 		addr = in_aton(name);
12391da177e4SLinus Torvalds 		memmove(name, cp, strlen(cp) + 1);
12401da177e4SLinus Torvalds 	} else
12411da177e4SLinus Torvalds 		addr = INADDR_NONE;
12421da177e4SLinus Torvalds 
12431da177e4SLinus Torvalds 	return addr;
12441da177e4SLinus Torvalds }
12451da177e4SLinus Torvalds 
12461da177e4SLinus Torvalds /*
12471da177e4SLinus Torvalds  *	IP Autoconfig dispatcher.
12481da177e4SLinus Torvalds  */
12491da177e4SLinus Torvalds 
12501da177e4SLinus Torvalds static int __init ip_auto_config(void)
12511da177e4SLinus Torvalds {
12521da177e4SLinus Torvalds 	u32 addr;
12531da177e4SLinus Torvalds 
12541da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
12551da177e4SLinus Torvalds 	proc_net_fops_create("pnp", S_IRUGO, &pnp_seq_fops);
12561da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */
12571da177e4SLinus Torvalds 
12581da177e4SLinus Torvalds 	if (!ic_enable)
12591da177e4SLinus Torvalds 		return 0;
12601da177e4SLinus Torvalds 
12611da177e4SLinus Torvalds 	DBG(("IP-Config: Entered.\n"));
12621da177e4SLinus Torvalds #ifdef IPCONFIG_DYNAMIC
12631da177e4SLinus Torvalds  try_try_again:
12641da177e4SLinus Torvalds #endif
12651da177e4SLinus Torvalds 	/* Give hardware a chance to settle */
12661da177e4SLinus Torvalds 	msleep(CONF_PRE_OPEN);
12671da177e4SLinus Torvalds 
12681da177e4SLinus Torvalds 	/* Setup all network devices */
12691da177e4SLinus Torvalds 	if (ic_open_devs() < 0)
12701da177e4SLinus Torvalds 		return -1;
12711da177e4SLinus Torvalds 
12721da177e4SLinus Torvalds 	/* Give drivers a chance to settle */
12731da177e4SLinus Torvalds 	ssleep(CONF_POST_OPEN);
12741da177e4SLinus Torvalds 
12751da177e4SLinus Torvalds 	/*
12761da177e4SLinus Torvalds 	 * If the config information is insufficient (e.g., our IP address or
12771da177e4SLinus Torvalds 	 * IP address of the boot server is missing or we have multiple network
12781da177e4SLinus Torvalds 	 * interfaces and no default was set), use BOOTP or RARP to get the
12791da177e4SLinus Torvalds 	 * missing values.
12801da177e4SLinus Torvalds 	 */
12811da177e4SLinus Torvalds 	if (ic_myaddr == INADDR_NONE ||
12821da177e4SLinus Torvalds #ifdef CONFIG_ROOT_NFS
12831da177e4SLinus Torvalds 	    (MAJOR(ROOT_DEV) == UNNAMED_MAJOR
12841da177e4SLinus Torvalds 	     && root_server_addr == INADDR_NONE
12851da177e4SLinus Torvalds 	     && ic_servaddr == INADDR_NONE) ||
12861da177e4SLinus Torvalds #endif
12871da177e4SLinus Torvalds 	    ic_first_dev->next) {
12881da177e4SLinus Torvalds #ifdef IPCONFIG_DYNAMIC
12891da177e4SLinus Torvalds 
12901da177e4SLinus Torvalds 		int retries = CONF_OPEN_RETRIES;
12911da177e4SLinus Torvalds 
12921da177e4SLinus Torvalds 		if (ic_dynamic() < 0) {
12931da177e4SLinus Torvalds 			ic_close_devs();
12941da177e4SLinus Torvalds 
12951da177e4SLinus Torvalds 			/*
12961da177e4SLinus Torvalds 			 * I don't know why, but sometimes the
12971da177e4SLinus Torvalds 			 * eepro100 driver (at least) gets upset and
12981da177e4SLinus Torvalds 			 * doesn't work the first time it's opened.
12991da177e4SLinus Torvalds 			 * But then if you close it and reopen it, it
13001da177e4SLinus Torvalds 			 * works just fine.  So we need to try that at
13011da177e4SLinus Torvalds 			 * least once before giving up.
13021da177e4SLinus Torvalds 			 *
13031da177e4SLinus Torvalds 			 * Also, if the root will be NFS-mounted, we
13041da177e4SLinus Torvalds 			 * have nowhere to go if DHCP fails.  So we
13051da177e4SLinus Torvalds 			 * just have to keep trying forever.
13061da177e4SLinus Torvalds 			 *
13071da177e4SLinus Torvalds 			 * 				-- Chip
13081da177e4SLinus Torvalds 			 */
13091da177e4SLinus Torvalds #ifdef CONFIG_ROOT_NFS
13101da177e4SLinus Torvalds 			if (ROOT_DEV ==  Root_NFS) {
13111da177e4SLinus Torvalds 				printk(KERN_ERR
13121da177e4SLinus Torvalds 					"IP-Config: Retrying forever (NFS root)...\n");
13131da177e4SLinus Torvalds 				goto try_try_again;
13141da177e4SLinus Torvalds 			}
13151da177e4SLinus Torvalds #endif
13161da177e4SLinus Torvalds 
13171da177e4SLinus Torvalds 			if (--retries) {
13181da177e4SLinus Torvalds 				printk(KERN_ERR
13191da177e4SLinus Torvalds 				       "IP-Config: Reopening network devices...\n");
13201da177e4SLinus Torvalds 				goto try_try_again;
13211da177e4SLinus Torvalds 			}
13221da177e4SLinus Torvalds 
13231da177e4SLinus Torvalds 			/* Oh, well.  At least we tried. */
13241da177e4SLinus Torvalds 			printk(KERN_ERR "IP-Config: Auto-configuration of network failed.\n");
13251da177e4SLinus Torvalds 			return -1;
13261da177e4SLinus Torvalds 		}
13271da177e4SLinus Torvalds #else /* !DYNAMIC */
13281da177e4SLinus Torvalds 		printk(KERN_ERR "IP-Config: Incomplete network configuration information.\n");
13291da177e4SLinus Torvalds 		ic_close_devs();
13301da177e4SLinus Torvalds 		return -1;
13311da177e4SLinus Torvalds #endif /* IPCONFIG_DYNAMIC */
13321da177e4SLinus Torvalds 	} else {
13331da177e4SLinus Torvalds 		/* Device selected manually or only one device -> use it */
13341da177e4SLinus Torvalds 		ic_dev = ic_first_dev->dev;
13351da177e4SLinus Torvalds 	}
13361da177e4SLinus Torvalds 
13371da177e4SLinus Torvalds 	addr = root_nfs_parse_addr(root_server_path);
13381da177e4SLinus Torvalds 	if (root_server_addr == INADDR_NONE)
13391da177e4SLinus Torvalds 		root_server_addr = addr;
13401da177e4SLinus Torvalds 
13411da177e4SLinus Torvalds 	/*
13421da177e4SLinus Torvalds 	 * Use defaults whereever applicable.
13431da177e4SLinus Torvalds 	 */
13441da177e4SLinus Torvalds 	if (ic_defaults() < 0)
13451da177e4SLinus Torvalds 		return -1;
13461da177e4SLinus Torvalds 
13471da177e4SLinus Torvalds 	/*
13481da177e4SLinus Torvalds 	 * Close all network devices except the device we've
13491da177e4SLinus Torvalds 	 * autoconfigured and set up routes.
13501da177e4SLinus Torvalds 	 */
13511da177e4SLinus Torvalds 	ic_close_devs();
13521da177e4SLinus Torvalds 	if (ic_setup_if() < 0 || ic_setup_routes() < 0)
13531da177e4SLinus Torvalds 		return -1;
13541da177e4SLinus Torvalds 
13551da177e4SLinus Torvalds 	/*
13561da177e4SLinus Torvalds 	 * Record which protocol was actually used.
13571da177e4SLinus Torvalds 	 */
13581da177e4SLinus Torvalds #ifdef IPCONFIG_DYNAMIC
13591da177e4SLinus Torvalds 	ic_proto_used = ic_got_reply | (ic_proto_enabled & IC_USE_DHCP);
13601da177e4SLinus Torvalds #endif
13611da177e4SLinus Torvalds 
13621da177e4SLinus Torvalds #ifndef IPCONFIG_SILENT
13631da177e4SLinus Torvalds 	/*
13641da177e4SLinus Torvalds 	 * Clue in the operator.
13651da177e4SLinus Torvalds 	 */
13661da177e4SLinus Torvalds 	printk("IP-Config: Complete:");
13671da177e4SLinus Torvalds 	printk("\n      device=%s", ic_dev->name);
13681da177e4SLinus Torvalds 	printk(", addr=%u.%u.%u.%u", NIPQUAD(ic_myaddr));
13691da177e4SLinus Torvalds 	printk(", mask=%u.%u.%u.%u", NIPQUAD(ic_netmask));
13701da177e4SLinus Torvalds 	printk(", gw=%u.%u.%u.%u", NIPQUAD(ic_gateway));
13711da177e4SLinus Torvalds 	printk(",\n     host=%s, domain=%s, nis-domain=%s",
13721da177e4SLinus Torvalds 	       system_utsname.nodename, ic_domain, system_utsname.domainname);
13731da177e4SLinus Torvalds 	printk(",\n     bootserver=%u.%u.%u.%u", NIPQUAD(ic_servaddr));
13741da177e4SLinus Torvalds 	printk(", rootserver=%u.%u.%u.%u", NIPQUAD(root_server_addr));
13751da177e4SLinus Torvalds 	printk(", rootpath=%s", root_server_path);
13761da177e4SLinus Torvalds 	printk("\n");
13771da177e4SLinus Torvalds #endif /* !SILENT */
13781da177e4SLinus Torvalds 
13791da177e4SLinus Torvalds 	return 0;
13801da177e4SLinus Torvalds }
13811da177e4SLinus Torvalds 
13821da177e4SLinus Torvalds late_initcall(ip_auto_config);
13831da177e4SLinus Torvalds 
13841da177e4SLinus Torvalds 
13851da177e4SLinus Torvalds /*
13861da177e4SLinus Torvalds  *  Decode any IP configuration options in the "ip=" or "nfsaddrs=" kernel
13871da177e4SLinus Torvalds  *  command line parameter. It consists of option fields separated by colons in
13881da177e4SLinus Torvalds  *  the following order:
13891da177e4SLinus Torvalds  *
13901da177e4SLinus Torvalds  *  <client-ip>:<server-ip>:<gw-ip>:<netmask>:<host name>:<device>:<PROTO>
13911da177e4SLinus Torvalds  *
13921da177e4SLinus Torvalds  *  Any of the fields can be empty which means to use a default value:
13931da177e4SLinus Torvalds  *	<client-ip>	- address given by BOOTP or RARP
13941da177e4SLinus Torvalds  *	<server-ip>	- address of host returning BOOTP or RARP packet
13951da177e4SLinus Torvalds  *	<gw-ip>		- none, or the address returned by BOOTP
13961da177e4SLinus Torvalds  *	<netmask>	- automatically determined from <client-ip>, or the
13971da177e4SLinus Torvalds  *			  one returned by BOOTP
13981da177e4SLinus Torvalds  *	<host name>	- <client-ip> in ASCII notation, or the name returned
13991da177e4SLinus Torvalds  *			  by BOOTP
14001da177e4SLinus Torvalds  *	<device>	- use all available devices
14011da177e4SLinus Torvalds  *	<PROTO>:
14021da177e4SLinus Torvalds  *	   off|none	    - don't do autoconfig at all (DEFAULT)
14031da177e4SLinus Torvalds  *	   on|any           - use any configured protocol
14041da177e4SLinus Torvalds  *	   dhcp|bootp|rarp  - use only the specified protocol
14051da177e4SLinus Torvalds  *	   both             - use both BOOTP and RARP (not DHCP)
14061da177e4SLinus Torvalds  */
14071da177e4SLinus Torvalds static int __init ic_proto_name(char *name)
14081da177e4SLinus Torvalds {
14091da177e4SLinus Torvalds 	if (!strcmp(name, "on") || !strcmp(name, "any")) {
14101da177e4SLinus Torvalds 		return 1;
14111da177e4SLinus Torvalds 	}
14121da177e4SLinus Torvalds #ifdef CONFIG_IP_PNP_DHCP
14131da177e4SLinus Torvalds 	else if (!strcmp(name, "dhcp")) {
14141da177e4SLinus Torvalds 		ic_proto_enabled &= ~IC_RARP;
14151da177e4SLinus Torvalds 		return 1;
14161da177e4SLinus Torvalds 	}
14171da177e4SLinus Torvalds #endif
14181da177e4SLinus Torvalds #ifdef CONFIG_IP_PNP_BOOTP
14191da177e4SLinus Torvalds 	else if (!strcmp(name, "bootp")) {
14201da177e4SLinus Torvalds 		ic_proto_enabled &= ~(IC_RARP | IC_USE_DHCP);
14211da177e4SLinus Torvalds 		return 1;
14221da177e4SLinus Torvalds 	}
14231da177e4SLinus Torvalds #endif
14241da177e4SLinus Torvalds #ifdef CONFIG_IP_PNP_RARP
14251da177e4SLinus Torvalds 	else if (!strcmp(name, "rarp")) {
14261da177e4SLinus Torvalds 		ic_proto_enabled &= ~(IC_BOOTP | IC_USE_DHCP);
14271da177e4SLinus Torvalds 		return 1;
14281da177e4SLinus Torvalds 	}
14291da177e4SLinus Torvalds #endif
14301da177e4SLinus Torvalds #ifdef IPCONFIG_DYNAMIC
14311da177e4SLinus Torvalds 	else if (!strcmp(name, "both")) {
14321da177e4SLinus Torvalds 		ic_proto_enabled &= ~IC_USE_DHCP; /* backward compat :-( */
14331da177e4SLinus Torvalds 		return 1;
14341da177e4SLinus Torvalds 	}
14351da177e4SLinus Torvalds #endif
14361da177e4SLinus Torvalds 	return 0;
14371da177e4SLinus Torvalds }
14381da177e4SLinus Torvalds 
14391da177e4SLinus Torvalds static int __init ip_auto_config_setup(char *addrs)
14401da177e4SLinus Torvalds {
14411da177e4SLinus Torvalds 	char *cp, *ip, *dp;
14421da177e4SLinus Torvalds 	int num = 0;
14431da177e4SLinus Torvalds 
14441da177e4SLinus Torvalds 	ic_set_manually = 1;
14451da177e4SLinus Torvalds 
14461da177e4SLinus Torvalds 	ic_enable = (*addrs &&
14471da177e4SLinus Torvalds 		(strcmp(addrs, "off") != 0) &&
14481da177e4SLinus Torvalds 		(strcmp(addrs, "none") != 0));
14491da177e4SLinus Torvalds 	if (!ic_enable)
14501da177e4SLinus Torvalds 		return 1;
14511da177e4SLinus Torvalds 
14521da177e4SLinus Torvalds 	if (ic_proto_name(addrs))
14531da177e4SLinus Torvalds 		return 1;
14541da177e4SLinus Torvalds 
14551da177e4SLinus Torvalds 	/* Parse the whole string */
14561da177e4SLinus Torvalds 	ip = addrs;
14571da177e4SLinus Torvalds 	while (ip && *ip) {
14581da177e4SLinus Torvalds 		if ((cp = strchr(ip, ':')))
14591da177e4SLinus Torvalds 			*cp++ = '\0';
14601da177e4SLinus Torvalds 		if (strlen(ip) > 0) {
14611da177e4SLinus Torvalds 			DBG(("IP-Config: Parameter #%d: `%s'\n", num, ip));
14621da177e4SLinus Torvalds 			switch (num) {
14631da177e4SLinus Torvalds 			case 0:
14641da177e4SLinus Torvalds 				if ((ic_myaddr = in_aton(ip)) == INADDR_ANY)
14651da177e4SLinus Torvalds 					ic_myaddr = INADDR_NONE;
14661da177e4SLinus Torvalds 				break;
14671da177e4SLinus Torvalds 			case 1:
14681da177e4SLinus Torvalds 				if ((ic_servaddr = in_aton(ip)) == INADDR_ANY)
14691da177e4SLinus Torvalds 					ic_servaddr = INADDR_NONE;
14701da177e4SLinus Torvalds 				break;
14711da177e4SLinus Torvalds 			case 2:
14721da177e4SLinus Torvalds 				if ((ic_gateway = in_aton(ip)) == INADDR_ANY)
14731da177e4SLinus Torvalds 					ic_gateway = INADDR_NONE;
14741da177e4SLinus Torvalds 				break;
14751da177e4SLinus Torvalds 			case 3:
14761da177e4SLinus Torvalds 				if ((ic_netmask = in_aton(ip)) == INADDR_ANY)
14771da177e4SLinus Torvalds 					ic_netmask = INADDR_NONE;
14781da177e4SLinus Torvalds 				break;
14791da177e4SLinus Torvalds 			case 4:
14801da177e4SLinus Torvalds 				if ((dp = strchr(ip, '.'))) {
14811da177e4SLinus Torvalds 					*dp++ = '\0';
14821da177e4SLinus Torvalds 					strlcpy(system_utsname.domainname, dp,
14831da177e4SLinus Torvalds 						sizeof(system_utsname.domainname));
14841da177e4SLinus Torvalds 				}
14851da177e4SLinus Torvalds 				strlcpy(system_utsname.nodename, ip,
14861da177e4SLinus Torvalds 					sizeof(system_utsname.nodename));
14871da177e4SLinus Torvalds 				ic_host_name_set = 1;
14881da177e4SLinus Torvalds 				break;
14891da177e4SLinus Torvalds 			case 5:
14901da177e4SLinus Torvalds 				strlcpy(user_dev_name, ip, sizeof(user_dev_name));
14911da177e4SLinus Torvalds 				break;
14921da177e4SLinus Torvalds 			case 6:
14931da177e4SLinus Torvalds 				ic_proto_name(ip);
14941da177e4SLinus Torvalds 				break;
14951da177e4SLinus Torvalds 			}
14961da177e4SLinus Torvalds 		}
14971da177e4SLinus Torvalds 		ip = cp;
14981da177e4SLinus Torvalds 		num++;
14991da177e4SLinus Torvalds 	}
15001da177e4SLinus Torvalds 
15011da177e4SLinus Torvalds 	return 1;
15021da177e4SLinus Torvalds }
15031da177e4SLinus Torvalds 
15041da177e4SLinus Torvalds static int __init nfsaddrs_config_setup(char *addrs)
15051da177e4SLinus Torvalds {
15061da177e4SLinus Torvalds 	return ip_auto_config_setup(addrs);
15071da177e4SLinus Torvalds }
15081da177e4SLinus Torvalds 
15091da177e4SLinus Torvalds __setup("ip=", ip_auto_config_setup);
15101da177e4SLinus Torvalds __setup("nfsaddrs=", nfsaddrs_config_setup);
1511