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