The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/lib/libsa/udp.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*      $NetBSD: udp.c,v 1.3 2003/08/31 22:40:49 fvdl Exp $     */
    2 
    3 /*
    4  * Copyright (c) 1992 Regents of the University of California.
    5  * All rights reserved.
    6  *
    7  * This software was developed by the Computer Systems Engineering group
    8  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
    9  * contributed to Berkeley.
   10  *
   11  * Redistribution and use in source and binary forms, with or without
   12  * modification, are permitted provided that the following conditions
   13  * are met:
   14  * 1. Redistributions of source code must retain the above copyright
   15  *    notice, this list of conditions and the following disclaimer.
   16  * 2. Redistributions in binary form must reproduce the above copyright
   17  *    notice, this list of conditions and the following disclaimer in the
   18  *    documentation and/or other materials provided with the distribution.
   19  * 3. All advertising materials mentioning features or use of this software
   20  *    must display the following acknowledgement:
   21  *      This product includes software developed by the University of
   22  *      California, Lawrence Berkeley Laboratory and its contributors.
   23  * 4. Neither the name of the University nor the names of its contributors
   24  *    may be used to endorse or promote products derived from this software
   25  *    without specific prior written permission.
   26  *
   27  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   28  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   29  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   30  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   31  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   32  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   33  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   34  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   35  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   36  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   37  * SUCH DAMAGE.
   38  *
   39  * @(#) Header: net.c,v 1.9 93/08/06 19:32:15 leres Exp  (LBL)
   40  */
   41 
   42 #include <sys/param.h>
   43 #include <sys/socket.h>
   44 
   45 #ifdef _STANDALONE
   46 #include <lib/libkern/libkern.h>
   47 #else
   48 #include <string.h>
   49 #endif
   50 
   51 #include <net/if.h>
   52 #include <net/if_ether.h>
   53 
   54 #include <netinet/in.h>
   55 #include <netinet/in_systm.h>
   56 #include <netinet/ip.h>
   57 #include <netinet/ip_var.h>
   58 #include <netinet/udp.h>
   59 #include <netinet/udp_var.h>
   60 
   61 #include "stand.h"
   62 #include "net.h"
   63 
   64 
   65 /* Caller must leave room for ethernet, ip and udp headers in front!! */
   66 ssize_t
   67 sendudp(d, pkt, len)
   68         struct iodesc *d;
   69         void *pkt;
   70         size_t len;
   71 {
   72         ssize_t cc;
   73         struct ip *ip;
   74         struct udphdr *uh;
   75         u_char *ea;
   76 
   77 #ifdef NET_DEBUG
   78         if (debug) {
   79                 printf("sendudp: d=%lx called.\n", (long)d);
   80                 if (d) {
   81                         printf("saddr: %s:%d",
   82                             inet_ntoa(d->myip), ntohs(d->myport));
   83                         printf(" daddr: %s:%d\n",
   84                             inet_ntoa(d->destip), ntohs(d->destport));
   85                 }
   86         }
   87 #endif
   88 
   89         uh = (struct udphdr *)pkt - 1;
   90         ip = (struct ip *)uh - 1;
   91         len += sizeof(*ip) + sizeof(*uh);
   92 
   93         bzero(ip, sizeof(*ip) + sizeof(*uh));
   94 
   95         ip->ip_v = IPVERSION;                   /* half-char */
   96         ip->ip_hl = sizeof(*ip) >> 2;           /* half-char */
   97         ip->ip_len = htons(len);
   98         ip->ip_p = IPPROTO_UDP;                 /* char */
   99         ip->ip_ttl = IPDEFTTL;                  /* char */
  100         ip->ip_src = d->myip;
  101         ip->ip_dst = d->destip;
  102         ip->ip_sum = in_cksum(ip, sizeof(*ip));  /* short, but special */
  103 
  104         uh->uh_sport = d->myport;
  105         uh->uh_dport = d->destport;
  106         uh->uh_ulen = htons(len - sizeof(*ip));
  107 
  108 #ifndef UDP_NO_CKSUM
  109         {
  110                 struct udpiphdr *ui;
  111                 struct ip tip;
  112 
  113                 /* Calculate checksum (must save and restore ip header) */
  114                 tip = *ip;
  115                 ui = (struct udpiphdr *)ip;
  116                 bzero(ui->ui_x1, sizeof(ui->ui_x1));
  117                 ui->ui_len = uh->uh_ulen;
  118                 uh->uh_sum = in_cksum(ui, len);
  119                 *ip = tip;
  120         }
  121 #endif
  122 
  123         if (ip->ip_dst.s_addr == INADDR_BROADCAST || ip->ip_src.s_addr == 0 ||
  124             netmask == 0 || SAMENET(ip->ip_src, ip->ip_dst, netmask))
  125                 ea = arpwhohas(d, ip->ip_dst);
  126         else
  127                 ea = arpwhohas(d, gateip);
  128 
  129         cc = sendether(d, ip, len, ea, ETHERTYPE_IP);
  130         if (cc == -1)
  131                 return (-1);
  132         if ((size_t)cc != len)
  133                 panic("sendudp: bad write (%d != %d)", cc, len);
  134         return (cc - (sizeof(*ip) + sizeof(*uh)));
  135 }
  136 
  137 /*
  138  * Receive a UDP packet and validate it is for us.
  139  * Caller leaves room for the headers (Ether, IP, UDP)
  140  */
  141 ssize_t
  142 readudp(d, pkt, len, tleft)
  143         struct iodesc *d;
  144         void *pkt;
  145         size_t len;
  146         time_t tleft;
  147 {
  148         ssize_t n;
  149         size_t hlen;
  150         struct ip *ip;
  151         struct udphdr *uh;
  152         u_int16_t etype;        /* host order */
  153 
  154 #ifdef NET_DEBUG
  155         if (debug)
  156                 printf("readudp: called\n");
  157 #endif
  158 
  159         uh = (struct udphdr *)pkt - 1;
  160         ip = (struct ip *)uh - 1;
  161 
  162         n = readether(d, ip, len + sizeof(*ip) + sizeof(*uh), tleft, &etype);
  163         if (n == -1 || (size_t)n < sizeof(*ip) + sizeof(*uh))
  164                 return -1;
  165 
  166         /* Ethernet address checks now in readether() */
  167 
  168         /* Need to respond to ARP requests. */
  169         if (etype == ETHERTYPE_ARP) {
  170                 struct arphdr *ah = (void *)ip;
  171                 if (ah->ar_op == htons(ARPOP_REQUEST)) {
  172                         /* Send ARP reply */
  173                         arp_reply(d, ah);
  174                 }
  175                 return -1;
  176         }
  177 
  178         if (etype != ETHERTYPE_IP) {
  179 #ifdef NET_DEBUG
  180                 if (debug)
  181                         printf("readudp: not IP. ether_type=%x\n", etype);
  182 #endif
  183                 return -1;
  184         }
  185 
  186         /* Check ip header */
  187         if (ip->ip_v != IPVERSION ||
  188             ip->ip_p != IPPROTO_UDP) {  /* half char */
  189 #ifdef NET_DEBUG
  190                 if (debug)
  191                         printf("readudp: IP version or not UDP. ip_v=%d ip_p=%d\n", ip->ip_v, ip->ip_p);
  192 #endif
  193                 return -1;
  194         }
  195 
  196         hlen = ip->ip_hl << 2;
  197         if (hlen < sizeof(*ip) ||
  198             in_cksum(ip, hlen) != 0) {
  199 #ifdef NET_DEBUG
  200                 if (debug)
  201                         printf("readudp: short hdr or bad cksum.\n");
  202 #endif
  203                 return -1;
  204         }
  205         if (n < ntohs(ip->ip_len)) {
  206 #ifdef NET_DEBUG
  207                 if (debug)
  208                         printf("readudp: bad length %d < %d.\n",
  209                                (int)n, ntohs(ip->ip_len));
  210 #endif
  211                 return -1;
  212         }
  213         if (d->myip.s_addr && ip->ip_dst.s_addr != d->myip.s_addr) {
  214 #ifdef NET_DEBUG
  215                 if (debug) {
  216                         printf("readudp: bad saddr %s != ", inet_ntoa(d->myip));
  217                         printf("%s\n", inet_ntoa(ip->ip_dst));
  218                 }
  219 #endif
  220                 return -1;
  221         }
  222 
  223         /* If there were ip options, make them go away */
  224         if (hlen != sizeof(*ip)) {
  225                 bcopy(((u_char *)ip) + hlen, uh, len - hlen);
  226                 ip->ip_len = htons(sizeof(*ip));
  227                 n -= hlen - sizeof(*ip);
  228         }
  229         if (uh->uh_dport != d->myport) {
  230 #ifdef NET_DEBUG
  231                 if (debug)
  232                         printf("readudp: bad dport %d != %d\n",
  233                                 d->myport, ntohs(uh->uh_dport));
  234 #endif
  235                 return -1;
  236         }
  237 
  238 #ifndef UDP_NO_CKSUM
  239         if (uh->uh_sum) {
  240                 struct udpiphdr *ui;
  241                 struct ip tip;
  242 
  243                 n = ntohs(uh->uh_ulen) + sizeof(*ip);
  244                 if (n > RECV_SIZE - ETHER_SIZE) {
  245                         printf("readudp: huge packet, udp len %d\n", (int)n);
  246                         return -1;
  247                 }
  248 
  249                 /* Check checksum (must save and restore ip header) */
  250                 tip = *ip;
  251                 ui = (struct udpiphdr *)ip;
  252                 bzero(ui->ui_x1, sizeof(ui->ui_x1));
  253                 ui->ui_len = uh->uh_ulen;
  254                 if (in_cksum(ui, n) != 0) {
  255 #ifdef NET_DEBUG
  256                         if (debug)
  257                                 printf("readudp: bad cksum\n");
  258 #endif
  259                         *ip = tip;
  260                         return -1;
  261                 }
  262                 *ip = tip;
  263         }
  264 #endif
  265         if (ntohs(uh->uh_ulen) < sizeof(*uh)) {
  266 #ifdef NET_DEBUG
  267                 if (debug)
  268                         printf("readudp: bad udp len %d < %d\n",
  269                                 ntohs(uh->uh_ulen), (int)sizeof(*uh));
  270 #endif
  271                 return -1;
  272         }
  273 
  274         n -= sizeof(*ip) + sizeof(*uh);
  275         return (n);
  276 }

Cache object: 4fa07de696b412f77a1a57ada5ffb314


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.