2 * Copyright (C) 2012-2013 Steven Barth <steven@midlink.org>
3 * Copyright (C) 2018 Hans Dedecker <dedeckeh@gmail.com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License v2 as published by
7 * the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
21 #include <sys/timerfd.h>
22 #include <arpa/inet.h>
24 #include <libubox/utils.h>
32 static void relay_client_request(struct sockaddr_in6
*source
,
33 const void *data
, size_t len
, struct interface
*iface
);
34 static void relay_server_response(uint8_t *data
, size_t len
);
36 static void handle_dhcpv6(void *addr
, void *data
, size_t len
,
37 struct interface
*iface
, void *dest
);
38 static void handle_client_request(void *addr
, void *data
, size_t len
,
39 struct interface
*iface
, void *dest_addr
);
42 /* Create socket and register events */
45 return dhcpv6_ia_init();
48 int dhcpv6_setup_interface(struct interface
*iface
, bool enable
)
52 enable
= enable
&& (iface
->dhcpv6
!= MODE_DISABLED
);
54 if (iface
->dhcpv6_event
.uloop
.fd
>= 0) {
55 uloop_fd_delete(&iface
->dhcpv6_event
.uloop
);
56 close(iface
->dhcpv6_event
.uloop
.fd
);
57 iface
->dhcpv6_event
.uloop
.fd
= -1;
60 /* Configure multicast settings */
62 struct sockaddr_in6 bind_addr
= {AF_INET6
, htons(DHCPV6_SERVER_PORT
),
63 0, IN6ADDR_ANY_INIT
, 0};
64 struct ipv6_mreq mreq
;
67 iface
->dhcpv6_event
.uloop
.fd
= socket(AF_INET6
, SOCK_DGRAM
| SOCK_CLOEXEC
, IPPROTO_UDP
);
68 if (iface
->dhcpv6_event
.uloop
.fd
< 0) {
69 syslog(LOG_ERR
, "socket(AF_INET6): %m");
74 /* Basic IPv6 configuration */
75 if (setsockopt(iface
->dhcpv6_event
.uloop
.fd
, SOL_SOCKET
, SO_BINDTODEVICE
,
76 iface
->ifname
, strlen(iface
->ifname
)) < 0) {
77 syslog(LOG_ERR
, "setsockopt(SO_BINDTODEVICE): %m");
82 if (setsockopt(iface
->dhcpv6_event
.uloop
.fd
, IPPROTO_IPV6
, IPV6_V6ONLY
,
83 &val
, sizeof(val
)) < 0) {
84 syslog(LOG_ERR
, "setsockopt(IPV6_V6ONLY): %m");
89 if (setsockopt(iface
->dhcpv6_event
.uloop
.fd
, SOL_SOCKET
, SO_REUSEADDR
,
90 &val
, sizeof(val
)) < 0) {
91 syslog(LOG_ERR
, "setsockopt(SO_REUSEADDR): %m");
96 if (setsockopt(iface
->dhcpv6_event
.uloop
.fd
, IPPROTO_IPV6
, IPV6_RECVPKTINFO
,
97 &val
, sizeof(val
)) < 0) {
98 syslog(LOG_ERR
, "setsockopt(IPV6_RECVPKTINFO): %m");
103 val
= DHCPV6_HOP_COUNT_LIMIT
;
104 if (setsockopt(iface
->dhcpv6_event
.uloop
.fd
, IPPROTO_IPV6
, IPV6_MULTICAST_HOPS
,
105 &val
, sizeof(val
)) < 0) {
106 syslog(LOG_ERR
, "setsockopt(IPV6_MULTICAST_HOPS): %m");
112 if (setsockopt(iface
->dhcpv6_event
.uloop
.fd
, IPPROTO_IPV6
, IPV6_MULTICAST_LOOP
,
113 &val
, sizeof(val
)) < 0) {
114 syslog(LOG_ERR
, "setsockopt(IPV6_MULTICAST_LOOP): %m");
119 if (bind(iface
->dhcpv6_event
.uloop
.fd
, (struct sockaddr
*)&bind_addr
,
120 sizeof(bind_addr
)) < 0) {
121 syslog(LOG_ERR
, "bind(): %m");
126 memset(&mreq
, 0, sizeof(mreq
));
127 inet_pton(AF_INET6
, ALL_DHCPV6_RELAYS
, &mreq
.ipv6mr_multiaddr
);
128 mreq
.ipv6mr_interface
= iface
->ifindex
;
130 if (setsockopt(iface
->dhcpv6_event
.uloop
.fd
, IPPROTO_IPV6
, IPV6_ADD_MEMBERSHIP
,
131 &mreq
, sizeof(mreq
)) < 0) {
132 syslog(LOG_ERR
, "setsockopt(IPV6_ADD_MEMBERSHIP): %m");
137 if (iface
->dhcpv6
== MODE_SERVER
) {
138 memset(&mreq
, 0, sizeof(mreq
));
139 inet_pton(AF_INET6
, ALL_DHCPV6_SERVERS
, &mreq
.ipv6mr_multiaddr
);
140 mreq
.ipv6mr_interface
= iface
->ifindex
;
142 if (setsockopt(iface
->dhcpv6_event
.uloop
.fd
, IPPROTO_IPV6
, IPV6_ADD_MEMBERSHIP
,
143 &mreq
, sizeof(mreq
)) < 0) {
144 syslog(LOG_ERR
, "setsockopt(IPV6_ADD_MEMBERSHIP): %m");
150 iface
->dhcpv6_event
.handle_dgram
= handle_dhcpv6
;
151 odhcpd_register(&iface
->dhcpv6_event
);
154 ret
= dhcpv6_ia_setup_interface(iface
, enable
);
157 if (ret
< 0 && iface
->dhcpv6_event
.uloop
.fd
>= 0) {
158 close(iface
->dhcpv6_event
.uloop
.fd
);
159 iface
->dhcpv6_event
.uloop
.fd
= -1;
169 #define IOV_STAT IOV_MAXRT
176 #define IOV_REFRESH IOV_PDBUF
184 static void handle_nested_message(uint8_t *data
, size_t len
,
185 struct dhcpv6_client_header
**c_hdr
, uint8_t **opts
,
186 uint8_t **end
, struct iovec iov
[IOV_TOTAL
])
188 struct dhcpv6_relay_header
*r_hdr
= (struct dhcpv6_relay_header
*)data
;
189 uint16_t otype
, olen
;
192 if (iov
[IOV_NESTED
].iov_base
== NULL
) {
193 iov
[IOV_NESTED
].iov_base
= data
;
194 iov
[IOV_NESTED
].iov_len
= len
;
197 if (len
< sizeof(struct dhcpv6_client_header
))
200 if (r_hdr
->msg_type
!= DHCPV6_MSG_RELAY_FORW
) {
201 iov
[IOV_NESTED
].iov_len
= data
- (uint8_t *)iov
[IOV_NESTED
].iov_base
;
202 *c_hdr
= (void *)data
;
203 *opts
= (uint8_t *)&(*c_hdr
)[1];
208 dhcpv6_for_each_option(r_hdr
->options
, data
+ len
, otype
, olen
, odata
) {
209 if (otype
== DHCPV6_OPT_RELAY_MSG
) {
210 iov
[IOV_RELAY_MSG
].iov_base
= odata
+ olen
;
211 iov
[IOV_RELAY_MSG
].iov_len
= (((uint8_t *)iov
[IOV_NESTED
].iov_base
) +
212 iov
[IOV_NESTED
].iov_len
) - (odata
+ olen
);
213 handle_nested_message(odata
, olen
, c_hdr
, opts
, end
, iov
);
220 static void update_nested_message(uint8_t *data
, size_t len
, ssize_t pdiff
)
222 struct dhcpv6_relay_header
*hdr
= (struct dhcpv6_relay_header
*)data
;
223 if (hdr
->msg_type
!= DHCPV6_MSG_RELAY_FORW
)
226 hdr
->msg_type
= DHCPV6_MSG_RELAY_REPL
;
228 uint16_t otype
, olen
;
230 dhcpv6_for_each_option(hdr
->options
, data
+ len
, otype
, olen
, odata
) {
231 if (otype
== DHCPV6_OPT_RELAY_MSG
) {
233 odata
[-2] = (olen
>> 8) & 0xff;
234 odata
[-1] = olen
& 0xff;
235 update_nested_message(odata
, olen
- pdiff
, pdiff
);
241 #ifdef DHCPV4_SUPPORT
243 struct dhcpv4_msg_data
{
249 static int send_reply(_unused
const void *buf
, size_t len
,
250 _unused
const struct sockaddr
*dest
, _unused socklen_t dest_len
,
251 _unused
void *opaque
)
253 struct dhcpv4_msg_data
*reply
= opaque
;
255 if (len
> reply
->maxsize
) {
256 syslog(LOG_ERR
, "4o6: reply too large, %u > %u", len
, reply
->maxsize
);
259 memcpy(reply
->msg
, buf
, len
);
266 static ssize_t
dhcpv6_4o6_query(uint8_t *buf
, size_t buflen
,
267 struct interface
*iface
,
268 const struct sockaddr_in6
*addr
,
269 const void *data
, const uint8_t *end
)
271 const struct dhcpv6_client_header
*hdr
= data
;
272 uint16_t otype
, olen
, msgv4_len
= 0;
273 uint8_t *msgv4_data
= NULL
;
274 uint8_t *start
= (uint8_t *)&hdr
[1], *odata
;
275 struct sockaddr_in addrv4
;
276 struct dhcpv4_msg_data reply
= { .msg
= buf
, .maxsize
= buflen
, .len
= -1 };
278 dhcpv6_for_each_option(start
, end
, otype
, olen
, odata
) {
279 if (otype
== DHCPV6_OPT_DHCPV4_MSG
) {
285 if (!msgv4_data
|| msgv4_len
== 0) {
286 syslog(LOG_ERR
, "4o6: missing DHCPv4 message option (%d)", DHCPV6_OPT_DHCPV4_MSG
);
290 // Dummy IPv4 address
291 memset(&addrv4
, 0, sizeof(addrv4
));
292 addrv4
.sin_family
= AF_INET
;
293 addrv4
.sin_addr
.s_addr
= INADDR_ANY
;
294 addrv4
.sin_port
= htons(DHCPV4_CLIENT_PORT
);
296 dhcpv4_handle_msg(&addrv4
, msgv4_data
, msgv4_len
,
297 iface
, NULL
, send_reply
, &reply
);
301 #endif /* DHCPV4_SUPPORT */
303 /* Simple DHCPv6-server for information requests */
304 static void handle_client_request(void *addr
, void *data
, size_t len
,
305 struct interface
*iface
, void *dest_addr
)
307 struct dhcpv6_client_header
*hdr
= data
;
308 uint8_t *opts
= (uint8_t *)&hdr
[1], *opts_end
= (uint8_t *)data
+ len
;
309 bool o_rapid_commit
= false;
311 if (len
< sizeof(*hdr
))
314 syslog(LOG_DEBUG
, "Got a DHCPv6-request on %s", iface
->name
);
316 /* Construct reply message */
317 struct __attribute__((packed
)) {
320 uint16_t serverid_type
;
321 uint16_t serverid_length
;
323 uint16_t hardware_type
;
325 uint16_t clientid_type
;
326 uint16_t clientid_length
;
327 uint8_t clientid_buf
[130];
329 .msg_type
= DHCPV6_MSG_REPLY
,
330 .serverid_type
= htons(DHCPV6_OPT_SERVERID
),
331 .serverid_length
= htons(10),
332 .duid_type
= htons(3),
333 .hardware_type
= htons(1),
334 .clientid_type
= htons(DHCPV6_OPT_CLIENTID
),
337 odhcpd_get_mac(iface
, dest
.mac
);
339 struct __attribute__((packed
)) {
343 } maxrt
= {htons(DHCPV6_OPT_SOL_MAX_RT
), htons(sizeof(maxrt
) - 4),
346 struct __attribute__((packed
)) {
349 } rapid_commit
= {htons(DHCPV6_OPT_RAPID_COMMIT
), 0};
351 struct __attribute__((packed
)) {
355 } stat
= {htons(DHCPV6_OPT_STATUS
), htons(sizeof(stat
) - 4),
356 htons(DHCPV6_STATUS_USEMULTICAST
)};
358 struct __attribute__((packed
)) {
362 } refresh
= {htons(DHCPV6_OPT_INFO_REFRESH
), htons(sizeof(uint32_t)),
365 struct in6_addr dns_addr
, *dns_addr_ptr
= iface
->dns
;
366 size_t dns_cnt
= iface
->dns_cnt
;
368 if ((dns_cnt
== 0) &&
369 !odhcpd_get_interface_dns_addr(iface
, &dns_addr
)) {
370 dns_addr_ptr
= &dns_addr
;
377 } dns
= {htons(DHCPV6_OPT_DNS_SERVERS
), htons(dns_cnt
* sizeof(*dns_addr_ptr
))};
381 /* DNS Search options */
382 uint8_t search_buf
[256], *search_domain
= iface
->search
;
383 size_t search_len
= iface
->search_len
;
385 if (!search_domain
&& !res_init() && _res
.dnsrch
[0] && _res
.dnsrch
[0][0]) {
386 int len
= dn_comp(_res
.dnsrch
[0], search_buf
,
387 sizeof(search_buf
), NULL
, NULL
);
389 search_domain
= search_buf
;
397 } search
= {htons(DHCPV6_OPT_DNS_DOMAIN
), htons(search_len
)};
400 struct __attribute__((packed
)) {
403 } dhcpv4o6_server
= {htons(DHCPV6_OPT_4O6_SERVER
), 0};
405 struct dhcpv6_cer_id cerid
= {
407 .type
= htons(EXT_CER_ID
),
410 .addr
= iface
->dhcpv6_pd_cer
,
415 struct iovec iov
[IOV_TOTAL
] = {
416 [IOV_NESTED
] = {NULL
, 0},
417 [IOV_DEST
] = {&dest
, (uint8_t*)&dest
.clientid_type
- (uint8_t*)&dest
},
418 [IOV_MAXRT
] = {&maxrt
, sizeof(maxrt
)},
419 [IOV_RAPID_COMMIT
] = {&rapid_commit
, 0},
420 [IOV_DNS
] = {&dns
, (dns_cnt
) ? sizeof(dns
) : 0},
421 [IOV_DNS_ADDR
] = {dns_addr_ptr
, dns_cnt
* sizeof(*dns_addr_ptr
)},
422 [IOV_SEARCH
] = {&search
, (search_len
) ? sizeof(search
) : 0},
423 [IOV_SEARCH_DOMAIN
] = {search_domain
, search_len
},
424 [IOV_PDBUF
] = {pdbuf
, 0},
425 [IOV_CERID
] = {&cerid
, 0},
426 [IOV_DHCPV6_RAW
] = {iface
->dhcpv6_raw
, iface
->dhcpv6_raw_len
},
427 [IOV_RELAY_MSG
] = {NULL
, 0},
428 [IOV_DHCPV4O6_SERVER
] = {&dhcpv4o6_server
, 0},
431 if (hdr
->msg_type
== DHCPV6_MSG_RELAY_FORW
)
432 handle_nested_message(data
, len
, &hdr
, &opts
, &opts_end
, iov
);
434 switch (hdr
->msg_type
) {
435 case DHCPV6_MSG_SOLICIT
:
436 case DHCPV6_MSG_REQUEST
:
437 case DHCPV6_MSG_CONFIRM
:
438 case DHCPV6_MSG_RENEW
:
439 case DHCPV6_MSG_REBIND
:
440 case DHCPV6_MSG_RELEASE
:
441 case DHCPV6_MSG_DECLINE
:
442 case DHCPV6_MSG_INFORMATION_REQUEST
:
443 case DHCPV6_MSG_RELAY_FORW
:
444 #ifdef DHCPV4_SUPPORT
445 case DHCPV6_MSG_DHCPV4_QUERY
:
447 break; /* Valid message types for clients */
448 case DHCPV6_MSG_ADVERTISE
:
449 case DHCPV6_MSG_REPLY
:
450 case DHCPV6_MSG_RECONFIGURE
:
451 case DHCPV6_MSG_RELAY_REPL
:
452 case DHCPV6_MSG_DHCPV4_RESPONSE
:
453 #ifndef DHCPV4_SUPPORT
454 case DHCPV6_MSG_DHCPV4_QUERY
:
457 return; /* Invalid message types for clients */
460 if (!IN6_IS_ADDR_MULTICAST((struct in6_addr
*)dest_addr
) && iov
[IOV_NESTED
].iov_len
== 0 &&
461 (hdr
->msg_type
== DHCPV6_MSG_SOLICIT
|| hdr
->msg_type
== DHCPV6_MSG_CONFIRM
||
462 hdr
->msg_type
== DHCPV6_MSG_REBIND
|| hdr
->msg_type
== DHCPV6_MSG_INFORMATION_REQUEST
))
465 memcpy(dest
.tr_id
, hdr
->transaction_id
, sizeof(dest
.tr_id
));
467 /* Go through options and find what we need */
468 uint16_t otype
, olen
;
470 dhcpv6_for_each_option(opts
, opts_end
, otype
, olen
, odata
) {
471 if (otype
== DHCPV6_OPT_CLIENTID
&& olen
<= 130) {
472 dest
.clientid_length
= htons(olen
);
473 memcpy(dest
.clientid_buf
, odata
, olen
);
474 iov
[IOV_DEST
].iov_len
+= 4 + olen
;
475 } else if (otype
== DHCPV6_OPT_SERVERID
) {
476 if (olen
!= ntohs(dest
.serverid_length
) ||
477 memcmp(odata
, &dest
.duid_type
, olen
))
478 return; /* Not for us */
479 } else if (iface
->filter_class
&& otype
== DHCPV6_OPT_USER_CLASS
) {
480 uint8_t *c
= odata
, *cend
= &odata
[olen
];
481 for (; &c
[2] <= cend
&& &c
[2 + (c
[0] << 8) + c
[1]] <= cend
; c
= &c
[2 + (c
[0] << 8) + c
[1]]) {
482 size_t elen
= strlen(iface
->filter_class
);
483 if (((((size_t)c
[0]) << 8) | c
[1]) == elen
&& !memcmp(&c
[2], iface
->filter_class
, elen
))
484 return; /* Ignore from homenet */
486 } else if (otype
== DHCPV6_OPT_IA_PD
) {
488 iov
[IOV_CERID
].iov_len
= sizeof(cerid
);
490 if (IN6_IS_ADDR_UNSPECIFIED(&cerid
.addr
)) {
491 struct odhcpd_ipaddr
*addrs
;
492 ssize_t len
= netlink_get_interface_addrs(0, true, &addrs
);
494 for (ssize_t i
= 0; i
< len
; ++i
)
495 if (IN6_IS_ADDR_UNSPECIFIED(&cerid
.addr
)
496 || memcmp(&addrs
[i
].addr
, &cerid
.addr
, sizeof(cerid
.addr
)) < 0)
497 cerid
.addr
= addrs
[i
].addr
.in6
;
502 } else if (otype
== DHCPV6_OPT_RAPID_COMMIT
&& hdr
->msg_type
== DHCPV6_MSG_SOLICIT
) {
503 iov
[IOV_RAPID_COMMIT
].iov_len
= sizeof(rapid_commit
);
504 o_rapid_commit
= true;
505 } else if (otype
== DHCPV6_OPT_ORO
) {
506 for (int i
=0; i
< olen
/2; i
++) {
507 uint16_t option
= ntohs(((uint16_t *)odata
)[i
]);
509 #ifdef DHCPV4_SUPPORT
510 case DHCPV6_OPT_4O6_SERVER
:
512 iov
[IOV_DHCPV4O6_SERVER
].iov_len
= sizeof(dhcpv4o6_server
);
514 #endif /* DHCPV4_SUPPORT */
522 if (!IN6_IS_ADDR_MULTICAST((struct in6_addr
*)dest_addr
) && iov
[IOV_NESTED
].iov_len
== 0 &&
523 (hdr
->msg_type
== DHCPV6_MSG_REQUEST
|| hdr
->msg_type
== DHCPV6_MSG_RENEW
||
524 hdr
->msg_type
== DHCPV6_MSG_RELEASE
|| hdr
->msg_type
== DHCPV6_MSG_DECLINE
)) {
525 iov
[IOV_STAT
].iov_base
= &stat
;
526 iov
[IOV_STAT
].iov_len
= sizeof(stat
);
528 for (ssize_t i
= IOV_STAT
+ 1; i
< IOV_TOTAL
; ++i
)
531 odhcpd_send(iface
->dhcpv6_event
.uloop
.fd
, addr
, iov
, ARRAY_SIZE(iov
), iface
);
535 if (hdr
->msg_type
== DHCPV6_MSG_SOLICIT
&& !o_rapid_commit
) {
536 dest
.msg_type
= DHCPV6_MSG_ADVERTISE
;
537 } else if (hdr
->msg_type
== DHCPV6_MSG_INFORMATION_REQUEST
) {
538 iov
[IOV_REFRESH
].iov_base
= &refresh
;
539 iov
[IOV_REFRESH
].iov_len
= sizeof(refresh
);
541 /* Return inf max rt option in reply to information request */
542 maxrt
.type
= htons(DHCPV6_OPT_INF_MAX_RT
);
545 #ifdef DHCPV4_SUPPORT
546 if (hdr
->msg_type
== DHCPV6_MSG_DHCPV4_QUERY
) {
547 struct _packed dhcpv4_msg_data
{
551 } *msg_opt
= (struct dhcpv4_msg_data
*)pdbuf
;
554 memset(pdbuf
, 0, sizeof(pdbuf
));
556 msglen
= dhcpv6_4o6_query(msg_opt
->msg
, sizeof(pdbuf
) - sizeof(*msg_opt
) + 1,
557 iface
, addr
, (const void *)hdr
, opts_end
);
559 syslog(LOG_ERR
, "4o6: query failed");
563 msg_opt
->type
= htons(DHCPV6_OPT_DHCPV4_MSG
);
564 msg_opt
->len
= htons(msglen
);
565 iov
[IOV_PDBUF
].iov_len
= sizeof(*msg_opt
) - 1 + msglen
;
566 dest
.msg_type
= DHCPV6_MSG_DHCPV4_RESPONSE
;
568 #endif /* DHCPV4_SUPPORT */
569 if (hdr
->msg_type
!= DHCPV6_MSG_INFORMATION_REQUEST
) {
570 ssize_t ialen
= dhcpv6_ia_handle_IAs(pdbuf
, sizeof(pdbuf
), iface
, addr
, (const void *)hdr
, opts_end
);
572 iov
[IOV_PDBUF
].iov_len
= ialen
;
574 (ialen
== 0 && (hdr
->msg_type
== DHCPV6_MSG_REBIND
|| hdr
->msg_type
== DHCPV6_MSG_CONFIRM
)))
578 if (iov
[IOV_NESTED
].iov_len
> 0) /* Update length */
579 update_nested_message(data
, len
, iov
[IOV_DEST
].iov_len
+ iov
[IOV_MAXRT
].iov_len
+
580 iov
[IOV_RAPID_COMMIT
].iov_len
+ iov
[IOV_DNS
].iov_len
+
581 iov
[IOV_DNS_ADDR
].iov_len
+ iov
[IOV_SEARCH
].iov_len
+
582 iov
[IOV_SEARCH_DOMAIN
].iov_len
+ iov
[IOV_PDBUF
].iov_len
+
583 iov
[IOV_DHCPV4O6_SERVER
].iov_len
+
584 iov
[IOV_CERID
].iov_len
+ iov
[IOV_DHCPV6_RAW
].iov_len
-
585 (4 + opts_end
- opts
));
587 syslog(LOG_DEBUG
, "Sending a DHCPv6-%s on %s", iov
[IOV_NESTED
].iov_len
? "relay-reply" : "reply", iface
->name
);
589 odhcpd_send(iface
->dhcpv6_event
.uloop
.fd
, addr
, iov
, ARRAY_SIZE(iov
), iface
);
593 /* Central DHCPv6-relay handler */
594 static void handle_dhcpv6(void *addr
, void *data
, size_t len
,
595 struct interface
*iface
, void *dest_addr
)
597 if (iface
->dhcpv6
== MODE_SERVER
) {
598 handle_client_request(addr
, data
, len
, iface
, dest_addr
);
599 } else if (iface
->dhcpv6
== MODE_RELAY
) {
601 relay_server_response(data
, len
);
603 relay_client_request(addr
, data
, len
, iface
);
608 /* Relay server response (regular relay server handling) */
609 static void relay_server_response(uint8_t *data
, size_t len
)
611 /* Information we need to gather */
612 uint8_t *payload_data
= NULL
;
613 size_t payload_len
= 0;
614 int32_t ifaceidx
= 0;
615 struct sockaddr_in6 target
= {AF_INET6
, htons(DHCPV6_CLIENT_PORT
),
616 0, IN6ADDR_ANY_INIT
, 0};
618 uint8_t *odata
, *end
= data
+ len
;
619 /* Relay DHCPv6 reply from server to client */
620 struct dhcpv6_relay_header
*h
= (void*)data
;
622 syslog(LOG_DEBUG
, "Got a DHCPv6-relay-reply");
624 if (len
< sizeof(*h
) || h
->msg_type
!= DHCPV6_MSG_RELAY_REPL
)
627 memcpy(&target
.sin6_addr
, &h
->peer_address
, sizeof(struct in6_addr
));
629 /* Go through options and find what we need */
630 dhcpv6_for_each_option(h
->options
, end
, otype
, olen
, odata
) {
631 if (otype
== DHCPV6_OPT_INTERFACE_ID
632 && olen
== sizeof(ifaceidx
)) {
633 memcpy(&ifaceidx
, odata
, sizeof(ifaceidx
));
634 } else if (otype
== DHCPV6_OPT_RELAY_MSG
) {
635 payload_data
= odata
;
640 /* Invalid interface-id or basic payload */
641 struct interface
*iface
= odhcpd_get_interface_by_index(ifaceidx
);
642 if (!iface
|| iface
->master
|| !payload_data
|| payload_len
< 4)
645 bool is_authenticated
= false;
646 struct in6_addr
*dns_ptr
= NULL
;
647 size_t dns_count
= 0;
649 /* If the payload is relay-reply we have to send to the server port */
650 if (payload_data
[0] == DHCPV6_MSG_RELAY_REPL
) {
651 target
.sin6_port
= htons(DHCPV6_SERVER_PORT
);
652 } else { /* Go through the payload data */
653 struct dhcpv6_client_header
*h
= (void*)payload_data
;
654 end
= payload_data
+ payload_len
;
656 dhcpv6_for_each_option(&h
[1], end
, otype
, olen
, odata
) {
657 if (otype
== DHCPV6_OPT_DNS_SERVERS
&& olen
>= 16) {
658 dns_ptr
= (struct in6_addr
*)odata
;
659 dns_count
= olen
/ 16;
660 } else if (otype
== DHCPV6_OPT_AUTH
) {
661 is_authenticated
= true;
666 /* Rewrite DNS servers if requested */
667 if (iface
->always_rewrite_dns
&& dns_ptr
&& dns_count
> 0) {
668 if (is_authenticated
)
669 return; /* Impossible to rewrite */
671 const struct in6_addr
*rewrite
= iface
->dns
;
672 struct in6_addr addr
;
673 size_t rewrite_cnt
= iface
->dns_cnt
;
675 if (rewrite_cnt
== 0) {
676 if (odhcpd_get_interface_dns_addr(iface
, &addr
))
677 return; /* Unable to get interface address */
683 /* Copy over any other addresses */
684 for (size_t i
= 0; i
< dns_count
; ++i
) {
685 size_t j
= (i
< rewrite_cnt
) ? i
: rewrite_cnt
- 1;
686 memcpy(&dns_ptr
[i
], &rewrite
[j
], sizeof(*rewrite
));
690 struct iovec iov
= {payload_data
, payload_len
};
692 syslog(LOG_DEBUG
, "Sending a DHCPv6-reply on %s", iface
->name
);
694 odhcpd_send(iface
->dhcpv6_event
.uloop
.fd
, &target
, &iov
, 1, iface
);
697 static struct odhcpd_ipaddr
*relay_link_address(struct interface
*iface
)
699 struct odhcpd_ipaddr
*addr
= NULL
;
700 time_t now
= odhcpd_time();
702 for (size_t i
= 0; i
< iface
->addr6_len
; i
++) {
703 if (iface
->addr6
[i
].valid
<= (uint32_t)now
)
706 if (iface
->addr6
[i
].preferred
> (uint32_t)now
) {
707 addr
= &iface
->addr6
[i
];
711 if (!addr
|| (iface
->addr6
[i
].valid
> addr
->valid
))
712 addr
= &iface
->addr6
[i
];
718 /* Relay client request (regular DHCPv6-relay) */
719 static void relay_client_request(struct sockaddr_in6
*source
,
720 const void *data
, size_t len
, struct interface
*iface
)
722 const struct dhcpv6_relay_header
*h
= data
;
723 /* Construct our forwarding envelope */
724 struct dhcpv6_relay_forward_envelope hdr
= {
725 .msg_type
= DHCPV6_MSG_RELAY_FORW
,
727 .interface_id_type
= htons(DHCPV6_OPT_INTERFACE_ID
),
728 .interface_id_len
= htons(sizeof(uint32_t)),
729 .relay_message_type
= htons(DHCPV6_OPT_RELAY_MSG
),
730 .relay_message_len
= htons(len
),
732 struct iovec iov
[2] = {{&hdr
, sizeof(hdr
)}, {(void *)data
, len
}};
734 struct odhcpd_ipaddr
*ip
;
735 struct sockaddr_in6 s
;
737 if (h
->msg_type
== DHCPV6_MSG_RELAY_REPL
||
738 h
->msg_type
== DHCPV6_MSG_RECONFIGURE
||
739 h
->msg_type
== DHCPV6_MSG_REPLY
||
740 h
->msg_type
== DHCPV6_MSG_ADVERTISE
)
741 return; /* Invalid message types for client */
743 syslog(LOG_DEBUG
, "Got a DHCPv6-request on %s", iface
->name
);
745 if (h
->msg_type
== DHCPV6_MSG_RELAY_FORW
) { /* handle relay-forward */
746 if (h
->hop_count
>= DHCPV6_HOP_COUNT_LIMIT
)
747 return; /* Invalid hop count */
749 hdr
.hop_count
= h
->hop_count
+ 1;
752 /* use memcpy here as the destination fields are unaligned */
753 memcpy(&hdr
.peer_address
, &source
->sin6_addr
, sizeof(struct in6_addr
));
754 memcpy(&hdr
.interface_id_data
, &iface
->ifindex
, sizeof(iface
->ifindex
));
756 /* Detect public IP of slave interface to use as link-address */
757 ip
= relay_link_address(iface
);
759 memcpy(&hdr
.link_address
, &ip
->addr
.in6
, sizeof(hdr
.link_address
));
761 memset(&s
, 0, sizeof(s
));
762 s
.sin6_family
= AF_INET6
;
763 s
.sin6_port
= htons(DHCPV6_SERVER_PORT
);
764 inet_pton(AF_INET6
, ALL_DHCPV6_SERVERS
, &s
.sin6_addr
);
766 avl_for_each_element(&interfaces
, c
, avl
) {
767 if (!c
->master
|| c
->dhcpv6
!= MODE_RELAY
)
771 /* No suitable address! Is the slave not configured yet?
772 * Detect public IP of master interface and use it instead
773 * This is WRONG and probably violates the RFC. However
774 * otherwise we have a hen and egg problem because the
775 * slave-interface cannot be auto-configured. */
776 ip
= relay_link_address(c
);
778 continue; /* Could not obtain a suitable address */
780 memcpy(&hdr
.link_address
, &ip
->addr
.in6
, sizeof(hdr
.link_address
));
784 syslog(LOG_DEBUG
, "Sending a DHCPv6-relay-forward on %s", c
->name
);
786 odhcpd_send(c
->dhcpv6_event
.uloop
.fd
, &s
, iov
, 2, c
);