Home | History | Annotate | Line # | Download | only in libsa
udp.c revision 1.7.4.1
      1  1.7.4.1      yamt /*	$NetBSD: udp.c,v 1.7.4.1 2009/05/04 08:13:52 yamt 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 ip *ip;
     71      1.1   thorpej 	struct udphdr *uh;
     72      1.1   thorpej 	u_char *ea;
     73      1.1   thorpej 
     74      1.1   thorpej #ifdef NET_DEBUG
     75      1.1   thorpej  	if (debug) {
     76      1.1   thorpej 		printf("sendudp: d=%lx called.\n", (long)d);
     77      1.1   thorpej 		if (d) {
     78      1.1   thorpej 			printf("saddr: %s:%d",
     79      1.1   thorpej 			    inet_ntoa(d->myip), ntohs(d->myport));
     80      1.1   thorpej 			printf(" daddr: %s:%d\n",
     81      1.1   thorpej 			    inet_ntoa(d->destip), ntohs(d->destport));
     82      1.1   thorpej 		}
     83      1.1   thorpej 	}
     84      1.1   thorpej #endif
     85      1.1   thorpej 
     86      1.1   thorpej 	uh = (struct udphdr *)pkt - 1;
     87      1.1   thorpej 	ip = (struct ip *)uh - 1;
     88      1.1   thorpej 	len += sizeof(*ip) + sizeof(*uh);
     89      1.1   thorpej 
     90      1.7  christos 	(void)memset(ip, 0, sizeof(*ip) + sizeof(*uh));
     91      1.1   thorpej 
     92      1.1   thorpej 	ip->ip_v = IPVERSION;			/* half-char */
     93      1.1   thorpej 	ip->ip_hl = sizeof(*ip) >> 2;		/* half-char */
     94      1.1   thorpej 	ip->ip_len = htons(len);
     95      1.1   thorpej 	ip->ip_p = IPPROTO_UDP;			/* char */
     96      1.2     ragge 	ip->ip_ttl = IPDEFTTL;			/* char */
     97      1.1   thorpej 	ip->ip_src = d->myip;
     98      1.1   thorpej 	ip->ip_dst = d->destip;
     99      1.5  christos 	ip->ip_sum = ip_cksum(ip, sizeof(*ip));	 /* short, but special */
    100      1.1   thorpej 
    101      1.1   thorpej 	uh->uh_sport = d->myport;
    102      1.1   thorpej 	uh->uh_dport = d->destport;
    103      1.1   thorpej 	uh->uh_ulen = htons(len - sizeof(*ip));
    104      1.1   thorpej 
    105      1.1   thorpej #ifndef UDP_NO_CKSUM
    106      1.1   thorpej 	{
    107      1.1   thorpej 		struct udpiphdr *ui;
    108      1.1   thorpej 		struct ip tip;
    109      1.1   thorpej 
    110      1.1   thorpej 		/* Calculate checksum (must save and restore ip header) */
    111      1.1   thorpej 		tip = *ip;
    112      1.1   thorpej 		ui = (struct udpiphdr *)ip;
    113      1.7  christos 		(void)memset(ui->ui_x1, 0, sizeof(ui->ui_x1));
    114      1.1   thorpej 		ui->ui_len = uh->uh_ulen;
    115      1.5  christos 		uh->uh_sum = ip_cksum(ui, len);
    116      1.1   thorpej 		*ip = tip;
    117      1.1   thorpej 	}
    118      1.1   thorpej #endif
    119      1.1   thorpej 
    120      1.1   thorpej 	if (ip->ip_dst.s_addr == INADDR_BROADCAST || ip->ip_src.s_addr == 0 ||
    121      1.1   thorpej 	    netmask == 0 || SAMENET(ip->ip_src, ip->ip_dst, netmask))
    122      1.1   thorpej 		ea = arpwhohas(d, ip->ip_dst);
    123      1.1   thorpej 	else
    124      1.1   thorpej 		ea = arpwhohas(d, gateip);
    125      1.1   thorpej 
    126      1.1   thorpej 	cc = sendether(d, ip, len, ea, ETHERTYPE_IP);
    127      1.1   thorpej 	if (cc == -1)
    128      1.6     isaki 		return -1;
    129      1.3      fvdl 	if ((size_t)cc != len)
    130      1.1   thorpej 		panic("sendudp: bad write (%d != %d)", cc, len);
    131      1.1   thorpej 	return (cc - (sizeof(*ip) + sizeof(*uh)));
    132      1.1   thorpej }
    133      1.1   thorpej 
    134      1.1   thorpej /*
    135      1.1   thorpej  * Receive a UDP packet and validate it is for us.
    136      1.1   thorpej  * Caller leaves room for the headers (Ether, IP, UDP)
    137      1.1   thorpej  */
    138      1.1   thorpej ssize_t
    139  1.7.4.1      yamt readudp(struct iodesc *d, void *pkt, size_t len, saseconds_t tleft)
    140      1.1   thorpej {
    141      1.1   thorpej 	ssize_t n;
    142      1.1   thorpej 	size_t hlen;
    143      1.1   thorpej 	struct ip *ip;
    144      1.1   thorpej 	struct udphdr *uh;
    145      1.1   thorpej 	u_int16_t etype;	/* host order */
    146      1.1   thorpej 
    147      1.1   thorpej #ifdef NET_DEBUG
    148      1.1   thorpej 	if (debug)
    149      1.1   thorpej 		printf("readudp: called\n");
    150      1.1   thorpej #endif
    151      1.1   thorpej 
    152      1.1   thorpej 	uh = (struct udphdr *)pkt - 1;
    153      1.1   thorpej 	ip = (struct ip *)uh - 1;
    154      1.1   thorpej 
    155      1.1   thorpej 	n = readether(d, ip, len + sizeof(*ip) + sizeof(*uh), tleft, &etype);
    156      1.3      fvdl 	if (n == -1 || (size_t)n < sizeof(*ip) + sizeof(*uh))
    157      1.1   thorpej 		return -1;
    158      1.1   thorpej 
    159      1.1   thorpej 	/* Ethernet address checks now in readether() */
    160      1.1   thorpej 
    161      1.1   thorpej 	/* Need to respond to ARP requests. */
    162      1.1   thorpej 	if (etype == ETHERTYPE_ARP) {
    163      1.1   thorpej 		struct arphdr *ah = (void *)ip;
    164      1.1   thorpej 		if (ah->ar_op == htons(ARPOP_REQUEST)) {
    165      1.1   thorpej 			/* Send ARP reply */
    166      1.1   thorpej 			arp_reply(d, ah);
    167      1.1   thorpej 		}
    168      1.1   thorpej 		return -1;
    169      1.1   thorpej 	}
    170      1.1   thorpej 
    171      1.1   thorpej 	if (etype != ETHERTYPE_IP) {
    172      1.1   thorpej #ifdef NET_DEBUG
    173      1.1   thorpej 		if (debug)
    174      1.1   thorpej 			printf("readudp: not IP. ether_type=%x\n", etype);
    175      1.1   thorpej #endif
    176      1.1   thorpej 		return -1;
    177      1.1   thorpej 	}
    178      1.1   thorpej 
    179      1.1   thorpej 	/* Check ip header */
    180      1.1   thorpej 	if (ip->ip_v != IPVERSION ||
    181      1.1   thorpej 	    ip->ip_p != IPPROTO_UDP) {	/* half char */
    182      1.1   thorpej #ifdef NET_DEBUG
    183      1.6     isaki 		if (debug) {
    184      1.6     isaki 			printf("readudp: IP version or not UDP. "
    185      1.6     isaki 			       "ip_v=%d ip_p=%d\n", ip->ip_v, ip->ip_p);
    186      1.6     isaki 		}
    187      1.1   thorpej #endif
    188      1.1   thorpej 		return -1;
    189      1.1   thorpej 	}
    190      1.1   thorpej 
    191      1.1   thorpej 	hlen = ip->ip_hl << 2;
    192      1.6     isaki 	if (hlen < sizeof(*ip) || ip_cksum(ip, hlen) != 0) {
    193      1.1   thorpej #ifdef NET_DEBUG
    194      1.1   thorpej 		if (debug)
    195      1.1   thorpej 			printf("readudp: short hdr or bad cksum.\n");
    196      1.1   thorpej #endif
    197      1.1   thorpej 		return -1;
    198      1.1   thorpej 	}
    199      1.1   thorpej 	if (n < ntohs(ip->ip_len)) {
    200      1.1   thorpej #ifdef NET_DEBUG
    201      1.1   thorpej 		if (debug)
    202      1.1   thorpej 			printf("readudp: bad length %d < %d.\n",
    203      1.1   thorpej 			       (int)n, ntohs(ip->ip_len));
    204      1.1   thorpej #endif
    205      1.1   thorpej 		return -1;
    206      1.1   thorpej 	}
    207      1.1   thorpej 	if (d->myip.s_addr && ip->ip_dst.s_addr != d->myip.s_addr) {
    208      1.1   thorpej #ifdef NET_DEBUG
    209      1.1   thorpej 		if (debug) {
    210      1.1   thorpej 			printf("readudp: bad saddr %s != ", inet_ntoa(d->myip));
    211      1.1   thorpej 			printf("%s\n", inet_ntoa(ip->ip_dst));
    212      1.1   thorpej 		}
    213      1.1   thorpej #endif
    214      1.1   thorpej 		return -1;
    215      1.1   thorpej 	}
    216      1.1   thorpej 
    217      1.1   thorpej 	/* If there were ip options, make them go away */
    218      1.1   thorpej 	if (hlen != sizeof(*ip)) {
    219      1.7  christos 		(void)memcpy(uh, ((u_char *)ip) + hlen, len - hlen);
    220      1.1   thorpej 		ip->ip_len = htons(sizeof(*ip));
    221      1.1   thorpej 		n -= hlen - sizeof(*ip);
    222      1.1   thorpej 	}
    223      1.1   thorpej 	if (uh->uh_dport != d->myport) {
    224      1.1   thorpej #ifdef NET_DEBUG
    225      1.1   thorpej 		if (debug)
    226      1.1   thorpej 			printf("readudp: bad dport %d != %d\n",
    227      1.1   thorpej 				d->myport, ntohs(uh->uh_dport));
    228      1.1   thorpej #endif
    229      1.1   thorpej 		return -1;
    230      1.1   thorpej 	}
    231      1.1   thorpej 
    232      1.1   thorpej #ifndef UDP_NO_CKSUM
    233      1.1   thorpej 	if (uh->uh_sum) {
    234      1.1   thorpej 		struct udpiphdr *ui;
    235      1.1   thorpej 		struct ip tip;
    236      1.1   thorpej 
    237      1.1   thorpej 		n = ntohs(uh->uh_ulen) + sizeof(*ip);
    238      1.1   thorpej 		if (n > RECV_SIZE - ETHER_SIZE) {
    239      1.1   thorpej 			printf("readudp: huge packet, udp len %d\n", (int)n);
    240      1.1   thorpej 			return -1;
    241      1.1   thorpej 		}
    242      1.1   thorpej 
    243      1.1   thorpej 		/* Check checksum (must save and restore ip header) */
    244      1.1   thorpej 		tip = *ip;
    245      1.1   thorpej 		ui = (struct udpiphdr *)ip;
    246      1.7  christos 		(void)memset(ui->ui_x1, 0, sizeof(ui->ui_x1));
    247      1.1   thorpej 		ui->ui_len = uh->uh_ulen;
    248      1.5  christos 		if (ip_cksum(ui, n) != 0) {
    249      1.1   thorpej #ifdef NET_DEBUG
    250      1.1   thorpej 			if (debug)
    251      1.1   thorpej 				printf("readudp: bad cksum\n");
    252      1.1   thorpej #endif
    253      1.1   thorpej 			*ip = tip;
    254      1.1   thorpej 			return -1;
    255      1.1   thorpej 		}
    256      1.1   thorpej 		*ip = tip;
    257      1.1   thorpej 	}
    258      1.1   thorpej #endif
    259      1.1   thorpej 	if (ntohs(uh->uh_ulen) < sizeof(*uh)) {
    260      1.1   thorpej #ifdef NET_DEBUG
    261      1.1   thorpej 		if (debug)
    262      1.1   thorpej 			printf("readudp: bad udp len %d < %d\n",
    263      1.1   thorpej 				ntohs(uh->uh_ulen), (int)sizeof(*uh));
    264      1.1   thorpej #endif
    265      1.1   thorpej 		return -1;
    266      1.1   thorpej 	}
    267      1.1   thorpej 
    268      1.1   thorpej 	n -= sizeof(*ip) + sizeof(*uh);
    269      1.6     isaki 	return n;
    270      1.1   thorpej }
    271