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