]>
Commit | Line | Data |
---|---|---|
db9ecf05 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
6235b3de | 2 | |
edda10f2 | 3 | /* Make sure the net/if.h header is included before any linux/ one */ |
540eb5f0 | 4 | #include <net/if.h> |
9f0cf80d | 5 | #include <linux/if_arp.h> |
ad851cae | 6 | #include <linux/if_bridge.h> |
edda10f2 | 7 | #include <netinet/in.h> |
6235b3de | 8 | |
737f1405 | 9 | #include "bridge.h" |
540eb5f0 | 10 | #include "netlink-util.h" |
23f53b99 | 11 | #include "networkd-manager.h" |
8e2cb51c | 12 | #include "string-table.h" |
0d6c68eb | 13 | #include "vlan-util.h" |
540eb5f0 | 14 | |
ad851cae YW |
15 | assert_cc((int) MULTICAST_ROUTER_NONE == (int) MDB_RTR_TYPE_DISABLED); |
16 | assert_cc((int) MULTICAST_ROUTER_TEMPORARY_QUERY == (int) MDB_RTR_TYPE_TEMP_QUERY); | |
17 | assert_cc((int) MULTICAST_ROUTER_PERMANENT == (int) MDB_RTR_TYPE_PERM); | |
18 | assert_cc((int) MULTICAST_ROUTER_TEMPORARY == (int) MDB_RTR_TYPE_TEMP); | |
19 | ||
8e2cb51c | 20 | static const char* const multicast_router_table[_MULTICAST_ROUTER_MAX] = { |
ad851cae | 21 | [MULTICAST_ROUTER_NONE] = "no", |
8e2cb51c | 22 | [MULTICAST_ROUTER_TEMPORARY_QUERY] = "query", |
ad851cae YW |
23 | [MULTICAST_ROUTER_PERMANENT] = "permanent", |
24 | [MULTICAST_ROUTER_TEMPORARY] = "temporary", | |
8e2cb51c YW |
25 | }; |
26 | ||
27 | DEFINE_STRING_TABLE_LOOKUP_WITH_BOOLEAN(multicast_router, MulticastRouter, _MULTICAST_ROUTER_INVALID); | |
28 | DEFINE_CONFIG_PARSE_ENUM(config_parse_multicast_router, multicast_router, MulticastRouter, | |
29 | "Failed to parse bridge multicast router setting"); | |
30 | ||
5238e957 | 31 | /* callback for bridge netdev's parameter set */ |
302a796f | 32 | static int netdev_bridge_set_handler(sd_netlink *rtnl, sd_netlink_message *m, NetDev *netdev) { |
540eb5f0 SS |
33 | int r; |
34 | ||
35 | assert(netdev); | |
36 | assert(m); | |
37 | ||
38 | r = sd_netlink_message_get_errno(m); | |
39 | if (r < 0) { | |
40 | log_netdev_warning_errno(netdev, r, "Bridge parameters could not be set: %m"); | |
41 | return 1; | |
42 | } | |
43 | ||
82936769 | 44 | log_netdev_debug(netdev, "Bridge parameters set success"); |
540eb5f0 SS |
45 | |
46 | return 1; | |
47 | } | |
48 | ||
81f01be0 | 49 | static int netdev_bridge_post_create_message(NetDev *netdev, sd_netlink_message *req) { |
117843fe | 50 | Bridge *b = BRIDGE(netdev); |
257cebb6 | 51 | int r; |
540eb5f0 SS |
52 | |
53 | r = sd_netlink_message_open_container(req, IFLA_LINKINFO); | |
54 | if (r < 0) | |
81f01be0 | 55 | return r; |
540eb5f0 SS |
56 | |
57 | r = sd_netlink_message_open_container_union(req, IFLA_INFO_DATA, netdev_kind_to_string(netdev->kind)); | |
58 | if (r < 0) | |
81f01be0 | 59 | return r; |
540eb5f0 | 60 | |
1a14863e | 61 | /* convert to jiffes */ |
730389b6 | 62 | if (b->forward_delay != USEC_INFINITY) { |
1a14863e | 63 | r = sd_netlink_message_append_u32(req, IFLA_BR_FORWARD_DELAY, usec_to_jiffies(b->forward_delay)); |
540eb5f0 | 64 | if (r < 0) |
81f01be0 | 65 | return r; |
540eb5f0 SS |
66 | } |
67 | ||
68 | if (b->hello_time > 0) { | |
1a14863e | 69 | r = sd_netlink_message_append_u32(req, IFLA_BR_HELLO_TIME, usec_to_jiffies(b->hello_time)); |
540eb5f0 | 70 | if (r < 0) |
81f01be0 | 71 | return r; |
540eb5f0 SS |
72 | } |
73 | ||
74 | if (b->max_age > 0) { | |
1a14863e | 75 | r = sd_netlink_message_append_u32(req, IFLA_BR_MAX_AGE, usec_to_jiffies(b->max_age)); |
540eb5f0 | 76 | if (r < 0) |
81f01be0 | 77 | return r; |
540eb5f0 SS |
78 | } |
79 | ||
0da81203 | 80 | if (b->ageing_time != USEC_INFINITY) { |
c7440e74 TJ |
81 | r = sd_netlink_message_append_u32(req, IFLA_BR_AGEING_TIME, usec_to_jiffies(b->ageing_time)); |
82 | if (r < 0) | |
81f01be0 | 83 | return r; |
c7440e74 TJ |
84 | } |
85 | ||
86 | if (b->priority > 0) { | |
87 | r = sd_netlink_message_append_u16(req, IFLA_BR_PRIORITY, b->priority); | |
88 | if (r < 0) | |
81f01be0 | 89 | return r; |
c7440e74 TJ |
90 | } |
91 | ||
c4819961 JC |
92 | if (b->group_fwd_mask > 0) { |
93 | r = sd_netlink_message_append_u16(req, IFLA_BR_GROUP_FWD_MASK, b->group_fwd_mask); | |
94 | if (r < 0) | |
81f01be0 | 95 | return r; |
c4819961 JC |
96 | } |
97 | ||
0d6c68eb | 98 | if (b->default_pvid != VLANID_INVALID) { |
c7440e74 TJ |
99 | r = sd_netlink_message_append_u16(req, IFLA_BR_VLAN_DEFAULT_PVID, b->default_pvid); |
100 | if (r < 0) | |
81f01be0 | 101 | return r; |
c7440e74 TJ |
102 | } |
103 | ||
3fef7a3f SS |
104 | if (b->mcast_querier >= 0) { |
105 | r = sd_netlink_message_append_u8(req, IFLA_BR_MCAST_QUERIER, b->mcast_querier); | |
106 | if (r < 0) | |
81f01be0 | 107 | return r; |
3fef7a3f SS |
108 | } |
109 | ||
6df6d898 SS |
110 | if (b->mcast_snooping >= 0) { |
111 | r = sd_netlink_message_append_u8(req, IFLA_BR_MCAST_SNOOPING, b->mcast_snooping); | |
112 | if (r < 0) | |
81f01be0 | 113 | return r; |
6df6d898 SS |
114 | } |
115 | ||
c6f8d17d TJ |
116 | if (b->vlan_filtering >= 0) { |
117 | r = sd_netlink_message_append_u8(req, IFLA_BR_VLAN_FILTERING, b->vlan_filtering); | |
118 | if (r < 0) | |
81f01be0 | 119 | return r; |
c6f8d17d TJ |
120 | } |
121 | ||
4df4df5b | 122 | if (b->vlan_protocol >= 0) { |
6eb35be8 | 123 | r = sd_netlink_message_append_u16(req, IFLA_BR_VLAN_PROTOCOL, htobe16(b->vlan_protocol)); |
4df4df5b | 124 | if (r < 0) |
81f01be0 | 125 | return r; |
4df4df5b RF |
126 | } |
127 | ||
b760a9af SS |
128 | if (b->stp >= 0) { |
129 | r = sd_netlink_message_append_u32(req, IFLA_BR_STP_STATE, b->stp); | |
130 | if (r < 0) | |
81f01be0 | 131 | return r; |
b760a9af SS |
132 | } |
133 | ||
afa51e2d SS |
134 | if (b->igmp_version > 0) { |
135 | r = sd_netlink_message_append_u8(req, IFLA_BR_MCAST_IGMP_VERSION, b->igmp_version); | |
136 | if (r < 0) | |
81f01be0 | 137 | return r; |
afa51e2d SS |
138 | } |
139 | ||
540eb5f0 SS |
140 | r = sd_netlink_message_close_container(req); |
141 | if (r < 0) | |
81f01be0 | 142 | return r; |
540eb5f0 SS |
143 | |
144 | r = sd_netlink_message_close_container(req); | |
145 | if (r < 0) | |
81f01be0 ZJS |
146 | return r; |
147 | ||
148 | return 0; | |
149 | } | |
150 | ||
c2b19b8f | 151 | static int netdev_bridge_post_create(NetDev *netdev, Link *link) { |
81f01be0 ZJS |
152 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL; |
153 | int r; | |
154 | ||
155 | assert(netdev); | |
156 | ||
157 | r = sd_rtnl_message_new_link(netdev->manager->rtnl, &req, RTM_NEWLINK, netdev->ifindex); | |
158 | if (r < 0) | |
159 | return log_netdev_error_errno(netdev, r, "Could not allocate netlink message: %m"); | |
160 | ||
161 | r = sd_netlink_message_set_flags(req, NLM_F_REQUEST | NLM_F_ACK); | |
162 | if (r < 0) | |
163 | return log_link_error_errno(link, r, "Could not set netlink message flags: %m"); | |
164 | ||
165 | r = netdev_bridge_post_create_message(netdev, req); | |
166 | if (r < 0) | |
167 | return log_netdev_error_errno(netdev, r, "Could not create netlink message: %m"); | |
540eb5f0 | 168 | |
302a796f YW |
169 | r = netlink_call_async(netdev->manager->rtnl, NULL, req, netdev_bridge_set_handler, |
170 | netdev_destroy_callback, netdev); | |
540eb5f0 | 171 | if (r < 0) |
81f01be0 | 172 | return log_netdev_error_errno(netdev, r, "Could not send netlink message: %m"); |
540eb5f0 SS |
173 | |
174 | netdev_ref(netdev); | |
175 | ||
176 | return r; | |
177 | } | |
6235b3de | 178 | |
afa51e2d SS |
179 | int config_parse_bridge_igmp_version( |
180 | const char *unit, | |
181 | const char *filename, | |
182 | unsigned line, | |
183 | const char *section, | |
184 | unsigned section_line, | |
185 | const char *lvalue, | |
186 | int ltype, | |
187 | const char *rvalue, | |
188 | void *data, | |
189 | void *userdata) { | |
190 | ||
afa51e2d SS |
191 | assert(filename); |
192 | assert(lvalue); | |
193 | assert(rvalue); | |
194 | assert(data); | |
195 | ||
851cdffd ZJS |
196 | Bridge *b = ASSERT_PTR(userdata); |
197 | ||
afa51e2d SS |
198 | if (isempty(rvalue)) { |
199 | b->igmp_version = 0; /* 0 means unset. */ | |
200 | return 0; | |
201 | } | |
202 | ||
851cdffd ZJS |
203 | return config_parse_uint8_bounded( |
204 | unit, filename, line, section, section_line, lvalue, rvalue, | |
205 | 2, 3, true, | |
206 | &b->igmp_version); | |
afa51e2d SS |
207 | } |
208 | ||
796aa313 YW |
209 | int config_parse_bridge_port_priority( |
210 | const char *unit, | |
211 | const char *filename, | |
212 | unsigned line, | |
213 | const char *section, | |
214 | unsigned section_line, | |
215 | const char *lvalue, | |
216 | int ltype, | |
217 | const char *rvalue, | |
218 | void *data, | |
219 | void *userdata) { | |
220 | ||
796aa313 YW |
221 | assert(filename); |
222 | assert(lvalue); | |
223 | assert(rvalue); | |
796aa313 | 224 | |
851cdffd | 225 | uint16_t *prio = ASSERT_PTR(data); |
796aa313 | 226 | |
851cdffd ZJS |
227 | return config_parse_uint16_bounded( |
228 | unit, filename, line, section, section_line, lvalue, rvalue, | |
229 | 0, LINK_BRIDGE_PORT_PRIORITY_MAX, true, | |
230 | prio); | |
796aa313 YW |
231 | } |
232 | ||
257cebb6 | 233 | static void bridge_init(NetDev *netdev) { |
117843fe | 234 | Bridge *b = BRIDGE(netdev); |
3fef7a3f SS |
235 | |
236 | b->mcast_querier = -1; | |
6df6d898 | 237 | b->mcast_snooping = -1; |
c6f8d17d | 238 | b->vlan_filtering = -1; |
4df4df5b | 239 | b->vlan_protocol = -1; |
b760a9af | 240 | b->stp = -1; |
0d6c68eb | 241 | b->default_pvid = VLANID_INVALID; |
730389b6 | 242 | b->forward_delay = USEC_INFINITY; |
0da81203 | 243 | b->ageing_time = USEC_INFINITY; |
3fef7a3f SS |
244 | } |
245 | ||
3be1d7e0 | 246 | const NetDevVTable bridge_vtable = { |
aa9f1140 | 247 | .object_size = sizeof(Bridge), |
3fef7a3f | 248 | .init = bridge_init, |
130b812f | 249 | .sections = NETDEV_COMMON_SECTIONS "Bridge\0", |
540eb5f0 | 250 | .post_create = netdev_bridge_post_create, |
97b2bc35 | 251 | .create_type = NETDEV_CREATE_INDEPENDENT, |
9f0cf80d | 252 | .iftype = ARPHRD_ETHER, |
5b8bdd20 | 253 | .generate_mac = true, |
3be1d7e0 | 254 | }; |