]>
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 static void *getsockopt_cmsg_data(struct msghdr
*msgh
, int level
, int type
)
80 for (cmsg
= CMSG_FIRSTHDR(msgh
); cmsg
!= NULL
;
81 cmsg
= CMSG_NXTHDR(msgh
, cmsg
))
82 if (cmsg
->cmsg_level
== level
&& cmsg
->cmsg_type
== type
)
83 return (ptr
= CMSG_DATA(cmsg
));
88 /* Set IPv6 packet info to the socket. */
89 int setsockopt_ipv6_pktinfo(int sock
, int val
)
93 #ifdef IPV6_RECVPKTINFO /*2292bis-01*/
94 ret
= setsockopt(sock
, IPPROTO_IPV6
, IPV6_RECVPKTINFO
, &val
,
97 flog_err(EC_LIB_SOCKET
,
98 "can't setsockopt IPV6_RECVPKTINFO : %s",
99 safe_strerror(errno
));
101 ret
= setsockopt(sock
, IPPROTO_IPV6
, IPV6_PKTINFO
, &val
, sizeof(val
));
103 flog_err(EC_LIB_SOCKET
, "can't setsockopt IPV6_PKTINFO : %s",
104 safe_strerror(errno
));
105 #endif /* INIA_IPV6 */
109 /* Set multicast hops val to the socket. */
110 int setsockopt_ipv6_checksum(int sock
, int val
)
115 ret
= setsockopt(sock
, IPPROTO_RAW
, IPV6_CHECKSUM
, &val
, sizeof(val
));
117 ret
= setsockopt(sock
, IPPROTO_IPV6
, IPV6_CHECKSUM
, &val
, sizeof(val
));
118 #endif /* GNU_LINUX */
120 flog_err(EC_LIB_SOCKET
, "can't setsockopt IPV6_CHECKSUM");
124 /* Set multicast hops val to the socket. */
125 int setsockopt_ipv6_multicast_hops(int sock
, int val
)
129 ret
= setsockopt(sock
, IPPROTO_IPV6
, IPV6_MULTICAST_HOPS
, &val
,
132 flog_err(EC_LIB_SOCKET
, "can't setsockopt IPV6_MULTICAST_HOPS");
136 /* Set multicast hops val to the socket. */
137 int setsockopt_ipv6_unicast_hops(int sock
, int val
)
141 ret
= setsockopt(sock
, IPPROTO_IPV6
, IPV6_UNICAST_HOPS
, &val
,
144 flog_err(EC_LIB_SOCKET
, "can't setsockopt IPV6_UNICAST_HOPS");
148 int setsockopt_ipv6_hoplimit(int sock
, int val
)
152 #ifdef IPV6_RECVHOPLIMIT /*2292bis-01*/
153 ret
= setsockopt(sock
, IPPROTO_IPV6
, IPV6_RECVHOPLIMIT
, &val
,
156 flog_err(EC_LIB_SOCKET
, "can't setsockopt IPV6_RECVHOPLIMIT");
158 ret
= setsockopt(sock
, IPPROTO_IPV6
, IPV6_HOPLIMIT
, &val
, sizeof(val
));
160 flog_err(EC_LIB_SOCKET
, "can't setsockopt IPV6_HOPLIMIT");
165 /* Set multicast loop zero to the socket. */
166 int setsockopt_ipv6_multicast_loop(int sock
, int val
)
170 ret
= setsockopt(sock
, IPPROTO_IPV6
, IPV6_MULTICAST_LOOP
, &val
,
173 flog_err(EC_LIB_SOCKET
, "can't setsockopt IPV6_MULTICAST_LOOP");
177 static int getsockopt_ipv6_ifindex(struct msghdr
*msgh
)
179 struct in6_pktinfo
*pktinfo
;
181 pktinfo
= getsockopt_cmsg_data(msgh
, IPPROTO_IPV6
, IPV6_PKTINFO
);
183 return pktinfo
->ipi6_ifindex
;
186 int setsockopt_ipv6_tclass(int sock
, int tclass
)
190 #ifdef IPV6_TCLASS /* RFC3542 */
191 ret
= setsockopt(sock
, IPPROTO_IPV6
, IPV6_TCLASS
, &tclass
,
194 flog_err(EC_LIB_SOCKET
,
195 "Can't set IPV6_TCLASS option for fd %d to %#x: %s",
196 sock
, tclass
, safe_strerror(errno
));
202 * Process multicast socket options for IPv4 in an OS-dependent manner.
203 * Supported options are IP_{ADD,DROP}_MEMBERSHIP.
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.
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.
222 int setsockopt_ipv4_multicast(int sock
, int optname
, struct in_addr if_addr
,
223 unsigned int mcast_addr
, ifindex_t ifindex
)
227 struct sockaddr_in
*si
;
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
;
233 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
234 si
->sin_len
= sizeof(struct sockaddr_in
);
235 #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
236 si
->sin_addr
.s_addr
= mcast_addr
;
237 ret
= setsockopt(sock
, IPPROTO_IP
,
238 (optname
== IP_ADD_MEMBERSHIP
) ? MCAST_JOIN_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
,
245 ret
= setsockopt(sock
, IPPROTO_IP
, MCAST_JOIN_GROUP
,
246 (void *)&gr
, sizeof(gr
));
250 #elif defined(HAVE_STRUCT_IP_MREQN_IMR_IFINDEX) && !defined(__FreeBSD__)
251 struct ip_mreqn mreqn
;
254 assert(optname
== IP_ADD_MEMBERSHIP
|| optname
== IP_DROP_MEMBERSHIP
);
255 memset(&mreqn
, 0, sizeof(mreqn
));
257 mreqn
.imr_multiaddr
.s_addr
= mcast_addr
;
258 mreqn
.imr_ifindex
= ifindex
;
260 ret
= setsockopt(sock
, IPPROTO_IP
, optname
, (void *)&mreqn
,
262 if ((ret
< 0) && (optname
== IP_ADD_MEMBERSHIP
)
263 && (errno
== EADDRINUSE
)) {
264 /* see above: handle possible problem when interface comes back
266 char buf
[1][INET_ADDRSTRLEN
];
268 "setsockopt_ipv4_multicast attempting to drop and "
269 "re-add (fd %d, mcast %s, ifindex %u)",
270 sock
, inet_ntop(AF_INET
, &mreqn
.imr_multiaddr
, buf
[0],
273 setsockopt(sock
, IPPROTO_IP
, IP_DROP_MEMBERSHIP
, (void *)&mreqn
,
275 ret
= setsockopt(sock
, IPPROTO_IP
, IP_ADD_MEMBERSHIP
,
276 (void *)&mreqn
, sizeof(mreqn
));
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! */
285 #elif defined(HAVE_BSD_STRUCT_IP_MREQ_HACK) /* #if OS_TYPE */
286 /* standard BSD API */
291 assert(optname
== IP_ADD_MEMBERSHIP
|| optname
== IP_DROP_MEMBERSHIP
);
294 memset(&mreq
, 0, sizeof(mreq
));
295 mreq
.imr_multiaddr
.s_addr
= mcast_addr
;
296 #if !defined __OpenBSD__
297 mreq
.imr_interface
.s_addr
= htonl(ifindex
);
299 mreq
.imr_interface
.s_addr
= if_addr
.s_addr
;
302 ret
= setsockopt(sock
, IPPROTO_IP
, optname
, (void *)&mreq
,
304 if ((ret
< 0) && (optname
== IP_ADD_MEMBERSHIP
)
305 && (errno
== EADDRINUSE
)) {
306 /* see above: handle possible problem when interface comes back
308 char buf
[1][INET_ADDRSTRLEN
];
310 "setsockopt_ipv4_multicast attempting to drop and "
311 "re-add (fd %d, mcast %s, ifindex %u)",
312 sock
, inet_ntop(AF_INET
, &mreq
.imr_multiaddr
, buf
[0],
315 setsockopt(sock
, IPPROTO_IP
, IP_DROP_MEMBERSHIP
, (void *)&mreq
,
317 ret
= setsockopt(sock
, IPPROTO_IP
, IP_ADD_MEMBERSHIP
,
318 (void *)&mreq
, sizeof(mreq
));
323 #error "Unsupported multicast API"
324 #endif /* #if OS_TYPE */
328 * Set IP_MULTICAST_IF socket option in an OS-dependent manner.
330 int setsockopt_ipv4_multicast_if(int sock
, struct in_addr if_addr
,
334 #ifdef HAVE_STRUCT_IP_MREQN_IMR_IFINDEX
335 struct ip_mreqn mreqn
;
336 memset(&mreqn
, 0, sizeof(mreqn
));
338 mreqn
.imr_ifindex
= ifindex
;
339 return setsockopt(sock
, IPPROTO_IP
, IP_MULTICAST_IF
, (void *)&mreqn
,
342 /* Example defines for another OS, boilerplate off other code in this
344 /* #elif defined(BOGON_NIX) && EXAMPLE_VERSION_CODE > -100000 */
345 /* Add your favourite OS here! */
346 #elif defined(HAVE_BSD_STRUCT_IP_MREQ_HACK)
349 #if !defined __OpenBSD__
350 m
.s_addr
= htonl(ifindex
);
352 m
.s_addr
= if_addr
.s_addr
;
355 return setsockopt(sock
, IPPROTO_IP
, IP_MULTICAST_IF
, (void *)&m
,
357 #elif defined(SUNOS_5)
358 char ifname
[IF_NAMESIZE
];
359 struct ifaddrs
*ifa
, *ifap
;
360 struct in_addr ifaddr
;
362 if (if_indextoname(ifindex
, ifname
) == NULL
)
365 if (getifaddrs(&ifa
) != 0)
368 for (ifap
= ifa
; ifap
!= NULL
; ifap
= ifap
->ifa_next
) {
369 struct sockaddr_in
*sa
;
371 if (strcmp(ifap
->ifa_name
, ifname
) != 0)
373 if (ifap
->ifa_addr
->sa_family
!= AF_INET
)
375 sa
= (struct sockaddr_in
*)ifap
->ifa_addr
;
376 memcpy(&ifaddr
, &sa
->sin_addr
, sizeof(ifaddr
));
381 if (!ifap
) /* This means we did not find an IP */
384 return setsockopt(sock
, IPPROTO_IP
, IP_MULTICAST_IF
, (void *)&ifaddr
,
387 #error "Unsupported multicast API"
391 int setsockopt_ipv4_multicast_loop(int sock
, uint8_t val
)
395 ret
= setsockopt(sock
, IPPROTO_IP
, IP_MULTICAST_LOOP
, (void *)&val
,
398 flog_err(EC_LIB_SOCKET
, "can't setsockopt IP_MULTICAST_LOOP");
403 static int setsockopt_ipv4_ifindex(int sock
, ifindex_t val
)
407 #if defined(IP_PKTINFO)
408 if ((ret
= setsockopt(sock
, IPPROTO_IP
, IP_PKTINFO
, &val
, sizeof(val
)))
410 flog_err(EC_LIB_SOCKET
,
411 "Can't set IP_PKTINFO option for fd %d to %d: %s",
412 sock
, val
, safe_strerror(errno
));
413 #elif defined(IP_RECVIF)
414 if ((ret
= setsockopt(sock
, IPPROTO_IP
, IP_RECVIF
, &val
, sizeof(val
)))
416 flog_err(EC_LIB_SOCKET
,
417 "Can't set IP_RECVIF option for fd %d to %d: %s", sock
,
418 val
, safe_strerror(errno
));
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.."
423 /* XXX Does this ever happen? Should there be a zlog_warn message here?
430 int setsockopt_ipv4_tos(int sock
, int tos
)
434 ret
= setsockopt(sock
, IPPROTO_IP
, IP_TOS
, &tos
, sizeof(tos
));
436 flog_err(EC_LIB_SOCKET
,
437 "Can't set IP_TOS option for fd %d to %#x: %s", sock
,
438 tos
, safe_strerror(errno
));
443 int setsockopt_ifindex(int af
, int sock
, ifindex_t val
)
449 ret
= setsockopt_ipv4_ifindex(sock
, val
);
452 ret
= setsockopt_ipv6_pktinfo(sock
, val
);
455 flog_err(EC_LIB_DEVELOPMENT
,
456 "setsockopt_ifindex: unknown address family %d", af
);
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.
465 * Returns the interface index (small integer >= 1) if it can be
466 * determined, or else 0.
468 static ifindex_t
getsockopt_ipv4_ifindex(struct msghdr
*msgh
)
472 #if defined(IP_PKTINFO)
473 /* Linux pktinfo based ifindex retrieval */
474 struct in_pktinfo
*pktinfo
;
476 pktinfo
= (struct in_pktinfo
*)getsockopt_cmsg_data(msgh
, IPPROTO_IP
,
479 /* getsockopt_ifindex() will forward this, being 0 "not found" */
483 ifindex
= pktinfo
->ipi_ifindex
;
485 #elif defined(IP_RECVIF)
487 /* retrieval based on IP_RECVIF */
490 /* BSD systems use a sockaddr_dl as the control message payload. */
491 struct sockaddr_dl
*sdl
;
493 /* SUNOS_5 uses an integer with the index. */
494 ifindex_t
*ifindex_p
;
499 sdl
= (struct sockaddr_dl
*)getsockopt_cmsg_data(msgh
, IPPROTO_IP
,
502 ifindex
= sdl
->sdl_index
;
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
511 ifindex_p
= (uint_t
*)getsockopt_cmsg_data(msgh
, IPPROTO_IP
, IP_RECVIF
);
512 if (ifindex_p
!= NULL
)
513 ifindex
= *ifindex_p
;
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.
525 #warning "getsockopt_ipv4_ifindex: Neither IP_PKTINFO nor IP_RECVIF defined."
526 #warning "Some daemons may fail to operate correctly!"
529 #endif /* IP_PKTINFO */
534 /* return ifindex, 0 if none found */
535 ifindex_t
getsockopt_ifindex(int af
, struct msghdr
*msgh
)
539 return (getsockopt_ipv4_ifindex(msgh
));
542 return (getsockopt_ipv6_ifindex(msgh
));
545 flog_err(EC_LIB_DEVELOPMENT
,
546 "getsockopt_ifindex: unknown address family %d", af
);
551 /* swab iph between order system uses for IP_HDRINCL and host order */
552 void sockopt_iphdrincl_swab_htosys(struct ip
*iph
)
554 /* BSD and derived take iph in network order, except for
557 #ifndef HAVE_IP_HDRINCL_BSD_ORDER
558 iph
->ip_len
= htons(iph
->ip_len
);
559 iph
->ip_off
= htons(iph
->ip_off
);
560 #endif /* HAVE_IP_HDRINCL_BSD_ORDER */
562 iph
->ip_id
= htons(iph
->ip_id
);
565 void sockopt_iphdrincl_swab_systoh(struct ip
*iph
)
567 #ifndef HAVE_IP_HDRINCL_BSD_ORDER
568 iph
->ip_len
= ntohs(iph
->ip_len
);
569 iph
->ip_off
= ntohs(iph
->ip_off
);
570 #endif /* HAVE_IP_HDRINCL_BSD_ORDER */
572 iph
->ip_id
= ntohs(iph
->ip_id
);
575 int sockopt_tcp_rtt(int sock
)
579 socklen_t len
= sizeof(ti
);
581 if (getsockopt(sock
, IPPROTO_TCP
, TCP_INFO
, &ti
, &len
) != 0)
584 return ti
.tcpi_rtt
/ 1000;
590 int sockopt_tcp_signature_ext(int sock
, union sockunion
*su
, uint16_t prefixlen
,
591 const char *password
)
593 #ifndef HAVE_DECL_TCP_MD5SIG
595 * We have been asked to enable MD5 auth for an address, but our
596 * platform doesn't support that
601 #ifndef TCP_MD5SIG_EXT
603 * We have been asked to enable MD5 auth for a prefix, but our platform
604 * doesn't support that
610 #if HAVE_DECL_TCP_MD5SIG
613 int optname
= TCP_MD5SIG
;
616 * XXX Need to do PF_KEY operation here to add/remove an SA entry,
617 * and add/remove an SP entry for this peer's packet flows also.
619 int md5sig
= password
&& *password
? 1 : 0;
621 int keylen
= password
? strlen(password
) : 0;
622 struct tcp_md5sig md5sig
;
623 union sockunion
*su2
, *susock
;
625 /* Figure out whether the socket and the sockunion are the same family..
626 * adding AF_INET to AF_INET6 needs to be v4 mapped, you'd think..
628 if (!(susock
= sockunion_getsockname(sock
)))
631 if (susock
->sa
.sa_family
== su
->sa
.sa_family
)
637 if (su2
->sa
.sa_family
== AF_INET
) {
638 sockunion_free(susock
);
642 /* If this does not work, then all users of this sockopt will
644 * differentiate between IPv4 and IPv6, and keep seperate
648 * Sadly, it doesn't seem to work at present. It's unknown
650 * this is a bug or not.
652 if (su2
->sa
.sa_family
== AF_INET6
653 && su
->sa
.sa_family
== AF_INET
) {
654 su2
->sin6
.sin6_family
= AF_INET6
;
655 /* V4Map the address */
656 memset(&su2
->sin6
.sin6_addr
, 0,
657 sizeof(struct in6_addr
));
658 su2
->sin6
.sin6_addr
.s6_addr32
[2] = htonl(0xffff);
659 memcpy(&su2
->sin6
.sin6_addr
.s6_addr32
[3],
660 &su
->sin
.sin_addr
, 4);
664 memset(&md5sig
, 0, sizeof(md5sig
));
665 memcpy(&md5sig
.tcpm_addr
, su2
, sizeof(*su2
));
667 md5sig
.tcpm_keylen
= keylen
;
669 memcpy(md5sig
.tcpm_key
, password
, keylen
);
670 sockunion_free(susock
);
673 * Handle support for MD5 signatures on prefixes, if available and
674 * requested. Technically the #ifdef check below is not needed because
675 * if prefixlen > 0 and we don't have support for this feature we would
676 * have already returned by now, but leaving it there to be explicit.
678 #ifdef TCP_MD5SIG_EXT
680 md5sig
.tcpm_prefixlen
= prefixlen
;
681 md5sig
.tcpm_flags
= TCP_MD5SIG_FLAG_PREFIX
;
682 optname
= TCP_MD5SIG_EXT
;
684 #endif /* TCP_MD5SIG_EXT */
686 #endif /* GNU_LINUX */
688 if ((ret
= setsockopt(sock
, IPPROTO_TCP
, optname
, &md5sig
,
691 /* ENOENT is harmless. It is returned when we clear a password
693 one was not previously set. */
699 "sockopt_tcp_signature: setsockopt(%d): %s",
700 sock
, safe_strerror(errno
));
703 #endif /* HAVE_TCP_MD5SIG */
706 * Making compiler happy. If we get to this point we probably
707 * have done something really really wrong.
712 int sockopt_tcp_signature(int sock
, union sockunion
*su
, const char *password
)
714 return sockopt_tcp_signature_ext(sock
, su
, 0, password
);