]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/network/netdev/vlan.c
Add SPDX license identifiers to source files under the LGPL
[thirdparty/systemd.git] / src / network / netdev / vlan.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 This file is part of systemd.
4
5 Copyright 2013 Tom Gundersen <teg@jklm.no>
6
7 systemd is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version.
11
12 systemd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
19 ***/
20
21 #include <linux/if_vlan.h>
22 #include <net/if.h>
23
24 #include "netdev/vlan.h"
25 #include "vlan-util.h"
26
27 static int netdev_vlan_fill_message_create(NetDev *netdev, Link *link, sd_netlink_message *req) {
28 struct ifla_vlan_flags flags = {};
29 VLan *v;
30 int r;
31
32 assert(netdev);
33 assert(link);
34 assert(req);
35
36 v = VLAN(netdev);
37
38 assert(v);
39
40 r = sd_netlink_message_append_u16(req, IFLA_VLAN_ID, v->id);
41 if (r < 0)
42 return log_netdev_error_errno(netdev, r, "Could not append IFLA_VLAN_ID attribute: %m");
43
44 if (v->gvrp != -1) {
45 flags.mask |= VLAN_FLAG_GVRP;
46 SET_FLAG(flags.flags, VLAN_FLAG_GVRP, v->gvrp);
47 }
48
49 if (v->mvrp != -1) {
50 flags.mask |= VLAN_FLAG_MVRP;
51 SET_FLAG(flags.flags, VLAN_FLAG_MVRP, v->mvrp);
52 }
53
54 if (v->reorder_hdr != -1) {
55 flags.mask |= VLAN_FLAG_REORDER_HDR;
56 SET_FLAG(flags.flags, VLAN_FLAG_REORDER_HDR, v->reorder_hdr);
57 }
58
59 if (v->loose_binding != -1) {
60 flags.mask |= VLAN_FLAG_LOOSE_BINDING;
61 SET_FLAG(flags.flags, VLAN_FLAG_LOOSE_BINDING, v->loose_binding);
62 }
63
64 r = sd_netlink_message_append_data(req, IFLA_VLAN_FLAGS, &flags, sizeof(struct ifla_vlan_flags));
65 if (r < 0)
66 return log_netdev_error_errno(netdev, r, "Could not append IFLA_VLAN_FLAGS attribute: %m");
67
68 return 0;
69 }
70
71 static int netdev_vlan_verify(NetDev *netdev, const char *filename) {
72 VLan *v;
73
74 assert(netdev);
75 assert(filename);
76
77 v = VLAN(netdev);
78
79 assert(v);
80
81 if (v->id == VLANID_INVALID) {
82 log_warning("VLAN without valid Id (%"PRIu16") configured in %s.", v->id, filename);
83 return -EINVAL;
84 }
85
86 return 0;
87 }
88
89 static void vlan_init(NetDev *netdev) {
90 VLan *v = VLAN(netdev);
91
92 assert(netdev);
93 assert(v);
94
95 v->id = VLANID_INVALID;
96 v->gvrp = -1;
97 v->mvrp = -1;
98 v->loose_binding = -1;
99 v->reorder_hdr = -1;
100 }
101
102 const NetDevVTable vlan_vtable = {
103 .object_size = sizeof(VLan),
104 .init = vlan_init,
105 .sections = "Match\0NetDev\0VLAN\0",
106 .fill_message_create = netdev_vlan_fill_message_create,
107 .create_type = NETDEV_CREATE_STACKED,
108 .config_verify = netdev_vlan_verify,
109 };