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 | { |
| 169 | if (su->sa.sa_family == AF_INET) |
| 170 | return inet_ntop (AF_INET, &su->sin.sin_addr, buf, len); |
| 171 | #ifdef HAVE_IPV6 |
| 172 | else if (su->sa.sa_family == AF_INET6) |
| 173 | return inet_ntop (AF_INET6, &su->sin6.sin6_addr, buf, len); |
| 174 | #endif /* HAVE_IPV6 */ |
| 175 | return NULL; |
| 176 | } |
| 177 | |
Paul Jakma | 2fb2a45 | 2012-06-14 10:37:40 +0100 | [diff] [blame] | 178 | union sockunion * |
| 179 | sockunion_str2su (const char *str) |
| 180 | { |
| 181 | union sockunion *su = XCALLOC (MTYPE_SOCKUNION, sizeof (union sockunion)); |
| 182 | |
| 183 | if (!str2sockunion (str, su)) |
| 184 | return su; |
| 185 | |
| 186 | XFREE (MTYPE_SOCKUNION, su); |
| 187 | return NULL; |
| 188 | } |
| 189 | |
Paul Jakma | 1a7dcf4 | 2008-09-09 21:17:04 +0100 | [diff] [blame] | 190 | /* Convert IPv4 compatible IPv6 address to IPv4 address. */ |
| 191 | static void |
| 192 | sockunion_normalise_mapped (union sockunion *su) |
| 193 | { |
| 194 | struct sockaddr_in sin; |
| 195 | |
| 196 | #ifdef HAVE_IPV6 |
| 197 | if (su->sa.sa_family == AF_INET6 |
| 198 | && IN6_IS_ADDR_V4MAPPED (&su->sin6.sin6_addr)) |
| 199 | { |
| 200 | memset (&sin, 0, sizeof (struct sockaddr_in)); |
| 201 | sin.sin_family = AF_INET; |
Stephen Hemminger | 3fa3f95 | 2009-07-11 21:27:51 -0700 | [diff] [blame] | 202 | sin.sin_port = su->sin6.sin6_port; |
Paul Jakma | 1a7dcf4 | 2008-09-09 21:17:04 +0100 | [diff] [blame] | 203 | memcpy (&sin.sin_addr, ((char *)&su->sin6.sin6_addr) + 12, 4); |
| 204 | memcpy (su, &sin, sizeof (struct sockaddr_in)); |
| 205 | } |
| 206 | #endif /* HAVE_IPV6 */ |
| 207 | } |
| 208 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 209 | /* Return socket of sockunion. */ |
| 210 | int |
Timo Teräs | 81b139b | 2015-04-29 09:43:01 +0300 | [diff] [blame] | 211 | sockunion_socket (const union sockunion *su) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 212 | { |
| 213 | int sock; |
| 214 | |
| 215 | sock = socket (su->sa.sa_family, SOCK_STREAM, 0); |
| 216 | if (sock < 0) |
| 217 | { |
ajs | 6099b3b | 2004-11-20 02:06:59 +0000 | [diff] [blame] | 218 | zlog (NULL, LOG_WARNING, "Can't make socket : %s", safe_strerror (errno)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 219 | return -1; |
| 220 | } |
| 221 | |
| 222 | return sock; |
| 223 | } |
| 224 | |
| 225 | /* Return accepted new socket file descriptor. */ |
| 226 | int |
| 227 | sockunion_accept (int sock, union sockunion *su) |
| 228 | { |
| 229 | socklen_t len; |
| 230 | int client_sock; |
| 231 | |
| 232 | len = sizeof (union sockunion); |
| 233 | client_sock = accept (sock, (struct sockaddr *) su, &len); |
| 234 | |
Paul Jakma | 84152ee | 2008-11-24 22:25:16 +0000 | [diff] [blame] | 235 | sockunion_normalise_mapped (su); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 236 | return client_sock; |
| 237 | } |
| 238 | |
| 239 | /* Return sizeof union sockunion. */ |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame] | 240 | static int |
Timo Teräs | 81b139b | 2015-04-29 09:43:01 +0300 | [diff] [blame] | 241 | sockunion_sizeof (const union sockunion *su) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 242 | { |
| 243 | int ret; |
| 244 | |
| 245 | ret = 0; |
| 246 | switch (su->sa.sa_family) |
| 247 | { |
| 248 | case AF_INET: |
| 249 | ret = sizeof (struct sockaddr_in); |
| 250 | break; |
| 251 | #ifdef HAVE_IPV6 |
| 252 | case AF_INET6: |
| 253 | ret = sizeof (struct sockaddr_in6); |
| 254 | break; |
| 255 | #endif /* AF_INET6 */ |
| 256 | } |
| 257 | return ret; |
| 258 | } |
| 259 | |
| 260 | /* return sockunion structure : this function should be revised. */ |
Stephen Hemminger | b24b19f | 2011-12-06 14:09:18 +0400 | [diff] [blame] | 261 | static const char * |
Timo Teräs | 81b139b | 2015-04-29 09:43:01 +0300 | [diff] [blame] | 262 | sockunion_log (const union sockunion *su, char *buf, size_t len) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 263 | { |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 264 | switch (su->sa.sa_family) |
| 265 | { |
| 266 | case AF_INET: |
Stephen Hemminger | b24b19f | 2011-12-06 14:09:18 +0400 | [diff] [blame] | 267 | return inet_ntop(AF_INET, &su->sin.sin_addr, buf, len); |
| 268 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 269 | #ifdef HAVE_IPV6 |
| 270 | case AF_INET6: |
Stephen Hemminger | b24b19f | 2011-12-06 14:09:18 +0400 | [diff] [blame] | 271 | return inet_ntop(AF_INET6, &(su->sin6.sin6_addr), buf, len); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 272 | break; |
| 273 | #endif /* HAVE_IPV6 */ |
Stephen Hemminger | b24b19f | 2011-12-06 14:09:18 +0400 | [diff] [blame] | 274 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 275 | default: |
Stephen Hemminger | b24b19f | 2011-12-06 14:09:18 +0400 | [diff] [blame] | 276 | snprintf (buf, len, "af_unknown %d ", su->sa.sa_family); |
| 277 | return buf; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 278 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | /* sockunion_connect returns |
| 282 | -1 : error occured |
| 283 | 0 : connect success |
| 284 | 1 : connect is in progress */ |
| 285 | enum connect_result |
Timo Teräs | 81b139b | 2015-04-29 09:43:01 +0300 | [diff] [blame] | 286 | sockunion_connect (int fd, const union sockunion *peersu, unsigned short port, |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 287 | unsigned int ifindex) |
| 288 | { |
| 289 | int ret; |
| 290 | int val; |
| 291 | union sockunion su; |
| 292 | |
| 293 | memcpy (&su, peersu, sizeof (union sockunion)); |
| 294 | |
| 295 | switch (su.sa.sa_family) |
| 296 | { |
| 297 | case AF_INET: |
| 298 | su.sin.sin_port = port; |
| 299 | break; |
| 300 | #ifdef HAVE_IPV6 |
| 301 | case AF_INET6: |
| 302 | su.sin6.sin6_port = port; |
| 303 | #ifdef KAME |
| 304 | if (IN6_IS_ADDR_LINKLOCAL(&su.sin6.sin6_addr) && ifindex) |
| 305 | { |
Paul Jakma | 6f0e3f6 | 2007-05-10 02:38:51 +0000 | [diff] [blame] | 306 | #ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 307 | /* su.sin6.sin6_scope_id = ifindex; */ |
Paul Jakma | 6f0e3f6 | 2007-05-10 02:38:51 +0000 | [diff] [blame] | 308 | #endif /* HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 309 | SET_IN6_LINKLOCAL_IFINDEX (su.sin6.sin6_addr, ifindex); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 310 | } |
| 311 | #endif /* KAME */ |
| 312 | break; |
| 313 | #endif /* HAVE_IPV6 */ |
| 314 | } |
| 315 | |
| 316 | /* Make socket non-block. */ |
| 317 | val = fcntl (fd, F_GETFL, 0); |
| 318 | fcntl (fd, F_SETFL, val|O_NONBLOCK); |
| 319 | |
| 320 | /* Call connect function. */ |
| 321 | ret = connect (fd, (struct sockaddr *) &su, sockunion_sizeof (&su)); |
| 322 | |
| 323 | /* Immediate success */ |
| 324 | if (ret == 0) |
| 325 | { |
| 326 | fcntl (fd, F_SETFL, val); |
| 327 | return connect_success; |
| 328 | } |
| 329 | |
| 330 | /* If connect is in progress then return 1 else it's real error. */ |
| 331 | if (ret < 0) |
| 332 | { |
| 333 | if (errno != EINPROGRESS) |
| 334 | { |
Stephen Hemminger | b24b19f | 2011-12-06 14:09:18 +0400 | [diff] [blame] | 335 | char str[SU_ADDRSTRLEN]; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 336 | zlog_info ("can't connect to %s fd %d : %s", |
Stephen Hemminger | b24b19f | 2011-12-06 14:09:18 +0400 | [diff] [blame] | 337 | sockunion_log (&su, str, sizeof str), |
| 338 | fd, safe_strerror (errno)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 339 | return connect_error; |
| 340 | } |
| 341 | } |
| 342 | |
| 343 | fcntl (fd, F_SETFL, val); |
| 344 | |
| 345 | return connect_in_progress; |
| 346 | } |
| 347 | |
| 348 | /* Make socket from sockunion union. */ |
| 349 | int |
| 350 | sockunion_stream_socket (union sockunion *su) |
| 351 | { |
| 352 | int sock; |
| 353 | |
| 354 | if (su->sa.sa_family == 0) |
| 355 | su->sa.sa_family = AF_INET_UNION; |
| 356 | |
| 357 | sock = socket (su->sa.sa_family, SOCK_STREAM, 0); |
| 358 | |
| 359 | if (sock < 0) |
| 360 | zlog (NULL, LOG_WARNING, "can't make socket sockunion_stream_socket"); |
| 361 | |
| 362 | return sock; |
| 363 | } |
| 364 | |
| 365 | /* Bind socket to specified address. */ |
| 366 | int |
| 367 | sockunion_bind (int sock, union sockunion *su, unsigned short port, |
| 368 | union sockunion *su_addr) |
| 369 | { |
| 370 | int size = 0; |
| 371 | int ret; |
| 372 | |
| 373 | if (su->sa.sa_family == AF_INET) |
| 374 | { |
| 375 | size = sizeof (struct sockaddr_in); |
| 376 | su->sin.sin_port = htons (port); |
Paul Jakma | 6f0e3f6 | 2007-05-10 02:38:51 +0000 | [diff] [blame] | 377 | #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 378 | su->sin.sin_len = size; |
Paul Jakma | 6f0e3f6 | 2007-05-10 02:38:51 +0000 | [diff] [blame] | 379 | #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 380 | if (su_addr == NULL) |
Jorge Boncompte [DTI2] | 1282932 | 2012-04-10 16:57:25 +0200 | [diff] [blame] | 381 | sockunion2ip (su) = htonl (INADDR_ANY); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 382 | } |
| 383 | #ifdef HAVE_IPV6 |
| 384 | else if (su->sa.sa_family == AF_INET6) |
| 385 | { |
| 386 | size = sizeof (struct sockaddr_in6); |
| 387 | su->sin6.sin6_port = htons (port); |
| 388 | #ifdef SIN6_LEN |
| 389 | su->sin6.sin6_len = size; |
| 390 | #endif /* SIN6_LEN */ |
| 391 | if (su_addr == NULL) |
| 392 | { |
David Lamparter | 6d6df30 | 2014-06-28 21:12:37 +0200 | [diff] [blame] | 393 | #ifdef LINUX_IPV6 |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 394 | memset (&su->sin6.sin6_addr, 0, sizeof (struct in6_addr)); |
| 395 | #else |
| 396 | su->sin6.sin6_addr = in6addr_any; |
| 397 | #endif /* LINUX_IPV6 */ |
| 398 | } |
| 399 | } |
| 400 | #endif /* HAVE_IPV6 */ |
| 401 | |
| 402 | |
| 403 | ret = bind (sock, (struct sockaddr *)su, size); |
| 404 | if (ret < 0) |
ajs | 6099b3b | 2004-11-20 02:06:59 +0000 | [diff] [blame] | 405 | zlog (NULL, LOG_WARNING, "can't bind socket : %s", safe_strerror (errno)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 406 | |
| 407 | return ret; |
| 408 | } |
| 409 | |
| 410 | int |
| 411 | sockopt_reuseaddr (int sock) |
| 412 | { |
| 413 | int ret; |
| 414 | int on = 1; |
| 415 | |
| 416 | ret = setsockopt (sock, SOL_SOCKET, SO_REUSEADDR, |
| 417 | (void *) &on, sizeof (on)); |
| 418 | if (ret < 0) |
| 419 | { |
| 420 | zlog (NULL, LOG_WARNING, "can't set sockopt SO_REUSEADDR to socket %d", sock); |
| 421 | return -1; |
| 422 | } |
| 423 | return 0; |
| 424 | } |
| 425 | |
| 426 | #ifdef SO_REUSEPORT |
| 427 | int |
| 428 | sockopt_reuseport (int sock) |
| 429 | { |
| 430 | int ret; |
| 431 | int on = 1; |
| 432 | |
| 433 | ret = setsockopt (sock, SOL_SOCKET, SO_REUSEPORT, |
| 434 | (void *) &on, sizeof (on)); |
| 435 | if (ret < 0) |
| 436 | { |
hasso | e7fe8c8 | 2005-05-06 19:33:35 +0000 | [diff] [blame] | 437 | 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] | 438 | return -1; |
| 439 | } |
| 440 | return 0; |
| 441 | } |
| 442 | #else |
| 443 | int |
| 444 | sockopt_reuseport (int sock) |
| 445 | { |
| 446 | return 0; |
| 447 | } |
| 448 | #endif /* 0 */ |
| 449 | |
| 450 | int |
| 451 | sockopt_ttl (int family, int sock, int ttl) |
| 452 | { |
| 453 | int ret; |
| 454 | |
| 455 | #ifdef IP_TTL |
| 456 | if (family == AF_INET) |
| 457 | { |
| 458 | ret = setsockopt (sock, IPPROTO_IP, IP_TTL, |
| 459 | (void *) &ttl, sizeof (int)); |
| 460 | if (ret < 0) |
| 461 | { |
| 462 | zlog (NULL, LOG_WARNING, "can't set sockopt IP_TTL %d to socket %d", ttl, sock); |
| 463 | return -1; |
| 464 | } |
| 465 | return 0; |
| 466 | } |
| 467 | #endif /* IP_TTL */ |
| 468 | #ifdef HAVE_IPV6 |
| 469 | if (family == AF_INET6) |
| 470 | { |
| 471 | ret = setsockopt (sock, IPPROTO_IPV6, IPV6_UNICAST_HOPS, |
| 472 | (void *) &ttl, sizeof (int)); |
| 473 | if (ret < 0) |
| 474 | { |
| 475 | zlog (NULL, LOG_WARNING, "can't set sockopt IPV6_UNICAST_HOPS %d to socket %d", |
| 476 | ttl, sock); |
| 477 | return -1; |
| 478 | } |
| 479 | return 0; |
| 480 | } |
| 481 | #endif /* HAVE_IPV6 */ |
| 482 | return 0; |
| 483 | } |
| 484 | |
Stephen Hemminger | 58192df | 2010-08-05 10:26:24 -0700 | [diff] [blame] | 485 | int |
| 486 | sockopt_cork (int sock, int onoff) |
| 487 | { |
| 488 | #ifdef TCP_CORK |
| 489 | return setsockopt (sock, IPPROTO_TCP, TCP_CORK, &onoff, sizeof(onoff)); |
| 490 | #else |
| 491 | return 0; |
| 492 | #endif |
| 493 | } |
| 494 | |
Nick Hilliard | fa411a2 | 2011-03-23 15:33:17 +0000 | [diff] [blame] | 495 | int |
| 496 | sockopt_minttl (int family, int sock, int minttl) |
| 497 | { |
Stephen Hemminger | 89b6d1f | 2011-03-24 10:51:59 +0000 | [diff] [blame] | 498 | #ifdef IP_MINTTL |
Stephen Hemminger | d876bdf | 2010-08-05 10:26:27 -0700 | [diff] [blame] | 499 | if (family == AF_INET) |
Nick Hilliard | fa411a2 | 2011-03-23 15:33:17 +0000 | [diff] [blame] | 500 | { |
Stephen Hemminger | d876bdf | 2010-08-05 10:26:27 -0700 | [diff] [blame] | 501 | int ret = setsockopt (sock, IPPROTO_IP, IP_MINTTL, &minttl, sizeof(minttl)); |
| 502 | if (ret < 0) |
| 503 | zlog (NULL, LOG_WARNING, |
| 504 | "can't set sockopt IP_MINTTL to %d on socket %d: %s", |
| 505 | minttl, sock, safe_strerror (errno)); |
| 506 | return ret; |
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 | #endif /* IP_MINTTL */ |
David Lamparter | 05a69d2 | 2015-05-27 22:15:37 +0200 | [diff] [blame] | 509 | #ifdef IPV6_MINHOPCNT |
Stephen Hemminger | d876bdf | 2010-08-05 10:26:27 -0700 | [diff] [blame] | 510 | if (family == AF_INET6) |
| 511 | { |
David Lamparter | 05a69d2 | 2015-05-27 22:15:37 +0200 | [diff] [blame] | 512 | int ret = setsockopt (sock, IPPROTO_IPV6, IPV6_MINHOPCNT, &minttl, sizeof(minttl)); |
Stephen Hemminger | d876bdf | 2010-08-05 10:26:27 -0700 | [diff] [blame] | 513 | if (ret < 0) |
| 514 | zlog (NULL, LOG_WARNING, |
David Lamparter | 05a69d2 | 2015-05-27 22:15:37 +0200 | [diff] [blame] | 515 | "can't set sockopt IPV6_MINHOPCNT to %d on socket %d: %s", |
Stephen Hemminger | d876bdf | 2010-08-05 10:26:27 -0700 | [diff] [blame] | 516 | minttl, sock, safe_strerror (errno)); |
| 517 | return ret; |
| 518 | } |
| 519 | #endif |
Nick Hilliard | fa411a2 | 2011-03-23 15:33:17 +0000 | [diff] [blame] | 520 | |
Stephen Hemminger | 89b6d1f | 2011-03-24 10:51:59 +0000 | [diff] [blame] | 521 | errno = EOPNOTSUPP; |
| 522 | return -1; |
Nick Hilliard | fa411a2 | 2011-03-23 15:33:17 +0000 | [diff] [blame] | 523 | } |
| 524 | |
David Lamparter | ca05126 | 2009-10-04 16:21:49 +0200 | [diff] [blame] | 525 | int |
| 526 | sockopt_v6only (int family, int sock) |
| 527 | { |
| 528 | int ret, on = 1; |
| 529 | |
| 530 | #ifdef HAVE_IPV6 |
| 531 | #ifdef IPV6_V6ONLY |
| 532 | if (family == AF_INET6) |
| 533 | { |
| 534 | ret = setsockopt (sock, IPPROTO_IPV6, IPV6_V6ONLY, |
| 535 | (void *) &on, sizeof (int)); |
| 536 | if (ret < 0) |
| 537 | { |
| 538 | zlog (NULL, LOG_WARNING, "can't set sockopt IPV6_V6ONLY " |
| 539 | "to socket %d", sock); |
| 540 | return -1; |
| 541 | } |
| 542 | return 0; |
| 543 | } |
| 544 | #endif /* IPV6_V6ONLY */ |
| 545 | #endif /* HAVE_IPV6 */ |
| 546 | return 0; |
| 547 | } |
| 548 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 549 | /* If same family and same prefix return 1. */ |
| 550 | int |
Timo Teräs | 81b139b | 2015-04-29 09:43:01 +0300 | [diff] [blame] | 551 | sockunion_same (const union sockunion *su1, const union sockunion *su2) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 552 | { |
| 553 | int ret = 0; |
| 554 | |
| 555 | if (su1->sa.sa_family != su2->sa.sa_family) |
| 556 | return 0; |
| 557 | |
| 558 | switch (su1->sa.sa_family) |
| 559 | { |
| 560 | case AF_INET: |
| 561 | ret = memcmp (&su1->sin.sin_addr, &su2->sin.sin_addr, |
| 562 | sizeof (struct in_addr)); |
| 563 | break; |
| 564 | #ifdef HAVE_IPV6 |
| 565 | case AF_INET6: |
| 566 | ret = memcmp (&su1->sin6.sin6_addr, &su2->sin6.sin6_addr, |
| 567 | sizeof (struct in6_addr)); |
| 568 | break; |
| 569 | #endif /* HAVE_IPV6 */ |
| 570 | } |
| 571 | if (ret == 0) |
| 572 | return 1; |
| 573 | else |
| 574 | return 0; |
| 575 | } |
| 576 | |
Timo Teräs | 9196caf | 2015-04-29 09:43:05 +0300 | [diff] [blame] | 577 | unsigned int |
| 578 | sockunion_hash (const union sockunion *su) |
| 579 | { |
| 580 | switch (sockunion_family(su)) |
| 581 | { |
| 582 | case AF_INET: |
| 583 | return jhash_1word(su->sin.sin_addr.s_addr, 0); |
| 584 | #ifdef HAVE_IPV6 |
| 585 | case AF_INET6: |
| 586 | return jhash2(su->sin6.sin6_addr.s6_addr32, ZEBRA_NUM_OF(su->sin6.sin6_addr.s6_addr32), 0); |
| 587 | #endif /* HAVE_IPV6 */ |
| 588 | } |
| 589 | return 0; |
| 590 | } |
| 591 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 592 | /* After TCP connection is established. Get local address and port. */ |
| 593 | union sockunion * |
| 594 | sockunion_getsockname (int fd) |
| 595 | { |
| 596 | int ret; |
paul | 2252829 | 2004-05-08 05:10:38 +0000 | [diff] [blame] | 597 | socklen_t len; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 598 | union |
| 599 | { |
| 600 | struct sockaddr sa; |
| 601 | struct sockaddr_in sin; |
| 602 | #ifdef HAVE_IPV6 |
| 603 | struct sockaddr_in6 sin6; |
| 604 | #endif /* HAVE_IPV6 */ |
| 605 | char tmp_buffer[128]; |
| 606 | } name; |
| 607 | union sockunion *su; |
| 608 | |
| 609 | memset (&name, 0, sizeof name); |
| 610 | len = sizeof name; |
| 611 | |
| 612 | ret = getsockname (fd, (struct sockaddr *)&name, &len); |
| 613 | if (ret < 0) |
| 614 | { |
| 615 | zlog_warn ("Can't get local address and port by getsockname: %s", |
ajs | 6099b3b | 2004-11-20 02:06:59 +0000 | [diff] [blame] | 616 | safe_strerror (errno)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 617 | return NULL; |
| 618 | } |
| 619 | |
| 620 | if (name.sa.sa_family == AF_INET) |
| 621 | { |
paul | 2ba9a37 | 2005-05-19 01:37:50 +0000 | [diff] [blame] | 622 | su = XCALLOC (MTYPE_SOCKUNION, sizeof (union sockunion)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 623 | memcpy (su, &name, sizeof (struct sockaddr_in)); |
| 624 | return su; |
| 625 | } |
| 626 | #ifdef HAVE_IPV6 |
| 627 | if (name.sa.sa_family == AF_INET6) |
| 628 | { |
paul | 2ba9a37 | 2005-05-19 01:37:50 +0000 | [diff] [blame] | 629 | su = XCALLOC (MTYPE_SOCKUNION, sizeof (union sockunion)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 630 | memcpy (su, &name, sizeof (struct sockaddr_in6)); |
Paul Jakma | 1a7dcf4 | 2008-09-09 21:17:04 +0100 | [diff] [blame] | 631 | sockunion_normalise_mapped (su); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 632 | return su; |
| 633 | } |
| 634 | #endif /* HAVE_IPV6 */ |
| 635 | return NULL; |
| 636 | } |
| 637 | |
| 638 | /* After TCP connection is established. Get remote address and port. */ |
| 639 | union sockunion * |
| 640 | sockunion_getpeername (int fd) |
| 641 | { |
| 642 | int ret; |
paul | 2252829 | 2004-05-08 05:10:38 +0000 | [diff] [blame] | 643 | socklen_t len; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 644 | union |
| 645 | { |
| 646 | struct sockaddr sa; |
| 647 | struct sockaddr_in sin; |
| 648 | #ifdef HAVE_IPV6 |
| 649 | struct sockaddr_in6 sin6; |
| 650 | #endif /* HAVE_IPV6 */ |
| 651 | char tmp_buffer[128]; |
| 652 | } name; |
| 653 | union sockunion *su; |
| 654 | |
| 655 | memset (&name, 0, sizeof name); |
| 656 | len = sizeof name; |
| 657 | ret = getpeername (fd, (struct sockaddr *)&name, &len); |
| 658 | if (ret < 0) |
| 659 | { |
| 660 | zlog (NULL, LOG_WARNING, "Can't get remote address and port: %s", |
ajs | 6099b3b | 2004-11-20 02:06:59 +0000 | [diff] [blame] | 661 | safe_strerror (errno)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 662 | return NULL; |
| 663 | } |
| 664 | |
| 665 | if (name.sa.sa_family == AF_INET) |
| 666 | { |
paul | 2ba9a37 | 2005-05-19 01:37:50 +0000 | [diff] [blame] | 667 | su = XCALLOC (MTYPE_SOCKUNION, sizeof (union sockunion)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 668 | memcpy (su, &name, sizeof (struct sockaddr_in)); |
| 669 | return su; |
| 670 | } |
| 671 | #ifdef HAVE_IPV6 |
| 672 | if (name.sa.sa_family == AF_INET6) |
| 673 | { |
paul | 2ba9a37 | 2005-05-19 01:37:50 +0000 | [diff] [blame] | 674 | su = XCALLOC (MTYPE_SOCKUNION, sizeof (union sockunion)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 675 | memcpy (su, &name, sizeof (struct sockaddr_in6)); |
Paul Jakma | 1a7dcf4 | 2008-09-09 21:17:04 +0100 | [diff] [blame] | 676 | sockunion_normalise_mapped (su); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 677 | return su; |
| 678 | } |
| 679 | #endif /* HAVE_IPV6 */ |
| 680 | return NULL; |
| 681 | } |
| 682 | |
| 683 | /* Print sockunion structure */ |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame] | 684 | static void __attribute__ ((unused)) |
Timo Teräs | 81b139b | 2015-04-29 09:43:01 +0300 | [diff] [blame] | 685 | sockunion_print (const union sockunion *su) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 686 | { |
| 687 | if (su == NULL) |
| 688 | return; |
| 689 | |
| 690 | switch (su->sa.sa_family) |
| 691 | { |
| 692 | case AF_INET: |
| 693 | printf ("%s\n", inet_ntoa (su->sin.sin_addr)); |
| 694 | break; |
| 695 | #ifdef HAVE_IPV6 |
| 696 | case AF_INET6: |
| 697 | { |
paul | 42d4986 | 2004-10-13 05:22:18 +0000 | [diff] [blame] | 698 | char buf [SU_ADDRSTRLEN]; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 699 | |
| 700 | printf ("%s\n", inet_ntop (AF_INET6, &(su->sin6.sin6_addr), |
| 701 | buf, sizeof (buf))); |
| 702 | } |
| 703 | break; |
| 704 | #endif /* HAVE_IPV6 */ |
| 705 | |
| 706 | #ifdef AF_LINK |
| 707 | case AF_LINK: |
| 708 | { |
| 709 | struct sockaddr_dl *sdl; |
| 710 | |
| 711 | sdl = (struct sockaddr_dl *)&(su->sa); |
| 712 | printf ("link#%d\n", sdl->sdl_index); |
| 713 | } |
| 714 | break; |
| 715 | #endif /* AF_LINK */ |
| 716 | default: |
| 717 | printf ("af_unknown %d\n", su->sa.sa_family); |
| 718 | break; |
| 719 | } |
| 720 | } |
| 721 | |
| 722 | #ifdef HAVE_IPV6 |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame] | 723 | static int |
Timo Teräs | 81b139b | 2015-04-29 09:43:01 +0300 | [diff] [blame] | 724 | in6addr_cmp (const struct in6_addr *addr1, const struct in6_addr *addr2) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 725 | { |
hasso | 8c328f1 | 2004-10-05 21:01:23 +0000 | [diff] [blame] | 726 | unsigned int i; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 727 | u_char *p1, *p2; |
| 728 | |
| 729 | p1 = (u_char *)addr1; |
| 730 | p2 = (u_char *)addr2; |
| 731 | |
| 732 | for (i = 0; i < sizeof (struct in6_addr); i++) |
| 733 | { |
| 734 | if (p1[i] > p2[i]) |
| 735 | return 1; |
| 736 | else if (p1[i] < p2[i]) |
| 737 | return -1; |
| 738 | } |
| 739 | return 0; |
| 740 | } |
| 741 | #endif /* HAVE_IPV6 */ |
| 742 | |
| 743 | int |
Timo Teräs | 81b139b | 2015-04-29 09:43:01 +0300 | [diff] [blame] | 744 | sockunion_cmp (const union sockunion *su1, const union sockunion *su2) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 745 | { |
| 746 | if (su1->sa.sa_family > su2->sa.sa_family) |
| 747 | return 1; |
| 748 | if (su1->sa.sa_family < su2->sa.sa_family) |
| 749 | return -1; |
| 750 | |
| 751 | if (su1->sa.sa_family == AF_INET) |
| 752 | { |
Jorge Boncompte [DTI2] | 1282932 | 2012-04-10 16:57:25 +0200 | [diff] [blame] | 753 | if (ntohl (sockunion2ip (su1)) == ntohl (sockunion2ip (su2))) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 754 | return 0; |
Jorge Boncompte [DTI2] | 1282932 | 2012-04-10 16:57:25 +0200 | [diff] [blame] | 755 | if (ntohl (sockunion2ip (su1)) > ntohl (sockunion2ip (su2))) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 756 | return 1; |
| 757 | else |
| 758 | return -1; |
| 759 | } |
| 760 | #ifdef HAVE_IPV6 |
| 761 | if (su1->sa.sa_family == AF_INET6) |
| 762 | return in6addr_cmp (&su1->sin6.sin6_addr, &su2->sin6.sin6_addr); |
| 763 | #endif /* HAVE_IPV6 */ |
| 764 | return 0; |
| 765 | } |
| 766 | |
| 767 | /* Duplicate sockunion. */ |
| 768 | union sockunion * |
Timo Teräs | 81b139b | 2015-04-29 09:43:01 +0300 | [diff] [blame] | 769 | sockunion_dup (const union sockunion *su) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 770 | { |
| 771 | union sockunion *dup = XCALLOC (MTYPE_SOCKUNION, sizeof (union sockunion)); |
| 772 | memcpy (dup, su, sizeof (union sockunion)); |
| 773 | return dup; |
| 774 | } |
| 775 | |
| 776 | void |
| 777 | sockunion_free (union sockunion *su) |
| 778 | { |
| 779 | XFREE (MTYPE_SOCKUNION, su); |
| 780 | } |