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> 64c6e970a0SAndrew Lunn #include <net/dsa.h> 651da177e4SLinus Torvalds #include <net/ip.h> 661da177e4SLinus Torvalds #include <net/ipconfig.h> 6714c85021SArnaldo Carvalho de Melo #include <net/route.h> 681da177e4SLinus Torvalds 697c0f6ba6SLinus Torvalds #include <linux/uaccess.h> 701da177e4SLinus Torvalds #include <net/checksum.h> 711da177e4SLinus Torvalds #include <asm/processor.h> 721da177e4SLinus Torvalds 731da177e4SLinus Torvalds #if defined(CONFIG_IP_PNP_DHCP) 741da177e4SLinus Torvalds #define IPCONFIG_DHCP 751da177e4SLinus Torvalds #endif 761da177e4SLinus Torvalds #if defined(CONFIG_IP_PNP_BOOTP) || defined(CONFIG_IP_PNP_DHCP) 771da177e4SLinus Torvalds #define IPCONFIG_BOOTP 781da177e4SLinus Torvalds #endif 791da177e4SLinus Torvalds #if defined(CONFIG_IP_PNP_RARP) 801da177e4SLinus Torvalds #define IPCONFIG_RARP 811da177e4SLinus Torvalds #endif 821da177e4SLinus Torvalds #if defined(IPCONFIG_BOOTP) || defined(IPCONFIG_RARP) 831da177e4SLinus Torvalds #define IPCONFIG_DYNAMIC 841da177e4SLinus Torvalds #endif 851da177e4SLinus Torvalds 861da177e4SLinus Torvalds /* Define the friendly delay before and after opening net devices */ 873fb72f1eSMicha Nelissen #define CONF_POST_OPEN 10 /* After opening: 10 msecs */ 883fb72f1eSMicha Nelissen #define CONF_CARRIER_TIMEOUT 120000 /* Wait for carrier timeout */ 891da177e4SLinus Torvalds 901da177e4SLinus Torvalds /* Define the timeout for waiting for a DHCP/BOOTP/RARP reply */ 911da177e4SLinus Torvalds #define CONF_OPEN_RETRIES 2 /* (Re)open devices twice */ 921da177e4SLinus Torvalds #define CONF_SEND_RETRIES 6 /* Send six requests per open */ 931da177e4SLinus Torvalds #define CONF_BASE_TIMEOUT (HZ*2) /* Initial timeout: 2 seconds */ 941da177e4SLinus Torvalds #define CONF_TIMEOUT_RANDOM (HZ) /* Maximum amount of randomization */ 951da177e4SLinus Torvalds #define CONF_TIMEOUT_MULT *7/4 /* Rate of timeout growth */ 961da177e4SLinus Torvalds #define CONF_TIMEOUT_MAX (HZ*30) /* Maximum allowed timeout */ 971da177e4SLinus Torvalds #define CONF_NAMESERVERS_MAX 3 /* Maximum number of nameservers 981da177e4SLinus Torvalds - '3' from resolv.h */ 99c04d2cb2SChris Novakovic #define CONF_NTP_SERVERS_MAX 3 /* Maximum number of NTP servers */ 1001da177e4SLinus Torvalds 10109640e63SHarvey Harrison #define NONE cpu_to_be32(INADDR_NONE) 10209640e63SHarvey Harrison #define ANY cpu_to_be32(INADDR_ANY) 1031da177e4SLinus Torvalds 1041da177e4SLinus Torvalds /* 1051da177e4SLinus Torvalds * Public IP configuration 1061da177e4SLinus Torvalds */ 1071da177e4SLinus Torvalds 1081da177e4SLinus Torvalds /* This is used by platforms which might be able to set the ipconfig 1091da177e4SLinus Torvalds * variables using firmware environment vars. If this is set, it will 1101da177e4SLinus Torvalds * ignore such firmware variables. 1111da177e4SLinus Torvalds */ 1121da177e4SLinus Torvalds int ic_set_manually __initdata = 0; /* IPconfig parameters set manually */ 1131da177e4SLinus Torvalds 1140d3979b9SFabian Frederick static int ic_enable __initdata; /* IP config enabled? */ 1151da177e4SLinus Torvalds 1161da177e4SLinus Torvalds /* Protocol choice */ 1171da177e4SLinus Torvalds int ic_proto_enabled __initdata = 0 1181da177e4SLinus Torvalds #ifdef IPCONFIG_BOOTP 1191da177e4SLinus Torvalds | IC_BOOTP 1201da177e4SLinus Torvalds #endif 1211da177e4SLinus Torvalds #ifdef CONFIG_IP_PNP_DHCP 1221da177e4SLinus Torvalds | IC_USE_DHCP 1231da177e4SLinus Torvalds #endif 1241da177e4SLinus Torvalds #ifdef IPCONFIG_RARP 1251da177e4SLinus Torvalds | IC_RARP 1261da177e4SLinus Torvalds #endif 1271da177e4SLinus Torvalds ; 1281da177e4SLinus Torvalds 1290d3979b9SFabian Frederick static int ic_host_name_set __initdata; /* Host name set by us? */ 1301da177e4SLinus Torvalds 1315a874db4SAl Viro __be32 ic_myaddr = NONE; /* My IP address */ 1325a874db4SAl Viro static __be32 ic_netmask = NONE; /* Netmask for local subnet */ 1335a874db4SAl Viro __be32 ic_gateway = NONE; /* Gateway IP address */ 1341da177e4SLinus Torvalds 13586ef7f9cSDavid S. Miller #ifdef IPCONFIG_DYNAMIC 1360b392be9SBen Dooks static __be32 ic_addrservaddr = NONE; /* IP Address of the IP addresses'server */ 13786ef7f9cSDavid S. Miller #endif 1389dd4a13aSPhilippe De Muyter 1395a874db4SAl Viro __be32 ic_servaddr = NONE; /* Boot server IP address */ 1401da177e4SLinus Torvalds 1415a874db4SAl Viro __be32 root_server_addr = NONE; /* Address of NFS server */ 1421da177e4SLinus Torvalds u8 root_server_path[256] = { 0, }; /* Path to mount as root */ 1431da177e4SLinus Torvalds 1441b0b04f9SDavid S. Miller /* vendor class identifier */ 1451b0b04f9SDavid S. Miller static char vendor_class_identifier[253] __initdata; 14662013dbbSRainer Jochem 14726fb342cSLi RongQing #if defined(CONFIG_IP_PNP_DHCP) 14826fb342cSLi RongQing static char dhcp_client_identifier[253] __initdata; 14926fb342cSLi RongQing #endif 15026fb342cSLi RongQing 1511da177e4SLinus Torvalds /* Persistent data: */ 1521da177e4SLinus Torvalds 15352b79e2bSArnd Bergmann #ifdef IPCONFIG_DYNAMIC 1541da177e4SLinus Torvalds static int ic_proto_used; /* Protocol used, if any */ 15552b79e2bSArnd Bergmann #else 15652b79e2bSArnd Bergmann #define ic_proto_used 0 15752b79e2bSArnd Bergmann #endif 1585a874db4SAl Viro static __be32 ic_nameservers[CONF_NAMESERVERS_MAX]; /* DNS Server IP addresses */ 159c04d2cb2SChris Novakovic static __be32 ic_ntp_servers[CONF_NTP_SERVERS_MAX]; /* NTP server IP addresses */ 1601da177e4SLinus Torvalds static u8 ic_domain[64]; /* DNS (not NIS) domain name */ 1611da177e4SLinus Torvalds 1621da177e4SLinus Torvalds /* 1631da177e4SLinus Torvalds * Private state. 1641da177e4SLinus Torvalds */ 1651da177e4SLinus Torvalds 1661da177e4SLinus Torvalds /* Name of user-selected boot device */ 1671da177e4SLinus Torvalds static char user_dev_name[IFNAMSIZ] __initdata = { 0, }; 1681da177e4SLinus Torvalds 1691da177e4SLinus Torvalds /* Protocols supported by available interfaces */ 1700d3979b9SFabian Frederick static int ic_proto_have_if __initdata; 1711da177e4SLinus Torvalds 1729643f455SChris Friesen /* MTU for boot device */ 1730d3979b9SFabian Frederick static int ic_dev_mtu __initdata; 1749643f455SChris Friesen 1751da177e4SLinus Torvalds #ifdef IPCONFIG_DYNAMIC 1761da177e4SLinus Torvalds static DEFINE_SPINLOCK(ic_recv_lock); 1770d3979b9SFabian Frederick static volatile int ic_got_reply __initdata; /* Proto(s) that replied */ 1781da177e4SLinus Torvalds #endif 1791da177e4SLinus Torvalds #ifdef IPCONFIG_DHCP 1800d3979b9SFabian Frederick static int ic_dhcp_msgtype __initdata; /* DHCP msg type received */ 1811da177e4SLinus Torvalds #endif 1821da177e4SLinus Torvalds 1831da177e4SLinus Torvalds 1841da177e4SLinus Torvalds /* 1851da177e4SLinus Torvalds * Network devices 1861da177e4SLinus Torvalds */ 1871da177e4SLinus Torvalds 1881da177e4SLinus Torvalds struct ic_device { 1891da177e4SLinus Torvalds struct ic_device *next; 1901da177e4SLinus Torvalds struct net_device *dev; 1911da177e4SLinus Torvalds unsigned short flags; 1921da177e4SLinus Torvalds short able; 1935a874db4SAl Viro __be32 xid; 1941da177e4SLinus Torvalds }; 1951da177e4SLinus Torvalds 1960d3979b9SFabian Frederick static struct ic_device *ic_first_dev __initdata; /* List of open device */ 1972647cffbSUwe Kleine-König static struct ic_device *ic_dev __initdata; /* Selected device */ 1981da177e4SLinus Torvalds 1993fb72f1eSMicha Nelissen static bool __init ic_is_init_dev(struct net_device *dev) 200964ad81cSDavid S. Miller { 2013fb72f1eSMicha Nelissen if (dev->flags & IFF_LOOPBACK) 2023fb72f1eSMicha Nelissen return false; 2033fb72f1eSMicha Nelissen return user_dev_name[0] ? !strcmp(dev->name, user_dev_name) : 204964ad81cSDavid S. Miller (!(dev->flags & IFF_LOOPBACK) && 205964ad81cSDavid S. Miller (dev->flags & (IFF_POINTOPOINT|IFF_BROADCAST)) && 2063fb72f1eSMicha Nelissen strncmp(dev->name, "dummy", 5)); 207964ad81cSDavid S. Miller } 208964ad81cSDavid S. Miller 2091da177e4SLinus Torvalds static int __init ic_open_devs(void) 2101da177e4SLinus Torvalds { 2111da177e4SLinus Torvalds struct ic_device *d, **last; 2121da177e4SLinus Torvalds struct net_device *dev; 2131da177e4SLinus Torvalds unsigned short oflags; 2145e404cd6SPaul Gortmaker unsigned long start, next_msg; 2151da177e4SLinus Torvalds 2161da177e4SLinus Torvalds last = &ic_first_dev; 2176756ae4bSStephen Hemminger rtnl_lock(); 2181da177e4SLinus Torvalds 219728c0208SFlorian Fainelli /* bring loopback and DSA master network devices up first */ 2200cc217e1SEric W. Biederman for_each_netdev(&init_net, dev) { 221728c0208SFlorian Fainelli if (!(dev->flags & IFF_LOOPBACK) && !netdev_uses_dsa(dev)) 2220cc217e1SEric W. Biederman continue; 2230cc217e1SEric W. Biederman if (dev_change_flags(dev, dev->flags | IFF_UP) < 0) 224058bd4d2SJoe Perches pr_err("IP-Config: Failed to open %s\n", dev->name); 2250cc217e1SEric W. Biederman } 2261da177e4SLinus Torvalds 227881d966bSEric W. Biederman for_each_netdev(&init_net, dev) { 2283fb72f1eSMicha Nelissen if (ic_is_init_dev(dev)) { 2291da177e4SLinus Torvalds int able = 0; 2301da177e4SLinus Torvalds if (dev->mtu >= 364) 2311da177e4SLinus Torvalds able |= IC_BOOTP; 2321da177e4SLinus Torvalds else 23309605cc1SBastian Stender pr_warn("DHCP/BOOTP: Ignoring device %s, MTU %d too small\n", 234058bd4d2SJoe Perches dev->name, dev->mtu); 2351da177e4SLinus Torvalds if (!(dev->flags & IFF_NOARP)) 2361da177e4SLinus Torvalds able |= IC_RARP; 2371da177e4SLinus Torvalds able &= ic_proto_enabled; 2381da177e4SLinus Torvalds if (ic_proto_enabled && !able) 2391da177e4SLinus Torvalds continue; 2401da177e4SLinus Torvalds oflags = dev->flags; 2411da177e4SLinus Torvalds if (dev_change_flags(dev, oflags | IFF_UP) < 0) { 242058bd4d2SJoe Perches pr_err("IP-Config: Failed to open %s\n", 243058bd4d2SJoe Perches dev->name); 2441da177e4SLinus Torvalds continue; 2451da177e4SLinus Torvalds } 2461da177e4SLinus Torvalds if (!(d = kmalloc(sizeof(struct ic_device), GFP_KERNEL))) { 2476756ae4bSStephen Hemminger rtnl_unlock(); 248964ad81cSDavid S. Miller return -ENOMEM; 2491da177e4SLinus Torvalds } 2501da177e4SLinus Torvalds d->dev = dev; 2511da177e4SLinus Torvalds *last = d; 2521da177e4SLinus Torvalds last = &d->next; 2531da177e4SLinus Torvalds d->flags = oflags; 2541da177e4SLinus Torvalds d->able = able; 2551da177e4SLinus Torvalds if (able & IC_BOOTP) 2565a874db4SAl Viro get_random_bytes(&d->xid, sizeof(__be32)); 2571da177e4SLinus Torvalds else 2581da177e4SLinus Torvalds d->xid = 0; 2591da177e4SLinus Torvalds ic_proto_have_if |= able; 26009605cc1SBastian Stender pr_debug("IP-Config: %s UP (able=%d, xid=%08x)\n", 26109605cc1SBastian Stender dev->name, able, d->xid); 2621da177e4SLinus Torvalds } 2631da177e4SLinus Torvalds } 2643fb72f1eSMicha Nelissen 265cd7816d1SGerlando Falauto /* no point in waiting if we could not bring up at least one device */ 266cd7816d1SGerlando Falauto if (!ic_first_dev) 267cd7816d1SGerlando Falauto goto have_carrier; 268cd7816d1SGerlando Falauto 2693fb72f1eSMicha Nelissen /* wait for a carrier on at least one device */ 2703fb72f1eSMicha Nelissen start = jiffies; 2715e404cd6SPaul Gortmaker next_msg = start + msecs_to_jiffies(CONF_CARRIER_TIMEOUT/12); 272c72c95a0SHimangi Saraogi while (time_before(jiffies, start + 273c72c95a0SHimangi Saraogi msecs_to_jiffies(CONF_CARRIER_TIMEOUT))) { 2745e404cd6SPaul Gortmaker int wait, elapsed; 2755e404cd6SPaul Gortmaker 2763fb72f1eSMicha Nelissen for_each_netdev(&init_net, dev) 2773fb72f1eSMicha Nelissen if (ic_is_init_dev(dev) && netif_carrier_ok(dev)) 2783fb72f1eSMicha Nelissen goto have_carrier; 2793fb72f1eSMicha Nelissen 2803fb72f1eSMicha Nelissen msleep(1); 2815e404cd6SPaul Gortmaker 282357137a4SFX Le Bail if (time_before(jiffies, next_msg)) 2835e404cd6SPaul Gortmaker continue; 2845e404cd6SPaul Gortmaker 2855e404cd6SPaul Gortmaker elapsed = jiffies_to_msecs(jiffies - start); 2865e404cd6SPaul Gortmaker wait = (CONF_CARRIER_TIMEOUT - elapsed + 500)/1000; 2875e404cd6SPaul Gortmaker pr_info("Waiting up to %d more seconds for network.\n", wait); 2885e404cd6SPaul Gortmaker next_msg = jiffies + msecs_to_jiffies(CONF_CARRIER_TIMEOUT/12); 2893fb72f1eSMicha Nelissen } 2903fb72f1eSMicha Nelissen have_carrier: 2916756ae4bSStephen Hemminger rtnl_unlock(); 2921da177e4SLinus Torvalds 2931da177e4SLinus Torvalds *last = NULL; 2941da177e4SLinus Torvalds 2951da177e4SLinus Torvalds if (!ic_first_dev) { 2961da177e4SLinus Torvalds if (user_dev_name[0]) 297058bd4d2SJoe Perches pr_err("IP-Config: Device `%s' not found\n", 298058bd4d2SJoe Perches user_dev_name); 2991da177e4SLinus Torvalds else 300058bd4d2SJoe Perches pr_err("IP-Config: No network devices available\n"); 301964ad81cSDavid S. Miller return -ENODEV; 3021da177e4SLinus Torvalds } 3031da177e4SLinus Torvalds return 0; 3041da177e4SLinus Torvalds } 3051da177e4SLinus Torvalds 3061da177e4SLinus Torvalds static void __init ic_close_devs(void) 3071da177e4SLinus Torvalds { 3081da177e4SLinus Torvalds struct ic_device *d, *next; 3091da177e4SLinus Torvalds struct net_device *dev; 3101da177e4SLinus Torvalds 3116756ae4bSStephen Hemminger rtnl_lock(); 3121da177e4SLinus Torvalds next = ic_first_dev; 3131da177e4SLinus Torvalds while ((d = next)) { 3141da177e4SLinus Torvalds next = d->next; 3151da177e4SLinus Torvalds dev = d->dev; 316ffefb6f4SMark Rutland if (d != ic_dev && !netdev_uses_dsa(dev)) { 31709605cc1SBastian Stender pr_debug("IP-Config: Downing %s\n", dev->name); 3181da177e4SLinus Torvalds dev_change_flags(dev, d->flags); 3191da177e4SLinus Torvalds } 3201da177e4SLinus Torvalds kfree(d); 3211da177e4SLinus Torvalds } 3226756ae4bSStephen Hemminger rtnl_unlock(); 3231da177e4SLinus Torvalds } 3241da177e4SLinus Torvalds 3251da177e4SLinus Torvalds /* 3261da177e4SLinus Torvalds * Interface to various network functions. 3271da177e4SLinus Torvalds */ 3281da177e4SLinus Torvalds 3291da177e4SLinus Torvalds static inline void 3305a874db4SAl Viro set_sockaddr(struct sockaddr_in *sin, __be32 addr, __be16 port) 3311da177e4SLinus Torvalds { 3321da177e4SLinus Torvalds sin->sin_family = AF_INET; 3331da177e4SLinus Torvalds sin->sin_addr.s_addr = addr; 3341da177e4SLinus Torvalds sin->sin_port = port; 3351da177e4SLinus Torvalds } 3361da177e4SLinus Torvalds 3371da177e4SLinus Torvalds /* 3381da177e4SLinus Torvalds * Set up interface addresses and routes. 3391da177e4SLinus Torvalds */ 3401da177e4SLinus Torvalds 3411da177e4SLinus Torvalds static int __init ic_setup_if(void) 3421da177e4SLinus Torvalds { 3431da177e4SLinus Torvalds struct ifreq ir; 3441da177e4SLinus Torvalds struct sockaddr_in *sin = (void *) &ir.ifr_ifru.ifru_addr; 3451da177e4SLinus Torvalds int err; 3461da177e4SLinus Torvalds 3471da177e4SLinus Torvalds memset(&ir, 0, sizeof(ir)); 3482647cffbSUwe Kleine-König strcpy(ir.ifr_ifrn.ifrn_name, ic_dev->dev->name); 3491da177e4SLinus Torvalds set_sockaddr(sin, ic_myaddr, 0); 35003aef17bSAl Viro if ((err = devinet_ioctl(&init_net, SIOCSIFADDR, &ir)) < 0) { 351058bd4d2SJoe Perches pr_err("IP-Config: Unable to set interface address (%d)\n", 352058bd4d2SJoe Perches err); 3531da177e4SLinus Torvalds return -1; 3541da177e4SLinus Torvalds } 3551da177e4SLinus Torvalds set_sockaddr(sin, ic_netmask, 0); 35603aef17bSAl Viro if ((err = devinet_ioctl(&init_net, SIOCSIFNETMASK, &ir)) < 0) { 357058bd4d2SJoe Perches pr_err("IP-Config: Unable to set interface netmask (%d)\n", 358058bd4d2SJoe Perches err); 3591da177e4SLinus Torvalds return -1; 3601da177e4SLinus Torvalds } 3611da177e4SLinus Torvalds set_sockaddr(sin, ic_myaddr | ~ic_netmask, 0); 36203aef17bSAl Viro if ((err = devinet_ioctl(&init_net, SIOCSIFBRDADDR, &ir)) < 0) { 363058bd4d2SJoe Perches pr_err("IP-Config: Unable to set interface broadcast address (%d)\n", 364058bd4d2SJoe Perches err); 3651da177e4SLinus Torvalds return -1; 3661da177e4SLinus Torvalds } 3679643f455SChris Friesen /* Handle the case where we need non-standard MTU on the boot link (a network 3689643f455SChris Friesen * using jumbo frames, for instance). If we can't set the mtu, don't error 3699643f455SChris Friesen * out, we'll try to muddle along. 3709643f455SChris Friesen */ 3719643f455SChris Friesen if (ic_dev_mtu != 0) { 3726a88fbe7SAl Viro rtnl_lock(); 3736a88fbe7SAl Viro if ((err = dev_set_mtu(ic_dev->dev, ic_dev_mtu)) < 0) 374058bd4d2SJoe Perches pr_err("IP-Config: Unable to set interface mtu to %d (%d)\n", 3759643f455SChris Friesen ic_dev_mtu, err); 3766a88fbe7SAl Viro rtnl_unlock(); 3779643f455SChris Friesen } 3781da177e4SLinus Torvalds return 0; 3791da177e4SLinus Torvalds } 3801da177e4SLinus Torvalds 3811da177e4SLinus Torvalds static int __init ic_setup_routes(void) 3821da177e4SLinus Torvalds { 3831da177e4SLinus Torvalds /* No need to setup device routes, only the default route... */ 3841da177e4SLinus Torvalds 3855a874db4SAl Viro if (ic_gateway != NONE) { 3861da177e4SLinus Torvalds struct rtentry rm; 3871da177e4SLinus Torvalds int err; 3881da177e4SLinus Torvalds 3891da177e4SLinus Torvalds memset(&rm, 0, sizeof(rm)); 3901da177e4SLinus Torvalds if ((ic_gateway ^ ic_myaddr) & ic_netmask) { 391058bd4d2SJoe Perches pr_err("IP-Config: Gateway not on directly connected network\n"); 3921da177e4SLinus Torvalds return -1; 3931da177e4SLinus Torvalds } 3941da177e4SLinus Torvalds set_sockaddr((struct sockaddr_in *) &rm.rt_dst, 0, 0); 3951da177e4SLinus Torvalds set_sockaddr((struct sockaddr_in *) &rm.rt_genmask, 0, 0); 3961da177e4SLinus Torvalds set_sockaddr((struct sockaddr_in *) &rm.rt_gateway, ic_gateway, 0); 3971da177e4SLinus Torvalds rm.rt_flags = RTF_UP | RTF_GATEWAY; 398ca25c300SAl Viro if ((err = ip_rt_ioctl(&init_net, SIOCADDRT, &rm)) < 0) { 399058bd4d2SJoe Perches pr_err("IP-Config: Cannot add default route (%d)\n", 400058bd4d2SJoe Perches err); 4011da177e4SLinus Torvalds return -1; 4021da177e4SLinus Torvalds } 4031da177e4SLinus Torvalds } 4041da177e4SLinus Torvalds 4051da177e4SLinus Torvalds return 0; 4061da177e4SLinus Torvalds } 4071da177e4SLinus Torvalds 4081da177e4SLinus Torvalds /* 4091da177e4SLinus Torvalds * Fill in default values for all missing parameters. 4101da177e4SLinus Torvalds */ 4111da177e4SLinus Torvalds 4121da177e4SLinus Torvalds static int __init ic_defaults(void) 4131da177e4SLinus Torvalds { 4141da177e4SLinus Torvalds /* 4151da177e4SLinus Torvalds * At this point we have no userspace running so need not 4161da177e4SLinus Torvalds * claim locks on system_utsname 4171da177e4SLinus Torvalds */ 4181da177e4SLinus Torvalds 4191da177e4SLinus Torvalds if (!ic_host_name_set) 420673d57e7SHarvey Harrison sprintf(init_utsname()->nodename, "%pI4", &ic_myaddr); 4211da177e4SLinus Torvalds 4225a874db4SAl Viro if (root_server_addr == NONE) 4231da177e4SLinus Torvalds root_server_addr = ic_servaddr; 4241da177e4SLinus Torvalds 4255a874db4SAl Viro if (ic_netmask == NONE) { 4261da177e4SLinus Torvalds if (IN_CLASSA(ntohl(ic_myaddr))) 4271da177e4SLinus Torvalds ic_netmask = htonl(IN_CLASSA_NET); 4281da177e4SLinus Torvalds else if (IN_CLASSB(ntohl(ic_myaddr))) 4291da177e4SLinus Torvalds ic_netmask = htonl(IN_CLASSB_NET); 4301da177e4SLinus Torvalds else if (IN_CLASSC(ntohl(ic_myaddr))) 4311da177e4SLinus Torvalds ic_netmask = htonl(IN_CLASSC_NET); 432*65cab850SDave Taht else if (IN_CLASSE(ntohl(ic_myaddr))) 433*65cab850SDave Taht ic_netmask = htonl(IN_CLASSE_NET); 4341da177e4SLinus Torvalds else { 435058bd4d2SJoe Perches pr_err("IP-Config: Unable to guess netmask for address %pI4\n", 436673d57e7SHarvey Harrison &ic_myaddr); 4371da177e4SLinus Torvalds return -1; 4381da177e4SLinus Torvalds } 43909605cc1SBastian Stender pr_notice("IP-Config: Guessing netmask %pI4\n", 44009605cc1SBastian Stender &ic_netmask); 4411da177e4SLinus Torvalds } 4421da177e4SLinus Torvalds 4431da177e4SLinus Torvalds return 0; 4441da177e4SLinus Torvalds } 4451da177e4SLinus Torvalds 4461da177e4SLinus Torvalds /* 4471da177e4SLinus Torvalds * RARP support. 4481da177e4SLinus Torvalds */ 4491da177e4SLinus Torvalds 4501da177e4SLinus Torvalds #ifdef IPCONFIG_RARP 4511da177e4SLinus Torvalds 452f2ccd8faSDavid S. Miller static int ic_rarp_recv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev); 4531da177e4SLinus Torvalds 4541da177e4SLinus Torvalds static struct packet_type rarp_packet_type __initdata = { 45509640e63SHarvey Harrison .type = cpu_to_be16(ETH_P_RARP), 4561da177e4SLinus Torvalds .func = ic_rarp_recv, 4571da177e4SLinus Torvalds }; 4581da177e4SLinus Torvalds 45945e741b8SSam Ravnborg static inline void __init ic_rarp_init(void) 4601da177e4SLinus Torvalds { 4611da177e4SLinus Torvalds dev_add_pack(&rarp_packet_type); 4621da177e4SLinus Torvalds } 4631da177e4SLinus Torvalds 46445e741b8SSam Ravnborg static inline void __init ic_rarp_cleanup(void) 4651da177e4SLinus Torvalds { 4661da177e4SLinus Torvalds dev_remove_pack(&rarp_packet_type); 4671da177e4SLinus Torvalds } 4681da177e4SLinus Torvalds 4691da177e4SLinus Torvalds /* 4701da177e4SLinus Torvalds * Process received RARP packet. 4711da177e4SLinus Torvalds */ 4721da177e4SLinus Torvalds static int __init 473f2ccd8faSDavid S. Miller ic_rarp_recv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev) 4741da177e4SLinus Torvalds { 4751da177e4SLinus Torvalds struct arphdr *rarp; 4761da177e4SLinus Torvalds unsigned char *rarp_ptr; 4775a874db4SAl Viro __be32 sip, tip; 4786b436d33SFabian Frederick unsigned char *tha; /* t for "target" */ 4791da177e4SLinus Torvalds struct ic_device *d; 4801da177e4SLinus Torvalds 481721499e8SYOSHIFUJI Hideaki if (!net_eq(dev_net(dev), &init_net)) 482e730c155SEric W. Biederman goto drop; 483e730c155SEric W. Biederman 48451456b29SIan Morris skb = skb_share_check(skb, GFP_ATOMIC); 48551456b29SIan Morris if (!skb) 4861da177e4SLinus Torvalds return NET_RX_DROP; 4871da177e4SLinus Torvalds 4881da177e4SLinus Torvalds if (!pskb_may_pull(skb, sizeof(struct arphdr))) 4891da177e4SLinus Torvalds goto drop; 4901da177e4SLinus Torvalds 4911da177e4SLinus Torvalds /* Basic sanity checks can be done without the lock. */ 4929c70220bSArnaldo Carvalho de Melo rarp = (struct arphdr *)skb_transport_header(skb); 4931da177e4SLinus Torvalds 4941da177e4SLinus Torvalds /* If this test doesn't pass, it's not IP, or we should 4951da177e4SLinus Torvalds * ignore it anyway. 4961da177e4SLinus Torvalds */ 4971da177e4SLinus Torvalds if (rarp->ar_hln != dev->addr_len || dev->type != ntohs(rarp->ar_hrd)) 4981da177e4SLinus Torvalds goto drop; 4991da177e4SLinus Torvalds 5001da177e4SLinus Torvalds /* If it's not a RARP reply, delete it. */ 5011da177e4SLinus Torvalds if (rarp->ar_op != htons(ARPOP_RREPLY)) 5021da177e4SLinus Torvalds goto drop; 5031da177e4SLinus Torvalds 5041da177e4SLinus Torvalds /* If it's not Ethernet, delete it. */ 5051da177e4SLinus Torvalds if (rarp->ar_pro != htons(ETH_P_IP)) 5061da177e4SLinus Torvalds goto drop; 5071da177e4SLinus Torvalds 508988b7050SPavel Emelyanov if (!pskb_may_pull(skb, arp_hdr_len(dev))) 5091da177e4SLinus Torvalds goto drop; 5101da177e4SLinus Torvalds 5111da177e4SLinus Torvalds /* OK, it is all there and looks valid, process... */ 5129c70220bSArnaldo Carvalho de Melo rarp = (struct arphdr *)skb_transport_header(skb); 5131da177e4SLinus Torvalds rarp_ptr = (unsigned char *) (rarp + 1); 5141da177e4SLinus Torvalds 5151da177e4SLinus Torvalds /* One reply at a time, please. */ 5161da177e4SLinus Torvalds spin_lock(&ic_recv_lock); 5171da177e4SLinus Torvalds 5181da177e4SLinus Torvalds /* If we already have a reply, just drop the packet */ 5191da177e4SLinus Torvalds if (ic_got_reply) 5201da177e4SLinus Torvalds goto drop_unlock; 5211da177e4SLinus Torvalds 5221da177e4SLinus Torvalds /* Find the ic_device that the packet arrived on */ 5231da177e4SLinus Torvalds d = ic_first_dev; 5241da177e4SLinus Torvalds while (d && d->dev != dev) 5251da177e4SLinus Torvalds d = d->next; 5261da177e4SLinus Torvalds if (!d) 5271da177e4SLinus Torvalds goto drop_unlock; /* should never happen */ 5281da177e4SLinus Torvalds 5291da177e4SLinus Torvalds /* Extract variable-width fields */ 5301da177e4SLinus Torvalds rarp_ptr += dev->addr_len; 5311da177e4SLinus Torvalds memcpy(&sip, rarp_ptr, 4); 5321da177e4SLinus Torvalds rarp_ptr += 4; 5331da177e4SLinus Torvalds tha = rarp_ptr; 5341da177e4SLinus Torvalds rarp_ptr += dev->addr_len; 5351da177e4SLinus Torvalds memcpy(&tip, rarp_ptr, 4); 5361da177e4SLinus Torvalds 5371da177e4SLinus Torvalds /* Discard packets which are not meant for us. */ 5381da177e4SLinus Torvalds if (memcmp(tha, dev->dev_addr, dev->addr_len)) 5391da177e4SLinus Torvalds goto drop_unlock; 5401da177e4SLinus Torvalds 5411da177e4SLinus Torvalds /* Discard packets which are not from specified server. */ 5425a874db4SAl Viro if (ic_servaddr != NONE && ic_servaddr != sip) 5431da177e4SLinus Torvalds goto drop_unlock; 5441da177e4SLinus Torvalds 5451da177e4SLinus Torvalds /* We have a winner! */ 5462647cffbSUwe Kleine-König ic_dev = d; 5475a874db4SAl Viro if (ic_myaddr == NONE) 5481da177e4SLinus Torvalds ic_myaddr = tip; 5491da177e4SLinus Torvalds ic_servaddr = sip; 5509dd4a13aSPhilippe De Muyter ic_addrservaddr = sip; 5511da177e4SLinus Torvalds ic_got_reply = IC_RARP; 5521da177e4SLinus Torvalds 5531da177e4SLinus Torvalds drop_unlock: 5541da177e4SLinus Torvalds /* Show's over. Nothing to see here. */ 5551da177e4SLinus Torvalds spin_unlock(&ic_recv_lock); 5561da177e4SLinus Torvalds 5571da177e4SLinus Torvalds drop: 5581da177e4SLinus Torvalds /* Throw the packet out. */ 5591da177e4SLinus Torvalds kfree_skb(skb); 5601da177e4SLinus Torvalds return 0; 5611da177e4SLinus Torvalds } 5621da177e4SLinus Torvalds 5631da177e4SLinus Torvalds 5641da177e4SLinus Torvalds /* 5651da177e4SLinus Torvalds * Send RARP request packet over a single interface. 5661da177e4SLinus Torvalds */ 5671da177e4SLinus Torvalds static void __init ic_rarp_send_if(struct ic_device *d) 5681da177e4SLinus Torvalds { 5691da177e4SLinus Torvalds struct net_device *dev = d->dev; 5701da177e4SLinus Torvalds arp_send(ARPOP_RREQUEST, ETH_P_RARP, 0, dev, 0, NULL, 5711da177e4SLinus Torvalds dev->dev_addr, dev->dev_addr); 5721da177e4SLinus Torvalds } 5731da177e4SLinus Torvalds #endif 5741da177e4SLinus Torvalds 5751da177e4SLinus Torvalds /* 576842b08bbSAndy Shevchenko * Predefine Nameservers 577842b08bbSAndy Shevchenko */ 578842b08bbSAndy Shevchenko static inline void __init ic_nameservers_predef(void) 579842b08bbSAndy Shevchenko { 580842b08bbSAndy Shevchenko int i; 581842b08bbSAndy Shevchenko 582842b08bbSAndy Shevchenko for (i = 0; i < CONF_NAMESERVERS_MAX; i++) 583842b08bbSAndy Shevchenko ic_nameservers[i] = NONE; 584842b08bbSAndy Shevchenko } 585842b08bbSAndy Shevchenko 586c04d2cb2SChris Novakovic /* Predefine NTP servers */ 587c04d2cb2SChris Novakovic static inline void __init ic_ntp_servers_predef(void) 588c04d2cb2SChris Novakovic { 589c04d2cb2SChris Novakovic int i; 590c04d2cb2SChris Novakovic 591c04d2cb2SChris Novakovic for (i = 0; i < CONF_NTP_SERVERS_MAX; i++) 592c04d2cb2SChris Novakovic ic_ntp_servers[i] = NONE; 593c04d2cb2SChris Novakovic } 594c04d2cb2SChris Novakovic 595842b08bbSAndy Shevchenko /* 5961da177e4SLinus Torvalds * DHCP/BOOTP support. 5971da177e4SLinus Torvalds */ 5981da177e4SLinus Torvalds 5991da177e4SLinus Torvalds #ifdef IPCONFIG_BOOTP 6001da177e4SLinus Torvalds 6011da177e4SLinus Torvalds struct bootp_pkt { /* BOOTP packet format */ 6021da177e4SLinus Torvalds struct iphdr iph; /* IP header */ 6031da177e4SLinus Torvalds struct udphdr udph; /* UDP header */ 6041da177e4SLinus Torvalds u8 op; /* 1=request, 2=reply */ 6051da177e4SLinus Torvalds u8 htype; /* HW address type */ 6061da177e4SLinus Torvalds u8 hlen; /* HW address length */ 6071da177e4SLinus Torvalds u8 hops; /* Used only by gateways */ 6085a874db4SAl Viro __be32 xid; /* Transaction ID */ 6095a874db4SAl Viro __be16 secs; /* Seconds since we started */ 6105a874db4SAl Viro __be16 flags; /* Just what it says */ 6115a874db4SAl Viro __be32 client_ip; /* Client's IP address if known */ 6125a874db4SAl Viro __be32 your_ip; /* Assigned IP address */ 6135a874db4SAl Viro __be32 server_ip; /* (Next, e.g. NFS) Server's IP address */ 6145a874db4SAl Viro __be32 relay_ip; /* IP address of BOOTP relay */ 6151da177e4SLinus Torvalds u8 hw_addr[16]; /* Client's HW address */ 6161da177e4SLinus Torvalds u8 serv_name[64]; /* Server host name */ 6171da177e4SLinus Torvalds u8 boot_file[128]; /* Name of boot file */ 6181da177e4SLinus Torvalds u8 exten[312]; /* DHCP options / BOOTP vendor extensions */ 6191da177e4SLinus Torvalds }; 6201da177e4SLinus Torvalds 6211da177e4SLinus Torvalds /* packet ops */ 6221da177e4SLinus Torvalds #define BOOTP_REQUEST 1 6231da177e4SLinus Torvalds #define BOOTP_REPLY 2 6241da177e4SLinus Torvalds 6251da177e4SLinus Torvalds /* DHCP message types */ 6261da177e4SLinus Torvalds #define DHCPDISCOVER 1 6271da177e4SLinus Torvalds #define DHCPOFFER 2 6281da177e4SLinus Torvalds #define DHCPREQUEST 3 6291da177e4SLinus Torvalds #define DHCPDECLINE 4 6301da177e4SLinus Torvalds #define DHCPACK 5 6311da177e4SLinus Torvalds #define DHCPNAK 6 6321da177e4SLinus Torvalds #define DHCPRELEASE 7 6331da177e4SLinus Torvalds #define DHCPINFORM 8 6341da177e4SLinus Torvalds 635f2ccd8faSDavid S. Miller static int ic_bootp_recv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev); 6361da177e4SLinus Torvalds 6371da177e4SLinus Torvalds static struct packet_type bootp_packet_type __initdata = { 63809640e63SHarvey Harrison .type = cpu_to_be16(ETH_P_IP), 6391da177e4SLinus Torvalds .func = ic_bootp_recv, 6401da177e4SLinus Torvalds }; 6411da177e4SLinus Torvalds 6421da177e4SLinus Torvalds /* 6431da177e4SLinus Torvalds * Initialize DHCP/BOOTP extension fields in the request. 6441da177e4SLinus Torvalds */ 6451da177e4SLinus Torvalds 6461da177e4SLinus Torvalds static const u8 ic_bootp_cookie[4] = { 99, 130, 83, 99 }; 6471da177e4SLinus Torvalds 6481da177e4SLinus Torvalds #ifdef IPCONFIG_DHCP 6491da177e4SLinus Torvalds 6501da177e4SLinus Torvalds static void __init 65122fc5388SUwe Kleine-König ic_dhcp_init_options(u8 *options, struct ic_device *d) 6521da177e4SLinus Torvalds { 6535a874db4SAl Viro u8 mt = ((ic_servaddr == NONE) 6541da177e4SLinus Torvalds ? DHCPDISCOVER : DHCPREQUEST); 6551da177e4SLinus Torvalds u8 *e = options; 65662013dbbSRainer Jochem int len; 6571da177e4SLinus Torvalds 65822fc5388SUwe Kleine-König pr_debug("DHCP: Sending message type %d (%s)\n", mt, d->dev->name); 6591da177e4SLinus Torvalds 6601da177e4SLinus Torvalds memcpy(e, ic_bootp_cookie, 4); /* RFC1048 Magic Cookie */ 6611da177e4SLinus Torvalds e += 4; 6621da177e4SLinus Torvalds 6631da177e4SLinus Torvalds *e++ = 53; /* DHCP message type */ 6641da177e4SLinus Torvalds *e++ = 1; 6651da177e4SLinus Torvalds *e++ = mt; 6661da177e4SLinus Torvalds 6671da177e4SLinus Torvalds if (mt == DHCPREQUEST) { 6681da177e4SLinus Torvalds *e++ = 54; /* Server ID (IP address) */ 6691da177e4SLinus Torvalds *e++ = 4; 6701da177e4SLinus Torvalds memcpy(e, &ic_servaddr, 4); 6711da177e4SLinus Torvalds e += 4; 6721da177e4SLinus Torvalds 6731da177e4SLinus Torvalds *e++ = 50; /* Requested IP address */ 6741da177e4SLinus Torvalds *e++ = 4; 6751da177e4SLinus Torvalds memcpy(e, &ic_myaddr, 4); 6761da177e4SLinus Torvalds e += 4; 6771da177e4SLinus Torvalds } 6781da177e4SLinus Torvalds 6791da177e4SLinus Torvalds /* always? */ 6801da177e4SLinus Torvalds { 6811da177e4SLinus Torvalds static const u8 ic_req_params[] = { 6821da177e4SLinus Torvalds 1, /* Subnet mask */ 6831da177e4SLinus Torvalds 3, /* Default gateway */ 6841da177e4SLinus Torvalds 6, /* DNS server */ 6851da177e4SLinus Torvalds 12, /* Host name */ 6861da177e4SLinus Torvalds 15, /* Domain name */ 6871da177e4SLinus Torvalds 17, /* Boot path */ 6889643f455SChris Friesen 26, /* MTU */ 6891da177e4SLinus Torvalds 40, /* NIS domain name */ 690c04d2cb2SChris Novakovic 42, /* NTP servers */ 6911da177e4SLinus Torvalds }; 6921da177e4SLinus Torvalds 6931da177e4SLinus Torvalds *e++ = 55; /* Parameter request list */ 6941da177e4SLinus Torvalds *e++ = sizeof(ic_req_params); 6951da177e4SLinus Torvalds memcpy(e, ic_req_params, sizeof(ic_req_params)); 6961da177e4SLinus Torvalds e += sizeof(ic_req_params); 69762013dbbSRainer Jochem 698130c0f47SWu Fengguang if (ic_host_name_set) { 699130c0f47SWu Fengguang *e++ = 12; /* host-name */ 700130c0f47SWu Fengguang len = strlen(utsname()->nodename); 701130c0f47SWu Fengguang *e++ = len; 702130c0f47SWu Fengguang memcpy(e, utsname()->nodename, len); 703130c0f47SWu Fengguang e += len; 704130c0f47SWu Fengguang } 70562013dbbSRainer Jochem if (*vendor_class_identifier) { 706058bd4d2SJoe Perches pr_info("DHCP: sending class identifier \"%s\"\n", 70762013dbbSRainer Jochem vendor_class_identifier); 70862013dbbSRainer Jochem *e++ = 60; /* Class-identifier */ 70962013dbbSRainer Jochem len = strlen(vendor_class_identifier); 71062013dbbSRainer Jochem *e++ = len; 71162013dbbSRainer Jochem memcpy(e, vendor_class_identifier, len); 71262013dbbSRainer Jochem e += len; 71362013dbbSRainer Jochem } 71426fb342cSLi RongQing len = strlen(dhcp_client_identifier + 1); 71526fb342cSLi RongQing /* the minimum length of identifier is 2, include 1 byte type, 71626fb342cSLi RongQing * and can not be larger than the length of options 71726fb342cSLi RongQing */ 71826fb342cSLi RongQing if (len >= 1 && len < 312 - (e - options) - 1) { 71926fb342cSLi RongQing *e++ = 61; 72026fb342cSLi RongQing *e++ = len + 1; 72126fb342cSLi RongQing memcpy(e, dhcp_client_identifier, len + 1); 72226fb342cSLi RongQing e += len + 1; 72326fb342cSLi RongQing } 7241da177e4SLinus Torvalds } 7251da177e4SLinus Torvalds 7261da177e4SLinus Torvalds *e++ = 255; /* End of the list */ 7271da177e4SLinus Torvalds } 7281da177e4SLinus Torvalds 7291da177e4SLinus Torvalds #endif /* IPCONFIG_DHCP */ 7301da177e4SLinus Torvalds 7311da177e4SLinus Torvalds static void __init ic_bootp_init_ext(u8 *e) 7321da177e4SLinus Torvalds { 7331da177e4SLinus Torvalds memcpy(e, ic_bootp_cookie, 4); /* RFC1048 Magic Cookie */ 7341da177e4SLinus Torvalds e += 4; 7351da177e4SLinus Torvalds *e++ = 1; /* Subnet mask request */ 7361da177e4SLinus Torvalds *e++ = 4; 7371da177e4SLinus Torvalds e += 4; 7381da177e4SLinus Torvalds *e++ = 3; /* Default gateway request */ 7391da177e4SLinus Torvalds *e++ = 4; 7401da177e4SLinus Torvalds e += 4; 741de1fa15bSChris Novakovic #if CONF_NAMESERVERS_MAX > 0 7424e1a8af2SChris Novakovic *e++ = 6; /* (DNS) name server request */ 743de1fa15bSChris Novakovic *e++ = 4 * CONF_NAMESERVERS_MAX; 744de1fa15bSChris Novakovic e += 4 * CONF_NAMESERVERS_MAX; 745de1fa15bSChris Novakovic #endif 7461da177e4SLinus Torvalds *e++ = 12; /* Host name request */ 7471da177e4SLinus Torvalds *e++ = 32; 7481da177e4SLinus Torvalds e += 32; 7491da177e4SLinus Torvalds *e++ = 40; /* NIS Domain name request */ 7501da177e4SLinus Torvalds *e++ = 32; 7511da177e4SLinus Torvalds e += 32; 7521da177e4SLinus Torvalds *e++ = 17; /* Boot path */ 7531da177e4SLinus Torvalds *e++ = 40; 7541da177e4SLinus Torvalds e += 40; 7551da177e4SLinus Torvalds 7561da177e4SLinus Torvalds *e++ = 57; /* set extension buffer size for reply */ 7571da177e4SLinus Torvalds *e++ = 2; 7581da177e4SLinus Torvalds *e++ = 1; /* 128+236+8+20+14, see dhcpd sources */ 7591da177e4SLinus Torvalds *e++ = 150; 7601da177e4SLinus Torvalds 7611da177e4SLinus Torvalds *e++ = 255; /* End of the list */ 7621da177e4SLinus Torvalds } 7631da177e4SLinus Torvalds 7641da177e4SLinus Torvalds 7651da177e4SLinus Torvalds /* 7665e953778SChristoph Fritz * Initialize the DHCP/BOOTP mechanism. 7675e953778SChristoph Fritz */ 7685e953778SChristoph Fritz static inline void __init ic_bootp_init(void) 7695e953778SChristoph Fritz { 770c04d2cb2SChris Novakovic /* Re-initialise all name servers and NTP servers to NONE, in case any 771c04d2cb2SChris Novakovic * were set via the "ip=" or "nfsaddrs=" kernel command line parameters: 772c04d2cb2SChris Novakovic * any IP addresses specified there will already have been decoded but 773c04d2cb2SChris Novakovic * are no longer needed 774300eec7cSChris Novakovic */ 7755e953778SChristoph Fritz ic_nameservers_predef(); 776c04d2cb2SChris Novakovic ic_ntp_servers_predef(); 7771da177e4SLinus Torvalds 7781da177e4SLinus Torvalds dev_add_pack(&bootp_packet_type); 7791da177e4SLinus Torvalds } 7801da177e4SLinus Torvalds 7811da177e4SLinus Torvalds 7821da177e4SLinus Torvalds /* 7831da177e4SLinus Torvalds * DHCP/BOOTP cleanup. 7841da177e4SLinus Torvalds */ 78545e741b8SSam Ravnborg static inline void __init ic_bootp_cleanup(void) 7861da177e4SLinus Torvalds { 7871da177e4SLinus Torvalds dev_remove_pack(&bootp_packet_type); 7881da177e4SLinus Torvalds } 7891da177e4SLinus Torvalds 7901da177e4SLinus Torvalds 7911da177e4SLinus Torvalds /* 7921da177e4SLinus Torvalds * Send DHCP/BOOTP request to single interface. 7931da177e4SLinus Torvalds */ 7941da177e4SLinus Torvalds static void __init ic_bootp_send_if(struct ic_device *d, unsigned long jiffies_diff) 7951da177e4SLinus Torvalds { 7961da177e4SLinus Torvalds struct net_device *dev = d->dev; 7971da177e4SLinus Torvalds struct sk_buff *skb; 7981da177e4SLinus Torvalds struct bootp_pkt *b; 7991da177e4SLinus Torvalds struct iphdr *h; 80066088243SHerbert Xu int hlen = LL_RESERVED_SPACE(dev); 80166088243SHerbert Xu int tlen = dev->needed_tailroom; 8021da177e4SLinus Torvalds 8031da177e4SLinus Torvalds /* Allocate packet */ 80466088243SHerbert Xu skb = alloc_skb(sizeof(struct bootp_pkt) + hlen + tlen + 15, 805f5184d26SJohannes Berg GFP_KERNEL); 8061da177e4SLinus Torvalds if (!skb) 8071da177e4SLinus Torvalds return; 80866088243SHerbert Xu skb_reserve(skb, hlen); 809b080db58SJohannes Berg b = skb_put_zero(skb, sizeof(struct bootp_pkt)); 8101da177e4SLinus Torvalds 8111da177e4SLinus Torvalds /* Construct IP header */ 81204b964dbSArnaldo Carvalho de Melo skb_reset_network_header(skb); 813eddc9ec5SArnaldo Carvalho de Melo h = ip_hdr(skb); 8141da177e4SLinus Torvalds h->version = 4; 8151da177e4SLinus Torvalds h->ihl = 5; 8161da177e4SLinus Torvalds h->tot_len = htons(sizeof(struct bootp_pkt)); 8171da177e4SLinus Torvalds h->frag_off = htons(IP_DF); 8181da177e4SLinus Torvalds h->ttl = 64; 8191da177e4SLinus Torvalds h->protocol = IPPROTO_UDP; 8205a874db4SAl Viro h->daddr = htonl(INADDR_BROADCAST); 8211da177e4SLinus Torvalds h->check = ip_fast_csum((unsigned char *) h, h->ihl); 8221da177e4SLinus Torvalds 8231da177e4SLinus Torvalds /* Construct UDP header */ 8241da177e4SLinus Torvalds b->udph.source = htons(68); 8251da177e4SLinus Torvalds b->udph.dest = htons(67); 8261da177e4SLinus Torvalds b->udph.len = htons(sizeof(struct bootp_pkt) - sizeof(struct iphdr)); 8271da177e4SLinus Torvalds /* UDP checksum not calculated -- explicitly allowed in BOOTP RFC */ 8281da177e4SLinus Torvalds 8291da177e4SLinus Torvalds /* Construct DHCP/BOOTP header */ 8301da177e4SLinus Torvalds b->op = BOOTP_REQUEST; 8311da177e4SLinus Torvalds if (dev->type < 256) /* check for false types */ 8321da177e4SLinus Torvalds b->htype = dev->type; 8331da177e4SLinus Torvalds else if (dev->type == ARPHRD_FDDI) 8341da177e4SLinus Torvalds b->htype = ARPHRD_ETHER; 8351da177e4SLinus Torvalds else { 83609605cc1SBastian Stender pr_warn("Unknown ARP type 0x%04x for device %s\n", dev->type, 83709605cc1SBastian Stender dev->name); 8381da177e4SLinus Torvalds b->htype = dev->type; /* can cause undefined behavior */ 8391da177e4SLinus Torvalds } 840dea75bdfSStephen Hemminger 841dea75bdfSStephen Hemminger /* server_ip and your_ip address are both already zero per RFC2131 */ 8421da177e4SLinus Torvalds b->hlen = dev->addr_len; 8431da177e4SLinus Torvalds memcpy(b->hw_addr, dev->dev_addr, dev->addr_len); 8441da177e4SLinus Torvalds b->secs = htons(jiffies_diff / HZ); 8451da177e4SLinus Torvalds b->xid = d->xid; 8461da177e4SLinus Torvalds 8471da177e4SLinus Torvalds /* add DHCP options or BOOTP extensions */ 8481da177e4SLinus Torvalds #ifdef IPCONFIG_DHCP 8491da177e4SLinus Torvalds if (ic_proto_enabled & IC_USE_DHCP) 85022fc5388SUwe Kleine-König ic_dhcp_init_options(b->exten, d); 8511da177e4SLinus Torvalds else 8521da177e4SLinus Torvalds #endif 8531da177e4SLinus Torvalds ic_bootp_init_ext(b->exten); 8541da177e4SLinus Torvalds 8551da177e4SLinus Torvalds /* Chain packet down the line... */ 8561da177e4SLinus Torvalds skb->dev = dev; 8571da177e4SLinus Torvalds skb->protocol = htons(ETH_P_IP); 8580c4e8581SStephen Hemminger if (dev_hard_header(skb, dev, ntohs(skb->protocol), 859ad79eefcSRongQing.Li dev->broadcast, dev->dev_addr, skb->len) < 0) { 860ad79eefcSRongQing.Li kfree_skb(skb); 861ad79eefcSRongQing.Li printk("E"); 862ad79eefcSRongQing.Li return; 863ad79eefcSRongQing.Li } 864ad79eefcSRongQing.Li 865ad79eefcSRongQing.Li if (dev_queue_xmit(skb) < 0) 8661da177e4SLinus Torvalds printk("E"); 8671da177e4SLinus Torvalds } 8681da177e4SLinus Torvalds 8691da177e4SLinus Torvalds 8701da177e4SLinus Torvalds /* 8711da177e4SLinus Torvalds * Copy BOOTP-supplied string if not already set. 8721da177e4SLinus Torvalds */ 8731da177e4SLinus Torvalds static int __init ic_bootp_string(char *dest, char *src, int len, int max) 8741da177e4SLinus Torvalds { 8751da177e4SLinus Torvalds if (!len) 8761da177e4SLinus Torvalds return 0; 8771da177e4SLinus Torvalds if (len > max-1) 8781da177e4SLinus Torvalds len = max-1; 8791da177e4SLinus Torvalds memcpy(dest, src, len); 8801da177e4SLinus Torvalds dest[len] = '\0'; 8811da177e4SLinus Torvalds return 1; 8821da177e4SLinus Torvalds } 8831da177e4SLinus Torvalds 8841da177e4SLinus Torvalds 8851da177e4SLinus Torvalds /* 8861da177e4SLinus Torvalds * Process BOOTP extensions. 8871da177e4SLinus Torvalds */ 8881da177e4SLinus Torvalds static void __init ic_do_bootp_ext(u8 *ext) 8891da177e4SLinus Torvalds { 8901da177e4SLinus Torvalds u8 servers; 8911da177e4SLinus Torvalds int i; 892747465efSEric Dumazet __be16 mtu; 8931da177e4SLinus Torvalds 8941da177e4SLinus Torvalds u8 *c; 8951da177e4SLinus Torvalds 89609605cc1SBastian Stender pr_debug("DHCP/BOOTP: Got extension %d:", *ext); 8971da177e4SLinus Torvalds for (c=ext+2; c<ext+2+ext[1]; c++) 89809605cc1SBastian Stender pr_debug(" %02x", *c); 89909605cc1SBastian Stender pr_debug("\n"); 9001da177e4SLinus Torvalds 9011da177e4SLinus Torvalds switch (*ext++) { 9021da177e4SLinus Torvalds case 1: /* Subnet mask */ 9035a874db4SAl Viro if (ic_netmask == NONE) 9041da177e4SLinus Torvalds memcpy(&ic_netmask, ext+1, 4); 9051da177e4SLinus Torvalds break; 9061da177e4SLinus Torvalds case 3: /* Default gateway */ 9075a874db4SAl Viro if (ic_gateway == NONE) 9081da177e4SLinus Torvalds memcpy(&ic_gateway, ext+1, 4); 9091da177e4SLinus Torvalds break; 9101da177e4SLinus Torvalds case 6: /* DNS server */ 9111da177e4SLinus Torvalds servers= *ext/4; 9121da177e4SLinus Torvalds if (servers > CONF_NAMESERVERS_MAX) 9131da177e4SLinus Torvalds servers = CONF_NAMESERVERS_MAX; 9141da177e4SLinus Torvalds for (i = 0; i < servers; i++) { 9155a874db4SAl Viro if (ic_nameservers[i] == NONE) 9161da177e4SLinus Torvalds memcpy(&ic_nameservers[i], ext+1+4*i, 4); 9171da177e4SLinus Torvalds } 9181da177e4SLinus Torvalds break; 9191da177e4SLinus Torvalds case 12: /* Host name */ 9201d67a516SJoe Perches ic_bootp_string(utsname()->nodename, ext+1, *ext, 9211d67a516SJoe Perches __NEW_UTS_LEN); 9221da177e4SLinus Torvalds ic_host_name_set = 1; 9231da177e4SLinus Torvalds break; 9241da177e4SLinus Torvalds case 15: /* Domain name (DNS) */ 9251da177e4SLinus Torvalds ic_bootp_string(ic_domain, ext+1, *ext, sizeof(ic_domain)); 9261da177e4SLinus Torvalds break; 9271da177e4SLinus Torvalds case 17: /* Root path */ 9281da177e4SLinus Torvalds if (!root_server_path[0]) 9291d67a516SJoe Perches ic_bootp_string(root_server_path, ext+1, *ext, 9301d67a516SJoe Perches sizeof(root_server_path)); 9311da177e4SLinus Torvalds break; 9329643f455SChris Friesen case 26: /* Interface MTU */ 9339643f455SChris Friesen memcpy(&mtu, ext+1, sizeof(mtu)); 9349643f455SChris Friesen ic_dev_mtu = ntohs(mtu); 9359643f455SChris Friesen break; 9361da177e4SLinus Torvalds case 40: /* NIS Domain name (_not_ DNS) */ 9371d67a516SJoe Perches ic_bootp_string(utsname()->domainname, ext+1, *ext, 9381d67a516SJoe Perches __NEW_UTS_LEN); 9391da177e4SLinus Torvalds break; 940c04d2cb2SChris Novakovic case 42: /* NTP servers */ 941c04d2cb2SChris Novakovic servers = *ext / 4; 942c04d2cb2SChris Novakovic if (servers > CONF_NTP_SERVERS_MAX) 943c04d2cb2SChris Novakovic servers = CONF_NTP_SERVERS_MAX; 944c04d2cb2SChris Novakovic for (i = 0; i < servers; i++) { 945c04d2cb2SChris Novakovic if (ic_ntp_servers[i] == NONE) 946c04d2cb2SChris Novakovic memcpy(&ic_ntp_servers[i], ext+1+4*i, 4); 947c04d2cb2SChris Novakovic } 948c04d2cb2SChris Novakovic break; 9491da177e4SLinus Torvalds } 9501da177e4SLinus Torvalds } 9511da177e4SLinus Torvalds 9521da177e4SLinus Torvalds 9531da177e4SLinus Torvalds /* 9541da177e4SLinus Torvalds * Receive BOOTP reply. 9551da177e4SLinus Torvalds */ 956f2ccd8faSDavid 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) 9571da177e4SLinus Torvalds { 9581da177e4SLinus Torvalds struct bootp_pkt *b; 9591da177e4SLinus Torvalds struct iphdr *h; 9601da177e4SLinus Torvalds struct ic_device *d; 9611da177e4SLinus Torvalds int len, ext_len; 9621da177e4SLinus Torvalds 963721499e8SYOSHIFUJI Hideaki if (!net_eq(dev_net(dev), &init_net)) 964e730c155SEric W. Biederman goto drop; 965e730c155SEric W. Biederman 9661da177e4SLinus Torvalds /* Perform verifications before taking the lock. */ 9671da177e4SLinus Torvalds if (skb->pkt_type == PACKET_OTHERHOST) 9681da177e4SLinus Torvalds goto drop; 9691da177e4SLinus Torvalds 97051456b29SIan Morris skb = skb_share_check(skb, GFP_ATOMIC); 97151456b29SIan Morris if (!skb) 9721da177e4SLinus Torvalds return NET_RX_DROP; 9731da177e4SLinus Torvalds 9741da177e4SLinus Torvalds if (!pskb_may_pull(skb, 9751da177e4SLinus Torvalds sizeof(struct iphdr) + 9761da177e4SLinus Torvalds sizeof(struct udphdr))) 9771da177e4SLinus Torvalds goto drop; 9781da177e4SLinus Torvalds 979eddc9ec5SArnaldo Carvalho de Melo b = (struct bootp_pkt *)skb_network_header(skb); 9801da177e4SLinus Torvalds h = &b->iph; 9811da177e4SLinus Torvalds 9821da177e4SLinus Torvalds if (h->ihl != 5 || h->version != 4 || h->protocol != IPPROTO_UDP) 9831da177e4SLinus Torvalds goto drop; 9841da177e4SLinus Torvalds 9851da177e4SLinus Torvalds /* Fragments are not supported */ 98656f8a75cSPaul Gortmaker if (ip_is_fragment(h)) { 987e87cc472SJoe Perches net_err_ratelimited("DHCP/BOOTP: Ignoring fragmented reply\n"); 9881da177e4SLinus Torvalds goto drop; 9891da177e4SLinus Torvalds } 9901da177e4SLinus Torvalds 9911da177e4SLinus Torvalds if (skb->len < ntohs(h->tot_len)) 9921da177e4SLinus Torvalds goto drop; 9931da177e4SLinus Torvalds 9941da177e4SLinus Torvalds if (ip_fast_csum((char *) h, h->ihl)) 9951da177e4SLinus Torvalds goto drop; 9961da177e4SLinus Torvalds 9971da177e4SLinus Torvalds if (b->udph.source != htons(67) || b->udph.dest != htons(68)) 9981da177e4SLinus Torvalds goto drop; 9991da177e4SLinus Torvalds 10001da177e4SLinus Torvalds if (ntohs(h->tot_len) < ntohs(b->udph.len) + sizeof(struct iphdr)) 10011da177e4SLinus Torvalds goto drop; 10021da177e4SLinus Torvalds 10031da177e4SLinus Torvalds len = ntohs(b->udph.len) - sizeof(struct udphdr); 10041da177e4SLinus Torvalds ext_len = len - (sizeof(*b) - 10051da177e4SLinus Torvalds sizeof(struct iphdr) - 10061da177e4SLinus Torvalds sizeof(struct udphdr) - 10071da177e4SLinus Torvalds sizeof(b->exten)); 10081da177e4SLinus Torvalds if (ext_len < 0) 10091da177e4SLinus Torvalds goto drop; 10101da177e4SLinus Torvalds 10111da177e4SLinus Torvalds /* Ok the front looks good, make sure we can get at the rest. */ 10121da177e4SLinus Torvalds if (!pskb_may_pull(skb, skb->len)) 10131da177e4SLinus Torvalds goto drop; 10141da177e4SLinus Torvalds 1015eddc9ec5SArnaldo Carvalho de Melo b = (struct bootp_pkt *)skb_network_header(skb); 10161da177e4SLinus Torvalds h = &b->iph; 10171da177e4SLinus Torvalds 10181da177e4SLinus Torvalds /* One reply at a time, please. */ 10191da177e4SLinus Torvalds spin_lock(&ic_recv_lock); 10201da177e4SLinus Torvalds 10211da177e4SLinus Torvalds /* If we already have a reply, just drop the packet */ 10221da177e4SLinus Torvalds if (ic_got_reply) 10231da177e4SLinus Torvalds goto drop_unlock; 10241da177e4SLinus Torvalds 10251da177e4SLinus Torvalds /* Find the ic_device that the packet arrived on */ 10261da177e4SLinus Torvalds d = ic_first_dev; 10271da177e4SLinus Torvalds while (d && d->dev != dev) 10281da177e4SLinus Torvalds d = d->next; 10291da177e4SLinus Torvalds if (!d) 10301da177e4SLinus Torvalds goto drop_unlock; /* should never happen */ 10311da177e4SLinus Torvalds 10321da177e4SLinus Torvalds /* Is it a reply to our BOOTP request? */ 10331da177e4SLinus Torvalds if (b->op != BOOTP_REPLY || 10341da177e4SLinus Torvalds b->xid != d->xid) { 103522fc5388SUwe Kleine-König net_err_ratelimited("DHCP/BOOTP: Reply not for us on %s, op[%x] xid[%x]\n", 103622fc5388SUwe Kleine-König d->dev->name, b->op, b->xid); 10371da177e4SLinus Torvalds goto drop_unlock; 10381da177e4SLinus Torvalds } 10391da177e4SLinus Torvalds 10401da177e4SLinus Torvalds /* Parse extensions */ 10411da177e4SLinus Torvalds if (ext_len >= 4 && 10421da177e4SLinus Torvalds !memcmp(b->exten, ic_bootp_cookie, 4)) { /* Check magic cookie */ 10431da177e4SLinus Torvalds u8 *end = (u8 *) b + ntohs(b->iph.tot_len); 10441da177e4SLinus Torvalds u8 *ext; 10451da177e4SLinus Torvalds 10461da177e4SLinus Torvalds #ifdef IPCONFIG_DHCP 10471da177e4SLinus Torvalds if (ic_proto_enabled & IC_USE_DHCP) { 10485a874db4SAl Viro __be32 server_id = NONE; 10491da177e4SLinus Torvalds int mt = 0; 10501da177e4SLinus Torvalds 10511da177e4SLinus Torvalds ext = &b->exten[4]; 10521da177e4SLinus Torvalds while (ext < end && *ext != 0xff) { 10531da177e4SLinus Torvalds u8 *opt = ext++; 10541da177e4SLinus Torvalds if (*opt == 0) /* Padding */ 10551da177e4SLinus Torvalds continue; 10561da177e4SLinus Torvalds ext += *ext + 1; 10571da177e4SLinus Torvalds if (ext >= end) 10581da177e4SLinus Torvalds break; 10591da177e4SLinus Torvalds switch (*opt) { 10601da177e4SLinus Torvalds case 53: /* Message type */ 10611da177e4SLinus Torvalds if (opt[1]) 10621da177e4SLinus Torvalds mt = opt[2]; 10631da177e4SLinus Torvalds break; 10641da177e4SLinus Torvalds case 54: /* Server ID (IP address) */ 10651da177e4SLinus Torvalds if (opt[1] >= 4) 10661da177e4SLinus Torvalds memcpy(&server_id, opt + 2, 4); 10671da177e4SLinus Torvalds break; 10683ff50b79SStephen Hemminger } 10691da177e4SLinus Torvalds } 10701da177e4SLinus Torvalds 107122fc5388SUwe Kleine-König pr_debug("DHCP: Got message type %d (%s)\n", mt, d->dev->name); 10721da177e4SLinus Torvalds 10731da177e4SLinus Torvalds switch (mt) { 10741da177e4SLinus Torvalds case DHCPOFFER: 10751da177e4SLinus Torvalds /* While in the process of accepting one offer, 10761da177e4SLinus Torvalds * ignore all others. 10771da177e4SLinus Torvalds */ 10785a874db4SAl Viro if (ic_myaddr != NONE) 10791da177e4SLinus Torvalds goto drop_unlock; 10801da177e4SLinus Torvalds 10811da177e4SLinus Torvalds /* Let's accept that offer. */ 10821da177e4SLinus Torvalds ic_myaddr = b->your_ip; 10831da177e4SLinus Torvalds ic_servaddr = server_id; 108409605cc1SBastian Stender pr_debug("DHCP: Offered address %pI4 by server %pI4\n", 10859dd4a13aSPhilippe De Muyter &ic_myaddr, &b->iph.saddr); 10861da177e4SLinus Torvalds /* The DHCP indicated server address takes 10871da177e4SLinus Torvalds * precedence over the bootp header one if 10881da177e4SLinus Torvalds * they are different. 10891da177e4SLinus Torvalds */ 10905a874db4SAl Viro if ((server_id != NONE) && 10911da177e4SLinus Torvalds (b->server_ip != server_id)) 10921da177e4SLinus Torvalds b->server_ip = ic_servaddr; 10931da177e4SLinus Torvalds break; 10941da177e4SLinus Torvalds 10951da177e4SLinus Torvalds case DHCPACK: 10961da177e4SLinus Torvalds if (memcmp(dev->dev_addr, b->hw_addr, dev->addr_len) != 0) 10971da177e4SLinus Torvalds goto drop_unlock; 10981da177e4SLinus Torvalds 10991da177e4SLinus Torvalds /* Yeah! */ 11001da177e4SLinus Torvalds break; 11011da177e4SLinus Torvalds 11021da177e4SLinus Torvalds default: 11031da177e4SLinus Torvalds /* Urque. Forget it*/ 11045a874db4SAl Viro ic_myaddr = NONE; 11055a874db4SAl Viro ic_servaddr = NONE; 11061da177e4SLinus Torvalds goto drop_unlock; 11073ff50b79SStephen Hemminger } 11081da177e4SLinus Torvalds 11091da177e4SLinus Torvalds ic_dhcp_msgtype = mt; 11101da177e4SLinus Torvalds 11111da177e4SLinus Torvalds } 11121da177e4SLinus Torvalds #endif /* IPCONFIG_DHCP */ 11131da177e4SLinus Torvalds 11141da177e4SLinus Torvalds ext = &b->exten[4]; 11151da177e4SLinus Torvalds while (ext < end && *ext != 0xff) { 11161da177e4SLinus Torvalds u8 *opt = ext++; 11171da177e4SLinus Torvalds if (*opt == 0) /* Padding */ 11181da177e4SLinus Torvalds continue; 11191da177e4SLinus Torvalds ext += *ext + 1; 11201da177e4SLinus Torvalds if (ext < end) 11211da177e4SLinus Torvalds ic_do_bootp_ext(opt); 11221da177e4SLinus Torvalds } 11231da177e4SLinus Torvalds } 11241da177e4SLinus Torvalds 11251da177e4SLinus Torvalds /* We have a winner! */ 11262647cffbSUwe Kleine-König ic_dev = d; 11271da177e4SLinus Torvalds ic_myaddr = b->your_ip; 11281da177e4SLinus Torvalds ic_servaddr = b->server_ip; 11299dd4a13aSPhilippe De Muyter ic_addrservaddr = b->iph.saddr; 11305a874db4SAl Viro if (ic_gateway == NONE && b->relay_ip) 11311da177e4SLinus Torvalds ic_gateway = b->relay_ip; 11325a874db4SAl Viro if (ic_nameservers[0] == NONE) 11331da177e4SLinus Torvalds ic_nameservers[0] = ic_servaddr; 11341da177e4SLinus Torvalds ic_got_reply = IC_BOOTP; 11351da177e4SLinus Torvalds 11361da177e4SLinus Torvalds drop_unlock: 11371da177e4SLinus Torvalds /* Show's over. Nothing to see here. */ 11381da177e4SLinus Torvalds spin_unlock(&ic_recv_lock); 11391da177e4SLinus Torvalds 11401da177e4SLinus Torvalds drop: 11411da177e4SLinus Torvalds /* Throw the packet out. */ 11421da177e4SLinus Torvalds kfree_skb(skb); 11431da177e4SLinus Torvalds 11441da177e4SLinus Torvalds return 0; 11451da177e4SLinus Torvalds } 11461da177e4SLinus Torvalds 11471da177e4SLinus Torvalds 11481da177e4SLinus Torvalds #endif 11491da177e4SLinus Torvalds 11501da177e4SLinus Torvalds 11511da177e4SLinus Torvalds /* 11521da177e4SLinus Torvalds * Dynamic IP configuration -- DHCP, BOOTP, RARP. 11531da177e4SLinus Torvalds */ 11541da177e4SLinus Torvalds 11551da177e4SLinus Torvalds #ifdef IPCONFIG_DYNAMIC 11561da177e4SLinus Torvalds 11571da177e4SLinus Torvalds static int __init ic_dynamic(void) 11581da177e4SLinus Torvalds { 11591da177e4SLinus Torvalds int retries; 11601da177e4SLinus Torvalds struct ic_device *d; 11611da177e4SLinus Torvalds unsigned long start_jiffies, timeout, jiff; 11621da177e4SLinus Torvalds int do_bootp = ic_proto_have_if & IC_BOOTP; 11631da177e4SLinus Torvalds int do_rarp = ic_proto_have_if & IC_RARP; 11641da177e4SLinus Torvalds 11651da177e4SLinus Torvalds /* 11661da177e4SLinus Torvalds * If none of DHCP/BOOTP/RARP was selected, return with an error. 11671da177e4SLinus Torvalds * This routine gets only called when some pieces of information 11681da177e4SLinus Torvalds * are missing, and without DHCP/BOOTP/RARP we are unable to get it. 11691da177e4SLinus Torvalds */ 11701da177e4SLinus Torvalds if (!ic_proto_enabled) { 1171058bd4d2SJoe Perches pr_err("IP-Config: Incomplete network configuration information\n"); 11721da177e4SLinus Torvalds return -1; 11731da177e4SLinus Torvalds } 11741da177e4SLinus Torvalds 11751da177e4SLinus Torvalds #ifdef IPCONFIG_BOOTP 11761da177e4SLinus Torvalds if ((ic_proto_enabled ^ ic_proto_have_if) & IC_BOOTP) 1177058bd4d2SJoe Perches pr_err("DHCP/BOOTP: No suitable device found\n"); 11781da177e4SLinus Torvalds #endif 11791da177e4SLinus Torvalds #ifdef IPCONFIG_RARP 11801da177e4SLinus Torvalds if ((ic_proto_enabled ^ ic_proto_have_if) & IC_RARP) 1181058bd4d2SJoe Perches pr_err("RARP: No suitable device found\n"); 11821da177e4SLinus Torvalds #endif 11831da177e4SLinus Torvalds 11841da177e4SLinus Torvalds if (!ic_proto_have_if) 11851da177e4SLinus Torvalds /* Error message already printed */ 11861da177e4SLinus Torvalds return -1; 11871da177e4SLinus Torvalds 11881da177e4SLinus Torvalds /* 11891da177e4SLinus Torvalds * Setup protocols 11901da177e4SLinus Torvalds */ 11911da177e4SLinus Torvalds #ifdef IPCONFIG_BOOTP 11921da177e4SLinus Torvalds if (do_bootp) 11931da177e4SLinus Torvalds ic_bootp_init(); 11941da177e4SLinus Torvalds #endif 11951da177e4SLinus Torvalds #ifdef IPCONFIG_RARP 11961da177e4SLinus Torvalds if (do_rarp) 11971da177e4SLinus Torvalds ic_rarp_init(); 11981da177e4SLinus Torvalds #endif 11991da177e4SLinus Torvalds 12001da177e4SLinus Torvalds /* 12011da177e4SLinus Torvalds * Send requests and wait, until we get an answer. This loop 12021da177e4SLinus Torvalds * seems to be a terrible waste of CPU time, but actually there is 12031da177e4SLinus Torvalds * only one process running at all, so we don't need to use any 12041da177e4SLinus Torvalds * scheduler functions. 12051da177e4SLinus Torvalds * [Actually we could now, but the nothing else running note still 12061da177e4SLinus Torvalds * applies.. - AC] 12071da177e4SLinus Torvalds */ 1208058bd4d2SJoe Perches pr_notice("Sending %s%s%s requests .", 12091da177e4SLinus Torvalds do_bootp 12101da177e4SLinus Torvalds ? ((ic_proto_enabled & IC_USE_DHCP) ? "DHCP" : "BOOTP") : "", 12111da177e4SLinus Torvalds (do_bootp && do_rarp) ? " and " : "", 12121da177e4SLinus Torvalds do_rarp ? "RARP" : ""); 12131da177e4SLinus Torvalds 12141da177e4SLinus Torvalds start_jiffies = jiffies; 12151da177e4SLinus Torvalds d = ic_first_dev; 12161da177e4SLinus Torvalds retries = CONF_SEND_RETRIES; 12171da177e4SLinus Torvalds get_random_bytes(&timeout, sizeof(timeout)); 121895c96174SEric Dumazet timeout = CONF_BASE_TIMEOUT + (timeout % (unsigned int) CONF_TIMEOUT_RANDOM); 12191da177e4SLinus Torvalds for (;;) { 1220405fd707SDavid S. Miller #ifdef IPCONFIG_BOOTP 12211da177e4SLinus Torvalds if (do_bootp && (d->able & IC_BOOTP)) 12221da177e4SLinus Torvalds ic_bootp_send_if(d, jiffies - start_jiffies); 12231da177e4SLinus Torvalds #endif 12241da177e4SLinus Torvalds #ifdef IPCONFIG_RARP 12251da177e4SLinus Torvalds if (do_rarp && (d->able & IC_RARP)) 12261da177e4SLinus Torvalds ic_rarp_send_if(d); 12271da177e4SLinus Torvalds #endif 12281da177e4SLinus Torvalds 1229e0688534SUwe Kleine-König if (!d->next) { 1230e0688534SUwe Kleine-König jiff = jiffies + timeout; 1231121caf57SNishanth Aravamudan while (time_before(jiffies, jiff) && !ic_got_reply) 1232121caf57SNishanth Aravamudan schedule_timeout_uninterruptible(1); 1233e0688534SUwe Kleine-König } 12341da177e4SLinus Torvalds #ifdef IPCONFIG_DHCP 12351da177e4SLinus Torvalds /* DHCP isn't done until we get a DHCPACK. */ 12369d4fb27dSJoe Perches if ((ic_got_reply & IC_BOOTP) && 12379d4fb27dSJoe Perches (ic_proto_enabled & IC_USE_DHCP) && 12389d4fb27dSJoe Perches ic_dhcp_msgtype != DHCPACK) { 12391da177e4SLinus Torvalds ic_got_reply = 0; 12402647cffbSUwe Kleine-König /* continue on device that got the reply */ 12412647cffbSUwe Kleine-König d = ic_dev; 12426c1c36b0SGeert Uytterhoeven pr_cont(","); 12431da177e4SLinus Torvalds continue; 12441da177e4SLinus Torvalds } 12451da177e4SLinus Torvalds #endif /* IPCONFIG_DHCP */ 12461da177e4SLinus Torvalds 12471da177e4SLinus Torvalds if (ic_got_reply) { 12486c1c36b0SGeert Uytterhoeven pr_cont(" OK\n"); 12491da177e4SLinus Torvalds break; 12501da177e4SLinus Torvalds } 12511da177e4SLinus Torvalds 12521da177e4SLinus Torvalds if ((d = d->next)) 12531da177e4SLinus Torvalds continue; 12541da177e4SLinus Torvalds 12551da177e4SLinus Torvalds if (! --retries) { 12566c1c36b0SGeert Uytterhoeven pr_cont(" timed out!\n"); 12571da177e4SLinus Torvalds break; 12581da177e4SLinus Torvalds } 12591da177e4SLinus Torvalds 12601da177e4SLinus Torvalds d = ic_first_dev; 12611da177e4SLinus Torvalds 12621da177e4SLinus Torvalds timeout = timeout CONF_TIMEOUT_MULT; 12631da177e4SLinus Torvalds if (timeout > CONF_TIMEOUT_MAX) 12641da177e4SLinus Torvalds timeout = CONF_TIMEOUT_MAX; 12651da177e4SLinus Torvalds 12666c1c36b0SGeert Uytterhoeven pr_cont("."); 12671da177e4SLinus Torvalds } 12681da177e4SLinus Torvalds 12691da177e4SLinus Torvalds #ifdef IPCONFIG_BOOTP 12701da177e4SLinus Torvalds if (do_bootp) 12711da177e4SLinus Torvalds ic_bootp_cleanup(); 12721da177e4SLinus Torvalds #endif 12731da177e4SLinus Torvalds #ifdef IPCONFIG_RARP 12741da177e4SLinus Torvalds if (do_rarp) 12751da177e4SLinus Torvalds ic_rarp_cleanup(); 12761da177e4SLinus Torvalds #endif 12771da177e4SLinus Torvalds 12787a1af5d7SMaxime Bizon if (!ic_got_reply) { 12795a874db4SAl Viro ic_myaddr = NONE; 12801da177e4SLinus Torvalds return -1; 12817a1af5d7SMaxime Bizon } 12821da177e4SLinus Torvalds 12836c1c36b0SGeert Uytterhoeven pr_info("IP-Config: Got %s answer from %pI4, my address is %pI4\n", 12841da177e4SLinus Torvalds ((ic_got_reply & IC_RARP) ? "RARP" 12851da177e4SLinus Torvalds : (ic_proto_enabled & IC_USE_DHCP) ? "DHCP" : "BOOTP"), 12866c1c36b0SGeert Uytterhoeven &ic_addrservaddr, &ic_myaddr); 12871da177e4SLinus Torvalds 12881da177e4SLinus Torvalds return 0; 12891da177e4SLinus Torvalds } 12901da177e4SLinus Torvalds 12911da177e4SLinus Torvalds #endif /* IPCONFIG_DYNAMIC */ 12921da177e4SLinus Torvalds 12931da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 1294289e1f4eSAnders Roxell /* proc_dir_entry for /proc/net/ipconfig */ 1295289e1f4eSAnders Roxell static struct proc_dir_entry *ipconfig_dir; 12961da177e4SLinus Torvalds 1297c04d2cb2SChris Novakovic /* Name servers: */ 12981da177e4SLinus Torvalds static int pnp_seq_show(struct seq_file *seq, void *v) 12991da177e4SLinus Torvalds { 13001da177e4SLinus Torvalds int i; 13011da177e4SLinus Torvalds 13021da177e4SLinus Torvalds if (ic_proto_used & IC_PROTO) 13031da177e4SLinus Torvalds seq_printf(seq, "#PROTO: %s\n", 13041da177e4SLinus Torvalds (ic_proto_used & IC_RARP) ? "RARP" 13051da177e4SLinus Torvalds : (ic_proto_used & IC_USE_DHCP) ? "DHCP" : "BOOTP"); 13061da177e4SLinus Torvalds else 13071da177e4SLinus Torvalds seq_puts(seq, "#MANUAL\n"); 13081da177e4SLinus Torvalds 13091da177e4SLinus Torvalds if (ic_domain[0]) 13101da177e4SLinus Torvalds seq_printf(seq, 13111da177e4SLinus Torvalds "domain %s\n", ic_domain); 13121da177e4SLinus Torvalds for (i = 0; i < CONF_NAMESERVERS_MAX; i++) { 13135a874db4SAl Viro if (ic_nameservers[i] != NONE) 1314673d57e7SHarvey Harrison seq_printf(seq, "nameserver %pI4\n", 1315673d57e7SHarvey Harrison &ic_nameservers[i]); 13161da177e4SLinus Torvalds } 13175a874db4SAl Viro if (ic_servaddr != NONE) 1318673d57e7SHarvey Harrison seq_printf(seq, "bootserver %pI4\n", 1319673d57e7SHarvey Harrison &ic_servaddr); 13201da177e4SLinus Torvalds return 0; 13211da177e4SLinus Torvalds } 13221da177e4SLinus Torvalds 13234d019b3fSChris Novakovic /* Create the /proc/net/ipconfig directory */ 1324c04d2cb2SChris Novakovic static int __init ipconfig_proc_net_init(void) 13254d019b3fSChris Novakovic { 13264d019b3fSChris Novakovic ipconfig_dir = proc_net_mkdir(&init_net, "ipconfig", init_net.proc_net); 13274d019b3fSChris Novakovic if (!ipconfig_dir) 13284d019b3fSChris Novakovic return -ENOMEM; 13294d019b3fSChris Novakovic 13304d019b3fSChris Novakovic return 0; 13314d019b3fSChris Novakovic } 1332c04d2cb2SChris Novakovic 1333c04d2cb2SChris Novakovic /* Create a new file under /proc/net/ipconfig */ 1334c04d2cb2SChris Novakovic static int ipconfig_proc_net_create(const char *name, 1335c04d2cb2SChris Novakovic const struct file_operations *fops) 1336c04d2cb2SChris Novakovic { 1337c04d2cb2SChris Novakovic char *pname; 1338c04d2cb2SChris Novakovic struct proc_dir_entry *p; 1339c04d2cb2SChris Novakovic 1340c04d2cb2SChris Novakovic if (!ipconfig_dir) 1341c04d2cb2SChris Novakovic return -ENOMEM; 1342c04d2cb2SChris Novakovic 1343c04d2cb2SChris Novakovic pname = kasprintf(GFP_KERNEL, "%s%s", "ipconfig/", name); 1344c04d2cb2SChris Novakovic if (!pname) 1345c04d2cb2SChris Novakovic return -ENOMEM; 1346c04d2cb2SChris Novakovic 1347c04d2cb2SChris Novakovic p = proc_create(pname, 0444, init_net.proc_net, fops); 1348c04d2cb2SChris Novakovic kfree(pname); 1349c04d2cb2SChris Novakovic if (!p) 1350c04d2cb2SChris Novakovic return -ENOMEM; 1351c04d2cb2SChris Novakovic 1352c04d2cb2SChris Novakovic return 0; 1353c04d2cb2SChris Novakovic } 1354c04d2cb2SChris Novakovic 1355c04d2cb2SChris Novakovic /* Write NTP server IP addresses to /proc/net/ipconfig/ntp_servers */ 1356c04d2cb2SChris Novakovic static int ntp_servers_seq_show(struct seq_file *seq, void *v) 1357c04d2cb2SChris Novakovic { 1358c04d2cb2SChris Novakovic int i; 1359c04d2cb2SChris Novakovic 1360c04d2cb2SChris Novakovic for (i = 0; i < CONF_NTP_SERVERS_MAX; i++) { 1361c04d2cb2SChris Novakovic if (ic_ntp_servers[i] != NONE) 1362c04d2cb2SChris Novakovic seq_printf(seq, "%pI4\n", &ic_ntp_servers[i]); 1363c04d2cb2SChris Novakovic } 1364c04d2cb2SChris Novakovic return 0; 1365c04d2cb2SChris Novakovic } 1366c04d2cb2SChris Novakovic 1367c04d2cb2SChris Novakovic static int ntp_servers_seq_open(struct inode *inode, struct file *file) 1368c04d2cb2SChris Novakovic { 1369c04d2cb2SChris Novakovic return single_open(file, ntp_servers_seq_show, NULL); 1370c04d2cb2SChris Novakovic } 1371c04d2cb2SChris Novakovic 1372c04d2cb2SChris Novakovic static const struct file_operations ntp_servers_seq_fops = { 1373c04d2cb2SChris Novakovic .open = ntp_servers_seq_open, 1374c04d2cb2SChris Novakovic .read = seq_read, 1375c04d2cb2SChris Novakovic .llseek = seq_lseek, 1376c04d2cb2SChris Novakovic .release = single_release, 1377c04d2cb2SChris Novakovic }; 13781da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */ 13791da177e4SLinus Torvalds 13801da177e4SLinus Torvalds /* 13811da177e4SLinus Torvalds * Extract IP address from the parameter string if needed. Note that we 13821da177e4SLinus Torvalds * need to have root_server_addr set _before_ IPConfig gets called as it 13831da177e4SLinus Torvalds * can override it. 13841da177e4SLinus Torvalds */ 13855a874db4SAl Viro __be32 __init root_nfs_parse_addr(char *name) 13861da177e4SLinus Torvalds { 13875a874db4SAl Viro __be32 addr; 13881da177e4SLinus Torvalds int octets = 0; 13891da177e4SLinus Torvalds char *cp, *cq; 13901da177e4SLinus Torvalds 13911da177e4SLinus Torvalds cp = cq = name; 13921da177e4SLinus Torvalds while (octets < 4) { 13931da177e4SLinus Torvalds while (*cp >= '0' && *cp <= '9') 13941da177e4SLinus Torvalds cp++; 13951da177e4SLinus Torvalds if (cp == cq || cp - cq > 3) 13961da177e4SLinus Torvalds break; 13971da177e4SLinus Torvalds if (*cp == '.' || octets == 3) 13981da177e4SLinus Torvalds octets++; 13991da177e4SLinus Torvalds if (octets < 4) 14001da177e4SLinus Torvalds cp++; 14011da177e4SLinus Torvalds cq = cp; 14021da177e4SLinus Torvalds } 14031da177e4SLinus Torvalds if (octets == 4 && (*cp == ':' || *cp == '\0')) { 14041da177e4SLinus Torvalds if (*cp == ':') 14051da177e4SLinus Torvalds *cp++ = '\0'; 14061da177e4SLinus Torvalds addr = in_aton(name); 14071da177e4SLinus Torvalds memmove(name, cp, strlen(cp) + 1); 14081da177e4SLinus Torvalds } else 14095a874db4SAl Viro addr = NONE; 14101da177e4SLinus Torvalds 14111da177e4SLinus Torvalds return addr; 14121da177e4SLinus Torvalds } 14131da177e4SLinus Torvalds 1414964ad81cSDavid S. Miller #define DEVICE_WAIT_MAX 12 /* 12 seconds */ 1415964ad81cSDavid S. Miller 1416964ad81cSDavid S. Miller static int __init wait_for_devices(void) 1417964ad81cSDavid S. Miller { 1418964ad81cSDavid S. Miller int i; 1419964ad81cSDavid S. Miller 1420964ad81cSDavid S. Miller for (i = 0; i < DEVICE_WAIT_MAX; i++) { 1421964ad81cSDavid S. Miller struct net_device *dev; 1422964ad81cSDavid S. Miller int found = 0; 1423964ad81cSDavid S. Miller 1424964ad81cSDavid S. Miller rtnl_lock(); 1425964ad81cSDavid S. Miller for_each_netdev(&init_net, dev) { 14263fb72f1eSMicha Nelissen if (ic_is_init_dev(dev)) { 1427964ad81cSDavid S. Miller found = 1; 1428964ad81cSDavid S. Miller break; 1429964ad81cSDavid S. Miller } 1430964ad81cSDavid S. Miller } 1431964ad81cSDavid S. Miller rtnl_unlock(); 1432964ad81cSDavid S. Miller if (found) 1433964ad81cSDavid S. Miller return 0; 1434964ad81cSDavid S. Miller ssleep(1); 1435964ad81cSDavid S. Miller } 1436964ad81cSDavid S. Miller return -ENODEV; 1437964ad81cSDavid S. Miller } 1438964ad81cSDavid S. Miller 14391da177e4SLinus Torvalds /* 14401da177e4SLinus Torvalds * IP Autoconfig dispatcher. 14411da177e4SLinus Torvalds */ 14421da177e4SLinus Torvalds 14431da177e4SLinus Torvalds static int __init ip_auto_config(void) 14441da177e4SLinus Torvalds { 14455a874db4SAl Viro __be32 addr; 14469d8dba6cSBenjamin Zores #ifdef IPCONFIG_DYNAMIC 14479d8dba6cSBenjamin Zores int retries = CONF_OPEN_RETRIES; 14489d8dba6cSBenjamin Zores #endif 1449964ad81cSDavid S. Miller int err; 14505e953778SChristoph Fritz unsigned int i; 14511da177e4SLinus Torvalds 1452c04d2cb2SChris Novakovic /* Initialise all name servers and NTP servers to NONE (but only if the 1453c04d2cb2SChris Novakovic * "ip=" or "nfsaddrs=" kernel command line parameters weren't decoded, 1454c04d2cb2SChris Novakovic * otherwise we'll overwrite the IP addresses specified there) 1455300eec7cSChris Novakovic */ 1456c04d2cb2SChris Novakovic if (ic_set_manually == 0) { 1457300eec7cSChris Novakovic ic_nameservers_predef(); 1458c04d2cb2SChris Novakovic ic_ntp_servers_predef(); 1459c04d2cb2SChris Novakovic } 1460300eec7cSChris Novakovic 14611da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 14623f3942acSChristoph Hellwig proc_create_single("pnp", 0444, init_net.proc_net, pnp_seq_show); 14634d019b3fSChris Novakovic 1464c04d2cb2SChris Novakovic if (ipconfig_proc_net_init() == 0) 1465c04d2cb2SChris Novakovic ipconfig_proc_net_create("ntp_servers", &ntp_servers_seq_fops); 14661da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */ 14671da177e4SLinus Torvalds 14681da177e4SLinus Torvalds if (!ic_enable) 14691da177e4SLinus Torvalds return 0; 14701da177e4SLinus Torvalds 147109605cc1SBastian Stender pr_debug("IP-Config: Entered.\n"); 14721da177e4SLinus Torvalds #ifdef IPCONFIG_DYNAMIC 14731da177e4SLinus Torvalds try_try_again: 14741da177e4SLinus Torvalds #endif 1475964ad81cSDavid S. Miller /* Wait for devices to appear */ 1476964ad81cSDavid S. Miller err = wait_for_devices(); 1477964ad81cSDavid S. Miller if (err) 1478964ad81cSDavid S. Miller return err; 14791da177e4SLinus Torvalds 14801da177e4SLinus Torvalds /* Setup all network devices */ 1481964ad81cSDavid S. Miller err = ic_open_devs(); 1482964ad81cSDavid S. Miller if (err) 1483964ad81cSDavid S. Miller return err; 14841da177e4SLinus Torvalds 14851da177e4SLinus Torvalds /* Give drivers a chance to settle */ 14863fb72f1eSMicha Nelissen msleep(CONF_POST_OPEN); 14871da177e4SLinus Torvalds 14881da177e4SLinus Torvalds /* 14891da177e4SLinus Torvalds * If the config information is insufficient (e.g., our IP address or 14901da177e4SLinus Torvalds * IP address of the boot server is missing or we have multiple network 14911da177e4SLinus Torvalds * interfaces and no default was set), use BOOTP or RARP to get the 14921da177e4SLinus Torvalds * missing values. 14931da177e4SLinus Torvalds */ 14945a874db4SAl Viro if (ic_myaddr == NONE || 14951da177e4SLinus Torvalds #ifdef CONFIG_ROOT_NFS 14969d4fb27dSJoe Perches (root_server_addr == NONE && 14979d4fb27dSJoe Perches ic_servaddr == NONE && 14989d4fb27dSJoe Perches ROOT_DEV == Root_NFS) || 14991da177e4SLinus Torvalds #endif 15001da177e4SLinus Torvalds ic_first_dev->next) { 15011da177e4SLinus Torvalds #ifdef IPCONFIG_DYNAMIC 15021da177e4SLinus Torvalds if (ic_dynamic() < 0) { 15031da177e4SLinus Torvalds ic_close_devs(); 15041da177e4SLinus Torvalds 15051da177e4SLinus Torvalds /* 15061da177e4SLinus Torvalds * I don't know why, but sometimes the 15071da177e4SLinus Torvalds * eepro100 driver (at least) gets upset and 15081da177e4SLinus Torvalds * doesn't work the first time it's opened. 15091da177e4SLinus Torvalds * But then if you close it and reopen it, it 15101da177e4SLinus Torvalds * works just fine. So we need to try that at 15111da177e4SLinus Torvalds * least once before giving up. 15121da177e4SLinus Torvalds * 15131da177e4SLinus Torvalds * Also, if the root will be NFS-mounted, we 15141da177e4SLinus Torvalds * have nowhere to go if DHCP fails. So we 15151da177e4SLinus Torvalds * just have to keep trying forever. 15161da177e4SLinus Torvalds * 15171da177e4SLinus Torvalds * -- Chip 15181da177e4SLinus Torvalds */ 15191da177e4SLinus Torvalds #ifdef CONFIG_ROOT_NFS 15201da177e4SLinus Torvalds if (ROOT_DEV == Root_NFS) { 1521058bd4d2SJoe Perches pr_err("IP-Config: Retrying forever (NFS root)...\n"); 15221da177e4SLinus Torvalds goto try_try_again; 15231da177e4SLinus Torvalds } 15241da177e4SLinus Torvalds #endif 15251da177e4SLinus Torvalds 15261da177e4SLinus Torvalds if (--retries) { 1527058bd4d2SJoe Perches pr_err("IP-Config: Reopening network devices...\n"); 15281da177e4SLinus Torvalds goto try_try_again; 15291da177e4SLinus Torvalds } 15301da177e4SLinus Torvalds 15311da177e4SLinus Torvalds /* Oh, well. At least we tried. */ 1532058bd4d2SJoe Perches pr_err("IP-Config: Auto-configuration of network failed\n"); 15331da177e4SLinus Torvalds return -1; 15341da177e4SLinus Torvalds } 15351da177e4SLinus Torvalds #else /* !DYNAMIC */ 1536058bd4d2SJoe Perches pr_err("IP-Config: Incomplete network configuration information\n"); 15371da177e4SLinus Torvalds ic_close_devs(); 15381da177e4SLinus Torvalds return -1; 15391da177e4SLinus Torvalds #endif /* IPCONFIG_DYNAMIC */ 15401da177e4SLinus Torvalds } else { 15411da177e4SLinus Torvalds /* Device selected manually or only one device -> use it */ 15422647cffbSUwe Kleine-König ic_dev = ic_first_dev; 15431da177e4SLinus Torvalds } 15441da177e4SLinus Torvalds 15451da177e4SLinus Torvalds addr = root_nfs_parse_addr(root_server_path); 15465a874db4SAl Viro if (root_server_addr == NONE) 15471da177e4SLinus Torvalds root_server_addr = addr; 15481da177e4SLinus Torvalds 15491da177e4SLinus Torvalds /* 155025985edcSLucas De Marchi * Use defaults wherever applicable. 15511da177e4SLinus Torvalds */ 15521da177e4SLinus Torvalds if (ic_defaults() < 0) 15531da177e4SLinus Torvalds return -1; 15541da177e4SLinus Torvalds 15551da177e4SLinus Torvalds /* 15561da177e4SLinus Torvalds * Record which protocol was actually used. 15571da177e4SLinus Torvalds */ 15581da177e4SLinus Torvalds #ifdef IPCONFIG_DYNAMIC 15591da177e4SLinus Torvalds ic_proto_used = ic_got_reply | (ic_proto_enabled & IC_USE_DHCP); 15601da177e4SLinus Torvalds #endif 15611da177e4SLinus Torvalds 15621da177e4SLinus Torvalds #ifndef IPCONFIG_SILENT 15631da177e4SLinus Torvalds /* 15641da177e4SLinus Torvalds * Clue in the operator. 15651da177e4SLinus Torvalds */ 1566058bd4d2SJoe Perches pr_info("IP-Config: Complete:\n"); 15679ecd1c3dSClaudio Fontana 15689ecd1c3dSClaudio Fontana pr_info(" device=%s, hwaddr=%*phC, ipaddr=%pI4, mask=%pI4, gw=%pI4\n", 15692647cffbSUwe Kleine-König ic_dev->dev->name, ic_dev->dev->addr_len, ic_dev->dev->dev_addr, 15709ecd1c3dSClaudio Fontana &ic_myaddr, &ic_netmask, &ic_gateway); 1571058bd4d2SJoe Perches pr_info(" host=%s, domain=%s, nis-domain=%s\n", 1572e9ff3990SSerge E. Hallyn utsname()->nodename, ic_domain, utsname()->domainname); 1573058bd4d2SJoe Perches pr_info(" bootserver=%pI4, rootserver=%pI4, rootpath=%s", 1574058bd4d2SJoe Perches &ic_servaddr, &root_server_addr, root_server_path); 15759643f455SChris Friesen if (ic_dev_mtu) 1576058bd4d2SJoe Perches pr_cont(", mtu=%d", ic_dev_mtu); 1577e18bdc83SChris Novakovic /* Name servers (if any): */ 1578e18bdc83SChris Novakovic for (i = 0; i < CONF_NAMESERVERS_MAX; i++) { 15795e953778SChristoph Fritz if (ic_nameservers[i] != NONE) { 1580e18bdc83SChris Novakovic if (i == 0) 1581e18bdc83SChris Novakovic pr_info(" nameserver%u=%pI4", 15825e953778SChristoph Fritz i, &ic_nameservers[i]); 1583e18bdc83SChris Novakovic else 1584e18bdc83SChris Novakovic pr_cont(", nameserver%u=%pI4", 1585e18bdc83SChris Novakovic i, &ic_nameservers[i]); 15865e953778SChristoph Fritz } 1587e18bdc83SChris Novakovic if (i + 1 == CONF_NAMESERVERS_MAX) 15886c1c36b0SGeert Uytterhoeven pr_cont("\n"); 1589e18bdc83SChris Novakovic } 1590c04d2cb2SChris Novakovic /* NTP servers (if any): */ 1591c04d2cb2SChris Novakovic for (i = 0; i < CONF_NTP_SERVERS_MAX; i++) { 1592c04d2cb2SChris Novakovic if (ic_ntp_servers[i] != NONE) { 1593c04d2cb2SChris Novakovic if (i == 0) 1594c04d2cb2SChris Novakovic pr_info(" ntpserver%u=%pI4", 1595c04d2cb2SChris Novakovic i, &ic_ntp_servers[i]); 1596c04d2cb2SChris Novakovic else 1597c04d2cb2SChris Novakovic pr_cont(", ntpserver%u=%pI4", 1598c04d2cb2SChris Novakovic i, &ic_ntp_servers[i]); 1599c04d2cb2SChris Novakovic } 1600c04d2cb2SChris Novakovic if (i + 1 == CONF_NTP_SERVERS_MAX) 1601c04d2cb2SChris Novakovic pr_cont("\n"); 1602c04d2cb2SChris Novakovic } 16031da177e4SLinus Torvalds #endif /* !SILENT */ 16041da177e4SLinus Torvalds 16059c706a49SUwe Kleine-König /* 16069c706a49SUwe Kleine-König * Close all network devices except the device we've 16079c706a49SUwe Kleine-König * autoconfigured and set up routes. 16089c706a49SUwe Kleine-König */ 16099c706a49SUwe Kleine-König if (ic_setup_if() < 0 || ic_setup_routes() < 0) 1610d2d371aeSThierry Reding err = -1; 1611d2d371aeSThierry Reding else 1612d2d371aeSThierry Reding err = 0; 16139c706a49SUwe Kleine-König 1614d2d371aeSThierry Reding ic_close_devs(); 16159c706a49SUwe Kleine-König 1616d2d371aeSThierry Reding return err; 16171da177e4SLinus Torvalds } 16181da177e4SLinus Torvalds 16191da177e4SLinus Torvalds late_initcall(ip_auto_config); 16201da177e4SLinus Torvalds 16211da177e4SLinus Torvalds 16221da177e4SLinus Torvalds /* 16231da177e4SLinus Torvalds * Decode any IP configuration options in the "ip=" or "nfsaddrs=" kernel 1624dc7a0816SJ. Bruce Fields * command line parameter. See Documentation/filesystems/nfs/nfsroot.txt. 16251da177e4SLinus Torvalds */ 16261da177e4SLinus Torvalds static int __init ic_proto_name(char *name) 16271da177e4SLinus Torvalds { 16281da177e4SLinus Torvalds if (!strcmp(name, "on") || !strcmp(name, "any")) { 16291da177e4SLinus Torvalds return 1; 16301da177e4SLinus Torvalds } 1631a6c05c3dSSimon Horman if (!strcmp(name, "off") || !strcmp(name, "none")) { 163292ffb85dSAmos Waterland return 0; 1633a6c05c3dSSimon Horman } 16341da177e4SLinus Torvalds #ifdef CONFIG_IP_PNP_DHCP 163526fb342cSLi RongQing else if (!strncmp(name, "dhcp", 4)) { 163626fb342cSLi RongQing char *client_id; 163726fb342cSLi RongQing 16381da177e4SLinus Torvalds ic_proto_enabled &= ~IC_RARP; 163926fb342cSLi RongQing client_id = strstr(name, "dhcp,"); 164026fb342cSLi RongQing if (client_id) { 164126fb342cSLi RongQing char *v; 164226fb342cSLi RongQing 164326fb342cSLi RongQing client_id = client_id + 5; 164426fb342cSLi RongQing v = strchr(client_id, ','); 164526fb342cSLi RongQing if (!v) 164626fb342cSLi RongQing return 1; 164726fb342cSLi RongQing *v = 0; 164826fb342cSLi RongQing if (kstrtou8(client_id, 0, dhcp_client_identifier)) 164909605cc1SBastian Stender pr_debug("DHCP: Invalid client identifier type\n"); 165026fb342cSLi RongQing strncpy(dhcp_client_identifier + 1, v + 1, 251); 165126fb342cSLi RongQing *v = ','; 165226fb342cSLi RongQing } 16531da177e4SLinus Torvalds return 1; 16541da177e4SLinus Torvalds } 16551da177e4SLinus Torvalds #endif 16561da177e4SLinus Torvalds #ifdef CONFIG_IP_PNP_BOOTP 16571da177e4SLinus Torvalds else if (!strcmp(name, "bootp")) { 16581da177e4SLinus Torvalds ic_proto_enabled &= ~(IC_RARP | IC_USE_DHCP); 16591da177e4SLinus Torvalds return 1; 16601da177e4SLinus Torvalds } 16611da177e4SLinus Torvalds #endif 16621da177e4SLinus Torvalds #ifdef CONFIG_IP_PNP_RARP 16631da177e4SLinus Torvalds else if (!strcmp(name, "rarp")) { 16641da177e4SLinus Torvalds ic_proto_enabled &= ~(IC_BOOTP | IC_USE_DHCP); 16651da177e4SLinus Torvalds return 1; 16661da177e4SLinus Torvalds } 16671da177e4SLinus Torvalds #endif 16681da177e4SLinus Torvalds #ifdef IPCONFIG_DYNAMIC 16691da177e4SLinus Torvalds else if (!strcmp(name, "both")) { 16701da177e4SLinus Torvalds ic_proto_enabled &= ~IC_USE_DHCP; /* backward compat :-( */ 16711da177e4SLinus Torvalds return 1; 16721da177e4SLinus Torvalds } 16731da177e4SLinus Torvalds #endif 16741da177e4SLinus Torvalds return 0; 16751da177e4SLinus Torvalds } 16761da177e4SLinus Torvalds 16771da177e4SLinus Torvalds static int __init ip_auto_config_setup(char *addrs) 16781da177e4SLinus Torvalds { 16791da177e4SLinus Torvalds char *cp, *ip, *dp; 16801da177e4SLinus Torvalds int num = 0; 16811da177e4SLinus Torvalds 16821da177e4SLinus Torvalds ic_set_manually = 1; 16839cecd07cSSimon Horman ic_enable = 1; 16841da177e4SLinus Torvalds 168592ffb85dSAmos Waterland /* 168692ffb85dSAmos Waterland * If any dhcp, bootp etc options are set, leave autoconfig on 168792ffb85dSAmos Waterland * and skip the below static IP processing. 168892ffb85dSAmos Waterland */ 16891da177e4SLinus Torvalds if (ic_proto_name(addrs)) 16901da177e4SLinus Torvalds return 1; 16911da177e4SLinus Torvalds 169292ffb85dSAmos Waterland /* If no static IP is given, turn off autoconfig and bail. */ 169392ffb85dSAmos Waterland if (*addrs == 0 || 169492ffb85dSAmos Waterland strcmp(addrs, "off") == 0 || 169592ffb85dSAmos Waterland strcmp(addrs, "none") == 0) { 169692ffb85dSAmos Waterland ic_enable = 0; 169792ffb85dSAmos Waterland return 1; 169892ffb85dSAmos Waterland } 169992ffb85dSAmos Waterland 1700c04d2cb2SChris Novakovic /* Initialise all name servers and NTP servers to NONE */ 17015e953778SChristoph Fritz ic_nameservers_predef(); 1702c04d2cb2SChris Novakovic ic_ntp_servers_predef(); 17035e953778SChristoph Fritz 170492ffb85dSAmos Waterland /* Parse string for static IP assignment. */ 17051da177e4SLinus Torvalds ip = addrs; 17061da177e4SLinus Torvalds while (ip && *ip) { 17071da177e4SLinus Torvalds if ((cp = strchr(ip, ':'))) 17081da177e4SLinus Torvalds *cp++ = '\0'; 17091da177e4SLinus Torvalds if (strlen(ip) > 0) { 171009605cc1SBastian Stender pr_debug("IP-Config: Parameter #%d: `%s'\n", num, ip); 17111da177e4SLinus Torvalds switch (num) { 17121da177e4SLinus Torvalds case 0: 1713e6f1cebfSAl Viro if ((ic_myaddr = in_aton(ip)) == ANY) 17145a874db4SAl Viro ic_myaddr = NONE; 17151da177e4SLinus Torvalds break; 17161da177e4SLinus Torvalds case 1: 1717e6f1cebfSAl Viro if ((ic_servaddr = in_aton(ip)) == ANY) 17185a874db4SAl Viro ic_servaddr = NONE; 17191da177e4SLinus Torvalds break; 17201da177e4SLinus Torvalds case 2: 1721e6f1cebfSAl Viro if ((ic_gateway = in_aton(ip)) == ANY) 17225a874db4SAl Viro ic_gateway = NONE; 17231da177e4SLinus Torvalds break; 17241da177e4SLinus Torvalds case 3: 1725e6f1cebfSAl Viro if ((ic_netmask = in_aton(ip)) == ANY) 17265a874db4SAl Viro ic_netmask = NONE; 17271da177e4SLinus Torvalds break; 17281da177e4SLinus Torvalds case 4: 17291da177e4SLinus Torvalds if ((dp = strchr(ip, '.'))) { 17301da177e4SLinus Torvalds *dp++ = '\0'; 1731e9ff3990SSerge E. Hallyn strlcpy(utsname()->domainname, dp, 1732e9ff3990SSerge E. Hallyn sizeof(utsname()->domainname)); 17331da177e4SLinus Torvalds } 1734e9ff3990SSerge E. Hallyn strlcpy(utsname()->nodename, ip, 1735e9ff3990SSerge E. Hallyn sizeof(utsname()->nodename)); 17361da177e4SLinus Torvalds ic_host_name_set = 1; 17371da177e4SLinus Torvalds break; 17381da177e4SLinus Torvalds case 5: 17391da177e4SLinus Torvalds strlcpy(user_dev_name, ip, sizeof(user_dev_name)); 17401da177e4SLinus Torvalds break; 17411da177e4SLinus Torvalds case 6: 174292ffb85dSAmos Waterland if (ic_proto_name(ip) == 0 && 174392ffb85dSAmos Waterland ic_myaddr == NONE) { 174492ffb85dSAmos Waterland ic_enable = 0; 174592ffb85dSAmos Waterland } 17461da177e4SLinus Torvalds break; 17475e953778SChristoph Fritz case 7: 17485e953778SChristoph Fritz if (CONF_NAMESERVERS_MAX >= 1) { 17495e953778SChristoph Fritz ic_nameservers[0] = in_aton(ip); 17505e953778SChristoph Fritz if (ic_nameservers[0] == ANY) 17515e953778SChristoph Fritz ic_nameservers[0] = NONE; 17525e953778SChristoph Fritz } 17535e953778SChristoph Fritz break; 17545e953778SChristoph Fritz case 8: 17555e953778SChristoph Fritz if (CONF_NAMESERVERS_MAX >= 2) { 17565e953778SChristoph Fritz ic_nameservers[1] = in_aton(ip); 17575e953778SChristoph Fritz if (ic_nameservers[1] == ANY) 17585e953778SChristoph Fritz ic_nameservers[1] = NONE; 17595e953778SChristoph Fritz } 17605e953778SChristoph Fritz break; 1761c04d2cb2SChris Novakovic case 9: 1762c04d2cb2SChris Novakovic if (CONF_NTP_SERVERS_MAX >= 1) { 1763c04d2cb2SChris Novakovic ic_ntp_servers[0] = in_aton(ip); 1764c04d2cb2SChris Novakovic if (ic_ntp_servers[0] == ANY) 1765c04d2cb2SChris Novakovic ic_ntp_servers[0] = NONE; 1766c04d2cb2SChris Novakovic } 1767c04d2cb2SChris Novakovic break; 17681da177e4SLinus Torvalds } 17691da177e4SLinus Torvalds } 17701da177e4SLinus Torvalds ip = cp; 17711da177e4SLinus Torvalds num++; 17721da177e4SLinus Torvalds } 17731da177e4SLinus Torvalds 17741da177e4SLinus Torvalds return 1; 17751da177e4SLinus Torvalds } 1776b37f4d7bSEldad Zack __setup("ip=", ip_auto_config_setup); 17771da177e4SLinus Torvalds 17781da177e4SLinus Torvalds static int __init nfsaddrs_config_setup(char *addrs) 17791da177e4SLinus Torvalds { 17801da177e4SLinus Torvalds return ip_auto_config_setup(addrs); 17811da177e4SLinus Torvalds } 1782b37f4d7bSEldad Zack __setup("nfsaddrs=", nfsaddrs_config_setup); 17831da177e4SLinus Torvalds 178462013dbbSRainer Jochem static int __init vendor_class_identifier_setup(char *addrs) 178562013dbbSRainer Jochem { 178662013dbbSRainer Jochem if (strlcpy(vendor_class_identifier, addrs, 178762013dbbSRainer Jochem sizeof(vendor_class_identifier)) 178862013dbbSRainer Jochem >= sizeof(vendor_class_identifier)) 178909605cc1SBastian Stender pr_warn("DHCP: vendorclass too long, truncated to \"%s\"\n", 179062013dbbSRainer Jochem vendor_class_identifier); 179162013dbbSRainer Jochem return 1; 179262013dbbSRainer Jochem } 179362013dbbSRainer Jochem __setup("dhcpclass=", vendor_class_identifier_setup); 1794