blob: 8a1eec1274545d46496851ae99aedf5c2eeb694c [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"
Paul Jakma0df7c912008-07-21 21:02:49 +000025#include "sockunion.h"
paul718e3742002-12-13 20:15:29 +000026
paul0b3acf42004-09-17 08:39:08 +000027int
28setsockopt_so_recvbuf (int sock, int size)
29{
30 int ret;
31
paulb89e60c2004-09-21 15:43:13 +000032 if ( (ret = setsockopt (sock, SOL_SOCKET, SO_RCVBUF, (char *)
ajsff29bb32004-11-19 18:29:22 +000033 &size, sizeof (int))) < 0)
34 zlog_err ("fd %d: can't setsockopt SO_RCVBUF to %d: %s",
ajsae5e24d2004-11-19 23:43:10 +000035 sock,size,safe_strerror(errno));
paul0b3acf42004-09-17 08:39:08 +000036
37 return ret;
38}
39
Denis Ovsienkob7fe4142007-08-21 16:32:56 +000040int
41setsockopt_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
53int
54getsockopt_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
paul4f7baa02004-07-23 15:11:07 +000069static void *
70getsockopt_cmsg_data (struct msghdr *msgh, int level, int type)
71{
72 struct cmsghdr *cmsg;
73 void *ptr = NULL;
74
ajsb99760a2005-01-04 16:24:43 +000075 for (cmsg = ZCMSG_FIRSTHDR(msgh);
paul4f7baa02004-07-23 15:11:07 +000076 cmsg != NULL;
77 cmsg = CMSG_NXTHDR(msgh, cmsg))
78 if (cmsg->cmsg_level == level && cmsg->cmsg_type)
79 return (ptr = CMSG_DATA(cmsg));
paul9035efa2004-10-10 11:56:56 +000080
81 return NULL;
paul4f7baa02004-07-23 15:11:07 +000082}
83
paul718e3742002-12-13 20:15:29 +000084#ifdef HAVE_IPV6
85/* Set IPv6 packet info to the socket. */
86int
87setsockopt_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)
ajs6099b3b2004-11-20 02:06:59 +000094 zlog_warn ("can't setsockopt IPV6_RECVPKTINFO : %s", safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +000095#else /*RFC2292*/
96 ret = setsockopt(sock, IPPROTO_IPV6, IPV6_PKTINFO, &val, sizeof(val));
97 if (ret < 0)
ajs6099b3b2004-11-20 02:06:59 +000098 zlog_warn ("can't setsockopt IPV6_PKTINFO : %s", safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +000099#endif /* INIA_IPV6 */
100 return ret;
101}
102
103/* Set multicast hops val to the socket. */
104int
105setsockopt_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. */
120int
121setsockopt_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. */
132int
133setsockopt_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
143int
144setsockopt_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. */
161int
162setsockopt_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
paul4f7baa02004-07-23 15:11:07 +0000173static int
paule6822762004-08-19 04:13:29 +0000174getsockopt_ipv6_ifindex (struct msghdr *msgh)
paul4f7baa02004-07-23 15:11:07 +0000175{
176 struct in6_pktinfo *pktinfo;
177
178 pktinfo = getsockopt_cmsg_data (msgh, IPPROTO_IPV6, IPV6_PKTINFO);
179
180 return pktinfo->ipi6_ifindex;
181}
paul718e3742002-12-13 20:15:29 +0000182#endif /* HAVE_IPV6 */
183
184
gdtcc49eb52004-12-30 13:50:32 +0000185/*
186 * Process multicast socket options for IPv4 in an OS-dependent manner.
Dmitrij Tejblumbf510e92011-08-18 20:22:17 +0400187 * Supported options are IP_{ADD,DROP}_MEMBERSHIP.
gdtcc49eb52004-12-30 13:50:32 +0000188 *
189 * Many operating systems have a limit on the number of groups that
190 * can be joined per socket (where each group and local address
191 * counts). This impacts OSPF, which joins groups on each interface
192 * using a single socket. The limit is typically 20, derived from the
193 * original BSD multicast implementation. Some systems have
194 * mechanisms for increasing this limit.
ajsc188c372005-10-21 02:57:41 +0000195 *
196 * In many 4.4BSD-derived systems, multicast group operations are not
197 * allowed on interfaces that are not UP. Thus, a previous attempt to
198 * leave the group may have failed, leaving it still joined, and we
199 * drop/join quietly to recover. This may not be necessary, but aims to
200 * defend against unknown behavior in that we will still return an error
201 * if the second join fails. It is not clear how other systems
202 * (e.g. Linux, Solaris) behave when leaving groups on down interfaces,
203 * but this behavior should not be harmful if they behave the same way,
204 * allow leaves, or implicitly leave all groups joined to down interfaces.
gdtcc49eb52004-12-30 13:50:32 +0000205 */
paul718e3742002-12-13 20:15:29 +0000206int
Dmitrij Tejblumbf510e92011-08-18 20:22:17 +0400207setsockopt_ipv4_multicast(int sock,
paul718e3742002-12-13 20:15:29 +0000208 int optname,
paul718e3742002-12-13 20:15:29 +0000209 unsigned int mcast_addr,
Dmitrij Tejblumbf510e92011-08-18 20:22:17 +0400210 unsigned int ifindex)
paul718e3742002-12-13 20:15:29 +0000211{
212
paul42c98192005-05-07 02:22:51 +0000213#ifdef HAVE_STRUCT_IP_MREQN_IMR_IFINDEX
paul718e3742002-12-13 20:15:29 +0000214 struct ip_mreqn mreqn;
ajsc188c372005-10-21 02:57:41 +0000215 int ret;
paul718e3742002-12-13 20:15:29 +0000216
Dmitrij Tejblum96a0a392011-08-19 23:00:30 +0400217 assert(optname == IP_ADD_MEMBERSHIP || optname == IP_DROP_MEMBERSHIP);
218 memset (&mreqn, 0, sizeof(mreqn));
219
220 if (mcast_addr)
221 mreqn.imr_multiaddr.s_addr = mcast_addr;
222
223 mreqn.imr_ifindex = ifindex;
224
225 ret = setsockopt(sock, IPPROTO_IP, optname,
226 (void *)&mreqn, sizeof(mreqn));
227 if ((ret < 0) && (optname == IP_ADD_MEMBERSHIP) && (errno == EADDRINUSE))
paul718e3742002-12-13 20:15:29 +0000228 {
Dmitrij Tejblum96a0a392011-08-19 23:00:30 +0400229 /* see above: handle possible problem when interface comes back up */
230 char buf[1][INET_ADDRSTRLEN];
231 zlog_info("setsockopt_ipv4_multicast attempting to drop and "
232 "re-add (fd %d, mcast %s, ifindex %u)",
233 sock,
234 inet_ntop(AF_INET, &mreqn.imr_multiaddr,
235 buf[0], sizeof(buf[0])), ifindex);
236 setsockopt(sock, IPPROTO_IP, IP_DROP_MEMBERSHIP,
237 (void *)&mreqn, sizeof(mreqn));
238 ret = setsockopt(sock, IPPROTO_IP, IP_ADD_MEMBERSHIP,
239 (void *)&mreqn, sizeof(mreqn));
paul718e3742002-12-13 20:15:29 +0000240 }
Dmitrij Tejblum96a0a392011-08-19 23:00:30 +0400241 return ret;
paul718e3742002-12-13 20:15:29 +0000242
243 /* Example defines for another OS, boilerplate off other code in this
244 function, AND handle optname as per other sections for consistency !! */
245 /* #elif defined(BOGON_NIX) && EXAMPLE_VERSION_CODE > -100000 */
246 /* Add your favourite OS here! */
247
Dmitrij Tejblumbf510e92011-08-18 20:22:17 +0400248#elif defined(HAVE_BSD_STRUCT_IP_MREQ_HACK) /* #if OS_TYPE */
gdtcc49eb52004-12-30 13:50:32 +0000249 /* standard BSD API */
paul718e3742002-12-13 20:15:29 +0000250
251 struct in_addr m;
252 struct ip_mreq mreq;
ajsc188c372005-10-21 02:57:41 +0000253 int ret;
paul718e3742002-12-13 20:15:29 +0000254
Dmitrij Tejblum96a0a392011-08-19 23:00:30 +0400255 assert(optname == IP_ADD_MEMBERSHIP || optname == IP_DROP_MEMBERSHIP);
256
Dmitrij Tejblumbf510e92011-08-18 20:22:17 +0400257 m.s_addr = htonl(ifindex);
paul42c98192005-05-07 02:22:51 +0000258
Dmitrij Tejblum96a0a392011-08-19 23:00:30 +0400259 memset (&mreq, 0, sizeof(mreq));
260 mreq.imr_multiaddr.s_addr = mcast_addr;
261 mreq.imr_interface = m;
262
263 ret = setsockopt (sock, IPPROTO_IP, optname, (void *)&mreq, sizeof(mreq));
264 if ((ret < 0) && (optname == IP_ADD_MEMBERSHIP) && (errno == EADDRINUSE))
paul718e3742002-12-13 20:15:29 +0000265 {
Dmitrij Tejblum96a0a392011-08-19 23:00:30 +0400266 /* see above: handle possible problem when interface comes back up */
267 char buf[1][INET_ADDRSTRLEN];
268 zlog_info("setsockopt_ipv4_multicast attempting to drop and "
269 "re-add (fd %d, mcast %s, ifindex %u)",
270 sock,
271 inet_ntop(AF_INET, &mreq.imr_multiaddr,
272 buf[0], sizeof(buf[0])), 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));
paul718e3742002-12-13 20:15:29 +0000277 }
Dmitrij Tejblum96a0a392011-08-19 23:00:30 +0400278 return ret;
279
Dmitrij Tejblumbf510e92011-08-18 20:22:17 +0400280#else
281 #error "Unsupported multicast API"
paul718e3742002-12-13 20:15:29 +0000282#endif /* #if OS_TYPE */
283
284}
paul4f7baa02004-07-23 15:11:07 +0000285
Dmitrij Tejblumbf510e92011-08-18 20:22:17 +0400286/*
287 * Set IP_MULTICAST_IF socket option in an OS-dependent manner.
288 */
289int
290setsockopt_ipv4_multicast_if(int sock,
291 unsigned int ifindex)
292{
293
294#ifdef HAVE_STRUCT_IP_MREQN_IMR_IFINDEX
295 struct ip_mreqn mreqn;
Dmitrij Tejblum87d29032011-08-19 22:25:23 +0400296 memset (&mreqn, 0, sizeof(mreqn));
Dmitrij Tejblumbf510e92011-08-18 20:22:17 +0400297
298 mreqn.imr_ifindex = ifindex;
299 return setsockopt(sock, IPPROTO_IP, IP_MULTICAST_IF, (void *)&mreqn, sizeof(mreqn));
300
301 /* Example defines for another OS, boilerplate off other code in this
302 function */
303 /* #elif defined(BOGON_NIX) && EXAMPLE_VERSION_CODE > -100000 */
304 /* Add your favourite OS here! */
305#elif defined(HAVE_BSD_STRUCT_IP_MREQ_HACK)
306 struct in_addr m;
307
308 m.s_addr = htonl(ifindex);
309
310 return setsockopt (sock, IPPROTO_IP, IP_MULTICAST_IF, (void *)&m, sizeof(m));
311#else
312 #error "Unsupported multicast API"
313#endif
314}
315
paul4f7baa02004-07-23 15:11:07 +0000316static int
paule6822762004-08-19 04:13:29 +0000317setsockopt_ipv4_ifindex (int sock, int val)
paul4f7baa02004-07-23 15:11:07 +0000318{
319 int ret;
320
321#if defined (IP_PKTINFO)
ajs1d75c8c2004-12-28 21:43:17 +0000322 if ((ret = setsockopt (sock, IPPROTO_IP, IP_PKTINFO, &val, sizeof (val))) < 0)
323 zlog_warn ("Can't set IP_PKTINFO option for fd %d to %d: %s",
324 sock,val,safe_strerror(errno));
paul4f7baa02004-07-23 15:11:07 +0000325#elif defined (IP_RECVIF)
ajs1d75c8c2004-12-28 21:43:17 +0000326 if ((ret = setsockopt (sock, IPPROTO_IP, IP_RECVIF, &val, sizeof (val))) < 0)
327 zlog_warn ("Can't set IP_RECVIF option for fd %d to %d: %s",
328 sock,val,safe_strerror(errno));
paul4f7baa02004-07-23 15:11:07 +0000329#else
330#warning "Neither IP_PKTINFO nor IP_RECVIF is available."
331#warning "Will not be able to receive link info."
332#warning "Things might be seriously broken.."
ajs1d75c8c2004-12-28 21:43:17 +0000333 /* XXX Does this ever happen? Should there be a zlog_warn message here? */
334 ret = -1;
paul4f7baa02004-07-23 15:11:07 +0000335#endif
paul4f7baa02004-07-23 15:11:07 +0000336 return ret;
337}
338
paule6822762004-08-19 04:13:29 +0000339int
Stephen Hemminger1423c802008-08-14 17:59:25 +0100340setsockopt_ipv4_tos(int sock, int tos)
341{
342 int ret;
343
344 ret = setsockopt (sock, IPPROTO_IP, IP_TOS, &tos, sizeof (tos));
345 if (ret < 0)
346 zlog_warn ("Can't set IP_TOS option for fd %d to %#x: %s",
347 sock, tos, safe_strerror(errno));
348 return ret;
349}
350
351
352int
paule6822762004-08-19 04:13:29 +0000353setsockopt_ifindex (int af, int sock, int val)
paul4f7baa02004-07-23 15:11:07 +0000354{
paule6822762004-08-19 04:13:29 +0000355 int ret = -1;
356
357 switch (af)
358 {
359 case AF_INET:
360 ret = setsockopt_ipv4_ifindex (sock, val);
361 break;
362#ifdef HAVE_IPV6
363 case AF_INET6:
364 ret = setsockopt_ipv6_pktinfo (sock, val);
365 break;
366#endif
367 default:
hassoe473b032004-09-26 16:08:11 +0000368 zlog_warn ("setsockopt_ifindex: unknown address family %d", af);
paule6822762004-08-19 04:13:29 +0000369 }
370 return ret;
371}
372
gdtd44debe2004-12-29 20:06:23 +0000373/*
374 * Requires: msgh is not NULL and points to a valid struct msghdr, which
375 * may or may not have control data about the incoming interface.
376 *
377 * Returns the interface index (small integer >= 1) if it can be
378 * determined, or else 0.
379 */
paule6822762004-08-19 04:13:29 +0000380static int
381getsockopt_ipv4_ifindex (struct msghdr *msgh)
382{
gdtd44debe2004-12-29 20:06:23 +0000383 /* XXX: initialize to zero? (Always overwritten, so just cosmetic.) */
paule6822762004-08-19 04:13:29 +0000384 int ifindex = -1;
gdt1d69fdf2004-12-29 18:53:30 +0000385
paule6822762004-08-19 04:13:29 +0000386#if defined(IP_PKTINFO)
387/* Linux pktinfo based ifindex retrieval */
paul4f7baa02004-07-23 15:11:07 +0000388 struct in_pktinfo *pktinfo;
paule6822762004-08-19 04:13:29 +0000389
390 pktinfo =
391 (struct in_pktinfo *)getsockopt_cmsg_data (msgh, IPPROTO_IP, IP_PKTINFO);
gdtd44debe2004-12-29 20:06:23 +0000392 /* XXX Can pktinfo be NULL? Clean up post 0.98. */
paule6822762004-08-19 04:13:29 +0000393 ifindex = pktinfo->ipi_ifindex;
394
395#elif defined(IP_RECVIF)
gdtd44debe2004-12-29 20:06:23 +0000396
397 /* retrieval based on IP_RECVIF */
398
paul4f7baa02004-07-23 15:11:07 +0000399#ifndef SUNOS_5
gdtd44debe2004-12-29 20:06:23 +0000400 /* BSD systems use a sockaddr_dl as the control message payload. */
paul4f7baa02004-07-23 15:11:07 +0000401 struct sockaddr_dl *sdl;
gdtd44debe2004-12-29 20:06:23 +0000402#else
403 /* SUNOS_5 uses an integer with the index. */
404 int *ifindex_p;
paul4f7baa02004-07-23 15:11:07 +0000405#endif /* SUNOS_5 */
gdt1d69fdf2004-12-29 18:53:30 +0000406
gdt33f92322004-07-23 16:14:32 +0000407#ifndef SUNOS_5
gdtd44debe2004-12-29 20:06:23 +0000408 /* BSD */
gdt33f92322004-07-23 16:14:32 +0000409 sdl =
paul4f7baa02004-07-23 15:11:07 +0000410 (struct sockaddr_dl *)getsockopt_cmsg_data (msgh, IPPROTO_IP, IP_RECVIF);
gdtd44debe2004-12-29 20:06:23 +0000411 if (sdl != NULL)
412 ifindex = sdl->sdl_index;
413 else
414 ifindex = 0;
415#else
416 /*
417 * Solaris. On Solaris 8, IP_RECVIF is defined, but the call to
418 * enable it fails with errno=99, and the struct msghdr has
419 * controllen 0.
420 */
421 ifindex_p = (uint_t *)getsockopt_cmsg_data (msgh, IPPROTO_IP, IP_RECVIF);
422 if (ifindex_p != NULL)
423 ifindex = *ifindex_p;
424 else
425 ifindex = 0;
paul4f7baa02004-07-23 15:11:07 +0000426#endif /* SUNOS_5 */
paule6822762004-08-19 04:13:29 +0000427
gdtd44debe2004-12-29 20:06:23 +0000428#else
429 /*
430 * Neither IP_PKTINFO nor IP_RECVIF defined - warn at compile time.
431 * XXX Decide if this is a core service, or if daemons have to cope.
432 * Since Solaris 8 and OpenBSD seem not to provide it, it seems that
433 * daemons have to cope.
434 */
435#warning "getsockopt_ipv4_ifindex: Neither IP_PKTINFO nor IP_RECVIF defined."
436#warning "Some daemons may fail to operate correctly!"
paul7d9c6e52004-10-22 10:54:39 +0000437 ifindex = 0;
gdtd44debe2004-12-29 20:06:23 +0000438
paule6822762004-08-19 04:13:29 +0000439#endif /* IP_PKTINFO */
gdtd44debe2004-12-29 20:06:23 +0000440
paul4f7baa02004-07-23 15:11:07 +0000441 return ifindex;
442}
443
444/* return ifindex, 0 if none found */
445int
paule6822762004-08-19 04:13:29 +0000446getsockopt_ifindex (int af, struct msghdr *msgh)
paul4f7baa02004-07-23 15:11:07 +0000447{
paul4f7baa02004-07-23 15:11:07 +0000448 switch (af)
449 {
450 case AF_INET:
paule6822762004-08-19 04:13:29 +0000451 return (getsockopt_ipv4_ifindex (msgh));
paul4f7baa02004-07-23 15:11:07 +0000452 break;
453#ifdef HAVE_IPV6
454 case AF_INET6:
paule6822762004-08-19 04:13:29 +0000455 return (getsockopt_ipv6_ifindex (msgh));
paul4f7baa02004-07-23 15:11:07 +0000456 break;
457#endif
458 default:
hassoe473b032004-09-26 16:08:11 +0000459 zlog_warn ("getsockopt_ifindex: unknown address family %d", af);
Denis Ovsienkoc69f91b2011-08-01 21:59:04 +0400460 return 0;
paul4f7baa02004-07-23 15:11:07 +0000461 }
462}
paul96e27c92004-10-05 14:33:43 +0000463
464/* swab iph between order system uses for IP_HDRINCL and host order */
465void
466sockopt_iphdrincl_swab_htosys (struct ip *iph)
467{
468 /* BSD and derived take iph in network order, except for
469 * ip_len and ip_off
470 */
471#ifndef HAVE_IP_HDRINCL_BSD_ORDER
472 iph->ip_len = htons(iph->ip_len);
473 iph->ip_off = htons(iph->ip_off);
474#endif /* HAVE_IP_HDRINCL_BSD_ORDER */
475
476 iph->ip_id = htons(iph->ip_id);
477}
478
479void
480sockopt_iphdrincl_swab_systoh (struct ip *iph)
481{
482#ifndef HAVE_IP_HDRINCL_BSD_ORDER
483 iph->ip_len = ntohs(iph->ip_len);
484 iph->ip_off = ntohs(iph->ip_off);
485#endif /* HAVE_IP_HDRINCL_BSD_ORDER */
486
487 iph->ip_id = ntohs(iph->ip_id);
488}
Paul Jakma0df7c912008-07-21 21:02:49 +0000489
490int
491sockopt_tcp_signature (int sock, union sockunion *su, const char *password)
492{
Paul Jakma3453a712009-06-12 14:21:02 +0100493#if defined(HAVE_TCP_MD5_LINUX24) && defined(GNU_LINUX)
494 /* Support for the old Linux 2.4 TCP-MD5 patch, taken from Hasso Tepper's
495 * version of the Quagga patch (based on work by Rick Payne, and Bruce
496 * Simpson)
497 */
498#define TCP_MD5_AUTH 13
499#define TCP_MD5_AUTH_ADD 1
500#define TCP_MD5_AUTH_DEL 2
501 struct tcp_rfc2385_cmd {
502 u_int8_t command; /* Command - Add/Delete */
503 u_int32_t address; /* IPV4 address associated */
504 u_int8_t keylen; /* MD5 Key len (do NOT assume 0 terminated ascii) */
505 void *key; /* MD5 Key */
506 } cmd;
507 struct in_addr *addr = &su->sin.sin_addr;
508
509 cmd.command = (password != NULL ? TCP_MD5_AUTH_ADD : TCP_MD5_AUTH_DEL);
510 cmd.address = addr->s_addr;
511 cmd.keylen = (password != NULL ? strlen (password) : 0);
512 cmd.key = password;
513
514 return setsockopt (sock, IPPROTO_TCP, TCP_MD5_AUTH, &cmd, sizeof cmd);
515
516#elif HAVE_DECL_TCP_MD5SIG
Paul Jakmaf5612dd2008-08-15 14:05:08 +0100517 int ret;
Paul Jakma0df7c912008-07-21 21:02:49 +0000518#ifndef GNU_LINUX
519 /*
520 * XXX Need to do PF_KEY operation here to add/remove an SA entry,
521 * and add/remove an SP entry for this peer's packet flows also.
522 */
523 int md5sig = password && *password ? 1 : 0;
524#else
525 int keylen = password ? strlen (password) : 0;
526 struct tcp_md5sig md5sig;
527 union sockunion *su2, *susock;
Paul Jakma0df7c912008-07-21 21:02:49 +0000528
529 /* Figure out whether the socket and the sockunion are the same family..
530 * adding AF_INET to AF_INET6 needs to be v4 mapped, you'd think..
531 */
532 if (!(susock = sockunion_getsockname (sock)))
533 return -1;
534
535 if (susock->sa.sa_family == su->sa.sa_family)
536 su2 = su;
537 else
538 {
539 /* oops.. */
540 su2 = susock;
541
542 if (su2->sa.sa_family == AF_INET)
543 {
544 sockunion_free (susock);
Chris Caputo2b35ae42009-06-23 05:34:29 +0000545 return 0;
546 }
Paul Jakma0df7c912008-07-21 21:02:49 +0000547
Stephen Hemmingere5862a22009-02-19 23:01:41 -0800548#ifdef HAVE_IPV6
Paul Jakma0df7c912008-07-21 21:02:49 +0000549 /* If this does not work, then all users of this sockopt will need to
550 * differentiate between IPv4 and IPv6, and keep seperate sockets for
551 * each.
552 *
553 * Sadly, it doesn't seem to work at present. It's unknown whether
554 * this is a bug or not.
555 */
556 if (su2->sa.sa_family == AF_INET6
557 && su->sa.sa_family == AF_INET)
558 {
559 su2->sin6.sin6_family = AF_INET6;
560 /* V4Map the address */
561 memset (&su2->sin6.sin6_addr, 0, sizeof (struct in6_addr));
562 su2->sin6.sin6_addr.s6_addr32[2] = htonl(0xffff);
563 memcpy (&su2->sin6.sin6_addr.s6_addr32[3], &su->sin.sin_addr, 4);
564 }
Stephen Hemmingere5862a22009-02-19 23:01:41 -0800565#endif
Paul Jakma0df7c912008-07-21 21:02:49 +0000566 }
567
568 memset (&md5sig, 0, sizeof (md5sig));
569 memcpy (&md5sig.tcpm_addr, su2, sizeof (*su2));
570 md5sig.tcpm_keylen = keylen;
571 if (keylen)
572 memcpy (md5sig.tcpm_key, password, keylen);
Paul Jakmaf5612dd2008-08-15 14:05:08 +0100573 sockunion_free (susock);
Paul Jakma0df7c912008-07-21 21:02:49 +0000574#endif /* GNU_LINUX */
Chris Caputo2b35ae42009-06-23 05:34:29 +0000575 if ((ret = setsockopt (sock, IPPROTO_TCP, TCP_MD5SIG, &md5sig, sizeof md5sig)) < 0)
576 {
577 /* ENOENT is harmless. It is returned when we clear a password for which
578 one was not previously set. */
579 if (ENOENT == errno)
580 ret = 0;
581 else
582 zlog_err ("sockopt_tcp_signature: setsockopt(%d): %s",
583 sock, safe_strerror(errno));
584 }
Paul Jakma0df7c912008-07-21 21:02:49 +0000585 return ret;
586#else /* HAVE_TCP_MD5SIG */
587 return -2;
Paul Jakma3453a712009-06-12 14:21:02 +0100588#endif /* !HAVE_TCP_MD5SIG */
Paul Jakma0df7c912008-07-21 21:02:49 +0000589}