]> git.proxmox.com Git - mirror_frr.git/blob - lib/sockopt.c
zebra, lib: fix the ZEBRA_INTERFACE_VRF_UPDATE zapi message
[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(EC_LIB_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(EC_LIB_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(EC_LIB_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(EC_LIB_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(EC_LIB_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(EC_LIB_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(EC_LIB_SOCKET, "can't setsockopt IPV6_MULTICAST_HOPS");
133 return ret;
134 }
135
136 /* Set multicast hops val to the socket. */
137 int setsockopt_ipv6_unicast_hops(int sock, int val)
138 {
139 int ret;
140
141 ret = setsockopt(sock, IPPROTO_IPV6, IPV6_UNICAST_HOPS, &val,
142 sizeof(val));
143 if (ret < 0)
144 flog_err(EC_LIB_SOCKET, "can't setsockopt IPV6_UNICAST_HOPS");
145 return ret;
146 }
147
148 int setsockopt_ipv6_hoplimit(int sock, int val)
149 {
150 int ret;
151
152 #ifdef IPV6_RECVHOPLIMIT /*2292bis-01*/
153 ret = setsockopt(sock, IPPROTO_IPV6, IPV6_RECVHOPLIMIT, &val,
154 sizeof(val));
155 if (ret < 0)
156 flog_err(EC_LIB_SOCKET, "can't setsockopt IPV6_RECVHOPLIMIT");
157 #else /*RFC2292*/
158 ret = setsockopt(sock, IPPROTO_IPV6, IPV6_HOPLIMIT, &val, sizeof(val));
159 if (ret < 0)
160 flog_err(EC_LIB_SOCKET, "can't setsockopt IPV6_HOPLIMIT");
161 #endif
162 return ret;
163 }
164
165 /* Set multicast loop zero to the socket. */
166 int setsockopt_ipv6_multicast_loop(int sock, int val)
167 {
168 int ret;
169
170 ret = setsockopt(sock, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, &val,
171 sizeof(val));
172 if (ret < 0)
173 flog_err(EC_LIB_SOCKET, "can't setsockopt IPV6_MULTICAST_LOOP");
174 return ret;
175 }
176
177 static int getsockopt_ipv6_ifindex(struct msghdr *msgh)
178 {
179 struct in6_pktinfo *pktinfo;
180
181 pktinfo = getsockopt_cmsg_data(msgh, IPPROTO_IPV6, IPV6_PKTINFO);
182
183 return pktinfo->ipi6_ifindex;
184 }
185
186 int setsockopt_ipv6_tclass(int sock, int tclass)
187 {
188 int ret = 0;
189
190 #ifdef IPV6_TCLASS /* RFC3542 */
191 ret = setsockopt(sock, IPPROTO_IPV6, IPV6_TCLASS, &tclass,
192 sizeof(tclass));
193 if (ret < 0)
194 flog_err(EC_LIB_SOCKET,
195 "Can't set IPV6_TCLASS option for fd %d to %#x: %s",
196 sock, tclass, safe_strerror(errno));
197 #endif
198 return ret;
199 }
200
201 /*
202 * Process multicast socket options for IPv4 in an OS-dependent manner.
203 * Supported options are IP_{ADD,DROP}_MEMBERSHIP.
204 *
205 * Many operating systems have a limit on the number of groups that
206 * can be joined per socket (where each group and local address
207 * counts). This impacts OSPF, which joins groups on each interface
208 * using a single socket. The limit is typically 20, derived from the
209 * original BSD multicast implementation. Some systems have
210 * mechanisms for increasing this limit.
211 *
212 * In many 4.4BSD-derived systems, multicast group operations are not
213 * allowed on interfaces that are not UP. Thus, a previous attempt to
214 * leave the group may have failed, leaving it still joined, and we
215 * drop/join quietly to recover. This may not be necessary, but aims to
216 * defend against unknown behavior in that we will still return an error
217 * if the second join fails. It is not clear how other systems
218 * (e.g. Linux, Solaris) behave when leaving groups on down interfaces,
219 * but this behavior should not be harmful if they behave the same way,
220 * allow leaves, or implicitly leave all groups joined to down interfaces.
221 */
222 int setsockopt_ipv4_multicast(int sock, int optname, struct in_addr if_addr,
223 unsigned int mcast_addr, ifindex_t ifindex)
224 {
225 #ifdef HAVE_RFC3678
226 struct group_req gr;
227 struct sockaddr_in *si;
228 int ret;
229 memset(&gr, 0, sizeof(gr));
230 si = (struct sockaddr_in *)&gr.gr_group;
231 gr.gr_interface = ifindex;
232 si->sin_family = AF_INET;
233 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
234 si->sin_len = sizeof(struct sockaddr_in);
235 #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
236 si->sin_addr.s_addr = mcast_addr;
237 ret = setsockopt(sock, IPPROTO_IP,
238 (optname == IP_ADD_MEMBERSHIP) ? MCAST_JOIN_GROUP
239 : MCAST_LEAVE_GROUP,
240 (void *)&gr, sizeof(gr));
241 if ((ret < 0) && (optname == IP_ADD_MEMBERSHIP)
242 && (errno == EADDRINUSE)) {
243 setsockopt(sock, IPPROTO_IP, MCAST_LEAVE_GROUP, (void *)&gr,
244 sizeof(gr));
245 ret = setsockopt(sock, IPPROTO_IP, MCAST_JOIN_GROUP,
246 (void *)&gr, sizeof(gr));
247 }
248 return ret;
249
250 #elif defined(HAVE_STRUCT_IP_MREQN_IMR_IFINDEX) && !defined(__FreeBSD__)
251 struct ip_mreqn mreqn;
252 int ret;
253
254 assert(optname == IP_ADD_MEMBERSHIP || optname == IP_DROP_MEMBERSHIP);
255 memset(&mreqn, 0, sizeof(mreqn));
256
257 mreqn.imr_multiaddr.s_addr = mcast_addr;
258 mreqn.imr_ifindex = ifindex;
259
260 ret = setsockopt(sock, IPPROTO_IP, optname, (void *)&mreqn,
261 sizeof(mreqn));
262 if ((ret < 0) && (optname == IP_ADD_MEMBERSHIP)
263 && (errno == EADDRINUSE)) {
264 /* see above: handle possible problem when interface comes back
265 * up */
266 char buf[1][INET_ADDRSTRLEN];
267 zlog_info(
268 "setsockopt_ipv4_multicast attempting to drop and "
269 "re-add (fd %d, mcast %s, ifindex %u)",
270 sock, inet_ntop(AF_INET, &mreqn.imr_multiaddr, buf[0],
271 sizeof(buf[0])),
272 ifindex);
273 setsockopt(sock, IPPROTO_IP, IP_DROP_MEMBERSHIP, (void *)&mreqn,
274 sizeof(mreqn));
275 ret = setsockopt(sock, IPPROTO_IP, IP_ADD_MEMBERSHIP,
276 (void *)&mreqn, sizeof(mreqn));
277 }
278 return ret;
279
280 /* Example defines for another OS, boilerplate off other code in this
281 function, AND handle optname as per other sections for consistency !! */
282 /* #elif defined(BOGON_NIX) && EXAMPLE_VERSION_CODE > -100000 */
283 /* Add your favourite OS here! */
284
285 #elif defined(HAVE_BSD_STRUCT_IP_MREQ_HACK) /* #if OS_TYPE */
286 /* standard BSD API */
287
288 struct ip_mreq mreq;
289 int ret;
290
291 assert(optname == IP_ADD_MEMBERSHIP || optname == IP_DROP_MEMBERSHIP);
292
293
294 memset(&mreq, 0, sizeof(mreq));
295 mreq.imr_multiaddr.s_addr = mcast_addr;
296 #if !defined __OpenBSD__
297 mreq.imr_interface.s_addr = htonl(ifindex);
298 #else
299 mreq.imr_interface.s_addr = if_addr.s_addr;
300 #endif
301
302 ret = setsockopt(sock, IPPROTO_IP, optname, (void *)&mreq,
303 sizeof(mreq));
304 if ((ret < 0) && (optname == IP_ADD_MEMBERSHIP)
305 && (errno == EADDRINUSE)) {
306 /* see above: handle possible problem when interface comes back
307 * up */
308 char buf[1][INET_ADDRSTRLEN];
309 zlog_info(
310 "setsockopt_ipv4_multicast attempting to drop and "
311 "re-add (fd %d, mcast %s, ifindex %u)",
312 sock, inet_ntop(AF_INET, &mreq.imr_multiaddr, buf[0],
313 sizeof(buf[0])),
314 ifindex);
315 setsockopt(sock, IPPROTO_IP, IP_DROP_MEMBERSHIP, (void *)&mreq,
316 sizeof(mreq));
317 ret = setsockopt(sock, IPPROTO_IP, IP_ADD_MEMBERSHIP,
318 (void *)&mreq, sizeof(mreq));
319 }
320 return ret;
321
322 #else
323 #error "Unsupported multicast API"
324 #endif /* #if OS_TYPE */
325 }
326
327 /*
328 * Set IP_MULTICAST_IF socket option in an OS-dependent manner.
329 */
330 int setsockopt_ipv4_multicast_if(int sock, struct in_addr if_addr,
331 ifindex_t ifindex)
332 {
333
334 #ifdef HAVE_STRUCT_IP_MREQN_IMR_IFINDEX
335 struct ip_mreqn mreqn;
336 memset(&mreqn, 0, sizeof(mreqn));
337
338 mreqn.imr_ifindex = ifindex;
339 return setsockopt(sock, IPPROTO_IP, IP_MULTICAST_IF, (void *)&mreqn,
340 sizeof(mreqn));
341
342 /* Example defines for another OS, boilerplate off other code in this
343 function */
344 /* #elif defined(BOGON_NIX) && EXAMPLE_VERSION_CODE > -100000 */
345 /* Add your favourite OS here! */
346 #elif defined(HAVE_BSD_STRUCT_IP_MREQ_HACK)
347 struct in_addr m;
348
349 #if !defined __OpenBSD__
350 m.s_addr = htonl(ifindex);
351 #else
352 m.s_addr = if_addr.s_addr;
353 #endif
354
355 return setsockopt(sock, IPPROTO_IP, IP_MULTICAST_IF, (void *)&m,
356 sizeof(m));
357 #elif defined(SUNOS_5)
358 char ifname[IF_NAMESIZE];
359 struct ifaddrs *ifa, *ifap;
360 struct in_addr ifaddr;
361
362 if (if_indextoname(ifindex, ifname) == NULL)
363 return -1;
364
365 if (getifaddrs(&ifa) != 0)
366 return -1;
367
368 for (ifap = ifa; ifap != NULL; ifap = ifap->ifa_next) {
369 struct sockaddr_in *sa;
370
371 if (strcmp(ifap->ifa_name, ifname) != 0)
372 continue;
373 if (ifap->ifa_addr->sa_family != AF_INET)
374 continue;
375 sa = (struct sockaddr_in *)ifap->ifa_addr;
376 memcpy(&ifaddr, &sa->sin_addr, sizeof(ifaddr));
377 break;
378 }
379
380 freeifaddrs(ifa);
381 if (!ifap) /* This means we did not find an IP */
382 return -1;
383
384 return setsockopt(sock, IPPROTO_IP, IP_MULTICAST_IF, (void *)&ifaddr,
385 sizeof(ifaddr));
386 #else
387 #error "Unsupported multicast API"
388 #endif
389 }
390
391 int setsockopt_ipv4_multicast_loop(int sock, uint8_t val)
392 {
393 int ret;
394
395 ret = setsockopt(sock, IPPROTO_IP, IP_MULTICAST_LOOP, (void *)&val,
396 sizeof(val));
397 if (ret < 0)
398 flog_err(EC_LIB_SOCKET, "can't setsockopt IP_MULTICAST_LOOP");
399
400 return ret;
401 }
402
403 static int setsockopt_ipv4_ifindex(int sock, ifindex_t val)
404 {
405 int ret;
406
407 #if defined(IP_PKTINFO)
408 if ((ret = setsockopt(sock, IPPROTO_IP, IP_PKTINFO, &val, sizeof(val)))
409 < 0)
410 flog_err(EC_LIB_SOCKET,
411 "Can't set IP_PKTINFO option for fd %d to %d: %s",
412 sock, val, safe_strerror(errno));
413 #elif defined(IP_RECVIF)
414 if ((ret = setsockopt(sock, IPPROTO_IP, IP_RECVIF, &val, sizeof(val)))
415 < 0)
416 flog_err(EC_LIB_SOCKET,
417 "Can't set IP_RECVIF option for fd %d to %d: %s", sock,
418 val, safe_strerror(errno));
419 #else
420 #warning "Neither IP_PKTINFO nor IP_RECVIF is available."
421 #warning "Will not be able to receive link info."
422 #warning "Things might be seriously broken.."
423 /* XXX Does this ever happen? Should there be a zlog_warn message here?
424 */
425 ret = -1;
426 #endif
427 return ret;
428 }
429
430 int setsockopt_ipv4_tos(int sock, int tos)
431 {
432 int ret;
433
434 ret = setsockopt(sock, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
435 if (ret < 0)
436 flog_err(EC_LIB_SOCKET,
437 "Can't set IP_TOS option for fd %d to %#x: %s", sock,
438 tos, safe_strerror(errno));
439 return ret;
440 }
441
442
443 int setsockopt_ifindex(int af, int sock, ifindex_t val)
444 {
445 int ret = -1;
446
447 switch (af) {
448 case AF_INET:
449 ret = setsockopt_ipv4_ifindex(sock, val);
450 break;
451 case AF_INET6:
452 ret = setsockopt_ipv6_pktinfo(sock, val);
453 break;
454 default:
455 flog_err(EC_LIB_DEVELOPMENT,
456 "setsockopt_ifindex: unknown address family %d", af);
457 }
458 return ret;
459 }
460
461 /*
462 * Requires: msgh is not NULL and points to a valid struct msghdr, which
463 * may or may not have control data about the incoming interface.
464 *
465 * Returns the interface index (small integer >= 1) if it can be
466 * determined, or else 0.
467 */
468 static ifindex_t getsockopt_ipv4_ifindex(struct msghdr *msgh)
469 {
470 ifindex_t ifindex;
471
472 #if defined(IP_PKTINFO)
473 /* Linux pktinfo based ifindex retrieval */
474 struct in_pktinfo *pktinfo;
475
476 pktinfo = (struct in_pktinfo *)getsockopt_cmsg_data(msgh, IPPROTO_IP,
477 IP_PKTINFO);
478
479 /* getsockopt_ifindex() will forward this, being 0 "not found" */
480 if (pktinfo == NULL)
481 return 0;
482
483 ifindex = pktinfo->ipi_ifindex;
484
485 #elif defined(IP_RECVIF)
486
487 /* retrieval based on IP_RECVIF */
488
489 #ifndef SUNOS_5
490 /* BSD systems use a sockaddr_dl as the control message payload. */
491 struct sockaddr_dl *sdl;
492 #else
493 /* SUNOS_5 uses an integer with the index. */
494 ifindex_t *ifindex_p;
495 #endif /* SUNOS_5 */
496
497 #ifndef SUNOS_5
498 /* BSD */
499 sdl = (struct sockaddr_dl *)getsockopt_cmsg_data(msgh, IPPROTO_IP,
500 IP_RECVIF);
501 if (sdl != NULL)
502 ifindex = sdl->sdl_index;
503 else
504 ifindex = 0;
505 #else
506 /*
507 * Solaris. On Solaris 8, IP_RECVIF is defined, but the call to
508 * enable it fails with errno=99, and the struct msghdr has
509 * controllen 0.
510 */
511 ifindex_p = (uint_t *)getsockopt_cmsg_data(msgh, IPPROTO_IP, IP_RECVIF);
512 if (ifindex_p != NULL)
513 ifindex = *ifindex_p;
514 else
515 ifindex = 0;
516 #endif /* SUNOS_5 */
517
518 #else
519 /*
520 * Neither IP_PKTINFO nor IP_RECVIF defined - warn at compile time.
521 * XXX Decide if this is a core service, or if daemons have to cope.
522 * Since Solaris 8 and OpenBSD seem not to provide it, it seems that
523 * daemons have to cope.
524 */
525 #warning "getsockopt_ipv4_ifindex: Neither IP_PKTINFO nor IP_RECVIF defined."
526 #warning "Some daemons may fail to operate correctly!"
527 ifindex = 0;
528
529 #endif /* IP_PKTINFO */
530
531 return ifindex;
532 }
533
534 /* return ifindex, 0 if none found */
535 ifindex_t getsockopt_ifindex(int af, struct msghdr *msgh)
536 {
537 switch (af) {
538 case AF_INET:
539 return (getsockopt_ipv4_ifindex(msgh));
540 break;
541 case AF_INET6:
542 return (getsockopt_ipv6_ifindex(msgh));
543 break;
544 default:
545 flog_err(EC_LIB_DEVELOPMENT,
546 "getsockopt_ifindex: unknown address family %d", af);
547 return 0;
548 }
549 }
550
551 /* swab iph between order system uses for IP_HDRINCL and host order */
552 void sockopt_iphdrincl_swab_htosys(struct ip *iph)
553 {
554 /* BSD and derived take iph in network order, except for
555 * ip_len and ip_off
556 */
557 #ifndef HAVE_IP_HDRINCL_BSD_ORDER
558 iph->ip_len = htons(iph->ip_len);
559 iph->ip_off = htons(iph->ip_off);
560 #endif /* HAVE_IP_HDRINCL_BSD_ORDER */
561
562 iph->ip_id = htons(iph->ip_id);
563 }
564
565 void sockopt_iphdrincl_swab_systoh(struct ip *iph)
566 {
567 #ifndef HAVE_IP_HDRINCL_BSD_ORDER
568 iph->ip_len = ntohs(iph->ip_len);
569 iph->ip_off = ntohs(iph->ip_off);
570 #endif /* HAVE_IP_HDRINCL_BSD_ORDER */
571
572 iph->ip_id = ntohs(iph->ip_id);
573 }
574
575 int sockopt_tcp_rtt(int sock)
576 {
577 #ifdef TCP_INFO
578 struct tcp_info ti;
579 socklen_t len = sizeof(ti);
580
581 if (getsockopt(sock, IPPROTO_TCP, TCP_INFO, &ti, &len) != 0)
582 return 0;
583
584 return ti.tcpi_rtt / 1000;
585 #else
586 return 0;
587 #endif
588 }
589
590 int sockopt_tcp_signature(int sock, union sockunion *su, const char *password)
591 {
592 #if HAVE_DECL_TCP_MD5SIG
593 int ret;
594 #ifndef GNU_LINUX
595 /*
596 * XXX Need to do PF_KEY operation here to add/remove an SA entry,
597 * and add/remove an SP entry for this peer's packet flows also.
598 */
599 int md5sig = password && *password ? 1 : 0;
600 #else
601 int keylen = password ? strlen(password) : 0;
602 struct tcp_md5sig md5sig;
603 union sockunion *su2, *susock;
604
605 /* Figure out whether the socket and the sockunion are the same family..
606 * adding AF_INET to AF_INET6 needs to be v4 mapped, you'd think..
607 */
608 if (!(susock = sockunion_getsockname(sock)))
609 return -1;
610
611 if (susock->sa.sa_family == su->sa.sa_family)
612 su2 = su;
613 else {
614 /* oops.. */
615 su2 = susock;
616
617 if (su2->sa.sa_family == AF_INET) {
618 sockunion_free(susock);
619 return 0;
620 }
621
622 /* If this does not work, then all users of this sockopt will
623 * need to
624 * differentiate between IPv4 and IPv6, and keep seperate
625 * sockets for
626 * each.
627 *
628 * Sadly, it doesn't seem to work at present. It's unknown
629 * whether
630 * this is a bug or not.
631 */
632 if (su2->sa.sa_family == AF_INET6
633 && su->sa.sa_family == AF_INET) {
634 su2->sin6.sin6_family = AF_INET6;
635 /* V4Map the address */
636 memset(&su2->sin6.sin6_addr, 0,
637 sizeof(struct in6_addr));
638 su2->sin6.sin6_addr.s6_addr32[2] = htonl(0xffff);
639 memcpy(&su2->sin6.sin6_addr.s6_addr32[3],
640 &su->sin.sin_addr, 4);
641 }
642 }
643
644 memset(&md5sig, 0, sizeof(md5sig));
645 memcpy(&md5sig.tcpm_addr, su2, sizeof(*su2));
646 md5sig.tcpm_keylen = keylen;
647 if (keylen)
648 memcpy(md5sig.tcpm_key, password, keylen);
649 sockunion_free(susock);
650 #endif /* GNU_LINUX */
651 if ((ret = setsockopt(sock, IPPROTO_TCP, TCP_MD5SIG, &md5sig,
652 sizeof md5sig))
653 < 0) {
654 /* ENOENT is harmless. It is returned when we clear a password
655 for which
656 one was not previously set. */
657 if (ENOENT == errno)
658 ret = 0;
659 else
660 flog_err_sys(
661 EC_LIB_SYSTEM_CALL,
662 "sockopt_tcp_signature: setsockopt(%d): %s",
663 sock, safe_strerror(errno));
664 }
665 return ret;
666 #else /* HAVE_TCP_MD5SIG */
667 return -2;
668 #endif /* !HAVE_TCP_MD5SIG */
669 }