1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0 21da177e4SLinus Torvalds /* 31da177e4SLinus Torvalds * Automatic Configuration of IP -- use DHCP, BOOTP, RARP, or 41da177e4SLinus Torvalds * user-supplied information to configure own IP address and routes. 51da177e4SLinus Torvalds * 61da177e4SLinus Torvalds * Copyright (C) 1996-1998 Martin Mares <mj@atrey.karlin.mff.cuni.cz> 71da177e4SLinus Torvalds * 81da177e4SLinus Torvalds * Derived from network configuration code in fs/nfs/nfsroot.c, 91da177e4SLinus Torvalds * originally Copyright (C) 1995, 1996 Gero Kuhlmann and me. 101da177e4SLinus Torvalds * 111da177e4SLinus Torvalds * BOOTP rewritten to construct and analyse packets itself instead 121da177e4SLinus Torvalds * of misusing the IP layer. num_bugs_causing_wrong_arp_replies--; 131da177e4SLinus Torvalds * -- MJ, December 1998 141da177e4SLinus Torvalds * 151da177e4SLinus Torvalds * Fixed ip_auto_config_setup calling at startup in the new "Linker Magic" 161da177e4SLinus Torvalds * initialization scheme. 171da177e4SLinus Torvalds * - Arnaldo Carvalho de Melo <acme@conectiva.com.br>, 08/11/1999 181da177e4SLinus Torvalds * 191da177e4SLinus Torvalds * DHCP support added. To users this looks like a whole separate 201da177e4SLinus Torvalds * protocol, but we know it's just a bag on the side of BOOTP. 211da177e4SLinus Torvalds * -- Chip Salzenberg <chip@valinux.com>, May 2000 221da177e4SLinus Torvalds * 231da177e4SLinus Torvalds * Ported DHCP support from 2.2.16 to 2.4.0-test4 241da177e4SLinus Torvalds * -- Eric Biederman <ebiederman@lnxi.com>, 30 Aug 2000 251da177e4SLinus Torvalds * 261da177e4SLinus Torvalds * Merged changes from 2.2.19 into 2.4.3 271da177e4SLinus Torvalds * -- Eric Biederman <ebiederman@lnxi.com>, 22 April Aug 2001 281da177e4SLinus Torvalds * 291da177e4SLinus Torvalds * Multiple Nameservers in /proc/net/pnp 301da177e4SLinus Torvalds * -- Josef Siemes <jsiemes@web.de>, Aug 2002 31c04d2cb2SChris Novakovic * 32c04d2cb2SChris Novakovic * NTP servers in /proc/net/ipconfig/ntp_servers 33c04d2cb2SChris Novakovic * -- Chris Novakovic <chris@chrisn.me.uk>, April 2018 341da177e4SLinus Torvalds */ 351da177e4SLinus Torvalds 361da177e4SLinus Torvalds #include <linux/types.h> 371da177e4SLinus Torvalds #include <linux/string.h> 381da177e4SLinus Torvalds #include <linux/kernel.h> 391da177e4SLinus Torvalds #include <linux/jiffies.h> 401da177e4SLinus Torvalds #include <linux/random.h> 411da177e4SLinus Torvalds #include <linux/init.h> 421da177e4SLinus Torvalds #include <linux/utsname.h> 431da177e4SLinus Torvalds #include <linux/in.h> 441da177e4SLinus Torvalds #include <linux/if.h> 451da177e4SLinus Torvalds #include <linux/inet.h> 4614c85021SArnaldo Carvalho de Melo #include <linux/inetdevice.h> 471da177e4SLinus Torvalds #include <linux/netdevice.h> 481da177e4SLinus Torvalds #include <linux/if_arp.h> 491da177e4SLinus Torvalds #include <linux/skbuff.h> 501da177e4SLinus Torvalds #include <linux/ip.h> 511da177e4SLinus Torvalds #include <linux/socket.h> 521da177e4SLinus Torvalds #include <linux/route.h> 531da177e4SLinus Torvalds #include <linux/udp.h> 541da177e4SLinus Torvalds #include <linux/proc_fs.h> 551da177e4SLinus Torvalds #include <linux/seq_file.h> 561da177e4SLinus Torvalds #include <linux/major.h> 571da177e4SLinus Torvalds #include <linux/root_dev.h> 581da177e4SLinus Torvalds #include <linux/delay.h> 5943d60661SAdrian Bunk #include <linux/nfs_fs.h> 605a0e3ad6STejun Heo #include <linux/slab.h> 61bc3b2d7fSPaul Gortmaker #include <linux/export.h> 62457c4cbcSEric W. Biederman #include <net/net_namespace.h> 631da177e4SLinus Torvalds #include <net/arp.h> 641da177e4SLinus Torvalds #include <net/ip.h> 651da177e4SLinus Torvalds #include <net/ipconfig.h> 6614c85021SArnaldo Carvalho de Melo #include <net/route.h> 671da177e4SLinus Torvalds 687c0f6ba6SLinus Torvalds #include <linux/uaccess.h> 691da177e4SLinus Torvalds #include <net/checksum.h> 701da177e4SLinus Torvalds #include <asm/processor.h> 711da177e4SLinus Torvalds 721da177e4SLinus Torvalds #if defined(CONFIG_IP_PNP_DHCP) 731da177e4SLinus Torvalds #define IPCONFIG_DHCP 741da177e4SLinus Torvalds #endif 751da177e4SLinus Torvalds #if defined(CONFIG_IP_PNP_BOOTP) || defined(CONFIG_IP_PNP_DHCP) 761da177e4SLinus Torvalds #define IPCONFIG_BOOTP 771da177e4SLinus Torvalds #endif 781da177e4SLinus Torvalds #if defined(CONFIG_IP_PNP_RARP) 791da177e4SLinus Torvalds #define IPCONFIG_RARP 801da177e4SLinus Torvalds #endif 811da177e4SLinus Torvalds #if defined(IPCONFIG_BOOTP) || defined(IPCONFIG_RARP) 821da177e4SLinus Torvalds #define IPCONFIG_DYNAMIC 831da177e4SLinus Torvalds #endif 841da177e4SLinus Torvalds 851da177e4SLinus Torvalds /* Define the friendly delay before and after opening net devices */ 863fb72f1eSMicha Nelissen #define CONF_POST_OPEN 10 /* After opening: 10 msecs */ 871da177e4SLinus Torvalds 881da177e4SLinus Torvalds /* Define the timeout for waiting for a DHCP/BOOTP/RARP reply */ 891da177e4SLinus Torvalds #define CONF_OPEN_RETRIES 2 /* (Re)open devices twice */ 901da177e4SLinus Torvalds #define CONF_SEND_RETRIES 6 /* Send six requests per open */ 911da177e4SLinus Torvalds #define CONF_BASE_TIMEOUT (HZ*2) /* Initial timeout: 2 seconds */ 921da177e4SLinus Torvalds #define CONF_TIMEOUT_RANDOM (HZ) /* Maximum amount of randomization */ 931da177e4SLinus Torvalds #define CONF_TIMEOUT_MULT *7/4 /* Rate of timeout growth */ 941da177e4SLinus Torvalds #define CONF_TIMEOUT_MAX (HZ*30) /* Maximum allowed timeout */ 951da177e4SLinus Torvalds #define CONF_NAMESERVERS_MAX 3 /* Maximum number of nameservers 961da177e4SLinus Torvalds - '3' from resolv.h */ 97c04d2cb2SChris Novakovic #define CONF_NTP_SERVERS_MAX 3 /* Maximum number of NTP servers */ 981da177e4SLinus Torvalds 9909640e63SHarvey Harrison #define NONE cpu_to_be32(INADDR_NONE) 10009640e63SHarvey Harrison #define ANY cpu_to_be32(INADDR_ANY) 1011da177e4SLinus Torvalds 1023fc46fc9SMartin Kepplinger /* Wait for carrier timeout default in seconds */ 1033fc46fc9SMartin Kepplinger static unsigned int carrier_timeout = 120; 1043fc46fc9SMartin Kepplinger 1051da177e4SLinus Torvalds /* 1061da177e4SLinus Torvalds * Public IP configuration 1071da177e4SLinus Torvalds */ 1081da177e4SLinus Torvalds 1091da177e4SLinus Torvalds /* This is used by platforms which might be able to set the ipconfig 1101da177e4SLinus Torvalds * variables using firmware environment vars. If this is set, it will 1111da177e4SLinus Torvalds * ignore such firmware variables. 1121da177e4SLinus Torvalds */ 1131da177e4SLinus Torvalds int ic_set_manually __initdata = 0; /* IPconfig parameters set manually */ 1141da177e4SLinus Torvalds 1150d3979b9SFabian Frederick static int ic_enable __initdata; /* IP config enabled? */ 1161da177e4SLinus Torvalds 1171da177e4SLinus Torvalds /* Protocol choice */ 1181da177e4SLinus Torvalds int ic_proto_enabled __initdata = 0 1191da177e4SLinus Torvalds #ifdef IPCONFIG_BOOTP 1201da177e4SLinus Torvalds | IC_BOOTP 1211da177e4SLinus Torvalds #endif 1221da177e4SLinus Torvalds #ifdef CONFIG_IP_PNP_DHCP 1231da177e4SLinus Torvalds | IC_USE_DHCP 1241da177e4SLinus Torvalds #endif 1251da177e4SLinus Torvalds #ifdef IPCONFIG_RARP 1261da177e4SLinus Torvalds | IC_RARP 1271da177e4SLinus Torvalds #endif 1281da177e4SLinus Torvalds ; 1291da177e4SLinus Torvalds 1300d3979b9SFabian Frederick static int ic_host_name_set __initdata; /* Host name set by us? */ 1311da177e4SLinus Torvalds 1325a874db4SAl Viro __be32 ic_myaddr = NONE; /* My IP address */ 1335a874db4SAl Viro static __be32 ic_netmask = NONE; /* Netmask for local subnet */ 1345a874db4SAl Viro __be32 ic_gateway = NONE; /* Gateway IP address */ 1351da177e4SLinus Torvalds 13686ef7f9cSDavid S. Miller #ifdef IPCONFIG_DYNAMIC 1370b392be9SBen Dooks static __be32 ic_addrservaddr = NONE; /* IP Address of the IP addresses'server */ 13886ef7f9cSDavid S. Miller #endif 1399dd4a13aSPhilippe De Muyter 1405a874db4SAl Viro __be32 ic_servaddr = NONE; /* Boot server IP address */ 1411da177e4SLinus Torvalds 1425a874db4SAl Viro __be32 root_server_addr = NONE; /* Address of NFS server */ 1431da177e4SLinus Torvalds u8 root_server_path[256] = { 0, }; /* Path to mount as root */ 1441da177e4SLinus Torvalds 1451b0b04f9SDavid S. Miller /* vendor class identifier */ 1461b0b04f9SDavid S. Miller static char vendor_class_identifier[253] __initdata; 14762013dbbSRainer Jochem 14826fb342cSLi RongQing #if defined(CONFIG_IP_PNP_DHCP) 14926fb342cSLi RongQing static char dhcp_client_identifier[253] __initdata; 15026fb342cSLi RongQing #endif 15126fb342cSLi RongQing 1521da177e4SLinus Torvalds /* Persistent data: */ 1531da177e4SLinus Torvalds 15452b79e2bSArnd Bergmann #ifdef IPCONFIG_DYNAMIC 1551da177e4SLinus Torvalds static int ic_proto_used; /* Protocol used, if any */ 15652b79e2bSArnd Bergmann #else 15752b79e2bSArnd Bergmann #define ic_proto_used 0 15852b79e2bSArnd Bergmann #endif 1595a874db4SAl Viro static __be32 ic_nameservers[CONF_NAMESERVERS_MAX]; /* DNS Server IP addresses */ 160c04d2cb2SChris Novakovic static __be32 ic_ntp_servers[CONF_NTP_SERVERS_MAX]; /* NTP server IP addresses */ 1611da177e4SLinus Torvalds static u8 ic_domain[64]; /* DNS (not NIS) domain name */ 1621da177e4SLinus Torvalds 1631da177e4SLinus Torvalds /* 1641da177e4SLinus Torvalds * Private state. 1651da177e4SLinus Torvalds */ 1661da177e4SLinus Torvalds 1671da177e4SLinus Torvalds /* Name of user-selected boot device */ 1681da177e4SLinus Torvalds static char user_dev_name[IFNAMSIZ] __initdata = { 0, }; 1691da177e4SLinus Torvalds 1701da177e4SLinus Torvalds /* Protocols supported by available interfaces */ 1710d3979b9SFabian Frederick static int ic_proto_have_if __initdata; 1721da177e4SLinus Torvalds 1739643f455SChris Friesen /* MTU for boot device */ 1740d3979b9SFabian Frederick static int ic_dev_mtu __initdata; 1759643f455SChris Friesen 1761da177e4SLinus Torvalds #ifdef IPCONFIG_DYNAMIC 1771da177e4SLinus Torvalds static DEFINE_SPINLOCK(ic_recv_lock); 1780d3979b9SFabian Frederick static volatile int ic_got_reply __initdata; /* Proto(s) that replied */ 1791da177e4SLinus Torvalds #endif 1801da177e4SLinus Torvalds #ifdef IPCONFIG_DHCP 1810d3979b9SFabian Frederick static int ic_dhcp_msgtype __initdata; /* DHCP msg type received */ 1821da177e4SLinus Torvalds #endif 1831da177e4SLinus Torvalds 1841da177e4SLinus Torvalds 1851da177e4SLinus Torvalds /* 1861da177e4SLinus Torvalds * Network devices 1871da177e4SLinus Torvalds */ 1881da177e4SLinus Torvalds 1891da177e4SLinus Torvalds struct ic_device { 1901da177e4SLinus Torvalds struct ic_device *next; 1911da177e4SLinus Torvalds struct net_device *dev; 1921da177e4SLinus Torvalds unsigned short flags; 1931da177e4SLinus Torvalds short able; 1945a874db4SAl Viro __be32 xid; 1951da177e4SLinus Torvalds }; 1961da177e4SLinus Torvalds 1970d3979b9SFabian Frederick static struct ic_device *ic_first_dev __initdata; /* List of open device */ 1982647cffbSUwe Kleine-König static struct ic_device *ic_dev __initdata; /* Selected device */ 1991da177e4SLinus Torvalds 2003fb72f1eSMicha Nelissen static bool __init ic_is_init_dev(struct net_device *dev) 201964ad81cSDavid S. Miller { 2023fb72f1eSMicha Nelissen if (dev->flags & IFF_LOOPBACK) 2033fb72f1eSMicha Nelissen return false; 2043fb72f1eSMicha Nelissen return user_dev_name[0] ? !strcmp(dev->name, user_dev_name) : 205964ad81cSDavid S. Miller (!(dev->flags & IFF_LOOPBACK) && 206964ad81cSDavid S. Miller (dev->flags & (IFF_POINTOPOINT|IFF_BROADCAST)) && 2073fb72f1eSMicha Nelissen strncmp(dev->name, "dummy", 5)); 208964ad81cSDavid S. Miller } 209964ad81cSDavid S. Miller 2101da177e4SLinus Torvalds static int __init ic_open_devs(void) 2111da177e4SLinus Torvalds { 2121da177e4SLinus Torvalds struct ic_device *d, **last; 2131da177e4SLinus Torvalds struct net_device *dev; 2141da177e4SLinus Torvalds unsigned short oflags; 2155e404cd6SPaul Gortmaker unsigned long start, next_msg; 2161da177e4SLinus Torvalds 2171da177e4SLinus Torvalds last = &ic_first_dev; 2186756ae4bSStephen Hemminger rtnl_lock(); 2191da177e4SLinus Torvalds 22046acf7bdSVladimir Oltean /* bring loopback device up first */ 2210cc217e1SEric W. Biederman for_each_netdev(&init_net, dev) { 22246acf7bdSVladimir Oltean if (!(dev->flags & IFF_LOOPBACK)) 2230cc217e1SEric W. Biederman continue; 224567c5e13SPetr Machata if (dev_change_flags(dev, dev->flags | IFF_UP, NULL) < 0) 225058bd4d2SJoe Perches pr_err("IP-Config: Failed to open %s\n", dev->name); 2260cc217e1SEric W. Biederman } 2271da177e4SLinus Torvalds 228881d966bSEric W. Biederman for_each_netdev(&init_net, dev) { 2293fb72f1eSMicha Nelissen if (ic_is_init_dev(dev)) { 2301da177e4SLinus Torvalds int able = 0; 2311da177e4SLinus Torvalds if (dev->mtu >= 364) 2321da177e4SLinus Torvalds able |= IC_BOOTP; 2331da177e4SLinus Torvalds else 23409605cc1SBastian Stender pr_warn("DHCP/BOOTP: Ignoring device %s, MTU %d too small\n", 235058bd4d2SJoe Perches dev->name, dev->mtu); 2361da177e4SLinus Torvalds if (!(dev->flags & IFF_NOARP)) 2371da177e4SLinus Torvalds able |= IC_RARP; 2381da177e4SLinus Torvalds able &= ic_proto_enabled; 2391da177e4SLinus Torvalds if (ic_proto_enabled && !able) 2401da177e4SLinus Torvalds continue; 2411da177e4SLinus Torvalds oflags = dev->flags; 242567c5e13SPetr Machata if (dev_change_flags(dev, oflags | IFF_UP, NULL) < 0) { 243058bd4d2SJoe Perches pr_err("IP-Config: Failed to open %s\n", 244058bd4d2SJoe Perches dev->name); 2451da177e4SLinus Torvalds continue; 2461da177e4SLinus Torvalds } 2471da177e4SLinus Torvalds if (!(d = kmalloc(sizeof(struct ic_device), GFP_KERNEL))) { 2486756ae4bSStephen Hemminger rtnl_unlock(); 249964ad81cSDavid S. Miller return -ENOMEM; 2501da177e4SLinus Torvalds } 2511da177e4SLinus Torvalds d->dev = dev; 2521da177e4SLinus Torvalds *last = d; 2531da177e4SLinus Torvalds last = &d->next; 2541da177e4SLinus Torvalds d->flags = oflags; 2551da177e4SLinus Torvalds d->able = able; 2561da177e4SLinus Torvalds if (able & IC_BOOTP) 2575a874db4SAl Viro get_random_bytes(&d->xid, sizeof(__be32)); 2581da177e4SLinus Torvalds else 2591da177e4SLinus Torvalds d->xid = 0; 2601da177e4SLinus Torvalds ic_proto_have_if |= able; 26109605cc1SBastian Stender pr_debug("IP-Config: %s UP (able=%d, xid=%08x)\n", 26209605cc1SBastian Stender dev->name, able, d->xid); 2631da177e4SLinus Torvalds } 2641da177e4SLinus Torvalds } 265ee046d9aSMaxime Chevallier /* Devices with a complex topology like SFP ethernet interfaces needs 266ee046d9aSMaxime Chevallier * the rtnl_lock at init. The carrier wait-loop must therefore run 267ee046d9aSMaxime Chevallier * without holding it. 268ee046d9aSMaxime Chevallier */ 269ee046d9aSMaxime Chevallier rtnl_unlock(); 2703fb72f1eSMicha Nelissen 271cd7816d1SGerlando Falauto /* no point in waiting if we could not bring up at least one device */ 272cd7816d1SGerlando Falauto if (!ic_first_dev) 273cd7816d1SGerlando Falauto goto have_carrier; 274cd7816d1SGerlando Falauto 2753fb72f1eSMicha Nelissen /* wait for a carrier on at least one device */ 2763fb72f1eSMicha Nelissen start = jiffies; 2773fc46fc9SMartin Kepplinger next_msg = start + msecs_to_jiffies(20000); 278c72c95a0SHimangi Saraogi while (time_before(jiffies, start + 2793fc46fc9SMartin Kepplinger msecs_to_jiffies(carrier_timeout * 1000))) { 2805e404cd6SPaul Gortmaker int wait, elapsed; 2815e404cd6SPaul Gortmaker 282ee046d9aSMaxime Chevallier rtnl_lock(); 2833fb72f1eSMicha Nelissen for_each_netdev(&init_net, dev) 284ee046d9aSMaxime Chevallier if (ic_is_init_dev(dev) && netif_carrier_ok(dev)) { 285ee046d9aSMaxime Chevallier rtnl_unlock(); 2863fb72f1eSMicha Nelissen goto have_carrier; 287ee046d9aSMaxime Chevallier } 288ee046d9aSMaxime Chevallier rtnl_unlock(); 2893fb72f1eSMicha Nelissen 2903fb72f1eSMicha Nelissen msleep(1); 2915e404cd6SPaul Gortmaker 292357137a4SFX Le Bail if (time_before(jiffies, next_msg)) 2935e404cd6SPaul Gortmaker continue; 2945e404cd6SPaul Gortmaker 2955e404cd6SPaul Gortmaker elapsed = jiffies_to_msecs(jiffies - start); 2963fc46fc9SMartin Kepplinger wait = (carrier_timeout * 1000 - elapsed + 500) / 1000; 2975e404cd6SPaul Gortmaker pr_info("Waiting up to %d more seconds for network.\n", wait); 2983fc46fc9SMartin Kepplinger next_msg = jiffies + msecs_to_jiffies(20000); 2993fb72f1eSMicha Nelissen } 3003fb72f1eSMicha Nelissen have_carrier: 3011da177e4SLinus Torvalds 3021da177e4SLinus Torvalds *last = NULL; 3031da177e4SLinus Torvalds 3041da177e4SLinus Torvalds if (!ic_first_dev) { 3051da177e4SLinus Torvalds if (user_dev_name[0]) 306058bd4d2SJoe Perches pr_err("IP-Config: Device `%s' not found\n", 307058bd4d2SJoe Perches user_dev_name); 3081da177e4SLinus Torvalds else 309058bd4d2SJoe Perches pr_err("IP-Config: No network devices available\n"); 310964ad81cSDavid S. Miller return -ENODEV; 3111da177e4SLinus Torvalds } 3121da177e4SLinus Torvalds return 0; 3131da177e4SLinus Torvalds } 3141da177e4SLinus Torvalds 31546acf7bdSVladimir Oltean /* Close all network interfaces except the one we've autoconfigured, and its 31646acf7bdSVladimir Oltean * lowers, in case it's a stacked virtual interface. 31746acf7bdSVladimir Oltean */ 3181da177e4SLinus Torvalds static void __init ic_close_devs(void) 3191da177e4SLinus Torvalds { 320a50a151eSVladimir Oltean struct net_device *selected_dev = ic_dev ? ic_dev->dev : NULL; 3211da177e4SLinus Torvalds struct ic_device *d, *next; 3221da177e4SLinus Torvalds struct net_device *dev; 3231da177e4SLinus Torvalds 3246756ae4bSStephen Hemminger rtnl_lock(); 3251da177e4SLinus Torvalds next = ic_first_dev; 3261da177e4SLinus Torvalds while ((d = next)) { 32746acf7bdSVladimir Oltean bool bring_down = (d != ic_dev); 328a50a151eSVladimir Oltean struct net_device *lower; 32946acf7bdSVladimir Oltean struct list_head *iter; 33046acf7bdSVladimir Oltean 3311da177e4SLinus Torvalds next = d->next; 3321da177e4SLinus Torvalds dev = d->dev; 33346acf7bdSVladimir Oltean 334a50a151eSVladimir Oltean if (selected_dev) { 335a50a151eSVladimir Oltean netdev_for_each_lower_dev(selected_dev, lower, iter) { 336a50a151eSVladimir Oltean if (dev == lower) { 33746acf7bdSVladimir Oltean bring_down = false; 33846acf7bdSVladimir Oltean break; 33946acf7bdSVladimir Oltean } 34046acf7bdSVladimir Oltean } 341a50a151eSVladimir Oltean } 34246acf7bdSVladimir Oltean if (bring_down) { 34309605cc1SBastian Stender pr_debug("IP-Config: Downing %s\n", dev->name); 344567c5e13SPetr Machata dev_change_flags(dev, d->flags, NULL); 3451da177e4SLinus Torvalds } 3461da177e4SLinus Torvalds kfree(d); 3471da177e4SLinus Torvalds } 3486756ae4bSStephen Hemminger rtnl_unlock(); 3491da177e4SLinus Torvalds } 3501da177e4SLinus Torvalds 3511da177e4SLinus Torvalds /* 3521da177e4SLinus Torvalds * Interface to various network functions. 3531da177e4SLinus Torvalds */ 3541da177e4SLinus Torvalds 3551da177e4SLinus Torvalds static inline void 3565a874db4SAl Viro set_sockaddr(struct sockaddr_in *sin, __be32 addr, __be16 port) 3571da177e4SLinus Torvalds { 3581da177e4SLinus Torvalds sin->sin_family = AF_INET; 3591da177e4SLinus Torvalds sin->sin_addr.s_addr = addr; 3601da177e4SLinus Torvalds sin->sin_port = port; 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); 37603aef17bSAl Viro if ((err = devinet_ioctl(&init_net, 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); 38203aef17bSAl Viro if ((err = devinet_ioctl(&init_net, 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); 38803aef17bSAl Viro if ((err = devinet_ioctl(&init_net, 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) { 3986a88fbe7SAl Viro rtnl_lock(); 3996a88fbe7SAl Viro if ((err = dev_set_mtu(ic_dev->dev, ic_dev_mtu)) < 0) 400058bd4d2SJoe Perches pr_err("IP-Config: Unable to set interface mtu to %d (%d)\n", 4019643f455SChris Friesen ic_dev_mtu, err); 4026a88fbe7SAl Viro rtnl_unlock(); 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; 424ca25c300SAl Viro if ((err = ip_rt_ioctl(&init_net, 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); 45865cab850SDave Taht else if (IN_CLASSE(ntohl(ic_myaddr))) 45965cab850SDave Taht ic_netmask = htonl(IN_CLASSE_NET); 4601da177e4SLinus Torvalds else { 461058bd4d2SJoe Perches pr_err("IP-Config: Unable to guess netmask for address %pI4\n", 462673d57e7SHarvey Harrison &ic_myaddr); 4631da177e4SLinus Torvalds return -1; 4641da177e4SLinus Torvalds } 46509605cc1SBastian Stender pr_notice("IP-Config: Guessing netmask %pI4\n", 46609605cc1SBastian Stender &ic_netmask); 4671da177e4SLinus Torvalds } 4681da177e4SLinus Torvalds 4691da177e4SLinus Torvalds return 0; 4701da177e4SLinus Torvalds } 4711da177e4SLinus Torvalds 4721da177e4SLinus Torvalds /* 4731da177e4SLinus Torvalds * RARP support. 4741da177e4SLinus Torvalds */ 4751da177e4SLinus Torvalds 4761da177e4SLinus Torvalds #ifdef IPCONFIG_RARP 4771da177e4SLinus Torvalds 478f2ccd8faSDavid S. Miller static int ic_rarp_recv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev); 4791da177e4SLinus Torvalds 4801da177e4SLinus Torvalds static struct packet_type rarp_packet_type __initdata = { 48109640e63SHarvey Harrison .type = cpu_to_be16(ETH_P_RARP), 4821da177e4SLinus Torvalds .func = ic_rarp_recv, 4831da177e4SLinus Torvalds }; 4841da177e4SLinus Torvalds 48545e741b8SSam Ravnborg static inline void __init ic_rarp_init(void) 4861da177e4SLinus Torvalds { 4871da177e4SLinus Torvalds dev_add_pack(&rarp_packet_type); 4881da177e4SLinus Torvalds } 4891da177e4SLinus Torvalds 49045e741b8SSam Ravnborg static inline void __init ic_rarp_cleanup(void) 4911da177e4SLinus Torvalds { 4921da177e4SLinus Torvalds dev_remove_pack(&rarp_packet_type); 4931da177e4SLinus Torvalds } 4941da177e4SLinus Torvalds 4951da177e4SLinus Torvalds /* 4961da177e4SLinus Torvalds * Process received RARP packet. 4971da177e4SLinus Torvalds */ 4981da177e4SLinus Torvalds static int __init 499f2ccd8faSDavid S. Miller ic_rarp_recv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev) 5001da177e4SLinus Torvalds { 5011da177e4SLinus Torvalds struct arphdr *rarp; 5021da177e4SLinus Torvalds unsigned char *rarp_ptr; 5035a874db4SAl Viro __be32 sip, tip; 5046b436d33SFabian Frederick unsigned char *tha; /* t for "target" */ 5051da177e4SLinus Torvalds struct ic_device *d; 5061da177e4SLinus Torvalds 507721499e8SYOSHIFUJI Hideaki if (!net_eq(dev_net(dev), &init_net)) 508e730c155SEric W. Biederman goto drop; 509e730c155SEric W. Biederman 51051456b29SIan Morris skb = skb_share_check(skb, GFP_ATOMIC); 51151456b29SIan Morris if (!skb) 5121da177e4SLinus Torvalds return NET_RX_DROP; 5131da177e4SLinus Torvalds 5141da177e4SLinus Torvalds if (!pskb_may_pull(skb, sizeof(struct arphdr))) 5151da177e4SLinus Torvalds goto drop; 5161da177e4SLinus Torvalds 5171da177e4SLinus Torvalds /* Basic sanity checks can be done without the lock. */ 5189c70220bSArnaldo Carvalho de Melo rarp = (struct arphdr *)skb_transport_header(skb); 5191da177e4SLinus Torvalds 5201da177e4SLinus Torvalds /* If this test doesn't pass, it's not IP, or we should 5211da177e4SLinus Torvalds * ignore it anyway. 5221da177e4SLinus Torvalds */ 5231da177e4SLinus Torvalds if (rarp->ar_hln != dev->addr_len || dev->type != ntohs(rarp->ar_hrd)) 5241da177e4SLinus Torvalds goto drop; 5251da177e4SLinus Torvalds 5261da177e4SLinus Torvalds /* If it's not a RARP reply, delete it. */ 5271da177e4SLinus Torvalds if (rarp->ar_op != htons(ARPOP_RREPLY)) 5281da177e4SLinus Torvalds goto drop; 5291da177e4SLinus Torvalds 5301da177e4SLinus Torvalds /* If it's not Ethernet, delete it. */ 5311da177e4SLinus Torvalds if (rarp->ar_pro != htons(ETH_P_IP)) 5321da177e4SLinus Torvalds goto drop; 5331da177e4SLinus Torvalds 534988b7050SPavel Emelyanov if (!pskb_may_pull(skb, arp_hdr_len(dev))) 5351da177e4SLinus Torvalds goto drop; 5361da177e4SLinus Torvalds 5371da177e4SLinus Torvalds /* OK, it is all there and looks valid, process... */ 5389c70220bSArnaldo Carvalho de Melo rarp = (struct arphdr *)skb_transport_header(skb); 5391da177e4SLinus Torvalds rarp_ptr = (unsigned char *) (rarp + 1); 5401da177e4SLinus Torvalds 5411da177e4SLinus Torvalds /* One reply at a time, please. */ 5421da177e4SLinus Torvalds spin_lock(&ic_recv_lock); 5431da177e4SLinus Torvalds 5441da177e4SLinus Torvalds /* If we already have a reply, just drop the packet */ 5451da177e4SLinus Torvalds if (ic_got_reply) 5461da177e4SLinus Torvalds goto drop_unlock; 5471da177e4SLinus Torvalds 5481da177e4SLinus Torvalds /* Find the ic_device that the packet arrived on */ 5491da177e4SLinus Torvalds d = ic_first_dev; 5501da177e4SLinus Torvalds while (d && d->dev != dev) 5511da177e4SLinus Torvalds d = d->next; 5521da177e4SLinus Torvalds if (!d) 5531da177e4SLinus Torvalds goto drop_unlock; /* should never happen */ 5541da177e4SLinus Torvalds 5551da177e4SLinus Torvalds /* Extract variable-width fields */ 5561da177e4SLinus Torvalds rarp_ptr += dev->addr_len; 5571da177e4SLinus Torvalds memcpy(&sip, rarp_ptr, 4); 5581da177e4SLinus Torvalds rarp_ptr += 4; 5591da177e4SLinus Torvalds tha = rarp_ptr; 5601da177e4SLinus Torvalds rarp_ptr += dev->addr_len; 5611da177e4SLinus Torvalds memcpy(&tip, rarp_ptr, 4); 5621da177e4SLinus Torvalds 5631da177e4SLinus Torvalds /* Discard packets which are not meant for us. */ 5641da177e4SLinus Torvalds if (memcmp(tha, dev->dev_addr, dev->addr_len)) 5651da177e4SLinus Torvalds goto drop_unlock; 5661da177e4SLinus Torvalds 5671da177e4SLinus Torvalds /* Discard packets which are not from specified server. */ 5685a874db4SAl Viro if (ic_servaddr != NONE && ic_servaddr != sip) 5691da177e4SLinus Torvalds goto drop_unlock; 5701da177e4SLinus Torvalds 5711da177e4SLinus Torvalds /* We have a winner! */ 5722647cffbSUwe Kleine-König ic_dev = d; 5735a874db4SAl Viro if (ic_myaddr == NONE) 5741da177e4SLinus Torvalds ic_myaddr = tip; 5751da177e4SLinus Torvalds ic_servaddr = sip; 5769dd4a13aSPhilippe De Muyter ic_addrservaddr = sip; 5771da177e4SLinus Torvalds ic_got_reply = IC_RARP; 5781da177e4SLinus Torvalds 5791da177e4SLinus Torvalds drop_unlock: 5801da177e4SLinus Torvalds /* Show's over. Nothing to see here. */ 5811da177e4SLinus Torvalds spin_unlock(&ic_recv_lock); 5821da177e4SLinus Torvalds 5831da177e4SLinus Torvalds drop: 5841da177e4SLinus Torvalds /* Throw the packet out. */ 5851da177e4SLinus Torvalds kfree_skb(skb); 5861da177e4SLinus Torvalds return 0; 5871da177e4SLinus Torvalds } 5881da177e4SLinus Torvalds 5891da177e4SLinus Torvalds 5901da177e4SLinus Torvalds /* 5911da177e4SLinus Torvalds * Send RARP request packet over a single interface. 5921da177e4SLinus Torvalds */ 5931da177e4SLinus Torvalds static void __init ic_rarp_send_if(struct ic_device *d) 5941da177e4SLinus Torvalds { 5951da177e4SLinus Torvalds struct net_device *dev = d->dev; 5961da177e4SLinus Torvalds arp_send(ARPOP_RREQUEST, ETH_P_RARP, 0, dev, 0, NULL, 5971da177e4SLinus Torvalds dev->dev_addr, dev->dev_addr); 5981da177e4SLinus Torvalds } 5991da177e4SLinus Torvalds #endif 6001da177e4SLinus Torvalds 6011da177e4SLinus Torvalds /* 602842b08bbSAndy Shevchenko * Predefine Nameservers 603842b08bbSAndy Shevchenko */ 604842b08bbSAndy Shevchenko static inline void __init ic_nameservers_predef(void) 605842b08bbSAndy Shevchenko { 606842b08bbSAndy Shevchenko int i; 607842b08bbSAndy Shevchenko 608842b08bbSAndy Shevchenko for (i = 0; i < CONF_NAMESERVERS_MAX; i++) 609842b08bbSAndy Shevchenko ic_nameservers[i] = NONE; 610842b08bbSAndy Shevchenko } 611842b08bbSAndy Shevchenko 612c04d2cb2SChris Novakovic /* Predefine NTP servers */ 613c04d2cb2SChris Novakovic static inline void __init ic_ntp_servers_predef(void) 614c04d2cb2SChris Novakovic { 615c04d2cb2SChris Novakovic int i; 616c04d2cb2SChris Novakovic 617c04d2cb2SChris Novakovic for (i = 0; i < CONF_NTP_SERVERS_MAX; i++) 618c04d2cb2SChris Novakovic ic_ntp_servers[i] = NONE; 619c04d2cb2SChris Novakovic } 620c04d2cb2SChris Novakovic 621842b08bbSAndy Shevchenko /* 6221da177e4SLinus Torvalds * DHCP/BOOTP support. 6231da177e4SLinus Torvalds */ 6241da177e4SLinus Torvalds 6251da177e4SLinus Torvalds #ifdef IPCONFIG_BOOTP 6261da177e4SLinus Torvalds 6271da177e4SLinus Torvalds struct bootp_pkt { /* BOOTP packet format */ 6281da177e4SLinus Torvalds struct iphdr iph; /* IP header */ 6291da177e4SLinus Torvalds struct udphdr udph; /* UDP header */ 6301da177e4SLinus Torvalds u8 op; /* 1=request, 2=reply */ 6311da177e4SLinus Torvalds u8 htype; /* HW address type */ 6321da177e4SLinus Torvalds u8 hlen; /* HW address length */ 6331da177e4SLinus Torvalds u8 hops; /* Used only by gateways */ 6345a874db4SAl Viro __be32 xid; /* Transaction ID */ 6355a874db4SAl Viro __be16 secs; /* Seconds since we started */ 6365a874db4SAl Viro __be16 flags; /* Just what it says */ 6375a874db4SAl Viro __be32 client_ip; /* Client's IP address if known */ 6385a874db4SAl Viro __be32 your_ip; /* Assigned IP address */ 6395a874db4SAl Viro __be32 server_ip; /* (Next, e.g. NFS) Server's IP address */ 6405a874db4SAl Viro __be32 relay_ip; /* IP address of BOOTP relay */ 6411da177e4SLinus Torvalds u8 hw_addr[16]; /* Client's HW address */ 6421da177e4SLinus Torvalds u8 serv_name[64]; /* Server host name */ 6431da177e4SLinus Torvalds u8 boot_file[128]; /* Name of boot file */ 6441da177e4SLinus Torvalds u8 exten[312]; /* DHCP options / BOOTP vendor extensions */ 6451da177e4SLinus Torvalds }; 6461da177e4SLinus Torvalds 6471da177e4SLinus Torvalds /* packet ops */ 6481da177e4SLinus Torvalds #define BOOTP_REQUEST 1 6491da177e4SLinus Torvalds #define BOOTP_REPLY 2 6501da177e4SLinus Torvalds 6511da177e4SLinus Torvalds /* DHCP message types */ 6521da177e4SLinus Torvalds #define DHCPDISCOVER 1 6531da177e4SLinus Torvalds #define DHCPOFFER 2 6541da177e4SLinus Torvalds #define DHCPREQUEST 3 6551da177e4SLinus Torvalds #define DHCPDECLINE 4 6561da177e4SLinus Torvalds #define DHCPACK 5 6571da177e4SLinus Torvalds #define DHCPNAK 6 6581da177e4SLinus Torvalds #define DHCPRELEASE 7 6591da177e4SLinus Torvalds #define DHCPINFORM 8 6601da177e4SLinus Torvalds 661f2ccd8faSDavid S. Miller static int ic_bootp_recv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev); 6621da177e4SLinus Torvalds 6631da177e4SLinus Torvalds static struct packet_type bootp_packet_type __initdata = { 66409640e63SHarvey Harrison .type = cpu_to_be16(ETH_P_IP), 6651da177e4SLinus Torvalds .func = ic_bootp_recv, 6661da177e4SLinus Torvalds }; 6671da177e4SLinus Torvalds 6681da177e4SLinus Torvalds /* 6691da177e4SLinus Torvalds * Initialize DHCP/BOOTP extension fields in the request. 6701da177e4SLinus Torvalds */ 6711da177e4SLinus Torvalds 6721da177e4SLinus Torvalds static const u8 ic_bootp_cookie[4] = { 99, 130, 83, 99 }; 6731da177e4SLinus Torvalds 6741da177e4SLinus Torvalds #ifdef IPCONFIG_DHCP 6751da177e4SLinus Torvalds 6761da177e4SLinus Torvalds static void __init 67722fc5388SUwe Kleine-König ic_dhcp_init_options(u8 *options, struct ic_device *d) 6781da177e4SLinus Torvalds { 6795a874db4SAl Viro u8 mt = ((ic_servaddr == NONE) 6801da177e4SLinus Torvalds ? DHCPDISCOVER : DHCPREQUEST); 6811da177e4SLinus Torvalds u8 *e = options; 68262013dbbSRainer Jochem int len; 6831da177e4SLinus Torvalds 68422fc5388SUwe Kleine-König pr_debug("DHCP: Sending message type %d (%s)\n", mt, d->dev->name); 6851da177e4SLinus Torvalds 6861da177e4SLinus Torvalds memcpy(e, ic_bootp_cookie, 4); /* RFC1048 Magic Cookie */ 6871da177e4SLinus Torvalds e += 4; 6881da177e4SLinus Torvalds 6891da177e4SLinus Torvalds *e++ = 53; /* DHCP message type */ 6901da177e4SLinus Torvalds *e++ = 1; 6911da177e4SLinus Torvalds *e++ = mt; 6921da177e4SLinus Torvalds 6931da177e4SLinus Torvalds if (mt == DHCPREQUEST) { 6941da177e4SLinus Torvalds *e++ = 54; /* Server ID (IP address) */ 6951da177e4SLinus Torvalds *e++ = 4; 6961da177e4SLinus Torvalds memcpy(e, &ic_servaddr, 4); 6971da177e4SLinus Torvalds e += 4; 6981da177e4SLinus Torvalds 6991da177e4SLinus Torvalds *e++ = 50; /* Requested IP address */ 7001da177e4SLinus Torvalds *e++ = 4; 7011da177e4SLinus Torvalds memcpy(e, &ic_myaddr, 4); 7021da177e4SLinus Torvalds e += 4; 7031da177e4SLinus Torvalds } 7041da177e4SLinus Torvalds 7051da177e4SLinus Torvalds /* always? */ 7061da177e4SLinus Torvalds { 7071da177e4SLinus Torvalds static const u8 ic_req_params[] = { 7081da177e4SLinus Torvalds 1, /* Subnet mask */ 7091da177e4SLinus Torvalds 3, /* Default gateway */ 7101da177e4SLinus Torvalds 6, /* DNS server */ 7111da177e4SLinus Torvalds 12, /* Host name */ 7121da177e4SLinus Torvalds 15, /* Domain name */ 7131da177e4SLinus Torvalds 17, /* Boot path */ 7149643f455SChris Friesen 26, /* MTU */ 7151da177e4SLinus Torvalds 40, /* NIS domain name */ 716c04d2cb2SChris Novakovic 42, /* NTP servers */ 7171da177e4SLinus Torvalds }; 7181da177e4SLinus Torvalds 7191da177e4SLinus Torvalds *e++ = 55; /* Parameter request list */ 7201da177e4SLinus Torvalds *e++ = sizeof(ic_req_params); 7211da177e4SLinus Torvalds memcpy(e, ic_req_params, sizeof(ic_req_params)); 7221da177e4SLinus Torvalds e += sizeof(ic_req_params); 72362013dbbSRainer Jochem 724130c0f47SWu Fengguang if (ic_host_name_set) { 725130c0f47SWu Fengguang *e++ = 12; /* host-name */ 726130c0f47SWu Fengguang len = strlen(utsname()->nodename); 727130c0f47SWu Fengguang *e++ = len; 728130c0f47SWu Fengguang memcpy(e, utsname()->nodename, len); 729130c0f47SWu Fengguang e += len; 730130c0f47SWu Fengguang } 73162013dbbSRainer Jochem if (*vendor_class_identifier) { 732058bd4d2SJoe Perches pr_info("DHCP: sending class identifier \"%s\"\n", 73362013dbbSRainer Jochem vendor_class_identifier); 73462013dbbSRainer Jochem *e++ = 60; /* Class-identifier */ 73562013dbbSRainer Jochem len = strlen(vendor_class_identifier); 73662013dbbSRainer Jochem *e++ = len; 73762013dbbSRainer Jochem memcpy(e, vendor_class_identifier, len); 73862013dbbSRainer Jochem e += len; 73962013dbbSRainer Jochem } 74026fb342cSLi RongQing len = strlen(dhcp_client_identifier + 1); 74126fb342cSLi RongQing /* the minimum length of identifier is 2, include 1 byte type, 74226fb342cSLi RongQing * and can not be larger than the length of options 74326fb342cSLi RongQing */ 74426fb342cSLi RongQing if (len >= 1 && len < 312 - (e - options) - 1) { 74526fb342cSLi RongQing *e++ = 61; 74626fb342cSLi RongQing *e++ = len + 1; 74726fb342cSLi RongQing memcpy(e, dhcp_client_identifier, len + 1); 74826fb342cSLi RongQing e += len + 1; 74926fb342cSLi RongQing } 7501da177e4SLinus Torvalds } 7511da177e4SLinus Torvalds 7521da177e4SLinus Torvalds *e++ = 255; /* End of the list */ 7531da177e4SLinus Torvalds } 7541da177e4SLinus Torvalds 7551da177e4SLinus Torvalds #endif /* IPCONFIG_DHCP */ 7561da177e4SLinus Torvalds 7571da177e4SLinus Torvalds static void __init ic_bootp_init_ext(u8 *e) 7581da177e4SLinus Torvalds { 7591da177e4SLinus Torvalds memcpy(e, ic_bootp_cookie, 4); /* RFC1048 Magic Cookie */ 7601da177e4SLinus Torvalds e += 4; 7611da177e4SLinus Torvalds *e++ = 1; /* Subnet mask request */ 7621da177e4SLinus Torvalds *e++ = 4; 7631da177e4SLinus Torvalds e += 4; 7641da177e4SLinus Torvalds *e++ = 3; /* Default gateway request */ 7651da177e4SLinus Torvalds *e++ = 4; 7661da177e4SLinus Torvalds e += 4; 767de1fa15bSChris Novakovic #if CONF_NAMESERVERS_MAX > 0 7684e1a8af2SChris Novakovic *e++ = 6; /* (DNS) name server request */ 769de1fa15bSChris Novakovic *e++ = 4 * CONF_NAMESERVERS_MAX; 770de1fa15bSChris Novakovic e += 4 * CONF_NAMESERVERS_MAX; 771de1fa15bSChris Novakovic #endif 7721da177e4SLinus Torvalds *e++ = 12; /* Host name request */ 7731da177e4SLinus Torvalds *e++ = 32; 7741da177e4SLinus Torvalds e += 32; 7751da177e4SLinus Torvalds *e++ = 40; /* NIS Domain name request */ 7761da177e4SLinus Torvalds *e++ = 32; 7771da177e4SLinus Torvalds e += 32; 7781da177e4SLinus Torvalds *e++ = 17; /* Boot path */ 7791da177e4SLinus Torvalds *e++ = 40; 7801da177e4SLinus Torvalds e += 40; 7811da177e4SLinus Torvalds 7821da177e4SLinus Torvalds *e++ = 57; /* set extension buffer size for reply */ 7831da177e4SLinus Torvalds *e++ = 2; 7841da177e4SLinus Torvalds *e++ = 1; /* 128+236+8+20+14, see dhcpd sources */ 7851da177e4SLinus Torvalds *e++ = 150; 7861da177e4SLinus Torvalds 7871da177e4SLinus Torvalds *e++ = 255; /* End of the list */ 7881da177e4SLinus Torvalds } 7891da177e4SLinus Torvalds 7901da177e4SLinus Torvalds 7911da177e4SLinus Torvalds /* 7925e953778SChristoph Fritz * Initialize the DHCP/BOOTP mechanism. 7935e953778SChristoph Fritz */ 7945e953778SChristoph Fritz static inline void __init ic_bootp_init(void) 7955e953778SChristoph Fritz { 796c04d2cb2SChris Novakovic /* Re-initialise all name servers and NTP servers to NONE, in case any 797c04d2cb2SChris Novakovic * were set via the "ip=" or "nfsaddrs=" kernel command line parameters: 798c04d2cb2SChris Novakovic * any IP addresses specified there will already have been decoded but 799c04d2cb2SChris Novakovic * are no longer needed 800300eec7cSChris Novakovic */ 8015e953778SChristoph Fritz ic_nameservers_predef(); 802c04d2cb2SChris Novakovic ic_ntp_servers_predef(); 8031da177e4SLinus Torvalds 8041da177e4SLinus Torvalds dev_add_pack(&bootp_packet_type); 8051da177e4SLinus Torvalds } 8061da177e4SLinus Torvalds 8071da177e4SLinus Torvalds 8081da177e4SLinus Torvalds /* 8091da177e4SLinus Torvalds * DHCP/BOOTP cleanup. 8101da177e4SLinus Torvalds */ 81145e741b8SSam Ravnborg static inline void __init ic_bootp_cleanup(void) 8121da177e4SLinus Torvalds { 8131da177e4SLinus Torvalds dev_remove_pack(&bootp_packet_type); 8141da177e4SLinus Torvalds } 8151da177e4SLinus Torvalds 8161da177e4SLinus Torvalds 8171da177e4SLinus Torvalds /* 8181da177e4SLinus Torvalds * Send DHCP/BOOTP request to single interface. 8191da177e4SLinus Torvalds */ 8201da177e4SLinus Torvalds static void __init ic_bootp_send_if(struct ic_device *d, unsigned long jiffies_diff) 8211da177e4SLinus Torvalds { 8221da177e4SLinus Torvalds struct net_device *dev = d->dev; 8231da177e4SLinus Torvalds struct sk_buff *skb; 8241da177e4SLinus Torvalds struct bootp_pkt *b; 8251da177e4SLinus Torvalds struct iphdr *h; 82666088243SHerbert Xu int hlen = LL_RESERVED_SPACE(dev); 82766088243SHerbert Xu int tlen = dev->needed_tailroom; 8281da177e4SLinus Torvalds 8291da177e4SLinus Torvalds /* Allocate packet */ 83066088243SHerbert Xu skb = alloc_skb(sizeof(struct bootp_pkt) + hlen + tlen + 15, 831f5184d26SJohannes Berg GFP_KERNEL); 8321da177e4SLinus Torvalds if (!skb) 8331da177e4SLinus Torvalds return; 83466088243SHerbert Xu skb_reserve(skb, hlen); 835b080db58SJohannes Berg b = skb_put_zero(skb, sizeof(struct bootp_pkt)); 8361da177e4SLinus Torvalds 8371da177e4SLinus Torvalds /* Construct IP header */ 83804b964dbSArnaldo Carvalho de Melo skb_reset_network_header(skb); 839eddc9ec5SArnaldo Carvalho de Melo h = ip_hdr(skb); 8401da177e4SLinus Torvalds h->version = 4; 8411da177e4SLinus Torvalds h->ihl = 5; 8421da177e4SLinus Torvalds h->tot_len = htons(sizeof(struct bootp_pkt)); 8431da177e4SLinus Torvalds h->frag_off = htons(IP_DF); 8441da177e4SLinus Torvalds h->ttl = 64; 8451da177e4SLinus Torvalds h->protocol = IPPROTO_UDP; 8465a874db4SAl Viro h->daddr = htonl(INADDR_BROADCAST); 8471da177e4SLinus Torvalds h->check = ip_fast_csum((unsigned char *) h, h->ihl); 8481da177e4SLinus Torvalds 8491da177e4SLinus Torvalds /* Construct UDP header */ 8501da177e4SLinus Torvalds b->udph.source = htons(68); 8511da177e4SLinus Torvalds b->udph.dest = htons(67); 8521da177e4SLinus Torvalds b->udph.len = htons(sizeof(struct bootp_pkt) - sizeof(struct iphdr)); 8531da177e4SLinus Torvalds /* UDP checksum not calculated -- explicitly allowed in BOOTP RFC */ 8541da177e4SLinus Torvalds 8551da177e4SLinus Torvalds /* Construct DHCP/BOOTP header */ 8561da177e4SLinus Torvalds b->op = BOOTP_REQUEST; 8571da177e4SLinus Torvalds if (dev->type < 256) /* check for false types */ 8581da177e4SLinus Torvalds b->htype = dev->type; 8591da177e4SLinus Torvalds else if (dev->type == ARPHRD_FDDI) 8601da177e4SLinus Torvalds b->htype = ARPHRD_ETHER; 8611da177e4SLinus Torvalds else { 86209605cc1SBastian Stender pr_warn("Unknown ARP type 0x%04x for device %s\n", dev->type, 86309605cc1SBastian Stender dev->name); 8641da177e4SLinus Torvalds b->htype = dev->type; /* can cause undefined behavior */ 8651da177e4SLinus Torvalds } 866dea75bdfSStephen Hemminger 867dea75bdfSStephen Hemminger /* server_ip and your_ip address are both already zero per RFC2131 */ 8681da177e4SLinus Torvalds b->hlen = dev->addr_len; 8691da177e4SLinus Torvalds memcpy(b->hw_addr, dev->dev_addr, dev->addr_len); 8701da177e4SLinus Torvalds b->secs = htons(jiffies_diff / HZ); 8711da177e4SLinus Torvalds b->xid = d->xid; 8721da177e4SLinus Torvalds 8731da177e4SLinus Torvalds /* add DHCP options or BOOTP extensions */ 8741da177e4SLinus Torvalds #ifdef IPCONFIG_DHCP 8751da177e4SLinus Torvalds if (ic_proto_enabled & IC_USE_DHCP) 87622fc5388SUwe Kleine-König ic_dhcp_init_options(b->exten, d); 8771da177e4SLinus Torvalds else 8781da177e4SLinus Torvalds #endif 8791da177e4SLinus Torvalds ic_bootp_init_ext(b->exten); 8801da177e4SLinus Torvalds 8811da177e4SLinus Torvalds /* Chain packet down the line... */ 8821da177e4SLinus Torvalds skb->dev = dev; 8831da177e4SLinus Torvalds skb->protocol = htons(ETH_P_IP); 8840c4e8581SStephen Hemminger if (dev_hard_header(skb, dev, ntohs(skb->protocol), 885ad79eefcSRongQing.Li dev->broadcast, dev->dev_addr, skb->len) < 0) { 886ad79eefcSRongQing.Li kfree_skb(skb); 887ad79eefcSRongQing.Li printk("E"); 888ad79eefcSRongQing.Li return; 889ad79eefcSRongQing.Li } 890ad79eefcSRongQing.Li 891ad79eefcSRongQing.Li if (dev_queue_xmit(skb) < 0) 8921da177e4SLinus Torvalds printk("E"); 8931da177e4SLinus Torvalds } 8941da177e4SLinus Torvalds 8951da177e4SLinus Torvalds 8961da177e4SLinus Torvalds /* 897b508d5fbSJosh Triplett * Copy BOOTP-supplied string 8981da177e4SLinus Torvalds */ 8991da177e4SLinus Torvalds static int __init ic_bootp_string(char *dest, char *src, int len, int max) 9001da177e4SLinus Torvalds { 9011da177e4SLinus Torvalds if (!len) 9021da177e4SLinus Torvalds return 0; 9031da177e4SLinus Torvalds if (len > max-1) 9041da177e4SLinus Torvalds len = max-1; 9051da177e4SLinus Torvalds memcpy(dest, src, len); 9061da177e4SLinus Torvalds dest[len] = '\0'; 9071da177e4SLinus Torvalds return 1; 9081da177e4SLinus Torvalds } 9091da177e4SLinus Torvalds 9101da177e4SLinus Torvalds 9111da177e4SLinus Torvalds /* 9121da177e4SLinus Torvalds * Process BOOTP extensions. 9131da177e4SLinus Torvalds */ 9141da177e4SLinus Torvalds static void __init ic_do_bootp_ext(u8 *ext) 9151da177e4SLinus Torvalds { 9161da177e4SLinus Torvalds u8 servers; 9171da177e4SLinus Torvalds int i; 918747465efSEric Dumazet __be16 mtu; 9191da177e4SLinus Torvalds 9201da177e4SLinus Torvalds u8 *c; 9211da177e4SLinus Torvalds 92209605cc1SBastian Stender pr_debug("DHCP/BOOTP: Got extension %d:", *ext); 9231da177e4SLinus Torvalds for (c=ext+2; c<ext+2+ext[1]; c++) 92409605cc1SBastian Stender pr_debug(" %02x", *c); 92509605cc1SBastian Stender pr_debug("\n"); 9261da177e4SLinus Torvalds 9271da177e4SLinus Torvalds switch (*ext++) { 9281da177e4SLinus Torvalds case 1: /* Subnet mask */ 9295a874db4SAl Viro if (ic_netmask == NONE) 9301da177e4SLinus Torvalds memcpy(&ic_netmask, ext+1, 4); 9311da177e4SLinus Torvalds break; 9321da177e4SLinus Torvalds case 3: /* Default gateway */ 9335a874db4SAl Viro if (ic_gateway == NONE) 9341da177e4SLinus Torvalds memcpy(&ic_gateway, ext+1, 4); 9351da177e4SLinus Torvalds break; 9361da177e4SLinus Torvalds case 6: /* DNS server */ 9371da177e4SLinus Torvalds servers= *ext/4; 9381da177e4SLinus Torvalds if (servers > CONF_NAMESERVERS_MAX) 9391da177e4SLinus Torvalds servers = CONF_NAMESERVERS_MAX; 9401da177e4SLinus Torvalds for (i = 0; i < servers; i++) { 9415a874db4SAl Viro if (ic_nameservers[i] == NONE) 9421da177e4SLinus Torvalds memcpy(&ic_nameservers[i], ext+1+4*i, 4); 9431da177e4SLinus Torvalds } 9441da177e4SLinus Torvalds break; 9451da177e4SLinus Torvalds case 12: /* Host name */ 946b508d5fbSJosh Triplett if (!ic_host_name_set) { 9471d67a516SJoe Perches ic_bootp_string(utsname()->nodename, ext+1, *ext, 9481d67a516SJoe Perches __NEW_UTS_LEN); 9491da177e4SLinus Torvalds ic_host_name_set = 1; 950b508d5fbSJosh Triplett } 9511da177e4SLinus Torvalds break; 9521da177e4SLinus Torvalds case 15: /* Domain name (DNS) */ 953b508d5fbSJosh Triplett if (!ic_domain[0]) 9541da177e4SLinus Torvalds ic_bootp_string(ic_domain, ext+1, *ext, sizeof(ic_domain)); 9551da177e4SLinus Torvalds break; 9561da177e4SLinus Torvalds case 17: /* Root path */ 9571da177e4SLinus Torvalds if (!root_server_path[0]) 9581d67a516SJoe Perches ic_bootp_string(root_server_path, ext+1, *ext, 9591d67a516SJoe Perches sizeof(root_server_path)); 9601da177e4SLinus Torvalds break; 9619643f455SChris Friesen case 26: /* Interface MTU */ 9629643f455SChris Friesen memcpy(&mtu, ext+1, sizeof(mtu)); 9639643f455SChris Friesen ic_dev_mtu = ntohs(mtu); 9649643f455SChris Friesen break; 9651da177e4SLinus Torvalds case 40: /* NIS Domain name (_not_ DNS) */ 9661d67a516SJoe Perches ic_bootp_string(utsname()->domainname, ext+1, *ext, 9671d67a516SJoe Perches __NEW_UTS_LEN); 9681da177e4SLinus Torvalds break; 969c04d2cb2SChris Novakovic case 42: /* NTP servers */ 970c04d2cb2SChris Novakovic servers = *ext / 4; 971c04d2cb2SChris Novakovic if (servers > CONF_NTP_SERVERS_MAX) 972c04d2cb2SChris Novakovic servers = CONF_NTP_SERVERS_MAX; 973c04d2cb2SChris Novakovic for (i = 0; i < servers; i++) { 974c04d2cb2SChris Novakovic if (ic_ntp_servers[i] == NONE) 975c04d2cb2SChris Novakovic memcpy(&ic_ntp_servers[i], ext+1+4*i, 4); 976c04d2cb2SChris Novakovic } 977c04d2cb2SChris Novakovic break; 9781da177e4SLinus Torvalds } 9791da177e4SLinus Torvalds } 9801da177e4SLinus Torvalds 9811da177e4SLinus Torvalds 9821da177e4SLinus Torvalds /* 9831da177e4SLinus Torvalds * Receive BOOTP reply. 9841da177e4SLinus Torvalds */ 985f2ccd8faSDavid 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) 9861da177e4SLinus Torvalds { 9871da177e4SLinus Torvalds struct bootp_pkt *b; 9881da177e4SLinus Torvalds struct iphdr *h; 9891da177e4SLinus Torvalds struct ic_device *d; 9901da177e4SLinus Torvalds int len, ext_len; 9911da177e4SLinus Torvalds 992721499e8SYOSHIFUJI Hideaki if (!net_eq(dev_net(dev), &init_net)) 993e730c155SEric W. Biederman goto drop; 994e730c155SEric W. Biederman 9951da177e4SLinus Torvalds /* Perform verifications before taking the lock. */ 9961da177e4SLinus Torvalds if (skb->pkt_type == PACKET_OTHERHOST) 9971da177e4SLinus Torvalds goto drop; 9981da177e4SLinus Torvalds 99951456b29SIan Morris skb = skb_share_check(skb, GFP_ATOMIC); 100051456b29SIan Morris if (!skb) 10011da177e4SLinus Torvalds return NET_RX_DROP; 10021da177e4SLinus Torvalds 10031da177e4SLinus Torvalds if (!pskb_may_pull(skb, 10041da177e4SLinus Torvalds sizeof(struct iphdr) + 10051da177e4SLinus Torvalds sizeof(struct udphdr))) 10061da177e4SLinus Torvalds goto drop; 10071da177e4SLinus Torvalds 1008eddc9ec5SArnaldo Carvalho de Melo b = (struct bootp_pkt *)skb_network_header(skb); 10091da177e4SLinus Torvalds h = &b->iph; 10101da177e4SLinus Torvalds 10111da177e4SLinus Torvalds if (h->ihl != 5 || h->version != 4 || h->protocol != IPPROTO_UDP) 10121da177e4SLinus Torvalds goto drop; 10131da177e4SLinus Torvalds 10141da177e4SLinus Torvalds /* Fragments are not supported */ 101556f8a75cSPaul Gortmaker if (ip_is_fragment(h)) { 1016e87cc472SJoe Perches net_err_ratelimited("DHCP/BOOTP: Ignoring fragmented reply\n"); 10171da177e4SLinus Torvalds goto drop; 10181da177e4SLinus Torvalds } 10191da177e4SLinus Torvalds 10201da177e4SLinus Torvalds if (skb->len < ntohs(h->tot_len)) 10211da177e4SLinus Torvalds goto drop; 10221da177e4SLinus Torvalds 10231da177e4SLinus Torvalds if (ip_fast_csum((char *) h, h->ihl)) 10241da177e4SLinus Torvalds goto drop; 10251da177e4SLinus Torvalds 10261da177e4SLinus Torvalds if (b->udph.source != htons(67) || b->udph.dest != htons(68)) 10271da177e4SLinus Torvalds goto drop; 10281da177e4SLinus Torvalds 10291da177e4SLinus Torvalds if (ntohs(h->tot_len) < ntohs(b->udph.len) + sizeof(struct iphdr)) 10301da177e4SLinus Torvalds goto drop; 10311da177e4SLinus Torvalds 10321da177e4SLinus Torvalds len = ntohs(b->udph.len) - sizeof(struct udphdr); 10331da177e4SLinus Torvalds ext_len = len - (sizeof(*b) - 10341da177e4SLinus Torvalds sizeof(struct iphdr) - 10351da177e4SLinus Torvalds sizeof(struct udphdr) - 10361da177e4SLinus Torvalds sizeof(b->exten)); 10371da177e4SLinus Torvalds if (ext_len < 0) 10381da177e4SLinus Torvalds goto drop; 10391da177e4SLinus Torvalds 10401da177e4SLinus Torvalds /* Ok the front looks good, make sure we can get at the rest. */ 10411da177e4SLinus Torvalds if (!pskb_may_pull(skb, skb->len)) 10421da177e4SLinus Torvalds goto drop; 10431da177e4SLinus Torvalds 1044eddc9ec5SArnaldo Carvalho de Melo b = (struct bootp_pkt *)skb_network_header(skb); 10451da177e4SLinus Torvalds h = &b->iph; 10461da177e4SLinus Torvalds 10471da177e4SLinus Torvalds /* One reply at a time, please. */ 10481da177e4SLinus Torvalds spin_lock(&ic_recv_lock); 10491da177e4SLinus Torvalds 10501da177e4SLinus Torvalds /* If we already have a reply, just drop the packet */ 10511da177e4SLinus Torvalds if (ic_got_reply) 10521da177e4SLinus Torvalds goto drop_unlock; 10531da177e4SLinus Torvalds 10541da177e4SLinus Torvalds /* Find the ic_device that the packet arrived on */ 10551da177e4SLinus Torvalds d = ic_first_dev; 10561da177e4SLinus Torvalds while (d && d->dev != dev) 10571da177e4SLinus Torvalds d = d->next; 10581da177e4SLinus Torvalds if (!d) 10591da177e4SLinus Torvalds goto drop_unlock; /* should never happen */ 10601da177e4SLinus Torvalds 10611da177e4SLinus Torvalds /* Is it a reply to our BOOTP request? */ 10621da177e4SLinus Torvalds if (b->op != BOOTP_REPLY || 10631da177e4SLinus Torvalds b->xid != d->xid) { 106422fc5388SUwe Kleine-König net_err_ratelimited("DHCP/BOOTP: Reply not for us on %s, op[%x] xid[%x]\n", 106522fc5388SUwe Kleine-König d->dev->name, b->op, b->xid); 10661da177e4SLinus Torvalds goto drop_unlock; 10671da177e4SLinus Torvalds } 10681da177e4SLinus Torvalds 10691da177e4SLinus Torvalds /* Parse extensions */ 10701da177e4SLinus Torvalds if (ext_len >= 4 && 10711da177e4SLinus Torvalds !memcmp(b->exten, ic_bootp_cookie, 4)) { /* Check magic cookie */ 10721da177e4SLinus Torvalds u8 *end = (u8 *) b + ntohs(b->iph.tot_len); 10731da177e4SLinus Torvalds u8 *ext; 10741da177e4SLinus Torvalds 10751da177e4SLinus Torvalds #ifdef IPCONFIG_DHCP 10761da177e4SLinus Torvalds if (ic_proto_enabled & IC_USE_DHCP) { 10775a874db4SAl Viro __be32 server_id = NONE; 10781da177e4SLinus Torvalds int mt = 0; 10791da177e4SLinus Torvalds 10801da177e4SLinus Torvalds ext = &b->exten[4]; 10811da177e4SLinus Torvalds while (ext < end && *ext != 0xff) { 10821da177e4SLinus Torvalds u8 *opt = ext++; 10831da177e4SLinus Torvalds if (*opt == 0) /* Padding */ 10841da177e4SLinus Torvalds continue; 10851da177e4SLinus Torvalds ext += *ext + 1; 10861da177e4SLinus Torvalds if (ext >= end) 10871da177e4SLinus Torvalds break; 10881da177e4SLinus Torvalds switch (*opt) { 10891da177e4SLinus Torvalds case 53: /* Message type */ 10901da177e4SLinus Torvalds if (opt[1]) 10911da177e4SLinus Torvalds mt = opt[2]; 10921da177e4SLinus Torvalds break; 10931da177e4SLinus Torvalds case 54: /* Server ID (IP address) */ 10941da177e4SLinus Torvalds if (opt[1] >= 4) 10951da177e4SLinus Torvalds memcpy(&server_id, opt + 2, 4); 10961da177e4SLinus Torvalds break; 10973ff50b79SStephen Hemminger } 10981da177e4SLinus Torvalds } 10991da177e4SLinus Torvalds 110022fc5388SUwe Kleine-König pr_debug("DHCP: Got message type %d (%s)\n", mt, d->dev->name); 11011da177e4SLinus Torvalds 11021da177e4SLinus Torvalds switch (mt) { 11031da177e4SLinus Torvalds case DHCPOFFER: 11041da177e4SLinus Torvalds /* While in the process of accepting one offer, 11051da177e4SLinus Torvalds * ignore all others. 11061da177e4SLinus Torvalds */ 11075a874db4SAl Viro if (ic_myaddr != NONE) 11081da177e4SLinus Torvalds goto drop_unlock; 11091da177e4SLinus Torvalds 11101da177e4SLinus Torvalds /* Let's accept that offer. */ 11111da177e4SLinus Torvalds ic_myaddr = b->your_ip; 11121da177e4SLinus Torvalds ic_servaddr = server_id; 111309605cc1SBastian Stender pr_debug("DHCP: Offered address %pI4 by server %pI4\n", 11149dd4a13aSPhilippe De Muyter &ic_myaddr, &b->iph.saddr); 11151da177e4SLinus Torvalds /* The DHCP indicated server address takes 11161da177e4SLinus Torvalds * precedence over the bootp header one if 11171da177e4SLinus Torvalds * they are different. 11181da177e4SLinus Torvalds */ 11195a874db4SAl Viro if ((server_id != NONE) && 11201da177e4SLinus Torvalds (b->server_ip != server_id)) 11211da177e4SLinus Torvalds b->server_ip = ic_servaddr; 11221da177e4SLinus Torvalds break; 11231da177e4SLinus Torvalds 11241da177e4SLinus Torvalds case DHCPACK: 11251da177e4SLinus Torvalds if (memcmp(dev->dev_addr, b->hw_addr, dev->addr_len) != 0) 11261da177e4SLinus Torvalds goto drop_unlock; 11271da177e4SLinus Torvalds 11281da177e4SLinus Torvalds /* Yeah! */ 11291da177e4SLinus Torvalds break; 11301da177e4SLinus Torvalds 11311da177e4SLinus Torvalds default: 11321da177e4SLinus Torvalds /* Urque. Forget it*/ 11335a874db4SAl Viro ic_myaddr = NONE; 11345a874db4SAl Viro ic_servaddr = NONE; 11351da177e4SLinus Torvalds goto drop_unlock; 11363ff50b79SStephen Hemminger } 11371da177e4SLinus Torvalds 11381da177e4SLinus Torvalds ic_dhcp_msgtype = mt; 11391da177e4SLinus Torvalds 11401da177e4SLinus Torvalds } 11411da177e4SLinus Torvalds #endif /* IPCONFIG_DHCP */ 11421da177e4SLinus Torvalds 11431da177e4SLinus Torvalds ext = &b->exten[4]; 11441da177e4SLinus Torvalds while (ext < end && *ext != 0xff) { 11451da177e4SLinus Torvalds u8 *opt = ext++; 11461da177e4SLinus Torvalds if (*opt == 0) /* Padding */ 11471da177e4SLinus Torvalds continue; 11481da177e4SLinus Torvalds ext += *ext + 1; 11491da177e4SLinus Torvalds if (ext < end) 11501da177e4SLinus Torvalds ic_do_bootp_ext(opt); 11511da177e4SLinus Torvalds } 11521da177e4SLinus Torvalds } 11531da177e4SLinus Torvalds 11541da177e4SLinus Torvalds /* We have a winner! */ 11552647cffbSUwe Kleine-König ic_dev = d; 11561da177e4SLinus Torvalds ic_myaddr = b->your_ip; 11571da177e4SLinus Torvalds ic_servaddr = b->server_ip; 11589dd4a13aSPhilippe De Muyter ic_addrservaddr = b->iph.saddr; 11595a874db4SAl Viro if (ic_gateway == NONE && b->relay_ip) 11601da177e4SLinus Torvalds ic_gateway = b->relay_ip; 11615a874db4SAl Viro if (ic_nameservers[0] == NONE) 11621da177e4SLinus Torvalds ic_nameservers[0] = ic_servaddr; 11631da177e4SLinus Torvalds ic_got_reply = IC_BOOTP; 11641da177e4SLinus Torvalds 11651da177e4SLinus Torvalds drop_unlock: 11661da177e4SLinus Torvalds /* Show's over. Nothing to see here. */ 11671da177e4SLinus Torvalds spin_unlock(&ic_recv_lock); 11681da177e4SLinus Torvalds 11691da177e4SLinus Torvalds drop: 11701da177e4SLinus Torvalds /* Throw the packet out. */ 11711da177e4SLinus Torvalds kfree_skb(skb); 11721da177e4SLinus Torvalds 11731da177e4SLinus Torvalds return 0; 11741da177e4SLinus Torvalds } 11751da177e4SLinus Torvalds 11761da177e4SLinus Torvalds 11771da177e4SLinus Torvalds #endif 11781da177e4SLinus Torvalds 11791da177e4SLinus Torvalds 11801da177e4SLinus Torvalds /* 11811da177e4SLinus Torvalds * Dynamic IP configuration -- DHCP, BOOTP, RARP. 11821da177e4SLinus Torvalds */ 11831da177e4SLinus Torvalds 11841da177e4SLinus Torvalds #ifdef IPCONFIG_DYNAMIC 11851da177e4SLinus Torvalds 11861da177e4SLinus Torvalds static int __init ic_dynamic(void) 11871da177e4SLinus Torvalds { 11881da177e4SLinus Torvalds int retries; 11891da177e4SLinus Torvalds struct ic_device *d; 11901da177e4SLinus Torvalds unsigned long start_jiffies, timeout, jiff; 11911da177e4SLinus Torvalds int do_bootp = ic_proto_have_if & IC_BOOTP; 11921da177e4SLinus Torvalds int do_rarp = ic_proto_have_if & IC_RARP; 11931da177e4SLinus Torvalds 11941da177e4SLinus Torvalds /* 11951da177e4SLinus Torvalds * If none of DHCP/BOOTP/RARP was selected, return with an error. 11961da177e4SLinus Torvalds * This routine gets only called when some pieces of information 11971da177e4SLinus Torvalds * are missing, and without DHCP/BOOTP/RARP we are unable to get it. 11981da177e4SLinus Torvalds */ 11991da177e4SLinus Torvalds if (!ic_proto_enabled) { 1200058bd4d2SJoe Perches pr_err("IP-Config: Incomplete network configuration information\n"); 12011da177e4SLinus Torvalds return -1; 12021da177e4SLinus Torvalds } 12031da177e4SLinus Torvalds 12041da177e4SLinus Torvalds #ifdef IPCONFIG_BOOTP 12051da177e4SLinus Torvalds if ((ic_proto_enabled ^ ic_proto_have_if) & IC_BOOTP) 1206058bd4d2SJoe Perches pr_err("DHCP/BOOTP: No suitable device found\n"); 12071da177e4SLinus Torvalds #endif 12081da177e4SLinus Torvalds #ifdef IPCONFIG_RARP 12091da177e4SLinus Torvalds if ((ic_proto_enabled ^ ic_proto_have_if) & IC_RARP) 1210058bd4d2SJoe Perches pr_err("RARP: No suitable device found\n"); 12111da177e4SLinus Torvalds #endif 12121da177e4SLinus Torvalds 12131da177e4SLinus Torvalds if (!ic_proto_have_if) 12141da177e4SLinus Torvalds /* Error message already printed */ 12151da177e4SLinus Torvalds return -1; 12161da177e4SLinus Torvalds 12171da177e4SLinus Torvalds /* 12181da177e4SLinus Torvalds * Setup protocols 12191da177e4SLinus Torvalds */ 12201da177e4SLinus Torvalds #ifdef IPCONFIG_BOOTP 12211da177e4SLinus Torvalds if (do_bootp) 12221da177e4SLinus Torvalds ic_bootp_init(); 12231da177e4SLinus Torvalds #endif 12241da177e4SLinus Torvalds #ifdef IPCONFIG_RARP 12251da177e4SLinus Torvalds if (do_rarp) 12261da177e4SLinus Torvalds ic_rarp_init(); 12271da177e4SLinus Torvalds #endif 12281da177e4SLinus Torvalds 12291da177e4SLinus Torvalds /* 12301da177e4SLinus Torvalds * Send requests and wait, until we get an answer. This loop 12311da177e4SLinus Torvalds * seems to be a terrible waste of CPU time, but actually there is 12321da177e4SLinus Torvalds * only one process running at all, so we don't need to use any 12331da177e4SLinus Torvalds * scheduler functions. 12341da177e4SLinus Torvalds * [Actually we could now, but the nothing else running note still 12351da177e4SLinus Torvalds * applies.. - AC] 12361da177e4SLinus Torvalds */ 1237058bd4d2SJoe Perches pr_notice("Sending %s%s%s requests .", 12381da177e4SLinus Torvalds do_bootp 12391da177e4SLinus Torvalds ? ((ic_proto_enabled & IC_USE_DHCP) ? "DHCP" : "BOOTP") : "", 12401da177e4SLinus Torvalds (do_bootp && do_rarp) ? " and " : "", 12411da177e4SLinus Torvalds do_rarp ? "RARP" : ""); 12421da177e4SLinus Torvalds 12431da177e4SLinus Torvalds start_jiffies = jiffies; 12441da177e4SLinus Torvalds d = ic_first_dev; 12451da177e4SLinus Torvalds retries = CONF_SEND_RETRIES; 12461da177e4SLinus Torvalds get_random_bytes(&timeout, sizeof(timeout)); 124795c96174SEric Dumazet timeout = CONF_BASE_TIMEOUT + (timeout % (unsigned int) CONF_TIMEOUT_RANDOM); 12481da177e4SLinus Torvalds for (;;) { 1249405fd707SDavid S. Miller #ifdef IPCONFIG_BOOTP 12501da177e4SLinus Torvalds if (do_bootp && (d->able & IC_BOOTP)) 12511da177e4SLinus Torvalds ic_bootp_send_if(d, jiffies - start_jiffies); 12521da177e4SLinus Torvalds #endif 12531da177e4SLinus Torvalds #ifdef IPCONFIG_RARP 12541da177e4SLinus Torvalds if (do_rarp && (d->able & IC_RARP)) 12551da177e4SLinus Torvalds ic_rarp_send_if(d); 12561da177e4SLinus Torvalds #endif 12571da177e4SLinus Torvalds 1258e0688534SUwe Kleine-König if (!d->next) { 1259e0688534SUwe Kleine-König jiff = jiffies + timeout; 1260121caf57SNishanth Aravamudan while (time_before(jiffies, jiff) && !ic_got_reply) 1261121caf57SNishanth Aravamudan schedule_timeout_uninterruptible(1); 1262e0688534SUwe Kleine-König } 12631da177e4SLinus Torvalds #ifdef IPCONFIG_DHCP 12641da177e4SLinus Torvalds /* DHCP isn't done until we get a DHCPACK. */ 12659d4fb27dSJoe Perches if ((ic_got_reply & IC_BOOTP) && 12669d4fb27dSJoe Perches (ic_proto_enabled & IC_USE_DHCP) && 12679d4fb27dSJoe Perches ic_dhcp_msgtype != DHCPACK) { 12681da177e4SLinus Torvalds ic_got_reply = 0; 12692647cffbSUwe Kleine-König /* continue on device that got the reply */ 12702647cffbSUwe Kleine-König d = ic_dev; 12716c1c36b0SGeert Uytterhoeven pr_cont(","); 12721da177e4SLinus Torvalds continue; 12731da177e4SLinus Torvalds } 12741da177e4SLinus Torvalds #endif /* IPCONFIG_DHCP */ 12751da177e4SLinus Torvalds 12761da177e4SLinus Torvalds if (ic_got_reply) { 12776c1c36b0SGeert Uytterhoeven pr_cont(" OK\n"); 12781da177e4SLinus Torvalds break; 12791da177e4SLinus Torvalds } 12801da177e4SLinus Torvalds 12811da177e4SLinus Torvalds if ((d = d->next)) 12821da177e4SLinus Torvalds continue; 12831da177e4SLinus Torvalds 12841da177e4SLinus Torvalds if (! --retries) { 12856c1c36b0SGeert Uytterhoeven pr_cont(" timed out!\n"); 12861da177e4SLinus Torvalds break; 12871da177e4SLinus Torvalds } 12881da177e4SLinus Torvalds 12891da177e4SLinus Torvalds d = ic_first_dev; 12901da177e4SLinus Torvalds 12911da177e4SLinus Torvalds timeout = timeout CONF_TIMEOUT_MULT; 12921da177e4SLinus Torvalds if (timeout > CONF_TIMEOUT_MAX) 12931da177e4SLinus Torvalds timeout = CONF_TIMEOUT_MAX; 12941da177e4SLinus Torvalds 12956c1c36b0SGeert Uytterhoeven pr_cont("."); 12961da177e4SLinus Torvalds } 12971da177e4SLinus Torvalds 12981da177e4SLinus Torvalds #ifdef IPCONFIG_BOOTP 12991da177e4SLinus Torvalds if (do_bootp) 13001da177e4SLinus Torvalds ic_bootp_cleanup(); 13011da177e4SLinus Torvalds #endif 13021da177e4SLinus Torvalds #ifdef IPCONFIG_RARP 13031da177e4SLinus Torvalds if (do_rarp) 13041da177e4SLinus Torvalds ic_rarp_cleanup(); 13051da177e4SLinus Torvalds #endif 13061da177e4SLinus Torvalds 13077a1af5d7SMaxime Bizon if (!ic_got_reply) { 13085a874db4SAl Viro ic_myaddr = NONE; 13091da177e4SLinus Torvalds return -1; 13107a1af5d7SMaxime Bizon } 13111da177e4SLinus Torvalds 13126c1c36b0SGeert Uytterhoeven pr_info("IP-Config: Got %s answer from %pI4, my address is %pI4\n", 13131da177e4SLinus Torvalds ((ic_got_reply & IC_RARP) ? "RARP" 13141da177e4SLinus Torvalds : (ic_proto_enabled & IC_USE_DHCP) ? "DHCP" : "BOOTP"), 13156c1c36b0SGeert Uytterhoeven &ic_addrservaddr, &ic_myaddr); 13161da177e4SLinus Torvalds 13171da177e4SLinus Torvalds return 0; 13181da177e4SLinus Torvalds } 13191da177e4SLinus Torvalds 13201da177e4SLinus Torvalds #endif /* IPCONFIG_DYNAMIC */ 13211da177e4SLinus Torvalds 13221da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 1323289e1f4eSAnders Roxell /* proc_dir_entry for /proc/net/ipconfig */ 1324289e1f4eSAnders Roxell static struct proc_dir_entry *ipconfig_dir; 13251da177e4SLinus Torvalds 1326c04d2cb2SChris Novakovic /* Name servers: */ 13271da177e4SLinus Torvalds static int pnp_seq_show(struct seq_file *seq, void *v) 13281da177e4SLinus Torvalds { 13291da177e4SLinus Torvalds int i; 13301da177e4SLinus Torvalds 13311da177e4SLinus Torvalds if (ic_proto_used & IC_PROTO) 13321da177e4SLinus Torvalds seq_printf(seq, "#PROTO: %s\n", 13331da177e4SLinus Torvalds (ic_proto_used & IC_RARP) ? "RARP" 13341da177e4SLinus Torvalds : (ic_proto_used & IC_USE_DHCP) ? "DHCP" : "BOOTP"); 13351da177e4SLinus Torvalds else 13361da177e4SLinus Torvalds seq_puts(seq, "#MANUAL\n"); 13371da177e4SLinus Torvalds 13381da177e4SLinus Torvalds if (ic_domain[0]) 13391da177e4SLinus Torvalds seq_printf(seq, 13401da177e4SLinus Torvalds "domain %s\n", ic_domain); 13411da177e4SLinus Torvalds for (i = 0; i < CONF_NAMESERVERS_MAX; i++) { 13425a874db4SAl Viro if (ic_nameservers[i] != NONE) 1343673d57e7SHarvey Harrison seq_printf(seq, "nameserver %pI4\n", 1344673d57e7SHarvey Harrison &ic_nameservers[i]); 13451da177e4SLinus Torvalds } 13465a874db4SAl Viro if (ic_servaddr != NONE) 1347673d57e7SHarvey Harrison seq_printf(seq, "bootserver %pI4\n", 1348673d57e7SHarvey Harrison &ic_servaddr); 13491da177e4SLinus Torvalds return 0; 13501da177e4SLinus Torvalds } 13511da177e4SLinus Torvalds 13524d019b3fSChris Novakovic /* Create the /proc/net/ipconfig directory */ 1353c04d2cb2SChris Novakovic static int __init ipconfig_proc_net_init(void) 13544d019b3fSChris Novakovic { 13554d019b3fSChris Novakovic ipconfig_dir = proc_net_mkdir(&init_net, "ipconfig", init_net.proc_net); 13564d019b3fSChris Novakovic if (!ipconfig_dir) 13574d019b3fSChris Novakovic return -ENOMEM; 13584d019b3fSChris Novakovic 13594d019b3fSChris Novakovic return 0; 13604d019b3fSChris Novakovic } 1361c04d2cb2SChris Novakovic 1362c04d2cb2SChris Novakovic /* Create a new file under /proc/net/ipconfig */ 1363c04d2cb2SChris Novakovic static int ipconfig_proc_net_create(const char *name, 136497a32539SAlexey Dobriyan const struct proc_ops *proc_ops) 1365c04d2cb2SChris Novakovic { 1366c04d2cb2SChris Novakovic char *pname; 1367c04d2cb2SChris Novakovic struct proc_dir_entry *p; 1368c04d2cb2SChris Novakovic 1369c04d2cb2SChris Novakovic if (!ipconfig_dir) 1370c04d2cb2SChris Novakovic return -ENOMEM; 1371c04d2cb2SChris Novakovic 1372c04d2cb2SChris Novakovic pname = kasprintf(GFP_KERNEL, "%s%s", "ipconfig/", name); 1373c04d2cb2SChris Novakovic if (!pname) 1374c04d2cb2SChris Novakovic return -ENOMEM; 1375c04d2cb2SChris Novakovic 137697a32539SAlexey Dobriyan p = proc_create(pname, 0444, init_net.proc_net, proc_ops); 1377c04d2cb2SChris Novakovic kfree(pname); 1378c04d2cb2SChris Novakovic if (!p) 1379c04d2cb2SChris Novakovic return -ENOMEM; 1380c04d2cb2SChris Novakovic 1381c04d2cb2SChris Novakovic return 0; 1382c04d2cb2SChris Novakovic } 1383c04d2cb2SChris Novakovic 1384c04d2cb2SChris Novakovic /* Write NTP server IP addresses to /proc/net/ipconfig/ntp_servers */ 138597a32539SAlexey Dobriyan static int ntp_servers_show(struct seq_file *seq, void *v) 1386c04d2cb2SChris Novakovic { 1387c04d2cb2SChris Novakovic int i; 1388c04d2cb2SChris Novakovic 1389c04d2cb2SChris Novakovic for (i = 0; i < CONF_NTP_SERVERS_MAX; i++) { 1390c04d2cb2SChris Novakovic if (ic_ntp_servers[i] != NONE) 1391c04d2cb2SChris Novakovic seq_printf(seq, "%pI4\n", &ic_ntp_servers[i]); 1392c04d2cb2SChris Novakovic } 1393c04d2cb2SChris Novakovic return 0; 1394c04d2cb2SChris Novakovic } 139597a32539SAlexey Dobriyan DEFINE_PROC_SHOW_ATTRIBUTE(ntp_servers); 13961da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */ 13971da177e4SLinus Torvalds 13981da177e4SLinus Torvalds /* 13991da177e4SLinus Torvalds * Extract IP address from the parameter string if needed. Note that we 14001da177e4SLinus Torvalds * need to have root_server_addr set _before_ IPConfig gets called as it 14011da177e4SLinus Torvalds * can override it. 14021da177e4SLinus Torvalds */ 14035a874db4SAl Viro __be32 __init root_nfs_parse_addr(char *name) 14041da177e4SLinus Torvalds { 14055a874db4SAl Viro __be32 addr; 14061da177e4SLinus Torvalds int octets = 0; 14071da177e4SLinus Torvalds char *cp, *cq; 14081da177e4SLinus Torvalds 14091da177e4SLinus Torvalds cp = cq = name; 14101da177e4SLinus Torvalds while (octets < 4) { 14111da177e4SLinus Torvalds while (*cp >= '0' && *cp <= '9') 14121da177e4SLinus Torvalds cp++; 14131da177e4SLinus Torvalds if (cp == cq || cp - cq > 3) 14141da177e4SLinus Torvalds break; 14151da177e4SLinus Torvalds if (*cp == '.' || octets == 3) 14161da177e4SLinus Torvalds octets++; 14171da177e4SLinus Torvalds if (octets < 4) 14181da177e4SLinus Torvalds cp++; 14191da177e4SLinus Torvalds cq = cp; 14201da177e4SLinus Torvalds } 14211da177e4SLinus Torvalds if (octets == 4 && (*cp == ':' || *cp == '\0')) { 14221da177e4SLinus Torvalds if (*cp == ':') 14231da177e4SLinus Torvalds *cp++ = '\0'; 14241da177e4SLinus Torvalds addr = in_aton(name); 14251da177e4SLinus Torvalds memmove(name, cp, strlen(cp) + 1); 14261da177e4SLinus Torvalds } else 14275a874db4SAl Viro addr = NONE; 14281da177e4SLinus Torvalds 14291da177e4SLinus Torvalds return addr; 14301da177e4SLinus Torvalds } 14311da177e4SLinus Torvalds 1432964ad81cSDavid S. Miller #define DEVICE_WAIT_MAX 12 /* 12 seconds */ 1433964ad81cSDavid S. Miller 1434964ad81cSDavid S. Miller static int __init wait_for_devices(void) 1435964ad81cSDavid S. Miller { 1436964ad81cSDavid S. Miller int i; 1437*dd429036SSaravana Kannan bool try_init_devs = true; 1438964ad81cSDavid S. Miller 1439964ad81cSDavid S. Miller for (i = 0; i < DEVICE_WAIT_MAX; i++) { 1440964ad81cSDavid S. Miller struct net_device *dev; 1441964ad81cSDavid S. Miller int found = 0; 1442964ad81cSDavid S. Miller 1443e2ffe3ffSThomas Bogendoerfer /* make sure deferred device probes are finished */ 1444e2ffe3ffSThomas Bogendoerfer wait_for_device_probe(); 1445e2ffe3ffSThomas Bogendoerfer 1446964ad81cSDavid S. Miller rtnl_lock(); 1447964ad81cSDavid S. Miller for_each_netdev(&init_net, dev) { 14483fb72f1eSMicha Nelissen if (ic_is_init_dev(dev)) { 1449964ad81cSDavid S. Miller found = 1; 1450964ad81cSDavid S. Miller break; 1451964ad81cSDavid S. Miller } 1452964ad81cSDavid S. Miller } 1453964ad81cSDavid S. Miller rtnl_unlock(); 1454964ad81cSDavid S. Miller if (found) 1455964ad81cSDavid S. Miller return 0; 1456*dd429036SSaravana Kannan if (try_init_devs && 1457*dd429036SSaravana Kannan (ROOT_DEV == Root_NFS || ROOT_DEV == Root_CIFS)) { 1458*dd429036SSaravana Kannan try_init_devs = false; 1459*dd429036SSaravana Kannan wait_for_init_devices_probe(); 1460*dd429036SSaravana Kannan } 1461964ad81cSDavid S. Miller ssleep(1); 1462964ad81cSDavid S. Miller } 1463964ad81cSDavid S. Miller return -ENODEV; 1464964ad81cSDavid S. Miller } 1465964ad81cSDavid S. Miller 14661da177e4SLinus Torvalds /* 14671da177e4SLinus Torvalds * IP Autoconfig dispatcher. 14681da177e4SLinus Torvalds */ 14691da177e4SLinus Torvalds 14701da177e4SLinus Torvalds static int __init ip_auto_config(void) 14711da177e4SLinus Torvalds { 14725a874db4SAl Viro __be32 addr; 14739d8dba6cSBenjamin Zores #ifdef IPCONFIG_DYNAMIC 14749d8dba6cSBenjamin Zores int retries = CONF_OPEN_RETRIES; 14759d8dba6cSBenjamin Zores #endif 1476964ad81cSDavid S. Miller int err; 1477c9f64d1fSThierry Reding unsigned int i, count; 14781da177e4SLinus Torvalds 1479c04d2cb2SChris Novakovic /* Initialise all name servers and NTP servers to NONE (but only if the 1480c04d2cb2SChris Novakovic * "ip=" or "nfsaddrs=" kernel command line parameters weren't decoded, 1481c04d2cb2SChris Novakovic * otherwise we'll overwrite the IP addresses specified there) 1482300eec7cSChris Novakovic */ 1483c04d2cb2SChris Novakovic if (ic_set_manually == 0) { 1484300eec7cSChris Novakovic ic_nameservers_predef(); 1485c04d2cb2SChris Novakovic ic_ntp_servers_predef(); 1486c04d2cb2SChris Novakovic } 1487300eec7cSChris Novakovic 14881da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 14893f3942acSChristoph Hellwig proc_create_single("pnp", 0444, init_net.proc_net, pnp_seq_show); 14904d019b3fSChris Novakovic 1491c04d2cb2SChris Novakovic if (ipconfig_proc_net_init() == 0) 149297a32539SAlexey Dobriyan ipconfig_proc_net_create("ntp_servers", &ntp_servers_proc_ops); 14931da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */ 14941da177e4SLinus Torvalds 14951da177e4SLinus Torvalds if (!ic_enable) 14961da177e4SLinus Torvalds return 0; 14971da177e4SLinus Torvalds 149809605cc1SBastian Stender pr_debug("IP-Config: Entered.\n"); 14991da177e4SLinus Torvalds #ifdef IPCONFIG_DYNAMIC 15001da177e4SLinus Torvalds try_try_again: 15011da177e4SLinus Torvalds #endif 1502964ad81cSDavid S. Miller /* Wait for devices to appear */ 1503964ad81cSDavid S. Miller err = wait_for_devices(); 1504964ad81cSDavid S. Miller if (err) 1505964ad81cSDavid S. Miller return err; 15061da177e4SLinus Torvalds 15071da177e4SLinus Torvalds /* Setup all network devices */ 1508964ad81cSDavid S. Miller err = ic_open_devs(); 1509964ad81cSDavid S. Miller if (err) 1510964ad81cSDavid S. Miller return err; 15111da177e4SLinus Torvalds 15121da177e4SLinus Torvalds /* Give drivers a chance to settle */ 15133fb72f1eSMicha Nelissen msleep(CONF_POST_OPEN); 15141da177e4SLinus Torvalds 15151da177e4SLinus Torvalds /* 15161da177e4SLinus Torvalds * If the config information is insufficient (e.g., our IP address or 15171da177e4SLinus Torvalds * IP address of the boot server is missing or we have multiple network 15181da177e4SLinus Torvalds * interfaces and no default was set), use BOOTP or RARP to get the 15191da177e4SLinus Torvalds * missing values. 15201da177e4SLinus Torvalds */ 15215a874db4SAl Viro if (ic_myaddr == NONE || 152251976f47SPaulo Alcantara (SUSE) #if defined(CONFIG_ROOT_NFS) || defined(CONFIG_CIFS_ROOT) 15239d4fb27dSJoe Perches (root_server_addr == NONE && 15249d4fb27dSJoe Perches ic_servaddr == NONE && 152551976f47SPaulo Alcantara (SUSE) (ROOT_DEV == Root_NFS || ROOT_DEV == Root_CIFS)) || 15261da177e4SLinus Torvalds #endif 15271da177e4SLinus Torvalds ic_first_dev->next) { 15281da177e4SLinus Torvalds #ifdef IPCONFIG_DYNAMIC 15291da177e4SLinus Torvalds if (ic_dynamic() < 0) { 15301da177e4SLinus Torvalds ic_close_devs(); 15311da177e4SLinus Torvalds 15321da177e4SLinus Torvalds /* 15331da177e4SLinus Torvalds * I don't know why, but sometimes the 15341da177e4SLinus Torvalds * eepro100 driver (at least) gets upset and 15351da177e4SLinus Torvalds * doesn't work the first time it's opened. 15361da177e4SLinus Torvalds * But then if you close it and reopen it, it 15371da177e4SLinus Torvalds * works just fine. So we need to try that at 15381da177e4SLinus Torvalds * least once before giving up. 15391da177e4SLinus Torvalds * 15401da177e4SLinus Torvalds * Also, if the root will be NFS-mounted, we 15411da177e4SLinus Torvalds * have nowhere to go if DHCP fails. So we 15421da177e4SLinus Torvalds * just have to keep trying forever. 15431da177e4SLinus Torvalds * 15441da177e4SLinus Torvalds * -- Chip 15451da177e4SLinus Torvalds */ 15461da177e4SLinus Torvalds #ifdef CONFIG_ROOT_NFS 15471da177e4SLinus Torvalds if (ROOT_DEV == Root_NFS) { 1548058bd4d2SJoe Perches pr_err("IP-Config: Retrying forever (NFS root)...\n"); 15491da177e4SLinus Torvalds goto try_try_again; 15501da177e4SLinus Torvalds } 15511da177e4SLinus Torvalds #endif 155251976f47SPaulo Alcantara (SUSE) #ifdef CONFIG_CIFS_ROOT 155351976f47SPaulo Alcantara (SUSE) if (ROOT_DEV == Root_CIFS) { 155451976f47SPaulo Alcantara (SUSE) pr_err("IP-Config: Retrying forever (CIFS root)...\n"); 155551976f47SPaulo Alcantara (SUSE) goto try_try_again; 155651976f47SPaulo Alcantara (SUSE) } 155751976f47SPaulo Alcantara (SUSE) #endif 15581da177e4SLinus Torvalds 15591da177e4SLinus Torvalds if (--retries) { 1560058bd4d2SJoe Perches pr_err("IP-Config: Reopening network devices...\n"); 15611da177e4SLinus Torvalds goto try_try_again; 15621da177e4SLinus Torvalds } 15631da177e4SLinus Torvalds 15641da177e4SLinus Torvalds /* Oh, well. At least we tried. */ 1565058bd4d2SJoe Perches pr_err("IP-Config: Auto-configuration of network failed\n"); 15661da177e4SLinus Torvalds return -1; 15671da177e4SLinus Torvalds } 15681da177e4SLinus Torvalds #else /* !DYNAMIC */ 1569058bd4d2SJoe Perches pr_err("IP-Config: Incomplete network configuration information\n"); 15701da177e4SLinus Torvalds ic_close_devs(); 15711da177e4SLinus Torvalds return -1; 15721da177e4SLinus Torvalds #endif /* IPCONFIG_DYNAMIC */ 15731da177e4SLinus Torvalds } else { 15741da177e4SLinus Torvalds /* Device selected manually or only one device -> use it */ 15752647cffbSUwe Kleine-König ic_dev = ic_first_dev; 15761da177e4SLinus Torvalds } 15771da177e4SLinus Torvalds 15781da177e4SLinus Torvalds addr = root_nfs_parse_addr(root_server_path); 15795a874db4SAl Viro if (root_server_addr == NONE) 15801da177e4SLinus Torvalds root_server_addr = addr; 15811da177e4SLinus Torvalds 15821da177e4SLinus Torvalds /* 158325985edcSLucas De Marchi * Use defaults wherever applicable. 15841da177e4SLinus Torvalds */ 15851da177e4SLinus Torvalds if (ic_defaults() < 0) 15861da177e4SLinus Torvalds return -1; 15871da177e4SLinus Torvalds 15881da177e4SLinus Torvalds /* 15891da177e4SLinus Torvalds * Record which protocol was actually used. 15901da177e4SLinus Torvalds */ 15911da177e4SLinus Torvalds #ifdef IPCONFIG_DYNAMIC 15921da177e4SLinus Torvalds ic_proto_used = ic_got_reply | (ic_proto_enabled & IC_USE_DHCP); 15931da177e4SLinus Torvalds #endif 15941da177e4SLinus Torvalds 15951da177e4SLinus Torvalds #ifndef IPCONFIG_SILENT 15961da177e4SLinus Torvalds /* 15971da177e4SLinus Torvalds * Clue in the operator. 15981da177e4SLinus Torvalds */ 1599058bd4d2SJoe Perches pr_info("IP-Config: Complete:\n"); 16009ecd1c3dSClaudio Fontana 16019ecd1c3dSClaudio Fontana pr_info(" device=%s, hwaddr=%*phC, ipaddr=%pI4, mask=%pI4, gw=%pI4\n", 16022647cffbSUwe Kleine-König ic_dev->dev->name, ic_dev->dev->addr_len, ic_dev->dev->dev_addr, 16039ecd1c3dSClaudio Fontana &ic_myaddr, &ic_netmask, &ic_gateway); 1604058bd4d2SJoe Perches pr_info(" host=%s, domain=%s, nis-domain=%s\n", 1605e9ff3990SSerge E. Hallyn utsname()->nodename, ic_domain, utsname()->domainname); 1606058bd4d2SJoe Perches pr_info(" bootserver=%pI4, rootserver=%pI4, rootpath=%s", 1607058bd4d2SJoe Perches &ic_servaddr, &root_server_addr, root_server_path); 16089643f455SChris Friesen if (ic_dev_mtu) 1609058bd4d2SJoe Perches pr_cont(", mtu=%d", ic_dev_mtu); 1610e18bdc83SChris Novakovic /* Name servers (if any): */ 1611c9f64d1fSThierry Reding for (i = 0, count = 0; i < CONF_NAMESERVERS_MAX; i++) { 16125e953778SChristoph Fritz if (ic_nameservers[i] != NONE) { 1613e18bdc83SChris Novakovic if (i == 0) 1614e18bdc83SChris Novakovic pr_info(" nameserver%u=%pI4", 16155e953778SChristoph Fritz i, &ic_nameservers[i]); 1616e18bdc83SChris Novakovic else 1617e18bdc83SChris Novakovic pr_cont(", nameserver%u=%pI4", 1618e18bdc83SChris Novakovic i, &ic_nameservers[i]); 1619c9f64d1fSThierry Reding 1620c9f64d1fSThierry Reding count++; 16215e953778SChristoph Fritz } 1622c9f64d1fSThierry Reding if ((i + 1 == CONF_NAMESERVERS_MAX) && count > 0) 16236c1c36b0SGeert Uytterhoeven pr_cont("\n"); 1624e18bdc83SChris Novakovic } 1625c04d2cb2SChris Novakovic /* NTP servers (if any): */ 1626c9f64d1fSThierry Reding for (i = 0, count = 0; i < CONF_NTP_SERVERS_MAX; i++) { 1627c04d2cb2SChris Novakovic if (ic_ntp_servers[i] != NONE) { 1628c04d2cb2SChris Novakovic if (i == 0) 1629c04d2cb2SChris Novakovic pr_info(" ntpserver%u=%pI4", 1630c04d2cb2SChris Novakovic i, &ic_ntp_servers[i]); 1631c04d2cb2SChris Novakovic else 1632c04d2cb2SChris Novakovic pr_cont(", ntpserver%u=%pI4", 1633c04d2cb2SChris Novakovic i, &ic_ntp_servers[i]); 1634c9f64d1fSThierry Reding 1635c9f64d1fSThierry Reding count++; 1636c04d2cb2SChris Novakovic } 1637c9f64d1fSThierry Reding if ((i + 1 == CONF_NTP_SERVERS_MAX) && count > 0) 1638c04d2cb2SChris Novakovic pr_cont("\n"); 1639c04d2cb2SChris Novakovic } 16401da177e4SLinus Torvalds #endif /* !SILENT */ 16411da177e4SLinus Torvalds 16429c706a49SUwe Kleine-König /* 16439c706a49SUwe Kleine-König * Close all network devices except the device we've 16449c706a49SUwe Kleine-König * autoconfigured and set up routes. 16459c706a49SUwe Kleine-König */ 16469c706a49SUwe Kleine-König if (ic_setup_if() < 0 || ic_setup_routes() < 0) 1647d2d371aeSThierry Reding err = -1; 1648d2d371aeSThierry Reding else 1649d2d371aeSThierry Reding err = 0; 16509c706a49SUwe Kleine-König 1651d2d371aeSThierry Reding ic_close_devs(); 16529c706a49SUwe Kleine-König 1653d2d371aeSThierry Reding return err; 16541da177e4SLinus Torvalds } 16551da177e4SLinus Torvalds 16561da177e4SLinus Torvalds late_initcall(ip_auto_config); 16571da177e4SLinus Torvalds 16581da177e4SLinus Torvalds 16591da177e4SLinus Torvalds /* 16601da177e4SLinus Torvalds * Decode any IP configuration options in the "ip=" or "nfsaddrs=" kernel 16613eb30c51SNiklas Söderlund * command line parameter. See Documentation/admin-guide/nfs/nfsroot.rst. 16621da177e4SLinus Torvalds */ 16631da177e4SLinus Torvalds static int __init ic_proto_name(char *name) 16641da177e4SLinus Torvalds { 16651da177e4SLinus Torvalds if (!strcmp(name, "on") || !strcmp(name, "any")) { 16661da177e4SLinus Torvalds return 1; 16671da177e4SLinus Torvalds } 1668a6c05c3dSSimon Horman if (!strcmp(name, "off") || !strcmp(name, "none")) { 166992ffb85dSAmos Waterland return 0; 1670a6c05c3dSSimon Horman } 16711da177e4SLinus Torvalds #ifdef CONFIG_IP_PNP_DHCP 167226fb342cSLi RongQing else if (!strncmp(name, "dhcp", 4)) { 167326fb342cSLi RongQing char *client_id; 167426fb342cSLi RongQing 16751da177e4SLinus Torvalds ic_proto_enabled &= ~IC_RARP; 167626fb342cSLi RongQing client_id = strstr(name, "dhcp,"); 167726fb342cSLi RongQing if (client_id) { 167826fb342cSLi RongQing char *v; 167926fb342cSLi RongQing 168026fb342cSLi RongQing client_id = client_id + 5; 168126fb342cSLi RongQing v = strchr(client_id, ','); 168226fb342cSLi RongQing if (!v) 168326fb342cSLi RongQing return 1; 168426fb342cSLi RongQing *v = 0; 168526fb342cSLi RongQing if (kstrtou8(client_id, 0, dhcp_client_identifier)) 168609605cc1SBastian Stender pr_debug("DHCP: Invalid client identifier type\n"); 168726fb342cSLi RongQing strncpy(dhcp_client_identifier + 1, v + 1, 251); 168826fb342cSLi RongQing *v = ','; 168926fb342cSLi RongQing } 16901da177e4SLinus Torvalds return 1; 16911da177e4SLinus Torvalds } 16921da177e4SLinus Torvalds #endif 16931da177e4SLinus Torvalds #ifdef CONFIG_IP_PNP_BOOTP 16941da177e4SLinus Torvalds else if (!strcmp(name, "bootp")) { 16951da177e4SLinus Torvalds ic_proto_enabled &= ~(IC_RARP | IC_USE_DHCP); 16961da177e4SLinus Torvalds return 1; 16971da177e4SLinus Torvalds } 16981da177e4SLinus Torvalds #endif 16991da177e4SLinus Torvalds #ifdef CONFIG_IP_PNP_RARP 17001da177e4SLinus Torvalds else if (!strcmp(name, "rarp")) { 17011da177e4SLinus Torvalds ic_proto_enabled &= ~(IC_BOOTP | IC_USE_DHCP); 17021da177e4SLinus Torvalds return 1; 17031da177e4SLinus Torvalds } 17041da177e4SLinus Torvalds #endif 17051da177e4SLinus Torvalds #ifdef IPCONFIG_DYNAMIC 17061da177e4SLinus Torvalds else if (!strcmp(name, "both")) { 17071da177e4SLinus Torvalds ic_proto_enabled &= ~IC_USE_DHCP; /* backward compat :-( */ 17081da177e4SLinus Torvalds return 1; 17091da177e4SLinus Torvalds } 17101da177e4SLinus Torvalds #endif 17111da177e4SLinus Torvalds return 0; 17121da177e4SLinus Torvalds } 17131da177e4SLinus Torvalds 17141da177e4SLinus Torvalds static int __init ip_auto_config_setup(char *addrs) 17151da177e4SLinus Torvalds { 17161da177e4SLinus Torvalds char *cp, *ip, *dp; 17171da177e4SLinus Torvalds int num = 0; 17181da177e4SLinus Torvalds 17191da177e4SLinus Torvalds ic_set_manually = 1; 17209cecd07cSSimon Horman ic_enable = 1; 17211da177e4SLinus Torvalds 172292ffb85dSAmos Waterland /* 172392ffb85dSAmos Waterland * If any dhcp, bootp etc options are set, leave autoconfig on 172492ffb85dSAmos Waterland * and skip the below static IP processing. 172592ffb85dSAmos Waterland */ 17261da177e4SLinus Torvalds if (ic_proto_name(addrs)) 17271da177e4SLinus Torvalds return 1; 17281da177e4SLinus Torvalds 172992ffb85dSAmos Waterland /* If no static IP is given, turn off autoconfig and bail. */ 173092ffb85dSAmos Waterland if (*addrs == 0 || 173192ffb85dSAmos Waterland strcmp(addrs, "off") == 0 || 173292ffb85dSAmos Waterland strcmp(addrs, "none") == 0) { 173392ffb85dSAmos Waterland ic_enable = 0; 173492ffb85dSAmos Waterland return 1; 173592ffb85dSAmos Waterland } 173692ffb85dSAmos Waterland 1737c04d2cb2SChris Novakovic /* Initialise all name servers and NTP servers to NONE */ 17385e953778SChristoph Fritz ic_nameservers_predef(); 1739c04d2cb2SChris Novakovic ic_ntp_servers_predef(); 17405e953778SChristoph Fritz 174192ffb85dSAmos Waterland /* Parse string for static IP assignment. */ 17421da177e4SLinus Torvalds ip = addrs; 17431da177e4SLinus Torvalds while (ip && *ip) { 17441da177e4SLinus Torvalds if ((cp = strchr(ip, ':'))) 17451da177e4SLinus Torvalds *cp++ = '\0'; 17461da177e4SLinus Torvalds if (strlen(ip) > 0) { 174709605cc1SBastian Stender pr_debug("IP-Config: Parameter #%d: `%s'\n", num, ip); 17481da177e4SLinus Torvalds switch (num) { 17491da177e4SLinus Torvalds case 0: 1750e6f1cebfSAl Viro if ((ic_myaddr = in_aton(ip)) == ANY) 17515a874db4SAl Viro ic_myaddr = NONE; 17521da177e4SLinus Torvalds break; 17531da177e4SLinus Torvalds case 1: 1754e6f1cebfSAl Viro if ((ic_servaddr = in_aton(ip)) == ANY) 17555a874db4SAl Viro ic_servaddr = NONE; 17561da177e4SLinus Torvalds break; 17571da177e4SLinus Torvalds case 2: 1758e6f1cebfSAl Viro if ((ic_gateway = in_aton(ip)) == ANY) 17595a874db4SAl Viro ic_gateway = NONE; 17601da177e4SLinus Torvalds break; 17611da177e4SLinus Torvalds case 3: 1762e6f1cebfSAl Viro if ((ic_netmask = in_aton(ip)) == ANY) 17635a874db4SAl Viro ic_netmask = NONE; 17641da177e4SLinus Torvalds break; 17651da177e4SLinus Torvalds case 4: 17661da177e4SLinus Torvalds if ((dp = strchr(ip, '.'))) { 17671da177e4SLinus Torvalds *dp++ = '\0'; 1768e9ff3990SSerge E. Hallyn strlcpy(utsname()->domainname, dp, 1769e9ff3990SSerge E. Hallyn sizeof(utsname()->domainname)); 17701da177e4SLinus Torvalds } 1771e9ff3990SSerge E. Hallyn strlcpy(utsname()->nodename, ip, 1772e9ff3990SSerge E. Hallyn sizeof(utsname()->nodename)); 17731da177e4SLinus Torvalds ic_host_name_set = 1; 17741da177e4SLinus Torvalds break; 17751da177e4SLinus Torvalds case 5: 17761da177e4SLinus Torvalds strlcpy(user_dev_name, ip, sizeof(user_dev_name)); 17771da177e4SLinus Torvalds break; 17781da177e4SLinus Torvalds case 6: 177992ffb85dSAmos Waterland if (ic_proto_name(ip) == 0 && 178092ffb85dSAmos Waterland ic_myaddr == NONE) { 178192ffb85dSAmos Waterland ic_enable = 0; 178292ffb85dSAmos Waterland } 17831da177e4SLinus Torvalds break; 17845e953778SChristoph Fritz case 7: 17855e953778SChristoph Fritz if (CONF_NAMESERVERS_MAX >= 1) { 17865e953778SChristoph Fritz ic_nameservers[0] = in_aton(ip); 17875e953778SChristoph Fritz if (ic_nameservers[0] == ANY) 17885e953778SChristoph Fritz ic_nameservers[0] = NONE; 17895e953778SChristoph Fritz } 17905e953778SChristoph Fritz break; 17915e953778SChristoph Fritz case 8: 17925e953778SChristoph Fritz if (CONF_NAMESERVERS_MAX >= 2) { 17935e953778SChristoph Fritz ic_nameservers[1] = in_aton(ip); 17945e953778SChristoph Fritz if (ic_nameservers[1] == ANY) 17955e953778SChristoph Fritz ic_nameservers[1] = NONE; 17965e953778SChristoph Fritz } 17975e953778SChristoph Fritz break; 1798c04d2cb2SChris Novakovic case 9: 1799c04d2cb2SChris Novakovic if (CONF_NTP_SERVERS_MAX >= 1) { 1800c04d2cb2SChris Novakovic ic_ntp_servers[0] = in_aton(ip); 1801c04d2cb2SChris Novakovic if (ic_ntp_servers[0] == ANY) 1802c04d2cb2SChris Novakovic ic_ntp_servers[0] = NONE; 1803c04d2cb2SChris Novakovic } 1804c04d2cb2SChris Novakovic break; 18051da177e4SLinus Torvalds } 18061da177e4SLinus Torvalds } 18071da177e4SLinus Torvalds ip = cp; 18081da177e4SLinus Torvalds num++; 18091da177e4SLinus Torvalds } 18101da177e4SLinus Torvalds 18111da177e4SLinus Torvalds return 1; 18121da177e4SLinus Torvalds } 1813b37f4d7bSEldad Zack __setup("ip=", ip_auto_config_setup); 18141da177e4SLinus Torvalds 18151da177e4SLinus Torvalds static int __init nfsaddrs_config_setup(char *addrs) 18161da177e4SLinus Torvalds { 18171da177e4SLinus Torvalds return ip_auto_config_setup(addrs); 18181da177e4SLinus Torvalds } 1819b37f4d7bSEldad Zack __setup("nfsaddrs=", nfsaddrs_config_setup); 18201da177e4SLinus Torvalds 182162013dbbSRainer Jochem static int __init vendor_class_identifier_setup(char *addrs) 182262013dbbSRainer Jochem { 182362013dbbSRainer Jochem if (strlcpy(vendor_class_identifier, addrs, 182462013dbbSRainer Jochem sizeof(vendor_class_identifier)) 182562013dbbSRainer Jochem >= sizeof(vendor_class_identifier)) 182609605cc1SBastian Stender pr_warn("DHCP: vendorclass too long, truncated to \"%s\"\n", 182762013dbbSRainer Jochem vendor_class_identifier); 182862013dbbSRainer Jochem return 1; 182962013dbbSRainer Jochem } 183062013dbbSRainer Jochem __setup("dhcpclass=", vendor_class_identifier_setup); 18313fc46fc9SMartin Kepplinger 18323fc46fc9SMartin Kepplinger static int __init set_carrier_timeout(char *str) 18333fc46fc9SMartin Kepplinger { 18343fc46fc9SMartin Kepplinger ssize_t ret; 18353fc46fc9SMartin Kepplinger 18363fc46fc9SMartin Kepplinger if (!str) 18373fc46fc9SMartin Kepplinger return 0; 18383fc46fc9SMartin Kepplinger 18393fc46fc9SMartin Kepplinger ret = kstrtouint(str, 0, &carrier_timeout); 18403fc46fc9SMartin Kepplinger if (ret) 18413fc46fc9SMartin Kepplinger return 0; 18423fc46fc9SMartin Kepplinger 18433fc46fc9SMartin Kepplinger return 1; 18443fc46fc9SMartin Kepplinger } 18453fc46fc9SMartin Kepplinger __setup("carrier_timeout=", set_carrier_timeout); 1846