1 /* Taken from $NetBSD: net.c,v 1.20 1997/12/26 22:41:30 scottr Exp $ */ 2 3 /* 4 * Copyright (c) 1992 Regents of the University of California. 5 * All rights reserved. 6 * 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. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 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 * 3. 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. 22 * 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 33 * SUCH DAMAGE. 34 * 35 * @(#) Header: net.c,v 1.9 93/08/06 19:32:15 leres Exp (LBL) 36 */ 37 38 #include <sys/cdefs.h> 39 __FBSDID("$FreeBSD$"); 40 41 #include <sys/param.h> 42 #include <sys/socket.h> 43 44 #include <string.h> 45 46 #include <net/if.h> 47 #include <netinet/in.h> 48 #include <netinet/if_ether.h> 49 #include <netinet/in_systm.h> 50 51 #include <netinet/ip.h> 52 #include <netinet/ip_var.h> 53 #include <netinet/udp.h> 54 #include <netinet/udp_var.h> 55 56 #include "stand.h" 57 #include "net.h" 58 59 /* Caller must leave room for ethernet, ip and udp headers in front!! */ 60 ssize_t 61 sendudp(struct iodesc *d, void *pkt, size_t len) 62 { 63 ssize_t cc; 64 struct udpiphdr *ui; 65 struct udphdr *uh; 66 67 #ifdef NET_DEBUG 68 if (debug) { 69 printf("sendudp: d=%lx called.\n", (long)d); 70 if (d) { 71 printf("saddr: %s:%d", 72 inet_ntoa(d->myip), ntohs(d->myport)); 73 printf(" daddr: %s:%d\n", 74 inet_ntoa(d->destip), ntohs(d->destport)); 75 } 76 } 77 #endif 78 79 ui = (struct udpiphdr *)pkt - 1; 80 bzero(ui, sizeof(*ui)); 81 82 uh = (struct udphdr *)pkt - 1; 83 len += sizeof(*uh); 84 85 uh->uh_sport = d->myport; 86 uh->uh_dport = d->destport; 87 uh->uh_ulen = htons(len); 88 89 ui->ui_pr = IPPROTO_UDP; 90 ui->ui_len = uh->uh_ulen; 91 ui->ui_src = d->myip; 92 ui->ui_dst = d->destip; 93 94 #ifndef UDP_NO_CKSUM 95 uh->uh_sum = in_cksum(ui, len + sizeof (struct ip)); 96 #endif 97 98 cc = sendip(d, uh, len, IPPROTO_UDP); 99 if (cc == -1) 100 return (-1); 101 if (cc != len) 102 panic("sendudp: bad write (%zd != %zd)", cc, len); 103 return (cc - sizeof(*uh)); 104 } 105 106 /* 107 * Receive a UDP packet and validate it is for us. 108 */ 109 ssize_t 110 readudp(struct iodesc *d, void **pkt, void **payload, time_t tleft) 111 { 112 ssize_t n; 113 struct udphdr *uh; 114 void *ptr; 115 116 #ifdef NET_DEBUG 117 if (debug) 118 printf("readudp: called\n"); 119 #endif 120 121 uh = NULL; 122 ptr = NULL; 123 n = readip(d, &ptr, (void **)&uh, tleft, IPPROTO_UDP); 124 if (n == -1 || n < sizeof(*uh) || n != ntohs(uh->uh_ulen)) { 125 free(ptr); 126 return (-1); 127 } 128 129 if (uh->uh_dport != d->myport) { 130 #ifdef NET_DEBUG 131 if (debug) 132 printf("readudp: bad dport %d != %d\n", 133 d->myport, ntohs(uh->uh_dport)); 134 #endif 135 free(ptr); 136 return (-1); 137 } 138 139 #ifndef UDP_NO_CKSUM 140 if (uh->uh_sum) { 141 struct udpiphdr *ui; 142 struct ip *ip; 143 struct ip tip; 144 145 n = ntohs(uh->uh_ulen) + sizeof(*ip); 146 147 /* Check checksum (must save and restore ip header) */ 148 ip = (struct ip *)uh - 1; 149 tip = *ip; 150 ui = (struct udpiphdr *)ip; 151 bzero(&ui->ui_x1, sizeof(ui->ui_x1)); 152 ui->ui_len = uh->uh_ulen; 153 if (in_cksum(ui, n) != 0) { 154 #ifdef NET_DEBUG 155 if (debug) 156 printf("readudp: bad cksum\n"); 157 #endif 158 free(ptr); 159 return (-1); 160 } 161 *ip = tip; 162 } 163 #endif 164 if (ntohs(uh->uh_ulen) < sizeof(*uh)) { 165 #ifdef NET_DEBUG 166 if (debug) 167 printf("readudp: bad udp len %d < %d\n", 168 ntohs(uh->uh_ulen), (int)sizeof(*uh)); 169 #endif 170 free(ptr); 171 return (-1); 172 } 173 174 n = (n > (ntohs(uh->uh_ulen) - sizeof(*uh))) ? 175 ntohs(uh->uh_ulen) - sizeof(*uh) : n; 176 *pkt = ptr; 177 *payload = (void *)((uintptr_t)uh + sizeof(*uh)); 178 return (n); 179 } 180