]> git.saurik.com Git - apple/xnu.git/blob - bsd/netinet6/raw_ip6.c
xnu-2422.1.72.tar.gz
[apple/xnu.git] / bsd / netinet6 / raw_ip6.c
1 /*
2 * Copyright (c) 2000-2013 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28 /*
29 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
30 * All rights reserved.
31 *
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
34 * are met:
35 * 1. Redistributions of source code must retain the above copyright
36 * notice, this list of conditions and the following disclaimer.
37 * 2. Redistributions in binary form must reproduce the above copyright
38 * notice, this list of conditions and the following disclaimer in the
39 * documentation and/or other materials provided with the distribution.
40 * 3. Neither the name of the project nor the names of its contributors
41 * may be used to endorse or promote products derived from this software
42 * without specific prior written permission.
43 *
44 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
45 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
46 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
47 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
48 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
49 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
50 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
51 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
52 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
53 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
54 * SUCH DAMAGE.
55 *
56 * $FreeBSD: src/sys/netinet6/raw_ip6.c,v 1.7.2.4 2001/07/29 19:32:40 ume Exp $
57 */
58
59 /*
60 * Copyright (c) 1982, 1986, 1988, 1993
61 * The Regents of the University of California. All rights reserved.
62 *
63 * Redistribution and use in source and binary forms, with or without
64 * modification, are permitted provided that the following conditions
65 * are met:
66 * 1. Redistributions of source code must retain the above copyright
67 * notice, this list of conditions and the following disclaimer.
68 * 2. Redistributions in binary form must reproduce the above copyright
69 * notice, this list of conditions and the following disclaimer in the
70 * documentation and/or other materials provided with the distribution.
71 * 3. All advertising materials mentioning features or use of this software
72 * must display the following acknowledgement:
73 * This product includes software developed by the University of
74 * California, Berkeley and its contributors.
75 * 4. Neither the name of the University nor the names of its contributors
76 * may be used to endorse or promote products derived from this software
77 * without specific prior written permission.
78 *
79 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
80 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
81 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
82 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
83 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
84 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
85 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
86 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
87 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
88 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
89 * SUCH DAMAGE.
90 *
91 * @(#)raw_ip.c 8.2 (Berkeley) 1/4/94
92 */
93 #include <sys/param.h>
94 #include <sys/malloc.h>
95 #include <sys/proc.h>
96 #include <sys/mcache.h>
97 #include <sys/mbuf.h>
98 #include <sys/socket.h>
99 #include <sys/protosw.h>
100 #include <sys/socketvar.h>
101 #include <sys/errno.h>
102 #include <sys/systm.h>
103
104 #include <net/if.h>
105 #include <net/route.h>
106 #include <net/if_types.h>
107
108 #include <netinet/in.h>
109 #include <netinet/in_var.h>
110 #include <netinet/in_systm.h>
111 #include <netinet/ip6.h>
112 #include <netinet6/ip6_var.h>
113 #include <netinet6/ip6_mroute.h>
114 #include <netinet/icmp6.h>
115 #include <netinet/in_pcb.h>
116 #include <netinet6/in6_pcb.h>
117 #include <netinet6/nd6.h>
118 #include <netinet6/ip6protosw.h>
119 #include <netinet6/scope6_var.h>
120 #include <netinet6/raw_ip6.h>
121 #include <netinet6/ip6_fw.h>
122
123 #if IPSEC
124 #include <netinet6/ipsec.h>
125 #include <netinet6/ipsec6.h>
126 extern int ipsec_bypass;
127 #endif /*IPSEC*/
128
129 /*
130 * Raw interface to IP6 protocol.
131 */
132
133 extern struct inpcbhead ripcb;
134 extern struct inpcbinfo ripcbinfo;
135 extern u_int32_t rip_sendspace;
136 extern u_int32_t rip_recvspace;
137
138 struct rip6stat rip6stat;
139
140 /*
141 * Setup generic address and protocol structures
142 * for raw_input routine, then pass them along with
143 * mbuf chain.
144 */
145 int
146 rip6_input(
147 struct mbuf **mp,
148 int *offp,
149 int proto)
150 {
151 struct mbuf *m = *mp;
152 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
153 struct inpcb *in6p;
154 struct inpcb *last = 0;
155 struct mbuf *opts = NULL;
156 struct sockaddr_in6 rip6src;
157 int ret;
158 struct ifnet *ifp = m->m_pkthdr.rcvif;
159
160 /* Expect 32-bit aligned data pointer on strict-align platforms */
161 MBUF_STRICT_DATA_ALIGNMENT_CHECK_32(m);
162
163 rip6stat.rip6s_ipackets++;
164
165 init_sin6(&rip6src, m); /* general init */
166
167 lck_rw_lock_shared(ripcbinfo.ipi_lock);
168 LIST_FOREACH(in6p, &ripcb, inp_list) {
169 if ((in6p->in6p_vflag & INP_IPV6) == 0)
170 continue;
171 if (in6p->in6p_ip6_nxt &&
172 in6p->in6p_ip6_nxt != proto)
173 continue;
174 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) &&
175 !IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, &ip6->ip6_dst))
176 continue;
177 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr) &&
178 !IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, &ip6->ip6_src))
179 continue;
180
181 if (inp_restricted(in6p, ifp))
182 continue;
183
184 if (ifp != NULL && IFNET_IS_CELLULAR(ifp) &&
185 (in6p->in6p_flags & INP_NO_IFT_CELLULAR))
186 continue;
187
188 if (proto == IPPROTO_ICMPV6 || in6p->in6p_cksum != -1) {
189 rip6stat.rip6s_isum++;
190 if (in6_cksum(m, ip6->ip6_nxt, *offp,
191 m->m_pkthdr.len - *offp)) {
192 rip6stat.rip6s_badsum++;
193 continue;
194 }
195 }
196 if (last) {
197 struct mbuf *n = m_copy(m, 0, (int)M_COPYALL);
198
199 #if IPSEC
200 /*
201 * Check AH/ESP integrity.
202 */
203 if (ipsec_bypass == 0 && n && ipsec6_in_reject_so(n, last->inp_socket)) {
204 m_freem(n);
205 IPSEC_STAT_INCREMENT(ipsec6stat.in_polvio);
206 /* do not inject data into pcb */
207 } else
208 #endif /*IPSEC*/
209 if (n) {
210 if ((last->in6p_flags & INP_CONTROLOPTS) != 0 ||
211 (last->in6p_socket->so_options & SO_TIMESTAMP) != 0 ||
212 (last->in6p_socket->so_options & SO_TIMESTAMP_MONOTONIC) != 0) {
213 ret = ip6_savecontrol(last, n, &opts);
214 if (ret != 0) {
215 m_freem(n);
216 m_freem(opts);
217 last = in6p;
218 continue;
219 }
220 }
221 /* strip intermediate headers */
222 m_adj(n, *offp);
223 so_recv_data_stat(last->in6p_socket, m, 0);
224 if (sbappendaddr(&last->in6p_socket->so_rcv,
225 (struct sockaddr *)&rip6src,
226 n, opts, NULL) == 0) {
227 rip6stat.rip6s_fullsock++;
228 } else
229 sorwakeup(last->in6p_socket);
230 opts = NULL;
231 }
232 }
233 last = in6p;
234 }
235
236 #if IPSEC
237 /*
238 * Check AH/ESP integrity.
239 */
240 if (ipsec_bypass == 0 && last && ipsec6_in_reject_so(m, last->inp_socket)) {
241 m_freem(m);
242 IPSEC_STAT_INCREMENT(ipsec6stat.in_polvio);
243 ip6stat.ip6s_delivered--;
244 /* do not inject data into pcb */
245 } else
246 #endif /*IPSEC*/
247 if (last) {
248 if ((last->in6p_flags & INP_CONTROLOPTS) != 0 ||
249 (last->in6p_socket->so_options & SO_TIMESTAMP) != 0 ||
250 (last->in6p_socket->so_options & SO_TIMESTAMP_MONOTONIC) != 0) {
251 ret = ip6_savecontrol(last, m, &opts);
252 if (ret != 0) {
253 m_freem(m);
254 m_freem(opts);
255 ip6stat.ip6s_delivered--;
256 goto unlock;
257 }
258
259 }
260 /* strip intermediate headers */
261 m_adj(m, *offp);
262 so_recv_data_stat(last->in6p_socket, m, 0);
263 if (sbappendaddr(&last->in6p_socket->so_rcv,
264 (struct sockaddr *)&rip6src, m, opts, NULL) == 0) {
265 rip6stat.rip6s_fullsock++;
266 } else
267 sorwakeup(last->in6p_socket);
268 } else {
269 rip6stat.rip6s_nosock++;
270 if (m->m_flags & M_MCAST)
271 rip6stat.rip6s_nosockmcast++;
272 if (proto == IPPROTO_NONE)
273 m_freem(m);
274 else {
275 char *prvnxtp = ip6_get_prevhdr(m, *offp); /* XXX */
276 icmp6_error(m, ICMP6_PARAM_PROB,
277 ICMP6_PARAMPROB_NEXTHEADER,
278 prvnxtp - mtod(m, char *));
279 }
280 ip6stat.ip6s_delivered--;
281 }
282
283 unlock:
284 lck_rw_done(ripcbinfo.ipi_lock);
285
286 return IPPROTO_DONE;
287 }
288
289 void
290 rip6_ctlinput(
291 int cmd,
292 struct sockaddr *sa,
293 void *d)
294 {
295 struct ip6_hdr *ip6;
296 struct mbuf *m;
297 void *cmdarg = NULL;
298 int off = 0;
299 struct ip6ctlparam *ip6cp = NULL;
300 const struct sockaddr_in6 *sa6_src = NULL;
301 void (*notify)(struct inpcb *, int) = in6_rtchange;
302
303 if (sa->sa_family != AF_INET6 ||
304 sa->sa_len != sizeof(struct sockaddr_in6))
305 return;
306
307 if ((unsigned)cmd >= PRC_NCMDS)
308 return;
309 if (PRC_IS_REDIRECT(cmd))
310 notify = in6_rtchange, d = NULL;
311 else if (cmd == PRC_HOSTDEAD)
312 d = NULL;
313 else if (inet6ctlerrmap[cmd] == 0)
314 return;
315
316 /* if the parameter is from icmp6, decode it. */
317 if (d != NULL) {
318 ip6cp = (struct ip6ctlparam *)d;
319 m = ip6cp->ip6c_m;
320 ip6 = ip6cp->ip6c_ip6;
321 off = ip6cp->ip6c_off;
322 cmdarg = ip6cp->ip6c_cmdarg;
323 sa6_src = ip6cp->ip6c_src;
324 } else {
325 m = NULL;
326 ip6 = NULL;
327 sa6_src = &sa6_any;
328 }
329
330 (void) in6_pcbnotify(&ripcbinfo, sa, 0, (const struct sockaddr *)sa6_src,
331 0, cmd, cmdarg, notify);
332 }
333
334 /*
335 * Generate IPv6 header and pass packet to ip6_output.
336 * Tack on options user may have setup with control call.
337 */
338 int
339 rip6_output(
340 struct mbuf *m,
341 struct socket *so,
342 struct sockaddr_in6 *dstsock,
343 struct mbuf *control,
344 int israw)
345 {
346 struct in6_addr *dst;
347 struct ip6_hdr *ip6;
348 struct inpcb *in6p;
349 u_int plen = m->m_pkthdr.len;
350 int error = 0;
351 struct ip6_pktopts opt, *optp = NULL;
352 struct ip6_moptions *im6o = NULL;
353 struct ifnet *oifp = NULL;
354 int type = 0, code = 0; /* for ICMPv6 output statistics only */
355 mbuf_svc_class_t msc = MBUF_SC_UNSPEC;
356 struct ip6_out_args ip6oa =
357 { IFSCOPE_NONE, { 0 }, IP6OAF_SELECT_SRCIF, 0 };
358 int flags = IPV6_OUTARGS;
359
360 in6p = sotoin6pcb(so);
361
362 if (in6p == NULL || (in6p->inp_flags2 & INP2_WANT_FLOW_DIVERT)) {
363 error = (in6p == NULL ? EINVAL : EPROTOTYPE);
364 goto bad;
365 }
366 if (dstsock != NULL && IN6_IS_ADDR_V4MAPPED(&dstsock->sin6_addr)) {
367 error = EINVAL;
368 goto bad;
369 }
370
371 if (in6p->inp_flags & INP_BOUND_IF) {
372 ip6oa.ip6oa_boundif = in6p->inp_boundifp->if_index;
373 ip6oa.ip6oa_flags |= IP6OAF_BOUND_IF;
374 }
375 if (in6p->inp_flags & INP_NO_IFT_CELLULAR)
376 ip6oa.ip6oa_flags |= IP6OAF_NO_CELLULAR;
377
378 dst = &dstsock->sin6_addr;
379 if (control) {
380 msc = mbuf_service_class_from_control(control);
381
382 if ((error = ip6_setpktopts(control, &opt, NULL,
383 SOCK_PROTO(so))) != 0)
384 goto bad;
385 optp = &opt;
386 } else
387 optp = in6p->in6p_outputopts;
388
389 /*
390 * For an ICMPv6 packet, we should know its type and code
391 * to update statistics.
392 */
393 if (SOCK_PROTO(so) == IPPROTO_ICMPV6) {
394 struct icmp6_hdr *icmp6;
395 if (m->m_len < sizeof(struct icmp6_hdr) &&
396 (m = m_pullup(m, sizeof(struct icmp6_hdr))) == NULL) {
397 error = ENOBUFS;
398 goto bad;
399 }
400 icmp6 = mtod(m, struct icmp6_hdr *);
401 type = icmp6->icmp6_type;
402 code = icmp6->icmp6_code;
403 }
404
405 if (in6p->inp_flowhash == 0)
406 in6p->inp_flowhash = inp_calc_flowhash(in6p);
407 /* update flowinfo - RFC 6437 */
408 if (in6p->inp_flow == 0 && in6p->in6p_flags & IN6P_AUTOFLOWLABEL) {
409 in6p->inp_flow &= ~IPV6_FLOWLABEL_MASK;
410 in6p->inp_flow |=
411 (htonl(in6p->inp_flowhash) & IPV6_FLOWLABEL_MASK);
412 }
413
414 M_PREPEND(m, sizeof(*ip6), M_WAIT);
415 if (m == NULL) {
416 error = ENOBUFS;
417 goto bad;
418 }
419 ip6 = mtod(m, struct ip6_hdr *);
420
421 /*
422 * Next header might not be ICMP6 but use its pseudo header anyway.
423 */
424 ip6->ip6_dst = *dst;
425
426 im6o = in6p->in6p_moptions;
427
428 /*
429 * If the scope of the destination is link-local, embed the interface
430 * index in the address.
431 *
432 * XXX advanced-api value overrides sin6_scope_id
433 */
434 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst)) {
435 struct in6_pktinfo *pi;
436 struct ifnet *im6o_multicast_ifp = NULL;
437
438 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) && im6o != NULL) {
439 IM6O_LOCK(im6o);
440 im6o_multicast_ifp = im6o->im6o_multicast_ifp;
441 IM6O_UNLOCK(im6o);
442 }
443 /*
444 * XXX Boundary check is assumed to be already done in
445 * ip6_setpktoptions().
446 */
447 ifnet_head_lock_shared();
448 if (optp && (pi = optp->ip6po_pktinfo) && pi->ipi6_ifindex) {
449 ip6->ip6_dst.s6_addr16[1] = htons(pi->ipi6_ifindex);
450 oifp = ifindex2ifnet[pi->ipi6_ifindex];
451 if (oifp != NULL)
452 ifnet_reference(oifp);
453 } else if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) &&
454 im6o != NULL && im6o_multicast_ifp != NULL) {
455 oifp = im6o_multicast_ifp;
456 ifnet_reference(oifp);
457 ip6->ip6_dst.s6_addr16[1] = htons(oifp->if_index);
458 } else if (dstsock->sin6_scope_id) {
459 /*
460 * boundary check
461 *
462 * Sinced stsock->sin6_scope_id is unsigned, we don't
463 * need to check if it's < 0
464 */
465 if (if_index < dstsock->sin6_scope_id) {
466 error = ENXIO; /* XXX EINVAL? */
467 ifnet_head_done();
468 goto bad;
469 }
470 ip6->ip6_dst.s6_addr16[1]
471 = htons(dstsock->sin6_scope_id & 0xffff);/*XXX*/
472 }
473 ifnet_head_done();
474 }
475
476 /*
477 * Source address selection.
478 */
479 {
480 struct in6_addr *in6a;
481 struct in6_addr storage;
482 u_short index = 0;
483
484 if (israw != 0 && optp && optp->ip6po_pktinfo && !IN6_IS_ADDR_UNSPECIFIED(&optp->ip6po_pktinfo->ipi6_addr)) {
485 in6a = &optp->ip6po_pktinfo->ipi6_addr;
486 flags |= IPV6_FLAG_NOSRCIFSEL;
487 } else if ((in6a = in6_selectsrc(dstsock, optp, in6p,
488 &in6p->in6p_route, NULL, &storage, ip6oa.ip6oa_boundif,
489 &error)) == 0) {
490 if (error == 0)
491 error = EADDRNOTAVAIL;
492 goto bad;
493 } else {
494 ip6oa.ip6oa_flags |= IP6OAF_BOUND_SRCADDR;
495 }
496 ip6->ip6_src = *in6a;
497 if (in6p->in6p_route.ro_rt != NULL) {
498 RT_LOCK(in6p->in6p_route.ro_rt);
499 if (in6p->in6p_route.ro_rt->rt_ifp != NULL)
500 index = in6p->in6p_route.ro_rt->rt_ifp->if_index;
501 RT_UNLOCK(in6p->in6p_route.ro_rt);
502 if (oifp != NULL)
503 ifnet_release(oifp);
504 ifnet_head_lock_shared();
505 if (index == 0 || if_index < index) {
506 panic("bad if_index on interface from route");
507 }
508 oifp = ifindex2ifnet[index];
509 if (oifp != NULL)
510 ifnet_reference(oifp);
511 ifnet_head_done();
512 }
513 }
514 ip6->ip6_flow = (ip6->ip6_flow & ~IPV6_FLOWINFO_MASK) |
515 (in6p->inp_flow & IPV6_FLOWINFO_MASK);
516 ip6->ip6_vfc = (ip6->ip6_vfc & ~IPV6_VERSION_MASK) |
517 (IPV6_VERSION & IPV6_VERSION_MASK);
518 /* ip6_plen will be filled in ip6_output, so not fill it here. */
519 ip6->ip6_nxt = in6p->in6p_ip6_nxt;
520 ip6->ip6_hlim = in6_selecthlim(in6p, oifp);
521
522 if (SOCK_PROTO(so) == IPPROTO_ICMPV6 || in6p->in6p_cksum != -1) {
523 struct mbuf *n;
524 int off;
525 u_int16_t *p;
526
527 /* compute checksum */
528 if (SOCK_PROTO(so) == IPPROTO_ICMPV6)
529 off = offsetof(struct icmp6_hdr, icmp6_cksum);
530 else
531 off = in6p->in6p_cksum;
532 if (plen < (unsigned int)(off + 1)) {
533 error = EINVAL;
534 goto bad;
535 }
536 off += sizeof(struct ip6_hdr);
537
538 n = m;
539 while (n && n->m_len <= off) {
540 off -= n->m_len;
541 n = n->m_next;
542 }
543 if (!n)
544 goto bad;
545 p = (u_int16_t *)(void *)(mtod(n, caddr_t) + off);
546 *p = 0;
547 *p = in6_cksum(m, ip6->ip6_nxt, sizeof(*ip6), plen);
548 }
549
550 #if IPSEC
551 if (ipsec_bypass == 0 && ipsec_setsocket(m, so) != 0) {
552 error = ENOBUFS;
553 goto bad;
554 }
555 #endif /*IPSEC*/
556
557 if (ROUTE_UNUSABLE(&in6p->in6p_route))
558 ROUTE_RELEASE(&in6p->in6p_route);
559
560 if (oifp != NULL) {
561 ifnet_release(oifp);
562 oifp = NULL;
563 }
564
565 set_packet_service_class(m, so, msc, PKT_SCF_IPV6);
566 m->m_pkthdr.pkt_flowsrc = FLOWSRC_INPCB;
567 m->m_pkthdr.pkt_flowid = in6p->inp_flowhash;
568 m->m_pkthdr.pkt_flags |= (PKTF_FLOW_ID | PKTF_FLOW_LOCALSRC |
569 PKTF_FLOW_RAWSOCK);
570 m->m_pkthdr.pkt_proto = in6p->in6p_ip6_nxt;
571
572 if (im6o != NULL)
573 IM6O_ADDREF(im6o);
574
575 error = ip6_output(m, optp, &in6p->in6p_route, flags, im6o,
576 &oifp, &ip6oa);
577
578 if (im6o != NULL)
579 IM6O_REMREF(im6o);
580
581 if (in6p->in6p_route.ro_rt != NULL) {
582 struct rtentry *rt = in6p->in6p_route.ro_rt;
583 struct ifnet *outif;
584
585 if ((rt->rt_flags & RTF_MULTICAST) ||
586 in6p->in6p_socket == NULL ||
587 !(in6p->in6p_socket->so_state & SS_ISCONNECTED)) {
588 rt = NULL; /* unusable */
589 }
590 /*
591 * Always discard the cached route for unconnected
592 * socket or if it is a multicast route.
593 */
594 if (rt == NULL)
595 ROUTE_RELEASE(&in6p->in6p_route);
596
597 /*
598 * If this is a connected socket and the destination
599 * route is not multicast, update outif with that of
600 * the route interface index used by IP.
601 */
602 if (rt != NULL &&
603 (outif = rt->rt_ifp) != in6p->in6p_last_outifp)
604 in6p->in6p_last_outifp = outif;
605 } else {
606 ROUTE_RELEASE(&in6p->in6p_route);
607 }
608
609 /*
610 * If output interface was cellular, and this socket is denied
611 * access to it, generate an event.
612 */
613 if (error != 0 && (ip6oa.ip6oa_retflags & IP6OARF_IFDENIED) &&
614 (in6p->inp_flags & INP_NO_IFT_CELLULAR))
615 soevent(in6p->inp_socket, (SO_FILT_HINT_LOCKED|
616 SO_FILT_HINT_IFDENIED));
617
618 if (SOCK_PROTO(so) == IPPROTO_ICMPV6) {
619 if (oifp)
620 icmp6_ifoutstat_inc(oifp, type, code);
621 icmp6stat.icp6s_outhist[type]++;
622 } else
623 rip6stat.rip6s_opackets++;
624
625 goto freectl;
626
627 bad:
628 if (m != NULL)
629 m_freem(m);
630
631 freectl:
632 if (optp == &opt && optp->ip6po_rthdr)
633 ROUTE_RELEASE(&optp->ip6po_route);
634
635 if (control != NULL) {
636 if (optp == &opt)
637 ip6_clearpktopts(optp, -1);
638 m_freem(control);
639 }
640 if (oifp != NULL)
641 ifnet_release(oifp);
642 return(error);
643 }
644
645 #if IPFW2
646 __private_extern__ void
647 load_ip6fw(void)
648 {
649 ip6_fw_init();
650 }
651 #endif
652
653 /*
654 * Raw IPv6 socket option processing.
655 */
656 int
657 rip6_ctloutput(
658 struct socket *so,
659 struct sockopt *sopt)
660 {
661 int error, optval;
662
663 /* Allow <SOL_SOCKET,SO_FLUSH> at this level */
664 if (sopt->sopt_level == IPPROTO_ICMPV6)
665 /*
666 * XXX: is it better to call icmp6_ctloutput() directly
667 * from protosw?
668 */
669 return(icmp6_ctloutput(so, sopt));
670 else if (sopt->sopt_level != IPPROTO_IPV6 &&
671 !(sopt->sopt_level == SOL_SOCKET && sopt->sopt_name == SO_FLUSH))
672 return (EINVAL);
673
674 error = 0;
675
676 switch (sopt->sopt_dir) {
677 case SOPT_GET:
678 switch (sopt->sopt_name) {
679 #if IPFW2
680 case IPV6_FW_ADD:
681 case IPV6_FW_GET:
682 if (ip6_fw_ctl_ptr == 0)
683 load_ip6fw();
684 if (ip6_fw_ctl_ptr)
685 error = ip6_fw_ctl_ptr(sopt);
686 else
687 error = ENOPROTOOPT;
688 break;
689 #endif
690
691 case MRT6_INIT:
692 case MRT6_DONE:
693 case MRT6_ADD_MIF:
694 case MRT6_DEL_MIF:
695 case MRT6_ADD_MFC:
696 case MRT6_DEL_MFC:
697 case MRT6_PIM:
698 #if MROUTING
699 error = ip6_mrouter_get(so, sopt);
700 #else
701 error = ENOPROTOOPT;
702 #endif /* MROUTING */
703 break;
704 case IPV6_CHECKSUM:
705 error = ip6_raw_ctloutput(so, sopt);
706 break;
707 default:
708 error = ip6_ctloutput(so, sopt);
709 break;
710 }
711 break;
712
713 case SOPT_SET:
714 switch (sopt->sopt_name) {
715 #if IPFW2
716 case IPV6_FW_ADD:
717 case IPV6_FW_DEL:
718 case IPV6_FW_FLUSH:
719 case IPV6_FW_ZERO:
720 if (ip6_fw_ctl_ptr == 0)
721 load_ip6fw();
722 if (ip6_fw_ctl_ptr)
723 error = ip6_fw_ctl_ptr(sopt);
724 else
725 error = ENOPROTOOPT;
726 break;
727 #endif
728
729 case MRT6_INIT:
730 case MRT6_DONE:
731 case MRT6_ADD_MIF:
732 case MRT6_DEL_MIF:
733 case MRT6_ADD_MFC:
734 case MRT6_DEL_MFC:
735 case MRT6_PIM:
736 #if MROUTING
737 error = ip6_mrouter_set(so, sopt);
738 #else
739 error = ENOPROTOOPT;
740 #endif
741 break;
742 case IPV6_CHECKSUM:
743 error = ip6_raw_ctloutput(so, sopt);
744 break;
745
746 case SO_FLUSH:
747 if ((error = sooptcopyin(sopt, &optval, sizeof (optval),
748 sizeof (optval))) != 0)
749 break;
750
751 error = inp_flush(sotoinpcb(so), optval);
752 break;
753
754 default:
755 error = ip6_ctloutput(so, sopt);
756 break;
757 }
758 break;
759 }
760
761 return (error);
762 }
763
764 static int
765 rip6_attach(struct socket *so, int proto, struct proc *p)
766 {
767 struct inpcb *inp;
768 int error;
769
770 inp = sotoinpcb(so);
771 if (inp)
772 panic("rip6_attach");
773 if ((error = proc_suser(p)) != 0)
774 return error;
775
776 error = soreserve(so, rip_sendspace, rip_recvspace);
777 if (error)
778 return error;
779 error = in_pcballoc(so, &ripcbinfo, p);
780 if (error)
781 return error;
782 inp = (struct inpcb *)so->so_pcb;
783 inp->inp_vflag |= INP_IPV6;
784 inp->in6p_ip6_nxt = (char)proto;
785 inp->in6p_hops = -1; /* use kernel default */
786 inp->in6p_cksum = -1;
787 MALLOC(inp->in6p_icmp6filt, struct icmp6_filter *,
788 sizeof(struct icmp6_filter), M_PCB, M_WAITOK);
789 if (inp->in6p_icmp6filt == NULL)
790 return (ENOMEM);
791 ICMP6_FILTER_SETPASSALL(inp->in6p_icmp6filt);
792 return 0;
793 }
794
795 static int
796 rip6_detach(struct socket *so)
797 {
798 struct inpcb *inp;
799
800 inp = sotoinpcb(so);
801 if (inp == 0)
802 panic("rip6_detach");
803 /* xxx: RSVP */
804 #if MROUTING
805 if (so == ip6_mrouter)
806 ip6_mrouter_done();
807 #endif
808 if (inp->in6p_icmp6filt) {
809 FREE(inp->in6p_icmp6filt, M_PCB);
810 inp->in6p_icmp6filt = NULL;
811 }
812 in6_pcbdetach(inp);
813 return 0;
814 }
815
816 static int
817 rip6_abort(struct socket *so)
818 {
819 soisdisconnected(so);
820 return rip6_detach(so);
821 }
822
823 static int
824 rip6_disconnect(struct socket *so)
825 {
826 struct inpcb *inp = sotoinpcb(so);
827
828 if ((so->so_state & SS_ISCONNECTED) == 0)
829 return ENOTCONN;
830 inp->in6p_faddr = in6addr_any;
831 return rip6_abort(so);
832 }
833
834 static int
835 rip6_bind(struct socket *so, struct sockaddr *nam, struct proc *p)
836 {
837 #pragma unused(p)
838 struct inpcb *inp = sotoinpcb(so);
839 struct sockaddr_in6 sin6;
840 struct ifaddr *ifa = NULL;
841 struct ifnet *outif = NULL;
842 int error;
843
844 if (inp == NULL || (inp->inp_flags2 & INP2_WANT_FLOW_DIVERT))
845 return (inp == NULL ? EINVAL : EPROTOTYPE);
846
847 if (nam->sa_len != sizeof (struct sockaddr_in6))
848 return (EINVAL);
849
850 if (TAILQ_EMPTY(&ifnet_head) || SIN6(nam)->sin6_family != AF_INET6)
851 return (EADDRNOTAVAIL);
852
853 bzero(&sin6, sizeof (sin6));
854 *(&sin6) = *SIN6(nam);
855
856 if ((error = sa6_embedscope(&sin6, ip6_use_defzone)) != 0)
857 return (error);
858
859 /* Sanitize local copy for address searches */
860 sin6.sin6_flowinfo = 0;
861 sin6.sin6_scope_id = 0;
862 sin6.sin6_port = 0;
863
864 if (!IN6_IS_ADDR_UNSPECIFIED(&sin6.sin6_addr) &&
865 (ifa = ifa_ifwithaddr(SA(&sin6))) == 0)
866 return (EADDRNOTAVAIL);
867 if (ifa != NULL) {
868 IFA_LOCK(ifa);
869 if (((struct in6_ifaddr *)ifa)->ia6_flags &
870 (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY|
871 IN6_IFF_DETACHED|IN6_IFF_DEPRECATED)) {
872 IFA_UNLOCK(ifa);
873 IFA_REMREF(ifa);
874 return (EADDRNOTAVAIL);
875 }
876 outif = ifa->ifa_ifp;
877 IFA_UNLOCK(ifa);
878 IFA_REMREF(ifa);
879 }
880 inp->in6p_laddr = sin6.sin6_addr;
881 inp->in6p_last_outifp = outif;
882 return (0);
883 }
884
885 static int
886 rip6_connect(struct socket *so, struct sockaddr *nam, __unused struct proc *p)
887 {
888 struct inpcb *inp = sotoinpcb(so);
889 struct sockaddr_in6 *addr = (struct sockaddr_in6 *)(void *)nam;
890 struct in6_addr *in6a = NULL;
891 struct in6_addr storage;
892 int error = 0;
893 #if ENABLE_DEFAULT_SCOPE
894 struct sockaddr_in6 tmp;
895 #endif
896 unsigned int ifscope;
897 struct ifnet *outif = NULL;
898
899 if (inp == NULL || (inp->inp_flags2 & INP2_WANT_FLOW_DIVERT))
900 return (inp == NULL ? EINVAL : EPROTOTYPE);
901 if (nam->sa_len != sizeof(*addr))
902 return EINVAL;
903 if (TAILQ_EMPTY(&ifnet_head))
904 return EADDRNOTAVAIL;
905 if (addr->sin6_family != AF_INET6)
906 return EAFNOSUPPORT;
907 #if ENABLE_DEFAULT_SCOPE
908 if (addr->sin6_scope_id == 0) { /* not change if specified */
909 /* avoid overwrites */
910 tmp = *addr;
911 addr = &tmp;
912 addr->sin6_scope_id = scope6_addr2default(&addr->sin6_addr);
913 }
914 #endif
915
916 ifscope = (inp->inp_flags & INP_BOUND_IF) ?
917 inp->inp_boundifp->if_index : IFSCOPE_NONE;
918
919 /* Source address selection. XXX: need pcblookup? */
920 in6a = in6_selectsrc(addr, inp->in6p_outputopts, inp, &inp->in6p_route,
921 NULL, &storage, ifscope, &error);
922 if (in6a == NULL)
923 return (error ? error : EADDRNOTAVAIL);
924 inp->in6p_laddr = *in6a;
925 inp->in6p_faddr = addr->sin6_addr;
926 if (inp->in6p_route.ro_rt != NULL)
927 outif = inp->in6p_route.ro_rt->rt_ifp;
928 inp->in6p_last_outifp = outif;
929 soisconnected(so);
930 return 0;
931 }
932
933 static int
934 rip6_shutdown(struct socket *so)
935 {
936 socantsendmore(so);
937 return 0;
938 }
939
940 static int
941 rip6_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
942 struct mbuf *control, struct proc *p)
943 {
944 #pragma unused(flags, p)
945 struct inpcb *inp = sotoinpcb(so);
946 struct sockaddr_in6 tmp;
947 struct sockaddr_in6 *dst = (struct sockaddr_in6 *)(void *)nam;
948 int error = 0;
949
950 if (inp == NULL || (inp->inp_flags2 & INP2_WANT_FLOW_DIVERT)) {
951 error = (inp == NULL ? EINVAL : EPROTOTYPE);
952 goto bad;
953 }
954
955 /* always copy sockaddr to avoid overwrites */
956 if (so->so_state & SS_ISCONNECTED) {
957 if (nam != NULL) {
958 error = EISCONN;
959 goto bad;
960 }
961 /* XXX */
962 bzero(&tmp, sizeof(tmp));
963 tmp.sin6_family = AF_INET6;
964 tmp.sin6_len = sizeof(struct sockaddr_in6);
965 bcopy(&inp->in6p_faddr, &tmp.sin6_addr,
966 sizeof(struct in6_addr));
967 dst = &tmp;
968 } else {
969 if (nam == NULL) {
970 error = ENOTCONN;
971 goto bad;
972 }
973 tmp = *(struct sockaddr_in6 *)(void *)nam;
974 dst = &tmp;
975 }
976 #if ENABLE_DEFAULT_SCOPE
977 if (dst->sin6_scope_id == 0) { /* not change if specified */
978 dst->sin6_scope_id = scope6_addr2default(&dst->sin6_addr);
979 }
980 #endif
981 return (rip6_output(m, so, dst, control, 1));
982
983 bad:
984 VERIFY(error != 0);
985
986 if (m != NULL)
987 m_freem(m);
988 if (control != NULL)
989 m_freem(control);
990
991 return (error);
992 }
993
994 struct pr_usrreqs rip6_usrreqs = {
995 .pru_abort = rip6_abort,
996 .pru_attach = rip6_attach,
997 .pru_bind = rip6_bind,
998 .pru_connect = rip6_connect,
999 .pru_control = in6_control,
1000 .pru_detach = rip6_detach,
1001 .pru_disconnect = rip6_disconnect,
1002 .pru_peeraddr = in6_getpeeraddr,
1003 .pru_send = rip6_send,
1004 .pru_shutdown = rip6_shutdown,
1005 .pru_sockaddr = in6_getsockaddr,
1006 .pru_sosend = sosend,
1007 .pru_soreceive = soreceive,
1008 };
1009
1010 __private_extern__ struct pr_usrreqs icmp6_dgram_usrreqs = {
1011 .pru_abort = rip6_abort,
1012 .pru_attach = icmp6_dgram_attach,
1013 .pru_bind = rip6_bind,
1014 .pru_connect = rip6_connect,
1015 .pru_control = in6_control,
1016 .pru_detach = rip6_detach,
1017 .pru_disconnect = rip6_disconnect,
1018 .pru_peeraddr = in6_getpeeraddr,
1019 .pru_send = icmp6_dgram_send,
1020 .pru_shutdown = rip6_shutdown,
1021 .pru_sockaddr = in6_getsockaddr,
1022 .pru_sosend = sosend,
1023 .pru_soreceive = soreceive,
1024 };