static void tcp6_usr_connect(netmsg_t msg) { struct socket *so = msg->connect.base.nm_so; struct sockaddr *nam = msg->connect.nm_nam; struct thread *td = msg->connect.nm_td; int error = 0; struct inpcb *inp; struct tcpcb *tp; struct sockaddr_in6 *sin6p; COMMON_START(so, inp, 0); /* * Must disallow TCP ``connections'' to multicast addresses. */ sin6p = (struct sockaddr_in6 *)nam; if (sin6p->sin6_family == AF_INET6 && IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr)) { error = EAFNOSUPPORT; goto out; } if (!prison_remote_ip(td, nam)) { error = EAFNOSUPPORT; /* IPv4 only jail */ goto out; } if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) { struct sockaddr_in *sinp; if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) { error = EINVAL; goto out; } sinp = kmalloc(sizeof(*sinp), M_LWKTMSG, M_INTWAIT); in6_sin6_2_sin(sinp, sin6p); inp->inp_vflag |= INP_IPV4; inp->inp_vflag &= ~INP_IPV6; msg->connect.nm_nam = (struct sockaddr *)sinp; msg->connect.nm_reconnect |= NMSG_RECONNECT_NAMALLOC; tcp_connect(msg); /* msg is invalid now */ return; } inp->inp_vflag &= ~INP_IPV4; inp->inp_vflag |= INP_IPV6; inp->inp_inc.inc_isipv6 = 1; msg->connect.nm_reconnect |= NMSG_RECONNECT_FALLBACK; tcp6_connect(msg); /* msg is invalid now */ return; out: if (msg->connect.nm_m) { m_freem(msg->connect.nm_m); msg->connect.nm_m = NULL; } lwkt_replymsg(&msg->lmsg, error); }
/* * Initiate connection to peer. * Create a template for use in transmissions on this connection. * Enter SYN_SENT state, and mark socket as connecting. * Start keep-alive timer, and seed output sequence space. * Send initial segment on connection. */ static int tcp_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td) { int error = 0; struct inpcb *inp; struct tcpcb *tp = NULL; struct sockaddr_in *sinp; //printf("tcp_usr_connect: called \n"); sinp = (struct sockaddr_in *)nam; if (nam->sa_len != sizeof (*sinp)) return (EINVAL); //printf("tcp_usr_connect: called family=%d\n", sinp->sin_family); /* * Must disallow TCP ``connections'' to multicast addresses. */ if (sinp->sin_family == AF_INET && IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) return (EAFNOSUPPORT); //printf("tcp_usr_connect: called 3\n"); #ifdef MAXHE_TODO if (jailed(td->td_ucred)) prison_remote_ip(td->td_ucred, 0, &sinp->sin_addr.s_addr); #endif // MAXHE_TODO TCPDEBUG0; INP_INFO_WLOCK(&tcbinfo); inp = sotoinpcb(so); KASSERT(inp != NULL, ("tcp_usr_connect: inp == NULL")); INP_LOCK(inp); #ifdef MAXHE_TODO if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) { error = EINVAL; //printf("tcp_usr_connect: error = EINVAL\n"); goto out; } #endif // MAXHE_TODO tp = intotcpcb(inp); TCPDEBUG1(); //printf("tcp_usr_connect: calling tcp_connect\n"); if ((error = tcp_connect(tp, nam, td)) != 0) goto out; error = tcp_output(tp); out: //printf("tcp_usr_connect: return error=%d\n", error); TCPDEBUG2(PRU_CONNECT); INP_UNLOCK(inp); INP_INFO_WUNLOCK(&tcbinfo); return (error); }
static void udp6_connect(netmsg_t msg) { struct socket *so = msg->connect.base.nm_so; struct sockaddr *nam = msg->connect.nm_nam; struct thread *td = msg->connect.nm_td; struct sockaddr_in6 *sin6_p; struct inpcb *inp; int error; inp = so->so_pcb; if (inp == NULL) { error = EINVAL; goto out; } sin6_p = (struct sockaddr_in6 *)nam; if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) { error = EADDRNOTAVAIL; goto out; } if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) { error = EISCONN; goto out; } if (inp->inp_flags & INP_WILDCARD) in_pcbremwildcardhash(inp); if (!prison_remote_ip(td, nam)) { error = EAFNOSUPPORT; /* IPv4 only jail */ goto out; } error = in6_pcbconnect(inp, nam, td); if (error == 0) { soisconnected(so); } else if (error == EAFNOSUPPORT) { /* connection dissolved */ /* * Follow traditional BSD behavior and retain * the local port binding. But, fix the old misbehavior * of overwriting any previously bound local address. */ if (!(inp->inp_flags & INP_WASBOUND_NOTANY)) inp->in6p_laddr = kin6addr_any; in_pcbinswildcardhash(inp); } out: lwkt_replymsg(&msg->connect.base.lmsg, error); }
static void tcp6_usr_connect(netmsg_t msg) { struct socket *so = msg->connect.base.nm_so; struct sockaddr *nam = msg->connect.nm_nam; struct thread *td = msg->connect.nm_td; int error = 0; struct inpcb *inp; struct tcpcb *tp; struct sockaddr_in6 *sin6p; COMMON_START(so, inp, 0); /* * Must disallow TCP ``connections'' to multicast addresses. */ sin6p = (struct sockaddr_in6 *)nam; if (sin6p->sin6_family == AF_INET6 && IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr)) { error = EAFNOSUPPORT; goto out; } if (!prison_remote_ip(td, nam)) { error = EAFNOSUPPORT; /* IPv4 only jail */ goto out; } /* Reject v4-mapped address */ if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) { error = EADDRNOTAVAIL; goto out; } inp->inp_inc.inc_isipv6 = 1; tcp6_connect(msg); /* msg is invalid now */ return; out: if (msg->connect.nm_m) { m_freem(msg->connect.nm_m); msg->connect.nm_m = NULL; } lwkt_replymsg(&msg->lmsg, error); }
/* * Initiate connection to peer. * Create a template for use in transmissions on this connection. * Enter SYN_SENT state, and mark socket as connecting. * Start keep-alive timer, and seed output sequence space. * Send initial segment on connection. */ static void tcp_usr_connect(netmsg_t msg) { struct socket *so = msg->connect.base.nm_so; struct sockaddr *nam = msg->connect.nm_nam; struct thread *td = msg->connect.nm_td; int error = 0; struct inpcb *inp; struct tcpcb *tp; struct sockaddr_in *sinp; COMMON_START(so, inp, 0); /* * Must disallow TCP ``connections'' to multicast addresses. */ sinp = (struct sockaddr_in *)nam; if (sinp->sin_family == AF_INET && IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) { error = EAFNOSUPPORT; goto out; } if (!prison_remote_ip(td, (struct sockaddr*)sinp)) { error = EAFNOSUPPORT; /* IPv6 only jail */ goto out; } tcp_connect(msg); /* msg is invalid now */ return; out: if (msg->connect.nm_m) { m_freem(msg->connect.nm_m); msg->connect.nm_m = NULL; } if (msg->connect.nm_flags & PRUC_HELDTD) lwkt_rele(td); if (error && (msg->connect.nm_flags & PRUC_ASYNC)) { so->so_error = error; soisdisconnected(so); } lwkt_replymsg(&msg->lmsg, error); }
int udp6_output(struct in6pcb *in6p, struct mbuf *m, struct sockaddr *addr6, struct mbuf *control, struct thread *td) { 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, *stickyopt = in6p->in6p_outputopts; int priv; int af = AF_INET6, hlen = sizeof(struct ip6_hdr); int flags; struct sockaddr_in6 tmp; priv = !priv_check(td, PRIV_ROOT); /* 1 if privileged, 0 if not */ if (control) { if ((error = ip6_setpktoptions(control, &opt, in6p->in6p_outputopts, IPPROTO_UDP, priv)) != 0) goto release; in6p->in6p_outputopts = &opt; } if (addr6) { /* * IPv4 version of udp_output calls in_pcbconnect in this case, * which needs splnet and affects performance. * Since we saw no essential reason for calling in_pcbconnect, * we get rid of such kind of logic, and call in6_selectsrc * and in6_pcbsetport in order to fill in the local address * and the local port. */ struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)addr6; if (sin6->sin6_port == 0) { error = EADDRNOTAVAIL; goto release; } if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) { /* how about ::ffff:0.0.0.0 case? */ error = EISCONN; goto release; } if (!prison_remote_ip(td, addr6)) { error = EAFNOSUPPORT; /* IPv4 only jail */ goto release; } /* protect *sin6 from overwrites */ tmp = *sin6; sin6 = &tmp; faddr = &sin6->sin6_addr; fport = sin6->sin6_port; /* allow 0 port */ if (IN6_IS_ADDR_V4MAPPED(faddr)) { if ((in6p->in6p_flags & IN6P_IPV6_V6ONLY)) { /* * I believe we should explicitly discard the * packet when mapped addresses are disabled, * rather than send the packet as an IPv6 one. * If we chose the latter approach, the packet * might be sent out on the wire based on the * default route, the situation which we'd * probably want to avoid. * (20010421 [email protected]) */ error = EINVAL; goto release; } else af = AF_INET; } /* KAME hack: embed scopeid */ if (in6_embedscope(&sin6->sin6_addr, sin6, in6p, NULL) != 0) { error = EINVAL; goto release; } if (!IN6_IS_ADDR_V4MAPPED(faddr)) { laddr = in6_selectsrc(sin6, in6p->in6p_outputopts, in6p->in6p_moptions, &in6p->in6p_route, &in6p->in6p_laddr, &error, NULL); } else laddr = &in6p->in6p_laddr; /* XXX */ if (laddr == NULL) { if (error == 0) error = EADDRNOTAVAIL; goto release; } if (in6p->in6p_lport == 0 && (error = in6_pcbsetport(laddr, in6p, td)) != 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)) { /* * XXX: this case would happen when the * application sets the V6ONLY flag after * connecting the foreign address. * Such applications should be fixed, * so we bark here. */ 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 (af == AF_INET) hlen = sizeof(struct ip); /* * Calculate data length and get a mbuf * for UDP and IP6 headers. */ M_PREPEND(m, hlen + sizeof(struct udphdr), MB_DONTWAIT); if (m == NULL) { error = ENOBUFS; goto release; } /* * Stuff checksum and output datagram. */ udp6 = (struct udphdr *)(mtod(m, caddr_t) + hlen); udp6->uh_sport = in6p->in6p_lport; /* lport is always set in the PCB */ 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_plen will be filled in ip6_output. */ 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; if ((udp6->uh_sum = in6_cksum(m, IPPROTO_UDP, sizeof(struct ip6_hdr), plen)) == 0) { udp6->uh_sum = 0xffff; } flags = 0; udp6stat.udp6s_opackets++; error = ip6_output(m, in6p->in6p_outputopts, &in6p->in6p_route, flags, in6p->in6p_moptions, NULL, in6p); break; case AF_INET: error = EAFNOSUPPORT; goto release; } goto releaseopt; release: m_freem(m); releaseopt: if (control) { ip6_clearpktopts(in6p->in6p_outputopts, -1); in6p->in6p_outputopts = stickyopt; m_freem(control); } return (error); }
static void udp_send(netmsg_t msg) { struct socket *so = msg->send.base.nm_so; struct mbuf *m = msg->send.nm_m; struct sockaddr *dstaddr = msg->send.nm_addr; int pru_flags = msg->send.nm_flags; struct inpcb *inp = so->so_pcb; struct thread *td = msg->send.nm_td; int flags; struct udpiphdr *ui; int len = m->m_pkthdr.len; struct sockaddr_in *sin; /* really is initialized before use */ int error = 0, cpu; KKASSERT(msg->send.nm_control == NULL); logudp(send_beg, inp); if (inp == NULL) { error = EINVAL; goto release; } if (len + sizeof(struct udpiphdr) > IP_MAXPACKET) { error = EMSGSIZE; goto release; } if (inp->inp_lport == 0) { /* unbound socket */ boolean_t forwarded; error = in_pcbbind(inp, NULL, td); if (error) goto release; /* * Need to call udp_send again, after this inpcb is * inserted into wildcard hash table. */ msg->send.base.lmsg.ms_flags |= MSGF_UDP_SEND; forwarded = udp_inswildcardhash(inp, &msg->send.base, 0); if (forwarded) { /* * The message is further forwarded, so we are * done here. */ logudp(send_inswildcard, inp); return; } } if (dstaddr != NULL) { /* destination address specified */ if (inp->inp_faddr.s_addr != INADDR_ANY) { /* already connected */ error = EISCONN; goto release; } sin = (struct sockaddr_in *)dstaddr; if (!prison_remote_ip(td, (struct sockaddr *)&sin)) { error = EAFNOSUPPORT; /* IPv6 only jail */ goto release; } } else { if (inp->inp_faddr.s_addr == INADDR_ANY) { /* no destination specified and not already connected */ error = ENOTCONN; goto release; } sin = NULL; } /* * Calculate data length and get a mbuf * for UDP and IP headers. */ M_PREPEND(m, sizeof(struct udpiphdr), M_NOWAIT); if (m == NULL) { error = ENOBUFS; goto release; } /* * Fill in mbuf with extended UDP header * and addresses and length put into network format. */ ui = mtod(m, struct udpiphdr *); bzero(ui->ui_x1, sizeof ui->ui_x1); /* XXX still needed? */ ui->ui_pr = IPPROTO_UDP; /* * Set destination address. */ if (dstaddr != NULL) { /* use specified destination */ ui->ui_dst = sin->sin_addr; ui->ui_dport = sin->sin_port; } else { /* use connected destination */ ui->ui_dst = inp->inp_faddr; ui->ui_dport = inp->inp_fport; } /* * Set source address. */ if (inp->inp_laddr.s_addr == INADDR_ANY || IN_MULTICAST(ntohl(inp->inp_laddr.s_addr))) { struct sockaddr_in *if_sin; if (dstaddr == NULL) { /* * connect() had (or should have) failed because * the interface had no IP address, but the * application proceeded to call send() anyways. */ error = ENOTCONN; goto release; } /* Look up outgoing interface. */ error = in_pcbladdr_find(inp, dstaddr, &if_sin, td, 1); if (error) goto release; ui->ui_src = if_sin->sin_addr; /* use address of interface */ } else { ui->ui_src = inp->inp_laddr; /* use non-null bound address */ } ui->ui_sport = inp->inp_lport; KASSERT(inp->inp_lport != 0, ("inp lport should have been bound")); /* * Release the original thread, since it is no longer used */ if (pru_flags & PRUS_HELDTD) { lwkt_rele(td); pru_flags &= ~PRUS_HELDTD; } /* * Free the dest address, since it is no longer needed */ if (pru_flags & PRUS_FREEADDR) { kfree(dstaddr, M_SONAME); pru_flags &= ~PRUS_FREEADDR; } ui->ui_ulen = htons((u_short)len + sizeof(struct udphdr)); /* * Set up checksum and output datagram. */ if (udpcksum) { ui->ui_sum = in_pseudo(ui->ui_src.s_addr, ui->ui_dst.s_addr, htons((u_short)len + sizeof(struct udphdr) + IPPROTO_UDP)); m->m_pkthdr.csum_flags = CSUM_UDP; m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum); m->m_pkthdr.csum_thlen = sizeof(struct udphdr); } else {
int udp6_output(struct in6pcb *in6p, struct mbuf *m, struct sockaddr *addr6, struct mbuf *control, struct thread *td) { 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, *stickyopt = in6p->in6p_outputopts; int priv; int hlen = sizeof(struct ip6_hdr); struct sockaddr_in6 tmp; priv = !priv_check(td, PRIV_ROOT); /* 1 if privileged, 0 if not */ if (control) { if ((error = ip6_setpktoptions(control, &opt, in6p->in6p_outputopts, IPPROTO_UDP, priv)) != 0) goto release; in6p->in6p_outputopts = &opt; } if (addr6) { /* * IPv4 version of udp_output calls in_pcbconnect in this case, * which needs splnet and affects performance. * Since we saw no essential reason for calling in_pcbconnect, * we get rid of such kind of logic, and call in6_selectsrc * and in6_pcbsetlport in order to fill in the local address * and the local port. */ struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)addr6; /* Caller should have rejected the v4-mapped address */ KASSERT(!IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr), ("v4-mapped address")); if (sin6->sin6_port == 0) { error = EADDRNOTAVAIL; goto release; } if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) { /* how about ::ffff:0.0.0.0 case? */ error = EISCONN; goto release; } if (!prison_remote_ip(td, addr6)) { error = EAFNOSUPPORT; /* IPv4 only jail */ goto release; } /* protect *sin6 from overwrites */ tmp = *sin6; sin6 = &tmp; faddr = &sin6->sin6_addr; fport = sin6->sin6_port; /* allow 0 port */ /* KAME hack: embed scopeid */ if (in6_embedscope(&sin6->sin6_addr, sin6, in6p, NULL) != 0) { error = EINVAL; goto release; } laddr = in6_selectsrc(sin6, in6p->in6p_outputopts, in6p->in6p_moptions, &in6p->in6p_route, &in6p->in6p_laddr, &error, NULL); if (laddr == NULL) { if (error == 0) error = EADDRNOTAVAIL; goto release; } if (in6p->in6p_lport == 0 && (error = in6_pcbsetlport(laddr, in6p, td)) != 0) goto release; } else { if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) { error = ENOTCONN; goto release; } /* Connection to v4-mapped address should have been rejected */ KASSERT(!IN6_IS_ADDR_V4MAPPED(&in6p->in6p_faddr), ("bound to v4-mapped address")); laddr = &in6p->in6p_laddr; faddr = &in6p->in6p_faddr; fport = in6p->in6p_fport; } /* * Calculate data length and get a mbuf * for UDP and IP6 headers. */ M_PREPEND(m, hlen + sizeof(struct udphdr), M_NOWAIT); if (m == NULL) { error = ENOBUFS; goto release; } /* * Stuff checksum and output datagram. */ udp6 = (struct udphdr *)(mtod(m, caddr_t) + hlen); udp6->uh_sport = in6p->in6p_lport; /* lport is always set in the PCB */ udp6->uh_dport = fport; if (plen <= 0xffff) udp6->uh_ulen = htons((u_short)plen); else udp6->uh_ulen = 0; udp6->uh_sum = 0; 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_plen will be filled in ip6_output. */ 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; if ((udp6->uh_sum = in6_cksum(m, IPPROTO_UDP, sizeof(struct ip6_hdr), plen)) == 0) { udp6->uh_sum = 0xffff; } udp6stat.udp6s_opackets++; error = ip6_output(m, in6p->in6p_outputopts, &in6p->in6p_route, 0, in6p->in6p_moptions, NULL, in6p); goto releaseopt; release: m_freem(m); releaseopt: if (control) { ip6_clearpktopts(in6p->in6p_outputopts, -1); in6p->in6p_outputopts = stickyopt; m_freem(control); } return (error); }