ip6_output.c revision 1.23.2.6 1 1.23.2.6 he /* $NetBSD: ip6_output.c,v 1.23.2.6 2002/02/26 20:14:37 he Exp $ */
2 1.23.2.2 he /* $KAME: ip6_output.c,v 1.109 2000/05/31 05:03:09 jinmei Exp $ */
3 1.23.2.2 he
4 1.23.2.2 he /*
5 1.23.2.2 he * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
6 1.23.2.2 he * All rights reserved.
7 1.23.2.2 he *
8 1.23.2.2 he * Redistribution and use in source and binary forms, with or without
9 1.23.2.2 he * modification, are permitted provided that the following conditions
10 1.23.2.2 he * are met:
11 1.23.2.2 he * 1. Redistributions of source code must retain the above copyright
12 1.23.2.2 he * notice, this list of conditions and the following disclaimer.
13 1.23.2.2 he * 2. Redistributions in binary form must reproduce the above copyright
14 1.23.2.2 he * notice, this list of conditions and the following disclaimer in the
15 1.23.2.2 he * documentation and/or other materials provided with the distribution.
16 1.23.2.2 he * 3. Neither the name of the project nor the names of its contributors
17 1.23.2.2 he * may be used to endorse or promote products derived from this software
18 1.23.2.2 he * without specific prior written permission.
19 1.23.2.2 he *
20 1.23.2.2 he * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 1.23.2.2 he * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 1.23.2.2 he * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 1.23.2.2 he * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 1.23.2.2 he * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 1.23.2.2 he * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 1.23.2.2 he * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 1.23.2.2 he * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 1.23.2.2 he * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 1.23.2.2 he * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 1.23.2.2 he * SUCH DAMAGE.
31 1.23.2.2 he */
32 1.23.2.2 he
33 1.23.2.2 he /*
34 1.23.2.2 he * Copyright (c) 1982, 1986, 1988, 1990, 1993
35 1.23.2.2 he * The Regents of the University of California. All rights reserved.
36 1.23.2.2 he *
37 1.23.2.2 he * Redistribution and use in source and binary forms, with or without
38 1.23.2.2 he * modification, are permitted provided that the following conditions
39 1.23.2.2 he * are met:
40 1.23.2.2 he * 1. Redistributions of source code must retain the above copyright
41 1.23.2.2 he * notice, this list of conditions and the following disclaimer.
42 1.23.2.2 he * 2. Redistributions in binary form must reproduce the above copyright
43 1.23.2.2 he * notice, this list of conditions and the following disclaimer in the
44 1.23.2.2 he * documentation and/or other materials provided with the distribution.
45 1.23.2.2 he * 3. All advertising materials mentioning features or use of this software
46 1.23.2.2 he * must display the following acknowledgement:
47 1.23.2.2 he * This product includes software developed by the University of
48 1.23.2.2 he * California, Berkeley and its contributors.
49 1.23.2.2 he * 4. Neither the name of the University nor the names of its contributors
50 1.23.2.2 he * may be used to endorse or promote products derived from this software
51 1.23.2.2 he * without specific prior written permission.
52 1.23.2.2 he *
53 1.23.2.2 he * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
54 1.23.2.2 he * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
55 1.23.2.2 he * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
56 1.23.2.2 he * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
57 1.23.2.2 he * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
58 1.23.2.2 he * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
59 1.23.2.2 he * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
60 1.23.2.2 he * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
61 1.23.2.2 he * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
62 1.23.2.2 he * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
63 1.23.2.2 he * SUCH DAMAGE.
64 1.23.2.2 he *
65 1.23.2.2 he * @(#)ip_output.c 8.3 (Berkeley) 1/21/94
66 1.23.2.2 he */
67 1.23.2.2 he
68 1.23.2.2 he #include "opt_inet.h"
69 1.23.2.2 he #include "opt_ipsec.h"
70 1.23.2.2 he #include "opt_pfil_hooks.h"
71 1.23.2.2 he
72 1.23.2.2 he #include <sys/param.h>
73 1.23.2.2 he #include <sys/malloc.h>
74 1.23.2.2 he #include <sys/mbuf.h>
75 1.23.2.2 he #include <sys/errno.h>
76 1.23.2.2 he #include <sys/protosw.h>
77 1.23.2.2 he #include <sys/socket.h>
78 1.23.2.2 he #include <sys/socketvar.h>
79 1.23.2.2 he #include <sys/systm.h>
80 1.23.2.2 he #include <sys/proc.h>
81 1.23.2.2 he
82 1.23.2.2 he #include <net/if.h>
83 1.23.2.2 he #include <net/route.h>
84 1.23.2.2 he #ifdef PFIL_HOOKS
85 1.23.2.2 he #include <net/pfil.h>
86 1.23.2.2 he #endif
87 1.23.2.2 he
88 1.23.2.2 he #include <netinet/in.h>
89 1.23.2.2 he #include <netinet/in_var.h>
90 1.23.2.2 he #include <netinet/ip6.h>
91 1.23.2.2 he #include <netinet/icmp6.h>
92 1.23.2.2 he #include <netinet6/ip6_var.h>
93 1.23.2.2 he #include <netinet6/in6_pcb.h>
94 1.23.2.2 he #include <netinet6/nd6.h>
95 1.23.2.6 he #include <netinet6/ip6protosw.h>
96 1.23.2.2 he
97 1.23.2.2 he #ifdef IPSEC
98 1.23.2.2 he #include <netinet6/ipsec.h>
99 1.23.2.2 he #include <netkey/key.h>
100 1.23.2.2 he #include <netkey/key_debug.h>
101 1.23.2.2 he #endif /* IPSEC */
102 1.23.2.2 he
103 1.23.2.2 he #include "loop.h"
104 1.23.2.2 he
105 1.23.2.2 he #include <net/net_osdep.h>
106 1.23.2.2 he
107 1.23.2.2 he #ifdef IPV6FIREWALL
108 1.23.2.2 he #include <netinet6/ip6_fw.h>
109 1.23.2.2 he #endif
110 1.23.2.2 he
111 1.23.2.2 he struct ip6_exthdrs {
112 1.23.2.2 he struct mbuf *ip6e_ip6;
113 1.23.2.2 he struct mbuf *ip6e_hbh;
114 1.23.2.2 he struct mbuf *ip6e_dest1;
115 1.23.2.2 he struct mbuf *ip6e_rthdr;
116 1.23.2.2 he struct mbuf *ip6e_dest2;
117 1.23.2.2 he };
118 1.23.2.2 he
119 1.23.2.2 he static int ip6_pcbopts __P((struct ip6_pktopts **, struct mbuf *,
120 1.23.2.2 he struct socket *));
121 1.23.2.2 he static int ip6_setmoptions __P((int, struct ip6_moptions **, struct mbuf *));
122 1.23.2.2 he static int ip6_getmoptions __P((int, struct ip6_moptions *, struct mbuf **));
123 1.23.2.2 he static int ip6_copyexthdr __P((struct mbuf **, caddr_t, int));
124 1.23.2.2 he static int ip6_insertfraghdr __P((struct mbuf *, struct mbuf *, int,
125 1.23.2.2 he struct ip6_frag **));
126 1.23.2.2 he static int ip6_insert_jumboopt __P((struct ip6_exthdrs *, u_int32_t));
127 1.23.2.2 he static int ip6_splithdr __P((struct mbuf *, struct ip6_exthdrs *));
128 1.23.2.2 he
129 1.23.2.2 he extern struct ifnet loif[NLOOP];
130 1.23.2.2 he
131 1.23.2.2 he /*
132 1.23.2.2 he * IP6 output. The packet in mbuf chain m contains a skeletal IP6
133 1.23.2.2 he * header (with pri, len, nxt, hlim, src, dst).
134 1.23.2.2 he * This function may modify ver and hlim only.
135 1.23.2.2 he * The mbuf chain containing the packet will be freed.
136 1.23.2.2 he * The mbuf opt, if present, will not be freed.
137 1.23.2.2 he */
138 1.23.2.2 he int
139 1.23.2.2 he ip6_output(m0, opt, ro, flags, im6o, ifpp)
140 1.23.2.2 he struct mbuf *m0;
141 1.23.2.2 he struct ip6_pktopts *opt;
142 1.23.2.2 he struct route_in6 *ro;
143 1.23.2.2 he int flags;
144 1.23.2.2 he struct ip6_moptions *im6o;
145 1.23.2.2 he struct ifnet **ifpp; /* XXX: just for statistics */
146 1.23.2.2 he {
147 1.23.2.2 he struct ip6_hdr *ip6, *mhip6;
148 1.23.2.2 he struct ifnet *ifp, *origifp;
149 1.23.2.2 he struct mbuf *m = m0;
150 1.23.2.2 he int hlen, tlen, len, off;
151 1.23.2.2 he struct route_in6 ip6route;
152 1.23.2.2 he struct sockaddr_in6 *dst;
153 1.23.2.2 he int error = 0;
154 1.23.2.2 he struct in6_ifaddr *ia;
155 1.23.2.2 he u_long mtu;
156 1.23.2.2 he u_int32_t optlen = 0, plen = 0, unfragpartlen = 0;
157 1.23.2.2 he struct ip6_exthdrs exthdrs;
158 1.23.2.2 he struct in6_addr finaldst;
159 1.23.2.2 he struct route_in6 *ro_pmtu = NULL;
160 1.23.2.2 he int hdrsplit = 0;
161 1.23.2.2 he int needipsec = 0;
162 1.23.2.2 he #ifdef PFIL_HOOKS
163 1.23.2.2 he struct packet_filter_hook *pfh;
164 1.23.2.2 he struct mbuf *m1;
165 1.23.2.2 he int rv;
166 1.23.2.2 he #endif /* PFIL_HOOKS */
167 1.23.2.2 he #ifdef IPSEC
168 1.23.2.2 he int needipsectun = 0;
169 1.23.2.2 he struct socket *so;
170 1.23.2.2 he struct secpolicy *sp = NULL;
171 1.23.2.2 he
172 1.23.2.2 he /* for AH processing. stupid to have "socket" variable in IP layer... */
173 1.23.2.2 he so = ipsec_getsocket(m);
174 1.23.2.4 he (void)ipsec_setsocket(m, NULL);
175 1.23.2.2 he ip6 = mtod(m, struct ip6_hdr *);
176 1.23.2.2 he #endif /* IPSEC */
177 1.23.2.2 he
178 1.23.2.2 he #define MAKE_EXTHDR(hp, mp) \
179 1.23.2.2 he do { \
180 1.23.2.2 he if (hp) { \
181 1.23.2.2 he struct ip6_ext *eh = (struct ip6_ext *)(hp); \
182 1.23.2.2 he error = ip6_copyexthdr((mp), (caddr_t)(hp), \
183 1.23.2.2 he ((eh)->ip6e_len + 1) << 3); \
184 1.23.2.2 he if (error) \
185 1.23.2.2 he goto freehdrs; \
186 1.23.2.2 he } \
187 1.23.2.2 he } while (0)
188 1.23.2.2 he
189 1.23.2.2 he bzero(&exthdrs, sizeof(exthdrs));
190 1.23.2.2 he if (opt) {
191 1.23.2.2 he /* Hop-by-Hop options header */
192 1.23.2.2 he MAKE_EXTHDR(opt->ip6po_hbh, &exthdrs.ip6e_hbh);
193 1.23.2.2 he /* Destination options header(1st part) */
194 1.23.2.2 he MAKE_EXTHDR(opt->ip6po_dest1, &exthdrs.ip6e_dest1);
195 1.23.2.2 he /* Routing header */
196 1.23.2.2 he MAKE_EXTHDR(opt->ip6po_rthdr, &exthdrs.ip6e_rthdr);
197 1.23.2.2 he /* Destination options header(2nd part) */
198 1.23.2.2 he MAKE_EXTHDR(opt->ip6po_dest2, &exthdrs.ip6e_dest2);
199 1.23.2.2 he }
200 1.23.2.2 he
201 1.23.2.2 he #ifdef IPSEC
202 1.23.2.2 he /* get a security policy for this packet */
203 1.23.2.2 he if (so == NULL)
204 1.23.2.2 he sp = ipsec6_getpolicybyaddr(m, IPSEC_DIR_OUTBOUND, 0, &error);
205 1.23.2.2 he else
206 1.23.2.2 he sp = ipsec6_getpolicybysock(m, IPSEC_DIR_OUTBOUND, so, &error);
207 1.23.2.2 he
208 1.23.2.2 he if (sp == NULL) {
209 1.23.2.2 he ipsec6stat.out_inval++;
210 1.23.2.2 he goto freehdrs;
211 1.23.2.2 he }
212 1.23.2.2 he
213 1.23.2.2 he error = 0;
214 1.23.2.2 he
215 1.23.2.2 he /* check policy */
216 1.23.2.2 he switch (sp->policy) {
217 1.23.2.2 he case IPSEC_POLICY_DISCARD:
218 1.23.2.2 he /*
219 1.23.2.2 he * This packet is just discarded.
220 1.23.2.2 he */
221 1.23.2.2 he ipsec6stat.out_polvio++;
222 1.23.2.2 he goto freehdrs;
223 1.23.2.2 he
224 1.23.2.2 he case IPSEC_POLICY_BYPASS:
225 1.23.2.2 he case IPSEC_POLICY_NONE:
226 1.23.2.2 he /* no need to do IPsec. */
227 1.23.2.2 he needipsec = 0;
228 1.23.2.2 he break;
229 1.23.2.2 he
230 1.23.2.2 he case IPSEC_POLICY_IPSEC:
231 1.23.2.2 he if (sp->req == NULL) {
232 1.23.2.2 he /* XXX should be panic ? */
233 1.23.2.2 he printf("ip6_output: No IPsec request specified.\n");
234 1.23.2.2 he error = EINVAL;
235 1.23.2.2 he goto freehdrs;
236 1.23.2.2 he }
237 1.23.2.2 he needipsec = 1;
238 1.23.2.2 he break;
239 1.23.2.2 he
240 1.23.2.2 he case IPSEC_POLICY_ENTRUST:
241 1.23.2.2 he default:
242 1.23.2.2 he printf("ip6_output: Invalid policy found. %d\n", sp->policy);
243 1.23.2.2 he }
244 1.23.2.2 he #endif /* IPSEC */
245 1.23.2.2 he
246 1.23.2.2 he /*
247 1.23.2.2 he * Calculate the total length of the extension header chain.
248 1.23.2.2 he * Keep the length of the unfragmentable part for fragmentation.
249 1.23.2.2 he */
250 1.23.2.2 he optlen = 0;
251 1.23.2.2 he if (exthdrs.ip6e_hbh) optlen += exthdrs.ip6e_hbh->m_len;
252 1.23.2.2 he if (exthdrs.ip6e_dest1) optlen += exthdrs.ip6e_dest1->m_len;
253 1.23.2.2 he if (exthdrs.ip6e_rthdr) optlen += exthdrs.ip6e_rthdr->m_len;
254 1.23.2.2 he unfragpartlen = optlen + sizeof(struct ip6_hdr);
255 1.23.2.2 he /* NOTE: we don't add AH/ESP length here. do that later. */
256 1.23.2.2 he if (exthdrs.ip6e_dest2) optlen += exthdrs.ip6e_dest2->m_len;
257 1.23.2.2 he
258 1.23.2.2 he /*
259 1.23.2.2 he * If we need IPsec, or there is at least one extension header,
260 1.23.2.2 he * separate IP6 header from the payload.
261 1.23.2.2 he */
262 1.23.2.2 he if ((needipsec || optlen) && !hdrsplit) {
263 1.23.2.2 he if ((error = ip6_splithdr(m, &exthdrs)) != 0) {
264 1.23.2.2 he m = NULL;
265 1.23.2.2 he goto freehdrs;
266 1.23.2.2 he }
267 1.23.2.2 he m = exthdrs.ip6e_ip6;
268 1.23.2.2 he hdrsplit++;
269 1.23.2.2 he }
270 1.23.2.2 he
271 1.23.2.2 he /* adjust pointer */
272 1.23.2.2 he ip6 = mtod(m, struct ip6_hdr *);
273 1.23.2.2 he
274 1.23.2.2 he /* adjust mbuf packet header length */
275 1.23.2.2 he m->m_pkthdr.len += optlen;
276 1.23.2.2 he plen = m->m_pkthdr.len - sizeof(*ip6);
277 1.23.2.2 he
278 1.23.2.2 he /* If this is a jumbo payload, insert a jumbo payload option. */
279 1.23.2.2 he if (plen > IPV6_MAXPACKET) {
280 1.23.2.2 he if (!hdrsplit) {
281 1.23.2.2 he if ((error = ip6_splithdr(m, &exthdrs)) != 0) {
282 1.23.2.2 he m = NULL;
283 1.23.2.2 he goto freehdrs;
284 1.23.2.2 he }
285 1.23.2.2 he m = exthdrs.ip6e_ip6;
286 1.23.2.2 he hdrsplit++;
287 1.23.2.2 he }
288 1.23.2.2 he /* adjust pointer */
289 1.23.2.2 he ip6 = mtod(m, struct ip6_hdr *);
290 1.23.2.2 he if ((error = ip6_insert_jumboopt(&exthdrs, plen)) != 0)
291 1.23.2.2 he goto freehdrs;
292 1.23.2.2 he ip6->ip6_plen = 0;
293 1.23.2.2 he } else
294 1.23.2.2 he ip6->ip6_plen = htons(plen);
295 1.23.2.2 he
296 1.23.2.2 he /*
297 1.23.2.2 he * Concatenate headers and fill in next header fields.
298 1.23.2.2 he * Here we have, on "m"
299 1.23.2.2 he * IPv6 payload
300 1.23.2.2 he * and we insert headers accordingly. Finally, we should be getting:
301 1.23.2.2 he * IPv6 hbh dest1 rthdr ah* [esp* dest2 payload]
302 1.23.2.2 he *
303 1.23.2.2 he * during the header composing process, "m" points to IPv6 header.
304 1.23.2.2 he * "mprev" points to an extension header prior to esp.
305 1.23.2.2 he */
306 1.23.2.2 he {
307 1.23.2.2 he u_char *nexthdrp = &ip6->ip6_nxt;
308 1.23.2.2 he struct mbuf *mprev = m;
309 1.23.2.2 he
310 1.23.2.2 he /*
311 1.23.2.2 he * we treat dest2 specially. this makes IPsec processing
312 1.23.2.2 he * much easier.
313 1.23.2.2 he *
314 1.23.2.2 he * result: IPv6 dest2 payload
315 1.23.2.2 he * m and mprev will point to IPv6 header.
316 1.23.2.2 he */
317 1.23.2.2 he if (exthdrs.ip6e_dest2) {
318 1.23.2.2 he if (!hdrsplit)
319 1.23.2.2 he panic("assumption failed: hdr not split");
320 1.23.2.2 he exthdrs.ip6e_dest2->m_next = m->m_next;
321 1.23.2.2 he m->m_next = exthdrs.ip6e_dest2;
322 1.23.2.2 he *mtod(exthdrs.ip6e_dest2, u_char *) = ip6->ip6_nxt;
323 1.23.2.2 he ip6->ip6_nxt = IPPROTO_DSTOPTS;
324 1.23.2.2 he }
325 1.23.2.2 he
326 1.23.2.2 he #define MAKE_CHAIN(m, mp, p, i)\
327 1.23.2.2 he do {\
328 1.23.2.2 he if (m) {\
329 1.23.2.2 he if (!hdrsplit) \
330 1.23.2.2 he panic("assumption failed: hdr not split"); \
331 1.23.2.2 he *mtod((m), u_char *) = *(p);\
332 1.23.2.2 he *(p) = (i);\
333 1.23.2.2 he p = mtod((m), u_char *);\
334 1.23.2.2 he (m)->m_next = (mp)->m_next;\
335 1.23.2.2 he (mp)->m_next = (m);\
336 1.23.2.2 he (mp) = (m);\
337 1.23.2.2 he }\
338 1.23.2.2 he } while (0)
339 1.23.2.2 he /*
340 1.23.2.2 he * result: IPv6 hbh dest1 rthdr dest2 payload
341 1.23.2.2 he * m will point to IPv6 header. mprev will point to the
342 1.23.2.2 he * extension header prior to dest2 (rthdr in the above case).
343 1.23.2.2 he */
344 1.23.2.2 he MAKE_CHAIN(exthdrs.ip6e_hbh, mprev,
345 1.23.2.2 he nexthdrp, IPPROTO_HOPOPTS);
346 1.23.2.2 he MAKE_CHAIN(exthdrs.ip6e_dest1, mprev,
347 1.23.2.2 he nexthdrp, IPPROTO_DSTOPTS);
348 1.23.2.2 he MAKE_CHAIN(exthdrs.ip6e_rthdr, mprev,
349 1.23.2.2 he nexthdrp, IPPROTO_ROUTING);
350 1.23.2.2 he
351 1.23.2.2 he #ifdef IPSEC
352 1.23.2.2 he if (!needipsec)
353 1.23.2.2 he goto skip_ipsec2;
354 1.23.2.2 he
355 1.23.2.2 he /*
356 1.23.2.2 he * pointers after IPsec headers are not valid any more.
357 1.23.2.2 he * other pointers need a great care too.
358 1.23.2.2 he * (IPsec routines should not mangle mbufs prior to AH/ESP)
359 1.23.2.2 he */
360 1.23.2.2 he exthdrs.ip6e_dest2 = NULL;
361 1.23.2.2 he
362 1.23.2.2 he {
363 1.23.2.2 he struct ip6_rthdr *rh = NULL;
364 1.23.2.2 he int segleft_org = 0;
365 1.23.2.2 he struct ipsec_output_state state;
366 1.23.2.2 he
367 1.23.2.2 he if (exthdrs.ip6e_rthdr) {
368 1.23.2.2 he rh = mtod(exthdrs.ip6e_rthdr, struct ip6_rthdr *);
369 1.23.2.2 he segleft_org = rh->ip6r_segleft;
370 1.23.2.2 he rh->ip6r_segleft = 0;
371 1.23.2.2 he }
372 1.23.2.2 he
373 1.23.2.2 he bzero(&state, sizeof(state));
374 1.23.2.2 he state.m = m;
375 1.23.2.2 he error = ipsec6_output_trans(&state, nexthdrp, mprev, sp, flags,
376 1.23.2.2 he &needipsectun);
377 1.23.2.2 he m = state.m;
378 1.23.2.2 he if (error) {
379 1.23.2.2 he /* mbuf is already reclaimed in ipsec6_output_trans. */
380 1.23.2.2 he m = NULL;
381 1.23.2.2 he switch (error) {
382 1.23.2.2 he case EHOSTUNREACH:
383 1.23.2.2 he case ENETUNREACH:
384 1.23.2.2 he case EMSGSIZE:
385 1.23.2.2 he case ENOBUFS:
386 1.23.2.2 he case ENOMEM:
387 1.23.2.2 he break;
388 1.23.2.2 he default:
389 1.23.2.2 he printf("ip6_output (ipsec): error code %d\n", error);
390 1.23.2.2 he /*fall through*/
391 1.23.2.2 he case ENOENT:
392 1.23.2.2 he /* don't show these error codes to the user */
393 1.23.2.2 he error = 0;
394 1.23.2.2 he break;
395 1.23.2.2 he }
396 1.23.2.2 he goto bad;
397 1.23.2.2 he }
398 1.23.2.2 he if (exthdrs.ip6e_rthdr) {
399 1.23.2.2 he /* ah6_output doesn't modify mbuf chain */
400 1.23.2.2 he rh->ip6r_segleft = segleft_org;
401 1.23.2.2 he }
402 1.23.2.2 he }
403 1.23.2.2 he skip_ipsec2:;
404 1.23.2.2 he #endif
405 1.23.2.2 he }
406 1.23.2.2 he
407 1.23.2.2 he /*
408 1.23.2.2 he * If there is a routing header, replace destination address field
409 1.23.2.2 he * with the first hop of the routing header.
410 1.23.2.2 he */
411 1.23.2.2 he if (exthdrs.ip6e_rthdr) {
412 1.23.2.2 he struct ip6_rthdr *rh =
413 1.23.2.2 he (struct ip6_rthdr *)(mtod(exthdrs.ip6e_rthdr,
414 1.23.2.2 he struct ip6_rthdr *));
415 1.23.2.2 he struct ip6_rthdr0 *rh0;
416 1.23.2.2 he
417 1.23.2.2 he finaldst = ip6->ip6_dst;
418 1.23.2.2 he switch(rh->ip6r_type) {
419 1.23.2.2 he case IPV6_RTHDR_TYPE_0:
420 1.23.2.2 he rh0 = (struct ip6_rthdr0 *)rh;
421 1.23.2.2 he ip6->ip6_dst = rh0->ip6r0_addr[0];
422 1.23.2.2 he bcopy((caddr_t)&rh0->ip6r0_addr[1],
423 1.23.2.2 he (caddr_t)&rh0->ip6r0_addr[0],
424 1.23.2.2 he sizeof(struct in6_addr)*(rh0->ip6r0_segleft - 1)
425 1.23.2.2 he );
426 1.23.2.2 he rh0->ip6r0_addr[rh0->ip6r0_segleft - 1] = finaldst;
427 1.23.2.2 he break;
428 1.23.2.2 he default: /* is it possible? */
429 1.23.2.2 he error = EINVAL;
430 1.23.2.2 he goto bad;
431 1.23.2.2 he }
432 1.23.2.2 he }
433 1.23.2.2 he
434 1.23.2.2 he /* Source address validation */
435 1.23.2.2 he if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src) &&
436 1.23.2.2 he (flags & IPV6_DADOUTPUT) == 0) {
437 1.23.2.2 he error = EOPNOTSUPP;
438 1.23.2.2 he ip6stat.ip6s_badscope++;
439 1.23.2.2 he goto bad;
440 1.23.2.2 he }
441 1.23.2.2 he if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) {
442 1.23.2.2 he error = EOPNOTSUPP;
443 1.23.2.2 he ip6stat.ip6s_badscope++;
444 1.23.2.2 he goto bad;
445 1.23.2.2 he }
446 1.23.2.2 he
447 1.23.2.2 he ip6stat.ip6s_localout++;
448 1.23.2.2 he
449 1.23.2.2 he /*
450 1.23.2.2 he * Route packet.
451 1.23.2.2 he */
452 1.23.2.2 he if (ro == 0) {
453 1.23.2.2 he ro = &ip6route;
454 1.23.2.2 he bzero((caddr_t)ro, sizeof(*ro));
455 1.23.2.2 he }
456 1.23.2.2 he ro_pmtu = ro;
457 1.23.2.2 he if (opt && opt->ip6po_rthdr)
458 1.23.2.2 he ro = &opt->ip6po_route;
459 1.23.2.2 he dst = (struct sockaddr_in6 *)&ro->ro_dst;
460 1.23.2.2 he /*
461 1.23.2.2 he * If there is a cached route,
462 1.23.2.2 he * check that it is to the same destination
463 1.23.2.2 he * and is still up. If not, free it and try again.
464 1.23.2.2 he */
465 1.23.2.2 he if (ro->ro_rt && ((ro->ro_rt->rt_flags & RTF_UP) == 0 ||
466 1.23.2.2 he !IN6_ARE_ADDR_EQUAL(&dst->sin6_addr, &ip6->ip6_dst))) {
467 1.23.2.2 he RTFREE(ro->ro_rt);
468 1.23.2.2 he ro->ro_rt = (struct rtentry *)0;
469 1.23.2.2 he }
470 1.23.2.2 he if (ro->ro_rt == 0) {
471 1.23.2.2 he bzero(dst, sizeof(*dst));
472 1.23.2.2 he dst->sin6_family = AF_INET6;
473 1.23.2.2 he dst->sin6_len = sizeof(struct sockaddr_in6);
474 1.23.2.2 he dst->sin6_addr = ip6->ip6_dst;
475 1.23.2.2 he }
476 1.23.2.2 he #ifdef IPSEC
477 1.23.2.2 he if (needipsec && needipsectun) {
478 1.23.2.2 he struct ipsec_output_state state;
479 1.23.2.2 he
480 1.23.2.2 he /*
481 1.23.2.2 he * All the extension headers will become inaccessible
482 1.23.2.2 he * (since they can be encrypted).
483 1.23.2.2 he * Don't panic, we need no more updates to extension headers
484 1.23.2.2 he * on inner IPv6 packet (since they are now encapsulated).
485 1.23.2.2 he *
486 1.23.2.2 he * IPv6 [ESP|AH] IPv6 [extension headers] payload
487 1.23.2.2 he */
488 1.23.2.2 he bzero(&exthdrs, sizeof(exthdrs));
489 1.23.2.2 he exthdrs.ip6e_ip6 = m;
490 1.23.2.2 he
491 1.23.2.2 he bzero(&state, sizeof(state));
492 1.23.2.2 he state.m = m;
493 1.23.2.2 he state.ro = (struct route *)ro;
494 1.23.2.2 he state.dst = (struct sockaddr *)dst;
495 1.23.2.2 he
496 1.23.2.2 he error = ipsec6_output_tunnel(&state, sp, flags);
497 1.23.2.2 he
498 1.23.2.2 he m = state.m;
499 1.23.2.2 he ro = (struct route_in6 *)state.ro;
500 1.23.2.2 he dst = (struct sockaddr_in6 *)state.dst;
501 1.23.2.2 he if (error) {
502 1.23.2.2 he /* mbuf is already reclaimed in ipsec6_output_tunnel. */
503 1.23.2.2 he m0 = m = NULL;
504 1.23.2.2 he m = NULL;
505 1.23.2.2 he switch (error) {
506 1.23.2.2 he case EHOSTUNREACH:
507 1.23.2.2 he case ENETUNREACH:
508 1.23.2.2 he case EMSGSIZE:
509 1.23.2.2 he case ENOBUFS:
510 1.23.2.2 he case ENOMEM:
511 1.23.2.2 he break;
512 1.23.2.2 he default:
513 1.23.2.2 he printf("ip6_output (ipsec): error code %d\n", error);
514 1.23.2.2 he /*fall through*/
515 1.23.2.2 he case ENOENT:
516 1.23.2.2 he /* don't show these error codes to the user */
517 1.23.2.2 he error = 0;
518 1.23.2.2 he break;
519 1.23.2.2 he }
520 1.23.2.2 he goto bad;
521 1.23.2.2 he }
522 1.23.2.2 he
523 1.23.2.2 he exthdrs.ip6e_ip6 = m;
524 1.23.2.2 he }
525 1.23.2.2 he #endif /*IPSEC*/
526 1.23.2.2 he
527 1.23.2.2 he if (!IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
528 1.23.2.2 he /* Unicast */
529 1.23.2.2 he
530 1.23.2.2 he #define ifatoia6(ifa) ((struct in6_ifaddr *)(ifa))
531 1.23.2.2 he #define sin6tosa(sin6) ((struct sockaddr *)(sin6))
532 1.23.2.2 he /* xxx
533 1.23.2.2 he * interface selection comes here
534 1.23.2.2 he * if an interface is specified from an upper layer,
535 1.23.2.2 he * ifp must point it.
536 1.23.2.2 he */
537 1.23.2.2 he if (ro->ro_rt == 0) {
538 1.23.2.2 he /*
539 1.23.2.2 he * non-bsdi always clone routes, if parent is
540 1.23.2.2 he * PRF_CLONING.
541 1.23.2.2 he */
542 1.23.2.2 he rtalloc((struct route *)ro);
543 1.23.2.2 he }
544 1.23.2.2 he if (ro->ro_rt == 0) {
545 1.23.2.2 he ip6stat.ip6s_noroute++;
546 1.23.2.2 he error = EHOSTUNREACH;
547 1.23.2.2 he /* XXX in6_ifstat_inc(ifp, ifs6_out_discard); */
548 1.23.2.2 he goto bad;
549 1.23.2.2 he }
550 1.23.2.2 he ia = ifatoia6(ro->ro_rt->rt_ifa);
551 1.23.2.2 he ifp = ro->ro_rt->rt_ifp;
552 1.23.2.2 he ro->ro_rt->rt_use++;
553 1.23.2.2 he if (ro->ro_rt->rt_flags & RTF_GATEWAY)
554 1.23.2.2 he dst = (struct sockaddr_in6 *)ro->ro_rt->rt_gateway;
555 1.23.2.2 he m->m_flags &= ~(M_BCAST | M_MCAST); /* just in case */
556 1.23.2.2 he
557 1.23.2.2 he in6_ifstat_inc(ifp, ifs6_out_request);
558 1.23.2.2 he
559 1.23.2.2 he /*
560 1.23.2.2 he * Check if the outgoing interface conflicts with
561 1.23.2.2 he * the interface specified by ifi6_ifindex (if specified).
562 1.23.2.2 he * Note that loopback interface is always okay.
563 1.23.2.2 he * (this may happen when we are sending a packet to one of
564 1.23.2.2 he * our own addresses.)
565 1.23.2.2 he */
566 1.23.2.2 he if (opt && opt->ip6po_pktinfo
567 1.23.2.2 he && opt->ip6po_pktinfo->ipi6_ifindex) {
568 1.23.2.2 he if (!(ifp->if_flags & IFF_LOOPBACK)
569 1.23.2.2 he && ifp->if_index != opt->ip6po_pktinfo->ipi6_ifindex) {
570 1.23.2.2 he ip6stat.ip6s_noroute++;
571 1.23.2.2 he in6_ifstat_inc(ifp, ifs6_out_discard);
572 1.23.2.2 he error = EHOSTUNREACH;
573 1.23.2.2 he goto bad;
574 1.23.2.2 he }
575 1.23.2.2 he }
576 1.23.2.2 he
577 1.23.2.2 he if (opt && opt->ip6po_hlim != -1)
578 1.23.2.2 he ip6->ip6_hlim = opt->ip6po_hlim & 0xff;
579 1.23.2.2 he } else {
580 1.23.2.2 he /* Multicast */
581 1.23.2.2 he struct in6_multi *in6m;
582 1.23.2.2 he
583 1.23.2.2 he m->m_flags = (m->m_flags & ~M_BCAST) | M_MCAST;
584 1.23.2.2 he
585 1.23.2.2 he /*
586 1.23.2.2 he * See if the caller provided any multicast options
587 1.23.2.2 he */
588 1.23.2.2 he ifp = NULL;
589 1.23.2.2 he if (im6o != NULL) {
590 1.23.2.2 he ip6->ip6_hlim = im6o->im6o_multicast_hlim;
591 1.23.2.2 he if (im6o->im6o_multicast_ifp != NULL)
592 1.23.2.2 he ifp = im6o->im6o_multicast_ifp;
593 1.23.2.2 he } else
594 1.23.2.2 he ip6->ip6_hlim = ip6_defmcasthlim;
595 1.23.2.2 he
596 1.23.2.2 he /*
597 1.23.2.2 he * See if the caller provided the outgoing interface
598 1.23.2.2 he * as an ancillary data.
599 1.23.2.2 he * Boundary check for ifindex is assumed to be already done.
600 1.23.2.2 he */
601 1.23.2.2 he if (opt && opt->ip6po_pktinfo && opt->ip6po_pktinfo->ipi6_ifindex)
602 1.23.2.2 he ifp = ifindex2ifnet[opt->ip6po_pktinfo->ipi6_ifindex];
603 1.23.2.2 he
604 1.23.2.2 he /*
605 1.23.2.2 he * If the destination is a node-local scope multicast,
606 1.23.2.2 he * the packet should be loop-backed only.
607 1.23.2.2 he */
608 1.23.2.2 he if (IN6_IS_ADDR_MC_NODELOCAL(&ip6->ip6_dst)) {
609 1.23.2.2 he /*
610 1.23.2.2 he * If the outgoing interface is already specified,
611 1.23.2.2 he * it should be a loopback interface.
612 1.23.2.2 he */
613 1.23.2.2 he if (ifp && (ifp->if_flags & IFF_LOOPBACK) == 0) {
614 1.23.2.2 he ip6stat.ip6s_badscope++;
615 1.23.2.2 he error = ENETUNREACH; /* XXX: better error? */
616 1.23.2.2 he /* XXX correct ifp? */
617 1.23.2.2 he in6_ifstat_inc(ifp, ifs6_out_discard);
618 1.23.2.2 he goto bad;
619 1.23.2.2 he } else {
620 1.23.2.2 he ifp = &loif[0];
621 1.23.2.2 he }
622 1.23.2.2 he }
623 1.23.2.2 he
624 1.23.2.2 he if (opt && opt->ip6po_hlim != -1)
625 1.23.2.2 he ip6->ip6_hlim = opt->ip6po_hlim & 0xff;
626 1.23.2.2 he
627 1.23.2.2 he /*
628 1.23.2.2 he * If caller did not provide an interface lookup a
629 1.23.2.2 he * default in the routing table. This is either a
630 1.23.2.2 he * default for the speicfied group (i.e. a host
631 1.23.2.2 he * route), or a multicast default (a route for the
632 1.23.2.2 he * ``net'' ff00::/8).
633 1.23.2.2 he */
634 1.23.2.2 he if (ifp == NULL) {
635 1.23.2.2 he if (ro->ro_rt == 0) {
636 1.23.2.2 he ro->ro_rt = rtalloc1((struct sockaddr *)
637 1.23.2.2 he &ro->ro_dst, 0
638 1.23.2.2 he );
639 1.23.2.2 he }
640 1.23.2.2 he if (ro->ro_rt == 0) {
641 1.23.2.2 he ip6stat.ip6s_noroute++;
642 1.23.2.2 he error = EHOSTUNREACH;
643 1.23.2.2 he /* XXX in6_ifstat_inc(ifp, ifs6_out_discard) */
644 1.23.2.2 he goto bad;
645 1.23.2.2 he }
646 1.23.2.2 he ia = ifatoia6(ro->ro_rt->rt_ifa);
647 1.23.2.2 he ifp = ro->ro_rt->rt_ifp;
648 1.23.2.2 he ro->ro_rt->rt_use++;
649 1.23.2.2 he }
650 1.23.2.2 he
651 1.23.2.2 he if ((flags & IPV6_FORWARDING) == 0)
652 1.23.2.2 he in6_ifstat_inc(ifp, ifs6_out_request);
653 1.23.2.2 he in6_ifstat_inc(ifp, ifs6_out_mcast);
654 1.23.2.2 he
655 1.23.2.2 he /*
656 1.23.2.2 he * Confirm that the outgoing interface supports multicast.
657 1.23.2.2 he */
658 1.23.2.2 he if ((ifp->if_flags & IFF_MULTICAST) == 0) {
659 1.23.2.2 he ip6stat.ip6s_noroute++;
660 1.23.2.2 he in6_ifstat_inc(ifp, ifs6_out_discard);
661 1.23.2.2 he error = ENETUNREACH;
662 1.23.2.2 he goto bad;
663 1.23.2.2 he }
664 1.23.2.2 he IN6_LOOKUP_MULTI(ip6->ip6_dst, ifp, in6m);
665 1.23.2.2 he if (in6m != NULL &&
666 1.23.2.2 he (im6o == NULL || im6o->im6o_multicast_loop)) {
667 1.23.2.2 he /*
668 1.23.2.2 he * If we belong to the destination multicast group
669 1.23.2.2 he * on the outgoing interface, and the caller did not
670 1.23.2.2 he * forbid loopback, loop back a copy.
671 1.23.2.2 he */
672 1.23.2.2 he ip6_mloopback(ifp, m, dst);
673 1.23.2.2 he } else {
674 1.23.2.2 he /*
675 1.23.2.2 he * If we are acting as a multicast router, perform
676 1.23.2.2 he * multicast forwarding as if the packet had just
677 1.23.2.2 he * arrived on the interface to which we are about
678 1.23.2.2 he * to send. The multicast forwarding function
679 1.23.2.2 he * recursively calls this function, using the
680 1.23.2.2 he * IPV6_FORWARDING flag to prevent infinite recursion.
681 1.23.2.2 he *
682 1.23.2.2 he * Multicasts that are looped back by ip6_mloopback(),
683 1.23.2.2 he * above, will be forwarded by the ip6_input() routine,
684 1.23.2.2 he * if necessary.
685 1.23.2.2 he */
686 1.23.2.2 he if (ip6_mrouter && (flags & IPV6_FORWARDING) == 0) {
687 1.23.2.2 he if (ip6_mforward(ip6, ifp, m) != 0) {
688 1.23.2.2 he m_freem(m);
689 1.23.2.2 he goto done;
690 1.23.2.2 he }
691 1.23.2.2 he }
692 1.23.2.2 he }
693 1.23.2.2 he /*
694 1.23.2.2 he * Multicasts with a hoplimit of zero may be looped back,
695 1.23.2.2 he * above, but must not be transmitted on a network.
696 1.23.2.2 he * Also, multicasts addressed to the loopback interface
697 1.23.2.2 he * are not sent -- the above call to ip6_mloopback() will
698 1.23.2.2 he * loop back a copy if this host actually belongs to the
699 1.23.2.2 he * destination group on the loopback interface.
700 1.23.2.2 he */
701 1.23.2.2 he if (ip6->ip6_hlim == 0 || (ifp->if_flags & IFF_LOOPBACK)) {
702 1.23.2.2 he m_freem(m);
703 1.23.2.2 he goto done;
704 1.23.2.2 he }
705 1.23.2.2 he }
706 1.23.2.2 he
707 1.23.2.2 he /*
708 1.23.2.2 he * Fill the outgoing inteface to tell the upper layer
709 1.23.2.2 he * to increment per-interface statistics.
710 1.23.2.2 he */
711 1.23.2.2 he if (ifpp)
712 1.23.2.2 he *ifpp = ifp;
713 1.23.2.2 he
714 1.23.2.2 he /*
715 1.23.2.2 he * Determine path MTU.
716 1.23.2.2 he */
717 1.23.2.2 he if (ro_pmtu != ro) {
718 1.23.2.2 he /* The first hop and the final destination may differ. */
719 1.23.2.2 he struct sockaddr_in6 *sin6_fin =
720 1.23.2.2 he (struct sockaddr_in6 *)&ro_pmtu->ro_dst;
721 1.23.2.2 he if (ro_pmtu->ro_rt && ((ro->ro_rt->rt_flags & RTF_UP) == 0 ||
722 1.23.2.2 he !IN6_ARE_ADDR_EQUAL(&sin6_fin->sin6_addr,
723 1.23.2.2 he &finaldst))) {
724 1.23.2.2 he RTFREE(ro_pmtu->ro_rt);
725 1.23.2.2 he ro_pmtu->ro_rt = (struct rtentry *)0;
726 1.23.2.2 he }
727 1.23.2.2 he if (ro_pmtu->ro_rt == 0) {
728 1.23.2.2 he bzero(sin6_fin, sizeof(*sin6_fin));
729 1.23.2.2 he sin6_fin->sin6_family = AF_INET6;
730 1.23.2.2 he sin6_fin->sin6_len = sizeof(struct sockaddr_in6);
731 1.23.2.2 he sin6_fin->sin6_addr = finaldst;
732 1.23.2.2 he
733 1.23.2.2 he rtalloc((struct route *)ro_pmtu);
734 1.23.2.2 he }
735 1.23.2.2 he }
736 1.23.2.2 he if (ro_pmtu->ro_rt != NULL) {
737 1.23.2.2 he u_int32_t ifmtu = nd_ifinfo[ifp->if_index].linkmtu;
738 1.23.2.2 he
739 1.23.2.2 he mtu = ro_pmtu->ro_rt->rt_rmx.rmx_mtu;
740 1.23.2.2 he if (mtu > ifmtu) {
741 1.23.2.2 he /*
742 1.23.2.2 he * The MTU on the route is larger than the MTU on
743 1.23.2.2 he * the interface! This shouldn't happen, unless the
744 1.23.2.2 he * MTU of the interface has been changed after the
745 1.23.2.2 he * interface was brought up. Change the MTU in the
746 1.23.2.2 he * route to match the interface MTU (as long as the
747 1.23.2.2 he * field isn't locked).
748 1.23.2.2 he */
749 1.23.2.2 he mtu = ifmtu;
750 1.23.2.2 he if ((ro_pmtu->ro_rt->rt_rmx.rmx_locks & RTV_MTU) == 0)
751 1.23.2.2 he ro_pmtu->ro_rt->rt_rmx.rmx_mtu = mtu; /* XXX */
752 1.23.2.2 he }
753 1.23.2.2 he } else {
754 1.23.2.2 he mtu = nd_ifinfo[ifp->if_index].linkmtu;
755 1.23.2.2 he }
756 1.23.2.2 he
757 1.23.2.2 he /* Fake scoped addresses */
758 1.23.2.2 he if ((ifp->if_flags & IFF_LOOPBACK) != 0) {
759 1.23.2.2 he /*
760 1.23.2.2 he * If source or destination address is a scoped address, and
761 1.23.2.2 he * the packet is going to be sent to a loopback interface,
762 1.23.2.2 he * we should keep the original interface.
763 1.23.2.2 he */
764 1.23.2.2 he
765 1.23.2.2 he /*
766 1.23.2.2 he * XXX: this is a very experimental and temporary solution.
767 1.23.2.2 he * We eventually have sockaddr_in6 and use the sin6_scope_id
768 1.23.2.2 he * field of the structure here.
769 1.23.2.2 he * We rely on the consistency between two scope zone ids
770 1.23.2.2 he * of source add destination, which should already be assured
771 1.23.2.2 he * Larger scopes than link will be supported in the near
772 1.23.2.2 he * future.
773 1.23.2.2 he */
774 1.23.2.2 he origifp = NULL;
775 1.23.2.2 he if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src))
776 1.23.2.2 he origifp = ifindex2ifnet[ntohs(ip6->ip6_src.s6_addr16[1])];
777 1.23.2.2 he else if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst))
778 1.23.2.2 he origifp = ifindex2ifnet[ntohs(ip6->ip6_dst.s6_addr16[1])];
779 1.23.2.2 he /*
780 1.23.2.2 he * XXX: origifp can be NULL even in those two cases above.
781 1.23.2.2 he * For example, if we remove the (only) link-local address
782 1.23.2.2 he * from the loopback interface, and try to send a link-local
783 1.23.2.2 he * address without link-id information. Then the source
784 1.23.2.2 he * address is ::1, and the destination address is the
785 1.23.2.2 he * link-local address with its s6_addr16[1] being zero.
786 1.23.2.2 he * What is worse, if the packet goes to the loopback interface
787 1.23.2.2 he * by a default rejected route, the null pointer would be
788 1.23.2.2 he * passed to looutput, and the kernel would hang.
789 1.23.2.2 he * The following last resort would prevent such disaster.
790 1.23.2.2 he */
791 1.23.2.3 he if (origifp == NULL)
792 1.23.2.2 he origifp = ifp;
793 1.23.2.2 he }
794 1.23.2.2 he else
795 1.23.2.2 he origifp = ifp;
796 1.23.2.2 he #ifndef FAKE_LOOPBACK_IF
797 1.23.2.2 he if ((ifp->if_flags & IFF_LOOPBACK) == 0)
798 1.23.2.2 he #else
799 1.23.2.2 he if (1)
800 1.23.2.2 he #endif
801 1.23.2.2 he {
802 1.23.2.2 he if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src))
803 1.23.2.2 he ip6->ip6_src.s6_addr16[1] = 0;
804 1.23.2.2 he if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst))
805 1.23.2.2 he ip6->ip6_dst.s6_addr16[1] = 0;
806 1.23.2.2 he }
807 1.23.2.2 he
808 1.23.2.2 he /*
809 1.23.2.2 he * If the outgoing packet contains a hop-by-hop options header,
810 1.23.2.2 he * it must be examined and processed even by the source node.
811 1.23.2.2 he * (RFC 2460, section 4.)
812 1.23.2.2 he */
813 1.23.2.2 he if (exthdrs.ip6e_hbh) {
814 1.23.2.2 he struct ip6_hbh *hbh = mtod(exthdrs.ip6e_hbh,
815 1.23.2.2 he struct ip6_hbh *);
816 1.23.2.2 he u_int32_t dummy1; /* XXX unused */
817 1.23.2.2 he u_int32_t dummy2; /* XXX unused */
818 1.23.2.2 he
819 1.23.2.2 he /*
820 1.23.2.2 he * XXX: if we have to send an ICMPv6 error to the sender,
821 1.23.2.2 he * we need the M_LOOP flag since icmp6_error() expects
822 1.23.2.2 he * the IPv6 and the hop-by-hop options header are
823 1.23.2.2 he * continuous unless the flag is set.
824 1.23.2.2 he */
825 1.23.2.2 he m->m_flags |= M_LOOP;
826 1.23.2.2 he m->m_pkthdr.rcvif = ifp;
827 1.23.2.2 he if (ip6_process_hopopts(m,
828 1.23.2.2 he (u_int8_t *)(hbh + 1),
829 1.23.2.2 he ((hbh->ip6h_len + 1) << 3) -
830 1.23.2.2 he sizeof(struct ip6_hbh),
831 1.23.2.2 he &dummy1, &dummy2) < 0) {
832 1.23.2.2 he /* m was already freed at this point */
833 1.23.2.2 he error = EINVAL;/* better error? */
834 1.23.2.2 he goto done;
835 1.23.2.2 he }
836 1.23.2.2 he m->m_flags &= ~M_LOOP; /* XXX */
837 1.23.2.2 he m->m_pkthdr.rcvif = NULL;
838 1.23.2.2 he }
839 1.23.2.2 he
840 1.23.2.2 he #ifdef PFIL_HOOKS
841 1.23.2.2 he /*
842 1.23.2.2 he * Run through list of hooks for output packets.
843 1.23.2.2 he */
844 1.23.2.2 he m1 = m;
845 1.23.2.6 he pfh = pfil_hook_get(PFIL_OUT,
846 1.23.2.6 he &inet6sw[ip6_protox[IPPROTO_IPV6]].pr_pfh);
847 1.23.2.2 he for (; pfh; pfh = pfh->pfil_link.tqe_next)
848 1.23.2.2 he if (pfh->pfil_func) {
849 1.23.2.2 he rv = pfh->pfil_func(ip6, sizeof(*ip6), ifp, 1, &m1);
850 1.23.2.2 he if (rv) {
851 1.23.2.2 he error = EHOSTUNREACH;
852 1.23.2.2 he goto done;
853 1.23.2.2 he }
854 1.23.2.2 he m = m1;
855 1.23.2.2 he if (m == NULL)
856 1.23.2.2 he goto done;
857 1.23.2.2 he ip6 = mtod(m, struct ip6_hdr *);
858 1.23.2.2 he }
859 1.23.2.2 he #endif /* PFIL_HOOKS */
860 1.23.2.2 he /*
861 1.23.2.2 he * Send the packet to the outgoing interface.
862 1.23.2.2 he * If necessary, do IPv6 fragmentation before sending.
863 1.23.2.2 he */
864 1.23.2.2 he tlen = m->m_pkthdr.len;
865 1.23.2.2 he if (tlen <= mtu
866 1.23.2.2 he #ifdef notyet
867 1.23.2.2 he /*
868 1.23.2.2 he * On any link that cannot convey a 1280-octet packet in one piece,
869 1.23.2.2 he * link-specific fragmentation and reassembly must be provided at
870 1.23.2.2 he * a layer below IPv6. [RFC 2460, sec.5]
871 1.23.2.2 he * Thus if the interface has ability of link-level fragmentation,
872 1.23.2.2 he * we can just send the packet even if the packet size is
873 1.23.2.2 he * larger than the link's MTU.
874 1.23.2.2 he * XXX: IFF_FRAGMENTABLE (or such) flag has not been defined yet...
875 1.23.2.2 he */
876 1.23.2.2 he
877 1.23.2.2 he || ifp->if_flags & IFF_FRAGMENTABLE
878 1.23.2.2 he #endif
879 1.23.2.2 he )
880 1.23.2.2 he {
881 1.23.2.2 he #ifdef IFA_STATS
882 1.23.2.2 he if (IFA_STATS) {
883 1.23.2.2 he struct in6_ifaddr *ia6;
884 1.23.2.2 he ip6 = mtod(m, struct ip6_hdr *);
885 1.23.2.2 he ia6 = in6_ifawithifp(ifp, &ip6->ip6_src);
886 1.23.2.2 he if (ia6) {
887 1.23.2.2 he ia->ia_ifa.ifa_data.ifad_outbytes +=
888 1.23.2.2 he m->m_pkthdr.len;
889 1.23.2.2 he }
890 1.23.2.2 he }
891 1.23.2.2 he #endif
892 1.23.2.4 he #ifdef IPSEC
893 1.23.2.4 he /* clean ipsec history once it goes out of the node */
894 1.23.2.4 he ipsec_delaux(m);
895 1.23.2.4 he #endif
896 1.23.2.2 he #ifdef OLDIP6OUTPUT
897 1.23.2.2 he error = (*ifp->if_output)(ifp, m, (struct sockaddr *)dst,
898 1.23.2.2 he ro->ro_rt);
899 1.23.2.2 he #else
900 1.23.2.2 he error = nd6_output(ifp, origifp, m, dst, ro->ro_rt);
901 1.23.2.2 he #endif
902 1.23.2.2 he goto done;
903 1.23.2.2 he } else if (mtu < IPV6_MMTU) {
904 1.23.2.2 he /*
905 1.23.2.2 he * note that path MTU is never less than IPV6_MMTU
906 1.23.2.2 he * (see icmp6_input).
907 1.23.2.2 he */
908 1.23.2.2 he error = EMSGSIZE;
909 1.23.2.2 he in6_ifstat_inc(ifp, ifs6_out_fragfail);
910 1.23.2.2 he goto bad;
911 1.23.2.2 he } else if (ip6->ip6_plen == 0) { /* jumbo payload cannot be fragmented */
912 1.23.2.2 he error = EMSGSIZE;
913 1.23.2.2 he in6_ifstat_inc(ifp, ifs6_out_fragfail);
914 1.23.2.2 he goto bad;
915 1.23.2.2 he } else {
916 1.23.2.2 he struct mbuf **mnext, *m_frgpart;
917 1.23.2.2 he struct ip6_frag *ip6f;
918 1.23.2.2 he u_int32_t id = htonl(ip6_id++);
919 1.23.2.2 he u_char nextproto;
920 1.23.2.2 he
921 1.23.2.2 he /*
922 1.23.2.2 he * Too large for the destination or interface;
923 1.23.2.2 he * fragment if possible.
924 1.23.2.2 he * Must be able to put at least 8 bytes per fragment.
925 1.23.2.2 he */
926 1.23.2.2 he hlen = unfragpartlen;
927 1.23.2.2 he if (mtu > IPV6_MAXPACKET)
928 1.23.2.2 he mtu = IPV6_MAXPACKET;
929 1.23.2.2 he len = (mtu - hlen - sizeof(struct ip6_frag)) & ~7;
930 1.23.2.2 he if (len < 8) {
931 1.23.2.2 he error = EMSGSIZE;
932 1.23.2.2 he in6_ifstat_inc(ifp, ifs6_out_fragfail);
933 1.23.2.2 he goto bad;
934 1.23.2.2 he }
935 1.23.2.2 he
936 1.23.2.2 he mnext = &m->m_nextpkt;
937 1.23.2.2 he
938 1.23.2.2 he /*
939 1.23.2.2 he * Change the next header field of the last header in the
940 1.23.2.2 he * unfragmentable part.
941 1.23.2.2 he */
942 1.23.2.2 he if (exthdrs.ip6e_rthdr) {
943 1.23.2.2 he nextproto = *mtod(exthdrs.ip6e_rthdr, u_char *);
944 1.23.2.2 he *mtod(exthdrs.ip6e_rthdr, u_char *) = IPPROTO_FRAGMENT;
945 1.23.2.2 he } else if (exthdrs.ip6e_dest1) {
946 1.23.2.2 he nextproto = *mtod(exthdrs.ip6e_dest1, u_char *);
947 1.23.2.2 he *mtod(exthdrs.ip6e_dest1, u_char *) = IPPROTO_FRAGMENT;
948 1.23.2.2 he } else if (exthdrs.ip6e_hbh) {
949 1.23.2.2 he nextproto = *mtod(exthdrs.ip6e_hbh, u_char *);
950 1.23.2.2 he *mtod(exthdrs.ip6e_hbh, u_char *) = IPPROTO_FRAGMENT;
951 1.23.2.2 he } else {
952 1.23.2.2 he nextproto = ip6->ip6_nxt;
953 1.23.2.2 he ip6->ip6_nxt = IPPROTO_FRAGMENT;
954 1.23.2.2 he }
955 1.23.2.2 he
956 1.23.2.2 he /*
957 1.23.2.2 he * Loop through length of segment after first fragment,
958 1.23.2.2 he * make new header and copy data of each part and link onto chain.
959 1.23.2.2 he */
960 1.23.2.2 he m0 = m;
961 1.23.2.2 he for (off = hlen; off < tlen; off += len) {
962 1.23.2.2 he MGETHDR(m, M_DONTWAIT, MT_HEADER);
963 1.23.2.2 he if (!m) {
964 1.23.2.2 he error = ENOBUFS;
965 1.23.2.2 he ip6stat.ip6s_odropped++;
966 1.23.2.2 he goto sendorfree;
967 1.23.2.2 he }
968 1.23.2.2 he m->m_flags = m0->m_flags & M_COPYFLAGS;
969 1.23.2.2 he *mnext = m;
970 1.23.2.2 he mnext = &m->m_nextpkt;
971 1.23.2.2 he m->m_data += max_linkhdr;
972 1.23.2.2 he mhip6 = mtod(m, struct ip6_hdr *);
973 1.23.2.2 he *mhip6 = *ip6;
974 1.23.2.2 he m->m_len = sizeof(*mhip6);
975 1.23.2.2 he error = ip6_insertfraghdr(m0, m, hlen, &ip6f);
976 1.23.2.2 he if (error) {
977 1.23.2.2 he ip6stat.ip6s_odropped++;
978 1.23.2.2 he goto sendorfree;
979 1.23.2.2 he }
980 1.23.2.2 he ip6f->ip6f_offlg = htons((u_short)((off - hlen) & ~7));
981 1.23.2.2 he if (off + len >= tlen)
982 1.23.2.2 he len = tlen - off;
983 1.23.2.2 he else
984 1.23.2.2 he ip6f->ip6f_offlg |= IP6F_MORE_FRAG;
985 1.23.2.2 he mhip6->ip6_plen = htons((u_short)(len + hlen +
986 1.23.2.2 he sizeof(*ip6f) -
987 1.23.2.2 he sizeof(struct ip6_hdr)));
988 1.23.2.2 he if ((m_frgpart = m_copy(m0, off, len)) == 0) {
989 1.23.2.2 he error = ENOBUFS;
990 1.23.2.2 he ip6stat.ip6s_odropped++;
991 1.23.2.2 he goto sendorfree;
992 1.23.2.2 he }
993 1.23.2.2 he m_cat(m, m_frgpart);
994 1.23.2.2 he m->m_pkthdr.len = len + hlen + sizeof(*ip6f);
995 1.23.2.2 he m->m_pkthdr.rcvif = (struct ifnet *)0;
996 1.23.2.2 he ip6f->ip6f_reserved = 0;
997 1.23.2.2 he ip6f->ip6f_ident = id;
998 1.23.2.2 he ip6f->ip6f_nxt = nextproto;
999 1.23.2.2 he ip6stat.ip6s_ofragments++;
1000 1.23.2.2 he in6_ifstat_inc(ifp, ifs6_out_fragcreat);
1001 1.23.2.2 he }
1002 1.23.2.2 he
1003 1.23.2.2 he in6_ifstat_inc(ifp, ifs6_out_fragok);
1004 1.23.2.2 he }
1005 1.23.2.2 he
1006 1.23.2.2 he /*
1007 1.23.2.2 he * Remove leading garbages.
1008 1.23.2.2 he */
1009 1.23.2.2 he sendorfree:
1010 1.23.2.2 he m = m0->m_nextpkt;
1011 1.23.2.2 he m0->m_nextpkt = 0;
1012 1.23.2.2 he m_freem(m0);
1013 1.23.2.2 he for (m0 = m; m; m = m0) {
1014 1.23.2.2 he m0 = m->m_nextpkt;
1015 1.23.2.2 he m->m_nextpkt = 0;
1016 1.23.2.2 he if (error == 0) {
1017 1.23.2.2 he #ifdef IFA_STATS
1018 1.23.2.2 he if (IFA_STATS) {
1019 1.23.2.2 he struct in6_ifaddr *ia6;
1020 1.23.2.2 he ip6 = mtod(m, struct ip6_hdr *);
1021 1.23.2.2 he ia6 = in6_ifawithifp(ifp, &ip6->ip6_src);
1022 1.23.2.2 he if (ia6) {
1023 1.23.2.2 he ia->ia_ifa.ifa_data.ifad_outbytes +=
1024 1.23.2.2 he m->m_pkthdr.len;
1025 1.23.2.2 he }
1026 1.23.2.2 he }
1027 1.23.2.2 he #endif
1028 1.23.2.4 he #ifdef IPSEC
1029 1.23.2.4 he /* clean ipsec history once it goes out of the node */
1030 1.23.2.4 he ipsec_delaux(m);
1031 1.23.2.4 he #endif
1032 1.23.2.2 he #ifdef OLDIP6OUTPUT
1033 1.23.2.2 he error = (*ifp->if_output)(ifp, m,
1034 1.23.2.2 he (struct sockaddr *)dst,
1035 1.23.2.2 he ro->ro_rt);
1036 1.23.2.2 he #else
1037 1.23.2.2 he error = nd6_output(ifp, origifp, m, dst, ro->ro_rt);
1038 1.23.2.2 he #endif
1039 1.23.2.2 he } else
1040 1.23.2.2 he m_freem(m);
1041 1.23.2.2 he }
1042 1.23.2.2 he
1043 1.23.2.2 he if (error == 0)
1044 1.23.2.2 he ip6stat.ip6s_fragmented++;
1045 1.23.2.2 he
1046 1.23.2.2 he done:
1047 1.23.2.2 he if (ro == &ip6route && ro->ro_rt) { /* brace necessary for RTFREE */
1048 1.23.2.2 he RTFREE(ro->ro_rt);
1049 1.23.2.2 he } else if (ro_pmtu == &ip6route && ro_pmtu->ro_rt) {
1050 1.23.2.2 he RTFREE(ro_pmtu->ro_rt);
1051 1.23.2.2 he }
1052 1.23.2.2 he
1053 1.23.2.2 he #ifdef IPSEC
1054 1.23.2.2 he if (sp != NULL)
1055 1.23.2.2 he key_freesp(sp);
1056 1.23.2.2 he #endif /* IPSEC */
1057 1.23.2.2 he
1058 1.23.2.2 he return(error);
1059 1.23.2.2 he
1060 1.23.2.2 he freehdrs:
1061 1.23.2.2 he m_freem(exthdrs.ip6e_hbh); /* m_freem will check if mbuf is 0 */
1062 1.23.2.2 he m_freem(exthdrs.ip6e_dest1);
1063 1.23.2.2 he m_freem(exthdrs.ip6e_rthdr);
1064 1.23.2.2 he m_freem(exthdrs.ip6e_dest2);
1065 1.23.2.2 he /* fall through */
1066 1.23.2.2 he bad:
1067 1.23.2.2 he m_freem(m);
1068 1.23.2.2 he goto done;
1069 1.23.2.2 he }
1070 1.23.2.2 he
1071 1.23.2.2 he static int
1072 1.23.2.2 he ip6_copyexthdr(mp, hdr, hlen)
1073 1.23.2.2 he struct mbuf **mp;
1074 1.23.2.2 he caddr_t hdr;
1075 1.23.2.2 he int hlen;
1076 1.23.2.2 he {
1077 1.23.2.2 he struct mbuf *m;
1078 1.23.2.2 he
1079 1.23.2.2 he if (hlen > MCLBYTES)
1080 1.23.2.2 he return(ENOBUFS); /* XXX */
1081 1.23.2.2 he
1082 1.23.2.2 he MGET(m, M_DONTWAIT, MT_DATA);
1083 1.23.2.2 he if (!m)
1084 1.23.2.2 he return(ENOBUFS);
1085 1.23.2.2 he
1086 1.23.2.2 he if (hlen > MLEN) {
1087 1.23.2.2 he MCLGET(m, M_DONTWAIT);
1088 1.23.2.2 he if ((m->m_flags & M_EXT) == 0) {
1089 1.23.2.2 he m_free(m);
1090 1.23.2.2 he return(ENOBUFS);
1091 1.23.2.2 he }
1092 1.23.2.2 he }
1093 1.23.2.2 he m->m_len = hlen;
1094 1.23.2.2 he if (hdr)
1095 1.23.2.2 he bcopy(hdr, mtod(m, caddr_t), hlen);
1096 1.23.2.2 he
1097 1.23.2.2 he *mp = m;
1098 1.23.2.2 he return(0);
1099 1.23.2.2 he }
1100 1.23.2.2 he
1101 1.23.2.2 he /*
1102 1.23.2.2 he * Insert jumbo payload option.
1103 1.23.2.2 he */
1104 1.23.2.2 he static int
1105 1.23.2.2 he ip6_insert_jumboopt(exthdrs, plen)
1106 1.23.2.2 he struct ip6_exthdrs *exthdrs;
1107 1.23.2.2 he u_int32_t plen;
1108 1.23.2.2 he {
1109 1.23.2.2 he struct mbuf *mopt;
1110 1.23.2.2 he u_char *optbuf;
1111 1.23.2.2 he
1112 1.23.2.2 he #define JUMBOOPTLEN 8 /* length of jumbo payload option and padding */
1113 1.23.2.2 he
1114 1.23.2.2 he /*
1115 1.23.2.2 he * If there is no hop-by-hop options header, allocate new one.
1116 1.23.2.2 he * If there is one but it doesn't have enough space to store the
1117 1.23.2.2 he * jumbo payload option, allocate a cluster to store the whole options.
1118 1.23.2.2 he * Otherwise, use it to store the options.
1119 1.23.2.2 he */
1120 1.23.2.2 he if (exthdrs->ip6e_hbh == 0) {
1121 1.23.2.2 he MGET(mopt, M_DONTWAIT, MT_DATA);
1122 1.23.2.2 he if (mopt == 0)
1123 1.23.2.2 he return(ENOBUFS);
1124 1.23.2.2 he mopt->m_len = JUMBOOPTLEN;
1125 1.23.2.2 he optbuf = mtod(mopt, u_char *);
1126 1.23.2.2 he optbuf[1] = 0; /* = ((JUMBOOPTLEN) >> 3) - 1 */
1127 1.23.2.2 he exthdrs->ip6e_hbh = mopt;
1128 1.23.2.2 he } else {
1129 1.23.2.2 he struct ip6_hbh *hbh;
1130 1.23.2.2 he
1131 1.23.2.2 he mopt = exthdrs->ip6e_hbh;
1132 1.23.2.2 he if (M_TRAILINGSPACE(mopt) < JUMBOOPTLEN) {
1133 1.23.2.2 he caddr_t oldoptp = mtod(mopt, caddr_t);
1134 1.23.2.2 he int oldoptlen = mopt->m_len;
1135 1.23.2.2 he
1136 1.23.2.2 he if (mopt->m_flags & M_EXT)
1137 1.23.2.2 he return(ENOBUFS); /* XXX */
1138 1.23.2.2 he MCLGET(mopt, M_DONTWAIT);
1139 1.23.2.2 he if ((mopt->m_flags & M_EXT) == 0)
1140 1.23.2.2 he return(ENOBUFS);
1141 1.23.2.2 he
1142 1.23.2.2 he bcopy(oldoptp, mtod(mopt, caddr_t), oldoptlen);
1143 1.23.2.2 he optbuf = mtod(mopt, caddr_t) + oldoptlen;
1144 1.23.2.2 he mopt->m_len = oldoptlen + JUMBOOPTLEN;
1145 1.23.2.2 he } else {
1146 1.23.2.2 he optbuf = mtod(mopt, u_char *) + mopt->m_len;
1147 1.23.2.2 he mopt->m_len += JUMBOOPTLEN;
1148 1.23.2.2 he }
1149 1.23.2.2 he optbuf[0] = IP6OPT_PADN;
1150 1.23.2.2 he optbuf[1] = 1;
1151 1.23.2.2 he
1152 1.23.2.2 he /*
1153 1.23.2.2 he * Adjust the header length according to the pad and
1154 1.23.2.2 he * the jumbo payload option.
1155 1.23.2.2 he */
1156 1.23.2.2 he hbh = mtod(mopt, struct ip6_hbh *);
1157 1.23.2.2 he hbh->ip6h_len += (JUMBOOPTLEN >> 3);
1158 1.23.2.2 he }
1159 1.23.2.2 he
1160 1.23.2.2 he /* fill in the option. */
1161 1.23.2.2 he optbuf[2] = IP6OPT_JUMBO;
1162 1.23.2.2 he optbuf[3] = 4;
1163 1.23.2.2 he *(u_int32_t *)&optbuf[4] = htonl(plen + JUMBOOPTLEN);
1164 1.23.2.2 he
1165 1.23.2.2 he /* finally, adjust the packet header length */
1166 1.23.2.2 he exthdrs->ip6e_ip6->m_pkthdr.len += JUMBOOPTLEN;
1167 1.23.2.2 he
1168 1.23.2.2 he return(0);
1169 1.23.2.2 he #undef JUMBOOPTLEN
1170 1.23.2.2 he }
1171 1.23.2.2 he
1172 1.23.2.2 he /*
1173 1.23.2.2 he * Insert fragment header and copy unfragmentable header portions.
1174 1.23.2.2 he */
1175 1.23.2.2 he static int
1176 1.23.2.2 he ip6_insertfraghdr(m0, m, hlen, frghdrp)
1177 1.23.2.2 he struct mbuf *m0, *m;
1178 1.23.2.2 he int hlen;
1179 1.23.2.2 he struct ip6_frag **frghdrp;
1180 1.23.2.2 he {
1181 1.23.2.2 he struct mbuf *n, *mlast;
1182 1.23.2.2 he
1183 1.23.2.2 he if (hlen > sizeof(struct ip6_hdr)) {
1184 1.23.2.2 he n = m_copym(m0, sizeof(struct ip6_hdr),
1185 1.23.2.2 he hlen - sizeof(struct ip6_hdr), M_DONTWAIT);
1186 1.23.2.2 he if (n == 0)
1187 1.23.2.2 he return(ENOBUFS);
1188 1.23.2.2 he m->m_next = n;
1189 1.23.2.2 he } else
1190 1.23.2.2 he n = m;
1191 1.23.2.2 he
1192 1.23.2.2 he /* Search for the last mbuf of unfragmentable part. */
1193 1.23.2.2 he for (mlast = n; mlast->m_next; mlast = mlast->m_next)
1194 1.23.2.2 he ;
1195 1.23.2.2 he
1196 1.23.2.2 he if ((mlast->m_flags & M_EXT) == 0 &&
1197 1.23.2.2 he M_TRAILINGSPACE(mlast) >= sizeof(struct ip6_frag)) {
1198 1.23.2.2 he /* use the trailing space of the last mbuf for the fragment hdr */
1199 1.23.2.2 he *frghdrp =
1200 1.23.2.2 he (struct ip6_frag *)(mtod(mlast, caddr_t) + mlast->m_len);
1201 1.23.2.2 he mlast->m_len += sizeof(struct ip6_frag);
1202 1.23.2.2 he m->m_pkthdr.len += sizeof(struct ip6_frag);
1203 1.23.2.2 he } else {
1204 1.23.2.2 he /* allocate a new mbuf for the fragment header */
1205 1.23.2.2 he struct mbuf *mfrg;
1206 1.23.2.2 he
1207 1.23.2.2 he MGET(mfrg, M_DONTWAIT, MT_DATA);
1208 1.23.2.2 he if (mfrg == 0)
1209 1.23.2.2 he return(ENOBUFS);
1210 1.23.2.2 he mfrg->m_len = sizeof(struct ip6_frag);
1211 1.23.2.2 he *frghdrp = mtod(mfrg, struct ip6_frag *);
1212 1.23.2.2 he mlast->m_next = mfrg;
1213 1.23.2.2 he }
1214 1.23.2.2 he
1215 1.23.2.2 he return(0);
1216 1.23.2.2 he }
1217 1.23.2.2 he
1218 1.23.2.2 he /*
1219 1.23.2.2 he * IP6 socket option processing.
1220 1.23.2.2 he */
1221 1.23.2.2 he int
1222 1.23.2.2 he ip6_ctloutput(op, so, level, optname, mp)
1223 1.23.2.2 he int op;
1224 1.23.2.2 he struct socket *so;
1225 1.23.2.2 he int level, optname;
1226 1.23.2.2 he struct mbuf **mp;
1227 1.23.2.2 he {
1228 1.23.2.2 he register struct in6pcb *in6p = sotoin6pcb(so);
1229 1.23.2.2 he register struct mbuf *m = *mp;
1230 1.23.2.2 he register int optval = 0;
1231 1.23.2.2 he int error = 0;
1232 1.23.2.2 he struct proc *p = curproc; /* XXX */
1233 1.23.2.2 he
1234 1.23.2.2 he if (level == IPPROTO_IPV6) {
1235 1.23.2.2 he switch (op) {
1236 1.23.2.2 he
1237 1.23.2.2 he case PRCO_SETOPT:
1238 1.23.2.2 he switch (optname) {
1239 1.23.2.2 he case IPV6_PKTOPTIONS:
1240 1.23.2.2 he /* m is freed in ip6_pcbopts */
1241 1.23.2.2 he return(ip6_pcbopts(&in6p->in6p_outputopts,
1242 1.23.2.2 he m, so));
1243 1.23.2.2 he case IPV6_HOPOPTS:
1244 1.23.2.2 he case IPV6_DSTOPTS:
1245 1.23.2.2 he if (p == 0 || suser(p->p_ucred, &p->p_acflag)) {
1246 1.23.2.2 he error = EPERM;
1247 1.23.2.2 he break;
1248 1.23.2.2 he }
1249 1.23.2.2 he /* fall through */
1250 1.23.2.2 he case IPV6_UNICAST_HOPS:
1251 1.23.2.2 he case IPV6_RECVOPTS:
1252 1.23.2.2 he case IPV6_RECVRETOPTS:
1253 1.23.2.2 he case IPV6_RECVDSTADDR:
1254 1.23.2.2 he case IPV6_PKTINFO:
1255 1.23.2.2 he case IPV6_HOPLIMIT:
1256 1.23.2.2 he case IPV6_RTHDR:
1257 1.23.2.2 he case IPV6_CHECKSUM:
1258 1.23.2.2 he case IPV6_FAITH:
1259 1.23.2.2 he #ifndef INET6_BINDV6ONLY
1260 1.23.2.2 he case IPV6_BINDV6ONLY:
1261 1.23.2.2 he #endif
1262 1.23.2.2 he if (!m || m->m_len != sizeof(int))
1263 1.23.2.2 he error = EINVAL;
1264 1.23.2.2 he else {
1265 1.23.2.2 he optval = *mtod(m, int *);
1266 1.23.2.2 he switch (optname) {
1267 1.23.2.2 he
1268 1.23.2.2 he case IPV6_UNICAST_HOPS:
1269 1.23.2.2 he if (optval < -1 || optval >= 256)
1270 1.23.2.2 he error = EINVAL;
1271 1.23.2.2 he else {
1272 1.23.2.2 he /* -1 = kernel default */
1273 1.23.2.2 he in6p->in6p_hops = optval;
1274 1.23.2.2 he }
1275 1.23.2.2 he break;
1276 1.23.2.2 he #define OPTSET(bit) \
1277 1.23.2.2 he if (optval) \
1278 1.23.2.2 he in6p->in6p_flags |= bit; \
1279 1.23.2.2 he else \
1280 1.23.2.2 he in6p->in6p_flags &= ~bit;
1281 1.23.2.2 he
1282 1.23.2.2 he case IPV6_RECVOPTS:
1283 1.23.2.2 he OPTSET(IN6P_RECVOPTS);
1284 1.23.2.2 he break;
1285 1.23.2.2 he
1286 1.23.2.2 he case IPV6_RECVRETOPTS:
1287 1.23.2.2 he OPTSET(IN6P_RECVRETOPTS);
1288 1.23.2.2 he break;
1289 1.23.2.2 he
1290 1.23.2.2 he case IPV6_RECVDSTADDR:
1291 1.23.2.2 he OPTSET(IN6P_RECVDSTADDR);
1292 1.23.2.2 he break;
1293 1.23.2.2 he
1294 1.23.2.2 he case IPV6_PKTINFO:
1295 1.23.2.2 he OPTSET(IN6P_PKTINFO);
1296 1.23.2.2 he break;
1297 1.23.2.2 he
1298 1.23.2.2 he case IPV6_HOPLIMIT:
1299 1.23.2.2 he OPTSET(IN6P_HOPLIMIT);
1300 1.23.2.2 he break;
1301 1.23.2.2 he
1302 1.23.2.2 he case IPV6_HOPOPTS:
1303 1.23.2.2 he OPTSET(IN6P_HOPOPTS);
1304 1.23.2.2 he break;
1305 1.23.2.2 he
1306 1.23.2.2 he case IPV6_DSTOPTS:
1307 1.23.2.2 he OPTSET(IN6P_DSTOPTS);
1308 1.23.2.2 he break;
1309 1.23.2.2 he
1310 1.23.2.2 he case IPV6_RTHDR:
1311 1.23.2.2 he OPTSET(IN6P_RTHDR);
1312 1.23.2.2 he break;
1313 1.23.2.2 he
1314 1.23.2.2 he case IPV6_CHECKSUM:
1315 1.23.2.2 he in6p->in6p_cksum = optval;
1316 1.23.2.2 he break;
1317 1.23.2.2 he
1318 1.23.2.2 he case IPV6_FAITH:
1319 1.23.2.2 he OPTSET(IN6P_FAITH);
1320 1.23.2.2 he break;
1321 1.23.2.2 he
1322 1.23.2.2 he #ifndef INET6_BINDV6ONLY
1323 1.23.2.2 he case IPV6_BINDV6ONLY:
1324 1.23.2.2 he OPTSET(IN6P_BINDV6ONLY);
1325 1.23.2.2 he break;
1326 1.23.2.2 he #endif
1327 1.23.2.2 he }
1328 1.23.2.2 he }
1329 1.23.2.2 he break;
1330 1.23.2.2 he #undef OPTSET
1331 1.23.2.2 he
1332 1.23.2.2 he case IPV6_MULTICAST_IF:
1333 1.23.2.2 he case IPV6_MULTICAST_HOPS:
1334 1.23.2.2 he case IPV6_MULTICAST_LOOP:
1335 1.23.2.2 he case IPV6_JOIN_GROUP:
1336 1.23.2.2 he case IPV6_LEAVE_GROUP:
1337 1.23.2.2 he error = ip6_setmoptions(optname, &in6p->in6p_moptions, m);
1338 1.23.2.2 he break;
1339 1.23.2.2 he
1340 1.23.2.2 he case IPV6_PORTRANGE:
1341 1.23.2.2 he optval = *mtod(m, int *);
1342 1.23.2.2 he
1343 1.23.2.2 he switch (optval) {
1344 1.23.2.2 he case IPV6_PORTRANGE_DEFAULT:
1345 1.23.2.2 he in6p->in6p_flags &= ~(IN6P_LOWPORT);
1346 1.23.2.2 he in6p->in6p_flags &= ~(IN6P_HIGHPORT);
1347 1.23.2.2 he break;
1348 1.23.2.2 he
1349 1.23.2.2 he case IPV6_PORTRANGE_HIGH:
1350 1.23.2.2 he in6p->in6p_flags &= ~(IN6P_LOWPORT);
1351 1.23.2.2 he in6p->in6p_flags |= IN6P_HIGHPORT;
1352 1.23.2.2 he break;
1353 1.23.2.2 he
1354 1.23.2.2 he case IPV6_PORTRANGE_LOW:
1355 1.23.2.2 he in6p->in6p_flags &= ~(IN6P_HIGHPORT);
1356 1.23.2.2 he in6p->in6p_flags |= IN6P_LOWPORT;
1357 1.23.2.2 he break;
1358 1.23.2.2 he
1359 1.23.2.2 he default:
1360 1.23.2.2 he error = EINVAL;
1361 1.23.2.2 he break;
1362 1.23.2.2 he }
1363 1.23.2.2 he break;
1364 1.23.2.2 he
1365 1.23.2.2 he #ifdef IPSEC
1366 1.23.2.2 he case IPV6_IPSEC_POLICY:
1367 1.23.2.2 he {
1368 1.23.2.2 he caddr_t req = NULL;
1369 1.23.2.2 he size_t len = 0;
1370 1.23.2.2 he
1371 1.23.2.2 he int priv = 0;
1372 1.23.2.2 he if (p == 0 || suser(p->p_ucred, &p->p_acflag))
1373 1.23.2.2 he priv = 0;
1374 1.23.2.2 he else
1375 1.23.2.2 he priv = 1;
1376 1.23.2.2 he if (m) {
1377 1.23.2.2 he req = mtod(m, caddr_t);
1378 1.23.2.2 he len = m->m_len;
1379 1.23.2.2 he }
1380 1.23.2.2 he error = ipsec6_set_policy(in6p,
1381 1.23.2.2 he optname, req, len, priv);
1382 1.23.2.2 he }
1383 1.23.2.2 he break;
1384 1.23.2.2 he #endif /* IPSEC */
1385 1.23.2.2 he
1386 1.23.2.2 he default:
1387 1.23.2.2 he error = ENOPROTOOPT;
1388 1.23.2.2 he break;
1389 1.23.2.2 he }
1390 1.23.2.2 he if (m)
1391 1.23.2.2 he (void)m_free(m);
1392 1.23.2.2 he break;
1393 1.23.2.2 he
1394 1.23.2.2 he case PRCO_GETOPT:
1395 1.23.2.2 he switch (optname) {
1396 1.23.2.2 he
1397 1.23.2.2 he case IPV6_OPTIONS:
1398 1.23.2.2 he case IPV6_RETOPTS:
1399 1.23.2.2 he #if 0
1400 1.23.2.2 he *mp = m = m_get(M_WAIT, MT_SOOPTS);
1401 1.23.2.2 he if (in6p->in6p_options) {
1402 1.23.2.2 he m->m_len = in6p->in6p_options->m_len;
1403 1.23.2.2 he bcopy(mtod(in6p->in6p_options, caddr_t),
1404 1.23.2.2 he mtod(m, caddr_t),
1405 1.23.2.2 he (unsigned)m->m_len);
1406 1.23.2.2 he } else
1407 1.23.2.2 he m->m_len = 0;
1408 1.23.2.2 he break;
1409 1.23.2.2 he #else
1410 1.23.2.2 he error = ENOPROTOOPT;
1411 1.23.2.2 he break;
1412 1.23.2.2 he #endif
1413 1.23.2.2 he
1414 1.23.2.2 he case IPV6_PKTOPTIONS:
1415 1.23.2.2 he if (in6p->in6p_options) {
1416 1.23.2.2 he *mp = m_copym(in6p->in6p_options, 0,
1417 1.23.2.2 he M_COPYALL, M_WAIT);
1418 1.23.2.2 he } else {
1419 1.23.2.2 he *mp = m_get(M_WAIT, MT_SOOPTS);
1420 1.23.2.2 he (*mp)->m_len = 0;
1421 1.23.2.2 he }
1422 1.23.2.2 he break;
1423 1.23.2.2 he
1424 1.23.2.2 he case IPV6_HOPOPTS:
1425 1.23.2.2 he case IPV6_DSTOPTS:
1426 1.23.2.2 he if (p == 0 || suser(p->p_ucred, &p->p_acflag)) {
1427 1.23.2.2 he error = EPERM;
1428 1.23.2.2 he break;
1429 1.23.2.2 he }
1430 1.23.2.2 he /* fall through */
1431 1.23.2.2 he case IPV6_UNICAST_HOPS:
1432 1.23.2.2 he case IPV6_RECVOPTS:
1433 1.23.2.2 he case IPV6_RECVRETOPTS:
1434 1.23.2.2 he case IPV6_RECVDSTADDR:
1435 1.23.2.2 he case IPV6_PORTRANGE:
1436 1.23.2.2 he case IPV6_PKTINFO:
1437 1.23.2.2 he case IPV6_HOPLIMIT:
1438 1.23.2.2 he case IPV6_RTHDR:
1439 1.23.2.2 he case IPV6_CHECKSUM:
1440 1.23.2.2 he case IPV6_FAITH:
1441 1.23.2.2 he #ifndef INET6_BINDV6ONLY
1442 1.23.2.2 he case IPV6_BINDV6ONLY:
1443 1.23.2.2 he #endif
1444 1.23.2.2 he *mp = m = m_get(M_WAIT, MT_SOOPTS);
1445 1.23.2.2 he m->m_len = sizeof(int);
1446 1.23.2.2 he switch (optname) {
1447 1.23.2.2 he
1448 1.23.2.2 he case IPV6_UNICAST_HOPS:
1449 1.23.2.2 he optval = in6p->in6p_hops;
1450 1.23.2.2 he break;
1451 1.23.2.2 he
1452 1.23.2.2 he #define OPTBIT(bit) (in6p->in6p_flags & bit ? 1 : 0)
1453 1.23.2.2 he
1454 1.23.2.2 he case IPV6_RECVOPTS:
1455 1.23.2.2 he optval = OPTBIT(IN6P_RECVOPTS);
1456 1.23.2.2 he break;
1457 1.23.2.2 he
1458 1.23.2.2 he case IPV6_RECVRETOPTS:
1459 1.23.2.2 he optval = OPTBIT(IN6P_RECVRETOPTS);
1460 1.23.2.2 he break;
1461 1.23.2.2 he
1462 1.23.2.2 he case IPV6_RECVDSTADDR:
1463 1.23.2.2 he optval = OPTBIT(IN6P_RECVDSTADDR);
1464 1.23.2.2 he break;
1465 1.23.2.2 he
1466 1.23.2.2 he case IPV6_PORTRANGE:
1467 1.23.2.2 he {
1468 1.23.2.2 he int flags;
1469 1.23.2.2 he flags = in6p->in6p_flags;
1470 1.23.2.2 he if (flags & IN6P_HIGHPORT)
1471 1.23.2.2 he optval = IPV6_PORTRANGE_HIGH;
1472 1.23.2.2 he else if (flags & IN6P_LOWPORT)
1473 1.23.2.2 he optval = IPV6_PORTRANGE_LOW;
1474 1.23.2.2 he else
1475 1.23.2.2 he optval = 0;
1476 1.23.2.2 he break;
1477 1.23.2.2 he }
1478 1.23.2.2 he
1479 1.23.2.2 he case IPV6_PKTINFO:
1480 1.23.2.2 he optval = OPTBIT(IN6P_PKTINFO);
1481 1.23.2.2 he break;
1482 1.23.2.2 he
1483 1.23.2.2 he case IPV6_HOPLIMIT:
1484 1.23.2.2 he optval = OPTBIT(IN6P_HOPLIMIT);
1485 1.23.2.2 he break;
1486 1.23.2.2 he
1487 1.23.2.2 he case IPV6_HOPOPTS:
1488 1.23.2.2 he optval = OPTBIT(IN6P_HOPOPTS);
1489 1.23.2.2 he break;
1490 1.23.2.2 he
1491 1.23.2.2 he case IPV6_DSTOPTS:
1492 1.23.2.2 he optval = OPTBIT(IN6P_DSTOPTS);
1493 1.23.2.2 he break;
1494 1.23.2.2 he
1495 1.23.2.2 he case IPV6_RTHDR:
1496 1.23.2.2 he optval = OPTBIT(IN6P_RTHDR);
1497 1.23.2.2 he break;
1498 1.23.2.2 he
1499 1.23.2.2 he case IPV6_CHECKSUM:
1500 1.23.2.2 he optval = in6p->in6p_cksum;
1501 1.23.2.2 he break;
1502 1.23.2.2 he
1503 1.23.2.2 he case IPV6_FAITH:
1504 1.23.2.2 he optval = OPTBIT(IN6P_FAITH);
1505 1.23.2.2 he break;
1506 1.23.2.2 he
1507 1.23.2.2 he #ifndef INET6_BINDV6ONLY
1508 1.23.2.2 he case IPV6_BINDV6ONLY:
1509 1.23.2.2 he optval = OPTBIT(IN6P_BINDV6ONLY);
1510 1.23.2.2 he break;
1511 1.23.2.2 he #endif
1512 1.23.2.2 he }
1513 1.23.2.2 he *mtod(m, int *) = optval;
1514 1.23.2.2 he break;
1515 1.23.2.2 he
1516 1.23.2.2 he case IPV6_MULTICAST_IF:
1517 1.23.2.2 he case IPV6_MULTICAST_HOPS:
1518 1.23.2.2 he case IPV6_MULTICAST_LOOP:
1519 1.23.2.2 he case IPV6_JOIN_GROUP:
1520 1.23.2.2 he case IPV6_LEAVE_GROUP:
1521 1.23.2.2 he error = ip6_getmoptions(optname, in6p->in6p_moptions, mp);
1522 1.23.2.2 he break;
1523 1.23.2.2 he
1524 1.23.2.2 he #ifdef IPSEC
1525 1.23.2.2 he case IPV6_IPSEC_POLICY:
1526 1.23.2.2 he {
1527 1.23.2.2 he caddr_t req = NULL;
1528 1.23.2.2 he size_t len = 0;
1529 1.23.2.2 he
1530 1.23.2.2 he if (m) {
1531 1.23.2.2 he req = mtod(m, caddr_t);
1532 1.23.2.2 he len = m->m_len;
1533 1.23.2.2 he }
1534 1.23.2.2 he error = ipsec6_get_policy(in6p, req, len, mp);
1535 1.23.2.2 he break;
1536 1.23.2.2 he }
1537 1.23.2.2 he #endif /* IPSEC */
1538 1.23.2.2 he
1539 1.23.2.2 he default:
1540 1.23.2.2 he error = ENOPROTOOPT;
1541 1.23.2.2 he break;
1542 1.23.2.2 he }
1543 1.23.2.2 he break;
1544 1.23.2.2 he }
1545 1.23.2.2 he } else {
1546 1.23.2.2 he error = EINVAL;
1547 1.23.2.2 he if (op == PRCO_SETOPT && *mp)
1548 1.23.2.2 he (void)m_free(*mp);
1549 1.23.2.2 he }
1550 1.23.2.2 he return(error);
1551 1.23.2.2 he }
1552 1.23.2.2 he
1553 1.23.2.2 he /*
1554 1.23.2.2 he * Set up IP6 options in pcb for insertion in output packets.
1555 1.23.2.2 he * Store in mbuf with pointer in pcbopt, adding pseudo-option
1556 1.23.2.2 he * with destination address if source routed.
1557 1.23.2.2 he */
1558 1.23.2.2 he static int
1559 1.23.2.2 he ip6_pcbopts(pktopt, m, so)
1560 1.23.2.2 he struct ip6_pktopts **pktopt;
1561 1.23.2.2 he register struct mbuf *m;
1562 1.23.2.2 he struct socket *so;
1563 1.23.2.2 he {
1564 1.23.2.2 he register struct ip6_pktopts *opt = *pktopt;
1565 1.23.2.2 he int error = 0;
1566 1.23.2.2 he struct proc *p = curproc; /* XXX */
1567 1.23.2.2 he int priv = 0;
1568 1.23.2.2 he
1569 1.23.2.2 he /* turn off any old options. */
1570 1.23.2.2 he if (opt) {
1571 1.23.2.2 he if (opt->ip6po_m)
1572 1.23.2.2 he (void)m_free(opt->ip6po_m);
1573 1.23.2.2 he } else
1574 1.23.2.2 he opt = malloc(sizeof(*opt), M_IP6OPT, M_WAITOK);
1575 1.23.2.2 he *pktopt = 0;
1576 1.23.2.2 he
1577 1.23.2.2 he if (!m || m->m_len == 0) {
1578 1.23.2.2 he /*
1579 1.23.2.2 he * Only turning off any previous options.
1580 1.23.2.2 he */
1581 1.23.2.2 he if (opt)
1582 1.23.2.2 he free(opt, M_IP6OPT);
1583 1.23.2.2 he if (m)
1584 1.23.2.2 he (void)m_free(m);
1585 1.23.2.2 he return(0);
1586 1.23.2.2 he }
1587 1.23.2.2 he
1588 1.23.2.2 he /* set options specified by user. */
1589 1.23.2.2 he if (p && !suser(p->p_ucred, &p->p_acflag))
1590 1.23.2.2 he priv = 1;
1591 1.23.2.2 he if ((error = ip6_setpktoptions(m, opt, priv)) != 0) {
1592 1.23.2.2 he (void)m_free(m);
1593 1.23.2.2 he return(error);
1594 1.23.2.2 he }
1595 1.23.2.2 he *pktopt = opt;
1596 1.23.2.2 he return(0);
1597 1.23.2.2 he }
1598 1.23.2.2 he
1599 1.23.2.2 he /*
1600 1.23.2.2 he * Set the IP6 multicast options in response to user setsockopt().
1601 1.23.2.2 he */
1602 1.23.2.2 he static int
1603 1.23.2.2 he ip6_setmoptions(optname, im6op, m)
1604 1.23.2.2 he int optname;
1605 1.23.2.2 he struct ip6_moptions **im6op;
1606 1.23.2.2 he struct mbuf *m;
1607 1.23.2.2 he {
1608 1.23.2.2 he int error = 0;
1609 1.23.2.2 he u_int loop, ifindex;
1610 1.23.2.2 he struct ipv6_mreq *mreq;
1611 1.23.2.2 he struct ifnet *ifp;
1612 1.23.2.2 he struct ip6_moptions *im6o = *im6op;
1613 1.23.2.2 he struct route_in6 ro;
1614 1.23.2.2 he struct sockaddr_in6 *dst;
1615 1.23.2.2 he struct in6_multi_mship *imm;
1616 1.23.2.2 he struct proc *p = curproc; /* XXX */
1617 1.23.2.2 he
1618 1.23.2.2 he if (im6o == NULL) {
1619 1.23.2.2 he /*
1620 1.23.2.2 he * No multicast option buffer attached to the pcb;
1621 1.23.2.2 he * allocate one and initialize to default values.
1622 1.23.2.2 he */
1623 1.23.2.2 he im6o = (struct ip6_moptions *)
1624 1.23.2.2 he malloc(sizeof(*im6o), M_IPMOPTS, M_WAITOK);
1625 1.23.2.2 he
1626 1.23.2.2 he if (im6o == NULL)
1627 1.23.2.2 he return(ENOBUFS);
1628 1.23.2.2 he *im6op = im6o;
1629 1.23.2.2 he im6o->im6o_multicast_ifp = NULL;
1630 1.23.2.2 he im6o->im6o_multicast_hlim = ip6_defmcasthlim;
1631 1.23.2.2 he im6o->im6o_multicast_loop = IPV6_DEFAULT_MULTICAST_LOOP;
1632 1.23.2.2 he LIST_INIT(&im6o->im6o_memberships);
1633 1.23.2.2 he }
1634 1.23.2.2 he
1635 1.23.2.2 he switch (optname) {
1636 1.23.2.2 he
1637 1.23.2.2 he case IPV6_MULTICAST_IF:
1638 1.23.2.2 he /*
1639 1.23.2.2 he * Select the interface for outgoing multicast packets.
1640 1.23.2.2 he */
1641 1.23.2.2 he if (m == NULL || m->m_len != sizeof(u_int)) {
1642 1.23.2.2 he error = EINVAL;
1643 1.23.2.2 he break;
1644 1.23.2.2 he }
1645 1.23.2.2 he ifindex = *(mtod(m, u_int *));
1646 1.23.2.2 he if (ifindex < 0 || if_index < ifindex) {
1647 1.23.2.2 he error = ENXIO; /* XXX EINVAL? */
1648 1.23.2.2 he break;
1649 1.23.2.2 he }
1650 1.23.2.2 he ifp = ifindex2ifnet[ifindex];
1651 1.23.2.2 he if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
1652 1.23.2.2 he error = EADDRNOTAVAIL;
1653 1.23.2.2 he break;
1654 1.23.2.2 he }
1655 1.23.2.2 he im6o->im6o_multicast_ifp = ifp;
1656 1.23.2.2 he break;
1657 1.23.2.2 he
1658 1.23.2.2 he case IPV6_MULTICAST_HOPS:
1659 1.23.2.2 he {
1660 1.23.2.2 he /*
1661 1.23.2.2 he * Set the IP6 hoplimit for outgoing multicast packets.
1662 1.23.2.2 he */
1663 1.23.2.2 he int optval;
1664 1.23.2.2 he if (m == NULL || m->m_len != sizeof(int)) {
1665 1.23.2.2 he error = EINVAL;
1666 1.23.2.2 he break;
1667 1.23.2.2 he }
1668 1.23.2.2 he optval = *(mtod(m, u_int *));
1669 1.23.2.2 he if (optval < -1 || optval >= 256)
1670 1.23.2.2 he error = EINVAL;
1671 1.23.2.2 he else if (optval == -1)
1672 1.23.2.2 he im6o->im6o_multicast_hlim = ip6_defmcasthlim;
1673 1.23.2.2 he else
1674 1.23.2.2 he im6o->im6o_multicast_hlim = optval;
1675 1.23.2.2 he break;
1676 1.23.2.2 he }
1677 1.23.2.2 he
1678 1.23.2.2 he case IPV6_MULTICAST_LOOP:
1679 1.23.2.2 he /*
1680 1.23.2.2 he * Set the loopback flag for outgoing multicast packets.
1681 1.23.2.2 he * Must be zero or one.
1682 1.23.2.2 he */
1683 1.23.2.2 he if (m == NULL || m->m_len != sizeof(u_int) ||
1684 1.23.2.2 he (loop = *(mtod(m, u_int *))) > 1) {
1685 1.23.2.2 he error = EINVAL;
1686 1.23.2.2 he break;
1687 1.23.2.2 he }
1688 1.23.2.2 he im6o->im6o_multicast_loop = loop;
1689 1.23.2.2 he break;
1690 1.23.2.2 he
1691 1.23.2.2 he case IPV6_JOIN_GROUP:
1692 1.23.2.2 he /*
1693 1.23.2.2 he * Add a multicast group membership.
1694 1.23.2.2 he * Group must be a valid IP6 multicast address.
1695 1.23.2.2 he */
1696 1.23.2.2 he if (m == NULL || m->m_len != sizeof(struct ipv6_mreq)) {
1697 1.23.2.2 he error = EINVAL;
1698 1.23.2.2 he break;
1699 1.23.2.2 he }
1700 1.23.2.2 he mreq = mtod(m, struct ipv6_mreq *);
1701 1.23.2.2 he if (IN6_IS_ADDR_UNSPECIFIED(&mreq->ipv6mr_multiaddr)) {
1702 1.23.2.2 he /*
1703 1.23.2.2 he * We use the unspecified address to specify to accept
1704 1.23.2.2 he * all multicast addresses. Only super user is allowed
1705 1.23.2.2 he * to do this.
1706 1.23.2.2 he */
1707 1.23.2.2 he if (suser(p->p_ucred, &p->p_acflag)) {
1708 1.23.2.2 he error = EACCES;
1709 1.23.2.2 he break;
1710 1.23.2.2 he }
1711 1.23.2.2 he } else if (!IN6_IS_ADDR_MULTICAST(&mreq->ipv6mr_multiaddr)) {
1712 1.23.2.2 he error = EINVAL;
1713 1.23.2.2 he break;
1714 1.23.2.2 he }
1715 1.23.2.2 he
1716 1.23.2.2 he /*
1717 1.23.2.2 he * If the interface is specified, validate it.
1718 1.23.2.2 he */
1719 1.23.2.2 he if (mreq->ipv6mr_interface < 0
1720 1.23.2.2 he || if_index < mreq->ipv6mr_interface) {
1721 1.23.2.2 he error = ENXIO; /* XXX EINVAL? */
1722 1.23.2.2 he break;
1723 1.23.2.2 he }
1724 1.23.2.2 he /*
1725 1.23.2.2 he * If no interface was explicitly specified, choose an
1726 1.23.2.2 he * appropriate one according to the given multicast address.
1727 1.23.2.2 he */
1728 1.23.2.2 he if (mreq->ipv6mr_interface == 0) {
1729 1.23.2.2 he /*
1730 1.23.2.2 he * If the multicast address is in node-local scope,
1731 1.23.2.2 he * the interface should be a loopback interface.
1732 1.23.2.2 he * Otherwise, look up the routing table for the
1733 1.23.2.2 he * address, and choose the outgoing interface.
1734 1.23.2.2 he * XXX: is it a good approach?
1735 1.23.2.2 he */
1736 1.23.2.2 he if (IN6_IS_ADDR_MC_NODELOCAL(&mreq->ipv6mr_multiaddr)) {
1737 1.23.2.2 he ifp = &loif[0];
1738 1.23.2.2 he } else {
1739 1.23.2.2 he ro.ro_rt = NULL;
1740 1.23.2.2 he dst = (struct sockaddr_in6 *)&ro.ro_dst;
1741 1.23.2.2 he bzero(dst, sizeof(*dst));
1742 1.23.2.2 he dst->sin6_len = sizeof(struct sockaddr_in6);
1743 1.23.2.2 he dst->sin6_family = AF_INET6;
1744 1.23.2.2 he dst->sin6_addr = mreq->ipv6mr_multiaddr;
1745 1.23.2.2 he rtalloc((struct route *)&ro);
1746 1.23.2.2 he if (ro.ro_rt == NULL) {
1747 1.23.2.2 he error = EADDRNOTAVAIL;
1748 1.23.2.2 he break;
1749 1.23.2.2 he }
1750 1.23.2.2 he ifp = ro.ro_rt->rt_ifp;
1751 1.23.2.2 he rtfree(ro.ro_rt);
1752 1.23.2.2 he }
1753 1.23.2.2 he } else
1754 1.23.2.2 he ifp = ifindex2ifnet[mreq->ipv6mr_interface];
1755 1.23.2.2 he
1756 1.23.2.2 he /*
1757 1.23.2.2 he * See if we found an interface, and confirm that it
1758 1.23.2.2 he * supports multicast
1759 1.23.2.2 he */
1760 1.23.2.2 he if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
1761 1.23.2.2 he error = EADDRNOTAVAIL;
1762 1.23.2.2 he break;
1763 1.23.2.2 he }
1764 1.23.2.2 he /*
1765 1.23.2.2 he * Put interface index into the multicast address,
1766 1.23.2.2 he * if the address has link-local scope.
1767 1.23.2.2 he */
1768 1.23.2.2 he if (IN6_IS_ADDR_MC_LINKLOCAL(&mreq->ipv6mr_multiaddr)) {
1769 1.23.2.2 he mreq->ipv6mr_multiaddr.s6_addr16[1]
1770 1.23.2.2 he = htons(mreq->ipv6mr_interface);
1771 1.23.2.2 he }
1772 1.23.2.2 he /*
1773 1.23.2.2 he * See if the membership already exists.
1774 1.23.2.2 he */
1775 1.23.2.2 he for (imm = im6o->im6o_memberships.lh_first;
1776 1.23.2.2 he imm != NULL; imm = imm->i6mm_chain.le_next)
1777 1.23.2.2 he if (imm->i6mm_maddr->in6m_ifp == ifp &&
1778 1.23.2.2 he IN6_ARE_ADDR_EQUAL(&imm->i6mm_maddr->in6m_addr,
1779 1.23.2.2 he &mreq->ipv6mr_multiaddr))
1780 1.23.2.2 he break;
1781 1.23.2.2 he if (imm != NULL) {
1782 1.23.2.2 he error = EADDRINUSE;
1783 1.23.2.2 he break;
1784 1.23.2.2 he }
1785 1.23.2.2 he /*
1786 1.23.2.2 he * Everything looks good; add a new record to the multicast
1787 1.23.2.2 he * address list for the given interface.
1788 1.23.2.2 he */
1789 1.23.2.2 he imm = malloc(sizeof(*imm), M_IPMADDR, M_WAITOK);
1790 1.23.2.2 he if (imm == NULL) {
1791 1.23.2.2 he error = ENOBUFS;
1792 1.23.2.2 he break;
1793 1.23.2.2 he }
1794 1.23.2.2 he if ((imm->i6mm_maddr =
1795 1.23.2.2 he in6_addmulti(&mreq->ipv6mr_multiaddr, ifp, &error)) == NULL) {
1796 1.23.2.2 he free(imm, M_IPMADDR);
1797 1.23.2.2 he break;
1798 1.23.2.2 he }
1799 1.23.2.2 he LIST_INSERT_HEAD(&im6o->im6o_memberships, imm, i6mm_chain);
1800 1.23.2.2 he break;
1801 1.23.2.2 he
1802 1.23.2.2 he case IPV6_LEAVE_GROUP:
1803 1.23.2.2 he /*
1804 1.23.2.2 he * Drop a multicast group membership.
1805 1.23.2.2 he * Group must be a valid IP6 multicast address.
1806 1.23.2.2 he */
1807 1.23.2.2 he if (m == NULL || m->m_len != sizeof(struct ipv6_mreq)) {
1808 1.23.2.2 he error = EINVAL;
1809 1.23.2.2 he break;
1810 1.23.2.2 he }
1811 1.23.2.2 he mreq = mtod(m, struct ipv6_mreq *);
1812 1.23.2.2 he if (IN6_IS_ADDR_UNSPECIFIED(&mreq->ipv6mr_multiaddr)) {
1813 1.23.2.2 he if (suser(p->p_ucred, &p->p_acflag)) {
1814 1.23.2.2 he error = EACCES;
1815 1.23.2.2 he break;
1816 1.23.2.2 he }
1817 1.23.2.2 he } else if (!IN6_IS_ADDR_MULTICAST(&mreq->ipv6mr_multiaddr)) {
1818 1.23.2.2 he error = EINVAL;
1819 1.23.2.2 he break;
1820 1.23.2.2 he }
1821 1.23.2.2 he /*
1822 1.23.2.2 he * If an interface address was specified, get a pointer
1823 1.23.2.2 he * to its ifnet structure.
1824 1.23.2.2 he */
1825 1.23.2.2 he if (mreq->ipv6mr_interface < 0
1826 1.23.2.2 he || if_index < mreq->ipv6mr_interface) {
1827 1.23.2.2 he error = ENXIO; /* XXX EINVAL? */
1828 1.23.2.2 he break;
1829 1.23.2.2 he }
1830 1.23.2.2 he ifp = ifindex2ifnet[mreq->ipv6mr_interface];
1831 1.23.2.2 he /*
1832 1.23.2.2 he * Put interface index into the multicast address,
1833 1.23.2.2 he * if the address has link-local scope.
1834 1.23.2.2 he */
1835 1.23.2.2 he if (IN6_IS_ADDR_MC_LINKLOCAL(&mreq->ipv6mr_multiaddr)) {
1836 1.23.2.2 he mreq->ipv6mr_multiaddr.s6_addr16[1]
1837 1.23.2.2 he = htons(mreq->ipv6mr_interface);
1838 1.23.2.2 he }
1839 1.23.2.2 he /*
1840 1.23.2.2 he * Find the membership in the membership list.
1841 1.23.2.2 he */
1842 1.23.2.2 he for (imm = im6o->im6o_memberships.lh_first;
1843 1.23.2.2 he imm != NULL; imm = imm->i6mm_chain.le_next) {
1844 1.23.2.2 he if ((ifp == NULL ||
1845 1.23.2.2 he imm->i6mm_maddr->in6m_ifp == ifp) &&
1846 1.23.2.2 he IN6_ARE_ADDR_EQUAL(&imm->i6mm_maddr->in6m_addr,
1847 1.23.2.2 he &mreq->ipv6mr_multiaddr))
1848 1.23.2.2 he break;
1849 1.23.2.2 he }
1850 1.23.2.2 he if (imm == NULL) {
1851 1.23.2.2 he /* Unable to resolve interface */
1852 1.23.2.2 he error = EADDRNOTAVAIL;
1853 1.23.2.2 he break;
1854 1.23.2.2 he }
1855 1.23.2.2 he /*
1856 1.23.2.2 he * Give up the multicast address record to which the
1857 1.23.2.2 he * membership points.
1858 1.23.2.2 he */
1859 1.23.2.2 he LIST_REMOVE(imm, i6mm_chain);
1860 1.23.2.2 he in6_delmulti(imm->i6mm_maddr);
1861 1.23.2.2 he free(imm, M_IPMADDR);
1862 1.23.2.2 he break;
1863 1.23.2.2 he
1864 1.23.2.2 he default:
1865 1.23.2.2 he error = EOPNOTSUPP;
1866 1.23.2.2 he break;
1867 1.23.2.2 he }
1868 1.23.2.2 he
1869 1.23.2.2 he /*
1870 1.23.2.2 he * If all options have default values, no need to keep the mbuf.
1871 1.23.2.2 he */
1872 1.23.2.2 he if (im6o->im6o_multicast_ifp == NULL &&
1873 1.23.2.2 he im6o->im6o_multicast_hlim == ip6_defmcasthlim &&
1874 1.23.2.2 he im6o->im6o_multicast_loop == IPV6_DEFAULT_MULTICAST_LOOP &&
1875 1.23.2.2 he im6o->im6o_memberships.lh_first == NULL) {
1876 1.23.2.2 he free(*im6op, M_IPMOPTS);
1877 1.23.2.2 he *im6op = NULL;
1878 1.23.2.2 he }
1879 1.23.2.2 he
1880 1.23.2.2 he return(error);
1881 1.23.2.2 he }
1882 1.23.2.2 he
1883 1.23.2.2 he /*
1884 1.23.2.2 he * Return the IP6 multicast options in response to user getsockopt().
1885 1.23.2.2 he */
1886 1.23.2.2 he static int
1887 1.23.2.2 he ip6_getmoptions(optname, im6o, mp)
1888 1.23.2.2 he int optname;
1889 1.23.2.2 he register struct ip6_moptions *im6o;
1890 1.23.2.2 he register struct mbuf **mp;
1891 1.23.2.2 he {
1892 1.23.2.2 he u_int *hlim, *loop, *ifindex;
1893 1.23.2.2 he
1894 1.23.2.2 he *mp = m_get(M_WAIT, MT_SOOPTS);
1895 1.23.2.2 he
1896 1.23.2.2 he switch (optname) {
1897 1.23.2.2 he
1898 1.23.2.2 he case IPV6_MULTICAST_IF:
1899 1.23.2.2 he ifindex = mtod(*mp, u_int *);
1900 1.23.2.2 he (*mp)->m_len = sizeof(u_int);
1901 1.23.2.2 he if (im6o == NULL || im6o->im6o_multicast_ifp == NULL)
1902 1.23.2.2 he *ifindex = 0;
1903 1.23.2.2 he else
1904 1.23.2.2 he *ifindex = im6o->im6o_multicast_ifp->if_index;
1905 1.23.2.2 he return(0);
1906 1.23.2.2 he
1907 1.23.2.2 he case IPV6_MULTICAST_HOPS:
1908 1.23.2.2 he hlim = mtod(*mp, u_int *);
1909 1.23.2.2 he (*mp)->m_len = sizeof(u_int);
1910 1.23.2.2 he if (im6o == NULL)
1911 1.23.2.2 he *hlim = ip6_defmcasthlim;
1912 1.23.2.2 he else
1913 1.23.2.2 he *hlim = im6o->im6o_multicast_hlim;
1914 1.23.2.2 he return(0);
1915 1.23.2.2 he
1916 1.23.2.2 he case IPV6_MULTICAST_LOOP:
1917 1.23.2.2 he loop = mtod(*mp, u_int *);
1918 1.23.2.2 he (*mp)->m_len = sizeof(u_int);
1919 1.23.2.2 he if (im6o == NULL)
1920 1.23.2.2 he *loop = ip6_defmcasthlim;
1921 1.23.2.2 he else
1922 1.23.2.2 he *loop = im6o->im6o_multicast_loop;
1923 1.23.2.2 he return(0);
1924 1.23.2.2 he
1925 1.23.2.2 he default:
1926 1.23.2.2 he return(EOPNOTSUPP);
1927 1.23.2.2 he }
1928 1.23.2.2 he }
1929 1.23.2.2 he
1930 1.23.2.2 he /*
1931 1.23.2.2 he * Discard the IP6 multicast options.
1932 1.23.2.2 he */
1933 1.23.2.2 he void
1934 1.23.2.2 he ip6_freemoptions(im6o)
1935 1.23.2.2 he register struct ip6_moptions *im6o;
1936 1.23.2.2 he {
1937 1.23.2.2 he struct in6_multi_mship *imm;
1938 1.23.2.2 he
1939 1.23.2.2 he if (im6o == NULL)
1940 1.23.2.2 he return;
1941 1.23.2.2 he
1942 1.23.2.2 he while ((imm = im6o->im6o_memberships.lh_first) != NULL) {
1943 1.23.2.2 he LIST_REMOVE(imm, i6mm_chain);
1944 1.23.2.2 he if (imm->i6mm_maddr)
1945 1.23.2.2 he in6_delmulti(imm->i6mm_maddr);
1946 1.23.2.2 he free(imm, M_IPMADDR);
1947 1.23.2.2 he }
1948 1.23.2.2 he free(im6o, M_IPMOPTS);
1949 1.23.2.2 he }
1950 1.23.2.2 he
1951 1.23.2.2 he /*
1952 1.23.2.2 he * Set IPv6 outgoing packet options based on advanced API.
1953 1.23.2.2 he */
1954 1.23.2.2 he int
1955 1.23.2.2 he ip6_setpktoptions(control, opt, priv)
1956 1.23.2.2 he struct mbuf *control;
1957 1.23.2.2 he struct ip6_pktopts *opt;
1958 1.23.2.2 he int priv;
1959 1.23.2.2 he {
1960 1.23.2.2 he register struct cmsghdr *cm = 0;
1961 1.23.2.2 he
1962 1.23.2.2 he if (control == 0 || opt == 0)
1963 1.23.2.2 he return(EINVAL);
1964 1.23.2.2 he
1965 1.23.2.2 he bzero(opt, sizeof(*opt));
1966 1.23.2.2 he opt->ip6po_hlim = -1; /* -1 means to use default hop limit */
1967 1.23.2.2 he
1968 1.23.2.2 he /*
1969 1.23.2.2 he * XXX: Currently, we assume all the optional information is stored
1970 1.23.2.2 he * in a single mbuf.
1971 1.23.2.2 he */
1972 1.23.2.2 he if (control->m_next)
1973 1.23.2.2 he return(EINVAL);
1974 1.23.2.2 he
1975 1.23.2.2 he opt->ip6po_m = control;
1976 1.23.2.2 he
1977 1.23.2.2 he for (; control->m_len; control->m_data += CMSG_ALIGN(cm->cmsg_len),
1978 1.23.2.2 he control->m_len -= CMSG_ALIGN(cm->cmsg_len)) {
1979 1.23.2.2 he cm = mtod(control, struct cmsghdr *);
1980 1.23.2.2 he if (cm->cmsg_len == 0 || cm->cmsg_len > control->m_len)
1981 1.23.2.2 he return(EINVAL);
1982 1.23.2.2 he if (cm->cmsg_level != IPPROTO_IPV6)
1983 1.23.2.2 he continue;
1984 1.23.2.2 he
1985 1.23.2.2 he switch(cm->cmsg_type) {
1986 1.23.2.2 he case IPV6_PKTINFO:
1987 1.23.2.2 he if (cm->cmsg_len != CMSG_LEN(sizeof(struct in6_pktinfo)))
1988 1.23.2.2 he return(EINVAL);
1989 1.23.2.2 he opt->ip6po_pktinfo = (struct in6_pktinfo *)CMSG_DATA(cm);
1990 1.23.2.2 he if (opt->ip6po_pktinfo->ipi6_ifindex &&
1991 1.23.2.2 he IN6_IS_ADDR_LINKLOCAL(&opt->ip6po_pktinfo->ipi6_addr))
1992 1.23.2.2 he opt->ip6po_pktinfo->ipi6_addr.s6_addr16[1] =
1993 1.23.2.2 he htons(opt->ip6po_pktinfo->ipi6_ifindex);
1994 1.23.2.2 he
1995 1.23.2.2 he if (opt->ip6po_pktinfo->ipi6_ifindex > if_index
1996 1.23.2.2 he || opt->ip6po_pktinfo->ipi6_ifindex < 0) {
1997 1.23.2.2 he return(ENXIO);
1998 1.23.2.2 he }
1999 1.23.2.2 he
2000 1.23.2.2 he /*
2001 1.23.2.2 he * Check if the requested source address is indeed a
2002 1.23.2.5 he * unicast address assigned to the node, and can be
2003 1.23.2.5 he * used as the packet's source address.
2004 1.23.2.2 he */
2005 1.23.2.2 he if (!IN6_IS_ADDR_UNSPECIFIED(&opt->ip6po_pktinfo->ipi6_addr)) {
2006 1.23.2.2 he struct ifaddr *ia;
2007 1.23.2.5 he struct in6_ifaddr *ia6;
2008 1.23.2.2 he struct sockaddr_in6 sin6;
2009 1.23.2.2 he
2010 1.23.2.2 he bzero(&sin6, sizeof(sin6));
2011 1.23.2.2 he sin6.sin6_len = sizeof(sin6);
2012 1.23.2.2 he sin6.sin6_family = AF_INET6;
2013 1.23.2.2 he sin6.sin6_addr =
2014 1.23.2.2 he opt->ip6po_pktinfo->ipi6_addr;
2015 1.23.2.2 he ia = ifa_ifwithaddr(sin6tosa(&sin6));
2016 1.23.2.2 he if (ia == NULL ||
2017 1.23.2.2 he (opt->ip6po_pktinfo->ipi6_ifindex &&
2018 1.23.2.2 he (ia->ifa_ifp->if_index !=
2019 1.23.2.2 he opt->ip6po_pktinfo->ipi6_ifindex))) {
2020 1.23.2.2 he return(EADDRNOTAVAIL);
2021 1.23.2.2 he }
2022 1.23.2.5 he ia6 = (struct in6_ifaddr *)ia;
2023 1.23.2.5 he if ((ia6->ia6_flags & (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY)) != 0) {
2024 1.23.2.5 he return(EADDRNOTAVAIL);
2025 1.23.2.5 he }
2026 1.23.2.5 he
2027 1.23.2.2 he /*
2028 1.23.2.2 he * Check if the requested source address is
2029 1.23.2.2 he * indeed a unicast address assigned to the
2030 1.23.2.2 he * node.
2031 1.23.2.2 he */
2032 1.23.2.2 he if (IN6_IS_ADDR_MULTICAST(&opt->ip6po_pktinfo->ipi6_addr))
2033 1.23.2.2 he return(EADDRNOTAVAIL);
2034 1.23.2.2 he }
2035 1.23.2.2 he break;
2036 1.23.2.2 he
2037 1.23.2.2 he case IPV6_HOPLIMIT:
2038 1.23.2.2 he if (cm->cmsg_len != CMSG_LEN(sizeof(int)))
2039 1.23.2.2 he return(EINVAL);
2040 1.23.2.2 he
2041 1.23.2.2 he opt->ip6po_hlim = *(int *)CMSG_DATA(cm);
2042 1.23.2.2 he if (opt->ip6po_hlim < -1 || opt->ip6po_hlim > 255)
2043 1.23.2.2 he return(EINVAL);
2044 1.23.2.2 he break;
2045 1.23.2.2 he
2046 1.23.2.2 he case IPV6_NEXTHOP:
2047 1.23.2.2 he if (!priv)
2048 1.23.2.2 he return(EPERM);
2049 1.23.2.2 he
2050 1.23.2.2 he if (cm->cmsg_len < sizeof(u_char) ||
2051 1.23.2.2 he /* check if cmsg_len is large enough for sa_len */
2052 1.23.2.2 he cm->cmsg_len < CMSG_LEN(*CMSG_DATA(cm)))
2053 1.23.2.2 he return(EINVAL);
2054 1.23.2.2 he
2055 1.23.2.2 he opt->ip6po_nexthop = (struct sockaddr *)CMSG_DATA(cm);
2056 1.23.2.2 he
2057 1.23.2.2 he break;
2058 1.23.2.2 he
2059 1.23.2.2 he case IPV6_HOPOPTS:
2060 1.23.2.2 he if (cm->cmsg_len < CMSG_LEN(sizeof(struct ip6_hbh)))
2061 1.23.2.2 he return(EINVAL);
2062 1.23.2.2 he opt->ip6po_hbh = (struct ip6_hbh *)CMSG_DATA(cm);
2063 1.23.2.2 he if (cm->cmsg_len !=
2064 1.23.2.2 he CMSG_LEN((opt->ip6po_hbh->ip6h_len + 1) << 3))
2065 1.23.2.2 he return(EINVAL);
2066 1.23.2.2 he break;
2067 1.23.2.2 he
2068 1.23.2.2 he case IPV6_DSTOPTS:
2069 1.23.2.2 he if (cm->cmsg_len < CMSG_LEN(sizeof(struct ip6_dest)))
2070 1.23.2.2 he return(EINVAL);
2071 1.23.2.2 he
2072 1.23.2.2 he /*
2073 1.23.2.2 he * If there is no routing header yet, the destination
2074 1.23.2.2 he * options header should be put on the 1st part.
2075 1.23.2.2 he * Otherwise, the header should be on the 2nd part.
2076 1.23.2.2 he * (See RFC 2460, section 4.1)
2077 1.23.2.2 he */
2078 1.23.2.2 he if (opt->ip6po_rthdr == NULL) {
2079 1.23.2.2 he opt->ip6po_dest1 =
2080 1.23.2.2 he (struct ip6_dest *)CMSG_DATA(cm);
2081 1.23.2.2 he if (cm->cmsg_len !=
2082 1.23.2.2 he CMSG_LEN((opt->ip6po_dest1->ip6d_len + 1)
2083 1.23.2.2 he << 3))
2084 1.23.2.2 he return(EINVAL);
2085 1.23.2.2 he }
2086 1.23.2.2 he else {
2087 1.23.2.2 he opt->ip6po_dest2 =
2088 1.23.2.2 he (struct ip6_dest *)CMSG_DATA(cm);
2089 1.23.2.2 he if (cm->cmsg_len !=
2090 1.23.2.2 he CMSG_LEN((opt->ip6po_dest2->ip6d_len + 1)
2091 1.23.2.2 he << 3))
2092 1.23.2.2 he return(EINVAL);
2093 1.23.2.2 he }
2094 1.23.2.2 he break;
2095 1.23.2.2 he
2096 1.23.2.2 he case IPV6_RTHDR:
2097 1.23.2.2 he if (cm->cmsg_len < CMSG_LEN(sizeof(struct ip6_rthdr)))
2098 1.23.2.2 he return(EINVAL);
2099 1.23.2.2 he opt->ip6po_rthdr = (struct ip6_rthdr *)CMSG_DATA(cm);
2100 1.23.2.2 he if (cm->cmsg_len !=
2101 1.23.2.2 he CMSG_LEN((opt->ip6po_rthdr->ip6r_len + 1) << 3))
2102 1.23.2.2 he return(EINVAL);
2103 1.23.2.2 he switch(opt->ip6po_rthdr->ip6r_type) {
2104 1.23.2.2 he case IPV6_RTHDR_TYPE_0:
2105 1.23.2.2 he if (opt->ip6po_rthdr->ip6r_segleft == 0)
2106 1.23.2.2 he return(EINVAL);
2107 1.23.2.2 he break;
2108 1.23.2.2 he default:
2109 1.23.2.2 he return(EINVAL);
2110 1.23.2.2 he }
2111 1.23.2.2 he break;
2112 1.23.2.2 he
2113 1.23.2.2 he default:
2114 1.23.2.2 he return(ENOPROTOOPT);
2115 1.23.2.2 he }
2116 1.23.2.2 he }
2117 1.23.2.2 he
2118 1.23.2.2 he return(0);
2119 1.23.2.2 he }
2120 1.23.2.2 he
2121 1.23.2.2 he /*
2122 1.23.2.2 he * Routine called from ip6_output() to loop back a copy of an IP6 multicast
2123 1.23.2.2 he * packet to the input queue of a specified interface. Note that this
2124 1.23.2.2 he * calls the output routine of the loopback "driver", but with an interface
2125 1.23.2.2 he * pointer that might NOT be &loif -- easier than replicating that code here.
2126 1.23.2.2 he */
2127 1.23.2.2 he void
2128 1.23.2.2 he ip6_mloopback(ifp, m, dst)
2129 1.23.2.2 he struct ifnet *ifp;
2130 1.23.2.2 he register struct mbuf *m;
2131 1.23.2.2 he register struct sockaddr_in6 *dst;
2132 1.23.2.2 he {
2133 1.23.2.2 he struct mbuf *copym;
2134 1.23.2.2 he struct ip6_hdr *ip6;
2135 1.23.2.2 he
2136 1.23.2.2 he copym = m_copy(m, 0, M_COPYALL);
2137 1.23.2.2 he if (copym == NULL)
2138 1.23.2.2 he return;
2139 1.23.2.2 he
2140 1.23.2.2 he /*
2141 1.23.2.2 he * Make sure to deep-copy IPv6 header portion in case the data
2142 1.23.2.2 he * is in an mbuf cluster, so that we can safely override the IPv6
2143 1.23.2.2 he * header portion later.
2144 1.23.2.2 he */
2145 1.23.2.2 he if ((copym->m_flags & M_EXT) != 0 ||
2146 1.23.2.2 he copym->m_len < sizeof(struct ip6_hdr)) {
2147 1.23.2.2 he copym = m_pullup(copym, sizeof(struct ip6_hdr));
2148 1.23.2.2 he if (copym == NULL)
2149 1.23.2.2 he return;
2150 1.23.2.2 he }
2151 1.23.2.2 he
2152 1.23.2.2 he #ifdef DIAGNOSTIC
2153 1.23.2.2 he if (copym->m_len < sizeof(*ip6)) {
2154 1.23.2.2 he m_freem(copym);
2155 1.23.2.2 he return;
2156 1.23.2.2 he }
2157 1.23.2.2 he #endif
2158 1.23.2.2 he
2159 1.23.2.2 he #ifndef FAKE_LOOPBACK_IF
2160 1.23.2.2 he if ((ifp->if_flags & IFF_LOOPBACK) == 0)
2161 1.23.2.2 he #else
2162 1.23.2.2 he if (1)
2163 1.23.2.2 he #endif
2164 1.23.2.2 he {
2165 1.23.2.2 he ip6 = mtod(copym, struct ip6_hdr *);
2166 1.23.2.2 he if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src))
2167 1.23.2.2 he ip6->ip6_src.s6_addr16[1] = 0;
2168 1.23.2.2 he if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst))
2169 1.23.2.2 he ip6->ip6_dst.s6_addr16[1] = 0;
2170 1.23.2.2 he }
2171 1.23.2.2 he
2172 1.23.2.2 he (void)looutput(ifp, copym, (struct sockaddr *)dst, NULL);
2173 1.23.2.2 he }
2174 1.23.2.2 he
2175 1.23.2.2 he /*
2176 1.23.2.2 he * Chop IPv6 header off from the payload.
2177 1.23.2.2 he */
2178 1.23.2.2 he static int
2179 1.23.2.2 he ip6_splithdr(m, exthdrs)
2180 1.23.2.2 he struct mbuf *m;
2181 1.23.2.2 he struct ip6_exthdrs *exthdrs;
2182 1.23.2.2 he {
2183 1.23.2.2 he struct mbuf *mh;
2184 1.23.2.2 he struct ip6_hdr *ip6;
2185 1.23.2.2 he
2186 1.23.2.2 he ip6 = mtod(m, struct ip6_hdr *);
2187 1.23.2.2 he if (m->m_len > sizeof(*ip6)) {
2188 1.23.2.2 he MGETHDR(mh, M_DONTWAIT, MT_HEADER);
2189 1.23.2.2 he if (mh == 0) {
2190 1.23.2.2 he m_freem(m);
2191 1.23.2.2 he return ENOBUFS;
2192 1.23.2.2 he }
2193 1.23.2.2 he M_COPY_PKTHDR(mh, m);
2194 1.23.2.2 he MH_ALIGN(mh, sizeof(*ip6));
2195 1.23.2.2 he m->m_flags &= ~M_PKTHDR;
2196 1.23.2.2 he m->m_len -= sizeof(*ip6);
2197 1.23.2.2 he m->m_data += sizeof(*ip6);
2198 1.23.2.2 he mh->m_next = m;
2199 1.23.2.2 he m = mh;
2200 1.23.2.2 he m->m_len = sizeof(*ip6);
2201 1.23.2.2 he bcopy((caddr_t)ip6, mtod(m, caddr_t), sizeof(*ip6));
2202 1.23.2.2 he }
2203 1.23.2.2 he exthdrs->ip6e_ip6 = m;
2204 1.23.2.2 he return 0;
2205 1.23.2.2 he }
2206 1.23.2.2 he
2207 1.23.2.2 he /*
2208 1.23.2.2 he * Compute IPv6 extension header length.
2209 1.23.2.2 he */
2210 1.23.2.2 he int
2211 1.23.2.2 he ip6_optlen(in6p)
2212 1.23.2.2 he struct in6pcb *in6p;
2213 1.23.2.2 he {
2214 1.23.2.2 he int len;
2215 1.23.2.2 he
2216 1.23.2.2 he if (!in6p->in6p_outputopts)
2217 1.23.2.2 he return 0;
2218 1.23.2.2 he
2219 1.23.2.2 he len = 0;
2220 1.23.2.2 he #define elen(x) \
2221 1.23.2.2 he (((struct ip6_ext *)(x)) ? (((struct ip6_ext *)(x))->ip6e_len + 1) << 3 : 0)
2222 1.23.2.2 he
2223 1.23.2.2 he len += elen(in6p->in6p_outputopts->ip6po_hbh);
2224 1.23.2.2 he len += elen(in6p->in6p_outputopts->ip6po_dest1);
2225 1.23.2.2 he len += elen(in6p->in6p_outputopts->ip6po_rthdr);
2226 1.23.2.2 he len += elen(in6p->in6p_outputopts->ip6po_dest2);
2227 1.23.2.2 he return len;
2228 1.23.2.2 he #undef elen
2229 1.23.2.2 he }
2230