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