1 /* $KAME: rthdr.c,v 1.8 2001/08/20 02:32:40 itojun Exp $ */
4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
31 * $FreeBSD: src/lib/libc/net/rthdr.c,v 1.2.2.1 2002/04/28 05:40:24 suz Exp $
32 * $DragonFly: src/lib/libc/net/rthdr.c,v 1.5 2007/05/29 10:58:11 hasso Exp $
35 #include <sys/param.h>
36 #include <sys/types.h>
37 #include <sys/socket.h>
39 #include <netinet/in.h>
40 #include <netinet/ip6.h>
46 inet6_rthdr_space(int type
, int seg
)
49 case IPV6_RTHDR_TYPE_0
:
50 if (seg
< 1 || seg
> 23)
52 return(CMSG_SPACE(sizeof(struct in6_addr
) * (seg
- 1)
53 + sizeof(struct ip6_rthdr0
)));
56 fprintf(stderr
, "inet6_rthdr_space: unknown type(%d)\n", type
);
63 inet6_rthdr_init(void *bp
, int type
)
65 struct cmsghdr
*ch
= (struct cmsghdr
*)bp
;
66 struct ip6_rthdr
*rthdr
;
68 rthdr
= (struct ip6_rthdr
*)CMSG_DATA(ch
);
70 ch
->cmsg_level
= IPPROTO_IPV6
;
71 ch
->cmsg_type
= IPV6_RTHDR
;
74 case IPV6_RTHDR_TYPE_0
:
75 ch
->cmsg_len
= CMSG_LEN(sizeof(struct ip6_rthdr0
) - sizeof(struct in6_addr
));
76 bzero(rthdr
, sizeof(struct ip6_rthdr0
));
77 rthdr
->ip6r_type
= IPV6_RTHDR_TYPE_0
;
81 fprintf(stderr
, "inet6_rthdr_init: unknown type(%d)\n", type
);
88 inet6_rthdr_add(struct cmsghdr
*cmsg
, const struct in6_addr
*addr
, u_int flags
)
90 struct ip6_rthdr
*rthdr
;
92 rthdr
= (struct ip6_rthdr
*)CMSG_DATA(cmsg
);
94 switch(rthdr
->ip6r_type
) {
95 case IPV6_RTHDR_TYPE_0
:
97 struct ip6_rthdr0
*rt0
= (struct ip6_rthdr0
*)rthdr
;
98 if (flags
!= IPV6_RTHDR_LOOSE
&& flags
!= IPV6_RTHDR_STRICT
) {
100 fprintf(stderr
, "inet6_rthdr_add: unsupported flag(%d)\n", flags
);
104 if (rt0
->ip6r0_segleft
== 23) {
106 fprintf(stderr
, "inet6_rthdr_add: segment overflow\n");
110 if (flags
== IPV6_RTHDR_STRICT
) {
112 c
= rt0
->ip6r0_segleft
/ 8;
113 b
= rt0
->ip6r0_segleft
% 8;
114 rt0
->ip6r0_slmap
[c
] |= (1 << (7 - b
));
116 rt0
->ip6r0_segleft
++;
117 bcopy(addr
, (caddr_t
)rt0
+ ((rt0
->ip6r0_len
+ 1) << 3),
118 sizeof(struct in6_addr
));
119 rt0
->ip6r0_len
+= sizeof(struct in6_addr
) >> 3;
120 cmsg
->cmsg_len
= CMSG_LEN((rt0
->ip6r0_len
+ 1) << 3);
125 fprintf(stderr
, "inet6_rthdr_add: unknown type(%d)\n",
135 inet6_rthdr_lasthop(struct cmsghdr
*cmsg
, unsigned int flags
)
137 struct ip6_rthdr
*rthdr
;
139 rthdr
= (struct ip6_rthdr
*)CMSG_DATA(cmsg
);
141 switch(rthdr
->ip6r_type
) {
142 case IPV6_RTHDR_TYPE_0
:
144 struct ip6_rthdr0
*rt0
= (struct ip6_rthdr0
*)rthdr
;
145 if (flags
!= IPV6_RTHDR_LOOSE
&& flags
!= IPV6_RTHDR_STRICT
) {
147 fprintf(stderr
, "inet6_rthdr_lasthop: unsupported flag(%d)\n", flags
);
151 if (rt0
->ip6r0_segleft
> 23) {
153 fprintf(stderr
, "inet6_rthdr_add: segment overflow\n");
157 if (flags
== IPV6_RTHDR_STRICT
) {
159 c
= rt0
->ip6r0_segleft
/ 8;
160 b
= rt0
->ip6r0_segleft
% 8;
161 rt0
->ip6r0_slmap
[c
] |= (1 << (7 - b
));
167 fprintf(stderr
, "inet6_rthdr_lasthop: unknown type(%d)\n",
178 inet6_rthdr_reverse(const struct cmsghdr
*in
, struct cmsghdr
*out
)
181 fprintf(stderr
, "inet6_rthdr_reverse: not implemented yet\n");
188 inet6_rthdr_segments(const struct cmsghdr
*cmsg
)
190 struct ip6_rthdr
*rthdr
;
192 rthdr
= (struct ip6_rthdr
*)CMSG_DATA(cmsg
);
194 switch(rthdr
->ip6r_type
) {
195 case IPV6_RTHDR_TYPE_0
:
197 struct ip6_rthdr0
*rt0
= (struct ip6_rthdr0
*)rthdr
;
199 if (rt0
->ip6r0_len
% 2 || 46 < rt0
->ip6r0_len
) {
201 fprintf(stderr
, "inet6_rthdr_segments: invalid size(%d)\n",
207 return (rt0
->ip6r0_len
* 8) / sizeof(struct in6_addr
);
212 fprintf(stderr
, "inet6_rthdr_segments: unknown type(%d)\n",
220 inet6_rthdr_getaddr(struct cmsghdr
*cmsg
, int idx
)
222 struct ip6_rthdr
*rthdr
;
224 rthdr
= (struct ip6_rthdr
*)CMSG_DATA(cmsg
);
226 switch(rthdr
->ip6r_type
) {
227 case IPV6_RTHDR_TYPE_0
:
229 struct ip6_rthdr0
*rt0
= (struct ip6_rthdr0
*)rthdr
;
232 if (rt0
->ip6r0_len
% 2 || 46 < rt0
->ip6r0_len
) {
234 fprintf(stderr
, "inet6_rthdr_getaddr: invalid size(%d)\n",
239 naddr
= (rt0
->ip6r0_len
* 8) / sizeof(struct in6_addr
);
240 if (idx
<= 0 || naddr
< idx
) {
242 fprintf(stderr
, "inet6_rthdr_getaddr: invalid idx(%d)\n", idx
);
246 return &rt0
->ip6r0_addr
[idx
- 1];
251 fprintf(stderr
, "inet6_rthdr_getaddr: unknown type(%d)\n",
259 inet6_rthdr_getflags(const struct cmsghdr
*cmsg
, int idx
)
261 struct ip6_rthdr
*rthdr
;
263 rthdr
= (struct ip6_rthdr
*)CMSG_DATA(cmsg
);
265 switch(rthdr
->ip6r_type
) {
266 case IPV6_RTHDR_TYPE_0
:
268 struct ip6_rthdr0
*rt0
= (struct ip6_rthdr0
*)rthdr
;
271 if (rt0
->ip6r0_len
% 2 || 46 < rt0
->ip6r0_len
) {
273 fprintf(stderr
, "inet6_rthdr_getflags: invalid size(%d)\n",
278 naddr
= (rt0
->ip6r0_len
* 8) / sizeof(struct in6_addr
);
279 if (idx
< 0 || naddr
< idx
) {
281 fprintf(stderr
, "inet6_rthdr_getflags: invalid idx(%d)\n", idx
);
285 if (rt0
->ip6r0_slmap
[idx
/ 8] & (0x80 >> (idx
% 8)))
286 return IPV6_RTHDR_STRICT
;
288 return IPV6_RTHDR_LOOSE
;
293 fprintf(stderr
, "inet6_rthdr_getflags: unknown type(%d)\n",
301 * RFC3542 (2292bis) API
305 inet6_rth_space(int type __unused
, int segments __unused
)
307 return (0); /* type not suppported */
311 inet6_rth_init(void *bp __unused
, socklen_t bp_len __unused
, int type __unused
,
312 int segments __unused
)
314 return (NULL
); /* type not supported */
318 inet6_rth_add(void *bp __unused
, const struct in6_addr
*addr __unused
)
320 return (-1); /* type not supported */
324 inet6_rth_reverse(const void *in __unused
, void *out __unused
)
326 return (-1); /* type not supported */
330 inet6_rth_segments(const void *bp __unused
)
332 return (-1); /* type not supported */
336 inet6_rth_getaddr(const void *bp __unused
, int idx __unused
)
338 return (NULL
); /* type not supported */