1847 lines · 43.3 KB · C
RawDownload
// SPDX-License-Identifier: GPL-2.0/* *  Automatic Configuration of IP -- use DHCP, BOOTP, RARP, or *  user-supplied information to configure own IP address and routes. * *  Copyright (C) 1996-1998 Martin Mares <mj@atrey.karlin.mff.cuni.cz> * *  Derived from network configuration code in fs/nfs/nfsroot.c, *  originally Copyright (C) 1995, 1996 Gero Kuhlmann and me. * *  BOOTP rewritten to construct and analyse packets itself instead *  of misusing the IP layer. num_bugs_causing_wrong_arp_replies--; *					     -- MJ, December 1998 * *  Fixed ip_auto_config_setup calling at startup in the new "Linker Magic" *  initialization scheme. *	- Arnaldo Carvalho de Melo <acme@conectiva.com.br>, 08/11/1999 * *  DHCP support added.  To users this looks like a whole separate *  protocol, but we know it's just a bag on the side of BOOTP. *		-- Chip Salzenberg <chip@valinux.com>, May 2000 * *  Ported DHCP support from 2.2.16 to 2.4.0-test4 *              -- Eric Biederman <ebiederman@lnxi.com>, 30 Aug 2000 * *  Merged changes from 2.2.19 into 2.4.3 *              -- Eric Biederman <ebiederman@lnxi.com>, 22 April Aug 2001 * *  Multiple Nameservers in /proc/net/pnp *              --  Josef Siemes <jsiemes@web.de>, Aug 2002 * *  NTP servers in /proc/net/ipconfig/ntp_servers *              --  Chris Novakovic <chris@chrisn.me.uk>, April 2018 */ #include <linux/types.h>#include <linux/string.h>#include <linux/kernel.h>#include <linux/jiffies.h>#include <linux/random.h>#include <linux/init.h>#include <linux/utsname.h>#include <linux/in.h>#include <linux/if.h>#include <linux/inet.h>#include <linux/inetdevice.h>#include <linux/netdevice.h>#include <linux/if_arp.h>#include <linux/skbuff.h>#include <linux/ip.h>#include <linux/socket.h>#include <linux/route.h>#include <linux/udp.h>#include <linux/proc_fs.h>#include <linux/seq_file.h>#include <linux/major.h>#include <linux/root_dev.h>#include <linux/delay.h>#include <linux/nfs_fs.h>#include <linux/slab.h>#include <linux/export.h>#include <net/net_namespace.h>#include <net/arp.h>#include <net/ip.h>#include <net/ipconfig.h>#include <net/route.h> #include <linux/uaccess.h>#include <net/checksum.h>#include <asm/processor.h> #if defined(CONFIG_IP_PNP_DHCP)#define IPCONFIG_DHCP#endif#if defined(CONFIG_IP_PNP_BOOTP) || defined(CONFIG_IP_PNP_DHCP)#define IPCONFIG_BOOTP#endif#if defined(CONFIG_IP_PNP_RARP)#define IPCONFIG_RARP#endif#if defined(IPCONFIG_BOOTP) || defined(IPCONFIG_RARP)#define IPCONFIG_DYNAMIC#endif /* Define the friendly delay before and after opening net devices */#define CONF_POST_OPEN		10	/* After opening: 10 msecs */ /* Define the timeout for waiting for a DHCP/BOOTP/RARP reply */#define CONF_OPEN_RETRIES 	2	/* (Re)open devices twice */#define CONF_SEND_RETRIES 	6	/* Send six requests per open */#define CONF_BASE_TIMEOUT	(HZ*2)	/* Initial timeout: 2 seconds */#define CONF_TIMEOUT_RANDOM	(HZ)	/* Maximum amount of randomization */#define CONF_TIMEOUT_MULT	*7/4	/* Rate of timeout growth */#define CONF_TIMEOUT_MAX	(HZ*30)	/* Maximum allowed timeout */#define CONF_NAMESERVERS_MAX   3       /* Maximum number of nameservers					   - '3' from resolv.h */#define CONF_NTP_SERVERS_MAX   3	/* Maximum number of NTP servers */ #define NONE cpu_to_be32(INADDR_NONE)#define ANY cpu_to_be32(INADDR_ANY) /* Wait for carrier timeout default in seconds */static unsigned int carrier_timeout = 120; /* * Public IP configuration */ /* This is used by platforms which might be able to set the ipconfig * variables using firmware environment vars.  If this is set, it will * ignore such firmware variables. */int ic_set_manually __initdata = 0;		/* IPconfig parameters set manually */ static int ic_enable __initdata;		/* IP config enabled? */ /* Protocol choice */int ic_proto_enabled __initdata = 0#ifdef IPCONFIG_BOOTP			| IC_BOOTP#endif#ifdef CONFIG_IP_PNP_DHCP			| IC_USE_DHCP#endif#ifdef IPCONFIG_RARP			| IC_RARP#endif			; static int ic_host_name_set __initdata;	/* Host name set by us? */ __be32 ic_myaddr = NONE;		/* My IP address */static __be32 ic_netmask = NONE;	/* Netmask for local subnet */__be32 ic_gateway = NONE;	/* Gateway IP address */ #ifdef IPCONFIG_DYNAMICstatic __be32 ic_addrservaddr = NONE;	/* IP Address of the IP addresses'server */#endif __be32 ic_servaddr = NONE;	/* Boot server IP address */ __be32 root_server_addr = NONE;	/* Address of NFS server */u8 root_server_path[256] = { 0, };	/* Path to mount as root */ /* vendor class identifier */static char vendor_class_identifier[253] __initdata; #if defined(CONFIG_IP_PNP_DHCP)static char dhcp_client_identifier[253] __initdata;#endif /* Persistent data: */ #ifdef IPCONFIG_DYNAMICstatic int ic_proto_used;			/* Protocol used, if any */#else#define ic_proto_used 0#endifstatic __be32 ic_nameservers[CONF_NAMESERVERS_MAX]; /* DNS Server IP addresses */static __be32 ic_ntp_servers[CONF_NTP_SERVERS_MAX]; /* NTP server IP addresses */static u8 ic_domain[64];		/* DNS (not NIS) domain name */ /* * Private state. */ /* Name of user-selected boot device */static char user_dev_name[IFNAMSIZ] __initdata = { 0, }; /* Protocols supported by available interfaces */static int ic_proto_have_if __initdata; /* MTU for boot device */static int ic_dev_mtu __initdata; #ifdef IPCONFIG_DYNAMICstatic DEFINE_SPINLOCK(ic_recv_lock);static volatile int ic_got_reply __initdata;    /* Proto(s) that replied */#endif#ifdef IPCONFIG_DHCPstatic int ic_dhcp_msgtype __initdata;	/* DHCP msg type received */#endif  /* *	Network devices */ struct ic_device {	struct ic_device *next;	struct net_device *dev;	unsigned short flags;	short able;	__be32 xid;}; static struct ic_device *ic_first_dev __initdata;	/* List of open device */static struct ic_device *ic_dev __initdata;		/* Selected device */ static bool __init ic_is_init_dev(struct net_device *dev){	if (dev->flags & IFF_LOOPBACK)		return false;	return user_dev_name[0] ? !strcmp(dev->name, user_dev_name) :	    (!(dev->flags & IFF_LOOPBACK) &&	     (dev->flags & (IFF_POINTOPOINT|IFF_BROADCAST)) &&	     strncmp(dev->name, "dummy", 5));} static int __init ic_open_devs(void){	struct ic_device *d, **last;	struct net_device *dev;	unsigned short oflags;	unsigned long start, next_msg; 	last = &ic_first_dev;	rtnl_lock(); 	/* bring loopback device up first */	for_each_netdev(&init_net, dev) {		if (!(dev->flags & IFF_LOOPBACK))			continue;		if (dev_change_flags(dev, dev->flags | IFF_UP, NULL) < 0)			pr_err("IP-Config: Failed to open %s\n", dev->name);	} 	for_each_netdev(&init_net, dev) {		if (ic_is_init_dev(dev)) {			int able = 0;			if (dev->mtu >= 364)				able |= IC_BOOTP;			else				pr_warn("DHCP/BOOTP: Ignoring device %s, MTU %d too small\n",					dev->name, dev->mtu);			if (!(dev->flags & IFF_NOARP))				able |= IC_RARP;			able &= ic_proto_enabled;			if (ic_proto_enabled && !able)				continue;			oflags = dev->flags;			if (dev_change_flags(dev, oflags | IFF_UP, NULL) < 0) {				pr_err("IP-Config: Failed to open %s\n",				       dev->name);				continue;			}			if (!(d = kmalloc_obj(struct ic_device))) {				rtnl_unlock();				return -ENOMEM;			}			d->dev = dev;			*last = d;			last = &d->next;			d->flags = oflags;			d->able = able;			if (able & IC_BOOTP)				get_random_bytes(&d->xid, sizeof(__be32));			else				d->xid = 0;			ic_proto_have_if |= able;			pr_debug("IP-Config: %s UP (able=%d, xid=%08x)\n",				 dev->name, able, d->xid);		}	}	/* Devices with a complex topology like SFP ethernet interfaces needs	 * the rtnl_lock at init. The carrier wait-loop must therefore run	 * without holding it.	 */	rtnl_unlock(); 	/* no point in waiting if we could not bring up at least one device */	if (!ic_first_dev)		goto have_carrier; 	/* wait for a carrier on at least one device */	start = jiffies;	next_msg = start + secs_to_jiffies(20);	while (time_before(jiffies, start +			   secs_to_jiffies(carrier_timeout))) {		int wait, elapsed; 		rtnl_lock();		for_each_netdev(&init_net, dev)			if (ic_is_init_dev(dev) && netif_carrier_ok(dev)) {				rtnl_unlock();				goto have_carrier;			}		rtnl_unlock(); 		msleep(1); 		if (time_before(jiffies, next_msg))			continue; 		elapsed = jiffies_to_msecs(jiffies - start);		wait = (carrier_timeout * 1000 - elapsed + 500) / 1000;		pr_info("Waiting up to %d more seconds for network.\n", wait);		next_msg = jiffies + secs_to_jiffies(20);	}have_carrier: 	*last = NULL; 	if (!ic_first_dev) {		if (user_dev_name[0])			pr_err("IP-Config: Device `%s' not found\n",			       user_dev_name);		else			pr_err("IP-Config: No network devices available\n");		return -ENODEV;	}	return 0;} /* Close all network interfaces except the one we've autoconfigured, and its * lowers, in case it's a stacked virtual interface. */static void __init ic_close_devs(void){	struct net_device *selected_dev = ic_dev ? ic_dev->dev : NULL;	struct ic_device *d, *next;	struct net_device *dev; 	rtnl_lock();	next = ic_first_dev;	while ((d = next)) {		bool bring_down = (d != ic_dev);		struct net_device *lower;		struct list_head *iter; 		next = d->next;		dev = d->dev; 		if (selected_dev) {			netdev_for_each_lower_dev(selected_dev, lower, iter) {				if (dev == lower) {					bring_down = false;					break;				}			}		}		if (bring_down) {			pr_debug("IP-Config: Downing %s\n", dev->name);			dev_change_flags(dev, d->flags, NULL);		}		kfree(d);	}	rtnl_unlock();} /* *	Interface to various network functions. */ static inline voidset_sockaddr(struct sockaddr_in *sin, __be32 addr, __be16 port){	sin->sin_family = AF_INET;	sin->sin_addr.s_addr = addr;	sin->sin_port = port;} /* *	Set up interface addresses and routes. */ static int __init ic_setup_if(void){	struct ifreq ir;	struct sockaddr_in *sin = (void *) &ir.ifr_ifru.ifru_addr;	int err; 	memset(&ir, 0, sizeof(ir));	strcpy(ir.ifr_ifrn.ifrn_name, ic_dev->dev->name);	set_sockaddr(sin, ic_myaddr, 0);	if ((err = devinet_ioctl(&init_net, SIOCSIFADDR, &ir)) < 0) {		pr_err("IP-Config: Unable to set interface address (%d)\n",		       err);		return -1;	}	set_sockaddr(sin, ic_netmask, 0);	if ((err = devinet_ioctl(&init_net, SIOCSIFNETMASK, &ir)) < 0) {		pr_err("IP-Config: Unable to set interface netmask (%d)\n",		       err);		return -1;	}	set_sockaddr(sin, ic_myaddr | ~ic_netmask, 0);	if ((err = devinet_ioctl(&init_net, SIOCSIFBRDADDR, &ir)) < 0) {		pr_err("IP-Config: Unable to set interface broadcast address (%d)\n",		       err);		return -1;	}	/* Handle the case where we need non-standard MTU on the boot link (a network	 * using jumbo frames, for instance).  If we can't set the mtu, don't error	 * out, we'll try to muddle along.	 */	if (ic_dev_mtu != 0) {		rtnl_lock();		if ((err = dev_set_mtu(ic_dev->dev, ic_dev_mtu)) < 0)			pr_err("IP-Config: Unable to set interface mtu to %d (%d)\n",			       ic_dev_mtu, err);		rtnl_unlock();	}	return 0;} static int __init ic_setup_routes(void){	/* No need to setup device routes, only the default route... */ 	if (ic_gateway != NONE) {		struct rtentry rm;		int err; 		memset(&rm, 0, sizeof(rm));		if ((ic_gateway ^ ic_myaddr) & ic_netmask) {			pr_err("IP-Config: Gateway not on directly connected network\n");			return -1;		}		set_sockaddr((struct sockaddr_in *) &rm.rt_dst, 0, 0);		set_sockaddr((struct sockaddr_in *) &rm.rt_genmask, 0, 0);		set_sockaddr((struct sockaddr_in *) &rm.rt_gateway, ic_gateway, 0);		rm.rt_flags = RTF_UP | RTF_GATEWAY;		if ((err = ip_rt_ioctl(&init_net, SIOCADDRT, &rm)) < 0) {			pr_err("IP-Config: Cannot add default route (%d)\n",			       err);			return -1;		}	} 	return 0;} /* *	Fill in default values for all missing parameters. */ static int __init ic_defaults(void){	/*	 *	At this point we have no userspace running so need not	 *	claim locks on system_utsname	 */ 	if (!ic_host_name_set)		sprintf(init_utsname()->nodename, "%pI4", &ic_myaddr); 	if (root_server_addr == NONE)		root_server_addr = ic_servaddr; 	if (ic_netmask == NONE) {		if (IN_CLASSA(ntohl(ic_myaddr)))			ic_netmask = htonl(IN_CLASSA_NET);		else if (IN_CLASSB(ntohl(ic_myaddr)))			ic_netmask = htonl(IN_CLASSB_NET);		else if (IN_CLASSC(ntohl(ic_myaddr)))			ic_netmask = htonl(IN_CLASSC_NET);		else if (IN_CLASSE(ntohl(ic_myaddr)))			ic_netmask = htonl(IN_CLASSE_NET);		else {			pr_err("IP-Config: Unable to guess netmask for address %pI4\n",			       &ic_myaddr);			return -1;		}		pr_notice("IP-Config: Guessing netmask %pI4\n",			  &ic_netmask);	} 	return 0;} /* *	RARP support. */ #ifdef IPCONFIG_RARP static int ic_rarp_recv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev); static struct packet_type rarp_packet_type __initdata = {	.type =	cpu_to_be16(ETH_P_RARP),	.func =	ic_rarp_recv,}; static inline void __init ic_rarp_init(void){	dev_add_pack(&rarp_packet_type);} static inline void __init ic_rarp_cleanup(void){	dev_remove_pack(&rarp_packet_type);} /* *  Process received RARP packet. */static int __initic_rarp_recv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev){

Lines 1–500 of 1847.