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