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