]>
git.proxmox.com Git - mirror_frr.git/blob - lib/sockopt.c
1 /* setsockopt functions
2 * Copyright (C) 1999 Kunihiro Ishiguro
4 * This file is part of GNU Zebra.
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
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.
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
29 #include "sockunion.h"
30 #include "lib_errors.h"
32 void setsockopt_so_recvbuf(int sock
, int size
)
36 while (setsockopt(sock
, SOL_SOCKET
, SO_RCVBUF
, &size
, sizeof(size
))
41 flog_err(EC_LIB_SOCKET
,
42 "%s: fd %d: SO_RCVBUF set to %d (requested %d)",
43 __func__
, sock
, size
, orig_req
);
46 void setsockopt_so_sendbuf(const int sock
, int size
)
50 while (setsockopt(sock
, SOL_SOCKET
, SO_SNDBUF
, &size
, sizeof(size
))
55 flog_err(EC_LIB_SOCKET
,
56 "%s: fd %d: SO_SNDBUF set to %d (requested %d)",
57 __func__
, sock
, size
, orig_req
);
60 int getsockopt_so_sendbuf(const int sock
)
63 socklen_t optlen
= sizeof(optval
);
64 int ret
= getsockopt(sock
, SOL_SOCKET
, SO_SNDBUF
, (char *)&optval
,
67 flog_err_sys(EC_LIB_SYSTEM_CALL
,
68 "fd %d: can't getsockopt SO_SNDBUF: %d (%s)", sock
,
69 errno
, safe_strerror(errno
));
75 int getsockopt_so_recvbuf(const int sock
)
78 socklen_t optlen
= sizeof(optval
);
79 int ret
= getsockopt(sock
, SOL_SOCKET
, SO_RCVBUF
, (char *)&optval
,
82 flog_err_sys(EC_LIB_SYSTEM_CALL
,
83 "fd %d: can't getsockopt SO_RCVBUF: %d (%s)", sock
,
84 errno
, safe_strerror(errno
));
90 static void *getsockopt_cmsg_data(struct msghdr
*msgh
, int level
, int type
)
94 for (cmsg
= CMSG_FIRSTHDR(msgh
); cmsg
!= NULL
;
95 cmsg
= CMSG_NXTHDR(msgh
, cmsg
))
96 if (cmsg
->cmsg_level
== level
&& cmsg
->cmsg_type
== type
)
97 return CMSG_DATA(cmsg
);
102 /* Set IPv6 packet info to the socket. */
103 int setsockopt_ipv6_pktinfo(int sock
, int val
)
107 #ifdef IPV6_RECVPKTINFO /*2292bis-01*/
108 ret
= setsockopt(sock
, IPPROTO_IPV6
, IPV6_RECVPKTINFO
, &val
,
111 flog_err(EC_LIB_SOCKET
,
112 "can't setsockopt IPV6_RECVPKTINFO : %s",
113 safe_strerror(errno
));
115 ret
= setsockopt(sock
, IPPROTO_IPV6
, IPV6_PKTINFO
, &val
, sizeof(val
));
117 flog_err(EC_LIB_SOCKET
, "can't setsockopt IPV6_PKTINFO : %s",
118 safe_strerror(errno
));
119 #endif /* INIA_IPV6 */
123 /* Set multicast hops val to the socket. */
124 int setsockopt_ipv6_multicast_hops(int sock
, int val
)
128 ret
= setsockopt(sock
, IPPROTO_IPV6
, IPV6_MULTICAST_HOPS
, &val
,
131 flog_err(EC_LIB_SOCKET
, "can't setsockopt IPV6_MULTICAST_HOPS");
135 /* Set multicast hops val to the socket. */
136 int setsockopt_ipv6_unicast_hops(int sock
, int val
)
140 ret
= setsockopt(sock
, IPPROTO_IPV6
, IPV6_UNICAST_HOPS
, &val
,
143 flog_err(EC_LIB_SOCKET
, "can't setsockopt IPV6_UNICAST_HOPS");
147 int setsockopt_ipv6_hoplimit(int sock
, int val
)
151 #ifdef IPV6_RECVHOPLIMIT /*2292bis-01*/
152 ret
= setsockopt(sock
, IPPROTO_IPV6
, IPV6_RECVHOPLIMIT
, &val
,
155 flog_err(EC_LIB_SOCKET
, "can't setsockopt IPV6_RECVHOPLIMIT");
157 ret
= setsockopt(sock
, IPPROTO_IPV6
, IPV6_HOPLIMIT
, &val
, sizeof(val
));
159 flog_err(EC_LIB_SOCKET
, "can't setsockopt IPV6_HOPLIMIT");
164 /* Set multicast loop zero to the socket. */
165 int setsockopt_ipv6_multicast_loop(int sock
, int val
)
169 ret
= setsockopt(sock
, IPPROTO_IPV6
, IPV6_MULTICAST_LOOP
, &val
,
172 flog_err(EC_LIB_SOCKET
, "can't setsockopt IPV6_MULTICAST_LOOP");
176 static int getsockopt_ipv6_ifindex(struct msghdr
*msgh
)
178 struct in6_pktinfo
*pktinfo
;
180 pktinfo
= getsockopt_cmsg_data(msgh
, IPPROTO_IPV6
, IPV6_PKTINFO
);
182 return pktinfo
->ipi6_ifindex
;
185 int setsockopt_ipv6_tclass(int sock
, int tclass
)
189 #ifdef IPV6_TCLASS /* RFC3542 */
190 ret
= setsockopt(sock
, IPPROTO_IPV6
, IPV6_TCLASS
, &tclass
,
193 flog_err(EC_LIB_SOCKET
,
194 "Can't set IPV6_TCLASS option for fd %d to %#x: %s",
195 sock
, tclass
, safe_strerror(errno
));
201 * Process multicast socket options for IPv4 in an OS-dependent manner.
202 * Supported options are IP_{ADD,DROP}_MEMBERSHIP.
204 * Many operating systems have a limit on the number of groups that
205 * can be joined per socket (where each group and local address
206 * counts). This impacts OSPF, which joins groups on each interface
207 * using a single socket. The limit is typically 20, derived from the
208 * original BSD multicast implementation. Some systems have
209 * mechanisms for increasing this limit.
211 * In many 4.4BSD-derived systems, multicast group operations are not
212 * allowed on interfaces that are not UP. Thus, a previous attempt to
213 * leave the group may have failed, leaving it still joined, and we
214 * drop/join quietly to recover. This may not be necessary, but aims to
215 * defend against unknown behavior in that we will still return an error
216 * if the second join fails. It is not clear how other systems
217 * (e.g. Linux, Solaris) behave when leaving groups on down interfaces,
218 * but this behavior should not be harmful if they behave the same way,
219 * allow leaves, or implicitly leave all groups joined to down interfaces.
221 int setsockopt_ipv4_multicast(int sock
, int optname
, struct in_addr if_addr
,
222 unsigned int mcast_addr
, ifindex_t ifindex
)
226 struct sockaddr_in
*si
;
228 memset(&gr
, 0, sizeof(gr
));
229 si
= (struct sockaddr_in
*)&gr
.gr_group
;
230 gr
.gr_interface
= ifindex
;
231 si
->sin_family
= AF_INET
;
232 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
233 si
->sin_len
= sizeof(struct sockaddr_in
);
234 #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
235 si
->sin_addr
.s_addr
= mcast_addr
;
236 ret
= setsockopt(sock
, IPPROTO_IP
,
237 (optname
== IP_ADD_MEMBERSHIP
) ? MCAST_JOIN_GROUP
239 (void *)&gr
, sizeof(gr
));
240 if ((ret
< 0) && (optname
== IP_ADD_MEMBERSHIP
)
241 && (errno
== EADDRINUSE
)) {
242 setsockopt(sock
, IPPROTO_IP
, MCAST_LEAVE_GROUP
, (void *)&gr
,
244 ret
= setsockopt(sock
, IPPROTO_IP
, MCAST_JOIN_GROUP
,
245 (void *)&gr
, sizeof(gr
));
249 #elif defined(HAVE_STRUCT_IP_MREQN_IMR_IFINDEX) && !defined(__FreeBSD__)
250 struct ip_mreqn mreqn
;
253 assert(optname
== IP_ADD_MEMBERSHIP
|| optname
== IP_DROP_MEMBERSHIP
);
254 memset(&mreqn
, 0, sizeof(mreqn
));
256 mreqn
.imr_multiaddr
.s_addr
= mcast_addr
;
257 mreqn
.imr_ifindex
= ifindex
;
259 ret
= setsockopt(sock
, IPPROTO_IP
, optname
, (void *)&mreqn
,
261 if ((ret
< 0) && (optname
== IP_ADD_MEMBERSHIP
)
262 && (errno
== EADDRINUSE
)) {
263 /* see above: handle possible problem when interface comes back
265 char buf
[1][INET_ADDRSTRLEN
];
267 "setsockopt_ipv4_multicast attempting to drop and "
268 "re-add (fd %d, mcast %s, ifindex %u)",
269 sock
, inet_ntop(AF_INET
, &mreqn
.imr_multiaddr
, buf
[0],
272 setsockopt(sock
, IPPROTO_IP
, IP_DROP_MEMBERSHIP
, (void *)&mreqn
,
274 ret
= setsockopt(sock
, IPPROTO_IP
, IP_ADD_MEMBERSHIP
,
275 (void *)&mreqn
, sizeof(mreqn
));
279 /* Example defines for another OS, boilerplate off other code in this
280 function, AND handle optname as per other sections for consistency !! */
281 /* #elif defined(BOGON_NIX) && EXAMPLE_VERSION_CODE > -100000 */
282 /* Add your favourite OS here! */
284 #elif defined(HAVE_BSD_STRUCT_IP_MREQ_HACK) /* #if OS_TYPE */
285 /* standard BSD API */
290 assert(optname
== IP_ADD_MEMBERSHIP
|| optname
== IP_DROP_MEMBERSHIP
);
293 memset(&mreq
, 0, sizeof(mreq
));
294 mreq
.imr_multiaddr
.s_addr
= mcast_addr
;
295 #if !defined __OpenBSD__
296 mreq
.imr_interface
.s_addr
= htonl(ifindex
);
298 mreq
.imr_interface
.s_addr
= if_addr
.s_addr
;
301 ret
= setsockopt(sock
, IPPROTO_IP
, optname
, (void *)&mreq
,
303 if ((ret
< 0) && (optname
== IP_ADD_MEMBERSHIP
)
304 && (errno
== EADDRINUSE
)) {
305 /* see above: handle possible problem when interface comes back
307 char buf
[1][INET_ADDRSTRLEN
];
309 "setsockopt_ipv4_multicast attempting to drop and "
310 "re-add (fd %d, mcast %s, ifindex %u)",
311 sock
, inet_ntop(AF_INET
, &mreq
.imr_multiaddr
, buf
[0],
314 setsockopt(sock
, IPPROTO_IP
, IP_DROP_MEMBERSHIP
, (void *)&mreq
,
316 ret
= setsockopt(sock
, IPPROTO_IP
, IP_ADD_MEMBERSHIP
,
317 (void *)&mreq
, sizeof(mreq
));
322 #error "Unsupported multicast API"
323 #endif /* #if OS_TYPE */
327 * Set IP_MULTICAST_IF socket option in an OS-dependent manner.
329 int setsockopt_ipv4_multicast_if(int sock
, struct in_addr if_addr
,
333 #ifdef HAVE_STRUCT_IP_MREQN_IMR_IFINDEX
334 struct ip_mreqn mreqn
;
335 memset(&mreqn
, 0, sizeof(mreqn
));
337 mreqn
.imr_ifindex
= ifindex
;
338 return setsockopt(sock
, IPPROTO_IP
, IP_MULTICAST_IF
, (void *)&mreqn
,
341 /* Example defines for another OS, boilerplate off other code in this
343 /* #elif defined(BOGON_NIX) && EXAMPLE_VERSION_CODE > -100000 */
344 /* Add your favourite OS here! */
345 #elif defined(HAVE_BSD_STRUCT_IP_MREQ_HACK)
348 #if !defined __OpenBSD__
349 m
.s_addr
= htonl(ifindex
);
351 m
.s_addr
= if_addr
.s_addr
;
354 return setsockopt(sock
, IPPROTO_IP
, IP_MULTICAST_IF
, (void *)&m
,
356 #elif defined(SUNOS_5)
357 char ifname
[IF_NAMESIZE
];
358 struct ifaddrs
*ifa
, *ifap
;
359 struct in_addr ifaddr
;
361 if (if_indextoname(ifindex
, ifname
) == NULL
)
364 if (getifaddrs(&ifa
) != 0)
367 for (ifap
= ifa
; ifap
!= NULL
; ifap
= ifap
->ifa_next
) {
368 struct sockaddr_in
*sa
;
370 if (strcmp(ifap
->ifa_name
, ifname
) != 0)
372 if (ifap
->ifa_addr
->sa_family
!= AF_INET
)
374 sa
= (struct sockaddr_in
*)ifap
->ifa_addr
;
375 memcpy(&ifaddr
, &sa
->sin_addr
, sizeof(ifaddr
));
380 if (!ifap
) /* This means we did not find an IP */
383 return setsockopt(sock
, IPPROTO_IP
, IP_MULTICAST_IF
, (void *)&ifaddr
,
386 #error "Unsupported multicast API"
390 int setsockopt_ipv4_multicast_loop(int sock
, uint8_t val
)
394 ret
= setsockopt(sock
, IPPROTO_IP
, IP_MULTICAST_LOOP
, (void *)&val
,
397 flog_err(EC_LIB_SOCKET
, "can't setsockopt IP_MULTICAST_LOOP");
402 static int setsockopt_ipv4_ifindex(int sock
, ifindex_t val
)
406 #if defined(IP_PKTINFO)
407 if ((ret
= setsockopt(sock
, IPPROTO_IP
, IP_PKTINFO
, &val
, sizeof(val
)))
409 flog_err(EC_LIB_SOCKET
,
410 "Can't set IP_PKTINFO option for fd %d to %d: %s",
411 sock
, val
, safe_strerror(errno
));
412 #elif defined(IP_RECVIF)
413 if ((ret
= setsockopt(sock
, IPPROTO_IP
, IP_RECVIF
, &val
, sizeof(val
)))
415 flog_err(EC_LIB_SOCKET
,
416 "Can't set IP_RECVIF option for fd %d to %d: %s", sock
,
417 val
, safe_strerror(errno
));
419 #warning "Neither IP_PKTINFO nor IP_RECVIF is available."
420 #warning "Will not be able to receive link info."
421 #warning "Things might be seriously broken.."
422 /* XXX Does this ever happen? Should there be a zlog_warn message here?
429 int setsockopt_ipv4_tos(int sock
, int tos
)
433 ret
= setsockopt(sock
, IPPROTO_IP
, IP_TOS
, &tos
, sizeof(tos
));
435 flog_err(EC_LIB_SOCKET
,
436 "Can't set IP_TOS option for fd %d to %#x: %s", sock
,
437 tos
, safe_strerror(errno
));
442 int setsockopt_ifindex(int af
, int sock
, ifindex_t val
)
448 ret
= setsockopt_ipv4_ifindex(sock
, val
);
451 ret
= setsockopt_ipv6_pktinfo(sock
, val
);
454 flog_err(EC_LIB_DEVELOPMENT
,
455 "setsockopt_ifindex: unknown address family %d", af
);
461 * Requires: msgh is not NULL and points to a valid struct msghdr, which
462 * may or may not have control data about the incoming interface.
464 * Returns the interface index (small integer >= 1) if it can be
465 * determined, or else 0.
467 static ifindex_t
getsockopt_ipv4_ifindex(struct msghdr
*msgh
)
471 #if defined(IP_PKTINFO)
472 /* Linux pktinfo based ifindex retrieval */
473 struct in_pktinfo
*pktinfo
;
475 pktinfo
= (struct in_pktinfo
*)getsockopt_cmsg_data(msgh
, IPPROTO_IP
,
478 /* getsockopt_ifindex() will forward this, being 0 "not found" */
482 ifindex
= pktinfo
->ipi_ifindex
;
484 #elif defined(IP_RECVIF)
486 /* retrieval based on IP_RECVIF */
489 /* BSD systems use a sockaddr_dl as the control message payload. */
490 struct sockaddr_dl
*sdl
;
492 /* SUNOS_5 uses an integer with the index. */
493 ifindex_t
*ifindex_p
;
498 sdl
= (struct sockaddr_dl
*)getsockopt_cmsg_data(msgh
, IPPROTO_IP
,
501 ifindex
= sdl
->sdl_index
;
506 * Solaris. On Solaris 8, IP_RECVIF is defined, but the call to
507 * enable it fails with errno=99, and the struct msghdr has
510 ifindex_p
= (uint_t
*)getsockopt_cmsg_data(msgh
, IPPROTO_IP
, IP_RECVIF
);
511 if (ifindex_p
!= NULL
)
512 ifindex
= *ifindex_p
;
519 * Neither IP_PKTINFO nor IP_RECVIF defined - warn at compile time.
520 * XXX Decide if this is a core service, or if daemons have to cope.
521 * Since Solaris 8 and OpenBSD seem not to provide it, it seems that
522 * daemons have to cope.
524 #warning "getsockopt_ipv4_ifindex: Neither IP_PKTINFO nor IP_RECVIF defined."
525 #warning "Some daemons may fail to operate correctly!"
528 #endif /* IP_PKTINFO */
533 /* return ifindex, 0 if none found */
534 ifindex_t
getsockopt_ifindex(int af
, struct msghdr
*msgh
)
538 return (getsockopt_ipv4_ifindex(msgh
));
540 return (getsockopt_ipv6_ifindex(msgh
));
542 flog_err(EC_LIB_DEVELOPMENT
,
543 "getsockopt_ifindex: unknown address family %d", af
);
548 /* swab iph between order system uses for IP_HDRINCL and host order */
549 void sockopt_iphdrincl_swab_htosys(struct ip
*iph
)
551 /* BSD and derived take iph in network order, except for
554 #ifndef HAVE_IP_HDRINCL_BSD_ORDER
555 iph
->ip_len
= htons(iph
->ip_len
);
556 iph
->ip_off
= htons(iph
->ip_off
);
557 #endif /* HAVE_IP_HDRINCL_BSD_ORDER */
559 iph
->ip_id
= htons(iph
->ip_id
);
562 void sockopt_iphdrincl_swab_systoh(struct ip
*iph
)
564 #ifndef HAVE_IP_HDRINCL_BSD_ORDER
565 iph
->ip_len
= ntohs(iph
->ip_len
);
566 iph
->ip_off
= ntohs(iph
->ip_off
);
567 #endif /* HAVE_IP_HDRINCL_BSD_ORDER */
569 iph
->ip_id
= ntohs(iph
->ip_id
);
572 int sockopt_tcp_rtt(int sock
)
576 socklen_t len
= sizeof(ti
);
578 if (getsockopt(sock
, IPPROTO_TCP
, TCP_INFO
, &ti
, &len
) != 0)
581 return ti
.tcpi_rtt
/ 1000;
587 int sockopt_tcp_signature_ext(int sock
, union sockunion
*su
, uint16_t prefixlen
,
588 const char *password
)
590 #ifndef HAVE_DECL_TCP_MD5SIG
592 * We have been asked to enable MD5 auth for an address, but our
593 * platform doesn't support that
598 #ifndef TCP_MD5SIG_EXT
600 * We have been asked to enable MD5 auth for a prefix, but our platform
601 * doesn't support that
607 #if HAVE_DECL_TCP_MD5SIG
610 int optname
= TCP_MD5SIG
;
613 * XXX Need to do PF_KEY operation here to add/remove an SA entry,
614 * and add/remove an SP entry for this peer's packet flows also.
616 int md5sig
= password
&& *password
? 1 : 0;
618 int keylen
= password
? strlen(password
) : 0;
619 struct tcp_md5sig md5sig
;
620 union sockunion
*su2
, *susock
;
622 /* Figure out whether the socket and the sockunion are the same family..
623 * adding AF_INET to AF_INET6 needs to be v4 mapped, you'd think..
625 if (!(susock
= sockunion_getsockname(sock
)))
628 if (susock
->sa
.sa_family
== su
->sa
.sa_family
)
634 if (su2
->sa
.sa_family
== AF_INET
) {
635 sockunion_free(susock
);
639 /* If this does not work, then all users of this sockopt will
641 * differentiate between IPv4 and IPv6, and keep seperate
645 * Sadly, it doesn't seem to work at present. It's unknown
647 * this is a bug or not.
649 if (su2
->sa
.sa_family
== AF_INET6
650 && su
->sa
.sa_family
== AF_INET
) {
651 su2
->sin6
.sin6_family
= AF_INET6
;
652 /* V4Map the address */
653 memset(&su2
->sin6
.sin6_addr
, 0,
654 sizeof(struct in6_addr
));
655 su2
->sin6
.sin6_addr
.s6_addr32
[2] = htonl(0xffff);
656 memcpy(&su2
->sin6
.sin6_addr
.s6_addr32
[3],
657 &su
->sin
.sin_addr
, 4);
661 memset(&md5sig
, 0, sizeof(md5sig
));
662 memcpy(&md5sig
.tcpm_addr
, su2
, sizeof(*su2
));
664 md5sig
.tcpm_keylen
= keylen
;
666 memcpy(md5sig
.tcpm_key
, password
, keylen
);
667 sockunion_free(susock
);
670 * Handle support for MD5 signatures on prefixes, if available and
671 * requested. Technically the #ifdef check below is not needed because
672 * if prefixlen > 0 and we don't have support for this feature we would
673 * have already returned by now, but leaving it there to be explicit.
675 #ifdef TCP_MD5SIG_EXT
677 md5sig
.tcpm_prefixlen
= prefixlen
;
678 md5sig
.tcpm_flags
= TCP_MD5SIG_FLAG_PREFIX
;
679 optname
= TCP_MD5SIG_EXT
;
681 #endif /* TCP_MD5SIG_EXT */
683 #endif /* GNU_LINUX */
685 if ((ret
= setsockopt(sock
, IPPROTO_TCP
, optname
, &md5sig
,
688 /* ENOENT is harmless. It is returned when we clear a password
690 one was not previously set. */
696 "sockopt_tcp_signature: setsockopt(%d): %s",
697 sock
, safe_strerror(errno
));
700 #endif /* HAVE_TCP_MD5SIG */
703 * Making compiler happy. If we get to this point we probably
704 * have done something really really wrong.
709 int sockopt_tcp_signature(int sock
, union sockunion
*su
, const char *password
)
711 return sockopt_tcp_signature_ext(sock
, su
, 0, password
);