74#include <sys/kernel.h>
76#include <sys/malloc.h>
80#include <sys/protosw.h>
81#include <sys/signalvar.h>
82#include <sys/socket.h>
83#include <sys/socketvar.h>
85#include <sys/syslog.h>
88#include <net/if_var.h>
89#include <net/if_types.h>
93#include <netinet/in.h>
94#include <netinet/in_var.h>
95#include <netinet/in_systm.h>
96#include <netinet/in_pcb.h>
98#include <netinet/icmp6.h>
99#include <netinet/ip6.h>
100#include <netinet/ip_var.h>
111#include <netipsec/ipsec_support.h>
113#include <machine/stdarg.h>
115#define satosin6(sa) ((struct sockaddr_in6 *)(sa))
116#define ifatoia6(ifa) ((struct in6_ifaddr *)(ifa))
123#define V_ripcbinfo VNET(ripcbinfo)
163 struct ip6_hdr *ip6 = c->
ip6;
164 int proto = c->
proto;
167 if ((inp->inp_vflag & INP_IPV6) == 0)
169 if (inp->inp_ip_p && inp->inp_ip_p != proto)
189 struct mbuf *n, *m = *mp;
190 struct ip6_hdr *ip6 = mtod(m,
struct ip6_hdr *);
192 struct mbuf *opts = NULL;
195 struct inpcb_iterator inpi = INP_ITERATOR(&
V_ripcbinfo,
205 ifp = m->m_pkthdr.rcvif;
207 while ((inp = inp_next(&inpi)) != NULL) {
208 INP_RLOCK_ASSERT(inp);
209#if defined(IPSEC) || defined(IPSEC_SUPPORT)
213 if (IPSEC_ENABLED(ipv6) &&
214 IPSEC_CHECK_POLICY(ipv6, m, inp) != 0) {
219 if (jailed_without_vnet(inp->inp_cred) &&
228 if (inp->in6p_cksum != -1) {
230 if (m->m_pkthdr.len - (*offp + inp->in6p_cksum) < 2 ||
232 m->m_pkthdr.len - *offp)) {
240 proto = IPPROTO_NONE;
250 if (inp->in6p_moptions &&
271 blocked = MCAST_PASS;
282 (
struct sockaddr *)&mcaddr,
283 (
struct sockaddr *)&fromsa);
285 if (blocked != MCAST_PASS) {
290 if ((n = m_copym(m, 0, M_COPYALL, M_NOWAIT)) == NULL)
292 if (inp->inp_flags & INP_CONTROLOPTS ||
293 inp->inp_socket->so_options & SO_TIMESTAMP)
297 if (sbappendaddr(&inp->inp_socket->so_rcv,
298 (
struct sockaddr *)&fromsa, n, opts) == 0) {
299 soroverflow(inp->inp_socket);
305 sorwakeup(inp->inp_socket);
310 if (delivered == 0) {
312 if (m->m_flags & M_MCAST)
314 if (proto == IPPROTO_NONE)
318 ICMP6_PARAMPROB_NEXTHEADER,
323 return (IPPROTO_DONE);
332 struct inpcb *(*notify)(
struct inpcb *, int) =
in6_rtchange;
334 if (sa->sa_family != AF_INET6 ||
338 if ((
unsigned)cmd >= PRC_NCMDS)
340 if (PRC_IS_REDIRECT(cmd))
342 else if (cmd == PRC_HOSTDEAD)
360 (
const struct sockaddr *)sa6_src, 0, cmd, cmdarg, notify);
370 struct epoch_tracker et;
371 struct mbuf *control;
376 u_int plen = m->m_pkthdr.len;
379 struct ifnet *oifp = NULL;
380 int type = 0, code = 0;
381 int scope_ambiguous = 0;
389 control = va_arg(ap,
struct mbuf *);
395 if (control != NULL) {
398 inp->in6p_outputopts, so->so_cred,
399 so->so_proto->pr_protocol);
407 optp = inp->in6p_outputopts;
414 if (!(so->so_state & SS_ISCONNECTED)) {
428 if (so->so_proto->pr_protocol == IPPROTO_ICMPV6) {
429 struct icmp6_hdr *icmp6;
430 if (m->m_len <
sizeof(
struct icmp6_hdr) &&
431 (m = m_pullup(m,
sizeof(
struct icmp6_hdr))) == NULL) {
435 icmp6 = mtod(m,
struct icmp6_hdr *);
436 type = icmp6->icmp6_type;
437 code = icmp6->icmp6_code;
440 M_PREPEND(m,
sizeof(*ip6), M_NOWAIT);
445 ip6 = mtod(m,
struct ip6_hdr *);
448 if (CALC_FLOWID_OUTBOUND) {
449 uint32_t hash_type, hash_val;
452 &dstsock->
sin6_addr, 0, 0, so->so_proto->pr_protocol,
454 inp->inp_flowid = hash_val;
455 inp->inp_flowtype = hash_type;
463 scope_ambiguous, &in6a, &hlim);
478 ip6->ip6_flow = (ip6->ip6_flow & ~IPV6_FLOWINFO_MASK) |
479 (inp->inp_flow & IPV6_FLOWINFO_MASK);
480 ip6->ip6_vfc = (ip6->ip6_vfc & ~IPV6_VERSION_MASK) |
481 (IPV6_VERSION & IPV6_VERSION_MASK);
486 ip6->ip6_nxt = inp->inp_ip_p;
487 ip6->ip6_hlim = hlim;
489 if (so->so_proto->pr_protocol == IPPROTO_ICMPV6 ||
490 inp->in6p_cksum != -1) {
496 if (so->so_proto->pr_protocol == IPPROTO_ICMPV6)
497 off = offsetof(
struct icmp6_hdr, icmp6_cksum);
499 off = inp->in6p_cksum;
500 if (plen < off + 2) {
504 off +=
sizeof(
struct ip6_hdr);
507 while (n && n->m_len <= off) {
513 p = (u_int16_t *)(mtod(n, caddr_t) + off);
515 *p =
in6_cksum(m, ip6->ip6_nxt,
sizeof(*ip6), plen);
523 so->so_proto->pr_protocol == IPPROTO_ICMPV6) {
525 case ND_ROUTER_ADVERT:
526 case ND_ROUTER_SOLICIT:
527 mtag = m_tag_get(PACKET_TAG_ND_OUTGOING,
528 sizeof(
unsigned short), M_NOWAIT);
531 m_tag_prepend(m, mtag);
536 error =
ip6_output(m, optp, NULL, 0, inp->in6p_moptions, &oifp, inp);
538 if (so->so_proto->pr_protocol == IPPROTO_ICMPV6) {
540 icmp6_ifoutstat_inc(oifp, type, code);
541 ICMP6STAT_INC(icp6s_outhist[type]);
552 if (control != NULL) {
569 if (sopt->sopt_level == IPPROTO_ICMPV6)
575 else if (sopt->sopt_level != IPPROTO_IPV6) {
576 if (sopt->sopt_level == SOL_SOCKET &&
577 sopt->sopt_name == SO_SETFIB) {
580 inp->inp_inc.inc_fibnum = so->so_fibnum;
589 switch (sopt->sopt_dir) {
591 switch (sopt->sopt_name) {
612 switch (sopt->sopt_name) {
640 struct icmp6_filter *filter;
644 KASSERT(inp == NULL, (
"rip6_attach: inp != NULL"));
646 error = priv_check(td, PRIV_NETINET_RAW);
652 filter = malloc(
sizeof(
struct icmp6_filter), M_PCB, M_NOWAIT);
660 inp = (
struct inpcb *)so->so_pcb;
661 inp->inp_vflag |= INP_IPV6;
662 inp->inp_ip_p = (
long)proto;
664 inp->in6p_cksum = -1;
665 inp->in6p_icmp6filt = filter;
666 ICMP6_FILTER_SETPASSALL(inp->in6p_icmp6filt);
677 KASSERT(inp != NULL, (
"rip6_detach: inp == NULL"));
683 free(inp->in6p_icmp6filt, M_PCB);
695 KASSERT(inp != NULL, (
"rip6_abort: inp == NULL"));
697 soisdisconnected(so);
706 KASSERT(inp != NULL, (
"rip6_close: inp == NULL"));
708 soisdisconnected(so);
717 KASSERT(inp != NULL, (
"rip6_disconnect: inp == NULL"));
719 if ((so->so_state & SS_ISCONNECTED) == 0)
727rip6_bind(
struct socket *so,
struct sockaddr *nam,
struct thread *td)
729 struct epoch_tracker et;
732 struct ifaddr *ifa = NULL;
736 KASSERT(inp != NULL, (
"rip6_bind: inp == NULL"));
738 if (nam->sa_family != AF_INET6)
739 return (EAFNOSUPPORT);
740 if (nam->sa_len !=
sizeof(*addr))
744 if (CK_STAILQ_EMPTY(&V_ifnet) || addr->
sin6_family != AF_INET6)
745 return (EADDRNOTAVAIL);
751 (ifa = ifa_ifwithaddr((
struct sockaddr *)addr)) == NULL) {
753 return (EADDRNOTAVAIL);
760 return (EADDRNOTAVAIL);
772rip6_connect(
struct socket *so,
struct sockaddr *nam,
struct thread *td)
777 struct epoch_tracker et;
778 int error = 0, scope_ambiguous = 0;
781 KASSERT(inp != NULL, (
"rip6_connect: inp == NULL"));
783 if (nam->sa_len !=
sizeof(*addr))
785 if (CK_STAILQ_EMPTY(&V_ifnet))
786 return (EADDRNOTAVAIL);
788 return (EAFNOSUPPORT);
808 inp, so->so_cred, scope_ambiguous, &in6a, NULL);
817 inp->in6p_laddr = in6a;
830 KASSERT(inp != NULL, (
"rip6_shutdown: inp == NULL"));
839rip6_send(
struct socket *so,
int flags,
struct mbuf *m,
struct sockaddr *nam,
840 struct mbuf *control,
struct thread *td)
848 KASSERT(inp != NULL, (
"rip6_send: inp == NULL"));
852 if (so->so_state & SS_ISCONNECTED) {
858 bzero(&tmp,
sizeof(tmp));
870 else if (nam->sa_family != AF_INET6)
871 error = EAFNOSUPPORT;
885 log(LOG_INFO,
"rip6 SEND: address family is "
886 "unspec. Assume AF_INET6\n");
889 error = EAFNOSUPPORT;
void icmp6_error(struct mbuf *m, int type, int code, int param)
int icmp6_ctloutput(struct socket *so, struct sockopt *sopt)
const struct sockaddr_in6 sa6_any
const struct in6_addr in6addr_any
int in6_control(struct socket *so, u_long cmd, caddr_t data, struct ifnet *ifp, struct thread *td)
#define IN6_IS_ADDR_UNSPECIFIED(a)
#define IN6_ARE_ADDR_EQUAL(a, b)
int in6_cksum(struct mbuf *, u_int8_t, u_int32_t, u_int32_t)
#define IN6_IS_ADDR_MULTICAST(a)
uint32_t fib6_calc_software_hash(const struct in6_addr *src, const struct in6_addr *dst, unsigned short src_port, unsigned short dst_port, char proto, uint32_t *phashtype)
int prison_check_ip6(const struct ucred *cred, const struct in6_addr *ia6)
int im6o_mc_filter(const struct ip6_moptions *imo, const struct ifnet *ifp, const struct sockaddr *group, const struct sockaddr *src)
int in6_getsockaddr(struct socket *so, struct sockaddr **nam)
struct inpcb * in6_rtchange(struct inpcb *inp, int errno __unused)
void init_sin6(struct sockaddr_in6 *sin6, struct mbuf *m, int srcordst)
void in6_pcbnotify(struct inpcbinfo *pcbinfo, struct sockaddr *dst, u_int fport_arg, const struct sockaddr *src, u_int lport_arg, int cmd, void *cmdarg, struct inpcb *(*notify)(struct inpcb *, int))
int in6_getpeeraddr(struct socket *so, struct sockaddr **nam)
int in6_selectsrc_socket(struct sockaddr_in6 *dstsock, struct ip6_pktopts *opts, struct inpcb *inp, struct ucred *cred, int scope_ambiguous, struct in6_addr *srcp, int *hlim)
#define IN6_IFF_DEPRECATED
void ip6_clearpktopts(struct ip6_pktopts *pktopt, int optname)
int ip6_ctloutput(struct socket *so, struct sockopt *sopt)
int ip6_output(struct mbuf *m0, struct ip6_pktopts *opt, struct route_in6 *ro, int flags, struct ip6_moptions *im6o, struct ifnet **ifpp, struct inpcb *inp)
int ip6_setpktopts(struct mbuf *control, struct ip6_pktopts *opt, struct ip6_pktopts *stickyopt, struct ucred *cred, int uproto)
int ip6_raw_ctloutput(struct socket *so, struct sockopt *sopt)
#define IP6STAT_INC(name)
#define IP6STAT_DEC(name)
#define V_ip6_use_defzone
int(* send_sendso_input_hook)(struct mbuf *, struct ifnet *, int, int)
static bool rip6_inp_match(const struct inpcb *inp, void *v)
int(* mrt6_ioctl)(u_long, caddr_t)
int(* ip6_mrouter_get)(struct socket *, struct sockopt *)
static int rip6_disconnect(struct socket *so)
int(* ip6_mrouter_set)(struct socket *, struct sockopt *)
static int rip6_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
VNET_PCPUSTAT_DEFINE(struct rip6stat, rip6stat)
struct pr_usrreqs rip6_usrreqs
int rip6_input(struct mbuf **mp, int *offp, int proto)
static int rip6_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam, struct mbuf *control, struct thread *td)
static void rip6_close(struct socket *so)
static int rip6_shutdown(struct socket *so)
int(* ip6_mrouter_done)(void)
int rip6_output(struct mbuf *m, struct socket *so,...)
static void rip6_abort(struct socket *so)
static int rip6_attach(struct socket *so, int proto, struct thread *td)
VNET_PCPUSTAT_SYSINIT(rip6stat)
int rip6_ctloutput(struct socket *so, struct sockopt *sopt)
void rip6_ctlinput(int cmd, struct sockaddr *sa, void *d)
VNET_DEFINE(struct socket *, ip6_mrouter)
static int rip6_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
int(* ip6_mforward)(struct ip6_hdr *, struct ifnet *, struct mbuf *)
static void rip6_detach(struct socket *so)
VNET_DECLARE(struct inpcbinfo, ripcbinfo)
#define RIP6STAT_INC(name)
int sa6_embedscope(struct sockaddr_in6 *sin6, int defaultok)
unsigned int ipi6_ifindex
struct in6_pktinfo * ip6po_pktinfo
struct sockaddr_in6 * ip6c_src
struct in6_addr sin6_addr