1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <net/if.h>
4 #include <netinet/in.h>
5 #include <linux/if_arp.h>
6
7 #include "conf-parser.h"
8 #include "ipvlan.h"
9 #include "ipvlan-util.h"
10 #include "networkd-link.h"
11 #include "string-util.h"
12
13 DEFINE_CONFIG_PARSE_ENUM(config_parse_ipvlan_mode, ipvlan_mode, IPVlanMode, "Failed to parse ipvlan mode");
14 DEFINE_CONFIG_PARSE_ENUM(config_parse_ipvlan_flags, ipvlan_flags, IPVlanFlags, "Failed to parse ipvlan flags");
15
netdev_ipvlan_fill_message_create(NetDev * netdev,Link * link,sd_netlink_message * req)16 static int netdev_ipvlan_fill_message_create(NetDev *netdev, Link *link, sd_netlink_message *req) {
17 IPVlan *m;
18 int r;
19
20 assert(netdev);
21 assert(link);
22 assert(netdev->ifname);
23
24 if (netdev->kind == NETDEV_KIND_IPVLAN)
25 m = IPVLAN(netdev);
26 else
27 m = IPVTAP(netdev);
28
29 assert(m);
30
31 if (m->mode != _NETDEV_IPVLAN_MODE_INVALID) {
32 r = sd_netlink_message_append_u16(req, IFLA_IPVLAN_MODE, m->mode);
33 if (r < 0)
34 return r;
35 }
36
37 if (m->flags != _NETDEV_IPVLAN_FLAGS_INVALID) {
38 r = sd_netlink_message_append_u16(req, IFLA_IPVLAN_FLAGS, m->flags);
39 if (r < 0)
40 return r;
41 }
42
43 return 0;
44 }
45
ipvlan_init(NetDev * n)46 static void ipvlan_init(NetDev *n) {
47 IPVlan *m;
48
49 assert(n);
50
51 if (n->kind == NETDEV_KIND_IPVLAN)
52 m = IPVLAN(n);
53 else
54 m = IPVTAP(n);
55
56 assert(m);
57
58 m->mode = _NETDEV_IPVLAN_MODE_INVALID;
59 m->flags = _NETDEV_IPVLAN_FLAGS_INVALID;
60 }
61
62 const NetDevVTable ipvlan_vtable = {
63 .object_size = sizeof(IPVlan),
64 .init = ipvlan_init,
65 .sections = NETDEV_COMMON_SECTIONS "IPVLAN\0",
66 .fill_message_create = netdev_ipvlan_fill_message_create,
67 .create_type = NETDEV_CREATE_STACKED,
68 .iftype = ARPHRD_ETHER,
69 .generate_mac = true,
70 };
71
72 const NetDevVTable ipvtap_vtable = {
73 .object_size = sizeof(IPVlan),
74 .init = ipvlan_init,
75 .sections = NETDEV_COMMON_SECTIONS "IPVTAP\0",
76 .fill_message_create = netdev_ipvlan_fill_message_create,
77 .create_type = NETDEV_CREATE_STACKED,
78 .iftype = ARPHRD_ETHER,
79 .generate_mac = true,
80 };
81
link_get_ipvlan_mode(Link * link)82 IPVlanMode link_get_ipvlan_mode(Link *link) {
83 IPVlan *ipvlan;
84
85 assert(link);
86
87 ipvlan = IPVLAN(link->netdev);
88 if (!ipvlan)
89 return _NETDEV_IPVLAN_MODE_INVALID;
90
91 return ipvlan->mode;
92 }
93