1 /* 2 * ntp_net.h - definitions for NTP network stuff 3 */ 4 5 #ifndef NTP_NET_H 6 #define NTP_NET_H 7 8 #include <sys/types.h> 9 #ifdef HAVE_SYS_SOCKET_H 10 #include <sys/socket.h> 11 #endif 12 #ifdef HAVE_NET_IF_H 13 #include <net/if.h> 14 #endif 15 #ifdef HAVE_NETINET_IN_H 16 #include <netinet/in.h> 17 #endif 18 #ifdef HAVE_NET_IF_VAR_H 19 #include <net/if_var.h> 20 #endif 21 #ifdef HAVE_NETINET_IN_VAR_H 22 #include <netinet/in_var.h> 23 #endif 24 25 #include "ntp_rfc2553.h" 26 #include "ntp_malloc.h" 27 28 typedef union { 29 struct sockaddr sa; 30 struct sockaddr_in sa4; 31 struct sockaddr_in6 sa6; 32 } sockaddr_u; 33 34 /* 35 * Utilities for manipulating sockaddr_u v4/v6 unions 36 */ 37 #define SOCK_ADDR4(psau) ((psau)->sa4.sin_addr) 38 #define SOCK_ADDR6(psau) ((psau)->sa6.sin6_addr) 39 40 #define PSOCK_ADDR4(psau) (&SOCK_ADDR4(psau)) 41 #define PSOCK_ADDR6(psau) (&SOCK_ADDR6(psau)) 42 43 #define AF(psau) ((psau)->sa.sa_family) 44 45 #define IS_IPV4(psau) (AF_INET == AF(psau)) 46 #define IS_IPV6(psau) (AF_INET6 == AF(psau)) 47 48 /* sockaddr_u v4 address in network byte order */ 49 #define NSRCADR(psau) (SOCK_ADDR4(psau).s_addr) 50 51 /* sockaddr_u v4 address in host byte order */ 52 #define SRCADR(psau) (ntohl(NSRCADR(psau))) 53 54 /* sockaddr_u v6 address in network byte order */ 55 #define NSRCADR6(psau) (SOCK_ADDR6(psau).s6_addr) 56 57 /* assign sockaddr_u v4 address from host byte order */ 58 #define SET_ADDR4(psau, addr4) (NSRCADR(psau) = htonl(addr4)) 59 60 /* assign sockaddr_u v4 address from network byte order */ 61 #define SET_ADDR4N(psau, addr4n) (NSRCADR(psau) = (addr4n)); 62 63 /* assign sockaddr_u v6 address from network byte order */ 64 #define SET_ADDR6N(psau, s6_addr) \ 65 (SOCK_ADDR6(psau) = (s6_addr)) 66 67 /* sockaddr_u v4/v6 port in network byte order */ 68 #define NSRCPORT(psau) ((psau)->sa4.sin_port) 69 70 /* sockaddr_u v4/v6 port in host byte order */ 71 #define SRCPORT(psau) (ntohs(NSRCPORT(psau))) 72 73 /* assign sockaddr_u v4/v6 port from host byte order */ 74 #define SET_PORT(psau, port) (NSRCPORT(psau) = htons(port)) 75 76 /* sockaddr_u v6 scope */ 77 #define SCOPE_VAR(psau) ((psau)->sa6.sin6_scope_id) 78 79 #ifdef ISC_PLATFORM_HAVESCOPEID 80 /* v4/v6 scope (always zero for v4) */ 81 # define SCOPE(psau) (IS_IPV4(psau) \ 82 ? 0 \ 83 : SCOPE_VAR(psau)) 84 85 /* are two v6 sockaddr_u scopes equal? */ 86 # define SCOPE_EQ(psau1, psau2) \ 87 (SCOPE_VAR(psau1) == SCOPE_VAR(psau2)) 88 89 /* assign scope if supported */ 90 # define SET_SCOPE(psau, s) \ 91 do \ 92 if (IS_IPV6(psau)) \ 93 SCOPE_VAR(psau) = (s); \ 94 while (0) 95 #else /* ISC_PLATFORM_HAVESCOPEID not defined */ 96 # define SCOPE(psau) (0) 97 # define SCOPE_EQ(psau1, psau2) (1) 98 # define SET_SCOPE(psau, s) do { } while (0) 99 #endif /* ISC_PLATFORM_HAVESCOPEID */ 100 101 /* v4/v6 is multicast address */ 102 #define IS_MCAST(psau) \ 103 (IS_IPV4(psau) \ 104 ? IN_CLASSD(SRCADR(psau)) \ 105 : IN6_IS_ADDR_MULTICAST(PSOCK_ADDR6(psau))) 106 107 /* v6 is interface ID scope universal, as with MAC-derived addresses */ 108 #define IS_IID_UNIV(psau) \ 109 (!!(0x02 & NSRCADR6(psau)[8])) 110 111 #define SIZEOF_INADDR(fam) \ 112 ((AF_INET == (fam)) \ 113 ? sizeof(struct in_addr) \ 114 : sizeof(struct in6_addr)) 115 116 #define SIZEOF_SOCKADDR(fam) \ 117 ((AF_INET == (fam)) \ 118 ? sizeof(struct sockaddr_in) \ 119 : sizeof(struct sockaddr_in6)) 120 121 #define SOCKLEN(psau) \ 122 (IS_IPV4(psau) \ 123 ? sizeof((psau)->sa4) \ 124 : sizeof((psau)->sa6)) 125 126 #define ZERO_SOCK(psau) \ 127 ZERO(*(psau)) 128 129 /* blast a byte value across sockaddr_u v6 address */ 130 #define MEMSET_ADDR6(psau, v) \ 131 memset((psau)->sa6.sin6_addr.s6_addr, (v), \ 132 sizeof((psau)->sa6.sin6_addr.s6_addr)) 133 134 #define SET_ONESMASK(psau) \ 135 do { \ 136 if (IS_IPV6(psau)) \ 137 MEMSET_ADDR6((psau), 0xff); \ 138 else \ 139 NSRCADR(psau) = 0xffffffff; \ 140 } while(0) 141 142 /* zero sockaddr_u, fill in family and all-ones (host) mask */ 143 #define SET_HOSTMASK(psau, family) \ 144 do { \ 145 ZERO_SOCK(psau); \ 146 AF(psau) = (family); \ 147 SET_ONESMASK(psau); \ 148 } while (0) 149 150 /* 151 * compare two in6_addr returning negative, 0, or positive. 152 * ADDR6_CMP is negative if *pin6A is lower than *pin6B, zero if they 153 * are equal, positive if *pin6A is higher than *pin6B. IN6ADDR_ANY 154 * is the lowest address (128 zero bits). 155 */ 156 #define ADDR6_CMP(pin6A, pin6B) \ 157 memcmp((pin6A)->s6_addr, (pin6B)->s6_addr, \ 158 sizeof(pin6A)->s6_addr) 159 160 /* compare two in6_addr for equality only */ 161 #if !defined(SYS_WINNT) || !defined(in_addr6) 162 #define ADDR6_EQ(pin6A, pin6B) \ 163 (!ADDR6_CMP(pin6A, pin6B)) 164 #else 165 #define ADDR6_EQ(pin6A, pin6B) \ 166 IN6_ADDR_EQUAL(pin6A, pin6B) 167 #endif 168 169 /* compare a in6_addr with socket address */ 170 #define S_ADDR6_EQ(psau, pin6) \ 171 ADDR6_EQ(&(psau)->sa6.sin6_addr, pin6) 172 173 /* are two sockaddr_u's addresses equal? (port excluded) */ 174 #define SOCK_EQ(psau1, psau2) \ 175 ((AF(psau1) != AF(psau2)) \ 176 ? 0 \ 177 : IS_IPV4(psau1) \ 178 ? (NSRCADR(psau1) == NSRCADR(psau2)) \ 179 : (S_ADDR6_EQ((psau1), PSOCK_ADDR6(psau2)) \ 180 && SCOPE_EQ((psau1), (psau2)))) 181 182 /* are two sockaddr_u's addresses and ports equal? */ 183 #define ADDR_PORT_EQ(psau1, psau2) \ 184 ((NSRCPORT(psau1) != NSRCPORT(psau2) \ 185 ? 0 \ 186 : SOCK_EQ((psau1), (psau2)))) 187 188 /* is sockaddr_u address unspecified? */ 189 #define SOCK_UNSPEC(psau) \ 190 (IS_IPV4(psau) \ 191 ? !NSRCADR(psau) \ 192 : IN6_IS_ADDR_UNSPECIFIED(PSOCK_ADDR6(psau))) 193 194 /* just how unspecified do you mean? (scope 0/unspec too) */ 195 #define SOCK_UNSPEC_S(psau) \ 196 (SOCK_UNSPEC(psau) && !SCOPE(psau)) 197 198 /* choose a default net interface (struct interface) for v4 or v6 */ 199 #define ANY_INTERFACE_BYFAM(family) \ 200 ((AF_INET == family) \ 201 ? any_interface \ 202 : any6_interface) 203 204 /* choose a default interface for addresses' protocol (addr family) */ 205 #define ANY_INTERFACE_CHOOSE(psau) \ 206 ANY_INTERFACE_BYFAM(AF(psau)) 207 208 209 /* 210 * We tell reference clocks from real peers by giving the reference 211 * clocks an address of the form 127.127.t.u, where t is the type and 212 * u is the unit number. We define some of this here since we will need 213 * some sanity checks to make sure this address isn't interpretted as 214 * that of a normal peer. 215 */ 216 #define REFCLOCK_ADDR 0x7f7f0000 /* 127.127.0.0 */ 217 #define REFCLOCK_MASK 0xffff0000 /* 255.255.0.0 */ 218 219 #define ISREFCLOCKADR(srcadr) \ 220 (IS_IPV4(srcadr) && \ 221 (SRCADR(srcadr) & REFCLOCK_MASK) == REFCLOCK_ADDR) 222 223 /* 224 * Macro for checking for invalid addresses. This is really, really 225 * gross, but is needed so no one configures a host on net 127 now that 226 * we're encouraging it the the configuration file. 227 */ 228 #define LOOPBACKADR 0x7f000001 229 #define LOOPNETMASK 0xff000000 230 231 #define ISBADADR(srcadr) \ 232 (IS_IPV4(srcadr) \ 233 && ((SRCADR(srcadr) & LOOPNETMASK) \ 234 == (LOOPBACKADR & LOOPNETMASK)) \ 235 && SRCADR(srcadr) != LOOPBACKADR) 236 237 238 #endif /* NTP_NET_H */ 239