omr-publisher.c revision 1.1 1 1.1 christos /* omr-publisher.c
2 1.1 christos *
3 1.1 christos * Copyright (c) 2023-2024 Apple Inc. All rights reserved.
4 1.1 christos *
5 1.1 christos * Licensed under the Apache License, Version 2.0 (the "License");
6 1.1 christos * you may not use this file except in compliance with the License.
7 1.1 christos * You may obtain a copy of the License at
8 1.1 christos *
9 1.1 christos * https://www.apache.org/licenses/LICENSE-2.0
10 1.1 christos *
11 1.1 christos * Unless required by applicable law or agreed to in writing, software
12 1.1 christos * distributed under the License is distributed on an "AS IS" BASIS,
13 1.1 christos * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 1.1 christos * See the License for the specific language governing permissions and
15 1.1 christos * limitations under the License.
16 1.1 christos *
17 1.1 christos * This file contains an implementation of the Off-Mesh-Routable (OMR)
18 1.1 christos * prefix publisher state machine.
19 1.1 christos */
20 1.1 christos
21 1.1 christos #include <stdlib.h>
22 1.1 christos #include <string.h>
23 1.1 christos #include <stdio.h>
24 1.1 christos #include <unistd.h>
25 1.1 christos #include <pwd.h>
26 1.1 christos #include <errno.h>
27 1.1 christos #include <sys/socket.h>
28 1.1 christos #include <netinet/in.h>
29 1.1 christos #include <arpa/inet.h>
30 1.1 christos #include <fcntl.h>
31 1.1 christos #include <time.h>
32 1.1 christos #include <dns_sd.h>
33 1.1 christos #include <net/if.h>
34 1.1 christos #include <inttypes.h>
35 1.1 christos #include <sys/resource.h>
36 1.1 christos #include "srp.h"
37 1.1 christos #include "dns-msg.h"
38 1.1 christos #include "srp-crypto.h"
39 1.1 christos #include "ioloop.h"
40 1.1 christos #include "srp-gw.h"
41 1.1 christos #include "srp-proxy.h"
42 1.1 christos #include "srp-mdns-proxy.h"
43 1.1 christos #include "config-parse.h"
44 1.1 christos #include "cti-services.h"
45 1.1 christos #include "route.h"
46 1.1 christos #include "dnssd-proxy.h"
47 1.1 christos
48 1.1 christos
49 1.1 christos #define STATE_MACHINE_IMPLEMENTATION 1
50 1.1 christos typedef enum {
51 1.1 christos omr_publisher_state_invalid,
52 1.1 christos omr_publisher_state_startup,
53 1.1 christos omr_publisher_state_check_for_dhcp,
54 1.1 christos omr_publisher_state_not_publishing,
55 1.1 christos omr_publisher_state_publishing_dhcp,
56 1.1 christos omr_publisher_state_publishing_ula,
57 1.1 christos } state_machine_state_t;
58 1.1 christos #define state_machine_state_invalid omr_publisher_state_invalid
59 1.1 christos
60 1.1 christos #include "state-machine.h"
61 1.1 christos #include "service-publisher.h"
62 1.1 christos #include "thread-service.h"
63 1.1 christos #include "omr-watcher.h"
64 1.1 christos #include "omr-publisher.h"
65 1.1 christos #include "route-tracker.h"
66 1.1 christos
67 1.1 christos typedef struct omr_publisher {
68 1.1 christos int ref_count;
69 1.1 christos state_machine_header_t state_header;
70 1.1 christos
71 1.1 christos route_state_t *route_state;
72 1.1 christos wakeup_t *NULLABLE wakeup_timer;
73 1.1 christos void *dhcp_client;
74 1.1 christos omr_watcher_callback_t *omr_watcher_callback;
75 1.1 christos omr_watcher_t *omr_watcher;
76 1.1 christos void (*reconnect_callback)(void *context);
77 1.1 christos
78 1.1 christos omr_prefix_t *published_prefix;
79 1.1 christos omr_prefix_t *publication_queue;
80 1.1 christos interface_t *dhcp_interface;
81 1.1 christos struct in6_addr ula_prefix;
82 1.1 christos struct in6_addr dhcp_prefix;
83 1.1 christos struct in6_addr new_dhcp_prefix;
84 1.1 christos int dhcp_prefix_length, new_dhcp_prefix_length;
85 1.1 christos uint32_t dhcp_preferred_lifetime, new_dhcp_preferred_lifetime;
86 1.1 christos int min_start;
87 1.1 christos omr_prefix_priority_t omr_priority, force_priority;
88 1.1 christos bool ula_prefix_published, dhcp_prefix_published;
89 1.1 christos bool dhcp_wanted;
90 1.1 christos bool dhcp_blocked;
91 1.1 christos bool first_time;
92 1.1 christos bool force_publication;
93 1.1 christos } omr_publisher_t;
94 1.1 christos
95 1.1 christos \
96 1.1 christos #define STATE_MACHINE_HEADER_TO_PUBLISHER(state_header) \
97 1.1 christos if (state_header->state_machine_type != state_machine_type_omr_publisher) { \
98 1.1 christos ERROR("state header type isn't omr_publisher: %d", state_header->state_machine_type); \
99 1.1 christos return omr_publisher_state_invalid; \
100 1.1 christos } \
101 1.1 christos omr_publisher_t *publisher = state_header->state_object
102 1.1 christos
103 1.1 christos static state_machine_state_t omr_publisher_action_startup(state_machine_header_t *state_header,
104 1.1 christos state_machine_event_t *event);
105 1.1 christos static state_machine_state_t omr_publisher_action_check_for_dhcp(state_machine_header_t *state_header,
106 1.1 christos state_machine_event_t *event);
107 1.1 christos static state_machine_state_t omr_publisher_action_not_publishing(state_machine_header_t *state_header,
108 1.1 christos state_machine_event_t *event);
109 1.1 christos static state_machine_state_t omr_publisher_action_publishing_dhcp(state_machine_header_t *state_header,
110 1.1 christos state_machine_event_t *event);
111 1.1 christos static state_machine_state_t omr_publisher_action_publishing_ula(state_machine_header_t *state_header,
112 1.1 christos state_machine_event_t *event);
113 1.1 christos
114 1.1 christos #define OMR_PUB_NAME_DECL(name) omr_publisher_state_##name, #name
115 1.1 christos static state_machine_decl_t omr_publisher_states[] = {
116 1.1 christos { OMR_PUB_NAME_DECL(invalid), NULL },
117 1.1 christos { OMR_PUB_NAME_DECL(startup), omr_publisher_action_startup },
118 1.1 christos { OMR_PUB_NAME_DECL(check_for_dhcp), omr_publisher_action_check_for_dhcp },
119 1.1 christos { OMR_PUB_NAME_DECL(not_publishing), omr_publisher_action_not_publishing },
120 1.1 christos { OMR_PUB_NAME_DECL(publishing_dhcp), omr_publisher_action_publishing_dhcp },
121 1.1 christos { OMR_PUB_NAME_DECL(publishing_ula), omr_publisher_action_publishing_ula },
122 1.1 christos };
123 1.1 christos #define OMR_PUBLISHER_NUM_CONNECTION_STATES ((sizeof(omr_publisher_states)) / (sizeof(state_machine_decl_t)))
124 1.1 christos
125 1.1 christos static void omr_publisher_discontinue_dhcp(omr_publisher_t *publisher);
126 1.1 christos static void omr_publisher_queue_prefix_update(omr_publisher_t *publisher, struct in6_addr *prefix_address,
127 1.1 christos omr_prefix_priority_t priority, bool preferred,
128 1.1 christos thread_service_publication_state_t initial_state);
129 1.1 christos
130 1.1 christos static void
131 1.1 christos omr_publisher_finalize(omr_publisher_t *publisher)
132 1.1 christos {
133 1.1 christos free(publisher->state_header.name);
134 1.1 christos free(publisher);
135 1.1 christos }
136 1.1 christos RELEASE_RETAIN_FUNCS(omr_publisher);
137 1.1 christos
138 1.1 christos static void
139 1.1 christos omr_publisher_context_release(route_state_t *UNUSED route_state, void *context)
140 1.1 christos {
141 1.1 christos omr_publisher_t *publisher = context;
142 1.1 christos RELEASE_HERE(publisher, omr_publisher);
143 1.1 christos }
144 1.1 christos
145 1.1 christos static void
146 1.1 christos omr_publisher_event_prefix_update_finished_finalize(state_machine_event_t *event)
147 1.1 christos {
148 1.1 christos if (event->thread_prefixes != NULL) {
149 1.1 christos omr_prefix_release(event->thread_prefixes);
150 1.1 christos event->thread_prefixes = NULL;
151 1.1 christos }
152 1.1 christos }
153 1.1 christos
154 1.1 christos static void
155 1.1 christos omr_publisher_watcher_callback(route_state_t *UNUSED NONNULL route_state, void *NULLABLE context, omr_watcher_event_type_t event_type,
156 1.1 christos omr_prefix_t *NULLABLE prefixes, omr_prefix_t *NULLABLE prefix)
157 1.1 christos {
158 1.1 christos omr_publisher_t *publisher = context;
159 1.1 christos
160 1.1 christos // On startup, notice any prefixes with the user flag set: we didn't publish these, so they must be left over from
161 1.1 christos // a crash or restart.
162 1.1 christos if (publisher->first_time && event_type == omr_watcher_event_prefix_added && prefix != NULL && prefix->user) {
163 1.1 christos // We might actually publish our own prefix before we go through this loop for the first time. If that's the case,
164 1.1 christos // don't unpublish it!
165 1.1 christos if (publisher->published_prefix == NULL ||
166 1.1 christos in6addr_compare(&prefix->prefix, &publisher->published_prefix->prefix))
167 1.1 christos {
168 1.1 christos SEGMENTED_IPv6_ADDR_GEN_SRP(prefix->prefix.s6_addr, prefix_buf);
169 1.1 christos INFO("removing stale prefix " PRI_SEGMENTED_IPv6_ADDR_SRP "/%d",
170 1.1 christos SEGMENTED_IPv6_ADDR_PARAM_SRP(prefix->prefix.s6_addr, prefix_buf), prefix->prefix_length);
171 1.1 christos omr_publisher_queue_prefix_update(publisher, &prefix->prefix, omr_prefix_priority_low, false, want_delete);
172 1.1 christos }
173 1.1 christos }
174 1.1 christos
175 1.1 christos // We don't otherwise care about "prefix appeared" and "prefix disappeared" events, nor about flag change events--
176 1.1 christos // just about the final state
177 1.1 christos if (event_type == omr_watcher_event_prefix_update_finished) {
178 1.1 christos state_machine_event_t *event = state_machine_event_create(state_machine_event_type_prefix,
179 1.1 christos omr_publisher_event_prefix_update_finished_finalize);
180 1.1 christos if (event == NULL) {
181 1.1 christos ERROR("unable to allocate event to deliver");
182 1.1 christos return;
183 1.1 christos }
184 1.1 christos event->thread_prefixes = prefixes;
185 1.1 christos if (prefixes != NULL) {
186 1.1 christos omr_prefix_retain(event->thread_prefixes);
187 1.1 christos }
188 1.1 christos state_machine_event_deliver(&publisher->state_header, event);
189 1.1 christos RELEASE_HERE(event, state_machine_event);
190 1.1 christos
191 1.1 christos publisher->first_time = false;
192 1.1 christos }
193 1.1 christos }
194 1.1 christos
195 1.1 christos void
196 1.1 christos omr_publisher_set_omr_watcher(omr_publisher_t *publisher, omr_watcher_t *watcher)
197 1.1 christos {
198 1.1 christos if (watcher != NULL) {
199 1.1 christos publisher->omr_watcher = watcher;
200 1.1 christos omr_watcher_retain(publisher->omr_watcher);
201 1.1 christos publisher->omr_watcher_callback = omr_watcher_callback_add(publisher->omr_watcher,
202 1.1 christos omr_publisher_watcher_callback,
203 1.1 christos omr_publisher_context_release, publisher);
204 1.1 christos RETAIN_HERE(publisher, omr_publisher); // The omr watcher callback holds a reference to the publisher
205 1.1 christos }
206 1.1 christos }
207 1.1 christos
208 1.1 christos void omr_publisher_set_reconnect_callback(omr_publisher_t *NONNULL publisher,
209 1.1 christos void (*NULLABLE reconnect_callback)(void *NULLABLE context))
210 1.1 christos {
211 1.1 christos publisher->reconnect_callback = reconnect_callback;
212 1.1 christos }
213 1.1 christos
214 1.1 christos void
215 1.1 christos omr_publisher_cancel(omr_publisher_t *publisher)
216 1.1 christos {
217 1.1 christos if (publisher->wakeup_timer != NULL) {
218 1.1 christos ioloop_cancel_wake_event(publisher->wakeup_timer);
219 1.1 christos ioloop_wakeup_release(publisher->wakeup_timer);
220 1.1 christos publisher->wakeup_timer = NULL;
221 1.1 christos }
222 1.1 christos if (publisher->omr_watcher_callback != NULL) {
223 1.1 christos omr_watcher_callback_cancel(publisher->omr_watcher, publisher->omr_watcher_callback);
224 1.1 christos publisher->omr_watcher_callback = NULL;
225 1.1 christos }
226 1.1 christos if (publisher->omr_watcher != NULL) {
227 1.1 christos omr_watcher_release(publisher->omr_watcher);
228 1.1 christos publisher->omr_watcher = NULL;
229 1.1 christos }
230 1.1 christos if (publisher->published_prefix != NULL) {
231 1.1 christos omr_publisher_unpublish_prefix(publisher);
232 1.1 christos }
233 1.1 christos if (publisher->dhcp_client) {
234 1.1 christos omr_publisher_discontinue_dhcp(publisher);
235 1.1 christos }
236 1.1 christos if (publisher->dhcp_interface != NULL) {
237 1.1 christos interface_release(publisher->dhcp_interface);
238 1.1 christos publisher->dhcp_interface = NULL;
239 1.1 christos }
240 1.1 christos state_machine_cancel(&publisher->state_header);
241 1.1 christos }
242 1.1 christos
243 1.1 christos omr_publisher_t *
244 1.1 christos omr_publisher_create(route_state_t *route_state, const char *name)
245 1.1 christos {
246 1.1 christos omr_publisher_t *ret = NULL, *publisher = calloc(1, sizeof(*publisher));
247 1.1 christos if (publisher == NULL) {
248 1.1 christos return publisher;
249 1.1 christos }
250 1.1 christos RETAIN_HERE(publisher, omr_publisher);
251 1.1 christos publisher->wakeup_timer = ioloop_wakeup_create();
252 1.1 christos if (publisher->wakeup_timer == NULL) {
253 1.1 christos ERROR("wakeup timer alloc failed");
254 1.1 christos goto out;
255 1.1 christos }
256 1.1 christos
257 1.1 christos if (!state_machine_header_setup(&publisher->state_header,
258 1.1 christos publisher, name,
259 1.1 christos state_machine_type_omr_publisher,
260 1.1 christos omr_publisher_states,
261 1.1 christos OMR_PUBLISHER_NUM_CONNECTION_STATES)) {
262 1.1 christos ERROR("header setup failed");
263 1.1 christos goto out;
264 1.1 christos }
265 1.1 christos
266 1.1 christos publisher->route_state = route_state;
267 1.1 christos
268 1.1 christos // Set the first_time flag so that we'll know to remove any locally-published on-mesh prefixes.
269 1.1 christos publisher->first_time = true;
270 1.1 christos publisher->min_start = OMR_PUBLISHER_MIN_START;
271 1.1 christos
272 1.1 christos ret = publisher;
273 1.1 christos publisher = NULL;
274 1.1 christos out:
275 1.1 christos if (publisher != NULL) {
276 1.1 christos RELEASE_HERE(publisher, omr_publisher);
277 1.1 christos }
278 1.1 christos return ret;
279 1.1 christos }
280 1.1 christos
281 1.1 christos void
282 1.1 christos omr_publisher_start(omr_publisher_t *publisher)
283 1.1 christos {
284 1.1 christos state_machine_next_state(&publisher->state_header, omr_publisher_state_startup);
285 1.1 christos }
286 1.1 christos
287 1.1 christos void
288 1.1 christos omr_publisher_force_publication(omr_publisher_t *publisher, omr_prefix_priority_t priority)
289 1.1 christos {
290 1.1 christos publisher->force_publication = true;
291 1.1 christos if (publisher->state_header.state == omr_publisher_state_publishing_dhcp ||
292 1.1 christos publisher->state_header.state == omr_publisher_state_publishing_ula)
293 1.1 christos {
294 1.1 christos ERROR("already publishing");
295 1.1 christos return;
296 1.1 christos } else {
297 1.1 christos INFO("forcing publication");
298 1.1 christos }
299 1.1 christos publisher->force_publication = true;
300 1.1 christos publisher->force_priority = priority;
301 1.1 christos state_machine_next_state(&publisher->state_header, omr_publisher_state_publishing_ula);
302 1.1 christos }
303 1.1 christos
304 1.1 christos omr_prefix_t *NULLABLE
305 1.1 christos omr_publisher_published_prefix_get(omr_publisher_t *publisher)
306 1.1 christos {
307 1.1 christos return publisher->published_prefix;
308 1.1 christos }
309 1.1 christos
310 1.1 christos static void
311 1.1 christos omr_publisher_wait_expired(void *context)
312 1.1 christos {
313 1.1 christos omr_publisher_t *publisher = context;
314 1.1 christos state_machine_event_t *event = state_machine_event_create(state_machine_event_type_timeout, NULL);
315 1.1 christos if (event == NULL) {
316 1.1 christos ERROR("unable to allocate event to deliver");
317 1.1 christos return;
318 1.1 christos }
319 1.1 christos state_machine_event_deliver(&publisher->state_header, event);
320 1.1 christos RELEASE_HERE(event, state_machine_event);
321 1.1 christos }
322 1.1 christos
323 1.1 christos static void
324 1.1 christos omr_publisher_wakeup_release(void *context)
325 1.1 christos {
326 1.1 christos omr_publisher_t *publisher = context;
327 1.1 christos RELEASE_HERE(publisher, omr_publisher);
328 1.1 christos }
329 1.1 christos
330 1.1 christos static void
331 1.1 christos omr_publisher_send_dhcp_event(omr_publisher_t *publisher, struct in6_addr *prefix, int prefix_length, uint32_t preferred_lifetime)
332 1.1 christos {
333 1.1 christos state_machine_event_t *event = state_machine_event_create(state_machine_event_type_dhcp, NULL);
334 1.1 christos if (event == NULL) {
335 1.1 christos ERROR("unable to allocate event to deliver");
336 1.1 christos return;
337 1.1 christos }
338 1.1 christos if (prefix == NULL) {
339 1.1 christos SEGMENTED_IPv6_ADDR_GEN_SRP(&publisher->dhcp_prefix.s6_addr, prefix_buf);
340 1.1 christos INFO("DHCPv6 prefix withdrawn " PRI_SEGMENTED_IPv6_ADDR_SRP "/%d, lifetime = %d",
341 1.1 christos SEGMENTED_IPv6_ADDR_PARAM_SRP(&publisher->dhcp_prefix.s6_addr, prefix_buf),
342 1.1 christos publisher->dhcp_prefix_length, preferred_lifetime);
343 1.1 christos in6addr_zero(&publisher->new_dhcp_prefix);
344 1.1 christos } else {
345 1.1 christos SEGMENTED_IPv6_ADDR_GEN_SRP(prefix->s6_addr, prefix_buf);
346 1.1 christos INFO("received DHCPv6 prefix " PRI_SEGMENTED_IPv6_ADDR_SRP "/%d, lifetime = %d",
347 1.1 christos SEGMENTED_IPv6_ADDR_PARAM_SRP(prefix->s6_addr, prefix_buf), prefix_length, preferred_lifetime);
348 1.1 christos publisher->new_dhcp_prefix = *prefix;
349 1.1 christos }
350 1.1 christos publisher->new_dhcp_prefix_length = prefix_length;
351 1.1 christos publisher->new_dhcp_preferred_lifetime = preferred_lifetime;
352 1.1 christos state_machine_event_deliver(&publisher->state_header, event);
353 1.1 christos RELEASE_HERE(event, state_machine_event);
354 1.1 christos }
355 1.1 christos
356 1.1 christos
357 1.1 christos static void
358 1.1 christos omr_publisher_initiate_dhcp(omr_publisher_t *publisher)
359 1.1 christos {
360 1.1 christos if (!publisher->dhcp_blocked) {
361 1.1 christos publisher->dhcp_wanted = true;
362 1.1 christos }
363 1.1 christos publisher->dhcp_client = (void *)-1;
364 1.1 christos }
365 1.1 christos
366 1.1 christos void
367 1.1 christos omr_publisher_interface_configuration_changed(omr_publisher_t *publisher)
368 1.1 christos {
369 1.1 christos // Check to see if DHCP interface became inactive/ineligible
370 1.1 christos if (publisher->dhcp_interface != NULL) {
371 1.1 christos if (publisher->dhcp_interface->inactive || publisher->dhcp_interface->ineligible) {
372 1.1 christos // If we have a DHCPv6 client running, we need to discontinue it.
373 1.1 christos if (publisher->dhcp_client != NULL) {
374 1.1 christos omr_publisher_dhcp_client_deactivate(publisher, (intptr_t)publisher->dhcp_client);
375 1.1 christos }
376 1.1 christos interface_release(publisher->dhcp_interface);
377 1.1 christos publisher->dhcp_interface = NULL;
378 1.1 christos }
379 1.1 christos }
380 1.1 christos if (publisher->dhcp_wanted && publisher->dhcp_client == NULL) {
381 1.1 christos // Start DHCP on the new interface (if there is one)
382 1.1 christos omr_publisher_initiate_dhcp(publisher);
383 1.1 christos }
384 1.1 christos }
385 1.1 christos
386 1.1 christos static void
387 1.1 christos omr_publisher_discontinue_dhcp(omr_publisher_t *publisher)
388 1.1 christos {
389 1.1 christos INFO("discontinuing DHCP PD client");
390 1.1 christos omr_publisher_dhcp_client_deactivate(publisher, (intptr_t)publisher->dhcp_client);
391 1.1 christos publisher->dhcp_wanted = false;
392 1.1 christos }
393 1.1 christos
394 1.1 christos static bool
395 1.1 christos omr_publisher_dhcp_prefix_available(omr_publisher_t *publisher)
396 1.1 christos {
397 1.1 christos if (publisher->new_dhcp_preferred_lifetime != 0 && publisher->new_dhcp_prefix_length <= 64) {
398 1.1 christos return true;
399 1.1 christos }
400 1.1 christos return false;
401 1.1 christos }
402 1.1 christos
403 1.1 christos static bool
404 1.1 christos omr_publisher_dhcp_prefix_changed(omr_publisher_t *publisher)
405 1.1 christos {
406 1.1 christos if (publisher->new_dhcp_prefix_length != publisher->dhcp_prefix_length ||
407 1.1 christos publisher->new_dhcp_preferred_lifetime != publisher->dhcp_preferred_lifetime ||
408 1.1 christos in6addr_compare(&publisher->new_dhcp_prefix, &publisher->dhcp_prefix))
409 1.1 christos {
410 1.1 christos return true;
411 1.1 christos }
412 1.1 christos return false;
413 1.1 christos }
414 1.1 christos
415 1.1 christos static bool
416 1.1 christos omr_publisher_dhcp_prefix_lost(omr_publisher_t *publisher)
417 1.1 christos {
418 1.1 christos if (publisher->new_dhcp_prefix_length == 0) {
419 1.1 christos return true;
420 1.1 christos }
421 1.1 christos return false;
422 1.1 christos }
423 1.1 christos
424 1.1 christos static void
425 1.1 christos omr_publisher_install_new_dhcp_prefix(omr_publisher_t *publisher)
426 1.1 christos {
427 1.1 christos publisher->dhcp_prefix = publisher->new_dhcp_prefix;
428 1.1 christos publisher->dhcp_prefix_length = publisher->new_dhcp_prefix_length;
429 1.1 christos publisher->dhcp_preferred_lifetime = publisher->new_dhcp_preferred_lifetime;
430 1.1 christos }
431 1.1 christos
432 1.1 christos static void
433 1.1 christos omr_publisher_prefix_init_from_published(omr_publisher_t *publisher, struct in6_addr *prefix, int *prefix_length)
434 1.1 christos {
435 1.1 christos if (publisher->published_prefix != NULL) {
436 1.1 christos in6addr_copy(prefix, &publisher->published_prefix->prefix);
437 1.1 christos *prefix_length = publisher->published_prefix->prefix_length;
438 1.1 christos } else {
439 1.1 christos in6addr_zero(prefix);
440 1.1 christos *prefix_length = 64;
441 1.1 christos }
442 1.1 christos }
443 1.1 christos
444 1.1 christos static bool
445 1.1 christos omr_publisher_prefix_present(omr_publisher_t *publisher, omr_prefix_priority_t priority)
446 1.1 christos {
447 1.1 christos struct in6_addr prefix;
448 1.1 christos int prefix_length;
449 1.1 christos if (publisher->omr_watcher == NULL) {
450 1.1 christos FAULT("expecting an omr_watcher to be on the publisher");
451 1.1 christos return true; // Saying yes because we have no watcher and hence can't behave correctly.
452 1.1 christos }
453 1.1 christos omr_publisher_prefix_init_from_published(publisher, &prefix, &prefix_length);
454 1.1 christos return omr_watcher_prefix_present(publisher->omr_watcher, priority, &prefix, prefix_length);
455 1.1 christos }
456 1.1 christos
457 1.1 christos static bool
458 1.1 christos omr_publisher_high_prefix_present(omr_publisher_t *publisher)
459 1.1 christos {
460 1.1 christos bool ret = omr_publisher_prefix_present(publisher, omr_prefix_priority_high);
461 1.1 christos if (ret) {
462 1.1 christos INFO("setting publisher->omr_priority to high");
463 1.1 christos publisher->omr_priority = omr_prefix_priority_high;
464 1.1 christos }
465 1.1 christos return ret;
466 1.1 christos }
467 1.1 christos
468 1.1 christos static bool
469 1.1 christos omr_publisher_medium_prefix_present(omr_publisher_t *publisher)
470 1.1 christos {
471 1.1 christos bool ret = omr_publisher_prefix_present(publisher, omr_prefix_priority_medium);
472 1.1 christos if (ret) {
473 1.1 christos INFO("setting publisher->omr_priority to medium");
474 1.1 christos publisher->omr_priority = omr_prefix_priority_medium;
475 1.1 christos }
476 1.1 christos return ret;
477 1.1 christos }
478 1.1 christos
479 1.1 christos static bool
480 1.1 christos omr_publisher_medium_or_high_prefix_present(omr_publisher_t *publisher)
481 1.1 christos {
482 1.1 christos return omr_publisher_medium_prefix_present(publisher) || omr_publisher_high_prefix_present(publisher);
483 1.1 christos }
484 1.1 christos
485 1.1 christos static bool
486 1.1 christos omr_publisher_low_prefix_present(omr_publisher_t *publisher)
487 1.1 christos {
488 1.1 christos return omr_publisher_prefix_present(publisher, omr_prefix_priority_low);
489 1.1 christos }
490 1.1 christos
491 1.1 christos static bool
492 1.1 christos omr_publisher_prefix_wins(omr_publisher_t *publisher, omr_prefix_priority_t priority)
493 1.1 christos {
494 1.1 christos struct in6_addr prefix;
495 1.1 christos int prefix_length;
496 1.1 christos if (publisher->omr_watcher == NULL) {
497 1.1 christos FAULT("expecting an omr_watcher to be on the publisher");
498 1.1 christos return true; // Saying yes because we have no watcher and hence can't behave correctly.
499 1.1 christos }
500 1.1 christos omr_publisher_prefix_init_from_published(publisher, &prefix, &prefix_length);
501 1.1 christos return omr_watcher_prefix_wins(publisher->omr_watcher, priority, &prefix, prefix_length);
502 1.1 christos }
503 1.1 christos
504 1.1 christos static bool
505 1.1 christos omr_publisher_medium_prefix_wins(omr_publisher_t *publisher)
506 1.1 christos {
507 1.1 christos return omr_publisher_prefix_wins(publisher, omr_prefix_priority_medium);
508 1.1 christos }
509 1.1 christos
510 1.1 christos static bool
511 1.1 christos omr_publisher_low_prefix_wins(omr_publisher_t *publisher)
512 1.1 christos {
513 1.1 christos return omr_publisher_prefix_wins(publisher, omr_prefix_priority_low);
514 1.1 christos }
515 1.1 christos
516 1.1 christos bool
517 1.1 christos omr_publisher_publishing_dhcp(omr_publisher_t *publisher)
518 1.1 christos {
519 1.1 christos if (publisher->state_header.state == omr_publisher_state_publishing_dhcp)
520 1.1 christos {
521 1.1 christos return true;
522 1.1 christos }
523 1.1 christos return false;
524 1.1 christos }
525 1.1 christos
526 1.1 christos bool
527 1.1 christos omr_publisher_publishing_ula(omr_publisher_t *publisher)
528 1.1 christos {
529 1.1 christos if (publisher->state_header.state == omr_publisher_state_publishing_ula)
530 1.1 christos {
531 1.1 christos return true;
532 1.1 christos }
533 1.1 christos return false;
534 1.1 christos }
535 1.1 christos
536 1.1 christos bool
537 1.1 christos omr_publisher_publishing_prefix(omr_publisher_t *publisher)
538 1.1 christos {
539 1.1 christos return omr_publisher_publishing_dhcp(publisher) ||
540 1.1 christos omr_publisher_publishing_ula(publisher);
541 1.1 christos }
542 1.1 christos
543 1.1 christos static void omr_publisher_queue_run(omr_publisher_t *publisher);
544 1.1 christos
545 1.1 christos static void
546 1.1 christos omr_publisher_prefix_update_callback(void *context, cti_status_t status)
547 1.1 christos {
548 1.1 christos omr_publisher_t *publisher = context;
549 1.1 christos omr_prefix_t *prefix = publisher->publication_queue;
550 1.1 christos
551 1.1 christos if (prefix == NULL) {
552 1.1 christos ERROR("no pending prefix update");
553 1.1 christos return;
554 1.1 christos }
555 1.1 christos SEGMENTED_IPv6_ADDR_GEN_SRP(prefix->prefix.s6_addr, prefix_buf);
556 1.1 christos INFO("prefix " PRI_SEGMENTED_IPv6_ADDR_SRP "/%d is in state " PUB_S_SRP ", status = %d",
557 1.1 christos SEGMENTED_IPv6_ADDR_PARAM_SRP(prefix->prefix.s6_addr, prefix_buf), prefix->prefix_length,
558 1.1 christos thread_service_publication_state_name_get(prefix->publication_state), status);
559 1.1 christos if (status == kCTIStatus_NoError) {
560 1.1 christos if (prefix->publication_state == add_pending) {
561 1.1 christos prefix->publication_state = add_complete;
562 1.1 christos if (publisher->omr_watcher != NULL) {
563 1.1 christos omr_watcher_prefix_add(publisher->omr_watcher, &prefix->prefix, prefix->prefix_length, prefix->priority);
564 1.1 christos }
565 1.1 christos } else if (prefix->publication_state == delete_pending) {
566 1.1 christos prefix->publication_state = delete_complete;
567 1.1 christos if (publisher->omr_watcher != NULL) {
568 1.1 christos omr_watcher_prefix_remove(publisher->omr_watcher, &prefix->prefix, prefix->prefix_length);
569 1.1 christos }
570 1.1 christos }
571 1.1 christos } else {
572 1.1 christos if (prefix->publication_state == add_pending) {
573 1.1 christos prefix->publication_state = add_failed;
574 1.1 christos } else if (prefix->publication_state == delete_pending) {
575 1.1 christos prefix->publication_state = delete_failed;
576 1.1 christos }
577 1.1 christos }
578 1.1 christos publisher->publication_queue = prefix->next;
579 1.1 christos omr_prefix_release(prefix);
580 1.1 christos omr_publisher_queue_run(publisher);
581 1.1 christos RELEASE_HERE(publisher, omr_publisher);
582 1.1 christos }
583 1.1 christos
584 1.1 christos static void
585 1.1 christos omr_publisher_queue_run(omr_publisher_t *publisher)
586 1.1 christos {
587 1.1 christos omr_prefix_t *prefix = publisher->publication_queue;
588 1.1 christos if (prefix == NULL) {
589 1.1 christos INFO("the queue is empty.");
590 1.1 christos // The queue just became empty, so release its reference to the publisher.
591 1.1 christos RELEASE_HERE(publisher, omr_publisher);
592 1.1 christos return;
593 1.1 christos }
594 1.1 christos if (prefix->publication_state == delete_pending || prefix->publication_state == add_pending) {
595 1.1 christos INFO("there is a pending update at the head of the queue.");
596 1.1 christos return;
597 1.1 christos }
598 1.1 christos if (prefix->publication_state == want_delete) {
599 1.1 christos cti_status_t status = cti_remove_prefix(publisher->route_state->srp_server, publisher,
600 1.1 christos omr_publisher_prefix_update_callback, NULL, &prefix->prefix,
601 1.1 christos prefix->prefix_length);
602 1.1 christos SEGMENTED_IPv6_ADDR_GEN_SRP(prefix, prefix_buf);
603 1.1 christos INFO("removing prefix " PRI_SEGMENTED_IPv6_ADDR_SRP "/%d",
604 1.1 christos SEGMENTED_IPv6_ADDR_PARAM_SRP(prefix->prefix.s6_addr, prefix_buf), prefix->prefix_length);
605 1.1 christos if (status != kCTIStatus_NoError) {
606 1.1 christos ERROR("cti_remove_prefix failed: %d", status);
607 1.1 christos // For removes, we'll leave it on the queue
608 1.1 christos } else {
609 1.1 christos prefix->publication_state = delete_pending;
610 1.1 christos RETAIN_HERE(publisher, omr_publisher); // for the callback
611 1.1 christos }
612 1.1 christos } else if (prefix->publication_state == want_add) {
613 1.1 christos SEGMENTED_IPv6_ADDR_GEN_SRP(prefix, prefix_buf);
614 1.1 christos INFO("adding prefix " PRI_SEGMENTED_IPv6_ADDR_SRP "/%d",
615 1.1 christos SEGMENTED_IPv6_ADDR_PARAM_SRP(prefix->prefix.s6_addr, prefix_buf), prefix->prefix_length);
616 1.1 christos cti_status_t status = cti_add_prefix(publisher->route_state->srp_server, publisher,
617 1.1 christos omr_publisher_prefix_update_callback, NULL, &prefix->prefix,
618 1.1 christos prefix->prefix_length, prefix->onmesh, prefix->preferred, prefix->slaac,
619 1.1 christos prefix->stable, omr_prefix_priority_to_int(prefix->priority));
620 1.1 christos if (status != kCTIStatus_NoError) {
621 1.1 christos ERROR("cti_add_prefix failed: %d", status);
622 1.1 christos publisher->publication_queue = prefix->next;
623 1.1 christos omr_prefix_release(prefix);
624 1.1 christos } else {
625 1.1 christos prefix->publication_state = add_pending;
626 1.1 christos RETAIN_HERE(publisher, omr_publisher); // for the callback
627 1.1 christos }
628 1.1 christos } else {
629 1.1 christos SEGMENTED_IPv6_ADDR_GEN_SRP(prefix, prefix_buf);
630 1.1 christos INFO("prefix " PRI_SEGMENTED_IPv6_ADDR_SRP "/%d is in unexpected state " PUB_S_SRP " on the publication queue",
631 1.1 christos SEGMENTED_IPv6_ADDR_PARAM_SRP(prefix->prefix.s6_addr, prefix_buf), prefix->prefix_length,
632 1.1 christos thread_service_publication_state_name_get(prefix->publication_state));
633 1.1 christos publisher->publication_queue = prefix->next;
634 1.1 christos omr_prefix_release(prefix);
635 1.1 christos }
636 1.1 christos }
637 1.1 christos
638 1.1 christos static void
639 1.1 christos omr_publisher_queue_prefix_update(omr_publisher_t *publisher, struct in6_addr *prefix_address,
640 1.1 christos omr_prefix_priority_t priority, bool preferred,
641 1.1 christos thread_service_publication_state_t initial_state)
642 1.1 christos {
643 1.1 christos const int mandatory_subnet_prefix_length = 64;
644 1.1 christos omr_prefix_t *prefix, **ppref, *old_queue = publisher->publication_queue;
645 1.1 christos int flags = omr_prefix_flags_generate(true /* onmesh */, preferred, true /* slaac */, priority);
646 1.1 christos
647 1.1 christos if (publisher->published_prefix != NULL) {
648 1.1 christos FAULT("published prefix still present");
649 1.1 christos }
650 1.1 christos prefix = omr_prefix_create(prefix_address, mandatory_subnet_prefix_length,
651 1.1 christos 0 /* metric */, flags, 0 /* rloc */, true /* stable */, false /* ncp */);
652 1.1 christos if (prefix == NULL) {
653 1.1 christos ERROR("no memory to remember published prefix!");
654 1.1 christos return;
655 1.1 christos }
656 1.1 christos prefix->publication_state = initial_state;
657 1.1 christos if (initial_state == want_add) {
658 1.1 christos publisher->published_prefix = prefix;
659 1.1 christos omr_prefix_retain(publisher->published_prefix);
660 1.1 christos }
661 1.1 christos // Find the end of the queue
662 1.1 christos for (ppref = &publisher->publication_queue; *ppref != NULL; ppref = &(*ppref)->next)
663 1.1 christos ;
664 1.1 christos *ppref = prefix;
665 1.1 christos // The prefix on the queue is retained by relying on the create/copy rule. When adding a prefix we also retain the
666 1.1 christos // prefix as publisher->published_prefix, so that retain is always explicit and this retain is always implicit.
667 1.1 christos // omr_prefix_retain(*ppref);
668 1.1 christos
669 1.1 christos // If there is anything in the queue, the queue holds a reference to the publisher, so that it will continue to
670 1.1 christos // run until it's complete.
671 1.1 christos if (old_queue == NULL && publisher->publication_queue != NULL) {
672 1.1 christos RETAIN_HERE(publisher, omr_publisher);
673 1.1 christos }
674 1.1 christos omr_publisher_queue_run(publisher);
675 1.1 christos }
676 1.1 christos
677 1.1 christos static void
678 1.1 christos omr_publisher_publish_prefix(omr_publisher_t *publisher,
679 1.1 christos struct in6_addr *prefix_address, omr_prefix_priority_t priority, bool preferred)
680 1.1 christos {
681 1.1 christos SEGMENTED_IPv6_ADDR_GEN_SRP(prefix_address, prefix_buf);
682 1.1 christos INFO("publishing prefix " PRI_SEGMENTED_IPv6_ADDR_SRP "/64",
683 1.1 christos SEGMENTED_IPv6_ADDR_PARAM_SRP(prefix_address, prefix_buf));
684 1.1 christos omr_publisher_queue_prefix_update(publisher, prefix_address, priority, preferred, want_add);
685 1.1 christos INFO("setting publisher->omr_priority to %d, " PUB_S_SRP "preferred", omr_prefix_priority_to_int(priority),
686 1.1 christos preferred ? "" : "not ");
687 1.1 christos publisher->omr_priority = priority;
688 1.1 christos }
689 1.1 christos
690 1.1 christos void
691 1.1 christos omr_publisher_unpublish_prefix(omr_publisher_t *publisher)
692 1.1 christos {
693 1.1 christos omr_prefix_t *prefix;
694 1.1 christos
695 1.1 christos prefix = publisher->published_prefix;
696 1.1 christos publisher->published_prefix = NULL;
697 1.1 christos if (prefix == NULL) {
698 1.1 christos ERROR("request to unpublished prefix that's not present");
699 1.1 christos return;
700 1.1 christos }
701 1.1 christos SEGMENTED_IPv6_ADDR_GEN_SRP(prefix, prefix_buf);
702 1.1 christos INFO("unpublishing prefix " PRI_SEGMENTED_IPv6_ADDR_SRP "/%d",
703 1.1 christos SEGMENTED_IPv6_ADDR_PARAM_SRP(prefix->prefix.s6_addr, prefix_buf), prefix->prefix_length);
704 1.1 christos omr_publisher_queue_prefix_update(publisher, &prefix->prefix, prefix->priority, false, want_delete);
705 1.1 christos omr_prefix_release(prefix);
706 1.1 christos }
707 1.1 christos
708 1.1 christos static void
709 1.1 christos omr_publisher_publish_dhcp_prefix(omr_publisher_t *publisher)
710 1.1 christos {
711 1.1 christos omr_publisher_install_new_dhcp_prefix(publisher);
712 1.1 christos omr_publisher_publish_prefix(publisher, &publisher->dhcp_prefix, omr_prefix_priority_medium,
713 1.1 christos publisher->dhcp_preferred_lifetime == 0 ? false : true);
714 1.1 christos publisher->dhcp_prefix_published = true;
715 1.1 christos return;
716 1.1 christos }
717 1.1 christos
718 1.1 christos static void
719 1.1 christos omr_publisher_unpublish_dhcp_prefix(omr_publisher_t *publisher)
720 1.1 christos {
721 1.1 christos omr_publisher_unpublish_prefix(publisher);
722 1.1 christos publisher->dhcp_prefix_published = false;
723 1.1 christos return;
724 1.1 christos }
725 1.1 christos
726 1.1 christos static void
727 1.1 christos omr_publisher_publish_ula_prefix(omr_publisher_t *publisher)
728 1.1 christos {
729 1.1 christos if (publisher->route_state == NULL || !publisher->route_state->have_thread_prefix) {
730 1.1 christos ERROR("don't have a thread prefix to publish!");
731 1.1 christos return;
732 1.1 christos }
733 1.1 christos omr_prefix_priority_t priority;
734 1.1 christos if (publisher->force_publication) {
735 1.1 christos priority = publisher->force_priority;
736 1.1 christos } else {
737 1.1 christos priority = omr_prefix_priority_low;
738 1.1 christos }
739 1.1 christos omr_publisher_publish_prefix(publisher, &publisher->route_state->my_thread_ula_prefix, priority, true);
740 1.1 christos publisher->ula_prefix_published = true;
741 1.1 christos return;
742 1.1 christos }
743 1.1 christos
744 1.1 christos static void
745 1.1 christos omr_publisher_unpublish_ula_prefix(omr_publisher_t *publisher)
746 1.1 christos {
747 1.1 christos omr_publisher_unpublish_prefix(publisher);
748 1.1 christos publisher->ula_prefix_published = false;
749 1.1 christos return;
750 1.1 christos }
751 1.1 christos
752 1.1 christos bool
753 1.1 christos omr_publisher_have_routable_prefix(omr_publisher_t *publisher)
754 1.1 christos {
755 1.1 christos if ((publisher->published_prefix != NULL && omr_watcher_prefix_is_non_ula_prefix(publisher->published_prefix)) ||
756 1.1 christos (publisher->omr_watcher != NULL && omr_watcher_non_ula_prefix_present(publisher->omr_watcher)))
757 1.1 christos {
758 1.1 christos INFO("we have a routable prefix");
759 1.1 christos return true;
760 1.1 christos }
761 1.1 christos INFO("we do not have a routable prefix");
762 1.1 christos return false;
763 1.1 christos }
764 1.1 christos
765 1.1 christos static state_machine_state_t
766 1.1 christos omr_publisher_dhcp_event(omr_publisher_t *publisher, state_machine_event_t *UNUSED event)
767 1.1 christos {
768 1.1 christos // If we got a DHCP prefix
769 1.1 christos if (omr_publisher_dhcp_prefix_available(publisher)) {
770 1.1 christos if (!omr_publisher_medium_or_high_prefix_present(publisher)) {
771 1.1 christos INFO("got a DHCP prefix, no competing prefixes present");
772 1.1 christos return omr_publisher_state_publishing_dhcp;
773 1.1 christos } else {
774 1.1 christos // If there's a medium or high priority prefix already published, don't publish another.
775 1.1 christos // This shouldn't happen, but if it does, discontinue DHCP (it should already have been discontinued).
776 1.1 christos INFO("medium- or high-priority prefix present, so discontinuing DHCP");
777 1.1 christos omr_publisher_discontinue_dhcp(publisher);
778 1.1 christos }
779 1.1 christos }
780 1.1 christos return omr_publisher_state_invalid;
781 1.1 christos }
782 1.1 christos
783 1.1 christos // In the startup state, we wait to learn about prefixes, or for a timeout to occur. If a prefix shows up that's of medium or
784 1.1 christos // high priority, we don't need to do DHCP, so go to not_publishing. If it's a low priority prefix, we start DHCP, but won't publish
785 1.1 christos // anything other than a DHCP prefix.
786 1.1 christos static state_machine_state_t
787 1.1 christos omr_publisher_action_startup(state_machine_header_t *state_header, state_machine_event_t *event)
788 1.1 christos {
789 1.1 christos STATE_MACHINE_HEADER_TO_PUBLISHER(state_header);
790 1.1 christos BR_STATE_ANNOUNCE(publisher, event);
791 1.1 christos
792 1.1 christos if (event == NULL) {
793 1.1 christos publisher->omr_priority = omr_prefix_priority_invalid;
794 1.1 christos if (publisher->wakeup_timer == NULL) {
795 1.1 christos publisher->wakeup_timer = ioloop_wakeup_create();
796 1.1 christos }
797 1.1 christos if (publisher->wakeup_timer == NULL) {
798 1.1 christos ERROR("unable to allocate a wakeup timer");
799 1.1 christos return omr_publisher_state_invalid;
800 1.1 christos }
801 1.1 christos ioloop_add_wake_event(publisher->wakeup_timer, publisher, omr_publisher_wait_expired,
802 1.1 christos omr_publisher_wakeup_release,
803 1.1 christos publisher->min_start + srp_random16() % OMR_PUBLISHER_START_WAIT);
804 1.1 christos publisher->min_start = 0; // Only need mandatory 3-second startup delay when first joining Thread network.
805 1.1 christos RETAIN_HERE(publisher, omr_publisher); // For wakeup
806 1.1 christos return omr_publisher_state_invalid;
807 1.1 christos }
808 1.1 christos // The only way out of the startup state is for the timer to expire--we don't care about prefixes showing up or
809 1.1 christos // going away.
810 1.1 christos if (event->type == state_machine_event_type_timeout) {
811 1.1 christos INFO("startup timeout");
812 1.1 christos return omr_publisher_state_check_for_dhcp;
813 1.1 christos } else {
814 1.1 christos BR_UNEXPECTED_EVENT(publisher, event);
815 1.1 christos }
816 1.1 christos }
817 1.1 christos
818 1.1 christos // In this state we haven't seen an OMR prefix yet, but we want to give DHCPv6 PD a chance to succeed before
819 1.1 christos // configuring an OMR prefix so that we don't push and then remove an OMR prefix. We also land in this state if
820 1.1 christos // we have seen a low-priority prefix; if we get a DHCP response, we'll publish that, and that should supersede
821 1.1 christos // the low-priority prefix.
822 1.1 christos static state_machine_state_t
823 1.1 christos omr_publisher_action_check_for_dhcp(state_machine_header_t *state_header, state_machine_event_t *event)
824 1.1 christos {
825 1.1 christos STATE_MACHINE_HEADER_TO_PUBLISHER(state_header);
826 1.1 christos BR_STATE_ANNOUNCE(publisher, event);
827 1.1 christos
828 1.1 christos if (event == NULL) {
829 1.1 christos if (publisher->wakeup_timer != NULL) {
830 1.1 christos ioloop_cancel_wake_event(publisher->wakeup_timer);
831 1.1 christos } else {
832 1.1 christos publisher->wakeup_timer = ioloop_wakeup_create();
833 1.1 christos }
834 1.1 christos if (publisher->wakeup_timer == NULL) {
835 1.1 christos ERROR("unable to allocate a wakeup timer");
836 1.1 christos return omr_publisher_state_invalid;
837 1.1 christos }
838 1.1 christos ioloop_add_wake_event(publisher->wakeup_timer, publisher, omr_publisher_wait_expired,
839 1.1 christos omr_publisher_wakeup_release, OMR_PUBLISHER_DHCP_SUCCESS_WAIT);
840 1.1 christos RETAIN_HERE(publisher, omr_publisher); // for the wakeup timer
841 1.1 christos if (publisher->dhcp_client == NULL) {
842 1.1 christos omr_publisher_initiate_dhcp(publisher);
843 1.1 christos }
844 1.1 christos return omr_publisher_state_invalid;
845 1.1 christos }
846 1.1 christos if (event->type == state_machine_event_type_timeout) {
847 1.1 christos // We didn't get a DHCPv6 prefix quickly (might still get one later)
848 1.1 christos INFO("timed out waiting for DHCP");
849 1.1 christos if (omr_publisher_low_prefix_present(publisher)) {
850 1.1 christos INFO("competing low priority prefix present");
851 1.1 christos publisher->omr_priority = omr_prefix_priority_low;
852 1.1 christos return omr_publisher_state_not_publishing;
853 1.1 christos } else if (omr_publisher_medium_or_high_prefix_present(publisher)) {
854 1.1 christos INFO("competing medium or high priority prefix present");
855 1.1 christos omr_publisher_discontinue_dhcp(publisher);
856 1.1 christos return omr_publisher_state_not_publishing;
857 1.1 christos }
858 1.1 christos return omr_publisher_state_publishing_ula;
859 1.1 christos } else if (event->type == state_machine_event_type_prefix) {
860 1.1 christos if (omr_publisher_medium_or_high_prefix_present(publisher)) {
861 1.1 christos INFO("competing medium- or high priority prefix showed up");
862 1.1 christos // We are trying to get a DHCPv6 prefix, so we need to stop.
863 1.1 christos if (publisher->dhcp_client != NULL) {
864 1.1 christos omr_publisher_discontinue_dhcp(publisher);
865 1.1 christos }
866 1.1 christos return omr_publisher_state_not_publishing;
867 1.1 christos } else if (omr_publisher_low_prefix_present(publisher)) {
868 1.1 christos INFO("competing low priority prefix showed up");
869 1.1 christos publisher->omr_priority = omr_prefix_priority_low;
870 1.1 christos return omr_publisher_state_not_publishing;
871 1.1 christos } else {
872 1.1 christos return omr_publisher_state_invalid;
873 1.1 christos }
874 1.1 christos } else if (event->type == state_machine_event_type_dhcp) {
875 1.1 christos return omr_publisher_dhcp_event(publisher, event);
876 1.1 christos } else {
877 1.1 christos BR_UNEXPECTED_EVENT(publisher, event);
878 1.1 christos }
879 1.1 christos }
880 1.1 christos
881 1.1 christos static state_machine_state_t
882 1.1 christos omr_publisher_action_not_publishing(state_machine_header_t *state_header, state_machine_event_t *event)
883 1.1 christos {
884 1.1 christos STATE_MACHINE_HEADER_TO_PUBLISHER(state_header);
885 1.1 christos BR_STATE_ANNOUNCE(publisher, event);
886 1.1 christos
887 1.1 christos if (event == NULL) {
888 1.1 christos if (publisher->wakeup_timer != NULL) {
889 1.1 christos ioloop_cancel_wake_event(publisher->wakeup_timer);
890 1.1 christos }
891 1.1 christos return omr_publisher_state_invalid;
892 1.1 christos } else if (event->type == state_machine_event_type_prefix) {
893 1.1 christos if (!omr_publisher_medium_or_high_prefix_present(publisher)) {
894 1.1 christos if (publisher->dhcp_client == NULL) {
895 1.1 christos INFO("lost competing medium or high-priority prefix");
896 1.1 christos return omr_publisher_state_startup;
897 1.1 christos } else if (!omr_publisher_low_prefix_present(publisher)) {
898 1.1 christos INFO("lost competing low-priority prefix.");
899 1.1 christos return omr_publisher_state_startup;
900 1.1 christos }
901 1.1 christos } else {
902 1.1 christos // If we were looking for DHCP, stop looking.
903 1.1 christos if (publisher->dhcp_client != NULL) {
904 1.1 christos omr_publisher_discontinue_dhcp(publisher);
905 1.1 christos }
906 1.1 christos }
907 1.1 christos // If we get to here, there is some kind of OMR prefix present, so we don't need to publish one.
908 1.1 christos return omr_publisher_state_invalid;
909 1.1 christos } else if (event->type == state_machine_event_type_dhcp) {
910 1.1 christos return omr_publisher_dhcp_event(publisher, event);
911 1.1 christos } else {
912 1.1 christos BR_UNEXPECTED_EVENT(publisher, event);
913 1.1 christos }
914 1.1 christos }
915 1.1 christos
916 1.1 christos // We enter this state when we decide to publish a prefix we got from DHCP. On entry, we publish the prefix. If a high priority
917 1.1 christos // prefix shows up, we unpublish it and go to the not_publishing state. If a medium priority prefix shows up, we do an election, since
918 1.1 christos // that's expected to be a DHCP prefix also. If ours loses, we unpublish and go to not_publishing, otherwise we remain in this state and
919 1.1 christos // do nothing. If our DHCP prefix goes away and is replaced with a different prefix, we unpublish the old and publish the new. If it just
920 1.1 christos // goes away and no new prefix is given, then we unpublish the old prefix and go to the publishing_ula state.
921 1.1 christos static state_machine_state_t
922 1.1 christos omr_publisher_action_publishing_dhcp(state_machine_header_t *state_header, state_machine_event_t *event)
923 1.1 christos {
924 1.1 christos STATE_MACHINE_HEADER_TO_PUBLISHER(state_header);
925 1.1 christos BR_STATE_ANNOUNCE(publisher, event);
926 1.1 christos if (event == NULL) {
927 1.1 christos // Publish the DHCPv6 prefix
928 1.1 christos omr_publisher_publish_dhcp_prefix(publisher);
929 1.1 christos return omr_publisher_state_invalid;
930 1.1 christos } else if (event->type == state_machine_event_type_prefix) {
931 1.1 christos if (omr_publisher_high_prefix_present(publisher)) {
932 1.1 christos INFO("deferring to high-priority prefix");
933 1.1 christos omr_publisher_unpublish_dhcp_prefix(publisher);
934 1.1 christos omr_publisher_discontinue_dhcp(publisher);
935 1.1 christos return omr_publisher_state_not_publishing;
936 1.1 christos } else if (omr_publisher_medium_prefix_present(publisher)) {
937 1.1 christos if (!omr_publisher_medium_prefix_wins(publisher)) {
938 1.1 christos INFO("deferring to winning medium-priority prefix");
939 1.1 christos omr_publisher_unpublish_dhcp_prefix(publisher);
940 1.1 christos omr_publisher_discontinue_dhcp(publisher);
941 1.1 christos return omr_publisher_state_not_publishing;
942 1.1 christos }
943 1.1 christos return omr_publisher_state_invalid;
944 1.1 christos }
945 1.1 christos return omr_publisher_state_invalid;
946 1.1 christos } else if (event->type == state_machine_event_type_dhcp) {
947 1.1 christos if (omr_publisher_dhcp_prefix_lost(publisher)) {
948 1.1 christos INFO("DHCP prefix withdrawn");
949 1.1 christos omr_publisher_unpublish_dhcp_prefix(publisher);
950 1.1 christos return omr_publisher_state_publishing_ula;
951 1.1 christos } else if (omr_publisher_dhcp_prefix_changed(publisher)) {
952 1.1 christos INFO("new prefix from DHCP server");
953 1.1 christos omr_publisher_unpublish_dhcp_prefix(publisher);
954 1.1 christos omr_publisher_publish_dhcp_prefix(publisher);
955 1.1 christos return omr_publisher_state_invalid;
956 1.1 christos }
957 1.1 christos return omr_publisher_state_invalid;
958 1.1 christos } else {
959 1.1 christos BR_UNEXPECTED_EVENT(publisher, event);
960 1.1 christos }
961 1.1 christos }
962 1.1 christos
963 1.1 christos // We enter this state when no prefix showed up while we were waiting, and no DHCP prefix was given, or when we've unpublished
964 1.1 christos // our own DHCP prefix because it was withdrawn or expired. In order for this to happen, it has to be the case that no competing
965 1.1 christos // prefix is being published, so we're going to publish our prefix unconditionally. It's possible that some other BR will publish
966 1.1 christos // a DHCP prefix or a priority prefix, in which case we unpublish ours and go to not_publishing. It's also possible that another BR
967 1.1 christos // will publish a ULA prefix (low priority). In this case, we do an election, and if we lose, we unpublish ours and go to
968 1.1 christos // not_publishing.
969 1.1 christos static state_machine_state_t
970 1.1 christos omr_publisher_action_publishing_ula(state_machine_header_t *state_header, state_machine_event_t *event)
971 1.1 christos {
972 1.1 christos STATE_MACHINE_HEADER_TO_PUBLISHER(state_header);
973 1.1 christos BR_STATE_ANNOUNCE(publisher, event);
974 1.1 christos if (event == NULL) {
975 1.1 christos omr_publisher_publish_ula_prefix(publisher);
976 1.1 christos if (publisher->dhcp_client == NULL) {
977 1.1 christos FAULT("no DHCP client running!");
978 1.1 christos omr_publisher_initiate_dhcp(publisher);
979 1.1 christos publisher->omr_priority = omr_prefix_priority_low;
980 1.1 christos }
981 1.1 christos return omr_publisher_state_invalid;
982 1.1 christos } else if (event->type == state_machine_event_type_prefix) {
983 1.1 christos if (publisher->force_publication) {
984 1.1 christos INFO("ignoring potentially competing prefix");
985 1.1 christos return omr_publisher_state_invalid;
986 1.1 christos }
987 1.1 christos if (omr_publisher_medium_or_high_prefix_present(publisher)) {
988 1.1 christos INFO("deferring to medium- or high-priority prefix");
989 1.1 christos omr_publisher_unpublish_ula_prefix(publisher);
990 1.1 christos return omr_publisher_state_not_publishing;
991 1.1 christos } else if (omr_publisher_low_prefix_present(publisher)) {
992 1.1 christos if (!omr_publisher_low_prefix_wins(publisher)) {
993 1.1 christos INFO("deferring to winning low-priority prefix");
994 1.1 christos omr_publisher_unpublish_ula_prefix(publisher);
995 1.1 christos publisher->omr_priority = omr_prefix_priority_low;
996 1.1 christos return omr_publisher_state_not_publishing;
997 1.1 christos }
998 1.1 christos return omr_publisher_state_invalid;
999 1.1 christos }
1000 1.1 christos return omr_publisher_state_invalid;
1001 1.1 christos } else if (event->type == state_machine_event_type_dhcp) {
1002 1.1 christos if (publisher->force_publication) {
1003 1.1 christos INFO("ignoring dhcp prefix");
1004 1.1 christos return omr_publisher_state_invalid;
1005 1.1 christos } else if (omr_publisher_dhcp_prefix_available(publisher)) {
1006 1.1 christos INFO("switching to DHCP prefix");
1007 1.1 christos omr_publisher_unpublish_ula_prefix(publisher);
1008 1.1 christos return omr_publisher_state_publishing_dhcp;
1009 1.1 christos }
1010 1.1 christos return omr_publisher_state_invalid;
1011 1.1 christos } else {
1012 1.1 christos BR_UNEXPECTED_EVENT(publisher, event);
1013 1.1 christos }
1014 1.1 christos }
1015 1.1 christos
1016 1.1 christos void
1017 1.1 christos omr_publisher_check_prefix(omr_publisher_t *publisher, struct in6_addr *prefix, int UNUSED len)
1018 1.1 christos {
1019 1.1 christos if (publisher == NULL) {
1020 1.1 christos return;
1021 1.1 christos }
1022 1.1 christos if (publisher->published_prefix == NULL) {
1023 1.1 christos return;
1024 1.1 christos }
1025 1.1 christos // Make sure that this prefix, which we are seeing advetised on infrastructure, is not published as the OMR prefix.
1026 1.1 christos if (!in6prefix_compare(&publisher->published_prefix->prefix, prefix, 8)) {
1027 1.1 christos if (!in6prefix_compare(&publisher->ula_prefix, prefix, 8)) {
1028 1.1 christos SEGMENTED_IPv6_ADDR_GEN_SRP(prefix->s6_addr, prefix_buf);
1029 1.1 christos FAULT("ULA prefix is being advertised on infrastructure: " PRI_SEGMENTED_IPv6_ADDR_SRP,
1030 1.1 christos SEGMENTED_IPv6_ADDR_PARAM_SRP(prefix->s6_addr, prefix_buf));
1031 1.1 christos } else {
1032 1.1 christos // If we get here it means that our DHCP prefix is bogus and we can't use it. So we're going to block DHCP, and treat this as
1033 1.1 christos // a DHCP prefix loss.
1034 1.1 christos SEGMENTED_IPv6_ADDR_GEN_SRP(prefix->s6_addr, prefix_buf);
1035 1.1 christos ERROR("DHCP prefix is being advertised on infrastructure: " PRI_SEGMENTED_IPv6_ADDR_SRP,
1036 1.1 christos SEGMENTED_IPv6_ADDR_PARAM_SRP(prefix->s6_addr, prefix_buf));
1037 1.1 christos
1038 1.1 christos publisher->dhcp_wanted = false;
1039 1.1 christos publisher->dhcp_blocked = true;
1040 1.1 christos omr_publisher_dhcp_client_deactivate(publisher, (intptr_t)publisher->dhcp_client);
1041 1.1 christos omr_publisher_send_dhcp_event(publisher, NULL, 0, 0);
1042 1.1 christos }
1043 1.1 christos }
1044 1.1 christos }
1045 1.1 christos
1046 1.1 christos // Local Variables:
1047 1.1 christos // mode: C
1048 1.1 christos // tab-width: 4
1049 1.1 christos // c-file-style: "bsd"
1050 1.1 christos // c-basic-offset: 4
1051 1.1 christos // fill-column: 120
1052 1.1 christos // indent-tabs-mode: nil
1053 1.1 christos // End:
1054