Annotation of src/sys/netinet/udp_usrreq.c, Revision 1.77
1.77 ! soda 1: /* $NetBSD: udp_usrreq.c,v 1.76 2001/05/08 10:15:14 itojun Exp $ */
1.48 itojun 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: */
1.14 cgd 31:
1.1 cgd 32: /*
1.44 thorpej 33: * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995
1.13 mycroft 34: * The Regents of the University of California. All rights reserved.
1.1 cgd 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: *
1.44 thorpej 64: * @(#)udp_usrreq.c 8.6 (Berkeley) 5/23/95
1.1 cgd 65: */
1.50 thorpej 66:
1.77 ! soda 67: #include "opt_inet.h"
1.50 thorpej 68: #include "opt_ipsec.h"
1.64 ws 69: #include "opt_ipkdb.h"
1.1 cgd 70:
1.5 mycroft 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>
1.13 mycroft 77: #include <sys/errno.h>
1.5 mycroft 78: #include <sys/stat.h>
1.27 christos 79: #include <sys/systm.h>
80: #include <sys/proc.h>
1.53 itojun 81: #include <sys/domain.h>
1.27 christos 82:
1.67 mrg 83: #include <uvm/uvm_extern.h>
1.27 christos 84: #include <sys/sysctl.h>
1.1 cgd 85:
1.5 mycroft 86: #include <net/if.h>
87: #include <net/route.h>
1.1 cgd 88:
1.5 mycroft 89: #include <netinet/in.h>
90: #include <netinet/in_systm.h>
1.15 cgd 91: #include <netinet/in_var.h>
1.5 mycroft 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>
1.1 cgd 98:
1.53 itojun 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:
1.76 itojun 114: #include "faith.h"
115: #if defined(NFAITH) && NFAITH > 0
116: #include <net/if_faith.h>
117: #endif
118:
1.27 christos 119: #include <machine/stdarg.h>
120:
1.48 itojun 121: #ifdef IPSEC
122: #include <netinet6/ipsec.h>
123: #include <netkey/key.h>
124: #endif /*IPSEC*/
125:
1.64 ws 126: #ifdef IPKDB
127: #include <ipkdb/ipkdb.h>
128: #endif
129:
1.8 mycroft 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:
1.72 itojun 140: #ifdef INET
1.53 itojun 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));
1.72 itojun 145: #endif
1.53 itojun 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
1.72 itojun 154: #ifdef INET
1.13 mycroft 155: static void udp_notify __P((struct inpcb *, int));
1.72 itojun 156: #endif
1.7 mycroft 157:
1.26 mycroft 158: #ifndef UDBHASHSIZE
159: #define UDBHASHSIZE 128
160: #endif
161: int udbhashsize = UDBHASHSIZE;
162:
1.7 mycroft 163: void
1.1 cgd 164: udp_init()
165: {
1.18 mycroft 166:
1.72 itojun 167: #ifdef INET
1.35 mycroft 168: in_pcbinit(&udbtable, udbhashsize, udbhashsize);
1.72 itojun 169: #endif
1.1 cgd 170: }
171:
1.56 itojun 172: #ifndef UDP6
1.72 itojun 173: #ifdef INET
1.7 mycroft 174: void
1.27 christos 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
1.1 cgd 182: {
1.53 itojun 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:
1.57 itojun 237: /* destination port of 0 is illegal, based on RFC768. */
238: if (uh->uh_dport == 0)
239: goto bad;
240:
1.53 itojun 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) {
1.70 sommerfe 247: if (ip->ip_len < iphlen + len || len < sizeof(struct udphdr)) {
1.53 itojun 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: }
1.61 itojun 306: udpstat.udps_noport++;
1.64 ws 307: #ifdef IPKDB
1.53 itojun 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: }
1.72 itojun 326: #endif
1.53 itojun 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:
1.76 itojun 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:
1.53 itojun 346: #if defined(NFAITH) && 0 < NFAITH
1.76 itojun 347: if (faithprefix(&ip6->ip6_dst)) {
348: /* send icmp6 host unreach? */
349: m_freem(m);
350: return IPPROTO_DONE;
1.53 itojun 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);
1.69 itojun 368: /*
369: * RFC2675 section 4: jumbograms will have 0 in the UDP header field,
370: * iff payload length > 0xffff.
371: */
1.53 itojun 372: if (ulen == 0 && plen > 0xffff)
373: ulen = plen;
374:
375: if (plen != ulen) {
376: udp6stat.udp6s_badlen++;
1.54 itojun 377: goto bad;
378: }
379:
1.57 itojun 380: /* destination port of 0 is illegal, based on RFC768. */
381: if (uh->uh_dport == 0)
382: goto bad;
383:
1.54 itojun 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 */
1.53 itojun 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);
1.69 itojun 408: /* KAME hack: recover scopeid */
409: (void)in6_recoverscope(&src, &ip6->ip6_src, m->m_pkthdr.rcvif);
1.53 itojun 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);
1.69 itojun 414: /* KAME hack: recover scopeid */
415: (void)in6_recoverscope(&dst, &ip6->ip6_dst, m->m_pkthdr.rcvif);
1.53 itojun 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: }
1.61 itojun 423: udp6stat.udp6s_noport++;
1.53 itojun 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:
1.72 itojun 435: #ifdef INET
1.53 itojun 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: }
1.72 itojun 490: #endif
1.53 itojun 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:
1.72 itojun 537: #ifdef INET
1.53 itojun 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:
1.73 itojun 561: if (IN_MULTICAST(dst4->s_addr) ||
1.53 itojun 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.
1.71 itojun 582: * we need udpiphdr for IPsec processing so we do that later.
1.53 itojun 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: }
1.61 itojun 647: udpstat.udps_noport++;
1.64 ws 648: #ifdef IPKDB
1.53 itojun 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: }
1.72 itojun 674: #endif
1.53 itojun 675:
676: #ifdef INET6
677: static int
678: in6_mcmatch(in6p, ia6, ifp)
679: struct in6pcb *in6p;
1.66 augustss 680: struct in6_addr *ia6;
1.53 itojun 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;
1.74 itojun 711: struct in_addr *dst4;
1.53 itojun 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;
1.74 itojun 726: dst4 = (struct in_addr *)&dst->sin6_addr.s6_addr32[12];
1.53 itojun 727:
728: if (IN6_IS_ADDR_MULTICAST(dst6)
1.73 itojun 729: || (af == AF_INET && IN_MULTICAST(dst4->s_addr))) {
1.53 itojun 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.
1.71 itojun 749: * we need udpiphdr for IPsec processing so we do that later.
1.53 itojun 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: }
1.55 itojun 763: #ifndef INET6_BINDV6ONLY
764: else {
765: if (IN6_IS_ADDR_V4MAPPED(dst6)
766: && (in6p->in6p_flags & IN6P_BINDV6ONLY))
767: continue;
768: }
769: #endif
1.53 itojun 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: }
1.55 itojun 775: #ifndef INET6_BINDV6ONLY
776: else {
777: if (IN6_IS_ADDR_V4MAPPED(src6)
778: && (in6p->in6p_flags & IN6P_BINDV6ONLY))
779: continue;
780: }
781: #endif
1.53 itojun 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: }
1.61 itojun 838: udpstat.udps_noport++;
1.53 itojun 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: }
1.61 itojun 848: udp6stat.udp6s_noport++;
1.53 itojun 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:
1.56 itojun 869: #else /*UDP6*/
870:
1.53 itojun 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: {
1.48 itojun 880: int proto;
1.66 augustss 881: struct ip *ip;
882: struct udphdr *uh;
883: struct inpcb *inp;
1.1 cgd 884: struct mbuf *opts = 0;
885: int len;
886: struct ip save_ip;
1.27 christos 887: int iphlen;
888: va_list ap;
1.34 mycroft 889: struct sockaddr_in udpsrc;
1.48 itojun 890: struct sockaddr *sa;
1.27 christos 891:
892: va_start(ap, m);
893: iphlen = va_arg(ap, int);
1.48 itojun 894: proto = va_arg(ap, int);
1.27 christos 895: va_end(ap);
1.1 cgd 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);
1.57 itojun 922:
923: /* destination port of 0 is illegal, based on RFC768. */
924: if (uh->uh_dport == 0)
925: goto bad;
1.1 cgd 926:
927: /*
928: * Make mbuf data length reflect UDP length.
929: * If not enough data to reflect UDP length, drop.
930: */
1.15 cgd 931: len = ntohs((u_int16_t)uh->uh_ulen);
1.47 mycroft 932: if (ip->ip_len != iphlen + len) {
1.70 sommerfe 933: if (ip->ip_len < iphlen + len || len < sizeof(struct udphdr)) {
1.1 cgd 934: udpstat.udps_badlen++;
935: goto bad;
936: }
1.47 mycroft 937: m_adj(m, iphlen + len - ip->ip_len);
1.1 cgd 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: */
1.29 mrg 948: if (uh->uh_sum) {
1.25 cgd 949: bzero(((struct ipovly *)ip)->ih_x1,
950: sizeof ((struct ipovly *)ip)->ih_x1);
1.1 cgd 951: ((struct ipovly *)ip)->ih_len = uh->uh_ulen;
1.46 mycroft 952: if (in_cksum(m, len + sizeof (struct ip)) != 0) {
1.1 cgd 953: udpstat.udps_badsum++;
954: m_freem(m);
955: return;
956: }
957: }
1.13 mycroft 958:
1.48 itojun 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:
1.16 mycroft 968: if (IN_MULTICAST(ip->ip_dst.s_addr) ||
1.13 mycroft 969: in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) {
1.40 thorpej 970: struct inpcb *last;
1.4 hpeyerl 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
1.13 mycroft 984: * fixing the interface. Maybe 4.5BSD will remedy this?)
1.4 hpeyerl 985: */
1.13 mycroft 986:
1.48 itojun 987: iphlen += sizeof(struct udphdr);
1.4 hpeyerl 988: /*
1.48 itojun 989: * KAME note: usually we drop udpiphdr from mbuf here.
1.71 itojun 990: * we need udpiphdr for IPsec processing so we do that later.
1.4 hpeyerl 991: */
992: /*
993: * Locate pcb(s) for datagram.
994: * (Algorithm copied from raw_intr().)
995: */
996: last = NULL;
1.22 cgd 997: for (inp = udbtable.inpt_queue.cqh_first;
998: inp != (struct inpcb *)&udbtable.inpt_queue;
999: inp = inp->inp_queue.cqe_next) {
1.4 hpeyerl 1000: if (inp->inp_lport != uh->uh_dport)
1001: continue;
1.34 mycroft 1002: if (!in_nullhost(inp->inp_laddr)) {
1003: if (!in_hosteq(inp->inp_laddr, ip->ip_dst))
1.4 hpeyerl 1004: continue;
1005: }
1.34 mycroft 1006: if (!in_nullhost(inp->inp_faddr)) {
1007: if (!in_hosteq(inp->inp_faddr, ip->ip_src) ||
1.4 hpeyerl 1008: inp->inp_fport != uh->uh_sport)
1009: continue;
1010: }
1011:
1012: if (last != NULL) {
1013: struct mbuf *n;
1014:
1.48 itojun 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*/
1.4 hpeyerl 1022: if ((n = m_copy(m, 0, M_COPYALL)) != NULL) {
1.40 thorpej 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: }
1.49 drochner 1029: m_adj(n, iphlen);
1.48 itojun 1030: sa = (struct sockaddr *)&udpsrc;
1.40 thorpej 1031: if (sbappendaddr(
1032: &last->inp_socket->so_rcv,
1.48 itojun 1033: sa, n, opts) == 0) {
1.4 hpeyerl 1034: m_freem(n);
1.40 thorpej 1035: if (opts)
1036: m_freem(opts);
1.13 mycroft 1037: } else
1.40 thorpej 1038: sorwakeup(last->inp_socket);
1039: opts = 0;
1.4 hpeyerl 1040: }
1041: }
1.40 thorpej 1042: last = inp;
1.4 hpeyerl 1043: /*
1.13 mycroft 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.
1.4 hpeyerl 1050: */
1.40 thorpej 1051: if ((last->inp_socket->so_options &
1052: (SO_REUSEPORT|SO_REUSEADDR)) == 0)
1.4 hpeyerl 1053: break;
1054: }
1.6 mycroft 1055:
1.4 hpeyerl 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: */
1.13 mycroft 1062: udpstat.udps_noportbcast++;
1.4 hpeyerl 1063: goto bad;
1064: }
1.48 itojun 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*/
1.40 thorpej 1072: if (last->inp_flags & INP_CONTROLOPTS ||
1073: last->inp_socket->so_options & SO_TIMESTAMP)
1074: ip_savecontrol(last, &opts, ip, m);
1.49 drochner 1075: m->m_len -= iphlen;
1076: m->m_pkthdr.len -= iphlen;
1077: m->m_data += iphlen;
1.48 itojun 1078: sa = (struct sockaddr *)&udpsrc;
1079: if (sbappendaddr(&last->inp_socket->so_rcv, sa, m, opts) == 0) {
1.13 mycroft 1080: udpstat.udps_fullsock++;
1.4 hpeyerl 1081: goto bad;
1.13 mycroft 1082: }
1.40 thorpej 1083: sorwakeup(last->inp_socket);
1.4 hpeyerl 1084: return;
1085: }
1.1 cgd 1086: /*
1087: * Locate pcb for datagram.
1088: */
1.35 mycroft 1089: inp = in_pcblookup_connect(&udbtable, ip->ip_src, uh->uh_sport,
1.26 mycroft 1090: ip->ip_dst, uh->uh_dport);
1091: if (inp == 0) {
1092: ++udpstat.udps_pcbhashmiss;
1.35 mycroft 1093: inp = in_pcblookup_bind(&udbtable, ip->ip_dst, uh->uh_dport);
1.18 mycroft 1094: if (inp == 0) {
1095: if (m->m_flags & (M_BCAST | M_MCAST)) {
1096: udpstat.udps_noportbcast++;
1097: goto bad;
1098: }
1.61 itojun 1099: udpstat.udps_noport++;
1.18 mycroft 1100: *ip = save_ip;
1.64 ws 1101: #ifdef IPKDB
1.38 ws 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)))
1.37 ws 1108: /* It was a debugger connect packet, just drop it now */
1109: goto bad;
1110: #endif
1.18 mycroft 1111: icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PORT, 0, 0);
1112: return;
1.13 mycroft 1113: }
1.1 cgd 1114: }
1.48 itojun 1115: #ifdef IPSEC
1116: if (inp != NULL && ipsec4_in_reject(m, inp)) {
1117: ipsecstat.in_polvio++;
1118: goto bad;
1119: }
1120: #endif /*IPSEC*/
1.1 cgd 1121:
1122: /*
1123: * Stuff source address and datagram in user buffer.
1124: */
1.40 thorpej 1125: if (inp->inp_flags & INP_CONTROLOPTS ||
1126: inp->inp_socket->so_options & SO_TIMESTAMP)
1127: ip_savecontrol(inp, &opts, ip, m);
1.1 cgd 1128: iphlen += sizeof(struct udphdr);
1129: m->m_len -= iphlen;
1130: m->m_pkthdr.len -= iphlen;
1131: m->m_data += iphlen;
1.48 itojun 1132: sa = (struct sockaddr *)&udpsrc;
1133: if (sbappendaddr(&inp->inp_socket->so_rcv, sa, m, opts) == 0) {
1.1 cgd 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: }
1.56 itojun 1144: #endif /*UDP6*/
1.1 cgd 1145:
1.72 itojun 1146: #ifdef INET
1.1 cgd 1147: /*
1148: * Notify a udp user of an asynchronous error;
1149: * just wake up so that he can collect error status.
1150: */
1.7 mycroft 1151: static void
1.1 cgd 1152: udp_notify(inp, errno)
1.66 augustss 1153: struct inpcb *inp;
1.7 mycroft 1154: int errno;
1.1 cgd 1155: {
1.34 mycroft 1156:
1.1 cgd 1157: inp->inp_socket->so_error = errno;
1158: sorwakeup(inp->inp_socket);
1159: sowwakeup(inp->inp_socket);
1160: }
1161:
1.27 christos 1162: void *
1163: udp_ctlinput(cmd, sa, v)
1.1 cgd 1164: int cmd;
1165: struct sockaddr *sa;
1.27 christos 1166: void *v;
1.1 cgd 1167: {
1.66 augustss 1168: struct ip *ip = v;
1169: struct udphdr *uh;
1.18 mycroft 1170: void (*notify) __P((struct inpcb *, int)) = udp_notify;
1.21 mycroft 1171: int errno;
1.1 cgd 1172:
1.53 itojun 1173: if (sa->sa_family != AF_INET
1174: || sa->sa_len != sizeof(struct sockaddr_in))
1.51 itojun 1175: return NULL;
1.20 mycroft 1176: if ((unsigned)cmd >= PRC_NCMDS)
1.27 christos 1177: return NULL;
1.20 mycroft 1178: errno = inetctlerrmap[cmd];
1.18 mycroft 1179: if (PRC_IS_REDIRECT(cmd))
1.19 mycroft 1180: notify = in_rtchange, ip = 0;
1.18 mycroft 1181: else if (cmd == PRC_HOSTDEAD)
1.19 mycroft 1182: ip = 0;
1.23 cgd 1183: else if (errno == 0)
1.27 christos 1184: return NULL;
1.19 mycroft 1185: if (ip) {
1.1 cgd 1186: uh = (struct udphdr *)((caddr_t)ip + (ip->ip_hl << 2));
1.34 mycroft 1187: in_pcbnotify(&udbtable, satosin(sa)->sin_addr, uh->uh_dport,
1188: ip->ip_src, uh->uh_sport, errno, notify);
1.53 itojun 1189:
1190: /* XXX mapped address case */
1.19 mycroft 1191: } else
1.34 mycroft 1192: in_pcbnotifyall(&udbtable, satosin(sa)->sin_addr, errno,
1193: notify);
1.27 christos 1194: return NULL;
1.1 cgd 1195: }
1196:
1.7 mycroft 1197: int
1.27 christos 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: {
1.66 augustss 1206: struct inpcb *inp;
1207: struct udpiphdr *ui;
1208: int len = m->m_pkthdr.len;
1.31 mycroft 1209: int error = 0;
1.27 christos 1210: va_list ap;
1211:
1212: va_start(ap, m);
1213: inp = va_arg(ap, struct inpcb *);
1214: va_end(ap);
1.1 cgd 1215:
1216: /*
1217: * Calculate data length and get a mbuf
1218: * for UDP and IP headers.
1219: */
1.13 mycroft 1220: M_PREPEND(m, sizeof(struct udpiphdr), M_DONTWAIT);
1221: if (m == 0) {
1222: error = ENOBUFS;
1.39 thorpej 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;
1.13 mycroft 1232: goto release;
1233: }
1.1 cgd 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 *);
1.25 cgd 1240: bzero(ui->ui_x1, sizeof ui->ui_x1);
1.1 cgd 1241: ui->ui_pr = IPPROTO_UDP;
1.15 cgd 1242: ui->ui_len = htons((u_int16_t)len + sizeof (struct udphdr));
1.1 cgd 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++;
1.48 itojun 1261:
1262: #ifdef IPSEC
1.75 itojun 1263: if (ipsec_setsocket(m, inp->inp_socket) != 0) {
1264: error = ENOBUFS;
1265: goto release;
1266: }
1.48 itojun 1267: #endif /*IPSEC*/
1268:
1.31 mycroft 1269: return (ip_output(m, inp->inp_options, &inp->inp_route,
1.12 mycroft 1270: inp->inp_socket->so_options & (SO_DONTROUTE | SO_BROADCAST),
1.31 mycroft 1271: inp->inp_moptions));
1.1 cgd 1272:
1273: release:
1274: m_freem(m);
1275: return (error);
1276: }
1277:
1.42 thorpej 1278: int udp_sendspace = 9216; /* really max datagram size */
1279: int udp_recvspace = 40 * (1024 + sizeof(struct sockaddr_in));
1.1 cgd 1280: /* 40 1K datagrams */
1281:
1282: /*ARGSUSED*/
1.7 mycroft 1283: int
1.31 mycroft 1284: udp_usrreq(so, req, m, nam, control, p)
1.1 cgd 1285: struct socket *so;
1286: int req;
1.31 mycroft 1287: struct mbuf *m, *nam, *control;
1.30 mycroft 1288: struct proc *p;
1.1 cgd 1289: {
1.66 augustss 1290: struct inpcb *inp;
1.1 cgd 1291: int s;
1.66 augustss 1292: int error = 0;
1.1 cgd 1293:
1294: if (req == PRU_CONTROL)
1.31 mycroft 1295: return (in_control(so, (long)m, (caddr_t)nam,
1.30 mycroft 1296: (struct ifnet *)control, p));
1.59 thorpej 1297:
1.60 thorpej 1298: if (req == PRU_PURGEIF) {
1299: in_purgeif((struct ifnet *)control);
1300: in_pcbpurgeif(&udbtable, (struct ifnet *)control);
1.59 thorpej 1301: return (0);
1302: }
1.31 mycroft 1303:
1304: s = splsoftnet();
1305: inp = sotoinpcb(so);
1.32 mycroft 1306: #ifdef DIAGNOSTIC
1307: if (req != PRU_SEND && req != PRU_SENDOOB && control)
1308: panic("udp_usrreq: unexpected control mbuf");
1309: #endif
1.31 mycroft 1310: if (inp == 0 && req != PRU_ATTACH) {
1311: error = EINVAL;
1312: goto release;
1313: }
1314:
1.1 cgd 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:
1.31 mycroft 1322: if (inp != 0) {
1323: error = EISCONN;
1.1 cgd 1324: break;
1325: }
1.31 mycroft 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: }
1.18 mycroft 1331: error = in_pcballoc(so, &udbtable);
1.1 cgd 1332: if (error)
1333: break;
1.31 mycroft 1334: inp = sotoinpcb(so);
1335: inp->inp_ip.ip_ttl = ip_defttl;
1.48 itojun 1336: #ifdef IPSEC
1.58 itojun 1337: error = ipsec_init_policy(so, &inp->inp_sp);
1.53 itojun 1338: if (error != 0) {
1.52 itojun 1339: in_pcbdetach(inp);
1.53 itojun 1340: break;
1341: }
1.48 itojun 1342: #endif /*IPSEC*/
1.1 cgd 1343: break;
1344:
1345: case PRU_DETACH:
1.31 mycroft 1346: in_pcbdetach(inp);
1.1 cgd 1347: break;
1348:
1349: case PRU_BIND:
1.31 mycroft 1350: error = in_pcbbind(inp, nam, p);
1.1 cgd 1351: break;
1352:
1353: case PRU_LISTEN:
1354: error = EOPNOTSUPP;
1355: break;
1356:
1357: case PRU_CONNECT:
1.31 mycroft 1358: error = in_pcbconnect(inp, nam);
1359: if (error)
1.1 cgd 1360: break;
1.31 mycroft 1361: soisconnected(so);
1.1 cgd 1362: break;
1363:
1364: case PRU_CONNECT2:
1365: error = EOPNOTSUPP;
1366: break;
1367:
1368: case PRU_DISCONNECT:
1.31 mycroft 1369: /*soisdisconnected(so);*/
1370: so->so_state &= ~SS_ISCONNECTED; /* XXX */
1.1 cgd 1371: in_pcbdisconnect(inp);
1.34 mycroft 1372: inp->inp_laddr = zeroin_addr; /* XXX */
1.35 mycroft 1373: in_pcbstate(inp, INP_BOUND); /* XXX */
1.1 cgd 1374: break;
1375:
1376: case PRU_SHUTDOWN:
1377: socantsendmore(so);
1378: break;
1379:
1.31 mycroft 1380: case PRU_RCVD:
1381: error = EOPNOTSUPP;
1.1 cgd 1382: break;
1383:
1.31 mycroft 1384: case PRU_SEND:
1.32 mycroft 1385: if (control && control->m_len) {
1386: m_freem(control);
1387: m_freem(m);
1388: error = EINVAL;
1389: break;
1390: }
1.31 mycroft 1391: {
1.35 mycroft 1392: struct in_addr laddr; /* XXX */
1.1 cgd 1393:
1.31 mycroft 1394: if (nam) {
1.35 mycroft 1395: laddr = inp->inp_laddr; /* XXX */
1.31 mycroft 1396: if ((so->so_state & SS_ISCONNECTED) != 0) {
1397: error = EISCONN;
1.32 mycroft 1398: goto die;
1.31 mycroft 1399: }
1400: error = in_pcbconnect(inp, nam);
1.32 mycroft 1401: if (error) {
1402: die:
1403: m_freem(m);
1.31 mycroft 1404: break;
1.32 mycroft 1405: }
1.31 mycroft 1406: } else {
1407: if ((so->so_state & SS_ISCONNECTED) == 0) {
1408: error = ENOTCONN;
1.32 mycroft 1409: goto die;
1.31 mycroft 1410: }
1411: }
1.33 mycroft 1412: error = udp_output(m, inp);
1.31 mycroft 1413: if (nam) {
1414: in_pcbdisconnect(inp);
1.35 mycroft 1415: inp->inp_laddr = laddr; /* XXX */
1416: in_pcbstate(inp, INP_BOUND); /* XXX */
1.31 mycroft 1417: }
1418: }
1.1 cgd 1419: break;
1420:
1421: case PRU_SENSE:
1422: /*
1423: * stat: don't bother with a blocksize.
1424: */
1.31 mycroft 1425: splx(s);
1.1 cgd 1426: return (0);
1427:
1.31 mycroft 1428: case PRU_RCVOOB:
1429: error = EOPNOTSUPP;
1430: break;
1431:
1.1 cgd 1432: case PRU_SENDOOB:
1.32 mycroft 1433: m_freem(control);
1.31 mycroft 1434: m_freem(m);
1.1 cgd 1435: error = EOPNOTSUPP;
1436: break;
1437:
1.31 mycroft 1438: case PRU_SOCKADDR:
1439: in_setsockaddr(inp, nam);
1440: break;
1441:
1442: case PRU_PEERADDR:
1443: in_setpeeraddr(inp, nam);
1444: break;
1.1 cgd 1445:
1446: default:
1447: panic("udp_usrreq");
1448: }
1449:
1450: release:
1.31 mycroft 1451: splx(s);
1.1 cgd 1452: return (error);
1.13 mycroft 1453: }
1454:
1455: /*
1456: * Sysctl for udp variables.
1457: */
1.27 christos 1458: int
1.13 mycroft 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));
1.42 thorpej 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));
1.13 mycroft 1480: default:
1481: return (ENOPROTOOPT);
1482: }
1483: /* NOTREACHED */
1.1 cgd 1484: }
1.72 itojun 1485: #endif
CVSweb <webmaster@jp.NetBSD.org>