GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: sbin/dhclient/packet.c Lines: 0 61 0.0 %
Date: 2016-12-06 Branches: 0 56 0.0 %

Line Branch Exec Source
1
/*	$OpenBSD: packet.c,v 1.30 2016/02/06 19:30:52 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/queue.h>
44
#include <sys/socket.h>
45
46
#include <net/if.h>
47
48
#include <netinet/in.h>
49
#include <netinet/ip.h>
50
#include <netinet/udp.h>
51
#include <netinet/if_ether.h>
52
53
#include <signal.h>
54
#include <stdio.h>
55
#include <string.h>
56
#include <unistd.h>
57
58
#include "dhcp.h"
59
#include "dhcpd.h"
60
61
u_int32_t
62
checksum(unsigned char *buf, unsigned nbytes, u_int32_t sum)
63
{
64
	int i;
65
66
	/* Checksum all the pairs of bytes first. */
67
	for (i = 0; i < (nbytes & ~1U); i += 2) {
68
		sum += (u_int16_t)ntohs(*((u_int16_t *)(buf + i)));
69
		if (sum > 0xFFFF)
70
			sum -= 0xFFFF;
71
	}
72
73
	/*
74
	 * If there's a single byte left over, checksum it, too.
75
	 * Network byte order is big-endian, so the remaining byte is
76
	 * the high byte.
77
	 */
78
	if (i < nbytes) {
79
		sum += buf[i] << 8;
80
		if (sum > 0xFFFF)
81
			sum -= 0xFFFF;
82
	}
83
84
	return (sum);
85
}
86
87
u_int32_t
88
wrapsum(u_int32_t sum)
89
{
90
	sum = ~sum & 0xFFFF;
91
	return (htons(sum));
92
}
93
94
void
95
assemble_eh_header(struct ether_header *eh)
96
{
97
	memset(eh->ether_dhost, 0xff, sizeof(eh->ether_dhost));
98
99
	memcpy(eh->ether_shost, ifi->hw_address.ether_addr_octet,
100
	    sizeof(eh->ether_shost));
101
102
	eh->ether_type = htons(ETHERTYPE_IP);
103
}
104
105
ssize_t
106
decode_hw_header(unsigned char *buf, int bufix, struct ether_addr *from)
107
{
108
	struct ether_header eh;
109
110
	memcpy(&eh, buf + bufix, ETHER_HDR_LEN);
111
112
	memcpy(from->ether_addr_octet, eh.ether_shost, ETHER_ADDR_LEN);
113
114
	return (sizeof(eh));
115
}
116
117
ssize_t
118
decode_udp_ip_header(unsigned char *buf, int bufix, struct sockaddr_in *from,
119
    int buflen)
120
{
121
	struct ip *ip;
122
	struct udphdr *udp;
123
	unsigned char *data;
124
	u_int32_t ip_len;
125
	u_int32_t sum, usum;
126
	static int ip_packets_seen;
127
	static int ip_packets_bad_checksum;
128
	static int udp_packets_seen;
129
	static int udp_packets_bad_checksum;
130
	static int udp_packets_length_checked;
131
	static int udp_packets_length_overflow;
132
	int len;
133
134
	/* Assure that an entire IP header is within the buffer. */
135
	if (sizeof(*ip) > buflen)
136
		return (-1);
137
	ip_len = (buf[bufix] & 0xf) << 2;
138
	if (ip_len > buflen)
139
		return (-1);
140
	ip = (struct ip *)(buf + bufix);
141
	ip_packets_seen++;
142
143
	/* Check the IP header checksum - it should be zero. */
144
	if (wrapsum(checksum(buf + bufix, ip_len, 0)) != 0) {
145
		ip_packets_bad_checksum++;
146
		if (ip_packets_seen > 4 && ip_packets_bad_checksum != 0 &&
147
		    (ip_packets_seen / ip_packets_bad_checksum) < 2) {
148
			note("%d bad IP checksums seen in %d packets",
149
			    ip_packets_bad_checksum, ip_packets_seen);
150
			ip_packets_seen = ip_packets_bad_checksum = 0;
151
		}
152
		return (-1);
153
	}
154
155
	memcpy(&from->sin_addr, &ip->ip_src, sizeof(from->sin_addr));
156
157
#ifdef DEBUG
158
	if (ntohs(ip->ip_len) != buflen)
159
		debug("ip length %hu disagrees with bytes received %d.",
160
		    ntohs(ip->ip_len), buflen);
161
#endif
162
163
	/* Assure that the entire IP packet is within the buffer. */
164
	if (ntohs(ip->ip_len) > buflen)
165
		return (-1);
166
167
	/* Assure that the UDP header is within the buffer. */
168
	if (ip_len + sizeof(*udp) > buflen)
169
		return (-1);
170
	udp = (struct udphdr *)(buf + bufix + ip_len);
171
	udp_packets_seen++;
172
173
	/* Assure that the entire UDP packet is within the buffer. */
174
	if (ip_len + ntohs(udp->uh_ulen) > buflen)
175
		return (-1);
176
	data = buf + bufix + ip_len + sizeof(*udp);
177
178
	/*
179
	 * Compute UDP checksums, including the ``pseudo-header'', the
180
	 * UDP header and the data. If the UDP checksum field is zero,
181
	 * we're not supposed to do a checksum.
182
	 */
183
	udp_packets_length_checked++;
184
	len = ntohs(udp->uh_ulen) - sizeof(*udp);
185
	if ((len < 0) || (len + data > buf + bufix + buflen)) {
186
		udp_packets_length_overflow++;
187
		if (udp_packets_length_checked > 4 &&
188
		    udp_packets_length_overflow != 0 &&
189
		    (udp_packets_length_checked /
190
		    udp_packets_length_overflow) < 2) {
191
			note("%d udp packets in %d too long - dropped",
192
			    udp_packets_length_overflow,
193
			    udp_packets_length_checked);
194
			udp_packets_length_overflow =
195
			    udp_packets_length_checked = 0;
196
		}
197
		return (-1);
198
	}
199
#ifdef DEBUG
200
	if (len + data != buf + bufix + buflen)
201
		debug("accepting packet with data after udp payload.");
202
#endif
203
204
	usum = udp->uh_sum;
205
	udp->uh_sum = 0;
206
207
	sum = wrapsum(checksum((unsigned char *)udp, sizeof(*udp),
208
	    checksum(data, len, checksum((unsigned char *)&ip->ip_src,
209
	    2 * sizeof(ip->ip_src),
210
	    IPPROTO_UDP + (u_int32_t)ntohs(udp->uh_ulen)))));
211
212
	udp_packets_seen++;
213
	if (usum && usum != sum) {
214
		udp_packets_bad_checksum++;
215
		if (udp_packets_seen > 4 && udp_packets_bad_checksum != 0 &&
216
		    (udp_packets_seen / udp_packets_bad_checksum) < 2) {
217
			note("%d bad udp checksums in %d packets",
218
			    udp_packets_bad_checksum, udp_packets_seen);
219
			udp_packets_seen = udp_packets_bad_checksum = 0;
220
		}
221
		return (-1);
222
	}
223
224
	memcpy(&from->sin_port, &udp->uh_sport, sizeof(udp->uh_sport));
225
226
	return (ip_len + sizeof(*udp));
227
}