Annotation of src/sys/netinet/udp_usrreq.c, Revision 1.52
1.52 ! itojun 1: /* $NetBSD: udp_usrreq.c,v 1.51 1999/08/09 10:55:29 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:
67: #include "opt_ipsec.h"
68:
1.38 ws 69: #include "ipkdb.h"
1.1 cgd 70:
1.48 itojun 71: /* XXX MAPPED_ADDR_ENABLED should be revisited */
72:
1.5 mycroft 73: #include <sys/param.h>
74: #include <sys/malloc.h>
75: #include <sys/mbuf.h>
1.48 itojun 76: #ifdef MAPPED_ADDR_ENABLED
77: #include <sys/domain.h>
78: #endif /* MAPPED_ADDR_ENABLED */
1.5 mycroft 79: #include <sys/protosw.h>
80: #include <sys/socket.h>
81: #include <sys/socketvar.h>
1.13 mycroft 82: #include <sys/errno.h>
1.5 mycroft 83: #include <sys/stat.h>
1.27 christos 84: #include <sys/systm.h>
85: #include <sys/proc.h>
86:
87: #include <vm/vm.h>
88: #include <sys/sysctl.h>
1.1 cgd 89:
1.5 mycroft 90: #include <net/if.h>
91: #include <net/route.h>
1.1 cgd 92:
1.5 mycroft 93: #include <netinet/in.h>
94: #include <netinet/in_systm.h>
1.15 cgd 95: #include <netinet/in_var.h>
1.5 mycroft 96: #include <netinet/ip.h>
97: #include <netinet/in_pcb.h>
98: #include <netinet/ip_var.h>
99: #include <netinet/ip_icmp.h>
100: #include <netinet/udp.h>
101: #include <netinet/udp_var.h>
1.1 cgd 102:
1.27 christos 103: #include <machine/stdarg.h>
104:
1.48 itojun 105: #ifdef IPSEC
106: #include <netinet6/ipsec.h>
107: #include <netkey/key.h>
108: #include <netkey/key_debug.h>
109: #endif /*IPSEC*/
110:
1.8 mycroft 111: /*
112: * UDP protocol implementation.
113: * Per RFC 768, August, 1980.
114: */
115: #ifndef COMPAT_42
116: int udpcksum = 1;
117: #else
118: int udpcksum = 0; /* XXX */
119: #endif
120:
1.13 mycroft 121: static void udp_notify __P((struct inpcb *, int));
1.7 mycroft 122:
1.26 mycroft 123: #ifndef UDBHASHSIZE
124: #define UDBHASHSIZE 128
125: #endif
126: int udbhashsize = UDBHASHSIZE;
127:
1.7 mycroft 128: void
1.1 cgd 129: udp_init()
130: {
1.18 mycroft 131:
1.35 mycroft 132: in_pcbinit(&udbtable, udbhashsize, udbhashsize);
1.1 cgd 133: }
134:
1.7 mycroft 135: void
1.27 christos 136: #if __STDC__
137: udp_input(struct mbuf *m, ...)
138: #else
139: udp_input(m, va_alist)
140: struct mbuf *m;
141: va_dcl
142: #endif
1.1 cgd 143: {
1.48 itojun 144: int proto;
1.1 cgd 145: register struct ip *ip;
146: register struct udphdr *uh;
147: register struct inpcb *inp;
148: struct mbuf *opts = 0;
149: int len;
150: struct ip save_ip;
1.27 christos 151: int iphlen;
152: va_list ap;
1.34 mycroft 153: struct sockaddr_in udpsrc;
1.48 itojun 154: #ifdef MAPPED_ADDR_ENABLED
155: struct sockaddr_in6 mapped;
156: #endif
157: struct sockaddr *sa;
1.27 christos 158:
159: va_start(ap, m);
160: iphlen = va_arg(ap, int);
1.48 itojun 161: proto = va_arg(ap, int);
1.27 christos 162: va_end(ap);
1.1 cgd 163:
164: udpstat.udps_ipackets++;
165:
166: /*
167: * Strip IP options, if any; should skip this,
168: * make available to user, and use on returned packets,
169: * but we don't yet have a way to check the checksum
170: * with options still present.
171: */
172: if (iphlen > sizeof (struct ip)) {
173: ip_stripoptions(m, (struct mbuf *)0);
174: iphlen = sizeof(struct ip);
175: }
176:
177: /*
178: * Get IP and UDP header together in first mbuf.
179: */
180: ip = mtod(m, struct ip *);
181: if (m->m_len < iphlen + sizeof(struct udphdr)) {
182: if ((m = m_pullup(m, iphlen + sizeof(struct udphdr))) == 0) {
183: udpstat.udps_hdrops++;
184: return;
185: }
186: ip = mtod(m, struct ip *);
187: }
188: uh = (struct udphdr *)((caddr_t)ip + iphlen);
189:
190: /*
191: * Make mbuf data length reflect UDP length.
192: * If not enough data to reflect UDP length, drop.
193: */
1.15 cgd 194: len = ntohs((u_int16_t)uh->uh_ulen);
1.47 mycroft 195: if (ip->ip_len != iphlen + len) {
196: if (ip->ip_len < iphlen + len) {
1.1 cgd 197: udpstat.udps_badlen++;
198: goto bad;
199: }
1.47 mycroft 200: m_adj(m, iphlen + len - ip->ip_len);
1.1 cgd 201: }
202: /*
203: * Save a copy of the IP header in case we want restore it
204: * for sending an ICMP error message in response.
205: */
206: save_ip = *ip;
207:
208: /*
209: * Checksum extended UDP header and data.
210: */
1.29 mrg 211: if (uh->uh_sum) {
1.25 cgd 212: bzero(((struct ipovly *)ip)->ih_x1,
213: sizeof ((struct ipovly *)ip)->ih_x1);
1.1 cgd 214: ((struct ipovly *)ip)->ih_len = uh->uh_ulen;
1.46 mycroft 215: if (in_cksum(m, len + sizeof (struct ip)) != 0) {
1.1 cgd 216: udpstat.udps_badsum++;
217: m_freem(m);
218: return;
219: }
220: }
1.13 mycroft 221:
1.48 itojun 222: /*
223: * Construct sockaddr format source address.
224: */
225: udpsrc.sin_family = AF_INET;
226: udpsrc.sin_len = sizeof(struct sockaddr_in);
227: udpsrc.sin_addr = ip->ip_src;
228: udpsrc.sin_port = uh->uh_sport;
229: bzero((caddr_t)udpsrc.sin_zero, sizeof(udpsrc.sin_zero));
230:
1.16 mycroft 231: if (IN_MULTICAST(ip->ip_dst.s_addr) ||
1.13 mycroft 232: in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) {
1.40 thorpej 233: struct inpcb *last;
1.4 hpeyerl 234: /*
235: * Deliver a multicast or broadcast datagram to *all* sockets
236: * for which the local and remote addresses and ports match
237: * those of the incoming datagram. This allows more than
238: * one process to receive multi/broadcasts on the same port.
239: * (This really ought to be done for unicast datagrams as
240: * well, but that would cause problems with existing
241: * applications that open both address-specific sockets and
242: * a wildcard socket listening to the same port -- they would
243: * end up receiving duplicates of every unicast datagram.
244: * Those applications open the multiple sockets to overcome an
245: * inadequacy of the UDP socket interface, but for backwards
246: * compatibility we avoid the problem here rather than
1.13 mycroft 247: * fixing the interface. Maybe 4.5BSD will remedy this?)
1.4 hpeyerl 248: */
1.13 mycroft 249:
1.48 itojun 250: iphlen += sizeof(struct udphdr);
1.4 hpeyerl 251: /*
1.48 itojun 252: * KAME note: usually we drop udpiphdr from mbuf here.
253: * we need udpiphdr for iPsec processing so we do that later.
1.4 hpeyerl 254: */
255: /*
256: * Locate pcb(s) for datagram.
257: * (Algorithm copied from raw_intr().)
258: */
259: last = NULL;
1.22 cgd 260: for (inp = udbtable.inpt_queue.cqh_first;
261: inp != (struct inpcb *)&udbtable.inpt_queue;
262: inp = inp->inp_queue.cqe_next) {
1.4 hpeyerl 263: if (inp->inp_lport != uh->uh_dport)
264: continue;
1.34 mycroft 265: if (!in_nullhost(inp->inp_laddr)) {
266: if (!in_hosteq(inp->inp_laddr, ip->ip_dst))
1.4 hpeyerl 267: continue;
268: }
1.34 mycroft 269: if (!in_nullhost(inp->inp_faddr)) {
270: if (!in_hosteq(inp->inp_faddr, ip->ip_src) ||
1.4 hpeyerl 271: inp->inp_fport != uh->uh_sport)
272: continue;
273: }
274:
275: if (last != NULL) {
276: struct mbuf *n;
277:
1.48 itojun 278: #ifdef IPSEC
279: /* check AH/ESP integrity. */
280: if (last != NULL && ipsec4_in_reject(m, last)) {
281: ipsecstat.in_polvio++;
282: /* do not inject data to pcb */
283: } else
284: #endif /*IPSEC*/
1.4 hpeyerl 285: if ((n = m_copy(m, 0, M_COPYALL)) != NULL) {
1.40 thorpej 286: if (last->inp_flags & INP_CONTROLOPTS
287: || last->inp_socket->so_options &
288: SO_TIMESTAMP) {
289: ip_savecontrol(last, &opts,
290: ip, n);
291: }
1.49 drochner 292: m_adj(n, iphlen);
1.48 itojun 293: sa = (struct sockaddr *)&udpsrc;
294: #ifdef MAPPED_ADDR_ENABLED
1.49 drochner 295: if (last->inp_socket->so_proto->
296: pr_domain->dom_family == AF_INET6) {
1.48 itojun 297: in6_sin_2_v4mapsin6(&udpsrc,
298: &mapped);
299: sa = (struct sockaddr *)&mapped;
300: }
301: #endif /* MAPPED_ADDR_ENABLED */
1.40 thorpej 302: if (sbappendaddr(
303: &last->inp_socket->so_rcv,
1.48 itojun 304: sa, n, opts) == 0) {
1.4 hpeyerl 305: m_freem(n);
1.40 thorpej 306: if (opts)
307: m_freem(opts);
1.13 mycroft 308: } else
1.40 thorpej 309: sorwakeup(last->inp_socket);
310: opts = 0;
1.4 hpeyerl 311: }
312: }
1.40 thorpej 313: last = inp;
1.4 hpeyerl 314: /*
1.13 mycroft 315: * Don't look for additional matches if this one does
316: * not have either the SO_REUSEPORT or SO_REUSEADDR
317: * socket options set. This heuristic avoids searching
318: * through all pcbs in the common case of a non-shared
319: * port. It * assumes that an application will never
320: * clear these options after setting them.
1.4 hpeyerl 321: */
1.40 thorpej 322: if ((last->inp_socket->so_options &
323: (SO_REUSEPORT|SO_REUSEADDR)) == 0)
1.4 hpeyerl 324: break;
325: }
1.6 mycroft 326:
1.4 hpeyerl 327: if (last == NULL) {
328: /*
329: * No matching pcb found; discard datagram.
330: * (No need to send an ICMP Port Unreachable
331: * for a broadcast or multicast datgram.)
332: */
1.48 itojun 333: udpstat.udps_noport++;
1.13 mycroft 334: udpstat.udps_noportbcast++;
1.4 hpeyerl 335: goto bad;
336: }
1.48 itojun 337: #ifdef IPSEC
338: /* check AH/ESP integrity. */
339: if (last != NULL && ipsec4_in_reject(m, last)) {
340: ipsecstat.in_polvio++;
341: goto bad;
342: }
343: #endif /*IPSEC*/
1.40 thorpej 344: if (last->inp_flags & INP_CONTROLOPTS ||
345: last->inp_socket->so_options & SO_TIMESTAMP)
346: ip_savecontrol(last, &opts, ip, m);
1.49 drochner 347: m->m_len -= iphlen;
348: m->m_pkthdr.len -= iphlen;
349: m->m_data += iphlen;
1.48 itojun 350: sa = (struct sockaddr *)&udpsrc;
351: #ifdef MAPPED_ADDR_ENABLED
352: if (last->inp_socket->so_proto->pr_domain->dom_family ==
353: AF_INET6) {
354: in6_sin_2_v4mapsin6(&udpsrc, &mapped);
355: sa = (struct sockaddr *)&mapped;
356: }
357: #endif /* MAPPED_ADDR_ENABLED */
358: if (sbappendaddr(&last->inp_socket->so_rcv, sa, m, opts) == 0) {
1.13 mycroft 359: udpstat.udps_fullsock++;
1.4 hpeyerl 360: goto bad;
1.13 mycroft 361: }
1.40 thorpej 362: sorwakeup(last->inp_socket);
1.4 hpeyerl 363: return;
364: }
1.1 cgd 365: /*
366: * Locate pcb for datagram.
367: */
1.35 mycroft 368: inp = in_pcblookup_connect(&udbtable, ip->ip_src, uh->uh_sport,
1.26 mycroft 369: ip->ip_dst, uh->uh_dport);
370: if (inp == 0) {
371: ++udpstat.udps_pcbhashmiss;
1.35 mycroft 372: inp = in_pcblookup_bind(&udbtable, ip->ip_dst, uh->uh_dport);
1.18 mycroft 373: if (inp == 0) {
374: udpstat.udps_noport++;
375: if (m->m_flags & (M_BCAST | M_MCAST)) {
376: udpstat.udps_noportbcast++;
377: goto bad;
378: }
379: *ip = save_ip;
1.38 ws 380: #if NIPKDB > 0
381: if (checkipkdb(&ip->ip_src,
382: uh->uh_sport,
383: uh->uh_dport,
384: m,
385: iphlen + sizeof(struct udphdr),
386: len - sizeof(struct udphdr)))
1.37 ws 387: /* It was a debugger connect packet, just drop it now */
388: goto bad;
389: #endif
1.18 mycroft 390: icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PORT, 0, 0);
391: return;
1.13 mycroft 392: }
1.1 cgd 393: }
1.48 itojun 394: #ifdef IPSEC
395: if (inp != NULL && ipsec4_in_reject(m, inp)) {
396: ipsecstat.in_polvio++;
397: goto bad;
398: }
399: #endif /*IPSEC*/
1.1 cgd 400:
401: /*
402: * Stuff source address and datagram in user buffer.
403: */
1.40 thorpej 404: if (inp->inp_flags & INP_CONTROLOPTS ||
405: inp->inp_socket->so_options & SO_TIMESTAMP)
406: ip_savecontrol(inp, &opts, ip, m);
1.1 cgd 407: iphlen += sizeof(struct udphdr);
408: m->m_len -= iphlen;
409: m->m_pkthdr.len -= iphlen;
410: m->m_data += iphlen;
1.48 itojun 411: sa = (struct sockaddr *)&udpsrc;
412: #ifdef MAPPED_ADDR_ENABLED
413: if (inp->inp_socket->so_proto->pr_domain->dom_family == AF_INET6) {
414: in6_sin_2_v4mapsin6(&udpsrc, &mapped);
415: sa = (struct sockaddr *)&mapped;
416: }
417: #endif /* MAPPED_ADDR_ENABLED */
418: if (sbappendaddr(&inp->inp_socket->so_rcv, sa, m, opts) == 0) {
1.1 cgd 419: udpstat.udps_fullsock++;
420: goto bad;
421: }
422: sorwakeup(inp->inp_socket);
423: return;
424: bad:
425: m_freem(m);
426: if (opts)
427: m_freem(opts);
428: }
429:
430: /*
431: * Notify a udp user of an asynchronous error;
432: * just wake up so that he can collect error status.
433: */
1.7 mycroft 434: static void
1.1 cgd 435: udp_notify(inp, errno)
436: register struct inpcb *inp;
1.7 mycroft 437: int errno;
1.1 cgd 438: {
1.34 mycroft 439:
1.1 cgd 440: inp->inp_socket->so_error = errno;
441: sorwakeup(inp->inp_socket);
442: sowwakeup(inp->inp_socket);
443: }
444:
1.27 christos 445: void *
446: udp_ctlinput(cmd, sa, v)
1.1 cgd 447: int cmd;
448: struct sockaddr *sa;
1.27 christos 449: void *v;
1.1 cgd 450: {
1.27 christos 451: register struct ip *ip = v;
1.1 cgd 452: register struct udphdr *uh;
1.21 mycroft 453: extern int inetctlerrmap[];
1.18 mycroft 454: void (*notify) __P((struct inpcb *, int)) = udp_notify;
1.21 mycroft 455: int errno;
1.1 cgd 456:
1.51 itojun 457: if (sa->sa_family != AF_INET ||
458: sa->sa_len != sizeof(struct sockaddr_in))
459: return NULL;
1.20 mycroft 460: if ((unsigned)cmd >= PRC_NCMDS)
1.27 christos 461: return NULL;
1.20 mycroft 462: errno = inetctlerrmap[cmd];
1.18 mycroft 463: if (PRC_IS_REDIRECT(cmd))
1.19 mycroft 464: notify = in_rtchange, ip = 0;
1.18 mycroft 465: else if (cmd == PRC_HOSTDEAD)
1.19 mycroft 466: ip = 0;
1.23 cgd 467: else if (errno == 0)
1.27 christos 468: return NULL;
1.19 mycroft 469: if (ip) {
1.1 cgd 470: uh = (struct udphdr *)((caddr_t)ip + (ip->ip_hl << 2));
1.34 mycroft 471: in_pcbnotify(&udbtable, satosin(sa)->sin_addr, uh->uh_dport,
472: ip->ip_src, uh->uh_sport, errno, notify);
1.19 mycroft 473: } else
1.34 mycroft 474: in_pcbnotifyall(&udbtable, satosin(sa)->sin_addr, errno,
475: notify);
1.27 christos 476: return NULL;
1.1 cgd 477: }
478:
1.7 mycroft 479: int
1.27 christos 480: #if __STDC__
481: udp_output(struct mbuf *m, ...)
482: #else
483: udp_output(m, va_alist)
484: struct mbuf *m;
485: va_dcl
486: #endif
487: {
1.1 cgd 488: register struct inpcb *inp;
489: register struct udpiphdr *ui;
490: register int len = m->m_pkthdr.len;
1.31 mycroft 491: int error = 0;
1.27 christos 492: va_list ap;
493:
494: va_start(ap, m);
495: inp = va_arg(ap, struct inpcb *);
496: va_end(ap);
1.1 cgd 497:
498: /*
499: * Calculate data length and get a mbuf
500: * for UDP and IP headers.
501: */
1.13 mycroft 502: M_PREPEND(m, sizeof(struct udpiphdr), M_DONTWAIT);
503: if (m == 0) {
504: error = ENOBUFS;
1.39 thorpej 505: goto release;
506: }
507:
508: /*
509: * Compute the packet length of the IP header, and
510: * punt if the length looks bogus.
511: */
512: if ((len + sizeof(struct udpiphdr)) > IP_MAXPACKET) {
513: error = EMSGSIZE;
1.13 mycroft 514: goto release;
515: }
1.1 cgd 516:
517: /*
518: * Fill in mbuf with extended UDP header
519: * and addresses and length put into network format.
520: */
521: ui = mtod(m, struct udpiphdr *);
1.25 cgd 522: bzero(ui->ui_x1, sizeof ui->ui_x1);
1.1 cgd 523: ui->ui_pr = IPPROTO_UDP;
1.15 cgd 524: ui->ui_len = htons((u_int16_t)len + sizeof (struct udphdr));
1.1 cgd 525: ui->ui_src = inp->inp_laddr;
526: ui->ui_dst = inp->inp_faddr;
527: ui->ui_sport = inp->inp_lport;
528: ui->ui_dport = inp->inp_fport;
529: ui->ui_ulen = ui->ui_len;
530:
531: /*
532: * Stuff checksum and output datagram.
533: */
534: ui->ui_sum = 0;
535: if (udpcksum) {
536: if ((ui->ui_sum = in_cksum(m, sizeof (struct udpiphdr) + len)) == 0)
537: ui->ui_sum = 0xffff;
538: }
539: ((struct ip *)ui)->ip_len = sizeof (struct udpiphdr) + len;
540: ((struct ip *)ui)->ip_ttl = inp->inp_ip.ip_ttl; /* XXX */
541: ((struct ip *)ui)->ip_tos = inp->inp_ip.ip_tos; /* XXX */
542: udpstat.udps_opackets++;
1.48 itojun 543:
544: #ifdef IPSEC
545: m->m_pkthdr.rcvif = (struct ifnet *)inp->inp_socket;
546: #endif /*IPSEC*/
547:
1.31 mycroft 548: return (ip_output(m, inp->inp_options, &inp->inp_route,
1.12 mycroft 549: inp->inp_socket->so_options & (SO_DONTROUTE | SO_BROADCAST),
1.31 mycroft 550: inp->inp_moptions));
1.1 cgd 551:
552: release:
553: m_freem(m);
554: return (error);
555: }
556:
1.42 thorpej 557: int udp_sendspace = 9216; /* really max datagram size */
558: int udp_recvspace = 40 * (1024 + sizeof(struct sockaddr_in));
1.1 cgd 559: /* 40 1K datagrams */
560:
561: /*ARGSUSED*/
1.7 mycroft 562: int
1.31 mycroft 563: udp_usrreq(so, req, m, nam, control, p)
1.1 cgd 564: struct socket *so;
565: int req;
1.31 mycroft 566: struct mbuf *m, *nam, *control;
1.30 mycroft 567: struct proc *p;
1.1 cgd 568: {
1.31 mycroft 569: register struct inpcb *inp;
1.1 cgd 570: int s;
1.31 mycroft 571: register int error = 0;
1.1 cgd 572:
573: if (req == PRU_CONTROL)
1.31 mycroft 574: return (in_control(so, (long)m, (caddr_t)nam,
1.30 mycroft 575: (struct ifnet *)control, p));
1.31 mycroft 576:
577: s = splsoftnet();
578: inp = sotoinpcb(so);
1.32 mycroft 579: #ifdef DIAGNOSTIC
580: if (req != PRU_SEND && req != PRU_SENDOOB && control)
581: panic("udp_usrreq: unexpected control mbuf");
582: #endif
1.31 mycroft 583: if (inp == 0 && req != PRU_ATTACH) {
584: error = EINVAL;
585: goto release;
586: }
587:
1.1 cgd 588: /*
589: * Note: need to block udp_input while changing
590: * the udp pcb queue and/or pcb addresses.
591: */
592: switch (req) {
593:
594: case PRU_ATTACH:
1.31 mycroft 595: if (inp != 0) {
596: error = EISCONN;
1.1 cgd 597: break;
598: }
1.31 mycroft 599: if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
600: error = soreserve(so, udp_sendspace, udp_recvspace);
601: if (error)
602: break;
603: }
1.18 mycroft 604: error = in_pcballoc(so, &udbtable);
1.1 cgd 605: if (error)
606: break;
1.31 mycroft 607: inp = sotoinpcb(so);
608: inp->inp_ip.ip_ttl = ip_defttl;
1.48 itojun 609: #ifdef IPSEC
610: inp = (struct inpcb *)so->so_pcb;
1.52 ! itojun 611: if ((error = ipsec_init_policy(&inp->inp_sp)) != 0)
! 612: in_pcbdetach(inp);
1.48 itojun 613: #endif /*IPSEC*/
1.1 cgd 614: break;
615:
616: case PRU_DETACH:
1.31 mycroft 617: in_pcbdetach(inp);
1.1 cgd 618: break;
619:
620: case PRU_BIND:
1.31 mycroft 621: error = in_pcbbind(inp, nam, p);
1.1 cgd 622: break;
623:
624: case PRU_LISTEN:
625: error = EOPNOTSUPP;
626: break;
627:
628: case PRU_CONNECT:
1.31 mycroft 629: error = in_pcbconnect(inp, nam);
630: if (error)
1.1 cgd 631: break;
1.31 mycroft 632: soisconnected(so);
1.1 cgd 633: break;
634:
635: case PRU_CONNECT2:
636: error = EOPNOTSUPP;
637: break;
638:
639: case PRU_DISCONNECT:
1.31 mycroft 640: /*soisdisconnected(so);*/
641: so->so_state &= ~SS_ISCONNECTED; /* XXX */
1.1 cgd 642: in_pcbdisconnect(inp);
1.34 mycroft 643: inp->inp_laddr = zeroin_addr; /* XXX */
1.35 mycroft 644: in_pcbstate(inp, INP_BOUND); /* XXX */
1.1 cgd 645: break;
646:
647: case PRU_SHUTDOWN:
648: socantsendmore(so);
649: break;
650:
1.31 mycroft 651: case PRU_RCVD:
652: error = EOPNOTSUPP;
1.1 cgd 653: break;
654:
1.31 mycroft 655: case PRU_SEND:
1.32 mycroft 656: if (control && control->m_len) {
657: m_freem(control);
658: m_freem(m);
659: error = EINVAL;
660: break;
661: }
1.31 mycroft 662: {
1.35 mycroft 663: struct in_addr laddr; /* XXX */
1.1 cgd 664:
1.31 mycroft 665: if (nam) {
1.35 mycroft 666: laddr = inp->inp_laddr; /* XXX */
1.31 mycroft 667: if ((so->so_state & SS_ISCONNECTED) != 0) {
668: error = EISCONN;
1.32 mycroft 669: goto die;
1.31 mycroft 670: }
671: error = in_pcbconnect(inp, nam);
1.32 mycroft 672: if (error) {
673: die:
674: m_freem(m);
1.31 mycroft 675: break;
1.32 mycroft 676: }
1.31 mycroft 677: } else {
678: if ((so->so_state & SS_ISCONNECTED) == 0) {
679: error = ENOTCONN;
1.32 mycroft 680: goto die;
1.31 mycroft 681: }
682: }
1.33 mycroft 683: error = udp_output(m, inp);
1.31 mycroft 684: if (nam) {
685: in_pcbdisconnect(inp);
1.35 mycroft 686: inp->inp_laddr = laddr; /* XXX */
687: in_pcbstate(inp, INP_BOUND); /* XXX */
1.31 mycroft 688: }
689: }
1.1 cgd 690: break;
691:
692: case PRU_SENSE:
693: /*
694: * stat: don't bother with a blocksize.
695: */
1.31 mycroft 696: splx(s);
1.1 cgd 697: return (0);
698:
1.31 mycroft 699: case PRU_RCVOOB:
700: error = EOPNOTSUPP;
701: break;
702:
1.1 cgd 703: case PRU_SENDOOB:
1.32 mycroft 704: m_freem(control);
1.31 mycroft 705: m_freem(m);
1.1 cgd 706: error = EOPNOTSUPP;
707: break;
708:
1.31 mycroft 709: case PRU_SOCKADDR:
710: in_setsockaddr(inp, nam);
711: break;
712:
713: case PRU_PEERADDR:
714: in_setpeeraddr(inp, nam);
715: break;
1.1 cgd 716:
717: default:
718: panic("udp_usrreq");
719: }
720:
721: release:
1.31 mycroft 722: splx(s);
1.1 cgd 723: return (error);
1.13 mycroft 724: }
725:
726: /*
727: * Sysctl for udp variables.
728: */
1.27 christos 729: int
1.13 mycroft 730: udp_sysctl(name, namelen, oldp, oldlenp, newp, newlen)
731: int *name;
732: u_int namelen;
733: void *oldp;
734: size_t *oldlenp;
735: void *newp;
736: size_t newlen;
737: {
738: /* All sysctl names at this level are terminal. */
739: if (namelen != 1)
740: return (ENOTDIR);
741:
742: switch (name[0]) {
743: case UDPCTL_CHECKSUM:
744: return (sysctl_int(oldp, oldlenp, newp, newlen, &udpcksum));
1.42 thorpej 745: case UDPCTL_SENDSPACE:
746: return (sysctl_int(oldp, oldlenp, newp, newlen,
747: &udp_sendspace));
748: case UDPCTL_RECVSPACE:
749: return (sysctl_int(oldp, oldlenp, newp, newlen,
750: &udp_recvspace));
1.13 mycroft 751: default:
752: return (ENOPROTOOPT);
753: }
754: /* NOTREACHED */
1.1 cgd 755: }
CVSweb <webmaster@jp.NetBSD.org>