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