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