1 /*-
2 * SPDX-License-Identifier: BSD-3-Clause
3 *
4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5 * Copyright (c) 2010-2011 Juniper Networks, Inc.
6 * Copyright (c) 2014 Kevin Lo
7 * All rights reserved.
8 *
9 * Portions of this software were developed by Robert N. M. Watson under
10 * contract to Juniper Networks, Inc.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of the project nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 *
36 * $KAME: udp6_usrreq.c,v 1.27 2001/05/21 05:45:10 jinmei Exp $
37 * $KAME: udp6_output.c,v 1.31 2001/05/21 16:39:15 jinmei Exp $
38 */
39
40 /*-
41 * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995
42 * The Regents of the University of California.
43 * All rights reserved.
44 *
45 * Redistribution and use in source and binary forms, with or without
46 * modification, are permitted provided that the following conditions
47 * are met:
48 * 1. Redistributions of source code must retain the above copyright
49 * notice, this list of conditions and the following disclaimer.
50 * 2. Redistributions in binary form must reproduce the above copyright
51 * notice, this list of conditions and the following disclaimer in the
52 * documentation and/or other materials provided with the distribution.
53 * 3. Neither the name of the University nor the names of its contributors
54 * may be used to endorse or promote products derived from this software
55 * without specific prior written permission.
56 *
57 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
58 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
59 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
60 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
61 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
62 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
63 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
64 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
65 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
66 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
67 * SUCH DAMAGE.
68 */
69
70 #include "opt_inet.h"
71 #include "opt_inet6.h"
72 #include "opt_ipsec.h"
73 #include "opt_route.h"
74 #include "opt_rss.h"
75
76 #include <sys/param.h>
77 #include <sys/domain.h>
78 #include <sys/jail.h>
79 #include <sys/kernel.h>
80 #include <sys/lock.h>
81 #include <sys/mbuf.h>
82 #include <sys/priv.h>
83 #include <sys/proc.h>
84 #include <sys/protosw.h>
85 #include <sys/sdt.h>
86 #include <sys/signalvar.h>
87 #include <sys/socket.h>
88 #include <sys/socketvar.h>
89 #include <sys/sx.h>
90 #include <sys/sysctl.h>
91 #include <sys/syslog.h>
92 #include <sys/systm.h>
93
94 #include <net/if.h>
95 #include <net/if_var.h>
96 #include <net/if_types.h>
97 #include <net/route.h>
98 #include <net/rss_config.h>
99
100 #include <netinet/in.h>
101 #include <netinet/in_kdtrace.h>
102 #include <netinet/in_pcb.h>
103 #include <netinet/in_systm.h>
104 #include <netinet/in_var.h>
105 #include <netinet/ip.h>
106 #include <netinet/ip6.h>
107 #include <netinet/icmp6.h>
108 #include <netinet/ip_var.h>
109 #include <netinet/udp.h>
110 #include <netinet/udp_var.h>
111 #include <netinet/udplite.h>
112
113 #include <netinet6/ip6_var.h>
114 #include <netinet6/in6_fib.h>
115 #include <netinet6/in6_pcb.h>
116 #include <netinet6/in6_rss.h>
117 #include <netinet6/udp6_var.h>
118 #include <netinet6/scope6_var.h>
119
120 #include <netipsec/ipsec_support.h>
121
122 #include <security/mac/mac_framework.h>
123
124 VNET_DEFINE(int, zero_checksum_port) = 0;
125 #define V_zero_checksum_port VNET(zero_checksum_port)
126 SYSCTL_INT(_net_inet6_udp6, OID_AUTO, rfc6935_port, CTLFLAG_VNET | CTLFLAG_RW,
127 &VNET_NAME(zero_checksum_port), 0,
128 "Zero UDP checksum allowed for traffic to/from this port.");
129
130 /*
131 * UDP protocol implementation.
132 * Per RFC 768, August, 1980.
133 */
134
135 static void udp6_detach(struct socket *so);
136
137 static int
udp6_append(struct inpcb * inp,struct mbuf * n,int off,struct sockaddr_in6 * fromsa)138 udp6_append(struct inpcb *inp, struct mbuf *n, int off,
139 struct sockaddr_in6 *fromsa)
140 {
141 struct socket *so;
142 struct mbuf *opts = NULL, *tmp_opts;
143 struct udpcb *up;
144
145 INP_LOCK_ASSERT(inp);
146
147 /*
148 * Engage the tunneling protocol.
149 */
150 up = intoudpcb(inp);
151 if (up->u_tun_func != NULL) {
152 bool filtered;
153
154 in_pcbref(inp);
155 INP_RUNLOCK(inp);
156 filtered = (*up->u_tun_func)(n, off, inp,
157 (struct sockaddr *)&fromsa[0], up->u_tun_ctx);
158 INP_RLOCK(inp);
159 if (in_pcbrele_rlocked(inp))
160 return (1);
161 if (filtered) {
162 INP_RUNLOCK(inp);
163 return (1);
164 }
165 }
166
167 off += sizeof(struct udphdr);
168
169 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
170 /* Check AH/ESP integrity. */
171 if (IPSEC_ENABLED(ipv6)) {
172 if (IPSEC_CHECK_POLICY(ipv6, n, inp) != 0) {
173 m_freem(n);
174 return (0);
175 }
176
177 /* IPSec UDP encaps. */
178 if ((up->u_flags & UF_ESPINUDP) != 0 &&
179 UDPENCAP_INPUT(ipv6, n, off, AF_INET6) != 0) {
180 return (0); /* Consumed. */
181 }
182 }
183 #endif /* IPSEC */
184 #ifdef MAC
185 if (mac_inpcb_check_deliver(inp, n) != 0) {
186 m_freem(n);
187 return (0);
188 }
189 #endif
190 opts = NULL;
191 if (inp->inp_flags & INP_CONTROLOPTS ||
192 inp->inp_socket->so_options & SO_TIMESTAMP)
193 ip6_savecontrol(inp, n, &opts);
194 if ((inp->inp_vflag & INP_IPV6) && (inp->inp_flags2 & INP_ORIGDSTADDR)) {
195 tmp_opts = sbcreatecontrol(&fromsa[1],
196 sizeof(struct sockaddr_in6), IPV6_ORIGDSTADDR,
197 IPPROTO_IPV6, M_NOWAIT);
198 if (tmp_opts) {
199 if (opts) {
200 tmp_opts->m_next = opts;
201 opts = tmp_opts;
202 } else
203 opts = tmp_opts;
204 }
205 }
206 m_adj(n, off);
207
208 so = inp->inp_socket;
209 SOCKBUF_LOCK(&so->so_rcv);
210 if (sbappendaddr_locked(&so->so_rcv, (struct sockaddr *)&fromsa[0], n,
211 opts) == 0) {
212 soroverflow_locked(so);
213 m_freem(n);
214 if (opts)
215 m_freem(opts);
216 UDPSTAT_INC(udps_fullsock);
217 } else
218 sorwakeup_locked(so);
219 return (0);
220 }
221
222 struct udp6_multi_match_ctx {
223 struct ip6_hdr *ip6;
224 struct udphdr *uh;
225 };
226
227 static bool
udp6_multi_match(const struct inpcb * inp,void * v)228 udp6_multi_match(const struct inpcb *inp, void *v)
229 {
230 struct udp6_multi_match_ctx *ctx = v;
231
232 if ((inp->inp_vflag & INP_IPV6) == 0)
233 return(false);
234 if (inp->inp_lport != ctx->uh->uh_dport)
235 return(false);
236 if (inp->inp_fport != 0 && inp->inp_fport != ctx->uh->uh_sport)
237 return(false);
238 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr) &&
239 !IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, &ctx->ip6->ip6_dst))
240 return (false);
241 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) &&
242 (!IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, &ctx->ip6->ip6_src) ||
243 inp->inp_fport != ctx->uh->uh_sport))
244 return (false);
245
246 return (true);
247 }
248
249 static int
udp6_multi_input(struct mbuf * m,int off,int proto,struct sockaddr_in6 * fromsa)250 udp6_multi_input(struct mbuf *m, int off, int proto,
251 struct sockaddr_in6 *fromsa)
252 {
253 struct udp6_multi_match_ctx ctx;
254 struct inpcb_iterator inpi = INP_ITERATOR(udp_get_inpcbinfo(proto),
255 INPLOOKUP_RLOCKPCB, udp6_multi_match, &ctx);
256 struct inpcb *inp;
257 struct ip6_moptions *imo;
258 struct mbuf *n;
259 int appends = 0;
260
261 /*
262 * In the event that laddr should be set to the link-local
263 * address (this happens in RIPng), the multicast address
264 * specified in the received packet will not match laddr. To
265 * handle this situation, matching is relaxed if the
266 * receiving interface is the same as one specified in the
267 * socket and if the destination multicast address matches
268 * one of the multicast groups specified in the socket.
269 */
270
271 /*
272 * KAME note: traditionally we dropped udpiphdr from mbuf
273 * here. We need udphdr for IPsec processing so we do that
274 * later.
275 */
276 ctx.ip6 = mtod(m, struct ip6_hdr *);
277 ctx.uh = (struct udphdr *)((char *)ctx.ip6 + off);
278 while ((inp = inp_next(&inpi)) != NULL) {
279 INP_RLOCK_ASSERT(inp);
280 /*
281 * XXXRW: Because we weren't holding either the inpcb
282 * or the hash lock when we checked for a match
283 * before, we should probably recheck now that the
284 * inpcb lock is (supposed to be) held.
285 */
286 /*
287 * Handle socket delivery policy for any-source
288 * and source-specific multicast. [RFC3678]
289 */
290 if ((imo = inp->in6p_moptions) != NULL) {
291 struct sockaddr_in6 mcaddr;
292 int blocked;
293
294 bzero(&mcaddr, sizeof(struct sockaddr_in6));
295 mcaddr.sin6_len = sizeof(struct sockaddr_in6);
296 mcaddr.sin6_family = AF_INET6;
297 mcaddr.sin6_addr = ctx.ip6->ip6_dst;
298
299 blocked = im6o_mc_filter(imo, m->m_pkthdr.rcvif,
300 (struct sockaddr *)&mcaddr,
301 (struct sockaddr *)&fromsa[0]);
302 if (blocked != MCAST_PASS) {
303 if (blocked == MCAST_NOTGMEMBER)
304 IP6STAT_INC(ip6s_notmember);
305 if (blocked == MCAST_NOTSMEMBER ||
306 blocked == MCAST_MUTED)
307 UDPSTAT_INC(udps_filtermcast);
308 continue;
309 }
310 }
311 if ((n = m_copym(m, 0, M_COPYALL, M_NOWAIT)) != NULL) {
312 if (proto == IPPROTO_UDPLITE)
313 UDPLITE_PROBE(receive, NULL, inp, ctx.ip6,
314 inp, ctx.uh);
315 else
316 UDP_PROBE(receive, NULL, inp, ctx.ip6, inp,
317 ctx.uh);
318 if (udp6_append(inp, n, off, fromsa)) {
319 break;
320 } else
321 appends++;
322 }
323 /*
324 * Don't look for additional matches if this one does
325 * not have either the SO_REUSEPORT or SO_REUSEADDR
326 * socket options set. This heuristic avoids
327 * searching through all pcbs in the common case of a
328 * non-shared port. It assumes that an application
329 * will never clear these options after setting them.
330 */
331 if ((inp->inp_socket->so_options &
332 (SO_REUSEPORT|SO_REUSEPORT_LB|SO_REUSEADDR)) == 0) {
333 INP_RUNLOCK(inp);
334 break;
335 }
336 }
337 m_freem(m);
338
339 if (appends == 0) {
340 /*
341 * No matching pcb found; discard datagram. (No need
342 * to send an ICMP Port Unreachable for a broadcast
343 * or multicast datagram.)
344 */
345 UDPSTAT_INC(udps_noport);
346 UDPSTAT_INC(udps_noportmcast);
347 }
348
349 return (IPPROTO_DONE);
350 }
351
352 int
udp6_input(struct mbuf ** mp,int * offp,int proto)353 udp6_input(struct mbuf **mp, int *offp, int proto)
354 {
355 struct mbuf *m = *mp;
356 struct ip6_hdr *ip6;
357 struct udphdr *uh;
358 struct inpcb *inp;
359 struct inpcbinfo *pcbinfo;
360 struct udpcb *up;
361 int off = *offp;
362 int cscov_partial;
363 int plen, ulen;
364 int lookupflags;
365 struct sockaddr_in6 fromsa[2];
366 struct m_tag *fwd_tag;
367 uint16_t uh_sum;
368 uint8_t nxt;
369
370 NET_EPOCH_ASSERT();
371
372 if (m->m_len < off + sizeof(struct udphdr)) {
373 m = m_pullup(m, off + sizeof(struct udphdr));
374 if (m == NULL) {
375 IP6STAT_INC(ip6s_exthdrtoolong);
376 *mp = NULL;
377 return (IPPROTO_DONE);
378 }
379 }
380 ip6 = mtod(m, struct ip6_hdr *);
381 uh = (struct udphdr *)((caddr_t)ip6 + off);
382
383 UDPSTAT_INC(udps_ipackets);
384
385 /*
386 * Destination port of 0 is illegal, based on RFC768.
387 */
388 if (uh->uh_dport == 0)
389 goto badunlocked;
390
391 plen = ntohs(ip6->ip6_plen) - off + sizeof(*ip6);
392 ulen = ntohs((u_short)uh->uh_ulen);
393
394 nxt = proto;
395 cscov_partial = (nxt == IPPROTO_UDPLITE) ? 1 : 0;
396 if (nxt == IPPROTO_UDPLITE) {
397 /* Zero means checksum over the complete packet. */
398 if (ulen == 0)
399 ulen = plen;
400 if (ulen == plen)
401 cscov_partial = 0;
402 if ((ulen < sizeof(struct udphdr)) || (ulen > plen)) {
403 /* XXX: What is the right UDPLite MIB counter? */
404 goto badunlocked;
405 }
406 if (uh->uh_sum == 0) {
407 /* XXX: What is the right UDPLite MIB counter? */
408 goto badunlocked;
409 }
410 } else {
411 if ((ulen < sizeof(struct udphdr)) || (plen != ulen)) {
412 UDPSTAT_INC(udps_badlen);
413 goto badunlocked;
414 }
415 if (uh->uh_sum == 0) {
416 UDPSTAT_INC(udps_nosum);
417 /*
418 * dport 0 was rejected earlier so this is OK even if
419 * zero_checksum_port is 0 (which is its default value).
420 */
421 if (ntohs(uh->uh_dport) == V_zero_checksum_port)
422 goto skip_checksum;
423 else
424 goto badunlocked;
425 }
426 }
427
428 if ((m->m_pkthdr.csum_flags & CSUM_DATA_VALID_IPV6) &&
429 !cscov_partial) {
430 if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR)
431 uh_sum = m->m_pkthdr.csum_data;
432 else
433 uh_sum = in6_cksum_pseudo(ip6, ulen, nxt,
434 m->m_pkthdr.csum_data);
435 uh_sum ^= 0xffff;
436 } else if (m->m_pkthdr.csum_flags & CSUM_IP6_UDP) {
437 /*
438 * Packet from local host (maybe from a VM).
439 * Checksum not required.
440 */
441 uh_sum = 0;
442 } else
443 uh_sum = in6_cksum_partial(m, nxt, off, plen, ulen);
444
445 if (uh_sum != 0) {
446 UDPSTAT_INC(udps_badsum);
447 goto badunlocked;
448 }
449
450 skip_checksum:
451 /*
452 * Construct sockaddr format source address.
453 */
454 init_sin6(&fromsa[0], m, 0);
455 fromsa[0].sin6_port = uh->uh_sport;
456 init_sin6(&fromsa[1], m, 1);
457 fromsa[1].sin6_port = uh->uh_dport;
458
459 pcbinfo = udp_get_inpcbinfo(nxt);
460 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
461 *mp = NULL;
462 return (udp6_multi_input(m, off, proto, fromsa));
463 }
464
465 /*
466 * Locate pcb for datagram.
467 */
468 lookupflags = INPLOOKUP_RLOCKPCB |
469 (V_udp_bind_all_fibs ? 0 : INPLOOKUP_FIB);
470
471 /*
472 * Grab info from PACKET_TAG_IPFORWARD tag prepended to the chain.
473 */
474 if ((m->m_flags & M_IP6_NEXTHOP) &&
475 (fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL)) != NULL) {
476 struct sockaddr_in6 *next_hop6;
477
478 next_hop6 = (struct sockaddr_in6 *)(fwd_tag + 1);
479
480 /*
481 * Transparently forwarded. Pretend to be the destination.
482 * Already got one like this?
483 */
484 inp = in6_pcblookup_mbuf(pcbinfo, &ip6->ip6_src,
485 uh->uh_sport, &ip6->ip6_dst, uh->uh_dport,
486 lookupflags, m->m_pkthdr.rcvif, m);
487 if (!inp) {
488 /*
489 * It's new. Try to find the ambushing socket.
490 * Because we've rewritten the destination address,
491 * any hardware-generated hash is ignored.
492 */
493 inp = in6_pcblookup(pcbinfo, &ip6->ip6_src,
494 uh->uh_sport, &next_hop6->sin6_addr,
495 next_hop6->sin6_port ? htons(next_hop6->sin6_port) :
496 uh->uh_dport, INPLOOKUP_WILDCARD | lookupflags,
497 m->m_pkthdr.rcvif);
498 }
499 /* Remove the tag from the packet. We don't need it anymore. */
500 m_tag_delete(m, fwd_tag);
501 m->m_flags &= ~M_IP6_NEXTHOP;
502 } else
503 inp = in6_pcblookup_mbuf(pcbinfo, &ip6->ip6_src,
504 uh->uh_sport, &ip6->ip6_dst, uh->uh_dport,
505 INPLOOKUP_WILDCARD | lookupflags,
506 m->m_pkthdr.rcvif, m);
507 if (inp == NULL) {
508 if (V_udp_log_in_vain) {
509 char ip6bufs[INET6_ADDRSTRLEN];
510 char ip6bufd[INET6_ADDRSTRLEN];
511
512 log(LOG_INFO,
513 "Connection attempt to UDP [%s]:%d from [%s]:%d\n",
514 ip6_sprintf(ip6bufd, &ip6->ip6_dst),
515 ntohs(uh->uh_dport),
516 ip6_sprintf(ip6bufs, &ip6->ip6_src),
517 ntohs(uh->uh_sport));
518 }
519 if (nxt == IPPROTO_UDPLITE)
520 UDPLITE_PROBE(receive, NULL, NULL, ip6, NULL, uh);
521 else
522 UDP_PROBE(receive, NULL, NULL, ip6, NULL, uh);
523 UDPSTAT_INC(udps_noport);
524 if (m->m_flags & M_MCAST) {
525 printf("UDP6: M_MCAST is set in a unicast packet.\n");
526 UDPSTAT_INC(udps_noportmcast);
527 goto badunlocked;
528 }
529 if (V_udp_blackhole && (V_udp_blackhole_local ||
530 !in6_localip(&ip6->ip6_src)))
531 goto badunlocked;
532 icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_NOPORT, 0);
533 *mp = NULL;
534 return (IPPROTO_DONE);
535 }
536 INP_RLOCK_ASSERT(inp);
537 up = intoudpcb(inp);
538 if (cscov_partial) {
539 if (up->u_rxcslen == 0 || up->u_rxcslen > ulen) {
540 INP_RUNLOCK(inp);
541 m_freem(m);
542 *mp = NULL;
543 return (IPPROTO_DONE);
544 }
545 }
546 if (nxt == IPPROTO_UDPLITE)
547 UDPLITE_PROBE(receive, NULL, inp, ip6, inp, uh);
548 else
549 UDP_PROBE(receive, NULL, inp, ip6, inp, uh);
550 if (udp6_append(inp, m, off, fromsa) == 0)
551 INP_RUNLOCK(inp);
552 *mp = NULL;
553 return (IPPROTO_DONE);
554
555 badunlocked:
556 m_freem(m);
557 *mp = NULL;
558 return (IPPROTO_DONE);
559 }
560
561 static void
udp6_common_ctlinput(struct ip6ctlparam * ip6cp,struct inpcbinfo * pcbinfo)562 udp6_common_ctlinput(struct ip6ctlparam *ip6cp, struct inpcbinfo *pcbinfo)
563 {
564 struct udphdr uh;
565 struct ip6_hdr *ip6;
566 struct mbuf *m;
567 struct inpcb *inp;
568 int errno, off = 0;
569 struct udp_portonly {
570 u_int16_t uh_sport;
571 u_int16_t uh_dport;
572 } *uhp;
573
574 if ((errno = icmp6_errmap(ip6cp->ip6c_icmp6)) == 0)
575 return;
576
577 m = ip6cp->ip6c_m;
578 ip6 = ip6cp->ip6c_ip6;
579 off = ip6cp->ip6c_off;
580
581 /* Check if we can safely examine src and dst ports. */
582 if (m->m_pkthdr.len < off + sizeof(*uhp))
583 return;
584
585 bzero(&uh, sizeof(uh));
586 m_copydata(m, off, sizeof(*uhp), (caddr_t)&uh);
587
588 /* Check to see if its tunneled */
589 inp = in6_pcblookup_mbuf(pcbinfo, &ip6->ip6_dst, uh.uh_dport,
590 &ip6->ip6_src, uh.uh_sport, INPLOOKUP_WILDCARD | INPLOOKUP_RLOCKPCB,
591 m->m_pkthdr.rcvif, m);
592 if (inp != NULL) {
593 struct udpcb *up;
594 udp_tun_icmp_t *func;
595
596 up = intoudpcb(inp);
597 func = up->u_icmp_func;
598 INP_RUNLOCK(inp);
599 if (func != NULL)
600 func(ip6cp);
601 }
602 in6_pcbnotify(pcbinfo, ip6cp->ip6c_finaldst, uh.uh_dport,
603 ip6cp->ip6c_src, uh.uh_sport, errno, ip6cp->ip6c_cmdarg,
604 udp_notify);
605 }
606
607 static void
udp6_ctlinput(struct ip6ctlparam * ctl)608 udp6_ctlinput(struct ip6ctlparam *ctl)
609 {
610
611 return (udp6_common_ctlinput(ctl, &V_udbinfo));
612 }
613
614 static void
udplite6_ctlinput(struct ip6ctlparam * ctl)615 udplite6_ctlinput(struct ip6ctlparam *ctl)
616 {
617
618 return (udp6_common_ctlinput(ctl, &V_ulitecbinfo));
619 }
620
621 static int
udp6_getcred(SYSCTL_HANDLER_ARGS)622 udp6_getcred(SYSCTL_HANDLER_ARGS)
623 {
624 struct xucred xuc;
625 struct sockaddr_in6 addrs[2];
626 struct epoch_tracker et;
627 struct inpcb *inp;
628 int error;
629
630 if (req->newptr == NULL)
631 return (EINVAL);
632 error = priv_check(req->td, PRIV_NETINET_GETCRED);
633 if (error)
634 return (error);
635
636 if (req->newlen != sizeof(addrs))
637 return (EINVAL);
638 if (req->oldlen != sizeof(struct xucred))
639 return (EINVAL);
640 error = SYSCTL_IN(req, addrs, sizeof(addrs));
641 if (error)
642 return (error);
643 if ((error = sa6_embedscope(&addrs[0], V_ip6_use_defzone)) != 0 ||
644 (error = sa6_embedscope(&addrs[1], V_ip6_use_defzone)) != 0) {
645 return (error);
646 }
647 NET_EPOCH_ENTER(et);
648 inp = in6_pcblookup(&V_udbinfo, &addrs[1].sin6_addr,
649 addrs[1].sin6_port, &addrs[0].sin6_addr, addrs[0].sin6_port,
650 INPLOOKUP_WILDCARD | INPLOOKUP_RLOCKPCB, NULL);
651 NET_EPOCH_EXIT(et);
652 if (inp != NULL) {
653 INP_RLOCK_ASSERT(inp);
654 if (inp->inp_socket == NULL)
655 error = ENOENT;
656 if (error == 0)
657 error = cr_canseesocket(req->td->td_ucred,
658 inp->inp_socket);
659 if (error == 0)
660 cru2x(inp->inp_cred, &xuc);
661 INP_RUNLOCK(inp);
662 } else
663 error = ENOENT;
664 if (error == 0)
665 error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred));
666 return (error);
667 }
668
669 SYSCTL_PROC(_net_inet6_udp6, OID_AUTO, getcred,
670 CTLTYPE_OPAQUE | CTLFLAG_RW | CTLFLAG_MPSAFE,
671 0, 0, udp6_getcred, "S,xucred",
672 "Get the xucred of a UDP6 connection");
673
674 static int
udp6_send(struct socket * so,int flags_arg,struct mbuf * m,struct sockaddr * addr6,struct mbuf * control,struct thread * td)675 udp6_send(struct socket *so, int flags_arg, struct mbuf *m,
676 struct sockaddr *addr6, struct mbuf *control, struct thread *td)
677 {
678 struct inpcb *inp;
679 struct ip6_hdr *ip6;
680 struct udphdr *udp6;
681 struct in6_addr *laddr, *faddr, in6a;
682 struct ip6_pktopts *optp, opt;
683 struct sockaddr_in6 *sin6, tmp;
684 struct epoch_tracker et;
685 int cscov_partial, error, flags, hlen, scope_ambiguous;
686 u_int32_t ulen, plen;
687 uint16_t cscov;
688 u_short fport;
689 uint8_t nxt;
690
691 if (addr6) {
692 error = 0;
693 if (addr6->sa_family != AF_INET6)
694 error = EAFNOSUPPORT;
695 else if (addr6->sa_len != sizeof(struct sockaddr_in6))
696 error = EINVAL;
697 if (__predict_false(error != 0)) {
698 m_freem(control);
699 m_freem(m);
700 return (error);
701 }
702 }
703
704 sin6 = (struct sockaddr_in6 *)addr6;
705
706 scope_ambiguous = 0;
707 if (sin6) {
708 /* Protect *addr6 from overwrites. */
709 tmp = *sin6;
710 sin6 = &tmp;
711
712 /*
713 * Application should provide a proper zone ID or the use of
714 * default zone IDs should be enabled. Unfortunately, some
715 * applications do not behave as it should, so we need a
716 * workaround. Even if an appropriate ID is not determined,
717 * we'll see if we can determine the outgoing interface. If we
718 * can, determine the zone ID based on the interface below.
719 */
720 if (sin6->sin6_scope_id == 0 && !V_ip6_use_defzone)
721 scope_ambiguous = 1;
722 if ((error = sa6_embedscope(sin6, V_ip6_use_defzone)) != 0) {
723 if (control)
724 m_freem(control);
725 m_freem(m);
726 return (error);
727 }
728 }
729
730 inp = sotoinpcb(so);
731 KASSERT(inp != NULL, ("%s: inp == NULL", __func__));
732 /*
733 * In the following cases we want a write lock on the inp for either
734 * local operations or for possible route cache updates in the IPv6
735 * output path:
736 * - on connected sockets (sin6 is NULL) for route cache updates,
737 * - when we are not bound to an address and source port (it is
738 * in6_pcbsetport() which will require the write lock).
739 *
740 * We check the inp fields before actually locking the inp, so
741 * here exists a race, and we may WLOCK the inp and end with already
742 * bound one by other thread. This is fine.
743 */
744 if (sin6 == NULL || (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr) &&
745 inp->inp_lport == 0))
746 INP_WLOCK(inp);
747 else
748 INP_RLOCK(inp);
749
750 nxt = (inp->inp_socket->so_proto->pr_protocol == IPPROTO_UDP) ?
751 IPPROTO_UDP : IPPROTO_UDPLITE;
752
753 #ifdef INET
754 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
755 int hasv4addr;
756
757 if (sin6 == NULL)
758 hasv4addr = (inp->inp_vflag & INP_IPV4);
759 else
760 hasv4addr = IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)
761 ? 1 : 0;
762 if (hasv4addr) {
763 /*
764 * XXXRW: We release UDP-layer locks before calling
765 * udp_send() in order to avoid recursion. However,
766 * this does mean there is a short window where inp's
767 * fields are unstable. Could this lead to a
768 * potential race in which the factors causing us to
769 * select the UDPv4 output routine are invalidated?
770 */
771 INP_UNLOCK(inp);
772 if (sin6)
773 in6_sin6_2_sin_in_sock((struct sockaddr *)sin6);
774 /* addr will just be freed in sendit(). */
775 return (udp_send(so, flags_arg | PRUS_IPV6, m,
776 (struct sockaddr *)sin6, control, td));
777 }
778 } else
779 #endif
780 if (sin6 && IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
781 /*
782 * Given this is either an IPv6-only socket or no INET is
783 * supported we will fail the send if the given destination
784 * address is a v4mapped address.
785 */
786 INP_UNLOCK(inp);
787 m_freem(m);
788 m_freem(control);
789 return (EINVAL);
790 }
791
792 NET_EPOCH_ENTER(et);
793 if (control) {
794 if ((error = ip6_setpktopts(control, &opt,
795 inp->in6p_outputopts, td->td_ucred, nxt)) != 0) {
796 goto release;
797 }
798 optp = &opt;
799 } else
800 optp = inp->in6p_outputopts;
801
802 if (sin6) {
803 /*
804 * Since we saw no essential reason for calling in_pcbconnect,
805 * we get rid of such kind of logic, and call in6_selectsrc
806 * and in6_pcbsetport in order to fill in the local address
807 * and the local port.
808 */
809 if (sin6->sin6_port == 0) {
810 error = EADDRNOTAVAIL;
811 goto release;
812 }
813
814 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
815 /* how about ::ffff:0.0.0.0 case? */
816 error = EISCONN;
817 goto release;
818 }
819
820 /*
821 * Given we handle the v4mapped case in the INET block above
822 * assert here that it must not happen anymore.
823 */
824 KASSERT(!IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr),
825 ("%s: sin6(%p)->sin6_addr is v4mapped which we "
826 "should have handled.", __func__, sin6));
827
828 /* This only requires read-locking. */
829 error = in6_selectsrc_socket(sin6, optp, inp,
830 td->td_ucred, scope_ambiguous, &in6a, NULL);
831 if (error)
832 goto release;
833 laddr = &in6a;
834
835 if (inp->inp_lport == 0) {
836 struct inpcbinfo *pcbinfo;
837
838 INP_WLOCK_ASSERT(inp);
839
840 pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol);
841 INP_HASH_WLOCK(pcbinfo);
842 error = in6_pcbsetport(laddr, inp, td->td_ucred);
843 INP_HASH_WUNLOCK(pcbinfo);
844 if (error != 0) {
845 /* Undo an address bind that may have occurred. */
846 inp->in6p_laddr = in6addr_any;
847 goto release;
848 }
849 }
850 faddr = &sin6->sin6_addr;
851 fport = sin6->sin6_port; /* allow 0 port */
852
853 } else {
854 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
855 error = ENOTCONN;
856 goto release;
857 }
858 laddr = &inp->in6p_laddr;
859 faddr = &inp->in6p_faddr;
860 fport = inp->inp_fport;
861 }
862
863
864 /*
865 * We do not support IPv6 Jumbograms (RFC2675), so validate the payload
866 * length fits in a normal gram.
867 */
868 ulen = m->m_pkthdr.len;
869 plen = sizeof(struct udphdr) + ulen;
870 hlen = sizeof(struct ip6_hdr);
871
872 if (plen > IPV6_MAXPAYLOAD) {
873 m_freem(control);
874 m_freem(m);
875 return (EMSGSIZE);
876 }
877
878 /*
879 * Calculate data length and get a mbuf for UDP, IP6, and possible
880 * link-layer headers. Immediate slide the data pointer back forward
881 * since we won't use that space at this layer.
882 */
883 M_PREPEND(m, hlen + sizeof(struct udphdr) + max_linkhdr, M_NOWAIT);
884 if (m == NULL) {
885 error = ENOBUFS;
886 goto release;
887 }
888 m->m_data += max_linkhdr;
889 m->m_len -= max_linkhdr;
890 m->m_pkthdr.len -= max_linkhdr;
891
892 /*
893 * Stuff checksum and output datagram.
894 */
895 cscov = cscov_partial = 0;
896 udp6 = (struct udphdr *)(mtod(m, caddr_t) + hlen);
897 udp6->uh_sport = inp->inp_lport; /* lport is always set in the PCB */
898 udp6->uh_dport = fport;
899 if (nxt == IPPROTO_UDPLITE) {
900 struct udpcb *up;
901
902 up = intoudpcb(inp);
903 cscov = up->u_txcslen;
904 if (cscov >= plen)
905 cscov = 0;
906 udp6->uh_ulen = htons(cscov);
907 /*
908 * For UDP-Lite, checksum coverage length of zero means
909 * the entire UDPLite packet is covered by the checksum.
910 */
911 cscov_partial = (cscov == 0) ? 0 : 1;
912 } else {
913 MPASS(plen <= IPV6_MAXPAYLOAD);
914 udp6->uh_ulen = htons((u_short)plen);
915 }
916 udp6->uh_sum = 0;
917
918 ip6 = mtod(m, struct ip6_hdr *);
919 ip6->ip6_flow = inp->inp_flow & IPV6_FLOWINFO_MASK;
920 ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
921 ip6->ip6_vfc |= IPV6_VERSION;
922 ip6->ip6_plen = htons((u_short)plen);
923 ip6->ip6_nxt = nxt;
924 ip6->ip6_hlim = in6_selecthlim(inp, NULL);
925 ip6->ip6_src = *laddr;
926 ip6->ip6_dst = *faddr;
927
928 #ifdef MAC
929 mac_inpcb_create_mbuf(inp, m);
930 #endif
931
932 if (cscov_partial) {
933 if ((udp6->uh_sum = in6_cksum_partial(m, nxt,
934 sizeof(struct ip6_hdr), plen, cscov)) == 0)
935 udp6->uh_sum = 0xffff;
936 } else {
937 udp6->uh_sum = in6_cksum_pseudo(ip6, plen, nxt, 0);
938 m->m_pkthdr.csum_flags = CSUM_UDP_IPV6;
939 m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
940 }
941
942 flags = 0;
943 #if defined(ROUTE_MPATH) || defined(RSS)
944 if (CALC_FLOWID_OUTBOUND_SENDTO) {
945 uint32_t hash_type, hash_val;
946 uint8_t pr;
947
948 pr = inp->inp_socket->so_proto->pr_protocol;
949
950 hash_val = fib6_calc_packet_hash(laddr, faddr,
951 inp->inp_lport, fport, pr, &hash_type);
952 m->m_pkthdr.flowid = hash_val;
953 M_HASHTYPE_SET(m, hash_type);
954 }
955 /* do not use inp flowid */
956 flags |= IP_NODEFAULTFLOWID;
957 #endif
958
959 UDPSTAT_INC(udps_opackets);
960 if (nxt == IPPROTO_UDPLITE)
961 UDPLITE_PROBE(send, NULL, inp, ip6, inp, udp6);
962 else
963 UDP_PROBE(send, NULL, inp, ip6, inp, udp6);
964 error = ip6_output(m, optp,
965 INP_WLOCKED(inp) ? &inp->inp_route6 : NULL, flags,
966 inp->in6p_moptions, NULL, inp);
967 INP_UNLOCK(inp);
968 NET_EPOCH_EXIT(et);
969
970 if (control) {
971 ip6_clearpktopts(&opt, -1);
972 m_freem(control);
973 }
974 return (error);
975
976 release:
977 INP_UNLOCK(inp);
978 NET_EPOCH_EXIT(et);
979 if (control) {
980 ip6_clearpktopts(&opt, -1);
981 m_freem(control);
982 }
983 m_freem(m);
984
985 return (error);
986 }
987
988 static void
udp6_abort(struct socket * so)989 udp6_abort(struct socket *so)
990 {
991 struct inpcb *inp;
992 struct inpcbinfo *pcbinfo;
993
994 pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol);
995 inp = sotoinpcb(so);
996 KASSERT(inp != NULL, ("udp6_abort: inp == NULL"));
997
998 INP_WLOCK(inp);
999 #ifdef INET
1000 if (inp->inp_vflag & INP_IPV4) {
1001 INP_WUNLOCK(inp);
1002 udp_abort(so);
1003 return;
1004 }
1005 #endif
1006
1007 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
1008 INP_HASH_WLOCK(pcbinfo);
1009 in6_pcbdisconnect(inp);
1010 INP_HASH_WUNLOCK(pcbinfo);
1011 soisdisconnected(so);
1012 }
1013 INP_WUNLOCK(inp);
1014 }
1015
1016 static int
udp6_attach(struct socket * so,int proto,struct thread * td)1017 udp6_attach(struct socket *so, int proto, struct thread *td)
1018 {
1019 struct inpcbinfo *pcbinfo;
1020 struct inpcb *inp;
1021 struct udpcb *up;
1022 int error;
1023
1024 pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol);
1025 inp = sotoinpcb(so);
1026 KASSERT(inp == NULL, ("udp6_attach: inp != NULL"));
1027
1028 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
1029 error = soreserve(so, udp_sendspace, udp_recvspace);
1030 if (error)
1031 return (error);
1032 }
1033 error = in_pcballoc(so, pcbinfo);
1034 if (error)
1035 return (error);
1036 inp = (struct inpcb *)so->so_pcb;
1037 inp->in6p_cksum = -1; /* just to be sure */
1038 /*
1039 * XXX: ugly!!
1040 * IPv4 TTL initialization is necessary for an IPv6 socket as well,
1041 * because the socket may be bound to an IPv6 wildcard address,
1042 * which may match an IPv4-mapped IPv6 address.
1043 */
1044 inp->inp_ip_ttl = V_ip_defttl;
1045 up = intoudpcb(inp);
1046 bzero(&up->u_start_zero, u_zero_size);
1047 INP_WUNLOCK(inp);
1048 return (0);
1049 }
1050
1051 static int
udp6_bind(struct socket * so,struct sockaddr * nam,struct thread * td)1052 udp6_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
1053 {
1054 struct sockaddr_in6 *sin6_p;
1055 struct inpcb *inp;
1056 struct inpcbinfo *pcbinfo;
1057 int error;
1058 u_char vflagsav;
1059
1060 pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol);
1061 inp = sotoinpcb(so);
1062 KASSERT(inp != NULL, ("udp6_bind: inp == NULL"));
1063
1064 if (nam->sa_family != AF_INET6)
1065 return (EAFNOSUPPORT);
1066 if (nam->sa_len != sizeof(struct sockaddr_in6))
1067 return (EINVAL);
1068
1069 sin6_p = (struct sockaddr_in6 *)nam;
1070
1071 INP_WLOCK(inp);
1072 INP_HASH_WLOCK(pcbinfo);
1073 vflagsav = inp->inp_vflag;
1074 inp->inp_vflag &= ~INP_IPV4;
1075 inp->inp_vflag |= INP_IPV6;
1076 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
1077 if (IN6_IS_ADDR_UNSPECIFIED(&sin6_p->sin6_addr))
1078 inp->inp_vflag |= INP_IPV4;
1079 #ifdef INET
1080 else if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
1081 struct sockaddr_in sin;
1082
1083 in6_sin6_2_sin(&sin, sin6_p);
1084 inp->inp_vflag |= INP_IPV4;
1085 inp->inp_vflag &= ~INP_IPV6;
1086 error = in_pcbbind(inp, &sin,
1087 V_udp_bind_all_fibs ? 0 : INPBIND_FIB,
1088 td->td_ucred);
1089 goto out;
1090 }
1091 #endif
1092 }
1093
1094 error = in6_pcbbind(inp, sin6_p, V_udp_bind_all_fibs ? 0 : INPBIND_FIB,
1095 td->td_ucred);
1096 #ifdef INET
1097 out:
1098 #endif
1099 if (error != 0)
1100 inp->inp_vflag = vflagsav;
1101 INP_HASH_WUNLOCK(pcbinfo);
1102 INP_WUNLOCK(inp);
1103 return (error);
1104 }
1105
1106 static void
udp6_close(struct socket * so)1107 udp6_close(struct socket *so)
1108 {
1109 struct inpcb *inp;
1110 struct inpcbinfo *pcbinfo;
1111
1112 pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol);
1113 inp = sotoinpcb(so);
1114 KASSERT(inp != NULL, ("udp6_close: inp == NULL"));
1115
1116 INP_WLOCK(inp);
1117 #ifdef INET
1118 if (inp->inp_vflag & INP_IPV4) {
1119 INP_WUNLOCK(inp);
1120 (void)udp_disconnect(so);
1121 return;
1122 }
1123 #endif
1124 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
1125 INP_HASH_WLOCK(pcbinfo);
1126 in6_pcbdisconnect(inp);
1127 INP_HASH_WUNLOCK(pcbinfo);
1128 soisdisconnected(so);
1129 }
1130 INP_WUNLOCK(inp);
1131 }
1132
1133 static int
udp6_connect(struct socket * so,struct sockaddr * nam,struct thread * td)1134 udp6_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
1135 {
1136 struct epoch_tracker et;
1137 struct inpcb *inp;
1138 struct inpcbinfo *pcbinfo;
1139 struct sockaddr_in6 *sin6;
1140 int error;
1141 u_char vflagsav;
1142
1143 pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol);
1144 inp = sotoinpcb(so);
1145 KASSERT(inp != NULL, ("udp6_connect: inp == NULL"));
1146
1147 sin6 = (struct sockaddr_in6 *)nam;
1148 if (sin6->sin6_family != AF_INET6)
1149 return (EAFNOSUPPORT);
1150 if (sin6->sin6_len != sizeof(*sin6))
1151 return (EINVAL);
1152
1153 /*
1154 * XXXRW: Need to clarify locking of v4/v6 flags.
1155 */
1156 INP_WLOCK(inp);
1157 #ifdef INET
1158 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
1159 struct sockaddr_in sin;
1160
1161 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) {
1162 error = EINVAL;
1163 goto out;
1164 }
1165 if ((inp->inp_vflag & INP_IPV4) == 0) {
1166 error = EAFNOSUPPORT;
1167 goto out;
1168 }
1169 if (inp->inp_faddr.s_addr != INADDR_ANY) {
1170 error = EISCONN;
1171 goto out;
1172 }
1173 in6_sin6_2_sin(&sin, sin6);
1174 error = prison_remote_ip4(td->td_ucred, &sin.sin_addr);
1175 if (error != 0)
1176 goto out;
1177 vflagsav = inp->inp_vflag;
1178 inp->inp_vflag |= INP_IPV4;
1179 inp->inp_vflag &= ~INP_IPV6;
1180 NET_EPOCH_ENTER(et);
1181 INP_HASH_WLOCK(pcbinfo);
1182 error = in_pcbconnect(inp, &sin, td->td_ucred);
1183 INP_HASH_WUNLOCK(pcbinfo);
1184 NET_EPOCH_EXIT(et);
1185 /*
1186 * If connect succeeds, mark socket as connected. If
1187 * connect fails and socket is unbound, reset inp_vflag
1188 * field.
1189 */
1190 if (error == 0)
1191 soisconnected(so);
1192 else if (inp->inp_laddr.s_addr == INADDR_ANY &&
1193 inp->inp_lport == 0)
1194 inp->inp_vflag = vflagsav;
1195 goto out;
1196 } else {
1197 if ((inp->inp_vflag & INP_IPV6) == 0) {
1198 error = EAFNOSUPPORT;
1199 goto out;
1200 }
1201 }
1202 #endif
1203 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
1204 error = EISCONN;
1205 goto out;
1206 }
1207 error = prison_remote_ip6(td->td_ucred, &sin6->sin6_addr);
1208 if (error != 0)
1209 goto out;
1210 vflagsav = inp->inp_vflag;
1211 inp->inp_vflag &= ~INP_IPV4;
1212 inp->inp_vflag |= INP_IPV6;
1213 NET_EPOCH_ENTER(et);
1214 INP_HASH_WLOCK(pcbinfo);
1215 error = in6_pcbconnect(inp, sin6, td->td_ucred, true);
1216 INP_HASH_WUNLOCK(pcbinfo);
1217 NET_EPOCH_EXIT(et);
1218 /*
1219 * If connect succeeds, mark socket as connected. If
1220 * connect fails and socket is unbound, reset inp_vflag
1221 * field.
1222 */
1223 if (error == 0)
1224 soisconnected(so);
1225 else if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr) &&
1226 inp->inp_lport == 0)
1227 inp->inp_vflag = vflagsav;
1228 out:
1229 INP_WUNLOCK(inp);
1230 return (error);
1231 }
1232
1233 static void
udp6_detach(struct socket * so)1234 udp6_detach(struct socket *so)
1235 {
1236 struct inpcb *inp;
1237
1238 inp = sotoinpcb(so);
1239 KASSERT(inp != NULL, ("udp6_detach: inp == NULL"));
1240
1241 INP_WLOCK(inp);
1242 in_pcbfree(inp);
1243 }
1244
1245 static int
udp6_disconnect(struct socket * so)1246 udp6_disconnect(struct socket *so)
1247 {
1248 struct inpcb *inp;
1249 struct inpcbinfo *pcbinfo;
1250
1251 pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol);
1252 inp = sotoinpcb(so);
1253 KASSERT(inp != NULL, ("udp6_disconnect: inp == NULL"));
1254
1255 INP_WLOCK(inp);
1256 #ifdef INET
1257 if (inp->inp_vflag & INP_IPV4) {
1258 INP_WUNLOCK(inp);
1259 (void)udp_disconnect(so);
1260 return (0);
1261 }
1262 #endif
1263
1264 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
1265 INP_WUNLOCK(inp);
1266 return (ENOTCONN);
1267 }
1268
1269 INP_HASH_WLOCK(pcbinfo);
1270 in6_pcbdisconnect(inp);
1271 INP_HASH_WUNLOCK(pcbinfo);
1272 SOCK_LOCK(so);
1273 so->so_state &= ~SS_ISCONNECTED; /* XXX */
1274 SOCK_UNLOCK(so);
1275 INP_WUNLOCK(inp);
1276 return (0);
1277 }
1278
1279 #define UDP6_PROTOSW \
1280 .pr_type = SOCK_DGRAM, \
1281 .pr_flags = PR_ATOMIC|PR_ADDR|PR_CAPATTACH, \
1282 .pr_ctloutput = udp_ctloutput, \
1283 .pr_abort = udp6_abort, \
1284 .pr_attach = udp6_attach, \
1285 .pr_bind = udp6_bind, \
1286 .pr_connect = udp6_connect, \
1287 .pr_control = in6_control, \
1288 .pr_detach = udp6_detach, \
1289 .pr_disconnect = udp6_disconnect, \
1290 .pr_peeraddr = in6_mapped_peeraddr, \
1291 .pr_send = udp6_send, \
1292 .pr_shutdown = udp_shutdown, \
1293 .pr_sockaddr = in6_mapped_sockaddr, \
1294 .pr_soreceive = soreceive_dgram, \
1295 .pr_sosend = sosend_dgram, \
1296 .pr_sosetlabel = in_pcbsosetlabel, \
1297 .pr_close = udp6_close
1298
1299 struct protosw udp6_protosw = {
1300 .pr_protocol = IPPROTO_UDP,
1301 UDP6_PROTOSW
1302 };
1303
1304 struct protosw udplite6_protosw = {
1305 .pr_protocol = IPPROTO_UDPLITE,
1306 UDP6_PROTOSW
1307 };
1308
1309 static void
udp6_init(void * arg __unused)1310 udp6_init(void *arg __unused)
1311 {
1312
1313 IP6PROTO_REGISTER(IPPROTO_UDP, udp6_input, udp6_ctlinput);
1314 IP6PROTO_REGISTER(IPPROTO_UDPLITE, udp6_input, udplite6_ctlinput);
1315 }
1316 SYSINIT(udp6_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD, udp6_init, NULL);
1317