xref: /linux/net/ipv4/ipconfig.c (revision d2d371ae5dd6af9a6a3d7f50b753627c42868409)
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>
565a0e3ad6STejun Heo #include <linux/slab.h>
57bc3b2d7fSPaul Gortmaker #include <linux/export.h>
58457c4cbcSEric W. Biederman #include <net/net_namespace.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 #if defined(CONFIG_IP_PNP_DHCP)
691da177e4SLinus Torvalds #define IPCONFIG_DHCP
701da177e4SLinus Torvalds #endif
711da177e4SLinus Torvalds #if defined(CONFIG_IP_PNP_BOOTP) || defined(CONFIG_IP_PNP_DHCP)
721da177e4SLinus Torvalds #define IPCONFIG_BOOTP
731da177e4SLinus Torvalds #endif
741da177e4SLinus Torvalds #if defined(CONFIG_IP_PNP_RARP)
751da177e4SLinus Torvalds #define IPCONFIG_RARP
761da177e4SLinus Torvalds #endif
771da177e4SLinus Torvalds #if defined(IPCONFIG_BOOTP) || defined(IPCONFIG_RARP)
781da177e4SLinus Torvalds #define IPCONFIG_DYNAMIC
791da177e4SLinus Torvalds #endif
801da177e4SLinus Torvalds 
811da177e4SLinus Torvalds /* Define the friendly delay before and after opening net devices */
823fb72f1eSMicha Nelissen #define CONF_POST_OPEN		10	/* After opening: 10 msecs */
833fb72f1eSMicha Nelissen #define CONF_CARRIER_TIMEOUT	120000	/* Wait for carrier timeout */
841da177e4SLinus Torvalds 
851da177e4SLinus Torvalds /* Define the timeout for waiting for a DHCP/BOOTP/RARP reply */
861da177e4SLinus Torvalds #define CONF_OPEN_RETRIES 	2	/* (Re)open devices twice */
871da177e4SLinus Torvalds #define CONF_SEND_RETRIES 	6	/* Send six requests per open */
881da177e4SLinus Torvalds #define CONF_BASE_TIMEOUT	(HZ*2)	/* Initial timeout: 2 seconds */
891da177e4SLinus Torvalds #define CONF_TIMEOUT_RANDOM	(HZ)	/* Maximum amount of randomization */
901da177e4SLinus Torvalds #define CONF_TIMEOUT_MULT	*7/4	/* Rate of timeout growth */
911da177e4SLinus Torvalds #define CONF_TIMEOUT_MAX	(HZ*30)	/* Maximum allowed timeout */
921da177e4SLinus Torvalds #define CONF_NAMESERVERS_MAX   3       /* Maximum number of nameservers
931da177e4SLinus Torvalds 					   - '3' from resolv.h */
941da177e4SLinus Torvalds 
9509640e63SHarvey Harrison #define NONE cpu_to_be32(INADDR_NONE)
9609640e63SHarvey Harrison #define ANY cpu_to_be32(INADDR_ANY)
971da177e4SLinus Torvalds 
981da177e4SLinus Torvalds /*
991da177e4SLinus Torvalds  * Public IP configuration
1001da177e4SLinus Torvalds  */
1011da177e4SLinus Torvalds 
1021da177e4SLinus Torvalds /* This is used by platforms which might be able to set the ipconfig
1031da177e4SLinus Torvalds  * variables using firmware environment vars.  If this is set, it will
1041da177e4SLinus Torvalds  * ignore such firmware variables.
1051da177e4SLinus Torvalds  */
1061da177e4SLinus Torvalds int ic_set_manually __initdata = 0;		/* IPconfig parameters set manually */
1071da177e4SLinus Torvalds 
1080d3979b9SFabian Frederick static int ic_enable __initdata;		/* IP config enabled? */
1091da177e4SLinus Torvalds 
1101da177e4SLinus Torvalds /* Protocol choice */
1111da177e4SLinus Torvalds int ic_proto_enabled __initdata = 0
1121da177e4SLinus Torvalds #ifdef IPCONFIG_BOOTP
1131da177e4SLinus Torvalds 			| IC_BOOTP
1141da177e4SLinus Torvalds #endif
1151da177e4SLinus Torvalds #ifdef CONFIG_IP_PNP_DHCP
1161da177e4SLinus Torvalds 			| IC_USE_DHCP
1171da177e4SLinus Torvalds #endif
1181da177e4SLinus Torvalds #ifdef IPCONFIG_RARP
1191da177e4SLinus Torvalds 			| IC_RARP
1201da177e4SLinus Torvalds #endif
1211da177e4SLinus Torvalds 			;
1221da177e4SLinus Torvalds 
1230d3979b9SFabian Frederick static int ic_host_name_set __initdata;	/* Host name set by us? */
1241da177e4SLinus Torvalds 
1255a874db4SAl Viro __be32 ic_myaddr = NONE;		/* My IP address */
1265a874db4SAl Viro static __be32 ic_netmask = NONE;	/* Netmask for local subnet */
1275a874db4SAl Viro __be32 ic_gateway = NONE;	/* Gateway IP address */
1281da177e4SLinus Torvalds 
12986ef7f9cSDavid S. Miller #ifdef IPCONFIG_DYNAMIC
1300b392be9SBen Dooks static __be32 ic_addrservaddr = NONE;	/* IP Address of the IP addresses'server */
13186ef7f9cSDavid S. Miller #endif
1329dd4a13aSPhilippe De Muyter 
1335a874db4SAl Viro __be32 ic_servaddr = NONE;	/* Boot server IP address */
1341da177e4SLinus Torvalds 
1355a874db4SAl Viro __be32 root_server_addr = NONE;	/* Address of NFS server */
1361da177e4SLinus Torvalds u8 root_server_path[256] = { 0, };	/* Path to mount as root */
1371da177e4SLinus Torvalds 
1381b0b04f9SDavid S. Miller /* vendor class identifier */
1391b0b04f9SDavid S. Miller static char vendor_class_identifier[253] __initdata;
14062013dbbSRainer Jochem 
14126fb342cSLi RongQing #if defined(CONFIG_IP_PNP_DHCP)
14226fb342cSLi RongQing static char dhcp_client_identifier[253] __initdata;
14326fb342cSLi RongQing #endif
14426fb342cSLi RongQing 
1451da177e4SLinus Torvalds /* Persistent data: */
1461da177e4SLinus Torvalds 
14752b79e2bSArnd Bergmann #ifdef IPCONFIG_DYNAMIC
1481da177e4SLinus Torvalds static int ic_proto_used;			/* Protocol used, if any */
14952b79e2bSArnd Bergmann #else
15052b79e2bSArnd Bergmann #define ic_proto_used 0
15152b79e2bSArnd Bergmann #endif
1525a874db4SAl Viro static __be32 ic_nameservers[CONF_NAMESERVERS_MAX]; /* DNS Server IP addresses */
1531da177e4SLinus Torvalds static u8 ic_domain[64];		/* DNS (not NIS) domain name */
1541da177e4SLinus Torvalds 
1551da177e4SLinus Torvalds /*
1561da177e4SLinus Torvalds  * Private state.
1571da177e4SLinus Torvalds  */
1581da177e4SLinus Torvalds 
1591da177e4SLinus Torvalds /* Name of user-selected boot device */
1601da177e4SLinus Torvalds static char user_dev_name[IFNAMSIZ] __initdata = { 0, };
1611da177e4SLinus Torvalds 
1621da177e4SLinus Torvalds /* Protocols supported by available interfaces */
1630d3979b9SFabian Frederick static int ic_proto_have_if __initdata;
1641da177e4SLinus Torvalds 
1659643f455SChris Friesen /* MTU for boot device */
1660d3979b9SFabian Frederick static int ic_dev_mtu __initdata;
1679643f455SChris Friesen 
1681da177e4SLinus Torvalds #ifdef IPCONFIG_DYNAMIC
1691da177e4SLinus Torvalds static DEFINE_SPINLOCK(ic_recv_lock);
1700d3979b9SFabian Frederick static volatile int ic_got_reply __initdata;    /* Proto(s) that replied */
1711da177e4SLinus Torvalds #endif
1721da177e4SLinus Torvalds #ifdef IPCONFIG_DHCP
1730d3979b9SFabian Frederick static int ic_dhcp_msgtype __initdata;	/* DHCP msg type received */
1741da177e4SLinus Torvalds #endif
1751da177e4SLinus Torvalds 
1761da177e4SLinus Torvalds 
1771da177e4SLinus Torvalds /*
1781da177e4SLinus Torvalds  *	Network devices
1791da177e4SLinus Torvalds  */
1801da177e4SLinus Torvalds 
1811da177e4SLinus Torvalds struct ic_device {
1821da177e4SLinus Torvalds 	struct ic_device *next;
1831da177e4SLinus Torvalds 	struct net_device *dev;
1841da177e4SLinus Torvalds 	unsigned short flags;
1851da177e4SLinus Torvalds 	short able;
1865a874db4SAl Viro 	__be32 xid;
1871da177e4SLinus Torvalds };
1881da177e4SLinus Torvalds 
1890d3979b9SFabian Frederick static struct ic_device *ic_first_dev __initdata;	/* List of open device */
1902647cffbSUwe Kleine-König static struct ic_device *ic_dev __initdata;		/* Selected device */
1911da177e4SLinus Torvalds 
1923fb72f1eSMicha Nelissen static bool __init ic_is_init_dev(struct net_device *dev)
193964ad81cSDavid S. Miller {
1943fb72f1eSMicha Nelissen 	if (dev->flags & IFF_LOOPBACK)
1953fb72f1eSMicha Nelissen 		return false;
1963fb72f1eSMicha Nelissen 	return user_dev_name[0] ? !strcmp(dev->name, user_dev_name) :
197964ad81cSDavid S. Miller 	    (!(dev->flags & IFF_LOOPBACK) &&
198964ad81cSDavid S. Miller 	     (dev->flags & (IFF_POINTOPOINT|IFF_BROADCAST)) &&
1993fb72f1eSMicha Nelissen 	     strncmp(dev->name, "dummy", 5));
200964ad81cSDavid S. Miller }
201964ad81cSDavid S. Miller 
2021da177e4SLinus Torvalds static int __init ic_open_devs(void)
2031da177e4SLinus Torvalds {
2041da177e4SLinus Torvalds 	struct ic_device *d, **last;
2051da177e4SLinus Torvalds 	struct net_device *dev;
2061da177e4SLinus Torvalds 	unsigned short oflags;
2075e404cd6SPaul Gortmaker 	unsigned long start, next_msg;
2081da177e4SLinus Torvalds 
2091da177e4SLinus Torvalds 	last = &ic_first_dev;
2106756ae4bSStephen Hemminger 	rtnl_lock();
2111da177e4SLinus Torvalds 
212728c0208SFlorian Fainelli 	/* bring loopback and DSA master network devices up first */
2130cc217e1SEric W. Biederman 	for_each_netdev(&init_net, dev) {
214728c0208SFlorian Fainelli 		if (!(dev->flags & IFF_LOOPBACK) && !netdev_uses_dsa(dev))
2150cc217e1SEric W. Biederman 			continue;
2160cc217e1SEric W. Biederman 		if (dev_change_flags(dev, dev->flags | IFF_UP) < 0)
217058bd4d2SJoe Perches 			pr_err("IP-Config: Failed to open %s\n", dev->name);
2180cc217e1SEric W. Biederman 	}
2191da177e4SLinus Torvalds 
220881d966bSEric W. Biederman 	for_each_netdev(&init_net, dev) {
2213fb72f1eSMicha Nelissen 		if (ic_is_init_dev(dev)) {
2221da177e4SLinus Torvalds 			int able = 0;
2231da177e4SLinus Torvalds 			if (dev->mtu >= 364)
2241da177e4SLinus Torvalds 				able |= IC_BOOTP;
2251da177e4SLinus Torvalds 			else
22609605cc1SBastian Stender 				pr_warn("DHCP/BOOTP: Ignoring device %s, MTU %d too small\n",
227058bd4d2SJoe Perches 					dev->name, dev->mtu);
2281da177e4SLinus Torvalds 			if (!(dev->flags & IFF_NOARP))
2291da177e4SLinus Torvalds 				able |= IC_RARP;
2301da177e4SLinus Torvalds 			able &= ic_proto_enabled;
2311da177e4SLinus Torvalds 			if (ic_proto_enabled && !able)
2321da177e4SLinus Torvalds 				continue;
2331da177e4SLinus Torvalds 			oflags = dev->flags;
2341da177e4SLinus Torvalds 			if (dev_change_flags(dev, oflags | IFF_UP) < 0) {
235058bd4d2SJoe Perches 				pr_err("IP-Config: Failed to open %s\n",
236058bd4d2SJoe Perches 				       dev->name);
2371da177e4SLinus Torvalds 				continue;
2381da177e4SLinus Torvalds 			}
2391da177e4SLinus Torvalds 			if (!(d = kmalloc(sizeof(struct ic_device), GFP_KERNEL))) {
2406756ae4bSStephen Hemminger 				rtnl_unlock();
241964ad81cSDavid S. Miller 				return -ENOMEM;
2421da177e4SLinus Torvalds 			}
2431da177e4SLinus Torvalds 			d->dev = dev;
2441da177e4SLinus Torvalds 			*last = d;
2451da177e4SLinus Torvalds 			last = &d->next;
2461da177e4SLinus Torvalds 			d->flags = oflags;
2471da177e4SLinus Torvalds 			d->able = able;
2481da177e4SLinus Torvalds 			if (able & IC_BOOTP)
2495a874db4SAl Viro 				get_random_bytes(&d->xid, sizeof(__be32));
2501da177e4SLinus Torvalds 			else
2511da177e4SLinus Torvalds 				d->xid = 0;
2521da177e4SLinus Torvalds 			ic_proto_have_if |= able;
25309605cc1SBastian Stender 			pr_debug("IP-Config: %s UP (able=%d, xid=%08x)\n",
25409605cc1SBastian Stender 				 dev->name, able, d->xid);
2551da177e4SLinus Torvalds 		}
2561da177e4SLinus Torvalds 	}
2573fb72f1eSMicha Nelissen 
258cd7816d1SGerlando Falauto 	/* no point in waiting if we could not bring up at least one device */
259cd7816d1SGerlando Falauto 	if (!ic_first_dev)
260cd7816d1SGerlando Falauto 		goto have_carrier;
261cd7816d1SGerlando Falauto 
2623fb72f1eSMicha Nelissen 	/* wait for a carrier on at least one device */
2633fb72f1eSMicha Nelissen 	start = jiffies;
2645e404cd6SPaul Gortmaker 	next_msg = start + msecs_to_jiffies(CONF_CARRIER_TIMEOUT/12);
265c72c95a0SHimangi Saraogi 	while (time_before(jiffies, start +
266c72c95a0SHimangi Saraogi 			   msecs_to_jiffies(CONF_CARRIER_TIMEOUT))) {
2675e404cd6SPaul Gortmaker 		int wait, elapsed;
2685e404cd6SPaul Gortmaker 
2693fb72f1eSMicha Nelissen 		for_each_netdev(&init_net, dev)
2703fb72f1eSMicha Nelissen 			if (ic_is_init_dev(dev) && netif_carrier_ok(dev))
2713fb72f1eSMicha Nelissen 				goto have_carrier;
2723fb72f1eSMicha Nelissen 
2733fb72f1eSMicha Nelissen 		msleep(1);
2745e404cd6SPaul Gortmaker 
275357137a4SFX Le Bail 		if (time_before(jiffies, next_msg))
2765e404cd6SPaul Gortmaker 			continue;
2775e404cd6SPaul Gortmaker 
2785e404cd6SPaul Gortmaker 		elapsed = jiffies_to_msecs(jiffies - start);
2795e404cd6SPaul Gortmaker 		wait = (CONF_CARRIER_TIMEOUT - elapsed + 500)/1000;
2805e404cd6SPaul Gortmaker 		pr_info("Waiting up to %d more seconds for network.\n", wait);
2815e404cd6SPaul Gortmaker 		next_msg = jiffies + msecs_to_jiffies(CONF_CARRIER_TIMEOUT/12);
2823fb72f1eSMicha Nelissen 	}
2833fb72f1eSMicha Nelissen have_carrier:
2846756ae4bSStephen Hemminger 	rtnl_unlock();
2851da177e4SLinus Torvalds 
2861da177e4SLinus Torvalds 	*last = NULL;
2871da177e4SLinus Torvalds 
2881da177e4SLinus Torvalds 	if (!ic_first_dev) {
2891da177e4SLinus Torvalds 		if (user_dev_name[0])
290058bd4d2SJoe Perches 			pr_err("IP-Config: Device `%s' not found\n",
291058bd4d2SJoe Perches 			       user_dev_name);
2921da177e4SLinus Torvalds 		else
293058bd4d2SJoe Perches 			pr_err("IP-Config: No network devices available\n");
294964ad81cSDavid S. Miller 		return -ENODEV;
2951da177e4SLinus Torvalds 	}
2961da177e4SLinus Torvalds 	return 0;
2971da177e4SLinus Torvalds }
2981da177e4SLinus Torvalds 
2991da177e4SLinus Torvalds static void __init ic_close_devs(void)
3001da177e4SLinus Torvalds {
3011da177e4SLinus Torvalds 	struct ic_device *d, *next;
3021da177e4SLinus Torvalds 	struct net_device *dev;
3031da177e4SLinus Torvalds 
3046756ae4bSStephen Hemminger 	rtnl_lock();
3051da177e4SLinus Torvalds 	next = ic_first_dev;
3061da177e4SLinus Torvalds 	while ((d = next)) {
3071da177e4SLinus Torvalds 		next = d->next;
3081da177e4SLinus Torvalds 		dev = d->dev;
3092647cffbSUwe Kleine-König 		if (dev != ic_dev->dev && !netdev_uses_dsa(dev)) {
31009605cc1SBastian Stender 			pr_debug("IP-Config: Downing %s\n", dev->name);
3111da177e4SLinus Torvalds 			dev_change_flags(dev, d->flags);
3121da177e4SLinus Torvalds 		}
3131da177e4SLinus Torvalds 		kfree(d);
3141da177e4SLinus Torvalds 	}
3156756ae4bSStephen Hemminger 	rtnl_unlock();
3161da177e4SLinus Torvalds }
3171da177e4SLinus Torvalds 
3181da177e4SLinus Torvalds /*
3191da177e4SLinus Torvalds  *	Interface to various network functions.
3201da177e4SLinus Torvalds  */
3211da177e4SLinus Torvalds 
3221da177e4SLinus Torvalds static inline void
3235a874db4SAl Viro set_sockaddr(struct sockaddr_in *sin, __be32 addr, __be16 port)
3241da177e4SLinus Torvalds {
3251da177e4SLinus Torvalds 	sin->sin_family = AF_INET;
3261da177e4SLinus Torvalds 	sin->sin_addr.s_addr = addr;
3271da177e4SLinus Torvalds 	sin->sin_port = port;
3281da177e4SLinus Torvalds }
3291da177e4SLinus Torvalds 
3309643f455SChris Friesen static int __init ic_devinet_ioctl(unsigned int cmd, struct ifreq *arg)
3311da177e4SLinus Torvalds {
3321da177e4SLinus Torvalds 	int res;
3331da177e4SLinus Torvalds 
3341da177e4SLinus Torvalds 	mm_segment_t oldfs = get_fs();
3351da177e4SLinus Torvalds 	set_fs(get_ds());
336e5b13cb1SDenis V. Lunev 	res = devinet_ioctl(&init_net, cmd, (struct ifreq __user *) arg);
3371da177e4SLinus Torvalds 	set_fs(oldfs);
3381da177e4SLinus Torvalds 	return res;
3391da177e4SLinus Torvalds }
3401da177e4SLinus Torvalds 
3419643f455SChris Friesen static int __init ic_dev_ioctl(unsigned int cmd, struct ifreq *arg)
3429643f455SChris Friesen {
3439643f455SChris Friesen 	int res;
3449643f455SChris Friesen 
3459643f455SChris Friesen 	mm_segment_t oldfs = get_fs();
3469643f455SChris Friesen 	set_fs(get_ds());
3479643f455SChris Friesen 	res = dev_ioctl(&init_net, cmd, (struct ifreq __user *) arg);
3489643f455SChris Friesen 	set_fs(oldfs);
3499643f455SChris Friesen 	return res;
3509643f455SChris Friesen }
3519643f455SChris Friesen 
3521da177e4SLinus Torvalds static int __init ic_route_ioctl(unsigned int cmd, struct rtentry *arg)
3531da177e4SLinus Torvalds {
3541da177e4SLinus Torvalds 	int res;
3551da177e4SLinus Torvalds 
3561da177e4SLinus Torvalds 	mm_segment_t oldfs = get_fs();
3571da177e4SLinus Torvalds 	set_fs(get_ds());
3581bad118aSDenis V. Lunev 	res = ip_rt_ioctl(&init_net, cmd, (void __user *) arg);
3591da177e4SLinus Torvalds 	set_fs(oldfs);
3601da177e4SLinus Torvalds 	return res;
3611da177e4SLinus Torvalds }
3621da177e4SLinus Torvalds 
3631da177e4SLinus Torvalds /*
3641da177e4SLinus Torvalds  *	Set up interface addresses and routes.
3651da177e4SLinus Torvalds  */
3661da177e4SLinus Torvalds 
3671da177e4SLinus Torvalds static int __init ic_setup_if(void)
3681da177e4SLinus Torvalds {
3691da177e4SLinus Torvalds 	struct ifreq ir;
3701da177e4SLinus Torvalds 	struct sockaddr_in *sin = (void *) &ir.ifr_ifru.ifru_addr;
3711da177e4SLinus Torvalds 	int err;
3721da177e4SLinus Torvalds 
3731da177e4SLinus Torvalds 	memset(&ir, 0, sizeof(ir));
3742647cffbSUwe Kleine-König 	strcpy(ir.ifr_ifrn.ifrn_name, ic_dev->dev->name);
3751da177e4SLinus Torvalds 	set_sockaddr(sin, ic_myaddr, 0);
3769643f455SChris Friesen 	if ((err = ic_devinet_ioctl(SIOCSIFADDR, &ir)) < 0) {
377058bd4d2SJoe Perches 		pr_err("IP-Config: Unable to set interface address (%d)\n",
378058bd4d2SJoe Perches 		       err);
3791da177e4SLinus Torvalds 		return -1;
3801da177e4SLinus Torvalds 	}
3811da177e4SLinus Torvalds 	set_sockaddr(sin, ic_netmask, 0);
3829643f455SChris Friesen 	if ((err = ic_devinet_ioctl(SIOCSIFNETMASK, &ir)) < 0) {
383058bd4d2SJoe Perches 		pr_err("IP-Config: Unable to set interface netmask (%d)\n",
384058bd4d2SJoe Perches 		       err);
3851da177e4SLinus Torvalds 		return -1;
3861da177e4SLinus Torvalds 	}
3871da177e4SLinus Torvalds 	set_sockaddr(sin, ic_myaddr | ~ic_netmask, 0);
3889643f455SChris Friesen 	if ((err = ic_devinet_ioctl(SIOCSIFBRDADDR, &ir)) < 0) {
389058bd4d2SJoe Perches 		pr_err("IP-Config: Unable to set interface broadcast address (%d)\n",
390058bd4d2SJoe Perches 		       err);
3911da177e4SLinus Torvalds 		return -1;
3921da177e4SLinus Torvalds 	}
3939643f455SChris Friesen 	/* Handle the case where we need non-standard MTU on the boot link (a network
3949643f455SChris Friesen 	 * using jumbo frames, for instance).  If we can't set the mtu, don't error
3959643f455SChris Friesen 	 * out, we'll try to muddle along.
3969643f455SChris Friesen 	 */
3979643f455SChris Friesen 	if (ic_dev_mtu != 0) {
3982647cffbSUwe Kleine-König 		strcpy(ir.ifr_name, ic_dev->dev->name);
3999643f455SChris Friesen 		ir.ifr_mtu = ic_dev_mtu;
4009643f455SChris Friesen 		if ((err = ic_dev_ioctl(SIOCSIFMTU, &ir)) < 0)
401058bd4d2SJoe Perches 			pr_err("IP-Config: Unable to set interface mtu to %d (%d)\n",
4029643f455SChris Friesen 			       ic_dev_mtu, err);
4039643f455SChris Friesen 	}
4041da177e4SLinus Torvalds 	return 0;
4051da177e4SLinus Torvalds }
4061da177e4SLinus Torvalds 
4071da177e4SLinus Torvalds static int __init ic_setup_routes(void)
4081da177e4SLinus Torvalds {
4091da177e4SLinus Torvalds 	/* No need to setup device routes, only the default route... */
4101da177e4SLinus Torvalds 
4115a874db4SAl Viro 	if (ic_gateway != NONE) {
4121da177e4SLinus Torvalds 		struct rtentry rm;
4131da177e4SLinus Torvalds 		int err;
4141da177e4SLinus Torvalds 
4151da177e4SLinus Torvalds 		memset(&rm, 0, sizeof(rm));
4161da177e4SLinus Torvalds 		if ((ic_gateway ^ ic_myaddr) & ic_netmask) {
417058bd4d2SJoe Perches 			pr_err("IP-Config: Gateway not on directly connected network\n");
4181da177e4SLinus Torvalds 			return -1;
4191da177e4SLinus Torvalds 		}
4201da177e4SLinus Torvalds 		set_sockaddr((struct sockaddr_in *) &rm.rt_dst, 0, 0);
4211da177e4SLinus Torvalds 		set_sockaddr((struct sockaddr_in *) &rm.rt_genmask, 0, 0);
4221da177e4SLinus Torvalds 		set_sockaddr((struct sockaddr_in *) &rm.rt_gateway, ic_gateway, 0);
4231da177e4SLinus Torvalds 		rm.rt_flags = RTF_UP | RTF_GATEWAY;
4241da177e4SLinus Torvalds 		if ((err = ic_route_ioctl(SIOCADDRT, &rm)) < 0) {
425058bd4d2SJoe Perches 			pr_err("IP-Config: Cannot add default route (%d)\n",
426058bd4d2SJoe Perches 			       err);
4271da177e4SLinus Torvalds 			return -1;
4281da177e4SLinus Torvalds 		}
4291da177e4SLinus Torvalds 	}
4301da177e4SLinus Torvalds 
4311da177e4SLinus Torvalds 	return 0;
4321da177e4SLinus Torvalds }
4331da177e4SLinus Torvalds 
4341da177e4SLinus Torvalds /*
4351da177e4SLinus Torvalds  *	Fill in default values for all missing parameters.
4361da177e4SLinus Torvalds  */
4371da177e4SLinus Torvalds 
4381da177e4SLinus Torvalds static int __init ic_defaults(void)
4391da177e4SLinus Torvalds {
4401da177e4SLinus Torvalds 	/*
4411da177e4SLinus Torvalds 	 *	At this point we have no userspace running so need not
4421da177e4SLinus Torvalds 	 *	claim locks on system_utsname
4431da177e4SLinus Torvalds 	 */
4441da177e4SLinus Torvalds 
4451da177e4SLinus Torvalds 	if (!ic_host_name_set)
446673d57e7SHarvey Harrison 		sprintf(init_utsname()->nodename, "%pI4", &ic_myaddr);
4471da177e4SLinus Torvalds 
4485a874db4SAl Viro 	if (root_server_addr == NONE)
4491da177e4SLinus Torvalds 		root_server_addr = ic_servaddr;
4501da177e4SLinus Torvalds 
4515a874db4SAl Viro 	if (ic_netmask == NONE) {
4521da177e4SLinus Torvalds 		if (IN_CLASSA(ntohl(ic_myaddr)))
4531da177e4SLinus Torvalds 			ic_netmask = htonl(IN_CLASSA_NET);
4541da177e4SLinus Torvalds 		else if (IN_CLASSB(ntohl(ic_myaddr)))
4551da177e4SLinus Torvalds 			ic_netmask = htonl(IN_CLASSB_NET);
4561da177e4SLinus Torvalds 		else if (IN_CLASSC(ntohl(ic_myaddr)))
4571da177e4SLinus Torvalds 			ic_netmask = htonl(IN_CLASSC_NET);
4581da177e4SLinus Torvalds 		else {
459058bd4d2SJoe Perches 			pr_err("IP-Config: Unable to guess netmask for address %pI4\n",
460673d57e7SHarvey Harrison 			       &ic_myaddr);
4611da177e4SLinus Torvalds 			return -1;
4621da177e4SLinus Torvalds 		}
46309605cc1SBastian Stender 		pr_notice("IP-Config: Guessing netmask %pI4\n",
46409605cc1SBastian Stender 			  &ic_netmask);
4651da177e4SLinus Torvalds 	}
4661da177e4SLinus Torvalds 
4671da177e4SLinus Torvalds 	return 0;
4681da177e4SLinus Torvalds }
4691da177e4SLinus Torvalds 
4701da177e4SLinus Torvalds /*
4711da177e4SLinus Torvalds  *	RARP support.
4721da177e4SLinus Torvalds  */
4731da177e4SLinus Torvalds 
4741da177e4SLinus Torvalds #ifdef IPCONFIG_RARP
4751da177e4SLinus Torvalds 
476f2ccd8faSDavid S. Miller static int ic_rarp_recv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev);
4771da177e4SLinus Torvalds 
4781da177e4SLinus Torvalds static struct packet_type rarp_packet_type __initdata = {
47909640e63SHarvey Harrison 	.type =	cpu_to_be16(ETH_P_RARP),
4801da177e4SLinus Torvalds 	.func =	ic_rarp_recv,
4811da177e4SLinus Torvalds };
4821da177e4SLinus Torvalds 
48345e741b8SSam Ravnborg static inline void __init ic_rarp_init(void)
4841da177e4SLinus Torvalds {
4851da177e4SLinus Torvalds 	dev_add_pack(&rarp_packet_type);
4861da177e4SLinus Torvalds }
4871da177e4SLinus Torvalds 
48845e741b8SSam Ravnborg static inline void __init ic_rarp_cleanup(void)
4891da177e4SLinus Torvalds {
4901da177e4SLinus Torvalds 	dev_remove_pack(&rarp_packet_type);
4911da177e4SLinus Torvalds }
4921da177e4SLinus Torvalds 
4931da177e4SLinus Torvalds /*
4941da177e4SLinus Torvalds  *  Process received RARP packet.
4951da177e4SLinus Torvalds  */
4961da177e4SLinus Torvalds static int __init
497f2ccd8faSDavid S. Miller ic_rarp_recv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev)
4981da177e4SLinus Torvalds {
4991da177e4SLinus Torvalds 	struct arphdr *rarp;
5001da177e4SLinus Torvalds 	unsigned char *rarp_ptr;
5015a874db4SAl Viro 	__be32 sip, tip;
5026b436d33SFabian Frederick 	unsigned char *tha;		/* t for "target" */
5031da177e4SLinus Torvalds 	struct ic_device *d;
5041da177e4SLinus Torvalds 
505721499e8SYOSHIFUJI Hideaki 	if (!net_eq(dev_net(dev), &init_net))
506e730c155SEric W. Biederman 		goto drop;
507e730c155SEric W. Biederman 
50851456b29SIan Morris 	skb = skb_share_check(skb, GFP_ATOMIC);
50951456b29SIan Morris 	if (!skb)
5101da177e4SLinus Torvalds 		return NET_RX_DROP;
5111da177e4SLinus Torvalds 
5121da177e4SLinus Torvalds 	if (!pskb_may_pull(skb, sizeof(struct arphdr)))
5131da177e4SLinus Torvalds 		goto drop;
5141da177e4SLinus Torvalds 
5151da177e4SLinus Torvalds 	/* Basic sanity checks can be done without the lock.  */
5169c70220bSArnaldo Carvalho de Melo 	rarp = (struct arphdr *)skb_transport_header(skb);
5171da177e4SLinus Torvalds 
5181da177e4SLinus Torvalds 	/* If this test doesn't pass, it's not IP, or we should
5191da177e4SLinus Torvalds 	 * ignore it anyway.
5201da177e4SLinus Torvalds 	 */
5211da177e4SLinus Torvalds 	if (rarp->ar_hln != dev->addr_len || dev->type != ntohs(rarp->ar_hrd))
5221da177e4SLinus Torvalds 		goto drop;
5231da177e4SLinus Torvalds 
5241da177e4SLinus Torvalds 	/* If it's not a RARP reply, delete it. */
5251da177e4SLinus Torvalds 	if (rarp->ar_op != htons(ARPOP_RREPLY))
5261da177e4SLinus Torvalds 		goto drop;
5271da177e4SLinus Torvalds 
5281da177e4SLinus Torvalds 	/* If it's not Ethernet, delete it. */
5291da177e4SLinus Torvalds 	if (rarp->ar_pro != htons(ETH_P_IP))
5301da177e4SLinus Torvalds 		goto drop;
5311da177e4SLinus Torvalds 
532988b7050SPavel Emelyanov 	if (!pskb_may_pull(skb, arp_hdr_len(dev)))
5331da177e4SLinus Torvalds 		goto drop;
5341da177e4SLinus Torvalds 
5351da177e4SLinus Torvalds 	/* OK, it is all there and looks valid, process... */
5369c70220bSArnaldo Carvalho de Melo 	rarp = (struct arphdr *)skb_transport_header(skb);
5371da177e4SLinus Torvalds 	rarp_ptr = (unsigned char *) (rarp + 1);
5381da177e4SLinus Torvalds 
5391da177e4SLinus Torvalds 	/* One reply at a time, please. */
5401da177e4SLinus Torvalds 	spin_lock(&ic_recv_lock);
5411da177e4SLinus Torvalds 
5421da177e4SLinus Torvalds 	/* If we already have a reply, just drop the packet */
5431da177e4SLinus Torvalds 	if (ic_got_reply)
5441da177e4SLinus Torvalds 		goto drop_unlock;
5451da177e4SLinus Torvalds 
5461da177e4SLinus Torvalds 	/* Find the ic_device that the packet arrived on */
5471da177e4SLinus Torvalds 	d = ic_first_dev;
5481da177e4SLinus Torvalds 	while (d && d->dev != dev)
5491da177e4SLinus Torvalds 		d = d->next;
5501da177e4SLinus Torvalds 	if (!d)
5511da177e4SLinus Torvalds 		goto drop_unlock;	/* should never happen */
5521da177e4SLinus Torvalds 
5531da177e4SLinus Torvalds 	/* Extract variable-width fields */
5541da177e4SLinus Torvalds 	rarp_ptr += dev->addr_len;
5551da177e4SLinus Torvalds 	memcpy(&sip, rarp_ptr, 4);
5561da177e4SLinus Torvalds 	rarp_ptr += 4;
5571da177e4SLinus Torvalds 	tha = rarp_ptr;
5581da177e4SLinus Torvalds 	rarp_ptr += dev->addr_len;
5591da177e4SLinus Torvalds 	memcpy(&tip, rarp_ptr, 4);
5601da177e4SLinus Torvalds 
5611da177e4SLinus Torvalds 	/* Discard packets which are not meant for us. */
5621da177e4SLinus Torvalds 	if (memcmp(tha, dev->dev_addr, dev->addr_len))
5631da177e4SLinus Torvalds 		goto drop_unlock;
5641da177e4SLinus Torvalds 
5651da177e4SLinus Torvalds 	/* Discard packets which are not from specified server. */
5665a874db4SAl Viro 	if (ic_servaddr != NONE && ic_servaddr != sip)
5671da177e4SLinus Torvalds 		goto drop_unlock;
5681da177e4SLinus Torvalds 
5691da177e4SLinus Torvalds 	/* We have a winner! */
5702647cffbSUwe Kleine-König 	ic_dev = d;
5715a874db4SAl Viro 	if (ic_myaddr == NONE)
5721da177e4SLinus Torvalds 		ic_myaddr = tip;
5731da177e4SLinus Torvalds 	ic_servaddr = sip;
5749dd4a13aSPhilippe De Muyter 	ic_addrservaddr = sip;
5751da177e4SLinus Torvalds 	ic_got_reply = IC_RARP;
5761da177e4SLinus Torvalds 
5771da177e4SLinus Torvalds drop_unlock:
5781da177e4SLinus Torvalds 	/* Show's over.  Nothing to see here.  */
5791da177e4SLinus Torvalds 	spin_unlock(&ic_recv_lock);
5801da177e4SLinus Torvalds 
5811da177e4SLinus Torvalds drop:
5821da177e4SLinus Torvalds 	/* Throw the packet out. */
5831da177e4SLinus Torvalds 	kfree_skb(skb);
5841da177e4SLinus Torvalds 	return 0;
5851da177e4SLinus Torvalds }
5861da177e4SLinus Torvalds 
5871da177e4SLinus Torvalds 
5881da177e4SLinus Torvalds /*
5891da177e4SLinus Torvalds  *  Send RARP request packet over a single interface.
5901da177e4SLinus Torvalds  */
5911da177e4SLinus Torvalds static void __init ic_rarp_send_if(struct ic_device *d)
5921da177e4SLinus Torvalds {
5931da177e4SLinus Torvalds 	struct net_device *dev = d->dev;
5941da177e4SLinus Torvalds 	arp_send(ARPOP_RREQUEST, ETH_P_RARP, 0, dev, 0, NULL,
5951da177e4SLinus Torvalds 		 dev->dev_addr, dev->dev_addr);
5961da177e4SLinus Torvalds }
5971da177e4SLinus Torvalds #endif
5981da177e4SLinus Torvalds 
5991da177e4SLinus Torvalds /*
600842b08bbSAndy Shevchenko  *  Predefine Nameservers
601842b08bbSAndy Shevchenko  */
602842b08bbSAndy Shevchenko static inline void __init ic_nameservers_predef(void)
603842b08bbSAndy Shevchenko {
604842b08bbSAndy Shevchenko 	int i;
605842b08bbSAndy Shevchenko 
606842b08bbSAndy Shevchenko 	for (i = 0; i < CONF_NAMESERVERS_MAX; i++)
607842b08bbSAndy Shevchenko 		ic_nameservers[i] = NONE;
608842b08bbSAndy Shevchenko }
609842b08bbSAndy Shevchenko 
610842b08bbSAndy Shevchenko /*
6111da177e4SLinus Torvalds  *	DHCP/BOOTP support.
6121da177e4SLinus Torvalds  */
6131da177e4SLinus Torvalds 
6141da177e4SLinus Torvalds #ifdef IPCONFIG_BOOTP
6151da177e4SLinus Torvalds 
6161da177e4SLinus Torvalds struct bootp_pkt {		/* BOOTP packet format */
6171da177e4SLinus Torvalds 	struct iphdr iph;	/* IP header */
6181da177e4SLinus Torvalds 	struct udphdr udph;	/* UDP header */
6191da177e4SLinus Torvalds 	u8 op;			/* 1=request, 2=reply */
6201da177e4SLinus Torvalds 	u8 htype;		/* HW address type */
6211da177e4SLinus Torvalds 	u8 hlen;		/* HW address length */
6221da177e4SLinus Torvalds 	u8 hops;		/* Used only by gateways */
6235a874db4SAl Viro 	__be32 xid;		/* Transaction ID */
6245a874db4SAl Viro 	__be16 secs;		/* Seconds since we started */
6255a874db4SAl Viro 	__be16 flags;		/* Just what it says */
6265a874db4SAl Viro 	__be32 client_ip;		/* Client's IP address if known */
6275a874db4SAl Viro 	__be32 your_ip;		/* Assigned IP address */
6285a874db4SAl Viro 	__be32 server_ip;		/* (Next, e.g. NFS) Server's IP address */
6295a874db4SAl Viro 	__be32 relay_ip;		/* IP address of BOOTP relay */
6301da177e4SLinus Torvalds 	u8 hw_addr[16];		/* Client's HW address */
6311da177e4SLinus Torvalds 	u8 serv_name[64];	/* Server host name */
6321da177e4SLinus Torvalds 	u8 boot_file[128];	/* Name of boot file */
6331da177e4SLinus Torvalds 	u8 exten[312];		/* DHCP options / BOOTP vendor extensions */
6341da177e4SLinus Torvalds };
6351da177e4SLinus Torvalds 
6361da177e4SLinus Torvalds /* packet ops */
6371da177e4SLinus Torvalds #define BOOTP_REQUEST	1
6381da177e4SLinus Torvalds #define BOOTP_REPLY	2
6391da177e4SLinus Torvalds 
6401da177e4SLinus Torvalds /* DHCP message types */
6411da177e4SLinus Torvalds #define DHCPDISCOVER	1
6421da177e4SLinus Torvalds #define DHCPOFFER	2
6431da177e4SLinus Torvalds #define DHCPREQUEST	3
6441da177e4SLinus Torvalds #define DHCPDECLINE	4
6451da177e4SLinus Torvalds #define DHCPACK		5
6461da177e4SLinus Torvalds #define DHCPNAK		6
6471da177e4SLinus Torvalds #define DHCPRELEASE	7
6481da177e4SLinus Torvalds #define DHCPINFORM	8
6491da177e4SLinus Torvalds 
650f2ccd8faSDavid S. Miller static int ic_bootp_recv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev);
6511da177e4SLinus Torvalds 
6521da177e4SLinus Torvalds static struct packet_type bootp_packet_type __initdata = {
65309640e63SHarvey Harrison 	.type =	cpu_to_be16(ETH_P_IP),
6541da177e4SLinus Torvalds 	.func =	ic_bootp_recv,
6551da177e4SLinus Torvalds };
6561da177e4SLinus Torvalds 
6571da177e4SLinus Torvalds /*
6581da177e4SLinus Torvalds  *  Initialize DHCP/BOOTP extension fields in the request.
6591da177e4SLinus Torvalds  */
6601da177e4SLinus Torvalds 
6611da177e4SLinus Torvalds static const u8 ic_bootp_cookie[4] = { 99, 130, 83, 99 };
6621da177e4SLinus Torvalds 
6631da177e4SLinus Torvalds #ifdef IPCONFIG_DHCP
6641da177e4SLinus Torvalds 
6651da177e4SLinus Torvalds static void __init
66622fc5388SUwe Kleine-König ic_dhcp_init_options(u8 *options, struct ic_device *d)
6671da177e4SLinus Torvalds {
6685a874db4SAl Viro 	u8 mt = ((ic_servaddr == NONE)
6691da177e4SLinus Torvalds 		 ? DHCPDISCOVER : DHCPREQUEST);
6701da177e4SLinus Torvalds 	u8 *e = options;
67162013dbbSRainer Jochem 	int len;
6721da177e4SLinus Torvalds 
67322fc5388SUwe Kleine-König 	pr_debug("DHCP: Sending message type %d (%s)\n", mt, d->dev->name);
6741da177e4SLinus Torvalds 
6751da177e4SLinus Torvalds 	memcpy(e, ic_bootp_cookie, 4);	/* RFC1048 Magic Cookie */
6761da177e4SLinus Torvalds 	e += 4;
6771da177e4SLinus Torvalds 
6781da177e4SLinus Torvalds 	*e++ = 53;		/* DHCP message type */
6791da177e4SLinus Torvalds 	*e++ = 1;
6801da177e4SLinus Torvalds 	*e++ = mt;
6811da177e4SLinus Torvalds 
6821da177e4SLinus Torvalds 	if (mt == DHCPREQUEST) {
6831da177e4SLinus Torvalds 		*e++ = 54;	/* Server ID (IP address) */
6841da177e4SLinus Torvalds 		*e++ = 4;
6851da177e4SLinus Torvalds 		memcpy(e, &ic_servaddr, 4);
6861da177e4SLinus Torvalds 		e += 4;
6871da177e4SLinus Torvalds 
6881da177e4SLinus Torvalds 		*e++ = 50;	/* Requested IP address */
6891da177e4SLinus Torvalds 		*e++ = 4;
6901da177e4SLinus Torvalds 		memcpy(e, &ic_myaddr, 4);
6911da177e4SLinus Torvalds 		e += 4;
6921da177e4SLinus Torvalds 	}
6931da177e4SLinus Torvalds 
6941da177e4SLinus Torvalds 	/* always? */
6951da177e4SLinus Torvalds 	{
6961da177e4SLinus Torvalds 		static const u8 ic_req_params[] = {
6971da177e4SLinus Torvalds 			1,	/* Subnet mask */
6981da177e4SLinus Torvalds 			3,	/* Default gateway */
6991da177e4SLinus Torvalds 			6,	/* DNS server */
7001da177e4SLinus Torvalds 			12,	/* Host name */
7011da177e4SLinus Torvalds 			15,	/* Domain name */
7021da177e4SLinus Torvalds 			17,	/* Boot path */
7039643f455SChris Friesen 			26,	/* MTU */
7041da177e4SLinus Torvalds 			40,	/* NIS domain name */
7051da177e4SLinus Torvalds 		};
7061da177e4SLinus Torvalds 
7071da177e4SLinus Torvalds 		*e++ = 55;	/* Parameter request list */
7081da177e4SLinus Torvalds 		*e++ = sizeof(ic_req_params);
7091da177e4SLinus Torvalds 		memcpy(e, ic_req_params, sizeof(ic_req_params));
7101da177e4SLinus Torvalds 		e += sizeof(ic_req_params);
71162013dbbSRainer Jochem 
712130c0f47SWu Fengguang 		if (ic_host_name_set) {
713130c0f47SWu Fengguang 			*e++ = 12;	/* host-name */
714130c0f47SWu Fengguang 			len = strlen(utsname()->nodename);
715130c0f47SWu Fengguang 			*e++ = len;
716130c0f47SWu Fengguang 			memcpy(e, utsname()->nodename, len);
717130c0f47SWu Fengguang 			e += len;
718130c0f47SWu Fengguang 		}
71962013dbbSRainer Jochem 		if (*vendor_class_identifier) {
720058bd4d2SJoe Perches 			pr_info("DHCP: sending class identifier \"%s\"\n",
72162013dbbSRainer Jochem 				vendor_class_identifier);
72262013dbbSRainer Jochem 			*e++ = 60;	/* Class-identifier */
72362013dbbSRainer Jochem 			len = strlen(vendor_class_identifier);
72462013dbbSRainer Jochem 			*e++ = len;
72562013dbbSRainer Jochem 			memcpy(e, vendor_class_identifier, len);
72662013dbbSRainer Jochem 			e += len;
72762013dbbSRainer Jochem 		}
72826fb342cSLi RongQing 		len = strlen(dhcp_client_identifier + 1);
72926fb342cSLi RongQing 		/* the minimum length of identifier is 2, include 1 byte type,
73026fb342cSLi RongQing 		 * and can not be larger than the length of options
73126fb342cSLi RongQing 		 */
73226fb342cSLi RongQing 		if (len >= 1 && len < 312 - (e - options) - 1) {
73326fb342cSLi RongQing 			*e++ = 61;
73426fb342cSLi RongQing 			*e++ = len + 1;
73526fb342cSLi RongQing 			memcpy(e, dhcp_client_identifier, len + 1);
73626fb342cSLi RongQing 			e += len + 1;
73726fb342cSLi RongQing 		}
7381da177e4SLinus Torvalds 	}
7391da177e4SLinus Torvalds 
7401da177e4SLinus Torvalds 	*e++ = 255;	/* End of the list */
7411da177e4SLinus Torvalds }
7421da177e4SLinus Torvalds 
7431da177e4SLinus Torvalds #endif /* IPCONFIG_DHCP */
7441da177e4SLinus Torvalds 
7451da177e4SLinus Torvalds static void __init ic_bootp_init_ext(u8 *e)
7461da177e4SLinus Torvalds {
7471da177e4SLinus Torvalds 	memcpy(e, ic_bootp_cookie, 4);	/* RFC1048 Magic Cookie */
7481da177e4SLinus Torvalds 	e += 4;
7491da177e4SLinus Torvalds 	*e++ = 1;		/* Subnet mask request */
7501da177e4SLinus Torvalds 	*e++ = 4;
7511da177e4SLinus Torvalds 	e += 4;
7521da177e4SLinus Torvalds 	*e++ = 3;		/* Default gateway request */
7531da177e4SLinus Torvalds 	*e++ = 4;
7541da177e4SLinus Torvalds 	e += 4;
7551da177e4SLinus Torvalds 	*e++ = 5;		/* Name server request */
7561da177e4SLinus Torvalds 	*e++ = 8;
7571da177e4SLinus Torvalds 	e += 8;
7581da177e4SLinus Torvalds 	*e++ = 12;		/* Host name request */
7591da177e4SLinus Torvalds 	*e++ = 32;
7601da177e4SLinus Torvalds 	e += 32;
7611da177e4SLinus Torvalds 	*e++ = 40;		/* NIS Domain name request */
7621da177e4SLinus Torvalds 	*e++ = 32;
7631da177e4SLinus Torvalds 	e += 32;
7641da177e4SLinus Torvalds 	*e++ = 17;		/* Boot path */
7651da177e4SLinus Torvalds 	*e++ = 40;
7661da177e4SLinus Torvalds 	e += 40;
7671da177e4SLinus Torvalds 
7681da177e4SLinus Torvalds 	*e++ = 57;		/* set extension buffer size for reply */
7691da177e4SLinus Torvalds 	*e++ = 2;
7701da177e4SLinus Torvalds 	*e++ = 1;		/* 128+236+8+20+14, see dhcpd sources */
7711da177e4SLinus Torvalds 	*e++ = 150;
7721da177e4SLinus Torvalds 
7731da177e4SLinus Torvalds 	*e++ = 255;		/* End of the list */
7741da177e4SLinus Torvalds }
7751da177e4SLinus Torvalds 
7761da177e4SLinus Torvalds 
7771da177e4SLinus Torvalds /*
7785e953778SChristoph Fritz  *  Initialize the DHCP/BOOTP mechanism.
7795e953778SChristoph Fritz  */
7805e953778SChristoph Fritz static inline void __init ic_bootp_init(void)
7815e953778SChristoph Fritz {
7825e953778SChristoph Fritz 	ic_nameservers_predef();
7831da177e4SLinus Torvalds 
7841da177e4SLinus Torvalds 	dev_add_pack(&bootp_packet_type);
7851da177e4SLinus Torvalds }
7861da177e4SLinus Torvalds 
7871da177e4SLinus Torvalds 
7881da177e4SLinus Torvalds /*
7891da177e4SLinus Torvalds  *  DHCP/BOOTP cleanup.
7901da177e4SLinus Torvalds  */
79145e741b8SSam Ravnborg static inline void __init ic_bootp_cleanup(void)
7921da177e4SLinus Torvalds {
7931da177e4SLinus Torvalds 	dev_remove_pack(&bootp_packet_type);
7941da177e4SLinus Torvalds }
7951da177e4SLinus Torvalds 
7961da177e4SLinus Torvalds 
7971da177e4SLinus Torvalds /*
7981da177e4SLinus Torvalds  *  Send DHCP/BOOTP request to single interface.
7991da177e4SLinus Torvalds  */
8001da177e4SLinus Torvalds static void __init ic_bootp_send_if(struct ic_device *d, unsigned long jiffies_diff)
8011da177e4SLinus Torvalds {
8021da177e4SLinus Torvalds 	struct net_device *dev = d->dev;
8031da177e4SLinus Torvalds 	struct sk_buff *skb;
8041da177e4SLinus Torvalds 	struct bootp_pkt *b;
8051da177e4SLinus Torvalds 	struct iphdr *h;
80666088243SHerbert Xu 	int hlen = LL_RESERVED_SPACE(dev);
80766088243SHerbert Xu 	int tlen = dev->needed_tailroom;
8081da177e4SLinus Torvalds 
8091da177e4SLinus Torvalds 	/* Allocate packet */
81066088243SHerbert Xu 	skb = alloc_skb(sizeof(struct bootp_pkt) + hlen + tlen + 15,
811f5184d26SJohannes Berg 			GFP_KERNEL);
8121da177e4SLinus Torvalds 	if (!skb)
8131da177e4SLinus Torvalds 		return;
81466088243SHerbert Xu 	skb_reserve(skb, hlen);
8151da177e4SLinus Torvalds 	b = (struct bootp_pkt *) skb_put(skb, sizeof(struct bootp_pkt));
8161da177e4SLinus Torvalds 	memset(b, 0, sizeof(struct bootp_pkt));
8171da177e4SLinus Torvalds 
8181da177e4SLinus Torvalds 	/* Construct IP header */
81904b964dbSArnaldo Carvalho de Melo 	skb_reset_network_header(skb);
820eddc9ec5SArnaldo Carvalho de Melo 	h = ip_hdr(skb);
8211da177e4SLinus Torvalds 	h->version = 4;
8221da177e4SLinus Torvalds 	h->ihl = 5;
8231da177e4SLinus Torvalds 	h->tot_len = htons(sizeof(struct bootp_pkt));
8241da177e4SLinus Torvalds 	h->frag_off = htons(IP_DF);
8251da177e4SLinus Torvalds 	h->ttl = 64;
8261da177e4SLinus Torvalds 	h->protocol = IPPROTO_UDP;
8275a874db4SAl Viro 	h->daddr = htonl(INADDR_BROADCAST);
8281da177e4SLinus Torvalds 	h->check = ip_fast_csum((unsigned char *) h, h->ihl);
8291da177e4SLinus Torvalds 
8301da177e4SLinus Torvalds 	/* Construct UDP header */
8311da177e4SLinus Torvalds 	b->udph.source = htons(68);
8321da177e4SLinus Torvalds 	b->udph.dest = htons(67);
8331da177e4SLinus Torvalds 	b->udph.len = htons(sizeof(struct bootp_pkt) - sizeof(struct iphdr));
8341da177e4SLinus Torvalds 	/* UDP checksum not calculated -- explicitly allowed in BOOTP RFC */
8351da177e4SLinus Torvalds 
8361da177e4SLinus Torvalds 	/* Construct DHCP/BOOTP header */
8371da177e4SLinus Torvalds 	b->op = BOOTP_REQUEST;
8381da177e4SLinus Torvalds 	if (dev->type < 256) /* check for false types */
8391da177e4SLinus Torvalds 		b->htype = dev->type;
8401da177e4SLinus Torvalds 	else if (dev->type == ARPHRD_FDDI)
8411da177e4SLinus Torvalds 		b->htype = ARPHRD_ETHER;
8421da177e4SLinus Torvalds 	else {
84309605cc1SBastian Stender 		pr_warn("Unknown ARP type 0x%04x for device %s\n", dev->type,
84409605cc1SBastian Stender 			dev->name);
8451da177e4SLinus Torvalds 		b->htype = dev->type; /* can cause undefined behavior */
8461da177e4SLinus Torvalds 	}
847dea75bdfSStephen Hemminger 
848dea75bdfSStephen Hemminger 	/* server_ip and your_ip address are both already zero per RFC2131 */
8491da177e4SLinus Torvalds 	b->hlen = dev->addr_len;
8501da177e4SLinus Torvalds 	memcpy(b->hw_addr, dev->dev_addr, dev->addr_len);
8511da177e4SLinus Torvalds 	b->secs = htons(jiffies_diff / HZ);
8521da177e4SLinus Torvalds 	b->xid = d->xid;
8531da177e4SLinus Torvalds 
8541da177e4SLinus Torvalds 	/* add DHCP options or BOOTP extensions */
8551da177e4SLinus Torvalds #ifdef IPCONFIG_DHCP
8561da177e4SLinus Torvalds 	if (ic_proto_enabled & IC_USE_DHCP)
85722fc5388SUwe Kleine-König 		ic_dhcp_init_options(b->exten, d);
8581da177e4SLinus Torvalds 	else
8591da177e4SLinus Torvalds #endif
8601da177e4SLinus Torvalds 		ic_bootp_init_ext(b->exten);
8611da177e4SLinus Torvalds 
8621da177e4SLinus Torvalds 	/* Chain packet down the line... */
8631da177e4SLinus Torvalds 	skb->dev = dev;
8641da177e4SLinus Torvalds 	skb->protocol = htons(ETH_P_IP);
8650c4e8581SStephen Hemminger 	if (dev_hard_header(skb, dev, ntohs(skb->protocol),
866ad79eefcSRongQing.Li 			    dev->broadcast, dev->dev_addr, skb->len) < 0) {
867ad79eefcSRongQing.Li 		kfree_skb(skb);
868ad79eefcSRongQing.Li 		printk("E");
869ad79eefcSRongQing.Li 		return;
870ad79eefcSRongQing.Li 	}
871ad79eefcSRongQing.Li 
872ad79eefcSRongQing.Li 	if (dev_queue_xmit(skb) < 0)
8731da177e4SLinus Torvalds 		printk("E");
8741da177e4SLinus Torvalds }
8751da177e4SLinus Torvalds 
8761da177e4SLinus Torvalds 
8771da177e4SLinus Torvalds /*
8781da177e4SLinus Torvalds  *  Copy BOOTP-supplied string if not already set.
8791da177e4SLinus Torvalds  */
8801da177e4SLinus Torvalds static int __init ic_bootp_string(char *dest, char *src, int len, int max)
8811da177e4SLinus Torvalds {
8821da177e4SLinus Torvalds 	if (!len)
8831da177e4SLinus Torvalds 		return 0;
8841da177e4SLinus Torvalds 	if (len > max-1)
8851da177e4SLinus Torvalds 		len = max-1;
8861da177e4SLinus Torvalds 	memcpy(dest, src, len);
8871da177e4SLinus Torvalds 	dest[len] = '\0';
8881da177e4SLinus Torvalds 	return 1;
8891da177e4SLinus Torvalds }
8901da177e4SLinus Torvalds 
8911da177e4SLinus Torvalds 
8921da177e4SLinus Torvalds /*
8931da177e4SLinus Torvalds  *  Process BOOTP extensions.
8941da177e4SLinus Torvalds  */
8951da177e4SLinus Torvalds static void __init ic_do_bootp_ext(u8 *ext)
8961da177e4SLinus Torvalds {
8971da177e4SLinus Torvalds 	u8 servers;
8981da177e4SLinus Torvalds 	int i;
899747465efSEric Dumazet 	__be16 mtu;
9001da177e4SLinus Torvalds 
9011da177e4SLinus Torvalds 	u8 *c;
9021da177e4SLinus Torvalds 
90309605cc1SBastian Stender 	pr_debug("DHCP/BOOTP: Got extension %d:", *ext);
9041da177e4SLinus Torvalds 	for (c=ext+2; c<ext+2+ext[1]; c++)
90509605cc1SBastian Stender 		pr_debug(" %02x", *c);
90609605cc1SBastian Stender 	pr_debug("\n");
9071da177e4SLinus Torvalds 
9081da177e4SLinus Torvalds 	switch (*ext++) {
9091da177e4SLinus Torvalds 	case 1:		/* Subnet mask */
9105a874db4SAl Viro 		if (ic_netmask == NONE)
9111da177e4SLinus Torvalds 			memcpy(&ic_netmask, ext+1, 4);
9121da177e4SLinus Torvalds 		break;
9131da177e4SLinus Torvalds 	case 3:		/* Default gateway */
9145a874db4SAl Viro 		if (ic_gateway == NONE)
9151da177e4SLinus Torvalds 			memcpy(&ic_gateway, ext+1, 4);
9161da177e4SLinus Torvalds 		break;
9171da177e4SLinus Torvalds 	case 6:		/* DNS server */
9181da177e4SLinus Torvalds 		servers= *ext/4;
9191da177e4SLinus Torvalds 		if (servers > CONF_NAMESERVERS_MAX)
9201da177e4SLinus Torvalds 			servers = CONF_NAMESERVERS_MAX;
9211da177e4SLinus Torvalds 		for (i = 0; i < servers; i++) {
9225a874db4SAl Viro 			if (ic_nameservers[i] == NONE)
9231da177e4SLinus Torvalds 				memcpy(&ic_nameservers[i], ext+1+4*i, 4);
9241da177e4SLinus Torvalds 		}
9251da177e4SLinus Torvalds 		break;
9261da177e4SLinus Torvalds 	case 12:	/* Host name */
9271d67a516SJoe Perches 		ic_bootp_string(utsname()->nodename, ext+1, *ext,
9281d67a516SJoe Perches 				__NEW_UTS_LEN);
9291da177e4SLinus Torvalds 		ic_host_name_set = 1;
9301da177e4SLinus Torvalds 		break;
9311da177e4SLinus Torvalds 	case 15:	/* Domain name (DNS) */
9321da177e4SLinus Torvalds 		ic_bootp_string(ic_domain, ext+1, *ext, sizeof(ic_domain));
9331da177e4SLinus Torvalds 		break;
9341da177e4SLinus Torvalds 	case 17:	/* Root path */
9351da177e4SLinus Torvalds 		if (!root_server_path[0])
9361d67a516SJoe Perches 			ic_bootp_string(root_server_path, ext+1, *ext,
9371d67a516SJoe Perches 					sizeof(root_server_path));
9381da177e4SLinus Torvalds 		break;
9399643f455SChris Friesen 	case 26:	/* Interface MTU */
9409643f455SChris Friesen 		memcpy(&mtu, ext+1, sizeof(mtu));
9419643f455SChris Friesen 		ic_dev_mtu = ntohs(mtu);
9429643f455SChris Friesen 		break;
9431da177e4SLinus Torvalds 	case 40:	/* NIS Domain name (_not_ DNS) */
9441d67a516SJoe Perches 		ic_bootp_string(utsname()->domainname, ext+1, *ext,
9451d67a516SJoe Perches 				__NEW_UTS_LEN);
9461da177e4SLinus Torvalds 		break;
9471da177e4SLinus Torvalds 	}
9481da177e4SLinus Torvalds }
9491da177e4SLinus Torvalds 
9501da177e4SLinus Torvalds 
9511da177e4SLinus Torvalds /*
9521da177e4SLinus Torvalds  *  Receive BOOTP reply.
9531da177e4SLinus Torvalds  */
954f2ccd8faSDavid 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)
9551da177e4SLinus Torvalds {
9561da177e4SLinus Torvalds 	struct bootp_pkt *b;
9571da177e4SLinus Torvalds 	struct iphdr *h;
9581da177e4SLinus Torvalds 	struct ic_device *d;
9591da177e4SLinus Torvalds 	int len, ext_len;
9601da177e4SLinus Torvalds 
961721499e8SYOSHIFUJI Hideaki 	if (!net_eq(dev_net(dev), &init_net))
962e730c155SEric W. Biederman 		goto drop;
963e730c155SEric W. Biederman 
9641da177e4SLinus Torvalds 	/* Perform verifications before taking the lock.  */
9651da177e4SLinus Torvalds 	if (skb->pkt_type == PACKET_OTHERHOST)
9661da177e4SLinus Torvalds 		goto drop;
9671da177e4SLinus Torvalds 
96851456b29SIan Morris 	skb = skb_share_check(skb, GFP_ATOMIC);
96951456b29SIan Morris 	if (!skb)
9701da177e4SLinus Torvalds 		return NET_RX_DROP;
9711da177e4SLinus Torvalds 
9721da177e4SLinus Torvalds 	if (!pskb_may_pull(skb,
9731da177e4SLinus Torvalds 			   sizeof(struct iphdr) +
9741da177e4SLinus Torvalds 			   sizeof(struct udphdr)))
9751da177e4SLinus Torvalds 		goto drop;
9761da177e4SLinus Torvalds 
977eddc9ec5SArnaldo Carvalho de Melo 	b = (struct bootp_pkt *)skb_network_header(skb);
9781da177e4SLinus Torvalds 	h = &b->iph;
9791da177e4SLinus Torvalds 
9801da177e4SLinus Torvalds 	if (h->ihl != 5 || h->version != 4 || h->protocol != IPPROTO_UDP)
9811da177e4SLinus Torvalds 		goto drop;
9821da177e4SLinus Torvalds 
9831da177e4SLinus Torvalds 	/* Fragments are not supported */
98456f8a75cSPaul Gortmaker 	if (ip_is_fragment(h)) {
985e87cc472SJoe Perches 		net_err_ratelimited("DHCP/BOOTP: Ignoring fragmented reply\n");
9861da177e4SLinus Torvalds 		goto drop;
9871da177e4SLinus Torvalds 	}
9881da177e4SLinus Torvalds 
9891da177e4SLinus Torvalds 	if (skb->len < ntohs(h->tot_len))
9901da177e4SLinus Torvalds 		goto drop;
9911da177e4SLinus Torvalds 
9921da177e4SLinus Torvalds 	if (ip_fast_csum((char *) h, h->ihl))
9931da177e4SLinus Torvalds 		goto drop;
9941da177e4SLinus Torvalds 
9951da177e4SLinus Torvalds 	if (b->udph.source != htons(67) || b->udph.dest != htons(68))
9961da177e4SLinus Torvalds 		goto drop;
9971da177e4SLinus Torvalds 
9981da177e4SLinus Torvalds 	if (ntohs(h->tot_len) < ntohs(b->udph.len) + sizeof(struct iphdr))
9991da177e4SLinus Torvalds 		goto drop;
10001da177e4SLinus Torvalds 
10011da177e4SLinus Torvalds 	len = ntohs(b->udph.len) - sizeof(struct udphdr);
10021da177e4SLinus Torvalds 	ext_len = len - (sizeof(*b) -
10031da177e4SLinus Torvalds 			 sizeof(struct iphdr) -
10041da177e4SLinus Torvalds 			 sizeof(struct udphdr) -
10051da177e4SLinus Torvalds 			 sizeof(b->exten));
10061da177e4SLinus Torvalds 	if (ext_len < 0)
10071da177e4SLinus Torvalds 		goto drop;
10081da177e4SLinus Torvalds 
10091da177e4SLinus Torvalds 	/* Ok the front looks good, make sure we can get at the rest.  */
10101da177e4SLinus Torvalds 	if (!pskb_may_pull(skb, skb->len))
10111da177e4SLinus Torvalds 		goto drop;
10121da177e4SLinus Torvalds 
1013eddc9ec5SArnaldo Carvalho de Melo 	b = (struct bootp_pkt *)skb_network_header(skb);
10141da177e4SLinus Torvalds 	h = &b->iph;
10151da177e4SLinus Torvalds 
10161da177e4SLinus Torvalds 	/* One reply at a time, please. */
10171da177e4SLinus Torvalds 	spin_lock(&ic_recv_lock);
10181da177e4SLinus Torvalds 
10191da177e4SLinus Torvalds 	/* If we already have a reply, just drop the packet */
10201da177e4SLinus Torvalds 	if (ic_got_reply)
10211da177e4SLinus Torvalds 		goto drop_unlock;
10221da177e4SLinus Torvalds 
10231da177e4SLinus Torvalds 	/* Find the ic_device that the packet arrived on */
10241da177e4SLinus Torvalds 	d = ic_first_dev;
10251da177e4SLinus Torvalds 	while (d && d->dev != dev)
10261da177e4SLinus Torvalds 		d = d->next;
10271da177e4SLinus Torvalds 	if (!d)
10281da177e4SLinus Torvalds 		goto drop_unlock;  /* should never happen */
10291da177e4SLinus Torvalds 
10301da177e4SLinus Torvalds 	/* Is it a reply to our BOOTP request? */
10311da177e4SLinus Torvalds 	if (b->op != BOOTP_REPLY ||
10321da177e4SLinus Torvalds 	    b->xid != d->xid) {
103322fc5388SUwe Kleine-König 		net_err_ratelimited("DHCP/BOOTP: Reply not for us on %s, op[%x] xid[%x]\n",
103422fc5388SUwe Kleine-König 				    d->dev->name, b->op, b->xid);
10351da177e4SLinus Torvalds 		goto drop_unlock;
10361da177e4SLinus Torvalds 	}
10371da177e4SLinus Torvalds 
10381da177e4SLinus Torvalds 	/* Parse extensions */
10391da177e4SLinus Torvalds 	if (ext_len >= 4 &&
10401da177e4SLinus Torvalds 	    !memcmp(b->exten, ic_bootp_cookie, 4)) { /* Check magic cookie */
10411da177e4SLinus Torvalds 		u8 *end = (u8 *) b + ntohs(b->iph.tot_len);
10421da177e4SLinus Torvalds 		u8 *ext;
10431da177e4SLinus Torvalds 
10441da177e4SLinus Torvalds #ifdef IPCONFIG_DHCP
10451da177e4SLinus Torvalds 		if (ic_proto_enabled & IC_USE_DHCP) {
10465a874db4SAl Viro 			__be32 server_id = NONE;
10471da177e4SLinus Torvalds 			int mt = 0;
10481da177e4SLinus Torvalds 
10491da177e4SLinus Torvalds 			ext = &b->exten[4];
10501da177e4SLinus Torvalds 			while (ext < end && *ext != 0xff) {
10511da177e4SLinus Torvalds 				u8 *opt = ext++;
10521da177e4SLinus Torvalds 				if (*opt == 0)	/* Padding */
10531da177e4SLinus Torvalds 					continue;
10541da177e4SLinus Torvalds 				ext += *ext + 1;
10551da177e4SLinus Torvalds 				if (ext >= end)
10561da177e4SLinus Torvalds 					break;
10571da177e4SLinus Torvalds 				switch (*opt) {
10581da177e4SLinus Torvalds 				case 53:	/* Message type */
10591da177e4SLinus Torvalds 					if (opt[1])
10601da177e4SLinus Torvalds 						mt = opt[2];
10611da177e4SLinus Torvalds 					break;
10621da177e4SLinus Torvalds 				case 54:	/* Server ID (IP address) */
10631da177e4SLinus Torvalds 					if (opt[1] >= 4)
10641da177e4SLinus Torvalds 						memcpy(&server_id, opt + 2, 4);
10651da177e4SLinus Torvalds 					break;
10663ff50b79SStephen Hemminger 				}
10671da177e4SLinus Torvalds 			}
10681da177e4SLinus Torvalds 
106922fc5388SUwe Kleine-König 			pr_debug("DHCP: Got message type %d (%s)\n", mt, d->dev->name);
10701da177e4SLinus Torvalds 
10711da177e4SLinus Torvalds 			switch (mt) {
10721da177e4SLinus Torvalds 			case DHCPOFFER:
10731da177e4SLinus Torvalds 				/* While in the process of accepting one offer,
10741da177e4SLinus Torvalds 				 * ignore all others.
10751da177e4SLinus Torvalds 				 */
10765a874db4SAl Viro 				if (ic_myaddr != NONE)
10771da177e4SLinus Torvalds 					goto drop_unlock;
10781da177e4SLinus Torvalds 
10791da177e4SLinus Torvalds 				/* Let's accept that offer. */
10801da177e4SLinus Torvalds 				ic_myaddr = b->your_ip;
10811da177e4SLinus Torvalds 				ic_servaddr = server_id;
108209605cc1SBastian Stender 				pr_debug("DHCP: Offered address %pI4 by server %pI4\n",
10839dd4a13aSPhilippe De Muyter 					 &ic_myaddr, &b->iph.saddr);
10841da177e4SLinus Torvalds 				/* The DHCP indicated server address takes
10851da177e4SLinus Torvalds 				 * precedence over the bootp header one if
10861da177e4SLinus Torvalds 				 * they are different.
10871da177e4SLinus Torvalds 				 */
10885a874db4SAl Viro 				if ((server_id != NONE) &&
10891da177e4SLinus Torvalds 				    (b->server_ip != server_id))
10901da177e4SLinus Torvalds 					b->server_ip = ic_servaddr;
10911da177e4SLinus Torvalds 				break;
10921da177e4SLinus Torvalds 
10931da177e4SLinus Torvalds 			case DHCPACK:
10941da177e4SLinus Torvalds 				if (memcmp(dev->dev_addr, b->hw_addr, dev->addr_len) != 0)
10951da177e4SLinus Torvalds 					goto drop_unlock;
10961da177e4SLinus Torvalds 
10971da177e4SLinus Torvalds 				/* Yeah! */
10981da177e4SLinus Torvalds 				break;
10991da177e4SLinus Torvalds 
11001da177e4SLinus Torvalds 			default:
11011da177e4SLinus Torvalds 				/* Urque.  Forget it*/
11025a874db4SAl Viro 				ic_myaddr = NONE;
11035a874db4SAl Viro 				ic_servaddr = NONE;
11041da177e4SLinus Torvalds 				goto drop_unlock;
11053ff50b79SStephen Hemminger 			}
11061da177e4SLinus Torvalds 
11071da177e4SLinus Torvalds 			ic_dhcp_msgtype = mt;
11081da177e4SLinus Torvalds 
11091da177e4SLinus Torvalds 		}
11101da177e4SLinus Torvalds #endif /* IPCONFIG_DHCP */
11111da177e4SLinus Torvalds 
11121da177e4SLinus Torvalds 		ext = &b->exten[4];
11131da177e4SLinus Torvalds 		while (ext < end && *ext != 0xff) {
11141da177e4SLinus Torvalds 			u8 *opt = ext++;
11151da177e4SLinus Torvalds 			if (*opt == 0)	/* Padding */
11161da177e4SLinus Torvalds 				continue;
11171da177e4SLinus Torvalds 			ext += *ext + 1;
11181da177e4SLinus Torvalds 			if (ext < end)
11191da177e4SLinus Torvalds 				ic_do_bootp_ext(opt);
11201da177e4SLinus Torvalds 		}
11211da177e4SLinus Torvalds 	}
11221da177e4SLinus Torvalds 
11231da177e4SLinus Torvalds 	/* We have a winner! */
11242647cffbSUwe Kleine-König 	ic_dev = d;
11251da177e4SLinus Torvalds 	ic_myaddr = b->your_ip;
11261da177e4SLinus Torvalds 	ic_servaddr = b->server_ip;
11279dd4a13aSPhilippe De Muyter 	ic_addrservaddr = b->iph.saddr;
11285a874db4SAl Viro 	if (ic_gateway == NONE && b->relay_ip)
11291da177e4SLinus Torvalds 		ic_gateway = b->relay_ip;
11305a874db4SAl Viro 	if (ic_nameservers[0] == NONE)
11311da177e4SLinus Torvalds 		ic_nameservers[0] = ic_servaddr;
11321da177e4SLinus Torvalds 	ic_got_reply = IC_BOOTP;
11331da177e4SLinus Torvalds 
11341da177e4SLinus Torvalds drop_unlock:
11351da177e4SLinus Torvalds 	/* Show's over.  Nothing to see here.  */
11361da177e4SLinus Torvalds 	spin_unlock(&ic_recv_lock);
11371da177e4SLinus Torvalds 
11381da177e4SLinus Torvalds drop:
11391da177e4SLinus Torvalds 	/* Throw the packet out. */
11401da177e4SLinus Torvalds 	kfree_skb(skb);
11411da177e4SLinus Torvalds 
11421da177e4SLinus Torvalds 	return 0;
11431da177e4SLinus Torvalds }
11441da177e4SLinus Torvalds 
11451da177e4SLinus Torvalds 
11461da177e4SLinus Torvalds #endif
11471da177e4SLinus Torvalds 
11481da177e4SLinus Torvalds 
11491da177e4SLinus Torvalds /*
11501da177e4SLinus Torvalds  *	Dynamic IP configuration -- DHCP, BOOTP, RARP.
11511da177e4SLinus Torvalds  */
11521da177e4SLinus Torvalds 
11531da177e4SLinus Torvalds #ifdef IPCONFIG_DYNAMIC
11541da177e4SLinus Torvalds 
11551da177e4SLinus Torvalds static int __init ic_dynamic(void)
11561da177e4SLinus Torvalds {
11571da177e4SLinus Torvalds 	int retries;
11581da177e4SLinus Torvalds 	struct ic_device *d;
11591da177e4SLinus Torvalds 	unsigned long start_jiffies, timeout, jiff;
11601da177e4SLinus Torvalds 	int do_bootp = ic_proto_have_if & IC_BOOTP;
11611da177e4SLinus Torvalds 	int do_rarp = ic_proto_have_if & IC_RARP;
11621da177e4SLinus Torvalds 
11631da177e4SLinus Torvalds 	/*
11641da177e4SLinus Torvalds 	 * If none of DHCP/BOOTP/RARP was selected, return with an error.
11651da177e4SLinus Torvalds 	 * This routine gets only called when some pieces of information
11661da177e4SLinus Torvalds 	 * are missing, and without DHCP/BOOTP/RARP we are unable to get it.
11671da177e4SLinus Torvalds 	 */
11681da177e4SLinus Torvalds 	if (!ic_proto_enabled) {
1169058bd4d2SJoe Perches 		pr_err("IP-Config: Incomplete network configuration information\n");
11701da177e4SLinus Torvalds 		return -1;
11711da177e4SLinus Torvalds 	}
11721da177e4SLinus Torvalds 
11731da177e4SLinus Torvalds #ifdef IPCONFIG_BOOTP
11741da177e4SLinus Torvalds 	if ((ic_proto_enabled ^ ic_proto_have_if) & IC_BOOTP)
1175058bd4d2SJoe Perches 		pr_err("DHCP/BOOTP: No suitable device found\n");
11761da177e4SLinus Torvalds #endif
11771da177e4SLinus Torvalds #ifdef IPCONFIG_RARP
11781da177e4SLinus Torvalds 	if ((ic_proto_enabled ^ ic_proto_have_if) & IC_RARP)
1179058bd4d2SJoe Perches 		pr_err("RARP: No suitable device found\n");
11801da177e4SLinus Torvalds #endif
11811da177e4SLinus Torvalds 
11821da177e4SLinus Torvalds 	if (!ic_proto_have_if)
11831da177e4SLinus Torvalds 		/* Error message already printed */
11841da177e4SLinus Torvalds 		return -1;
11851da177e4SLinus Torvalds 
11861da177e4SLinus Torvalds 	/*
11871da177e4SLinus Torvalds 	 * Setup protocols
11881da177e4SLinus Torvalds 	 */
11891da177e4SLinus Torvalds #ifdef IPCONFIG_BOOTP
11901da177e4SLinus Torvalds 	if (do_bootp)
11911da177e4SLinus Torvalds 		ic_bootp_init();
11921da177e4SLinus Torvalds #endif
11931da177e4SLinus Torvalds #ifdef IPCONFIG_RARP
11941da177e4SLinus Torvalds 	if (do_rarp)
11951da177e4SLinus Torvalds 		ic_rarp_init();
11961da177e4SLinus Torvalds #endif
11971da177e4SLinus Torvalds 
11981da177e4SLinus Torvalds 	/*
11991da177e4SLinus Torvalds 	 * Send requests and wait, until we get an answer. This loop
12001da177e4SLinus Torvalds 	 * seems to be a terrible waste of CPU time, but actually there is
12011da177e4SLinus Torvalds 	 * only one process running at all, so we don't need to use any
12021da177e4SLinus Torvalds 	 * scheduler functions.
12031da177e4SLinus Torvalds 	 * [Actually we could now, but the nothing else running note still
12041da177e4SLinus Torvalds 	 *  applies.. - AC]
12051da177e4SLinus Torvalds 	 */
1206058bd4d2SJoe Perches 	pr_notice("Sending %s%s%s requests .",
12071da177e4SLinus Torvalds 		  do_bootp
12081da177e4SLinus Torvalds 		  ? ((ic_proto_enabled & IC_USE_DHCP) ? "DHCP" : "BOOTP") : "",
12091da177e4SLinus Torvalds 		  (do_bootp && do_rarp) ? " and " : "",
12101da177e4SLinus Torvalds 		  do_rarp ? "RARP" : "");
12111da177e4SLinus Torvalds 
12121da177e4SLinus Torvalds 	start_jiffies = jiffies;
12131da177e4SLinus Torvalds 	d = ic_first_dev;
12141da177e4SLinus Torvalds 	retries = CONF_SEND_RETRIES;
12151da177e4SLinus Torvalds 	get_random_bytes(&timeout, sizeof(timeout));
121695c96174SEric Dumazet 	timeout = CONF_BASE_TIMEOUT + (timeout % (unsigned int) CONF_TIMEOUT_RANDOM);
12171da177e4SLinus Torvalds 	for (;;) {
1218405fd707SDavid S. Miller #ifdef IPCONFIG_BOOTP
12191da177e4SLinus Torvalds 		if (do_bootp && (d->able & IC_BOOTP))
12201da177e4SLinus Torvalds 			ic_bootp_send_if(d, jiffies - start_jiffies);
12211da177e4SLinus Torvalds #endif
12221da177e4SLinus Torvalds #ifdef IPCONFIG_RARP
12231da177e4SLinus Torvalds 		if (do_rarp && (d->able & IC_RARP))
12241da177e4SLinus Torvalds 			ic_rarp_send_if(d);
12251da177e4SLinus Torvalds #endif
12261da177e4SLinus Torvalds 
1227e0688534SUwe Kleine-König 		if (!d->next) {
1228e0688534SUwe Kleine-König 			jiff = jiffies + timeout;
1229121caf57SNishanth Aravamudan 			while (time_before(jiffies, jiff) && !ic_got_reply)
1230121caf57SNishanth Aravamudan 				schedule_timeout_uninterruptible(1);
1231e0688534SUwe Kleine-König 		}
12321da177e4SLinus Torvalds #ifdef IPCONFIG_DHCP
12331da177e4SLinus Torvalds 		/* DHCP isn't done until we get a DHCPACK. */
12349d4fb27dSJoe Perches 		if ((ic_got_reply & IC_BOOTP) &&
12359d4fb27dSJoe Perches 		    (ic_proto_enabled & IC_USE_DHCP) &&
12369d4fb27dSJoe Perches 		    ic_dhcp_msgtype != DHCPACK) {
12371da177e4SLinus Torvalds 			ic_got_reply = 0;
12382647cffbSUwe Kleine-König 			/* continue on device that got the reply */
12392647cffbSUwe Kleine-König 			d = ic_dev;
12406c1c36b0SGeert Uytterhoeven 			pr_cont(",");
12411da177e4SLinus Torvalds 			continue;
12421da177e4SLinus Torvalds 		}
12431da177e4SLinus Torvalds #endif /* IPCONFIG_DHCP */
12441da177e4SLinus Torvalds 
12451da177e4SLinus Torvalds 		if (ic_got_reply) {
12466c1c36b0SGeert Uytterhoeven 			pr_cont(" OK\n");
12471da177e4SLinus Torvalds 			break;
12481da177e4SLinus Torvalds 		}
12491da177e4SLinus Torvalds 
12501da177e4SLinus Torvalds 		if ((d = d->next))
12511da177e4SLinus Torvalds 			continue;
12521da177e4SLinus Torvalds 
12531da177e4SLinus Torvalds 		if (! --retries) {
12546c1c36b0SGeert Uytterhoeven 			pr_cont(" timed out!\n");
12551da177e4SLinus Torvalds 			break;
12561da177e4SLinus Torvalds 		}
12571da177e4SLinus Torvalds 
12581da177e4SLinus Torvalds 		d = ic_first_dev;
12591da177e4SLinus Torvalds 
12601da177e4SLinus Torvalds 		timeout = timeout CONF_TIMEOUT_MULT;
12611da177e4SLinus Torvalds 		if (timeout > CONF_TIMEOUT_MAX)
12621da177e4SLinus Torvalds 			timeout = CONF_TIMEOUT_MAX;
12631da177e4SLinus Torvalds 
12646c1c36b0SGeert Uytterhoeven 		pr_cont(".");
12651da177e4SLinus Torvalds 	}
12661da177e4SLinus Torvalds 
12671da177e4SLinus Torvalds #ifdef IPCONFIG_BOOTP
12681da177e4SLinus Torvalds 	if (do_bootp)
12691da177e4SLinus Torvalds 		ic_bootp_cleanup();
12701da177e4SLinus Torvalds #endif
12711da177e4SLinus Torvalds #ifdef IPCONFIG_RARP
12721da177e4SLinus Torvalds 	if (do_rarp)
12731da177e4SLinus Torvalds 		ic_rarp_cleanup();
12741da177e4SLinus Torvalds #endif
12751da177e4SLinus Torvalds 
12767a1af5d7SMaxime Bizon 	if (!ic_got_reply) {
12775a874db4SAl Viro 		ic_myaddr = NONE;
12781da177e4SLinus Torvalds 		return -1;
12797a1af5d7SMaxime Bizon 	}
12801da177e4SLinus Torvalds 
12816c1c36b0SGeert Uytterhoeven 	pr_info("IP-Config: Got %s answer from %pI4, my address is %pI4\n",
12821da177e4SLinus Torvalds 		((ic_got_reply & IC_RARP) ? "RARP"
12831da177e4SLinus Torvalds 		: (ic_proto_enabled & IC_USE_DHCP) ? "DHCP" : "BOOTP"),
12846c1c36b0SGeert Uytterhoeven 		&ic_addrservaddr, &ic_myaddr);
12851da177e4SLinus Torvalds 
12861da177e4SLinus Torvalds 	return 0;
12871da177e4SLinus Torvalds }
12881da177e4SLinus Torvalds 
12891da177e4SLinus Torvalds #endif /* IPCONFIG_DYNAMIC */
12901da177e4SLinus Torvalds 
12911da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
12921da177e4SLinus Torvalds 
12931da177e4SLinus Torvalds static int pnp_seq_show(struct seq_file *seq, void *v)
12941da177e4SLinus Torvalds {
12951da177e4SLinus Torvalds 	int i;
12961da177e4SLinus Torvalds 
12971da177e4SLinus Torvalds 	if (ic_proto_used & IC_PROTO)
12981da177e4SLinus Torvalds 		seq_printf(seq, "#PROTO: %s\n",
12991da177e4SLinus Torvalds 			   (ic_proto_used & IC_RARP) ? "RARP"
13001da177e4SLinus Torvalds 			   : (ic_proto_used & IC_USE_DHCP) ? "DHCP" : "BOOTP");
13011da177e4SLinus Torvalds 	else
13021da177e4SLinus Torvalds 		seq_puts(seq, "#MANUAL\n");
13031da177e4SLinus Torvalds 
13041da177e4SLinus Torvalds 	if (ic_domain[0])
13051da177e4SLinus Torvalds 		seq_printf(seq,
13061da177e4SLinus Torvalds 			   "domain %s\n", ic_domain);
13071da177e4SLinus Torvalds 	for (i = 0; i < CONF_NAMESERVERS_MAX; i++) {
13085a874db4SAl Viro 		if (ic_nameservers[i] != NONE)
1309673d57e7SHarvey Harrison 			seq_printf(seq, "nameserver %pI4\n",
1310673d57e7SHarvey Harrison 				   &ic_nameservers[i]);
13111da177e4SLinus Torvalds 	}
13125a874db4SAl Viro 	if (ic_servaddr != NONE)
1313673d57e7SHarvey Harrison 		seq_printf(seq, "bootserver %pI4\n",
1314673d57e7SHarvey Harrison 			   &ic_servaddr);
13151da177e4SLinus Torvalds 	return 0;
13161da177e4SLinus Torvalds }
13171da177e4SLinus Torvalds 
13181da177e4SLinus Torvalds static int pnp_seq_open(struct inode *indoe, struct file *file)
13191da177e4SLinus Torvalds {
13201da177e4SLinus Torvalds 	return single_open(file, pnp_seq_show, NULL);
13211da177e4SLinus Torvalds }
13221da177e4SLinus Torvalds 
13239a32144eSArjan van de Ven static const struct file_operations pnp_seq_fops = {
13241da177e4SLinus Torvalds 	.owner		= THIS_MODULE,
13251da177e4SLinus Torvalds 	.open		= pnp_seq_open,
13261da177e4SLinus Torvalds 	.read		= seq_read,
13271da177e4SLinus Torvalds 	.llseek		= seq_lseek,
13281da177e4SLinus Torvalds 	.release	= single_release,
13291da177e4SLinus Torvalds };
13301da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */
13311da177e4SLinus Torvalds 
13321da177e4SLinus Torvalds /*
13331da177e4SLinus Torvalds  *  Extract IP address from the parameter string if needed. Note that we
13341da177e4SLinus Torvalds  *  need to have root_server_addr set _before_ IPConfig gets called as it
13351da177e4SLinus Torvalds  *  can override it.
13361da177e4SLinus Torvalds  */
13375a874db4SAl Viro __be32 __init root_nfs_parse_addr(char *name)
13381da177e4SLinus Torvalds {
13395a874db4SAl Viro 	__be32 addr;
13401da177e4SLinus Torvalds 	int octets = 0;
13411da177e4SLinus Torvalds 	char *cp, *cq;
13421da177e4SLinus Torvalds 
13431da177e4SLinus Torvalds 	cp = cq = name;
13441da177e4SLinus Torvalds 	while (octets < 4) {
13451da177e4SLinus Torvalds 		while (*cp >= '0' && *cp <= '9')
13461da177e4SLinus Torvalds 			cp++;
13471da177e4SLinus Torvalds 		if (cp == cq || cp - cq > 3)
13481da177e4SLinus Torvalds 			break;
13491da177e4SLinus Torvalds 		if (*cp == '.' || octets == 3)
13501da177e4SLinus Torvalds 			octets++;
13511da177e4SLinus Torvalds 		if (octets < 4)
13521da177e4SLinus Torvalds 			cp++;
13531da177e4SLinus Torvalds 		cq = cp;
13541da177e4SLinus Torvalds 	}
13551da177e4SLinus Torvalds 	if (octets == 4 && (*cp == ':' || *cp == '\0')) {
13561da177e4SLinus Torvalds 		if (*cp == ':')
13571da177e4SLinus Torvalds 			*cp++ = '\0';
13581da177e4SLinus Torvalds 		addr = in_aton(name);
13591da177e4SLinus Torvalds 		memmove(name, cp, strlen(cp) + 1);
13601da177e4SLinus Torvalds 	} else
13615a874db4SAl Viro 		addr = NONE;
13621da177e4SLinus Torvalds 
13631da177e4SLinus Torvalds 	return addr;
13641da177e4SLinus Torvalds }
13651da177e4SLinus Torvalds 
1366964ad81cSDavid S. Miller #define DEVICE_WAIT_MAX		12 /* 12 seconds */
1367964ad81cSDavid S. Miller 
1368964ad81cSDavid S. Miller static int __init wait_for_devices(void)
1369964ad81cSDavid S. Miller {
1370964ad81cSDavid S. Miller 	int i;
1371964ad81cSDavid S. Miller 
1372964ad81cSDavid S. Miller 	for (i = 0; i < DEVICE_WAIT_MAX; i++) {
1373964ad81cSDavid S. Miller 		struct net_device *dev;
1374964ad81cSDavid S. Miller 		int found = 0;
1375964ad81cSDavid S. Miller 
1376964ad81cSDavid S. Miller 		rtnl_lock();
1377964ad81cSDavid S. Miller 		for_each_netdev(&init_net, dev) {
13783fb72f1eSMicha Nelissen 			if (ic_is_init_dev(dev)) {
1379964ad81cSDavid S. Miller 				found = 1;
1380964ad81cSDavid S. Miller 				break;
1381964ad81cSDavid S. Miller 			}
1382964ad81cSDavid S. Miller 		}
1383964ad81cSDavid S. Miller 		rtnl_unlock();
1384964ad81cSDavid S. Miller 		if (found)
1385964ad81cSDavid S. Miller 			return 0;
1386964ad81cSDavid S. Miller 		ssleep(1);
1387964ad81cSDavid S. Miller 	}
1388964ad81cSDavid S. Miller 	return -ENODEV;
1389964ad81cSDavid S. Miller }
1390964ad81cSDavid S. Miller 
13911da177e4SLinus Torvalds /*
13921da177e4SLinus Torvalds  *	IP Autoconfig dispatcher.
13931da177e4SLinus Torvalds  */
13941da177e4SLinus Torvalds 
13951da177e4SLinus Torvalds static int __init ip_auto_config(void)
13961da177e4SLinus Torvalds {
13975a874db4SAl Viro 	__be32 addr;
13989d8dba6cSBenjamin Zores #ifdef IPCONFIG_DYNAMIC
13999d8dba6cSBenjamin Zores 	int retries = CONF_OPEN_RETRIES;
14009d8dba6cSBenjamin Zores #endif
1401964ad81cSDavid S. Miller 	int err;
14025e953778SChristoph Fritz 	unsigned int i;
14031da177e4SLinus Torvalds 
14041da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
1405d4beaa66SGao feng 	proc_create("pnp", S_IRUGO, init_net.proc_net, &pnp_seq_fops);
14061da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */
14071da177e4SLinus Torvalds 
14081da177e4SLinus Torvalds 	if (!ic_enable)
14091da177e4SLinus Torvalds 		return 0;
14101da177e4SLinus Torvalds 
141109605cc1SBastian Stender 	pr_debug("IP-Config: Entered.\n");
14121da177e4SLinus Torvalds #ifdef IPCONFIG_DYNAMIC
14131da177e4SLinus Torvalds  try_try_again:
14141da177e4SLinus Torvalds #endif
1415964ad81cSDavid S. Miller 	/* Wait for devices to appear */
1416964ad81cSDavid S. Miller 	err = wait_for_devices();
1417964ad81cSDavid S. Miller 	if (err)
1418964ad81cSDavid S. Miller 		return err;
14191da177e4SLinus Torvalds 
14201da177e4SLinus Torvalds 	/* Setup all network devices */
1421964ad81cSDavid S. Miller 	err = ic_open_devs();
1422964ad81cSDavid S. Miller 	if (err)
1423964ad81cSDavid S. Miller 		return err;
14241da177e4SLinus Torvalds 
14251da177e4SLinus Torvalds 	/* Give drivers a chance to settle */
14263fb72f1eSMicha Nelissen 	msleep(CONF_POST_OPEN);
14271da177e4SLinus Torvalds 
14281da177e4SLinus Torvalds 	/*
14291da177e4SLinus Torvalds 	 * If the config information is insufficient (e.g., our IP address or
14301da177e4SLinus Torvalds 	 * IP address of the boot server is missing or we have multiple network
14311da177e4SLinus Torvalds 	 * interfaces and no default was set), use BOOTP or RARP to get the
14321da177e4SLinus Torvalds 	 * missing values.
14331da177e4SLinus Torvalds 	 */
14345a874db4SAl Viro 	if (ic_myaddr == NONE ||
14351da177e4SLinus Torvalds #ifdef CONFIG_ROOT_NFS
14369d4fb27dSJoe Perches 	    (root_server_addr == NONE &&
14379d4fb27dSJoe Perches 	     ic_servaddr == NONE &&
14389d4fb27dSJoe Perches 	     ROOT_DEV == Root_NFS) ||
14391da177e4SLinus Torvalds #endif
14401da177e4SLinus Torvalds 	    ic_first_dev->next) {
14411da177e4SLinus Torvalds #ifdef IPCONFIG_DYNAMIC
14421da177e4SLinus Torvalds 		if (ic_dynamic() < 0) {
14431da177e4SLinus Torvalds 			ic_close_devs();
14441da177e4SLinus Torvalds 
14451da177e4SLinus Torvalds 			/*
14461da177e4SLinus Torvalds 			 * I don't know why, but sometimes the
14471da177e4SLinus Torvalds 			 * eepro100 driver (at least) gets upset and
14481da177e4SLinus Torvalds 			 * doesn't work the first time it's opened.
14491da177e4SLinus Torvalds 			 * But then if you close it and reopen it, it
14501da177e4SLinus Torvalds 			 * works just fine.  So we need to try that at
14511da177e4SLinus Torvalds 			 * least once before giving up.
14521da177e4SLinus Torvalds 			 *
14531da177e4SLinus Torvalds 			 * Also, if the root will be NFS-mounted, we
14541da177e4SLinus Torvalds 			 * have nowhere to go if DHCP fails.  So we
14551da177e4SLinus Torvalds 			 * just have to keep trying forever.
14561da177e4SLinus Torvalds 			 *
14571da177e4SLinus Torvalds 			 * 				-- Chip
14581da177e4SLinus Torvalds 			 */
14591da177e4SLinus Torvalds #ifdef CONFIG_ROOT_NFS
14601da177e4SLinus Torvalds 			if (ROOT_DEV ==  Root_NFS) {
1461058bd4d2SJoe Perches 				pr_err("IP-Config: Retrying forever (NFS root)...\n");
14621da177e4SLinus Torvalds 				goto try_try_again;
14631da177e4SLinus Torvalds 			}
14641da177e4SLinus Torvalds #endif
14651da177e4SLinus Torvalds 
14661da177e4SLinus Torvalds 			if (--retries) {
1467058bd4d2SJoe Perches 				pr_err("IP-Config: Reopening network devices...\n");
14681da177e4SLinus Torvalds 				goto try_try_again;
14691da177e4SLinus Torvalds 			}
14701da177e4SLinus Torvalds 
14711da177e4SLinus Torvalds 			/* Oh, well.  At least we tried. */
1472058bd4d2SJoe Perches 			pr_err("IP-Config: Auto-configuration of network failed\n");
14731da177e4SLinus Torvalds 			return -1;
14741da177e4SLinus Torvalds 		}
14751da177e4SLinus Torvalds #else /* !DYNAMIC */
1476058bd4d2SJoe Perches 		pr_err("IP-Config: Incomplete network configuration information\n");
14771da177e4SLinus Torvalds 		ic_close_devs();
14781da177e4SLinus Torvalds 		return -1;
14791da177e4SLinus Torvalds #endif /* IPCONFIG_DYNAMIC */
14801da177e4SLinus Torvalds 	} else {
14811da177e4SLinus Torvalds 		/* Device selected manually or only one device -> use it */
14822647cffbSUwe Kleine-König 		ic_dev = ic_first_dev;
14831da177e4SLinus Torvalds 	}
14841da177e4SLinus Torvalds 
14851da177e4SLinus Torvalds 	addr = root_nfs_parse_addr(root_server_path);
14865a874db4SAl Viro 	if (root_server_addr == NONE)
14871da177e4SLinus Torvalds 		root_server_addr = addr;
14881da177e4SLinus Torvalds 
14891da177e4SLinus Torvalds 	/*
149025985edcSLucas De Marchi 	 * Use defaults wherever applicable.
14911da177e4SLinus Torvalds 	 */
14921da177e4SLinus Torvalds 	if (ic_defaults() < 0)
14931da177e4SLinus Torvalds 		return -1;
14941da177e4SLinus Torvalds 
14951da177e4SLinus Torvalds 	/*
14961da177e4SLinus Torvalds 	 * Record which protocol was actually used.
14971da177e4SLinus Torvalds 	 */
14981da177e4SLinus Torvalds #ifdef IPCONFIG_DYNAMIC
14991da177e4SLinus Torvalds 	ic_proto_used = ic_got_reply | (ic_proto_enabled & IC_USE_DHCP);
15001da177e4SLinus Torvalds #endif
15011da177e4SLinus Torvalds 
15021da177e4SLinus Torvalds #ifndef IPCONFIG_SILENT
15031da177e4SLinus Torvalds 	/*
15041da177e4SLinus Torvalds 	 * Clue in the operator.
15051da177e4SLinus Torvalds 	 */
1506058bd4d2SJoe Perches 	pr_info("IP-Config: Complete:\n");
15079ecd1c3dSClaudio Fontana 
15089ecd1c3dSClaudio Fontana 	pr_info("     device=%s, hwaddr=%*phC, ipaddr=%pI4, mask=%pI4, gw=%pI4\n",
15092647cffbSUwe Kleine-König 		ic_dev->dev->name, ic_dev->dev->addr_len, ic_dev->dev->dev_addr,
15109ecd1c3dSClaudio Fontana 		&ic_myaddr, &ic_netmask, &ic_gateway);
1511058bd4d2SJoe Perches 	pr_info("     host=%s, domain=%s, nis-domain=%s\n",
1512e9ff3990SSerge E. Hallyn 		utsname()->nodename, ic_domain, utsname()->domainname);
1513058bd4d2SJoe Perches 	pr_info("     bootserver=%pI4, rootserver=%pI4, rootpath=%s",
1514058bd4d2SJoe Perches 		&ic_servaddr, &root_server_addr, root_server_path);
15159643f455SChris Friesen 	if (ic_dev_mtu)
1516058bd4d2SJoe Perches 		pr_cont(", mtu=%d", ic_dev_mtu);
15175e953778SChristoph Fritz 	for (i = 0; i < CONF_NAMESERVERS_MAX; i++)
15185e953778SChristoph Fritz 		if (ic_nameservers[i] != NONE) {
15196c1c36b0SGeert Uytterhoeven 			pr_cont("     nameserver%u=%pI4",
15205e953778SChristoph Fritz 				i, &ic_nameservers[i]);
15215e953778SChristoph Fritz 			break;
15225e953778SChristoph Fritz 		}
15235e953778SChristoph Fritz 	for (i++; i < CONF_NAMESERVERS_MAX; i++)
15245e953778SChristoph Fritz 		if (ic_nameservers[i] != NONE)
15256c1c36b0SGeert Uytterhoeven 			pr_cont(", nameserver%u=%pI4", i, &ic_nameservers[i]);
15266c1c36b0SGeert Uytterhoeven 	pr_cont("\n");
15271da177e4SLinus Torvalds #endif /* !SILENT */
15281da177e4SLinus Torvalds 
15299c706a49SUwe Kleine-König 	/*
15309c706a49SUwe Kleine-König 	 * Close all network devices except the device we've
15319c706a49SUwe Kleine-König 	 * autoconfigured and set up routes.
15329c706a49SUwe Kleine-König 	 */
15339c706a49SUwe Kleine-König 	if (ic_setup_if() < 0 || ic_setup_routes() < 0)
1534*d2d371aeSThierry Reding 		err = -1;
1535*d2d371aeSThierry Reding 	else
1536*d2d371aeSThierry Reding 		err = 0;
15379c706a49SUwe Kleine-König 
1538*d2d371aeSThierry Reding 	ic_close_devs();
15399c706a49SUwe Kleine-König 
1540*d2d371aeSThierry Reding 	return err;
15411da177e4SLinus Torvalds }
15421da177e4SLinus Torvalds 
15431da177e4SLinus Torvalds late_initcall(ip_auto_config);
15441da177e4SLinus Torvalds 
15451da177e4SLinus Torvalds 
15461da177e4SLinus Torvalds /*
15471da177e4SLinus Torvalds  *  Decode any IP configuration options in the "ip=" or "nfsaddrs=" kernel
1548dc7a0816SJ. Bruce Fields  *  command line parameter.  See Documentation/filesystems/nfs/nfsroot.txt.
15491da177e4SLinus Torvalds  */
15501da177e4SLinus Torvalds static int __init ic_proto_name(char *name)
15511da177e4SLinus Torvalds {
15521da177e4SLinus Torvalds 	if (!strcmp(name, "on") || !strcmp(name, "any")) {
15531da177e4SLinus Torvalds 		return 1;
15541da177e4SLinus Torvalds 	}
1555a6c05c3dSSimon Horman 	if (!strcmp(name, "off") || !strcmp(name, "none")) {
155692ffb85dSAmos Waterland 		return 0;
1557a6c05c3dSSimon Horman 	}
15581da177e4SLinus Torvalds #ifdef CONFIG_IP_PNP_DHCP
155926fb342cSLi RongQing 	else if (!strncmp(name, "dhcp", 4)) {
156026fb342cSLi RongQing 		char *client_id;
156126fb342cSLi RongQing 
15621da177e4SLinus Torvalds 		ic_proto_enabled &= ~IC_RARP;
156326fb342cSLi RongQing 		client_id = strstr(name, "dhcp,");
156426fb342cSLi RongQing 		if (client_id) {
156526fb342cSLi RongQing 			char *v;
156626fb342cSLi RongQing 
156726fb342cSLi RongQing 			client_id = client_id + 5;
156826fb342cSLi RongQing 			v = strchr(client_id, ',');
156926fb342cSLi RongQing 			if (!v)
157026fb342cSLi RongQing 				return 1;
157126fb342cSLi RongQing 			*v = 0;
157226fb342cSLi RongQing 			if (kstrtou8(client_id, 0, dhcp_client_identifier))
157309605cc1SBastian Stender 				pr_debug("DHCP: Invalid client identifier type\n");
157426fb342cSLi RongQing 			strncpy(dhcp_client_identifier + 1, v + 1, 251);
157526fb342cSLi RongQing 			*v = ',';
157626fb342cSLi RongQing 		}
15771da177e4SLinus Torvalds 		return 1;
15781da177e4SLinus Torvalds 	}
15791da177e4SLinus Torvalds #endif
15801da177e4SLinus Torvalds #ifdef CONFIG_IP_PNP_BOOTP
15811da177e4SLinus Torvalds 	else if (!strcmp(name, "bootp")) {
15821da177e4SLinus Torvalds 		ic_proto_enabled &= ~(IC_RARP | IC_USE_DHCP);
15831da177e4SLinus Torvalds 		return 1;
15841da177e4SLinus Torvalds 	}
15851da177e4SLinus Torvalds #endif
15861da177e4SLinus Torvalds #ifdef CONFIG_IP_PNP_RARP
15871da177e4SLinus Torvalds 	else if (!strcmp(name, "rarp")) {
15881da177e4SLinus Torvalds 		ic_proto_enabled &= ~(IC_BOOTP | IC_USE_DHCP);
15891da177e4SLinus Torvalds 		return 1;
15901da177e4SLinus Torvalds 	}
15911da177e4SLinus Torvalds #endif
15921da177e4SLinus Torvalds #ifdef IPCONFIG_DYNAMIC
15931da177e4SLinus Torvalds 	else if (!strcmp(name, "both")) {
15941da177e4SLinus Torvalds 		ic_proto_enabled &= ~IC_USE_DHCP; /* backward compat :-( */
15951da177e4SLinus Torvalds 		return 1;
15961da177e4SLinus Torvalds 	}
15971da177e4SLinus Torvalds #endif
15981da177e4SLinus Torvalds 	return 0;
15991da177e4SLinus Torvalds }
16001da177e4SLinus Torvalds 
16011da177e4SLinus Torvalds static int __init ip_auto_config_setup(char *addrs)
16021da177e4SLinus Torvalds {
16031da177e4SLinus Torvalds 	char *cp, *ip, *dp;
16041da177e4SLinus Torvalds 	int num = 0;
16051da177e4SLinus Torvalds 
16061da177e4SLinus Torvalds 	ic_set_manually = 1;
16079cecd07cSSimon Horman 	ic_enable = 1;
16081da177e4SLinus Torvalds 
160992ffb85dSAmos Waterland 	/*
161092ffb85dSAmos Waterland 	 * If any dhcp, bootp etc options are set, leave autoconfig on
161192ffb85dSAmos Waterland 	 * and skip the below static IP processing.
161292ffb85dSAmos Waterland 	 */
16131da177e4SLinus Torvalds 	if (ic_proto_name(addrs))
16141da177e4SLinus Torvalds 		return 1;
16151da177e4SLinus Torvalds 
161692ffb85dSAmos Waterland 	/* If no static IP is given, turn off autoconfig and bail.  */
161792ffb85dSAmos Waterland 	if (*addrs == 0 ||
161892ffb85dSAmos Waterland 	    strcmp(addrs, "off") == 0 ||
161992ffb85dSAmos Waterland 	    strcmp(addrs, "none") == 0) {
162092ffb85dSAmos Waterland 		ic_enable = 0;
162192ffb85dSAmos Waterland 		return 1;
162292ffb85dSAmos Waterland 	}
162392ffb85dSAmos Waterland 
16245e953778SChristoph Fritz 	ic_nameservers_predef();
16255e953778SChristoph Fritz 
162692ffb85dSAmos Waterland 	/* Parse string for static IP assignment.  */
16271da177e4SLinus Torvalds 	ip = addrs;
16281da177e4SLinus Torvalds 	while (ip && *ip) {
16291da177e4SLinus Torvalds 		if ((cp = strchr(ip, ':')))
16301da177e4SLinus Torvalds 			*cp++ = '\0';
16311da177e4SLinus Torvalds 		if (strlen(ip) > 0) {
163209605cc1SBastian Stender 			pr_debug("IP-Config: Parameter #%d: `%s'\n", num, ip);
16331da177e4SLinus Torvalds 			switch (num) {
16341da177e4SLinus Torvalds 			case 0:
1635e6f1cebfSAl Viro 				if ((ic_myaddr = in_aton(ip)) == ANY)
16365a874db4SAl Viro 					ic_myaddr = NONE;
16371da177e4SLinus Torvalds 				break;
16381da177e4SLinus Torvalds 			case 1:
1639e6f1cebfSAl Viro 				if ((ic_servaddr = in_aton(ip)) == ANY)
16405a874db4SAl Viro 					ic_servaddr = NONE;
16411da177e4SLinus Torvalds 				break;
16421da177e4SLinus Torvalds 			case 2:
1643e6f1cebfSAl Viro 				if ((ic_gateway = in_aton(ip)) == ANY)
16445a874db4SAl Viro 					ic_gateway = NONE;
16451da177e4SLinus Torvalds 				break;
16461da177e4SLinus Torvalds 			case 3:
1647e6f1cebfSAl Viro 				if ((ic_netmask = in_aton(ip)) == ANY)
16485a874db4SAl Viro 					ic_netmask = NONE;
16491da177e4SLinus Torvalds 				break;
16501da177e4SLinus Torvalds 			case 4:
16511da177e4SLinus Torvalds 				if ((dp = strchr(ip, '.'))) {
16521da177e4SLinus Torvalds 					*dp++ = '\0';
1653e9ff3990SSerge E. Hallyn 					strlcpy(utsname()->domainname, dp,
1654e9ff3990SSerge E. Hallyn 						sizeof(utsname()->domainname));
16551da177e4SLinus Torvalds 				}
1656e9ff3990SSerge E. Hallyn 				strlcpy(utsname()->nodename, ip,
1657e9ff3990SSerge E. Hallyn 					sizeof(utsname()->nodename));
16581da177e4SLinus Torvalds 				ic_host_name_set = 1;
16591da177e4SLinus Torvalds 				break;
16601da177e4SLinus Torvalds 			case 5:
16611da177e4SLinus Torvalds 				strlcpy(user_dev_name, ip, sizeof(user_dev_name));
16621da177e4SLinus Torvalds 				break;
16631da177e4SLinus Torvalds 			case 6:
166492ffb85dSAmos Waterland 				if (ic_proto_name(ip) == 0 &&
166592ffb85dSAmos Waterland 				    ic_myaddr == NONE) {
166692ffb85dSAmos Waterland 					ic_enable = 0;
166792ffb85dSAmos Waterland 				}
16681da177e4SLinus Torvalds 				break;
16695e953778SChristoph Fritz 			case 7:
16705e953778SChristoph Fritz 				if (CONF_NAMESERVERS_MAX >= 1) {
16715e953778SChristoph Fritz 					ic_nameservers[0] = in_aton(ip);
16725e953778SChristoph Fritz 					if (ic_nameservers[0] == ANY)
16735e953778SChristoph Fritz 						ic_nameservers[0] = NONE;
16745e953778SChristoph Fritz 				}
16755e953778SChristoph Fritz 				break;
16765e953778SChristoph Fritz 			case 8:
16775e953778SChristoph Fritz 				if (CONF_NAMESERVERS_MAX >= 2) {
16785e953778SChristoph Fritz 					ic_nameservers[1] = in_aton(ip);
16795e953778SChristoph Fritz 					if (ic_nameservers[1] == ANY)
16805e953778SChristoph Fritz 						ic_nameservers[1] = NONE;
16815e953778SChristoph Fritz 				}
16825e953778SChristoph Fritz 				break;
16831da177e4SLinus Torvalds 			}
16841da177e4SLinus Torvalds 		}
16851da177e4SLinus Torvalds 		ip = cp;
16861da177e4SLinus Torvalds 		num++;
16871da177e4SLinus Torvalds 	}
16881da177e4SLinus Torvalds 
16891da177e4SLinus Torvalds 	return 1;
16901da177e4SLinus Torvalds }
1691b37f4d7bSEldad Zack __setup("ip=", ip_auto_config_setup);
16921da177e4SLinus Torvalds 
16931da177e4SLinus Torvalds static int __init nfsaddrs_config_setup(char *addrs)
16941da177e4SLinus Torvalds {
16951da177e4SLinus Torvalds 	return ip_auto_config_setup(addrs);
16961da177e4SLinus Torvalds }
1697b37f4d7bSEldad Zack __setup("nfsaddrs=", nfsaddrs_config_setup);
16981da177e4SLinus Torvalds 
169962013dbbSRainer Jochem static int __init vendor_class_identifier_setup(char *addrs)
170062013dbbSRainer Jochem {
170162013dbbSRainer Jochem 	if (strlcpy(vendor_class_identifier, addrs,
170262013dbbSRainer Jochem 		    sizeof(vendor_class_identifier))
170362013dbbSRainer Jochem 	    >= sizeof(vendor_class_identifier))
170409605cc1SBastian Stender 		pr_warn("DHCP: vendorclass too long, truncated to \"%s\"\n",
170562013dbbSRainer Jochem 			vendor_class_identifier);
170662013dbbSRainer Jochem 	return 1;
170762013dbbSRainer Jochem }
170862013dbbSRainer Jochem __setup("dhcpclass=", vendor_class_identifier_setup);
1709