]> git.ipfire.org Git - thirdparty/systemd.git/blame_incremental - src/libsystemd-network/dhcp-packet.c
tree-wide: use proper unicode © instead of (C) where we can
[thirdparty/systemd.git] / src / libsystemd-network / dhcp-packet.c
... / ...
CommitLineData
1/* SPDX-License-Identifier: LGPL-2.1+ */
2/***
3 Copyright © 2013 Intel Corporation. All rights reserved.
4 Copyright © 2014 Tom Gundersen
5***/
6
7#include <errno.h>
8#include <net/ethernet.h>
9#include <net/if_arp.h>
10#include <string.h>
11
12#include "dhcp-internal.h"
13#include "dhcp-protocol.h"
14
15#define DHCP_CLIENT_MIN_OPTIONS_SIZE 312
16
17int dhcp_message_init(DHCPMessage *message, uint8_t op, uint32_t xid,
18 uint8_t type, uint16_t arp_type, size_t optlen,
19 size_t *optoffset) {
20 size_t offset = 0;
21 int r;
22
23 assert(IN_SET(op, BOOTREQUEST, BOOTREPLY));
24 assert(IN_SET(arp_type, ARPHRD_ETHER, ARPHRD_INFINIBAND));
25
26 message->op = op;
27 message->htype = arp_type;
28 message->hlen = (arp_type == ARPHRD_ETHER) ? ETHER_ADDR_LEN : 0;
29 message->xid = htobe32(xid);
30 message->magic = htobe32(DHCP_MAGIC_COOKIE);
31
32 r = dhcp_option_append(message, optlen, &offset, 0,
33 SD_DHCP_OPTION_MESSAGE_TYPE, 1, &type);
34 if (r < 0)
35 return r;
36
37 *optoffset = offset;
38
39 return 0;
40}
41
42uint16_t dhcp_packet_checksum(uint8_t *buf, size_t len) {
43 uint64_t *buf_64 = (uint64_t*)buf;
44 uint64_t *end_64 = buf_64 + (len / sizeof(uint64_t));
45 uint64_t sum = 0;
46
47 /* See RFC1071 */
48
49 while (buf_64 < end_64) {
50 sum += *buf_64;
51 if (sum < *buf_64)
52 /* wrap around in one's complement */
53 sum++;
54
55 buf_64++;
56 }
57
58 if (len % sizeof(uint64_t)) {
59 /* If the buffer is not aligned to 64-bit, we need
60 to zero-pad the last few bytes and add them in */
61 uint64_t buf_tail = 0;
62
63 memcpy(&buf_tail, buf_64, len % sizeof(uint64_t));
64
65 sum += buf_tail;
66 if (sum < buf_tail)
67 /* wrap around */
68 sum++;
69 }
70
71 while (sum >> 16)
72 sum = (sum & 0xffff) + (sum >> 16);
73
74 return ~sum;
75}
76
77void dhcp_packet_append_ip_headers(DHCPPacket *packet, be32_t source_addr,
78 uint16_t source_port, be32_t destination_addr,
79 uint16_t destination_port, uint16_t len) {
80 packet->ip.version = IPVERSION;
81 packet->ip.ihl = DHCP_IP_SIZE / 4;
82 packet->ip.tot_len = htobe16(len);
83
84 packet->ip.tos = IPTOS_CLASS_CS6;
85
86 packet->ip.protocol = IPPROTO_UDP;
87 packet->ip.saddr = source_addr;
88 packet->ip.daddr = destination_addr;
89
90 packet->udp.source = htobe16(source_port);
91 packet->udp.dest = htobe16(destination_port);
92
93 packet->udp.len = htobe16(len - DHCP_IP_SIZE);
94
95 packet->ip.check = packet->udp.len;
96 packet->udp.check = dhcp_packet_checksum((uint8_t*)&packet->ip.ttl, len - 8);
97
98 packet->ip.ttl = IPDEFTTL;
99 packet->ip.check = 0;
100 packet->ip.check = dhcp_packet_checksum((uint8_t*)&packet->ip, DHCP_IP_SIZE);
101}
102
103int dhcp_packet_verify_headers(DHCPPacket *packet, size_t len, bool checksum, uint16_t port) {
104 size_t hdrlen;
105
106 assert(packet);
107
108 /* IP */
109
110 if (packet->ip.version != IPVERSION) {
111 log_debug("ignoring packet: not IPv4");
112 return -EINVAL;
113 }
114
115 if (packet->ip.ihl < 5) {
116 log_debug("ignoring packet: IPv4 IHL (%u words) invalid",
117 packet->ip.ihl);
118 return -EINVAL;
119 }
120
121 hdrlen = packet->ip.ihl * 4;
122 if (hdrlen < 20) {
123 log_debug("ignoring packet: IPv4 IHL (%zu bytes) "
124 "smaller than minimum (20 bytes)", hdrlen);
125 return -EINVAL;
126 }
127
128 if (len < hdrlen) {
129 log_debug("ignoring packet: packet (%zu bytes) "
130 "smaller than expected (%zu) by IP header", len,
131 hdrlen);
132 return -EINVAL;
133 }
134
135 /* UDP */
136
137 if (packet->ip.protocol != IPPROTO_UDP) {
138 log_debug("ignoring packet: not UDP");
139 return -EINVAL;
140 }
141
142 if (len < hdrlen + be16toh(packet->udp.len)) {
143 log_debug("ignoring packet: packet (%zu bytes) "
144 "smaller than expected (%zu) by UDP header", len,
145 hdrlen + be16toh(packet->udp.len));
146 return -EINVAL;
147 }
148
149 if (be16toh(packet->udp.dest) != port) {
150 log_debug("ignoring packet: to port %u, which "
151 "is not the DHCP client port (%u)",
152 be16toh(packet->udp.dest), port);
153 return -EINVAL;
154 }
155
156 /* checksums - computing these is relatively expensive, so only do it
157 if all the other checks have passed
158 */
159
160 if (dhcp_packet_checksum((uint8_t*)&packet->ip, hdrlen)) {
161 log_debug("ignoring packet: invalid IP checksum");
162 return -EINVAL;
163 }
164
165 if (checksum && packet->udp.check) {
166 packet->ip.check = packet->udp.len;
167 packet->ip.ttl = 0;
168
169 if (dhcp_packet_checksum((uint8_t*)&packet->ip.ttl,
170 be16toh(packet->udp.len) + 12)) {
171 log_debug("ignoring packet: invalid UDP checksum");
172 return -EINVAL;
173 }
174 }
175
176 return 0;
177}