32 #include <linux/types.h>
33 #include <linux/string.h>
34 #include <linux/kernel.h>
36 #include <linux/random.h>
38 #include <linux/utsname.h>
43 #include <linux/netdevice.h>
44 #include <linux/if_arp.h>
47 #include <linux/socket.h>
49 #include <linux/udp.h>
55 #include <linux/nfs_fs.h>
56 #include <linux/slab.h>
57 #include <linux/export.h>
64 #include <asm/uaccess.h>
66 #include <asm/processor.h>
72 #define DBG(x) printk x
74 #define DBG(x) do { } while(0)
77 #if defined(CONFIG_IP_PNP_DHCP)
80 #if defined(CONFIG_IP_PNP_BOOTP) || defined(CONFIG_IP_PNP_DHCP)
81 #define IPCONFIG_BOOTP
83 #if defined(CONFIG_IP_PNP_RARP)
86 #if defined(IPCONFIG_BOOTP) || defined(IPCONFIG_RARP)
87 #define IPCONFIG_DYNAMIC
91 #define CONF_POST_OPEN 10
92 #define CONF_CARRIER_TIMEOUT 120000
95 #define CONF_OPEN_RETRIES 2
96 #define CONF_SEND_RETRIES 6
97 #define CONF_INTER_TIMEOUT (HZ/2)
98 #define CONF_BASE_TIMEOUT (HZ*2)
99 #define CONF_TIMEOUT_RANDOM (HZ)
100 #define CONF_TIMEOUT_MULT *7/4
101 #define CONF_TIMEOUT_MAX (HZ*30)
102 #define CONF_NAMESERVERS_MAX 3
105 #define NONE cpu_to_be32(INADDR_NONE)
106 #define ANY cpu_to_be32(INADDR_ANY)
122 #ifdef IPCONFIG_BOOTP
125 #ifdef CONFIG_IP_PNP_DHCP
147 static char vendor_class_identifier[253]
__initdata;
151 static int ic_proto_used;
153 static u8 ic_domain[64];
168 #ifdef IPCONFIG_DYNAMIC
170 static volatile int ic_got_reply
__initdata = 0;
196 return user_dev_name[0] ? !
strcmp(dev->
name, user_dev_name) :
202 static int __init ic_open_devs(
void)
206 unsigned short oflags;
209 last = &ic_first_dev;
217 pr_err(
"IP-Config: Failed to open %s\n", dev->
name);
221 if (ic_is_init_dev(dev)) {
226 pr_warn(
"DHCP/BOOTP: Ignoring device %s, MTU %d too small",
231 if (ic_proto_enabled && !able)
235 pr_err(
"IP-Config: Failed to open %s\n",
252 ic_proto_have_if |= able;
253 DBG((
"IP-Config: %s UP (able=%d, xid=%08x)\n",
266 if (ic_is_init_dev(dev) && netif_carrier_ok(dev))
277 if (user_dev_name[0])
278 pr_err(
"IP-Config: Device `%s' not found\n",
281 pr_err(
"IP-Config: No network devices available\n");
287 static void __init ic_close_devs(
void)
298 DBG((
"IP-Config: Downing %s\n", dev->
name));
329 static int __init ic_dev_ioctl(
unsigned int cmd,
struct ifreq *arg)
340 static int __init ic_route_ioctl(
unsigned int cmd,
struct rtentry *arg)
355 static int __init ic_setup_if(
void)
362 strcpy(
ir.ifr_ifrn.ifrn_name, ic_dev->name);
365 pr_err(
"IP-Config: Unable to set interface address (%d)\n",
369 set_sockaddr(sin, ic_netmask, 0);
371 pr_err(
"IP-Config: Unable to set interface netmask (%d)\n",
375 set_sockaddr(sin,
ic_myaddr | ~ic_netmask, 0);
377 pr_err(
"IP-Config: Unable to set interface broadcast address (%d)\n",
385 if (ic_dev_mtu != 0) {
387 ir.ifr_mtu = ic_dev_mtu;
389 pr_err(
"IP-Config: Unable to set interface mtu to %d (%d)\n",
395 static int __init ic_setup_routes(
void)
403 memset(&rm, 0,
sizeof(rm));
405 pr_err(
"IP-Config: Gateway not on directly connected network\n");
408 set_sockaddr((
struct sockaddr_in *) &rm.rt_dst, 0, 0);
409 set_sockaddr((
struct sockaddr_in *) &rm.rt_genmask, 0, 0);
412 if ((err = ic_route_ioctl(
SIOCADDRT, &rm)) < 0) {
413 pr_err(
"IP-Config: Cannot add default route (%d)\n",
426 static int __init ic_defaults(
void)
433 if (!ic_host_name_set)
434 sprintf(init_utsname()->nodename,
"%pI4", &ic_myaddr);
439 if (ic_netmask ==
NONE) {
447 pr_err(
"IP-Config: Unable to guess netmask for address %pI4\n",
451 printk(
"IP-Config: Guessing netmask %pI4\n", &ic_netmask);
467 .func = ic_rarp_recv,
470 static inline void __init ic_rarp_init(
void)
475 static inline void __init ic_rarp_cleanup(
void)
487 unsigned char *rarp_ptr;
489 unsigned char *sha, *tha;
492 if (!net_eq(dev_net(dev), &
init_net))
498 if (!pskb_may_pull(skb,
sizeof(
struct arphdr)))
502 rarp = (
struct arphdr *)skb_transport_header(skb);
518 if (!pskb_may_pull(skb, arp_hdr_len(dev)))
522 rarp = (
struct arphdr *)skb_transport_header(skb);
523 rarp_ptr = (
unsigned char *) (rarp + 1);
526 spin_lock(&ic_recv_lock);
534 while (d && d->
dev != dev)
542 memcpy(&sip, rarp_ptr, 4);
546 memcpy(&tip, rarp_ptr, 4);
558 if (ic_myaddr ==
NONE)
565 spin_unlock(&ic_recv_lock);
588 static inline void __init ic_nameservers_predef(
void)
593 ic_nameservers[i] =
NONE;
600 #ifdef IPCONFIG_BOOTP
623 #define BOOTP_REQUEST 1
624 #define BOOTP_REPLY 2
627 #define DHCPDISCOVER 1
629 #define DHCPREQUEST 3
630 #define DHCPDECLINE 4
633 #define DHCPRELEASE 7
638 static struct packet_type bootp_packet_type __initdata = {
640 .func = ic_bootp_recv,
648 static const u8 ic_bootp_cookie[4] = { 99, 130, 83, 99 };
656 ? DHCPDISCOVER : DHCPREQUEST);
660 #ifdef IPCONFIG_DEBUG
661 printk(
"DHCP: Sending message type %d\n", mt);
664 memcpy(e, ic_bootp_cookie, 4);
671 if (mt == DHCPREQUEST) {
685 static const u8 ic_req_params[] = {
697 *e++ =
sizeof(ic_req_params);
698 memcpy(e, ic_req_params,
sizeof(ic_req_params));
699 e +=
sizeof(ic_req_params);
701 if (ic_host_name_set) {
703 len =
strlen(utsname()->nodename);
705 memcpy(e, utsname()->nodename, len);
708 if (*vendor_class_identifier) {
709 pr_info(
"DHCP: sending class identifier \"%s\"\n",
710 vendor_class_identifier);
712 len =
strlen(vendor_class_identifier);
714 memcpy(e, vendor_class_identifier, len);
724 static void __init ic_bootp_init_ext(
u8 *e)
726 memcpy(e, ic_bootp_cookie, 4);
759 static inline void __init ic_bootp_init(
void)
761 ic_nameservers_predef();
770 static inline void __init ic_bootp_cleanup(
void)
779 static void __init ic_bootp_send_if(
struct ic_device *d,
unsigned long jiffies_diff)
789 skb = alloc_skb(
sizeof(
struct bootp_pkt) + hlen + tlen + 15,
793 skb_reserve(skb, hlen);
794 b = (
struct bootp_pkt *)
skb_put(skb,
sizeof(
struct bootp_pkt));
795 memset(b, 0,
sizeof(
struct bootp_pkt));
798 skb_reset_network_header(skb);
810 b->udph.source =
htons(68);
811 b->udph.dest =
htons(67);
812 b->udph.len =
htons(
sizeof(
struct bootp_pkt) -
sizeof(
struct iphdr));
816 b->op = BOOTP_REQUEST;
818 b->htype = dev->
type;
822 printk(
"Unknown ARP type 0x%04x for device %s\n", dev->
type, dev->
name);
823 b->htype = dev->
type;
829 b->secs =
htons(jiffies_diff /
HZ);
835 ic_dhcp_init_options(b->exten);
838 ic_bootp_init_ext(b->exten);
879 #ifdef IPCONFIG_DEBUG
882 printk(
"DHCP/BOOTP: Got extension %d:",*ext);
883 for (c=ext+2; c<ext+2+ext[1]; c++)
890 if (ic_netmask ==
NONE)
891 memcpy(&ic_netmask, ext+1, 4);
901 for (i = 0; i < servers; i++) {
902 if (ic_nameservers[i] ==
NONE)
903 memcpy(&ic_nameservers[i], ext+1+4*i, 4);
907 ic_bootp_string(utsname()->nodename, ext+1, *ext,
909 ic_host_name_set = 1;
912 ic_bootp_string(ic_domain, ext+1, *ext,
sizeof(ic_domain));
920 memcpy(&mtu, ext+1,
sizeof(mtu));
921 ic_dev_mtu =
ntohs(mtu);
924 ic_bootp_string(utsname()->domainname, ext+1, *ext,
941 if (!net_eq(dev_net(dev), &
init_net))
951 if (!pskb_may_pull(skb,
952 sizeof(
struct iphdr) +
956 b = (
struct bootp_pkt *)skb_network_header(skb);
963 if (ip_is_fragment(h)) {
974 if (b->udph.source !=
htons(67) || b->udph.dest !=
htons(68))
981 ext_len = len - (
sizeof(*b) -
982 sizeof(
struct iphdr) -
989 if (!pskb_may_pull(skb, skb->
len))
992 b = (
struct bootp_pkt *)skb_network_header(skb);
996 spin_lock(&ic_recv_lock);
1004 while (d && d->
dev != dev)
1010 if (b->op != BOOTP_REPLY ||
1025 !
memcmp(b->exten, ic_bootp_cookie, 4)) {
1029 #ifdef IPCONFIG_DHCP
1030 if (ic_proto_enabled & IC_USE_DHCP) {
1035 while (ext < end && *ext != 0xff) {
1049 memcpy(&server_id, opt + 2, 4);
1054 #ifdef IPCONFIG_DEBUG
1055 printk(
"DHCP: Got message type %d\n", mt);
1063 if (ic_myaddr !=
NONE)
1067 ic_myaddr = b->your_ip;
1069 #ifdef IPCONFIG_DEBUG
1070 printk(
"DHCP: Offered address %pI4 by server %pI4\n",
1077 if ((server_id !=
NONE) &&
1078 (b->server_ip != server_id))
1096 ic_dhcp_msgtype = mt;
1102 while (ext < end && *ext != 0xff) {
1108 ic_do_bootp_ext(opt);
1114 ic_myaddr = b->your_ip;
1118 if (ic_nameservers[0] ==
NONE)
1124 spin_unlock(&ic_recv_lock);
1141 #ifdef IPCONFIG_DYNAMIC
1143 static int __init ic_dynamic(
void)
1147 unsigned long start_jiffies,
timeout, jiff;
1148 int do_bootp = ic_proto_have_if &
IC_BOOTP;
1149 int do_rarp = ic_proto_have_if &
IC_RARP;
1156 if (!ic_proto_enabled) {
1157 pr_err(
"IP-Config: Incomplete network configuration information\n");
1161 #ifdef IPCONFIG_BOOTP
1162 if ((ic_proto_enabled ^ ic_proto_have_if) & IC_BOOTP)
1163 pr_err(
"DHCP/BOOTP: No suitable device found\n");
1165 #ifdef IPCONFIG_RARP
1166 if ((ic_proto_enabled ^ ic_proto_have_if) & IC_RARP)
1167 pr_err(
"RARP: No suitable device found\n");
1170 if (!ic_proto_have_if)
1177 #ifdef IPCONFIG_BOOTP
1181 #ifdef IPCONFIG_RARP
1196 ? ((ic_proto_enabled & IC_USE_DHCP) ?
"DHCP" :
"BOOTP") :
"",
1197 (do_bootp && do_rarp) ?
" and " :
"",
1198 do_rarp ?
"RARP" :
"");
1209 #ifdef IPCONFIG_BOOTP
1210 if (do_bootp && (d->
able & IC_BOOTP))
1211 ic_bootp_send_if(d, jiffies - start_jiffies);
1213 #ifdef IPCONFIG_RARP
1214 if (do_rarp && (d->
able & IC_RARP))
1219 while (
time_before(jiffies, jiff) && !ic_got_reply)
1221 #ifdef IPCONFIG_DHCP
1223 if ((ic_got_reply & IC_BOOTP) &&
1225 ic_dhcp_msgtype != DHCPACK) {
1254 #ifdef IPCONFIG_BOOTP
1258 #ifdef IPCONFIG_RARP
1263 if (!ic_got_reply) {
1268 printk(
"IP-Config: Got %s answer from %pI4, ",
1269 ((ic_got_reply & IC_RARP) ?
"RARP"
1270 : (ic_proto_enabled & IC_USE_DHCP) ?
"DHCP" :
"BOOTP"),
1272 pr_cont(
"my address is %pI4\n", &ic_myaddr);
1279 #ifdef CONFIG_PROC_FS
1281 static int pnp_seq_show(
struct seq_file *seq,
void *
v)
1287 (ic_proto_used & IC_RARP) ?
"RARP"
1288 : (ic_proto_used & IC_USE_DHCP) ?
"DHCP" :
"BOOTP");
1294 "domain %s\n", ic_domain);
1296 if (ic_nameservers[i] !=
NONE)
1298 &ic_nameservers[i]);
1306 static int pnp_seq_open(
struct inode *indoe,
struct file *
file)
1313 .open = pnp_seq_open,
1332 while (octets < 4) {
1333 while (*cp >=
'0' && *cp <=
'9')
1335 if (cp == cq || cp - cq > 3)
1337 if (*cp ==
'.' || octets == 3)
1343 if (octets == 4 && (*cp ==
':' || *cp ==
'\0')) {
1354 #define DEVICE_WAIT_MAX 12
1356 static int __init wait_for_devices(
void)
1366 if (ic_is_init_dev(dev)) {
1383 static int __init ip_auto_config(
void)
1386 #ifdef IPCONFIG_DYNAMIC
1392 #ifdef CONFIG_PROC_FS
1399 DBG((
"IP-Config: Entered.\n"));
1400 #ifdef IPCONFIG_DYNAMIC
1404 err = wait_for_devices();
1409 err = ic_open_devs();
1422 if (ic_myaddr ==
NONE ||
1423 #ifdef CONFIG_ROOT_NFS
1428 ic_first_dev->next) {
1429 #ifdef IPCONFIG_DYNAMIC
1430 if (ic_dynamic() < 0) {
1447 #ifdef CONFIG_ROOT_NFS
1449 pr_err(
"IP-Config: Retrying forever (NFS root)...\n");
1455 pr_err(
"IP-Config: Reopening network devices...\n");
1460 pr_err(
"IP-Config: Auto-configuration of network failed\n");
1464 pr_err(
"IP-Config: Incomplete network configuration information\n");
1470 ic_dev = ic_first_dev->dev;
1480 if (ic_defaults() < 0)
1488 if (ic_setup_if() < 0 || ic_setup_routes() < 0)
1494 #ifdef IPCONFIG_DYNAMIC
1498 #ifndef IPCONFIG_SILENT
1502 pr_info(
"IP-Config: Complete:\n");
1503 pr_info(
" device=%s, addr=%pI4, mask=%pI4, gw=%pI4\n",
1504 ic_dev->name, &ic_myaddr, &ic_netmask, &
ic_gateway);
1505 pr_info(
" host=%s, domain=%s, nis-domain=%s\n",
1506 utsname()->nodename, ic_domain, utsname()->domainname);
1507 pr_info(
" bootserver=%pI4, rootserver=%pI4, rootpath=%s",
1510 pr_cont(
", mtu=%d", ic_dev_mtu);
1512 if (ic_nameservers[i] !=
NONE) {
1514 i, &ic_nameservers[i]);
1518 if (ic_nameservers[i] !=
NONE)
1519 pr_cont(
", nameserver%u=%pI4\n", i, &ic_nameservers[i]);
1540 #ifdef CONFIG_IP_PNP_DHCP
1541 else if (!
strcmp(name,
"dhcp")) {
1546 #ifdef CONFIG_IP_PNP_BOOTP
1547 else if (!
strcmp(name,
"bootp")) {
1552 #ifdef CONFIG_IP_PNP_RARP
1553 else if (!
strcmp(name,
"rarp")) {
1558 #ifdef IPCONFIG_DYNAMIC
1559 else if (!
strcmp(name,
"both")) {
1567 static int __init ip_auto_config_setup(
char *addrs)
1579 if (ic_proto_name(addrs))
1584 strcmp(addrs,
"off") == 0 ||
1585 strcmp(addrs,
"none") == 0) {
1590 ic_nameservers_predef();
1595 if ((cp =
strchr(ip,
':')))
1598 DBG((
"IP-Config: Parameter #%d: `%s'\n", num, ip));
1617 if ((dp =
strchr(ip,
'.'))) {
1619 strlcpy(utsname()->domainname, dp,
1620 sizeof(utsname()->domainname));
1622 strlcpy(utsname()->nodename, ip,
1623 sizeof(utsname()->nodename));
1624 ic_host_name_set = 1;
1627 strlcpy(user_dev_name, ip,
sizeof(user_dev_name));
1630 if (ic_proto_name(ip) == 0 &&
1631 ic_myaddr ==
NONE) {
1636 if (CONF_NAMESERVERS_MAX >= 1) {
1637 ic_nameservers[0] =
in_aton(ip);
1638 if (ic_nameservers[0] ==
ANY)
1639 ic_nameservers[0] =
NONE;
1643 if (CONF_NAMESERVERS_MAX >= 2) {
1644 ic_nameservers[1] =
in_aton(ip);
1645 if (ic_nameservers[1] ==
ANY)
1646 ic_nameservers[1] =
NONE;
1657 __setup(
"ip=", ip_auto_config_setup);
1659 static int __init nfsaddrs_config_setup(
char *addrs)
1661 return ip_auto_config_setup(addrs);
1663 __setup(
"nfsaddrs=", nfsaddrs_config_setup);
1665 static int __init vendor_class_identifier_setup(
char *addrs)
1667 if (
strlcpy(vendor_class_identifier, addrs,
1668 sizeof(vendor_class_identifier))
1669 >=
sizeof(vendor_class_identifier))
1670 pr_warn(
"DHCP: vendorclass too long, truncated to \"%s\"",
1671 vendor_class_identifier);
1674 __setup(
"dhcpclass=", vendor_class_identifier_setup);