paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1 | /* Socket union related function. |
| 2 | * Copyright (c) 1997, 98 Kunihiro Ishiguro |
| 3 | * |
| 4 | * This file is part of GNU Zebra. |
| 5 | * |
| 6 | * GNU Zebra is free software; you can redistribute it and/or modify it |
| 7 | * under the terms of the GNU General Public License as published by the |
| 8 | * Free Software Foundation; either version 2, or (at your option) any |
| 9 | * later version. |
| 10 | * |
| 11 | * GNU Zebra is distributed in the hope that it will be useful, but |
| 12 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | * General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with GNU Zebra; see the file COPYING. If not, write to the Free |
| 18 | * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
| 19 | * 02111-1307, USA. |
| 20 | */ |
| 21 | |
| 22 | #include <zebra.h> |
| 23 | |
| 24 | #include "prefix.h" |
| 25 | #include "vty.h" |
| 26 | #include "sockunion.h" |
| 27 | #include "memory.h" |
| 28 | #include "str.h" |
| 29 | #include "log.h" |
Timo Teräs | 9196caf | 2015-04-29 09:43:05 +0300 | [diff] [blame] | 30 | #include "jhash.h" |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 31 | |
Donald Sharp | 54b88ca | 2015-05-13 20:34:46 -0400 | [diff] [blame^] | 32 | #if defined(HAVE_LINUX_IN6_H) |
| 33 | #include <linux/in6.h> |
| 34 | #endif |
| 35 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 36 | #ifndef HAVE_INET_ATON |
| 37 | int |
| 38 | inet_aton (const char *cp, struct in_addr *inaddr) |
| 39 | { |
| 40 | int dots = 0; |
| 41 | register u_long addr = 0; |
| 42 | register u_long val = 0, base = 10; |
| 43 | |
| 44 | do |
| 45 | { |
| 46 | register char c = *cp; |
| 47 | |
| 48 | switch (c) |
| 49 | { |
| 50 | case '0': case '1': case '2': case '3': case '4': case '5': |
| 51 | case '6': case '7': case '8': case '9': |
| 52 | val = (val * base) + (c - '0'); |
| 53 | break; |
| 54 | case '.': |
| 55 | if (++dots > 3) |
| 56 | return 0; |
| 57 | case '\0': |
| 58 | if (val > 255) |
| 59 | return 0; |
| 60 | addr = addr << 8 | val; |
| 61 | val = 0; |
| 62 | break; |
| 63 | default: |
| 64 | return 0; |
| 65 | } |
| 66 | } while (*cp++) ; |
| 67 | |
| 68 | if (dots < 3) |
| 69 | addr <<= 8 * (3 - dots); |
| 70 | if (inaddr) |
| 71 | inaddr->s_addr = htonl (addr); |
| 72 | return 1; |
| 73 | } |
| 74 | #endif /* ! HAVE_INET_ATON */ |
| 75 | |
| 76 | |
| 77 | #ifndef HAVE_INET_PTON |
| 78 | int |
| 79 | inet_pton (int family, const char *strptr, void *addrptr) |
| 80 | { |
| 81 | if (family == AF_INET) |
| 82 | { |
| 83 | struct in_addr in_val; |
| 84 | |
| 85 | if (inet_aton (strptr, &in_val)) |
| 86 | { |
| 87 | memcpy (addrptr, &in_val, sizeof (struct in_addr)); |
| 88 | return 1; |
| 89 | } |
| 90 | return 0; |
| 91 | } |
| 92 | errno = EAFNOSUPPORT; |
| 93 | return -1; |
| 94 | } |
| 95 | #endif /* ! HAVE_INET_PTON */ |
| 96 | |
| 97 | #ifndef HAVE_INET_NTOP |
| 98 | const char * |
| 99 | inet_ntop (int family, const void *addrptr, char *strptr, size_t len) |
| 100 | { |
| 101 | unsigned char *p = (unsigned char *) addrptr; |
| 102 | |
| 103 | if (family == AF_INET) |
| 104 | { |
| 105 | char temp[INET_ADDRSTRLEN]; |
| 106 | |
| 107 | snprintf(temp, sizeof(temp), "%d.%d.%d.%d", p[0], p[1], p[2], p[3]); |
| 108 | |
| 109 | if (strlen(temp) >= len) |
| 110 | { |
| 111 | errno = ENOSPC; |
| 112 | return NULL; |
| 113 | } |
| 114 | strcpy(strptr, temp); |
| 115 | return strptr; |
| 116 | } |
| 117 | |
| 118 | errno = EAFNOSUPPORT; |
| 119 | return NULL; |
| 120 | } |
| 121 | #endif /* ! HAVE_INET_NTOP */ |
| 122 | |
| 123 | const char * |
Timo Teräs | 81b139b | 2015-04-29 09:43:01 +0300 | [diff] [blame] | 124 | inet_sutop (const union sockunion *su, char *str) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 125 | { |
| 126 | switch (su->sa.sa_family) |
| 127 | { |
| 128 | case AF_INET: |
| 129 | inet_ntop (AF_INET, &su->sin.sin_addr, str, INET_ADDRSTRLEN); |
| 130 | break; |
| 131 | #ifdef HAVE_IPV6 |
| 132 | case AF_INET6: |
| 133 | inet_ntop (AF_INET6, &su->sin6.sin6_addr, str, INET6_ADDRSTRLEN); |
| 134 | break; |
| 135 | #endif /* HAVE_IPV6 */ |
| 136 | } |
| 137 | return str; |
| 138 | } |
| 139 | |
| 140 | int |
hasso | a149411 | 2004-10-11 12:53:17 +0000 | [diff] [blame] | 141 | str2sockunion (const char *str, union sockunion *su) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 142 | { |
| 143 | int ret; |
| 144 | |
| 145 | memset (su, 0, sizeof (union sockunion)); |
| 146 | |
| 147 | ret = inet_pton (AF_INET, str, &su->sin.sin_addr); |
| 148 | if (ret > 0) /* Valid IPv4 address format. */ |
| 149 | { |
| 150 | su->sin.sin_family = AF_INET; |
Paul Jakma | 6f0e3f6 | 2007-05-10 02:38:51 +0000 | [diff] [blame] | 151 | #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 152 | su->sin.sin_len = sizeof(struct sockaddr_in); |
Paul Jakma | 6f0e3f6 | 2007-05-10 02:38:51 +0000 | [diff] [blame] | 153 | #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 154 | return 0; |
| 155 | } |
| 156 | #ifdef HAVE_IPV6 |
| 157 | ret = inet_pton (AF_INET6, str, &su->sin6.sin6_addr); |
| 158 | if (ret > 0) /* Valid IPv6 address format. */ |
| 159 | { |
| 160 | su->sin6.sin6_family = AF_INET6; |
| 161 | #ifdef SIN6_LEN |
| 162 | su->sin6.sin6_len = sizeof(struct sockaddr_in6); |
| 163 | #endif /* SIN6_LEN */ |
| 164 | return 0; |
| 165 | } |
| 166 | #endif /* HAVE_IPV6 */ |
| 167 | return -1; |
| 168 | } |
| 169 | |
| 170 | const char * |
Timo Teräs | 81b139b | 2015-04-29 09:43:01 +0300 | [diff] [blame] | 171 | sockunion2str (const union sockunion *su, char *buf, size_t len) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 172 | { |
| 173 | if (su->sa.sa_family == AF_INET) |
| 174 | return inet_ntop (AF_INET, &su->sin.sin_addr, buf, len); |
| 175 | #ifdef HAVE_IPV6 |
| 176 | else if (su->sa.sa_family == AF_INET6) |
| 177 | return inet_ntop (AF_INET6, &su->sin6.sin6_addr, buf, len); |
| 178 | #endif /* HAVE_IPV6 */ |
| 179 | return NULL; |
| 180 | } |
| 181 | |
Paul Jakma | 2fb2a45 | 2012-06-14 10:37:40 +0100 | [diff] [blame] | 182 | union sockunion * |
| 183 | sockunion_str2su (const char *str) |
| 184 | { |
| 185 | union sockunion *su = XCALLOC (MTYPE_SOCKUNION, sizeof (union sockunion)); |
| 186 | |
| 187 | if (!str2sockunion (str, su)) |
| 188 | return su; |
| 189 | |
| 190 | XFREE (MTYPE_SOCKUNION, su); |
| 191 | return NULL; |
| 192 | } |
| 193 | |
Paul Jakma | 1a7dcf4 | 2008-09-09 21:17:04 +0100 | [diff] [blame] | 194 | /* Convert IPv4 compatible IPv6 address to IPv4 address. */ |
| 195 | static void |
| 196 | sockunion_normalise_mapped (union sockunion *su) |
| 197 | { |
| 198 | struct sockaddr_in sin; |
| 199 | |
| 200 | #ifdef HAVE_IPV6 |
| 201 | if (su->sa.sa_family == AF_INET6 |
| 202 | && IN6_IS_ADDR_V4MAPPED (&su->sin6.sin6_addr)) |
| 203 | { |
| 204 | memset (&sin, 0, sizeof (struct sockaddr_in)); |
| 205 | sin.sin_family = AF_INET; |
Stephen Hemminger | 3fa3f95 | 2009-07-11 21:27:51 -0700 | [diff] [blame] | 206 | sin.sin_port = su->sin6.sin6_port; |
Paul Jakma | 1a7dcf4 | 2008-09-09 21:17:04 +0100 | [diff] [blame] | 207 | memcpy (&sin.sin_addr, ((char *)&su->sin6.sin6_addr) + 12, 4); |
| 208 | memcpy (su, &sin, sizeof (struct sockaddr_in)); |
| 209 | } |
| 210 | #endif /* HAVE_IPV6 */ |
| 211 | } |
| 212 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 213 | /* Return socket of sockunion. */ |
| 214 | int |
Timo Teräs | 81b139b | 2015-04-29 09:43:01 +0300 | [diff] [blame] | 215 | sockunion_socket (const union sockunion *su) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 216 | { |
| 217 | int sock; |
| 218 | |
| 219 | sock = socket (su->sa.sa_family, SOCK_STREAM, 0); |
| 220 | if (sock < 0) |
| 221 | { |
ajs | 6099b3b | 2004-11-20 02:06:59 +0000 | [diff] [blame] | 222 | zlog (NULL, LOG_WARNING, "Can't make socket : %s", safe_strerror (errno)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 223 | return -1; |
| 224 | } |
| 225 | |
| 226 | return sock; |
| 227 | } |
| 228 | |
| 229 | /* Return accepted new socket file descriptor. */ |
| 230 | int |
| 231 | sockunion_accept (int sock, union sockunion *su) |
| 232 | { |
| 233 | socklen_t len; |
| 234 | int client_sock; |
| 235 | |
| 236 | len = sizeof (union sockunion); |
| 237 | client_sock = accept (sock, (struct sockaddr *) su, &len); |
| 238 | |
Paul Jakma | 84152ee | 2008-11-24 22:25:16 +0000 | [diff] [blame] | 239 | sockunion_normalise_mapped (su); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 240 | return client_sock; |
| 241 | } |
| 242 | |
| 243 | /* Return sizeof union sockunion. */ |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame] | 244 | static int |
Timo Teräs | 81b139b | 2015-04-29 09:43:01 +0300 | [diff] [blame] | 245 | sockunion_sizeof (const union sockunion *su) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 246 | { |
| 247 | int ret; |
| 248 | |
| 249 | ret = 0; |
| 250 | switch (su->sa.sa_family) |
| 251 | { |
| 252 | case AF_INET: |
| 253 | ret = sizeof (struct sockaddr_in); |
| 254 | break; |
| 255 | #ifdef HAVE_IPV6 |
| 256 | case AF_INET6: |
| 257 | ret = sizeof (struct sockaddr_in6); |
| 258 | break; |
| 259 | #endif /* AF_INET6 */ |
| 260 | } |
| 261 | return ret; |
| 262 | } |
| 263 | |
| 264 | /* return sockunion structure : this function should be revised. */ |
Stephen Hemminger | b24b19f | 2011-12-06 14:09:18 +0400 | [diff] [blame] | 265 | static const char * |
Timo Teräs | 81b139b | 2015-04-29 09:43:01 +0300 | [diff] [blame] | 266 | sockunion_log (const union sockunion *su, char *buf, size_t len) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 267 | { |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 268 | switch (su->sa.sa_family) |
| 269 | { |
| 270 | case AF_INET: |
Stephen Hemminger | b24b19f | 2011-12-06 14:09:18 +0400 | [diff] [blame] | 271 | return inet_ntop(AF_INET, &su->sin.sin_addr, buf, len); |
| 272 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 273 | #ifdef HAVE_IPV6 |
| 274 | case AF_INET6: |
Stephen Hemminger | b24b19f | 2011-12-06 14:09:18 +0400 | [diff] [blame] | 275 | return inet_ntop(AF_INET6, &(su->sin6.sin6_addr), buf, len); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 276 | break; |
| 277 | #endif /* HAVE_IPV6 */ |
Stephen Hemminger | b24b19f | 2011-12-06 14:09:18 +0400 | [diff] [blame] | 278 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 279 | default: |
Stephen Hemminger | b24b19f | 2011-12-06 14:09:18 +0400 | [diff] [blame] | 280 | snprintf (buf, len, "af_unknown %d ", su->sa.sa_family); |
| 281 | return buf; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 282 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | /* sockunion_connect returns |
| 286 | -1 : error occured |
| 287 | 0 : connect success |
| 288 | 1 : connect is in progress */ |
| 289 | enum connect_result |
Timo Teräs | 81b139b | 2015-04-29 09:43:01 +0300 | [diff] [blame] | 290 | sockunion_connect (int fd, const union sockunion *peersu, unsigned short port, |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 291 | unsigned int ifindex) |
| 292 | { |
| 293 | int ret; |
| 294 | int val; |
| 295 | union sockunion su; |
| 296 | |
| 297 | memcpy (&su, peersu, sizeof (union sockunion)); |
| 298 | |
| 299 | switch (su.sa.sa_family) |
| 300 | { |
| 301 | case AF_INET: |
| 302 | su.sin.sin_port = port; |
| 303 | break; |
| 304 | #ifdef HAVE_IPV6 |
| 305 | case AF_INET6: |
| 306 | su.sin6.sin6_port = port; |
| 307 | #ifdef KAME |
| 308 | if (IN6_IS_ADDR_LINKLOCAL(&su.sin6.sin6_addr) && ifindex) |
| 309 | { |
Paul Jakma | 6f0e3f6 | 2007-05-10 02:38:51 +0000 | [diff] [blame] | 310 | #ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 311 | /* su.sin6.sin6_scope_id = ifindex; */ |
Paul Jakma | 6f0e3f6 | 2007-05-10 02:38:51 +0000 | [diff] [blame] | 312 | #endif /* HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 313 | SET_IN6_LINKLOCAL_IFINDEX (su.sin6.sin6_addr, ifindex); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 314 | } |
| 315 | #endif /* KAME */ |
| 316 | break; |
| 317 | #endif /* HAVE_IPV6 */ |
| 318 | } |
| 319 | |
| 320 | /* Make socket non-block. */ |
| 321 | val = fcntl (fd, F_GETFL, 0); |
| 322 | fcntl (fd, F_SETFL, val|O_NONBLOCK); |
| 323 | |
| 324 | /* Call connect function. */ |
| 325 | ret = connect (fd, (struct sockaddr *) &su, sockunion_sizeof (&su)); |
| 326 | |
| 327 | /* Immediate success */ |
| 328 | if (ret == 0) |
| 329 | { |
| 330 | fcntl (fd, F_SETFL, val); |
| 331 | return connect_success; |
| 332 | } |
| 333 | |
| 334 | /* If connect is in progress then return 1 else it's real error. */ |
| 335 | if (ret < 0) |
| 336 | { |
| 337 | if (errno != EINPROGRESS) |
| 338 | { |
Stephen Hemminger | b24b19f | 2011-12-06 14:09:18 +0400 | [diff] [blame] | 339 | char str[SU_ADDRSTRLEN]; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 340 | zlog_info ("can't connect to %s fd %d : %s", |
Stephen Hemminger | b24b19f | 2011-12-06 14:09:18 +0400 | [diff] [blame] | 341 | sockunion_log (&su, str, sizeof str), |
| 342 | fd, safe_strerror (errno)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 343 | return connect_error; |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | fcntl (fd, F_SETFL, val); |
| 348 | |
| 349 | return connect_in_progress; |
| 350 | } |
| 351 | |
| 352 | /* Make socket from sockunion union. */ |
| 353 | int |
| 354 | sockunion_stream_socket (union sockunion *su) |
| 355 | { |
| 356 | int sock; |
| 357 | |
| 358 | if (su->sa.sa_family == 0) |
| 359 | su->sa.sa_family = AF_INET_UNION; |
| 360 | |
| 361 | sock = socket (su->sa.sa_family, SOCK_STREAM, 0); |
| 362 | |
| 363 | if (sock < 0) |
| 364 | zlog (NULL, LOG_WARNING, "can't make socket sockunion_stream_socket"); |
| 365 | |
| 366 | return sock; |
| 367 | } |
| 368 | |
| 369 | /* Bind socket to specified address. */ |
| 370 | int |
| 371 | sockunion_bind (int sock, union sockunion *su, unsigned short port, |
| 372 | union sockunion *su_addr) |
| 373 | { |
| 374 | int size = 0; |
| 375 | int ret; |
| 376 | |
| 377 | if (su->sa.sa_family == AF_INET) |
| 378 | { |
| 379 | size = sizeof (struct sockaddr_in); |
| 380 | su->sin.sin_port = htons (port); |
Paul Jakma | 6f0e3f6 | 2007-05-10 02:38:51 +0000 | [diff] [blame] | 381 | #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 382 | su->sin.sin_len = size; |
Paul Jakma | 6f0e3f6 | 2007-05-10 02:38:51 +0000 | [diff] [blame] | 383 | #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 384 | if (su_addr == NULL) |
Jorge Boncompte [DTI2] | 1282932 | 2012-04-10 16:57:25 +0200 | [diff] [blame] | 385 | sockunion2ip (su) = htonl (INADDR_ANY); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 386 | } |
| 387 | #ifdef HAVE_IPV6 |
| 388 | else if (su->sa.sa_family == AF_INET6) |
| 389 | { |
| 390 | size = sizeof (struct sockaddr_in6); |
| 391 | su->sin6.sin6_port = htons (port); |
| 392 | #ifdef SIN6_LEN |
| 393 | su->sin6.sin6_len = size; |
| 394 | #endif /* SIN6_LEN */ |
| 395 | if (su_addr == NULL) |
| 396 | { |
David Lamparter | 6d6df30 | 2014-06-28 21:12:37 +0200 | [diff] [blame] | 397 | #ifdef LINUX_IPV6 |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 398 | memset (&su->sin6.sin6_addr, 0, sizeof (struct in6_addr)); |
| 399 | #else |
| 400 | su->sin6.sin6_addr = in6addr_any; |
| 401 | #endif /* LINUX_IPV6 */ |
| 402 | } |
| 403 | } |
| 404 | #endif /* HAVE_IPV6 */ |
| 405 | |
| 406 | |
| 407 | ret = bind (sock, (struct sockaddr *)su, size); |
| 408 | if (ret < 0) |
ajs | 6099b3b | 2004-11-20 02:06:59 +0000 | [diff] [blame] | 409 | zlog (NULL, LOG_WARNING, "can't bind socket : %s", safe_strerror (errno)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 410 | |
| 411 | return ret; |
| 412 | } |
| 413 | |
| 414 | int |
| 415 | sockopt_reuseaddr (int sock) |
| 416 | { |
| 417 | int ret; |
| 418 | int on = 1; |
| 419 | |
| 420 | ret = setsockopt (sock, SOL_SOCKET, SO_REUSEADDR, |
| 421 | (void *) &on, sizeof (on)); |
| 422 | if (ret < 0) |
| 423 | { |
| 424 | zlog (NULL, LOG_WARNING, "can't set sockopt SO_REUSEADDR to socket %d", sock); |
| 425 | return -1; |
| 426 | } |
| 427 | return 0; |
| 428 | } |
| 429 | |
| 430 | #ifdef SO_REUSEPORT |
| 431 | int |
| 432 | sockopt_reuseport (int sock) |
| 433 | { |
| 434 | int ret; |
| 435 | int on = 1; |
| 436 | |
| 437 | ret = setsockopt (sock, SOL_SOCKET, SO_REUSEPORT, |
| 438 | (void *) &on, sizeof (on)); |
| 439 | if (ret < 0) |
| 440 | { |
hasso | e7fe8c8 | 2005-05-06 19:33:35 +0000 | [diff] [blame] | 441 | zlog (NULL, LOG_WARNING, "can't set sockopt SO_REUSEPORT to socket %d", sock); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 442 | return -1; |
| 443 | } |
| 444 | return 0; |
| 445 | } |
| 446 | #else |
| 447 | int |
| 448 | sockopt_reuseport (int sock) |
| 449 | { |
| 450 | return 0; |
| 451 | } |
| 452 | #endif /* 0 */ |
| 453 | |
| 454 | int |
| 455 | sockopt_ttl (int family, int sock, int ttl) |
| 456 | { |
| 457 | int ret; |
| 458 | |
| 459 | #ifdef IP_TTL |
| 460 | if (family == AF_INET) |
| 461 | { |
| 462 | ret = setsockopt (sock, IPPROTO_IP, IP_TTL, |
| 463 | (void *) &ttl, sizeof (int)); |
| 464 | if (ret < 0) |
| 465 | { |
| 466 | zlog (NULL, LOG_WARNING, "can't set sockopt IP_TTL %d to socket %d", ttl, sock); |
| 467 | return -1; |
| 468 | } |
| 469 | return 0; |
| 470 | } |
| 471 | #endif /* IP_TTL */ |
| 472 | #ifdef HAVE_IPV6 |
| 473 | if (family == AF_INET6) |
| 474 | { |
| 475 | ret = setsockopt (sock, IPPROTO_IPV6, IPV6_UNICAST_HOPS, |
| 476 | (void *) &ttl, sizeof (int)); |
| 477 | if (ret < 0) |
| 478 | { |
| 479 | zlog (NULL, LOG_WARNING, "can't set sockopt IPV6_UNICAST_HOPS %d to socket %d", |
| 480 | ttl, sock); |
| 481 | return -1; |
| 482 | } |
| 483 | return 0; |
| 484 | } |
| 485 | #endif /* HAVE_IPV6 */ |
| 486 | return 0; |
| 487 | } |
| 488 | |
Stephen Hemminger | 58192df | 2010-08-05 10:26:24 -0700 | [diff] [blame] | 489 | int |
| 490 | sockopt_cork (int sock, int onoff) |
| 491 | { |
| 492 | #ifdef TCP_CORK |
| 493 | return setsockopt (sock, IPPROTO_TCP, TCP_CORK, &onoff, sizeof(onoff)); |
| 494 | #else |
| 495 | return 0; |
| 496 | #endif |
| 497 | } |
| 498 | |
Nick Hilliard | fa411a2 | 2011-03-23 15:33:17 +0000 | [diff] [blame] | 499 | int |
| 500 | sockopt_minttl (int family, int sock, int minttl) |
| 501 | { |
Stephen Hemminger | 89b6d1f | 2011-03-24 10:51:59 +0000 | [diff] [blame] | 502 | #ifdef IP_MINTTL |
Stephen Hemminger | d876bdf | 2010-08-05 10:26:27 -0700 | [diff] [blame] | 503 | if (family == AF_INET) |
Nick Hilliard | fa411a2 | 2011-03-23 15:33:17 +0000 | [diff] [blame] | 504 | { |
Stephen Hemminger | d876bdf | 2010-08-05 10:26:27 -0700 | [diff] [blame] | 505 | int ret = setsockopt (sock, IPPROTO_IP, IP_MINTTL, &minttl, sizeof(minttl)); |
| 506 | if (ret < 0) |
| 507 | zlog (NULL, LOG_WARNING, |
| 508 | "can't set sockopt IP_MINTTL to %d on socket %d: %s", |
| 509 | minttl, sock, safe_strerror (errno)); |
| 510 | return ret; |
Nick Hilliard | fa411a2 | 2011-03-23 15:33:17 +0000 | [diff] [blame] | 511 | } |
Stephen Hemminger | d876bdf | 2010-08-05 10:26:27 -0700 | [diff] [blame] | 512 | #endif /* IP_MINTTL */ |
Donald Sharp | 54b88ca | 2015-05-13 20:34:46 -0400 | [diff] [blame^] | 513 | #ifdef IPV6_MINHOPCOUNT |
Stephen Hemminger | d876bdf | 2010-08-05 10:26:27 -0700 | [diff] [blame] | 514 | if (family == AF_INET6) |
| 515 | { |
Donald Sharp | 54b88ca | 2015-05-13 20:34:46 -0400 | [diff] [blame^] | 516 | int ret = setsockopt (sock, IPPROTO_IPV6, IPV6_MINHOPCOUNT, &minttl, sizeof(minttl)); |
Stephen Hemminger | d876bdf | 2010-08-05 10:26:27 -0700 | [diff] [blame] | 517 | if (ret < 0) |
| 518 | zlog (NULL, LOG_WARNING, |
Donald Sharp | 54b88ca | 2015-05-13 20:34:46 -0400 | [diff] [blame^] | 519 | "can't set sockopt IPV6_MINHOPCOUNT to %d on socket %d: %s", |
Stephen Hemminger | d876bdf | 2010-08-05 10:26:27 -0700 | [diff] [blame] | 520 | minttl, sock, safe_strerror (errno)); |
| 521 | return ret; |
| 522 | } |
| 523 | #endif |
Nick Hilliard | fa411a2 | 2011-03-23 15:33:17 +0000 | [diff] [blame] | 524 | |
Stephen Hemminger | 89b6d1f | 2011-03-24 10:51:59 +0000 | [diff] [blame] | 525 | errno = EOPNOTSUPP; |
| 526 | return -1; |
Nick Hilliard | fa411a2 | 2011-03-23 15:33:17 +0000 | [diff] [blame] | 527 | } |
| 528 | |
David Lamparter | ca05126 | 2009-10-04 16:21:49 +0200 | [diff] [blame] | 529 | int |
| 530 | sockopt_v6only (int family, int sock) |
| 531 | { |
| 532 | int ret, on = 1; |
| 533 | |
| 534 | #ifdef HAVE_IPV6 |
| 535 | #ifdef IPV6_V6ONLY |
| 536 | if (family == AF_INET6) |
| 537 | { |
| 538 | ret = setsockopt (sock, IPPROTO_IPV6, IPV6_V6ONLY, |
| 539 | (void *) &on, sizeof (int)); |
| 540 | if (ret < 0) |
| 541 | { |
| 542 | zlog (NULL, LOG_WARNING, "can't set sockopt IPV6_V6ONLY " |
| 543 | "to socket %d", sock); |
| 544 | return -1; |
| 545 | } |
| 546 | return 0; |
| 547 | } |
| 548 | #endif /* IPV6_V6ONLY */ |
| 549 | #endif /* HAVE_IPV6 */ |
| 550 | return 0; |
| 551 | } |
| 552 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 553 | /* If same family and same prefix return 1. */ |
| 554 | int |
Timo Teräs | 81b139b | 2015-04-29 09:43:01 +0300 | [diff] [blame] | 555 | sockunion_same (const union sockunion *su1, const union sockunion *su2) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 556 | { |
| 557 | int ret = 0; |
| 558 | |
| 559 | if (su1->sa.sa_family != su2->sa.sa_family) |
| 560 | return 0; |
| 561 | |
| 562 | switch (su1->sa.sa_family) |
| 563 | { |
| 564 | case AF_INET: |
| 565 | ret = memcmp (&su1->sin.sin_addr, &su2->sin.sin_addr, |
| 566 | sizeof (struct in_addr)); |
| 567 | break; |
| 568 | #ifdef HAVE_IPV6 |
| 569 | case AF_INET6: |
| 570 | ret = memcmp (&su1->sin6.sin6_addr, &su2->sin6.sin6_addr, |
| 571 | sizeof (struct in6_addr)); |
| 572 | break; |
| 573 | #endif /* HAVE_IPV6 */ |
| 574 | } |
| 575 | if (ret == 0) |
| 576 | return 1; |
| 577 | else |
| 578 | return 0; |
| 579 | } |
| 580 | |
Timo Teräs | 9196caf | 2015-04-29 09:43:05 +0300 | [diff] [blame] | 581 | unsigned int |
| 582 | sockunion_hash (const union sockunion *su) |
| 583 | { |
| 584 | switch (sockunion_family(su)) |
| 585 | { |
| 586 | case AF_INET: |
| 587 | return jhash_1word(su->sin.sin_addr.s_addr, 0); |
| 588 | #ifdef HAVE_IPV6 |
| 589 | case AF_INET6: |
| 590 | return jhash2(su->sin6.sin6_addr.s6_addr32, ZEBRA_NUM_OF(su->sin6.sin6_addr.s6_addr32), 0); |
| 591 | #endif /* HAVE_IPV6 */ |
| 592 | } |
| 593 | return 0; |
| 594 | } |
| 595 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 596 | /* After TCP connection is established. Get local address and port. */ |
| 597 | union sockunion * |
| 598 | sockunion_getsockname (int fd) |
| 599 | { |
| 600 | int ret; |
paul | 2252829 | 2004-05-08 05:10:38 +0000 | [diff] [blame] | 601 | socklen_t len; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 602 | union |
| 603 | { |
| 604 | struct sockaddr sa; |
| 605 | struct sockaddr_in sin; |
| 606 | #ifdef HAVE_IPV6 |
| 607 | struct sockaddr_in6 sin6; |
| 608 | #endif /* HAVE_IPV6 */ |
| 609 | char tmp_buffer[128]; |
| 610 | } name; |
| 611 | union sockunion *su; |
| 612 | |
| 613 | memset (&name, 0, sizeof name); |
| 614 | len = sizeof name; |
| 615 | |
| 616 | ret = getsockname (fd, (struct sockaddr *)&name, &len); |
| 617 | if (ret < 0) |
| 618 | { |
| 619 | zlog_warn ("Can't get local address and port by getsockname: %s", |
ajs | 6099b3b | 2004-11-20 02:06:59 +0000 | [diff] [blame] | 620 | safe_strerror (errno)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 621 | return NULL; |
| 622 | } |
| 623 | |
| 624 | if (name.sa.sa_family == AF_INET) |
| 625 | { |
paul | 2ba9a37 | 2005-05-19 01:37:50 +0000 | [diff] [blame] | 626 | su = XCALLOC (MTYPE_SOCKUNION, sizeof (union sockunion)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 627 | memcpy (su, &name, sizeof (struct sockaddr_in)); |
| 628 | return su; |
| 629 | } |
| 630 | #ifdef HAVE_IPV6 |
| 631 | if (name.sa.sa_family == AF_INET6) |
| 632 | { |
paul | 2ba9a37 | 2005-05-19 01:37:50 +0000 | [diff] [blame] | 633 | su = XCALLOC (MTYPE_SOCKUNION, sizeof (union sockunion)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 634 | memcpy (su, &name, sizeof (struct sockaddr_in6)); |
Paul Jakma | 1a7dcf4 | 2008-09-09 21:17:04 +0100 | [diff] [blame] | 635 | sockunion_normalise_mapped (su); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 636 | return su; |
| 637 | } |
| 638 | #endif /* HAVE_IPV6 */ |
| 639 | return NULL; |
| 640 | } |
| 641 | |
| 642 | /* After TCP connection is established. Get remote address and port. */ |
| 643 | union sockunion * |
| 644 | sockunion_getpeername (int fd) |
| 645 | { |
| 646 | int ret; |
paul | 2252829 | 2004-05-08 05:10:38 +0000 | [diff] [blame] | 647 | socklen_t len; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 648 | union |
| 649 | { |
| 650 | struct sockaddr sa; |
| 651 | struct sockaddr_in sin; |
| 652 | #ifdef HAVE_IPV6 |
| 653 | struct sockaddr_in6 sin6; |
| 654 | #endif /* HAVE_IPV6 */ |
| 655 | char tmp_buffer[128]; |
| 656 | } name; |
| 657 | union sockunion *su; |
| 658 | |
| 659 | memset (&name, 0, sizeof name); |
| 660 | len = sizeof name; |
| 661 | ret = getpeername (fd, (struct sockaddr *)&name, &len); |
| 662 | if (ret < 0) |
| 663 | { |
| 664 | zlog (NULL, LOG_WARNING, "Can't get remote address and port: %s", |
ajs | 6099b3b | 2004-11-20 02:06:59 +0000 | [diff] [blame] | 665 | safe_strerror (errno)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 666 | return NULL; |
| 667 | } |
| 668 | |
| 669 | if (name.sa.sa_family == AF_INET) |
| 670 | { |
paul | 2ba9a37 | 2005-05-19 01:37:50 +0000 | [diff] [blame] | 671 | su = XCALLOC (MTYPE_SOCKUNION, sizeof (union sockunion)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 672 | memcpy (su, &name, sizeof (struct sockaddr_in)); |
| 673 | return su; |
| 674 | } |
| 675 | #ifdef HAVE_IPV6 |
| 676 | if (name.sa.sa_family == AF_INET6) |
| 677 | { |
paul | 2ba9a37 | 2005-05-19 01:37:50 +0000 | [diff] [blame] | 678 | su = XCALLOC (MTYPE_SOCKUNION, sizeof (union sockunion)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 679 | memcpy (su, &name, sizeof (struct sockaddr_in6)); |
Paul Jakma | 1a7dcf4 | 2008-09-09 21:17:04 +0100 | [diff] [blame] | 680 | sockunion_normalise_mapped (su); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 681 | return su; |
| 682 | } |
| 683 | #endif /* HAVE_IPV6 */ |
| 684 | return NULL; |
| 685 | } |
| 686 | |
| 687 | /* Print sockunion structure */ |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame] | 688 | static void __attribute__ ((unused)) |
Timo Teräs | 81b139b | 2015-04-29 09:43:01 +0300 | [diff] [blame] | 689 | sockunion_print (const union sockunion *su) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 690 | { |
| 691 | if (su == NULL) |
| 692 | return; |
| 693 | |
| 694 | switch (su->sa.sa_family) |
| 695 | { |
| 696 | case AF_INET: |
| 697 | printf ("%s\n", inet_ntoa (su->sin.sin_addr)); |
| 698 | break; |
| 699 | #ifdef HAVE_IPV6 |
| 700 | case AF_INET6: |
| 701 | { |
paul | 42d4986 | 2004-10-13 05:22:18 +0000 | [diff] [blame] | 702 | char buf [SU_ADDRSTRLEN]; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 703 | |
| 704 | printf ("%s\n", inet_ntop (AF_INET6, &(su->sin6.sin6_addr), |
| 705 | buf, sizeof (buf))); |
| 706 | } |
| 707 | break; |
| 708 | #endif /* HAVE_IPV6 */ |
| 709 | |
| 710 | #ifdef AF_LINK |
| 711 | case AF_LINK: |
| 712 | { |
| 713 | struct sockaddr_dl *sdl; |
| 714 | |
| 715 | sdl = (struct sockaddr_dl *)&(su->sa); |
| 716 | printf ("link#%d\n", sdl->sdl_index); |
| 717 | } |
| 718 | break; |
| 719 | #endif /* AF_LINK */ |
| 720 | default: |
| 721 | printf ("af_unknown %d\n", su->sa.sa_family); |
| 722 | break; |
| 723 | } |
| 724 | } |
| 725 | |
| 726 | #ifdef HAVE_IPV6 |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame] | 727 | static int |
Timo Teräs | 81b139b | 2015-04-29 09:43:01 +0300 | [diff] [blame] | 728 | in6addr_cmp (const struct in6_addr *addr1, const struct in6_addr *addr2) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 729 | { |
hasso | 8c328f1 | 2004-10-05 21:01:23 +0000 | [diff] [blame] | 730 | unsigned int i; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 731 | u_char *p1, *p2; |
| 732 | |
| 733 | p1 = (u_char *)addr1; |
| 734 | p2 = (u_char *)addr2; |
| 735 | |
| 736 | for (i = 0; i < sizeof (struct in6_addr); i++) |
| 737 | { |
| 738 | if (p1[i] > p2[i]) |
| 739 | return 1; |
| 740 | else if (p1[i] < p2[i]) |
| 741 | return -1; |
| 742 | } |
| 743 | return 0; |
| 744 | } |
| 745 | #endif /* HAVE_IPV6 */ |
| 746 | |
| 747 | int |
Timo Teräs | 81b139b | 2015-04-29 09:43:01 +0300 | [diff] [blame] | 748 | sockunion_cmp (const union sockunion *su1, const union sockunion *su2) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 749 | { |
| 750 | if (su1->sa.sa_family > su2->sa.sa_family) |
| 751 | return 1; |
| 752 | if (su1->sa.sa_family < su2->sa.sa_family) |
| 753 | return -1; |
| 754 | |
| 755 | if (su1->sa.sa_family == AF_INET) |
| 756 | { |
Jorge Boncompte [DTI2] | 1282932 | 2012-04-10 16:57:25 +0200 | [diff] [blame] | 757 | if (ntohl (sockunion2ip (su1)) == ntohl (sockunion2ip (su2))) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 758 | return 0; |
Jorge Boncompte [DTI2] | 1282932 | 2012-04-10 16:57:25 +0200 | [diff] [blame] | 759 | if (ntohl (sockunion2ip (su1)) > ntohl (sockunion2ip (su2))) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 760 | return 1; |
| 761 | else |
| 762 | return -1; |
| 763 | } |
| 764 | #ifdef HAVE_IPV6 |
| 765 | if (su1->sa.sa_family == AF_INET6) |
| 766 | return in6addr_cmp (&su1->sin6.sin6_addr, &su2->sin6.sin6_addr); |
| 767 | #endif /* HAVE_IPV6 */ |
| 768 | return 0; |
| 769 | } |
| 770 | |
| 771 | /* Duplicate sockunion. */ |
| 772 | union sockunion * |
Timo Teräs | 81b139b | 2015-04-29 09:43:01 +0300 | [diff] [blame] | 773 | sockunion_dup (const union sockunion *su) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 774 | { |
| 775 | union sockunion *dup = XCALLOC (MTYPE_SOCKUNION, sizeof (union sockunion)); |
| 776 | memcpy (dup, su, sizeof (union sockunion)); |
| 777 | return dup; |
| 778 | } |
| 779 | |
| 780 | void |
| 781 | sockunion_free (union sockunion *su) |
| 782 | { |
| 783 | XFREE (MTYPE_SOCKUNION, su); |
| 784 | } |