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