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