Debian Codegraph @

src/network/netdev/ipvlan.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <linux/if_arp.h>
4
5 #include "sd-netlink.h"
6
7 #include "conf-parser.h"
8 #include "ipvlan.h"
9 #include "ipvlan-util.h"
10 #include "networkd-link.h"
11
12 DEFINE_CONFIG_PARSE_ENUM(config_parse_ipvlan_mode, ipvlan_mode, IPVlanMode);
13 DEFINE_CONFIG_PARSE_ENUM(config_parse_ipvlan_flags, ipvlan_flags, IPVlanFlags);
14
15 static int netdev_ipvlan_fill_message_create(NetDev *netdev, Link *link, sd_netlink_message *req) {
16 assert(netdev);
17 assert(link);
18 assert(netdev->ifname);
19
20 IPVlan *m = netdev->kind == NETDEV_KIND_IPVLAN ? IPVLAN(netdev) : IPVTAP(netdev);
21 int r;
22
23 if (m->mode != _NETDEV_IPVLAN_MODE_INVALID) {
24 r = sd_netlink_message_append_u16(req, IFLA_IPVLAN_MODE, m->mode);
25 if (r < 0)
26 return r;
27 }
28
29 if (m->flags != _NETDEV_IPVLAN_FLAGS_INVALID) {
30 r = sd_netlink_message_append_u16(req, IFLA_IPVLAN_FLAGS, m->flags);
31 if (r < 0)
32 return r;
33 }
34
35 return 0;
36 }
37
38 static bool ipvlan_can_set_mac(NetDev *netdev, const struct hw_addr_data *hw_addr) {
39 assert(netdev);
40
41 /* MAC address cannot be updated. Even unchanged, IFLA_ADDRESS attribute cannot be set in the message. */
42 return netdev->ifindex <= 0;
43 }
44
45 static void ipvlan_init(NetDev *netdev) {
46 IPVlan *m = ASSERT_PTR(netdev)->kind == NETDEV_KIND_IPVLAN ? IPVLAN(netdev) : IPVTAP(netdev);
47
48 m->mode = _NETDEV_IPVLAN_MODE_INVALID;
49 m->flags = _NETDEV_IPVLAN_FLAGS_INVALID;
50 }
51
52 const NetDevVTable ipvlan_vtable = {
53 .object_size = sizeof(IPVlan),
54 .init = ipvlan_init,
55 .sections = NETDEV_COMMON_SECTIONS "IPVLAN\0",
56 .fill_message_create = netdev_ipvlan_fill_message_create,
57 .create_type = NETDEV_CREATE_STACKED,
58 .can_set_mac = ipvlan_can_set_mac,
59 .iftype = ARPHRD_ETHER,
60 .generate_mac = true,
61 .keep_existing = true,
62 };
63
64 const NetDevVTable ipvtap_vtable = {
65 .object_size = sizeof(IPVlan),
66 .init = ipvlan_init,
67 .sections = NETDEV_COMMON_SECTIONS "IPVTAP\0",
68 .fill_message_create = netdev_ipvlan_fill_message_create,
69 .create_type = NETDEV_CREATE_STACKED,
70 .can_set_mac = ipvlan_can_set_mac,
71 .iftype = ARPHRD_ETHER,
72 .generate_mac = true,
73 .keep_existing = true,
74 };
75
76 IPVlanMode link_get_ipvlan_mode(Link *link) {
77 assert(link);
78
79 if (!link->netdev || link->netdev->kind != NETDEV_KIND_IPVLAN)
80 return _NETDEV_IPVLAN_MODE_INVALID;
81
82 return IPVLAN(link->netdev)->mode;
83 }