chiark / gitweb /
fba9c46ef226fd9e3cf4d0bdbe2b67879c0e3aac
[elogind.git] / src / libsystemd-network / dhcp-packet.c
1 /***
2   This file is part of systemd.
3
4   Copyright (C) 2013 Intel Corporation. All rights reserved.
5   Copyright (C) 2014 Tom Gundersen
6
7   systemd is free software; you can redistribute it and/or modify it
8   under the terms of the GNU Lesser General Public License as published by
9   the Free Software Foundation; either version 2.1 of the License, or
10   (at your option) any later version.
11
12   systemd is distributed in the hope that it will be useful, but
13   WITHOUT ANY WARRANTY; without even the implied warranty of
14   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   Lesser General Public License for more details.
16
17   You should have received a copy of the GNU Lesser General Public License
18   along with systemd; If not, see <http://www.gnu.org/licenses/>.
19 ***/
20
21 #include <stdlib.h>
22 #include <errno.h>
23 #include <string.h>
24 #include <stdio.h>
25 #include <net/ethernet.h>
26 #include <net/if_arp.h>
27 #include <sys/param.h>
28
29 #include "util.h"
30 #include "list.h"
31
32 #include "dhcp-protocol.h"
33 #include "dhcp-lease-internal.h"
34 #include "dhcp-internal.h"
35 #include "sd-dhcp-lease.h"
36 #include "sd-dhcp-client.h"
37
38 #define DHCP_CLIENT_MIN_OPTIONS_SIZE            312
39
40 int dhcp_message_init(DHCPMessage *message, uint8_t op, uint32_t xid,
41                       uint8_t type, uint8_t **opt, size_t *optlen) {
42         int err;
43
44         assert(op == BOOTREQUEST || op == BOOTREPLY);
45
46         message->op = op;
47         message->htype = ARPHRD_ETHER;
48         message->hlen = ETHER_ADDR_LEN;
49         message->xid = htobe32(xid);
50         message->magic = htobe32(DHCP_MAGIC_COOKIE);
51
52         *opt = (uint8_t *)(message + 1);
53
54         err = dhcp_option_append(opt, optlen, DHCP_OPTION_MESSAGE_TYPE, 1,
55                                  &type);
56         if (err < 0)
57                 return err;
58
59         return 0;
60 }
61
62 uint16_t dhcp_packet_checksum(void *buf, size_t len) {
63         uint64_t *buf_64 = buf;
64         uint64_t *end_64 = (uint64_t*)buf + (len / sizeof(uint64_t));
65         uint32_t *buf_32;
66         uint16_t *buf_16;
67         uint8_t *buf_8;
68         uint64_t sum = 0;
69
70         while (buf_64 < end_64) {
71                 sum += *buf_64;
72                 if (sum < *buf_64)
73                         sum++;
74
75                 buf_64 ++;
76         }
77
78         buf_32 = (uint32_t*)buf_64;
79
80         if (len & sizeof(uint32_t)) {
81                 sum += *buf_32;
82                 if (sum < *buf_32)
83                         sum++;
84
85                 buf_32 ++;
86         }
87
88         buf_16 = (uint16_t*)buf_32;
89
90         if (len & sizeof(uint16_t)) {
91                 sum += *buf_16;
92                 if (sum < *buf_16)
93                         sum ++;
94
95                 buf_16 ++;
96         }
97
98         buf_8 = (uint8_t*)buf_16;
99
100         if (len & sizeof(uint8_t)) {
101                 sum += *buf_8;
102                 if (sum < *buf_8)
103                         sum++;
104         }
105
106         while (sum >> 16)
107                 sum = (sum & 0xffff) + (sum >> 16);
108
109         return ~sum;
110 }
111
112 void dhcp_packet_append_ip_headers(DHCPPacket *packet, be32_t source_addr,
113                                    uint16_t source_port, be32_t destination_addr,
114                                    uint16_t destination_port, uint16_t len) {
115         packet->ip.version = IPVERSION;
116         packet->ip.ihl = DHCP_IP_SIZE / 4;
117         packet->ip.tot_len = htobe16(len);
118
119         packet->ip.protocol = IPPROTO_UDP;
120         packet->ip.saddr = source_addr;
121         packet->ip.daddr = destination_addr;
122
123         packet->udp.source = htobe16(source_port);
124         packet->udp.dest = htobe16(destination_port);
125
126         packet->udp.len = htobe16(len - DHCP_IP_SIZE);
127
128         packet->ip.check = packet->udp.len;
129         packet->udp.check = dhcp_packet_checksum(&packet->ip.ttl, len - 8);
130
131         packet->ip.ttl = IPDEFTTL;
132         packet->ip.check = 0;
133         packet->ip.check = dhcp_packet_checksum(&packet->ip, DHCP_IP_SIZE);
134 }
135
136 int dhcp_packet_verify_headers(DHCPPacket *packet, size_t len, bool checksum) {
137         size_t hdrlen;
138
139         assert(packet);
140
141         /* IP */
142
143         if (packet->ip.version != IPVERSION) {
144                 log_dhcp_client(client, "ignoring packet: not IPv4");
145                 return -EINVAL;
146         }
147
148         if (packet->ip.ihl < 5) {
149                 log_dhcp_client(client, "ignoring packet: IPv4 IHL (%u words) invalid",
150                                 packet->ip.ihl);
151                 return -EINVAL;
152         }
153
154         hdrlen = packet->ip.ihl * 4;
155         if (hdrlen < 20) {
156                 log_dhcp_client(client, "ignoring packet: IPv4 IHL (%zu bytes) "
157                                 "smaller than minimum (20 bytes)", hdrlen);
158                 return -EINVAL;
159         }
160
161         if (len < hdrlen) {
162                 log_dhcp_client(client, "ignoring packet: packet (%zu bytes) "
163                                 "smaller than expected (%zu) by IP header", len,
164                                 hdrlen);
165                 return -EINVAL;
166         }
167
168         /* UDP */
169
170         if (packet->ip.protocol != IPPROTO_UDP) {
171                 log_dhcp_client(client, "ignoring packet: not UDP");
172                 return -EINVAL;
173         }
174
175         if (len < hdrlen + be16toh(packet->udp.len)) {
176                 log_dhcp_client(client, "ignoring packet: packet (%zu bytes) "
177                                 "smaller than expected (%zu) by UDP header", len,
178                                 hdrlen + be16toh(packet->udp.len));
179                 return -EINVAL;
180         }
181
182         if (be16toh(packet->udp.dest) != DHCP_PORT_CLIENT) {
183                 log_dhcp_client(client, "ignoring packet: to port %u, which "
184                                 "is not the DHCP client port (%u)",
185                                 be16toh(packet->udp.dest), DHCP_PORT_CLIENT);
186                 return -EINVAL;
187         }
188
189         /* checksums - computing these is relatively expensive, so only do it
190            if all the other checks have passed
191          */
192
193         if (dhcp_packet_checksum(&packet->ip, hdrlen)) {
194                 log_dhcp_client(client, "ignoring packet: invalid IP checksum");
195                 return -EINVAL;
196         }
197
198         if (checksum && packet->udp.check) {
199                 packet->ip.check = packet->udp.len;
200                 packet->ip.ttl = 0;
201
202                 if (dhcp_packet_checksum(&packet->ip.ttl,
203                                   be16toh(packet->udp.len) + 12)) {
204                         log_dhcp_client(client, "ignoring packet: invalid UDP checksum");
205                         return -EINVAL;
206                 }
207         }
208
209         return 0;
210 }