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