1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Automatic Configuration of IP -- use DHCP, BOOTP, RARP, or 4 * user-supplied information to configure own IP address and routes. 5 * 6 * Copyright (C) 1996-1998 Martin Mares <mj@atrey.karlin.mff.cuni.cz> 7 * 8 * Derived from network configuration code in fs/nfs/nfsroot.c, 9 * originally Copyright (C) 1995, 1996 Gero Kuhlmann and me. 10 * 11 * BOOTP rewritten to construct and analyse packets itself instead 12 * of misusing the IP layer. num_bugs_causing_wrong_arp_replies--; 13 * -- MJ, December 1998 14 * 15 * Fixed ip_auto_config_setup calling at startup in the new "Linker Magic" 16 * initialization scheme. 17 * - Arnaldo Carvalho de Melo <acme@conectiva.com.br>, 08/11/1999 18 * 19 * DHCP support added. To users this looks like a whole separate 20 * protocol, but we know it's just a bag on the side of BOOTP. 21 * -- Chip Salzenberg <chip@valinux.com>, May 2000 22 * 23 * Ported DHCP support from 2.2.16 to 2.4.0-test4 24 * -- Eric Biederman <ebiederman@lnxi.com>, 30 Aug 2000 25 * 26 * Merged changes from 2.2.19 into 2.4.3 27 * -- Eric Biederman <ebiederman@lnxi.com>, 22 April Aug 2001 28 * 29 * Multiple Nameservers in /proc/net/pnp 30 * -- Josef Siemes <jsiemes@web.de>, Aug 2002 31 * 32 * NTP servers in /proc/net/ipconfig/ntp_servers 33 * -- Chris Novakovic <chris@chrisn.me.uk>, April 2018 34 */ 35 36 #include <linux/types.h> 37 #include <linux/string.h> 38 #include <linux/kernel.h> 39 #include <linux/jiffies.h> 40 #include <linux/random.h> 41 #include <linux/init.h> 42 #include <linux/utsname.h> 43 #include <linux/in.h> 44 #include <linux/if.h> 45 #include <linux/inet.h> 46 #include <linux/inetdevice.h> 47 #include <linux/netdevice.h> 48 #include <linux/if_arp.h> 49 #include <linux/skbuff.h> 50 #include <linux/ip.h> 51 #include <linux/socket.h> 52 #include <linux/route.h> 53 #include <linux/udp.h> 54 #include <linux/proc_fs.h> 55 #include <linux/seq_file.h> 56 #include <linux/major.h> 57 #include <linux/root_dev.h> 58 #include <linux/delay.h> 59 #include <linux/nfs_fs.h> 60 #include <linux/slab.h> 61 #include <linux/export.h> 62 #include <net/net_namespace.h> 63 #include <net/arp.h> 64 #include <net/ip.h> 65 #include <net/ipconfig.h> 66 #include <net/route.h> 67 68 #include <linux/uaccess.h> 69 #include <net/checksum.h> 70 #include <asm/processor.h> 71 72 #if defined(CONFIG_IP_PNP_DHCP) 73 #define IPCONFIG_DHCP 74 #endif 75 #if defined(CONFIG_IP_PNP_BOOTP) || defined(CONFIG_IP_PNP_DHCP) 76 #define IPCONFIG_BOOTP 77 #endif 78 #if defined(CONFIG_IP_PNP_RARP) 79 #define IPCONFIG_RARP 80 #endif 81 #if defined(IPCONFIG_BOOTP) || defined(IPCONFIG_RARP) 82 #define IPCONFIG_DYNAMIC 83 #endif 84 85 /* Define the friendly delay before and after opening net devices */ 86 #define CONF_POST_OPEN 10 /* After opening: 10 msecs */ 87 88 /* Define the timeout for waiting for a DHCP/BOOTP/RARP reply */ 89 #define CONF_OPEN_RETRIES 2 /* (Re)open devices twice */ 90 #define CONF_SEND_RETRIES 6 /* Send six requests per open */ 91 #define CONF_BASE_TIMEOUT (HZ*2) /* Initial timeout: 2 seconds */ 92 #define CONF_TIMEOUT_RANDOM (HZ) /* Maximum amount of randomization */ 93 #define CONF_TIMEOUT_MULT *7/4 /* Rate of timeout growth */ 94 #define CONF_TIMEOUT_MAX (HZ*30) /* Maximum allowed timeout */ 95 #define CONF_NAMESERVERS_MAX 3 /* Maximum number of nameservers 96 - '3' from resolv.h */ 97 #define CONF_NTP_SERVERS_MAX 3 /* Maximum number of NTP servers */ 98 99 #define NONE cpu_to_be32(INADDR_NONE) 100 #define ANY cpu_to_be32(INADDR_ANY) 101 102 /* Wait for carrier timeout default in seconds */ 103 static unsigned int carrier_timeout = 120; 104 105 /* 106 * Public IP configuration 107 */ 108 109 /* This is used by platforms which might be able to set the ipconfig 110 * variables using firmware environment vars. If this is set, it will 111 * ignore such firmware variables. 112 */ 113 int ic_set_manually __initdata = 0; /* IPconfig parameters set manually */ 114 115 static int ic_enable __initdata; /* IP config enabled? */ 116 117 /* Protocol choice */ 118 int ic_proto_enabled __initdata = 0 119 #ifdef IPCONFIG_BOOTP 120 | IC_BOOTP 121 #endif 122 #ifdef CONFIG_IP_PNP_DHCP 123 | IC_USE_DHCP 124 #endif 125 #ifdef IPCONFIG_RARP 126 | IC_RARP 127 #endif 128 ; 129 130 static int ic_host_name_set __initdata; /* Host name set by us? */ 131 132 __be32 ic_myaddr = NONE; /* My IP address */ 133 static __be32 ic_netmask = NONE; /* Netmask for local subnet */ 134 __be32 ic_gateway = NONE; /* Gateway IP address */ 135 136 #ifdef IPCONFIG_DYNAMIC 137 static __be32 ic_addrservaddr = NONE; /* IP Address of the IP addresses'server */ 138 #endif 139 140 __be32 ic_servaddr = NONE; /* Boot server IP address */ 141 142 __be32 root_server_addr = NONE; /* Address of NFS server */ 143 u8 root_server_path[256] = { 0, }; /* Path to mount as root */ 144 145 /* vendor class identifier */ 146 static char vendor_class_identifier[253] __initdata; 147 148 #if defined(CONFIG_IP_PNP_DHCP) 149 static char dhcp_client_identifier[253] __initdata; 150 #endif 151 152 /* Persistent data: */ 153 154 #ifdef IPCONFIG_DYNAMIC 155 static int ic_proto_used; /* Protocol used, if any */ 156 #else 157 #define ic_proto_used 0 158 #endif 159 static __be32 ic_nameservers[CONF_NAMESERVERS_MAX]; /* DNS Server IP addresses */ 160 static __be32 ic_ntp_servers[CONF_NTP_SERVERS_MAX]; /* NTP server IP addresses */ 161 static u8 ic_domain[64]; /* DNS (not NIS) domain name */ 162 163 /* 164 * Private state. 165 */ 166 167 /* Name of user-selected boot device */ 168 static char user_dev_name[IFNAMSIZ] __initdata = { 0, }; 169 170 /* Protocols supported by available interfaces */ 171 static int ic_proto_have_if __initdata; 172 173 /* MTU for boot device */ 174 static int ic_dev_mtu __initdata; 175 176 #ifdef IPCONFIG_DYNAMIC 177 static DEFINE_SPINLOCK(ic_recv_lock); 178 static volatile int ic_got_reply __initdata; /* Proto(s) that replied */ 179 #endif 180 #ifdef IPCONFIG_DHCP 181 static int ic_dhcp_msgtype __initdata; /* DHCP msg type received */ 182 #endif 183 184 185 /* 186 * Network devices 187 */ 188 189 struct ic_device { 190 struct ic_device *next; 191 struct net_device *dev; 192 unsigned short flags; 193 short able; 194 __be32 xid; 195 }; 196 197 static struct ic_device *ic_first_dev __initdata; /* List of open device */ 198 static struct ic_device *ic_dev __initdata; /* Selected device */ 199 200 static bool __init ic_is_init_dev(struct net_device *dev) 201 { 202 if (dev->flags & IFF_LOOPBACK) 203 return false; 204 return user_dev_name[0] ? !strcmp(dev->name, user_dev_name) : 205 (!(dev->flags & IFF_LOOPBACK) && 206 (dev->flags & (IFF_POINTOPOINT|IFF_BROADCAST)) && 207 strncmp(dev->name, "dummy", 5)); 208 } 209 210 static int __init ic_open_devs(void) 211 { 212 struct ic_device *d, **last; 213 struct net_device *dev; 214 unsigned short oflags; 215 unsigned long start, next_msg; 216 217 last = &ic_first_dev; 218 rtnl_lock(); 219 220 /* bring loopback device up first */ 221 for_each_netdev(&init_net, dev) { 222 if (!(dev->flags & IFF_LOOPBACK)) 223 continue; 224 if (dev_change_flags(dev, dev->flags | IFF_UP, NULL) < 0) 225 pr_err("IP-Config: Failed to open %s\n", dev->name); 226 } 227 228 for_each_netdev(&init_net, dev) { 229 if (ic_is_init_dev(dev)) { 230 int able = 0; 231 if (dev->mtu >= 364) 232 able |= IC_BOOTP; 233 else 234 pr_warn("DHCP/BOOTP: Ignoring device %s, MTU %d too small\n", 235 dev->name, dev->mtu); 236 if (!(dev->flags & IFF_NOARP)) 237 able |= IC_RARP; 238 able &= ic_proto_enabled; 239 if (ic_proto_enabled && !able) 240 continue; 241 oflags = dev->flags; 242 if (dev_change_flags(dev, oflags | IFF_UP, NULL) < 0) { 243 pr_err("IP-Config: Failed to open %s\n", 244 dev->name); 245 continue; 246 } 247 if (!(d = kmalloc(sizeof(struct ic_device), GFP_KERNEL))) { 248 rtnl_unlock(); 249 return -ENOMEM; 250 } 251 d->dev = dev; 252 *last = d; 253 last = &d->next; 254 d->flags = oflags; 255 d->able = able; 256 if (able & IC_BOOTP) 257 get_random_bytes(&d->xid, sizeof(__be32)); 258 else 259 d->xid = 0; 260 ic_proto_have_if |= able; 261 pr_debug("IP-Config: %s UP (able=%d, xid=%08x)\n", 262 dev->name, able, d->xid); 263 } 264 } 265 266 /* no point in waiting if we could not bring up at least one device */ 267 if (!ic_first_dev) 268 goto have_carrier; 269 270 /* wait for a carrier on at least one device */ 271 start = jiffies; 272 next_msg = start + msecs_to_jiffies(20000); 273 while (time_before(jiffies, start + 274 msecs_to_jiffies(carrier_timeout * 1000))) { 275 int wait, elapsed; 276 277 for_each_netdev(&init_net, dev) 278 if (ic_is_init_dev(dev) && netif_carrier_ok(dev)) 279 goto have_carrier; 280 281 msleep(1); 282 283 if (time_before(jiffies, next_msg)) 284 continue; 285 286 elapsed = jiffies_to_msecs(jiffies - start); 287 wait = (carrier_timeout * 1000 - elapsed + 500) / 1000; 288 pr_info("Waiting up to %d more seconds for network.\n", wait); 289 next_msg = jiffies + msecs_to_jiffies(20000); 290 } 291 have_carrier: 292 rtnl_unlock(); 293 294 *last = NULL; 295 296 if (!ic_first_dev) { 297 if (user_dev_name[0]) 298 pr_err("IP-Config: Device `%s' not found\n", 299 user_dev_name); 300 else 301 pr_err("IP-Config: No network devices available\n"); 302 return -ENODEV; 303 } 304 return 0; 305 } 306 307 /* Close all network interfaces except the one we've autoconfigured, and its 308 * lowers, in case it's a stacked virtual interface. 309 */ 310 static void __init ic_close_devs(void) 311 { 312 struct net_device *selected_dev = ic_dev->dev; 313 struct ic_device *d, *next; 314 struct net_device *dev; 315 316 rtnl_lock(); 317 next = ic_first_dev; 318 while ((d = next)) { 319 bool bring_down = (d != ic_dev); 320 struct net_device *lower_dev; 321 struct list_head *iter; 322 323 next = d->next; 324 dev = d->dev; 325 326 netdev_for_each_lower_dev(selected_dev, lower_dev, iter) { 327 if (dev == lower_dev) { 328 bring_down = false; 329 break; 330 } 331 } 332 if (bring_down) { 333 pr_debug("IP-Config: Downing %s\n", dev->name); 334 dev_change_flags(dev, d->flags, NULL); 335 } 336 kfree(d); 337 } 338 rtnl_unlock(); 339 } 340 341 /* 342 * Interface to various network functions. 343 */ 344 345 static inline void 346 set_sockaddr(struct sockaddr_in *sin, __be32 addr, __be16 port) 347 { 348 sin->sin_family = AF_INET; 349 sin->sin_addr.s_addr = addr; 350 sin->sin_port = port; 351 } 352 353 /* 354 * Set up interface addresses and routes. 355 */ 356 357 static int __init ic_setup_if(void) 358 { 359 struct ifreq ir; 360 struct sockaddr_in *sin = (void *) &ir.ifr_ifru.ifru_addr; 361 int err; 362 363 memset(&ir, 0, sizeof(ir)); 364 strcpy(ir.ifr_ifrn.ifrn_name, ic_dev->dev->name); 365 set_sockaddr(sin, ic_myaddr, 0); 366 if ((err = devinet_ioctl(&init_net, SIOCSIFADDR, &ir)) < 0) { 367 pr_err("IP-Config: Unable to set interface address (%d)\n", 368 err); 369 return -1; 370 } 371 set_sockaddr(sin, ic_netmask, 0); 372 if ((err = devinet_ioctl(&init_net, SIOCSIFNETMASK, &ir)) < 0) { 373 pr_err("IP-Config: Unable to set interface netmask (%d)\n", 374 err); 375 return -1; 376 } 377 set_sockaddr(sin, ic_myaddr | ~ic_netmask, 0); 378 if ((err = devinet_ioctl(&init_net, SIOCSIFBRDADDR, &ir)) < 0) { 379 pr_err("IP-Config: Unable to set interface broadcast address (%d)\n", 380 err); 381 return -1; 382 } 383 /* Handle the case where we need non-standard MTU on the boot link (a network 384 * using jumbo frames, for instance). If we can't set the mtu, don't error 385 * out, we'll try to muddle along. 386 */ 387 if (ic_dev_mtu != 0) { 388 rtnl_lock(); 389 if ((err = dev_set_mtu(ic_dev->dev, ic_dev_mtu)) < 0) 390 pr_err("IP-Config: Unable to set interface mtu to %d (%d)\n", 391 ic_dev_mtu, err); 392 rtnl_unlock(); 393 } 394 return 0; 395 } 396 397 static int __init ic_setup_routes(void) 398 { 399 /* No need to setup device routes, only the default route... */ 400 401 if (ic_gateway != NONE) { 402 struct rtentry rm; 403 int err; 404 405 memset(&rm, 0, sizeof(rm)); 406 if ((ic_gateway ^ ic_myaddr) & ic_netmask) { 407 pr_err("IP-Config: Gateway not on directly connected network\n"); 408 return -1; 409 } 410 set_sockaddr((struct sockaddr_in *) &rm.rt_dst, 0, 0); 411 set_sockaddr((struct sockaddr_in *) &rm.rt_genmask, 0, 0); 412 set_sockaddr((struct sockaddr_in *) &rm.rt_gateway, ic_gateway, 0); 413 rm.rt_flags = RTF_UP | RTF_GATEWAY; 414 if ((err = ip_rt_ioctl(&init_net, SIOCADDRT, &rm)) < 0) { 415 pr_err("IP-Config: Cannot add default route (%d)\n", 416 err); 417 return -1; 418 } 419 } 420 421 return 0; 422 } 423 424 /* 425 * Fill in default values for all missing parameters. 426 */ 427 428 static int __init ic_defaults(void) 429 { 430 /* 431 * At this point we have no userspace running so need not 432 * claim locks on system_utsname 433 */ 434 435 if (!ic_host_name_set) 436 sprintf(init_utsname()->nodename, "%pI4", &ic_myaddr); 437 438 if (root_server_addr == NONE) 439 root_server_addr = ic_servaddr; 440 441 if (ic_netmask == NONE) { 442 if (IN_CLASSA(ntohl(ic_myaddr))) 443 ic_netmask = htonl(IN_CLASSA_NET); 444 else if (IN_CLASSB(ntohl(ic_myaddr))) 445 ic_netmask = htonl(IN_CLASSB_NET); 446 else if (IN_CLASSC(ntohl(ic_myaddr))) 447 ic_netmask = htonl(IN_CLASSC_NET); 448 else if (IN_CLASSE(ntohl(ic_myaddr))) 449 ic_netmask = htonl(IN_CLASSE_NET); 450 else { 451 pr_err("IP-Config: Unable to guess netmask for address %pI4\n", 452 &ic_myaddr); 453 return -1; 454 } 455 pr_notice("IP-Config: Guessing netmask %pI4\n", 456 &ic_netmask); 457 } 458 459 return 0; 460 } 461 462 /* 463 * RARP support. 464 */ 465 466 #ifdef IPCONFIG_RARP 467 468 static int ic_rarp_recv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev); 469 470 static struct packet_type rarp_packet_type __initdata = { 471 .type = cpu_to_be16(ETH_P_RARP), 472 .func = ic_rarp_recv, 473 }; 474 475 static inline void __init ic_rarp_init(void) 476 { 477 dev_add_pack(&rarp_packet_type); 478 } 479 480 static inline void __init ic_rarp_cleanup(void) 481 { 482 dev_remove_pack(&rarp_packet_type); 483 } 484 485 /* 486 * Process received RARP packet. 487 */ 488 static int __init 489 ic_rarp_recv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev) 490 { 491 struct arphdr *rarp; 492 unsigned char *rarp_ptr; 493 __be32 sip, tip; 494 unsigned char *tha; /* t for "target" */ 495 struct ic_device *d; 496 497 if (!net_eq(dev_net(dev), &init_net)) 498 goto drop; 499 500 skb = skb_share_check(skb, GFP_ATOMIC); 501 if (!skb) 502 return NET_RX_DROP; 503 504 if (!pskb_may_pull(skb, sizeof(struct arphdr))) 505 goto drop; 506 507 /* Basic sanity checks can be done without the lock. */ 508 rarp = (struct arphdr *)skb_transport_header(skb); 509 510 /* If this test doesn't pass, it's not IP, or we should 511 * ignore it anyway. 512 */ 513 if (rarp->ar_hln != dev->addr_len || dev->type != ntohs(rarp->ar_hrd)) 514 goto drop; 515 516 /* If it's not a RARP reply, delete it. */ 517 if (rarp->ar_op != htons(ARPOP_RREPLY)) 518 goto drop; 519 520 /* If it's not Ethernet, delete it. */ 521 if (rarp->ar_pro != htons(ETH_P_IP)) 522 goto drop; 523 524 if (!pskb_may_pull(skb, arp_hdr_len(dev))) 525 goto drop; 526 527 /* OK, it is all there and looks valid, process... */ 528 rarp = (struct arphdr *)skb_transport_header(skb); 529 rarp_ptr = (unsigned char *) (rarp + 1); 530 531 /* One reply at a time, please. */ 532 spin_lock(&ic_recv_lock); 533 534 /* If we already have a reply, just drop the packet */ 535 if (ic_got_reply) 536 goto drop_unlock; 537 538 /* Find the ic_device that the packet arrived on */ 539 d = ic_first_dev; 540 while (d && d->dev != dev) 541 d = d->next; 542 if (!d) 543 goto drop_unlock; /* should never happen */ 544 545 /* Extract variable-width fields */ 546 rarp_ptr += dev->addr_len; 547 memcpy(&sip, rarp_ptr, 4); 548 rarp_ptr += 4; 549 tha = rarp_ptr; 550 rarp_ptr += dev->addr_len; 551 memcpy(&tip, rarp_ptr, 4); 552 553 /* Discard packets which are not meant for us. */ 554 if (memcmp(tha, dev->dev_addr, dev->addr_len)) 555 goto drop_unlock; 556 557 /* Discard packets which are not from specified server. */ 558 if (ic_servaddr != NONE && ic_servaddr != sip) 559 goto drop_unlock; 560 561 /* We have a winner! */ 562 ic_dev = d; 563 if (ic_myaddr == NONE) 564 ic_myaddr = tip; 565 ic_servaddr = sip; 566 ic_addrservaddr = sip; 567 ic_got_reply = IC_RARP; 568 569 drop_unlock: 570 /* Show's over. Nothing to see here. */ 571 spin_unlock(&ic_recv_lock); 572 573 drop: 574 /* Throw the packet out. */ 575 kfree_skb(skb); 576 return 0; 577 } 578 579 580 /* 581 * Send RARP request packet over a single interface. 582 */ 583 static void __init ic_rarp_send_if(struct ic_device *d) 584 { 585 struct net_device *dev = d->dev; 586 arp_send(ARPOP_RREQUEST, ETH_P_RARP, 0, dev, 0, NULL, 587 dev->dev_addr, dev->dev_addr); 588 } 589 #endif 590 591 /* 592 * Predefine Nameservers 593 */ 594 static inline void __init ic_nameservers_predef(void) 595 { 596 int i; 597 598 for (i = 0; i < CONF_NAMESERVERS_MAX; i++) 599 ic_nameservers[i] = NONE; 600 } 601 602 /* Predefine NTP servers */ 603 static inline void __init ic_ntp_servers_predef(void) 604 { 605 int i; 606 607 for (i = 0; i < CONF_NTP_SERVERS_MAX; i++) 608 ic_ntp_servers[i] = NONE; 609 } 610 611 /* 612 * DHCP/BOOTP support. 613 */ 614 615 #ifdef IPCONFIG_BOOTP 616 617 struct bootp_pkt { /* BOOTP packet format */ 618 struct iphdr iph; /* IP header */ 619 struct udphdr udph; /* UDP header */ 620 u8 op; /* 1=request, 2=reply */ 621 u8 htype; /* HW address type */ 622 u8 hlen; /* HW address length */ 623 u8 hops; /* Used only by gateways */ 624 __be32 xid; /* Transaction ID */ 625 __be16 secs; /* Seconds since we started */ 626 __be16 flags; /* Just what it says */ 627 __be32 client_ip; /* Client's IP address if known */ 628 __be32 your_ip; /* Assigned IP address */ 629 __be32 server_ip; /* (Next, e.g. NFS) Server's IP address */ 630 __be32 relay_ip; /* IP address of BOOTP relay */ 631 u8 hw_addr[16]; /* Client's HW address */ 632 u8 serv_name[64]; /* Server host name */ 633 u8 boot_file[128]; /* Name of boot file */ 634 u8 exten[312]; /* DHCP options / BOOTP vendor extensions */ 635 }; 636 637 /* packet ops */ 638 #define BOOTP_REQUEST 1 639 #define BOOTP_REPLY 2 640 641 /* DHCP message types */ 642 #define DHCPDISCOVER 1 643 #define DHCPOFFER 2 644 #define DHCPREQUEST 3 645 #define DHCPDECLINE 4 646 #define DHCPACK 5 647 #define DHCPNAK 6 648 #define DHCPRELEASE 7 649 #define DHCPINFORM 8 650 651 static int ic_bootp_recv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev); 652 653 static struct packet_type bootp_packet_type __initdata = { 654 .type = cpu_to_be16(ETH_P_IP), 655 .func = ic_bootp_recv, 656 }; 657 658 /* 659 * Initialize DHCP/BOOTP extension fields in the request. 660 */ 661 662 static const u8 ic_bootp_cookie[4] = { 99, 130, 83, 99 }; 663 664 #ifdef IPCONFIG_DHCP 665 666 static void __init 667 ic_dhcp_init_options(u8 *options, struct ic_device *d) 668 { 669 u8 mt = ((ic_servaddr == NONE) 670 ? DHCPDISCOVER : DHCPREQUEST); 671 u8 *e = options; 672 int len; 673 674 pr_debug("DHCP: Sending message type %d (%s)\n", mt, d->dev->name); 675 676 memcpy(e, ic_bootp_cookie, 4); /* RFC1048 Magic Cookie */ 677 e += 4; 678 679 *e++ = 53; /* DHCP message type */ 680 *e++ = 1; 681 *e++ = mt; 682 683 if (mt == DHCPREQUEST) { 684 *e++ = 54; /* Server ID (IP address) */ 685 *e++ = 4; 686 memcpy(e, &ic_servaddr, 4); 687 e += 4; 688 689 *e++ = 50; /* Requested IP address */ 690 *e++ = 4; 691 memcpy(e, &ic_myaddr, 4); 692 e += 4; 693 } 694 695 /* always? */ 696 { 697 static const u8 ic_req_params[] = { 698 1, /* Subnet mask */ 699 3, /* Default gateway */ 700 6, /* DNS server */ 701 12, /* Host name */ 702 15, /* Domain name */ 703 17, /* Boot path */ 704 26, /* MTU */ 705 40, /* NIS domain name */ 706 42, /* NTP servers */ 707 }; 708 709 *e++ = 55; /* Parameter request list */ 710 *e++ = sizeof(ic_req_params); 711 memcpy(e, ic_req_params, sizeof(ic_req_params)); 712 e += sizeof(ic_req_params); 713 714 if (ic_host_name_set) { 715 *e++ = 12; /* host-name */ 716 len = strlen(utsname()->nodename); 717 *e++ = len; 718 memcpy(e, utsname()->nodename, len); 719 e += len; 720 } 721 if (*vendor_class_identifier) { 722 pr_info("DHCP: sending class identifier \"%s\"\n", 723 vendor_class_identifier); 724 *e++ = 60; /* Class-identifier */ 725 len = strlen(vendor_class_identifier); 726 *e++ = len; 727 memcpy(e, vendor_class_identifier, len); 728 e += len; 729 } 730 len = strlen(dhcp_client_identifier + 1); 731 /* the minimum length of identifier is 2, include 1 byte type, 732 * and can not be larger than the length of options 733 */ 734 if (len >= 1 && len < 312 - (e - options) - 1) { 735 *e++ = 61; 736 *e++ = len + 1; 737 memcpy(e, dhcp_client_identifier, len + 1); 738 e += len + 1; 739 } 740 } 741 742 *e++ = 255; /* End of the list */ 743 } 744 745 #endif /* IPCONFIG_DHCP */ 746 747 static void __init ic_bootp_init_ext(u8 *e) 748 { 749 memcpy(e, ic_bootp_cookie, 4); /* RFC1048 Magic Cookie */ 750 e += 4; 751 *e++ = 1; /* Subnet mask request */ 752 *e++ = 4; 753 e += 4; 754 *e++ = 3; /* Default gateway request */ 755 *e++ = 4; 756 e += 4; 757 #if CONF_NAMESERVERS_MAX > 0 758 *e++ = 6; /* (DNS) name server request */ 759 *e++ = 4 * CONF_NAMESERVERS_MAX; 760 e += 4 * CONF_NAMESERVERS_MAX; 761 #endif 762 *e++ = 12; /* Host name request */ 763 *e++ = 32; 764 e += 32; 765 *e++ = 40; /* NIS Domain name request */ 766 *e++ = 32; 767 e += 32; 768 *e++ = 17; /* Boot path */ 769 *e++ = 40; 770 e += 40; 771 772 *e++ = 57; /* set extension buffer size for reply */ 773 *e++ = 2; 774 *e++ = 1; /* 128+236+8+20+14, see dhcpd sources */ 775 *e++ = 150; 776 777 *e++ = 255; /* End of the list */ 778 } 779 780 781 /* 782 * Initialize the DHCP/BOOTP mechanism. 783 */ 784 static inline void __init ic_bootp_init(void) 785 { 786 /* Re-initialise all name servers and NTP servers to NONE, in case any 787 * were set via the "ip=" or "nfsaddrs=" kernel command line parameters: 788 * any IP addresses specified there will already have been decoded but 789 * are no longer needed 790 */ 791 ic_nameservers_predef(); 792 ic_ntp_servers_predef(); 793 794 dev_add_pack(&bootp_packet_type); 795 } 796 797 798 /* 799 * DHCP/BOOTP cleanup. 800 */ 801 static inline void __init ic_bootp_cleanup(void) 802 { 803 dev_remove_pack(&bootp_packet_type); 804 } 805 806 807 /* 808 * Send DHCP/BOOTP request to single interface. 809 */ 810 static void __init ic_bootp_send_if(struct ic_device *d, unsigned long jiffies_diff) 811 { 812 struct net_device *dev = d->dev; 813 struct sk_buff *skb; 814 struct bootp_pkt *b; 815 struct iphdr *h; 816 int hlen = LL_RESERVED_SPACE(dev); 817 int tlen = dev->needed_tailroom; 818 819 /* Allocate packet */ 820 skb = alloc_skb(sizeof(struct bootp_pkt) + hlen + tlen + 15, 821 GFP_KERNEL); 822 if (!skb) 823 return; 824 skb_reserve(skb, hlen); 825 b = skb_put_zero(skb, sizeof(struct bootp_pkt)); 826 827 /* Construct IP header */ 828 skb_reset_network_header(skb); 829 h = ip_hdr(skb); 830 h->version = 4; 831 h->ihl = 5; 832 h->tot_len = htons(sizeof(struct bootp_pkt)); 833 h->frag_off = htons(IP_DF); 834 h->ttl = 64; 835 h->protocol = IPPROTO_UDP; 836 h->daddr = htonl(INADDR_BROADCAST); 837 h->check = ip_fast_csum((unsigned char *) h, h->ihl); 838 839 /* Construct UDP header */ 840 b->udph.source = htons(68); 841 b->udph.dest = htons(67); 842 b->udph.len = htons(sizeof(struct bootp_pkt) - sizeof(struct iphdr)); 843 /* UDP checksum not calculated -- explicitly allowed in BOOTP RFC */ 844 845 /* Construct DHCP/BOOTP header */ 846 b->op = BOOTP_REQUEST; 847 if (dev->type < 256) /* check for false types */ 848 b->htype = dev->type; 849 else if (dev->type == ARPHRD_FDDI) 850 b->htype = ARPHRD_ETHER; 851 else { 852 pr_warn("Unknown ARP type 0x%04x for device %s\n", dev->type, 853 dev->name); 854 b->htype = dev->type; /* can cause undefined behavior */ 855 } 856 857 /* server_ip and your_ip address are both already zero per RFC2131 */ 858 b->hlen = dev->addr_len; 859 memcpy(b->hw_addr, dev->dev_addr, dev->addr_len); 860 b->secs = htons(jiffies_diff / HZ); 861 b->xid = d->xid; 862 863 /* add DHCP options or BOOTP extensions */ 864 #ifdef IPCONFIG_DHCP 865 if (ic_proto_enabled & IC_USE_DHCP) 866 ic_dhcp_init_options(b->exten, d); 867 else 868 #endif 869 ic_bootp_init_ext(b->exten); 870 871 /* Chain packet down the line... */ 872 skb->dev = dev; 873 skb->protocol = htons(ETH_P_IP); 874 if (dev_hard_header(skb, dev, ntohs(skb->protocol), 875 dev->broadcast, dev->dev_addr, skb->len) < 0) { 876 kfree_skb(skb); 877 printk("E"); 878 return; 879 } 880 881 if (dev_queue_xmit(skb) < 0) 882 printk("E"); 883 } 884 885 886 /* 887 * Copy BOOTP-supplied string if not already set. 888 */ 889 static int __init ic_bootp_string(char *dest, char *src, int len, int max) 890 { 891 if (!len) 892 return 0; 893 if (len > max-1) 894 len = max-1; 895 memcpy(dest, src, len); 896 dest[len] = '\0'; 897 return 1; 898 } 899 900 901 /* 902 * Process BOOTP extensions. 903 */ 904 static void __init ic_do_bootp_ext(u8 *ext) 905 { 906 u8 servers; 907 int i; 908 __be16 mtu; 909 910 u8 *c; 911 912 pr_debug("DHCP/BOOTP: Got extension %d:", *ext); 913 for (c=ext+2; c<ext+2+ext[1]; c++) 914 pr_debug(" %02x", *c); 915 pr_debug("\n"); 916 917 switch (*ext++) { 918 case 1: /* Subnet mask */ 919 if (ic_netmask == NONE) 920 memcpy(&ic_netmask, ext+1, 4); 921 break; 922 case 3: /* Default gateway */ 923 if (ic_gateway == NONE) 924 memcpy(&ic_gateway, ext+1, 4); 925 break; 926 case 6: /* DNS server */ 927 servers= *ext/4; 928 if (servers > CONF_NAMESERVERS_MAX) 929 servers = CONF_NAMESERVERS_MAX; 930 for (i = 0; i < servers; i++) { 931 if (ic_nameservers[i] == NONE) 932 memcpy(&ic_nameservers[i], ext+1+4*i, 4); 933 } 934 break; 935 case 12: /* Host name */ 936 ic_bootp_string(utsname()->nodename, ext+1, *ext, 937 __NEW_UTS_LEN); 938 ic_host_name_set = 1; 939 break; 940 case 15: /* Domain name (DNS) */ 941 ic_bootp_string(ic_domain, ext+1, *ext, sizeof(ic_domain)); 942 break; 943 case 17: /* Root path */ 944 if (!root_server_path[0]) 945 ic_bootp_string(root_server_path, ext+1, *ext, 946 sizeof(root_server_path)); 947 break; 948 case 26: /* Interface MTU */ 949 memcpy(&mtu, ext+1, sizeof(mtu)); 950 ic_dev_mtu = ntohs(mtu); 951 break; 952 case 40: /* NIS Domain name (_not_ DNS) */ 953 ic_bootp_string(utsname()->domainname, ext+1, *ext, 954 __NEW_UTS_LEN); 955 break; 956 case 42: /* NTP servers */ 957 servers = *ext / 4; 958 if (servers > CONF_NTP_SERVERS_MAX) 959 servers = CONF_NTP_SERVERS_MAX; 960 for (i = 0; i < servers; i++) { 961 if (ic_ntp_servers[i] == NONE) 962 memcpy(&ic_ntp_servers[i], ext+1+4*i, 4); 963 } 964 break; 965 } 966 } 967 968 969 /* 970 * Receive BOOTP reply. 971 */ 972 static int __init ic_bootp_recv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev) 973 { 974 struct bootp_pkt *b; 975 struct iphdr *h; 976 struct ic_device *d; 977 int len, ext_len; 978 979 if (!net_eq(dev_net(dev), &init_net)) 980 goto drop; 981 982 /* Perform verifications before taking the lock. */ 983 if (skb->pkt_type == PACKET_OTHERHOST) 984 goto drop; 985 986 skb = skb_share_check(skb, GFP_ATOMIC); 987 if (!skb) 988 return NET_RX_DROP; 989 990 if (!pskb_may_pull(skb, 991 sizeof(struct iphdr) + 992 sizeof(struct udphdr))) 993 goto drop; 994 995 b = (struct bootp_pkt *)skb_network_header(skb); 996 h = &b->iph; 997 998 if (h->ihl != 5 || h->version != 4 || h->protocol != IPPROTO_UDP) 999 goto drop; 1000 1001 /* Fragments are not supported */ 1002 if (ip_is_fragment(h)) { 1003 net_err_ratelimited("DHCP/BOOTP: Ignoring fragmented reply\n"); 1004 goto drop; 1005 } 1006 1007 if (skb->len < ntohs(h->tot_len)) 1008 goto drop; 1009 1010 if (ip_fast_csum((char *) h, h->ihl)) 1011 goto drop; 1012 1013 if (b->udph.source != htons(67) || b->udph.dest != htons(68)) 1014 goto drop; 1015 1016 if (ntohs(h->tot_len) < ntohs(b->udph.len) + sizeof(struct iphdr)) 1017 goto drop; 1018 1019 len = ntohs(b->udph.len) - sizeof(struct udphdr); 1020 ext_len = len - (sizeof(*b) - 1021 sizeof(struct iphdr) - 1022 sizeof(struct udphdr) - 1023 sizeof(b->exten)); 1024 if (ext_len < 0) 1025 goto drop; 1026 1027 /* Ok the front looks good, make sure we can get at the rest. */ 1028 if (!pskb_may_pull(skb, skb->len)) 1029 goto drop; 1030 1031 b = (struct bootp_pkt *)skb_network_header(skb); 1032 h = &b->iph; 1033 1034 /* One reply at a time, please. */ 1035 spin_lock(&ic_recv_lock); 1036 1037 /* If we already have a reply, just drop the packet */ 1038 if (ic_got_reply) 1039 goto drop_unlock; 1040 1041 /* Find the ic_device that the packet arrived on */ 1042 d = ic_first_dev; 1043 while (d && d->dev != dev) 1044 d = d->next; 1045 if (!d) 1046 goto drop_unlock; /* should never happen */ 1047 1048 /* Is it a reply to our BOOTP request? */ 1049 if (b->op != BOOTP_REPLY || 1050 b->xid != d->xid) { 1051 net_err_ratelimited("DHCP/BOOTP: Reply not for us on %s, op[%x] xid[%x]\n", 1052 d->dev->name, b->op, b->xid); 1053 goto drop_unlock; 1054 } 1055 1056 /* Parse extensions */ 1057 if (ext_len >= 4 && 1058 !memcmp(b->exten, ic_bootp_cookie, 4)) { /* Check magic cookie */ 1059 u8 *end = (u8 *) b + ntohs(b->iph.tot_len); 1060 u8 *ext; 1061 1062 #ifdef IPCONFIG_DHCP 1063 if (ic_proto_enabled & IC_USE_DHCP) { 1064 __be32 server_id = NONE; 1065 int mt = 0; 1066 1067 ext = &b->exten[4]; 1068 while (ext < end && *ext != 0xff) { 1069 u8 *opt = ext++; 1070 if (*opt == 0) /* Padding */ 1071 continue; 1072 ext += *ext + 1; 1073 if (ext >= end) 1074 break; 1075 switch (*opt) { 1076 case 53: /* Message type */ 1077 if (opt[1]) 1078 mt = opt[2]; 1079 break; 1080 case 54: /* Server ID (IP address) */ 1081 if (opt[1] >= 4) 1082 memcpy(&server_id, opt + 2, 4); 1083 break; 1084 } 1085 } 1086 1087 pr_debug("DHCP: Got message type %d (%s)\n", mt, d->dev->name); 1088 1089 switch (mt) { 1090 case DHCPOFFER: 1091 /* While in the process of accepting one offer, 1092 * ignore all others. 1093 */ 1094 if (ic_myaddr != NONE) 1095 goto drop_unlock; 1096 1097 /* Let's accept that offer. */ 1098 ic_myaddr = b->your_ip; 1099 ic_servaddr = server_id; 1100 pr_debug("DHCP: Offered address %pI4 by server %pI4\n", 1101 &ic_myaddr, &b->iph.saddr); 1102 /* The DHCP indicated server address takes 1103 * precedence over the bootp header one if 1104 * they are different. 1105 */ 1106 if ((server_id != NONE) && 1107 (b->server_ip != server_id)) 1108 b->server_ip = ic_servaddr; 1109 break; 1110 1111 case DHCPACK: 1112 if (memcmp(dev->dev_addr, b->hw_addr, dev->addr_len) != 0) 1113 goto drop_unlock; 1114 1115 /* Yeah! */ 1116 break; 1117 1118 default: 1119 /* Urque. Forget it*/ 1120 ic_myaddr = NONE; 1121 ic_servaddr = NONE; 1122 goto drop_unlock; 1123 } 1124 1125 ic_dhcp_msgtype = mt; 1126 1127 } 1128 #endif /* IPCONFIG_DHCP */ 1129 1130 ext = &b->exten[4]; 1131 while (ext < end && *ext != 0xff) { 1132 u8 *opt = ext++; 1133 if (*opt == 0) /* Padding */ 1134 continue; 1135 ext += *ext + 1; 1136 if (ext < end) 1137 ic_do_bootp_ext(opt); 1138 } 1139 } 1140 1141 /* We have a winner! */ 1142 ic_dev = d; 1143 ic_myaddr = b->your_ip; 1144 ic_servaddr = b->server_ip; 1145 ic_addrservaddr = b->iph.saddr; 1146 if (ic_gateway == NONE && b->relay_ip) 1147 ic_gateway = b->relay_ip; 1148 if (ic_nameservers[0] == NONE) 1149 ic_nameservers[0] = ic_servaddr; 1150 ic_got_reply = IC_BOOTP; 1151 1152 drop_unlock: 1153 /* Show's over. Nothing to see here. */ 1154 spin_unlock(&ic_recv_lock); 1155 1156 drop: 1157 /* Throw the packet out. */ 1158 kfree_skb(skb); 1159 1160 return 0; 1161 } 1162 1163 1164 #endif 1165 1166 1167 /* 1168 * Dynamic IP configuration -- DHCP, BOOTP, RARP. 1169 */ 1170 1171 #ifdef IPCONFIG_DYNAMIC 1172 1173 static int __init ic_dynamic(void) 1174 { 1175 int retries; 1176 struct ic_device *d; 1177 unsigned long start_jiffies, timeout, jiff; 1178 int do_bootp = ic_proto_have_if & IC_BOOTP; 1179 int do_rarp = ic_proto_have_if & IC_RARP; 1180 1181 /* 1182 * If none of DHCP/BOOTP/RARP was selected, return with an error. 1183 * This routine gets only called when some pieces of information 1184 * are missing, and without DHCP/BOOTP/RARP we are unable to get it. 1185 */ 1186 if (!ic_proto_enabled) { 1187 pr_err("IP-Config: Incomplete network configuration information\n"); 1188 return -1; 1189 } 1190 1191 #ifdef IPCONFIG_BOOTP 1192 if ((ic_proto_enabled ^ ic_proto_have_if) & IC_BOOTP) 1193 pr_err("DHCP/BOOTP: No suitable device found\n"); 1194 #endif 1195 #ifdef IPCONFIG_RARP 1196 if ((ic_proto_enabled ^ ic_proto_have_if) & IC_RARP) 1197 pr_err("RARP: No suitable device found\n"); 1198 #endif 1199 1200 if (!ic_proto_have_if) 1201 /* Error message already printed */ 1202 return -1; 1203 1204 /* 1205 * Setup protocols 1206 */ 1207 #ifdef IPCONFIG_BOOTP 1208 if (do_bootp) 1209 ic_bootp_init(); 1210 #endif 1211 #ifdef IPCONFIG_RARP 1212 if (do_rarp) 1213 ic_rarp_init(); 1214 #endif 1215 1216 /* 1217 * Send requests and wait, until we get an answer. This loop 1218 * seems to be a terrible waste of CPU time, but actually there is 1219 * only one process running at all, so we don't need to use any 1220 * scheduler functions. 1221 * [Actually we could now, but the nothing else running note still 1222 * applies.. - AC] 1223 */ 1224 pr_notice("Sending %s%s%s requests .", 1225 do_bootp 1226 ? ((ic_proto_enabled & IC_USE_DHCP) ? "DHCP" : "BOOTP") : "", 1227 (do_bootp && do_rarp) ? " and " : "", 1228 do_rarp ? "RARP" : ""); 1229 1230 start_jiffies = jiffies; 1231 d = ic_first_dev; 1232 retries = CONF_SEND_RETRIES; 1233 get_random_bytes(&timeout, sizeof(timeout)); 1234 timeout = CONF_BASE_TIMEOUT + (timeout % (unsigned int) CONF_TIMEOUT_RANDOM); 1235 for (;;) { 1236 #ifdef IPCONFIG_BOOTP 1237 if (do_bootp && (d->able & IC_BOOTP)) 1238 ic_bootp_send_if(d, jiffies - start_jiffies); 1239 #endif 1240 #ifdef IPCONFIG_RARP 1241 if (do_rarp && (d->able & IC_RARP)) 1242 ic_rarp_send_if(d); 1243 #endif 1244 1245 if (!d->next) { 1246 jiff = jiffies + timeout; 1247 while (time_before(jiffies, jiff) && !ic_got_reply) 1248 schedule_timeout_uninterruptible(1); 1249 } 1250 #ifdef IPCONFIG_DHCP 1251 /* DHCP isn't done until we get a DHCPACK. */ 1252 if ((ic_got_reply & IC_BOOTP) && 1253 (ic_proto_enabled & IC_USE_DHCP) && 1254 ic_dhcp_msgtype != DHCPACK) { 1255 ic_got_reply = 0; 1256 /* continue on device that got the reply */ 1257 d = ic_dev; 1258 pr_cont(","); 1259 continue; 1260 } 1261 #endif /* IPCONFIG_DHCP */ 1262 1263 if (ic_got_reply) { 1264 pr_cont(" OK\n"); 1265 break; 1266 } 1267 1268 if ((d = d->next)) 1269 continue; 1270 1271 if (! --retries) { 1272 pr_cont(" timed out!\n"); 1273 break; 1274 } 1275 1276 d = ic_first_dev; 1277 1278 timeout = timeout CONF_TIMEOUT_MULT; 1279 if (timeout > CONF_TIMEOUT_MAX) 1280 timeout = CONF_TIMEOUT_MAX; 1281 1282 pr_cont("."); 1283 } 1284 1285 #ifdef IPCONFIG_BOOTP 1286 if (do_bootp) 1287 ic_bootp_cleanup(); 1288 #endif 1289 #ifdef IPCONFIG_RARP 1290 if (do_rarp) 1291 ic_rarp_cleanup(); 1292 #endif 1293 1294 if (!ic_got_reply) { 1295 ic_myaddr = NONE; 1296 return -1; 1297 } 1298 1299 pr_info("IP-Config: Got %s answer from %pI4, my address is %pI4\n", 1300 ((ic_got_reply & IC_RARP) ? "RARP" 1301 : (ic_proto_enabled & IC_USE_DHCP) ? "DHCP" : "BOOTP"), 1302 &ic_addrservaddr, &ic_myaddr); 1303 1304 return 0; 1305 } 1306 1307 #endif /* IPCONFIG_DYNAMIC */ 1308 1309 #ifdef CONFIG_PROC_FS 1310 /* proc_dir_entry for /proc/net/ipconfig */ 1311 static struct proc_dir_entry *ipconfig_dir; 1312 1313 /* Name servers: */ 1314 static int pnp_seq_show(struct seq_file *seq, void *v) 1315 { 1316 int i; 1317 1318 if (ic_proto_used & IC_PROTO) 1319 seq_printf(seq, "#PROTO: %s\n", 1320 (ic_proto_used & IC_RARP) ? "RARP" 1321 : (ic_proto_used & IC_USE_DHCP) ? "DHCP" : "BOOTP"); 1322 else 1323 seq_puts(seq, "#MANUAL\n"); 1324 1325 if (ic_domain[0]) 1326 seq_printf(seq, 1327 "domain %s\n", ic_domain); 1328 for (i = 0; i < CONF_NAMESERVERS_MAX; i++) { 1329 if (ic_nameservers[i] != NONE) 1330 seq_printf(seq, "nameserver %pI4\n", 1331 &ic_nameservers[i]); 1332 } 1333 if (ic_servaddr != NONE) 1334 seq_printf(seq, "bootserver %pI4\n", 1335 &ic_servaddr); 1336 return 0; 1337 } 1338 1339 /* Create the /proc/net/ipconfig directory */ 1340 static int __init ipconfig_proc_net_init(void) 1341 { 1342 ipconfig_dir = proc_net_mkdir(&init_net, "ipconfig", init_net.proc_net); 1343 if (!ipconfig_dir) 1344 return -ENOMEM; 1345 1346 return 0; 1347 } 1348 1349 /* Create a new file under /proc/net/ipconfig */ 1350 static int ipconfig_proc_net_create(const char *name, 1351 const struct proc_ops *proc_ops) 1352 { 1353 char *pname; 1354 struct proc_dir_entry *p; 1355 1356 if (!ipconfig_dir) 1357 return -ENOMEM; 1358 1359 pname = kasprintf(GFP_KERNEL, "%s%s", "ipconfig/", name); 1360 if (!pname) 1361 return -ENOMEM; 1362 1363 p = proc_create(pname, 0444, init_net.proc_net, proc_ops); 1364 kfree(pname); 1365 if (!p) 1366 return -ENOMEM; 1367 1368 return 0; 1369 } 1370 1371 /* Write NTP server IP addresses to /proc/net/ipconfig/ntp_servers */ 1372 static int ntp_servers_show(struct seq_file *seq, void *v) 1373 { 1374 int i; 1375 1376 for (i = 0; i < CONF_NTP_SERVERS_MAX; i++) { 1377 if (ic_ntp_servers[i] != NONE) 1378 seq_printf(seq, "%pI4\n", &ic_ntp_servers[i]); 1379 } 1380 return 0; 1381 } 1382 DEFINE_PROC_SHOW_ATTRIBUTE(ntp_servers); 1383 #endif /* CONFIG_PROC_FS */ 1384 1385 /* 1386 * Extract IP address from the parameter string if needed. Note that we 1387 * need to have root_server_addr set _before_ IPConfig gets called as it 1388 * can override it. 1389 */ 1390 __be32 __init root_nfs_parse_addr(char *name) 1391 { 1392 __be32 addr; 1393 int octets = 0; 1394 char *cp, *cq; 1395 1396 cp = cq = name; 1397 while (octets < 4) { 1398 while (*cp >= '0' && *cp <= '9') 1399 cp++; 1400 if (cp == cq || cp - cq > 3) 1401 break; 1402 if (*cp == '.' || octets == 3) 1403 octets++; 1404 if (octets < 4) 1405 cp++; 1406 cq = cp; 1407 } 1408 if (octets == 4 && (*cp == ':' || *cp == '\0')) { 1409 if (*cp == ':') 1410 *cp++ = '\0'; 1411 addr = in_aton(name); 1412 memmove(name, cp, strlen(cp) + 1); 1413 } else 1414 addr = NONE; 1415 1416 return addr; 1417 } 1418 1419 #define DEVICE_WAIT_MAX 12 /* 12 seconds */ 1420 1421 static int __init wait_for_devices(void) 1422 { 1423 int i; 1424 1425 for (i = 0; i < DEVICE_WAIT_MAX; i++) { 1426 struct net_device *dev; 1427 int found = 0; 1428 1429 /* make sure deferred device probes are finished */ 1430 wait_for_device_probe(); 1431 1432 rtnl_lock(); 1433 for_each_netdev(&init_net, dev) { 1434 if (ic_is_init_dev(dev)) { 1435 found = 1; 1436 break; 1437 } 1438 } 1439 rtnl_unlock(); 1440 if (found) 1441 return 0; 1442 ssleep(1); 1443 } 1444 return -ENODEV; 1445 } 1446 1447 /* 1448 * IP Autoconfig dispatcher. 1449 */ 1450 1451 static int __init ip_auto_config(void) 1452 { 1453 __be32 addr; 1454 #ifdef IPCONFIG_DYNAMIC 1455 int retries = CONF_OPEN_RETRIES; 1456 #endif 1457 int err; 1458 unsigned int i, count; 1459 1460 /* Initialise all name servers and NTP servers to NONE (but only if the 1461 * "ip=" or "nfsaddrs=" kernel command line parameters weren't decoded, 1462 * otherwise we'll overwrite the IP addresses specified there) 1463 */ 1464 if (ic_set_manually == 0) { 1465 ic_nameservers_predef(); 1466 ic_ntp_servers_predef(); 1467 } 1468 1469 #ifdef CONFIG_PROC_FS 1470 proc_create_single("pnp", 0444, init_net.proc_net, pnp_seq_show); 1471 1472 if (ipconfig_proc_net_init() == 0) 1473 ipconfig_proc_net_create("ntp_servers", &ntp_servers_proc_ops); 1474 #endif /* CONFIG_PROC_FS */ 1475 1476 if (!ic_enable) 1477 return 0; 1478 1479 pr_debug("IP-Config: Entered.\n"); 1480 #ifdef IPCONFIG_DYNAMIC 1481 try_try_again: 1482 #endif 1483 /* Wait for devices to appear */ 1484 err = wait_for_devices(); 1485 if (err) 1486 return err; 1487 1488 /* Setup all network devices */ 1489 err = ic_open_devs(); 1490 if (err) 1491 return err; 1492 1493 /* Give drivers a chance to settle */ 1494 msleep(CONF_POST_OPEN); 1495 1496 /* 1497 * If the config information is insufficient (e.g., our IP address or 1498 * IP address of the boot server is missing or we have multiple network 1499 * interfaces and no default was set), use BOOTP or RARP to get the 1500 * missing values. 1501 */ 1502 if (ic_myaddr == NONE || 1503 #if defined(CONFIG_ROOT_NFS) || defined(CONFIG_CIFS_ROOT) 1504 (root_server_addr == NONE && 1505 ic_servaddr == NONE && 1506 (ROOT_DEV == Root_NFS || ROOT_DEV == Root_CIFS)) || 1507 #endif 1508 ic_first_dev->next) { 1509 #ifdef IPCONFIG_DYNAMIC 1510 if (ic_dynamic() < 0) { 1511 ic_close_devs(); 1512 1513 /* 1514 * I don't know why, but sometimes the 1515 * eepro100 driver (at least) gets upset and 1516 * doesn't work the first time it's opened. 1517 * But then if you close it and reopen it, it 1518 * works just fine. So we need to try that at 1519 * least once before giving up. 1520 * 1521 * Also, if the root will be NFS-mounted, we 1522 * have nowhere to go if DHCP fails. So we 1523 * just have to keep trying forever. 1524 * 1525 * -- Chip 1526 */ 1527 #ifdef CONFIG_ROOT_NFS 1528 if (ROOT_DEV == Root_NFS) { 1529 pr_err("IP-Config: Retrying forever (NFS root)...\n"); 1530 goto try_try_again; 1531 } 1532 #endif 1533 #ifdef CONFIG_CIFS_ROOT 1534 if (ROOT_DEV == Root_CIFS) { 1535 pr_err("IP-Config: Retrying forever (CIFS root)...\n"); 1536 goto try_try_again; 1537 } 1538 #endif 1539 1540 if (--retries) { 1541 pr_err("IP-Config: Reopening network devices...\n"); 1542 goto try_try_again; 1543 } 1544 1545 /* Oh, well. At least we tried. */ 1546 pr_err("IP-Config: Auto-configuration of network failed\n"); 1547 return -1; 1548 } 1549 #else /* !DYNAMIC */ 1550 pr_err("IP-Config: Incomplete network configuration information\n"); 1551 ic_close_devs(); 1552 return -1; 1553 #endif /* IPCONFIG_DYNAMIC */ 1554 } else { 1555 /* Device selected manually or only one device -> use it */ 1556 ic_dev = ic_first_dev; 1557 } 1558 1559 addr = root_nfs_parse_addr(root_server_path); 1560 if (root_server_addr == NONE) 1561 root_server_addr = addr; 1562 1563 /* 1564 * Use defaults wherever applicable. 1565 */ 1566 if (ic_defaults() < 0) 1567 return -1; 1568 1569 /* 1570 * Record which protocol was actually used. 1571 */ 1572 #ifdef IPCONFIG_DYNAMIC 1573 ic_proto_used = ic_got_reply | (ic_proto_enabled & IC_USE_DHCP); 1574 #endif 1575 1576 #ifndef IPCONFIG_SILENT 1577 /* 1578 * Clue in the operator. 1579 */ 1580 pr_info("IP-Config: Complete:\n"); 1581 1582 pr_info(" device=%s, hwaddr=%*phC, ipaddr=%pI4, mask=%pI4, gw=%pI4\n", 1583 ic_dev->dev->name, ic_dev->dev->addr_len, ic_dev->dev->dev_addr, 1584 &ic_myaddr, &ic_netmask, &ic_gateway); 1585 pr_info(" host=%s, domain=%s, nis-domain=%s\n", 1586 utsname()->nodename, ic_domain, utsname()->domainname); 1587 pr_info(" bootserver=%pI4, rootserver=%pI4, rootpath=%s", 1588 &ic_servaddr, &root_server_addr, root_server_path); 1589 if (ic_dev_mtu) 1590 pr_cont(", mtu=%d", ic_dev_mtu); 1591 /* Name servers (if any): */ 1592 for (i = 0, count = 0; i < CONF_NAMESERVERS_MAX; i++) { 1593 if (ic_nameservers[i] != NONE) { 1594 if (i == 0) 1595 pr_info(" nameserver%u=%pI4", 1596 i, &ic_nameservers[i]); 1597 else 1598 pr_cont(", nameserver%u=%pI4", 1599 i, &ic_nameservers[i]); 1600 1601 count++; 1602 } 1603 if ((i + 1 == CONF_NAMESERVERS_MAX) && count > 0) 1604 pr_cont("\n"); 1605 } 1606 /* NTP servers (if any): */ 1607 for (i = 0, count = 0; i < CONF_NTP_SERVERS_MAX; i++) { 1608 if (ic_ntp_servers[i] != NONE) { 1609 if (i == 0) 1610 pr_info(" ntpserver%u=%pI4", 1611 i, &ic_ntp_servers[i]); 1612 else 1613 pr_cont(", ntpserver%u=%pI4", 1614 i, &ic_ntp_servers[i]); 1615 1616 count++; 1617 } 1618 if ((i + 1 == CONF_NTP_SERVERS_MAX) && count > 0) 1619 pr_cont("\n"); 1620 } 1621 #endif /* !SILENT */ 1622 1623 /* 1624 * Close all network devices except the device we've 1625 * autoconfigured and set up routes. 1626 */ 1627 if (ic_setup_if() < 0 || ic_setup_routes() < 0) 1628 err = -1; 1629 else 1630 err = 0; 1631 1632 ic_close_devs(); 1633 1634 return err; 1635 } 1636 1637 late_initcall(ip_auto_config); 1638 1639 1640 /* 1641 * Decode any IP configuration options in the "ip=" or "nfsaddrs=" kernel 1642 * command line parameter. See Documentation/admin-guide/nfs/nfsroot.rst. 1643 */ 1644 static int __init ic_proto_name(char *name) 1645 { 1646 if (!strcmp(name, "on") || !strcmp(name, "any")) { 1647 return 1; 1648 } 1649 if (!strcmp(name, "off") || !strcmp(name, "none")) { 1650 return 0; 1651 } 1652 #ifdef CONFIG_IP_PNP_DHCP 1653 else if (!strncmp(name, "dhcp", 4)) { 1654 char *client_id; 1655 1656 ic_proto_enabled &= ~IC_RARP; 1657 client_id = strstr(name, "dhcp,"); 1658 if (client_id) { 1659 char *v; 1660 1661 client_id = client_id + 5; 1662 v = strchr(client_id, ','); 1663 if (!v) 1664 return 1; 1665 *v = 0; 1666 if (kstrtou8(client_id, 0, dhcp_client_identifier)) 1667 pr_debug("DHCP: Invalid client identifier type\n"); 1668 strncpy(dhcp_client_identifier + 1, v + 1, 251); 1669 *v = ','; 1670 } 1671 return 1; 1672 } 1673 #endif 1674 #ifdef CONFIG_IP_PNP_BOOTP 1675 else if (!strcmp(name, "bootp")) { 1676 ic_proto_enabled &= ~(IC_RARP | IC_USE_DHCP); 1677 return 1; 1678 } 1679 #endif 1680 #ifdef CONFIG_IP_PNP_RARP 1681 else if (!strcmp(name, "rarp")) { 1682 ic_proto_enabled &= ~(IC_BOOTP | IC_USE_DHCP); 1683 return 1; 1684 } 1685 #endif 1686 #ifdef IPCONFIG_DYNAMIC 1687 else if (!strcmp(name, "both")) { 1688 ic_proto_enabled &= ~IC_USE_DHCP; /* backward compat :-( */ 1689 return 1; 1690 } 1691 #endif 1692 return 0; 1693 } 1694 1695 static int __init ip_auto_config_setup(char *addrs) 1696 { 1697 char *cp, *ip, *dp; 1698 int num = 0; 1699 1700 ic_set_manually = 1; 1701 ic_enable = 1; 1702 1703 /* 1704 * If any dhcp, bootp etc options are set, leave autoconfig on 1705 * and skip the below static IP processing. 1706 */ 1707 if (ic_proto_name(addrs)) 1708 return 1; 1709 1710 /* If no static IP is given, turn off autoconfig and bail. */ 1711 if (*addrs == 0 || 1712 strcmp(addrs, "off") == 0 || 1713 strcmp(addrs, "none") == 0) { 1714 ic_enable = 0; 1715 return 1; 1716 } 1717 1718 /* Initialise all name servers and NTP servers to NONE */ 1719 ic_nameservers_predef(); 1720 ic_ntp_servers_predef(); 1721 1722 /* Parse string for static IP assignment. */ 1723 ip = addrs; 1724 while (ip && *ip) { 1725 if ((cp = strchr(ip, ':'))) 1726 *cp++ = '\0'; 1727 if (strlen(ip) > 0) { 1728 pr_debug("IP-Config: Parameter #%d: `%s'\n", num, ip); 1729 switch (num) { 1730 case 0: 1731 if ((ic_myaddr = in_aton(ip)) == ANY) 1732 ic_myaddr = NONE; 1733 break; 1734 case 1: 1735 if ((ic_servaddr = in_aton(ip)) == ANY) 1736 ic_servaddr = NONE; 1737 break; 1738 case 2: 1739 if ((ic_gateway = in_aton(ip)) == ANY) 1740 ic_gateway = NONE; 1741 break; 1742 case 3: 1743 if ((ic_netmask = in_aton(ip)) == ANY) 1744 ic_netmask = NONE; 1745 break; 1746 case 4: 1747 if ((dp = strchr(ip, '.'))) { 1748 *dp++ = '\0'; 1749 strlcpy(utsname()->domainname, dp, 1750 sizeof(utsname()->domainname)); 1751 } 1752 strlcpy(utsname()->nodename, ip, 1753 sizeof(utsname()->nodename)); 1754 ic_host_name_set = 1; 1755 break; 1756 case 5: 1757 strlcpy(user_dev_name, ip, sizeof(user_dev_name)); 1758 break; 1759 case 6: 1760 if (ic_proto_name(ip) == 0 && 1761 ic_myaddr == NONE) { 1762 ic_enable = 0; 1763 } 1764 break; 1765 case 7: 1766 if (CONF_NAMESERVERS_MAX >= 1) { 1767 ic_nameservers[0] = in_aton(ip); 1768 if (ic_nameservers[0] == ANY) 1769 ic_nameservers[0] = NONE; 1770 } 1771 break; 1772 case 8: 1773 if (CONF_NAMESERVERS_MAX >= 2) { 1774 ic_nameservers[1] = in_aton(ip); 1775 if (ic_nameservers[1] == ANY) 1776 ic_nameservers[1] = NONE; 1777 } 1778 break; 1779 case 9: 1780 if (CONF_NTP_SERVERS_MAX >= 1) { 1781 ic_ntp_servers[0] = in_aton(ip); 1782 if (ic_ntp_servers[0] == ANY) 1783 ic_ntp_servers[0] = NONE; 1784 } 1785 break; 1786 } 1787 } 1788 ip = cp; 1789 num++; 1790 } 1791 1792 return 1; 1793 } 1794 __setup("ip=", ip_auto_config_setup); 1795 1796 static int __init nfsaddrs_config_setup(char *addrs) 1797 { 1798 return ip_auto_config_setup(addrs); 1799 } 1800 __setup("nfsaddrs=", nfsaddrs_config_setup); 1801 1802 static int __init vendor_class_identifier_setup(char *addrs) 1803 { 1804 if (strlcpy(vendor_class_identifier, addrs, 1805 sizeof(vendor_class_identifier)) 1806 >= sizeof(vendor_class_identifier)) 1807 pr_warn("DHCP: vendorclass too long, truncated to \"%s\"\n", 1808 vendor_class_identifier); 1809 return 1; 1810 } 1811 __setup("dhcpclass=", vendor_class_identifier_setup); 1812 1813 static int __init set_carrier_timeout(char *str) 1814 { 1815 ssize_t ret; 1816 1817 if (!str) 1818 return 0; 1819 1820 ret = kstrtouint(str, 0, &carrier_timeout); 1821 if (ret) 1822 return 0; 1823 1824 return 1; 1825 } 1826 __setup("carrier_timeout=", set_carrier_timeout); 1827