]> git.proxmox.com Git - mirror_frr.git/blob - lib/sockopt.c
Merge pull request #3006 from pacovn/static_analysis__shadow_variables1
[mirror_frr.git] / lib / sockopt.c
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 #ifdef SUNOS_5
24 #include <ifaddrs.h>
25 #endif
26
27 #include "log.h"
28 #include "sockopt.h"
29 #include "sockunion.h"
30 #include "lib_errors.h"
31
32 void setsockopt_so_recvbuf(int sock, int size)
33 {
34 int orig_req = size;
35
36 while (setsockopt(sock, SOL_SOCKET, SO_RCVBUF, &size, sizeof(size))
37 == -1)
38 size /= 2;
39
40 if (size != orig_req)
41 flog_err(LIB_ERR_SOCKET,
42 "%s: fd %d: SO_RCVBUF set to %d (requested %d)",
43 __func__, sock, size, orig_req);
44 }
45
46 void setsockopt_so_sendbuf(const int sock, int size)
47 {
48 int orig_req = size;
49
50 while (setsockopt(sock, SOL_SOCKET, SO_SNDBUF, &size, sizeof(size))
51 == -1)
52 size /= 2;
53
54 if (size != orig_req)
55 flog_err(LIB_ERR_SOCKET,
56 "%s: fd %d: SO_SNDBUF set to %d (requested %d)",
57 __func__, sock, size, orig_req);
58 }
59
60 int getsockopt_so_sendbuf(const int sock)
61 {
62 uint32_t optval;
63 socklen_t optlen = sizeof(optval);
64 int ret = getsockopt(sock, SOL_SOCKET, SO_SNDBUF, (char *)&optval,
65 &optlen);
66 if (ret < 0) {
67 flog_err_sys(LIB_ERR_SYSTEM_CALL,
68 "fd %d: can't getsockopt SO_SNDBUF: %d (%s)", sock,
69 errno, safe_strerror(errno));
70 return ret;
71 }
72 return optval;
73 }
74
75 static void *getsockopt_cmsg_data(struct msghdr *msgh, int level, int type)
76 {
77 struct cmsghdr *cmsg;
78 void *ptr = NULL;
79
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));
84
85 return NULL;
86 }
87
88 /* Set IPv6 packet info to the socket. */
89 int setsockopt_ipv6_pktinfo(int sock, int val)
90 {
91 int ret;
92
93 #ifdef IPV6_RECVPKTINFO /*2292bis-01*/
94 ret = setsockopt(sock, IPPROTO_IPV6, IPV6_RECVPKTINFO, &val,
95 sizeof(val));
96 if (ret < 0)
97 flog_err(LIB_ERR_SOCKET,
98 "can't setsockopt IPV6_RECVPKTINFO : %s",
99 safe_strerror(errno));
100 #else /*RFC2292*/
101 ret = setsockopt(sock, IPPROTO_IPV6, IPV6_PKTINFO, &val, sizeof(val));
102 if (ret < 0)
103 flog_err(LIB_ERR_SOCKET, "can't setsockopt IPV6_PKTINFO : %s",
104 safe_strerror(errno));
105 #endif /* INIA_IPV6 */
106 return ret;
107 }
108
109 /* Set multicast hops val to the socket. */
110 int setsockopt_ipv6_checksum(int sock, int val)
111 {
112 int ret;
113
114 #ifdef GNU_LINUX
115 ret = setsockopt(sock, IPPROTO_RAW, IPV6_CHECKSUM, &val, sizeof(val));
116 #else
117 ret = setsockopt(sock, IPPROTO_IPV6, IPV6_CHECKSUM, &val, sizeof(val));
118 #endif /* GNU_LINUX */
119 if (ret < 0)
120 flog_err(LIB_ERR_SOCKET, "can't setsockopt IPV6_CHECKSUM");
121 return ret;
122 }
123
124 /* Set multicast hops val to the socket. */
125 int setsockopt_ipv6_multicast_hops(int sock, int val)
126 {
127 int ret;
128
129 ret = setsockopt(sock, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &val,
130 sizeof(val));
131 if (ret < 0)
132 flog_err(LIB_ERR_SOCKET,
133 "can't setsockopt IPV6_MULTICAST_HOPS");
134 return ret;
135 }
136
137 /* Set multicast hops val to the socket. */
138 int setsockopt_ipv6_unicast_hops(int sock, int val)
139 {
140 int ret;
141
142 ret = setsockopt(sock, IPPROTO_IPV6, IPV6_UNICAST_HOPS, &val,
143 sizeof(val));
144 if (ret < 0)
145 flog_err(LIB_ERR_SOCKET, "can't setsockopt IPV6_UNICAST_HOPS");
146 return ret;
147 }
148
149 int setsockopt_ipv6_hoplimit(int sock, int val)
150 {
151 int ret;
152
153 #ifdef IPV6_RECVHOPLIMIT /*2292bis-01*/
154 ret = setsockopt(sock, IPPROTO_IPV6, IPV6_RECVHOPLIMIT, &val,
155 sizeof(val));
156 if (ret < 0)
157 flog_err(LIB_ERR_SOCKET, "can't setsockopt IPV6_RECVHOPLIMIT");
158 #else /*RFC2292*/
159 ret = setsockopt(sock, IPPROTO_IPV6, IPV6_HOPLIMIT, &val, sizeof(val));
160 if (ret < 0)
161 flog_err(LIB_ERR_SOCKET, "can't setsockopt IPV6_HOPLIMIT");
162 #endif
163 return ret;
164 }
165
166 /* Set multicast loop zero to the socket. */
167 int setsockopt_ipv6_multicast_loop(int sock, int val)
168 {
169 int ret;
170
171 ret = setsockopt(sock, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, &val,
172 sizeof(val));
173 if (ret < 0)
174 flog_err(LIB_ERR_SOCKET,
175 "can't setsockopt IPV6_MULTICAST_LOOP");
176 return ret;
177 }
178
179 static int getsockopt_ipv6_ifindex(struct msghdr *msgh)
180 {
181 struct in6_pktinfo *pktinfo;
182
183 pktinfo = getsockopt_cmsg_data(msgh, IPPROTO_IPV6, IPV6_PKTINFO);
184
185 return pktinfo->ipi6_ifindex;
186 }
187
188 int setsockopt_ipv6_tclass(int sock, int tclass)
189 {
190 int ret = 0;
191
192 #ifdef IPV6_TCLASS /* RFC3542 */
193 ret = setsockopt(sock, IPPROTO_IPV6, IPV6_TCLASS, &tclass,
194 sizeof(tclass));
195 if (ret < 0)
196 flog_err(LIB_ERR_SOCKET,
197 "Can't set IPV6_TCLASS option for fd %d to %#x: %s",
198 sock, tclass, safe_strerror(errno));
199 #endif
200 return ret;
201 }
202
203 /*
204 * Process multicast socket options for IPv4 in an OS-dependent manner.
205 * Supported options are IP_{ADD,DROP}_MEMBERSHIP.
206 *
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.
213 *
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.
223 */
224 int setsockopt_ipv4_multicast(int sock, int optname, struct in_addr if_addr,
225 unsigned int mcast_addr, ifindex_t ifindex)
226 {
227 #ifdef HAVE_RFC3678
228 struct group_req gr;
229 struct sockaddr_in *si;
230 int ret;
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
241 : MCAST_LEAVE_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,
246 sizeof(gr));
247 ret = setsockopt(sock, IPPROTO_IP, MCAST_JOIN_GROUP,
248 (void *)&gr, sizeof(gr));
249 }
250 return ret;
251
252 #elif defined(HAVE_STRUCT_IP_MREQN_IMR_IFINDEX) && !defined(__FreeBSD__)
253 struct ip_mreqn mreqn;
254 int ret;
255
256 assert(optname == IP_ADD_MEMBERSHIP || optname == IP_DROP_MEMBERSHIP);
257 memset(&mreqn, 0, sizeof(mreqn));
258
259 mreqn.imr_multiaddr.s_addr = mcast_addr;
260 mreqn.imr_ifindex = ifindex;
261
262 ret = setsockopt(sock, IPPROTO_IP, optname, (void *)&mreqn,
263 sizeof(mreqn));
264 if ((ret < 0) && (optname == IP_ADD_MEMBERSHIP)
265 && (errno == EADDRINUSE)) {
266 /* see above: handle possible problem when interface comes back
267 * up */
268 char buf[1][INET_ADDRSTRLEN];
269 zlog_info(
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],
273 sizeof(buf[0])),
274 ifindex);
275 setsockopt(sock, IPPROTO_IP, IP_DROP_MEMBERSHIP, (void *)&mreqn,
276 sizeof(mreqn));
277 ret = setsockopt(sock, IPPROTO_IP, IP_ADD_MEMBERSHIP,
278 (void *)&mreqn, sizeof(mreqn));
279 }
280 return ret;
281
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! */
286
287 #elif defined(HAVE_BSD_STRUCT_IP_MREQ_HACK) /* #if OS_TYPE */
288 /* standard BSD API */
289
290 struct ip_mreq mreq;
291 int ret;
292
293 assert(optname == IP_ADD_MEMBERSHIP || optname == IP_DROP_MEMBERSHIP);
294
295
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);
300 #else
301 mreq.imr_interface.s_addr = if_addr.s_addr;
302 #endif
303
304 ret = setsockopt(sock, IPPROTO_IP, optname, (void *)&mreq,
305 sizeof(mreq));
306 if ((ret < 0) && (optname == IP_ADD_MEMBERSHIP)
307 && (errno == EADDRINUSE)) {
308 /* see above: handle possible problem when interface comes back
309 * up */
310 char buf[1][INET_ADDRSTRLEN];
311 zlog_info(
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],
315 sizeof(buf[0])),
316 ifindex);
317 setsockopt(sock, IPPROTO_IP, IP_DROP_MEMBERSHIP, (void *)&mreq,
318 sizeof(mreq));
319 ret = setsockopt(sock, IPPROTO_IP, IP_ADD_MEMBERSHIP,
320 (void *)&mreq, sizeof(mreq));
321 }
322 return ret;
323
324 #else
325 #error "Unsupported multicast API"
326 #endif /* #if OS_TYPE */
327 }
328
329 /*
330 * Set IP_MULTICAST_IF socket option in an OS-dependent manner.
331 */
332 int setsockopt_ipv4_multicast_if(int sock, struct in_addr if_addr,
333 ifindex_t ifindex)
334 {
335
336 #ifdef HAVE_STRUCT_IP_MREQN_IMR_IFINDEX
337 struct ip_mreqn mreqn;
338 memset(&mreqn, 0, sizeof(mreqn));
339
340 mreqn.imr_ifindex = ifindex;
341 return setsockopt(sock, IPPROTO_IP, IP_MULTICAST_IF, (void *)&mreqn,
342 sizeof(mreqn));
343
344 /* Example defines for another OS, boilerplate off other code in this
345 function */
346 /* #elif defined(BOGON_NIX) && EXAMPLE_VERSION_CODE > -100000 */
347 /* Add your favourite OS here! */
348 #elif defined(HAVE_BSD_STRUCT_IP_MREQ_HACK)
349 struct in_addr m;
350
351 #if !defined __OpenBSD__
352 m.s_addr = htonl(ifindex);
353 #else
354 m.s_addr = if_addr.s_addr;
355 #endif
356
357 return setsockopt(sock, IPPROTO_IP, IP_MULTICAST_IF, (void *)&m,
358 sizeof(m));
359 #elif defined(SUNOS_5)
360 char ifname[IF_NAMESIZE];
361 struct ifaddrs *ifa, *ifap;
362 struct in_addr ifaddr;
363
364 if (if_indextoname(ifindex, ifname) == NULL)
365 return -1;
366
367 if (getifaddrs(&ifa) != 0)
368 return -1;
369
370 for (ifap = ifa; ifap != NULL; ifap = ifap->ifa_next) {
371 struct sockaddr_in *sa;
372
373 if (strcmp(ifap->ifa_name, ifname) != 0)
374 continue;
375 if (ifap->ifa_addr->sa_family != AF_INET)
376 continue;
377 sa = (struct sockaddr_in *)ifap->ifa_addr;
378 memcpy(&ifaddr, &sa->sin_addr, sizeof(ifaddr));
379 break;
380 }
381
382 freeifaddrs(ifa);
383 if (!ifap) /* This means we did not find an IP */
384 return -1;
385
386 return setsockopt(sock, IPPROTO_IP, IP_MULTICAST_IF, (void *)&ifaddr,
387 sizeof(ifaddr));
388 #else
389 #error "Unsupported multicast API"
390 #endif
391 }
392
393 int setsockopt_ipv4_multicast_loop(int sock, uint8_t val)
394 {
395 int ret;
396
397 ret = setsockopt(sock, IPPROTO_IP, IP_MULTICAST_LOOP, (void *)&val,
398 sizeof(val));
399 if (ret < 0)
400 flog_err(LIB_ERR_SOCKET, "can't setsockopt IP_MULTICAST_LOOP");
401
402 return ret;
403 }
404
405 static int setsockopt_ipv4_ifindex(int sock, ifindex_t val)
406 {
407 int ret;
408
409 #if defined(IP_PKTINFO)
410 if ((ret = setsockopt(sock, IPPROTO_IP, IP_PKTINFO, &val, sizeof(val)))
411 < 0)
412 flog_err(LIB_ERR_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)))
417 < 0)
418 flog_err(LIB_ERR_SOCKET,
419 "Can't set IP_RECVIF option for fd %d to %d: %s", sock,
420 val, safe_strerror(errno));
421 #else
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?
426 */
427 ret = -1;
428 #endif
429 return ret;
430 }
431
432 int setsockopt_ipv4_tos(int sock, int tos)
433 {
434 int ret;
435
436 ret = setsockopt(sock, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
437 if (ret < 0)
438 flog_err(LIB_ERR_SOCKET,
439 "Can't set IP_TOS option for fd %d to %#x: %s", sock,
440 tos, safe_strerror(errno));
441 return ret;
442 }
443
444
445 int setsockopt_ifindex(int af, int sock, ifindex_t val)
446 {
447 int ret = -1;
448
449 switch (af) {
450 case AF_INET:
451 ret = setsockopt_ipv4_ifindex(sock, val);
452 break;
453 case AF_INET6:
454 ret = setsockopt_ipv6_pktinfo(sock, val);
455 break;
456 default:
457 flog_err(LIB_ERR_DEVELOPMENT,
458 "setsockopt_ifindex: unknown address family %d", af);
459 }
460 return ret;
461 }
462
463 /*
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.
466 *
467 * Returns the interface index (small integer >= 1) if it can be
468 * determined, or else 0.
469 */
470 static ifindex_t getsockopt_ipv4_ifindex(struct msghdr *msgh)
471 {
472 ifindex_t ifindex;
473
474 #if defined(IP_PKTINFO)
475 /* Linux pktinfo based ifindex retrieval */
476 struct in_pktinfo *pktinfo;
477
478 pktinfo = (struct in_pktinfo *)getsockopt_cmsg_data(msgh, IPPROTO_IP,
479 IP_PKTINFO);
480
481 /* getsockopt_ifindex() will forward this, being 0 "not found" */
482 if (pktinfo == NULL)
483 return 0;
484
485 ifindex = pktinfo->ipi_ifindex;
486
487 #elif defined(IP_RECVIF)
488
489 /* retrieval based on IP_RECVIF */
490
491 #ifndef SUNOS_5
492 /* BSD systems use a sockaddr_dl as the control message payload. */
493 struct sockaddr_dl *sdl;
494 #else
495 /* SUNOS_5 uses an integer with the index. */
496 ifindex_t *ifindex_p;
497 #endif /* SUNOS_5 */
498
499 #ifndef SUNOS_5
500 /* BSD */
501 sdl = (struct sockaddr_dl *)getsockopt_cmsg_data(msgh, IPPROTO_IP,
502 IP_RECVIF);
503 if (sdl != NULL)
504 ifindex = sdl->sdl_index;
505 else
506 ifindex = 0;
507 #else
508 /*
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
511 * controllen 0.
512 */
513 ifindex_p = (uint_t *)getsockopt_cmsg_data(msgh, IPPROTO_IP, IP_RECVIF);
514 if (ifindex_p != NULL)
515 ifindex = *ifindex_p;
516 else
517 ifindex = 0;
518 #endif /* SUNOS_5 */
519
520 #else
521 /*
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.
526 */
527 #warning "getsockopt_ipv4_ifindex: Neither IP_PKTINFO nor IP_RECVIF defined."
528 #warning "Some daemons may fail to operate correctly!"
529 ifindex = 0;
530
531 #endif /* IP_PKTINFO */
532
533 return ifindex;
534 }
535
536 /* return ifindex, 0 if none found */
537 ifindex_t getsockopt_ifindex(int af, struct msghdr *msgh)
538 {
539 switch (af) {
540 case AF_INET:
541 return (getsockopt_ipv4_ifindex(msgh));
542 break;
543 case AF_INET6:
544 return (getsockopt_ipv6_ifindex(msgh));
545 break;
546 default:
547 flog_err(LIB_ERR_DEVELOPMENT,
548 "getsockopt_ifindex: unknown address family %d", af);
549 return 0;
550 }
551 }
552
553 /* swab iph between order system uses for IP_HDRINCL and host order */
554 void sockopt_iphdrincl_swab_htosys(struct ip *iph)
555 {
556 /* BSD and derived take iph in network order, except for
557 * ip_len and ip_off
558 */
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 */
563
564 iph->ip_id = htons(iph->ip_id);
565 }
566
567 void sockopt_iphdrincl_swab_systoh(struct ip *iph)
568 {
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 */
573
574 iph->ip_id = ntohs(iph->ip_id);
575 }
576
577 int sockopt_tcp_rtt(int sock)
578 {
579 #ifdef TCP_INFO
580 struct tcp_info ti;
581 socklen_t len = sizeof(ti);
582
583 if (getsockopt(sock, IPPROTO_TCP, TCP_INFO, &ti, &len) != 0)
584 return 0;
585
586 return ti.tcpi_rtt / 1000;
587 #else
588 return 0;
589 #endif
590 }
591
592 int sockopt_tcp_signature(int sock, union sockunion *su, const char *password)
593 {
594 #if HAVE_DECL_TCP_MD5SIG
595 int ret;
596 #ifndef GNU_LINUX
597 /*
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.
600 */
601 int md5sig = password && *password ? 1 : 0;
602 #else
603 int keylen = password ? strlen(password) : 0;
604 struct tcp_md5sig md5sig;
605 union sockunion *su2, *susock;
606
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..
609 */
610 if (!(susock = sockunion_getsockname(sock)))
611 return -1;
612
613 if (susock->sa.sa_family == su->sa.sa_family)
614 su2 = su;
615 else {
616 /* oops.. */
617 su2 = susock;
618
619 if (su2->sa.sa_family == AF_INET) {
620 sockunion_free(susock);
621 return 0;
622 }
623
624 /* If this does not work, then all users of this sockopt will
625 * need to
626 * differentiate between IPv4 and IPv6, and keep seperate
627 * sockets for
628 * each.
629 *
630 * Sadly, it doesn't seem to work at present. It's unknown
631 * whether
632 * this is a bug or not.
633 */
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);
643 }
644 }
645
646 memset(&md5sig, 0, sizeof(md5sig));
647 memcpy(&md5sig.tcpm_addr, su2, sizeof(*su2));
648 md5sig.tcpm_keylen = keylen;
649 if (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,
654 sizeof md5sig))
655 < 0) {
656 /* ENOENT is harmless. It is returned when we clear a password
657 for which
658 one was not previously set. */
659 if (ENOENT == errno)
660 ret = 0;
661 else
662 flog_err_sys(
663 LIB_ERR_SYSTEM_CALL,
664 "sockopt_tcp_signature: setsockopt(%d): %s",
665 sock, safe_strerror(errno));
666 }
667 return ret;
668 #else /* HAVE_TCP_MD5SIG */
669 return -2;
670 #endif /* !HAVE_TCP_MD5SIG */
671 }