]>
Commit | Line | Data |
---|---|---|
cf597f65 TG |
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" | |
fe8db0c5 | 33 | #include "dhcp-lease-internal.h" |
cf597f65 | 34 | #include "dhcp-internal.h" |
fe8db0c5 | 35 | #include "sd-dhcp-lease.h" |
cf597f65 TG |
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, | |
0a1b6da8 | 41 | uint8_t type, uint8_t **opt, size_t *optlen) { |
cf597f65 TG |
42 | int err; |
43 | ||
0a1b6da8 TG |
44 | assert(op == BOOTREQUEST || op == BOOTREPLY); |
45 | ||
cf597f65 TG |
46 | message->op = op; |
47 | message->htype = ARPHRD_ETHER; | |
48 | message->hlen = ETHER_ADDR_LEN; | |
49 | message->xid = htobe32(xid); | |
3b7ca119 | 50 | message->magic = htobe32(DHCP_MAGIC_COOKIE); |
cf597f65 | 51 | |
3b7ca119 | 52 | *opt = (uint8_t *)(message + 1); |
cf597f65 TG |
53 | |
54 | err = dhcp_option_append(opt, optlen, DHCP_OPTION_MESSAGE_TYPE, 1, | |
55 | &type); | |
56 | if (err < 0) | |
57 | return err; | |
58 | ||
59 | return 0; | |
60 | } | |
61 | ||
a838c939 | 62 | uint16_t dhcp_packet_checksum(void *buf, int len) { |
cf597f65 TG |
63 | uint32_t sum; |
64 | uint16_t *check; | |
65 | int i; | |
66 | uint8_t *odd; | |
67 | ||
68 | sum = 0; | |
69 | check = buf; | |
70 | ||
71 | for (i = 0; i < len / 2 ; i++) | |
72 | sum += check[i]; | |
73 | ||
74 | if (len & 0x01) { | |
75 | odd = buf; | |
76 | sum += odd[len - 1]; | |
77 | } | |
78 | ||
79 | while (sum >> 16) | |
80 | sum = (sum & 0xffff) + (sum >> 16); | |
81 | ||
82 | return ~sum; | |
83 | } | |
84 | ||
63edaa62 TG |
85 | void dhcp_packet_append_ip_headers(DHCPPacket *packet, be32_t source_addr, |
86 | uint16_t source_port, be32_t destination_addr, | |
87 | uint16_t destination_port, uint16_t len) { | |
cf597f65 TG |
88 | packet->ip.version = IPVERSION; |
89 | packet->ip.ihl = DHCP_IP_SIZE / 4; | |
90 | packet->ip.tot_len = htobe16(len); | |
91 | ||
92 | packet->ip.protocol = IPPROTO_UDP; | |
63edaa62 TG |
93 | packet->ip.saddr = source_addr; |
94 | packet->ip.daddr = destination_addr; | |
cf597f65 | 95 | |
63edaa62 TG |
96 | packet->udp.source = htobe16(source_port); |
97 | packet->udp.dest = htobe16(destination_port); | |
cf597f65 TG |
98 | |
99 | packet->udp.len = htobe16(len - DHCP_IP_SIZE); | |
100 | ||
101 | packet->ip.check = packet->udp.len; | |
a838c939 | 102 | packet->udp.check = dhcp_packet_checksum(&packet->ip.ttl, len - 8); |
cf597f65 TG |
103 | |
104 | packet->ip.ttl = IPDEFTTL; | |
105 | packet->ip.check = 0; | |
a838c939 | 106 | packet->ip.check = dhcp_packet_checksum(&packet->ip, DHCP_IP_SIZE); |
cf597f65 TG |
107 | } |
108 | ||
55dab2ed | 109 | int dhcp_packet_verify_headers(DHCPPacket *packet, size_t len, bool checksum) { |
cf597f65 TG |
110 | size_t hdrlen; |
111 | ||
5266a81e TG |
112 | assert(packet); |
113 | ||
06b44be7 | 114 | /* IP */ |
cf597f65 | 115 | |
6e34949d TG |
116 | if (packet->ip.version != IPVERSION) { |
117 | log_dhcp_client(client, "ignoring packet: not IPv4"); | |
118 | return -EINVAL; | |
119 | } | |
120 | ||
06b44be7 TG |
121 | if (packet->ip.ihl < 5) { |
122 | log_dhcp_client(client, "ignoring packet: IPv4 IHL (%u words) invalid", | |
123 | packet->ip.ihl); | |
cf597f65 | 124 | return -EINVAL; |
ac4f16ab | 125 | } |
cf597f65 TG |
126 | |
127 | hdrlen = packet->ip.ihl * 4; | |
06b44be7 TG |
128 | if (hdrlen < 20) { |
129 | log_dhcp_client(client, "ignoring packet: IPv4 IHL (%zu bytes) " | |
130 | "smaller than minimum (20 bytes)", hdrlen); | |
131 | return -EINVAL; | |
132 | } | |
133 | ||
134 | if (len < hdrlen) { | |
135 | log_dhcp_client(client, "ignoring packet: packet (%zu bytes) " | |
136 | "smaller than expected (%zu) by IP header", len, | |
137 | hdrlen); | |
cf597f65 | 138 | return -EINVAL; |
ac4f16ab | 139 | } |
cf597f65 | 140 | |
06b44be7 TG |
141 | /* UDP */ |
142 | ||
d454a674 UTL |
143 | if (packet->ip.protocol != IPPROTO_UDP) { |
144 | log_dhcp_client(client, "ignoring packet: not UDP"); | |
145 | return -EINVAL; | |
146 | } | |
147 | ||
8fa2eeac | 148 | if (len < hdrlen + be16toh(packet->udp.len)) { |
06b44be7 TG |
149 | log_dhcp_client(client, "ignoring packet: packet (%zu bytes) " |
150 | "smaller than expected (%zu) by UDP header", len, | |
151 | hdrlen + be16toh(packet->udp.len)); | |
ac4f16ab TG |
152 | return -EINVAL; |
153 | } | |
cf597f65 | 154 | |
2ad7561f TG |
155 | if (be16toh(packet->udp.dest) != DHCP_PORT_CLIENT) { |
156 | log_dhcp_client(client, "ignoring packet: to port %u, which " | |
157 | "is not the DHCP client port (%u)", | |
158 | be16toh(packet->udp.dest), DHCP_PORT_CLIENT); | |
159 | return -EINVAL; | |
160 | } | |
161 | ||
162 | /* checksums - computing these is relatively expensive, so only do it | |
163 | if all the other checks have passed | |
164 | */ | |
165 | ||
166 | if (dhcp_packet_checksum(&packet->ip, hdrlen)) { | |
167 | log_dhcp_client(client, "ignoring packet: invalid IP checksum"); | |
168 | return -EINVAL; | |
169 | } | |
170 | ||
55dab2ed | 171 | if (checksum && packet->udp.check) { |
cf597f65 TG |
172 | packet->ip.check = packet->udp.len; |
173 | packet->ip.ttl = 0; | |
174 | ||
a838c939 | 175 | if (dhcp_packet_checksum(&packet->ip.ttl, |
ac4f16ab | 176 | be16toh(packet->udp.len) + 12)) { |
06b44be7 | 177 | log_dhcp_client(client, "ignoring packet: invalid UDP checksum"); |
cf597f65 | 178 | return -EINVAL; |
ac4f16ab | 179 | } |
cf597f65 TG |
180 | } |
181 | ||
cf597f65 TG |
182 | return 0; |
183 | } |