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