chiark / gitweb /
install: make InstallContext::{will_install,have_installed} OrderedHashmaps
[elogind.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 <stdlib.h>
22 #include <errno.h>
23 #include <string.h>
24 #include <stdio.h>
25 #include <net/ethernet.h>
26 #include <net/if_arp.h>
27 #include <sys/param.h>
28
29 #include "util.h"
30 #include "list.h"
31
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"
37
38 #define DHCP_CLIENT_MIN_OPTIONS_SIZE            312
39
40 int dhcp_message_init(DHCPMessage *message, uint8_t op, uint32_t xid,
41                       uint8_t type, uint16_t arp_type, size_t optlen,
42                       size_t *optoffset) {
43         size_t offset = 0;
44         int r;
45
46         assert(op == BOOTREQUEST || op == BOOTREPLY);
47         assert(arp_type == ARPHRD_ETHER || arp_type == ARPHRD_INFINIBAND);
48
49         message->op = op;
50         message->htype = arp_type;
51         message->hlen = (arp_type == ARPHRD_ETHER) ? ETHER_ADDR_LEN : 0;
52         message->xid = htobe32(xid);
53         message->magic = htobe32(DHCP_MAGIC_COOKIE);
54
55         r = dhcp_option_append(message, optlen, &offset, 0,
56                                DHCP_OPTION_MESSAGE_TYPE, 1, &type);
57         if (r < 0)
58                 return r;
59
60         *optoffset = offset;
61
62         return 0;
63 }
64
65 uint16_t dhcp_packet_checksum(uint8_t *buf, size_t len) {
66         uint64_t *buf_64 = (uint64_t*)buf;
67         uint64_t *end_64 = buf_64 + (len / sizeof(uint64_t));
68         uint64_t sum = 0;
69
70         /* See RFC1071 */
71
72         while (buf_64 < end_64) {
73                 sum += *buf_64;
74                 if (sum < *buf_64)
75                         /* wrap around in one's complement */
76                         sum++;
77
78                 buf_64 ++;
79         }
80
81         if (len % sizeof(uint64_t)) {
82                 /* If the buffer is not aligned to 64-bit, we need
83                    to zero-pad the last few bytes and add them in */
84                 uint64_t buf_tail = 0;
85
86                 memcpy(&buf_tail, buf_64, len % sizeof(uint64_t));
87
88                 sum += buf_tail;
89                 if (sum < buf_tail)
90                         /* wrap around */
91                         sum++;
92         }
93
94         while (sum >> 16)
95                 sum = (sum & 0xffff) + (sum >> 16);
96
97         return ~sum;
98 }
99
100 void dhcp_packet_append_ip_headers(DHCPPacket *packet, be32_t source_addr,
101                                    uint16_t source_port, be32_t destination_addr,
102                                    uint16_t destination_port, uint16_t len) {
103         packet->ip.version = IPVERSION;
104         packet->ip.ihl = DHCP_IP_SIZE / 4;
105         packet->ip.tot_len = htobe16(len);
106
107         packet->ip.tos = IPTOS_CLASS_CS6;
108
109         packet->ip.protocol = IPPROTO_UDP;
110         packet->ip.saddr = source_addr;
111         packet->ip.daddr = destination_addr;
112
113         packet->udp.source = htobe16(source_port);
114         packet->udp.dest = htobe16(destination_port);
115
116         packet->udp.len = htobe16(len - DHCP_IP_SIZE);
117
118         packet->ip.check = packet->udp.len;
119         packet->udp.check = dhcp_packet_checksum((uint8_t*)&packet->ip.ttl, len - 8);
120
121         packet->ip.ttl = IPDEFTTL;
122         packet->ip.check = 0;
123         packet->ip.check = dhcp_packet_checksum((uint8_t*)&packet->ip, DHCP_IP_SIZE);
124 }
125
126 int dhcp_packet_verify_headers(DHCPPacket *packet, size_t len, bool checksum) {
127         size_t hdrlen;
128
129         assert(packet);
130
131         /* IP */
132
133         if (packet->ip.version != IPVERSION) {
134                 log_debug("ignoring packet: not IPv4");
135                 return -EINVAL;
136         }
137
138         if (packet->ip.ihl < 5) {
139                 log_debug("ignoring packet: IPv4 IHL (%u words) invalid",
140                           packet->ip.ihl);
141                 return -EINVAL;
142         }
143
144         hdrlen = packet->ip.ihl * 4;
145         if (hdrlen < 20) {
146                 log_debug("ignoring packet: IPv4 IHL (%zu bytes) "
147                           "smaller than minimum (20 bytes)", hdrlen);
148                 return -EINVAL;
149         }
150
151         if (len < hdrlen) {
152                 log_debug("ignoring packet: packet (%zu bytes) "
153                           "smaller than expected (%zu) by IP header", len,
154                           hdrlen);
155                 return -EINVAL;
156         }
157
158         /* UDP */
159
160         if (packet->ip.protocol != IPPROTO_UDP) {
161                 log_debug("ignoring packet: not UDP");
162                 return -EINVAL;
163         }
164
165         if (len < hdrlen + be16toh(packet->udp.len)) {
166                 log_debug("ignoring packet: packet (%zu bytes) "
167                           "smaller than expected (%zu) by UDP header", len,
168                           hdrlen + be16toh(packet->udp.len));
169                 return -EINVAL;
170         }
171
172         if (be16toh(packet->udp.dest) != DHCP_PORT_CLIENT) {
173                 log_debug("ignoring packet: to port %u, which "
174                           "is not the DHCP client port (%u)",
175                           be16toh(packet->udp.dest), DHCP_PORT_CLIENT);
176                 return -EINVAL;
177         }
178
179         /* checksums - computing these is relatively expensive, so only do it
180            if all the other checks have passed
181          */
182
183         if (dhcp_packet_checksum((uint8_t*)&packet->ip, hdrlen)) {
184                 log_debug("ignoring packet: invalid IP checksum");
185                 return -EINVAL;
186         }
187
188         if (checksum && packet->udp.check) {
189                 packet->ip.check = packet->udp.len;
190                 packet->ip.ttl = 0;
191
192                 if (dhcp_packet_checksum((uint8_t*)&packet->ip.ttl,
193                                   be16toh(packet->udp.len) + 12)) {
194                         log_debug("ignoring packet: invalid UDP checksum");
195                         return -EINVAL;
196                 }
197         }
198
199         return 0;
200 }