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

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

CVSweb <webmaster@jp.NetBSD.org>