[BACK]Return to udp_usrreq.c CVS log [TXT][DIR] Up to [cvs.NetBSD.org] / src / sys / netinet

Annotation of src/sys/netinet/udp_usrreq.c, Revision 1.47.4.2

1.47.4.2! thorpej     1: /*     $NetBSD: udp_usrreq.c,v 1.47.4.1 1999/07/01 23:47:04 thorpej Exp $      */
1.47.4.1  thorpej     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.47.4.2! thorpej    66:
        !            67: #include "opt_ipsec.h"
        !            68:
1.38      ws         69: #include "ipkdb.h"
1.1       cgd        70:
1.47.4.1  thorpej    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.47.4.1  thorpej    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.47.4.1  thorpej   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.47.4.1  thorpej   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.47.4.1  thorpej   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.47.4.1  thorpej   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.47.4.1  thorpej   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.47.4.1  thorpej   250:                iphlen += sizeof(struct udphdr);
1.4       hpeyerl   251:                /*
1.47.4.1  thorpej   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.47.4.1  thorpej   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.47.4.2! thorpej   292:                                        m_adj(n, iphlen);
1.47.4.1  thorpej   293:                                        sa = (struct sockaddr *)&udpsrc;
                    294: #ifdef MAPPED_ADDR_ENABLED
1.47.4.2! thorpej   295:                                        if (last->inp_socket->so_proto->
        !           296:                                            pr_domain->dom_family == AF_INET6) {
1.47.4.1  thorpej   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.47.4.1  thorpej   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.47.4.1  thorpej   333:                        udpstat.udps_noport++;
1.13      mycroft   334:                        udpstat.udps_noportbcast++;
1.4       hpeyerl   335:                        goto bad;
                    336:                }
1.47.4.1  thorpej   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.47.4.2! thorpej   347:                m->m_len -= iphlen;
        !           348:                m->m_pkthdr.len -= iphlen;
        !           349:                m->m_data += iphlen;
1.47.4.1  thorpej   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.47.4.1  thorpej   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.47.4.1  thorpej   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.20      mycroft   457:        if ((unsigned)cmd >= PRC_NCMDS)
1.27      christos  458:                return NULL;
1.20      mycroft   459:        errno = inetctlerrmap[cmd];
1.18      mycroft   460:        if (PRC_IS_REDIRECT(cmd))
1.19      mycroft   461:                notify = in_rtchange, ip = 0;
1.18      mycroft   462:        else if (cmd == PRC_HOSTDEAD)
1.19      mycroft   463:                ip = 0;
1.23      cgd       464:        else if (errno == 0)
1.27      christos  465:                return NULL;
1.19      mycroft   466:        if (ip) {
1.1       cgd       467:                uh = (struct udphdr *)((caddr_t)ip + (ip->ip_hl << 2));
1.34      mycroft   468:                in_pcbnotify(&udbtable, satosin(sa)->sin_addr, uh->uh_dport,
                    469:                    ip->ip_src, uh->uh_sport, errno, notify);
1.19      mycroft   470:        } else
1.34      mycroft   471:                in_pcbnotifyall(&udbtable, satosin(sa)->sin_addr, errno,
                    472:                    notify);
1.27      christos  473:        return NULL;
1.1       cgd       474: }
                    475:
1.7       mycroft   476: int
1.27      christos  477: #if __STDC__
                    478: udp_output(struct mbuf *m, ...)
                    479: #else
                    480: udp_output(m, va_alist)
                    481:        struct mbuf *m;
                    482:        va_dcl
                    483: #endif
                    484: {
1.1       cgd       485:        register struct inpcb *inp;
                    486:        register struct udpiphdr *ui;
                    487:        register int len = m->m_pkthdr.len;
1.31      mycroft   488:        int error = 0;
1.27      christos  489:        va_list ap;
                    490:
                    491:        va_start(ap, m);
                    492:        inp = va_arg(ap, struct inpcb *);
                    493:        va_end(ap);
1.1       cgd       494:
                    495:        /*
                    496:         * Calculate data length and get a mbuf
                    497:         * for UDP and IP headers.
                    498:         */
