Home | History | Annotate | Line # | Download | only in libsa
udp.c revision 1.11.56.1
      1  1.11.56.1  christos /*	$NetBSD: udp.c,v 1.11.56.1 2019/06/10 22:09:05 christos Exp $	*/
      2        1.1   thorpej 
      3        1.1   thorpej /*
      4        1.1   thorpej  * Copyright (c) 1992 Regents of the University of California.
      5        1.1   thorpej  * All rights reserved.
      6        1.1   thorpej  *
      7        1.1   thorpej  * This software was developed by the Computer Systems Engineering group
      8        1.1   thorpej  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
      9        1.1   thorpej  * contributed to Berkeley.
     10        1.1   thorpej  *
     11        1.1   thorpej  * Redistribution and use in source and binary forms, with or without
     12        1.1   thorpej  * modification, are permitted provided that the following conditions
     13        1.1   thorpej  * are met:
     14        1.1   thorpej  * 1. Redistributions of source code must retain the above copyright
     15        1.1   thorpej  *    notice, this list of conditions and the following disclaimer.
     16        1.1   thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     17        1.1   thorpej  *    notice, this list of conditions and the following disclaimer in the
     18        1.1   thorpej  *    documentation and/or other materials provided with the distribution.
     19        1.1   thorpej  * 3. All advertising materials mentioning features or use of this software
     20        1.1   thorpej  *    must display the following acknowledgement:
     21        1.1   thorpej  *	This product includes software developed by the University of
     22        1.1   thorpej  *	California, Lawrence Berkeley Laboratory and its contributors.
     23        1.1   thorpej  * 4. Neither the name of the University nor the names of its contributors
     24        1.1   thorpej  *    may be used to endorse or promote products derived from this software
     25        1.1   thorpej  *    without specific prior written permission.
     26        1.1   thorpej  *
     27        1.1   thorpej  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     28        1.1   thorpej  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     29        1.1   thorpej  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     30        1.1   thorpej  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     31        1.1   thorpej  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     32        1.1   thorpej  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     33        1.1   thorpej  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     34        1.1   thorpej  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     35        1.1   thorpej  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     36        1.1   thorpej  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     37        1.1   thorpej  * SUCH DAMAGE.
     38        1.1   thorpej  *
     39        1.1   thorpej  * @(#) Header: net.c,v 1.9 93/08/06 19:32:15 leres Exp  (LBL)
     40        1.1   thorpej  */
     41        1.1   thorpej 
     42        1.1   thorpej #include <sys/param.h>
     43        1.1   thorpej #include <sys/socket.h>
     44        1.1   thorpej 
     45        1.1   thorpej #ifdef _STANDALONE
     46        1.1   thorpej #include <lib/libkern/libkern.h>
     47        1.1   thorpej #else
     48        1.1   thorpej #include <string.h>
     49        1.1   thorpej #endif
     50        1.1   thorpej 
     51        1.1   thorpej #include <net/if.h>
     52        1.1   thorpej #include <net/if_ether.h>
     53        1.1   thorpej 
     54        1.1   thorpej #include <netinet/in.h>
     55        1.1   thorpej #include <netinet/in_systm.h>
     56        1.1   thorpej #include <netinet/ip.h>
     57        1.1   thorpej #include <netinet/ip_var.h>
     58        1.1   thorpej #include <netinet/udp.h>
     59        1.1   thorpej #include <netinet/udp_var.h>
     60        1.1   thorpej 
     61        1.1   thorpej #include "stand.h"
     62        1.1   thorpej #include "net.h"
     63        1.1   thorpej 
     64        1.1   thorpej 
     65        1.1   thorpej /* Caller must leave room for ethernet, ip and udp headers in front!! */
     66        1.1   thorpej ssize_t
     67        1.6     isaki sendudp(struct iodesc *d, void *pkt, size_t len)
     68        1.1   thorpej {
     69        1.1   thorpej 	ssize_t cc;
     70        1.1   thorpej 	struct udphdr *uh;
     71        1.1   thorpej 
     72        1.1   thorpej #ifdef NET_DEBUG
     73        1.1   thorpej  	if (debug) {
     74  1.11.56.1  christos 		printf("%s: d=%p called.\n", __func__, d);
     75        1.1   thorpej 		if (d) {
     76        1.1   thorpej 			printf("saddr: %s:%d",
     77        1.1   thorpej 			    inet_ntoa(d->myip), ntohs(d->myport));
     78        1.1   thorpej 			printf(" daddr: %s:%d\n",
     79        1.1   thorpej 			    inet_ntoa(d->destip), ntohs(d->destport));
     80        1.1   thorpej 		}
     81        1.1   thorpej 	}
     82        1.1   thorpej #endif
     83        1.1   thorpej 
     84        1.1   thorpej 	uh = (struct udphdr *)pkt - 1;
     85       1.11    zoltan 	len += sizeof(*uh);
     86        1.1   thorpej 
     87       1.11    zoltan 	(void)memset(uh, 0, sizeof(*uh));
     88        1.1   thorpej 
     89        1.1   thorpej 	uh->uh_sport = d->myport;
     90        1.1   thorpej 	uh->uh_dport = d->destport;
     91       1.11    zoltan 	uh->uh_ulen = htons(len);
     92        1.1   thorpej 
     93       1.11    zoltan 	cc = sendip(d, uh, len, IPPROTO_UDP);
     94        1.1   thorpej 	if (cc == -1)
     95        1.6     isaki 		return -1;
     96        1.3      fvdl 	if ((size_t)cc != len)
     97  1.11.56.1  christos 		panic("%s: bad write (%zd != %zu)", __func__, cc, len);
     98       1.11    zoltan 	return (cc - sizeof(*uh));
     99        1.1   thorpej }
    100        1.1   thorpej 
    101        1.1   thorpej /*
    102        1.1   thorpej  * Receive a UDP packet and validate it is for us.
    103        1.1   thorpej  * Caller leaves room for the headers (Ether, IP, UDP)
    104        1.1   thorpej  */
    105        1.1   thorpej ssize_t
    106        1.8   tsutsui readudp(struct iodesc *d, void *pkt, size_t len, saseconds_t tleft)
    107        1.1   thorpej {
    108        1.1   thorpej 	ssize_t n;
    109        1.1   thorpej 	struct udphdr *uh;
    110        1.1   thorpej 
    111        1.1   thorpej 	uh = (struct udphdr *)pkt - 1;
    112       1.11    zoltan 	n = readip(d, uh, len + sizeof(*uh), tleft, IPPROTO_UDP);
    113       1.11    zoltan 	if (n == -1 || (size_t)n < sizeof(*uh))
    114        1.1   thorpej 		return -1;
    115        1.1   thorpej 
    116        1.1   thorpej 	if (uh->uh_dport != d->myport) {
    117        1.1   thorpej #ifdef NET_DEBUG
    118        1.1   thorpej 		if (debug)
    119  1.11.56.1  christos 			printf("%s: bad dport %d != %d\n", __func__,
    120  1.11.56.1  christos 			    ntohs(d->myport), ntohs(uh->uh_dport));
    121        1.1   thorpej #endif
    122        1.1   thorpej 		return -1;
    123        1.1   thorpej 	}
    124        1.1   thorpej 
    125        1.1   thorpej 	if (ntohs(uh->uh_ulen) < sizeof(*uh)) {
    126        1.1   thorpej #ifdef NET_DEBUG
    127        1.1   thorpej 		if (debug)
    128  1.11.56.1  christos 			printf("%s: bad udp len %d < %zu\n", __func__,
    129  1.11.56.1  christos 				ntohs(uh->uh_ulen), sizeof(*uh));
    130        1.1   thorpej #endif
    131        1.1   thorpej 		return -1;
    132        1.1   thorpej 	}
    133        1.1   thorpej 
    134       1.11    zoltan 	n -= sizeof(*uh);
    135        1.6     isaki 	return n;
    136        1.1   thorpej }
    137