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 INP_WLOCK_ASSERT(inp);
836
837 error = in6_pcbsetport(laddr, inp, td->td_ucred);
838 if (error != 0) {
839 /* Undo an address bind that may have occurred. */
840 inp->in6p_laddr = in6addr_any;
841 goto release;
842 }
843 }
844 faddr = &sin6->sin6_addr;
845 fport = sin6->sin6_port; /* allow 0 port */
846
847 } else {
848 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
849 error = ENOTCONN;
850 goto release;
851 }
852 laddr = &inp->in6p_laddr;
853 faddr = &inp->in6p_faddr;
854 fport = inp->inp_fport;
855 }
856
857
858 /*
859 * We do not support IPv6 Jumbograms (RFC2675), so validate the payload
860 * length fits in a normal gram.
861 */
862 ulen = m->m_pkthdr.len;
863 plen = sizeof(struct udphdr) + ulen;
864 hlen = sizeof(struct ip6_hdr);
865
866 if (plen > IPV6_MAXPAYLOAD) {
867 m_freem(control);
868 m_freem(m);
869 return (EMSGSIZE);
870 }
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 {
907 MPASS(plen <= IPV6_MAXPAYLOAD);
908 udp6->uh_ulen = htons((u_short)plen);
909 }
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 (CALC_FLOWID_OUTBOUND_SENDTO) {
938 uint32_t hash_type, hash_val;
939 uint8_t pr;
940
941 pr = inp->inp_socket->so_proto->pr_protocol;
942
943 hash_val = fib6_calc_packet_hash(laddr, faddr,
944 inp->inp_lport, fport, pr, &hash_type);
945 m->m_pkthdr.flowid = hash_val;
946 M_HASHTYPE_SET(m, hash_type);
947 }
948 /* do not use inp flowid */
949 flags |= IP_NODEFAULTFLOWID;
950
951 UDPSTAT_INC(udps_opackets);
952 if (nxt == IPPROTO_UDPLITE)
953 UDPLITE_PROBE(send, NULL, inp, ip6, inp, udp6);
954 else
955 UDP_PROBE(send, NULL, inp, ip6, inp, udp6);
956 error = ip6_output(m, optp,
957 INP_WLOCKED(inp) ? &inp->inp_route6 : NULL, flags,
958 inp->in6p_moptions, NULL, inp);
959 INP_UNLOCK(inp);
960 NET_EPOCH_EXIT(et);
961
962 if (control) {
963 ip6_clearpktopts(&opt, -1);
964 m_freem(control);
965 }
966 return (error);
967
968 release:
969 INP_UNLOCK(inp);
970 NET_EPOCH_EXIT(et);
971 if (control) {
972 ip6_clearpktopts(&opt, -1);
973 m_freem(control);
974 }
975 m_freem(m);
976
977 return (error);
978 }
979
980 static void
udp6_abort(struct socket * so)981 udp6_abort(struct socket *so)
982 {
983 struct inpcb *inp;
984
985 inp = sotoinpcb(so);
986 KASSERT(inp != NULL, ("udp6_abort: inp == NULL"));
987
988 INP_WLOCK(inp);
989 #ifdef INET
990 if (inp->inp_vflag & INP_IPV4) {
991 INP_WUNLOCK(inp);
992 udp_abort(so);
993 return;
994 }
995 #endif
996
997 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
998 in6_pcbdisconnect(inp);
999 memset(&inp->in6p_laddr, 0, sizeof(inp->in6p_laddr));
1000 soisdisconnected(so);
1001 }
1002 INP_WUNLOCK(inp);
1003 }
1004
1005 static int
udp6_attach(struct socket * so,int proto,struct thread * td)1006 udp6_attach(struct socket *so, int proto, struct thread *td)
1007 {
1008 struct inpcbinfo *pcbinfo;
1009 struct inpcb *inp;
1010 struct udpcb *up;
1011 int error;
1012
1013 pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol);
1014 inp = sotoinpcb(so);
1015 KASSERT(inp == NULL, ("udp6_attach: inp != NULL"));
1016
1017 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
1018 error = soreserve(so, udp_sendspace, udp_recvspace);
1019 if (error)
1020 return (error);
1021 }
1022 error = in_pcballoc(so, pcbinfo);
1023 if (error)
1024 return (error);
1025 inp = (struct inpcb *)so->so_pcb;
1026 inp->in6p_cksum = -1; /* just to be sure */
1027 /*
1028 * XXX: ugly!!
1029 * IPv4 TTL initialization is necessary for an IPv6 socket as well,
1030 * because the socket may be bound to an IPv6 wildcard address,
1031 * which may match an IPv4-mapped IPv6 address.
1032 */
1033 inp->inp_ip_ttl = V_ip_defttl;
1034 up = intoudpcb(inp);
1035 bzero(&up->u_start_zero, u_zero_size);
1036 INP_WUNLOCK(inp);
1037 return (0);
1038 }
1039
1040 static int
udp6_bind(struct socket * so,struct sockaddr * nam,struct thread * td)1041 udp6_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
1042 {
1043 struct sockaddr_in6 *sin6_p;
1044 struct inpcb *inp;
1045 int error;
1046 u_char vflagsav;
1047
1048 inp = sotoinpcb(so);
1049 KASSERT(inp != NULL, ("udp6_bind: inp == NULL"));
1050
1051 if (nam->sa_family != AF_INET6)
1052 return (EAFNOSUPPORT);
1053 if (nam->sa_len != sizeof(struct sockaddr_in6))
1054 return (EINVAL);
1055
1056 sin6_p = (struct sockaddr_in6 *)nam;
1057
1058 INP_WLOCK(inp);
1059 vflagsav = inp->inp_vflag;
1060 inp->inp_vflag &= ~INP_IPV4;
1061 inp->inp_vflag |= INP_IPV6;
1062 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
1063 if (IN6_IS_ADDR_UNSPECIFIED(&sin6_p->sin6_addr))
1064 inp->inp_vflag |= INP_IPV4;
1065 #ifdef INET
1066 else if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
1067 struct sockaddr_in sin;
1068
1069 in6_sin6_2_sin(&sin, sin6_p);
1070 inp->inp_vflag |= INP_IPV4;
1071 inp->inp_vflag &= ~INP_IPV6;
1072 error = in_pcbbind(inp, &sin,
1073 V_udp_bind_all_fibs ? 0 : INPBIND_FIB,
1074 td->td_ucred);
1075 goto out;
1076 }
1077 #endif
1078 }
1079
1080 error = in6_pcbbind(inp, sin6_p, V_udp_bind_all_fibs ? 0 : INPBIND_FIB,
1081 td->td_ucred);
1082 #ifdef INET
1083 out:
1084 #endif
1085 if (error != 0)
1086 inp->inp_vflag = vflagsav;
1087 INP_WUNLOCK(inp);
1088 return (error);
1089 }
1090
1091 static void
udp6_close(struct socket * so)1092 udp6_close(struct socket *so)
1093 {
1094 struct inpcb *inp;
1095
1096 inp = sotoinpcb(so);
1097 KASSERT(inp != NULL, ("udp6_close: inp == NULL"));
1098
1099 INP_WLOCK(inp);
1100 #ifdef INET
1101 if (inp->inp_vflag & INP_IPV4) {
1102 INP_WUNLOCK(inp);
1103 (void)udp_disconnect(so);
1104 return;
1105 }
1106 #endif
1107 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
1108 in6_pcbdisconnect(inp);
1109 memset(&inp->in6p_laddr, 0, sizeof(inp->in6p_laddr));
1110 soisdisconnected(so);
1111 }
1112 INP_WUNLOCK(inp);
1113 }
1114
1115 static int
udp6_connect(struct socket * so,struct sockaddr * nam,struct thread * td)1116 udp6_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
1117 {
1118 struct epoch_tracker et;
1119 struct inpcb *inp;
1120 struct sockaddr_in6 *sin6;
1121 int error;
1122 u_char vflagsav;
1123
1124 inp = sotoinpcb(so);
1125 KASSERT(inp != NULL, ("udp6_connect: inp == NULL"));
1126
1127 sin6 = (struct sockaddr_in6 *)nam;
1128 if (sin6->sin6_family != AF_INET6)
1129 return (EAFNOSUPPORT);
1130 if (sin6->sin6_len != sizeof(*sin6))
1131 return (EINVAL);
1132
1133 /*
1134 * XXXRW: Need to clarify locking of v4/v6 flags.
1135 */
1136 INP_WLOCK(inp);
1137 #ifdef INET
1138 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
1139 struct sockaddr_in sin;
1140
1141 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) {
1142 error = EINVAL;
1143 goto out;
1144 }
1145 if ((inp->inp_vflag & INP_IPV4) == 0) {
1146 error = EAFNOSUPPORT;
1147 goto out;
1148 }
1149 if (inp->inp_faddr.s_addr != INADDR_ANY) {
1150 error = EISCONN;
1151 goto out;
1152 }
1153 in6_sin6_2_sin(&sin, sin6);
1154 error = prison_remote_ip4(td->td_ucred, &sin.sin_addr);
1155 if (error != 0)
1156 goto out;
1157 vflagsav = inp->inp_vflag;
1158 inp->inp_vflag |= INP_IPV4;
1159 inp->inp_vflag &= ~INP_IPV6;
1160 NET_EPOCH_ENTER(et);
1161 error = in_pcbconnect(inp, &sin, td->td_ucred);
1162 NET_EPOCH_EXIT(et);
1163 /*
1164 * If connect succeeds, mark socket as connected. If
1165 * connect fails and socket is unbound, reset inp_vflag
1166 * field.
1167 */
1168 if (error == 0)
1169 soisconnected(so);
1170 else if (inp->inp_laddr.s_addr == INADDR_ANY &&
1171 inp->inp_lport == 0)
1172 inp->inp_vflag = vflagsav;
1173 goto out;
1174 } else {
1175 if ((inp->inp_vflag & INP_IPV6) == 0) {
1176 error = EAFNOSUPPORT;
1177 goto out;
1178 }
1179 }
1180 #endif
1181 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
1182 error = EISCONN;
1183 goto out;
1184 }
1185 error = prison_remote_ip6(td->td_ucred, &sin6->sin6_addr);
1186 if (error != 0)
1187 goto out;
1188 vflagsav = inp->inp_vflag;
1189 inp->inp_vflag &= ~INP_IPV4;
1190 inp->inp_vflag |= INP_IPV6;
1191 NET_EPOCH_ENTER(et);
1192 error = in6_pcbconnect(inp, sin6, td->td_ucred, true);
1193 NET_EPOCH_EXIT(et);
1194 /*
1195 * If connect succeeds, mark socket as connected. If
1196 * connect fails and socket is unbound, reset inp_vflag
1197 * field.
1198 */
1199 if (error == 0)
1200 soisconnected(so);
1201 else if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr) &&
1202 inp->inp_lport == 0)
1203 inp->inp_vflag = vflagsav;
1204 out:
1205 INP_WUNLOCK(inp);
1206 return (error);
1207 }
1208
1209 static void
udp6_detach(struct socket * so)1210 udp6_detach(struct socket *so)
1211 {
1212 struct inpcb *inp;
1213
1214 inp = sotoinpcb(so);
1215 KASSERT(inp != NULL, ("udp6_detach: inp == NULL"));
1216
1217 INP_WLOCK(inp);
1218 in_pcbfree(inp);
1219 }
1220
1221 static int
udp6_disconnect(struct socket * so)1222 udp6_disconnect(struct socket *so)
1223 {
1224 struct inpcb *inp;
1225
1226 inp = sotoinpcb(so);
1227 KASSERT(inp != NULL, ("udp6_disconnect: inp == NULL"));
1228
1229 INP_WLOCK(inp);
1230 #ifdef INET
1231 if (inp->inp_vflag & INP_IPV4) {
1232 INP_WUNLOCK(inp);
1233 (void)udp_disconnect(so);
1234 return (0);
1235 }
1236 #endif
1237
1238 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
1239 INP_WUNLOCK(inp);
1240 return (ENOTCONN);
1241 }
1242
1243 in6_pcbdisconnect(inp);
1244 memset(&inp->in6p_laddr, 0, sizeof(inp->in6p_laddr));
1245 SOCK_LOCK(so);
1246 so->so_state &= ~SS_ISCONNECTED; /* XXX */
1247 SOCK_UNLOCK(so);
1248 INP_WUNLOCK(inp);
1249 return (0);
1250 }
1251
1252 #define UDP6_PROTOSW \
1253 .pr_type = SOCK_DGRAM, \
1254 .pr_flags = PR_ATOMIC|PR_ADDR|PR_CAPATTACH, \
1255 .pr_ctloutput = udp_ctloutput, \
1256 .pr_abort = udp6_abort, \
1257 .pr_attach = udp6_attach, \
1258 .pr_bind = udp6_bind, \
1259 .pr_connect = udp6_connect, \
1260 .pr_control = in6_control, \
1261 .pr_detach = udp6_detach, \
1262 .pr_disconnect = udp6_disconnect, \
1263 .pr_peeraddr = in6_mapped_peeraddr, \
1264 .pr_send = udp6_send, \
1265 .pr_shutdown = udp_shutdown, \
1266 .pr_sockaddr = in6_mapped_sockaddr, \
1267 .pr_soreceive = soreceive_dgram, \
1268 .pr_sosend = sosend_dgram, \
1269 .pr_sosetlabel = in_pcbsosetlabel, \
1270 .pr_close = udp6_close
1271
1272 struct protosw udp6_protosw = {
1273 .pr_protocol = IPPROTO_UDP,
1274 UDP6_PROTOSW
1275 };
1276
1277 struct protosw udplite6_protosw = {
1278 .pr_protocol = IPPROTO_UDPLITE,
1279 UDP6_PROTOSW
1280 };
1281
1282 static void
udp6_init(void * arg __unused)1283 udp6_init(void *arg __unused)
1284 {
1285
1286 IP6PROTO_REGISTER(IPPROTO_UDP, udp6_input, udp6_ctlinput);
1287 IP6PROTO_REGISTER(IPPROTO_UDPLITE, udp6_input, udplite6_ctlinput);
1288 }
1289 SYSINIT(udp6_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD, udp6_init, NULL);
1290