2 This file is part of systemd.
4 Copyright (C) 2013 Intel Corporation. All rights reserved.
5 Copyright (C) 2014 Tom Gundersen
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.
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.
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/>.
25 #include <net/ethernet.h>
26 #include <net/if_arp.h>
27 #include <sys/param.h>
32 #include "dhcp-protocol.h"
33 #include "dhcp-lease-internal.h"
34 #include "dhcp-internal.h"
35 #include "sd-dhcp-lease.h"
36 #include "sd-dhcp-client.h"
38 #define DHCP_CLIENT_MIN_OPTIONS_SIZE 312
40 int dhcp_message_init(DHCPMessage *message, uint8_t op, uint32_t xid,
41 uint8_t type, uint8_t **opt, size_t *optlen) {
44 assert(op == BOOTREQUEST || op == BOOTREPLY);
46 *opt = (uint8_t *)(message + 1);
53 message->htype = ARPHRD_ETHER;
54 message->hlen = ETHER_ADDR_LEN;
55 message->xid = htobe32(xid);
64 err = dhcp_option_append(opt, optlen, DHCP_OPTION_MESSAGE_TYPE, 1,
72 static uint16_t dhcp_checksum(void *buf, int len) {
81 for (i = 0; i < len / 2 ; i++)
90 sum = (sum & 0xffff) + (sum >> 16);
95 void dhcp_packet_append_ip_headers(DHCPPacket *packet, uint16_t len) {
96 packet->ip.version = IPVERSION;
97 packet->ip.ihl = DHCP_IP_SIZE / 4;
98 packet->ip.tot_len = htobe16(len);
100 packet->ip.protocol = IPPROTO_UDP;
101 packet->ip.saddr = INADDR_ANY;
102 packet->ip.daddr = INADDR_BROADCAST;
104 packet->udp.source = htobe16(DHCP_PORT_CLIENT);
105 packet->udp.dest = htobe16(DHCP_PORT_SERVER);
107 packet->udp.len = htobe16(len - DHCP_IP_SIZE);
109 packet->ip.check = packet->udp.len;
110 packet->udp.check = dhcp_checksum(&packet->ip.ttl, len - 8);
112 packet->ip.ttl = IPDEFTTL;
113 packet->ip.check = 0;
114 packet->ip.check = dhcp_checksum(&packet->ip, DHCP_IP_SIZE);
117 int dhcp_packet_verify_headers(DHCPPacket *packet, size_t len, bool checksum) {
124 if (len < DHCP_IP_SIZE) {
125 log_dhcp_client(client, "ignoring packet: packet (%zu bytes) "
126 " smaller than IP header (%u bytes)", len,
131 if (packet->ip.ihl < 5) {
132 log_dhcp_client(client, "ignoring packet: IPv4 IHL (%u words) invalid",
137 hdrlen = packet->ip.ihl * 4;
139 log_dhcp_client(client, "ignoring packet: IPv4 IHL (%zu bytes) "
140 "smaller than minimum (20 bytes)", hdrlen);
145 log_dhcp_client(client, "ignoring packet: packet (%zu bytes) "
146 "smaller than expected (%zu) by IP header", len,
151 if (dhcp_checksum(&packet->ip, hdrlen)) {
152 log_dhcp_client(client, "ignoring packet: invalid IP checksum");
158 if (len < DHCP_IP_UDP_SIZE) {
159 log_dhcp_client(client, "ignoring packet: packet (%zu bytes) "
160 " smaller than IP+UDP header (%u bytes)", len,
165 if (len < hdrlen + be16toh(packet->udp.len)) {
166 log_dhcp_client(client, "ignoring packet: packet (%zu bytes) "
167 "smaller than expected (%zu) by UDP header", len,
168 hdrlen + be16toh(packet->udp.len));
172 if (checksum && packet->udp.check) {
173 packet->ip.check = packet->udp.len;
176 if (dhcp_checksum(&packet->ip.ttl,
177 be16toh(packet->udp.len) + 12)) {
178 log_dhcp_client(client, "ignoring packet: invalid UDP checksum");
183 if (be16toh(packet->udp.dest) != DHCP_PORT_CLIENT) {
184 log_dhcp_client(client, "ignoring packet: to port %u, which "
185 "is not the DHCP client port (%u)",
186 be16toh(packet->udp.dest), DHCP_PORT_CLIENT);