]>
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" |
ebb61fca | 77 | #include "zebra/netconf_netlink.h" |
1fdc9eae | 78 | |
0268f30e | 79 | extern struct zebra_privs_t zserv_privs; |
1fdc9eae | 80 | |
81 | /* Note: on netlink systems, there should be a 1-to-1 mapping between interface | |
82 | names and ifindex values. */ | |
d62a17ae | 83 | static void set_ifindex(struct interface *ifp, ifindex_t ifi_index, |
84 | struct zebra_ns *zns) | |
1fdc9eae | 85 | { |
d62a17ae | 86 | struct interface *oifp; |
87 | ||
88 | if (((oifp = if_lookup_by_index_per_ns(zns, ifi_index)) != NULL) | |
89 | && (oifp != ifp)) { | |
90 | if (ifi_index == IFINDEX_INTERNAL) | |
af4c2728 | 91 | flog_err( |
450971aa | 92 | EC_LIB_INTERFACE, |
4d43f68a | 93 | "Netlink is setting interface %s ifindex to reserved internal value %u", |
d62a17ae | 94 | ifp->name, ifi_index); |
95 | else { | |
96 | if (IS_ZEBRA_DEBUG_KERNEL) | |
97 | zlog_debug( | |
98 | "interface index %d was renamed from %s to %s", | |
99 | ifi_index, oifp->name, ifp->name); | |
100 | if (if_is_up(oifp)) | |
af4c2728 | 101 | flog_err( |
450971aa | 102 | EC_LIB_INTERFACE, |
4d43f68a | 103 | "interface rename detected on up interface: index %d was renamed from %s to %s, results are uncertain!", |
d62a17ae | 104 | ifi_index, oifp->name, ifp->name); |
105 | if_delete_update(oifp); | |
106 | } | |
107 | } | |
ff880b78 | 108 | if_set_index(ifp, ifi_index); |
1fdc9eae | 109 | } |
110 | ||
111 | /* Utility function to parse hardware link-layer address and update ifp */ | |
d62a17ae | 112 | static void netlink_interface_update_hw_addr(struct rtattr **tb, |
113 | struct interface *ifp) | |
1fdc9eae | 114 | { |
d62a17ae | 115 | int i; |
116 | ||
117 | if (tb[IFLA_ADDRESS]) { | |
118 | int hw_addr_len; | |
119 | ||
120 | hw_addr_len = RTA_PAYLOAD(tb[IFLA_ADDRESS]); | |
121 | ||
122 | if (hw_addr_len > INTERFACE_HWADDR_MAX) | |
9df414fe QY |
123 | zlog_debug("Hardware address is too large: %d", |
124 | hw_addr_len); | |
d62a17ae | 125 | else { |
126 | ifp->hw_addr_len = hw_addr_len; | |
127 | memcpy(ifp->hw_addr, RTA_DATA(tb[IFLA_ADDRESS]), | |
128 | hw_addr_len); | |
129 | ||
130 | for (i = 0; i < hw_addr_len; i++) | |
131 | if (ifp->hw_addr[i] != 0) | |
132 | break; | |
133 | ||
134 | if (i == hw_addr_len) | |
135 | ifp->hw_addr_len = 0; | |
136 | else | |
137 | ifp->hw_addr_len = hw_addr_len; | |
138 | } | |
139 | } | |
1fdc9eae | 140 | } |
141 | ||
d62a17ae | 142 | static enum zebra_link_type netlink_to_zebra_link_type(unsigned int hwt) |
1fdc9eae | 143 | { |
d62a17ae | 144 | switch (hwt) { |
145 | case ARPHRD_ETHER: | |
146 | return ZEBRA_LLT_ETHER; | |
147 | case ARPHRD_EETHER: | |
148 | return ZEBRA_LLT_EETHER; | |
149 | case ARPHRD_AX25: | |
150 | return ZEBRA_LLT_AX25; | |
151 | case ARPHRD_PRONET: | |
152 | return ZEBRA_LLT_PRONET; | |
153 | case ARPHRD_IEEE802: | |
154 | return ZEBRA_LLT_IEEE802; | |
155 | case ARPHRD_ARCNET: | |
156 | return ZEBRA_LLT_ARCNET; | |
157 | case ARPHRD_APPLETLK: | |
158 | return ZEBRA_LLT_APPLETLK; | |
159 | case ARPHRD_DLCI: | |
160 | return ZEBRA_LLT_DLCI; | |
161 | case ARPHRD_ATM: | |
162 | return ZEBRA_LLT_ATM; | |
163 | case ARPHRD_METRICOM: | |
164 | return ZEBRA_LLT_METRICOM; | |
165 | case ARPHRD_IEEE1394: | |
166 | return ZEBRA_LLT_IEEE1394; | |
167 | case ARPHRD_EUI64: | |
168 | return ZEBRA_LLT_EUI64; | |
169 | case ARPHRD_INFINIBAND: | |
170 | return ZEBRA_LLT_INFINIBAND; | |
171 | case ARPHRD_SLIP: | |
172 | return ZEBRA_LLT_SLIP; | |
173 | case ARPHRD_CSLIP: | |
174 | return ZEBRA_LLT_CSLIP; | |
175 | case ARPHRD_SLIP6: | |
176 | return ZEBRA_LLT_SLIP6; | |
177 | case ARPHRD_CSLIP6: | |
178 | return ZEBRA_LLT_CSLIP6; | |
179 | case ARPHRD_RSRVD: | |
180 | return ZEBRA_LLT_RSRVD; | |
181 | case ARPHRD_ADAPT: | |
182 | return ZEBRA_LLT_ADAPT; | |
183 | case ARPHRD_ROSE: | |
184 | return ZEBRA_LLT_ROSE; | |
185 | case ARPHRD_X25: | |
186 | return ZEBRA_LLT_X25; | |
187 | case ARPHRD_PPP: | |
188 | return ZEBRA_LLT_PPP; | |
189 | case ARPHRD_CISCO: | |
190 | return ZEBRA_LLT_CHDLC; | |
191 | case ARPHRD_LAPB: | |
192 | return ZEBRA_LLT_LAPB; | |
193 | case ARPHRD_RAWHDLC: | |
194 | return ZEBRA_LLT_RAWHDLC; | |
195 | case ARPHRD_TUNNEL: | |
196 | return ZEBRA_LLT_IPIP; | |
197 | case ARPHRD_TUNNEL6: | |
198 | return ZEBRA_LLT_IPIP6; | |
199 | case ARPHRD_FRAD: | |
200 | return ZEBRA_LLT_FRAD; | |
201 | case ARPHRD_SKIP: | |
202 | return ZEBRA_LLT_SKIP; | |
203 | case ARPHRD_LOOPBACK: | |
204 | return ZEBRA_LLT_LOOPBACK; | |
205 | case ARPHRD_LOCALTLK: | |
206 | return ZEBRA_LLT_LOCALTLK; | |
207 | case ARPHRD_FDDI: | |
208 | return ZEBRA_LLT_FDDI; | |
209 | case ARPHRD_SIT: | |
210 | return ZEBRA_LLT_SIT; | |
211 | case ARPHRD_IPDDP: | |
212 | return ZEBRA_LLT_IPDDP; | |
213 | case ARPHRD_IPGRE: | |
214 | return ZEBRA_LLT_IPGRE; | |
215 | case ARPHRD_PIMREG: | |
216 | return ZEBRA_LLT_PIMREG; | |
217 | case ARPHRD_HIPPI: | |
218 | return ZEBRA_LLT_HIPPI; | |
219 | case ARPHRD_ECONET: | |
220 | return ZEBRA_LLT_ECONET; | |
221 | case ARPHRD_IRDA: | |
222 | return ZEBRA_LLT_IRDA; | |
223 | case ARPHRD_FCPP: | |
224 | return ZEBRA_LLT_FCPP; | |
225 | case ARPHRD_FCAL: | |
226 | return ZEBRA_LLT_FCAL; | |
227 | case ARPHRD_FCPL: | |
228 | return ZEBRA_LLT_FCPL; | |
229 | case ARPHRD_FCFABRIC: | |
230 | return ZEBRA_LLT_FCFABRIC; | |
231 | case ARPHRD_IEEE802_TR: | |
232 | return ZEBRA_LLT_IEEE802_TR; | |
233 | case ARPHRD_IEEE80211: | |
234 | return ZEBRA_LLT_IEEE80211; | |
4268e09e | 235 | #ifdef ARPHRD_IEEE802154 |
d62a17ae | 236 | case ARPHRD_IEEE802154: |
237 | return ZEBRA_LLT_IEEE802154; | |
4268e09e | 238 | #endif |
1fdc9eae | 239 | #ifdef ARPHRD_IP6GRE |
d62a17ae | 240 | case ARPHRD_IP6GRE: |
241 | return ZEBRA_LLT_IP6GRE; | |
1fdc9eae | 242 | #endif |
243 | #ifdef ARPHRD_IEEE802154_PHY | |
d62a17ae | 244 | case ARPHRD_IEEE802154_PHY: |
245 | return ZEBRA_LLT_IEEE802154_PHY; | |
1fdc9eae | 246 | #endif |
247 | ||
d62a17ae | 248 | default: |
249 | return ZEBRA_LLT_UNKNOWN; | |
250 | } | |
1fdc9eae | 251 | } |
252 | ||
42b56639 | 253 | static inline void zebra_if_set_ziftype(struct interface *ifp, |
e6f2bec0 | 254 | enum zebra_iftype zif_type, |
a81982fa | 255 | enum zebra_slave_iftype zif_slave_type) |
42b56639 AK |
256 | { |
257 | struct zebra_if *zif; | |
258 | ||
259 | zif = (struct zebra_if *)ifp->info; | |
260 | zif->zif_slave_type = zif_slave_type; | |
261 | ||
262 | if (zif->zif_type != zif_type) { | |
263 | zif->zif_type = zif_type; | |
264 | /* If the if_type has been set to bond initialize ES info | |
265 | * against it. XXX - note that we don't handle the case where | |
266 | * a zif changes from bond to non-bond; it is really | |
267 | * an unexpected/error condition. | |
268 | */ | |
269 | zebra_evpn_if_init(zif); | |
270 | } | |
271 | } | |
272 | ||
b9368db9 | 273 | static void netlink_determine_zebra_iftype(const char *kind, |
e6f2bec0 | 274 | enum zebra_iftype *zif_type) |
6675513d | 275 | { |
d62a17ae | 276 | *zif_type = ZEBRA_IF_OTHER; |
277 | ||
278 | if (!kind) | |
279 | return; | |
280 | ||
281 | if (strcmp(kind, "vrf") == 0) | |
282 | *zif_type = ZEBRA_IF_VRF; | |
283 | else if (strcmp(kind, "bridge") == 0) | |
284 | *zif_type = ZEBRA_IF_BRIDGE; | |
285 | else if (strcmp(kind, "vlan") == 0) | |
286 | *zif_type = ZEBRA_IF_VLAN; | |
287 | else if (strcmp(kind, "vxlan") == 0) | |
288 | *zif_type = ZEBRA_IF_VXLAN; | |
1a98c087 MK |
289 | else if (strcmp(kind, "macvlan") == 0) |
290 | *zif_type = ZEBRA_IF_MACVLAN; | |
0e4864ea PG |
291 | else if (strcmp(kind, "veth") == 0) |
292 | *zif_type = ZEBRA_IF_VETH; | |
b9368db9 DD |
293 | else if (strcmp(kind, "bond") == 0) |
294 | *zif_type = ZEBRA_IF_BOND; | |
295 | else if (strcmp(kind, "bond_slave") == 0) | |
296 | *zif_type = ZEBRA_IF_BOND_SLAVE; | |
077c07cc PG |
297 | else if (strcmp(kind, "gre") == 0) |
298 | *zif_type = ZEBRA_IF_GRE; | |
6675513d | 299 | } |
52d8f0d8 | 300 | |
d62a17ae | 301 | static void netlink_vrf_change(struct nlmsghdr *h, struct rtattr *tb, |
5e031198 | 302 | uint32_t ns_id, const char *name) |
1fdc9eae | 303 | { |
d62a17ae | 304 | struct ifinfomsg *ifi; |
305 | struct rtattr *linkinfo[IFLA_INFO_MAX + 1]; | |
306 | struct rtattr *attr[IFLA_VRF_MAX + 1]; | |
75d26fb3 | 307 | struct vrf *vrf = NULL; |
d62a17ae | 308 | struct zebra_vrf *zvrf; |
d7c0a89a | 309 | uint32_t nl_table_id; |
d62a17ae | 310 | |
311 | ifi = NLMSG_DATA(h); | |
312 | ||
c9d842c7 | 313 | netlink_parse_rtattr_nested(linkinfo, IFLA_INFO_MAX, tb); |
d62a17ae | 314 | |
315 | if (!linkinfo[IFLA_INFO_DATA]) { | |
316 | if (IS_ZEBRA_DEBUG_KERNEL) | |
317 | zlog_debug( | |
318 | "%s: IFLA_INFO_DATA missing from VRF message: %s", | |
319 | __func__, name); | |
320 | return; | |
321 | } | |
322 | ||
c9d842c7 DS |
323 | netlink_parse_rtattr_nested(attr, IFLA_VRF_MAX, |
324 | linkinfo[IFLA_INFO_DATA]); | |
d62a17ae | 325 | if (!attr[IFLA_VRF_TABLE]) { |
326 | if (IS_ZEBRA_DEBUG_KERNEL) | |
327 | zlog_debug( | |
328 | "%s: IFLA_VRF_TABLE missing from VRF message: %s", | |
329 | __func__, name); | |
330 | return; | |
1fdc9eae | 331 | } |
332 | ||
d7c0a89a | 333 | nl_table_id = *(uint32_t *)RTA_DATA(attr[IFLA_VRF_TABLE]); |
d62a17ae | 334 | |
335 | if (h->nlmsg_type == RTM_NEWLINK) { | |
336 | if (IS_ZEBRA_DEBUG_KERNEL) | |
337 | zlog_debug("RTM_NEWLINK for VRF %s(%u) table %u", name, | |
338 | ifi->ifi_index, nl_table_id); | |
339 | ||
2e86d16d RW |
340 | if (!vrf_lookup_by_id((vrf_id_t)ifi->ifi_index)) { |
341 | vrf_id_t exist_id; | |
5e031198 | 342 | |
2e86d16d RW |
343 | exist_id = vrf_lookup_by_table(nl_table_id, ns_id); |
344 | if (exist_id != VRF_DEFAULT) { | |
345 | vrf = vrf_lookup_by_id(exist_id); | |
346 | ||
347 | flog_err( | |
348 | EC_ZEBRA_VRF_MISCONFIGURED, | |
349 | "VRF %s id %u table id overlaps existing vrf %s, misconfiguration exiting", | |
350 | name, ifi->ifi_index, vrf->name); | |
351 | exit(-1); | |
352 | } | |
5e031198 | 353 | } |
2e86d16d | 354 | |
75d26fb3 | 355 | vrf = vrf_update((vrf_id_t)ifi->ifi_index, name); |
d62a17ae | 356 | if (!vrf) { |
450971aa | 357 | flog_err(EC_LIB_INTERFACE, "VRF %s id %u not created", |
1c50c1c0 | 358 | name, ifi->ifi_index); |
d62a17ae | 359 | return; |
360 | } | |
361 | ||
d62a17ae | 362 | /* |
363 | * This is the only place that we get the actual kernel table_id | |
364 | * being used. We need it to set the table_id of the routes | |
365 | * we are passing to the kernel.... And to throw some totally | |
366 | * awesome parties. that too. | |
593406a1 DS |
367 | * |
368 | * At this point we *must* have a zvrf because the vrf_create | |
369 | * callback creates one. We *must* set the table id | |
370 | * before the vrf_enable because of( at the very least ) | |
371 | * static routes being delayed for installation until | |
372 | * during the vrf_enable callbacks. | |
d62a17ae | 373 | */ |
374 | zvrf = (struct zebra_vrf *)vrf->info; | |
375 | zvrf->table_id = nl_table_id; | |
593406a1 DS |
376 | |
377 | /* Enable the created VRF. */ | |
378 | if (!vrf_enable(vrf)) { | |
450971aa | 379 | flog_err(EC_LIB_INTERFACE, |
1c50c1c0 QY |
380 | "Failed to enable VRF %s id %u", name, |
381 | ifi->ifi_index); | |
593406a1 DS |
382 | return; |
383 | } | |
384 | ||
d62a17ae | 385 | } else // h->nlmsg_type == RTM_DELLINK |
386 | { | |
387 | if (IS_ZEBRA_DEBUG_KERNEL) | |
388 | zlog_debug("RTM_DELLINK for VRF %s(%u)", name, | |
389 | ifi->ifi_index); | |
390 | ||
391 | vrf = vrf_lookup_by_id((vrf_id_t)ifi->ifi_index); | |
392 | ||
393 | if (!vrf) { | |
e914ccbe | 394 | flog_warn(EC_ZEBRA_VRF_NOT_FOUND, "%s: vrf not found", |
9df414fe | 395 | __func__); |
d62a17ae | 396 | return; |
397 | } | |
398 | ||
399 | vrf_delete(vrf); | |
400 | } | |
1fdc9eae | 401 | } |
402 | ||
67188ca2 | 403 | static uint32_t get_iflink_speed(struct interface *interface, int *error) |
535fe877 | 404 | { |
d62a17ae | 405 | struct ifreq ifdata; |
406 | struct ethtool_cmd ecmd; | |
407 | int sd; | |
408 | int rc; | |
0268f30e | 409 | const char *ifname = interface->name; |
d62a17ae | 410 | |
594c2878 JF |
411 | if (error) |
412 | *error = 0; | |
d62a17ae | 413 | /* initialize struct */ |
414 | memset(&ifdata, 0, sizeof(ifdata)); | |
415 | ||
416 | /* set interface name */ | |
0af35d90 | 417 | strlcpy(ifdata.ifr_name, ifname, sizeof(ifdata.ifr_name)); |
d62a17ae | 418 | |
419 | /* initialize ethtool interface */ | |
420 | memset(&ecmd, 0, sizeof(ecmd)); | |
421 | ecmd.cmd = ETHTOOL_GSET; /* ETHTOOL_GLINK */ | |
ba85366a | 422 | ifdata.ifr_data = (caddr_t)&ecmd; |
d62a17ae | 423 | |
424 | /* use ioctl to get IP address of an interface */ | |
0cf6db21 | 425 | frr_with_privs(&zserv_privs) { |
01b9e3fd | 426 | sd = vrf_socket(PF_INET, SOCK_DGRAM, IPPROTO_IP, |
096f7609 | 427 | interface->vrf->vrf_id, NULL); |
01b9e3fd DL |
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 */ |
096f7609 | 438 | rc = vrf_ioctl(interface->vrf->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 | |
f60a1188 | 975 | ifp = if_get_by_name(name, vrf_id, NULL); |
ea7ec261 | 976 | set_ifindex(ifp, ifi->ifi_index, zns); /* add it to ns struct */ |
d5c65bf1 | 977 | |
d62a17ae | 978 | ifp->flags = ifi->ifi_flags & 0x0000fffff; |
d62a17ae | 979 | ifp->mtu6 = ifp->mtu = *(uint32_t *)RTA_DATA(tb[IFLA_MTU]); |
980 | ifp->metric = 0; | |
594c2878 | 981 | ifp->speed = get_iflink_speed(ifp, NULL); |
d62a17ae | 982 | ifp->ptm_status = ZEBRA_PTM_STATUS_UNKNOWN; |
983 | ||
984 | /* Set zebra interface type */ | |
985 | zebra_if_set_ziftype(ifp, zif_type, zif_slave_type); | |
b0fa6f6a CS |
986 | if (IS_ZEBRA_IF_VRF(ifp)) |
987 | SET_FLAG(ifp->status, ZEBRA_INTERFACE_VRF_LOOPBACK); | |
d62a17ae | 988 | |
98efddf1 DS |
989 | /* |
990 | * Just set the @link/lower-device ifindex. During nldump interfaces are | |
520ebf72 AK |
991 | * not ordered in any fashion so we may end up getting upper devices |
992 | * before lower devices. We will setup the real linkage once the dump | |
98efddf1 DS |
993 | * is complete. |
994 | */ | |
520ebf72 AK |
995 | zif = (struct zebra_if *)ifp->info; |
996 | zif->link_ifindex = link_ifindex; | |
d62a17ae | 997 | |
ba5165ec DS |
998 | if (desc) { |
999 | XFREE(MTYPE_TMP, zif->desc); | |
1000 | zif->desc = XSTRDUP(MTYPE_TMP, desc); | |
1001 | } | |
1002 | ||
d62a17ae | 1003 | /* Hardware type and address. */ |
1004 | ifp->ll_type = netlink_to_zebra_link_type(ifi->ifi_type); | |
ebb61fca | 1005 | |
d62a17ae | 1006 | netlink_interface_update_hw_addr(tb, ifp); |
1007 | ||
1008 | if_add_update(ifp); | |
1009 | ||
1010 | /* Extract and save L2 interface information, take additional actions. | |
1011 | */ | |
14ddb3d9 PG |
1012 | netlink_interface_update_l2info(ifp, linkinfo[IFLA_INFO_DATA], |
1013 | 1, link_nsid); | |
c36e442c AK |
1014 | if (IS_ZEBRA_IF_BOND(ifp)) |
1015 | zebra_l2if_update_bond(ifp, true); | |
d62a17ae | 1016 | if (IS_ZEBRA_IF_BRIDGE_SLAVE(ifp)) |
c7620108 PG |
1017 | zebra_l2if_update_bridge_slave(ifp, bridge_ifindex, ns_id, |
1018 | ZEBRA_BRIDGE_NO_ACTION); | |
b9368db9 | 1019 | else if (IS_ZEBRA_IF_BOND_SLAVE(ifp)) |
00a7710c | 1020 | zebra_l2if_update_bond_slave(ifp, bond_ifindex, !!bypass); |
d62a17ae | 1021 | |
c36e442c AK |
1022 | if (tb[IFLA_PROTO_DOWN]) { |
1023 | uint8_t protodown; | |
1024 | ||
1025 | protodown = *(uint8_t *)RTA_DATA(tb[IFLA_PROTO_DOWN]); | |
1026 | netlink_proc_dplane_if_protodown(zif, !!protodown); | |
1027 | } | |
1028 | ||
d62a17ae | 1029 | return 0; |
1fdc9eae | 1030 | } |
1031 | ||
289602d7 | 1032 | /* Request for specific interface or address information from the kernel */ |
85a75f1e MS |
1033 | static int netlink_request_intf_addr(struct nlsock *netlink_cmd, int family, |
1034 | int type, uint32_t filter_mask) | |
289602d7 | 1035 | { |
d62a17ae | 1036 | struct { |
1037 | struct nlmsghdr n; | |
1038 | struct ifinfomsg ifm; | |
1039 | char buf[256]; | |
1040 | } req; | |
1041 | ||
1042 | /* Form the request, specifying filter (rtattr) if needed. */ | |
1043 | memset(&req, 0, sizeof(req)); | |
1044 | req.n.nlmsg_type = type; | |
718f9b0f | 1045 | req.n.nlmsg_flags = NLM_F_ROOT | NLM_F_MATCH | NLM_F_REQUEST; |
d62a17ae | 1046 | req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg)); |
1047 | req.ifm.ifi_family = family; | |
1048 | ||
1049 | /* Include filter, if specified. */ | |
1050 | if (filter_mask) | |
312a6bee | 1051 | nl_attr_put32(&req.n, sizeof(req), IFLA_EXT_MASK, filter_mask); |
d62a17ae | 1052 | |
fd3f8e52 | 1053 | return netlink_request(netlink_cmd, &req); |
289602d7 | 1054 | } |
1055 | ||
62b4b7e4 PG |
1056 | enum netlink_msg_status |
1057 | netlink_put_gre_set_msg(struct nl_batch *bth, struct zebra_dplane_ctx *ctx) | |
1058 | { | |
1059 | enum dplane_op_e op; | |
1060 | enum netlink_msg_status ret; | |
1061 | ||
1062 | op = dplane_ctx_get_op(ctx); | |
1063 | assert(op == DPLANE_OP_GRE_SET); | |
1064 | ||
1065 | ret = netlink_batch_add_msg(bth, ctx, netlink_gre_set_msg_encoder, false); | |
1066 | ||
1067 | return ret; | |
1068 | } | |
1069 | ||
1fdc9eae | 1070 | /* Interface lookup by netlink socket. */ |
d62a17ae | 1071 | int interface_lookup_netlink(struct zebra_ns *zns) |
1fdc9eae | 1072 | { |
d62a17ae | 1073 | int ret; |
85a75f1e MS |
1074 | struct zebra_dplane_info dp_info; |
1075 | struct nlsock *netlink_cmd = &zns->netlink_cmd; | |
1076 | ||
1077 | /* Capture key info from ns struct */ | |
1078 | zebra_dplane_info_from_zns(&dp_info, zns, true /*is_cmd*/); | |
d62a17ae | 1079 | |
1080 | /* Get interface information. */ | |
85a75f1e | 1081 | ret = netlink_request_intf_addr(netlink_cmd, AF_PACKET, RTM_GETLINK, 0); |
d62a17ae | 1082 | if (ret < 0) |
1083 | return ret; | |
85a75f1e | 1084 | ret = netlink_parse_info(netlink_interface, netlink_cmd, &dp_info, 0, |
9bfadae8 | 1085 | true); |
d62a17ae | 1086 | if (ret < 0) |
1087 | return ret; | |
1088 | ||
1089 | /* Get interface information - for bridge interfaces. */ | |
85a75f1e | 1090 | ret = netlink_request_intf_addr(netlink_cmd, AF_BRIDGE, RTM_GETLINK, |
d62a17ae | 1091 | RTEXT_FILTER_BRVLAN); |
1092 | if (ret < 0) | |
1093 | return ret; | |
85a75f1e | 1094 | ret = netlink_parse_info(netlink_interface, netlink_cmd, &dp_info, 0, |
9bfadae8 | 1095 | true); |
d62a17ae | 1096 | if (ret < 0) |
1097 | return ret; | |
1098 | ||
520ebf72 | 1099 | /* fixup linkages */ |
357b150d | 1100 | zebra_if_update_all_links(zns); |
d2bec88a SW |
1101 | return 0; |
1102 | } | |
1103 | ||
1104 | /** | |
1105 | * interface_addr_lookup_netlink() - Look up interface addresses | |
1106 | * | |
1107 | * @zns: Zebra netlink socket | |
1108 | * Return: Result status | |
1109 | */ | |
1110 | static int interface_addr_lookup_netlink(struct zebra_ns *zns) | |
1111 | { | |
1112 | int ret; | |
1113 | struct zebra_dplane_info dp_info; | |
1114 | struct nlsock *netlink_cmd = &zns->netlink_cmd; | |
1115 | ||
1116 | /* Capture key info from ns struct */ | |
1117 | zebra_dplane_info_from_zns(&dp_info, zns, true /*is_cmd*/); | |
520ebf72 | 1118 | |
d62a17ae | 1119 | /* Get IPv4 address of the interfaces. */ |
85a75f1e | 1120 | ret = netlink_request_intf_addr(netlink_cmd, AF_INET, RTM_GETADDR, 0); |
d62a17ae | 1121 | if (ret < 0) |
1122 | return ret; | |
85a75f1e | 1123 | ret = netlink_parse_info(netlink_interface_addr, netlink_cmd, &dp_info, |
9bfadae8 | 1124 | 0, true); |
d62a17ae | 1125 | if (ret < 0) |
1126 | return ret; | |
1127 | ||
1128 | /* Get IPv6 address of the interfaces. */ | |
85a75f1e | 1129 | ret = netlink_request_intf_addr(netlink_cmd, AF_INET6, RTM_GETADDR, 0); |
d62a17ae | 1130 | if (ret < 0) |
1131 | return ret; | |
85a75f1e | 1132 | ret = netlink_parse_info(netlink_interface_addr, netlink_cmd, &dp_info, |
9bfadae8 | 1133 | 0, true); |
d62a17ae | 1134 | if (ret < 0) |
1135 | return ret; | |
1136 | ||
1137 | return 0; | |
1fdc9eae | 1138 | } |
1139 | ||
e0ae31b8 DS |
1140 | int kernel_interface_set_master(struct interface *master, |
1141 | struct interface *slave) | |
1142 | { | |
1143 | struct zebra_ns *zns = zebra_ns_lookup(NS_DEFAULT); | |
1144 | ||
1145 | struct { | |
1146 | struct nlmsghdr n; | |
1147 | struct ifinfomsg ifa; | |
1148 | char buf[NL_PKT_BUF_SIZE]; | |
1149 | } req; | |
1150 | ||
0d6f7fd6 | 1151 | memset(&req, 0, sizeof(req)); |
e0ae31b8 DS |
1152 | |
1153 | req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg)); | |
1154 | req.n.nlmsg_flags = NLM_F_REQUEST; | |
1155 | req.n.nlmsg_type = RTM_SETLINK; | |
1156 | req.n.nlmsg_pid = zns->netlink_cmd.snl.nl_pid; | |
1157 | ||
1158 | req.ifa.ifi_index = slave->ifindex; | |
1159 | ||
a757997c JU |
1160 | nl_attr_put32(&req.n, sizeof(req), IFLA_MASTER, master->ifindex); |
1161 | nl_attr_put32(&req.n, sizeof(req), IFLA_LINK, slave->ifindex); | |
e0ae31b8 DS |
1162 | |
1163 | return netlink_talk(netlink_talk_filter, &req.n, &zns->netlink_cmd, zns, | |
9bfadae8 | 1164 | false); |
e0ae31b8 DS |
1165 | } |
1166 | ||
1fdc9eae | 1167 | /* Interface address modification. */ |
67e3369e JU |
1168 | static ssize_t netlink_address_msg_encoder(struct zebra_dplane_ctx *ctx, |
1169 | void *buf, size_t buflen) | |
1fdc9eae | 1170 | { |
d62a17ae | 1171 | int bytelen; |
64168803 MS |
1172 | const struct prefix *p; |
1173 | int cmd; | |
1174 | const char *label; | |
1fdc9eae | 1175 | |
d62a17ae | 1176 | struct { |
1177 | struct nlmsghdr n; | |
1178 | struct ifaddrmsg ifa; | |
67e3369e JU |
1179 | char buf[0]; |
1180 | } *req = buf; | |
1181 | ||
1182 | if (buflen < sizeof(*req)) | |
1183 | return 0; | |
1fdc9eae | 1184 | |
64168803 | 1185 | p = dplane_ctx_get_intf_addr(ctx); |
67e3369e | 1186 | memset(req, 0, sizeof(*req)); |
1fdc9eae | 1187 | |
64168803 | 1188 | bytelen = (p->family == AF_INET ? 4 : 16); |
1fdc9eae | 1189 | |
67e3369e JU |
1190 | req->n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg)); |
1191 | req->n.nlmsg_flags = NLM_F_REQUEST; | |
a55ba23f | 1192 | |
64168803 MS |
1193 | if (dplane_ctx_get_op(ctx) == DPLANE_OP_ADDR_INSTALL) |
1194 | cmd = RTM_NEWADDR; | |
1195 | else | |
1196 | cmd = RTM_DELADDR; | |
1197 | ||
67e3369e JU |
1198 | req->n.nlmsg_type = cmd; |
1199 | req->ifa.ifa_family = p->family; | |
1fdc9eae | 1200 | |
67e3369e | 1201 | req->ifa.ifa_index = dplane_ctx_get_ifindex(ctx); |
1fdc9eae | 1202 | |
67e3369e JU |
1203 | if (!nl_attr_put(&req->n, buflen, IFA_LOCAL, &p->u.prefix, bytelen)) |
1204 | return 0; | |
1fdc9eae | 1205 | |
64168803 MS |
1206 | if (p->family == AF_INET) { |
1207 | if (dplane_ctx_intf_is_connected(ctx)) { | |
1208 | p = dplane_ctx_get_intf_dest(ctx); | |
67e3369e JU |
1209 | if (!nl_attr_put(&req->n, buflen, IFA_ADDRESS, |
1210 | &p->u.prefix, bytelen)) | |
1211 | return 0; | |
0f3af738 JW |
1212 | } else if (cmd == RTM_NEWADDR) { |
1213 | struct in_addr broad = { | |
1214 | .s_addr = ipv4_broadcast_addr(p->u.prefix4.s_addr, | |
1215 | p->prefixlen) | |
1216 | }; | |
67e3369e JU |
1217 | if (!nl_attr_put(&req->n, buflen, IFA_BROADCAST, &broad, |
1218 | bytelen)) | |
1219 | return 0; | |
d62a17ae | 1220 | } |
1221 | } | |
1fdc9eae | 1222 | |
64168803 | 1223 | /* p is now either address or destination/bcast addr */ |
67e3369e | 1224 | req->ifa.ifa_prefixlen = p->prefixlen; |
e8d19a05 | 1225 | |
64168803 | 1226 | if (dplane_ctx_intf_is_secondary(ctx)) |
67e3369e | 1227 | SET_FLAG(req->ifa.ifa_flags, IFA_F_SECONDARY); |
1fdc9eae | 1228 | |
64168803 MS |
1229 | if (dplane_ctx_intf_has_label(ctx)) { |
1230 | label = dplane_ctx_get_intf_label(ctx); | |
67e3369e JU |
1231 | if (!nl_attr_put(&req->n, buflen, IFA_LABEL, label, |
1232 | strlen(label) + 1)) | |
1233 | return 0; | |
64168803 | 1234 | } |
1fdc9eae | 1235 | |
67e3369e | 1236 | return NLMSG_ALIGN(req->n.nlmsg_len); |
e86b71f1 PG |
1237 | } |
1238 | ||
67e3369e JU |
1239 | enum netlink_msg_status |
1240 | netlink_put_address_update_msg(struct nl_batch *bth, | |
1241 | struct zebra_dplane_ctx *ctx) | |
1242 | { | |
1243 | return netlink_batch_add_msg(bth, ctx, netlink_address_msg_encoder, | |
1244 | false); | |
e86b71f1 PG |
1245 | } |
1246 | ||
2414abd3 | 1247 | int netlink_interface_addr(struct nlmsghdr *h, ns_id_t ns_id, int startup) |
1fdc9eae | 1248 | { |
d62a17ae | 1249 | int len; |
1250 | struct ifaddrmsg *ifa; | |
1251 | struct rtattr *tb[IFA_MAX + 1]; | |
1252 | struct interface *ifp; | |
1253 | void *addr; | |
1254 | void *broad; | |
d7c0a89a | 1255 | uint8_t flags = 0; |
d62a17ae | 1256 | char *label = NULL; |
1257 | struct zebra_ns *zns; | |
cde1af84 | 1258 | uint32_t metric = METRIC_MAX; |
9254efed | 1259 | uint32_t kernel_flags = 0; |
d62a17ae | 1260 | |
1261 | zns = zebra_ns_lookup(ns_id); | |
1262 | ifa = NLMSG_DATA(h); | |
1263 | ||
8a1b681c | 1264 | if (ifa->ifa_family != AF_INET && ifa->ifa_family != AF_INET6) { |
9df414fe | 1265 | flog_warn( |
e914ccbe | 1266 | EC_ZEBRA_UNKNOWN_FAMILY, |
87b5d1b0 DS |
1267 | "Invalid address family: %u received from kernel interface addr change: %s", |
1268 | ifa->ifa_family, nl_msg_type_to_str(h->nlmsg_type)); | |
d62a17ae | 1269 | return 0; |
8a1b681c | 1270 | } |
d62a17ae | 1271 | |
1272 | if (h->nlmsg_type != RTM_NEWADDR && h->nlmsg_type != RTM_DELADDR) | |
1273 | return 0; | |
1274 | ||
1275 | len = h->nlmsg_len - NLMSG_LENGTH(sizeof(struct ifaddrmsg)); | |
9bdf8618 | 1276 | if (len < 0) { |
15569c58 DA |
1277 | zlog_err( |
1278 | "%s: Message received from netlink is of a broken size: %d %zu", | |
1279 | __func__, h->nlmsg_len, | |
1280 | (size_t)NLMSG_LENGTH(sizeof(struct ifaddrmsg))); | |
d62a17ae | 1281 | return -1; |
9bdf8618 | 1282 | } |
d62a17ae | 1283 | |
d62a17ae | 1284 | netlink_parse_rtattr(tb, IFA_MAX, IFA_RTA(ifa), len); |
1285 | ||
1286 | ifp = if_lookup_by_index_per_ns(zns, ifa->ifa_index); | |
1287 | if (ifp == NULL) { | |
8eec31ef YY |
1288 | if (startup) { |
1289 | /* During startup, failure to lookup the referenced | |
1290 | * interface should not be an error, so we have | |
1291 | * downgraded this condition to warning, and we permit | |
1292 | * the startup interface state retrieval to continue. | |
1293 | */ | |
1294 | flog_warn(EC_LIB_INTERFACE, | |
1295 | "%s: can't find interface by index %d", | |
1296 | __func__, ifa->ifa_index); | |
1297 | return 0; | |
1298 | } else { | |
1299 | flog_err(EC_LIB_INTERFACE, | |
1300 | "%s: can't find interface by index %d", | |
1301 | __func__, ifa->ifa_index); | |
1302 | return -1; | |
1303 | } | |
d62a17ae | 1304 | } |
1305 | ||
9254efed DS |
1306 | /* Flags passed through */ |
1307 | if (tb[IFA_FLAGS]) | |
1308 | kernel_flags = *(int *)RTA_DATA(tb[IFA_FLAGS]); | |
1309 | else | |
1310 | kernel_flags = ifa->ifa_flags; | |
1311 | ||
d62a17ae | 1312 | if (IS_ZEBRA_DEBUG_KERNEL) /* remove this line to see initial ifcfg */ |
1313 | { | |
1314 | char buf[BUFSIZ]; | |
1315 | zlog_debug("netlink_interface_addr %s %s flags 0x%x:", | |
1316 | nl_msg_type_to_str(h->nlmsg_type), ifp->name, | |
9254efed | 1317 | kernel_flags); |
d62a17ae | 1318 | if (tb[IFA_LOCAL]) |
1319 | zlog_debug(" IFA_LOCAL %s/%d", | |
1320 | inet_ntop(ifa->ifa_family, | |
1321 | RTA_DATA(tb[IFA_LOCAL]), buf, | |
1322 | BUFSIZ), | |
1323 | ifa->ifa_prefixlen); | |
1324 | if (tb[IFA_ADDRESS]) | |
1325 | zlog_debug(" IFA_ADDRESS %s/%d", | |
1326 | inet_ntop(ifa->ifa_family, | |
1327 | RTA_DATA(tb[IFA_ADDRESS]), buf, | |
1328 | BUFSIZ), | |
1329 | ifa->ifa_prefixlen); | |
1330 | if (tb[IFA_BROADCAST]) | |
1331 | zlog_debug(" IFA_BROADCAST %s/%d", | |
1332 | inet_ntop(ifa->ifa_family, | |
1333 | RTA_DATA(tb[IFA_BROADCAST]), buf, | |
1334 | BUFSIZ), | |
1335 | ifa->ifa_prefixlen); | |
1336 | if (tb[IFA_LABEL] && strcmp(ifp->name, RTA_DATA(tb[IFA_LABEL]))) | |
1337 | zlog_debug(" IFA_LABEL %s", | |
1338 | (char *)RTA_DATA(tb[IFA_LABEL])); | |
1339 | ||
1340 | if (tb[IFA_CACHEINFO]) { | |
1341 | struct ifa_cacheinfo *ci = RTA_DATA(tb[IFA_CACHEINFO]); | |
1342 | zlog_debug(" IFA_CACHEINFO pref %d, valid %d", | |
1343 | ci->ifa_prefered, ci->ifa_valid); | |
1344 | } | |
1345 | } | |
1346 | ||
1347 | /* logic copied from iproute2/ip/ipaddress.c:print_addrinfo() */ | |
1348 | if (tb[IFA_LOCAL] == NULL) | |
1349 | tb[IFA_LOCAL] = tb[IFA_ADDRESS]; | |
1350 | if (tb[IFA_ADDRESS] == NULL) | |
1351 | tb[IFA_ADDRESS] = tb[IFA_LOCAL]; | |
1352 | ||
1353 | /* local interface address */ | |
1354 | addr = (tb[IFA_LOCAL] ? RTA_DATA(tb[IFA_LOCAL]) : NULL); | |
1355 | ||
1356 | /* is there a peer address? */ | |
1357 | if (tb[IFA_ADDRESS] | |
1358 | && memcmp(RTA_DATA(tb[IFA_ADDRESS]), RTA_DATA(tb[IFA_LOCAL]), | |
1359 | RTA_PAYLOAD(tb[IFA_ADDRESS]))) { | |
1360 | broad = RTA_DATA(tb[IFA_ADDRESS]); | |
1361 | SET_FLAG(flags, ZEBRA_IFA_PEER); | |
1362 | } else | |
1363 | /* seeking a broadcast address */ | |
1364 | broad = (tb[IFA_BROADCAST] ? RTA_DATA(tb[IFA_BROADCAST]) | |
1365 | : NULL); | |
1366 | ||
1367 | /* addr is primary key, SOL if we don't have one */ | |
1368 | if (addr == NULL) { | |
14a4d9d0 | 1369 | zlog_debug("%s: Local Interface Address is NULL for %s", |
1370 | __func__, ifp->name); | |
d62a17ae | 1371 | return -1; |
1372 | } | |
1373 | ||
1374 | /* Flags. */ | |
9254efed | 1375 | if (kernel_flags & IFA_F_SECONDARY) |
d62a17ae | 1376 | SET_FLAG(flags, ZEBRA_IFA_SECONDARY); |
1377 | ||
1378 | /* Label */ | |
1379 | if (tb[IFA_LABEL]) | |
1380 | label = (char *)RTA_DATA(tb[IFA_LABEL]); | |
1381 | ||
2e1cc436 | 1382 | if (label && strcmp(ifp->name, label) == 0) |
d62a17ae | 1383 | label = NULL; |
1384 | ||
cde1af84 AK |
1385 | if (tb[IFA_RT_PRIORITY]) |
1386 | metric = *(uint32_t *)RTA_DATA(tb[IFA_RT_PRIORITY]); | |
1387 | ||
d62a17ae | 1388 | /* Register interface address to the interface. */ |
1389 | if (ifa->ifa_family == AF_INET) { | |
930571d2 | 1390 | if (ifa->ifa_prefixlen > IPV4_MAX_BITLEN) { |
e17d9b2d | 1391 | zlog_err( |
87b5d1b0 DS |
1392 | "Invalid prefix length: %u received from kernel interface addr change: %s", |
1393 | ifa->ifa_prefixlen, | |
1394 | nl_msg_type_to_str(h->nlmsg_type)); | |
e17d9b2d | 1395 | return -1; |
930571d2 | 1396 | } |
20e879f9 | 1397 | |
d62a17ae | 1398 | if (h->nlmsg_type == RTM_NEWADDR) |
1399 | connected_add_ipv4(ifp, flags, (struct in_addr *)addr, | |
1400 | ifa->ifa_prefixlen, | |
cde1af84 AK |
1401 | (struct in_addr *)broad, label, |
1402 | metric); | |
20e879f9 MS |
1403 | else if (CHECK_FLAG(flags, ZEBRA_IFA_PEER)) { |
1404 | /* Delete with a peer address */ | |
1405 | connected_delete_ipv4( | |
1406 | ifp, flags, (struct in_addr *)addr, | |
1407 | ifa->ifa_prefixlen, broad); | |
1408 | } else | |
d62a17ae | 1409 | connected_delete_ipv4( |
1410 | ifp, flags, (struct in_addr *)addr, | |
fd267f08 | 1411 | ifa->ifa_prefixlen, NULL); |
d62a17ae | 1412 | } |
20e879f9 | 1413 | |
d62a17ae | 1414 | if (ifa->ifa_family == AF_INET6) { |
930571d2 | 1415 | if (ifa->ifa_prefixlen > IPV6_MAX_BITLEN) { |
e17d9b2d | 1416 | zlog_err( |
87b5d1b0 DS |
1417 | "Invalid prefix length: %u received from kernel interface addr change: %s", |
1418 | ifa->ifa_prefixlen, | |
1419 | nl_msg_type_to_str(h->nlmsg_type)); | |
e17d9b2d | 1420 | return -1; |
930571d2 | 1421 | } |
d62a17ae | 1422 | if (h->nlmsg_type == RTM_NEWADDR) { |
1423 | /* Only consider valid addresses; we'll not get a | |
1424 | * notification from | |
1425 | * the kernel till IPv6 DAD has completed, but at init | |
1426 | * time, Quagga | |
1427 | * does query for and will receive all addresses. | |
1428 | */ | |
9254efed | 1429 | if (!(kernel_flags |
d62a17ae | 1430 | & (IFA_F_DADFAILED | IFA_F_TENTATIVE))) |
60466a63 QY |
1431 | connected_add_ipv6(ifp, flags, |
1432 | (struct in6_addr *)addr, | |
60c0687a | 1433 | (struct in6_addr *)broad, |
cde1af84 AK |
1434 | ifa->ifa_prefixlen, label, |
1435 | metric); | |
d62a17ae | 1436 | } else |
1437 | connected_delete_ipv6(ifp, (struct in6_addr *)addr, | |
fd267f08 | 1438 | NULL, ifa->ifa_prefixlen); |
d62a17ae | 1439 | } |
1440 | ||
2a181147 SW |
1441 | /* |
1442 | * Linux kernel does not send route delete on interface down/addr del | |
1443 | * so we have to re-process routes it owns (i.e. kernel routes) | |
1444 | */ | |
1445 | if (h->nlmsg_type != RTM_NEWADDR) | |
1446 | rib_update(RIB_UPDATE_KERNEL); | |
1447 | ||
d62a17ae | 1448 | return 0; |
1fdc9eae | 1449 | } |
1450 | ||
e7c2c198 MS |
1451 | /* |
1452 | * Parse and validate an incoming interface address change message, | |
1453 | * generating a dplane context object. | |
1454 | * This runs in the dplane pthread; the context is enqueued to the | |
1455 | * main pthread for processing. | |
1456 | */ | |
1457 | int netlink_interface_addr_dplane(struct nlmsghdr *h, ns_id_t ns_id, | |
1458 | int startup /*ignored*/) | |
1459 | { | |
1460 | int len; | |
1461 | struct ifaddrmsg *ifa; | |
1462 | struct rtattr *tb[IFA_MAX + 1]; | |
1463 | void *addr; | |
1464 | void *broad; | |
1465 | char *label = NULL; | |
1466 | uint32_t metric = METRIC_MAX; | |
1467 | uint32_t kernel_flags = 0; | |
1468 | struct zebra_dplane_ctx *ctx; | |
1469 | struct prefix p; | |
1470 | ||
1471 | ifa = NLMSG_DATA(h); | |
1472 | ||
1473 | /* Validate message types */ | |
1474 | if (h->nlmsg_type != RTM_NEWADDR && h->nlmsg_type != RTM_DELADDR) | |
1475 | return 0; | |
1476 | ||
1477 | if (ifa->ifa_family != AF_INET && ifa->ifa_family != AF_INET6) { | |
1478 | if (IS_ZEBRA_DEBUG_KERNEL) | |
1479 | zlog_debug("%s: %s: Invalid address family: %u", | |
1480 | __func__, nl_msg_type_to_str(h->nlmsg_type), | |
1481 | ifa->ifa_family); | |
1482 | return 0; | |
1483 | } | |
1484 | ||
1485 | len = h->nlmsg_len - NLMSG_LENGTH(sizeof(struct ifaddrmsg)); | |
1486 | if (len < 0) { | |
1487 | if (IS_ZEBRA_DEBUG_KERNEL) | |
1488 | zlog_debug("%s: %s: netlink msg bad size: %d %zu", | |
1489 | __func__, nl_msg_type_to_str(h->nlmsg_type), | |
1490 | h->nlmsg_len, | |
1491 | (size_t)NLMSG_LENGTH( | |
1492 | sizeof(struct ifaddrmsg))); | |
1493 | return -1; | |
1494 | } | |
1495 | ||
1496 | netlink_parse_rtattr(tb, IFA_MAX, IFA_RTA(ifa), len); | |
1497 | ||
1498 | /* Flags passed through */ | |
1499 | if (tb[IFA_FLAGS]) | |
1500 | kernel_flags = *(int *)RTA_DATA(tb[IFA_FLAGS]); | |
1501 | else | |
1502 | kernel_flags = ifa->ifa_flags; | |
1503 | ||
1504 | if (IS_ZEBRA_DEBUG_KERNEL) { /* remove this line to see initial ifcfg */ | |
1505 | char buf[PREFIX_STRLEN]; | |
1506 | ||
1507 | zlog_debug("%s: %s nsid %u ifindex %u flags 0x%x:", __func__, | |
1508 | nl_msg_type_to_str(h->nlmsg_type), ns_id, | |
1509 | ifa->ifa_index, kernel_flags); | |
1510 | if (tb[IFA_LOCAL]) | |
1511 | zlog_debug(" IFA_LOCAL %s/%d", | |
1512 | inet_ntop(ifa->ifa_family, | |
1513 | RTA_DATA(tb[IFA_LOCAL]), buf, | |
1514 | sizeof(buf)), | |
1515 | ifa->ifa_prefixlen); | |
1516 | if (tb[IFA_ADDRESS]) | |
1517 | zlog_debug(" IFA_ADDRESS %s/%d", | |
1518 | inet_ntop(ifa->ifa_family, | |
1519 | RTA_DATA(tb[IFA_ADDRESS]), buf, | |
1520 | sizeof(buf)), | |
1521 | ifa->ifa_prefixlen); | |
1522 | if (tb[IFA_BROADCAST]) | |
1523 | zlog_debug(" IFA_BROADCAST %s/%d", | |
1524 | inet_ntop(ifa->ifa_family, | |
1525 | RTA_DATA(tb[IFA_BROADCAST]), buf, | |
1526 | sizeof(buf)), | |
1527 | ifa->ifa_prefixlen); | |
1528 | if (tb[IFA_LABEL]) | |
1529 | zlog_debug(" IFA_LABEL %s", | |
1530 | (const char *)RTA_DATA(tb[IFA_LABEL])); | |
1531 | ||
1532 | if (tb[IFA_CACHEINFO]) { | |
1533 | struct ifa_cacheinfo *ci = RTA_DATA(tb[IFA_CACHEINFO]); | |
1534 | ||
1535 | zlog_debug(" IFA_CACHEINFO pref %d, valid %d", | |
1536 | ci->ifa_prefered, ci->ifa_valid); | |
1537 | } | |
1538 | } | |
1539 | ||
1540 | /* Validate prefix length */ | |
1541 | ||
1542 | if (ifa->ifa_family == AF_INET | |
1543 | && ifa->ifa_prefixlen > IPV4_MAX_BITLEN) { | |
1544 | if (IS_ZEBRA_DEBUG_KERNEL) | |
1545 | zlog_debug("%s: %s: Invalid prefix length: %u", | |
1546 | __func__, nl_msg_type_to_str(h->nlmsg_type), | |
1547 | ifa->ifa_prefixlen); | |
1548 | return -1; | |
1549 | } | |
1550 | ||
1551 | if (ifa->ifa_family == AF_INET6) { | |
1552 | if (ifa->ifa_prefixlen > IPV6_MAX_BITLEN) { | |
1553 | if (IS_ZEBRA_DEBUG_KERNEL) | |
1554 | zlog_debug("%s: %s: Invalid prefix length: %u", | |
1555 | __func__, | |
1556 | nl_msg_type_to_str(h->nlmsg_type), | |
1557 | ifa->ifa_prefixlen); | |
1558 | return -1; | |
1559 | } | |
1560 | ||
1561 | /* Only consider valid addresses; we'll not get a kernel | |
1562 | * notification till IPv6 DAD has completed, but at init | |
1563 | * time, FRR does query for and will receive all addresses. | |
1564 | */ | |
1565 | if (h->nlmsg_type == RTM_NEWADDR | |
1566 | && (kernel_flags & (IFA_F_DADFAILED | IFA_F_TENTATIVE))) { | |
1567 | if (IS_ZEBRA_DEBUG_KERNEL) | |
1568 | zlog_debug("%s: %s: Invalid/tentative addr", | |
1569 | __func__, | |
1570 | nl_msg_type_to_str(h->nlmsg_type)); | |
1571 | return 0; | |
1572 | } | |
1573 | } | |
1574 | ||
1575 | /* logic copied from iproute2/ip/ipaddress.c:print_addrinfo() */ | |
1576 | if (tb[IFA_LOCAL] == NULL) | |
1577 | tb[IFA_LOCAL] = tb[IFA_ADDRESS]; | |
1578 | if (tb[IFA_ADDRESS] == NULL) | |
1579 | tb[IFA_ADDRESS] = tb[IFA_LOCAL]; | |
1580 | ||
1581 | /* local interface address */ | |
1582 | addr = (tb[IFA_LOCAL] ? RTA_DATA(tb[IFA_LOCAL]) : NULL); | |
1583 | ||
1584 | /* addr is primary key, SOL if we don't have one */ | |
1585 | if (addr == NULL) { | |
1586 | if (IS_ZEBRA_DEBUG_KERNEL) | |
1587 | zlog_debug("%s: %s: No local interface address", | |
1588 | __func__, nl_msg_type_to_str(h->nlmsg_type)); | |
1589 | return -1; | |
1590 | } | |
1591 | ||
1592 | /* Allocate a context object, now that validation is done. */ | |
1593 | ctx = dplane_ctx_alloc(); | |
1594 | if (h->nlmsg_type == RTM_NEWADDR) | |
1595 | dplane_ctx_set_op(ctx, DPLANE_OP_INTF_ADDR_ADD); | |
1596 | else | |
1597 | dplane_ctx_set_op(ctx, DPLANE_OP_INTF_ADDR_DEL); | |
1598 | ||
1599 | dplane_ctx_set_ifindex(ctx, ifa->ifa_index); | |
1600 | dplane_ctx_set_ns_id(ctx, ns_id); | |
1601 | ||
1602 | /* Convert addr to prefix */ | |
1603 | memset(&p, 0, sizeof(p)); | |
1604 | p.family = ifa->ifa_family; | |
1605 | p.prefixlen = ifa->ifa_prefixlen; | |
1606 | if (p.family == AF_INET) | |
1607 | p.u.prefix4 = *(struct in_addr *)addr; | |
1608 | else | |
1609 | p.u.prefix6 = *(struct in6_addr *)addr; | |
1610 | ||
1611 | dplane_ctx_set_intf_addr(ctx, &p); | |
1612 | ||
1613 | /* is there a peer address? */ | |
1614 | if (tb[IFA_ADDRESS] | |
1615 | && memcmp(RTA_DATA(tb[IFA_ADDRESS]), RTA_DATA(tb[IFA_LOCAL]), | |
1616 | RTA_PAYLOAD(tb[IFA_ADDRESS]))) { | |
1617 | broad = RTA_DATA(tb[IFA_ADDRESS]); | |
1618 | dplane_ctx_intf_set_connected(ctx); | |
1619 | } else if (tb[IFA_BROADCAST]) { | |
1620 | /* seeking a broadcast address */ | |
1621 | broad = RTA_DATA(tb[IFA_BROADCAST]); | |
1622 | dplane_ctx_intf_set_broadcast(ctx); | |
1623 | } else | |
1624 | broad = NULL; | |
1625 | ||
1626 | if (broad) { | |
1627 | /* Convert addr to prefix */ | |
1628 | memset(&p, 0, sizeof(p)); | |
1629 | p.family = ifa->ifa_family; | |
1630 | p.prefixlen = ifa->ifa_prefixlen; | |
1631 | if (p.family == AF_INET) | |
1632 | p.u.prefix4 = *(struct in_addr *)broad; | |
1633 | else | |
1634 | p.u.prefix6 = *(struct in6_addr *)broad; | |
1635 | ||
1636 | dplane_ctx_set_intf_dest(ctx, &p); | |
1637 | } | |
1638 | ||
1639 | /* Flags. */ | |
1640 | if (kernel_flags & IFA_F_SECONDARY) | |
1641 | dplane_ctx_intf_set_secondary(ctx); | |
1642 | ||
1643 | /* Label */ | |
1644 | if (tb[IFA_LABEL]) { | |
1645 | label = (char *)RTA_DATA(tb[IFA_LABEL]); | |
1646 | dplane_ctx_set_intf_label(ctx, label); | |
1647 | } | |
1648 | ||
1649 | if (tb[IFA_RT_PRIORITY]) | |
1650 | metric = *(uint32_t *)RTA_DATA(tb[IFA_RT_PRIORITY]); | |
1651 | ||
1652 | dplane_ctx_set_intf_metric(ctx, metric); | |
1653 | ||
1654 | /* Enqueue ctx for main pthread to process */ | |
1655 | dplane_provider_enqueue_to_zebra(ctx); | |
1656 | ||
1657 | return 0; | |
1658 | } | |
1659 | ||
2414abd3 | 1660 | int netlink_link_change(struct nlmsghdr *h, ns_id_t ns_id, int startup) |
1fdc9eae | 1661 | { |
d62a17ae | 1662 | int len; |
1663 | struct ifinfomsg *ifi; | |
1664 | struct rtattr *tb[IFLA_MAX + 1]; | |
1665 | struct rtattr *linkinfo[IFLA_MAX + 1]; | |
1666 | struct interface *ifp; | |
1667 | char *name = NULL; | |
1668 | char *kind = NULL; | |
48884c6b | 1669 | char *desc = NULL; |
d62a17ae | 1670 | char *slave_kind = NULL; |
1671 | struct zebra_ns *zns; | |
1672 | vrf_id_t vrf_id = VRF_DEFAULT; | |
e6f2bec0 | 1673 | enum zebra_iftype zif_type = ZEBRA_IF_OTHER; |
a81982fa | 1674 | enum zebra_slave_iftype zif_slave_type = ZEBRA_IF_SLAVE_NONE; |
d62a17ae | 1675 | ifindex_t bridge_ifindex = IFINDEX_INTERNAL; |
b9368db9 | 1676 | ifindex_t bond_ifindex = IFINDEX_INTERNAL; |
d62a17ae | 1677 | ifindex_t link_ifindex = IFINDEX_INTERNAL; |
97c4e1d0 | 1678 | uint8_t old_hw_addr[INTERFACE_HWADDR_MAX]; |
ba5165ec | 1679 | struct zebra_if *zif; |
14ddb3d9 | 1680 | ns_id_t link_nsid = ns_id; |
c36e442c | 1681 | ifindex_t master_infindex = IFINDEX_INTERNAL; |
00a7710c | 1682 | uint8_t bypass = 0; |
d62a17ae | 1683 | |
1684 | zns = zebra_ns_lookup(ns_id); | |
1685 | ifi = NLMSG_DATA(h); | |
1686 | ||
fe533c56 | 1687 | /* assume if not default zns, then new VRF */ |
d62a17ae | 1688 | if (!(h->nlmsg_type == RTM_NEWLINK || h->nlmsg_type == RTM_DELLINK)) { |
1689 | /* If this is not link add/delete message so print warning. */ | |
87b5d1b0 DS |
1690 | zlog_debug("netlink_link_change: wrong kernel message %s", |
1691 | nl_msg_type_to_str(h->nlmsg_type)); | |
d62a17ae | 1692 | return 0; |
1693 | } | |
1694 | ||
8a1b681c SW |
1695 | if (!(ifi->ifi_family == AF_UNSPEC || ifi->ifi_family == AF_BRIDGE |
1696 | || ifi->ifi_family == AF_INET6)) { | |
9df414fe | 1697 | flog_warn( |
e914ccbe | 1698 | EC_ZEBRA_UNKNOWN_FAMILY, |
87b5d1b0 DS |
1699 | "Invalid address family: %u received from kernel link change: %s", |
1700 | ifi->ifi_family, nl_msg_type_to_str(h->nlmsg_type)); | |
8a1b681c SW |
1701 | return 0; |
1702 | } | |
1703 | ||
d62a17ae | 1704 | len = h->nlmsg_len - NLMSG_LENGTH(sizeof(struct ifinfomsg)); |
9bdf8618 | 1705 | if (len < 0) { |
15569c58 DA |
1706 | zlog_err( |
1707 | "%s: Message received from netlink is of a broken size %d %zu", | |
1708 | __func__, h->nlmsg_len, | |
1709 | (size_t)NLMSG_LENGTH(sizeof(struct ifinfomsg))); | |
d62a17ae | 1710 | return -1; |
9bdf8618 | 1711 | } |
d62a17ae | 1712 | |
1713 | /* We are interested in some AF_BRIDGE notifications. */ | |
1714 | if (ifi->ifi_family == AF_BRIDGE) | |
1715 | return netlink_bridge_interface(h, len, ns_id, startup); | |
1716 | ||
1717 | /* Looking up interface name. */ | |
0d6f7fd6 | 1718 | memset(linkinfo, 0, sizeof(linkinfo)); |
d62a17ae | 1719 | netlink_parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), len); |
1fdc9eae | 1720 | |
d62a17ae | 1721 | /* check for wireless messages to ignore */ |
1722 | if ((tb[IFLA_WIRELESS] != NULL) && (ifi->ifi_change == 0)) { | |
1723 | if (IS_ZEBRA_DEBUG_KERNEL) | |
1724 | zlog_debug("%s: ignoring IFLA_WIRELESS message", | |
1725 | __func__); | |
1726 | return 0; | |
1727 | } | |
1fdc9eae | 1728 | |
d62a17ae | 1729 | if (tb[IFLA_IFNAME] == NULL) |
1730 | return -1; | |
1731 | name = (char *)RTA_DATA(tb[IFLA_IFNAME]); | |
1fdc9eae | 1732 | |
e9f79fff MS |
1733 | /* Must be valid string. */ |
1734 | len = RTA_PAYLOAD(tb[IFLA_IFNAME]); | |
1735 | if (len < 2 || name[len - 1] != '\0') { | |
1736 | if (IS_ZEBRA_DEBUG_KERNEL) | |
1737 | zlog_debug("%s: invalid intf name", __func__); | |
1738 | return -1; | |
1739 | } | |
1740 | ||
d62a17ae | 1741 | if (tb[IFLA_LINKINFO]) { |
c9d842c7 DS |
1742 | netlink_parse_rtattr_nested(linkinfo, IFLA_INFO_MAX, |
1743 | tb[IFLA_LINKINFO]); | |
1fdc9eae | 1744 | |
d62a17ae | 1745 | if (linkinfo[IFLA_INFO_KIND]) |
1746 | kind = RTA_DATA(linkinfo[IFLA_INFO_KIND]); | |
1fdc9eae | 1747 | |
d62a17ae | 1748 | if (linkinfo[IFLA_INFO_SLAVE_KIND]) |
1749 | slave_kind = RTA_DATA(linkinfo[IFLA_INFO_SLAVE_KIND]); | |
1fdc9eae | 1750 | |
d62a17ae | 1751 | netlink_determine_zebra_iftype(kind, &zif_type); |
1752 | } | |
6675513d | 1753 | |
d62a17ae | 1754 | /* If linking to another interface, note it. */ |
1755 | if (tb[IFLA_LINK]) | |
1756 | link_ifindex = *(ifindex_t *)RTA_DATA(tb[IFLA_LINK]); | |
1fdc9eae | 1757 | |
b1cc23b2 | 1758 | if (tb[IFLA_LINK_NETNSID]) { |
14ddb3d9 | 1759 | link_nsid = *(ns_id_t *)RTA_DATA(tb[IFLA_LINK_NETNSID]); |
b1cc23b2 PG |
1760 | link_nsid = ns_id_get_absolute(ns_id, link_nsid); |
1761 | } | |
48884c6b DS |
1762 | if (tb[IFLA_IFALIAS]) { |
1763 | desc = (char *)RTA_DATA(tb[IFLA_IFALIAS]); | |
1764 | } | |
1765 | ||
d62a17ae | 1766 | /* See if interface is present. */ |
1767 | ifp = if_lookup_by_name_per_ns(zns, name); | |
1768 | ||
1769 | if (h->nlmsg_type == RTM_NEWLINK) { | |
e4c5b3ba IR |
1770 | /* If VRF, create or update the VRF structure itself. */ |
1771 | if (zif_type == ZEBRA_IF_VRF && !vrf_is_backend_netns()) { | |
1772 | netlink_vrf_change(h, tb[IFLA_LINKINFO], ns_id, name); | |
1773 | vrf_id = (vrf_id_t)ifi->ifi_index; | |
1774 | } | |
1775 | ||
d62a17ae | 1776 | if (tb[IFLA_MASTER]) { |
78dd30b2 PG |
1777 | if (slave_kind && (strcmp(slave_kind, "vrf") == 0) |
1778 | && !vrf_is_backend_netns()) { | |
d62a17ae | 1779 | zif_slave_type = ZEBRA_IF_SLAVE_VRF; |
c36e442c AK |
1780 | master_infindex = vrf_id = |
1781 | *(uint32_t *)RTA_DATA(tb[IFLA_MASTER]); | |
d62a17ae | 1782 | } else if (slave_kind |
1783 | && (strcmp(slave_kind, "bridge") == 0)) { | |
1784 | zif_slave_type = ZEBRA_IF_SLAVE_BRIDGE; | |
c36e442c | 1785 | master_infindex = bridge_ifindex = |
d62a17ae | 1786 | *(ifindex_t *)RTA_DATA(tb[IFLA_MASTER]); |
b9368db9 DD |
1787 | } else if (slave_kind |
1788 | && (strcmp(slave_kind, "bond") == 0)) { | |
1789 | zif_slave_type = ZEBRA_IF_SLAVE_BOND; | |
c36e442c | 1790 | master_infindex = bond_ifindex = |
b9368db9 | 1791 | *(ifindex_t *)RTA_DATA(tb[IFLA_MASTER]); |
00a7710c | 1792 | bypass = netlink_parse_lacp_bypass(linkinfo); |
d62a17ae | 1793 | } else |
1794 | zif_slave_type = ZEBRA_IF_SLAVE_OTHER; | |
1795 | } | |
fe533c56 PG |
1796 | if (vrf_is_backend_netns()) |
1797 | vrf_id = (vrf_id_t)ns_id; | |
d62a17ae | 1798 | if (ifp == NULL |
1799 | || !CHECK_FLAG(ifp->status, ZEBRA_INTERFACE_ACTIVE)) { | |
1800 | /* Add interface notification from kernel */ | |
1801 | if (IS_ZEBRA_DEBUG_KERNEL) | |
1802 | zlog_debug( | |
3efd0893 | 1803 | "RTM_NEWLINK ADD for %s(%u) vrf_id %u type %d sl_type %d master %u flags 0x%x", |
d62a17ae | 1804 | name, ifi->ifi_index, vrf_id, zif_type, |
c36e442c | 1805 | zif_slave_type, master_infindex, |
d62a17ae | 1806 | ifi->ifi_flags); |
1807 | ||
1808 | if (ifp == NULL) { | |
1809 | /* unknown interface */ | |
f60a1188 | 1810 | ifp = if_get_by_name(name, vrf_id, NULL); |
d62a17ae | 1811 | } else { |
1812 | /* pre-configured interface, learnt now */ | |
096f7609 | 1813 | if (ifp->vrf->vrf_id != vrf_id) |
a36898e7 | 1814 | if_update_to_new_vrf(ifp, vrf_id); |
d62a17ae | 1815 | } |
1816 | ||
1817 | /* Update interface information. */ | |
1818 | set_ifindex(ifp, ifi->ifi_index, zns); | |
1819 | ifp->flags = ifi->ifi_flags & 0x0000fffff; | |
d23b983b | 1820 | if (!tb[IFLA_MTU]) { |
9df414fe | 1821 | zlog_debug( |
d23b983b SW |
1822 | "RTM_NEWLINK for interface %s(%u) without MTU set", |
1823 | name, ifi->ifi_index); | |
1824 | return 0; | |
1825 | } | |
d62a17ae | 1826 | ifp->mtu6 = ifp->mtu = *(int *)RTA_DATA(tb[IFLA_MTU]); |
1827 | ifp->metric = 0; | |
1828 | ifp->ptm_status = ZEBRA_PTM_STATUS_UNKNOWN; | |
1829 | ||
1830 | /* Set interface type */ | |
1831 | zebra_if_set_ziftype(ifp, zif_type, zif_slave_type); | |
b0fa6f6a CS |
1832 | if (IS_ZEBRA_IF_VRF(ifp)) |
1833 | SET_FLAG(ifp->status, | |
1834 | ZEBRA_INTERFACE_VRF_LOOPBACK); | |
d62a17ae | 1835 | |
1836 | /* Update link. */ | |
680c278f | 1837 | zebra_if_update_link(ifp, link_ifindex, ns_id); |
d62a17ae | 1838 | |
8975bbbd IR |
1839 | ifp->ll_type = |
1840 | netlink_to_zebra_link_type(ifi->ifi_type); | |
d62a17ae | 1841 | netlink_interface_update_hw_addr(tb, ifp); |
1842 | ||
1843 | /* Inform clients, install any configured addresses. */ | |
1844 | if_add_update(ifp); | |
1845 | ||
1846 | /* Extract and save L2 interface information, take | |
1847 | * additional actions. */ | |
1848 | netlink_interface_update_l2info( | |
14ddb3d9 PG |
1849 | ifp, linkinfo[IFLA_INFO_DATA], |
1850 | 1, link_nsid); | |
d62a17ae | 1851 | if (IS_ZEBRA_IF_BRIDGE_SLAVE(ifp)) |
c7620108 PG |
1852 | zebra_l2if_update_bridge_slave( |
1853 | ifp, bridge_ifindex, ns_id, | |
1854 | ZEBRA_BRIDGE_NO_ACTION); | |
b9368db9 | 1855 | else if (IS_ZEBRA_IF_BOND_SLAVE(ifp)) |
00a7710c AK |
1856 | zebra_l2if_update_bond_slave(ifp, bond_ifindex, |
1857 | !!bypass); | |
c36e442c AK |
1858 | |
1859 | if (tb[IFLA_PROTO_DOWN]) { | |
1860 | uint8_t protodown; | |
1861 | ||
1862 | protodown = *(uint8_t *)RTA_DATA( | |
1863 | tb[IFLA_PROTO_DOWN]); | |
1864 | netlink_proc_dplane_if_protodown(ifp->info, | |
1865 | !!protodown); | |
1866 | } | |
096f7609 | 1867 | } else if (ifp->vrf->vrf_id != vrf_id) { |
d62a17ae | 1868 | /* VRF change for an interface. */ |
1869 | if (IS_ZEBRA_DEBUG_KERNEL) | |
1870 | zlog_debug( | |
3efd0893 | 1871 | "RTM_NEWLINK vrf-change for %s(%u) vrf_id %u -> %u flags 0x%x", |
096f7609 IR |
1872 | name, ifp->ifindex, ifp->vrf->vrf_id, |
1873 | vrf_id, ifi->ifi_flags); | |
d62a17ae | 1874 | |
a36898e7 | 1875 | if_handle_vrf_change(ifp, vrf_id); |
d62a17ae | 1876 | } else { |
b9368db9 | 1877 | bool was_bridge_slave, was_bond_slave; |
c7620108 | 1878 | uint8_t chgflags = ZEBRA_BRIDGE_NO_ACTION; |
2d04bd98 | 1879 | zif = ifp->info; |
d62a17ae | 1880 | |
1881 | /* Interface update. */ | |
1882 | if (IS_ZEBRA_DEBUG_KERNEL) | |
1883 | zlog_debug( | |
3efd0893 | 1884 | "RTM_NEWLINK update for %s(%u) sl_type %d master %u flags 0x%x", |
d62a17ae | 1885 | name, ifp->ifindex, zif_slave_type, |
c36e442c | 1886 | master_infindex, ifi->ifi_flags); |
d62a17ae | 1887 | |
1888 | set_ifindex(ifp, ifi->ifi_index, zns); | |
d23b983b | 1889 | if (!tb[IFLA_MTU]) { |
9df414fe | 1890 | zlog_debug( |
d23b983b SW |
1891 | "RTM_NEWLINK for interface %s(%u) without MTU set", |
1892 | name, ifi->ifi_index); | |
1893 | return 0; | |
1894 | } | |
d62a17ae | 1895 | ifp->mtu6 = ifp->mtu = *(int *)RTA_DATA(tb[IFLA_MTU]); |
1896 | ifp->metric = 0; | |
1897 | ||
1898 | /* Update interface type - NOTE: Only slave_type can | |
1899 | * change. */ | |
1900 | was_bridge_slave = IS_ZEBRA_IF_BRIDGE_SLAVE(ifp); | |
b9368db9 | 1901 | was_bond_slave = IS_ZEBRA_IF_BOND_SLAVE(ifp); |
d62a17ae | 1902 | zebra_if_set_ziftype(ifp, zif_type, zif_slave_type); |
1903 | ||
97c4e1d0 CS |
1904 | memcpy(old_hw_addr, ifp->hw_addr, INTERFACE_HWADDR_MAX); |
1905 | ||
ecffe916 PG |
1906 | /* Update link. */ |
1907 | zebra_if_update_link(ifp, link_ifindex, ns_id); | |
1908 | ||
8975bbbd IR |
1909 | ifp->ll_type = |
1910 | netlink_to_zebra_link_type(ifi->ifi_type); | |
d62a17ae | 1911 | netlink_interface_update_hw_addr(tb, ifp); |
1912 | ||
2d04bd98 CS |
1913 | if (tb[IFLA_PROTO_DOWN]) { |
1914 | uint8_t protodown; | |
1915 | ||
1916 | protodown = *(uint8_t *)RTA_DATA( | |
1917 | tb[IFLA_PROTO_DOWN]); | |
1918 | netlink_proc_dplane_if_protodown(zif, | |
1919 | !!protodown); | |
1920 | } | |
1921 | ||
d62a17ae | 1922 | if (if_is_no_ptm_operative(ifp)) { |
1923 | ifp->flags = ifi->ifi_flags & 0x0000fffff; | |
2d04bd98 CS |
1924 | if (!if_is_no_ptm_operative(ifp) || |
1925 | CHECK_FLAG(zif->flags, | |
1926 | ZIF_FLAG_PROTODOWN)) { | |
d62a17ae | 1927 | if (IS_ZEBRA_DEBUG_KERNEL) |
1928 | zlog_debug( | |
1929 | "Intf %s(%u) has gone DOWN", | |
1930 | name, ifp->ifindex); | |
1931 | if_down(ifp); | |
2a181147 | 1932 | rib_update(RIB_UPDATE_KERNEL); |
d62a17ae | 1933 | } else if (if_is_operative(ifp)) { |
c7620108 PG |
1934 | bool mac_updated = false; |
1935 | ||
d62a17ae | 1936 | /* Must notify client daemons of new |
1937 | * interface status. */ | |
1938 | if (IS_ZEBRA_DEBUG_KERNEL) | |
1939 | zlog_debug( | |
1940 | "Intf %s(%u) PTM up, notifying clients", | |
1941 | name, ifp->ifindex); | |
d78fa571 | 1942 | if_up(ifp); |
97c4e1d0 CS |
1943 | |
1944 | /* Update EVPN VNI when SVI MAC change | |
1945 | */ | |
c7620108 PG |
1946 | if (memcmp(old_hw_addr, ifp->hw_addr, |
1947 | INTERFACE_HWADDR_MAX)) | |
1948 | mac_updated = true; | |
1949 | if (IS_ZEBRA_IF_VLAN(ifp) | |
1950 | && mac_updated) { | |
97c4e1d0 CS |
1951 | struct interface *link_if; |
1952 | ||
1953 | link_if = | |
1954 | if_lookup_by_index_per_ns( | |
1955 | zebra_ns_lookup(NS_DEFAULT), | |
1956 | link_ifindex); | |
1957 | if (link_if) | |
1958 | zebra_vxlan_svi_up(ifp, | |
1959 | link_if); | |
c7620108 PG |
1960 | } else if (mac_updated |
1961 | && IS_ZEBRA_IF_BRIDGE(ifp)) { | |
1962 | zlog_debug( | |
1963 | "Intf %s(%u) bridge changed MAC address", | |
1964 | name, ifp->ifindex); | |
1965 | chgflags = | |
1966 | ZEBRA_BRIDGE_MASTER_MAC_CHANGE; | |
97c4e1d0 | 1967 | } |
d62a17ae | 1968 | } |
1969 | } else { | |
1970 | ifp->flags = ifi->ifi_flags & 0x0000fffff; | |
2d04bd98 CS |
1971 | if (if_is_operative(ifp) && |
1972 | !CHECK_FLAG(zif->flags, | |
1973 | ZIF_FLAG_PROTODOWN)) { | |
d62a17ae | 1974 | if (IS_ZEBRA_DEBUG_KERNEL) |
1975 | zlog_debug( | |
1976 | "Intf %s(%u) has come UP", | |
1977 | name, ifp->ifindex); | |
1978 | if_up(ifp); | |
f56a15b5 PG |
1979 | if (IS_ZEBRA_IF_BRIDGE(ifp)) |
1980 | chgflags = | |
1981 | ZEBRA_BRIDGE_MASTER_UP; | |
6f908ded QY |
1982 | } else { |
1983 | if (IS_ZEBRA_DEBUG_KERNEL) | |
1984 | zlog_debug( | |
7913714e | 1985 | "Intf %s(%u) has gone DOWN", |
6f908ded QY |
1986 | name, ifp->ifindex); |
1987 | if_down(ifp); | |
2a181147 | 1988 | rib_update(RIB_UPDATE_KERNEL); |
d62a17ae | 1989 | } |
1990 | } | |
1991 | ||
1992 | /* Extract and save L2 interface information, take | |
1993 | * additional actions. */ | |
1994 | netlink_interface_update_l2info( | |
14ddb3d9 PG |
1995 | ifp, linkinfo[IFLA_INFO_DATA], |
1996 | 0, link_nsid); | |
c7620108 PG |
1997 | if (IS_ZEBRA_IF_BRIDGE(ifp)) |
1998 | zebra_l2if_update_bridge(ifp, chgflags); | |
c36e442c AK |
1999 | if (IS_ZEBRA_IF_BOND(ifp)) |
2000 | zebra_l2if_update_bond(ifp, true); | |
d62a17ae | 2001 | if (IS_ZEBRA_IF_BRIDGE_SLAVE(ifp) || was_bridge_slave) |
c7620108 PG |
2002 | zebra_l2if_update_bridge_slave( |
2003 | ifp, bridge_ifindex, ns_id, chgflags); | |
b9368db9 | 2004 | else if (IS_ZEBRA_IF_BOND_SLAVE(ifp) || was_bond_slave) |
00a7710c AK |
2005 | zebra_l2if_update_bond_slave(ifp, bond_ifindex, |
2006 | !!bypass); | |
d62a17ae | 2007 | } |
26f13577 DS |
2008 | |
2009 | zif = ifp->info; | |
2010 | if (zif) { | |
2011 | XFREE(MTYPE_TMP, zif->desc); | |
2012 | if (desc) | |
2013 | zif->desc = XSTRDUP(MTYPE_TMP, desc); | |
2014 | } | |
d62a17ae | 2015 | } else { |
2016 | /* Delete interface notification from kernel */ | |
2017 | if (ifp == NULL) { | |
9df414fe QY |
2018 | if (IS_ZEBRA_DEBUG_KERNEL) |
2019 | zlog_debug( | |
2020 | "RTM_DELLINK for unknown interface %s(%u)", | |
2021 | name, ifi->ifi_index); | |
d62a17ae | 2022 | return 0; |
2023 | } | |
2024 | ||
2025 | if (IS_ZEBRA_DEBUG_KERNEL) | |
2026 | zlog_debug("RTM_DELLINK for %s(%u)", name, | |
2027 | ifp->ifindex); | |
2028 | ||
2029 | UNSET_FLAG(ifp->status, ZEBRA_INTERFACE_VRF_LOOPBACK); | |
2030 | ||
c36e442c AK |
2031 | if (IS_ZEBRA_IF_BOND(ifp)) |
2032 | zebra_l2if_update_bond(ifp, false); | |
00a7710c AK |
2033 | if (IS_ZEBRA_IF_BOND_SLAVE(ifp)) |
2034 | zebra_l2if_update_bond_slave(ifp, bond_ifindex, false); | |
d62a17ae | 2035 | /* Special handling for bridge or VxLAN interfaces. */ |
2036 | if (IS_ZEBRA_IF_BRIDGE(ifp)) | |
2037 | zebra_l2_bridge_del(ifp); | |
2038 | else if (IS_ZEBRA_IF_VXLAN(ifp)) | |
2039 | zebra_l2_vxlanif_del(ifp); | |
2040 | ||
af736200 | 2041 | if_delete_update(ifp); |
e4c5b3ba IR |
2042 | |
2043 | /* If VRF, delete the VRF structure itself. */ | |
2044 | if (zif_type == ZEBRA_IF_VRF && !vrf_is_backend_netns()) | |
2045 | netlink_vrf_change(h, tb[IFLA_LINKINFO], ns_id, name); | |
1fdc9eae | 2046 | } |
2047 | ||
d62a17ae | 2048 | return 0; |
1fdc9eae | 2049 | } |
718e3744 | 2050 | |
c3bd894e QY |
2051 | int netlink_protodown(struct interface *ifp, bool down) |
2052 | { | |
2053 | struct zebra_ns *zns = zebra_ns_lookup(NS_DEFAULT); | |
2054 | ||
2055 | struct { | |
2056 | struct nlmsghdr n; | |
2057 | struct ifinfomsg ifa; | |
2058 | char buf[NL_PKT_BUF_SIZE]; | |
2059 | } req; | |
2060 | ||
2fff50ec | 2061 | memset(&req, 0, sizeof(req)); |
c3bd894e QY |
2062 | |
2063 | req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg)); | |
2064 | req.n.nlmsg_flags = NLM_F_REQUEST; | |
2065 | req.n.nlmsg_type = RTM_SETLINK; | |
2066 | req.n.nlmsg_pid = zns->netlink_cmd.snl.nl_pid; | |
2067 | ||
2068 | req.ifa.ifi_index = ifp->ifindex; | |
2069 | ||
312a6bee | 2070 | nl_attr_put(&req.n, sizeof(req), IFLA_PROTO_DOWN, &down, sizeof(down)); |
a757997c | 2071 | nl_attr_put32(&req.n, sizeof(req), IFLA_LINK, ifp->ifindex); |
c3bd894e QY |
2072 | |
2073 | return netlink_talk(netlink_talk_filter, &req.n, &zns->netlink_cmd, zns, | |
9bfadae8 | 2074 | false); |
c3bd894e QY |
2075 | } |
2076 | ||
718e3744 | 2077 | /* Interface information read by netlink. */ |
d62a17ae | 2078 | void interface_list(struct zebra_ns *zns) |
718e3744 | 2079 | { |
d62a17ae | 2080 | interface_lookup_netlink(zns); |
d9f5b2f5 SW |
2081 | /* We add routes for interface address, |
2082 | * so we need to get the nexthop info | |
2083 | * from the kernel before we can do that | |
2084 | */ | |
81505946 | 2085 | netlink_nexthop_read(zns); |
cc4e0650 | 2086 | |
d2bec88a | 2087 | interface_addr_lookup_netlink(zns); |
718e3744 | 2088 | } |
ddfeb486 DL |
2089 | |
2090 | #endif /* GNU_LINUX */ |