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