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 /*
707 * In contrast to IPv4 we do not validate the max. packet length
708 * here due to IPv6 Jumbograms (RFC2675).
709 */
710
711 scope_ambiguous = 0;
712 if (sin6) {
713 /* Protect *addr6 from overwrites. */
714 tmp = *sin6;
715 sin6 = &tmp;
716
717 /*
718 * Application should provide a proper zone ID or the use of
719 * default zone IDs should be enabled. Unfortunately, some
720 * applications do not behave as it should, so we need a
721 * workaround. Even if an appropriate ID is not determined,
722 * we'll see if we can determine the outgoing interface. If we
723 * can, determine the zone ID based on the interface below.
724 */
725 if (sin6->sin6_scope_id == 0 && !V_ip6_use_defzone)
726 scope_ambiguous = 1;
727 if ((error = sa6_embedscope(sin6, V_ip6_use_defzone)) != 0) {
728 if (control)
729 m_freem(control);
730 m_freem(m);
731 return (error);
732 }
733 }
734
735 inp = sotoinpcb(so);
736 KASSERT(inp != NULL, ("%s: inp == NULL", __func__));
737 /*
738 * In the following cases we want a write lock on the inp for either
739 * local operations or for possible route cache updates in the IPv6
740 * output path:
741 * - on connected sockets (sin6 is NULL) for route cache updates,
742 * - when we are not bound to an address and source port (it is
743 * in6_pcbsetport() which will require the write lock).
744 *
745 * We check the inp fields before actually locking the inp, so
746 * here exists a race, and we may WLOCK the inp and end with already
747 * bound one by other thread. This is fine.
748 */
749 if (sin6 == NULL || (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr) &&
750 inp->inp_lport == 0))
751 INP_WLOCK(inp);
752 else
753 INP_RLOCK(inp);
754
755 nxt = (inp->inp_socket->so_proto->pr_protocol == IPPROTO_UDP) ?
756 IPPROTO_UDP : IPPROTO_UDPLITE;
757
758 #ifdef INET
759 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
760 int hasv4addr;
761
762 if (sin6 == NULL)
763 hasv4addr = (inp->inp_vflag & INP_IPV4);
764 else
765 hasv4addr = IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)
766 ? 1 : 0;
767 if (hasv4addr) {
768 /*
769 * XXXRW: We release UDP-layer locks before calling
770 * udp_send() in order to avoid recursion. However,
771 * this does mean there is a short window where inp's
772 * fields are unstable. Could this lead to a
773 * potential race in which the factors causing us to
774 * select the UDPv4 output routine are invalidated?
775 */
776 INP_UNLOCK(inp);
777 if (sin6)
778 in6_sin6_2_sin_in_sock((struct sockaddr *)sin6);
779 /* addr will just be freed in sendit(). */
780 return (udp_send(so, flags_arg | PRUS_IPV6, m,
781 (struct sockaddr *)sin6, control, td));
782 }
783 } else
784 #endif
785 if (sin6 && IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
786 /*
787 * Given this is either an IPv6-only socket or no INET is
788 * supported we will fail the send if the given destination
789 * address is a v4mapped address.
790 */
791 INP_UNLOCK(inp);
792 m_freem(m);
793 m_freem(control);
794 return (EINVAL);
795 }
796
797 NET_EPOCH_ENTER(et);
798 if (control) {
799 if ((error = ip6_setpktopts(control, &opt,
800 inp->in6p_outputopts, td->td_ucred, nxt)) != 0) {
801 goto release;
802 }
803 optp = &opt;
804 } else
805 optp = inp->in6p_outputopts;
806
807 if (sin6) {
808 /*
809 * Since we saw no essential reason for calling in_pcbconnect,
810 * we get rid of such kind of logic, and call in6_selectsrc
811 * and in6_pcbsetport in order to fill in the local address
812 * and the local port.
813 */
814 if (sin6->sin6_port == 0) {
815 error = EADDRNOTAVAIL;
816 goto release;
817 }
818
819 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
820 /* how about ::ffff:0.0.0.0 case? */
821 error = EISCONN;
822 goto release;
823 }
824
825 /*
826 * Given we handle the v4mapped case in the INET block above
827 * assert here that it must not happen anymore.
828 */
829 KASSERT(!IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr),
830 ("%s: sin6(%p)->sin6_addr is v4mapped which we "
831 "should have handled.", __func__, sin6));
832
833 /* This only requires read-locking. */
834 error = in6_selectsrc_socket(sin6, optp, inp,
835 td->td_ucred, scope_ambiguous, &in6a, NULL);
836 if (error)
837 goto release;
838 laddr = &in6a;
839
840 if (inp->inp_lport == 0) {
841 struct inpcbinfo *pcbinfo;
842
843 INP_WLOCK_ASSERT(inp);
844
845 pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol);
846 INP_HASH_WLOCK(pcbinfo);
847 error = in6_pcbsetport(laddr, inp, td->td_ucred);
848 INP_HASH_WUNLOCK(pcbinfo);
849 if (error != 0) {
850 /* Undo an address bind that may have occurred. */
851 inp->in6p_laddr = in6addr_any;
852 goto release;
853 }
854 }
855 faddr = &sin6->sin6_addr;
856 fport = sin6->sin6_port; /* allow 0 port */
857
858 } else {
859 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
860 error = ENOTCONN;
861 goto release;
862 }
863 laddr = &inp->in6p_laddr;
864 faddr = &inp->in6p_faddr;
865 fport = inp->inp_fport;
866 }
867
868 ulen = m->m_pkthdr.len;
869 plen = sizeof(struct udphdr) + ulen;
870 hlen = sizeof(struct ip6_hdr);
871
872 /*
873 * Calculate data length and get a mbuf for UDP, IP6, and possible
874 * link-layer headers. Immediate slide the data pointer back forward
875 * since we won't use that space at this layer.
876 */
877 M_PREPEND(m, hlen + sizeof(struct udphdr) + max_linkhdr, M_NOWAIT);
878 if (m == NULL) {
879 error = ENOBUFS;
880 goto release;
881 }
882 m->m_data += max_linkhdr;
883 m->m_len -= max_linkhdr;
884 m->m_pkthdr.len -= max_linkhdr;
885
886 /*
887 * Stuff checksum and output datagram.
888 */
889 cscov = cscov_partial = 0;
890 udp6 = (struct udphdr *)(mtod(m, caddr_t) + hlen);
891 udp6->uh_sport = inp->inp_lport; /* lport is always set in the PCB */
892 udp6->uh_dport = fport;
893 if (nxt == IPPROTO_UDPLITE) {
894 struct udpcb *up;
895
896 up = intoudpcb(inp);
897 cscov = up->u_txcslen;
898 if (cscov >= plen)
899 cscov = 0;
900 udp6->uh_ulen = htons(cscov);
901 /*
902 * For UDP-Lite, checksum coverage length of zero means
903 * the entire UDPLite packet is covered by the checksum.
904 */
905 cscov_partial = (cscov == 0) ? 0 : 1;
906 } else if (plen <= 0xffff)
907 udp6->uh_ulen = htons((u_short)plen);
908 else
909 udp6->uh_ulen = 0;
910 udp6->uh_sum = 0;
911
912 ip6 = mtod(m, struct ip6_hdr *);
913 ip6->ip6_flow = inp->inp_flow & IPV6_FLOWINFO_MASK;
914 ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
915 ip6->ip6_vfc |= IPV6_VERSION;
916 ip6->ip6_plen = htons((u_short)plen);
917 ip6->ip6_nxt = nxt;
918 ip6->ip6_hlim = in6_selecthlim(inp, NULL);
919 ip6->ip6_src = *laddr;
920 ip6->ip6_dst = *faddr;
921
922 #ifdef MAC
923 mac_inpcb_create_mbuf(inp, m);
924 #endif
925
926 if (cscov_partial) {
927 if ((udp6->uh_sum = in6_cksum_partial(m, nxt,
928 sizeof(struct ip6_hdr), plen, cscov)) == 0)
929 udp6->uh_sum = 0xffff;
930 } else {
931 udp6->uh_sum = in6_cksum_pseudo(ip6, plen, nxt, 0);
932 m->m_pkthdr.csum_flags = CSUM_UDP_IPV6;
933 m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
934 }
935
936 flags = 0;
937 #if defined(ROUTE_MPATH) || defined(RSS)
938 if (CALC_FLOWID_OUTBOUND_SENDTO) {
939 uint32_t hash_type, hash_val;
940 uint8_t pr;
941
942 pr = inp->inp_socket->so_proto->pr_protocol;
943
944 hash_val = fib6_calc_packet_hash(laddr, faddr,
945 inp->inp_lport, fport, pr, &hash_type);
946 m->m_pkthdr.flowid = hash_val;
947 M_HASHTYPE_SET(m, hash_type);
948 }
949 /* do not use inp flowid */
950 flags |= IP_NODEFAULTFLOWID;
951 #endif
952
953 UDPSTAT_INC(udps_opackets);
954 if (nxt == IPPROTO_UDPLITE)
955 UDPLITE_PROBE(send, NULL, inp, ip6, inp, udp6);
956 else
957 UDP_PROBE(send, NULL, inp, ip6, inp, udp6);
958 error = ip6_output(m, optp,
959 INP_WLOCKED(inp) ? &inp->inp_route6 : NULL, flags,
960 inp->in6p_moptions, NULL, inp);
961 INP_UNLOCK(inp);
962 NET_EPOCH_EXIT(et);
963
964 if (control) {
965 ip6_clearpktopts(&opt, -1);
966 m_freem(control);
967 }
968 return (error);
969
970 release:
971 INP_UNLOCK(inp);
972 NET_EPOCH_EXIT(et);
973 if (control) {
974 ip6_clearpktopts(&opt, -1);
975 m_freem(control);
976 }
977 m_freem(m);
978
979 return (error);
980 }
981
982 static void
udp6_abort(struct socket * so)983 udp6_abort(struct socket *so)
984 {
985 struct inpcb *inp;
986 struct inpcbinfo *pcbinfo;
987
988 pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol);
989 inp = sotoinpcb(so);
990 KASSERT(inp != NULL, ("udp6_abort: inp == NULL"));
991
992 INP_WLOCK(inp);
993 #ifdef INET
994 if (inp->inp_vflag & INP_IPV4) {
995 INP_WUNLOCK(inp);
996 udp_abort(so);
997 return;
998 }
999 #endif
1000
1001 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
1002 INP_HASH_WLOCK(pcbinfo);
1003 in6_pcbdisconnect(inp);
1004 INP_HASH_WUNLOCK(pcbinfo);
1005 soisdisconnected(so);
1006 }
1007 INP_WUNLOCK(inp);
1008 }
1009
1010 static int
udp6_attach(struct socket * so,int proto,struct thread * td)1011 udp6_attach(struct socket *so, int proto, struct thread *td)
1012 {
1013 struct inpcbinfo *pcbinfo;
1014 struct inpcb *inp;
1015 struct udpcb *up;
1016 int error;
1017
1018 pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol);
1019 inp = sotoinpcb(so);
1020 KASSERT(inp == NULL, ("udp6_attach: inp != NULL"));
1021
1022 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
1023 error = soreserve(so, udp_sendspace, udp_recvspace);
1024 if (error)
1025 return (error);
1026 }
1027 error = in_pcballoc(so, pcbinfo);
1028 if (error)
1029 return (error);
1030 inp = (struct inpcb *)so->so_pcb;
1031 inp->in6p_cksum = -1; /* just to be sure */
1032 /*
1033 * XXX: ugly!!
1034 * IPv4 TTL initialization is necessary for an IPv6 socket as well,
1035 * because the socket may be bound to an IPv6 wildcard address,
1036 * which may match an IPv4-mapped IPv6 address.
1037 */
1038 inp->inp_ip_ttl = V_ip_defttl;
1039 up = intoudpcb(inp);
1040 bzero(&up->u_start_zero, u_zero_size);
1041 INP_WUNLOCK(inp);
1042 return (0);
1043 }
1044
1045 static int
udp6_bind(struct socket * so,struct sockaddr * nam,struct thread * td)1046 udp6_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
1047 {
1048 struct sockaddr_in6 *sin6_p;
1049 struct inpcb *inp;
1050 struct inpcbinfo *pcbinfo;
1051 int error;
1052 u_char vflagsav;
1053
1054 pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol);
1055 inp = sotoinpcb(so);
1056 KASSERT(inp != NULL, ("udp6_bind: inp == NULL"));
1057
1058 if (nam->sa_family != AF_INET6)
1059 return (EAFNOSUPPORT);
1060 if (nam->sa_len != sizeof(struct sockaddr_in6))
1061 return (EINVAL);
1062
1063 sin6_p = (struct sockaddr_in6 *)nam;
1064
1065 INP_WLOCK(inp);
1066 INP_HASH_WLOCK(pcbinfo);
1067 vflagsav = inp->inp_vflag;
1068 inp->inp_vflag &= ~INP_IPV4;
1069 inp->inp_vflag |= INP_IPV6;
1070 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
1071 if (IN6_IS_ADDR_UNSPECIFIED(&sin6_p->sin6_addr))
1072 inp->inp_vflag |= INP_IPV4;
1073 #ifdef INET
1074 else if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
1075 struct sockaddr_in sin;
1076
1077 in6_sin6_2_sin(&sin, sin6_p);
1078 inp->inp_vflag |= INP_IPV4;
1079 inp->inp_vflag &= ~INP_IPV6;
1080 error = in_pcbbind(inp, &sin,
1081 V_udp_bind_all_fibs ? 0 : INPBIND_FIB,
1082 td->td_ucred);
1083 goto out;
1084 }
1085 #endif
1086 }
1087
1088 error = in6_pcbbind(inp, sin6_p, V_udp_bind_all_fibs ? 0 : INPBIND_FIB,
1089 td->td_ucred);
1090 #ifdef INET
1091 out:
1092 #endif
1093 if (error != 0)
1094 inp->inp_vflag = vflagsav;
1095 INP_HASH_WUNLOCK(pcbinfo);
1096 INP_WUNLOCK(inp);
1097 return (error);
1098 }
1099
1100 static void
udp6_close(struct socket * so)1101 udp6_close(struct socket *so)
1102 {
1103 struct inpcb *inp;
1104 struct inpcbinfo *pcbinfo;
1105
1106 pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol);
1107 inp = sotoinpcb(so);
1108 KASSERT(inp != NULL, ("udp6_close: inp == NULL"));
1109
1110 INP_WLOCK(inp);
1111 #ifdef INET
1112 if (inp->inp_vflag & INP_IPV4) {
1113 INP_WUNLOCK(inp);
1114 (void)udp_disconnect(so);
1115 return;
1116 }
1117 #endif
1118 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
1119 INP_HASH_WLOCK(pcbinfo);
1120 in6_pcbdisconnect(inp);
1121 INP_HASH_WUNLOCK(pcbinfo);
1122 soisdisconnected(so);
1123 }
1124 INP_WUNLOCK(inp);
1125 }
1126
1127 static int
udp6_connect(struct socket * so,struct sockaddr * nam,struct thread * td)1128 udp6_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
1129 {
1130 struct epoch_tracker et;
1131 struct inpcb *inp;
1132 struct inpcbinfo *pcbinfo;
1133 struct sockaddr_in6 *sin6;
1134 int error;
1135 u_char vflagsav;
1136
1137 pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol);
1138 inp = sotoinpcb(so);
1139 KASSERT(inp != NULL, ("udp6_connect: inp == NULL"));
1140
1141 sin6 = (struct sockaddr_in6 *)nam;
1142 if (sin6->sin6_family != AF_INET6)
1143 return (EAFNOSUPPORT);
1144 if (sin6->sin6_len != sizeof(*sin6))
1145 return (EINVAL);
1146
1147 /*
1148 * XXXRW: Need to clarify locking of v4/v6 flags.
1149 */
1150 INP_WLOCK(inp);
1151 #ifdef INET
1152 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
1153 struct sockaddr_in sin;
1154
1155 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) {
1156 error = EINVAL;
1157 goto out;
1158 }
1159 if ((inp->inp_vflag & INP_IPV4) == 0) {
1160 error = EAFNOSUPPORT;
1161 goto out;
1162 }
1163 if (inp->inp_faddr.s_addr != INADDR_ANY) {
1164 error = EISCONN;
1165 goto out;
1166 }
1167 in6_sin6_2_sin(&sin, sin6);
1168 error = prison_remote_ip4(td->td_ucred, &sin.sin_addr);
1169 if (error != 0)
1170 goto out;
1171 vflagsav = inp->inp_vflag;
1172 inp->inp_vflag |= INP_IPV4;
1173 inp->inp_vflag &= ~INP_IPV6;
1174 NET_EPOCH_ENTER(et);
1175 INP_HASH_WLOCK(pcbinfo);
1176 error = in_pcbconnect(inp, &sin, td->td_ucred);
1177 INP_HASH_WUNLOCK(pcbinfo);
1178 NET_EPOCH_EXIT(et);
1179 /*
1180 * If connect succeeds, mark socket as connected. If
1181 * connect fails and socket is unbound, reset inp_vflag
1182 * field.
1183 */
1184 if (error == 0)
1185 soisconnected(so);
1186 else if (inp->inp_laddr.s_addr == INADDR_ANY &&
1187 inp->inp_lport == 0)
1188 inp->inp_vflag = vflagsav;
1189 goto out;
1190 } else {
1191 if ((inp->inp_vflag & INP_IPV6) == 0) {
1192 error = EAFNOSUPPORT;
1193 goto out;
1194 }
1195 }
1196 #endif
1197 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
1198 error = EISCONN;
1199 goto out;
1200 }
1201 error = prison_remote_ip6(td->td_ucred, &sin6->sin6_addr);
1202 if (error != 0)
1203 goto out;
1204 vflagsav = inp->inp_vflag;
1205 inp->inp_vflag &= ~INP_IPV4;
1206 inp->inp_vflag |= INP_IPV6;
1207 NET_EPOCH_ENTER(et);
1208 INP_HASH_WLOCK(pcbinfo);
1209 error = in6_pcbconnect(inp, sin6, td->td_ucred, true);
1210 INP_HASH_WUNLOCK(pcbinfo);
1211 NET_EPOCH_EXIT(et);
1212 /*
1213 * If connect succeeds, mark socket as connected. If
1214 * connect fails and socket is unbound, reset inp_vflag
1215 * field.
1216 */
1217 if (error == 0)
1218 soisconnected(so);
1219 else if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr) &&
1220 inp->inp_lport == 0)
1221 inp->inp_vflag = vflagsav;
1222 out:
1223 INP_WUNLOCK(inp);
1224 return (error);
1225 }
1226
1227 static void
udp6_detach(struct socket * so)1228 udp6_detach(struct socket *so)
1229 {
1230 struct inpcb *inp;
1231
1232 inp = sotoinpcb(so);
1233 KASSERT(inp != NULL, ("udp6_detach: inp == NULL"));
1234
1235 INP_WLOCK(inp);
1236 in_pcbfree(inp);
1237 }
1238
1239 static int
udp6_disconnect(struct socket * so)1240 udp6_disconnect(struct socket *so)
1241 {
1242 struct inpcb *inp;
1243 struct inpcbinfo *pcbinfo;
1244
1245 pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol);
1246 inp = sotoinpcb(so);
1247 KASSERT(inp != NULL, ("udp6_disconnect: inp == NULL"));
1248
1249 INP_WLOCK(inp);
1250 #ifdef INET
1251 if (inp->inp_vflag & INP_IPV4) {
1252 INP_WUNLOCK(inp);
1253 (void)udp_disconnect(so);
1254 return (0);
1255 }
1256 #endif
1257
1258 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
1259 INP_WUNLOCK(inp);
1260 return (ENOTCONN);
1261 }
1262
1263 INP_HASH_WLOCK(pcbinfo);
1264 in6_pcbdisconnect(inp);
1265 INP_HASH_WUNLOCK(pcbinfo);
1266 SOCK_LOCK(so);
1267 so->so_state &= ~SS_ISCONNECTED; /* XXX */
1268 SOCK_UNLOCK(so);
1269 INP_WUNLOCK(inp);
1270 return (0);
1271 }
1272
1273 #define UDP6_PROTOSW \
1274 .pr_type = SOCK_DGRAM, \
1275 .pr_flags = PR_ATOMIC|PR_ADDR|PR_CAPATTACH, \
1276 .pr_ctloutput = udp_ctloutput, \
1277 .pr_abort = udp6_abort, \
1278 .pr_attach = udp6_attach, \
1279 .pr_bind = udp6_bind, \
1280 .pr_connect = udp6_connect, \
1281 .pr_control = in6_control, \
1282 .pr_detach = udp6_detach, \
1283 .pr_disconnect = udp6_disconnect, \
1284 .pr_peeraddr = in6_mapped_peeraddr, \
1285 .pr_send = udp6_send, \
1286 .pr_shutdown = udp_shutdown, \
1287 .pr_sockaddr = in6_mapped_sockaddr, \
1288 .pr_soreceive = soreceive_dgram, \
1289 .pr_sosend = sosend_dgram, \
1290 .pr_sosetlabel = in_pcbsosetlabel, \
1291 .pr_close = udp6_close
1292
1293 struct protosw udp6_protosw = {
1294 .pr_protocol = IPPROTO_UDP,
1295 UDP6_PROTOSW
1296 };
1297
1298 struct protosw udplite6_protosw = {
1299 .pr_protocol = IPPROTO_UDPLITE,
1300 UDP6_PROTOSW
1301 };
1302
1303 static void
udp6_init(void * arg __unused)1304 udp6_init(void *arg __unused)
1305 {
1306
1307 IP6PROTO_REGISTER(IPPROTO_UDP, udp6_input, udp6_ctlinput);
1308 IP6PROTO_REGISTER(IPPROTO_UDPLITE, udp6_input, udplite6_ctlinput);
1309 }
1310 SYSINIT(udp6_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD, udp6_init, NULL);
1311