9a006fe9a05abc03c0e81977802194dab8737206Tinderbox User * Copyright (C) 1999-2005, 2007, 2008, 2012-2017 Internet Systems Consortium, Inc. ("ISC")
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * This Source Code Form is subject to the terms of the Mozilla Public
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * License, v. 2.0. If a copy of the MPL was not distributed with this
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * file, You can obtain one at http://mozilla.org/MPL/2.0/.
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 * Definitions about UDP port range specification. This is a total mess of
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 * portability variants: some use sysctl (but the sysctl names vary), some use
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 * system-specific interfaces, some have the same interface for IPv4 and IPv6,
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 * some separate them, etc...
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 * The last resort defaults: use all non well known port space
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉#endif /* ISC_NET_PORTRANGELOW */
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉#endif /* ISC_NET_PORTRANGEHIGH */
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 * sysctl variants
64ecc88345766e124e1fb053dbf4e36550471f83Evan Hunt#if defined(__FreeBSD__) || defined(__APPLE__) || defined(__DragonFly__)
64ecc88345766e124e1fb053dbf4e36550471f83Evan Hunt#define SYSCTL_V4PORTRANGE_LOW "net.inet.ip.portrange.hifirst"
64ecc88345766e124e1fb053dbf4e36550471f83Evan Hunt#define SYSCTL_V4PORTRANGE_HIGH "net.inet.ip.portrange.hilast"
64ecc88345766e124e1fb053dbf4e36550471f83Evan Hunt#define SYSCTL_V6PORTRANGE_LOW "net.inet.ip.portrange.hifirst"
64ecc88345766e124e1fb053dbf4e36550471f83Evan Hunt#define SYSCTL_V6PORTRANGE_HIGH "net.inet.ip.portrange.hilast"
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉#define SYSCTL_V4PORTRANGE_LOW "net.inet.ip.anonportmin"
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉#define SYSCTL_V4PORTRANGE_HIGH "net.inet.ip.anonportmax"
64ecc88345766e124e1fb053dbf4e36550471f83Evan Hunt#define SYSCTL_V6PORTRANGE_LOW "net.inet6.ip6.anonportmin"
64ecc88345766e124e1fb053dbf4e36550471f83Evan Hunt#define SYSCTL_V6PORTRANGE_HIGH "net.inet6.ip6.anonportmax"
64ecc88345766e124e1fb053dbf4e36550471f83Evan Hunt#else /* !HAVE_SYSCTLBYNAME */
64ecc88345766e124e1fb053dbf4e36550471f83Evan Hunt#define SYSCTL_V4PORTRANGE_LOW { CTL_NET, PF_INET, IPPROTO_IP, \
64ecc88345766e124e1fb053dbf4e36550471f83Evan Hunt#define SYSCTL_V4PORTRANGE_HIGH { CTL_NET, PF_INET, IPPROTO_IP, \
64ecc88345766e124e1fb053dbf4e36550471f83Evan Hunt/* Same for IPv6 */
64ecc88345766e124e1fb053dbf4e36550471f83Evan Hunt#define SYSCTL_V6PORTRANGE_LOW SYSCTL_V4PORTRANGE_LOW
64ecc88345766e124e1fb053dbf4e36550471f83Evan Hunt#define SYSCTL_V6PORTRANGE_HIGH SYSCTL_V4PORTRANGE_HIGH
64ecc88345766e124e1fb053dbf4e36550471f83Evan Hunt#endif /* HAVE_SYSCTLBYNAME */
435532822dc571a904207d8176e063cc8731eef5Bob Halleyconst struct in6_addr isc_net_in6addrany = IN6ADDR_ANY_INIT;
9d99e3fda11eb8549a2bc7d5af31b7630ed68162Mark Andrewsconst struct in6_addr isc_net_in6addrloop = IN6ADDR_LOOPBACK_INIT;
aa39170da817cae7b4c6c735cc832e05ec3d2351Mark Andrewsstatic isc_once_t once_ipv6only = ISC_ONCE_INIT;
cc32d38366fa680fc29f9fb17bd3ebe515835445Mark Andrewsstatic isc_once_t once_ipv6pktinfo = ISC_ONCE_INIT;
9effea437d7973c9ed981ab2e14b219182d86ee7Curtis Blackburn# endif /* WANT_IPV6 */
7de2c6e6d51f38daeb2d346f3f21dc01ccece6daEvan Hunt#endif /* ISC_PLATFORM_HAVEIPV6 */
9effea437d7973c9ed981ab2e14b219182d86ee7Curtis Blackburn#define ISC_CMSG_IP_TOS 0 /* As of 10.8.2. */
9effea437d7973c9ed981ab2e14b219182d86ee7Curtis Blackburn#else /* ! __APPLE__ */
9effea437d7973c9ed981ab2e14b219182d86ee7Curtis Blackburn#endif /* ! __APPLE__ */
9effea437d7973c9ed981ab2e14b219182d86ee7Curtis Blackburn#endif /* ! ISC_CMSG_IP_TOS */
4423c99613db1399dbb5c51e86ef0d351a1418c2Mark Andrewsstatic isc_result_t unix_result = ISC_R_NOTFOUND;
aa39170da817cae7b4c6c735cc832e05ec3d2351Mark Andrewsstatic isc_result_t ipv6only_result = ISC_R_NOTFOUND;
cc32d38366fa680fc29f9fb17bd3ebe515835445Mark Andrewsstatic isc_result_t ipv6pktinfo_result = ISC_R_NOTFOUND;
e08daafffce8fb74ffca8ecf5772e334299ccf76Bob Halley if (s == -1) {
9cd6d409b78a6f833b681c13a68fbdc7c024fe66David Lawrence "socket() %s: %s",
64a84169d7eed05486b10be90afea58f4af146f9Brian Wellington * Check to see if IPv6 is broken, as is common on Linux.
2d67fbd786284ed570031fa82fc9807b0413aa3fBrian Wellington if (getsockname(s, (struct sockaddr *)&sin6, (void *)&len) < 0)
64a84169d7eed05486b10be90afea58f4af146f9Brian Wellington isc_log_write(isc_lctx, ISC_LOGCATEGORY_GENERAL,
9efa0d2ae311157ed1cce3c16129579111b1e3b0Andreas Gustafsson "retrieving the address of an IPv6 "
64a84169d7eed05486b10be90afea58f4af146f9Brian Wellington "socket from the kernel failed.");
64a84169d7eed05486b10be90afea58f4af146f9Brian Wellington isc_log_write(isc_lctx, ISC_LOGCATEGORY_GENERAL,
0ffaee887ff5674b8c3bb0435ae838f641981706Mark Andrews "IPv6 is not supported.");
64a84169d7eed05486b10be90afea58f4af146f9Brian Wellington "IPv6 structures in kernel and "
64a84169d7eed05486b10be90afea58f4af146f9Brian Wellington "user space do not match.");
0ffaee887ff5674b8c3bb0435ae838f641981706Mark Andrews "IPv6 is not supported.");
e08daafffce8fb74ffca8ecf5772e334299ccf76Bob Halley RUNTIME_CHECK(isc_once_do(&once, initialize_action) == ISC_R_SUCCESS);
aa39170da817cae7b4c6c735cc832e05ec3d2351Mark Andrews /* check for TCP sockets */
aa39170da817cae7b4c6c735cc832e05ec3d2351Mark Andrews if (s == -1) {
aa39170da817cae7b4c6c735cc832e05ec3d2351Mark Andrews "socket() %s: %s",
aa39170da817cae7b4c6c735cc832e05ec3d2351Mark Andrews if (setsockopt(s, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof(on)) < 0) {
aa39170da817cae7b4c6c735cc832e05ec3d2351Mark Andrews /* check for UDP sockets */
aa39170da817cae7b4c6c735cc832e05ec3d2351Mark Andrews if (s == -1) {
aa39170da817cae7b4c6c735cc832e05ec3d2351Mark Andrews "socket() %s: %s",
aa39170da817cae7b4c6c735cc832e05ec3d2351Mark Andrews if (setsockopt(s, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof(on)) < 0) {
cc32d38366fa680fc29f9fb17bd3ebe515835445Mark Andrews#endif /* IPV6_V6ONLY */
7de2c6e6d51f38daeb2d346f3f21dc01ccece6daEvan Hunt#endif /* WANT_IPV6 */
cc32d38366fa680fc29f9fb17bd3ebe515835445Mark Andrews /* we only use this for UDP sockets */
cc32d38366fa680fc29f9fb17bd3ebe515835445Mark Andrews if (s == -1) {
cc32d38366fa680fc29f9fb17bd3ebe515835445Mark Andrews "socket() %s: %s",
cc32d38366fa680fc29f9fb17bd3ebe515835445Mark Andrews if (setsockopt(s, IPPROTO_IPV6, optname, &on, sizeof(on)) < 0) {
d8f2dd46cba3a16c2433e85657a5b15543013ca6Mark Andrews#endif /* WANT_IPV6 */
4f082b58b17ce39087930d5affc2ada90ef386e6Mark Andrews#endif /* ISC_PLATFORM_HAVEIN6PKTINFO */
7de2c6e6d51f38daeb2d346f3f21dc01ccece6daEvan Hunt#endif /* ISC_PLATFORM_HAVEIPV6 */
9effea437d7973c9ed981ab2e14b219182d86ee7Curtis Blackburn defined(ISC_NET_BSD44MSGHDR) && defined(IPV6_TCLASS) && defined(WANT_IPV6)
313b0ea9f258edd8530f4454c69e6ba194280162Tinderbox User * Cast NULL so that any pointer arithmetic performed by CMSG_DATA
313b0ea9f258edd8530f4454c69e6ba194280162Tinderbox User * is correct.
313b0ea9f258edd8530f4454c69e6ba194280162Tinderbox User hdrlen = (ISC_SOCKADDR_LEN_T)CMSG_DATA(((struct cmsghdr *)NULL));
313b0ea9f258edd8530f4454c69e6ba194280162Tinderbox User * XXX: The buffer length is an ad-hoc value, but should be enough
313b0ea9f258edd8530f4454c69e6ba194280162Tinderbox User * in a practical sense.
313b0ea9f258edd8530f4454c69e6ba194280162Tinderbox User char dummybuf[sizeof(struct cmsghdr) + 1024];
313b0ea9f258edd8530f4454c69e6ba194280162Tinderbox User return ((char *)cmsgp - (char *)msg.msg_control);
d8c1e4eccf5bf88a3b4f8dc4a24391b5712bb3a3Mark Andrews * Make a fd non-blocking.
0a00c3eb03464988821a0b6817b63ddb3c090890Tinderbox User isc__strerror(errno, strbuf, sizeof(strbuf));
67adc03ef81fb610f8df093b17f55275ee816754Evan Huntcmsgsend(int s, int level, int type, struct addrinfo *res) {
67adc03ef81fb610f8df093b17f55275ee816754Evan Hunt unsigned char b[256];
fd2f4551d9498e1dce8e44a24e5e886ef2aa75cbMark Andrews int dscp = (46 << 2); /* Expedited forwarding. */
67adc03ef81fb610f8df093b17f55275ee816754Evan Hunt if (getsockname(s, (struct sockaddr *)&ss, &len) < 0) {
b5edc023a1bb8066d86777dd40fd4555e285de57Mark Andrews msg.msg_controllen += cmsg_space(sizeof(char));
b5edc023a1bb8066d86777dd40fd4555e285de57Mark Andrews msg.msg_controllen += cmsg_space(sizeof(dscp));
fd2f4551d9498e1dce8e44a24e5e886ef2aa75cbMark Andrews typestr = (type == IP_TOS) ? "IP_TOS" : "IPV6_TCLASS";
fd2f4551d9498e1dce8e44a24e5e886ef2aa75cbMark Andrews msgstr = isc_msgcat_get(isc_msgcat, ISC_MSGSET_GENERAL,
fd2f4551d9498e1dce8e44a24e5e886ef2aa75cbMark Andrews UNEXPECTED_ERROR(__FILE__, __LINE__, "probing "
fd2f4551d9498e1dce8e44a24e5e886ef2aa75cbMark Andrews "sendmsg() with %s=%02x %s: %s",
d8c1e4eccf5bf88a3b4f8dc4a24391b5712bb3a3Mark Andrews * Make sure the message actually got sent.
89cf81b4625c574f60c21e0dce12b150f3c5583cMark Andrews#endif /* IP_RECVTOS */
67adc03ef81fb610f8df093b17f55275ee816754Evan Hunt n = getaddrinfo("127.0.0.1", NULL, &hints, &res0);
67adc03ef81fb610f8df093b17f55275ee816754Evan Hunt s = socket(res0->ai_family, res0->ai_socktype, res0->ai_protocol);
67adc03ef81fb610f8df093b17f55275ee816754Evan Hunt if (s == -1) {
67adc03ef81fb610f8df093b17f55275ee816754Evan Hunt if (setsockopt(s, IPPROTO_IP, IP_TOS, &dscp, sizeof(dscp)) == 0)
67adc03ef81fb610f8df093b17f55275ee816754Evan Hunt if (setsockopt(s, IPPROTO_IP, IP_RECVTOS, &on, sizeof(on)) == 0)
89cf81b4625c574f60c21e0dce12b150f3c5583cMark Andrews#endif /* IP_RECVTOS */
89cf81b4625c574f60c21e0dce12b150f3c5583cMark Andrews#endif /* ISC_CMSG_IP_TOS */
89cf81b4625c574f60c21e0dce12b150f3c5583cMark Andrews#endif /* ISC_NET_BSD44MSGHDR */
89cf81b4625c574f60c21e0dce12b150f3c5583cMark Andrews#endif /* IP_TOS */
89cf81b4625c574f60c21e0dce12b150f3c5583cMark Andrews#endif /* IPV6_RECVTCLASS */
67adc03ef81fb610f8df093b17f55275ee816754Evan Hunt s = socket(res0->ai_family, res0->ai_socktype, res0->ai_protocol);
67adc03ef81fb610f8df093b17f55275ee816754Evan Hunt if (s == -1) {
67adc03ef81fb610f8df093b17f55275ee816754Evan Hunt if (setsockopt(s, IPPROTO_IPV6, IPV6_TCLASS, &dscp, sizeof(dscp)) == 0)
67adc03ef81fb610f8df093b17f55275ee816754Evan Hunt if (setsockopt(s, IPPROTO_IPV6, IPV6_RECVTCLASS, &on, sizeof(on)) == 0)
89cf81b4625c574f60c21e0dce12b150f3c5583cMark Andrews#endif /* IPV6_RECVTCLASS */
89cf81b4625c574f60c21e0dce12b150f3c5583cMark Andrews#endif /* ISC_NET_BSD44MSGHDR */
89cf81b4625c574f60c21e0dce12b150f3c5583cMark Andrews#endif /* IPV6_TCLASS */
89cf81b4625c574f60c21e0dce12b150f3c5583cMark Andrews#endif /* WANT_IPV6 */
89cf81b4625c574f60c21e0dce12b150f3c5583cMark Andrews#endif /* ISC_PLATFORM_HAVEIPV6 */
67adc03ef81fb610f8df093b17f55275ee816754Evan Hunt RUNTIME_CHECK(isc_once_do(&once_dscp, try_dscp) == ISC_R_SUCCESS);
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉getudpportrange_sysctl(int af, in_port_t *low, in_port_t *high) {
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 const char *sysctlname_lowport, *sysctlname_hiport;
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 sysctlname_lowport = SYSCTL_V4PORTRANGE_LOW;
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 sysctlname_hiport = SYSCTL_V4PORTRANGE_HIGH;
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 sysctlname_lowport = SYSCTL_V6PORTRANGE_LOW;
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 sysctlname_hiport = SYSCTL_V6PORTRANGE_HIGH;
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 if (sysctlbyname(sysctlname_lowport, &port_low, &portlen,
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 if (sysctlbyname(sysctlname_hiport, &port_high, &portlen,
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 if ((port_low & ~0xffff) != 0 || (port_high & ~0xffff) != 0)
64ecc88345766e124e1fb053dbf4e36550471f83Evan Hunt#else /* !HAVE_SYSCTLBYNAME */
64ecc88345766e124e1fb053dbf4e36550471f83Evan Huntgetudpportrange_sysctl(int af, in_port_t *low, in_port_t *high) {
64ecc88345766e124e1fb053dbf4e36550471f83Evan Hunt if (sysctl(mib_lo, miblen, &port_low, &portlen, NULL, 0) < 0) {
64ecc88345766e124e1fb053dbf4e36550471f83Evan Hunt if (sysctl(mib_hi, miblen, &port_high, &portlen, NULL, 0) < 0) {
64ecc88345766e124e1fb053dbf4e36550471f83Evan Hunt if ((port_low & ~0xffff) != 0 || (port_high & ~0xffff) != 0)
64ecc88345766e124e1fb053dbf4e36550471f83Evan Hunt#endif /* HAVE_SYSCTLBYNAME */
64ecc88345766e124e1fb053dbf4e36550471f83Evan Hunt#endif /* USE_SYSCTL_PORTRANGE */
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉isc_net_getudpportrange(int af, in_port_t *low, in_port_t *high) {
e77e44954909dd2e0af1ce724e01f4199ae1e9c0Mark Andrews#if !defined(USE_SYSCTL_PORTRANGE) && defined(__linux)
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 result = getudpportrange_sysctl(af, low, high);
e77e44954909dd2e0af1ce724e01f4199ae1e9c0Mark Andrews * Linux local ports are address family agnostic.
e77e44954909dd2e0af1ce724e01f4199ae1e9c0Mark Andrews fp = fopen("/proc/sys/net/ipv4/ip_local_port_range", "r");
e77e44954909dd2e0af1ce724e01f4199ae1e9c0Mark Andrews unsigned int l, h;
e77e44954909dd2e0af1ce724e01f4199ae1e9c0Mark Andrews if (n == 2 && (l & ~0xffff) == 0 && (h & ~0xffff) == 0) {
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 return (ISC_R_SUCCESS); /* we currently never fail in this function */