#include <sys/param.h>
#include <sys/malloc.h>
#include <sys/mbuf.h>
#include <sys/protosw.h>
#include <sys/socket.h>
#include <sys/socketvar.h>
#include <sys/sysctl.h>
#include <sys/errno.h>
#include <sys/stat.h>
#include <sys/systm.h>
#include <sys/proc.h>
#include <sys/syslog.h>
#include <machine/endian.h>
#include <net/if.h>
#include <net/route.h>
#include <net/if_types.h>
#include <net/ntstat.h>
#include <netinet/in.h>
#include <netinet/in_var.h>
#include <netinet/in_systm.h>
#include <netinet/ip.h>
#include <netinet/ip_var.h>
#include <netinet/in_pcb.h>
#include <netinet/udp.h>
#include <netinet/udp_var.h>
#include <netinet/ip6.h>
#include <netinet6/ip6_var.h>
#include <netinet6/in6_pcb.h>
#include <netinet6/udp6_var.h>
#include <netinet/icmp6.h>
#include <netinet6/ip6protosw.h>
#ifdef IPSEC
#include <netinet6/ipsec.h>
#ifdef INET6
#include <netinet6/ipsec6.h>
#endif
extern int ipsec_bypass;
#endif
#include <net/net_osdep.h>
#define in6pcb inpcb
#define udp6stat udpstat
#define udp6s_opackets udps_opackets
static __inline__ u_int16_t
get_socket_id(struct socket * s)
{
u_int16_t val;
if (s == NULL) {
return (0);
}
val = (u_int16_t)(((uintptr_t)s) / sizeof(struct socket));
if (val == 0) {
val = 0xffff;
}
return (val);
}
int
udp6_output(in6p, m, addr6, control, p)
struct in6pcb *in6p;
struct mbuf *m;
struct mbuf *control;
struct sockaddr *addr6;
struct proc *p;
{
u_int32_t ulen = m->m_pkthdr.len;
u_int32_t plen = sizeof(struct udphdr) + ulen;
struct ip6_hdr *ip6;
struct udphdr *udp6;
struct in6_addr *laddr, *faddr;
u_short fport;
int error = 0;
struct ip6_pktopts opt, *optp = NULL;
struct ip6_moptions *im6o;
int af = AF_INET6, hlen = sizeof(struct ip6_hdr);
int flags;
struct sockaddr_in6 tmp;
struct in6_addr storage;
mbuf_svc_class_t msc = MBUF_SC_UNSPEC;
struct ip6_out_args ip6oa =
{ IFSCOPE_NONE, { 0 }, IP6OAF_SELECT_SRCIF };
struct flowadv *adv = &ip6oa.ip6oa_flowadv;
int flowadv = 0;
flowadv = (in6p->inp_socket->so_state & SS_ISCONNECTED) ? 1 : 0;
if (flowadv && INP_WAIT_FOR_IF_FEEDBACK(in6p)) {
error = ENOBUFS;
goto release;
}
if (in6p->inp_flags & INP_BOUND_IF) {
ip6oa.ip6oa_boundif = in6p->inp_boundifp->if_index;
ip6oa.ip6oa_flags |= IP6OAF_BOUND_IF;
}
if (in6p->inp_flags & INP_NO_IFT_CELLULAR)
ip6oa.ip6oa_flags |= IP6OAF_NO_CELLULAR;
if (control) {
msc = mbuf_service_class_from_control(control);
if ((error = ip6_setpktopts(control, &opt, NULL, IPPROTO_UDP)) != 0)
goto release;
optp = &opt;
} else
optp = in6p->in6p_outputopts;
if (addr6) {
struct sockaddr_in6 *sin6 =
(struct sockaddr_in6 *)(void *)addr6;
if (sin6->sin6_port == 0) {
error = EADDRNOTAVAIL;
goto release;
}
if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
error = EISCONN;
goto release;
}
tmp = *sin6;
sin6 = &tmp;
faddr = &sin6->sin6_addr;
fport = sin6->sin6_port;
if (IN6_IS_ADDR_V4MAPPED(faddr)) {
if ((in6p->in6p_flags & IN6P_IPV6_V6ONLY)) {
error = EINVAL;
goto release;
} else
af = AF_INET;
}
if (in6_embedscope(&sin6->sin6_addr, sin6, in6p, NULL,
optp) != 0) {
error = EINVAL;
goto release;
}
if (!IN6_IS_ADDR_V4MAPPED(faddr)) {
laddr = in6_selectsrc(sin6, optp,
in6p, &in6p->in6p_route, NULL, &storage,
ip6oa.ip6oa_boundif, &error);
} else
laddr = &in6p->in6p_laddr;
if (laddr == NULL) {
if (error == 0)
error = EADDRNOTAVAIL;
goto release;
}
if (in6p->in6p_lport == 0 &&
(error = in6_pcbsetport(laddr, in6p, p, 0)) != 0)
goto release;
} else {
if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
error = ENOTCONN;
goto release;
}
if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_faddr)) {
if ((in6p->in6p_flags & IN6P_IPV6_V6ONLY)) {
log(LOG_INFO, "udp6_output: IPV6_V6ONLY "
"option was set for a connected socket\n");
error = EINVAL;
goto release;
} else
af = AF_INET;
}
laddr = &in6p->in6p_laddr;
faddr = &in6p->in6p_faddr;
fport = in6p->in6p_fport;
}
if (in6p->inp_flowhash == 0)
in6p->inp_flowhash = inp_calc_flowhash(in6p);
if (af == AF_INET)
hlen = sizeof(struct ip);
M_PREPEND(m, hlen + sizeof(struct udphdr), M_DONTWAIT);
if (m == 0) {
error = ENOBUFS;
goto release;
}
udp6 = (struct udphdr *)(void *)(mtod(m, caddr_t) + hlen);
udp6->uh_sport = in6p->in6p_lport;
udp6->uh_dport = fport;
if (plen <= 0xffff)
udp6->uh_ulen = htons((u_short)plen);
else
udp6->uh_ulen = 0;
udp6->uh_sum = 0;
switch (af) {
case AF_INET6:
ip6 = mtod(m, struct ip6_hdr *);
ip6->ip6_flow = in6p->in6p_flowinfo & IPV6_FLOWINFO_MASK;
ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
ip6->ip6_vfc |= IPV6_VERSION;
#if 0
ip6->ip6_plen = htons((u_short)plen);
#endif
ip6->ip6_nxt = IPPROTO_UDP;
ip6->ip6_hlim = in6_selecthlim(in6p,
in6p->in6p_route.ro_rt ?
in6p->in6p_route.ro_rt->rt_ifp : NULL);
ip6->ip6_src = *laddr;
ip6->ip6_dst = *faddr;
udp6->uh_sum = in6_cksum_phdr(laddr, faddr,
htonl(plen), htonl(IPPROTO_UDP));
m->m_pkthdr.csum_flags = CSUM_UDPIPV6;
m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
if (!IN6_IS_ADDR_UNSPECIFIED(laddr))
ip6oa.ip6oa_flags |= IP6OAF_BOUND_SRCADDR;
flags = IPV6_OUTARGS;
udp6stat.udp6s_opackets++;
#ifdef IPSEC
if (ipsec_bypass == 0 && ipsec_setsocket(m, in6p->in6p_socket) != 0) {
error = ENOBUFS;
goto release;
}
#endif
m->m_pkthdr.socket_id = get_socket_id(in6p->in6p_socket);
set_packet_service_class(m, in6p->in6p_socket, msc, PKT_SCF_IPV6);
m->m_pkthdr.m_flowhash = in6p->inp_flowhash;
m->m_pkthdr.m_fhflags |= PF_TAG_FLOWHASH;
if (flowadv)
m->m_pkthdr.m_fhflags |= PF_TAG_FLOWADV;
im6o = in6p->in6p_moptions;
if (im6o != NULL) {
IM6O_LOCK(im6o);
IM6O_ADDREF_LOCKED(im6o);
if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) &&
im6o->im6o_multicast_ifp != NULL) {
in6p->in6p_last_outifp = im6o->im6o_multicast_ifp;
}
IM6O_UNLOCK(im6o);
}
in6p->inp_sndinprog_cnt++;
error = ip6_output(m, optp, &in6p->in6p_route,
flags, im6o, NULL, &ip6oa);
if (im6o != NULL)
IM6O_REMREF(im6o);
if (error == 0 && nstat_collect) {
locked_add_64(&in6p->inp_stat->txpackets, 1);
locked_add_64(&in6p->inp_stat->txbytes, ulen);
}
if (flowadv && (adv->code == FADV_FLOW_CONTROLLED ||
adv->code == FADV_SUSPENDED)) {
error = ENOBUFS;
inp_set_fc_state(in6p, adv->code);
}
VERIFY(in6p->inp_sndinprog_cnt > 0);
if ( --in6p->inp_sndinprog_cnt == 0)
in6p->inp_flags &= ~(INP_FC_FEEDBACK);
if (in6p->in6p_route.ro_rt != NULL) {
struct rtentry *rt = in6p->in6p_route.ro_rt;
struct ifnet *outif;
if ((rt->rt_flags & RTF_MULTICAST) ||
in6p->in6p_socket == NULL ||
!(in6p->in6p_socket->so_state & SS_ISCONNECTED)) {
rt = NULL;
}
if (rt == NULL) {
rtfree(in6p->in6p_route.ro_rt);
in6p->in6p_route.ro_rt = NULL;
}
if (rt != NULL &&
(outif = rt->rt_ifp) != in6p->in6p_last_outifp)
in6p->in6p_last_outifp = outif;
}
break;
case AF_INET:
error = EAFNOSUPPORT;
goto release;
}
goto releaseopt;
release:
m_freem(m);
releaseopt:
if (control) {
if (optp == &opt)
ip6_clearpktopts(optp, -1);
m_freem(control);
}
return(error);
}