📄 ipconfig.c
字号:
/* * $Id: ipconfig.c,v 1.34 2000/07/26 01:04:18 davem Exp $ * * Automatic Configuration of IP -- use BOOTP or 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 */#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 <net/arp.h>#include <net/ip.h>#include <net/ipconfig.h>#include <asm/uaccess.h>#include <asm/checksum.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/* Define the timeout for waiting for a RARP/BOOTP reply */#define CONF_BASE_TIMEOUT (HZ*5) /* Initial timeout: 5 seconds */#define CONF_RETRIES 10 /* 10 retries */#define CONF_TIMEOUT_RANDOM (HZ) /* Maximum amount of randomization */#define CONF_TIMEOUT_MULT *5/4 /* Rate of timeout growth */#define CONF_TIMEOUT_MAX (HZ*30) /* Maximum allowed timeout *//* IP configuration */static char user_dev_name[IFNAMSIZ] __initdata = { 0, };/* Name of user-selected boot device */u32 ic_myaddr __initdata = INADDR_NONE; /* My IP address */u32 ic_servaddr __initdata = INADDR_NONE; /* Server IP address */u32 ic_gateway __initdata = INADDR_NONE; /* Gateway IP address */u32 ic_netmask __initdata = INADDR_NONE; /* Netmask for local subnet */int ic_enable __initdata = 1; /* Automatic IP configuration enabled */int ic_host_name_set __initdata = 0; /* Host name configured manually */int ic_set_manually __initdata = 0; /* IPconfig parameters set manually */u32 root_server_addr __initdata = INADDR_NONE; /* Address of boot server */u8 root_server_path[256] __initdata = { 0, }; /* Path to mount as root */#if defined(CONFIG_IP_PNP_BOOTP) || defined(CONFIG_IP_PNP_RARP)#define CONFIG_IP_PNP_DYNAMICint ic_proto_enabled __initdata = 0 /* Protocols enabled */#ifdef CONFIG_IP_PNP_BOOTP | IC_BOOTP#endif#ifdef CONFIG_IP_PNP_RARP | IC_RARP#endif ;static int ic_got_reply __initdata = 0; /* Protocol(s) we got reply from */#elsestatic int ic_proto_enabled __initdata = 0;#endifstatic int ic_proto_have_if __initdata = 0;/* * Network devices */struct ic_device { struct ic_device *next; struct net_device *dev; unsigned short flags; int able;};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 "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))) return -1; d->dev = dev; *last = d; last = &d->next; d->flags = oflags; d->able = able; ic_proto_have_if |= able; DBG(("IP-Config: Opened %s (able=%d)\n", dev->name, able)); } } 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) strcpy(system_utsname.nodename, in_ntoa(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 = htonl(IN_CLASSA_NET); else if (IN_CLASSB(ntohl(ic_myaddr))) ic_netmask = htonl(IN_CLASSB_NET); else if (IN_CLASSC(ntohl(ic_myaddr))) ic_netmask = 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 CONFIG_IP_PNP_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 = { __constant_htons(ETH_P_RARP), NULL, /* Listen to all devices */ ic_rarp_recv, NULL, NULL};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" */ /* If we already have a reply, just drop the packet */ if (ic_got_reply) goto drop; /* 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 != htons(ARPOP_RREPLY)) goto drop; /* If it's not Ethernet, delete it. */ if (rarp->ar_pro != 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; /* Victory! The packet is what we were looking for! */ if (!ic_got_reply) { ic_got_reply = IC_RARP; ic_dev = dev; if (ic_myaddr == INADDR_NONE) ic_myaddr = tip; ic_servaddr = sip; } /* And throw the packet out... */drop: kfree_skb(skb); return 0;}/* * Send RARP request packet over all devices which allow RARP. */static void __init ic_rarp_send(void){ struct ic_device *d; for (d=ic_first_dev; d; d=d->next) if (d->able & IC_RARP) { struct net_device *dev = d->dev; arp_send(ARPOP_RREQUEST, ETH_P_RARP, 0, dev, 0, NULL, dev->dev_addr, dev->dev_addr); }}#endif/* * BOOTP support. */#ifdef CONFIG_IP_PNP_BOOTPstruct bootp_pkt { /* BOOTP packet format */ struct iphdr iph; /* IP header */ struct udphdr udph; /* UDP header */ u8 op; /* 1=request, 2=reply */ u8 htype; /* HW address type */ u8 hlen; /* HW address length */ u8 hops; /* Used only by gateways */ u32 xid; /* Transaction ID */ u16 secs; /* Seconds since we started */ u16 flags; /* Just what it says */ u32 client_ip; /* Client's IP address if known */ u32 your_ip; /* Assigned IP address */ u32 server_ip; /* Server's IP address */ u32 relay_ip; /* IP address of BOOTP relay */ u8 hw_addr[16]; /* Client's HW address */ u8 serv_name[64]; /* Server host name */ u8 boot_file[128]; /* Name of boot file */ u8 vendor_area[128]; /* Area for extensions */};#define BOOTP_REQUEST 1#define BOOTP_REPLY 2static u32 ic_bootp_xid;static int ic_bootp_recv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt);static struct packet_type bootp_packet_type __initdata = { __constant_htons(ETH_P_IP), NULL, /* Listen to all devices */ ic_bootp_recv, NULL, NULL};/* * Initialize BOOTP extension fields in the request. */static void __init ic_bootp_init_ext(u8 *e){ *e++ = 99; /* RFC1048 Magic Cookie */ *e++ = 130; *e++ = 83; *e++ = 99; *e++ = 1; /* Subnet mask request */ *e++ = 4; e += 4; *e++ = 3; /* Default gateway request */ *e++ = 4; e += 4; *e++ = 12; /* Host name request */ *e++ = 32; e += 32; *e++ = 40; /* NIS Domain name request */ *e++ = 32; e += 32; *e++ = 17; /* Boot path */ *e++ = 32; e += 32; *e = 255; /* End of the list */}/* * Initialize the BOOTP mechanism. */static inline void ic_bootp_init(void){ get_random_bytes(&ic_bootp_xid, sizeof(u32)); DBG(("BOOTP: XID=%08x\n", ic_bootp_xid)); dev_add_pack(&bootp_packet_type);}/* * BOOTP cleanup. */static inline void ic_bootp_cleanup(void){ dev_remove_pack(&bootp_packet_type);}/*
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -