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