]> git.proxmox.com Git - mirror_frr.git/blob - zebra/connected.c
Merge pull request #5793 from ton31337/fix/formatting_show_bgp_summary_failed
[mirror_frr.git] / zebra / connected.c
1 /*
2 * Address linked list routine.
3 * Copyright (C) 1997, 98 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 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
20 */
21
22 #include <zebra.h>
23
24 #include "prefix.h"
25 #include "linklist.h"
26 #include "if.h"
27 #include "table.h"
28 #include "rib.h"
29 #include "table.h"
30 #include "log.h"
31 #include "memory.h"
32 #include "zebra_memory.h"
33
34 #include "vty.h"
35 #include "zebra/debug.h"
36 #include "zebra/zserv.h"
37 #include "zebra/redistribute.h"
38 #include "zebra/interface.h"
39 #include "zebra/connected.h"
40 #include "zebra/rtadv.h"
41 #include "zebra/zebra_mpls.h"
42 #include "zebra/debug.h"
43 #include "zebra/zebra_errors.h"
44
45 /* communicate the withdrawal of a connected address */
46 static void connected_withdraw(struct connected *ifc)
47 {
48 if (!ifc)
49 return;
50
51 /* Update interface address information to protocol daemon. */
52 if (CHECK_FLAG(ifc->conf, ZEBRA_IFC_REAL)) {
53 zebra_interface_address_delete_update(ifc->ifp, ifc);
54
55 if (ifc->address->family == AF_INET)
56 if_subnet_delete(ifc->ifp, ifc);
57
58 connected_down(ifc->ifp, ifc);
59
60 UNSET_FLAG(ifc->conf, ZEBRA_IFC_REAL);
61 }
62
63 /* The address is not in the kernel anymore, so clear the flag */
64 UNSET_FLAG(ifc->conf, ZEBRA_IFC_QUEUED);
65
66 if (!CHECK_FLAG(ifc->conf, ZEBRA_IFC_CONFIGURED)) {
67 listnode_delete(ifc->ifp->connected, ifc);
68 connected_free(&ifc);
69 }
70 }
71
72 static void connected_announce(struct interface *ifp, struct connected *ifc)
73 {
74 if (!ifc)
75 return;
76
77 if (!if_is_loopback(ifp) && ifc->address->family == AF_INET &&
78 !IS_ZEBRA_IF_VRF(ifp)) {
79 if (ifc->address->prefixlen == 32)
80 SET_FLAG(ifc->flags, ZEBRA_IFA_UNNUMBERED);
81 else
82 UNSET_FLAG(ifc->flags, ZEBRA_IFA_UNNUMBERED);
83 }
84
85 listnode_add(ifp->connected, ifc);
86
87 /* Update interface address information to protocol daemon. */
88 if (ifc->address->family == AF_INET)
89 if_subnet_add(ifp, ifc);
90
91 zebra_interface_address_add_update(ifp, ifc);
92
93 if (if_is_operative(ifp)) {
94 connected_up(ifp, ifc);
95 }
96 }
97
98 /* If same interface address is already exist... */
99 struct connected *connected_check(struct interface *ifp,
100 union prefixconstptr pu)
101 {
102 const struct prefix *p = pu.p;
103 struct connected *ifc;
104 struct listnode *node;
105
106 for (ALL_LIST_ELEMENTS_RO(ifp->connected, node, ifc))
107 if (prefix_same(ifc->address, p))
108 return ifc;
109
110 return NULL;
111 }
112
113 /* same, but with peer address */
114 struct connected *connected_check_ptp(struct interface *ifp,
115 union prefixconstptr pu,
116 union prefixconstptr du)
117 {
118 const struct prefix *p = pu.p;
119 const struct prefix *d = du.p;
120 struct connected *ifc;
121 struct listnode *node;
122
123 for (ALL_LIST_ELEMENTS_RO(ifp->connected, node, ifc)) {
124 if (!prefix_same(ifc->address, p))
125 continue;
126 if (!CONNECTED_PEER(ifc) && !d)
127 return ifc;
128 if (CONNECTED_PEER(ifc) && d
129 && prefix_same(ifc->destination, d))
130 return ifc;
131 }
132
133 return NULL;
134 }
135
136 /* Check if two ifc's describe the same address in the same state */
137 static int connected_same(struct connected *ifc1, struct connected *ifc2)
138 {
139 if (ifc1->ifp != ifc2->ifp)
140 return 0;
141
142 if (ifc1->flags != ifc2->flags)
143 return 0;
144
145 if (ifc1->conf != ifc2->conf)
146 return 0;
147
148 if (ifc1->destination)
149 if (!ifc2->destination)
150 return 0;
151 if (ifc2->destination)
152 if (!ifc1->destination)
153 return 0;
154
155 if (ifc1->destination && ifc2->destination)
156 if (!prefix_same(ifc1->destination, ifc2->destination))
157 return 0;
158
159 return 1;
160 }
161
162 /* Handle changes to addresses and send the neccesary announcements
163 * to clients. */
164 static void connected_update(struct interface *ifp, struct connected *ifc)
165 {
166 struct connected *current;
167
168 /* Check same connected route. */
169 current = connected_check_ptp(ifp, ifc->address, ifc->destination);
170 if (current) {
171 if (CHECK_FLAG(current->conf, ZEBRA_IFC_CONFIGURED))
172 SET_FLAG(ifc->conf, ZEBRA_IFC_CONFIGURED);
173
174 /* Avoid spurious withdraws, this might be just the kernel
175 * 'reflecting'
176 * back an address we have already added.
177 */
178 if (connected_same(current, ifc)) {
179 /* nothing to do */
180 connected_free(&ifc);
181 return;
182 }
183
184 /* Clear the configured flag on the old ifc, so it will be freed
185 * by
186 * connected withdraw. */
187 UNSET_FLAG(current->conf, ZEBRA_IFC_CONFIGURED);
188 connected_withdraw(
189 current); /* implicit withdraw - freebsd does this */
190 }
191
192 /* If the connected is new or has changed, announce it, if it is usable
193 */
194 if (CHECK_FLAG(ifc->conf, ZEBRA_IFC_REAL))
195 connected_announce(ifp, ifc);
196 }
197
198 /* Called from if_up(). */
199 void connected_up(struct interface *ifp, struct connected *ifc)
200 {
201 afi_t afi;
202 struct prefix p = {0};
203 struct nexthop nh = {
204 .type = NEXTHOP_TYPE_IFINDEX,
205 .ifindex = ifp->ifindex,
206 .vrf_id = ifp->vrf_id,
207 };
208 struct zebra_vrf *zvrf;
209 uint32_t metric;
210
211 zvrf = zebra_vrf_lookup_by_id(ifp->vrf_id);
212 if (!zvrf) {
213 flog_err(EC_ZEBRA_VRF_NOT_FOUND,
214 "%s: Received Up for interface but no associated zvrf: %d",
215 __PRETTY_FUNCTION__, ifp->vrf_id);
216 return;
217 }
218 if (!CHECK_FLAG(ifc->conf, ZEBRA_IFC_REAL))
219 return;
220
221 PREFIX_COPY(&p, CONNECTED_PREFIX(ifc));
222
223 /* Apply mask to the network. */
224 apply_mask(&p);
225
226 afi = family2afi(p.family);
227
228 switch (afi) {
229 case AFI_IP:
230 /*
231 * In case of connected address is 0.0.0.0/0 we treat it tunnel
232 * address.
233 */
234 if (prefix_ipv4_any((struct prefix_ipv4 *)&p))
235 return;
236 break;
237 case AFI_IP6:
238 #ifndef GNU_LINUX
239 /* XXX: It is already done by rib_bogus_ipv6 within rib_add */
240 if (IN6_IS_ADDR_UNSPECIFIED(&p.u.prefix6))
241 return;
242 #endif
243 break;
244 default:
245 flog_warn(EC_ZEBRA_CONNECTED_AFI_UNKNOWN,
246 "Received unknown AFI: %s", afi2str(afi));
247 return;
248 break;
249 }
250
251 metric = (ifc->metric < (uint32_t)METRIC_MAX) ?
252 ifc->metric : ifp->metric;
253 rib_add(afi, SAFI_UNICAST, zvrf->vrf->vrf_id, ZEBRA_ROUTE_CONNECT,
254 0, 0, &p, NULL, &nh, 0, zvrf->table_id, metric, 0, 0, 0);
255
256 rib_add(afi, SAFI_MULTICAST, zvrf->vrf->vrf_id, ZEBRA_ROUTE_CONNECT,
257 0, 0, &p, NULL, &nh, 0, zvrf->table_id, metric, 0, 0, 0);
258
259 /* Schedule LSP forwarding entries for processing, if appropriate. */
260 if (zvrf->vrf->vrf_id == VRF_DEFAULT) {
261 if (IS_ZEBRA_DEBUG_MPLS) {
262 char buf[PREFIX_STRLEN];
263
264 zlog_debug(
265 "%u: IF %s IP %s address add/up, scheduling MPLS processing",
266 zvrf->vrf->vrf_id, ifp->name,
267 prefix2str(&p, buf, sizeof(buf)));
268 }
269 mpls_mark_lsps_for_processing(zvrf, &p);
270 }
271 }
272
273 /* Add connected IPv4 route to the interface. */
274 void connected_add_ipv4(struct interface *ifp, int flags, struct in_addr *addr,
275 uint16_t prefixlen, struct in_addr *dest,
276 const char *label, uint32_t metric)
277 {
278 struct prefix_ipv4 *p;
279 struct connected *ifc;
280
281 if (ipv4_martian(addr))
282 return;
283
284 /* Make connected structure. */
285 ifc = connected_new();
286 ifc->ifp = ifp;
287 ifc->flags = flags;
288 ifc->metric = metric;
289 /* If we get a notification from the kernel,
290 * we can safely assume the address is known to the kernel */
291 SET_FLAG(ifc->conf, ZEBRA_IFC_QUEUED);
292
293 /* Allocate new connected address. */
294 p = prefix_ipv4_new();
295 p->family = AF_INET;
296 p->prefix = *addr;
297 p->prefixlen = CHECK_FLAG(flags, ZEBRA_IFA_PEER) ? IPV4_MAX_PREFIXLEN
298 : prefixlen;
299 ifc->address = (struct prefix *)p;
300
301 /* If there is a peer address. */
302 if (CONNECTED_PEER(ifc)) {
303 /* validate the destination address */
304 if (dest) {
305 p = prefix_ipv4_new();
306 p->family = AF_INET;
307 p->prefix = *dest;
308 p->prefixlen = prefixlen;
309 ifc->destination = (struct prefix *)p;
310
311 if (IPV4_ADDR_SAME(addr, dest))
312 flog_warn(
313 EC_ZEBRA_IFACE_SAME_LOCAL_AS_PEER,
314 "warning: interface %s has same local and peer "
315 "address %s, routing protocols may malfunction",
316 ifp->name, inet_ntoa(*addr));
317 } else {
318 zlog_debug(
319 "warning: %s called for interface %s "
320 "with peer flag set, but no peer address supplied",
321 __func__, ifp->name);
322 UNSET_FLAG(ifc->flags, ZEBRA_IFA_PEER);
323 }
324 }
325
326 /* no destination address was supplied */
327 if (!dest && (prefixlen == IPV4_MAX_PREFIXLEN)
328 && if_is_pointopoint(ifp))
329 zlog_debug(
330 "warning: PtP interface %s with addr %s/%d needs a "
331 "peer address",
332 ifp->name, inet_ntoa(*addr), prefixlen);
333
334 /* Label of this address. */
335 if (label)
336 ifc->label = XSTRDUP(MTYPE_CONNECTED_LABEL, label);
337
338 /* For all that I know an IPv4 address is always ready when we receive
339 * the notification. So it should be safe to set the REAL flag here. */
340 SET_FLAG(ifc->conf, ZEBRA_IFC_REAL);
341
342 connected_update(ifp, ifc);
343 }
344
345 void connected_down(struct interface *ifp, struct connected *ifc)
346 {
347 afi_t afi;
348 struct prefix p;
349 struct nexthop nh = {
350 .type = NEXTHOP_TYPE_IFINDEX,
351 .ifindex = ifp->ifindex,
352 .vrf_id = ifp->vrf_id,
353 };
354 struct zebra_vrf *zvrf;
355
356 zvrf = zebra_vrf_lookup_by_id(ifp->vrf_id);
357 if (!zvrf) {
358 flog_err(EC_ZEBRA_VRF_NOT_FOUND,
359 "%s: Received Up for interface but no associated zvrf: %d",
360 __PRETTY_FUNCTION__, ifp->vrf_id);
361 return;
362 }
363
364 if (!CHECK_FLAG(ifc->conf, ZEBRA_IFC_REAL))
365 return;
366
367 PREFIX_COPY(&p, CONNECTED_PREFIX(ifc));
368
369 /* Apply mask to the network. */
370 apply_mask(&p);
371
372 afi = family2afi(p.family);
373
374 switch (afi) {
375 case AFI_IP:
376 /*
377 * In case of connected address is 0.0.0.0/0 we treat it tunnel
378 * address.
379 */
380 if (prefix_ipv4_any((struct prefix_ipv4 *)&p))
381 return;
382 break;
383 case AFI_IP6:
384 if (IN6_IS_ADDR_UNSPECIFIED(&p.u.prefix6))
385 return;
386 break;
387 default:
388 zlog_warn("Unknown AFI: %s", afi2str(afi));
389 break;
390 }
391
392 /*
393 * Same logic as for connected_up(): push the changes into the
394 * head.
395 */
396 rib_delete(afi, SAFI_UNICAST, zvrf->vrf->vrf_id, ZEBRA_ROUTE_CONNECT, 0,
397 0, &p, NULL, &nh, 0, zvrf->table_id, 0, 0, false);
398
399 rib_delete(afi, SAFI_MULTICAST, zvrf->vrf->vrf_id, ZEBRA_ROUTE_CONNECT,
400 0, 0, &p, NULL, &nh, 0, zvrf->table_id, 0, 0, false);
401
402 /* Schedule LSP forwarding entries for processing, if appropriate. */
403 if (zvrf->vrf->vrf_id == VRF_DEFAULT) {
404 if (IS_ZEBRA_DEBUG_MPLS) {
405 char buf[PREFIX_STRLEN];
406
407 zlog_debug(
408 "%u: IF %s IP %s address down, scheduling MPLS processing",
409 zvrf->vrf->vrf_id, ifp->name,
410 prefix2str(&p, buf, sizeof(buf)));
411 }
412 mpls_mark_lsps_for_processing(zvrf, &p);
413 }
414 }
415
416 static void connected_delete_helper(struct connected *ifc, struct prefix *p)
417 {
418 struct interface *ifp;
419
420 if (!ifc)
421 return;
422 ifp = ifc->ifp;
423
424 connected_withdraw(ifc);
425
426 /* Schedule LSP forwarding entries for processing, if appropriate. */
427 if (ifp->vrf_id == VRF_DEFAULT) {
428 if (IS_ZEBRA_DEBUG_MPLS) {
429 char buf[PREFIX_STRLEN];
430
431 zlog_debug(
432 "%u: IF %s IP %s address delete, scheduling MPLS processing",
433 ifp->vrf_id, ifp->name,
434 prefix2str(p, buf, sizeof(buf)));
435 }
436 mpls_mark_lsps_for_processing(vrf_info_lookup(ifp->vrf_id), p);
437 }
438 }
439
440 /* Delete connected IPv4 route to the interface. */
441 void connected_delete_ipv4(struct interface *ifp, int flags,
442 struct in_addr *addr, uint16_t prefixlen,
443 struct in_addr *dest)
444 {
445 struct prefix p, d;
446 struct connected *ifc;
447
448 memset(&p, 0, sizeof(struct prefix));
449 p.family = AF_INET;
450 p.u.prefix4 = *addr;
451 p.prefixlen = CHECK_FLAG(flags, ZEBRA_IFA_PEER) ? IPV4_MAX_PREFIXLEN
452 : prefixlen;
453
454 if (dest) {
455 memset(&d, 0, sizeof(struct prefix));
456 d.family = AF_INET;
457 d.u.prefix4 = *dest;
458 d.prefixlen = prefixlen;
459 ifc = connected_check_ptp(ifp, &p, &d);
460 } else
461 ifc = connected_check_ptp(ifp, &p, NULL);
462
463 connected_delete_helper(ifc, &p);
464 }
465
466 /* Add connected IPv6 route to the interface. */
467 void connected_add_ipv6(struct interface *ifp, int flags, struct in6_addr *addr,
468 struct in6_addr *dest, uint16_t prefixlen,
469 const char *label, uint32_t metric)
470 {
471 struct prefix_ipv6 *p;
472 struct connected *ifc;
473
474 if (ipv6_martian(addr))
475 return;
476
477 /* Make connected structure. */
478 ifc = connected_new();
479 ifc->ifp = ifp;
480 ifc->flags = flags;
481 ifc->metric = metric;
482 /* If we get a notification from the kernel,
483 * we can safely assume the address is known to the kernel */
484 SET_FLAG(ifc->conf, ZEBRA_IFC_QUEUED);
485
486 /* Allocate new connected address. */
487 p = prefix_ipv6_new();
488 p->family = AF_INET6;
489 IPV6_ADDR_COPY(&p->prefix, addr);
490 p->prefixlen = prefixlen;
491 ifc->address = (struct prefix *)p;
492
493 if (dest) {
494 p = prefix_ipv6_new();
495 p->family = AF_INET6;
496 IPV6_ADDR_COPY(&p->prefix, dest);
497 p->prefixlen = prefixlen;
498 ifc->destination = (struct prefix *)p;
499 } else {
500 if (CHECK_FLAG(ifc->flags, ZEBRA_IFA_PEER)) {
501 zlog_debug(
502 "warning: %s called for interface %s with peer flag set, but no peer address supplied",
503 __func__, ifp->name);
504 UNSET_FLAG(ifc->flags, ZEBRA_IFA_PEER);
505 }
506 }
507
508 /* Label of this address. */
509 if (label)
510 ifc->label = XSTRDUP(MTYPE_CONNECTED_LABEL, label);
511
512 /* On Linux, we only get here when DAD is complete, therefore we can set
513 * ZEBRA_IFC_REAL.
514 *
515 * On BSD, there currently doesn't seem to be a way to check for
516 * completion of
517 * DAD, so we replicate the old behaviour and set ZEBRA_IFC_REAL,
518 * although DAD
519 * might still be running.
520 */
521 SET_FLAG(ifc->conf, ZEBRA_IFC_REAL);
522 connected_update(ifp, ifc);
523 }
524
525 void connected_delete_ipv6(struct interface *ifp, struct in6_addr *address,
526 struct in6_addr *dest, uint16_t prefixlen)
527 {
528 struct prefix p, d;
529 struct connected *ifc;
530
531 memset(&p, 0, sizeof(struct prefix));
532 p.family = AF_INET6;
533 memcpy(&p.u.prefix6, address, sizeof(struct in6_addr));
534 p.prefixlen = prefixlen;
535
536 if (dest) {
537 memset(&d, 0, sizeof(struct prefix));
538 d.family = AF_INET6;
539 IPV6_ADDR_COPY(&d.u.prefix6, dest);
540 d.prefixlen = prefixlen;
541 ifc = connected_check_ptp(ifp, &p, &d);
542 } else
543 ifc = connected_check_ptp(ifp, &p, NULL);
544
545 connected_delete_helper(ifc, &p);
546 }
547
548 int connected_is_unnumbered(struct interface *ifp)
549 {
550 struct connected *connected;
551 struct listnode *node;
552
553 for (ALL_LIST_ELEMENTS_RO(ifp->connected, node, connected)) {
554 if (CHECK_FLAG(connected->conf, ZEBRA_IFC_REAL)
555 && connected->address->family == AF_INET)
556 return CHECK_FLAG(connected->flags,
557 ZEBRA_IFA_UNNUMBERED);
558 }
559 return 1;
560 }