Home | History | Annotate | Line # | Download | only in ServiceRegistration
      1  1.1  christos /* route-tracker.c
      2  1.1  christos  *
      3  1.1  christos  * Copyright (c) 2023 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 the implementation for a route tracker for tracking prefixes and routes on infrastructure so that
     18  1.1  christos  * they can be published on the Thread network.
     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 <netinet/icmp6.h>
     37  1.1  christos #include "srp.h"
     38  1.1  christos #include "dns-msg.h"
     39  1.1  christos #include "srp-crypto.h"
     40  1.1  christos #include "ioloop.h"
     41  1.1  christos #include "srp-gw.h"
     42  1.1  christos #include "srp-proxy.h"
     43  1.1  christos #include "cti-services.h"
     44  1.1  christos #include "srp-mdns-proxy.h"
     45  1.1  christos #include "dnssd-proxy.h"
     46  1.1  christos #include "config-parse.h"
     47  1.1  christos #include "cti-services.h"
     48  1.1  christos #include "route.h"
     49  1.1  christos #include "nat64.h"
     50  1.1  christos #include "nat64-macos.h"
     51  1.1  christos #include "adv-ctl-server.h"
     52  1.1  christos #include "state-machine.h"
     53  1.1  christos #include "thread-service.h"
     54  1.1  christos #include "omr-watcher.h"
     55  1.1  christos #include "omr-publisher.h"
     56  1.1  christos #include "route-tracker.h"
     57  1.1  christos 
     58  1.1  christos #ifdef BUILD_TEST_ENTRY_POINTS
     59  1.1  christos #undef cti_remove_route
     60  1.1  christos #undef cti_add_route
     61  1.1  christos #define cti_remove_route cti_remove_route_test
     62  1.1  christos #define cti_add_route cti_add_route_test
     63  1.1  christos 
     64  1.1  christos static int cti_add_route_test(srp_server_t *NULLABLE UNUSED server, void *NULLABLE context, cti_reply_t NONNULL callback,
     65  1.1  christos                               run_context_t NULLABLE UNUSED client_queue, struct in6_addr *NONNULL prefix,
     66  1.1  christos                               int UNUSED prefix_length, int UNUSED priority, int UNUSED domain_id, bool UNUSED stable,
     67  1.1  christos                               bool UNUSED nat64);
     68  1.1  christos static int cti_remove_route_test(srp_server_t *NULLABLE UNUSED server, void *NULLABLE context, cti_reply_t NONNULL callback,
     69  1.1  christos                                  run_context_t NULLABLE UNUSED client_queue, struct in6_addr *NONNULL prefix,
     70  1.1  christos                                  int UNUSED prefix_length, int UNUSED priority);
     71  1.1  christos #endif
     72  1.1  christos 
     73  1.1  christos typedef struct prefix_tracker prefix_tracker_t;
     74  1.1  christos struct prefix_tracker {
     75  1.1  christos     int ref_count;
     76  1.1  christos     prefix_tracker_t *next; // This is for the prefix advertise queue
     77  1.1  christos     struct in6_addr prefix;
     78  1.1  christos     int prefix_length;
     79  1.1  christos     uint32_t preferred_lifetime, valid_lifetime;
     80  1.1  christos     int num_routers;
     81  1.1  christos     int new_num_routers;
     82  1.1  christos     bool pending;
     83  1.1  christos };
     84  1.1  christos 
     85  1.1  christos // The route tracker keeps a set of prefixes that it's tracking. These prefixes are what's published on the
     86  1.1  christos // Thread mesh.
     87  1.1  christos struct route_tracker {
     88  1.1  christos     int ref_count;
     89  1.1  christos     int max_prefixes;
     90  1.1  christos     void (*reconnect_callback)(void *);
     91  1.1  christos     route_state_t *route_state;
     92  1.1  christos     char *name;
     93  1.1  christos     prefix_tracker_t **prefixes;
     94  1.1  christos     prefix_tracker_t *update_queue;
     95  1.1  christos     interface_t *infrastructure;
     96  1.1  christos     bool canceled;
     97  1.1  christos     bool user_route_seen;
     98  1.1  christos     bool blocked;
     99  1.1  christos #ifdef BUILD_TEST_ENTRY_POINTS
    100  1.1  christos     uint32_t current_mask, add_mask, remove_mask, intended_mask;
    101  1.1  christos     cti_reply_t callback;
    102  1.1  christos     int iterations;
    103  1.1  christos #endif
    104  1.1  christos };
    105  1.1  christos 
    106  1.1  christos 
    107  1.1  christos static void route_tracker_add_prefix_to_queue(route_tracker_t *tracker, prefix_tracker_t *prefix);
    108  1.1  christos 
    109  1.1  christos #ifdef BUILD_TEST_ENTRY_POINTS
    110  1.1  christos static void route_tracker_test_update_queue_empty(route_tracker_t *tracker);
    111  1.1  christos #endif
    112  1.1  christos 
    113  1.1  christos static void
    114  1.1  christos prefix_tracker_finalize(prefix_tracker_t *prefix)
    115  1.1  christos {
    116  1.1  christos     free(prefix);
    117  1.1  christos }
    118  1.1  christos 
    119  1.1  christos static prefix_tracker_t *
    120  1.1  christos prefix_tracker_create(struct in6_addr *prefix_bits, int prefix_length, uint32_t preferred_lifetime, uint32_t valid_lifetime)
    121  1.1  christos {
    122  1.1  christos     prefix_tracker_t *prefix = calloc(1, sizeof (*prefix));
    123  1.1  christos     if (prefix == NULL) {
    124  1.1  christos         ERROR("no memory for prefix");
    125  1.1  christos         return NULL;
    126  1.1  christos     }
    127  1.1  christos     RETAIN_HERE(prefix, prefix_tracker);
    128  1.1  christos     prefix->prefix = *prefix_bits;
    129  1.1  christos     prefix->prefix_length = prefix_length;
    130  1.1  christos     prefix->preferred_lifetime = preferred_lifetime;
    131  1.1  christos     prefix->valid_lifetime = valid_lifetime;
    132  1.1  christos     return prefix;
    133  1.1  christos }
    134  1.1  christos 
    135  1.1  christos static void
    136  1.1  christos route_tracker_finalize(route_tracker_t *tracker)
    137  1.1  christos {
    138  1.1  christos     free(tracker->prefixes);
    139  1.1  christos     free(tracker->name);
    140  1.1  christos     free(tracker);
    141  1.1  christos }
    142  1.1  christos 
    143  1.1  christos #ifndef BUILD_TEST_ENTRY_POINTS
    144  1.1  christos RELEASE_RETAIN_FUNCS(route_tracker);
    145  1.1  christos #endif // BUILD_TEST_ENTRY_POINTS
    146  1.1  christos 
    147  1.1  christos void
    148  1.1  christos route_tracker_cancel(route_tracker_t *tracker)
    149  1.1  christos {
    150  1.1  christos     if (tracker->prefixes != NULL) {
    151  1.1  christos         for (int i = 0; i < tracker->max_prefixes; i++) {
    152  1.1  christos             prefix_tracker_t *prefix = tracker->prefixes[i];
    153  1.1  christos             if (prefix != NULL) {
    154  1.1  christos                 tracker->prefixes[i] = NULL;
    155  1.1  christos                 // If we have published a route to this prefix, queue it for removal.
    156  1.1  christos                 if (prefix->num_routers != 0) {
    157  1.1  christos                     prefix->num_routers = 0;
    158  1.1  christos                     route_tracker_add_prefix_to_queue(tracker, prefix);
    159  1.1  christos                 }
    160  1.1  christos                 RELEASE_HERE(prefix, prefix_tracker);
    161  1.1  christos             }
    162  1.1  christos         }
    163  1.1  christos     }
    164  1.1  christos #ifndef BUILD_TEST_ENTRY_POINTS
    165  1.1  christos     if (tracker->infrastructure) {
    166  1.1  christos         interface_release(tracker->infrastructure);
    167  1.1  christos         tracker->infrastructure = NULL;
    168  1.1  christos     }
    169  1.1  christos #endif // BUILD_TEST_ENTRY_POINTS
    170  1.1  christos     tracker->canceled = true;
    171  1.1  christos }
    172  1.1  christos 
    173  1.1  christos route_tracker_t *
    174  1.1  christos route_tracker_create(route_state_t *NONNULL route_state, const char *NONNULL name)
    175  1.1  christos {
    176  1.1  christos     route_tracker_t *ret = NULL, *tracker = calloc(1, sizeof(*tracker));
    177  1.1  christos     if (tracker == NULL) {
    178  1.1  christos         INFO("no memory for tracker");
    179  1.1  christos         return tracker;
    180  1.1  christos     }
    181  1.1  christos     RETAIN_HERE(tracker, route_tracker);
    182  1.1  christos     tracker->route_state = route_state;
    183  1.1  christos     tracker->name = strdup(name);
    184  1.1  christos     if (tracker->name == NULL) {
    185  1.1  christos         goto out;
    186  1.1  christos     }
    187  1.1  christos     tracker->max_prefixes = 10;
    188  1.1  christos     tracker->prefixes = calloc(tracker->max_prefixes, sizeof (*tracker->prefixes));
    189  1.1  christos     if (tracker->prefixes == NULL) {
    190  1.1  christos         INFO("no memory for prefix vector");
    191  1.1  christos         goto out;
    192  1.1  christos     }
    193  1.1  christos 
    194  1.1  christos     ret = tracker;
    195  1.1  christos     tracker = NULL;
    196  1.1  christos out:
    197  1.1  christos     if (tracker != NULL) {
    198  1.1  christos         RELEASE_HERE(tracker, route_tracker);
    199  1.1  christos     }
    200  1.1  christos     return ret;
    201  1.1  christos }
    202  1.1  christos 
    203  1.1  christos static bool
    204  1.1  christos route_tracker_add_prefix(route_tracker_t *tracker, prefix_tracker_t *prefix)
    205  1.1  christos {
    206  1.1  christos     int open_slot = -1;
    207  1.1  christos     for (int i = 0; i < tracker->max_prefixes; i++) {
    208  1.1  christos         if (tracker->prefixes[i] == NULL && open_slot == -1) {
    209  1.1  christos             open_slot = i;
    210  1.1  christos         }
    211  1.1  christos         if (tracker->prefixes[i] == prefix) {
    212  1.1  christos             SEGMENTED_IPv6_ADDR_GEN_SRP(&prefix->prefix, prefix_buf);
    213  1.1  christos             INFO("prefix " PRI_SEGMENTED_IPv6_ADDR_SRP "/%d is already present",
    214  1.1  christos                  SEGMENTED_IPv6_ADDR_PARAM_SRP(&prefix->prefix, prefix_buf), prefix->prefix_length);
    215  1.1  christos             return true;
    216  1.1  christos         }
    217  1.1  christos     }
    218  1.1  christos     if (open_slot != -1) {
    219  1.1  christos         tracker->prefixes[open_slot] = prefix;
    220  1.1  christos         RETAIN_HERE(tracker->prefixes[open_slot], prefix_tracker);
    221  1.1  christos         return true;
    222  1.1  christos     }
    223  1.1  christos 
    224  1.1  christos     int new_max = tracker->max_prefixes * 2;
    225  1.1  christos     prefix_tracker_t **prefixes = calloc(new_max, sizeof (*prefixes));
    226  1.1  christos     if (prefixes == NULL) {
    227  1.1  christos         INFO("no memory to add prefix");
    228  1.1  christos         return false;
    229  1.1  christos     }
    230  1.1  christos     memcpy(prefixes, tracker->prefixes, tracker->max_prefixes * sizeof(*tracker->prefixes));
    231  1.1  christos     free(tracker->prefixes);
    232  1.1  christos     tracker->prefixes = prefixes;
    233  1.1  christos     tracker->prefixes[tracker->max_prefixes] = prefix;
    234  1.1  christos     RETAIN_HERE(tracker->prefixes[tracker->max_prefixes], prefix_tracker);
    235  1.1  christos     tracker->max_prefixes = new_max;
    236  1.1  christos     return true;
    237  1.1  christos }
    238  1.1  christos 
    239  1.1  christos 
    240  1.1  christos void
    241  1.1  christos route_tracker_set_reconnect_callback(route_tracker_t *tracker, void (*reconnect_callback)(void *context))
    242  1.1  christos {
    243  1.1  christos     tracker->reconnect_callback = reconnect_callback;
    244  1.1  christos }
    245  1.1  christos 
    246  1.1  christos void
    247  1.1  christos route_tracker_start(route_tracker_t *tracker)
    248  1.1  christos {
    249  1.1  christos     INFO("starting tracker " PUB_S_SRP, tracker->name);
    250  1.1  christos     // Immediately check to see if we can advertise a prefix.
    251  1.1  christos #ifndef BUILD_TEST_ENTRY_POINTS
    252  1.1  christos     route_tracker_interface_configuration_changed(tracker);
    253  1.1  christos #endif
    254  1.1  christos     return;
    255  1.1  christos }
    256  1.1  christos 
    257  1.1  christos static void route_tracker_start_next_update(route_tracker_t *tracker);
    258  1.1  christos 
    259  1.1  christos static void
    260  1.1  christos route_tracker_remove_prefix(route_tracker_t *tracker, prefix_tracker_t *prefix)
    261  1.1  christos {
    262  1.1  christos     for (int i = 0; i < tracker->max_prefixes; i++) {
    263  1.1  christos         if (tracker->prefixes[i] == prefix) {
    264  1.1  christos             RELEASE_HERE(tracker->prefixes[i], prefix_tracker);
    265  1.1  christos             tracker->prefixes[i] = NULL;
    266  1.1  christos             return;
    267  1.1  christos         }
    268  1.1  christos     }
    269  1.1  christos }
    270  1.1  christos 
    271  1.1  christos static void
    272  1.1  christos route_tracker_update_callback(void *context, cti_status_t status)
    273  1.1  christos {
    274  1.1  christos     route_tracker_t *tracker = context;
    275  1.1  christos     prefix_tracker_t *prefix = tracker->update_queue;
    276  1.1  christos     INFO("status %d", status);
    277  1.1  christos     if (tracker->update_queue == NULL) {
    278  1.1  christos         ERROR("update seems to have disappeared");
    279  1.1  christos #ifdef BUILD_TEST_ENTRY_POINTS
    280  1.1  christos         route_tracker_test_update_queue_empty(tracker);
    281  1.1  christos #endif
    282  1.1  christos         return;
    283  1.1  christos     }
    284  1.1  christos     if (prefix->pending) {
    285  1.1  christos         prefix->pending = false;
    286  1.1  christos         tracker->update_queue = prefix->next;
    287  1.1  christos         prefix->next = NULL;
    288  1.1  christos         if (prefix->num_routers == 0) {
    289  1.1  christos             route_tracker_remove_prefix(tracker, prefix);
    290  1.1  christos         }
    291  1.1  christos         RELEASE_HERE(prefix, prefix_tracker);
    292  1.1  christos     }
    293  1.1  christos     if (tracker->update_queue != NULL) {
    294  1.1  christos         route_tracker_start_next_update(tracker);
    295  1.1  christos     } else {
    296  1.1  christos         // The update queue holds a reference to the tracker when there is something on the
    297  1.1  christos         // queue.
    298  1.1  christos         RELEASE_HERE(tracker, route_tracker);
    299  1.1  christos #ifdef BUILD_TEST_ENTRY_POINTS
    300  1.1  christos         route_tracker_test_update_queue_empty(tracker);
    301  1.1  christos #endif
    302  1.1  christos     }
    303  1.1  christos }
    304  1.1  christos 
    305  1.1  christos static void
    306  1.1  christos route_tracker_start_next_update(route_tracker_t *tracker)
    307  1.1  christos {
    308  1.1  christos     prefix_tracker_t *prefix = tracker->update_queue;
    309  1.1  christos     if (prefix == NULL) {
    310  1.1  christos         ERROR("start_next_update called with no update");
    311  1.1  christos         return;
    312  1.1  christos     }
    313  1.1  christos 
    314  1.1  christos     cti_status_t status;
    315  1.1  christos 
    316  1.1  christos     // If num_routers is zero, remove the prefix.
    317  1.1  christos     if (prefix->num_routers == 0) {
    318  1.1  christos         SEGMENTED_IPv6_ADDR_GEN_SRP(&prefix->prefix, prefix_buf);
    319  1.1  christos         INFO("removing route: " PRI_SEGMENTED_IPv6_ADDR_SRP "/%d",
    320  1.1  christos              SEGMENTED_IPv6_ADDR_PARAM_SRP(&prefix->prefix, prefix_buf), prefix->prefix_length);
    321  1.1  christos         status = cti_remove_route(tracker->route_state->srp_server, tracker, route_tracker_update_callback,
    322  1.1  christos                                   NULL, &prefix->prefix, prefix->prefix_length, 0);
    323  1.1  christos     } else {
    324  1.1  christos         SEGMENTED_IPv6_ADDR_GEN_SRP(&prefix->prefix, prefix_buf);
    325  1.1  christos         INFO("  adding route: " PRI_SEGMENTED_IPv6_ADDR_SRP "/%d",
    326  1.1  christos              SEGMENTED_IPv6_ADDR_PARAM_SRP(&prefix->prefix, prefix_buf), prefix->prefix_length);
    327  1.1  christos         status = cti_add_route(tracker->route_state->srp_server, tracker, route_tracker_update_callback,
    328  1.1  christos                                NULL, &prefix->prefix, prefix->prefix_length,
    329  1.1  christos                                offmesh_route_preference_medium, 0, true, false);
    330  1.1  christos     }
    331  1.1  christos     if (status != kCTIStatus_NoError) {
    332  1.1  christos         ERROR("route update failed: %d", status);
    333  1.1  christos     } else {
    334  1.1  christos         prefix->pending = true;
    335  1.1  christos     }
    336  1.1  christos }
    337  1.1  christos 
    338  1.1  christos static void
    339  1.1  christos route_tracker_add_prefix_to_queue(route_tracker_t *tracker, prefix_tracker_t *prefix)
    340  1.1  christos {
    341  1.1  christos     prefix_tracker_t **ptp, *old_queue = tracker->update_queue;
    342  1.1  christos     // Find the prefix on the queue, or find the end of the queue.
    343  1.1  christos     for (ptp = &tracker->update_queue; *ptp != NULL && *ptp != prefix; ptp = &(*ptp)->next)
    344  1.1  christos         ;
    345  1.1  christos     // Not on the queue...
    346  1.1  christos     if (*ptp == NULL) {
    347  1.1  christos         *ptp = prefix;
    348  1.1  christos         RETAIN_HERE(prefix, prefix_tracker);
    349  1.1  christos         // Turns out we added it to the beginning of the queue.
    350  1.1  christos         if (tracker->update_queue == prefix) {
    351  1.1  christos             route_tracker_start_next_update(tracker);
    352  1.1  christos         }
    353  1.1  christos         goto out;
    354  1.1  christos     }
    355  1.1  christos     // We have started to update the prefix, but haven't gotten the callback yet. Since we have put the prefix
    356  1.1  christos     // back on the update queue, and it's at the beginning, mark it not pending so that when we get the callback
    357  1.1  christos     // from the update function, we update this route again rather than going on to the next.
    358  1.1  christos     if (prefix == tracker->update_queue) {
    359  1.1  christos         prefix->pending = false;
    360  1.1  christos     }
    361  1.1  christos     // If we get to here, the prefix is already on the update queue and its update hasn't started yet, so we can just leave it.
    362  1.1  christos out:
    363  1.1  christos     // As long as there is anything in the queue, the queue needs to hold a reference to the tracker, so that if it's
    364  1.1  christos     // canceled and released, we finish running the queue before stopping.
    365  1.1  christos     if (old_queue == NULL && tracker->update_queue != NULL) {
    366  1.1  christos         RETAIN_HERE(tracker, route_tracker);
    367  1.1  christos     }
    368  1.1  christos }
    369  1.1  christos 
    370  1.1  christos static void
    371  1.1  christos route_tracker_track_prefix(route_tracker_t *tracker, struct in6_addr *prefix_bits, int prefix_length,
    372  1.1  christos                            uint32_t preferred_lifetime, uint32_t valid_lifetime, bool count)
    373  1.1  christos {
    374  1.1  christos     prefix_tracker_t *prefix = NULL;
    375  1.1  christos     int i;
    376  1.1  christos     for (i = 0; i < tracker->max_prefixes; i++) {
    377  1.1  christos         prefix = tracker->prefixes[i];
    378  1.1  christos         if (prefix == NULL) {
    379  1.1  christos             continue;
    380  1.1  christos         }
    381  1.1  christos         if (prefix->prefix_length == prefix_length && !in6addr_compare(&prefix->prefix, prefix_bits)) {
    382  1.1  christos             break;
    383  1.1  christos         }
    384  1.1  christos     }
    385  1.1  christos     if (i == tracker->max_prefixes) {
    386  1.1  christos         prefix = prefix_tracker_create(prefix_bits, prefix_length, preferred_lifetime, valid_lifetime);
    387  1.1  christos         if (prefix == NULL) {
    388  1.1  christos             return;
    389  1.1  christos         }
    390  1.1  christos         SEGMENTED_IPv6_ADDR_GEN_SRP(prefix_bits, prefix_buf);
    391  1.1  christos         INFO("adding prefix " PRI_SEGMENTED_IPv6_ADDR_SRP, SEGMENTED_IPv6_ADDR_PARAM_SRP(prefix_bits, prefix_buf));
    392  1.1  christos         if (!route_tracker_add_prefix(tracker, prefix)) {
    393  1.1  christos             // Weren't able to add it.
    394  1.1  christos             RELEASE_HERE(prefix, prefix_tracker);
    395  1.1  christos             return;
    396  1.1  christos         }
    397  1.1  christos         if (count) {
    398  1.1  christos             prefix->new_num_routers++;
    399  1.1  christos         }
    400  1.1  christos         // To avoid confusion, route_tracker_add_prefix retains the prefix. That means the reference we got from
    401  1.1  christos         // creating it is still held, so release it.
    402  1.1  christos         RELEASE_HERE(prefix, prefix_tracker);
    403  1.1  christos     } else {
    404  1.1  christos         if (count && prefix != NULL) {
    405  1.1  christos             prefix->new_num_routers++;
    406  1.1  christos         }
    407  1.1  christos     }
    408  1.1  christos }
    409  1.1  christos 
    410  1.1  christos #ifndef BUILD_TEST_ENTRY_POINTS
    411  1.1  christos #if SRP_FEATURE_PUBLISH_SPECIFIC_ROUTES
    412  1.1  christos static void
    413  1.1  christos route_tracker_count_prefixes(route_tracker_t *tracker, icmp_message_t *router, bool have_routable_omr_prefix)
    414  1.1  christos {
    415  1.1  christos     icmp_option_t *router_option = router->options;
    416  1.1  christos     for (int i = 0; i < router->num_options; i++, router_option++) {
    417  1.1  christos         // Always track PIO if it's on-link
    418  1.1  christos         if (router_option->type == icmp_option_prefix_information &&
    419  1.1  christos             (router_option->option.route_information.flags & ND_OPT_PI_FLAG_ONLINK))
    420  1.1  christos         {
    421  1.1  christos             route_tracker_track_prefix(tracker,
    422  1.1  christos                                        &router_option->option.prefix_information.prefix,
    423  1.1  christos                                        router_option->option.prefix_information.length,
    424  1.1  christos                                        router_option->option.prefix_information.preferred_lifetime,
    425  1.1  christos                                        router_option->option.prefix_information.valid_lifetime, true);
    426  1.1  christos         } else if (have_routable_omr_prefix && router_option->type == icmp_option_prefix_information) {
    427  1.1  christos             route_tracker_track_prefix(tracker,
    428  1.1  christos                                        &router_option->option.route_information.prefix,
    429  1.1  christos                                        router_option->option.route_information.length,
    430  1.1  christos                                        router_option->option.route_information.route_lifetime,
    431  1.1  christos                                        router_option->option.route_information.route_lifetime, true);
    432  1.1  christos         }
    433  1.1  christos     }
    434  1.1  christos }
    435  1.1  christos #endif
    436  1.1  christos #endif // BUILD_TEST_ENTRY_POINTS
    437  1.1  christos 
    438  1.1  christos static void
    439  1.1  christos route_tracker_reset_counts(route_tracker_t *tracker)
    440  1.1  christos {
    441  1.1  christos     // Set num_routers to zero on each prefix
    442  1.1  christos     for (int i = 0; i < tracker->max_prefixes; i++) {
    443  1.1  christos         prefix_tracker_t *prefix = tracker->prefixes[i];
    444  1.1  christos         if (prefix != NULL) {
    445  1.1  christos             prefix->new_num_routers = 0;
    446  1.1  christos         }
    447  1.1  christos     }
    448  1.1  christos }
    449  1.1  christos 
    450  1.1  christos static void
    451  1.1  christos route_tracker_publish_changes(route_tracker_t *tracker)
    452  1.1  christos {
    453  1.1  christos     // Now go through the prefixes and queue updates for anything that changed.
    454  1.1  christos     for (int i = 0; i < tracker->max_prefixes; i++) {
    455  1.1  christos         prefix_tracker_t *prefix = tracker->prefixes[i];
    456  1.1  christos         if (prefix != NULL) {
    457  1.1  christos             // If the number of routers advertising the prefix changed, and the total number of routers either went to zero
    458  1.1  christos             // or was previously zero, put the prefix on the queue to publish.
    459  1.1  christos             if (prefix->new_num_routers != prefix->num_routers &&
    460  1.1  christos                 (prefix->new_num_routers == 0 || prefix->num_routers == 0))
    461  1.1  christos             {
    462  1.1  christos                 prefix->num_routers = prefix->new_num_routers;
    463  1.1  christos                 route_tracker_add_prefix_to_queue(tracker, prefix);
    464  1.1  christos                 SEGMENTED_IPv6_ADDR_GEN_SRP(&prefix->prefix, prefix_buf);
    465  1.1  christos                 INFO("(not) " PUB_S_SRP " prefix " PRI_SEGMENTED_IPv6_ADDR_SRP, prefix->num_routers ? "adding" : "removing",
    466  1.1  christos                      SEGMENTED_IPv6_ADDR_PARAM_SRP(&prefix->prefix, prefix_buf));
    467  1.1  christos             } else {
    468  1.1  christos                 // Update the number of routers, but don't do anything else.
    469  1.1  christos                 prefix->num_routers = prefix->new_num_routers;
    470  1.1  christos             }
    471  1.1  christos         }
    472  1.1  christos     }
    473  1.1  christos }
    474  1.1  christos 
    475  1.1  christos bool
    476  1.1  christos route_tracker_check_for_gua_prefixes_on_infrastructure(route_tracker_t *tracker)
    477  1.1  christos {
    478  1.1  christos     bool present = false;
    479  1.1  christos     interface_t *interface = tracker->infrastructure;
    480  1.1  christos     if (tracker == NULL || interface == NULL) {
    481  1.1  christos         goto out;
    482  1.1  christos     }
    483  1.1  christos     for (icmp_message_t *router = interface->routers; router != NULL; router = router->next) {
    484  1.1  christos         for (int i = 0; i < router->num_options; i++) {
    485  1.1  christos             icmp_option_t *option = &router->options[i];
    486  1.1  christos             if (option->type == icmp_option_prefix_information) {
    487  1.1  christos                 prefix_information_t *prefix = &option->option.prefix_information;
    488  1.1  christos                 if (omr_watcher_prefix_is_non_ula_prefix(prefix)) {
    489  1.1  christos                     present = true;
    490  1.1  christos                     goto done;
    491  1.1  christos                 }
    492  1.1  christos             } else if (option->type == icmp_option_route_information) {
    493  1.1  christos                 route_information_t *rio = &option->option.route_information;
    494  1.1  christos                 if (omr_watcher_prefix_is_non_ula_prefix(rio)) {
    495  1.1  christos                     present = true;
    496  1.1  christos                     goto done;
    497  1.1  christos                 }
    498  1.1  christos             }
    499  1.1  christos         }
    500  1.1  christos     }
    501  1.1  christos done:
    502  1.1  christos     INFO("interface " PUB_S_SRP ": checked for GUAs on infrastructure: " PUB_S_SRP "present",
    503  1.1  christos          interface->name, present ? "" : "not ");
    504  1.1  christos out:
    505  1.1  christos     return present;
    506  1.1  christos }
    507  1.1  christos 
    508  1.1  christos 
    509  1.1  christos #ifndef BUILD_TEST_ENTRY_POINTS
    510  1.1  christos void
    511  1.1  christos route_tracker_route_state_changed(route_tracker_t *tracker, interface_t *interface)
    512  1.1  christos {
    513  1.1  christos     if (tracker->blocked) {
    514  1.1  christos         INFO("tracker is blocked");
    515  1.1  christos         return;
    516  1.1  christos     }
    517  1.1  christos     if (tracker->route_state == NULL) {
    518  1.1  christos         ERROR("tracker has no route_state");
    519  1.1  christos         return;
    520  1.1  christos     }
    521  1.1  christos     route_state_t *route_state = tracker->route_state;
    522  1.1  christos 
    523  1.1  christos     bool have_routable_omr_prefix;
    524  1.1  christos     if (route_state->omr_publisher != NULL && omr_publisher_have_routable_prefix(route_state->omr_publisher)) {
    525  1.1  christos         have_routable_omr_prefix = true;
    526  1.1  christos     } else {
    527  1.1  christos         have_routable_omr_prefix = false;
    528  1.1  christos     }
    529  1.1  christos     if (interface != tracker->infrastructure) {
    530  1.1  christos         return;
    531  1.1  christos     }
    532  1.1  christos     bool need_default_route = have_routable_omr_prefix;
    533  1.1  christos     // We need a default route if there is a routable omr prefix, but also if there are GUA prefixes on the adjacent
    534  1.1  christos     // infrastructure link or reachable via the adjacent infrastructure link's router(s).
    535  1.1  christos     if (interface != NULL && !need_default_route) {
    536  1.1  christos         need_default_route = route_tracker_check_for_gua_prefixes_on_infrastructure(tracker);
    537  1.1  christos     }
    538  1.1  christos     INFO("interface: " PUB_S_SRP PUB_S_SRP " need default route, " PUB_S_SRP " routable OMR prefix",
    539  1.1  christos          interface != NULL ? interface->name : "(no interface)", need_default_route ? "" : " don't",
    540  1.1  christos          have_routable_omr_prefix ? "have" : "don't have");
    541  1.1  christos 
    542  1.1  christos     route_tracker_reset_counts(tracker);
    543  1.1  christos 
    544  1.1  christos     // If we have no interface, then all we really care about is that any routes we're publishing should be
    545  1.1  christos     // removed.
    546  1.1  christos     if (interface != NULL) {
    547  1.1  christos         static struct in6_addr prefix;
    548  1.1  christos         int width = 0;
    549  1.1  christos         if (need_default_route) {
    550  1.1  christos             ((uint8_t *)&prefix)[0] = 0;
    551  1.1  christos         } else {
    552  1.1  christos             ((uint8_t *)&prefix)[0] = 0xfc;
    553  1.1  christos             width = 7;
    554  1.1  christos         }
    555  1.1  christos         route_tracker_track_prefix(tracker, &prefix, width, 1800, 1800, true);
    556  1.1  christos     }
    557  1.1  christos #if SRP_FEATURE_NAT64
    558  1.1  christos     nat64_omr_route_update(route_state->nat64, have_routable_omr_prefix);
    559  1.1  christos #endif
    560  1.1  christos     route_tracker_publish_changes(tracker);
    561  1.1  christos }
    562  1.1  christos 
    563  1.1  christos void
    564  1.1  christos route_tracker_interface_configuration_changed(route_tracker_t *tracker)
    565  1.1  christos {
    566  1.1  christos     interface_t *preferred = NULL;
    567  1.1  christos     if (tracker->blocked) {
    568  1.1  christos         INFO("tracker is blocked");
    569  1.1  christos         return;
    570  1.1  christos     }
    571  1.1  christos     if (tracker->route_state == NULL) {
    572  1.1  christos         ERROR("tracker has no route_state");
    573  1.1  christos         return;
    574  1.1  christos     }
    575  1.1  christos     route_state_t *route_state = tracker->route_state;
    576  1.1  christos 
    577  1.1  christos     for (interface_t *interface = route_state->interfaces; interface; interface = interface->next) {
    578  1.1  christos         if (interface->ip_configuration_service != NULL) {
    579  1.1  christos             preferred = interface;
    580  1.1  christos             break;
    581  1.1  christos         }
    582  1.1  christos         if (!interface->inactive && !interface->ineligible) {
    583  1.1  christos             if (tracker->infrastructure == interface) {
    584  1.1  christos                 preferred = interface;
    585  1.1  christos                 break;
    586  1.1  christos             }
    587  1.1  christos             if (preferred != NULL) {
    588  1.1  christos                 FAULT("more than one infra interface: " PUB_S_SRP " and " PUB_S_SRP " (picked)", interface->name, preferred->name);
    589  1.1  christos             } else {
    590  1.1  christos                 preferred = interface;
    591  1.1  christos             }
    592  1.1  christos         }
    593  1.1  christos     }
    594  1.1  christos     if (preferred == NULL) {
    595  1.1  christos         if (tracker->infrastructure != NULL) {
    596  1.1  christos             interface_release(tracker->infrastructure);
    597  1.1  christos             tracker->infrastructure = NULL;
    598  1.1  christos             INFO("no infrastructure");
    599  1.1  christos             route_tracker_route_state_changed(tracker, NULL);
    600  1.1  christos         }
    601  1.1  christos     } else {
    602  1.1  christos         if (tracker->infrastructure != preferred) {
    603  1.1  christos             if (tracker->infrastructure != NULL) {
    604  1.1  christos                 interface_release(tracker->infrastructure);
    605  1.1  christos                 tracker->infrastructure = NULL;
    606  1.1  christos             }
    607  1.1  christos             INFO("preferred infrastructure interface is now " PUB_S_SRP, preferred->name);
    608  1.1  christos #if SRP_FEATURE_NAT64
    609  1.1  christos             nat64_pass_all_pf_rule_set(preferred->name);
    610  1.1  christos #endif // SRP_FEATURE_NAT64
    611  1.1  christos             tracker->infrastructure = preferred;
    612  1.1  christos             interface_retain(tracker->infrastructure);
    613  1.1  christos             route_tracker_route_state_changed(tracker, tracker->infrastructure);
    614  1.1  christos         }
    615  1.1  christos     }
    616  1.1  christos }
    617  1.1  christos 
    618  1.1  christos void
    619  1.1  christos route_tracker_monitor_mesh_routes(route_tracker_t *tracker, cti_route_vec_t *routes)
    620  1.1  christos {
    621  1.1  christos     tracker->user_route_seen = false;
    622  1.1  christos     for (size_t i = 0; i < routes->num; i++) {
    623  1.1  christos         cti_route_t *route = routes->routes[i];
    624  1.1  christos         if (route && route->origin == offmesh_route_origin_user) {
    625  1.1  christos             route_tracker_track_prefix(tracker, &route->prefix, route->prefix_length, 100, 100, false);
    626  1.1  christos             tracker->user_route_seen = true;
    627  1.1  christos         }
    628  1.1  christos     }
    629  1.1  christos     if (!tracker->user_route_seen && tracker->route_state->srp_server->awaiting_route_removal) {
    630  1.1  christos         tracker->route_state->srp_server->awaiting_route_removal = false;
    631  1.1  christos         adv_ctl_thread_shutdown_status_check(tracker->route_state->srp_server);
    632  1.1  christos     }
    633  1.1  christos }
    634  1.1  christos 
    635  1.1  christos bool
    636  1.1  christos route_tracker_local_routes_seen(route_tracker_t *tracker)
    637  1.1  christos {
    638  1.1  christos     if (tracker != NULL) {
    639  1.1  christos         return tracker->user_route_seen;
    640  1.1  christos     }
    641  1.1  christos     return false;
    642  1.1  christos }
    643  1.1  christos 
    644  1.1  christos void
    645  1.1  christos route_tracker_shutdown(route_state_t *route_state)
    646  1.1  christos {
    647  1.1  christos     if (route_state == NULL || route_state->route_tracker == NULL) {
    648  1.1  christos         return;
    649  1.1  christos     }
    650  1.1  christos     route_tracker_reset_counts(route_state->route_tracker);
    651  1.1  christos     route_tracker_publish_changes(route_state->route_tracker);
    652  1.1  christos     route_state->route_tracker->blocked = true;
    653  1.1  christos }
    654  1.1  christos #else // !defined(BUILD_TEST_ENTRY_POINTS)
    655  1.1  christos 
    656  1.1  christos static void
    657  1.1  christos route_tracker_remove_callback(void *context)
    658  1.1  christos {
    659  1.1  christos     route_tracker_update_callback(context, 0);
    660  1.1  christos }
    661  1.1  christos 
    662  1.1  christos static void
    663  1.1  christos route_tracker_test_route_update(void *context, struct in6_addr *prefix, cti_reply_t callback, bool remove)
    664  1.1  christos {
    665  1.1  christos     route_tracker_t *tracker = context;
    666  1.1  christos 
    667  1.1  christos     for (int i = 0; i < 4; i++) {
    668  1.1  christos         if (prefix->s6_addr[i] != 0) {
    669  1.1  christos             for (int j = 0; j < 8; j++) {
    670  1.1  christos                 if (prefix->s6_addr[i] == (1 << j)) {
    671  1.1  christos                     int bit = i * 8 + j;
    672  1.1  christos                     if (remove) {
    673  1.1  christos                         tracker->remove_mask |= (1 << bit);
    674  1.1  christos                         INFO("bit %d removed", bit);
    675  1.1  christos                     } else {
    676  1.1  christos                         tracker->add_mask |= (1 << bit);
    677  1.1  christos                         INFO("bit %d added", bit);
    678  1.1  christos                     }
    679  1.1  christos                     tracker->callback = callback;
    680  1.1  christos                     ioloop_run_async(route_tracker_remove_callback, tracker);
    681  1.1  christos                     return;
    682  1.1  christos                 }
    683  1.1  christos             }
    684  1.1  christos         }
    685  1.1  christos     }
    686  1.1  christos     INFO("no bit");
    687  1.1  christos }
    688  1.1  christos 
    689  1.1  christos int
    690  1.1  christos cti_add_route_test(srp_server_t *NULLABLE UNUSED server, void *NULLABLE context, cti_reply_t NONNULL callback,
    691  1.1  christos                    run_context_t NULLABLE UNUSED client_queue, struct in6_addr *NONNULL prefix,
    692  1.1  christos                    int UNUSED prefix_length, int UNUSED priority, int UNUSED domain_id, bool UNUSED stable,
    693  1.1  christos                    bool UNUSED nat64)
    694  1.1  christos {
    695  1.1  christos     route_tracker_test_route_update(context, prefix, callback, false);
    696  1.1  christos     return 0;
    697  1.1  christos }
    698  1.1  christos 
    699  1.1  christos int
    700  1.1  christos cti_remove_route_test(srp_server_t *NULLABLE UNUSED server, void *NULLABLE context, cti_reply_t NONNULL callback,
    701  1.1  christos                       run_context_t NULLABLE UNUSED client_queue, struct in6_addr *NONNULL prefix,
    702  1.1  christos                       int UNUSED prefix_length, int UNUSED priority)
    703  1.1  christos {
    704  1.1  christos     route_tracker_test_route_update(context, prefix, callback, true);
    705  1.1  christos     return 0;
    706  1.1  christos }
    707  1.1  christos 
    708  1.1  christos static void
    709  1.1  christos route_tracker_test_announce_prefixes_in_mask(route_tracker_t *tracker, uint32_t mask)
    710  1.1  christos {
    711  1.1  christos     struct in6_addr prefix;
    712  1.1  christos 
    713  1.1  christos     tracker->intended_mask = mask;
    714  1.1  christos     route_tracker_reset_counts(tracker);
    715  1.1  christos 
    716  1.1  christos     for (int i = 0; i < 32; i++) {
    717  1.1  christos         if ((mask & (1 << i)) != 0) {
    718  1.1  christos             INFO("%x   is in   %x", 1 << i, mask);
    719  1.1  christos             int byte = i / 8;
    720  1.1  christos             uint8_t bit = (uint8_t)(1 << (i & 7));
    721  1.1  christos 
    722  1.1  christos             in6addr_zero(&prefix);
    723  1.1  christos             prefix.s6_addr[byte] = bit;
    724  1.1  christos 
    725  1.1  christos             SEGMENTED_IPv6_ADDR_GEN_SRP(&prefix, prefix_buf);
    726  1.1  christos             INFO("prefix " PRI_SEGMENTED_IPv6_ADDR_SRP " byte %d bit %x i %d",
    727  1.1  christos                  SEGMENTED_IPv6_ADDR_PARAM_SRP(&prefix, prefix_buf), byte, bit, i);
    728  1.1  christos 
    729  1.1  christos             route_tracker_track_prefix(tracker, &prefix, 64, 100, 100, true);
    730  1.1  christos         } else {
    731  1.1  christos             INFO("%x is not in %x", 1 << i, mask);
    732  1.1  christos         }
    733  1.1  christos     }
    734  1.1  christos     route_tracker_publish_changes(tracker);
    735  1.1  christos     // If we get the same mask twice in a row, there will be no updates, so we won't get any callbacks.
    736  1.1  christos     if (tracker->update_queue == NULL) {
    737  1.1  christos         route_tracker_test_update_queue_empty(tracker);
    738  1.1  christos     }
    739  1.1  christos }
    740  1.1  christos 
    741  1.1  christos static void
    742  1.1  christos route_tracker_test_iterate(route_tracker_t *tracker)
    743  1.1  christos {
    744  1.1  christos     tracker->intended_mask = srp_random32();
    745  1.1  christos     route_tracker_test_announce_prefixes_in_mask(tracker, tracker->intended_mask);
    746  1.1  christos }
    747  1.1  christos 
    748  1.1  christos route_state_t *test_route_state;
    749  1.1  christos srp_server_t *test_srp_server;
    750  1.1  christos 
    751  1.1  christos void
    752  1.1  christos route_tracker_test_start(int iterations)
    753  1.1  christos {
    754  1.1  christos 	test_srp_server = calloc(1, sizeof(*test_srp_server));
    755  1.1  christos 	if (test_srp_server == NULL) {
    756  1.1  christos 		ERROR("no memory for srp state");
    757  1.1  christos 		exit(1);
    758  1.1  christos 	}
    759  1.1  christos 	test_route_state = calloc(1, sizeof(*test_route_state));
    760  1.1  christos 	if (test_route_state == NULL) {
    761  1.1  christos 		ERROR("no memory for route state");
    762  1.1  christos 		exit(1);
    763  1.1  christos 	}
    764  1.1  christos     test_route_state->srp_server = test_srp_server;
    765  1.1  christos 	test_route_state->route_tracker = route_tracker_create(test_route_state, "test");
    766  1.1  christos     test_route_state->route_tracker->iterations = iterations;
    767  1.1  christos     route_tracker_start(test_route_state->route_tracker);
    768  1.1  christos     route_tracker_test_iterate(test_route_state->route_tracker);
    769  1.1  christos }
    770  1.1  christos 
    771  1.1  christos static void
    772  1.1  christos route_tracker_test_update_queue_empty(route_tracker_t *tracker)
    773  1.1  christos {
    774  1.1  christos     uint32_t result = ((tracker->current_mask & ~tracker->remove_mask)) | tracker->add_mask;
    775  1.1  christos     INFO("current: %x intended: %x  result: %x  add: %x  remove: %x",
    776  1.1  christos          tracker->current_mask, tracker->intended_mask, result, tracker->add_mask, tracker->remove_mask);
    777  1.1  christos     if (tracker->intended_mask != result) {
    778  1.1  christos         ERROR("test failed.");
    779  1.1  christos         exit(1);
    780  1.1  christos     }
    781  1.1  christos     tracker->current_mask = tracker->intended_mask;
    782  1.1  christos     tracker->add_mask = 0;
    783  1.1  christos     tracker->remove_mask = 0;
    784  1.1  christos     tracker->intended_mask = 0;
    785  1.1  christos     if (tracker->iterations != 0) {
    786  1.1  christos         tracker->iterations--;
    787  1.1  christos         route_tracker_test_iterate(tracker);
    788  1.1  christos     } else {
    789  1.1  christos         INFO("test completed");
    790  1.1  christos         exit(0);
    791  1.1  christos     }
    792  1.1  christos }
    793  1.1  christos #endif //  BUILD_TEST_ENTRY_POINTS
    794  1.1  christos 
    795  1.1  christos // Local Variables:
    796  1.1  christos // mode: C
    797  1.1  christos // tab-width: 4
    798  1.1  christos // c-file-style: "bsd"
    799  1.1  christos // c-basic-offset: 4
    800  1.1  christos // fill-column: 120
    801  1.1  christos // indent-tabs-mode: nil
    802  1.1  christos // End:
    803