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