]> git.proxmox.com Git - mirror_frr.git/blame - zebra/connected.c
Merge pull request #5706 from mjstapp/fix_nh_debug_show
[mirror_frr.git] / zebra / connected.c
CommitLineData
718e3744 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 *
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>
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"
0752ef0b 31#include "memory.h"
4a1ab8e4 32#include "zebra_memory.h"
718e3744 33
82f97584 34#include "vty.h"
b84c7253 35#include "zebra/debug.h"
718e3744 36#include "zebra/zserv.h"
37#include "zebra/redistribute.h"
eef1fe11 38#include "zebra/interface.h"
a1ac18c4 39#include "zebra/connected.h"
b6120505 40#include "zebra/rtadv.h"
40c7bdb0 41#include "zebra/zebra_mpls.h"
939fba27 42#include "zebra/debug.h"
9df414fe 43#include "zebra/zebra_errors.h"
6b0655a2 44
02b4805f 45/* communicate the withdrawal of a connected address */
d62a17ae 46static void connected_withdraw(struct connected *ifc)
ca16218d 47{
d62a17ae 48 if (!ifc)
49 return;
ca16218d 50
d62a17ae 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);
ca16218d 54
d62a17ae 55 if (ifc->address->family == AF_INET)
56 if_subnet_delete(ifc->ifp, ifc);
9db047fc 57
11461c63 58 connected_down(ifc->ifp, ifc);
ca16218d 59
d62a17ae 60 UNSET_FLAG(ifc->conf, ZEBRA_IFC_REAL);
61 }
ca16218d 62
d62a17ae 63 /* The address is not in the kernel anymore, so clear the flag */
64 UNSET_FLAG(ifc->conf, ZEBRA_IFC_QUEUED);
f7f740fe 65
d62a17ae 66 if (!CHECK_FLAG(ifc->conf, ZEBRA_IFC_CONFIGURED)) {
67 listnode_delete(ifc->ifp->connected, ifc);
721c0857 68 connected_free(&ifc);
d62a17ae 69 }
ca16218d 70}
71
d62a17ae 72static void connected_announce(struct interface *ifp, struct connected *ifc)
ca16218d 73{
d62a17ae 74 if (!ifc)
75 return;
76
b0fa6f6a
CS
77 if (!if_is_loopback(ifp) && ifc->address->family == AF_INET &&
78 !IS_ZEBRA_IF_VRF(ifp)) {
d62a17ae 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)) {
ae87977c 94 connected_up(ifp, ifc);
d62a17ae 95 }
ca16218d 96}
6b0655a2 97
718e3744 98/* If same interface address is already exist... */
abffde07
DL
99struct connected *connected_check(struct interface *ifp,
100 union prefixconstptr pu)
718e3744 101{
abffde07 102 const struct prefix *p = pu.p;
d62a17ae 103 struct connected *ifc;
104 struct listnode *node;
718e3744 105
d62a17ae 106 for (ALL_LIST_ELEMENTS_RO(ifp->connected, node, ifc))
107 if (prefix_same(ifc->address, p))
108 return ifc;
718e3744 109
d62a17ae 110 return NULL;
718e3744 111}
112
abffde07
DL
113/* same, but with peer address */
114struct 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
abffde07
DL
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
02b4805f 136/* Check if two ifc's describe the same address in the same state */
d62a17ae 137static int connected_same(struct connected *ifc1, struct connected *ifc2)
74ecdc9e 138{
d62a17ae 139 if (ifc1->ifp != ifc2->ifp)
140 return 0;
141
0f3af738
JW
142 if (ifc1->flags != ifc2->flags)
143 return 0;
144
145 if (ifc1->conf != ifc2->conf)
146 return 0;
147
d62a17ae 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
d62a17ae 159 return 1;
74ecdc9e
PJ
160}
161
d7f5dad6
CF
162/* Handle changes to addresses and send the neccesary announcements
163 * to clients. */
d62a17ae 164static void connected_update(struct interface *ifp, struct connected *ifc)
74ecdc9e 165{
d62a17ae 166 struct connected *current;
167
168 /* Check same connected route. */
abffde07
DL
169 current = connected_check_ptp(ifp, ifc->address, ifc->destination);
170 if (current) {
d62a17ae 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 */
721c0857 180 connected_free(&ifc);
d62a17ae 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);
74ecdc9e
PJ
196}
197
718e3744 198/* Called from if_up(). */
ae87977c 199void connected_up(struct interface *ifp, struct connected *ifc)
718e3744 200{
ae87977c 201 afi_t afi;
49085521 202 struct prefix p = {0};
fd36be7e 203 struct nexthop nh = {
4a7371e9
DS
204 .type = NEXTHOP_TYPE_IFINDEX,
205 .ifindex = ifp->ifindex,
a36898e7 206 .vrf_id = ifp->vrf_id,
fd36be7e 207 };
56e78254 208 struct zebra_vrf *zvrf;
cde1af84 209 uint32_t metric;
d62a17ae 210
a36898e7 211 zvrf = zebra_vrf_lookup_by_id(ifp->vrf_id);
56e78254
DS
212 if (!zvrf) {
213 flog_err(EC_ZEBRA_VRF_NOT_FOUND,
214 "%s: Received Up for interface but no associated zvrf: %d",
a36898e7 215 __PRETTY_FUNCTION__, ifp->vrf_id);
56e78254
DS
216 return;
217 }
d62a17ae 218 if (!CHECK_FLAG(ifc->conf, ZEBRA_IFC_REAL))
219 return;
220
ae87977c 221 PREFIX_COPY(&p, CONNECTED_PREFIX(ifc));
d62a17ae 222
223 /* Apply mask to the network. */
224 apply_mask(&p);
225
ae87977c
DS
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:
70bc8385 238#ifndef GNU_LINUX
ae87977c
DS
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:
e914ccbe 245 flog_warn(EC_ZEBRA_CONNECTED_AFI_UNKNOWN,
9df414fe 246 "Received unknown AFI: %s", afi2str(afi));
d62a17ae 247 return;
ae87977c
DS
248 break;
249 }
d62a17ae 250
cde1af84
AK
251 metric = (ifc->metric < (uint32_t)METRIC_MAX) ?
252 ifc->metric : ifp->metric;
56e78254 253 rib_add(afi, SAFI_UNICAST, zvrf->vrf->vrf_id, ZEBRA_ROUTE_CONNECT,
8032b717 254 0, 0, &p, NULL, &nh, 0, zvrf->table_id, metric, 0, 0, 0);
d62a17ae 255
56e78254 256 rib_add(afi, SAFI_MULTICAST, zvrf->vrf->vrf_id, ZEBRA_ROUTE_CONNECT,
8032b717 257 0, 0, &p, NULL, &nh, 0, zvrf->table_id, metric, 0, 0, 0);
d62a17ae 258
d62a17ae 259 /* Schedule LSP forwarding entries for processing, if appropriate. */
56e78254 260 if (zvrf->vrf->vrf_id == VRF_DEFAULT) {
ae87977c
DS
261 if (IS_ZEBRA_DEBUG_MPLS) {
262 char buf[PREFIX_STRLEN];
263
996c9314
LB
264 zlog_debug(
265 "%u: IF %s IP %s address add/up, scheduling MPLS processing",
56e78254 266 zvrf->vrf->vrf_id, ifp->name,
996c9314 267 prefix2str(&p, buf, sizeof(buf)));
ae87977c 268 }
56e78254 269 mpls_mark_lsps_for_processing(zvrf, &p);
d62a17ae 270 }
718e3744 271}
272
273/* Add connected IPv4 route to the interface. */
d62a17ae 274void connected_add_ipv4(struct interface *ifp, int flags, struct in_addr *addr,
0f3af738 275 uint16_t prefixlen, struct in_addr *dest,
cde1af84 276 const char *label, uint32_t metric)
718e3744 277{
d62a17ae 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;
cde1af84 288 ifc->metric = metric;
d62a17ae 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;
abffde07
DL
297 p->prefixlen = CHECK_FLAG(flags, ZEBRA_IFA_PEER) ? IPV4_MAX_PREFIXLEN
298 : prefixlen;
d62a17ae 299 ifc->address = (struct prefix *)p;
300
0f3af738
JW
301 /* If there is a peer address. */
302 if (CONNECTED_PEER(ifc)) {
d62a17ae 303 /* validate the destination address */
0f3af738
JW
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))
9df414fe 312 flog_warn(
e914ccbe 313 EC_ZEBRA_IFACE_SAME_LOCAL_AS_PEER,
d62a17ae 314 "warning: interface %s has same local and peer "
315 "address %s, routing protocols may malfunction",
316 ifp->name, inet_ntoa(*addr));
317 } else {
9df414fe 318 zlog_debug(
d62a17ae 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 }
e4529636
AS
324 }
325
0f3af738
JW
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
d62a17ae 334 /* Label of this address. */
335 if (label)
336 ifc->label = XSTRDUP(MTYPE_CONNECTED_LABEL, label);
718e3744 337
d62a17ae 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);
718e3744 341
d62a17ae 342 connected_update(ifp, ifc);
718e3744 343}
344
11461c63 345void connected_down(struct interface *ifp, struct connected *ifc)
718e3744 346{
11461c63 347 afi_t afi;
d62a17ae 348 struct prefix p;
fd36be7e 349 struct nexthop nh = {
4a7371e9
DS
350 .type = NEXTHOP_TYPE_IFINDEX,
351 .ifindex = ifp->ifindex,
a36898e7 352 .vrf_id = ifp->vrf_id,
fd36be7e 353 };
56e78254
DS
354 struct zebra_vrf *zvrf;
355
a36898e7 356 zvrf = zebra_vrf_lookup_by_id(ifp->vrf_id);
56e78254
DS
357 if (!zvrf) {
358 flog_err(EC_ZEBRA_VRF_NOT_FOUND,
359 "%s: Received Up for interface but no associated zvrf: %d",
a36898e7 360 __PRETTY_FUNCTION__, ifp->vrf_id);
56e78254
DS
361 return;
362 }
718e3744 363
d62a17ae 364 if (!CHECK_FLAG(ifc->conf, ZEBRA_IFC_REAL))
365 return;
718e3744 366
11461c63 367 PREFIX_COPY(&p, CONNECTED_PREFIX(ifc));
718e3744 368
d62a17ae 369 /* Apply mask to the network. */
370 apply_mask(&p);
718e3744 371
11461c63
DS
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:
14a4d9d0 388 zlog_warn("Unknown AFI: %s", afi2str(afi));
11461c63
DS
389 break;
390 }
718e3744 391
11461c63
DS
392 /*
393 * Same logic as for connected_up(): push the changes into the
394 * head.
395 */
bc541126
SW
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);
718e3744 398
56e78254 399 rib_delete(afi, SAFI_MULTICAST, zvrf->vrf->vrf_id, ZEBRA_ROUTE_CONNECT,
bc541126 400 0, 0, &p, NULL, &nh, 0, zvrf->table_id, 0, 0, false);
42cb6b66 401
d62a17ae 402 /* Schedule LSP forwarding entries for processing, if appropriate. */
56e78254 403 if (zvrf->vrf->vrf_id == VRF_DEFAULT) {
11461c63
DS
404 if (IS_ZEBRA_DEBUG_MPLS) {
405 char buf[PREFIX_STRLEN];
406
996c9314
LB
407 zlog_debug(
408 "%u: IF %s IP %s address down, scheduling MPLS processing",
56e78254 409 zvrf->vrf->vrf_id, ifp->name,
996c9314 410 prefix2str(&p, buf, sizeof(buf)));
11461c63 411 }
56e78254 412 mpls_mark_lsps_for_processing(zvrf, &p);
d62a17ae 413 }
718e3744 414}
415
dc7cd304
DS
416static 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
dc7cd304 426 /* Schedule LSP forwarding entries for processing, if appropriate. */
a36898e7 427 if (ifp->vrf_id == VRF_DEFAULT) {
dc7cd304
DS
428 if (IS_ZEBRA_DEBUG_MPLS) {
429 char buf[PREFIX_STRLEN];
430
996c9314
LB
431 zlog_debug(
432 "%u: IF %s IP %s address delete, scheduling MPLS processing",
a36898e7 433 ifp->vrf_id, ifp->name,
996c9314 434 prefix2str(p, buf, sizeof(buf)));
dc7cd304 435 }
a36898e7 436 mpls_mark_lsps_for_processing(vrf_info_lookup(ifp->vrf_id), p);
dc7cd304
DS
437 }
438}
439
718e3744 440/* Delete connected IPv4 route to the interface. */
d62a17ae 441void connected_delete_ipv4(struct interface *ifp, int flags,
f93eee44 442 struct in_addr *addr, uint16_t prefixlen,
0f3af738 443 struct in_addr *dest)
718e3744 444{
dc7cd304 445 struct prefix p, d;
d62a17ae 446 struct connected *ifc;
447
dc7cd304 448 memset(&p, 0, sizeof(struct prefix));
d62a17ae 449 p.family = AF_INET;
dc7cd304 450 p.u.prefix4 = *addr;
abffde07
DL
451 p.prefixlen = CHECK_FLAG(flags, ZEBRA_IFA_PEER) ? IPV4_MAX_PREFIXLEN
452 : prefixlen;
453
0f3af738 454 if (dest) {
dc7cd304 455 memset(&d, 0, sizeof(struct prefix));
abffde07 456 d.family = AF_INET;
0f3af738 457 d.u.prefix4 = *dest;
abffde07 458 d.prefixlen = prefixlen;
dc7cd304 459 ifc = connected_check_ptp(ifp, &p, &d);
abffde07 460 } else
dc7cd304 461 ifc = connected_check_ptp(ifp, &p, NULL);
d62a17ae 462
dc7cd304 463 connected_delete_helper(ifc, &p);
718e3744 464}
465
718e3744 466/* Add connected IPv6 route to the interface. */
d62a17ae 467void connected_add_ipv6(struct interface *ifp, int flags, struct in6_addr *addr,
0f3af738 468 struct in6_addr *dest, uint16_t prefixlen,
cde1af84 469 const char *label, uint32_t metric)
718e3744 470{
d62a17ae 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;
cde1af84 481 ifc->metric = metric;
d62a17ae 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
0f3af738 493 if (dest) {
60c0687a
DS
494 p = prefix_ipv6_new();
495 p->family = AF_INET6;
0f3af738 496 IPV6_ADDR_COPY(&p->prefix, dest);
60c0687a
DS
497 p->prefixlen = prefixlen;
498 ifc->destination = (struct prefix *)p;
f52d0a1a
DS
499 } else {
500 if (CHECK_FLAG(ifc->flags, ZEBRA_IFA_PEER)) {
9df414fe
QY
501 zlog_debug(
502 "warning: %s called for interface %s with peer flag set, but no peer address supplied",
503 __func__, ifp->name);
f52d0a1a
DS
504 UNSET_FLAG(ifc->flags, ZEBRA_IFA_PEER);
505 }
60c0687a
DS
506 }
507
d62a17ae 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);
718e3744 523}
524
d62a17ae 525void connected_delete_ipv6(struct interface *ifp, struct in6_addr *address,
0f3af738 526 struct in6_addr *dest, uint16_t prefixlen)
718e3744 527{
60c0687a 528 struct prefix p, d;
d62a17ae 529 struct connected *ifc;
530
dc7cd304 531 memset(&p, 0, sizeof(struct prefix));
d62a17ae 532 p.family = AF_INET6;
dc7cd304 533 memcpy(&p.u.prefix6, address, sizeof(struct in6_addr));
d62a17ae 534 p.prefixlen = prefixlen;
535
0f3af738 536 if (dest) {
60c0687a
DS
537 memset(&d, 0, sizeof(struct prefix));
538 d.family = AF_INET6;
0f3af738 539 IPV6_ADDR_COPY(&d.u.prefix6, dest);
60c0687a
DS
540 d.prefixlen = prefixlen;
541 ifc = connected_check_ptp(ifp, &p, &d);
542 } else
543 ifc = connected_check_ptp(ifp, &p, NULL);
d62a17ae 544
dc7cd304 545 connected_delete_helper(ifc, &p);
718e3744 546}
d44ca835 547
d62a17ae 548int connected_is_unnumbered(struct interface *ifp)
d44ca835 549{
d62a17ae 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 }
e93a6fbb 559 return 1;
d44ca835 560}