]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/libsystemd-network/dhcp-packet.c
Merge pull request #4635 from eworm-de/resolved
[thirdparty/systemd.git] / src / libsystemd-network / dhcp-packet.c
CommitLineData
cf597f65
TG
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
cf597f65 21#include <errno.h>
cf597f65
TG
22#include <net/ethernet.h>
23#include <net/if_arp.h>
cf0fbc49 24#include <string.h>
cf597f65 25
cf597f65 26#include "dhcp-internal.h"
cf0fbc49 27#include "dhcp-protocol.h"
cf597f65
TG
28
29#define DHCP_CLIENT_MIN_OPTIONS_SIZE 312
30
31int dhcp_message_init(DHCPMessage *message, uint8_t op, uint32_t xid,
76253e73
DW
32 uint8_t type, uint16_t arp_type, size_t optlen,
33 size_t *optoffset) {
20b958bf
TG
34 size_t offset = 0;
35 int r;
cf597f65 36
0a1b6da8 37 assert(op == BOOTREQUEST || op == BOOTREPLY);
76253e73 38 assert(arp_type == ARPHRD_ETHER || arp_type == ARPHRD_INFINIBAND);
0a1b6da8 39
cf597f65 40 message->op = op;
76253e73
DW
41 message->htype = arp_type;
42 message->hlen = (arp_type == ARPHRD_ETHER) ? ETHER_ADDR_LEN : 0;
cf597f65 43 message->xid = htobe32(xid);
3b7ca119 44 message->magic = htobe32(DHCP_MAGIC_COOKIE);
cf597f65 45
04b28be1 46 r = dhcp_option_append(message, optlen, &offset, 0,
22805d92 47 SD_DHCP_OPTION_MESSAGE_TYPE, 1, &type);
20b958bf
TG
48 if (r < 0)
49 return r;
cf597f65 50
20b958bf 51 *optoffset = offset;
cf597f65
TG
52
53 return 0;
54}
55
0bbc2c1f
TG
56uint16_t dhcp_packet_checksum(uint8_t *buf, size_t len) {
57 uint64_t *buf_64 = (uint64_t*)buf;
58 uint64_t *end_64 = buf_64 + (len / sizeof(uint64_t));
d5761274
TG
59 uint64_t sum = 0;
60
0bbc2c1f
TG
61 /* See RFC1071 */
62
d5761274
TG
63 while (buf_64 < end_64) {
64 sum += *buf_64;
65 if (sum < *buf_64)
0bbc2c1f 66 /* wrap around in one's complement */
d5761274
TG
67 sum++;
68
313cefa1 69 buf_64++;
d5761274
TG
70 }
71
0bbc2c1f
TG
72 if (len % sizeof(uint64_t)) {
73 /* If the buffer is not aligned to 64-bit, we need
74 to zero-pad the last few bytes and add them in */
75 uint64_t buf_tail = 0;
cf597f65 76
0bbc2c1f 77 memcpy(&buf_tail, buf_64, len % sizeof(uint64_t));
cf597f65 78
0bbc2c1f
TG
79 sum += buf_tail;
80 if (sum < buf_tail)
81 /* wrap around */
d5761274 82 sum++;
cf597f65
TG
83 }
84
85 while (sum >> 16)
86 sum = (sum & 0xffff) + (sum >> 16);
87
88 return ~sum;
89}
90
63edaa62
TG
91void dhcp_packet_append_ip_headers(DHCPPacket *packet, be32_t source_addr,
92 uint16_t source_port, be32_t destination_addr,
93 uint16_t destination_port, uint16_t len) {
cf597f65
TG
94 packet->ip.version = IPVERSION;
95 packet->ip.ihl = DHCP_IP_SIZE / 4;
96 packet->ip.tot_len = htobe16(len);
97
85923f79
TG
98 packet->ip.tos = IPTOS_CLASS_CS6;
99
cf597f65 100 packet->ip.protocol = IPPROTO_UDP;
63edaa62
TG
101 packet->ip.saddr = source_addr;
102 packet->ip.daddr = destination_addr;
cf597f65 103
63edaa62
TG
104 packet->udp.source = htobe16(source_port);
105 packet->udp.dest = htobe16(destination_port);
cf597f65
TG
106
107 packet->udp.len = htobe16(len - DHCP_IP_SIZE);
108
109 packet->ip.check = packet->udp.len;
0bbc2c1f 110 packet->udp.check = dhcp_packet_checksum((uint8_t*)&packet->ip.ttl, len - 8);
cf597f65
TG
111
112 packet->ip.ttl = IPDEFTTL;
113 packet->ip.check = 0;
0bbc2c1f 114 packet->ip.check = dhcp_packet_checksum((uint8_t*)&packet->ip, DHCP_IP_SIZE);
cf597f65
TG
115}
116
55dab2ed 117int dhcp_packet_verify_headers(DHCPPacket *packet, size_t len, bool checksum) {
cf597f65
TG
118 size_t hdrlen;
119
5266a81e
TG
120 assert(packet);
121
06b44be7 122 /* IP */
cf597f65 123
6e34949d 124 if (packet->ip.version != IPVERSION) {
aa6fc9b8 125 log_debug("ignoring packet: not IPv4");
6e34949d
TG
126 return -EINVAL;
127 }
128
06b44be7 129 if (packet->ip.ihl < 5) {
aa6fc9b8
TG
130 log_debug("ignoring packet: IPv4 IHL (%u words) invalid",
131 packet->ip.ihl);
cf597f65 132 return -EINVAL;
ac4f16ab 133 }
cf597f65
TG
134
135 hdrlen = packet->ip.ihl * 4;
06b44be7 136 if (hdrlen < 20) {
aa6fc9b8
TG
137 log_debug("ignoring packet: IPv4 IHL (%zu bytes) "
138 "smaller than minimum (20 bytes)", hdrlen);
06b44be7
TG
139 return -EINVAL;
140 }
141
142 if (len < hdrlen) {
aa6fc9b8
TG
143 log_debug("ignoring packet: packet (%zu bytes) "
144 "smaller than expected (%zu) by IP header", len,
145 hdrlen);
cf597f65 146 return -EINVAL;
ac4f16ab 147 }
cf597f65 148
06b44be7
TG
149 /* UDP */
150
d454a674 151 if (packet->ip.protocol != IPPROTO_UDP) {
aa6fc9b8 152 log_debug("ignoring packet: not UDP");
d454a674
UTL
153 return -EINVAL;
154 }
155
8fa2eeac 156 if (len < hdrlen + be16toh(packet->udp.len)) {
aa6fc9b8
TG
157 log_debug("ignoring packet: packet (%zu bytes) "
158 "smaller than expected (%zu) by UDP header", len,
159 hdrlen + be16toh(packet->udp.len));
ac4f16ab
TG
160 return -EINVAL;
161 }
cf597f65 162
2ad7561f 163 if (be16toh(packet->udp.dest) != DHCP_PORT_CLIENT) {
aa6fc9b8
TG
164 log_debug("ignoring packet: to port %u, which "
165 "is not the DHCP client port (%u)",
166 be16toh(packet->udp.dest), DHCP_PORT_CLIENT);
2ad7561f
TG
167 return -EINVAL;
168 }
169
170 /* checksums - computing these is relatively expensive, so only do it
171 if all the other checks have passed
172 */
173
0bbc2c1f 174 if (dhcp_packet_checksum((uint8_t*)&packet->ip, hdrlen)) {
aa6fc9b8 175 log_debug("ignoring packet: invalid IP checksum");
2ad7561f
TG
176 return -EINVAL;
177 }
178
55dab2ed 179 if (checksum && packet->udp.check) {
cf597f65
TG
180 packet->ip.check = packet->udp.len;
181 packet->ip.ttl = 0;
182
0bbc2c1f 183 if (dhcp_packet_checksum((uint8_t*)&packet->ip.ttl,
ac4f16ab 184 be16toh(packet->udp.len) + 12)) {
aa6fc9b8 185 log_debug("ignoring packet: invalid UDP checksum");
cf597f65 186 return -EINVAL;
ac4f16ab 187 }
cf597f65
TG
188 }
189
cf597f65
TG
190 return 0;
191}