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 #include <sys/param.h> 40 #include <sys/socket.h> 41 42 #include <string.h> 43 44 #include <net/if.h> 45 #include <netinet/in.h> 46 #include <netinet/if_ether.h> 47 #include <netinet/in_systm.h> 48 49 #include <netinet/ip.h> 50 #include <netinet/ip_var.h> 51 #include <netinet/udp.h> 52 #include <netinet/udp_var.h> 53 54 #include "stand.h" 55 #include "net.h" 56 57 /* Caller must leave room for ethernet, ip and udp headers in front!! */ 58 ssize_t 59 sendudp(struct iodesc *d, void *pkt, size_t len) 60 { 61 ssize_t cc; 62 struct udpiphdr *ui; 63 struct udphdr *uh; 64 65 #ifdef NET_DEBUG 66 if (debug) { 67 printf("sendudp: d=%lx called.\n", (long)d); 68 if (d) { 69 printf("saddr: %s:%d", 70 inet_ntoa(d->myip), ntohs(d->myport)); 71 printf(" daddr: %s:%d\n", 72 inet_ntoa(d->destip), ntohs(d->destport)); 73 } 74 } 75 #endif 76 77 ui = (struct udpiphdr *)pkt - 1; 78 bzero(ui, sizeof(*ui)); 79 80 uh = (struct udphdr *)pkt - 1; 81 len += sizeof(*uh); 82 83 uh->uh_sport = d->myport; 84 uh->uh_dport = d->destport; 85 uh->uh_ulen = htons(len); 86 87 ui->ui_pr = IPPROTO_UDP; 88 ui->ui_len = uh->uh_ulen; 89 ui->ui_src = d->myip; 90 ui->ui_dst = d->destip; 91 92 #ifndef UDP_NO_CKSUM 93 uh->uh_sum = in_cksum(ui, len + sizeof (struct ip)); 94 #endif 95 96 cc = sendip(d, uh, len, IPPROTO_UDP); 97 if (cc == -1) 98 return (-1); 99 if (cc != len) 100 panic("sendudp: bad write (%zd != %zd)", cc, len); 101 return (cc - sizeof(*uh)); 102 } 103 104 /* 105 * Receive a UDP packet and validate it is for us. 106 */ 107 ssize_t 108 readudp(struct iodesc *d, void **pkt, void **payload, time_t tleft) 109 { 110 ssize_t n; 111 struct udphdr *uh; 112 void *ptr; 113 114 #ifdef NET_DEBUG 115 if (debug) 116 printf("readudp: called\n"); 117 #endif 118 119 uh = NULL; 120 ptr = NULL; 121 n = readip(d, &ptr, (void **)&uh, tleft, IPPROTO_UDP); 122 if (n == -1 || n < sizeof(*uh) || n != ntohs(uh->uh_ulen)) { 123 free(ptr); 124 return (-1); 125 } 126 127 if (uh->uh_dport != d->myport) { 128 #ifdef NET_DEBUG 129 if (debug) 130 printf("readudp: bad dport %d != %d\n", 131 d->myport, ntohs(uh->uh_dport)); 132 #endif 133 free(ptr); 134 return (-1); 135 } 136 137 #ifndef UDP_NO_CKSUM 138 if (uh->uh_sum) { 139 struct udpiphdr *ui; 140 struct ip *ip; 141 struct ip tip; 142 143 n = ntohs(uh->uh_ulen) + sizeof(*ip); 144 145 /* Check checksum (must save and restore ip header) */ 146 ip = (struct ip *)uh - 1; 147 tip = *ip; 148 ui = (struct udpiphdr *)ip; 149 bzero(&ui->ui_x1, sizeof(ui->ui_x1)); 150 ui->ui_len = uh->uh_ulen; 151 if (in_cksum(ui, n) != 0) { 152 #ifdef NET_DEBUG 153 if (debug) 154 printf("readudp: bad cksum\n"); 155 #endif 156 free(ptr); 157 return (-1); 158 } 159 *ip = tip; 160 } 161 #endif 162 if (ntohs(uh->uh_ulen) < sizeof(*uh)) { 163 #ifdef NET_DEBUG 164 if (debug) 165 printf("readudp: bad udp len %d < %d\n", 166 ntohs(uh->uh_ulen), (int)sizeof(*uh)); 167 #endif 168 free(ptr); 169 return (-1); 170 } 171 172 n = (n > (ntohs(uh->uh_ulen) - sizeof(*uh))) ? 173 ntohs(uh->uh_ulen) - sizeof(*uh) : n; 174 *pkt = ptr; 175 *payload = (void *)((uintptr_t)uh + sizeof(*uh)); 176 return (n); 177 } 178