]>
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"
32 setsockopt_so_recvbuf (int sock
, int size
)
36 while (setsockopt(sock
, SOL_SOCKET
, SO_RCVBUF
, &size
, sizeof (size
)) == -1)
40 zlog_warn ("%s: fd %d: SO_RCVBUF set to %d (requested %d)", __func__
, sock
,
45 setsockopt_so_sendbuf (const int sock
, int size
)
49 while (setsockopt(sock
, SOL_SOCKET
, SO_SNDBUF
, &size
, sizeof (size
)) == -1)
53 zlog_warn ("%s: fd %d: SO_SNDBUF set to %d (requested %d)", __func__
, sock
,
58 getsockopt_so_sendbuf (const int sock
)
61 socklen_t optlen
= sizeof (optval
);
62 int ret
= getsockopt (sock
, SOL_SOCKET
, SO_SNDBUF
,
63 (char *)&optval
, &optlen
);
66 zlog_err ("fd %d: can't getsockopt SO_SNDBUF: %d (%s)",
67 sock
, errno
, safe_strerror (errno
));
74 getsockopt_cmsg_data (struct msghdr
*msgh
, int level
, int type
)
79 for (cmsg
= ZCMSG_FIRSTHDR(msgh
);
81 cmsg
= CMSG_NXTHDR(msgh
, cmsg
))
82 if (cmsg
->cmsg_level
== level
&& cmsg
->cmsg_type
)
83 return (ptr
= CMSG_DATA(cmsg
));
88 /* Set IPv6 packet info to the socket. */
90 setsockopt_ipv6_pktinfo (int sock
, int val
)
94 #ifdef IPV6_RECVPKTINFO /*2292bis-01*/
95 ret
= setsockopt(sock
, IPPROTO_IPV6
, IPV6_RECVPKTINFO
, &val
, sizeof(val
));
97 zlog_warn ("can't setsockopt IPV6_RECVPKTINFO : %s", safe_strerror (errno
));
99 ret
= setsockopt(sock
, IPPROTO_IPV6
, IPV6_PKTINFO
, &val
, sizeof(val
));
101 zlog_warn ("can't setsockopt IPV6_PKTINFO : %s", safe_strerror (errno
));
102 #endif /* INIA_IPV6 */
106 /* Set multicast hops val to the socket. */
108 setsockopt_ipv6_checksum (int sock
, int val
)
113 ret
= setsockopt(sock
, IPPROTO_RAW
, IPV6_CHECKSUM
, &val
, sizeof(val
));
115 ret
= setsockopt(sock
, IPPROTO_IPV6
, IPV6_CHECKSUM
, &val
, sizeof(val
));
116 #endif /* GNU_LINUX */
118 zlog_warn ("can't setsockopt IPV6_CHECKSUM");
122 /* Set multicast hops val to the socket. */
124 setsockopt_ipv6_multicast_hops (int sock
, int val
)
128 ret
= setsockopt(sock
, IPPROTO_IPV6
, IPV6_MULTICAST_HOPS
, &val
, sizeof(val
));
130 zlog_warn ("can't setsockopt IPV6_MULTICAST_HOPS");
134 /* Set multicast hops val to the socket. */
136 setsockopt_ipv6_unicast_hops (int sock
, int val
)
140 ret
= setsockopt(sock
, IPPROTO_IPV6
, IPV6_UNICAST_HOPS
, &val
, sizeof(val
));
142 zlog_warn ("can't setsockopt IPV6_UNICAST_HOPS");
147 setsockopt_ipv6_hoplimit (int sock
, int val
)
151 #ifdef IPV6_RECVHOPLIMIT /*2292bis-01*/
152 ret
= setsockopt (sock
, IPPROTO_IPV6
, IPV6_RECVHOPLIMIT
, &val
, sizeof(val
));
154 zlog_warn ("can't setsockopt IPV6_RECVHOPLIMIT");
156 ret
= setsockopt (sock
, IPPROTO_IPV6
, IPV6_HOPLIMIT
, &val
, sizeof(val
));
158 zlog_warn ("can't setsockopt IPV6_HOPLIMIT");
163 /* Set multicast loop zero to the socket. */
165 setsockopt_ipv6_multicast_loop (int sock
, int val
)
169 ret
= setsockopt (sock
, IPPROTO_IPV6
, IPV6_MULTICAST_LOOP
, &val
,
172 zlog_warn ("can't setsockopt IPV6_MULTICAST_LOOP");
177 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
;
187 setsockopt_ipv6_tclass(int sock
, int tclass
)
191 #ifdef IPV6_TCLASS /* RFC3542 */
192 ret
= setsockopt (sock
, IPPROTO_IPV6
, IPV6_TCLASS
, &tclass
, sizeof (tclass
));
194 zlog_warn ("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.
222 setsockopt_ipv4_multicast(int sock
,
224 struct in_addr if_addr
,
225 unsigned int mcast_addr
,
230 struct sockaddr_in
*si
;
232 memset (&gr
, 0, sizeof(gr
));
233 si
= (struct sockaddr_in
*)&gr
.gr_group
;
234 gr
.gr_interface
= ifindex
;
235 si
->sin_family
= AF_INET
;
236 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
237 si
->sin_len
= sizeof(struct sockaddr_in
);
238 #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
239 si
->sin_addr
.s_addr
= mcast_addr
;
240 ret
= setsockopt(sock
, IPPROTO_IP
, (optname
== IP_ADD_MEMBERSHIP
) ?
241 MCAST_JOIN_GROUP
: MCAST_LEAVE_GROUP
, (void *)&gr
, sizeof(gr
));
242 if ((ret
< 0) && (optname
== IP_ADD_MEMBERSHIP
) && (errno
== EADDRINUSE
))
244 setsockopt(sock
, IPPROTO_IP
, MCAST_LEAVE_GROUP
, (void *)&gr
, sizeof(gr
));
245 ret
= setsockopt(sock
, IPPROTO_IP
, MCAST_JOIN_GROUP
, (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
,
260 (void *)&mreqn
, sizeof(mreqn
));
261 if ((ret
< 0) && (optname
== IP_ADD_MEMBERSHIP
) && (errno
== EADDRINUSE
))
263 /* see above: handle possible problem when interface comes back up */
264 char buf
[1][INET_ADDRSTRLEN
];
265 zlog_info("setsockopt_ipv4_multicast attempting to drop and "
266 "re-add (fd %d, mcast %s, ifindex %u)",
268 inet_ntop(AF_INET
, &mreqn
.imr_multiaddr
,
269 buf
[0], sizeof(buf
[0])), ifindex
);
270 setsockopt(sock
, IPPROTO_IP
, IP_DROP_MEMBERSHIP
,
271 (void *)&mreqn
, sizeof(mreqn
));
272 ret
= setsockopt(sock
, IPPROTO_IP
, IP_ADD_MEMBERSHIP
,
273 (void *)&mreqn
, sizeof(mreqn
));
277 /* Example defines for another OS, boilerplate off other code in this
278 function, AND handle optname as per other sections for consistency !! */
279 /* #elif defined(BOGON_NIX) && EXAMPLE_VERSION_CODE > -100000 */
280 /* Add your favourite OS here! */
282 #elif defined(HAVE_BSD_STRUCT_IP_MREQ_HACK) /* #if OS_TYPE */
283 /* standard BSD API */
288 assert(optname
== IP_ADD_MEMBERSHIP
|| optname
== IP_DROP_MEMBERSHIP
);
291 memset (&mreq
, 0, sizeof(mreq
));
292 mreq
.imr_multiaddr
.s_addr
= mcast_addr
;
293 #if !defined __OpenBSD__
294 mreq
.imr_interface
.s_addr
= htonl (ifindex
);
296 mreq
.imr_interface
.s_addr
= if_addr
.s_addr
;
299 ret
= setsockopt (sock
, IPPROTO_IP
, optname
, (void *)&mreq
, sizeof(mreq
));
300 if ((ret
< 0) && (optname
== IP_ADD_MEMBERSHIP
) && (errno
== EADDRINUSE
))
302 /* see above: handle possible problem when interface comes back up */
303 char buf
[1][INET_ADDRSTRLEN
];
304 zlog_info("setsockopt_ipv4_multicast attempting to drop and "
305 "re-add (fd %d, mcast %s, ifindex %u)",
307 inet_ntop(AF_INET
, &mreq
.imr_multiaddr
,
308 buf
[0], sizeof(buf
[0])), ifindex
);
309 setsockopt (sock
, IPPROTO_IP
, IP_DROP_MEMBERSHIP
,
310 (void *)&mreq
, sizeof(mreq
));
311 ret
= setsockopt (sock
, IPPROTO_IP
, IP_ADD_MEMBERSHIP
,
312 (void *)&mreq
, sizeof(mreq
));
317 #error "Unsupported multicast API"
318 #endif /* #if OS_TYPE */
323 * Set IP_MULTICAST_IF socket option in an OS-dependent manner.
326 setsockopt_ipv4_multicast_if(int sock
, struct in_addr if_addr
,
330 #ifdef HAVE_STRUCT_IP_MREQN_IMR_IFINDEX
331 struct ip_mreqn mreqn
;
332 memset (&mreqn
, 0, sizeof(mreqn
));
334 mreqn
.imr_ifindex
= ifindex
;
335 return setsockopt(sock
, IPPROTO_IP
, IP_MULTICAST_IF
, (void *)&mreqn
, sizeof(mreqn
));
337 /* Example defines for another OS, boilerplate off other code in this
339 /* #elif defined(BOGON_NIX) && EXAMPLE_VERSION_CODE > -100000 */
340 /* Add your favourite OS here! */
341 #elif defined(HAVE_BSD_STRUCT_IP_MREQ_HACK)
344 #if !defined __OpenBSD__
345 m
.s_addr
= htonl (ifindex
);
347 m
.s_addr
= if_addr
.s_addr
;
350 return setsockopt (sock
, IPPROTO_IP
, IP_MULTICAST_IF
, (void *)&m
, sizeof(m
));
351 #elif defined(SUNOS_5)
352 char ifname
[IF_NAMESIZE
];
353 struct ifaddrs
*ifa
, *ifap
;
354 struct in_addr ifaddr
;
356 if (if_indextoname(ifindex
, ifname
) == NULL
)
359 if (getifaddrs(&ifa
) != 0)
362 for (ifap
= ifa
; ifap
!= NULL
; ifap
= ifap
->ifa_next
)
364 struct sockaddr_in
*sa
;
366 if (strcmp(ifap
->ifa_name
, ifname
) != 0)
368 if (ifap
->ifa_addr
->sa_family
!= AF_INET
)
370 sa
= (struct sockaddr_in
*)ifap
->ifa_addr
;
371 memcpy(&ifaddr
, &sa
->sin_addr
, sizeof(ifaddr
));
376 if (!ifap
) /* This means we did not find an IP */
379 return setsockopt(sock
, IPPROTO_IP
, IP_MULTICAST_IF
, (void *)&ifaddr
, sizeof(ifaddr
));
381 #error "Unsupported multicast API"
386 setsockopt_ipv4_multicast_loop (int sock
, u_char val
)
390 ret
= setsockopt (sock
, IPPROTO_IP
, IP_MULTICAST_LOOP
, (void *) &val
,
393 zlog_warn ("can't setsockopt IP_MULTICAST_LOOP");
399 setsockopt_ipv4_ifindex (int sock
, ifindex_t val
)
403 #if defined (IP_PKTINFO)
404 if ((ret
= setsockopt (sock
, IPPROTO_IP
, IP_PKTINFO
, &val
, sizeof (val
))) < 0)
405 zlog_warn ("Can't set IP_PKTINFO option for fd %d to %d: %s",
406 sock
,val
,safe_strerror(errno
));
407 #elif defined (IP_RECVIF)
408 if ((ret
= setsockopt (sock
, IPPROTO_IP
, IP_RECVIF
, &val
, sizeof (val
))) < 0)
409 zlog_warn ("Can't set IP_RECVIF option for fd %d to %d: %s",
410 sock
,val
,safe_strerror(errno
));
412 #warning "Neither IP_PKTINFO nor IP_RECVIF is available."
413 #warning "Will not be able to receive link info."
414 #warning "Things might be seriously broken.."
415 /* XXX Does this ever happen? Should there be a zlog_warn message here? */
422 setsockopt_ipv4_tos(int sock
, int tos
)
426 ret
= setsockopt (sock
, IPPROTO_IP
, IP_TOS
, &tos
, sizeof (tos
));
428 zlog_warn ("Can't set IP_TOS option for fd %d to %#x: %s",
429 sock
, tos
, safe_strerror(errno
));
435 setsockopt_ifindex (int af
, int sock
, ifindex_t val
)
442 ret
= setsockopt_ipv4_ifindex (sock
, val
);
445 ret
= setsockopt_ipv6_pktinfo (sock
, val
);
448 zlog_warn ("setsockopt_ifindex: unknown address family %d", af
);
454 * Requires: msgh is not NULL and points to a valid struct msghdr, which
455 * may or may not have control data about the incoming interface.
457 * Returns the interface index (small integer >= 1) if it can be
458 * determined, or else 0.
461 getsockopt_ipv4_ifindex (struct msghdr
*msgh
)
463 /* XXX: initialize to zero? (Always overwritten, so just cosmetic.) */
464 ifindex_t ifindex
= -1;
466 #if defined(IP_PKTINFO)
467 /* Linux pktinfo based ifindex retrieval */
468 struct in_pktinfo
*pktinfo
;
471 (struct in_pktinfo
*)getsockopt_cmsg_data (msgh
, IPPROTO_IP
, IP_PKTINFO
);
472 /* XXX Can pktinfo be NULL? Clean up post 0.98. */
473 ifindex
= pktinfo
->ipi_ifindex
;
475 #elif defined(IP_RECVIF)
477 /* retrieval based on IP_RECVIF */
480 /* BSD systems use a sockaddr_dl as the control message payload. */
481 struct sockaddr_dl
*sdl
;
483 /* SUNOS_5 uses an integer with the index. */
484 ifindex_t
*ifindex_p
;
490 (struct sockaddr_dl
*)getsockopt_cmsg_data (msgh
, IPPROTO_IP
, IP_RECVIF
);
492 ifindex
= sdl
->sdl_index
;
497 * Solaris. On Solaris 8, IP_RECVIF is defined, but the call to
498 * enable it fails with errno=99, and the struct msghdr has
501 ifindex_p
= (uint_t
*)getsockopt_cmsg_data (msgh
, IPPROTO_IP
, IP_RECVIF
);
502 if (ifindex_p
!= NULL
)
503 ifindex
= *ifindex_p
;
510 * Neither IP_PKTINFO nor IP_RECVIF defined - warn at compile time.
511 * XXX Decide if this is a core service, or if daemons have to cope.
512 * Since Solaris 8 and OpenBSD seem not to provide it, it seems that
513 * daemons have to cope.
515 #warning "getsockopt_ipv4_ifindex: Neither IP_PKTINFO nor IP_RECVIF defined."
516 #warning "Some daemons may fail to operate correctly!"
519 #endif /* IP_PKTINFO */
524 /* return ifindex, 0 if none found */
526 getsockopt_ifindex (int af
, struct msghdr
*msgh
)
531 return (getsockopt_ipv4_ifindex (msgh
));
534 return (getsockopt_ipv6_ifindex (msgh
));
537 zlog_warn ("getsockopt_ifindex: unknown address family %d", af
);
542 /* swab iph between order system uses for IP_HDRINCL and host order */
544 sockopt_iphdrincl_swab_htosys (struct ip
*iph
)
546 /* BSD and derived take iph in network order, except for
549 #ifndef HAVE_IP_HDRINCL_BSD_ORDER
550 iph
->ip_len
= htons(iph
->ip_len
);
551 iph
->ip_off
= htons(iph
->ip_off
);
552 #endif /* HAVE_IP_HDRINCL_BSD_ORDER */
554 iph
->ip_id
= htons(iph
->ip_id
);
558 sockopt_iphdrincl_swab_systoh (struct ip
*iph
)
560 #ifndef HAVE_IP_HDRINCL_BSD_ORDER
561 iph
->ip_len
= ntohs(iph
->ip_len
);
562 iph
->ip_off
= ntohs(iph
->ip_off
);
563 #endif /* HAVE_IP_HDRINCL_BSD_ORDER */
565 iph
->ip_id
= ntohs(iph
->ip_id
);
569 sockopt_tcp_rtt (int sock
)
573 socklen_t len
= sizeof(ti
);
575 if (getsockopt (sock
, IPPROTO_TCP
, TCP_INFO
, &ti
, &len
) != 0)
578 return ti
.tcpi_rtt
/ 1000;
585 sockopt_tcp_signature (int sock
, union sockunion
*su
, const char *password
)
587 #if defined(HAVE_TCP_MD5_LINUX24) && defined(GNU_LINUX)
588 /* Support for the old Linux 2.4 TCP-MD5 patch, taken from Hasso Tepper's
589 * version of the Quagga patch (based on work by Rick Payne, and Bruce
592 #define TCP_MD5_AUTH 13
593 #define TCP_MD5_AUTH_ADD 1
594 #define TCP_MD5_AUTH_DEL 2
595 struct tcp_rfc2385_cmd
{
596 u_int8_t command
; /* Command - Add/Delete */
597 u_int32_t address
; /* IPV4 address associated */
598 u_int8_t keylen
; /* MD5 Key len (do NOT assume 0 terminated ascii) */
599 void *key
; /* MD5 Key */
601 struct in_addr
*addr
= &su
->sin
.sin_addr
;
603 cmd
.command
= (password
!= NULL
? TCP_MD5_AUTH_ADD
: TCP_MD5_AUTH_DEL
);
604 cmd
.address
= addr
->s_addr
;
605 cmd
.keylen
= (password
!= NULL
? strlen (password
) : 0);
608 return setsockopt (sock
, IPPROTO_TCP
, TCP_MD5_AUTH
, &cmd
, sizeof cmd
);
610 #elif HAVE_DECL_TCP_MD5SIG
614 * XXX Need to do PF_KEY operation here to add/remove an SA entry,
615 * and add/remove an SP entry for this peer's packet flows also.
617 int md5sig
= password
&& *password
? 1 : 0;
619 int keylen
= password
? strlen (password
) : 0;
620 struct tcp_md5sig md5sig
;
621 union sockunion
*su2
, *susock
;
623 /* Figure out whether the socket and the sockunion are the same family..
624 * adding AF_INET to AF_INET6 needs to be v4 mapped, you'd think..
626 if (!(susock
= sockunion_getsockname (sock
)))
629 if (susock
->sa
.sa_family
== su
->sa
.sa_family
)
636 if (su2
->sa
.sa_family
== AF_INET
)
638 sockunion_free (susock
);
642 /* If this does not work, then all users of this sockopt will need to
643 * differentiate between IPv4 and IPv6, and keep seperate sockets for
646 * Sadly, it doesn't seem to work at present. It's unknown whether
647 * this is a bug or not.
649 if (su2
->sa
.sa_family
== AF_INET6
650 && su
->sa
.sa_family
== AF_INET
)
652 su2
->sin6
.sin6_family
= AF_INET6
;
653 /* V4Map the address */
654 memset (&su2
->sin6
.sin6_addr
, 0, sizeof (struct in6_addr
));
655 su2
->sin6
.sin6_addr
.s6_addr32
[2] = htonl(0xffff);
656 memcpy (&su2
->sin6
.sin6_addr
.s6_addr32
[3], &su
->sin
.sin_addr
, 4);
660 memset (&md5sig
, 0, sizeof (md5sig
));
661 memcpy (&md5sig
.tcpm_addr
, su2
, sizeof (*su2
));
662 md5sig
.tcpm_keylen
= keylen
;
664 memcpy (md5sig
.tcpm_key
, password
, keylen
);
665 sockunion_free (susock
);
666 #endif /* GNU_LINUX */
667 if ((ret
= setsockopt (sock
, IPPROTO_TCP
, TCP_MD5SIG
, &md5sig
, sizeof md5sig
)) < 0)
669 /* ENOENT is harmless. It is returned when we clear a password for which
670 one was not previously set. */
674 zlog_err ("sockopt_tcp_signature: setsockopt(%d): %s",
675 sock
, safe_strerror(errno
));
678 #else /* HAVE_TCP_MD5SIG */
680 #endif /* !HAVE_TCP_MD5SIG */