tools/llvm: Do not build with symbols
[minix3.git] / lib / libc / net / rthdr.c
blob9517cc0cc608d02fa9f6d0160a3c2962d8140077
1 /* $NetBSD: rthdr.c,v 1.18 2012/03/13 21:13:42 christos Exp $ */
3 /*
4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the project nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
32 #include <sys/cdefs.h>
33 #if defined(LIBC_SCCS) && !defined(lint)
34 __RCSID("$NetBSD: rthdr.c,v 1.18 2012/03/13 21:13:42 christos Exp $");
35 #endif /* LIBC_SCCS and not lint */
37 #include "namespace.h"
38 #include <sys/param.h>
39 #include <sys/types.h>
40 #include <sys/socket.h>
42 #include <netinet/in.h>
43 #include <netinet/ip6.h>
45 #include <assert.h>
46 #include <string.h>
47 #include <stdio.h>
49 #ifdef __weak_alias
50 __weak_alias(inet6_rthdr_add,_inet6_rthdr_add)
51 __weak_alias(inet6_rthdr_getaddr,_inet6_rthdr_getaddr)
52 __weak_alias(inet6_rthdr_getflags,_inet6_rthdr_getflags)
53 __weak_alias(inet6_rthdr_init,_inet6_rthdr_init)
54 __weak_alias(inet6_rthdr_lasthop,_inet6_rthdr_lasthop)
55 __weak_alias(inet6_rthdr_segments,_inet6_rthdr_segments)
56 __weak_alias(inet6_rthdr_space,_inet6_rthdr_space)
57 __weak_alias(inet6_rth_space, _inet6_rth_space)
58 __weak_alias(inet6_rth_init, _inet6_rth_init)
59 __weak_alias(inet6_rth_add, _inet6_rth_add)
60 __weak_alias(inet6_rth_reverse, _inet6_rth_reverse)
61 __weak_alias(inet6_rth_segments, _inet6_rth_segments)
62 __weak_alias(inet6_rth_getaddr, _inet6_rth_getaddr)
63 #endif
66 * RFC2292 API
69 size_t
70 inet6_rthdr_space(int type, int seg)
72 switch (type) {
73 case IPV6_RTHDR_TYPE_0:
74 if (seg < 1 || seg > 23)
75 return (0);
76 return (CMSG_SPACE(sizeof(struct in6_addr) * seg +
77 sizeof(struct ip6_rthdr0)));
78 default:
79 return (0);
83 struct cmsghdr *
84 inet6_rthdr_init(void *bp, int type)
86 struct cmsghdr *ch;
87 struct ip6_rthdr *rthdr;
89 _DIAGASSERT(bp != NULL);
91 ch = (struct cmsghdr *)bp;
92 rthdr = (struct ip6_rthdr *)(void *)CMSG_DATA(ch);
94 ch->cmsg_level = IPPROTO_IPV6;
95 ch->cmsg_type = IPV6_RTHDR;
97 switch (type) {
98 case IPV6_RTHDR_TYPE_0:
99 #ifdef COMPAT_RFC2292
100 ch->cmsg_len = CMSG_LEN(sizeof(struct ip6_rthdr0) -
101 sizeof(struct in6_addr));
102 #else
103 ch->cmsg_len = CMSG_LEN(sizeof(struct ip6_rthdr0));
104 #endif
105 (void)memset(rthdr, 0, sizeof(struct ip6_rthdr0));
106 rthdr->ip6r_type = IPV6_RTHDR_TYPE_0;
107 return (ch);
108 default:
109 return (NULL);
114 inet6_rthdr_add(struct cmsghdr *cmsg, const struct in6_addr *addr, u_int flags)
116 struct ip6_rthdr *rthdr;
118 _DIAGASSERT(cmsg != NULL);
119 _DIAGASSERT(addr != NULL);
121 rthdr = (struct ip6_rthdr *)(void *)CMSG_DATA(cmsg);
123 switch (rthdr->ip6r_type) {
124 case IPV6_RTHDR_TYPE_0:
126 size_t len;
127 struct ip6_rthdr0 *rt0 = (struct ip6_rthdr0 *)(void *)rthdr;
128 if (flags != IPV6_RTHDR_LOOSE && flags != IPV6_RTHDR_STRICT)
129 return (-1);
130 if (rt0->ip6r0_segleft == 23)
131 return (-1);
132 if (flags != IPV6_RTHDR_LOOSE)
133 return (-1);
134 rt0->ip6r0_segleft++;
135 (void)memcpy(((caddr_t)(void *)rt0) +
136 ((rt0->ip6r0_len + 1) << 3), addr, sizeof(struct in6_addr));
137 rt0->ip6r0_len += sizeof(struct in6_addr) >> 3;
138 len = CMSG_LEN((rt0->ip6r0_len + 1) << 3);
139 _DIAGASSERT(__type_fit(socklen_t, len));
140 cmsg->cmsg_len = (socklen_t)len;
141 break;
143 default:
144 return (-1);
147 return (0);
151 inet6_rthdr_lasthop(struct cmsghdr *cmsg, unsigned int flags)
153 struct ip6_rthdr *rthdr;
155 _DIAGASSERT(cmsg != NULL);
157 rthdr = (struct ip6_rthdr *)(void *)CMSG_DATA(cmsg);
159 switch (rthdr->ip6r_type) {
160 case IPV6_RTHDR_TYPE_0:
162 struct ip6_rthdr0 *rt0 = (struct ip6_rthdr0 *)(void *)rthdr;
163 if (rt0->ip6r0_segleft > 23)
164 return (-1);
165 if (flags != IPV6_RTHDR_LOOSE)
166 return (-1);
167 break;
169 default:
170 return (-1);
173 return (0);
176 #if 0
178 inet6_rthdr_reverse(const struct cmsghdr *in, struct cmsghdr *out)
181 return (-1);
183 #endif
186 inet6_rthdr_segments(const struct cmsghdr *cmsg)
188 const struct ip6_rthdr *rthdr;
190 _DIAGASSERT(cmsg != NULL);
192 rthdr = __UNCONST(CCMSG_DATA(cmsg));
194 switch (rthdr->ip6r_type) {
195 case IPV6_RTHDR_TYPE_0:
197 const struct ip6_rthdr0 *rt0 =
198 (const struct ip6_rthdr0 *)(const void *)rthdr;
199 size_t len;
201 if (rt0->ip6r0_len % 2 || 46 < rt0->ip6r0_len)
202 return (-1);
204 len = (rt0->ip6r0_len * 8) / sizeof(struct in6_addr);
205 _DIAGASSERT(__type_fit(int, len));
206 return (int)len;
209 default:
210 return (-1);
214 struct in6_addr *
215 inet6_rthdr_getaddr(struct cmsghdr *cmsg, int idx)
217 struct ip6_rthdr *rthdr;
219 _DIAGASSERT(cmsg != NULL);
221 rthdr = (struct ip6_rthdr *)(void *)CMSG_DATA(cmsg);
223 switch (rthdr->ip6r_type) {
224 case IPV6_RTHDR_TYPE_0:
226 struct ip6_rthdr0 *rt0 = (struct ip6_rthdr0 *)(void *)rthdr;
227 int naddr;
228 size_t len;
230 if (rt0->ip6r0_len % 2 || 46 < rt0->ip6r0_len)
231 return NULL;
232 len = (rt0->ip6r0_len * 8) / sizeof(struct in6_addr);
233 _DIAGASSERT(__type_fit(int, len));
234 naddr = (int)len;
235 if (idx <= 0 || naddr < idx)
236 return NULL;
237 #ifdef COMPAT_RFC2292
238 return ((struct in6_addr *)(void *)(rt0 + 1)) + idx - 1;
239 #else
240 return ((struct in6_addr *)(void *)(rt0 + 1)) + idx;
241 #endif
244 default:
245 return NULL;
250 inet6_rthdr_getflags(const struct cmsghdr *cmsg, int idx)
252 const struct ip6_rthdr *rthdr;
254 _DIAGASSERT(cmsg != NULL);
256 rthdr = __UNCONST(CCMSG_DATA(cmsg));
258 switch (rthdr->ip6r_type) {
259 case IPV6_RTHDR_TYPE_0:
261 const struct ip6_rthdr0 *rt0 = (const struct ip6_rthdr0 *)
262 (const void *)rthdr;
263 int naddr;
264 size_t len;
266 if (rt0->ip6r0_len % 2 || 46 < rt0->ip6r0_len)
267 return (-1);
268 len = (rt0->ip6r0_len * 8) / sizeof(struct in6_addr);
269 _DIAGASSERT(__type_fit(int, len));
270 naddr = (int)len;
271 if (idx < 0 || naddr < idx)
272 return (-1);
273 return IPV6_RTHDR_LOOSE;
276 default:
277 return (-1);
282 * RFC3542 (2292bis) API
285 socklen_t
286 inet6_rth_space(int type, int segments)
288 switch (type) {
289 case IPV6_RTHDR_TYPE_0:
290 return (((segments * 2) + 1) << 3);
291 default:
292 return (0); /* type not suppported */
296 void *
297 inet6_rth_init(void *bp, socklen_t bp_len, int type, int segments)
299 struct ip6_rthdr *rth;
300 struct ip6_rthdr0 *rth0;
302 _DIAGASSERT(bp != NULL);
304 rth = (struct ip6_rthdr *)bp;
306 switch (type) {
307 case IPV6_RTHDR_TYPE_0:
308 /* length validation */
309 if (bp_len < inet6_rth_space(IPV6_RTHDR_TYPE_0, segments))
310 return (NULL);
312 memset(bp, 0, bp_len);
313 rth0 = (struct ip6_rthdr0 *)(void *)rth;
314 rth0->ip6r0_len = segments * 2;
315 rth0->ip6r0_type = IPV6_RTHDR_TYPE_0;
316 rth0->ip6r0_segleft = 0;
317 rth0->ip6r0_reserved = 0;
318 break;
319 default:
320 return (NULL); /* type not supported */
323 return (bp);
327 inet6_rth_add(void *bp, const struct in6_addr *addr)
329 struct ip6_rthdr *rth;
330 struct ip6_rthdr0 *rth0;
331 struct in6_addr *nextaddr;
333 _DIAGASSERT(bp != NULL);
335 rth = (struct ip6_rthdr *)bp;
337 switch (rth->ip6r_type) {
338 case IPV6_RTHDR_TYPE_0:
339 rth0 = (struct ip6_rthdr0 *)(void *)rth;
340 nextaddr = (struct in6_addr *)(void *)(rth0 + 1)
341 + rth0->ip6r0_segleft;
342 *nextaddr = *addr;
343 rth0->ip6r0_segleft++;
344 break;
345 default:
346 return (-1); /* type not supported */
349 return (0);
353 inet6_rth_reverse(const void *in, void *out)
355 const struct ip6_rthdr *rth_in;
356 const struct ip6_rthdr0 *rth0_in;
357 struct ip6_rthdr0 *rth0_out;
358 int i, segments;
360 _DIAGASSERT(in != NULL);
361 _DIAGASSERT(out != NULL);
363 rth_in = (const struct ip6_rthdr *)in;
365 switch (rth_in->ip6r_type) {
366 case IPV6_RTHDR_TYPE_0:
367 rth0_in = (const struct ip6_rthdr0 *)in;
368 rth0_out = (struct ip6_rthdr0 *)out;
370 /* parameter validation XXX too paranoid? */
371 if (rth0_in->ip6r0_len % 2)
372 return (-1);
373 segments = rth0_in->ip6r0_len / 2;
375 /* we can't use memcpy here, since in and out may overlap */
376 memmove((void *)rth0_out, (const void *)rth0_in,
377 (unsigned int)(((rth0_in->ip6r0_len) + 1) << 3));
378 rth0_out->ip6r0_segleft = segments;
380 /* reverse the addresses */
381 for (i = 0; i < segments / 2; i++) {
382 struct in6_addr addr_tmp, *addr1, *addr2;
384 addr1 = (struct in6_addr *)(void *)(rth0_out + 1) + i;
385 addr2 = (struct in6_addr *)(void *)(rth0_out + 1) +
386 (segments - i - 1);
387 addr_tmp = *addr1;
388 *addr1 = *addr2;
389 *addr2 = addr_tmp;
392 break;
393 default:
394 return (-1); /* type not supported */
397 return (0);
401 inet6_rth_segments(const void *bp)
403 const struct ip6_rthdr *rh;
404 const struct ip6_rthdr0 *rh0;
405 unsigned int addrs;
407 _DIAGASSERT(bp != NULL);
409 rh = (const struct ip6_rthdr *)bp;
411 switch (rh->ip6r_type) {
412 case IPV6_RTHDR_TYPE_0:
413 rh0 = (const struct ip6_rthdr0 *)bp;
416 * Validation for a type-0 routing header.
417 * Is this too strict?
419 if ((rh0->ip6r0_len % 2) != 0 ||
420 (addrs = (rh0->ip6r0_len / 2)) < rh0->ip6r0_segleft)
421 return (-1);
423 return (addrs);
424 default:
425 return (-1); /* unknown type */
429 struct in6_addr *
430 inet6_rth_getaddr(const void *bp, int idx)
432 const struct ip6_rthdr *rh;
433 const struct ip6_rthdr0 *rh0;
434 unsigned int addrs;
436 _DIAGASSERT(bp != NULL);
438 rh = (const struct ip6_rthdr *)bp;
440 switch (rh->ip6r_type) {
441 case IPV6_RTHDR_TYPE_0:
442 rh0 = (const struct ip6_rthdr0 *)bp;
445 * Validation for a type-0 routing header.
446 * Is this too strict?
448 if ((rh0->ip6r0_len % 2) != 0 ||
449 (addrs = (rh0->ip6r0_len / 2)) < rh0->ip6r0_segleft)
450 return (NULL);
452 if (idx < 0 || addrs <= (unsigned int)idx)
453 return (NULL);
455 return (((struct in6_addr *)(void *)__UNCONST(rh0 + 1)) + idx);
456 default:
457 return (NULL); /* unknown type */