]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/libsystemd-network/dhcp-packet.c
Add an extra debug log to dissect_image()
[thirdparty/systemd.git] / src / libsystemd-network / dhcp-packet.c
CommitLineData
db9ecf05 1/* SPDX-License-Identifier: LGPL-2.1-or-later */
cf597f65 2/***
810adae9 3 Copyright © 2013 Intel Corporation. All rights reserved.
cf597f65
TG
4***/
5
cf597f65 6#include <errno.h>
cf597f65
TG
7#include <net/ethernet.h>
8#include <net/if_arp.h>
cf0fbc49 9#include <string.h>
cf597f65 10
8664ded7
YW
11#include "dhcp-option.h"
12#include "dhcp-packet.h"
97fa338d 13#include "memory-util.h"
cf597f65
TG
14
15#define DHCP_CLIENT_MIN_OPTIONS_SIZE 312
16
97fa338d
YW
17int dhcp_message_init(
18 DHCPMessage *message,
19 uint8_t op,
20 uint32_t xid,
21 uint8_t type,
22 uint16_t arp_type,
23 uint8_t hlen,
24 const uint8_t *chaddr,
25 size_t optlen,
26 size_t *optoffset) {
27
20b958bf
TG
28 size_t offset = 0;
29 int r;
cf597f65 30
3742095b 31 assert(IN_SET(op, BOOTREQUEST, BOOTREPLY));
97fa338d 32 assert(chaddr || hlen == 0);
0a1b6da8 33
cf597f65 34 message->op = op;
76253e73 35 message->htype = arp_type;
97fa338d
YW
36
37 /* RFC2131 section 4.1.1:
38 The client MUST include its hardware address in the ’chaddr’ field, if
39 necessary for delivery of DHCP reply messages.
40
41 RFC 4390 section 2.1:
42 A DHCP client, when working over an IPoIB interface, MUST follow the
43 following rules:
44 "htype" (hardware address type) MUST be 32 [ARPPARAM].
45 "hlen" (hardware address length) MUST be 0.
46 "chaddr" (client hardware address) field MUST be zeroed.
47 */
0b6a4795 48 message->hlen = arp_type == ARPHRD_INFINIBAND ? 0 : hlen;
97fa338d
YW
49 memcpy_safe(message->chaddr, chaddr, message->hlen);
50
cf597f65 51 message->xid = htobe32(xid);
3b7ca119 52 message->magic = htobe32(DHCP_MAGIC_COOKIE);
cf597f65 53
04b28be1 54 r = dhcp_option_append(message, optlen, &offset, 0,
22805d92 55 SD_DHCP_OPTION_MESSAGE_TYPE, 1, &type);
20b958bf
TG
56 if (r < 0)
57 return r;
cf597f65 58
20b958bf 59 *optoffset = offset;
cf597f65
TG
60
61 return 0;
62}
63
0bbc2c1f
TG
64uint16_t dhcp_packet_checksum(uint8_t *buf, size_t len) {
65 uint64_t *buf_64 = (uint64_t*)buf;
66 uint64_t *end_64 = buf_64 + (len / sizeof(uint64_t));
d5761274
TG
67 uint64_t sum = 0;
68
0bbc2c1f
TG
69 /* See RFC1071 */
70
d5761274
TG
71 while (buf_64 < end_64) {
72 sum += *buf_64;
73 if (sum < *buf_64)
0bbc2c1f 74 /* wrap around in one's complement */
d5761274
TG
75 sum++;
76
313cefa1 77 buf_64++;
d5761274
TG
78 }
79
0bbc2c1f
TG
80 if (len % sizeof(uint64_t)) {
81 /* If the buffer is not aligned to 64-bit, we need
82 to zero-pad the last few bytes and add them in */
83 uint64_t buf_tail = 0;
cf597f65 84
0bbc2c1f 85 memcpy(&buf_tail, buf_64, len % sizeof(uint64_t));
cf597f65 86
0bbc2c1f
TG
87 sum += buf_tail;
88 if (sum < buf_tail)
89 /* wrap around */
d5761274 90 sum++;
cf597f65
TG
91 }
92
93 while (sum >> 16)
94 sum = (sum & 0xffff) + (sum >> 16);
95
96 return ~sum;
97}
98
63edaa62
TG
99void dhcp_packet_append_ip_headers(DHCPPacket *packet, be32_t source_addr,
100 uint16_t source_port, be32_t destination_addr,
afe42aef 101 uint16_t destination_port, uint16_t len, int ip_service_type) {
cf597f65
TG
102 packet->ip.version = IPVERSION;
103 packet->ip.ihl = DHCP_IP_SIZE / 4;
104 packet->ip.tot_len = htobe16(len);
105
afe42aef
SC
106 if (ip_service_type >= 0)
107 packet->ip.tos = ip_service_type;
108 else
109 packet->ip.tos = IPTOS_CLASS_CS6;
85923f79 110
cf597f65 111 packet->ip.protocol = IPPROTO_UDP;
63edaa62
TG
112 packet->ip.saddr = source_addr;
113 packet->ip.daddr = destination_addr;
cf597f65 114
63edaa62
TG
115 packet->udp.source = htobe16(source_port);
116 packet->udp.dest = htobe16(destination_port);
cf597f65
TG
117
118 packet->udp.len = htobe16(len - DHCP_IP_SIZE);
119
120 packet->ip.check = packet->udp.len;
0bbc2c1f 121 packet->udp.check = dhcp_packet_checksum((uint8_t*)&packet->ip.ttl, len - 8);
cf597f65
TG
122
123 packet->ip.ttl = IPDEFTTL;
124 packet->ip.check = 0;
0bbc2c1f 125 packet->ip.check = dhcp_packet_checksum((uint8_t*)&packet->ip, DHCP_IP_SIZE);
cf597f65
TG
126}
127
9faed222 128int dhcp_packet_verify_headers(DHCPPacket *packet, size_t len, bool checksum, uint16_t port) {
cf597f65
TG
129 size_t hdrlen;
130
5266a81e 131 assert(packet);
a57dd1d1
YW
132
133 if (len < sizeof(DHCPPacket))
134 return 0;
5266a81e 135
06b44be7 136 /* IP */
cf597f65 137
baaa35ad
ZJS
138 if (packet->ip.version != IPVERSION)
139 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL),
140 "ignoring packet: not IPv4");
6e34949d 141
baaa35ad
ZJS
142 if (packet->ip.ihl < 5)
143 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL),
c0f86d66 144 "ignoring packet: IPv4 IHL (%i words) invalid",
baaa35ad 145 packet->ip.ihl);
cf597f65
TG
146
147 hdrlen = packet->ip.ihl * 4;
baaa35ad
ZJS
148 if (hdrlen < 20)
149 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL),
9a94baa6 150 "ignoring packet: IPv4 IHL (%zu bytes) smaller than minimum (20 bytes)",
baaa35ad
ZJS
151 hdrlen);
152
153 if (len < hdrlen)
154 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL),
9a94baa6 155 "ignoring packet: packet (%zu bytes) smaller than expected (%zu) by IP header",
baaa35ad 156 len, hdrlen);
cf597f65 157
06b44be7
TG
158 /* UDP */
159
baaa35ad
ZJS
160 if (packet->ip.protocol != IPPROTO_UDP)
161 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL),
162 "ignoring packet: not UDP");
d454a674 163
baaa35ad
ZJS
164 if (len < hdrlen + be16toh(packet->udp.len))
165 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL),
9a94baa6 166 "ignoring packet: packet (%zu bytes) smaller than expected (%zu) by UDP header",
baaa35ad 167 len, hdrlen + be16toh(packet->udp.len));
cf597f65 168
baaa35ad
ZJS
169 if (be16toh(packet->udp.dest) != port)
170 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL),
9a94baa6 171 "ignoring packet: to port %u, which is not the DHCP client port (%u)",
baaa35ad 172 be16toh(packet->udp.dest), port);
2ad7561f
TG
173
174 /* checksums - computing these is relatively expensive, so only do it
175 if all the other checks have passed
176 */
177
baaa35ad
ZJS
178 if (dhcp_packet_checksum((uint8_t*)&packet->ip, hdrlen))
179 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL),
180 "ignoring packet: invalid IP checksum");
2ad7561f 181
55dab2ed 182 if (checksum && packet->udp.check) {
cf597f65
TG
183 packet->ip.check = packet->udp.len;
184 packet->ip.ttl = 0;
185
0bbc2c1f 186 if (dhcp_packet_checksum((uint8_t*)&packet->ip.ttl,
baaa35ad
ZJS
187 be16toh(packet->udp.len) + 12))
188 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL),
189 "ignoring packet: invalid UDP checksum");
cf597f65
TG
190 }
191
cf597f65
TG
192 return 0;
193}