]> git.proxmox.com Git - mirror_frr.git/blame_incremental - lib/sockopt.c
Merge pull request #11076 from routingrocks/vrrp_master_ad_cli
[mirror_frr.git] / lib / sockopt.c
... / ...
CommitLineData
1/* setsockopt functions
2 * Copyright (C) 1999 Kunihiro Ishiguro
3 *
4 * This file is part of GNU Zebra.
5 *
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
9 * later version.
10 *
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.
15 *
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
19 */
20
21#include <zebra.h>
22
23#include "log.h"
24#include "sockopt.h"
25#include "sockunion.h"
26#include "lib_errors.h"
27
28#if (defined(__FreeBSD__) \
29 && ((__FreeBSD_version >= 500022 && __FreeBSD_version < 700000) \
30 || (__FreeBSD_version < 500000 && __FreeBSD_version >= 440000))) \
31 || (defined(__NetBSD__) && defined(__NetBSD_Version__) \
32 && __NetBSD_Version__ >= 106010000) \
33 || defined(__OpenBSD__) || defined(__APPLE__) \
34 || defined(__DragonFly__) || defined(__sun)
35#define HAVE_BSD_STRUCT_IP_MREQ_HACK
36#endif
37
38void setsockopt_so_recvbuf(int sock, int size)
39{
40 int orig_req = size;
41
42 while (setsockopt(sock, SOL_SOCKET, SO_RCVBUF, &size, sizeof(size))
43 == -1)
44 size /= 2;
45
46 if (size != orig_req)
47 flog_err(EC_LIB_SOCKET,
48 "%s: fd %d: SO_RCVBUF set to %d (requested %d)",
49 __func__, sock, size, orig_req);
50}
51
52void setsockopt_so_sendbuf(const int sock, int size)
53{
54 int orig_req = size;
55
56 while (setsockopt(sock, SOL_SOCKET, SO_SNDBUF, &size, sizeof(size))
57 == -1)
58 size /= 2;
59
60 if (size != orig_req)
61 flog_err(EC_LIB_SOCKET,
62 "%s: fd %d: SO_SNDBUF set to %d (requested %d)",
63 __func__, sock, size, orig_req);
64}
65
66int getsockopt_so_sendbuf(const int sock)
67{
68 uint32_t optval;
69 socklen_t optlen = sizeof(optval);
70 int ret = getsockopt(sock, SOL_SOCKET, SO_SNDBUF, (char *)&optval,
71 &optlen);
72 if (ret < 0) {
73 flog_err_sys(EC_LIB_SYSTEM_CALL,
74 "fd %d: can't getsockopt SO_SNDBUF: %d (%s)", sock,
75 errno, safe_strerror(errno));
76 return ret;
77 }
78 return optval;
79}
80
81int getsockopt_so_recvbuf(const int sock)
82{
83 uint32_t optval;
84 socklen_t optlen = sizeof(optval);
85 int ret = getsockopt(sock, SOL_SOCKET, SO_RCVBUF, (char *)&optval,
86 &optlen);
87 if (ret < 0) {
88 flog_err_sys(EC_LIB_SYSTEM_CALL,
89 "fd %d: can't getsockopt SO_RCVBUF: %d (%s)", sock,
90 errno, safe_strerror(errno));
91 return ret;
92 }
93 return optval;
94}
95
96static void *getsockopt_cmsg_data(struct msghdr *msgh, int level, int type)
97{
98 struct cmsghdr *cmsg;
99
100 for (cmsg = CMSG_FIRSTHDR(msgh); cmsg != NULL;
101 cmsg = CMSG_NXTHDR(msgh, cmsg))
102 if (cmsg->cmsg_level == level && cmsg->cmsg_type == type)
103 return CMSG_DATA(cmsg);
104
105 return NULL;
106}
107
108/* Set IPv6 packet info to the socket. */
109int setsockopt_ipv6_pktinfo(int sock, int val)
110{
111 int ret;
112
113#ifdef IPV6_RECVPKTINFO /*2292bis-01*/
114 ret = setsockopt(sock, IPPROTO_IPV6, IPV6_RECVPKTINFO, &val,
115 sizeof(val));
116 if (ret < 0)
117 flog_err(EC_LIB_SOCKET,
118 "can't setsockopt IPV6_RECVPKTINFO : %s",
119 safe_strerror(errno));
120#else /*RFC2292*/
121 ret = setsockopt(sock, IPPROTO_IPV6, IPV6_PKTINFO, &val, sizeof(val));
122 if (ret < 0)
123 flog_err(EC_LIB_SOCKET, "can't setsockopt IPV6_PKTINFO : %s",
124 safe_strerror(errno));
125#endif /* IANA_IPV6 */
126 return ret;
127}
128
129/* Set multicast hops val to the socket. */
130int setsockopt_ipv6_multicast_hops(int sock, int val)
131{
132 int ret;
133
134 ret = setsockopt(sock, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &val,
135 sizeof(val));
136 if (ret < 0)
137 flog_err(EC_LIB_SOCKET, "can't setsockopt IPV6_MULTICAST_HOPS");
138 return ret;
139}
140
141/* Set multicast hops val to the socket. */
142int setsockopt_ipv6_unicast_hops(int sock, int val)
143{
144 int ret;
145
146 ret = setsockopt(sock, IPPROTO_IPV6, IPV6_UNICAST_HOPS, &val,
147 sizeof(val));
148 if (ret < 0)
149 flog_err(EC_LIB_SOCKET, "can't setsockopt IPV6_UNICAST_HOPS");
150 return ret;
151}
152
153int setsockopt_ipv6_hoplimit(int sock, int val)
154{
155 int ret;
156
157#ifdef IPV6_RECVHOPLIMIT /*2292bis-01*/
158 ret = setsockopt(sock, IPPROTO_IPV6, IPV6_RECVHOPLIMIT, &val,
159 sizeof(val));
160 if (ret < 0)
161 flog_err(EC_LIB_SOCKET, "can't setsockopt IPV6_RECVHOPLIMIT");
162#else /*RFC2292*/
163 ret = setsockopt(sock, IPPROTO_IPV6, IPV6_HOPLIMIT, &val, sizeof(val));
164 if (ret < 0)
165 flog_err(EC_LIB_SOCKET, "can't setsockopt IPV6_HOPLIMIT");
166#endif
167 return ret;
168}
169
170/* Set multicast loop zero to the socket. */
171int setsockopt_ipv6_multicast_loop(int sock, int val)
172{
173 int ret;
174
175 ret = setsockopt(sock, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, &val,
176 sizeof(val));
177 if (ret < 0)
178 flog_err(EC_LIB_SOCKET, "can't setsockopt IPV6_MULTICAST_LOOP");
179 return ret;
180}
181
182static int getsockopt_ipv6_ifindex(struct msghdr *msgh)
183{
184 struct in6_pktinfo *pktinfo;
185
186 pktinfo = getsockopt_cmsg_data(msgh, IPPROTO_IPV6, IPV6_PKTINFO);
187
188 return pktinfo->ipi6_ifindex;
189}
190
191int setsockopt_ipv6_tclass(int sock, int tclass)
192{
193 int ret = 0;
194
195#ifdef IPV6_TCLASS /* RFC3542 */
196 ret = setsockopt(sock, IPPROTO_IPV6, IPV6_TCLASS, &tclass,
197 sizeof(tclass));
198 if (ret < 0)
199 flog_err(EC_LIB_SOCKET,
200 "Can't set IPV6_TCLASS option for fd %d to %#x: %s",
201 sock, tclass, safe_strerror(errno));
202#endif
203 return ret;
204}
205
206/*
207 * Process multicast socket options for IPv4 in an OS-dependent manner.
208 * Supported options are IP_{ADD,DROP}_MEMBERSHIP.
209 *
210 * Many operating systems have a limit on the number of groups that
211 * can be joined per socket (where each group and local address
212 * counts). This impacts OSPF, which joins groups on each interface
213 * using a single socket. The limit is typically 20, derived from the
214 * original BSD multicast implementation. Some systems have
215 * mechanisms for increasing this limit.
216 *
217 * In many 4.4BSD-derived systems, multicast group operations are not
218 * allowed on interfaces that are not UP. Thus, a previous attempt to
219 * leave the group may have failed, leaving it still joined, and we
220 * drop/join quietly to recover. This may not be necessary, but aims to
221 * defend against unknown behavior in that we will still return an error
222 * if the second join fails. It is not clear how other systems
223 * (e.g. Linux, Solaris) behave when leaving groups on down interfaces,
224 * but this behavior should not be harmful if they behave the same way,
225 * allow leaves, or implicitly leave all groups joined to down interfaces.
226 */
227int setsockopt_ipv4_multicast(int sock, int optname, struct in_addr if_addr,
228 unsigned int mcast_addr, ifindex_t ifindex)
229{
230#ifdef HAVE_RFC3678
231 struct group_req gr;
232 struct sockaddr_in *si;
233 int ret;
234 memset(&gr, 0, sizeof(gr));
235 si = (struct sockaddr_in *)&gr.gr_group;
236 gr.gr_interface = ifindex;
237 si->sin_family = AF_INET;
238#ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
239 si->sin_len = sizeof(struct sockaddr_in);
240#endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
241 si->sin_addr.s_addr = mcast_addr;
242 ret = setsockopt(sock, IPPROTO_IP,
243 (optname == IP_ADD_MEMBERSHIP) ? MCAST_JOIN_GROUP
244 : MCAST_LEAVE_GROUP,
245 (void *)&gr, sizeof(gr));
246 if ((ret < 0) && (optname == IP_ADD_MEMBERSHIP)
247 && (errno == EADDRINUSE)) {
248 setsockopt(sock, IPPROTO_IP, MCAST_LEAVE_GROUP, (void *)&gr,
249 sizeof(gr));
250 ret = setsockopt(sock, IPPROTO_IP, MCAST_JOIN_GROUP,
251 (void *)&gr, sizeof(gr));
252 }
253 return ret;
254
255#elif defined(HAVE_STRUCT_IP_MREQN_IMR_IFINDEX) && !defined(__FreeBSD__)
256 struct ip_mreqn mreqn;
257 int ret;
258
259 assert(optname == IP_ADD_MEMBERSHIP || optname == IP_DROP_MEMBERSHIP);
260 memset(&mreqn, 0, sizeof(mreqn));
261
262 mreqn.imr_multiaddr.s_addr = mcast_addr;
263 mreqn.imr_ifindex = ifindex;
264
265 ret = setsockopt(sock, IPPROTO_IP, optname, (void *)&mreqn,
266 sizeof(mreqn));
267 if ((ret < 0) && (optname == IP_ADD_MEMBERSHIP)
268 && (errno == EADDRINUSE)) {
269 /* see above: handle possible problem when interface comes back
270 * up */
271 char buf[1][INET_ADDRSTRLEN];
272 zlog_info(
273 "setsockopt_ipv4_multicast attempting to drop and re-add (fd %d, mcast %s, ifindex %u)",
274 sock, inet_ntop(AF_INET, &mreqn.imr_multiaddr, buf[0],
275 sizeof(buf[0])),
276 ifindex);
277 setsockopt(sock, IPPROTO_IP, IP_DROP_MEMBERSHIP, (void *)&mreqn,
278 sizeof(mreqn));
279 ret = setsockopt(sock, IPPROTO_IP, IP_ADD_MEMBERSHIP,
280 (void *)&mreqn, sizeof(mreqn));
281 }
282 return ret;
283
284/* Example defines for another OS, boilerplate off other code in this
285 function, AND handle optname as per other sections for consistency !! */
286/* #elif defined(BOGON_NIX) && EXAMPLE_VERSION_CODE > -100000 */
287/* Add your favourite OS here! */
288
289#elif defined(HAVE_BSD_STRUCT_IP_MREQ_HACK) /* #if OS_TYPE */
290 /* standard BSD API */
291
292 struct ip_mreq mreq;
293 int ret;
294
295 assert(optname == IP_ADD_MEMBERSHIP || optname == IP_DROP_MEMBERSHIP);
296
297
298 memset(&mreq, 0, sizeof(mreq));
299 mreq.imr_multiaddr.s_addr = mcast_addr;
300#if !defined __OpenBSD__
301 mreq.imr_interface.s_addr = htonl(ifindex);
302#else
303 mreq.imr_interface.s_addr = if_addr.s_addr;
304#endif
305
306 ret = setsockopt(sock, IPPROTO_IP, optname, (void *)&mreq,
307 sizeof(mreq));
308 if ((ret < 0) && (optname == IP_ADD_MEMBERSHIP)
309 && (errno == EADDRINUSE)) {
310 /* see above: handle possible problem when interface comes back
311 * up */
312 char buf[1][INET_ADDRSTRLEN];
313 zlog_info(
314 "setsockopt_ipv4_multicast attempting to drop and re-add (fd %d, mcast %s, ifindex %u)",
315 sock, inet_ntop(AF_INET, &mreq.imr_multiaddr, buf[0],
316 sizeof(buf[0])),
317 ifindex);
318 setsockopt(sock, IPPROTO_IP, IP_DROP_MEMBERSHIP, (void *)&mreq,
319 sizeof(mreq));
320 ret = setsockopt(sock, IPPROTO_IP, IP_ADD_MEMBERSHIP,
321 (void *)&mreq, sizeof(mreq));
322 }
323 return ret;
324
325#else
326#error "Unsupported multicast API"
327#endif /* #if OS_TYPE */
328}
329
330/*
331 * Set IP_MULTICAST_IF socket option in an OS-dependent manner.
332 */
333int setsockopt_ipv4_multicast_if(int sock, struct in_addr if_addr,
334 ifindex_t ifindex)
335{
336
337#ifdef HAVE_STRUCT_IP_MREQN_IMR_IFINDEX
338 struct ip_mreqn mreqn;
339 memset(&mreqn, 0, sizeof(mreqn));
340
341 mreqn.imr_ifindex = ifindex;
342 return setsockopt(sock, IPPROTO_IP, IP_MULTICAST_IF, (void *)&mreqn,
343 sizeof(mreqn));
344
345/* Example defines for another OS, boilerplate off other code in this
346 function */
347/* #elif defined(BOGON_NIX) && EXAMPLE_VERSION_CODE > -100000 */
348/* Add your favourite OS here! */
349#elif defined(HAVE_BSD_STRUCT_IP_MREQ_HACK)
350 struct in_addr m;
351
352#if !defined __OpenBSD__
353 m.s_addr = htonl(ifindex);
354#else
355 m.s_addr = if_addr.s_addr;
356#endif
357
358 return setsockopt(sock, IPPROTO_IP, IP_MULTICAST_IF, (void *)&m,
359 sizeof(m));
360#else
361#error "Unsupported multicast API"
362#endif
363}
364
365int setsockopt_ipv4_multicast_loop(int sock, uint8_t val)
366{
367 int ret;
368
369 ret = setsockopt(sock, IPPROTO_IP, IP_MULTICAST_LOOP, (void *)&val,
370 sizeof(val));
371 if (ret < 0)
372 flog_err(EC_LIB_SOCKET, "can't setsockopt IP_MULTICAST_LOOP");
373
374 return ret;
375}
376
377static int setsockopt_ipv4_ifindex(int sock, ifindex_t val)
378{
379 int ret;
380
381#if defined(IP_PKTINFO)
382 ret = setsockopt(sock, IPPROTO_IP, IP_PKTINFO, &val, sizeof(val));
383 if (ret < 0)
384 flog_err(EC_LIB_SOCKET,
385 "Can't set IP_PKTINFO option for fd %d to %d: %s",
386 sock, val, safe_strerror(errno));
387#elif defined(IP_RECVIF)
388 ret = setsockopt(sock, IPPROTO_IP, IP_RECVIF, &val, sizeof(val));
389 if (ret < 0)
390 flog_err(EC_LIB_SOCKET,
391 "Can't set IP_RECVIF option for fd %d to %d: %s", sock,
392 val, safe_strerror(errno));
393#else
394#warning "Neither IP_PKTINFO nor IP_RECVIF is available."
395#warning "Will not be able to receive link info."
396#warning "Things might be seriously broken.."
397 /* XXX Does this ever happen? Should there be a zlog_warn message here?
398 */
399 ret = -1;
400#endif
401 return ret;
402}
403
404int setsockopt_ipv4_tos(int sock, int tos)
405{
406 int ret;
407
408 ret = setsockopt(sock, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
409 if (ret < 0)
410 flog_err(EC_LIB_SOCKET,
411 "Can't set IP_TOS option for fd %d to %#x: %s", sock,
412 tos, safe_strerror(errno));
413 return ret;
414}
415
416
417int setsockopt_ifindex(int af, int sock, ifindex_t val)
418{
419 int ret = -1;
420
421 switch (af) {
422 case AF_INET:
423 ret = setsockopt_ipv4_ifindex(sock, val);
424 break;
425 case AF_INET6:
426 ret = setsockopt_ipv6_pktinfo(sock, val);
427 break;
428 default:
429 flog_err(EC_LIB_DEVELOPMENT,
430 "setsockopt_ifindex: unknown address family %d", af);
431 }
432 return ret;
433}
434
435/*
436 * Requires: msgh is not NULL and points to a valid struct msghdr, which
437 * may or may not have control data about the incoming interface.
438 *
439 * Returns the interface index (small integer >= 1) if it can be
440 * determined, or else 0.
441 */
442static ifindex_t getsockopt_ipv4_ifindex(struct msghdr *msgh)
443{
444 ifindex_t ifindex;
445
446#if defined(IP_PKTINFO)
447 /* Linux pktinfo based ifindex retrieval */
448 struct in_pktinfo *pktinfo;
449
450 pktinfo = (struct in_pktinfo *)getsockopt_cmsg_data(msgh, IPPROTO_IP,
451 IP_PKTINFO);
452
453 /* getsockopt_ifindex() will forward this, being 0 "not found" */
454 if (pktinfo == NULL)
455 return 0;
456
457 ifindex = pktinfo->ipi_ifindex;
458
459#elif defined(IP_RECVIF)
460
461/* retrieval based on IP_RECVIF */
462
463 /* BSD systems use a sockaddr_dl as the control message payload. */
464 struct sockaddr_dl *sdl;
465
466 /* BSD */
467 sdl = (struct sockaddr_dl *)getsockopt_cmsg_data(msgh, IPPROTO_IP,
468 IP_RECVIF);
469 if (sdl != NULL)
470 ifindex = sdl->sdl_index;
471 else
472 ifindex = 0;
473
474#else
475/*
476 * Neither IP_PKTINFO nor IP_RECVIF defined - warn at compile time.
477 * XXX Decide if this is a core service, or if daemons have to cope.
478 * Since Solaris 8 and OpenBSD seem not to provide it, it seems that
479 * daemons have to cope.
480 */
481#warning "getsockopt_ipv4_ifindex: Neither IP_PKTINFO nor IP_RECVIF defined."
482#warning "Some daemons may fail to operate correctly!"
483 ifindex = 0;
484
485#endif /* IP_PKTINFO */
486
487 return ifindex;
488}
489
490/* return ifindex, 0 if none found */
491ifindex_t getsockopt_ifindex(int af, struct msghdr *msgh)
492{
493 switch (af) {
494 case AF_INET:
495 return (getsockopt_ipv4_ifindex(msgh));
496 case AF_INET6:
497 return (getsockopt_ipv6_ifindex(msgh));
498 default:
499 flog_err(EC_LIB_DEVELOPMENT,
500 "getsockopt_ifindex: unknown address family %d", af);
501 return 0;
502 }
503}
504
505/* swab iph between order system uses for IP_HDRINCL and host order */
506void sockopt_iphdrincl_swab_htosys(struct ip *iph)
507{
508/* BSD and derived take iph in network order, except for
509 * ip_len and ip_off
510 */
511#ifndef HAVE_IP_HDRINCL_BSD_ORDER
512 iph->ip_len = htons(iph->ip_len);
513 iph->ip_off = htons(iph->ip_off);
514#endif /* HAVE_IP_HDRINCL_BSD_ORDER */
515
516 iph->ip_id = htons(iph->ip_id);
517}
518
519void sockopt_iphdrincl_swab_systoh(struct ip *iph)
520{
521#ifndef HAVE_IP_HDRINCL_BSD_ORDER
522 iph->ip_len = ntohs(iph->ip_len);
523 iph->ip_off = ntohs(iph->ip_off);
524#endif /* HAVE_IP_HDRINCL_BSD_ORDER */
525
526 iph->ip_id = ntohs(iph->ip_id);
527}
528
529int sockopt_tcp_rtt(int sock)
530{
531#ifdef TCP_INFO
532 struct tcp_info ti;
533 socklen_t len = sizeof(ti);
534
535 if (getsockopt(sock, IPPROTO_TCP, TCP_INFO, &ti, &len) != 0)
536 return 0;
537
538 return ti.tcpi_rtt / 1000;
539#else
540 return 0;
541#endif
542}
543
544int sockopt_tcp_signature_ext(int sock, union sockunion *su, uint16_t prefixlen,
545 const char *password)
546{
547#ifndef HAVE_DECL_TCP_MD5SIG
548 /*
549 * We have been asked to enable MD5 auth for an address, but our
550 * platform doesn't support that
551 */
552 return -2;
553#endif
554
555#ifndef TCP_MD5SIG_EXT
556 /*
557 * We have been asked to enable MD5 auth for a prefix, but our platform
558 * doesn't support that
559 */
560 if (prefixlen > 0)
561 return -2;
562#endif
563
564#if HAVE_DECL_TCP_MD5SIG
565 int ret;
566
567 int optname = TCP_MD5SIG;
568#ifndef GNU_LINUX
569 /*
570 * XXX Need to do PF_KEY operation here to add/remove an SA entry,
571 * and add/remove an SP entry for this peer's packet flows also.
572 */
573 int md5sig = password && *password ? 1 : 0;
574#else
575 int keylen = password ? strlen(password) : 0;
576 struct tcp_md5sig md5sig;
577 union sockunion *su2, *susock;
578
579 /* Figure out whether the socket and the sockunion are the same family..
580 * adding AF_INET to AF_INET6 needs to be v4 mapped, you'd think..
581 */
582 if (!(susock = sockunion_getsockname(sock)))
583 return -1;
584
585 if (susock->sa.sa_family == su->sa.sa_family)
586 su2 = su;
587 else {
588 /* oops.. */
589 su2 = susock;
590
591 if (su2->sa.sa_family == AF_INET) {
592 sockunion_free(susock);
593 return 0;
594 }
595
596 /* If this does not work, then all users of this sockopt will
597 * need to
598 * differentiate between IPv4 and IPv6, and keep separate
599 * sockets for
600 * each.
601 *
602 * Sadly, it doesn't seem to work at present. It's unknown
603 * whether
604 * this is a bug or not.
605 */
606 if (su2->sa.sa_family == AF_INET6
607 && su->sa.sa_family == AF_INET) {
608 su2->sin6.sin6_family = AF_INET6;
609 /* V4Map the address */
610 memset(&su2->sin6.sin6_addr, 0,
611 sizeof(struct in6_addr));
612 su2->sin6.sin6_addr.s6_addr32[2] = htonl(0xffff);
613 memcpy(&su2->sin6.sin6_addr.s6_addr32[3],
614 &su->sin.sin_addr, 4);
615 }
616 }
617
618 memset(&md5sig, 0, sizeof(md5sig));
619 memcpy(&md5sig.tcpm_addr, su2, sizeof(*su2));
620
621 md5sig.tcpm_keylen = keylen;
622 if (keylen)
623 memcpy(md5sig.tcpm_key, password, keylen);
624 sockunion_free(susock);
625
626 /*
627 * Handle support for MD5 signatures on prefixes, if available and
628 * requested. Technically the #ifdef check below is not needed because
629 * if prefixlen > 0 and we don't have support for this feature we would
630 * have already returned by now, but leaving it there to be explicit.
631 */
632#ifdef TCP_MD5SIG_EXT
633 if (prefixlen > 0) {
634 md5sig.tcpm_prefixlen = prefixlen;
635 md5sig.tcpm_flags = TCP_MD5SIG_FLAG_PREFIX;
636 optname = TCP_MD5SIG_EXT;
637 }
638#endif /* TCP_MD5SIG_EXT */
639
640#endif /* GNU_LINUX */
641
642 ret = setsockopt(sock, IPPROTO_TCP, optname, &md5sig, sizeof(md5sig));
643 if (ret < 0) {
644 if (ENOENT == errno)
645 ret = 0;
646 else
647 flog_err_sys(
648 EC_LIB_SYSTEM_CALL,
649 "sockopt_tcp_signature: setsockopt(%d): %s",
650 sock, safe_strerror(errno));
651 }
652 return ret;
653#endif /* HAVE_TCP_MD5SIG */
654
655 /*
656 * Making compiler happy. If we get to this point we probably
657 * have done something really really wrong.
658 */
659 return -2;
660}
661
662int sockopt_tcp_signature(int sock, union sockunion *su, const char *password)
663{
664 return sockopt_tcp_signature_ext(sock, su, 0, password);
665}
666
667/* set TCP mss value to socket */
668int sockopt_tcp_mss_set(int sock, int tcp_maxseg)
669{
670 int ret = 0;
671 socklen_t tcp_maxseg_len = sizeof(tcp_maxseg);
672
673 ret = setsockopt(sock, IPPROTO_TCP, TCP_MAXSEG, &tcp_maxseg,
674 tcp_maxseg_len);
675 if (ret != 0) {
676 flog_err_sys(EC_LIB_SYSTEM_CALL,
677 "%s failed: setsockopt(%d): %s", __func__, sock,
678 safe_strerror(errno));
679 }
680
681 return ret;
682}
683
684/* get TCP mss value synced by socket */
685int sockopt_tcp_mss_get(int sock)
686{
687 int ret = 0;
688 int tcp_maxseg = 0;
689 socklen_t tcp_maxseg_len = sizeof(tcp_maxseg);
690
691 ret = getsockopt(sock, IPPROTO_TCP, TCP_MAXSEG, &tcp_maxseg,
692 &tcp_maxseg_len);
693 if (ret != 0) {
694 flog_err_sys(EC_LIB_SYSTEM_CALL,
695 "%s failed: getsockopt(%d): %s", __func__, sock,
696 safe_strerror(errno));
697 return 0;
698 }
699
700 return tcp_maxseg;
701}