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