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