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