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