]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/libsystemd-network/dhcp-packet.c
tree-wide: drop () around the first argument of a ternary op
[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
cf597f65 11#include "dhcp-internal.h"
cf0fbc49 12#include "dhcp-protocol.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);
6f1b4574 132 assert(len >= sizeof(DHCPPacket));
5266a81e 133
06b44be7 134 /* IP */
cf597f65 135
baaa35ad
ZJS
136 if (packet->ip.version != IPVERSION)
137 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL),
138 "ignoring packet: not IPv4");
6e34949d 139
baaa35ad
ZJS
140 if (packet->ip.ihl < 5)
141 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL),
c0f86d66 142 "ignoring packet: IPv4 IHL (%i words) invalid",
baaa35ad 143 packet->ip.ihl);
cf597f65
TG
144
145 hdrlen = packet->ip.ihl * 4;
baaa35ad
ZJS
146 if (hdrlen < 20)
147 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL),
9a94baa6 148 "ignoring packet: IPv4 IHL (%zu bytes) smaller than minimum (20 bytes)",
baaa35ad
ZJS
149 hdrlen);
150
151 if (len < hdrlen)
152 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL),
9a94baa6 153 "ignoring packet: packet (%zu bytes) smaller than expected (%zu) by IP header",
baaa35ad 154 len, hdrlen);
cf597f65 155
06b44be7
TG
156 /* UDP */
157
baaa35ad
ZJS
158 if (packet->ip.protocol != IPPROTO_UDP)
159 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL),
160 "ignoring packet: not UDP");
d454a674 161
baaa35ad
ZJS
162 if (len < hdrlen + be16toh(packet->udp.len))
163 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL),
9a94baa6 164 "ignoring packet: packet (%zu bytes) smaller than expected (%zu) by UDP header",
baaa35ad 165 len, hdrlen + be16toh(packet->udp.len));
cf597f65 166
baaa35ad
ZJS
167 if (be16toh(packet->udp.dest) != port)
168 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL),
9a94baa6 169 "ignoring packet: to port %u, which is not the DHCP client port (%u)",
baaa35ad 170 be16toh(packet->udp.dest), port);
2ad7561f
TG
171
172 /* checksums - computing these is relatively expensive, so only do it
173 if all the other checks have passed
174 */
175
baaa35ad
ZJS
176 if (dhcp_packet_checksum((uint8_t*)&packet->ip, hdrlen))
177 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL),
178 "ignoring packet: invalid IP checksum");
2ad7561f 179
55dab2ed 180 if (checksum && packet->udp.check) {
cf597f65
TG
181 packet->ip.check = packet->udp.len;
182 packet->ip.ttl = 0;
183
0bbc2c1f 184 if (dhcp_packet_checksum((uint8_t*)&packet->ip.ttl,
baaa35ad
ZJS
185 be16toh(packet->udp.len) + 12))
186 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL),
187 "ignoring packet: invalid UDP checksum");
cf597f65
TG
188 }
189
cf597f65
TG
190 return 0;
191}