raw_ip6.c revision 1.86.8.2 1 1.86.8.2 dyoung /* $NetBSD: raw_ip6.c,v 1.86.8.2 2007/07/19 20:48:59 dyoung Exp $ */
2 1.86.8.2 dyoung /* $KAME: raw_ip6.c,v 1.82 2001/07/23 18:57:56 jinmei Exp $ */
3 1.86.8.2 dyoung
4 1.86.8.2 dyoung /*
5 1.86.8.2 dyoung * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
6 1.86.8.2 dyoung * All rights reserved.
7 1.86.8.2 dyoung *
8 1.86.8.2 dyoung * Redistribution and use in source and binary forms, with or without
9 1.86.8.2 dyoung * modification, are permitted provided that the following conditions
10 1.86.8.2 dyoung * are met:
11 1.86.8.2 dyoung * 1. Redistributions of source code must retain the above copyright
12 1.86.8.2 dyoung * notice, this list of conditions and the following disclaimer.
13 1.86.8.2 dyoung * 2. Redistributions in binary form must reproduce the above copyright
14 1.86.8.2 dyoung * notice, this list of conditions and the following disclaimer in the
15 1.86.8.2 dyoung * documentation and/or other materials provided with the distribution.
16 1.86.8.2 dyoung * 3. Neither the name of the project nor the names of its contributors
17 1.86.8.2 dyoung * may be used to endorse or promote products derived from this software
18 1.86.8.2 dyoung * without specific prior written permission.
19 1.86.8.2 dyoung *
20 1.86.8.2 dyoung * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 1.86.8.2 dyoung * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 1.86.8.2 dyoung * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 1.86.8.2 dyoung * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 1.86.8.2 dyoung * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 1.86.8.2 dyoung * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 1.86.8.2 dyoung * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 1.86.8.2 dyoung * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 1.86.8.2 dyoung * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 1.86.8.2 dyoung * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 1.86.8.2 dyoung * SUCH DAMAGE.
31 1.86.8.2 dyoung */
32 1.86.8.2 dyoung
33 1.86.8.2 dyoung /*
34 1.86.8.2 dyoung * Copyright (c) 1982, 1986, 1988, 1993
35 1.86.8.2 dyoung * The Regents of the University of California. All rights reserved.
36 1.86.8.2 dyoung *
37 1.86.8.2 dyoung * Redistribution and use in source and binary forms, with or without
38 1.86.8.2 dyoung * modification, are permitted provided that the following conditions
39 1.86.8.2 dyoung * are met:
40 1.86.8.2 dyoung * 1. Redistributions of source code must retain the above copyright
41 1.86.8.2 dyoung * notice, this list of conditions and the following disclaimer.
42 1.86.8.2 dyoung * 2. Redistributions in binary form must reproduce the above copyright
43 1.86.8.2 dyoung * notice, this list of conditions and the following disclaimer in the
44 1.86.8.2 dyoung * documentation and/or other materials provided with the distribution.
45 1.86.8.2 dyoung * 3. Neither the name of the University nor the names of its contributors
46 1.86.8.2 dyoung * may be used to endorse or promote products derived from this software
47 1.86.8.2 dyoung * without specific prior written permission.
48 1.86.8.2 dyoung *
49 1.86.8.2 dyoung * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50 1.86.8.2 dyoung * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51 1.86.8.2 dyoung * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52 1.86.8.2 dyoung * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53 1.86.8.2 dyoung * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54 1.86.8.2 dyoung * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55 1.86.8.2 dyoung * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56 1.86.8.2 dyoung * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57 1.86.8.2 dyoung * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58 1.86.8.2 dyoung * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59 1.86.8.2 dyoung * SUCH DAMAGE.
60 1.86.8.2 dyoung *
61 1.86.8.2 dyoung * @(#)raw_ip.c 8.2 (Berkeley) 1/4/94
62 1.86.8.2 dyoung */
63 1.86.8.2 dyoung
64 1.86.8.2 dyoung #include <sys/cdefs.h>
65 1.86.8.2 dyoung __KERNEL_RCSID(0, "$NetBSD: raw_ip6.c,v 1.86.8.2 2007/07/19 20:48:59 dyoung Exp $");
66 1.86.8.2 dyoung
67 1.86.8.2 dyoung #include "opt_ipsec.h"
68 1.86.8.2 dyoung
69 1.86.8.2 dyoung #include <sys/param.h>
70 1.86.8.2 dyoung #include <sys/sysctl.h>
71 1.86.8.2 dyoung #include <sys/malloc.h>
72 1.86.8.2 dyoung #include <sys/mbuf.h>
73 1.86.8.2 dyoung #include <sys/socket.h>
74 1.86.8.2 dyoung #include <sys/protosw.h>
75 1.86.8.2 dyoung #include <sys/socketvar.h>
76 1.86.8.2 dyoung #include <sys/errno.h>
77 1.86.8.2 dyoung #include <sys/systm.h>
78 1.86.8.2 dyoung #include <sys/proc.h>
79 1.86.8.2 dyoung #include <sys/kauth.h>
80 1.86.8.2 dyoung
81 1.86.8.2 dyoung #include <net/if.h>
82 1.86.8.2 dyoung #include <net/route.h>
83 1.86.8.2 dyoung #include <net/if_types.h>
84 1.86.8.2 dyoung
85 1.86.8.2 dyoung #include <netinet/in.h>
86 1.86.8.2 dyoung #include <netinet/in_var.h>
87 1.86.8.2 dyoung #include <netinet/ip6.h>
88 1.86.8.2 dyoung #include <netinet6/ip6_var.h>
89 1.86.8.2 dyoung #include <netinet6/ip6_mroute.h>
90 1.86.8.2 dyoung #include <netinet/icmp6.h>
91 1.86.8.2 dyoung #include <netinet6/in6_pcb.h>
92 1.86.8.2 dyoung #include <netinet6/nd6.h>
93 1.86.8.2 dyoung #include <netinet6/ip6protosw.h>
94 1.86.8.2 dyoung #include <netinet6/scope6_var.h>
95 1.86.8.2 dyoung #include <netinet6/raw_ip6.h>
96 1.86.8.2 dyoung
97 1.86.8.2 dyoung #ifdef IPSEC
98 1.86.8.2 dyoung #include <netinet6/ipsec.h>
99 1.86.8.2 dyoung #endif /* IPSEC */
100 1.86.8.2 dyoung
101 1.86.8.2 dyoung #ifdef FAST_IPSEC
102 1.86.8.2 dyoung #include <netipsec/ipsec.h>
103 1.86.8.2 dyoung #include <netipsec/ipsec_var.h> /* XXX ipsecstat namespace */
104 1.86.8.2 dyoung #include <netipsec/ipsec6.h>
105 1.86.8.2 dyoung #endif
106 1.86.8.2 dyoung
107 1.86.8.2 dyoung #include "faith.h"
108 1.86.8.2 dyoung #if defined(NFAITH) && 0 < NFAITH
109 1.86.8.2 dyoung #include <net/if_faith.h>
110 1.86.8.2 dyoung #endif
111 1.86.8.2 dyoung
112 1.86.8.2 dyoung extern struct inpcbtable rawcbtable;
113 1.86.8.2 dyoung struct inpcbtable raw6cbtable;
114 1.86.8.2 dyoung #define ifatoia6(ifa) ((struct in6_ifaddr *)(ifa))
115 1.86.8.2 dyoung
116 1.86.8.2 dyoung /*
117 1.86.8.2 dyoung * Raw interface to IP6 protocol.
118 1.86.8.2 dyoung */
119 1.86.8.2 dyoung
120 1.86.8.2 dyoung struct rip6stat rip6stat;
121 1.86.8.2 dyoung
122 1.86.8.2 dyoung /*
123 1.86.8.2 dyoung * Initialize raw connection block queue.
124 1.86.8.2 dyoung */
125 1.86.8.2 dyoung void
126 1.86.8.2 dyoung rip6_init()
127 1.86.8.2 dyoung {
128 1.86.8.2 dyoung
129 1.86.8.2 dyoung in6_pcbinit(&raw6cbtable, 1, 1);
130 1.86.8.2 dyoung }
131 1.86.8.2 dyoung
132 1.86.8.2 dyoung /*
133 1.86.8.2 dyoung * Setup generic address and protocol structures
134 1.86.8.2 dyoung * for raw_input routine, then pass them along with
135 1.86.8.2 dyoung * mbuf chain.
136 1.86.8.2 dyoung */
137 1.86.8.2 dyoung int
138 1.86.8.2 dyoung rip6_input(struct mbuf **mp, int *offp, int proto)
139 1.86.8.2 dyoung {
140 1.86.8.2 dyoung struct mbuf *m = *mp;
141 1.86.8.2 dyoung struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
142 1.86.8.2 dyoung struct inpcb_hdr *inph;
143 1.86.8.2 dyoung struct in6pcb *in6p;
144 1.86.8.2 dyoung struct in6pcb *last = NULL;
145 1.86.8.2 dyoung struct sockaddr_in6 rip6src;
146 1.86.8.2 dyoung struct mbuf *opts = NULL;
147 1.86.8.2 dyoung
148 1.86.8.2 dyoung rip6stat.rip6s_ipackets++;
149 1.86.8.2 dyoung
150 1.86.8.2 dyoung #if defined(NFAITH) && 0 < NFAITH
151 1.86.8.2 dyoung if (faithprefix(&ip6->ip6_dst)) {
152 1.86.8.2 dyoung /* send icmp6 host unreach? */
153 1.86.8.2 dyoung m_freem(m);
154 1.86.8.2 dyoung return IPPROTO_DONE;
155 1.86.8.2 dyoung }
156 1.86.8.2 dyoung #endif
157 1.86.8.2 dyoung
158 1.86.8.2 dyoung /* Be proactive about malicious use of IPv4 mapped address */
159 1.86.8.2 dyoung if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) ||
160 1.86.8.2 dyoung IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) {
161 1.86.8.2 dyoung /* XXX stat */
162 1.86.8.2 dyoung m_freem(m);
163 1.86.8.2 dyoung return IPPROTO_DONE;
164 1.86.8.2 dyoung }
165 1.86.8.2 dyoung
166 1.86.8.2 dyoung bzero(&rip6src, sizeof(rip6src));
167 1.86.8.2 dyoung rip6src.sin6_len = sizeof(struct sockaddr_in6);
168 1.86.8.2 dyoung rip6src.sin6_family = AF_INET6;
169 1.86.8.2 dyoung rip6src.sin6_addr = ip6->ip6_src;
170 1.86.8.2 dyoung if (sa6_recoverscope(&rip6src) != 0) {
171 1.86.8.2 dyoung /* XXX: should be impossible. */
172 1.86.8.2 dyoung m_freem(m);
173 1.86.8.2 dyoung return IPPROTO_DONE;
174 1.86.8.2 dyoung }
175 1.86.8.2 dyoung
176 1.86.8.2 dyoung CIRCLEQ_FOREACH(inph, &raw6cbtable.inpt_queue, inph_queue) {
177 1.86.8.2 dyoung in6p = (struct in6pcb *)inph;
178 1.86.8.2 dyoung if (in6p->in6p_af != AF_INET6)
179 1.86.8.2 dyoung continue;
180 1.86.8.2 dyoung if (in6p->in6p_ip6.ip6_nxt &&
181 1.86.8.2 dyoung in6p->in6p_ip6.ip6_nxt != proto)
182 1.86.8.2 dyoung continue;
183 1.86.8.2 dyoung if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) &&
184 1.86.8.2 dyoung !IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, &ip6->ip6_dst))
185 1.86.8.2 dyoung continue;
186 1.86.8.2 dyoung if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr) &&
187 1.86.8.2 dyoung !IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, &ip6->ip6_src))
188 1.86.8.2 dyoung continue;
189 1.86.8.2 dyoung if (in6p->in6p_cksum != -1) {
190 1.86.8.2 dyoung rip6stat.rip6s_isum++;
191 1.86.8.2 dyoung if (in6_cksum(m, proto, *offp,
192 1.86.8.2 dyoung m->m_pkthdr.len - *offp)) {
193 1.86.8.2 dyoung rip6stat.rip6s_badsum++;
194 1.86.8.2 dyoung continue;
195 1.86.8.2 dyoung }
196 1.86.8.2 dyoung }
197 1.86.8.2 dyoung if (last) {
198 1.86.8.2 dyoung struct mbuf *n;
199 1.86.8.2 dyoung
200 1.86.8.2 dyoung #ifdef IPSEC
201 1.86.8.2 dyoung /*
202 1.86.8.2 dyoung * Check AH/ESP integrity.
203 1.86.8.2 dyoung */
204 1.86.8.2 dyoung if (ipsec6_in_reject(m, last)) {
205 1.86.8.2 dyoung ipsec6stat.in_polvio++;
206 1.86.8.2 dyoung /* do not inject data into pcb */
207 1.86.8.2 dyoung } else
208 1.86.8.2 dyoung #endif /* IPSEC */
209 1.86.8.2 dyoung #ifdef FAST_IPSEC
210 1.86.8.2 dyoung /*
211 1.86.8.2 dyoung * Check AH/ESP integrity
212 1.86.8.2 dyoung */
213 1.86.8.2 dyoung if (!ipsec6_in_reject(m,last))
214 1.86.8.2 dyoung #endif /* FAST_IPSEC */
215 1.86.8.2 dyoung if ((n = m_copy(m, 0, (int)M_COPYALL)) != NULL) {
216 1.86.8.2 dyoung if (last->in6p_flags & IN6P_CONTROLOPTS)
217 1.86.8.2 dyoung ip6_savecontrol(last, &opts, ip6, n);
218 1.86.8.2 dyoung /* strip intermediate headers */
219 1.86.8.2 dyoung m_adj(n, *offp);
220 1.86.8.2 dyoung if (sbappendaddr(&last->in6p_socket->so_rcv,
221 1.86.8.2 dyoung (struct sockaddr *)&rip6src, n, opts) == 0) {
222 1.86.8.2 dyoung /* should notify about lost packet */
223 1.86.8.2 dyoung m_freem(n);
224 1.86.8.2 dyoung if (opts)
225 1.86.8.2 dyoung m_freem(opts);
226 1.86.8.2 dyoung rip6stat.rip6s_fullsock++;
227 1.86.8.2 dyoung } else
228 1.86.8.2 dyoung sorwakeup(last->in6p_socket);
229 1.86.8.2 dyoung opts = NULL;
230 1.86.8.2 dyoung }
231 1.86.8.2 dyoung }
232 1.86.8.2 dyoung last = in6p;
233 1.86.8.2 dyoung }
234 1.86.8.2 dyoung #ifdef IPSEC
235 1.86.8.2 dyoung /*
236 1.86.8.2 dyoung * Check AH/ESP integrity.
237 1.86.8.2 dyoung */
238 1.86.8.2 dyoung if (last && ipsec6_in_reject(m, last)) {
239 1.86.8.2 dyoung m_freem(m);
240 1.86.8.2 dyoung ipsec6stat.in_polvio++;
241 1.86.8.2 dyoung ip6stat.ip6s_delivered--;
242 1.86.8.2 dyoung /* do not inject data into pcb */
243 1.86.8.2 dyoung } else
244 1.86.8.2 dyoung #endif /* IPSEC */
245 1.86.8.2 dyoung #ifdef FAST_IPSEC
246 1.86.8.2 dyoung if (last && ipsec6_in_reject(m, last)) {
247 1.86.8.2 dyoung m_freem(m);
248 1.86.8.2 dyoung /*
249 1.86.8.2 dyoung * XXX ipsec6_in_reject update stat if there is an error
250 1.86.8.2 dyoung * so we just need to update stats by hand in the case of last is
251 1.86.8.2 dyoung * NULL
252 1.86.8.2 dyoung */
253 1.86.8.2 dyoung if (!last)
254 1.86.8.2 dyoung ipsec6stat.in_polvio++;
255 1.86.8.2 dyoung ip6stat.ip6s_delivered--;
256 1.86.8.2 dyoung /* do not inject data into pcb */
257 1.86.8.2 dyoung } else
258 1.86.8.2 dyoung #endif /* FAST_IPSEC */
259 1.86.8.2 dyoung if (last) {
260 1.86.8.2 dyoung if (last->in6p_flags & IN6P_CONTROLOPTS)
261 1.86.8.2 dyoung ip6_savecontrol(last, &opts, ip6, m);
262 1.86.8.2 dyoung /* strip intermediate headers */
263 1.86.8.2 dyoung m_adj(m, *offp);
264 1.86.8.2 dyoung if (sbappendaddr(&last->in6p_socket->so_rcv,
265 1.86.8.2 dyoung (struct sockaddr *)&rip6src, m, opts) == 0) {
266 1.86.8.2 dyoung m_freem(m);
267 1.86.8.2 dyoung if (opts)
268 1.86.8.2 dyoung m_freem(opts);
269 1.86.8.2 dyoung rip6stat.rip6s_fullsock++;
270 1.86.8.2 dyoung } else
271 1.86.8.2 dyoung sorwakeup(last->in6p_socket);
272 1.86.8.2 dyoung } else {
273 1.86.8.2 dyoung rip6stat.rip6s_nosock++;
274 1.86.8.2 dyoung if (m->m_flags & M_MCAST)
275 1.86.8.2 dyoung rip6stat.rip6s_nosockmcast++;
276 1.86.8.2 dyoung if (proto == IPPROTO_NONE)
277 1.86.8.2 dyoung m_freem(m);
278 1.86.8.2 dyoung else {
279 1.86.8.2 dyoung u_int8_t *prvnxtp = ip6_get_prevhdr(m, *offp); /* XXX */
280 1.86.8.2 dyoung in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_protounknown);
281 1.86.8.2 dyoung icmp6_error(m, ICMP6_PARAM_PROB,
282 1.86.8.2 dyoung ICMP6_PARAMPROB_NEXTHEADER,
283 1.86.8.2 dyoung prvnxtp - mtod(m, u_int8_t *));
284 1.86.8.2 dyoung }
285 1.86.8.2 dyoung ip6stat.ip6s_delivered--;
286 1.86.8.2 dyoung }
287 1.86.8.2 dyoung return IPPROTO_DONE;
288 1.86.8.2 dyoung }
289 1.86.8.2 dyoung
290 1.86.8.2 dyoung void
291 1.86.8.2 dyoung rip6_ctlinput(int cmd, const struct sockaddr *sa, void *d)
292 1.86.8.2 dyoung {
293 1.86.8.2 dyoung struct ip6_hdr *ip6;
294 1.86.8.2 dyoung struct ip6ctlparam *ip6cp = NULL;
295 1.86.8.2 dyoung const struct sockaddr_in6 *sa6_src = NULL;
296 1.86.8.2 dyoung void *cmdarg;
297 1.86.8.2 dyoung void (*notify) __P((struct in6pcb *, int)) = in6_rtchange;
298 1.86.8.2 dyoung int nxt;
299 1.86.8.2 dyoung
300 1.86.8.2 dyoung if (sa->sa_family != AF_INET6 ||
301 1.86.8.2 dyoung sa->sa_len != sizeof(struct sockaddr_in6))
302 1.86.8.2 dyoung return;
303 1.86.8.2 dyoung
304 1.86.8.2 dyoung if ((unsigned)cmd >= PRC_NCMDS)
305 1.86.8.2 dyoung return;
306 1.86.8.2 dyoung if (PRC_IS_REDIRECT(cmd))
307 1.86.8.2 dyoung notify = in6_rtchange, d = NULL;
308 1.86.8.2 dyoung else if (cmd == PRC_HOSTDEAD)
309 1.86.8.2 dyoung d = NULL;
310 1.86.8.2 dyoung else if (cmd == PRC_MSGSIZE)
311 1.86.8.2 dyoung ; /* special code is present, see below */
312 1.86.8.2 dyoung else if (inet6ctlerrmap[cmd] == 0)
313 1.86.8.2 dyoung return;
314 1.86.8.2 dyoung
315 1.86.8.2 dyoung /* if the parameter is from icmp6, decode it. */
316 1.86.8.2 dyoung if (d != NULL) {
317 1.86.8.2 dyoung ip6cp = (struct ip6ctlparam *)d;
318 1.86.8.2 dyoung ip6 = ip6cp->ip6c_ip6;
319 1.86.8.2 dyoung cmdarg = ip6cp->ip6c_cmdarg;
320 1.86.8.2 dyoung sa6_src = ip6cp->ip6c_src;
321 1.86.8.2 dyoung nxt = ip6cp->ip6c_nxt;
322 1.86.8.2 dyoung } else {
323 1.86.8.2 dyoung ip6 = NULL;
324 1.86.8.2 dyoung cmdarg = NULL;
325 1.86.8.2 dyoung sa6_src = &sa6_any;
326 1.86.8.2 dyoung nxt = -1;
327 1.86.8.2 dyoung }
328 1.86.8.2 dyoung
329 1.86.8.2 dyoung if (ip6 && cmd == PRC_MSGSIZE) {
330 1.86.8.2 dyoung const struct sockaddr_in6 *sa6 = (const struct sockaddr_in6 *)sa;
331 1.86.8.2 dyoung int valid = 0;
332 1.86.8.2 dyoung struct in6pcb *in6p;
333 1.86.8.2 dyoung
334 1.86.8.2 dyoung /*
335 1.86.8.2 dyoung * Check to see if we have a valid raw IPv6 socket
336 1.86.8.2 dyoung * corresponding to the address in the ICMPv6 message
337 1.86.8.2 dyoung * payload, and the protocol (ip6_nxt) meets the socket.
338 1.86.8.2 dyoung * XXX chase extension headers, or pass final nxt value
339 1.86.8.2 dyoung * from icmp6_notify_error()
340 1.86.8.2 dyoung */
341 1.86.8.2 dyoung in6p = NULL;
342 1.86.8.2 dyoung in6p = in6_pcblookup_connect(&raw6cbtable, &sa6->sin6_addr, 0,
343 1.86.8.2 dyoung (const struct in6_addr *)&sa6_src->sin6_addr, 0, 0);
344 1.86.8.2 dyoung #if 0
345 1.86.8.2 dyoung if (!in6p) {
346 1.86.8.2 dyoung /*
347 1.86.8.2 dyoung * As the use of sendto(2) is fairly popular,
348 1.86.8.2 dyoung * we may want to allow non-connected pcb too.
349 1.86.8.2 dyoung * But it could be too weak against attacks...
350 1.86.8.2 dyoung * We should at least check if the local
351 1.86.8.2 dyoung * address (= s) is really ours.
352 1.86.8.2 dyoung */
353 1.86.8.2 dyoung in6p = in6_pcblookup_bind(&raw6cbtable,
354 1.86.8.2 dyoung &sa6->sin6_addr, 0, 0);
355 1.86.8.2 dyoung }
356 1.86.8.2 dyoung #endif
357 1.86.8.2 dyoung
358 1.86.8.2 dyoung if (in6p && in6p->in6p_ip6.ip6_nxt &&
359 1.86.8.2 dyoung in6p->in6p_ip6.ip6_nxt == nxt)
360 1.86.8.2 dyoung valid++;
361 1.86.8.2 dyoung
362 1.86.8.2 dyoung /*
363 1.86.8.2 dyoung * Depending on the value of "valid" and routing table
364 1.86.8.2 dyoung * size (mtudisc_{hi,lo}wat), we will:
365 1.86.8.2 dyoung * - recalculate the new MTU and create the
366 1.86.8.2 dyoung * corresponding routing entry, or
367 1.86.8.2 dyoung * - ignore the MTU change notification.
368 1.86.8.2 dyoung */
369 1.86.8.2 dyoung icmp6_mtudisc_update((struct ip6ctlparam *)d, valid);
370 1.86.8.2 dyoung
371 1.86.8.2 dyoung /*
372 1.86.8.2 dyoung * regardless of if we called icmp6_mtudisc_update(),
373 1.86.8.2 dyoung * we need to call in6_pcbnotify(), to notify path MTU
374 1.86.8.2 dyoung * change to the userland (RFC3542), because some
375 1.86.8.2 dyoung * unconnected sockets may share the same destination
376 1.86.8.2 dyoung * and want to know the path MTU.
377 1.86.8.2 dyoung */
378 1.86.8.2 dyoung }
379 1.86.8.2 dyoung
380 1.86.8.2 dyoung (void) in6_pcbnotify(&raw6cbtable, sa, 0,
381 1.86.8.2 dyoung (const struct sockaddr *)sa6_src, 0, cmd, cmdarg, notify);
382 1.86.8.2 dyoung }
383 1.86.8.2 dyoung
384 1.86.8.2 dyoung /*
385 1.86.8.2 dyoung * Generate IPv6 header and pass packet to ip6_output.
386 1.86.8.2 dyoung * Tack on options user may have setup with control call.
387 1.86.8.2 dyoung */
388 1.86.8.2 dyoung int
389 1.86.8.2 dyoung rip6_output(struct mbuf *m, struct socket *so, struct sockaddr_in6 *dstsock,
390 1.86.8.2 dyoung struct mbuf *control)
391 1.86.8.2 dyoung {
392 1.86.8.2 dyoung struct in6_addr *dst;
393 1.86.8.2 dyoung struct ip6_hdr *ip6;
394 1.86.8.2 dyoung struct in6pcb *in6p;
395 1.86.8.2 dyoung u_int plen = m->m_pkthdr.len;
396 1.86.8.2 dyoung int error = 0;
397 1.86.8.2 dyoung struct ip6_pktopts opt, *optp = NULL;
398 1.86.8.2 dyoung struct ifnet *oifp = NULL;
399 1.86.8.2 dyoung int type, code; /* for ICMPv6 output statistics only */
400 1.86.8.2 dyoung int priv = 0;
401 1.86.8.2 dyoung int scope_ambiguous = 0;
402 1.86.8.2 dyoung struct in6_addr *in6a;
403 1.86.8.2 dyoung
404 1.86.8.2 dyoung in6p = sotoin6pcb(so);
405 1.86.8.2 dyoung
406 1.86.8.2 dyoung priv = 0;
407 1.86.8.2 dyoung if (curlwp && !kauth_authorize_generic(curlwp->l_cred,
408 1.86.8.2 dyoung KAUTH_GENERIC_ISSUSER, NULL))
409 1.86.8.2 dyoung priv = 1;
410 1.86.8.2 dyoung
411 1.86.8.2 dyoung dst = &dstsock->sin6_addr;
412 1.86.8.2 dyoung if (control) {
413 1.86.8.2 dyoung if ((error = ip6_setpktopts(control, &opt,
414 1.86.8.2 dyoung in6p->in6p_outputopts,
415 1.86.8.2 dyoung priv, so->so_proto->pr_protocol)) != 0) {
416 1.86.8.2 dyoung goto bad;
417 1.86.8.2 dyoung }
418 1.86.8.2 dyoung optp = &opt;
419 1.86.8.2 dyoung } else
420 1.86.8.2 dyoung optp = in6p->in6p_outputopts;
421 1.86.8.2 dyoung
422 1.86.8.2 dyoung /*
423 1.86.8.2 dyoung * Check and convert scope zone ID into internal form.
424 1.86.8.2 dyoung * XXX: we may still need to determine the zone later.
425 1.86.8.2 dyoung */
426 1.86.8.2 dyoung if (!(so->so_state & SS_ISCONNECTED)) {
427 1.86.8.2 dyoung if (dstsock->sin6_scope_id == 0 && !ip6_use_defzone)
428 1.86.8.2 dyoung scope_ambiguous = 1;
429 1.86.8.2 dyoung if ((error = sa6_embedscope(dstsock, ip6_use_defzone)) != 0)
430 1.86.8.2 dyoung goto bad;
431 1.86.8.2 dyoung }
432 1.86.8.2 dyoung
433 1.86.8.2 dyoung /*
434 1.86.8.2 dyoung * For an ICMPv6 packet, we should know its type and code
435 1.86.8.2 dyoung * to update statistics.
436 1.86.8.2 dyoung */
437 1.86.8.2 dyoung if (so->so_proto->pr_protocol == IPPROTO_ICMPV6) {
438 1.86.8.2 dyoung struct icmp6_hdr *icmp6;
439 1.86.8.2 dyoung if (m->m_len < sizeof(struct icmp6_hdr) &&
440 1.86.8.2 dyoung (m = m_pullup(m, sizeof(struct icmp6_hdr))) == NULL) {
441 1.86.8.2 dyoung error = ENOBUFS;
442 1.86.8.2 dyoung goto bad;
443 1.86.8.2 dyoung }
444 1.86.8.2 dyoung icmp6 = mtod(m, struct icmp6_hdr *);
445 1.86.8.2 dyoung type = icmp6->icmp6_type;
446 1.86.8.2 dyoung code = icmp6->icmp6_code;
447 1.86.8.2 dyoung } else {
448 1.86.8.2 dyoung type = 0;
449 1.86.8.2 dyoung code = 0;
450 1.86.8.2 dyoung }
451 1.86.8.2 dyoung
452 1.86.8.2 dyoung M_PREPEND(m, sizeof(*ip6), M_DONTWAIT);
453 1.86.8.2 dyoung if (!m) {
454 1.86.8.2 dyoung error = ENOBUFS;
455 1.86.8.2 dyoung goto bad;
456 1.86.8.2 dyoung }
457 1.86.8.2 dyoung ip6 = mtod(m, struct ip6_hdr *);
458 1.86.8.2 dyoung
459 1.86.8.2 dyoung /*
460 1.86.8.2 dyoung * Next header might not be ICMP6 but use its pseudo header anyway.
461 1.86.8.2 dyoung */
462 1.86.8.2 dyoung ip6->ip6_dst = *dst;
463 1.86.8.2 dyoung
464 1.86.8.2 dyoung /*
465 1.86.8.2 dyoung * Source address selection.
466 1.86.8.2 dyoung */
467 1.86.8.2 dyoung if ((in6a = in6_selectsrc(dstsock, optp, in6p->in6p_moptions,
468 1.86.8.2 dyoung (struct route *)&in6p->in6p_route, &in6p->in6p_laddr, &oifp,
469 1.86.8.2 dyoung &error)) == 0) {
470 1.86.8.2 dyoung if (error == 0)
471 1.86.8.2 dyoung error = EADDRNOTAVAIL;
472 1.86.8.2 dyoung goto bad;
473 1.86.8.2 dyoung }
474 1.86.8.2 dyoung ip6->ip6_src = *in6a;
475 1.86.8.2 dyoung
476 1.86.8.2 dyoung if (oifp && scope_ambiguous) {
477 1.86.8.2 dyoung /*
478 1.86.8.2 dyoung * Application should provide a proper zone ID or the use of
479 1.86.8.2 dyoung * default zone IDs should be enabled. Unfortunately, some
480 1.86.8.2 dyoung * applications do not behave as it should, so we need a
481 1.86.8.2 dyoung * workaround. Even if an appropriate ID is not determined
482 1.86.8.2 dyoung * (when it's required), if we can determine the outgoing
483 1.86.8.2 dyoung * interface. determine the zone ID based on the interface.
484 1.86.8.2 dyoung */
485 1.86.8.2 dyoung error = in6_setscope(&dstsock->sin6_addr, oifp, NULL);
486 1.86.8.2 dyoung if (error != 0)
487 1.86.8.2 dyoung goto bad;
488 1.86.8.2 dyoung }
489 1.86.8.2 dyoung ip6->ip6_dst = dstsock->sin6_addr;
490 1.86.8.2 dyoung
491 1.86.8.2 dyoung /* fill in the rest of the IPv6 header fields */
492 1.86.8.2 dyoung ip6->ip6_flow = in6p->in6p_flowinfo & IPV6_FLOWINFO_MASK;
493 1.86.8.2 dyoung ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
494 1.86.8.2 dyoung ip6->ip6_vfc |= IPV6_VERSION;
495 1.86.8.2 dyoung /* ip6_plen will be filled in ip6_output, so not fill it here. */
496 1.86.8.2 dyoung ip6->ip6_nxt = in6p->in6p_ip6.ip6_nxt;
497 1.86.8.2 dyoung ip6->ip6_hlim = in6_selecthlim(in6p, oifp);
498 1.86.8.2 dyoung
499 1.86.8.2 dyoung if (so->so_proto->pr_protocol == IPPROTO_ICMPV6 ||
500 1.86.8.2 dyoung in6p->in6p_cksum != -1) {
501 1.86.8.2 dyoung int off;
502 1.86.8.2 dyoung u_int16_t sum;
503 1.86.8.2 dyoung
504 1.86.8.2 dyoung /* compute checksum */
505 1.86.8.2 dyoung if (so->so_proto->pr_protocol == IPPROTO_ICMPV6)
506 1.86.8.2 dyoung off = offsetof(struct icmp6_hdr, icmp6_cksum);
507 1.86.8.2 dyoung else
508 1.86.8.2 dyoung off = in6p->in6p_cksum;
509 1.86.8.2 dyoung if (plen < off + 1) {
510 1.86.8.2 dyoung error = EINVAL;
511 1.86.8.2 dyoung goto bad;
512 1.86.8.2 dyoung }
513 1.86.8.2 dyoung off += sizeof(struct ip6_hdr);
514 1.86.8.2 dyoung
515 1.86.8.2 dyoung sum = 0;
516 1.86.8.2 dyoung m = m_copyback_cow(m, off, sizeof(sum), (void *)&sum,
517 1.86.8.2 dyoung M_DONTWAIT);
518 1.86.8.2 dyoung if (m == NULL) {
519 1.86.8.2 dyoung error = ENOBUFS;
520 1.86.8.2 dyoung goto bad;
521 1.86.8.2 dyoung }
522 1.86.8.2 dyoung sum = in6_cksum(m, ip6->ip6_nxt, sizeof(*ip6), plen);
523 1.86.8.2 dyoung m = m_copyback_cow(m, off, sizeof(sum), (void *)&sum,
524 1.86.8.2 dyoung M_DONTWAIT);
525 1.86.8.2 dyoung if (m == NULL) {
526 1.86.8.2 dyoung error = ENOBUFS;
527 1.86.8.2 dyoung goto bad;
528 1.86.8.2 dyoung }
529 1.86.8.2 dyoung }
530 1.86.8.2 dyoung
531 1.86.8.2 dyoung error = ip6_output(m, optp, &in6p->in6p_route, 0,
532 1.86.8.2 dyoung in6p->in6p_moptions, so, &oifp);
533 1.86.8.2 dyoung if (so->so_proto->pr_protocol == IPPROTO_ICMPV6) {
534 1.86.8.2 dyoung if (oifp)
535 1.86.8.2 dyoung icmp6_ifoutstat_inc(oifp, type, code);
536 1.86.8.2 dyoung icmp6stat.icp6s_outhist[type]++;
537 1.86.8.2 dyoung } else
538 1.86.8.2 dyoung rip6stat.rip6s_opackets++;
539 1.86.8.2 dyoung
540 1.86.8.2 dyoung goto freectl;
541 1.86.8.2 dyoung
542 1.86.8.2 dyoung bad:
543 1.86.8.2 dyoung if (m)
544 1.86.8.2 dyoung m_freem(m);
545 1.86.8.2 dyoung
546 1.86.8.2 dyoung freectl:
547 1.86.8.2 dyoung if (control) {
548 1.86.8.2 dyoung ip6_clearpktopts(&opt, -1);
549 1.86.8.2 dyoung m_freem(control);
550 1.86.8.2 dyoung }
551 1.86.8.2 dyoung return error;
552 1.86.8.2 dyoung }
553 1.86.8.2 dyoung
554 1.86.8.2 dyoung /*
555 1.86.8.2 dyoung * Raw IPv6 socket option processing.
556 1.86.8.2 dyoung */
557 1.86.8.2 dyoung int
558 1.86.8.2 dyoung rip6_ctloutput(int op, struct socket *so, int level, int optname,
559 1.86.8.2 dyoung struct mbuf **mp)
560 1.86.8.2 dyoung {
561 1.86.8.2 dyoung int error = 0;
562 1.86.8.2 dyoung
563 1.86.8.2 dyoung switch (level) {
564 1.86.8.2 dyoung case IPPROTO_IPV6:
565 1.86.8.2 dyoung switch (optname) {
566 1.86.8.2 dyoung case MRT6_INIT:
567 1.86.8.2 dyoung case MRT6_DONE:
568 1.86.8.2 dyoung case MRT6_ADD_MIF:
569 1.86.8.2 dyoung case MRT6_DEL_MIF:
570 1.86.8.2 dyoung case MRT6_ADD_MFC:
571 1.86.8.2 dyoung case MRT6_DEL_MFC:
572 1.86.8.2 dyoung case MRT6_PIM:
573 1.86.8.2 dyoung if (op == PRCO_SETOPT) {
574 1.86.8.2 dyoung error = ip6_mrouter_set(optname, so, *mp);
575 1.86.8.2 dyoung if (*mp)
576 1.86.8.2 dyoung (void)m_free(*mp);
577 1.86.8.2 dyoung } else if (op == PRCO_GETOPT)
578 1.86.8.2 dyoung error = ip6_mrouter_get(optname, so, mp);
579 1.86.8.2 dyoung else
580 1.86.8.2 dyoung error = EINVAL;
581 1.86.8.2 dyoung return error;
582 1.86.8.2 dyoung case IPV6_CHECKSUM:
583 1.86.8.2 dyoung return ip6_raw_ctloutput(op, so, level, optname, mp);
584 1.86.8.2 dyoung default:
585 1.86.8.2 dyoung return ip6_ctloutput(op, so, level, optname, mp);
586 1.86.8.2 dyoung }
587 1.86.8.2 dyoung
588 1.86.8.2 dyoung case IPPROTO_ICMPV6:
589 1.86.8.2 dyoung /*
590 1.86.8.2 dyoung * XXX: is it better to call icmp6_ctloutput() directly
591 1.86.8.2 dyoung * from protosw?
592 1.86.8.2 dyoung */
593 1.86.8.2 dyoung return icmp6_ctloutput(op, so, level, optname, mp);
594 1.86.8.2 dyoung
595 1.86.8.2 dyoung default:
596 1.86.8.2 dyoung if (op == PRCO_SETOPT && *mp)
597 1.86.8.2 dyoung m_free(*mp);
598 1.86.8.2 dyoung return EINVAL;
599 1.86.8.2 dyoung }
600 1.86.8.2 dyoung }
601 1.86.8.2 dyoung
602 1.86.8.2 dyoung extern u_long rip6_sendspace;
603 1.86.8.2 dyoung extern u_long rip6_recvspace;
604 1.86.8.2 dyoung
605 1.86.8.2 dyoung int
606 1.86.8.2 dyoung rip6_usrreq(struct socket *so, int req, struct mbuf *m,
607 1.86.8.2 dyoung struct mbuf *nam, struct mbuf *control, struct lwp *l)
608 1.86.8.2 dyoung {
609 1.86.8.2 dyoung struct in6pcb *in6p = sotoin6pcb(so);
610 1.86.8.2 dyoung int s;
611 1.86.8.2 dyoung int error = 0;
612 1.86.8.2 dyoung int priv;
613 1.86.8.2 dyoung
614 1.86.8.2 dyoung priv = 0;
615 1.86.8.2 dyoung if (l && !kauth_authorize_generic(l->l_cred,
616 1.86.8.2 dyoung KAUTH_GENERIC_ISSUSER, NULL))
617 1.86.8.2 dyoung priv++;
618 1.86.8.2 dyoung
619 1.86.8.2 dyoung if (req == PRU_CONTROL)
620 1.86.8.2 dyoung return in6_control(so, (u_long)m, (void *)nam,
621 1.86.8.2 dyoung (struct ifnet *)control, l);
622 1.86.8.2 dyoung
623 1.86.8.2 dyoung if (req == PRU_PURGEIF) {
624 1.86.8.2 dyoung in6_pcbpurgeif0(&raw6cbtable, (struct ifnet *)control);
625 1.86.8.2 dyoung in6_purgeif((struct ifnet *)control);
626 1.86.8.2 dyoung in6_pcbpurgeif(&raw6cbtable, (struct ifnet *)control);
627 1.86.8.2 dyoung return 0;
628 1.86.8.2 dyoung }
629 1.86.8.2 dyoung
630 1.86.8.2 dyoung switch (req) {
631 1.86.8.2 dyoung case PRU_ATTACH:
632 1.86.8.2 dyoung if (in6p != NULL)
633 1.86.8.2 dyoung panic("rip6_attach");
634 1.86.8.2 dyoung if (!priv) {
635 1.86.8.2 dyoung error = EACCES;
636 1.86.8.2 dyoung break;
637 1.86.8.2 dyoung }
638 1.86.8.2 dyoung s = splsoftnet();
639 1.86.8.2 dyoung error = soreserve(so, rip6_sendspace, rip6_recvspace);
640 1.86.8.2 dyoung if (error != 0) {
641 1.86.8.2 dyoung splx(s);
642 1.86.8.2 dyoung break;
643 1.86.8.2 dyoung }
644 1.86.8.2 dyoung if ((error = in6_pcballoc(so, &raw6cbtable)) != 0) {
645 1.86.8.2 dyoung splx(s);
646 1.86.8.2 dyoung break;
647 1.86.8.2 dyoung }
648 1.86.8.2 dyoung splx(s);
649 1.86.8.2 dyoung in6p = sotoin6pcb(so);
650 1.86.8.2 dyoung in6p->in6p_ip6.ip6_nxt = (long)nam;
651 1.86.8.2 dyoung in6p->in6p_cksum = -1;
652 1.86.8.2 dyoung
653 1.86.8.2 dyoung MALLOC(in6p->in6p_icmp6filt, struct icmp6_filter *,
654 1.86.8.2 dyoung sizeof(struct icmp6_filter), M_PCB, M_NOWAIT);
655 1.86.8.2 dyoung if (in6p->in6p_icmp6filt == NULL) {
656 1.86.8.2 dyoung in6_pcbdetach(in6p);
657 1.86.8.2 dyoung error = ENOMEM;
658 1.86.8.2 dyoung break;
659 1.86.8.2 dyoung }
660 1.86.8.2 dyoung ICMP6_FILTER_SETPASSALL(in6p->in6p_icmp6filt);
661 1.86.8.2 dyoung break;
662 1.86.8.2 dyoung
663 1.86.8.2 dyoung case PRU_DISCONNECT:
664 1.86.8.2 dyoung if ((so->so_state & SS_ISCONNECTED) == 0) {
665 1.86.8.2 dyoung error = ENOTCONN;
666 1.86.8.2 dyoung break;
667 1.86.8.2 dyoung }
668 1.86.8.2 dyoung in6p->in6p_faddr = in6addr_any;
669 1.86.8.2 dyoung so->so_state &= ~SS_ISCONNECTED; /* XXX */
670 1.86.8.2 dyoung break;
671 1.86.8.2 dyoung
672 1.86.8.2 dyoung case PRU_ABORT:
673 1.86.8.2 dyoung soisdisconnected(so);
674 1.86.8.2 dyoung /* Fallthrough */
675 1.86.8.2 dyoung case PRU_DETACH:
676 1.86.8.2 dyoung if (in6p == NULL)
677 1.86.8.2 dyoung panic("rip6_detach");
678 1.86.8.2 dyoung if (so == ip6_mrouter)
679 1.86.8.2 dyoung ip6_mrouter_done();
680 1.86.8.2 dyoung /* xxx: RSVP */
681 1.86.8.2 dyoung if (in6p->in6p_icmp6filt != NULL) {
682 1.86.8.2 dyoung FREE(in6p->in6p_icmp6filt, M_PCB);
683 1.86.8.2 dyoung in6p->in6p_icmp6filt = NULL;
684 1.86.8.2 dyoung }
685 1.86.8.2 dyoung in6_pcbdetach(in6p);
686 1.86.8.2 dyoung break;
687 1.86.8.2 dyoung
688 1.86.8.2 dyoung case PRU_BIND:
689 1.86.8.2 dyoung {
690 1.86.8.2 dyoung struct sockaddr_in6 *addr = mtod(nam, struct sockaddr_in6 *);
691 1.86.8.2 dyoung struct ifaddr *ia = NULL;
692 1.86.8.2 dyoung
693 1.86.8.2 dyoung if (nam->m_len != sizeof(*addr)) {
694 1.86.8.2 dyoung error = EINVAL;
695 1.86.8.2 dyoung break;
696 1.86.8.2 dyoung }
697 1.86.8.2 dyoung if (TAILQ_EMPTY(&ifnet) || addr->sin6_family != AF_INET6) {
698 1.86.8.2 dyoung error = EADDRNOTAVAIL;
699 1.86.8.2 dyoung break;
700 1.86.8.2 dyoung }
701 1.86.8.2 dyoung if ((error = sa6_embedscope(addr, ip6_use_defzone)) != 0)
702 1.86.8.2 dyoung break;
703 1.86.8.2 dyoung
704 1.86.8.2 dyoung /*
705 1.86.8.2 dyoung * we don't support mapped address here, it would confuse
706 1.86.8.2 dyoung * users so reject it
707 1.86.8.2 dyoung */
708 1.86.8.2 dyoung if (IN6_IS_ADDR_V4MAPPED(&addr->sin6_addr)) {
709 1.86.8.2 dyoung error = EADDRNOTAVAIL;
710 1.86.8.2 dyoung break;
711 1.86.8.2 dyoung }
712 1.86.8.2 dyoung if (!IN6_IS_ADDR_UNSPECIFIED(&addr->sin6_addr) &&
713 1.86.8.2 dyoung (ia = ifa_ifwithaddr((struct sockaddr *)addr)) == 0) {
714 1.86.8.2 dyoung error = EADDRNOTAVAIL;
715 1.86.8.2 dyoung break;
716 1.86.8.2 dyoung }
717 1.86.8.2 dyoung if (ia && ((struct in6_ifaddr *)ia)->ia6_flags &
718 1.86.8.2 dyoung (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY|
719 1.86.8.2 dyoung IN6_IFF_DETACHED|IN6_IFF_DEPRECATED)) {
720 1.86.8.2 dyoung error = EADDRNOTAVAIL;
721 1.86.8.2 dyoung break;
722 1.86.8.2 dyoung }
723 1.86.8.2 dyoung in6p->in6p_laddr = addr->sin6_addr;
724 1.86.8.2 dyoung break;
725 1.86.8.2 dyoung }
726 1.86.8.2 dyoung
727 1.86.8.2 dyoung case PRU_CONNECT:
728 1.86.8.2 dyoung {
729 1.86.8.2 dyoung struct sockaddr_in6 *addr = mtod(nam, struct sockaddr_in6 *);
730 1.86.8.2 dyoung struct in6_addr *in6a = NULL;
731 1.86.8.2 dyoung struct ifnet *ifp = NULL;
732 1.86.8.2 dyoung int scope_ambiguous = 0;
733 1.86.8.2 dyoung
734 1.86.8.2 dyoung if (nam->m_len != sizeof(*addr)) {
735 1.86.8.2 dyoung error = EINVAL;
736 1.86.8.2 dyoung break;
737 1.86.8.2 dyoung }
738 1.86.8.2 dyoung if (TAILQ_EMPTY(&ifnet)) {
739 1.86.8.2 dyoung error = EADDRNOTAVAIL;
740 1.86.8.2 dyoung break;
741 1.86.8.2 dyoung }
742 1.86.8.2 dyoung if (addr->sin6_family != AF_INET6) {
743 1.86.8.2 dyoung error = EAFNOSUPPORT;
744 1.86.8.2 dyoung break;
745 1.86.8.2 dyoung }
746 1.86.8.2 dyoung
747 1.86.8.2 dyoung /*
748 1.86.8.2 dyoung * Application should provide a proper zone ID or the use of
749 1.86.8.2 dyoung * default zone IDs should be enabled. Unfortunately, some
750 1.86.8.2 dyoung * applications do not behave as it should, so we need a
751 1.86.8.2 dyoung * workaround. Even if an appropriate ID is not determined,
752 1.86.8.2 dyoung * we'll see if we can determine the outgoing interface. If we
753 1.86.8.2 dyoung * can, determine the zone ID based on the interface below.
754 1.86.8.2 dyoung */
755 1.86.8.2 dyoung if (addr->sin6_scope_id == 0 && !ip6_use_defzone)
756 1.86.8.2 dyoung scope_ambiguous = 1;
757 1.86.8.2 dyoung if ((error = sa6_embedscope(addr, ip6_use_defzone)) != 0)
758 1.86.8.2 dyoung return error;
759 1.86.8.2 dyoung
760 1.86.8.2 dyoung /* Source address selection. XXX: need pcblookup? */
761 1.86.8.2 dyoung in6a = in6_selectsrc(addr, in6p->in6p_outputopts,
762 1.86.8.2 dyoung in6p->in6p_moptions, (struct route *)&in6p->in6p_route,
763 1.86.8.2 dyoung &in6p->in6p_laddr, &ifp, &error);
764 1.86.8.2 dyoung if (in6a == NULL) {
765 1.86.8.2 dyoung if (error == 0)
766 1.86.8.2 dyoung error = EADDRNOTAVAIL;
767 1.86.8.2 dyoung break;
768 1.86.8.2 dyoung }
769 1.86.8.2 dyoung /* XXX: see above */
770 1.86.8.2 dyoung if (ifp && scope_ambiguous &&
771 1.86.8.2 dyoung (error = in6_setscope(&addr->sin6_addr, ifp, NULL)) != 0) {
772 1.86.8.2 dyoung break;
773 1.86.8.2 dyoung }
774 1.86.8.2 dyoung in6p->in6p_laddr = *in6a;
775 1.86.8.2 dyoung in6p->in6p_faddr = addr->sin6_addr;
776 1.86.8.2 dyoung soisconnected(so);
777 1.86.8.2 dyoung break;
778 1.86.8.2 dyoung }
779 1.86.8.2 dyoung
780 1.86.8.2 dyoung case PRU_CONNECT2:
781 1.86.8.2 dyoung error = EOPNOTSUPP;
782 1.86.8.2 dyoung break;
783 1.86.8.2 dyoung
784 1.86.8.2 dyoung /*
785 1.86.8.2 dyoung * Mark the connection as being incapable of futther input.
786 1.86.8.2 dyoung */
787 1.86.8.2 dyoung case PRU_SHUTDOWN:
788 1.86.8.2 dyoung socantsendmore(so);
789 1.86.8.2 dyoung break;
790 1.86.8.2 dyoung /*
791 1.86.8.2 dyoung * Ship a packet out. The appropriate raw output
792 1.86.8.2 dyoung * routine handles any messaging necessary.
793 1.86.8.2 dyoung */
794 1.86.8.2 dyoung case PRU_SEND:
795 1.86.8.2 dyoung {
796 1.86.8.2 dyoung struct sockaddr_in6 tmp;
797 1.86.8.2 dyoung struct sockaddr_in6 *dst;
798 1.86.8.2 dyoung
799 1.86.8.2 dyoung /* always copy sockaddr to avoid overwrites */
800 1.86.8.2 dyoung if (so->so_state & SS_ISCONNECTED) {
801 1.86.8.2 dyoung if (nam) {
802 1.86.8.2 dyoung error = EISCONN;
803 1.86.8.2 dyoung break;
804 1.86.8.2 dyoung }
805 1.86.8.2 dyoung /* XXX */
806 1.86.8.2 dyoung bzero(&tmp, sizeof(tmp));
807 1.86.8.2 dyoung tmp.sin6_family = AF_INET6;
808 1.86.8.2 dyoung tmp.sin6_len = sizeof(struct sockaddr_in6);
809 1.86.8.2 dyoung bcopy(&in6p->in6p_faddr, &tmp.sin6_addr,
810 1.86.8.2 dyoung sizeof(struct in6_addr));
811 1.86.8.2 dyoung dst = &tmp;
812 1.86.8.2 dyoung } else {
813 1.86.8.2 dyoung if (nam == NULL) {
814 1.86.8.2 dyoung error = ENOTCONN;
815 1.86.8.2 dyoung break;
816 1.86.8.2 dyoung }
817 1.86.8.2 dyoung if (nam->m_len != sizeof(tmp)) {
818 1.86.8.2 dyoung error = EINVAL;
819 1.86.8.2 dyoung break;
820 1.86.8.2 dyoung }
821 1.86.8.2 dyoung
822 1.86.8.2 dyoung tmp = *mtod(nam, struct sockaddr_in6 *);
823 1.86.8.2 dyoung dst = &tmp;
824 1.86.8.2 dyoung
825 1.86.8.2 dyoung if (dst->sin6_family != AF_INET6) {
826 1.86.8.2 dyoung error = EAFNOSUPPORT;
827 1.86.8.2 dyoung break;
828 1.86.8.2 dyoung }
829 1.86.8.2 dyoung }
830 1.86.8.2 dyoung error = rip6_output(m, so, dst, control);
831 1.86.8.2 dyoung m = NULL;
832 1.86.8.2 dyoung break;
833 1.86.8.2 dyoung }
834 1.86.8.2 dyoung
835 1.86.8.2 dyoung case PRU_SENSE:
836 1.86.8.2 dyoung /*
837 1.86.8.2 dyoung * stat: don't bother with a blocksize
838 1.86.8.2 dyoung */
839 1.86.8.2 dyoung return 0;
840 1.86.8.2 dyoung /*
841 1.86.8.2 dyoung * Not supported.
842 1.86.8.2 dyoung */
843 1.86.8.2 dyoung case PRU_RCVOOB:
844 1.86.8.2 dyoung case PRU_RCVD:
845 1.86.8.2 dyoung case PRU_LISTEN:
846 1.86.8.2 dyoung case PRU_ACCEPT:
847 1.86.8.2 dyoung case PRU_SENDOOB:
848 1.86.8.2 dyoung error = EOPNOTSUPP;
849 1.86.8.2 dyoung break;
850 1.86.8.2 dyoung
851 1.86.8.2 dyoung case PRU_SOCKADDR:
852 1.86.8.2 dyoung in6_setsockaddr(in6p, nam);
853 1.86.8.2 dyoung break;
854 1.86.8.2 dyoung
855 1.86.8.2 dyoung case PRU_PEERADDR:
856 1.86.8.2 dyoung in6_setpeeraddr(in6p, nam);
857 1.86.8.2 dyoung break;
858 1.86.8.2 dyoung
859 1.86.8.2 dyoung default:
860 1.86.8.2 dyoung panic("rip6_usrreq");
861 1.86.8.2 dyoung }
862 1.86.8.2 dyoung if (m != NULL)
863 1.86.8.2 dyoung m_freem(m);
864 1.86.8.2 dyoung return error;
865 1.86.8.2 dyoung }
866 1.86.8.2 dyoung
867 1.86.8.2 dyoung SYSCTL_SETUP(sysctl_net_inet6_raw6_setup, "sysctl net.inet6.raw6 subtree setup")
868 1.86.8.2 dyoung {
869 1.86.8.2 dyoung
870 1.86.8.2 dyoung sysctl_createv(clog, 0, NULL, NULL,
871 1.86.8.2 dyoung CTLFLAG_PERMANENT,
872 1.86.8.2 dyoung CTLTYPE_NODE, "net", NULL,
873 1.86.8.2 dyoung NULL, 0, NULL, 0,
874 1.86.8.2 dyoung CTL_NET, CTL_EOL);
875 1.86.8.2 dyoung sysctl_createv(clog, 0, NULL, NULL,
876 1.86.8.2 dyoung CTLFLAG_PERMANENT,
877 1.86.8.2 dyoung CTLTYPE_NODE, "inet6", NULL,
878 1.86.8.2 dyoung NULL, 0, NULL, 0,
879 1.86.8.2 dyoung CTL_NET, PF_INET6, CTL_EOL);
880 1.86.8.2 dyoung sysctl_createv(clog, 0, NULL, NULL,
881 1.86.8.2 dyoung CTLFLAG_PERMANENT,
882 1.86.8.2 dyoung CTLTYPE_NODE, "raw6",
883 1.86.8.2 dyoung SYSCTL_DESCR("Raw IPv6 settings"),
884 1.86.8.2 dyoung NULL, 0, NULL, 0,
885 1.86.8.2 dyoung CTL_NET, PF_INET6, IPPROTO_RAW, CTL_EOL);
886 1.86.8.2 dyoung
887 1.86.8.2 dyoung sysctl_createv(clog, 0, NULL, NULL,
888 1.86.8.2 dyoung CTLFLAG_PERMANENT,
889 1.86.8.2 dyoung CTLTYPE_STRUCT, "pcblist",
890 1.86.8.2 dyoung SYSCTL_DESCR("Raw IPv6 control block list"),
891 1.86.8.2 dyoung sysctl_inpcblist, 0, &raw6cbtable, 0,
892 1.86.8.2 dyoung CTL_NET, PF_INET6, IPPROTO_RAW,
893 1.86.8.2 dyoung CTL_CREATE, CTL_EOL);
894 1.86.8.2 dyoung sysctl_createv(clog, 0, NULL, NULL,
895 1.86.8.2 dyoung CTLFLAG_PERMANENT,
896 1.86.8.2 dyoung CTLTYPE_STRUCT, "stats",
897 1.86.8.2 dyoung SYSCTL_DESCR("Raw IPv6 statistics"),
898 1.86.8.2 dyoung NULL, 0, &rip6stat, sizeof(rip6stat),
899 1.86.8.2 dyoung CTL_NET, PF_INET6, IPPROTO_RAW, RAW6CTL_STATS,
900 1.86.8.2 dyoung CTL_EOL);
901 1.86.8.2 dyoung }
902