Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 1 | /* dnsmasq is Copyright (c) 2000-2005 Simon Kelley |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 2 | |
| 3 | This program is free software; you can redistribute it and/or modify |
| 4 | it under the terms of the GNU General Public License as published by |
| 5 | the Free Software Foundation; version 2 dated June, 1991. |
| 6 | |
| 7 | This program is distributed in the hope that it will be useful, |
| 8 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 | GNU General Public License for more details. |
| 11 | */ |
| 12 | |
| 13 | /* Author's email: simon@thekelleys.org.uk */ |
| 14 | |
| 15 | #include "dnsmasq.h" |
| 16 | |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 17 | void dhcp_init(struct daemon *daemon) |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 18 | { |
| 19 | int fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP); |
| 20 | struct sockaddr_in saddr; |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 21 | int flags, oneopt = 1, zeroopt = 0; |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 22 | struct dhcp_config *configs, *cp; |
Simon Kelley | dfa666f | 2004-08-02 18:27:27 +0100 | [diff] [blame] | 23 | |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 24 | if (fd == -1) |
Simon Kelley | b8187c8 | 2005-11-26 21:46:27 +0000 | [diff] [blame] | 25 | die (_("cannot create DHCP socket : %s"), NULL); |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 26 | |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 27 | if ((flags = fcntl(fd, F_GETFL, 0)) == -1 || |
| 28 | fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1 || |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 29 | #if defined(IP_PKTINFO) |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 30 | setsockopt(fd, SOL_IP, IP_PKTINFO, &oneopt, sizeof(oneopt)) == -1 || |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 31 | #elif defined(IP_RECVIF) |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 32 | setsockopt(fd, IPPROTO_IP, IP_RECVIF, &oneopt, sizeof(oneopt)) == -1 || |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 33 | #endif |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 34 | setsockopt(fd, SOL_SOCKET, SO_BROADCAST, &oneopt, sizeof(oneopt)) == -1) |
Simon Kelley | b8187c8 | 2005-11-26 21:46:27 +0000 | [diff] [blame] | 35 | die(_("failed to set options on DHCP socket: %s"), NULL); |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 36 | |
Simon Kelley | f6b7dc4 | 2005-01-23 12:06:08 +0000 | [diff] [blame] | 37 | /* When bind-interfaces is set, there might be more than one dnmsasq |
| 38 | instance binding port 67. That's Ok if they serve different networks. |
| 39 | Need to set REUSEADDR to make this posible. */ |
| 40 | if ((daemon->options & OPT_NOWILD) && |
| 41 | setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &oneopt, sizeof(oneopt)) == -1) |
Simon Kelley | b8187c8 | 2005-11-26 21:46:27 +0000 | [diff] [blame] | 42 | die(_("failed to set SO_REUSEADDR on DHCP socket: %s"), NULL); |
Simon Kelley | f6b7dc4 | 2005-01-23 12:06:08 +0000 | [diff] [blame] | 43 | |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 44 | saddr.sin_family = AF_INET; |
| 45 | saddr.sin_port = htons(DHCP_SERVER_PORT); |
| 46 | saddr.sin_addr.s_addr = INADDR_ANY; |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 47 | #ifdef HAVE_SOCKADDR_SA_LEN |
| 48 | saddr.sin_len = sizeof(struct sockaddr_in); |
| 49 | #endif |
| 50 | |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 51 | if (bind(fd, (struct sockaddr *)&saddr, sizeof(struct sockaddr_in))) |
Simon Kelley | b8187c8 | 2005-11-26 21:46:27 +0000 | [diff] [blame] | 52 | die(_("failed to bind DHCP server socket: %s"), NULL); |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 53 | |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 54 | daemon->dhcpfd = fd; |
| 55 | |
| 56 | if ((fd = socket (AF_INET, SOCK_RAW, IPPROTO_ICMP)) == -1 || |
Simon Kelley | 26128d2 | 2004-11-14 16:43:54 +0000 | [diff] [blame] | 57 | (flags = fcntl(fd, F_GETFL, 0)) == -1 || |
| 58 | fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1 || |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 59 | setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &oneopt, sizeof(oneopt)) == -1 || |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 60 | setsockopt(fd, SOL_SOCKET, SO_DONTROUTE, &zeroopt, sizeof(zeroopt)) == -1) |
Simon Kelley | b8187c8 | 2005-11-26 21:46:27 +0000 | [diff] [blame] | 61 | die(_("cannot create ICMP raw socket: %s."), NULL); |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 62 | |
| 63 | daemon->dhcp_icmp_fd = fd; |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 64 | |
| 65 | #ifdef HAVE_BPF |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 66 | { |
| 67 | int i = 0; |
| 68 | while (1) |
| 69 | { |
| 70 | char filename[50]; |
| 71 | sprintf(filename, "/dev/bpf%d", i++); |
| 72 | if ((fd = open(filename, O_RDWR, 0)) != -1) |
| 73 | break; |
| 74 | if (errno != EBUSY) |
Simon Kelley | b8187c8 | 2005-11-26 21:46:27 +0000 | [diff] [blame] | 75 | die(_("cannot create DHCP BPF socket: %s"), NULL); |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 76 | } |
| 77 | } |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 78 | #else |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 79 | /* since we don't ever use the packet socket for reception, |
| 80 | and it receives copies of _all_ IP packets, then that data |
| 81 | will build up in kernel buffers, wasting memory. Set the |
| 82 | socket receive buffer size to one to avoid that. (zero is |
| 83 | rejected as non-sensical by some BSD kernels) */ |
| 84 | if ((fd = socket(PF_PACKET, SOCK_DGRAM, htons(ETHERTYPE_IP))) == -1 || |
| 85 | setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &oneopt, sizeof(oneopt)) == -1) |
Simon Kelley | b8187c8 | 2005-11-26 21:46:27 +0000 | [diff] [blame] | 86 | die(_("cannot create DHCP packet socket: %s. " |
| 87 | "Is CONFIG_PACKET enabled in your kernel?"), NULL); |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 88 | #endif |
| 89 | |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 90 | daemon->dhcp_raw_fd = fd; |
| 91 | |
Simon Kelley | dfa666f | 2004-08-02 18:27:27 +0100 | [diff] [blame] | 92 | /* If the same IP appears in more than one host config, then DISCOVER |
| 93 | for one of the hosts will get the address, but REQUEST will be NAKed, |
| 94 | since the address is reserved by the other one -> protocol loop. */ |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 95 | for (configs = daemon->dhcp_conf; configs; configs = configs->next) |
Simon Kelley | dfa666f | 2004-08-02 18:27:27 +0100 | [diff] [blame] | 96 | for (cp = configs->next; cp; cp = cp->next) |
| 97 | if ((configs->flags & cp->flags & CONFIG_ADDR) && configs->addr.s_addr == cp->addr.s_addr) |
Simon Kelley | b8187c8 | 2005-11-26 21:46:27 +0000 | [diff] [blame] | 98 | die(_("duplicate IP address %s in dhcp-config directive."), inet_ntoa(cp->addr)); |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 99 | |
| 100 | daemon->dhcp_packet = safe_malloc(sizeof(struct udp_dhcp_packet)); |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 101 | /* These two each hold a DHCP option max size 255 |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 102 | and get a terminating zero added */ |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 103 | daemon->dhcp_buff = safe_malloc(256); |
| 104 | daemon->dhcp_buff2 = safe_malloc(256); |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 105 | daemon->ping_results = NULL; |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 106 | } |
| 107 | |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 108 | void dhcp_packet(struct daemon *daemon, time_t now) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 109 | { |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 110 | struct udp_dhcp_packet *rawpacket = daemon->dhcp_packet; |
| 111 | struct dhcp_packet *mess = &rawpacket->data; |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 112 | struct dhcp_context *context; |
| 113 | struct iname *tmp; |
| 114 | struct ifreq ifr; |
| 115 | struct msghdr msg; |
| 116 | struct iovec iov[2]; |
| 117 | struct cmsghdr *cmptr; |
| 118 | int sz, newlen, iface_index = 0; |
Simon Kelley | b8187c8 | 2005-11-26 21:46:27 +0000 | [diff] [blame] | 119 | int unicast_dest = 0; |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 120 | struct in_addr iface_addr; |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 121 | #ifdef HAVE_BPF |
| 122 | unsigned char iface_hwaddr[ETHER_ADDR_LEN]; |
| 123 | #endif |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 124 | |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 125 | union { |
| 126 | struct cmsghdr align; /* this ensures alignment */ |
| 127 | #ifdef IP_PKTINFO |
| 128 | char control[CMSG_SPACE(sizeof(struct in_pktinfo))]; |
| 129 | #else |
| 130 | char control[CMSG_SPACE(sizeof(struct sockaddr_dl))]; |
| 131 | #endif |
| 132 | } control_u; |
| 133 | |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 134 | iov[0].iov_base = (char *)mess; |
| 135 | iov[0].iov_len = sizeof(struct dhcp_packet); |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 136 | |
| 137 | msg.msg_control = control_u.control; |
| 138 | msg.msg_controllen = sizeof(control_u); |
| 139 | msg.msg_flags = 0; |
| 140 | msg.msg_name = NULL; |
| 141 | msg.msg_namelen = 0; |
| 142 | msg.msg_iov = iov; |
| 143 | msg.msg_iovlen = 1; |
| 144 | |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 145 | sz = recvmsg(daemon->dhcpfd, &msg, 0); |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 146 | |
| 147 | if (sz < (int)(sizeof(*mess) - sizeof(mess->options))) |
| 148 | return; |
| 149 | |
| 150 | #if defined (IP_PKTINFO) |
| 151 | if (msg.msg_controllen < sizeof(struct cmsghdr)) |
| 152 | return; |
| 153 | for (cmptr = CMSG_FIRSTHDR(&msg); cmptr; cmptr = CMSG_NXTHDR(&msg, cmptr)) |
| 154 | if (cmptr->cmsg_level == SOL_IP && cmptr->cmsg_type == IP_PKTINFO) |
Simon Kelley | b8187c8 | 2005-11-26 21:46:27 +0000 | [diff] [blame] | 155 | { |
| 156 | iface_index = ((struct in_pktinfo *)CMSG_DATA(cmptr))->ipi_ifindex; |
| 157 | if (((struct in_pktinfo *)CMSG_DATA(cmptr))->ipi_addr.s_addr != INADDR_BROADCAST) |
| 158 | unicast_dest = 1; |
| 159 | } |
| 160 | |
Simon Kelley | 8a911cc | 2004-03-16 18:35:52 +0000 | [diff] [blame] | 161 | if (!(ifr.ifr_ifindex = iface_index) || |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 162 | ioctl(daemon->dhcpfd, SIOCGIFNAME, &ifr) == -1) |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 163 | return; |
Simon Kelley | b8187c8 | 2005-11-26 21:46:27 +0000 | [diff] [blame] | 164 | |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 165 | #elif defined(IP_RECVIF) |
| 166 | if (msg.msg_controllen < sizeof(struct cmsghdr)) |
| 167 | return; |
| 168 | for (cmptr = CMSG_FIRSTHDR(&msg); cmptr; cmptr = CMSG_NXTHDR(&msg, cmptr)) |
| 169 | if (cmptr->cmsg_level == IPPROTO_IP && cmptr->cmsg_type == IP_RECVIF) |
| 170 | iface_index = ((struct sockaddr_dl *)CMSG_DATA(cmptr))->sdl_index; |
Simon Kelley | b8187c8 | 2005-11-26 21:46:27 +0000 | [diff] [blame] | 171 | |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 172 | if (!iface_index || !if_indextoname(iface_index, ifr.ifr_name)) |
| 173 | return; |
| 174 | |
| 175 | #else |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 176 | { |
| 177 | struct iname *name; |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 178 | for (name = daemon->if_names; name->isloop; name = name->next); |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 179 | strcpy(ifr.ifr_name, name->name); |
| 180 | } |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 181 | #endif |
| 182 | |
| 183 | #ifdef HAVE_BPF |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 184 | ifr.ifr_addr.sa_family = AF_LINK; |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 185 | if (ioctl(daemon->dhcpfd, SIOCGIFADDR, &ifr) < 0) |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 186 | return; |
| 187 | memcpy(iface_hwaddr, LLADDR((struct sockaddr_dl *)&ifr.ifr_addr), ETHER_ADDR_LEN); |
| 188 | #endif |
| 189 | |
| 190 | ifr.ifr_addr.sa_family = AF_INET; |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 191 | if (ioctl(daemon->dhcpfd, SIOCGIFADDR, &ifr) < 0 ) |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 192 | return; |
| 193 | iface_addr = ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr; |
| 194 | |
| 195 | /* enforce available interface configuration */ |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 196 | for (tmp = daemon->if_except; tmp; tmp = tmp->next) |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 197 | if (tmp->name && (strcmp(tmp->name, ifr.ifr_name) == 0)) |
| 198 | return; |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 199 | |
| 200 | for (tmp = daemon->dhcp_except; tmp; tmp = tmp->next) |
| 201 | if (tmp->name && (strcmp(tmp->name, ifr.ifr_name) == 0)) |
| 202 | return; |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 203 | |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 204 | if (daemon->if_names || daemon->if_addrs) |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 205 | { |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 206 | for (tmp = daemon->if_names; tmp; tmp = tmp->next) |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 207 | if (tmp->name && (strcmp(tmp->name, ifr.ifr_name) == 0)) |
| 208 | break; |
| 209 | if (!tmp) |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 210 | for (tmp = daemon->if_addrs; tmp; tmp = tmp->next) |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 211 | if (tmp->addr.sa.sa_family == AF_INET && |
| 212 | tmp->addr.in.sin_addr.s_addr == iface_addr.s_addr) |
| 213 | break; |
| 214 | if (!tmp) |
| 215 | return; |
| 216 | } |
| 217 | |
Simon Kelley | 91dccd0 | 2005-03-31 17:48:32 +0100 | [diff] [blame] | 218 | /* unlinked contexts are marked by context->current == context */ |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 219 | for (context = daemon->dhcp; context; context = context->next) |
Simon Kelley | 91dccd0 | 2005-03-31 17:48:32 +0100 | [diff] [blame] | 220 | context->current = context; |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 221 | |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 222 | #ifdef HAVE_RTNETLINK |
| 223 | if (!netlink_process(daemon, iface_index, mess->giaddr, iface_addr, &context)) |
| 224 | #endif |
| 225 | { |
| 226 | struct in_addr iface_netmask, iface_broadcast; |
Simon Kelley | e17fb62 | 2006-01-14 20:33:46 +0000 | [diff] [blame^] | 227 | |
| 228 | #ifdef HAVE_RTNETLINK |
| 229 | static int warned = 0; |
| 230 | |
| 231 | if (!warned) |
| 232 | { |
| 233 | syslog(LOG_WARNING, _("Cannot use RTnetlink socket, falling back to ioctl API")); |
| 234 | warned = 1; |
| 235 | } |
| 236 | #endif |
| 237 | |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 238 | if (ioctl(daemon->dhcpfd, SIOCGIFNETMASK, &ifr) < 0) |
| 239 | return; |
| 240 | iface_netmask = ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr; |
| 241 | |
| 242 | if (ioctl(daemon->dhcpfd, SIOCGIFBRDADDR, &ifr) < 0) |
| 243 | return; |
| 244 | iface_broadcast = ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr; |
| 245 | |
| 246 | context = complete_context(daemon, iface_addr, NULL, iface_netmask, |
| 247 | iface_broadcast, mess->giaddr, iface_addr); |
| 248 | } |
| 249 | |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 250 | lease_prune(NULL, now); /* lose any expired leases */ |
Simon Kelley | b8187c8 | 2005-11-26 21:46:27 +0000 | [diff] [blame] | 251 | newlen = dhcp_reply(daemon, context, ifr.ifr_name, sz, now, unicast_dest); |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 252 | lease_update_file(0, now); |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 253 | lease_update_dns(daemon); |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 254 | |
| 255 | if (newlen == 0) |
| 256 | return; |
| 257 | |
| 258 | if (mess->giaddr.s_addr || mess->ciaddr.s_addr) |
| 259 | { |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 260 | /* To send to BOOTP relay or configured client, use the IP packet */ |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 261 | |
| 262 | struct sockaddr_in dest; |
| 263 | dest.sin_family = AF_INET; |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 264 | #ifdef HAVE_SOCKADDR_SA_LEN |
| 265 | dest.sin_len = sizeof(struct sockaddr_in); |
| 266 | #endif |
| 267 | |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 268 | if (mess->giaddr.s_addr) |
| 269 | { |
| 270 | dest.sin_port = htons(DHCP_SERVER_PORT); |
| 271 | dest.sin_addr = mess->giaddr; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 272 | } |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 273 | else |
| 274 | { |
| 275 | dest.sin_port = htons(DHCP_CLIENT_PORT); |
| 276 | dest.sin_addr = mess->ciaddr; |
| 277 | } |
| 278 | |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 279 | while(sendto(daemon->dhcpfd, mess, newlen, 0, |
| 280 | (struct sockaddr *)&dest, sizeof(dest)) == -1 && |
| 281 | retry_send()); |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 282 | } |
| 283 | else |
| 284 | { |
| 285 | /* Hairy stuff, packet either has to go to the |
| 286 | net broadcast or the destination can't reply to ARP yet, |
| 287 | but we do know the physical address. |
| 288 | Build the packet by steam, and send directly, bypassing |
| 289 | the kernel IP stack */ |
| 290 | |
| 291 | u32 i, sum; |
| 292 | unsigned char hwdest[ETHER_ADDR_LEN]; |
| 293 | |
| 294 | if (ntohs(mess->flags) & 0x8000) |
| 295 | { |
| 296 | memset(hwdest, 255, ETHER_ADDR_LEN); |
| 297 | rawpacket->ip.ip_dst.s_addr = INADDR_BROADCAST; |
| 298 | } |
| 299 | else |
| 300 | { |
| 301 | memcpy(hwdest, mess->chaddr, ETHER_ADDR_LEN); |
| 302 | rawpacket->ip.ip_dst.s_addr = mess->yiaddr.s_addr; |
| 303 | } |
| 304 | |
| 305 | rawpacket->ip.ip_p = IPPROTO_UDP; |
| 306 | rawpacket->ip.ip_src.s_addr = iface_addr.s_addr; |
| 307 | rawpacket->ip.ip_len = htons(sizeof(struct ip) + |
| 308 | sizeof(struct udphdr) + |
| 309 | newlen) ; |
| 310 | rawpacket->ip.ip_hl = sizeof(struct ip) / 4; |
| 311 | rawpacket->ip.ip_v = IPVERSION; |
| 312 | rawpacket->ip.ip_tos = 0; |
| 313 | rawpacket->ip.ip_id = htons(0); |
| 314 | rawpacket->ip.ip_off = htons(0x4000); /* don't fragment */ |
| 315 | rawpacket->ip.ip_ttl = IPDEFTTL; |
| 316 | rawpacket->ip.ip_sum = 0; |
| 317 | for (sum = 0, i = 0; i < sizeof(struct ip) / 2; i++) |
| 318 | sum += ((u16 *)&rawpacket->ip)[i]; |
| 319 | while (sum>>16) |
| 320 | sum = (sum & 0xffff) + (sum >> 16); |
| 321 | rawpacket->ip.ip_sum = (sum == 0xffff) ? sum : ~sum; |
| 322 | |
| 323 | rawpacket->udp.uh_sport = htons(DHCP_SERVER_PORT); |
| 324 | rawpacket->udp.uh_dport = htons(DHCP_CLIENT_PORT); |
| 325 | ((u8 *)&rawpacket->data)[newlen] = 0; /* for checksum, in case length is odd. */ |
| 326 | rawpacket->udp.uh_sum = 0; |
| 327 | rawpacket->udp.uh_ulen = sum = htons(sizeof(struct udphdr) + newlen); |
| 328 | sum += htons(IPPROTO_UDP); |
| 329 | for (i = 0; i < 4; i++) |
| 330 | sum += ((u16 *)&rawpacket->ip.ip_src)[i]; |
| 331 | for (i = 0; i < (sizeof(struct udphdr) + newlen + 1) / 2; i++) |
| 332 | sum += ((u16 *)&rawpacket->udp)[i]; |
| 333 | while (sum>>16) |
| 334 | sum = (sum & 0xffff) + (sum >> 16); |
| 335 | rawpacket->udp.uh_sum = (sum == 0xffff) ? sum : ~sum; |
| 336 | |
| 337 | { |
| 338 | #ifdef HAVE_BPF |
| 339 | struct ether_header header; |
| 340 | |
| 341 | header.ether_type = htons(ETHERTYPE_IP); |
| 342 | memcpy(header.ether_shost, iface_hwaddr, ETHER_ADDR_LEN); |
| 343 | memcpy(header.ether_dhost, hwdest, ETHER_ADDR_LEN); |
| 344 | |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 345 | ioctl(daemon->dhcp_raw_fd, BIOCSETIF, &ifr); |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 346 | |
| 347 | iov[0].iov_base = (char *)&header; |
| 348 | iov[0].iov_len = sizeof(struct ether_header); |
| 349 | iov[1].iov_base = (char *)rawpacket; |
| 350 | iov[1].iov_len = ntohs(rawpacket->ip.ip_len); |
Simon Kelley | 26128d2 | 2004-11-14 16:43:54 +0000 | [diff] [blame] | 351 | while (writev(daemon->dhcp_raw_fd, iov, 2) == -1 && retry_send()); |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 352 | #else |
| 353 | struct sockaddr_ll dest; |
| 354 | |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 355 | memset(&dest, 0, sizeof(dest)); |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 356 | dest.sll_family = AF_PACKET; |
| 357 | dest.sll_halen = ETHER_ADDR_LEN; |
| 358 | dest.sll_ifindex = iface_index; |
| 359 | dest.sll_protocol = htons(ETHERTYPE_IP); |
| 360 | memcpy(dest.sll_addr, hwdest, ETHER_ADDR_LEN); |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 361 | while (sendto(daemon->dhcp_raw_fd, rawpacket, ntohs(rawpacket->ip.ip_len), |
| 362 | 0, (struct sockaddr *)&dest, sizeof(dest)) == -1 && |
Simon Kelley | 26128d2 | 2004-11-14 16:43:54 +0000 | [diff] [blame] | 363 | retry_send()); |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 364 | #endif |
| 365 | } |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 366 | } |
| 367 | } |
| 368 | |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 369 | /* This is a complex routine: it gets called with each (address,netmask,broadcast) triple |
| 370 | of the interface on which a DHCP packet arrives (and any relay address) and does the |
| 371 | following things: |
| 372 | 1) Fills in any netmask and broadcast addresses which have not been explicitly configured. |
| 373 | 2) Fills in local (this host) and router (this host or relay) addresses. |
| 374 | 3) Links contexts which are valid for hosts directly connected to the arrival interface on ->current. |
| 375 | Note that the current chain may be superceded later for configured hosts or those coming via gateways. */ |
| 376 | struct dhcp_context *complete_context(struct daemon *daemon, struct in_addr local, struct dhcp_context *current, |
| 377 | struct in_addr netmask, struct in_addr broadcast, struct in_addr relay, |
| 378 | struct in_addr primary) |
| 379 | { |
| 380 | struct dhcp_context *context; |
| 381 | |
| 382 | for (context = daemon->dhcp; context; context = context->next) |
| 383 | { |
| 384 | if (!(context->flags & CONTEXT_NETMASK) && |
| 385 | (is_same_net(local, context->start, netmask) || |
| 386 | is_same_net(local, context->end, netmask))) |
| 387 | { |
| 388 | if (context->netmask.s_addr != netmask.s_addr && |
| 389 | !(is_same_net(local, context->start, netmask) && |
| 390 | is_same_net(local, context->end, netmask))) |
| 391 | { |
| 392 | strcpy(daemon->dhcp_buff, inet_ntoa(context->start)); |
| 393 | strcpy(daemon->dhcp_buff2, inet_ntoa(context->end)); |
Simon Kelley | b8187c8 | 2005-11-26 21:46:27 +0000 | [diff] [blame] | 394 | syslog(LOG_WARNING, _("DHCP range %s -- %s is not consistent with netmask %s"), |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 395 | daemon->dhcp_buff, daemon->dhcp_buff2, inet_ntoa(netmask)); |
| 396 | } |
| 397 | context->netmask = netmask; |
| 398 | } |
| 399 | |
| 400 | if (context->netmask.s_addr) |
| 401 | { |
| 402 | if (is_same_net(local, context->start, context->netmask) && |
| 403 | is_same_net(local, context->end, context->netmask)) |
| 404 | { |
Simon Kelley | 91dccd0 | 2005-03-31 17:48:32 +0100 | [diff] [blame] | 405 | /* link it onto the current chain if we've not seen it before */ |
| 406 | if (context->current == context) |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 407 | { |
| 408 | context->router = local; |
| 409 | context->local = local; |
| 410 | context->current = current; |
| 411 | current = context; |
| 412 | } |
| 413 | |
| 414 | if (!(context->flags & CONTEXT_BRDCAST)) |
| 415 | { |
| 416 | if (is_same_net(broadcast, context->start, context->netmask)) |
| 417 | context->broadcast = broadcast; |
| 418 | else |
| 419 | context->broadcast.s_addr = context->start.s_addr | ~context->netmask.s_addr; |
| 420 | } |
| 421 | } |
| 422 | else if (relay.s_addr && is_same_net(relay, context->start, context->netmask)) |
| 423 | { |
| 424 | context->router = relay; |
| 425 | context->local = primary; |
| 426 | /* fill in missing broadcast addresses for relayed ranges */ |
| 427 | if (!(context->flags & CONTEXT_BRDCAST)) |
| 428 | context->broadcast.s_addr = context->start.s_addr | ~context->netmask.s_addr; |
| 429 | } |
| 430 | |
| 431 | } |
| 432 | } |
| 433 | |
| 434 | return current; |
| 435 | } |
| 436 | |
Simon Kelley | 59353a6 | 2004-11-21 19:34:28 +0000 | [diff] [blame] | 437 | struct dhcp_context *address_available(struct dhcp_context *context, struct in_addr taddr) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 438 | { |
Simon Kelley | 36717ee | 2004-09-20 19:20:58 +0100 | [diff] [blame] | 439 | /* Check is an address is OK for this network, check all |
| 440 | possible ranges. */ |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 441 | |
Simon Kelley | 36717ee | 2004-09-20 19:20:58 +0100 | [diff] [blame] | 442 | unsigned int start, end, addr = ntohl(taddr.s_addr); |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 443 | |
Simon Kelley | 36717ee | 2004-09-20 19:20:58 +0100 | [diff] [blame] | 444 | for (; context; context = context->current) |
| 445 | { |
| 446 | start = ntohl(context->start.s_addr); |
| 447 | end = ntohl(context->end.s_addr); |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 448 | |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 449 | if (!(context->flags & CONTEXT_STATIC) && |
Simon Kelley | 36717ee | 2004-09-20 19:20:58 +0100 | [diff] [blame] | 450 | addr >= start && |
| 451 | addr <= end) |
Simon Kelley | 59353a6 | 2004-11-21 19:34:28 +0000 | [diff] [blame] | 452 | return context; |
Simon Kelley | 36717ee | 2004-09-20 19:20:58 +0100 | [diff] [blame] | 453 | } |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 454 | |
Simon Kelley | 59353a6 | 2004-11-21 19:34:28 +0000 | [diff] [blame] | 455 | return NULL; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 456 | } |
Simon Kelley | 59353a6 | 2004-11-21 19:34:28 +0000 | [diff] [blame] | 457 | |
| 458 | struct dhcp_context *narrow_context(struct dhcp_context *context, struct in_addr taddr) |
| 459 | { |
Simon Kelley | e17fb62 | 2006-01-14 20:33:46 +0000 | [diff] [blame^] | 460 | /* We start of with a set of possible contexts, all on the current physical interface. |
Simon Kelley | 59353a6 | 2004-11-21 19:34:28 +0000 | [diff] [blame] | 461 | These are chained on ->current. |
| 462 | Here we have an address, and return the actual context correponding to that |
| 463 | address. Note that none may fit, if the address came a dhcp-host and is outside |
Simon Kelley | e17fb62 | 2006-01-14 20:33:46 +0000 | [diff] [blame^] | 464 | any dhcp-range. In that case we return a static range if possible, or failing that, |
| 465 | any context on the correct subnet. (If there's more than one, this is a dodgy |
| 466 | configuration: maybe there should be a warning.) */ |
Simon Kelley | 59353a6 | 2004-11-21 19:34:28 +0000 | [diff] [blame] | 467 | |
Simon Kelley | e17fb62 | 2006-01-14 20:33:46 +0000 | [diff] [blame^] | 468 | struct dhcp_context *tmp; |
Simon Kelley | 59353a6 | 2004-11-21 19:34:28 +0000 | [diff] [blame] | 469 | |
Simon Kelley | e17fb62 | 2006-01-14 20:33:46 +0000 | [diff] [blame^] | 470 | if ((tmp = address_available(context, taddr))) |
Simon Kelley | 59353a6 | 2004-11-21 19:34:28 +0000 | [diff] [blame] | 471 | return tmp; |
| 472 | |
| 473 | for (tmp = context; tmp; tmp = tmp->current) |
Simon Kelley | e17fb62 | 2006-01-14 20:33:46 +0000 | [diff] [blame^] | 474 | if (is_same_net(taddr, tmp->start, tmp->netmask) && |
| 475 | (tmp->flags & CONTEXT_STATIC)) |
Simon Kelley | 59353a6 | 2004-11-21 19:34:28 +0000 | [diff] [blame] | 476 | return tmp; |
| 477 | |
Simon Kelley | e17fb62 | 2006-01-14 20:33:46 +0000 | [diff] [blame^] | 478 | for (tmp = context; tmp; tmp = tmp->current) |
| 479 | if (is_same_net(taddr, tmp->start, tmp->netmask)) |
| 480 | return tmp; |
| 481 | |
| 482 | return NULL; |
Simon Kelley | 59353a6 | 2004-11-21 19:34:28 +0000 | [diff] [blame] | 483 | } |
| 484 | |
Simon Kelley | dfa666f | 2004-08-02 18:27:27 +0100 | [diff] [blame] | 485 | struct dhcp_config *config_find_by_address(struct dhcp_config *configs, struct in_addr addr) |
| 486 | { |
| 487 | struct dhcp_config *config; |
| 488 | |
| 489 | for (config = configs; config; config = config->next) |
| 490 | if ((config->flags & CONFIG_ADDR) && config->addr.s_addr == addr.s_addr) |
| 491 | return config; |
| 492 | |
| 493 | return NULL; |
| 494 | } |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 495 | |
Simon Kelley | f6b7dc4 | 2005-01-23 12:06:08 +0000 | [diff] [blame] | 496 | /* Is every member of check matched by a member of pool? */ |
| 497 | int match_netid(struct dhcp_netid *check, struct dhcp_netid *pool) |
| 498 | { |
| 499 | struct dhcp_netid *tmp1; |
| 500 | |
| 501 | if (!check) |
| 502 | return 0; |
| 503 | |
| 504 | for (; check; check = check->next) |
| 505 | { |
| 506 | if (check->net[0] != '#') |
| 507 | { |
| 508 | for (tmp1 = pool; tmp1; tmp1 = tmp1->next) |
| 509 | if (strcmp(check->net, tmp1->net) == 0) |
| 510 | break; |
| 511 | if (!tmp1) |
| 512 | return 0; |
| 513 | } |
| 514 | else |
| 515 | for (tmp1 = pool; tmp1; tmp1 = tmp1->next) |
| 516 | if (strcmp((check->net)+1, tmp1->net) == 0) |
| 517 | return 0; |
| 518 | } |
| 519 | return 1; |
| 520 | } |
| 521 | |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 522 | int address_allocate(struct dhcp_context *context, struct daemon *daemon, |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 523 | struct in_addr *addrp, unsigned char *hwaddr, |
| 524 | struct dhcp_netid *netids, time_t now) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 525 | { |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 526 | /* Find a free address: exclude anything in use and anything allocated to |
Simon Kelley | f6b7dc4 | 2005-01-23 12:06:08 +0000 | [diff] [blame] | 527 | a particular hwaddr/clientid/hostname in our configuration. |
| 528 | Try to return from contexts which mathc netis first. */ |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 529 | |
Simon Kelley | a84fa1d | 2004-04-23 22:21:21 +0100 | [diff] [blame] | 530 | struct in_addr start, addr ; |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 531 | struct dhcp_context *c; |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 532 | unsigned int i, j; |
Simon Kelley | a84fa1d | 2004-04-23 22:21:21 +0100 | [diff] [blame] | 533 | |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 534 | for (c = context; c; c = c->current) |
| 535 | if (c->flags & CONTEXT_STATIC) |
Simon Kelley | f6b7dc4 | 2005-01-23 12:06:08 +0000 | [diff] [blame] | 536 | continue; |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 537 | else if (netids && !(c->flags & CONTEXT_FILTER)) |
Simon Kelley | f6b7dc4 | 2005-01-23 12:06:08 +0000 | [diff] [blame] | 538 | continue; |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 539 | else if (!netids && (c->flags & CONTEXT_FILTER)) |
Simon Kelley | f6b7dc4 | 2005-01-23 12:06:08 +0000 | [diff] [blame] | 540 | continue; |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 541 | else if (netids && (c->flags & CONTEXT_FILTER) && !match_netid(&c->netid, netids)) |
Simon Kelley | f6b7dc4 | 2005-01-23 12:06:08 +0000 | [diff] [blame] | 542 | continue; |
| 543 | else |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 544 | { |
Simon Kelley | 36717ee | 2004-09-20 19:20:58 +0100 | [diff] [blame] | 545 | /* pick a seed based on hwaddr then iterate until we find a free address. */ |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 546 | for (j = c->addr_epoch, i = 0; i < ETHER_ADDR_LEN; i++) |
Simon Kelley | 36717ee | 2004-09-20 19:20:58 +0100 | [diff] [blame] | 547 | j += hwaddr[i] + (hwaddr[i] << 8) + (hwaddr[i] << 16); |
| 548 | |
| 549 | start.s_addr = addr.s_addr = |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 550 | htonl(ntohl(c->start.s_addr) + |
| 551 | (j % (1 + ntohl(c->end.s_addr) - ntohl(c->start.s_addr)))); |
Simon Kelley | 36717ee | 2004-09-20 19:20:58 +0100 | [diff] [blame] | 552 | |
| 553 | do { |
| 554 | if (!lease_find_by_addr(addr) && |
| 555 | !config_find_by_address(daemon->dhcp_conf, addr)) |
| 556 | { |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 557 | struct ping_result *r, *victim = NULL; |
| 558 | int count; |
| 559 | |
| 560 | /* check if we failed to ping addr sometime in the last |
| 561 | 30s. If so, assume the same situation still exists. |
| 562 | This avoids problems when a stupid client bangs |
| 563 | on us repeatedly. As a final check, is we did more |
| 564 | than six ping checks in the last 30s, we are in |
| 565 | high-load mode, so don't do any more. */ |
| 566 | for (count = 0, r = daemon->ping_results; r; r = r->next) |
| 567 | if (difftime(now, r->time) > 30.0) |
| 568 | victim = r; /* old record */ |
| 569 | else if (++count == 6 || r->addr.s_addr == addr.s_addr) |
| 570 | { |
| 571 | *addrp = addr; |
| 572 | return 1; |
| 573 | } |
| 574 | |
Simon Kelley | 36717ee | 2004-09-20 19:20:58 +0100 | [diff] [blame] | 575 | if (icmp_ping(daemon, addr)) |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 576 | /* address in use: perturb address selection so that we are |
Simon Kelley | 36717ee | 2004-09-20 19:20:58 +0100 | [diff] [blame] | 577 | less likely to try this address again. */ |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 578 | c->addr_epoch++; |
Simon Kelley | 36717ee | 2004-09-20 19:20:58 +0100 | [diff] [blame] | 579 | else |
| 580 | { |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 581 | /* at this point victim may hold an expired record */ |
| 582 | if (!victim) |
| 583 | { |
| 584 | if ((victim = malloc(sizeof(struct ping_result)))) |
| 585 | { |
| 586 | victim->next = daemon->ping_results; |
| 587 | daemon->ping_results = victim; |
| 588 | } |
| 589 | } |
| 590 | |
| 591 | /* record that this address is OK for 30s |
| 592 | without more ping checks */ |
| 593 | if (victim) |
| 594 | { |
| 595 | victim->addr = addr; |
| 596 | victim->time = now; |
| 597 | } |
Simon Kelley | 36717ee | 2004-09-20 19:20:58 +0100 | [diff] [blame] | 598 | *addrp = addr; |
| 599 | return 1; |
| 600 | } |
| 601 | } |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 602 | |
Simon Kelley | 36717ee | 2004-09-20 19:20:58 +0100 | [diff] [blame] | 603 | addr.s_addr = htonl(ntohl(addr.s_addr) + 1); |
| 604 | |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 605 | if (addr.s_addr == htonl(ntohl(c->end.s_addr) + 1)) |
| 606 | addr = c->start; |
Simon Kelley | 36717ee | 2004-09-20 19:20:58 +0100 | [diff] [blame] | 607 | |
| 608 | } while (addr.s_addr != start.s_addr); |
| 609 | } |
Simon Kelley | f6b7dc4 | 2005-01-23 12:06:08 +0000 | [diff] [blame] | 610 | |
| 611 | if (netids) |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 612 | return address_allocate(context, daemon, addrp, hwaddr, NULL, now); |
Simon Kelley | f6b7dc4 | 2005-01-23 12:06:08 +0000 | [diff] [blame] | 613 | |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 614 | return 0; |
| 615 | } |
| 616 | |
| 617 | static int is_addr_in_context(struct dhcp_context *context, struct dhcp_config *config) |
| 618 | { |
Simon Kelley | b8187c8 | 2005-11-26 21:46:27 +0000 | [diff] [blame] | 619 | if (!context) /* called via find_config() from lease_update_from_configs() */ |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 620 | return 1; |
Simon Kelley | 33820b7 | 2004-04-03 21:10:00 +0100 | [diff] [blame] | 621 | if (!(config->flags & CONFIG_ADDR)) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 622 | return 1; |
Simon Kelley | b8187c8 | 2005-11-26 21:46:27 +0000 | [diff] [blame] | 623 | for (; context; context = context->current) |
| 624 | if (is_same_net(config->addr, context->start, context->netmask)) |
| 625 | return 1; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 626 | |
| 627 | return 0; |
| 628 | } |
| 629 | |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 630 | |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 631 | struct dhcp_config *find_config(struct dhcp_config *configs, |
| 632 | struct dhcp_context *context, |
| 633 | unsigned char *clid, int clid_len, |
| 634 | unsigned char *hwaddr, char *hostname) |
| 635 | { |
| 636 | struct dhcp_config *config; |
| 637 | |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 638 | if (clid) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 639 | for (config = configs; config; config = config->next) |
Simon Kelley | 33820b7 | 2004-04-03 21:10:00 +0100 | [diff] [blame] | 640 | if (config->flags & CONFIG_CLID) |
| 641 | { |
| 642 | if (config->clid_len == clid_len && |
| 643 | memcmp(config->clid, clid, clid_len) == 0 && |
| 644 | is_addr_in_context(context, config)) |
| 645 | return config; |
| 646 | |
| 647 | /* dhcpcd prefixes ASCII client IDs by zero which is wrong, but we try and |
| 648 | cope with that here */ |
| 649 | if (*clid == 0 && config->clid_len == clid_len-1 && |
| 650 | memcmp(config->clid, clid+1, clid_len-1) == 0 && |
| 651 | is_addr_in_context(context, config)) |
| 652 | return config; |
| 653 | } |
| 654 | |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 655 | |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 656 | if (hwaddr) |
| 657 | for (config = configs; config; config = config->next) |
| 658 | if ((config->flags & CONFIG_HWADDR) && |
| 659 | config->wildcard_mask == 0 && |
| 660 | memcmp(config->hwaddr, hwaddr, ETHER_ADDR_LEN) == 0 && |
| 661 | is_addr_in_context(context, config)) |
| 662 | return config; |
| 663 | |
| 664 | |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 665 | if (hostname && context) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 666 | for (config = configs; config; config = config->next) |
Simon Kelley | 33820b7 | 2004-04-03 21:10:00 +0100 | [diff] [blame] | 667 | if ((config->flags & CONFIG_NAME) && |
| 668 | hostname_isequal(config->hostname, hostname) && |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 669 | is_addr_in_context(context, config)) |
| 670 | return config; |
| 671 | |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 672 | if (hwaddr) |
| 673 | for (config = configs; config; config = config->next) |
| 674 | if ((config->flags & CONFIG_HWADDR) && |
| 675 | config->wildcard_mask != 0 && |
| 676 | is_addr_in_context(context, config)) |
| 677 | { |
| 678 | int i; |
| 679 | unsigned int mask = config->wildcard_mask; |
| 680 | for (i = ETHER_ADDR_LEN - 1; i >= 0; i--, mask = mask >> 1) |
| 681 | if (mask & 1) |
| 682 | config->hwaddr[i] = hwaddr[i]; |
| 683 | if (memcmp(config->hwaddr, hwaddr, ETHER_ADDR_LEN) == 0) |
| 684 | return config; |
| 685 | } |
| 686 | |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 687 | return NULL; |
| 688 | } |
| 689 | |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 690 | void dhcp_read_ethers(struct daemon *daemon) |
Simon Kelley | 1ab84e2 | 2004-01-29 16:48:35 +0000 | [diff] [blame] | 691 | { |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 692 | FILE *f = fopen(ETHERSFILE, "r"); |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 693 | unsigned int flags; |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 694 | char *buff = daemon->namebuff; |
Simon Kelley | 33820b7 | 2004-04-03 21:10:00 +0100 | [diff] [blame] | 695 | char *ip, *cp; |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 696 | struct in_addr addr; |
Simon Kelley | 33820b7 | 2004-04-03 21:10:00 +0100 | [diff] [blame] | 697 | unsigned char hwaddr[ETHER_ADDR_LEN]; |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 698 | struct dhcp_config *config, *configs = daemon->dhcp_conf; |
Simon Kelley | b8187c8 | 2005-11-26 21:46:27 +0000 | [diff] [blame] | 699 | int count = 0, lineno = 0; |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 700 | |
| 701 | addr.s_addr = 0; /* eliminate warning */ |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 702 | |
| 703 | if (!f) |
Simon Kelley | 33820b7 | 2004-04-03 21:10:00 +0100 | [diff] [blame] | 704 | { |
Simon Kelley | b8187c8 | 2005-11-26 21:46:27 +0000 | [diff] [blame] | 705 | syslog(LOG_ERR, _("failed to read %s:%m"), ETHERSFILE); |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 706 | return; |
Simon Kelley | 33820b7 | 2004-04-03 21:10:00 +0100 | [diff] [blame] | 707 | } |
| 708 | |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 709 | while (fgets(buff, MAXDNAME, f)) |
| 710 | { |
Simon Kelley | b8187c8 | 2005-11-26 21:46:27 +0000 | [diff] [blame] | 711 | lineno++; |
| 712 | |
Simon Kelley | 33820b7 | 2004-04-03 21:10:00 +0100 | [diff] [blame] | 713 | while (strlen(buff) > 0 && isspace(buff[strlen(buff)-1])) |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 714 | buff[strlen(buff)-1] = 0; |
| 715 | |
| 716 | if ((*buff == '#') || (*buff == '+')) |
| 717 | continue; |
| 718 | |
Simon Kelley | 33820b7 | 2004-04-03 21:10:00 +0100 | [diff] [blame] | 719 | for (ip = buff; *ip && !isspace(*ip); ip++); |
| 720 | for(; *ip && isspace(*ip); ip++) |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 721 | *ip = 0; |
Simon Kelley | b8187c8 | 2005-11-26 21:46:27 +0000 | [diff] [blame] | 722 | if (!*ip || parse_hex(buff, hwaddr, 6, NULL) != 6) |
| 723 | { |
| 724 | syslog(LOG_ERR, _("bad line at %s line %d"), ETHERSFILE, lineno); |
| 725 | continue; |
| 726 | } |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 727 | |
| 728 | /* check for name or dotted-quad */ |
| 729 | for (cp = ip; *cp; cp++) |
| 730 | if (!(*cp == '.' || (*cp >='0' && *cp <= '9'))) |
| 731 | break; |
| 732 | |
| 733 | if (!*cp) |
| 734 | { |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 735 | if ((addr.s_addr = inet_addr(ip)) == (in_addr_t)-1) |
Simon Kelley | b8187c8 | 2005-11-26 21:46:27 +0000 | [diff] [blame] | 736 | { |
| 737 | syslog(LOG_ERR, _("bad address at %s line %d"), ETHERSFILE, lineno); |
| 738 | continue; |
| 739 | } |
| 740 | |
Simon Kelley | 33820b7 | 2004-04-03 21:10:00 +0100 | [diff] [blame] | 741 | flags = CONFIG_ADDR; |
Simon Kelley | 1cff166 | 2004-03-12 08:12:58 +0000 | [diff] [blame] | 742 | |
| 743 | for (config = configs; config; config = config->next) |
Simon Kelley | 33820b7 | 2004-04-03 21:10:00 +0100 | [diff] [blame] | 744 | if ((config->flags & CONFIG_ADDR) && config->addr.s_addr == addr.s_addr) |
Simon Kelley | 1cff166 | 2004-03-12 08:12:58 +0000 | [diff] [blame] | 745 | break; |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 746 | } |
| 747 | else |
| 748 | { |
Simon Kelley | 1cff166 | 2004-03-12 08:12:58 +0000 | [diff] [blame] | 749 | if (!canonicalise(ip)) |
Simon Kelley | b8187c8 | 2005-11-26 21:46:27 +0000 | [diff] [blame] | 750 | { |
| 751 | syslog(LOG_ERR, _("bad name at %s line %d"), ETHERSFILE, lineno); |
| 752 | continue; |
| 753 | } |
| 754 | |
Simon Kelley | 33820b7 | 2004-04-03 21:10:00 +0100 | [diff] [blame] | 755 | flags = CONFIG_NAME; |
Simon Kelley | 1cff166 | 2004-03-12 08:12:58 +0000 | [diff] [blame] | 756 | |
| 757 | for (config = configs; config; config = config->next) |
Simon Kelley | 33820b7 | 2004-04-03 21:10:00 +0100 | [diff] [blame] | 758 | if ((config->flags & CONFIG_NAME) && hostname_isequal(config->hostname, ip)) |
Simon Kelley | 1cff166 | 2004-03-12 08:12:58 +0000 | [diff] [blame] | 759 | break; |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 760 | } |
| 761 | |
Simon Kelley | 1cff166 | 2004-03-12 08:12:58 +0000 | [diff] [blame] | 762 | if (!config) |
| 763 | { |
Simon Kelley | 33820b7 | 2004-04-03 21:10:00 +0100 | [diff] [blame] | 764 | for (config = configs; config; config = config->next) |
| 765 | if ((config->flags & CONFIG_HWADDR) && |
Simon Kelley | 91dccd0 | 2005-03-31 17:48:32 +0100 | [diff] [blame] | 766 | config->wildcard_mask == 0 && |
Simon Kelley | 33820b7 | 2004-04-03 21:10:00 +0100 | [diff] [blame] | 767 | memcmp(config->hwaddr, hwaddr, ETHER_ADDR_LEN) == 0) |
| 768 | break; |
| 769 | |
| 770 | if (!config) |
| 771 | { |
| 772 | if (!(config = malloc(sizeof(struct dhcp_config)))) |
| 773 | continue; |
| 774 | config->flags = 0; |
Simon Kelley | 91dccd0 | 2005-03-31 17:48:32 +0100 | [diff] [blame] | 775 | config->wildcard_mask = 0; |
Simon Kelley | 33820b7 | 2004-04-03 21:10:00 +0100 | [diff] [blame] | 776 | config->next = configs; |
| 777 | configs = config; |
| 778 | } |
| 779 | |
| 780 | config->flags |= flags; |
| 781 | |
| 782 | if (flags & CONFIG_NAME) |
| 783 | { |
| 784 | if ((config->hostname = malloc(strlen(ip)+1))) |
| 785 | strcpy(config->hostname, ip); |
| 786 | else |
| 787 | config->flags &= ~CONFIG_NAME; |
| 788 | } |
| 789 | |
| 790 | if (flags & CONFIG_ADDR) |
| 791 | config->addr = addr; |
Simon Kelley | 1cff166 | 2004-03-12 08:12:58 +0000 | [diff] [blame] | 792 | } |
Simon Kelley | 33820b7 | 2004-04-03 21:10:00 +0100 | [diff] [blame] | 793 | |
Simon Kelley | de37951 | 2004-06-22 20:23:33 +0100 | [diff] [blame] | 794 | config->flags |= CONFIG_HWADDR | CONFIG_NOCLID; |
Simon Kelley | 33820b7 | 2004-04-03 21:10:00 +0100 | [diff] [blame] | 795 | memcpy(config->hwaddr, hwaddr, ETHER_ADDR_LEN); |
Simon Kelley | 1cff166 | 2004-03-12 08:12:58 +0000 | [diff] [blame] | 796 | |
Simon Kelley | 33820b7 | 2004-04-03 21:10:00 +0100 | [diff] [blame] | 797 | count++; |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 798 | } |
| 799 | |
| 800 | fclose(f); |
Simon Kelley | 33820b7 | 2004-04-03 21:10:00 +0100 | [diff] [blame] | 801 | |
Simon Kelley | b8187c8 | 2005-11-26 21:46:27 +0000 | [diff] [blame] | 802 | syslog(LOG_INFO, _("read %s - %d addresses"), ETHERSFILE, count); |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 803 | |
| 804 | daemon->dhcp_conf = configs; |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 805 | } |
| 806 | |
| 807 | void dhcp_update_configs(struct dhcp_config *configs) |
| 808 | { |
| 809 | /* Some people like to keep all static IP addresses in /etc/hosts. |
| 810 | This goes through /etc/hosts and sets static addresses for any DHCP config |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 811 | records which don't have an address and whose name matches. |
| 812 | We take care to maintain the invariant that any IP address can appear |
| 813 | in at most one dhcp-host. */ |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 814 | |
Simon Kelley | 1ab84e2 | 2004-01-29 16:48:35 +0000 | [diff] [blame] | 815 | struct dhcp_config *config; |
| 816 | struct crec *crec; |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 817 | |
Simon Kelley | 1ab84e2 | 2004-01-29 16:48:35 +0000 | [diff] [blame] | 818 | for (config = configs; config; config = config->next) |
Simon Kelley | 33820b7 | 2004-04-03 21:10:00 +0100 | [diff] [blame] | 819 | if (!(config->flags & CONFIG_ADDR) && |
| 820 | (config->flags & CONFIG_NAME) && |
Simon Kelley | 1ab84e2 | 2004-01-29 16:48:35 +0000 | [diff] [blame] | 821 | (crec = cache_find_by_name(NULL, config->hostname, 0, F_IPV4)) && |
| 822 | (crec->flags & F_HOSTS)) |
Simon Kelley | 33820b7 | 2004-04-03 21:10:00 +0100 | [diff] [blame] | 823 | { |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 824 | if (config_find_by_address(configs, crec->addr.addr.addr.addr4)) |
Simon Kelley | b8187c8 | 2005-11-26 21:46:27 +0000 | [diff] [blame] | 825 | syslog(LOG_WARNING, _("duplicate IP address %s (%s) in dhcp-config directive"), |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 826 | inet_ntoa(crec->addr.addr.addr.addr4), config->hostname); |
| 827 | else |
| 828 | { |
| 829 | config->addr = crec->addr.addr.addr.addr4; |
| 830 | config->flags |= CONFIG_ADDR; |
| 831 | } |
Simon Kelley | 33820b7 | 2004-04-03 21:10:00 +0100 | [diff] [blame] | 832 | } |
Simon Kelley | 1ab84e2 | 2004-01-29 16:48:35 +0000 | [diff] [blame] | 833 | } |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 834 | |
Simon Kelley | bb01cb9 | 2004-12-13 20:56:23 +0000 | [diff] [blame] | 835 | /* If we've not found a hostname any other way, try and see if there's one in /etc/hosts |
| 836 | for this address. If it has a domain part, that must match the set domain and |
| 837 | it gets stripped. */ |
| 838 | char *host_from_dns(struct daemon *daemon, struct in_addr addr) |
| 839 | { |
| 840 | struct crec *lookup = cache_find_by_addr(NULL, (struct all_addr *)&addr, 0, F_IPV4); |
| 841 | char *hostname = NULL; |
| 842 | |
| 843 | if (lookup && (lookup->flags & F_HOSTS)) |
| 844 | { |
| 845 | hostname = daemon->dhcp_buff; |
| 846 | hostname[256] = 0; |
| 847 | strncpy(hostname, cache_get_name(lookup), 256); |
| 848 | hostname = strip_hostname(daemon, hostname); |
| 849 | } |
| 850 | |
| 851 | return hostname; |
| 852 | } |
| 853 | |
| 854 | char *strip_hostname(struct daemon *daemon, char *hostname) |
| 855 | { |
| 856 | char *dot = strchr(hostname, '.'); |
| 857 | if (dot) |
| 858 | { |
| 859 | if (!daemon->domain_suffix || !hostname_isequal(dot+1, daemon->domain_suffix)) |
| 860 | { |
Simon Kelley | b8187c8 | 2005-11-26 21:46:27 +0000 | [diff] [blame] | 861 | syslog(LOG_WARNING, _("Ignoring DHCP host name %s because it has an illegal domain part"), hostname); |
Simon Kelley | bb01cb9 | 2004-12-13 20:56:23 +0000 | [diff] [blame] | 862 | hostname = NULL; |
| 863 | } |
| 864 | else |
| 865 | { |
| 866 | *dot = 0; /* truncate */ |
| 867 | if (strlen(hostname) == 0) |
| 868 | hostname = NULL; /* nothing left */ |
| 869 | } |
| 870 | } |
| 871 | return hostname; |
| 872 | } |