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