1 /* Taken from $NetBSD: net.c,v 1.20 1997/12/26 22:41:30 scottr Exp $ */
4 * Copyright (c) 1992 Regents of the University of California.
7 * This software was developed by the Computer Systems Engineering group
8 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
9 * contributed to Berkeley.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * @(#) Header: net.c,v 1.9 93/08/06 19:32:15 leres Exp (LBL)
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
41 #include <sys/param.h>
42 #include <sys/socket.h>
47 #include <netinet/in.h>
48 #include <netinet/if_ether.h>
49 #include <netinet/in_systm.h>
51 #include <netinet/ip.h>
52 #include <netinet/ip_var.h>
53 #include <netinet/udp.h>
54 #include <netinet/udp_var.h>
59 /* Caller must leave room for ethernet, ip and udp headers in front!! */
73 printf("sendudp: d=%lx called.\n", (long)d);
75 printf("saddr: %s:%d",
76 inet_ntoa(d->myip), ntohs(d->myport));
77 printf(" daddr: %s:%d\n",
78 inet_ntoa(d->destip), ntohs(d->destport));
83 uh = (struct udphdr *)pkt - 1;
84 ip = (struct ip *)uh - 1;
85 len += sizeof(*ip) + sizeof(*uh);
87 bzero(ip, sizeof(*ip) + sizeof(*uh));
89 ip->ip_v = IPVERSION; /* half-char */
90 ip->ip_hl = sizeof(*ip) >> 2; /* half-char */
91 ip->ip_len = htons(len);
92 ip->ip_p = IPPROTO_UDP; /* char */
93 ip->ip_ttl = IP_TTL; /* char */
95 ip->ip_dst = d->destip;
96 ip->ip_sum = in_cksum(ip, sizeof(*ip)); /* short, but special */
98 uh->uh_sport = d->myport;
99 uh->uh_dport = d->destport;
100 uh->uh_ulen = htons(len - sizeof(*ip));
107 /* Calculate checksum (must save and restore ip header) */
109 ui = (struct udpiphdr *)ip;
110 bzero(&ui->ui_x1, sizeof(ui->ui_x1));
111 ui->ui_len = uh->uh_ulen;
112 uh->uh_sum = in_cksum(ui, len);
117 if (ip->ip_dst.s_addr == INADDR_BROADCAST || ip->ip_src.s_addr == 0 ||
118 netmask == 0 || SAMENET(ip->ip_src, ip->ip_dst, netmask))
119 ea = arpwhohas(d, ip->ip_dst);
121 ea = arpwhohas(d, gateip);
123 cc = sendether(d, ip, len, ea, ETHERTYPE_IP);
127 panic("sendudp: bad write (%zd != %zd)", cc, len);
128 return (cc - (sizeof(*ip) + sizeof(*uh)));
132 * Receive a UDP packet and validate it is for us.
133 * Caller leaves room for the headers (Ether, IP, UDP)
136 readudp(d, pkt, len, tleft)
146 u_int16_t etype; /* host order */
150 printf("readudp: called\n");
153 uh = (struct udphdr *)pkt - 1;
154 ip = (struct ip *)uh - 1;
156 n = readether(d, ip, len + sizeof(*ip) + sizeof(*uh), tleft, &etype);
157 if (n == -1 || n < sizeof(*ip) + sizeof(*uh))
160 /* Ethernet address checks now in readether() */
162 /* Need to respond to ARP requests. */
163 if (etype == ETHERTYPE_ARP) {
164 struct arphdr *ah = (void *)ip;
165 if (ah->ar_op == htons(ARPOP_REQUEST)) {
172 if (etype != ETHERTYPE_IP) {
175 printf("readudp: not IP. ether_type=%x\n", etype);
180 /* Check ip header */
181 if (ip->ip_v != IPVERSION ||
182 ip->ip_p != IPPROTO_UDP) { /* half char */
185 printf("readudp: IP version or not UDP. ip_v=%d ip_p=%d\n", ip->ip_v, ip->ip_p);
190 hlen = ip->ip_hl << 2;
191 if (hlen < sizeof(*ip) ||
192 in_cksum(ip, hlen) != 0) {
195 printf("readudp: short hdr or bad cksum.\n");
199 if (n < ntohs(ip->ip_len)) {
202 printf("readudp: bad length %d < %d.\n",
203 (int)n, ntohs(ip->ip_len));
207 if (d->myip.s_addr && ip->ip_dst.s_addr != d->myip.s_addr) {
210 printf("readudp: bad saddr %s != ", inet_ntoa(d->myip));
211 printf("%s\n", inet_ntoa(ip->ip_dst));
217 /* If there were ip options, make them go away */
218 if (hlen != sizeof(*ip)) {
219 bcopy(((u_char *)ip) + hlen, uh, len - hlen);
220 ip->ip_len = htons(sizeof(*ip));
221 n -= hlen - sizeof(*ip);
223 if (uh->uh_dport != d->myport) {
226 printf("readudp: bad dport %d != %d\n",
227 d->myport, ntohs(uh->uh_dport));
237 n = ntohs(uh->uh_ulen) + sizeof(*ip);
238 if (n > RECV_SIZE - ETHER_SIZE) {
239 printf("readudp: huge packet, udp len %d\n", (int)n);
243 /* Check checksum (must save and restore ip header) */
245 ui = (struct udpiphdr *)ip;
246 bzero(&ui->ui_x1, sizeof(ui->ui_x1));
247 ui->ui_len = uh->uh_ulen;
248 if (in_cksum(ui, n) != 0) {
251 printf("readudp: bad cksum\n");
259 if (ntohs(uh->uh_ulen) < sizeof(*uh)) {
262 printf("readudp: bad udp len %d < %d\n",
263 ntohs(uh->uh_ulen), (int)sizeof(*uh));
268 n = (n > (ntohs(uh->uh_ulen) - sizeof(*uh))) ?
269 ntohs(uh->uh_ulen) - sizeof(*uh) : n;