blob: 6ddbded132c5430f9e5c6566cbf210f115408555 [file] [log] [blame]
paul718e3742002-12-13 20:15:29 +00001/* 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"
paule6822762004-08-19 04:13:29 +000024#include "sockopt.h"
paul718e3742002-12-13 20:15:29 +000025
paul0b3acf42004-09-17 08:39:08 +000026int
27setsockopt_so_recvbuf (int sock, int size)
28{
29 int ret;
30
paulb89e60c2004-09-21 15:43:13 +000031 if ( (ret = setsockopt (sock, SOL_SOCKET, SO_RCVBUF, (char *)
ajsff29bb32004-11-19 18:29:22 +000032 &size, sizeof (int))) < 0)
33 zlog_err ("fd %d: can't setsockopt SO_RCVBUF to %d: %s",
ajsae5e24d2004-11-19 23:43:10 +000034 sock,size,safe_strerror(errno));
paul0b3acf42004-09-17 08:39:08 +000035
36 return ret;
37}
38
paul4f7baa02004-07-23 15:11:07 +000039static void *
40getsockopt_cmsg_data (struct msghdr *msgh, int level, int type)
41{
42 struct cmsghdr *cmsg;
43 void *ptr = NULL;
44
ajsb99760a2005-01-04 16:24:43 +000045 for (cmsg = ZCMSG_FIRSTHDR(msgh);
paul4f7baa02004-07-23 15:11:07 +000046 cmsg != NULL;
47 cmsg = CMSG_NXTHDR(msgh, cmsg))
48 if (cmsg->cmsg_level == level && cmsg->cmsg_type)
49 return (ptr = CMSG_DATA(cmsg));
paul9035efa2004-10-10 11:56:56 +000050
51 return NULL;
paul4f7baa02004-07-23 15:11:07 +000052}
53
paul718e3742002-12-13 20:15:29 +000054#ifdef HAVE_IPV6
55/* Set IPv6 packet info to the socket. */
56int
57setsockopt_ipv6_pktinfo (int sock, int val)
58{
59 int ret;
60
61#ifdef IPV6_RECVPKTINFO /*2292bis-01*/
62 ret = setsockopt(sock, IPPROTO_IPV6, IPV6_RECVPKTINFO, &val, sizeof(val));
63 if (ret < 0)
ajs6099b3b2004-11-20 02:06:59 +000064 zlog_warn ("can't setsockopt IPV6_RECVPKTINFO : %s", safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +000065#else /*RFC2292*/
66 ret = setsockopt(sock, IPPROTO_IPV6, IPV6_PKTINFO, &val, sizeof(val));
67 if (ret < 0)
ajs6099b3b2004-11-20 02:06:59 +000068 zlog_warn ("can't setsockopt IPV6_PKTINFO : %s", safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +000069#endif /* INIA_IPV6 */
70 return ret;
71}
72
73/* Set multicast hops val to the socket. */
74int
75setsockopt_ipv6_checksum (int sock, int val)
76{
77 int ret;
78
79#ifdef GNU_LINUX
80 ret = setsockopt(sock, IPPROTO_RAW, IPV6_CHECKSUM, &val, sizeof(val));
81#else
82 ret = setsockopt(sock, IPPROTO_IPV6, IPV6_CHECKSUM, &val, sizeof(val));
83#endif /* GNU_LINUX */
84 if (ret < 0)
85 zlog_warn ("can't setsockopt IPV6_CHECKSUM");
86 return ret;
87}
88
89/* Set multicast hops val to the socket. */
90int
91setsockopt_ipv6_multicast_hops (int sock, int val)
92{
93 int ret;
94
95 ret = setsockopt(sock, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &val, sizeof(val));
96 if (ret < 0)
97 zlog_warn ("can't setsockopt IPV6_MULTICAST_HOPS");
98 return ret;
99}
100
101/* Set multicast hops val to the socket. */
102int
103setsockopt_ipv6_unicast_hops (int sock, int val)
104{
105 int ret;
106
107 ret = setsockopt(sock, IPPROTO_IPV6, IPV6_UNICAST_HOPS, &val, sizeof(val));
108 if (ret < 0)
109 zlog_warn ("can't setsockopt IPV6_UNICAST_HOPS");
110 return ret;
111}
112
113int
114setsockopt_ipv6_hoplimit (int sock, int val)
115{
116 int ret;
117
118#ifdef IPV6_RECVHOPLIMIT /*2292bis-01*/
119 ret = setsockopt (sock, IPPROTO_IPV6, IPV6_RECVHOPLIMIT, &val, sizeof(val));
120 if (ret < 0)
121 zlog_warn ("can't setsockopt IPV6_RECVHOPLIMIT");
122#else /*RFC2292*/
123 ret = setsockopt (sock, IPPROTO_IPV6, IPV6_HOPLIMIT, &val, sizeof(val));
124 if (ret < 0)
125 zlog_warn ("can't setsockopt IPV6_HOPLIMIT");
126#endif
127 return ret;
128}
129
130/* Set multicast loop zero to the socket. */
131int
132setsockopt_ipv6_multicast_loop (int sock, int val)
133{
134 int ret;
135
136 ret = setsockopt (sock, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, &val,
137 sizeof (val));
138 if (ret < 0)
139 zlog_warn ("can't setsockopt IPV6_MULTICAST_LOOP");
140 return ret;
141}
142
paul4f7baa02004-07-23 15:11:07 +0000143static int
paule6822762004-08-19 04:13:29 +0000144getsockopt_ipv6_ifindex (struct msghdr *msgh)
paul4f7baa02004-07-23 15:11:07 +0000145{
146 struct in6_pktinfo *pktinfo;
147
148 pktinfo = getsockopt_cmsg_data (msgh, IPPROTO_IPV6, IPV6_PKTINFO);
149
150 return pktinfo->ipi6_ifindex;
151}
paul718e3742002-12-13 20:15:29 +0000152#endif /* HAVE_IPV6 */
153
154
gdtcc49eb52004-12-30 13:50:32 +0000155/*
156 * Process multicast socket options for IPv4 in an OS-dependent manner.
157 * Supported options are IP_MULTICAST_IF and IP_{ADD,DROP}_MEMBERSHIP.
158 *
159 * Many operating systems have a limit on the number of groups that
160 * can be joined per socket (where each group and local address
161 * counts). This impacts OSPF, which joins groups on each interface
162 * using a single socket. The limit is typically 20, derived from the
163 * original BSD multicast implementation. Some systems have
164 * mechanisms for increasing this limit.
ajsc188c372005-10-21 02:57:41 +0000165 *
166 * In many 4.4BSD-derived systems, multicast group operations are not
167 * allowed on interfaces that are not UP. Thus, a previous attempt to
168 * leave the group may have failed, leaving it still joined, and we
169 * drop/join quietly to recover. This may not be necessary, but aims to
170 * defend against unknown behavior in that we will still return an error
171 * if the second join fails. It is not clear how other systems
172 * (e.g. Linux, Solaris) behave when leaving groups on down interfaces,
173 * but this behavior should not be harmful if they behave the same way,
174 * allow leaves, or implicitly leave all groups joined to down interfaces.
gdtcc49eb52004-12-30 13:50:32 +0000175 */
paul718e3742002-12-13 20:15:29 +0000176int
177setsockopt_multicast_ipv4(int sock,
178 int optname,
179 struct in_addr if_addr,
180 unsigned int mcast_addr,
181 unsigned int ifindex)
182{
183
paul42c98192005-05-07 02:22:51 +0000184#ifdef HAVE_STRUCT_IP_MREQN_IMR_IFINDEX
paul718e3742002-12-13 20:15:29 +0000185 /* This is better because it uses ifindex directly */
186 struct ip_mreqn mreqn;
ajsc188c372005-10-21 02:57:41 +0000187 int ret;
paul718e3742002-12-13 20:15:29 +0000188
189 switch (optname)
190 {
191 case IP_MULTICAST_IF:
192 case IP_ADD_MEMBERSHIP:
193 case IP_DROP_MEMBERSHIP:
194 memset (&mreqn, 0, sizeof(mreqn));
195
196 if (mcast_addr)
197 mreqn.imr_multiaddr.s_addr = mcast_addr;
198
199 if (ifindex)
200 mreqn.imr_ifindex = ifindex;
201 else
202 mreqn.imr_address = if_addr;
203
ajsc188c372005-10-21 02:57:41 +0000204 ret = setsockopt(sock, IPPROTO_IP, optname,
205 (void *)&mreqn, sizeof(mreqn));
206 if ((ret < 0) && (optname == IP_ADD_MEMBERSHIP) && (errno == EADDRINUSE))
207 {
208 /* see above: handle possible problem when interface comes back up */
209 char buf[2][INET_ADDRSTRLEN];
210 zlog_info("setsockopt_multicast_ipv4 attempting to drop and "
211 "re-add (fd %d, ifaddr %s, mcast %s, ifindex %u)",
212 sock,
213 inet_ntop(AF_INET, &if_addr, buf[0], sizeof(buf[0])),
214 inet_ntop(AF_INET, &mreqn.imr_multiaddr,
215 buf[1], sizeof(buf[1])), ifindex);
216 setsockopt(sock, IPPROTO_IP, IP_DROP_MEMBERSHIP,
217 (void *)&mreqn, sizeof(mreqn));
218 ret = setsockopt(sock, IPPROTO_IP, IP_ADD_MEMBERSHIP,
219 (void *)&mreqn, sizeof(mreqn));
220 }
221 return ret;
paul718e3742002-12-13 20:15:29 +0000222 break;
223
224 default:
225 /* Can out and give an understandable error */
226 errno = EINVAL;
227 return -1;
228 break;
229 }
230
231 /* Example defines for another OS, boilerplate off other code in this
232 function, AND handle optname as per other sections for consistency !! */
233 /* #elif defined(BOGON_NIX) && EXAMPLE_VERSION_CODE > -100000 */
234 /* Add your favourite OS here! */
235
236#else /* #if OS_TYPE */
gdtcc49eb52004-12-30 13:50:32 +0000237 /* standard BSD API */
paul718e3742002-12-13 20:15:29 +0000238
239 struct in_addr m;
240 struct ip_mreq mreq;
ajsc188c372005-10-21 02:57:41 +0000241 int ret;
paul718e3742002-12-13 20:15:29 +0000242
paul42c98192005-05-07 02:22:51 +0000243#ifdef HAVE_BSD_STRUCT_IP_MREQ_HACK
244 if (ifindex)
245 m.s_addr = htonl(ifindex);
246 else
247#endif
248 m = if_addr;
249
paul718e3742002-12-13 20:15:29 +0000250 switch (optname)
251 {
252 case IP_MULTICAST_IF:
paul718e3742002-12-13 20:15:29 +0000253 return setsockopt (sock, IPPROTO_IP, optname, (void *)&m, sizeof(m));
254 break;
255
256 case IP_ADD_MEMBERSHIP:
257 case IP_DROP_MEMBERSHIP:
258 memset (&mreq, 0, sizeof(mreq));
259 mreq.imr_multiaddr.s_addr = mcast_addr;
paul42c98192005-05-07 02:22:51 +0000260 mreq.imr_interface = m;
paul718e3742002-12-13 20:15:29 +0000261
ajsc188c372005-10-21 02:57:41 +0000262 ret = setsockopt (sock, IPPROTO_IP, optname, (void *)&mreq, sizeof(mreq));
263 if ((ret < 0) && (optname == IP_ADD_MEMBERSHIP) && (errno == EADDRINUSE))
264 {
265 /* see above: handle possible problem when interface comes back up */
266 char buf[2][INET_ADDRSTRLEN];
267 zlog_info("setsockopt_multicast_ipv4 attempting to drop and "
268 "re-add (fd %d, ifaddr %s, mcast %s, ifindex %u)",
269 sock,
270 inet_ntop(AF_INET, &if_addr, buf[0], sizeof(buf[0])),
271 inet_ntop(AF_INET, &mreq.imr_multiaddr,
272 buf[1], sizeof(buf[1])), ifindex);
273 setsockopt (sock, IPPROTO_IP, IP_DROP_MEMBERSHIP,
274 (void *)&mreq, sizeof(mreq));
275 ret = setsockopt (sock, IPPROTO_IP, IP_ADD_MEMBERSHIP,
276 (void *)&mreq, sizeof(mreq));
277 }
278 return ret;
paul718e3742002-12-13 20:15:29 +0000279 break;
280
281 default:
282 /* Can out and give an understandable error */
283 errno = EINVAL;
284 return -1;
285 break;
286 }
287#endif /* #if OS_TYPE */
288
289}
paul4f7baa02004-07-23 15:11:07 +0000290
291static int
paule6822762004-08-19 04:13:29 +0000292setsockopt_ipv4_ifindex (int sock, int val)
paul4f7baa02004-07-23 15:11:07 +0000293{
294 int ret;
295
296#if defined (IP_PKTINFO)
ajs1d75c8c2004-12-28 21:43:17 +0000297 if ((ret = setsockopt (sock, IPPROTO_IP, IP_PKTINFO, &val, sizeof (val))) < 0)
298 zlog_warn ("Can't set IP_PKTINFO option for fd %d to %d: %s",
299 sock,val,safe_strerror(errno));
paul4f7baa02004-07-23 15:11:07 +0000300#elif defined (IP_RECVIF)
ajs1d75c8c2004-12-28 21:43:17 +0000301 if ((ret = setsockopt (sock, IPPROTO_IP, IP_RECVIF, &val, sizeof (val))) < 0)
302 zlog_warn ("Can't set IP_RECVIF option for fd %d to %d: %s",
303 sock,val,safe_strerror(errno));
paul4f7baa02004-07-23 15:11:07 +0000304#else
305#warning "Neither IP_PKTINFO nor IP_RECVIF is available."
306#warning "Will not be able to receive link info."
307#warning "Things might be seriously broken.."
ajs1d75c8c2004-12-28 21:43:17 +0000308 /* XXX Does this ever happen? Should there be a zlog_warn message here? */
309 ret = -1;
paul4f7baa02004-07-23 15:11:07 +0000310#endif
paul4f7baa02004-07-23 15:11:07 +0000311 return ret;
312}
313
paule6822762004-08-19 04:13:29 +0000314int
315setsockopt_ifindex (int af, int sock, int val)
paul4f7baa02004-07-23 15:11:07 +0000316{
paule6822762004-08-19 04:13:29 +0000317 int ret = -1;
318
319 switch (af)
320 {
321 case AF_INET:
322 ret = setsockopt_ipv4_ifindex (sock, val);
323 break;
324#ifdef HAVE_IPV6
325 case AF_INET6:
326 ret = setsockopt_ipv6_pktinfo (sock, val);
327 break;
328#endif
329 default:
hassoe473b032004-09-26 16:08:11 +0000330 zlog_warn ("setsockopt_ifindex: unknown address family %d", af);
paule6822762004-08-19 04:13:29 +0000331 }
332 return ret;
333}
334
gdtd44debe2004-12-29 20:06:23 +0000335/*
336 * Requires: msgh is not NULL and points to a valid struct msghdr, which
337 * may or may not have control data about the incoming interface.
338 *
339 * Returns the interface index (small integer >= 1) if it can be
340 * determined, or else 0.
341 */
paule6822762004-08-19 04:13:29 +0000342static int
343getsockopt_ipv4_ifindex (struct msghdr *msgh)
344{
gdtd44debe2004-12-29 20:06:23 +0000345 /* XXX: initialize to zero? (Always overwritten, so just cosmetic.) */
paule6822762004-08-19 04:13:29 +0000346 int ifindex = -1;
gdt1d69fdf2004-12-29 18:53:30 +0000347
paule6822762004-08-19 04:13:29 +0000348#if defined(IP_PKTINFO)
349/* Linux pktinfo based ifindex retrieval */
paul4f7baa02004-07-23 15:11:07 +0000350 struct in_pktinfo *pktinfo;
paule6822762004-08-19 04:13:29 +0000351
352 pktinfo =
353 (struct in_pktinfo *)getsockopt_cmsg_data (msgh, IPPROTO_IP, IP_PKTINFO);
gdtd44debe2004-12-29 20:06:23 +0000354 /* XXX Can pktinfo be NULL? Clean up post 0.98. */
paule6822762004-08-19 04:13:29 +0000355 ifindex = pktinfo->ipi_ifindex;
356
357#elif defined(IP_RECVIF)
gdtd44debe2004-12-29 20:06:23 +0000358
359 /* retrieval based on IP_RECVIF */
360
paul4f7baa02004-07-23 15:11:07 +0000361#ifndef SUNOS_5
gdtd44debe2004-12-29 20:06:23 +0000362 /* BSD systems use a sockaddr_dl as the control message payload. */
paul4f7baa02004-07-23 15:11:07 +0000363 struct sockaddr_dl *sdl;
gdtd44debe2004-12-29 20:06:23 +0000364#else
365 /* SUNOS_5 uses an integer with the index. */
366 int *ifindex_p;
paul4f7baa02004-07-23 15:11:07 +0000367#endif /* SUNOS_5 */
gdt1d69fdf2004-12-29 18:53:30 +0000368
gdt33f92322004-07-23 16:14:32 +0000369#ifndef SUNOS_5
gdtd44debe2004-12-29 20:06:23 +0000370 /* BSD */
gdt33f92322004-07-23 16:14:32 +0000371 sdl =
paul4f7baa02004-07-23 15:11:07 +0000372 (struct sockaddr_dl *)getsockopt_cmsg_data (msgh, IPPROTO_IP, IP_RECVIF);
gdtd44debe2004-12-29 20:06:23 +0000373 if (sdl != NULL)
374 ifindex = sdl->sdl_index;
375 else
376 ifindex = 0;
377#else
378 /*
379 * Solaris. On Solaris 8, IP_RECVIF is defined, but the call to
380 * enable it fails with errno=99, and the struct msghdr has
381 * controllen 0.
382 */
383 ifindex_p = (uint_t *)getsockopt_cmsg_data (msgh, IPPROTO_IP, IP_RECVIF);
384 if (ifindex_p != NULL)
385 ifindex = *ifindex_p;
386 else
387 ifindex = 0;
paul4f7baa02004-07-23 15:11:07 +0000388#endif /* SUNOS_5 */
paule6822762004-08-19 04:13:29 +0000389
gdtd44debe2004-12-29 20:06:23 +0000390#else
391 /*
392 * Neither IP_PKTINFO nor IP_RECVIF defined - warn at compile time.
393 * XXX Decide if this is a core service, or if daemons have to cope.
394 * Since Solaris 8 and OpenBSD seem not to provide it, it seems that
395 * daemons have to cope.
396 */
397#warning "getsockopt_ipv4_ifindex: Neither IP_PKTINFO nor IP_RECVIF defined."
398#warning "Some daemons may fail to operate correctly!"
paul7d9c6e52004-10-22 10:54:39 +0000399 ifindex = 0;
gdtd44debe2004-12-29 20:06:23 +0000400
paule6822762004-08-19 04:13:29 +0000401#endif /* IP_PKTINFO */
gdtd44debe2004-12-29 20:06:23 +0000402
paul4f7baa02004-07-23 15:11:07 +0000403 return ifindex;
404}
405
406/* return ifindex, 0 if none found */
407int
paule6822762004-08-19 04:13:29 +0000408getsockopt_ifindex (int af, struct msghdr *msgh)
paul4f7baa02004-07-23 15:11:07 +0000409{
410 int ifindex = 0;
411
412 switch (af)
413 {
414 case AF_INET:
paule6822762004-08-19 04:13:29 +0000415 return (getsockopt_ipv4_ifindex (msgh));
paul4f7baa02004-07-23 15:11:07 +0000416 break;
417#ifdef HAVE_IPV6
418 case AF_INET6:
paule6822762004-08-19 04:13:29 +0000419 return (getsockopt_ipv6_ifindex (msgh));
paul4f7baa02004-07-23 15:11:07 +0000420 break;
421#endif
422 default:
hassoe473b032004-09-26 16:08:11 +0000423 zlog_warn ("getsockopt_ifindex: unknown address family %d", af);
paul4f7baa02004-07-23 15:11:07 +0000424 return (ifindex = 0);
425 }
426}
paul96e27c92004-10-05 14:33:43 +0000427
428/* swab iph between order system uses for IP_HDRINCL and host order */
429void
430sockopt_iphdrincl_swab_htosys (struct ip *iph)
431{
432 /* BSD and derived take iph in network order, except for
433 * ip_len and ip_off
434 */
435#ifndef HAVE_IP_HDRINCL_BSD_ORDER
436 iph->ip_len = htons(iph->ip_len);
437 iph->ip_off = htons(iph->ip_off);
438#endif /* HAVE_IP_HDRINCL_BSD_ORDER */
439
440 iph->ip_id = htons(iph->ip_id);
441}
442
443void
444sockopt_iphdrincl_swab_systoh (struct ip *iph)
445{
446#ifndef HAVE_IP_HDRINCL_BSD_ORDER
447 iph->ip_len = ntohs(iph->ip_len);
448 iph->ip_off = ntohs(iph->ip_off);
449#endif /* HAVE_IP_HDRINCL_BSD_ORDER */
450
451 iph->ip_id = ntohs(iph->ip_id);
452}