3 * Copyright (C) 1997, 1999 Kunihiro Ishiguro
5 * This file is part of GNU Zebra.
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
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.
17 * You should have received a copy of the GNU General Public License
18 * along with GNU Zebra; see the file COPYING. If not, write to the Free
19 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
27 #include "sockunion.h"
32 #include "connected.h"
36 #include "zebra/interface.h"
37 #include "zebra/rtadv.h"
38 #include "zebra/rib.h"
39 #include "zebra/zserv.h"
40 #include "zebra/redistribute.h"
41 #include "zebra/debug.h"
42 #include "zebra/irdp.h"
45 /* Order is intentional. Matches RFC4191. This array is also used for
46 command matching, so only modify with care. */
47 const char *rtadv_pref_strs
[] = { "medium", "high", "INVALID", "low", 0 };
50 /* Called when new interface is added. */
52 if_zebra_new_hook (struct interface
*ifp
)
54 struct zebra_if
*zebra_if
;
56 zebra_if
= XCALLOC (MTYPE_TMP
, sizeof (struct zebra_if
));
58 zebra_if
->multicast
= IF_ZEBRA_MULTICAST_UNSPEC
;
59 zebra_if
->shutdown
= IF_ZEBRA_SHUTDOWN_UNSPEC
;
63 /* Set default router advertise values. */
64 struct rtadvconf
*rtadv
;
66 rtadv
= &zebra_if
->rtadv
;
68 rtadv
->AdvSendAdvertisements
= 0;
69 rtadv
->MaxRtrAdvInterval
= RTADV_MAX_RTR_ADV_INTERVAL
;
70 rtadv
->MinRtrAdvInterval
= RTADV_MIN_RTR_ADV_INTERVAL
;
71 rtadv
->AdvIntervalTimer
= 0;
72 rtadv
->AdvManagedFlag
= 0;
73 rtadv
->AdvOtherConfigFlag
= 0;
74 rtadv
->AdvHomeAgentFlag
= 0;
75 rtadv
->AdvLinkMTU
= 0;
76 rtadv
->AdvReachableTime
= 0;
77 rtadv
->AdvRetransTimer
= 0;
78 rtadv
->AdvCurHopLimit
= 0;
79 rtadv
->AdvDefaultLifetime
= RTADV_ADV_DEFAULT_LIFETIME
;
80 rtadv
->HomeAgentPreference
= 0;
81 rtadv
->HomeAgentLifetime
= RTADV_ADV_DEFAULT_LIFETIME
;
82 rtadv
->AdvIntervalOption
= 0;
83 rtadv
->DefaultPreference
= RTADV_PREF_MEDIUM
;
85 rtadv
->AdvPrefixList
= list_new ();
89 /* Initialize installed address chains tree. */
90 zebra_if
->ipv4_subnets
= route_table_init ();
96 /* Called when interface is deleted. */
98 if_zebra_delete_hook (struct interface
*ifp
)
100 struct zebra_if
*zebra_if
;
104 zebra_if
= ifp
->info
;
106 /* Free installed address chains tree. */
107 if (zebra_if
->ipv4_subnets
)
108 route_table_finish (zebra_if
->ipv4_subnets
);
110 XFREE (MTYPE_TMP
, zebra_if
);
116 /* Tie an interface address to its derived subnet list of addresses. */
118 if_subnet_add (struct interface
*ifp
, struct connected
*ifc
)
120 struct route_node
*rn
;
121 struct zebra_if
*zebra_if
;
123 struct list
*addr_list
;
125 assert (ifp
&& ifp
->info
&& ifc
);
126 zebra_if
= ifp
->info
;
128 /* Get address derived subnet node and associated address list, while marking
129 address secondary attribute appropriately. */
132 rn
= route_node_get (zebra_if
->ipv4_subnets
, &cp
);
134 if ((addr_list
= rn
->info
))
135 SET_FLAG (ifc
->flags
, ZEBRA_IFA_SECONDARY
);
138 UNSET_FLAG (ifc
->flags
, ZEBRA_IFA_SECONDARY
);
139 rn
->info
= addr_list
= list_new ();
140 route_lock_node (rn
);
143 /* Tie address at the tail of address list. */
144 listnode_add (addr_list
, ifc
);
146 /* Return list element count. */
147 return (addr_list
->count
);
150 /* Untie an interface address from its derived subnet list of addresses. */
152 if_subnet_delete (struct interface
*ifp
, struct connected
*ifc
)
154 struct route_node
*rn
;
155 struct zebra_if
*zebra_if
;
156 struct list
*addr_list
;
158 assert (ifp
&& ifp
->info
&& ifc
);
159 zebra_if
= ifp
->info
;
161 /* Get address derived subnet node. */
162 rn
= route_node_lookup (zebra_if
->ipv4_subnets
, ifc
->address
);
163 if (! (rn
&& rn
->info
))
165 route_unlock_node (rn
);
167 /* Untie address from subnet's address list. */
168 addr_list
= rn
->info
;
169 listnode_delete (addr_list
, ifc
);
170 route_unlock_node (rn
);
172 /* Return list element count, if not empty. */
173 if (addr_list
->count
)
175 /* If deleted address is primary, mark subsequent one as such and distribute. */
176 if (! CHECK_FLAG (ifc
->flags
, ZEBRA_IFA_SECONDARY
))
178 ifc
= listgetdata (listhead (addr_list
));
179 zebra_interface_address_delete_update (ifp
, ifc
);
180 UNSET_FLAG (ifc
->flags
, ZEBRA_IFA_SECONDARY
);
181 zebra_interface_address_add_update (ifp
, ifc
);
184 return addr_list
->count
;
187 /* Otherwise, free list and route node. */
188 list_free (addr_list
);
190 route_unlock_node (rn
);
195 /* if_flags_mangle: A place for hacks that require mangling
196 * or tweaking the interface flags.
198 * ******************** Solaris flags hacks **************************
200 * Solaris IFF_UP flag reflects only the primary interface as the
201 * routing socket only sends IFINFO for the primary interface. Hence
202 * ~IFF_UP does not per se imply all the logical interfaces are also
203 * down - which we only know of as addresses. Instead we must determine
204 * whether the interface really is up or not according to how many
205 * addresses are still attached. (Solaris always sends RTM_DELADDR if
206 * an interface, logical or not, goes ~IFF_UP).
208 * Ie, we mangle IFF_UP to *additionally* reflect whether or not there
209 * are addresses left in struct connected, not just the actual underlying
212 * We must hence remember the real state of IFF_UP, which we do in
213 * struct zebra_if.primary_state.
215 * Setting IFF_UP within zebra to administratively shutdown the
216 * interface will affect only the primary interface/address on Solaris.
217 ************************End Solaris flags hacks ***********************
220 if_flags_mangle (struct interface
*ifp
, uint64_t *newflags
)
223 struct zebra_if
*zif
= ifp
->info
;
225 zif
->primary_state
= *newflags
& (IFF_UP
& 0xff);
227 if (CHECK_FLAG (zif
->primary_state
, IFF_UP
)
228 || listcount(ifp
->connected
) > 0)
229 SET_FLAG (*newflags
, IFF_UP
);
231 UNSET_FLAG (*newflags
, IFF_UP
);
235 /* Update the flags field of the ifp with the new flag set provided.
236 * Take whatever actions are required for any changes in flags we care
239 * newflags should be the raw value, as obtained from the OS.
242 if_flags_update (struct interface
*ifp
, uint64_t newflags
)
244 if_flags_mangle (ifp
, &newflags
);
246 if (if_is_operative (ifp
))
248 /* operative -> inoperative? */
249 ifp
->flags
= newflags
;
250 if (!if_is_operative (ifp
))
255 /* inoperative -> operative? */
256 ifp
->flags
= newflags
;
257 if (if_is_operative (ifp
))
262 /* Wake up configured address if it is not in current kernel
265 if_addr_wakeup (struct interface
*ifp
)
267 struct listnode
*node
, *nnode
;
268 struct connected
*ifc
;
272 for (ALL_LIST_ELEMENTS (ifp
->connected
, node
, nnode
, ifc
))
276 if (CHECK_FLAG (ifc
->conf
, ZEBRA_IFC_CONFIGURED
)
277 && ! CHECK_FLAG (ifc
->conf
, ZEBRA_IFC_REAL
))
280 if (p
->family
== AF_INET
)
282 if (! if_is_up (ifp
))
284 /* XXX: WTF is it trying to set flags here?
285 * caller has just gotten a new interface, has been
286 * handed the flags already. This code has no business
287 * trying to override administrative status of the interface.
288 * The only call path to here which doesn't originate from
289 * kernel event is irdp - what on earth is it trying to do?
291 * further RUNNING is not a settable flag on any system
292 * I (paulj) am aware of.
294 if_set_flags (ifp
, IFF_UP
| IFF_RUNNING
);
298 ret
= if_set_prefix (ifp
, ifc
);
301 zlog_warn ("Can't set interface's address: %s",
302 safe_strerror(errno
));
306 /* Add to subnet chain list. */
307 if_subnet_add (ifp
, ifc
);
309 SET_FLAG (ifc
->conf
, ZEBRA_IFC_REAL
);
311 zebra_interface_address_add_update (ifp
, ifc
);
313 if (if_is_operative(ifp
))
314 connected_up_ipv4 (ifp
, ifc
);
317 if (p
->family
== AF_INET6
)
319 if (! if_is_up (ifp
))
321 /* XXX: See long comment above */
322 if_set_flags (ifp
, IFF_UP
| IFF_RUNNING
);
326 ret
= if_prefix_add_ipv6 (ifp
, ifc
);
329 zlog_warn ("Can't set interface's address: %s",
330 safe_strerror(errno
));
333 SET_FLAG (ifc
->conf
, ZEBRA_IFC_REAL
);
335 zebra_interface_address_add_update (ifp
, ifc
);
337 if (if_is_operative(ifp
))
338 connected_up_ipv6 (ifp
, ifc
);
340 #endif /* HAVE_IPV6 */
345 /* Handle interface addition */
347 if_add_update (struct interface
*ifp
)
349 struct zebra_if
*if_data
;
352 if (if_data
->multicast
== IF_ZEBRA_MULTICAST_ON
)
353 if_set_flags (ifp
, IFF_MULTICAST
);
354 else if (if_data
->multicast
== IF_ZEBRA_MULTICAST_OFF
)
355 if_unset_flags (ifp
, IFF_MULTICAST
);
357 zebra_interface_add_update (ifp
);
359 if (! CHECK_FLAG (ifp
->status
, ZEBRA_INTERFACE_ACTIVE
))
361 SET_FLAG (ifp
->status
, ZEBRA_INTERFACE_ACTIVE
);
363 if_addr_wakeup (ifp
);
365 if (IS_ZEBRA_DEBUG_KERNEL
)
366 zlog_debug ("interface %s index %d becomes active.",
367 ifp
->name
, ifp
->ifindex
);
371 if (IS_ZEBRA_DEBUG_KERNEL
)
372 zlog_debug ("interface %s index %d is added.", ifp
->name
, ifp
->ifindex
);
376 /* Handle an interface delete event */
378 if_delete_update (struct interface
*ifp
)
380 struct connected
*ifc
;
382 struct route_node
*rn
;
383 struct zebra_if
*zebra_if
;
385 zebra_if
= ifp
->info
;
389 zlog_err ("interface %s index %d is still up while being deleted.",
390 ifp
->name
, ifp
->ifindex
);
394 /* Mark interface as inactive */
395 UNSET_FLAG (ifp
->status
, ZEBRA_INTERFACE_ACTIVE
);
397 if (IS_ZEBRA_DEBUG_KERNEL
)
398 zlog_debug ("interface %s index %d is now inactive.",
399 ifp
->name
, ifp
->ifindex
);
401 /* Delete connected routes from the kernel. */
404 struct listnode
*node
;
405 struct listnode
*last
= NULL
;
407 while ((node
= (last
? last
->next
: listhead (ifp
->connected
))))
409 ifc
= listgetdata (node
);
412 if (p
->family
== AF_INET
413 && (rn
= route_node_lookup (zebra_if
->ipv4_subnets
, p
)))
415 struct listnode
*anode
;
416 struct listnode
*next
;
417 struct listnode
*first
;
418 struct list
*addr_list
;
420 route_unlock_node (rn
);
421 addr_list
= (struct list
*) rn
->info
;
423 /* Remove addresses, secondaries first. */
424 first
= listhead (addr_list
);
425 for (anode
= first
->next
; anode
|| first
; anode
= next
)
434 ifc
= listgetdata (anode
);
437 connected_down_ipv4 (ifp
, ifc
);
439 zebra_interface_address_delete_update (ifp
, ifc
);
441 UNSET_FLAG (ifc
->conf
, ZEBRA_IFC_REAL
);
443 /* Remove from subnet chain. */
444 list_delete_node (addr_list
, anode
);
445 route_unlock_node (rn
);
447 /* Remove from interface address list (unconditionally). */
448 if (!CHECK_FLAG (ifc
->conf
, ZEBRA_IFC_CONFIGURED
))
450 listnode_delete (ifp
->connected
, ifc
);
451 connected_free (ifc
);
457 /* Free chain list and respective route node. */
458 list_delete (addr_list
);
460 route_unlock_node (rn
);
463 else if (p
->family
== AF_INET6
)
465 connected_down_ipv6 (ifp
, ifc
);
467 zebra_interface_address_delete_update (ifp
, ifc
);
469 UNSET_FLAG (ifc
->conf
, ZEBRA_IFC_REAL
);
471 if (CHECK_FLAG (ifc
->conf
, ZEBRA_IFC_CONFIGURED
))
475 listnode_delete (ifp
->connected
, ifc
);
476 connected_free (ifc
);
479 #endif /* HAVE_IPV6 */
482 zebra_interface_delete_update (ifp
);
484 /* Update ifindex after distributing the delete message. This is in
485 case any client needs to have the old value of ifindex available
486 while processing the deletion. Each client daemon is responsible
487 for setting ifindex to IFINDEX_INTERNAL after processing the
488 interface deletion message. */
489 ifp
->ifindex
= IFINDEX_INTERNAL
;
492 /* Interface is up. */
494 if_up (struct interface
*ifp
)
496 struct listnode
*node
;
497 struct listnode
*next
;
498 struct connected
*ifc
;
501 /* Notify the protocol daemons. */
502 zebra_interface_up_update (ifp
);
504 /* Install connected routes to the kernel. */
507 for (ALL_LIST_ELEMENTS (ifp
->connected
, node
, next
, ifc
))
511 if (p
->family
== AF_INET
)
512 connected_up_ipv4 (ifp
, ifc
);
514 else if (p
->family
== AF_INET6
)
515 connected_up_ipv6 (ifp
, ifc
);
516 #endif /* HAVE_IPV6 */
520 /* Examine all static routes. */
524 /* Interface goes down. We have to manage different behavior of based
527 if_down (struct interface
*ifp
)
529 struct listnode
*node
;
530 struct listnode
*next
;
531 struct connected
*ifc
;
534 /* Notify to the protocol daemons. */
535 zebra_interface_down_update (ifp
);
537 /* Delete connected routes from the kernel. */
540 for (ALL_LIST_ELEMENTS (ifp
->connected
, node
, next
, ifc
))
544 if (p
->family
== AF_INET
)
545 connected_down_ipv4 (ifp
, ifc
);
547 else if (p
->family
== AF_INET6
)
548 connected_down_ipv6 (ifp
, ifc
);
549 #endif /* HAVE_IPV6 */
553 /* Examine all static routes which direct to the interface. */
558 if_refresh (struct interface
*ifp
)
563 /* Output prefix string to vty. */
565 prefix_vty_out (struct vty
*vty
, struct prefix
*p
)
567 char str
[INET6_ADDRSTRLEN
];
569 inet_ntop (p
->family
, &p
->u
.prefix
, str
, sizeof (str
));
570 vty_out (vty
, "%s", str
);
574 /* Dump if address information to vty. */
576 connected_dump_vty (struct vty
*vty
, struct connected
*connected
)
580 /* Print interface address. */
581 p
= connected
->address
;
582 vty_out (vty
, " %s ", prefix_family_str (p
));
583 prefix_vty_out (vty
, p
);
584 vty_out (vty
, "/%d", p
->prefixlen
);
586 /* If there is destination address, print it. */
587 if (connected
->destination
)
589 vty_out (vty
, (CONNECTED_PEER(connected
) ? " peer " : " broadcast "));
590 prefix_vty_out (vty
, connected
->destination
);
593 if (CHECK_FLAG (connected
->flags
, ZEBRA_IFA_SECONDARY
))
594 vty_out (vty
, " secondary");
596 if (connected
->label
)
597 vty_out (vty
, " %s", connected
->label
);
599 vty_out (vty
, "%s", VTY_NEWLINE
);
603 /* Dump interface ND information to vty. */
605 nd_dump_vty (struct vty
*vty
, struct interface
*ifp
)
607 struct zebra_if
*zif
;
608 struct rtadvconf
*rtadv
;
611 zif
= (struct zebra_if
*) ifp
->info
;
614 if (rtadv
->AdvSendAdvertisements
)
616 vty_out (vty
, " ND advertised reachable time is %d milliseconds%s",
617 rtadv
->AdvReachableTime
, VTY_NEWLINE
);
618 vty_out (vty
, " ND advertised retransmit interval is %d milliseconds%s",
619 rtadv
->AdvRetransTimer
, VTY_NEWLINE
);
620 interval
= rtadv
->MaxRtrAdvInterval
;
622 vty_out (vty
, " ND router advertisements are sent every "
623 "%d milliseconds%s", interval
,
626 vty_out (vty
, " ND router advertisements are sent every "
627 "%d seconds%s", interval
/ 1000,
629 vty_out (vty
, " ND router advertisements live for %d seconds%s",
630 rtadv
->AdvDefaultLifetime
, VTY_NEWLINE
);
631 vty_out (vty
, " ND router advertisement default router preference is "
632 "%s%s", rtadv_pref_strs
[rtadv
->DefaultPreference
],
634 if (rtadv
->AdvManagedFlag
)
635 vty_out (vty
, " Hosts use DHCP to obtain routable addresses.%s",
638 vty_out (vty
, " Hosts use stateless autoconfig for addresses.%s",
640 if (rtadv
->AdvHomeAgentFlag
)
641 vty_out (vty
, " ND router advertisements with "
642 "Home Agent flag bit set.%s",
644 if (rtadv
->AdvIntervalOption
)
645 vty_out (vty
, " ND router advertisements with Adv. Interval option.%s",
651 /* Interface's information print out to vty interface. */
653 if_dump_vty (struct vty
*vty
, struct interface
*ifp
)
655 #ifdef HAVE_STRUCT_SOCKADDR_DL
656 struct sockaddr_dl
*sdl
;
657 #endif /* HAVE_STRUCT_SOCKADDR_DL */
658 struct connected
*connected
;
659 struct listnode
*node
;
660 struct route_node
*rn
;
661 struct zebra_if
*zebra_if
;
663 zebra_if
= ifp
->info
;
665 vty_out (vty
, "Interface %s is ", ifp
->name
);
667 vty_out (vty
, "up, line protocol ");
669 if (CHECK_FLAG(ifp
->status
, ZEBRA_INTERFACE_LINKDETECTION
)) {
670 if (if_is_running(ifp
))
671 vty_out (vty
, "is up%s", VTY_NEWLINE
);
673 vty_out (vty
, "is down%s", VTY_NEWLINE
);
675 vty_out (vty
, "detection is disabled%s", VTY_NEWLINE
);
678 vty_out (vty
, "down%s", VTY_NEWLINE
);
682 vty_out (vty
, " Description: %s%s", ifp
->desc
,
684 if (ifp
->ifindex
== IFINDEX_INTERNAL
)
686 vty_out(vty
, " pseudo interface%s", VTY_NEWLINE
);
689 else if (! CHECK_FLAG (ifp
->status
, ZEBRA_INTERFACE_ACTIVE
))
691 vty_out(vty
, " index %d inactive interface%s",
697 vty_out (vty
, " index %d metric %d mtu %d ",
698 ifp
->ifindex
, ifp
->metric
, ifp
->mtu
);
700 if (ifp
->mtu6
!= ifp
->mtu
)
701 vty_out (vty
, "mtu6 %d ", ifp
->mtu6
);
703 vty_out (vty
, "%s flags: %s%s", VTY_NEWLINE
,
704 if_flag_dump (ifp
->flags
), VTY_NEWLINE
);
706 /* Hardware address. */
707 #ifdef HAVE_STRUCT_SOCKADDR_DL
709 if (sdl
!= NULL
&& sdl
->sdl_alen
!= 0)
714 vty_out (vty
, " HWaddr: ");
715 for (i
= 0, ptr
= (u_char
*)LLADDR (sdl
); i
< sdl
->sdl_alen
; i
++, ptr
++)
716 vty_out (vty
, "%s%02x", i
== 0 ? "" : ":", *ptr
);
717 vty_out (vty
, "%s", VTY_NEWLINE
);
720 if (ifp
->hw_addr_len
!= 0)
724 vty_out (vty
, " HWaddr: ");
725 for (i
= 0; i
< ifp
->hw_addr_len
; i
++)
726 vty_out (vty
, "%s%02x", i
== 0 ? "" : ":", ifp
->hw_addr
[i
]);
727 vty_out (vty
, "%s", VTY_NEWLINE
);
729 #endif /* HAVE_STRUCT_SOCKADDR_DL */
731 /* Bandwidth in kbps */
732 if (ifp
->bandwidth
!= 0)
734 vty_out(vty
, " bandwidth %u kbps", ifp
->bandwidth
);
735 vty_out(vty
, "%s", VTY_NEWLINE
);
738 for (rn
= route_top (zebra_if
->ipv4_subnets
); rn
; rn
= route_next (rn
))
743 for (ALL_LIST_ELEMENTS_RO ((struct list
*)rn
->info
, node
, connected
))
744 connected_dump_vty (vty
, connected
);
747 for (ALL_LIST_ELEMENTS_RO (ifp
->connected
, node
, connected
))
749 if (CHECK_FLAG (connected
->conf
, ZEBRA_IFC_REAL
) &&
750 (connected
->address
->family
== AF_INET6
))
751 connected_dump_vty (vty
, connected
);
755 nd_dump_vty (vty
, ifp
);
758 #ifdef HAVE_PROC_NET_DEV
759 /* Statistics print out using proc file system. */
760 vty_out (vty
, " %lu input packets (%lu multicast), %lu bytes, "
762 ifp
->stats
.rx_packets
, ifp
->stats
.rx_multicast
,
763 ifp
->stats
.rx_bytes
, ifp
->stats
.rx_dropped
, VTY_NEWLINE
);
765 vty_out (vty
, " %lu input errors, %lu length, %lu overrun,"
766 " %lu CRC, %lu frame%s",
767 ifp
->stats
.rx_errors
, ifp
->stats
.rx_length_errors
,
768 ifp
->stats
.rx_over_errors
, ifp
->stats
.rx_crc_errors
,
769 ifp
->stats
.rx_frame_errors
, VTY_NEWLINE
);
771 vty_out (vty
, " %lu fifo, %lu missed%s", ifp
->stats
.rx_fifo_errors
,
772 ifp
->stats
.rx_missed_errors
, VTY_NEWLINE
);
774 vty_out (vty
, " %lu output packets, %lu bytes, %lu dropped%s",
775 ifp
->stats
.tx_packets
, ifp
->stats
.tx_bytes
,
776 ifp
->stats
.tx_dropped
, VTY_NEWLINE
);
778 vty_out (vty
, " %lu output errors, %lu aborted, %lu carrier,"
779 " %lu fifo, %lu heartbeat%s",
780 ifp
->stats
.tx_errors
, ifp
->stats
.tx_aborted_errors
,
781 ifp
->stats
.tx_carrier_errors
, ifp
->stats
.tx_fifo_errors
,
782 ifp
->stats
.tx_heartbeat_errors
, VTY_NEWLINE
);
784 vty_out (vty
, " %lu window, %lu collisions%s",
785 ifp
->stats
.tx_window_errors
, ifp
->stats
.collisions
, VTY_NEWLINE
);
786 #endif /* HAVE_PROC_NET_DEV */
788 #ifdef HAVE_NET_RT_IFLIST
789 #if defined (__bsdi__) || defined (__NetBSD__)
790 /* Statistics print out using sysctl (). */
791 vty_out (vty
, " input packets %qu, bytes %qu, dropped %qu,"
792 " multicast packets %qu%s",
793 ifp
->stats
.ifi_ipackets
, ifp
->stats
.ifi_ibytes
,
794 ifp
->stats
.ifi_iqdrops
, ifp
->stats
.ifi_imcasts
,
797 vty_out (vty
, " input errors %qu%s",
798 ifp
->stats
.ifi_ierrors
, VTY_NEWLINE
);
800 vty_out (vty
, " output packets %qu, bytes %qu, multicast packets %qu%s",
801 ifp
->stats
.ifi_opackets
, ifp
->stats
.ifi_obytes
,
802 ifp
->stats
.ifi_omcasts
, VTY_NEWLINE
);
804 vty_out (vty
, " output errors %qu%s",
805 ifp
->stats
.ifi_oerrors
, VTY_NEWLINE
);
807 vty_out (vty
, " collisions %qu%s",
808 ifp
->stats
.ifi_collisions
, VTY_NEWLINE
);
810 /* Statistics print out using sysctl (). */
811 vty_out (vty
, " input packets %lu, bytes %lu, dropped %lu,"
812 " multicast packets %lu%s",
813 ifp
->stats
.ifi_ipackets
, ifp
->stats
.ifi_ibytes
,
814 ifp
->stats
.ifi_iqdrops
, ifp
->stats
.ifi_imcasts
,
817 vty_out (vty
, " input errors %lu%s",
818 ifp
->stats
.ifi_ierrors
, VTY_NEWLINE
);
820 vty_out (vty
, " output packets %lu, bytes %lu, multicast packets %lu%s",
821 ifp
->stats
.ifi_opackets
, ifp
->stats
.ifi_obytes
,
822 ifp
->stats
.ifi_omcasts
, VTY_NEWLINE
);
824 vty_out (vty
, " output errors %lu%s",
825 ifp
->stats
.ifi_oerrors
, VTY_NEWLINE
);
827 vty_out (vty
, " collisions %lu%s",
828 ifp
->stats
.ifi_collisions
, VTY_NEWLINE
);
829 #endif /* __bsdi__ || __NetBSD__ */
830 #endif /* HAVE_NET_RT_IFLIST */
833 /* Wrapper hook point for zebra daemon so that ifindex can be set
834 * DEFUN macro not used as extract.pl HAS to ignore this
835 * See also interface_cmd in lib/if.c
837 DEFUN_NOSH (zebra_interface
,
840 "Select an interface to configure\n"
841 "Interface's name\n")
844 struct interface
* ifp
;
846 /* Call lib interface() */
847 if ((ret
= interface_cmd
.func (self
, vty
, argc
, argv
)) != CMD_SUCCESS
)
852 if (ifp
->ifindex
== IFINDEX_INTERNAL
)
853 /* Is this really necessary? Shouldn't status be initialized to 0
855 UNSET_FLAG (ifp
->status
, ZEBRA_INTERFACE_ACTIVE
);
860 struct cmd_node interface_node
=
867 /* Show all or specified interface to vty. */
868 DEFUN (show_interface
, show_interface_cmd
,
869 "show interface [IFNAME]",
871 "Interface status and configuration\n"
874 struct listnode
*node
;
875 struct interface
*ifp
;
877 #ifdef HAVE_PROC_NET_DEV
878 /* If system has interface statistics via proc file system, update
880 ifstat_update_proc ();
881 #endif /* HAVE_PROC_NET_DEV */
882 #ifdef HAVE_NET_RT_IFLIST
883 ifstat_update_sysctl ();
884 #endif /* HAVE_NET_RT_IFLIST */
886 /* Specified interface print. */
889 ifp
= if_lookup_by_name (argv
[0]);
892 vty_out (vty
, "%% Can't find interface %s%s", argv
[0],
896 if_dump_vty (vty
, ifp
);
900 /* All interface print. */
901 for (ALL_LIST_ELEMENTS_RO (iflist
, node
, ifp
))
902 if_dump_vty (vty
, ifp
);
907 DEFUN (show_interface_desc
,
908 show_interface_desc_cmd
,
909 "show interface description",
911 "Interface status and configuration\n"
912 "Interface description\n")
914 struct listnode
*node
;
915 struct interface
*ifp
;
917 vty_out (vty
, "Interface Status Protocol Description%s", VTY_NEWLINE
);
918 for (ALL_LIST_ELEMENTS_RO (iflist
, node
, ifp
))
922 len
= vty_out (vty
, "%s", ifp
->name
);
923 vty_out (vty
, "%*s", (16 - len
), " ");
927 vty_out (vty
, "up ");
928 if (CHECK_FLAG(ifp
->status
, ZEBRA_INTERFACE_LINKDETECTION
))
930 if (if_is_running(ifp
))
931 vty_out (vty
, "up ");
933 vty_out (vty
, "down ");
937 vty_out (vty
, "unknown ");
942 vty_out (vty
, "down down ");
946 vty_out (vty
, "%s", ifp
->desc
);
947 vty_out (vty
, "%s", VTY_NEWLINE
);
955 "Set multicast flag to interface\n")
958 struct interface
*ifp
;
959 struct zebra_if
*if_data
;
961 ifp
= (struct interface
*) vty
->index
;
962 if (CHECK_FLAG (ifp
->status
, ZEBRA_INTERFACE_ACTIVE
))
964 ret
= if_set_flags (ifp
, IFF_MULTICAST
);
967 vty_out (vty
, "Can't set multicast flag%s", VTY_NEWLINE
);
973 if_data
->multicast
= IF_ZEBRA_MULTICAST_ON
;
982 "Unset multicast flag to interface\n")
985 struct interface
*ifp
;
986 struct zebra_if
*if_data
;
988 ifp
= (struct interface
*) vty
->index
;
989 if (CHECK_FLAG (ifp
->status
, ZEBRA_INTERFACE_ACTIVE
))
991 ret
= if_unset_flags (ifp
, IFF_MULTICAST
);
994 vty_out (vty
, "Can't unset multicast flag%s", VTY_NEWLINE
);
1000 if_data
->multicast
= IF_ZEBRA_MULTICAST_OFF
;
1008 "Enable link detection on interface\n")
1010 struct interface
*ifp
;
1011 int if_was_operative
;
1013 ifp
= (struct interface
*) vty
->index
;
1014 if_was_operative
= if_is_operative(ifp
);
1015 SET_FLAG(ifp
->status
, ZEBRA_INTERFACE_LINKDETECTION
);
1017 /* When linkdetection is enabled, if might come down */
1018 if (!if_is_operative(ifp
) && if_was_operative
) if_down(ifp
);
1020 /* FIXME: Will defer status change forwarding if interface
1021 does not come down! */
1027 DEFUN (no_linkdetect
,
1031 "Disable link detection on interface\n")
1033 struct interface
*ifp
;
1034 int if_was_operative
;
1036 ifp
= (struct interface
*) vty
->index
;
1037 if_was_operative
= if_is_operative(ifp
);
1038 UNSET_FLAG(ifp
->status
, ZEBRA_INTERFACE_LINKDETECTION
);
1040 /* Interface may come up after disabling link detection */
1041 if (if_is_operative(ifp
) && !if_was_operative
) if_up(ifp
);
1043 /* FIXME: see linkdetect_cmd */
1051 "Shutdown the selected interface\n")
1054 struct interface
*ifp
;
1055 struct zebra_if
*if_data
;
1057 ifp
= (struct interface
*) vty
->index
;
1058 ret
= if_unset_flags (ifp
, IFF_UP
);
1061 vty_out (vty
, "Can't shutdown interface%s", VTY_NEWLINE
);
1065 if_data
= ifp
->info
;
1066 if_data
->shutdown
= IF_ZEBRA_SHUTDOWN_ON
;
1071 DEFUN (no_shutdown_if
,
1075 "Shutdown the selected interface\n")
1078 struct interface
*ifp
;
1079 struct zebra_if
*if_data
;
1081 ifp
= (struct interface
*) vty
->index
;
1082 ret
= if_set_flags (ifp
, IFF_UP
| IFF_RUNNING
);
1085 vty_out (vty
, "Can't up interface%s", VTY_NEWLINE
);
1089 if_data
= ifp
->info
;
1090 if_data
->shutdown
= IF_ZEBRA_SHUTDOWN_OFF
;
1095 DEFUN (bandwidth_if
,
1097 "bandwidth <1-10000000>",
1098 "Set bandwidth informational parameter\n"
1099 "Bandwidth in kilobits\n")
1101 struct interface
*ifp
;
1102 unsigned int bandwidth
;
1104 ifp
= (struct interface
*) vty
->index
;
1105 bandwidth
= strtol(argv
[0], NULL
, 10);
1107 /* bandwidth range is <1-10000000> */
1108 if (bandwidth
< 1 || bandwidth
> 10000000)
1110 vty_out (vty
, "Bandwidth is invalid%s", VTY_NEWLINE
);
1114 ifp
->bandwidth
= bandwidth
;
1116 /* force protocols to recalculate routes due to cost change */
1117 if (if_is_operative (ifp
))
1118 zebra_interface_up_update (ifp
);
1123 DEFUN (no_bandwidth_if
,
1124 no_bandwidth_if_cmd
,
1127 "Set bandwidth informational parameter\n")
1129 struct interface
*ifp
;
1131 ifp
= (struct interface
*) vty
->index
;
1135 /* force protocols to recalculate routes due to cost change */
1136 if (if_is_operative (ifp
))
1137 zebra_interface_up_update (ifp
);
1142 ALIAS (no_bandwidth_if
,
1143 no_bandwidth_if_val_cmd
,
1144 "no bandwidth <1-10000000>",
1146 "Set bandwidth informational parameter\n"
1147 "Bandwidth in kilobits\n")
1150 ip_address_install (struct vty
*vty
, struct interface
*ifp
,
1151 const char *addr_str
, const char *peer_str
,
1154 struct prefix_ipv4 cp
;
1155 struct connected
*ifc
;
1156 struct prefix_ipv4
*p
;
1159 ret
= str2prefix_ipv4 (addr_str
, &cp
);
1162 vty_out (vty
, "%% Malformed address %s", VTY_NEWLINE
);
1166 ifc
= connected_check (ifp
, (struct prefix
*) &cp
);
1169 ifc
= connected_new ();
1173 p
= prefix_ipv4_new ();
1175 ifc
->address
= (struct prefix
*) p
;
1178 if (p
->prefixlen
<= IPV4_MAX_PREFIXLEN
-2)
1180 p
= prefix_ipv4_new ();
1182 p
->prefix
.s_addr
= ipv4_broadcast_addr(p
->prefix
.s_addr
,p
->prefixlen
);
1183 ifc
->destination
= (struct prefix
*) p
;
1188 ifc
->label
= XSTRDUP (MTYPE_CONNECTED_LABEL
, label
);
1190 /* Add to linked list. */
1191 listnode_add (ifp
->connected
, ifc
);
1194 /* This address is configured from zebra. */
1195 if (! CHECK_FLAG (ifc
->conf
, ZEBRA_IFC_CONFIGURED
))
1196 SET_FLAG (ifc
->conf
, ZEBRA_IFC_CONFIGURED
);
1198 /* In case of this route need to install kernel. */
1199 if (! CHECK_FLAG (ifc
->conf
, ZEBRA_IFC_REAL
)
1200 && CHECK_FLAG (ifp
->status
, ZEBRA_INTERFACE_ACTIVE
))
1202 /* Some system need to up the interface to set IP address. */
1203 if (! if_is_up (ifp
))
1205 if_set_flags (ifp
, IFF_UP
| IFF_RUNNING
);
1209 ret
= if_set_prefix (ifp
, ifc
);
1212 vty_out (vty
, "%% Can't set interface IP address: %s.%s",
1213 safe_strerror(errno
), VTY_NEWLINE
);
1217 /* Add to subnet chain list (while marking secondary attribute). */
1218 if_subnet_add (ifp
, ifc
);
1220 /* IP address propery set. */
1221 SET_FLAG (ifc
->conf
, ZEBRA_IFC_REAL
);
1223 /* Update interface address information to protocol daemon. */
1224 zebra_interface_address_add_update (ifp
, ifc
);
1226 /* If interface is up register connected route. */
1227 if (if_is_operative(ifp
))
1228 connected_up_ipv4 (ifp
, ifc
);
1235 ip_address_uninstall (struct vty
*vty
, struct interface
*ifp
,
1236 const char *addr_str
, const char *peer_str
,
1239 struct prefix_ipv4 cp
;
1240 struct connected
*ifc
;
1243 /* Convert to prefix structure. */
1244 ret
= str2prefix_ipv4 (addr_str
, &cp
);
1247 vty_out (vty
, "%% Malformed address %s", VTY_NEWLINE
);
1251 /* Check current interface address. */
1252 ifc
= connected_check (ifp
, (struct prefix
*) &cp
);
1255 vty_out (vty
, "%% Can't find address%s", VTY_NEWLINE
);
1259 /* This is not configured address. */
1260 if (! CHECK_FLAG (ifc
->conf
, ZEBRA_IFC_CONFIGURED
))
1263 UNSET_FLAG (ifc
->conf
, ZEBRA_IFC_CONFIGURED
);
1265 /* This is not real address or interface is not active. */
1266 if (! CHECK_FLAG (ifc
->conf
, ZEBRA_IFC_REAL
)
1267 || ! CHECK_FLAG (ifp
->status
, ZEBRA_INTERFACE_ACTIVE
))
1269 listnode_delete (ifp
->connected
, ifc
);
1270 connected_free (ifc
);
1274 /* This is real route. */
1275 ret
= if_unset_prefix (ifp
, ifc
);
1278 vty_out (vty
, "%% Can't unset interface IP address: %s.%s",
1279 safe_strerror(errno
), VTY_NEWLINE
);
1284 /* Redistribute this information. */
1285 zebra_interface_address_delete_update (ifp
, ifc
);
1287 /* Remove connected route. */
1288 connected_down_ipv4 (ifp
, ifc
);
1290 /* Free address information. */
1291 listnode_delete (ifp
->connected
, ifc
);
1292 connected_free (ifc
);
1300 "ip address A.B.C.D/M",
1301 "Interface Internet Protocol config commands\n"
1302 "Set the IP address of an interface\n"
1303 "IP address (e.g. 10.0.0.1/8)\n")
1305 return ip_address_install (vty
, vty
->index
, argv
[0], NULL
, NULL
);
1308 DEFUN (no_ip_address
,
1310 "no ip address A.B.C.D/M",
1312 "Interface Internet Protocol config commands\n"
1313 "Set the IP address of an interface\n"
1314 "IP Address (e.g. 10.0.0.1/8)")
1316 return ip_address_uninstall (vty
, vty
->index
, argv
[0], NULL
, NULL
);
1320 DEFUN (ip_address_label
,
1321 ip_address_label_cmd
,
1322 "ip address A.B.C.D/M label LINE",
1323 "Interface Internet Protocol config commands\n"
1324 "Set the IP address of an interface\n"
1325 "IP address (e.g. 10.0.0.1/8)\n"
1326 "Label of this address\n"
1329 return ip_address_install (vty
, vty
->index
, argv
[0], NULL
, argv
[1]);
1332 DEFUN (no_ip_address_label
,
1333 no_ip_address_label_cmd
,
1334 "no ip address A.B.C.D/M label LINE",
1336 "Interface Internet Protocol config commands\n"
1337 "Set the IP address of an interface\n"
1338 "IP address (e.g. 10.0.0.1/8)\n"
1339 "Label of this address\n"
1342 return ip_address_uninstall (vty
, vty
->index
, argv
[0], NULL
, argv
[1]);
1344 #endif /* HAVE_NETLINK */
1348 ipv6_address_install (struct vty
*vty
, struct interface
*ifp
,
1349 const char *addr_str
, const char *peer_str
,
1350 const char *label
, int secondary
)
1352 struct prefix_ipv6 cp
;
1353 struct connected
*ifc
;
1354 struct prefix_ipv6
*p
;
1357 ret
= str2prefix_ipv6 (addr_str
, &cp
);
1360 vty_out (vty
, "%% Malformed address %s", VTY_NEWLINE
);
1364 ifc
= connected_check (ifp
, (struct prefix
*) &cp
);
1367 ifc
= connected_new ();
1371 p
= prefix_ipv6_new ();
1373 ifc
->address
= (struct prefix
*) p
;
1377 SET_FLAG (ifc
->flags
, ZEBRA_IFA_SECONDARY
);
1381 ifc
->label
= XSTRDUP (MTYPE_CONNECTED_LABEL
, label
);
1383 /* Add to linked list. */
1384 listnode_add (ifp
->connected
, ifc
);
1387 /* This address is configured from zebra. */
1388 if (! CHECK_FLAG (ifc
->conf
, ZEBRA_IFC_CONFIGURED
))
1389 SET_FLAG (ifc
->conf
, ZEBRA_IFC_CONFIGURED
);
1391 /* In case of this route need to install kernel. */
1392 if (! CHECK_FLAG (ifc
->conf
, ZEBRA_IFC_REAL
)
1393 && CHECK_FLAG (ifp
->status
, ZEBRA_INTERFACE_ACTIVE
))
1395 /* Some system need to up the interface to set IP address. */
1396 if (! if_is_up (ifp
))
1398 if_set_flags (ifp
, IFF_UP
| IFF_RUNNING
);
1402 ret
= if_prefix_add_ipv6 (ifp
, ifc
);
1406 vty_out (vty
, "%% Can't set interface IP address: %s.%s",
1407 safe_strerror(errno
), VTY_NEWLINE
);
1411 /* IP address propery set. */
1412 SET_FLAG (ifc
->conf
, ZEBRA_IFC_REAL
);
1414 /* Update interface address information to protocol daemon. */
1415 zebra_interface_address_add_update (ifp
, ifc
);
1417 /* If interface is up register connected route. */
1418 if (if_is_operative(ifp
))
1419 connected_up_ipv6 (ifp
, ifc
);
1426 ipv6_address_uninstall (struct vty
*vty
, struct interface
*ifp
,
1427 const char *addr_str
, const char *peer_str
,
1428 const char *label
, int secondry
)
1430 struct prefix_ipv6 cp
;
1431 struct connected
*ifc
;
1434 /* Convert to prefix structure. */
1435 ret
= str2prefix_ipv6 (addr_str
, &cp
);
1438 vty_out (vty
, "%% Malformed address %s", VTY_NEWLINE
);
1442 /* Check current interface address. */
1443 ifc
= connected_check (ifp
, (struct prefix
*) &cp
);
1446 vty_out (vty
, "%% Can't find address%s", VTY_NEWLINE
);
1450 /* This is not configured address. */
1451 if (! CHECK_FLAG (ifc
->conf
, ZEBRA_IFC_CONFIGURED
))
1454 /* This is not real address or interface is not active. */
1455 if (! CHECK_FLAG (ifc
->conf
, ZEBRA_IFC_REAL
)
1456 || ! CHECK_FLAG (ifp
->status
, ZEBRA_INTERFACE_ACTIVE
))
1458 listnode_delete (ifp
->connected
, ifc
);
1459 connected_free (ifc
);
1463 /* This is real route. */
1464 ret
= if_prefix_delete_ipv6 (ifp
, ifc
);
1467 vty_out (vty
, "%% Can't unset interface IP address: %s.%s",
1468 safe_strerror(errno
), VTY_NEWLINE
);
1472 /* Redistribute this information. */
1473 zebra_interface_address_delete_update (ifp
, ifc
);
1475 /* Remove connected route. */
1476 connected_down_ipv6 (ifp
, ifc
);
1478 /* Free address information. */
1479 listnode_delete (ifp
->connected
, ifc
);
1480 connected_free (ifc
);
1485 DEFUN (ipv6_address
,
1487 "ipv6 address X:X::X:X/M",
1488 "Interface IPv6 config commands\n"
1489 "Set the IP address of an interface\n"
1490 "IPv6 address (e.g. 3ffe:506::1/48)\n")
1492 return ipv6_address_install (vty
, vty
->index
, argv
[0], NULL
, NULL
, 0);
1495 DEFUN (no_ipv6_address
,
1496 no_ipv6_address_cmd
,
1497 "no ipv6 address X:X::X:X/M",
1499 "Interface IPv6 config commands\n"
1500 "Set the IP address of an interface\n"
1501 "IPv6 address (e.g. 3ffe:506::1/48)\n")
1503 return ipv6_address_uninstall (vty
, vty
->index
, argv
[0], NULL
, NULL
, 0);
1505 #endif /* HAVE_IPV6 */
1508 if_config_write (struct vty
*vty
)
1510 struct listnode
*node
;
1511 struct interface
*ifp
;
1513 for (ALL_LIST_ELEMENTS_RO (iflist
, node
, ifp
))
1515 struct zebra_if
*if_data
;
1516 struct listnode
*addrnode
;
1517 struct connected
*ifc
;
1520 if_data
= ifp
->info
;
1522 vty_out (vty
, "interface %s%s", ifp
->name
,
1526 vty_out (vty
, " description %s%s", ifp
->desc
,
1529 /* Assign bandwidth here to avoid unnecessary interface flap
1530 while processing config script */
1531 if (ifp
->bandwidth
!= 0)
1532 vty_out(vty
, " bandwidth %u%s", ifp
->bandwidth
, VTY_NEWLINE
);
1534 if (CHECK_FLAG(ifp
->status
, ZEBRA_INTERFACE_LINKDETECTION
))
1535 vty_out(vty
, " link-detect%s", VTY_NEWLINE
);
1537 for (ALL_LIST_ELEMENTS_RO (ifp
->connected
, addrnode
, ifc
))
1539 if (CHECK_FLAG (ifc
->conf
, ZEBRA_IFC_CONFIGURED
))
1541 char buf
[INET6_ADDRSTRLEN
];
1543 vty_out (vty
, " ip%s address %s/%d",
1544 p
->family
== AF_INET
? "" : "v6",
1545 inet_ntop (p
->family
, &p
->u
.prefix
, buf
, sizeof(buf
)),
1549 vty_out (vty
, " label %s", ifc
->label
);
1551 vty_out (vty
, "%s", VTY_NEWLINE
);
1557 if (if_data
->shutdown
== IF_ZEBRA_SHUTDOWN_ON
)
1558 vty_out (vty
, " shutdown%s", VTY_NEWLINE
);
1560 if (if_data
->multicast
!= IF_ZEBRA_MULTICAST_UNSPEC
)
1561 vty_out (vty
, " %smulticast%s",
1562 if_data
->multicast
== IF_ZEBRA_MULTICAST_ON
? "" : "no ",
1567 rtadv_config_write (vty
, ifp
);
1571 irdp_config_write (vty
, ifp
);
1574 vty_out (vty
, "!%s", VTY_NEWLINE
);
1579 /* Allocate and initialize interface vector. */
1581 zebra_if_init (void)
1583 /* Initialize interface and new hook. */
1585 if_add_hook (IF_NEW_HOOK
, if_zebra_new_hook
);
1586 if_add_hook (IF_DELETE_HOOK
, if_zebra_delete_hook
);
1588 /* Install configuration write function. */
1589 install_node (&interface_node
, if_config_write
);
1591 install_element (VIEW_NODE
, &show_interface_cmd
);
1592 install_element (ENABLE_NODE
, &show_interface_cmd
);
1593 install_element (ENABLE_NODE
, &show_interface_desc_cmd
);
1594 install_element (CONFIG_NODE
, &zebra_interface_cmd
);
1595 install_element (CONFIG_NODE
, &no_interface_cmd
);
1596 install_default (INTERFACE_NODE
);
1597 install_element (INTERFACE_NODE
, &interface_desc_cmd
);
1598 install_element (INTERFACE_NODE
, &no_interface_desc_cmd
);
1599 install_element (INTERFACE_NODE
, &multicast_cmd
);
1600 install_element (INTERFACE_NODE
, &no_multicast_cmd
);
1601 install_element (INTERFACE_NODE
, &linkdetect_cmd
);
1602 install_element (INTERFACE_NODE
, &no_linkdetect_cmd
);
1603 install_element (INTERFACE_NODE
, &shutdown_if_cmd
);
1604 install_element (INTERFACE_NODE
, &no_shutdown_if_cmd
);
1605 install_element (INTERFACE_NODE
, &bandwidth_if_cmd
);
1606 install_element (INTERFACE_NODE
, &no_bandwidth_if_cmd
);
1607 install_element (INTERFACE_NODE
, &no_bandwidth_if_val_cmd
);
1608 install_element (INTERFACE_NODE
, &ip_address_cmd
);
1609 install_element (INTERFACE_NODE
, &no_ip_address_cmd
);
1611 install_element (INTERFACE_NODE
, &ipv6_address_cmd
);
1612 install_element (INTERFACE_NODE
, &no_ipv6_address_cmd
);
1613 #endif /* HAVE_IPV6 */
1615 install_element (INTERFACE_NODE
, &ip_address_label_cmd
);
1616 install_element (INTERFACE_NODE
, &no_ip_address_label_cmd
);
1617 #endif /* HAVE_NETLINK */