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