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
= (struct connected
*) addr_list
->head
->data
;
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
;
196 struct connected
*ifc
;
200 for (node
= listhead (ifp
->connected
); node
; nextnode (node
))
202 ifc
= getdata (node
);
205 if (CHECK_FLAG (ifc
->conf
, ZEBRA_IFC_CONFIGURED
)
206 && ! CHECK_FLAG (ifc
->conf
, ZEBRA_IFC_REAL
))
209 if (p
->family
== AF_INET
)
211 if (! if_is_up (ifp
))
213 if_set_flags (ifp
, IFF_UP
| IFF_RUNNING
);
217 ret
= if_set_prefix (ifp
, ifc
);
220 zlog_warn ("Can't set interface's address: %s",
221 safe_strerror(errno
));
225 /* Add to subnet chain list. */
226 if_subnet_add (ifp
, ifc
);
228 SET_FLAG (ifc
->conf
, ZEBRA_IFC_REAL
);
230 zebra_interface_address_add_update (ifp
, ifc
);
232 if (if_is_operative(ifp
))
233 connected_up_ipv4 (ifp
, ifc
);
236 if (p
->family
== AF_INET6
)
238 if (! if_is_up (ifp
))
240 if_set_flags (ifp
, IFF_UP
| IFF_RUNNING
);
244 ret
= if_prefix_add_ipv6 (ifp
, ifc
);
247 zlog_warn ("Can't set interface's address: %s",
248 safe_strerror(errno
));
251 SET_FLAG (ifc
->conf
, ZEBRA_IFC_REAL
);
253 zebra_interface_address_add_update (ifp
, ifc
);
255 if (if_is_operative(ifp
))
256 connected_up_ipv6 (ifp
, ifc
);
258 #endif /* HAVE_IPV6 */
263 /* Handle interface addition */
265 if_add_update (struct interface
*ifp
)
267 struct zebra_if
*if_data
;
270 if (if_data
->multicast
== IF_ZEBRA_MULTICAST_ON
)
271 if_set_flags (ifp
, IFF_MULTICAST
);
272 else if (if_data
->multicast
== IF_ZEBRA_MULTICAST_OFF
)
273 if_unset_flags (ifp
, IFF_MULTICAST
);
275 zebra_interface_add_update (ifp
);
277 if (! CHECK_FLAG (ifp
->status
, ZEBRA_INTERFACE_ACTIVE
))
279 SET_FLAG (ifp
->status
, ZEBRA_INTERFACE_ACTIVE
);
281 if_addr_wakeup (ifp
);
283 if (IS_ZEBRA_DEBUG_KERNEL
)
284 zlog_debug ("interface %s index %d becomes active.",
285 ifp
->name
, ifp
->ifindex
);
289 if (IS_ZEBRA_DEBUG_KERNEL
)
290 zlog_debug ("interface %s index %d is added.", ifp
->name
, ifp
->ifindex
);
295 /* Handle an interface delete event
297 * This function is only called when support for
298 * RTM_IFANNOUNCE or AF_NETLINK sockets (RTM_DELLINK message)
299 * is available. It is not called on, eg, Solaris.
301 #if (defined(RTM_IFANNOUNCE) || defined(HAVE_NETLINK))
303 if_delete_update (struct interface
*ifp
)
305 struct listnode
*node
;
306 struct listnode
*next
;
307 struct listnode
*first
;
308 struct listnode
*last
;
309 struct connected
*ifc
;
311 struct route_node
*rn
;
312 struct zebra_if
*zebra_if
;
313 struct list
*addr_list
;
315 zebra_if
= ifp
->info
;
319 zlog_err ("interface %s index %d is still up while being deleted.",
320 ifp
->name
, ifp
->ifindex
);
324 /* Mark interface as inactive */
325 UNSET_FLAG (ifp
->status
, ZEBRA_INTERFACE_ACTIVE
);
327 if (IS_ZEBRA_DEBUG_KERNEL
)
328 zlog_debug ("interface %s index %d is now inactive.",
329 ifp
->name
, ifp
->ifindex
);
331 /* Delete connected routes from the kernel. */
335 while ((node
= (last
? last
->next
: listhead (ifp
->connected
))))
337 ifc
= getdata (node
);
340 if (p
->family
== AF_INET
)
342 rn
= route_node_lookup (zebra_if
->ipv4_subnets
, p
);
343 route_unlock_node (rn
);
344 addr_list
= (struct list
*) rn
->info
;
346 /* Remove addresses, secondaries first. */
347 first
= listhead (addr_list
);
348 for (node
= first
->next
; node
|| first
; node
= next
)
357 ifc
= getdata (node
);
360 connected_down_ipv4 (ifp
, ifc
);
362 zebra_interface_address_delete_update (ifp
, ifc
);
364 UNSET_FLAG (ifc
->conf
, ZEBRA_IFC_REAL
);
366 /* Remove from subnet chain. */
367 list_delete_node (addr_list
, node
);
368 route_unlock_node (rn
);
370 /* Remove from interface address list (unconditionally). */
371 listnode_delete (ifp
->connected
, ifc
);
372 connected_free (ifc
);
375 /* Free chain list and respective route node. */
376 list_delete (addr_list
);
378 route_unlock_node (rn
);
381 else if (p
->family
== AF_INET6
)
383 connected_down_ipv6 (ifp
, ifc
);
385 zebra_interface_address_delete_update (ifp
, ifc
);
387 UNSET_FLAG (ifc
->conf
, ZEBRA_IFC_REAL
);
389 if (CHECK_FLAG (ifc
->conf
, ZEBRA_IFC_CONFIGURED
))
393 listnode_delete (ifp
->connected
, ifc
);
394 connected_free (ifc
);
397 #endif /* HAVE_IPV6 */
400 zebra_interface_delete_update (ifp
);
402 /* Update ifindex after distributing the delete message. This is in
403 case any client needs to have the old value of ifindex available
404 while processing the deletion. Each client daemon is responsible
405 for setting ifindex to IFINDEX_INTERNAL after processing the
406 interface deletion message. */
407 ifp
->ifindex
= IFINDEX_INTERNAL
;
409 #endif /* (defined(RTM_IFANNOUNCE) || defined(HAVE_NETLINK) */
411 /* Interface is up. */
413 if_up (struct interface
*ifp
)
415 struct listnode
*node
;
416 struct listnode
*next
;
417 struct connected
*ifc
;
420 /* Notify the protocol daemons. */
421 zebra_interface_up_update (ifp
);
423 /* Install connected routes to the kernel. */
426 for (node
= listhead (ifp
->connected
); node
; node
= next
)
429 ifc
= getdata (node
);
432 if (p
->family
== AF_INET
)
433 connected_up_ipv4 (ifp
, ifc
);
435 else if (p
->family
== AF_INET6
)
436 connected_up_ipv6 (ifp
, ifc
);
437 #endif /* HAVE_IPV6 */
441 /* Examine all static routes. */
445 /* Interface goes down. We have to manage different behavior of based
448 if_down (struct interface
*ifp
)
450 struct listnode
*node
;
451 struct listnode
*next
;
452 struct connected
*ifc
;
455 /* Notify to the protocol daemons. */
456 zebra_interface_down_update (ifp
);
458 /* Delete connected routes from the kernel. */
461 for (node
= listhead (ifp
->connected
); node
; node
= next
)
464 ifc
= getdata (node
);
467 if (p
->family
== AF_INET
)
468 connected_down_ipv4 (ifp
, ifc
);
470 else if (p
->family
== AF_INET6
)
471 connected_down_ipv6 (ifp
, ifc
);
472 #endif /* HAVE_IPV6 */
476 /* Examine all static routes which direct to the interface. */
481 if_refresh (struct interface
*ifp
)
483 if (if_is_operative (ifp
))
486 if (! if_is_operative (ifp
))
492 if (if_is_operative (ifp
))
497 /* Printout flag information into vty */
499 if_flag_dump_vty (struct vty
*vty
, unsigned long flag
)
503 #define IFF_OUT_VTY(X, Y) \
504 if ((X) && (flag & (X))) \
507 vty_out (vty, ","); \
514 IFF_OUT_VTY (IFF_UP
, "UP");
515 IFF_OUT_VTY (IFF_BROADCAST
, "BROADCAST");
516 IFF_OUT_VTY (IFF_DEBUG
, "DEBUG");
517 IFF_OUT_VTY (IFF_LOOPBACK
, "LOOPBACK");
518 IFF_OUT_VTY (IFF_POINTOPOINT
, "POINTOPOINT");
519 IFF_OUT_VTY (IFF_NOTRAILERS
, "NOTRAILERS");
520 IFF_OUT_VTY (IFF_RUNNING
, "RUNNING");
521 IFF_OUT_VTY (IFF_NOARP
, "NOARP");
522 IFF_OUT_VTY (IFF_PROMISC
, "PROMISC");
523 IFF_OUT_VTY (IFF_ALLMULTI
, "ALLMULTI");
524 IFF_OUT_VTY (IFF_OACTIVE
, "OACTIVE");
525 IFF_OUT_VTY (IFF_SIMPLEX
, "SIMPLEX");
526 IFF_OUT_VTY (IFF_LINK0
, "LINK0");
527 IFF_OUT_VTY (IFF_LINK1
, "LINK1");
528 IFF_OUT_VTY (IFF_LINK2
, "LINK2");
529 IFF_OUT_VTY (IFF_MULTICAST
, "MULTICAST");
531 IFF_OUT_VTY (IFF_IPV4
, "IFF_IPv4");
532 IFF_OUT_VTY (IFF_IPV6
, "IFF_IPv6");
533 #endif /* SOLARIS_IPV6 */
537 /* Output prefix string to vty. */
539 prefix_vty_out (struct vty
*vty
, struct prefix
*p
)
541 char str
[INET6_ADDRSTRLEN
];
543 inet_ntop (p
->family
, &p
->u
.prefix
, str
, sizeof (str
));
544 vty_out (vty
, "%s", str
);
548 /* Dump if address information to vty. */
550 connected_dump_vty (struct vty
*vty
, struct connected
*connected
)
553 struct interface
*ifp
;
555 /* Set interface pointer. */
556 ifp
= connected
->ifp
;
558 /* Print interface address. */
559 p
= connected
->address
;
560 vty_out (vty
, " %s ", prefix_family_str (p
));
561 prefix_vty_out (vty
, p
);
562 vty_out (vty
, "/%d", p
->prefixlen
);
564 /* If there is destination address, print it. */
565 p
= connected
->destination
;
568 if (p
->family
== AF_INET
)
569 if (ifp
->flags
& IFF_BROADCAST
)
571 vty_out (vty
, " broadcast ");
572 prefix_vty_out (vty
, p
);
575 if (ifp
->flags
& IFF_POINTOPOINT
)
577 vty_out (vty
, " pointopoint ");
578 prefix_vty_out (vty
, p
);
582 if (CHECK_FLAG (connected
->flags
, ZEBRA_IFA_SECONDARY
))
583 vty_out (vty
, " secondary");
585 if (connected
->label
)
586 vty_out (vty
, " %s", connected
->label
);
588 vty_out (vty
, "%s", VTY_NEWLINE
);
592 /* Dump interface ND information to vty. */
594 nd_dump_vty (struct vty
*vty
, struct interface
*ifp
)
596 struct zebra_if
*zif
;
597 struct rtadvconf
*rtadv
;
600 zif
= (struct zebra_if
*) ifp
->info
;
603 if (rtadv
->AdvSendAdvertisements
)
605 vty_out (vty
, " ND advertised reachable time is %d milliseconds%s",
606 rtadv
->AdvReachableTime
, VTY_NEWLINE
);
607 vty_out (vty
, " ND advertised retransmit interval is %d milliseconds%s",
608 rtadv
->AdvRetransTimer
, VTY_NEWLINE
);
609 interval
= rtadv
->MaxRtrAdvInterval
;
611 vty_out (vty
, " ND router advertisements are sent every "
612 "%d milliseconds%s", interval
,
615 vty_out (vty
, " ND router advertisements are sent every "
616 "%d seconds%s", interval
/ 1000,
618 vty_out (vty
, " ND router advertisements live for %d seconds%s",
619 rtadv
->AdvDefaultLifetime
, VTY_NEWLINE
);
620 if (rtadv
->AdvManagedFlag
)
621 vty_out (vty
, " Hosts use DHCP to obtain routable addresses.%s",
624 vty_out (vty
, " Hosts use stateless autoconfig for addresses.%s",
626 if (rtadv
->AdvHomeAgentFlag
)
627 vty_out (vty
, " ND router advertisements with "
628 "Home Agent flag bit set.%s",
630 if (rtadv
->AdvIntervalOption
)
631 vty_out (vty
, " ND router advertisements with Adv. Interval option.%s",
637 /* Interface's information print out to vty interface. */
639 if_dump_vty (struct vty
*vty
, struct interface
*ifp
)
641 #ifdef HAVE_SOCKADDR_DL
642 struct sockaddr_dl
*sdl
;
643 #endif /* HAVE_SOCKADDR_DL */
644 struct connected
*connected
;
645 struct listnode
*node
;
646 struct route_node
*rn
;
647 struct zebra_if
*zebra_if
;
649 zebra_if
= ifp
->info
;
651 vty_out (vty
, "Interface %s is ", ifp
->name
);
653 vty_out (vty
, "up, line protocol ");
655 if (CHECK_FLAG(ifp
->status
, ZEBRA_INTERFACE_LINKDETECTION
)) {
656 if (if_is_running(ifp
))
657 vty_out (vty
, "is up%s", VTY_NEWLINE
);
659 vty_out (vty
, "is down%s", VTY_NEWLINE
);
661 vty_out (vty
, "detection is disabled%s", VTY_NEWLINE
);
664 vty_out (vty
, "down%s", VTY_NEWLINE
);
668 vty_out (vty
, " Description: %s%s", ifp
->desc
,
670 if (ifp
->ifindex
== IFINDEX_INTERNAL
)
672 vty_out(vty
, " pseudo interface%s", VTY_NEWLINE
);
675 else if (! CHECK_FLAG (ifp
->status
, ZEBRA_INTERFACE_ACTIVE
))
677 vty_out(vty
, " index %d inactive interface%s",
683 vty_out (vty
, " index %d metric %d mtu %d ",
684 ifp
->ifindex
, ifp
->metric
, ifp
->mtu
);
685 if_flag_dump_vty (vty
, ifp
->flags
);
687 if (ifp
->mtu6
!= ifp
->mtu
)
688 vty_out (vty
, "mtu6 %d ", ifp
->mtu6
);
691 vty_out (vty
, "%s", VTY_NEWLINE
);
693 /* Hardware address. */
694 #ifdef HAVE_SOCKADDR_DL
696 if (sdl
!= NULL
&& sdl
->sdl_alen
!= 0)
701 vty_out (vty
, " HWaddr: ");
702 for (i
= 0, ptr
= (u_char
*)LLADDR (sdl
); i
< sdl
->sdl_alen
; i
++, ptr
++)
703 vty_out (vty
, "%s%02x", i
== 0 ? "" : ":", *ptr
);
704 vty_out (vty
, "%s", VTY_NEWLINE
);
707 if (ifp
->hw_addr_len
!= 0)
711 vty_out (vty
, " HWaddr: ");
712 for (i
= 0; i
< ifp
->hw_addr_len
; i
++)
713 vty_out (vty
, "%s%02x", i
== 0 ? "" : ":", ifp
->hw_addr
[i
]);
714 vty_out (vty
, "%s", VTY_NEWLINE
);
716 #endif /* HAVE_SOCKADDR_DL */
718 /* Bandwidth in kbps */
719 if (ifp
->bandwidth
!= 0)
721 vty_out(vty
, " bandwidth %u kbps", ifp
->bandwidth
);
722 vty_out(vty
, "%s", VTY_NEWLINE
);
725 for (rn
= route_top (zebra_if
->ipv4_subnets
); rn
; rn
= route_next (rn
))
730 for (node
= listhead ((struct list
*) rn
->info
); node
; nextnode (node
))
732 connected
= getdata (node
);
733 connected_dump_vty (vty
, connected
);
737 for (node
= listhead (ifp
->connected
); node
; nextnode (node
))
739 connected
= getdata (node
);
740 if (CHECK_FLAG (connected
->conf
, ZEBRA_IFC_REAL
) &&
741 (connected
->address
->family
== AF_INET6
))
742 connected_dump_vty (vty
, connected
);
746 nd_dump_vty (vty
, ifp
);
749 #ifdef HAVE_PROC_NET_DEV
750 /* Statistics print out using proc file system. */
751 vty_out (vty
, " %lu input packets (%lu multicast), %lu bytes, "
753 ifp
->stats
.rx_packets
, ifp
->stats
.rx_multicast
,
754 ifp
->stats
.rx_bytes
, ifp
->stats
.rx_dropped
, VTY_NEWLINE
);
756 vty_out (vty
, " %lu input errors, %lu length, %lu overrun,"
757 " %lu CRC, %lu frame%s",
758 ifp
->stats
.rx_errors
, ifp
->stats
.rx_length_errors
,
759 ifp
->stats
.rx_over_errors
, ifp
->stats
.rx_crc_errors
,
760 ifp
->stats
.rx_frame_errors
, VTY_NEWLINE
);
762 vty_out (vty
, " %lu fifo, %lu missed%s", ifp
->stats
.rx_fifo_errors
,
763 ifp
->stats
.rx_missed_errors
, VTY_NEWLINE
);
765 vty_out (vty
, " %lu output packets, %lu bytes, %lu dropped%s",
766 ifp
->stats
.tx_packets
, ifp
->stats
.tx_bytes
,
767 ifp
->stats
.tx_dropped
, VTY_NEWLINE
);
769 vty_out (vty
, " %lu output errors, %lu aborted, %lu carrier,"
770 " %lu fifo, %lu heartbeat%s",
771 ifp
->stats
.tx_errors
, ifp
->stats
.tx_aborted_errors
,
772 ifp
->stats
.tx_carrier_errors
, ifp
->stats
.tx_fifo_errors
,
773 ifp
->stats
.tx_heartbeat_errors
, VTY_NEWLINE
);
775 vty_out (vty
, " %lu window, %lu collisions%s",
776 ifp
->stats
.tx_window_errors
, ifp
->stats
.collisions
, VTY_NEWLINE
);
777 #endif /* HAVE_PROC_NET_DEV */
779 #ifdef HAVE_NET_RT_IFLIST
780 #if defined (__bsdi__) || defined (__NetBSD__)
781 /* Statistics print out using sysctl (). */
782 vty_out (vty
, " input packets %qu, bytes %qu, dropped %qu,"
783 " multicast packets %qu%s",
784 ifp
->stats
.ifi_ipackets
, ifp
->stats
.ifi_ibytes
,
785 ifp
->stats
.ifi_iqdrops
, ifp
->stats
.ifi_imcasts
,
788 vty_out (vty
, " input errors %qu%s",
789 ifp
->stats
.ifi_ierrors
, VTY_NEWLINE
);
791 vty_out (vty
, " output packets %qu, bytes %qu, multicast packets %qu%s",
792 ifp
->stats
.ifi_opackets
, ifp
->stats
.ifi_obytes
,
793 ifp
->stats
.ifi_omcasts
, VTY_NEWLINE
);
795 vty_out (vty
, " output errors %qu%s",
796 ifp
->stats
.ifi_oerrors
, VTY_NEWLINE
);
798 vty_out (vty
, " collisions %qu%s",
799 ifp
->stats
.ifi_collisions
, VTY_NEWLINE
);
801 /* Statistics print out using sysctl (). */
802 vty_out (vty
, " input packets %lu, bytes %lu, dropped %lu,"
803 " multicast packets %lu%s",
804 ifp
->stats
.ifi_ipackets
, ifp
->stats
.ifi_ibytes
,
805 ifp
->stats
.ifi_iqdrops
, ifp
->stats
.ifi_imcasts
,
808 vty_out (vty
, " input errors %lu%s",
809 ifp
->stats
.ifi_ierrors
, VTY_NEWLINE
);
811 vty_out (vty
, " output packets %lu, bytes %lu, multicast packets %lu%s",
812 ifp
->stats
.ifi_opackets
, ifp
->stats
.ifi_obytes
,
813 ifp
->stats
.ifi_omcasts
, VTY_NEWLINE
);
815 vty_out (vty
, " output errors %lu%s",
816 ifp
->stats
.ifi_oerrors
, VTY_NEWLINE
);
818 vty_out (vty
, " collisions %lu%s",
819 ifp
->stats
.ifi_collisions
, VTY_NEWLINE
);
820 #endif /* __bsdi__ || __NetBSD__ */
821 #endif /* HAVE_NET_RT_IFLIST */
824 /* Check supported address family. */
826 if_supported_family (int family
)
828 if (family
== AF_INET
)
831 if (family
== AF_INET6
)
833 #endif /* HAVE_IPV6 */
837 /* Wrapper hook point for zebra daemon so that ifindex can be set
838 * DEFUN macro not used as extract.pl HAS to ignore this
839 * See also interface_cmd in lib/if.c
841 DEFUN_NOSH (zebra_interface
,
844 "Select an interface to configure\n"
845 "Interface's name\n")
848 struct interface
* ifp
;
850 /* Call lib interface() */
851 if ((ret
= interface_cmd
.func (self
, vty
, argc
, argv
)) != CMD_SUCCESS
)
856 if (ifp
->ifindex
== IFINDEX_INTERNAL
)
857 /* Is this really necessary? Shouldn't status be initialized to 0
859 UNSET_FLAG (ifp
->status
, ZEBRA_INTERFACE_ACTIVE
);
864 struct cmd_node interface_node
=
871 /* Show all or specified interface to vty. */
872 DEFUN (show_interface
, show_interface_cmd
,
873 "show interface [IFNAME]",
875 "Interface status and configuration\n"
878 struct listnode
*node
;
879 struct interface
*ifp
;
881 #ifdef HAVE_PROC_NET_DEV
882 /* If system has interface statistics via proc file system, update
884 ifstat_update_proc ();
885 #endif /* HAVE_PROC_NET_DEV */
886 #ifdef HAVE_NET_RT_IFLIST
887 ifstat_update_sysctl ();
888 #endif /* HAVE_NET_RT_IFLIST */
890 /* Specified interface print. */
893 ifp
= if_lookup_by_name (argv
[0]);
896 vty_out (vty
, "%% Can't find interface %s%s", argv
[0],
900 if_dump_vty (vty
, ifp
);
904 /* All interface print. */
905 for (node
= listhead (iflist
); node
; nextnode (node
))
906 if_dump_vty (vty
, getdata (node
));
911 DEFUN (show_interface_desc
,
912 show_interface_desc_cmd
,
913 "show interface description",
915 "Interface status and configuration\n"
916 "Interface description\n")
918 struct listnode
*node
;
919 struct interface
*ifp
;
921 vty_out (vty
, "Interface Status Protocol Description%s", VTY_NEWLINE
);
922 for (node
= listhead (iflist
); node
; nextnode (node
))
925 ifp
= getdata (node
);
927 len
= vty_out (vty
, "%s", ifp
->name
);
928 vty_out (vty
, "%*s", (16 - len
), " ");
932 vty_out (vty
, "up ");
933 if (CHECK_FLAG(ifp
->status
, ZEBRA_INTERFACE_LINKDETECTION
))
935 if (if_is_running(ifp
))
936 vty_out (vty
, "up ");
938 vty_out (vty
, "down ");
942 vty_out (vty
, "unknown ");
947 vty_out (vty
, "down down ");
951 vty_out (vty
, "%s", ifp
->desc
);
952 vty_out (vty
, "%s", VTY_NEWLINE
);
960 "Set multicast flag to interface\n")
963 struct interface
*ifp
;
964 struct zebra_if
*if_data
;
966 ifp
= (struct interface
*) vty
->index
;
967 if (CHECK_FLAG (ifp
->status
, ZEBRA_INTERFACE_ACTIVE
))
969 ret
= if_set_flags (ifp
, IFF_MULTICAST
);
972 vty_out (vty
, "Can't set multicast flag%s", VTY_NEWLINE
);
978 if_data
->multicast
= IF_ZEBRA_MULTICAST_ON
;
987 "Unset multicast flag to interface\n")
990 struct interface
*ifp
;
991 struct zebra_if
*if_data
;
993 ifp
= (struct interface
*) vty
->index
;
994 if (CHECK_FLAG (ifp
->status
, ZEBRA_INTERFACE_ACTIVE
))
996 ret
= if_unset_flags (ifp
, IFF_MULTICAST
);
999 vty_out (vty
, "Can't unset multicast flag%s", VTY_NEWLINE
);
1004 if_data
= ifp
->info
;
1005 if_data
->multicast
= IF_ZEBRA_MULTICAST_OFF
;
1013 "Enable link detection on interface\n")
1015 struct interface
*ifp
;
1016 int if_was_operative
;
1018 ifp
= (struct interface
*) vty
->index
;
1019 if_was_operative
= if_is_operative(ifp
);
1020 SET_FLAG(ifp
->status
, ZEBRA_INTERFACE_LINKDETECTION
);
1022 /* When linkdetection is enabled, if might come down */
1023 if (!if_is_operative(ifp
) && if_was_operative
) if_down(ifp
);
1025 /* FIXME: Will defer status change forwarding if interface
1026 does not come down! */
1032 DEFUN (no_linkdetect
,
1036 "Disable link detection on interface\n")
1038 struct interface
*ifp
;
1039 int if_was_operative
;
1041 ifp
= (struct interface
*) vty
->index
;
1042 if_was_operative
= if_is_operative(ifp
);
1043 UNSET_FLAG(ifp
->status
, ZEBRA_INTERFACE_LINKDETECTION
);
1045 /* Interface may come up after disabling link detection */
1046 if (if_is_operative(ifp
) && !if_was_operative
) if_up(ifp
);
1048 /* FIXME: see linkdetect_cmd */
1056 "Shutdown the selected interface\n")
1059 struct interface
*ifp
;
1060 struct zebra_if
*if_data
;
1062 ifp
= (struct interface
*) vty
->index
;
1063 ret
= if_unset_flags (ifp
, IFF_UP
);
1066 vty_out (vty
, "Can't shutdown interface%s", VTY_NEWLINE
);
1070 if_data
= ifp
->info
;
1071 if_data
->shutdown
= IF_ZEBRA_SHUTDOWN_ON
;
1076 DEFUN (no_shutdown_if
,
1080 "Shutdown the selected interface\n")
1083 struct interface
*ifp
;
1084 struct zebra_if
*if_data
;
1086 ifp
= (struct interface
*) vty
->index
;
1087 ret
= if_set_flags (ifp
, IFF_UP
| IFF_RUNNING
);
1090 vty_out (vty
, "Can't up interface%s", VTY_NEWLINE
);
1094 if_data
= ifp
->info
;
1095 if_data
->shutdown
= IF_ZEBRA_SHUTDOWN_OFF
;
1100 DEFUN (bandwidth_if
,
1102 "bandwidth <1-10000000>",
1103 "Set bandwidth informational parameter\n"
1104 "Bandwidth in kilobits\n")
1106 struct interface
*ifp
;
1107 unsigned int bandwidth
;
1109 ifp
= (struct interface
*) vty
->index
;
1110 bandwidth
= strtol(argv
[0], NULL
, 10);
1112 /* bandwidth range is <1-10000000> */
1113 if (bandwidth
< 1 || bandwidth
> 10000000)
1115 vty_out (vty
, "Bandwidth is invalid%s", VTY_NEWLINE
);
1119 ifp
->bandwidth
= bandwidth
;
1121 /* force protocols to recalculate routes due to cost change */
1122 if (if_is_operative (ifp
))
1123 zebra_interface_up_update (ifp
);
1128 DEFUN (no_bandwidth_if
,
1129 no_bandwidth_if_cmd
,
1132 "Set bandwidth informational parameter\n")
1134 struct interface
*ifp
;
1136 ifp
= (struct interface
*) vty
->index
;
1140 /* force protocols to recalculate routes due to cost change */
1141 if (if_is_operative (ifp
))
1142 zebra_interface_up_update (ifp
);
1147 ALIAS (no_bandwidth_if
,
1148 no_bandwidth_if_val_cmd
,
1149 "no bandwidth <1-10000000>",
1151 "Set bandwidth informational parameter\n"
1152 "Bandwidth in kilobits\n")
1155 ip_address_install (struct vty
*vty
, struct interface
*ifp
,
1156 const char *addr_str
, const char *peer_str
,
1159 struct prefix_ipv4 cp
;
1160 struct connected
*ifc
;
1161 struct prefix_ipv4
*p
;
1164 ret
= str2prefix_ipv4 (addr_str
, &cp
);
1167 vty_out (vty
, "%% Malformed address %s", VTY_NEWLINE
);
1171 ifc
= connected_check_ipv4 (ifp
, (struct prefix
*) &cp
);
1174 ifc
= connected_new ();
1178 p
= prefix_ipv4_new ();
1180 ifc
->address
= (struct prefix
*) p
;
1183 if (p
->prefixlen
<= IPV4_MAX_PREFIXLEN
-2)
1185 p
= prefix_ipv4_new ();
1187 p
->prefix
.s_addr
= ipv4_broadcast_addr(p
->prefix
.s_addr
,p
->prefixlen
);
1188 ifc
->destination
= (struct prefix
*) p
;
1193 ifc
->label
= strdup (label
);
1195 /* Add to linked list. */
1196 listnode_add (ifp
->connected
, ifc
);
1199 /* This address is configured from zebra. */
1200 if (! CHECK_FLAG (ifc
->conf
, ZEBRA_IFC_CONFIGURED
))
1201 SET_FLAG (ifc
->conf
, ZEBRA_IFC_CONFIGURED
);
1203 /* In case of this route need to install kernel. */
1204 if (! CHECK_FLAG (ifc
->conf
, ZEBRA_IFC_REAL
)
1205 && CHECK_FLAG (ifp
->status
, ZEBRA_INTERFACE_ACTIVE
))
1207 /* Some system need to up the interface to set IP address. */
1208 if (! if_is_up (ifp
))
1210 if_set_flags (ifp
, IFF_UP
| IFF_RUNNING
);
1214 ret
= if_set_prefix (ifp
, ifc
);
1217 vty_out (vty
, "%% Can't set interface IP address: %s.%s",
1218 safe_strerror(errno
), VTY_NEWLINE
);
1222 /* Add to subnet chain list (while marking secondary attribute). */
1223 if_subnet_add (ifp
, ifc
);
1225 /* IP address propery set. */
1226 SET_FLAG (ifc
->conf
, ZEBRA_IFC_REAL
);
1228 /* Update interface address information to protocol daemon. */
1229 zebra_interface_address_add_update (ifp
, ifc
);
1231 /* If interface is up register connected route. */
1232 if (if_is_operative(ifp
))
1233 connected_up_ipv4 (ifp
, ifc
);
1240 ip_address_uninstall (struct vty
*vty
, struct interface
*ifp
,
1241 const char *addr_str
, const char *peer_str
,
1244 struct prefix_ipv4 cp
;
1245 struct connected
*ifc
;
1248 /* Convert to prefix structure. */
1249 ret
= str2prefix_ipv4 (addr_str
, &cp
);
1252 vty_out (vty
, "%% Malformed address %s", VTY_NEWLINE
);
1256 /* Check current interface address. */
1257 ifc
= connected_check_ipv4 (ifp
, (struct prefix
*) &cp
);
1260 vty_out (vty
, "%% Can't find address%s", VTY_NEWLINE
);
1264 /* This is not configured address. */
1265 if (! CHECK_FLAG (ifc
->conf
, ZEBRA_IFC_CONFIGURED
))
1268 /* This is not real address or interface is not active. */
1269 if (! CHECK_FLAG (ifc
->conf
, ZEBRA_IFC_REAL
)
1270 || ! CHECK_FLAG (ifp
->status
, ZEBRA_INTERFACE_ACTIVE
))
1272 listnode_delete (ifp
->connected
, ifc
);
1273 connected_free (ifc
);
1277 /* This is real route. */
1278 ret
= if_unset_prefix (ifp
, ifc
);
1281 vty_out (vty
, "%% Can't unset interface IP address: %s.%s",
1282 safe_strerror(errno
), VTY_NEWLINE
);
1287 /* Redistribute this information. */
1288 zebra_interface_address_delete_update (ifp
, ifc
);
1290 /* Remove connected route. */
1291 connected_down_ipv4 (ifp
, ifc
);
1293 /* Free address information. */
1294 listnode_delete (ifp
->connected
, ifc
);
1295 connected_free (ifc
);
1303 "ip address A.B.C.D/M",
1304 "Interface Internet Protocol config commands\n"
1305 "Set the IP address of an interface\n"
1306 "IP address (e.g. 10.0.0.1/8)\n")
1308 return ip_address_install (vty
, vty
->index
, argv
[0], NULL
, NULL
);
1311 DEFUN (no_ip_address
,
1313 "no ip address A.B.C.D/M",
1315 "Interface Internet Protocol config commands\n"
1316 "Set the IP address of an interface\n"
1317 "IP Address (e.g. 10.0.0.1/8)")
1319 return ip_address_uninstall (vty
, vty
->index
, argv
[0], NULL
, NULL
);
1323 DEFUN (ip_address_label
,
1324 ip_address_label_cmd
,
1325 "ip address A.B.C.D/M label LINE",
1326 "Interface Internet Protocol config commands\n"
1327 "Set the IP address of an interface\n"
1328 "IP address (e.g. 10.0.0.1/8)\n"
1329 "Label of this address\n"
1332 return ip_address_install (vty
, vty
->index
, argv
[0], NULL
, argv
[1]);
1335 DEFUN (no_ip_address_label
,
1336 no_ip_address_label_cmd
,
1337 "no ip address A.B.C.D/M label LINE",
1339 "Interface Internet Protocol config commands\n"
1340 "Set the IP address of an interface\n"
1341 "IP address (e.g. 10.0.0.1/8)\n"
1342 "Label of this address\n"
1345 return ip_address_uninstall (vty
, vty
->index
, argv
[0], NULL
, argv
[1]);
1347 #endif /* HAVE_NETLINK */
1351 ipv6_address_install (struct vty
*vty
, struct interface
*ifp
,
1352 const char *addr_str
, const char *peer_str
,
1353 const char *label
, int secondary
)
1355 struct prefix_ipv6 cp
;
1356 struct connected
*ifc
;
1357 struct prefix_ipv6
*p
;
1360 ret
= str2prefix_ipv6 (addr_str
, &cp
);
1363 vty_out (vty
, "%% Malformed address %s", VTY_NEWLINE
);
1367 ifc
= connected_check_ipv6 (ifp
, (struct prefix
*) &cp
);
1370 ifc
= connected_new ();
1374 p
= prefix_ipv6_new ();
1376 ifc
->address
= (struct prefix
*) p
;
1380 SET_FLAG (ifc
->flags
, ZEBRA_IFA_SECONDARY
);
1384 ifc
->label
= strdup (label
);
1386 /* Add to linked list. */
1387 listnode_add (ifp
->connected
, ifc
);
1390 /* This address is configured from zebra. */
1391 if (! CHECK_FLAG (ifc
->conf
, ZEBRA_IFC_CONFIGURED
))
1392 SET_FLAG (ifc
->conf
, ZEBRA_IFC_CONFIGURED
);
1394 /* In case of this route need to install kernel. */
1395 if (! CHECK_FLAG (ifc
->conf
, ZEBRA_IFC_REAL
)
1396 && CHECK_FLAG (ifp
->status
, ZEBRA_INTERFACE_ACTIVE
))
1398 /* Some system need to up the interface to set IP address. */
1399 if (! if_is_up (ifp
))
1401 if_set_flags (ifp
, IFF_UP
| IFF_RUNNING
);
1405 ret
= if_prefix_add_ipv6 (ifp
, ifc
);
1409 vty_out (vty
, "%% Can't set interface IP address: %s.%s",
1410 safe_strerror(errno
), VTY_NEWLINE
);
1414 /* IP address propery set. */
1415 SET_FLAG (ifc
->conf
, ZEBRA_IFC_REAL
);
1417 /* Update interface address information to protocol daemon. */
1418 zebra_interface_address_add_update (ifp
, ifc
);
1420 /* If interface is up register connected route. */
1421 if (if_is_operative(ifp
))
1422 connected_up_ipv6 (ifp
, ifc
);
1429 ipv6_address_uninstall (struct vty
*vty
, struct interface
*ifp
,
1430 const char *addr_str
, const char *peer_str
,
1431 const char *label
, int secondry
)
1433 struct prefix_ipv6 cp
;
1434 struct connected
*ifc
;
1437 /* Convert to prefix structure. */
1438 ret
= str2prefix_ipv6 (addr_str
, &cp
);
1441 vty_out (vty
, "%% Malformed address %s", VTY_NEWLINE
);
1445 /* Check current interface address. */
1446 ifc
= connected_check_ipv6 (ifp
, (struct prefix
*) &cp
);
1449 vty_out (vty
, "%% Can't find address%s", VTY_NEWLINE
);
1453 /* This is not configured address. */
1454 if (! CHECK_FLAG (ifc
->conf
, ZEBRA_IFC_CONFIGURED
))
1457 /* This is not real address or interface is not active. */
1458 if (! CHECK_FLAG (ifc
->conf
, ZEBRA_IFC_REAL
)
1459 || ! CHECK_FLAG (ifp
->status
, ZEBRA_INTERFACE_ACTIVE
))
1461 listnode_delete (ifp
->connected
, ifc
);
1462 connected_free (ifc
);
1466 /* This is real route. */
1467 ret
= if_prefix_delete_ipv6 (ifp
, ifc
);
1470 vty_out (vty
, "%% Can't unset interface IP address: %s.%s",
1471 safe_strerror(errno
), VTY_NEWLINE
);
1475 /* Redistribute this information. */
1476 zebra_interface_address_delete_update (ifp
, ifc
);
1478 /* Remove connected route. */
1479 connected_down_ipv6 (ifp
, ifc
);
1481 /* Free address information. */
1482 listnode_delete (ifp
->connected
, ifc
);
1483 connected_free (ifc
);
1488 DEFUN (ipv6_address
,
1490 "ipv6 address X:X::X:X/M",
1491 "Interface IPv6 config commands\n"
1492 "Set the IP address of an interface\n"
1493 "IPv6 address (e.g. 3ffe:506::1/48)\n")
1495 return ipv6_address_install (vty
, vty
->index
, argv
[0], NULL
, NULL
, 0);
1498 DEFUN (no_ipv6_address
,
1499 no_ipv6_address_cmd
,
1500 "no ipv6 address X:X::X:X/M",
1502 "Interface IPv6 config commands\n"
1503 "Set the IP address of an interface\n"
1504 "IPv6 address (e.g. 3ffe:506::1/48)\n")
1506 return ipv6_address_uninstall (vty
, vty
->index
, argv
[0], NULL
, NULL
, 0);
1508 #endif /* HAVE_IPV6 */
1511 if_config_write (struct vty
*vty
)
1513 struct listnode
*node
;
1514 struct interface
*ifp
;
1517 for (node
= listhead (iflist
); node
; nextnode (node
))
1519 struct zebra_if
*if_data
;
1520 struct listnode
*addrnode
;
1521 struct connected
*ifc
;
1524 ifp
= getdata (node
);
1525 if_data
= ifp
->info
;
1527 vty_out (vty
, "interface %s%s", ifp
->name
,
1531 vty_out (vty
, " description %s%s", ifp
->desc
,
1534 /* Assign bandwidth here to avoid unnecessary interface flap
1535 while processing config script */
1536 if (ifp
->bandwidth
!= 0)
1537 vty_out(vty
, " bandwidth %u%s", ifp
->bandwidth
, VTY_NEWLINE
);
1539 if (CHECK_FLAG(ifp
->status
, ZEBRA_INTERFACE_LINKDETECTION
))
1540 vty_out(vty
, " link-detect%s", VTY_NEWLINE
);
1542 for (addrnode
= listhead (ifp
->connected
); addrnode
; nextnode (addrnode
))
1544 ifc
= getdata (addrnode
);
1545 if (CHECK_FLAG (ifc
->conf
, ZEBRA_IFC_CONFIGURED
))
1548 vty_out (vty
, " ip%s address %s/%d",
1549 p
->family
== AF_INET
? "" : "v6",
1550 inet_ntop (p
->family
, &p
->u
.prefix
, buf
, BUFSIZ
),
1554 vty_out (vty
, " label %s", ifc
->label
);
1556 vty_out (vty
, "%s", VTY_NEWLINE
);
1562 if (if_data
->shutdown
== IF_ZEBRA_SHUTDOWN_ON
)
1563 vty_out (vty
, " shutdown%s", VTY_NEWLINE
);
1565 if (if_data
->multicast
!= IF_ZEBRA_MULTICAST_UNSPEC
)
1566 vty_out (vty
, " %smulticast%s",
1567 if_data
->multicast
== IF_ZEBRA_MULTICAST_ON
? "" : "no ",
1572 rtadv_config_write (vty
, ifp
);
1576 irdp_config_write (vty
, ifp
);
1579 vty_out (vty
, "!%s", VTY_NEWLINE
);
1584 /* Allocate and initialize interface vector. */
1588 /* Initialize interface and new hook. */
1590 if_add_hook (IF_NEW_HOOK
, if_zebra_new_hook
);
1591 if_add_hook (IF_DELETE_HOOK
, if_zebra_delete_hook
);
1593 /* Install configuration write function. */
1594 install_node (&interface_node
, if_config_write
);
1596 install_element (VIEW_NODE
, &show_interface_cmd
);
1597 install_element (ENABLE_NODE
, &show_interface_cmd
);
1598 install_element (ENABLE_NODE
, &show_interface_desc_cmd
);
1599 install_element (CONFIG_NODE
, &zebra_interface_cmd
);
1600 install_element (CONFIG_NODE
, &no_interface_cmd
);
1601 install_default (INTERFACE_NODE
);
1602 install_element (INTERFACE_NODE
, &interface_desc_cmd
);
1603 install_element (INTERFACE_NODE
, &no_interface_desc_cmd
);
1604 install_element (INTERFACE_NODE
, &multicast_cmd
);
1605 install_element (INTERFACE_NODE
, &no_multicast_cmd
);
1606 install_element (INTERFACE_NODE
, &linkdetect_cmd
);
1607 install_element (INTERFACE_NODE
, &no_linkdetect_cmd
);
1608 install_element (INTERFACE_NODE
, &shutdown_if_cmd
);
1609 install_element (INTERFACE_NODE
, &no_shutdown_if_cmd
);
1610 install_element (INTERFACE_NODE
, &bandwidth_if_cmd
);
1611 install_element (INTERFACE_NODE
, &no_bandwidth_if_cmd
);
1612 install_element (INTERFACE_NODE
, &no_bandwidth_if_val_cmd
);
1613 install_element (INTERFACE_NODE
, &ip_address_cmd
);
1614 install_element (INTERFACE_NODE
, &no_ip_address_cmd
);
1616 install_element (INTERFACE_NODE
, &ipv6_address_cmd
);
1617 install_element (INTERFACE_NODE
, &no_ipv6_address_cmd
);
1618 #endif /* HAVE_IPV6 */
1620 install_element (INTERFACE_NODE
, &ip_address_label_cmd
);
1621 install_element (INTERFACE_NODE
, &no_ip_address_label_cmd
);
1622 #endif /* HAVE_NETLINK */