rc.d/moused: Remove wrong stop_cmd.
[dragonfly.git] / sys / netinet6 / udp6_usrreq.c
CommitLineData
984263bc
MD
1/* $FreeBSD: src/sys/netinet6/udp6_usrreq.c,v 1.6.2.13 2003/01/24 05:11:35 sam Exp $ */
2/* $KAME: udp6_usrreq.c,v 1.27 2001/05/21 05:45:10 jinmei Exp $ */
3
4/*
5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the project nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33/*
34 * Copyright (c) 1982, 1986, 1989, 1993
35 * The Regents of the University of California. All rights reserved.
36 *
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
39 * are met:
40 * 1. Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer.
42 * 2. Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in the
44 * documentation and/or other materials provided with the distribution.
dc71b7ab 45 * 3. Neither the name of the University nor the names of its contributors
984263bc
MD
46 * may be used to endorse or promote products derived from this software
47 * without specific prior written permission.
48 *
49 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59 * SUCH DAMAGE.
60 *
61 * @(#)udp_var.h 8.1 (Berkeley) 6/10/93
62 */
63
64#include "opt_inet.h"
65#include "opt_inet6.h"
66#include "opt_ipsec.h"
67
68#include <sys/param.h>
69#include <sys/kernel.h>
70#include <sys/mbuf.h>
71#include <sys/protosw.h>
72#include <sys/socket.h>
73#include <sys/socketvar.h>
74#include <sys/sysctl.h>
75#include <sys/errno.h>
76#include <sys/stat.h>
77#include <sys/systm.h>
78#include <sys/syslog.h>
79#include <sys/proc.h>
895c1f85 80#include <sys/priv.h>
6cef7136 81
abf782b3 82#include <sys/thread2.h>
6cef7136 83#include <sys/socketvar2.h>
002c1265 84#include <sys/msgport2.h>
984263bc
MD
85
86#include <net/if.h>
87#include <net/route.h>
88#include <net/if_types.h>
5337421c 89#include <net/netisr2.h>
984263bc
MD
90
91#include <netinet/in.h>
92#include <netinet/in_systm.h>
93#include <netinet/ip.h>
94#include <netinet/in_pcb.h>
95#include <netinet/in_var.h>
96#include <netinet/ip_var.h>
97#include <netinet/udp.h>
98#include <netinet/udp_var.h>
99#include <netinet/ip6.h>
100#include <netinet6/ip6_var.h>
101#include <netinet6/in6_pcb.h>
102#include <netinet/icmp6.h>
103#include <netinet6/udp6_var.h>
104#include <netinet6/ip6protosw.h>
105
106#ifdef IPSEC
107#include <netinet6/ipsec.h>
108#include <netinet6/ipsec6.h>
109#endif /* IPSEC */
110
111#ifdef FAST_IPSEC
bf844ffa
JH
112#include <netproto/ipsec/ipsec.h>
113#include <netproto/ipsec/ipsec6.h>
984263bc
MD
114#endif /* FAST_IPSEC */
115
116/*
117 * UDP protocol inplementation.
118 * Per RFC 768, August, 1980.
119 */
120
121extern struct protosw inetsw[];
56d702cc 122static int in6_mcmatch (struct inpcb *, struct in6_addr *, struct ifnet *);
984263bc
MD
123
124static int
122ebd49 125in6_mcmatch(struct inpcb *in6p, struct in6_addr *ia6, struct ifnet *ifp)
984263bc
MD
126{
127 struct ip6_moptions *im6o = in6p->in6p_moptions;
128 struct in6_multi_mship *imm;
129
130 if (im6o == NULL)
131 return 0;
132
133 for (imm = im6o->im6o_memberships.lh_first; imm != NULL;
134 imm = imm->i6mm_chain.le_next) {
135 if ((ifp == NULL ||
136 imm->i6mm_maddr->in6m_ifp == ifp) &&
137 IN6_ARE_ADDR_EQUAL(&imm->i6mm_maddr->in6m_addr,
138 ia6))
139 return 1;
140 }
141 return 0;
142}
143
144int
dadab5e9 145udp6_input(struct mbuf **mp, int *offp, int proto)
984263bc
MD
146{
147 struct mbuf *m = *mp;
34ee9825
RG
148 struct ip6_hdr *ip6;
149 struct udphdr *uh;
150 struct inpcb *in6p;
984263bc
MD
151 struct mbuf *opts = NULL;
152 int off = *offp;
153 int plen, ulen;
154 struct sockaddr_in6 udp_in6;
e475836d 155 struct socket *so;
984263bc
MD
156
157 IP6_EXTHDR_CHECK(m, off, sizeof(struct udphdr), IPPROTO_DONE);
158
159 ip6 = mtod(m, struct ip6_hdr *);
160
161 if (faithprefix_p != NULL && (*faithprefix_p)(&ip6->ip6_dst)) {
162 /* XXX send icmp6 host/port unreach? */
163 m_freem(m);
164 return IPPROTO_DONE;
165 }
166
6e78e7fe 167 udp_stat.udps_ipackets++;
984263bc
MD
168
169 plen = ntohs(ip6->ip6_plen) - off + sizeof(*ip6);
170 uh = (struct udphdr *)((caddr_t)ip6 + off);
171 ulen = ntohs((u_short)uh->uh_ulen);
172
173 if (plen != ulen) {
6e78e7fe 174 udp_stat.udps_badlen++;
984263bc
MD
175 goto bad;
176 }
177
178 /*
179 * Checksum extended UDP header and data.
180 */
181 if (uh->uh_sum == 0)
6e78e7fe 182 udp_stat.udps_nosum++;
984263bc 183 else if (in6_cksum(m, IPPROTO_UDP, off, ulen) != 0) {
6e78e7fe 184 udp_stat.udps_badsum++;
984263bc
MD
185 goto bad;
186 }
187
188 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
189 struct inpcb *last;
190
191 /*
192 * Deliver a multicast datagram to all sockets
193 * for which the local and remote addresses and ports match
194 * those of the incoming datagram. This allows more than
195 * one process to receive multicasts on the same port.
196 * (This really ought to be done for unicast datagrams as
197 * well, but that would cause problems with existing
198 * applications that open both address-specific sockets and
199 * a wildcard socket listening to the same port -- they would
200 * end up receiving duplicates of every unicast datagram.
201 * Those applications open the multiple sockets to overcome an
202 * inadequacy of the UDP socket interface, but for backwards
203 * compatibility we avoid the problem here rather than
204 * fixing the interface. Maybe 4.5BSD will remedy this?)
205 */
206
207 /*
208 * In a case that laddr should be set to the link-local
209 * address (this happens in RIPng), the multicast address
210 * specified in the received packet does not match with
211 * laddr. To cure this situation, the matching is relaxed
212 * if the receiving interface is the same as one specified
213 * in the socket and if the destination multicast address
214 * matches one of the multicast groups specified in the socket.
215 */
216
217 /*
218 * Construct sockaddr format source address.
219 */
220 init_sin6(&udp_in6, m); /* general init */
221 udp_in6.sin6_port = uh->uh_sport;
222 /*
223 * KAME note: traditionally we dropped udpiphdr from mbuf here.
224 * We need udphdr for IPsec processing so we do that later.
225 */
226
227 /*
228 * Locate pcb(s) for datagram.
229 * (Algorithm copied from raw_intr().)
230 */
231 last = NULL;
0854930e 232 LIST_FOREACH(in6p, &udbinfo.pcblisthead, inp_list) {
5a38e392
SZ
233 KKASSERT((in6p->inp_flags & INP_PLACEMARKER) == 0);
234
bde3511a 235 if (!(in6p->inp_vflag & INP_IPV6))
984263bc
MD
236 continue;
237 if (in6p->in6p_lport != uh->uh_dport)
238 continue;
239 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) {
240 if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr,
241 &ip6->ip6_dst) &&
242 !in6_mcmatch(in6p, &ip6->ip6_dst,
243 m->m_pkthdr.rcvif))
244 continue;
245 }
246 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
247 if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr,
248 &ip6->ip6_src) ||
249 in6p->in6p_fport != uh->uh_sport)
250 continue;
251 }
252
253 if (last != NULL) {
e475836d 254 struct mbuf *n;
984263bc
MD
255
256#ifdef IPSEC
257 /*
258 * Check AH/ESP integrity.
259 */
260 if (ipsec6_in_reject_so(m, last->inp_socket))
261 ipsec6stat.in_polvio++;
262 /* do not inject data into pcb */
263 else
264#endif /* IPSEC */
265#ifdef FAST_IPSEC
266 /*
267 * Check AH/ESP integrity.
268 */
269 if (ipsec6_in_reject(m, last))
270 ;
271 else
272#endif /* FAST_IPSEC */
273 if ((n = m_copy(m, 0, M_COPYALL)) != NULL) {
274 /*
275 * KAME NOTE: do not
276 * m_copy(m, offset, ...) above.
6d49aa6f 277 * ssb_appendaddr() expects M_PKTHDR,
984263bc
MD
278 * and m_copy() will copy M_PKTHDR
279 * only if offset is 0.
280 */
e475836d
MD
281 so = last->in6p_socket;
282 if ((last->in6p_flags & IN6P_CONTROLOPTS) ||
283 (so->so_options & SO_TIMESTAMP)) {
984263bc
MD
284 ip6_savecontrol(last, &opts,
285 ip6, n);
e475836d 286 }
984263bc 287 m_adj(n, off + sizeof(struct udphdr));
e475836d
MD
288 lwkt_gettoken(&so->so_rcv.ssb_token);
289 if (ssb_appendaddr(&so->so_rcv,
290 (struct sockaddr *)&udp_in6,
291 n, opts) == 0) {
984263bc
MD
292 m_freem(n);
293 if (opts)
294 m_freem(opts);
6e78e7fe 295 udp_stat.udps_fullsock++;
e475836d
MD
296 } else {
297 sorwakeup(so);
298 }
299 lwkt_reltoken(&so->so_rcv.ssb_token);
984263bc
MD
300 opts = NULL;
301 }
302 }
303 last = in6p;
304 /*
305 * Don't look for additional matches if this one does
306 * not have either the SO_REUSEPORT or SO_REUSEADDR
307 * socket options set. This heuristic avoids searching
308 * through all pcbs in the common case of a non-shared
309 * port. It assumes that an application will never
310 * clear these options after setting them.
311 */
312 if ((last->in6p_socket->so_options &
bde3511a 313 (SO_REUSEPORT | SO_REUSEADDR)) == 0)
984263bc
MD
314 break;
315 }
316
317 if (last == NULL) {
318 /*
319 * No matching pcb found; discard datagram.
320 * (No need to send an ICMP Port Unreachable
321 * for a broadcast or multicast datgram.)
322 */
6e78e7fe
SZ
323 udp_stat.udps_noport++;
324 udp_stat.udps_noportmcast++;
984263bc
MD
325 goto bad;
326 }
327#ifdef IPSEC
328 /*
329 * Check AH/ESP integrity.
330 */
331 if (ipsec6_in_reject_so(m, last->inp_socket)) {
332 ipsec6stat.in_polvio++;
333 goto bad;
334 }
335#endif /* IPSEC */
336#ifdef FAST_IPSEC
337 /*
338 * Check AH/ESP integrity.
339 */
340 if (ipsec6_in_reject(m, last)) {
341 goto bad;
342 }
343#endif /* FAST_IPSEC */
344 if (last->in6p_flags & IN6P_CONTROLOPTS
345 || last->in6p_socket->so_options & SO_TIMESTAMP)
346 ip6_savecontrol(last, &opts, ip6, m);
347
348 m_adj(m, off + sizeof(struct udphdr));
e475836d
MD
349 so = last->in6p_socket;
350 lwkt_gettoken(&so->so_rcv.ssb_token);
351 if (ssb_appendaddr(&so->so_rcv, (struct sockaddr *)&udp_in6,
352 m, opts) == 0) {
6e78e7fe 353 udp_stat.udps_fullsock++;
e475836d 354 lwkt_reltoken(&so->so_rcv.ssb_token);
984263bc
MD
355 goto bad;
356 }
e475836d
MD
357 sorwakeup(so);
358 lwkt_reltoken(&so->so_rcv.ssb_token);
984263bc
MD
359 return IPPROTO_DONE;
360 }
361 /*
362 * Locate pcb for datagram.
363 */
364 in6p = in6_pcblookup_hash(&udbinfo, &ip6->ip6_src, uh->uh_sport,
365 &ip6->ip6_dst, uh->uh_dport, 1,
366 m->m_pkthdr.rcvif);
bde3511a 367 if (in6p == NULL) {
984263bc
MD
368 if (log_in_vain) {
369 char buf[INET6_ADDRSTRLEN];
370
371 strcpy(buf, ip6_sprintf(&ip6->ip6_dst));
372 log(LOG_INFO,
373 "Connection attempt to UDP [%s]:%d from [%s]:%d\n",
374 buf, ntohs(uh->uh_dport),
375 ip6_sprintf(&ip6->ip6_src), ntohs(uh->uh_sport));
376 }
6e78e7fe 377 udp_stat.udps_noport++;
984263bc 378 if (m->m_flags & M_MCAST) {
a6ec04bc 379 kprintf("UDP6: M_MCAST is set in a unicast packet.\n");
6e78e7fe 380 udp_stat.udps_noportmcast++;
984263bc
MD
381 goto bad;
382 }
383 icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_NOPORT, 0);
384 return IPPROTO_DONE;
385 }
386#ifdef IPSEC
387 /*
388 * Check AH/ESP integrity.
389 */
390 if (ipsec6_in_reject_so(m, in6p->in6p_socket)) {
391 ipsec6stat.in_polvio++;
392 goto bad;
393 }
394#endif /* IPSEC */
395#ifdef FAST_IPSEC
396 /*
397 * Check AH/ESP integrity.
398 */
399 if (ipsec6_in_reject(m, in6p)) {
400 goto bad;
401 }
402#endif /* FAST_IPSEC */
403
404 /*
405 * Construct sockaddr format source address.
406 * Stuff source address and datagram in user buffer.
407 */
408 init_sin6(&udp_in6, m); /* general init */
409 udp_in6.sin6_port = uh->uh_sport;
410 if (in6p->in6p_flags & IN6P_CONTROLOPTS
411 || in6p->in6p_socket->so_options & SO_TIMESTAMP)
412 ip6_savecontrol(in6p, &opts, ip6, m);
413 m_adj(m, off + sizeof(struct udphdr));
e475836d
MD
414 so = in6p->in6p_socket;
415 lwkt_gettoken(&so->so_rcv.ssb_token);
416 if (ssb_appendaddr(&so->so_rcv, (struct sockaddr *)&udp_in6,
417 m, opts) == 0) {
6e78e7fe 418 udp_stat.udps_fullsock++;
e475836d 419 lwkt_reltoken(&so->so_rcv.ssb_token);
984263bc
MD
420 goto bad;
421 }
e475836d
MD
422 sorwakeup(so);
423 lwkt_reltoken(&so->so_rcv.ssb_token);
984263bc
MD
424 return IPPROTO_DONE;
425bad:
426 if (m)
427 m_freem(m);
428 if (opts)
429 m_freem(opts);
430 return IPPROTO_DONE;
431}
432
433void
002c1265 434udp6_ctlinput(netmsg_t msg)
984263bc 435{
002c1265
MD
436 int cmd = msg->ctlinput.nm_cmd;
437 struct sockaddr *sa = msg->ctlinput.nm_arg;
438 void *d = msg->ctlinput.nm_extra;
984263bc
MD
439 struct udphdr uh;
440 struct ip6_hdr *ip6;
441 struct mbuf *m;
442 int off = 0;
443 struct ip6ctlparam *ip6cp = NULL;
444 const struct sockaddr_in6 *sa6_src = NULL;
56d702cc 445 void (*notify) (struct inpcb *, int) = udp_notify;
984263bc
MD
446 struct udp_portonly {
447 u_int16_t uh_sport;
448 u_int16_t uh_dport;
449 } *uhp;
450
451 if (sa->sa_family != AF_INET6 ||
452 sa->sa_len != sizeof(struct sockaddr_in6))
002c1265 453 goto out;
984263bc
MD
454
455 if ((unsigned)cmd >= PRC_NCMDS)
002c1265 456 goto out;
984263bc
MD
457 if (PRC_IS_REDIRECT(cmd))
458 notify = in6_rtchange, d = NULL;
459 else if (cmd == PRC_HOSTDEAD)
460 d = NULL;
461 else if (inet6ctlerrmap[cmd] == 0)
002c1265 462 goto out;
984263bc
MD
463
464 /* if the parameter is from icmp6, decode it. */
465 if (d != NULL) {
466 ip6cp = (struct ip6ctlparam *)d;
467 m = ip6cp->ip6c_m;
468 ip6 = ip6cp->ip6c_ip6;
469 off = ip6cp->ip6c_off;
470 sa6_src = ip6cp->ip6c_src;
471 } else {
472 m = NULL;
473 ip6 = NULL;
474 sa6_src = &sa6_any;
475 }
476
477 if (ip6) {
478 /*
479 * XXX: We assume that when IPV6 is non NULL,
480 * M and OFF are valid.
481 */
482
483 /* check if we can safely examine src and dst ports */
484 if (m->m_pkthdr.len < off + sizeof(*uhp))
485 return;
486
487 bzero(&uh, sizeof(uh));
488 m_copydata(m, off, sizeof(*uhp), (caddr_t)&uh);
489
7d448528
JH
490 in6_pcbnotify(&udbinfo.pcblisthead, sa, uh.uh_dport,
491 (struct sockaddr *)ip6cp->ip6c_src, uh.uh_sport,
492 cmd, 0, notify);
002c1265 493 } else {
7d448528
JH
494 in6_pcbnotify(&udbinfo.pcblisthead, sa, 0,
495 (const struct sockaddr *)sa6_src, 0,
496 cmd, 0, notify);
002c1265
MD
497 }
498out:
499 lwkt_replymsg(&msg->ctlinput.base.lmsg, 0);
984263bc
MD
500}
501
502static int
503udp6_getcred(SYSCTL_HANDLER_ARGS)
504{
505 struct sockaddr_in6 addrs[2];
506 struct inpcb *inp;
abf782b3 507 int error;
984263bc 508
895c1f85 509 error = priv_check(req->td, PRIV_ROOT);
984263bc
MD
510 if (error)
511 return (error);
512
513 if (req->newlen != sizeof(addrs))
514 return (EINVAL);
515 if (req->oldlen != sizeof(struct ucred))
516 return (EINVAL);
517 error = SYSCTL_IN(req, addrs, sizeof(addrs));
518 if (error)
519 return (error);
abf782b3 520 crit_enter();
984263bc
MD
521 inp = in6_pcblookup_hash(&udbinfo, &addrs[1].sin6_addr,
522 addrs[1].sin6_port,
523 &addrs[0].sin6_addr, addrs[0].sin6_port,
524 1, NULL);
525 if (!inp || !inp->inp_socket) {
526 error = ENOENT;
527 goto out;
528 }
529 error = SYSCTL_OUT(req, inp->inp_socket->so_cred,
530 sizeof(struct ucred));
531
532out:
abf782b3 533 crit_exit();
984263bc
MD
534 return (error);
535}
536
537SYSCTL_PROC(_net_inet6_udp6, OID_AUTO, getcred, CTLTYPE_OPAQUE|CTLFLAG_RW,
538 0, 0,
539 udp6_getcred, "S,ucred", "Get the ucred of a UDP6 connection");
540
6cef7136
MD
541/*
542 * NOTE: (so) is referenced from soabort*() and netmsg_pru_abort()
543 * will sofree() it when we return.
544 */
002c1265
MD
545static void
546udp6_abort(netmsg_t msg)
984263bc 547{
002c1265 548 struct socket *so = msg->abort.base.nm_so;
984263bc 549 struct inpcb *inp;
6cef7136 550 int error;
984263bc 551
ed894f8c 552 inp = so->so_pcb;
6cef7136
MD
553 if (inp) {
554 soisdisconnected(so);
aad1f734
SZ
555
556 udbinfo_barrier_set();
6cef7136 557 in6_pcbdetach(inp);
aad1f734 558 udbinfo_barrier_rem();
6cef7136
MD
559 error = 0;
560 } else {
561 error = EINVAL;
562 }
002c1265 563 lwkt_replymsg(&msg->abort.base.lmsg, error);
984263bc
MD
564}
565
002c1265
MD
566static void
567udp6_attach(netmsg_t msg)
984263bc 568{
002c1265
MD
569 struct socket *so = msg->attach.base.nm_so;
570 struct pru_attach_info *ai = msg->attach.nm_ai;
984263bc 571 struct inpcb *inp;
abf782b3 572 int error;
984263bc 573
ed894f8c 574 inp = so->so_pcb;
002c1265
MD
575 if (inp != NULL) {
576 error = EINVAL;
577 goto out;
578 }
984263bc 579
6d49aa6f 580 if (so->so_snd.ssb_hiwat == 0 || so->so_rcv.ssb_hiwat == 0) {
e4700d00
JH
581 error = soreserve(so, udp_sendspace, udp_recvspace,
582 ai->sb_rlimit);
984263bc 583 if (error)
002c1265 584 goto out;
984263bc 585 }
aad1f734
SZ
586
587 udbinfo_barrier_set();
e4700d00 588 error = in_pcballoc(so, &udbinfo);
aad1f734
SZ
589 udbinfo_barrier_rem();
590
984263bc 591 if (error)
002c1265 592 goto out;
aad1f734 593
984263bc
MD
594 inp = (struct inpcb *)so->so_pcb;
595 inp->inp_vflag |= INP_IPV6;
596 if (!ip6_v6only)
597 inp->inp_vflag |= INP_IPV4;
598 inp->in6p_hops = -1; /* use kernel default */
599 inp->in6p_cksum = -1; /* just to be sure */
600 /*
601 * XXX: ugly!!
602 * IPv4 TTL initialization is necessary for an IPv6 socket as well,
603 * because the socket may be bound to an IPv6 wildcard address,
604 * which may match an IPv4-mapped IPv6 address.
605 */
606 inp->inp_ip_ttl = ip_defttl;
002c1265
MD
607 error = 0;
608out:
609 lwkt_replymsg(&msg->attach.base.lmsg, error);
984263bc
MD
610}
611
002c1265
MD
612static void
613udp6_bind(netmsg_t msg)
984263bc 614{
002c1265
MD
615 struct socket *so =msg->bind.base.nm_so;
616 struct sockaddr *nam = msg->bind.nm_nam;
617 struct thread *td = msg->bind.nm_td;
2322f6ca 618 struct sockaddr_in6 *sin6_p = (struct sockaddr_in6 *)nam;
984263bc 619 struct inpcb *inp;
abf782b3 620 int error;
984263bc 621
ed894f8c 622 inp = so->so_pcb;
002c1265
MD
623 if (inp == NULL) {
624 error = EINVAL;
625 goto out;
626 }
984263bc
MD
627
628 inp->inp_vflag &= ~INP_IPV4;
629 inp->inp_vflag |= INP_IPV6;
bde3511a 630 if (!(inp->inp_flags & IN6P_IPV6_V6ONLY)) {
984263bc
MD
631 if (IN6_IS_ADDR_UNSPECIFIED(&sin6_p->sin6_addr))
632 inp->inp_vflag |= INP_IPV4;
633 else if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
634 struct sockaddr_in sin;
635
636 in6_sin6_2_sin(&sin, sin6_p);
637 inp->inp_vflag |= INP_IPV4;
638 inp->inp_vflag &= ~INP_IPV6;
abf782b3 639 crit_enter();
dadab5e9 640 error = in_pcbbind(inp, (struct sockaddr *)&sin, td);
abf782b3 641 crit_exit();
002c1265 642 goto out;
984263bc
MD
643 }
644 }
645
abf782b3 646 crit_enter();
dadab5e9 647 error = in6_pcbbind(inp, nam, td);
abf782b3 648 crit_exit();
f772087c
JH
649 if (error == 0) {
650 if (IN6_IS_ADDR_UNSPECIFIED(&sin6_p->sin6_addr))
651 inp->inp_flags |= INP_WASBOUND_NOTANY;
aad1f734
SZ
652
653 udbinfo_barrier_set();
f772087c 654 in_pcbinswildcardhash(inp);
aad1f734 655 udbinfo_barrier_rem();
f772087c 656 }
002c1265
MD
657out:
658 lwkt_replymsg(&msg->bind.base.lmsg, error);
984263bc
MD
659}
660
002c1265
MD
661static void
662udp6_connect(netmsg_t msg)
984263bc 663{
002c1265
MD
664 struct socket *so = msg->connect.base.nm_so;
665 struct sockaddr *nam = msg->connect.nm_nam;
666 struct thread *td = msg->connect.nm_td;
984263bc 667 struct inpcb *inp;
abf782b3 668 int error;
984263bc 669
aad1f734
SZ
670 udbinfo_barrier_set();
671
ed894f8c 672 inp = so->so_pcb;
002c1265
MD
673 if (inp == NULL) {
674 error = EINVAL;
675 goto out;
676 }
984263bc 677
bde3511a 678 if (!(inp->inp_flags & IN6P_IPV6_V6ONLY)) {
984263bc
MD
679 struct sockaddr_in6 *sin6_p;
680
681 sin6_p = (struct sockaddr_in6 *)nam;
682 if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
683 struct sockaddr_in sin;
684
002c1265
MD
685 if (inp->inp_faddr.s_addr != INADDR_ANY) {
686 error = EISCONN;
687 goto out;
688 }
984263bc 689 in6_sin6_2_sin(&sin, sin6_p);
abf782b3 690 crit_enter();
95390190
SZ
691 if (inp->inp_flags & INP_WILDCARD)
692 in_pcbremwildcardhash(inp);
dadab5e9 693 error = in_pcbconnect(inp, (struct sockaddr *)&sin, td);
abf782b3 694 crit_exit();
984263bc
MD
695 if (error == 0) {
696 inp->inp_vflag |= INP_IPV4;
697 inp->inp_vflag &= ~INP_IPV6;
698 soisconnected(so);
699 }
002c1265 700 goto out;
984263bc
MD
701 }
702 }
002c1265
MD
703 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
704 error = EISCONN;
705 goto out;
706 }
2322f6ca
JH
707 if (inp->inp_flags & INP_WILDCARD)
708 in_pcbremwildcardhash(inp);
002c1265
MD
709 if (!prison_remote_ip(td, nam)) {
710 error = EAFNOSUPPORT; /* IPv4 only jail */
711 goto out;
712 }
abf782b3 713 crit_enter();
dadab5e9 714 error = in6_pcbconnect(inp, nam, td);
abf782b3 715 crit_exit();
984263bc
MD
716 if (error == 0) {
717 if (!ip6_v6only) { /* should be non mapped addr */
718 inp->inp_vflag &= ~INP_IPV4;
719 inp->inp_vflag |= INP_IPV6;
720 }
721 soisconnected(so);
2322f6ca
JH
722 } else if (error == EAFNOSUPPORT) { /* connection dissolved */
723 /*
724 * Follow traditional BSD behavior and retain
725 * the local port binding. But, fix the old misbehavior
726 * of overwriting any previously bound local address.
727 */
728 if (!(inp->inp_flags & INP_WASBOUND_NOTANY))
84204577 729 inp->in6p_laddr = kin6addr_any;
2322f6ca 730 in_pcbinswildcardhash(inp);
984263bc 731 }
002c1265 732out:
aad1f734 733 udbinfo_barrier_rem();
002c1265 734 lwkt_replymsg(&msg->connect.base.lmsg, error);
984263bc
MD
735}
736
002c1265
MD
737static void
738udp6_detach(netmsg_t msg)
984263bc 739{
002c1265 740 struct socket *so = msg->detach.base.nm_so;
984263bc 741 struct inpcb *inp;
002c1265 742 int error;
984263bc 743
ed894f8c 744 inp = so->so_pcb;
002c1265 745 if (inp) {
aad1f734 746 udbinfo_barrier_set();
002c1265 747 in6_pcbdetach(inp);
aad1f734 748 udbinfo_barrier_rem();
002c1265
MD
749 error = 0;
750 } else {
751 error = EINVAL;
752 }
753 lwkt_replymsg(&msg->detach.base.lmsg, error);
984263bc
MD
754}
755
002c1265
MD
756static void
757udp6_disconnect(netmsg_t msg)
984263bc 758{
002c1265 759 struct socket *so = msg->disconnect.base.nm_so;
984263bc 760 struct inpcb *inp;
002c1265 761 int error;
984263bc 762
ed894f8c 763 inp = so->so_pcb;
002c1265
MD
764 if (inp == NULL) {
765 error = EINVAL;
766 goto out;
767 }
984263bc
MD
768
769 if (inp->inp_vflag & INP_IPV4) {
68925b20 770 const struct pr_usrreqs *pru;
984263bc
MD
771
772 pru = inetsw[ip_protox[IPPROTO_UDP]].pr_usrreqs;
002c1265
MD
773 pru->pru_disconnect(msg); /* XXX on right port? */
774 return;
984263bc
MD
775 }
776
002c1265
MD
777 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
778 error = ENOTCONN;
779 } else {
aad1f734 780 udbinfo_barrier_set();
002c1265 781 in6_pcbdisconnect(inp);
aad1f734 782 udbinfo_barrier_rem();
002c1265
MD
783 soclrstate(so, SS_ISCONNECTED); /* XXX */
784 error = 0;
785 }
786out:
787 lwkt_replymsg(&msg->disconnect.base.lmsg, error);
984263bc
MD
788}
789
002c1265
MD
790static void
791udp6_send(netmsg_t msg)
984263bc 792{
002c1265
MD
793 struct socket *so = msg->send.base.nm_so;
794 struct mbuf *m = msg->send.nm_m;
795 struct sockaddr *addr = msg->send.nm_addr;
796 struct mbuf *control = msg->send.nm_control;
797 struct thread *td = msg->send.nm_td;
984263bc
MD
798 struct inpcb *inp;
799 int error = 0;
800
ed894f8c 801 inp = so->so_pcb;
bde3511a 802 if (inp == NULL) {
984263bc
MD
803 error = EINVAL;
804 goto bad;
805 }
806
807 if (addr) {
bde3511a 808 if (addr->sa_len != sizeof(struct sockaddr_in6)) {
984263bc
MD
809 error = EINVAL;
810 goto bad;
811 }
812 if (addr->sa_family != AF_INET6) {
813 error = EAFNOSUPPORT;
814 goto bad;
815 }
816 }
817
818 if (!ip6_v6only) {
819 int hasv4addr;
4090d6ff 820 struct sockaddr_in6 *sin6 = NULL;
984263bc 821
bde3511a 822 if (addr == NULL)
984263bc
MD
823 hasv4addr = (inp->inp_vflag & INP_IPV4);
824 else {
825 sin6 = (struct sockaddr_in6 *)addr;
826 hasv4addr = IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)
827 ? 1 : 0;
828 }
829 if (hasv4addr) {
68925b20 830 const struct pr_usrreqs *pru;
984263bc
MD
831
832 if (sin6)
833 in6_sin6_2_sin_in_sock(addr);
834 pru = inetsw[ip_protox[IPPROTO_UDP]].pr_usrreqs;
002c1265
MD
835 pru->pru_send(msg);
836 /* msg invalid now */
837 return;
984263bc
MD
838 }
839 }
840
002c1265 841 error = udp6_output(inp, m, addr, control, td);
c14a35e9
MD
842 lwkt_replymsg(&msg->send.base.lmsg, error);
843 return;
bde3511a 844bad:
984263bc 845 m_freem(m);
002c1265 846 lwkt_replymsg(&msg->send.base.lmsg, error);
984263bc
MD
847}
848
849struct pr_usrreqs udp6_usrreqs = {
fa5e758c 850 .pru_abort = udp6_abort,
002c1265 851 .pru_accept = pr_generic_notsupp,
fa5e758c
MD
852 .pru_attach = udp6_attach,
853 .pru_bind = udp6_bind,
854 .pru_connect = udp6_connect,
002c1265
MD
855 .pru_connect2 = pr_generic_notsupp,
856 .pru_control = in6_control_dispatch,
fa5e758c
MD
857 .pru_detach = udp6_detach,
858 .pru_disconnect = udp6_disconnect,
002c1265
MD
859 .pru_listen = pr_generic_notsupp,
860 .pru_peeraddr = in6_mapped_peeraddr_dispatch,
861 .pru_rcvd = pr_generic_notsupp,
862 .pru_rcvoob = pr_generic_notsupp,
fa5e758c
MD
863 .pru_send = udp6_send,
864 .pru_sense = pru_sense_null,
865 .pru_shutdown = udp_shutdown,
002c1265 866 .pru_sockaddr = in6_mapped_sockaddr_dispatch,
fa5e758c 867 .pru_sosend = sosend,
8b5c39bb 868 .pru_soreceive = soreceive
984263bc 869};
fa5e758c 870