3 * Copyright (c) 2019-2020 Apple Computer, Inc. All rights reserved.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
18 #ifndef __SERVICE_REGISTRATION_ROUTE_H
19 #define __SERVICE_REGISTRATION_ROUTE_H
20 #if defined(USE_IPCONFIGURATION_SERVICE)
21 #include <SystemConfiguration/SystemConfiguration.h>
22 #include "IPConfigurationService.h"
25 #define MIN_DELAY_BETWEEN_RAS 4000
26 #define MSEC_PER_SEC (NSEC_PER_SEC / NSEC_PER_MSEC)
27 #define MAX_ROUTER_RECEIVED_TIME_GAP_BEFORE_STALE 600 * MSEC_PER_SEC
30 #ifndef IPV6_ROUTER_MODE_EXCLUSIVE
31 #define IPV6_ROUTER_MODE_DISABLED 0
32 #define IPV6_ROUTER_MODE_EXCLUSIVE 1
33 #define IPV6_ROUTER_MODE_HYBRID 2
37 typedef struct interface interface_t
;
38 typedef struct icmp_message icmp_message_t
;
39 typedef struct network_link network_link_t
;
43 interface_t
*NULLABLE next
;
46 // Wakeup event for next beacon.
47 wakeup_t
*NULLABLE beacon_wakeup
;
49 // Wakeup event called after we're done sending solicits. At this point we delete all routes more than 10 minutes
50 // old; if none are left, then we assume there's no IPv6 service on the interface.
51 wakeup_t
*NULLABLE post_solicit_wakeup
;
53 // Wakeup event to trigger the next router solicit to be sent.
54 wakeup_t
*NULLABLE router_solicit_wakeup
;
56 // Wakeup event to deconfigure the on-link prefix after it is no longer valid.
57 wakeup_t
*NULLABLE deconfigure_wakeup
;
59 // Wakeup event to detect that vicarious router discovery is complete
60 wakeup_t
*NULLABLE vicarious_discovery_complete
;
62 // List of ICMP messages from different routers.
63 icmp_message_t
*NULLABLE routers
;
65 // The link to which this interface is connected.
66 network_link_t
*NULLABLE link
;
68 #if defined(USE_IPCONFIGURATION_SERVICE)
69 // The service used to configure this interface with an address in the on-link prefix
70 IPConfigurationServiceRef NULLABLE ip_configuration_service
;
73 SCDynamicStoreRef NULLABLE ip_configuration_store
;
76 struct in6_addr link_local
; // Link-local address
77 struct in6_addr ipv6_prefix
; // This is the prefix we advertise, if advertise_ipv6_prefix is true.
79 // Absolute time of last beacon, and of next beacon.
80 uint64_t last_beacon
, next_beacon
;
82 // Absolute deadline for deprecating the on-link prefix we've been announcing
83 uint64_t deprecate_deadline
;
85 // Preferred lifetime for the on-link prefix
86 uint32_t preferred_lifetime
;
88 // Valid lifetime for the on-link prefix
89 uint32_t valid_lifetime
;
91 // When the interface becomes active, we send up to three solicits.
92 int num_solicits_sent
;
94 // The interface index according to the operating systme.
97 // Number of IPv4 addresses configured on link.
98 int num_ipv4_addresses
;
100 // Number of beacons sent. After the first three, the inter-beacon interval goes up.
101 int num_beacons_sent
;
103 // The interface link layer address, if known.
104 uint8_t link_layer
[6];
106 // True if the interface is not usable.
109 // True if this interface can never be used for routing to the thread network (e.g., loopback, tunnels, etc.)
112 // True if we've determined that it's the thread interface.
115 // True if we should advertise an on-link prefix.
116 bool advertise_ipv6_prefix
;
118 // True if we've gotten a link-layer address.
119 bool have_link_layer_address
;
121 // True if the on-link prefix is configured on the interface.
122 bool on_link_prefix_configured
;
124 // True if we've sent our first beacon since the interface came up.
125 bool sent_first_beacon
;
127 // Indicates whether or not router discovery has completed for this interface.
128 bool router_discovery_complete
;
130 // Indicates whether we're currently doing router discovery, so that we don't
131 // restart it when we're already doing it.
132 bool router_discovery_in_progress
;
134 // Indicates that we've received a router discovery message from some other host,
135 // and are waiting 20 seconds to snoop for replies to that RD message that are
136 // multicast. If we hear no replies during that time, we trigger router discovery.
137 bool vicarious_router_discovery_in_progress
;
140 typedef enum icmp_option_type
{
141 icmp_option_source_link_layer_address
= 1,
142 icmp_option_target_link_layer_address
= 2,
143 icmp_option_prefix_information
= 3,
144 icmp_option_redirected_header
= 4,
146 icmp_option_route_information
= 24,
147 } icmp_option_type_t
;
149 typedef enum icmp_type
{
150 icmp_type_echo_request
= 128,
151 icmp_type_echo_reply
= 129,
152 icmp_type_router_solicitation
= 133,
153 icmp_type_router_advertisement
= 134,
154 icmp_type_neighbor_solicitation
= 135,
155 icmp_type_neighbor_advertisement
= 136,
156 icmp_type_redirect
= 137,
159 typedef struct link_layer_address
{
162 } link_layer_address_t
;
164 typedef struct prefix_information
{
165 struct in6_addr prefix
;
168 uint32_t valid_lifetime
;
169 uint32_t preferred_lifetime
;
170 } prefix_information_t
;
172 typedef struct route_information
{
173 struct in6_addr prefix
;
176 uint32_t route_lifetime
;
177 } route_information_t
;
179 typedef struct icmp_option
{
180 icmp_option_type_t type
;
182 link_layer_address_t link_layer_address
;
183 prefix_information_t prefix_information
;
184 route_information_t route_information
;
188 struct icmp_message
{
189 icmp_message_t
*NULLABLE next
;
190 interface_t
*NULLABLE interface
;
191 icmp_option_t
*NULLABLE options
;
192 bool new_router
; // If this router information is a newly recevied one.
193 bool received_time_already_adjusted
; // if the received time of the message is already adjusted by
195 struct in6_addr source
;
196 struct in6_addr destination
;
198 uint64_t received_time
;
200 uint32_t reachable_time
;
201 uint32_t retransmission_timer
;
202 uint8_t cur_hop_limit
; // Current hop limit for Router Advertisement messages.
206 uint16_t checksum
; // We hope the kernel figures this out for us.
207 uint16_t router_lifetime
;
210 int hop_limit
; // Hop limit provided by the kernel, must be 255.
213 void ula_generate(void);
214 bool start_route_listener(void);
215 bool start_icmp_listener(void);
216 void icmp_leave_join(int sock
, int ifindex
, bool join
);
218 #define interface_create(name, iface) interface_create_(name, iface, __FILE__, __LINE__)
219 interface_t
*NULLABLE
interface_create_(const char *NONNULL name
, int ifindex
,
220 const char *NONNULL file
, int line
);
221 #define interface_retain(interface) interface_retain_(interface, __FILE__, __LINE__)
222 void interface_retain_(interface_t
*NONNULL interface
, const char *NONNULL file
, int line
);
223 #define interface_release(interface) interface_release_(interface, __FILE__, __LINE__)
224 void interface_release_(interface_t
*NONNULL interface
, const char *NONNULL file
, int line
);
225 bool interface_monitor_start(void);
226 void thread_network_startup(void);
227 void thread_network_shutdown(void);
228 void partition_stop_advertising_pref_id(void);
229 void partition_start_srp_listener(void);
230 #endif // __SERVICE_REGISTRATION_ROUTE_H
235 // c-file-style: "bsd"
238 // indent-tabs-mode: nil