1 /* $OpenBSD: packet.c,v 1.11 2016/02/07 00:49:28 krw 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/types.h> 44 #include <sys/socket.h> 45 46 #include <arpa/inet.h> 47 48 #include <net/if.h> 49 #include <net/if_enc.h> 50 51 #include <netinet/in.h> 52 #include <netinet/ip.h> 53 #include <netinet/udp.h> 54 #include <netinet/if_ether.h> 55 56 #include <string.h> 57 58 #include "dhcp.h" 59 #include "dhcpd.h" 60 61 62 u_int32_t checksum(unsigned char *, unsigned, u_int32_t); 63 u_int32_t wrapsum(u_int32_t); 64 65 u_int32_t 66 checksum(unsigned char *buf, unsigned nbytes, u_int32_t sum) 67 { 68 int i; 69 70 /* Checksum all the pairs of bytes first... */ 71 for (i = 0; i < (nbytes & ~1U); i += 2) { 72 sum += (u_int16_t)ntohs(*((u_int16_t *)(buf + i))); 73 if (sum > 0xFFFF) 74 sum -= 0xFFFF; 75 } 76 77 /* 78 * If there's a single byte left over, checksum it, too. 79 * Network byte order is big-endian, so the remaining byte is 80 * the high byte. 81 */ 82 if (i < nbytes) { 83 sum += buf[i] << 8; 84 if (sum > 0xFFFF) 85 sum -= 0xFFFF; 86 } 87 88 return (sum); 89 } 90 91 u_int32_t 92 wrapsum(u_int32_t sum) 93 { 94 sum = ~sum & 0xFFFF; 95 return (htons(sum)); 96 } 97 98 void 99 assemble_hw_header(struct interface_info *interface, unsigned char *buf, 100 int *bufix, struct hardware *to) 101 { 102 struct ether_header eh; 103 104 if (to != NULL && to->hlen == 6) /* XXX */ 105 memcpy(eh.ether_dhost, to->haddr, sizeof(eh.ether_dhost)); 106 else 107 memset(eh.ether_dhost, 0xff, sizeof(eh.ether_dhost)); 108 109 /* source address is filled in by the kernel */ 110 memset(eh.ether_shost, 0x00, sizeof(eh.ether_shost)); 111 112 eh.ether_type = htons(ETHERTYPE_IP); 113 114 memcpy(&buf[*bufix], &eh, ETHER_HDR_LEN); 115 *bufix += ETHER_HDR_LEN; 116 } 117 118 void 119 assemble_udp_ip_header(struct interface_info *interface, unsigned char *buf, 120 int *bufix, u_int32_t from, u_int32_t to, unsigned int port, 121 unsigned char *data, int len) 122 { 123 struct ip ip; 124 struct udphdr udp; 125 126 ip.ip_v = 4; 127 ip.ip_hl = 5; 128 ip.ip_tos = IPTOS_LOWDELAY; 129 ip.ip_len = htons(sizeof(ip) + sizeof(udp) + len); 130 ip.ip_id = 0; 131 ip.ip_off = 0; 132 ip.ip_ttl = 16; 133 ip.ip_p = IPPROTO_UDP; 134 ip.ip_sum = 0; 135 ip.ip_src.s_addr = from; 136 ip.ip_dst.s_addr = to; 137 138 ip.ip_sum = wrapsum(checksum((unsigned char *)&ip, sizeof(ip), 0)); 139 memcpy(&buf[*bufix], &ip, sizeof(ip)); 140 *bufix += sizeof(ip); 141 142 udp.uh_sport = server_port; /* XXX */ 143 udp.uh_dport = port; /* XXX */ 144 udp.uh_ulen = htons(sizeof(udp) + len); 145 memset(&udp.uh_sum, 0, sizeof(udp.uh_sum)); 146 147 udp.uh_sum = wrapsum(checksum((unsigned char *)&udp, sizeof(udp), 148 checksum(data, len, checksum((unsigned char *)&ip.ip_src, 149 2 * sizeof(ip.ip_src), 150 IPPROTO_UDP + (u_int32_t)ntohs(udp.uh_ulen))))); 151 152 memcpy(&buf[*bufix], &udp, sizeof(udp)); 153 *bufix += sizeof(udp); 154 } 155 156 ssize_t 157 decode_hw_header(struct interface_info *interface, unsigned char *buf, 158 int bufix, struct hardware *from) 159 { 160 struct ether_header eh; 161 size_t offset = 0; 162 163 if (interface->hw_address.htype == HTYPE_IPSEC_TUNNEL) { 164 u_int32_t ip_len; 165 struct ip *ip; 166 167 bufix += ENC_HDRLEN; 168 ip_len = (buf[bufix] & 0xf) << 2; 169 ip = (struct ip *)(buf + bufix); 170 171 /* Encapsulated IP */ 172 if (ip->ip_p != IPPROTO_IPIP) 173 return (-1); 174 175 bzero(&eh, sizeof(eh)); 176 offset = ENC_HDRLEN + ip_len; 177 } else { 178 memcpy(&eh, buf + bufix, ETHER_HDR_LEN); 179 offset = sizeof(eh); 180 } 181 182 memcpy(from->haddr, eh.ether_shost, sizeof(eh.ether_shost)); 183 from->htype = ARPHRD_ETHER; 184 from->hlen = sizeof(eh.ether_shost); 185 186 return (offset); 187 } 188 189 ssize_t 190 decode_udp_ip_header(struct interface_info *interface, unsigned char *buf, 191 int bufix, struct sockaddr_in *from, int buflen) 192 { 193 struct ip *ip; 194 struct udphdr *udp; 195 unsigned char *data; 196 u_int32_t ip_len; 197 u_int32_t sum, usum; 198 static unsigned int ip_packets_seen; 199 static unsigned int ip_packets_bad_checksum; 200 static unsigned int udp_packets_seen; 201 static unsigned int udp_packets_bad_checksum; 202 static unsigned int udp_packets_length_checked; 203 static unsigned int udp_packets_length_overflow; 204 int len; 205 206 /* Assure that an entire IP header is within the buffer. */ 207 if (sizeof(*ip) > buflen) 208 return (-1); 209 ip_len = (buf[bufix] & 0xf) << 2; 210 if (ip_len > buflen) 211 return (-1); 212 ip = (struct ip *)(buf + bufix); 213 ip_packets_seen++; 214 215 /* Check the IP header checksum - it should be zero. */ 216 if (wrapsum(checksum(buf + bufix, ip_len, 0)) != 0) { 217 ip_packets_bad_checksum++; 218 if (ip_packets_seen > 4 && ip_packets_bad_checksum != 0 && 219 (ip_packets_seen / ip_packets_bad_checksum) < 2) { 220 note("%u bad IP checksums seen in %u packets", 221 ip_packets_bad_checksum, ip_packets_seen); 222 ip_packets_seen = ip_packets_bad_checksum = 0; 223 } 224 return (-1); 225 } 226 227 memcpy(&from->sin_addr, &ip->ip_src, sizeof(from->sin_addr)); 228 229 #ifdef DEBUG 230 if (ntohs(ip->ip_len) != buflen) 231 debug("ip length %d disagrees with bytes received %d.", 232 ntohs(ip->ip_len), buflen); 233 #endif 234 235 /* Assure that the entire IP packet is within the buffer. */ 236 if (ntohs(ip->ip_len) > buflen) 237 return (-1); 238 239 /* Assure that the UDP header is within the buffer. */ 240 if (ip_len + sizeof(*udp) > buflen) 241 return (-1); 242 udp = (struct udphdr *)(buf + bufix + ip_len); 243 udp_packets_seen++; 244 245 /* Assure that the entire UDP packet is within the buffer. */ 246 if (ip_len + ntohs(udp->uh_ulen) > buflen) 247 return (-1); 248 data = buf + bufix + ip_len + sizeof(*udp); 249 250 /* 251 * Compute UDP checksums, including the ``pseudo-header'', the 252 * UDP header and the data. If the UDP checksum field is zero, 253 * we're not supposed to do a checksum. 254 */ 255 udp_packets_length_checked++; 256 len = ntohs(udp->uh_ulen) - sizeof(*udp); 257 if ((len < 0) || (len + data > buf + bufix + buflen)) { 258 udp_packets_length_overflow++; 259 if (udp_packets_length_checked > 4 && 260 udp_packets_length_overflow != 0 && 261 (udp_packets_length_checked / 262 udp_packets_length_overflow) < 2) { 263 note("%u udp packets in %u too long - dropped", 264 udp_packets_length_overflow, 265 udp_packets_length_checked); 266 udp_packets_length_overflow = 267 udp_packets_length_checked = 0; 268 } 269 return (-1); 270 } 271 if (len + data != buf + bufix + buflen) 272 debug("accepting packet with data after udp payload."); 273 274 usum = udp->uh_sum; 275 udp->uh_sum = 0; 276 277 sum = wrapsum(checksum((unsigned char *)udp, sizeof(*udp), 278 checksum(data, len, checksum((unsigned char *)&ip->ip_src, 279 2 * sizeof(ip->ip_src), 280 IPPROTO_UDP + (u_int32_t)ntohs(udp->uh_ulen))))); 281 282 udp_packets_seen++; 283 if (usum && usum != sum) { 284 udp_packets_bad_checksum++; 285 if (udp_packets_seen > 4 && udp_packets_bad_checksum != 0 && 286 (udp_packets_seen / udp_packets_bad_checksum) < 2) { 287 note("%u bad udp checksums in %u packets", 288 udp_packets_bad_checksum, udp_packets_seen); 289 udp_packets_seen = udp_packets_bad_checksum = 0; 290 } 291 return (-1); 292 } 293 294 memcpy(&from->sin_port, &udp->uh_sport, sizeof(udp->uh_sport)); 295 296 return (ip_len + sizeof(*udp)); 297 } 298