LCOV - code coverage report
Current view: top level - network/netdev - vxlan.c (source / functions) Hit Total Coverage
Test: main_coverage.info Lines: 0 194 0.0 %
Date: 2019-08-22 15:41:25 Functions: 0 10 0.0 %

          Line data    Source code
       1             : /* SPDX-License-Identifier: LGPL-2.1+ */
       2             : 
       3             : #include <net/if.h>
       4             : 
       5             : #include "sd-netlink.h"
       6             : 
       7             : #include "conf-parser.h"
       8             : #include "alloc-util.h"
       9             : #include "extract-word.h"
      10             : #include "string-table.h"
      11             : #include "string-util.h"
      12             : #include "strv.h"
      13             : #include "parse-util.h"
      14             : #include "missing.h"
      15             : 
      16             : #include "networkd-link.h"
      17             : #include "netdev/vxlan.h"
      18             : 
      19             : static const char* const df_table[_NETDEV_VXLAN_DF_MAX] = {
      20             :         [NETDEV_VXLAN_DF_NO] = "no",
      21             :         [NETDEV_VXLAN_DF_YES] = "yes",
      22             :         [NETDEV_VXLAN_DF_INHERIT] = "inherit",
      23             : };
      24             : 
      25           0 : DEFINE_STRING_TABLE_LOOKUP_WITH_BOOLEAN(df, VxLanDF, NETDEV_VXLAN_DF_YES);
      26           0 : DEFINE_CONFIG_PARSE_ENUM(config_parse_df, df, VxLanDF, "Failed to parse VXLAN IPDoNotFragment= setting");
      27             : 
      28           0 : static int netdev_vxlan_fill_message_create(NetDev *netdev, Link *link, sd_netlink_message *m) {
      29             :         VxLan *v;
      30             :         int r;
      31             : 
      32           0 :         assert(netdev);
      33           0 :         assert(link);
      34           0 :         assert(m);
      35             : 
      36           0 :         v = VXLAN(netdev);
      37             : 
      38           0 :         assert(v);
      39             : 
      40           0 :         if (v->vni <= VXLAN_VID_MAX) {
      41           0 :                 r = sd_netlink_message_append_u32(m, IFLA_VXLAN_ID, v->vni);
      42           0 :                 if (r < 0)
      43           0 :                         return log_netdev_error_errno(netdev, r, "Could not append IFLA_VXLAN_ID attribute: %m");
      44             :         }
      45             : 
      46           0 :         if (in_addr_is_null(v->remote_family, &v->remote) == 0) {
      47           0 :                 if (v->remote_family == AF_INET)
      48           0 :                         r = sd_netlink_message_append_in_addr(m, IFLA_VXLAN_GROUP, &v->remote.in);
      49             :                 else
      50           0 :                         r = sd_netlink_message_append_in6_addr(m, IFLA_VXLAN_GROUP6, &v->remote.in6);
      51           0 :                 if (r < 0)
      52           0 :                         return log_netdev_error_errno(netdev, r, "Could not append IFLA_VXLAN_GROUP attribute: %m");
      53             :         }
      54             : 
      55           0 :         if (in_addr_is_null(v->local_family, &v->local) == 0) {
      56           0 :                 if (v->local_family == AF_INET)
      57           0 :                         r = sd_netlink_message_append_in_addr(m, IFLA_VXLAN_LOCAL, &v->local.in);
      58             :                 else
      59           0 :                         r = sd_netlink_message_append_in6_addr(m, IFLA_VXLAN_LOCAL6, &v->local.in6);
      60           0 :                 if (r < 0)
      61           0 :                         return log_netdev_error_errno(netdev, r, "Could not append IFLA_VXLAN_LOCAL attribute: %m");
      62             :         }
      63             : 
      64           0 :         r = sd_netlink_message_append_u32(m, IFLA_VXLAN_LINK, link->ifindex);
      65           0 :         if (r < 0)
      66           0 :                 return log_netdev_error_errno(netdev, r, "Could not append IFLA_VXLAN_LINK attribute: %m");
      67             : 
      68           0 :         if (v->inherit) {
      69           0 :                 r = sd_netlink_message_append_flag(m, IFLA_VXLAN_TTL_INHERIT);
      70           0 :                 if (r < 0)
      71           0 :                         return log_netdev_error_errno(netdev, r, "Could not append IFLA_VXLAN_TTL_INHERIT attribute: %m");
      72             :         } else {
      73           0 :                 r = sd_netlink_message_append_u8(m, IFLA_VXLAN_TTL, v->ttl);
      74           0 :                 if (r < 0)
      75           0 :                         return log_netdev_error_errno(netdev, r, "Could not append IFLA_VXLAN_TTL attribute: %m");
      76             :         }
      77             : 
      78           0 :         if (v->tos != 0) {
      79           0 :                 r = sd_netlink_message_append_u8(m, IFLA_VXLAN_TOS, v->tos);
      80           0 :                 if (r < 0)
      81           0 :                         return log_netdev_error_errno(netdev, r, "Could not append IFLA_VXLAN_TOS attribute: %m");
      82             :         }
      83             : 
      84           0 :         r = sd_netlink_message_append_u8(m, IFLA_VXLAN_LEARNING, v->learning);
      85           0 :         if (r < 0)
      86           0 :                 return log_netdev_error_errno(netdev, r, "Could not append IFLA_VXLAN_LEARNING attribute: %m");
      87             : 
      88           0 :         r = sd_netlink_message_append_u8(m, IFLA_VXLAN_RSC, v->route_short_circuit);
      89           0 :         if (r < 0)
      90           0 :                 return log_netdev_error_errno(netdev, r, "Could not append IFLA_VXLAN_RSC attribute: %m");
      91             : 
      92           0 :         r = sd_netlink_message_append_u8(m, IFLA_VXLAN_PROXY, v->arp_proxy);
      93           0 :         if (r < 0)
      94           0 :                 return log_netdev_error_errno(netdev, r, "Could not append IFLA_VXLAN_PROXY attribute: %m");
      95             : 
      96           0 :         r = sd_netlink_message_append_u8(m, IFLA_VXLAN_L2MISS, v->l2miss);
      97           0 :         if (r < 0)
      98           0 :                 return log_netdev_error_errno(netdev, r, "Could not append IFLA_VXLAN_L2MISS attribute: %m");
      99             : 
     100           0 :         r = sd_netlink_message_append_u8(m, IFLA_VXLAN_L3MISS, v->l3miss);
     101           0 :         if (r < 0)
     102           0 :                 return log_netdev_error_errno(netdev, r, "Could not append IFLA_VXLAN_L3MISS attribute: %m");
     103             : 
     104           0 :         if (v->fdb_ageing != 0) {
     105           0 :                 r = sd_netlink_message_append_u32(m, IFLA_VXLAN_AGEING, v->fdb_ageing / USEC_PER_SEC);
     106           0 :                 if (r < 0)
     107           0 :                         return log_netdev_error_errno(netdev, r, "Could not append IFLA_VXLAN_AGEING attribute: %m");
     108             :         }
     109             : 
     110           0 :         if (v->max_fdb != 0) {
     111           0 :                 r = sd_netlink_message_append_u32(m, IFLA_VXLAN_LIMIT, v->max_fdb);
     112           0 :                 if (r < 0)
     113           0 :                         return log_netdev_error_errno(netdev, r, "Could not append IFLA_VXLAN_LIMIT attribute: %m");
     114             :         }
     115             : 
     116           0 :         r = sd_netlink_message_append_u8(m, IFLA_VXLAN_UDP_CSUM, v->udpcsum);
     117           0 :         if (r < 0)
     118           0 :                 return log_netdev_error_errno(netdev, r, "Could not append IFLA_VXLAN_UDP_CSUM attribute: %m");
     119             : 
     120           0 :         r = sd_netlink_message_append_u8(m, IFLA_VXLAN_UDP_ZERO_CSUM6_TX, v->udp6zerocsumtx);
     121           0 :         if (r < 0)
     122           0 :                 return log_netdev_error_errno(netdev, r, "Could not append IFLA_VXLAN_UDP_ZERO_CSUM6_TX attribute: %m");
     123             : 
     124           0 :         r = sd_netlink_message_append_u8(m, IFLA_VXLAN_UDP_ZERO_CSUM6_RX, v->udp6zerocsumrx);
     125           0 :         if (r < 0)
     126           0 :                 return log_netdev_error_errno(netdev, r, "Could not append IFLA_VXLAN_UDP_ZERO_CSUM6_RX attribute: %m");
     127             : 
     128           0 :         r = sd_netlink_message_append_u8(m, IFLA_VXLAN_REMCSUM_TX, v->remote_csum_tx);
     129           0 :         if (r < 0)
     130           0 :                 return log_netdev_error_errno(netdev, r, "Could not append IFLA_VXLAN_REMCSUM_TX attribute: %m");
     131             : 
     132           0 :         r = sd_netlink_message_append_u8(m, IFLA_VXLAN_REMCSUM_RX, v->remote_csum_rx);
     133           0 :         if (r < 0)
     134           0 :                 return log_netdev_error_errno(netdev, r, "Could not append IFLA_VXLAN_REMCSUM_RX attribute: %m");
     135             : 
     136           0 :         r = sd_netlink_message_append_u16(m, IFLA_VXLAN_PORT, htobe16(v->dest_port));
     137           0 :         if (r < 0)
     138           0 :                 return log_netdev_error_errno(netdev, r, "Could not append IFLA_VXLAN_PORT attribute: %m");
     139             : 
     140           0 :         if (v->port_range.low != 0 || v->port_range.high != 0) {
     141             :                 struct ifla_vxlan_port_range port_range;
     142             : 
     143           0 :                 port_range.low = htobe16(v->port_range.low);
     144           0 :                 port_range.high = htobe16(v->port_range.high);
     145             : 
     146           0 :                 r = sd_netlink_message_append_data(m, IFLA_VXLAN_PORT_RANGE, &port_range, sizeof(port_range));
     147           0 :                 if (r < 0)
     148           0 :                         return log_netdev_error_errno(netdev, r, "Could not append IFLA_VXLAN_PORT_RANGE attribute: %m");
     149             :         }
     150             : 
     151           0 :         r = sd_netlink_message_append_u32(m, IFLA_VXLAN_LABEL, htobe32(v->flow_label));
     152           0 :         if (r < 0)
     153           0 :                 return log_netdev_error_errno(netdev, r, "Could not append IFLA_VXLAN_LABEL attribute: %m");
     154             : 
     155           0 :         if (v->group_policy) {
     156           0 :                 r = sd_netlink_message_append_flag(m, IFLA_VXLAN_GBP);
     157           0 :                 if (r < 0)
     158           0 :                         return log_netdev_error_errno(netdev, r, "Could not append IFLA_VXLAN_GBP attribute: %m");
     159             :         }
     160             : 
     161           0 :         if (v->generic_protocol_extension) {
     162           0 :                 r = sd_netlink_message_append_flag(m, IFLA_VXLAN_GPE);
     163           0 :                 if (r < 0)
     164           0 :                         return log_netdev_error_errno(netdev, r, "Could not append IFLA_VXLAN_GPE attribute: %m");
     165             :         }
     166             : 
     167           0 :         if (v->df != _NETDEV_VXLAN_DF_INVALID) {
     168           0 :                 r = sd_netlink_message_append_u8(m, IFLA_VXLAN_DF, v->df);
     169           0 :                 if (r < 0)
     170           0 :                         return log_netdev_error_errno(netdev, r, "Could not append IFLA_VXLAN_DF attribute: %m");
     171             :         }
     172             : 
     173           0 :         return r;
     174             : }
     175             : 
     176           0 : int config_parse_vxlan_address(const char *unit,
     177             :                                const char *filename,
     178             :                                unsigned line,
     179             :                                const char *section,
     180             :                                unsigned section_line,
     181             :                                const char *lvalue,
     182             :                                int ltype,
     183             :                                const char *rvalue,
     184             :                                void *data,
     185             :                                void *userdata) {
     186           0 :         VxLan *v = userdata;
     187           0 :         union in_addr_union *addr = data, buffer;
     188             :         int r, f;
     189             : 
     190           0 :         assert(filename);
     191           0 :         assert(lvalue);
     192           0 :         assert(rvalue);
     193           0 :         assert(data);
     194             : 
     195           0 :         r = in_addr_from_string_auto(rvalue, &f, &buffer);
     196           0 :         if (r < 0) {
     197           0 :                 log_syntax(unit, LOG_ERR, filename, line, r, "vxlan '%s' address is invalid, ignoring assignment: %s", lvalue, rvalue);
     198           0 :                 return 0;
     199             :         }
     200             : 
     201           0 :         r = in_addr_is_multicast(f, &buffer);
     202             : 
     203           0 :         if (streq(lvalue, "Group")) {
     204           0 :                 if (r <= 0) {
     205           0 :                         log_syntax(unit, LOG_ERR, filename, line, 0, "vxlan %s invalid multicast address, ignoring assignment: %s", lvalue, rvalue);
     206           0 :                         return 0;
     207             :                 }
     208             : 
     209           0 :                 v->group_family = f;
     210             :         } else {
     211           0 :                 if (r > 0) {
     212           0 :                         log_syntax(unit, LOG_ERR, filename, line, 0, "vxlan %s cannot be a multicast address, ignoring assignment: %s", lvalue, rvalue);
     213           0 :                         return 0;
     214             :                 }
     215             : 
     216           0 :                 if (streq(lvalue, "Remote"))
     217           0 :                         v->remote_family = f;
     218             :                 else
     219           0 :                         v->local_family = f;
     220             :         }
     221             : 
     222           0 :         *addr = buffer;
     223             : 
     224           0 :         return 0;
     225             : }
     226             : 
     227           0 : int config_parse_port_range(const char *unit,
     228             :                             const char *filename,
     229             :                             unsigned line,
     230             :                             const char *section,
     231             :                             unsigned section_line,
     232             :                             const char *lvalue,
     233             :                             int ltype,
     234             :                             const char *rvalue,
     235             :                             void *data,
     236             :                             void *userdata) {
     237           0 :         VxLan *v = userdata;
     238             :         uint16_t low, high;
     239             :         int r;
     240             : 
     241           0 :         assert(filename);
     242           0 :         assert(lvalue);
     243           0 :         assert(rvalue);
     244           0 :         assert(data);
     245             : 
     246           0 :         r = parse_ip_port_range(rvalue, &low, &high);
     247           0 :         if (r < 0) {
     248           0 :                 log_syntax(unit, LOG_ERR, filename, line, r,
     249             :                            "Failed to parse VXLAN port range '%s'. Port should be greater than 0 and less than 65535.", rvalue);
     250           0 :                 return 0;
     251             :         }
     252             : 
     253           0 :         v->port_range.low = low;
     254           0 :         v->port_range.high = high;
     255             : 
     256           0 :         return 0;
     257             : }
     258             : 
     259           0 : int config_parse_flow_label(const char *unit,
     260             :                             const char *filename,
     261             :                             unsigned line,
     262             :                             const char *section,
     263             :                             unsigned section_line,
     264             :                             const char *lvalue,
     265             :                             int ltype,
     266             :                             const char *rvalue,
     267             :                             void *data,
     268             :                             void *userdata) {
     269           0 :         VxLan *v = userdata;
     270             :         unsigned f;
     271             :         int r;
     272             : 
     273           0 :         assert(filename);
     274           0 :         assert(lvalue);
     275           0 :         assert(rvalue);
     276           0 :         assert(data);
     277             : 
     278           0 :         r = safe_atou(rvalue, &f);
     279           0 :         if (r < 0) {
     280           0 :                 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse VXLAN flow label '%s'.", rvalue);
     281           0 :                 return 0;
     282             :         }
     283             : 
     284           0 :         if (f & ~VXLAN_FLOW_LABEL_MAX_MASK) {
     285           0 :                 log_syntax(unit, LOG_ERR, filename, line, r,
     286             :                            "VXLAN flow label '%s' not valid. Flow label range should be [0-1048575].", rvalue);
     287           0 :                 return 0;
     288             :         }
     289             : 
     290           0 :         v->flow_label = f;
     291             : 
     292           0 :         return 0;
     293             : }
     294             : 
     295           0 : int config_parse_vxlan_ttl(const char *unit,
     296             :                            const char *filename,
     297             :                            unsigned line,
     298             :                            const char *section,
     299             :                            unsigned section_line,
     300             :                            const char *lvalue,
     301             :                            int ltype,
     302             :                            const char *rvalue,
     303             :                             void *data,
     304             :                            void *userdata) {
     305           0 :         VxLan *v = userdata;
     306             :         unsigned f;
     307             :         int r;
     308             : 
     309           0 :         assert(filename);
     310           0 :         assert(lvalue);
     311           0 :         assert(rvalue);
     312           0 :         assert(data);
     313             : 
     314           0 :         if (streq(rvalue, "inherit"))
     315           0 :                 v->inherit = true;
     316             :         else {
     317           0 :                 r = safe_atou(rvalue, &f);
     318           0 :                 if (r < 0) {
     319           0 :                         log_syntax(unit, LOG_ERR, filename, line, r,
     320             :                                    "Failed to parse VXLAN TTL '%s', ignoring assignment: %m", rvalue);
     321           0 :                         return 0;
     322             :                 }
     323             : 
     324           0 :                 if (f > 255) {
     325           0 :                         log_syntax(unit, LOG_ERR, filename, line, 0,
     326             :                                    "Invalid VXLAN TTL '%s'. TTL must be <= 255. Ignoring assignment.", rvalue);
     327           0 :                         return 0;
     328             :                 }
     329             : 
     330           0 :                 v->ttl = f;
     331             :         }
     332             : 
     333           0 :         return 0;
     334             : }
     335             : 
     336           0 : static int netdev_vxlan_verify(NetDev *netdev, const char *filename) {
     337           0 :         VxLan *v = VXLAN(netdev);
     338             : 
     339           0 :         assert(netdev);
     340           0 :         assert(v);
     341           0 :         assert(filename);
     342             : 
     343           0 :         if (v->vni > VXLAN_VID_MAX)
     344           0 :                 return log_netdev_warning_errno(netdev, SYNTHETIC_ERRNO(EINVAL),
     345             :                                                 "%s: VXLAN without valid VNI (or VXLAN Segment ID) configured. Ignoring.",
     346             :                                                 filename);
     347             : 
     348           0 :         if (v->ttl > 255)
     349           0 :                 return log_netdev_warning_errno(netdev, SYNTHETIC_ERRNO(EINVAL),
     350             :                                                 "%s: VXLAN TTL must be <= 255. Ignoring.",
     351             :                                                 filename);
     352             : 
     353           0 :         if (!v->dest_port && v->generic_protocol_extension)
     354           0 :                 v->dest_port = 4790;
     355             : 
     356           0 :         return 0;
     357             : }
     358             : 
     359           0 : static void vxlan_init(NetDev *netdev) {
     360             :         VxLan *v;
     361             : 
     362           0 :         assert(netdev);
     363             : 
     364           0 :         v = VXLAN(netdev);
     365             : 
     366           0 :         assert(v);
     367             : 
     368           0 :         v->vni = VXLAN_VID_MAX + 1;
     369           0 :         v->df = _NETDEV_VXLAN_DF_INVALID;
     370           0 :         v->learning = true;
     371           0 :         v->udpcsum = false;
     372           0 :         v->udp6zerocsumtx = false;
     373           0 :         v->udp6zerocsumrx = false;
     374           0 : }
     375             : 
     376             : const NetDevVTable vxlan_vtable = {
     377             :         .object_size = sizeof(VxLan),
     378             :         .init = vxlan_init,
     379             :         .sections = "Match\0NetDev\0VXLAN\0",
     380             :         .fill_message_create = netdev_vxlan_fill_message_create,
     381             :         .create_type = NETDEV_CREATE_STACKED,
     382             :         .config_verify = netdev_vxlan_verify,
     383             :         .generate_mac = true,
     384             : };

Generated by: LCOV version 1.14