Simon Kelley | 5954608 | 2012-01-06 20:02:04 +0000 | [diff] [blame] | 1 | /* dnsmasq is Copyright (c) 2000-2012 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 |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 5 | the Free Software Foundation; version 2 dated June, 1991, or |
| 6 | (at your option) version 3 dated 29 June, 2007. |
| 7 | |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 8 | This program is distributed in the hope that it will be useful, |
| 9 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | GNU General Public License for more details. |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 12 | |
Simon Kelley | 73a08a2 | 2009-02-05 20:28:08 +0000 | [diff] [blame] | 13 | You should have received a copy of the GNU General Public License |
| 14 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 15 | */ |
| 16 | |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 17 | #include "dnsmasq.h" |
| 18 | |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 19 | #ifdef HAVE_DHCP |
| 20 | |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 21 | struct iface_param { |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 22 | struct dhcp_context *current; |
| 23 | int ind; |
| 24 | }; |
| 25 | |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 26 | struct match_param { |
| 27 | int ind, matched; |
| 28 | struct in_addr netmask, broadcast, addr; |
| 29 | }; |
| 30 | |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 31 | static int complete_context(struct in_addr local, int if_index, |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 32 | struct in_addr netmask, struct in_addr broadcast, void *vparam); |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 33 | static int check_listen_addrs(struct in_addr local, int if_index, |
| 34 | struct in_addr netmask, struct in_addr broadcast, void *vparam); |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 35 | |
Simon Kelley | 316e273 | 2010-01-22 20:16:09 +0000 | [diff] [blame] | 36 | static int make_fd(int port) |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 37 | { |
| 38 | int fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP); |
| 39 | struct sockaddr_in saddr; |
Simon Kelley | 7cebd20 | 2006-05-06 14:13:33 +0100 | [diff] [blame] | 40 | int oneopt = 1; |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 41 | #if defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DONT) |
| 42 | int mtu = IP_PMTUDISC_DONT; |
| 43 | #endif |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 44 | #if defined(IP_TOS) && defined(IPTOS_CLASS_CS6) |
| 45 | int tos = IPTOS_CLASS_CS6; |
| 46 | #endif |
Simon Kelley | dfa666f | 2004-08-02 18:27:27 +0100 | [diff] [blame] | 47 | |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 48 | if (fd == -1) |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 49 | die (_("cannot create DHCP socket: %s"), NULL, EC_BADNET); |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 50 | |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 51 | if (!fix_fd(fd) || |
| 52 | #if defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DONT) |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 53 | setsockopt(fd, IPPROTO_IP, IP_MTU_DISCOVER, &mtu, sizeof(mtu)) == -1 || |
| 54 | #endif |
| 55 | #if defined(IP_TOS) && defined(IPTOS_CLASS_CS6) |
| 56 | setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos)) == -1 || |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 57 | #endif |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 58 | #if defined(HAVE_LINUX_NETWORK) |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 59 | setsockopt(fd, IPPROTO_IP, IP_PKTINFO, &oneopt, sizeof(oneopt)) == -1 || |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 60 | #else |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 61 | setsockopt(fd, IPPROTO_IP, IP_RECVIF, &oneopt, sizeof(oneopt)) == -1 || |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 62 | #endif |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 63 | setsockopt(fd, SOL_SOCKET, SO_BROADCAST, &oneopt, sizeof(oneopt)) == -1) |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 64 | die(_("failed to set options on DHCP socket: %s"), NULL, EC_BADNET); |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 65 | |
Simon Kelley | f6b7dc4 | 2005-01-23 12:06:08 +0000 | [diff] [blame] | 66 | /* When bind-interfaces is set, there might be more than one dnmsasq |
Simon Kelley | 4011c4e | 2006-10-28 16:26:19 +0100 | [diff] [blame] | 67 | instance binding port 67. That's OK if they serve different networks. |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 68 | Need to set REUSEADDR to make this posible, or REUSEPORT on *BSD. */ |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 69 | if (option_bool(OPT_NOWILD)) |
Simon Kelley | 4011c4e | 2006-10-28 16:26:19 +0100 | [diff] [blame] | 70 | { |
| 71 | #ifdef SO_REUSEPORT |
| 72 | int rc = setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &oneopt, sizeof(oneopt)); |
| 73 | #else |
| 74 | int rc = setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &oneopt, sizeof(oneopt)); |
| 75 | #endif |
| 76 | if (rc == -1) |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 77 | die(_("failed to set SO_REUSE{ADDR|PORT} on DHCP socket: %s"), NULL, EC_BADNET); |
Simon Kelley | 4011c4e | 2006-10-28 16:26:19 +0100 | [diff] [blame] | 78 | } |
Simon Kelley | f6b7dc4 | 2005-01-23 12:06:08 +0000 | [diff] [blame] | 79 | |
Simon Kelley | 849a835 | 2006-06-09 21:02:31 +0100 | [diff] [blame] | 80 | memset(&saddr, 0, sizeof(saddr)); |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 81 | saddr.sin_family = AF_INET; |
Simon Kelley | 316e273 | 2010-01-22 20:16:09 +0000 | [diff] [blame] | 82 | saddr.sin_port = htons(port); |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 83 | saddr.sin_addr.s_addr = INADDR_ANY; |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 84 | #ifdef HAVE_SOCKADDR_SA_LEN |
| 85 | saddr.sin_len = sizeof(struct sockaddr_in); |
| 86 | #endif |
| 87 | |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 88 | if (bind(fd, (struct sockaddr *)&saddr, sizeof(struct sockaddr_in))) |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 89 | die(_("failed to bind DHCP server socket: %s"), NULL, EC_BADNET); |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 90 | |
Simon Kelley | 316e273 | 2010-01-22 20:16:09 +0000 | [diff] [blame] | 91 | return fd; |
| 92 | } |
| 93 | |
| 94 | void dhcp_init(void) |
| 95 | { |
| 96 | #if defined(HAVE_BSD_NETWORK) |
| 97 | int oneopt = 1; |
| 98 | #endif |
| 99 | |
| 100 | daemon->dhcpfd = make_fd(daemon->dhcp_server_port); |
| 101 | if (daemon->enable_pxe) |
| 102 | daemon->pxefd = make_fd(PXE_PORT); |
| 103 | else |
| 104 | daemon->pxefd = -1; |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 105 | |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 106 | #if defined(HAVE_BSD_NETWORK) |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 107 | /* When we're not using capabilities, we need to do this here before |
| 108 | we drop root. Also, set buffer size small, to avoid wasting |
| 109 | kernel buffers */ |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 110 | |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 111 | if (option_bool(OPT_NO_PING)) |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 112 | daemon->dhcp_icmp_fd = -1; |
| 113 | else if ((daemon->dhcp_icmp_fd = make_icmp_sock()) == -1 || |
| 114 | setsockopt(daemon->dhcp_icmp_fd, SOL_SOCKET, SO_RCVBUF, &oneopt, sizeof(oneopt)) == -1 ) |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 115 | die(_("cannot create ICMP raw socket: %s."), NULL, EC_BADNET); |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 116 | |
| 117 | /* Make BPF raw send socket */ |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 118 | init_bpf(); |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 119 | #endif |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 120 | |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 121 | check_dhcp_hosts(1); |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 122 | } |
| 123 | |
Simon Kelley | 316e273 | 2010-01-22 20:16:09 +0000 | [diff] [blame] | 124 | void dhcp_packet(time_t now, int pxe_fd) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 125 | { |
Simon Kelley | 316e273 | 2010-01-22 20:16:09 +0000 | [diff] [blame] | 126 | int fd = pxe_fd ? daemon->pxefd : daemon->dhcpfd; |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 127 | struct dhcp_packet *mess; |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 128 | struct dhcp_context *context; |
| 129 | struct iname *tmp; |
| 130 | struct ifreq ifr; |
| 131 | struct msghdr msg; |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 132 | struct sockaddr_in dest; |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 133 | struct cmsghdr *cmptr; |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 134 | struct iovec iov; |
| 135 | ssize_t sz; |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 136 | int iface_index = 0, unicast_dest = 0, is_inform = 0; |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 137 | struct in_addr iface_addr; |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 138 | struct iface_param parm; |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 139 | #ifdef HAVE_LINUX_NETWORK |
| 140 | struct arpreq arp_req; |
| 141 | #endif |
| 142 | |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 143 | union { |
| 144 | struct cmsghdr align; /* this ensures alignment */ |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 145 | #if defined(HAVE_LINUX_NETWORK) |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 146 | char control[CMSG_SPACE(sizeof(struct in_pktinfo))]; |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 147 | #elif defined(HAVE_SOLARIS_NETWORK) |
| 148 | char control[CMSG_SPACE(sizeof(unsigned int))]; |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 149 | #elif defined(HAVE_BSD_NETWORK) |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 150 | char control[CMSG_SPACE(sizeof(struct sockaddr_dl))]; |
| 151 | #endif |
| 152 | } control_u; |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 153 | struct dhcp_bridge *bridge, *alias; |
| 154 | |
| 155 | msg.msg_controllen = sizeof(control_u); |
| 156 | msg.msg_control = control_u.control; |
| 157 | msg.msg_name = &dest; |
| 158 | msg.msg_namelen = sizeof(dest); |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 159 | msg.msg_iov = &daemon->dhcp_packet; |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 160 | msg.msg_iovlen = 1; |
| 161 | |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 162 | if ((sz = recv_dhcp_packet(fd, &msg)) == -1 || |
| 163 | (sz < (ssize_t)(sizeof(*mess) - sizeof(mess->options)))) |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 164 | return; |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 165 | |
| 166 | #if defined (HAVE_LINUX_NETWORK) |
Simon Kelley | 4011c4e | 2006-10-28 16:26:19 +0100 | [diff] [blame] | 167 | if (msg.msg_controllen >= sizeof(struct cmsghdr)) |
| 168 | for (cmptr = CMSG_FIRSTHDR(&msg); cmptr; cmptr = CMSG_NXTHDR(&msg, cmptr)) |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 169 | if (cmptr->cmsg_level == IPPROTO_IP && cmptr->cmsg_type == IP_PKTINFO) |
Simon Kelley | 4011c4e | 2006-10-28 16:26:19 +0100 | [diff] [blame] | 170 | { |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 171 | union { |
| 172 | unsigned char *c; |
| 173 | struct in_pktinfo *p; |
| 174 | } p; |
| 175 | p.c = CMSG_DATA(cmptr); |
| 176 | iface_index = p.p->ipi_ifindex; |
| 177 | if (p.p->ipi_addr.s_addr != INADDR_BROADCAST) |
Simon Kelley | 4011c4e | 2006-10-28 16:26:19 +0100 | [diff] [blame] | 178 | unicast_dest = 1; |
| 179 | } |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 180 | |
| 181 | #elif defined(HAVE_BSD_NETWORK) |
Simon Kelley | 4011c4e | 2006-10-28 16:26:19 +0100 | [diff] [blame] | 182 | if (msg.msg_controllen >= sizeof(struct cmsghdr)) |
| 183 | for (cmptr = CMSG_FIRSTHDR(&msg); cmptr; cmptr = CMSG_NXTHDR(&msg, cmptr)) |
| 184 | if (cmptr->cmsg_level == IPPROTO_IP && cmptr->cmsg_type == IP_RECVIF) |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 185 | { |
| 186 | union { |
| 187 | unsigned char *c; |
| 188 | struct sockaddr_dl *s; |
| 189 | } p; |
| 190 | p.c = CMSG_DATA(cmptr); |
| 191 | iface_index = p.s->sdl_index; |
| 192 | } |
Simon Kelley | 4011c4e | 2006-10-28 16:26:19 +0100 | [diff] [blame] | 193 | |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 194 | #elif defined(HAVE_SOLARIS_NETWORK) |
| 195 | if (msg.msg_controllen >= sizeof(struct cmsghdr)) |
| 196 | for (cmptr = CMSG_FIRSTHDR(&msg); cmptr; cmptr = CMSG_NXTHDR(&msg, cmptr)) |
| 197 | if (cmptr->cmsg_level == IPPROTO_IP && cmptr->cmsg_type == IP_RECVIF) |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 198 | { |
| 199 | union { |
| 200 | unsigned char *c; |
| 201 | unsigned int *i; |
| 202 | } p; |
| 203 | p.c = CMSG_DATA(cmptr); |
| 204 | iface_index = *(p.i); |
| 205 | } |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 206 | #endif |
| 207 | |
| 208 | if (!indextoname(daemon->dhcpfd, iface_index, ifr.ifr_name)) |
| 209 | return; |
| 210 | |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 211 | #ifdef HAVE_LINUX_NETWORK |
| 212 | /* ARP fiddling uses original interface even if we pretend to use a different one. */ |
| 213 | strncpy(arp_req.arp_dev, ifr.ifr_name, 16); |
| 214 | #endif |
| 215 | |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 216 | /* One form of bridging on BSD has the property that packets |
| 217 | can be recieved on bridge interfaces which do not have an IP address. |
| 218 | We allow these to be treated as aliases of another interface which does have |
| 219 | an IP address with --dhcp-bridge=interface,alias,alias */ |
| 220 | for (bridge = daemon->bridges; bridge; bridge = bridge->next) |
| 221 | { |
| 222 | for (alias = bridge->alias; alias; alias = alias->next) |
| 223 | if (strncmp(ifr.ifr_name, alias->iface, IF_NAMESIZE) == 0) |
| 224 | { |
| 225 | if (!(iface_index = if_nametoindex(bridge->iface))) |
| 226 | { |
| 227 | my_syslog(LOG_WARNING, _("unknown interface %s in bridge-interface"), ifr.ifr_name); |
| 228 | return; |
| 229 | } |
| 230 | else |
| 231 | { |
| 232 | strncpy(ifr.ifr_name, bridge->iface, IF_NAMESIZE); |
| 233 | break; |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | if (alias) |
| 238 | break; |
| 239 | } |
| 240 | |
Simon Kelley | 4011c4e | 2006-10-28 16:26:19 +0100 | [diff] [blame] | 241 | #ifdef MSG_BCAST |
| 242 | /* OpenBSD tells us when a packet was broadcast */ |
| 243 | if (!(msg.msg_flags & MSG_BCAST)) |
| 244 | unicast_dest = 1; |
| 245 | #endif |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 246 | |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 247 | ifr.ifr_addr.sa_family = AF_INET; |
Simon Kelley | 832af0b | 2007-01-21 20:01:28 +0000 | [diff] [blame] | 248 | if (ioctl(daemon->dhcpfd, SIOCGIFADDR, &ifr) != -1 ) |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 249 | iface_addr = ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr; |
| 250 | else |
Simon Kelley | 832af0b | 2007-01-21 20:01:28 +0000 | [diff] [blame] | 251 | { |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 252 | my_syslog(MS_DHCP | LOG_WARNING, _("DHCP packet received on %s which has no address"), ifr.ifr_name); |
| 253 | return; |
Simon Kelley | 832af0b | 2007-01-21 20:01:28 +0000 | [diff] [blame] | 254 | } |
Simon Kelley | 832af0b | 2007-01-21 20:01:28 +0000 | [diff] [blame] | 255 | |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 256 | for (tmp = daemon->dhcp_except; tmp; tmp = tmp->next) |
| 257 | if (tmp->name && (strcmp(tmp->name, ifr.ifr_name) == 0)) |
| 258 | return; |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 259 | |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 260 | /* weird libvirt-inspired access control */ |
| 261 | for (context = daemon->dhcp; context; context = context->next) |
| 262 | if (!context->interface || strcmp(context->interface, ifr.ifr_name) == 0) |
| 263 | break; |
| 264 | |
| 265 | if (!context) |
| 266 | return; |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 267 | |
Simon Kelley | 91dccd0 | 2005-03-31 17:48:32 +0100 | [diff] [blame] | 268 | /* unlinked contexts are marked by context->current == context */ |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 269 | for (context = daemon->dhcp; context; context = context->next) |
Simon Kelley | 91dccd0 | 2005-03-31 17:48:32 +0100 | [diff] [blame] | 270 | context->current = context; |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 271 | |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 272 | parm.current = NULL; |
| 273 | parm.ind = iface_index; |
Simon Kelley | e17fb62 | 2006-01-14 20:33:46 +0000 | [diff] [blame] | 274 | |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 275 | if (!iface_check(AF_INET, (struct all_addr *)&iface_addr, ifr.ifr_name)) |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 276 | { |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 277 | /* If we failed to match the primary address of the interface, see if we've got a --listen-address |
| 278 | for a secondary */ |
| 279 | struct match_param match; |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 280 | |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 281 | match.matched = 0; |
| 282 | match.ind = iface_index; |
| 283 | |
| 284 | if (!daemon->if_addrs || |
| 285 | !iface_enumerate(AF_INET, &match, check_listen_addrs) || |
| 286 | !match.matched) |
| 287 | return; |
| 288 | |
| 289 | iface_addr = match.addr; |
| 290 | /* make sure secondary address gets priority in case |
| 291 | there is more than one address on the interface in the same subnet */ |
| 292 | complete_context(match.addr, iface_index, match.netmask, match.broadcast, &parm); |
| 293 | } |
| 294 | |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 295 | if (!iface_enumerate(AF_INET, &parm, complete_context)) |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 296 | return; |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 297 | |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 298 | lease_prune(NULL, now); /* lose any expired leases */ |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 299 | iov.iov_len = dhcp_reply(parm.current, ifr.ifr_name, iface_index, (size_t)sz, |
Simon Kelley | 7de060b | 2011-08-26 17:24:52 +0100 | [diff] [blame] | 300 | now, unicast_dest, &is_inform, pxe_fd, iface_addr); |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 301 | lease_update_file(now); |
| 302 | lease_update_dns(); |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 303 | |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 304 | if (iov.iov_len == 0) |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 305 | return; |
| 306 | |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 307 | msg.msg_name = &dest; |
| 308 | msg.msg_namelen = sizeof(dest); |
| 309 | msg.msg_control = NULL; |
| 310 | msg.msg_controllen = 0; |
| 311 | msg.msg_iov = &iov; |
| 312 | iov.iov_base = daemon->dhcp_packet.iov_base; |
| 313 | |
| 314 | /* packet buffer may have moved */ |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 315 | mess = (struct dhcp_packet *)daemon->dhcp_packet.iov_base; |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 316 | |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 317 | #ifdef HAVE_SOCKADDR_SA_LEN |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 318 | dest.sin_len = sizeof(struct sockaddr_in); |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 319 | #endif |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 320 | |
Simon Kelley | 316e273 | 2010-01-22 20:16:09 +0000 | [diff] [blame] | 321 | if (pxe_fd) |
| 322 | { |
| 323 | if (mess->ciaddr.s_addr != 0) |
| 324 | dest.sin_addr = mess->ciaddr; |
| 325 | } |
| 326 | else if (mess->giaddr.s_addr) |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 327 | { |
| 328 | /* Send to BOOTP relay */ |
Simon Kelley | 9e03894 | 2008-05-30 20:06:34 +0100 | [diff] [blame] | 329 | dest.sin_port = htons(daemon->dhcp_server_port); |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 330 | dest.sin_addr = mess->giaddr; |
| 331 | } |
| 332 | else if (mess->ciaddr.s_addr) |
| 333 | { |
Simon Kelley | 208b65c | 2006-08-05 21:41:37 +0100 | [diff] [blame] | 334 | /* If the client's idea of its own address tallys with |
| 335 | the source address in the request packet, we believe the |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 336 | source port too, and send back to that. If we're replying |
| 337 | to a DHCPINFORM, trust the source address always. */ |
| 338 | if ((!is_inform && dest.sin_addr.s_addr != mess->ciaddr.s_addr) || |
| 339 | dest.sin_port == 0 || dest.sin_addr.s_addr == 0) |
Simon Kelley | 208b65c | 2006-08-05 21:41:37 +0100 | [diff] [blame] | 340 | { |
Simon Kelley | 9e03894 | 2008-05-30 20:06:34 +0100 | [diff] [blame] | 341 | dest.sin_port = htons(daemon->dhcp_client_port); |
Simon Kelley | 208b65c | 2006-08-05 21:41:37 +0100 | [diff] [blame] | 342 | dest.sin_addr = mess->ciaddr; |
| 343 | } |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 344 | } |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 345 | #if defined(HAVE_LINUX_NETWORK) |
Simon Kelley | 849a835 | 2006-06-09 21:02:31 +0100 | [diff] [blame] | 346 | else if ((ntohs(mess->flags) & 0x8000) || mess->hlen == 0 || |
| 347 | mess->hlen > sizeof(ifr.ifr_addr.sa_data) || mess->htype == 0) |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 348 | { |
Simon Kelley | 849a835 | 2006-06-09 21:02:31 +0100 | [diff] [blame] | 349 | /* broadcast to 255.255.255.255 (or mac address invalid) */ |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 350 | struct in_pktinfo *pkt; |
Simon Kelley | 26d0dba | 2006-04-23 20:00:42 +0100 | [diff] [blame] | 351 | msg.msg_control = control_u.control; |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 352 | msg.msg_controllen = sizeof(control_u); |
| 353 | cmptr = CMSG_FIRSTHDR(&msg); |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 354 | pkt = (struct in_pktinfo *)CMSG_DATA(cmptr); |
| 355 | pkt->ipi_ifindex = iface_index; |
| 356 | pkt->ipi_spec_dst.s_addr = 0; |
| 357 | msg.msg_controllen = cmptr->cmsg_len = CMSG_LEN(sizeof(struct in_pktinfo)); |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 358 | cmptr->cmsg_level = IPPROTO_IP; |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 359 | cmptr->cmsg_type = IP_PKTINFO; |
| 360 | dest.sin_addr.s_addr = INADDR_BROADCAST; |
Simon Kelley | 9e03894 | 2008-05-30 20:06:34 +0100 | [diff] [blame] | 361 | dest.sin_port = htons(daemon->dhcp_client_port); |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 362 | } |
| 363 | else |
| 364 | { |
Simon Kelley | 849a835 | 2006-06-09 21:02:31 +0100 | [diff] [blame] | 365 | /* unicast to unconfigured client. Inject mac address direct into ARP cache. |
| 366 | struct sockaddr limits size to 14 bytes. */ |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 367 | dest.sin_addr = mess->yiaddr; |
Simon Kelley | 9e03894 | 2008-05-30 20:06:34 +0100 | [diff] [blame] | 368 | dest.sin_port = htons(daemon->dhcp_client_port); |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 369 | memcpy(&arp_req.arp_pa, &dest, sizeof(struct sockaddr_in)); |
| 370 | arp_req.arp_ha.sa_family = mess->htype; |
| 371 | memcpy(arp_req.arp_ha.sa_data, mess->chaddr, mess->hlen); |
| 372 | /* interface name already copied in */ |
| 373 | arp_req.arp_flags = ATF_COM; |
| 374 | ioctl(daemon->dhcpfd, SIOCSARP, &arp_req); |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 375 | } |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 376 | #elif defined(HAVE_SOLARIS_NETWORK) |
| 377 | else if ((ntohs(mess->flags) & 0x8000) || mess->hlen != ETHER_ADDR_LEN || mess->htype != ARPHRD_ETHER) |
| 378 | { |
| 379 | /* broadcast to 255.255.255.255 (or mac address invalid) */ |
| 380 | dest.sin_addr.s_addr = INADDR_BROADCAST; |
Simon Kelley | 9e03894 | 2008-05-30 20:06:34 +0100 | [diff] [blame] | 381 | dest.sin_port = htons(daemon->dhcp_client_port); |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 382 | /* note that we don't specify the interface here: that's done by the |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 383 | IP_BOUND_IF sockopt lower down. */ |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 384 | } |
| 385 | else |
| 386 | { |
| 387 | /* unicast to unconfigured client. Inject mac address direct into ARP cache. |
| 388 | Note that this only works for ethernet on solaris, because we use SIOCSARP |
| 389 | and not SIOCSXARP, which would be perfect, except that it returns ENXIO |
| 390 | mysteriously. Bah. Fall back to broadcast for other net types. */ |
| 391 | struct arpreq req; |
| 392 | dest.sin_addr = mess->yiaddr; |
Simon Kelley | 9e03894 | 2008-05-30 20:06:34 +0100 | [diff] [blame] | 393 | dest.sin_port = htons(daemon->dhcp_client_port); |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 394 | *((struct sockaddr_in *)&req.arp_pa) = dest; |
| 395 | req.arp_ha.sa_family = AF_UNSPEC; |
| 396 | memcpy(req.arp_ha.sa_data, mess->chaddr, mess->hlen); |
| 397 | req.arp_flags = ATF_COM; |
| 398 | ioctl(daemon->dhcpfd, SIOCSARP, &req); |
| 399 | } |
| 400 | #elif defined(HAVE_BSD_NETWORK) |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 401 | else |
| 402 | { |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 403 | send_via_bpf(mess, iov.iov_len, iface_addr, &ifr); |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 404 | return; |
| 405 | } |
| 406 | #endif |
| 407 | |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 408 | #ifdef HAVE_SOLARIS_NETWORK |
Simon Kelley | 316e273 | 2010-01-22 20:16:09 +0000 | [diff] [blame] | 409 | setsockopt(fd, IPPROTO_IP, IP_BOUND_IF, &iface_index, sizeof(iface_index)); |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 410 | #endif |
| 411 | |
Simon Kelley | 316e273 | 2010-01-22 20:16:09 +0000 | [diff] [blame] | 412 | while(sendmsg(fd, &msg, 0) == -1 && retry_send()); |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 413 | } |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 414 | |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 415 | /* check against secondary interface addresses */ |
| 416 | static int check_listen_addrs(struct in_addr local, int if_index, |
| 417 | struct in_addr netmask, struct in_addr broadcast, void *vparam) |
| 418 | { |
| 419 | struct match_param *param = vparam; |
| 420 | struct iname *tmp; |
| 421 | |
| 422 | if (if_index == param->ind) |
| 423 | { |
| 424 | for (tmp = daemon->if_addrs; tmp; tmp = tmp->next) |
| 425 | if ( tmp->addr.sa.sa_family == AF_INET && |
| 426 | tmp->addr.in.sin_addr.s_addr == local.s_addr) |
| 427 | { |
| 428 | param->matched = 1; |
| 429 | param->addr = local; |
| 430 | param->netmask = netmask; |
| 431 | param->broadcast = broadcast; |
| 432 | break; |
| 433 | } |
| 434 | } |
| 435 | |
| 436 | return 1; |
| 437 | } |
| 438 | |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 439 | /* This is a complex routine: it gets called with each (address,netmask,broadcast) triple |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 440 | of each interface (and any relay address) and does the following things: |
| 441 | |
| 442 | 1) Discards stuff for interfaces other than the one on which a DHCP packet just arrived. |
| 443 | 2) Fills in any netmask and broadcast addresses which have not been explicitly configured. |
| 444 | 3) Fills in local (this host) and router (this host or relay) addresses. |
| 445 | 4) Links contexts which are valid for hosts directly connected to the arrival interface on ->current. |
| 446 | |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 447 | Note that the current chain may be superceded later for configured hosts or those coming via gateways. */ |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 448 | |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 449 | static int complete_context(struct in_addr local, int if_index, |
| 450 | struct in_addr netmask, struct in_addr broadcast, void *vparam) |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 451 | { |
| 452 | struct dhcp_context *context; |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 453 | struct iface_param *param = vparam; |
| 454 | |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 455 | for (context = daemon->dhcp; context; context = context->next) |
| 456 | { |
| 457 | if (!(context->flags & CONTEXT_NETMASK) && |
| 458 | (is_same_net(local, context->start, netmask) || |
| 459 | is_same_net(local, context->end, netmask))) |
| 460 | { |
| 461 | if (context->netmask.s_addr != netmask.s_addr && |
| 462 | !(is_same_net(local, context->start, netmask) && |
| 463 | is_same_net(local, context->end, netmask))) |
| 464 | { |
| 465 | strcpy(daemon->dhcp_buff, inet_ntoa(context->start)); |
| 466 | strcpy(daemon->dhcp_buff2, inet_ntoa(context->end)); |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 467 | my_syslog(MS_DHCP | LOG_WARNING, _("DHCP range %s -- %s is not consistent with netmask %s"), |
Simon Kelley | f2621c7 | 2007-04-29 19:47:21 +0100 | [diff] [blame] | 468 | daemon->dhcp_buff, daemon->dhcp_buff2, inet_ntoa(netmask)); |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 469 | } |
| 470 | context->netmask = netmask; |
| 471 | } |
| 472 | |
Simon Kelley | 7de060b | 2011-08-26 17:24:52 +0100 | [diff] [blame] | 473 | if (context->netmask.s_addr != 0 && |
| 474 | is_same_net(local, context->start, context->netmask) && |
| 475 | is_same_net(local, context->end, context->netmask)) |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 476 | { |
Simon Kelley | 7de060b | 2011-08-26 17:24:52 +0100 | [diff] [blame] | 477 | /* link it onto the current chain if we've not seen it before */ |
| 478 | if (if_index == param->ind && context->current == context) |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 479 | { |
Simon Kelley | 7de060b | 2011-08-26 17:24:52 +0100 | [diff] [blame] | 480 | context->router = local; |
| 481 | context->local = local; |
| 482 | context->current = param->current; |
| 483 | param->current = context; |
| 484 | } |
| 485 | |
| 486 | if (!(context->flags & CONTEXT_BRDCAST)) |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 487 | { |
Simon Kelley | 7de060b | 2011-08-26 17:24:52 +0100 | [diff] [blame] | 488 | if (is_same_net(broadcast, context->start, context->netmask)) |
| 489 | context->broadcast = broadcast; |
| 490 | else |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 491 | context->broadcast.s_addr = context->start.s_addr | ~context->netmask.s_addr; |
| 492 | } |
Simon Kelley | 7de060b | 2011-08-26 17:24:52 +0100 | [diff] [blame] | 493 | } |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 494 | } |
| 495 | |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 496 | return 1; |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 497 | } |
| 498 | |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 499 | struct dhcp_context *address_available(struct dhcp_context *context, |
| 500 | struct in_addr taddr, |
| 501 | struct dhcp_netid *netids) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 502 | { |
Simon Kelley | 36717ee | 2004-09-20 19:20:58 +0100 | [diff] [blame] | 503 | /* Check is an address is OK for this network, check all |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 504 | possible ranges. Make sure that the address isn't in use |
| 505 | by the server itself. */ |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 506 | |
Simon Kelley | 36717ee | 2004-09-20 19:20:58 +0100 | [diff] [blame] | 507 | unsigned int start, end, addr = ntohl(taddr.s_addr); |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 508 | struct dhcp_context *tmp; |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 509 | |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 510 | for (tmp = context; tmp; tmp = tmp->current) |
Simon Kelley | 849a835 | 2006-06-09 21:02:31 +0100 | [diff] [blame] | 511 | if (taddr.s_addr == context->router.s_addr) |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 512 | return NULL; |
| 513 | |
| 514 | for (tmp = context; tmp; tmp = tmp->current) |
| 515 | { |
| 516 | start = ntohl(tmp->start.s_addr); |
| 517 | end = ntohl(tmp->end.s_addr); |
| 518 | |
Simon Kelley | 7de060b | 2011-08-26 17:24:52 +0100 | [diff] [blame] | 519 | if (!(tmp->flags & (CONTEXT_STATIC | CONTEXT_PROXY)) && |
Simon Kelley | 36717ee | 2004-09-20 19:20:58 +0100 | [diff] [blame] | 520 | addr >= start && |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 521 | addr <= end && |
| 522 | match_netid(tmp->filter, netids, 1)) |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 523 | return tmp; |
Simon Kelley | 36717ee | 2004-09-20 19:20:58 +0100 | [diff] [blame] | 524 | } |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 525 | |
Simon Kelley | 59353a6 | 2004-11-21 19:34:28 +0000 | [diff] [blame] | 526 | return NULL; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 527 | } |
Simon Kelley | 59353a6 | 2004-11-21 19:34:28 +0000 | [diff] [blame] | 528 | |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 529 | struct dhcp_context *narrow_context(struct dhcp_context *context, |
| 530 | struct in_addr taddr, |
| 531 | struct dhcp_netid *netids) |
Simon Kelley | 59353a6 | 2004-11-21 19:34:28 +0000 | [diff] [blame] | 532 | { |
Simon Kelley | e17fb62 | 2006-01-14 20:33:46 +0000 | [diff] [blame] | 533 | /* 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] | 534 | These are chained on ->current. |
| 535 | Here we have an address, and return the actual context correponding to that |
| 536 | 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] | 537 | any dhcp-range. In that case we return a static range if possible, or failing that, |
| 538 | any context on the correct subnet. (If there's more than one, this is a dodgy |
| 539 | configuration: maybe there should be a warning.) */ |
Simon Kelley | 59353a6 | 2004-11-21 19:34:28 +0000 | [diff] [blame] | 540 | |
Simon Kelley | e17fb62 | 2006-01-14 20:33:46 +0000 | [diff] [blame] | 541 | struct dhcp_context *tmp; |
Simon Kelley | 59353a6 | 2004-11-21 19:34:28 +0000 | [diff] [blame] | 542 | |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 543 | if (!(tmp = address_available(context, taddr, netids))) |
| 544 | { |
| 545 | for (tmp = context; tmp; tmp = tmp->current) |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 546 | if (match_netid(tmp->filter, netids, 1) && |
| 547 | is_same_net(taddr, tmp->start, tmp->netmask) && |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 548 | (tmp->flags & CONTEXT_STATIC)) |
| 549 | break; |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 550 | |
| 551 | if (!tmp) |
| 552 | for (tmp = context; tmp; tmp = tmp->current) |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 553 | if (match_netid(tmp->filter, netids, 1) && |
Simon Kelley | 7de060b | 2011-08-26 17:24:52 +0100 | [diff] [blame] | 554 | is_same_net(taddr, tmp->start, tmp->netmask) && |
| 555 | !(tmp->flags & CONTEXT_PROXY)) |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 556 | break; |
| 557 | } |
Simon Kelley | 59353a6 | 2004-11-21 19:34:28 +0000 | [diff] [blame] | 558 | |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 559 | /* Only one context allowed now */ |
| 560 | if (tmp) |
| 561 | tmp->current = NULL; |
| 562 | |
| 563 | return tmp; |
Simon Kelley | 59353a6 | 2004-11-21 19:34:28 +0000 | [diff] [blame] | 564 | } |
| 565 | |
Simon Kelley | dfa666f | 2004-08-02 18:27:27 +0100 | [diff] [blame] | 566 | struct dhcp_config *config_find_by_address(struct dhcp_config *configs, struct in_addr addr) |
| 567 | { |
| 568 | struct dhcp_config *config; |
| 569 | |
| 570 | for (config = configs; config; config = config->next) |
| 571 | if ((config->flags & CONFIG_ADDR) && config->addr.s_addr == addr.s_addr) |
| 572 | return config; |
| 573 | |
| 574 | return NULL; |
| 575 | } |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 576 | |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 577 | int address_allocate(struct dhcp_context *context, |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 578 | struct in_addr *addrp, unsigned char *hwaddr, int hw_len, |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 579 | struct dhcp_netid *netids, time_t now) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 580 | { |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 581 | /* Find a free address: exclude anything in use and anything allocated to |
Simon Kelley | f6b7dc4 | 2005-01-23 12:06:08 +0000 | [diff] [blame] | 582 | a particular hwaddr/clientid/hostname in our configuration. |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 583 | Try to return from contexts which match netids first. */ |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 584 | |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 585 | struct in_addr start, addr; |
| 586 | struct dhcp_context *c, *d; |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 587 | int i, pass; |
| 588 | unsigned int j; |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 589 | |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 590 | /* hash hwaddr: use the SDBM hashing algorithm. Seems to give good |
| 591 | dispersal even with similarly-valued "strings". */ |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 592 | for (j = 0, i = 0; i < hw_len; i++) |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 593 | j += hwaddr[i] + (j << 6) + (j << 16) - j; |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 594 | |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 595 | for (pass = 0; pass <= 1; pass++) |
| 596 | for (c = context; c; c = c->current) |
Simon Kelley | 7de060b | 2011-08-26 17:24:52 +0100 | [diff] [blame] | 597 | if (c->flags & (CONTEXT_STATIC | CONTEXT_PROXY)) |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 598 | continue; |
| 599 | else if (!match_netid(c->filter, netids, pass)) |
| 600 | continue; |
| 601 | else |
| 602 | { |
Simon Kelley | 7de060b | 2011-08-26 17:24:52 +0100 | [diff] [blame] | 603 | if (option_bool(OPT_CONSEC_ADDR)) |
| 604 | /* seed is largest extant lease addr in this context */ |
| 605 | start = lease_find_max_addr(c); |
| 606 | else |
| 607 | /* pick a seed based on hwaddr */ |
| 608 | start.s_addr = htonl(ntohl(c->start.s_addr) + |
| 609 | ((j + c->addr_epoch) % (1 + ntohl(c->end.s_addr) - ntohl(c->start.s_addr)))); |
| 610 | |
| 611 | /* iterate until we find a free address. */ |
| 612 | addr = start; |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 613 | |
| 614 | do { |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 615 | /* eliminate addresses in use by the server. */ |
| 616 | for (d = context; d; d = d->current) |
Simon Kelley | 849a835 | 2006-06-09 21:02:31 +0100 | [diff] [blame] | 617 | if (addr.s_addr == d->router.s_addr) |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 618 | break; |
| 619 | |
Simon Kelley | 73a08a2 | 2009-02-05 20:28:08 +0000 | [diff] [blame] | 620 | /* Addresses which end in .255 and .0 are broken in Windows even when using |
| 621 | supernetting. ie dhcp-range=192.168.0.1,192.168.1.254,255,255,254.0 |
| 622 | then 192.168.0.255 is a valid IP address, but not for Windows as it's |
| 623 | in the class C range. See KB281579. We therefore don't allocate these |
| 624 | addresses to avoid hard-to-diagnose problems. Thanks Bill. */ |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 625 | if (!d && |
| 626 | !lease_find_by_addr(addr) && |
Simon Kelley | 73a08a2 | 2009-02-05 20:28:08 +0000 | [diff] [blame] | 627 | !config_find_by_address(daemon->dhcp_conf, addr) && |
| 628 | (!IN_CLASSC(ntohl(addr.s_addr)) || |
| 629 | ((ntohl(addr.s_addr) & 0xff) != 0xff && ((ntohl(addr.s_addr) & 0xff) != 0x0)))) |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 630 | { |
| 631 | struct ping_result *r, *victim = NULL; |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 632 | int count, max = (int)(0.6 * (((float)PING_CACHE_TIME)/ |
| 633 | ((float)PING_WAIT))); |
| 634 | |
| 635 | *addrp = addr; |
| 636 | |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 637 | /* check if we failed to ping addr sometime in the last |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 638 | PING_CACHE_TIME seconds. If so, assume the same situation still exists. |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 639 | This avoids problems when a stupid client bangs |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 640 | on us repeatedly. As a final check, if we did more |
| 641 | than 60% of the possible ping checks in the last |
| 642 | PING_CACHE_TIME, we are in high-load mode, so don't do any more. */ |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 643 | for (count = 0, r = daemon->ping_results; r; r = r->next) |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 644 | if (difftime(now, r->time) > (float)PING_CACHE_TIME) |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 645 | victim = r; /* old record */ |
Simon Kelley | 7de060b | 2011-08-26 17:24:52 +0100 | [diff] [blame] | 646 | else |
| 647 | { |
| 648 | count++; |
| 649 | if (r->addr.s_addr == addr.s_addr) |
| 650 | { |
| 651 | /* consec-ip mode: we offered this address for another client |
| 652 | (different hash) recently, don't offer it to this one. */ |
| 653 | if (option_bool(OPT_CONSEC_ADDR) && r->hash != j) |
| 654 | break; |
| 655 | |
| 656 | return 1; |
| 657 | } |
| 658 | } |
| 659 | |
| 660 | if (!r) |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 661 | { |
Simon Kelley | 7de060b | 2011-08-26 17:24:52 +0100 | [diff] [blame] | 662 | if ((count < max) && !option_bool(OPT_NO_PING) && icmp_ping(addr)) |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 663 | { |
Simon Kelley | 7de060b | 2011-08-26 17:24:52 +0100 | [diff] [blame] | 664 | /* address in use: perturb address selection so that we are |
| 665 | less likely to try this address again. */ |
| 666 | if (!option_bool(OPT_CONSEC_ADDR)) |
| 667 | c->addr_epoch++; |
| 668 | } |
| 669 | else |
| 670 | { |
| 671 | /* at this point victim may hold an expired record */ |
| 672 | if (!victim) |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 673 | { |
Simon Kelley | 7de060b | 2011-08-26 17:24:52 +0100 | [diff] [blame] | 674 | if ((victim = whine_malloc(sizeof(struct ping_result)))) |
| 675 | { |
| 676 | victim->next = daemon->ping_results; |
| 677 | daemon->ping_results = victim; |
| 678 | } |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 679 | } |
Simon Kelley | 7de060b | 2011-08-26 17:24:52 +0100 | [diff] [blame] | 680 | |
| 681 | /* record that this address is OK for 30s |
| 682 | without more ping checks */ |
| 683 | if (victim) |
| 684 | { |
| 685 | victim->addr = addr; |
| 686 | victim->time = now; |
| 687 | victim->hash = j; |
| 688 | } |
| 689 | return 1; |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 690 | } |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 691 | } |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 692 | } |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 693 | |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 694 | addr.s_addr = htonl(ntohl(addr.s_addr) + 1); |
| 695 | |
| 696 | if (addr.s_addr == htonl(ntohl(c->end.s_addr) + 1)) |
| 697 | addr = c->start; |
| 698 | |
| 699 | } while (addr.s_addr != start.s_addr); |
| 700 | } |
Simon Kelley | 7de060b | 2011-08-26 17:24:52 +0100 | [diff] [blame] | 701 | |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 702 | return 0; |
| 703 | } |
| 704 | |
| 705 | static int is_addr_in_context(struct dhcp_context *context, struct dhcp_config *config) |
| 706 | { |
Simon Kelley | b8187c8 | 2005-11-26 21:46:27 +0000 | [diff] [blame] | 707 | if (!context) /* called via find_config() from lease_update_from_configs() */ |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 708 | return 1; |
Simon Kelley | 33820b7 | 2004-04-03 21:10:00 +0100 | [diff] [blame] | 709 | if (!(config->flags & CONFIG_ADDR)) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 710 | return 1; |
Simon Kelley | b8187c8 | 2005-11-26 21:46:27 +0000 | [diff] [blame] | 711 | for (; context; context = context->current) |
| 712 | if (is_same_net(config->addr, context->start, context->netmask)) |
| 713 | return 1; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 714 | |
| 715 | return 0; |
| 716 | } |
| 717 | |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 718 | int config_has_mac(struct dhcp_config *config, unsigned char *hwaddr, int len, int type) |
| 719 | { |
| 720 | struct hwaddr_config *conf_addr; |
| 721 | |
| 722 | for (conf_addr = config->hwaddr; conf_addr; conf_addr = conf_addr->next) |
| 723 | if (conf_addr->wildcard_mask == 0 && |
| 724 | conf_addr->hwaddr_len == len && |
| 725 | (conf_addr->hwaddr_type == type || conf_addr->hwaddr_type == 0) && |
| 726 | memcmp(conf_addr->hwaddr, hwaddr, len) == 0) |
| 727 | return 1; |
| 728 | |
| 729 | return 0; |
| 730 | } |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 731 | |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 732 | struct dhcp_config *find_config(struct dhcp_config *configs, |
| 733 | struct dhcp_context *context, |
| 734 | unsigned char *clid, int clid_len, |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 735 | unsigned char *hwaddr, int hw_len, |
| 736 | int hw_type, char *hostname) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 737 | { |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 738 | int count, new; |
| 739 | struct dhcp_config *config, *candidate; |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 740 | struct hwaddr_config *conf_addr; |
| 741 | |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 742 | if (clid) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 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_CLID) |
| 745 | { |
| 746 | if (config->clid_len == clid_len && |
| 747 | memcmp(config->clid, clid, clid_len) == 0 && |
| 748 | is_addr_in_context(context, config)) |
| 749 | return config; |
| 750 | |
| 751 | /* dhcpcd prefixes ASCII client IDs by zero which is wrong, but we try and |
| 752 | cope with that here */ |
| 753 | if (*clid == 0 && config->clid_len == clid_len-1 && |
| 754 | memcmp(config->clid, clid+1, clid_len-1) == 0 && |
| 755 | is_addr_in_context(context, config)) |
| 756 | return config; |
| 757 | } |
| 758 | |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 759 | |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 760 | for (config = configs; config; config = config->next) |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 761 | if (config_has_mac(config, hwaddr, hw_len, hw_type) && |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 762 | is_addr_in_context(context, config)) |
| 763 | return config; |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 764 | |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 765 | if (hostname && context) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 766 | for (config = configs; config; config = config->next) |
Simon Kelley | 33820b7 | 2004-04-03 21:10:00 +0100 | [diff] [blame] | 767 | if ((config->flags & CONFIG_NAME) && |
| 768 | hostname_isequal(config->hostname, hostname) && |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 769 | is_addr_in_context(context, config)) |
| 770 | return config; |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 771 | |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 772 | /* use match with fewest wildcard octets */ |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 773 | for (candidate = NULL, count = 0, config = configs; config; config = config->next) |
| 774 | if (is_addr_in_context(context, config)) |
| 775 | for (conf_addr = config->hwaddr; conf_addr; conf_addr = conf_addr->next) |
| 776 | if (conf_addr->wildcard_mask != 0 && |
| 777 | conf_addr->hwaddr_len == hw_len && |
| 778 | (conf_addr->hwaddr_type == hw_type || conf_addr->hwaddr_type == 0) && |
| 779 | (new = memcmp_masked(conf_addr->hwaddr, hwaddr, hw_len, conf_addr->wildcard_mask)) > count) |
| 780 | { |
| 781 | count = new; |
| 782 | candidate = config; |
| 783 | } |
| 784 | |
| 785 | return candidate; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 786 | } |
| 787 | |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 788 | void dhcp_read_ethers(void) |
Simon Kelley | 1ab84e2 | 2004-01-29 16:48:35 +0000 | [diff] [blame] | 789 | { |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 790 | FILE *f = fopen(ETHERSFILE, "r"); |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 791 | unsigned int flags; |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 792 | char *buff = daemon->namebuff; |
Simon Kelley | 33820b7 | 2004-04-03 21:10:00 +0100 | [diff] [blame] | 793 | char *ip, *cp; |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 794 | struct in_addr addr; |
Simon Kelley | 33820b7 | 2004-04-03 21:10:00 +0100 | [diff] [blame] | 795 | unsigned char hwaddr[ETHER_ADDR_LEN]; |
Simon Kelley | 849a835 | 2006-06-09 21:02:31 +0100 | [diff] [blame] | 796 | struct dhcp_config **up, *tmp; |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 797 | struct dhcp_config *config; |
Simon Kelley | b8187c8 | 2005-11-26 21:46:27 +0000 | [diff] [blame] | 798 | int count = 0, lineno = 0; |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 799 | |
| 800 | addr.s_addr = 0; /* eliminate warning */ |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 801 | |
| 802 | if (!f) |
Simon Kelley | 33820b7 | 2004-04-03 21:10:00 +0100 | [diff] [blame] | 803 | { |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 804 | my_syslog(MS_DHCP | LOG_ERR, _("failed to read %s: %s"), ETHERSFILE, strerror(errno)); |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 805 | return; |
Simon Kelley | 33820b7 | 2004-04-03 21:10:00 +0100 | [diff] [blame] | 806 | } |
| 807 | |
Simon Kelley | 849a835 | 2006-06-09 21:02:31 +0100 | [diff] [blame] | 808 | /* This can be called again on SIGHUP, so remove entries created last time round. */ |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 809 | for (up = &daemon->dhcp_conf, config = daemon->dhcp_conf; config; config = tmp) |
Simon Kelley | 849a835 | 2006-06-09 21:02:31 +0100 | [diff] [blame] | 810 | { |
| 811 | tmp = config->next; |
| 812 | if (config->flags & CONFIG_FROM_ETHERS) |
| 813 | { |
| 814 | *up = tmp; |
| 815 | /* cannot have a clid */ |
| 816 | if (config->flags & CONFIG_NAME) |
| 817 | free(config->hostname); |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 818 | free(config->hwaddr); |
Simon Kelley | 849a835 | 2006-06-09 21:02:31 +0100 | [diff] [blame] | 819 | free(config); |
| 820 | } |
| 821 | else |
| 822 | up = &config->next; |
| 823 | } |
| 824 | |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 825 | while (fgets(buff, MAXDNAME, f)) |
| 826 | { |
Simon Kelley | 1f15b81 | 2009-10-13 17:49:32 +0100 | [diff] [blame] | 827 | char *host = NULL; |
| 828 | |
Simon Kelley | b8187c8 | 2005-11-26 21:46:27 +0000 | [diff] [blame] | 829 | lineno++; |
| 830 | |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 831 | while (strlen(buff) > 0 && isspace((int)buff[strlen(buff)-1])) |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 832 | buff[strlen(buff)-1] = 0; |
| 833 | |
Simon Kelley | 73a08a2 | 2009-02-05 20:28:08 +0000 | [diff] [blame] | 834 | if ((*buff == '#') || (*buff == '+') || (*buff == 0)) |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 835 | continue; |
| 836 | |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 837 | for (ip = buff; *ip && !isspace((int)*ip); ip++); |
| 838 | for(; *ip && isspace((int)*ip); ip++) |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 839 | *ip = 0; |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 840 | if (!*ip || parse_hex(buff, hwaddr, ETHER_ADDR_LEN, NULL, NULL) != ETHER_ADDR_LEN) |
Simon Kelley | b8187c8 | 2005-11-26 21:46:27 +0000 | [diff] [blame] | 841 | { |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 842 | my_syslog(MS_DHCP | LOG_ERR, _("bad line at %s line %d"), ETHERSFILE, lineno); |
Simon Kelley | b8187c8 | 2005-11-26 21:46:27 +0000 | [diff] [blame] | 843 | continue; |
| 844 | } |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 845 | |
| 846 | /* check for name or dotted-quad */ |
| 847 | for (cp = ip; *cp; cp++) |
| 848 | if (!(*cp == '.' || (*cp >='0' && *cp <= '9'))) |
| 849 | break; |
| 850 | |
| 851 | if (!*cp) |
| 852 | { |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 853 | if ((addr.s_addr = inet_addr(ip)) == (in_addr_t)-1) |
Simon Kelley | b8187c8 | 2005-11-26 21:46:27 +0000 | [diff] [blame] | 854 | { |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 855 | my_syslog(MS_DHCP | LOG_ERR, _("bad address at %s line %d"), ETHERSFILE, lineno); |
Simon Kelley | b8187c8 | 2005-11-26 21:46:27 +0000 | [diff] [blame] | 856 | continue; |
| 857 | } |
| 858 | |
Simon Kelley | 33820b7 | 2004-04-03 21:10:00 +0100 | [diff] [blame] | 859 | flags = CONFIG_ADDR; |
Simon Kelley | 1cff166 | 2004-03-12 08:12:58 +0000 | [diff] [blame] | 860 | |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 861 | for (config = daemon->dhcp_conf; config; config = config->next) |
Simon Kelley | 33820b7 | 2004-04-03 21:10:00 +0100 | [diff] [blame] | 862 | if ((config->flags & CONFIG_ADDR) && config->addr.s_addr == addr.s_addr) |
Simon Kelley | 1cff166 | 2004-03-12 08:12:58 +0000 | [diff] [blame] | 863 | break; |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 864 | } |
| 865 | else |
| 866 | { |
Simon Kelley | 1f15b81 | 2009-10-13 17:49:32 +0100 | [diff] [blame] | 867 | int nomem; |
| 868 | if (!(host = canonicalise(ip, &nomem)) || !legal_hostname(host)) |
Simon Kelley | b8187c8 | 2005-11-26 21:46:27 +0000 | [diff] [blame] | 869 | { |
Simon Kelley | 1f15b81 | 2009-10-13 17:49:32 +0100 | [diff] [blame] | 870 | if (!nomem) |
| 871 | my_syslog(MS_DHCP | LOG_ERR, _("bad name at %s line %d"), ETHERSFILE, lineno); |
| 872 | free(host); |
Simon Kelley | b8187c8 | 2005-11-26 21:46:27 +0000 | [diff] [blame] | 873 | continue; |
| 874 | } |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 875 | |
Simon Kelley | 33820b7 | 2004-04-03 21:10:00 +0100 | [diff] [blame] | 876 | flags = CONFIG_NAME; |
Simon Kelley | 1cff166 | 2004-03-12 08:12:58 +0000 | [diff] [blame] | 877 | |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 878 | for (config = daemon->dhcp_conf; config; config = config->next) |
Simon Kelley | 1f15b81 | 2009-10-13 17:49:32 +0100 | [diff] [blame] | 879 | if ((config->flags & CONFIG_NAME) && hostname_isequal(config->hostname, host)) |
Simon Kelley | 1cff166 | 2004-03-12 08:12:58 +0000 | [diff] [blame] | 880 | break; |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 881 | } |
Simon Kelley | 1f15b81 | 2009-10-13 17:49:32 +0100 | [diff] [blame] | 882 | |
| 883 | if (config && (config->flags & CONFIG_FROM_ETHERS)) |
| 884 | { |
| 885 | my_syslog(MS_DHCP | LOG_ERR, _("ignoring %s line %d, duplicate name or IP address"), ETHERSFILE, lineno); |
| 886 | continue; |
| 887 | } |
| 888 | |
Simon Kelley | 1cff166 | 2004-03-12 08:12:58 +0000 | [diff] [blame] | 889 | if (!config) |
| 890 | { |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 891 | for (config = daemon->dhcp_conf; config; config = config->next) |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 892 | { |
| 893 | struct hwaddr_config *conf_addr = config->hwaddr; |
| 894 | if (conf_addr && |
| 895 | conf_addr->next == NULL && |
| 896 | conf_addr->wildcard_mask == 0 && |
| 897 | conf_addr->hwaddr_len == ETHER_ADDR_LEN && |
| 898 | (conf_addr->hwaddr_type == ARPHRD_ETHER || conf_addr->hwaddr_type == 0) && |
| 899 | memcmp(conf_addr->hwaddr, hwaddr, ETHER_ADDR_LEN) == 0) |
| 900 | break; |
| 901 | } |
Simon Kelley | 33820b7 | 2004-04-03 21:10:00 +0100 | [diff] [blame] | 902 | |
| 903 | if (!config) |
| 904 | { |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 905 | if (!(config = whine_malloc(sizeof(struct dhcp_config)))) |
Simon Kelley | 33820b7 | 2004-04-03 21:10:00 +0100 | [diff] [blame] | 906 | continue; |
Simon Kelley | 849a835 | 2006-06-09 21:02:31 +0100 | [diff] [blame] | 907 | config->flags = CONFIG_FROM_ETHERS; |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 908 | config->hwaddr = NULL; |
| 909 | config->domain = NULL; |
Simon Kelley | c52e189 | 2010-06-07 22:01:39 +0100 | [diff] [blame] | 910 | config->netid = NULL; |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 911 | config->next = daemon->dhcp_conf; |
| 912 | daemon->dhcp_conf = config; |
Simon Kelley | 33820b7 | 2004-04-03 21:10:00 +0100 | [diff] [blame] | 913 | } |
| 914 | |
| 915 | config->flags |= flags; |
| 916 | |
| 917 | if (flags & CONFIG_NAME) |
| 918 | { |
Simon Kelley | 1f15b81 | 2009-10-13 17:49:32 +0100 | [diff] [blame] | 919 | config->hostname = host; |
| 920 | host = NULL; |
Simon Kelley | 33820b7 | 2004-04-03 21:10:00 +0100 | [diff] [blame] | 921 | } |
| 922 | |
| 923 | if (flags & CONFIG_ADDR) |
| 924 | config->addr = addr; |
Simon Kelley | 1cff166 | 2004-03-12 08:12:58 +0000 | [diff] [blame] | 925 | } |
Simon Kelley | 33820b7 | 2004-04-03 21:10:00 +0100 | [diff] [blame] | 926 | |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 927 | config->flags |= CONFIG_NOCLID; |
| 928 | if (!config->hwaddr) |
| 929 | config->hwaddr = whine_malloc(sizeof(struct hwaddr_config)); |
| 930 | if (config->hwaddr) |
| 931 | { |
| 932 | memcpy(config->hwaddr->hwaddr, hwaddr, ETHER_ADDR_LEN); |
| 933 | config->hwaddr->hwaddr_len = ETHER_ADDR_LEN; |
| 934 | config->hwaddr->hwaddr_type = ARPHRD_ETHER; |
| 935 | config->hwaddr->wildcard_mask = 0; |
| 936 | config->hwaddr->next = NULL; |
| 937 | } |
Simon Kelley | 33820b7 | 2004-04-03 21:10:00 +0100 | [diff] [blame] | 938 | count++; |
Simon Kelley | 1f15b81 | 2009-10-13 17:49:32 +0100 | [diff] [blame] | 939 | |
| 940 | free(host); |
| 941 | |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 942 | } |
| 943 | |
| 944 | fclose(f); |
Simon Kelley | 33820b7 | 2004-04-03 21:10:00 +0100 | [diff] [blame] | 945 | |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 946 | my_syslog(MS_DHCP | LOG_INFO, _("read %s - %d addresses"), ETHERSFILE, count); |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 947 | } |
| 948 | |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 949 | |
Simon Kelley | bb01cb9 | 2004-12-13 20:56:23 +0000 | [diff] [blame] | 950 | /* If we've not found a hostname any other way, try and see if there's one in /etc/hosts |
| 951 | for this address. If it has a domain part, that must match the set domain and |
Simon Kelley | 1f15b81 | 2009-10-13 17:49:32 +0100 | [diff] [blame] | 952 | it gets stripped. The set of legal domain names is bigger than the set of legal hostnames |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 953 | so check here that the domain name is legal as a hostname. |
| 954 | NOTE: we're only allowed to overwrite daemon->dhcp_buff if we succeed. */ |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 955 | char *host_from_dns(struct in_addr addr) |
Simon Kelley | bb01cb9 | 2004-12-13 20:56:23 +0000 | [diff] [blame] | 956 | { |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 957 | struct crec *lookup; |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 958 | |
| 959 | if (daemon->port == 0) |
| 960 | return NULL; /* DNS disabled. */ |
Simon Kelley | bb01cb9 | 2004-12-13 20:56:23 +0000 | [diff] [blame] | 961 | |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 962 | lookup = cache_find_by_addr(NULL, (struct all_addr *)&addr, 0, F_IPV4); |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 963 | |
Simon Kelley | bb01cb9 | 2004-12-13 20:56:23 +0000 | [diff] [blame] | 964 | if (lookup && (lookup->flags & F_HOSTS)) |
| 965 | { |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 966 | char *dot, *hostname = cache_get_name(lookup); |
| 967 | dot = strchr(hostname, '.'); |
| 968 | |
| 969 | if (dot && strlen(dot+1) != 0) |
| 970 | { |
| 971 | char *d2 = get_domain(addr); |
| 972 | if (!d2 || !hostname_isequal(dot+1, d2)) |
| 973 | return NULL; /* wrong domain */ |
| 974 | } |
| 975 | |
| 976 | if (!legal_hostname(hostname)) |
| 977 | return NULL; |
| 978 | |
| 979 | strncpy(daemon->dhcp_buff, hostname, 256); |
| 980 | daemon->dhcp_buff[255] = 0; |
| 981 | strip_hostname(daemon->dhcp_buff); |
| 982 | |
| 983 | return daemon->dhcp_buff; |
Simon Kelley | bb01cb9 | 2004-12-13 20:56:23 +0000 | [diff] [blame] | 984 | } |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 985 | |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 986 | return NULL; |
Simon Kelley | bb01cb9 | 2004-12-13 20:56:23 +0000 | [diff] [blame] | 987 | } |
| 988 | |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 989 | #endif |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 990 | |