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