]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/libsystemd-network/dhcp-packet.c
Merge pull request #2717 from keszybz/networkctl-prettification
[thirdparty/systemd.git] / src / libsystemd-network / dhcp-packet.c
1 /***
2 This file is part of systemd.
3
4 Copyright (C) 2013 Intel Corporation. All rights reserved.
5 Copyright (C) 2014 Tom Gundersen
6
7 systemd is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version.
11
12 systemd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
19 ***/
20
21 #include <errno.h>
22 #include <net/ethernet.h>
23 #include <net/if_arp.h>
24 #include <string.h>
25
26 #include "dhcp-internal.h"
27 #include "dhcp-protocol.h"
28
29 #define DHCP_CLIENT_MIN_OPTIONS_SIZE 312
30
31 int dhcp_message_init(DHCPMessage *message, uint8_t op, uint32_t xid,
32 uint8_t type, uint16_t arp_type, size_t optlen,
33 size_t *optoffset) {
34 size_t offset = 0;
35 int r;
36
37 assert(op == BOOTREQUEST || op == BOOTREPLY);
38 assert(arp_type == ARPHRD_ETHER || arp_type == ARPHRD_INFINIBAND);
39
40 message->op = op;
41 message->htype = arp_type;
42 message->hlen = (arp_type == ARPHRD_ETHER) ? ETHER_ADDR_LEN : 0;
43 message->xid = htobe32(xid);
44 message->magic = htobe32(DHCP_MAGIC_COOKIE);
45
46 r = dhcp_option_append(message, optlen, &offset, 0,
47 SD_DHCP_OPTION_MESSAGE_TYPE, 1, &type);
48 if (r < 0)
49 return r;
50
51 *optoffset = offset;
52
53 return 0;
54 }
55
56 uint16_t dhcp_packet_checksum(uint8_t *buf, size_t len) {
57 uint64_t *buf_64 = (uint64_t*)buf;
58 uint64_t *end_64 = buf_64 + (len / sizeof(uint64_t));
59 uint64_t sum = 0;
60
61 /* See RFC1071 */
62
63 while (buf_64 < end_64) {
64 sum += *buf_64;
65 if (sum < *buf_64)
66 /* wrap around in one's complement */
67 sum++;
68
69 buf_64++;
70 }
71
72 if (len % sizeof(uint64_t)) {
73 /* If the buffer is not aligned to 64-bit, we need
74 to zero-pad the last few bytes and add them in */
75 uint64_t buf_tail = 0;
76
77 memcpy(&buf_tail, buf_64, len % sizeof(uint64_t));
78
79 sum += buf_tail;
80 if (sum < buf_tail)
81 /* wrap around */
82 sum++;
83 }
84
85 while (sum >> 16)
86 sum = (sum & 0xffff) + (sum >> 16);
87
88 return ~sum;
89 }
90
91 void dhcp_packet_append_ip_headers(DHCPPacket *packet, be32_t source_addr,
92 uint16_t source_port, be32_t destination_addr,
93 uint16_t destination_port, uint16_t len) {
94 packet->ip.version = IPVERSION;
95 packet->ip.ihl = DHCP_IP_SIZE / 4;
96 packet->ip.tot_len = htobe16(len);
97
98 packet->ip.tos = IPTOS_CLASS_CS6;
99
100 packet->ip.protocol = IPPROTO_UDP;
101 packet->ip.saddr = source_addr;
102 packet->ip.daddr = destination_addr;
103
104 packet->udp.source = htobe16(source_port);
105 packet->udp.dest = htobe16(destination_port);
106
107 packet->udp.len = htobe16(len - DHCP_IP_SIZE);
108
109 packet->ip.check = packet->udp.len;
110 packet->udp.check = dhcp_packet_checksum((uint8_t*)&packet->ip.ttl, len - 8);
111
112 packet->ip.ttl = IPDEFTTL;
113 packet->ip.check = 0;
114 packet->ip.check = dhcp_packet_checksum((uint8_t*)&packet->ip, DHCP_IP_SIZE);
115 }
116
117 int dhcp_packet_verify_headers(DHCPPacket *packet, size_t len, bool checksum) {
118 size_t hdrlen;
119
120 assert(packet);
121
122 /* IP */
123
124 if (packet->ip.version != IPVERSION) {
125 log_debug("ignoring packet: not IPv4");
126 return -EINVAL;
127 }
128
129 if (packet->ip.ihl < 5) {
130 log_debug("ignoring packet: IPv4 IHL (%u words) invalid",
131 packet->ip.ihl);
132 return -EINVAL;
133 }
134
135 hdrlen = packet->ip.ihl * 4;
136 if (hdrlen < 20) {
137 log_debug("ignoring packet: IPv4 IHL (%zu bytes) "
138 "smaller than minimum (20 bytes)", hdrlen);
139 return -EINVAL;
140 }
141
142 if (len < hdrlen) {
143 log_debug("ignoring packet: packet (%zu bytes) "
144 "smaller than expected (%zu) by IP header", len,
145 hdrlen);
146 return -EINVAL;
147 }
148
149 /* UDP */
150
151 if (packet->ip.protocol != IPPROTO_UDP) {
152 log_debug("ignoring packet: not UDP");
153 return -EINVAL;
154 }
155
156 if (len < hdrlen + be16toh(packet->udp.len)) {
157 log_debug("ignoring packet: packet (%zu bytes) "
158 "smaller than expected (%zu) by UDP header", len,
159 hdrlen + be16toh(packet->udp.len));
160 return -EINVAL;
161 }
162
163 if (be16toh(packet->udp.dest) != DHCP_PORT_CLIENT) {
164 log_debug("ignoring packet: to port %u, which "
165 "is not the DHCP client port (%u)",
166 be16toh(packet->udp.dest), DHCP_PORT_CLIENT);
167 return -EINVAL;
168 }
169
170 /* checksums - computing these is relatively expensive, so only do it
171 if all the other checks have passed
172 */
173
174 if (dhcp_packet_checksum((uint8_t*)&packet->ip, hdrlen)) {
175 log_debug("ignoring packet: invalid IP checksum");
176 return -EINVAL;
177 }
178
179 if (checksum && packet->udp.check) {
180 packet->ip.check = packet->udp.len;
181 packet->ip.ttl = 0;
182
183 if (dhcp_packet_checksum((uint8_t*)&packet->ip.ttl,
184 be16toh(packet->udp.len) + 12)) {
185 log_debug("ignoring packet: invalid UDP checksum");
186 return -EINVAL;
187 }
188 }
189
190 return 0;
191 }