1.13      mycroft   499:        M_PREPEND(m, sizeof(struct udpiphdr), M_DONTWAIT);
                    500:        if (m == 0) {
                    501:                error = ENOBUFS;
1.39      thorpej   502:                goto release;
                    503:        }
                    504:
                    505:        /*
                    506:         * Compute the packet length of the IP header, and
                    507:         * punt if the length looks bogus.
                    508:         */
                    509:        if ((len + sizeof(struct udpiphdr)) > IP_MAXPACKET) {
                    510:                error = EMSGSIZE;
1.13      mycroft   511:                goto release;
                    512:        }
1.1       cgd       513:
                    514:        /*
                    515:         * Fill in mbuf with extended UDP header
                    516:         * and addresses and length put into network format.
                    517:         */
                    518:        ui = mtod(m, struct udpiphdr *);
1.25      cgd       519:        bzero(ui->ui_x1, sizeof ui->ui_x1);
1.1       cgd       520:        ui->ui_pr = IPPROTO_UDP;
1.15      cgd       521:        ui->ui_len = htons((u_int16_t)len + sizeof (struct udphdr));
1.1       cgd       522:        ui->ui_src = inp->inp_laddr;
                    523:        ui->ui_dst = inp->inp_faddr;
                    524:        ui->ui_sport = inp->inp_lport;
                    525:        ui->ui_dport = inp->inp_fport;
                    526:        ui->ui_ulen = ui->ui_len;
                    527:
                    528:        /*
                    529:         * Stuff checksum and output datagram.
                    530:         */
                    531:        ui->ui_sum = 0;
                    532:        if (udpcksum) {
                    533:            if ((ui->ui_sum = in_cksum(m, sizeof (struct udpiphdr) + len)) == 0)
                    534:                ui->ui_sum = 0xffff;
                    535:        }
                    536:        ((struct ip *)ui)->ip_len = sizeof (struct udpiphdr) + len;
                    537:        ((struct ip *)ui)->ip_ttl = inp->inp_ip.ip_ttl; /* XXX */
                    538:        ((struct ip *)ui)->ip_tos = inp->inp_ip.ip_tos; /* XXX */
                    539:        udpstat.udps_opackets++;
1.47.4.1  thorpej   540:
                    541: #ifdef IPSEC
                    542:        m->m_pkthdr.rcvif = (struct ifnet *)inp->inp_socket;
                    543: #endif /*IPSEC*/
                    544:
1.31      mycroft   545:        return (ip_output(m, inp->inp_options, &inp->inp_route,
1.12      mycroft   546:            inp->inp_socket->so_options & (SO_DONTROUTE | SO_BROADCAST),
1.31      mycroft   547:            inp->inp_moptions));
1.1       cgd       548:
                    549: release:
                    550:        m_freem(m);
                    551:        return (error);
                    552: }
                    553:
1.42      thorpej   554: int    udp_sendspace = 9216;           /* really max datagram size */
                    555: int    udp_recvspace = 40 * (1024 + sizeof(struct sockaddr_in));
1.1       cgd       556:                                        /* 40 1K datagrams */
                    557:
                    558: /*ARGSUSED*/
1.7       mycroft   559: int
1.31      mycroft   560: udp_usrreq(so, req, m, nam, control, p)
1.1       cgd       561:        struct socket *so;
                    562:        int req;
