blob: 655ecdc06ed7273245bd0f5e8090eff8f47a86e2 [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.
165 */
paul718e3742002-12-13 20:15:29 +0000166int
167setsockopt_multicast_ipv4(int sock,
168 int optname,
169 struct in_addr if_addr,
170 unsigned int mcast_addr,
171 unsigned int ifindex)
172{
173
paul42c98192005-05-07 02:22:51 +0000174#ifdef HAVE_STRUCT_IP_MREQN_IMR_IFINDEX
paul718e3742002-12-13 20:15:29 +0000175 /* This is better because it uses ifindex directly */
176 struct ip_mreqn mreqn;
177
178 switch (optname)
179 {
180 case IP_MULTICAST_IF:
181 case IP_ADD_MEMBERSHIP:
182 case IP_DROP_MEMBERSHIP:
183 memset (&mreqn, 0, sizeof(mreqn));
184
185 if (mcast_addr)
186 mreqn.imr_multiaddr.s_addr = mcast_addr;
187
188 if (ifindex)
189 mreqn.imr_ifindex = ifindex;
190 else
191 mreqn.imr_address = if_addr;
192
193 return setsockopt(sock, IPPROTO_IP, optname, (void *)&mreqn, sizeof(mreqn));
194 break;
195
196 default:
197 /* Can out and give an understandable error */
198 errno = EINVAL;
199 return -1;
200 break;
201 }
202
203 /* Example defines for another OS, boilerplate off other code in this
204 function, AND handle optname as per other sections for consistency !! */
205 /* #elif defined(BOGON_NIX) && EXAMPLE_VERSION_CODE > -100000 */
206 /* Add your favourite OS here! */
207
208#else /* #if OS_TYPE */
gdtcc49eb52004-12-30 13:50:32 +0000209 /* standard BSD API */
paul718e3742002-12-13 20:15:29 +0000210
211 struct in_addr m;
212 struct ip_mreq mreq;
213
paul42c98192005-05-07 02:22:51 +0000214#ifdef HAVE_BSD_STRUCT_IP_MREQ_HACK
215 if (ifindex)
216 m.s_addr = htonl(ifindex);
217 else
218#endif
219 m = if_addr;
220
paul718e3742002-12-13 20:15:29 +0000221 switch (optname)
222 {
223 case IP_MULTICAST_IF:
paul718e3742002-12-13 20:15:29 +0000224 return setsockopt (sock, IPPROTO_IP, optname, (void *)&m, sizeof(m));
225 break;
226
227 case IP_ADD_MEMBERSHIP:
228 case IP_DROP_MEMBERSHIP:
229 memset (&mreq, 0, sizeof(mreq));
230 mreq.imr_multiaddr.s_addr = mcast_addr;
paul42c98192005-05-07 02:22:51 +0000231 mreq.imr_interface = m;
paul718e3742002-12-13 20:15:29 +0000232
233 return setsockopt (sock,
234 IPPROTO_IP,
235 optname,
236 (void *)&mreq,
237 sizeof(mreq));
238 break;
239
240 default:
241 /* Can out and give an understandable error */
242 errno = EINVAL;
243 return -1;
244 break;
245 }
246#endif /* #if OS_TYPE */
247
248}
paul4f7baa02004-07-23 15:11:07 +0000249
250static int
paule6822762004-08-19 04:13:29 +0000251setsockopt_ipv4_ifindex (int sock, int val)
paul4f7baa02004-07-23 15:11:07 +0000252{
253 int ret;
254
255#if defined (IP_PKTINFO)
ajs1d75c8c2004-12-28 21:43:17 +0000256 if ((ret = setsockopt (sock, IPPROTO_IP, IP_PKTINFO, &val, sizeof (val))) < 0)
257 zlog_warn ("Can't set IP_PKTINFO option for fd %d to %d: %s",
258 sock,val,safe_strerror(errno));
paul4f7baa02004-07-23 15:11:07 +0000259#elif defined (IP_RECVIF)
ajs1d75c8c2004-12-28 21:43:17 +0000260 if ((ret = setsockopt (sock, IPPROTO_IP, IP_RECVIF, &val, sizeof (val))) < 0)
261 zlog_warn ("Can't set IP_RECVIF option for fd %d to %d: %s",
262 sock,val,safe_strerror(errno));
paul4f7baa02004-07-23 15:11:07 +0000263#else
264#warning "Neither IP_PKTINFO nor IP_RECVIF is available."
265#warning "Will not be able to receive link info."
266#warning "Things might be seriously broken.."
ajs1d75c8c2004-12-28 21:43:17 +0000267 /* XXX Does this ever happen? Should there be a zlog_warn message here? */
268 ret = -1;
paul4f7baa02004-07-23 15:11:07 +0000269#endif
paul4f7baa02004-07-23 15:11:07 +0000270 return ret;
271}
272
paule6822762004-08-19 04:13:29 +0000273int
274setsockopt_ifindex (int af, int sock, int val)
paul4f7baa02004-07-23 15:11:07 +0000275{
paule6822762004-08-19 04:13:29 +0000276 int ret = -1;
277
278 switch (af)
279 {
280 case AF_INET:
281 ret = setsockopt_ipv4_ifindex (sock, val);
282 break;
283#ifdef HAVE_IPV6
284 case AF_INET6:
285 ret = setsockopt_ipv6_pktinfo (sock, val);
286 break;
287#endif
288 default:
hassoe473b032004-09-26 16:08:11 +0000289 zlog_warn ("setsockopt_ifindex: unknown address family %d", af);
paule6822762004-08-19 04:13:29 +0000290 }
291 return ret;
292}
293
gdtd44debe2004-12-29 20:06:23 +0000294/*
295 * Requires: msgh is not NULL and points to a valid struct msghdr, which
296 * may or may not have control data about the incoming interface.
297 *
298 * Returns the interface index (small integer >= 1) if it can be
299 * determined, or else 0.
300 */
paule6822762004-08-19 04:13:29 +0000301static int
302getsockopt_ipv4_ifindex (struct msghdr *msgh)
303{
gdtd44debe2004-12-29 20:06:23 +0000304 /* XXX: initialize to zero? (Always overwritten, so just cosmetic.) */
paule6822762004-08-19 04:13:29 +0000305 int ifindex = -1;
gdt1d69fdf2004-12-29 18:53:30 +0000306
paule6822762004-08-19 04:13:29 +0000307#if defined(IP_PKTINFO)
308/* Linux pktinfo based ifindex retrieval */
paul4f7baa02004-07-23 15:11:07 +0000309 struct in_pktinfo *pktinfo;
paule6822762004-08-19 04:13:29 +0000310
311 pktinfo =
312 (struct in_pktinfo *)getsockopt_cmsg_data (msgh, IPPROTO_IP, IP_PKTINFO);
gdtd44debe2004-12-29 20:06:23 +0000313 /* XXX Can pktinfo be NULL? Clean up post 0.98. */
paule6822762004-08-19 04:13:29 +0000314 ifindex = pktinfo->ipi_ifindex;
315
316#elif defined(IP_RECVIF)
gdtd44debe2004-12-29 20:06:23 +0000317
318 /* retrieval based on IP_RECVIF */
319
paul4f7baa02004-07-23 15:11:07 +0000320#ifndef SUNOS_5
gdtd44debe2004-12-29 20:06:23 +0000321 /* BSD systems use a sockaddr_dl as the control message payload. */
paul4f7baa02004-07-23 15:11:07 +0000322 struct sockaddr_dl *sdl;
gdtd44debe2004-12-29 20:06:23 +0000323#else
324 /* SUNOS_5 uses an integer with the index. */
325 int *ifindex_p;
paul4f7baa02004-07-23 15:11:07 +0000326#endif /* SUNOS_5 */
gdt1d69fdf2004-12-29 18:53:30 +0000327
gdt33f92322004-07-23 16:14:32 +0000328#ifndef SUNOS_5
gdtd44debe2004-12-29 20:06:23 +0000329 /* BSD */
gdt33f92322004-07-23 16:14:32 +0000330 sdl =
paul4f7baa02004-07-23 15:11:07 +0000331 (struct sockaddr_dl *)getsockopt_cmsg_data (msgh, IPPROTO_IP, IP_RECVIF);
gdtd44debe2004-12-29 20:06:23 +0000332 if (sdl != NULL)
333 ifindex = sdl->sdl_index;
334 else
335 ifindex = 0;
336#else
337 /*
338 * Solaris. On Solaris 8, IP_RECVIF is defined, but the call to
339 * enable it fails with errno=99, and the struct msghdr has
340 * controllen 0.
341 */
342 ifindex_p = (uint_t *)getsockopt_cmsg_data (msgh, IPPROTO_IP, IP_RECVIF);
343 if (ifindex_p != NULL)
344 ifindex = *ifindex_p;
345 else
346 ifindex = 0;
paul4f7baa02004-07-23 15:11:07 +0000347#endif /* SUNOS_5 */
paule6822762004-08-19 04:13:29 +0000348
gdtd44debe2004-12-29 20:06:23 +0000349#else
350 /*
351 * Neither IP_PKTINFO nor IP_RECVIF defined - warn at compile time.
352 * XXX Decide if this is a core service, or if daemons have to cope.
353 * Since Solaris 8 and OpenBSD seem not to provide it, it seems that
354 * daemons have to cope.
355 */
356#warning "getsockopt_ipv4_ifindex: Neither IP_PKTINFO nor IP_RECVIF defined."
357#warning "Some daemons may fail to operate correctly!"
paul7d9c6e52004-10-22 10:54:39 +0000358 ifindex = 0;
gdtd44debe2004-12-29 20:06:23 +0000359
paule6822762004-08-19 04:13:29 +0000360#endif /* IP_PKTINFO */
gdtd44debe2004-12-29 20:06:23 +0000361
paul4f7baa02004-07-23 15:11:07 +0000362 return ifindex;
363}
364
365/* return ifindex, 0 if none found */
366int
paule6822762004-08-19 04:13:29 +0000367getsockopt_ifindex (int af, struct msghdr *msgh)
paul4f7baa02004-07-23 15:11:07 +0000368{
369 int ifindex = 0;
370
371 switch (af)
372 {
373 case AF_INET:
paule6822762004-08-19 04:13:29 +0000374 return (getsockopt_ipv4_ifindex (msgh));
paul4f7baa02004-07-23 15:11:07 +0000375 break;
376#ifdef HAVE_IPV6
377 case AF_INET6:
paule6822762004-08-19 04:13:29 +0000378 return (getsockopt_ipv6_ifindex (msgh));
paul4f7baa02004-07-23 15:11:07 +0000379 break;
380#endif
381 default:
hassoe473b032004-09-26 16:08:11 +0000382 zlog_warn ("getsockopt_ifindex: unknown address family %d", af);
paul4f7baa02004-07-23 15:11:07 +0000383 return (ifindex = 0);
384 }
385}
paul96e27c92004-10-05 14:33:43 +0000386
387/* swab iph between order system uses for IP_HDRINCL and host order */
388void
389sockopt_iphdrincl_swab_htosys (struct ip *iph)
390{
391 /* BSD and derived take iph in network order, except for
392 * ip_len and ip_off
393 */
394#ifndef HAVE_IP_HDRINCL_BSD_ORDER
395 iph->ip_len = htons(iph->ip_len);
396 iph->ip_off = htons(iph->ip_off);
397#endif /* HAVE_IP_HDRINCL_BSD_ORDER */
398
399 iph->ip_id = htons(iph->ip_id);
400}
401
402void
403sockopt_iphdrincl_swab_systoh (struct ip *iph)
404{
405#ifndef HAVE_IP_HDRINCL_BSD_ORDER
406 iph->ip_len = ntohs(iph->ip_len);
407 iph->ip_off = ntohs(iph->ip_off);
408#endif /* HAVE_IP_HDRINCL_BSD_ORDER */
409
410 iph->ip_id = ntohs(iph->ip_id);
411}