📄 ipconfig.c
字号:
/* * $Id: ipconfig.c,v 1.43.2.1 2001/12/13 10:39:53 davem Exp $ * * Automatic Configuration of IP -- use DHCP, BOOTP, RARP, or * user-supplied information to configure own IP address and routes. * * Copyright (C) 1996-1998 Martin Mares <mj@atrey.karlin.mff.cuni.cz> * * Derived from network configuration code in fs/nfs/nfsroot.c, * originally Copyright (C) 1995, 1996 Gero Kuhlmann and me. * * BOOTP rewritten to construct and analyse packets itself instead * of misusing the IP layer. num_bugs_causing_wrong_arp_replies--; * -- MJ, December 1998 * * Fixed ip_auto_config_setup calling at startup in the new "Linker Magic" * initialization scheme. * - Arnaldo Carvalho de Melo <acme@conectiva.com.br>, 08/11/1999 * * DHCP support added. To users this looks like a whole separate * protocol, but we know it's just a bag on the side of BOOTP. * -- Chip Salzenberg <chip@valinux.com>, May 2000 * * Ported DHCP support from 2.2.16 to 2.4.0-test4 * -- Eric Biederman <ebiederman@lnxi.com>, 30 Aug 2000 * * Merged changes from 2.2.19 into 2.4.3 * -- Eric Biederman <ebiederman@lnxi.com>, 22 April Aug 2001 */#include <linux/config.h>#include <linux/types.h>#include <linux/string.h>#include <linux/kernel.h>#include <linux/sched.h>#include <linux/random.h>#include <linux/init.h>#include <linux/utsname.h>#include <linux/in.h>#include <linux/if.h>#include <linux/inet.h>#include <linux/netdevice.h>#include <linux/if_arp.h>#include <linux/skbuff.h>#include <linux/ip.h>#include <linux/socket.h>#include <linux/route.h>#include <linux/udp.h>#include <linux/proc_fs.h>#include <linux/major.h>#include <net/arp.h>#include <net/ip.h>#include <net/ipconfig.h>#include <asm/uaccess.h>#include <asm/checksum.h>#include <asm/processor.h>/* Define this to allow debugging output */#undef IPCONFIG_DEBUG#ifdef IPCONFIG_DEBUG#define DBG(x) printk x#else#define DBG(x) do { } while(0)#endif#if defined(CONFIG_IP_PNP_DHCP)#define IPCONFIG_DHCP#endif#if defined(CONFIG_IP_PNP_BOOTP) || defined(CONFIG_IP_PNP_DHCP)#define IPCONFIG_BOOTP#endif#if defined(CONFIG_IP_PNP_RARP)#define IPCONFIG_RARP#endif#if defined(IPCONFIG_BOOTP) || defined(IPCONFIG_RARP)#define IPCONFIG_DYNAMIC#endif/* Define the friendly delay before and after opening net devices */#define CONF_PRE_OPEN (HZ/2) /* Before opening: 1/2 second */#define CONF_POST_OPEN (1*HZ) /* After opening: 1 second *//* Define the timeout for waiting for a DHCP/BOOTP/RARP reply */#define CONF_OPEN_RETRIES 2 /* (Re)open devices twice */#define CONF_SEND_RETRIES 6 /* Send six requests per open */#define CONF_INTER_TIMEOUT (HZ/2) /* Inter-device timeout: 1/2 second */#define CONF_BASE_TIMEOUT (HZ*2) /* Initial timeout: 2 seconds */#define CONF_TIMEOUT_RANDOM (HZ) /* Maximum amount of randomization */#define CONF_TIMEOUT_MULT *7/4 /* Rate of timeout growth */#define CONF_TIMEOUT_MAX (HZ*30) /* Maximum allowed timeout *//* * Public IP configuration *//* This is used by platforms which might be able to set the ipconfig * variables using firmware environment vars. If this is set, it will * ignore such firmware variables. */int ic_set_manually __initdata = 0; /* IPconfig parameters set manually */int ic_enable __initdata = 0; /* IP config enabled? *//* Protocol choice */int ic_proto_enabled __initdata = 0#ifdef IPCONFIG_BOOTP | IC_BOOTP#endif#ifdef CONFIG_IP_PNP_DHCP | IC_USE_DHCP#endif#ifdef IPCONFIG_RARP | IC_RARP#endif ;int ic_host_name_set __initdata = 0; /* Host name set by us? */u32 ic_myaddr __initdata = INADDR_NONE; /* My IP address */u32 ic_netmask __initdata = INADDR_NONE; /* Netmask for local subnet */u32 ic_gateway __initdata = INADDR_NONE; /* Gateway IP address */u32 ic_servaddr __initdata = INADDR_NONE; /* Boot server IP address */u32 root_server_addr __initdata = INADDR_NONE; /* Address of NFS server */u8 root_server_path[256] __initdata = { 0, }; /* Path to mount as root *//* Persistent data: */int ic_proto_used; /* Protocol used, if any */u32 ic_nameserver = INADDR_NONE; /* DNS Server IP address */u8 ic_domain[64]; /* DNS (not NIS) domain name *//* * Private state. *//* Name of user-selected boot device */static char user_dev_name[IFNAMSIZ] __initdata = { 0, };/* Protocols supported by available interfaces */static int ic_proto_have_if __initdata = 0;#ifdef IPCONFIG_DYNAMICstatic spinlock_t ic_recv_lock = SPIN_LOCK_UNLOCKED;static volatile int ic_got_reply __initdata = 0; /* Proto(s) that replied */#endif#ifdef IPCONFIG_DHCPstatic int ic_dhcp_msgtype __initdata = 0; /* DHCP msg type received */#endif/* * Network devices */struct ic_device { struct ic_device *next; struct net_device *dev; unsigned short flags; short able; u32 xid;};static struct ic_device *ic_first_dev __initdata = NULL;/* List of open device */static struct net_device *ic_dev __initdata = NULL; /* Selected device */static int __init ic_open_devs(void){ struct ic_device *d, **last; struct net_device *dev; unsigned short oflags; last = &ic_first_dev; rtnl_shlock(); for (dev = dev_base; dev; dev = dev->next) { if (user_dev_name[0] ? !strcmp(dev->name, user_dev_name) : (!(dev->flags & IFF_LOOPBACK) && (dev->flags & (IFF_POINTOPOINT|IFF_BROADCAST)) && strncmp(dev->name, "dummy", 5))) { int able = 0; if (dev->mtu >= 364) able |= IC_BOOTP; else printk(KERN_WARNING "DHCP/BOOTP: Ignoring device %s, MTU %d too small", dev->name, dev->mtu); if (!(dev->flags & IFF_NOARP)) able |= IC_RARP; able &= ic_proto_enabled; if (ic_proto_enabled && !able) continue; oflags = dev->flags; if (dev_change_flags(dev, oflags | IFF_UP) < 0) { printk(KERN_ERR "IP-Config: Failed to open %s\n", dev->name); continue; } if (!(d = kmalloc(sizeof(struct ic_device), GFP_KERNEL))) { rtnl_shunlock(); return -1; } d->dev = dev; *last = d; last = &d->next; d->flags = oflags; d->able = able; if (able & IC_BOOTP) get_random_bytes(&d->xid, sizeof(u32)); else d->xid = 0; ic_proto_have_if |= able; DBG(("IP-Config: %s UP (able=%d, xid=%08x)\n", dev->name, able, d->xid)); } } rtnl_shunlock(); *last = NULL; if (!ic_first_dev) { if (user_dev_name[0]) printk(KERN_ERR "IP-Config: Device `%s' not found.\n", user_dev_name); else printk(KERN_ERR "IP-Config: No network devices available.\n"); return -1; } return 0;}static void __init ic_close_devs(void){ struct ic_device *d, *next; struct net_device *dev; rtnl_shlock(); next = ic_first_dev; while ((d = next)) { next = d->next; dev = d->dev; if (dev != ic_dev) { DBG(("IP-Config: Downing %s\n", dev->name)); dev_change_flags(dev, d->flags); } kfree(d); } rtnl_shunlock();}/* * Interface to various network functions. */static inline voidset_sockaddr(struct sockaddr_in *sin, u32 addr, u16 port){ sin->sin_family = AF_INET; sin->sin_addr.s_addr = addr; sin->sin_port = port;}static int __init ic_dev_ioctl(unsigned int cmd, struct ifreq *arg){ int res; mm_segment_t oldfs = get_fs(); set_fs(get_ds()); res = devinet_ioctl(cmd, arg); set_fs(oldfs); return res;}static int __init ic_route_ioctl(unsigned int cmd, struct rtentry *arg){ int res; mm_segment_t oldfs = get_fs(); set_fs(get_ds()); res = ip_rt_ioctl(cmd, arg); set_fs(oldfs); return res;}/* * Set up interface addresses and routes. */static int __init ic_setup_if(void){ struct ifreq ir; struct sockaddr_in *sin = (void *) &ir.ifr_ifru.ifru_addr; int err; memset(&ir, 0, sizeof(ir)); strcpy(ir.ifr_ifrn.ifrn_name, ic_dev->name); set_sockaddr(sin, ic_myaddr, 0); if ((err = ic_dev_ioctl(SIOCSIFADDR, &ir)) < 0) { printk(KERN_ERR "IP-Config: Unable to set interface address (%d).\n", err); return -1; } set_sockaddr(sin, ic_netmask, 0); if ((err = ic_dev_ioctl(SIOCSIFNETMASK, &ir)) < 0) { printk(KERN_ERR "IP-Config: Unable to set interface netmask (%d).\n", err); return -1; } set_sockaddr(sin, ic_myaddr | ~ic_netmask, 0); if ((err = ic_dev_ioctl(SIOCSIFBRDADDR, &ir)) < 0) { printk(KERN_ERR "IP-Config: Unable to set interface broadcast address (%d).\n", err); return -1; } return 0;}static int __init ic_setup_routes(void){ /* No need to setup device routes, only the default route... */ if (ic_gateway != INADDR_NONE) { struct rtentry rm; int err; memset(&rm, 0, sizeof(rm)); if ((ic_gateway ^ ic_myaddr) & ic_netmask) { printk(KERN_ERR "IP-Config: Gateway not on directly connected network.\n"); return -1; } set_sockaddr((struct sockaddr_in *) &rm.rt_dst, 0, 0); set_sockaddr((struct sockaddr_in *) &rm.rt_genmask, 0, 0); set_sockaddr((struct sockaddr_in *) &rm.rt_gateway, ic_gateway, 0); rm.rt_flags = RTF_UP | RTF_GATEWAY; if ((err = ic_route_ioctl(SIOCADDRT, &rm)) < 0) { printk(KERN_ERR "IP-Config: Cannot add default route (%d).\n", err); return -1; } } return 0;}/* * Fill in default values for all missing parameters. */static int __init ic_defaults(void){ /* * At this point we have no userspace running so need not * claim locks on system_utsname */ if (!ic_host_name_set) sprintf(system_utsname.nodename, "%u.%u.%u.%u", NIPQUAD(ic_myaddr)); if (root_server_addr == INADDR_NONE) root_server_addr = ic_servaddr; if (ic_netmask == INADDR_NONE) { if (IN_CLASSA(ntohl(ic_myaddr))) ic_netmask = __constant_htonl(IN_CLASSA_NET); else if (IN_CLASSB(ntohl(ic_myaddr))) ic_netmask = __constant_htonl(IN_CLASSB_NET); else if (IN_CLASSC(ntohl(ic_myaddr))) ic_netmask = __constant_htonl(IN_CLASSC_NET); else { printk(KERN_ERR "IP-Config: Unable to guess netmask for address %u.%u.%u.%u\n", NIPQUAD(ic_myaddr)); return -1; } printk("IP-Config: Guessing netmask %u.%u.%u.%u\n", NIPQUAD(ic_netmask)); } return 0;}/* * RARP support. */#ifdef IPCONFIG_RARPstatic int ic_rarp_recv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt);static struct packet_type rarp_packet_type __initdata = { type: __constant_htons(ETH_P_RARP), func: ic_rarp_recv,};static inline void ic_rarp_init(void){ dev_add_pack(&rarp_packet_type);}static inline void ic_rarp_cleanup(void){ dev_remove_pack(&rarp_packet_type);}/* * Process received RARP packet. */static int __initic_rarp_recv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt){ struct arphdr *rarp = (struct arphdr *)skb->h.raw; unsigned char *rarp_ptr = (unsigned char *) (rarp + 1); unsigned long sip, tip; unsigned char *sha, *tha; /* s for "source", t for "target" */ struct ic_device *d; /* One reply at a time, please. */ spin_lock(&ic_recv_lock); /* If we already have a reply, just drop the packet */ if (ic_got_reply) goto drop; /* Find the ic_device that the packet arrived on */ d = ic_first_dev; while (d && d->dev != dev) d = d->next; if (!d) goto drop; /* should never happen */ /* If this test doesn't pass, it's not IP, or we should ignore it anyway */ if (rarp->ar_hln != dev->addr_len || dev->type != ntohs(rarp->ar_hrd)) goto drop; /* If it's not a RARP reply, delete it. */ if (rarp->ar_op != __constant_htons(ARPOP_RREPLY)) goto drop; /* If it's not Ethernet, delete it. */ if (rarp->ar_pro != __constant_htons(ETH_P_IP)) goto drop; /* Extract variable-width fields */ sha = rarp_ptr; rarp_ptr += dev->addr_len; memcpy(&sip, rarp_ptr, 4); rarp_ptr += 4; tha = rarp_ptr; rarp_ptr += dev->addr_len; memcpy(&tip, rarp_ptr, 4); /* Discard packets which are not meant for us. */ if (memcmp(tha, dev->dev_addr, dev->addr_len)) goto drop; /* Discard packets which are not from specified server. */ if (ic_servaddr != INADDR_NONE && ic_servaddr != sip) goto drop; /* We have a winner! */ ic_dev = dev; if (ic_myaddr == INADDR_NONE) ic_myaddr = tip; ic_servaddr = sip;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -