1999-11-14 00:17:56 +03:00
|
|
|
/* $NetBSD: net.c,v 1.25 1999/11/13 21:17:57 thorpej Exp $ */
|
1994-10-26 08:44:32 +03:00
|
|
|
|
1994-05-08 20:11:14 +04:00
|
|
|
/*
|
|
|
|
* Copyright (c) 1992 Regents of the University of California.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This software was developed by the Computer Systems Engineering group
|
|
|
|
* at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
|
|
|
|
* contributed to Berkeley.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by the University of
|
|
|
|
* California, Lawrence Berkeley Laboratory and its contributors.
|
|
|
|
* 4. Neither the name of the University nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
1994-10-26 08:44:32 +03:00
|
|
|
* @(#) Header: net.c,v 1.9 93/08/06 19:32:15 leres Exp (LBL)
|
1994-05-08 20:11:14 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
|
1999-11-14 00:17:56 +03:00
|
|
|
#ifdef _STANDALONE
|
|
|
|
#include <lib/libkern/libkern.h>
|
|
|
|
#else
|
|
|
|
#include <string.h>
|
|
|
|
#endif
|
|
|
|
|
1994-05-08 20:11:14 +04:00
|
|
|
#include <net/if.h>
|
1997-07-07 19:52:49 +04:00
|
|
|
#include <net/if_ether.h>
|
1994-05-08 20:11:14 +04:00
|
|
|
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <netinet/in_systm.h>
|
|
|
|
#include <netinet/ip.h>
|
|
|
|
#include <netinet/ip_var.h>
|
|
|
|
#include <netinet/udp.h>
|
|
|
|
#include <netinet/udp_var.h>
|
|
|
|
|
|
|
|
#include "stand.h"
|
|
|
|
#include "net.h"
|
|
|
|
|
1999-02-11 12:10:43 +03:00
|
|
|
|
1994-05-08 20:11:14 +04:00
|
|
|
/* Caller must leave room for ethernet, ip and udp headers in front!! */
|
1995-09-15 03:45:21 +04:00
|
|
|
ssize_t
|
1995-02-20 14:04:00 +03:00
|
|
|
sendudp(d, pkt, len)
|
1994-05-08 20:11:14 +04:00
|
|
|
register struct iodesc *d;
|
1995-02-20 14:04:00 +03:00
|
|
|
register void *pkt;
|
|
|
|
register size_t len;
|
1994-05-08 20:11:14 +04:00
|
|
|
{
|
1995-09-15 03:45:21 +04:00
|
|
|
register ssize_t cc;
|
1994-05-08 20:11:14 +04:00
|
|
|
register struct ip *ip;
|
|
|
|
register struct udphdr *uh;
|
|
|
|
register u_char *ea;
|
|
|
|
|
|
|
|
#ifdef NET_DEBUG
|
|
|
|
if (debug) {
|
1997-09-06 17:57:14 +04:00
|
|
|
printf("sendudp: d=%lx called.\n", (long)d);
|
1994-05-08 20:11:14 +04:00
|
|
|
if (d) {
|
1996-10-13 06:28:58 +04:00
|
|
|
printf("saddr: %s:%d",
|
1996-10-11 02:46:11 +04:00
|
|
|
inet_ntoa(d->myip), ntohs(d->myport));
|
1996-10-13 06:28:58 +04:00
|
|
|
printf(" daddr: %s:%d\n",
|
1996-10-11 02:46:11 +04:00
|
|
|
inet_ntoa(d->destip), ntohs(d->destport));
|
1994-05-08 20:11:14 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
1995-02-20 14:04:00 +03:00
|
|
|
|
|
|
|
uh = (struct udphdr *)pkt - 1;
|
|
|
|
ip = (struct ip *)uh - 1;
|
1994-05-08 20:11:14 +04:00
|
|
|
len += sizeof(*ip) + sizeof(*uh);
|
|
|
|
|
|
|
|
bzero(ip, sizeof(*ip) + sizeof(*uh));
|
|
|
|
|
|
|
|
ip->ip_v = IPVERSION; /* half-char */
|
|
|
|
ip->ip_hl = sizeof(*ip) >> 2; /* half-char */
|
|
|
|
ip->ip_len = htons(len);
|
|
|
|
ip->ip_p = IPPROTO_UDP; /* char */
|
|
|
|
ip->ip_ttl = IP_TTL; /* char */
|
1995-09-19 01:19:18 +04:00
|
|
|
ip->ip_src = d->myip;
|
|
|
|
ip->ip_dst = d->destip;
|
1994-05-08 20:11:14 +04:00
|
|
|
ip->ip_sum = in_cksum(ip, sizeof(*ip)); /* short, but special */
|
|
|
|
|
1995-09-19 01:19:18 +04:00
|
|
|
uh->uh_sport = d->myport;
|
|
|
|
uh->uh_dport = d->destport;
|
1994-05-08 20:11:14 +04:00
|
|
|
uh->uh_ulen = htons(len - sizeof(*ip));
|
|
|
|
|
1997-09-17 20:30:51 +04:00
|
|
|
#ifndef UDP_NO_CKSUM
|
|
|
|
{
|
|
|
|
register struct udpiphdr *ui;
|
|
|
|
struct ip tip;
|
|
|
|
|
|
|
|
/* Calculate checksum (must save and restore ip header) */
|
|
|
|
tip = *ip;
|
|
|
|
ui = (struct udpiphdr *)ip;
|
|
|
|
bzero(ui->ui_x1, sizeof(ui->ui_x1));
|
|
|
|
ui->ui_len = uh->uh_ulen;
|
|
|
|
uh->uh_sum = in_cksum(ui, len);
|
|
|
|
*ip = tip;
|
|
|
|
}
|
|
|
|
#endif
|
1994-05-08 20:11:14 +04:00
|
|
|
|
|
|
|
if (ip->ip_dst.s_addr == INADDR_BROADCAST || ip->ip_src.s_addr == 0 ||
|
1995-09-19 01:19:18 +04:00
|
|
|
netmask == 0 || SAMENET(ip->ip_src, ip->ip_dst, netmask))
|
|
|
|
ea = arpwhohas(d, ip->ip_dst);
|
1994-05-08 20:11:14 +04:00
|
|
|
else
|
1995-09-19 01:19:18 +04:00
|
|
|
ea = arpwhohas(d, gateip);
|
1994-05-08 20:11:14 +04:00
|
|
|
|
|
|
|
cc = sendether(d, ip, len, ea, ETHERTYPE_IP);
|
1995-02-20 14:04:00 +03:00
|
|
|
if (cc == -1)
|
|
|
|
return (-1);
|
1994-05-08 20:11:14 +04:00
|
|
|
if (cc != len)
|
|
|
|
panic("sendudp: bad write (%d != %d)", cc, len);
|
|
|
|
return (cc - (sizeof(*ip) + sizeof(*uh)));
|
|
|
|
}
|
|
|
|
|
1995-09-12 01:11:36 +04:00
|
|
|
/*
|
|
|
|
* Receive a UDP packet and validate it is for us.
|
|
|
|
* Caller leaves room for the headers (Ether, IP, UDP)
|
|
|
|
*/
|
1995-09-15 03:45:21 +04:00
|
|
|
ssize_t
|
1995-02-20 14:04:00 +03:00
|
|
|
readudp(d, pkt, len, tleft)
|
1994-05-08 20:11:14 +04:00
|
|
|
register struct iodesc *d;
|
|
|
|
register void *pkt;
|
1995-02-20 14:04:00 +03:00
|
|
|
register size_t len;
|
|
|
|
time_t tleft;
|
1994-05-08 20:11:14 +04:00
|
|
|
{
|
1995-09-15 03:45:21 +04:00
|
|
|
register ssize_t n;
|
1995-02-20 14:04:00 +03:00
|
|
|
register size_t hlen;
|
1994-05-08 20:11:14 +04:00
|
|
|
register struct ip *ip;
|
|
|
|
register struct udphdr *uh;
|
1995-09-12 01:11:36 +04:00
|
|
|
u_int16_t etype; /* host order */
|
1994-05-08 20:11:14 +04:00
|
|
|
|
|
|
|
#ifdef NET_DEBUG
|
|
|
|
if (debug)
|
1996-10-13 06:28:58 +04:00
|
|
|
printf("readudp: called\n");
|
1994-05-08 20:11:14 +04:00
|
|
|
#endif
|
|
|
|
|
1995-02-20 14:04:00 +03:00
|
|
|
uh = (struct udphdr *)pkt - 1;
|
|
|
|
ip = (struct ip *)uh - 1;
|
|
|
|
|
1995-09-15 03:45:21 +04:00
|
|
|
n = readether(d, ip, len + sizeof(*ip) + sizeof(*uh), tleft, &etype);
|
|
|
|
if (n == -1 || n < sizeof(*ip) + sizeof(*uh))
|
1995-09-17 04:49:37 +04:00
|
|
|
return -1;
|
1995-02-20 14:04:00 +03:00
|
|
|
|
1995-09-12 01:11:36 +04:00
|
|
|
/* Ethernet address checks now in readether() */
|
|
|
|
|
|
|
|
/* Need to respond to ARP requests. */
|
|
|
|
if (etype == ETHERTYPE_ARP) {
|
|
|
|
struct arphdr *ah = (void *)ip;
|
1995-09-17 04:49:37 +04:00
|
|
|
if (ah->ar_op == htons(ARPOP_REQUEST)) {
|
1995-09-12 01:11:36 +04:00
|
|
|
/* Send ARP reply */
|
|
|
|
arp_reply(d, ah);
|
|
|
|
}
|
1995-09-17 04:49:37 +04:00
|
|
|
return -1;
|
1995-02-20 14:04:00 +03:00
|
|
|
}
|
1995-09-12 01:11:36 +04:00
|
|
|
|
|
|
|
if (etype != ETHERTYPE_IP) {
|
1994-05-08 20:11:14 +04:00
|
|
|
#ifdef NET_DEBUG
|
|
|
|
if (debug)
|
1996-10-13 06:28:58 +04:00
|
|
|
printf("readudp: not IP. ether_type=%x\n", etype);
|
1994-05-08 20:11:14 +04:00
|
|
|
#endif
|
1995-09-17 04:49:37 +04:00
|
|
|
return -1;
|
1994-05-08 20:11:14 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Check ip header */
|
1995-02-20 14:04:00 +03:00
|
|
|
if (ip->ip_v != IPVERSION ||
|
|
|
|
ip->ip_p != IPPROTO_UDP) { /* half char */
|
1994-05-08 20:11:14 +04:00
|
|
|
#ifdef NET_DEBUG
|
|
|
|
if (debug)
|
1996-10-13 06:28:58 +04:00
|
|
|
printf("readudp: IP version or not UDP. ip_v=%d ip_p=%d\n", ip->ip_v, ip->ip_p);
|
1994-05-08 20:11:14 +04:00
|
|
|
#endif
|
1995-09-17 04:49:37 +04:00
|
|
|
return -1;
|
1994-05-08 20:11:14 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
hlen = ip->ip_hl << 2;
|
1995-02-20 14:04:00 +03:00
|
|
|
if (hlen < sizeof(*ip) ||
|
|
|
|
in_cksum(ip, hlen) != 0) {
|
1994-05-08 20:11:14 +04:00
|
|
|
#ifdef NET_DEBUG
|
|
|
|
if (debug)
|
1996-10-13 06:28:58 +04:00
|
|
|
printf("readudp: short hdr or bad cksum.\n");
|
1994-05-08 20:11:14 +04:00
|
|
|
#endif
|
1995-09-17 04:49:37 +04:00
|
|
|
return -1;
|
1994-05-08 20:11:14 +04:00
|
|
|
}
|
1997-09-17 20:30:51 +04:00
|
|
|
if (n < ntohs(ip->ip_len)) {
|
1994-05-08 20:11:14 +04:00
|
|
|
#ifdef NET_DEBUG
|
|
|
|
if (debug)
|
1997-09-17 20:30:51 +04:00
|
|
|
printf("readudp: bad length %d < %d.\n",
|
|
|
|
(int)n, ntohs(ip->ip_len));
|
1994-05-08 20:11:14 +04:00
|
|
|
#endif
|
1995-09-17 04:49:37 +04:00
|
|
|
return -1;
|
1994-05-08 20:11:14 +04:00
|
|
|
}
|
1995-09-19 01:19:18 +04:00
|
|
|
if (d->myip.s_addr && ip->ip_dst.s_addr != d->myip.s_addr) {
|
1994-05-08 20:11:14 +04:00
|
|
|
#ifdef NET_DEBUG
|
|
|
|
if (debug) {
|
1996-10-13 06:28:58 +04:00
|
|
|
printf("readudp: bad saddr %s != ", inet_ntoa(d->myip));
|
|
|
|
printf("%s\n", inet_ntoa(ip->ip_dst));
|
1994-05-08 20:11:14 +04:00
|
|
|
}
|
|
|
|
#endif
|
1995-09-17 04:49:37 +04:00
|
|
|
return -1;
|
1994-05-08 20:11:14 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* If there were ip options, make them go away */
|
|
|
|
if (hlen != sizeof(*ip)) {
|
1995-02-20 14:04:00 +03:00
|
|
|
bcopy(((u_char *)ip) + hlen, uh, len - hlen);
|
1997-09-17 20:30:51 +04:00
|
|
|
ip->ip_len = htons(sizeof(*ip));
|
1995-09-15 03:45:21 +04:00
|
|
|
n -= hlen - sizeof(*ip);
|
1994-05-08 20:11:14 +04:00
|
|
|
}
|
1995-09-19 01:19:18 +04:00
|
|
|
if (uh->uh_dport != d->myport) {
|
1994-05-08 20:11:14 +04:00
|
|
|
#ifdef NET_DEBUG
|
|
|
|
if (debug)
|
1996-10-13 06:28:58 +04:00
|
|
|
printf("readudp: bad dport %d != %d\n",
|
1994-05-08 20:11:14 +04:00
|
|
|
d->myport, ntohs(uh->uh_dport));
|
|
|
|
#endif
|
1995-09-17 04:49:37 +04:00
|
|
|
return -1;
|
1994-05-08 20:11:14 +04:00
|
|
|
}
|
|
|
|
|
1997-09-17 20:30:51 +04:00
|
|
|
#ifndef UDP_NO_CKSUM
|
1994-05-08 20:11:14 +04:00
|
|
|
if (uh->uh_sum) {
|
1997-09-17 20:30:51 +04:00
|
|
|
register struct udpiphdr *ui;
|
|
|
|
struct ip tip;
|
|
|
|
|
1995-09-15 03:45:21 +04:00
|
|
|
n = ntohs(uh->uh_ulen) + sizeof(*ip);
|
|
|
|
if (n > RECV_SIZE - ETHER_SIZE) {
|
1997-09-06 17:57:14 +04:00
|
|
|
printf("readudp: huge packet, udp len %d\n", (int)n);
|
1995-09-17 04:49:37 +04:00
|
|
|
return -1;
|
1994-05-08 20:11:14 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Check checksum (must save and restore ip header) */
|
|
|
|
tip = *ip;
|
|
|
|
ui = (struct udpiphdr *)ip;
|
1995-11-29 09:12:08 +03:00
|
|
|
bzero(ui->ui_x1, sizeof(ui->ui_x1));
|
1994-05-08 20:11:14 +04:00
|
|
|
ui->ui_len = uh->uh_ulen;
|
1995-09-15 03:45:21 +04:00
|
|
|
if (in_cksum(ui, n) != 0) {
|
1994-05-08 20:11:14 +04:00
|
|
|
#ifdef NET_DEBUG
|
|
|
|
if (debug)
|
1996-10-13 06:28:58 +04:00
|
|
|
printf("readudp: bad cksum\n");
|
1994-05-08 20:11:14 +04:00
|
|
|
#endif
|
|
|
|
*ip = tip;
|
1995-09-17 04:49:37 +04:00
|
|
|
return -1;
|
1994-05-08 20:11:14 +04:00
|
|
|
}
|
|
|
|
*ip = tip;
|
|
|
|
}
|
1997-09-17 20:30:51 +04:00
|
|
|
#endif
|
|
|
|
if (ntohs(uh->uh_ulen) < sizeof(*uh)) {
|
1994-05-08 20:11:14 +04:00
|
|
|
#ifdef NET_DEBUG
|
|
|
|
if (debug)
|
1996-10-13 06:28:58 +04:00
|
|
|
printf("readudp: bad udp len %d < %d\n",
|
1997-09-17 20:30:51 +04:00
|
|
|
ntohs(uh->uh_ulen), (int)sizeof(*uh));
|
1994-05-08 20:11:14 +04:00
|
|
|
#endif
|
1995-09-17 04:49:37 +04:00
|
|
|
return -1;
|
1994-05-08 20:11:14 +04:00
|
|
|
}
|
1995-02-20 14:04:00 +03:00
|
|
|
|
1995-09-15 03:45:21 +04:00
|
|
|
n -= sizeof(*ip) + sizeof(*uh);
|
|
|
|
return (n);
|
1994-05-08 20:11:14 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Send a packet and wait for a reply, with exponential backoff.
|
|
|
|
*
|
1997-12-27 01:41:30 +03:00
|
|
|
* The send routine must return the actual number of bytes written,
|
|
|
|
* or -1 on error.
|
1994-05-08 20:11:14 +04:00
|
|
|
*
|
|
|
|
* The receive routine can indicate success by returning the number of
|
|
|
|
* bytes read; it can return 0 to indicate EOF; it can return -1 with a
|
|
|
|
* non-zero errno to indicate failure; finally, it can return -1 with a
|
|
|
|
* zero errno to indicate it isn't done yet.
|
|
|
|
*/
|
1995-09-15 03:45:21 +04:00
|
|
|
ssize_t
|
1994-05-08 20:11:14 +04:00
|
|
|
sendrecv(d, sproc, sbuf, ssize, rproc, rbuf, rsize)
|
|
|
|
register struct iodesc *d;
|
1995-09-15 03:45:21 +04:00
|
|
|
register ssize_t (*sproc)(struct iodesc *, void *, size_t);
|
1994-05-08 20:11:14 +04:00
|
|
|
register void *sbuf;
|
1995-02-20 14:04:00 +03:00
|
|
|
register size_t ssize;
|
1995-09-15 03:45:21 +04:00
|
|
|
register ssize_t (*rproc)(struct iodesc *, void *, size_t, time_t);
|
1994-05-08 20:11:14 +04:00
|
|
|
register void *rbuf;
|
1995-02-20 14:04:00 +03:00
|
|
|
register size_t rsize;
|
1994-05-08 20:11:14 +04:00
|
|
|
{
|
1995-09-15 03:45:21 +04:00
|
|
|
register ssize_t cc;
|
1995-12-14 02:38:10 +03:00
|
|
|
register time_t t, tmo, tlast;
|
|
|
|
long tleft;
|
1994-05-08 20:11:14 +04:00
|
|
|
|
|
|
|
#ifdef NET_DEBUG
|
|
|
|
if (debug)
|
1996-10-13 06:28:58 +04:00
|
|
|
printf("sendrecv: called\n");
|
1994-05-08 20:11:14 +04:00
|
|
|
#endif
|
1995-02-20 14:04:00 +03:00
|
|
|
|
1994-05-08 20:11:14 +04:00
|
|
|
tmo = MINTMO;
|
|
|
|
tlast = tleft = 0;
|
|
|
|
t = getsecs();
|
|
|
|
for (;;) {
|
|
|
|
if (tleft <= 0) {
|
1995-12-14 02:38:10 +03:00
|
|
|
if (tmo >= MAXTMO) {
|
1995-09-19 01:19:18 +04:00
|
|
|
errno = ETIMEDOUT;
|
|
|
|
return -1;
|
|
|
|
}
|
1994-05-08 20:11:14 +04:00
|
|
|
cc = (*sproc)(d, sbuf, ssize);
|
1997-12-27 01:41:30 +03:00
|
|
|
if (cc != -1 && cc < ssize)
|
1994-05-08 20:11:14 +04:00
|
|
|
panic("sendrecv: short write! (%d < %d)",
|
|
|
|
cc, ssize);
|
|
|
|
|
|
|
|
tleft = tmo;
|
|
|
|
tmo <<= 1;
|
|
|
|
if (tmo > MAXTMO)
|
|
|
|
tmo = MAXTMO;
|
1997-12-27 01:41:30 +03:00
|
|
|
|
|
|
|
if (cc == -1) {
|
|
|
|
/* Error on transmit; wait before retrying */
|
|
|
|
while ((getsecs() - t) < tmo);
|
|
|
|
tleft = 0;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
1994-05-08 20:11:14 +04:00
|
|
|
tlast = t;
|
|
|
|
}
|
|
|
|
|
1995-02-20 14:04:00 +03:00
|
|
|
/* Try to get a packet and process it. */
|
|
|
|
cc = (*rproc)(d, rbuf, rsize, tleft);
|
|
|
|
/* Return on data, EOF or real error. */
|
|
|
|
if (cc != -1 || errno != 0)
|
|
|
|
return (cc);
|
|
|
|
|
1994-05-08 20:11:14 +04:00
|
|
|
/* Timed out or didn't get the packet we're waiting for */
|
|
|
|
t = getsecs();
|
|
|
|
tleft -= t - tlast;
|
|
|
|
tlast = t;
|
|
|
|
}
|
|
|
|
}
|