Simon Kelley | 2a8710a | 2020-01-05 16:40:06 +0000 | [diff] [blame] | 1 | /* dnsmasq is Copyright (c) 2000-2020 Simon Kelley |
Simon Kelley | 1d03016 | 2015-12-21 14:17:06 +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 | 5bb88f0 | 2015-12-21 16:23:47 +0000 | [diff] [blame] | 19 | unsigned char *find_pseudoheader(struct dns_header *header, size_t plen, size_t *len, unsigned char **p, int *is_sign, int *is_last) |
Simon Kelley | 1d03016 | 2015-12-21 14:17:06 +0000 | [diff] [blame] | 20 | { |
| 21 | /* See if packet has an RFC2671 pseudoheader, and if so return a pointer to it. |
| 22 | also return length of pseudoheader in *len and pointer to the UDP size in *p |
| 23 | Finally, check to see if a packet is signed. If it is we cannot change a single bit before |
Simon Kelley | 5bb88f0 | 2015-12-21 16:23:47 +0000 | [diff] [blame] | 24 | forwarding. We look for TSIG in the addition section, and TKEY queries (for GSS-TSIG) */ |
Simon Kelley | 1d03016 | 2015-12-21 14:17:06 +0000 | [diff] [blame] | 25 | |
| 26 | int i, arcount = ntohs(header->arcount); |
| 27 | unsigned char *ansp = (unsigned char *)(header+1); |
| 28 | unsigned short rdlen, type, class; |
| 29 | unsigned char *ret = NULL; |
| 30 | |
| 31 | if (is_sign) |
| 32 | { |
| 33 | *is_sign = 0; |
| 34 | |
| 35 | if (OPCODE(header) == QUERY) |
| 36 | { |
| 37 | for (i = ntohs(header->qdcount); i != 0; i--) |
| 38 | { |
| 39 | if (!(ansp = skip_name(ansp, header, plen, 4))) |
| 40 | return NULL; |
| 41 | |
| 42 | GETSHORT(type, ansp); |
| 43 | GETSHORT(class, ansp); |
| 44 | |
| 45 | if (class == C_IN && type == T_TKEY) |
| 46 | *is_sign = 1; |
| 47 | } |
| 48 | } |
| 49 | } |
| 50 | else |
| 51 | { |
| 52 | if (!(ansp = skip_questions(header, plen))) |
| 53 | return NULL; |
| 54 | } |
| 55 | |
| 56 | if (arcount == 0) |
| 57 | return NULL; |
| 58 | |
| 59 | if (!(ansp = skip_section(ansp, ntohs(header->ancount) + ntohs(header->nscount), header, plen))) |
| 60 | return NULL; |
| 61 | |
| 62 | for (i = 0; i < arcount; i++) |
| 63 | { |
| 64 | unsigned char *save, *start = ansp; |
| 65 | if (!(ansp = skip_name(ansp, header, plen, 10))) |
| 66 | return NULL; |
| 67 | |
| 68 | GETSHORT(type, ansp); |
| 69 | save = ansp; |
| 70 | GETSHORT(class, ansp); |
| 71 | ansp += 4; /* TTL */ |
| 72 | GETSHORT(rdlen, ansp); |
| 73 | if (!ADD_RDLEN(header, ansp, plen, rdlen)) |
| 74 | return NULL; |
| 75 | if (type == T_OPT) |
| 76 | { |
| 77 | if (len) |
| 78 | *len = ansp - start; |
Simon Kelley | 5bb88f0 | 2015-12-21 16:23:47 +0000 | [diff] [blame] | 79 | |
Simon Kelley | 1d03016 | 2015-12-21 14:17:06 +0000 | [diff] [blame] | 80 | if (p) |
| 81 | *p = save; |
Simon Kelley | 5bb88f0 | 2015-12-21 16:23:47 +0000 | [diff] [blame] | 82 | |
| 83 | if (is_last) |
| 84 | *is_last = (i == arcount-1); |
| 85 | |
Simon Kelley | 1d03016 | 2015-12-21 14:17:06 +0000 | [diff] [blame] | 86 | ret = start; |
| 87 | } |
| 88 | else if (is_sign && |
| 89 | i == arcount - 1 && |
| 90 | class == C_ANY && |
| 91 | type == T_TSIG) |
| 92 | *is_sign = 1; |
| 93 | } |
| 94 | |
| 95 | return ret; |
| 96 | } |
Simon Kelley | 1d03016 | 2015-12-21 14:17:06 +0000 | [diff] [blame] | 97 | |
Simon Kelley | 8798585 | 2016-04-25 15:33:30 +0100 | [diff] [blame] | 98 | |
| 99 | /* replace == 2 ->delete existing option only. */ |
Simon Kelley | 1d03016 | 2015-12-21 14:17:06 +0000 | [diff] [blame] | 100 | size_t add_pseudoheader(struct dns_header *header, size_t plen, unsigned char *limit, |
Simon Kelley | c7f3bd2 | 2016-02-28 21:48:34 +0000 | [diff] [blame] | 101 | unsigned short udp_sz, int optno, unsigned char *opt, size_t optlen, int set_do, int replace) |
Simon Kelley | 1d03016 | 2015-12-21 14:17:06 +0000 | [diff] [blame] | 102 | { |
Simon Kelley | 5bb88f0 | 2015-12-21 16:23:47 +0000 | [diff] [blame] | 103 | unsigned char *lenp, *datap, *p, *udp_len, *buff = NULL; |
| 104 | int rdlen = 0, is_sign, is_last; |
| 105 | unsigned short flags = set_do ? 0x8000 : 0, rcode = 0; |
Simon Kelley | 1d03016 | 2015-12-21 14:17:06 +0000 | [diff] [blame] | 106 | |
Simon Kelley | 5bb88f0 | 2015-12-21 16:23:47 +0000 | [diff] [blame] | 107 | p = find_pseudoheader(header, plen, NULL, &udp_len, &is_sign, &is_last); |
| 108 | |
| 109 | if (is_sign) |
| 110 | return plen; |
| 111 | |
| 112 | if (p) |
Simon Kelley | 1d03016 | 2015-12-21 14:17:06 +0000 | [diff] [blame] | 113 | { |
Simon Kelley | 5bb88f0 | 2015-12-21 16:23:47 +0000 | [diff] [blame] | 114 | /* Existing header */ |
Simon Kelley | 1d03016 | 2015-12-21 14:17:06 +0000 | [diff] [blame] | 115 | int i; |
Simon Kelley | 5bb88f0 | 2015-12-21 16:23:47 +0000 | [diff] [blame] | 116 | unsigned short code, len; |
| 117 | |
| 118 | p = udp_len; |
| 119 | GETSHORT(udp_sz, p); |
| 120 | GETSHORT(rcode, p); |
Simon Kelley | 1d03016 | 2015-12-21 14:17:06 +0000 | [diff] [blame] | 121 | GETSHORT(flags, p); |
Simon Kelley | 5bb88f0 | 2015-12-21 16:23:47 +0000 | [diff] [blame] | 122 | |
Simon Kelley | 1d03016 | 2015-12-21 14:17:06 +0000 | [diff] [blame] | 123 | if (set_do) |
| 124 | { |
Simon Kelley | c7f3bd2 | 2016-02-28 21:48:34 +0000 | [diff] [blame] | 125 | p -= 2; |
Simon Kelley | 5bb88f0 | 2015-12-21 16:23:47 +0000 | [diff] [blame] | 126 | flags |= 0x8000; |
| 127 | PUTSHORT(flags, p); |
Simon Kelley | 1d03016 | 2015-12-21 14:17:06 +0000 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | lenp = p; |
| 131 | GETSHORT(rdlen, p); |
| 132 | if (!CHECK_LEN(header, p, plen, rdlen)) |
| 133 | return plen; /* bad packet */ |
| 134 | datap = p; |
| 135 | |
| 136 | /* no option to add */ |
| 137 | if (optno == 0) |
| 138 | return plen; |
| 139 | |
| 140 | /* check if option already there */ |
Simon Kelley | c7f3bd2 | 2016-02-28 21:48:34 +0000 | [diff] [blame] | 141 | for (i = 0; i + 4 < rdlen;) |
Simon Kelley | 1d03016 | 2015-12-21 14:17:06 +0000 | [diff] [blame] | 142 | { |
| 143 | GETSHORT(code, p); |
| 144 | GETSHORT(len, p); |
Simon Kelley | c7f3bd2 | 2016-02-28 21:48:34 +0000 | [diff] [blame] | 145 | |
| 146 | /* malformed option, delete the whole OPT RR and start again. */ |
Simon Kelley | 897c113 | 2017-09-25 20:11:58 +0100 | [diff] [blame] | 147 | if (i + 4 + len > rdlen) |
Simon Kelley | c7f3bd2 | 2016-02-28 21:48:34 +0000 | [diff] [blame] | 148 | { |
| 149 | rdlen = 0; |
Simon Kelley | aa300f7 | 2016-03-01 15:19:13 +0000 | [diff] [blame] | 150 | is_last = 0; |
Simon Kelley | c7f3bd2 | 2016-02-28 21:48:34 +0000 | [diff] [blame] | 151 | break; |
| 152 | } |
| 153 | |
Simon Kelley | 1d03016 | 2015-12-21 14:17:06 +0000 | [diff] [blame] | 154 | if (code == optno) |
Simon Kelley | c7f3bd2 | 2016-02-28 21:48:34 +0000 | [diff] [blame] | 155 | { |
Simon Kelley | 8798585 | 2016-04-25 15:33:30 +0100 | [diff] [blame] | 156 | if (replace == 0) |
Simon Kelley | c7f3bd2 | 2016-02-28 21:48:34 +0000 | [diff] [blame] | 157 | return plen; |
| 158 | |
| 159 | /* delete option if we're to replace it. */ |
| 160 | p -= 4; |
| 161 | rdlen -= len + 4; |
Simon Kelley | 6a0b00f | 2017-09-25 20:19:55 +0100 | [diff] [blame] | 162 | memmove(p, p+len+4, rdlen - i); |
Simon Kelley | c7f3bd2 | 2016-02-28 21:48:34 +0000 | [diff] [blame] | 163 | PUTSHORT(rdlen, lenp); |
| 164 | lenp -= 2; |
| 165 | } |
| 166 | else |
| 167 | { |
| 168 | p += len; |
| 169 | i += len + 4; |
| 170 | } |
Simon Kelley | 1d03016 | 2015-12-21 14:17:06 +0000 | [diff] [blame] | 171 | } |
Simon Kelley | 5bb88f0 | 2015-12-21 16:23:47 +0000 | [diff] [blame] | 172 | |
| 173 | /* If we're going to extend the RR, it has to be the last RR in the packet */ |
| 174 | if (!is_last) |
| 175 | { |
| 176 | /* First, take a copy of the options. */ |
| 177 | if (rdlen != 0 && (buff = whine_malloc(rdlen))) |
| 178 | memcpy(buff, datap, rdlen); |
| 179 | |
| 180 | /* now, delete OPT RR */ |
| 181 | plen = rrfilter(header, plen, 0); |
| 182 | |
| 183 | /* Now, force addition of a new one */ |
| 184 | p = NULL; |
| 185 | } |
Simon Kelley | 1d03016 | 2015-12-21 14:17:06 +0000 | [diff] [blame] | 186 | } |
| 187 | |
Simon Kelley | 5bb88f0 | 2015-12-21 16:23:47 +0000 | [diff] [blame] | 188 | if (!p) |
| 189 | { |
| 190 | /* We are (re)adding the pseudoheader */ |
| 191 | if (!(p = skip_questions(header, plen)) || |
| 192 | !(p = skip_section(p, |
| 193 | ntohs(header->ancount) + ntohs(header->nscount) + ntohs(header->arcount), |
| 194 | header, plen))) |
Simon Kelley | 51eadb6 | 2017-09-25 20:16:50 +0100 | [diff] [blame] | 195 | { |
| 196 | free(buff); |
Simon Kelley | 5bb88f0 | 2015-12-21 16:23:47 +0000 | [diff] [blame] | 197 | return plen; |
Simon Kelley | 51eadb6 | 2017-09-25 20:16:50 +0100 | [diff] [blame] | 198 | } |
Simon Kelley | 897c113 | 2017-09-25 20:11:58 +0100 | [diff] [blame] | 199 | if (p + 11 > limit) |
Simon Kelley | 51eadb6 | 2017-09-25 20:16:50 +0100 | [diff] [blame] | 200 | { |
| 201 | free(buff); |
| 202 | return plen; /* Too big */ |
| 203 | } |
Simon Kelley | 5bb88f0 | 2015-12-21 16:23:47 +0000 | [diff] [blame] | 204 | *p++ = 0; /* empty name */ |
| 205 | PUTSHORT(T_OPT, p); |
| 206 | PUTSHORT(udp_sz, p); /* max packet length, 512 if not given in EDNS0 header */ |
| 207 | PUTSHORT(rcode, p); /* extended RCODE and version */ |
| 208 | PUTSHORT(flags, p); /* DO flag */ |
| 209 | lenp = p; |
| 210 | PUTSHORT(rdlen, p); /* RDLEN */ |
| 211 | datap = p; |
| 212 | /* Copy back any options */ |
| 213 | if (buff) |
| 214 | { |
Simon Kelley | 897c113 | 2017-09-25 20:11:58 +0100 | [diff] [blame] | 215 | if (p + rdlen > limit) |
| 216 | { |
| 217 | free(buff); |
| 218 | return plen; /* Too big */ |
| 219 | } |
Simon Kelley | 5bb88f0 | 2015-12-21 16:23:47 +0000 | [diff] [blame] | 220 | memcpy(p, buff, rdlen); |
| 221 | free(buff); |
| 222 | p += rdlen; |
| 223 | } |
Simon Kelley | a3303e1 | 2017-09-07 20:45:00 +0100 | [diff] [blame] | 224 | |
| 225 | /* Only bump arcount if RR is going to fit */ |
| 226 | if (((ssize_t)optlen) <= (limit - (p + 4))) |
| 227 | header->arcount = htons(ntohs(header->arcount) + 1); |
Simon Kelley | 5bb88f0 | 2015-12-21 16:23:47 +0000 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | if (((ssize_t)optlen) > (limit - (p + 4))) |
| 231 | return plen; /* Too big */ |
| 232 | |
| 233 | /* Add new option */ |
Simon Kelley | 8798585 | 2016-04-25 15:33:30 +0100 | [diff] [blame] | 234 | if (optno != 0 && replace != 2) |
Simon Kelley | 1d03016 | 2015-12-21 14:17:06 +0000 | [diff] [blame] | 235 | { |
Simon Kelley | 897c113 | 2017-09-25 20:11:58 +0100 | [diff] [blame] | 236 | if (p + 4 > limit) |
| 237 | return plen; /* Too big */ |
Simon Kelley | 1d03016 | 2015-12-21 14:17:06 +0000 | [diff] [blame] | 238 | PUTSHORT(optno, p); |
| 239 | PUTSHORT(optlen, p); |
Simon Kelley | 897c113 | 2017-09-25 20:11:58 +0100 | [diff] [blame] | 240 | if (p + optlen > limit) |
| 241 | return plen; /* Too big */ |
Simon Kelley | 1d03016 | 2015-12-21 14:17:06 +0000 | [diff] [blame] | 242 | memcpy(p, opt, optlen); |
| 243 | p += optlen; |
Simon Kelley | 5bb88f0 | 2015-12-21 16:23:47 +0000 | [diff] [blame] | 244 | PUTSHORT(p - datap, lenp); |
Simon Kelley | 1d03016 | 2015-12-21 14:17:06 +0000 | [diff] [blame] | 245 | } |
Simon Kelley | 1d03016 | 2015-12-21 14:17:06 +0000 | [diff] [blame] | 246 | return p - (unsigned char *)header; |
Simon Kelley | 1d03016 | 2015-12-21 14:17:06 +0000 | [diff] [blame] | 247 | } |
| 248 | |
Simon Kelley | 33702ab | 2015-12-28 23:17:15 +0000 | [diff] [blame] | 249 | size_t add_do_bit(struct dns_header *header, size_t plen, unsigned char *limit) |
Simon Kelley | efef497 | 2015-12-21 17:30:44 +0000 | [diff] [blame] | 250 | { |
Simon Kelley | c7f3bd2 | 2016-02-28 21:48:34 +0000 | [diff] [blame] | 251 | return add_pseudoheader(header, plen, (unsigned char *)limit, PACKETSZ, 0, NULL, 0, 1, 0); |
Simon Kelley | efef497 | 2015-12-21 17:30:44 +0000 | [diff] [blame] | 252 | } |
| 253 | |
Simon Kelley | 33702ab | 2015-12-28 23:17:15 +0000 | [diff] [blame] | 254 | static unsigned char char64(unsigned char c) |
| 255 | { |
| 256 | return "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[c & 0x3f]; |
| 257 | } |
| 258 | |
| 259 | static void encoder(unsigned char *in, char *out) |
| 260 | { |
| 261 | out[0] = char64(in[0]>>2); |
| 262 | out[1] = char64((in[0]<<4) | (in[1]>>4)); |
| 263 | out[2] = char64((in[1]<<2) | (in[2]>>6)); |
| 264 | out[3] = char64(in[2]); |
| 265 | } |
| 266 | |
Simon Kelley | 25e63f1 | 2020-11-25 21:17:52 +0000 | [diff] [blame] | 267 | static size_t add_dns_client(struct dns_header *header, size_t plen, unsigned char *limit, |
| 268 | union mysockaddr *l3, time_t now, int *cacheablep) |
Simon Kelley | 33702ab | 2015-12-28 23:17:15 +0000 | [diff] [blame] | 269 | { |
Simon Kelley | 8798585 | 2016-04-25 15:33:30 +0100 | [diff] [blame] | 270 | int maclen, replace = 2; /* can't get mac address, just delete any incoming. */ |
Simon Kelley | 33702ab | 2015-12-28 23:17:15 +0000 | [diff] [blame] | 271 | unsigned char mac[DHCP_CHADDR_MAX]; |
Simon Kelley | 9e4cf47 | 2016-02-17 20:26:32 +0000 | [diff] [blame] | 272 | char encode[18]; /* handle 6 byte MACs */ |
Simon Kelley | 33702ab | 2015-12-28 23:17:15 +0000 | [diff] [blame] | 273 | |
| 274 | if ((maclen = find_mac(l3, mac, 1, now)) == 6) |
| 275 | { |
Simon Kelley | 8798585 | 2016-04-25 15:33:30 +0100 | [diff] [blame] | 276 | replace = 1; |
Simon Kelley | 25e63f1 | 2020-11-25 21:17:52 +0000 | [diff] [blame] | 277 | *cacheablep = 0; |
Simon Kelley | 8798585 | 2016-04-25 15:33:30 +0100 | [diff] [blame] | 278 | |
Simon Kelley | 9e4cf47 | 2016-02-17 20:26:32 +0000 | [diff] [blame] | 279 | if (option_bool(OPT_MAC_HEX)) |
| 280 | print_mac(encode, mac, maclen); |
| 281 | else |
| 282 | { |
| 283 | encoder(mac, encode); |
| 284 | encoder(mac+3, encode+4); |
| 285 | encode[8] = 0; |
| 286 | } |
Simon Kelley | 33702ab | 2015-12-28 23:17:15 +0000 | [diff] [blame] | 287 | } |
| 288 | |
Simon Kelley | 8798585 | 2016-04-25 15:33:30 +0100 | [diff] [blame] | 289 | return add_pseudoheader(header, plen, limit, PACKETSZ, EDNS0_OPTION_NOMDEVICEID, (unsigned char *)encode, strlen(encode), 0, replace); |
Simon Kelley | 33702ab | 2015-12-28 23:17:15 +0000 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | |
Simon Kelley | 25e63f1 | 2020-11-25 21:17:52 +0000 | [diff] [blame] | 293 | static size_t add_mac(struct dns_header *header, size_t plen, unsigned char *limit, |
| 294 | union mysockaddr *l3, time_t now, int *cacheablep) |
Simon Kelley | 1d03016 | 2015-12-21 14:17:06 +0000 | [diff] [blame] | 295 | { |
Simon Kelley | 11867dc | 2015-12-23 16:15:58 +0000 | [diff] [blame] | 296 | int maclen; |
| 297 | unsigned char mac[DHCP_CHADDR_MAX]; |
Simon Kelley | 1d03016 | 2015-12-21 14:17:06 +0000 | [diff] [blame] | 298 | |
Simon Kelley | 33702ab | 2015-12-28 23:17:15 +0000 | [diff] [blame] | 299 | if ((maclen = find_mac(l3, mac, 1, now)) != 0) |
Simon Kelley | 25e63f1 | 2020-11-25 21:17:52 +0000 | [diff] [blame] | 300 | { |
| 301 | *cacheablep = 0; |
| 302 | plen = add_pseudoheader(header, plen, limit, PACKETSZ, EDNS0_OPTION_MAC, mac, maclen, 0, 0); |
| 303 | } |
| 304 | |
Simon Kelley | 11867dc | 2015-12-23 16:15:58 +0000 | [diff] [blame] | 305 | return plen; |
Simon Kelley | 1d03016 | 2015-12-21 14:17:06 +0000 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | struct subnet_opt { |
| 309 | u16 family; |
Simon Kelley | ee87504 | 2018-10-23 22:10:17 +0100 | [diff] [blame] | 310 | u8 source_netmask, scope_netmask; |
Simon Kelley | 1d03016 | 2015-12-21 14:17:06 +0000 | [diff] [blame] | 311 | u8 addr[IN6ADDRSZ]; |
Simon Kelley | 1d03016 | 2015-12-21 14:17:06 +0000 | [diff] [blame] | 312 | }; |
| 313 | |
| 314 | static void *get_addrp(union mysockaddr *addr, const short family) |
| 315 | { |
Simon Kelley | 1d03016 | 2015-12-21 14:17:06 +0000 | [diff] [blame] | 316 | if (family == AF_INET6) |
| 317 | return &addr->in6.sin6_addr; |
Simon Kelley | 1d03016 | 2015-12-21 14:17:06 +0000 | [diff] [blame] | 318 | |
| 319 | return &addr->in.sin_addr; |
| 320 | } |
| 321 | |
Simon Kelley | 25e63f1 | 2020-11-25 21:17:52 +0000 | [diff] [blame] | 322 | static size_t calc_subnet_opt(struct subnet_opt *opt, union mysockaddr *source, int *cacheablep) |
Simon Kelley | 1d03016 | 2015-12-21 14:17:06 +0000 | [diff] [blame] | 323 | { |
| 324 | /* http://tools.ietf.org/html/draft-vandergaast-edns-client-subnet-02 */ |
| 325 | |
| 326 | int len; |
Simon Kelley | 0fbd980 | 2017-05-21 22:24:43 +0100 | [diff] [blame] | 327 | void *addrp = NULL; |
Simon Kelley | 1d03016 | 2015-12-21 14:17:06 +0000 | [diff] [blame] | 328 | int sa_family = source->sa.sa_family; |
Simon Kelley | 25e63f1 | 2020-11-25 21:17:52 +0000 | [diff] [blame] | 329 | int cacheable = 0; |
| 330 | |
Simon Kelley | 22fe2fd | 2016-02-28 17:07:10 +0000 | [diff] [blame] | 331 | opt->source_netmask = 0; |
| 332 | opt->scope_netmask = 0; |
Simon Kelley | 25e63f1 | 2020-11-25 21:17:52 +0000 | [diff] [blame] | 333 | |
Simon Kelley | 22fe2fd | 2016-02-28 17:07:10 +0000 | [diff] [blame] | 334 | if (source->sa.sa_family == AF_INET6 && daemon->add_subnet6) |
Simon Kelley | 1d03016 | 2015-12-21 14:17:06 +0000 | [diff] [blame] | 335 | { |
| 336 | opt->source_netmask = daemon->add_subnet6->mask; |
| 337 | if (daemon->add_subnet6->addr_used) |
| 338 | { |
| 339 | sa_family = daemon->add_subnet6->addr.sa.sa_family; |
| 340 | addrp = get_addrp(&daemon->add_subnet6->addr, sa_family); |
Simon Kelley | 25e63f1 | 2020-11-25 21:17:52 +0000 | [diff] [blame] | 341 | cacheable = 1; |
Simon Kelley | 1d03016 | 2015-12-21 14:17:06 +0000 | [diff] [blame] | 342 | } |
| 343 | else |
| 344 | addrp = &source->in6.sin6_addr; |
| 345 | } |
Simon Kelley | 22fe2fd | 2016-02-28 17:07:10 +0000 | [diff] [blame] | 346 | |
| 347 | if (source->sa.sa_family == AF_INET && daemon->add_subnet4) |
Simon Kelley | 1d03016 | 2015-12-21 14:17:06 +0000 | [diff] [blame] | 348 | { |
| 349 | opt->source_netmask = daemon->add_subnet4->mask; |
| 350 | if (daemon->add_subnet4->addr_used) |
| 351 | { |
| 352 | sa_family = daemon->add_subnet4->addr.sa.sa_family; |
| 353 | addrp = get_addrp(&daemon->add_subnet4->addr, sa_family); |
Simon Kelley | 25e63f1 | 2020-11-25 21:17:52 +0000 | [diff] [blame] | 354 | cacheable = 1; /* Address is constant */ |
Simon Kelley | 1d03016 | 2015-12-21 14:17:06 +0000 | [diff] [blame] | 355 | } |
Simon Kelley | 22fe2fd | 2016-02-28 17:07:10 +0000 | [diff] [blame] | 356 | else |
| 357 | addrp = &source->in.sin_addr; |
Simon Kelley | 1d03016 | 2015-12-21 14:17:06 +0000 | [diff] [blame] | 358 | } |
| 359 | |
Simon Kelley | 22fe2fd | 2016-02-28 17:07:10 +0000 | [diff] [blame] | 360 | opt->family = htons(sa_family == AF_INET6 ? 2 : 1); |
Simon Kelley | 22fe2fd | 2016-02-28 17:07:10 +0000 | [diff] [blame] | 361 | |
Simon Kelley | 0fbd980 | 2017-05-21 22:24:43 +0100 | [diff] [blame] | 362 | if (addrp && opt->source_netmask != 0) |
Simon Kelley | 1d03016 | 2015-12-21 14:17:06 +0000 | [diff] [blame] | 363 | { |
Simon Kelley | 1d03016 | 2015-12-21 14:17:06 +0000 | [diff] [blame] | 364 | len = ((opt->source_netmask - 1) >> 3) + 1; |
| 365 | memcpy(opt->addr, addrp, len); |
| 366 | if (opt->source_netmask & 7) |
| 367 | opt->addr[len-1] &= 0xff << (8 - (opt->source_netmask & 7)); |
| 368 | } |
Simon Kelley | 25e63f1 | 2020-11-25 21:17:52 +0000 | [diff] [blame] | 369 | else |
| 370 | { |
| 371 | cacheable = 1; /* No address ever supplied. */ |
| 372 | len = 0; |
| 373 | } |
| 374 | |
| 375 | if (cacheablep) |
| 376 | *cacheablep = cacheable; |
Simon Kelley | 22fe2fd | 2016-02-28 17:07:10 +0000 | [diff] [blame] | 377 | |
Simon Kelley | 1d03016 | 2015-12-21 14:17:06 +0000 | [diff] [blame] | 378 | return len + 4; |
| 379 | } |
| 380 | |
Simon Kelley | 25e63f1 | 2020-11-25 21:17:52 +0000 | [diff] [blame] | 381 | static size_t add_source_addr(struct dns_header *header, size_t plen, unsigned char *limit, union mysockaddr *source, int *cacheable) |
Simon Kelley | 1d03016 | 2015-12-21 14:17:06 +0000 | [diff] [blame] | 382 | { |
| 383 | /* http://tools.ietf.org/html/draft-vandergaast-edns-client-subnet-02 */ |
| 384 | |
| 385 | int len; |
| 386 | struct subnet_opt opt; |
| 387 | |
Simon Kelley | 25e63f1 | 2020-11-25 21:17:52 +0000 | [diff] [blame] | 388 | len = calc_subnet_opt(&opt, source, cacheable); |
Simon Kelley | c7f3bd2 | 2016-02-28 21:48:34 +0000 | [diff] [blame] | 389 | return add_pseudoheader(header, plen, (unsigned char *)limit, PACKETSZ, EDNS0_OPTION_CLIENT_SUBNET, (unsigned char *)&opt, len, 0, 0); |
Simon Kelley | 1d03016 | 2015-12-21 14:17:06 +0000 | [diff] [blame] | 390 | } |
| 391 | |
Simon Kelley | 1d03016 | 2015-12-21 14:17:06 +0000 | [diff] [blame] | 392 | int check_source(struct dns_header *header, size_t plen, unsigned char *pseudoheader, union mysockaddr *peer) |
| 393 | { |
| 394 | /* Section 9.2, Check that subnet option in reply matches. */ |
Simon Kelley | 5aa5f0f | 2015-12-21 17:20:35 +0000 | [diff] [blame] | 395 | |
| 396 | int len, calc_len; |
Simon Kelley | 1d03016 | 2015-12-21 14:17:06 +0000 | [diff] [blame] | 397 | struct subnet_opt opt; |
| 398 | unsigned char *p; |
| 399 | int code, i, rdlen; |
| 400 | |
Simon Kelley | 25e63f1 | 2020-11-25 21:17:52 +0000 | [diff] [blame] | 401 | calc_len = calc_subnet_opt(&opt, peer, NULL); |
Simon Kelley | 1d03016 | 2015-12-21 14:17:06 +0000 | [diff] [blame] | 402 | |
Simon Kelley | 25e63f1 | 2020-11-25 21:17:52 +0000 | [diff] [blame] | 403 | if (!(p = skip_name(pseudoheader, header, plen, 10))) |
| 404 | return 1; |
| 405 | |
| 406 | p += 8; /* skip UDP length and RCODE */ |
| 407 | |
| 408 | GETSHORT(rdlen, p); |
| 409 | if (!CHECK_LEN(header, p, plen, rdlen)) |
| 410 | return 1; /* bad packet */ |
| 411 | |
| 412 | /* check if option there */ |
Simon Kelley | 1d03016 | 2015-12-21 14:17:06 +0000 | [diff] [blame] | 413 | for (i = 0; i + 4 < rdlen; i += len + 4) |
| 414 | { |
| 415 | GETSHORT(code, p); |
| 416 | GETSHORT(len, p); |
| 417 | if (code == EDNS0_OPTION_CLIENT_SUBNET) |
| 418 | { |
| 419 | /* make sure this doesn't mismatch. */ |
| 420 | opt.scope_netmask = p[3]; |
| 421 | if (len != calc_len || memcmp(p, &opt, len) != 0) |
| 422 | return 0; |
| 423 | } |
| 424 | p += len; |
| 425 | } |
| 426 | |
| 427 | return 1; |
| 428 | } |
Simon Kelley | 33702ab | 2015-12-28 23:17:15 +0000 | [diff] [blame] | 429 | |
Simon Kelley | 25e63f1 | 2020-11-25 21:17:52 +0000 | [diff] [blame] | 430 | /* Set *check_subnet if we add a client subnet option, which needs to checked |
| 431 | in the reply. Set *cacheable to zero if we add an option which the answer |
| 432 | may depend on. */ |
Simon Kelley | 33702ab | 2015-12-28 23:17:15 +0000 | [diff] [blame] | 433 | size_t add_edns0_config(struct dns_header *header, size_t plen, unsigned char *limit, |
Simon Kelley | 25e63f1 | 2020-11-25 21:17:52 +0000 | [diff] [blame] | 434 | union mysockaddr *source, time_t now, int *check_subnet, int *cacheable) |
Simon Kelley | 33702ab | 2015-12-28 23:17:15 +0000 | [diff] [blame] | 435 | { |
| 436 | *check_subnet = 0; |
Simon Kelley | 25e63f1 | 2020-11-25 21:17:52 +0000 | [diff] [blame] | 437 | *cacheable = 1; |
| 438 | |
Simon Kelley | 33702ab | 2015-12-28 23:17:15 +0000 | [diff] [blame] | 439 | if (option_bool(OPT_ADD_MAC)) |
Simon Kelley | 25e63f1 | 2020-11-25 21:17:52 +0000 | [diff] [blame] | 440 | plen = add_mac(header, plen, limit, source, now, cacheable); |
Simon Kelley | 33702ab | 2015-12-28 23:17:15 +0000 | [diff] [blame] | 441 | |
Simon Kelley | 9e4cf47 | 2016-02-17 20:26:32 +0000 | [diff] [blame] | 442 | if (option_bool(OPT_MAC_B64) || option_bool(OPT_MAC_HEX)) |
Simon Kelley | 25e63f1 | 2020-11-25 21:17:52 +0000 | [diff] [blame] | 443 | plen = add_dns_client(header, plen, limit, source, now, cacheable); |
| 444 | |
Simon Kelley | 1e50512 | 2016-01-25 21:29:23 +0000 | [diff] [blame] | 445 | if (daemon->dns_client_id) |
| 446 | plen = add_pseudoheader(header, plen, limit, PACKETSZ, EDNS0_OPTION_NOMCPEID, |
Simon Kelley | c7f3bd2 | 2016-02-28 21:48:34 +0000 | [diff] [blame] | 447 | (unsigned char *)daemon->dns_client_id, strlen(daemon->dns_client_id), 0, 1); |
Simon Kelley | 33702ab | 2015-12-28 23:17:15 +0000 | [diff] [blame] | 448 | |
| 449 | if (option_bool(OPT_CLIENT_SUBNET)) |
| 450 | { |
Simon Kelley | 25e63f1 | 2020-11-25 21:17:52 +0000 | [diff] [blame] | 451 | plen = add_source_addr(header, plen, limit, source, cacheable); |
Simon Kelley | 33702ab | 2015-12-28 23:17:15 +0000 | [diff] [blame] | 452 | *check_subnet = 1; |
| 453 | } |
| 454 | |
| 455 | return plen; |
| 456 | } |