LCOV - code coverage report
Current view: top level - libsystemd-network - sd-ipv4ll.c (source / functions) Hit Total Coverage
Test: main_coverage.info Lines: 112 147 76.2 %
Date: 2019-08-22 15:41:25 Functions: 18 21 85.7 %

          Line data    Source code
       1             : /* SPDX-License-Identifier: LGPL-2.1+ */
       2             : /***
       3             :   Copyright © 2014 Axis Communications AB. All rights reserved.
       4             : ***/
       5             : 
       6             : #include <arpa/inet.h>
       7             : #include <errno.h>
       8             : #include <stdio.h>
       9             : #include <stdlib.h>
      10             : #include <string.h>
      11             : 
      12             : #include "sd-id128.h"
      13             : #include "sd-ipv4acd.h"
      14             : #include "sd-ipv4ll.h"
      15             : 
      16             : #include "alloc-util.h"
      17             : #include "ether-addr-util.h"
      18             : #include "in-addr-util.h"
      19             : #include "list.h"
      20             : #include "random-util.h"
      21             : #include "siphash24.h"
      22             : #include "sparse-endian.h"
      23             : #include "string-util.h"
      24             : #include "util.h"
      25             : 
      26             : #define IPV4LL_NETWORK UINT32_C(0xA9FE0000)
      27             : #define IPV4LL_NETMASK UINT32_C(0xFFFF0000)
      28             : 
      29             : #define IPV4LL_DONT_DESTROY(ll) \
      30             :         _cleanup_(sd_ipv4ll_unrefp) _unused_ sd_ipv4ll *_dont_destroy_##ll = sd_ipv4ll_ref(ll)
      31             : 
      32             : struct sd_ipv4ll {
      33             :         unsigned n_ref;
      34             : 
      35             :         sd_ipv4acd *acd;
      36             : 
      37             :         be32_t address; /* the address pushed to ACD */
      38             :         struct ether_addr mac;
      39             : 
      40             :         struct {
      41             :                 le64_t value;
      42             :                 le64_t generation;
      43             :         } seed;
      44             :         bool seed_set;
      45             : 
      46             :         /* External */
      47             :         be32_t claimed_address;
      48             : 
      49             :         sd_ipv4ll_callback_t callback;
      50             :         void* userdata;
      51             : };
      52             : 
      53             : #define log_ipv4ll_errno(ll, error, fmt, ...) log_internal(LOG_DEBUG, error, PROJECT_FILE, __LINE__, __func__, "IPV4LL: " fmt, ##__VA_ARGS__)
      54             : #define log_ipv4ll(ll, fmt, ...) log_ipv4ll_errno(ll, 0, fmt, ##__VA_ARGS__)
      55             : 
      56             : static void ipv4ll_on_acd(sd_ipv4acd *ll, int event, void *userdata);
      57             : 
      58           2 : static sd_ipv4ll *ipv4ll_free(sd_ipv4ll *ll) {
      59           2 :         assert(ll);
      60             : 
      61           2 :         sd_ipv4acd_unref(ll->acd);
      62           2 :         return mfree(ll);
      63             : }
      64             : 
      65          12 : DEFINE_TRIVIAL_REF_UNREF_FUNC(sd_ipv4ll, sd_ipv4ll, ipv4ll_free);
      66             : 
      67           2 : int sd_ipv4ll_new(sd_ipv4ll **ret) {
      68           2 :         _cleanup_(sd_ipv4ll_unrefp) sd_ipv4ll *ll = NULL;
      69             :         int r;
      70             : 
      71           2 :         assert_return(ret, -EINVAL);
      72             : 
      73           2 :         ll = new0(sd_ipv4ll, 1);
      74           2 :         if (!ll)
      75           0 :                 return -ENOMEM;
      76             : 
      77           2 :         ll->n_ref = 1;
      78             : 
      79           2 :         r = sd_ipv4acd_new(&ll->acd);
      80           2 :         if (r < 0)
      81           0 :                 return r;
      82             : 
      83           2 :         r = sd_ipv4acd_set_callback(ll->acd, ipv4ll_on_acd, ll);
      84           2 :         if (r < 0)
      85           0 :                 return r;
      86             : 
      87           2 :         *ret = TAKE_PTR(ll);
      88             : 
      89           2 :         return 0;
      90             : }
      91             : 
      92           1 : int sd_ipv4ll_stop(sd_ipv4ll *ll) {
      93           1 :         assert_return(ll, -EINVAL);
      94             : 
      95           1 :         return sd_ipv4acd_stop(ll->acd);
      96             : }
      97             : 
      98           6 : int sd_ipv4ll_set_ifindex(sd_ipv4ll *ll, int ifindex) {
      99           6 :         assert_return(ll, -EINVAL);
     100           5 :         assert_return(ifindex > 0, -EINVAL);
     101           3 :         assert_return(sd_ipv4ll_is_running(ll) == 0, -EBUSY);
     102             : 
     103           3 :         return sd_ipv4acd_set_ifindex(ll->acd, ifindex);
     104             : }
     105             : 
     106           4 : int sd_ipv4ll_set_mac(sd_ipv4ll *ll, const struct ether_addr *addr) {
     107             :         int r;
     108             : 
     109           4 :         assert_return(ll, -EINVAL);
     110           3 :         assert_return(addr, -EINVAL);
     111           2 :         assert_return(sd_ipv4ll_is_running(ll) == 0, -EBUSY);
     112             : 
     113           2 :         r = sd_ipv4acd_set_mac(ll->acd, addr);
     114           2 :         if (r < 0)
     115           0 :                 return r;
     116             : 
     117           2 :         ll->mac = *addr;
     118           2 :         return 0;
     119             : }
     120             : 
     121           0 : int sd_ipv4ll_detach_event(sd_ipv4ll *ll) {
     122           0 :         assert_return(ll, -EINVAL);
     123             : 
     124           0 :         return sd_ipv4acd_detach_event(ll->acd);
     125             : }
     126             : 
     127           4 : int sd_ipv4ll_attach_event(sd_ipv4ll *ll, sd_event *event, int64_t priority) {
     128           4 :         assert_return(ll, -EINVAL);
     129             : 
     130           3 :         return sd_ipv4acd_attach_event(ll->acd, event, priority);
     131             : }
     132             : 
     133           3 : int sd_ipv4ll_set_callback(sd_ipv4ll *ll, sd_ipv4ll_callback_t cb, void *userdata) {
     134           3 :         assert_return(ll, -EINVAL);
     135             : 
     136           2 :         ll->callback = cb;
     137           2 :         ll->userdata = userdata;
     138             : 
     139           2 :         return 0;
     140             : }
     141             : 
     142           0 : int sd_ipv4ll_get_address(sd_ipv4ll *ll, struct in_addr *address) {
     143           0 :         assert_return(ll, -EINVAL);
     144           0 :         assert_return(address, -EINVAL);
     145             : 
     146           0 :         if (ll->claimed_address == 0)
     147           0 :                 return -ENOENT;
     148             : 
     149           0 :         address->s_addr = ll->claimed_address;
     150             : 
     151           0 :         return 0;
     152             : }
     153             : 
     154           2 : int sd_ipv4ll_set_address_seed(sd_ipv4ll *ll, uint64_t seed) {
     155           2 :         assert_return(ll, -EINVAL);
     156           1 :         assert_return(sd_ipv4ll_is_running(ll) == 0, -EBUSY);
     157             : 
     158           1 :         ll->seed.value = htole64(seed);
     159           1 :         ll->seed_set = true;
     160             : 
     161           1 :         return 0;
     162             : }
     163             : 
     164          13 : int sd_ipv4ll_is_running(sd_ipv4ll *ll) {
     165          13 :         assert_return(ll, false);
     166             : 
     167          13 :         return sd_ipv4acd_is_running(ll->acd);
     168             : }
     169             : 
     170           8 : static bool ipv4ll_address_is_valid(const struct in_addr *address) {
     171           8 :         assert(address);
     172             : 
     173           8 :         if (!in_addr_is_link_local(AF_INET, (const union in_addr_union *) address))
     174           1 :                 return false;
     175             : 
     176           7 :         return !IN_SET(be32toh(address->s_addr) & 0x0000FF00U, 0x0000U, 0xFF00U);
     177             : }
     178             : 
     179           8 : int sd_ipv4ll_set_address(sd_ipv4ll *ll, const struct in_addr *address) {
     180             :         int r;
     181             : 
     182           8 :         assert_return(ll, -EINVAL);
     183           8 :         assert_return(address, -EINVAL);
     184           8 :         assert_return(ipv4ll_address_is_valid(address), -EINVAL);
     185             : 
     186           4 :         r = sd_ipv4acd_set_address(ll->acd, address);
     187           4 :         if (r < 0)
     188           0 :                 return r;
     189             : 
     190           4 :         ll->address = address->s_addr;
     191             : 
     192           4 :         return 0;
     193             : }
     194             : 
     195             : #define PICK_HASH_KEY SD_ID128_MAKE(15,ac,82,a6,d6,3f,49,78,98,77,5d,0c,69,02,94,0b)
     196             : 
     197           3 : static int ipv4ll_pick_address(sd_ipv4ll *ll) {
     198           3 :         _cleanup_free_ char *address = NULL;
     199             :         be32_t addr;
     200             : 
     201           3 :         assert(ll);
     202             : 
     203             :         do {
     204             :                 uint64_t h;
     205             : 
     206           3 :                 h = siphash24(&ll->seed, sizeof(ll->seed), PICK_HASH_KEY.bytes);
     207             : 
     208             :                 /* Increase the generation counter by one */
     209           3 :                 ll->seed.generation = htole64(le64toh(ll->seed.generation) + 1);
     210             : 
     211           3 :                 addr = htobe32((h & UINT32_C(0x0000FFFF)) | IPV4LL_NETWORK);
     212           6 :         } while (addr == ll->address ||
     213           3 :                  IN_SET(be32toh(addr) & 0x0000FF00U, 0x0000U, 0xFF00U));
     214             : 
     215           3 :         (void) in_addr_to_string(AF_INET, &(union in_addr_union) { .in.s_addr = addr }, &address);
     216           3 :         log_ipv4ll(ll, "Picked new IP address %s.", strna(address));
     217             : 
     218           3 :         return sd_ipv4ll_set_address(ll, &(struct in_addr) { addr });
     219             : }
     220             : 
     221             : #define MAC_HASH_KEY SD_ID128_MAKE(df,04,22,98,3f,ad,14,52,f9,87,2e,d1,9c,70,e2,f2)
     222             : 
     223           5 : static int ipv4ll_start_internal(sd_ipv4ll *ll, bool reset_generation) {
     224             :         int r;
     225           5 :         bool picked_address = false;
     226             : 
     227           5 :         assert_return(ll, -EINVAL);
     228           5 :         assert_return(!ether_addr_is_null(&ll->mac), -EINVAL);
     229             : 
     230             :         /* If no random seed is set, generate some from the MAC address */
     231           3 :         if (!ll->seed_set)
     232           3 :                 ll->seed.value = htole64(siphash24(ll->mac.ether_addr_octet, ETH_ALEN, MAC_HASH_KEY.bytes));
     233             : 
     234           3 :         if (reset_generation)
     235           3 :                 ll->seed.generation = 0;
     236             : 
     237           3 :         if (ll->address == 0) {
     238           3 :                 r = ipv4ll_pick_address(ll);
     239           3 :                 if (r < 0)
     240           0 :                         return r;
     241             : 
     242           3 :                 picked_address = true;
     243             :         }
     244             : 
     245           3 :         r = sd_ipv4acd_start(ll->acd);
     246           3 :         if (r < 0) {
     247             : 
     248             :                 /* We couldn't start? If so, let's forget the picked address again, the user might make a change and
     249             :                  * retry, and we want the new data to take effect when picking an address. */
     250           2 :                 if (picked_address)
     251           2 :                         ll->address = 0;
     252             : 
     253           2 :                 return r;
     254             :         }
     255             : 
     256           1 :         return 0;
     257             : }
     258             : 
     259           6 : int sd_ipv4ll_start(sd_ipv4ll *ll) {
     260           6 :         assert_return(ll, -EINVAL);
     261           6 :         assert_return(sd_ipv4ll_is_running(ll) == 0, -EBUSY);
     262             : 
     263           5 :         return ipv4ll_start_internal(ll, true);
     264             : }
     265             : 
     266           0 : int sd_ipv4ll_restart(sd_ipv4ll *ll) {
     267           0 :         ll->address = 0;
     268             : 
     269           0 :         return ipv4ll_start_internal(ll, false);
     270             : }
     271             : 
     272           1 : static void ipv4ll_client_notify(sd_ipv4ll *ll, int event) {
     273           1 :         assert(ll);
     274             : 
     275           1 :         if (ll->callback)
     276           1 :                 ll->callback(ll, event, ll->userdata);
     277           1 : }
     278             : 
     279           1 : void ipv4ll_on_acd(sd_ipv4acd *acd, int event, void *userdata) {
     280           1 :         sd_ipv4ll *ll = userdata;
     281           2 :         IPV4LL_DONT_DESTROY(ll);
     282             :         int r;
     283             : 
     284           1 :         assert(acd);
     285           1 :         assert(ll);
     286             : 
     287           1 :         switch (event) {
     288             : 
     289           1 :         case SD_IPV4ACD_EVENT_STOP:
     290           1 :                 ipv4ll_client_notify(ll, SD_IPV4LL_EVENT_STOP);
     291           1 :                 ll->claimed_address = 0;
     292           1 :                 break;
     293             : 
     294           0 :         case SD_IPV4ACD_EVENT_BIND:
     295           0 :                 ll->claimed_address = ll->address;
     296           0 :                 ipv4ll_client_notify(ll, SD_IPV4LL_EVENT_BIND);
     297           0 :                 break;
     298             : 
     299           0 :         case SD_IPV4ACD_EVENT_CONFLICT:
     300             :                 /* if an address was already bound we must call up to the
     301             :                    user to handle this, otherwise we just try again */
     302           0 :                 if (ll->claimed_address != 0) {
     303           0 :                         ipv4ll_client_notify(ll, SD_IPV4LL_EVENT_CONFLICT);
     304             : 
     305           0 :                         ll->claimed_address = 0;
     306             :                 } else {
     307           0 :                         r = sd_ipv4ll_restart(ll);
     308           0 :                         if (r < 0)
     309           0 :                                 goto error;
     310             :                 }
     311             : 
     312           0 :                 break;
     313             : 
     314           0 :         default:
     315           0 :                 assert_not_reached("Invalid IPv4ACD event.");
     316             :         }
     317             : 
     318           1 :         return;
     319             : 
     320           0 : error:
     321           0 :         ipv4ll_client_notify(ll, SD_IPV4LL_EVENT_STOP);
     322             : }

Generated by: LCOV version 1.14