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