2 * Copyright (C) 2012-2013 Steven Barth <steven@midlink.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License v2 as published by
6 * the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
20 #include <sys/timerfd.h>
26 static void relay_client_request(struct sockaddr_in6
*source
,
27 const void *data
, size_t len
, struct interface
*iface
);
28 static void relay_server_response(uint8_t *data
, size_t len
);
30 static void handle_dhcpv6(void *addr
, void *data
, size_t len
,
31 struct interface
*iface
);
32 static void handle_client_request(void *addr
, void *data
, size_t len
,
33 struct interface
*iface
);
37 // Create socket and register events
45 int setup_dhcpv6_interface(struct interface
*iface
, bool enable
)
47 if (iface
->dhcpv6_event
.uloop
.fd
> 0) {
48 uloop_fd_delete(&iface
->dhcpv6_event
.uloop
);
49 close(iface
->dhcpv6_event
.uloop
.fd
);
50 iface
->dhcpv6_event
.uloop
.fd
= -1;
53 // Configure multicast settings
54 if (enable
&& iface
->dhcpv6
&& !iface
->master
) {
55 int sock
= socket(AF_INET6
, SOCK_DGRAM
| SOCK_CLOEXEC
, IPPROTO_UDP
);
57 syslog(LOG_ERR
, "Failed to create DHCPv6 server socket: %s",
62 // Basic IPv6 configuration
63 setsockopt(sock
, SOL_SOCKET
, SO_BINDTODEVICE
, iface
->ifname
, strlen(iface
->ifname
));
66 setsockopt(sock
, IPPROTO_IPV6
, IPV6_V6ONLY
, &val
, sizeof(val
));
67 setsockopt(sock
, SOL_SOCKET
, SO_REUSEADDR
, &val
, sizeof(val
));
68 setsockopt(sock
, IPPROTO_IPV6
, IPV6_RECVPKTINFO
, &val
, sizeof(val
));
70 val
= DHCPV6_HOP_COUNT_LIMIT
;
71 setsockopt(sock
, IPPROTO_IPV6
, IPV6_MULTICAST_HOPS
, &val
, sizeof(val
));
74 setsockopt(sock
, IPPROTO_IPV6
, IPV6_MULTICAST_LOOP
, &val
, sizeof(val
));
76 struct sockaddr_in6 bind_addr
= {AF_INET6
, htons(DHCPV6_SERVER_PORT
),
77 0, IN6ADDR_ANY_INIT
, 0};
79 if (bind(sock
, (struct sockaddr
*)&bind_addr
, sizeof(bind_addr
))) {
80 syslog(LOG_ERR
, "Failed to open DHCPv6 server socket: %s",
85 struct ipv6_mreq relay
= {ALL_DHCPV6_RELAYS
, iface
->ifindex
};
86 struct ipv6_mreq server
= {ALL_DHCPV6_SERVERS
, iface
->ifindex
};
87 setsockopt(sock
, IPPROTO_IPV6
, IPV6_ADD_MEMBERSHIP
, &relay
, sizeof(relay
));
89 if (iface
->dhcpv6
== RELAYD_SERVER
)
90 setsockopt(sock
, IPPROTO_IPV6
, IPV6_ADD_MEMBERSHIP
, &server
, sizeof(server
));
92 iface
->dhcpv6_event
.uloop
.fd
= sock
;
93 iface
->dhcpv6_event
.handle_dgram
= handle_dhcpv6
;
94 odhcpd_register(&iface
->dhcpv6_event
);
97 return setup_dhcpv6_ia_interface(iface
, enable
);
101 static void handle_nested_message(uint8_t *data
, size_t len
,
102 uint8_t **opts
, uint8_t **end
, struct iovec iov
[6])
104 struct dhcpv6_relay_header
*hdr
= (struct dhcpv6_relay_header
*)data
;
105 if (iov
[0].iov_base
== NULL
) {
106 iov
[0].iov_base
= data
;
107 iov
[0].iov_len
= len
;
110 if (len
< sizeof(struct dhcpv6_client_header
))
113 if (hdr
->msg_type
!= DHCPV6_MSG_RELAY_FORW
) {
114 iov
[0].iov_len
= data
- (uint8_t*)iov
[0].iov_base
;
115 struct dhcpv6_client_header
*hdr
= (void*)data
;
116 *opts
= (uint8_t*)&hdr
[1];
121 uint16_t otype
, olen
;
123 dhcpv6_for_each_option(hdr
->options
, data
+ len
, otype
, olen
, odata
) {
124 if (otype
== DHCPV6_OPT_RELAY_MSG
) {
125 iov
[7].iov_base
= odata
+ olen
;
126 iov
[7].iov_len
= (((uint8_t*)iov
[0].iov_base
) + iov
[0].iov_len
)
128 handle_nested_message(odata
, olen
, opts
, end
, iov
);
135 static void update_nested_message(uint8_t *data
, size_t len
, ssize_t pdiff
)
137 struct dhcpv6_relay_header
*hdr
= (struct dhcpv6_relay_header
*)data
;
138 if (hdr
->msg_type
!= DHCPV6_MSG_RELAY_FORW
)
141 hdr
->msg_type
= DHCPV6_MSG_RELAY_REPL
;
143 uint16_t otype
, olen
;
145 dhcpv6_for_each_option(hdr
->options
, data
+ len
, otype
, olen
, odata
) {
146 if (otype
== DHCPV6_OPT_RELAY_MSG
) {
148 odata
[-2] = (olen
>> 8) & 0xff;
149 odata
[-1] = olen
& 0xff;
150 update_nested_message(odata
, olen
- pdiff
, pdiff
);
157 // Simple DHCPv6-server for information requests
158 static void handle_client_request(void *addr
, void *data
, size_t len
,
159 struct interface
*iface
)
161 struct dhcpv6_client_header
*hdr
= data
;
162 if (len
< sizeof(*hdr
))
165 syslog(LOG_NOTICE
, "Got DHCPv6 request");
167 // Construct reply message
168 struct __attribute__((packed
)) {
171 uint16_t serverid_type
;
172 uint16_t serverid_length
;
174 uint16_t hardware_type
;
176 uint16_t clientid_type
;
177 uint16_t clientid_length
;
178 uint8_t clientid_buf
[130];
180 .msg_type
= DHCPV6_MSG_REPLY
,
181 .serverid_type
= htons(DHCPV6_OPT_SERVERID
),
182 .serverid_length
= htons(10),
183 .duid_type
= htons(3),
184 .hardware_type
= htons(1),
185 .clientid_type
= htons(DHCPV6_OPT_CLIENTID
),
188 odhcpd_get_mac(iface
, dest
.mac
);
190 struct __attribute__((packed
)) {
194 } stat
= {htons(DHCPV6_OPT_STATUS
), htons(sizeof(stat
) - 4),
195 htons(DHCPV6_STATUS_NOADDRSAVAIL
)};
197 struct __attribute__((packed
)) {
201 } refresh
= {htons(DHCPV6_OPT_INFO_REFRESH
), htons(sizeof(uint32_t)),
204 struct odhcpd_ipaddr ipaddr
;
205 struct in6_addr
*dns_addr
= iface
->dns
;
206 size_t dns_cnt
= iface
->dns_cnt
;
208 if (dns_cnt
== 0 && odhcpd_get_interface_addresses(iface
->ifindex
, &ipaddr
, 1) == 1) {
209 dns_addr
= &ipaddr
.addr
;
216 } dns
= {htons(DHCPV6_OPT_DNS_SERVERS
), htons(dns_cnt
* sizeof(*dns_addr
))};
220 // DNS Search options
221 uint8_t search_buf
[256], *search_domain
= iface
->search
;
222 size_t search_len
= iface
->search_len
;
224 if (!search_domain
&& !res_init() && _res
.dnsrch
[0] && _res
.dnsrch
[0][0]) {
225 int len
= dn_comp(_res
.dnsrch
[0], search_buf
,
226 sizeof(search_buf
), NULL
, NULL
);
228 search_domain
= search_buf
;
236 } search
= {htons(DHCPV6_OPT_DNS_DOMAIN
), htons(search_len
)};
241 struct iovec iov
[] = {{NULL
, 0},
242 {&dest
, (uint8_t*)&dest
.clientid_type
- (uint8_t*)&dest
},
243 {&dns
, (dns_cnt
) ? sizeof(dns
) : 0},
244 {dns_addr
, dns_cnt
* sizeof(*dns_addr
)},
245 {&search
, (search_len
) ? sizeof(search
) : 0},
246 {search_domain
, search_len
},
250 uint8_t *opts
= (uint8_t*)&hdr
[1], *opts_end
= (uint8_t*)data
+ len
;
251 if (hdr
->msg_type
== DHCPV6_MSG_RELAY_FORW
)
252 handle_nested_message(data
, len
, &opts
, &opts_end
, iov
);
254 memcpy(dest
.tr_id
, &opts
[-3], sizeof(dest
.tr_id
));
256 if (opts
[-4] == DHCPV6_MSG_ADVERTISE
|| opts
[-4] == DHCPV6_MSG_REPLY
|| opts
[-4] == DHCPV6_MSG_RELAY_REPL
)
259 if (opts
[-4] == DHCPV6_MSG_SOLICIT
) {
260 dest
.msg_type
= DHCPV6_MSG_ADVERTISE
;
261 } else if (opts
[-4] == DHCPV6_MSG_INFORMATION_REQUEST
) {
262 iov
[6].iov_base
= &refresh
;
263 iov
[6].iov_len
= sizeof(refresh
);
266 // Go through options and find what we need
267 uint16_t otype
, olen
;
269 dhcpv6_for_each_option(opts
, opts_end
, otype
, olen
, odata
) {
270 if (otype
== DHCPV6_OPT_CLIENTID
&& olen
<= 130) {
271 dest
.clientid_length
= htons(olen
);
272 memcpy(dest
.clientid_buf
, odata
, olen
);
273 iov
[1].iov_len
+= 4 + olen
;
274 } else if (otype
== DHCPV6_OPT_SERVERID
) {
275 if (olen
!= ntohs(dest
.serverid_length
) ||
276 memcmp(odata
, &dest
.duid_type
, olen
))
277 return; // Not for us
281 if (opts
[-4] != DHCPV6_MSG_INFORMATION_REQUEST
) {
282 iov
[6].iov_len
= dhcpv6_handle_ia(pdbuf
, sizeof(pdbuf
), iface
, addr
, &opts
[-4], opts_end
);
283 if (iov
[6].iov_len
== 0 && opts
[-4] == DHCPV6_MSG_REBIND
)
287 if (iov
[0].iov_len
> 0) // Update length
288 update_nested_message(data
, len
, iov
[1].iov_len
+ iov
[2].iov_len
+
289 iov
[3].iov_len
+ iov
[4].iov_len
+ iov
[5].iov_len
+
290 iov
[6].iov_len
- (4 + opts_end
- opts
));
292 odhcpd_send(iface
->dhcpv6_event
.uloop
.fd
, addr
, iov
, ARRAY_SIZE(iov
), iface
);
296 // Central DHCPv6-relay handler
297 static void handle_dhcpv6(void *addr
, void *data
, size_t len
,
298 struct interface
*iface
)
300 if (iface
->dhcpv6
== RELAYD_SERVER
) {
301 handle_client_request(addr
, data
, len
, iface
);
302 } else if (iface
->dhcpv6
== RELAYD_RELAY
) {
304 relay_server_response(data
, len
);
306 relay_client_request(addr
, data
, len
, iface
);
311 // Relay server response (regular relay server handling)
312 static void relay_server_response(uint8_t *data
, size_t len
)
314 // Information we need to gather
315 uint8_t *payload_data
= NULL
;
316 size_t payload_len
= 0;
317 int32_t ifaceidx
= 0;
318 struct sockaddr_in6 target
= {AF_INET6
, htons(DHCPV6_CLIENT_PORT
),
319 0, IN6ADDR_ANY_INIT
, 0};
321 syslog(LOG_NOTICE
, "Got a DHCPv6-reply");
324 uint8_t *odata
, *end
= data
+ len
;
326 // Relay DHCPv6 reply from server to client
327 struct dhcpv6_relay_header
*h
= (void*)data
;
328 if (len
< sizeof(*h
) || h
->msg_type
!= DHCPV6_MSG_RELAY_REPL
)
331 memcpy(&target
.sin6_addr
, &h
->peer_address
,
332 sizeof(struct in6_addr
));
334 // Go through options and find what we need
335 dhcpv6_for_each_option(h
->options
, end
, otype
, olen
, odata
) {
336 if (otype
== DHCPV6_OPT_INTERFACE_ID
337 && olen
== sizeof(ifaceidx
)) {
338 memcpy(&ifaceidx
, odata
, sizeof(ifaceidx
));
339 } else if (otype
== DHCPV6_OPT_RELAY_MSG
) {
340 payload_data
= odata
;
345 // Invalid interface-id or basic payload
346 struct interface
*iface
= odhcpd_get_interface_by_index(ifaceidx
);
347 if (!iface
|| iface
->master
|| !payload_data
|| payload_len
< 4)
350 bool is_authenticated
= false;
351 struct in6_addr
*dns_ptr
= NULL
;
352 size_t dns_count
= 0;
354 // If the payload is relay-reply we have to send to the server port
355 if (payload_data
[0] == DHCPV6_MSG_RELAY_REPL
) {
356 target
.sin6_port
= htons(DHCPV6_SERVER_PORT
);
357 } else { // Go through the payload data
358 struct dhcpv6_client_header
*h
= (void*)payload_data
;
359 end
= payload_data
+ payload_len
;
361 dhcpv6_for_each_option(&h
[1], end
, otype
, olen
, odata
) {
362 if (otype
== DHCPV6_OPT_DNS_SERVERS
&& olen
>= 16) {
363 dns_ptr
= (struct in6_addr
*)odata
;
364 dns_count
= olen
/ 16;
365 } else if (otype
== DHCPV6_OPT_AUTH
) {
366 is_authenticated
= true;
371 // Rewrite DNS servers if requested
372 if (iface
->always_rewrite_dns
&& dns_ptr
&& dns_count
> 0) {
373 if (is_authenticated
)
374 return; // Impossible to rewrite
376 struct odhcpd_ipaddr ip
;
377 const struct in6_addr
*rewrite
= iface
->dns
;
378 size_t rewrite_cnt
= iface
->dns_cnt
;
380 if (rewrite_cnt
== 0) {
381 if (odhcpd_get_interface_addresses(iface
->ifindex
, &ip
, 1) < 1)
382 return; // Unable to get interface address
388 // Copy over any other addresses
389 for (size_t i
= 0; i
< dns_count
; ++i
) {
390 size_t j
= (i
< rewrite_cnt
) ? i
: rewrite_cnt
- 1;
391 memcpy(&dns_ptr
[i
], &rewrite
[j
], sizeof(*rewrite
));
395 struct iovec iov
= {payload_data
, payload_len
};
396 odhcpd_send(iface
->dhcpv6_event
.uloop
.fd
, &target
, &iov
, 1, iface
);
400 // Relay client request (regular DHCPv6-relay)
401 static void relay_client_request(struct sockaddr_in6
*source
,
402 const void *data
, size_t len
, struct interface
*iface
)
404 struct interface
*master
= odhcpd_get_master_interface();
405 const struct dhcpv6_relay_header
*h
= data
;
406 if (!master
|| master
->dhcpv6
!= RELAYD_RELAY
||
407 h
->msg_type
== DHCPV6_MSG_RELAY_REPL
||
408 h
->msg_type
== DHCPV6_MSG_RECONFIGURE
||
409 h
->msg_type
== DHCPV6_MSG_REPLY
||
410 h
->msg_type
== DHCPV6_MSG_ADVERTISE
)
411 return; // Invalid message types for client
413 syslog(LOG_NOTICE
, "Got a DHCPv6-request");
415 // Construct our forwarding envelope
416 struct dhcpv6_relay_forward_envelope hdr
= {
417 .msg_type
= DHCPV6_MSG_RELAY_FORW
,
419 .interface_id_type
= htons(DHCPV6_OPT_INTERFACE_ID
),
420 .interface_id_len
= htons(sizeof(uint32_t)),
421 .relay_message_type
= htons(DHCPV6_OPT_RELAY_MSG
),
422 .relay_message_len
= htons(len
),
425 if (h
->msg_type
== DHCPV6_MSG_RELAY_FORW
) { // handle relay-forward
426 if (h
->hop_count
>= DHCPV6_HOP_COUNT_LIMIT
)
427 return; // Invalid hop count
429 hdr
.hop_count
= h
->hop_count
+ 1;
432 // use memcpy here as the destination fields are unaligned
433 uint32_t ifindex
= iface
->ifindex
;
434 memcpy(&hdr
.peer_address
, &source
->sin6_addr
, sizeof(struct in6_addr
));
435 memcpy(&hdr
.interface_id_data
, &ifindex
, sizeof(ifindex
));
437 // Detect public IP of slave interface to use as link-address
438 struct odhcpd_ipaddr ip
;
439 if (odhcpd_get_interface_addresses(iface
->ifindex
, &ip
, 1) < 1) {
440 // No suitable address! Is the slave not configured yet?
441 // Detect public IP of master interface and use it instead
442 // This is WRONG and probably violates the RFC. However
443 // otherwise we have a hen and egg problem because the
444 // slave-interface cannot be auto-configured.
445 if (odhcpd_get_interface_addresses(master
->ifindex
, &ip
, 1) < 1)
446 return; // Could not obtain a suitable address
448 memcpy(&hdr
.link_address
, &ip
.addr
, sizeof(hdr
.link_address
));
450 struct sockaddr_in6 dhcpv6_servers
= {AF_INET6
,
451 htons(DHCPV6_SERVER_PORT
), 0, ALL_DHCPV6_SERVERS
, 0};
452 struct iovec iov
[2] = {{&hdr
, sizeof(hdr
)}, {(void*)data
, len
}};
453 odhcpd_send(iface
->dhcpv6_event
.uloop
.fd
, &dhcpv6_servers
, iov
, 2, master
);