1/* $OpenBSD: udp6_output.c,v 1.65 2024/04/17 20:48:51 bluhm Exp $ */ 2/* $KAME: udp6_output.c,v 1.21 2001/02/07 11:51:54 itojun Exp $ */ 3 4/* 5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. Neither the name of the project nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 */ 32 33/* 34 * Copyright (c) 1982, 1986, 1989, 1993 35 * The Regents of the University of California. All rights reserved. 36 * 37 * Redistribution and use in source and binary forms, with or without 38 * modification, are permitted provided that the following conditions 39 * are met: 40 * 1. Redistributions of source code must retain the above copyright 41 * notice, this list of conditions and the following disclaimer. 42 * 2. Redistributions in binary form must reproduce the above copyright 43 * notice, this list of conditions and the following disclaimer in the 44 * documentation and/or other materials provided with the distribution. 45 * 3. Neither the name of the University nor the names of its contributors 46 * may be used to endorse or promote products derived from this software 47 * without specific prior written permission. 48 * 49 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 50 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 52 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 59 * SUCH DAMAGE. 60 */ 61 62#include "pf.h" 63 64#include <sys/param.h> 65#include <sys/mbuf.h> 66#include <sys/socket.h> 67#include <sys/socketvar.h> 68#include <sys/errno.h> 69#include <sys/stat.h> 70#include <sys/systm.h> 71#include <sys/syslog.h> 72 73#include <net/if.h> 74#include <net/if_var.h> 75#include <net/route.h> 76#if NPF > 0 77#include <net/pfvar.h> 78#endif 79 80#include <netinet/in.h> 81#include <netinet6/in6_var.h> 82#include <netinet/ip.h> 83#include <netinet/ip_var.h> 84#include <netinet/in_pcb.h> 85#include <netinet/udp.h> 86#include <netinet/udp_var.h> 87#include <netinet/ip6.h> 88#include <netinet6/ip6_var.h> 89#include <netinet/icmp6.h> 90 91/* 92 * UDP protocol implementation. 93 * Per RFC 768, August, 1980. 94 */ 95int 96udp6_output(struct inpcb *inp, struct mbuf *m, struct mbuf *addr6, 97 struct mbuf *control) 98{ 99 u_int32_t ulen = m->m_pkthdr.len; 100 u_int32_t plen = sizeof(struct udphdr) + ulen; 101 int error = 0, priv = 0, hlen, flags; 102 struct ip6_hdr *ip6; 103 struct udphdr *udp6; 104 const struct in6_addr *laddr, *faddr; 105 struct ip6_pktopts *optp, opt; 106 struct sockaddr_in6 tmp, valid; 107 struct proc *p = curproc; /* XXX */ 108 u_short fport; 109 110 if ((inp->inp_socket->so_state & SS_PRIV) != 0) 111 priv = 1; 112 if (control) { 113 if ((error = ip6_setpktopts(control, &opt, 114 inp->inp_outputopts6, priv, IPPROTO_UDP)) != 0) 115 goto release; 116 optp = &opt; 117 } else 118 optp = inp->inp_outputopts6; 119 120 if (addr6) { 121 struct sockaddr_in6 *sin6; 122 123 if ((error = in6_nam2sin6(addr6, &sin6))) 124 goto release; 125 if (sin6->sin6_port == 0) { 126 error = EADDRNOTAVAIL; 127 goto release; 128 } 129 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { 130 error = EADDRNOTAVAIL; 131 goto release; 132 } 133 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->inp_faddr6)) { 134 error = EISCONN; 135 goto release; 136 } 137 138 /* protect *sin6 from overwrites */ 139 tmp = *sin6; 140 sin6 = &tmp; 141 142 faddr = &sin6->sin6_addr; 143 fport = sin6->sin6_port; /* allow 0 port */ 144 145 /* KAME hack: embed scopeid */ 146 if (in6_embedscope(&sin6->sin6_addr, sin6, 147 inp->inp_outputopts6, inp->inp_moptions6) != 0) { 148 error = EINVAL; 149 goto release; 150 } 151 152 error = in6_pcbselsrc(&laddr, sin6, inp, optp); 153 if (error) 154 goto release; 155 156 if (inp->inp_lport == 0){ 157 error = in_pcbbind(inp, NULL, p); 158 if (error) 159 goto release; 160 } 161 162 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->inp_laddr6) && 163 !IN6_ARE_ADDR_EQUAL(&inp->inp_laddr6, laddr)) { 164 valid.sin6_addr = *laddr; 165 valid.sin6_port = inp->inp_lport; 166 valid.sin6_scope_id = 0; 167 valid.sin6_family = AF_INET6; 168 valid.sin6_len = sizeof(valid); 169 error = in6_pcbaddrisavail(inp, &valid, 0, p); 170 if (error) 171 goto release; 172 } 173 } else { 174 if (IN6_IS_ADDR_UNSPECIFIED(&inp->inp_faddr6)) { 175 error = ENOTCONN; 176 goto release; 177 } 178 laddr = &inp->inp_laddr6; 179 faddr = &inp->inp_faddr6; 180 fport = inp->inp_fport; 181 } 182 183 hlen = sizeof(struct ip6_hdr); 184 185 /* 186 * Calculate data length and get a mbuf 187 * for UDP and IP6 headers. 188 */ 189 M_PREPEND(m, hlen + sizeof(struct udphdr), M_DONTWAIT); 190 if (m == NULL) { 191 error = ENOBUFS; 192 goto releaseopt; 193 } 194 195 /* 196 * Stuff checksum and output datagram. 197 */ 198 udp6 = (struct udphdr *)(mtod(m, caddr_t) + hlen); 199 udp6->uh_sport = inp->inp_lport; /* lport is always set in the PCB */ 200 udp6->uh_dport = fport; 201 if (plen <= 0xffff) 202 udp6->uh_ulen = htons((u_short)plen); 203 else 204 udp6->uh_ulen = 0; 205 udp6->uh_sum = 0; 206 207 ip6 = mtod(m, struct ip6_hdr *); 208 ip6->ip6_flow = inp->inp_flowinfo & IPV6_FLOWINFO_MASK; 209 ip6->ip6_vfc &= ~IPV6_VERSION_MASK; 210 ip6->ip6_vfc |= IPV6_VERSION; 211#if 0 /* ip6_plen will be filled in ip6_output. */ 212 ip6->ip6_plen = htons((u_short)plen); 213#endif 214 ip6->ip6_nxt = IPPROTO_UDP; 215 ip6->ip6_hlim = in6_selecthlim(inp); 216 ip6->ip6_src = *laddr; 217 ip6->ip6_dst = *faddr; 218 219 m->m_pkthdr.csum_flags |= M_UDP_CSUM_OUT; 220 221 flags = 0; 222 if (inp->inp_flags & IN6P_MINMTU) 223 flags |= IPV6_MINMTU; 224 225 udpstat_inc(udps_opackets); 226 227 /* force routing table */ 228 m->m_pkthdr.ph_rtableid = inp->inp_rtableid; 229 230#if NPF > 0 231 if (inp->inp_socket->so_state & SS_ISCONNECTED) 232 pf_mbuf_link_inpcb(m, inp); 233#endif 234 235 error = ip6_output(m, optp, &inp->inp_route, 236 flags, inp->inp_moptions6, &inp->inp_seclevel); 237 goto releaseopt; 238 239release: 240 m_freem(m); 241 242releaseopt: 243 if (control) { 244 ip6_clearpktopts(&opt, -1); 245 m_freem(control); 246 } 247 return (error); 248} 249