1*dba7b4a8Spatrick /* $OpenBSD: netudp.c,v 1.4 2018/03/31 17:09:56 patrick Exp $ */
26dd6a60bSmiod /* $NetBSD: net.c,v 1.14 1996/10/13 02:29:02 christos Exp $ */
36dd6a60bSmiod
46dd6a60bSmiod /*
56dd6a60bSmiod * Copyright (c) 1992 Regents of the University of California.
66dd6a60bSmiod * All rights reserved.
76dd6a60bSmiod *
86dd6a60bSmiod * This software was developed by the Computer Systems Engineering group
96dd6a60bSmiod * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
106dd6a60bSmiod * contributed to Berkeley.
116dd6a60bSmiod *
126dd6a60bSmiod * Redistribution and use in source and binary forms, with or without
136dd6a60bSmiod * modification, are permitted provided that the following conditions
146dd6a60bSmiod * are met:
156dd6a60bSmiod * 1. Redistributions of source code must retain the above copyright
166dd6a60bSmiod * notice, this list of conditions and the following disclaimer.
176dd6a60bSmiod * 2. Redistributions in binary form must reproduce the above copyright
186dd6a60bSmiod * notice, this list of conditions and the following disclaimer in the
196dd6a60bSmiod * documentation and/or other materials provided with the distribution.
206dd6a60bSmiod * 3. All advertising materials mentioning features or use of this software
216dd6a60bSmiod * must display the following acknowledgement:
226dd6a60bSmiod * This product includes software developed by the University of
236dd6a60bSmiod * California, Lawrence Berkeley Laboratory and its contributors.
246dd6a60bSmiod * 4. Neither the name of the University nor the names of its contributors
256dd6a60bSmiod * may be used to endorse or promote products derived from this software
266dd6a60bSmiod * without specific prior written permission.
276dd6a60bSmiod *
286dd6a60bSmiod * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
296dd6a60bSmiod * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
306dd6a60bSmiod * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
316dd6a60bSmiod * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
326dd6a60bSmiod * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
336dd6a60bSmiod * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
346dd6a60bSmiod * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
356dd6a60bSmiod * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
366dd6a60bSmiod * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
376dd6a60bSmiod * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
386dd6a60bSmiod * SUCH DAMAGE.
396dd6a60bSmiod *
406dd6a60bSmiod * @(#) Header: net.c,v 1.9 93/08/06 19:32:15 leres Exp (LBL)
416dd6a60bSmiod */
426dd6a60bSmiod
436dd6a60bSmiod #include <sys/param.h>
446dd6a60bSmiod #include <sys/socket.h>
456dd6a60bSmiod
466dd6a60bSmiod #include <net/if.h>
476dd6a60bSmiod #include <netinet/in.h>
486dd6a60bSmiod
496dd6a60bSmiod #include <netinet/if_ether.h>
506dd6a60bSmiod #include <netinet/ip.h>
516dd6a60bSmiod #include <netinet/ip_var.h>
526dd6a60bSmiod #include <netinet/udp.h>
536dd6a60bSmiod #include <netinet/udp_var.h>
546dd6a60bSmiod
556dd6a60bSmiod #include "stand.h"
566dd6a60bSmiod #include "net.h"
576dd6a60bSmiod
586dd6a60bSmiod /* Caller must leave room for ethernet, ip and udp headers in front!! */
596dd6a60bSmiod ssize_t
sendudp(struct iodesc * d,void * pkt,size_t len)606dd6a60bSmiod sendudp(struct iodesc *d, void *pkt, size_t len)
616dd6a60bSmiod {
626dd6a60bSmiod ssize_t cc;
636dd6a60bSmiod struct ip *ip;
646dd6a60bSmiod struct udpiphdr *ui;
656dd6a60bSmiod struct udphdr *uh;
666dd6a60bSmiod u_char *ea;
676dd6a60bSmiod struct ip tip;
686dd6a60bSmiod
696dd6a60bSmiod #ifdef NET_DEBUG
706dd6a60bSmiod if (debug) {
716dd6a60bSmiod printf("sendudp: d=%x called.\n", (u_int)d);
726dd6a60bSmiod if (d) {
736dd6a60bSmiod printf("saddr: %s:%d",
746dd6a60bSmiod inet_ntoa(d->myip), ntohs(d->myport));
756dd6a60bSmiod printf(" daddr: %s:%d\n",
766dd6a60bSmiod inet_ntoa(d->destip), ntohs(d->destport));
776dd6a60bSmiod }
786dd6a60bSmiod }
796dd6a60bSmiod #endif
806dd6a60bSmiod
816dd6a60bSmiod uh = (struct udphdr *)pkt - 1;
826dd6a60bSmiod ip = (struct ip *)uh - 1;
836dd6a60bSmiod len += sizeof(*ip) + sizeof(*uh);
846dd6a60bSmiod
856dd6a60bSmiod bzero(ip, sizeof(*ip) + sizeof(*uh));
866dd6a60bSmiod
876dd6a60bSmiod ip->ip_v = IPVERSION; /* half-char */
886dd6a60bSmiod ip->ip_hl = sizeof(*ip) >> 2; /* half-char */
896dd6a60bSmiod ip->ip_len = htons(len);
906dd6a60bSmiod ip->ip_p = IPPROTO_UDP; /* char */
916dd6a60bSmiod ip->ip_ttl = IP_TTL; /* char */
926dd6a60bSmiod ip->ip_src = d->myip;
936dd6a60bSmiod ip->ip_dst = d->destip;
946dd6a60bSmiod ip->ip_sum = in_cksum(ip, sizeof(*ip)); /* short, but special */
956dd6a60bSmiod
966dd6a60bSmiod uh->uh_sport = d->myport;
976dd6a60bSmiod uh->uh_dport = d->destport;
986dd6a60bSmiod uh->uh_ulen = htons(len - sizeof(*ip));
996dd6a60bSmiod
1006dd6a60bSmiod /* Calculate checksum (must save and restore ip header) */
1016dd6a60bSmiod tip = *ip;
1026dd6a60bSmiod ui = (struct udpiphdr *)ip;
1036dd6a60bSmiod bzero(ui->ui_x1, sizeof(ui->ui_x1));
1046dd6a60bSmiod ui->ui_len = uh->uh_ulen;
1056dd6a60bSmiod uh->uh_sum = in_cksum(ui, len);
1066dd6a60bSmiod *ip = tip;
1076dd6a60bSmiod
1086dd6a60bSmiod if (ip->ip_dst.s_addr == INADDR_BROADCAST || ip->ip_src.s_addr == 0 ||
1096dd6a60bSmiod netmask == 0 || SAMENET(ip->ip_src, ip->ip_dst, netmask))
1106dd6a60bSmiod ea = arpwhohas(d, ip->ip_dst);
1116dd6a60bSmiod else
1126dd6a60bSmiod ea = arpwhohas(d, gateip);
1136dd6a60bSmiod
1146dd6a60bSmiod cc = sendether(d, ip, len, ea, ETHERTYPE_IP);
1156dd6a60bSmiod if (cc < 0)
1166dd6a60bSmiod return (-1);
1176dd6a60bSmiod if ((size_t)cc != len)
1186dd6a60bSmiod panic("sendudp: bad write (%d != %d)", cc, len);
1196dd6a60bSmiod return (cc - (sizeof(*ip) + sizeof(*uh)));
1206dd6a60bSmiod }
1216dd6a60bSmiod
1226dd6a60bSmiod /*
1236dd6a60bSmiod * Receive a UDP packet and validate it is for us.
1246dd6a60bSmiod * Caller leaves room for the headers (Ether, IP, UDP)
1256dd6a60bSmiod */
1266dd6a60bSmiod ssize_t
readudp(struct iodesc * d,void * pkt,size_t len,time_t tleft)1276dd6a60bSmiod readudp(struct iodesc *d, void *pkt, size_t len, time_t tleft)
1286dd6a60bSmiod {
1296dd6a60bSmiod ssize_t n;
1306dd6a60bSmiod size_t hlen;
1316dd6a60bSmiod struct ip *ip;
1326dd6a60bSmiod struct udphdr *uh;
1336dd6a60bSmiod struct udpiphdr *ui;
1346dd6a60bSmiod struct ip tip;
1356dd6a60bSmiod u_int16_t etype; /* host order */
1366dd6a60bSmiod
1376dd6a60bSmiod #ifdef NET_DEBUG
1386dd6a60bSmiod if (debug)
1396dd6a60bSmiod printf("readudp: called\n");
1406dd6a60bSmiod #endif
1416dd6a60bSmiod
1426dd6a60bSmiod uh = (struct udphdr *)pkt - 1;
1436dd6a60bSmiod ip = (struct ip *)uh - 1;
1446dd6a60bSmiod
1456dd6a60bSmiod n = readether(d, ip, len + sizeof(*ip) + sizeof(*uh), tleft, &etype);
1466dd6a60bSmiod if (n < 0 || (size_t)n < sizeof(*ip) + sizeof(*uh))
1476dd6a60bSmiod return -1;
1486dd6a60bSmiod
1496dd6a60bSmiod /* Ethernet address checks now in readether() */
1506dd6a60bSmiod
1516dd6a60bSmiod /* Need to respond to ARP requests. */
1526dd6a60bSmiod if (etype == ETHERTYPE_ARP) {
1536dd6a60bSmiod struct arphdr *ah = (void *)ip;
1546dd6a60bSmiod if (ah->ar_op == htons(ARPOP_REQUEST)) {
1556dd6a60bSmiod /* Send ARP reply */
1566dd6a60bSmiod arp_reply(d, ah);
1576dd6a60bSmiod }
1586dd6a60bSmiod return -1;
1596dd6a60bSmiod }
1606dd6a60bSmiod
1616dd6a60bSmiod if (etype != ETHERTYPE_IP) {
1626dd6a60bSmiod #ifdef NET_DEBUG
1636dd6a60bSmiod if (debug)
1646dd6a60bSmiod printf("readudp: not IP. ether_type=%x\n", etype);
1656dd6a60bSmiod #endif
1666dd6a60bSmiod return -1;
1676dd6a60bSmiod }
1686dd6a60bSmiod
1696dd6a60bSmiod /* Check ip header */
1706dd6a60bSmiod if (ip->ip_v != IPVERSION || ip->ip_p != IPPROTO_UDP) { /* half char */
1716dd6a60bSmiod #ifdef NET_DEBUG
1726dd6a60bSmiod if (debug)
1736dd6a60bSmiod printf("readudp: IP version or not UDP. ip_v=%d ip_p=%d\n", ip->ip_v, ip->ip_p);
1746dd6a60bSmiod #endif
1756dd6a60bSmiod return -1;
1766dd6a60bSmiod }
1776dd6a60bSmiod
1786dd6a60bSmiod hlen = ip->ip_hl << 2;
1796dd6a60bSmiod if (hlen < sizeof(*ip) || in_cksum(ip, hlen) != 0) {
1806dd6a60bSmiod #ifdef NET_DEBUG
1816dd6a60bSmiod if (debug)
1826dd6a60bSmiod printf("readudp: short hdr or bad cksum.\n");
1836dd6a60bSmiod #endif
1846dd6a60bSmiod return -1;
1856dd6a60bSmiod }
186*dba7b4a8Spatrick if (n < ntohs(ip->ip_len)) {
1876dd6a60bSmiod #ifdef NET_DEBUG
1886dd6a60bSmiod if (debug)
189*dba7b4a8Spatrick printf("readudp: bad length %d < %d.\n",
190*dba7b4a8Spatrick n, ntohs(ip->ip_len));
1916dd6a60bSmiod #endif
1926dd6a60bSmiod return -1;
1936dd6a60bSmiod }
1946dd6a60bSmiod if (d->myip.s_addr && ip->ip_dst.s_addr != d->myip.s_addr) {
1956dd6a60bSmiod #ifdef NET_DEBUG
1966dd6a60bSmiod if (debug) {
1976dd6a60bSmiod printf("readudp: bad saddr %s != ", inet_ntoa(d->myip));
1986dd6a60bSmiod printf("%s\n", inet_ntoa(ip->ip_dst));
1996dd6a60bSmiod }
2006dd6a60bSmiod #endif
2016dd6a60bSmiod return -1;
2026dd6a60bSmiod }
2036dd6a60bSmiod
2046dd6a60bSmiod /* If there were ip options, make them go away */
2056dd6a60bSmiod if (hlen != sizeof(*ip)) {
2066dd6a60bSmiod bcopy(((u_char *)ip) + hlen, uh, len - hlen);
207*dba7b4a8Spatrick ip->ip_len = htons(sizeof(*ip));
2086dd6a60bSmiod n -= hlen - sizeof(*ip);
2096dd6a60bSmiod }
2106dd6a60bSmiod if (uh->uh_dport != d->myport) {
2116dd6a60bSmiod #ifdef NET_DEBUG
2126dd6a60bSmiod if (debug)
2136dd6a60bSmiod printf("readudp: bad dport %d != %d\n",
2146dd6a60bSmiod d->myport, ntohs(uh->uh_dport));
2156dd6a60bSmiod #endif
2166dd6a60bSmiod return -1;
2176dd6a60bSmiod }
2186dd6a60bSmiod
2196dd6a60bSmiod if (uh->uh_sum) {
2206dd6a60bSmiod n = ntohs(uh->uh_ulen) + sizeof(*ip);
2216dd6a60bSmiod if (n > RECV_SIZE - ETHER_SIZE) {
2226dd6a60bSmiod printf("readudp: huge packet, udp len %ld\n", (long)n);
2236dd6a60bSmiod return -1;
2246dd6a60bSmiod }
2256dd6a60bSmiod
2266dd6a60bSmiod /* Check checksum (must save and restore ip header) */
2276dd6a60bSmiod tip = *ip;
2286dd6a60bSmiod ui = (struct udpiphdr *)ip;
2296dd6a60bSmiod bzero(ui->ui_x1, sizeof(ui->ui_x1));
2306dd6a60bSmiod ui->ui_len = uh->uh_ulen;
2316dd6a60bSmiod if (in_cksum(ui, n) != 0) {
2326dd6a60bSmiod #ifdef NET_DEBUG
2336dd6a60bSmiod if (debug)
2346dd6a60bSmiod printf("readudp: bad cksum\n");
2356dd6a60bSmiod #endif
2366dd6a60bSmiod *ip = tip;
2376dd6a60bSmiod return -1;
2386dd6a60bSmiod }
2396dd6a60bSmiod *ip = tip;
2406dd6a60bSmiod }
241*dba7b4a8Spatrick if (ntohs(uh->uh_ulen) < sizeof(*uh)) {
2426dd6a60bSmiod #ifdef NET_DEBUG
2436dd6a60bSmiod if (debug)
2446dd6a60bSmiod printf("readudp: bad udp len %d < %d\n",
245*dba7b4a8Spatrick ntohs(uh->uh_ulen), sizeof(*uh));
2466dd6a60bSmiod #endif
2476dd6a60bSmiod return -1;
2486dd6a60bSmiod }
2496dd6a60bSmiod
2506dd6a60bSmiod n -= sizeof(*ip) + sizeof(*uh);
2516dd6a60bSmiod return (n);
2526dd6a60bSmiod }
253