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>
78 #include <sys/syslog.h>
81 #include <net/if_types.h>
82 #include <net/route.h>
84 #include <netinet/in.h>
85 #include <netinet/in_var.h>
86 #include <netinet/in_systm.h>
87 #include <netinet/icmp6.h>
88 #include <netinet/in_pcb.h>
89 #include <netinet/ip6.h>
90 #include <netinet6/ip6protosw.h>
91 #include <netinet6/ip6_mroute.h>
92 #include <netinet6/in6_pcb.h>
93 #include <netinet6/ip6_var.h>
94 #include <netinet6/nd6.h>
95 #include <netinet6/raw_ip6.h>
96 #include <netinet6/scope6_var.h>
99 #include <netinet6/ipsec.h>
100 #include <netinet6/ipsec6.h>
104 #include <netipsec/ipsec.h>
105 #include <netipsec/ipsec6.h>
106 #endif /* FAST_IPSEC */
108 #include <machine/stdarg.h>
110 #define satosin6(sa) ((struct sockaddr_in6 *)(sa))
111 #define ifatoia6(ifa) ((struct in6_ifaddr *)(ifa))
114 * Raw interface to IP6 protocol.
117 extern struct inpcbhead ripcb;
118 extern struct inpcbinfo ripcbinfo;
119 extern u_long rip_sendspace;
120 extern u_long rip_recvspace;
122 struct rip6stat rip6stat;
125 * Setup generic address and protocol structures
126 * for raw_input routine, then pass them along with
130 rip6_input(mp, offp, proto)
134 struct mbuf *m = *mp;
135 register struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
136 register struct inpcb *in6p;
137 struct inpcb *last = 0;
138 struct mbuf *opts = NULL;
139 struct sockaddr_in6 fromsa;
141 rip6stat.rip6s_ipackets++;
143 if (faithprefix_p != NULL && (*faithprefix_p)(&ip6->ip6_dst)) {
144 /* XXX send icmp6 host/port unreach? */
149 init_sin6(&fromsa, m); /* general init */
151 INP_INFO_RLOCK(&ripcbinfo);
152 LIST_FOREACH(in6p, &ripcb, inp_list) {
154 if ((in6p->in6p_vflag & INP_IPV6) == 0) {
159 if (in6p->in6p_ip6_nxt &&
160 in6p->in6p_ip6_nxt != proto)
162 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) &&
163 !IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, &ip6->ip6_dst))
165 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr) &&
166 !IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, &ip6->ip6_src))
168 if (in6p->in6p_cksum != -1) {
169 rip6stat.rip6s_isum++;
170 if (in6_cksum(m, proto, *offp,
171 m->m_pkthdr.len - *offp)) {
172 rip6stat.rip6s_badsum++;
177 struct mbuf *n = m_copy(m, 0, (int)M_COPYALL);
179 #if defined(IPSEC) || defined(FAST_IPSEC)
181 * Check AH/ESP integrity.
183 if (n && ipsec6_in_reject(n, last)) {
186 ipsec6stat.in_polvio++;
188 /* do not inject data into pcb */
190 #endif /*IPSEC || FAST_IPSEC*/
192 if (last->in6p_flags & IN6P_CONTROLOPTS ||
193 last->in6p_socket->so_options & SO_TIMESTAMP)
194 ip6_savecontrol(last, n, &opts);
195 /* strip intermediate headers */
197 if (sbappendaddr(&last->in6p_socket->so_rcv,
198 (struct sockaddr *)&fromsa,
203 rip6stat.rip6s_fullsock++;
205 sorwakeup(last->in6p_socket);
212 #if defined(IPSEC) || defined(FAST_IPSEC)
214 * Check AH/ESP integrity.
216 if (last && ipsec6_in_reject(m, last)) {
219 ipsec6stat.in_polvio++;
221 ip6stat.ip6s_delivered--;
222 /* 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 int scope_ambiguous = 0;
332 struct in6_addr *in6a;
336 so = va_arg(ap, struct socket *);
337 dstsock = va_arg(ap, struct sockaddr_in6 *);
338 control = va_arg(ap, struct mbuf *);
341 in6p = sotoin6pcb(so);
345 if (so->so_cred->cr_uid == 0)
347 dst = &dstsock->sin6_addr;
349 if ((error = ip6_setpktopts(control, &opt,
350 in6p->in6p_outputopts, priv, so->so_proto->pr_protocol))
356 optp = in6p->in6p_outputopts;
359 * Check and convert scope zone ID into internal form.
360 * XXX: we may still need to determine the zone later.
362 if (!(so->so_state & SS_ISCONNECTED)) {
363 if (dstsock->sin6_scope_id == 0 && !ip6_use_defzone)
365 if ((error = sa6_embedscope(dstsock, ip6_use_defzone)) != 0)
370 * For an ICMPv6 packet, we should know its type and code
371 * to update statistics.
373 if (so->so_proto->pr_protocol == IPPROTO_ICMPV6) {
374 struct icmp6_hdr *icmp6;
375 if (m->m_len < sizeof(struct icmp6_hdr) &&
376 (m = m_pullup(m, sizeof(struct icmp6_hdr))) == NULL) {
380 icmp6 = mtod(m, struct icmp6_hdr *);
381 type = icmp6->icmp6_type;
382 code = icmp6->icmp6_code;
385 M_PREPEND(m, sizeof(*ip6), M_DONTWAIT);
390 ip6 = mtod(m, struct ip6_hdr *);
393 * Source address selection.
395 if ((in6a = in6_selectsrc(dstsock, optp, in6p->in6p_moptions, NULL,
396 &in6p->in6p_laddr, &oifp, &error)) == NULL) {
398 error = EADDRNOTAVAIL;
401 ip6->ip6_src = *in6a;
403 if (oifp && scope_ambiguous) {
405 * Application should provide a proper zone ID or the use of
406 * default zone IDs should be enabled. Unfortunately, some
407 * applications do not behave as it should, so we need a
408 * workaround. Even if an appropriate ID is not determined
409 * (when it's required), if we can determine the outgoing
410 * interface. determine the zone ID based on the interface.
412 error = in6_setscope(&dstsock->sin6_addr, oifp, NULL);
416 ip6->ip6_dst = dstsock->sin6_addr;
418 /* fill in the rest of the IPv6 header fields */
419 ip6->ip6_flow = (ip6->ip6_flow & ~IPV6_FLOWINFO_MASK) |
420 (in6p->in6p_flowinfo & IPV6_FLOWINFO_MASK);
421 ip6->ip6_vfc = (ip6->ip6_vfc & ~IPV6_VERSION_MASK) |
422 (IPV6_VERSION & IPV6_VERSION_MASK);
423 /* ip6_plen will be filled in ip6_output, so not fill it here. */
424 ip6->ip6_nxt = in6p->in6p_ip6_nxt;
425 ip6->ip6_hlim = in6_selecthlim(in6p, oifp);
427 if (so->so_proto->pr_protocol == IPPROTO_ICMPV6 ||
428 in6p->in6p_cksum != -1) {
433 /* compute checksum */
434 if (so->so_proto->pr_protocol == IPPROTO_ICMPV6)
435 off = offsetof(struct icmp6_hdr, icmp6_cksum);
437 off = in6p->in6p_cksum;
438 if (plen < off + 1) {
442 off += sizeof(struct ip6_hdr);
445 while (n && n->m_len <= off) {
451 p = (u_int16_t *)(mtod(n, caddr_t) + off);
453 *p = in6_cksum(m, ip6->ip6_nxt, sizeof(*ip6), plen);
456 error = ip6_output(m, optp, NULL, 0, in6p->in6p_moptions, &oifp, in6p);
457 if (so->so_proto->pr_protocol == IPPROTO_ICMPV6) {
459 icmp6_ifoutstat_inc(oifp, type, code);
460 icmp6stat.icp6s_outhist[type]++;
462 rip6stat.rip6s_opackets++;
472 ip6_clearpktopts(&opt, -1);
480 * Raw IPv6 socket option processing.
483 rip6_ctloutput(so, sopt)
485 struct sockopt *sopt;
489 if (sopt->sopt_level == IPPROTO_ICMPV6)
491 * XXX: is it better to call icmp6_ctloutput() directly
494 return (icmp6_ctloutput(so, sopt));
495 else if (sopt->sopt_level != IPPROTO_IPV6)
500 switch (sopt->sopt_dir) {
502 switch (sopt->sopt_name) {
510 error = ip6_mrouter_get(so, sopt);
513 error = ip6_raw_ctloutput(so, sopt);
516 error = ip6_ctloutput(so, sopt);
522 switch (sopt->sopt_name) {
530 error = ip6_mrouter_set(so, sopt);
533 error = ip6_raw_ctloutput(so, sopt);
536 error = ip6_ctloutput(so, sopt);
546 rip6_attach(struct socket *so, int proto, struct thread *td)
549 struct icmp6_filter *filter;
553 KASSERT(inp == NULL, ("rip6_attach: inp != NULL"));
554 if (td && (error = suser(td)) != 0)
556 error = soreserve(so, rip_sendspace, rip_recvspace);
559 MALLOC(filter, struct icmp6_filter *,
560 sizeof(struct icmp6_filter), M_PCB, M_NOWAIT);
563 INP_INFO_WLOCK(&ripcbinfo);
565 error = in_pcballoc(so, &ripcbinfo, "raw6inp");
568 INP_INFO_WUNLOCK(&ripcbinfo);
572 inp = (struct inpcb *)so->so_pcb;
574 INP_INFO_WUNLOCK(&ripcbinfo);
575 inp->inp_vflag |= INP_IPV6;
576 inp->in6p_ip6_nxt = (long)proto;
577 inp->in6p_hops = -1; /* use kernel default */
578 inp->in6p_cksum = -1;
579 inp->in6p_icmp6filt = filter;
580 ICMP6_FILTER_SETPASSALL(inp->in6p_icmp6filt);
586 rip6_detach(struct socket *so)
591 KASSERT(inp != NULL, ("rip6_detach: inp == NULL"));
593 if (so == ip6_mrouter)
595 if (inp->in6p_icmp6filt) {
596 FREE(inp->in6p_icmp6filt, M_PCB);
597 inp->in6p_icmp6filt = NULL;
599 INP_INFO_WLOCK(&ripcbinfo);
603 INP_INFO_WUNLOCK(&ripcbinfo);
607 rip6_abort(struct socket *so)
609 soisdisconnected(so);
614 rip6_disconnect(struct socket *so)
616 struct inpcb *inp = sotoinpcb(so);
618 if ((so->so_state & SS_ISCONNECTED) == 0)
620 inp->in6p_faddr = in6addr_any;
626 rip6_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
628 struct inpcb *inp = sotoinpcb(so);
629 struct sockaddr_in6 *addr = (struct sockaddr_in6 *)nam;
630 struct ifaddr *ia = NULL;
633 KASSERT(inp != NULL, ("rip6_bind: inp == NULL"));
634 if (nam->sa_len != sizeof(*addr))
636 if (TAILQ_EMPTY(&ifnet) || addr->sin6_family != AF_INET6)
637 return EADDRNOTAVAIL;
638 if ((error = sa6_embedscope(addr, ip6_use_defzone)) != 0)
641 if (!IN6_IS_ADDR_UNSPECIFIED(&addr->sin6_addr) &&
642 (ia = ifa_ifwithaddr((struct sockaddr *)addr)) == 0)
643 return EADDRNOTAVAIL;
645 ((struct in6_ifaddr *)ia)->ia6_flags &
646 (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY|
647 IN6_IFF_DETACHED|IN6_IFF_DEPRECATED)) {
648 return (EADDRNOTAVAIL);
650 INP_INFO_WLOCK(&ripcbinfo);
652 inp->in6p_laddr = addr->sin6_addr;
654 INP_INFO_WUNLOCK(&ripcbinfo);
659 rip6_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
661 struct inpcb *inp = sotoinpcb(so);
662 struct sockaddr_in6 *addr = (struct sockaddr_in6 *)nam;
663 struct in6_addr *in6a = NULL;
664 struct ifnet *ifp = NULL;
665 int error = 0, scope_ambiguous = 0;
667 KASSERT(inp != NULL, ("rip6_connect: inp == NULL"));
668 if (nam->sa_len != sizeof(*addr))
670 if (TAILQ_EMPTY(&ifnet))
671 return EADDRNOTAVAIL;
672 if (addr->sin6_family != AF_INET6)
676 * Application should provide a proper zone ID or the use of
677 * default zone IDs should be enabled. Unfortunately, some
678 * applications do not behave as it should, so we need a
679 * workaround. Even if an appropriate ID is not determined,
680 * we'll see if we can determine the outgoing interface. If we
681 * can, determine the zone ID based on the interface below.
683 if (addr->sin6_scope_id == 0 && !ip6_use_defzone)
685 if ((error = sa6_embedscope(addr, ip6_use_defzone)) != 0)
688 INP_INFO_WLOCK(&ripcbinfo);
690 /* Source address selection. XXX: need pcblookup? */
691 in6a = in6_selectsrc(addr, inp->in6p_outputopts,
692 inp->in6p_moptions, NULL,
693 &inp->in6p_laddr, &ifp, &error);
696 INP_INFO_WUNLOCK(&ripcbinfo);
697 return (error ? error : EADDRNOTAVAIL);
701 if (ifp && scope_ambiguous &&
702 (error = in6_setscope(&addr->sin6_addr, ifp, NULL)) != 0) {
704 INP_INFO_WUNLOCK(&ripcbinfo);
707 inp->in6p_faddr = addr->sin6_addr;
708 inp->in6p_laddr = *in6a;
711 INP_INFO_WUNLOCK(&ripcbinfo);
716 rip6_shutdown(struct socket *so)
721 KASSERT(inp != NULL, ("rip6_shutdown: inp == NULL"));
729 rip6_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
730 struct mbuf *control, struct thread *td)
732 struct inpcb *inp = sotoinpcb(so);
733 struct sockaddr_in6 tmp;
734 struct sockaddr_in6 *dst;
737 KASSERT(inp != NULL, ("rip6_send: inp == NULL"));
738 INP_INFO_WLOCK(&ripcbinfo);
739 /* always copy sockaddr to avoid overwrites */
741 if (so->so_state & SS_ISCONNECTED) {
743 INP_INFO_WUNLOCK(&ripcbinfo);
748 bzero(&tmp, sizeof(tmp));
749 tmp.sin6_family = AF_INET6;
750 tmp.sin6_len = sizeof(struct sockaddr_in6);
751 bcopy(&inp->in6p_faddr, &tmp.sin6_addr,
752 sizeof(struct in6_addr));
756 INP_INFO_WUNLOCK(&ripcbinfo);
760 if (nam->sa_len != sizeof(struct sockaddr_in6)) {
761 INP_INFO_WUNLOCK(&ripcbinfo);
765 tmp = *(struct sockaddr_in6 *)nam;
768 if (dst->sin6_family == AF_UNSPEC) {
770 * XXX: we allow this case for backward
771 * compatibility to buggy applications that
772 * rely on old (and wrong) kernel behavior.
774 log(LOG_INFO, "rip6 SEND: address family is "
775 "unspec. Assume AF_INET6\n");
776 dst->sin6_family = AF_INET6;
777 } else if (dst->sin6_family != AF_INET6) {
778 INP_INFO_WUNLOCK(&ripcbinfo);
780 return(EAFNOSUPPORT);
783 ret = rip6_output(m, so, dst, control);
784 INP_INFO_WUNLOCK(&ripcbinfo);
788 struct pr_usrreqs rip6_usrreqs = {
789 .pru_abort = rip6_abort,
790 .pru_attach = rip6_attach,
791 .pru_bind = rip6_bind,
792 .pru_connect = rip6_connect,
793 .pru_control = in6_control,
794 .pru_detach = rip6_detach,
795 .pru_disconnect = rip6_disconnect,
796 .pru_peeraddr = in6_setpeeraddr,
797 .pru_send = rip6_send,
798 .pru_shutdown = rip6_shutdown,
799 .pru_sockaddr = in6_setsockaddr,