| Chung-yih Wang | 0a1907d | 2009-04-23 12:26:00 +0800 | [diff] [blame^] | 1 | /* $NetBSD: sockmisc.c,v 1.8.6.1 2007/08/01 11:52:22 vanhu Exp $ */ |
| 2 | |
| 3 | /* Id: sockmisc.c,v 1.24 2006/05/07 21:32:59 manubsd Exp */ |
| 4 | |
| 5 | /* |
| 6 | * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. |
| 7 | * All rights reserved. |
| 8 | * |
| 9 | * Redistribution and use in source and binary forms, with or without |
| 10 | * modification, are permitted provided that the following conditions |
| 11 | * are met: |
| 12 | * 1. Redistributions of source code must retain the above copyright |
| 13 | * notice, this list of conditions and the following disclaimer. |
| 14 | * 2. Redistributions in binary form must reproduce the above copyright |
| 15 | * notice, this list of conditions and the following disclaimer in the |
| 16 | * documentation and/or other materials provided with the distribution. |
| 17 | * 3. Neither the name of the project nor the names of its contributors |
| 18 | * may be used to endorse or promote products derived from this software |
| 19 | * without specific prior written permission. |
| 20 | * |
| 21 | * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND |
| 22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE |
| 25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 27 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 28 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 30 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 31 | * SUCH DAMAGE. |
| 32 | */ |
| 33 | |
| 34 | #include "config.h" |
| 35 | |
| 36 | #include <sys/types.h> |
| 37 | #include <sys/param.h> |
| 38 | #include <sys/socket.h> |
| 39 | #include <sys/uio.h> |
| 40 | |
| 41 | #include <netinet/in.h> |
| 42 | #include PATH_IPSEC_H |
| 43 | |
| 44 | #if defined(INET6) && !defined(INET6_ADVAPI) && \ |
| 45 | defined(IP_RECVDSTADDR) && !defined(IPV6_RECVDSTADDR) |
| 46 | #define IPV6_RECVDSTADDR IP_RECVDSTADDR |
| 47 | #endif |
| 48 | |
| 49 | #include <stdlib.h> |
| 50 | #include <stdio.h> |
| 51 | #include <string.h> |
| 52 | #include <errno.h> |
| 53 | #ifdef HAVE_UNISTD_H |
| 54 | #include <unistd.h> |
| 55 | #endif |
| 56 | |
| 57 | #include "var.h" |
| 58 | #include "misc.h" |
| 59 | #include "plog.h" |
| 60 | #include "sockmisc.h" |
| 61 | #include "debug.h" |
| 62 | #include "gcmalloc.h" |
| 63 | #include "debugrm.h" |
| 64 | #include "libpfkey.h" |
| 65 | |
| 66 | #ifndef IP_IPSEC_POLICY |
| 67 | #define IP_IPSEC_POLICY 16 /* XXX: from linux/in.h */ |
| 68 | #endif |
| 69 | |
| 70 | #ifndef IPV6_IPSEC_POLICY |
| 71 | #define IPV6_IPSEC_POLICY 34 /* XXX: from linux/???.h per |
| 72 | "Tom Lendacky" <toml@us.ibm.com> */ |
| 73 | #endif |
| 74 | |
| 75 | const int niflags = 0; |
| 76 | |
| 77 | /* |
| 78 | * compare two sockaddr without port number. |
| 79 | * OUT: 0: equal. |
| 80 | * 1: not equal. |
| 81 | */ |
| 82 | int |
| 83 | cmpsaddrwop(addr1, addr2) |
| 84 | const struct sockaddr *addr1; |
| 85 | const struct sockaddr *addr2; |
| 86 | { |
| 87 | caddr_t sa1, sa2; |
| 88 | |
| 89 | if (addr1 == 0 && addr2 == 0) |
| 90 | return 0; |
| 91 | if (addr1 == 0 || addr2 == 0) |
| 92 | return 1; |
| 93 | |
| 94 | #ifdef __linux__ |
| 95 | if (addr1->sa_family != addr2->sa_family) |
| 96 | return 1; |
| 97 | #else |
| 98 | if (addr1->sa_len != addr2->sa_len |
| 99 | || addr1->sa_family != addr2->sa_family) |
| 100 | return 1; |
| 101 | |
| 102 | #endif /* __linux__ */ |
| 103 | |
| 104 | switch (addr1->sa_family) { |
| 105 | case AF_INET: |
| 106 | sa1 = (caddr_t)&((struct sockaddr_in *)addr1)->sin_addr; |
| 107 | sa2 = (caddr_t)&((struct sockaddr_in *)addr2)->sin_addr; |
| 108 | if (memcmp(sa1, sa2, sizeof(struct in_addr)) != 0) |
| 109 | return 1; |
| 110 | break; |
| 111 | #ifdef INET6 |
| 112 | case AF_INET6: |
| 113 | sa1 = (caddr_t)&((struct sockaddr_in6 *)addr1)->sin6_addr; |
| 114 | sa2 = (caddr_t)&((struct sockaddr_in6 *)addr2)->sin6_addr; |
| 115 | if (memcmp(sa1, sa2, sizeof(struct in6_addr)) != 0) |
| 116 | return 1; |
| 117 | if (((struct sockaddr_in6 *)addr1)->sin6_scope_id != |
| 118 | ((struct sockaddr_in6 *)addr2)->sin6_scope_id) |
| 119 | return 1; |
| 120 | break; |
| 121 | #endif |
| 122 | default: |
| 123 | return 1; |
| 124 | } |
| 125 | |
| 126 | return 0; |
| 127 | } |
| 128 | |
| 129 | /* |
| 130 | * compare two sockaddr with port, taking care wildcard. |
| 131 | * addr1 is a subject address, addr2 is in a database entry. |
| 132 | * OUT: 0: equal. |
| 133 | * 1: not equal. |
| 134 | */ |
| 135 | int |
| 136 | cmpsaddrwild(addr1, addr2) |
| 137 | const struct sockaddr *addr1; |
| 138 | const struct sockaddr *addr2; |
| 139 | { |
| 140 | caddr_t sa1, sa2; |
| 141 | u_short port1, port2; |
| 142 | |
| 143 | if (addr1 == 0 && addr2 == 0) |
| 144 | return 0; |
| 145 | if (addr1 == 0 || addr2 == 0) |
| 146 | return 1; |
| 147 | |
| 148 | #ifdef __linux__ |
| 149 | if (addr1->sa_family != addr2->sa_family) |
| 150 | return 1; |
| 151 | #else |
| 152 | if (addr1->sa_len != addr2->sa_len |
| 153 | || addr1->sa_family != addr2->sa_family) |
| 154 | return 1; |
| 155 | |
| 156 | #endif /* __linux__ */ |
| 157 | |
| 158 | switch (addr1->sa_family) { |
| 159 | case AF_INET: |
| 160 | sa1 = (caddr_t)&((struct sockaddr_in *)addr1)->sin_addr; |
| 161 | sa2 = (caddr_t)&((struct sockaddr_in *)addr2)->sin_addr; |
| 162 | port1 = ((struct sockaddr_in *)addr1)->sin_port; |
| 163 | port2 = ((struct sockaddr_in *)addr2)->sin_port; |
| 164 | if (!(port1 == IPSEC_PORT_ANY || |
| 165 | port2 == IPSEC_PORT_ANY || |
| 166 | port1 == port2)) |
| 167 | return 1; |
| 168 | if (memcmp(sa1, sa2, sizeof(struct in_addr)) != 0) |
| 169 | return 1; |
| 170 | break; |
| 171 | #ifdef INET6 |
| 172 | case AF_INET6: |
| 173 | sa1 = (caddr_t)&((struct sockaddr_in6 *)addr1)->sin6_addr; |
| 174 | sa2 = (caddr_t)&((struct sockaddr_in6 *)addr2)->sin6_addr; |
| 175 | port1 = ((struct sockaddr_in6 *)addr1)->sin6_port; |
| 176 | port2 = ((struct sockaddr_in6 *)addr2)->sin6_port; |
| 177 | if (!(port1 == IPSEC_PORT_ANY || |
| 178 | port2 == IPSEC_PORT_ANY || |
| 179 | port1 == port2)) |
| 180 | return 1; |
| 181 | if (memcmp(sa1, sa2, sizeof(struct in6_addr)) != 0) |
| 182 | return 1; |
| 183 | if (((struct sockaddr_in6 *)addr1)->sin6_scope_id != |
| 184 | ((struct sockaddr_in6 *)addr2)->sin6_scope_id) |
| 185 | return 1; |
| 186 | break; |
| 187 | #endif |
| 188 | default: |
| 189 | return 1; |
| 190 | } |
| 191 | |
| 192 | return 0; |
| 193 | } |
| 194 | |
| 195 | /* |
| 196 | * compare two sockaddr with strict match on port. |
| 197 | * OUT: 0: equal. |
| 198 | * 1: not equal. |
| 199 | */ |
| 200 | int |
| 201 | cmpsaddrstrict(addr1, addr2) |
| 202 | const struct sockaddr *addr1; |
| 203 | const struct sockaddr *addr2; |
| 204 | { |
| 205 | caddr_t sa1, sa2; |
| 206 | u_short port1, port2; |
| 207 | |
| 208 | if (addr1 == 0 && addr2 == 0) |
| 209 | return 0; |
| 210 | if (addr1 == 0 || addr2 == 0) |
| 211 | return 1; |
| 212 | |
| 213 | #ifdef __linux__ |
| 214 | if (addr1->sa_family != addr2->sa_family) |
| 215 | return 1; |
| 216 | #else |
| 217 | if (addr1->sa_len != addr2->sa_len |
| 218 | || addr1->sa_family != addr2->sa_family) |
| 219 | return 1; |
| 220 | |
| 221 | #endif /* __linux__ */ |
| 222 | |
| 223 | switch (addr1->sa_family) { |
| 224 | case AF_INET: |
| 225 | sa1 = (caddr_t)&((struct sockaddr_in *)addr1)->sin_addr; |
| 226 | sa2 = (caddr_t)&((struct sockaddr_in *)addr2)->sin_addr; |
| 227 | port1 = ((struct sockaddr_in *)addr1)->sin_port; |
| 228 | port2 = ((struct sockaddr_in *)addr2)->sin_port; |
| 229 | if (port1 != port2) |
| 230 | return 1; |
| 231 | if (memcmp(sa1, sa2, sizeof(struct in_addr)) != 0) |
| 232 | return 1; |
| 233 | break; |
| 234 | #ifdef INET6 |
| 235 | case AF_INET6: |
| 236 | sa1 = (caddr_t)&((struct sockaddr_in6 *)addr1)->sin6_addr; |
| 237 | sa2 = (caddr_t)&((struct sockaddr_in6 *)addr2)->sin6_addr; |
| 238 | port1 = ((struct sockaddr_in6 *)addr1)->sin6_port; |
| 239 | port2 = ((struct sockaddr_in6 *)addr2)->sin6_port; |
| 240 | if (port1 != port2) |
| 241 | return 1; |
| 242 | if (memcmp(sa1, sa2, sizeof(struct in6_addr)) != 0) |
| 243 | return 1; |
| 244 | if (((struct sockaddr_in6 *)addr1)->sin6_scope_id != |
| 245 | ((struct sockaddr_in6 *)addr2)->sin6_scope_id) |
| 246 | return 1; |
| 247 | break; |
| 248 | #endif |
| 249 | default: |
| 250 | return 1; |
| 251 | } |
| 252 | |
| 253 | return 0; |
| 254 | } |
| 255 | |
| 256 | /* get local address against the destination. */ |
| 257 | struct sockaddr * |
| 258 | getlocaladdr(remote) |
| 259 | struct sockaddr *remote; |
| 260 | { |
| 261 | struct sockaddr *local; |
| 262 | u_int local_len = sizeof(struct sockaddr_storage); |
| 263 | int s; /* for dummy connection */ |
| 264 | |
| 265 | /* allocate buffer */ |
| 266 | if ((local = racoon_calloc(1, local_len)) == NULL) { |
| 267 | plog(LLV_ERROR, LOCATION, NULL, |
| 268 | "failed to get address buffer.\n"); |
| 269 | goto err; |
| 270 | } |
| 271 | |
| 272 | /* get real interface received packet */ |
| 273 | if ((s = socket(remote->sa_family, SOCK_DGRAM, 0)) < 0) { |
| 274 | plog(LLV_ERROR, LOCATION, NULL, |
| 275 | "socket (%s)\n", strerror(errno)); |
| 276 | goto err; |
| 277 | } |
| 278 | |
| 279 | setsockopt_bypass(s, remote->sa_family); |
| 280 | |
| 281 | if (connect(s, remote, sysdep_sa_len(remote)) < 0) { |
| 282 | plog(LLV_ERROR, LOCATION, NULL, |
| 283 | "connect (%s)\n", strerror(errno)); |
| 284 | close(s); |
| 285 | goto err; |
| 286 | } |
| 287 | |
| 288 | if (getsockname(s, local, &local_len) < 0) { |
| 289 | plog(LLV_ERROR, LOCATION, NULL, |
| 290 | "getsockname (%s)\n", strerror(errno)); |
| 291 | close(s); |
| 292 | return NULL; |
| 293 | } |
| 294 | |
| 295 | close(s); |
| 296 | return local; |
| 297 | |
| 298 | err: |
| 299 | if (local != NULL) |
| 300 | racoon_free(local); |
| 301 | return NULL; |
| 302 | } |
| 303 | |
| 304 | /* |
| 305 | * Receive packet, with src/dst information. It is assumed that necessary |
| 306 | * setsockopt() have already performed on socket. |
| 307 | */ |
| 308 | int |
| 309 | recvfromto(s, buf, buflen, flags, from, fromlen, to, tolen) |
| 310 | int s; |
| 311 | void *buf; |
| 312 | size_t buflen; |
| 313 | int flags; |
| 314 | struct sockaddr *from; |
| 315 | socklen_t *fromlen; |
| 316 | struct sockaddr *to; |
| 317 | u_int *tolen; |
| 318 | { |
| 319 | int otolen; |
| 320 | u_int len; |
| 321 | struct sockaddr_storage ss; |
| 322 | struct msghdr m; |
| 323 | struct cmsghdr *cm; |
| 324 | struct iovec iov[2]; |
| 325 | u_char cmsgbuf[256]; |
| 326 | #if defined(INET6) && defined(INET6_ADVAPI) |
| 327 | struct in6_pktinfo *pi; |
| 328 | #endif /*INET6_ADVAPI*/ |
| 329 | struct sockaddr_in *sin; |
| 330 | #ifdef INET6 |
| 331 | struct sockaddr_in6 *sin6; |
| 332 | #endif |
| 333 | |
| 334 | len = sizeof(ss); |
| 335 | if (getsockname(s, (struct sockaddr *)&ss, &len) < 0) { |
| 336 | plog(LLV_ERROR, LOCATION, NULL, |
| 337 | "getsockname (%s)\n", strerror(errno)); |
| 338 | return -1; |
| 339 | } |
| 340 | |
| 341 | m.msg_name = (caddr_t)from; |
| 342 | m.msg_namelen = *fromlen; |
| 343 | iov[0].iov_base = (caddr_t)buf; |
| 344 | iov[0].iov_len = buflen; |
| 345 | m.msg_iov = iov; |
| 346 | m.msg_iovlen = 1; |
| 347 | memset(cmsgbuf, 0, sizeof(cmsgbuf)); |
| 348 | cm = (struct cmsghdr *)cmsgbuf; |
| 349 | m.msg_control = (caddr_t)cm; |
| 350 | m.msg_controllen = sizeof(cmsgbuf); |
| 351 | if ((len = recvmsg(s, &m, flags)) < 0) { |
| 352 | plog(LLV_ERROR, LOCATION, NULL, |
| 353 | "recvmsg (%s)\n", strerror(errno)); |
| 354 | return -1; |
| 355 | } |
| 356 | *fromlen = m.msg_namelen; |
| 357 | |
| 358 | otolen = *tolen; |
| 359 | *tolen = 0; |
| 360 | for (cm = (struct cmsghdr *)CMSG_FIRSTHDR(&m); |
| 361 | m.msg_controllen != 0 && cm; |
| 362 | cm = (struct cmsghdr *)CMSG_NXTHDR(&m, cm)) { |
| 363 | #if 0 |
| 364 | plog(LLV_ERROR, LOCATION, NULL, |
| 365 | "cmsg %d %d\n", cm->cmsg_level, cm->cmsg_type);) |
| 366 | #endif |
| 367 | #if defined(INET6) && defined(INET6_ADVAPI) |
| 368 | if (ss.ss_family == AF_INET6 |
| 369 | && cm->cmsg_level == IPPROTO_IPV6 |
| 370 | && cm->cmsg_type == IPV6_PKTINFO |
| 371 | && otolen >= sizeof(*sin6)) { |
| 372 | pi = (struct in6_pktinfo *)(CMSG_DATA(cm)); |
| 373 | *tolen = sizeof(*sin6); |
| 374 | sin6 = (struct sockaddr_in6 *)to; |
| 375 | memset(sin6, 0, sizeof(*sin6)); |
| 376 | sin6->sin6_family = AF_INET6; |
| 377 | #ifndef __linux__ |
| 378 | sin6->sin6_len = sizeof(*sin6); |
| 379 | #endif |
| 380 | memcpy(&sin6->sin6_addr, &pi->ipi6_addr, |
| 381 | sizeof(sin6->sin6_addr)); |
| 382 | /* XXX other cases, such as site-local? */ |
| 383 | if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) |
| 384 | sin6->sin6_scope_id = pi->ipi6_ifindex; |
| 385 | else |
| 386 | sin6->sin6_scope_id = 0; |
| 387 | sin6->sin6_port = |
| 388 | ((struct sockaddr_in6 *)&ss)->sin6_port; |
| 389 | otolen = -1; /* "to" already set */ |
| 390 | continue; |
| 391 | } |
| 392 | #endif |
| 393 | #ifdef __linux__ |
| 394 | if (ss.ss_family == AF_INET |
| 395 | && cm->cmsg_level == IPPROTO_IP |
| 396 | && cm->cmsg_type == IP_PKTINFO |
| 397 | && otolen >= sizeof(sin)) { |
| 398 | struct in_pktinfo *pi = (struct in_pktinfo *)(CMSG_DATA(cm)); |
| 399 | *tolen = sizeof(*sin); |
| 400 | sin = (struct sockaddr_in *)to; |
| 401 | memset(sin, 0, sizeof(*sin)); |
| 402 | sin->sin_family = AF_INET; |
| 403 | memcpy(&sin->sin_addr, &pi->ipi_addr, |
| 404 | sizeof(sin->sin_addr)); |
| 405 | sin->sin_port = |
| 406 | ((struct sockaddr_in *)&ss)->sin_port; |
| 407 | otolen = -1; /* "to" already set */ |
| 408 | continue; |
| 409 | } |
| 410 | #endif |
| 411 | #if defined(INET6) && defined(IPV6_RECVDSTADDR) |
| 412 | if (ss.ss_family == AF_INET6 |
| 413 | && cm->cmsg_level == IPPROTO_IPV6 |
| 414 | && cm->cmsg_type == IPV6_RECVDSTADDR |
| 415 | && otolen >= sizeof(*sin6)) { |
| 416 | *tolen = sizeof(*sin6); |
| 417 | sin6 = (struct sockaddr_in6 *)to; |
| 418 | memset(sin6, 0, sizeof(*sin6)); |
| 419 | sin6->sin6_family = AF_INET6; |
| 420 | sin6->sin6_len = sizeof(*sin6); |
| 421 | memcpy(&sin6->sin6_addr, CMSG_DATA(cm), |
| 422 | sizeof(sin6->sin6_addr)); |
| 423 | sin6->sin6_port = |
| 424 | ((struct sockaddr_in6 *)&ss)->sin6_port; |
| 425 | otolen = -1; /* "to" already set */ |
| 426 | continue; |
| 427 | } |
| 428 | #endif |
| 429 | #ifndef __linux__ |
| 430 | if (ss.ss_family == AF_INET |
| 431 | && cm->cmsg_level == IPPROTO_IP |
| 432 | && cm->cmsg_type == IP_RECVDSTADDR |
| 433 | && otolen >= sizeof(*sin)) { |
| 434 | *tolen = sizeof(*sin); |
| 435 | sin = (struct sockaddr_in *)to; |
| 436 | memset(sin, 0, sizeof(*sin)); |
| 437 | sin->sin_family = AF_INET; |
| 438 | sin->sin_len = sizeof(*sin); |
| 439 | memcpy(&sin->sin_addr, CMSG_DATA(cm), |
| 440 | sizeof(sin->sin_addr)); |
| 441 | sin->sin_port = ((struct sockaddr_in *)&ss)->sin_port; |
| 442 | otolen = -1; /* "to" already set */ |
| 443 | continue; |
| 444 | } |
| 445 | #endif |
| 446 | } |
| 447 | |
| 448 | return len; |
| 449 | } |
| 450 | |
| 451 | /* send packet, with fixing src/dst address pair. */ |
| 452 | int |
| 453 | sendfromto(s, buf, buflen, src, dst, cnt) |
| 454 | int s, cnt; |
| 455 | const void *buf; |
| 456 | size_t buflen; |
| 457 | struct sockaddr *src; |
| 458 | struct sockaddr *dst; |
| 459 | { |
| 460 | struct sockaddr_storage ss; |
| 461 | u_int len; |
| 462 | int i; |
| 463 | |
| 464 | if (src->sa_family != dst->sa_family) { |
| 465 | plog(LLV_ERROR, LOCATION, NULL, |
| 466 | "address family mismatch\n"); |
| 467 | return -1; |
| 468 | } |
| 469 | |
| 470 | len = sizeof(ss); |
| 471 | if (getsockname(s, (struct sockaddr *)&ss, &len) < 0) { |
| 472 | plog(LLV_ERROR, LOCATION, NULL, |
| 473 | "getsockname (%s)\n", strerror(errno)); |
| 474 | return -1; |
| 475 | } |
| 476 | |
| 477 | plog(LLV_DEBUG, LOCATION, NULL, |
| 478 | "sockname %s\n", saddr2str((struct sockaddr *)&ss)); |
| 479 | plog(LLV_DEBUG, LOCATION, NULL, |
| 480 | "send packet from %s\n", saddr2str(src)); |
| 481 | plog(LLV_DEBUG, LOCATION, NULL, |
| 482 | "send packet to %s\n", saddr2str(dst)); |
| 483 | |
| 484 | if (src->sa_family != ss.ss_family) { |
| 485 | plog(LLV_ERROR, LOCATION, NULL, |
| 486 | "address family mismatch\n"); |
| 487 | return -1; |
| 488 | } |
| 489 | |
| 490 | switch (src->sa_family) { |
| 491 | #if defined(INET6) && defined(INET6_ADVAPI) |
| 492 | // XXX: This block wasn't compiled on Linux - does it work? |
| 493 | case AF_INET6: |
| 494 | { |
| 495 | struct msghdr m; |
| 496 | struct cmsghdr *cm; |
| 497 | struct iovec iov[2]; |
| 498 | u_char cmsgbuf[256]; |
| 499 | struct in6_pktinfo *pi; |
| 500 | int ifindex; |
| 501 | struct sockaddr_in6 src6, dst6; |
| 502 | |
| 503 | memcpy(&src6, src, sizeof(src6)); |
| 504 | memcpy(&dst6, dst, sizeof(dst6)); |
| 505 | |
| 506 | /* XXX take care of other cases, such as site-local */ |
| 507 | ifindex = 0; |
| 508 | if (IN6_IS_ADDR_LINKLOCAL(&src6.sin6_addr) |
| 509 | || IN6_IS_ADDR_MULTICAST(&src6.sin6_addr)) { |
| 510 | ifindex = src6.sin6_scope_id; /*???*/ |
| 511 | } |
| 512 | |
| 513 | /* XXX some sanity check on dst6.sin6_scope_id */ |
| 514 | |
| 515 | /* flowinfo for IKE? mmm, maybe useful but for now make it 0 */ |
| 516 | src6.sin6_flowinfo = dst6.sin6_flowinfo = 0; |
| 517 | |
| 518 | memset(&m, 0, sizeof(m)); |
| 519 | m.msg_name = (caddr_t)&dst6; |
| 520 | m.msg_namelen = sizeof(dst6); |
| 521 | iov[0].iov_base = (char *)buf; |
| 522 | iov[0].iov_len = buflen; |
| 523 | m.msg_iov = iov; |
| 524 | m.msg_iovlen = 1; |
| 525 | |
| 526 | memset(cmsgbuf, 0, sizeof(cmsgbuf)); |
| 527 | cm = (struct cmsghdr *)cmsgbuf; |
| 528 | m.msg_control = (caddr_t)cm; |
| 529 | m.msg_controllen = CMSG_SPACE(sizeof(struct in6_pktinfo)); |
| 530 | |
| 531 | cm->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo)); |
| 532 | cm->cmsg_level = IPPROTO_IPV6; |
| 533 | cm->cmsg_type = IPV6_PKTINFO; |
| 534 | pi = (struct in6_pktinfo *)CMSG_DATA(cm); |
| 535 | memcpy(&pi->ipi6_addr, &src6.sin6_addr, sizeof(src6.sin6_addr)); |
| 536 | pi->ipi6_ifindex = ifindex; |
| 537 | |
| 538 | plog(LLV_DEBUG, LOCATION, NULL, |
| 539 | "src6 %s %d\n", |
| 540 | saddr2str((struct sockaddr *)&src6), |
| 541 | src6.sin6_scope_id); |
| 542 | plog(LLV_DEBUG, LOCATION, NULL, |
| 543 | "dst6 %s %d\n", |
| 544 | saddr2str((struct sockaddr *)&dst6), |
| 545 | dst6.sin6_scope_id); |
| 546 | |
| 547 | for (i = 0; i < cnt; i++) { |
| 548 | len = sendmsg(s, &m, 0 /*MSG_DONTROUTE*/); |
| 549 | if (len < 0) { |
| 550 | plog(LLV_ERROR, LOCATION, NULL, |
| 551 | "sendmsg (%s)\n", strerror(errno)); |
| 552 | return -1; |
| 553 | } |
| 554 | plog(LLV_DEBUG, LOCATION, NULL, |
| 555 | "%d times of %d bytes message will be sent " |
| 556 | "to %s\n", |
| 557 | i + 1, len, saddr2str(dst)); |
| 558 | } |
| 559 | plogdump(LLV_DEBUG, (char *)buf, buflen); |
| 560 | |
| 561 | return len; |
| 562 | } |
| 563 | #endif |
| 564 | #ifdef __linux__ |
| 565 | case AF_INET: |
| 566 | { |
| 567 | struct msghdr m; |
| 568 | struct cmsghdr *cm; |
| 569 | struct iovec iov[2]; |
| 570 | u_char cmsgbuf[256]; |
| 571 | struct in_pktinfo *pi; |
| 572 | int ifindex = 0; |
| 573 | struct sockaddr_in src6, dst6; |
| 574 | |
| 575 | memcpy(&src6, src, sizeof(src6)); |
| 576 | memcpy(&dst6, dst, sizeof(dst6)); |
| 577 | |
| 578 | memset(&m, 0, sizeof(m)); |
| 579 | m.msg_name = (caddr_t)&dst6; |
| 580 | m.msg_namelen = sizeof(dst6); |
| 581 | iov[0].iov_base = (char *)buf; |
| 582 | iov[0].iov_len = buflen; |
| 583 | m.msg_iov = iov; |
| 584 | m.msg_iovlen = 1; |
| 585 | |
| 586 | memset(cmsgbuf, 0, sizeof(cmsgbuf)); |
| 587 | cm = (struct cmsghdr *)cmsgbuf; |
| 588 | m.msg_control = (caddr_t)cm; |
| 589 | m.msg_controllen = CMSG_SPACE(sizeof(struct in_pktinfo)); |
| 590 | |
| 591 | cm->cmsg_len = CMSG_LEN(sizeof(struct in_pktinfo)); |
| 592 | cm->cmsg_level = IPPROTO_IP; |
| 593 | cm->cmsg_type = IP_PKTINFO; |
| 594 | pi = (struct in_pktinfo *)CMSG_DATA(cm); |
| 595 | memcpy(&pi->ipi_spec_dst, &src6.sin_addr, sizeof(src6.sin_addr)); |
| 596 | pi->ipi_ifindex = ifindex; |
| 597 | |
| 598 | plog(LLV_DEBUG, LOCATION, NULL, |
| 599 | "src4 %s\n", |
| 600 | saddr2str((struct sockaddr *)&src6)); |
| 601 | plog(LLV_DEBUG, LOCATION, NULL, |
| 602 | "dst4 %s\n", |
| 603 | saddr2str((struct sockaddr *)&dst6)); |
| 604 | |
| 605 | for (i = 0; i < cnt; i++) { |
| 606 | len = sendmsg(s, &m, 0 /*MSG_DONTROUTE*/); |
| 607 | if (len < 0) { |
| 608 | plog(LLV_ERROR, LOCATION, NULL, |
| 609 | "sendmsg (%s)\n", strerror(errno)); |
| 610 | return -1; |
| 611 | } |
| 612 | plog(LLV_DEBUG, LOCATION, NULL, |
| 613 | "%d times of %d bytes message will be sent " |
| 614 | "to %s\n", |
| 615 | i + 1, len, saddr2str(dst)); |
| 616 | } |
| 617 | plogdump(LLV_DEBUG, (char *)buf, buflen); |
| 618 | |
| 619 | return len; |
| 620 | } |
| 621 | #endif /* __linux__ */ |
| 622 | default: |
| 623 | { |
| 624 | int needclose = 0; |
| 625 | int sendsock; |
| 626 | |
| 627 | if (ss.ss_family == src->sa_family && memcmp(&ss, src, sysdep_sa_len(src)) == 0) { |
| 628 | sendsock = s; |
| 629 | needclose = 0; |
| 630 | } else { |
| 631 | int yes = 1; |
| 632 | /* |
| 633 | * Use newly opened socket for sending packets. |
| 634 | * NOTE: this is unsafe, because if the peer is quick enough |
| 635 | * the packet from the peer may be queued into sendsock. |
| 636 | * Better approach is to prepare bind'ed udp sockets for |
| 637 | * each of the interface addresses. |
| 638 | */ |
| 639 | sendsock = socket(src->sa_family, SOCK_DGRAM, 0); |
| 640 | if (sendsock < 0) { |
| 641 | plog(LLV_ERROR, LOCATION, NULL, |
| 642 | "socket (%s)\n", strerror(errno)); |
| 643 | return -1; |
| 644 | } |
| 645 | if (setsockopt(sendsock, SOL_SOCKET, |
| 646 | #ifdef __linux__ |
| 647 | SO_REUSEADDR, |
| 648 | #else |
| 649 | SO_REUSEPORT, |
| 650 | #endif |
| 651 | (void *)&yes, sizeof(yes)) < 0) { |
| 652 | plog(LLV_ERROR, LOCATION, NULL, |
| 653 | "setsockopt SO_REUSEPORT (%s)\n", |
| 654 | strerror(errno)); |
| 655 | close(sendsock); |
| 656 | return -1; |
| 657 | } |
| 658 | #ifdef IPV6_USE_MIN_MTU |
| 659 | if (src->sa_family == AF_INET6 && |
| 660 | setsockopt(sendsock, IPPROTO_IPV6, IPV6_USE_MIN_MTU, |
| 661 | (void *)&yes, sizeof(yes)) < 0) { |
| 662 | plog(LLV_ERROR, LOCATION, NULL, |
| 663 | "setsockopt IPV6_USE_MIN_MTU (%s)\n", |
| 664 | strerror(errno)); |
| 665 | close(sendsock); |
| 666 | return -1; |
| 667 | } |
| 668 | #endif |
| 669 | if (setsockopt_bypass(sendsock, src->sa_family) < 0) { |
| 670 | close(sendsock); |
| 671 | return -1; |
| 672 | } |
| 673 | |
| 674 | if (bind(sendsock, (struct sockaddr *)src, sysdep_sa_len(src)) < 0) { |
| 675 | plog(LLV_ERROR, LOCATION, NULL, |
| 676 | "bind 1 (%s)\n", strerror(errno)); |
| 677 | close(sendsock); |
| 678 | return -1; |
| 679 | } |
| 680 | needclose = 1; |
| 681 | } |
| 682 | |
| 683 | for (i = 0; i < cnt; i++) { |
| 684 | len = sendto(sendsock, buf, buflen, 0, dst, sysdep_sa_len(dst)); |
| 685 | if (len < 0) { |
| 686 | plog(LLV_ERROR, LOCATION, NULL, |
| 687 | "sendto (%s)\n", strerror(errno)); |
| 688 | if (needclose) |
| 689 | close(sendsock); |
| 690 | return len; |
| 691 | } |
| 692 | plog(LLV_DEBUG, LOCATION, NULL, |
| 693 | "%d times of %d bytes message will be sent " |
| 694 | "to %s\n", |
| 695 | i + 1, len, saddr2str(dst)); |
| 696 | } |
| 697 | plogdump(LLV_DEBUG, (char *)buf, buflen); |
| 698 | |
| 699 | if (needclose) |
| 700 | close(sendsock); |
| 701 | |
| 702 | return len; |
| 703 | } |
| 704 | } |
| 705 | } |
| 706 | |
| 707 | int |
| 708 | setsockopt_bypass(so, family) |
| 709 | int so, family; |
| 710 | { |
| 711 | int level; |
| 712 | char *buf; |
| 713 | char *policy; |
| 714 | |
| 715 | switch (family) { |
| 716 | case AF_INET: |
| 717 | level = IPPROTO_IP; |
| 718 | break; |
| 719 | #ifdef INET6 |
| 720 | case AF_INET6: |
| 721 | level = IPPROTO_IPV6; |
| 722 | break; |
| 723 | #endif |
| 724 | default: |
| 725 | plog(LLV_ERROR, LOCATION, NULL, |
| 726 | "unsupported address family %d\n", family); |
| 727 | return -1; |
| 728 | } |
| 729 | |
| 730 | policy = "in bypass"; |
| 731 | buf = ipsec_set_policy(policy, strlen(policy)); |
| 732 | if (buf == NULL) { |
| 733 | plog(LLV_ERROR, LOCATION, NULL, |
| 734 | "ipsec_set_policy (%s)\n", |
| 735 | ipsec_strerror()); |
| 736 | return -1; |
| 737 | } |
| 738 | if (setsockopt(so, level, |
| 739 | (level == IPPROTO_IP ? |
| 740 | IP_IPSEC_POLICY : IPV6_IPSEC_POLICY), |
| 741 | buf, ipsec_get_policylen(buf)) < 0) { |
| 742 | plog(LLV_ERROR, LOCATION, NULL, |
| 743 | "setsockopt IP_IPSEC_POLICY (%s)\n", |
| 744 | strerror(errno)); |
| 745 | return -1; |
| 746 | } |
| 747 | racoon_free(buf); |
| 748 | |
| 749 | policy = "out bypass"; |
| 750 | buf = ipsec_set_policy(policy, strlen(policy)); |
| 751 | if (buf == NULL) { |
| 752 | plog(LLV_ERROR, LOCATION, NULL, |
| 753 | "ipsec_set_policy (%s)\n", |
| 754 | ipsec_strerror()); |
| 755 | return -1; |
| 756 | } |
| 757 | if (setsockopt(so, level, |
| 758 | (level == IPPROTO_IP ? |
| 759 | IP_IPSEC_POLICY : IPV6_IPSEC_POLICY), |
| 760 | buf, ipsec_get_policylen(buf)) < 0) { |
| 761 | plog(LLV_ERROR, LOCATION, NULL, |
| 762 | "setsockopt IP_IPSEC_POLICY (%s)\n", |
| 763 | strerror(errno)); |
| 764 | return -1; |
| 765 | } |
| 766 | racoon_free(buf); |
| 767 | |
| 768 | return 0; |
| 769 | } |
| 770 | |
| 771 | struct sockaddr * |
| 772 | newsaddr(len) |
| 773 | int len; |
| 774 | { |
| 775 | struct sockaddr *new; |
| 776 | |
| 777 | if ((new = racoon_calloc(1, len)) == NULL) { |
| 778 | plog(LLV_ERROR, LOCATION, NULL, |
| 779 | "%s\n", strerror(errno)); |
| 780 | goto out; |
| 781 | } |
| 782 | |
| 783 | #ifdef __linux__ |
| 784 | if (len == sizeof (struct sockaddr_in6)) |
| 785 | new->sa_family = AF_INET6; |
| 786 | else |
| 787 | new->sa_family = AF_INET; |
| 788 | #else |
| 789 | /* initial */ |
| 790 | new->sa_len = len; |
| 791 | #endif |
| 792 | out: |
| 793 | return new; |
| 794 | } |
| 795 | |
| 796 | struct sockaddr * |
| 797 | dupsaddr(src) |
| 798 | struct sockaddr *src; |
| 799 | { |
| 800 | struct sockaddr *dst; |
| 801 | |
| 802 | dst = racoon_calloc(1, sysdep_sa_len(src)); |
| 803 | if (dst == NULL) { |
| 804 | plog(LLV_ERROR, LOCATION, NULL, |
| 805 | "%s\n", strerror(errno)); |
| 806 | return NULL; |
| 807 | } |
| 808 | |
| 809 | memcpy(dst, src, sysdep_sa_len(src)); |
| 810 | |
| 811 | return dst; |
| 812 | } |
| 813 | |
| 814 | char * |
| 815 | saddr2str(saddr) |
| 816 | const struct sockaddr *saddr; |
| 817 | { |
| 818 | static char buf[NI_MAXHOST + NI_MAXSERV + 10]; |
| 819 | char addr[NI_MAXHOST], port[NI_MAXSERV]; |
| 820 | |
| 821 | if (saddr == NULL) |
| 822 | return NULL; |
| 823 | |
| 824 | if (saddr->sa_family == AF_UNSPEC) |
| 825 | snprintf (buf, sizeof(buf), "%s", "anonymous"); |
| 826 | else { |
| 827 | GETNAMEINFO(saddr, addr, port); |
| 828 | snprintf(buf, sizeof(buf), "%s[%s]", addr, port); |
| 829 | } |
| 830 | |
| 831 | return buf; |
| 832 | } |
| 833 | |
| 834 | char * |
| 835 | saddrwop2str(saddr) |
| 836 | const struct sockaddr *saddr; |
| 837 | { |
| 838 | static char buf[NI_MAXHOST + NI_MAXSERV + 10]; |
| 839 | char addr[NI_MAXHOST]; |
| 840 | |
| 841 | if (saddr == NULL) |
| 842 | return NULL; |
| 843 | |
| 844 | GETNAMEINFO_NULL(saddr, addr); |
| 845 | snprintf(buf, sizeof(buf), "%s", addr); |
| 846 | |
| 847 | return buf; |
| 848 | } |
| 849 | |
| 850 | char * |
| 851 | naddrwop2str(const struct netaddr *naddr) |
| 852 | { |
| 853 | static char buf[NI_MAXHOST + 10]; |
| 854 | static const struct sockaddr sa_any; /* this is initialized to all zeros */ |
| 855 | |
| 856 | if (naddr == NULL) |
| 857 | return NULL; |
| 858 | |
| 859 | if (memcmp(&naddr->sa, &sa_any, sizeof(sa_any)) == 0) |
| 860 | snprintf(buf, sizeof(buf), "%s", "any"); |
| 861 | else { |
| 862 | snprintf(buf, sizeof(buf), "%s", saddrwop2str(&naddr->sa.sa)); |
| 863 | snprintf(&buf[strlen(buf)], sizeof(buf) - strlen(buf), "/%ld", naddr->prefix); |
| 864 | } |
| 865 | return buf; |
| 866 | } |
| 867 | |
| 868 | char * |
| 869 | naddrwop2str_fromto(const char *format, const struct netaddr *saddr, |
| 870 | const struct netaddr *daddr) |
| 871 | { |
| 872 | static char buf[2*(NI_MAXHOST + NI_MAXSERV + 10) + 100]; |
| 873 | char *src, *dst; |
| 874 | |
| 875 | src = racoon_strdup(naddrwop2str(saddr)); |
| 876 | dst = racoon_strdup(naddrwop2str(daddr)); |
| 877 | STRDUP_FATAL(src); |
| 878 | STRDUP_FATAL(dst); |
| 879 | /* WARNING: Be careful about the format string! Don't |
| 880 | ever pass in something that a user can modify!!! */ |
| 881 | snprintf (buf, sizeof(buf), format, src, dst); |
| 882 | racoon_free (src); |
| 883 | racoon_free (dst); |
| 884 | |
| 885 | return buf; |
| 886 | } |
| 887 | |
| 888 | char * |
| 889 | saddr2str_fromto(format, saddr, daddr) |
| 890 | const char *format; |
| 891 | const struct sockaddr *saddr; |
| 892 | const struct sockaddr *daddr; |
| 893 | { |
| 894 | static char buf[2*(NI_MAXHOST + NI_MAXSERV + 10) + 100]; |
| 895 | char *src, *dst; |
| 896 | |
| 897 | src = racoon_strdup(saddr2str(saddr)); |
| 898 | dst = racoon_strdup(saddr2str(daddr)); |
| 899 | STRDUP_FATAL(src); |
| 900 | STRDUP_FATAL(dst); |
| 901 | /* WARNING: Be careful about the format string! Don't |
| 902 | ever pass in something that a user can modify!!! */ |
| 903 | snprintf (buf, sizeof(buf), format, src, dst); |
| 904 | racoon_free (src); |
| 905 | racoon_free (dst); |
| 906 | |
| 907 | return buf; |
| 908 | } |
| 909 | |
| 910 | struct sockaddr * |
| 911 | str2saddr(host, port) |
| 912 | char *host; |
| 913 | char *port; |
| 914 | { |
| 915 | struct addrinfo hints, *res; |
| 916 | struct sockaddr *saddr; |
| 917 | int error; |
| 918 | |
| 919 | memset(&hints, 0, sizeof(hints)); |
| 920 | hints.ai_family = PF_UNSPEC; |
| 921 | hints.ai_socktype = SOCK_DGRAM; |
| 922 | hints.ai_flags = AI_NUMERICHOST; |
| 923 | error = getaddrinfo(host, port, &hints, &res); |
| 924 | if (error != 0) { |
| 925 | plog(LLV_ERROR, LOCATION, NULL, |
| 926 | "getaddrinfo(%s%s%s): %s\n", |
| 927 | host, port ? "," : "", port ? port : "", |
| 928 | gai_strerror(error)); |
| 929 | return NULL; |
| 930 | } |
| 931 | if (res->ai_next != NULL) { |
| 932 | plog(LLV_WARNING, LOCATION, NULL, |
| 933 | "getaddrinfo(%s%s%s): " |
| 934 | "resolved to multiple address, " |
| 935 | "taking the first one\n", |
| 936 | host, port ? "," : "", port ? port : ""); |
| 937 | } |
| 938 | saddr = racoon_malloc(res->ai_addrlen); |
| 939 | if (saddr == NULL) { |
| 940 | plog(LLV_ERROR, LOCATION, NULL, |
| 941 | "failed to allocate buffer.\n"); |
| 942 | freeaddrinfo(res); |
| 943 | return NULL; |
| 944 | } |
| 945 | memcpy(saddr, res->ai_addr, res->ai_addrlen); |
| 946 | freeaddrinfo(res); |
| 947 | |
| 948 | return saddr; |
| 949 | } |
| 950 | |
| 951 | void |
| 952 | mask_sockaddr(a, b, l) |
| 953 | struct sockaddr *a; |
| 954 | const struct sockaddr *b; |
| 955 | size_t l; |
| 956 | { |
| 957 | size_t i; |
| 958 | u_int8_t *p, alen; |
| 959 | |
| 960 | switch (b->sa_family) { |
| 961 | case AF_INET: |
| 962 | alen = sizeof(struct in_addr); |
| 963 | p = (u_int8_t *)&((struct sockaddr_in *)a)->sin_addr; |
| 964 | break; |
| 965 | #ifdef INET6 |
| 966 | case AF_INET6: |
| 967 | alen = sizeof(struct in6_addr); |
| 968 | p = (u_int8_t *)&((struct sockaddr_in6 *)a)->sin6_addr; |
| 969 | break; |
| 970 | #endif |
| 971 | default: |
| 972 | plog(LLV_ERROR, LOCATION, NULL, |
| 973 | "invalid family: %d\n", b->sa_family); |
| 974 | exit(1); |
| 975 | } |
| 976 | |
| 977 | if ((alen << 3) < l) { |
| 978 | plog(LLV_ERROR, LOCATION, NULL, |
| 979 | "unexpected inconsistency: %d %zu\n", b->sa_family, l); |
| 980 | exit(1); |
| 981 | } |
| 982 | |
| 983 | memcpy(a, b, sysdep_sa_len(b)); |
| 984 | p[l / 8] &= (0xff00 >> (l % 8)) & 0xff; |
| 985 | for (i = l / 8 + 1; i < alen; i++) |
| 986 | p[i] = 0x00; |
| 987 | } |
| 988 | |
| 989 | /* Compute a score describing how "accurate" a netaddr is for a given sockaddr. |
| 990 | * Examples: |
| 991 | * Return values for address 10.20.30.40 [port 500] and given netaddresses... |
| 992 | * 10.10.0.0/16 => -1 ... doesn't match |
| 993 | * 0.0.0.0/0 => 0 ... matches, but only 0 bits. |
| 994 | * 10.20.0.0/16 => 16 ... 16 bits match |
| 995 | * 10.20.30.0/24 => 24 ... guess what ;-) |
| 996 | * 10.20.30.40/32 => 32 ... whole address match |
| 997 | * 10.20.30.40:500 => 33 ... both address and port match |
| 998 | * 10.20.30.40:501 => -1 ... port doesn't match and isn't 0 (=any) |
| 999 | */ |
| 1000 | int |
| 1001 | naddr_score(const struct netaddr *naddr, const struct sockaddr *saddr) |
| 1002 | { |
| 1003 | static const struct netaddr naddr_any; /* initialized to all-zeros */ |
| 1004 | struct sockaddr sa; |
| 1005 | u_int16_t naddr_port, saddr_port; |
| 1006 | int port_score; |
| 1007 | |
| 1008 | if (!naddr || !saddr) { |
| 1009 | plog(LLV_ERROR, LOCATION, NULL, |
| 1010 | "Call with null args: naddr=%p, saddr=%p\n", |
| 1011 | naddr, saddr); |
| 1012 | return -1; |
| 1013 | } |
| 1014 | |
| 1015 | /* Wildcard address matches, but only 0 bits. */ |
| 1016 | if (memcmp(naddr, &naddr_any, sizeof(naddr_any)) == 0) |
| 1017 | return 0; |
| 1018 | |
| 1019 | /* If families don't match we really can't do much... */ |
| 1020 | if (naddr->sa.sa.sa_family != saddr->sa_family) |
| 1021 | return -1; |
| 1022 | |
| 1023 | /* If port check fail don't bother to check addresses. */ |
| 1024 | naddr_port = extract_port(&naddr->sa.sa); |
| 1025 | saddr_port = extract_port(saddr); |
| 1026 | if (naddr_port == 0 || saddr_port == 0) /* wildcard match */ |
| 1027 | port_score = 0; |
| 1028 | else if (naddr_port == saddr_port) /* exact match */ |
| 1029 | port_score = 1; |
| 1030 | else /* mismatch :-) */ |
| 1031 | return -1; |
| 1032 | |
| 1033 | /* Here it comes - compare network addresses. */ |
| 1034 | mask_sockaddr(&sa, saddr, naddr->prefix); |
| 1035 | if (loglevel >= LLV_DEBUG) { /* debug only */ |
| 1036 | char *a1, *a2, *a3; |
| 1037 | a1 = racoon_strdup(naddrwop2str(naddr)); |
| 1038 | a2 = racoon_strdup(saddrwop2str(saddr)); |
| 1039 | a3 = racoon_strdup(saddrwop2str(&sa)); |
| 1040 | STRDUP_FATAL(a1); |
| 1041 | STRDUP_FATAL(a2); |
| 1042 | STRDUP_FATAL(a3); |
| 1043 | plog(LLV_DEBUG, LOCATION, NULL, |
| 1044 | "naddr=%s, saddr=%s (masked=%s)\n", |
| 1045 | a1, a2, a3); |
| 1046 | free(a1); |
| 1047 | free(a2); |
| 1048 | free(a3); |
| 1049 | } |
| 1050 | if (cmpsaddrwop(&sa, &naddr->sa.sa) == 0) |
| 1051 | return naddr->prefix + port_score; |
| 1052 | |
| 1053 | return -1; |
| 1054 | } |
| 1055 | |
| 1056 | /* Some usefull functions for sockaddr port manipulations. */ |
| 1057 | u_int16_t |
| 1058 | extract_port (const struct sockaddr *addr) |
| 1059 | { |
| 1060 | u_int16_t port = 0; |
| 1061 | |
| 1062 | if (!addr) |
| 1063 | return port; |
| 1064 | |
| 1065 | switch (addr->sa_family) { |
| 1066 | case AF_INET: |
| 1067 | port = ((struct sockaddr_in *)addr)->sin_port; |
| 1068 | break; |
| 1069 | case AF_INET6: |
| 1070 | port = ((struct sockaddr_in6 *)addr)->sin6_port; |
| 1071 | break; |
| 1072 | default: |
| 1073 | plog(LLV_ERROR, LOCATION, NULL, "unknown AF: %u\n", addr->sa_family); |
| 1074 | break; |
| 1075 | } |
| 1076 | |
| 1077 | return ntohs(port); |
| 1078 | } |
| 1079 | |
| 1080 | u_int16_t * |
| 1081 | get_port_ptr (struct sockaddr *addr) |
| 1082 | { |
| 1083 | u_int16_t *port_ptr; |
| 1084 | |
| 1085 | if (!addr) |
| 1086 | return NULL; |
| 1087 | |
| 1088 | switch (addr->sa_family) { |
| 1089 | case AF_INET: |
| 1090 | port_ptr = &(((struct sockaddr_in *)addr)->sin_port); |
| 1091 | break; |
| 1092 | case AF_INET6: |
| 1093 | port_ptr = &(((struct sockaddr_in6 *)addr)->sin6_port); |
| 1094 | break; |
| 1095 | default: |
| 1096 | plog(LLV_ERROR, LOCATION, NULL, "unknown AF: %u\n", addr->sa_family); |
| 1097 | return NULL; |
| 1098 | break; |
| 1099 | } |
| 1100 | |
| 1101 | return port_ptr; |
| 1102 | } |
| 1103 | |
| 1104 | u_int16_t * |
| 1105 | set_port (struct sockaddr *addr, u_int16_t new_port) |
| 1106 | { |
| 1107 | u_int16_t *port_ptr; |
| 1108 | |
| 1109 | port_ptr = get_port_ptr (addr); |
| 1110 | |
| 1111 | if (port_ptr) |
| 1112 | *port_ptr = htons(new_port); |
| 1113 | |
| 1114 | return port_ptr; |
| 1115 | } |