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