udp_usrreq.c revision 1.77 1 /* $NetBSD: udp_usrreq.c,v 1.77 2001/05/31 19:56:13 soda Exp $ */
2
3 /*
4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the project nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32 /*
33 * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995
34 * The Regents of the University of California. All rights reserved.
35 *
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
38 * are met:
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
44 * 3. All advertising materials mentioning features or use of this software
45 * must display the following acknowledgement:
46 * This product includes software developed by the University of
47 * California, Berkeley and its contributors.
48 * 4. Neither the name of the University nor the names of its contributors
49 * may be used to endorse or promote products derived from this software
50 * without specific prior written permission.
51 *
52 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
53 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
54 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
55 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
56 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
57 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
58 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
59 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
60 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
61 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62 * SUCH DAMAGE.
63 *
64 * @(#)udp_usrreq.c 8.6 (Berkeley) 5/23/95
65 */
66
67 #include "opt_inet.h"
68 #include "opt_ipsec.h"
69 #include "opt_ipkdb.h"
70
71 #include <sys/param.h>
72 #include <sys/malloc.h>
73 #include <sys/mbuf.h>
74 #include <sys/protosw.h>
75 #include <sys/socket.h>
76 #include <sys/socketvar.h>
77 #include <sys/errno.h>
78 #include <sys/stat.h>
79 #include <sys/systm.h>
80 #include <sys/proc.h>
81 #include <sys/domain.h>
82
83 #include <uvm/uvm_extern.h>
84 #include <sys/sysctl.h>
85
86 #include <net/if.h>
87 #include <net/route.h>
88
89 #include <netinet/in.h>
90 #include <netinet/in_systm.h>
91 #include <netinet/in_var.h>
92 #include <netinet/ip.h>
93 #include <netinet/in_pcb.h>
94 #include <netinet/ip_var.h>
95 #include <netinet/ip_icmp.h>
96 #include <netinet/udp.h>
97 #include <netinet/udp_var.h>
98
99 #ifdef INET6
100 #include <netinet/ip6.h>
101 #include <netinet/icmp6.h>
102 #include <netinet6/ip6_var.h>
103 #include <netinet6/in6_pcb.h>
104 #include <netinet6/udp6_var.h>
105 #endif
106
107 #ifdef PULLDOWN_TEST
108 #ifndef INET6
109 /* always need ip6.h for IP6_EXTHDR_GET */
110 #include <netinet/ip6.h>
111 #endif
112 #endif
113
114 #include "faith.h"
115 #if defined(NFAITH) && NFAITH > 0
116 #include <net/if_faith.h>
117 #endif
118
119 #include <machine/stdarg.h>
120
121 #ifdef IPSEC
122 #include <netinet6/ipsec.h>
123 #include <netkey/key.h>
124 #endif /*IPSEC*/
125
126 #ifdef IPKDB
127 #include <ipkdb/ipkdb.h>
128 #endif
129
130 /*
131 * UDP protocol implementation.
132 * Per RFC 768, August, 1980.
133 */
134 #ifndef COMPAT_42
135 int udpcksum = 1;
136 #else
137 int udpcksum = 0; /* XXX */
138 #endif
139
140 #ifdef INET
141 static void udp4_sendup __P((struct mbuf *, int, struct sockaddr *,
142 struct socket *));
143 static int udp4_realinput __P((struct sockaddr_in *, struct sockaddr_in *,
144 struct mbuf *, int));
145 #endif
146 #ifdef INET6
147 static void udp6_sendup __P((struct mbuf *, int, struct sockaddr *,
148 struct socket *));
149 static int in6_mcmatch __P((struct in6pcb *, struct in6_addr *,
150 struct ifnet *));
151 static int udp6_realinput __P((int, struct sockaddr_in6 *,
152 struct sockaddr_in6 *, struct mbuf *, int));
153 #endif
154 #ifdef INET
155 static void udp_notify __P((struct inpcb *, int));
156 #endif
157
158 #ifndef UDBHASHSIZE
159 #define UDBHASHSIZE 128
160 #endif
161 int udbhashsize = UDBHASHSIZE;
162
163 void
164 udp_init()
165 {
166
167 #ifdef INET
168 in_pcbinit(&udbtable, udbhashsize, udbhashsize);
169 #endif
170 }
171
172 #ifndef UDP6
173 #ifdef INET
174 void
175 #if __STDC__
176 udp_input(struct mbuf *m, ...)
177 #else
178 udp_input(m, va_alist)
179 struct mbuf *m;
180 va_dcl
181 #endif
182 {
183 va_list ap;
184 struct sockaddr_in src, dst;
185 struct ip *ip;
186 struct udphdr *uh;
187 int iphlen, proto;
188 int len;
189 int n;
190
191 va_start(ap, m);
192 iphlen = va_arg(ap, int);
193 proto = va_arg(ap, int);
194 va_end(ap);
195
196 udpstat.udps_ipackets++;
197
198 #ifndef PULLDOWN_TEST
199 /*
200 * Strip IP options, if any; should skip this,
201 * make available to user, and use on returned packets,
202 * but we don't yet have a way to check the checksum
203 * with options still present.
204 */
205 if (iphlen > sizeof (struct ip)) {
206 ip_stripoptions(m, (struct mbuf *)0);
207 iphlen = sizeof(struct ip);
208 }
209 #else
210 /*
211 * we may enable the above code if we save and pass IPv4 options
212 * to the userland.
213 */
214 #endif
215
216 /*
217 * Get IP and UDP header together in first mbuf.
218 */
219 ip = mtod(m, struct ip *);
220 #ifndef PULLDOWN_TEST
221 if (m->m_len < iphlen + sizeof(struct udphdr)) {
222 if ((m = m_pullup(m, iphlen + sizeof(struct udphdr))) == 0) {
223 udpstat.udps_hdrops++;
224 return;
225 }
226 ip = mtod(m, struct ip *);
227 }
228 uh = (struct udphdr *)((caddr_t)ip + iphlen);
229 #else
230 IP6_EXTHDR_GET(uh, struct udphdr *, m, iphlen, sizeof(struct udphdr));
231 if (uh == NULL) {
232 udpstat.udps_hdrops++;
233 return;
234 }
235 #endif
236
237 /* destination port of 0 is illegal, based on RFC768. */
238 if (uh->uh_dport == 0)
239 goto bad;
240
241 /*
242 * Make mbuf data length reflect UDP length.
243 * If not enough data to reflect UDP length, drop.
244 */
245 len = ntohs((u_int16_t)uh->uh_ulen);
246 if (ip->ip_len != iphlen + len) {
247 if (ip->ip_len < iphlen + len || len < sizeof(struct udphdr)) {
248 udpstat.udps_badlen++;
249 goto bad;
250 }
251 m_adj(m, iphlen + len - ip->ip_len);
252 }
253
254 /*
255 * Checksum extended UDP header and data.
256 */
257 if (uh->uh_sum) {
258 if (in4_cksum(m, IPPROTO_UDP, iphlen, len) != 0) {
259 udpstat.udps_badsum++;
260 m_freem(m);
261 return;
262 }
263 }
264
265 /* construct source and dst sockaddrs. */
266 bzero(&src, sizeof(src));
267 src.sin_family = AF_INET;
268 src.sin_len = sizeof(struct sockaddr_in);
269 bcopy(&ip->ip_src, &src.sin_addr, sizeof(src.sin_addr));
270 src.sin_port = uh->uh_sport;
271 bzero(&dst, sizeof(dst));
272 dst.sin_family = AF_INET;
273 dst.sin_len = sizeof(struct sockaddr_in);
274 bcopy(&ip->ip_dst, &dst.sin_addr, sizeof(dst.sin_addr));
275 dst.sin_port = uh->uh_dport;
276
277 n = udp4_realinput(&src, &dst, m, iphlen);
278 #ifdef INET6
279 if (IN_MULTICAST(ip->ip_dst.s_addr) || n == 0) {
280 struct sockaddr_in6 src6, dst6;
281
282 bzero(&src6, sizeof(src6));
283 src6.sin6_family = AF_INET6;
284 src6.sin6_len = sizeof(struct sockaddr_in6);
285 src6.sin6_addr.s6_addr[10] = src6.sin6_addr.s6_addr[11] = 0xff;
286 bcopy(&ip->ip_src, &src6.sin6_addr.s6_addr[12],
287 sizeof(ip->ip_src));
288 src6.sin6_port = uh->uh_sport;
289 bzero(&dst6, sizeof(dst6));
290 dst6.sin6_family = AF_INET6;
291 dst6.sin6_len = sizeof(struct sockaddr_in6);
292 dst6.sin6_addr.s6_addr[10] = dst6.sin6_addr.s6_addr[11] = 0xff;
293 bcopy(&ip->ip_dst, &dst6.sin6_addr.s6_addr[12],
294 sizeof(ip->ip_dst));
295 dst6.sin6_port = uh->uh_dport;
296
297 n += udp6_realinput(AF_INET, &src6, &dst6, m, iphlen);
298 }
299 #endif
300
301 if (n == 0) {
302 if (m->m_flags & (M_BCAST | M_MCAST)) {
303 udpstat.udps_noportbcast++;
304 goto bad;
305 }
306 udpstat.udps_noport++;
307 #ifdef IPKDB
308 if (checkipkdb(&ip->ip_src, uh->uh_sport, uh->uh_dport,
309 m, iphlen + sizeof(struct udphdr),
310 m->m_pkthdr.len - iphlen - sizeof(struct udphdr))) {
311 /*
312 * It was a debugger connect packet,
313 * just drop it now
314 */
315 goto bad;
316 }
317 #endif
318 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PORT, 0, 0);
319 m = NULL;
320 }
321
322 bad:
323 if (m)
324 m_freem(m);
325 }
326 #endif
327
328 #ifdef INET6
329 int
330 udp6_input(mp, offp, proto)
331 struct mbuf **mp;
332 int *offp, proto;
333 {
334 struct mbuf *m = *mp;
335 int off = *offp;
336 struct sockaddr_in6 src, dst;
337 struct ip6_hdr *ip6;
338 struct udphdr *uh;
339 u_int32_t plen, ulen;
340
341 #ifndef PULLDOWN_TEST
342 IP6_EXTHDR_CHECK(m, off, sizeof(struct udphdr), IPPROTO_DONE);
343 #endif
344 ip6 = mtod(m, struct ip6_hdr *);
345
346 #if defined(NFAITH) && 0 < NFAITH
347 if (faithprefix(&ip6->ip6_dst)) {
348 /* send icmp6 host unreach? */
349 m_freem(m);
350 return IPPROTO_DONE;
351 }
352 #endif
353
354 udp6stat.udp6s_ipackets++;
355
356 /* check for jumbogram is done in ip6_input. we can trust pkthdr.len */
357 plen = m->m_pkthdr.len - off;
358 #ifndef PULLDOWN_TEST
359 uh = (struct udphdr *)((caddr_t)ip6 + off);
360 #else
361 IP6_EXTHDR_GET(uh, struct udphdr *, m, off, sizeof(struct udphdr));
362 if (uh == NULL) {
363 ip6stat.ip6s_tooshort++;
364 return IPPROTO_DONE;
365 }
366 #endif
367 ulen = ntohs((u_short)uh->uh_ulen);
368 /*
369 * RFC2675 section 4: jumbograms will have 0 in the UDP header field,
370 * iff payload length > 0xffff.
371 */
372 if (ulen == 0 && plen > 0xffff)
373 ulen = plen;
374
375 if (plen != ulen) {
376 udp6stat.udp6s_badlen++;
377 goto bad;
378 }
379
380 /* destination port of 0 is illegal, based on RFC768. */
381 if (uh->uh_dport == 0)
382 goto bad;
383
384 /* Be proactive about malicious use of IPv4 mapped address */
385 if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) ||
386 IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) {
387 /* XXX stat */
388 goto bad;
389 }
390
391 /*
392 * Checksum extended UDP header and data.
393 */
394 if (uh->uh_sum == 0)
395 udp6stat.udp6s_nosum++;
396 else if (in6_cksum(m, IPPROTO_UDP, off, ulen) != 0) {
397 udp6stat.udp6s_badsum++;
398 goto bad;
399 }
400
401 /*
402 * Construct source and dst sockaddrs.
403 * Note that ifindex (s6_addr16[1]) is already filled.
404 */
405 bzero(&src, sizeof(src));
406 src.sin6_family = AF_INET6;
407 src.sin6_len = sizeof(struct sockaddr_in6);
408 /* KAME hack: recover scopeid */
409 (void)in6_recoverscope(&src, &ip6->ip6_src, m->m_pkthdr.rcvif);
410 src.sin6_port = uh->uh_sport;
411 bzero(&dst, sizeof(dst));
412 dst.sin6_family = AF_INET6;
413 dst.sin6_len = sizeof(struct sockaddr_in6);
414 /* KAME hack: recover scopeid */
415 (void)in6_recoverscope(&dst, &ip6->ip6_dst, m->m_pkthdr.rcvif);
416 dst.sin6_port = uh->uh_dport;
417
418 if (udp6_realinput(AF_INET6, &src, &dst, m, off) == 0) {
419 if (m->m_flags & M_MCAST) {
420 udp6stat.udp6s_noportmcast++;
421 goto bad;
422 }
423 udp6stat.udp6s_noport++;
424 icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_NOPORT, 0);
425 m = NULL;
426 }
427
428 bad:
429 if (m)
430 m_freem(m);
431 return IPPROTO_DONE;
432 }
433 #endif
434
435 #ifdef INET
436 static void
437 udp4_sendup(m, off, src, so)
438 struct mbuf *m;
439 int off; /* offset of data portion */
440 struct sockaddr *src;
441 struct socket *so;
442 {
443 struct mbuf *opts = NULL;
444 struct mbuf *n;
445 struct inpcb *inp = NULL;
446 #ifdef INET6
447 struct in6pcb *in6p = NULL;
448 #endif
449
450 if (!so)
451 return;
452 switch (so->so_proto->pr_domain->dom_family) {
453 case AF_INET:
454 inp = sotoinpcb(so);
455 break;
456 #ifdef INET6
457 case AF_INET6:
458 in6p = sotoin6pcb(so);
459 break;
460 #endif
461 default:
462 return;
463 }
464
465 #ifdef IPSEC
466 /* check AH/ESP integrity. */
467 if (so != NULL && ipsec4_in_reject_so(m, so)) {
468 ipsecstat.in_polvio++;
469 return;
470 }
471 #endif /*IPSEC*/
472
473 if ((n = m_copy(m, 0, M_COPYALL)) != NULL) {
474 if (inp && (inp->inp_flags & INP_CONTROLOPTS
475 || so->so_options & SO_TIMESTAMP)) {
476 struct ip *ip = mtod(n, struct ip *);
477 ip_savecontrol(inp, &opts, ip, n);
478 }
479
480 m_adj(n, off);
481 if (sbappendaddr(&so->so_rcv, src, n,
482 opts) == 0) {
483 m_freem(n);
484 if (opts)
485 m_freem(opts);
486 } else
487 sorwakeup(so);
488 }
489 }
490 #endif
491
492 #ifdef INET6
493 static void
494 udp6_sendup(m, off, src, so)
495 struct mbuf *m;
496 int off; /* offset of data portion */
497 struct sockaddr *src;
498 struct socket *so;
499 {
500 struct mbuf *opts = NULL;
501 struct mbuf *n;
502 struct in6pcb *in6p = NULL;
503
504 if (!so)
505 return;
506 if (so->so_proto->pr_domain->dom_family != AF_INET6)
507 return;
508 in6p = sotoin6pcb(so);
509
510 #ifdef IPSEC
511 /* check AH/ESP integrity. */
512 if (so != NULL && ipsec6_in_reject_so(m, so)) {
513 ipsec6stat.in_polvio++;
514 return;
515 }
516 #endif /*IPSEC*/
517
518 if ((n = m_copy(m, 0, M_COPYALL)) != NULL) {
519 if (in6p && (in6p->in6p_flags & IN6P_CONTROLOPTS
520 || in6p->in6p_socket->so_options & SO_TIMESTAMP)) {
521 struct ip6_hdr *ip6 = mtod(n, struct ip6_hdr *);
522 ip6_savecontrol(in6p, &opts, ip6, n);
523 }
524
525 m_adj(n, off);
526 if (sbappendaddr(&so->so_rcv, src, n, opts) == 0) {
527 m_freem(n);
528 if (opts)
529 m_freem(opts);
530 udp6stat.udp6s_fullsock++;
531 } else
532 sorwakeup(so);
533 }
534 }
535 #endif
536
537 #ifdef INET
538 static int
539 udp4_realinput(src, dst, m, off)
540 struct sockaddr_in *src;
541 struct sockaddr_in *dst;
542 struct mbuf *m;
543 int off; /* offset of udphdr */
544 {
545 u_int16_t *sport, *dport;
546 int rcvcnt;
547 struct in_addr *src4, *dst4;
548 struct inpcb *inp;
549
550 rcvcnt = 0;
551 off += sizeof(struct udphdr); /* now, offset of payload */
552
553 if (src->sin_family != AF_INET || dst->sin_family != AF_INET)
554 goto bad;
555
556 src4 = &src->sin_addr;
557 sport = &src->sin_port;
558 dst4 = &dst->sin_addr;
559 dport = &dst->sin_port;
560
561 if (IN_MULTICAST(dst4->s_addr) ||
562 in_broadcast(*dst4, m->m_pkthdr.rcvif)) {
563 struct inpcb *last;
564 /*
565 * Deliver a multicast or broadcast datagram to *all* sockets
566 * for which the local and remote addresses and ports match
567 * those of the incoming datagram. This allows more than
568 * one process to receive multi/broadcasts on the same port.
569 * (This really ought to be done for unicast datagrams as
570 * well, but that would cause problems with existing
571 * applications that open both address-specific sockets and
572 * a wildcard socket listening to the same port -- they would
573 * end up receiving duplicates of every unicast datagram.
574 * Those applications open the multiple sockets to overcome an
575 * inadequacy of the UDP socket interface, but for backwards
576 * compatibility we avoid the problem here rather than
577 * fixing the interface. Maybe 4.5BSD will remedy this?)
578 */
579
580 /*
581 * KAME note: usually we drop udpiphdr from mbuf here.
582 * we need udpiphdr for IPsec processing so we do that later.
583 */
584 /*
585 * Locate pcb(s) for datagram.
586 */
587 for (inp = udbtable.inpt_queue.cqh_first;
588 inp != (struct inpcb *)&udbtable.inpt_queue;
589 inp = inp->inp_queue.cqe_next) {
590 if (inp->inp_lport != *dport)
591 continue;
592 if (!in_nullhost(inp->inp_laddr)) {
593 if (!in_hosteq(inp->inp_laddr, *dst4))
594 continue;
595 }
596 if (!in_nullhost(inp->inp_faddr)) {
597 if (!in_hosteq(inp->inp_faddr, *src4) ||
598 inp->inp_fport != *sport)
599 continue;
600 }
601
602 last = inp;
603 udp4_sendup(m, off, (struct sockaddr *)src,
604 inp->inp_socket);
605 rcvcnt++;
606
607 /*
608 * Don't look for additional matches if this one does
609 * not have either the SO_REUSEPORT or SO_REUSEADDR
610 * socket options set. This heuristic avoids searching
611 * through all pcbs in the common case of a non-shared
612 * port. It assumes that an application will never
613 * clear these options after setting them.
614 */
615 if ((inp->inp_socket->so_options &
616 (SO_REUSEPORT|SO_REUSEADDR)) == 0)
617 break;
618 }
619
620 #if 0
621 if (last == NULL) {
622 /*
623 * No matching pcb found; discard datagram.
624 * (No need to send an ICMP Port Unreachable
625 * for a broadcast or multicast datgram.)
626 */
627 udpstat.udps_noportbcast++;
628 goto bad;
629 }
630 #endif
631 } else {
632 /*
633 * Locate pcb for datagram.
634 */
635 inp = in_pcblookup_connect(&udbtable, *src4, *sport, *dst4, *dport);
636 if (inp == 0) {
637 ++udpstat.udps_pcbhashmiss;
638 inp = in_pcblookup_bind(&udbtable, *dst4, *dport);
639 if (inp == 0) {
640 #if 0
641 struct mbuf *n;
642
643 if (m->m_flags & (M_BCAST | M_MCAST)) {
644 udpstat.udps_noportbcast++;
645 goto bad;
646 }
647 udpstat.udps_noport++;
648 #ifdef IPKDB
649 if (checkipkdb(src4, *sport, *dport, m, off,
650 m->m_pkthdr.len - off)) {
651 /*
652 * It was a debugger connect packet,
653 * just drop it now
654 */
655 goto bad;
656 }
657 #endif
658 if ((n = m_copy(m, 0, M_COPYALL)) != NULL) {
659 icmp_error(n, ICMP_UNREACH,
660 ICMP_UNREACH_PORT, 0, 0);
661 }
662 #endif
663 return rcvcnt;
664 }
665 }
666
667 udp4_sendup(m, off, (struct sockaddr *)src, inp->inp_socket);
668 rcvcnt++;
669 }
670
671 bad:
672 return rcvcnt;
673 }
674 #endif
675
676 #ifdef INET6
677 static int
678 in6_mcmatch(in6p, ia6, ifp)
679 struct in6pcb *in6p;
680 struct in6_addr *ia6;
681 struct ifnet *ifp;
682 {
683 struct ip6_moptions *im6o = in6p->in6p_moptions;
684 struct in6_multi_mship *imm;
685
686 if (im6o == NULL)
687 return 0;
688
689 for (imm = im6o->im6o_memberships.lh_first; imm != NULL;
690 imm = imm->i6mm_chain.le_next) {
691 if ((ifp == NULL ||
692 imm->i6mm_maddr->in6m_ifp == ifp) &&
693 IN6_ARE_ADDR_EQUAL(&imm->i6mm_maddr->in6m_addr,
694 ia6))
695 return 1;
696 }
697 return 0;
698 }
699
700 static int
701 udp6_realinput(af, src, dst, m, off)
702 int af; /* af on packet */
703 struct sockaddr_in6 *src;
704 struct sockaddr_in6 *dst;
705 struct mbuf *m;
706 int off; /* offset of udphdr */
707 {
708 u_int16_t *sport, *dport;
709 int rcvcnt;
710 struct in6_addr *src6, *dst6;
711 struct in_addr *dst4;
712 struct in6pcb *in6p;
713
714 rcvcnt = 0;
715 off += sizeof(struct udphdr); /* now, offset of payload */
716
717 if (af != AF_INET && af != AF_INET6)
718 goto bad;
719 if (src->sin6_family != AF_INET6 || dst->sin6_family != AF_INET6)
720 goto bad;
721
722 src6 = &src->sin6_addr;
723 sport = &src->sin6_port;
724 dst6 = &dst->sin6_addr;
725 dport = &dst->sin6_port;
726 dst4 = (struct in_addr *)&dst->sin6_addr.s6_addr32[12];
727
728 if (IN6_IS_ADDR_MULTICAST(dst6)
729 || (af == AF_INET && IN_MULTICAST(dst4->s_addr))) {
730 struct in6pcb *last;
731 /*
732 * Deliver a multicast or broadcast datagram to *all* sockets
733 * for which the local and remote addresses and ports match
734 * those of the incoming datagram. This allows more than
735 * one process to receive multi/broadcasts on the same port.
736 * (This really ought to be done for unicast datagrams as
737 * well, but that would cause problems with existing
738 * applications that open both address-specific sockets and
739 * a wildcard socket listening to the same port -- they would
740 * end up receiving duplicates of every unicast datagram.
741 * Those applications open the multiple sockets to overcome an
742 * inadequacy of the UDP socket interface, but for backwards
743 * compatibility we avoid the problem here rather than
744 * fixing the interface. Maybe 4.5BSD will remedy this?)
745 */
746
747 /*
748 * KAME note: usually we drop udpiphdr from mbuf here.
749 * we need udpiphdr for IPsec processing so we do that later.
750 */
751 /*
752 * Locate pcb(s) for datagram.
753 */
754 for (in6p = udb6.in6p_next; in6p != &udb6;
755 in6p = in6p->in6p_next) {
756 if (in6p->in6p_lport != *dport)
757 continue;
758 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) {
759 if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, dst6)
760 && !in6_mcmatch(in6p, dst6, m->m_pkthdr.rcvif))
761 continue;
762 }
763 #ifndef INET6_BINDV6ONLY
764 else {
765 if (IN6_IS_ADDR_V4MAPPED(dst6)
766 && (in6p->in6p_flags & IN6P_BINDV6ONLY))
767 continue;
768 }
769 #endif
770 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
771 if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, src6)
772 || in6p->in6p_fport != *sport)
773 continue;
774 }
775 #ifndef INET6_BINDV6ONLY
776 else {
777 if (IN6_IS_ADDR_V4MAPPED(src6)
778 && (in6p->in6p_flags & IN6P_BINDV6ONLY))
779 continue;
780 }
781 #endif
782
783 last = in6p;
784 udp6_sendup(m, off, (struct sockaddr *)src,
785 in6p->in6p_socket);
786 rcvcnt++;
787
788 /*
789 * Don't look for additional matches if this one does
790 * not have either the SO_REUSEPORT or SO_REUSEADDR
791 * socket options set. This heuristic avoids searching
792 * through all pcbs in the common case of a non-shared
793 * port. It assumes that an application will never
794 * clear these options after setting them.
795 */
796 if ((in6p->in6p_socket->so_options &
797 (SO_REUSEPORT|SO_REUSEADDR)) == 0)
798 break;
799 }
800
801 #if 0
802 if (last == NULL) {
803 /*
804 * No matching pcb found; discard datagram.
805 * (No need to send an ICMP Port Unreachable
806 * for a broadcast or multicast datgram.)
807 */
808 switch (af) {
809 case AF_INET:
810 udpstat.udps_noportbcast++;
811 break;
812 case AF_INET6:
813 udp6stat.udp6s_noportmcast++;
814 break;
815 }
816 goto bad;
817 }
818 #endif
819 } else {
820 /*
821 * Locate pcb for datagram.
822 */
823 in6p = in6_pcblookup_connect(&udb6, src6, *sport,
824 dst6, *dport, 0);
825 if (in6p == 0) {
826 ++udpstat.udps_pcbhashmiss;
827 in6p = in6_pcblookup_bind(&udb6, dst6, *dport, 0);
828 if (in6p == 0) {
829 #if 0
830 struct mbuf *n;
831 n = m_copy(m, 0, M_COPYALL);
832 switch (af) {
833 case AF_INET:
834 if (m->m_flags & (M_BCAST | M_MCAST)) {
835 udpstat.udps_noportbcast++;
836 goto bad;
837 }
838 udpstat.udps_noport++;
839 if (n != NULL)
840 icmp_error(n, ICMP_UNREACH,
841 ICMP_UNREACH_PORT, 0, 0);
842 break;
843 case AF_INET6:
844 if (m->m_flags & M_MCAST) {
845 udp6stat.udp6s_noportmcast++;
846 goto bad;
847 }
848 udp6stat.udp6s_noport++;
849 if (n != NULL)
850 icmp6_error(n, ICMP6_DST_UNREACH,
851 ICMP6_DST_UNREACH_NOPORT, 0);
852 break;
853 }
854 #endif
855
856 return rcvcnt;
857 }
858 }
859
860 udp6_sendup(m, off, (struct sockaddr *)src, in6p->in6p_socket);
861 rcvcnt++;
862 }
863
864 bad:
865 return rcvcnt;
866 }
867 #endif
868
869 #else /*UDP6*/
870
871 void
872 #if __STDC__
873 udp_input(struct mbuf *m, ...)
874 #else
875 udp_input(m, va_alist)
876 struct mbuf *m;
877 va_dcl
878 #endif
879 {
880 int proto;
881 struct ip *ip;
882 struct udphdr *uh;
883 struct inpcb *inp;
884 struct mbuf *opts = 0;
885 int len;
886 struct ip save_ip;
887 int iphlen;
888 va_list ap;
889 struct sockaddr_in udpsrc;
890 struct sockaddr *sa;
891
892 va_start(ap, m);
893 iphlen = va_arg(ap, int);
894 proto = va_arg(ap, int);
895 va_end(ap);
896
897 udpstat.udps_ipackets++;
898
899 /*
900 * Strip IP options, if any; should skip this,
901 * make available to user, and use on returned packets,
902 * but we don't yet have a way to check the checksum
903 * with options still present.
904 */
905 if (iphlen > sizeof (struct ip)) {
906 ip_stripoptions(m, (struct mbuf *)0);
907 iphlen = sizeof(struct ip);
908 }
909
910 /*
911 * Get IP and UDP header together in first mbuf.
912 */
913 ip = mtod(m, struct ip *);
914 if (m->m_len < iphlen + sizeof(struct udphdr)) {
915 if ((m = m_pullup(m, iphlen + sizeof(struct udphdr))) == 0) {
916 udpstat.udps_hdrops++;
917 return;
918 }
919 ip = mtod(m, struct ip *);
920 }
921 uh = (struct udphdr *)((caddr_t)ip + iphlen);
922
923 /* destination port of 0 is illegal, based on RFC768. */
924 if (uh->uh_dport == 0)
925 goto bad;
926
927 /*
928 * Make mbuf data length reflect UDP length.
929 * If not enough data to reflect UDP length, drop.
930 */
931 len = ntohs((u_int16_t)uh->uh_ulen);
932 if (ip->ip_len != iphlen + len) {
933 if (ip->ip_len < iphlen + len || len < sizeof(struct udphdr)) {
934 udpstat.udps_badlen++;
935 goto bad;
936 }
937 m_adj(m, iphlen + len - ip->ip_len);
938 }
939 /*
940 * Save a copy of the IP header in case we want restore it
941 * for sending an ICMP error message in response.
942 */
943 save_ip = *ip;
944
945 /*
946 * Checksum extended UDP header and data.
947 */
948 if (uh->uh_sum) {
949 bzero(((struct ipovly *)ip)->ih_x1,
950 sizeof ((struct ipovly *)ip)->ih_x1);
951 ((struct ipovly *)ip)->ih_len = uh->uh_ulen;
952 if (in_cksum(m, len + sizeof (struct ip)) != 0) {
953 udpstat.udps_badsum++;
954 m_freem(m);
955 return;
956 }
957 }
958
959 /*
960 * Construct sockaddr format source address.
961 */
962 udpsrc.sin_family = AF_INET;
963 udpsrc.sin_len = sizeof(struct sockaddr_in);
964 udpsrc.sin_addr = ip->ip_src;
965 udpsrc.sin_port = uh->uh_sport;
966 bzero((caddr_t)udpsrc.sin_zero, sizeof(udpsrc.sin_zero));
967
968 if (IN_MULTICAST(ip->ip_dst.s_addr) ||
969 in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) {
970 struct inpcb *last;
971 /*
972 * Deliver a multicast or broadcast datagram to *all* sockets
973 * for which the local and remote addresses and ports match
974 * those of the incoming datagram. This allows more than
975 * one process to receive multi/broadcasts on the same port.
976 * (This really ought to be done for unicast datagrams as
977 * well, but that would cause problems with existing
978 * applications that open both address-specific sockets and
979 * a wildcard socket listening to the same port -- they would
980 * end up receiving duplicates of every unicast datagram.
981 * Those applications open the multiple sockets to overcome an
982 * inadequacy of the UDP socket interface, but for backwards
983 * compatibility we avoid the problem here rather than
984 * fixing the interface. Maybe 4.5BSD will remedy this?)
985 */
986
987 iphlen += sizeof(struct udphdr);
988 /*
989 * KAME note: usually we drop udpiphdr from mbuf here.
990 * we need udpiphdr for IPsec processing so we do that later.
991 */
992 /*
993 * Locate pcb(s) for datagram.
994 * (Algorithm copied from raw_intr().)
995 */
996 last = NULL;
997 for (inp = udbtable.inpt_queue.cqh_first;
998 inp != (struct inpcb *)&udbtable.inpt_queue;
999 inp = inp->inp_queue.cqe_next) {
1000 if (inp->inp_lport != uh->uh_dport)
1001 continue;
1002 if (!in_nullhost(inp->inp_laddr)) {
1003 if (!in_hosteq(inp->inp_laddr, ip->ip_dst))
1004 continue;
1005 }
1006 if (!in_nullhost(inp->inp_faddr)) {
1007 if (!in_hosteq(inp->inp_faddr, ip->ip_src) ||
1008 inp->inp_fport != uh->uh_sport)
1009 continue;
1010 }
1011
1012 if (last != NULL) {
1013 struct mbuf *n;
1014
1015 #ifdef IPSEC
1016 /* check AH/ESP integrity. */
1017 if (last != NULL && ipsec4_in_reject(m, last)) {
1018 ipsecstat.in_polvio++;
1019 /* do not inject data to pcb */
1020 } else
1021 #endif /*IPSEC*/
1022 if ((n = m_copy(m, 0, M_COPYALL)) != NULL) {
1023 if (last->inp_flags & INP_CONTROLOPTS
1024 || last->inp_socket->so_options &
1025 SO_TIMESTAMP) {
1026 ip_savecontrol(last, &opts,
1027 ip, n);
1028 }
1029 m_adj(n, iphlen);
1030 sa = (struct sockaddr *)&udpsrc;
1031 if (sbappendaddr(
1032 &last->inp_socket->so_rcv,
1033 sa, n, opts) == 0) {
1034 m_freem(n);
1035 if (opts)
1036 m_freem(opts);
1037 } else
1038 sorwakeup(last->inp_socket);
1039 opts = 0;
1040 }
1041 }
1042 last = inp;
1043 /*
1044 * Don't look for additional matches if this one does
1045 * not have either the SO_REUSEPORT or SO_REUSEADDR
1046 * socket options set. This heuristic avoids searching
1047 * through all pcbs in the common case of a non-shared
1048 * port. It * assumes that an application will never
1049 * clear these options after setting them.
1050 */
1051 if ((last->inp_socket->so_options &
1052 (SO_REUSEPORT|SO_REUSEADDR)) == 0)
1053 break;
1054 }
1055
1056 if (last == NULL) {
1057 /*
1058 * No matching pcb found; discard datagram.
1059 * (No need to send an ICMP Port Unreachable
1060 * for a broadcast or multicast datgram.)
1061 */
1062 udpstat.udps_noportbcast++;
1063 goto bad;
1064 }
1065 #ifdef IPSEC
1066 /* check AH/ESP integrity. */
1067 if (last != NULL && ipsec4_in_reject(m, last)) {
1068 ipsecstat.in_polvio++;
1069 goto bad;
1070 }
1071 #endif /*IPSEC*/
1072 if (last->inp_flags & INP_CONTROLOPTS ||
1073 last->inp_socket->so_options & SO_TIMESTAMP)
1074 ip_savecontrol(last, &opts, ip, m);
1075 m->m_len -= iphlen;
1076 m->m_pkthdr.len -= iphlen;
1077 m->m_data += iphlen;
1078 sa = (struct sockaddr *)&udpsrc;
1079 if (sbappendaddr(&last->inp_socket->so_rcv, sa, m, opts) == 0) {
1080 udpstat.udps_fullsock++;
1081 goto bad;
1082 }
1083 sorwakeup(last->inp_socket);
1084 return;
1085 }
1086 /*
1087 * Locate pcb for datagram.
1088 */
1089 inp = in_pcblookup_connect(&udbtable, ip->ip_src, uh->uh_sport,
1090 ip->ip_dst, uh->uh_dport);
1091 if (inp == 0) {
1092 ++udpstat.udps_pcbhashmiss;
1093 inp = in_pcblookup_bind(&udbtable, ip->ip_dst, uh->uh_dport);
1094 if (inp == 0) {
1095 if (m->m_flags & (M_BCAST | M_MCAST)) {
1096 udpstat.udps_noportbcast++;
1097 goto bad;
1098 }
1099 udpstat.udps_noport++;
1100 *ip = save_ip;
1101 #ifdef IPKDB
1102 if (checkipkdb(&ip->ip_src,
1103 uh->uh_sport,
1104 uh->uh_dport,
1105 m,
1106 iphlen + sizeof(struct udphdr),
1107 len - sizeof(struct udphdr)))
1108 /* It was a debugger connect packet, just drop it now */
1109 goto bad;
1110 #endif
1111 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PORT, 0, 0);
1112 return;
1113 }
1114 }
1115 #ifdef IPSEC
1116 if (inp != NULL && ipsec4_in_reject(m, inp)) {
1117 ipsecstat.in_polvio++;
1118 goto bad;
1119 }
1120 #endif /*IPSEC*/
1121
1122 /*
1123 * Stuff source address and datagram in user buffer.
1124 */
1125 if (inp->inp_flags & INP_CONTROLOPTS ||
1126 inp->inp_socket->so_options & SO_TIMESTAMP)
1127 ip_savecontrol(inp, &opts, ip, m);
1128 iphlen += sizeof(struct udphdr);
1129 m->m_len -= iphlen;
1130 m->m_pkthdr.len -= iphlen;
1131 m->m_data += iphlen;
1132 sa = (struct sockaddr *)&udpsrc;
1133 if (sbappendaddr(&inp->inp_socket->so_rcv, sa, m, opts) == 0) {
1134 udpstat.udps_fullsock++;
1135 goto bad;
1136 }
1137 sorwakeup(inp->inp_socket);
1138 return;
1139 bad:
1140 m_freem(m);
1141 if (opts)
1142 m_freem(opts);
1143 }
1144 #endif /*UDP6*/
1145
1146 #ifdef INET
1147 /*
1148 * Notify a udp user of an asynchronous error;
1149 * just wake up so that he can collect error status.
1150 */
1151 static void
1152 udp_notify(inp, errno)
1153 struct inpcb *inp;
1154 int errno;
1155 {
1156
1157 inp->inp_socket->so_error = errno;
1158 sorwakeup(inp->inp_socket);
1159 sowwakeup(inp->inp_socket);
1160 }
1161
1162 void *
1163 udp_ctlinput(cmd, sa, v)
1164 int cmd;
1165 struct sockaddr *sa;
1166 void *v;
1167 {
1168 struct ip *ip = v;
1169 struct udphdr *uh;
1170 void (*notify) __P((struct inpcb *, int)) = udp_notify;
1171 int errno;
1172
1173 if (sa->sa_family != AF_INET
1174 || sa->sa_len != sizeof(struct sockaddr_in))
1175 return NULL;
1176 if ((unsigned)cmd >= PRC_NCMDS)
1177 return NULL;
1178 errno = inetctlerrmap[cmd];
1179 if (PRC_IS_REDIRECT(cmd))
1180 notify = in_rtchange, ip = 0;
1181 else if (cmd == PRC_HOSTDEAD)
1182 ip = 0;
1183 else if (errno == 0)
1184 return NULL;
1185 if (ip) {
1186 uh = (struct udphdr *)((caddr_t)ip + (ip->ip_hl << 2));
1187 in_pcbnotify(&udbtable, satosin(sa)->sin_addr, uh->uh_dport,
1188 ip->ip_src, uh->uh_sport, errno, notify);
1189
1190 /* XXX mapped address case */
1191 } else
1192 in_pcbnotifyall(&udbtable, satosin(sa)->sin_addr, errno,
1193 notify);
1194 return NULL;
1195 }
1196
1197 int
1198 #if __STDC__
1199 udp_output(struct mbuf *m, ...)
1200 #else
1201 udp_output(m, va_alist)
1202 struct mbuf *m;
1203 va_dcl
1204 #endif
1205 {
1206 struct inpcb *inp;
1207 struct udpiphdr *ui;
1208 int len = m->m_pkthdr.len;
1209 int error = 0;
1210 va_list ap;
1211
1212 va_start(ap, m);
1213 inp = va_arg(ap, struct inpcb *);
1214 va_end(ap);
1215
1216 /*
1217 * Calculate data length and get a mbuf
1218 * for UDP and IP headers.
1219 */
1220 M_PREPEND(m, sizeof(struct udpiphdr), M_DONTWAIT);
1221 if (m == 0) {
1222 error = ENOBUFS;
1223 goto release;
1224 }
1225
1226 /*
1227 * Compute the packet length of the IP header, and
1228 * punt if the length looks bogus.
1229 */
1230 if ((len + sizeof(struct udpiphdr)) > IP_MAXPACKET) {
1231 error = EMSGSIZE;
1232 goto release;
1233 }
1234
1235 /*
1236 * Fill in mbuf with extended UDP header
1237 * and addresses and length put into network format.
1238 */
1239 ui = mtod(m, struct udpiphdr *);
1240 bzero(ui->ui_x1, sizeof ui->ui_x1);
1241 ui->ui_pr = IPPROTO_UDP;
1242 ui->ui_len = htons((u_int16_t)len + sizeof (struct udphdr));
1243 ui->ui_src = inp->inp_laddr;
1244 ui->ui_dst = inp->inp_faddr;
1245 ui->ui_sport = inp->inp_lport;
1246 ui->ui_dport = inp->inp_fport;
1247 ui->ui_ulen = ui->ui_len;
1248
1249 /*
1250 * Stuff checksum and output datagram.
1251 */
1252 ui->ui_sum = 0;
1253 if (udpcksum) {
1254 if ((ui->ui_sum = in_cksum(m, sizeof (struct udpiphdr) + len)) == 0)
1255 ui->ui_sum = 0xffff;
1256 }
1257 ((struct ip *)ui)->ip_len = sizeof (struct udpiphdr) + len;
1258 ((struct ip *)ui)->ip_ttl = inp->inp_ip.ip_ttl; /* XXX */
1259 ((struct ip *)ui)->ip_tos = inp->inp_ip.ip_tos; /* XXX */
1260 udpstat.udps_opackets++;
1261
1262 #ifdef IPSEC
1263 if (ipsec_setsocket(m, inp->inp_socket) != 0) {
1264 error = ENOBUFS;
1265 goto release;
1266 }
1267 #endif /*IPSEC*/
1268
1269 return (ip_output(m, inp->inp_options, &inp->inp_route,
1270 inp->inp_socket->so_options & (SO_DONTROUTE | SO_BROADCAST),
1271 inp->inp_moptions));
1272
1273 release:
1274 m_freem(m);
1275 return (error);
1276 }
1277
1278 int udp_sendspace = 9216; /* really max datagram size */
1279 int udp_recvspace = 40 * (1024 + sizeof(struct sockaddr_in));
1280 /* 40 1K datagrams */
1281
1282 /*ARGSUSED*/
1283 int
1284 udp_usrreq(so, req, m, nam, control, p)
1285 struct socket *so;
1286 int req;
1287 struct mbuf *m, *nam, *control;
1288 struct proc *p;
1289 {
1290 struct inpcb *inp;
1291 int s;
1292 int error = 0;
1293
1294 if (req == PRU_CONTROL)
1295 return (in_control(so, (long)m, (caddr_t)nam,
1296 (struct ifnet *)control, p));
1297
1298 if (req == PRU_PURGEIF) {
1299 in_purgeif((struct ifnet *)control);
1300 in_pcbpurgeif(&udbtable, (struct ifnet *)control);
1301 return (0);
1302 }
1303
1304 s = splsoftnet();
1305 inp = sotoinpcb(so);
1306 #ifdef DIAGNOSTIC
1307 if (req != PRU_SEND && req != PRU_SENDOOB && control)
1308 panic("udp_usrreq: unexpected control mbuf");
1309 #endif
1310 if (inp == 0 && req != PRU_ATTACH) {
1311 error = EINVAL;
1312 goto release;
1313 }
1314
1315 /*
1316 * Note: need to block udp_input while changing
1317 * the udp pcb queue and/or pcb addresses.
1318 */
1319 switch (req) {
1320
1321 case PRU_ATTACH:
1322 if (inp != 0) {
1323 error = EISCONN;
1324 break;
1325 }
1326 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
1327 error = soreserve(so, udp_sendspace, udp_recvspace);
1328 if (error)
1329 break;
1330 }
1331 error = in_pcballoc(so, &udbtable);
1332 if (error)
1333 break;
1334 inp = sotoinpcb(so);
1335 inp->inp_ip.ip_ttl = ip_defttl;
1336 #ifdef IPSEC
1337 error = ipsec_init_policy(so, &inp->inp_sp);
1338 if (error != 0) {
1339 in_pcbdetach(inp);
1340 break;
1341 }
1342 #endif /*IPSEC*/
1343 break;
1344
1345 case PRU_DETACH:
1346 in_pcbdetach(inp);
1347 break;
1348
1349 case PRU_BIND:
1350 error = in_pcbbind(inp, nam, p);
1351 break;
1352
1353 case PRU_LISTEN:
1354 error = EOPNOTSUPP;
1355 break;
1356
1357 case PRU_CONNECT:
1358 error = in_pcbconnect(inp, nam);
1359 if (error)
1360 break;
1361 soisconnected(so);
1362 break;
1363
1364 case PRU_CONNECT2:
1365 error = EOPNOTSUPP;
1366 break;
1367
1368 case PRU_DISCONNECT:
1369 /*soisdisconnected(so);*/
1370 so->so_state &= ~SS_ISCONNECTED; /* XXX */
1371 in_pcbdisconnect(inp);
1372 inp->inp_laddr = zeroin_addr; /* XXX */
1373 in_pcbstate(inp, INP_BOUND); /* XXX */
1374 break;
1375
1376 case PRU_SHUTDOWN:
1377 socantsendmore(so);
1378 break;
1379
1380 case PRU_RCVD:
1381 error = EOPNOTSUPP;
1382 break;
1383
1384 case PRU_SEND:
1385 if (control && control->m_len) {
1386 m_freem(control);
1387 m_freem(m);
1388 error = EINVAL;
1389 break;
1390 }
1391 {
1392 struct in_addr laddr; /* XXX */
1393
1394 if (nam) {
1395 laddr = inp->inp_laddr; /* XXX */
1396 if ((so->so_state & SS_ISCONNECTED) != 0) {
1397 error = EISCONN;
1398 goto die;
1399 }
1400 error = in_pcbconnect(inp, nam);
1401 if (error) {
1402 die:
1403 m_freem(m);
1404 break;
1405 }
1406 } else {
1407 if ((so->so_state & SS_ISCONNECTED) == 0) {
1408 error = ENOTCONN;
1409 goto die;
1410 }
1411 }
1412 error = udp_output(m, inp);
1413 if (nam) {
1414 in_pcbdisconnect(inp);
1415 inp->inp_laddr = laddr; /* XXX */
1416 in_pcbstate(inp, INP_BOUND); /* XXX */
1417 }
1418 }
1419 break;
1420
1421 case PRU_SENSE:
1422 /*
1423 * stat: don't bother with a blocksize.
1424 */
1425 splx(s);
1426 return (0);
1427
1428 case PRU_RCVOOB:
1429 error = EOPNOTSUPP;
1430 break;
1431
1432 case PRU_SENDOOB:
1433 m_freem(control);
1434 m_freem(m);
1435 error = EOPNOTSUPP;
1436 break;
1437
1438 case PRU_SOCKADDR:
1439 in_setsockaddr(inp, nam);
1440 break;
1441
1442 case PRU_PEERADDR:
1443 in_setpeeraddr(inp, nam);
1444 break;
1445
1446 default:
1447 panic("udp_usrreq");
1448 }
1449
1450 release:
1451 splx(s);
1452 return (error);
1453 }
1454
1455 /*
1456 * Sysctl for udp variables.
1457 */
1458 int
1459 udp_sysctl(name, namelen, oldp, oldlenp, newp, newlen)
1460 int *name;
1461 u_int namelen;
1462 void *oldp;
1463 size_t *oldlenp;
1464 void *newp;
1465 size_t newlen;
1466 {
1467 /* All sysctl names at this level are terminal. */
1468 if (namelen != 1)
1469 return (ENOTDIR);
1470
1471 switch (name[0]) {
1472 case UDPCTL_CHECKSUM:
1473 return (sysctl_int(oldp, oldlenp, newp, newlen, &udpcksum));
1474 case UDPCTL_SENDSPACE:
1475 return (sysctl_int(oldp, oldlenp, newp, newlen,
1476 &udp_sendspace));
1477 case UDPCTL_RECVSPACE:
1478 return (sysctl_int(oldp, oldlenp, newp, newlen,
1479 &udp_recvspace));
1480 default:
1481 return (ENOPROTOOPT);
1482 }
1483 /* NOTREACHED */
1484 }
1485 #endif
1486