[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.6.2

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

CVSweb <webmaster@jp.NetBSD.org>