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