| 1 |
/* |
| 2 |
* Copyright (c) 1989 The Regents of the University of California. |
| 3 |
* All rights reserved. |
| 4 |
* |
| 5 |
* This code is derived from software contributed to Berkeley by |
| 6 |
* Mike Muuss. |
| 7 |
* |
| 8 |
* Redistribution and use in source and binary forms, with or without |
| 9 |
* modification, are permitted provided that the following conditions |
| 10 |
* are met: |
| 11 |
* 1. Redistributions of source code must retain the above copyright |
| 12 |
* notice, this list of conditions and the following disclaimer. |
| 13 |
* 2. Redistributions in binary form must reproduce the above copyright |
| 14 |
* notice, this list of conditions and the following disclaimer in the |
| 15 |
* documentation and/or other materials provided with the distribution. |
| 16 |
* 3. Neither the name of the University nor the names of its contributors |
| 17 |
* may be used to endorse or promote products derived from this software |
| 18 |
* without specific prior written permission. |
| 19 |
* |
| 20 |
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
| 21 |
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 22 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 23 |
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 24 |
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 25 |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 26 |
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 27 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 28 |
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 29 |
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 30 |
* SUCH DAMAGE. |
| 31 |
*/ |
| 32 |
|
| 33 |
#define _GNU_SOURCE |
| 34 |
|
| 35 |
#include <stddef.h> |
| 36 |
|
| 37 |
#include "iputils_common.h" |
| 38 |
#include "md5.h" |
| 39 |
#include "ping.h" |
| 40 |
|
| 41 |
struct niquery_option { |
| 42 |
char *name; |
| 43 |
int namelen; |
| 44 |
int has_arg; |
| 45 |
int data; |
| 46 |
int (*handler)(struct ping_ni *ni, int index, const char *arg); |
| 47 |
}; |
| 48 |
|
| 49 |
#define NIQUERY_OPTION(_name, _has_arg, _data, _handler) \ |
| 50 |
{ \ |
| 51 |
.name = (_name), \ |
| 52 |
.namelen = sizeof(_name) - 1, \ |
| 53 |
.has_arg = (_has_arg), \ |
| 54 |
.data = (_data), \ |
| 55 |
.handler = (_handler) \ |
| 56 |
} |
| 57 |
|
| 58 |
static int niquery_option_name_handler(struct ping_ni *ni, int index __attribute__((__unused__)), const char *arg __attribute__((__unused__))); |
| 59 |
static int niquery_option_ipv6_handler(struct ping_ni *ni, int index __attribute__((__unused__)), const char *arg __attribute__((__unused__))); |
| 60 |
static int niquery_option_ipv6_flag_handler(struct ping_ni *ni, int index, const char *arg); |
| 61 |
static int niquery_option_ipv4_handler(struct ping_ni *ni, int index, const char *arg); |
| 62 |
static int niquery_option_ipv4_flag_handler(struct ping_ni *ni, int index, const char *arg); |
| 63 |
static int niquery_option_subject_addr_handler(struct ping_ni *ni, int index, const char *arg); |
| 64 |
static int niquery_option_subject_name_handler(struct ping_ni *ni, int index, const char *name); |
| 65 |
static int niquery_option_help_handler(struct ping_ni *ni, int index, const char *arg); |
| 66 |
|
| 67 |
struct niquery_option niquery_options[] = { |
| 68 |
NIQUERY_OPTION("name", 0, 0, niquery_option_name_handler), |
| 69 |
NIQUERY_OPTION("fqdn", 0, 0, niquery_option_name_handler), |
| 70 |
NIQUERY_OPTION("ipv6", 0, 0, niquery_option_ipv6_handler), |
| 71 |
NIQUERY_OPTION("ipv6-all", 0, IPUTILS_NI_IPV6_FLAG_ALL, niquery_option_ipv6_flag_handler), |
| 72 |
NIQUERY_OPTION("ipv6-compatible", 0, IPUTILS_NI_IPV6_FLAG_COMPAT, niquery_option_ipv6_flag_handler), |
| 73 |
NIQUERY_OPTION("ipv6-linklocal", 0, IPUTILS_NI_IPV6_FLAG_LINKLOCAL, niquery_option_ipv6_flag_handler), |
| 74 |
NIQUERY_OPTION("ipv6-sitelocal", 0, IPUTILS_NI_IPV6_FLAG_SITELOCAL, niquery_option_ipv6_flag_handler), |
| 75 |
NIQUERY_OPTION("ipv6-global", 0, IPUTILS_NI_IPV6_FLAG_GLOBAL, niquery_option_ipv6_flag_handler), |
| 76 |
NIQUERY_OPTION("ipv4", 0, 0, niquery_option_ipv4_handler), |
| 77 |
NIQUERY_OPTION("ipv4-all", 0, IPUTILS_NI_IPV4_FLAG_ALL, niquery_option_ipv4_flag_handler), |
| 78 |
NIQUERY_OPTION("subject-ipv6", 1, IPUTILS_NI_ICMP6_SUBJ_IPV6, niquery_option_subject_addr_handler), |
| 79 |
NIQUERY_OPTION("subject-ipv4", 1, IPUTILS_NI_ICMP6_SUBJ_IPV4, niquery_option_subject_addr_handler), |
| 80 |
NIQUERY_OPTION("subject-name", 1, 0, niquery_option_subject_name_handler), |
| 81 |
NIQUERY_OPTION("subject-fqdn", 1, -1, niquery_option_subject_name_handler), |
| 82 |
NIQUERY_OPTION("help", 0, 0, niquery_option_help_handler), |
| 83 |
{NULL, 0, 0, 0, NULL} |
| 84 |
}; |
| 85 |
|
| 86 |
int niquery_is_enabled(struct ping_ni *ni) |
| 87 |
{ |
| 88 |
return ni->query >= 0; |
| 89 |
} |
| 90 |
|
| 91 |
void niquery_init_nonce(struct ping_ni *ni) |
| 92 |
{ |
| 93 |
#if PING6_NONCE_MEMORY |
| 94 |
iputils_srand(); |
| 95 |
ni->nonce_ptr = calloc(NI_NONCE_SIZE, MAX_DUP_CHK); |
| 96 |
if (!ni->nonce_ptr) |
| 97 |
error(2, errno, "calloc"); |
| 98 |
|
| 99 |
ni->nonce_ptr[0] = ~0; |
| 100 |
#else |
| 101 |
gettimeofday(&ni->nonce_secret.tv, NULL); |
| 102 |
ni->nonce_secret.pid = getpid(); |
| 103 |
#endif |
| 104 |
} |
| 105 |
|
| 106 |
#if !PING6_NONCE_MEMORY |
| 107 |
static int niquery_nonce(struct ping_ni *ni, uint8_t *nonce, int fill) |
| 108 |
{ |
| 109 |
static uint8_t digest[IPUTILS_MD5LENGTH]; |
| 110 |
static int seq = -1; |
| 111 |
|
| 112 |
if (fill || seq != *(uint16_t *)nonce || seq == -1) { |
| 113 |
IPUTILS_MD5_CTX ctxt; |
| 114 |
|
| 115 |
iputils_MD5Init(&ctxt); |
| 116 |
iputils_MD5Update(&ctxt, (const char *)&ni->nonce_secret, |
| 117 |
sizeof(ni->nonce_secret)); |
| 118 |
iputils_MD5Update(&ctxt, (const char *)nonce, sizeof(uint16_t)); |
| 119 |
iputils_MD5Final(digest, &ctxt); |
| 120 |
|
| 121 |
seq = *(uint16_t *)nonce; |
| 122 |
} |
| 123 |
|
| 124 |
if (fill) { |
| 125 |
memcpy(nonce + sizeof(uint16_t), digest, NI_NONCE_SIZE - sizeof(uint16_t)); |
| 126 |
return 0; |
| 127 |
} |
| 128 |
|
| 129 |
if (memcmp(nonce + sizeof(uint16_t), digest, NI_NONCE_SIZE - sizeof(uint16_t))) |
| 130 |
return -1; |
| 131 |
|
| 132 |
return ntohsp((uint16_t *)nonce); |
| 133 |
} |
| 134 |
#endif |
| 135 |
|
| 136 |
void niquery_fill_nonce(struct ping_ni *ni, uint16_t seq, uint8_t *nonce) |
| 137 |
{ |
| 138 |
uint16_t v = htons(seq); |
| 139 |
#if PING6_NONCE_MEMORY |
| 140 |
int i; |
| 141 |
|
| 142 |
memcpy(&ni->nonce_ptr[NI_NONCE_SIZE * (seq % MAX_DUP_CHK)], &v, sizeof(v)); |
| 143 |
|
| 144 |
for (i = sizeof(v); i < NI_NONCE_SIZE; i++) |
| 145 |
ni->nonce_ptr[NI_NONCE_SIZE * (seq % MAX_DUP_CHK) + i] = 0x100 * (rand() / (RAND_MAX + 1.0)); |
| 146 |
|
| 147 |
memcpy(nonce, &ni->nonce_ptr[NI_NONCE_SIZE * (seq % MAX_DUP_CHK)], NI_NONCE_SIZE); |
| 148 |
#else |
| 149 |
memcpy(nonce, &v, sizeof(v)); |
| 150 |
niquery_nonce(ni, nonce, 1); |
| 151 |
#endif |
| 152 |
} |
| 153 |
|
| 154 |
int niquery_check_nonce(struct ping_ni *ni, uint8_t *nonce) |
| 155 |
{ |
| 156 |
#if PING6_NONCE_MEMORY |
| 157 |
uint16_t seq = ntohsp((uint16_t *)nonce); |
| 158 |
if (memcmp(nonce, &ni->nonce_ptr[NI_NONCE_SIZE * (seq % MAX_DUP_CHK)], NI_NONCE_SIZE)) |
| 159 |
return -1; |
| 160 |
return seq; |
| 161 |
#else |
| 162 |
return niquery_nonce(ni, nonce, 0); |
| 163 |
#endif |
| 164 |
} |
| 165 |
|
| 166 |
static int niquery_set_qtype(struct ping_ni *ni, int type) |
| 167 |
{ |
| 168 |
if (niquery_is_enabled(ni) && ni->query != type) { |
| 169 |
printf(_("Qtype conflict\n")); |
| 170 |
return -1; |
| 171 |
} |
| 172 |
ni->query = type; |
| 173 |
return 0; |
| 174 |
} |
| 175 |
|
| 176 |
static int niquery_option_name_handler(struct ping_ni *ni, int index __attribute__((__unused__)), const char *arg __attribute__((__unused__))) |
| 177 |
{ |
| 178 |
if (niquery_set_qtype(ni, IPUTILS_NI_QTYPE_DNSNAME) < 0) |
| 179 |
return -1; |
| 180 |
return 0; |
| 181 |
} |
| 182 |
|
| 183 |
static int niquery_option_ipv6_handler(struct ping_ni *ni, int index __attribute__((__unused__)), const char *arg __attribute__((__unused__))) |
| 184 |
{ |
| 185 |
if (niquery_set_qtype(ni, IPUTILS_NI_QTYPE_IPV6ADDR) < 0) |
| 186 |
return -1; |
| 187 |
return 0; |
| 188 |
} |
| 189 |
|
| 190 |
static int niquery_option_ipv6_flag_handler(struct ping_ni *ni, int index, const char *arg __attribute__((__unused__))) |
| 191 |
{ |
| 192 |
if (niquery_set_qtype(ni, IPUTILS_NI_QTYPE_IPV6ADDR) < 0) |
| 193 |
return -1; |
| 194 |
ni->flag |= niquery_options[index].data; |
| 195 |
return 0; |
| 196 |
} |
| 197 |
|
| 198 |
static int niquery_option_ipv4_handler(struct ping_ni *ni, int index __attribute__((__unused__)), const char *arg __attribute__((__unused__))) |
| 199 |
{ |
| 200 |
if (niquery_set_qtype(ni, IPUTILS_NI_QTYPE_IPV4ADDR) < 0) |
| 201 |
return -1; |
| 202 |
return 0; |
| 203 |
} |
| 204 |
|
| 205 |
static int niquery_option_ipv4_flag_handler(struct ping_ni *ni, int index, const char *arg __attribute__((__unused__))) |
| 206 |
{ |
| 207 |
if (niquery_set_qtype(ni, IPUTILS_NI_QTYPE_IPV4ADDR) < 0) |
| 208 |
return -1; |
| 209 |
ni->flag |= niquery_options[index].data; |
| 210 |
return 0; |
| 211 |
} |
| 212 |
|
| 213 |
int niquery_is_subject_valid(struct ping_ni *ni) |
| 214 |
{ |
| 215 |
return ni->subject_type >= 0 && ni->subject; |
| 216 |
} |
| 217 |
|
| 218 |
static int niquery_set_subject_type(struct ping_ni *ni, int type) |
| 219 |
{ |
| 220 |
if (niquery_is_subject_valid(ni) && ni->subject_type != type) { |
| 221 |
printf(_("Subject type conflict\n")); |
| 222 |
return -1; |
| 223 |
} |
| 224 |
ni->subject_type = type; |
| 225 |
return 0; |
| 226 |
} |
| 227 |
|
| 228 |
static int niquery_option_subject_addr_handler(struct ping_ni *ni, int index, const char *arg) |
| 229 |
{ |
| 230 |
struct addrinfo hints = { |
| 231 |
.ai_family = AF_UNSPEC, |
| 232 |
.ai_socktype = SOCK_DGRAM, |
| 233 |
.ai_flags = getaddrinfo_flags |
| 234 |
}; |
| 235 |
struct addrinfo *result, *ai; |
| 236 |
int ret_val; |
| 237 |
int offset; |
| 238 |
|
| 239 |
if (niquery_set_subject_type(ni, niquery_options[index].data) < 0) |
| 240 |
return -1; |
| 241 |
|
| 242 |
ni->subject_type = niquery_options[index].data; |
| 243 |
|
| 244 |
switch (niquery_options[index].data) { |
| 245 |
case IPUTILS_NI_ICMP6_SUBJ_IPV6: |
| 246 |
ni->subject_len = sizeof(struct in6_addr); |
| 247 |
offset = offsetof(struct sockaddr_in6, sin6_addr); |
| 248 |
hints.ai_family = AF_INET6; |
| 249 |
break; |
| 250 |
case IPUTILS_NI_ICMP6_SUBJ_IPV4: |
| 251 |
ni->subject_len = sizeof(struct in_addr); |
| 252 |
offset = offsetof(struct sockaddr_in, sin_addr); |
| 253 |
hints.ai_family = AF_INET; |
| 254 |
break; |
| 255 |
default: |
| 256 |
/* should not happen. */ |
| 257 |
offset = -1; |
| 258 |
} |
| 259 |
|
| 260 |
ret_val = getaddrinfo(arg, 0, &hints, &result); |
| 261 |
if (ret_val) { |
| 262 |
error(0, 0, "%s: %s", arg, gai_strerror(ret_val)); |
| 263 |
return -1; |
| 264 |
} |
| 265 |
|
| 266 |
for (ai = result; ai; ai = ai->ai_next) { |
| 267 |
void *p = malloc(ni->subject_len); |
| 268 |
if (!p) |
| 269 |
continue; |
| 270 |
memcpy(p, (uint8_t *)ai->ai_addr + offset, ni->subject_len); |
| 271 |
free(ni->subject); |
| 272 |
ni->subject = p; |
| 273 |
break; |
| 274 |
} |
| 275 |
freeaddrinfo(result); |
| 276 |
|
| 277 |
return 0; |
| 278 |
} |
| 279 |
|
| 280 |
#ifdef USE_IDN |
| 281 |
# if IDN2_VERSION_NUMBER >= 0x02000000 |
| 282 |
# define IDN2_FLAGS IDN2_NONTRANSITIONAL |
| 283 |
# else |
| 284 |
# define IDN2_FLAGS 0 |
| 285 |
# endif |
| 286 |
#endif |
| 287 |
|
| 288 |
static int niquery_option_subject_name_handler(struct ping_ni *ni, int index, const char *name) |
| 289 |
{ |
| 290 |
static char nigroup_buf[INET6_ADDRSTRLEN + 1 + IFNAMSIZ]; |
| 291 |
unsigned char *dnptrs[2], **dpp, **lastdnptr; |
| 292 |
int n; |
| 293 |
size_t i; |
| 294 |
char *p; |
| 295 |
char *canonname = NULL, *idn = NULL; |
| 296 |
char *buf = NULL; |
| 297 |
size_t namelen; |
| 298 |
size_t buflen; |
| 299 |
int dots, fqdn = niquery_options[index].data; |
| 300 |
IPUTILS_MD5_CTX ctxt; |
| 301 |
uint8_t digest[IPUTILS_MD5LENGTH]; |
| 302 |
#ifdef USE_IDN |
| 303 |
int rc; |
| 304 |
#endif |
| 305 |
|
| 306 |
if (niquery_set_subject_type(ni, IPUTILS_NI_ICMP6_SUBJ_FQDN) < 0) |
| 307 |
return -1; |
| 308 |
|
| 309 |
#ifdef USE_IDN |
| 310 |
rc = idn2_lookup_ul(name, &idn, IDN2_FLAGS); |
| 311 |
if (rc) |
| 312 |
error(2, 0, _("IDN encoding error: %s"), idn2_strerror(rc)); |
| 313 |
#else |
| 314 |
idn = strdup(name); |
| 315 |
if (!idn) |
| 316 |
goto oomexit; |
| 317 |
#endif |
| 318 |
|
| 319 |
p = strchr(idn, SCOPE_DELIMITER); |
| 320 |
if (p) { |
| 321 |
*p = '\0'; |
| 322 |
if (strlen(p + 1) >= IFNAMSIZ) |
| 323 |
error(2, 0, _("too long scope name")); |
| 324 |
} |
| 325 |
|
| 326 |
namelen = strlen(idn); |
| 327 |
canonname = malloc(namelen + 1); |
| 328 |
if (!canonname) |
| 329 |
goto oomexit; |
| 330 |
|
| 331 |
dots = 0; |
| 332 |
for (i = 0; i < namelen + 1; i++) { |
| 333 |
canonname[i] = isupper(idn[i]) ? tolower(idn[i]) : idn[i]; |
| 334 |
if (idn[i] == '.') |
| 335 |
dots++; |
| 336 |
} |
| 337 |
|
| 338 |
if (fqdn == 0) { |
| 339 |
/* guess if hostname is FQDN */ |
| 340 |
fqdn = dots ? 1 : -1; |
| 341 |
} |
| 342 |
|
| 343 |
buflen = namelen + 3 + 1; /* dn_comp() requires strlen() + 3, |
| 344 |
plus non-fqdn indicator. */ |
| 345 |
buf = malloc(buflen); |
| 346 |
if (!buf) { |
| 347 |
error(0, errno, _("memory allocation failed")); |
| 348 |
goto errexit; |
| 349 |
} |
| 350 |
|
| 351 |
dpp = dnptrs; |
| 352 |
lastdnptr = &dnptrs[ARRAY_SIZE(dnptrs)]; |
| 353 |
|
| 354 |
*dpp++ = (unsigned char *)buf; |
| 355 |
*dpp++ = NULL; |
| 356 |
|
| 357 |
n = dn_comp(canonname, (unsigned char *)buf, buflen, dnptrs, lastdnptr); |
| 358 |
if (n < 0) { |
| 359 |
error(0, 0, _("inappropriate subject name: %s"), canonname); |
| 360 |
goto errexit; |
| 361 |
} else if ((size_t)n >= buflen) { |
| 362 |
error(0, 0, _("dn_comp() returned too long result")); |
| 363 |
goto errexit; |
| 364 |
} |
| 365 |
|
| 366 |
iputils_MD5Init(&ctxt); |
| 367 |
iputils_MD5Update(&ctxt, buf, buf[0]); |
| 368 |
iputils_MD5Final(digest, &ctxt); |
| 369 |
|
| 370 |
sprintf(nigroup_buf, "ff02::2:%02x%02x:%02x%02x%s%s", |
| 371 |
digest[0], digest[1], digest[2], digest[3], |
| 372 |
p ? "%" : "", |
| 373 |
p ? p + 1 : ""); |
| 374 |
|
| 375 |
if (fqdn < 0) |
| 376 |
buf[n] = 0; |
| 377 |
|
| 378 |
free(ni->subject); |
| 379 |
|
| 380 |
ni->group = nigroup_buf; |
| 381 |
ni->subject = buf; |
| 382 |
ni->subject_len = n + (fqdn < 0); |
| 383 |
|
| 384 |
free(canonname); |
| 385 |
free(idn); |
| 386 |
|
| 387 |
return 0; |
| 388 |
oomexit: |
| 389 |
error(0, errno, _("memory allocation failed")); |
| 390 |
errexit: |
| 391 |
free(buf); |
| 392 |
free(canonname); |
| 393 |
free(idn); |
| 394 |
exit(2); |
| 395 |
} |
| 396 |
|
| 397 |
int niquery_option_help_handler(struct ping_ni *ni __attribute__((__unused__)), |
| 398 |
int index, |
| 399 |
const char *arg __attribute__((__unused__))) |
| 400 |
{ |
| 401 |
fprintf(index ? stdout : stderr, |
| 402 |
_("ping -6 -N <nodeinfo opt>\n" |
| 403 |
"Help:\n" |
| 404 |
" help\n" |
| 405 |
"Query:\n" |
| 406 |
" name\n" |
| 407 |
" ipv6\n" |
| 408 |
" ipv6-all\n" |
| 409 |
" ipv6-compatible\n" |
| 410 |
" ipv6-global\n" |
| 411 |
" ipv6-linklocal\n" |
| 412 |
" ipv6-sitelocal\n" |
| 413 |
" ipv4\n" |
| 414 |
" ipv4-all\n" |
| 415 |
"Subject:\n" |
| 416 |
" subject-ipv6=addr\n" |
| 417 |
" subject-ipv4=addr\n" |
| 418 |
" subject-name=name\n" |
| 419 |
" subject-fqdn=name\n" |
| 420 |
)); |
| 421 |
index ? exit(0) : exit(2); |
| 422 |
} |
| 423 |
|
| 424 |
int niquery_option_handler(struct ping_ni *ni, const char *opt_arg) |
| 425 |
{ |
| 426 |
struct niquery_option *p; |
| 427 |
int i; |
| 428 |
int ret = -1; |
| 429 |
for (i = 0, p = niquery_options; p->name; i++, p++) { |
| 430 |
if (strncmp(p->name, opt_arg, p->namelen)) |
| 431 |
continue; |
| 432 |
if (!p->has_arg) { |
| 433 |
if (opt_arg[p->namelen] == '\0') { |
| 434 |
ret = p->handler(ni, i, NULL); |
| 435 |
if (ret >= 0) |
| 436 |
break; |
| 437 |
} |
| 438 |
} else { |
| 439 |
if (opt_arg[p->namelen] == '=') { |
| 440 |
ret = p->handler(ni, i, &opt_arg[p->namelen] + 1); |
| 441 |
if (ret >= 0) |
| 442 |
break; |
| 443 |
} |
| 444 |
} |
| 445 |
} |
| 446 |
if (!p->name) |
| 447 |
ret = niquery_option_help_handler(ni, 0, NULL); |
| 448 |
return ret; |
| 449 |
} |