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