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