Simon Kelley | d1ced3a | 2018-01-01 22:18:03 +0000 | [diff] [blame] | 1 | /* dnsmasq is Copyright (c) 2000-2018 Simon Kelley |
Simon Kelley | 11867dc | 2015-12-23 16:15:58 +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, or |
| 6 | (at your option) version 3 dated 29 June, 2007. |
| 7 | |
| 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. |
| 12 | |
| 13 | You should have received a copy of the GNU General Public License |
| 14 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 15 | */ |
| 16 | |
| 17 | #include "dnsmasq.h" |
| 18 | |
Simon Kelley | 33702ab | 2015-12-28 23:17:15 +0000 | [diff] [blame] | 19 | /* Time between forced re-loads from kernel. */ |
| 20 | #define INTERVAL 90 |
| 21 | |
| 22 | #define ARP_MARK 0 |
| 23 | #define ARP_FOUND 1 /* Confirmed */ |
| 24 | #define ARP_NEW 2 /* Newly created */ |
| 25 | #define ARP_EMPTY 3 /* No MAC addr */ |
Simon Kelley | 11867dc | 2015-12-23 16:15:58 +0000 | [diff] [blame] | 26 | |
| 27 | struct arp_record { |
Simon Kelley | 33702ab | 2015-12-28 23:17:15 +0000 | [diff] [blame] | 28 | unsigned short hwlen, status; |
Simon Kelley | 11867dc | 2015-12-23 16:15:58 +0000 | [diff] [blame] | 29 | int family; |
| 30 | unsigned char hwaddr[DHCP_CHADDR_MAX]; |
| 31 | struct all_addr addr; |
| 32 | struct arp_record *next; |
| 33 | }; |
| 34 | |
Simon Kelley | 33702ab | 2015-12-28 23:17:15 +0000 | [diff] [blame] | 35 | static struct arp_record *arps = NULL, *old = NULL, *freelist = NULL; |
| 36 | static time_t last = 0; |
Simon Kelley | 11867dc | 2015-12-23 16:15:58 +0000 | [diff] [blame] | 37 | |
| 38 | static int filter_mac(int family, char *addrp, char *mac, size_t maclen, void *parmv) |
| 39 | { |
Simon Kelley | 11867dc | 2015-12-23 16:15:58 +0000 | [diff] [blame] | 40 | struct arp_record *arp; |
| 41 | |
Simon Kelley | 33702ab | 2015-12-28 23:17:15 +0000 | [diff] [blame] | 42 | (void)parmv; |
| 43 | |
Simon Kelley | 11867dc | 2015-12-23 16:15:58 +0000 | [diff] [blame] | 44 | if (maclen > DHCP_CHADDR_MAX) |
| 45 | return 1; |
| 46 | |
Simon Kelley | 1566bac | 2016-02-05 14:38:06 +0000 | [diff] [blame] | 47 | #ifndef HAVE_IPV6 |
| 48 | if (family != AF_INET) |
| 49 | return 1; |
| 50 | #endif |
| 51 | |
Simon Kelley | 11867dc | 2015-12-23 16:15:58 +0000 | [diff] [blame] | 52 | /* Look for existing entry */ |
| 53 | for (arp = arps; arp; arp = arp->next) |
| 54 | { |
| 55 | if (family != arp->family || arp->status == ARP_NEW) |
| 56 | continue; |
| 57 | |
| 58 | if (family == AF_INET) |
| 59 | { |
| 60 | if (arp->addr.addr.addr4.s_addr != ((struct in_addr *)addrp)->s_addr) |
| 61 | continue; |
| 62 | } |
| 63 | #ifdef HAVE_IPV6 |
| 64 | else |
| 65 | { |
| 66 | if (!IN6_ARE_ADDR_EQUAL(&arp->addr.addr.addr6, (struct in6_addr *)addrp)) |
| 67 | continue; |
| 68 | } |
| 69 | #endif |
| 70 | |
Simon Kelley | 33702ab | 2015-12-28 23:17:15 +0000 | [diff] [blame] | 71 | if (arp->status == ARP_EMPTY) |
Simon Kelley | 11867dc | 2015-12-23 16:15:58 +0000 | [diff] [blame] | 72 | { |
Simon Kelley | 33702ab | 2015-12-28 23:17:15 +0000 | [diff] [blame] | 73 | /* existing address, was negative. */ |
Simon Kelley | 11867dc | 2015-12-23 16:15:58 +0000 | [diff] [blame] | 74 | arp->status = ARP_NEW; |
| 75 | arp->hwlen = maclen; |
Simon Kelley | 11867dc | 2015-12-23 16:15:58 +0000 | [diff] [blame] | 76 | memcpy(arp->hwaddr, mac, maclen); |
| 77 | } |
Simon Kelley | 33702ab | 2015-12-28 23:17:15 +0000 | [diff] [blame] | 78 | else if (arp->hwlen == maclen && memcmp(arp->hwaddr, mac, maclen) == 0) |
| 79 | /* Existing entry matches - confirm. */ |
| 80 | arp->status = ARP_FOUND; |
| 81 | else |
| 82 | continue; |
Simon Kelley | 11867dc | 2015-12-23 16:15:58 +0000 | [diff] [blame] | 83 | |
| 84 | break; |
| 85 | } |
| 86 | |
| 87 | if (!arp) |
| 88 | { |
| 89 | /* New entry */ |
Simon Kelley | 33702ab | 2015-12-28 23:17:15 +0000 | [diff] [blame] | 90 | if (freelist) |
Simon Kelley | 11867dc | 2015-12-23 16:15:58 +0000 | [diff] [blame] | 91 | { |
Simon Kelley | 33702ab | 2015-12-28 23:17:15 +0000 | [diff] [blame] | 92 | arp = freelist; |
| 93 | freelist = freelist->next; |
Simon Kelley | 11867dc | 2015-12-23 16:15:58 +0000 | [diff] [blame] | 94 | } |
| 95 | else if (!(arp = whine_malloc(sizeof(struct arp_record)))) |
| 96 | return 1; |
| 97 | |
| 98 | arp->next = arps; |
| 99 | arps = arp; |
| 100 | arp->status = ARP_NEW; |
| 101 | arp->hwlen = maclen; |
| 102 | arp->family = family; |
| 103 | memcpy(arp->hwaddr, mac, maclen); |
| 104 | if (family == AF_INET) |
| 105 | arp->addr.addr.addr4.s_addr = ((struct in_addr *)addrp)->s_addr; |
| 106 | #ifdef HAVE_IPV6 |
| 107 | else |
| 108 | memcpy(&arp->addr.addr.addr6, addrp, IN6ADDRSZ); |
| 109 | #endif |
| 110 | } |
| 111 | |
| 112 | return 1; |
| 113 | } |
| 114 | |
| 115 | /* If in lazy mode, we cache absence of ARP entries. */ |
Simon Kelley | 33702ab | 2015-12-28 23:17:15 +0000 | [diff] [blame] | 116 | int find_mac(union mysockaddr *addr, unsigned char *mac, int lazy, time_t now) |
Simon Kelley | 11867dc | 2015-12-23 16:15:58 +0000 | [diff] [blame] | 117 | { |
Simon Kelley | d917275 | 2016-01-04 17:17:41 +0000 | [diff] [blame] | 118 | struct arp_record *arp, *tmp, **up; |
Simon Kelley | 11867dc | 2015-12-23 16:15:58 +0000 | [diff] [blame] | 119 | int updated = 0; |
| 120 | |
| 121 | again: |
| 122 | |
Simon Kelley | 33702ab | 2015-12-28 23:17:15 +0000 | [diff] [blame] | 123 | /* If the database is less then INTERVAL old, look in there */ |
| 124 | if (difftime(now, last) < INTERVAL) |
Simon Kelley | 1e50512 | 2016-01-25 21:29:23 +0000 | [diff] [blame] | 125 | { |
| 126 | /* addr == NULL -> just make cache up-to-date */ |
| 127 | if (!addr) |
| 128 | return 0; |
| 129 | |
| 130 | for (arp = arps; arp; arp = arp->next) |
| 131 | { |
Simon Kelley | 2c0c36f | 2016-05-01 20:57:08 +0100 | [diff] [blame] | 132 | if (addr->sa.sa_family != arp->family) |
| 133 | continue; |
| 134 | |
| 135 | if (arp->family == AF_INET && |
| 136 | arp->addr.addr.addr4.s_addr != addr->in.sin_addr.s_addr) |
| 137 | continue; |
| 138 | |
Simon Kelley | 11867dc | 2015-12-23 16:15:58 +0000 | [diff] [blame] | 139 | #ifdef HAVE_IPV6 |
Simon Kelley | 2c0c36f | 2016-05-01 20:57:08 +0100 | [diff] [blame] | 140 | if (arp->family == AF_INET6 && |
| 141 | !IN6_ARE_ADDR_EQUAL(&arp->addr.addr.addr6, &addr->in6.sin6_addr)) |
| 142 | continue; |
Simon Kelley | 11867dc | 2015-12-23 16:15:58 +0000 | [diff] [blame] | 143 | #endif |
Simon Kelley | 1e50512 | 2016-01-25 21:29:23 +0000 | [diff] [blame] | 144 | |
| 145 | /* Only accept positive entries unless in lazy mode. */ |
| 146 | if (arp->status != ARP_EMPTY || lazy || updated) |
| 147 | { |
| 148 | if (mac && arp->hwlen != 0) |
| 149 | memcpy(mac, arp->hwaddr, arp->hwlen); |
| 150 | return arp->hwlen; |
| 151 | } |
| 152 | } |
| 153 | } |
| 154 | |
Simon Kelley | 11867dc | 2015-12-23 16:15:58 +0000 | [diff] [blame] | 155 | /* Not found, try the kernel */ |
| 156 | if (!updated) |
| 157 | { |
| 158 | updated = 1; |
Simon Kelley | 33702ab | 2015-12-28 23:17:15 +0000 | [diff] [blame] | 159 | last = now; |
| 160 | |
Simon Kelley | 11867dc | 2015-12-23 16:15:58 +0000 | [diff] [blame] | 161 | /* Mark all non-negative entries */ |
Simon Kelley | bb58f63 | 2016-01-14 19:23:10 +0000 | [diff] [blame] | 162 | for (arp = arps; arp; arp = arp->next) |
Simon Kelley | 11867dc | 2015-12-23 16:15:58 +0000 | [diff] [blame] | 163 | if (arp->status != ARP_EMPTY) |
Simon Kelley | 33702ab | 2015-12-28 23:17:15 +0000 | [diff] [blame] | 164 | arp->status = ARP_MARK; |
Simon Kelley | 11867dc | 2015-12-23 16:15:58 +0000 | [diff] [blame] | 165 | |
| 166 | iface_enumerate(AF_UNSPEC, NULL, filter_mac); |
| 167 | |
Simon Kelley | 33702ab | 2015-12-28 23:17:15 +0000 | [diff] [blame] | 168 | /* Remove all unconfirmed entries to old list. */ |
Simon Kelley | d917275 | 2016-01-04 17:17:41 +0000 | [diff] [blame] | 169 | for (arp = arps, up = &arps; arp; arp = tmp) |
| 170 | { |
| 171 | tmp = arp->next; |
Simon Kelley | 33702ab | 2015-12-28 23:17:15 +0000 | [diff] [blame] | 172 | |
Simon Kelley | d917275 | 2016-01-04 17:17:41 +0000 | [diff] [blame] | 173 | if (arp->status == ARP_MARK) |
| 174 | { |
| 175 | *up = arp->next; |
| 176 | arp->next = old; |
| 177 | old = arp; |
| 178 | } |
| 179 | else |
| 180 | up = &arp->next; |
| 181 | } |
| 182 | |
Simon Kelley | 11867dc | 2015-12-23 16:15:58 +0000 | [diff] [blame] | 183 | goto again; |
| 184 | } |
| 185 | |
| 186 | /* record failure, so we don't consult the kernel each time |
| 187 | we're asked for this address */ |
Simon Kelley | 33702ab | 2015-12-28 23:17:15 +0000 | [diff] [blame] | 188 | if (freelist) |
Simon Kelley | 11867dc | 2015-12-23 16:15:58 +0000 | [diff] [blame] | 189 | { |
Simon Kelley | 33702ab | 2015-12-28 23:17:15 +0000 | [diff] [blame] | 190 | arp = freelist; |
| 191 | freelist = freelist->next; |
Simon Kelley | 11867dc | 2015-12-23 16:15:58 +0000 | [diff] [blame] | 192 | } |
| 193 | else |
| 194 | arp = whine_malloc(sizeof(struct arp_record)); |
| 195 | |
| 196 | if (arp) |
| 197 | { |
| 198 | arp->next = arps; |
| 199 | arps = arp; |
| 200 | arp->status = ARP_EMPTY; |
| 201 | arp->family = addr->sa.sa_family; |
Simon Kelley | f4d0c66 | 2016-01-18 12:51:08 +0000 | [diff] [blame] | 202 | arp->hwlen = 0; |
| 203 | |
Simon Kelley | 11867dc | 2015-12-23 16:15:58 +0000 | [diff] [blame] | 204 | if (addr->sa.sa_family == AF_INET) |
| 205 | arp->addr.addr.addr4.s_addr = addr->in.sin_addr.s_addr; |
| 206 | #ifdef HAVE_IPV6 |
| 207 | else |
| 208 | memcpy(&arp->addr.addr.addr6, &addr->in6.sin6_addr, IN6ADDRSZ); |
| 209 | #endif |
| 210 | } |
| 211 | |
| 212 | return 0; |
| 213 | } |
| 214 | |
Simon Kelley | 33702ab | 2015-12-28 23:17:15 +0000 | [diff] [blame] | 215 | int do_arp_script_run(void) |
| 216 | { |
| 217 | struct arp_record *arp; |
| 218 | |
| 219 | /* Notify any which went, then move to free list */ |
| 220 | if (old) |
| 221 | { |
| 222 | #ifdef HAVE_SCRIPT |
Simon Kelley | 1e50512 | 2016-01-25 21:29:23 +0000 | [diff] [blame] | 223 | if (option_bool(OPT_SCRIPT_ARP)) |
Simon Kelley | e6e751b | 2016-02-01 17:59:07 +0000 | [diff] [blame] | 224 | queue_arp(ACTION_ARP_DEL, old->hwaddr, old->hwlen, old->family, &old->addr); |
Simon Kelley | 33702ab | 2015-12-28 23:17:15 +0000 | [diff] [blame] | 225 | #endif |
| 226 | arp = old; |
| 227 | old = arp->next; |
| 228 | arp->next = freelist; |
| 229 | freelist = arp; |
| 230 | return 1; |
| 231 | } |
| 232 | |
| 233 | for (arp = arps; arp; arp = arp->next) |
| 234 | if (arp->status == ARP_NEW) |
| 235 | { |
| 236 | #ifdef HAVE_SCRIPT |
Simon Kelley | 1e50512 | 2016-01-25 21:29:23 +0000 | [diff] [blame] | 237 | if (option_bool(OPT_SCRIPT_ARP)) |
Simon Kelley | 33702ab | 2015-12-28 23:17:15 +0000 | [diff] [blame] | 238 | queue_arp(ACTION_ARP, arp->hwaddr, arp->hwlen, arp->family, &arp->addr); |
| 239 | #endif |
| 240 | arp->status = ARP_FOUND; |
| 241 | return 1; |
| 242 | } |
| 243 | |
| 244 | return 0; |
| 245 | } |
| 246 | |
Simon Kelley | 11867dc | 2015-12-23 16:15:58 +0000 | [diff] [blame] | 247 | |