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