159086Sps/* Taken from $NetBSD: net.c,v 1.20 1997/12/26 22:41:30 scottr Exp $ */ 259086Sps 359086Sps/* 459086Sps * Copyright (c) 1992 Regents of the University of California. 559086Sps * All rights reserved. 659086Sps * 759086Sps * This software was developed by the Computer Systems Engineering group 859086Sps * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and 959086Sps * contributed to Berkeley. 1059086Sps * 1159086Sps * Redistribution and use in source and binary forms, with or without 1259086Sps * modification, are permitted provided that the following conditions 1359086Sps * are met: 1459086Sps * 1. Redistributions of source code must retain the above copyright 1559086Sps * notice, this list of conditions and the following disclaimer. 1659086Sps * 2. Redistributions in binary form must reproduce the above copyright 1759086Sps * notice, this list of conditions and the following disclaimer in the 1859086Sps * documentation and/or other materials provided with the distribution. 1959086Sps * 4. Neither the name of the University nor the names of its contributors 2059086Sps * may be used to endorse or promote products derived from this software 2159086Sps * without specific prior written permission. 2259086Sps * 2359086Sps * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 2459086Sps * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2559086Sps * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2659086Sps * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2759086Sps * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2859086Sps * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2959086Sps * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 3059086Sps * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 3159086Sps * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 3259086Sps * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3359086Sps * SUCH DAMAGE. 3459086Sps * 3559086Sps * @(#) Header: net.c,v 1.9 93/08/06 19:32:15 leres Exp (LBL) 3659086Sps */ 3759086Sps 3884221Sdillon#include <sys/cdefs.h> 3984221Sdillon__FBSDID("$FreeBSD$"); 4084221Sdillon 4159086Sps#include <sys/param.h> 4259086Sps#include <sys/socket.h> 4359086Sps 4459086Sps#include <string.h> 4559086Sps 4659086Sps#include <net/if.h> 4759086Sps#include <netinet/in.h> 4859086Sps#include <netinet/if_ether.h> 4959086Sps#include <netinet/in_systm.h> 5059086Sps 51221364Srodrigc#include <netinet/in_pcb.h> 5259086Sps#include <netinet/ip.h> 5359086Sps#include <netinet/ip_var.h> 5459086Sps#include <netinet/udp.h> 5559086Sps#include <netinet/udp_var.h> 5659086Sps 5759086Sps#include "stand.h" 5859086Sps#include "net.h" 5959086Sps 6059086Sps/* Caller must leave room for ethernet, ip and udp headers in front!! */ 6159086Spsssize_t 6259086Spssendudp(d, pkt, len) 6392913Sobrien struct iodesc *d; 6492913Sobrien void *pkt; 6592913Sobrien size_t len; 6659086Sps{ 6792913Sobrien ssize_t cc; 6892913Sobrien struct ip *ip; 6992913Sobrien struct udphdr *uh; 7092913Sobrien u_char *ea; 7159086Sps 7259086Sps#ifdef NET_DEBUG 7359086Sps if (debug) { 7459086Sps printf("sendudp: d=%lx called.\n", (long)d); 7559086Sps if (d) { 7659086Sps printf("saddr: %s:%d", 7759086Sps inet_ntoa(d->myip), ntohs(d->myport)); 7859086Sps printf(" daddr: %s:%d\n", 7959086Sps inet_ntoa(d->destip), ntohs(d->destport)); 8059086Sps } 8159086Sps } 8259086Sps#endif 8359086Sps 8459086Sps uh = (struct udphdr *)pkt - 1; 8559086Sps ip = (struct ip *)uh - 1; 8659086Sps len += sizeof(*ip) + sizeof(*uh); 8759086Sps 8859086Sps bzero(ip, sizeof(*ip) + sizeof(*uh)); 8959086Sps 9059086Sps ip->ip_v = IPVERSION; /* half-char */ 9159086Sps ip->ip_hl = sizeof(*ip) >> 2; /* half-char */ 9259086Sps ip->ip_len = htons(len); 9359086Sps ip->ip_p = IPPROTO_UDP; /* char */ 94198941Smarcel ip->ip_ttl = IPDEFTTL; /* char */ 9559086Sps ip->ip_src = d->myip; 9659086Sps ip->ip_dst = d->destip; 9759086Sps ip->ip_sum = in_cksum(ip, sizeof(*ip)); /* short, but special */ 9859086Sps 9959086Sps uh->uh_sport = d->myport; 10059086Sps uh->uh_dport = d->destport; 10159086Sps uh->uh_ulen = htons(len - sizeof(*ip)); 10259086Sps 10359086Sps#ifndef UDP_NO_CKSUM 10459086Sps { 10592913Sobrien struct udpiphdr *ui; 10659086Sps struct ip tip; 10759086Sps 10859086Sps /* Calculate checksum (must save and restore ip header) */ 10959086Sps tip = *ip; 11059086Sps ui = (struct udpiphdr *)ip; 11159086Sps bzero(&ui->ui_x1, sizeof(ui->ui_x1)); 11259086Sps ui->ui_len = uh->uh_ulen; 11359086Sps uh->uh_sum = in_cksum(ui, len); 11459086Sps *ip = tip; 11559086Sps } 11659086Sps#endif 11759086Sps 11859086Sps if (ip->ip_dst.s_addr == INADDR_BROADCAST || ip->ip_src.s_addr == 0 || 11959086Sps netmask == 0 || SAMENET(ip->ip_src, ip->ip_dst, netmask)) 12059086Sps ea = arpwhohas(d, ip->ip_dst); 12159086Sps else 12259086Sps ea = arpwhohas(d, gateip); 12359086Sps 12459086Sps cc = sendether(d, ip, len, ea, ETHERTYPE_IP); 12559086Sps if (cc == -1) 12659086Sps return (-1); 12759086Sps if (cc != len) 128146835Sjhb panic("sendudp: bad write (%zd != %zd)", cc, len); 12959086Sps return (cc - (sizeof(*ip) + sizeof(*uh))); 13059086Sps} 13159086Sps 13259086Sps/* 13359086Sps * Receive a UDP packet and validate it is for us. 13459086Sps * Caller leaves room for the headers (Ether, IP, UDP) 13559086Sps */ 13659086Spsssize_t 13759086Spsreadudp(d, pkt, len, tleft) 13892913Sobrien struct iodesc *d; 13992913Sobrien void *pkt; 14092913Sobrien size_t len; 14159086Sps time_t tleft; 14259086Sps{ 14392913Sobrien ssize_t n; 14492913Sobrien size_t hlen; 14592913Sobrien struct ip *ip; 14692913Sobrien struct udphdr *uh; 14759086Sps u_int16_t etype; /* host order */ 14859086Sps 14959086Sps#ifdef NET_DEBUG 15059086Sps if (debug) 15159086Sps printf("readudp: called\n"); 15259086Sps#endif 15359086Sps 15459086Sps uh = (struct udphdr *)pkt - 1; 15559086Sps ip = (struct ip *)uh - 1; 15659086Sps 15759086Sps n = readether(d, ip, len + sizeof(*ip) + sizeof(*uh), tleft, &etype); 15859086Sps if (n == -1 || n < sizeof(*ip) + sizeof(*uh)) 15959086Sps return -1; 16059086Sps 16159086Sps /* Ethernet address checks now in readether() */ 16259086Sps 16359086Sps /* Need to respond to ARP requests. */ 16459086Sps if (etype == ETHERTYPE_ARP) { 16559086Sps struct arphdr *ah = (void *)ip; 16659086Sps if (ah->ar_op == htons(ARPOP_REQUEST)) { 16759086Sps /* Send ARP reply */ 16859086Sps arp_reply(d, ah); 16959086Sps } 17059086Sps return -1; 17159086Sps } 17259086Sps 17359086Sps if (etype != ETHERTYPE_IP) { 17459086Sps#ifdef NET_DEBUG 17559086Sps if (debug) 17659086Sps printf("readudp: not IP. ether_type=%x\n", etype); 17759086Sps#endif 17859086Sps return -1; 17959086Sps } 18059086Sps 18159086Sps /* Check ip header */ 18259086Sps if (ip->ip_v != IPVERSION || 18359086Sps ip->ip_p != IPPROTO_UDP) { /* half char */ 18459086Sps#ifdef NET_DEBUG 18559086Sps if (debug) 18659086Sps printf("readudp: IP version or not UDP. ip_v=%d ip_p=%d\n", ip->ip_v, ip->ip_p); 18759086Sps#endif 18859086Sps return -1; 18959086Sps } 19059086Sps 19159086Sps hlen = ip->ip_hl << 2; 19259086Sps if (hlen < sizeof(*ip) || 19359086Sps in_cksum(ip, hlen) != 0) { 19459086Sps#ifdef NET_DEBUG 19559086Sps if (debug) 19659086Sps printf("readudp: short hdr or bad cksum.\n"); 19759086Sps#endif 19859086Sps return -1; 19959086Sps } 20059086Sps if (n < ntohs(ip->ip_len)) { 20159086Sps#ifdef NET_DEBUG 20259086Sps if (debug) 20359086Sps printf("readudp: bad length %d < %d.\n", 20459086Sps (int)n, ntohs(ip->ip_len)); 20559086Sps#endif 20659086Sps return -1; 20759086Sps } 20859086Sps if (d->myip.s_addr && ip->ip_dst.s_addr != d->myip.s_addr) { 20959086Sps#ifdef NET_DEBUG 21059086Sps if (debug) { 21159086Sps printf("readudp: bad saddr %s != ", inet_ntoa(d->myip)); 21259086Sps printf("%s\n", inet_ntoa(ip->ip_dst)); 21359086Sps } 21459086Sps#endif 21559086Sps return -1; 21659086Sps } 21759086Sps 21859086Sps /* If there were ip options, make them go away */ 21959086Sps if (hlen != sizeof(*ip)) { 22059086Sps bcopy(((u_char *)ip) + hlen, uh, len - hlen); 22159086Sps ip->ip_len = htons(sizeof(*ip)); 22259086Sps n -= hlen - sizeof(*ip); 22359086Sps } 22459086Sps if (uh->uh_dport != d->myport) { 22559086Sps#ifdef NET_DEBUG 22659086Sps if (debug) 22759086Sps printf("readudp: bad dport %d != %d\n", 22859086Sps d->myport, ntohs(uh->uh_dport)); 22959086Sps#endif 23059086Sps return -1; 23159086Sps } 23259086Sps 23359086Sps#ifndef UDP_NO_CKSUM 23459086Sps if (uh->uh_sum) { 23592913Sobrien struct udpiphdr *ui; 23659086Sps struct ip tip; 23759086Sps 23859086Sps n = ntohs(uh->uh_ulen) + sizeof(*ip); 23959086Sps if (n > RECV_SIZE - ETHER_SIZE) { 24059086Sps printf("readudp: huge packet, udp len %d\n", (int)n); 24159086Sps return -1; 24259086Sps } 24359086Sps 24459086Sps /* Check checksum (must save and restore ip header) */ 24559086Sps tip = *ip; 24659086Sps ui = (struct udpiphdr *)ip; 24759086Sps bzero(&ui->ui_x1, sizeof(ui->ui_x1)); 24859086Sps ui->ui_len = uh->uh_ulen; 24959086Sps if (in_cksum(ui, n) != 0) { 25059086Sps#ifdef NET_DEBUG 25159086Sps if (debug) 25259086Sps printf("readudp: bad cksum\n"); 25359086Sps#endif 25459086Sps *ip = tip; 25559086Sps return -1; 25659086Sps } 25759086Sps *ip = tip; 25859086Sps } 25959086Sps#endif 26059086Sps if (ntohs(uh->uh_ulen) < sizeof(*uh)) { 26159086Sps#ifdef NET_DEBUG 26259086Sps if (debug) 26359086Sps printf("readudp: bad udp len %d < %d\n", 26459086Sps ntohs(uh->uh_ulen), (int)sizeof(*uh)); 26559086Sps#endif 26659086Sps return -1; 26759086Sps } 26859086Sps 26977370Smsmith n = (n > (ntohs(uh->uh_ulen) - sizeof(*uh))) ? 27077370Smsmith ntohs(uh->uh_ulen) - sizeof(*uh) : n; 27159086Sps return (n); 27259086Sps} 273