]>
Commit | Line | Data |
---|---|---|
718e3744 | 1 | /* |
2 | * Interface looking up by netlink. | |
3 | * Copyright (C) 1998 Kunihiro Ishiguro | |
4 | * | |
5 | * This file is part of GNU Zebra. | |
6 | * | |
7 | * GNU Zebra is free software; you can redistribute it and/or modify it | |
8 | * under the terms of the GNU General Public License as published by the | |
9 | * Free Software Foundation; either version 2, or (at your option) any | |
10 | * later version. | |
11 | * | |
12 | * GNU Zebra 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 | * General Public License for more details. | |
16 | * | |
896014f4 DL |
17 | * You should have received a copy of the GNU General Public License along |
18 | * with this program; see the file COPYING; if not, write to the Free Software | |
19 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
718e3744 | 20 | */ |
21 | ||
22 | #include <zebra.h> | |
6675513d | 23 | |
ddfeb486 DL |
24 | #ifdef GNU_LINUX |
25 | ||
6675513d | 26 | /* The following definition is to workaround an issue in the Linux kernel |
27 | * header files with redefinition of 'struct in6_addr' in both | |
28 | * netinet/in.h and linux/in6.h. | |
29 | * Reference - https://sourceware.org/ml/libc-alpha/2013-01/msg00599.html | |
30 | */ | |
31 | #define _LINUX_IN6_H | |
077c07cc PG |
32 | #define _LINUX_IF_H |
33 | #define _LINUX_IP_H | |
6675513d | 34 | |
ba85366a | 35 | #include <netinet/if_ether.h> |
6675513d | 36 | #include <linux/if_bridge.h> |
ba777396 | 37 | #include <linux/if_link.h> |
077c07cc | 38 | #include <linux/if_tunnel.h> |
1fdc9eae | 39 | #include <net/if_arp.h> |
535fe877 DS |
40 | #include <linux/sockios.h> |
41 | #include <linux/ethtool.h> | |
1fdc9eae | 42 | |
43 | #include "linklist.h" | |
44 | #include "if.h" | |
45 | #include "log.h" | |
46 | #include "prefix.h" | |
47 | #include "connected.h" | |
48 | #include "table.h" | |
49 | #include "memory.h" | |
1fdc9eae | 50 | #include "rib.h" |
51 | #include "thread.h" | |
52 | #include "privs.h" | |
53 | #include "nexthop.h" | |
54 | #include "vrf.h" | |
7922fc65 | 55 | #include "vrf_int.h" |
1fdc9eae | 56 | #include "mpls.h" |
174482ef | 57 | #include "lib_errors.h" |
718e3744 | 58 | |
5e6a74d8 | 59 | #include "vty.h" |
7dbeea9d | 60 | #include "zebra/zserv.h" |
1fdc9eae | 61 | #include "zebra/zebra_ns.h" |
62 | #include "zebra/zebra_vrf.h" | |
63 | #include "zebra/rt.h" | |
64 | #include "zebra/redistribute.h" | |
65 | #include "zebra/interface.h" | |
66 | #include "zebra/debug.h" | |
67 | #include "zebra/rtadv.h" | |
68 | #include "zebra/zebra_ptm.h" | |
69 | #include "zebra/zebra_mpls.h" | |
70 | #include "zebra/kernel_netlink.h" | |
d9f5b2f5 | 71 | #include "zebra/rt_netlink.h" |
1fdc9eae | 72 | #include "zebra/if_netlink.h" |
9df414fe | 73 | #include "zebra/zebra_errors.h" |
97c4e1d0 | 74 | #include "zebra/zebra_vxlan.h" |
42b56639 | 75 | #include "zebra/zebra_evpn_mh.h" |
e3d3fa06 | 76 | #include "zebra/zebra_l2.h" |
1fdc9eae | 77 | |
0268f30e | 78 | extern struct zebra_privs_t zserv_privs; |
1fdc9eae | 79 | |
80 | /* Note: on netlink systems, there should be a 1-to-1 mapping between interface | |
81 | names and ifindex values. */ | |
d62a17ae | 82 | static void set_ifindex(struct interface *ifp, ifindex_t ifi_index, |
83 | struct zebra_ns *zns) | |
1fdc9eae | 84 | { |
d62a17ae | 85 | struct interface *oifp; |
86 | ||
87 | if (((oifp = if_lookup_by_index_per_ns(zns, ifi_index)) != NULL) | |
88 | && (oifp != ifp)) { | |
89 | if (ifi_index == IFINDEX_INTERNAL) | |
af4c2728 | 90 | flog_err( |
450971aa | 91 | EC_LIB_INTERFACE, |
4d43f68a | 92 | "Netlink is setting interface %s ifindex to reserved internal value %u", |
d62a17ae | 93 | ifp->name, ifi_index); |
94 | else { | |
95 | if (IS_ZEBRA_DEBUG_KERNEL) | |
96 | zlog_debug( | |
97 | "interface index %d was renamed from %s to %s", | |
98 | ifi_index, oifp->name, ifp->name); | |
99 | if (if_is_up(oifp)) | |
af4c2728 | 100 | flog_err( |
450971aa | 101 | EC_LIB_INTERFACE, |
4d43f68a | 102 | "interface rename detected on up interface: index %d was renamed from %s to %s, results are uncertain!", |
d62a17ae | 103 | ifi_index, oifp->name, ifp->name); |
104 | if_delete_update(oifp); | |
105 | } | |
106 | } | |
ff880b78 | 107 | if_set_index(ifp, ifi_index); |
1fdc9eae | 108 | } |
109 | ||
110 | /* Utility function to parse hardware link-layer address and update ifp */ | |
d62a17ae | 111 | static void netlink_interface_update_hw_addr(struct rtattr **tb, |
112 | struct interface *ifp) | |
1fdc9eae | 113 | { |
d62a17ae | 114 | int i; |
115 | ||
116 | if (tb[IFLA_ADDRESS]) { | |
117 | int hw_addr_len; | |
118 | ||
119 | hw_addr_len = RTA_PAYLOAD(tb[IFLA_ADDRESS]); | |
120 | ||
121 | if (hw_addr_len > INTERFACE_HWADDR_MAX) | |
9df414fe QY |
122 | zlog_debug("Hardware address is too large: %d", |
123 | hw_addr_len); | |
d62a17ae | 124 | else { |
125 | ifp->hw_addr_len = hw_addr_len; | |
126 | memcpy(ifp->hw_addr, RTA_DATA(tb[IFLA_ADDRESS]), | |
127 | hw_addr_len); | |
128 | ||
129 | for (i = 0; i < hw_addr_len; i++) | |
130 | if (ifp->hw_addr[i] != 0) | |
131 | break; | |
132 | ||
133 | if (i == hw_addr_len) | |
134 | ifp->hw_addr_len = 0; | |
135 | else | |
136 | ifp->hw_addr_len = hw_addr_len; | |
137 | } | |
138 | } | |
1fdc9eae | 139 | } |
140 | ||
d62a17ae | 141 | static enum zebra_link_type netlink_to_zebra_link_type(unsigned int hwt) |
1fdc9eae | 142 | { |
d62a17ae | 143 | switch (hwt) { |
144 | case ARPHRD_ETHER: | |
145 | return ZEBRA_LLT_ETHER; | |
146 | case ARPHRD_EETHER: | |
147 | return ZEBRA_LLT_EETHER; | |
148 | case ARPHRD_AX25: | |
149 | return ZEBRA_LLT_AX25; | |
150 | case ARPHRD_PRONET: | |
151 | return ZEBRA_LLT_PRONET; | |
152 | case ARPHRD_IEEE802: | |
153 | return ZEBRA_LLT_IEEE802; | |
154 | case ARPHRD_ARCNET: | |
155 | return ZEBRA_LLT_ARCNET; | |
156 | case ARPHRD_APPLETLK: | |
157 | return ZEBRA_LLT_APPLETLK; | |
158 | case ARPHRD_DLCI: | |
159 | return ZEBRA_LLT_DLCI; | |
160 | case ARPHRD_ATM: | |
161 | return ZEBRA_LLT_ATM; | |
162 | case ARPHRD_METRICOM: | |
163 | return ZEBRA_LLT_METRICOM; | |
164 | case ARPHRD_IEEE1394: | |
165 | return ZEBRA_LLT_IEEE1394; | |
166 | case ARPHRD_EUI64: | |
167 | return ZEBRA_LLT_EUI64; | |
168 | case ARPHRD_INFINIBAND: | |
169 | return ZEBRA_LLT_INFINIBAND; | |
170 | case ARPHRD_SLIP: | |
171 | return ZEBRA_LLT_SLIP; | |
172 | case ARPHRD_CSLIP: | |
173 | return ZEBRA_LLT_CSLIP; | |
174 | case ARPHRD_SLIP6: | |
175 | return ZEBRA_LLT_SLIP6; | |
176 | case ARPHRD_CSLIP6: | |
177 | return ZEBRA_LLT_CSLIP6; | |
178 | case ARPHRD_RSRVD: | |
179 | return ZEBRA_LLT_RSRVD; | |
180 | case ARPHRD_ADAPT: | |
181 | return ZEBRA_LLT_ADAPT; | |
182 | case ARPHRD_ROSE: | |
183 | return ZEBRA_LLT_ROSE; | |
184 | case ARPHRD_X25: | |
185 | return ZEBRA_LLT_X25; | |
186 | case ARPHRD_PPP: | |
187 | return ZEBRA_LLT_PPP; | |
188 | case ARPHRD_CISCO: | |
189 | return ZEBRA_LLT_CHDLC; | |
190 | case ARPHRD_LAPB: | |
191 | return ZEBRA_LLT_LAPB; | |
192 | case ARPHRD_RAWHDLC: | |
193 | return ZEBRA_LLT_RAWHDLC; | |
194 | case ARPHRD_TUNNEL: | |
195 | return ZEBRA_LLT_IPIP; | |
196 | case ARPHRD_TUNNEL6: | |
197 | return ZEBRA_LLT_IPIP6; | |
198 | case ARPHRD_FRAD: | |
199 | return ZEBRA_LLT_FRAD; | |
200 | case ARPHRD_SKIP: | |
201 | return ZEBRA_LLT_SKIP; | |
202 | case ARPHRD_LOOPBACK: | |
203 | return ZEBRA_LLT_LOOPBACK; | |
204 | case ARPHRD_LOCALTLK: | |
205 | return ZEBRA_LLT_LOCALTLK; | |
206 | case ARPHRD_FDDI: | |
207 | return ZEBRA_LLT_FDDI; | |
208 | case ARPHRD_SIT: | |
209 | return ZEBRA_LLT_SIT; | |
210 | case ARPHRD_IPDDP: | |
211 | return ZEBRA_LLT_IPDDP; | |
212 | case ARPHRD_IPGRE: | |
213 | return ZEBRA_LLT_IPGRE; | |
214 | case ARPHRD_PIMREG: | |
215 | return ZEBRA_LLT_PIMREG; | |
216 | case ARPHRD_HIPPI: | |
217 | return ZEBRA_LLT_HIPPI; | |
218 | case ARPHRD_ECONET: | |
219 | return ZEBRA_LLT_ECONET; | |
220 | case ARPHRD_IRDA: | |
221 | return ZEBRA_LLT_IRDA; | |
222 | case ARPHRD_FCPP: | |
223 | return ZEBRA_LLT_FCPP; | |
224 | case ARPHRD_FCAL: | |
225 | return ZEBRA_LLT_FCAL; | |
226 | case ARPHRD_FCPL: | |
227 | return ZEBRA_LLT_FCPL; | |
228 | case ARPHRD_FCFABRIC: | |
229 | return ZEBRA_LLT_FCFABRIC; | |
230 | case ARPHRD_IEEE802_TR: | |
231 | return ZEBRA_LLT_IEEE802_TR; | |
232 | case ARPHRD_IEEE80211: | |
233 | return ZEBRA_LLT_IEEE80211; | |
4268e09e | 234 | #ifdef ARPHRD_IEEE802154 |
d62a17ae | 235 | case ARPHRD_IEEE802154: |
236 | return ZEBRA_LLT_IEEE802154; | |
4268e09e | 237 | #endif |
1fdc9eae | 238 | #ifdef ARPHRD_IP6GRE |
d62a17ae | 239 | case ARPHRD_IP6GRE: |
240 | return ZEBRA_LLT_IP6GRE; | |
1fdc9eae | 241 | #endif |
242 | #ifdef ARPHRD_IEEE802154_PHY | |
d62a17ae | 243 | case ARPHRD_IEEE802154_PHY: |
244 | return ZEBRA_LLT_IEEE802154_PHY; | |
1fdc9eae | 245 | #endif |
246 | ||
d62a17ae | 247 | default: |
248 | return ZEBRA_LLT_UNKNOWN; | |
249 | } | |
1fdc9eae | 250 | } |
251 | ||
42b56639 | 252 | static inline void zebra_if_set_ziftype(struct interface *ifp, |
e6f2bec0 | 253 | enum zebra_iftype zif_type, |
a81982fa | 254 | enum zebra_slave_iftype zif_slave_type) |
42b56639 AK |
255 | { |
256 | struct zebra_if *zif; | |
257 | ||
258 | zif = (struct zebra_if *)ifp->info; | |
259 | zif->zif_slave_type = zif_slave_type; | |
260 | ||
261 | if (zif->zif_type != zif_type) { | |
262 | zif->zif_type = zif_type; | |
263 | /* If the if_type has been set to bond initialize ES info | |
264 | * against it. XXX - note that we don't handle the case where | |
265 | * a zif changes from bond to non-bond; it is really | |
266 | * an unexpected/error condition. | |
267 | */ | |
268 | zebra_evpn_if_init(zif); | |
269 | } | |
270 | } | |
271 | ||
b9368db9 | 272 | static void netlink_determine_zebra_iftype(const char *kind, |
e6f2bec0 | 273 | enum zebra_iftype *zif_type) |
6675513d | 274 | { |
d62a17ae | 275 | *zif_type = ZEBRA_IF_OTHER; |
276 | ||
277 | if (!kind) | |
278 | return; | |
279 | ||
280 | if (strcmp(kind, "vrf") == 0) | |
281 | *zif_type = ZEBRA_IF_VRF; | |
282 | else if (strcmp(kind, "bridge") == 0) | |
283 | *zif_type = ZEBRA_IF_BRIDGE; | |
284 | else if (strcmp(kind, "vlan") == 0) | |
285 | *zif_type = ZEBRA_IF_VLAN; | |
286 | else if (strcmp(kind, "vxlan") == 0) | |
287 | *zif_type = ZEBRA_IF_VXLAN; | |
1a98c087 MK |
288 | else if (strcmp(kind, "macvlan") == 0) |
289 | *zif_type = ZEBRA_IF_MACVLAN; | |
0e4864ea PG |
290 | else if (strcmp(kind, "veth") == 0) |
291 | *zif_type = ZEBRA_IF_VETH; | |
b9368db9 DD |
292 | else if (strcmp(kind, "bond") == 0) |
293 | *zif_type = ZEBRA_IF_BOND; | |
294 | else if (strcmp(kind, "bond_slave") == 0) | |
295 | *zif_type = ZEBRA_IF_BOND_SLAVE; | |
077c07cc PG |
296 | else if (strcmp(kind, "gre") == 0) |
297 | *zif_type = ZEBRA_IF_GRE; | |
6675513d | 298 | } |
52d8f0d8 | 299 | |
d62a17ae | 300 | static void netlink_vrf_change(struct nlmsghdr *h, struct rtattr *tb, |
5e031198 | 301 | uint32_t ns_id, const char *name) |
1fdc9eae | 302 | { |
d62a17ae | 303 | struct ifinfomsg *ifi; |
304 | struct rtattr *linkinfo[IFLA_INFO_MAX + 1]; | |
305 | struct rtattr *attr[IFLA_VRF_MAX + 1]; | |
75d26fb3 | 306 | struct vrf *vrf = NULL; |
d62a17ae | 307 | struct zebra_vrf *zvrf; |
d7c0a89a | 308 | uint32_t nl_table_id; |
d62a17ae | 309 | |
310 | ifi = NLMSG_DATA(h); | |
311 | ||
c9d842c7 | 312 | netlink_parse_rtattr_nested(linkinfo, IFLA_INFO_MAX, tb); |
d62a17ae | 313 | |
314 | if (!linkinfo[IFLA_INFO_DATA]) { | |
315 | if (IS_ZEBRA_DEBUG_KERNEL) | |
316 | zlog_debug( | |
317 | "%s: IFLA_INFO_DATA missing from VRF message: %s", | |
318 | __func__, name); | |
319 | return; | |
320 | } | |
321 | ||
c9d842c7 DS |
322 | netlink_parse_rtattr_nested(attr, IFLA_VRF_MAX, |
323 | linkinfo[IFLA_INFO_DATA]); | |
d62a17ae | 324 | if (!attr[IFLA_VRF_TABLE]) { |
325 | if (IS_ZEBRA_DEBUG_KERNEL) | |
326 | zlog_debug( | |
327 | "%s: IFLA_VRF_TABLE missing from VRF message: %s", | |
328 | __func__, name); | |
329 | return; | |
1fdc9eae | 330 | } |
331 | ||
d7c0a89a | 332 | nl_table_id = *(uint32_t *)RTA_DATA(attr[IFLA_VRF_TABLE]); |
d62a17ae | 333 | |
334 | if (h->nlmsg_type == RTM_NEWLINK) { | |
335 | if (IS_ZEBRA_DEBUG_KERNEL) | |
336 | zlog_debug("RTM_NEWLINK for VRF %s(%u) table %u", name, | |
337 | ifi->ifi_index, nl_table_id); | |
338 | ||
2e86d16d RW |
339 | if (!vrf_lookup_by_id((vrf_id_t)ifi->ifi_index)) { |
340 | vrf_id_t exist_id; | |
5e031198 | 341 | |
2e86d16d RW |
342 | exist_id = vrf_lookup_by_table(nl_table_id, ns_id); |
343 | if (exist_id != VRF_DEFAULT) { | |
344 | vrf = vrf_lookup_by_id(exist_id); | |
345 | ||
346 | flog_err( | |
347 | EC_ZEBRA_VRF_MISCONFIGURED, | |
348 | "VRF %s id %u table id overlaps existing vrf %s, misconfiguration exiting", | |
349 | name, ifi->ifi_index, vrf->name); | |
350 | exit(-1); | |
351 | } | |
5e031198 | 352 | } |
2e86d16d | 353 | |
75d26fb3 | 354 | vrf = vrf_update((vrf_id_t)ifi->ifi_index, name); |
d62a17ae | 355 | if (!vrf) { |
450971aa | 356 | flog_err(EC_LIB_INTERFACE, "VRF %s id %u not created", |
1c50c1c0 | 357 | name, ifi->ifi_index); |
d62a17ae | 358 | return; |
359 | } | |
360 | ||
d62a17ae | 361 | /* |
362 | * This is the only place that we get the actual kernel table_id | |
363 | * being used. We need it to set the table_id of the routes | |
364 | * we are passing to the kernel.... And to throw some totally | |
365 | * awesome parties. that too. | |
593406a1 DS |
366 | * |
367 | * At this point we *must* have a zvrf because the vrf_create | |
368 | * callback creates one. We *must* set the table id | |
369 | * before the vrf_enable because of( at the very least ) | |
370 | * static routes being delayed for installation until | |
371 | * during the vrf_enable callbacks. | |
d62a17ae | 372 | */ |
373 | zvrf = (struct zebra_vrf *)vrf->info; | |
374 | zvrf->table_id = nl_table_id; | |
593406a1 DS |
375 | |
376 | /* Enable the created VRF. */ | |
377 | if (!vrf_enable(vrf)) { | |
450971aa | 378 | flog_err(EC_LIB_INTERFACE, |
1c50c1c0 QY |
379 | "Failed to enable VRF %s id %u", name, |
380 | ifi->ifi_index); | |
593406a1 DS |
381 | return; |
382 | } | |
383 | ||
d62a17ae | 384 | } else // h->nlmsg_type == RTM_DELLINK |
385 | { | |
386 | if (IS_ZEBRA_DEBUG_KERNEL) | |
387 | zlog_debug("RTM_DELLINK for VRF %s(%u)", name, | |
388 | ifi->ifi_index); | |
389 | ||
390 | vrf = vrf_lookup_by_id((vrf_id_t)ifi->ifi_index); | |
391 | ||
392 | if (!vrf) { | |
e914ccbe | 393 | flog_warn(EC_ZEBRA_VRF_NOT_FOUND, "%s: vrf not found", |
9df414fe | 394 | __func__); |
d62a17ae | 395 | return; |
396 | } | |
397 | ||
398 | vrf_delete(vrf); | |
399 | } | |
1fdc9eae | 400 | } |
401 | ||
67188ca2 | 402 | static uint32_t get_iflink_speed(struct interface *interface, int *error) |
535fe877 | 403 | { |
d62a17ae | 404 | struct ifreq ifdata; |
405 | struct ethtool_cmd ecmd; | |
406 | int sd; | |
407 | int rc; | |
0268f30e | 408 | const char *ifname = interface->name; |
d62a17ae | 409 | |
594c2878 JF |
410 | if (error) |
411 | *error = 0; | |
d62a17ae | 412 | /* initialize struct */ |
413 | memset(&ifdata, 0, sizeof(ifdata)); | |
414 | ||
415 | /* set interface name */ | |
0af35d90 | 416 | strlcpy(ifdata.ifr_name, ifname, sizeof(ifdata.ifr_name)); |
d62a17ae | 417 | |
418 | /* initialize ethtool interface */ | |
419 | memset(&ecmd, 0, sizeof(ecmd)); | |
420 | ecmd.cmd = ETHTOOL_GSET; /* ETHTOOL_GLINK */ | |
ba85366a | 421 | ifdata.ifr_data = (caddr_t)&ecmd; |
d62a17ae | 422 | |
423 | /* use ioctl to get IP address of an interface */ | |
0cf6db21 | 424 | frr_with_privs(&zserv_privs) { |
01b9e3fd | 425 | sd = vrf_socket(PF_INET, SOCK_DGRAM, IPPROTO_IP, |
a36898e7 | 426 | interface->vrf_id, |
01b9e3fd DL |
427 | NULL); |
428 | if (sd < 0) { | |
429 | if (IS_ZEBRA_DEBUG_KERNEL) | |
430 | zlog_debug("Failure to read interface %s speed: %d %s", | |
431 | ifname, errno, safe_strerror(errno)); | |
594c2878 JF |
432 | /* no vrf socket creation may probably mean vrf issue */ |
433 | if (error) | |
434 | *error = -1; | |
01b9e3fd DL |
435 | return 0; |
436 | } | |
d62a17ae | 437 | /* Get the current link state for the interface */ |
a36898e7 | 438 | rc = vrf_ioctl(interface->vrf_id, sd, SIOCETHTOOL, |
633fc9b1 | 439 | (char *)&ifdata); |
01b9e3fd | 440 | } |
d62a17ae | 441 | if (rc < 0) { |
f767cee4 | 442 | if (errno != EOPNOTSUPP && IS_ZEBRA_DEBUG_KERNEL) |
bd7d0299 DS |
443 | zlog_debug( |
444 | "IOCTL failure to read interface %s speed: %d %s", | |
445 | ifname, errno, safe_strerror(errno)); | |
594c2878 JF |
446 | /* no device means interface unreachable */ |
447 | if (errno == ENODEV && error) | |
448 | *error = -1; | |
d62a17ae | 449 | ecmd.speed_hi = 0; |
450 | ecmd.speed = 0; | |
451 | } | |
452 | ||
453 | close(sd); | |
454 | ||
67188ca2 | 455 | return ((uint32_t)ecmd.speed_hi << 16) | ecmd.speed; |
535fe877 DS |
456 | } |
457 | ||
594c2878 | 458 | uint32_t kernel_get_speed(struct interface *ifp, int *error) |
dc7b3cae | 459 | { |
594c2878 | 460 | return get_iflink_speed(ifp, error); |
dc7b3cae DS |
461 | } |
462 | ||
62b4b7e4 PG |
463 | static ssize_t |
464 | netlink_gre_set_msg_encoder(struct zebra_dplane_ctx *ctx, void *buf, | |
465 | size_t buflen) | |
466 | { | |
467 | struct { | |
468 | struct nlmsghdr n; | |
469 | struct ifinfomsg ifi; | |
470 | char buf[]; | |
471 | } *req = buf; | |
472 | uint32_t link_idx; | |
db51f0cd | 473 | unsigned int mtu; |
62b4b7e4 | 474 | struct rtattr *rta_info, *rta_data; |
e3d3fa06 | 475 | const struct zebra_l2info_gre *gre_info; |
62b4b7e4 PG |
476 | |
477 | if (buflen < sizeof(*req)) | |
478 | return 0; | |
479 | memset(req, 0, sizeof(*req)); | |
480 | ||
481 | req->n.nlmsg_type = RTM_NEWLINK; | |
482 | req->n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg)); | |
483 | req->n.nlmsg_flags = NLM_F_REQUEST; | |
484 | ||
485 | req->ifi.ifi_index = dplane_ctx_get_ifindex(ctx); | |
e3d3fa06 PG |
486 | |
487 | gre_info = dplane_ctx_gre_get_info(ctx); | |
488 | if (!gre_info) | |
489 | return 0; | |
490 | ||
62b4b7e4 PG |
491 | req->ifi.ifi_change = 0xFFFFFFFF; |
492 | link_idx = dplane_ctx_gre_get_link_ifindex(ctx); | |
db51f0cd PG |
493 | mtu = dplane_ctx_gre_get_mtu(ctx); |
494 | ||
495 | if (mtu && !nl_attr_put32(&req->n, buflen, IFLA_MTU, mtu)) | |
496 | return 0; | |
62b4b7e4 PG |
497 | |
498 | rta_info = nl_attr_nest(&req->n, buflen, IFLA_LINKINFO); | |
499 | if (!rta_info) | |
500 | return 0; | |
e3d3fa06 | 501 | |
62b4b7e4 PG |
502 | if (!nl_attr_put(&req->n, buflen, IFLA_INFO_KIND, "gre", 3)) |
503 | return 0; | |
e3d3fa06 | 504 | |
62b4b7e4 | 505 | rta_data = nl_attr_nest(&req->n, buflen, IFLA_INFO_DATA); |
e3d3fa06 | 506 | if (!rta_data) |
62b4b7e4 | 507 | return 0; |
e3d3fa06 | 508 | |
62b4b7e4 PG |
509 | if (!nl_attr_put32(&req->n, buflen, IFLA_GRE_LINK, link_idx)) |
510 | return 0; | |
e3d3fa06 PG |
511 | |
512 | if (gre_info->vtep_ip.s_addr && | |
513 | !nl_attr_put32(&req->n, buflen, IFLA_GRE_LOCAL, | |
514 | gre_info->vtep_ip.s_addr)) | |
515 | return 0; | |
516 | ||
517 | if (gre_info->vtep_ip_remote.s_addr && | |
518 | !nl_attr_put32(&req->n, buflen, IFLA_GRE_REMOTE, | |
519 | gre_info->vtep_ip_remote.s_addr)) | |
520 | return 0; | |
521 | ||
522 | if (gre_info->ikey && | |
523 | !nl_attr_put32(&req->n, buflen, IFLA_GRE_IKEY, | |
524 | gre_info->ikey)) | |
525 | return 0; | |
526 | if (gre_info->okey && | |
527 | !nl_attr_put32(&req->n, buflen, IFLA_GRE_IKEY, | |
528 | gre_info->okey)) | |
529 | return 0; | |
530 | ||
62b4b7e4 PG |
531 | nl_attr_nest_end(&req->n, rta_data); |
532 | nl_attr_nest_end(&req->n, rta_info); | |
533 | ||
534 | return NLMSG_ALIGN(req->n.nlmsg_len); | |
535 | } | |
536 | ||
d62a17ae | 537 | static int netlink_extract_bridge_info(struct rtattr *link_data, |
538 | struct zebra_l2info_bridge *bridge_info) | |
6675513d | 539 | { |
d62a17ae | 540 | struct rtattr *attr[IFLA_BR_MAX + 1]; |
541 | ||
542 | memset(bridge_info, 0, sizeof(*bridge_info)); | |
c9d842c7 | 543 | netlink_parse_rtattr_nested(attr, IFLA_BR_MAX, link_data); |
d62a17ae | 544 | if (attr[IFLA_BR_VLAN_FILTERING]) |
545 | bridge_info->vlan_aware = | |
d7c0a89a | 546 | *(uint8_t *)RTA_DATA(attr[IFLA_BR_VLAN_FILTERING]); |
d62a17ae | 547 | return 0; |
6675513d | 548 | } |
549 | ||
d62a17ae | 550 | static int netlink_extract_vlan_info(struct rtattr *link_data, |
551 | struct zebra_l2info_vlan *vlan_info) | |
6675513d | 552 | { |
d62a17ae | 553 | struct rtattr *attr[IFLA_VLAN_MAX + 1]; |
554 | vlanid_t vid_in_msg; | |
555 | ||
556 | memset(vlan_info, 0, sizeof(*vlan_info)); | |
c9d842c7 | 557 | netlink_parse_rtattr_nested(attr, IFLA_VLAN_MAX, link_data); |
d62a17ae | 558 | if (!attr[IFLA_VLAN_ID]) { |
559 | if (IS_ZEBRA_DEBUG_KERNEL) | |
560 | zlog_debug("IFLA_VLAN_ID missing from VLAN IF message"); | |
561 | return -1; | |
562 | } | |
563 | ||
564 | vid_in_msg = *(vlanid_t *)RTA_DATA(attr[IFLA_VLAN_ID]); | |
565 | vlan_info->vid = vid_in_msg; | |
566 | return 0; | |
6675513d | 567 | } |
568 | ||
077c07cc PG |
569 | static int netlink_extract_gre_info(struct rtattr *link_data, |
570 | struct zebra_l2info_gre *gre_info) | |
571 | { | |
572 | struct rtattr *attr[IFLA_GRE_MAX + 1]; | |
573 | ||
574 | memset(gre_info, 0, sizeof(*gre_info)); | |
575 | memset(attr, 0, sizeof(attr)); | |
269b69d7 | 576 | netlink_parse_rtattr_nested(attr, IFLA_GRE_MAX, link_data); |
077c07cc PG |
577 | |
578 | if (!attr[IFLA_GRE_LOCAL]) { | |
579 | if (IS_ZEBRA_DEBUG_KERNEL) | |
580 | zlog_debug( | |
581 | "IFLA_GRE_LOCAL missing from GRE IF message"); | |
582 | } else | |
583 | gre_info->vtep_ip = | |
584 | *(struct in_addr *)RTA_DATA(attr[IFLA_GRE_LOCAL]); | |
585 | if (!attr[IFLA_GRE_REMOTE]) { | |
586 | if (IS_ZEBRA_DEBUG_KERNEL) | |
587 | zlog_debug( | |
588 | "IFLA_GRE_REMOTE missing from GRE IF message"); | |
589 | } else | |
590 | gre_info->vtep_ip_remote = | |
591 | *(struct in_addr *)RTA_DATA(attr[IFLA_GRE_REMOTE]); | |
592 | ||
593 | if (!attr[IFLA_GRE_LINK]) { | |
594 | if (IS_ZEBRA_DEBUG_KERNEL) | |
595 | zlog_debug("IFLA_GRE_LINK missing from GRE IF message"); | |
62b4b7e4 | 596 | } else { |
077c07cc PG |
597 | gre_info->ifindex_link = |
598 | *(ifindex_t *)RTA_DATA(attr[IFLA_GRE_LINK]); | |
62b4b7e4 PG |
599 | if (IS_ZEBRA_DEBUG_KERNEL) |
600 | zlog_debug("IFLA_GRE_LINK obtained is %u", | |
601 | gre_info->ifindex_link); | |
602 | } | |
077c07cc PG |
603 | if (attr[IFLA_GRE_IKEY]) |
604 | gre_info->ikey = *(uint32_t *)RTA_DATA(attr[IFLA_GRE_IKEY]); | |
605 | if (attr[IFLA_GRE_OKEY]) | |
606 | gre_info->okey = *(uint32_t *)RTA_DATA(attr[IFLA_GRE_OKEY]); | |
607 | return 0; | |
608 | } | |
609 | ||
d62a17ae | 610 | static int netlink_extract_vxlan_info(struct rtattr *link_data, |
611 | struct zebra_l2info_vxlan *vxl_info) | |
6675513d | 612 | { |
d62a17ae | 613 | struct rtattr *attr[IFLA_VXLAN_MAX + 1]; |
614 | vni_t vni_in_msg; | |
615 | struct in_addr vtep_ip_in_msg; | |
14ddb3d9 | 616 | ifindex_t ifindex_link; |
d62a17ae | 617 | |
618 | memset(vxl_info, 0, sizeof(*vxl_info)); | |
c9d842c7 | 619 | netlink_parse_rtattr_nested(attr, IFLA_VXLAN_MAX, link_data); |
d62a17ae | 620 | if (!attr[IFLA_VXLAN_ID]) { |
621 | if (IS_ZEBRA_DEBUG_KERNEL) | |
622 | zlog_debug( | |
623 | "IFLA_VXLAN_ID missing from VXLAN IF message"); | |
624 | return -1; | |
625 | } | |
626 | ||
627 | vni_in_msg = *(vni_t *)RTA_DATA(attr[IFLA_VXLAN_ID]); | |
628 | vxl_info->vni = vni_in_msg; | |
629 | if (!attr[IFLA_VXLAN_LOCAL]) { | |
630 | if (IS_ZEBRA_DEBUG_KERNEL) | |
631 | zlog_debug( | |
632 | "IFLA_VXLAN_LOCAL missing from VXLAN IF message"); | |
633 | } else { | |
634 | vtep_ip_in_msg = | |
635 | *(struct in_addr *)RTA_DATA(attr[IFLA_VXLAN_LOCAL]); | |
636 | vxl_info->vtep_ip = vtep_ip_in_msg; | |
637 | } | |
638 | ||
d7fe235c AK |
639 | if (attr[IFLA_VXLAN_GROUP]) { |
640 | vxl_info->mcast_grp = | |
641 | *(struct in_addr *)RTA_DATA(attr[IFLA_VXLAN_GROUP]); | |
642 | } | |
643 | ||
14ddb3d9 PG |
644 | if (!attr[IFLA_VXLAN_LINK]) { |
645 | if (IS_ZEBRA_DEBUG_KERNEL) | |
3efd0893 | 646 | zlog_debug("IFLA_VXLAN_LINK missing from VXLAN IF message"); |
14ddb3d9 PG |
647 | } else { |
648 | ifindex_link = | |
649 | *(ifindex_t *)RTA_DATA(attr[IFLA_VXLAN_LINK]); | |
650 | vxl_info->ifindex_link = ifindex_link; | |
651 | } | |
d62a17ae | 652 | return 0; |
6675513d | 653 | } |
654 | ||
655 | /* | |
656 | * Extract and save L2 params (of interest) for an interface. When a | |
657 | * bridge interface is added or updated, take further actions to map | |
658 | * its members. Likewise, for VxLAN interface. | |
659 | */ | |
d62a17ae | 660 | static void netlink_interface_update_l2info(struct interface *ifp, |
14ddb3d9 PG |
661 | struct rtattr *link_data, int add, |
662 | ns_id_t link_nsid) | |
6675513d | 663 | { |
d62a17ae | 664 | if (!link_data) |
665 | return; | |
666 | ||
667 | if (IS_ZEBRA_IF_BRIDGE(ifp)) { | |
668 | struct zebra_l2info_bridge bridge_info; | |
669 | ||
670 | netlink_extract_bridge_info(link_data, &bridge_info); | |
671 | zebra_l2_bridge_add_update(ifp, &bridge_info, add); | |
672 | } else if (IS_ZEBRA_IF_VLAN(ifp)) { | |
673 | struct zebra_l2info_vlan vlan_info; | |
674 | ||
675 | netlink_extract_vlan_info(link_data, &vlan_info); | |
676 | zebra_l2_vlanif_update(ifp, &vlan_info); | |
243b74ed AK |
677 | zebra_evpn_acc_bd_svi_set(ifp->info, NULL, |
678 | !!if_is_operative(ifp)); | |
d62a17ae | 679 | } else if (IS_ZEBRA_IF_VXLAN(ifp)) { |
680 | struct zebra_l2info_vxlan vxlan_info; | |
681 | ||
682 | netlink_extract_vxlan_info(link_data, &vxlan_info); | |
14ddb3d9 | 683 | vxlan_info.link_nsid = link_nsid; |
d62a17ae | 684 | zebra_l2_vxlanif_add_update(ifp, &vxlan_info, add); |
14ddb3d9 PG |
685 | if (link_nsid != NS_UNKNOWN && |
686 | vxlan_info.ifindex_link) | |
687 | zebra_if_update_link(ifp, vxlan_info.ifindex_link, | |
688 | link_nsid); | |
077c07cc PG |
689 | } else if (IS_ZEBRA_IF_GRE(ifp)) { |
690 | struct zebra_l2info_gre gre_info; | |
691 | ||
692 | netlink_extract_gre_info(link_data, &gre_info); | |
693 | gre_info.link_nsid = link_nsid; | |
694 | zebra_l2_greif_add_update(ifp, &gre_info, add); | |
695 | if (link_nsid != NS_UNKNOWN && | |
696 | gre_info.ifindex_link) | |
697 | zebra_if_update_link(ifp, gre_info.ifindex_link, | |
698 | link_nsid); | |
d62a17ae | 699 | } |
6675513d | 700 | } |
701 | ||
42b56639 AK |
702 | static int netlink_bridge_vxlan_update(struct interface *ifp, |
703 | struct rtattr *af_spec) | |
704 | { | |
705 | struct rtattr *aftb[IFLA_BRIDGE_MAX + 1]; | |
706 | struct bridge_vlan_info *vinfo; | |
707 | vlanid_t access_vlan; | |
708 | ||
ec897751 | 709 | if (!af_spec) |
710 | return 0; | |
711 | ||
42b56639 AK |
712 | /* There is a 1-to-1 mapping of VLAN to VxLAN - hence |
713 | * only 1 access VLAN is accepted. | |
714 | */ | |
c9d842c7 | 715 | netlink_parse_rtattr_nested(aftb, IFLA_BRIDGE_MAX, af_spec); |
42b56639 AK |
716 | if (!aftb[IFLA_BRIDGE_VLAN_INFO]) |
717 | return 0; | |
718 | ||
719 | vinfo = RTA_DATA(aftb[IFLA_BRIDGE_VLAN_INFO]); | |
720 | if (!(vinfo->flags & BRIDGE_VLAN_INFO_PVID)) | |
721 | return 0; | |
722 | ||
723 | access_vlan = (vlanid_t)vinfo->vid; | |
724 | if (IS_ZEBRA_DEBUG_KERNEL) | |
725 | zlog_debug("Access VLAN %u for VxLAN IF %s(%u)", access_vlan, | |
726 | ifp->name, ifp->ifindex); | |
727 | zebra_l2_vxlanif_update_access_vlan(ifp, access_vlan); | |
728 | return 0; | |
729 | } | |
730 | ||
731 | static void netlink_bridge_vlan_update(struct interface *ifp, | |
732 | struct rtattr *af_spec) | |
733 | { | |
734 | struct rtattr *i; | |
735 | int rem; | |
736 | uint16_t vid_range_start = 0; | |
737 | struct zebra_if *zif; | |
738 | bitfield_t old_vlan_bitmap; | |
739 | struct bridge_vlan_info *vinfo; | |
740 | ||
741 | zif = (struct zebra_if *)ifp->info; | |
742 | ||
743 | /* cache the old bitmap addrs */ | |
744 | old_vlan_bitmap = zif->vlan_bitmap; | |
745 | /* create a new bitmap space for re-eval */ | |
746 | bf_init(zif->vlan_bitmap, IF_VLAN_BITMAP_MAX); | |
747 | ||
ec897751 | 748 | if (af_spec) { |
749 | for (i = RTA_DATA(af_spec), rem = RTA_PAYLOAD(af_spec); | |
750 | RTA_OK(i, rem); i = RTA_NEXT(i, rem)) { | |
42b56639 | 751 | |
ec897751 | 752 | if (i->rta_type != IFLA_BRIDGE_VLAN_INFO) |
753 | continue; | |
42b56639 | 754 | |
ec897751 | 755 | vinfo = RTA_DATA(i); |
42b56639 | 756 | |
ec897751 | 757 | if (vinfo->flags & BRIDGE_VLAN_INFO_RANGE_BEGIN) { |
758 | vid_range_start = vinfo->vid; | |
759 | continue; | |
760 | } | |
42b56639 | 761 | |
ec897751 | 762 | if (!(vinfo->flags & BRIDGE_VLAN_INFO_RANGE_END)) |
763 | vid_range_start = vinfo->vid; | |
42b56639 | 764 | |
ec897751 | 765 | zebra_vlan_bitmap_compute(ifp, vid_range_start, |
766 | vinfo->vid); | |
767 | } | |
42b56639 AK |
768 | } |
769 | ||
770 | zebra_vlan_mbr_re_eval(ifp, old_vlan_bitmap); | |
771 | ||
772 | bf_free(old_vlan_bitmap); | |
773 | } | |
774 | ||
d62a17ae | 775 | static int netlink_bridge_interface(struct nlmsghdr *h, int len, ns_id_t ns_id, |
776 | int startup) | |
6675513d | 777 | { |
d62a17ae | 778 | char *name = NULL; |
779 | struct ifinfomsg *ifi; | |
780 | struct rtattr *tb[IFLA_MAX + 1]; | |
781 | struct interface *ifp; | |
42b56639 AK |
782 | struct zebra_if *zif; |
783 | struct rtattr *af_spec; | |
d62a17ae | 784 | |
785 | /* Fetch name and ifindex */ | |
786 | ifi = NLMSG_DATA(h); | |
d62a17ae | 787 | netlink_parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), len); |
788 | ||
789 | if (tb[IFLA_IFNAME] == NULL) | |
790 | return -1; | |
791 | name = (char *)RTA_DATA(tb[IFLA_IFNAME]); | |
792 | ||
793 | /* The interface should already be known, if not discard. */ | |
794 | ifp = if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id), ifi->ifi_index); | |
795 | if (!ifp) { | |
9df414fe QY |
796 | zlog_debug("Cannot find bridge IF %s(%u)", name, |
797 | ifi->ifi_index); | |
d62a17ae | 798 | return 0; |
799 | } | |
d62a17ae | 800 | |
801 | /* We are only interested in the access VLAN i.e., AF_SPEC */ | |
42b56639 | 802 | af_spec = tb[IFLA_AF_SPEC]; |
d62a17ae | 803 | |
42b56639 AK |
804 | if (IS_ZEBRA_IF_VXLAN(ifp)) |
805 | return netlink_bridge_vxlan_update(ifp, af_spec); | |
d62a17ae | 806 | |
42b56639 AK |
807 | /* build vlan bitmap associated with this interface if that |
808 | * device type is interested in the vlans | |
809 | */ | |
810 | zif = (struct zebra_if *)ifp->info; | |
811 | if (bf_is_inited(zif->vlan_bitmap)) | |
812 | netlink_bridge_vlan_update(ifp, af_spec); | |
d62a17ae | 813 | |
d62a17ae | 814 | return 0; |
6675513d | 815 | } |
816 | ||
2bcf92e1 | 817 | /* If the interface is an es bond member then it must follow EVPN's |
c36e442c AK |
818 | * protodown setting |
819 | */ | |
820 | static void netlink_proc_dplane_if_protodown(struct zebra_if *zif, | |
821 | bool protodown) | |
822 | { | |
823 | bool zif_protodown; | |
824 | ||
825 | zif_protodown = !!(zif->flags & ZIF_FLAG_PROTODOWN); | |
826 | if (protodown == zif_protodown) | |
827 | return; | |
828 | ||
829 | if (IS_ZEBRA_DEBUG_EVPN_MH_ES || IS_ZEBRA_DEBUG_KERNEL) | |
830 | zlog_debug("interface %s dplane change, protdown %s", | |
831 | zif->ifp->name, protodown ? "on" : "off"); | |
832 | ||
833 | if (zebra_evpn_is_es_bond_member(zif->ifp)) { | |
834 | if (IS_ZEBRA_DEBUG_EVPN_MH_ES || IS_ZEBRA_DEBUG_KERNEL) | |
835 | zlog_debug( | |
836 | "bond mbr %s re-instate protdown %s in the dplane", | |
837 | zif->ifp->name, zif_protodown ? "on" : "off"); | |
838 | netlink_protodown(zif->ifp, zif_protodown); | |
839 | } else { | |
840 | if (protodown) | |
841 | zif->flags |= ZIF_FLAG_PROTODOWN; | |
842 | else | |
843 | zif->flags &= ~ZIF_FLAG_PROTODOWN; | |
844 | } | |
845 | } | |
846 | ||
00a7710c AK |
847 | static uint8_t netlink_parse_lacp_bypass(struct rtattr **linkinfo) |
848 | { | |
849 | uint8_t bypass = 0; | |
850 | struct rtattr *mbrinfo[IFLA_BOND_SLAVE_MAX + 1]; | |
851 | ||
c9d842c7 DS |
852 | netlink_parse_rtattr_nested(mbrinfo, IFLA_BOND_SLAVE_MAX, |
853 | linkinfo[IFLA_INFO_SLAVE_DATA]); | |
00a7710c AK |
854 | if (mbrinfo[IFLA_BOND_SLAVE_AD_RX_BYPASS]) |
855 | bypass = *(uint8_t *)RTA_DATA( | |
856 | mbrinfo[IFLA_BOND_SLAVE_AD_RX_BYPASS]); | |
857 | ||
858 | return bypass; | |
859 | } | |
860 | ||
2414abd3 DS |
861 | /* |
862 | * Called from interface_lookup_netlink(). This function is only used | |
863 | * during bootstrap. | |
864 | */ | |
865 | static int netlink_interface(struct nlmsghdr *h, ns_id_t ns_id, int startup) | |
1fdc9eae | 866 | { |
d62a17ae | 867 | int len; |
868 | struct ifinfomsg *ifi; | |
869 | struct rtattr *tb[IFLA_MAX + 1]; | |
870 | struct rtattr *linkinfo[IFLA_MAX + 1]; | |
871 | struct interface *ifp; | |
872 | char *name = NULL; | |
873 | char *kind = NULL; | |
48884c6b | 874 | char *desc = NULL; |
d62a17ae | 875 | char *slave_kind = NULL; |
ea7ec261 | 876 | struct zebra_ns *zns = NULL; |
d62a17ae | 877 | vrf_id_t vrf_id = VRF_DEFAULT; |
e6f2bec0 | 878 | enum zebra_iftype zif_type = ZEBRA_IF_OTHER; |
a81982fa | 879 | enum zebra_slave_iftype zif_slave_type = ZEBRA_IF_SLAVE_NONE; |
d62a17ae | 880 | ifindex_t bridge_ifindex = IFINDEX_INTERNAL; |
881 | ifindex_t link_ifindex = IFINDEX_INTERNAL; | |
b9368db9 | 882 | ifindex_t bond_ifindex = IFINDEX_INTERNAL; |
520ebf72 | 883 | struct zebra_if *zif; |
14ddb3d9 | 884 | ns_id_t link_nsid = ns_id; |
00a7710c | 885 | uint8_t bypass = 0; |
d62a17ae | 886 | |
887 | zns = zebra_ns_lookup(ns_id); | |
888 | ifi = NLMSG_DATA(h); | |
889 | ||
890 | if (h->nlmsg_type != RTM_NEWLINK) | |
891 | return 0; | |
892 | ||
893 | len = h->nlmsg_len - NLMSG_LENGTH(sizeof(struct ifinfomsg)); | |
9bdf8618 | 894 | if (len < 0) { |
15569c58 DA |
895 | zlog_err( |
896 | "%s: Message received from netlink is of a broken size: %d %zu", | |
897 | __func__, h->nlmsg_len, | |
898 | (size_t)NLMSG_LENGTH(sizeof(struct ifinfomsg))); | |
d62a17ae | 899 | return -1; |
9bdf8618 | 900 | } |
d62a17ae | 901 | |
902 | /* We are interested in some AF_BRIDGE notifications. */ | |
903 | if (ifi->ifi_family == AF_BRIDGE) | |
904 | return netlink_bridge_interface(h, len, ns_id, startup); | |
905 | ||
906 | /* Looking up interface name. */ | |
0d6f7fd6 | 907 | memset(linkinfo, 0, sizeof(linkinfo)); |
d62a17ae | 908 | netlink_parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), len); |
1fdc9eae | 909 | |
d62a17ae | 910 | /* check for wireless messages to ignore */ |
911 | if ((tb[IFLA_WIRELESS] != NULL) && (ifi->ifi_change == 0)) { | |
912 | if (IS_ZEBRA_DEBUG_KERNEL) | |
913 | zlog_debug("%s: ignoring IFLA_WIRELESS message", | |
914 | __func__); | |
915 | return 0; | |
916 | } | |
1fdc9eae | 917 | |
d62a17ae | 918 | if (tb[IFLA_IFNAME] == NULL) |
919 | return -1; | |
920 | name = (char *)RTA_DATA(tb[IFLA_IFNAME]); | |
1fdc9eae | 921 | |
48884c6b DS |
922 | if (tb[IFLA_IFALIAS]) |
923 | desc = (char *)RTA_DATA(tb[IFLA_IFALIAS]); | |
924 | ||
d62a17ae | 925 | if (tb[IFLA_LINKINFO]) { |
c9d842c7 DS |
926 | netlink_parse_rtattr_nested(linkinfo, IFLA_INFO_MAX, |
927 | tb[IFLA_LINKINFO]); | |
1fdc9eae | 928 | |
d62a17ae | 929 | if (linkinfo[IFLA_INFO_KIND]) |
930 | kind = RTA_DATA(linkinfo[IFLA_INFO_KIND]); | |
1fdc9eae | 931 | |
d62a17ae | 932 | if (linkinfo[IFLA_INFO_SLAVE_KIND]) |
933 | slave_kind = RTA_DATA(linkinfo[IFLA_INFO_SLAVE_KIND]); | |
1fdc9eae | 934 | |
b9368db9 DD |
935 | if ((slave_kind != NULL) && strcmp(slave_kind, "bond") == 0) |
936 | netlink_determine_zebra_iftype("bond_slave", &zif_type); | |
937 | else | |
938 | netlink_determine_zebra_iftype(kind, &zif_type); | |
d62a17ae | 939 | } |
940 | ||
941 | /* If VRF, create the VRF structure itself. */ | |
78dd30b2 | 942 | if (zif_type == ZEBRA_IF_VRF && !vrf_is_backend_netns()) { |
5e031198 | 943 | netlink_vrf_change(h, tb[IFLA_LINKINFO], ns_id, name); |
d62a17ae | 944 | vrf_id = (vrf_id_t)ifi->ifi_index; |
945 | } | |
946 | ||
947 | if (tb[IFLA_MASTER]) { | |
78dd30b2 PG |
948 | if (slave_kind && (strcmp(slave_kind, "vrf") == 0) |
949 | && !vrf_is_backend_netns()) { | |
d62a17ae | 950 | zif_slave_type = ZEBRA_IF_SLAVE_VRF; |
d7c0a89a | 951 | vrf_id = *(uint32_t *)RTA_DATA(tb[IFLA_MASTER]); |
d62a17ae | 952 | } else if (slave_kind && (strcmp(slave_kind, "bridge") == 0)) { |
953 | zif_slave_type = ZEBRA_IF_SLAVE_BRIDGE; | |
954 | bridge_ifindex = | |
955 | *(ifindex_t *)RTA_DATA(tb[IFLA_MASTER]); | |
b9368db9 DD |
956 | } else if (slave_kind && (strcmp(slave_kind, "bond") == 0)) { |
957 | zif_slave_type = ZEBRA_IF_SLAVE_BOND; | |
958 | bond_ifindex = *(ifindex_t *)RTA_DATA(tb[IFLA_MASTER]); | |
00a7710c | 959 | bypass = netlink_parse_lacp_bypass(linkinfo); |
d62a17ae | 960 | } else |
961 | zif_slave_type = ZEBRA_IF_SLAVE_OTHER; | |
962 | } | |
78dd30b2 PG |
963 | if (vrf_is_backend_netns()) |
964 | vrf_id = (vrf_id_t)ns_id; | |
a36898e7 | 965 | |
d62a17ae | 966 | /* If linking to another interface, note it. */ |
967 | if (tb[IFLA_LINK]) | |
968 | link_ifindex = *(ifindex_t *)RTA_DATA(tb[IFLA_LINK]); | |
969 | ||
b1cc23b2 | 970 | if (tb[IFLA_LINK_NETNSID]) { |
14ddb3d9 | 971 | link_nsid = *(ns_id_t *)RTA_DATA(tb[IFLA_LINK_NETNSID]); |
b1cc23b2 PG |
972 | link_nsid = ns_id_get_absolute(ns_id, link_nsid); |
973 | } | |
14ddb3d9 | 974 | |
ea7ec261 DD |
975 | /* Add interface. |
976 | * We add by index first because in some cases such as the master | |
977 | * interface, we have the index before we have the name. Fixing | |
978 | * back references on the slave interfaces is painful if not done | |
979 | * this way, i.e. by creating by ifindex. | |
980 | */ | |
bd23c840 | 981 | ifp = if_get_by_ifindex(ifi->ifi_index, vrf_id); |
ea7ec261 | 982 | set_ifindex(ifp, ifi->ifi_index, zns); /* add it to ns struct */ |
d5c65bf1 | 983 | |
bd23c840 PR |
984 | if_set_name(ifp, name); |
985 | ||
d62a17ae | 986 | ifp->flags = ifi->ifi_flags & 0x0000fffff; |
d62a17ae | 987 | ifp->mtu6 = ifp->mtu = *(uint32_t *)RTA_DATA(tb[IFLA_MTU]); |
988 | ifp->metric = 0; | |
594c2878 | 989 | ifp->speed = get_iflink_speed(ifp, NULL); |
d62a17ae | 990 | ifp->ptm_status = ZEBRA_PTM_STATUS_UNKNOWN; |
991 | ||
992 | /* Set zebra interface type */ | |
993 | zebra_if_set_ziftype(ifp, zif_type, zif_slave_type); | |
b0fa6f6a CS |
994 | if (IS_ZEBRA_IF_VRF(ifp)) |
995 | SET_FLAG(ifp->status, ZEBRA_INTERFACE_VRF_LOOPBACK); | |
d62a17ae | 996 | |
98efddf1 DS |
997 | /* |
998 | * Just set the @link/lower-device ifindex. During nldump interfaces are | |
520ebf72 AK |
999 | * not ordered in any fashion so we may end up getting upper devices |
1000 | * before lower devices. We will setup the real linkage once the dump | |
98efddf1 DS |
1001 | * is complete. |
1002 | */ | |
520ebf72 AK |
1003 | zif = (struct zebra_if *)ifp->info; |
1004 | zif->link_ifindex = link_ifindex; | |
d62a17ae | 1005 | |
ba5165ec DS |
1006 | if (desc) { |
1007 | XFREE(MTYPE_TMP, zif->desc); | |
1008 | zif->desc = XSTRDUP(MTYPE_TMP, desc); | |
1009 | } | |
1010 | ||
d62a17ae | 1011 | /* Hardware type and address. */ |
1012 | ifp->ll_type = netlink_to_zebra_link_type(ifi->ifi_type); | |
1013 | netlink_interface_update_hw_addr(tb, ifp); | |
1014 | ||
1015 | if_add_update(ifp); | |
1016 | ||
1017 | /* Extract and save L2 interface information, take additional actions. | |
1018 | */ | |
14ddb3d9 PG |
1019 | netlink_interface_update_l2info(ifp, linkinfo[IFLA_INFO_DATA], |
1020 | 1, link_nsid); | |
c36e442c AK |
1021 | if (IS_ZEBRA_IF_BOND(ifp)) |
1022 | zebra_l2if_update_bond(ifp, true); | |
d62a17ae | 1023 | if (IS_ZEBRA_IF_BRIDGE_SLAVE(ifp)) |
c7620108 PG |
1024 | zebra_l2if_update_bridge_slave(ifp, bridge_ifindex, ns_id, |
1025 | ZEBRA_BRIDGE_NO_ACTION); | |
b9368db9 | 1026 | else if (IS_ZEBRA_IF_BOND_SLAVE(ifp)) |
00a7710c | 1027 | zebra_l2if_update_bond_slave(ifp, bond_ifindex, !!bypass); |
d62a17ae | 1028 | |
c36e442c AK |
1029 | if (tb[IFLA_PROTO_DOWN]) { |
1030 | uint8_t protodown; | |
1031 | ||
1032 | protodown = *(uint8_t *)RTA_DATA(tb[IFLA_PROTO_DOWN]); | |
1033 | netlink_proc_dplane_if_protodown(zif, !!protodown); | |
1034 | } | |
1035 | ||
d62a17ae | 1036 | return 0; |
1fdc9eae | 1037 | } |
1038 | ||
289602d7 | 1039 | /* Request for specific interface or address information from the kernel */ |
85a75f1e MS |
1040 | static int netlink_request_intf_addr(struct nlsock *netlink_cmd, int family, |
1041 | int type, uint32_t filter_mask) | |
289602d7 | 1042 | { |
d62a17ae | 1043 | struct { |
1044 | struct nlmsghdr n; | |
1045 | struct ifinfomsg ifm; | |
1046 | char buf[256]; | |
1047 | } req; | |
1048 | ||
1049 | /* Form the request, specifying filter (rtattr) if needed. */ | |
1050 | memset(&req, 0, sizeof(req)); | |
1051 | req.n.nlmsg_type = type; | |
718f9b0f | 1052 | req.n.nlmsg_flags = NLM_F_ROOT | NLM_F_MATCH | NLM_F_REQUEST; |
d62a17ae | 1053 | req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg)); |
1054 | req.ifm.ifi_family = family; | |
1055 | ||
1056 | /* Include filter, if specified. */ | |
1057 | if (filter_mask) | |
312a6bee | 1058 | nl_attr_put32(&req.n, sizeof(req), IFLA_EXT_MASK, filter_mask); |
d62a17ae | 1059 | |
fd3f8e52 | 1060 | return netlink_request(netlink_cmd, &req); |
289602d7 | 1061 | } |
1062 | ||
62b4b7e4 PG |
1063 | enum netlink_msg_status |
1064 | netlink_put_gre_set_msg(struct nl_batch *bth, struct zebra_dplane_ctx *ctx) | |
1065 | { | |
1066 | enum dplane_op_e op; | |
1067 | enum netlink_msg_status ret; | |
1068 | ||
1069 | op = dplane_ctx_get_op(ctx); | |
1070 | assert(op == DPLANE_OP_GRE_SET); | |
1071 | ||
1072 | ret = netlink_batch_add_msg(bth, ctx, netlink_gre_set_msg_encoder, false); | |
1073 | ||
1074 | return ret; | |
1075 | } | |
1076 | ||
1fdc9eae | 1077 | /* Interface lookup by netlink socket. */ |
d62a17ae | 1078 | int interface_lookup_netlink(struct zebra_ns *zns) |
1fdc9eae | 1079 | { |
d62a17ae | 1080 | int ret; |
85a75f1e MS |
1081 | struct zebra_dplane_info dp_info; |
1082 | struct nlsock *netlink_cmd = &zns->netlink_cmd; | |
1083 | ||
1084 | /* Capture key info from ns struct */ | |
1085 | zebra_dplane_info_from_zns(&dp_info, zns, true /*is_cmd*/); | |
d62a17ae | 1086 | |
1087 | /* Get interface information. */ | |
85a75f1e | 1088 | ret = netlink_request_intf_addr(netlink_cmd, AF_PACKET, RTM_GETLINK, 0); |
d62a17ae | 1089 | if (ret < 0) |
1090 | return ret; | |
85a75f1e | 1091 | ret = netlink_parse_info(netlink_interface, netlink_cmd, &dp_info, 0, |
d62a17ae | 1092 | 1); |
1093 | if (ret < 0) | |
1094 | return ret; | |
1095 | ||
1096 | /* Get interface information - for bridge interfaces. */ | |
85a75f1e | 1097 | ret = netlink_request_intf_addr(netlink_cmd, AF_BRIDGE, RTM_GETLINK, |
d62a17ae | 1098 | RTEXT_FILTER_BRVLAN); |
1099 | if (ret < 0) | |
1100 | return ret; | |
85a75f1e | 1101 | ret = netlink_parse_info(netlink_interface, netlink_cmd, &dp_info, 0, |
0cf0069d | 1102 | 1); |
d62a17ae | 1103 | if (ret < 0) |
1104 | return ret; | |
1105 | ||
520ebf72 | 1106 | /* fixup linkages */ |
357b150d | 1107 | zebra_if_update_all_links(zns); |
d2bec88a SW |
1108 | return 0; |
1109 | } | |
1110 | ||
1111 | /** | |
1112 | * interface_addr_lookup_netlink() - Look up interface addresses | |
1113 | * | |
1114 | * @zns: Zebra netlink socket | |
1115 | * Return: Result status | |
1116 | */ | |
1117 | static int interface_addr_lookup_netlink(struct zebra_ns *zns) | |
1118 | { | |
1119 | int ret; | |
1120 | struct zebra_dplane_info dp_info; | |
1121 | struct nlsock *netlink_cmd = &zns->netlink_cmd; | |
1122 | ||
1123 | /* Capture key info from ns struct */ | |
1124 | zebra_dplane_info_from_zns(&dp_info, zns, true /*is_cmd*/); | |
520ebf72 | 1125 | |
d62a17ae | 1126 | /* Get IPv4 address of the interfaces. */ |
85a75f1e | 1127 | ret = netlink_request_intf_addr(netlink_cmd, AF_INET, RTM_GETADDR, 0); |
d62a17ae | 1128 | if (ret < 0) |
1129 | return ret; | |
85a75f1e | 1130 | ret = netlink_parse_info(netlink_interface_addr, netlink_cmd, &dp_info, |
d62a17ae | 1131 | 0, 1); |
1132 | if (ret < 0) | |
1133 | return ret; | |
1134 | ||
1135 | /* Get IPv6 address of the interfaces. */ | |
85a75f1e | 1136 | ret = netlink_request_intf_addr(netlink_cmd, AF_INET6, RTM_GETADDR, 0); |
d62a17ae | 1137 | if (ret < 0) |
1138 | return ret; | |
85a75f1e | 1139 | ret = netlink_parse_info(netlink_interface_addr, netlink_cmd, &dp_info, |
d62a17ae | 1140 | 0, 1); |
1141 | if (ret < 0) | |
1142 | return ret; | |
1143 | ||
1144 | return 0; | |
1fdc9eae | 1145 | } |
1146 | ||
e0ae31b8 DS |
1147 | int kernel_interface_set_master(struct interface *master, |
1148 | struct interface *slave) | |
1149 | { | |
1150 | struct zebra_ns *zns = zebra_ns_lookup(NS_DEFAULT); | |
1151 | ||
1152 | struct { | |
1153 | struct nlmsghdr n; | |
1154 | struct ifinfomsg ifa; | |
1155 | char buf[NL_PKT_BUF_SIZE]; | |
1156 | } req; | |
1157 | ||
0d6f7fd6 | 1158 | memset(&req, 0, sizeof(req)); |
e0ae31b8 DS |
1159 | |
1160 | req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg)); | |
1161 | req.n.nlmsg_flags = NLM_F_REQUEST; | |
1162 | req.n.nlmsg_type = RTM_SETLINK; | |
1163 | req.n.nlmsg_pid = zns->netlink_cmd.snl.nl_pid; | |
1164 | ||
1165 | req.ifa.ifi_index = slave->ifindex; | |
1166 | ||
a757997c JU |
1167 | nl_attr_put32(&req.n, sizeof(req), IFLA_MASTER, master->ifindex); |
1168 | nl_attr_put32(&req.n, sizeof(req), IFLA_LINK, slave->ifindex); | |
e0ae31b8 DS |
1169 | |
1170 | return netlink_talk(netlink_talk_filter, &req.n, &zns->netlink_cmd, zns, | |
1171 | 0); | |
1172 | } | |
1173 | ||
1fdc9eae | 1174 | /* Interface address modification. */ |
67e3369e JU |
1175 | static ssize_t netlink_address_msg_encoder(struct zebra_dplane_ctx *ctx, |
1176 | void *buf, size_t buflen) | |
1fdc9eae | 1177 | { |
d62a17ae | 1178 | int bytelen; |
64168803 MS |
1179 | const struct prefix *p; |
1180 | int cmd; | |
1181 | const char *label; | |
1fdc9eae | 1182 | |
d62a17ae | 1183 | struct { |
1184 | struct nlmsghdr n; | |
1185 | struct ifaddrmsg ifa; | |
67e3369e JU |
1186 | char buf[0]; |
1187 | } *req = buf; | |
1188 | ||
1189 | if (buflen < sizeof(*req)) | |
1190 | return 0; | |
1fdc9eae | 1191 | |
64168803 | 1192 | p = dplane_ctx_get_intf_addr(ctx); |
67e3369e | 1193 | memset(req, 0, sizeof(*req)); |
1fdc9eae | 1194 | |
64168803 | 1195 | bytelen = (p->family == AF_INET ? 4 : 16); |
1fdc9eae | 1196 | |
67e3369e JU |
1197 | req->n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg)); |
1198 | req->n.nlmsg_flags = NLM_F_REQUEST; | |
a55ba23f | 1199 | |
64168803 MS |
1200 | if (dplane_ctx_get_op(ctx) == DPLANE_OP_ADDR_INSTALL) |
1201 | cmd = RTM_NEWADDR; | |
1202 | else | |
1203 | cmd = RTM_DELADDR; | |
1204 | ||
67e3369e JU |
1205 | req->n.nlmsg_type = cmd; |
1206 | req->ifa.ifa_family = p->family; | |
1fdc9eae | 1207 | |
67e3369e | 1208 | req->ifa.ifa_index = dplane_ctx_get_ifindex(ctx); |
1fdc9eae | 1209 | |
67e3369e JU |
1210 | if (!nl_attr_put(&req->n, buflen, IFA_LOCAL, &p->u.prefix, bytelen)) |
1211 | return 0; | |
1fdc9eae | 1212 | |
64168803 MS |
1213 | if (p->family == AF_INET) { |
1214 | if (dplane_ctx_intf_is_connected(ctx)) { | |
1215 | p = dplane_ctx_get_intf_dest(ctx); | |
67e3369e JU |
1216 | if (!nl_attr_put(&req->n, buflen, IFA_ADDRESS, |
1217 | &p->u.prefix, bytelen)) | |
1218 | return 0; | |
0f3af738 JW |
1219 | } else if (cmd == RTM_NEWADDR) { |
1220 | struct in_addr broad = { | |
1221 | .s_addr = ipv4_broadcast_addr(p->u.prefix4.s_addr, | |
1222 | p->prefixlen) | |
1223 | }; | |
67e3369e JU |
1224 | if (!nl_attr_put(&req->n, buflen, IFA_BROADCAST, &broad, |
1225 | bytelen)) | |
1226 | return 0; | |
d62a17ae | 1227 | } |
1228 | } | |
1fdc9eae | 1229 | |
64168803 | 1230 | /* p is now either address or destination/bcast addr */ |
67e3369e | 1231 | req->ifa.ifa_prefixlen = p->prefixlen; |
e8d19a05 | 1232 | |
64168803 | 1233 | if (dplane_ctx_intf_is_secondary(ctx)) |
67e3369e | 1234 | SET_FLAG(req->ifa.ifa_flags, IFA_F_SECONDARY); |
1fdc9eae | 1235 | |
64168803 MS |
1236 | if (dplane_ctx_intf_has_label(ctx)) { |
1237 | label = dplane_ctx_get_intf_label(ctx); | |
67e3369e JU |
1238 | if (!nl_attr_put(&req->n, buflen, IFA_LABEL, label, |
1239 | strlen(label) + 1)) | |
1240 | return 0; | |
64168803 | 1241 | } |
1fdc9eae | 1242 | |
67e3369e | 1243 | return NLMSG_ALIGN(req->n.nlmsg_len); |
e86b71f1 PG |
1244 | } |
1245 | ||
67e3369e JU |
1246 | enum netlink_msg_status |
1247 | netlink_put_address_update_msg(struct nl_batch *bth, | |
1248 | struct zebra_dplane_ctx *ctx) | |
1249 | { | |
1250 | return netlink_batch_add_msg(bth, ctx, netlink_address_msg_encoder, | |
1251 | false); | |
e86b71f1 PG |
1252 | } |
1253 | ||
2414abd3 | 1254 | int netlink_interface_addr(struct nlmsghdr *h, ns_id_t ns_id, int startup) |
1fdc9eae | 1255 | { |
d62a17ae | 1256 | int len; |
1257 | struct ifaddrmsg *ifa; | |
1258 | struct rtattr *tb[IFA_MAX + 1]; | |
1259 | struct interface *ifp; | |
1260 | void *addr; | |
1261 | void *broad; | |
d7c0a89a | 1262 | uint8_t flags = 0; |
d62a17ae | 1263 | char *label = NULL; |
1264 | struct zebra_ns *zns; | |
cde1af84 | 1265 | uint32_t metric = METRIC_MAX; |
9254efed | 1266 | uint32_t kernel_flags = 0; |
d62a17ae | 1267 | |
1268 | zns = zebra_ns_lookup(ns_id); | |
1269 | ifa = NLMSG_DATA(h); | |
1270 | ||
8a1b681c | 1271 | if (ifa->ifa_family != AF_INET && ifa->ifa_family != AF_INET6) { |
9df414fe | 1272 | flog_warn( |
e914ccbe | 1273 | EC_ZEBRA_UNKNOWN_FAMILY, |
87b5d1b0 DS |
1274 | "Invalid address family: %u received from kernel interface addr change: %s", |
1275 | ifa->ifa_family, nl_msg_type_to_str(h->nlmsg_type)); | |
d62a17ae | 1276 | return 0; |
8a1b681c | 1277 | } |
d62a17ae | 1278 | |
1279 | if (h->nlmsg_type != RTM_NEWADDR && h->nlmsg_type != RTM_DELADDR) | |
1280 | return 0; | |
1281 | ||
1282 | len = h->nlmsg_len - NLMSG_LENGTH(sizeof(struct ifaddrmsg)); | |
9bdf8618 | 1283 | if (len < 0) { |
15569c58 DA |
1284 | zlog_err( |
1285 | "%s: Message received from netlink is of a broken size: %d %zu", | |
1286 | __func__, h->nlmsg_len, | |
1287 | (size_t)NLMSG_LENGTH(sizeof(struct ifaddrmsg))); | |
d62a17ae | 1288 | return -1; |
9bdf8618 | 1289 | } |
d62a17ae | 1290 | |
d62a17ae | 1291 | netlink_parse_rtattr(tb, IFA_MAX, IFA_RTA(ifa), len); |
1292 | ||
1293 | ifp = if_lookup_by_index_per_ns(zns, ifa->ifa_index); | |
1294 | if (ifp == NULL) { | |
af4c2728 | 1295 | flog_err( |
450971aa | 1296 | EC_LIB_INTERFACE, |
d62a17ae | 1297 | "netlink_interface_addr can't find interface by index %d", |
1298 | ifa->ifa_index); | |
1299 | return -1; | |
1300 | } | |
1301 | ||
9254efed DS |
1302 | /* Flags passed through */ |
1303 | if (tb[IFA_FLAGS]) | |
1304 | kernel_flags = *(int *)RTA_DATA(tb[IFA_FLAGS]); | |
1305 | else | |
1306 | kernel_flags = ifa->ifa_flags; | |
1307 | ||
d62a17ae | 1308 | if (IS_ZEBRA_DEBUG_KERNEL) /* remove this line to see initial ifcfg */ |
1309 | { | |
1310 | char buf[BUFSIZ]; | |
1311 | zlog_debug("netlink_interface_addr %s %s flags 0x%x:", | |
1312 | nl_msg_type_to_str(h->nlmsg_type), ifp->name, | |
9254efed | 1313 | kernel_flags); |
d62a17ae | 1314 | if (tb[IFA_LOCAL]) |
1315 | zlog_debug(" IFA_LOCAL %s/%d", | |
1316 | inet_ntop(ifa->ifa_family, | |
1317 | RTA_DATA(tb[IFA_LOCAL]), buf, | |
1318 | BUFSIZ), | |
1319 | ifa->ifa_prefixlen); | |
1320 | if (tb[IFA_ADDRESS]) | |
1321 | zlog_debug(" IFA_ADDRESS %s/%d", | |
1322 | inet_ntop(ifa->ifa_family, | |
1323 | RTA_DATA(tb[IFA_ADDRESS]), buf, | |
1324 | BUFSIZ), | |
1325 | ifa->ifa_prefixlen); | |
1326 | if (tb[IFA_BROADCAST]) | |
1327 | zlog_debug(" IFA_BROADCAST %s/%d", | |
1328 | inet_ntop(ifa->ifa_family, | |
1329 | RTA_DATA(tb[IFA_BROADCAST]), buf, | |
1330 | BUFSIZ), | |
1331 | ifa->ifa_prefixlen); | |
1332 | if (tb[IFA_LABEL] && strcmp(ifp->name, RTA_DATA(tb[IFA_LABEL]))) | |
1333 | zlog_debug(" IFA_LABEL %s", | |
1334 | (char *)RTA_DATA(tb[IFA_LABEL])); | |
1335 | ||
1336 | if (tb[IFA_CACHEINFO]) { | |
1337 | struct ifa_cacheinfo *ci = RTA_DATA(tb[IFA_CACHEINFO]); | |
1338 | zlog_debug(" IFA_CACHEINFO pref %d, valid %d", | |
1339 | ci->ifa_prefered, ci->ifa_valid); | |
1340 | } | |
1341 | } | |
1342 | ||
1343 | /* logic copied from iproute2/ip/ipaddress.c:print_addrinfo() */ | |
1344 | if (tb[IFA_LOCAL] == NULL) | |
1345 | tb[IFA_LOCAL] = tb[IFA_ADDRESS]; | |
1346 | if (tb[IFA_ADDRESS] == NULL) | |
1347 | tb[IFA_ADDRESS] = tb[IFA_LOCAL]; | |
1348 | ||
1349 | /* local interface address */ | |
1350 | addr = (tb[IFA_LOCAL] ? RTA_DATA(tb[IFA_LOCAL]) : NULL); | |
1351 | ||
1352 | /* is there a peer address? */ | |
1353 | if (tb[IFA_ADDRESS] | |
1354 | && memcmp(RTA_DATA(tb[IFA_ADDRESS]), RTA_DATA(tb[IFA_LOCAL]), | |
1355 | RTA_PAYLOAD(tb[IFA_ADDRESS]))) { | |
1356 | broad = RTA_DATA(tb[IFA_ADDRESS]); | |
1357 | SET_FLAG(flags, ZEBRA_IFA_PEER); | |
1358 | } else | |
1359 | /* seeking a broadcast address */ | |
1360 | broad = (tb[IFA_BROADCAST] ? RTA_DATA(tb[IFA_BROADCAST]) | |
1361 | : NULL); | |
1362 | ||
1363 | /* addr is primary key, SOL if we don't have one */ | |
1364 | if (addr == NULL) { | |
14a4d9d0 | 1365 | zlog_debug("%s: Local Interface Address is NULL for %s", |
1366 | __func__, ifp->name); | |
d62a17ae | 1367 | return -1; |
1368 | } | |
1369 | ||
1370 | /* Flags. */ | |
9254efed | 1371 | if (kernel_flags & IFA_F_SECONDARY) |
d62a17ae | 1372 | SET_FLAG(flags, ZEBRA_IFA_SECONDARY); |
1373 | ||
1374 | /* Label */ | |
1375 | if (tb[IFA_LABEL]) | |
1376 | label = (char *)RTA_DATA(tb[IFA_LABEL]); | |
1377 | ||
2e1cc436 | 1378 | if (label && strcmp(ifp->name, label) == 0) |
d62a17ae | 1379 | label = NULL; |
1380 | ||
cde1af84 AK |
1381 | if (tb[IFA_RT_PRIORITY]) |
1382 | metric = *(uint32_t *)RTA_DATA(tb[IFA_RT_PRIORITY]); | |
1383 | ||
d62a17ae | 1384 | /* Register interface address to the interface. */ |
1385 | if (ifa->ifa_family == AF_INET) { | |
930571d2 | 1386 | if (ifa->ifa_prefixlen > IPV4_MAX_BITLEN) { |
e17d9b2d | 1387 | zlog_err( |
87b5d1b0 DS |
1388 | "Invalid prefix length: %u received from kernel interface addr change: %s", |
1389 | ifa->ifa_prefixlen, | |
1390 | nl_msg_type_to_str(h->nlmsg_type)); | |
e17d9b2d | 1391 | return -1; |
930571d2 | 1392 | } |
20e879f9 | 1393 | |
d62a17ae | 1394 | if (h->nlmsg_type == RTM_NEWADDR) |
1395 | connected_add_ipv4(ifp, flags, (struct in_addr *)addr, | |
1396 | ifa->ifa_prefixlen, | |
cde1af84 AK |
1397 | (struct in_addr *)broad, label, |
1398 | metric); | |
20e879f9 MS |
1399 | else if (CHECK_FLAG(flags, ZEBRA_IFA_PEER)) { |
1400 | /* Delete with a peer address */ | |
1401 | connected_delete_ipv4( | |
1402 | ifp, flags, (struct in_addr *)addr, | |
1403 | ifa->ifa_prefixlen, broad); | |
1404 | } else | |
d62a17ae | 1405 | connected_delete_ipv4( |
1406 | ifp, flags, (struct in_addr *)addr, | |
fd267f08 | 1407 | ifa->ifa_prefixlen, NULL); |
d62a17ae | 1408 | } |
20e879f9 | 1409 | |
d62a17ae | 1410 | if (ifa->ifa_family == AF_INET6) { |
930571d2 | 1411 | if (ifa->ifa_prefixlen > IPV6_MAX_BITLEN) { |
e17d9b2d | 1412 | zlog_err( |
87b5d1b0 DS |
1413 | "Invalid prefix length: %u received from kernel interface addr change: %s", |
1414 | ifa->ifa_prefixlen, | |
1415 | nl_msg_type_to_str(h->nlmsg_type)); | |
e17d9b2d | 1416 | return -1; |
930571d2 | 1417 | } |
d62a17ae | 1418 | if (h->nlmsg_type == RTM_NEWADDR) { |
1419 | /* Only consider valid addresses; we'll not get a | |
1420 | * notification from | |
1421 | * the kernel till IPv6 DAD has completed, but at init | |
1422 | * time, Quagga | |
1423 | * does query for and will receive all addresses. | |
1424 | */ | |
9254efed | 1425 | if (!(kernel_flags |
d62a17ae | 1426 | & (IFA_F_DADFAILED | IFA_F_TENTATIVE))) |
60466a63 QY |
1427 | connected_add_ipv6(ifp, flags, |
1428 | (struct in6_addr *)addr, | |
60c0687a | 1429 | (struct in6_addr *)broad, |
cde1af84 AK |
1430 | ifa->ifa_prefixlen, label, |
1431 | metric); | |
d62a17ae | 1432 | } else |
1433 | connected_delete_ipv6(ifp, (struct in6_addr *)addr, | |
fd267f08 | 1434 | NULL, ifa->ifa_prefixlen); |
d62a17ae | 1435 | } |
1436 | ||
2a181147 SW |
1437 | /* |
1438 | * Linux kernel does not send route delete on interface down/addr del | |
1439 | * so we have to re-process routes it owns (i.e. kernel routes) | |
1440 | */ | |
1441 | if (h->nlmsg_type != RTM_NEWADDR) | |
1442 | rib_update(RIB_UPDATE_KERNEL); | |
1443 | ||
d62a17ae | 1444 | return 0; |
1fdc9eae | 1445 | } |
1446 | ||
e7c2c198 MS |
1447 | /* |
1448 | * Parse and validate an incoming interface address change message, | |
1449 | * generating a dplane context object. | |
1450 | * This runs in the dplane pthread; the context is enqueued to the | |
1451 | * main pthread for processing. | |
1452 | */ | |
1453 | int netlink_interface_addr_dplane(struct nlmsghdr *h, ns_id_t ns_id, | |
1454 | int startup /*ignored*/) | |
1455 | { | |
1456 | int len; | |
1457 | struct ifaddrmsg *ifa; | |
1458 | struct rtattr *tb[IFA_MAX + 1]; | |
1459 | void *addr; | |
1460 | void *broad; | |
1461 | char *label = NULL; | |
1462 | uint32_t metric = METRIC_MAX; | |
1463 | uint32_t kernel_flags = 0; | |
1464 | struct zebra_dplane_ctx *ctx; | |
1465 | struct prefix p; | |
1466 | ||
1467 | ifa = NLMSG_DATA(h); | |
1468 | ||
1469 | /* Validate message types */ | |
1470 | if (h->nlmsg_type != RTM_NEWADDR && h->nlmsg_type != RTM_DELADDR) | |
1471 | return 0; | |
1472 | ||
1473 | if (ifa->ifa_family != AF_INET && ifa->ifa_family != AF_INET6) { | |
1474 | if (IS_ZEBRA_DEBUG_KERNEL) | |
1475 | zlog_debug("%s: %s: Invalid address family: %u", | |
1476 | __func__, nl_msg_type_to_str(h->nlmsg_type), | |
1477 | ifa->ifa_family); | |
1478 | return 0; | |
1479 | } | |
1480 | ||
1481 | len = h->nlmsg_len - NLMSG_LENGTH(sizeof(struct ifaddrmsg)); | |
1482 | if (len < 0) { | |
1483 | if (IS_ZEBRA_DEBUG_KERNEL) | |
1484 | zlog_debug("%s: %s: netlink msg bad size: %d %zu", | |
1485 | __func__, nl_msg_type_to_str(h->nlmsg_type), | |
1486 | h->nlmsg_len, | |
1487 | (size_t)NLMSG_LENGTH( | |
1488 | sizeof(struct ifaddrmsg))); | |
1489 | return -1; | |
1490 | } | |
1491 | ||
1492 | netlink_parse_rtattr(tb, IFA_MAX, IFA_RTA(ifa), len); | |
1493 | ||
1494 | /* Flags passed through */ | |
1495 | if (tb[IFA_FLAGS]) | |
1496 | kernel_flags = *(int *)RTA_DATA(tb[IFA_FLAGS]); | |
1497 | else | |
1498 | kernel_flags = ifa->ifa_flags; | |
1499 | ||
1500 | if (IS_ZEBRA_DEBUG_KERNEL) { /* remove this line to see initial ifcfg */ | |
1501 | char buf[PREFIX_STRLEN]; | |
1502 | ||
1503 | zlog_debug("%s: %s nsid %u ifindex %u flags 0x%x:", __func__, | |
1504 | nl_msg_type_to_str(h->nlmsg_type), ns_id, | |
1505 | ifa->ifa_index, kernel_flags); | |
1506 | if (tb[IFA_LOCAL]) | |
1507 | zlog_debug(" IFA_LOCAL %s/%d", | |
1508 | inet_ntop(ifa->ifa_family, | |
1509 | RTA_DATA(tb[IFA_LOCAL]), buf, | |
1510 | sizeof(buf)), | |
1511 | ifa->ifa_prefixlen); | |
1512 | if (tb[IFA_ADDRESS]) | |
1513 | zlog_debug(" IFA_ADDRESS %s/%d", | |
1514 | inet_ntop(ifa->ifa_family, | |
1515 | RTA_DATA(tb[IFA_ADDRESS]), buf, | |
1516 | sizeof(buf)), | |
1517 | ifa->ifa_prefixlen); | |
1518 | if (tb[IFA_BROADCAST]) | |
1519 | zlog_debug(" IFA_BROADCAST %s/%d", | |
1520 | inet_ntop(ifa->ifa_family, | |
1521 | RTA_DATA(tb[IFA_BROADCAST]), buf, | |
1522 | sizeof(buf)), | |
1523 | ifa->ifa_prefixlen); | |
1524 | if (tb[IFA_LABEL]) | |
1525 | zlog_debug(" IFA_LABEL %s", | |
1526 | (const char *)RTA_DATA(tb[IFA_LABEL])); | |
1527 | ||
1528 | if (tb[IFA_CACHEINFO]) { | |
1529 | struct ifa_cacheinfo *ci = RTA_DATA(tb[IFA_CACHEINFO]); | |
1530 | ||
1531 | zlog_debug(" IFA_CACHEINFO pref %d, valid %d", | |
1532 | ci->ifa_prefered, ci->ifa_valid); | |
1533 | } | |
1534 | } | |
1535 | ||
1536 | /* Validate prefix length */ | |
1537 | ||
1538 | if (ifa->ifa_family == AF_INET | |
1539 | && ifa->ifa_prefixlen > IPV4_MAX_BITLEN) { | |
1540 | if (IS_ZEBRA_DEBUG_KERNEL) | |
1541 | zlog_debug("%s: %s: Invalid prefix length: %u", | |
1542 | __func__, nl_msg_type_to_str(h->nlmsg_type), | |
1543 | ifa->ifa_prefixlen); | |
1544 | return -1; | |
1545 | } | |
1546 | ||
1547 | if (ifa->ifa_family == AF_INET6) { | |
1548 | if (ifa->ifa_prefixlen > IPV6_MAX_BITLEN) { | |
1549 | if (IS_ZEBRA_DEBUG_KERNEL) | |
1550 | zlog_debug("%s: %s: Invalid prefix length: %u", | |
1551 | __func__, | |
1552 | nl_msg_type_to_str(h->nlmsg_type), | |
1553 | ifa->ifa_prefixlen); | |
1554 | return -1; | |
1555 | } | |
1556 | ||
1557 | /* Only consider valid addresses; we'll not get a kernel | |
1558 | * notification till IPv6 DAD has completed, but at init | |
1559 | * time, FRR does query for and will receive all addresses. | |
1560 | */ | |
1561 | if (h->nlmsg_type == RTM_NEWADDR | |
1562 | && (kernel_flags & (IFA_F_DADFAILED | IFA_F_TENTATIVE))) { | |
1563 | if (IS_ZEBRA_DEBUG_KERNEL) | |
1564 | zlog_debug("%s: %s: Invalid/tentative addr", | |
1565 | __func__, | |
1566 | nl_msg_type_to_str(h->nlmsg_type)); | |
1567 | return 0; | |
1568 | } | |
1569 | } | |
1570 | ||
1571 | /* logic copied from iproute2/ip/ipaddress.c:print_addrinfo() */ | |
1572 | if (tb[IFA_LOCAL] == NULL) | |
1573 | tb[IFA_LOCAL] = tb[IFA_ADDRESS]; | |
1574 | if (tb[IFA_ADDRESS] == NULL) | |
1575 | tb[IFA_ADDRESS] = tb[IFA_LOCAL]; | |
1576 | ||
1577 | /* local interface address */ | |
1578 | addr = (tb[IFA_LOCAL] ? RTA_DATA(tb[IFA_LOCAL]) : NULL); | |
1579 | ||
1580 | /* addr is primary key, SOL if we don't have one */ | |
1581 | if (addr == NULL) { | |
1582 | if (IS_ZEBRA_DEBUG_KERNEL) | |
1583 | zlog_debug("%s: %s: No local interface address", | |
1584 | __func__, nl_msg_type_to_str(h->nlmsg_type)); | |
1585 | return -1; | |
1586 | } | |
1587 | ||
1588 | /* Allocate a context object, now that validation is done. */ | |
1589 | ctx = dplane_ctx_alloc(); | |
1590 | if (h->nlmsg_type == RTM_NEWADDR) | |
1591 | dplane_ctx_set_op(ctx, DPLANE_OP_INTF_ADDR_ADD); | |
1592 | else | |
1593 | dplane_ctx_set_op(ctx, DPLANE_OP_INTF_ADDR_DEL); | |
1594 | ||
1595 | dplane_ctx_set_ifindex(ctx, ifa->ifa_index); | |
1596 | dplane_ctx_set_ns_id(ctx, ns_id); | |
1597 | ||
1598 | /* Convert addr to prefix */ | |
1599 | memset(&p, 0, sizeof(p)); | |
1600 | p.family = ifa->ifa_family; | |
1601 | p.prefixlen = ifa->ifa_prefixlen; | |
1602 | if (p.family == AF_INET) | |
1603 | p.u.prefix4 = *(struct in_addr *)addr; | |
1604 | else | |
1605 | p.u.prefix6 = *(struct in6_addr *)addr; | |
1606 | ||
1607 | dplane_ctx_set_intf_addr(ctx, &p); | |
1608 | ||
1609 | /* is there a peer address? */ | |
1610 | if (tb[IFA_ADDRESS] | |
1611 | && memcmp(RTA_DATA(tb[IFA_ADDRESS]), RTA_DATA(tb[IFA_LOCAL]), | |
1612 | RTA_PAYLOAD(tb[IFA_ADDRESS]))) { | |
1613 | broad = RTA_DATA(tb[IFA_ADDRESS]); | |
1614 | dplane_ctx_intf_set_connected(ctx); | |
1615 | } else if (tb[IFA_BROADCAST]) { | |
1616 | /* seeking a broadcast address */ | |
1617 | broad = RTA_DATA(tb[IFA_BROADCAST]); | |
1618 | dplane_ctx_intf_set_broadcast(ctx); | |
1619 | } else | |
1620 | broad = NULL; | |
1621 | ||
1622 | if (broad) { | |
1623 | /* Convert addr to prefix */ | |
1624 | memset(&p, 0, sizeof(p)); | |
1625 | p.family = ifa->ifa_family; | |
1626 | p.prefixlen = ifa->ifa_prefixlen; | |
1627 | if (p.family == AF_INET) | |
1628 | p.u.prefix4 = *(struct in_addr *)broad; | |
1629 | else | |
1630 | p.u.prefix6 = *(struct in6_addr *)broad; | |
1631 | ||
1632 | dplane_ctx_set_intf_dest(ctx, &p); | |
1633 | } | |
1634 | ||
1635 | /* Flags. */ | |
1636 | if (kernel_flags & IFA_F_SECONDARY) | |
1637 | dplane_ctx_intf_set_secondary(ctx); | |
1638 | ||
1639 | /* Label */ | |
1640 | if (tb[IFA_LABEL]) { | |
1641 | label = (char *)RTA_DATA(tb[IFA_LABEL]); | |
1642 | dplane_ctx_set_intf_label(ctx, label); | |
1643 | } | |
1644 | ||
1645 | if (tb[IFA_RT_PRIORITY]) | |
1646 | metric = *(uint32_t *)RTA_DATA(tb[IFA_RT_PRIORITY]); | |
1647 | ||
1648 | dplane_ctx_set_intf_metric(ctx, metric); | |
1649 | ||
1650 | /* Enqueue ctx for main pthread to process */ | |
1651 | dplane_provider_enqueue_to_zebra(ctx); | |
1652 | ||
1653 | return 0; | |
1654 | } | |
1655 | ||
2414abd3 | 1656 | int netlink_link_change(struct nlmsghdr *h, ns_id_t ns_id, int startup) |
1fdc9eae | 1657 | { |
d62a17ae | 1658 | int len; |
1659 | struct ifinfomsg *ifi; | |
1660 | struct rtattr *tb[IFLA_MAX + 1]; | |
1661 | struct rtattr *linkinfo[IFLA_MAX + 1]; | |
1662 | struct interface *ifp; | |
1663 | char *name = NULL; | |
1664 | char *kind = NULL; | |
48884c6b | 1665 | char *desc = NULL; |
d62a17ae | 1666 | char *slave_kind = NULL; |
1667 | struct zebra_ns *zns; | |
1668 | vrf_id_t vrf_id = VRF_DEFAULT; | |
e6f2bec0 | 1669 | enum zebra_iftype zif_type = ZEBRA_IF_OTHER; |
a81982fa | 1670 | enum zebra_slave_iftype zif_slave_type = ZEBRA_IF_SLAVE_NONE; |
d62a17ae | 1671 | ifindex_t bridge_ifindex = IFINDEX_INTERNAL; |
b9368db9 | 1672 | ifindex_t bond_ifindex = IFINDEX_INTERNAL; |
d62a17ae | 1673 | ifindex_t link_ifindex = IFINDEX_INTERNAL; |
97c4e1d0 | 1674 | uint8_t old_hw_addr[INTERFACE_HWADDR_MAX]; |
ba5165ec | 1675 | struct zebra_if *zif; |
14ddb3d9 | 1676 | ns_id_t link_nsid = ns_id; |
c36e442c | 1677 | ifindex_t master_infindex = IFINDEX_INTERNAL; |
00a7710c | 1678 | uint8_t bypass = 0; |
d62a17ae | 1679 | |
1680 | zns = zebra_ns_lookup(ns_id); | |
1681 | ifi = NLMSG_DATA(h); | |
1682 | ||
fe533c56 | 1683 | /* assume if not default zns, then new VRF */ |
d62a17ae | 1684 | if (!(h->nlmsg_type == RTM_NEWLINK || h->nlmsg_type == RTM_DELLINK)) { |
1685 | /* If this is not link add/delete message so print warning. */ | |
87b5d1b0 DS |
1686 | zlog_debug("netlink_link_change: wrong kernel message %s", |
1687 | nl_msg_type_to_str(h->nlmsg_type)); | |
d62a17ae | 1688 | return 0; |
1689 | } | |
1690 | ||
8a1b681c SW |
1691 | if (!(ifi->ifi_family == AF_UNSPEC || ifi->ifi_family == AF_BRIDGE |
1692 | || ifi->ifi_family == AF_INET6)) { | |
9df414fe | 1693 | flog_warn( |
e914ccbe | 1694 | EC_ZEBRA_UNKNOWN_FAMILY, |
87b5d1b0 DS |
1695 | "Invalid address family: %u received from kernel link change: %s", |
1696 | ifi->ifi_family, nl_msg_type_to_str(h->nlmsg_type)); | |
8a1b681c SW |
1697 | return 0; |
1698 | } | |
1699 | ||
d62a17ae | 1700 | len = h->nlmsg_len - NLMSG_LENGTH(sizeof(struct ifinfomsg)); |
9bdf8618 | 1701 | if (len < 0) { |
15569c58 DA |
1702 | zlog_err( |
1703 | "%s: Message received from netlink is of a broken size %d %zu", | |
1704 | __func__, h->nlmsg_len, | |
1705 | (size_t)NLMSG_LENGTH(sizeof(struct ifinfomsg))); | |
d62a17ae | 1706 | return -1; |
9bdf8618 | 1707 | } |
d62a17ae | 1708 | |
1709 | /* We are interested in some AF_BRIDGE notifications. */ | |
1710 | if (ifi->ifi_family == AF_BRIDGE) | |
1711 | return netlink_bridge_interface(h, len, ns_id, startup); | |
1712 | ||
1713 | /* Looking up interface name. */ | |
0d6f7fd6 | 1714 | memset(linkinfo, 0, sizeof(linkinfo)); |
d62a17ae | 1715 | netlink_parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), len); |
1fdc9eae | 1716 | |
d62a17ae | 1717 | /* check for wireless messages to ignore */ |
1718 | if ((tb[IFLA_WIRELESS] != NULL) && (ifi->ifi_change == 0)) { | |
1719 | if (IS_ZEBRA_DEBUG_KERNEL) | |
1720 | zlog_debug("%s: ignoring IFLA_WIRELESS message", | |
1721 | __func__); | |
1722 | return 0; | |
1723 | } | |
1fdc9eae | 1724 | |
d62a17ae | 1725 | if (tb[IFLA_IFNAME] == NULL) |
1726 | return -1; | |
1727 | name = (char *)RTA_DATA(tb[IFLA_IFNAME]); | |
1fdc9eae | 1728 | |
e9f79fff MS |
1729 | /* Must be valid string. */ |
1730 | len = RTA_PAYLOAD(tb[IFLA_IFNAME]); | |
1731 | if (len < 2 || name[len - 1] != '\0') { | |
1732 | if (IS_ZEBRA_DEBUG_KERNEL) | |
1733 | zlog_debug("%s: invalid intf name", __func__); | |
1734 | return -1; | |
1735 | } | |
1736 | ||
d62a17ae | 1737 | if (tb[IFLA_LINKINFO]) { |
c9d842c7 DS |
1738 | netlink_parse_rtattr_nested(linkinfo, IFLA_INFO_MAX, |
1739 | tb[IFLA_LINKINFO]); | |
1fdc9eae | 1740 | |
d62a17ae | 1741 | if (linkinfo[IFLA_INFO_KIND]) |
1742 | kind = RTA_DATA(linkinfo[IFLA_INFO_KIND]); | |
1fdc9eae | 1743 | |
d62a17ae | 1744 | if (linkinfo[IFLA_INFO_SLAVE_KIND]) |
1745 | slave_kind = RTA_DATA(linkinfo[IFLA_INFO_SLAVE_KIND]); | |
1fdc9eae | 1746 | |
d62a17ae | 1747 | netlink_determine_zebra_iftype(kind, &zif_type); |
1748 | } | |
6675513d | 1749 | |
d62a17ae | 1750 | /* If linking to another interface, note it. */ |
1751 | if (tb[IFLA_LINK]) | |
1752 | link_ifindex = *(ifindex_t *)RTA_DATA(tb[IFLA_LINK]); | |
1fdc9eae | 1753 | |
b1cc23b2 | 1754 | if (tb[IFLA_LINK_NETNSID]) { |
14ddb3d9 | 1755 | link_nsid = *(ns_id_t *)RTA_DATA(tb[IFLA_LINK_NETNSID]); |
b1cc23b2 PG |
1756 | link_nsid = ns_id_get_absolute(ns_id, link_nsid); |
1757 | } | |
48884c6b DS |
1758 | if (tb[IFLA_IFALIAS]) { |
1759 | desc = (char *)RTA_DATA(tb[IFLA_IFALIAS]); | |
1760 | } | |
1761 | ||
d62a17ae | 1762 | /* If VRF, create or update the VRF structure itself. */ |
78dd30b2 | 1763 | if (zif_type == ZEBRA_IF_VRF && !vrf_is_backend_netns()) { |
5e031198 | 1764 | netlink_vrf_change(h, tb[IFLA_LINKINFO], ns_id, name); |
d62a17ae | 1765 | vrf_id = (vrf_id_t)ifi->ifi_index; |
1766 | } | |
1fdc9eae | 1767 | |
d62a17ae | 1768 | /* See if interface is present. */ |
1769 | ifp = if_lookup_by_name_per_ns(zns, name); | |
1770 | ||
1771 | if (h->nlmsg_type == RTM_NEWLINK) { | |
1772 | if (tb[IFLA_MASTER]) { | |
78dd30b2 PG |
1773 | if (slave_kind && (strcmp(slave_kind, "vrf") == 0) |
1774 | && !vrf_is_backend_netns()) { | |
d62a17ae | 1775 | zif_slave_type = ZEBRA_IF_SLAVE_VRF; |
c36e442c AK |
1776 | master_infindex = vrf_id = |
1777 | *(uint32_t *)RTA_DATA(tb[IFLA_MASTER]); | |
d62a17ae | 1778 | } else if (slave_kind |
1779 | && (strcmp(slave_kind, "bridge") == 0)) { | |
1780 | zif_slave_type = ZEBRA_IF_SLAVE_BRIDGE; | |
c36e442c | 1781 | master_infindex = bridge_ifindex = |
d62a17ae | 1782 | *(ifindex_t *)RTA_DATA(tb[IFLA_MASTER]); |
b9368db9 DD |
1783 | } else if (slave_kind |
1784 | && (strcmp(slave_kind, "bond") == 0)) { | |
1785 | zif_slave_type = ZEBRA_IF_SLAVE_BOND; | |
c36e442c | 1786 | master_infindex = bond_ifindex = |
b9368db9 | 1787 | *(ifindex_t *)RTA_DATA(tb[IFLA_MASTER]); |
00a7710c | 1788 | bypass = netlink_parse_lacp_bypass(linkinfo); |
d62a17ae | 1789 | } else |
1790 | zif_slave_type = ZEBRA_IF_SLAVE_OTHER; | |
1791 | } | |
fe533c56 PG |
1792 | if (vrf_is_backend_netns()) |
1793 | vrf_id = (vrf_id_t)ns_id; | |
d62a17ae | 1794 | if (ifp == NULL |
1795 | || !CHECK_FLAG(ifp->status, ZEBRA_INTERFACE_ACTIVE)) { | |
1796 | /* Add interface notification from kernel */ | |
1797 | if (IS_ZEBRA_DEBUG_KERNEL) | |
1798 | zlog_debug( | |
3efd0893 | 1799 | "RTM_NEWLINK ADD for %s(%u) vrf_id %u type %d sl_type %d master %u flags 0x%x", |
d62a17ae | 1800 | name, ifi->ifi_index, vrf_id, zif_type, |
c36e442c | 1801 | zif_slave_type, master_infindex, |
d62a17ae | 1802 | ifi->ifi_flags); |
1803 | ||
1804 | if (ifp == NULL) { | |
1805 | /* unknown interface */ | |
a36898e7 | 1806 | ifp = if_get_by_name(name, vrf_id); |
d62a17ae | 1807 | } else { |
1808 | /* pre-configured interface, learnt now */ | |
a36898e7 DS |
1809 | if (ifp->vrf_id != vrf_id) |
1810 | if_update_to_new_vrf(ifp, vrf_id); | |
d62a17ae | 1811 | } |
1812 | ||
1813 | /* Update interface information. */ | |
1814 | set_ifindex(ifp, ifi->ifi_index, zns); | |
1815 | ifp->flags = ifi->ifi_flags & 0x0000fffff; | |
d23b983b | 1816 | if (!tb[IFLA_MTU]) { |
9df414fe | 1817 | zlog_debug( |
d23b983b SW |
1818 | "RTM_NEWLINK for interface %s(%u) without MTU set", |
1819 | name, ifi->ifi_index); | |
1820 | return 0; | |
1821 | } | |
d62a17ae | 1822 | ifp->mtu6 = ifp->mtu = *(int *)RTA_DATA(tb[IFLA_MTU]); |
1823 | ifp->metric = 0; | |
1824 | ifp->ptm_status = ZEBRA_PTM_STATUS_UNKNOWN; | |
1825 | ||
1826 | /* Set interface type */ | |
1827 | zebra_if_set_ziftype(ifp, zif_type, zif_slave_type); | |
b0fa6f6a CS |
1828 | if (IS_ZEBRA_IF_VRF(ifp)) |
1829 | SET_FLAG(ifp->status, | |
1830 | ZEBRA_INTERFACE_VRF_LOOPBACK); | |
d62a17ae | 1831 | |
1832 | /* Update link. */ | |
680c278f | 1833 | zebra_if_update_link(ifp, link_ifindex, ns_id); |
d62a17ae | 1834 | |
1835 | netlink_interface_update_hw_addr(tb, ifp); | |
1836 | ||
1837 | /* Inform clients, install any configured addresses. */ | |
1838 | if_add_update(ifp); | |
1839 | ||
1840 | /* Extract and save L2 interface information, take | |
1841 | * additional actions. */ | |
1842 | netlink_interface_update_l2info( | |
14ddb3d9 PG |
1843 | ifp, linkinfo[IFLA_INFO_DATA], |
1844 | 1, link_nsid); | |
d62a17ae | 1845 | if (IS_ZEBRA_IF_BRIDGE_SLAVE(ifp)) |
c7620108 PG |
1846 | zebra_l2if_update_bridge_slave( |
1847 | ifp, bridge_ifindex, ns_id, | |
1848 | ZEBRA_BRIDGE_NO_ACTION); | |
b9368db9 | 1849 | else if (IS_ZEBRA_IF_BOND_SLAVE(ifp)) |
00a7710c AK |
1850 | zebra_l2if_update_bond_slave(ifp, bond_ifindex, |
1851 | !!bypass); | |
c36e442c AK |
1852 | |
1853 | if (tb[IFLA_PROTO_DOWN]) { | |
1854 | uint8_t protodown; | |
1855 | ||
1856 | protodown = *(uint8_t *)RTA_DATA( | |
1857 | tb[IFLA_PROTO_DOWN]); | |
1858 | netlink_proc_dplane_if_protodown(ifp->info, | |
1859 | !!protodown); | |
1860 | } | |
a36898e7 | 1861 | } else if (ifp->vrf_id != vrf_id) { |
d62a17ae | 1862 | /* VRF change for an interface. */ |
1863 | if (IS_ZEBRA_DEBUG_KERNEL) | |
1864 | zlog_debug( | |
3efd0893 | 1865 | "RTM_NEWLINK vrf-change for %s(%u) vrf_id %u -> %u flags 0x%x", |
a36898e7 DS |
1866 | name, ifp->ifindex, ifp->vrf_id, vrf_id, |
1867 | ifi->ifi_flags); | |
d62a17ae | 1868 | |
a36898e7 | 1869 | if_handle_vrf_change(ifp, vrf_id); |
d62a17ae | 1870 | } else { |
b9368db9 | 1871 | bool was_bridge_slave, was_bond_slave; |
c7620108 | 1872 | uint8_t chgflags = ZEBRA_BRIDGE_NO_ACTION; |
d62a17ae | 1873 | |
1874 | /* Interface update. */ | |
1875 | if (IS_ZEBRA_DEBUG_KERNEL) | |
1876 | zlog_debug( | |
3efd0893 | 1877 | "RTM_NEWLINK update for %s(%u) sl_type %d master %u flags 0x%x", |
d62a17ae | 1878 | name, ifp->ifindex, zif_slave_type, |
c36e442c | 1879 | master_infindex, ifi->ifi_flags); |
d62a17ae | 1880 | |
1881 | set_ifindex(ifp, ifi->ifi_index, zns); | |
d23b983b | 1882 | if (!tb[IFLA_MTU]) { |
9df414fe | 1883 | zlog_debug( |
d23b983b SW |
1884 | "RTM_NEWLINK for interface %s(%u) without MTU set", |
1885 | name, ifi->ifi_index); | |
1886 | return 0; | |
1887 | } | |
d62a17ae | 1888 | ifp->mtu6 = ifp->mtu = *(int *)RTA_DATA(tb[IFLA_MTU]); |
1889 | ifp->metric = 0; | |
1890 | ||
1891 | /* Update interface type - NOTE: Only slave_type can | |
1892 | * change. */ | |
1893 | was_bridge_slave = IS_ZEBRA_IF_BRIDGE_SLAVE(ifp); | |
b9368db9 | 1894 | was_bond_slave = IS_ZEBRA_IF_BOND_SLAVE(ifp); |
d62a17ae | 1895 | zebra_if_set_ziftype(ifp, zif_type, zif_slave_type); |
1896 | ||
97c4e1d0 CS |
1897 | memcpy(old_hw_addr, ifp->hw_addr, INTERFACE_HWADDR_MAX); |
1898 | ||
ecffe916 PG |
1899 | /* Update link. */ |
1900 | zebra_if_update_link(ifp, link_ifindex, ns_id); | |
1901 | ||
d62a17ae | 1902 | netlink_interface_update_hw_addr(tb, ifp); |
1903 | ||
1904 | if (if_is_no_ptm_operative(ifp)) { | |
1905 | ifp->flags = ifi->ifi_flags & 0x0000fffff; | |
1906 | if (!if_is_no_ptm_operative(ifp)) { | |
1907 | if (IS_ZEBRA_DEBUG_KERNEL) | |
1908 | zlog_debug( | |
1909 | "Intf %s(%u) has gone DOWN", | |
1910 | name, ifp->ifindex); | |
1911 | if_down(ifp); | |
2a181147 | 1912 | rib_update(RIB_UPDATE_KERNEL); |
d62a17ae | 1913 | } else if (if_is_operative(ifp)) { |
c7620108 PG |
1914 | bool mac_updated = false; |
1915 | ||
d62a17ae | 1916 | /* Must notify client daemons of new |
1917 | * interface status. */ | |
1918 | if (IS_ZEBRA_DEBUG_KERNEL) | |
1919 | zlog_debug( | |
1920 | "Intf %s(%u) PTM up, notifying clients", | |
1921 | name, ifp->ifindex); | |
1922 | zebra_interface_up_update(ifp); | |
97c4e1d0 CS |
1923 | |
1924 | /* Update EVPN VNI when SVI MAC change | |
1925 | */ | |
c7620108 PG |
1926 | if (memcmp(old_hw_addr, ifp->hw_addr, |
1927 | INTERFACE_HWADDR_MAX)) | |
1928 | mac_updated = true; | |
1929 | if (IS_ZEBRA_IF_VLAN(ifp) | |
1930 | && mac_updated) { | |
97c4e1d0 CS |
1931 | struct interface *link_if; |
1932 | ||
1933 | link_if = | |
1934 | if_lookup_by_index_per_ns( | |
1935 | zebra_ns_lookup(NS_DEFAULT), | |
1936 | link_ifindex); | |
1937 | if (link_if) | |
1938 | zebra_vxlan_svi_up(ifp, | |
1939 | link_if); | |
c7620108 PG |
1940 | } else if (mac_updated |
1941 | && IS_ZEBRA_IF_BRIDGE(ifp)) { | |
1942 | zlog_debug( | |
1943 | "Intf %s(%u) bridge changed MAC address", | |
1944 | name, ifp->ifindex); | |
1945 | chgflags = | |
1946 | ZEBRA_BRIDGE_MASTER_MAC_CHANGE; | |
97c4e1d0 | 1947 | } |
d62a17ae | 1948 | } |
1949 | } else { | |
1950 | ifp->flags = ifi->ifi_flags & 0x0000fffff; | |
1951 | if (if_is_operative(ifp)) { | |
1952 | if (IS_ZEBRA_DEBUG_KERNEL) | |
1953 | zlog_debug( | |
1954 | "Intf %s(%u) has come UP", | |
1955 | name, ifp->ifindex); | |
1956 | if_up(ifp); | |
f56a15b5 PG |
1957 | if (IS_ZEBRA_IF_BRIDGE(ifp)) |
1958 | chgflags = | |
1959 | ZEBRA_BRIDGE_MASTER_UP; | |
6f908ded QY |
1960 | } else { |
1961 | if (IS_ZEBRA_DEBUG_KERNEL) | |
1962 | zlog_debug( | |
7913714e | 1963 | "Intf %s(%u) has gone DOWN", |
6f908ded QY |
1964 | name, ifp->ifindex); |
1965 | if_down(ifp); | |
2a181147 | 1966 | rib_update(RIB_UPDATE_KERNEL); |
d62a17ae | 1967 | } |
1968 | } | |
1969 | ||
1970 | /* Extract and save L2 interface information, take | |
1971 | * additional actions. */ | |
1972 | netlink_interface_update_l2info( | |
14ddb3d9 PG |
1973 | ifp, linkinfo[IFLA_INFO_DATA], |
1974 | 0, link_nsid); | |
c7620108 PG |
1975 | if (IS_ZEBRA_IF_BRIDGE(ifp)) |
1976 | zebra_l2if_update_bridge(ifp, chgflags); | |
c36e442c AK |
1977 | if (IS_ZEBRA_IF_BOND(ifp)) |
1978 | zebra_l2if_update_bond(ifp, true); | |
d62a17ae | 1979 | if (IS_ZEBRA_IF_BRIDGE_SLAVE(ifp) || was_bridge_slave) |
c7620108 PG |
1980 | zebra_l2if_update_bridge_slave( |
1981 | ifp, bridge_ifindex, ns_id, chgflags); | |
b9368db9 | 1982 | else if (IS_ZEBRA_IF_BOND_SLAVE(ifp) || was_bond_slave) |
00a7710c AK |
1983 | zebra_l2if_update_bond_slave(ifp, bond_ifindex, |
1984 | !!bypass); | |
c36e442c AK |
1985 | |
1986 | if (tb[IFLA_PROTO_DOWN]) { | |
1987 | uint8_t protodown; | |
1988 | ||
1989 | protodown = *(uint8_t *)RTA_DATA( | |
1990 | tb[IFLA_PROTO_DOWN]); | |
1991 | netlink_proc_dplane_if_protodown(ifp->info, | |
1992 | !!protodown); | |
1993 | } | |
d62a17ae | 1994 | } |
26f13577 DS |
1995 | |
1996 | zif = ifp->info; | |
1997 | if (zif) { | |
1998 | XFREE(MTYPE_TMP, zif->desc); | |
1999 | if (desc) | |
2000 | zif->desc = XSTRDUP(MTYPE_TMP, desc); | |
2001 | } | |
d62a17ae | 2002 | } else { |
2003 | /* Delete interface notification from kernel */ | |
2004 | if (ifp == NULL) { | |
9df414fe QY |
2005 | if (IS_ZEBRA_DEBUG_KERNEL) |
2006 | zlog_debug( | |
2007 | "RTM_DELLINK for unknown interface %s(%u)", | |
2008 | name, ifi->ifi_index); | |
d62a17ae | 2009 | return 0; |
2010 | } | |
2011 | ||
2012 | if (IS_ZEBRA_DEBUG_KERNEL) | |
2013 | zlog_debug("RTM_DELLINK for %s(%u)", name, | |
2014 | ifp->ifindex); | |
2015 | ||
2016 | UNSET_FLAG(ifp->status, ZEBRA_INTERFACE_VRF_LOOPBACK); | |
2017 | ||
c36e442c AK |
2018 | if (IS_ZEBRA_IF_BOND(ifp)) |
2019 | zebra_l2if_update_bond(ifp, false); | |
00a7710c AK |
2020 | if (IS_ZEBRA_IF_BOND_SLAVE(ifp)) |
2021 | zebra_l2if_update_bond_slave(ifp, bond_ifindex, false); | |
d62a17ae | 2022 | /* Special handling for bridge or VxLAN interfaces. */ |
2023 | if (IS_ZEBRA_IF_BRIDGE(ifp)) | |
2024 | zebra_l2_bridge_del(ifp); | |
2025 | else if (IS_ZEBRA_IF_VXLAN(ifp)) | |
2026 | zebra_l2_vxlanif_del(ifp); | |
2027 | ||
af736200 | 2028 | if_delete_update(ifp); |
1fdc9eae | 2029 | } |
2030 | ||
d62a17ae | 2031 | return 0; |
1fdc9eae | 2032 | } |
718e3744 | 2033 | |
c3bd894e QY |
2034 | int netlink_protodown(struct interface *ifp, bool down) |
2035 | { | |
2036 | struct zebra_ns *zns = zebra_ns_lookup(NS_DEFAULT); | |
2037 | ||
2038 | struct { | |
2039 | struct nlmsghdr n; | |
2040 | struct ifinfomsg ifa; | |
2041 | char buf[NL_PKT_BUF_SIZE]; | |
2042 | } req; | |
2043 | ||
2fff50ec | 2044 | memset(&req, 0, sizeof(req)); |
c3bd894e QY |
2045 | |
2046 | req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg)); | |
2047 | req.n.nlmsg_flags = NLM_F_REQUEST; | |
2048 | req.n.nlmsg_type = RTM_SETLINK; | |
2049 | req.n.nlmsg_pid = zns->netlink_cmd.snl.nl_pid; | |
2050 | ||
2051 | req.ifa.ifi_index = ifp->ifindex; | |
2052 | ||
312a6bee | 2053 | nl_attr_put(&req.n, sizeof(req), IFLA_PROTO_DOWN, &down, sizeof(down)); |
a757997c | 2054 | nl_attr_put32(&req.n, sizeof(req), IFLA_LINK, ifp->ifindex); |
c3bd894e QY |
2055 | |
2056 | return netlink_talk(netlink_talk_filter, &req.n, &zns->netlink_cmd, zns, | |
2057 | 0); | |
2058 | } | |
2059 | ||
718e3744 | 2060 | /* Interface information read by netlink. */ |
d62a17ae | 2061 | void interface_list(struct zebra_ns *zns) |
718e3744 | 2062 | { |
d62a17ae | 2063 | interface_lookup_netlink(zns); |
d9f5b2f5 SW |
2064 | /* We add routes for interface address, |
2065 | * so we need to get the nexthop info | |
2066 | * from the kernel before we can do that | |
2067 | */ | |
81505946 | 2068 | netlink_nexthop_read(zns); |
cc4e0650 | 2069 | |
d2bec88a | 2070 | interface_addr_lookup_netlink(zns); |
718e3744 | 2071 | } |
ddfeb486 DL |
2072 | |
2073 | #endif /* GNU_LINUX */ |