BadVPN – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | /** |
2 | * @file BDHCPClient.c |
||
3 | * @author Ambroz Bizjak <ambrop7@gmail.com> |
||
4 | * |
||
5 | * @section LICENSE |
||
6 | * |
||
7 | * Redistribution and use in source and binary forms, with or without |
||
8 | * modification, are permitted provided that the following conditions are met: |
||
9 | * 1. Redistributions of source code must retain the above copyright |
||
10 | * notice, this list of conditions and the following disclaimer. |
||
11 | * 2. Redistributions in binary form must reproduce the above copyright |
||
12 | * notice, this list of conditions and the following disclaimer in the |
||
13 | * documentation and/or other materials provided with the distribution. |
||
14 | * 3. Neither the name of the author nor the |
||
15 | * names of its contributors may be used to endorse or promote products |
||
16 | * derived from this software without specific prior written permission. |
||
17 | * |
||
18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
||
19 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
||
20 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
||
21 | * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
||
22 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
||
23 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
||
24 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
||
25 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||
26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
||
27 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||
28 | */ |
||
29 | |||
30 | #include <stdio.h> |
||
31 | #include <string.h> |
||
32 | #include <unistd.h> |
||
33 | #include <sys/socket.h> |
||
34 | #include <net/if.h> |
||
35 | #include <net/if_arp.h> |
||
36 | #include <sys/ioctl.h> |
||
37 | #include <linux/filter.h> |
||
38 | |||
39 | #include <misc/debug.h> |
||
40 | #include <misc/byteorder.h> |
||
41 | #include <misc/ethernet_proto.h> |
||
42 | #include <misc/ipv4_proto.h> |
||
43 | #include <misc/udp_proto.h> |
||
44 | #include <misc/dhcp_proto.h> |
||
45 | #include <misc/get_iface_info.h> |
||
46 | #include <base/BLog.h> |
||
47 | |||
48 | #include <dhcpclient/BDHCPClient.h> |
||
49 | |||
50 | #include <generated/blog_channel_BDHCPClient.h> |
||
51 | |||
52 | #define DHCP_SERVER_PORT 67 |
||
53 | #define DHCP_CLIENT_PORT 68 |
||
54 | |||
55 | #define IPUDP_OVERHEAD (sizeof(struct ipv4_header) + sizeof(struct udp_header)) |
||
56 | |||
57 | static const struct sock_filter dhcp_sock_filter[] = { |
||
58 | BPF_STMT(BPF_LD + BPF_B + BPF_ABS, 9), // A <- IP protocol |
||
59 | BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, IPV4_PROTOCOL_UDP, 0, 3), // IP protocol = UDP ? |
||
60 | BPF_STMT(BPF_LD + BPF_H + BPF_ABS, 22), // A <- UDP destination port |
||
61 | BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, DHCP_CLIENT_PORT, 0, 1), // UDP destination port = DHCP client ? |
||
62 | BPF_STMT(BPF_RET + BPF_K, 65535), // return all |
||
63 | BPF_STMT(BPF_RET + BPF_K, 0) // ignore |
||
64 | }; |
||
65 | |||
66 | static void dgram_handler (BDHCPClient *o, int event) |
||
67 | { |
||
68 | DebugObject_Access(&o->d_obj); |
||
69 | |||
70 | BLog(BLOG_ERROR, "packet socket error"); |
||
71 | |||
72 | // report error |
||
73 | DEBUGERROR(&o->d_err, o->handler(o->user, BDHCPCLIENT_EVENT_ERROR)); |
||
74 | return; |
||
75 | } |
||
76 | |||
77 | static void dhcp_handler (BDHCPClient *o, int event) |
||
78 | { |
||
79 | DebugObject_Access(&o->d_obj); |
||
80 | |||
81 | switch (event) { |
||
82 | case BDHCPCLIENTCORE_EVENT_UP: |
||
83 | ASSERT(!o->up) |
||
84 | o->up = 1; |
||
85 | o->handler(o->user, BDHCPCLIENT_EVENT_UP); |
||
86 | return; |
||
87 | |||
88 | case BDHCPCLIENTCORE_EVENT_DOWN: |
||
89 | ASSERT(o->up) |
||
90 | o->up = 0; |
||
91 | o->handler(o->user, BDHCPCLIENT_EVENT_DOWN); |
||
92 | return; |
||
93 | |||
94 | default: |
||
95 | ASSERT(0); |
||
96 | } |
||
97 | } |
||
98 | |||
99 | static void dhcp_func_getsendermac (BDHCPClient *o, uint8_t *out_mac) |
||
100 | { |
||
101 | DebugObject_Access(&o->d_obj); |
||
102 | |||
103 | BAddr remote_addr; |
||
104 | BIPAddr local_addr; |
||
105 | if (!BDatagram_GetLastReceiveAddrs(&o->dgram, &remote_addr, &local_addr)) { |
||
106 | BLog(BLOG_ERROR, "BDatagram_GetLastReceiveAddrs failed"); |
||
107 | goto fail; |
||
108 | } |
||
109 | |||
110 | if (remote_addr.type != BADDR_TYPE_PACKET) { |
||
111 | BLog(BLOG_ERROR, "address type invalid"); |
||
112 | goto fail; |
||
113 | } |
||
114 | |||
115 | if (remote_addr.packet.header_type != BADDR_PACKET_HEADER_TYPE_ETHERNET) { |
||
116 | BLog(BLOG_ERROR, "address header type invalid"); |
||
117 | goto fail; |
||
118 | } |
||
119 | |||
120 | memcpy(out_mac, remote_addr.packet.phys_addr, 6); |
||
121 | return; |
||
122 | |||
123 | fail: |
||
124 | memset(out_mac, 0, 6); |
||
125 | } |
||
126 | |||
127 | int BDHCPClient_Init (BDHCPClient *o, const char *ifname, struct BDHCPClient_opts opts, BReactor *reactor, BRandom2 *random2, BDHCPClient_handler handler, void *user) |
||
128 | { |
||
129 | // init arguments |
||
130 | o->reactor = reactor; |
||
131 | o->handler = handler; |
||
132 | o->user = user; |
||
133 | |||
134 | // get interface information |
||
135 | uint8_t if_mac[6]; |
||
136 | int if_mtu; |
||
137 | int if_index; |
||
138 | if (!badvpn_get_iface_info(ifname, if_mac, &if_mtu, &if_index)) { |
||
139 | BLog(BLOG_ERROR, "failed to get interface information"); |
||
140 | goto fail0; |
||
141 | } |
||
142 | |||
143 | BLog(BLOG_INFO, "if_mac=%02"PRIx8":%02"PRIx8":%02"PRIx8":%02"PRIx8":%02"PRIx8":%02"PRIx8" if_mtu=%d if_index=%d", |
||
144 | if_mac[0], if_mac[1], if_mac[2], if_mac[3], if_mac[4], if_mac[5], if_mtu, if_index); |
||
145 | |||
146 | if (if_mtu < IPUDP_OVERHEAD) { |
||
147 | BLog(BLOG_ERROR, "MTU is too small for UDP/IP !?!"); |
||
148 | goto fail0; |
||
149 | } |
||
150 | |||
151 | int dhcp_mtu = if_mtu - IPUDP_OVERHEAD; |
||
152 | |||
153 | // init dgram |
||
154 | if (!BDatagram_Init(&o->dgram, BADDR_TYPE_PACKET, o->reactor, o, (BDatagram_handler)dgram_handler)) { |
||
155 | BLog(BLOG_ERROR, "BDatagram_Init failed"); |
||
156 | goto fail0; |
||
157 | } |
||
158 | |||
159 | // set socket filter |
||
160 | { |
||
161 | struct sock_filter filter[sizeof(dhcp_sock_filter) / sizeof(dhcp_sock_filter[0])]; |
||
162 | memcpy(filter, dhcp_sock_filter, sizeof(filter)); |
||
163 | struct sock_fprog fprog = { |
||
164 | .len = sizeof(filter) / sizeof(filter[0]), |
||
165 | .filter = filter |
||
166 | }; |
||
167 | if (setsockopt(BDatagram_GetFd(&o->dgram), SOL_SOCKET, SO_ATTACH_FILTER, &fprog, sizeof(fprog)) < 0) { |
||
168 | BLog(BLOG_NOTICE, "not using socket filter"); |
||
169 | } |
||
170 | } |
||
171 | |||
172 | // bind dgram |
||
173 | BAddr bind_addr; |
||
174 | BAddr_InitPacket(&bind_addr, hton16(ETHERTYPE_IPV4), if_index, BADDR_PACKET_HEADER_TYPE_ETHERNET, BADDR_PACKET_PACKET_TYPE_HOST, if_mac); |
||
175 | if (!BDatagram_Bind(&o->dgram, bind_addr)) { |
||
176 | BLog(BLOG_ERROR, "BDatagram_Bind failed"); |
||
177 | goto fail1; |
||
178 | } |
||
179 | |||
180 | // set dgram send addresses |
||
181 | BAddr dest_addr; |
||
182 | uint8_t broadcast_mac[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff}; |
||
183 | BAddr_InitPacket(&dest_addr, hton16(ETHERTYPE_IPV4), if_index, BADDR_PACKET_HEADER_TYPE_ETHERNET, BADDR_PACKET_PACKET_TYPE_BROADCAST, broadcast_mac); |
||
184 | BIPAddr local_addr; |
||
185 | BIPAddr_InitInvalid(&local_addr); |
||
186 | BDatagram_SetSendAddrs(&o->dgram, dest_addr, local_addr); |
||
187 | |||
188 | // init dgram interfaces |
||
189 | BDatagram_SendAsync_Init(&o->dgram, if_mtu); |
||
190 | BDatagram_RecvAsync_Init(&o->dgram, if_mtu); |
||
191 | |||
192 | // init sending |
||
193 | |||
194 | // init copier |
||
195 | PacketCopier_Init(&o->send_copier, dhcp_mtu, BReactor_PendingGroup(o->reactor)); |
||
196 | |||
197 | // init encoder |
||
198 | DHCPIpUdpEncoder_Init(&o->send_encoder, PacketCopier_GetOutput(&o->send_copier), BReactor_PendingGroup(o->reactor)); |
||
199 | |||
200 | // init buffer |
||
201 | if (!SinglePacketBuffer_Init(&o->send_buffer, DHCPIpUdpEncoder_GetOutput(&o->send_encoder), BDatagram_SendAsync_GetIf(&o->dgram), BReactor_PendingGroup(o->reactor))) { |
||
202 | BLog(BLOG_ERROR, "SinglePacketBuffer_Init failed"); |
||
203 | goto fail2; |
||
204 | } |
||
205 | |||
206 | // init receiving |
||
207 | |||
208 | // init copier |
||
209 | PacketCopier_Init(&o->recv_copier, dhcp_mtu, BReactor_PendingGroup(o->reactor)); |
||
210 | |||
211 | // init decoder |
||
212 | DHCPIpUdpDecoder_Init(&o->recv_decoder, PacketCopier_GetInput(&o->recv_copier), BReactor_PendingGroup(o->reactor)); |
||
213 | |||
214 | // init buffer |
||
215 | if (!SinglePacketBuffer_Init(&o->recv_buffer, BDatagram_RecvAsync_GetIf(&o->dgram), DHCPIpUdpDecoder_GetInput(&o->recv_decoder), BReactor_PendingGroup(o->reactor))) { |
||
216 | BLog(BLOG_ERROR, "SinglePacketBuffer_Init failed"); |
||
217 | goto fail3; |
||
218 | } |
||
219 | |||
220 | // init options |
||
221 | struct BDHCPClientCore_opts core_opts; |
||
222 | core_opts.hostname = opts.hostname; |
||
223 | core_opts.vendorclassid = opts.vendorclassid; |
||
224 | core_opts.clientid = opts.clientid; |
||
225 | core_opts.clientid_len = opts.clientid_len; |
||
226 | |||
227 | // auto-generate clientid from MAC if requested |
||
228 | uint8_t mac_cid[7]; |
||
229 | if (opts.auto_clientid) { |
||
230 | mac_cid[0] = DHCP_HARDWARE_ADDRESS_TYPE_ETHERNET; |
||
231 | memcpy(mac_cid + 1, if_mac, 6); |
||
232 | core_opts.clientid = mac_cid; |
||
233 | core_opts.clientid_len = sizeof(mac_cid); |
||
234 | } |
||
235 | |||
236 | // init dhcp |
||
237 | if (!BDHCPClientCore_Init(&o->dhcp, PacketCopier_GetInput(&o->send_copier), PacketCopier_GetOutput(&o->recv_copier), if_mac, core_opts, o->reactor, random2, o, |
||
238 | (BDHCPClientCore_func_getsendermac)dhcp_func_getsendermac, |
||
239 | (BDHCPClientCore_handler)dhcp_handler |
||
240 | )) { |
||
241 | BLog(BLOG_ERROR, "BDHCPClientCore_Init failed"); |
||
242 | goto fail4; |
||
243 | } |
||
244 | |||
245 | // set not up |
||
246 | o->up = 0; |
||
247 | |||
248 | DebugError_Init(&o->d_err, BReactor_PendingGroup(o->reactor)); |
||
249 | DebugObject_Init(&o->d_obj); |
||
250 | return 1; |
||
251 | |||
252 | fail4: |
||
253 | SinglePacketBuffer_Free(&o->recv_buffer); |
||
254 | fail3: |
||
255 | DHCPIpUdpDecoder_Free(&o->recv_decoder); |
||
256 | PacketCopier_Free(&o->recv_copier); |
||
257 | SinglePacketBuffer_Free(&o->send_buffer); |
||
258 | fail2: |
||
259 | DHCPIpUdpEncoder_Free(&o->send_encoder); |
||
260 | PacketCopier_Free(&o->send_copier); |
||
261 | BDatagram_RecvAsync_Free(&o->dgram); |
||
262 | BDatagram_SendAsync_Free(&o->dgram); |
||
263 | fail1: |
||
264 | BDatagram_Free(&o->dgram); |
||
265 | fail0: |
||
266 | return 0; |
||
267 | } |
||
268 | |||
269 | void BDHCPClient_Free (BDHCPClient *o) |
||
270 | { |
||
271 | DebugObject_Free(&o->d_obj); |
||
272 | DebugError_Free(&o->d_err); |
||
273 | |||
274 | // free dhcp |
||
275 | BDHCPClientCore_Free(&o->dhcp); |
||
276 | |||
277 | // free receiving |
||
278 | SinglePacketBuffer_Free(&o->recv_buffer); |
||
279 | DHCPIpUdpDecoder_Free(&o->recv_decoder); |
||
280 | PacketCopier_Free(&o->recv_copier); |
||
281 | |||
282 | // free sending |
||
283 | SinglePacketBuffer_Free(&o->send_buffer); |
||
284 | DHCPIpUdpEncoder_Free(&o->send_encoder); |
||
285 | PacketCopier_Free(&o->send_copier); |
||
286 | |||
287 | // free dgram interfaces |
||
288 | BDatagram_RecvAsync_Free(&o->dgram); |
||
289 | BDatagram_SendAsync_Free(&o->dgram); |
||
290 | |||
291 | // free dgram |
||
292 | BDatagram_Free(&o->dgram); |
||
293 | } |
||
294 | |||
295 | int BDHCPClient_IsUp (BDHCPClient *o) |
||
296 | { |
||
297 | DebugObject_Access(&o->d_obj); |
||
298 | |||
299 | return o->up; |
||
300 | } |
||
301 | |||
302 | void BDHCPClient_GetClientIP (BDHCPClient *o, uint32_t *out_ip) |
||
303 | { |
||
304 | DebugObject_Access(&o->d_obj); |
||
305 | ASSERT(o->up) |
||
306 | |||
307 | BDHCPClientCore_GetClientIP(&o->dhcp, out_ip); |
||
308 | } |
||
309 | |||
310 | void BDHCPClient_GetClientMask (BDHCPClient *o, uint32_t *out_mask) |
||
311 | { |
||
312 | DebugObject_Access(&o->d_obj); |
||
313 | ASSERT(o->up) |
||
314 | |||
315 | BDHCPClientCore_GetClientMask(&o->dhcp, out_mask); |
||
316 | } |
||
317 | |||
318 | int BDHCPClient_GetRouter (BDHCPClient *o, uint32_t *out_router) |
||
319 | { |
||
320 | DebugObject_Access(&o->d_obj); |
||
321 | ASSERT(o->up) |
||
322 | |||
323 | return BDHCPClientCore_GetRouter(&o->dhcp, out_router); |
||
324 | } |
||
325 | |||
326 | int BDHCPClient_GetDNS (BDHCPClient *o, uint32_t *out_dns_servers, size_t max_dns_servers) |
||
327 | { |
||
328 | DebugObject_Access(&o->d_obj); |
||
329 | ASSERT(o->up) |
||
330 | |||
331 | return BDHCPClientCore_GetDNS(&o->dhcp, out_dns_servers, max_dns_servers); |
||
332 | } |
||
333 | |||
334 | void BDHCPClient_GetServerMAC (BDHCPClient *o, uint8_t *out_mac) |
||
335 | { |
||
336 | DebugObject_Access(&o->d_obj); |
||
337 | ASSERT(o->up) |
||
338 | |||
339 | BDHCPClientCore_GetServerMAC(&o->dhcp, out_mac); |
||
340 | } |