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