]>
Commit | Line | Data |
---|---|---|
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 <net/if_arp.h> |
cf0fbc49 | 7 | #include <string.h> |
cf597f65 | 8 | |
8664ded7 YW |
9 | #include "dhcp-option.h" |
10 | #include "dhcp-packet.h" | |
93a1f792 | 11 | #include "log.h" |
97fa338d | 12 | #include "memory-util.h" |
cf597f65 TG |
13 | |
14 | #define DHCP_CLIENT_MIN_OPTIONS_SIZE 312 | |
15 | ||
4ad29bad | 16 | int bootp_message_init( |
97fa338d YW |
17 | DHCPMessage *message, |
18 | uint8_t op, | |
19 | uint32_t xid, | |
97fa338d YW |
20 | uint16_t arp_type, |
21 | uint8_t hlen, | |
4ad29bad | 22 | const uint8_t *chaddr) { |
cf597f65 | 23 | |
556f641f | 24 | assert(message); |
3742095b | 25 | assert(IN_SET(op, BOOTREQUEST, BOOTREPLY)); |
97fa338d | 26 | assert(chaddr || hlen == 0); |
0a1b6da8 | 27 | |
cf597f65 | 28 | message->op = op; |
76253e73 | 29 | message->htype = arp_type; |
97fa338d YW |
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 | */ | |
0b6a4795 | 42 | message->hlen = arp_type == ARPHRD_INFINIBAND ? 0 : hlen; |
97fa338d YW |
43 | memcpy_safe(message->chaddr, chaddr, message->hlen); |
44 | ||
cf597f65 | 45 | message->xid = htobe32(xid); |
3b7ca119 | 46 | message->magic = htobe32(DHCP_MAGIC_COOKIE); |
cf597f65 | 47 | |
4ad29bad CF |
48 | return 0; |
49 | } | |
50 | ||
51 | int 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 | ||
04b28be1 | 73 | r = dhcp_option_append(message, optlen, &offset, 0, |
22805d92 | 74 | SD_DHCP_OPTION_MESSAGE_TYPE, 1, &type); |
20b958bf TG |
75 | if (r < 0) |
76 | return r; | |
cf597f65 | 77 | |
556f641f | 78 | *ret_optoffset = offset; |
cf597f65 TG |
79 | return 0; |
80 | } | |
81 | ||
0bbc2c1f TG |
82 | uint16_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)); | |
d5761274 TG |
85 | uint64_t sum = 0; |
86 | ||
0bbc2c1f TG |
87 | /* See RFC1071 */ |
88 | ||
d5761274 TG |
89 | while (buf_64 < end_64) { |
90 | sum += *buf_64; | |
91 | if (sum < *buf_64) | |
0bbc2c1f | 92 | /* wrap around in one's complement */ |
d5761274 TG |
93 | sum++; |
94 | ||
313cefa1 | 95 | buf_64++; |
d5761274 TG |
96 | } |
97 | ||
0bbc2c1f TG |
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; | |
cf597f65 | 102 | |
0bbc2c1f | 103 | memcpy(&buf_tail, buf_64, len % sizeof(uint64_t)); |
cf597f65 | 104 | |
0bbc2c1f TG |
105 | sum += buf_tail; |
106 | if (sum < buf_tail) | |
107 | /* wrap around */ | |
d5761274 | 108 | sum++; |
cf597f65 TG |
109 | } |
110 | ||
111 | while (sum >> 16) | |
112 | sum = (sum & 0xffff) + (sum >> 16); | |
113 | ||
114 | return ~sum; | |
115 | } | |
116 | ||
63edaa62 TG |
117 | void dhcp_packet_append_ip_headers(DHCPPacket *packet, be32_t source_addr, |
118 | uint16_t source_port, be32_t destination_addr, | |
afe42aef | 119 | uint16_t destination_port, uint16_t len, int ip_service_type) { |
cf597f65 TG |
120 | packet->ip.version = IPVERSION; |
121 | packet->ip.ihl = DHCP_IP_SIZE / 4; | |
122 | packet->ip.tot_len = htobe16(len); | |
123 | ||
afe42aef SC |
124 | if (ip_service_type >= 0) |
125 | packet->ip.tos = ip_service_type; | |
126 | else | |
127 | packet->ip.tos = IPTOS_CLASS_CS6; | |
85923f79 | 128 | |
cf597f65 | 129 | packet->ip.protocol = IPPROTO_UDP; |
63edaa62 TG |
130 | packet->ip.saddr = source_addr; |
131 | packet->ip.daddr = destination_addr; | |
cf597f65 | 132 | |
63edaa62 TG |
133 | packet->udp.source = htobe16(source_port); |
134 | packet->udp.dest = htobe16(destination_port); | |
cf597f65 TG |
135 | |
136 | packet->udp.len = htobe16(len - DHCP_IP_SIZE); | |
137 | ||
138 | packet->ip.check = packet->udp.len; | |
0bbc2c1f | 139 | packet->udp.check = dhcp_packet_checksum((uint8_t*)&packet->ip.ttl, len - 8); |
cf597f65 TG |
140 | |
141 | packet->ip.ttl = IPDEFTTL; | |
142 | packet->ip.check = 0; | |
0bbc2c1f | 143 | packet->ip.check = dhcp_packet_checksum((uint8_t*)&packet->ip, DHCP_IP_SIZE); |
cf597f65 TG |
144 | } |
145 | ||
9faed222 | 146 | int dhcp_packet_verify_headers(DHCPPacket *packet, size_t len, bool checksum, uint16_t port) { |
cf597f65 TG |
147 | size_t hdrlen; |
148 | ||
5266a81e | 149 | assert(packet); |
a57dd1d1 YW |
150 | |
151 | if (len < sizeof(DHCPPacket)) | |
152 | return 0; | |
5266a81e | 153 | |
06b44be7 | 154 | /* IP */ |
cf597f65 | 155 | |
baaa35ad ZJS |
156 | if (packet->ip.version != IPVERSION) |
157 | return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), | |
158 | "ignoring packet: not IPv4"); | |
6e34949d | 159 | |
baaa35ad ZJS |
160 | if (packet->ip.ihl < 5) |
161 | return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), | |
c0f86d66 | 162 | "ignoring packet: IPv4 IHL (%i words) invalid", |
baaa35ad | 163 | packet->ip.ihl); |
cf597f65 TG |
164 | |
165 | hdrlen = packet->ip.ihl * 4; | |
baaa35ad ZJS |
166 | if (hdrlen < 20) |
167 | return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), | |
9a94baa6 | 168 | "ignoring packet: IPv4 IHL (%zu bytes) smaller than minimum (20 bytes)", |
baaa35ad ZJS |
169 | hdrlen); |
170 | ||
171 | if (len < hdrlen) | |
172 | return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), | |
9a94baa6 | 173 | "ignoring packet: packet (%zu bytes) smaller than expected (%zu) by IP header", |
baaa35ad | 174 | len, hdrlen); |
cf597f65 | 175 | |
06b44be7 TG |
176 | /* UDP */ |
177 | ||
baaa35ad ZJS |
178 | if (packet->ip.protocol != IPPROTO_UDP) |
179 | return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), | |
180 | "ignoring packet: not UDP"); | |
d454a674 | 181 | |
baaa35ad ZJS |
182 | if (len < hdrlen + be16toh(packet->udp.len)) |
183 | return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), | |
9a94baa6 | 184 | "ignoring packet: packet (%zu bytes) smaller than expected (%zu) by UDP header", |
baaa35ad | 185 | len, hdrlen + be16toh(packet->udp.len)); |
cf597f65 | 186 | |
baaa35ad ZJS |
187 | if (be16toh(packet->udp.dest) != port) |
188 | return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), | |
9a94baa6 | 189 | "ignoring packet: to port %u, which is not the DHCP client port (%u)", |
baaa35ad | 190 | be16toh(packet->udp.dest), port); |
2ad7561f TG |
191 | |
192 | /* checksums - computing these is relatively expensive, so only do it | |
193 | if all the other checks have passed | |
194 | */ | |
195 | ||
baaa35ad ZJS |
196 | if (dhcp_packet_checksum((uint8_t*)&packet->ip, hdrlen)) |
197 | return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), | |
198 | "ignoring packet: invalid IP checksum"); | |
2ad7561f | 199 | |
55dab2ed | 200 | if (checksum && packet->udp.check) { |
cf597f65 TG |
201 | packet->ip.check = packet->udp.len; |
202 | packet->ip.ttl = 0; | |
203 | ||
0bbc2c1f | 204 | if (dhcp_packet_checksum((uint8_t*)&packet->ip.ttl, |
baaa35ad ZJS |
205 | be16toh(packet->udp.len) + 12)) |
206 | return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), | |
207 | "ignoring packet: invalid UDP checksum"); | |
cf597f65 TG |
208 | } |
209 | ||
cf597f65 TG |
210 | return 0; |
211 | } |