1 /* $OpenBSD: packet.c,v 1.9 2004/05/04 18:58:50 deraadt Exp $ */ 2 3 /* Packet assembly code, originally contributed by Archie Cobbs. */ 4 5 /* 6 * Copyright (c) 1995, 1996, 1999 The Internet Software Consortium. 7 * All rights reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. Neither the name of The Internet Software Consortium nor the names 19 * of its contributors may be used to endorse or promote products derived 20 * from this software without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE INTERNET SOFTWARE CONSORTIUM AND 23 * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, 24 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 25 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 26 * DISCLAIMED. IN NO EVENT SHALL THE INTERNET SOFTWARE CONSORTIUM OR 27 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 29 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 30 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 31 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 32 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 33 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 * 36 * This software has been written for the Internet Software Consortium 37 * by Ted Lemon <mellon@fugue.com> in cooperation with Vixie 38 * Enterprises. To learn more about the Internet Software Consortium, 39 * see ``http://www.vix.com/isc''. To learn more about Vixie 40 * Enterprises, see ``http://www.vix.com''. 41 */ 42 43 #include <sys/cdefs.h> 44 __FBSDID("$FreeBSD$"); 45 46 #include "dhcpd.h" 47 48 #include <netinet/in_systm.h> 49 #include <netinet/ip.h> 50 #include <netinet/udp.h> 51 #include <netinet/if_ether.h> 52 53 #define ETHER_HEADER_SIZE (ETHER_ADDR_LEN * 2 + sizeof(u_int16_t)) 54 55 u_int32_t checksum(unsigned char *, unsigned, u_int32_t); 56 u_int32_t wrapsum(u_int32_t); 57 58 u_int32_t 59 checksum(unsigned char *buf, unsigned nbytes, u_int32_t sum) 60 { 61 int i; 62 63 /* Checksum all the pairs of bytes first... */ 64 for (i = 0; i < (nbytes & ~1U); i += 2) { 65 sum += (u_int16_t)ntohs(*((u_int16_t *)(buf + i))); 66 if (sum > 0xFFFF) 67 sum -= 0xFFFF; 68 } 69 70 /* 71 * If there's a single byte left over, checksum it, too. 72 * Network byte order is big-endian, so the remaining byte is 73 * the high byte. 74 */ 75 if (i < nbytes) { 76 sum += buf[i] << 8; 77 if (sum > 0xFFFF) 78 sum -= 0xFFFF; 79 } 80 81 return (sum); 82 } 83 84 u_int32_t 85 wrapsum(u_int32_t sum) 86 { 87 sum = ~sum & 0xFFFF; 88 return (htons(sum)); 89 } 90 91 void 92 assemble_hw_header(struct interface_info *interface, unsigned char *buf, 93 int *bufix, struct hardware *to) 94 { 95 struct ether_header eh; 96 97 if (to != NULL && to->hlen == 6) /* XXX */ 98 memcpy(eh.ether_dhost, to->haddr, sizeof(eh.ether_dhost)); 99 else 100 memset(eh.ether_dhost, 0xff, sizeof(eh.ether_dhost)); 101 if (interface->hw_address.hlen == sizeof(eh.ether_shost)) 102 memcpy(eh.ether_shost, interface->hw_address.haddr, 103 sizeof(eh.ether_shost)); 104 else 105 memset(eh.ether_shost, 0x00, sizeof(eh.ether_shost)); 106 107 eh.ether_type = htons(ETHERTYPE_IP); 108 109 memcpy(&buf[*bufix], &eh, ETHER_HEADER_SIZE); 110 *bufix += ETHER_HEADER_SIZE; 111 } 112 113 void 114 assemble_udp_ip_header(unsigned char *buf, int *bufix, u_int32_t from, 115 u_int32_t to, unsigned int port, unsigned char *data, int len) 116 { 117 struct ip ip; 118 struct udphdr udp; 119 120 ip.ip_v = 4; 121 ip.ip_hl = 5; 122 ip.ip_tos = IPTOS_LOWDELAY; 123 ip.ip_len = htons(sizeof(ip) + sizeof(udp) + len); 124 ip.ip_id = 0; 125 ip.ip_off = 0; 126 ip.ip_ttl = 128; 127 ip.ip_p = IPPROTO_UDP; 128 ip.ip_sum = 0; 129 ip.ip_src.s_addr = from; 130 ip.ip_dst.s_addr = to; 131 132 ip.ip_sum = wrapsum(checksum((unsigned char *)&ip, sizeof(ip), 0)); 133 134 /* 135 * While the BPF -- used for broadcasts -- expects a "true" IP header 136 * with all the bytes in network byte order, the raw socket interface 137 * which is used for unicasts expects the ip_len field to be in host 138 * byte order. In both cases, the checksum has to be correct, so this 139 * is as good a place as any to turn the bytes around again. 140 */ 141 if (to != INADDR_BROADCAST) 142 ip.ip_len = ntohs(ip.ip_len); 143 144 memcpy(&buf[*bufix], &ip, sizeof(ip)); 145 *bufix += sizeof(ip); 146 147 udp.uh_sport = htons(LOCAL_PORT); /* XXX */ 148 udp.uh_dport = port; /* XXX */ 149 udp.uh_ulen = htons(sizeof(udp) + len); 150 memset(&udp.uh_sum, 0, sizeof(udp.uh_sum)); 151 152 udp.uh_sum = wrapsum(checksum((unsigned char *)&udp, sizeof(udp), 153 checksum(data, len, checksum((unsigned char *)&ip.ip_src, 154 2 * sizeof(ip.ip_src), 155 IPPROTO_UDP + (u_int32_t)ntohs(udp.uh_ulen))))); 156 157 memcpy(&buf[*bufix], &udp, sizeof(udp)); 158 *bufix += sizeof(udp); 159 } 160 161 ssize_t 162 decode_hw_header(unsigned char *buf, int bufix, struct hardware *from) 163 { 164 struct ether_header eh; 165 166 memcpy(&eh, buf + bufix, ETHER_HEADER_SIZE); 167 168 memcpy(from->haddr, eh.ether_shost, sizeof(eh.ether_shost)); 169 from->htype = ARPHRD_ETHER; 170 from->hlen = sizeof(eh.ether_shost); 171 172 return (sizeof(eh)); 173 } 174 175 ssize_t 176 decode_udp_ip_header(unsigned char *buf, int bufix, struct sockaddr_in *from, 177 unsigned char *data, int buflen) 178 { 179 struct ip *ip; 180 struct udphdr *udp; 181 u_int32_t ip_len = (buf[bufix] & 0xf) << 2; 182 u_int32_t sum, usum; 183 static int ip_packets_seen; 184 static int ip_packets_bad_checksum; 185 static int udp_packets_seen; 186 static int udp_packets_bad_checksum; 187 static int udp_packets_length_checked; 188 static int udp_packets_length_overflow; 189 int len = 0; 190 191 ip = (struct ip *)(buf + bufix); 192 udp = (struct udphdr *)(buf + bufix + ip_len); 193 194 /* Check the IP header checksum - it should be zero. */ 195 ip_packets_seen++; 196 if (wrapsum(checksum(buf + bufix, ip_len, 0)) != 0) { 197 ip_packets_bad_checksum++; 198 if (ip_packets_seen > 4 && 199 (ip_packets_seen / ip_packets_bad_checksum) < 2) { 200 note("%d bad IP checksums seen in %d packets", 201 ip_packets_bad_checksum, ip_packets_seen); 202 ip_packets_seen = ip_packets_bad_checksum = 0; 203 } 204 return (-1); 205 } 206 207 if (ntohs(ip->ip_len) != buflen) 208 debug("ip length %d disagrees with bytes received %d.", 209 ntohs(ip->ip_len), buflen); 210 211 memcpy(&from->sin_addr, &ip->ip_src, 4); 212 213 /* 214 * Compute UDP checksums, including the ``pseudo-header'', the 215 * UDP header and the data. If the UDP checksum field is zero, 216 * we're not supposed to do a checksum. 217 */ 218 if (!data) { 219 data = buf + bufix + ip_len + sizeof(*udp); 220 len = ntohs(udp->uh_ulen) - sizeof(*udp); 221 udp_packets_length_checked++; 222 if (len + data > buf + bufix + buflen) { 223 udp_packets_length_overflow++; 224 if (udp_packets_length_checked > 4 && 225 (udp_packets_length_checked / 226 udp_packets_length_overflow) < 2) { 227 note("%d udp packets in %d too long - dropped", 228 udp_packets_length_overflow, 229 udp_packets_length_checked); 230 udp_packets_length_overflow = 231 udp_packets_length_checked = 0; 232 } 233 return (-1); 234 } 235 if (len + data != buf + bufix + buflen) 236 debug("accepting packet with data after udp payload."); 237 } 238 239 usum = udp->uh_sum; 240 udp->uh_sum = 0; 241 242 sum = wrapsum(checksum((unsigned char *)udp, sizeof(*udp), 243 checksum(data, len, checksum((unsigned char *)&ip->ip_src, 244 2 * sizeof(ip->ip_src), 245 IPPROTO_UDP + (u_int32_t)ntohs(udp->uh_ulen))))); 246 247 udp_packets_seen++; 248 if (usum && usum != sum) { 249 udp_packets_bad_checksum++; 250 if (udp_packets_seen > 4 && 251 (udp_packets_seen / udp_packets_bad_checksum) < 2) { 252 note("%d bad udp checksums in %d packets", 253 udp_packets_bad_checksum, udp_packets_seen); 254 udp_packets_seen = udp_packets_bad_checksum = 0; 255 } 256 return (-1); 257 } 258 259 memcpy(&from->sin_port, &udp->uh_sport, sizeof(udp->uh_sport)); 260 261 return (ip_len + sizeof(*udp)); 262 } 263