LCOV - code coverage report
Current view: top level - network - networkd-ipv4ll.c (source / functions) Hit Total Coverage
Test: systemd test coverage Lines: 0 137 0.0 %
Date: 2015-07-29 18:47:03 Functions: 0 6 0.0 %

          Line data    Source code
       1             : /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
       2             : 
       3             : /***
       4             :   This file is part of systemd.
       5             : 
       6             :   Copyright 2013-2014 Tom Gundersen <teg@jklm.no>
       7             : 
       8             :   systemd is free software; you can redistribute it and/or modify it
       9             :   under the terms of the GNU Lesser General Public License as published by
      10             :   the Free Software Foundation; either version 2.1 of the License, or
      11             :   (at your option) any later version.
      12             : 
      13             :   systemd is distributed in the hope that it will be useful, but
      14             :   WITHOUT ANY WARRANTY; without even the implied warranty of
      15             :   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
      16             :   Lesser General Public License for more details.
      17             : 
      18             :   You should have received a copy of the GNU Lesser General Public License
      19             :   along with systemd; If not, see <http://www.gnu.org/licenses/>.
      20             : ***/
      21             : 
      22             : #include <netinet/ether.h>
      23             : #include <linux/if.h>
      24             : 
      25             : #include "networkd-link.h"
      26             : #include "network-internal.h"
      27             : 
      28           0 : static int ipv4ll_address_lost(Link *link) {
      29           0 :         _cleanup_address_free_ Address *address = NULL;
      30           0 :         _cleanup_route_free_ Route *route = NULL;
      31             :         struct in_addr addr;
      32             :         int r;
      33             : 
      34           0 :         assert(link);
      35             : 
      36           0 :         link->ipv4ll_route = false;
      37           0 :         link->ipv4ll_address = false;
      38             : 
      39           0 :         r = sd_ipv4ll_get_address(link->ipv4ll, &addr);
      40           0 :         if (r < 0)
      41           0 :                 return 0;
      42             : 
      43           0 :         log_link_debug(link, "IPv4 link-local release %u.%u.%u.%u", ADDRESS_FMT_VAL(addr));
      44             : 
      45           0 :         r = address_new_dynamic(&address);
      46           0 :         if (r < 0) {
      47           0 :                 log_link_error(link, "Could not allocate address: %s", strerror(-r));
      48           0 :                 return r;
      49             :         }
      50             : 
      51           0 :         address->family = AF_INET;
      52           0 :         address->in_addr.in = addr;
      53           0 :         address->prefixlen = 16;
      54           0 :         address->scope = RT_SCOPE_LINK;
      55             : 
      56           0 :         address_drop(address, link, &link_address_drop_handler);
      57             : 
      58           0 :         r = route_new_dynamic(&route, RTPROT_UNSPEC);
      59           0 :         if (r < 0) {
      60           0 :                 log_link_error(link, "Could not allocate route: %s",
      61             :                                strerror(-r));
      62           0 :                 return r;
      63             :         }
      64             : 
      65           0 :         route->family = AF_INET;
      66           0 :         route->scope = RT_SCOPE_LINK;
      67           0 :         route->metrics = IPV4LL_ROUTE_METRIC;
      68             : 
      69           0 :         route_drop(route, link, &link_route_drop_handler);
      70             : 
      71           0 :         link_client_handler(link);
      72             : 
      73           0 :         return 0;
      74             : }
      75             : 
      76           0 : static int ipv4ll_route_handler(sd_netlink *rtnl, sd_netlink_message *m, void *userdata) {
      77           0 :         _cleanup_link_unref_ Link *link = userdata;
      78             :         int r;
      79             : 
      80           0 :         assert(link);
      81           0 :         assert(!link->ipv4ll_route);
      82             : 
      83           0 :         r = sd_netlink_message_get_errno(m);
      84           0 :         if (r < 0 && r != -EEXIST) {
      85           0 :                 log_link_error(link, "could not set ipv4ll route: %s", strerror(-r));
      86           0 :                 link_enter_failed(link);
      87             :         }
      88             : 
      89           0 :         link->ipv4ll_route = true;
      90             : 
      91           0 :         if (link->ipv4ll_address == true)
      92           0 :                 link_client_handler(link);
      93             : 
      94           0 :         return 1;
      95             : }
      96             : 
      97           0 : static int ipv4ll_address_handler(sd_netlink *rtnl, sd_netlink_message *m, void *userdata) {
      98           0 :         _cleanup_link_unref_ Link *link = userdata;
      99             :         int r;
     100             : 
     101           0 :         assert(link);
     102           0 :         assert(!link->ipv4ll_address);
     103             : 
     104           0 :         r = sd_netlink_message_get_errno(m);
     105           0 :         if (r < 0 && r != -EEXIST) {
     106           0 :                 log_link_error(link, "could not set ipv4ll address: %s", strerror(-r));
     107           0 :                 link_enter_failed(link);
     108           0 :         } else if (r >= 0)
     109           0 :                 link_rtnl_process_address(rtnl, m, link->manager);
     110             : 
     111           0 :         link->ipv4ll_address = true;
     112             : 
     113           0 :         if (link->ipv4ll_route == true)
     114           0 :                 link_client_handler(link);
     115             : 
     116           0 :         return 1;
     117             : }
     118             : 
     119           0 : static int ipv4ll_address_claimed(sd_ipv4ll *ll, Link *link) {
     120           0 :         _cleanup_address_free_ Address *ll_addr = NULL;
     121           0 :         _cleanup_route_free_ Route *route = NULL;
     122             :         struct in_addr address;
     123             :         int r;
     124             : 
     125           0 :         assert(ll);
     126           0 :         assert(link);
     127             : 
     128           0 :         r = sd_ipv4ll_get_address(ll, &address);
     129           0 :         if (r == -ENOENT)
     130           0 :                 return 0;
     131           0 :         else if (r < 0)
     132           0 :                 return r;
     133             : 
     134           0 :         log_link_debug(link, "IPv4 link-local claim %u.%u.%u.%u",
     135             :                        ADDRESS_FMT_VAL(address));
     136             : 
     137           0 :         r = address_new_dynamic(&ll_addr);
     138           0 :         if (r < 0)
     139           0 :                 return r;
     140             : 
     141           0 :         ll_addr->family = AF_INET;
     142           0 :         ll_addr->in_addr.in = address;
     143           0 :         ll_addr->prefixlen = 16;
     144           0 :         ll_addr->broadcast.s_addr = ll_addr->in_addr.in.s_addr | htonl(0xfffffffflu >> ll_addr->prefixlen);
     145           0 :         ll_addr->scope = RT_SCOPE_LINK;
     146             : 
     147           0 :         r = address_configure(ll_addr, link, ipv4ll_address_handler);
     148           0 :         if (r < 0)
     149           0 :                 return r;
     150             : 
     151           0 :         link->ipv4ll_address = false;
     152             : 
     153           0 :         r = route_new_dynamic(&route, RTPROT_STATIC);
     154           0 :         if (r < 0)
     155           0 :                 return r;
     156             : 
     157           0 :         route->family = AF_INET;
     158           0 :         route->scope = RT_SCOPE_LINK;
     159           0 :         route->metrics = IPV4LL_ROUTE_METRIC;
     160             : 
     161           0 :         r = route_configure(route, link, ipv4ll_route_handler);
     162           0 :         if (r < 0)
     163           0 :                 return r;
     164             : 
     165           0 :         link->ipv4ll_route = false;
     166             : 
     167           0 :         return 0;
     168             : }
     169             : 
     170           0 : static void ipv4ll_handler(sd_ipv4ll *ll, int event, void *userdata){
     171           0 :         Link *link = userdata;
     172             :         int r;
     173             : 
     174           0 :         assert(link);
     175           0 :         assert(link->network);
     176           0 :         assert(link->manager);
     177             : 
     178           0 :         if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
     179           0 :                 return;
     180             : 
     181           0 :         switch(event) {
     182             :                 case IPV4LL_EVENT_STOP:
     183             :                 case IPV4LL_EVENT_CONFLICT:
     184           0 :                         r = ipv4ll_address_lost(link);
     185           0 :                         if (r < 0) {
     186           0 :                                 link_enter_failed(link);
     187           0 :                                 return;
     188             :                         }
     189           0 :                         break;
     190             :                 case IPV4LL_EVENT_BIND:
     191           0 :                         r = ipv4ll_address_claimed(ll, link);
     192           0 :                         if (r < 0) {
     193           0 :                                 link_enter_failed(link);
     194           0 :                                 return;
     195             :                         }
     196           0 :                         break;
     197             :                 default:
     198           0 :                         if (event < 0)
     199           0 :                                 log_link_warning(link, "IPv4 link-local error: %s", strerror(-event));
     200             :                         else
     201           0 :                                 log_link_warning(link, "IPv4 link-local unknown event: %d", event);
     202           0 :                         break;
     203             :         }
     204             : }
     205             : 
     206           0 : int ipv4ll_configure(Link *link) {
     207             :         uint8_t seed[8];
     208             :         int r;
     209             : 
     210           0 :         assert(link);
     211           0 :         assert(link->network);
     212           0 :         assert(link->network->link_local & ADDRESS_FAMILY_IPV4);
     213             : 
     214           0 :         r = sd_ipv4ll_new(&link->ipv4ll);
     215           0 :         if (r < 0)
     216           0 :                 return r;
     217             : 
     218           0 :         if (link->udev_device) {
     219           0 :                 r = net_get_unique_predictable_data(link->udev_device, seed);
     220           0 :                 if (r >= 0) {
     221           0 :                         r = sd_ipv4ll_set_address_seed(link->ipv4ll, seed);
     222           0 :                         if (r < 0)
     223           0 :                                 return r;
     224             :                 }
     225             :         }
     226             : 
     227           0 :         r = sd_ipv4ll_attach_event(link->ipv4ll, NULL, 0);
     228           0 :         if (r < 0)
     229           0 :                 return r;
     230             : 
     231           0 :         r = sd_ipv4ll_set_mac(link->ipv4ll, &link->mac);
     232           0 :         if (r < 0)
     233           0 :                 return r;
     234             : 
     235           0 :         r = sd_ipv4ll_set_index(link->ipv4ll, link->ifindex);
     236           0 :         if (r < 0)
     237           0 :                 return r;
     238             : 
     239           0 :         r = sd_ipv4ll_set_callback(link->ipv4ll, ipv4ll_handler, link);
     240           0 :         if (r < 0)
     241           0 :                 return r;
     242             : 
     243           0 :         return 0;
     244             : }

Generated by: LCOV version 1.11