]>
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 | |
32 | ||
ba85366a | 33 | #include <netinet/if_ether.h> |
6675513d | 34 | #include <linux/if_bridge.h> |
ba777396 | 35 | #include <linux/if_link.h> |
1fdc9eae | 36 | #include <net/if_arp.h> |
535fe877 DS |
37 | #include <linux/sockios.h> |
38 | #include <linux/ethtool.h> | |
1fdc9eae | 39 | |
40 | #include "linklist.h" | |
41 | #include "if.h" | |
42 | #include "log.h" | |
43 | #include "prefix.h" | |
44 | #include "connected.h" | |
45 | #include "table.h" | |
46 | #include "memory.h" | |
47 | #include "zebra_memory.h" | |
48 | #include "rib.h" | |
49 | #include "thread.h" | |
50 | #include "privs.h" | |
51 | #include "nexthop.h" | |
52 | #include "vrf.h" | |
7922fc65 | 53 | #include "vrf_int.h" |
1fdc9eae | 54 | #include "mpls.h" |
174482ef | 55 | #include "lib_errors.h" |
718e3744 | 56 | |
5e6a74d8 | 57 | #include "vty.h" |
7dbeea9d | 58 | #include "zebra/zserv.h" |
1fdc9eae | 59 | #include "zebra/zebra_ns.h" |
60 | #include "zebra/zebra_vrf.h" | |
61 | #include "zebra/rt.h" | |
62 | #include "zebra/redistribute.h" | |
63 | #include "zebra/interface.h" | |
64 | #include "zebra/debug.h" | |
65 | #include "zebra/rtadv.h" | |
66 | #include "zebra/zebra_ptm.h" | |
67 | #include "zebra/zebra_mpls.h" | |
68 | #include "zebra/kernel_netlink.h" | |
d9f5b2f5 | 69 | #include "zebra/rt_netlink.h" |
1fdc9eae | 70 | #include "zebra/if_netlink.h" |
9df414fe | 71 | #include "zebra/zebra_errors.h" |
97c4e1d0 | 72 | #include "zebra/zebra_vxlan.h" |
42b56639 | 73 | #include "zebra/zebra_evpn_mh.h" |
1fdc9eae | 74 | |
0268f30e | 75 | extern struct zebra_privs_t zserv_privs; |
1fdc9eae | 76 | |
77 | /* Note: on netlink systems, there should be a 1-to-1 mapping between interface | |
78 | names and ifindex values. */ | |
d62a17ae | 79 | static void set_ifindex(struct interface *ifp, ifindex_t ifi_index, |
80 | struct zebra_ns *zns) | |
1fdc9eae | 81 | { |
d62a17ae | 82 | struct interface *oifp; |
83 | ||
84 | if (((oifp = if_lookup_by_index_per_ns(zns, ifi_index)) != NULL) | |
85 | && (oifp != ifp)) { | |
86 | if (ifi_index == IFINDEX_INTERNAL) | |
af4c2728 | 87 | flog_err( |
450971aa | 88 | EC_LIB_INTERFACE, |
4d43f68a | 89 | "Netlink is setting interface %s ifindex to reserved internal value %u", |
d62a17ae | 90 | ifp->name, ifi_index); |
91 | else { | |
92 | if (IS_ZEBRA_DEBUG_KERNEL) | |
93 | zlog_debug( | |
94 | "interface index %d was renamed from %s to %s", | |
95 | ifi_index, oifp->name, ifp->name); | |
96 | if (if_is_up(oifp)) | |
af4c2728 | 97 | flog_err( |
450971aa | 98 | EC_LIB_INTERFACE, |
4d43f68a | 99 | "interface rename detected on up interface: index %d was renamed from %s to %s, results are uncertain!", |
d62a17ae | 100 | ifi_index, oifp->name, ifp->name); |
101 | if_delete_update(oifp); | |
102 | } | |
103 | } | |
ff880b78 | 104 | if_set_index(ifp, ifi_index); |
1fdc9eae | 105 | } |
106 | ||
107 | /* Utility function to parse hardware link-layer address and update ifp */ | |
d62a17ae | 108 | static void netlink_interface_update_hw_addr(struct rtattr **tb, |
109 | struct interface *ifp) | |
1fdc9eae | 110 | { |
d62a17ae | 111 | int i; |
112 | ||
113 | if (tb[IFLA_ADDRESS]) { | |
114 | int hw_addr_len; | |
115 | ||
116 | hw_addr_len = RTA_PAYLOAD(tb[IFLA_ADDRESS]); | |
117 | ||
118 | if (hw_addr_len > INTERFACE_HWADDR_MAX) | |
9df414fe QY |
119 | zlog_debug("Hardware address is too large: %d", |
120 | hw_addr_len); | |
d62a17ae | 121 | else { |
122 | ifp->hw_addr_len = hw_addr_len; | |
123 | memcpy(ifp->hw_addr, RTA_DATA(tb[IFLA_ADDRESS]), | |
124 | hw_addr_len); | |
125 | ||
126 | for (i = 0; i < hw_addr_len; i++) | |
127 | if (ifp->hw_addr[i] != 0) | |
128 | break; | |
129 | ||
130 | if (i == hw_addr_len) | |
131 | ifp->hw_addr_len = 0; | |
132 | else | |
133 | ifp->hw_addr_len = hw_addr_len; | |
134 | } | |
135 | } | |
1fdc9eae | 136 | } |
137 | ||
d62a17ae | 138 | static enum zebra_link_type netlink_to_zebra_link_type(unsigned int hwt) |
1fdc9eae | 139 | { |
d62a17ae | 140 | switch (hwt) { |
141 | case ARPHRD_ETHER: | |
142 | return ZEBRA_LLT_ETHER; | |
143 | case ARPHRD_EETHER: | |
144 | return ZEBRA_LLT_EETHER; | |
145 | case ARPHRD_AX25: | |
146 | return ZEBRA_LLT_AX25; | |
147 | case ARPHRD_PRONET: | |
148 | return ZEBRA_LLT_PRONET; | |
149 | case ARPHRD_IEEE802: | |
150 | return ZEBRA_LLT_IEEE802; | |
151 | case ARPHRD_ARCNET: | |
152 | return ZEBRA_LLT_ARCNET; | |
153 | case ARPHRD_APPLETLK: | |
154 | return ZEBRA_LLT_APPLETLK; | |
155 | case ARPHRD_DLCI: | |
156 | return ZEBRA_LLT_DLCI; | |
157 | case ARPHRD_ATM: | |
158 | return ZEBRA_LLT_ATM; | |
159 | case ARPHRD_METRICOM: | |
160 | return ZEBRA_LLT_METRICOM; | |
161 | case ARPHRD_IEEE1394: | |
162 | return ZEBRA_LLT_IEEE1394; | |
163 | case ARPHRD_EUI64: | |
164 | return ZEBRA_LLT_EUI64; | |
165 | case ARPHRD_INFINIBAND: | |
166 | return ZEBRA_LLT_INFINIBAND; | |
167 | case ARPHRD_SLIP: | |
168 | return ZEBRA_LLT_SLIP; | |
169 | case ARPHRD_CSLIP: | |
170 | return ZEBRA_LLT_CSLIP; | |
171 | case ARPHRD_SLIP6: | |
172 | return ZEBRA_LLT_SLIP6; | |
173 | case ARPHRD_CSLIP6: | |
174 | return ZEBRA_LLT_CSLIP6; | |
175 | case ARPHRD_RSRVD: | |
176 | return ZEBRA_LLT_RSRVD; | |
177 | case ARPHRD_ADAPT: | |
178 | return ZEBRA_LLT_ADAPT; | |
179 | case ARPHRD_ROSE: | |
180 | return ZEBRA_LLT_ROSE; | |
181 | case ARPHRD_X25: | |
182 | return ZEBRA_LLT_X25; | |
183 | case ARPHRD_PPP: | |
184 | return ZEBRA_LLT_PPP; | |
185 | case ARPHRD_CISCO: | |
186 | return ZEBRA_LLT_CHDLC; | |
187 | case ARPHRD_LAPB: | |
188 | return ZEBRA_LLT_LAPB; | |
189 | case ARPHRD_RAWHDLC: | |
190 | return ZEBRA_LLT_RAWHDLC; | |
191 | case ARPHRD_TUNNEL: | |
192 | return ZEBRA_LLT_IPIP; | |
193 | case ARPHRD_TUNNEL6: | |
194 | return ZEBRA_LLT_IPIP6; | |
195 | case ARPHRD_FRAD: | |
196 | return ZEBRA_LLT_FRAD; | |
197 | case ARPHRD_SKIP: | |
198 | return ZEBRA_LLT_SKIP; | |
199 | case ARPHRD_LOOPBACK: | |
200 | return ZEBRA_LLT_LOOPBACK; | |
201 | case ARPHRD_LOCALTLK: | |
202 | return ZEBRA_LLT_LOCALTLK; | |
203 | case ARPHRD_FDDI: | |
204 | return ZEBRA_LLT_FDDI; | |
205 | case ARPHRD_SIT: | |
206 | return ZEBRA_LLT_SIT; | |
207 | case ARPHRD_IPDDP: | |
208 | return ZEBRA_LLT_IPDDP; | |
209 | case ARPHRD_IPGRE: | |
210 | return ZEBRA_LLT_IPGRE; | |
211 | case ARPHRD_PIMREG: | |
212 | return ZEBRA_LLT_PIMREG; | |
213 | case ARPHRD_HIPPI: | |
214 | return ZEBRA_LLT_HIPPI; | |
215 | case ARPHRD_ECONET: | |
216 | return ZEBRA_LLT_ECONET; | |
217 | case ARPHRD_IRDA: | |
218 | return ZEBRA_LLT_IRDA; | |
219 | case ARPHRD_FCPP: | |
220 | return ZEBRA_LLT_FCPP; | |
221 | case ARPHRD_FCAL: | |
222 | return ZEBRA_LLT_FCAL; | |
223 | case ARPHRD_FCPL: | |
224 | return ZEBRA_LLT_FCPL; | |
225 | case ARPHRD_FCFABRIC: | |
226 | return ZEBRA_LLT_FCFABRIC; | |
227 | case ARPHRD_IEEE802_TR: | |
228 | return ZEBRA_LLT_IEEE802_TR; | |
229 | case ARPHRD_IEEE80211: | |
230 | return ZEBRA_LLT_IEEE80211; | |
4268e09e | 231 | #ifdef ARPHRD_IEEE802154 |
d62a17ae | 232 | case ARPHRD_IEEE802154: |
233 | return ZEBRA_LLT_IEEE802154; | |
4268e09e | 234 | #endif |
1fdc9eae | 235 | #ifdef ARPHRD_IP6GRE |
d62a17ae | 236 | case ARPHRD_IP6GRE: |
237 | return ZEBRA_LLT_IP6GRE; | |
1fdc9eae | 238 | #endif |
239 | #ifdef ARPHRD_IEEE802154_PHY | |
d62a17ae | 240 | case ARPHRD_IEEE802154_PHY: |
241 | return ZEBRA_LLT_IEEE802154_PHY; | |
1fdc9eae | 242 | #endif |
243 | ||
d62a17ae | 244 | default: |
245 | return ZEBRA_LLT_UNKNOWN; | |
246 | } | |
1fdc9eae | 247 | } |
248 | ||
42b56639 AK |
249 | static inline void zebra_if_set_ziftype(struct interface *ifp, |
250 | zebra_iftype_t zif_type, | |
251 | zebra_slave_iftype_t zif_slave_type) | |
252 | { | |
253 | struct zebra_if *zif; | |
254 | ||
255 | zif = (struct zebra_if *)ifp->info; | |
256 | zif->zif_slave_type = zif_slave_type; | |
257 | ||
258 | if (zif->zif_type != zif_type) { | |
259 | zif->zif_type = zif_type; | |
260 | /* If the if_type has been set to bond initialize ES info | |
261 | * against it. XXX - note that we don't handle the case where | |
262 | * a zif changes from bond to non-bond; it is really | |
263 | * an unexpected/error condition. | |
264 | */ | |
265 | zebra_evpn_if_init(zif); | |
266 | } | |
267 | } | |
268 | ||
b9368db9 DD |
269 | static void netlink_determine_zebra_iftype(const char *kind, |
270 | zebra_iftype_t *zif_type) | |
6675513d | 271 | { |
d62a17ae | 272 | *zif_type = ZEBRA_IF_OTHER; |
273 | ||
274 | if (!kind) | |
275 | return; | |
276 | ||
277 | if (strcmp(kind, "vrf") == 0) | |
278 | *zif_type = ZEBRA_IF_VRF; | |
279 | else if (strcmp(kind, "bridge") == 0) | |
280 | *zif_type = ZEBRA_IF_BRIDGE; | |
281 | else if (strcmp(kind, "vlan") == 0) | |
282 | *zif_type = ZEBRA_IF_VLAN; | |
283 | else if (strcmp(kind, "vxlan") == 0) | |
284 | *zif_type = ZEBRA_IF_VXLAN; | |
1a98c087 MK |
285 | else if (strcmp(kind, "macvlan") == 0) |
286 | *zif_type = ZEBRA_IF_MACVLAN; | |
0e4864ea PG |
287 | else if (strcmp(kind, "veth") == 0) |
288 | *zif_type = ZEBRA_IF_VETH; | |
b9368db9 DD |
289 | else if (strcmp(kind, "bond") == 0) |
290 | *zif_type = ZEBRA_IF_BOND; | |
291 | else if (strcmp(kind, "bond_slave") == 0) | |
292 | *zif_type = ZEBRA_IF_BOND_SLAVE; | |
6675513d | 293 | } |
52d8f0d8 | 294 | |
d62a17ae | 295 | #define parse_rtattr_nested(tb, max, rta) \ |
296 | netlink_parse_rtattr((tb), (max), RTA_DATA(rta), RTA_PAYLOAD(rta)) | |
1fdc9eae | 297 | |
d62a17ae | 298 | static void netlink_vrf_change(struct nlmsghdr *h, struct rtattr *tb, |
5e031198 | 299 | uint32_t ns_id, const char *name) |
1fdc9eae | 300 | { |
d62a17ae | 301 | struct ifinfomsg *ifi; |
302 | struct rtattr *linkinfo[IFLA_INFO_MAX + 1]; | |
303 | struct rtattr *attr[IFLA_VRF_MAX + 1]; | |
304 | struct vrf *vrf; | |
305 | struct zebra_vrf *zvrf; | |
d7c0a89a | 306 | uint32_t nl_table_id; |
d62a17ae | 307 | |
308 | ifi = NLMSG_DATA(h); | |
309 | ||
0d6f7fd6 | 310 | memset(linkinfo, 0, sizeof(linkinfo)); |
d62a17ae | 311 | parse_rtattr_nested(linkinfo, IFLA_INFO_MAX, tb); |
312 | ||
313 | if (!linkinfo[IFLA_INFO_DATA]) { | |
314 | if (IS_ZEBRA_DEBUG_KERNEL) | |
315 | zlog_debug( | |
316 | "%s: IFLA_INFO_DATA missing from VRF message: %s", | |
317 | __func__, name); | |
318 | return; | |
319 | } | |
320 | ||
0d6f7fd6 | 321 | memset(attr, 0, sizeof(attr)); |
d62a17ae | 322 | parse_rtattr_nested(attr, IFLA_VRF_MAX, linkinfo[IFLA_INFO_DATA]); |
323 | if (!attr[IFLA_VRF_TABLE]) { | |
324 | if (IS_ZEBRA_DEBUG_KERNEL) | |
325 | zlog_debug( | |
326 | "%s: IFLA_VRF_TABLE missing from VRF message: %s", | |
327 | __func__, name); | |
328 | return; | |
1fdc9eae | 329 | } |
330 | ||
d7c0a89a | 331 | nl_table_id = *(uint32_t *)RTA_DATA(attr[IFLA_VRF_TABLE]); |
d62a17ae | 332 | |
333 | if (h->nlmsg_type == RTM_NEWLINK) { | |
334 | if (IS_ZEBRA_DEBUG_KERNEL) | |
335 | zlog_debug("RTM_NEWLINK for VRF %s(%u) table %u", name, | |
336 | ifi->ifi_index, nl_table_id); | |
337 | ||
2e86d16d RW |
338 | if (!vrf_lookup_by_id((vrf_id_t)ifi->ifi_index)) { |
339 | vrf_id_t exist_id; | |
5e031198 | 340 | |
2e86d16d RW |
341 | exist_id = vrf_lookup_by_table(nl_table_id, ns_id); |
342 | if (exist_id != VRF_DEFAULT) { | |
343 | vrf = vrf_lookup_by_id(exist_id); | |
344 | ||
345 | flog_err( | |
346 | EC_ZEBRA_VRF_MISCONFIGURED, | |
347 | "VRF %s id %u table id overlaps existing vrf %s, misconfiguration exiting", | |
348 | name, ifi->ifi_index, vrf->name); | |
349 | exit(-1); | |
350 | } | |
5e031198 | 351 | } |
2e86d16d | 352 | |
d62a17ae | 353 | /* |
354 | * vrf_get is implied creation if it does not exist | |
355 | */ | |
356 | vrf = vrf_get((vrf_id_t)ifi->ifi_index, | |
357 | name); // It would create vrf | |
358 | if (!vrf) { | |
450971aa | 359 | flog_err(EC_LIB_INTERFACE, "VRF %s id %u not created", |
1c50c1c0 | 360 | name, ifi->ifi_index); |
d62a17ae | 361 | return; |
362 | } | |
363 | ||
d62a17ae | 364 | /* |
365 | * This is the only place that we get the actual kernel table_id | |
366 | * being used. We need it to set the table_id of the routes | |
367 | * we are passing to the kernel.... And to throw some totally | |
368 | * awesome parties. that too. | |
593406a1 DS |
369 | * |
370 | * At this point we *must* have a zvrf because the vrf_create | |
371 | * callback creates one. We *must* set the table id | |
372 | * before the vrf_enable because of( at the very least ) | |
373 | * static routes being delayed for installation until | |
374 | * during the vrf_enable callbacks. | |
d62a17ae | 375 | */ |
376 | zvrf = (struct zebra_vrf *)vrf->info; | |
377 | zvrf->table_id = nl_table_id; | |
593406a1 DS |
378 | |
379 | /* Enable the created VRF. */ | |
380 | if (!vrf_enable(vrf)) { | |
450971aa | 381 | flog_err(EC_LIB_INTERFACE, |
1c50c1c0 QY |
382 | "Failed to enable VRF %s id %u", name, |
383 | ifi->ifi_index); | |
593406a1 DS |
384 | return; |
385 | } | |
386 | ||
d62a17ae | 387 | } else // h->nlmsg_type == RTM_DELLINK |
388 | { | |
389 | if (IS_ZEBRA_DEBUG_KERNEL) | |
390 | zlog_debug("RTM_DELLINK for VRF %s(%u)", name, | |
391 | ifi->ifi_index); | |
392 | ||
393 | vrf = vrf_lookup_by_id((vrf_id_t)ifi->ifi_index); | |
394 | ||
395 | if (!vrf) { | |
e914ccbe | 396 | flog_warn(EC_ZEBRA_VRF_NOT_FOUND, "%s: vrf not found", |
9df414fe | 397 | __func__); |
d62a17ae | 398 | return; |
399 | } | |
400 | ||
401 | vrf_delete(vrf); | |
402 | } | |
1fdc9eae | 403 | } |
404 | ||
67188ca2 | 405 | static uint32_t get_iflink_speed(struct interface *interface, int *error) |
535fe877 | 406 | { |
d62a17ae | 407 | struct ifreq ifdata; |
408 | struct ethtool_cmd ecmd; | |
409 | int sd; | |
410 | int rc; | |
0268f30e | 411 | const char *ifname = interface->name; |
d62a17ae | 412 | |
594c2878 JF |
413 | if (error) |
414 | *error = 0; | |
d62a17ae | 415 | /* initialize struct */ |
416 | memset(&ifdata, 0, sizeof(ifdata)); | |
417 | ||
418 | /* set interface name */ | |
0af35d90 | 419 | strlcpy(ifdata.ifr_name, ifname, sizeof(ifdata.ifr_name)); |
d62a17ae | 420 | |
421 | /* initialize ethtool interface */ | |
422 | memset(&ecmd, 0, sizeof(ecmd)); | |
423 | ecmd.cmd = ETHTOOL_GSET; /* ETHTOOL_GLINK */ | |
ba85366a | 424 | ifdata.ifr_data = (caddr_t)&ecmd; |
d62a17ae | 425 | |
426 | /* use ioctl to get IP address of an interface */ | |
0cf6db21 | 427 | frr_with_privs(&zserv_privs) { |
01b9e3fd | 428 | sd = vrf_socket(PF_INET, SOCK_DGRAM, IPPROTO_IP, |
a36898e7 | 429 | interface->vrf_id, |
01b9e3fd DL |
430 | NULL); |
431 | if (sd < 0) { | |
432 | if (IS_ZEBRA_DEBUG_KERNEL) | |
433 | zlog_debug("Failure to read interface %s speed: %d %s", | |
434 | ifname, errno, safe_strerror(errno)); | |
594c2878 JF |
435 | /* no vrf socket creation may probably mean vrf issue */ |
436 | if (error) | |
437 | *error = -1; | |
01b9e3fd DL |
438 | return 0; |
439 | } | |
d62a17ae | 440 | /* Get the current link state for the interface */ |
a36898e7 | 441 | rc = vrf_ioctl(interface->vrf_id, sd, SIOCETHTOOL, |
633fc9b1 | 442 | (char *)&ifdata); |
01b9e3fd | 443 | } |
d62a17ae | 444 | if (rc < 0) { |
f767cee4 | 445 | if (errno != EOPNOTSUPP && IS_ZEBRA_DEBUG_KERNEL) |
bd7d0299 DS |
446 | zlog_debug( |
447 | "IOCTL failure to read interface %s speed: %d %s", | |
448 | ifname, errno, safe_strerror(errno)); | |
594c2878 JF |
449 | /* no device means interface unreachable */ |
450 | if (errno == ENODEV && error) | |
451 | *error = -1; | |
d62a17ae | 452 | ecmd.speed_hi = 0; |
453 | ecmd.speed = 0; | |
454 | } | |
455 | ||
456 | close(sd); | |
457 | ||
67188ca2 | 458 | return ((uint32_t)ecmd.speed_hi << 16) | ecmd.speed; |
535fe877 DS |
459 | } |
460 | ||
594c2878 | 461 | uint32_t kernel_get_speed(struct interface *ifp, int *error) |
dc7b3cae | 462 | { |
594c2878 | 463 | return get_iflink_speed(ifp, error); |
dc7b3cae DS |
464 | } |
465 | ||
d62a17ae | 466 | static int netlink_extract_bridge_info(struct rtattr *link_data, |
467 | struct zebra_l2info_bridge *bridge_info) | |
6675513d | 468 | { |
d62a17ae | 469 | struct rtattr *attr[IFLA_BR_MAX + 1]; |
470 | ||
471 | memset(bridge_info, 0, sizeof(*bridge_info)); | |
0d6f7fd6 | 472 | memset(attr, 0, sizeof(attr)); |
d62a17ae | 473 | parse_rtattr_nested(attr, IFLA_BR_MAX, link_data); |
474 | if (attr[IFLA_BR_VLAN_FILTERING]) | |
475 | bridge_info->vlan_aware = | |
d7c0a89a | 476 | *(uint8_t *)RTA_DATA(attr[IFLA_BR_VLAN_FILTERING]); |
d62a17ae | 477 | return 0; |
6675513d | 478 | } |
479 | ||
d62a17ae | 480 | static int netlink_extract_vlan_info(struct rtattr *link_data, |
481 | struct zebra_l2info_vlan *vlan_info) | |
6675513d | 482 | { |
d62a17ae | 483 | struct rtattr *attr[IFLA_VLAN_MAX + 1]; |
484 | vlanid_t vid_in_msg; | |
485 | ||
486 | memset(vlan_info, 0, sizeof(*vlan_info)); | |
0d6f7fd6 | 487 | memset(attr, 0, sizeof(attr)); |
d62a17ae | 488 | parse_rtattr_nested(attr, IFLA_VLAN_MAX, link_data); |
489 | if (!attr[IFLA_VLAN_ID]) { | |
490 | if (IS_ZEBRA_DEBUG_KERNEL) | |
491 | zlog_debug("IFLA_VLAN_ID missing from VLAN IF message"); | |
492 | return -1; | |
493 | } | |
494 | ||
495 | vid_in_msg = *(vlanid_t *)RTA_DATA(attr[IFLA_VLAN_ID]); | |
496 | vlan_info->vid = vid_in_msg; | |
497 | return 0; | |
6675513d | 498 | } |
499 | ||
d62a17ae | 500 | static int netlink_extract_vxlan_info(struct rtattr *link_data, |
501 | struct zebra_l2info_vxlan *vxl_info) | |
6675513d | 502 | { |
d62a17ae | 503 | struct rtattr *attr[IFLA_VXLAN_MAX + 1]; |
504 | vni_t vni_in_msg; | |
505 | struct in_addr vtep_ip_in_msg; | |
14ddb3d9 | 506 | ifindex_t ifindex_link; |
d62a17ae | 507 | |
508 | memset(vxl_info, 0, sizeof(*vxl_info)); | |
0d6f7fd6 | 509 | memset(attr, 0, sizeof(attr)); |
d62a17ae | 510 | parse_rtattr_nested(attr, IFLA_VXLAN_MAX, link_data); |
511 | if (!attr[IFLA_VXLAN_ID]) { | |
512 | if (IS_ZEBRA_DEBUG_KERNEL) | |
513 | zlog_debug( | |
514 | "IFLA_VXLAN_ID missing from VXLAN IF message"); | |
515 | return -1; | |
516 | } | |
517 | ||
518 | vni_in_msg = *(vni_t *)RTA_DATA(attr[IFLA_VXLAN_ID]); | |
519 | vxl_info->vni = vni_in_msg; | |
520 | if (!attr[IFLA_VXLAN_LOCAL]) { | |
521 | if (IS_ZEBRA_DEBUG_KERNEL) | |
522 | zlog_debug( | |
523 | "IFLA_VXLAN_LOCAL missing from VXLAN IF message"); | |
524 | } else { | |
525 | vtep_ip_in_msg = | |
526 | *(struct in_addr *)RTA_DATA(attr[IFLA_VXLAN_LOCAL]); | |
527 | vxl_info->vtep_ip = vtep_ip_in_msg; | |
528 | } | |
529 | ||
d7fe235c AK |
530 | if (attr[IFLA_VXLAN_GROUP]) { |
531 | vxl_info->mcast_grp = | |
532 | *(struct in_addr *)RTA_DATA(attr[IFLA_VXLAN_GROUP]); | |
533 | } | |
534 | ||
14ddb3d9 PG |
535 | if (!attr[IFLA_VXLAN_LINK]) { |
536 | if (IS_ZEBRA_DEBUG_KERNEL) | |
3efd0893 | 537 | zlog_debug("IFLA_VXLAN_LINK missing from VXLAN IF message"); |
14ddb3d9 PG |
538 | } else { |
539 | ifindex_link = | |
540 | *(ifindex_t *)RTA_DATA(attr[IFLA_VXLAN_LINK]); | |
541 | vxl_info->ifindex_link = ifindex_link; | |
542 | } | |
d62a17ae | 543 | return 0; |
6675513d | 544 | } |
545 | ||
546 | /* | |
547 | * Extract and save L2 params (of interest) for an interface. When a | |
548 | * bridge interface is added or updated, take further actions to map | |
549 | * its members. Likewise, for VxLAN interface. | |
550 | */ | |
d62a17ae | 551 | static void netlink_interface_update_l2info(struct interface *ifp, |
14ddb3d9 PG |
552 | struct rtattr *link_data, int add, |
553 | ns_id_t link_nsid) | |
6675513d | 554 | { |
d62a17ae | 555 | if (!link_data) |
556 | return; | |
557 | ||
558 | if (IS_ZEBRA_IF_BRIDGE(ifp)) { | |
559 | struct zebra_l2info_bridge bridge_info; | |
560 | ||
561 | netlink_extract_bridge_info(link_data, &bridge_info); | |
562 | zebra_l2_bridge_add_update(ifp, &bridge_info, add); | |
563 | } else if (IS_ZEBRA_IF_VLAN(ifp)) { | |
564 | struct zebra_l2info_vlan vlan_info; | |
565 | ||
566 | netlink_extract_vlan_info(link_data, &vlan_info); | |
567 | zebra_l2_vlanif_update(ifp, &vlan_info); | |
568 | } else if (IS_ZEBRA_IF_VXLAN(ifp)) { | |
569 | struct zebra_l2info_vxlan vxlan_info; | |
570 | ||
571 | netlink_extract_vxlan_info(link_data, &vxlan_info); | |
14ddb3d9 | 572 | vxlan_info.link_nsid = link_nsid; |
d62a17ae | 573 | zebra_l2_vxlanif_add_update(ifp, &vxlan_info, add); |
14ddb3d9 PG |
574 | if (link_nsid != NS_UNKNOWN && |
575 | vxlan_info.ifindex_link) | |
576 | zebra_if_update_link(ifp, vxlan_info.ifindex_link, | |
577 | link_nsid); | |
d62a17ae | 578 | } |
6675513d | 579 | } |
580 | ||
42b56639 AK |
581 | static int netlink_bridge_vxlan_update(struct interface *ifp, |
582 | struct rtattr *af_spec) | |
583 | { | |
584 | struct rtattr *aftb[IFLA_BRIDGE_MAX + 1]; | |
585 | struct bridge_vlan_info *vinfo; | |
586 | vlanid_t access_vlan; | |
587 | ||
588 | /* There is a 1-to-1 mapping of VLAN to VxLAN - hence | |
589 | * only 1 access VLAN is accepted. | |
590 | */ | |
591 | memset(aftb, 0, sizeof(aftb)); | |
592 | parse_rtattr_nested(aftb, IFLA_BRIDGE_MAX, af_spec); | |
593 | if (!aftb[IFLA_BRIDGE_VLAN_INFO]) | |
594 | return 0; | |
595 | ||
596 | vinfo = RTA_DATA(aftb[IFLA_BRIDGE_VLAN_INFO]); | |
597 | if (!(vinfo->flags & BRIDGE_VLAN_INFO_PVID)) | |
598 | return 0; | |
599 | ||
600 | access_vlan = (vlanid_t)vinfo->vid; | |
601 | if (IS_ZEBRA_DEBUG_KERNEL) | |
602 | zlog_debug("Access VLAN %u for VxLAN IF %s(%u)", access_vlan, | |
603 | ifp->name, ifp->ifindex); | |
604 | zebra_l2_vxlanif_update_access_vlan(ifp, access_vlan); | |
605 | return 0; | |
606 | } | |
607 | ||
608 | static void netlink_bridge_vlan_update(struct interface *ifp, | |
609 | struct rtattr *af_spec) | |
610 | { | |
611 | struct rtattr *i; | |
612 | int rem; | |
613 | uint16_t vid_range_start = 0; | |
614 | struct zebra_if *zif; | |
615 | bitfield_t old_vlan_bitmap; | |
616 | struct bridge_vlan_info *vinfo; | |
617 | ||
618 | zif = (struct zebra_if *)ifp->info; | |
619 | ||
620 | /* cache the old bitmap addrs */ | |
621 | old_vlan_bitmap = zif->vlan_bitmap; | |
622 | /* create a new bitmap space for re-eval */ | |
623 | bf_init(zif->vlan_bitmap, IF_VLAN_BITMAP_MAX); | |
624 | ||
625 | for (i = RTA_DATA(af_spec), rem = RTA_PAYLOAD(af_spec); | |
626 | RTA_OK(i, rem); i = RTA_NEXT(i, rem)) { | |
627 | ||
628 | if (i->rta_type != IFLA_BRIDGE_VLAN_INFO) | |
629 | continue; | |
630 | ||
631 | vinfo = RTA_DATA(i); | |
632 | ||
633 | if (vinfo->flags & BRIDGE_VLAN_INFO_RANGE_BEGIN) { | |
634 | vid_range_start = vinfo->vid; | |
635 | continue; | |
636 | } | |
637 | ||
638 | if (!(vinfo->flags & BRIDGE_VLAN_INFO_RANGE_END)) | |
639 | vid_range_start = vinfo->vid; | |
640 | ||
641 | zebra_vlan_bitmap_compute(ifp, vid_range_start, vinfo->vid); | |
642 | } | |
643 | ||
644 | zebra_vlan_mbr_re_eval(ifp, old_vlan_bitmap); | |
645 | ||
646 | bf_free(old_vlan_bitmap); | |
647 | } | |
648 | ||
d62a17ae | 649 | static int netlink_bridge_interface(struct nlmsghdr *h, int len, ns_id_t ns_id, |
650 | int startup) | |
6675513d | 651 | { |
d62a17ae | 652 | char *name = NULL; |
653 | struct ifinfomsg *ifi; | |
654 | struct rtattr *tb[IFLA_MAX + 1]; | |
655 | struct interface *ifp; | |
42b56639 AK |
656 | struct zebra_if *zif; |
657 | struct rtattr *af_spec; | |
d62a17ae | 658 | |
659 | /* Fetch name and ifindex */ | |
660 | ifi = NLMSG_DATA(h); | |
0d6f7fd6 | 661 | memset(tb, 0, sizeof(tb)); |
d62a17ae | 662 | netlink_parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), len); |
663 | ||
664 | if (tb[IFLA_IFNAME] == NULL) | |
665 | return -1; | |
666 | name = (char *)RTA_DATA(tb[IFLA_IFNAME]); | |
667 | ||
668 | /* The interface should already be known, if not discard. */ | |
669 | ifp = if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id), ifi->ifi_index); | |
670 | if (!ifp) { | |
9df414fe QY |
671 | zlog_debug("Cannot find bridge IF %s(%u)", name, |
672 | ifi->ifi_index); | |
d62a17ae | 673 | return 0; |
674 | } | |
d62a17ae | 675 | |
676 | /* We are only interested in the access VLAN i.e., AF_SPEC */ | |
42b56639 AK |
677 | af_spec = tb[IFLA_AF_SPEC]; |
678 | if (!af_spec) | |
679 | return 0; | |
d62a17ae | 680 | |
42b56639 AK |
681 | if (IS_ZEBRA_IF_VXLAN(ifp)) |
682 | return netlink_bridge_vxlan_update(ifp, af_spec); | |
d62a17ae | 683 | |
42b56639 AK |
684 | /* build vlan bitmap associated with this interface if that |
685 | * device type is interested in the vlans | |
686 | */ | |
687 | zif = (struct zebra_if *)ifp->info; | |
688 | if (bf_is_inited(zif->vlan_bitmap)) | |
689 | netlink_bridge_vlan_update(ifp, af_spec); | |
d62a17ae | 690 | |
d62a17ae | 691 | return 0; |
6675513d | 692 | } |
693 | ||
2414abd3 DS |
694 | /* |
695 | * Called from interface_lookup_netlink(). This function is only used | |
696 | * during bootstrap. | |
697 | */ | |
698 | static int netlink_interface(struct nlmsghdr *h, ns_id_t ns_id, int startup) | |
1fdc9eae | 699 | { |
d62a17ae | 700 | int len; |
701 | struct ifinfomsg *ifi; | |
702 | struct rtattr *tb[IFLA_MAX + 1]; | |
703 | struct rtattr *linkinfo[IFLA_MAX + 1]; | |
704 | struct interface *ifp; | |
705 | char *name = NULL; | |
706 | char *kind = NULL; | |
48884c6b | 707 | char *desc = NULL; |
d62a17ae | 708 | char *slave_kind = NULL; |
ea7ec261 | 709 | struct zebra_ns *zns = NULL; |
d62a17ae | 710 | vrf_id_t vrf_id = VRF_DEFAULT; |
711 | zebra_iftype_t zif_type = ZEBRA_IF_OTHER; | |
712 | zebra_slave_iftype_t zif_slave_type = ZEBRA_IF_SLAVE_NONE; | |
713 | ifindex_t bridge_ifindex = IFINDEX_INTERNAL; | |
714 | ifindex_t link_ifindex = IFINDEX_INTERNAL; | |
b9368db9 | 715 | ifindex_t bond_ifindex = IFINDEX_INTERNAL; |
520ebf72 | 716 | struct zebra_if *zif; |
14ddb3d9 | 717 | ns_id_t link_nsid = ns_id; |
d62a17ae | 718 | |
719 | zns = zebra_ns_lookup(ns_id); | |
720 | ifi = NLMSG_DATA(h); | |
721 | ||
722 | if (h->nlmsg_type != RTM_NEWLINK) | |
723 | return 0; | |
724 | ||
725 | len = h->nlmsg_len - NLMSG_LENGTH(sizeof(struct ifinfomsg)); | |
9bdf8618 | 726 | if (len < 0) { |
15569c58 DA |
727 | zlog_err( |
728 | "%s: Message received from netlink is of a broken size: %d %zu", | |
729 | __func__, h->nlmsg_len, | |
730 | (size_t)NLMSG_LENGTH(sizeof(struct ifinfomsg))); | |
d62a17ae | 731 | return -1; |
9bdf8618 | 732 | } |
d62a17ae | 733 | |
734 | /* We are interested in some AF_BRIDGE notifications. */ | |
735 | if (ifi->ifi_family == AF_BRIDGE) | |
736 | return netlink_bridge_interface(h, len, ns_id, startup); | |
737 | ||
738 | /* Looking up interface name. */ | |
0d6f7fd6 DA |
739 | memset(tb, 0, sizeof(tb)); |
740 | memset(linkinfo, 0, sizeof(linkinfo)); | |
d62a17ae | 741 | netlink_parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), len); |
1fdc9eae | 742 | |
d62a17ae | 743 | /* check for wireless messages to ignore */ |
744 | if ((tb[IFLA_WIRELESS] != NULL) && (ifi->ifi_change == 0)) { | |
745 | if (IS_ZEBRA_DEBUG_KERNEL) | |
746 | zlog_debug("%s: ignoring IFLA_WIRELESS message", | |
747 | __func__); | |
748 | return 0; | |
749 | } | |
1fdc9eae | 750 | |
d62a17ae | 751 | if (tb[IFLA_IFNAME] == NULL) |
752 | return -1; | |
753 | name = (char *)RTA_DATA(tb[IFLA_IFNAME]); | |
1fdc9eae | 754 | |
48884c6b DS |
755 | if (tb[IFLA_IFALIAS]) |
756 | desc = (char *)RTA_DATA(tb[IFLA_IFALIAS]); | |
757 | ||
d62a17ae | 758 | if (tb[IFLA_LINKINFO]) { |
759 | parse_rtattr_nested(linkinfo, IFLA_INFO_MAX, tb[IFLA_LINKINFO]); | |
1fdc9eae | 760 | |
d62a17ae | 761 | if (linkinfo[IFLA_INFO_KIND]) |
762 | kind = RTA_DATA(linkinfo[IFLA_INFO_KIND]); | |
1fdc9eae | 763 | |
d62a17ae | 764 | if (linkinfo[IFLA_INFO_SLAVE_KIND]) |
765 | slave_kind = RTA_DATA(linkinfo[IFLA_INFO_SLAVE_KIND]); | |
1fdc9eae | 766 | |
b9368db9 DD |
767 | if ((slave_kind != NULL) && strcmp(slave_kind, "bond") == 0) |
768 | netlink_determine_zebra_iftype("bond_slave", &zif_type); | |
769 | else | |
770 | netlink_determine_zebra_iftype(kind, &zif_type); | |
d62a17ae | 771 | } |
772 | ||
773 | /* If VRF, create the VRF structure itself. */ | |
78dd30b2 | 774 | if (zif_type == ZEBRA_IF_VRF && !vrf_is_backend_netns()) { |
5e031198 | 775 | netlink_vrf_change(h, tb[IFLA_LINKINFO], ns_id, name); |
d62a17ae | 776 | vrf_id = (vrf_id_t)ifi->ifi_index; |
777 | } | |
778 | ||
779 | if (tb[IFLA_MASTER]) { | |
78dd30b2 PG |
780 | if (slave_kind && (strcmp(slave_kind, "vrf") == 0) |
781 | && !vrf_is_backend_netns()) { | |
d62a17ae | 782 | zif_slave_type = ZEBRA_IF_SLAVE_VRF; |
d7c0a89a | 783 | vrf_id = *(uint32_t *)RTA_DATA(tb[IFLA_MASTER]); |
d62a17ae | 784 | } else if (slave_kind && (strcmp(slave_kind, "bridge") == 0)) { |
785 | zif_slave_type = ZEBRA_IF_SLAVE_BRIDGE; | |
786 | bridge_ifindex = | |
787 | *(ifindex_t *)RTA_DATA(tb[IFLA_MASTER]); | |
b9368db9 DD |
788 | } else if (slave_kind && (strcmp(slave_kind, "bond") == 0)) { |
789 | zif_slave_type = ZEBRA_IF_SLAVE_BOND; | |
790 | bond_ifindex = *(ifindex_t *)RTA_DATA(tb[IFLA_MASTER]); | |
d62a17ae | 791 | } else |
792 | zif_slave_type = ZEBRA_IF_SLAVE_OTHER; | |
793 | } | |
78dd30b2 PG |
794 | if (vrf_is_backend_netns()) |
795 | vrf_id = (vrf_id_t)ns_id; | |
a36898e7 | 796 | |
d62a17ae | 797 | /* If linking to another interface, note it. */ |
798 | if (tb[IFLA_LINK]) | |
799 | link_ifindex = *(ifindex_t *)RTA_DATA(tb[IFLA_LINK]); | |
800 | ||
b1cc23b2 | 801 | if (tb[IFLA_LINK_NETNSID]) { |
14ddb3d9 | 802 | link_nsid = *(ns_id_t *)RTA_DATA(tb[IFLA_LINK_NETNSID]); |
b1cc23b2 PG |
803 | link_nsid = ns_id_get_absolute(ns_id, link_nsid); |
804 | } | |
14ddb3d9 | 805 | |
ea7ec261 DD |
806 | /* Add interface. |
807 | * We add by index first because in some cases such as the master | |
808 | * interface, we have the index before we have the name. Fixing | |
809 | * back references on the slave interfaces is painful if not done | |
810 | * this way, i.e. by creating by ifindex. | |
811 | */ | |
bd23c840 | 812 | ifp = if_get_by_ifindex(ifi->ifi_index, vrf_id); |
ea7ec261 | 813 | set_ifindex(ifp, ifi->ifi_index, zns); /* add it to ns struct */ |
d5c65bf1 | 814 | |
bd23c840 PR |
815 | if_set_name(ifp, name); |
816 | ||
d62a17ae | 817 | ifp->flags = ifi->ifi_flags & 0x0000fffff; |
d62a17ae | 818 | ifp->mtu6 = ifp->mtu = *(uint32_t *)RTA_DATA(tb[IFLA_MTU]); |
819 | ifp->metric = 0; | |
594c2878 | 820 | ifp->speed = get_iflink_speed(ifp, NULL); |
d62a17ae | 821 | ifp->ptm_status = ZEBRA_PTM_STATUS_UNKNOWN; |
822 | ||
823 | /* Set zebra interface type */ | |
824 | zebra_if_set_ziftype(ifp, zif_type, zif_slave_type); | |
b0fa6f6a CS |
825 | if (IS_ZEBRA_IF_VRF(ifp)) |
826 | SET_FLAG(ifp->status, ZEBRA_INTERFACE_VRF_LOOPBACK); | |
d62a17ae | 827 | |
98efddf1 DS |
828 | /* |
829 | * Just set the @link/lower-device ifindex. During nldump interfaces are | |
520ebf72 AK |
830 | * not ordered in any fashion so we may end up getting upper devices |
831 | * before lower devices. We will setup the real linkage once the dump | |
98efddf1 DS |
832 | * is complete. |
833 | */ | |
520ebf72 AK |
834 | zif = (struct zebra_if *)ifp->info; |
835 | zif->link_ifindex = link_ifindex; | |
d62a17ae | 836 | |
ba5165ec DS |
837 | if (desc) { |
838 | XFREE(MTYPE_TMP, zif->desc); | |
839 | zif->desc = XSTRDUP(MTYPE_TMP, desc); | |
840 | } | |
841 | ||
d62a17ae | 842 | /* Hardware type and address. */ |
843 | ifp->ll_type = netlink_to_zebra_link_type(ifi->ifi_type); | |
844 | netlink_interface_update_hw_addr(tb, ifp); | |
845 | ||
846 | if_add_update(ifp); | |
847 | ||
848 | /* Extract and save L2 interface information, take additional actions. | |
849 | */ | |
14ddb3d9 PG |
850 | netlink_interface_update_l2info(ifp, linkinfo[IFLA_INFO_DATA], |
851 | 1, link_nsid); | |
d62a17ae | 852 | if (IS_ZEBRA_IF_BRIDGE_SLAVE(ifp)) |
a6e0edf2 | 853 | zebra_l2if_update_bridge_slave(ifp, bridge_ifindex, ns_id); |
b9368db9 DD |
854 | else if (IS_ZEBRA_IF_BOND_SLAVE(ifp)) |
855 | zebra_l2if_update_bond_slave(ifp, bond_ifindex); | |
d62a17ae | 856 | |
857 | return 0; | |
1fdc9eae | 858 | } |
859 | ||
289602d7 | 860 | /* Request for specific interface or address information from the kernel */ |
85a75f1e MS |
861 | static int netlink_request_intf_addr(struct nlsock *netlink_cmd, int family, |
862 | int type, uint32_t filter_mask) | |
289602d7 | 863 | { |
d62a17ae | 864 | struct { |
865 | struct nlmsghdr n; | |
866 | struct ifinfomsg ifm; | |
867 | char buf[256]; | |
868 | } req; | |
869 | ||
870 | /* Form the request, specifying filter (rtattr) if needed. */ | |
871 | memset(&req, 0, sizeof(req)); | |
872 | req.n.nlmsg_type = type; | |
718f9b0f | 873 | req.n.nlmsg_flags = NLM_F_ROOT | NLM_F_MATCH | NLM_F_REQUEST; |
d62a17ae | 874 | req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg)); |
875 | req.ifm.ifi_family = family; | |
876 | ||
877 | /* Include filter, if specified. */ | |
878 | if (filter_mask) | |
312a6bee | 879 | nl_attr_put32(&req.n, sizeof(req), IFLA_EXT_MASK, filter_mask); |
d62a17ae | 880 | |
fd3f8e52 | 881 | return netlink_request(netlink_cmd, &req); |
289602d7 | 882 | } |
883 | ||
1fdc9eae | 884 | /* Interface lookup by netlink socket. */ |
d62a17ae | 885 | int interface_lookup_netlink(struct zebra_ns *zns) |
1fdc9eae | 886 | { |
d62a17ae | 887 | int ret; |
85a75f1e MS |
888 | struct zebra_dplane_info dp_info; |
889 | struct nlsock *netlink_cmd = &zns->netlink_cmd; | |
890 | ||
891 | /* Capture key info from ns struct */ | |
892 | zebra_dplane_info_from_zns(&dp_info, zns, true /*is_cmd*/); | |
d62a17ae | 893 | |
894 | /* Get interface information. */ | |
85a75f1e | 895 | ret = netlink_request_intf_addr(netlink_cmd, AF_PACKET, RTM_GETLINK, 0); |
d62a17ae | 896 | if (ret < 0) |
897 | return ret; | |
85a75f1e | 898 | ret = netlink_parse_info(netlink_interface, netlink_cmd, &dp_info, 0, |
d62a17ae | 899 | 1); |
900 | if (ret < 0) | |
901 | return ret; | |
902 | ||
903 | /* Get interface information - for bridge interfaces. */ | |
85a75f1e | 904 | ret = netlink_request_intf_addr(netlink_cmd, AF_BRIDGE, RTM_GETLINK, |
d62a17ae | 905 | RTEXT_FILTER_BRVLAN); |
906 | if (ret < 0) | |
907 | return ret; | |
85a75f1e | 908 | ret = netlink_parse_info(netlink_interface, netlink_cmd, &dp_info, 0, |
d62a17ae | 909 | 0); |
910 | if (ret < 0) | |
911 | return ret; | |
912 | ||
913 | /* Get interface information - for bridge interfaces. */ | |
85a75f1e | 914 | ret = netlink_request_intf_addr(netlink_cmd, AF_BRIDGE, RTM_GETLINK, |
d62a17ae | 915 | RTEXT_FILTER_BRVLAN); |
916 | if (ret < 0) | |
917 | return ret; | |
85a75f1e | 918 | ret = netlink_parse_info(netlink_interface, netlink_cmd, &dp_info, 0, |
d62a17ae | 919 | 0); |
920 | if (ret < 0) | |
921 | return ret; | |
922 | ||
520ebf72 AK |
923 | /* fixup linkages */ |
924 | zebra_if_update_all_links(); | |
d2bec88a SW |
925 | return 0; |
926 | } | |
927 | ||
928 | /** | |
929 | * interface_addr_lookup_netlink() - Look up interface addresses | |
930 | * | |
931 | * @zns: Zebra netlink socket | |
932 | * Return: Result status | |
933 | */ | |
934 | static int interface_addr_lookup_netlink(struct zebra_ns *zns) | |
935 | { | |
936 | int ret; | |
937 | struct zebra_dplane_info dp_info; | |
938 | struct nlsock *netlink_cmd = &zns->netlink_cmd; | |
939 | ||
940 | /* Capture key info from ns struct */ | |
941 | zebra_dplane_info_from_zns(&dp_info, zns, true /*is_cmd*/); | |
520ebf72 | 942 | |
d62a17ae | 943 | /* Get IPv4 address of the interfaces. */ |
85a75f1e | 944 | ret = netlink_request_intf_addr(netlink_cmd, AF_INET, RTM_GETADDR, 0); |
d62a17ae | 945 | if (ret < 0) |
946 | return ret; | |
85a75f1e | 947 | ret = netlink_parse_info(netlink_interface_addr, netlink_cmd, &dp_info, |
d62a17ae | 948 | 0, 1); |
949 | if (ret < 0) | |
950 | return ret; | |
951 | ||
952 | /* Get IPv6 address of the interfaces. */ | |
85a75f1e | 953 | ret = netlink_request_intf_addr(netlink_cmd, AF_INET6, RTM_GETADDR, 0); |
d62a17ae | 954 | if (ret < 0) |
955 | return ret; | |
85a75f1e | 956 | ret = netlink_parse_info(netlink_interface_addr, netlink_cmd, &dp_info, |
d62a17ae | 957 | 0, 1); |
958 | if (ret < 0) | |
959 | return ret; | |
960 | ||
961 | return 0; | |
1fdc9eae | 962 | } |
963 | ||
e0ae31b8 DS |
964 | int kernel_interface_set_master(struct interface *master, |
965 | struct interface *slave) | |
966 | { | |
967 | struct zebra_ns *zns = zebra_ns_lookup(NS_DEFAULT); | |
968 | ||
969 | struct { | |
970 | struct nlmsghdr n; | |
971 | struct ifinfomsg ifa; | |
972 | char buf[NL_PKT_BUF_SIZE]; | |
973 | } req; | |
974 | ||
0d6f7fd6 | 975 | memset(&req, 0, sizeof(req)); |
e0ae31b8 DS |
976 | |
977 | req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg)); | |
978 | req.n.nlmsg_flags = NLM_F_REQUEST; | |
979 | req.n.nlmsg_type = RTM_SETLINK; | |
980 | req.n.nlmsg_pid = zns->netlink_cmd.snl.nl_pid; | |
981 | ||
982 | req.ifa.ifi_index = slave->ifindex; | |
983 | ||
a757997c JU |
984 | nl_attr_put32(&req.n, sizeof(req), IFLA_MASTER, master->ifindex); |
985 | nl_attr_put32(&req.n, sizeof(req), IFLA_LINK, slave->ifindex); | |
e0ae31b8 DS |
986 | |
987 | return netlink_talk(netlink_talk_filter, &req.n, &zns->netlink_cmd, zns, | |
988 | 0); | |
989 | } | |
990 | ||
1fdc9eae | 991 | /* Interface address modification. */ |
67e3369e JU |
992 | static ssize_t netlink_address_msg_encoder(struct zebra_dplane_ctx *ctx, |
993 | void *buf, size_t buflen) | |
1fdc9eae | 994 | { |
d62a17ae | 995 | int bytelen; |
64168803 MS |
996 | const struct prefix *p; |
997 | int cmd; | |
998 | const char *label; | |
1fdc9eae | 999 | |
d62a17ae | 1000 | struct { |
1001 | struct nlmsghdr n; | |
1002 | struct ifaddrmsg ifa; | |
67e3369e JU |
1003 | char buf[0]; |
1004 | } *req = buf; | |
1005 | ||
1006 | if (buflen < sizeof(*req)) | |
1007 | return 0; | |
1fdc9eae | 1008 | |
64168803 | 1009 | p = dplane_ctx_get_intf_addr(ctx); |
67e3369e | 1010 | memset(req, 0, sizeof(*req)); |
1fdc9eae | 1011 | |
64168803 | 1012 | bytelen = (p->family == AF_INET ? 4 : 16); |
1fdc9eae | 1013 | |
67e3369e JU |
1014 | req->n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg)); |
1015 | req->n.nlmsg_flags = NLM_F_REQUEST; | |
a55ba23f | 1016 | |
64168803 MS |
1017 | if (dplane_ctx_get_op(ctx) == DPLANE_OP_ADDR_INSTALL) |
1018 | cmd = RTM_NEWADDR; | |
1019 | else | |
1020 | cmd = RTM_DELADDR; | |
1021 | ||
67e3369e JU |
1022 | req->n.nlmsg_type = cmd; |
1023 | req->ifa.ifa_family = p->family; | |
1fdc9eae | 1024 | |
67e3369e | 1025 | req->ifa.ifa_index = dplane_ctx_get_ifindex(ctx); |
1fdc9eae | 1026 | |
67e3369e JU |
1027 | if (!nl_attr_put(&req->n, buflen, IFA_LOCAL, &p->u.prefix, bytelen)) |
1028 | return 0; | |
1fdc9eae | 1029 | |
64168803 MS |
1030 | if (p->family == AF_INET) { |
1031 | if (dplane_ctx_intf_is_connected(ctx)) { | |
1032 | p = dplane_ctx_get_intf_dest(ctx); | |
67e3369e JU |
1033 | if (!nl_attr_put(&req->n, buflen, IFA_ADDRESS, |
1034 | &p->u.prefix, bytelen)) | |
1035 | return 0; | |
0f3af738 JW |
1036 | } else if (cmd == RTM_NEWADDR) { |
1037 | struct in_addr broad = { | |
1038 | .s_addr = ipv4_broadcast_addr(p->u.prefix4.s_addr, | |
1039 | p->prefixlen) | |
1040 | }; | |
67e3369e JU |
1041 | if (!nl_attr_put(&req->n, buflen, IFA_BROADCAST, &broad, |
1042 | bytelen)) | |
1043 | return 0; | |
d62a17ae | 1044 | } |
1045 | } | |
1fdc9eae | 1046 | |
64168803 | 1047 | /* p is now either address or destination/bcast addr */ |
67e3369e | 1048 | req->ifa.ifa_prefixlen = p->prefixlen; |
e8d19a05 | 1049 | |
64168803 | 1050 | if (dplane_ctx_intf_is_secondary(ctx)) |
67e3369e | 1051 | SET_FLAG(req->ifa.ifa_flags, IFA_F_SECONDARY); |
1fdc9eae | 1052 | |
64168803 MS |
1053 | if (dplane_ctx_intf_has_label(ctx)) { |
1054 | label = dplane_ctx_get_intf_label(ctx); | |
67e3369e JU |
1055 | if (!nl_attr_put(&req->n, buflen, IFA_LABEL, label, |
1056 | strlen(label) + 1)) | |
1057 | return 0; | |
64168803 | 1058 | } |
1fdc9eae | 1059 | |
67e3369e | 1060 | return NLMSG_ALIGN(req->n.nlmsg_len); |
e86b71f1 PG |
1061 | } |
1062 | ||
67e3369e JU |
1063 | enum netlink_msg_status |
1064 | netlink_put_address_update_msg(struct nl_batch *bth, | |
1065 | struct zebra_dplane_ctx *ctx) | |
1066 | { | |
1067 | return netlink_batch_add_msg(bth, ctx, netlink_address_msg_encoder, | |
1068 | false); | |
e86b71f1 PG |
1069 | } |
1070 | ||
2414abd3 | 1071 | int netlink_interface_addr(struct nlmsghdr *h, ns_id_t ns_id, int startup) |
1fdc9eae | 1072 | { |
d62a17ae | 1073 | int len; |
1074 | struct ifaddrmsg *ifa; | |
1075 | struct rtattr *tb[IFA_MAX + 1]; | |
1076 | struct interface *ifp; | |
1077 | void *addr; | |
1078 | void *broad; | |
d7c0a89a | 1079 | uint8_t flags = 0; |
d62a17ae | 1080 | char *label = NULL; |
1081 | struct zebra_ns *zns; | |
cde1af84 | 1082 | uint32_t metric = METRIC_MAX; |
9254efed | 1083 | uint32_t kernel_flags = 0; |
d62a17ae | 1084 | |
1085 | zns = zebra_ns_lookup(ns_id); | |
1086 | ifa = NLMSG_DATA(h); | |
1087 | ||
8a1b681c | 1088 | if (ifa->ifa_family != AF_INET && ifa->ifa_family != AF_INET6) { |
9df414fe | 1089 | flog_warn( |
e914ccbe | 1090 | EC_ZEBRA_UNKNOWN_FAMILY, |
87b5d1b0 DS |
1091 | "Invalid address family: %u received from kernel interface addr change: %s", |
1092 | ifa->ifa_family, nl_msg_type_to_str(h->nlmsg_type)); | |
d62a17ae | 1093 | return 0; |
8a1b681c | 1094 | } |
d62a17ae | 1095 | |
1096 | if (h->nlmsg_type != RTM_NEWADDR && h->nlmsg_type != RTM_DELADDR) | |
1097 | return 0; | |
1098 | ||
1099 | len = h->nlmsg_len - NLMSG_LENGTH(sizeof(struct ifaddrmsg)); | |
9bdf8618 | 1100 | if (len < 0) { |
15569c58 DA |
1101 | zlog_err( |
1102 | "%s: Message received from netlink is of a broken size: %d %zu", | |
1103 | __func__, h->nlmsg_len, | |
1104 | (size_t)NLMSG_LENGTH(sizeof(struct ifaddrmsg))); | |
d62a17ae | 1105 | return -1; |
9bdf8618 | 1106 | } |
d62a17ae | 1107 | |
0d6f7fd6 | 1108 | memset(tb, 0, sizeof(tb)); |
d62a17ae | 1109 | netlink_parse_rtattr(tb, IFA_MAX, IFA_RTA(ifa), len); |
1110 | ||
1111 | ifp = if_lookup_by_index_per_ns(zns, ifa->ifa_index); | |
1112 | if (ifp == NULL) { | |
af4c2728 | 1113 | flog_err( |
450971aa | 1114 | EC_LIB_INTERFACE, |
d62a17ae | 1115 | "netlink_interface_addr can't find interface by index %d", |
1116 | ifa->ifa_index); | |
1117 | return -1; | |
1118 | } | |
1119 | ||
9254efed DS |
1120 | /* Flags passed through */ |
1121 | if (tb[IFA_FLAGS]) | |
1122 | kernel_flags = *(int *)RTA_DATA(tb[IFA_FLAGS]); | |
1123 | else | |
1124 | kernel_flags = ifa->ifa_flags; | |
1125 | ||
d62a17ae | 1126 | if (IS_ZEBRA_DEBUG_KERNEL) /* remove this line to see initial ifcfg */ |
1127 | { | |
1128 | char buf[BUFSIZ]; | |
1129 | zlog_debug("netlink_interface_addr %s %s flags 0x%x:", | |
1130 | nl_msg_type_to_str(h->nlmsg_type), ifp->name, | |
9254efed | 1131 | kernel_flags); |
d62a17ae | 1132 | if (tb[IFA_LOCAL]) |
1133 | zlog_debug(" IFA_LOCAL %s/%d", | |
1134 | inet_ntop(ifa->ifa_family, | |
1135 | RTA_DATA(tb[IFA_LOCAL]), buf, | |
1136 | BUFSIZ), | |
1137 | ifa->ifa_prefixlen); | |
1138 | if (tb[IFA_ADDRESS]) | |
1139 | zlog_debug(" IFA_ADDRESS %s/%d", | |
1140 | inet_ntop(ifa->ifa_family, | |
1141 | RTA_DATA(tb[IFA_ADDRESS]), buf, | |
1142 | BUFSIZ), | |
1143 | ifa->ifa_prefixlen); | |
1144 | if (tb[IFA_BROADCAST]) | |
1145 | zlog_debug(" IFA_BROADCAST %s/%d", | |
1146 | inet_ntop(ifa->ifa_family, | |
1147 | RTA_DATA(tb[IFA_BROADCAST]), buf, | |
1148 | BUFSIZ), | |
1149 | ifa->ifa_prefixlen); | |
1150 | if (tb[IFA_LABEL] && strcmp(ifp->name, RTA_DATA(tb[IFA_LABEL]))) | |
1151 | zlog_debug(" IFA_LABEL %s", | |
1152 | (char *)RTA_DATA(tb[IFA_LABEL])); | |
1153 | ||
1154 | if (tb[IFA_CACHEINFO]) { | |
1155 | struct ifa_cacheinfo *ci = RTA_DATA(tb[IFA_CACHEINFO]); | |
1156 | zlog_debug(" IFA_CACHEINFO pref %d, valid %d", | |
1157 | ci->ifa_prefered, ci->ifa_valid); | |
1158 | } | |
1159 | } | |
1160 | ||
1161 | /* logic copied from iproute2/ip/ipaddress.c:print_addrinfo() */ | |
1162 | if (tb[IFA_LOCAL] == NULL) | |
1163 | tb[IFA_LOCAL] = tb[IFA_ADDRESS]; | |
1164 | if (tb[IFA_ADDRESS] == NULL) | |
1165 | tb[IFA_ADDRESS] = tb[IFA_LOCAL]; | |
1166 | ||
1167 | /* local interface address */ | |
1168 | addr = (tb[IFA_LOCAL] ? RTA_DATA(tb[IFA_LOCAL]) : NULL); | |
1169 | ||
1170 | /* is there a peer address? */ | |
1171 | if (tb[IFA_ADDRESS] | |
1172 | && memcmp(RTA_DATA(tb[IFA_ADDRESS]), RTA_DATA(tb[IFA_LOCAL]), | |
1173 | RTA_PAYLOAD(tb[IFA_ADDRESS]))) { | |
1174 | broad = RTA_DATA(tb[IFA_ADDRESS]); | |
1175 | SET_FLAG(flags, ZEBRA_IFA_PEER); | |
1176 | } else | |
1177 | /* seeking a broadcast address */ | |
1178 | broad = (tb[IFA_BROADCAST] ? RTA_DATA(tb[IFA_BROADCAST]) | |
1179 | : NULL); | |
1180 | ||
1181 | /* addr is primary key, SOL if we don't have one */ | |
1182 | if (addr == NULL) { | |
14a4d9d0 | 1183 | zlog_debug("%s: Local Interface Address is NULL for %s", |
1184 | __func__, ifp->name); | |
d62a17ae | 1185 | return -1; |
1186 | } | |
1187 | ||
1188 | /* Flags. */ | |
9254efed | 1189 | if (kernel_flags & IFA_F_SECONDARY) |
d62a17ae | 1190 | SET_FLAG(flags, ZEBRA_IFA_SECONDARY); |
1191 | ||
1192 | /* Label */ | |
1193 | if (tb[IFA_LABEL]) | |
1194 | label = (char *)RTA_DATA(tb[IFA_LABEL]); | |
1195 | ||
2e1cc436 | 1196 | if (label && strcmp(ifp->name, label) == 0) |
d62a17ae | 1197 | label = NULL; |
1198 | ||
cde1af84 AK |
1199 | if (tb[IFA_RT_PRIORITY]) |
1200 | metric = *(uint32_t *)RTA_DATA(tb[IFA_RT_PRIORITY]); | |
1201 | ||
d62a17ae | 1202 | /* Register interface address to the interface. */ |
1203 | if (ifa->ifa_family == AF_INET) { | |
930571d2 | 1204 | if (ifa->ifa_prefixlen > IPV4_MAX_BITLEN) { |
e17d9b2d | 1205 | zlog_err( |
87b5d1b0 DS |
1206 | "Invalid prefix length: %u received from kernel interface addr change: %s", |
1207 | ifa->ifa_prefixlen, | |
1208 | nl_msg_type_to_str(h->nlmsg_type)); | |
e17d9b2d | 1209 | return -1; |
930571d2 | 1210 | } |
20e879f9 | 1211 | |
d62a17ae | 1212 | if (h->nlmsg_type == RTM_NEWADDR) |
1213 | connected_add_ipv4(ifp, flags, (struct in_addr *)addr, | |
1214 | ifa->ifa_prefixlen, | |
cde1af84 AK |
1215 | (struct in_addr *)broad, label, |
1216 | metric); | |
20e879f9 MS |
1217 | else if (CHECK_FLAG(flags, ZEBRA_IFA_PEER)) { |
1218 | /* Delete with a peer address */ | |
1219 | connected_delete_ipv4( | |
1220 | ifp, flags, (struct in_addr *)addr, | |
1221 | ifa->ifa_prefixlen, broad); | |
1222 | } else | |
d62a17ae | 1223 | connected_delete_ipv4( |
1224 | ifp, flags, (struct in_addr *)addr, | |
fd267f08 | 1225 | ifa->ifa_prefixlen, NULL); |
d62a17ae | 1226 | } |
20e879f9 | 1227 | |
d62a17ae | 1228 | if (ifa->ifa_family == AF_INET6) { |
930571d2 | 1229 | if (ifa->ifa_prefixlen > IPV6_MAX_BITLEN) { |
e17d9b2d | 1230 | zlog_err( |
87b5d1b0 DS |
1231 | "Invalid prefix length: %u received from kernel interface addr change: %s", |
1232 | ifa->ifa_prefixlen, | |
1233 | nl_msg_type_to_str(h->nlmsg_type)); | |
e17d9b2d | 1234 | return -1; |
930571d2 | 1235 | } |
d62a17ae | 1236 | if (h->nlmsg_type == RTM_NEWADDR) { |
1237 | /* Only consider valid addresses; we'll not get a | |
1238 | * notification from | |
1239 | * the kernel till IPv6 DAD has completed, but at init | |
1240 | * time, Quagga | |
1241 | * does query for and will receive all addresses. | |
1242 | */ | |
9254efed | 1243 | if (!(kernel_flags |
d62a17ae | 1244 | & (IFA_F_DADFAILED | IFA_F_TENTATIVE))) |
60466a63 QY |
1245 | connected_add_ipv6(ifp, flags, |
1246 | (struct in6_addr *)addr, | |
60c0687a | 1247 | (struct in6_addr *)broad, |
cde1af84 AK |
1248 | ifa->ifa_prefixlen, label, |
1249 | metric); | |
d62a17ae | 1250 | } else |
1251 | connected_delete_ipv6(ifp, (struct in6_addr *)addr, | |
fd267f08 | 1252 | NULL, ifa->ifa_prefixlen); |
d62a17ae | 1253 | } |
1254 | ||
2a181147 SW |
1255 | |
1256 | /* | |
1257 | * Linux kernel does not send route delete on interface down/addr del | |
1258 | * so we have to re-process routes it owns (i.e. kernel routes) | |
1259 | */ | |
1260 | if (h->nlmsg_type != RTM_NEWADDR) | |
1261 | rib_update(RIB_UPDATE_KERNEL); | |
1262 | ||
d62a17ae | 1263 | return 0; |
1fdc9eae | 1264 | } |
1265 | ||
2414abd3 | 1266 | int netlink_link_change(struct nlmsghdr *h, ns_id_t ns_id, int startup) |
1fdc9eae | 1267 | { |
d62a17ae | 1268 | int len; |
1269 | struct ifinfomsg *ifi; | |
1270 | struct rtattr *tb[IFLA_MAX + 1]; | |
1271 | struct rtattr *linkinfo[IFLA_MAX + 1]; | |
1272 | struct interface *ifp; | |
1273 | char *name = NULL; | |
1274 | char *kind = NULL; | |
48884c6b | 1275 | char *desc = NULL; |
d62a17ae | 1276 | char *slave_kind = NULL; |
1277 | struct zebra_ns *zns; | |
1278 | vrf_id_t vrf_id = VRF_DEFAULT; | |
1279 | zebra_iftype_t zif_type = ZEBRA_IF_OTHER; | |
1280 | zebra_slave_iftype_t zif_slave_type = ZEBRA_IF_SLAVE_NONE; | |
1281 | ifindex_t bridge_ifindex = IFINDEX_INTERNAL; | |
b9368db9 | 1282 | ifindex_t bond_ifindex = IFINDEX_INTERNAL; |
d62a17ae | 1283 | ifindex_t link_ifindex = IFINDEX_INTERNAL; |
97c4e1d0 | 1284 | uint8_t old_hw_addr[INTERFACE_HWADDR_MAX]; |
ba5165ec | 1285 | struct zebra_if *zif; |
14ddb3d9 | 1286 | ns_id_t link_nsid = ns_id; |
d62a17ae | 1287 | |
1288 | zns = zebra_ns_lookup(ns_id); | |
1289 | ifi = NLMSG_DATA(h); | |
1290 | ||
fe533c56 | 1291 | /* assume if not default zns, then new VRF */ |
d62a17ae | 1292 | if (!(h->nlmsg_type == RTM_NEWLINK || h->nlmsg_type == RTM_DELLINK)) { |
1293 | /* If this is not link add/delete message so print warning. */ | |
87b5d1b0 DS |
1294 | zlog_debug("netlink_link_change: wrong kernel message %s", |
1295 | nl_msg_type_to_str(h->nlmsg_type)); | |
d62a17ae | 1296 | return 0; |
1297 | } | |
1298 | ||
8a1b681c SW |
1299 | if (!(ifi->ifi_family == AF_UNSPEC || ifi->ifi_family == AF_BRIDGE |
1300 | || ifi->ifi_family == AF_INET6)) { | |
9df414fe | 1301 | flog_warn( |
e914ccbe | 1302 | EC_ZEBRA_UNKNOWN_FAMILY, |
87b5d1b0 DS |
1303 | "Invalid address family: %u received from kernel link change: %s", |
1304 | ifi->ifi_family, nl_msg_type_to_str(h->nlmsg_type)); | |
8a1b681c SW |
1305 | return 0; |
1306 | } | |
1307 | ||
d62a17ae | 1308 | len = h->nlmsg_len - NLMSG_LENGTH(sizeof(struct ifinfomsg)); |
9bdf8618 | 1309 | if (len < 0) { |
15569c58 DA |
1310 | zlog_err( |
1311 | "%s: Message received from netlink is of a broken size %d %zu", | |
1312 | __func__, h->nlmsg_len, | |
1313 | (size_t)NLMSG_LENGTH(sizeof(struct ifinfomsg))); | |
d62a17ae | 1314 | return -1; |
9bdf8618 | 1315 | } |
d62a17ae | 1316 | |
1317 | /* We are interested in some AF_BRIDGE notifications. */ | |
1318 | if (ifi->ifi_family == AF_BRIDGE) | |
1319 | return netlink_bridge_interface(h, len, ns_id, startup); | |
1320 | ||
1321 | /* Looking up interface name. */ | |
0d6f7fd6 DA |
1322 | memset(tb, 0, sizeof(tb)); |
1323 | memset(linkinfo, 0, sizeof(linkinfo)); | |
d62a17ae | 1324 | netlink_parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), len); |
1fdc9eae | 1325 | |
d62a17ae | 1326 | /* check for wireless messages to ignore */ |
1327 | if ((tb[IFLA_WIRELESS] != NULL) && (ifi->ifi_change == 0)) { | |
1328 | if (IS_ZEBRA_DEBUG_KERNEL) | |
1329 | zlog_debug("%s: ignoring IFLA_WIRELESS message", | |
1330 | __func__); | |
1331 | return 0; | |
1332 | } | |
1fdc9eae | 1333 | |
d62a17ae | 1334 | if (tb[IFLA_IFNAME] == NULL) |
1335 | return -1; | |
1336 | name = (char *)RTA_DATA(tb[IFLA_IFNAME]); | |
1fdc9eae | 1337 | |
d62a17ae | 1338 | if (tb[IFLA_LINKINFO]) { |
1339 | parse_rtattr_nested(linkinfo, IFLA_INFO_MAX, tb[IFLA_LINKINFO]); | |
1fdc9eae | 1340 | |
d62a17ae | 1341 | if (linkinfo[IFLA_INFO_KIND]) |
1342 | kind = RTA_DATA(linkinfo[IFLA_INFO_KIND]); | |
1fdc9eae | 1343 | |
d62a17ae | 1344 | if (linkinfo[IFLA_INFO_SLAVE_KIND]) |
1345 | slave_kind = RTA_DATA(linkinfo[IFLA_INFO_SLAVE_KIND]); | |
1fdc9eae | 1346 | |
d62a17ae | 1347 | netlink_determine_zebra_iftype(kind, &zif_type); |
1348 | } | |
6675513d | 1349 | |
d62a17ae | 1350 | /* If linking to another interface, note it. */ |
1351 | if (tb[IFLA_LINK]) | |
1352 | link_ifindex = *(ifindex_t *)RTA_DATA(tb[IFLA_LINK]); | |
1fdc9eae | 1353 | |
b1cc23b2 | 1354 | if (tb[IFLA_LINK_NETNSID]) { |
14ddb3d9 | 1355 | link_nsid = *(ns_id_t *)RTA_DATA(tb[IFLA_LINK_NETNSID]); |
b1cc23b2 PG |
1356 | link_nsid = ns_id_get_absolute(ns_id, link_nsid); |
1357 | } | |
48884c6b DS |
1358 | if (tb[IFLA_IFALIAS]) { |
1359 | desc = (char *)RTA_DATA(tb[IFLA_IFALIAS]); | |
1360 | } | |
1361 | ||
d62a17ae | 1362 | /* If VRF, create or update the VRF structure itself. */ |
78dd30b2 | 1363 | if (zif_type == ZEBRA_IF_VRF && !vrf_is_backend_netns()) { |
5e031198 | 1364 | netlink_vrf_change(h, tb[IFLA_LINKINFO], ns_id, name); |
d62a17ae | 1365 | vrf_id = (vrf_id_t)ifi->ifi_index; |
1366 | } | |
1fdc9eae | 1367 | |
d62a17ae | 1368 | /* See if interface is present. */ |
1369 | ifp = if_lookup_by_name_per_ns(zns, name); | |
1370 | ||
1371 | if (h->nlmsg_type == RTM_NEWLINK) { | |
1372 | if (tb[IFLA_MASTER]) { | |
78dd30b2 PG |
1373 | if (slave_kind && (strcmp(slave_kind, "vrf") == 0) |
1374 | && !vrf_is_backend_netns()) { | |
d62a17ae | 1375 | zif_slave_type = ZEBRA_IF_SLAVE_VRF; |
d7c0a89a | 1376 | vrf_id = *(uint32_t *)RTA_DATA(tb[IFLA_MASTER]); |
d62a17ae | 1377 | } else if (slave_kind |
1378 | && (strcmp(slave_kind, "bridge") == 0)) { | |
1379 | zif_slave_type = ZEBRA_IF_SLAVE_BRIDGE; | |
1380 | bridge_ifindex = | |
1381 | *(ifindex_t *)RTA_DATA(tb[IFLA_MASTER]); | |
b9368db9 DD |
1382 | } else if (slave_kind |
1383 | && (strcmp(slave_kind, "bond") == 0)) { | |
1384 | zif_slave_type = ZEBRA_IF_SLAVE_BOND; | |
1385 | bond_ifindex = | |
1386 | *(ifindex_t *)RTA_DATA(tb[IFLA_MASTER]); | |
d62a17ae | 1387 | } else |
1388 | zif_slave_type = ZEBRA_IF_SLAVE_OTHER; | |
1389 | } | |
fe533c56 PG |
1390 | if (vrf_is_backend_netns()) |
1391 | vrf_id = (vrf_id_t)ns_id; | |
d62a17ae | 1392 | if (ifp == NULL |
1393 | || !CHECK_FLAG(ifp->status, ZEBRA_INTERFACE_ACTIVE)) { | |
1394 | /* Add interface notification from kernel */ | |
1395 | if (IS_ZEBRA_DEBUG_KERNEL) | |
1396 | zlog_debug( | |
3efd0893 | 1397 | "RTM_NEWLINK ADD for %s(%u) vrf_id %u type %d sl_type %d master %u flags 0x%x", |
d62a17ae | 1398 | name, ifi->ifi_index, vrf_id, zif_type, |
1399 | zif_slave_type, bridge_ifindex, | |
1400 | ifi->ifi_flags); | |
1401 | ||
1402 | if (ifp == NULL) { | |
1403 | /* unknown interface */ | |
a36898e7 | 1404 | ifp = if_get_by_name(name, vrf_id); |
d62a17ae | 1405 | } else { |
1406 | /* pre-configured interface, learnt now */ | |
a36898e7 DS |
1407 | if (ifp->vrf_id != vrf_id) |
1408 | if_update_to_new_vrf(ifp, vrf_id); | |
d62a17ae | 1409 | } |
1410 | ||
1411 | /* Update interface information. */ | |
1412 | set_ifindex(ifp, ifi->ifi_index, zns); | |
1413 | ifp->flags = ifi->ifi_flags & 0x0000fffff; | |
d23b983b | 1414 | if (!tb[IFLA_MTU]) { |
9df414fe | 1415 | zlog_debug( |
d23b983b SW |
1416 | "RTM_NEWLINK for interface %s(%u) without MTU set", |
1417 | name, ifi->ifi_index); | |
1418 | return 0; | |
1419 | } | |
d62a17ae | 1420 | ifp->mtu6 = ifp->mtu = *(int *)RTA_DATA(tb[IFLA_MTU]); |
1421 | ifp->metric = 0; | |
1422 | ifp->ptm_status = ZEBRA_PTM_STATUS_UNKNOWN; | |
1423 | ||
1424 | /* Set interface type */ | |
1425 | zebra_if_set_ziftype(ifp, zif_type, zif_slave_type); | |
b0fa6f6a CS |
1426 | if (IS_ZEBRA_IF_VRF(ifp)) |
1427 | SET_FLAG(ifp->status, | |
1428 | ZEBRA_INTERFACE_VRF_LOOPBACK); | |
d62a17ae | 1429 | |
1430 | /* Update link. */ | |
680c278f | 1431 | zebra_if_update_link(ifp, link_ifindex, ns_id); |
d62a17ae | 1432 | |
1433 | netlink_interface_update_hw_addr(tb, ifp); | |
1434 | ||
1435 | /* Inform clients, install any configured addresses. */ | |
1436 | if_add_update(ifp); | |
1437 | ||
1438 | /* Extract and save L2 interface information, take | |
1439 | * additional actions. */ | |
1440 | netlink_interface_update_l2info( | |
14ddb3d9 PG |
1441 | ifp, linkinfo[IFLA_INFO_DATA], |
1442 | 1, link_nsid); | |
d62a17ae | 1443 | if (IS_ZEBRA_IF_BRIDGE_SLAVE(ifp)) |
1444 | zebra_l2if_update_bridge_slave(ifp, | |
a6e0edf2 PG |
1445 | bridge_ifindex, |
1446 | ns_id); | |
b9368db9 DD |
1447 | else if (IS_ZEBRA_IF_BOND_SLAVE(ifp)) |
1448 | zebra_l2if_update_bond_slave(ifp, bond_ifindex); | |
a36898e7 | 1449 | } else if (ifp->vrf_id != vrf_id) { |
d62a17ae | 1450 | /* VRF change for an interface. */ |
1451 | if (IS_ZEBRA_DEBUG_KERNEL) | |
1452 | zlog_debug( | |
3efd0893 | 1453 | "RTM_NEWLINK vrf-change for %s(%u) vrf_id %u -> %u flags 0x%x", |
a36898e7 DS |
1454 | name, ifp->ifindex, ifp->vrf_id, vrf_id, |
1455 | ifi->ifi_flags); | |
d62a17ae | 1456 | |
a36898e7 | 1457 | if_handle_vrf_change(ifp, vrf_id); |
d62a17ae | 1458 | } else { |
b9368db9 | 1459 | bool was_bridge_slave, was_bond_slave; |
d62a17ae | 1460 | |
1461 | /* Interface update. */ | |
1462 | if (IS_ZEBRA_DEBUG_KERNEL) | |
1463 | zlog_debug( | |
3efd0893 | 1464 | "RTM_NEWLINK update for %s(%u) sl_type %d master %u flags 0x%x", |
d62a17ae | 1465 | name, ifp->ifindex, zif_slave_type, |
1466 | bridge_ifindex, ifi->ifi_flags); | |
1467 | ||
1468 | set_ifindex(ifp, ifi->ifi_index, zns); | |
d23b983b | 1469 | if (!tb[IFLA_MTU]) { |
9df414fe | 1470 | zlog_debug( |
d23b983b SW |
1471 | "RTM_NEWLINK for interface %s(%u) without MTU set", |
1472 | name, ifi->ifi_index); | |
1473 | return 0; | |
1474 | } | |
d62a17ae | 1475 | ifp->mtu6 = ifp->mtu = *(int *)RTA_DATA(tb[IFLA_MTU]); |
1476 | ifp->metric = 0; | |
1477 | ||
1478 | /* Update interface type - NOTE: Only slave_type can | |
1479 | * change. */ | |
1480 | was_bridge_slave = IS_ZEBRA_IF_BRIDGE_SLAVE(ifp); | |
b9368db9 | 1481 | was_bond_slave = IS_ZEBRA_IF_BOND_SLAVE(ifp); |
d62a17ae | 1482 | zebra_if_set_ziftype(ifp, zif_type, zif_slave_type); |
1483 | ||
97c4e1d0 CS |
1484 | memcpy(old_hw_addr, ifp->hw_addr, INTERFACE_HWADDR_MAX); |
1485 | ||
d62a17ae | 1486 | netlink_interface_update_hw_addr(tb, ifp); |
1487 | ||
1488 | if (if_is_no_ptm_operative(ifp)) { | |
1489 | ifp->flags = ifi->ifi_flags & 0x0000fffff; | |
1490 | if (!if_is_no_ptm_operative(ifp)) { | |
1491 | if (IS_ZEBRA_DEBUG_KERNEL) | |
1492 | zlog_debug( | |
1493 | "Intf %s(%u) has gone DOWN", | |
1494 | name, ifp->ifindex); | |
1495 | if_down(ifp); | |
2a181147 | 1496 | rib_update(RIB_UPDATE_KERNEL); |
d62a17ae | 1497 | } else if (if_is_operative(ifp)) { |
1498 | /* Must notify client daemons of new | |
1499 | * interface status. */ | |
1500 | if (IS_ZEBRA_DEBUG_KERNEL) | |
1501 | zlog_debug( | |
1502 | "Intf %s(%u) PTM up, notifying clients", | |
1503 | name, ifp->ifindex); | |
1504 | zebra_interface_up_update(ifp); | |
97c4e1d0 CS |
1505 | |
1506 | /* Update EVPN VNI when SVI MAC change | |
1507 | */ | |
1508 | if (IS_ZEBRA_IF_VLAN(ifp) && | |
1509 | memcmp(old_hw_addr, ifp->hw_addr, | |
1510 | INTERFACE_HWADDR_MAX)) { | |
1511 | struct interface *link_if; | |
1512 | ||
1513 | link_if = | |
1514 | if_lookup_by_index_per_ns( | |
1515 | zebra_ns_lookup(NS_DEFAULT), | |
1516 | link_ifindex); | |
1517 | if (link_if) | |
1518 | zebra_vxlan_svi_up(ifp, | |
1519 | link_if); | |
1520 | } | |
d62a17ae | 1521 | } |
1522 | } else { | |
1523 | ifp->flags = ifi->ifi_flags & 0x0000fffff; | |
1524 | if (if_is_operative(ifp)) { | |
1525 | if (IS_ZEBRA_DEBUG_KERNEL) | |
1526 | zlog_debug( | |
1527 | "Intf %s(%u) has come UP", | |
1528 | name, ifp->ifindex); | |
1529 | if_up(ifp); | |
6f908ded QY |
1530 | } else { |
1531 | if (IS_ZEBRA_DEBUG_KERNEL) | |
1532 | zlog_debug( | |
7913714e | 1533 | "Intf %s(%u) has gone DOWN", |
6f908ded QY |
1534 | name, ifp->ifindex); |
1535 | if_down(ifp); | |
2a181147 | 1536 | rib_update(RIB_UPDATE_KERNEL); |
d62a17ae | 1537 | } |
1538 | } | |
1539 | ||
1540 | /* Extract and save L2 interface information, take | |
1541 | * additional actions. */ | |
1542 | netlink_interface_update_l2info( | |
14ddb3d9 PG |
1543 | ifp, linkinfo[IFLA_INFO_DATA], |
1544 | 0, link_nsid); | |
d62a17ae | 1545 | if (IS_ZEBRA_IF_BRIDGE_SLAVE(ifp) || was_bridge_slave) |
1546 | zebra_l2if_update_bridge_slave(ifp, | |
a6e0edf2 PG |
1547 | bridge_ifindex, |
1548 | ns_id); | |
b9368db9 DD |
1549 | else if (IS_ZEBRA_IF_BOND_SLAVE(ifp) || was_bond_slave) |
1550 | zebra_l2if_update_bond_slave(ifp, bond_ifindex); | |
d62a17ae | 1551 | } |
26f13577 DS |
1552 | |
1553 | zif = ifp->info; | |
1554 | if (zif) { | |
1555 | XFREE(MTYPE_TMP, zif->desc); | |
1556 | if (desc) | |
1557 | zif->desc = XSTRDUP(MTYPE_TMP, desc); | |
1558 | } | |
d62a17ae | 1559 | } else { |
1560 | /* Delete interface notification from kernel */ | |
1561 | if (ifp == NULL) { | |
9df414fe QY |
1562 | if (IS_ZEBRA_DEBUG_KERNEL) |
1563 | zlog_debug( | |
1564 | "RTM_DELLINK for unknown interface %s(%u)", | |
1565 | name, ifi->ifi_index); | |
d62a17ae | 1566 | return 0; |
1567 | } | |
1568 | ||
1569 | if (IS_ZEBRA_DEBUG_KERNEL) | |
1570 | zlog_debug("RTM_DELLINK for %s(%u)", name, | |
1571 | ifp->ifindex); | |
1572 | ||
1573 | UNSET_FLAG(ifp->status, ZEBRA_INTERFACE_VRF_LOOPBACK); | |
1574 | ||
1575 | /* Special handling for bridge or VxLAN interfaces. */ | |
1576 | if (IS_ZEBRA_IF_BRIDGE(ifp)) | |
1577 | zebra_l2_bridge_del(ifp); | |
1578 | else if (IS_ZEBRA_IF_VXLAN(ifp)) | |
1579 | zebra_l2_vxlanif_del(ifp); | |
1580 | ||
1581 | if (!IS_ZEBRA_IF_VRF(ifp)) | |
1582 | if_delete_update(ifp); | |
1fdc9eae | 1583 | } |
1584 | ||
d62a17ae | 1585 | return 0; |
1fdc9eae | 1586 | } |
718e3744 | 1587 | |
c3bd894e QY |
1588 | int netlink_protodown(struct interface *ifp, bool down) |
1589 | { | |
1590 | struct zebra_ns *zns = zebra_ns_lookup(NS_DEFAULT); | |
1591 | ||
1592 | struct { | |
1593 | struct nlmsghdr n; | |
1594 | struct ifinfomsg ifa; | |
1595 | char buf[NL_PKT_BUF_SIZE]; | |
1596 | } req; | |
1597 | ||
2fff50ec | 1598 | memset(&req, 0, sizeof(req)); |
c3bd894e QY |
1599 | |
1600 | req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg)); | |
1601 | req.n.nlmsg_flags = NLM_F_REQUEST; | |
1602 | req.n.nlmsg_type = RTM_SETLINK; | |
1603 | req.n.nlmsg_pid = zns->netlink_cmd.snl.nl_pid; | |
1604 | ||
1605 | req.ifa.ifi_index = ifp->ifindex; | |
1606 | ||
312a6bee | 1607 | nl_attr_put(&req.n, sizeof(req), IFLA_PROTO_DOWN, &down, sizeof(down)); |
a757997c | 1608 | nl_attr_put32(&req.n, sizeof(req), IFLA_LINK, ifp->ifindex); |
c3bd894e QY |
1609 | |
1610 | return netlink_talk(netlink_talk_filter, &req.n, &zns->netlink_cmd, zns, | |
1611 | 0); | |
1612 | } | |
1613 | ||
718e3744 | 1614 | /* Interface information read by netlink. */ |
d62a17ae | 1615 | void interface_list(struct zebra_ns *zns) |
718e3744 | 1616 | { |
d62a17ae | 1617 | interface_lookup_netlink(zns); |
d9f5b2f5 SW |
1618 | /* We add routes for interface address, |
1619 | * so we need to get the nexthop info | |
1620 | * from the kernel before we can do that | |
1621 | */ | |
81505946 | 1622 | netlink_nexthop_read(zns); |
cc4e0650 | 1623 | |
d2bec88a | 1624 | interface_addr_lookup_netlink(zns); |
718e3744 | 1625 | } |
ddfeb486 DL |
1626 | |
1627 | #endif /* GNU_LINUX */ |