]>
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" | |
33 | #include "dhcp-lease.h" | |
34 | #include "dhcp-internal.h" | |
35 | #include "sd-dhcp-client.h" | |
36 | ||
37 | #define DHCP_CLIENT_MIN_OPTIONS_SIZE 312 | |
38 | ||
39 | int dhcp_message_init(DHCPMessage *message, uint8_t op, uint32_t xid, | |
40 | uint8_t type, uint16_t secs, uint8_t **opt, | |
41 | size_t *optlen) { | |
42 | int err; | |
43 | ||
44 | *opt = (uint8_t *)(message + 1); | |
45 | ||
46 | if (*optlen < 4) | |
47 | return -ENOBUFS; | |
48 | *optlen -= 4; | |
49 | ||
50 | message->op = op; | |
51 | message->htype = ARPHRD_ETHER; | |
52 | message->hlen = ETHER_ADDR_LEN; | |
53 | message->xid = htobe32(xid); | |
54 | ||
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); | |
58 | ||
59 | (*opt)[0] = 0x63; | |
60 | (*opt)[1] = 0x82; | |
61 | (*opt)[2] = 0x53; | |
62 | (*opt)[3] = 0x63; | |
63 | ||
64 | *opt += 4; | |
65 | ||
66 | err = dhcp_option_append(opt, optlen, DHCP_OPTION_MESSAGE_TYPE, 1, | |
67 | &type); | |
68 | if (err < 0) | |
69 | return err; | |
70 | ||
71 | return 0; | |
72 | } | |
73 | ||
74 | static uint16_t dhcp_checksum(void *buf, int len) { | |
75 | uint32_t sum; | |
76 | uint16_t *check; | |
77 | int i; | |
78 | uint8_t *odd; | |
79 | ||
80 | sum = 0; | |
81 | check = buf; | |
82 | ||
83 | for (i = 0; i < len / 2 ; i++) | |
84 | sum += check[i]; | |
85 | ||
86 | if (len & 0x01) { | |
87 | odd = buf; | |
88 | sum += odd[len - 1]; | |
89 | } | |
90 | ||
91 | while (sum >> 16) | |
92 | sum = (sum & 0xffff) + (sum >> 16); | |
93 | ||
94 | return ~sum; | |
95 | } | |
96 | ||
06b44be7 | 97 | void dhcp_packet_append_ip_headers(DHCPPacket *packet, 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; | |
103 | packet->ip.saddr = INADDR_ANY; | |
104 | packet->ip.daddr = INADDR_BROADCAST; | |
105 | ||
06b44be7 TG |
106 | packet->udp.source = htobe16(DHCP_PORT_CLIENT); |
107 | packet->udp.dest = htobe16(DHCP_PORT_SERVER); | |
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 | ||
06b44be7 | 119 | int dhcp_packet_verify_headers(DHCPPacket *packet, size_t len) { |
cf597f65 TG |
120 | size_t hdrlen; |
121 | ||
06b44be7 | 122 | /* IP */ |
cf597f65 | 123 | |
06b44be7 TG |
124 | if (len < DHCP_IP_SIZE) { |
125 | log_dhcp_client(client, "ignoring packet: packet (%zu bytes) " | |
126 | " smaller than IP header (%u bytes)", len, | |
127 | DHCP_IP_SIZE); | |
128 | return -EINVAL; | |
129 | } | |
130 | ||
131 | if (packet->ip.ihl < 5) { | |
132 | log_dhcp_client(client, "ignoring packet: IPv4 IHL (%u words) invalid", | |
133 | packet->ip.ihl); | |
cf597f65 | 134 | return -EINVAL; |
ac4f16ab | 135 | } |
cf597f65 TG |
136 | |
137 | hdrlen = packet->ip.ihl * 4; | |
06b44be7 TG |
138 | if (hdrlen < 20) { |
139 | log_dhcp_client(client, "ignoring packet: IPv4 IHL (%zu bytes) " | |
140 | "smaller than minimum (20 bytes)", hdrlen); | |
141 | return -EINVAL; | |
142 | } | |
143 | ||
144 | if (len < hdrlen) { | |
145 | log_dhcp_client(client, "ignoring packet: packet (%zu bytes) " | |
146 | "smaller than expected (%zu) by IP header", len, | |
147 | hdrlen); | |
cf597f65 | 148 | return -EINVAL; |
ac4f16ab | 149 | } |
cf597f65 | 150 | |
ac4f16ab | 151 | if (dhcp_checksum(&packet->ip, hdrlen)) { |
06b44be7 TG |
152 | log_dhcp_client(client, "ignoring packet: invalid IP checksum"); |
153 | return -EINVAL; | |
154 | } | |
155 | ||
156 | /* UDP */ | |
157 | ||
158 | if (len < DHCP_IP_UDP_SIZE) { | |
159 | log_dhcp_client(client, "ignoring packet: packet (%zu bytes) " | |
160 | " smaller than IP+UDP header (%u bytes)", len, | |
161 | DHCP_IP_UDP_SIZE); | |
cf597f65 | 162 | return -EINVAL; |
ac4f16ab TG |
163 | } |
164 | ||
165 | if (hdrlen + be16toh(packet->udp.len) > len) { | |
06b44be7 TG |
166 | log_dhcp_client(client, "ignoring packet: packet (%zu bytes) " |
167 | "smaller than expected (%zu) by UDP header", len, | |
168 | hdrlen + be16toh(packet->udp.len)); | |
ac4f16ab TG |
169 | return -EINVAL; |
170 | } | |
cf597f65 TG |
171 | |
172 | if (packet->udp.check) { | |
173 | packet->ip.check = packet->udp.len; | |
174 | packet->ip.ttl = 0; | |
175 | ||
176 | if (dhcp_checksum(&packet->ip.ttl, | |
ac4f16ab | 177 | be16toh(packet->udp.len) + 12)) { |
06b44be7 | 178 | log_dhcp_client(client, "ignoring packet: invalid UDP checksum"); |
cf597f65 | 179 | return -EINVAL; |
ac4f16ab | 180 | } |
cf597f65 TG |
181 | } |
182 | ||
06b44be7 TG |
183 | if (be16toh(packet->udp.source) != DHCP_PORT_SERVER || |
184 | be16toh(packet->udp.dest) != DHCP_PORT_CLIENT) { | |
185 | log_dhcp_client(client, "ignoring packet: wrong ports (source: %u, destination: %u)", | |
186 | be16toh(packet->udp.source), be16toh(packet->udp.dest)); | |
cf597f65 | 187 | return -EINVAL; |
ac4f16ab | 188 | } |
cf597f65 | 189 | |
cf597f65 TG |
190 | return 0; |
191 | } |