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 /* Called when new interface is added. */
47 if_zebra_new_hook (struct interface
*ifp
)
49 struct zebra_if
*zebra_if
;
51 zebra_if
= XMALLOC (MTYPE_TMP
, sizeof (struct zebra_if
));
52 memset (zebra_if
, 0, sizeof (struct zebra_if
));
54 zebra_if
->multicast
= IF_ZEBRA_MULTICAST_UNSPEC
;
55 zebra_if
->shutdown
= IF_ZEBRA_SHUTDOWN_UNSPEC
;
59 /* Set default router advertise values. */
60 struct rtadvconf
*rtadv
;
62 rtadv
= &zebra_if
->rtadv
;
64 rtadv
->AdvSendAdvertisements
= 0;
65 rtadv
->MaxRtrAdvInterval
= RTADV_MAX_RTR_ADV_INTERVAL
;
66 rtadv
->MinRtrAdvInterval
= RTADV_MIN_RTR_ADV_INTERVAL
;
67 rtadv
->AdvIntervalTimer
= 0;
68 rtadv
->AdvManagedFlag
= 0;
69 rtadv
->AdvOtherConfigFlag
= 0;
70 rtadv
->AdvHomeAgentFlag
= 0;
71 rtadv
->AdvLinkMTU
= 0;
72 rtadv
->AdvReachableTime
= 0;
73 rtadv
->AdvRetransTimer
= 0;
74 rtadv
->AdvCurHopLimit
= 0;
75 rtadv
->AdvDefaultLifetime
= RTADV_ADV_DEFAULT_LIFETIME
;
76 rtadv
->HomeAgentPreference
= 0;
77 rtadv
->HomeAgentLifetime
= RTADV_ADV_DEFAULT_LIFETIME
;
78 rtadv
->AdvIntervalOption
= 0;
80 rtadv
->AdvPrefixList
= list_new ();
84 /* Initialize installed address chains tree. */
85 zebra_if
->ipv4_subnets
= route_table_init ();
91 /* Called when interface is deleted. */
93 if_zebra_delete_hook (struct interface
*ifp
)
95 struct zebra_if
*zebra_if
;
101 /* Free installed address chains tree. */
102 if (zebra_if
->ipv4_subnets
)
103 route_table_finish (zebra_if
->ipv4_subnets
);
105 XFREE (MTYPE_TMP
, zebra_if
);
111 /* Tie an interface address to its derived subnet list of addresses. */
113 if_subnet_add (struct interface
*ifp
, struct connected
*ifc
)
115 struct route_node
*rn
;
116 struct zebra_if
*zebra_if
;
118 struct list
*addr_list
;
120 assert (ifp
&& ifp
->info
&& ifc
);
121 zebra_if
= ifp
->info
;
123 /* Get address derived subnet node and associated address list, while marking
124 address secondary attribute appropriately. */
127 rn
= route_node_get (zebra_if
->ipv4_subnets
, &cp
);
129 if ((addr_list
= rn
->info
))
130 SET_FLAG (ifc
->flags
, ZEBRA_IFA_SECONDARY
);
133 UNSET_FLAG (ifc
->flags
, ZEBRA_IFA_SECONDARY
);
134 rn
->info
= addr_list
= list_new ();
135 route_lock_node (rn
);
138 /* Tie address at the tail of address list. */
139 listnode_add (addr_list
, ifc
);
141 /* Return list element count. */
142 return (addr_list
->count
);
145 /* Untie an interface address from its derived subnet list of addresses. */
147 if_subnet_delete (struct interface
*ifp
, struct connected
*ifc
)
149 struct route_node
*rn
;
150 struct zebra_if
*zebra_if
;
151 struct list
*addr_list
;
153 assert (ifp
&& ifp
->info
&& ifc
);
154 zebra_if
= ifp
->info
;
156 /* Get address derived subnet node. */
157 rn
= route_node_lookup (zebra_if
->ipv4_subnets
, ifc
->address
);
158 if (! (rn
&& rn
->info
))
160 route_unlock_node (rn
);
162 /* Untie address from subnet's address list. */
163 addr_list
= rn
->info
;
164 listnode_delete (addr_list
, ifc
);
165 route_unlock_node (rn
);
167 /* Return list element count, if not empty. */
168 if (addr_list
->count
)
170 /* If deleted address is primary, mark subsequent one as such and distribute. */
171 if (! CHECK_FLAG (ifc
->flags
, ZEBRA_IFA_SECONDARY
))
173 ifc
= listgetdata (listhead (addr_list
));
174 zebra_interface_address_delete_update (ifp
, ifc
);
175 UNSET_FLAG (ifc
->flags
, ZEBRA_IFA_SECONDARY
);
176 zebra_interface_address_add_update (ifp
, ifc
);
179 return addr_list
->count
;
182 /* Otherwise, free list and route node. */
183 list_free (addr_list
);
185 route_unlock_node (rn
);
190 /* Wake up configured address if it is not in current kernel
193 if_addr_wakeup (struct interface
*ifp
)
195 struct listnode
*node
, *nnode
;
196 struct connected
*ifc
;
200 for (ALL_LIST_ELEMENTS (ifp
->connected
, node
, nnode
, ifc
))
204 if (CHECK_FLAG (ifc
->conf
, ZEBRA_IFC_CONFIGURED
)
205 && ! CHECK_FLAG (ifc
->conf
, ZEBRA_IFC_REAL
))
208 if (p
->family
== AF_INET
)
210 if (! if_is_up (ifp
))
212 if_set_flags (ifp
, IFF_UP
| IFF_RUNNING
);
216 ret
= if_set_prefix (ifp
, ifc
);
219 zlog_warn ("Can't set interface's address: %s",
220 safe_strerror(errno
));
224 /* Add to subnet chain list. */
225 if_subnet_add (ifp
, ifc
);
227 SET_FLAG (ifc
->conf
, ZEBRA_IFC_REAL
);
229 zebra_interface_address_add_update (ifp
, ifc
);
231 if (if_is_operative(ifp
))
232 connected_up_ipv4 (ifp
, ifc
);
235 if (p
->family
== AF_INET6
)
237 if (! if_is_up (ifp
))
239 if_set_flags (ifp
, IFF_UP
| IFF_RUNNING
);
243 ret
= if_prefix_add_ipv6 (ifp
, ifc
);
246 zlog_warn ("Can't set interface's address: %s",
247 safe_strerror(errno
));
250 SET_FLAG (ifc
->conf
, ZEBRA_IFC_REAL
);
252 zebra_interface_address_add_update (ifp
, ifc
);
254 if (if_is_operative(ifp
))
255 connected_up_ipv6 (ifp
, ifc
);
257 #endif /* HAVE_IPV6 */
262 /* Handle interface addition */
264 if_add_update (struct interface
*ifp
)
266 struct zebra_if
*if_data
;
269 if (if_data
->multicast
== IF_ZEBRA_MULTICAST_ON
)
270 if_set_flags (ifp
, IFF_MULTICAST
);
271 else if (if_data
->multicast
== IF_ZEBRA_MULTICAST_OFF
)
272 if_unset_flags (ifp
, IFF_MULTICAST
);
274 zebra_interface_add_update (ifp
);
276 if (! CHECK_FLAG (ifp
->status
, ZEBRA_INTERFACE_ACTIVE
))
278 SET_FLAG (ifp
->status
, ZEBRA_INTERFACE_ACTIVE
);
280 if_addr_wakeup (ifp
);
282 if (IS_ZEBRA_DEBUG_KERNEL
)
283 zlog_debug ("interface %s index %d becomes active.",
284 ifp
->name
, ifp
->ifindex
);
288 if (IS_ZEBRA_DEBUG_KERNEL
)
289 zlog_debug ("interface %s index %d is added.", ifp
->name
, ifp
->ifindex
);
294 /* Handle an interface delete event
296 * This function is only called when support for
297 * RTM_IFANNOUNCE or AF_NETLINK sockets (RTM_DELLINK message)
298 * is available. It is not called on, eg, Solaris.
300 #if (defined(RTM_IFANNOUNCE) || defined(HAVE_NETLINK))
302 if_delete_update (struct interface
*ifp
)
304 struct listnode
*node
;
305 struct listnode
*next
;
306 struct listnode
*first
;
307 struct listnode
*last
;
308 struct connected
*ifc
;
310 struct route_node
*rn
;
311 struct zebra_if
*zebra_if
;
312 struct list
*addr_list
;
314 zebra_if
= ifp
->info
;
318 zlog_err ("interface %s index %d is still up while being deleted.",
319 ifp
->name
, ifp
->ifindex
);
323 /* Mark interface as inactive */
324 UNSET_FLAG (ifp
->status
, ZEBRA_INTERFACE_ACTIVE
);
326 if (IS_ZEBRA_DEBUG_KERNEL
)
327 zlog_debug ("interface %s index %d is now inactive.",
328 ifp
->name
, ifp
->ifindex
);
330 /* Delete connected routes from the kernel. */
334 while ((node
= (last
? last
->next
: listhead (ifp
->connected
))))
336 ifc
= listgetdata (node
);
339 if (p
->family
== AF_INET
)
341 rn
= route_node_lookup (zebra_if
->ipv4_subnets
, p
);
342 route_unlock_node (rn
);
343 addr_list
= (struct list
*) rn
->info
;
345 /* Remove addresses, secondaries first. */
346 first
= listhead (addr_list
);
347 for (node
= first
->next
; node
|| first
; node
= next
)
356 ifc
= listgetdata (node
);
359 connected_down_ipv4 (ifp
, ifc
);
361 zebra_interface_address_delete_update (ifp
, ifc
);
363 UNSET_FLAG (ifc
->conf
, ZEBRA_IFC_REAL
);
365 /* Remove from subnet chain. */
366 list_delete_node (addr_list
, node
);
367 route_unlock_node (rn
);
369 /* Remove from interface address list (unconditionally). */
370 listnode_delete (ifp
->connected
, ifc
);
371 connected_free (ifc
);
374 /* Free chain list and respective route node. */
375 list_delete (addr_list
);
377 route_unlock_node (rn
);
380 else if (p
->family
== AF_INET6
)
382 connected_down_ipv6 (ifp
, ifc
);
384 zebra_interface_address_delete_update (ifp
, ifc
);
386 UNSET_FLAG (ifc
->conf
, ZEBRA_IFC_REAL
);
388 if (CHECK_FLAG (ifc
->conf
, ZEBRA_IFC_CONFIGURED
))
392 listnode_delete (ifp
->connected
, ifc
);
393 connected_free (ifc
);
396 #endif /* HAVE_IPV6 */
399 zebra_interface_delete_update (ifp
);
401 /* Update ifindex after distributing the delete message. This is in
402 case any client needs to have the old value of ifindex available
403 while processing the deletion. Each client daemon is responsible
404 for setting ifindex to IFINDEX_INTERNAL after processing the
405 interface deletion message. */
406 ifp
->ifindex
= IFINDEX_INTERNAL
;
408 #endif /* (defined(RTM_IFANNOUNCE) || defined(HAVE_NETLINK) */
410 /* Interface is up. */
412 if_up (struct interface
*ifp
)
414 struct listnode
*node
;
415 struct listnode
*next
;
416 struct connected
*ifc
;
419 /* Notify the protocol daemons. */
420 zebra_interface_up_update (ifp
);
422 /* Install connected routes to the kernel. */
425 for (ALL_LIST_ELEMENTS (ifp
->connected
, node
, next
, ifc
))
429 if (p
->family
== AF_INET
)
430 connected_up_ipv4 (ifp
, ifc
);
432 else if (p
->family
== AF_INET6
)
433 connected_up_ipv6 (ifp
, ifc
);
434 #endif /* HAVE_IPV6 */
438 /* Examine all static routes. */
442 /* Interface goes down. We have to manage different behavior of based
445 if_down (struct interface
*ifp
)
447 struct listnode
*node
;
448 struct listnode
*next
;
449 struct connected
*ifc
;
452 /* Notify to the protocol daemons. */
453 zebra_interface_down_update (ifp
);
455 /* Delete connected routes from the kernel. */
458 for (ALL_LIST_ELEMENTS (ifp
->connected
, node
, next
, ifc
))
462 if (p
->family
== AF_INET
)
463 connected_down_ipv4 (ifp
, ifc
);
465 else if (p
->family
== AF_INET6
)
466 connected_down_ipv6 (ifp
, ifc
);
467 #endif /* HAVE_IPV6 */
471 /* Examine all static routes which direct to the interface. */
476 if_refresh (struct interface
*ifp
)
478 if (if_is_operative (ifp
))
481 if (! if_is_operative (ifp
))
487 if (if_is_operative (ifp
))
492 /* Printout flag information into vty */
494 if_flag_dump_vty (struct vty
*vty
, unsigned long flag
)
498 #define IFF_OUT_VTY(X, Y) \
499 if ((X) && (flag & (X))) \
502 vty_out (vty, ","); \
509 IFF_OUT_VTY (IFF_UP
, "UP");
510 IFF_OUT_VTY (IFF_BROADCAST
, "BROADCAST");
511 IFF_OUT_VTY (IFF_DEBUG
, "DEBUG");
512 IFF_OUT_VTY (IFF_LOOPBACK
, "LOOPBACK");
513 IFF_OUT_VTY (IFF_POINTOPOINT
, "POINTOPOINT");
514 IFF_OUT_VTY (IFF_NOTRAILERS
, "NOTRAILERS");
515 IFF_OUT_VTY (IFF_RUNNING
, "RUNNING");
516 IFF_OUT_VTY (IFF_NOARP
, "NOARP");
517 IFF_OUT_VTY (IFF_PROMISC
, "PROMISC");
518 IFF_OUT_VTY (IFF_ALLMULTI
, "ALLMULTI");
519 IFF_OUT_VTY (IFF_OACTIVE
, "OACTIVE");
520 IFF_OUT_VTY (IFF_SIMPLEX
, "SIMPLEX");
521 IFF_OUT_VTY (IFF_LINK0
, "LINK0");
522 IFF_OUT_VTY (IFF_LINK1
, "LINK1");
523 IFF_OUT_VTY (IFF_LINK2
, "LINK2");
524 IFF_OUT_VTY (IFF_MULTICAST
, "MULTICAST");
526 IFF_OUT_VTY (IFF_IPV4
, "IFF_IPv4");
527 IFF_OUT_VTY (IFF_IPV6
, "IFF_IPv6");
528 #endif /* SOLARIS_IPV6 */
532 /* Output prefix string to vty. */
534 prefix_vty_out (struct vty
*vty
, struct prefix
*p
)
536 char str
[INET6_ADDRSTRLEN
];
538 inet_ntop (p
->family
, &p
->u
.prefix
, str
, sizeof (str
));
539 vty_out (vty
, "%s", str
);
543 /* Dump if address information to vty. */
545 connected_dump_vty (struct vty
*vty
, struct connected
*connected
)
548 struct interface
*ifp
;
550 /* Set interface pointer. */
551 ifp
= connected
->ifp
;
553 /* Print interface address. */
554 p
= connected
->address
;
555 vty_out (vty
, " %s ", prefix_family_str (p
));
556 prefix_vty_out (vty
, p
);
557 vty_out (vty
, "/%d", p
->prefixlen
);
559 /* If there is destination address, print it. */
560 p
= connected
->destination
;
563 if (p
->family
== AF_INET
)
564 if (ifp
->flags
& IFF_BROADCAST
)
566 vty_out (vty
, " broadcast ");
567 prefix_vty_out (vty
, p
);
570 if (ifp
->flags
& IFF_POINTOPOINT
)
572 vty_out (vty
, " pointopoint ");
573 prefix_vty_out (vty
, p
);
577 if (CHECK_FLAG (connected
->flags
, ZEBRA_IFA_SECONDARY
))
578 vty_out (vty
, " secondary");
580 if (connected
->label
)
581 vty_out (vty
, " %s", connected
->label
);
583 vty_out (vty
, "%s", VTY_NEWLINE
);
587 /* Dump interface ND information to vty. */
589 nd_dump_vty (struct vty
*vty
, struct interface
*ifp
)
591 struct zebra_if
*zif
;
592 struct rtadvconf
*rtadv
;
595 zif
= (struct zebra_if
*) ifp
->info
;
598 if (rtadv
->AdvSendAdvertisements
)
600 vty_out (vty
, " ND advertised reachable time is %d milliseconds%s",
601 rtadv
->AdvReachableTime
, VTY_NEWLINE
);
602 vty_out (vty
, " ND advertised retransmit interval is %d milliseconds%s",
603 rtadv
->AdvRetransTimer
, VTY_NEWLINE
);
604 interval
= rtadv
->MaxRtrAdvInterval
;
606 vty_out (vty
, " ND router advertisements are sent every "
607 "%d milliseconds%s", interval
,
610 vty_out (vty
, " ND router advertisements are sent every "
611 "%d seconds%s", interval
/ 1000,
613 vty_out (vty
, " ND router advertisements live for %d seconds%s",
614 rtadv
->AdvDefaultLifetime
, VTY_NEWLINE
);
615 if (rtadv
->AdvManagedFlag
)
616 vty_out (vty
, " Hosts use DHCP to obtain routable addresses.%s",
619 vty_out (vty
, " Hosts use stateless autoconfig for addresses.%s",
621 if (rtadv
->AdvHomeAgentFlag
)
622 vty_out (vty
, " ND router advertisements with "
623 "Home Agent flag bit set.%s",
625 if (rtadv
->AdvIntervalOption
)
626 vty_out (vty
, " ND router advertisements with Adv. Interval option.%s",
632 /* Interface's information print out to vty interface. */
634 if_dump_vty (struct vty
*vty
, struct interface
*ifp
)
636 #ifdef HAVE_SOCKADDR_DL
637 struct sockaddr_dl
*sdl
;
638 #endif /* HAVE_SOCKADDR_DL */
639 struct connected
*connected
;
640 struct listnode
*node
;
641 struct route_node
*rn
;
642 struct zebra_if
*zebra_if
;
644 zebra_if
= ifp
->info
;
646 vty_out (vty
, "Interface %s is ", ifp
->name
);
648 vty_out (vty
, "up, line protocol ");
650 if (CHECK_FLAG(ifp
->status
, ZEBRA_INTERFACE_LINKDETECTION
)) {
651 if (if_is_running(ifp
))
652 vty_out (vty
, "is up%s", VTY_NEWLINE
);
654 vty_out (vty
, "is down%s", VTY_NEWLINE
);
656 vty_out (vty
, "detection is disabled%s", VTY_NEWLINE
);
659 vty_out (vty
, "down%s", VTY_NEWLINE
);
663 vty_out (vty
, " Description: %s%s", ifp
->desc
,
665 if (ifp
->ifindex
== IFINDEX_INTERNAL
)
667 vty_out(vty
, " pseudo interface%s", VTY_NEWLINE
);
670 else if (! CHECK_FLAG (ifp
->status
, ZEBRA_INTERFACE_ACTIVE
))
672 vty_out(vty
, " index %d inactive interface%s",
678 vty_out (vty
, " index %d metric %d mtu %d ",
679 ifp
->ifindex
, ifp
->metric
, ifp
->mtu
);
680 if_flag_dump_vty (vty
, ifp
->flags
);
682 if (ifp
->mtu6
!= ifp
->mtu
)
683 vty_out (vty
, "mtu6 %d ", ifp
->mtu6
);
686 vty_out (vty
, "%s", VTY_NEWLINE
);
688 /* Hardware address. */
689 #ifdef HAVE_SOCKADDR_DL
691 if (sdl
!= NULL
&& sdl
->sdl_alen
!= 0)
696 vty_out (vty
, " HWaddr: ");
697 for (i
= 0, ptr
= (u_char
*)LLADDR (sdl
); i
< sdl
->sdl_alen
; i
++, ptr
++)
698 vty_out (vty
, "%s%02x", i
== 0 ? "" : ":", *ptr
);
699 vty_out (vty
, "%s", VTY_NEWLINE
);
702 if (ifp
->hw_addr_len
!= 0)
706 vty_out (vty
, " HWaddr: ");
707 for (i
= 0; i
< ifp
->hw_addr_len
; i
++)
708 vty_out (vty
, "%s%02x", i
== 0 ? "" : ":", ifp
->hw_addr
[i
]);
709 vty_out (vty
, "%s", VTY_NEWLINE
);
711 #endif /* HAVE_SOCKADDR_DL */
713 /* Bandwidth in kbps */
714 if (ifp
->bandwidth
!= 0)
716 vty_out(vty
, " bandwidth %u kbps", ifp
->bandwidth
);
717 vty_out(vty
, "%s", VTY_NEWLINE
);
720 for (rn
= route_top (zebra_if
->ipv4_subnets
); rn
; rn
= route_next (rn
))
725 for (ALL_LIST_ELEMENTS_RO ((struct list
*)rn
->info
, node
, connected
))
726 connected_dump_vty (vty
, connected
);
729 for (ALL_LIST_ELEMENTS_RO (ifp
->connected
, node
, connected
))
731 if (CHECK_FLAG (connected
->conf
, ZEBRA_IFC_REAL
) &&
732 (connected
->address
->family
== AF_INET6
))
733 connected_dump_vty (vty
, connected
);
737 nd_dump_vty (vty
, ifp
);
740 #ifdef HAVE_PROC_NET_DEV
741 /* Statistics print out using proc file system. */
742 vty_out (vty
, " %lu input packets (%lu multicast), %lu bytes, "
744 ifp
->stats
.rx_packets
, ifp
->stats
.rx_multicast
,
745 ifp
->stats
.rx_bytes
, ifp
->stats
.rx_dropped
, VTY_NEWLINE
);
747 vty_out (vty
, " %lu input errors, %lu length, %lu overrun,"
748 " %lu CRC, %lu frame%s",
749 ifp
->stats
.rx_errors
, ifp
->stats
.rx_length_errors
,
750 ifp
->stats
.rx_over_errors
, ifp
->stats
.rx_crc_errors
,
751 ifp
->stats
.rx_frame_errors
, VTY_NEWLINE
);
753 vty_out (vty
, " %lu fifo, %lu missed%s", ifp
->stats
.rx_fifo_errors
,
754 ifp
->stats
.rx_missed_errors
, VTY_NEWLINE
);
756 vty_out (vty
, " %lu output packets, %lu bytes, %lu dropped%s",
757 ifp
->stats
.tx_packets
, ifp
->stats
.tx_bytes
,
758 ifp
->stats
.tx_dropped
, VTY_NEWLINE
);
760 vty_out (vty
, " %lu output errors, %lu aborted, %lu carrier,"
761 " %lu fifo, %lu heartbeat%s",
762 ifp
->stats
.tx_errors
, ifp
->stats
.tx_aborted_errors
,
763 ifp
->stats
.tx_carrier_errors
, ifp
->stats
.tx_fifo_errors
,
764 ifp
->stats
.tx_heartbeat_errors
, VTY_NEWLINE
);
766 vty_out (vty
, " %lu window, %lu collisions%s",
767 ifp
->stats
.tx_window_errors
, ifp
->stats
.collisions
, VTY_NEWLINE
);
768 #endif /* HAVE_PROC_NET_DEV */
770 #ifdef HAVE_NET_RT_IFLIST
771 #if defined (__bsdi__) || defined (__NetBSD__)
772 /* Statistics print out using sysctl (). */
773 vty_out (vty
, " input packets %qu, bytes %qu, dropped %qu,"
774 " multicast packets %qu%s",
775 ifp
->stats
.ifi_ipackets
, ifp
->stats
.ifi_ibytes
,
776 ifp
->stats
.ifi_iqdrops
, ifp
->stats
.ifi_imcasts
,
779 vty_out (vty
, " input errors %qu%s",
780 ifp
->stats
.ifi_ierrors
, VTY_NEWLINE
);
782 vty_out (vty
, " output packets %qu, bytes %qu, multicast packets %qu%s",
783 ifp
->stats
.ifi_opackets
, ifp
->stats
.ifi_obytes
,
784 ifp
->stats
.ifi_omcasts
, VTY_NEWLINE
);
786 vty_out (vty
, " output errors %qu%s",
787 ifp
->stats
.ifi_oerrors
, VTY_NEWLINE
);
789 vty_out (vty
, " collisions %qu%s",
790 ifp
->stats
.ifi_collisions
, VTY_NEWLINE
);
792 /* Statistics print out using sysctl (). */
793 vty_out (vty
, " input packets %lu, bytes %lu, dropped %lu,"
794 " multicast packets %lu%s",
795 ifp
->stats
.ifi_ipackets
, ifp
->stats
.ifi_ibytes
,
796 ifp
->stats
.ifi_iqdrops
, ifp
->stats
.ifi_imcasts
,
799 vty_out (vty
, " input errors %lu%s",
800 ifp
->stats
.ifi_ierrors
, VTY_NEWLINE
);
802 vty_out (vty
, " output packets %lu, bytes %lu, multicast packets %lu%s",
803 ifp
->stats
.ifi_opackets
, ifp
->stats
.ifi_obytes
,
804 ifp
->stats
.ifi_omcasts
, VTY_NEWLINE
);
806 vty_out (vty
, " output errors %lu%s",
807 ifp
->stats
.ifi_oerrors
, VTY_NEWLINE
);
809 vty_out (vty
, " collisions %lu%s",
810 ifp
->stats
.ifi_collisions
, VTY_NEWLINE
);
811 #endif /* __bsdi__ || __NetBSD__ */
812 #endif /* HAVE_NET_RT_IFLIST */
815 /* Check supported address family. */
817 if_supported_family (int family
)
819 if (family
== AF_INET
)
822 if (family
== AF_INET6
)
824 #endif /* HAVE_IPV6 */
828 /* Wrapper hook point for zebra daemon so that ifindex can be set
829 * DEFUN macro not used as extract.pl HAS to ignore this
830 * See also interface_cmd in lib/if.c
832 DEFUN_NOSH (zebra_interface
,
835 "Select an interface to configure\n"
836 "Interface's name\n")
839 struct interface
* ifp
;
841 /* Call lib interface() */
842 if ((ret
= interface_cmd
.func (self
, vty
, argc
, argv
)) != CMD_SUCCESS
)
847 if (ifp
->ifindex
== IFINDEX_INTERNAL
)
848 /* Is this really necessary? Shouldn't status be initialized to 0
850 UNSET_FLAG (ifp
->status
, ZEBRA_INTERFACE_ACTIVE
);
855 struct cmd_node interface_node
=
862 /* Show all or specified interface to vty. */
863 DEFUN (show_interface
, show_interface_cmd
,
864 "show interface [IFNAME]",
866 "Interface status and configuration\n"
869 struct listnode
*node
;
870 struct interface
*ifp
;
872 #ifdef HAVE_PROC_NET_DEV
873 /* If system has interface statistics via proc file system, update
875 ifstat_update_proc ();
876 #endif /* HAVE_PROC_NET_DEV */
877 #ifdef HAVE_NET_RT_IFLIST
878 ifstat_update_sysctl ();
879 #endif /* HAVE_NET_RT_IFLIST */
881 /* Specified interface print. */
884 ifp
= if_lookup_by_name (argv
[0]);
887 vty_out (vty
, "%% Can't find interface %s%s", argv
[0],
891 if_dump_vty (vty
, ifp
);
895 /* All interface print. */
896 for (ALL_LIST_ELEMENTS_RO (iflist
, node
, ifp
))
897 if_dump_vty (vty
, ifp
);
902 DEFUN (show_interface_desc
,
903 show_interface_desc_cmd
,
904 "show interface description",
906 "Interface status and configuration\n"
907 "Interface description\n")
909 struct listnode
*node
;
910 struct interface
*ifp
;
912 vty_out (vty
, "Interface Status Protocol Description%s", VTY_NEWLINE
);
913 for (ALL_LIST_ELEMENTS_RO (iflist
, node
, ifp
))
917 len
= vty_out (vty
, "%s", ifp
->name
);
918 vty_out (vty
, "%*s", (16 - len
), " ");
922 vty_out (vty
, "up ");
923 if (CHECK_FLAG(ifp
->status
, ZEBRA_INTERFACE_LINKDETECTION
))
925 if (if_is_running(ifp
))
926 vty_out (vty
, "up ");
928 vty_out (vty
, "down ");
932 vty_out (vty
, "unknown ");
937 vty_out (vty
, "down down ");
941 vty_out (vty
, "%s", ifp
->desc
);
942 vty_out (vty
, "%s", VTY_NEWLINE
);
950 "Set multicast flag to interface\n")
953 struct interface
*ifp
;
954 struct zebra_if
*if_data
;
956 ifp
= (struct interface
*) vty
->index
;
957 if (CHECK_FLAG (ifp
->status
, ZEBRA_INTERFACE_ACTIVE
))
959 ret
= if_set_flags (ifp
, IFF_MULTICAST
);
962 vty_out (vty
, "Can't set multicast flag%s", VTY_NEWLINE
);
968 if_data
->multicast
= IF_ZEBRA_MULTICAST_ON
;
977 "Unset multicast flag to interface\n")
980 struct interface
*ifp
;
981 struct zebra_if
*if_data
;
983 ifp
= (struct interface
*) vty
->index
;
984 if (CHECK_FLAG (ifp
->status
, ZEBRA_INTERFACE_ACTIVE
))
986 ret
= if_unset_flags (ifp
, IFF_MULTICAST
);
989 vty_out (vty
, "Can't unset multicast flag%s", VTY_NEWLINE
);
995 if_data
->multicast
= IF_ZEBRA_MULTICAST_OFF
;
1003 "Enable link detection on interface\n")
1005 struct interface
*ifp
;
1006 int if_was_operative
;
1008 ifp
= (struct interface
*) vty
->index
;
1009 if_was_operative
= if_is_operative(ifp
);
1010 SET_FLAG(ifp
->status
, ZEBRA_INTERFACE_LINKDETECTION
);
1012 /* When linkdetection is enabled, if might come down */
1013 if (!if_is_operative(ifp
) && if_was_operative
) if_down(ifp
);
1015 /* FIXME: Will defer status change forwarding if interface
1016 does not come down! */
1022 DEFUN (no_linkdetect
,
1026 "Disable link detection on interface\n")
1028 struct interface
*ifp
;
1029 int if_was_operative
;
1031 ifp
= (struct interface
*) vty
->index
;
1032 if_was_operative
= if_is_operative(ifp
);
1033 UNSET_FLAG(ifp
->status
, ZEBRA_INTERFACE_LINKDETECTION
);
1035 /* Interface may come up after disabling link detection */
1036 if (if_is_operative(ifp
) && !if_was_operative
) if_up(ifp
);
1038 /* FIXME: see linkdetect_cmd */
1046 "Shutdown the selected interface\n")
1049 struct interface
*ifp
;
1050 struct zebra_if
*if_data
;
1052 ifp
= (struct interface
*) vty
->index
;
1053 ret
= if_unset_flags (ifp
, IFF_UP
);
1056 vty_out (vty
, "Can't shutdown interface%s", VTY_NEWLINE
);
1060 if_data
= ifp
->info
;
1061 if_data
->shutdown
= IF_ZEBRA_SHUTDOWN_ON
;
1066 DEFUN (no_shutdown_if
,
1070 "Shutdown the selected interface\n")
1073 struct interface
*ifp
;
1074 struct zebra_if
*if_data
;
1076 ifp
= (struct interface
*) vty
->index
;
1077 ret
= if_set_flags (ifp
, IFF_UP
| IFF_RUNNING
);
1080 vty_out (vty
, "Can't up interface%s", VTY_NEWLINE
);
1084 if_data
= ifp
->info
;
1085 if_data
->shutdown
= IF_ZEBRA_SHUTDOWN_OFF
;
1090 DEFUN (bandwidth_if
,
1092 "bandwidth <1-10000000>",
1093 "Set bandwidth informational parameter\n"
1094 "Bandwidth in kilobits\n")
1096 struct interface
*ifp
;
1097 unsigned int bandwidth
;
1099 ifp
= (struct interface
*) vty
->index
;
1100 bandwidth
= strtol(argv
[0], NULL
, 10);
1102 /* bandwidth range is <1-10000000> */
1103 if (bandwidth
< 1 || bandwidth
> 10000000)
1105 vty_out (vty
, "Bandwidth is invalid%s", VTY_NEWLINE
);
1109 ifp
->bandwidth
= bandwidth
;
1111 /* force protocols to recalculate routes due to cost change */
1112 if (if_is_operative (ifp
))
1113 zebra_interface_up_update (ifp
);
1118 DEFUN (no_bandwidth_if
,
1119 no_bandwidth_if_cmd
,
1122 "Set bandwidth informational parameter\n")
1124 struct interface
*ifp
;
1126 ifp
= (struct interface
*) vty
->index
;
1130 /* force protocols to recalculate routes due to cost change */
1131 if (if_is_operative (ifp
))
1132 zebra_interface_up_update (ifp
);
1137 ALIAS (no_bandwidth_if
,
1138 no_bandwidth_if_val_cmd
,
1139 "no bandwidth <1-10000000>",
1141 "Set bandwidth informational parameter\n"
1142 "Bandwidth in kilobits\n")
1145 ip_address_install (struct vty
*vty
, struct interface
*ifp
,
1146 const char *addr_str
, const char *peer_str
,
1149 struct prefix_ipv4 cp
;
1150 struct connected
*ifc
;
1151 struct prefix_ipv4
*p
;
1154 ret
= str2prefix_ipv4 (addr_str
, &cp
);
1157 vty_out (vty
, "%% Malformed address %s", VTY_NEWLINE
);
1161 ifc
= connected_check_ipv4 (ifp
, (struct prefix
*) &cp
);
1164 ifc
= connected_new ();
1168 p
= prefix_ipv4_new ();
1170 ifc
->address
= (struct prefix
*) p
;
1173 if (p
->prefixlen
<= IPV4_MAX_PREFIXLEN
-2)
1175 p
= prefix_ipv4_new ();
1177 p
->prefix
.s_addr
= ipv4_broadcast_addr(p
->prefix
.s_addr
,p
->prefixlen
);
1178 ifc
->destination
= (struct prefix
*) p
;
1183 ifc
->label
= strdup (label
);
1185 /* Add to linked list. */
1186 listnode_add (ifp
->connected
, ifc
);
1189 /* This address is configured from zebra. */
1190 if (! CHECK_FLAG (ifc
->conf
, ZEBRA_IFC_CONFIGURED
))
1191 SET_FLAG (ifc
->conf
, ZEBRA_IFC_CONFIGURED
);
1193 /* In case of this route need to install kernel. */
1194 if (! CHECK_FLAG (ifc
->conf
, ZEBRA_IFC_REAL
)
1195 && CHECK_FLAG (ifp
->status
, ZEBRA_INTERFACE_ACTIVE
))
1197 /* Some system need to up the interface to set IP address. */
1198 if (! if_is_up (ifp
))
1200 if_set_flags (ifp
, IFF_UP
| IFF_RUNNING
);
1204 ret
= if_set_prefix (ifp
, ifc
);
1207 vty_out (vty
, "%% Can't set interface IP address: %s.%s",
1208 safe_strerror(errno
), VTY_NEWLINE
);
1212 /* Add to subnet chain list (while marking secondary attribute). */
1213 if_subnet_add (ifp
, ifc
);
1215 /* IP address propery set. */
1216 SET_FLAG (ifc
->conf
, ZEBRA_IFC_REAL
);
1218 /* Update interface address information to protocol daemon. */
1219 zebra_interface_address_add_update (ifp
, ifc
);
1221 /* If interface is up register connected route. */
1222 if (if_is_operative(ifp
))
1223 connected_up_ipv4 (ifp
, ifc
);
1230 ip_address_uninstall (struct vty
*vty
, struct interface
*ifp
,
1231 const char *addr_str
, const char *peer_str
,
1234 struct prefix_ipv4 cp
;
1235 struct connected
*ifc
;
1238 /* Convert to prefix structure. */
1239 ret
= str2prefix_ipv4 (addr_str
, &cp
);
1242 vty_out (vty
, "%% Malformed address %s", VTY_NEWLINE
);
1246 /* Check current interface address. */
1247 ifc
= connected_check_ipv4 (ifp
, (struct prefix
*) &cp
);
1250 vty_out (vty
, "%% Can't find address%s", VTY_NEWLINE
);
1254 /* This is not configured address. */
1255 if (! CHECK_FLAG (ifc
->conf
, ZEBRA_IFC_CONFIGURED
))
1258 /* This is not real address or interface is not active. */
1259 if (! CHECK_FLAG (ifc
->conf
, ZEBRA_IFC_REAL
)
1260 || ! CHECK_FLAG (ifp
->status
, ZEBRA_INTERFACE_ACTIVE
))
1262 listnode_delete (ifp
->connected
, ifc
);
1263 connected_free (ifc
);
1267 /* This is real route. */
1268 ret
= if_unset_prefix (ifp
, ifc
);
1271 vty_out (vty
, "%% Can't unset interface IP address: %s.%s",
1272 safe_strerror(errno
), VTY_NEWLINE
);
1277 /* Redistribute this information. */
1278 zebra_interface_address_delete_update (ifp
, ifc
);
1280 /* Remove connected route. */
1281 connected_down_ipv4 (ifp
, ifc
);
1283 /* Free address information. */
1284 listnode_delete (ifp
->connected
, ifc
);
1285 connected_free (ifc
);
1293 "ip address A.B.C.D/M",
1294 "Interface Internet Protocol config commands\n"
1295 "Set the IP address of an interface\n"
1296 "IP address (e.g. 10.0.0.1/8)\n")
1298 return ip_address_install (vty
, vty
->index
, argv
[0], NULL
, NULL
);
1301 DEFUN (no_ip_address
,
1303 "no ip address A.B.C.D/M",
1305 "Interface Internet Protocol config commands\n"
1306 "Set the IP address of an interface\n"
1307 "IP Address (e.g. 10.0.0.1/8)")
1309 return ip_address_uninstall (vty
, vty
->index
, argv
[0], NULL
, NULL
);
1313 DEFUN (ip_address_label
,
1314 ip_address_label_cmd
,
1315 "ip address A.B.C.D/M label LINE",
1316 "Interface Internet Protocol config commands\n"
1317 "Set the IP address of an interface\n"
1318 "IP address (e.g. 10.0.0.1/8)\n"
1319 "Label of this address\n"
1322 return ip_address_install (vty
, vty
->index
, argv
[0], NULL
, argv
[1]);
1325 DEFUN (no_ip_address_label
,
1326 no_ip_address_label_cmd
,
1327 "no ip address A.B.C.D/M label LINE",
1329 "Interface Internet Protocol config commands\n"
1330 "Set the IP address of an interface\n"
1331 "IP address (e.g. 10.0.0.1/8)\n"
1332 "Label of this address\n"
1335 return ip_address_uninstall (vty
, vty
->index
, argv
[0], NULL
, argv
[1]);
1337 #endif /* HAVE_NETLINK */
1341 ipv6_address_install (struct vty
*vty
, struct interface
*ifp
,
1342 const char *addr_str
, const char *peer_str
,
1343 const char *label
, int secondary
)
1345 struct prefix_ipv6 cp
;
1346 struct connected
*ifc
;
1347 struct prefix_ipv6
*p
;
1350 ret
= str2prefix_ipv6 (addr_str
, &cp
);
1353 vty_out (vty
, "%% Malformed address %s", VTY_NEWLINE
);
1357 ifc
= connected_check_ipv6 (ifp
, (struct prefix
*) &cp
);
1360 ifc
= connected_new ();
1364 p
= prefix_ipv6_new ();
1366 ifc
->address
= (struct prefix
*) p
;
1370 SET_FLAG (ifc
->flags
, ZEBRA_IFA_SECONDARY
);
1374 ifc
->label
= strdup (label
);
1376 /* Add to linked list. */
1377 listnode_add (ifp
->connected
, ifc
);
1380 /* This address is configured from zebra. */
1381 if (! CHECK_FLAG (ifc
->conf
, ZEBRA_IFC_CONFIGURED
))
1382 SET_FLAG (ifc
->conf
, ZEBRA_IFC_CONFIGURED
);
1384 /* In case of this route need to install kernel. */
1385 if (! CHECK_FLAG (ifc
->conf
, ZEBRA_IFC_REAL
)
1386 && CHECK_FLAG (ifp
->status
, ZEBRA_INTERFACE_ACTIVE
))
1388 /* Some system need to up the interface to set IP address. */
1389 if (! if_is_up (ifp
))
1391 if_set_flags (ifp
, IFF_UP
| IFF_RUNNING
);
1395 ret
= if_prefix_add_ipv6 (ifp
, ifc
);
1399 vty_out (vty
, "%% Can't set interface IP address: %s.%s",
1400 safe_strerror(errno
), VTY_NEWLINE
);
1404 /* IP address propery set. */
1405 SET_FLAG (ifc
->conf
, ZEBRA_IFC_REAL
);
1407 /* Update interface address information to protocol daemon. */
1408 zebra_interface_address_add_update (ifp
, ifc
);
1410 /* If interface is up register connected route. */
1411 if (if_is_operative(ifp
))
1412 connected_up_ipv6 (ifp
, ifc
);
1419 ipv6_address_uninstall (struct vty
*vty
, struct interface
*ifp
,
1420 const char *addr_str
, const char *peer_str
,
1421 const char *label
, int secondry
)
1423 struct prefix_ipv6 cp
;
1424 struct connected
*ifc
;
1427 /* Convert to prefix structure. */
1428 ret
= str2prefix_ipv6 (addr_str
, &cp
);
1431 vty_out (vty
, "%% Malformed address %s", VTY_NEWLINE
);
1435 /* Check current interface address. */
1436 ifc
= connected_check_ipv6 (ifp
, (struct prefix
*) &cp
);
1439 vty_out (vty
, "%% Can't find address%s", VTY_NEWLINE
);
1443 /* This is not configured address. */
1444 if (! CHECK_FLAG (ifc
->conf
, ZEBRA_IFC_CONFIGURED
))
1447 /* This is not real address or interface is not active. */
1448 if (! CHECK_FLAG (ifc
->conf
, ZEBRA_IFC_REAL
)
1449 || ! CHECK_FLAG (ifp
->status
, ZEBRA_INTERFACE_ACTIVE
))
1451 listnode_delete (ifp
->connected
, ifc
);
1452 connected_free (ifc
);
1456 /* This is real route. */
1457 ret
= if_prefix_delete_ipv6 (ifp
, ifc
);
1460 vty_out (vty
, "%% Can't unset interface IP address: %s.%s",
1461 safe_strerror(errno
), VTY_NEWLINE
);
1465 /* Redistribute this information. */
1466 zebra_interface_address_delete_update (ifp
, ifc
);
1468 /* Remove connected route. */
1469 connected_down_ipv6 (ifp
, ifc
);
1471 /* Free address information. */
1472 listnode_delete (ifp
->connected
, ifc
);
1473 connected_free (ifc
);
1478 DEFUN (ipv6_address
,
1480 "ipv6 address X:X::X:X/M",
1481 "Interface IPv6 config commands\n"
1482 "Set the IP address of an interface\n"
1483 "IPv6 address (e.g. 3ffe:506::1/48)\n")
1485 return ipv6_address_install (vty
, vty
->index
, argv
[0], NULL
, NULL
, 0);
1488 DEFUN (no_ipv6_address
,
1489 no_ipv6_address_cmd
,
1490 "no ipv6 address X:X::X:X/M",
1492 "Interface IPv6 config commands\n"
1493 "Set the IP address of an interface\n"
1494 "IPv6 address (e.g. 3ffe:506::1/48)\n")
1496 return ipv6_address_uninstall (vty
, vty
->index
, argv
[0], NULL
, NULL
, 0);
1498 #endif /* HAVE_IPV6 */
1501 if_config_write (struct vty
*vty
)
1503 struct listnode
*node
;
1504 struct interface
*ifp
;
1507 for (ALL_LIST_ELEMENTS_RO (iflist
, node
, ifp
))
1509 struct zebra_if
*if_data
;
1510 struct listnode
*addrnode
;
1511 struct connected
*ifc
;
1514 if_data
= ifp
->info
;
1516 vty_out (vty
, "interface %s%s", ifp
->name
,
1520 vty_out (vty
, " description %s%s", ifp
->desc
,
1523 /* Assign bandwidth here to avoid unnecessary interface flap
1524 while processing config script */
1525 if (ifp
->bandwidth
!= 0)
1526 vty_out(vty
, " bandwidth %u%s", ifp
->bandwidth
, VTY_NEWLINE
);
1528 if (CHECK_FLAG(ifp
->status
, ZEBRA_INTERFACE_LINKDETECTION
))
1529 vty_out(vty
, " link-detect%s", VTY_NEWLINE
);
1531 for (ALL_LIST_ELEMENTS_RO (ifp
->connected
, addrnode
, ifc
))
1533 if (CHECK_FLAG (ifc
->conf
, ZEBRA_IFC_CONFIGURED
))
1536 vty_out (vty
, " ip%s address %s/%d",
1537 p
->family
== AF_INET
? "" : "v6",
1538 inet_ntop (p
->family
, &p
->u
.prefix
, buf
, BUFSIZ
),
1542 vty_out (vty
, " label %s", ifc
->label
);
1544 vty_out (vty
, "%s", VTY_NEWLINE
);
1550 if (if_data
->shutdown
== IF_ZEBRA_SHUTDOWN_ON
)
1551 vty_out (vty
, " shutdown%s", VTY_NEWLINE
);
1553 if (if_data
->multicast
!= IF_ZEBRA_MULTICAST_UNSPEC
)
1554 vty_out (vty
, " %smulticast%s",
1555 if_data
->multicast
== IF_ZEBRA_MULTICAST_ON
? "" : "no ",
1560 rtadv_config_write (vty
, ifp
);
1564 irdp_config_write (vty
, ifp
);
1567 vty_out (vty
, "!%s", VTY_NEWLINE
);
1572 /* Allocate and initialize interface vector. */
1576 /* Initialize interface and new hook. */
1578 if_add_hook (IF_NEW_HOOK
, if_zebra_new_hook
);
1579 if_add_hook (IF_DELETE_HOOK
, if_zebra_delete_hook
);
1581 /* Install configuration write function. */
1582 install_node (&interface_node
, if_config_write
);
1584 install_element (VIEW_NODE
, &show_interface_cmd
);
1585 install_element (ENABLE_NODE
, &show_interface_cmd
);
1586 install_element (ENABLE_NODE
, &show_interface_desc_cmd
);
1587 install_element (CONFIG_NODE
, &zebra_interface_cmd
);
1588 install_element (CONFIG_NODE
, &no_interface_cmd
);
1589 install_default (INTERFACE_NODE
);
1590 install_element (INTERFACE_NODE
, &interface_desc_cmd
);
1591 install_element (INTERFACE_NODE
, &no_interface_desc_cmd
);
1592 install_element (INTERFACE_NODE
, &multicast_cmd
);
1593 install_element (INTERFACE_NODE
, &no_multicast_cmd
);
1594 install_element (INTERFACE_NODE
, &linkdetect_cmd
);
1595 install_element (INTERFACE_NODE
, &no_linkdetect_cmd
);
1596 install_element (INTERFACE_NODE
, &shutdown_if_cmd
);
1597 install_element (INTERFACE_NODE
, &no_shutdown_if_cmd
);
1598 install_element (INTERFACE_NODE
, &bandwidth_if_cmd
);
1599 install_element (INTERFACE_NODE
, &no_bandwidth_if_cmd
);
1600 install_element (INTERFACE_NODE
, &no_bandwidth_if_val_cmd
);
1601 install_element (INTERFACE_NODE
, &ip_address_cmd
);
1602 install_element (INTERFACE_NODE
, &no_ip_address_cmd
);
1604 install_element (INTERFACE_NODE
, &ipv6_address_cmd
);
1605 install_element (INTERFACE_NODE
, &no_ipv6_address_cmd
);
1606 #endif /* HAVE_IPV6 */
1608 install_element (INTERFACE_NODE
, &ip_address_label_cmd
);
1609 install_element (INTERFACE_NODE
, &no_ip_address_label_cmd
);
1610 #endif /* HAVE_NETLINK */