1 // SPDX-License-Identifier: MIT
3 Copyright 2011 by Matthieu Boutier and Juliusz Chroboczek
12 #include "distribute.h"
13 #include "lib_errors.h"
16 #include "babel_main.h"
19 #include "babel_interface.h"
22 #include "babel_zebra.h"
23 #include "neighbour.h"
26 #include "babel_errors.h"
28 #ifndef VTYSH_EXTRACT_PL
29 #include "babeld/babel_interface_clippy.c"
32 DEFINE_MTYPE_STATIC(BABELD
, BABEL_IF
, "Babel Interface");
34 #define IS_ENABLE(ifp) (babel_enable_if_lookup(ifp->name) >= 0)
36 static int babel_enable_if_lookup (const char *ifname
);
37 static int babel_enable_if_add (const char *ifname
);
38 static int babel_enable_if_delete (const char *ifname
);
39 static int interface_recalculate(struct interface
*ifp
);
40 static int interface_reset(struct interface
*ifp
);
41 static int babel_if_new_hook (struct interface
*ifp
);
42 static int babel_if_delete_hook (struct interface
*ifp
);
43 static int interface_config_write (struct vty
*vty
);
44 static babel_interface_nfo
* babel_interface_allocate (void);
45 static void babel_interface_free (babel_interface_nfo
*bi
);
48 static vector babel_enable_if
; /* enable interfaces (by cmd). */
50 int babel_ifp_up(struct interface
*ifp
)
52 debugf(BABEL_DEBUG_IF
, "receive a 'interface up'");
54 interface_recalculate(ifp
);
59 babel_ifp_down(struct interface
*ifp
)
61 debugf(BABEL_DEBUG_IF
, "receive a 'interface down'");
71 int babel_ifp_create (struct interface
*ifp
)
73 debugf(BABEL_DEBUG_IF
, "receive a 'interface add'");
75 interface_recalculate(ifp
);
81 babel_ifp_destroy(struct interface
*ifp
)
83 debugf(BABEL_DEBUG_IF
, "receive a 'interface delete'");
92 babel_interface_address_add (ZAPI_CALLBACK_ARGS
)
94 babel_interface_nfo
*babel_ifp
;
95 struct connected
*ifc
;
96 struct prefix
*prefix
;
98 debugf(BABEL_DEBUG_IF
, "receive a 'interface address add'");
100 ifc
= zebra_interface_address_read (ZEBRA_INTERFACE_ADDRESS_ADD
,
101 zclient
->ibuf
, vrf_id
);
106 prefix
= ifc
->address
;
108 if (prefix
->family
== AF_INET
) {
109 flush_interface_routes(ifc
->ifp
, 0);
110 babel_ifp
= babel_get_if_nfo(ifc
->ifp
);
111 if (babel_ifp
->ipv4
== NULL
) {
112 babel_ifp
->ipv4
= malloc(4);
113 if (babel_ifp
->ipv4
== NULL
) {
114 flog_err(EC_BABEL_MEMORY
, "not enough memory");
116 memcpy(babel_ifp
->ipv4
, &prefix
->u
.prefix4
, 4);
121 send_request(ifc
->ifp
, NULL
, 0);
122 send_update(ifc
->ifp
, 0, NULL
, 0);
128 babel_interface_address_delete (ZAPI_CALLBACK_ARGS
)
130 babel_interface_nfo
*babel_ifp
;
131 struct connected
*ifc
;
132 struct prefix
*prefix
;
134 debugf(BABEL_DEBUG_IF
, "receive a 'interface address delete'");
136 ifc
= zebra_interface_address_read (ZEBRA_INTERFACE_ADDRESS_DELETE
,
137 zclient
->ibuf
, vrf_id
);
142 prefix
= ifc
->address
;
144 if (prefix
->family
== AF_INET
) {
145 flush_interface_routes(ifc
->ifp
, 0);
146 babel_ifp
= babel_get_if_nfo(ifc
->ifp
);
147 if (babel_ifp
->ipv4
!= NULL
148 && memcmp(babel_ifp
->ipv4
, &prefix
->u
.prefix4
, IPV4_MAX_BYTELEN
)
150 free(babel_ifp
->ipv4
);
151 babel_ifp
->ipv4
= NULL
;
155 send_request(ifc
->ifp
, NULL
, 0);
156 send_update(ifc
->ifp
, 0, NULL
, 0);
158 connected_free(&ifc
);
162 /* Lookup function. */
164 babel_enable_if_lookup (const char *ifname
)
169 for (i
= 0; i
< vector_active (babel_enable_if
); i
++)
170 if ((str
= vector_slot (babel_enable_if
, i
)) != NULL
)
171 if (strcmp (str
, ifname
) == 0)
176 /* Add interface to babel_enable_if. */
178 babel_enable_if_add (const char *ifname
)
181 struct interface
*ifp
= NULL
;
183 ret
= babel_enable_if_lookup (ifname
);
187 vector_set (babel_enable_if
, strdup (ifname
));
189 ifp
= if_lookup_by_name(ifname
, VRF_DEFAULT
);
191 interface_recalculate(ifp
);
196 /* Delete interface from babel_enable_if. */
198 babel_enable_if_delete (const char *ifname
)
200 int babel_enable_if_index
;
202 struct interface
*ifp
= NULL
;
204 babel_enable_if_index
= babel_enable_if_lookup (ifname
);
205 if (babel_enable_if_index
< 0)
208 str
= vector_slot (babel_enable_if
, babel_enable_if_index
);
210 vector_unset (babel_enable_if
, babel_enable_if_index
);
212 ifp
= if_lookup_by_name(ifname
, VRF_DEFAULT
);
214 interface_reset(ifp
);
219 /* [Babel Command] Babel enable on specified interface or matched network. */
220 DEFUN (babel_network
,
222 "network IF_OR_ADDR",
223 "Enable Babel protocol on specified interface or network.\n"
224 "Interface or address\n")
229 ret
= str2prefix (argv
[1]->arg
, &p
);
231 /* Given string is: */
232 if (ret
) /* an IPv4 or v6 network */
233 return CMD_ERR_NO_MATCH
; /* not implemented yet */
234 else /* an interface name */
235 ret
= babel_enable_if_add (argv
[1]->arg
);
238 vty_out (vty
, "There is same network configuration %s\n",
246 /* [Babel Command] Babel enable on specified interface or matched network. */
247 DEFUN (no_babel_network
,
248 no_babel_network_cmd
,
249 "no network IF_OR_ADDR",
251 "Disable Babel protocol on specified interface or network.\n"
252 "Interface or address\n")
257 ret
= str2prefix (argv
[2]->arg
, &p
);
259 /* Given string is: */
260 if (ret
) /* an IPv4 or v6 network */
261 return CMD_ERR_NO_MATCH
; /* not implemented yet */
262 else /* an interface name */
263 ret
= babel_enable_if_delete (argv
[2]->arg
);
266 vty_out (vty
, "can't find network %s\n",argv
[2]->arg
);
267 return CMD_WARNING_CONFIG_FAILED
;
273 /* There are a number of interface parameters that must be changed when
274 an interface becomes wired/wireless. In Quagga, they cannot be
275 configured separately. */
278 babel_set_wired_internal(babel_interface_nfo
*babel_ifp
, int wired
)
281 babel_ifp
->flags
|= BABEL_IF_WIRED
;
282 babel_ifp
->flags
|= BABEL_IF_SPLIT_HORIZON
;
283 babel_ifp
->cost
= BABEL_DEFAULT_RXCOST_WIRED
;
284 babel_ifp
->channel
= BABEL_IF_CHANNEL_NONINTERFERING
;
285 babel_ifp
->flags
&= ~BABEL_IF_LQ
;
287 babel_ifp
->flags
&= ~BABEL_IF_WIRED
;
288 babel_ifp
->flags
&= ~BABEL_IF_SPLIT_HORIZON
;
289 babel_ifp
->cost
= BABEL_DEFAULT_RXCOST_WIRELESS
;
290 babel_ifp
->channel
= BABEL_IF_CHANNEL_INTERFERING
;
291 babel_ifp
->flags
|= BABEL_IF_LQ
;
296 /* [Interface Command] Tell the interface is wire. */
297 DEFPY (babel_set_wired
,
301 "Babel interface commands\n"
302 "Enable wired optimizations\n")
304 VTY_DECLVAR_CONTEXT(interface
, ifp
);
305 babel_interface_nfo
*babel_ifp
;
307 babel_ifp
= babel_get_if_nfo(ifp
);
309 assert (babel_ifp
!= NULL
);
310 babel_set_wired_internal(babel_ifp
, no
? 0 : 1);
314 /* [Interface Command] Tell the interface is wireless (default). */
315 DEFPY (babel_set_wireless
,
316 babel_set_wireless_cmd
,
317 "[no] babel wireless",
319 "Babel interface commands\n"
320 "Disable wired optimizations (assume wireless)\n")
322 VTY_DECLVAR_CONTEXT(interface
, ifp
);
323 babel_interface_nfo
*babel_ifp
;
325 babel_ifp
= babel_get_if_nfo(ifp
);
327 assert (babel_ifp
!= NULL
);
328 babel_set_wired_internal(babel_ifp
, no
? 1 : 0);
332 /* [Interface Command] Enable split horizon. */
333 DEFPY (babel_split_horizon
,
334 babel_split_horizon_cmd
,
335 "[no] babel split-horizon",
337 "Babel interface commands\n"
338 "Enable split horizon processing\n")
340 VTY_DECLVAR_CONTEXT(interface
, ifp
);
341 babel_interface_nfo
*babel_ifp
;
343 babel_ifp
= babel_get_if_nfo(ifp
);
345 assert (babel_ifp
!= NULL
);
347 SET_FLAG(babel_ifp
->flags
, BABEL_IF_SPLIT_HORIZON
);
349 UNSET_FLAG(babel_ifp
->flags
, BABEL_IF_SPLIT_HORIZON
);
353 /* [Interface Command]. */
354 DEFPY (babel_set_hello_interval
,
355 babel_set_hello_interval_cmd
,
356 "[no] babel hello-interval (20-655340)",
358 "Babel interface commands\n"
359 "Time between scheduled hellos\n"
362 VTY_DECLVAR_CONTEXT(interface
, ifp
);
363 babel_interface_nfo
*babel_ifp
;
365 babel_ifp
= babel_get_if_nfo(ifp
);
366 assert (babel_ifp
!= NULL
);
368 babel_ifp
->hello_interval
= no
?
369 BABEL_DEFAULT_HELLO_INTERVAL
: hello_interval
;
373 /* [Interface Command]. */
374 DEFPY (babel_set_update_interval
,
375 babel_set_update_interval_cmd
,
376 "[no] babel update-interval (20-655340)",
378 "Babel interface commands\n"
379 "Time between scheduled updates\n"
382 VTY_DECLVAR_CONTEXT(interface
, ifp
);
383 babel_interface_nfo
*babel_ifp
;
385 babel_ifp
= babel_get_if_nfo(ifp
);
386 assert (babel_ifp
!= NULL
);
388 babel_ifp
->update_interval
= no
?
389 BABEL_DEFAULT_UPDATE_INTERVAL
: update_interval
;
393 DEFPY (babel_set_rxcost
,
394 babel_set_rxcost_cmd
,
395 "[no] babel rxcost (1-65534)",
397 "Babel interface commands\n"
398 "Rxcost multiplier\n"
401 VTY_DECLVAR_CONTEXT(interface
, ifp
);
402 babel_interface_nfo
*babel_ifp
;
404 babel_ifp
= babel_get_if_nfo(ifp
);
405 assert (babel_ifp
!= NULL
);
408 rxcost
= CHECK_FLAG(babel_ifp
->flags
, BABEL_IF_WIRED
) ?
409 BABEL_DEFAULT_RXCOST_WIRED
: BABEL_DEFAULT_RXCOST_WIRELESS
;
411 babel_ifp
->cost
= rxcost
;
415 DEFPY (babel_set_rtt_decay
,
416 babel_set_rtt_decay_cmd
,
417 "[no] babel rtt-decay (1-256)",
419 "Babel interface commands\n"
420 "Decay factor for exponential moving average of RTT samples\n"
423 VTY_DECLVAR_CONTEXT(interface
, ifp
);
424 babel_interface_nfo
*babel_ifp
;
426 babel_ifp
= babel_get_if_nfo(ifp
);
427 assert (babel_ifp
!= NULL
);
429 babel_ifp
->rtt_decay
= no
? BABEL_DEFAULT_RTT_DECAY
: rtt_decay
;
433 DEFPY (babel_set_rtt_min
,
434 babel_set_rtt_min_cmd
,
435 "[no] babel rtt-min (1-65535)",
437 "Babel interface commands\n"
438 "Minimum RTT starting for increasing cost\n"
441 VTY_DECLVAR_CONTEXT(interface
, ifp
);
442 babel_interface_nfo
*babel_ifp
;
444 babel_ifp
= babel_get_if_nfo(ifp
);
445 assert (babel_ifp
!= NULL
);
447 /* The value is entered in milliseconds but stored as microseconds. */
448 babel_ifp
->rtt_min
= no
? BABEL_DEFAULT_RTT_MIN
: rtt_min
* 1000;
452 DEFPY (babel_set_rtt_max
,
453 babel_set_rtt_max_cmd
,
454 "[no] babel rtt-max (1-65535)",
456 "Babel interface commands\n"
460 VTY_DECLVAR_CONTEXT(interface
, ifp
);
461 babel_interface_nfo
*babel_ifp
;
463 babel_ifp
= babel_get_if_nfo(ifp
);
464 assert (babel_ifp
!= NULL
);
466 /* The value is entered in milliseconds but stored as microseconds. */
467 babel_ifp
->rtt_max
= no
? BABEL_DEFAULT_RTT_MAX
: rtt_max
* 1000;
471 DEFPY (babel_set_max_rtt_penalty
,
472 babel_set_max_rtt_penalty_cmd
,
473 "[no] babel max-rtt-penalty (0-65535)",
475 "Babel interface commands\n"
476 "Maximum additional cost due to RTT\n"
479 VTY_DECLVAR_CONTEXT(interface
, ifp
);
480 babel_interface_nfo
*babel_ifp
;
482 babel_ifp
= babel_get_if_nfo(ifp
);
483 assert (babel_ifp
!= NULL
);
485 babel_ifp
->max_rtt_penalty
= no
?
486 BABEL_DEFAULT_MAX_RTT_PENALTY
: max_rtt_penalty
;
490 DEFPY (babel_set_enable_timestamps
,
491 babel_set_enable_timestamps_cmd
,
492 "[no] babel enable-timestamps",
494 "Babel interface commands\n"
495 "Enable timestamps\n")
497 VTY_DECLVAR_CONTEXT(interface
, ifp
);
498 babel_interface_nfo
*babel_ifp
;
500 babel_ifp
= babel_get_if_nfo(ifp
);
501 assert (babel_ifp
!= NULL
);
503 SET_FLAG(babel_ifp
->flags
, BABEL_IF_TIMESTAMPS
);
505 UNSET_FLAG(babel_ifp
->flags
, BABEL_IF_TIMESTAMPS
);
509 DEFPY (babel_set_channel
,
510 babel_set_channel_cmd
,
511 "[no] babel channel <(1-254)$ch|interfering$interfering|"
512 "noninterfering$noninterfering>",
514 "Babel interface commands\n"
515 "Channel number for diversity routing\n"
517 "Mark channel as interfering\n"
518 "Mark channel as noninterfering\n"
521 VTY_DECLVAR_CONTEXT(interface
, ifp
);
522 babel_interface_nfo
*babel_ifp
;
524 babel_ifp
= babel_get_if_nfo(ifp
);
525 assert (babel_ifp
!= NULL
);
528 ch
= CHECK_FLAG(babel_ifp
->flags
, BABEL_IF_WIRED
) ?
529 BABEL_IF_CHANNEL_NONINTERFERING
: BABEL_IF_CHANNEL_INTERFERING
;
530 else if (interfering
)
531 ch
= BABEL_IF_CHANNEL_INTERFERING
;
532 else if (noninterfering
)
533 ch
= BABEL_IF_CHANNEL_NONINTERFERING
;
535 babel_ifp
->channel
= ch
;
539 /* This should be no more than half the hello interval, so that hellos
540 aren't sent late. The result is in milliseconds. */
542 jitter(babel_interface_nfo
*babel_ifp
, int urgent
)
544 unsigned interval
= babel_ifp
->hello_interval
;
546 interval
= MIN(interval
, 100);
548 interval
= MIN(interval
, 4000);
549 return roughly(interval
) / 4;
553 update_jitter(babel_interface_nfo
*babel_ifp
, int urgent
)
555 unsigned interval
= babel_ifp
->hello_interval
;
557 interval
= MIN(interval
, 100);
559 interval
= MIN(interval
, 4000);
560 return roughly(interval
);
563 /* calculate babeld's specific datas of an interface (change when the interface
566 interface_recalculate(struct interface
*ifp
)
568 babel_interface_nfo
*babel_ifp
= babel_get_if_nfo(ifp
);
569 unsigned char *tmp
= NULL
;
571 struct ipv6_mreq mreq
;
576 if (!if_is_operative(ifp
) || !CHECK_FLAG(ifp
->flags
, IFF_RUNNING
)) {
577 interface_reset(ifp
);
581 babel_ifp
->flags
|= BABEL_IF_IS_UP
;
583 mtu
= MIN(ifp
->mtu
, ifp
->mtu6
);
585 /* We need to be able to fit at least two messages into a packet,
586 so MTUs below 116 require lower layer fragmentation. */
587 /* In IPv6, the minimum MTU is 1280, and every host must be able
588 to reassemble up to 1500 bytes, but I'd rather not rely on this. */
590 debugf(BABEL_DEBUG_IF
, "Suspiciously low MTU %d on interface %s (%d).",
591 mtu
, ifp
->name
, ifp
->ifindex
);
595 /* 4 for Babel header; 40 for IPv6 header, 8 for UDP header, 12 for good luck. */
596 babel_ifp
->bufsize
= mtu
- 4 - 60;
597 tmp
= babel_ifp
->sendbuf
;
598 babel_ifp
->sendbuf
= realloc(babel_ifp
->sendbuf
, babel_ifp
->bufsize
);
599 if(babel_ifp
->sendbuf
== NULL
) {
600 flog_err(EC_BABEL_MEMORY
, "Couldn't reallocate sendbuf.");
602 babel_ifp
->bufsize
= 0;
607 rc
= resize_receive_buffer(mtu
);
609 zlog_warn("couldn't resize receive buffer for interface %s (%d) (%d bytes).",
610 ifp
->name
, ifp
->ifindex
, mtu
);
612 memset(&mreq
, 0, sizeof(mreq
));
613 memcpy(&mreq
.ipv6mr_multiaddr
, protocol_group
, 16);
614 mreq
.ipv6mr_interface
= ifp
->ifindex
;
616 rc
= setsockopt(protocol_socket
, IPPROTO_IPV6
, IPV6_JOIN_GROUP
,
617 (char*)&mreq
, sizeof(mreq
));
618 if (rc
< 0 && errno
!= EADDRINUSE
) {
619 flog_err_sys(EC_LIB_SOCKET
,
620 "setsockopt(IPV6_JOIN_GROUP) on interface '%s': %s",
621 ifp
->name
, safe_strerror(errno
));
622 /* This is probably due to a missing link-local address,
623 so down this interface, and wait until the main loop
624 tries to up it again. */
625 interface_reset(ifp
);
629 set_timeout(&babel_ifp
->hello_timeout
, babel_ifp
->hello_interval
);
630 set_timeout(&babel_ifp
->update_timeout
, babel_ifp
->update_interval
);
632 send_request(ifp
, NULL
, 0);
634 update_interface_metric(ifp
);
636 debugf(BABEL_DEBUG_COMMON
,
637 "Upped interface %s (%s, cost=%d, channel=%d%s).",
639 (babel_ifp
->flags
& BABEL_IF_WIRED
) ? "wired" : "wireless",
642 babel_ifp
->ipv4
? ", IPv4" : "");
645 send_update(ifp
, 0, NULL
, 0);
650 /* Reset the interface as it was new: it's not removed from the interface list,
651 and may be considered as a upped interface. */
653 interface_reset(struct interface
*ifp
)
656 struct ipv6_mreq mreq
;
657 babel_interface_nfo
*babel_ifp
= babel_get_if_nfo(ifp
);
659 if (!(babel_ifp
->flags
& BABEL_IF_IS_UP
))
662 debugf(BABEL_DEBUG_IF
, "interface reset: %s", ifp
->name
);
663 babel_ifp
->flags
&= ~BABEL_IF_IS_UP
;
665 flush_interface_routes(ifp
, 0);
666 babel_ifp
->buffered
= 0;
667 babel_ifp
->bufsize
= 0;
668 free(babel_ifp
->sendbuf
);
669 babel_ifp
->num_buffered_updates
= 0;
670 babel_ifp
->update_bufsize
= 0;
671 if(babel_ifp
->buffered_updates
)
672 free(babel_ifp
->buffered_updates
);
673 babel_ifp
->buffered_updates
= NULL
;
674 babel_ifp
->sendbuf
= NULL
;
676 if(ifp
->ifindex
> 0) {
677 memset(&mreq
, 0, sizeof(mreq
));
678 memcpy(&mreq
.ipv6mr_multiaddr
, protocol_group
, 16);
679 mreq
.ipv6mr_interface
= ifp
->ifindex
;
680 rc
= setsockopt(protocol_socket
, IPPROTO_IPV6
, IPV6_LEAVE_GROUP
,
681 (char*)&mreq
, sizeof(mreq
));
683 flog_err_sys(EC_LIB_SOCKET
,
684 "setsockopt(IPV6_LEAVE_GROUP) on interface '%s': %s",
685 ifp
->name
, safe_strerror(errno
));
688 update_interface_metric(ifp
);
690 debugf(BABEL_DEBUG_COMMON
,"Upped network %s (%s, cost=%d%s).",
692 (babel_ifp
->flags
& BABEL_IF_WIRED
) ? "wired" : "wireless",
694 babel_ifp
->ipv4
? ", IPv4" : "");
699 /* Send retraction to all, and reset all interfaces statistics. */
701 babel_interface_close_all(void)
703 struct vrf
*vrf
= vrf_lookup_by_id(VRF_DEFAULT
);
704 struct interface
*ifp
= NULL
;
706 FOR_ALL_INTERFACES(vrf
, ifp
) {
709 send_wildcard_retraction(ifp
);
710 /* Make sure that we expire quickly from our neighbours'
711 association caches. */
712 send_hello_noupdate(ifp
, 10);
714 usleep(roughly(1000));
717 FOR_ALL_INTERFACES(vrf
, ifp
) {
720 /* Make sure they got it. */
721 send_wildcard_retraction(ifp
);
722 send_hello_noupdate(ifp
, 1);
724 usleep(roughly(10000));
726 interface_reset(ifp
);
730 /* return "true" if address is one of our ipv6 addresses */
732 is_interface_ll_address(struct interface
*ifp
, const unsigned char *address
)
734 struct connected
*connected
;
735 struct listnode
*node
;
740 FOR_ALL_INTERFACES_ADDRESSES(ifp
, connected
, node
) {
741 if (connected
->address
->family
== AF_INET6
742 && memcmp(&connected
->address
->u
.prefix6
, address
,
752 show_babel_interface_sub (struct vty
*vty
, struct interface
*ifp
)
755 babel_interface_nfo
*babel_ifp
;
757 vty_out (vty
, "%s is %s\n", ifp
->name
,
758 ((is_up
= if_is_operative(ifp
)) ? "up" : "down"));
759 vty_out (vty
, " ifindex %u, MTU %u bytes %s\n",
760 ifp
->ifindex
, MIN(ifp
->mtu
, ifp
->mtu6
), if_flag_dump(ifp
->flags
));
764 vty_out (vty
, " Babel protocol is not enabled on this interface\n");
770 " Babel protocol is enabled, but not running on this interface\n");
773 babel_ifp
= babel_get_if_nfo (ifp
);
774 vty_out (vty
, " Babel protocol is running on this interface\n");
775 vty_out (vty
, " Operating mode is \"%s\"\n",
776 CHECK_FLAG(babel_ifp
->flags
, BABEL_IF_WIRED
) ? "wired" : "wireless");
777 vty_out (vty
, " Split horizon mode is %s\n",
778 CHECK_FLAG(babel_ifp
->flags
, BABEL_IF_SPLIT_HORIZON
) ? "On" : "Off");
779 vty_out (vty
, " Hello interval is %u ms\n", babel_ifp
->hello_interval
);
780 vty_out (vty
, " Update interval is %u ms\n", babel_ifp
->update_interval
);
781 vty_out (vty
, " Rxcost multiplier is %u\n", babel_ifp
->cost
);
784 DEFUN (show_babel_interface
,
785 show_babel_interface_cmd
,
786 "show babel interface [IFNAME]",
788 "Babel information\n"
789 "Interface information\n"
792 struct vrf
*vrf
= vrf_lookup_by_id(VRF_DEFAULT
);
793 struct interface
*ifp
;
797 FOR_ALL_INTERFACES (vrf
, ifp
)
798 show_babel_interface_sub (vty
, ifp
);
801 if ((ifp
= if_lookup_by_name (argv
[3]->arg
, VRF_DEFAULT
)) == NULL
)
803 vty_out (vty
, "No such interface name\n");
806 show_babel_interface_sub (vty
, ifp
);
811 show_babel_neighbour_sub (struct vty
*vty
, struct neighbour
*neigh
)
814 "Neighbour %s dev %s reach %04x rxcost %d txcost %d rtt %s rttcost %d%s.\n",
815 format_address(neigh
->address
),
818 neighbour_rxcost(neigh
),
820 format_thousands(neigh
->rtt
),
821 neighbour_rttcost(neigh
),
822 if_up(neigh
->ifp
) ? "" : " (down)");
825 DEFUN (show_babel_neighbour
,
826 show_babel_neighbour_cmd
,
827 "show babel neighbor [IFNAME]",
829 "Babel information\n"
833 struct neighbour
*neigh
;
834 struct interface
*ifp
;
837 FOR_ALL_NEIGHBOURS(neigh
) {
838 show_babel_neighbour_sub(vty
, neigh
);
842 if ((ifp
= if_lookup_by_name (argv
[3]->arg
, VRF_DEFAULT
)) == NULL
)
844 vty_out (vty
, "No such interface name\n");
847 FOR_ALL_NEIGHBOURS(neigh
) {
848 if(ifp
->ifindex
== neigh
->ifp
->ifindex
) {
849 show_babel_neighbour_sub(vty
, neigh
);
856 babel_prefix_eq(struct prefix
*prefix
, unsigned char *p
, int plen
)
858 if(prefix
->family
== AF_INET6
) {
859 if (prefix
->prefixlen
!= plen
860 || memcmp(&prefix
->u
.prefix6
, p
, IPV6_MAX_BYTELEN
) != 0)
862 } else if(prefix
->family
== AF_INET
) {
863 if (plen
< 96 || !v4mapped(p
) || prefix
->prefixlen
!= plen
- 96
864 || memcmp(&prefix
->u
.prefix4
, p
+ 12, IPV4_MAX_BYTELEN
) != 0)
874 show_babel_routes_sub(struct babel_route
*route
, struct vty
*vty
,
875 struct prefix
*prefix
)
877 const unsigned char *nexthop
=
878 memcmp(route
->nexthop
, route
->neigh
->address
, IPV6_MAX_BYTELEN
)
885 && !babel_prefix_eq(prefix
, route
->src
->prefix
, route
->src
->plen
))
888 if (route
->channels
[0] == 0)
892 snprintf(channels
, sizeof(channels
), " chan (");
893 j
= strlen(channels
);
894 for (k
= 0; k
< DIVERSITY_HOPS
; k
++) {
895 if (route
->channels
[k
] == 0)
899 snprintf(channels
+ j
, 100 - j
, "%u",
901 j
= strlen(channels
);
903 snprintf(channels
+ j
, 100 - j
, ")");
909 "%s metric %d refmetric %d id %s seqno %d%s age %d via %s neigh %s%s%s%s\n",
910 format_prefix(route
->src
->prefix
, route
->src
->plen
),
911 route_metric(route
), route
->refmetric
,
912 format_eui64(route
->src
->id
),
915 (int)(babel_now
.tv_sec
- route
->time
),
916 route
->neigh
->ifp
->name
,
917 format_address(route
->neigh
->address
),
918 nexthop
? " nexthop " : "",
919 nexthop
? format_address(nexthop
) : "",
920 route
->installed
? " (installed)" : route_feasible(route
) ? " (feasible)" : "");
924 show_babel_xroutes_sub (struct xroute
*xroute
, struct vty
*vty
,
925 struct prefix
*prefix
)
927 if(prefix
&& !babel_prefix_eq(prefix
, xroute
->prefix
, xroute
->plen
))
930 vty_out (vty
, "%s metric %d (exported)\n",
931 format_prefix(xroute
->prefix
, xroute
->plen
),
935 DEFUN (show_babel_route
,
936 show_babel_route_cmd
,
939 "Babel information\n"
940 "Babel internal routing table\n")
942 struct route_stream
*routes
= NULL
;
943 struct xroute_stream
*xroutes
= NULL
;
944 routes
= route_stream(0);
947 struct babel_route
*route
= route_stream_next(routes
);
950 show_babel_routes_sub(route
, vty
, NULL
);
952 route_stream_done(routes
);
954 flog_err(EC_BABEL_MEMORY
, "Couldn't allocate route stream.");
956 xroutes
= xroute_stream();
959 struct xroute
*xroute
= xroute_stream_next(xroutes
);
962 show_babel_xroutes_sub(xroute
, vty
, NULL
);
964 xroute_stream_done(xroutes
);
966 flog_err(EC_BABEL_MEMORY
, "Couldn't allocate route stream.");
971 DEFUN (show_babel_route_prefix
,
972 show_babel_route_prefix_cmd
,
973 "show babel route <A.B.C.D/M|X:X::X:X/M>",
975 "Babel information\n"
976 "Babel internal routing table\n"
977 "IPv4 prefix <network>/<length>\n"
978 "IPv6 prefix <network>/<length>\n")
980 struct route_stream
*routes
= NULL
;
981 struct xroute_stream
*xroutes
= NULL
;
982 struct prefix prefix
;
985 ret
= str2prefix(argv
[3]->arg
, &prefix
);
987 vty_out (vty
, "%% Malformed address\n");
991 routes
= route_stream(0);
994 struct babel_route
*route
= route_stream_next(routes
);
997 show_babel_routes_sub(route
, vty
, &prefix
);
999 route_stream_done(routes
);
1001 flog_err(EC_BABEL_MEMORY
, "Couldn't allocate route stream.");
1003 xroutes
= xroute_stream();
1006 struct xroute
*xroute
= xroute_stream_next(xroutes
);
1009 show_babel_xroutes_sub(xroute
, vty
, &prefix
);
1011 xroute_stream_done(xroutes
);
1013 flog_err(EC_BABEL_MEMORY
, "Couldn't allocate route stream.");
1019 DEFUN (show_babel_route_addr
,
1020 show_babel_route_addr_cmd
,
1021 "show babel route A.B.C.D",
1023 "Babel information\n"
1024 "Babel internal routing table\n"
1025 "IPv4 address <network>/<length>\n")
1027 struct in_addr addr
;
1028 char buf
[INET_ADDRSTRLEN
+ 8];
1029 char buf1
[INET_ADDRSTRLEN
+ 8];
1030 struct route_stream
*routes
= NULL
;
1031 struct xroute_stream
*xroutes
= NULL
;
1032 struct prefix prefix
;
1035 ret
= inet_aton (argv
[3]->arg
, &addr
);
1037 vty_out (vty
, "%% Malformed address\n");
1041 /* Quagga has no convenient prefix constructors. */
1042 snprintf(buf
, sizeof(buf
), "%s/%d",
1043 inet_ntop(AF_INET
, &addr
, buf1
, sizeof(buf1
)), 32);
1045 ret
= str2prefix(buf
, &prefix
);
1047 vty_out (vty
, "%% Parse error -- this shouldn't happen\n");
1051 routes
= route_stream(0);
1054 struct babel_route
*route
= route_stream_next(routes
);
1057 show_babel_routes_sub(route
, vty
, &prefix
);
1059 route_stream_done(routes
);
1061 flog_err(EC_BABEL_MEMORY
, "Couldn't allocate route stream.");
1063 xroutes
= xroute_stream();
1066 struct xroute
*xroute
= xroute_stream_next(xroutes
);
1069 show_babel_xroutes_sub(xroute
, vty
, &prefix
);
1071 xroute_stream_done(xroutes
);
1073 flog_err(EC_BABEL_MEMORY
, "Couldn't allocate route stream.");
1078 DEFUN (show_babel_route_addr6
,
1079 show_babel_route_addr6_cmd
,
1080 "show babel route X:X::X:X",
1082 "Babel information\n"
1083 "Babel internal routing table\n"
1084 "IPv6 address <network>/<length>\n")
1086 struct in6_addr addr
;
1087 char buf1
[INET6_ADDRSTRLEN
];
1088 char buf
[INET6_ADDRSTRLEN
+ 8];
1089 struct route_stream
*routes
= NULL
;
1090 struct xroute_stream
*xroutes
= NULL
;
1091 struct prefix prefix
;
1094 ret
= inet_pton (AF_INET6
, argv
[3]->arg
, &addr
);
1096 vty_out (vty
, "%% Malformed address\n");
1100 /* Quagga has no convenient prefix constructors. */
1101 snprintf(buf
, sizeof(buf
), "%s/%d",
1102 inet_ntop(AF_INET6
, &addr
, buf1
, sizeof(buf1
)), 128);
1104 ret
= str2prefix(buf
, &prefix
);
1106 vty_out (vty
, "%% Parse error -- this shouldn't happen\n");
1110 routes
= route_stream(0);
1113 struct babel_route
*route
= route_stream_next(routes
);
1116 show_babel_routes_sub(route
, vty
, &prefix
);
1118 route_stream_done(routes
);
1120 flog_err(EC_BABEL_MEMORY
, "Couldn't allocate route stream.");
1122 xroutes
= xroute_stream();
1125 struct xroute
*xroute
= xroute_stream_next(xroutes
);
1128 show_babel_xroutes_sub(xroute
, vty
, &prefix
);
1130 xroute_stream_done(xroutes
);
1132 flog_err(EC_BABEL_MEMORY
, "Couldn't allocate route stream.");
1137 DEFUN (show_babel_parameters
,
1138 show_babel_parameters_cmd
,
1139 "show babel parameters",
1141 "Babel information\n"
1142 "Configuration information\n")
1144 struct babel
*babel_ctx
;
1146 vty_out (vty
, " -- Babel running configuration --\n");
1147 show_babel_main_configuration(vty
);
1149 babel_ctx
= babel_lookup();
1151 vty_out (vty
, " -- distribution lists --\n");
1152 config_show_distribute(vty
, babel_ctx
->distribute_ctx
);
1160 /* initialize interface list */
1161 hook_register_prio(if_add
, 0, babel_if_new_hook
);
1162 hook_register_prio(if_del
, 0, babel_if_delete_hook
);
1164 babel_enable_if
= vector_init (1);
1166 /* install interface node and commands */
1167 if_cmd_init(interface_config_write
);
1169 install_element(BABEL_NODE
, &babel_network_cmd
);
1170 install_element(BABEL_NODE
, &no_babel_network_cmd
);
1171 install_element(INTERFACE_NODE
, &babel_split_horizon_cmd
);
1172 install_element(INTERFACE_NODE
, &babel_set_wired_cmd
);
1173 install_element(INTERFACE_NODE
, &babel_set_wireless_cmd
);
1174 install_element(INTERFACE_NODE
, &babel_set_hello_interval_cmd
);
1175 install_element(INTERFACE_NODE
, &babel_set_update_interval_cmd
);
1176 install_element(INTERFACE_NODE
, &babel_set_rxcost_cmd
);
1177 install_element(INTERFACE_NODE
, &babel_set_channel_cmd
);
1178 install_element(INTERFACE_NODE
, &babel_set_rtt_decay_cmd
);
1179 install_element(INTERFACE_NODE
, &babel_set_rtt_min_cmd
);
1180 install_element(INTERFACE_NODE
, &babel_set_rtt_max_cmd
);
1181 install_element(INTERFACE_NODE
, &babel_set_max_rtt_penalty_cmd
);
1182 install_element(INTERFACE_NODE
, &babel_set_enable_timestamps_cmd
);
1184 /* "show babel ..." commands */
1185 install_element(VIEW_NODE
, &show_babel_interface_cmd
);
1186 install_element(VIEW_NODE
, &show_babel_neighbour_cmd
);
1187 install_element(VIEW_NODE
, &show_babel_route_cmd
);
1188 install_element(VIEW_NODE
, &show_babel_route_prefix_cmd
);
1189 install_element(VIEW_NODE
, &show_babel_route_addr_cmd
);
1190 install_element(VIEW_NODE
, &show_babel_route_addr6_cmd
);
1191 install_element(VIEW_NODE
, &show_babel_parameters_cmd
);
1194 /* hooks: functions called respectively when struct interface is
1195 created or deleted. */
1197 babel_if_new_hook (struct interface
*ifp
)
1199 ifp
->info
= babel_interface_allocate();
1204 babel_if_delete_hook (struct interface
*ifp
)
1206 babel_interface_free(ifp
->info
);
1211 /* Output an "interface" section for each of the known interfaces with
1212 babeld-specific statement lines where appropriate. */
1214 interface_config_write (struct vty
*vty
)
1216 struct vrf
*vrf
= vrf_lookup_by_id(VRF_DEFAULT
);
1217 struct interface
*ifp
;
1220 FOR_ALL_INTERFACES (vrf
, ifp
) {
1221 if_vty_config_start(vty
, ifp
);
1223 vty_out (vty
, " description %s\n",ifp
->desc
);
1224 babel_interface_nfo
*babel_ifp
= babel_get_if_nfo (ifp
);
1225 /* wireless is the default*/
1226 if (CHECK_FLAG (babel_ifp
->flags
, BABEL_IF_WIRED
))
1228 vty_out (vty
, " babel wired\n");
1231 if (babel_ifp
->hello_interval
!= BABEL_DEFAULT_HELLO_INTERVAL
)
1233 vty_out (vty
, " babel hello-interval %u\n",
1234 babel_ifp
->hello_interval
);
1237 if (babel_ifp
->update_interval
!= BABEL_DEFAULT_UPDATE_INTERVAL
)
1239 vty_out (vty
, " babel update-interval %u\n",
1240 babel_ifp
->update_interval
);
1243 if (CHECK_FLAG(babel_ifp
->flags
, BABEL_IF_TIMESTAMPS
)) {
1244 vty_out(vty
, " babel enable-timestamps\n");
1247 if (babel_ifp
->max_rtt_penalty
!= BABEL_DEFAULT_MAX_RTT_PENALTY
) {
1248 vty_out(vty
, " babel max-rtt-penalty %u\n",
1249 babel_ifp
->max_rtt_penalty
);
1252 if (babel_ifp
->rtt_decay
!= BABEL_DEFAULT_RTT_DECAY
) {
1253 vty_out(vty
, " babel rtt-decay %u\n", babel_ifp
->rtt_decay
);
1256 if (babel_ifp
->rtt_min
!= BABEL_DEFAULT_RTT_MIN
) {
1257 vty_out(vty
, " babel rtt-min %u\n", babel_ifp
->rtt_min
/ 1000);
1260 if (babel_ifp
->rtt_max
!= BABEL_DEFAULT_RTT_MAX
) {
1261 vty_out(vty
, " babel rtt-max %u\n", babel_ifp
->rtt_max
/ 1000);
1264 /* Some parameters have different defaults for wired/wireless. */
1265 if (CHECK_FLAG (babel_ifp
->flags
, BABEL_IF_WIRED
)) {
1266 if (!CHECK_FLAG (babel_ifp
->flags
, BABEL_IF_SPLIT_HORIZON
)) {
1267 vty_out (vty
, " no babel split-horizon\n");
1270 if (babel_ifp
->cost
!= BABEL_DEFAULT_RXCOST_WIRED
) {
1271 vty_out (vty
, " babel rxcost %u\n", babel_ifp
->cost
);
1274 if (babel_ifp
->channel
== BABEL_IF_CHANNEL_INTERFERING
) {
1275 vty_out (vty
, " babel channel interfering\n");
1277 } else if(babel_ifp
->channel
!= BABEL_IF_CHANNEL_NONINTERFERING
) {
1278 vty_out (vty
, " babel channel %d\n",babel_ifp
->channel
);
1282 if (CHECK_FLAG (babel_ifp
->flags
, BABEL_IF_SPLIT_HORIZON
)) {
1283 vty_out (vty
, " babel split-horizon\n");
1286 if (babel_ifp
->cost
!= BABEL_DEFAULT_RXCOST_WIRELESS
) {
1287 vty_out (vty
, " babel rxcost %u\n", babel_ifp
->cost
);
1290 if (babel_ifp
->channel
== BABEL_IF_CHANNEL_NONINTERFERING
) {
1291 vty_out (vty
, " babel channel noninterfering\n");
1293 } else if(babel_ifp
->channel
!= BABEL_IF_CHANNEL_INTERFERING
) {
1294 vty_out (vty
, " babel channel %d\n",babel_ifp
->channel
);
1298 if_vty_config_end(vty
);
1304 /* Output a "network" statement line for each of the enabled interfaces. */
1306 babel_enable_if_config_write (struct vty
* vty
)
1308 unsigned int i
, lines
= 0;
1311 for (i
= 0; i
< vector_active (babel_enable_if
); i
++)
1312 if ((str
= vector_slot (babel_enable_if
, i
)) != NULL
)
1314 vty_out (vty
, " network %s\n", str
);
1320 /* functions to allocate or free memory for a babel_interface_nfo, filling
1322 static babel_interface_nfo
*
1323 babel_interface_allocate (void)
1325 babel_interface_nfo
*babel_ifp
;
1326 babel_ifp
= XCALLOC(MTYPE_BABEL_IF
, sizeof(babel_interface_nfo
));
1327 /* All flags are unset */
1328 babel_ifp
->bucket_time
= babel_now
.tv_sec
;
1329 babel_ifp
->bucket
= BUCKET_TOKENS_MAX
;
1330 babel_ifp
->hello_seqno
= (frr_weak_random() & 0xFFFF);
1331 babel_ifp
->rtt_decay
= BABEL_DEFAULT_RTT_DECAY
;
1332 babel_ifp
->rtt_min
= BABEL_DEFAULT_RTT_MIN
;
1333 babel_ifp
->rtt_max
= BABEL_DEFAULT_RTT_MAX
;
1334 babel_ifp
->max_rtt_penalty
= BABEL_DEFAULT_MAX_RTT_PENALTY
;
1335 babel_ifp
->hello_interval
= BABEL_DEFAULT_HELLO_INTERVAL
;
1336 babel_ifp
->update_interval
= BABEL_DEFAULT_UPDATE_INTERVAL
;
1337 babel_ifp
->channel
= BABEL_IF_CHANNEL_INTERFERING
;
1338 babel_set_wired_internal(babel_ifp
, 0);
1344 babel_interface_free (babel_interface_nfo
*babel_ifp
)
1346 XFREE(MTYPE_BABEL_IF
, babel_ifp
);