]> git.proxmox.com Git - mirror_frr.git/blame - lib/sockopt.c
Merge pull request #12708 from donaldsharp/no_notification
[mirror_frr.git] / lib / sockopt.c
CommitLineData
718e3744 1/* setsockopt functions
2 * Copyright (C) 1999 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 *
896014f4
DL
16 * You should have received a copy of the GNU General Public License along
17 * with this program; see the file COPYING; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
718e3744 19 */
20
21#include <zebra.h>
e6f8d095 22
718e3744 23#include "log.h"
e6822768 24#include "sockopt.h"
0df7c91f 25#include "sockunion.h"
481bc15f 26#include "lib_errors.h"
718e3744 27
1b636c0a
DL
28#if (defined(__FreeBSD__) \
29 && ((__FreeBSD_version >= 500022 && __FreeBSD_version < 700000) \
30 || (__FreeBSD_version < 500000 && __FreeBSD_version >= 440000))) \
31 || (defined(__NetBSD__) && defined(__NetBSD_Version__) \
32 && __NetBSD_Version__ >= 106010000) \
33 || defined(__OpenBSD__) || defined(__APPLE__) \
34 || defined(__DragonFly__) || defined(__sun)
35#define HAVE_BSD_STRUCT_IP_MREQ_HACK
36#endif
37
d62a17ae 38void setsockopt_so_recvbuf(int sock, int size)
0b3acf4f 39{
d62a17ae 40 int orig_req = size;
6228a3b8 41
d62a17ae 42 while (setsockopt(sock, SOL_SOCKET, SO_RCVBUF, &size, sizeof(size))
43 == -1)
44 size /= 2;
0b3acf4f 45
d62a17ae 46 if (size != orig_req)
450971aa 47 flog_err(EC_LIB_SOCKET,
4496fbb3
DS
48 "%s: fd %d: SO_RCVBUF set to %d (requested %d)",
49 __func__, sock, size, orig_req);
0b3acf4f 50}
51
d62a17ae 52void setsockopt_so_sendbuf(const int sock, int size)
b7fe4141 53{
d62a17ae 54 int orig_req = size;
6228a3b8 55
d62a17ae 56 while (setsockopt(sock, SOL_SOCKET, SO_SNDBUF, &size, sizeof(size))
57 == -1)
58 size /= 2;
b7fe4141 59
d62a17ae 60 if (size != orig_req)
450971aa 61 flog_err(EC_LIB_SOCKET,
4496fbb3
DS
62 "%s: fd %d: SO_SNDBUF set to %d (requested %d)",
63 __func__, sock, size, orig_req);
b7fe4141
DO
64}
65
d62a17ae 66int getsockopt_so_sendbuf(const int sock)
b7fe4141 67{
d7c0a89a 68 uint32_t optval;
d62a17ae 69 socklen_t optlen = sizeof(optval);
70 int ret = getsockopt(sock, SOL_SOCKET, SO_SNDBUF, (char *)&optval,
71 &optlen);
72 if (ret < 0) {
450971aa 73 flog_err_sys(EC_LIB_SYSTEM_CALL,
09c866e3
QY
74 "fd %d: can't getsockopt SO_SNDBUF: %d (%s)", sock,
75 errno, safe_strerror(errno));
d62a17ae 76 return ret;
77 }
78 return optval;
b7fe4141
DO
79}
80
772aae8b
DS
81int getsockopt_so_recvbuf(const int sock)
82{
83 uint32_t optval;
84 socklen_t optlen = sizeof(optval);
85 int ret = getsockopt(sock, SOL_SOCKET, SO_RCVBUF, (char *)&optval,
86 &optlen);
87 if (ret < 0) {
88 flog_err_sys(EC_LIB_SYSTEM_CALL,
89 "fd %d: can't getsockopt SO_RCVBUF: %d (%s)", sock,
90 errno, safe_strerror(errno));
91 return ret;
92 }
93 return optval;
94}
95
d62a17ae 96static void *getsockopt_cmsg_data(struct msghdr *msgh, int level, int type)
4f7baa0e 97{
d62a17ae 98 struct cmsghdr *cmsg;
d62a17ae 99
adf0e7c6 100 for (cmsg = CMSG_FIRSTHDR(msgh); cmsg != NULL;
d62a17ae 101 cmsg = CMSG_NXTHDR(msgh, cmsg))
dc094865 102 if (cmsg->cmsg_level == level && cmsg->cmsg_type == type)
5037cc3e 103 return CMSG_DATA(cmsg);
d62a17ae 104
105 return NULL;
4f7baa0e 106}
107
718e3744 108/* Set IPv6 packet info to the socket. */
d62a17ae 109int setsockopt_ipv6_pktinfo(int sock, int val)
718e3744 110{
d62a17ae 111 int ret;
112
113#ifdef IPV6_RECVPKTINFO /*2292bis-01*/
114 ret = setsockopt(sock, IPPROTO_IPV6, IPV6_RECVPKTINFO, &val,
115 sizeof(val));
116 if (ret < 0)
450971aa 117 flog_err(EC_LIB_SOCKET,
4496fbb3
DS
118 "can't setsockopt IPV6_RECVPKTINFO : %s",
119 safe_strerror(errno));
d62a17ae 120#else /*RFC2292*/
121 ret = setsockopt(sock, IPPROTO_IPV6, IPV6_PKTINFO, &val, sizeof(val));
122 if (ret < 0)
450971aa 123 flog_err(EC_LIB_SOCKET, "can't setsockopt IPV6_PKTINFO : %s",
4496fbb3 124 safe_strerror(errno));
03bad95a 125#endif /* IANA_IPV6 */
d62a17ae 126 return ret;
718e3744 127}
128
718e3744 129/* Set multicast hops val to the socket. */
d62a17ae 130int setsockopt_ipv6_multicast_hops(int sock, int val)
718e3744 131{
d62a17ae 132 int ret;
718e3744 133
d62a17ae 134 ret = setsockopt(sock, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &val,
135 sizeof(val));
136 if (ret < 0)
1c50c1c0 137 flog_err(EC_LIB_SOCKET, "can't setsockopt IPV6_MULTICAST_HOPS");
d62a17ae 138 return ret;
718e3744 139}
140
141/* Set multicast hops val to the socket. */
d62a17ae 142int setsockopt_ipv6_unicast_hops(int sock, int val)
718e3744 143{
d62a17ae 144 int ret;
718e3744 145
d62a17ae 146 ret = setsockopt(sock, IPPROTO_IPV6, IPV6_UNICAST_HOPS, &val,
147 sizeof(val));
148 if (ret < 0)
450971aa 149 flog_err(EC_LIB_SOCKET, "can't setsockopt IPV6_UNICAST_HOPS");
d62a17ae 150 return ret;
718e3744 151}
152
d62a17ae 153int setsockopt_ipv6_hoplimit(int sock, int val)
718e3744 154{
d62a17ae 155 int ret;
156
157#ifdef IPV6_RECVHOPLIMIT /*2292bis-01*/
158 ret = setsockopt(sock, IPPROTO_IPV6, IPV6_RECVHOPLIMIT, &val,
159 sizeof(val));
160 if (ret < 0)
450971aa 161 flog_err(EC_LIB_SOCKET, "can't setsockopt IPV6_RECVHOPLIMIT");
d62a17ae 162#else /*RFC2292*/
163 ret = setsockopt(sock, IPPROTO_IPV6, IPV6_HOPLIMIT, &val, sizeof(val));
164 if (ret < 0)
450971aa 165 flog_err(EC_LIB_SOCKET, "can't setsockopt IPV6_HOPLIMIT");
718e3744 166#endif
d62a17ae 167 return ret;
718e3744 168}
169
170/* Set multicast loop zero to the socket. */
d62a17ae 171int setsockopt_ipv6_multicast_loop(int sock, int val)
718e3744 172{
d62a17ae 173 int ret;
174
175 ret = setsockopt(sock, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, &val,
176 sizeof(val));
177 if (ret < 0)
1c50c1c0 178 flog_err(EC_LIB_SOCKET, "can't setsockopt IPV6_MULTICAST_LOOP");
d62a17ae 179 return ret;
718e3744 180}
181
d62a17ae 182static int getsockopt_ipv6_ifindex(struct msghdr *msgh)
4f7baa0e 183{
d62a17ae 184 struct in6_pktinfo *pktinfo;
185
186 pktinfo = getsockopt_cmsg_data(msgh, IPPROTO_IPV6, IPV6_PKTINFO);
187
188 return pktinfo->ipi6_ifindex;
4f7baa0e 189}
718e3744 190
d62a17ae 191int setsockopt_ipv6_tclass(int sock, int tclass)
6d0732c8 192{
d62a17ae 193 int ret = 0;
6d0732c8 194
ad61af67 195#ifdef IPV6_TCLASS /* RFC3542 */
d62a17ae 196 ret = setsockopt(sock, IPPROTO_IPV6, IPV6_TCLASS, &tclass,
197 sizeof(tclass));
198 if (ret < 0)
450971aa 199 flog_err(EC_LIB_SOCKET,
4496fbb3
DS
200 "Can't set IPV6_TCLASS option for fd %d to %#x: %s",
201 sock, tclass, safe_strerror(errno));
ad61af67 202#endif
d62a17ae 203 return ret;
6d0732c8 204}
718e3744 205
cc49eb5a 206/*
207 * Process multicast socket options for IPv4 in an OS-dependent manner.
69bf3a39 208 * Supported options are IP_{ADD,DROP}_MEMBERSHIP.
cc49eb5a 209 *
210 * Many operating systems have a limit on the number of groups that
211 * can be joined per socket (where each group and local address
212 * counts). This impacts OSPF, which joins groups on each interface
213 * using a single socket. The limit is typically 20, derived from the
214 * original BSD multicast implementation. Some systems have
215 * mechanisms for increasing this limit.
c188c37c 216 *
217 * In many 4.4BSD-derived systems, multicast group operations are not
218 * allowed on interfaces that are not UP. Thus, a previous attempt to
219 * leave the group may have failed, leaving it still joined, and we
220 * drop/join quietly to recover. This may not be necessary, but aims to
221 * defend against unknown behavior in that we will still return an error
222 * if the second join fails. It is not clear how other systems
223 * (e.g. Linux, Solaris) behave when leaving groups on down interfaces,
224 * but this behavior should not be harmful if they behave the same way,
225 * allow leaves, or implicitly leave all groups joined to down interfaces.
cc49eb5a 226 */
d62a17ae 227int setsockopt_ipv4_multicast(int sock, int optname, struct in_addr if_addr,
228 unsigned int mcast_addr, ifindex_t ifindex)
718e3744 229{
10d04cdb 230#ifdef HAVE_RFC3678
d62a17ae 231 struct group_req gr;
232 struct sockaddr_in *si;
233 int ret;
234 memset(&gr, 0, sizeof(gr));
235 si = (struct sockaddr_in *)&gr.gr_group;
236 gr.gr_interface = ifindex;
237 si->sin_family = AF_INET;
10d04cdb 238#ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
d62a17ae 239 si->sin_len = sizeof(struct sockaddr_in);
10d04cdb 240#endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
d62a17ae 241 si->sin_addr.s_addr = mcast_addr;
242 ret = setsockopt(sock, IPPROTO_IP,
243 (optname == IP_ADD_MEMBERSHIP) ? MCAST_JOIN_GROUP
244 : MCAST_LEAVE_GROUP,
245 (void *)&gr, sizeof(gr));
246 if ((ret < 0) && (optname == IP_ADD_MEMBERSHIP)
247 && (errno == EADDRINUSE)) {
248 setsockopt(sock, IPPROTO_IP, MCAST_LEAVE_GROUP, (void *)&gr,
249 sizeof(gr));
250 ret = setsockopt(sock, IPPROTO_IP, MCAST_JOIN_GROUP,
251 (void *)&gr, sizeof(gr));
252 }
253 return ret;
718e3744 254
10d04cdb 255#elif defined(HAVE_STRUCT_IP_MREQN_IMR_IFINDEX) && !defined(__FreeBSD__)
d62a17ae 256 struct ip_mreqn mreqn;
257 int ret;
258
259 assert(optname == IP_ADD_MEMBERSHIP || optname == IP_DROP_MEMBERSHIP);
260 memset(&mreqn, 0, sizeof(mreqn));
261
262 mreqn.imr_multiaddr.s_addr = mcast_addr;
263 mreqn.imr_ifindex = ifindex;
264
265 ret = setsockopt(sock, IPPROTO_IP, optname, (void *)&mreqn,
266 sizeof(mreqn));
267 if ((ret < 0) && (optname == IP_ADD_MEMBERSHIP)
268 && (errno == EADDRINUSE)) {
269 /* see above: handle possible problem when interface comes back
270 * up */
d62a17ae 271 zlog_info(
9edb6016
DS
272 "setsockopt_ipv4_multicast attempting to drop and re-add (fd %d, mcast %pI4, ifindex %u)",
273 sock, &mreqn.imr_multiaddr, ifindex);
d62a17ae 274 setsockopt(sock, IPPROTO_IP, IP_DROP_MEMBERSHIP, (void *)&mreqn,
275 sizeof(mreqn));
276 ret = setsockopt(sock, IPPROTO_IP, IP_ADD_MEMBERSHIP,
277 (void *)&mreqn, sizeof(mreqn));
278 }
279 return ret;
280
281/* Example defines for another OS, boilerplate off other code in this
282 function, AND handle optname as per other sections for consistency !! */
283/* #elif defined(BOGON_NIX) && EXAMPLE_VERSION_CODE > -100000 */
284/* Add your favourite OS here! */
285
286#elif defined(HAVE_BSD_STRUCT_IP_MREQ_HACK) /* #if OS_TYPE */
287 /* standard BSD API */
288
289 struct ip_mreq mreq;
290 int ret;
291
292 assert(optname == IP_ADD_MEMBERSHIP || optname == IP_DROP_MEMBERSHIP);
293
294
295 memset(&mreq, 0, sizeof(mreq));
296 mreq.imr_multiaddr.s_addr = mcast_addr;
4a3867d0 297#if !defined __OpenBSD__
d62a17ae 298 mreq.imr_interface.s_addr = htonl(ifindex);
4a3867d0 299#else
d62a17ae 300 mreq.imr_interface.s_addr = if_addr.s_addr;
4a3867d0
RW
301#endif
302
d62a17ae 303 ret = setsockopt(sock, IPPROTO_IP, optname, (void *)&mreq,
304 sizeof(mreq));
305 if ((ret < 0) && (optname == IP_ADD_MEMBERSHIP)
306 && (errno == EADDRINUSE)) {
307 /* see above: handle possible problem when interface comes back
308 * up */
d62a17ae 309 zlog_info(
0418cf2e
DS
310 "setsockopt_ipv4_multicast attempting to drop and re-add (fd %d, mcast %pI4, ifindex %u)",
311 sock, &mreq.imr_multiaddr, ifindex);
d62a17ae 312 setsockopt(sock, IPPROTO_IP, IP_DROP_MEMBERSHIP, (void *)&mreq,
313 sizeof(mreq));
314 ret = setsockopt(sock, IPPROTO_IP, IP_ADD_MEMBERSHIP,
315 (void *)&mreq, sizeof(mreq));
316 }
317 return ret;
ee7e75d3 318
69bf3a39 319#else
d62a17ae 320#error "Unsupported multicast API"
718e3744 321#endif /* #if OS_TYPE */
718e3744 322}
4f7baa0e 323
69bf3a39
DT
324/*
325 * Set IP_MULTICAST_IF socket option in an OS-dependent manner.
326 */
d62a17ae 327int setsockopt_ipv4_multicast_if(int sock, struct in_addr if_addr,
328 ifindex_t ifindex)
69bf3a39
DT
329{
330
331#ifdef HAVE_STRUCT_IP_MREQN_IMR_IFINDEX
d62a17ae 332 struct ip_mreqn mreqn;
333 memset(&mreqn, 0, sizeof(mreqn));
69bf3a39 334
d62a17ae 335 mreqn.imr_ifindex = ifindex;
336 return setsockopt(sock, IPPROTO_IP, IP_MULTICAST_IF, (void *)&mreqn,
337 sizeof(mreqn));
69bf3a39 338
d62a17ae 339/* Example defines for another OS, boilerplate off other code in this
340 function */
341/* #elif defined(BOGON_NIX) && EXAMPLE_VERSION_CODE > -100000 */
342/* Add your favourite OS here! */
69bf3a39 343#elif defined(HAVE_BSD_STRUCT_IP_MREQ_HACK)
d62a17ae 344 struct in_addr m;
69bf3a39 345
4a3867d0 346#if !defined __OpenBSD__
d62a17ae 347 m.s_addr = htonl(ifindex);
4a3867d0 348#else
d62a17ae 349 m.s_addr = if_addr.s_addr;
4a3867d0 350#endif
69bf3a39 351
d62a17ae 352 return setsockopt(sock, IPPROTO_IP, IP_MULTICAST_IF, (void *)&m,
353 sizeof(m));
69bf3a39 354#else
d62a17ae 355#error "Unsupported multicast API"
69bf3a39
DT
356#endif
357}
c5bdb09f 358
d7c0a89a 359int setsockopt_ipv4_multicast_loop(int sock, uint8_t val)
c5bdb09f 360{
d62a17ae 361 int ret;
c5bdb09f 362
d62a17ae 363 ret = setsockopt(sock, IPPROTO_IP, IP_MULTICAST_LOOP, (void *)&val,
364 sizeof(val));
365 if (ret < 0)
450971aa 366 flog_err(EC_LIB_SOCKET, "can't setsockopt IP_MULTICAST_LOOP");
c5bdb09f 367
d62a17ae 368 return ret;
c5bdb09f
RW
369}
370
d62a17ae 371static int setsockopt_ipv4_ifindex(int sock, ifindex_t val)
4f7baa0e 372{
d62a17ae 373 int ret;
374
375#if defined(IP_PKTINFO)
0e2d7076
DA
376 ret = setsockopt(sock, IPPROTO_IP, IP_PKTINFO, &val, sizeof(val));
377 if (ret < 0)
450971aa 378 flog_err(EC_LIB_SOCKET,
4496fbb3
DS
379 "Can't set IP_PKTINFO option for fd %d to %d: %s",
380 sock, val, safe_strerror(errno));
d62a17ae 381#elif defined(IP_RECVIF)
0e2d7076
DA
382 ret = setsockopt(sock, IPPROTO_IP, IP_RECVIF, &val, sizeof(val));
383 if (ret < 0)
450971aa 384 flog_err(EC_LIB_SOCKET,
4496fbb3
DS
385 "Can't set IP_RECVIF option for fd %d to %d: %s", sock,
386 val, safe_strerror(errno));
4f7baa0e 387#else
388#warning "Neither IP_PKTINFO nor IP_RECVIF is available."
389#warning "Will not be able to receive link info."
390#warning "Things might be seriously broken.."
d62a17ae 391 /* XXX Does this ever happen? Should there be a zlog_warn message here?
392 */
393 ret = -1;
4f7baa0e 394#endif
d62a17ae 395 return ret;
4f7baa0e 396}
397
d62a17ae 398int setsockopt_ipv4_tos(int sock, int tos)
1423c809 399{
d62a17ae 400 int ret;
1423c809 401
d62a17ae 402 ret = setsockopt(sock, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
403 if (ret < 0)
450971aa 404 flog_err(EC_LIB_SOCKET,
4496fbb3
DS
405 "Can't set IP_TOS option for fd %d to %#x: %s", sock,
406 tos, safe_strerror(errno));
d62a17ae 407 return ret;
1423c809
SH
408}
409
410
d62a17ae 411int setsockopt_ifindex(int af, int sock, ifindex_t val)
e6822768 412{
d62a17ae 413 int ret = -1;
414
415 switch (af) {
416 case AF_INET:
417 ret = setsockopt_ipv4_ifindex(sock, val);
418 break;
419 case AF_INET6:
420 ret = setsockopt_ipv6_pktinfo(sock, val);
421 break;
422 default:
450971aa 423 flog_err(EC_LIB_DEVELOPMENT,
4496fbb3 424 "setsockopt_ifindex: unknown address family %d", af);
d62a17ae 425 }
426 return ret;
e6822768 427}
d62a17ae 428
d44debed 429/*
430 * Requires: msgh is not NULL and points to a valid struct msghdr, which
431 * may or may not have control data about the incoming interface.
432 *
433 * Returns the interface index (small integer >= 1) if it can be
434 * determined, or else 0.
435 */
d62a17ae 436static ifindex_t getsockopt_ipv4_ifindex(struct msghdr *msgh)
4f7baa0e 437{
a2b6e694 438 ifindex_t ifindex;
1d69fdf6 439
e6822768 440#if defined(IP_PKTINFO)
d62a17ae 441 /* Linux pktinfo based ifindex retrieval */
442 struct in_pktinfo *pktinfo;
443
444 pktinfo = (struct in_pktinfo *)getsockopt_cmsg_data(msgh, IPPROTO_IP,
445 IP_PKTINFO);
a2b6e694 446
447 /* getsockopt_ifindex() will forward this, being 0 "not found" */
448 if (pktinfo == NULL)
449 return 0;
450
d62a17ae 451 ifindex = pktinfo->ipi_ifindex;
452
e6822768 453#elif defined(IP_RECVIF)
d44debed 454
d62a17ae 455/* retrieval based on IP_RECVIF */
d44debed 456
d62a17ae 457 /* BSD systems use a sockaddr_dl as the control message payload. */
458 struct sockaddr_dl *sdl;
1d69fdf6 459
d62a17ae 460 /* BSD */
461 sdl = (struct sockaddr_dl *)getsockopt_cmsg_data(msgh, IPPROTO_IP,
462 IP_RECVIF);
463 if (sdl != NULL)
464 ifindex = sdl->sdl_index;
465 else
466 ifindex = 0;
e6822768 467
d44debed 468#else
d62a17ae 469/*
470 * Neither IP_PKTINFO nor IP_RECVIF defined - warn at compile time.
471 * XXX Decide if this is a core service, or if daemons have to cope.
472 * Since Solaris 8 and OpenBSD seem not to provide it, it seems that
473 * daemons have to cope.
474 */
d44debed 475#warning "getsockopt_ipv4_ifindex: Neither IP_PKTINFO nor IP_RECVIF defined."
476#warning "Some daemons may fail to operate correctly!"
d62a17ae 477 ifindex = 0;
d44debed 478
d62a17ae 479#endif /* IP_PKTINFO */
d44debed 480
d62a17ae 481 return ifindex;
4f7baa0e 482}
483
484/* return ifindex, 0 if none found */
d62a17ae 485ifindex_t getsockopt_ifindex(int af, struct msghdr *msgh)
4f7baa0e 486{
d62a17ae 487 switch (af) {
488 case AF_INET:
489 return (getsockopt_ipv4_ifindex(msgh));
d62a17ae 490 case AF_INET6:
491 return (getsockopt_ipv6_ifindex(msgh));
d62a17ae 492 default:
450971aa 493 flog_err(EC_LIB_DEVELOPMENT,
4496fbb3 494 "getsockopt_ifindex: unknown address family %d", af);
d62a17ae 495 return 0;
496 }
4f7baa0e 497}
96e27c99 498
499/* swab iph between order system uses for IP_HDRINCL and host order */
d62a17ae 500void sockopt_iphdrincl_swab_htosys(struct ip *iph)
96e27c99 501{
d62a17ae 502/* BSD and derived take iph in network order, except for
503 * ip_len and ip_off
504 */
96e27c99 505#ifndef HAVE_IP_HDRINCL_BSD_ORDER
d62a17ae 506 iph->ip_len = htons(iph->ip_len);
507 iph->ip_off = htons(iph->ip_off);
96e27c99 508#endif /* HAVE_IP_HDRINCL_BSD_ORDER */
509
d62a17ae 510 iph->ip_id = htons(iph->ip_id);
96e27c99 511}
512
d62a17ae 513void sockopt_iphdrincl_swab_systoh(struct ip *iph)
96e27c99 514{
515#ifndef HAVE_IP_HDRINCL_BSD_ORDER
d62a17ae 516 iph->ip_len = ntohs(iph->ip_len);
517 iph->ip_off = ntohs(iph->ip_off);
96e27c99 518#endif /* HAVE_IP_HDRINCL_BSD_ORDER */
519
d62a17ae 520 iph->ip_id = ntohs(iph->ip_id);
96e27c99 521}
0df7c91f 522
d62a17ae 523int sockopt_tcp_rtt(int sock)
cf279b3a
TT
524{
525#ifdef TCP_INFO
d62a17ae 526 struct tcp_info ti;
527 socklen_t len = sizeof(ti);
cf279b3a 528
d62a17ae 529 if (getsockopt(sock, IPPROTO_TCP, TCP_INFO, &ti, &len) != 0)
530 return 0;
cf279b3a 531
d62a17ae 532 return ti.tcpi_rtt / 1000;
cf279b3a 533#else
d62a17ae 534 return 0;
cf279b3a
TT
535#endif
536}
537
b33e4666
QY
538int sockopt_tcp_signature_ext(int sock, union sockunion *su, uint16_t prefixlen,
539 const char *password)
0df7c91f 540{
b33e4666
QY
541#ifndef HAVE_DECL_TCP_MD5SIG
542 /*
543 * We have been asked to enable MD5 auth for an address, but our
544 * platform doesn't support that
545 */
546 return -2;
547#endif
548
549#ifndef TCP_MD5SIG_EXT
550 /*
551 * We have been asked to enable MD5 auth for a prefix, but our platform
552 * doesn't support that
553 */
554 if (prefixlen > 0)
555 return -2;
556#endif
557
3535a785 558#if HAVE_DECL_TCP_MD5SIG
d62a17ae 559 int ret;
b33e4666
QY
560
561 int optname = TCP_MD5SIG;
0df7c91f 562#ifndef GNU_LINUX
d62a17ae 563 /*
564 * XXX Need to do PF_KEY operation here to add/remove an SA entry,
565 * and add/remove an SP entry for this peer's packet flows also.
566 */
567 int md5sig = password && *password ? 1 : 0;
0df7c91f 568#else
d62a17ae 569 int keylen = password ? strlen(password) : 0;
570 struct tcp_md5sig md5sig;
571 union sockunion *su2, *susock;
572
573 /* Figure out whether the socket and the sockunion are the same family..
574 * adding AF_INET to AF_INET6 needs to be v4 mapped, you'd think..
575 */
576 if (!(susock = sockunion_getsockname(sock)))
577 return -1;
578
579 if (susock->sa.sa_family == su->sa.sa_family)
580 su2 = su;
581 else {
582 /* oops.. */
583 su2 = susock;
584
585 if (su2->sa.sa_family == AF_INET) {
586 sockunion_free(susock);
587 return 0;
588 }
589
590 /* If this does not work, then all users of this sockopt will
591 * need to
03bad95a 592 * differentiate between IPv4 and IPv6, and keep separate
d62a17ae 593 * sockets for
594 * each.
595 *
596 * Sadly, it doesn't seem to work at present. It's unknown
597 * whether
598 * this is a bug or not.
599 */
600 if (su2->sa.sa_family == AF_INET6
601 && su->sa.sa_family == AF_INET) {
602 su2->sin6.sin6_family = AF_INET6;
603 /* V4Map the address */
604 memset(&su2->sin6.sin6_addr, 0,
605 sizeof(struct in6_addr));
606 su2->sin6.sin6_addr.s6_addr32[2] = htonl(0xffff);
607 memcpy(&su2->sin6.sin6_addr.s6_addr32[3],
608 &su->sin.sin_addr, 4);
609 }
610 }
611
612 memset(&md5sig, 0, sizeof(md5sig));
613 memcpy(&md5sig.tcpm_addr, su2, sizeof(*su2));
b33e4666 614
d62a17ae 615 md5sig.tcpm_keylen = keylen;
616 if (keylen)
617 memcpy(md5sig.tcpm_key, password, keylen);
618 sockunion_free(susock);
b33e4666
QY
619
620 /*
621 * Handle support for MD5 signatures on prefixes, if available and
622 * requested. Technically the #ifdef check below is not needed because
623 * if prefixlen > 0 and we don't have support for this feature we would
624 * have already returned by now, but leaving it there to be explicit.
625 */
626#ifdef TCP_MD5SIG_EXT
627 if (prefixlen > 0) {
628 md5sig.tcpm_prefixlen = prefixlen;
629 md5sig.tcpm_flags = TCP_MD5SIG_FLAG_PREFIX;
630 optname = TCP_MD5SIG_EXT;
631 }
632#endif /* TCP_MD5SIG_EXT */
633
0df7c91f 634#endif /* GNU_LINUX */
b33e4666 635
0e2d7076
DA
636 ret = setsockopt(sock, IPPROTO_TCP, optname, &md5sig, sizeof(md5sig));
637 if (ret < 0) {
d62a17ae 638 if (ENOENT == errno)
639 ret = 0;
640 else
09c866e3 641 flog_err_sys(
450971aa 642 EC_LIB_SYSTEM_CALL,
09c866e3
QY
643 "sockopt_tcp_signature: setsockopt(%d): %s",
644 sock, safe_strerror(errno));
d62a17ae 645 }
646 return ret;
b33e4666 647#endif /* HAVE_TCP_MD5SIG */
adc109b5
DS
648
649 /*
650 * Making compiler happy. If we get to this point we probably
651 * have done something really really wrong.
652 */
653 return -2;
b33e4666
QY
654}
655
656int sockopt_tcp_signature(int sock, union sockunion *su, const char *password)
657{
658 return sockopt_tcp_signature_ext(sock, su, 0, password);
0df7c91f 659}
4ab46701
AR
660
661/* set TCP mss value to socket */
662int sockopt_tcp_mss_set(int sock, int tcp_maxseg)
663{
664 int ret = 0;
665 socklen_t tcp_maxseg_len = sizeof(tcp_maxseg);
666
667 ret = setsockopt(sock, IPPROTO_TCP, TCP_MAXSEG, &tcp_maxseg,
668 tcp_maxseg_len);
669 if (ret != 0) {
670 flog_err_sys(EC_LIB_SYSTEM_CALL,
671 "%s failed: setsockopt(%d): %s", __func__, sock,
672 safe_strerror(errno));
673 }
674
675 return ret;
676}
677
678/* get TCP mss value synced by socket */
679int sockopt_tcp_mss_get(int sock)
680{
681 int ret = 0;
682 int tcp_maxseg = 0;
683 socklen_t tcp_maxseg_len = sizeof(tcp_maxseg);
684
685 ret = getsockopt(sock, IPPROTO_TCP, TCP_MAXSEG, &tcp_maxseg,
686 &tcp_maxseg_len);
687 if (ret != 0) {
688 flog_err_sys(EC_LIB_SYSTEM_CALL,
689 "%s failed: getsockopt(%d): %s", __func__, sock,
690 safe_strerror(errno));
691 return 0;
692 }
693
694 return tcp_maxseg;
695}
5c480b5d
XL
696
697int setsockopt_tcp_keepalive(int sock, uint16_t keepalive_idle,
698 uint16_t keepalive_intvl,
699 uint16_t keepalive_probes)
700{
701 int val = 1;
702
703 if (setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, &val, sizeof(val)) < 0) {
704 flog_err_sys(EC_LIB_SYSTEM_CALL,
705 "%s failed: setsockopt SO_KEEPALIVE (%d): %s",
706 __func__, sock, safe_strerror(errno));
707 return -1;
708 }
709
710#if defined __OpenBSD__
711 return 0;
712#else
713 /* Send first probe after keepalive_idle seconds */
714 val = keepalive_idle;
715 if (setsockopt(sock, IPPROTO_TCP, TCP_KEEPIDLE, &val, sizeof(val)) <
716 0) {
717 flog_err_sys(EC_LIB_SYSTEM_CALL,
718 "%s failed: setsockopt TCP_KEEPIDLE (%d): %s",
719 __func__, sock, safe_strerror(errno));
720 return -1;
721 }
722
723 /* Set interval between two probes */
724 val = keepalive_intvl;
725 if (setsockopt(sock, IPPROTO_TCP, TCP_KEEPINTVL, &val, sizeof(val)) <
726 0) {
727 flog_err_sys(EC_LIB_SYSTEM_CALL,
728 "%s failed: setsockopt TCP_KEEPINTVL (%d): %s",
729 __func__, sock, safe_strerror(errno));
730 return -1;
731 }
732
733 /* Set maximum probes */
734 val = keepalive_probes;
735 if (setsockopt(sock, IPPROTO_TCP, TCP_KEEPCNT, &val, sizeof(val)) < 0) {
736 flog_err_sys(EC_LIB_SYSTEM_CALL,
737 "%s failed: setsockopt TCP_KEEPCNT (%d): %s",
738 __func__, sock, safe_strerror(errno));
739 return -1;
740 }
741
742 return 0;
743#endif
744}