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