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.h"
34 #include "dhcp-internal.h"
35 #include "sd-dhcp-client.h"
37 #define DHCP_CLIENT_MIN_OPTIONS_SIZE 312
39 int dhcp_message_init(DHCPMessage *message, uint8_t op, uint32_t xid,
40 uint8_t type, uint16_t secs, uint8_t **opt,
44 *opt = (uint8_t *)(message + 1);
51 message->htype = ARPHRD_ETHER;
52 message->hlen = ETHER_ADDR_LEN;
53 message->xid = htobe32(xid);
55 /* Although 'secs' field is a SHOULD in RFC 2131, certain DHCP servers
56 refuse to issue an DHCP lease if 'secs' is set to zero */
57 message->secs = htobe16(secs);
66 err = dhcp_option_append(opt, optlen, DHCP_OPTION_MESSAGE_TYPE, 1,
74 static uint16_t dhcp_checksum(void *buf, int len) {
83 for (i = 0; i < len / 2 ; i++)
92 sum = (sum & 0xffff) + (sum >> 16);
97 void dhcp_packet_append_ip_headers(DHCPPacket *packet, uint8_t op,
99 assert(op == BOOTREQUEST || op == BOOTREPLY);
101 packet->ip.version = IPVERSION;
102 packet->ip.ihl = DHCP_IP_SIZE / 4;
103 packet->ip.tot_len = htobe16(len);
105 packet->ip.protocol = IPPROTO_UDP;
106 packet->ip.saddr = INADDR_ANY;
107 packet->ip.daddr = INADDR_BROADCAST;
111 packet->udp.source = htobe16(DHCP_PORT_CLIENT);
112 packet->udp.dest = htobe16(DHCP_PORT_SERVER);
115 packet->udp.source = htobe16(DHCP_PORT_SERVER);
116 packet->udp.dest = htobe16(DHCP_PORT_CLIENT);
120 packet->udp.len = htobe16(len - DHCP_IP_SIZE);
122 packet->ip.check = packet->udp.len;
123 packet->udp.check = dhcp_checksum(&packet->ip.ttl, len - 8);
125 packet->ip.ttl = IPDEFTTL;
126 packet->ip.check = 0;
127 packet->ip.check = dhcp_checksum(&packet->ip, DHCP_IP_SIZE);
130 int dhcp_packet_verify_headers(DHCPPacket *packet, uint8_t op, size_t len) {
133 assert(op == BOOTREQUEST || op == BOOTREPLY);
135 if (len < (DHCP_IP_UDP_SIZE + DHCP_MESSAGE_SIZE))
138 hdrlen = packet->ip.ihl * 4;
139 if (hdrlen < 20 || hdrlen > len || dhcp_checksum(&packet->ip, hdrlen))
142 if (hdrlen + be16toh(packet->udp.len) > len)
145 if (packet->udp.check) {
146 packet->ip.check = packet->udp.len;
149 if (dhcp_checksum(&packet->ip.ttl,
150 be16toh(packet->udp.len) + 12))
154 if (packet->dhcp.op != op)
159 if (be16toh(packet->udp.source) != DHCP_PORT_CLIENT ||
160 be16toh(packet->udp.dest) != DHCP_PORT_SERVER)
164 if (be16toh(packet->udp.source) != DHCP_PORT_SERVER ||
165 be16toh(packet->udp.dest) != DHCP_PORT_CLIENT)