1.31      mycroft   563:        struct mbuf *m, *nam, *control;
1.30      mycroft   564:        struct proc *p;
1.1       cgd       565: {
1.31      mycroft   566:        register struct inpcb *inp;
1.1       cgd       567:        int s;
1.31      mycroft   568:        register int error = 0;
1.1       cgd       569:
                    570:        if (req == PRU_CONTROL)
1.31      mycroft   571:                return (in_control(so, (long)m, (caddr_t)nam,
1.30      mycroft   572:                    (struct ifnet *)control, p));
1.31      mycroft   573:
                    574:        s = splsoftnet();
                    575:        inp = sotoinpcb(so);
1.32      mycroft   576: #ifdef DIAGNOSTIC
                    577:        if (req != PRU_SEND && req != PRU_SENDOOB && control)
                    578:                panic("udp_usrreq: unexpected control mbuf");
                    579: #endif
1.31      mycroft   580:        if (inp == 0 && req != PRU_ATTACH) {
                    581:                error = EINVAL;
                    582:                goto release;
                    583:        }
                    584:
1.1       cgd       585:        /*
                    586:         * Note: need to block udp_input while changing
                    587:         * the udp pcb queue and/or pcb addresses.
                    588:         */
                    589:        switch (req) {
                    590:
                    591:        case PRU_ATTACH:
1.31      mycroft   592:                if (inp != 0) {
                    593:                        error = EISCONN;
1.1       cgd       594:                        break;
                    595:                }
1.31      mycroft   596:                if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
                    597:                        error = soreserve(so, udp_sendspace, udp_recvspace);
                    598:                        if (error)
                    599:                                break;
                    600:                }
1.18      mycroft   601:                error = in_pcballoc(so, &udbtable);
1.1       cgd       602:                if (error)
                    603:                        break;
1.31      mycroft   604:                inp = sotoinpcb(so);
                    605:                inp->inp_ip.ip_ttl = ip_defttl;
1.47.4.1  thorpej   606: #ifdef IPSEC
                    607:                inp = (struct inpcb *)so->so_pcb;
                    608:                error = ipsec_init_policy(&inp->inp_sp);
                    609: #endif /*IPSEC*/
1.1       cgd       610:                break;
                    611:
                    612:        case PRU_DETACH:
1.31      mycroft   613:                in_pcbdetach(inp);
1.1       cgd       614:                break;
                    615:
                    616:        case PRU_BIND:
1.31      mycroft   617:                error = in_pcbbind(inp, nam, p);
1.1       cgd       618:                break;
                    619:
                    620:        case PRU_LISTEN:
                    621:                error = EOPNOTSUPP;
                    622:                break;
                    623:
                    624:        case PRU_CONNECT:
1.31      mycroft   625:                error = in_pcbconnect(inp, nam);
                    626:                if (error)
1.1       cgd       627:                        break;
1.31      mycroft   628:                soisconnected(so);
1.1       cgd       629:                break;
                    630:
                    631:        case PRU_CONNECT2:
                    632:                error = EOPNOTSUPP;
                    633:                break;
                    634:
                    635:        case PRU_DISCONNECT:
1.31      mycroft   636:                /*soisdisconnected(so);*/
                    637:                so->so_state &= ~SS_ISCONNECTED;        /* XXX */
1.1       cgd       638:                in_pcbdisconnect(inp);
1.34      mycroft   639:                inp->inp_laddr = zeroin_addr;           /* XXX */
1.35      mycroft   640:                in_pcbstate(inp, INP_BOUND);            /* XXX */
1.1       cgd       641:                break;
                    642:
                    643:        case PRU_SHUTDOWN:
                    644:                socantsendmore(so);
                    645:                break;
                    646:
1.31      mycroft   647:        case PRU_RCVD:
                    648:                error = EOPNOTSUPP;
1.1       cgd       649:                break;
                    650:
1.31      mycroft   651:        case PRU_SEND:
1.32      mycroft   652:                if (control && control->m_len) {
                    653:                        m_freem(control);
                    654:                        m_freem(m);
                    655:                        error = EINVAL;
                    656:                        break;
                    657:                }
1.31      mycroft   658:        {
1.35      mycroft   659:                struct in_addr laddr;                   /* XXX */
1.1       cgd       660:
1.31      mycroft   661:                if (nam) {
1.35      mycroft   662:                        laddr = inp->inp_laddr;         /* XXX */
1.31      mycroft   663:                        if ((so->so_state & SS_ISCONNECTED) != 0) {
                    664:                                error = EISCONN;
1.32      mycroft   665:                                goto die;
1.31      mycroft   666:                        }
                    667:                        error = in_pcbconnect(inp, nam);
1.32      mycroft   668:                        if (error) {
                    669:                        die:
                    670:                                m_freem(m);
1.31      mycroft   671:                                break;
1.32      mycroft   672:                        }
1.31      mycroft   673:                } else {
                    674:                        if ((so->so_state & SS_ISCONNECTED) == 0) {
                    675:                                error = ENOTCONN;
1.32      mycroft   676:                                goto die;
1.31      mycroft   677:                        }
                    678:                }
1.33      mycroft   679:                error = udp_output(m, inp);
1.31      mycroft   680:                if (nam) {
                    681:                        in_pcbdisconnect(inp);
1.35      mycroft   682:                        inp->inp_laddr = laddr;         /* XXX */
                    683:                        in_pcbstate(inp, INP_BOUND);    /* XXX */
1.31      mycroft   684:                }
                    685:        }
1.1       cgd       686:                break;
                    687:
                    688:        case PRU_SENSE:
                    689:                /*
                    690:                 * stat: don't bother with a blocksize.
                    691:                 */
1.31      mycroft   692:                splx(s);
1.1       cgd       693:                return (0);
                    694:
1.31      mycroft   695:        case PRU_RCVOOB:
                    696:                error =  EOPNOTSUPP;
                    697:                break;
                    698:
1.1       cgd       699:        case PRU_SENDOOB:
1.32      mycroft   700:                m_freem(control);
1.31      mycroft   701:                m_freem(m);
1.1       cgd       702:                error =  EOPNOTSUPP;
                    703:                break;
                    704:
1.31      mycroft   705:        case PRU_SOCKADDR:
                    706:                in_setsockaddr(inp, nam);
                    707:                break;
                    708:
                    709:        case PRU_PEERADDR:
                    710:                in_setpeeraddr(inp, nam);
                    711:                break;
1.1       cgd       712:
                    713:        default:
                    714:                panic("udp_usrreq");
                    715:        }
                    716:
                    717: release:
