]> git.proxmox.com Git - mirror_frr.git/blob - zebra/connected.c
Merge pull request #6891 from opensourcerouting/feature/sr-te-bgpd
[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 address %s, routing protocols may malfunction",
316 ifp->name, inet_ntoa(*addr));
317 } else {
318 zlog_debug(
319 "warning: %s called for interface %s with peer flag set, but no peer address supplied",
320 __func__, ifp->name);
321 UNSET_FLAG(ifc->flags, ZEBRA_IFA_PEER);
322 }
323 }
324
325 /* no destination address was supplied */
326 if (!dest && (prefixlen == IPV4_MAX_PREFIXLEN)
327 && if_is_pointopoint(ifp))
328 zlog_debug(
329 "warning: PtP interface %s with addr %s/%d needs a peer address",
330 ifp->name, inet_ntoa(*addr), prefixlen);
331
332 /* Label of this address. */
333 if (label)
334 ifc->label = XSTRDUP(MTYPE_CONNECTED_LABEL, label);
335
336 /* For all that I know an IPv4 address is always ready when we receive
337 * the notification. So it should be safe to set the REAL flag here. */
338 SET_FLAG(ifc->conf, ZEBRA_IFC_REAL);
339
340 connected_update(ifp, ifc);
341 }
342
343 void connected_down(struct interface *ifp, struct connected *ifc)
344 {
345 afi_t afi;
346 struct prefix p;
347 struct nexthop nh = {
348 .type = NEXTHOP_TYPE_IFINDEX,
349 .ifindex = ifp->ifindex,
350 .vrf_id = ifp->vrf_id,
351 };
352 struct zebra_vrf *zvrf;
353
354 zvrf = zebra_vrf_lookup_by_id(ifp->vrf_id);
355 if (!zvrf) {
356 flog_err(
357 EC_ZEBRA_VRF_NOT_FOUND,
358 "%s: Received Up for interface but no associated zvrf: %d",
359 __func__, ifp->vrf_id);
360 return;
361 }
362
363 if (!CHECK_FLAG(ifc->conf, ZEBRA_IFC_REAL))
364 return;
365
366 PREFIX_COPY(&p, CONNECTED_PREFIX(ifc));
367
368 /* Apply mask to the network. */
369 apply_mask(&p);
370
371 afi = family2afi(p.family);
372
373 switch (afi) {
374 case AFI_IP:
375 /*
376 * In case of connected address is 0.0.0.0/0 we treat it tunnel
377 * address.
378 */
379 if (prefix_ipv4_any((struct prefix_ipv4 *)&p))
380 return;
381 break;
382 case AFI_IP6:
383 if (IN6_IS_ADDR_UNSPECIFIED(&p.u.prefix6))
384 return;
385 break;
386 default:
387 zlog_warn("Unknown AFI: %s", afi2str(afi));
388 break;
389 }
390
391 /*
392 * Same logic as for connected_up(): push the changes into the
393 * head.
394 */
395 rib_delete(afi, SAFI_UNICAST, zvrf->vrf->vrf_id, ZEBRA_ROUTE_CONNECT, 0,
396 0, &p, NULL, &nh, 0, zvrf->table_id, 0, 0, false, true);
397
398 rib_delete(afi, SAFI_MULTICAST, zvrf->vrf->vrf_id, ZEBRA_ROUTE_CONNECT,
399 0, 0, &p, NULL, &nh, 0, zvrf->table_id, 0, 0, false, true);
400
401 /* Schedule LSP forwarding entries for processing, if appropriate. */
402 if (zvrf->vrf->vrf_id == VRF_DEFAULT) {
403 if (IS_ZEBRA_DEBUG_MPLS) {
404 char buf[PREFIX_STRLEN];
405
406 zlog_debug(
407 "%u: IF %s IP %s address down, scheduling MPLS processing",
408 zvrf->vrf->vrf_id, ifp->name,
409 prefix2str(&p, buf, sizeof(buf)));
410 }
411 mpls_mark_lsps_for_processing(zvrf, &p);
412 }
413 }
414
415 static void connected_delete_helper(struct connected *ifc, struct prefix *p)
416 {
417 struct interface *ifp;
418
419 if (!ifc)
420 return;
421 ifp = ifc->ifp;
422
423 connected_withdraw(ifc);
424
425 /* Schedule LSP forwarding entries for processing, if appropriate. */
426 if (ifp->vrf_id == VRF_DEFAULT) {
427 if (IS_ZEBRA_DEBUG_MPLS) {
428 char buf[PREFIX_STRLEN];
429
430 zlog_debug(
431 "%u: IF %s IP %s address delete, scheduling MPLS processing",
432 ifp->vrf_id, ifp->name,
433 prefix2str(p, buf, sizeof(buf)));
434 }
435 mpls_mark_lsps_for_processing(vrf_info_lookup(ifp->vrf_id), p);
436 }
437 }
438
439 /* Delete connected IPv4 route to the interface. */
440 void connected_delete_ipv4(struct interface *ifp, int flags,
441 struct in_addr *addr, uint16_t prefixlen,
442 struct in_addr *dest)
443 {
444 struct prefix p, d;
445 struct connected *ifc;
446
447 memset(&p, 0, sizeof(struct prefix));
448 p.family = AF_INET;
449 p.u.prefix4 = *addr;
450 p.prefixlen = CHECK_FLAG(flags, ZEBRA_IFA_PEER) ? IPV4_MAX_PREFIXLEN
451 : prefixlen;
452
453 if (dest) {
454 memset(&d, 0, sizeof(struct prefix));
455 d.family = AF_INET;
456 d.u.prefix4 = *dest;
457 d.prefixlen = prefixlen;
458 ifc = connected_check_ptp(ifp, &p, &d);
459 } else
460 ifc = connected_check_ptp(ifp, &p, NULL);
461
462 connected_delete_helper(ifc, &p);
463 }
464
465 /* Add connected IPv6 route to the interface. */
466 void connected_add_ipv6(struct interface *ifp, int flags, struct in6_addr *addr,
467 struct in6_addr *dest, uint16_t prefixlen,
468 const char *label, uint32_t metric)
469 {
470 struct prefix_ipv6 *p;
471 struct connected *ifc;
472
473 if (ipv6_martian(addr))
474 return;
475
476 /* Make connected structure. */
477 ifc = connected_new();
478 ifc->ifp = ifp;
479 ifc->flags = flags;
480 ifc->metric = metric;
481 /* If we get a notification from the kernel,
482 * we can safely assume the address is known to the kernel */
483 SET_FLAG(ifc->conf, ZEBRA_IFC_QUEUED);
484
485 /* Allocate new connected address. */
486 p = prefix_ipv6_new();
487 p->family = AF_INET6;
488 IPV6_ADDR_COPY(&p->prefix, addr);
489 p->prefixlen = prefixlen;
490 ifc->address = (struct prefix *)p;
491
492 /* Add global ipv6 address to the RA prefix list */
493 if (!IN6_IS_ADDR_LINKLOCAL(&p->prefix))
494 rtadv_add_prefix(ifp->info, p);
495
496 if (dest) {
497 p = prefix_ipv6_new();
498 p->family = AF_INET6;
499 IPV6_ADDR_COPY(&p->prefix, dest);
500 p->prefixlen = prefixlen;
501 ifc->destination = (struct prefix *)p;
502 } else {
503 if (CHECK_FLAG(ifc->flags, ZEBRA_IFA_PEER)) {
504 zlog_debug(
505 "warning: %s called for interface %s with peer flag set, but no peer address supplied",
506 __func__, ifp->name);
507 UNSET_FLAG(ifc->flags, ZEBRA_IFA_PEER);
508 }
509 }
510
511 /* Label of this address. */
512 if (label)
513 ifc->label = XSTRDUP(MTYPE_CONNECTED_LABEL, label);
514
515 /* On Linux, we only get here when DAD is complete, therefore we can set
516 * ZEBRA_IFC_REAL.
517 *
518 * On BSD, there currently doesn't seem to be a way to check for
519 * completion of
520 * DAD, so we replicate the old behaviour and set ZEBRA_IFC_REAL,
521 * although DAD
522 * might still be running.
523 */
524 SET_FLAG(ifc->conf, ZEBRA_IFC_REAL);
525 connected_update(ifp, ifc);
526 }
527
528 void connected_delete_ipv6(struct interface *ifp, struct in6_addr *address,
529 struct in6_addr *dest, uint16_t prefixlen)
530 {
531 struct prefix p, d;
532 struct connected *ifc;
533
534 memset(&p, 0, sizeof(struct prefix));
535 p.family = AF_INET6;
536 memcpy(&p.u.prefix6, address, sizeof(struct in6_addr));
537 p.prefixlen = prefixlen;
538
539 /* Delete global ipv6 address from RA prefix list */
540 if (!IN6_IS_ADDR_LINKLOCAL(&p.u.prefix6))
541 rtadv_delete_prefix(ifp->info, &p);
542
543 if (dest) {
544 memset(&d, 0, sizeof(struct prefix));
545 d.family = AF_INET6;
546 IPV6_ADDR_COPY(&d.u.prefix6, dest);
547 d.prefixlen = prefixlen;
548 ifc = connected_check_ptp(ifp, &p, &d);
549 } else
550 ifc = connected_check_ptp(ifp, &p, NULL);
551
552 connected_delete_helper(ifc, &p);
553 }
554
555 int connected_is_unnumbered(struct interface *ifp)
556 {
557 struct connected *connected;
558 struct listnode *node;
559
560 for (ALL_LIST_ELEMENTS_RO(ifp->connected, node, connected)) {
561 if (CHECK_FLAG(connected->conf, ZEBRA_IFC_REAL)
562 && connected->address->family == AF_INET)
563 return CHECK_FLAG(connected->flags,
564 ZEBRA_IFA_UNNUMBERED);
565 }
566 return 0;
567 }