]>
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
17 * along with GNU Zebra; see the file COPYING. If not, write to the Free
18 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
25 #include "sockunion.h"
28 setsockopt_so_recvbuf (int sock
, int size
)
32 if ( (ret
= setsockopt (sock
, SOL_SOCKET
, SO_RCVBUF
, (char *)
33 &size
, sizeof (int))) < 0)
34 zlog_err ("fd %d: can't setsockopt SO_RCVBUF to %d: %s",
35 sock
,size
,safe_strerror(errno
));
41 setsockopt_so_sendbuf (const int sock
, int size
)
43 int ret
= setsockopt (sock
, SOL_SOCKET
, SO_SNDBUF
,
44 (char *)&size
, sizeof (int));
47 zlog_err ("fd %d: can't setsockopt SO_SNDBUF to %d: %s",
48 sock
, size
, safe_strerror (errno
));
54 getsockopt_so_sendbuf (const int sock
)
57 socklen_t optlen
= sizeof (optval
);
58 int ret
= getsockopt (sock
, SOL_SOCKET
, SO_SNDBUF
,
59 (char *)&optval
, &optlen
);
62 zlog_err ("fd %d: can't getsockopt SO_SNDBUF: %d (%s)",
63 sock
, errno
, safe_strerror (errno
));
70 getsockopt_cmsg_data (struct msghdr
*msgh
, int level
, int type
)
75 for (cmsg
= ZCMSG_FIRSTHDR(msgh
);
77 cmsg
= CMSG_NXTHDR(msgh
, cmsg
))
78 if (cmsg
->cmsg_level
== level
&& cmsg
->cmsg_type
)
79 return (ptr
= CMSG_DATA(cmsg
));
85 /* Set IPv6 packet info to the socket. */
87 setsockopt_ipv6_pktinfo (int sock
, int val
)
91 #ifdef IPV6_RECVPKTINFO /*2292bis-01*/
92 ret
= setsockopt(sock
, IPPROTO_IPV6
, IPV6_RECVPKTINFO
, &val
, sizeof(val
));
94 zlog_warn ("can't setsockopt IPV6_RECVPKTINFO : %s", safe_strerror (errno
));
96 ret
= setsockopt(sock
, IPPROTO_IPV6
, IPV6_PKTINFO
, &val
, sizeof(val
));
98 zlog_warn ("can't setsockopt IPV6_PKTINFO : %s", safe_strerror (errno
));
99 #endif /* INIA_IPV6 */
103 /* Set multicast hops val to the socket. */
105 setsockopt_ipv6_checksum (int sock
, int val
)
110 ret
= setsockopt(sock
, IPPROTO_RAW
, IPV6_CHECKSUM
, &val
, sizeof(val
));
112 ret
= setsockopt(sock
, IPPROTO_IPV6
, IPV6_CHECKSUM
, &val
, sizeof(val
));
113 #endif /* GNU_LINUX */
115 zlog_warn ("can't setsockopt IPV6_CHECKSUM");
119 /* Set multicast hops val to the socket. */
121 setsockopt_ipv6_multicast_hops (int sock
, int val
)
125 ret
= setsockopt(sock
, IPPROTO_IPV6
, IPV6_MULTICAST_HOPS
, &val
, sizeof(val
));
127 zlog_warn ("can't setsockopt IPV6_MULTICAST_HOPS");
131 /* Set multicast hops val to the socket. */
133 setsockopt_ipv6_unicast_hops (int sock
, int val
)
137 ret
= setsockopt(sock
, IPPROTO_IPV6
, IPV6_UNICAST_HOPS
, &val
, sizeof(val
));
139 zlog_warn ("can't setsockopt IPV6_UNICAST_HOPS");
144 setsockopt_ipv6_hoplimit (int sock
, int val
)
148 #ifdef IPV6_RECVHOPLIMIT /*2292bis-01*/
149 ret
= setsockopt (sock
, IPPROTO_IPV6
, IPV6_RECVHOPLIMIT
, &val
, sizeof(val
));
151 zlog_warn ("can't setsockopt IPV6_RECVHOPLIMIT");
153 ret
= setsockopt (sock
, IPPROTO_IPV6
, IPV6_HOPLIMIT
, &val
, sizeof(val
));
155 zlog_warn ("can't setsockopt IPV6_HOPLIMIT");
160 /* Set multicast loop zero to the socket. */
162 setsockopt_ipv6_multicast_loop (int sock
, int val
)
166 ret
= setsockopt (sock
, IPPROTO_IPV6
, IPV6_MULTICAST_LOOP
, &val
,
169 zlog_warn ("can't setsockopt IPV6_MULTICAST_LOOP");
174 getsockopt_ipv6_ifindex (struct msghdr
*msgh
)
176 struct in6_pktinfo
*pktinfo
;
178 pktinfo
= getsockopt_cmsg_data (msgh
, IPPROTO_IPV6
, IPV6_PKTINFO
);
180 return pktinfo
->ipi6_ifindex
;
184 setsockopt_ipv6_tclass(int sock
, int tclass
)
188 #ifdef IPV6_TCLASS /* RFC3542 */
189 ret
= setsockopt (sock
, IPPROTO_IPV6
, IPV6_TCLASS
, &tclass
, sizeof (tclass
));
191 zlog_warn ("Can't set IPV6_TCLASS option for fd %d to %#x: %s",
192 sock
, tclass
, safe_strerror(errno
));
196 #endif /* HAVE_IPV6 */
199 * Process multicast socket options for IPv4 in an OS-dependent manner.
200 * Supported options are IP_{ADD,DROP}_MEMBERSHIP.
202 * Many operating systems have a limit on the number of groups that
203 * can be joined per socket (where each group and local address
204 * counts). This impacts OSPF, which joins groups on each interface
205 * using a single socket. The limit is typically 20, derived from the
206 * original BSD multicast implementation. Some systems have
207 * mechanisms for increasing this limit.
209 * In many 4.4BSD-derived systems, multicast group operations are not
210 * allowed on interfaces that are not UP. Thus, a previous attempt to
211 * leave the group may have failed, leaving it still joined, and we
212 * drop/join quietly to recover. This may not be necessary, but aims to
213 * defend against unknown behavior in that we will still return an error
214 * if the second join fails. It is not clear how other systems
215 * (e.g. Linux, Solaris) behave when leaving groups on down interfaces,
216 * but this behavior should not be harmful if they behave the same way,
217 * allow leaves, or implicitly leave all groups joined to down interfaces.
220 setsockopt_ipv4_multicast(int sock
,
222 struct in_addr if_addr
,
223 unsigned int mcast_addr
,
228 struct sockaddr_in
*si
;
230 memset (&gr
, 0, sizeof(gr
));
231 si
= (struct sockaddr_in
*)&gr
.gr_group
;
232 gr
.gr_interface
= ifindex
;
233 si
->sin_family
= AF_INET
;
234 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
235 si
->sin_len
= sizeof(struct sockaddr_in
);
236 #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
237 si
->sin_addr
.s_addr
= mcast_addr
;
238 ret
= setsockopt(sock
, IPPROTO_IP
, (optname
== IP_ADD_MEMBERSHIP
) ?
239 MCAST_JOIN_GROUP
: MCAST_LEAVE_GROUP
, (void *)&gr
, sizeof(gr
));
240 if ((ret
< 0) && (optname
== IP_ADD_MEMBERSHIP
) && (errno
== EADDRINUSE
))
242 setsockopt(sock
, IPPROTO_IP
, MCAST_LEAVE_GROUP
, (void *)&gr
, sizeof(gr
));
243 ret
= setsockopt(sock
, IPPROTO_IP
, MCAST_JOIN_GROUP
, (void *)&gr
, sizeof(gr
));
247 #elif defined(HAVE_STRUCT_IP_MREQN_IMR_IFINDEX) && !defined(__FreeBSD__)
248 struct ip_mreqn mreqn
;
251 assert(optname
== IP_ADD_MEMBERSHIP
|| optname
== IP_DROP_MEMBERSHIP
);
252 memset (&mreqn
, 0, sizeof(mreqn
));
254 mreqn
.imr_multiaddr
.s_addr
= mcast_addr
;
255 mreqn
.imr_ifindex
= ifindex
;
257 ret
= setsockopt(sock
, IPPROTO_IP
, optname
,
258 (void *)&mreqn
, sizeof(mreqn
));
259 if ((ret
< 0) && (optname
== IP_ADD_MEMBERSHIP
) && (errno
== EADDRINUSE
))
261 /* see above: handle possible problem when interface comes back up */
262 char buf
[1][INET_ADDRSTRLEN
];
263 zlog_info("setsockopt_ipv4_multicast attempting to drop and "
264 "re-add (fd %d, mcast %s, ifindex %u)",
266 inet_ntop(AF_INET
, &mreqn
.imr_multiaddr
,
267 buf
[0], sizeof(buf
[0])), ifindex
);
268 setsockopt(sock
, IPPROTO_IP
, IP_DROP_MEMBERSHIP
,
269 (void *)&mreqn
, sizeof(mreqn
));
270 ret
= setsockopt(sock
, IPPROTO_IP
, IP_ADD_MEMBERSHIP
,
271 (void *)&mreqn
, sizeof(mreqn
));
275 /* Example defines for another OS, boilerplate off other code in this
276 function, AND handle optname as per other sections for consistency !! */
277 /* #elif defined(BOGON_NIX) && EXAMPLE_VERSION_CODE > -100000 */
278 /* Add your favourite OS here! */
280 #elif defined(HAVE_BSD_STRUCT_IP_MREQ_HACK) /* #if OS_TYPE */
281 /* standard BSD API */
286 assert(optname
== IP_ADD_MEMBERSHIP
|| optname
== IP_DROP_MEMBERSHIP
);
289 memset (&mreq
, 0, sizeof(mreq
));
290 mreq
.imr_multiaddr
.s_addr
= mcast_addr
;
291 #if !defined __OpenBSD__
292 mreq
.imr_interface
.s_addr
= htonl (ifindex
);
294 mreq
.imr_interface
.s_addr
= if_addr
.s_addr
;
297 ret
= setsockopt (sock
, IPPROTO_IP
, optname
, (void *)&mreq
, sizeof(mreq
));
298 if ((ret
< 0) && (optname
== IP_ADD_MEMBERSHIP
) && (errno
== EADDRINUSE
))
300 /* see above: handle possible problem when interface comes back up */
301 char buf
[1][INET_ADDRSTRLEN
];
302 zlog_info("setsockopt_ipv4_multicast attempting to drop and "
303 "re-add (fd %d, mcast %s, ifindex %u)",
305 inet_ntop(AF_INET
, &mreq
.imr_multiaddr
,
306 buf
[0], sizeof(buf
[0])), ifindex
);
307 setsockopt (sock
, IPPROTO_IP
, IP_DROP_MEMBERSHIP
,
308 (void *)&mreq
, sizeof(mreq
));
309 ret
= setsockopt (sock
, IPPROTO_IP
, IP_ADD_MEMBERSHIP
,
310 (void *)&mreq
, sizeof(mreq
));
315 #error "Unsupported multicast API"
316 #endif /* #if OS_TYPE */
321 * Set IP_MULTICAST_IF socket option in an OS-dependent manner.
324 setsockopt_ipv4_multicast_if(int sock
, struct in_addr if_addr
,
328 #ifdef HAVE_STRUCT_IP_MREQN_IMR_IFINDEX
329 struct ip_mreqn mreqn
;
330 memset (&mreqn
, 0, sizeof(mreqn
));
332 mreqn
.imr_ifindex
= ifindex
;
333 return setsockopt(sock
, IPPROTO_IP
, IP_MULTICAST_IF
, (void *)&mreqn
, sizeof(mreqn
));
335 /* Example defines for another OS, boilerplate off other code in this
337 /* #elif defined(BOGON_NIX) && EXAMPLE_VERSION_CODE > -100000 */
338 /* Add your favourite OS here! */
339 #elif defined(HAVE_BSD_STRUCT_IP_MREQ_HACK)
342 #if !defined __OpenBSD__
343 m
.s_addr
= htonl (ifindex
);
345 m
.s_addr
= if_addr
.s_addr
;
348 return setsockopt (sock
, IPPROTO_IP
, IP_MULTICAST_IF
, (void *)&m
, sizeof(m
));
350 #error "Unsupported multicast API"
355 setsockopt_ipv4_ifindex (int sock
, ifindex_t val
)
359 #if defined (IP_PKTINFO)
360 if ((ret
= setsockopt (sock
, IPPROTO_IP
, IP_PKTINFO
, &val
, sizeof (val
))) < 0)
361 zlog_warn ("Can't set IP_PKTINFO option for fd %d to %d: %s",
362 sock
,val
,safe_strerror(errno
));
363 #elif defined (IP_RECVIF)
364 if ((ret
= setsockopt (sock
, IPPROTO_IP
, IP_RECVIF
, &val
, sizeof (val
))) < 0)
365 zlog_warn ("Can't set IP_RECVIF option for fd %d to %d: %s",
366 sock
,val
,safe_strerror(errno
));
368 #warning "Neither IP_PKTINFO nor IP_RECVIF is available."
369 #warning "Will not be able to receive link info."
370 #warning "Things might be seriously broken.."
371 /* XXX Does this ever happen? Should there be a zlog_warn message here? */
378 setsockopt_ipv4_tos(int sock
, int tos
)
382 ret
= setsockopt (sock
, IPPROTO_IP
, IP_TOS
, &tos
, sizeof (tos
));
384 zlog_warn ("Can't set IP_TOS option for fd %d to %#x: %s",
385 sock
, tos
, safe_strerror(errno
));
391 setsockopt_ifindex (int af
, int sock
, ifindex_t val
)
398 ret
= setsockopt_ipv4_ifindex (sock
, val
);
402 ret
= setsockopt_ipv6_pktinfo (sock
, val
);
406 zlog_warn ("setsockopt_ifindex: unknown address family %d", af
);
412 * Requires: msgh is not NULL and points to a valid struct msghdr, which
413 * may or may not have control data about the incoming interface.
415 * Returns the interface index (small integer >= 1) if it can be
416 * determined, or else 0.
419 getsockopt_ipv4_ifindex (struct msghdr
*msgh
)
421 /* XXX: initialize to zero? (Always overwritten, so just cosmetic.) */
422 ifindex_t ifindex
= -1;
424 #if defined(IP_PKTINFO)
425 /* Linux pktinfo based ifindex retrieval */
426 struct in_pktinfo
*pktinfo
;
429 (struct in_pktinfo
*)getsockopt_cmsg_data (msgh
, IPPROTO_IP
, IP_PKTINFO
);
430 /* XXX Can pktinfo be NULL? Clean up post 0.98. */
431 ifindex
= pktinfo
->ipi_ifindex
;
433 #elif defined(IP_RECVIF)
435 /* retrieval based on IP_RECVIF */
438 /* BSD systems use a sockaddr_dl as the control message payload. */
439 struct sockaddr_dl
*sdl
;
441 /* SUNOS_5 uses an integer with the index. */
442 ifindex_t
*ifindex_p
;
448 (struct sockaddr_dl
*)getsockopt_cmsg_data (msgh
, IPPROTO_IP
, IP_RECVIF
);
450 ifindex
= sdl
->sdl_index
;
455 * Solaris. On Solaris 8, IP_RECVIF is defined, but the call to
456 * enable it fails with errno=99, and the struct msghdr has
459 ifindex_p
= (uint_t
*)getsockopt_cmsg_data (msgh
, IPPROTO_IP
, IP_RECVIF
);
460 if (ifindex_p
!= NULL
)
461 ifindex
= *ifindex_p
;
468 * Neither IP_PKTINFO nor IP_RECVIF defined - warn at compile time.
469 * XXX Decide if this is a core service, or if daemons have to cope.
470 * Since Solaris 8 and OpenBSD seem not to provide it, it seems that
471 * daemons have to cope.
473 #warning "getsockopt_ipv4_ifindex: Neither IP_PKTINFO nor IP_RECVIF defined."
474 #warning "Some daemons may fail to operate correctly!"
477 #endif /* IP_PKTINFO */
482 /* return ifindex, 0 if none found */
484 getsockopt_ifindex (int af
, struct msghdr
*msgh
)
489 return (getsockopt_ipv4_ifindex (msgh
));
493 return (getsockopt_ipv6_ifindex (msgh
));
497 zlog_warn ("getsockopt_ifindex: unknown address family %d", af
);
502 /* swab iph between order system uses for IP_HDRINCL and host order */
504 sockopt_iphdrincl_swab_htosys (struct ip
*iph
)
506 /* BSD and derived take iph in network order, except for
509 #ifndef HAVE_IP_HDRINCL_BSD_ORDER
510 iph
->ip_len
= htons(iph
->ip_len
);
511 iph
->ip_off
= htons(iph
->ip_off
);
512 #endif /* HAVE_IP_HDRINCL_BSD_ORDER */
514 iph
->ip_id
= htons(iph
->ip_id
);
518 sockopt_iphdrincl_swab_systoh (struct ip
*iph
)
520 #ifndef HAVE_IP_HDRINCL_BSD_ORDER
521 iph
->ip_len
= ntohs(iph
->ip_len
);
522 iph
->ip_off
= ntohs(iph
->ip_off
);
523 #endif /* HAVE_IP_HDRINCL_BSD_ORDER */
525 iph
->ip_id
= ntohs(iph
->ip_id
);
529 sockopt_tcp_rtt (int sock
)
533 socklen_t len
= sizeof(ti
);
535 if (getsockopt (sock
, IPPROTO_TCP
, TCP_INFO
, &ti
, &len
) != 0)
538 return ti
.tcpi_rtt
/ 1000;
545 sockopt_tcp_signature (int sock
, union sockunion
*su
, const char *password
)
547 #if defined(HAVE_TCP_MD5_LINUX24) && defined(GNU_LINUX)
548 /* Support for the old Linux 2.4 TCP-MD5 patch, taken from Hasso Tepper's
549 * version of the Quagga patch (based on work by Rick Payne, and Bruce
552 #define TCP_MD5_AUTH 13
553 #define TCP_MD5_AUTH_ADD 1
554 #define TCP_MD5_AUTH_DEL 2
555 struct tcp_rfc2385_cmd
{
556 u_int8_t command
; /* Command - Add/Delete */
557 u_int32_t address
; /* IPV4 address associated */
558 u_int8_t keylen
; /* MD5 Key len (do NOT assume 0 terminated ascii) */
559 void *key
; /* MD5 Key */
561 struct in_addr
*addr
= &su
->sin
.sin_addr
;
563 cmd
.command
= (password
!= NULL
? TCP_MD5_AUTH_ADD
: TCP_MD5_AUTH_DEL
);
564 cmd
.address
= addr
->s_addr
;
565 cmd
.keylen
= (password
!= NULL
? strlen (password
) : 0);
568 return setsockopt (sock
, IPPROTO_TCP
, TCP_MD5_AUTH
, &cmd
, sizeof cmd
);
570 #elif HAVE_DECL_TCP_MD5SIG
574 * XXX Need to do PF_KEY operation here to add/remove an SA entry,
575 * and add/remove an SP entry for this peer's packet flows also.
577 int md5sig
= password
&& *password
? 1 : 0;
579 int keylen
= password
? strlen (password
) : 0;
580 struct tcp_md5sig md5sig
;
581 union sockunion
*su2
, *susock
;
583 /* Figure out whether the socket and the sockunion are the same family..
584 * adding AF_INET to AF_INET6 needs to be v4 mapped, you'd think..
586 if (!(susock
= sockunion_getsockname (sock
)))
589 if (susock
->sa
.sa_family
== su
->sa
.sa_family
)
596 if (su2
->sa
.sa_family
== AF_INET
)
598 sockunion_free (susock
);
603 /* If this does not work, then all users of this sockopt will need to
604 * differentiate between IPv4 and IPv6, and keep seperate sockets for
607 * Sadly, it doesn't seem to work at present. It's unknown whether
608 * this is a bug or not.
610 if (su2
->sa
.sa_family
== AF_INET6
611 && su
->sa
.sa_family
== AF_INET
)
613 su2
->sin6
.sin6_family
= AF_INET6
;
614 /* V4Map the address */
615 memset (&su2
->sin6
.sin6_addr
, 0, sizeof (struct in6_addr
));
616 su2
->sin6
.sin6_addr
.s6_addr32
[2] = htonl(0xffff);
617 memcpy (&su2
->sin6
.sin6_addr
.s6_addr32
[3], &su
->sin
.sin_addr
, 4);
622 memset (&md5sig
, 0, sizeof (md5sig
));
623 memcpy (&md5sig
.tcpm_addr
, su2
, sizeof (*su2
));
624 md5sig
.tcpm_keylen
= keylen
;
626 memcpy (md5sig
.tcpm_key
, password
, keylen
);
627 sockunion_free (susock
);
628 #endif /* GNU_LINUX */
629 if ((ret
= setsockopt (sock
, IPPROTO_TCP
, TCP_MD5SIG
, &md5sig
, sizeof md5sig
)) < 0)
631 /* ENOENT is harmless. It is returned when we clear a password for which
632 one was not previously set. */
636 zlog_err ("sockopt_tcp_signature: setsockopt(%d): %s",
637 sock
, safe_strerror(errno
));
640 #else /* HAVE_TCP_MD5SIG */
642 #endif /* !HAVE_TCP_MD5SIG */