]>
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 | * | |
17 | * You should have received a copy of the GNU General Public License | |
18 | * along with GNU Zebra; see the file COPYING. If not, write to the Free | |
19 | * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA | |
20 | * 02111-1307, USA. | |
21 | */ | |
22 | ||
23 | #include <zebra.h> | |
1fdc9eae | 24 | #include <net/if_arp.h> |
25 | ||
26 | #include "linklist.h" | |
27 | #include "if.h" | |
28 | #include "log.h" | |
29 | #include "prefix.h" | |
30 | #include "connected.h" | |
31 | #include "table.h" | |
32 | #include "memory.h" | |
33 | #include "zebra_memory.h" | |
34 | #include "rib.h" | |
35 | #include "thread.h" | |
36 | #include "privs.h" | |
37 | #include "nexthop.h" | |
38 | #include "vrf.h" | |
39 | #include "mpls.h" | |
718e3744 | 40 | |
5e6a74d8 | 41 | #include "vty.h" |
7dbeea9d | 42 | #include "zebra/zserv.h" |
1fdc9eae | 43 | #include "zebra/zebra_ns.h" |
44 | #include "zebra/zebra_vrf.h" | |
45 | #include "zebra/rt.h" | |
46 | #include "zebra/redistribute.h" | |
47 | #include "zebra/interface.h" | |
48 | #include "zebra/debug.h" | |
49 | #include "zebra/rtadv.h" | |
50 | #include "zebra/zebra_ptm.h" | |
51 | #include "zebra/zebra_mpls.h" | |
52 | #include "zebra/kernel_netlink.h" | |
53 | #include "zebra/if_netlink.h" | |
54 | ||
55 | ||
56 | /* Note: on netlink systems, there should be a 1-to-1 mapping between interface | |
57 | names and ifindex values. */ | |
58 | static void | |
59 | set_ifindex(struct interface *ifp, ifindex_t ifi_index, struct zebra_ns *zns) | |
60 | { | |
61 | struct interface *oifp; | |
62 | ||
63 | if (((oifp = if_lookup_by_index_per_ns (zns, ifi_index)) != NULL) && (oifp != ifp)) | |
64 | { | |
65 | if (ifi_index == IFINDEX_INTERNAL) | |
66 | zlog_err("Netlink is setting interface %s ifindex to reserved " | |
67 | "internal value %u", ifp->name, ifi_index); | |
68 | else | |
69 | { | |
70 | if (IS_ZEBRA_DEBUG_KERNEL) | |
71 | zlog_debug("interface index %d was renamed from %s to %s", | |
72 | ifi_index, oifp->name, ifp->name); | |
73 | if (if_is_up(oifp)) | |
74 | zlog_err("interface rename detected on up interface: index %d " | |
75 | "was renamed from %s to %s, results are uncertain!", | |
76 | ifi_index, oifp->name, ifp->name); | |
77 | if_delete_update(oifp); | |
78 | } | |
79 | } | |
80 | ifp->ifindex = ifi_index; | |
81 | } | |
82 | ||
83 | /* Utility function to parse hardware link-layer address and update ifp */ | |
84 | static void | |
85 | netlink_interface_update_hw_addr (struct rtattr **tb, struct interface *ifp) | |
86 | { | |
87 | int i; | |
88 | ||
89 | if (tb[IFLA_ADDRESS]) | |
90 | { | |
91 | int hw_addr_len; | |
92 | ||
93 | hw_addr_len = RTA_PAYLOAD (tb[IFLA_ADDRESS]); | |
94 | ||
95 | if (hw_addr_len > INTERFACE_HWADDR_MAX) | |
96 | zlog_warn ("Hardware address is too large: %d", hw_addr_len); | |
97 | else | |
98 | { | |
99 | ifp->hw_addr_len = hw_addr_len; | |
100 | memcpy (ifp->hw_addr, RTA_DATA (tb[IFLA_ADDRESS]), hw_addr_len); | |
101 | ||
102 | for (i = 0; i < hw_addr_len; i++) | |
103 | if (ifp->hw_addr[i] != 0) | |
104 | break; | |
105 | ||
106 | if (i == hw_addr_len) | |
107 | ifp->hw_addr_len = 0; | |
108 | else | |
109 | ifp->hw_addr_len = hw_addr_len; | |
110 | } | |
111 | } | |
112 | } | |
113 | ||
114 | static enum zebra_link_type | |
115 | netlink_to_zebra_link_type (unsigned int hwt) | |
116 | { | |
117 | switch (hwt) | |
118 | { | |
119 | case ARPHRD_ETHER: return ZEBRA_LLT_ETHER; | |
120 | case ARPHRD_EETHER: return ZEBRA_LLT_EETHER; | |
121 | case ARPHRD_AX25: return ZEBRA_LLT_AX25; | |
122 | case ARPHRD_PRONET: return ZEBRA_LLT_PRONET; | |
123 | case ARPHRD_IEEE802: return ZEBRA_LLT_IEEE802; | |
124 | case ARPHRD_ARCNET: return ZEBRA_LLT_ARCNET; | |
125 | case ARPHRD_APPLETLK: return ZEBRA_LLT_APPLETLK; | |
126 | case ARPHRD_DLCI: return ZEBRA_LLT_DLCI; | |
127 | case ARPHRD_ATM: return ZEBRA_LLT_ATM; | |
128 | case ARPHRD_METRICOM: return ZEBRA_LLT_METRICOM; | |
129 | case ARPHRD_IEEE1394: return ZEBRA_LLT_IEEE1394; | |
130 | case ARPHRD_EUI64: return ZEBRA_LLT_EUI64; | |
131 | case ARPHRD_INFINIBAND: return ZEBRA_LLT_INFINIBAND; | |
132 | case ARPHRD_SLIP: return ZEBRA_LLT_SLIP; | |
133 | case ARPHRD_CSLIP: return ZEBRA_LLT_CSLIP; | |
134 | case ARPHRD_SLIP6: return ZEBRA_LLT_SLIP6; | |
135 | case ARPHRD_CSLIP6: return ZEBRA_LLT_CSLIP6; | |
136 | case ARPHRD_RSRVD: return ZEBRA_LLT_RSRVD; | |
137 | case ARPHRD_ADAPT: return ZEBRA_LLT_ADAPT; | |
138 | case ARPHRD_ROSE: return ZEBRA_LLT_ROSE; | |
139 | case ARPHRD_X25: return ZEBRA_LLT_X25; | |
140 | case ARPHRD_PPP: return ZEBRA_LLT_PPP; | |
141 | case ARPHRD_CISCO: return ZEBRA_LLT_CHDLC; | |
142 | case ARPHRD_LAPB: return ZEBRA_LLT_LAPB; | |
143 | case ARPHRD_RAWHDLC: return ZEBRA_LLT_RAWHDLC; | |
144 | case ARPHRD_TUNNEL: return ZEBRA_LLT_IPIP; | |
145 | case ARPHRD_TUNNEL6: return ZEBRA_LLT_IPIP6; | |
146 | case ARPHRD_FRAD: return ZEBRA_LLT_FRAD; | |
147 | case ARPHRD_SKIP: return ZEBRA_LLT_SKIP; | |
148 | case ARPHRD_LOOPBACK: return ZEBRA_LLT_LOOPBACK; | |
149 | case ARPHRD_LOCALTLK: return ZEBRA_LLT_LOCALTLK; | |
150 | case ARPHRD_FDDI: return ZEBRA_LLT_FDDI; | |
151 | case ARPHRD_SIT: return ZEBRA_LLT_SIT; | |
152 | case ARPHRD_IPDDP: return ZEBRA_LLT_IPDDP; | |
153 | case ARPHRD_IPGRE: return ZEBRA_LLT_IPGRE; | |
154 | case ARPHRD_PIMREG: return ZEBRA_LLT_PIMREG; | |
155 | case ARPHRD_HIPPI: return ZEBRA_LLT_HIPPI; | |
156 | case ARPHRD_ECONET: return ZEBRA_LLT_ECONET; | |
157 | case ARPHRD_IRDA: return ZEBRA_LLT_IRDA; | |
158 | case ARPHRD_FCPP: return ZEBRA_LLT_FCPP; | |
159 | case ARPHRD_FCAL: return ZEBRA_LLT_FCAL; | |
160 | case ARPHRD_FCPL: return ZEBRA_LLT_FCPL; | |
161 | case ARPHRD_FCFABRIC: return ZEBRA_LLT_FCFABRIC; | |
162 | case ARPHRD_IEEE802_TR: return ZEBRA_LLT_IEEE802_TR; | |
163 | case ARPHRD_IEEE80211: return ZEBRA_LLT_IEEE80211; | |
164 | case ARPHRD_IEEE802154: return ZEBRA_LLT_IEEE802154; | |
165 | #ifdef ARPHRD_IP6GRE | |
166 | case ARPHRD_IP6GRE: return ZEBRA_LLT_IP6GRE; | |
167 | #endif | |
168 | #ifdef ARPHRD_IEEE802154_PHY | |
169 | case ARPHRD_IEEE802154_PHY: return ZEBRA_LLT_IEEE802154_PHY; | |
170 | #endif | |
171 | ||
172 | default: return ZEBRA_LLT_UNKNOWN; | |
173 | } | |
174 | } | |
175 | ||
176 | #define parse_rtattr_nested(tb, max, rta) \ | |
177 | netlink_parse_rtattr((tb), (max), RTA_DATA(rta), RTA_PAYLOAD(rta)) | |
178 | ||
179 | static void | |
180 | netlink_vrf_change (struct nlmsghdr *h, struct rtattr *tb, const char *name) | |
181 | { | |
182 | struct ifinfomsg *ifi; | |
183 | struct rtattr *linkinfo[IFLA_INFO_MAX+1]; | |
184 | struct rtattr *attr[IFLA_VRF_MAX+1]; | |
185 | struct vrf *vrf; | |
186 | struct zebra_vrf *zvrf; | |
187 | u_int32_t nl_table_id; | |
188 | ||
189 | ifi = NLMSG_DATA (h); | |
190 | ||
191 | memset (linkinfo, 0, sizeof linkinfo); | |
192 | parse_rtattr_nested(linkinfo, IFLA_INFO_MAX, tb); | |
193 | ||
194 | if (!linkinfo[IFLA_INFO_DATA]) { | |
195 | if (IS_ZEBRA_DEBUG_KERNEL) | |
196 | zlog_debug ("%s: IFLA_INFO_DATA missing from VRF message: %s", __func__, name); | |
197 | return; | |
198 | } | |
199 | ||
200 | memset (attr, 0, sizeof attr); | |
201 | parse_rtattr_nested(attr, IFLA_VRF_MAX, linkinfo[IFLA_INFO_DATA]); | |
202 | if (!attr[IFLA_VRF_TABLE]) { | |
203 | if (IS_ZEBRA_DEBUG_KERNEL) | |
204 | zlog_debug ("%s: IFLA_VRF_TABLE missing from VRF message: %s", __func__, name); | |
205 | return; | |
206 | } | |
207 | ||
208 | nl_table_id = *(u_int32_t *)RTA_DATA(attr[IFLA_VRF_TABLE]); | |
209 | ||
210 | if (h->nlmsg_type == RTM_NEWLINK) | |
211 | { | |
1fdc9eae | 212 | if (IS_ZEBRA_DEBUG_KERNEL) |
213 | zlog_debug ("RTM_NEWLINK for VRF %s(%u) table %u", | |
214 | name, ifi->ifi_index, nl_table_id); | |
215 | ||
216 | /* | |
217 | * vrf_get is implied creation if it does not exist | |
218 | */ | |
219 | vrf = vrf_get((vrf_id_t)ifi->ifi_index, name); // It would create vrf | |
220 | if (!vrf) | |
221 | { | |
222 | zlog_err ("VRF %s id %u not created", name, ifi->ifi_index); | |
223 | return; | |
224 | } | |
225 | ||
226 | /* Enable the created VRF. */ | |
227 | if (!vrf_enable (vrf)) | |
228 | { | |
229 | zlog_err ("Failed to enable VRF %s id %u", name, ifi->ifi_index); | |
230 | return; | |
231 | } | |
232 | ||
233 | /* | |
234 | * This is the only place that we get the actual kernel table_id | |
235 | * being used. We need it to set the table_id of the routes | |
236 | * we are passing to the kernel.... And to throw some totally | |
237 | * awesome parties. that too. | |
238 | */ | |
239 | zvrf = (struct zebra_vrf *)vrf->info; | |
240 | zvrf->table_id = nl_table_id; | |
241 | } | |
242 | else //h->nlmsg_type == RTM_DELLINK | |
243 | { | |
244 | if (IS_ZEBRA_DEBUG_KERNEL) | |
245 | zlog_debug ("RTM_DELLINK for VRF %s(%u)", name, ifi->ifi_index); | |
246 | ||
5f3d1bdf | 247 | vrf = vrf_lookup_by_id ((vrf_id_t)ifi->ifi_index); |
1fdc9eae | 248 | |
249 | if (!vrf) | |
250 | { | |
251 | zlog_warn ("%s: vrf not found", __func__); | |
252 | return; | |
253 | } | |
254 | ||
255 | vrf_delete (vrf); | |
256 | } | |
257 | } | |
258 | ||
259 | /* Called from interface_lookup_netlink(). This function is only used | |
260 | during bootstrap. */ | |
261 | static int | |
262 | netlink_interface (struct sockaddr_nl *snl, struct nlmsghdr *h, | |
263 | ns_id_t ns_id) | |
264 | { | |
265 | int len; | |
266 | struct ifinfomsg *ifi; | |
267 | struct rtattr *tb[IFLA_MAX + 1]; | |
268 | struct rtattr *linkinfo[IFLA_MAX + 1]; | |
269 | struct interface *ifp; | |
270 | char *name = NULL; | |
271 | char *kind = NULL; | |
272 | char *slave_kind = NULL; | |
273 | int vrf_device = 0; | |
274 | struct zebra_ns *zns; | |
275 | vrf_id_t vrf_id = VRF_DEFAULT; | |
276 | ||
277 | zns = zebra_ns_lookup (ns_id); | |
278 | ifi = NLMSG_DATA (h); | |
279 | ||
280 | if (h->nlmsg_type != RTM_NEWLINK) | |
281 | return 0; | |
282 | ||
283 | len = h->nlmsg_len - NLMSG_LENGTH (sizeof (struct ifinfomsg)); | |
284 | if (len < 0) | |
285 | return -1; | |
286 | ||
287 | if (ifi->ifi_family == AF_BRIDGE) | |
288 | return 0; | |
289 | ||
290 | /* Looking up interface name. */ | |
291 | memset (tb, 0, sizeof tb); | |
292 | netlink_parse_rtattr (tb, IFLA_MAX, IFLA_RTA (ifi), len); | |
293 | ||
294 | #ifdef IFLA_WIRELESS | |
295 | /* check for wireless messages to ignore */ | |
296 | if ((tb[IFLA_WIRELESS] != NULL) && (ifi->ifi_change == 0)) | |
297 | { | |
298 | if (IS_ZEBRA_DEBUG_KERNEL) | |
299 | zlog_debug ("%s: ignoring IFLA_WIRELESS message", __func__); | |
300 | return 0; | |
301 | } | |
302 | #endif /* IFLA_WIRELESS */ | |
303 | ||
304 | if (tb[IFLA_IFNAME] == NULL) | |
305 | return -1; | |
306 | name = (char *) RTA_DATA (tb[IFLA_IFNAME]); | |
307 | ||
308 | if (tb[IFLA_LINKINFO]) | |
309 | { | |
310 | memset (linkinfo, 0, sizeof linkinfo); | |
311 | parse_rtattr_nested(linkinfo, IFLA_INFO_MAX, tb[IFLA_LINKINFO]); | |
312 | ||
313 | if (linkinfo[IFLA_INFO_KIND]) | |
314 | kind = RTA_DATA(linkinfo[IFLA_INFO_KIND]); | |
315 | ||
316 | #if HAVE_DECL_IFLA_INFO_SLAVE_KIND | |
317 | if (linkinfo[IFLA_INFO_SLAVE_KIND]) | |
318 | slave_kind = RTA_DATA(linkinfo[IFLA_INFO_SLAVE_KIND]); | |
319 | #endif | |
320 | ||
321 | if (kind && strcmp(kind, "vrf") == 0) | |
322 | { | |
323 | vrf_device = 1; | |
324 | netlink_vrf_change(h, tb[IFLA_LINKINFO], name); | |
325 | vrf_id = (vrf_id_t)ifi->ifi_index; | |
326 | } | |
327 | } | |
328 | ||
329 | if (tb[IFLA_MASTER]) | |
330 | { | |
331 | if (slave_kind && (strcmp(slave_kind, "vrf") == 0)) | |
332 | vrf_id = *(u_int32_t *)RTA_DATA(tb[IFLA_MASTER]); | |
333 | } | |
334 | ||
335 | /* Add interface. */ | |
336 | ifp = if_get_by_name_vrf (name, vrf_id); | |
337 | set_ifindex(ifp, ifi->ifi_index, zns); | |
338 | ifp->flags = ifi->ifi_flags & 0x0000fffff; | |
339 | if (vrf_device) | |
340 | SET_FLAG(ifp->status, ZEBRA_INTERFACE_VRF_LOOPBACK); | |
341 | ifp->mtu6 = ifp->mtu = *(uint32_t *) RTA_DATA (tb[IFLA_MTU]); | |
342 | ifp->metric = 0; | |
343 | ifp->ptm_status = ZEBRA_PTM_STATUS_UNKNOWN; | |
344 | ||
345 | /* Hardware type and address. */ | |
346 | ifp->ll_type = netlink_to_zebra_link_type (ifi->ifi_type); | |
347 | netlink_interface_update_hw_addr (tb, ifp); | |
348 | ||
349 | if_add_update (ifp); | |
350 | ||
351 | return 0; | |
352 | } | |
353 | ||
354 | /* Interface lookup by netlink socket. */ | |
355 | int | |
356 | interface_lookup_netlink (struct zebra_ns *zns) | |
357 | { | |
358 | int ret; | |
359 | ||
360 | /* Get interface information. */ | |
361 | ret = netlink_request (AF_PACKET, RTM_GETLINK, &zns->netlink_cmd); | |
362 | if (ret < 0) | |
363 | return ret; | |
364 | ret = netlink_parse_info (netlink_interface, &zns->netlink_cmd, zns, 0); | |
365 | if (ret < 0) | |
366 | return ret; | |
367 | ||
368 | /* Get IPv4 address of the interfaces. */ | |
369 | ret = netlink_request (AF_INET, RTM_GETADDR, &zns->netlink_cmd); | |
370 | if (ret < 0) | |
371 | return ret; | |
372 | ret = netlink_parse_info (netlink_interface_addr, &zns->netlink_cmd, zns, 0); | |
373 | if (ret < 0) | |
374 | return ret; | |
375 | ||
1fdc9eae | 376 | /* Get IPv6 address of the interfaces. */ |
377 | ret = netlink_request (AF_INET6, RTM_GETADDR, &zns->netlink_cmd); | |
378 | if (ret < 0) | |
379 | return ret; | |
380 | ret = netlink_parse_info (netlink_interface_addr, &zns->netlink_cmd, zns, 0); | |
381 | if (ret < 0) | |
382 | return ret; | |
1fdc9eae | 383 | |
384 | return 0; | |
385 | } | |
386 | ||
387 | /* Interface address modification. */ | |
388 | static int | |
389 | netlink_address (int cmd, int family, struct interface *ifp, | |
390 | struct connected *ifc) | |
391 | { | |
392 | int bytelen; | |
393 | struct prefix *p; | |
394 | ||
395 | struct | |
396 | { | |
397 | struct nlmsghdr n; | |
398 | struct ifaddrmsg ifa; | |
399 | char buf[NL_PKT_BUF_SIZE]; | |
400 | } req; | |
401 | ||
402 | struct zebra_ns *zns = zebra_ns_lookup (NS_DEFAULT); | |
403 | ||
404 | p = ifc->address; | |
405 | memset (&req, 0, sizeof req - NL_PKT_BUF_SIZE); | |
406 | ||
407 | bytelen = (family == AF_INET ? 4 : 16); | |
408 | ||
409 | req.n.nlmsg_len = NLMSG_LENGTH (sizeof (struct ifaddrmsg)); | |
410 | req.n.nlmsg_flags = NLM_F_REQUEST; | |
411 | req.n.nlmsg_type = cmd; | |
412 | req.ifa.ifa_family = family; | |
413 | ||
414 | req.ifa.ifa_index = ifp->ifindex; | |
415 | req.ifa.ifa_prefixlen = p->prefixlen; | |
416 | ||
417 | addattr_l (&req.n, sizeof req, IFA_LOCAL, &p->u.prefix, bytelen); | |
418 | ||
419 | if (family == AF_INET && cmd == RTM_NEWADDR) | |
420 | { | |
421 | if (!CONNECTED_PEER(ifc) && ifc->destination) | |
422 | { | |
423 | p = ifc->destination; | |
424 | addattr_l (&req.n, sizeof req, IFA_BROADCAST, &p->u.prefix, | |
425 | bytelen); | |
426 | } | |
427 | } | |
428 | ||
429 | if (CHECK_FLAG (ifc->flags, ZEBRA_IFA_SECONDARY)) | |
430 | SET_FLAG (req.ifa.ifa_flags, IFA_F_SECONDARY); | |
431 | ||
432 | if (ifc->label) | |
433 | addattr_l (&req.n, sizeof req, IFA_LABEL, ifc->label, | |
434 | strlen (ifc->label) + 1); | |
435 | ||
436 | return netlink_talk (&req.n, &zns->netlink_cmd, zns); | |
437 | } | |
438 | ||
439 | int | |
440 | kernel_address_add_ipv4 (struct interface *ifp, struct connected *ifc) | |
441 | { | |
442 | return netlink_address (RTM_NEWADDR, AF_INET, ifp, ifc); | |
443 | } | |
444 | ||
445 | int | |
446 | kernel_address_delete_ipv4 (struct interface *ifp, struct connected *ifc) | |
447 | { | |
448 | return netlink_address (RTM_DELADDR, AF_INET, ifp, ifc); | |
449 | } | |
450 | ||
451 | int | |
452 | netlink_interface_addr (struct sockaddr_nl *snl, struct nlmsghdr *h, | |
453 | ns_id_t ns_id) | |
454 | { | |
455 | int len; | |
456 | struct ifaddrmsg *ifa; | |
457 | struct rtattr *tb[IFA_MAX + 1]; | |
458 | struct interface *ifp; | |
459 | void *addr; | |
460 | void *broad; | |
461 | u_char flags = 0; | |
462 | char *label = NULL; | |
463 | struct zebra_ns *zns; | |
464 | ||
465 | zns = zebra_ns_lookup (ns_id); | |
466 | ifa = NLMSG_DATA (h); | |
467 | ||
468 | if (ifa->ifa_family != AF_INET | |
56c1f7d8 | 469 | && ifa->ifa_family != AF_INET6) |
1fdc9eae | 470 | return 0; |
471 | ||
472 | if (h->nlmsg_type != RTM_NEWADDR && h->nlmsg_type != RTM_DELADDR) | |
473 | return 0; | |
474 | ||
475 | len = h->nlmsg_len - NLMSG_LENGTH (sizeof (struct ifaddrmsg)); | |
476 | if (len < 0) | |
477 | return -1; | |
478 | ||
479 | memset (tb, 0, sizeof tb); | |
480 | netlink_parse_rtattr (tb, IFA_MAX, IFA_RTA (ifa), len); | |
481 | ||
482 | ifp = if_lookup_by_index_per_ns (zns, ifa->ifa_index); | |
483 | if (ifp == NULL) | |
484 | { | |
485 | zlog_err ("netlink_interface_addr can't find interface by index %d", | |
486 | ifa->ifa_index); | |
487 | return -1; | |
488 | } | |
489 | ||
490 | if (IS_ZEBRA_DEBUG_KERNEL) /* remove this line to see initial ifcfg */ | |
491 | { | |
492 | char buf[BUFSIZ]; | |
493 | zlog_debug ("netlink_interface_addr %s %s flags 0x%x:", | |
494 | nl_msg_type_to_str (h->nlmsg_type), ifp->name, | |
495 | ifa->ifa_flags); | |
496 | if (tb[IFA_LOCAL]) | |
497 | zlog_debug (" IFA_LOCAL %s/%d", | |
498 | inet_ntop (ifa->ifa_family, RTA_DATA (tb[IFA_LOCAL]), | |
499 | buf, BUFSIZ), ifa->ifa_prefixlen); | |
500 | if (tb[IFA_ADDRESS]) | |
501 | zlog_debug (" IFA_ADDRESS %s/%d", | |
502 | inet_ntop (ifa->ifa_family, RTA_DATA (tb[IFA_ADDRESS]), | |
503 | buf, BUFSIZ), ifa->ifa_prefixlen); | |
504 | if (tb[IFA_BROADCAST]) | |
505 | zlog_debug (" IFA_BROADCAST %s/%d", | |
506 | inet_ntop (ifa->ifa_family, RTA_DATA (tb[IFA_BROADCAST]), | |
507 | buf, BUFSIZ), ifa->ifa_prefixlen); | |
508 | if (tb[IFA_LABEL] && strcmp (ifp->name, RTA_DATA (tb[IFA_LABEL]))) | |
509 | zlog_debug (" IFA_LABEL %s", (char *)RTA_DATA (tb[IFA_LABEL])); | |
510 | ||
511 | if (tb[IFA_CACHEINFO]) | |
512 | { | |
513 | struct ifa_cacheinfo *ci = RTA_DATA (tb[IFA_CACHEINFO]); | |
514 | zlog_debug (" IFA_CACHEINFO pref %d, valid %d", | |
515 | ci->ifa_prefered, ci->ifa_valid); | |
516 | } | |
517 | } | |
518 | ||
519 | /* logic copied from iproute2/ip/ipaddress.c:print_addrinfo() */ | |
520 | if (tb[IFA_LOCAL] == NULL) | |
521 | tb[IFA_LOCAL] = tb[IFA_ADDRESS]; | |
522 | if (tb[IFA_ADDRESS] == NULL) | |
523 | tb[IFA_ADDRESS] = tb[IFA_LOCAL]; | |
524 | ||
525 | /* local interface address */ | |
526 | addr = (tb[IFA_LOCAL] ? RTA_DATA(tb[IFA_LOCAL]) : NULL); | |
527 | ||
528 | /* is there a peer address? */ | |
529 | if (tb[IFA_ADDRESS] && | |
530 | memcmp(RTA_DATA(tb[IFA_ADDRESS]), RTA_DATA(tb[IFA_LOCAL]), RTA_PAYLOAD(tb[IFA_ADDRESS]))) | |
531 | { | |
532 | broad = RTA_DATA(tb[IFA_ADDRESS]); | |
533 | SET_FLAG (flags, ZEBRA_IFA_PEER); | |
534 | } | |
535 | else | |
536 | /* seeking a broadcast address */ | |
537 | broad = (tb[IFA_BROADCAST] ? RTA_DATA(tb[IFA_BROADCAST]) : NULL); | |
538 | ||
539 | /* addr is primary key, SOL if we don't have one */ | |
540 | if (addr == NULL) | |
541 | { | |
542 | zlog_debug ("%s: NULL address", __func__); | |
543 | return -1; | |
544 | } | |
545 | ||
546 | /* Flags. */ | |
547 | if (ifa->ifa_flags & IFA_F_SECONDARY) | |
548 | SET_FLAG (flags, ZEBRA_IFA_SECONDARY); | |
549 | ||
550 | /* Label */ | |
551 | if (tb[IFA_LABEL]) | |
552 | label = (char *) RTA_DATA (tb[IFA_LABEL]); | |
553 | ||
554 | if (ifp && label && strcmp (ifp->name, label) == 0) | |
555 | label = NULL; | |
556 | ||
557 | /* Register interface address to the interface. */ | |
558 | if (ifa->ifa_family == AF_INET) | |
559 | { | |
560 | if (h->nlmsg_type == RTM_NEWADDR) | |
561 | connected_add_ipv4 (ifp, flags, | |
562 | (struct in_addr *) addr, ifa->ifa_prefixlen, | |
563 | (struct in_addr *) broad, label); | |
564 | else | |
565 | connected_delete_ipv4 (ifp, flags, | |
566 | (struct in_addr *) addr, ifa->ifa_prefixlen, | |
567 | (struct in_addr *) broad); | |
568 | } | |
1fdc9eae | 569 | if (ifa->ifa_family == AF_INET6) |
570 | { | |
571 | if (h->nlmsg_type == RTM_NEWADDR) | |
572 | { | |
573 | /* Only consider valid addresses; we'll not get a notification from | |
574 | * the kernel till IPv6 DAD has completed, but at init time, Quagga | |
575 | * does query for and will receive all addresses. | |
576 | */ | |
577 | if (!(ifa->ifa_flags & (IFA_F_DADFAILED | IFA_F_TENTATIVE))) | |
578 | connected_add_ipv6 (ifp, flags, (struct in6_addr *) addr, | |
579 | ifa->ifa_prefixlen, (struct in6_addr *) broad, label); | |
580 | } | |
581 | else | |
582 | connected_delete_ipv6 (ifp, | |
583 | (struct in6_addr *) addr, ifa->ifa_prefixlen, | |
584 | (struct in6_addr *) broad); | |
585 | } | |
1fdc9eae | 586 | |
587 | return 0; | |
588 | } | |
589 | ||
590 | int | |
591 | netlink_link_change (struct sockaddr_nl *snl, struct nlmsghdr *h, | |
592 | ns_id_t ns_id) | |
593 | { | |
594 | int len; | |
595 | struct ifinfomsg *ifi; | |
596 | struct rtattr *tb[IFLA_MAX + 1]; | |
597 | struct rtattr *linkinfo[IFLA_MAX + 1]; | |
598 | struct interface *ifp; | |
599 | char *name = NULL; | |
600 | char *kind = NULL; | |
601 | char *slave_kind = NULL; | |
602 | int vrf_device = 0; | |
603 | struct zebra_ns *zns; | |
604 | vrf_id_t vrf_id = VRF_DEFAULT; | |
605 | ||
606 | zns = zebra_ns_lookup (ns_id); | |
607 | ifi = NLMSG_DATA (h); | |
608 | ||
609 | if (!(h->nlmsg_type == RTM_NEWLINK || h->nlmsg_type == RTM_DELLINK)) | |
610 | { | |
611 | /* If this is not link add/delete message so print warning. */ | |
612 | zlog_warn ("netlink_link_change: wrong kernel message %d", | |
613 | h->nlmsg_type); | |
614 | return 0; | |
615 | } | |
616 | ||
617 | len = h->nlmsg_len - NLMSG_LENGTH (sizeof (struct ifinfomsg)); | |
618 | if (len < 0) | |
619 | return -1; | |
620 | ||
621 | if (ifi->ifi_family == AF_BRIDGE) | |
622 | return 0; | |
623 | ||
624 | /* Looking up interface name. */ | |
625 | memset (tb, 0, sizeof tb); | |
626 | netlink_parse_rtattr (tb, IFLA_MAX, IFLA_RTA (ifi), len); | |
627 | ||
628 | #ifdef IFLA_WIRELESS | |
629 | /* check for wireless messages to ignore */ | |
630 | if ((tb[IFLA_WIRELESS] != NULL) && (ifi->ifi_change == 0)) | |
631 | { | |
632 | if (IS_ZEBRA_DEBUG_KERNEL) | |
633 | zlog_debug ("%s: ignoring IFLA_WIRELESS message", __func__); | |
634 | return 0; | |
635 | } | |
636 | #endif /* IFLA_WIRELESS */ | |
637 | ||
638 | if (tb[IFLA_IFNAME] == NULL) | |
639 | return -1; | |
640 | name = (char *) RTA_DATA (tb[IFLA_IFNAME]); | |
641 | ||
642 | if (tb[IFLA_LINKINFO]) | |
643 | { | |
644 | memset (linkinfo, 0, sizeof linkinfo); | |
645 | parse_rtattr_nested(linkinfo, IFLA_INFO_MAX, tb[IFLA_LINKINFO]); | |
646 | ||
647 | if (linkinfo[IFLA_INFO_KIND]) | |
648 | kind = RTA_DATA(linkinfo[IFLA_INFO_KIND]); | |
649 | ||
650 | #if HAVE_DECL_IFLA_INFO_SLAVE_KIND | |
651 | if (linkinfo[IFLA_INFO_SLAVE_KIND]) | |
652 | slave_kind = RTA_DATA(linkinfo[IFLA_INFO_SLAVE_KIND]); | |
653 | #endif | |
654 | ||
655 | if (kind && strcmp(kind, "vrf") == 0) | |
656 | { | |
657 | vrf_device = 1; | |
658 | netlink_vrf_change(h, tb[IFLA_LINKINFO], name); | |
659 | vrf_id = (vrf_id_t)ifi->ifi_index; | |
660 | } | |
661 | } | |
662 | ||
663 | /* See if interface is present. */ | |
664 | ifp = if_lookup_by_index_per_ns (zns, ifi->ifi_index); | |
665 | ||
666 | if (h->nlmsg_type == RTM_NEWLINK) | |
667 | { | |
668 | if (tb[IFLA_MASTER]) | |
669 | { | |
670 | if (slave_kind && (strcmp(slave_kind, "vrf") == 0)) | |
671 | vrf_id = *(u_int32_t *)RTA_DATA(tb[IFLA_MASTER]); | |
672 | } | |
673 | ||
674 | if (ifp == NULL || !CHECK_FLAG (ifp->status, ZEBRA_INTERFACE_ACTIVE)) | |
675 | { | |
676 | /* Add interface notification from kernel */ | |
677 | if (IS_ZEBRA_DEBUG_KERNEL) | |
678 | zlog_debug ("RTM_NEWLINK for %s(%u) (ifp %p) vrf_id %u flags 0x%x", | |
679 | name, ifi->ifi_index, ifp, vrf_id, ifi->ifi_flags); | |
680 | ||
681 | if (ifp == NULL) | |
682 | { | |
683 | /* unknown interface */ | |
684 | ifp = if_get_by_name_vrf (name, vrf_id); | |
685 | } | |
686 | else | |
687 | { | |
688 | /* pre-configured interface, learnt now */ | |
689 | if (ifp->vrf_id != vrf_id) | |
690 | if_update_vrf (ifp, name, strlen(name), vrf_id); | |
691 | } | |
692 | ||
693 | /* Update interface information. */ | |
694 | set_ifindex(ifp, ifi->ifi_index, zns); | |
695 | ifp->flags = ifi->ifi_flags & 0x0000fffff; | |
696 | if (vrf_device) | |
697 | SET_FLAG(ifp->status, ZEBRA_INTERFACE_VRF_LOOPBACK); | |
698 | ifp->mtu6 = ifp->mtu = *(int *) RTA_DATA (tb[IFLA_MTU]); | |
699 | ifp->metric = 0; | |
700 | ifp->ptm_status = ZEBRA_PTM_STATUS_UNKNOWN; | |
701 | ||
702 | netlink_interface_update_hw_addr (tb, ifp); | |
703 | ||
704 | /* Inform clients, install any configured addresses. */ | |
705 | if_add_update (ifp); | |
706 | } | |
707 | else if (ifp->vrf_id != vrf_id) | |
708 | { | |
709 | /* VRF change for an interface. */ | |
710 | if (IS_ZEBRA_DEBUG_KERNEL) | |
711 | zlog_debug ("RTM_NEWLINK vrf-change for %s(%u) " | |
712 | "vrf_id %u -> %u flags 0x%x", | |
713 | name, ifp->ifindex, ifp->vrf_id, | |
714 | vrf_id, ifi->ifi_flags); | |
715 | ||
716 | if_handle_vrf_change (ifp, vrf_id); | |
717 | } | |
718 | else | |
719 | { | |
720 | /* Interface status change. */ | |
721 | if (IS_ZEBRA_DEBUG_KERNEL) | |
722 | zlog_debug ("RTM_NEWLINK status for %s(%u) flags 0x%x", | |
723 | name, ifp->ifindex, ifi->ifi_flags); | |
724 | ||
725 | set_ifindex(ifp, ifi->ifi_index, zns); | |
726 | ifp->mtu6 = ifp->mtu = *(int *) RTA_DATA (tb[IFLA_MTU]); | |
727 | ifp->metric = 0; | |
728 | ||
729 | netlink_interface_update_hw_addr (tb, ifp); | |
730 | ||
731 | if (if_is_no_ptm_operative (ifp)) | |
732 | { | |
733 | ifp->flags = ifi->ifi_flags & 0x0000fffff; | |
734 | if (!if_is_no_ptm_operative (ifp)) | |
735 | if_down (ifp); | |
736 | else if (if_is_operative (ifp)) | |
737 | /* Must notify client daemons of new interface status. */ | |
738 | zebra_interface_up_update (ifp); | |
739 | } | |
740 | else | |
741 | { | |
742 | ifp->flags = ifi->ifi_flags & 0x0000fffff; | |
743 | if (if_is_operative (ifp)) | |
744 | if_up (ifp); | |
745 | } | |
746 | } | |
747 | } | |
748 | else | |
749 | { | |
750 | /* Delete interface notification from kernel */ | |
751 | if (ifp == NULL) | |
752 | { | |
753 | zlog_warn ("RTM_DELLINK for unknown interface %s(%u)", | |
754 | name, ifi->ifi_index); | |
755 | return 0; | |
756 | } | |
757 | ||
758 | if (IS_ZEBRA_DEBUG_KERNEL) | |
759 | zlog_debug ("RTM_DELLINK for %s(%u)", name, ifp->ifindex); | |
760 | ||
761 | UNSET_FLAG(ifp->status, ZEBRA_INTERFACE_VRF_LOOPBACK); | |
762 | ||
763 | if (!vrf_device) | |
764 | if_delete_update (ifp); | |
765 | } | |
766 | ||
767 | return 0; | |
768 | } | |
718e3744 | 769 | |
770 | /* Interface information read by netlink. */ | |
771 | void | |
12f6fb97 | 772 | interface_list (struct zebra_ns *zns) |
718e3744 | 773 | { |
12f6fb97 | 774 | interface_lookup_netlink (zns); |
718e3744 | 775 | } |