]> git.proxmox.com Git - mirror_frr.git/blob - zebra/connected.c
Merge pull request #6293 from GalaxyGorilla/json_diff
[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(
214 EC_ZEBRA_VRF_NOT_FOUND,
215 "%s: Received Up for interface but no associated zvrf: %d",
216 __func__, ifp->vrf_id);
217 return;
218 }
219 if (!CHECK_FLAG(ifc->conf, ZEBRA_IFC_REAL))
220 return;
221
222 PREFIX_COPY(&p, CONNECTED_PREFIX(ifc));
223
224 /* Apply mask to the network. */
225 apply_mask(&p);
226
227 afi = family2afi(p.family);
228
229 switch (afi) {
230 case AFI_IP:
231 /*
232 * In case of connected address is 0.0.0.0/0 we treat it tunnel
233 * address.
234 */
235 if (prefix_ipv4_any((struct prefix_ipv4 *)&p))
236 return;
237 break;
238 case AFI_IP6:
239 #ifndef GNU_LINUX
240 /* XXX: It is already done by rib_bogus_ipv6 within rib_add */
241 if (IN6_IS_ADDR_UNSPECIFIED(&p.u.prefix6))
242 return;
243 #endif
244 break;
245 default:
246 flog_warn(EC_ZEBRA_CONNECTED_AFI_UNKNOWN,
247 "Received unknown AFI: %s", afi2str(afi));
248 return;
249 break;
250 }
251
252 metric = (ifc->metric < (uint32_t)METRIC_MAX) ?
253 ifc->metric : ifp->metric;
254 rib_add(afi, SAFI_UNICAST, zvrf->vrf->vrf_id, ZEBRA_ROUTE_CONNECT,
255 0, 0, &p, NULL, &nh, 0, zvrf->table_id, metric, 0, 0, 0);
256
257 rib_add(afi, SAFI_MULTICAST, zvrf->vrf->vrf_id, ZEBRA_ROUTE_CONNECT,
258 0, 0, &p, NULL, &nh, 0, zvrf->table_id, metric, 0, 0, 0);
259
260 /* Schedule LSP forwarding entries for processing, if appropriate. */
261 if (zvrf->vrf->vrf_id == VRF_DEFAULT) {
262 if (IS_ZEBRA_DEBUG_MPLS) {
263 char buf[PREFIX_STRLEN];
264
265 zlog_debug(
266 "%u: IF %s IP %s address add/up, scheduling MPLS processing",
267 zvrf->vrf->vrf_id, ifp->name,
268 prefix2str(&p, buf, sizeof(buf)));
269 }
270 mpls_mark_lsps_for_processing(zvrf, &p);
271 }
272 }
273
274 /* Add connected IPv4 route to the interface. */
275 void connected_add_ipv4(struct interface *ifp, int flags, struct in_addr *addr,
276 uint16_t prefixlen, struct in_addr *dest,
277 const char *label, uint32_t metric)
278 {
279 struct prefix_ipv4 *p;
280 struct connected *ifc;
281
282 if (ipv4_martian(addr))
283 return;
284
285 /* Make connected structure. */
286 ifc = connected_new();
287 ifc->ifp = ifp;
288 ifc->flags = flags;
289 ifc->metric = metric;
290 /* If we get a notification from the kernel,
291 * we can safely assume the address is known to the kernel */
292 SET_FLAG(ifc->conf, ZEBRA_IFC_QUEUED);
293
294 /* Allocate new connected address. */
295 p = prefix_ipv4_new();
296 p->family = AF_INET;
297 p->prefix = *addr;
298 p->prefixlen = CHECK_FLAG(flags, ZEBRA_IFA_PEER) ? IPV4_MAX_PREFIXLEN
299 : prefixlen;
300 ifc->address = (struct prefix *)p;
301
302 /* If there is a peer address. */
303 if (CONNECTED_PEER(ifc)) {
304 /* validate the destination address */
305 if (dest) {
306 p = prefix_ipv4_new();
307 p->family = AF_INET;
308 p->prefix = *dest;
309 p->prefixlen = prefixlen;
310 ifc->destination = (struct prefix *)p;
311
312 if (IPV4_ADDR_SAME(addr, dest))
313 flog_warn(
314 EC_ZEBRA_IFACE_SAME_LOCAL_AS_PEER,
315 "warning: interface %s has same local and peer "
316 "address %s, routing protocols may malfunction",
317 ifp->name, inet_ntoa(*addr));
318 } else {
319 zlog_debug(
320 "warning: %s called for interface %s "
321 "with peer flag set, but no peer address supplied",
322 __func__, ifp->name);
323 UNSET_FLAG(ifc->flags, ZEBRA_IFA_PEER);
324 }
325 }
326
327 /* no destination address was supplied */
328 if (!dest && (prefixlen == IPV4_MAX_PREFIXLEN)
329 && if_is_pointopoint(ifp))
330 zlog_debug(
331 "warning: PtP interface %s with addr %s/%d needs a "
332 "peer address",
333 ifp->name, inet_ntoa(*addr), prefixlen);
334
335 /* Label of this address. */
336 if (label)
337 ifc->label = XSTRDUP(MTYPE_CONNECTED_LABEL, label);
338
339 /* For all that I know an IPv4 address is always ready when we receive
340 * the notification. So it should be safe to set the REAL flag here. */
341 SET_FLAG(ifc->conf, ZEBRA_IFC_REAL);
342
343 connected_update(ifp, ifc);
344 }
345
346 void connected_down(struct interface *ifp, struct connected *ifc)
347 {
348 afi_t afi;
349 struct prefix p;
350 struct nexthop nh = {
351 .type = NEXTHOP_TYPE_IFINDEX,
352 .ifindex = ifp->ifindex,
353 .vrf_id = ifp->vrf_id,
354 };
355 struct zebra_vrf *zvrf;
356
357 zvrf = zebra_vrf_lookup_by_id(ifp->vrf_id);
358 if (!zvrf) {
359 flog_err(
360 EC_ZEBRA_VRF_NOT_FOUND,
361 "%s: Received Up for interface but no associated zvrf: %d",
362 __func__, ifp->vrf_id);
363 return;
364 }
365
366 if (!CHECK_FLAG(ifc->conf, ZEBRA_IFC_REAL))
367 return;
368
369 PREFIX_COPY(&p, CONNECTED_PREFIX(ifc));
370
371 /* Apply mask to the network. */
372 apply_mask(&p);
373
374 afi = family2afi(p.family);
375
376 switch (afi) {
377 case AFI_IP:
378 /*
379 * In case of connected address is 0.0.0.0/0 we treat it tunnel
380 * address.
381 */
382 if (prefix_ipv4_any((struct prefix_ipv4 *)&p))
383 return;
384 break;
385 case AFI_IP6:
386 if (IN6_IS_ADDR_UNSPECIFIED(&p.u.prefix6))
387 return;
388 break;
389 default:
390 zlog_warn("Unknown AFI: %s", afi2str(afi));
391 break;
392 }
393
394 /*
395 * Same logic as for connected_up(): push the changes into the
396 * head.
397 */
398 rib_delete(afi, SAFI_UNICAST, zvrf->vrf->vrf_id, ZEBRA_ROUTE_CONNECT, 0,
399 0, &p, NULL, &nh, 0, zvrf->table_id, 0, 0, false);
400
401 rib_delete(afi, SAFI_MULTICAST, zvrf->vrf->vrf_id, ZEBRA_ROUTE_CONNECT,
402 0, 0, &p, NULL, &nh, 0, zvrf->table_id, 0, 0, false);
403
404 /* Schedule LSP forwarding entries for processing, if appropriate. */
405 if (zvrf->vrf->vrf_id == VRF_DEFAULT) {
406 if (IS_ZEBRA_DEBUG_MPLS) {
407 char buf[PREFIX_STRLEN];
408
409 zlog_debug(
410 "%u: IF %s IP %s address down, scheduling MPLS processing",
411 zvrf->vrf->vrf_id, ifp->name,
412 prefix2str(&p, buf, sizeof(buf)));
413 }
414 mpls_mark_lsps_for_processing(zvrf, &p);
415 }
416 }
417
418 static void connected_delete_helper(struct connected *ifc, struct prefix *p)
419 {
420 struct interface *ifp;
421
422 if (!ifc)
423 return;
424 ifp = ifc->ifp;
425
426 connected_withdraw(ifc);
427
428 /* Schedule LSP forwarding entries for processing, if appropriate. */
429 if (ifp->vrf_id == VRF_DEFAULT) {
430 if (IS_ZEBRA_DEBUG_MPLS) {
431 char buf[PREFIX_STRLEN];
432
433 zlog_debug(
434 "%u: IF %s IP %s address delete, scheduling MPLS processing",
435 ifp->vrf_id, ifp->name,
436 prefix2str(p, buf, sizeof(buf)));
437 }
438 mpls_mark_lsps_for_processing(vrf_info_lookup(ifp->vrf_id), p);
439 }
440 }
441
442 /* Delete connected IPv4 route to the interface. */
443 void connected_delete_ipv4(struct interface *ifp, int flags,
444 struct in_addr *addr, uint16_t prefixlen,
445 struct in_addr *dest)
446 {
447 struct prefix p, d;
448 struct connected *ifc;
449
450 memset(&p, 0, sizeof(struct prefix));
451 p.family = AF_INET;
452 p.u.prefix4 = *addr;
453 p.prefixlen = CHECK_FLAG(flags, ZEBRA_IFA_PEER) ? IPV4_MAX_PREFIXLEN
454 : prefixlen;
455
456 if (dest) {
457 memset(&d, 0, sizeof(struct prefix));
458 d.family = AF_INET;
459 d.u.prefix4 = *dest;
460 d.prefixlen = prefixlen;
461 ifc = connected_check_ptp(ifp, &p, &d);
462 } else
463 ifc = connected_check_ptp(ifp, &p, NULL);
464
465 connected_delete_helper(ifc, &p);
466 }
467
468 /* Add connected IPv6 route to the interface. */
469 void connected_add_ipv6(struct interface *ifp, int flags, struct in6_addr *addr,
470 struct in6_addr *dest, uint16_t prefixlen,
471 const char *label, uint32_t metric)
472 {
473 struct prefix_ipv6 *p;
474 struct connected *ifc;
475
476 if (ipv6_martian(addr))
477 return;
478
479 /* Make connected structure. */
480 ifc = connected_new();
481 ifc->ifp = ifp;
482 ifc->flags = flags;
483 ifc->metric = metric;
484 /* If we get a notification from the kernel,
485 * we can safely assume the address is known to the kernel */
486 SET_FLAG(ifc->conf, ZEBRA_IFC_QUEUED);
487
488 /* Allocate new connected address. */
489 p = prefix_ipv6_new();
490 p->family = AF_INET6;
491 IPV6_ADDR_COPY(&p->prefix, addr);
492 p->prefixlen = prefixlen;
493 ifc->address = (struct prefix *)p;
494
495 /* Add global ipv6 address to the RA prefix list */
496 if (!IN6_IS_ADDR_LINKLOCAL(&p->prefix))
497 rtadv_add_prefix(ifp->info, p);
498
499 if (dest) {
500 p = prefix_ipv6_new();
501 p->family = AF_INET6;
502 IPV6_ADDR_COPY(&p->prefix, dest);
503 p->prefixlen = prefixlen;
504 ifc->destination = (struct prefix *)p;
505 } else {
506 if (CHECK_FLAG(ifc->flags, ZEBRA_IFA_PEER)) {
507 zlog_debug(
508 "warning: %s called for interface %s with peer flag set, but no peer address supplied",
509 __func__, ifp->name);
510 UNSET_FLAG(ifc->flags, ZEBRA_IFA_PEER);
511 }
512 }
513
514 /* Label of this address. */
515 if (label)
516 ifc->label = XSTRDUP(MTYPE_CONNECTED_LABEL, label);
517
518 /* On Linux, we only get here when DAD is complete, therefore we can set
519 * ZEBRA_IFC_REAL.
520 *
521 * On BSD, there currently doesn't seem to be a way to check for
522 * completion of
523 * DAD, so we replicate the old behaviour and set ZEBRA_IFC_REAL,
524 * although DAD
525 * might still be running.
526 */
527 SET_FLAG(ifc->conf, ZEBRA_IFC_REAL);
528 connected_update(ifp, ifc);
529 }
530
531 void connected_delete_ipv6(struct interface *ifp, struct in6_addr *address,
532 struct in6_addr *dest, uint16_t prefixlen)
533 {
534 struct prefix p, d;
535 struct connected *ifc;
536
537 memset(&p, 0, sizeof(struct prefix));
538 p.family = AF_INET6;
539 memcpy(&p.u.prefix6, address, sizeof(struct in6_addr));
540 p.prefixlen = prefixlen;
541
542 /* Delete global ipv6 address from RA prefix list */
543 if (!IN6_IS_ADDR_LINKLOCAL(&p.u.prefix6))
544 rtadv_delete_prefix(ifp->info, &p);
545
546 if (dest) {
547 memset(&d, 0, sizeof(struct prefix));
548 d.family = AF_INET6;
549 IPV6_ADDR_COPY(&d.u.prefix6, dest);
550 d.prefixlen = prefixlen;
551 ifc = connected_check_ptp(ifp, &p, &d);
552 } else
553 ifc = connected_check_ptp(ifp, &p, NULL);
554
555 connected_delete_helper(ifc, &p);
556 }
557
558 int connected_is_unnumbered(struct interface *ifp)
559 {
560 struct connected *connected;
561 struct listnode *node;
562
563 for (ALL_LIST_ELEMENTS_RO(ifp->connected, node, connected)) {
564 if (CHECK_FLAG(connected->conf, ZEBRA_IFC_REAL)
565 && connected->address->family == AF_INET)
566 return CHECK_FLAG(connected->flags,
567 ZEBRA_IFA_UNNUMBERED);
568 }
569 return 0;
570 }