blob: 9abf833f3a9907f403352fc5d05a4399d6e66f0d [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * Automatic Configuration of IP -- use DHCP, BOOTP, RARP, or
4 * user-supplied information to configure own IP address and routes.
5 *
6 * Copyright (C) 1996-1998 Martin Mares <mj@atrey.karlin.mff.cuni.cz>
7 *
8 * Derived from network configuration code in fs/nfs/nfsroot.c,
9 * originally Copyright (C) 1995, 1996 Gero Kuhlmann and me.
10 *
11 * BOOTP rewritten to construct and analyse packets itself instead
12 * of misusing the IP layer. num_bugs_causing_wrong_arp_replies--;
13 * -- MJ, December 1998
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +090014 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 * Fixed ip_auto_config_setup calling at startup in the new "Linker Magic"
16 * initialization scheme.
17 * - Arnaldo Carvalho de Melo <acme@conectiva.com.br>, 08/11/1999
18 *
19 * DHCP support added. To users this looks like a whole separate
20 * protocol, but we know it's just a bag on the side of BOOTP.
21 * -- Chip Salzenberg <chip@valinux.com>, May 2000
22 *
23 * Ported DHCP support from 2.2.16 to 2.4.0-test4
24 * -- Eric Biederman <ebiederman@lnxi.com>, 30 Aug 2000
25 *
26 * Merged changes from 2.2.19 into 2.4.3
27 * -- Eric Biederman <ebiederman@lnxi.com>, 22 April Aug 2001
28 *
29 * Multiple Nameservers in /proc/net/pnp
30 * -- Josef Siemes <jsiemes@web.de>, Aug 2002
31 */
32
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/types.h>
34#include <linux/string.h>
35#include <linux/kernel.h>
36#include <linux/jiffies.h>
37#include <linux/random.h>
38#include <linux/init.h>
39#include <linux/utsname.h>
40#include <linux/in.h>
41#include <linux/if.h>
42#include <linux/inet.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020043#include <linux/inetdevice.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <linux/netdevice.h>
45#include <linux/if_arp.h>
46#include <linux/skbuff.h>
47#include <linux/ip.h>
48#include <linux/socket.h>
49#include <linux/route.h>
50#include <linux/udp.h>
51#include <linux/proc_fs.h>
52#include <linux/seq_file.h>
53#include <linux/major.h>
54#include <linux/root_dev.h>
55#include <linux/delay.h>
Adrian Bunk43d60662005-09-05 18:05:52 -070056#include <linux/nfs_fs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090057#include <linux/slab.h>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040058#include <linux/export.h>
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020059#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#include <net/arp.h>
Andrew Lunnc6e970a2017-03-28 23:45:06 +020061#include <net/dsa.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#include <net/ip.h>
63#include <net/ipconfig.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020064#include <net/route.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080066#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070067#include <net/checksum.h>
68#include <asm/processor.h>
69
Linus Torvalds1da177e2005-04-16 15:20:36 -070070#if defined(CONFIG_IP_PNP_DHCP)
71#define IPCONFIG_DHCP
72#endif
73#if defined(CONFIG_IP_PNP_BOOTP) || defined(CONFIG_IP_PNP_DHCP)
74#define IPCONFIG_BOOTP
75#endif
76#if defined(CONFIG_IP_PNP_RARP)
77#define IPCONFIG_RARP
78#endif
79#if defined(IPCONFIG_BOOTP) || defined(IPCONFIG_RARP)
80#define IPCONFIG_DYNAMIC
81#endif
82
83/* Define the friendly delay before and after opening net devices */
Micha Nelissen3fb72f12011-05-19 10:14:06 +000084#define CONF_POST_OPEN 10 /* After opening: 10 msecs */
85#define CONF_CARRIER_TIMEOUT 120000 /* Wait for carrier timeout */
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
87/* Define the timeout for waiting for a DHCP/BOOTP/RARP reply */
88#define CONF_OPEN_RETRIES 2 /* (Re)open devices twice */
89#define CONF_SEND_RETRIES 6 /* Send six requests per open */
Linus Torvalds1da177e2005-04-16 15:20:36 -070090#define CONF_BASE_TIMEOUT (HZ*2) /* Initial timeout: 2 seconds */
91#define CONF_TIMEOUT_RANDOM (HZ) /* Maximum amount of randomization */
92#define CONF_TIMEOUT_MULT *7/4 /* Rate of timeout growth */
93#define CONF_TIMEOUT_MAX (HZ*30) /* Maximum allowed timeout */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +090094#define CONF_NAMESERVERS_MAX 3 /* Maximum number of nameservers
95 - '3' from resolv.h */
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
Harvey Harrison09640e62009-02-01 00:45:17 -080097#define NONE cpu_to_be32(INADDR_NONE)
98#define ANY cpu_to_be32(INADDR_ANY)
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
100/*
101 * Public IP configuration
102 */
103
104/* This is used by platforms which might be able to set the ipconfig
105 * variables using firmware environment vars. If this is set, it will
106 * ignore such firmware variables.
107 */
108int ic_set_manually __initdata = 0; /* IPconfig parameters set manually */
109
Fabian Frederick0d3979b2014-11-04 20:37:46 +0100110static int ic_enable __initdata; /* IP config enabled? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
112/* Protocol choice */
113int ic_proto_enabled __initdata = 0
114#ifdef IPCONFIG_BOOTP
115 | IC_BOOTP
116#endif
117#ifdef CONFIG_IP_PNP_DHCP
118 | IC_USE_DHCP
119#endif
120#ifdef IPCONFIG_RARP
121 | IC_RARP
122#endif
123 ;
124
Fabian Frederick0d3979b2014-11-04 20:37:46 +0100125static int ic_host_name_set __initdata; /* Host name set by us? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
Al Viro5a874db2006-11-08 00:19:38 -0800127__be32 ic_myaddr = NONE; /* My IP address */
128static __be32 ic_netmask = NONE; /* Netmask for local subnet */
129__be32 ic_gateway = NONE; /* Gateway IP address */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
David S. Miller86ef7f9c2016-06-11 20:40:24 -0700131#ifdef IPCONFIG_DYNAMIC
Ben Dooks0b392be2016-06-10 12:11:06 +0100132static __be32 ic_addrservaddr = NONE; /* IP Address of the IP addresses'server */
David S. Miller86ef7f9c2016-06-11 20:40:24 -0700133#endif
Philippe De Muyter9dd4a132013-01-03 19:02:12 +0100134
Al Viro5a874db2006-11-08 00:19:38 -0800135__be32 ic_servaddr = NONE; /* Boot server IP address */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
Al Viro5a874db2006-11-08 00:19:38 -0800137__be32 root_server_addr = NONE; /* Address of NFS server */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138u8 root_server_path[256] = { 0, }; /* Path to mount as root */
139
David S. Miller1b0b04f2007-11-19 21:56:16 -0800140/* vendor class identifier */
141static char vendor_class_identifier[253] __initdata;
Rainer Jochem62013db2007-11-14 02:18:39 -0800142
Li RongQing26fb3422015-10-15 16:54:36 +0800143#if defined(CONFIG_IP_PNP_DHCP)
144static char dhcp_client_identifier[253] __initdata;
145#endif
146
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147/* Persistent data: */
148
Arnd Bergmann52b79e22016-01-28 17:39:24 +0100149#ifdef IPCONFIG_DYNAMIC
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150static int ic_proto_used; /* Protocol used, if any */
Arnd Bergmann52b79e22016-01-28 17:39:24 +0100151#else
152#define ic_proto_used 0
153#endif
Al Viro5a874db2006-11-08 00:19:38 -0800154static __be32 ic_nameservers[CONF_NAMESERVERS_MAX]; /* DNS Server IP addresses */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155static u8 ic_domain[64]; /* DNS (not NIS) domain name */
156
157/*
158 * Private state.
159 */
160
Chris Novakovic4d019b32018-04-24 03:56:38 +0100161/* proc_dir_entry for /proc/net/ipconfig */
162static struct proc_dir_entry *ipconfig_dir;
163
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164/* Name of user-selected boot device */
165static char user_dev_name[IFNAMSIZ] __initdata = { 0, };
166
167/* Protocols supported by available interfaces */
Fabian Frederick0d3979b2014-11-04 20:37:46 +0100168static int ic_proto_have_if __initdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
Chris Friesen9643f452009-05-19 15:31:50 -0700170/* MTU for boot device */
Fabian Frederick0d3979b2014-11-04 20:37:46 +0100171static int ic_dev_mtu __initdata;
Chris Friesen9643f452009-05-19 15:31:50 -0700172
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173#ifdef IPCONFIG_DYNAMIC
174static DEFINE_SPINLOCK(ic_recv_lock);
Fabian Frederick0d3979b2014-11-04 20:37:46 +0100175static volatile int ic_got_reply __initdata; /* Proto(s) that replied */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176#endif
177#ifdef IPCONFIG_DHCP
Fabian Frederick0d3979b2014-11-04 20:37:46 +0100178static int ic_dhcp_msgtype __initdata; /* DHCP msg type received */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179#endif
180
181
182/*
183 * Network devices
184 */
185
186struct ic_device {
187 struct ic_device *next;
188 struct net_device *dev;
189 unsigned short flags;
190 short able;
Al Viro5a874db2006-11-08 00:19:38 -0800191 __be32 xid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192};
193
Fabian Frederick0d3979b2014-11-04 20:37:46 +0100194static struct ic_device *ic_first_dev __initdata; /* List of open device */
Uwe Kleine-König2647cff2016-07-29 11:30:38 +0200195static struct ic_device *ic_dev __initdata; /* Selected device */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
Micha Nelissen3fb72f12011-05-19 10:14:06 +0000197static bool __init ic_is_init_dev(struct net_device *dev)
David S. Miller964ad812010-03-12 00:00:17 -0800198{
Micha Nelissen3fb72f12011-05-19 10:14:06 +0000199 if (dev->flags & IFF_LOOPBACK)
200 return false;
201 return user_dev_name[0] ? !strcmp(dev->name, user_dev_name) :
David S. Miller964ad812010-03-12 00:00:17 -0800202 (!(dev->flags & IFF_LOOPBACK) &&
203 (dev->flags & (IFF_POINTOPOINT|IFF_BROADCAST)) &&
Micha Nelissen3fb72f12011-05-19 10:14:06 +0000204 strncmp(dev->name, "dummy", 5));
David S. Miller964ad812010-03-12 00:00:17 -0800205}
206
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207static int __init ic_open_devs(void)
208{
209 struct ic_device *d, **last;
210 struct net_device *dev;
211 unsigned short oflags;
Paul Gortmaker5e404cd2013-04-02 09:58:25 -0400212 unsigned long start, next_msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
214 last = &ic_first_dev;
Stephen Hemminger6756ae42006-03-20 22:23:58 -0800215 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
Florian Fainelli728c0202015-01-16 09:56:01 -0800217 /* bring loopback and DSA master network devices up first */
Eric W. Biederman0cc217e2007-09-26 22:10:06 -0700218 for_each_netdev(&init_net, dev) {
Florian Fainelli728c0202015-01-16 09:56:01 -0800219 if (!(dev->flags & IFF_LOOPBACK) && !netdev_uses_dsa(dev))
Eric W. Biederman0cc217e2007-09-26 22:10:06 -0700220 continue;
221 if (dev_change_flags(dev, dev->flags | IFF_UP) < 0)
Joe Perches058bd4d2012-03-11 18:36:11 +0000222 pr_err("IP-Config: Failed to open %s\n", dev->name);
Eric W. Biederman0cc217e2007-09-26 22:10:06 -0700223 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
Eric W. Biederman881d9662007-09-17 11:56:21 -0700225 for_each_netdev(&init_net, dev) {
Micha Nelissen3fb72f12011-05-19 10:14:06 +0000226 if (ic_is_init_dev(dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 int able = 0;
228 if (dev->mtu >= 364)
229 able |= IC_BOOTP;
230 else
Bastian Stender09605cc2015-11-13 11:40:34 +0100231 pr_warn("DHCP/BOOTP: Ignoring device %s, MTU %d too small\n",
Joe Perches058bd4d2012-03-11 18:36:11 +0000232 dev->name, dev->mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 if (!(dev->flags & IFF_NOARP))
234 able |= IC_RARP;
235 able &= ic_proto_enabled;
236 if (ic_proto_enabled && !able)
237 continue;
238 oflags = dev->flags;
239 if (dev_change_flags(dev, oflags | IFF_UP) < 0) {
Joe Perches058bd4d2012-03-11 18:36:11 +0000240 pr_err("IP-Config: Failed to open %s\n",
241 dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 continue;
243 }
244 if (!(d = kmalloc(sizeof(struct ic_device), GFP_KERNEL))) {
Stephen Hemminger6756ae42006-03-20 22:23:58 -0800245 rtnl_unlock();
David S. Miller964ad812010-03-12 00:00:17 -0800246 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 }
248 d->dev = dev;
249 *last = d;
250 last = &d->next;
251 d->flags = oflags;
252 d->able = able;
253 if (able & IC_BOOTP)
Al Viro5a874db2006-11-08 00:19:38 -0800254 get_random_bytes(&d->xid, sizeof(__be32));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 else
256 d->xid = 0;
257 ic_proto_have_if |= able;
Bastian Stender09605cc2015-11-13 11:40:34 +0100258 pr_debug("IP-Config: %s UP (able=%d, xid=%08x)\n",
259 dev->name, able, d->xid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 }
261 }
Micha Nelissen3fb72f12011-05-19 10:14:06 +0000262
Gerlando Falautocd7816d2011-12-19 22:58:04 +0000263 /* no point in waiting if we could not bring up at least one device */
264 if (!ic_first_dev)
265 goto have_carrier;
266
Micha Nelissen3fb72f12011-05-19 10:14:06 +0000267 /* wait for a carrier on at least one device */
268 start = jiffies;
Paul Gortmaker5e404cd2013-04-02 09:58:25 -0400269 next_msg = start + msecs_to_jiffies(CONF_CARRIER_TIMEOUT/12);
Himangi Saraogic72c95a2014-08-20 23:14:10 +0530270 while (time_before(jiffies, start +
271 msecs_to_jiffies(CONF_CARRIER_TIMEOUT))) {
Paul Gortmaker5e404cd2013-04-02 09:58:25 -0400272 int wait, elapsed;
273
Micha Nelissen3fb72f12011-05-19 10:14:06 +0000274 for_each_netdev(&init_net, dev)
275 if (ic_is_init_dev(dev) && netif_carrier_ok(dev))
276 goto have_carrier;
277
278 msleep(1);
Paul Gortmaker5e404cd2013-04-02 09:58:25 -0400279
FX Le Bail357137a2014-02-10 16:46:54 +0100280 if (time_before(jiffies, next_msg))
Paul Gortmaker5e404cd2013-04-02 09:58:25 -0400281 continue;
282
283 elapsed = jiffies_to_msecs(jiffies - start);
284 wait = (CONF_CARRIER_TIMEOUT - elapsed + 500)/1000;
285 pr_info("Waiting up to %d more seconds for network.\n", wait);
286 next_msg = jiffies + msecs_to_jiffies(CONF_CARRIER_TIMEOUT/12);
Micha Nelissen3fb72f12011-05-19 10:14:06 +0000287 }
288have_carrier:
Stephen Hemminger6756ae42006-03-20 22:23:58 -0800289 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
291 *last = NULL;
292
293 if (!ic_first_dev) {
294 if (user_dev_name[0])
Joe Perches058bd4d2012-03-11 18:36:11 +0000295 pr_err("IP-Config: Device `%s' not found\n",
296 user_dev_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 else
Joe Perches058bd4d2012-03-11 18:36:11 +0000298 pr_err("IP-Config: No network devices available\n");
David S. Miller964ad812010-03-12 00:00:17 -0800299 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 }
301 return 0;
302}
303
304static void __init ic_close_devs(void)
305{
306 struct ic_device *d, *next;
307 struct net_device *dev;
308
Stephen Hemminger6756ae42006-03-20 22:23:58 -0800309 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 next = ic_first_dev;
311 while ((d = next)) {
312 next = d->next;
313 dev = d->dev;
Mark Rutlandffefb6f2017-03-27 18:00:14 +0100314 if (d != ic_dev && !netdev_uses_dsa(dev)) {
Bastian Stender09605cc2015-11-13 11:40:34 +0100315 pr_debug("IP-Config: Downing %s\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 dev_change_flags(dev, d->flags);
317 }
318 kfree(d);
319 }
Stephen Hemminger6756ae42006-03-20 22:23:58 -0800320 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321}
322
323/*
324 * Interface to various network functions.
325 */
326
327static inline void
Al Viro5a874db2006-11-08 00:19:38 -0800328set_sockaddr(struct sockaddr_in *sin, __be32 addr, __be16 port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329{
330 sin->sin_family = AF_INET;
331 sin->sin_addr.s_addr = addr;
332 sin->sin_port = port;
333}
334
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335/*
336 * Set up interface addresses and routes.
337 */
338
339static int __init ic_setup_if(void)
340{
341 struct ifreq ir;
342 struct sockaddr_in *sin = (void *) &ir.ifr_ifru.ifru_addr;
343 int err;
344
345 memset(&ir, 0, sizeof(ir));
Uwe Kleine-König2647cff2016-07-29 11:30:38 +0200346 strcpy(ir.ifr_ifrn.ifrn_name, ic_dev->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 set_sockaddr(sin, ic_myaddr, 0);
Al Viro03aef172017-07-01 07:53:12 -0400348 if ((err = devinet_ioctl(&init_net, SIOCSIFADDR, &ir)) < 0) {
Joe Perches058bd4d2012-03-11 18:36:11 +0000349 pr_err("IP-Config: Unable to set interface address (%d)\n",
350 err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 return -1;
352 }
353 set_sockaddr(sin, ic_netmask, 0);
Al Viro03aef172017-07-01 07:53:12 -0400354 if ((err = devinet_ioctl(&init_net, SIOCSIFNETMASK, &ir)) < 0) {
Joe Perches058bd4d2012-03-11 18:36:11 +0000355 pr_err("IP-Config: Unable to set interface netmask (%d)\n",
356 err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 return -1;
358 }
359 set_sockaddr(sin, ic_myaddr | ~ic_netmask, 0);
Al Viro03aef172017-07-01 07:53:12 -0400360 if ((err = devinet_ioctl(&init_net, SIOCSIFBRDADDR, &ir)) < 0) {
Joe Perches058bd4d2012-03-11 18:36:11 +0000361 pr_err("IP-Config: Unable to set interface broadcast address (%d)\n",
362 err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 return -1;
364 }
Chris Friesen9643f452009-05-19 15:31:50 -0700365 /* Handle the case where we need non-standard MTU on the boot link (a network
366 * using jumbo frames, for instance). If we can't set the mtu, don't error
367 * out, we'll try to muddle along.
368 */
369 if (ic_dev_mtu != 0) {
Al Viro6a88fbe2017-10-01 20:27:01 -0400370 rtnl_lock();
371 if ((err = dev_set_mtu(ic_dev->dev, ic_dev_mtu)) < 0)
Joe Perches058bd4d2012-03-11 18:36:11 +0000372 pr_err("IP-Config: Unable to set interface mtu to %d (%d)\n",
373 ic_dev_mtu, err);
Al Viro6a88fbe2017-10-01 20:27:01 -0400374 rtnl_unlock();
Chris Friesen9643f452009-05-19 15:31:50 -0700375 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 return 0;
377}
378
379static int __init ic_setup_routes(void)
380{
381 /* No need to setup device routes, only the default route... */
382
Al Viro5a874db2006-11-08 00:19:38 -0800383 if (ic_gateway != NONE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 struct rtentry rm;
385 int err;
386
387 memset(&rm, 0, sizeof(rm));
388 if ((ic_gateway ^ ic_myaddr) & ic_netmask) {
Joe Perches058bd4d2012-03-11 18:36:11 +0000389 pr_err("IP-Config: Gateway not on directly connected network\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 return -1;
391 }
392 set_sockaddr((struct sockaddr_in *) &rm.rt_dst, 0, 0);
393 set_sockaddr((struct sockaddr_in *) &rm.rt_genmask, 0, 0);
394 set_sockaddr((struct sockaddr_in *) &rm.rt_gateway, ic_gateway, 0);
395 rm.rt_flags = RTF_UP | RTF_GATEWAY;
Al Viroca25c302017-07-01 08:03:10 -0400396 if ((err = ip_rt_ioctl(&init_net, SIOCADDRT, &rm)) < 0) {
Joe Perches058bd4d2012-03-11 18:36:11 +0000397 pr_err("IP-Config: Cannot add default route (%d)\n",
398 err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 return -1;
400 }
401 }
402
403 return 0;
404}
405
406/*
407 * Fill in default values for all missing parameters.
408 */
409
410static int __init ic_defaults(void)
411{
412 /*
413 * At this point we have no userspace running so need not
414 * claim locks on system_utsname
415 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900416
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 if (!ic_host_name_set)
Harvey Harrison673d57e2008-10-31 00:53:57 -0700418 sprintf(init_utsname()->nodename, "%pI4", &ic_myaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
Al Viro5a874db2006-11-08 00:19:38 -0800420 if (root_server_addr == NONE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 root_server_addr = ic_servaddr;
422
Al Viro5a874db2006-11-08 00:19:38 -0800423 if (ic_netmask == NONE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 if (IN_CLASSA(ntohl(ic_myaddr)))
425 ic_netmask = htonl(IN_CLASSA_NET);
426 else if (IN_CLASSB(ntohl(ic_myaddr)))
427 ic_netmask = htonl(IN_CLASSB_NET);
428 else if (IN_CLASSC(ntohl(ic_myaddr)))
429 ic_netmask = htonl(IN_CLASSC_NET);
430 else {
Joe Perches058bd4d2012-03-11 18:36:11 +0000431 pr_err("IP-Config: Unable to guess netmask for address %pI4\n",
432 &ic_myaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 return -1;
434 }
Bastian Stender09605cc2015-11-13 11:40:34 +0100435 pr_notice("IP-Config: Guessing netmask %pI4\n",
436 &ic_netmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 }
438
439 return 0;
440}
441
442/*
443 * RARP support.
444 */
445
446#ifdef IPCONFIG_RARP
447
David S. Millerf2ccd8f2005-08-09 19:34:12 -0700448static int ic_rarp_recv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
450static struct packet_type rarp_packet_type __initdata = {
Harvey Harrison09640e62009-02-01 00:45:17 -0800451 .type = cpu_to_be16(ETH_P_RARP),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 .func = ic_rarp_recv,
453};
454
Sam Ravnborg45e741b2008-04-29 20:58:15 -0700455static inline void __init ic_rarp_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456{
457 dev_add_pack(&rarp_packet_type);
458}
459
Sam Ravnborg45e741b2008-04-29 20:58:15 -0700460static inline void __init ic_rarp_cleanup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461{
462 dev_remove_pack(&rarp_packet_type);
463}
464
465/*
466 * Process received RARP packet.
467 */
468static int __init
David S. Millerf2ccd8f2005-08-09 19:34:12 -0700469ic_rarp_recv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470{
471 struct arphdr *rarp;
472 unsigned char *rarp_ptr;
Al Viro5a874db2006-11-08 00:19:38 -0800473 __be32 sip, tip;
Fabian Frederick6b436d32014-10-27 19:03:22 +0100474 unsigned char *tha; /* t for "target" */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 struct ic_device *d;
476
YOSHIFUJI Hideaki721499e2008-07-19 22:34:43 -0700477 if (!net_eq(dev_net(dev), &init_net))
Eric W. Biedermane730c152007-09-17 11:53:39 -0700478 goto drop;
479
Ian Morris51456b22015-04-03 09:17:26 +0100480 skb = skb_share_check(skb, GFP_ATOMIC);
481 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 return NET_RX_DROP;
483
484 if (!pskb_may_pull(skb, sizeof(struct arphdr)))
485 goto drop;
486
487 /* Basic sanity checks can be done without the lock. */
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -0700488 rarp = (struct arphdr *)skb_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
490 /* If this test doesn't pass, it's not IP, or we should
491 * ignore it anyway.
492 */
493 if (rarp->ar_hln != dev->addr_len || dev->type != ntohs(rarp->ar_hrd))
494 goto drop;
495
496 /* If it's not a RARP reply, delete it. */
497 if (rarp->ar_op != htons(ARPOP_RREPLY))
498 goto drop;
499
500 /* If it's not Ethernet, delete it. */
501 if (rarp->ar_pro != htons(ETH_P_IP))
502 goto drop;
503
Pavel Emelyanov988b7052008-03-03 12:20:57 -0800504 if (!pskb_may_pull(skb, arp_hdr_len(dev)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 goto drop;
506
507 /* OK, it is all there and looks valid, process... */
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -0700508 rarp = (struct arphdr *)skb_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 rarp_ptr = (unsigned char *) (rarp + 1);
510
511 /* One reply at a time, please. */
512 spin_lock(&ic_recv_lock);
513
514 /* If we already have a reply, just drop the packet */
515 if (ic_got_reply)
516 goto drop_unlock;
517
518 /* Find the ic_device that the packet arrived on */
519 d = ic_first_dev;
520 while (d && d->dev != dev)
521 d = d->next;
522 if (!d)
523 goto drop_unlock; /* should never happen */
524
525 /* Extract variable-width fields */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 rarp_ptr += dev->addr_len;
527 memcpy(&sip, rarp_ptr, 4);
528 rarp_ptr += 4;
529 tha = rarp_ptr;
530 rarp_ptr += dev->addr_len;
531 memcpy(&tip, rarp_ptr, 4);
532
533 /* Discard packets which are not meant for us. */
534 if (memcmp(tha, dev->dev_addr, dev->addr_len))
535 goto drop_unlock;
536
537 /* Discard packets which are not from specified server. */
Al Viro5a874db2006-11-08 00:19:38 -0800538 if (ic_servaddr != NONE && ic_servaddr != sip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 goto drop_unlock;
540
541 /* We have a winner! */
Uwe Kleine-König2647cff2016-07-29 11:30:38 +0200542 ic_dev = d;
Al Viro5a874db2006-11-08 00:19:38 -0800543 if (ic_myaddr == NONE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 ic_myaddr = tip;
545 ic_servaddr = sip;
Philippe De Muyter9dd4a132013-01-03 19:02:12 +0100546 ic_addrservaddr = sip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 ic_got_reply = IC_RARP;
548
549drop_unlock:
550 /* Show's over. Nothing to see here. */
551 spin_unlock(&ic_recv_lock);
552
553drop:
554 /* Throw the packet out. */
555 kfree_skb(skb);
556 return 0;
557}
558
559
560/*
561 * Send RARP request packet over a single interface.
562 */
563static void __init ic_rarp_send_if(struct ic_device *d)
564{
565 struct net_device *dev = d->dev;
566 arp_send(ARPOP_RREQUEST, ETH_P_RARP, 0, dev, 0, NULL,
567 dev->dev_addr, dev->dev_addr);
568}
569#endif
570
571/*
Andy Shevchenko842b08b2012-09-24 22:09:58 +0000572 * Predefine Nameservers
573 */
574static inline void __init ic_nameservers_predef(void)
575{
576 int i;
577
578 for (i = 0; i < CONF_NAMESERVERS_MAX; i++)
579 ic_nameservers[i] = NONE;
580}
581
582/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 * DHCP/BOOTP support.
584 */
585
586#ifdef IPCONFIG_BOOTP
587
588struct bootp_pkt { /* BOOTP packet format */
589 struct iphdr iph; /* IP header */
590 struct udphdr udph; /* UDP header */
591 u8 op; /* 1=request, 2=reply */
592 u8 htype; /* HW address type */
593 u8 hlen; /* HW address length */
594 u8 hops; /* Used only by gateways */
Al Viro5a874db2006-11-08 00:19:38 -0800595 __be32 xid; /* Transaction ID */
596 __be16 secs; /* Seconds since we started */
597 __be16 flags; /* Just what it says */
598 __be32 client_ip; /* Client's IP address if known */
599 __be32 your_ip; /* Assigned IP address */
600 __be32 server_ip; /* (Next, e.g. NFS) Server's IP address */
601 __be32 relay_ip; /* IP address of BOOTP relay */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 u8 hw_addr[16]; /* Client's HW address */
603 u8 serv_name[64]; /* Server host name */
604 u8 boot_file[128]; /* Name of boot file */
605 u8 exten[312]; /* DHCP options / BOOTP vendor extensions */
606};
607
608/* packet ops */
609#define BOOTP_REQUEST 1
610#define BOOTP_REPLY 2
611
612/* DHCP message types */
613#define DHCPDISCOVER 1
614#define DHCPOFFER 2
615#define DHCPREQUEST 3
616#define DHCPDECLINE 4
617#define DHCPACK 5
618#define DHCPNAK 6
619#define DHCPRELEASE 7
620#define DHCPINFORM 8
621
David S. Millerf2ccd8f2005-08-09 19:34:12 -0700622static int ic_bootp_recv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
624static struct packet_type bootp_packet_type __initdata = {
Harvey Harrison09640e62009-02-01 00:45:17 -0800625 .type = cpu_to_be16(ETH_P_IP),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 .func = ic_bootp_recv,
627};
628
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629/*
630 * Initialize DHCP/BOOTP extension fields in the request.
631 */
632
633static const u8 ic_bootp_cookie[4] = { 99, 130, 83, 99 };
634
635#ifdef IPCONFIG_DHCP
636
637static void __init
Uwe Kleine-König22fc5382016-07-29 11:30:37 +0200638ic_dhcp_init_options(u8 *options, struct ic_device *d)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639{
Al Viro5a874db2006-11-08 00:19:38 -0800640 u8 mt = ((ic_servaddr == NONE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 ? DHCPDISCOVER : DHCPREQUEST);
642 u8 *e = options;
Rainer Jochem62013db2007-11-14 02:18:39 -0800643 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
Uwe Kleine-König22fc5382016-07-29 11:30:37 +0200645 pr_debug("DHCP: Sending message type %d (%s)\n", mt, d->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646
647 memcpy(e, ic_bootp_cookie, 4); /* RFC1048 Magic Cookie */
648 e += 4;
649
650 *e++ = 53; /* DHCP message type */
651 *e++ = 1;
652 *e++ = mt;
653
654 if (mt == DHCPREQUEST) {
655 *e++ = 54; /* Server ID (IP address) */
656 *e++ = 4;
657 memcpy(e, &ic_servaddr, 4);
658 e += 4;
659
660 *e++ = 50; /* Requested IP address */
661 *e++ = 4;
662 memcpy(e, &ic_myaddr, 4);
663 e += 4;
664 }
665
666 /* always? */
667 {
668 static const u8 ic_req_params[] = {
669 1, /* Subnet mask */
670 3, /* Default gateway */
671 6, /* DNS server */
672 12, /* Host name */
673 15, /* Domain name */
674 17, /* Boot path */
Chris Friesen9643f452009-05-19 15:31:50 -0700675 26, /* MTU */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 40, /* NIS domain name */
677 };
678
679 *e++ = 55; /* Parameter request list */
680 *e++ = sizeof(ic_req_params);
681 memcpy(e, ic_req_params, sizeof(ic_req_params));
682 e += sizeof(ic_req_params);
Rainer Jochem62013db2007-11-14 02:18:39 -0800683
Wu Fengguang130c0f42010-05-30 17:19:53 +0000684 if (ic_host_name_set) {
685 *e++ = 12; /* host-name */
686 len = strlen(utsname()->nodename);
687 *e++ = len;
688 memcpy(e, utsname()->nodename, len);
689 e += len;
690 }
Rainer Jochem62013db2007-11-14 02:18:39 -0800691 if (*vendor_class_identifier) {
Joe Perches058bd4d2012-03-11 18:36:11 +0000692 pr_info("DHCP: sending class identifier \"%s\"\n",
693 vendor_class_identifier);
Rainer Jochem62013db2007-11-14 02:18:39 -0800694 *e++ = 60; /* Class-identifier */
695 len = strlen(vendor_class_identifier);
696 *e++ = len;
697 memcpy(e, vendor_class_identifier, len);
698 e += len;
699 }
Li RongQing26fb3422015-10-15 16:54:36 +0800700 len = strlen(dhcp_client_identifier + 1);
701 /* the minimum length of identifier is 2, include 1 byte type,
702 * and can not be larger than the length of options
703 */
704 if (len >= 1 && len < 312 - (e - options) - 1) {
705 *e++ = 61;
706 *e++ = len + 1;
707 memcpy(e, dhcp_client_identifier, len + 1);
708 e += len + 1;
709 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 }
711
712 *e++ = 255; /* End of the list */
713}
714
715#endif /* IPCONFIG_DHCP */
716
717static void __init ic_bootp_init_ext(u8 *e)
718{
719 memcpy(e, ic_bootp_cookie, 4); /* RFC1048 Magic Cookie */
720 e += 4;
721 *e++ = 1; /* Subnet mask request */
722 *e++ = 4;
723 e += 4;
724 *e++ = 3; /* Default gateway request */
725 *e++ = 4;
726 e += 4;
Chris Novakovicde1fa152018-04-24 03:56:35 +0100727#if CONF_NAMESERVERS_MAX > 0
Chris Novakovic4e1a8af2018-04-24 03:56:34 +0100728 *e++ = 6; /* (DNS) name server request */
Chris Novakovicde1fa152018-04-24 03:56:35 +0100729 *e++ = 4 * CONF_NAMESERVERS_MAX;
730 e += 4 * CONF_NAMESERVERS_MAX;
731#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 *e++ = 12; /* Host name request */
733 *e++ = 32;
734 e += 32;
735 *e++ = 40; /* NIS Domain name request */
736 *e++ = 32;
737 e += 32;
738 *e++ = 17; /* Boot path */
739 *e++ = 40;
740 e += 40;
741
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900742 *e++ = 57; /* set extension buffer size for reply */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 *e++ = 2;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900744 *e++ = 1; /* 128+236+8+20+14, see dhcpd sources */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 *e++ = 150;
746
747 *e++ = 255; /* End of the list */
748}
749
750
751/*
Christoph Fritz5e953772012-09-21 08:31:19 +0000752 * Initialize the DHCP/BOOTP mechanism.
753 */
754static inline void __init ic_bootp_init(void)
755{
Chris Novakovic300eec72018-04-24 03:56:37 +0100756 /* Re-initialise all name servers to NONE, in case any were set via the
757 * "ip=" or "nfsaddrs=" kernel command line parameters: any IP addresses
758 * specified there will already have been decoded but are no longer
759 * needed
760 */
Christoph Fritz5e953772012-09-21 08:31:19 +0000761 ic_nameservers_predef();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762
763 dev_add_pack(&bootp_packet_type);
764}
765
766
767/*
768 * DHCP/BOOTP cleanup.
769 */
Sam Ravnborg45e741b2008-04-29 20:58:15 -0700770static inline void __init ic_bootp_cleanup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771{
772 dev_remove_pack(&bootp_packet_type);
773}
774
775
776/*
777 * Send DHCP/BOOTP request to single interface.
778 */
779static void __init ic_bootp_send_if(struct ic_device *d, unsigned long jiffies_diff)
780{
781 struct net_device *dev = d->dev;
782 struct sk_buff *skb;
783 struct bootp_pkt *b;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 struct iphdr *h;
Herbert Xu66088242011-11-18 02:20:04 +0000785 int hlen = LL_RESERVED_SPACE(dev);
786 int tlen = dev->needed_tailroom;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787
788 /* Allocate packet */
Herbert Xu66088242011-11-18 02:20:04 +0000789 skb = alloc_skb(sizeof(struct bootp_pkt) + hlen + tlen + 15,
Johannes Bergf5184d22008-05-12 20:48:31 -0700790 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 if (!skb)
792 return;
Herbert Xu66088242011-11-18 02:20:04 +0000793 skb_reserve(skb, hlen);
Johannes Bergb080db52017-06-16 14:29:19 +0200794 b = skb_put_zero(skb, sizeof(struct bootp_pkt));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795
796 /* Construct IP header */
Arnaldo Carvalho de Melo04b964d2007-03-10 19:27:27 -0300797 skb_reset_network_header(skb);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700798 h = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 h->version = 4;
800 h->ihl = 5;
801 h->tot_len = htons(sizeof(struct bootp_pkt));
802 h->frag_off = htons(IP_DF);
803 h->ttl = 64;
804 h->protocol = IPPROTO_UDP;
Al Viro5a874db2006-11-08 00:19:38 -0800805 h->daddr = htonl(INADDR_BROADCAST);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 h->check = ip_fast_csum((unsigned char *) h, h->ihl);
807
808 /* Construct UDP header */
809 b->udph.source = htons(68);
810 b->udph.dest = htons(67);
811 b->udph.len = htons(sizeof(struct bootp_pkt) - sizeof(struct iphdr));
812 /* UDP checksum not calculated -- explicitly allowed in BOOTP RFC */
813
814 /* Construct DHCP/BOOTP header */
815 b->op = BOOTP_REQUEST;
816 if (dev->type < 256) /* check for false types */
817 b->htype = dev->type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 else if (dev->type == ARPHRD_FDDI)
819 b->htype = ARPHRD_ETHER;
820 else {
Bastian Stender09605cc2015-11-13 11:40:34 +0100821 pr_warn("Unknown ARP type 0x%04x for device %s\n", dev->type,
822 dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 b->htype = dev->type; /* can cause undefined behavior */
824 }
Stephen Hemmingerdea75bd2008-03-04 17:03:49 -0800825
826 /* server_ip and your_ip address are both already zero per RFC2131 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 b->hlen = dev->addr_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 memcpy(b->hw_addr, dev->dev_addr, dev->addr_len);
829 b->secs = htons(jiffies_diff / HZ);
830 b->xid = d->xid;
831
832 /* add DHCP options or BOOTP extensions */
833#ifdef IPCONFIG_DHCP
834 if (ic_proto_enabled & IC_USE_DHCP)
Uwe Kleine-König22fc5382016-07-29 11:30:37 +0200835 ic_dhcp_init_options(b->exten, d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 else
837#endif
838 ic_bootp_init_ext(b->exten);
839
840 /* Chain packet down the line... */
841 skb->dev = dev;
842 skb->protocol = htons(ETH_P_IP);
Stephen Hemminger0c4e8582007-10-09 01:36:32 -0700843 if (dev_hard_header(skb, dev, ntohs(skb->protocol),
RongQing.Liad79eef2011-11-14 14:37:24 -0500844 dev->broadcast, dev->dev_addr, skb->len) < 0) {
845 kfree_skb(skb);
846 printk("E");
847 return;
848 }
849
850 if (dev_queue_xmit(skb) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 printk("E");
852}
853
854
855/*
856 * Copy BOOTP-supplied string if not already set.
857 */
858static int __init ic_bootp_string(char *dest, char *src, int len, int max)
859{
860 if (!len)
861 return 0;
862 if (len > max-1)
863 len = max-1;
864 memcpy(dest, src, len);
865 dest[len] = '\0';
866 return 1;
867}
868
869
870/*
871 * Process BOOTP extensions.
872 */
873static void __init ic_do_bootp_ext(u8 *ext)
874{
Eric Dumazet747465e2012-01-16 19:27:39 +0000875 u8 servers;
876 int i;
877 __be16 mtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 u8 *c;
880
Bastian Stender09605cc2015-11-13 11:40:34 +0100881 pr_debug("DHCP/BOOTP: Got extension %d:", *ext);
Stephen Hemminger132adf52007-03-08 20:44:43 -0800882 for (c=ext+2; c<ext+2+ext[1]; c++)
Bastian Stender09605cc2015-11-13 11:40:34 +0100883 pr_debug(" %02x", *c);
884 pr_debug("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885
886 switch (*ext++) {
Joe Perches1d67a512011-07-01 09:43:05 +0000887 case 1: /* Subnet mask */
888 if (ic_netmask == NONE)
889 memcpy(&ic_netmask, ext+1, 4);
890 break;
891 case 3: /* Default gateway */
892 if (ic_gateway == NONE)
893 memcpy(&ic_gateway, ext+1, 4);
894 break;
895 case 6: /* DNS server */
896 servers= *ext/4;
897 if (servers > CONF_NAMESERVERS_MAX)
898 servers = CONF_NAMESERVERS_MAX;
899 for (i = 0; i < servers; i++) {
900 if (ic_nameservers[i] == NONE)
901 memcpy(&ic_nameservers[i], ext+1+4*i, 4);
902 }
903 break;
904 case 12: /* Host name */
905 ic_bootp_string(utsname()->nodename, ext+1, *ext,
906 __NEW_UTS_LEN);
907 ic_host_name_set = 1;
908 break;
909 case 15: /* Domain name (DNS) */
910 ic_bootp_string(ic_domain, ext+1, *ext, sizeof(ic_domain));
911 break;
912 case 17: /* Root path */
913 if (!root_server_path[0])
914 ic_bootp_string(root_server_path, ext+1, *ext,
915 sizeof(root_server_path));
916 break;
917 case 26: /* Interface MTU */
918 memcpy(&mtu, ext+1, sizeof(mtu));
919 ic_dev_mtu = ntohs(mtu);
920 break;
921 case 40: /* NIS Domain name (_not_ DNS) */
922 ic_bootp_string(utsname()->domainname, ext+1, *ext,
923 __NEW_UTS_LEN);
924 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 }
926}
927
928
929/*
930 * Receive BOOTP reply.
931 */
David S. Millerf2ccd8f2005-08-09 19:34:12 -0700932static int __init ic_bootp_recv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933{
934 struct bootp_pkt *b;
935 struct iphdr *h;
936 struct ic_device *d;
937 int len, ext_len;
938
YOSHIFUJI Hideaki721499e2008-07-19 22:34:43 -0700939 if (!net_eq(dev_net(dev), &init_net))
Eric W. Biedermane730c152007-09-17 11:53:39 -0700940 goto drop;
941
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 /* Perform verifications before taking the lock. */
943 if (skb->pkt_type == PACKET_OTHERHOST)
944 goto drop;
945
Ian Morris51456b22015-04-03 09:17:26 +0100946 skb = skb_share_check(skb, GFP_ATOMIC);
947 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 return NET_RX_DROP;
949
950 if (!pskb_may_pull(skb,
951 sizeof(struct iphdr) +
952 sizeof(struct udphdr)))
953 goto drop;
954
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700955 b = (struct bootp_pkt *)skb_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 h = &b->iph;
957
958 if (h->ihl != 5 || h->version != 4 || h->protocol != IPPROTO_UDP)
959 goto drop;
960
961 /* Fragments are not supported */
Paul Gortmaker56f8a752011-06-21 20:33:34 -0700962 if (ip_is_fragment(h)) {
Joe Perchese87cc472012-05-13 21:56:26 +0000963 net_err_ratelimited("DHCP/BOOTP: Ignoring fragmented reply\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 goto drop;
965 }
966
967 if (skb->len < ntohs(h->tot_len))
968 goto drop;
969
970 if (ip_fast_csum((char *) h, h->ihl))
971 goto drop;
972
973 if (b->udph.source != htons(67) || b->udph.dest != htons(68))
974 goto drop;
975
976 if (ntohs(h->tot_len) < ntohs(b->udph.len) + sizeof(struct iphdr))
977 goto drop;
978
979 len = ntohs(b->udph.len) - sizeof(struct udphdr);
980 ext_len = len - (sizeof(*b) -
981 sizeof(struct iphdr) -
982 sizeof(struct udphdr) -
983 sizeof(b->exten));
984 if (ext_len < 0)
985 goto drop;
986
987 /* Ok the front looks good, make sure we can get at the rest. */
988 if (!pskb_may_pull(skb, skb->len))
989 goto drop;
990
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700991 b = (struct bootp_pkt *)skb_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 h = &b->iph;
993
994 /* One reply at a time, please. */
995 spin_lock(&ic_recv_lock);
996
997 /* If we already have a reply, just drop the packet */
998 if (ic_got_reply)
999 goto drop_unlock;
1000
1001 /* Find the ic_device that the packet arrived on */
1002 d = ic_first_dev;
1003 while (d && d->dev != dev)
1004 d = d->next;
1005 if (!d)
1006 goto drop_unlock; /* should never happen */
1007
1008 /* Is it a reply to our BOOTP request? */
1009 if (b->op != BOOTP_REPLY ||
1010 b->xid != d->xid) {
Uwe Kleine-König22fc5382016-07-29 11:30:37 +02001011 net_err_ratelimited("DHCP/BOOTP: Reply not for us on %s, op[%x] xid[%x]\n",
1012 d->dev->name, b->op, b->xid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 goto drop_unlock;
1014 }
1015
1016 /* Parse extensions */
1017 if (ext_len >= 4 &&
1018 !memcmp(b->exten, ic_bootp_cookie, 4)) { /* Check magic cookie */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001019 u8 *end = (u8 *) b + ntohs(b->iph.tot_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 u8 *ext;
1021
1022#ifdef IPCONFIG_DHCP
1023 if (ic_proto_enabled & IC_USE_DHCP) {
Al Viro5a874db2006-11-08 00:19:38 -08001024 __be32 server_id = NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 int mt = 0;
1026
1027 ext = &b->exten[4];
1028 while (ext < end && *ext != 0xff) {
1029 u8 *opt = ext++;
1030 if (*opt == 0) /* Padding */
1031 continue;
1032 ext += *ext + 1;
1033 if (ext >= end)
1034 break;
1035 switch (*opt) {
1036 case 53: /* Message type */
1037 if (opt[1])
1038 mt = opt[2];
1039 break;
1040 case 54: /* Server ID (IP address) */
1041 if (opt[1] >= 4)
1042 memcpy(&server_id, opt + 2, 4);
1043 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001044 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 }
1046
Uwe Kleine-König22fc5382016-07-29 11:30:37 +02001047 pr_debug("DHCP: Got message type %d (%s)\n", mt, d->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048
1049 switch (mt) {
1050 case DHCPOFFER:
1051 /* While in the process of accepting one offer,
1052 * ignore all others.
1053 */
Al Viro5a874db2006-11-08 00:19:38 -08001054 if (ic_myaddr != NONE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 goto drop_unlock;
1056
1057 /* Let's accept that offer. */
1058 ic_myaddr = b->your_ip;
1059 ic_servaddr = server_id;
Bastian Stender09605cc2015-11-13 11:40:34 +01001060 pr_debug("DHCP: Offered address %pI4 by server %pI4\n",
1061 &ic_myaddr, &b->iph.saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 /* The DHCP indicated server address takes
1063 * precedence over the bootp header one if
1064 * they are different.
1065 */
Al Viro5a874db2006-11-08 00:19:38 -08001066 if ((server_id != NONE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 (b->server_ip != server_id))
1068 b->server_ip = ic_servaddr;
1069 break;
1070
1071 case DHCPACK:
1072 if (memcmp(dev->dev_addr, b->hw_addr, dev->addr_len) != 0)
1073 goto drop_unlock;
1074
1075 /* Yeah! */
1076 break;
1077
1078 default:
1079 /* Urque. Forget it*/
Al Viro5a874db2006-11-08 00:19:38 -08001080 ic_myaddr = NONE;
1081 ic_servaddr = NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 goto drop_unlock;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001083 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084
1085 ic_dhcp_msgtype = mt;
1086
1087 }
1088#endif /* IPCONFIG_DHCP */
1089
1090 ext = &b->exten[4];
1091 while (ext < end && *ext != 0xff) {
1092 u8 *opt = ext++;
1093 if (*opt == 0) /* Padding */
1094 continue;
1095 ext += *ext + 1;
1096 if (ext < end)
1097 ic_do_bootp_ext(opt);
1098 }
1099 }
1100
1101 /* We have a winner! */
Uwe Kleine-König2647cff2016-07-29 11:30:38 +02001102 ic_dev = d;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 ic_myaddr = b->your_ip;
1104 ic_servaddr = b->server_ip;
Philippe De Muyter9dd4a132013-01-03 19:02:12 +01001105 ic_addrservaddr = b->iph.saddr;
Al Viro5a874db2006-11-08 00:19:38 -08001106 if (ic_gateway == NONE && b->relay_ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 ic_gateway = b->relay_ip;
Al Viro5a874db2006-11-08 00:19:38 -08001108 if (ic_nameservers[0] == NONE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 ic_nameservers[0] = ic_servaddr;
1110 ic_got_reply = IC_BOOTP;
1111
1112drop_unlock:
1113 /* Show's over. Nothing to see here. */
1114 spin_unlock(&ic_recv_lock);
1115
1116drop:
1117 /* Throw the packet out. */
1118 kfree_skb(skb);
1119
1120 return 0;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001121}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122
1123
1124#endif
1125
1126
1127/*
1128 * Dynamic IP configuration -- DHCP, BOOTP, RARP.
1129 */
1130
1131#ifdef IPCONFIG_DYNAMIC
1132
1133static int __init ic_dynamic(void)
1134{
1135 int retries;
1136 struct ic_device *d;
1137 unsigned long start_jiffies, timeout, jiff;
1138 int do_bootp = ic_proto_have_if & IC_BOOTP;
1139 int do_rarp = ic_proto_have_if & IC_RARP;
1140
1141 /*
1142 * If none of DHCP/BOOTP/RARP was selected, return with an error.
1143 * This routine gets only called when some pieces of information
1144 * are missing, and without DHCP/BOOTP/RARP we are unable to get it.
1145 */
1146 if (!ic_proto_enabled) {
Joe Perches058bd4d2012-03-11 18:36:11 +00001147 pr_err("IP-Config: Incomplete network configuration information\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 return -1;
1149 }
1150
1151#ifdef IPCONFIG_BOOTP
1152 if ((ic_proto_enabled ^ ic_proto_have_if) & IC_BOOTP)
Joe Perches058bd4d2012-03-11 18:36:11 +00001153 pr_err("DHCP/BOOTP: No suitable device found\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154#endif
1155#ifdef IPCONFIG_RARP
1156 if ((ic_proto_enabled ^ ic_proto_have_if) & IC_RARP)
Joe Perches058bd4d2012-03-11 18:36:11 +00001157 pr_err("RARP: No suitable device found\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158#endif
1159
1160 if (!ic_proto_have_if)
1161 /* Error message already printed */
1162 return -1;
1163
1164 /*
1165 * Setup protocols
1166 */
1167#ifdef IPCONFIG_BOOTP
1168 if (do_bootp)
1169 ic_bootp_init();
1170#endif
1171#ifdef IPCONFIG_RARP
1172 if (do_rarp)
1173 ic_rarp_init();
1174#endif
1175
1176 /*
1177 * Send requests and wait, until we get an answer. This loop
1178 * seems to be a terrible waste of CPU time, but actually there is
1179 * only one process running at all, so we don't need to use any
1180 * scheduler functions.
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001181 * [Actually we could now, but the nothing else running note still
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 * applies.. - AC]
1183 */
Joe Perches058bd4d2012-03-11 18:36:11 +00001184 pr_notice("Sending %s%s%s requests .",
1185 do_bootp
1186 ? ((ic_proto_enabled & IC_USE_DHCP) ? "DHCP" : "BOOTP") : "",
1187 (do_bootp && do_rarp) ? " and " : "",
1188 do_rarp ? "RARP" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189
1190 start_jiffies = jiffies;
1191 d = ic_first_dev;
1192 retries = CONF_SEND_RETRIES;
1193 get_random_bytes(&timeout, sizeof(timeout));
Eric Dumazet95c96172012-04-15 05:58:06 +00001194 timeout = CONF_BASE_TIMEOUT + (timeout % (unsigned int) CONF_TIMEOUT_RANDOM);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001195 for (;;) {
David S. Miller405fd702014-07-09 22:25:18 -07001196#ifdef IPCONFIG_BOOTP
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 if (do_bootp && (d->able & IC_BOOTP))
1198 ic_bootp_send_if(d, jiffies - start_jiffies);
1199#endif
1200#ifdef IPCONFIG_RARP
1201 if (do_rarp && (d->able & IC_RARP))
1202 ic_rarp_send_if(d);
1203#endif
1204
Uwe Kleine-Könige0688532016-07-29 11:30:39 +02001205 if (!d->next) {
1206 jiff = jiffies + timeout;
1207 while (time_before(jiffies, jiff) && !ic_got_reply)
1208 schedule_timeout_uninterruptible(1);
1209 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210#ifdef IPCONFIG_DHCP
1211 /* DHCP isn't done until we get a DHCPACK. */
Joe Perches9d4fb272009-11-23 10:41:23 -08001212 if ((ic_got_reply & IC_BOOTP) &&
1213 (ic_proto_enabled & IC_USE_DHCP) &&
1214 ic_dhcp_msgtype != DHCPACK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 ic_got_reply = 0;
Uwe Kleine-König2647cff2016-07-29 11:30:38 +02001216 /* continue on device that got the reply */
1217 d = ic_dev;
Geert Uytterhoeven6c1c36b2015-11-24 14:08:23 +01001218 pr_cont(",");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 continue;
1220 }
1221#endif /* IPCONFIG_DHCP */
1222
1223 if (ic_got_reply) {
Geert Uytterhoeven6c1c36b2015-11-24 14:08:23 +01001224 pr_cont(" OK\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 break;
1226 }
1227
1228 if ((d = d->next))
1229 continue;
1230
1231 if (! --retries) {
Geert Uytterhoeven6c1c36b2015-11-24 14:08:23 +01001232 pr_cont(" timed out!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 break;
1234 }
1235
1236 d = ic_first_dev;
1237
1238 timeout = timeout CONF_TIMEOUT_MULT;
1239 if (timeout > CONF_TIMEOUT_MAX)
1240 timeout = CONF_TIMEOUT_MAX;
1241
Geert Uytterhoeven6c1c36b2015-11-24 14:08:23 +01001242 pr_cont(".");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 }
1244
1245#ifdef IPCONFIG_BOOTP
1246 if (do_bootp)
1247 ic_bootp_cleanup();
1248#endif
1249#ifdef IPCONFIG_RARP
1250 if (do_rarp)
1251 ic_rarp_cleanup();
1252#endif
1253
Maxime Bizon7a1af5d2005-06-28 13:21:12 -07001254 if (!ic_got_reply) {
Al Viro5a874db2006-11-08 00:19:38 -08001255 ic_myaddr = NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 return -1;
Maxime Bizon7a1af5d2005-06-28 13:21:12 -07001257 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258
Geert Uytterhoeven6c1c36b2015-11-24 14:08:23 +01001259 pr_info("IP-Config: Got %s answer from %pI4, my address is %pI4\n",
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001260 ((ic_got_reply & IC_RARP) ? "RARP"
Bastian Stender09605cc2015-11-13 11:40:34 +01001261 : (ic_proto_enabled & IC_USE_DHCP) ? "DHCP" : "BOOTP"),
Geert Uytterhoeven6c1c36b2015-11-24 14:08:23 +01001262 &ic_addrservaddr, &ic_myaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263
1264 return 0;
1265}
1266
1267#endif /* IPCONFIG_DYNAMIC */
1268
1269#ifdef CONFIG_PROC_FS
1270
1271static int pnp_seq_show(struct seq_file *seq, void *v)
1272{
1273 int i;
1274
1275 if (ic_proto_used & IC_PROTO)
1276 seq_printf(seq, "#PROTO: %s\n",
1277 (ic_proto_used & IC_RARP) ? "RARP"
1278 : (ic_proto_used & IC_USE_DHCP) ? "DHCP" : "BOOTP");
1279 else
1280 seq_puts(seq, "#MANUAL\n");
1281
1282 if (ic_domain[0])
1283 seq_printf(seq,
1284 "domain %s\n", ic_domain);
1285 for (i = 0; i < CONF_NAMESERVERS_MAX; i++) {
Al Viro5a874db2006-11-08 00:19:38 -08001286 if (ic_nameservers[i] != NONE)
Harvey Harrison673d57e2008-10-31 00:53:57 -07001287 seq_printf(seq, "nameserver %pI4\n",
1288 &ic_nameservers[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 }
Al Viro5a874db2006-11-08 00:19:38 -08001290 if (ic_servaddr != NONE)
Harvey Harrison673d57e2008-10-31 00:53:57 -07001291 seq_printf(seq, "bootserver %pI4\n",
1292 &ic_servaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 return 0;
1294}
1295
1296static int pnp_seq_open(struct inode *indoe, struct file *file)
1297{
1298 return single_open(file, pnp_seq_show, NULL);
1299}
1300
Arjan van de Ven9a321442007-02-12 00:55:35 -08001301static const struct file_operations pnp_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 .open = pnp_seq_open,
1303 .read = seq_read,
1304 .llseek = seq_lseek,
1305 .release = single_release,
1306};
Chris Novakovic4d019b32018-04-24 03:56:38 +01001307
1308/* Create the /proc/net/ipconfig directory */
1309static int ipconfig_proc_net_init(void)
1310{
1311 ipconfig_dir = proc_net_mkdir(&init_net, "ipconfig", init_net.proc_net);
1312 if (!ipconfig_dir)
1313 return -ENOMEM;
1314
1315 return 0;
1316}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317#endif /* CONFIG_PROC_FS */
1318
1319/*
1320 * Extract IP address from the parameter string if needed. Note that we
1321 * need to have root_server_addr set _before_ IPConfig gets called as it
1322 * can override it.
1323 */
Al Viro5a874db2006-11-08 00:19:38 -08001324__be32 __init root_nfs_parse_addr(char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325{
Al Viro5a874db2006-11-08 00:19:38 -08001326 __be32 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 int octets = 0;
1328 char *cp, *cq;
1329
1330 cp = cq = name;
1331 while (octets < 4) {
1332 while (*cp >= '0' && *cp <= '9')
1333 cp++;
1334 if (cp == cq || cp - cq > 3)
1335 break;
1336 if (*cp == '.' || octets == 3)
1337 octets++;
1338 if (octets < 4)
1339 cp++;
1340 cq = cp;
1341 }
1342 if (octets == 4 && (*cp == ':' || *cp == '\0')) {
1343 if (*cp == ':')
1344 *cp++ = '\0';
1345 addr = in_aton(name);
1346 memmove(name, cp, strlen(cp) + 1);
1347 } else
Al Viro5a874db2006-11-08 00:19:38 -08001348 addr = NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349
1350 return addr;
1351}
1352
David S. Miller964ad812010-03-12 00:00:17 -08001353#define DEVICE_WAIT_MAX 12 /* 12 seconds */
1354
1355static int __init wait_for_devices(void)
1356{
1357 int i;
1358
David S. Miller964ad812010-03-12 00:00:17 -08001359 for (i = 0; i < DEVICE_WAIT_MAX; i++) {
1360 struct net_device *dev;
1361 int found = 0;
1362
1363 rtnl_lock();
1364 for_each_netdev(&init_net, dev) {
Micha Nelissen3fb72f12011-05-19 10:14:06 +00001365 if (ic_is_init_dev(dev)) {
David S. Miller964ad812010-03-12 00:00:17 -08001366 found = 1;
1367 break;
1368 }
1369 }
1370 rtnl_unlock();
1371 if (found)
1372 return 0;
1373 ssleep(1);
1374 }
1375 return -ENODEV;
1376}
1377
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378/*
1379 * IP Autoconfig dispatcher.
1380 */
1381
1382static int __init ip_auto_config(void)
1383{
Al Viro5a874db2006-11-08 00:19:38 -08001384 __be32 addr;
Benjamin Zores9d8dba62009-01-29 16:19:13 -08001385#ifdef IPCONFIG_DYNAMIC
1386 int retries = CONF_OPEN_RETRIES;
1387#endif
David S. Miller964ad812010-03-12 00:00:17 -08001388 int err;
Christoph Fritz5e953772012-09-21 08:31:19 +00001389 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390
Chris Novakovic300eec72018-04-24 03:56:37 +01001391 /* Initialise all name servers to NONE (but only if the "ip=" or
1392 * "nfsaddrs=" kernel command line parameters weren't decoded, otherwise
1393 * we'll overwrite the IP addresses specified there)
1394 */
1395 if (ic_set_manually == 0)
1396 ic_nameservers_predef();
1397
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398#ifdef CONFIG_PROC_FS
Joe Perchesd6444062018-03-23 15:54:38 -07001399 proc_create("pnp", 0444, init_net.proc_net, &pnp_seq_fops);
Chris Novakovic4d019b32018-04-24 03:56:38 +01001400
1401 ipconfig_proc_net_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402#endif /* CONFIG_PROC_FS */
1403
1404 if (!ic_enable)
1405 return 0;
1406
Bastian Stender09605cc2015-11-13 11:40:34 +01001407 pr_debug("IP-Config: Entered.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408#ifdef IPCONFIG_DYNAMIC
1409 try_try_again:
1410#endif
David S. Miller964ad812010-03-12 00:00:17 -08001411 /* Wait for devices to appear */
1412 err = wait_for_devices();
1413 if (err)
1414 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415
1416 /* Setup all network devices */
David S. Miller964ad812010-03-12 00:00:17 -08001417 err = ic_open_devs();
1418 if (err)
1419 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420
1421 /* Give drivers a chance to settle */
Micha Nelissen3fb72f12011-05-19 10:14:06 +00001422 msleep(CONF_POST_OPEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423
1424 /*
1425 * If the config information is insufficient (e.g., our IP address or
1426 * IP address of the boot server is missing or we have multiple network
1427 * interfaces and no default was set), use BOOTP or RARP to get the
1428 * missing values.
1429 */
Al Viro5a874db2006-11-08 00:19:38 -08001430 if (ic_myaddr == NONE ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431#ifdef CONFIG_ROOT_NFS
Joe Perches9d4fb272009-11-23 10:41:23 -08001432 (root_server_addr == NONE &&
1433 ic_servaddr == NONE &&
1434 ROOT_DEV == Root_NFS) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435#endif
1436 ic_first_dev->next) {
1437#ifdef IPCONFIG_DYNAMIC
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 if (ic_dynamic() < 0) {
1439 ic_close_devs();
1440
1441 /*
1442 * I don't know why, but sometimes the
1443 * eepro100 driver (at least) gets upset and
1444 * doesn't work the first time it's opened.
1445 * But then if you close it and reopen it, it
1446 * works just fine. So we need to try that at
1447 * least once before giving up.
1448 *
1449 * Also, if the root will be NFS-mounted, we
1450 * have nowhere to go if DHCP fails. So we
1451 * just have to keep trying forever.
1452 *
1453 * -- Chip
1454 */
1455#ifdef CONFIG_ROOT_NFS
1456 if (ROOT_DEV == Root_NFS) {
Joe Perches058bd4d2012-03-11 18:36:11 +00001457 pr_err("IP-Config: Retrying forever (NFS root)...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 goto try_try_again;
1459 }
1460#endif
1461
1462 if (--retries) {
Joe Perches058bd4d2012-03-11 18:36:11 +00001463 pr_err("IP-Config: Reopening network devices...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 goto try_try_again;
1465 }
1466
1467 /* Oh, well. At least we tried. */
Joe Perches058bd4d2012-03-11 18:36:11 +00001468 pr_err("IP-Config: Auto-configuration of network failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 return -1;
1470 }
1471#else /* !DYNAMIC */
Joe Perches058bd4d2012-03-11 18:36:11 +00001472 pr_err("IP-Config: Incomplete network configuration information\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 ic_close_devs();
1474 return -1;
1475#endif /* IPCONFIG_DYNAMIC */
1476 } else {
1477 /* Device selected manually or only one device -> use it */
Uwe Kleine-König2647cff2016-07-29 11:30:38 +02001478 ic_dev = ic_first_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 }
1480
1481 addr = root_nfs_parse_addr(root_server_path);
Al Viro5a874db2006-11-08 00:19:38 -08001482 if (root_server_addr == NONE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 root_server_addr = addr;
1484
1485 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001486 * Use defaults wherever applicable.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 */
1488 if (ic_defaults() < 0)
1489 return -1;
1490
1491 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 * Record which protocol was actually used.
1493 */
1494#ifdef IPCONFIG_DYNAMIC
1495 ic_proto_used = ic_got_reply | (ic_proto_enabled & IC_USE_DHCP);
1496#endif
1497
1498#ifndef IPCONFIG_SILENT
1499 /*
1500 * Clue in the operator.
1501 */
Joe Perches058bd4d2012-03-11 18:36:11 +00001502 pr_info("IP-Config: Complete:\n");
Claudio Fontana9ecd1c32012-10-25 01:15:43 +00001503
1504 pr_info(" device=%s, hwaddr=%*phC, ipaddr=%pI4, mask=%pI4, gw=%pI4\n",
Uwe Kleine-König2647cff2016-07-29 11:30:38 +02001505 ic_dev->dev->name, ic_dev->dev->addr_len, ic_dev->dev->dev_addr,
Claudio Fontana9ecd1c32012-10-25 01:15:43 +00001506 &ic_myaddr, &ic_netmask, &ic_gateway);
Joe Perches058bd4d2012-03-11 18:36:11 +00001507 pr_info(" host=%s, domain=%s, nis-domain=%s\n",
1508 utsname()->nodename, ic_domain, utsname()->domainname);
1509 pr_info(" bootserver=%pI4, rootserver=%pI4, rootpath=%s",
1510 &ic_servaddr, &root_server_addr, root_server_path);
Chris Friesen9643f452009-05-19 15:31:50 -07001511 if (ic_dev_mtu)
Joe Perches058bd4d2012-03-11 18:36:11 +00001512 pr_cont(", mtu=%d", ic_dev_mtu);
Chris Novakovice18bdc82018-04-24 03:56:33 +01001513 /* Name servers (if any): */
1514 for (i = 0; i < CONF_NAMESERVERS_MAX; i++) {
Christoph Fritz5e953772012-09-21 08:31:19 +00001515 if (ic_nameservers[i] != NONE) {
Chris Novakovice18bdc82018-04-24 03:56:33 +01001516 if (i == 0)
1517 pr_info(" nameserver%u=%pI4",
1518 i, &ic_nameservers[i]);
1519 else
1520 pr_cont(", nameserver%u=%pI4",
1521 i, &ic_nameservers[i]);
Christoph Fritz5e953772012-09-21 08:31:19 +00001522 }
Chris Novakovice18bdc82018-04-24 03:56:33 +01001523 if (i + 1 == CONF_NAMESERVERS_MAX)
1524 pr_cont("\n");
1525 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526#endif /* !SILENT */
1527
Uwe Kleine-König9c706a42016-08-10 11:44:17 +02001528 /*
1529 * Close all network devices except the device we've
1530 * autoconfigured and set up routes.
1531 */
Uwe Kleine-König9c706a42016-08-10 11:44:17 +02001532 if (ic_setup_if() < 0 || ic_setup_routes() < 0)
Thierry Redingd2d371a2016-08-16 16:45:38 +02001533 err = -1;
1534 else
1535 err = 0;
Uwe Kleine-König9c706a42016-08-10 11:44:17 +02001536
Thierry Redingd2d371a2016-08-16 16:45:38 +02001537 ic_close_devs();
Uwe Kleine-König9c706a42016-08-10 11:44:17 +02001538
Thierry Redingd2d371a2016-08-16 16:45:38 +02001539 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540}
1541
1542late_initcall(ip_auto_config);
1543
1544
1545/*
1546 * Decode any IP configuration options in the "ip=" or "nfsaddrs=" kernel
J. Bruce Fieldsdc7a0812009-10-27 14:41:35 -04001547 * command line parameter. See Documentation/filesystems/nfs/nfsroot.txt.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 */
1549static int __init ic_proto_name(char *name)
1550{
1551 if (!strcmp(name, "on") || !strcmp(name, "any")) {
1552 return 1;
1553 }
Simon Hormana6c05c32007-12-25 20:54:42 -08001554 if (!strcmp(name, "off") || !strcmp(name, "none")) {
Amos Waterland92ffb852008-01-05 23:23:06 -08001555 return 0;
Simon Hormana6c05c32007-12-25 20:54:42 -08001556 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557#ifdef CONFIG_IP_PNP_DHCP
Li RongQing26fb3422015-10-15 16:54:36 +08001558 else if (!strncmp(name, "dhcp", 4)) {
1559 char *client_id;
1560
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 ic_proto_enabled &= ~IC_RARP;
Li RongQing26fb3422015-10-15 16:54:36 +08001562 client_id = strstr(name, "dhcp,");
1563 if (client_id) {
1564 char *v;
1565
1566 client_id = client_id + 5;
1567 v = strchr(client_id, ',');
1568 if (!v)
1569 return 1;
1570 *v = 0;
1571 if (kstrtou8(client_id, 0, dhcp_client_identifier))
Bastian Stender09605cc2015-11-13 11:40:34 +01001572 pr_debug("DHCP: Invalid client identifier type\n");
Li RongQing26fb3422015-10-15 16:54:36 +08001573 strncpy(dhcp_client_identifier + 1, v + 1, 251);
1574 *v = ',';
1575 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 return 1;
1577 }
1578#endif
1579#ifdef CONFIG_IP_PNP_BOOTP
1580 else if (!strcmp(name, "bootp")) {
1581 ic_proto_enabled &= ~(IC_RARP | IC_USE_DHCP);
1582 return 1;
1583 }
1584#endif
1585#ifdef CONFIG_IP_PNP_RARP
1586 else if (!strcmp(name, "rarp")) {
1587 ic_proto_enabled &= ~(IC_BOOTP | IC_USE_DHCP);
1588 return 1;
1589 }
1590#endif
1591#ifdef IPCONFIG_DYNAMIC
1592 else if (!strcmp(name, "both")) {
1593 ic_proto_enabled &= ~IC_USE_DHCP; /* backward compat :-( */
1594 return 1;
1595 }
1596#endif
1597 return 0;
1598}
1599
1600static int __init ip_auto_config_setup(char *addrs)
1601{
1602 char *cp, *ip, *dp;
1603 int num = 0;
1604
1605 ic_set_manually = 1;
Simon Horman9cecd072007-12-27 21:19:10 -08001606 ic_enable = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607
Amos Waterland92ffb852008-01-05 23:23:06 -08001608 /*
1609 * If any dhcp, bootp etc options are set, leave autoconfig on
1610 * and skip the below static IP processing.
1611 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 if (ic_proto_name(addrs))
1613 return 1;
1614
Amos Waterland92ffb852008-01-05 23:23:06 -08001615 /* If no static IP is given, turn off autoconfig and bail. */
1616 if (*addrs == 0 ||
1617 strcmp(addrs, "off") == 0 ||
1618 strcmp(addrs, "none") == 0) {
1619 ic_enable = 0;
1620 return 1;
1621 }
1622
Chris Novakovic300eec72018-04-24 03:56:37 +01001623 /* Initialise all name servers to NONE */
Christoph Fritz5e953772012-09-21 08:31:19 +00001624 ic_nameservers_predef();
1625
Amos Waterland92ffb852008-01-05 23:23:06 -08001626 /* Parse string for static IP assignment. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 ip = addrs;
1628 while (ip && *ip) {
1629 if ((cp = strchr(ip, ':')))
1630 *cp++ = '\0';
1631 if (strlen(ip) > 0) {
Bastian Stender09605cc2015-11-13 11:40:34 +01001632 pr_debug("IP-Config: Parameter #%d: `%s'\n", num, ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 switch (num) {
1634 case 0:
Al Viroe6f1ceb2008-03-17 22:44:53 -07001635 if ((ic_myaddr = in_aton(ip)) == ANY)
Al Viro5a874db2006-11-08 00:19:38 -08001636 ic_myaddr = NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 break;
1638 case 1:
Al Viroe6f1ceb2008-03-17 22:44:53 -07001639 if ((ic_servaddr = in_aton(ip)) == ANY)
Al Viro5a874db2006-11-08 00:19:38 -08001640 ic_servaddr = NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 break;
1642 case 2:
Al Viroe6f1ceb2008-03-17 22:44:53 -07001643 if ((ic_gateway = in_aton(ip)) == ANY)
Al Viro5a874db2006-11-08 00:19:38 -08001644 ic_gateway = NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 break;
1646 case 3:
Al Viroe6f1ceb2008-03-17 22:44:53 -07001647 if ((ic_netmask = in_aton(ip)) == ANY)
Al Viro5a874db2006-11-08 00:19:38 -08001648 ic_netmask = NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 break;
1650 case 4:
1651 if ((dp = strchr(ip, '.'))) {
1652 *dp++ = '\0';
Serge E. Hallyne9ff3992006-10-02 02:18:11 -07001653 strlcpy(utsname()->domainname, dp,
1654 sizeof(utsname()->domainname));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 }
Serge E. Hallyne9ff3992006-10-02 02:18:11 -07001656 strlcpy(utsname()->nodename, ip,
1657 sizeof(utsname()->nodename));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 ic_host_name_set = 1;
1659 break;
1660 case 5:
1661 strlcpy(user_dev_name, ip, sizeof(user_dev_name));
1662 break;
1663 case 6:
Amos Waterland92ffb852008-01-05 23:23:06 -08001664 if (ic_proto_name(ip) == 0 &&
1665 ic_myaddr == NONE) {
1666 ic_enable = 0;
1667 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 break;
Christoph Fritz5e953772012-09-21 08:31:19 +00001669 case 7:
1670 if (CONF_NAMESERVERS_MAX >= 1) {
1671 ic_nameservers[0] = in_aton(ip);
1672 if (ic_nameservers[0] == ANY)
1673 ic_nameservers[0] = NONE;
1674 }
1675 break;
1676 case 8:
1677 if (CONF_NAMESERVERS_MAX >= 2) {
1678 ic_nameservers[1] = in_aton(ip);
1679 if (ic_nameservers[1] == ANY)
1680 ic_nameservers[1] = NONE;
1681 }
1682 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 }
1684 }
1685 ip = cp;
1686 num++;
1687 }
1688
1689 return 1;
1690}
Eldad Zackb37f4d72012-05-19 14:04:39 +00001691__setup("ip=", ip_auto_config_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692
1693static int __init nfsaddrs_config_setup(char *addrs)
1694{
1695 return ip_auto_config_setup(addrs);
1696}
Eldad Zackb37f4d72012-05-19 14:04:39 +00001697__setup("nfsaddrs=", nfsaddrs_config_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698
Rainer Jochem62013db2007-11-14 02:18:39 -08001699static int __init vendor_class_identifier_setup(char *addrs)
1700{
1701 if (strlcpy(vendor_class_identifier, addrs,
1702 sizeof(vendor_class_identifier))
1703 >= sizeof(vendor_class_identifier))
Bastian Stender09605cc2015-11-13 11:40:34 +01001704 pr_warn("DHCP: vendorclass too long, truncated to \"%s\"\n",
Joe Perches058bd4d2012-03-11 18:36:11 +00001705 vendor_class_identifier);
Rainer Jochem62013db2007-11-14 02:18:39 -08001706 return 1;
1707}
Rainer Jochem62013db2007-11-14 02:18:39 -08001708__setup("dhcpclass=", vendor_class_identifier_setup);