]>
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
,
133 "can't setsockopt IPV6_MULTICAST_HOPS");
137 /* Set multicast hops val to the socket. */
138 int setsockopt_ipv6_unicast_hops(int sock
, int val
)
142 ret
= setsockopt(sock
, IPPROTO_IPV6
, IPV6_UNICAST_HOPS
, &val
,
145 flog_err(EC_LIB_SOCKET
, "can't setsockopt IPV6_UNICAST_HOPS");
149 int setsockopt_ipv6_hoplimit(int sock
, int val
)
153 #ifdef IPV6_RECVHOPLIMIT /*2292bis-01*/
154 ret
= setsockopt(sock
, IPPROTO_IPV6
, IPV6_RECVHOPLIMIT
, &val
,
157 flog_err(EC_LIB_SOCKET
, "can't setsockopt IPV6_RECVHOPLIMIT");
159 ret
= setsockopt(sock
, IPPROTO_IPV6
, IPV6_HOPLIMIT
, &val
, sizeof(val
));
161 flog_err(EC_LIB_SOCKET
, "can't setsockopt IPV6_HOPLIMIT");
166 /* Set multicast loop zero to the socket. */
167 int setsockopt_ipv6_multicast_loop(int sock
, int val
)
171 ret
= setsockopt(sock
, IPPROTO_IPV6
, IPV6_MULTICAST_LOOP
, &val
,
174 flog_err(EC_LIB_SOCKET
,
175 "can't setsockopt IPV6_MULTICAST_LOOP");
179 static int getsockopt_ipv6_ifindex(struct msghdr
*msgh
)
181 struct in6_pktinfo
*pktinfo
;
183 pktinfo
= getsockopt_cmsg_data(msgh
, IPPROTO_IPV6
, IPV6_PKTINFO
);
185 return pktinfo
->ipi6_ifindex
;
188 int setsockopt_ipv6_tclass(int sock
, int tclass
)
192 #ifdef IPV6_TCLASS /* RFC3542 */
193 ret
= setsockopt(sock
, IPPROTO_IPV6
, IPV6_TCLASS
, &tclass
,
196 flog_err(EC_LIB_SOCKET
,
197 "Can't set IPV6_TCLASS option for fd %d to %#x: %s",
198 sock
, tclass
, safe_strerror(errno
));
204 * Process multicast socket options for IPv4 in an OS-dependent manner.
205 * Supported options are IP_{ADD,DROP}_MEMBERSHIP.
207 * Many operating systems have a limit on the number of groups that
208 * can be joined per socket (where each group and local address
209 * counts). This impacts OSPF, which joins groups on each interface
210 * using a single socket. The limit is typically 20, derived from the
211 * original BSD multicast implementation. Some systems have
212 * mechanisms for increasing this limit.
214 * In many 4.4BSD-derived systems, multicast group operations are not
215 * allowed on interfaces that are not UP. Thus, a previous attempt to
216 * leave the group may have failed, leaving it still joined, and we
217 * drop/join quietly to recover. This may not be necessary, but aims to
218 * defend against unknown behavior in that we will still return an error
219 * if the second join fails. It is not clear how other systems
220 * (e.g. Linux, Solaris) behave when leaving groups on down interfaces,
221 * but this behavior should not be harmful if they behave the same way,
222 * allow leaves, or implicitly leave all groups joined to down interfaces.
224 int setsockopt_ipv4_multicast(int sock
, int optname
, struct in_addr if_addr
,
225 unsigned int mcast_addr
, ifindex_t ifindex
)
229 struct sockaddr_in
*si
;
231 memset(&gr
, 0, sizeof(gr
));
232 si
= (struct sockaddr_in
*)&gr
.gr_group
;
233 gr
.gr_interface
= ifindex
;
234 si
->sin_family
= AF_INET
;
235 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
236 si
->sin_len
= sizeof(struct sockaddr_in
);
237 #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
238 si
->sin_addr
.s_addr
= mcast_addr
;
239 ret
= setsockopt(sock
, IPPROTO_IP
,
240 (optname
== IP_ADD_MEMBERSHIP
) ? MCAST_JOIN_GROUP
242 (void *)&gr
, sizeof(gr
));
243 if ((ret
< 0) && (optname
== IP_ADD_MEMBERSHIP
)
244 && (errno
== EADDRINUSE
)) {
245 setsockopt(sock
, IPPROTO_IP
, MCAST_LEAVE_GROUP
, (void *)&gr
,
247 ret
= setsockopt(sock
, IPPROTO_IP
, MCAST_JOIN_GROUP
,
248 (void *)&gr
, sizeof(gr
));
252 #elif defined(HAVE_STRUCT_IP_MREQN_IMR_IFINDEX) && !defined(__FreeBSD__)
253 struct ip_mreqn mreqn
;
256 assert(optname
== IP_ADD_MEMBERSHIP
|| optname
== IP_DROP_MEMBERSHIP
);
257 memset(&mreqn
, 0, sizeof(mreqn
));
259 mreqn
.imr_multiaddr
.s_addr
= mcast_addr
;
260 mreqn
.imr_ifindex
= ifindex
;
262 ret
= setsockopt(sock
, IPPROTO_IP
, optname
, (void *)&mreqn
,
264 if ((ret
< 0) && (optname
== IP_ADD_MEMBERSHIP
)
265 && (errno
== EADDRINUSE
)) {
266 /* see above: handle possible problem when interface comes back
268 char buf
[1][INET_ADDRSTRLEN
];
270 "setsockopt_ipv4_multicast attempting to drop and "
271 "re-add (fd %d, mcast %s, ifindex %u)",
272 sock
, inet_ntop(AF_INET
, &mreqn
.imr_multiaddr
, buf
[0],
275 setsockopt(sock
, IPPROTO_IP
, IP_DROP_MEMBERSHIP
, (void *)&mreqn
,
277 ret
= setsockopt(sock
, IPPROTO_IP
, IP_ADD_MEMBERSHIP
,
278 (void *)&mreqn
, sizeof(mreqn
));
282 /* Example defines for another OS, boilerplate off other code in this
283 function, AND handle optname as per other sections for consistency !! */
284 /* #elif defined(BOGON_NIX) && EXAMPLE_VERSION_CODE > -100000 */
285 /* Add your favourite OS here! */
287 #elif defined(HAVE_BSD_STRUCT_IP_MREQ_HACK) /* #if OS_TYPE */
288 /* standard BSD API */
293 assert(optname
== IP_ADD_MEMBERSHIP
|| optname
== IP_DROP_MEMBERSHIP
);
296 memset(&mreq
, 0, sizeof(mreq
));
297 mreq
.imr_multiaddr
.s_addr
= mcast_addr
;
298 #if !defined __OpenBSD__
299 mreq
.imr_interface
.s_addr
= htonl(ifindex
);
301 mreq
.imr_interface
.s_addr
= if_addr
.s_addr
;
304 ret
= setsockopt(sock
, IPPROTO_IP
, optname
, (void *)&mreq
,
306 if ((ret
< 0) && (optname
== IP_ADD_MEMBERSHIP
)
307 && (errno
== EADDRINUSE
)) {
308 /* see above: handle possible problem when interface comes back
310 char buf
[1][INET_ADDRSTRLEN
];
312 "setsockopt_ipv4_multicast attempting to drop and "
313 "re-add (fd %d, mcast %s, ifindex %u)",
314 sock
, inet_ntop(AF_INET
, &mreq
.imr_multiaddr
, buf
[0],
317 setsockopt(sock
, IPPROTO_IP
, IP_DROP_MEMBERSHIP
, (void *)&mreq
,
319 ret
= setsockopt(sock
, IPPROTO_IP
, IP_ADD_MEMBERSHIP
,
320 (void *)&mreq
, sizeof(mreq
));
325 #error "Unsupported multicast API"
326 #endif /* #if OS_TYPE */
330 * Set IP_MULTICAST_IF socket option in an OS-dependent manner.
332 int setsockopt_ipv4_multicast_if(int sock
, struct in_addr if_addr
,
336 #ifdef HAVE_STRUCT_IP_MREQN_IMR_IFINDEX
337 struct ip_mreqn mreqn
;
338 memset(&mreqn
, 0, sizeof(mreqn
));
340 mreqn
.imr_ifindex
= ifindex
;
341 return setsockopt(sock
, IPPROTO_IP
, IP_MULTICAST_IF
, (void *)&mreqn
,
344 /* Example defines for another OS, boilerplate off other code in this
346 /* #elif defined(BOGON_NIX) && EXAMPLE_VERSION_CODE > -100000 */
347 /* Add your favourite OS here! */
348 #elif defined(HAVE_BSD_STRUCT_IP_MREQ_HACK)
351 #if !defined __OpenBSD__
352 m
.s_addr
= htonl(ifindex
);
354 m
.s_addr
= if_addr
.s_addr
;
357 return setsockopt(sock
, IPPROTO_IP
, IP_MULTICAST_IF
, (void *)&m
,
359 #elif defined(SUNOS_5)
360 char ifname
[IF_NAMESIZE
];
361 struct ifaddrs
*ifa
, *ifap
;
362 struct in_addr ifaddr
;
364 if (if_indextoname(ifindex
, ifname
) == NULL
)
367 if (getifaddrs(&ifa
) != 0)
370 for (ifap
= ifa
; ifap
!= NULL
; ifap
= ifap
->ifa_next
) {
371 struct sockaddr_in
*sa
;
373 if (strcmp(ifap
->ifa_name
, ifname
) != 0)
375 if (ifap
->ifa_addr
->sa_family
!= AF_INET
)
377 sa
= (struct sockaddr_in
*)ifap
->ifa_addr
;
378 memcpy(&ifaddr
, &sa
->sin_addr
, sizeof(ifaddr
));
383 if (!ifap
) /* This means we did not find an IP */
386 return setsockopt(sock
, IPPROTO_IP
, IP_MULTICAST_IF
, (void *)&ifaddr
,
389 #error "Unsupported multicast API"
393 int setsockopt_ipv4_multicast_loop(int sock
, uint8_t val
)
397 ret
= setsockopt(sock
, IPPROTO_IP
, IP_MULTICAST_LOOP
, (void *)&val
,
400 flog_err(EC_LIB_SOCKET
, "can't setsockopt IP_MULTICAST_LOOP");
405 static int setsockopt_ipv4_ifindex(int sock
, ifindex_t val
)
409 #if defined(IP_PKTINFO)
410 if ((ret
= setsockopt(sock
, IPPROTO_IP
, IP_PKTINFO
, &val
, sizeof(val
)))
412 flog_err(EC_LIB_SOCKET
,
413 "Can't set IP_PKTINFO option for fd %d to %d: %s",
414 sock
, val
, safe_strerror(errno
));
415 #elif defined(IP_RECVIF)
416 if ((ret
= setsockopt(sock
, IPPROTO_IP
, IP_RECVIF
, &val
, sizeof(val
)))
418 flog_err(EC_LIB_SOCKET
,
419 "Can't set IP_RECVIF option for fd %d to %d: %s", sock
,
420 val
, safe_strerror(errno
));
422 #warning "Neither IP_PKTINFO nor IP_RECVIF is available."
423 #warning "Will not be able to receive link info."
424 #warning "Things might be seriously broken.."
425 /* XXX Does this ever happen? Should there be a zlog_warn message here?
432 int setsockopt_ipv4_tos(int sock
, int tos
)
436 ret
= setsockopt(sock
, IPPROTO_IP
, IP_TOS
, &tos
, sizeof(tos
));
438 flog_err(EC_LIB_SOCKET
,
439 "Can't set IP_TOS option for fd %d to %#x: %s", sock
,
440 tos
, safe_strerror(errno
));
445 int setsockopt_ifindex(int af
, int sock
, ifindex_t val
)
451 ret
= setsockopt_ipv4_ifindex(sock
, val
);
454 ret
= setsockopt_ipv6_pktinfo(sock
, val
);
457 flog_err(EC_LIB_DEVELOPMENT
,
458 "setsockopt_ifindex: unknown address family %d", af
);
464 * Requires: msgh is not NULL and points to a valid struct msghdr, which
465 * may or may not have control data about the incoming interface.
467 * Returns the interface index (small integer >= 1) if it can be
468 * determined, or else 0.
470 static ifindex_t
getsockopt_ipv4_ifindex(struct msghdr
*msgh
)
474 #if defined(IP_PKTINFO)
475 /* Linux pktinfo based ifindex retrieval */
476 struct in_pktinfo
*pktinfo
;
478 pktinfo
= (struct in_pktinfo
*)getsockopt_cmsg_data(msgh
, IPPROTO_IP
,
481 /* getsockopt_ifindex() will forward this, being 0 "not found" */
485 ifindex
= pktinfo
->ipi_ifindex
;
487 #elif defined(IP_RECVIF)
489 /* retrieval based on IP_RECVIF */
492 /* BSD systems use a sockaddr_dl as the control message payload. */
493 struct sockaddr_dl
*sdl
;
495 /* SUNOS_5 uses an integer with the index. */
496 ifindex_t
*ifindex_p
;
501 sdl
= (struct sockaddr_dl
*)getsockopt_cmsg_data(msgh
, IPPROTO_IP
,
504 ifindex
= sdl
->sdl_index
;
509 * Solaris. On Solaris 8, IP_RECVIF is defined, but the call to
510 * enable it fails with errno=99, and the struct msghdr has
513 ifindex_p
= (uint_t
*)getsockopt_cmsg_data(msgh
, IPPROTO_IP
, IP_RECVIF
);
514 if (ifindex_p
!= NULL
)
515 ifindex
= *ifindex_p
;
522 * Neither IP_PKTINFO nor IP_RECVIF defined - warn at compile time.
523 * XXX Decide if this is a core service, or if daemons have to cope.
524 * Since Solaris 8 and OpenBSD seem not to provide it, it seems that
525 * daemons have to cope.
527 #warning "getsockopt_ipv4_ifindex: Neither IP_PKTINFO nor IP_RECVIF defined."
528 #warning "Some daemons may fail to operate correctly!"
531 #endif /* IP_PKTINFO */
536 /* return ifindex, 0 if none found */
537 ifindex_t
getsockopt_ifindex(int af
, struct msghdr
*msgh
)
541 return (getsockopt_ipv4_ifindex(msgh
));
544 return (getsockopt_ipv6_ifindex(msgh
));
547 flog_err(EC_LIB_DEVELOPMENT
,
548 "getsockopt_ifindex: unknown address family %d", af
);
553 /* swab iph between order system uses for IP_HDRINCL and host order */
554 void sockopt_iphdrincl_swab_htosys(struct ip
*iph
)
556 /* BSD and derived take iph in network order, except for
559 #ifndef HAVE_IP_HDRINCL_BSD_ORDER
560 iph
->ip_len
= htons(iph
->ip_len
);
561 iph
->ip_off
= htons(iph
->ip_off
);
562 #endif /* HAVE_IP_HDRINCL_BSD_ORDER */
564 iph
->ip_id
= htons(iph
->ip_id
);
567 void sockopt_iphdrincl_swab_systoh(struct ip
*iph
)
569 #ifndef HAVE_IP_HDRINCL_BSD_ORDER
570 iph
->ip_len
= ntohs(iph
->ip_len
);
571 iph
->ip_off
= ntohs(iph
->ip_off
);
572 #endif /* HAVE_IP_HDRINCL_BSD_ORDER */
574 iph
->ip_id
= ntohs(iph
->ip_id
);
577 int sockopt_tcp_rtt(int sock
)
581 socklen_t len
= sizeof(ti
);
583 if (getsockopt(sock
, IPPROTO_TCP
, TCP_INFO
, &ti
, &len
) != 0)
586 return ti
.tcpi_rtt
/ 1000;
592 int sockopt_tcp_signature(int sock
, union sockunion
*su
, const char *password
)
594 #if HAVE_DECL_TCP_MD5SIG
598 * XXX Need to do PF_KEY operation here to add/remove an SA entry,
599 * and add/remove an SP entry for this peer's packet flows also.
601 int md5sig
= password
&& *password
? 1 : 0;
603 int keylen
= password
? strlen(password
) : 0;
604 struct tcp_md5sig md5sig
;
605 union sockunion
*su2
, *susock
;
607 /* Figure out whether the socket and the sockunion are the same family..
608 * adding AF_INET to AF_INET6 needs to be v4 mapped, you'd think..
610 if (!(susock
= sockunion_getsockname(sock
)))
613 if (susock
->sa
.sa_family
== su
->sa
.sa_family
)
619 if (su2
->sa
.sa_family
== AF_INET
) {
620 sockunion_free(susock
);
624 /* If this does not work, then all users of this sockopt will
626 * differentiate between IPv4 and IPv6, and keep seperate
630 * Sadly, it doesn't seem to work at present. It's unknown
632 * this is a bug or not.
634 if (su2
->sa
.sa_family
== AF_INET6
635 && su
->sa
.sa_family
== AF_INET
) {
636 su2
->sin6
.sin6_family
= AF_INET6
;
637 /* V4Map the address */
638 memset(&su2
->sin6
.sin6_addr
, 0,
639 sizeof(struct in6_addr
));
640 su2
->sin6
.sin6_addr
.s6_addr32
[2] = htonl(0xffff);
641 memcpy(&su2
->sin6
.sin6_addr
.s6_addr32
[3],
642 &su
->sin
.sin_addr
, 4);
646 memset(&md5sig
, 0, sizeof(md5sig
));
647 memcpy(&md5sig
.tcpm_addr
, su2
, sizeof(*su2
));
648 md5sig
.tcpm_keylen
= keylen
;
650 memcpy(md5sig
.tcpm_key
, password
, keylen
);
651 sockunion_free(susock
);
652 #endif /* GNU_LINUX */
653 if ((ret
= setsockopt(sock
, IPPROTO_TCP
, TCP_MD5SIG
, &md5sig
,
656 /* ENOENT is harmless. It is returned when we clear a password
658 one was not previously set. */
664 "sockopt_tcp_signature: setsockopt(%d): %s",
665 sock
, safe_strerror(errno
));
668 #else /* HAVE_TCP_MD5SIG */
670 #endif /* !HAVE_TCP_MD5SIG */