]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/network/netdev/vlan.c
tree-wide: make sure net/if.h is included before any linux/ header
[thirdparty/systemd.git] / src / network / netdev / vlan.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 /* Make sure the net/if.h header is included before any linux/ one */
4 #include <net/if.h>
5 #include <errno.h>
6 #include <linux/if_arp.h>
7 #include <linux/if_vlan.h>
8
9 #include "parse-util.h"
10 #include "vlan-util.h"
11 #include "vlan.h"
12
13 static int netdev_vlan_fill_message_create(NetDev *netdev, Link *link, sd_netlink_message *req) {
14 assert(link);
15 assert(req);
16
17 struct ifla_vlan_flags flags = {};
18 VLan *v = VLAN(netdev);
19 int r;
20
21 r = sd_netlink_message_append_u16(req, IFLA_VLAN_ID, v->id);
22 if (r < 0)
23 return r;
24
25 if (v->protocol >= 0) {
26 r = sd_netlink_message_append_u16(req, IFLA_VLAN_PROTOCOL, htobe16(v->protocol));
27 if (r < 0)
28 return r;
29 }
30
31 if (v->gvrp != -1) {
32 flags.mask |= VLAN_FLAG_GVRP;
33 SET_FLAG(flags.flags, VLAN_FLAG_GVRP, v->gvrp);
34 }
35
36 if (v->mvrp != -1) {
37 flags.mask |= VLAN_FLAG_MVRP;
38 SET_FLAG(flags.flags, VLAN_FLAG_MVRP, v->mvrp);
39 }
40
41 if (v->reorder_hdr != -1) {
42 flags.mask |= VLAN_FLAG_REORDER_HDR;
43 SET_FLAG(flags.flags, VLAN_FLAG_REORDER_HDR, v->reorder_hdr);
44 }
45
46 if (v->loose_binding != -1) {
47 flags.mask |= VLAN_FLAG_LOOSE_BINDING;
48 SET_FLAG(flags.flags, VLAN_FLAG_LOOSE_BINDING, v->loose_binding);
49 }
50
51 r = sd_netlink_message_append_data(req, IFLA_VLAN_FLAGS, &flags, sizeof(struct ifla_vlan_flags));
52 if (r < 0)
53 return r;
54
55 if (!set_isempty(v->egress_qos_maps)) {
56 struct ifla_vlan_qos_mapping *m;
57
58 r = sd_netlink_message_open_container(req, IFLA_VLAN_EGRESS_QOS);
59 if (r < 0)
60 return r;
61
62 SET_FOREACH(m, v->egress_qos_maps) {
63 r = sd_netlink_message_append_data(req, IFLA_VLAN_QOS_MAPPING, m, sizeof(struct ifla_vlan_qos_mapping));
64 if (r < 0)
65 return r;
66 }
67
68 r = sd_netlink_message_close_container(req);
69 if (r < 0)
70 return r;
71 }
72
73 if (!set_isempty(v->ingress_qos_maps)) {
74 struct ifla_vlan_qos_mapping *m;
75
76 r = sd_netlink_message_open_container(req, IFLA_VLAN_INGRESS_QOS);
77 if (r < 0)
78 return r;
79
80 SET_FOREACH(m, v->ingress_qos_maps) {
81 r = sd_netlink_message_append_data(req, IFLA_VLAN_QOS_MAPPING, m, sizeof(struct ifla_vlan_qos_mapping));
82 if (r < 0)
83 return r;
84 }
85
86 r = sd_netlink_message_close_container(req);
87 if (r < 0)
88 return r;
89 }
90
91 return 0;
92 }
93
94 static void vlan_qos_maps_hash_func(const struct ifla_vlan_qos_mapping *x, struct siphash *state) {
95 siphash24_compress_typesafe(x->from, state);
96 siphash24_compress_typesafe(x->to, state);
97 }
98
99 static int vlan_qos_maps_compare_func(const struct ifla_vlan_qos_mapping *a, const struct ifla_vlan_qos_mapping *b) {
100 int r;
101
102 r = CMP(a->from, b->from);
103 if (r != 0)
104 return r;
105
106 return CMP(a->to, b->to);
107 }
108
109 DEFINE_PRIVATE_HASH_OPS_WITH_KEY_DESTRUCTOR(
110 vlan_qos_maps_hash_ops,
111 struct ifla_vlan_qos_mapping,
112 vlan_qos_maps_hash_func,
113 vlan_qos_maps_compare_func,
114 free);
115
116 int config_parse_vlan_qos_maps(
117 const char *unit,
118 const char *filename,
119 unsigned line,
120 const char *section,
121 unsigned section_line,
122 const char *lvalue,
123 int ltype,
124 const char *rvalue,
125 void *data,
126 void *userdata) {
127
128 Set **s = ASSERT_PTR(data);
129 int r;
130
131 assert(filename);
132 assert(lvalue);
133 assert(rvalue);
134
135 if (isempty(rvalue)) {
136 *s = set_free(*s);
137 return 0;
138 }
139
140 for (const char *p = rvalue;;) {
141 _cleanup_free_ struct ifla_vlan_qos_mapping *m = NULL;
142 _cleanup_free_ char *w = NULL;
143 unsigned from, to;
144
145 r = extract_first_word(&p, &w, NULL, EXTRACT_CUNESCAPE|EXTRACT_UNQUOTE);
146 if (r == -ENOMEM)
147 return log_oom();
148 if (r < 0) {
149 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to parse %s, ignoring: %s", lvalue, rvalue);
150 return 0;
151 }
152 if (r == 0)
153 return 0;
154
155 r = parse_range(w, &from, &to);
156 if (r < 0) {
157 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to parse %s, ignoring: %s", lvalue, w);
158 continue;
159 }
160
161 m = new(struct ifla_vlan_qos_mapping, 1);
162 if (!m)
163 return log_oom();
164
165 *m = (struct ifla_vlan_qos_mapping) {
166 .from = from,
167 .to = to,
168 };
169
170 r = set_ensure_consume(s, &vlan_qos_maps_hash_ops, TAKE_PTR(m));
171 if (r < 0) {
172 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to store %s, ignoring: %s", lvalue, w);
173 continue;
174 }
175 }
176 }
177
178 static int netdev_vlan_verify(NetDev *netdev, const char *filename) {
179 assert(filename);
180
181 VLan *v = VLAN(netdev);
182
183 if (v->id == VLANID_INVALID) {
184 log_netdev_warning(netdev, "VLAN without valid Id (%"PRIu16") configured in %s.", v->id, filename);
185 return -EINVAL;
186 }
187
188 return 0;
189 }
190
191 static void vlan_done(NetDev *netdev) {
192 VLan *v = VLAN(netdev);
193
194 set_free(v->egress_qos_maps);
195 set_free(v->ingress_qos_maps);
196 }
197
198 static void vlan_init(NetDev *netdev) {
199 VLan *v = VLAN(netdev);
200
201 v->id = VLANID_INVALID;
202 v->protocol = -1;
203 v->gvrp = -1;
204 v->mvrp = -1;
205 v->loose_binding = -1;
206 v->reorder_hdr = -1;
207 }
208
209 const NetDevVTable vlan_vtable = {
210 .object_size = sizeof(VLan),
211 .init = vlan_init,
212 .sections = NETDEV_COMMON_SECTIONS "VLAN\0",
213 .fill_message_create = netdev_vlan_fill_message_create,
214 .create_type = NETDEV_CREATE_STACKED,
215 .config_verify = netdev_vlan_verify,
216 .done = vlan_done,
217 .iftype = ARPHRD_ETHER,
218 };