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"
44 /* Allocate a new internal interface index
45 * This works done from the top so that %d macros
49 if_new_intern_ifindex (void)
51 /* Start here so that first one assigned is 0xFFFFFFFF */
52 static unsigned int ifindex
= IFINDEX_INTERNBASE
+ 1;
57 if ( ifindex
<= IFINDEX_INTERNBASE
)
60 if (if_lookup_by_index(ifindex
) == NULL
)
65 /* Called when new interface is added. */
67 if_zebra_new_hook (struct interface
*ifp
)
69 struct zebra_if
*zebra_if
;
71 zebra_if
= XMALLOC (MTYPE_TMP
, sizeof (struct zebra_if
));
72 memset (zebra_if
, 0, sizeof (struct zebra_if
));
74 zebra_if
->multicast
= IF_ZEBRA_MULTICAST_UNSPEC
;
75 zebra_if
->shutdown
= IF_ZEBRA_SHUTDOWN_UNSPEC
;
79 /* Set default router advertise values. */
80 struct rtadvconf
*rtadv
;
82 rtadv
= &zebra_if
->rtadv
;
84 rtadv
->AdvSendAdvertisements
= 0;
85 rtadv
->MaxRtrAdvInterval
= RTADV_MAX_RTR_ADV_INTERVAL
;
86 rtadv
->MinRtrAdvInterval
= RTADV_MIN_RTR_ADV_INTERVAL
;
87 rtadv
->AdvIntervalTimer
= 0;
88 rtadv
->AdvManagedFlag
= 0;
89 rtadv
->AdvOtherConfigFlag
= 0;
90 rtadv
->AdvLinkMTU
= 0;
91 rtadv
->AdvReachableTime
= 0;
92 rtadv
->AdvRetransTimer
= 0;
93 rtadv
->AdvCurHopLimit
= 0;
94 rtadv
->AdvDefaultLifetime
= RTADV_ADV_DEFAULT_LIFETIME
;
96 rtadv
->AdvPrefixList
= list_new ();
100 /* Initialize installed address chains tree. */
101 zebra_if
->ipv4_subnets
= route_table_init ();
103 ifp
->info
= zebra_if
;
107 /* Called when interface is deleted. */
109 if_zebra_delete_hook (struct interface
*ifp
)
111 struct zebra_if
*zebra_if
;
115 zebra_if
= ifp
->info
;
117 /* Free installed address chains tree. */
118 if (zebra_if
->ipv4_subnets
)
119 route_table_finish (zebra_if
->ipv4_subnets
);
121 XFREE (MTYPE_TMP
, zebra_if
);
127 /* Tie an interface address to its derived subnet list of addresses. */
129 if_subnet_add (struct interface
*ifp
, struct connected
*ifc
)
131 struct route_node
*rn
;
132 struct zebra_if
*zebra_if
;
134 struct list
*addr_list
;
136 assert (ifp
&& ifp
->info
&& ifc
);
137 zebra_if
= ifp
->info
;
139 /* Get address derived subnet node and associated address list, while marking
140 address secondary attribute appropriately. */
143 rn
= route_node_get (zebra_if
->ipv4_subnets
, &cp
);
145 if ((addr_list
= rn
->info
))
146 SET_FLAG (ifc
->flags
, ZEBRA_IFA_SECONDARY
);
149 UNSET_FLAG (ifc
->flags
, ZEBRA_IFA_SECONDARY
);
150 rn
->info
= addr_list
= list_new ();
151 route_lock_node (rn
);
154 /* Tie address at the tail of address list. */
155 listnode_add (addr_list
, ifc
);
157 /* Return list element count. */
158 return (addr_list
->count
);
161 /* Untie an interface address from its derived subnet list of addresses. */
163 if_subnet_delete (struct interface
*ifp
, struct connected
*ifc
)
165 struct route_node
*rn
;
166 struct zebra_if
*zebra_if
;
167 struct list
*addr_list
;
169 assert (ifp
&& ifp
->info
&& ifc
);
170 zebra_if
= ifp
->info
;
172 /* Get address derived subnet node. */
173 rn
= route_node_lookup (zebra_if
->ipv4_subnets
, ifc
->address
);
174 if (! (rn
&& rn
->info
))
176 route_unlock_node (rn
);
178 /* Untie address from subnet's address list. */
179 addr_list
= rn
->info
;
180 listnode_delete (addr_list
, ifc
);
181 route_unlock_node (rn
);
183 /* Return list element count, if not empty. */
184 if (addr_list
->count
)
186 /* If deleted address is primary, mark subsequent one as such and distribute. */
187 if (! CHECK_FLAG (ifc
->flags
, ZEBRA_IFA_SECONDARY
))
189 ifc
= (struct connected
*) addr_list
->head
->data
;
190 zebra_interface_address_delete_update (ifp
, ifc
);
191 UNSET_FLAG (ifc
->flags
, ZEBRA_IFA_SECONDARY
);
192 zebra_interface_address_add_update (ifp
, ifc
);
195 return addr_list
->count
;
198 /* Otherwise, free list and route node. */
199 list_free (addr_list
);
201 route_unlock_node (rn
);
206 /* Wake up configured address if it is not in current kernel
209 if_addr_wakeup (struct interface
*ifp
)
211 struct listnode
*node
;
212 struct connected
*ifc
;
216 for (node
= listhead (ifp
->connected
); node
; nextnode (node
))
218 ifc
= getdata (node
);
221 if (CHECK_FLAG (ifc
->conf
, ZEBRA_IFC_CONFIGURED
)
222 && ! CHECK_FLAG (ifc
->conf
, ZEBRA_IFC_REAL
))
225 if (p
->family
== AF_INET
)
227 if (! if_is_up (ifp
))
229 if_set_flags (ifp
, IFF_UP
| IFF_RUNNING
);
233 ret
= if_set_prefix (ifp
, ifc
);
236 zlog_warn ("Can't set interface's address: %s",
241 /* Add to subnet chain list. */
242 if_subnet_add (ifp
, ifc
);
244 SET_FLAG (ifc
->conf
, ZEBRA_IFC_REAL
);
246 zebra_interface_address_add_update (ifp
, ifc
);
248 if (if_is_operative(ifp
))
249 connected_up_ipv4 (ifp
, ifc
);
252 if (p
->family
== AF_INET6
)
254 if (! if_is_up (ifp
))
256 if_set_flags (ifp
, IFF_UP
| IFF_RUNNING
);
260 ret
= if_prefix_add_ipv6 (ifp
, ifc
);
263 zlog_warn ("Can't set interface's address: %s",
267 SET_FLAG (ifc
->conf
, ZEBRA_IFC_REAL
);
269 zebra_interface_address_add_update (ifp
, ifc
);
271 if (if_is_operative(ifp
))
272 connected_up_ipv6 (ifp
, ifc
);
274 #endif /* HAVE_IPV6 */
279 /* Handle interface addition */
281 if_add_update (struct interface
*ifp
)
283 struct zebra_if
*if_data
;
286 if (if_data
->multicast
== IF_ZEBRA_MULTICAST_ON
)
287 if_set_flags (ifp
, IFF_MULTICAST
);
288 else if (if_data
->multicast
== IF_ZEBRA_MULTICAST_OFF
)
289 if_unset_flags (ifp
, IFF_MULTICAST
);
291 zebra_interface_add_update (ifp
);
293 if (! CHECK_FLAG (ifp
->status
, ZEBRA_INTERFACE_ACTIVE
))
295 SET_FLAG (ifp
->status
, ZEBRA_INTERFACE_ACTIVE
);
297 if_addr_wakeup (ifp
);
299 if (IS_ZEBRA_DEBUG_KERNEL
)
300 zlog_info ("interface %s index %d becomes active.",
301 ifp
->name
, ifp
->ifindex
);
305 if (IS_ZEBRA_DEBUG_KERNEL
)
306 zlog_info ("interface %s index %d is added.", ifp
->name
, ifp
->ifindex
);
311 /* Handle an interface delete event
313 * This function is only called when support for
314 * RTM_IFANNOUNCE or AF_NETLINK sockets (RTM_DELLINK message)
315 * is available. It is not called on, eg, Solaris.
317 #if (defined(RTM_IFANNOUNCE) || defined(HAVE_NETLINK))
319 if_delete_update (struct interface
*ifp
)
321 struct listnode
*node
;
322 struct listnode
*next
;
323 struct listnode
*first
;
324 struct listnode
*last
;
325 struct connected
*ifc
;
327 struct route_node
*rn
;
328 struct zebra_if
*zebra_if
;
329 struct list
*addr_list
;
331 zebra_if
= ifp
->info
;
335 zlog_err ("interface %s index %d is still up while being deleted.",
336 ifp
->name
, ifp
->ifindex
);
340 /* Mark interface as inactive */
341 UNSET_FLAG (ifp
->status
, ZEBRA_INTERFACE_ACTIVE
);
343 if (IS_ZEBRA_DEBUG_KERNEL
)
344 zlog_info ("interface %s index %d is now inactive.",
345 ifp
->name
, ifp
->ifindex
);
347 /* Delete connected routes from the kernel. */
351 while ((node
= (last
? last
->next
: listhead (ifp
->connected
))))
353 ifc
= getdata (node
);
356 if (p
->family
== AF_INET
)
358 rn
= route_node_lookup (zebra_if
->ipv4_subnets
, p
);
359 route_unlock_node (rn
);
360 addr_list
= (struct list
*) rn
->info
;
362 /* Remove addresses, secondaries first. */
363 first
= listhead (addr_list
);
364 for (node
= first
->next
; node
|| first
; node
= next
)
373 ifc
= getdata (node
);
376 connected_down_ipv4 (ifp
, ifc
);
378 zebra_interface_address_delete_update (ifp
, ifc
);
380 UNSET_FLAG (ifc
->conf
, ZEBRA_IFC_REAL
);
382 /* Remove from subnet chain. */
383 list_delete_node (addr_list
, node
);
384 route_unlock_node (rn
);
386 /* Remove from interface address list (unconditionally). */
387 listnode_delete (ifp
->connected
, ifc
);
388 connected_free (ifc
);
391 /* Free chain list and respective route node. */
392 list_delete (addr_list
);
394 route_unlock_node (rn
);
397 else if (p
->family
== AF_INET6
)
399 connected_down_ipv6 (ifp
, ifc
);
401 zebra_interface_address_delete_update (ifp
, ifc
);
403 UNSET_FLAG (ifc
->conf
, ZEBRA_IFC_REAL
);
405 if (CHECK_FLAG (ifc
->conf
, ZEBRA_IFC_CONFIGURED
))
409 listnode_delete (ifp
->connected
, ifc
);
410 connected_free (ifc
);
413 #endif /* HAVE_IPV6 */
416 zebra_interface_delete_update (ifp
);
418 #endif /* (defined(RTM_IFANNOUNCE) || defined(HAVE_NETLINK) */
420 /* Interface is up. */
422 if_up (struct interface
*ifp
)
424 struct listnode
*node
;
425 struct listnode
*next
;
426 struct connected
*ifc
;
429 /* Notify the protocol daemons. */
430 zebra_interface_up_update (ifp
);
432 /* Install connected routes to the kernel. */
435 for (node
= listhead (ifp
->connected
); node
; node
= next
)
438 ifc
= getdata (node
);
441 if (p
->family
== AF_INET
)
442 connected_up_ipv4 (ifp
, ifc
);
444 else if (p
->family
== AF_INET6
)
445 connected_up_ipv6 (ifp
, ifc
);
446 #endif /* HAVE_IPV6 */
450 /* Examine all static routes. */
454 /* Interface goes down. We have to manage different behavior of based
457 if_down (struct interface
*ifp
)
459 struct listnode
*node
;
460 struct listnode
*next
;
461 struct connected
*ifc
;
464 /* Notify to the protocol daemons. */
465 zebra_interface_down_update (ifp
);
467 /* Delete connected routes from the kernel. */
470 for (node
= listhead (ifp
->connected
); node
; node
= next
)
473 ifc
= getdata (node
);
476 if (p
->family
== AF_INET
)
477 connected_down_ipv4 (ifp
, ifc
);
479 else if (p
->family
== AF_INET6
)
480 connected_down_ipv6 (ifp
, ifc
);
481 #endif /* HAVE_IPV6 */
485 /* Examine all static routes which direct to the interface. */
490 if_refresh (struct interface
*ifp
)
492 if (if_is_operative (ifp
))
495 if (! if_is_operative (ifp
))
501 if (if_is_operative (ifp
))
506 /* Printout flag information into vty */
508 if_flag_dump_vty (struct vty
*vty
, unsigned long flag
)
512 #define IFF_OUT_VTY(X, Y) \
513 if ((X) && (flag & (X))) \
516 vty_out (vty, ","); \
523 IFF_OUT_VTY (IFF_UP
, "UP");
524 IFF_OUT_VTY (IFF_BROADCAST
, "BROADCAST");
525 IFF_OUT_VTY (IFF_DEBUG
, "DEBUG");
526 IFF_OUT_VTY (IFF_LOOPBACK
, "LOOPBACK");
527 IFF_OUT_VTY (IFF_POINTOPOINT
, "POINTOPOINT");
528 IFF_OUT_VTY (IFF_NOTRAILERS
, "NOTRAILERS");
529 IFF_OUT_VTY (IFF_RUNNING
, "RUNNING");
530 IFF_OUT_VTY (IFF_NOARP
, "NOARP");
531 IFF_OUT_VTY (IFF_PROMISC
, "PROMISC");
532 IFF_OUT_VTY (IFF_ALLMULTI
, "ALLMULTI");
533 IFF_OUT_VTY (IFF_OACTIVE
, "OACTIVE");
534 IFF_OUT_VTY (IFF_SIMPLEX
, "SIMPLEX");
535 IFF_OUT_VTY (IFF_LINK0
, "LINK0");
536 IFF_OUT_VTY (IFF_LINK1
, "LINK1");
537 IFF_OUT_VTY (IFF_LINK2
, "LINK2");
538 IFF_OUT_VTY (IFF_MULTICAST
, "MULTICAST");
540 IFF_OUT_VTY (IFF_IPV4
, "IFF_IPv4");
541 IFF_OUT_VTY (IFF_IPV6
, "IFF_IPv6");
542 #endif /* SOLARIS_IPV6 */
546 /* Output prefix string to vty. */
548 prefix_vty_out (struct vty
*vty
, struct prefix
*p
)
550 char str
[INET6_ADDRSTRLEN
];
552 inet_ntop (p
->family
, &p
->u
.prefix
, str
, sizeof (str
));
553 vty_out (vty
, "%s", str
);
557 /* Dump if address information to vty. */
559 connected_dump_vty (struct vty
*vty
, struct connected
*connected
)
562 struct interface
*ifp
;
564 /* Set interface pointer. */
565 ifp
= connected
->ifp
;
567 /* Print interface address. */
568 p
= connected
->address
;
569 vty_out (vty
, " %s ", prefix_family_str (p
));
570 prefix_vty_out (vty
, p
);
571 vty_out (vty
, "/%d", p
->prefixlen
);
573 /* If there is destination address, print it. */
574 p
= connected
->destination
;
577 if (p
->family
== AF_INET
)
578 if (ifp
->flags
& IFF_BROADCAST
)
580 vty_out (vty
, " broadcast ");
581 prefix_vty_out (vty
, p
);
584 if (ifp
->flags
& IFF_POINTOPOINT
)
586 vty_out (vty
, " pointopoint ");
587 prefix_vty_out (vty
, p
);
591 if (CHECK_FLAG (connected
->flags
, ZEBRA_IFA_SECONDARY
))
592 vty_out (vty
, " secondary");
594 if (connected
->label
)
595 vty_out (vty
, " %s", connected
->label
);
597 vty_out (vty
, "%s", VTY_NEWLINE
);
601 /* Dump interface ND information to vty. */
603 nd_dump_vty (struct vty
*vty
, struct interface
*ifp
)
605 struct zebra_if
*zif
;
606 struct rtadvconf
*rtadv
;
608 zif
= (struct zebra_if
*) ifp
->info
;
611 if (rtadv
->AdvSendAdvertisements
)
613 vty_out (vty
, " ND advertised reachable time is %d milliseconds%s",
614 rtadv
->AdvReachableTime
, VTY_NEWLINE
);
615 vty_out (vty
, " ND advertised retransmit interval is %d milliseconds%s",
616 rtadv
->AdvRetransTimer
, VTY_NEWLINE
);
617 vty_out (vty
, " ND router advertisements are sent every %d seconds%s",
618 rtadv
->MaxRtrAdvInterval
, VTY_NEWLINE
);
619 vty_out (vty
, " ND router advertisements live for %d seconds%s",
620 rtadv
->AdvDefaultLifetime
, VTY_NEWLINE
);
621 if (rtadv
->AdvManagedFlag
)
622 vty_out (vty
, " Hosts use DHCP to obtain routable addresses.%s",
625 vty_out (vty
, " Hosts use stateless autoconfig for addresses.%s",
631 /* Interface's information print out to vty interface. */
633 if_dump_vty (struct vty
*vty
, struct interface
*ifp
)
635 #ifdef HAVE_SOCKADDR_DL
636 struct sockaddr_dl
*sdl
;
637 #endif /* HAVE_SOCKADDR_DL */
638 struct connected
*connected
;
639 struct listnode
*node
;
640 struct route_node
*rn
;
641 struct zebra_if
*zebra_if
;
643 zebra_if
= ifp
->info
;
645 vty_out (vty
, "Interface %s is ", ifp
->name
);
647 vty_out (vty
, "up, line protocol ");
649 if (CHECK_FLAG(ifp
->status
, ZEBRA_INTERFACE_LINKDETECTION
)) {
650 if (if_is_running(ifp
))
651 vty_out (vty
, "is up%s", VTY_NEWLINE
);
653 vty_out (vty
, "is down%s", VTY_NEWLINE
);
655 vty_out (vty
, "detection is disabled%s", VTY_NEWLINE
);
658 vty_out (vty
, "down%s", VTY_NEWLINE
);
662 vty_out (vty
, " Description: %s%s", ifp
->desc
,
664 if (ifp
->ifindex
<= 0)
666 vty_out(vty
, " index %d pseudo interface%s", ifp
->ifindex
, VTY_NEWLINE
);
669 else if (! CHECK_FLAG (ifp
->status
, ZEBRA_INTERFACE_ACTIVE
))
671 vty_out(vty
, " index %d inactive interface%s",
677 vty_out (vty
, " index %d metric %d mtu %d ",
678 ifp
->ifindex
, ifp
->metric
, ifp
->mtu
);
679 if_flag_dump_vty (vty
, ifp
->flags
);
681 if (ifp
->mtu6
!= ifp
->mtu
)
682 vty_out (vty
, "mtu6 %d ", ifp
->mtu6
);
685 vty_out (vty
, "%s", VTY_NEWLINE
);
687 /* Hardware address. */
688 #ifdef HAVE_SOCKADDR_DL
690 if (sdl
!= NULL
&& sdl
->sdl_alen
!= 0)
695 vty_out (vty
, " HWaddr: ");
696 for (i
= 0, ptr
= (u_char
*)LLADDR (sdl
); i
< sdl
->sdl_alen
; i
++, ptr
++)
697 vty_out (vty
, "%s%02x", i
== 0 ? "" : ":", *ptr
);
698 vty_out (vty
, "%s", VTY_NEWLINE
);
701 if (ifp
->hw_addr_len
!= 0)
705 vty_out (vty
, " HWaddr: ");
706 for (i
= 0; i
< ifp
->hw_addr_len
; i
++)
707 vty_out (vty
, "%s%02x", i
== 0 ? "" : ":", ifp
->hw_addr
[i
]);
708 vty_out (vty
, "%s", VTY_NEWLINE
);
710 #endif /* HAVE_SOCKADDR_DL */
712 /* Bandwidth in kbps */
713 if (ifp
->bandwidth
!= 0)
715 vty_out(vty
, " bandwidth %u kbps", ifp
->bandwidth
);
716 vty_out(vty
, "%s", VTY_NEWLINE
);
719 for (rn
= route_top (zebra_if
->ipv4_subnets
); rn
; rn
= route_next (rn
))
724 for (node
= listhead ((struct list
*) rn
->info
); node
; nextnode (node
))
726 connected
= getdata (node
);
727 connected_dump_vty (vty
, connected
);
731 for (node
= listhead (ifp
->connected
); node
; nextnode (node
))
733 connected
= getdata (node
);
734 if (CHECK_FLAG (connected
->conf
, ZEBRA_IFC_REAL
) &&
735 (connected
->address
->family
== AF_INET6
))
736 connected_dump_vty (vty
, connected
);
740 nd_dump_vty (vty
, ifp
);
743 #ifdef HAVE_PROC_NET_DEV
744 /* Statistics print out using proc file system. */
745 vty_out (vty
, " input packets %lu, bytes %lu, dropped %lu,"
746 " multicast packets %lu%s",
747 ifp
->stats
.rx_packets
, ifp
->stats
.rx_bytes
,
748 ifp
->stats
.rx_dropped
, ifp
->stats
.rx_multicast
, VTY_NEWLINE
);
750 vty_out (vty
, " input errors %lu, length %lu, overrun %lu,"
751 " CRC %lu, frame %lu, fifo %lu, missed %lu%s",
752 ifp
->stats
.rx_errors
, ifp
->stats
.rx_length_errors
,
753 ifp
->stats
.rx_over_errors
, ifp
->stats
.rx_crc_errors
,
754 ifp
->stats
.rx_frame_errors
, ifp
->stats
.rx_fifo_errors
,
755 ifp
->stats
.rx_missed_errors
, VTY_NEWLINE
);
757 vty_out (vty
, " output packets %lu, bytes %lu, dropped %lu%s",
758 ifp
->stats
.tx_packets
, ifp
->stats
.tx_bytes
,
759 ifp
->stats
.tx_dropped
, VTY_NEWLINE
);
761 vty_out (vty
, " output errors %lu, aborted %lu, carrier %lu,"
762 " fifo %lu, heartbeat %lu, window %lu%s",
763 ifp
->stats
.tx_errors
, ifp
->stats
.tx_aborted_errors
,
764 ifp
->stats
.tx_carrier_errors
, ifp
->stats
.tx_fifo_errors
,
765 ifp
->stats
.tx_heartbeat_errors
, ifp
->stats
.tx_window_errors
,
768 vty_out (vty
, " collisions %lu%s", ifp
->stats
.collisions
, VTY_NEWLINE
);
769 #endif /* HAVE_PROC_NET_DEV */
771 #ifdef HAVE_NET_RT_IFLIST
772 #if defined (__bsdi__) || defined (__NetBSD__)
773 /* Statistics print out using sysctl (). */
774 vty_out (vty
, " input packets %qu, bytes %qu, dropped %qu,"
775 " multicast packets %qu%s",
776 ifp
->stats
.ifi_ipackets
, ifp
->stats
.ifi_ibytes
,
777 ifp
->stats
.ifi_iqdrops
, ifp
->stats
.ifi_imcasts
,
780 vty_out (vty
, " input errors %qu%s",
781 ifp
->stats
.ifi_ierrors
, VTY_NEWLINE
);
783 vty_out (vty
, " output packets %qu, bytes %qu, multicast packets %qu%s",
784 ifp
->stats
.ifi_opackets
, ifp
->stats
.ifi_obytes
,
785 ifp
->stats
.ifi_omcasts
, VTY_NEWLINE
);
787 vty_out (vty
, " output errors %qu%s",
788 ifp
->stats
.ifi_oerrors
, VTY_NEWLINE
);
790 vty_out (vty
, " collisions %qu%s",
791 ifp
->stats
.ifi_collisions
, VTY_NEWLINE
);
793 /* Statistics print out using sysctl (). */
794 vty_out (vty
, " input packets %lu, bytes %lu, dropped %lu,"
795 " multicast packets %lu%s",
796 ifp
->stats
.ifi_ipackets
, ifp
->stats
.ifi_ibytes
,
797 ifp
->stats
.ifi_iqdrops
, ifp
->stats
.ifi_imcasts
,
800 vty_out (vty
, " input errors %lu%s",
801 ifp
->stats
.ifi_ierrors
, VTY_NEWLINE
);
803 vty_out (vty
, " output packets %lu, bytes %lu, multicast packets %lu%s",
804 ifp
->stats
.ifi_opackets
, ifp
->stats
.ifi_obytes
,
805 ifp
->stats
.ifi_omcasts
, VTY_NEWLINE
);
807 vty_out (vty
, " output errors %lu%s",
808 ifp
->stats
.ifi_oerrors
, VTY_NEWLINE
);
810 vty_out (vty
, " collisions %lu%s",
811 ifp
->stats
.ifi_collisions
, VTY_NEWLINE
);
812 #endif /* __bsdi__ || __NetBSD__ */
813 #endif /* HAVE_NET_RT_IFLIST */
816 /* Check supported address family. */
818 if_supported_family (int family
)
820 if (family
== AF_INET
)
823 if (family
== AF_INET6
)
825 #endif /* HAVE_IPV6 */
829 /* Wrapper hook point for zebra daemon so that ifindex can be set
830 * DEFUN macro not used as extract.pl HAS to ignore this
831 * See also interface_cmd in lib/if.c
833 DEFUN_NOSH (zebra_interface
,
836 "Select an interface to configure\n"
837 "Interface's name\n")
840 struct interface
* ifp
;
842 /* Call lib interface() */
843 ret
= interface_cmd
.func (self
, vty
, argc
, argv
);
848 this only happens if interface is NOT in kernel */
849 if (ifp
->ifindex
== 0)
851 ifp
->ifindex
= if_new_intern_ifindex ();
852 UNSET_FLAG (ifp
->status
, ZEBRA_INTERFACE_ACTIVE
);
858 struct cmd_node interface_node
=
865 /* Show all or specified interface to vty. */
866 DEFUN (show_interface
, show_interface_cmd
,
867 "show interface [IFNAME]",
869 "Interface status and configuration\n"
872 struct listnode
*node
;
873 struct interface
*ifp
;
875 #ifdef HAVE_PROC_NET_DEV
876 /* If system has interface statistics via proc file system, update
878 ifstat_update_proc ();
879 #endif /* HAVE_PROC_NET_DEV */
880 #ifdef HAVE_NET_RT_IFLIST
881 ifstat_update_sysctl ();
882 #endif /* HAVE_NET_RT_IFLIST */
884 /* Specified interface print. */
887 ifp
= if_lookup_by_name (argv
[0]);
890 vty_out (vty
, "%% Can't find interface %s%s", argv
[0],
894 if_dump_vty (vty
, ifp
);
898 /* All interface print. */
899 for (node
= listhead (iflist
); node
; nextnode (node
))
900 if_dump_vty (vty
, getdata (node
));
908 "Set multicast flag to interface\n")
911 struct interface
*ifp
;
912 struct zebra_if
*if_data
;
914 ifp
= (struct interface
*) vty
->index
;
915 if (CHECK_FLAG (ifp
->status
, ZEBRA_INTERFACE_ACTIVE
))
917 ret
= if_set_flags (ifp
, IFF_MULTICAST
);
920 vty_out (vty
, "Can't set multicast flag%s", VTY_NEWLINE
);
926 if_data
->multicast
= IF_ZEBRA_MULTICAST_ON
;
935 "Unset multicast flag to interface\n")
938 struct interface
*ifp
;
939 struct zebra_if
*if_data
;
941 ifp
= (struct interface
*) vty
->index
;
942 if (CHECK_FLAG (ifp
->status
, ZEBRA_INTERFACE_ACTIVE
))
944 ret
= if_unset_flags (ifp
, IFF_MULTICAST
);
947 vty_out (vty
, "Can't unset multicast flag%s", VTY_NEWLINE
);
953 if_data
->multicast
= IF_ZEBRA_MULTICAST_OFF
;
961 "Enable link detection on interface\n")
963 struct interface
*ifp
;
964 int if_was_operative
;
966 ifp
= (struct interface
*) vty
->index
;
967 if_was_operative
= if_is_operative(ifp
);
968 SET_FLAG(ifp
->status
, ZEBRA_INTERFACE_LINKDETECTION
);
970 /* When linkdetection is enabled, if might come down */
971 if (!if_is_operative(ifp
) && if_was_operative
) if_down(ifp
);
973 /* FIXME: Will defer status change forwarding if interface
974 does not come down! */
980 DEFUN (no_linkdetect
,
984 "Disable link detection on interface\n")
986 struct interface
*ifp
;
987 int if_was_operative
;
989 ifp
= (struct interface
*) vty
->index
;
990 if_was_operative
= if_is_operative(ifp
);
991 UNSET_FLAG(ifp
->status
, ZEBRA_INTERFACE_LINKDETECTION
);
993 /* Interface may come up after disabling link detection */
994 if (if_is_operative(ifp
) && !if_was_operative
) if_up(ifp
);
996 /* FIXME: see linkdetect_cmd */
1004 "Shutdown the selected interface\n")
1007 struct interface
*ifp
;
1008 struct zebra_if
*if_data
;
1010 ifp
= (struct interface
*) vty
->index
;
1011 ret
= if_unset_flags (ifp
, IFF_UP
);
1014 vty_out (vty
, "Can't shutdown interface%s", VTY_NEWLINE
);
1018 if_data
= ifp
->info
;
1019 if_data
->shutdown
= IF_ZEBRA_SHUTDOWN_ON
;
1024 DEFUN (no_shutdown_if
,
1028 "Shutdown the selected interface\n")
1031 struct interface
*ifp
;
1032 struct zebra_if
*if_data
;
1034 ifp
= (struct interface
*) vty
->index
;
1035 ret
= if_set_flags (ifp
, IFF_UP
| IFF_RUNNING
);
1038 vty_out (vty
, "Can't up interface%s", VTY_NEWLINE
);
1042 if_data
= ifp
->info
;
1043 if_data
->shutdown
= IF_ZEBRA_SHUTDOWN_OFF
;
1048 DEFUN (bandwidth_if
,
1050 "bandwidth <1-10000000>",
1051 "Set bandwidth informational parameter\n"
1052 "Bandwidth in kilobits\n")
1054 struct interface
*ifp
;
1055 unsigned int bandwidth
;
1057 ifp
= (struct interface
*) vty
->index
;
1058 bandwidth
= strtol(argv
[0], NULL
, 10);
1060 /* bandwidth range is <1-10000000> */
1061 if (bandwidth
< 1 || bandwidth
> 10000000)
1063 vty_out (vty
, "Bandwidth is invalid%s", VTY_NEWLINE
);
1067 ifp
->bandwidth
= bandwidth
;
1069 /* force protocols to recalculate routes due to cost change */
1070 if (if_is_operative (ifp
))
1071 zebra_interface_up_update (ifp
);
1076 DEFUN (no_bandwidth_if
,
1077 no_bandwidth_if_cmd
,
1080 "Set bandwidth informational parameter\n")
1082 struct interface
*ifp
;
1084 ifp
= (struct interface
*) vty
->index
;
1088 /* force protocols to recalculate routes due to cost change */
1089 if (if_is_operative (ifp
))
1090 zebra_interface_up_update (ifp
);
1095 ALIAS (no_bandwidth_if
,
1096 no_bandwidth_if_val_cmd
,
1097 "no bandwidth <1-10000000>",
1099 "Set bandwidth informational parameter\n"
1100 "Bandwidth in kilobits\n")
1103 ip_address_install (struct vty
*vty
, struct interface
*ifp
,
1104 const char *addr_str
, const char *peer_str
,
1107 struct prefix_ipv4 cp
;
1108 struct connected
*ifc
;
1109 struct prefix_ipv4
*p
;
1110 struct in_addr mask
;
1113 ret
= str2prefix_ipv4 (addr_str
, &cp
);
1116 vty_out (vty
, "%% Malformed address %s", VTY_NEWLINE
);
1120 ifc
= connected_check_ipv4 (ifp
, (struct prefix
*) &cp
);
1123 ifc
= connected_new ();
1127 p
= prefix_ipv4_new ();
1129 ifc
->address
= (struct prefix
*) p
;
1132 if (p
->prefixlen
<= 30)
1134 p
= prefix_ipv4_new ();
1136 masklen2ip (p
->prefixlen
, &mask
);
1137 p
->prefix
.s_addr
|= ~mask
.s_addr
;
1138 ifc
->destination
= (struct prefix
*) p
;
1143 ifc
->label
= strdup (label
);
1145 /* Add to linked list. */
1146 listnode_add (ifp
->connected
, ifc
);
1149 /* This address is configured from zebra. */
1150 if (! CHECK_FLAG (ifc
->conf
, ZEBRA_IFC_CONFIGURED
))
1151 SET_FLAG (ifc
->conf
, ZEBRA_IFC_CONFIGURED
);
1153 /* In case of this route need to install kernel. */
1154 if (! CHECK_FLAG (ifc
->conf
, ZEBRA_IFC_REAL
)
1155 && CHECK_FLAG (ifp
->status
, ZEBRA_INTERFACE_ACTIVE
))
1157 /* Some system need to up the interface to set IP address. */
1158 if (! if_is_up (ifp
))
1160 if_set_flags (ifp
, IFF_UP
| IFF_RUNNING
);
1164 ret
= if_set_prefix (ifp
, ifc
);
1167 vty_out (vty
, "%% Can't set interface IP address: %s.%s",
1168 strerror(errno
), VTY_NEWLINE
);
1172 /* Add to subnet chain list (while marking secondary attribute). */
1173 if_subnet_add (ifp
, ifc
);
1175 /* IP address propery set. */
1176 SET_FLAG (ifc
->conf
, ZEBRA_IFC_REAL
);
1178 /* Update interface address information to protocol daemon. */
1179 zebra_interface_address_add_update (ifp
, ifc
);
1181 /* If interface is up register connected route. */
1182 if (if_is_operative(ifp
))
1183 connected_up_ipv4 (ifp
, ifc
);
1190 ip_address_uninstall (struct vty
*vty
, struct interface
*ifp
,
1191 const char *addr_str
, const char *peer_str
,
1194 struct prefix_ipv4 cp
;
1195 struct connected
*ifc
;
1198 /* Convert to prefix structure. */
1199 ret
= str2prefix_ipv4 (addr_str
, &cp
);
1202 vty_out (vty
, "%% Malformed address %s", VTY_NEWLINE
);
1206 /* Check current interface address. */
1207 ifc
= connected_check_ipv4 (ifp
, (struct prefix
*) &cp
);
1210 vty_out (vty
, "%% Can't find address%s", VTY_NEWLINE
);
1214 /* This is not configured address. */
1215 if (! CHECK_FLAG (ifc
->conf
, ZEBRA_IFC_CONFIGURED
))
1218 /* This is not real address or interface is not active. */
1219 if (! CHECK_FLAG (ifc
->conf
, ZEBRA_IFC_REAL
)
1220 || ! CHECK_FLAG (ifp
->status
, ZEBRA_INTERFACE_ACTIVE
))
1222 listnode_delete (ifp
->connected
, ifc
);
1223 connected_free (ifc
);
1227 /* This is real route. */
1228 ret
= if_unset_prefix (ifp
, ifc
);
1231 vty_out (vty
, "%% Can't unset interface IP address: %s.%s",
1232 strerror(errno
), VTY_NEWLINE
);
1237 /* Redistribute this information. */
1238 zebra_interface_address_delete_update (ifp
, ifc
);
1240 /* Remove connected route. */
1241 connected_down_ipv4 (ifp
, ifc
);
1243 /* Free address information. */
1244 listnode_delete (ifp
->connected
, ifc
);
1245 connected_free (ifc
);
1253 "ip address A.B.C.D/M",
1254 "Interface Internet Protocol config commands\n"
1255 "Set the IP address of an interface\n"
1256 "IP address (e.g. 10.0.0.1/8)\n")
1258 return ip_address_install (vty
, vty
->index
, argv
[0], NULL
, NULL
);
1261 DEFUN (no_ip_address
,
1263 "no ip address A.B.C.D/M",
1265 "Interface Internet Protocol config commands\n"
1266 "Set the IP address of an interface\n"
1267 "IP Address (e.g. 10.0.0.1/8)")
1269 return ip_address_uninstall (vty
, vty
->index
, argv
[0], NULL
, NULL
);
1273 DEFUN (ip_address_label
,
1274 ip_address_label_cmd
,
1275 "ip address A.B.C.D/M label LINE",
1276 "Interface Internet Protocol config commands\n"
1277 "Set the IP address of an interface\n"
1278 "IP address (e.g. 10.0.0.1/8)\n"
1279 "Label of this address\n"
1282 return ip_address_install (vty
, vty
->index
, argv
[0], NULL
, argv
[1]);
1285 DEFUN (no_ip_address_label
,
1286 no_ip_address_label_cmd
,
1287 "no ip address A.B.C.D/M label LINE",
1289 "Interface Internet Protocol config commands\n"
1290 "Set the IP address of an interface\n"
1291 "IP address (e.g. 10.0.0.1/8)\n"
1292 "Label of this address\n"
1295 return ip_address_uninstall (vty
, vty
->index
, argv
[0], NULL
, argv
[1]);
1297 #endif /* HAVE_NETLINK */
1301 ipv6_address_install (struct vty
*vty
, struct interface
*ifp
,
1302 const char *addr_str
, const char *peer_str
,
1303 const char *label
, int secondary
)
1305 struct prefix_ipv6 cp
;
1306 struct connected
*ifc
;
1307 struct prefix_ipv6
*p
;
1310 ret
= str2prefix_ipv6 (addr_str
, &cp
);
1313 vty_out (vty
, "%% Malformed address %s", VTY_NEWLINE
);
1317 ifc
= connected_check_ipv6 (ifp
, (struct prefix
*) &cp
);
1320 ifc
= connected_new ();
1324 p
= prefix_ipv6_new ();
1326 ifc
->address
= (struct prefix
*) p
;
1330 SET_FLAG (ifc
->flags
, ZEBRA_IFA_SECONDARY
);
1334 ifc
->label
= strdup (label
);
1336 /* Add to linked list. */
1337 listnode_add (ifp
->connected
, ifc
);
1340 /* This address is configured from zebra. */
1341 if (! CHECK_FLAG (ifc
->conf
, ZEBRA_IFC_CONFIGURED
))
1342 SET_FLAG (ifc
->conf
, ZEBRA_IFC_CONFIGURED
);
1344 /* In case of this route need to install kernel. */
1345 if (! CHECK_FLAG (ifc
->conf
, ZEBRA_IFC_REAL
)
1346 && CHECK_FLAG (ifp
->status
, ZEBRA_INTERFACE_ACTIVE
))
1348 /* Some system need to up the interface to set IP address. */
1349 if (! if_is_up (ifp
))
1351 if_set_flags (ifp
, IFF_UP
| IFF_RUNNING
);
1355 ret
= if_prefix_add_ipv6 (ifp
, ifc
);
1359 vty_out (vty
, "%% Can't set interface IP address: %s.%s",
1360 strerror(errno
), VTY_NEWLINE
);
1364 /* IP address propery set. */
1365 SET_FLAG (ifc
->conf
, ZEBRA_IFC_REAL
);
1367 /* Update interface address information to protocol daemon. */
1368 zebra_interface_address_add_update (ifp
, ifc
);
1370 /* If interface is up register connected route. */
1371 if (if_is_operative(ifp
))
1372 connected_up_ipv6 (ifp
, ifc
);
1379 ipv6_address_uninstall (struct vty
*vty
, struct interface
*ifp
,
1380 const char *addr_str
, const char *peer_str
,
1381 const char *label
, int secondry
)
1383 struct prefix_ipv6 cp
;
1384 struct connected
*ifc
;
1387 /* Convert to prefix structure. */
1388 ret
= str2prefix_ipv6 (addr_str
, &cp
);
1391 vty_out (vty
, "%% Malformed address %s", VTY_NEWLINE
);
1395 /* Check current interface address. */
1396 ifc
= connected_check_ipv6 (ifp
, (struct prefix
*) &cp
);
1399 vty_out (vty
, "%% Can't find address%s", VTY_NEWLINE
);
1403 /* This is not configured address. */
1404 if (! CHECK_FLAG (ifc
->conf
, ZEBRA_IFC_CONFIGURED
))
1407 /* This is not real address or interface is not active. */
1408 if (! CHECK_FLAG (ifc
->conf
, ZEBRA_IFC_REAL
)
1409 || ! CHECK_FLAG (ifp
->status
, ZEBRA_INTERFACE_ACTIVE
))
1411 listnode_delete (ifp
->connected
, ifc
);
1412 connected_free (ifc
);
1416 /* This is real route. */
1417 ret
= if_prefix_delete_ipv6 (ifp
, ifc
);
1420 vty_out (vty
, "%% Can't unset interface IP address: %s.%s",
1421 strerror(errno
), VTY_NEWLINE
);
1425 /* Redistribute this information. */
1426 zebra_interface_address_delete_update (ifp
, ifc
);
1428 /* Remove connected route. */
1429 connected_down_ipv6 (ifp
, ifc
);
1431 /* Free address information. */
1432 listnode_delete (ifp
->connected
, ifc
);
1433 connected_free (ifc
);
1438 DEFUN (ipv6_address
,
1440 "ipv6 address X:X::X:X/M",
1441 "Interface IPv6 config commands\n"
1442 "Set the IP address of an interface\n"
1443 "IPv6 address (e.g. 3ffe:506::1/48)\n")
1445 return ipv6_address_install (vty
, vty
->index
, argv
[0], NULL
, NULL
, 0);
1448 DEFUN (no_ipv6_address
,
1449 no_ipv6_address_cmd
,
1450 "no ipv6 address X:X::X:X/M",
1452 "Interface IPv6 config commands\n"
1453 "Set the IP address of an interface\n"
1454 "IPv6 address (e.g. 3ffe:506::1/48)\n")
1456 return ipv6_address_uninstall (vty
, vty
->index
, argv
[0], NULL
, NULL
, 0);
1458 #endif /* HAVE_IPV6 */
1463 "ip tunnel IP_address IP_address",
1464 "KAME ip tunneling configuration commands\n"
1465 "Set FROM IP address and TO IP address\n")
1470 DEFUN (no_ip_tunnel
, no_ip_tunnel_cmd
,
1473 "Set FROM IP address and TO IP address\n")
1480 if_config_write (struct vty
*vty
)
1482 struct listnode
*node
;
1483 struct interface
*ifp
;
1486 for (node
= listhead (iflist
); node
; nextnode (node
))
1488 struct zebra_if
*if_data
;
1489 struct listnode
*addrnode
;
1490 struct connected
*ifc
;
1493 ifp
= getdata (node
);
1494 if_data
= ifp
->info
;
1496 vty_out (vty
, "interface %s%s", ifp
->name
,
1500 vty_out (vty
, " description %s%s", ifp
->desc
,
1503 /* Assign bandwidth here to avoid unnecessary interface flap
1504 while processing config script */
1505 if (ifp
->bandwidth
!= 0)
1506 vty_out(vty
, " bandwidth %u%s", ifp
->bandwidth
, VTY_NEWLINE
);
1508 if (CHECK_FLAG(ifp
->status
, ZEBRA_INTERFACE_LINKDETECTION
))
1509 vty_out(vty
, " link-detect%s", VTY_NEWLINE
);
1511 for (addrnode
= listhead (ifp
->connected
); addrnode
; nextnode (addrnode
))
1513 ifc
= getdata (addrnode
);
1514 if (CHECK_FLAG (ifc
->conf
, ZEBRA_IFC_CONFIGURED
))
1517 vty_out (vty
, " ip%s address %s/%d",
1518 p
->family
== AF_INET
? "" : "v6",
1519 inet_ntop (p
->family
, &p
->u
.prefix
, buf
, BUFSIZ
),
1523 vty_out (vty
, " label %s", ifc
->label
);
1525 vty_out (vty
, "%s", VTY_NEWLINE
);
1531 if (if_data
->shutdown
== IF_ZEBRA_SHUTDOWN_ON
)
1532 vty_out (vty
, " shutdown%s", VTY_NEWLINE
);
1534 if (if_data
->multicast
!= IF_ZEBRA_MULTICAST_UNSPEC
)
1535 vty_out (vty
, " %smulticast%s",
1536 if_data
->multicast
== IF_ZEBRA_MULTICAST_ON
? "" : "no ",
1541 rtadv_config_write (vty
, ifp
);
1545 irdp_config_write (vty
, ifp
);
1548 vty_out (vty
, "!%s", VTY_NEWLINE
);
1553 /* Allocate and initialize interface vector. */
1557 /* Initialize interface and new hook. */
1559 if_add_hook (IF_NEW_HOOK
, if_zebra_new_hook
);
1560 if_add_hook (IF_DELETE_HOOK
, if_zebra_delete_hook
);
1562 /* Install configuration write function. */
1563 install_node (&interface_node
, if_config_write
);
1565 install_element (VIEW_NODE
, &show_interface_cmd
);
1566 install_element (ENABLE_NODE
, &show_interface_cmd
);
1567 install_element (CONFIG_NODE
, &zebra_interface_cmd
);
1568 install_element (CONFIG_NODE
, &no_interface_cmd
);
1569 install_default (INTERFACE_NODE
);
1570 install_element (INTERFACE_NODE
, &interface_desc_cmd
);
1571 install_element (INTERFACE_NODE
, &no_interface_desc_cmd
);
1572 install_element (INTERFACE_NODE
, &multicast_cmd
);
1573 install_element (INTERFACE_NODE
, &no_multicast_cmd
);
1574 install_element (INTERFACE_NODE
, &linkdetect_cmd
);
1575 install_element (INTERFACE_NODE
, &no_linkdetect_cmd
);
1576 install_element (INTERFACE_NODE
, &shutdown_if_cmd
);
1577 install_element (INTERFACE_NODE
, &no_shutdown_if_cmd
);
1578 install_element (INTERFACE_NODE
, &bandwidth_if_cmd
);
1579 install_element (INTERFACE_NODE
, &no_bandwidth_if_cmd
);
1580 install_element (INTERFACE_NODE
, &no_bandwidth_if_val_cmd
);
1581 install_element (INTERFACE_NODE
, &ip_address_cmd
);
1582 install_element (INTERFACE_NODE
, &no_ip_address_cmd
);
1584 install_element (INTERFACE_NODE
, &ipv6_address_cmd
);
1585 install_element (INTERFACE_NODE
, &no_ipv6_address_cmd
);
1586 #endif /* HAVE_IPV6 */
1588 install_element (INTERFACE_NODE
, &ip_tunnel_cmd
);
1589 install_element (INTERFACE_NODE
, &no_ip_tunnel_cmd
);
1592 install_element (INTERFACE_NODE
, &ip_address_label_cmd
);
1593 install_element (INTERFACE_NODE
, &no_ip_address_label_cmd
);
1594 #endif /* HAVE_NETLINK */