]>
Commit | Line | Data |
---|---|---|
718e3744 | 1 | /* |
2 | * Interface looking up by netlink. | |
3 | * Copyright (C) 1998 Kunihiro Ishiguro | |
4 | * | |
5 | * This file is part of GNU Zebra. | |
6 | * | |
7 | * GNU Zebra is free software; you can redistribute it and/or modify it | |
8 | * under the terms of the GNU General Public License as published by the | |
9 | * Free Software Foundation; either version 2, or (at your option) any | |
10 | * later version. | |
11 | * | |
12 | * GNU Zebra is distributed in the hope that it will be useful, but | |
13 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 | * General Public License for more details. | |
16 | * | |
896014f4 DL |
17 | * You should have received a copy of the GNU General Public License along |
18 | * with this program; see the file COPYING; if not, write to the Free Software | |
19 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
718e3744 | 20 | */ |
21 | ||
22 | #include <zebra.h> | |
6675513d | 23 | |
ddfeb486 DL |
24 | #ifdef GNU_LINUX |
25 | ||
6675513d | 26 | /* The following definition is to workaround an issue in the Linux kernel |
27 | * header files with redefinition of 'struct in6_addr' in both | |
28 | * netinet/in.h and linux/in6.h. | |
29 | * Reference - https://sourceware.org/ml/libc-alpha/2013-01/msg00599.html | |
30 | */ | |
31 | #define _LINUX_IN6_H | |
32 | ||
ba85366a | 33 | #include <netinet/if_ether.h> |
6675513d | 34 | #include <linux/if_bridge.h> |
ba777396 | 35 | #include <linux/if_link.h> |
1fdc9eae | 36 | #include <net/if_arp.h> |
535fe877 DS |
37 | #include <linux/sockios.h> |
38 | #include <linux/ethtool.h> | |
1fdc9eae | 39 | |
40 | #include "linklist.h" | |
41 | #include "if.h" | |
42 | #include "log.h" | |
43 | #include "prefix.h" | |
44 | #include "connected.h" | |
45 | #include "table.h" | |
46 | #include "memory.h" | |
47 | #include "zebra_memory.h" | |
48 | #include "rib.h" | |
49 | #include "thread.h" | |
50 | #include "privs.h" | |
51 | #include "nexthop.h" | |
52 | #include "vrf.h" | |
7922fc65 | 53 | #include "vrf_int.h" |
1fdc9eae | 54 | #include "mpls.h" |
174482ef | 55 | #include "lib_errors.h" |
718e3744 | 56 | |
5e6a74d8 | 57 | #include "vty.h" |
7dbeea9d | 58 | #include "zebra/zserv.h" |
1fdc9eae | 59 | #include "zebra/zebra_ns.h" |
60 | #include "zebra/zebra_vrf.h" | |
61 | #include "zebra/rt.h" | |
62 | #include "zebra/redistribute.h" | |
63 | #include "zebra/interface.h" | |
64 | #include "zebra/debug.h" | |
65 | #include "zebra/rtadv.h" | |
66 | #include "zebra/zebra_ptm.h" | |
67 | #include "zebra/zebra_mpls.h" | |
68 | #include "zebra/kernel_netlink.h" | |
69 | #include "zebra/if_netlink.h" | |
9df414fe | 70 | #include "zebra/zebra_errors.h" |
1fdc9eae | 71 | |
0268f30e | 72 | extern struct zebra_privs_t zserv_privs; |
1fdc9eae | 73 | |
74 | /* Note: on netlink systems, there should be a 1-to-1 mapping between interface | |
75 | names and ifindex values. */ | |
d62a17ae | 76 | static void set_ifindex(struct interface *ifp, ifindex_t ifi_index, |
77 | struct zebra_ns *zns) | |
1fdc9eae | 78 | { |
d62a17ae | 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) | |
af4c2728 | 84 | flog_err( |
4d43f68a DS |
85 | LIB_ERR_INTERFACE, |
86 | "Netlink is setting interface %s ifindex to reserved internal value %u", | |
d62a17ae | 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)) | |
af4c2728 | 94 | flog_err( |
4d43f68a DS |
95 | LIB_ERR_INTERFACE, |
96 | "interface rename detected on up interface: index %d was renamed from %s to %s, results are uncertain!", | |
d62a17ae | 97 | ifi_index, oifp->name, ifp->name); |
98 | if_delete_update(oifp); | |
99 | } | |
100 | } | |
ff880b78 | 101 | if_set_index(ifp, ifi_index); |
1fdc9eae | 102 | } |
103 | ||
104 | /* Utility function to parse hardware link-layer address and update ifp */ | |
d62a17ae | 105 | static void netlink_interface_update_hw_addr(struct rtattr **tb, |
106 | struct interface *ifp) | |
1fdc9eae | 107 | { |
d62a17ae | 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) | |
9df414fe QY |
116 | zlog_debug("Hardware address is too large: %d", |
117 | hw_addr_len); | |
d62a17ae | 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 | } | |
1fdc9eae | 133 | } |
134 | ||
d62a17ae | 135 | static enum zebra_link_type netlink_to_zebra_link_type(unsigned int hwt) |
1fdc9eae | 136 | { |
d62a17ae | 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; | |
4268e09e | 228 | #ifdef ARPHRD_IEEE802154 |
d62a17ae | 229 | case ARPHRD_IEEE802154: |
230 | return ZEBRA_LLT_IEEE802154; | |
4268e09e | 231 | #endif |
1fdc9eae | 232 | #ifdef ARPHRD_IP6GRE |
d62a17ae | 233 | case ARPHRD_IP6GRE: |
234 | return ZEBRA_LLT_IP6GRE; | |
1fdc9eae | 235 | #endif |
236 | #ifdef ARPHRD_IEEE802154_PHY | |
d62a17ae | 237 | case ARPHRD_IEEE802154_PHY: |
238 | return ZEBRA_LLT_IEEE802154_PHY; | |
1fdc9eae | 239 | #endif |
240 | ||
d62a17ae | 241 | default: |
242 | return ZEBRA_LLT_UNKNOWN; | |
243 | } | |
1fdc9eae | 244 | } |
245 | ||
d62a17ae | 246 | static void netlink_determine_zebra_iftype(char *kind, zebra_iftype_t *zif_type) |
6675513d | 247 | { |
d62a17ae | 248 | *zif_type = ZEBRA_IF_OTHER; |
249 | ||
250 | if (!kind) | |
251 | return; | |
252 | ||
253 | if (strcmp(kind, "vrf") == 0) | |
254 | *zif_type = ZEBRA_IF_VRF; | |
255 | else if (strcmp(kind, "bridge") == 0) | |
256 | *zif_type = ZEBRA_IF_BRIDGE; | |
257 | else if (strcmp(kind, "vlan") == 0) | |
258 | *zif_type = ZEBRA_IF_VLAN; | |
259 | else if (strcmp(kind, "vxlan") == 0) | |
260 | *zif_type = ZEBRA_IF_VXLAN; | |
1a98c087 MK |
261 | else if (strcmp(kind, "macvlan") == 0) |
262 | *zif_type = ZEBRA_IF_MACVLAN; | |
0e4864ea PG |
263 | else if (strcmp(kind, "veth") == 0) |
264 | *zif_type = ZEBRA_IF_VETH; | |
6675513d | 265 | } |
52d8f0d8 | 266 | |
d62a17ae | 267 | #define parse_rtattr_nested(tb, max, rta) \ |
268 | netlink_parse_rtattr((tb), (max), RTA_DATA(rta), RTA_PAYLOAD(rta)) | |
1fdc9eae | 269 | |
d62a17ae | 270 | static void netlink_vrf_change(struct nlmsghdr *h, struct rtattr *tb, |
271 | const char *name) | |
1fdc9eae | 272 | { |
d62a17ae | 273 | struct ifinfomsg *ifi; |
274 | struct rtattr *linkinfo[IFLA_INFO_MAX + 1]; | |
275 | struct rtattr *attr[IFLA_VRF_MAX + 1]; | |
276 | struct vrf *vrf; | |
277 | struct zebra_vrf *zvrf; | |
d7c0a89a | 278 | uint32_t nl_table_id; |
d62a17ae | 279 | |
280 | ifi = NLMSG_DATA(h); | |
281 | ||
282 | memset(linkinfo, 0, sizeof linkinfo); | |
283 | parse_rtattr_nested(linkinfo, IFLA_INFO_MAX, tb); | |
284 | ||
285 | if (!linkinfo[IFLA_INFO_DATA]) { | |
286 | if (IS_ZEBRA_DEBUG_KERNEL) | |
287 | zlog_debug( | |
288 | "%s: IFLA_INFO_DATA missing from VRF message: %s", | |
289 | __func__, name); | |
290 | return; | |
291 | } | |
292 | ||
293 | memset(attr, 0, sizeof attr); | |
294 | parse_rtattr_nested(attr, IFLA_VRF_MAX, linkinfo[IFLA_INFO_DATA]); | |
295 | if (!attr[IFLA_VRF_TABLE]) { | |
296 | if (IS_ZEBRA_DEBUG_KERNEL) | |
297 | zlog_debug( | |
298 | "%s: IFLA_VRF_TABLE missing from VRF message: %s", | |
299 | __func__, name); | |
300 | return; | |
1fdc9eae | 301 | } |
302 | ||
d7c0a89a | 303 | nl_table_id = *(uint32_t *)RTA_DATA(attr[IFLA_VRF_TABLE]); |
d62a17ae | 304 | |
305 | if (h->nlmsg_type == RTM_NEWLINK) { | |
306 | if (IS_ZEBRA_DEBUG_KERNEL) | |
307 | zlog_debug("RTM_NEWLINK for VRF %s(%u) table %u", name, | |
308 | ifi->ifi_index, nl_table_id); | |
309 | ||
310 | /* | |
311 | * vrf_get is implied creation if it does not exist | |
312 | */ | |
313 | vrf = vrf_get((vrf_id_t)ifi->ifi_index, | |
314 | name); // It would create vrf | |
315 | if (!vrf) { | |
af4c2728 | 316 | flog_err(LIB_ERR_INTERFACE, "VRF %s id %u not created", |
4d43f68a | 317 | name, ifi->ifi_index); |
d62a17ae | 318 | return; |
319 | } | |
320 | ||
d62a17ae | 321 | /* |
322 | * This is the only place that we get the actual kernel table_id | |
323 | * being used. We need it to set the table_id of the routes | |
324 | * we are passing to the kernel.... And to throw some totally | |
325 | * awesome parties. that too. | |
593406a1 DS |
326 | * |
327 | * At this point we *must* have a zvrf because the vrf_create | |
328 | * callback creates one. We *must* set the table id | |
329 | * before the vrf_enable because of( at the very least ) | |
330 | * static routes being delayed for installation until | |
331 | * during the vrf_enable callbacks. | |
d62a17ae | 332 | */ |
333 | zvrf = (struct zebra_vrf *)vrf->info; | |
334 | zvrf->table_id = nl_table_id; | |
593406a1 DS |
335 | |
336 | /* Enable the created VRF. */ | |
337 | if (!vrf_enable(vrf)) { | |
af4c2728 | 338 | flog_err(LIB_ERR_INTERFACE, |
4d43f68a DS |
339 | "Failed to enable VRF %s id %u", name, |
340 | ifi->ifi_index); | |
593406a1 DS |
341 | return; |
342 | } | |
343 | ||
d62a17ae | 344 | } else // h->nlmsg_type == RTM_DELLINK |
345 | { | |
346 | if (IS_ZEBRA_DEBUG_KERNEL) | |
347 | zlog_debug("RTM_DELLINK for VRF %s(%u)", name, | |
348 | ifi->ifi_index); | |
349 | ||
350 | vrf = vrf_lookup_by_id((vrf_id_t)ifi->ifi_index); | |
351 | ||
352 | if (!vrf) { | |
e914ccbe | 353 | flog_warn(EC_ZEBRA_VRF_NOT_FOUND, "%s: vrf not found", |
9df414fe | 354 | __func__); |
d62a17ae | 355 | return; |
356 | } | |
357 | ||
358 | vrf_delete(vrf); | |
359 | } | |
1fdc9eae | 360 | } |
361 | ||
0268f30e | 362 | static int get_iflink_speed(struct interface *interface) |
535fe877 | 363 | { |
d62a17ae | 364 | struct ifreq ifdata; |
365 | struct ethtool_cmd ecmd; | |
366 | int sd; | |
367 | int rc; | |
0268f30e | 368 | const char *ifname = interface->name; |
d62a17ae | 369 | |
370 | /* initialize struct */ | |
371 | memset(&ifdata, 0, sizeof(ifdata)); | |
372 | ||
373 | /* set interface name */ | |
0af35d90 | 374 | strlcpy(ifdata.ifr_name, ifname, sizeof(ifdata.ifr_name)); |
d62a17ae | 375 | |
376 | /* initialize ethtool interface */ | |
377 | memset(&ecmd, 0, sizeof(ecmd)); | |
378 | ecmd.cmd = ETHTOOL_GSET; /* ETHTOOL_GLINK */ | |
ba85366a | 379 | ifdata.ifr_data = (caddr_t)&ecmd; |
d62a17ae | 380 | |
381 | /* use ioctl to get IP address of an interface */ | |
01b9e3fd DL |
382 | frr_elevate_privs(&zserv_privs) { |
383 | sd = vrf_socket(PF_INET, SOCK_DGRAM, IPPROTO_IP, | |
384 | interface->vrf_id, | |
385 | NULL); | |
386 | if (sd < 0) { | |
387 | if (IS_ZEBRA_DEBUG_KERNEL) | |
388 | zlog_debug("Failure to read interface %s speed: %d %s", | |
389 | ifname, errno, safe_strerror(errno)); | |
390 | return 0; | |
391 | } | |
d62a17ae | 392 | /* Get the current link state for the interface */ |
633fc9b1 DL |
393 | rc = vrf_ioctl(interface->vrf_id, sd, SIOCETHTOOL, |
394 | (char *)&ifdata); | |
01b9e3fd | 395 | } |
d62a17ae | 396 | if (rc < 0) { |
bd7d0299 DS |
397 | if (IS_ZEBRA_DEBUG_KERNEL) |
398 | zlog_debug( | |
399 | "IOCTL failure to read interface %s speed: %d %s", | |
400 | ifname, errno, safe_strerror(errno)); | |
d62a17ae | 401 | ecmd.speed_hi = 0; |
402 | ecmd.speed = 0; | |
403 | } | |
404 | ||
405 | close(sd); | |
406 | ||
407 | return (ecmd.speed_hi << 16) | ecmd.speed; | |
535fe877 DS |
408 | } |
409 | ||
dc7b3cae DS |
410 | uint32_t kernel_get_speed(struct interface *ifp) |
411 | { | |
0268f30e | 412 | return get_iflink_speed(ifp); |
dc7b3cae DS |
413 | } |
414 | ||
d62a17ae | 415 | static int netlink_extract_bridge_info(struct rtattr *link_data, |
416 | struct zebra_l2info_bridge *bridge_info) | |
6675513d | 417 | { |
d62a17ae | 418 | struct rtattr *attr[IFLA_BR_MAX + 1]; |
419 | ||
420 | memset(bridge_info, 0, sizeof(*bridge_info)); | |
421 | memset(attr, 0, sizeof attr); | |
422 | parse_rtattr_nested(attr, IFLA_BR_MAX, link_data); | |
423 | if (attr[IFLA_BR_VLAN_FILTERING]) | |
424 | bridge_info->vlan_aware = | |
d7c0a89a | 425 | *(uint8_t *)RTA_DATA(attr[IFLA_BR_VLAN_FILTERING]); |
d62a17ae | 426 | return 0; |
6675513d | 427 | } |
428 | ||
d62a17ae | 429 | static int netlink_extract_vlan_info(struct rtattr *link_data, |
430 | struct zebra_l2info_vlan *vlan_info) | |
6675513d | 431 | { |
d62a17ae | 432 | struct rtattr *attr[IFLA_VLAN_MAX + 1]; |
433 | vlanid_t vid_in_msg; | |
434 | ||
435 | memset(vlan_info, 0, sizeof(*vlan_info)); | |
436 | memset(attr, 0, sizeof attr); | |
437 | parse_rtattr_nested(attr, IFLA_VLAN_MAX, link_data); | |
438 | if (!attr[IFLA_VLAN_ID]) { | |
439 | if (IS_ZEBRA_DEBUG_KERNEL) | |
440 | zlog_debug("IFLA_VLAN_ID missing from VLAN IF message"); | |
441 | return -1; | |
442 | } | |
443 | ||
444 | vid_in_msg = *(vlanid_t *)RTA_DATA(attr[IFLA_VLAN_ID]); | |
445 | vlan_info->vid = vid_in_msg; | |
446 | return 0; | |
6675513d | 447 | } |
448 | ||
d62a17ae | 449 | static int netlink_extract_vxlan_info(struct rtattr *link_data, |
450 | struct zebra_l2info_vxlan *vxl_info) | |
6675513d | 451 | { |
d62a17ae | 452 | struct rtattr *attr[IFLA_VXLAN_MAX + 1]; |
453 | vni_t vni_in_msg; | |
454 | struct in_addr vtep_ip_in_msg; | |
455 | ||
456 | memset(vxl_info, 0, sizeof(*vxl_info)); | |
457 | memset(attr, 0, sizeof attr); | |
458 | parse_rtattr_nested(attr, IFLA_VXLAN_MAX, link_data); | |
459 | if (!attr[IFLA_VXLAN_ID]) { | |
460 | if (IS_ZEBRA_DEBUG_KERNEL) | |
461 | zlog_debug( | |
462 | "IFLA_VXLAN_ID missing from VXLAN IF message"); | |
463 | return -1; | |
464 | } | |
465 | ||
466 | vni_in_msg = *(vni_t *)RTA_DATA(attr[IFLA_VXLAN_ID]); | |
467 | vxl_info->vni = vni_in_msg; | |
468 | if (!attr[IFLA_VXLAN_LOCAL]) { | |
469 | if (IS_ZEBRA_DEBUG_KERNEL) | |
470 | zlog_debug( | |
471 | "IFLA_VXLAN_LOCAL missing from VXLAN IF message"); | |
472 | } else { | |
473 | vtep_ip_in_msg = | |
474 | *(struct in_addr *)RTA_DATA(attr[IFLA_VXLAN_LOCAL]); | |
475 | vxl_info->vtep_ip = vtep_ip_in_msg; | |
476 | } | |
477 | ||
478 | return 0; | |
6675513d | 479 | } |
480 | ||
481 | /* | |
482 | * Extract and save L2 params (of interest) for an interface. When a | |
483 | * bridge interface is added or updated, take further actions to map | |
484 | * its members. Likewise, for VxLAN interface. | |
485 | */ | |
d62a17ae | 486 | static void netlink_interface_update_l2info(struct interface *ifp, |
487 | struct rtattr *link_data, int add) | |
6675513d | 488 | { |
d62a17ae | 489 | if (!link_data) |
490 | return; | |
491 | ||
492 | if (IS_ZEBRA_IF_BRIDGE(ifp)) { | |
493 | struct zebra_l2info_bridge bridge_info; | |
494 | ||
495 | netlink_extract_bridge_info(link_data, &bridge_info); | |
496 | zebra_l2_bridge_add_update(ifp, &bridge_info, add); | |
497 | } else if (IS_ZEBRA_IF_VLAN(ifp)) { | |
498 | struct zebra_l2info_vlan vlan_info; | |
499 | ||
500 | netlink_extract_vlan_info(link_data, &vlan_info); | |
501 | zebra_l2_vlanif_update(ifp, &vlan_info); | |
502 | } else if (IS_ZEBRA_IF_VXLAN(ifp)) { | |
503 | struct zebra_l2info_vxlan vxlan_info; | |
504 | ||
505 | netlink_extract_vxlan_info(link_data, &vxlan_info); | |
506 | zebra_l2_vxlanif_add_update(ifp, &vxlan_info, add); | |
507 | } | |
6675513d | 508 | } |
509 | ||
d62a17ae | 510 | static int netlink_bridge_interface(struct nlmsghdr *h, int len, ns_id_t ns_id, |
511 | int startup) | |
6675513d | 512 | { |
d62a17ae | 513 | char *name = NULL; |
514 | struct ifinfomsg *ifi; | |
515 | struct rtattr *tb[IFLA_MAX + 1]; | |
516 | struct interface *ifp; | |
517 | struct rtattr *aftb[IFLA_BRIDGE_MAX + 1]; | |
518 | struct { | |
d7c0a89a QY |
519 | uint16_t flags; |
520 | uint16_t vid; | |
d62a17ae | 521 | } * vinfo; |
522 | vlanid_t access_vlan; | |
523 | ||
524 | /* Fetch name and ifindex */ | |
525 | ifi = NLMSG_DATA(h); | |
526 | memset(tb, 0, sizeof tb); | |
527 | netlink_parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), len); | |
528 | ||
529 | if (tb[IFLA_IFNAME] == NULL) | |
530 | return -1; | |
531 | name = (char *)RTA_DATA(tb[IFLA_IFNAME]); | |
532 | ||
533 | /* The interface should already be known, if not discard. */ | |
534 | ifp = if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id), ifi->ifi_index); | |
535 | if (!ifp) { | |
9df414fe QY |
536 | zlog_debug("Cannot find bridge IF %s(%u)", name, |
537 | ifi->ifi_index); | |
d62a17ae | 538 | return 0; |
539 | } | |
540 | if (!IS_ZEBRA_IF_VXLAN(ifp)) | |
541 | return 0; | |
542 | ||
543 | /* We are only interested in the access VLAN i.e., AF_SPEC */ | |
544 | if (!tb[IFLA_AF_SPEC]) | |
545 | return 0; | |
546 | ||
547 | /* There is a 1-to-1 mapping of VLAN to VxLAN - hence | |
548 | * only 1 access VLAN is accepted. | |
549 | */ | |
550 | memset(aftb, 0, sizeof aftb); | |
551 | parse_rtattr_nested(aftb, IFLA_BRIDGE_MAX, tb[IFLA_AF_SPEC]); | |
552 | if (!aftb[IFLA_BRIDGE_VLAN_INFO]) | |
553 | return 0; | |
554 | ||
555 | vinfo = RTA_DATA(aftb[IFLA_BRIDGE_VLAN_INFO]); | |
556 | if (!(vinfo->flags & BRIDGE_VLAN_INFO_PVID)) | |
557 | return 0; | |
558 | ||
559 | access_vlan = (vlanid_t)vinfo->vid; | |
560 | if (IS_ZEBRA_DEBUG_KERNEL) | |
561 | zlog_debug("Access VLAN %u for VxLAN IF %s(%u)", access_vlan, | |
562 | name, ifi->ifi_index); | |
563 | zebra_l2_vxlanif_update_access_vlan(ifp, access_vlan); | |
564 | return 0; | |
6675513d | 565 | } |
566 | ||
2414abd3 DS |
567 | /* |
568 | * Called from interface_lookup_netlink(). This function is only used | |
569 | * during bootstrap. | |
570 | */ | |
571 | static int netlink_interface(struct nlmsghdr *h, ns_id_t ns_id, int startup) | |
1fdc9eae | 572 | { |
d62a17ae | 573 | int len; |
574 | struct ifinfomsg *ifi; | |
575 | struct rtattr *tb[IFLA_MAX + 1]; | |
576 | struct rtattr *linkinfo[IFLA_MAX + 1]; | |
577 | struct interface *ifp; | |
578 | char *name = NULL; | |
579 | char *kind = NULL; | |
48884c6b | 580 | char *desc = NULL; |
d62a17ae | 581 | char *slave_kind = NULL; |
582 | struct zebra_ns *zns; | |
583 | vrf_id_t vrf_id = VRF_DEFAULT; | |
584 | zebra_iftype_t zif_type = ZEBRA_IF_OTHER; | |
585 | zebra_slave_iftype_t zif_slave_type = ZEBRA_IF_SLAVE_NONE; | |
586 | ifindex_t bridge_ifindex = IFINDEX_INTERNAL; | |
587 | ifindex_t link_ifindex = IFINDEX_INTERNAL; | |
588 | ||
589 | zns = zebra_ns_lookup(ns_id); | |
590 | ifi = NLMSG_DATA(h); | |
591 | ||
592 | if (h->nlmsg_type != RTM_NEWLINK) | |
593 | return 0; | |
594 | ||
595 | len = h->nlmsg_len - NLMSG_LENGTH(sizeof(struct ifinfomsg)); | |
9bdf8618 DS |
596 | if (len < 0) { |
597 | zlog_err("%s: Message received from netlink is of a broken size: %d %zu", | |
598 | __PRETTY_FUNCTION__, | |
599 | h->nlmsg_len, | |
600 | (size_t)NLMSG_LENGTH(sizeof(struct ifinfomsg))); | |
d62a17ae | 601 | return -1; |
9bdf8618 | 602 | } |
d62a17ae | 603 | |
604 | /* We are interested in some AF_BRIDGE notifications. */ | |
605 | if (ifi->ifi_family == AF_BRIDGE) | |
606 | return netlink_bridge_interface(h, len, ns_id, startup); | |
607 | ||
608 | /* Looking up interface name. */ | |
609 | memset(tb, 0, sizeof tb); | |
610 | memset(linkinfo, 0, sizeof linkinfo); | |
611 | netlink_parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), len); | |
1fdc9eae | 612 | |
d62a17ae | 613 | /* check for wireless messages to ignore */ |
614 | if ((tb[IFLA_WIRELESS] != NULL) && (ifi->ifi_change == 0)) { | |
615 | if (IS_ZEBRA_DEBUG_KERNEL) | |
616 | zlog_debug("%s: ignoring IFLA_WIRELESS message", | |
617 | __func__); | |
618 | return 0; | |
619 | } | |
1fdc9eae | 620 | |
d62a17ae | 621 | if (tb[IFLA_IFNAME] == NULL) |
622 | return -1; | |
623 | name = (char *)RTA_DATA(tb[IFLA_IFNAME]); | |
1fdc9eae | 624 | |
48884c6b DS |
625 | if (tb[IFLA_IFALIAS]) |
626 | desc = (char *)RTA_DATA(tb[IFLA_IFALIAS]); | |
627 | ||
d62a17ae | 628 | if (tb[IFLA_LINKINFO]) { |
629 | parse_rtattr_nested(linkinfo, IFLA_INFO_MAX, tb[IFLA_LINKINFO]); | |
1fdc9eae | 630 | |
d62a17ae | 631 | if (linkinfo[IFLA_INFO_KIND]) |
632 | kind = RTA_DATA(linkinfo[IFLA_INFO_KIND]); | |
1fdc9eae | 633 | |
d62a17ae | 634 | if (linkinfo[IFLA_INFO_SLAVE_KIND]) |
635 | slave_kind = RTA_DATA(linkinfo[IFLA_INFO_SLAVE_KIND]); | |
1fdc9eae | 636 | |
d62a17ae | 637 | netlink_determine_zebra_iftype(kind, &zif_type); |
638 | } | |
639 | ||
640 | /* If VRF, create the VRF structure itself. */ | |
78dd30b2 | 641 | if (zif_type == ZEBRA_IF_VRF && !vrf_is_backend_netns()) { |
d62a17ae | 642 | netlink_vrf_change(h, tb[IFLA_LINKINFO], name); |
643 | vrf_id = (vrf_id_t)ifi->ifi_index; | |
644 | } | |
645 | ||
646 | if (tb[IFLA_MASTER]) { | |
78dd30b2 PG |
647 | if (slave_kind && (strcmp(slave_kind, "vrf") == 0) |
648 | && !vrf_is_backend_netns()) { | |
d62a17ae | 649 | zif_slave_type = ZEBRA_IF_SLAVE_VRF; |
d7c0a89a | 650 | vrf_id = *(uint32_t *)RTA_DATA(tb[IFLA_MASTER]); |
d62a17ae | 651 | } else if (slave_kind && (strcmp(slave_kind, "bridge") == 0)) { |
652 | zif_slave_type = ZEBRA_IF_SLAVE_BRIDGE; | |
653 | bridge_ifindex = | |
654 | *(ifindex_t *)RTA_DATA(tb[IFLA_MASTER]); | |
655 | } else | |
656 | zif_slave_type = ZEBRA_IF_SLAVE_OTHER; | |
657 | } | |
78dd30b2 PG |
658 | if (vrf_is_backend_netns()) |
659 | vrf_id = (vrf_id_t)ns_id; | |
d62a17ae | 660 | |
661 | /* If linking to another interface, note it. */ | |
662 | if (tb[IFLA_LINK]) | |
663 | link_ifindex = *(ifindex_t *)RTA_DATA(tb[IFLA_LINK]); | |
664 | ||
665 | /* Add interface. */ | |
bcc24579 | 666 | ifp = if_get_by_name(name, vrf_id, 0); |
d62a17ae | 667 | set_ifindex(ifp, ifi->ifi_index, zns); |
668 | ifp->flags = ifi->ifi_flags & 0x0000fffff; | |
d62a17ae | 669 | ifp->mtu6 = ifp->mtu = *(uint32_t *)RTA_DATA(tb[IFLA_MTU]); |
670 | ifp->metric = 0; | |
0268f30e | 671 | ifp->speed = get_iflink_speed(ifp); |
d62a17ae | 672 | ifp->ptm_status = ZEBRA_PTM_STATUS_UNKNOWN; |
673 | ||
48884c6b DS |
674 | if (desc) |
675 | ifp->desc = XSTRDUP(MTYPE_TMP, desc); | |
676 | ||
d62a17ae | 677 | /* Set zebra interface type */ |
678 | zebra_if_set_ziftype(ifp, zif_type, zif_slave_type); | |
b0fa6f6a CS |
679 | if (IS_ZEBRA_IF_VRF(ifp)) |
680 | SET_FLAG(ifp->status, ZEBRA_INTERFACE_VRF_LOOPBACK); | |
d62a17ae | 681 | |
682 | /* Update link. */ | |
680c278f | 683 | zebra_if_update_link(ifp, link_ifindex, ns_id); |
d62a17ae | 684 | |
685 | /* Hardware type and address. */ | |
686 | ifp->ll_type = netlink_to_zebra_link_type(ifi->ifi_type); | |
687 | netlink_interface_update_hw_addr(tb, ifp); | |
688 | ||
689 | if_add_update(ifp); | |
690 | ||
691 | /* Extract and save L2 interface information, take additional actions. | |
692 | */ | |
693 | netlink_interface_update_l2info(ifp, linkinfo[IFLA_INFO_DATA], 1); | |
694 | if (IS_ZEBRA_IF_BRIDGE_SLAVE(ifp)) | |
695 | zebra_l2if_update_bridge_slave(ifp, bridge_ifindex); | |
696 | ||
697 | return 0; | |
1fdc9eae | 698 | } |
699 | ||
289602d7 | 700 | /* Request for specific interface or address information from the kernel */ |
d62a17ae | 701 | static int netlink_request_intf_addr(struct zebra_ns *zns, int family, int type, |
d7c0a89a | 702 | uint32_t filter_mask) |
289602d7 | 703 | { |
d62a17ae | 704 | struct { |
705 | struct nlmsghdr n; | |
706 | struct ifinfomsg ifm; | |
707 | char buf[256]; | |
708 | } req; | |
709 | ||
710 | /* Form the request, specifying filter (rtattr) if needed. */ | |
711 | memset(&req, 0, sizeof(req)); | |
712 | req.n.nlmsg_type = type; | |
713 | req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg)); | |
714 | req.ifm.ifi_family = family; | |
715 | ||
716 | /* Include filter, if specified. */ | |
717 | if (filter_mask) | |
718 | addattr32(&req.n, sizeof(req), IFLA_EXT_MASK, filter_mask); | |
719 | ||
720 | return netlink_request(&zns->netlink_cmd, &req.n); | |
289602d7 | 721 | } |
722 | ||
1fdc9eae | 723 | /* Interface lookup by netlink socket. */ |
d62a17ae | 724 | int interface_lookup_netlink(struct zebra_ns *zns) |
1fdc9eae | 725 | { |
d62a17ae | 726 | int ret; |
727 | ||
728 | /* Get interface information. */ | |
729 | ret = netlink_request_intf_addr(zns, AF_PACKET, RTM_GETLINK, 0); | |
730 | if (ret < 0) | |
731 | return ret; | |
732 | ret = netlink_parse_info(netlink_interface, &zns->netlink_cmd, zns, 0, | |
733 | 1); | |
734 | if (ret < 0) | |
735 | return ret; | |
736 | ||
737 | /* Get interface information - for bridge interfaces. */ | |
738 | ret = netlink_request_intf_addr(zns, AF_BRIDGE, RTM_GETLINK, | |
739 | RTEXT_FILTER_BRVLAN); | |
740 | if (ret < 0) | |
741 | return ret; | |
742 | ret = netlink_parse_info(netlink_interface, &zns->netlink_cmd, zns, 0, | |
743 | 0); | |
744 | if (ret < 0) | |
745 | return ret; | |
746 | ||
747 | /* Get interface information - for bridge interfaces. */ | |
748 | ret = netlink_request_intf_addr(zns, AF_BRIDGE, RTM_GETLINK, | |
749 | RTEXT_FILTER_BRVLAN); | |
750 | if (ret < 0) | |
751 | return ret; | |
752 | ret = netlink_parse_info(netlink_interface, &zns->netlink_cmd, zns, 0, | |
753 | 0); | |
754 | if (ret < 0) | |
755 | return ret; | |
756 | ||
757 | /* Get IPv4 address of the interfaces. */ | |
758 | ret = netlink_request_intf_addr(zns, AF_INET, RTM_GETADDR, 0); | |
759 | if (ret < 0) | |
760 | return ret; | |
761 | ret = netlink_parse_info(netlink_interface_addr, &zns->netlink_cmd, zns, | |
762 | 0, 1); | |
763 | if (ret < 0) | |
764 | return ret; | |
765 | ||
766 | /* Get IPv6 address of the interfaces. */ | |
767 | ret = netlink_request_intf_addr(zns, AF_INET6, RTM_GETADDR, 0); | |
768 | if (ret < 0) | |
769 | return ret; | |
770 | ret = netlink_parse_info(netlink_interface_addr, &zns->netlink_cmd, zns, | |
771 | 0, 1); | |
772 | if (ret < 0) | |
773 | return ret; | |
774 | ||
775 | return 0; | |
1fdc9eae | 776 | } |
777 | ||
e0ae31b8 DS |
778 | int kernel_interface_set_master(struct interface *master, |
779 | struct interface *slave) | |
780 | { | |
781 | struct zebra_ns *zns = zebra_ns_lookup(NS_DEFAULT); | |
782 | ||
783 | struct { | |
784 | struct nlmsghdr n; | |
785 | struct ifinfomsg ifa; | |
786 | char buf[NL_PKT_BUF_SIZE]; | |
787 | } req; | |
788 | ||
789 | memset(&req, 0, sizeof req); | |
790 | ||
791 | req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg)); | |
792 | req.n.nlmsg_flags = NLM_F_REQUEST; | |
793 | req.n.nlmsg_type = RTM_SETLINK; | |
794 | req.n.nlmsg_pid = zns->netlink_cmd.snl.nl_pid; | |
795 | ||
796 | req.ifa.ifi_index = slave->ifindex; | |
797 | ||
798 | addattr_l(&req.n, sizeof req, IFLA_MASTER, &master->ifindex, 4); | |
799 | addattr_l(&req.n, sizeof req, IFLA_LINK, &slave->ifindex, 4); | |
800 | ||
801 | return netlink_talk(netlink_talk_filter, &req.n, &zns->netlink_cmd, zns, | |
802 | 0); | |
803 | } | |
804 | ||
1fdc9eae | 805 | /* Interface address modification. */ |
d62a17ae | 806 | static int netlink_address(int cmd, int family, struct interface *ifp, |
807 | struct connected *ifc) | |
1fdc9eae | 808 | { |
d62a17ae | 809 | int bytelen; |
810 | struct prefix *p; | |
1fdc9eae | 811 | |
d62a17ae | 812 | struct { |
813 | struct nlmsghdr n; | |
814 | struct ifaddrmsg ifa; | |
815 | char buf[NL_PKT_BUF_SIZE]; | |
816 | } req; | |
1fdc9eae | 817 | |
fe533c56 | 818 | struct zebra_ns *zns; |
1fdc9eae | 819 | |
fe533c56 PG |
820 | if (vrf_is_backend_netns()) |
821 | zns = zebra_ns_lookup((ns_id_t)ifp->vrf_id); | |
822 | else | |
823 | zns = zebra_ns_lookup(NS_DEFAULT); | |
d62a17ae | 824 | p = ifc->address; |
825 | memset(&req, 0, sizeof req - NL_PKT_BUF_SIZE); | |
1fdc9eae | 826 | |
d62a17ae | 827 | bytelen = (family == AF_INET ? 4 : 16); |
1fdc9eae | 828 | |
d62a17ae | 829 | req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg)); |
830 | req.n.nlmsg_flags = NLM_F_REQUEST; | |
831 | req.n.nlmsg_type = cmd; | |
832 | req.n.nlmsg_pid = zns->netlink_cmd.snl.nl_pid; | |
a55ba23f | 833 | |
d62a17ae | 834 | req.ifa.ifa_family = family; |
1fdc9eae | 835 | |
d62a17ae | 836 | req.ifa.ifa_index = ifp->ifindex; |
1fdc9eae | 837 | |
d62a17ae | 838 | addattr_l(&req.n, sizeof req, IFA_LOCAL, &p->u.prefix, bytelen); |
1fdc9eae | 839 | |
e8d19a05 DL |
840 | if (family == AF_INET) { |
841 | if (CONNECTED_PEER(ifc)) { | |
842 | p = ifc->destination; | |
60466a63 QY |
843 | addattr_l(&req.n, sizeof req, IFA_ADDRESS, &p->u.prefix, |
844 | bytelen); | |
e8d19a05 | 845 | } else if (cmd == RTM_NEWADDR && ifc->destination) { |
d62a17ae | 846 | p = ifc->destination; |
847 | addattr_l(&req.n, sizeof req, IFA_BROADCAST, | |
848 | &p->u.prefix, bytelen); | |
849 | } | |
850 | } | |
1fdc9eae | 851 | |
e8d19a05 DL |
852 | /* p is now either ifc->address or ifc->destination */ |
853 | req.ifa.ifa_prefixlen = p->prefixlen; | |
854 | ||
d62a17ae | 855 | if (CHECK_FLAG(ifc->flags, ZEBRA_IFA_SECONDARY)) |
856 | SET_FLAG(req.ifa.ifa_flags, IFA_F_SECONDARY); | |
1fdc9eae | 857 | |
d62a17ae | 858 | if (ifc->label) |
859 | addattr_l(&req.n, sizeof req, IFA_LABEL, ifc->label, | |
860 | strlen(ifc->label) + 1); | |
1fdc9eae | 861 | |
d62a17ae | 862 | return netlink_talk(netlink_talk_filter, &req.n, &zns->netlink_cmd, zns, |
863 | 0); | |
1fdc9eae | 864 | } |
865 | ||
d62a17ae | 866 | int kernel_address_add_ipv4(struct interface *ifp, struct connected *ifc) |
1fdc9eae | 867 | { |
d62a17ae | 868 | return netlink_address(RTM_NEWADDR, AF_INET, ifp, ifc); |
1fdc9eae | 869 | } |
870 | ||
d62a17ae | 871 | int kernel_address_delete_ipv4(struct interface *ifp, struct connected *ifc) |
1fdc9eae | 872 | { |
d62a17ae | 873 | return netlink_address(RTM_DELADDR, AF_INET, ifp, ifc); |
1fdc9eae | 874 | } |
875 | ||
996c9314 | 876 | int kernel_address_add_ipv6(struct interface *ifp, struct connected *ifc) |
e86b71f1 | 877 | { |
996c9314 | 878 | return netlink_address(RTM_NEWADDR, AF_INET6, ifp, ifc); |
e86b71f1 PG |
879 | } |
880 | ||
996c9314 | 881 | int kernel_address_delete_ipv6(struct interface *ifp, struct connected *ifc) |
e86b71f1 | 882 | { |
996c9314 | 883 | return netlink_address(RTM_DELADDR, AF_INET6, ifp, ifc); |
e86b71f1 PG |
884 | } |
885 | ||
2414abd3 | 886 | int netlink_interface_addr(struct nlmsghdr *h, ns_id_t ns_id, int startup) |
1fdc9eae | 887 | { |
d62a17ae | 888 | int len; |
889 | struct ifaddrmsg *ifa; | |
890 | struct rtattr *tb[IFA_MAX + 1]; | |
891 | struct interface *ifp; | |
892 | void *addr; | |
893 | void *broad; | |
d7c0a89a | 894 | uint8_t flags = 0; |
d62a17ae | 895 | char *label = NULL; |
896 | struct zebra_ns *zns; | |
897 | ||
898 | zns = zebra_ns_lookup(ns_id); | |
899 | ifa = NLMSG_DATA(h); | |
900 | ||
8a1b681c | 901 | if (ifa->ifa_family != AF_INET && ifa->ifa_family != AF_INET6) { |
9df414fe | 902 | flog_warn( |
e914ccbe | 903 | EC_ZEBRA_UNKNOWN_FAMILY, |
81227874 | 904 | "Invalid address family: %u received from kernel interface addr change: %u", |
8a1b681c | 905 | ifa->ifa_family, h->nlmsg_type); |
d62a17ae | 906 | return 0; |
8a1b681c | 907 | } |
d62a17ae | 908 | |
909 | if (h->nlmsg_type != RTM_NEWADDR && h->nlmsg_type != RTM_DELADDR) | |
910 | return 0; | |
911 | ||
912 | len = h->nlmsg_len - NLMSG_LENGTH(sizeof(struct ifaddrmsg)); | |
9bdf8618 DS |
913 | if (len < 0) { |
914 | zlog_err("%s: Message received from netlink is of a broken size: %d %zu", | |
915 | __PRETTY_FUNCTION__, | |
916 | h->nlmsg_len, | |
917 | (size_t)NLMSG_LENGTH(sizeof(struct ifaddrmsg))); | |
d62a17ae | 918 | return -1; |
9bdf8618 | 919 | } |
d62a17ae | 920 | |
921 | memset(tb, 0, sizeof tb); | |
922 | netlink_parse_rtattr(tb, IFA_MAX, IFA_RTA(ifa), len); | |
923 | ||
924 | ifp = if_lookup_by_index_per_ns(zns, ifa->ifa_index); | |
925 | if (ifp == NULL) { | |
af4c2728 | 926 | flog_err( |
4d43f68a | 927 | LIB_ERR_INTERFACE, |
d62a17ae | 928 | "netlink_interface_addr can't find interface by index %d", |
929 | ifa->ifa_index); | |
930 | return -1; | |
931 | } | |
932 | ||
933 | if (IS_ZEBRA_DEBUG_KERNEL) /* remove this line to see initial ifcfg */ | |
934 | { | |
935 | char buf[BUFSIZ]; | |
936 | zlog_debug("netlink_interface_addr %s %s flags 0x%x:", | |
937 | nl_msg_type_to_str(h->nlmsg_type), ifp->name, | |
938 | ifa->ifa_flags); | |
939 | if (tb[IFA_LOCAL]) | |
940 | zlog_debug(" IFA_LOCAL %s/%d", | |
941 | inet_ntop(ifa->ifa_family, | |
942 | RTA_DATA(tb[IFA_LOCAL]), buf, | |
943 | BUFSIZ), | |
944 | ifa->ifa_prefixlen); | |
945 | if (tb[IFA_ADDRESS]) | |
946 | zlog_debug(" IFA_ADDRESS %s/%d", | |
947 | inet_ntop(ifa->ifa_family, | |
948 | RTA_DATA(tb[IFA_ADDRESS]), buf, | |
949 | BUFSIZ), | |
950 | ifa->ifa_prefixlen); | |
951 | if (tb[IFA_BROADCAST]) | |
952 | zlog_debug(" IFA_BROADCAST %s/%d", | |
953 | inet_ntop(ifa->ifa_family, | |
954 | RTA_DATA(tb[IFA_BROADCAST]), buf, | |
955 | BUFSIZ), | |
956 | ifa->ifa_prefixlen); | |
957 | if (tb[IFA_LABEL] && strcmp(ifp->name, RTA_DATA(tb[IFA_LABEL]))) | |
958 | zlog_debug(" IFA_LABEL %s", | |
959 | (char *)RTA_DATA(tb[IFA_LABEL])); | |
960 | ||
961 | if (tb[IFA_CACHEINFO]) { | |
962 | struct ifa_cacheinfo *ci = RTA_DATA(tb[IFA_CACHEINFO]); | |
963 | zlog_debug(" IFA_CACHEINFO pref %d, valid %d", | |
964 | ci->ifa_prefered, ci->ifa_valid); | |
965 | } | |
966 | } | |
967 | ||
968 | /* logic copied from iproute2/ip/ipaddress.c:print_addrinfo() */ | |
969 | if (tb[IFA_LOCAL] == NULL) | |
970 | tb[IFA_LOCAL] = tb[IFA_ADDRESS]; | |
971 | if (tb[IFA_ADDRESS] == NULL) | |
972 | tb[IFA_ADDRESS] = tb[IFA_LOCAL]; | |
973 | ||
974 | /* local interface address */ | |
975 | addr = (tb[IFA_LOCAL] ? RTA_DATA(tb[IFA_LOCAL]) : NULL); | |
976 | ||
977 | /* is there a peer address? */ | |
978 | if (tb[IFA_ADDRESS] | |
979 | && memcmp(RTA_DATA(tb[IFA_ADDRESS]), RTA_DATA(tb[IFA_LOCAL]), | |
980 | RTA_PAYLOAD(tb[IFA_ADDRESS]))) { | |
981 | broad = RTA_DATA(tb[IFA_ADDRESS]); | |
982 | SET_FLAG(flags, ZEBRA_IFA_PEER); | |
983 | } else | |
984 | /* seeking a broadcast address */ | |
985 | broad = (tb[IFA_BROADCAST] ? RTA_DATA(tb[IFA_BROADCAST]) | |
986 | : NULL); | |
987 | ||
988 | /* addr is primary key, SOL if we don't have one */ | |
989 | if (addr == NULL) { | |
990 | zlog_debug("%s: NULL address", __func__); | |
991 | return -1; | |
992 | } | |
993 | ||
994 | /* Flags. */ | |
995 | if (ifa->ifa_flags & IFA_F_SECONDARY) | |
996 | SET_FLAG(flags, ZEBRA_IFA_SECONDARY); | |
997 | ||
998 | /* Label */ | |
999 | if (tb[IFA_LABEL]) | |
1000 | label = (char *)RTA_DATA(tb[IFA_LABEL]); | |
1001 | ||
2e1cc436 | 1002 | if (label && strcmp(ifp->name, label) == 0) |
d62a17ae | 1003 | label = NULL; |
1004 | ||
1005 | /* Register interface address to the interface. */ | |
1006 | if (ifa->ifa_family == AF_INET) { | |
930571d2 | 1007 | if (ifa->ifa_prefixlen > IPV4_MAX_BITLEN) { |
e17d9b2d | 1008 | zlog_err( |
75829703 | 1009 | "Invalid prefix length: %u received from kernel interface addr change: %u", |
930571d2 | 1010 | ifa->ifa_prefixlen, h->nlmsg_type); |
e17d9b2d | 1011 | return -1; |
930571d2 | 1012 | } |
d62a17ae | 1013 | if (h->nlmsg_type == RTM_NEWADDR) |
1014 | connected_add_ipv4(ifp, flags, (struct in_addr *)addr, | |
1015 | ifa->ifa_prefixlen, | |
1016 | (struct in_addr *)broad, label); | |
1017 | else | |
1018 | connected_delete_ipv4( | |
1019 | ifp, flags, (struct in_addr *)addr, | |
1020 | ifa->ifa_prefixlen, (struct in_addr *)broad); | |
1021 | } | |
1022 | if (ifa->ifa_family == AF_INET6) { | |
930571d2 | 1023 | if (ifa->ifa_prefixlen > IPV6_MAX_BITLEN) { |
e17d9b2d | 1024 | zlog_err( |
75829703 | 1025 | "Invalid prefix length: %u received from kernel interface addr change: %u", |
930571d2 | 1026 | ifa->ifa_prefixlen, h->nlmsg_type); |
e17d9b2d | 1027 | return -1; |
930571d2 | 1028 | } |
d62a17ae | 1029 | if (h->nlmsg_type == RTM_NEWADDR) { |
1030 | /* Only consider valid addresses; we'll not get a | |
1031 | * notification from | |
1032 | * the kernel till IPv6 DAD has completed, but at init | |
1033 | * time, Quagga | |
1034 | * does query for and will receive all addresses. | |
1035 | */ | |
1036 | if (!(ifa->ifa_flags | |
1037 | & (IFA_F_DADFAILED | IFA_F_TENTATIVE))) | |
60466a63 QY |
1038 | connected_add_ipv6(ifp, flags, |
1039 | (struct in6_addr *)addr, | |
60c0687a | 1040 | (struct in6_addr *)broad, |
60466a63 | 1041 | ifa->ifa_prefixlen, label); |
d62a17ae | 1042 | } else |
1043 | connected_delete_ipv6(ifp, (struct in6_addr *)addr, | |
60c0687a | 1044 | (struct in6_addr *)broad, |
608105a7 | 1045 | ifa->ifa_prefixlen); |
d62a17ae | 1046 | } |
1047 | ||
1048 | return 0; | |
1fdc9eae | 1049 | } |
1050 | ||
2414abd3 | 1051 | int netlink_link_change(struct nlmsghdr *h, ns_id_t ns_id, int startup) |
1fdc9eae | 1052 | { |
d62a17ae | 1053 | int len; |
1054 | struct ifinfomsg *ifi; | |
1055 | struct rtattr *tb[IFLA_MAX + 1]; | |
1056 | struct rtattr *linkinfo[IFLA_MAX + 1]; | |
1057 | struct interface *ifp; | |
1058 | char *name = NULL; | |
1059 | char *kind = NULL; | |
48884c6b | 1060 | char *desc = NULL; |
d62a17ae | 1061 | char *slave_kind = NULL; |
1062 | struct zebra_ns *zns; | |
1063 | vrf_id_t vrf_id = VRF_DEFAULT; | |
1064 | zebra_iftype_t zif_type = ZEBRA_IF_OTHER; | |
1065 | zebra_slave_iftype_t zif_slave_type = ZEBRA_IF_SLAVE_NONE; | |
1066 | ifindex_t bridge_ifindex = IFINDEX_INTERNAL; | |
1067 | ifindex_t link_ifindex = IFINDEX_INTERNAL; | |
1068 | ||
1069 | ||
1070 | zns = zebra_ns_lookup(ns_id); | |
1071 | ifi = NLMSG_DATA(h); | |
1072 | ||
fe533c56 | 1073 | /* assume if not default zns, then new VRF */ |
d62a17ae | 1074 | if (!(h->nlmsg_type == RTM_NEWLINK || h->nlmsg_type == RTM_DELLINK)) { |
1075 | /* If this is not link add/delete message so print warning. */ | |
9df414fe QY |
1076 | zlog_debug("netlink_link_change: wrong kernel message %d", |
1077 | h->nlmsg_type); | |
d62a17ae | 1078 | return 0; |
1079 | } | |
1080 | ||
8a1b681c SW |
1081 | if (!(ifi->ifi_family == AF_UNSPEC || ifi->ifi_family == AF_BRIDGE |
1082 | || ifi->ifi_family == AF_INET6)) { | |
9df414fe | 1083 | flog_warn( |
e914ccbe | 1084 | EC_ZEBRA_UNKNOWN_FAMILY, |
81227874 | 1085 | "Invalid address family: %u received from kernel link change: %u", |
8a1b681c SW |
1086 | ifi->ifi_family, h->nlmsg_type); |
1087 | return 0; | |
1088 | } | |
1089 | ||
d62a17ae | 1090 | len = h->nlmsg_len - NLMSG_LENGTH(sizeof(struct ifinfomsg)); |
9bdf8618 DS |
1091 | if (len < 0) { |
1092 | zlog_err("%s: Message received from netlink is of a broken size %d %zu", | |
1093 | __PRETTY_FUNCTION__, h->nlmsg_len, | |
1094 | (size_t)NLMSG_LENGTH(sizeof(struct ifinfomsg))); | |
d62a17ae | 1095 | return -1; |
9bdf8618 | 1096 | } |
d62a17ae | 1097 | |
1098 | /* We are interested in some AF_BRIDGE notifications. */ | |
1099 | if (ifi->ifi_family == AF_BRIDGE) | |
1100 | return netlink_bridge_interface(h, len, ns_id, startup); | |
1101 | ||
1102 | /* Looking up interface name. */ | |
1103 | memset(tb, 0, sizeof tb); | |
1104 | memset(linkinfo, 0, sizeof linkinfo); | |
1105 | netlink_parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), len); | |
1fdc9eae | 1106 | |
d62a17ae | 1107 | /* check for wireless messages to ignore */ |
1108 | if ((tb[IFLA_WIRELESS] != NULL) && (ifi->ifi_change == 0)) { | |
1109 | if (IS_ZEBRA_DEBUG_KERNEL) | |
1110 | zlog_debug("%s: ignoring IFLA_WIRELESS message", | |
1111 | __func__); | |
1112 | return 0; | |
1113 | } | |
1fdc9eae | 1114 | |
d62a17ae | 1115 | if (tb[IFLA_IFNAME] == NULL) |
1116 | return -1; | |
1117 | name = (char *)RTA_DATA(tb[IFLA_IFNAME]); | |
1fdc9eae | 1118 | |
d62a17ae | 1119 | if (tb[IFLA_LINKINFO]) { |
1120 | parse_rtattr_nested(linkinfo, IFLA_INFO_MAX, tb[IFLA_LINKINFO]); | |
1fdc9eae | 1121 | |
d62a17ae | 1122 | if (linkinfo[IFLA_INFO_KIND]) |
1123 | kind = RTA_DATA(linkinfo[IFLA_INFO_KIND]); | |
1fdc9eae | 1124 | |
d62a17ae | 1125 | if (linkinfo[IFLA_INFO_SLAVE_KIND]) |
1126 | slave_kind = RTA_DATA(linkinfo[IFLA_INFO_SLAVE_KIND]); | |
1fdc9eae | 1127 | |
d62a17ae | 1128 | netlink_determine_zebra_iftype(kind, &zif_type); |
1129 | } | |
6675513d | 1130 | |
d62a17ae | 1131 | /* If linking to another interface, note it. */ |
1132 | if (tb[IFLA_LINK]) | |
1133 | link_ifindex = *(ifindex_t *)RTA_DATA(tb[IFLA_LINK]); | |
1fdc9eae | 1134 | |
48884c6b DS |
1135 | if (tb[IFLA_IFALIAS]) { |
1136 | desc = (char *)RTA_DATA(tb[IFLA_IFALIAS]); | |
1137 | } | |
1138 | ||
d62a17ae | 1139 | /* If VRF, create or update the VRF structure itself. */ |
78dd30b2 | 1140 | if (zif_type == ZEBRA_IF_VRF && !vrf_is_backend_netns()) { |
d62a17ae | 1141 | netlink_vrf_change(h, tb[IFLA_LINKINFO], name); |
1142 | vrf_id = (vrf_id_t)ifi->ifi_index; | |
1143 | } | |
1fdc9eae | 1144 | |
d62a17ae | 1145 | /* See if interface is present. */ |
1146 | ifp = if_lookup_by_name_per_ns(zns, name); | |
1147 | ||
48884c6b DS |
1148 | if (ifp) { |
1149 | if (ifp->desc) | |
1150 | XFREE(MTYPE_TMP, ifp->desc); | |
1151 | if (desc) | |
1152 | ifp->desc = XSTRDUP(MTYPE_TMP, desc); | |
1153 | } | |
1154 | ||
d62a17ae | 1155 | if (h->nlmsg_type == RTM_NEWLINK) { |
1156 | if (tb[IFLA_MASTER]) { | |
78dd30b2 PG |
1157 | if (slave_kind && (strcmp(slave_kind, "vrf") == 0) |
1158 | && !vrf_is_backend_netns()) { | |
d62a17ae | 1159 | zif_slave_type = ZEBRA_IF_SLAVE_VRF; |
d7c0a89a | 1160 | vrf_id = *(uint32_t *)RTA_DATA(tb[IFLA_MASTER]); |
d62a17ae | 1161 | } else if (slave_kind |
1162 | && (strcmp(slave_kind, "bridge") == 0)) { | |
1163 | zif_slave_type = ZEBRA_IF_SLAVE_BRIDGE; | |
1164 | bridge_ifindex = | |
1165 | *(ifindex_t *)RTA_DATA(tb[IFLA_MASTER]); | |
1166 | } else | |
1167 | zif_slave_type = ZEBRA_IF_SLAVE_OTHER; | |
1168 | } | |
fe533c56 PG |
1169 | if (vrf_is_backend_netns()) |
1170 | vrf_id = (vrf_id_t)ns_id; | |
d62a17ae | 1171 | if (ifp == NULL |
1172 | || !CHECK_FLAG(ifp->status, ZEBRA_INTERFACE_ACTIVE)) { | |
1173 | /* Add interface notification from kernel */ | |
1174 | if (IS_ZEBRA_DEBUG_KERNEL) | |
1175 | zlog_debug( | |
1176 | "RTM_NEWLINK ADD for %s(%u) vrf_id %u type %d " | |
1177 | "sl_type %d master %u flags 0x%x", | |
1178 | name, ifi->ifi_index, vrf_id, zif_type, | |
1179 | zif_slave_type, bridge_ifindex, | |
1180 | ifi->ifi_flags); | |
1181 | ||
1182 | if (ifp == NULL) { | |
1183 | /* unknown interface */ | |
bcc24579 | 1184 | ifp = if_get_by_name(name, vrf_id, 0); |
d62a17ae | 1185 | } else { |
1186 | /* pre-configured interface, learnt now */ | |
1187 | if (ifp->vrf_id != vrf_id) | |
1188 | if_update_to_new_vrf(ifp, vrf_id); | |
1189 | } | |
1190 | ||
1191 | /* Update interface information. */ | |
1192 | set_ifindex(ifp, ifi->ifi_index, zns); | |
1193 | ifp->flags = ifi->ifi_flags & 0x0000fffff; | |
d23b983b | 1194 | if (!tb[IFLA_MTU]) { |
9df414fe | 1195 | zlog_debug( |
d23b983b SW |
1196 | "RTM_NEWLINK for interface %s(%u) without MTU set", |
1197 | name, ifi->ifi_index); | |
1198 | return 0; | |
1199 | } | |
d62a17ae | 1200 | ifp->mtu6 = ifp->mtu = *(int *)RTA_DATA(tb[IFLA_MTU]); |
1201 | ifp->metric = 0; | |
1202 | ifp->ptm_status = ZEBRA_PTM_STATUS_UNKNOWN; | |
1203 | ||
1204 | /* Set interface type */ | |
1205 | zebra_if_set_ziftype(ifp, zif_type, zif_slave_type); | |
b0fa6f6a CS |
1206 | if (IS_ZEBRA_IF_VRF(ifp)) |
1207 | SET_FLAG(ifp->status, | |
1208 | ZEBRA_INTERFACE_VRF_LOOPBACK); | |
d62a17ae | 1209 | |
1210 | /* Update link. */ | |
680c278f | 1211 | zebra_if_update_link(ifp, link_ifindex, ns_id); |
d62a17ae | 1212 | |
1213 | netlink_interface_update_hw_addr(tb, ifp); | |
1214 | ||
1215 | /* Inform clients, install any configured addresses. */ | |
1216 | if_add_update(ifp); | |
1217 | ||
1218 | /* Extract and save L2 interface information, take | |
1219 | * additional actions. */ | |
1220 | netlink_interface_update_l2info( | |
1221 | ifp, linkinfo[IFLA_INFO_DATA], 1); | |
1222 | if (IS_ZEBRA_IF_BRIDGE_SLAVE(ifp)) | |
1223 | zebra_l2if_update_bridge_slave(ifp, | |
1224 | bridge_ifindex); | |
1225 | } else if (ifp->vrf_id != vrf_id) { | |
1226 | /* VRF change for an interface. */ | |
1227 | if (IS_ZEBRA_DEBUG_KERNEL) | |
1228 | zlog_debug( | |
1229 | "RTM_NEWLINK vrf-change for %s(%u) " | |
1230 | "vrf_id %u -> %u flags 0x%x", | |
1231 | name, ifp->ifindex, ifp->vrf_id, vrf_id, | |
1232 | ifi->ifi_flags); | |
1233 | ||
1234 | if_handle_vrf_change(ifp, vrf_id); | |
1235 | } else { | |
1236 | int was_bridge_slave; | |
1237 | ||
1238 | /* Interface update. */ | |
1239 | if (IS_ZEBRA_DEBUG_KERNEL) | |
1240 | zlog_debug( | |
1241 | "RTM_NEWLINK update for %s(%u) " | |
1242 | "sl_type %d master %u flags 0x%x", | |
1243 | name, ifp->ifindex, zif_slave_type, | |
1244 | bridge_ifindex, ifi->ifi_flags); | |
1245 | ||
1246 | set_ifindex(ifp, ifi->ifi_index, zns); | |
d23b983b | 1247 | if (!tb[IFLA_MTU]) { |
9df414fe | 1248 | zlog_debug( |
d23b983b SW |
1249 | "RTM_NEWLINK for interface %s(%u) without MTU set", |
1250 | name, ifi->ifi_index); | |
1251 | return 0; | |
1252 | } | |
d62a17ae | 1253 | ifp->mtu6 = ifp->mtu = *(int *)RTA_DATA(tb[IFLA_MTU]); |
1254 | ifp->metric = 0; | |
1255 | ||
1256 | /* Update interface type - NOTE: Only slave_type can | |
1257 | * change. */ | |
1258 | was_bridge_slave = IS_ZEBRA_IF_BRIDGE_SLAVE(ifp); | |
1259 | zebra_if_set_ziftype(ifp, zif_type, zif_slave_type); | |
1260 | ||
1261 | netlink_interface_update_hw_addr(tb, ifp); | |
1262 | ||
1263 | if (if_is_no_ptm_operative(ifp)) { | |
1264 | ifp->flags = ifi->ifi_flags & 0x0000fffff; | |
1265 | if (!if_is_no_ptm_operative(ifp)) { | |
1266 | if (IS_ZEBRA_DEBUG_KERNEL) | |
1267 | zlog_debug( | |
1268 | "Intf %s(%u) has gone DOWN", | |
1269 | name, ifp->ifindex); | |
1270 | if_down(ifp); | |
1271 | } else if (if_is_operative(ifp)) { | |
1272 | /* Must notify client daemons of new | |
1273 | * interface status. */ | |
1274 | if (IS_ZEBRA_DEBUG_KERNEL) | |
1275 | zlog_debug( | |
1276 | "Intf %s(%u) PTM up, notifying clients", | |
1277 | name, ifp->ifindex); | |
1278 | zebra_interface_up_update(ifp); | |
1279 | } | |
1280 | } else { | |
1281 | ifp->flags = ifi->ifi_flags & 0x0000fffff; | |
1282 | if (if_is_operative(ifp)) { | |
1283 | if (IS_ZEBRA_DEBUG_KERNEL) | |
1284 | zlog_debug( | |
1285 | "Intf %s(%u) has come UP", | |
1286 | name, ifp->ifindex); | |
1287 | if_up(ifp); | |
1288 | } | |
1289 | } | |
1290 | ||
1291 | /* Extract and save L2 interface information, take | |
1292 | * additional actions. */ | |
1293 | netlink_interface_update_l2info( | |
1294 | ifp, linkinfo[IFLA_INFO_DATA], 0); | |
1295 | if (IS_ZEBRA_IF_BRIDGE_SLAVE(ifp) || was_bridge_slave) | |
1296 | zebra_l2if_update_bridge_slave(ifp, | |
1297 | bridge_ifindex); | |
1298 | } | |
1299 | } else { | |
1300 | /* Delete interface notification from kernel */ | |
1301 | if (ifp == NULL) { | |
9df414fe QY |
1302 | if (IS_ZEBRA_DEBUG_KERNEL) |
1303 | zlog_debug( | |
1304 | "RTM_DELLINK for unknown interface %s(%u)", | |
1305 | name, ifi->ifi_index); | |
d62a17ae | 1306 | return 0; |
1307 | } | |
1308 | ||
1309 | if (IS_ZEBRA_DEBUG_KERNEL) | |
1310 | zlog_debug("RTM_DELLINK for %s(%u)", name, | |
1311 | ifp->ifindex); | |
1312 | ||
1313 | UNSET_FLAG(ifp->status, ZEBRA_INTERFACE_VRF_LOOPBACK); | |
1314 | ||
1315 | /* Special handling for bridge or VxLAN interfaces. */ | |
1316 | if (IS_ZEBRA_IF_BRIDGE(ifp)) | |
1317 | zebra_l2_bridge_del(ifp); | |
1318 | else if (IS_ZEBRA_IF_VXLAN(ifp)) | |
1319 | zebra_l2_vxlanif_del(ifp); | |
1320 | ||
1321 | if (!IS_ZEBRA_IF_VRF(ifp)) | |
1322 | if_delete_update(ifp); | |
1fdc9eae | 1323 | } |
1324 | ||
d62a17ae | 1325 | return 0; |
1fdc9eae | 1326 | } |
718e3744 | 1327 | |
1328 | /* Interface information read by netlink. */ | |
d62a17ae | 1329 | void interface_list(struct zebra_ns *zns) |
718e3744 | 1330 | { |
d62a17ae | 1331 | interface_lookup_netlink(zns); |
718e3744 | 1332 | } |
ddfeb486 DL |
1333 | |
1334 | #endif /* GNU_LINUX */ |