2 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the project nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * Copyright (c) 1982, 1986, 1988, 1993
34 * The Regents of the University of California. All rights reserved.
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
44 * 4. Neither the name of the University nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
60 * @(#)raw_ip.c 8.2 (Berkeley) 1/4/94
63 #include "opt_ipsec.h"
64 #include "opt_inet6.h"
66 #include <sys/param.h>
67 #include <sys/errno.h>
69 #include <sys/malloc.h>
72 #include <sys/protosw.h>
73 #include <sys/signalvar.h>
74 #include <sys/socket.h>
75 #include <sys/socketvar.h>
77 #include <sys/systm.h>
80 #include <net/if_types.h>
81 #include <net/route.h>
83 #include <netinet/in.h>
84 #include <netinet/in_var.h>
85 #include <netinet/in_systm.h>
86 #include <netinet/icmp6.h>
87 #include <netinet/in_pcb.h>
88 #include <netinet/ip6.h>
89 #include <netinet6/ip6protosw.h>
90 #include <netinet6/ip6_mroute.h>
91 #include <netinet6/in6_pcb.h>
92 #include <netinet6/ip6_var.h>
93 #include <netinet6/nd6.h>
94 #include <netinet6/raw_ip6.h>
95 #ifdef ENABLE_DEFAULT_SCOPE
96 #include <netinet6/scope6_var.h>
100 #include <netinet6/ipsec.h>
101 #include <netinet6/ipsec6.h>
105 #include <netipsec/ipsec.h>
106 #include <netipsec/ipsec6.h>
107 #endif /* FAST_IPSEC */
109 #include <machine/stdarg.h>
111 #define satosin6(sa) ((struct sockaddr_in6 *)(sa))
112 #define ifatoia6(ifa) ((struct in6_ifaddr *)(ifa))
115 * Raw interface to IP6 protocol.
118 extern struct inpcbhead ripcb;
119 extern struct inpcbinfo ripcbinfo;
120 extern u_long rip_sendspace;
121 extern u_long rip_recvspace;
123 struct rip6stat rip6stat;
126 * Setup generic address and protocol structures
127 * for raw_input routine, then pass them along with
131 rip6_input(mp, offp, proto)
135 struct mbuf *m = *mp;
136 register struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
137 register struct inpcb *in6p;
138 struct inpcb *last = 0;
139 struct mbuf *opts = NULL;
140 struct sockaddr_in6 fromsa;
142 rip6stat.rip6s_ipackets++;
144 if (faithprefix_p != NULL && (*faithprefix_p)(&ip6->ip6_dst)) {
145 /* XXX send icmp6 host/port unreach? */
150 init_sin6(&fromsa, m); /* general init */
152 INP_INFO_RLOCK(&ripcbinfo);
153 LIST_FOREACH(in6p, &ripcb, inp_list) {
155 if ((in6p->in6p_vflag & INP_IPV6) == 0) {
160 if (in6p->in6p_ip6_nxt &&
161 in6p->in6p_ip6_nxt != proto)
163 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) &&
164 !IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, &ip6->ip6_dst))
166 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr) &&
167 !IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, &ip6->ip6_src))
169 if (in6p->in6p_cksum != -1) {
170 rip6stat.rip6s_isum++;
171 if (in6_cksum(m, ip6->ip6_nxt, *offp,
172 m->m_pkthdr.len - *offp)) {
173 rip6stat.rip6s_badsum++;
178 struct mbuf *n = m_copy(m, 0, (int)M_COPYALL);
180 #if defined(IPSEC) || defined(FAST_IPSEC)
182 * Check AH/ESP integrity.
184 if (n && ipsec6_in_reject(n, last)) {
187 ipsec6stat.in_polvio++;
189 /* do not inject data into pcb */
191 #endif /*IPSEC || FAST_IPSEC*/
193 if (last->in6p_flags & IN6P_CONTROLOPTS ||
194 last->in6p_socket->so_options & SO_TIMESTAMP)
195 ip6_savecontrol(last, n, &opts);
196 /* strip intermediate headers */
198 if (sbappendaddr(&last->in6p_socket->so_rcv,
199 (struct sockaddr *)&fromsa,
204 rip6stat.rip6s_fullsock++;
206 sorwakeup(last->in6p_socket);
213 #if defined(IPSEC) || defined(FAST_IPSEC)
215 * Check AH/ESP integrity.
217 if (last && ipsec6_in_reject(m, last)) {
220 ipsec6stat.in_polvio++;
222 ip6stat.ip6s_delivered--;
223 /* do not inject data into pcb */
225 #endif /*IPSEC || FAST_IPSEC*/
227 if (last->in6p_flags & IN6P_CONTROLOPTS ||
228 last->in6p_socket->so_options & SO_TIMESTAMP)
229 ip6_savecontrol(last, m, &opts);
230 /* strip intermediate headers */
232 if (sbappendaddr(&last->in6p_socket->so_rcv,
233 (struct sockaddr *)&fromsa, m, opts) == 0) {
237 rip6stat.rip6s_fullsock++;
239 sorwakeup(last->in6p_socket);
242 rip6stat.rip6s_nosock++;
243 if (m->m_flags & M_MCAST)
244 rip6stat.rip6s_nosockmcast++;
245 if (proto == IPPROTO_NONE)
248 char *prvnxtp = ip6_get_prevhdr(m, *offp); /* XXX */
249 icmp6_error(m, ICMP6_PARAM_PROB,
250 ICMP6_PARAMPROB_NEXTHEADER,
251 prvnxtp - mtod(m, char *));
253 ip6stat.ip6s_delivered--;
255 INP_INFO_RUNLOCK(&ripcbinfo);
260 rip6_ctlinput(cmd, sa, d)
268 struct ip6ctlparam *ip6cp = NULL;
269 const struct sockaddr_in6 *sa6_src = NULL;
271 struct inpcb *(*notify) __P((struct inpcb *, int)) = in6_rtchange;
273 if (sa->sa_family != AF_INET6 ||
274 sa->sa_len != sizeof(struct sockaddr_in6))
277 if ((unsigned)cmd >= PRC_NCMDS)
279 if (PRC_IS_REDIRECT(cmd))
280 notify = in6_rtchange, d = NULL;
281 else if (cmd == PRC_HOSTDEAD)
283 else if (inet6ctlerrmap[cmd] == 0)
286 /* if the parameter is from icmp6, decode it. */
288 ip6cp = (struct ip6ctlparam *)d;
290 ip6 = ip6cp->ip6c_ip6;
291 off = ip6cp->ip6c_off;
292 cmdarg = ip6cp->ip6c_cmdarg;
293 sa6_src = ip6cp->ip6c_src;
301 (void) in6_pcbnotify(&ripcbinfo, sa, 0,
302 (const struct sockaddr *)sa6_src,
303 0, cmd, cmdarg, notify);
307 * Generate IPv6 header and pass packet to ip6_output.
308 * Tack on options user may have setup with control call.
312 rip6_output(struct mbuf *m, ...)
314 rip6_output(m, va_alist)
319 struct mbuf *control;
321 struct sockaddr_in6 *dstsock;
322 struct in6_addr *dst;
325 u_int plen = m->m_pkthdr.len;
327 struct ip6_pktopts opt, *optp;
328 struct ifnet *oifp = NULL;
329 int type = 0, code = 0; /* for ICMPv6 output statistics only */
331 struct in6_addr *in6a;
335 so = va_arg(ap, struct socket *);
336 dstsock = va_arg(ap, struct sockaddr_in6 *);
337 control = va_arg(ap, struct mbuf *);
340 in6p = sotoin6pcb(so);
344 if (so->so_cred->cr_uid == 0)
346 dst = &dstsock->sin6_addr;
348 if ((error = ip6_setpktopts(control, &opt,
349 in6p->in6p_outputopts, priv, 0, so->so_proto->pr_protocol))
355 optp = in6p->in6p_outputopts;
358 * For an ICMPv6 packet, we should know its type and code
359 * to update statistics.
361 if (so->so_proto->pr_protocol == IPPROTO_ICMPV6) {
362 struct icmp6_hdr *icmp6;
363 if (m->m_len < sizeof(struct icmp6_hdr) &&
364 (m = m_pullup(m, sizeof(struct icmp6_hdr))) == NULL) {
368 icmp6 = mtod(m, struct icmp6_hdr *);
369 type = icmp6->icmp6_type;
370 code = icmp6->icmp6_code;
373 M_PREPEND(m, sizeof(*ip6), M_DONTWAIT);
378 ip6 = mtod(m, struct ip6_hdr *);
381 * Next header might not be ICMP6 but use its pseudo header anyway.
386 * If the scope of the destination is link-local, embed the interface
387 * index in the address.
389 * XXX advanced-api value overrides sin6_scope_id
391 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst)) {
392 struct in6_pktinfo *pi;
395 * XXX Boundary check is assumed to be already done in
399 (pi = optp->ip6po_pktinfo) &&
401 ip6->ip6_dst.s6_addr16[1] = htons(pi->ipi6_ifindex);
402 oifp = ifnet_byindex(pi->ipi6_ifindex);
403 } else if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) &&
404 in6p->in6p_moptions &&
405 in6p->in6p_moptions->im6o_multicast_ifp) {
406 oifp = in6p->in6p_moptions->im6o_multicast_ifp;
407 ip6->ip6_dst.s6_addr16[1] = htons(oifp->if_index);
408 } else if (dstsock->sin6_scope_id) {
410 if (dstsock->sin6_scope_id < 0 ||
411 if_index < dstsock->sin6_scope_id) {
412 error = ENXIO; /* XXX EINVAL? */
415 ip6->ip6_dst.s6_addr16[1] =
416 htons(dstsock->sin6_scope_id & 0xffff); /* XXX */
421 * Source address selection.
423 if ((in6a = in6_selectsrc(dstsock, optp, in6p->in6p_moptions, NULL,
424 &in6p->in6p_laddr, &error)) == 0) {
426 error = EADDRNOTAVAIL;
429 ip6->ip6_src = *in6a;
430 ip6->ip6_flow = (ip6->ip6_flow & ~IPV6_FLOWINFO_MASK) |
431 (in6p->in6p_flowinfo & IPV6_FLOWINFO_MASK);
432 ip6->ip6_vfc = (ip6->ip6_vfc & ~IPV6_VERSION_MASK) |
433 (IPV6_VERSION & IPV6_VERSION_MASK);
434 /* ip6_plen will be filled in ip6_output, so not fill it here. */
435 ip6->ip6_nxt = in6p->in6p_ip6_nxt;
436 ip6->ip6_hlim = in6_selecthlim(in6p, oifp);
438 if (so->so_proto->pr_protocol == IPPROTO_ICMPV6 ||
439 in6p->in6p_cksum != -1) {
444 /* compute checksum */
445 if (so->so_proto->pr_protocol == IPPROTO_ICMPV6)
446 off = offsetof(struct icmp6_hdr, icmp6_cksum);
448 off = in6p->in6p_cksum;
449 if (plen < off + 1) {
453 off += sizeof(struct ip6_hdr);
456 while (n && n->m_len <= off) {
462 p = (u_int16_t *)(mtod(n, caddr_t) + off);
464 *p = in6_cksum(m, ip6->ip6_nxt, sizeof(*ip6), plen);
467 error = ip6_output(m, optp, NULL, 0, in6p->in6p_moptions, &oifp, in6p);
468 if (so->so_proto->pr_protocol == IPPROTO_ICMPV6) {
470 icmp6_ifoutstat_inc(oifp, type, code);
471 icmp6stat.icp6s_outhist[type]++;
473 rip6stat.rip6s_opackets++;
483 ip6_clearpktopts(&opt, -1);
491 * Raw IPv6 socket option processing.
494 rip6_ctloutput(so, sopt)
496 struct sockopt *sopt;
500 if (sopt->sopt_level == IPPROTO_ICMPV6)
502 * XXX: is it better to call icmp6_ctloutput() directly
505 return (icmp6_ctloutput(so, sopt));
506 else if (sopt->sopt_level != IPPROTO_IPV6)
511 switch (sopt->sopt_dir) {
513 switch (sopt->sopt_name) {
521 error = ip6_mrouter_get(so, sopt);
524 error = ip6_raw_ctloutput(so, sopt);
527 error = ip6_ctloutput(so, sopt);
533 switch (sopt->sopt_name) {
541 error = ip6_mrouter_set(so, sopt);
544 error = ip6_raw_ctloutput(so, sopt);
547 error = ip6_ctloutput(so, sopt);
557 rip6_attach(struct socket *so, int proto, struct thread *td)
560 struct icmp6_filter *filter;
563 INP_INFO_WLOCK(&ripcbinfo);
566 INP_INFO_WUNLOCK(&ripcbinfo);
567 panic("rip6_attach");
569 if (td && (error = suser(td)) != 0) {
570 INP_INFO_WUNLOCK(&ripcbinfo);
573 error = soreserve(so, rip_sendspace, rip_recvspace);
575 INP_INFO_WUNLOCK(&ripcbinfo);
578 MALLOC(filter, struct icmp6_filter *,
579 sizeof(struct icmp6_filter), M_PCB, M_NOWAIT);
583 error = in_pcballoc(so, &ripcbinfo, "raw6inp");
586 INP_INFO_WUNLOCK(&ripcbinfo);
590 inp = (struct inpcb *)so->so_pcb;
592 INP_INFO_WUNLOCK(&ripcbinfo);
593 inp->inp_vflag |= INP_IPV6;
594 inp->in6p_ip6_nxt = (long)proto;
595 inp->in6p_hops = -1; /* use kernel default */
596 inp->in6p_cksum = -1;
597 inp->in6p_icmp6filt = filter;
598 ICMP6_FILTER_SETPASSALL(inp->in6p_icmp6filt);
604 rip6_detach(struct socket *so)
608 INP_INFO_WLOCK(&ripcbinfo);
611 INP_INFO_WUNLOCK(&ripcbinfo);
612 panic("rip6_detach");
615 if (so == ip6_mrouter)
617 if (inp->in6p_icmp6filt) {
618 FREE(inp->in6p_icmp6filt, M_PCB);
619 inp->in6p_icmp6filt = NULL;
623 INP_INFO_WUNLOCK(&ripcbinfo);
628 rip6_abort(struct socket *so)
630 soisdisconnected(so);
631 return rip6_detach(so);
635 rip6_disconnect(struct socket *so)
637 struct inpcb *inp = sotoinpcb(so);
639 if ((so->so_state & SS_ISCONNECTED) == 0)
641 inp->in6p_faddr = in6addr_any;
642 return rip6_abort(so);
646 rip6_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
648 struct inpcb *inp = sotoinpcb(so);
649 struct sockaddr_in6 *addr = (struct sockaddr_in6 *)nam;
650 struct ifaddr *ia = NULL;
652 if (nam->sa_len != sizeof(*addr))
654 if (TAILQ_EMPTY(&ifnet) || addr->sin6_family != AF_INET6)
655 return EADDRNOTAVAIL;
656 #ifdef ENABLE_DEFAULT_SCOPE
657 if (addr->sin6_scope_id == 0) { /* not change if specified */
658 addr->sin6_scope_id = scope6_addr2default(&addr->sin6_addr);
661 if (!IN6_IS_ADDR_UNSPECIFIED(&addr->sin6_addr) &&
662 (ia = ifa_ifwithaddr((struct sockaddr *)addr)) == 0)
663 return EADDRNOTAVAIL;
665 ((struct in6_ifaddr *)ia)->ia6_flags &
666 (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY|
667 IN6_IFF_DETACHED|IN6_IFF_DEPRECATED)) {
668 return (EADDRNOTAVAIL);
670 INP_INFO_WLOCK(&ripcbinfo);
672 inp->in6p_laddr = addr->sin6_addr;
674 INP_INFO_WUNLOCK(&ripcbinfo);
679 rip6_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
681 struct inpcb *inp = sotoinpcb(so);
682 struct sockaddr_in6 *addr = (struct sockaddr_in6 *)nam;
683 struct in6_addr *in6a = NULL;
685 #ifdef ENABLE_DEFAULT_SCOPE
686 struct sockaddr_in6 tmp;
689 if (nam->sa_len != sizeof(*addr))
691 if (TAILQ_EMPTY(&ifnet))
692 return EADDRNOTAVAIL;
693 if (addr->sin6_family != AF_INET6)
695 #ifdef ENABLE_DEFAULT_SCOPE
696 if (addr->sin6_scope_id == 0) { /* not change if specified */
697 /* avoid overwrites */
700 addr->sin6_scope_id = scope6_addr2default(&addr->sin6_addr);
703 INP_INFO_WLOCK(&ripcbinfo);
705 /* Source address selection. XXX: need pcblookup? */
706 in6a = in6_selectsrc(addr, inp->in6p_outputopts,
707 inp->in6p_moptions, NULL,
708 &inp->in6p_laddr, &error);
711 INP_INFO_WUNLOCK(&ripcbinfo);
712 return (error ? error : EADDRNOTAVAIL);
714 inp->in6p_laddr = *in6a;
715 inp->in6p_faddr = addr->sin6_addr;
718 INP_INFO_WUNLOCK(&ripcbinfo);
723 rip6_shutdown(struct socket *so)
727 INP_INFO_RLOCK(&ripcbinfo);
730 INP_INFO_RUNLOCK(&ripcbinfo);
737 rip6_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
738 struct mbuf *control, struct thread *td)
740 struct inpcb *inp = sotoinpcb(so);
741 struct sockaddr_in6 tmp;
742 struct sockaddr_in6 *dst;
745 INP_INFO_WLOCK(&ripcbinfo);
746 /* always copy sockaddr to avoid overwrites */
748 if (so->so_state & SS_ISCONNECTED) {
750 INP_INFO_WUNLOCK(&ripcbinfo);
755 bzero(&tmp, sizeof(tmp));
756 tmp.sin6_family = AF_INET6;
757 tmp.sin6_len = sizeof(struct sockaddr_in6);
758 bcopy(&inp->in6p_faddr, &tmp.sin6_addr,
759 sizeof(struct in6_addr));
763 INP_INFO_WUNLOCK(&ripcbinfo);
767 tmp = *(struct sockaddr_in6 *)nam;
770 #ifdef ENABLE_DEFAULT_SCOPE
771 if (dst->sin6_scope_id == 0) { /* not change if specified */
772 dst->sin6_scope_id = scope6_addr2default(&dst->sin6_addr);
775 ret = rip6_output(m, so, dst, control);
776 INP_INFO_WUNLOCK(&ripcbinfo);
780 struct pr_usrreqs rip6_usrreqs = {
781 .pru_abort = rip6_abort,
782 .pru_attach = rip6_attach,
783 .pru_bind = rip6_bind,
784 .pru_connect = rip6_connect,
785 .pru_control = in6_control,
786 .pru_detach = rip6_detach,
787 .pru_disconnect = rip6_disconnect,
788 .pru_peeraddr = in6_setpeeraddr,
789 .pru_send = rip6_send,
790 .pru_shutdown = rip6_shutdown,
791 .pru_sockaddr = in6_setsockaddr,