xref: /linux/net/ipv4/ipconfig.c (revision 964ad81cbd933e5fa310faeec1e923c14651284b)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *  Automatic Configuration of IP -- use DHCP, BOOTP, RARP, or
31da177e4SLinus Torvalds  *  user-supplied information to configure own IP address and routes.
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  *  Copyright (C) 1996-1998 Martin Mares <mj@atrey.karlin.mff.cuni.cz>
61da177e4SLinus Torvalds  *
71da177e4SLinus Torvalds  *  Derived from network configuration code in fs/nfs/nfsroot.c,
81da177e4SLinus Torvalds  *  originally Copyright (C) 1995, 1996 Gero Kuhlmann and me.
91da177e4SLinus Torvalds  *
101da177e4SLinus Torvalds  *  BOOTP rewritten to construct and analyse packets itself instead
111da177e4SLinus Torvalds  *  of misusing the IP layer. num_bugs_causing_wrong_arp_replies--;
121da177e4SLinus Torvalds  *					     -- MJ, December 1998
131da177e4SLinus Torvalds  *
141da177e4SLinus Torvalds  *  Fixed ip_auto_config_setup calling at startup in the new "Linker Magic"
151da177e4SLinus Torvalds  *  initialization scheme.
161da177e4SLinus Torvalds  *	- Arnaldo Carvalho de Melo <acme@conectiva.com.br>, 08/11/1999
171da177e4SLinus Torvalds  *
181da177e4SLinus Torvalds  *  DHCP support added.  To users this looks like a whole separate
191da177e4SLinus Torvalds  *  protocol, but we know it's just a bag on the side of BOOTP.
201da177e4SLinus Torvalds  *		-- Chip Salzenberg <chip@valinux.com>, May 2000
211da177e4SLinus Torvalds  *
221da177e4SLinus Torvalds  *  Ported DHCP support from 2.2.16 to 2.4.0-test4
231da177e4SLinus Torvalds  *              -- Eric Biederman <ebiederman@lnxi.com>, 30 Aug 2000
241da177e4SLinus Torvalds  *
251da177e4SLinus Torvalds  *  Merged changes from 2.2.19 into 2.4.3
261da177e4SLinus Torvalds  *              -- Eric Biederman <ebiederman@lnxi.com>, 22 April Aug 2001
271da177e4SLinus Torvalds  *
281da177e4SLinus Torvalds  *  Multiple Nameservers in /proc/net/pnp
291da177e4SLinus Torvalds  *              --  Josef Siemes <jsiemes@web.de>, Aug 2002
301da177e4SLinus Torvalds  */
311da177e4SLinus Torvalds 
321da177e4SLinus Torvalds #include <linux/types.h>
331da177e4SLinus Torvalds #include <linux/string.h>
341da177e4SLinus Torvalds #include <linux/kernel.h>
351da177e4SLinus Torvalds #include <linux/jiffies.h>
361da177e4SLinus Torvalds #include <linux/random.h>
371da177e4SLinus Torvalds #include <linux/init.h>
381da177e4SLinus Torvalds #include <linux/utsname.h>
391da177e4SLinus Torvalds #include <linux/in.h>
401da177e4SLinus Torvalds #include <linux/if.h>
411da177e4SLinus Torvalds #include <linux/inet.h>
4214c85021SArnaldo Carvalho de Melo #include <linux/inetdevice.h>
431da177e4SLinus Torvalds #include <linux/netdevice.h>
441da177e4SLinus Torvalds #include <linux/if_arp.h>
451da177e4SLinus Torvalds #include <linux/skbuff.h>
461da177e4SLinus Torvalds #include <linux/ip.h>
471da177e4SLinus Torvalds #include <linux/socket.h>
481da177e4SLinus Torvalds #include <linux/route.h>
491da177e4SLinus Torvalds #include <linux/udp.h>
501da177e4SLinus Torvalds #include <linux/proc_fs.h>
511da177e4SLinus Torvalds #include <linux/seq_file.h>
521da177e4SLinus Torvalds #include <linux/major.h>
531da177e4SLinus Torvalds #include <linux/root_dev.h>
541da177e4SLinus Torvalds #include <linux/delay.h>
5543d60661SAdrian Bunk #include <linux/nfs_fs.h>
56457c4cbcSEric W. Biederman #include <net/net_namespace.h>
571da177e4SLinus Torvalds #include <net/arp.h>
581da177e4SLinus Torvalds #include <net/ip.h>
591da177e4SLinus Torvalds #include <net/ipconfig.h>
6014c85021SArnaldo Carvalho de Melo #include <net/route.h>
611da177e4SLinus Torvalds 
621da177e4SLinus Torvalds #include <asm/uaccess.h>
631da177e4SLinus Torvalds #include <net/checksum.h>
641da177e4SLinus Torvalds #include <asm/processor.h>
651da177e4SLinus Torvalds 
661da177e4SLinus Torvalds /* Define this to allow debugging output */
671da177e4SLinus Torvalds #undef IPCONFIG_DEBUG
681da177e4SLinus Torvalds 
691da177e4SLinus Torvalds #ifdef IPCONFIG_DEBUG
701da177e4SLinus Torvalds #define DBG(x) printk x
711da177e4SLinus Torvalds #else
721da177e4SLinus Torvalds #define DBG(x) do { } while(0)
731da177e4SLinus Torvalds #endif
741da177e4SLinus Torvalds 
751da177e4SLinus Torvalds #if defined(CONFIG_IP_PNP_DHCP)
761da177e4SLinus Torvalds #define IPCONFIG_DHCP
771da177e4SLinus Torvalds #endif
781da177e4SLinus Torvalds #if defined(CONFIG_IP_PNP_BOOTP) || defined(CONFIG_IP_PNP_DHCP)
791da177e4SLinus Torvalds #define IPCONFIG_BOOTP
801da177e4SLinus Torvalds #endif
811da177e4SLinus Torvalds #if defined(CONFIG_IP_PNP_RARP)
821da177e4SLinus Torvalds #define IPCONFIG_RARP
831da177e4SLinus Torvalds #endif
841da177e4SLinus Torvalds #if defined(IPCONFIG_BOOTP) || defined(IPCONFIG_RARP)
851da177e4SLinus Torvalds #define IPCONFIG_DYNAMIC
861da177e4SLinus Torvalds #endif
871da177e4SLinus Torvalds 
881da177e4SLinus Torvalds /* Define the friendly delay before and after opening net devices */
891da177e4SLinus Torvalds #define CONF_PRE_OPEN		500	/* Before opening: 1/2 second */
901da177e4SLinus Torvalds #define CONF_POST_OPEN		1	/* After opening: 1 second */
911da177e4SLinus Torvalds 
921da177e4SLinus Torvalds /* Define the timeout for waiting for a DHCP/BOOTP/RARP reply */
931da177e4SLinus Torvalds #define CONF_OPEN_RETRIES 	2	/* (Re)open devices twice */
941da177e4SLinus Torvalds #define CONF_SEND_RETRIES 	6	/* Send six requests per open */
951da177e4SLinus Torvalds #define CONF_INTER_TIMEOUT	(HZ/2)	/* Inter-device timeout: 1/2 second */
961da177e4SLinus Torvalds #define CONF_BASE_TIMEOUT	(HZ*2)	/* Initial timeout: 2 seconds */
971da177e4SLinus Torvalds #define CONF_TIMEOUT_RANDOM	(HZ)	/* Maximum amount of randomization */
981da177e4SLinus Torvalds #define CONF_TIMEOUT_MULT	*7/4	/* Rate of timeout growth */
991da177e4SLinus Torvalds #define CONF_TIMEOUT_MAX	(HZ*30)	/* Maximum allowed timeout */
1001da177e4SLinus Torvalds #define CONF_NAMESERVERS_MAX   3       /* Maximum number of nameservers
1011da177e4SLinus Torvalds 					   - '3' from resolv.h */
1021da177e4SLinus Torvalds 
10309640e63SHarvey Harrison #define NONE cpu_to_be32(INADDR_NONE)
10409640e63SHarvey Harrison #define ANY cpu_to_be32(INADDR_ANY)
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 
1335a874db4SAl Viro __be32 ic_myaddr = NONE;		/* My IP address */
1345a874db4SAl Viro static __be32 ic_netmask = NONE;	/* Netmask for local subnet */
1355a874db4SAl Viro __be32 ic_gateway = NONE;	/* Gateway IP address */
1361da177e4SLinus Torvalds 
1375a874db4SAl Viro __be32 ic_servaddr = NONE;	/* Boot server IP address */
1381da177e4SLinus Torvalds 
1395a874db4SAl Viro __be32 root_server_addr = NONE;	/* Address of NFS server */
1401da177e4SLinus Torvalds u8 root_server_path[256] = { 0, };	/* Path to mount as root */
1411da177e4SLinus Torvalds 
1422513dfb8SChris Friesen u32 ic_dev_xid;		/* Device under configuration */
1432513dfb8SChris Friesen 
1441b0b04f9SDavid S. Miller /* vendor class identifier */
1451b0b04f9SDavid S. Miller static char vendor_class_identifier[253] __initdata;
14662013dbbSRainer Jochem 
1471da177e4SLinus Torvalds /* Persistent data: */
1481da177e4SLinus Torvalds 
1491da177e4SLinus Torvalds static int ic_proto_used;			/* Protocol used, if any */
1505a874db4SAl Viro static __be32 ic_nameservers[CONF_NAMESERVERS_MAX]; /* DNS Server IP addresses */
1511da177e4SLinus Torvalds static u8 ic_domain[64];		/* DNS (not NIS) domain name */
1521da177e4SLinus Torvalds 
1531da177e4SLinus Torvalds /*
1541da177e4SLinus Torvalds  * Private state.
1551da177e4SLinus Torvalds  */
1561da177e4SLinus Torvalds 
1571da177e4SLinus Torvalds /* Name of user-selected boot device */
1581da177e4SLinus Torvalds static char user_dev_name[IFNAMSIZ] __initdata = { 0, };
1591da177e4SLinus Torvalds 
1601da177e4SLinus Torvalds /* Protocols supported by available interfaces */
1611da177e4SLinus Torvalds static int ic_proto_have_if __initdata = 0;
1621da177e4SLinus Torvalds 
1639643f455SChris Friesen /* MTU for boot device */
1649643f455SChris Friesen static int ic_dev_mtu __initdata = 0;
1659643f455SChris Friesen 
1661da177e4SLinus Torvalds #ifdef IPCONFIG_DYNAMIC
1671da177e4SLinus Torvalds static DEFINE_SPINLOCK(ic_recv_lock);
1681da177e4SLinus Torvalds static volatile int ic_got_reply __initdata = 0;    /* Proto(s) that replied */
1691da177e4SLinus Torvalds #endif
1701da177e4SLinus Torvalds #ifdef IPCONFIG_DHCP
1711da177e4SLinus Torvalds static int ic_dhcp_msgtype __initdata = 0;	/* DHCP msg type received */
1721da177e4SLinus Torvalds #endif
1731da177e4SLinus Torvalds 
1741da177e4SLinus Torvalds 
1751da177e4SLinus Torvalds /*
1761da177e4SLinus Torvalds  *	Network devices
1771da177e4SLinus Torvalds  */
1781da177e4SLinus Torvalds 
1791da177e4SLinus Torvalds struct ic_device {
1801da177e4SLinus Torvalds 	struct ic_device *next;
1811da177e4SLinus Torvalds 	struct net_device *dev;
1821da177e4SLinus Torvalds 	unsigned short flags;
1831da177e4SLinus Torvalds 	short able;
1845a874db4SAl Viro 	__be32 xid;
1851da177e4SLinus Torvalds };
1861da177e4SLinus Torvalds 
1871da177e4SLinus Torvalds static struct ic_device *ic_first_dev __initdata = NULL;/* List of open device */
1881da177e4SLinus Torvalds static struct net_device *ic_dev __initdata = NULL;	/* Selected device */
1891da177e4SLinus Torvalds 
190*964ad81cSDavid S. Miller static bool __init ic_device_match(struct net_device *dev)
191*964ad81cSDavid S. Miller {
192*964ad81cSDavid S. Miller 	if (user_dev_name[0] ? !strcmp(dev->name, user_dev_name) :
193*964ad81cSDavid S. Miller 	    (!(dev->flags & IFF_LOOPBACK) &&
194*964ad81cSDavid S. Miller 	     (dev->flags & (IFF_POINTOPOINT|IFF_BROADCAST)) &&
195*964ad81cSDavid S. Miller 	     strncmp(dev->name, "dummy", 5)))
196*964ad81cSDavid S. Miller 		return true;
197*964ad81cSDavid S. Miller 	return false;
198*964ad81cSDavid S. Miller }
199*964ad81cSDavid S. Miller 
2001da177e4SLinus Torvalds static int __init ic_open_devs(void)
2011da177e4SLinus Torvalds {
2021da177e4SLinus Torvalds 	struct ic_device *d, **last;
2031da177e4SLinus Torvalds 	struct net_device *dev;
2041da177e4SLinus Torvalds 	unsigned short oflags;
2051da177e4SLinus Torvalds 
2061da177e4SLinus Torvalds 	last = &ic_first_dev;
2076756ae4bSStephen Hemminger 	rtnl_lock();
2081da177e4SLinus Torvalds 
2091da177e4SLinus Torvalds 	/* bring loopback device up first */
2100cc217e1SEric W. Biederman 	for_each_netdev(&init_net, dev) {
2110cc217e1SEric W. Biederman 		if (!(dev->flags & IFF_LOOPBACK))
2120cc217e1SEric W. Biederman 			continue;
2130cc217e1SEric W. Biederman 		if (dev_change_flags(dev, dev->flags | IFF_UP) < 0)
2140cc217e1SEric W. Biederman 			printk(KERN_ERR "IP-Config: Failed to open %s\n", dev->name);
2150cc217e1SEric W. Biederman 	}
2161da177e4SLinus Torvalds 
217881d966bSEric W. Biederman 	for_each_netdev(&init_net, dev) {
2180cc217e1SEric W. Biederman 		if (dev->flags & IFF_LOOPBACK)
2191da177e4SLinus Torvalds 			continue;
220*964ad81cSDavid S. Miller 		if (ic_device_match(dev)) {
2211da177e4SLinus Torvalds 			int able = 0;
2221da177e4SLinus Torvalds 			if (dev->mtu >= 364)
2231da177e4SLinus Torvalds 				able |= IC_BOOTP;
2241da177e4SLinus Torvalds 			else
2251da177e4SLinus Torvalds 				printk(KERN_WARNING "DHCP/BOOTP: Ignoring device %s, MTU %d too small", dev->name, dev->mtu);
2261da177e4SLinus Torvalds 			if (!(dev->flags & IFF_NOARP))
2271da177e4SLinus Torvalds 				able |= IC_RARP;
2281da177e4SLinus Torvalds 			able &= ic_proto_enabled;
2291da177e4SLinus Torvalds 			if (ic_proto_enabled && !able)
2301da177e4SLinus Torvalds 				continue;
2311da177e4SLinus Torvalds 			oflags = dev->flags;
2321da177e4SLinus Torvalds 			if (dev_change_flags(dev, oflags | IFF_UP) < 0) {
2331da177e4SLinus Torvalds 				printk(KERN_ERR "IP-Config: Failed to open %s\n", dev->name);
2341da177e4SLinus Torvalds 				continue;
2351da177e4SLinus Torvalds 			}
2361da177e4SLinus Torvalds 			if (!(d = kmalloc(sizeof(struct ic_device), GFP_KERNEL))) {
2376756ae4bSStephen Hemminger 				rtnl_unlock();
238*964ad81cSDavid S. Miller 				return -ENOMEM;
2391da177e4SLinus Torvalds 			}
2401da177e4SLinus Torvalds 			d->dev = dev;
2411da177e4SLinus Torvalds 			*last = d;
2421da177e4SLinus Torvalds 			last = &d->next;
2431da177e4SLinus Torvalds 			d->flags = oflags;
2441da177e4SLinus Torvalds 			d->able = able;
2451da177e4SLinus Torvalds 			if (able & IC_BOOTP)
2465a874db4SAl Viro 				get_random_bytes(&d->xid, sizeof(__be32));
2471da177e4SLinus Torvalds 			else
2481da177e4SLinus Torvalds 				d->xid = 0;
2491da177e4SLinus Torvalds 			ic_proto_have_if |= able;
2501da177e4SLinus Torvalds 			DBG(("IP-Config: %s UP (able=%d, xid=%08x)\n",
2511da177e4SLinus Torvalds 				dev->name, able, d->xid));
2521da177e4SLinus Torvalds 		}
2531da177e4SLinus Torvalds 	}
2546756ae4bSStephen Hemminger 	rtnl_unlock();
2551da177e4SLinus Torvalds 
2561da177e4SLinus Torvalds 	*last = NULL;
2571da177e4SLinus Torvalds 
2581da177e4SLinus Torvalds 	if (!ic_first_dev) {
2591da177e4SLinus Torvalds 		if (user_dev_name[0])
2601da177e4SLinus Torvalds 			printk(KERN_ERR "IP-Config: Device `%s' not found.\n", user_dev_name);
2611da177e4SLinus Torvalds 		else
2621da177e4SLinus Torvalds 			printk(KERN_ERR "IP-Config: No network devices available.\n");
263*964ad81cSDavid S. Miller 		return -ENODEV;
2641da177e4SLinus Torvalds 	}
2651da177e4SLinus Torvalds 	return 0;
2661da177e4SLinus Torvalds }
2671da177e4SLinus Torvalds 
2681da177e4SLinus Torvalds static void __init ic_close_devs(void)
2691da177e4SLinus Torvalds {
2701da177e4SLinus Torvalds 	struct ic_device *d, *next;
2711da177e4SLinus Torvalds 	struct net_device *dev;
2721da177e4SLinus Torvalds 
2736756ae4bSStephen Hemminger 	rtnl_lock();
2741da177e4SLinus Torvalds 	next = ic_first_dev;
2751da177e4SLinus Torvalds 	while ((d = next)) {
2761da177e4SLinus Torvalds 		next = d->next;
2771da177e4SLinus Torvalds 		dev = d->dev;
2781da177e4SLinus Torvalds 		if (dev != ic_dev) {
2791da177e4SLinus Torvalds 			DBG(("IP-Config: Downing %s\n", dev->name));
2801da177e4SLinus Torvalds 			dev_change_flags(dev, d->flags);
2811da177e4SLinus Torvalds 		}
2821da177e4SLinus Torvalds 		kfree(d);
2831da177e4SLinus Torvalds 	}
2846756ae4bSStephen Hemminger 	rtnl_unlock();
2851da177e4SLinus Torvalds }
2861da177e4SLinus Torvalds 
2871da177e4SLinus Torvalds /*
2881da177e4SLinus Torvalds  *	Interface to various network functions.
2891da177e4SLinus Torvalds  */
2901da177e4SLinus Torvalds 
2911da177e4SLinus Torvalds static inline void
2925a874db4SAl Viro set_sockaddr(struct sockaddr_in *sin, __be32 addr, __be16 port)
2931da177e4SLinus Torvalds {
2941da177e4SLinus Torvalds 	sin->sin_family = AF_INET;
2951da177e4SLinus Torvalds 	sin->sin_addr.s_addr = addr;
2961da177e4SLinus Torvalds 	sin->sin_port = port;
2971da177e4SLinus Torvalds }
2981da177e4SLinus Torvalds 
2999643f455SChris Friesen static int __init ic_devinet_ioctl(unsigned int cmd, struct ifreq *arg)
3001da177e4SLinus Torvalds {
3011da177e4SLinus Torvalds 	int res;
3021da177e4SLinus Torvalds 
3031da177e4SLinus Torvalds 	mm_segment_t oldfs = get_fs();
3041da177e4SLinus Torvalds 	set_fs(get_ds());
305e5b13cb1SDenis V. Lunev 	res = devinet_ioctl(&init_net, cmd, (struct ifreq __user *) arg);
3061da177e4SLinus Torvalds 	set_fs(oldfs);
3071da177e4SLinus Torvalds 	return res;
3081da177e4SLinus Torvalds }
3091da177e4SLinus Torvalds 
3109643f455SChris Friesen static int __init ic_dev_ioctl(unsigned int cmd, struct ifreq *arg)
3119643f455SChris Friesen {
3129643f455SChris Friesen 	int res;
3139643f455SChris Friesen 
3149643f455SChris Friesen 	mm_segment_t oldfs = get_fs();
3159643f455SChris Friesen 	set_fs(get_ds());
3169643f455SChris Friesen 	res = dev_ioctl(&init_net, cmd, (struct ifreq __user *) arg);
3179643f455SChris Friesen 	set_fs(oldfs);
3189643f455SChris Friesen 	return res;
3199643f455SChris Friesen }
3209643f455SChris Friesen 
3211da177e4SLinus Torvalds static int __init ic_route_ioctl(unsigned int cmd, struct rtentry *arg)
3221da177e4SLinus Torvalds {
3231da177e4SLinus Torvalds 	int res;
3241da177e4SLinus Torvalds 
3251da177e4SLinus Torvalds 	mm_segment_t oldfs = get_fs();
3261da177e4SLinus Torvalds 	set_fs(get_ds());
3271bad118aSDenis V. Lunev 	res = ip_rt_ioctl(&init_net, cmd, (void __user *) arg);
3281da177e4SLinus Torvalds 	set_fs(oldfs);
3291da177e4SLinus Torvalds 	return res;
3301da177e4SLinus Torvalds }
3311da177e4SLinus Torvalds 
3321da177e4SLinus Torvalds /*
3331da177e4SLinus Torvalds  *	Set up interface addresses and routes.
3341da177e4SLinus Torvalds  */
3351da177e4SLinus Torvalds 
3361da177e4SLinus Torvalds static int __init ic_setup_if(void)
3371da177e4SLinus Torvalds {
3381da177e4SLinus Torvalds 	struct ifreq ir;
3391da177e4SLinus Torvalds 	struct sockaddr_in *sin = (void *) &ir.ifr_ifru.ifru_addr;
3401da177e4SLinus Torvalds 	int err;
3411da177e4SLinus Torvalds 
3421da177e4SLinus Torvalds 	memset(&ir, 0, sizeof(ir));
3431da177e4SLinus Torvalds 	strcpy(ir.ifr_ifrn.ifrn_name, ic_dev->name);
3441da177e4SLinus Torvalds 	set_sockaddr(sin, ic_myaddr, 0);
3459643f455SChris Friesen 	if ((err = ic_devinet_ioctl(SIOCSIFADDR, &ir)) < 0) {
3461da177e4SLinus Torvalds 		printk(KERN_ERR "IP-Config: Unable to set interface address (%d).\n", err);
3471da177e4SLinus Torvalds 		return -1;
3481da177e4SLinus Torvalds 	}
3491da177e4SLinus Torvalds 	set_sockaddr(sin, ic_netmask, 0);
3509643f455SChris Friesen 	if ((err = ic_devinet_ioctl(SIOCSIFNETMASK, &ir)) < 0) {
3511da177e4SLinus Torvalds 		printk(KERN_ERR "IP-Config: Unable to set interface netmask (%d).\n", err);
3521da177e4SLinus Torvalds 		return -1;
3531da177e4SLinus Torvalds 	}
3541da177e4SLinus Torvalds 	set_sockaddr(sin, ic_myaddr | ~ic_netmask, 0);
3559643f455SChris Friesen 	if ((err = ic_devinet_ioctl(SIOCSIFBRDADDR, &ir)) < 0) {
3561da177e4SLinus Torvalds 		printk(KERN_ERR "IP-Config: Unable to set interface broadcast address (%d).\n", err);
3571da177e4SLinus Torvalds 		return -1;
3581da177e4SLinus Torvalds 	}
3599643f455SChris Friesen 	/* Handle the case where we need non-standard MTU on the boot link (a network
3609643f455SChris Friesen 	 * using jumbo frames, for instance).  If we can't set the mtu, don't error
3619643f455SChris Friesen 	 * out, we'll try to muddle along.
3629643f455SChris Friesen 	 */
3639643f455SChris Friesen 	if (ic_dev_mtu != 0) {
3649643f455SChris Friesen 		strcpy(ir.ifr_name, ic_dev->name);
3659643f455SChris Friesen 		ir.ifr_mtu = ic_dev_mtu;
3669643f455SChris Friesen 		if ((err = ic_dev_ioctl(SIOCSIFMTU, &ir)) < 0)
3679643f455SChris Friesen 			printk(KERN_ERR "IP-Config: Unable to set interface mtu to %d (%d).\n",
3689643f455SChris Friesen 			                 ic_dev_mtu, err);
3699643f455SChris Friesen 	}
3701da177e4SLinus Torvalds 	return 0;
3711da177e4SLinus Torvalds }
3721da177e4SLinus Torvalds 
3731da177e4SLinus Torvalds static int __init ic_setup_routes(void)
3741da177e4SLinus Torvalds {
3751da177e4SLinus Torvalds 	/* No need to setup device routes, only the default route... */
3761da177e4SLinus Torvalds 
3775a874db4SAl Viro 	if (ic_gateway != NONE) {
3781da177e4SLinus Torvalds 		struct rtentry rm;
3791da177e4SLinus Torvalds 		int err;
3801da177e4SLinus Torvalds 
3811da177e4SLinus Torvalds 		memset(&rm, 0, sizeof(rm));
3821da177e4SLinus Torvalds 		if ((ic_gateway ^ ic_myaddr) & ic_netmask) {
3831da177e4SLinus Torvalds 			printk(KERN_ERR "IP-Config: Gateway not on directly connected network.\n");
3841da177e4SLinus Torvalds 			return -1;
3851da177e4SLinus Torvalds 		}
3861da177e4SLinus Torvalds 		set_sockaddr((struct sockaddr_in *) &rm.rt_dst, 0, 0);
3871da177e4SLinus Torvalds 		set_sockaddr((struct sockaddr_in *) &rm.rt_genmask, 0, 0);
3881da177e4SLinus Torvalds 		set_sockaddr((struct sockaddr_in *) &rm.rt_gateway, ic_gateway, 0);
3891da177e4SLinus Torvalds 		rm.rt_flags = RTF_UP | RTF_GATEWAY;
3901da177e4SLinus Torvalds 		if ((err = ic_route_ioctl(SIOCADDRT, &rm)) < 0) {
3911da177e4SLinus Torvalds 			printk(KERN_ERR "IP-Config: Cannot add default route (%d).\n", err);
3921da177e4SLinus Torvalds 			return -1;
3931da177e4SLinus Torvalds 		}
3941da177e4SLinus Torvalds 	}
3951da177e4SLinus Torvalds 
3961da177e4SLinus Torvalds 	return 0;
3971da177e4SLinus Torvalds }
3981da177e4SLinus Torvalds 
3991da177e4SLinus Torvalds /*
4001da177e4SLinus Torvalds  *	Fill in default values for all missing parameters.
4011da177e4SLinus Torvalds  */
4021da177e4SLinus Torvalds 
4031da177e4SLinus Torvalds static int __init ic_defaults(void)
4041da177e4SLinus Torvalds {
4051da177e4SLinus Torvalds 	/*
4061da177e4SLinus Torvalds 	 *	At this point we have no userspace running so need not
4071da177e4SLinus Torvalds 	 *	claim locks on system_utsname
4081da177e4SLinus Torvalds 	 */
4091da177e4SLinus Torvalds 
4101da177e4SLinus Torvalds 	if (!ic_host_name_set)
411673d57e7SHarvey Harrison 		sprintf(init_utsname()->nodename, "%pI4", &ic_myaddr);
4121da177e4SLinus Torvalds 
4135a874db4SAl Viro 	if (root_server_addr == NONE)
4141da177e4SLinus Torvalds 		root_server_addr = ic_servaddr;
4151da177e4SLinus Torvalds 
4165a874db4SAl Viro 	if (ic_netmask == NONE) {
4171da177e4SLinus Torvalds 		if (IN_CLASSA(ntohl(ic_myaddr)))
4181da177e4SLinus Torvalds 			ic_netmask = htonl(IN_CLASSA_NET);
4191da177e4SLinus Torvalds 		else if (IN_CLASSB(ntohl(ic_myaddr)))
4201da177e4SLinus Torvalds 			ic_netmask = htonl(IN_CLASSB_NET);
4211da177e4SLinus Torvalds 		else if (IN_CLASSC(ntohl(ic_myaddr)))
4221da177e4SLinus Torvalds 			ic_netmask = htonl(IN_CLASSC_NET);
4231da177e4SLinus Torvalds 		else {
424673d57e7SHarvey Harrison 			printk(KERN_ERR "IP-Config: Unable to guess netmask for address %pI4\n",
425673d57e7SHarvey Harrison 				&ic_myaddr);
4261da177e4SLinus Torvalds 			return -1;
4271da177e4SLinus Torvalds 		}
428673d57e7SHarvey Harrison 		printk("IP-Config: Guessing netmask %pI4\n", &ic_netmask);
4291da177e4SLinus Torvalds 	}
4301da177e4SLinus Torvalds 
4311da177e4SLinus Torvalds 	return 0;
4321da177e4SLinus Torvalds }
4331da177e4SLinus Torvalds 
4341da177e4SLinus Torvalds /*
4351da177e4SLinus Torvalds  *	RARP support.
4361da177e4SLinus Torvalds  */
4371da177e4SLinus Torvalds 
4381da177e4SLinus Torvalds #ifdef IPCONFIG_RARP
4391da177e4SLinus Torvalds 
440f2ccd8faSDavid S. Miller static int ic_rarp_recv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev);
4411da177e4SLinus Torvalds 
4421da177e4SLinus Torvalds static struct packet_type rarp_packet_type __initdata = {
44309640e63SHarvey Harrison 	.type =	cpu_to_be16(ETH_P_RARP),
4441da177e4SLinus Torvalds 	.func =	ic_rarp_recv,
4451da177e4SLinus Torvalds };
4461da177e4SLinus Torvalds 
44745e741b8SSam Ravnborg static inline void __init ic_rarp_init(void)
4481da177e4SLinus Torvalds {
4491da177e4SLinus Torvalds 	dev_add_pack(&rarp_packet_type);
4501da177e4SLinus Torvalds }
4511da177e4SLinus Torvalds 
45245e741b8SSam Ravnborg static inline void __init ic_rarp_cleanup(void)
4531da177e4SLinus Torvalds {
4541da177e4SLinus Torvalds 	dev_remove_pack(&rarp_packet_type);
4551da177e4SLinus Torvalds }
4561da177e4SLinus Torvalds 
4571da177e4SLinus Torvalds /*
4581da177e4SLinus Torvalds  *  Process received RARP packet.
4591da177e4SLinus Torvalds  */
4601da177e4SLinus Torvalds static int __init
461f2ccd8faSDavid S. Miller ic_rarp_recv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev)
4621da177e4SLinus Torvalds {
4631da177e4SLinus Torvalds 	struct arphdr *rarp;
4641da177e4SLinus Torvalds 	unsigned char *rarp_ptr;
4655a874db4SAl Viro 	__be32 sip, tip;
4661da177e4SLinus Torvalds 	unsigned char *sha, *tha;		/* s for "source", t for "target" */
4671da177e4SLinus Torvalds 	struct ic_device *d;
4681da177e4SLinus Torvalds 
469721499e8SYOSHIFUJI Hideaki 	if (!net_eq(dev_net(dev), &init_net))
470e730c155SEric W. Biederman 		goto drop;
471e730c155SEric W. Biederman 
4721da177e4SLinus Torvalds 	if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL)
4731da177e4SLinus Torvalds 		return NET_RX_DROP;
4741da177e4SLinus Torvalds 
4751da177e4SLinus Torvalds 	if (!pskb_may_pull(skb, sizeof(struct arphdr)))
4761da177e4SLinus Torvalds 		goto drop;
4771da177e4SLinus Torvalds 
4781da177e4SLinus Torvalds 	/* Basic sanity checks can be done without the lock.  */
4799c70220bSArnaldo Carvalho de Melo 	rarp = (struct arphdr *)skb_transport_header(skb);
4801da177e4SLinus Torvalds 
4811da177e4SLinus Torvalds 	/* If this test doesn't pass, it's not IP, or we should
4821da177e4SLinus Torvalds 	 * ignore it anyway.
4831da177e4SLinus Torvalds 	 */
4841da177e4SLinus Torvalds 	if (rarp->ar_hln != dev->addr_len || dev->type != ntohs(rarp->ar_hrd))
4851da177e4SLinus Torvalds 		goto drop;
4861da177e4SLinus Torvalds 
4871da177e4SLinus Torvalds 	/* If it's not a RARP reply, delete it. */
4881da177e4SLinus Torvalds 	if (rarp->ar_op != htons(ARPOP_RREPLY))
4891da177e4SLinus Torvalds 		goto drop;
4901da177e4SLinus Torvalds 
4911da177e4SLinus Torvalds 	/* If it's not Ethernet, delete it. */
4921da177e4SLinus Torvalds 	if (rarp->ar_pro != htons(ETH_P_IP))
4931da177e4SLinus Torvalds 		goto drop;
4941da177e4SLinus Torvalds 
495988b7050SPavel Emelyanov 	if (!pskb_may_pull(skb, arp_hdr_len(dev)))
4961da177e4SLinus Torvalds 		goto drop;
4971da177e4SLinus Torvalds 
4981da177e4SLinus Torvalds 	/* OK, it is all there and looks valid, process... */
4999c70220bSArnaldo Carvalho de Melo 	rarp = (struct arphdr *)skb_transport_header(skb);
5001da177e4SLinus Torvalds 	rarp_ptr = (unsigned char *) (rarp + 1);
5011da177e4SLinus Torvalds 
5021da177e4SLinus Torvalds 	/* One reply at a time, please. */
5031da177e4SLinus Torvalds 	spin_lock(&ic_recv_lock);
5041da177e4SLinus Torvalds 
5051da177e4SLinus Torvalds 	/* If we already have a reply, just drop the packet */
5061da177e4SLinus Torvalds 	if (ic_got_reply)
5071da177e4SLinus Torvalds 		goto drop_unlock;
5081da177e4SLinus Torvalds 
5091da177e4SLinus Torvalds 	/* Find the ic_device that the packet arrived on */
5101da177e4SLinus Torvalds 	d = ic_first_dev;
5111da177e4SLinus Torvalds 	while (d && d->dev != dev)
5121da177e4SLinus Torvalds 		d = d->next;
5131da177e4SLinus Torvalds 	if (!d)
5141da177e4SLinus Torvalds 		goto drop_unlock;	/* should never happen */
5151da177e4SLinus Torvalds 
5161da177e4SLinus Torvalds 	/* Extract variable-width fields */
5171da177e4SLinus Torvalds 	sha = rarp_ptr;
5181da177e4SLinus Torvalds 	rarp_ptr += dev->addr_len;
5191da177e4SLinus Torvalds 	memcpy(&sip, rarp_ptr, 4);
5201da177e4SLinus Torvalds 	rarp_ptr += 4;
5211da177e4SLinus Torvalds 	tha = rarp_ptr;
5221da177e4SLinus Torvalds 	rarp_ptr += dev->addr_len;
5231da177e4SLinus Torvalds 	memcpy(&tip, rarp_ptr, 4);
5241da177e4SLinus Torvalds 
5251da177e4SLinus Torvalds 	/* Discard packets which are not meant for us. */
5261da177e4SLinus Torvalds 	if (memcmp(tha, dev->dev_addr, dev->addr_len))
5271da177e4SLinus Torvalds 		goto drop_unlock;
5281da177e4SLinus Torvalds 
5291da177e4SLinus Torvalds 	/* Discard packets which are not from specified server. */
5305a874db4SAl Viro 	if (ic_servaddr != NONE && ic_servaddr != sip)
5311da177e4SLinus Torvalds 		goto drop_unlock;
5321da177e4SLinus Torvalds 
5331da177e4SLinus Torvalds 	/* We have a winner! */
5341da177e4SLinus Torvalds 	ic_dev = dev;
5355a874db4SAl Viro 	if (ic_myaddr == NONE)
5361da177e4SLinus Torvalds 		ic_myaddr = tip;
5371da177e4SLinus Torvalds 	ic_servaddr = sip;
5381da177e4SLinus Torvalds 	ic_got_reply = IC_RARP;
5391da177e4SLinus Torvalds 
5401da177e4SLinus Torvalds drop_unlock:
5411da177e4SLinus Torvalds 	/* Show's over.  Nothing to see here.  */
5421da177e4SLinus Torvalds 	spin_unlock(&ic_recv_lock);
5431da177e4SLinus Torvalds 
5441da177e4SLinus Torvalds drop:
5451da177e4SLinus Torvalds 	/* Throw the packet out. */
5461da177e4SLinus Torvalds 	kfree_skb(skb);
5471da177e4SLinus Torvalds 	return 0;
5481da177e4SLinus Torvalds }
5491da177e4SLinus Torvalds 
5501da177e4SLinus Torvalds 
5511da177e4SLinus Torvalds /*
5521da177e4SLinus Torvalds  *  Send RARP request packet over a single interface.
5531da177e4SLinus Torvalds  */
5541da177e4SLinus Torvalds static void __init ic_rarp_send_if(struct ic_device *d)
5551da177e4SLinus Torvalds {
5561da177e4SLinus Torvalds 	struct net_device *dev = d->dev;
5571da177e4SLinus Torvalds 	arp_send(ARPOP_RREQUEST, ETH_P_RARP, 0, dev, 0, NULL,
5581da177e4SLinus Torvalds 		 dev->dev_addr, dev->dev_addr);
5591da177e4SLinus Torvalds }
5601da177e4SLinus Torvalds #endif
5611da177e4SLinus Torvalds 
5621da177e4SLinus Torvalds /*
5631da177e4SLinus Torvalds  *	DHCP/BOOTP support.
5641da177e4SLinus Torvalds  */
5651da177e4SLinus Torvalds 
5661da177e4SLinus Torvalds #ifdef IPCONFIG_BOOTP
5671da177e4SLinus Torvalds 
5681da177e4SLinus Torvalds struct bootp_pkt {		/* BOOTP packet format */
5691da177e4SLinus Torvalds 	struct iphdr iph;	/* IP header */
5701da177e4SLinus Torvalds 	struct udphdr udph;	/* UDP header */
5711da177e4SLinus Torvalds 	u8 op;			/* 1=request, 2=reply */
5721da177e4SLinus Torvalds 	u8 htype;		/* HW address type */
5731da177e4SLinus Torvalds 	u8 hlen;		/* HW address length */
5741da177e4SLinus Torvalds 	u8 hops;		/* Used only by gateways */
5755a874db4SAl Viro 	__be32 xid;		/* Transaction ID */
5765a874db4SAl Viro 	__be16 secs;		/* Seconds since we started */
5775a874db4SAl Viro 	__be16 flags;		/* Just what it says */
5785a874db4SAl Viro 	__be32 client_ip;		/* Client's IP address if known */
5795a874db4SAl Viro 	__be32 your_ip;		/* Assigned IP address */
5805a874db4SAl Viro 	__be32 server_ip;		/* (Next, e.g. NFS) Server's IP address */
5815a874db4SAl Viro 	__be32 relay_ip;		/* IP address of BOOTP relay */
5821da177e4SLinus Torvalds 	u8 hw_addr[16];		/* Client's HW address */
5831da177e4SLinus Torvalds 	u8 serv_name[64];	/* Server host name */
5841da177e4SLinus Torvalds 	u8 boot_file[128];	/* Name of boot file */
5851da177e4SLinus Torvalds 	u8 exten[312];		/* DHCP options / BOOTP vendor extensions */
5861da177e4SLinus Torvalds };
5871da177e4SLinus Torvalds 
5881da177e4SLinus Torvalds /* packet ops */
5891da177e4SLinus Torvalds #define BOOTP_REQUEST	1
5901da177e4SLinus Torvalds #define BOOTP_REPLY	2
5911da177e4SLinus Torvalds 
5921da177e4SLinus Torvalds /* DHCP message types */
5931da177e4SLinus Torvalds #define DHCPDISCOVER	1
5941da177e4SLinus Torvalds #define DHCPOFFER	2
5951da177e4SLinus Torvalds #define DHCPREQUEST	3
5961da177e4SLinus Torvalds #define DHCPDECLINE	4
5971da177e4SLinus Torvalds #define DHCPACK		5
5981da177e4SLinus Torvalds #define DHCPNAK		6
5991da177e4SLinus Torvalds #define DHCPRELEASE	7
6001da177e4SLinus Torvalds #define DHCPINFORM	8
6011da177e4SLinus Torvalds 
602f2ccd8faSDavid S. Miller static int ic_bootp_recv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev);
6031da177e4SLinus Torvalds 
6041da177e4SLinus Torvalds static struct packet_type bootp_packet_type __initdata = {
60509640e63SHarvey Harrison 	.type =	cpu_to_be16(ETH_P_IP),
6061da177e4SLinus Torvalds 	.func =	ic_bootp_recv,
6071da177e4SLinus Torvalds };
6081da177e4SLinus Torvalds 
6091da177e4SLinus Torvalds 
6101da177e4SLinus Torvalds /*
6111da177e4SLinus Torvalds  *  Initialize DHCP/BOOTP extension fields in the request.
6121da177e4SLinus Torvalds  */
6131da177e4SLinus Torvalds 
6141da177e4SLinus Torvalds static const u8 ic_bootp_cookie[4] = { 99, 130, 83, 99 };
6151da177e4SLinus Torvalds 
6161da177e4SLinus Torvalds #ifdef IPCONFIG_DHCP
6171da177e4SLinus Torvalds 
6181da177e4SLinus Torvalds static void __init
6191da177e4SLinus Torvalds ic_dhcp_init_options(u8 *options)
6201da177e4SLinus Torvalds {
6215a874db4SAl Viro 	u8 mt = ((ic_servaddr == NONE)
6221da177e4SLinus Torvalds 		 ? DHCPDISCOVER : DHCPREQUEST);
6231da177e4SLinus Torvalds 	u8 *e = options;
62462013dbbSRainer Jochem 	int len;
6251da177e4SLinus Torvalds 
6261da177e4SLinus Torvalds #ifdef IPCONFIG_DEBUG
6271da177e4SLinus Torvalds 	printk("DHCP: Sending message type %d\n", mt);
6281da177e4SLinus Torvalds #endif
6291da177e4SLinus Torvalds 
6301da177e4SLinus Torvalds 	memcpy(e, ic_bootp_cookie, 4);	/* RFC1048 Magic Cookie */
6311da177e4SLinus Torvalds 	e += 4;
6321da177e4SLinus Torvalds 
6331da177e4SLinus Torvalds 	*e++ = 53;		/* DHCP message type */
6341da177e4SLinus Torvalds 	*e++ = 1;
6351da177e4SLinus Torvalds 	*e++ = mt;
6361da177e4SLinus Torvalds 
6371da177e4SLinus Torvalds 	if (mt == DHCPREQUEST) {
6381da177e4SLinus Torvalds 		*e++ = 54;	/* Server ID (IP address) */
6391da177e4SLinus Torvalds 		*e++ = 4;
6401da177e4SLinus Torvalds 		memcpy(e, &ic_servaddr, 4);
6411da177e4SLinus Torvalds 		e += 4;
6421da177e4SLinus Torvalds 
6431da177e4SLinus Torvalds 		*e++ = 50;	/* Requested IP address */
6441da177e4SLinus Torvalds 		*e++ = 4;
6451da177e4SLinus Torvalds 		memcpy(e, &ic_myaddr, 4);
6461da177e4SLinus Torvalds 		e += 4;
6471da177e4SLinus Torvalds 	}
6481da177e4SLinus Torvalds 
6491da177e4SLinus Torvalds 	/* always? */
6501da177e4SLinus Torvalds 	{
6511da177e4SLinus Torvalds 		static const u8 ic_req_params[] = {
6521da177e4SLinus Torvalds 			1,	/* Subnet mask */
6531da177e4SLinus Torvalds 			3,	/* Default gateway */
6541da177e4SLinus Torvalds 			6,	/* DNS server */
6551da177e4SLinus Torvalds 			12,	/* Host name */
6561da177e4SLinus Torvalds 			15,	/* Domain name */
6571da177e4SLinus Torvalds 			17,	/* Boot path */
6589643f455SChris Friesen 			26,	/* MTU */
6591da177e4SLinus Torvalds 			40,	/* NIS domain name */
6601da177e4SLinus Torvalds 		};
6611da177e4SLinus Torvalds 
6621da177e4SLinus Torvalds 		*e++ = 55;	/* Parameter request list */
6631da177e4SLinus Torvalds 		*e++ = sizeof(ic_req_params);
6641da177e4SLinus Torvalds 		memcpy(e, ic_req_params, sizeof(ic_req_params));
6651da177e4SLinus Torvalds 		e += sizeof(ic_req_params);
66662013dbbSRainer Jochem 
66762013dbbSRainer Jochem 		if (*vendor_class_identifier) {
66862013dbbSRainer Jochem 			printk(KERN_INFO "DHCP: sending class identifier \"%s\"\n",
66962013dbbSRainer Jochem 			       vendor_class_identifier);
67062013dbbSRainer Jochem 			*e++ = 60;	/* Class-identifier */
67162013dbbSRainer Jochem 			len = strlen(vendor_class_identifier);
67262013dbbSRainer Jochem 			*e++ = len;
67362013dbbSRainer Jochem 			memcpy(e, vendor_class_identifier, len);
67462013dbbSRainer Jochem 			e += len;
67562013dbbSRainer Jochem 		}
6761da177e4SLinus Torvalds 	}
6771da177e4SLinus Torvalds 
6781da177e4SLinus Torvalds 	*e++ = 255;	/* End of the list */
6791da177e4SLinus Torvalds }
6801da177e4SLinus Torvalds 
6811da177e4SLinus Torvalds #endif /* IPCONFIG_DHCP */
6821da177e4SLinus Torvalds 
6831da177e4SLinus Torvalds static void __init ic_bootp_init_ext(u8 *e)
6841da177e4SLinus Torvalds {
6851da177e4SLinus Torvalds 	memcpy(e, ic_bootp_cookie, 4);	/* RFC1048 Magic Cookie */
6861da177e4SLinus Torvalds 	e += 4;
6871da177e4SLinus Torvalds 	*e++ = 1;		/* Subnet mask request */
6881da177e4SLinus Torvalds 	*e++ = 4;
6891da177e4SLinus Torvalds 	e += 4;
6901da177e4SLinus Torvalds 	*e++ = 3;		/* Default gateway request */
6911da177e4SLinus Torvalds 	*e++ = 4;
6921da177e4SLinus Torvalds 	e += 4;
6931da177e4SLinus Torvalds 	*e++ = 5;		/* Name server request */
6941da177e4SLinus Torvalds 	*e++ = 8;
6951da177e4SLinus Torvalds 	e += 8;
6961da177e4SLinus Torvalds 	*e++ = 12;		/* Host name request */
6971da177e4SLinus Torvalds 	*e++ = 32;
6981da177e4SLinus Torvalds 	e += 32;
6991da177e4SLinus Torvalds 	*e++ = 40;		/* NIS Domain name request */
7001da177e4SLinus Torvalds 	*e++ = 32;
7011da177e4SLinus Torvalds 	e += 32;
7021da177e4SLinus Torvalds 	*e++ = 17;		/* Boot path */
7031da177e4SLinus Torvalds 	*e++ = 40;
7041da177e4SLinus Torvalds 	e += 40;
7051da177e4SLinus Torvalds 
7061da177e4SLinus Torvalds 	*e++ = 57;		/* set extension buffer size for reply */
7071da177e4SLinus Torvalds 	*e++ = 2;
7081da177e4SLinus Torvalds 	*e++ = 1;		/* 128+236+8+20+14, see dhcpd sources */
7091da177e4SLinus Torvalds 	*e++ = 150;
7101da177e4SLinus Torvalds 
7111da177e4SLinus Torvalds 	*e++ = 255;		/* End of the list */
7121da177e4SLinus Torvalds }
7131da177e4SLinus Torvalds 
7141da177e4SLinus Torvalds 
7151da177e4SLinus Torvalds /*
7161da177e4SLinus Torvalds  *  Initialize the DHCP/BOOTP mechanism.
7171da177e4SLinus Torvalds  */
71845e741b8SSam Ravnborg static inline void __init ic_bootp_init(void)
7191da177e4SLinus Torvalds {
7201da177e4SLinus Torvalds 	int i;
7211da177e4SLinus Torvalds 
7221da177e4SLinus Torvalds 	for (i = 0; i < CONF_NAMESERVERS_MAX; i++)
7235a874db4SAl Viro 		ic_nameservers[i] = NONE;
7241da177e4SLinus Torvalds 
7251da177e4SLinus Torvalds 	dev_add_pack(&bootp_packet_type);
7261da177e4SLinus Torvalds }
7271da177e4SLinus Torvalds 
7281da177e4SLinus Torvalds 
7291da177e4SLinus Torvalds /*
7301da177e4SLinus Torvalds  *  DHCP/BOOTP cleanup.
7311da177e4SLinus Torvalds  */
73245e741b8SSam Ravnborg static inline void __init ic_bootp_cleanup(void)
7331da177e4SLinus Torvalds {
7341da177e4SLinus Torvalds 	dev_remove_pack(&bootp_packet_type);
7351da177e4SLinus Torvalds }
7361da177e4SLinus Torvalds 
7371da177e4SLinus Torvalds 
7381da177e4SLinus Torvalds /*
7391da177e4SLinus Torvalds  *  Send DHCP/BOOTP request to single interface.
7401da177e4SLinus Torvalds  */
7411da177e4SLinus Torvalds static void __init ic_bootp_send_if(struct ic_device *d, unsigned long jiffies_diff)
7421da177e4SLinus Torvalds {
7431da177e4SLinus Torvalds 	struct net_device *dev = d->dev;
7441da177e4SLinus Torvalds 	struct sk_buff *skb;
7451da177e4SLinus Torvalds 	struct bootp_pkt *b;
7461da177e4SLinus Torvalds 	struct iphdr *h;
7471da177e4SLinus Torvalds 
7481da177e4SLinus Torvalds 	/* Allocate packet */
749f5184d26SJohannes Berg 	skb = alloc_skb(sizeof(struct bootp_pkt) + LL_ALLOCATED_SPACE(dev) + 15,
750f5184d26SJohannes Berg 			GFP_KERNEL);
7511da177e4SLinus Torvalds 	if (!skb)
7521da177e4SLinus Torvalds 		return;
753f5184d26SJohannes Berg 	skb_reserve(skb, LL_RESERVED_SPACE(dev));
7541da177e4SLinus Torvalds 	b = (struct bootp_pkt *) skb_put(skb, sizeof(struct bootp_pkt));
7551da177e4SLinus Torvalds 	memset(b, 0, sizeof(struct bootp_pkt));
7561da177e4SLinus Torvalds 
7571da177e4SLinus Torvalds 	/* Construct IP header */
75804b964dbSArnaldo Carvalho de Melo 	skb_reset_network_header(skb);
759eddc9ec5SArnaldo Carvalho de Melo 	h = ip_hdr(skb);
7601da177e4SLinus Torvalds 	h->version = 4;
7611da177e4SLinus Torvalds 	h->ihl = 5;
7621da177e4SLinus Torvalds 	h->tot_len = htons(sizeof(struct bootp_pkt));
7631da177e4SLinus Torvalds 	h->frag_off = htons(IP_DF);
7641da177e4SLinus Torvalds 	h->ttl = 64;
7651da177e4SLinus Torvalds 	h->protocol = IPPROTO_UDP;
7665a874db4SAl Viro 	h->daddr = htonl(INADDR_BROADCAST);
7671da177e4SLinus Torvalds 	h->check = ip_fast_csum((unsigned char *) h, h->ihl);
7681da177e4SLinus Torvalds 
7691da177e4SLinus Torvalds 	/* Construct UDP header */
7701da177e4SLinus Torvalds 	b->udph.source = htons(68);
7711da177e4SLinus Torvalds 	b->udph.dest = htons(67);
7721da177e4SLinus Torvalds 	b->udph.len = htons(sizeof(struct bootp_pkt) - sizeof(struct iphdr));
7731da177e4SLinus Torvalds 	/* UDP checksum not calculated -- explicitly allowed in BOOTP RFC */
7741da177e4SLinus Torvalds 
7751da177e4SLinus Torvalds 	/* Construct DHCP/BOOTP header */
7761da177e4SLinus Torvalds 	b->op = BOOTP_REQUEST;
7771da177e4SLinus Torvalds 	if (dev->type < 256) /* check for false types */
7781da177e4SLinus Torvalds 		b->htype = dev->type;
7791da177e4SLinus Torvalds 	else if (dev->type == ARPHRD_IEEE802_TR) /* fix for token ring */
7801da177e4SLinus Torvalds 		b->htype = ARPHRD_IEEE802;
7811da177e4SLinus Torvalds 	else if (dev->type == ARPHRD_FDDI)
7821da177e4SLinus Torvalds 		b->htype = ARPHRD_ETHER;
7831da177e4SLinus Torvalds 	else {
7841da177e4SLinus Torvalds 		printk("Unknown ARP type 0x%04x for device %s\n", dev->type, dev->name);
7851da177e4SLinus Torvalds 		b->htype = dev->type; /* can cause undefined behavior */
7861da177e4SLinus Torvalds 	}
787dea75bdfSStephen Hemminger 
788dea75bdfSStephen Hemminger 	/* server_ip and your_ip address are both already zero per RFC2131 */
7891da177e4SLinus Torvalds 	b->hlen = dev->addr_len;
7901da177e4SLinus Torvalds 	memcpy(b->hw_addr, dev->dev_addr, dev->addr_len);
7911da177e4SLinus Torvalds 	b->secs = htons(jiffies_diff / HZ);
7921da177e4SLinus Torvalds 	b->xid = d->xid;
7931da177e4SLinus Torvalds 
7941da177e4SLinus Torvalds 	/* add DHCP options or BOOTP extensions */
7951da177e4SLinus Torvalds #ifdef IPCONFIG_DHCP
7961da177e4SLinus Torvalds 	if (ic_proto_enabled & IC_USE_DHCP)
7971da177e4SLinus Torvalds 		ic_dhcp_init_options(b->exten);
7981da177e4SLinus Torvalds 	else
7991da177e4SLinus Torvalds #endif
8001da177e4SLinus Torvalds 		ic_bootp_init_ext(b->exten);
8011da177e4SLinus Torvalds 
8021da177e4SLinus Torvalds 	/* Chain packet down the line... */
8031da177e4SLinus Torvalds 	skb->dev = dev;
8041da177e4SLinus Torvalds 	skb->protocol = htons(ETH_P_IP);
8050c4e8581SStephen Hemminger 	if (dev_hard_header(skb, dev, ntohs(skb->protocol),
8060c4e8581SStephen Hemminger 			    dev->broadcast, dev->dev_addr, skb->len) < 0 ||
8071da177e4SLinus Torvalds 	    dev_queue_xmit(skb) < 0)
8081da177e4SLinus Torvalds 		printk("E");
8091da177e4SLinus Torvalds }
8101da177e4SLinus Torvalds 
8111da177e4SLinus Torvalds 
8121da177e4SLinus Torvalds /*
8131da177e4SLinus Torvalds  *  Copy BOOTP-supplied string if not already set.
8141da177e4SLinus Torvalds  */
8151da177e4SLinus Torvalds static int __init ic_bootp_string(char *dest, char *src, int len, int max)
8161da177e4SLinus Torvalds {
8171da177e4SLinus Torvalds 	if (!len)
8181da177e4SLinus Torvalds 		return 0;
8191da177e4SLinus Torvalds 	if (len > max-1)
8201da177e4SLinus Torvalds 		len = max-1;
8211da177e4SLinus Torvalds 	memcpy(dest, src, len);
8221da177e4SLinus Torvalds 	dest[len] = '\0';
8231da177e4SLinus Torvalds 	return 1;
8241da177e4SLinus Torvalds }
8251da177e4SLinus Torvalds 
8261da177e4SLinus Torvalds 
8271da177e4SLinus Torvalds /*
8281da177e4SLinus Torvalds  *  Process BOOTP extensions.
8291da177e4SLinus Torvalds  */
8301da177e4SLinus Torvalds static void __init ic_do_bootp_ext(u8 *ext)
8311da177e4SLinus Torvalds {
8321da177e4SLinus Torvalds        u8 servers;
8331da177e4SLinus Torvalds        int i;
8349643f455SChris Friesen 	u16 mtu;
8351da177e4SLinus Torvalds 
8361da177e4SLinus Torvalds #ifdef IPCONFIG_DEBUG
8371da177e4SLinus Torvalds 	u8 *c;
8381da177e4SLinus Torvalds 
8391da177e4SLinus Torvalds 	printk("DHCP/BOOTP: Got extension %d:",*ext);
8401da177e4SLinus Torvalds 	for (c=ext+2; c<ext+2+ext[1]; c++)
8411da177e4SLinus Torvalds 		printk(" %02x", *c);
8421da177e4SLinus Torvalds 	printk("\n");
8431da177e4SLinus Torvalds #endif
8441da177e4SLinus Torvalds 
8451da177e4SLinus Torvalds 	switch (*ext++) {
8461da177e4SLinus Torvalds 		case 1:		/* Subnet mask */
8475a874db4SAl Viro 			if (ic_netmask == NONE)
8481da177e4SLinus Torvalds 				memcpy(&ic_netmask, ext+1, 4);
8491da177e4SLinus Torvalds 			break;
8501da177e4SLinus Torvalds 		case 3:		/* Default gateway */
8515a874db4SAl Viro 			if (ic_gateway == NONE)
8521da177e4SLinus Torvalds 				memcpy(&ic_gateway, ext+1, 4);
8531da177e4SLinus Torvalds 			break;
8541da177e4SLinus Torvalds 		case 6:		/* DNS server */
8551da177e4SLinus Torvalds 			servers= *ext/4;
8561da177e4SLinus Torvalds 			if (servers > CONF_NAMESERVERS_MAX)
8571da177e4SLinus Torvalds 				servers = CONF_NAMESERVERS_MAX;
8581da177e4SLinus Torvalds 			for (i = 0; i < servers; i++) {
8595a874db4SAl Viro 				if (ic_nameservers[i] == NONE)
8601da177e4SLinus Torvalds 					memcpy(&ic_nameservers[i], ext+1+4*i, 4);
8611da177e4SLinus Torvalds 			}
8621da177e4SLinus Torvalds 			break;
8631da177e4SLinus Torvalds 		case 12:	/* Host name */
864e9ff3990SSerge E. Hallyn 			ic_bootp_string(utsname()->nodename, ext+1, *ext, __NEW_UTS_LEN);
8651da177e4SLinus Torvalds 			ic_host_name_set = 1;
8661da177e4SLinus Torvalds 			break;
8671da177e4SLinus Torvalds 		case 15:	/* Domain name (DNS) */
8681da177e4SLinus Torvalds 			ic_bootp_string(ic_domain, ext+1, *ext, sizeof(ic_domain));
8691da177e4SLinus Torvalds 			break;
8701da177e4SLinus Torvalds 		case 17:	/* Root path */
8711da177e4SLinus Torvalds 			if (!root_server_path[0])
8721da177e4SLinus Torvalds 				ic_bootp_string(root_server_path, ext+1, *ext, sizeof(root_server_path));
8731da177e4SLinus Torvalds 			break;
8749643f455SChris Friesen 		case 26:	/* Interface MTU */
8759643f455SChris Friesen 			memcpy(&mtu, ext+1, sizeof(mtu));
8769643f455SChris Friesen 			ic_dev_mtu = ntohs(mtu);
8779643f455SChris Friesen 			break;
8781da177e4SLinus Torvalds 		case 40:	/* NIS Domain name (_not_ DNS) */
879e9ff3990SSerge E. Hallyn 			ic_bootp_string(utsname()->domainname, ext+1, *ext, __NEW_UTS_LEN);
8801da177e4SLinus Torvalds 			break;
8811da177e4SLinus Torvalds 	}
8821da177e4SLinus Torvalds }
8831da177e4SLinus Torvalds 
8841da177e4SLinus Torvalds 
8851da177e4SLinus Torvalds /*
8861da177e4SLinus Torvalds  *  Receive BOOTP reply.
8871da177e4SLinus Torvalds  */
888f2ccd8faSDavid 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)
8891da177e4SLinus Torvalds {
8901da177e4SLinus Torvalds 	struct bootp_pkt *b;
8911da177e4SLinus Torvalds 	struct iphdr *h;
8921da177e4SLinus Torvalds 	struct ic_device *d;
8931da177e4SLinus Torvalds 	int len, ext_len;
8941da177e4SLinus Torvalds 
895721499e8SYOSHIFUJI Hideaki 	if (!net_eq(dev_net(dev), &init_net))
896e730c155SEric W. Biederman 		goto drop;
897e730c155SEric W. Biederman 
8981da177e4SLinus Torvalds 	/* Perform verifications before taking the lock.  */
8991da177e4SLinus Torvalds 	if (skb->pkt_type == PACKET_OTHERHOST)
9001da177e4SLinus Torvalds 		goto drop;
9011da177e4SLinus Torvalds 
9021da177e4SLinus Torvalds 	if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL)
9031da177e4SLinus Torvalds 		return NET_RX_DROP;
9041da177e4SLinus Torvalds 
9051da177e4SLinus Torvalds 	if (!pskb_may_pull(skb,
9061da177e4SLinus Torvalds 			   sizeof(struct iphdr) +
9071da177e4SLinus Torvalds 			   sizeof(struct udphdr)))
9081da177e4SLinus Torvalds 		goto drop;
9091da177e4SLinus Torvalds 
910eddc9ec5SArnaldo Carvalho de Melo 	b = (struct bootp_pkt *)skb_network_header(skb);
9111da177e4SLinus Torvalds 	h = &b->iph;
9121da177e4SLinus Torvalds 
9131da177e4SLinus Torvalds 	if (h->ihl != 5 || h->version != 4 || h->protocol != IPPROTO_UDP)
9141da177e4SLinus Torvalds 		goto drop;
9151da177e4SLinus Torvalds 
9161da177e4SLinus Torvalds 	/* Fragments are not supported */
9171da177e4SLinus Torvalds 	if (h->frag_off & htons(IP_OFFSET | IP_MF)) {
9181da177e4SLinus Torvalds 		if (net_ratelimit())
9191da177e4SLinus Torvalds 			printk(KERN_ERR "DHCP/BOOTP: Ignoring fragmented "
9201da177e4SLinus Torvalds 			       "reply.\n");
9211da177e4SLinus Torvalds 		goto drop;
9221da177e4SLinus Torvalds 	}
9231da177e4SLinus Torvalds 
9241da177e4SLinus Torvalds 	if (skb->len < ntohs(h->tot_len))
9251da177e4SLinus Torvalds 		goto drop;
9261da177e4SLinus Torvalds 
9271da177e4SLinus Torvalds 	if (ip_fast_csum((char *) h, h->ihl))
9281da177e4SLinus Torvalds 		goto drop;
9291da177e4SLinus Torvalds 
9301da177e4SLinus Torvalds 	if (b->udph.source != htons(67) || b->udph.dest != htons(68))
9311da177e4SLinus Torvalds 		goto drop;
9321da177e4SLinus Torvalds 
9331da177e4SLinus Torvalds 	if (ntohs(h->tot_len) < ntohs(b->udph.len) + sizeof(struct iphdr))
9341da177e4SLinus Torvalds 		goto drop;
9351da177e4SLinus Torvalds 
9361da177e4SLinus Torvalds 	len = ntohs(b->udph.len) - sizeof(struct udphdr);
9371da177e4SLinus Torvalds 	ext_len = len - (sizeof(*b) -
9381da177e4SLinus Torvalds 			 sizeof(struct iphdr) -
9391da177e4SLinus Torvalds 			 sizeof(struct udphdr) -
9401da177e4SLinus Torvalds 			 sizeof(b->exten));
9411da177e4SLinus Torvalds 	if (ext_len < 0)
9421da177e4SLinus Torvalds 		goto drop;
9431da177e4SLinus Torvalds 
9441da177e4SLinus Torvalds 	/* Ok the front looks good, make sure we can get at the rest.  */
9451da177e4SLinus Torvalds 	if (!pskb_may_pull(skb, skb->len))
9461da177e4SLinus Torvalds 		goto drop;
9471da177e4SLinus Torvalds 
948eddc9ec5SArnaldo Carvalho de Melo 	b = (struct bootp_pkt *)skb_network_header(skb);
9491da177e4SLinus Torvalds 	h = &b->iph;
9501da177e4SLinus Torvalds 
9511da177e4SLinus Torvalds 	/* One reply at a time, please. */
9521da177e4SLinus Torvalds 	spin_lock(&ic_recv_lock);
9531da177e4SLinus Torvalds 
9541da177e4SLinus Torvalds 	/* If we already have a reply, just drop the packet */
9551da177e4SLinus Torvalds 	if (ic_got_reply)
9561da177e4SLinus Torvalds 		goto drop_unlock;
9571da177e4SLinus Torvalds 
9581da177e4SLinus Torvalds 	/* Find the ic_device that the packet arrived on */
9591da177e4SLinus Torvalds 	d = ic_first_dev;
9601da177e4SLinus Torvalds 	while (d && d->dev != dev)
9611da177e4SLinus Torvalds 		d = d->next;
9621da177e4SLinus Torvalds 	if (!d)
9631da177e4SLinus Torvalds 		goto drop_unlock;  /* should never happen */
9641da177e4SLinus Torvalds 
9651da177e4SLinus Torvalds 	/* Is it a reply to our BOOTP request? */
9661da177e4SLinus Torvalds 	if (b->op != BOOTP_REPLY ||
9671da177e4SLinus Torvalds 	    b->xid != d->xid) {
9681da177e4SLinus Torvalds 		if (net_ratelimit())
9691da177e4SLinus Torvalds 			printk(KERN_ERR "DHCP/BOOTP: Reply not for us, "
9701da177e4SLinus Torvalds 			       "op[%x] xid[%x]\n",
9711da177e4SLinus Torvalds 			       b->op, b->xid);
9721da177e4SLinus Torvalds 		goto drop_unlock;
9731da177e4SLinus Torvalds 	}
9741da177e4SLinus Torvalds 
9752513dfb8SChris Friesen 	/* Is it a reply for the device we are configuring? */
9762513dfb8SChris Friesen 	if (b->xid != ic_dev_xid) {
9772513dfb8SChris Friesen 		if (net_ratelimit())
9782513dfb8SChris Friesen 			printk(KERN_ERR "DHCP/BOOTP: Ignoring delayed packet \n");
9792513dfb8SChris Friesen 		goto drop_unlock;
9802513dfb8SChris Friesen 	}
9812513dfb8SChris Friesen 
9821da177e4SLinus Torvalds 	/* Parse extensions */
9831da177e4SLinus Torvalds 	if (ext_len >= 4 &&
9841da177e4SLinus Torvalds 	    !memcmp(b->exten, ic_bootp_cookie, 4)) { /* Check magic cookie */
9851da177e4SLinus Torvalds 		u8 *end = (u8 *) b + ntohs(b->iph.tot_len);
9861da177e4SLinus Torvalds 		u8 *ext;
9871da177e4SLinus Torvalds 
9881da177e4SLinus Torvalds #ifdef IPCONFIG_DHCP
9891da177e4SLinus Torvalds 		if (ic_proto_enabled & IC_USE_DHCP) {
9905a874db4SAl Viro 			__be32 server_id = NONE;
9911da177e4SLinus Torvalds 			int mt = 0;
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 					break;
10011da177e4SLinus Torvalds 				switch (*opt) {
10021da177e4SLinus Torvalds 				case 53:	/* Message type */
10031da177e4SLinus Torvalds 					if (opt[1])
10041da177e4SLinus Torvalds 						mt = opt[2];
10051da177e4SLinus Torvalds 					break;
10061da177e4SLinus Torvalds 				case 54:	/* Server ID (IP address) */
10071da177e4SLinus Torvalds 					if (opt[1] >= 4)
10081da177e4SLinus Torvalds 						memcpy(&server_id, opt + 2, 4);
10091da177e4SLinus Torvalds 					break;
10103ff50b79SStephen Hemminger 				}
10111da177e4SLinus Torvalds 			}
10121da177e4SLinus Torvalds 
10131da177e4SLinus Torvalds #ifdef IPCONFIG_DEBUG
10141da177e4SLinus Torvalds 			printk("DHCP: Got message type %d\n", mt);
10151da177e4SLinus Torvalds #endif
10161da177e4SLinus Torvalds 
10171da177e4SLinus Torvalds 			switch (mt) {
10181da177e4SLinus Torvalds 			case DHCPOFFER:
10191da177e4SLinus Torvalds 				/* While in the process of accepting one offer,
10201da177e4SLinus Torvalds 				 * ignore all others.
10211da177e4SLinus Torvalds 				 */
10225a874db4SAl Viro 				if (ic_myaddr != NONE)
10231da177e4SLinus Torvalds 					goto drop_unlock;
10241da177e4SLinus Torvalds 
10251da177e4SLinus Torvalds 				/* Let's accept that offer. */
10261da177e4SLinus Torvalds 				ic_myaddr = b->your_ip;
10271da177e4SLinus Torvalds 				ic_servaddr = server_id;
10281da177e4SLinus Torvalds #ifdef IPCONFIG_DEBUG
1029673d57e7SHarvey Harrison 				printk("DHCP: Offered address %pI4 by server %pI4\n",
1030673d57e7SHarvey Harrison 				       &ic_myaddr, &ic_servaddr);
10311da177e4SLinus Torvalds #endif
10321da177e4SLinus Torvalds 				/* The DHCP indicated server address takes
10331da177e4SLinus Torvalds 				 * precedence over the bootp header one if
10341da177e4SLinus Torvalds 				 * they are different.
10351da177e4SLinus Torvalds 				 */
10365a874db4SAl Viro 				if ((server_id != NONE) &&
10371da177e4SLinus Torvalds 				    (b->server_ip != server_id))
10381da177e4SLinus Torvalds 					b->server_ip = ic_servaddr;
10391da177e4SLinus Torvalds 				break;
10401da177e4SLinus Torvalds 
10411da177e4SLinus Torvalds 			case DHCPACK:
10421da177e4SLinus Torvalds 				if (memcmp(dev->dev_addr, b->hw_addr, dev->addr_len) != 0)
10431da177e4SLinus Torvalds 					goto drop_unlock;
10441da177e4SLinus Torvalds 
10451da177e4SLinus Torvalds 				/* Yeah! */
10461da177e4SLinus Torvalds 				break;
10471da177e4SLinus Torvalds 
10481da177e4SLinus Torvalds 			default:
10491da177e4SLinus Torvalds 				/* Urque.  Forget it*/
10505a874db4SAl Viro 				ic_myaddr = NONE;
10515a874db4SAl Viro 				ic_servaddr = NONE;
10521da177e4SLinus Torvalds 				goto drop_unlock;
10533ff50b79SStephen Hemminger 			}
10541da177e4SLinus Torvalds 
10551da177e4SLinus Torvalds 			ic_dhcp_msgtype = mt;
10561da177e4SLinus Torvalds 
10571da177e4SLinus Torvalds 		}
10581da177e4SLinus Torvalds #endif /* IPCONFIG_DHCP */
10591da177e4SLinus Torvalds 
10601da177e4SLinus Torvalds 		ext = &b->exten[4];
10611da177e4SLinus Torvalds 		while (ext < end && *ext != 0xff) {
10621da177e4SLinus Torvalds 			u8 *opt = ext++;
10631da177e4SLinus Torvalds 			if (*opt == 0)	/* Padding */
10641da177e4SLinus Torvalds 				continue;
10651da177e4SLinus Torvalds 			ext += *ext + 1;
10661da177e4SLinus Torvalds 			if (ext < end)
10671da177e4SLinus Torvalds 				ic_do_bootp_ext(opt);
10681da177e4SLinus Torvalds 		}
10691da177e4SLinus Torvalds 	}
10701da177e4SLinus Torvalds 
10711da177e4SLinus Torvalds 	/* We have a winner! */
10721da177e4SLinus Torvalds 	ic_dev = dev;
10731da177e4SLinus Torvalds 	ic_myaddr = b->your_ip;
10741da177e4SLinus Torvalds 	ic_servaddr = b->server_ip;
10755a874db4SAl Viro 	if (ic_gateway == NONE && b->relay_ip)
10761da177e4SLinus Torvalds 		ic_gateway = b->relay_ip;
10775a874db4SAl Viro 	if (ic_nameservers[0] == NONE)
10781da177e4SLinus Torvalds 		ic_nameservers[0] = ic_servaddr;
10791da177e4SLinus Torvalds 	ic_got_reply = IC_BOOTP;
10801da177e4SLinus Torvalds 
10811da177e4SLinus Torvalds drop_unlock:
10821da177e4SLinus Torvalds 	/* Show's over.  Nothing to see here.  */
10831da177e4SLinus Torvalds 	spin_unlock(&ic_recv_lock);
10841da177e4SLinus Torvalds 
10851da177e4SLinus Torvalds drop:
10861da177e4SLinus Torvalds 	/* Throw the packet out. */
10871da177e4SLinus Torvalds 	kfree_skb(skb);
10881da177e4SLinus Torvalds 
10891da177e4SLinus Torvalds 	return 0;
10901da177e4SLinus Torvalds }
10911da177e4SLinus Torvalds 
10921da177e4SLinus Torvalds 
10931da177e4SLinus Torvalds #endif
10941da177e4SLinus Torvalds 
10951da177e4SLinus Torvalds 
10961da177e4SLinus Torvalds /*
10971da177e4SLinus Torvalds  *	Dynamic IP configuration -- DHCP, BOOTP, RARP.
10981da177e4SLinus Torvalds  */
10991da177e4SLinus Torvalds 
11001da177e4SLinus Torvalds #ifdef IPCONFIG_DYNAMIC
11011da177e4SLinus Torvalds 
11021da177e4SLinus Torvalds static int __init ic_dynamic(void)
11031da177e4SLinus Torvalds {
11041da177e4SLinus Torvalds 	int retries;
11051da177e4SLinus Torvalds 	struct ic_device *d;
11061da177e4SLinus Torvalds 	unsigned long start_jiffies, timeout, jiff;
11071da177e4SLinus Torvalds 	int do_bootp = ic_proto_have_if & IC_BOOTP;
11081da177e4SLinus Torvalds 	int do_rarp = ic_proto_have_if & IC_RARP;
11091da177e4SLinus Torvalds 
11101da177e4SLinus Torvalds 	/*
11111da177e4SLinus Torvalds 	 * If none of DHCP/BOOTP/RARP was selected, return with an error.
11121da177e4SLinus Torvalds 	 * This routine gets only called when some pieces of information
11131da177e4SLinus Torvalds 	 * are missing, and without DHCP/BOOTP/RARP we are unable to get it.
11141da177e4SLinus Torvalds 	 */
11151da177e4SLinus Torvalds 	if (!ic_proto_enabled) {
11161da177e4SLinus Torvalds 		printk(KERN_ERR "IP-Config: Incomplete network configuration information.\n");
11171da177e4SLinus Torvalds 		return -1;
11181da177e4SLinus Torvalds 	}
11191da177e4SLinus Torvalds 
11201da177e4SLinus Torvalds #ifdef IPCONFIG_BOOTP
11211da177e4SLinus Torvalds 	if ((ic_proto_enabled ^ ic_proto_have_if) & IC_BOOTP)
11221da177e4SLinus Torvalds 		printk(KERN_ERR "DHCP/BOOTP: No suitable device found.\n");
11231da177e4SLinus Torvalds #endif
11241da177e4SLinus Torvalds #ifdef IPCONFIG_RARP
11251da177e4SLinus Torvalds 	if ((ic_proto_enabled ^ ic_proto_have_if) & IC_RARP)
11261da177e4SLinus Torvalds 		printk(KERN_ERR "RARP: No suitable device found.\n");
11271da177e4SLinus Torvalds #endif
11281da177e4SLinus Torvalds 
11291da177e4SLinus Torvalds 	if (!ic_proto_have_if)
11301da177e4SLinus Torvalds 		/* Error message already printed */
11311da177e4SLinus Torvalds 		return -1;
11321da177e4SLinus Torvalds 
11331da177e4SLinus Torvalds 	/*
11341da177e4SLinus Torvalds 	 * Setup protocols
11351da177e4SLinus Torvalds 	 */
11361da177e4SLinus Torvalds #ifdef IPCONFIG_BOOTP
11371da177e4SLinus Torvalds 	if (do_bootp)
11381da177e4SLinus Torvalds 		ic_bootp_init();
11391da177e4SLinus Torvalds #endif
11401da177e4SLinus Torvalds #ifdef IPCONFIG_RARP
11411da177e4SLinus Torvalds 	if (do_rarp)
11421da177e4SLinus Torvalds 		ic_rarp_init();
11431da177e4SLinus Torvalds #endif
11441da177e4SLinus Torvalds 
11451da177e4SLinus Torvalds 	/*
11461da177e4SLinus Torvalds 	 * Send requests and wait, until we get an answer. This loop
11471da177e4SLinus Torvalds 	 * seems to be a terrible waste of CPU time, but actually there is
11481da177e4SLinus Torvalds 	 * only one process running at all, so we don't need to use any
11491da177e4SLinus Torvalds 	 * scheduler functions.
11501da177e4SLinus Torvalds 	 * [Actually we could now, but the nothing else running note still
11511da177e4SLinus Torvalds 	 *  applies.. - AC]
11521da177e4SLinus Torvalds 	 */
11531da177e4SLinus Torvalds 	printk(KERN_NOTICE "Sending %s%s%s requests .",
11541da177e4SLinus Torvalds 	       do_bootp
11551da177e4SLinus Torvalds 		? ((ic_proto_enabled & IC_USE_DHCP) ? "DHCP" : "BOOTP") : "",
11561da177e4SLinus Torvalds 	       (do_bootp && do_rarp) ? " and " : "",
11571da177e4SLinus Torvalds 	       do_rarp ? "RARP" : "");
11581da177e4SLinus Torvalds 
11591da177e4SLinus Torvalds 	start_jiffies = jiffies;
11601da177e4SLinus Torvalds 	d = ic_first_dev;
11611da177e4SLinus Torvalds 	retries = CONF_SEND_RETRIES;
11621da177e4SLinus Torvalds 	get_random_bytes(&timeout, sizeof(timeout));
11631da177e4SLinus Torvalds 	timeout = CONF_BASE_TIMEOUT + (timeout % (unsigned) CONF_TIMEOUT_RANDOM);
11641da177e4SLinus Torvalds 	for (;;) {
11652513dfb8SChris Friesen 		/* Track the device we are configuring */
11662513dfb8SChris Friesen 		ic_dev_xid = d->xid;
11672513dfb8SChris Friesen 
11681da177e4SLinus Torvalds #ifdef IPCONFIG_BOOTP
11691da177e4SLinus Torvalds 		if (do_bootp && (d->able & IC_BOOTP))
11701da177e4SLinus Torvalds 			ic_bootp_send_if(d, jiffies - start_jiffies);
11711da177e4SLinus Torvalds #endif
11721da177e4SLinus Torvalds #ifdef IPCONFIG_RARP
11731da177e4SLinus Torvalds 		if (do_rarp && (d->able & IC_RARP))
11741da177e4SLinus Torvalds 			ic_rarp_send_if(d);
11751da177e4SLinus Torvalds #endif
11761da177e4SLinus Torvalds 
11771da177e4SLinus Torvalds 		jiff = jiffies + (d->next ? CONF_INTER_TIMEOUT : timeout);
1178121caf57SNishanth Aravamudan 		while (time_before(jiffies, jiff) && !ic_got_reply)
1179121caf57SNishanth Aravamudan 			schedule_timeout_uninterruptible(1);
11801da177e4SLinus Torvalds #ifdef IPCONFIG_DHCP
11811da177e4SLinus Torvalds 		/* DHCP isn't done until we get a DHCPACK. */
11829d4fb27dSJoe Perches 		if ((ic_got_reply & IC_BOOTP) &&
11839d4fb27dSJoe Perches 		    (ic_proto_enabled & IC_USE_DHCP) &&
11849d4fb27dSJoe Perches 		    ic_dhcp_msgtype != DHCPACK) {
11851da177e4SLinus Torvalds 			ic_got_reply = 0;
11861da177e4SLinus Torvalds 			printk(",");
11871da177e4SLinus Torvalds 			continue;
11881da177e4SLinus Torvalds 		}
11891da177e4SLinus Torvalds #endif /* IPCONFIG_DHCP */
11901da177e4SLinus Torvalds 
11911da177e4SLinus Torvalds 		if (ic_got_reply) {
11921da177e4SLinus Torvalds 			printk(" OK\n");
11931da177e4SLinus Torvalds 			break;
11941da177e4SLinus Torvalds 		}
11951da177e4SLinus Torvalds 
11961da177e4SLinus Torvalds 		if ((d = d->next))
11971da177e4SLinus Torvalds 			continue;
11981da177e4SLinus Torvalds 
11991da177e4SLinus Torvalds 		if (! --retries) {
12001da177e4SLinus Torvalds 			printk(" timed out!\n");
12011da177e4SLinus Torvalds 			break;
12021da177e4SLinus Torvalds 		}
12031da177e4SLinus Torvalds 
12041da177e4SLinus Torvalds 		d = ic_first_dev;
12051da177e4SLinus Torvalds 
12061da177e4SLinus Torvalds 		timeout = timeout CONF_TIMEOUT_MULT;
12071da177e4SLinus Torvalds 		if (timeout > CONF_TIMEOUT_MAX)
12081da177e4SLinus Torvalds 			timeout = CONF_TIMEOUT_MAX;
12091da177e4SLinus Torvalds 
12101da177e4SLinus Torvalds 		printk(".");
12111da177e4SLinus Torvalds 	}
12121da177e4SLinus Torvalds 
12131da177e4SLinus Torvalds #ifdef IPCONFIG_BOOTP
12141da177e4SLinus Torvalds 	if (do_bootp)
12151da177e4SLinus Torvalds 		ic_bootp_cleanup();
12161da177e4SLinus Torvalds #endif
12171da177e4SLinus Torvalds #ifdef IPCONFIG_RARP
12181da177e4SLinus Torvalds 	if (do_rarp)
12191da177e4SLinus Torvalds 		ic_rarp_cleanup();
12201da177e4SLinus Torvalds #endif
12211da177e4SLinus Torvalds 
12227a1af5d7SMaxime Bizon 	if (!ic_got_reply) {
12235a874db4SAl Viro 		ic_myaddr = NONE;
12241da177e4SLinus Torvalds 		return -1;
12257a1af5d7SMaxime Bizon 	}
12261da177e4SLinus Torvalds 
1227673d57e7SHarvey Harrison 	printk("IP-Config: Got %s answer from %pI4, ",
12281da177e4SLinus Torvalds 		((ic_got_reply & IC_RARP) ? "RARP"
12291da177e4SLinus Torvalds 		 : (ic_proto_enabled & IC_USE_DHCP) ? "DHCP" : "BOOTP"),
1230673d57e7SHarvey Harrison 		&ic_servaddr);
1231673d57e7SHarvey Harrison 	printk("my address is %pI4\n", &ic_myaddr);
12321da177e4SLinus Torvalds 
12331da177e4SLinus Torvalds 	return 0;
12341da177e4SLinus Torvalds }
12351da177e4SLinus Torvalds 
12361da177e4SLinus Torvalds #endif /* IPCONFIG_DYNAMIC */
12371da177e4SLinus Torvalds 
12381da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
12391da177e4SLinus Torvalds 
12401da177e4SLinus Torvalds static int pnp_seq_show(struct seq_file *seq, void *v)
12411da177e4SLinus Torvalds {
12421da177e4SLinus Torvalds 	int i;
12431da177e4SLinus Torvalds 
12441da177e4SLinus Torvalds 	if (ic_proto_used & IC_PROTO)
12451da177e4SLinus Torvalds 		seq_printf(seq, "#PROTO: %s\n",
12461da177e4SLinus Torvalds 			   (ic_proto_used & IC_RARP) ? "RARP"
12471da177e4SLinus Torvalds 			   : (ic_proto_used & IC_USE_DHCP) ? "DHCP" : "BOOTP");
12481da177e4SLinus Torvalds 	else
12491da177e4SLinus Torvalds 		seq_puts(seq, "#MANUAL\n");
12501da177e4SLinus Torvalds 
12511da177e4SLinus Torvalds 	if (ic_domain[0])
12521da177e4SLinus Torvalds 		seq_printf(seq,
12531da177e4SLinus Torvalds 			   "domain %s\n", ic_domain);
12541da177e4SLinus Torvalds 	for (i = 0; i < CONF_NAMESERVERS_MAX; i++) {
12555a874db4SAl Viro 		if (ic_nameservers[i] != NONE)
1256673d57e7SHarvey Harrison 			seq_printf(seq, "nameserver %pI4\n",
1257673d57e7SHarvey Harrison 				   &ic_nameservers[i]);
12581da177e4SLinus Torvalds 	}
12595a874db4SAl Viro 	if (ic_servaddr != NONE)
1260673d57e7SHarvey Harrison 		seq_printf(seq, "bootserver %pI4\n",
1261673d57e7SHarvey Harrison 			   &ic_servaddr);
12621da177e4SLinus Torvalds 	return 0;
12631da177e4SLinus Torvalds }
12641da177e4SLinus Torvalds 
12651da177e4SLinus Torvalds static int pnp_seq_open(struct inode *indoe, struct file *file)
12661da177e4SLinus Torvalds {
12671da177e4SLinus Torvalds 	return single_open(file, pnp_seq_show, NULL);
12681da177e4SLinus Torvalds }
12691da177e4SLinus Torvalds 
12709a32144eSArjan van de Ven static const struct file_operations pnp_seq_fops = {
12711da177e4SLinus Torvalds 	.owner		= THIS_MODULE,
12721da177e4SLinus Torvalds 	.open		= pnp_seq_open,
12731da177e4SLinus Torvalds 	.read		= seq_read,
12741da177e4SLinus Torvalds 	.llseek		= seq_lseek,
12751da177e4SLinus Torvalds 	.release	= single_release,
12761da177e4SLinus Torvalds };
12771da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */
12781da177e4SLinus Torvalds 
12791da177e4SLinus Torvalds /*
12801da177e4SLinus Torvalds  *  Extract IP address from the parameter string if needed. Note that we
12811da177e4SLinus Torvalds  *  need to have root_server_addr set _before_ IPConfig gets called as it
12821da177e4SLinus Torvalds  *  can override it.
12831da177e4SLinus Torvalds  */
12845a874db4SAl Viro __be32 __init root_nfs_parse_addr(char *name)
12851da177e4SLinus Torvalds {
12865a874db4SAl Viro 	__be32 addr;
12871da177e4SLinus Torvalds 	int octets = 0;
12881da177e4SLinus Torvalds 	char *cp, *cq;
12891da177e4SLinus Torvalds 
12901da177e4SLinus Torvalds 	cp = cq = name;
12911da177e4SLinus Torvalds 	while (octets < 4) {
12921da177e4SLinus Torvalds 		while (*cp >= '0' && *cp <= '9')
12931da177e4SLinus Torvalds 			cp++;
12941da177e4SLinus Torvalds 		if (cp == cq || cp - cq > 3)
12951da177e4SLinus Torvalds 			break;
12961da177e4SLinus Torvalds 		if (*cp == '.' || octets == 3)
12971da177e4SLinus Torvalds 			octets++;
12981da177e4SLinus Torvalds 		if (octets < 4)
12991da177e4SLinus Torvalds 			cp++;
13001da177e4SLinus Torvalds 		cq = cp;
13011da177e4SLinus Torvalds 	}
13021da177e4SLinus Torvalds 	if (octets == 4 && (*cp == ':' || *cp == '\0')) {
13031da177e4SLinus Torvalds 		if (*cp == ':')
13041da177e4SLinus Torvalds 			*cp++ = '\0';
13051da177e4SLinus Torvalds 		addr = in_aton(name);
13061da177e4SLinus Torvalds 		memmove(name, cp, strlen(cp) + 1);
13071da177e4SLinus Torvalds 	} else
13085a874db4SAl Viro 		addr = NONE;
13091da177e4SLinus Torvalds 
13101da177e4SLinus Torvalds 	return addr;
13111da177e4SLinus Torvalds }
13121da177e4SLinus Torvalds 
1313*964ad81cSDavid S. Miller #define DEVICE_WAIT_MAX		12 /* 12 seconds */
1314*964ad81cSDavid S. Miller 
1315*964ad81cSDavid S. Miller static int __init wait_for_devices(void)
1316*964ad81cSDavid S. Miller {
1317*964ad81cSDavid S. Miller 	int i;
1318*964ad81cSDavid S. Miller 
1319*964ad81cSDavid S. Miller 	msleep(CONF_PRE_OPEN);
1320*964ad81cSDavid S. Miller 	for (i = 0; i < DEVICE_WAIT_MAX; i++) {
1321*964ad81cSDavid S. Miller 		struct net_device *dev;
1322*964ad81cSDavid S. Miller 		int found = 0;
1323*964ad81cSDavid S. Miller 
1324*964ad81cSDavid S. Miller 		rtnl_lock();
1325*964ad81cSDavid S. Miller 		for_each_netdev(&init_net, dev) {
1326*964ad81cSDavid S. Miller 			if (ic_device_match(dev)) {
1327*964ad81cSDavid S. Miller 				found = 1;
1328*964ad81cSDavid S. Miller 				break;
1329*964ad81cSDavid S. Miller 			}
1330*964ad81cSDavid S. Miller 		}
1331*964ad81cSDavid S. Miller 		rtnl_unlock();
1332*964ad81cSDavid S. Miller 		if (found)
1333*964ad81cSDavid S. Miller 			return 0;
1334*964ad81cSDavid S. Miller 		ssleep(1);
1335*964ad81cSDavid S. Miller 	}
1336*964ad81cSDavid S. Miller 	return -ENODEV;
1337*964ad81cSDavid S. Miller }
1338*964ad81cSDavid S. Miller 
13391da177e4SLinus Torvalds /*
13401da177e4SLinus Torvalds  *	IP Autoconfig dispatcher.
13411da177e4SLinus Torvalds  */
13421da177e4SLinus Torvalds 
13431da177e4SLinus Torvalds static int __init ip_auto_config(void)
13441da177e4SLinus Torvalds {
13455a874db4SAl Viro 	__be32 addr;
13469d8dba6cSBenjamin Zores #ifdef IPCONFIG_DYNAMIC
13479d8dba6cSBenjamin Zores 	int retries = CONF_OPEN_RETRIES;
13489d8dba6cSBenjamin Zores #endif
1349*964ad81cSDavid S. Miller 	int err;
13501da177e4SLinus Torvalds 
13511da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
1352457c4cbcSEric W. Biederman 	proc_net_fops_create(&init_net, "pnp", S_IRUGO, &pnp_seq_fops);
13531da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */
13541da177e4SLinus Torvalds 
13551da177e4SLinus Torvalds 	if (!ic_enable)
13561da177e4SLinus Torvalds 		return 0;
13571da177e4SLinus Torvalds 
13581da177e4SLinus Torvalds 	DBG(("IP-Config: Entered.\n"));
13591da177e4SLinus Torvalds #ifdef IPCONFIG_DYNAMIC
13601da177e4SLinus Torvalds  try_try_again:
13611da177e4SLinus Torvalds #endif
1362*964ad81cSDavid S. Miller 	/* Wait for devices to appear */
1363*964ad81cSDavid S. Miller 	err = wait_for_devices();
1364*964ad81cSDavid S. Miller 	if (err)
1365*964ad81cSDavid S. Miller 		return err;
13661da177e4SLinus Torvalds 
13671da177e4SLinus Torvalds 	/* Setup all network devices */
1368*964ad81cSDavid S. Miller 	err = ic_open_devs();
1369*964ad81cSDavid S. Miller 	if (err)
1370*964ad81cSDavid S. Miller 		return err;
13711da177e4SLinus Torvalds 
13721da177e4SLinus Torvalds 	/* Give drivers a chance to settle */
13731da177e4SLinus Torvalds 	ssleep(CONF_POST_OPEN);
13741da177e4SLinus Torvalds 
13751da177e4SLinus Torvalds 	/*
13761da177e4SLinus Torvalds 	 * If the config information is insufficient (e.g., our IP address or
13771da177e4SLinus Torvalds 	 * IP address of the boot server is missing or we have multiple network
13781da177e4SLinus Torvalds 	 * interfaces and no default was set), use BOOTP or RARP to get the
13791da177e4SLinus Torvalds 	 * missing values.
13801da177e4SLinus Torvalds 	 */
13815a874db4SAl Viro 	if (ic_myaddr == NONE ||
13821da177e4SLinus Torvalds #ifdef CONFIG_ROOT_NFS
13839d4fb27dSJoe Perches 	    (root_server_addr == NONE &&
13849d4fb27dSJoe Perches 	     ic_servaddr == NONE &&
13859d4fb27dSJoe Perches 	     ROOT_DEV == Root_NFS) ||
13861da177e4SLinus Torvalds #endif
13871da177e4SLinus Torvalds 	    ic_first_dev->next) {
13881da177e4SLinus Torvalds #ifdef IPCONFIG_DYNAMIC
13891da177e4SLinus Torvalds 		if (ic_dynamic() < 0) {
13901da177e4SLinus Torvalds 			ic_close_devs();
13911da177e4SLinus Torvalds 
13921da177e4SLinus Torvalds 			/*
13931da177e4SLinus Torvalds 			 * I don't know why, but sometimes the
13941da177e4SLinus Torvalds 			 * eepro100 driver (at least) gets upset and
13951da177e4SLinus Torvalds 			 * doesn't work the first time it's opened.
13961da177e4SLinus Torvalds 			 * But then if you close it and reopen it, it
13971da177e4SLinus Torvalds 			 * works just fine.  So we need to try that at
13981da177e4SLinus Torvalds 			 * least once before giving up.
13991da177e4SLinus Torvalds 			 *
14001da177e4SLinus Torvalds 			 * Also, if the root will be NFS-mounted, we
14011da177e4SLinus Torvalds 			 * have nowhere to go if DHCP fails.  So we
14021da177e4SLinus Torvalds 			 * just have to keep trying forever.
14031da177e4SLinus Torvalds 			 *
14041da177e4SLinus Torvalds 			 * 				-- Chip
14051da177e4SLinus Torvalds 			 */
14061da177e4SLinus Torvalds #ifdef CONFIG_ROOT_NFS
14071da177e4SLinus Torvalds 			if (ROOT_DEV ==  Root_NFS) {
14081da177e4SLinus Torvalds 				printk(KERN_ERR
14091da177e4SLinus Torvalds 					"IP-Config: Retrying forever (NFS root)...\n");
14101da177e4SLinus Torvalds 				goto try_try_again;
14111da177e4SLinus Torvalds 			}
14121da177e4SLinus Torvalds #endif
14131da177e4SLinus Torvalds 
14141da177e4SLinus Torvalds 			if (--retries) {
14151da177e4SLinus Torvalds 				printk(KERN_ERR
14161da177e4SLinus Torvalds 				       "IP-Config: Reopening network devices...\n");
14171da177e4SLinus Torvalds 				goto try_try_again;
14181da177e4SLinus Torvalds 			}
14191da177e4SLinus Torvalds 
14201da177e4SLinus Torvalds 			/* Oh, well.  At least we tried. */
14211da177e4SLinus Torvalds 			printk(KERN_ERR "IP-Config: Auto-configuration of network failed.\n");
14221da177e4SLinus Torvalds 			return -1;
14231da177e4SLinus Torvalds 		}
14241da177e4SLinus Torvalds #else /* !DYNAMIC */
14251da177e4SLinus Torvalds 		printk(KERN_ERR "IP-Config: Incomplete network configuration information.\n");
14261da177e4SLinus Torvalds 		ic_close_devs();
14271da177e4SLinus Torvalds 		return -1;
14281da177e4SLinus Torvalds #endif /* IPCONFIG_DYNAMIC */
14291da177e4SLinus Torvalds 	} else {
14301da177e4SLinus Torvalds 		/* Device selected manually or only one device -> use it */
14311da177e4SLinus Torvalds 		ic_dev = ic_first_dev->dev;
14321da177e4SLinus Torvalds 	}
14331da177e4SLinus Torvalds 
14341da177e4SLinus Torvalds 	addr = root_nfs_parse_addr(root_server_path);
14355a874db4SAl Viro 	if (root_server_addr == NONE)
14361da177e4SLinus Torvalds 		root_server_addr = addr;
14371da177e4SLinus Torvalds 
14381da177e4SLinus Torvalds 	/*
14391da177e4SLinus Torvalds 	 * Use defaults whereever applicable.
14401da177e4SLinus Torvalds 	 */
14411da177e4SLinus Torvalds 	if (ic_defaults() < 0)
14421da177e4SLinus Torvalds 		return -1;
14431da177e4SLinus Torvalds 
14441da177e4SLinus Torvalds 	/*
14451da177e4SLinus Torvalds 	 * Close all network devices except the device we've
14461da177e4SLinus Torvalds 	 * autoconfigured and set up routes.
14471da177e4SLinus Torvalds 	 */
14481da177e4SLinus Torvalds 	ic_close_devs();
14491da177e4SLinus Torvalds 	if (ic_setup_if() < 0 || ic_setup_routes() < 0)
14501da177e4SLinus Torvalds 		return -1;
14511da177e4SLinus Torvalds 
14521da177e4SLinus Torvalds 	/*
14531da177e4SLinus Torvalds 	 * Record which protocol was actually used.
14541da177e4SLinus Torvalds 	 */
14551da177e4SLinus Torvalds #ifdef IPCONFIG_DYNAMIC
14561da177e4SLinus Torvalds 	ic_proto_used = ic_got_reply | (ic_proto_enabled & IC_USE_DHCP);
14571da177e4SLinus Torvalds #endif
14581da177e4SLinus Torvalds 
14591da177e4SLinus Torvalds #ifndef IPCONFIG_SILENT
14601da177e4SLinus Torvalds 	/*
14611da177e4SLinus Torvalds 	 * Clue in the operator.
14621da177e4SLinus Torvalds 	 */
14631da177e4SLinus Torvalds 	printk("IP-Config: Complete:");
14641da177e4SLinus Torvalds 	printk("\n     device=%s", ic_dev->name);
1465673d57e7SHarvey Harrison 	printk(", addr=%pI4", &ic_myaddr);
1466673d57e7SHarvey Harrison 	printk(", mask=%pI4", &ic_netmask);
1467673d57e7SHarvey Harrison 	printk(", gw=%pI4", &ic_gateway);
14681da177e4SLinus Torvalds 	printk(",\n     host=%s, domain=%s, nis-domain=%s",
1469e9ff3990SSerge E. Hallyn 	       utsname()->nodename, ic_domain, utsname()->domainname);
1470673d57e7SHarvey Harrison 	printk(",\n     bootserver=%pI4", &ic_servaddr);
1471673d57e7SHarvey Harrison 	printk(", rootserver=%pI4", &root_server_addr);
14721da177e4SLinus Torvalds 	printk(", rootpath=%s", root_server_path);
14739643f455SChris Friesen 	if (ic_dev_mtu)
14749643f455SChris Friesen 		printk(", mtu=%d", ic_dev_mtu);
14751da177e4SLinus Torvalds 	printk("\n");
14761da177e4SLinus Torvalds #endif /* !SILENT */
14771da177e4SLinus Torvalds 
14781da177e4SLinus Torvalds 	return 0;
14791da177e4SLinus Torvalds }
14801da177e4SLinus Torvalds 
14811da177e4SLinus Torvalds late_initcall(ip_auto_config);
14821da177e4SLinus Torvalds 
14831da177e4SLinus Torvalds 
14841da177e4SLinus Torvalds /*
14851da177e4SLinus Torvalds  *  Decode any IP configuration options in the "ip=" or "nfsaddrs=" kernel
1486dc7a0816SJ. Bruce Fields  *  command line parameter.  See Documentation/filesystems/nfs/nfsroot.txt.
14871da177e4SLinus Torvalds  */
14881da177e4SLinus Torvalds static int __init ic_proto_name(char *name)
14891da177e4SLinus Torvalds {
14901da177e4SLinus Torvalds 	if (!strcmp(name, "on") || !strcmp(name, "any")) {
14911da177e4SLinus Torvalds 		return 1;
14921da177e4SLinus Torvalds 	}
1493a6c05c3dSSimon Horman 	if (!strcmp(name, "off") || !strcmp(name, "none")) {
149492ffb85dSAmos Waterland 		return 0;
1495a6c05c3dSSimon Horman 	}
14961da177e4SLinus Torvalds #ifdef CONFIG_IP_PNP_DHCP
14971da177e4SLinus Torvalds 	else if (!strcmp(name, "dhcp")) {
14981da177e4SLinus Torvalds 		ic_proto_enabled &= ~IC_RARP;
14991da177e4SLinus Torvalds 		return 1;
15001da177e4SLinus Torvalds 	}
15011da177e4SLinus Torvalds #endif
15021da177e4SLinus Torvalds #ifdef CONFIG_IP_PNP_BOOTP
15031da177e4SLinus Torvalds 	else if (!strcmp(name, "bootp")) {
15041da177e4SLinus Torvalds 		ic_proto_enabled &= ~(IC_RARP | IC_USE_DHCP);
15051da177e4SLinus Torvalds 		return 1;
15061da177e4SLinus Torvalds 	}
15071da177e4SLinus Torvalds #endif
15081da177e4SLinus Torvalds #ifdef CONFIG_IP_PNP_RARP
15091da177e4SLinus Torvalds 	else if (!strcmp(name, "rarp")) {
15101da177e4SLinus Torvalds 		ic_proto_enabled &= ~(IC_BOOTP | IC_USE_DHCP);
15111da177e4SLinus Torvalds 		return 1;
15121da177e4SLinus Torvalds 	}
15131da177e4SLinus Torvalds #endif
15141da177e4SLinus Torvalds #ifdef IPCONFIG_DYNAMIC
15151da177e4SLinus Torvalds 	else if (!strcmp(name, "both")) {
15161da177e4SLinus Torvalds 		ic_proto_enabled &= ~IC_USE_DHCP; /* backward compat :-( */
15171da177e4SLinus Torvalds 		return 1;
15181da177e4SLinus Torvalds 	}
15191da177e4SLinus Torvalds #endif
15201da177e4SLinus Torvalds 	return 0;
15211da177e4SLinus Torvalds }
15221da177e4SLinus Torvalds 
15231da177e4SLinus Torvalds static int __init ip_auto_config_setup(char *addrs)
15241da177e4SLinus Torvalds {
15251da177e4SLinus Torvalds 	char *cp, *ip, *dp;
15261da177e4SLinus Torvalds 	int num = 0;
15271da177e4SLinus Torvalds 
15281da177e4SLinus Torvalds 	ic_set_manually = 1;
15299cecd07cSSimon Horman 	ic_enable = 1;
15301da177e4SLinus Torvalds 
153192ffb85dSAmos Waterland 	/*
153292ffb85dSAmos Waterland 	 * If any dhcp, bootp etc options are set, leave autoconfig on
153392ffb85dSAmos Waterland 	 * and skip the below static IP processing.
153492ffb85dSAmos Waterland 	 */
15351da177e4SLinus Torvalds 	if (ic_proto_name(addrs))
15361da177e4SLinus Torvalds 		return 1;
15371da177e4SLinus Torvalds 
153892ffb85dSAmos Waterland 	/* If no static IP is given, turn off autoconfig and bail.  */
153992ffb85dSAmos Waterland 	if (*addrs == 0 ||
154092ffb85dSAmos Waterland 	    strcmp(addrs, "off") == 0 ||
154192ffb85dSAmos Waterland 	    strcmp(addrs, "none") == 0) {
154292ffb85dSAmos Waterland 		ic_enable = 0;
154392ffb85dSAmos Waterland 		return 1;
154492ffb85dSAmos Waterland 	}
154592ffb85dSAmos Waterland 
154692ffb85dSAmos Waterland 	/* Parse string for static IP assignment.  */
15471da177e4SLinus Torvalds 	ip = addrs;
15481da177e4SLinus Torvalds 	while (ip && *ip) {
15491da177e4SLinus Torvalds 		if ((cp = strchr(ip, ':')))
15501da177e4SLinus Torvalds 			*cp++ = '\0';
15511da177e4SLinus Torvalds 		if (strlen(ip) > 0) {
15521da177e4SLinus Torvalds 			DBG(("IP-Config: Parameter #%d: `%s'\n", num, ip));
15531da177e4SLinus Torvalds 			switch (num) {
15541da177e4SLinus Torvalds 			case 0:
1555e6f1cebfSAl Viro 				if ((ic_myaddr = in_aton(ip)) == ANY)
15565a874db4SAl Viro 					ic_myaddr = NONE;
15571da177e4SLinus Torvalds 				break;
15581da177e4SLinus Torvalds 			case 1:
1559e6f1cebfSAl Viro 				if ((ic_servaddr = in_aton(ip)) == ANY)
15605a874db4SAl Viro 					ic_servaddr = NONE;
15611da177e4SLinus Torvalds 				break;
15621da177e4SLinus Torvalds 			case 2:
1563e6f1cebfSAl Viro 				if ((ic_gateway = in_aton(ip)) == ANY)
15645a874db4SAl Viro 					ic_gateway = NONE;
15651da177e4SLinus Torvalds 				break;
15661da177e4SLinus Torvalds 			case 3:
1567e6f1cebfSAl Viro 				if ((ic_netmask = in_aton(ip)) == ANY)
15685a874db4SAl Viro 					ic_netmask = NONE;
15691da177e4SLinus Torvalds 				break;
15701da177e4SLinus Torvalds 			case 4:
15711da177e4SLinus Torvalds 				if ((dp = strchr(ip, '.'))) {
15721da177e4SLinus Torvalds 					*dp++ = '\0';
1573e9ff3990SSerge E. Hallyn 					strlcpy(utsname()->domainname, dp,
1574e9ff3990SSerge E. Hallyn 						sizeof(utsname()->domainname));
15751da177e4SLinus Torvalds 				}
1576e9ff3990SSerge E. Hallyn 				strlcpy(utsname()->nodename, ip,
1577e9ff3990SSerge E. Hallyn 					sizeof(utsname()->nodename));
15781da177e4SLinus Torvalds 				ic_host_name_set = 1;
15791da177e4SLinus Torvalds 				break;
15801da177e4SLinus Torvalds 			case 5:
15811da177e4SLinus Torvalds 				strlcpy(user_dev_name, ip, sizeof(user_dev_name));
15821da177e4SLinus Torvalds 				break;
15831da177e4SLinus Torvalds 			case 6:
158492ffb85dSAmos Waterland 				if (ic_proto_name(ip) == 0 &&
158592ffb85dSAmos Waterland 				    ic_myaddr == NONE) {
158692ffb85dSAmos Waterland 					ic_enable = 0;
158792ffb85dSAmos Waterland 				}
15881da177e4SLinus Torvalds 				break;
15891da177e4SLinus Torvalds 			}
15901da177e4SLinus Torvalds 		}
15911da177e4SLinus Torvalds 		ip = cp;
15921da177e4SLinus Torvalds 		num++;
15931da177e4SLinus Torvalds 	}
15941da177e4SLinus Torvalds 
15951da177e4SLinus Torvalds 	return 1;
15961da177e4SLinus Torvalds }
15971da177e4SLinus Torvalds 
15981da177e4SLinus Torvalds static int __init nfsaddrs_config_setup(char *addrs)
15991da177e4SLinus Torvalds {
16001da177e4SLinus Torvalds 	return ip_auto_config_setup(addrs);
16011da177e4SLinus Torvalds }
16021da177e4SLinus Torvalds 
160362013dbbSRainer Jochem static int __init vendor_class_identifier_setup(char *addrs)
160462013dbbSRainer Jochem {
160562013dbbSRainer Jochem 	if (strlcpy(vendor_class_identifier, addrs,
160662013dbbSRainer Jochem 		    sizeof(vendor_class_identifier))
160762013dbbSRainer Jochem 	    >= sizeof(vendor_class_identifier))
160862013dbbSRainer Jochem 		printk(KERN_WARNING "DHCP: vendorclass too long, truncated to \"%s\"",
160962013dbbSRainer Jochem 		       vendor_class_identifier);
161062013dbbSRainer Jochem 	return 1;
161162013dbbSRainer Jochem }
161262013dbbSRainer Jochem 
16131da177e4SLinus Torvalds __setup("ip=", ip_auto_config_setup);
16141da177e4SLinus Torvalds __setup("nfsaddrs=", nfsaddrs_config_setup);
161562013dbbSRainer Jochem __setup("dhcpclass=", vendor_class_identifier_setup);
1616