1.31      mycroft   718:        splx(s);
1.1       cgd       719:        return (error);
1.13      mycroft   720: }
                    721:
                    722: /*
                    723:  * Sysctl for udp variables.
                    724:  */
1.27      christos  725: int
1.13      mycroft   726: udp_sysctl(name, namelen, oldp, oldlenp, newp, newlen)
                    727:        int *name;
                    728:        u_int namelen;
                    729:        void *oldp;
                    730:        size_t *oldlenp;
                    731:        void *newp;
                    732:        size_t newlen;
                    733: {
                    734:        /* All sysctl names at this level are terminal. */
                    735:        if (namelen != 1)
                    736:                return (ENOTDIR);
                    737:
                    738:        switch (name[0]) {
                    739:        case UDPCTL_CHECKSUM:
                    740:                return (sysctl_int(oldp, oldlenp, newp, newlen, &udpcksum));
1.42      thorpej   741:        case UDPCTL_SENDSPACE:
                    742:                return (sysctl_int(oldp, oldlenp, newp, newlen,
                    743:                    &udp_sendspace));
                    744:        case UDPCTL_RECVSPACE:
                    745:                return (sysctl_int(oldp, oldlenp, newp, newlen,
                    746:                    &udp_recvspace));
1.13      mycroft   747:        default:
                    748:                return (ENOPROTOOPT);
                    749:        }
                    750:        /* NOTREACHED */
1.1       cgd       751: }

CVSweb <webmaster@jp.NetBSD.org>