1 /*-
2 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the project nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 * $KAME: udp6_usrreq.c,v 1.27 2001/05/21 05:45:10 jinmei Exp $
30 * $KAME: udp6_output.c,v 1.31 2001/05/21 16:39:15 jinmei Exp $
31 */
32
33 /*-
34 * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995
35 * The Regents of the University of California.
36 * All rights reserved.
37 *
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
40 * are met:
41 * 1. Redistributions of source code must retain the above copyright
42 * notice, this list of conditions and the following disclaimer.
43 * 2. Redistributions in binary form must reproduce the above copyright
44 * notice, this list of conditions and the following disclaimer in the
45 * documentation and/or other materials provided with the distribution.
46 * 4. Neither the name of the University nor the names of its contributors
47 * may be used to endorse or promote products derived from this software
48 * without specific prior written permission.
49 *
50 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
51 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
52 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
53 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
54 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
55 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
56 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
57 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
58 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
59 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
60 * SUCH DAMAGE.
61 *
62 * @(#)udp_usrreq.c 8.6 (Berkeley) 5/23/95
63 */
64
65 #include <sys/cdefs.h>
66 __FBSDID("$FreeBSD: releng/8.0/sys/netinet6/udp6_usrreq.c 196019 2009-08-01 19:26:27Z rwatson $");
67
68 #include "opt_inet.h"
69 #include "opt_inet6.h"
70 #include "opt_ipsec.h"
71
72 #include <sys/param.h>
73 #include <sys/jail.h>
74 #include <sys/kernel.h>
75 #include <sys/lock.h>
76 #include <sys/mbuf.h>
77 #include <sys/priv.h>
78 #include <sys/proc.h>
79 #include <sys/protosw.h>
80 #include <sys/signalvar.h>
81 #include <sys/socket.h>
82 #include <sys/socketvar.h>
83 #include <sys/sx.h>
84 #include <sys/sysctl.h>
85 #include <sys/syslog.h>
86 #include <sys/systm.h>
87
88 #include <net/if.h>
89 #include <net/if_types.h>
90 #include <net/route.h>
91
92 #include <netinet/in.h>
93 #include <netinet/in_pcb.h>
94 #include <netinet/in_systm.h>
95 #include <netinet/in_var.h>
96 #include <netinet/ip.h>
97 #include <netinet/ip_icmp.h>
98 #include <netinet/ip6.h>
99 #include <netinet/icmp_var.h>
100 #include <netinet/icmp6.h>
101 #include <netinet/ip_var.h>
102 #include <netinet/udp.h>
103 #include <netinet/udp_var.h>
104
105 #include <netinet6/ip6protosw.h>
106 #include <netinet6/ip6_var.h>
107 #include <netinet6/in6_pcb.h>
108 #include <netinet6/udp6_var.h>
109 #include <netinet6/scope6_var.h>
110
111 #ifdef IPSEC
112 #include <netipsec/ipsec.h>
113 #include <netipsec/ipsec6.h>
114 #endif /* IPSEC */
115
116 #include <security/mac/mac_framework.h>
117
118 /*
119 * UDP protocol implementation.
120 * Per RFC 768, August, 1980.
121 */
122
123 extern struct protosw inetsw[];
124 static void udp6_detach(struct socket *so);
125
126 static void
127 udp6_append(struct inpcb *inp, struct mbuf *n, int off,
128 struct sockaddr_in6 *fromsa)
129 {
130 struct socket *so;
131 struct mbuf *opts;
132
133 INP_LOCK_ASSERT(inp);
134
135 #ifdef IPSEC
136 /* Check AH/ESP integrity. */
137 if (ipsec6_in_reject(n, inp)) {
138 m_freem(n);
139 V_ipsec6stat.in_polvio++;
140 return;
141 }
142 #endif /* IPSEC */
143 #ifdef MAC
144 if (mac_inpcb_check_deliver(inp, n) != 0) {
145 m_freem(n);
146 return;
147 }
148 #endif
149 opts = NULL;
150 if (inp->inp_flags & INP_CONTROLOPTS ||
151 inp->inp_socket->so_options & SO_TIMESTAMP)
152 ip6_savecontrol(inp, n, &opts);
153 m_adj(n, off + sizeof(struct udphdr));
154
155 so = inp->inp_socket;
156 SOCKBUF_LOCK(&so->so_rcv);
157 if (sbappendaddr_locked(&so->so_rcv, (struct sockaddr *)fromsa, n,
158 opts) == 0) {
159 SOCKBUF_UNLOCK(&so->so_rcv);
160 m_freem(n);
161 if (opts)
162 m_freem(opts);
163 UDPSTAT_INC(udps_fullsock);
164 } else
165 sorwakeup_locked(so);
166 }
167
168 int
169 udp6_input(struct mbuf **mp, int *offp, int proto)
170 {
171 struct mbuf *m = *mp;
172 struct ifnet *ifp;
173 struct ip6_hdr *ip6;
174 struct udphdr *uh;
175 struct inpcb *inp;
176 struct udpcb *up;
177 int off = *offp;
178 int plen, ulen;
179 struct sockaddr_in6 fromsa;
180
181 ifp = m->m_pkthdr.rcvif;
182 ip6 = mtod(m, struct ip6_hdr *);
183
184 if (faithprefix_p != NULL && (*faithprefix_p)(&ip6->ip6_dst)) {
185 /* XXX send icmp6 host/port unreach? */
186 m_freem(m);
187 return (IPPROTO_DONE);
188 }
189
190 #ifndef PULLDOWN_TEST
191 IP6_EXTHDR_CHECK(m, off, sizeof(struct udphdr), IPPROTO_DONE);
192 ip6 = mtod(m, struct ip6_hdr *);
193 uh = (struct udphdr *)((caddr_t)ip6 + off);
194 #else
195 IP6_EXTHDR_GET(uh, struct udphdr *, m, off, sizeof(*uh));
196 if (!uh)
197 return (IPPROTO_DONE);
198 #endif
199
200 UDPSTAT_INC(udps_ipackets);
201
202 /*
203 * Destination port of 0 is illegal, based on RFC768.
204 */
205 if (uh->uh_dport == 0)
206 goto badunlocked;
207
208 plen = ntohs(ip6->ip6_plen) - off + sizeof(*ip6);
209 ulen = ntohs((u_short)uh->uh_ulen);
210
211 if (plen != ulen) {
212 UDPSTAT_INC(udps_badlen);
213 goto badunlocked;
214 }
215
216 /*
217 * Checksum extended UDP header and data.
218 */
219 if (uh->uh_sum == 0) {
220 UDPSTAT_INC(udps_nosum);
221 goto badunlocked;
222 }
223 if (in6_cksum(m, IPPROTO_UDP, off, ulen) != 0) {
224 UDPSTAT_INC(udps_badsum);
225 goto badunlocked;
226 }
227
228 /*
229 * Construct sockaddr format source address.
230 */
231 init_sin6(&fromsa, m);
232 fromsa.sin6_port = uh->uh_sport;
233
234 INP_INFO_RLOCK(&V_udbinfo);
235 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
236 struct inpcb *last;
237 struct ip6_moptions *imo;
238
239 /*
240 * In the event that laddr should be set to the link-local
241 * address (this happens in RIPng), the multicast address
242 * specified in the received packet will not match laddr. To
243 * handle this situation, matching is relaxed if the
244 * receiving interface is the same as one specified in the
245 * socket and if the destination multicast address matches
246 * one of the multicast groups specified in the socket.
247 */
248
249 /*
250 * KAME note: traditionally we dropped udpiphdr from mbuf
251 * here. We need udphdr for IPsec processing so we do that
252 * later.
253 */
254 last = NULL;
255 LIST_FOREACH(inp, &V_udb, inp_list) {
256 if ((inp->inp_vflag & INP_IPV6) == 0)
257 continue;
258 if (inp->inp_lport != uh->uh_dport)
259 continue;
260 if (inp->inp_fport != 0 &&
261 inp->inp_fport != uh->uh_sport)
262 continue;
263 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
264 if (!IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr,
265 &ip6->ip6_dst))
266 continue;
267 }
268 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
269 if (!IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr,
270 &ip6->ip6_src) ||
271 inp->inp_fport != uh->uh_sport)
272 continue;
273 }
274
275 /*
276 * Handle socket delivery policy for any-source
277 * and source-specific multicast. [RFC3678]
278 */
279 imo = inp->in6p_moptions;
280 if (imo && IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
281 struct sockaddr_in6 mcaddr;
282 int blocked;
283
284 INP_RLOCK(inp);
285
286 bzero(&mcaddr, sizeof(struct sockaddr_in6));
287 mcaddr.sin6_len = sizeof(struct sockaddr_in6);
288 mcaddr.sin6_family = AF_INET6;
289 mcaddr.sin6_addr = ip6->ip6_dst;
290
291 blocked = im6o_mc_filter(imo, ifp,
292 (struct sockaddr *)&mcaddr,
293 (struct sockaddr *)&fromsa);
294 if (blocked != MCAST_PASS) {
295 if (blocked == MCAST_NOTGMEMBER)
296 IP6STAT_INC(ip6s_notmember);
297 if (blocked == MCAST_NOTSMEMBER ||
298 blocked == MCAST_MUTED)
299 UDPSTAT_INC(udps_filtermcast);
300 INP_RUNLOCK(inp); /* XXX */
301 continue;
302 }
303
304 INP_RUNLOCK(inp);
305 }
306 if (last != NULL) {
307 struct mbuf *n;
308
309 if ((n = m_copy(m, 0, M_COPYALL)) != NULL) {
310 INP_RLOCK(last);
311 up = intoudpcb(last);
312 if (up->u_tun_func == NULL) {
313 udp6_append(last, n, off, &fromsa);
314 } else {
315 /*
316 * Engage the tunneling
317 * protocol we will have to
318 * leave the info_lock up,
319 * since we are hunting
320 * through multiple UDP's.
321 *
322 */
323 (*up->u_tun_func)(n, off, last);
324 }
325 INP_RUNLOCK(last);
326 }
327 }
328 last = inp;
329 /*
330 * Don't look for additional matches if this one does
331 * not have either the SO_REUSEPORT or SO_REUSEADDR
332 * socket options set. This heuristic avoids
333 * searching through all pcbs in the common case of a
334 * non-shared port. It assumes that an application
335 * will never clear these options after setting them.
336 */
337 if ((last->inp_socket->so_options &
338 (SO_REUSEPORT|SO_REUSEADDR)) == 0)
339 break;
340 }
341
342 if (last == NULL) {
343 /*
344 * No matching pcb found; discard datagram. (No need
345 * to send an ICMP Port Unreachable for a broadcast
346 * or multicast datgram.)
347 */
348 UDPSTAT_INC(udps_noport);
349 UDPSTAT_INC(udps_noportmcast);
350 goto badheadlocked;
351 }
352 INP_RLOCK(last);
353 INP_INFO_RUNLOCK(&V_udbinfo);
354 up = intoudpcb(last);
355 if (up->u_tun_func == NULL) {
356 udp6_append(last, m, off, &fromsa);
357 } else {
358 /*
359 * Engage the tunneling protocol.
360 */
361 (*up->u_tun_func)(m, off, last);
362 }
363 INP_RUNLOCK(last);
364 return (IPPROTO_DONE);
365 }
366 /*
367 * Locate pcb for datagram.
368 */
369 inp = in6_pcblookup_hash(&V_udbinfo, &ip6->ip6_src, uh->uh_sport,
370 &ip6->ip6_dst, uh->uh_dport, 1, m->m_pkthdr.rcvif);
371 if (inp == NULL) {
372 if (udp_log_in_vain) {
373 char ip6bufs[INET6_ADDRSTRLEN];
374 char ip6bufd[INET6_ADDRSTRLEN];
375
376 log(LOG_INFO,
377 "Connection attempt to UDP [%s]:%d from [%s]:%d\n",
378 ip6_sprintf(ip6bufd, &ip6->ip6_dst),
379 ntohs(uh->uh_dport),
380 ip6_sprintf(ip6bufs, &ip6->ip6_src),
381 ntohs(uh->uh_sport));
382 }
383 UDPSTAT_INC(udps_noport);
384 if (m->m_flags & M_MCAST) {
385 printf("UDP6: M_MCAST is set in a unicast packet.\n");
386 UDPSTAT_INC(udps_noportmcast);
387 goto badheadlocked;
388 }
389 INP_INFO_RUNLOCK(&V_udbinfo);
390 if (V_udp_blackhole)
391 goto badunlocked;
392 if (badport_bandlim(BANDLIM_ICMP6_UNREACH) < 0)
393 goto badunlocked;
394 icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_NOPORT, 0);
395 return (IPPROTO_DONE);
396 }
397 INP_RLOCK(inp);
398 INP_INFO_RUNLOCK(&V_udbinfo);
399 up = intoudpcb(inp);
400 if (up->u_tun_func == NULL) {
401 udp6_append(inp, m, off, &fromsa);
402 } else {
403 /*
404 * Engage the tunneling protocol.
405 */
406
407 (*up->u_tun_func)(m, off, inp);
408 }
409 INP_RUNLOCK(inp);
410 return (IPPROTO_DONE);
411
412 badheadlocked:
413 INP_INFO_RUNLOCK(&V_udbinfo);
414 badunlocked:
415 if (m)
416 m_freem(m);
417 return (IPPROTO_DONE);
418 }
419
420 void
421 udp6_ctlinput(int cmd, struct sockaddr *sa, void *d)
422 {
423 struct udphdr uh;
424 struct ip6_hdr *ip6;
425 struct mbuf *m;
426 int off = 0;
427 struct ip6ctlparam *ip6cp = NULL;
428 const struct sockaddr_in6 *sa6_src = NULL;
429 void *cmdarg;
430 struct inpcb *(*notify)(struct inpcb *, int) = udp_notify;
431 struct udp_portonly {
432 u_int16_t uh_sport;
433 u_int16_t uh_dport;
434 } *uhp;
435
436 if (sa->sa_family != AF_INET6 ||
437 sa->sa_len != sizeof(struct sockaddr_in6))
438 return;
439
440 if ((unsigned)cmd >= PRC_NCMDS)
441 return;
442 if (PRC_IS_REDIRECT(cmd))
443 notify = in6_rtchange, d = NULL;
444 else if (cmd == PRC_HOSTDEAD)
445 d = NULL;
446 else if (inet6ctlerrmap[cmd] == 0)
447 return;
448
449 /* if the parameter is from icmp6, decode it. */
450 if (d != NULL) {
451 ip6cp = (struct ip6ctlparam *)d;
452 m = ip6cp->ip6c_m;
453 ip6 = ip6cp->ip6c_ip6;
454 off = ip6cp->ip6c_off;
455 cmdarg = ip6cp->ip6c_cmdarg;
456 sa6_src = ip6cp->ip6c_src;
457 } else {
458 m = NULL;
459 ip6 = NULL;
460 cmdarg = NULL;
461 sa6_src = &sa6_any;
462 }
463
464 if (ip6) {
465 /*
466 * XXX: We assume that when IPV6 is non NULL,
467 * M and OFF are valid.
468 */
469
470 /* Check if we can safely examine src and dst ports. */
471 if (m->m_pkthdr.len < off + sizeof(*uhp))
472 return;
473
474 bzero(&uh, sizeof(uh));
475 m_copydata(m, off, sizeof(*uhp), (caddr_t)&uh);
476
477 (void) in6_pcbnotify(&V_udbinfo, sa, uh.uh_dport,
478 (struct sockaddr *)ip6cp->ip6c_src, uh.uh_sport, cmd,
479 cmdarg, notify);
480 } else
481 (void) in6_pcbnotify(&V_udbinfo, sa, 0,
482 (const struct sockaddr *)sa6_src, 0, cmd, cmdarg, notify);
483 }
484
485 static int
486 udp6_getcred(SYSCTL_HANDLER_ARGS)
487 {
488 struct xucred xuc;
489 struct sockaddr_in6 addrs[2];
490 struct inpcb *inp;
491 int error;
492
493 error = priv_check(req->td, PRIV_NETINET_GETCRED);
494 if (error)
495 return (error);
496
497 if (req->newlen != sizeof(addrs))
498 return (EINVAL);
499 if (req->oldlen != sizeof(struct xucred))
500 return (EINVAL);
501 error = SYSCTL_IN(req, addrs, sizeof(addrs));
502 if (error)
503 return (error);
504 if ((error = sa6_embedscope(&addrs[0], V_ip6_use_defzone)) != 0 ||
505 (error = sa6_embedscope(&addrs[1], V_ip6_use_defzone)) != 0) {
506 return (error);
507 }
508 INP_INFO_RLOCK(&V_udbinfo);
509 inp = in6_pcblookup_hash(&V_udbinfo, &addrs[1].sin6_addr,
510 addrs[1].sin6_port, &addrs[0].sin6_addr, addrs[0].sin6_port, 1,
511 NULL);
512 if (inp != NULL) {
513 INP_RLOCK(inp);
514 INP_INFO_RUNLOCK(&V_udbinfo);
515 if (inp->inp_socket == NULL)
516 error = ENOENT;
517 if (error == 0)
518 error = cr_canseesocket(req->td->td_ucred,
519 inp->inp_socket);
520 if (error == 0)
521 cru2x(inp->inp_cred, &xuc);
522 INP_RUNLOCK(inp);
523 } else {
524 INP_INFO_RUNLOCK(&V_udbinfo);
525 error = ENOENT;
526 }
527 if (error == 0)
528 error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred));
529 return (error);
530 }
531
532 SYSCTL_PROC(_net_inet6_udp6, OID_AUTO, getcred, CTLTYPE_OPAQUE|CTLFLAG_RW, 0,
533 0, udp6_getcred, "S,xucred", "Get the xucred of a UDP6 connection");
534
535 static int
536 udp6_output(struct inpcb *inp, struct mbuf *m, struct sockaddr *addr6,
537 struct mbuf *control, struct thread *td)
538 {
539 u_int32_t ulen = m->m_pkthdr.len;
540 u_int32_t plen = sizeof(struct udphdr) + ulen;
541 struct ip6_hdr *ip6;
542 struct udphdr *udp6;
543 struct in6_addr *laddr, *faddr, in6a;
544 struct sockaddr_in6 *sin6 = NULL;
545 struct ifnet *oifp = NULL;
546 int scope_ambiguous = 0;
547 u_short fport;
548 int error = 0;
549 struct ip6_pktopts *optp, opt;
550 int af = AF_INET6, hlen = sizeof(struct ip6_hdr);
551 int flags;
552 struct sockaddr_in6 tmp;
553
554 INP_WLOCK_ASSERT(inp);
555
556 if (addr6) {
557 /* addr6 has been validated in udp6_send(). */
558 sin6 = (struct sockaddr_in6 *)addr6;
559
560 /* protect *sin6 from overwrites */
561 tmp = *sin6;
562 sin6 = &tmp;
563
564 /*
565 * Application should provide a proper zone ID or the use of
566 * default zone IDs should be enabled. Unfortunately, some
567 * applications do not behave as it should, so we need a
568 * workaround. Even if an appropriate ID is not determined,
569 * we'll see if we can determine the outgoing interface. If we
570 * can, determine the zone ID based on the interface below.
571 */
572 if (sin6->sin6_scope_id == 0 && !V_ip6_use_defzone)
573 scope_ambiguous = 1;
574 if ((error = sa6_embedscope(sin6, V_ip6_use_defzone)) != 0)
575 return (error);
576 }
577
578 if (control) {
579 if ((error = ip6_setpktopts(control, &opt,
580 inp->in6p_outputopts, td->td_ucred, IPPROTO_UDP)) != 0)
581 goto release;
582 optp = &opt;
583 } else
584 optp = inp->in6p_outputopts;
585
586 if (sin6) {
587 faddr = &sin6->sin6_addr;
588
589 /*
590 * IPv4 version of udp_output calls in_pcbconnect in this case,
591 * which needs splnet and affects performance.
592 * Since we saw no essential reason for calling in_pcbconnect,
593 * we get rid of such kind of logic, and call in6_selectsrc
594 * and in6_pcbsetport in order to fill in the local address
595 * and the local port.
596 */
597 if (sin6->sin6_port == 0) {
598 error = EADDRNOTAVAIL;
599 goto release;
600 }
601
602 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
603 /* how about ::ffff:0.0.0.0 case? */
604 error = EISCONN;
605 goto release;
606 }
607
608 fport = sin6->sin6_port; /* allow 0 port */
609
610 if (IN6_IS_ADDR_V4MAPPED(faddr)) {
611 if ((inp->inp_flags & IN6P_IPV6_V6ONLY)) {
612 /*
613 * I believe we should explicitly discard the
614 * packet when mapped addresses are disabled,
615 * rather than send the packet as an IPv6 one.
616 * If we chose the latter approach, the packet
617 * might be sent out on the wire based on the
618 * default route, the situation which we'd
619 * probably want to avoid.
620 * (20010421 jinmei@kame.net)
621 */
622 error = EINVAL;
623 goto release;
624 }
625 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr) &&
626 !IN6_IS_ADDR_V4MAPPED(&inp->in6p_laddr)) {
627 /*
628 * when remote addr is an IPv4-mapped address,
629 * local addr should not be an IPv6 address,
630 * since you cannot determine how to map IPv6
631 * source address to IPv4.
632 */
633 error = EINVAL;
634 goto release;
635 }
636
637 af = AF_INET;
638 }
639
640 if (!IN6_IS_ADDR_V4MAPPED(faddr)) {
641 error = in6_selectsrc(sin6, optp, inp, NULL,
642 td->td_ucred, &oifp, &in6a);
643 if (error)
644 goto release;
645 if (oifp && scope_ambiguous &&
646 (error = in6_setscope(&sin6->sin6_addr,
647 oifp, NULL))) {
648 goto release;
649 }
650 laddr = &in6a;
651 } else
652 laddr = &inp->in6p_laddr; /* XXX */
653 if (laddr == NULL) {
654 if (error == 0)
655 error = EADDRNOTAVAIL;
656 goto release;
657 }
658 if (inp->inp_lport == 0 &&
659 (error = in6_pcbsetport(laddr, inp, td->td_ucred)) != 0)
660 goto release;
661 } else {
662 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
663 error = ENOTCONN;
664 goto release;
665 }
666 if (IN6_IS_ADDR_V4MAPPED(&inp->in6p_faddr)) {
667 if ((inp->inp_flags & IN6P_IPV6_V6ONLY)) {
668 /*
669 * XXX: this case would happen when the
670 * application sets the V6ONLY flag after
671 * connecting the foreign address.
672 * Such applications should be fixed,
673 * so we bark here.
674 */
675 log(LOG_INFO, "udp6_output: IPV6_V6ONLY "
676 "option was set for a connected socket\n");
677 error = EINVAL;
678 goto release;
679 } else
680 af = AF_INET;
681 }
682 laddr = &inp->in6p_laddr;
683 faddr = &inp->in6p_faddr;
684 fport = inp->inp_fport;
685 }
686
687 if (af == AF_INET)
688 hlen = sizeof(struct ip);
689
690 /*
691 * Calculate data length and get a mbuf
692 * for UDP and IP6 headers.
693 */
694 M_PREPEND(m, hlen + sizeof(struct udphdr), M_DONTWAIT);
695 if (m == 0) {
696 error = ENOBUFS;
697 goto release;
698 }
699
700 /*
701 * Stuff checksum and output datagram.
702 */
703 udp6 = (struct udphdr *)(mtod(m, caddr_t) + hlen);
704 udp6->uh_sport = inp->inp_lport; /* lport is always set in the PCB */
705 udp6->uh_dport = fport;
706 if (plen <= 0xffff)
707 udp6->uh_ulen = htons((u_short)plen);
708 else
709 udp6->uh_ulen = 0;
710 udp6->uh_sum = 0;
711
712 switch (af) {
713 case AF_INET6:
714 ip6 = mtod(m, struct ip6_hdr *);
715 ip6->ip6_flow = inp->inp_flow & IPV6_FLOWINFO_MASK;
716 ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
717 ip6->ip6_vfc |= IPV6_VERSION;
718 #if 0 /* ip6_plen will be filled in ip6_output. */
719 ip6->ip6_plen = htons((u_short)plen);
720 #endif
721 ip6->ip6_nxt = IPPROTO_UDP;
722 ip6->ip6_hlim = in6_selecthlim(inp, NULL);
723 ip6->ip6_src = *laddr;
724 ip6->ip6_dst = *faddr;
725
726 if ((udp6->uh_sum = in6_cksum(m, IPPROTO_UDP,
727 sizeof(struct ip6_hdr), plen)) == 0) {
728 udp6->uh_sum = 0xffff;
729 }
730
731 flags = 0;
732
733 UDPSTAT_INC(udps_opackets);
734 error = ip6_output(m, optp, NULL, flags, inp->in6p_moptions,
735 NULL, inp);
736 break;
737 case AF_INET:
738 error = EAFNOSUPPORT;
739 goto release;
740 }
741 goto releaseopt;
742
743 release:
744 m_freem(m);
745
746 releaseopt:
747 if (control) {
748 ip6_clearpktopts(&opt, -1);
749 m_freem(control);
750 }
751 return (error);
752 }
753
754 static void
755 udp6_abort(struct socket *so)
756 {
757 struct inpcb *inp;
758
759 inp = sotoinpcb(so);
760 KASSERT(inp != NULL, ("udp6_abort: inp == NULL"));
761
762 #ifdef INET
763 if (inp->inp_vflag & INP_IPV4) {
764 struct pr_usrreqs *pru;
765
766 pru = inetsw[ip_protox[IPPROTO_UDP]].pr_usrreqs;
767 (*pru->pru_abort)(so);
768 return;
769 }
770 #endif
771
772 INP_INFO_WLOCK(&V_udbinfo);
773 INP_WLOCK(inp);
774 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
775 in6_pcbdisconnect(inp);
776 inp->in6p_laddr = in6addr_any;
777 soisdisconnected(so);
778 }
779 INP_WUNLOCK(inp);
780 INP_INFO_WUNLOCK(&V_udbinfo);
781 }
782
783 static int
784 udp6_attach(struct socket *so, int proto, struct thread *td)
785 {
786 struct inpcb *inp;
787 int error;
788
789 inp = sotoinpcb(so);
790 KASSERT(inp == NULL, ("udp6_attach: inp != NULL"));
791
792 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
793 error = soreserve(so, udp_sendspace, udp_recvspace);
794 if (error)
795 return (error);
796 }
797 INP_INFO_WLOCK(&V_udbinfo);
798 error = in_pcballoc(so, &V_udbinfo);
799 if (error) {
800 INP_INFO_WUNLOCK(&V_udbinfo);
801 return (error);
802 }
803 inp = (struct inpcb *)so->so_pcb;
804 inp->inp_vflag |= INP_IPV6;
805 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0)
806 inp->inp_vflag |= INP_IPV4;
807 inp->in6p_hops = -1; /* use kernel default */
808 inp->in6p_cksum = -1; /* just to be sure */
809 /*
810 * XXX: ugly!!
811 * IPv4 TTL initialization is necessary for an IPv6 socket as well,
812 * because the socket may be bound to an IPv6 wildcard address,
813 * which may match an IPv4-mapped IPv6 address.
814 */
815 inp->inp_ip_ttl = V_ip_defttl;
816
817 error = udp_newudpcb(inp);
818 if (error) {
819 in_pcbdetach(inp);
820 in_pcbfree(inp);
821 INP_INFO_WUNLOCK(&V_udbinfo);
822 return (error);
823 }
824 INP_WUNLOCK(inp);
825 INP_INFO_WUNLOCK(&V_udbinfo);
826 return (0);
827 }
828
829 static int
830 udp6_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
831 {
832 struct inpcb *inp;
833 int error;
834
835 inp = sotoinpcb(so);
836 KASSERT(inp != NULL, ("udp6_bind: inp == NULL"));
837
838 INP_INFO_WLOCK(&V_udbinfo);
839 INP_WLOCK(inp);
840 inp->inp_vflag &= ~INP_IPV4;
841 inp->inp_vflag |= INP_IPV6;
842 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
843 struct sockaddr_in6 *sin6_p;
844
845 sin6_p = (struct sockaddr_in6 *)nam;
846
847 if (IN6_IS_ADDR_UNSPECIFIED(&sin6_p->sin6_addr))
848 inp->inp_vflag |= INP_IPV4;
849 else if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
850 struct sockaddr_in sin;
851
852 in6_sin6_2_sin(&sin, sin6_p);
853 inp->inp_vflag |= INP_IPV4;
854 inp->inp_vflag &= ~INP_IPV6;
855 error = in_pcbbind(inp, (struct sockaddr *)&sin,
856 td->td_ucred);
857 goto out;
858 }
859 }
860
861 error = in6_pcbbind(inp, nam, td->td_ucred);
862 out:
863 INP_WUNLOCK(inp);
864 INP_INFO_WUNLOCK(&V_udbinfo);
865 return (error);
866 }
867
868 static void
869 udp6_close(struct socket *so)
870 {
871 struct inpcb *inp;
872
873 inp = sotoinpcb(so);
874 KASSERT(inp != NULL, ("udp6_close: inp == NULL"));
875
876 #ifdef INET
877 if (inp->inp_vflag & INP_IPV4) {
878 struct pr_usrreqs *pru;
879
880 pru = inetsw[ip_protox[IPPROTO_UDP]].pr_usrreqs;
881 (*pru->pru_disconnect)(so);
882 return;
883 }
884 #endif
885 INP_INFO_WLOCK(&V_udbinfo);
886 INP_WLOCK(inp);
887 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
888 in6_pcbdisconnect(inp);
889 inp->in6p_laddr = in6addr_any;
890 soisdisconnected(so);
891 }
892 INP_WUNLOCK(inp);
893 INP_INFO_WUNLOCK(&V_udbinfo);
894 }
895
896 static int
897 udp6_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
898 {
899 struct inpcb *inp;
900 struct sockaddr_in6 *sin6;
901 int error;
902
903 inp = sotoinpcb(so);
904 sin6 = (struct sockaddr_in6 *)nam;
905 KASSERT(inp != NULL, ("udp6_connect: inp == NULL"));
906
907 INP_INFO_WLOCK(&V_udbinfo);
908 INP_WLOCK(inp);
909 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0 &&
910 IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
911 struct sockaddr_in sin;
912
913 if (inp->inp_faddr.s_addr != INADDR_ANY) {
914 error = EISCONN;
915 goto out;
916 }
917 in6_sin6_2_sin(&sin, sin6);
918 error = prison_remote_ip4(td->td_ucred, &sin.sin_addr);
919 if (error != 0)
920 goto out;
921 error = in_pcbconnect(inp, (struct sockaddr *)&sin,
922 td->td_ucred);
923 if (error == 0) {
924 inp->inp_vflag |= INP_IPV4;
925 inp->inp_vflag &= ~INP_IPV6;
926 soisconnected(so);
927 }
928 goto out;
929 }
930 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
931 error = EISCONN;
932 goto out;
933 }
934 error = prison_remote_ip6(td->td_ucred, &sin6->sin6_addr);
935 if (error != 0)
936 goto out;
937 error = in6_pcbconnect(inp, nam, td->td_ucred);
938 if (error == 0) {
939 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
940 /* should be non mapped addr */
941 inp->inp_vflag &= ~INP_IPV4;
942 inp->inp_vflag |= INP_IPV6;
943 }
944 soisconnected(so);
945 }
946 out:
947 INP_WUNLOCK(inp);
948 INP_INFO_WUNLOCK(&V_udbinfo);
949 return (error);
950 }
951
952 static void
953 udp6_detach(struct socket *so)
954 {
955 struct inpcb *inp;
956 struct udpcb *up;
957
958 inp = sotoinpcb(so);
959 KASSERT(inp != NULL, ("udp6_detach: inp == NULL"));
960
961 INP_INFO_WLOCK(&V_udbinfo);
962 INP_WLOCK(inp);
963 up = intoudpcb(inp);
964 KASSERT(up != NULL, ("%s: up == NULL", __func__));
965 in_pcbdetach(inp);
966 in_pcbfree(inp);
967 INP_INFO_WUNLOCK(&V_udbinfo);
968 udp_discardcb(up);
969 }
970
971 static int
972 udp6_disconnect(struct socket *so)
973 {
974 struct inpcb *inp;
975 int error;
976
977 inp = sotoinpcb(so);
978 KASSERT(inp != NULL, ("udp6_disconnect: inp == NULL"));
979
980 INP_INFO_WLOCK(&V_udbinfo);
981 INP_WLOCK(inp);
982
983 #ifdef INET
984 if (inp->inp_vflag & INP_IPV4) {
985 struct pr_usrreqs *pru;
986
987 pru = inetsw[ip_protox[IPPROTO_UDP]].pr_usrreqs;
988 error = (*pru->pru_disconnect)(so);
989 goto out;
990 }
991 #endif
992
993 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
994 error = ENOTCONN;
995 goto out;
996 }
997
998 in6_pcbdisconnect(inp);
999 inp->in6p_laddr = in6addr_any;
1000 SOCK_LOCK(so);
1001 so->so_state &= ~SS_ISCONNECTED; /* XXX */
1002 SOCK_UNLOCK(so);
1003 out:
1004 INP_WUNLOCK(inp);
1005 INP_INFO_WUNLOCK(&V_udbinfo);
1006 return (0);
1007 }
1008
1009 static int
1010 udp6_send(struct socket *so, int flags, struct mbuf *m,
1011 struct sockaddr *addr, struct mbuf *control, struct thread *td)
1012 {
1013 struct inpcb *inp;
1014 int error = 0;
1015
1016 inp = sotoinpcb(so);
1017 KASSERT(inp != NULL, ("udp6_send: inp == NULL"));
1018
1019 INP_INFO_WLOCK(&V_udbinfo);
1020 INP_WLOCK(inp);
1021 if (addr) {
1022 if (addr->sa_len != sizeof(struct sockaddr_in6)) {
1023 error = EINVAL;
1024 goto bad;
1025 }
1026 if (addr->sa_family != AF_INET6) {
1027 error = EAFNOSUPPORT;
1028 goto bad;
1029 }
1030 }
1031
1032 #ifdef INET
1033 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
1034 int hasv4addr;
1035 struct sockaddr_in6 *sin6 = 0;
1036
1037 if (addr == 0)
1038 hasv4addr = (inp->inp_vflag & INP_IPV4);
1039 else {
1040 sin6 = (struct sockaddr_in6 *)addr;
1041 hasv4addr = IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)
1042 ? 1 : 0;
1043 }
1044 if (hasv4addr) {
1045 struct pr_usrreqs *pru;
1046
1047 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr) &&
1048 !IN6_IS_ADDR_V4MAPPED(&inp->in6p_laddr)) {
1049 /*
1050 * When remote addr is IPv4-mapped address,
1051 * local addr should not be an IPv6 address;
1052 * since you cannot determine how to map IPv6
1053 * source address to IPv4.
1054 */
1055 error = EINVAL;
1056 goto out;
1057 }
1058
1059 /*
1060 * XXXRW: We release UDP-layer locks before calling
1061 * udp_send() in order to avoid recursion. However,
1062 * this does mean there is a short window where inp's
1063 * fields are unstable. Could this lead to a
1064 * potential race in which the factors causing us to
1065 * select the UDPv4 output routine are invalidated?
1066 */
1067 INP_WUNLOCK(inp);
1068 INP_INFO_WUNLOCK(&V_udbinfo);
1069 if (sin6)
1070 in6_sin6_2_sin_in_sock(addr);
1071 pru = inetsw[ip_protox[IPPROTO_UDP]].pr_usrreqs;
1072 /* addr will just be freed in sendit(). */
1073 return ((*pru->pru_send)(so, flags, m, addr, control,
1074 td));
1075 }
1076 }
1077 #endif
1078 #ifdef MAC
1079 mac_inpcb_create_mbuf(inp, m);
1080 #endif
1081 error = udp6_output(inp, m, addr, control, td);
1082 out:
1083 INP_WUNLOCK(inp);
1084 INP_INFO_WUNLOCK(&V_udbinfo);
1085 return (error);
1086
1087 bad:
1088 INP_WUNLOCK(inp);
1089 INP_INFO_WUNLOCK(&V_udbinfo);
1090 m_freem(m);
1091 return (error);
1092 }
1093
1094 struct pr_usrreqs udp6_usrreqs = {
1095 .pru_abort = udp6_abort,
1096 .pru_attach = udp6_attach,
1097 .pru_bind = udp6_bind,
1098 .pru_connect = udp6_connect,
1099 .pru_control = in6_control,
1100 .pru_detach = udp6_detach,
1101 .pru_disconnect = udp6_disconnect,
1102 .pru_peeraddr = in6_mapped_peeraddr,
1103 .pru_send = udp6_send,
1104 .pru_shutdown = udp_shutdown,
1105 .pru_sockaddr = in6_mapped_sockaddr,
1106 .pru_soreceive = soreceive_dgram,
1107 .pru_sosend = sosend_dgram,
1108 .pru_sosetlabel = in_pcbsosetlabel,
1109 .pru_close = udp6_close
1110 };
Cache object: d3c592ca353a1a4103ebfa2f284b8659
|