]> git.proxmox.com Git - mirror_frr.git/blob - zebra/interface.c
Merge pull request #1611 from qlyoung/fix-vtysh-perf
[mirror_frr.git] / zebra / interface.c
1 /*
2 * Interface function.
3 * Copyright (C) 1997, 1999 Kunihiro Ishiguro
4 *
5 * This file is part of GNU Zebra.
6 *
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
10 * later version.
11 *
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.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; see the file COPYING; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 #include <zebra.h>
23
24 #include "if.h"
25 #include "vty.h"
26 #include "sockunion.h"
27 #include "prefix.h"
28 #include "command.h"
29 #include "memory.h"
30 #include "zebra_memory.h"
31 #include "ioctl.h"
32 #include "connected.h"
33 #include "log.h"
34 #include "zclient.h"
35 #include "vrf.h"
36
37 #include "zebra/rtadv.h"
38 #include "zebra_ns.h"
39 #include "zebra_vrf.h"
40 #include "zebra/interface.h"
41 #include "zebra/rib.h"
42 #include "zebra/rt.h"
43 #include "zebra/zserv.h"
44 #include "zebra/redistribute.h"
45 #include "zebra/debug.h"
46 #include "zebra/irdp.h"
47 #include "zebra/zebra_ptm.h"
48 #include "zebra/rt_netlink.h"
49 #include "zebra/interface.h"
50 #include "zebra/zebra_vxlan.h"
51 #include "zebra/zebra_static.h"
52
53 #define ZEBRA_PTM_SUPPORT
54
55 DEFINE_HOOK(zebra_if_extra_info, (struct vty *vty, struct interface *ifp),
56 (vty, ifp))
57 DEFINE_HOOK(zebra_if_config_wr, (struct vty *vty, struct interface *ifp),
58 (vty, ifp))
59
60
61 static void if_down_del_nbr_connected(struct interface *ifp);
62
63 static int if_zebra_speed_update(struct thread *thread)
64 {
65 struct interface *ifp = THREAD_ARG(thread);
66 struct zebra_if *zif = ifp->info;
67 uint32_t new_speed;
68
69 zif->speed_update = NULL;
70
71 new_speed = kernel_get_speed(ifp);
72 if (new_speed != ifp->speed) {
73 zlog_info("%s: %s old speed: %u new speed: %u",
74 __PRETTY_FUNCTION__, ifp->name,
75 ifp->speed, new_speed);
76 ifp->speed = new_speed;
77 if_add_update(ifp);
78 }
79
80 return 1;
81 }
82
83 static void zebra_if_node_destroy(route_table_delegate_t *delegate,
84 struct route_table *table,
85 struct route_node *node)
86 {
87 if (node->info)
88 list_delete_and_null((struct list **)&node->info);
89 route_node_destroy(delegate, table, node);
90 }
91
92 route_table_delegate_t zebra_if_table_delegate = {
93 .create_node = route_node_create,
94 .destroy_node = zebra_if_node_destroy};
95
96 /* Called when new interface is added. */
97 static int if_zebra_new_hook(struct interface *ifp)
98 {
99 struct zebra_if *zebra_if;
100
101 zebra_if = XCALLOC(MTYPE_TMP, sizeof(struct zebra_if));
102
103 zebra_if->multicast = IF_ZEBRA_MULTICAST_UNSPEC;
104 zebra_if->shutdown = IF_ZEBRA_SHUTDOWN_OFF;
105 zebra_ptm_if_init(zebra_if);
106
107 ifp->ptm_enable = zebra_ptm_get_enable_state();
108 #if defined(HAVE_RTADV)
109 {
110 /* Set default router advertise values. */
111 struct rtadvconf *rtadv;
112
113 rtadv = &zebra_if->rtadv;
114
115 rtadv->AdvSendAdvertisements = 0;
116 rtadv->MaxRtrAdvInterval = RTADV_MAX_RTR_ADV_INTERVAL;
117 rtadv->MinRtrAdvInterval = RTADV_MIN_RTR_ADV_INTERVAL;
118 rtadv->AdvIntervalTimer = 0;
119 rtadv->AdvManagedFlag = 0;
120 rtadv->AdvOtherConfigFlag = 0;
121 rtadv->AdvHomeAgentFlag = 0;
122 rtadv->AdvLinkMTU = 0;
123 rtadv->AdvReachableTime = 0;
124 rtadv->AdvRetransTimer = 0;
125 rtadv->AdvCurHopLimit = 0;
126 rtadv->AdvDefaultLifetime =
127 -1; /* derive from MaxRtrAdvInterval */
128 rtadv->HomeAgentPreference = 0;
129 rtadv->HomeAgentLifetime =
130 -1; /* derive from AdvDefaultLifetime */
131 rtadv->AdvIntervalOption = 0;
132 rtadv->DefaultPreference = RTADV_PREF_MEDIUM;
133
134 rtadv->AdvPrefixList = list_new();
135 }
136 #endif /* HAVE_RTADV */
137
138 /* Initialize installed address chains tree. */
139 zebra_if->ipv4_subnets =
140 route_table_init_with_delegate(&zebra_if_table_delegate);
141
142 ifp->info = zebra_if;
143
144 /*
145 * Some platforms are telling us that the interface is
146 * up and ready to go. When we check the speed we
147 * sometimes get the wrong value. Wait a couple
148 * of seconds and ask again. Hopefully it's all settled
149 * down upon startup.
150 */
151 thread_add_timer(zebrad.master, if_zebra_speed_update,
152 ifp, 15, &zebra_if->speed_update);
153 return 0;
154 }
155
156 /* Called when interface is deleted. */
157 static int if_zebra_delete_hook(struct interface *ifp)
158 {
159 struct zebra_if *zebra_if;
160
161 if (ifp->info) {
162 zebra_if = ifp->info;
163
164 /* Free installed address chains tree. */
165 if (zebra_if->ipv4_subnets)
166 route_table_finish(zebra_if->ipv4_subnets);
167 #if defined(HAVE_RTADV)
168
169 struct rtadvconf *rtadv;
170
171 rtadv = &zebra_if->rtadv;
172 list_delete_and_null(&rtadv->AdvPrefixList);
173 #endif /* HAVE_RTADV */
174
175 THREAD_OFF(zebra_if->speed_update);
176
177 XFREE(MTYPE_TMP, zebra_if);
178 }
179
180 return 0;
181 }
182
183 /* Build the table key */
184 static void if_build_key(u_int32_t ifindex, struct prefix *p)
185 {
186 p->family = AF_INET;
187 p->prefixlen = IPV4_MAX_BITLEN;
188 p->u.prefix4.s_addr = ifindex;
189 }
190
191 /* Link an interface in a per NS interface tree */
192 struct interface *if_link_per_ns(struct zebra_ns *ns, struct interface *ifp)
193 {
194 struct prefix p;
195 struct route_node *rn;
196
197 if (ifp->ifindex == IFINDEX_INTERNAL)
198 return NULL;
199
200 if_build_key(ifp->ifindex, &p);
201 rn = route_node_get(ns->if_table, &p);
202 if (rn->info) {
203 ifp = (struct interface *)rn->info;
204 route_unlock_node(rn); /* get */
205 return ifp;
206 }
207
208 rn->info = ifp;
209 ifp->node = rn;
210
211 return ifp;
212 }
213
214 /* Delete a VRF. This is called in vrf_terminate(). */
215 void if_unlink_per_ns(struct interface *ifp)
216 {
217 ifp->node->info = NULL;
218 route_unlock_node(ifp->node);
219 ifp->node = NULL;
220 }
221
222 /* Look up an interface by identifier within a NS */
223 struct interface *if_lookup_by_index_per_ns(struct zebra_ns *ns,
224 u_int32_t ifindex)
225 {
226 struct prefix p;
227 struct route_node *rn;
228 struct interface *ifp = NULL;
229
230 if_build_key(ifindex, &p);
231 rn = route_node_lookup(ns->if_table, &p);
232 if (rn) {
233 ifp = (struct interface *)rn->info;
234 route_unlock_node(rn); /* lookup */
235 }
236 return ifp;
237 }
238
239 /* Look up an interface by name within a NS */
240 struct interface *if_lookup_by_name_per_ns(struct zebra_ns *ns,
241 const char *ifname)
242 {
243 struct route_node *rn;
244 struct interface *ifp;
245
246 for (rn = route_top(ns->if_table); rn; rn = route_next(rn)) {
247 ifp = (struct interface *)rn->info;
248 if (ifp && strcmp(ifp->name, ifname) == 0)
249 return (ifp);
250 }
251
252 return NULL;
253 }
254
255 const char *ifindex2ifname_per_ns(struct zebra_ns *zns, unsigned int ifindex)
256 {
257 struct interface *ifp;
258
259 return ((ifp = if_lookup_by_index_per_ns(zns, ifindex)) != NULL)
260 ? ifp->name
261 : "unknown";
262 }
263
264 /* Tie an interface address to its derived subnet list of addresses. */
265 int if_subnet_add(struct interface *ifp, struct connected *ifc)
266 {
267 struct route_node *rn;
268 struct zebra_if *zebra_if;
269 struct prefix cp;
270 struct list *addr_list;
271
272 assert(ifp && ifp->info && ifc);
273 zebra_if = ifp->info;
274
275 /* Get address derived subnet node and associated address list, while
276 marking
277 address secondary attribute appropriately. */
278 cp = *CONNECTED_PREFIX(ifc);
279 apply_mask(&cp);
280 rn = route_node_get(zebra_if->ipv4_subnets, &cp);
281
282 if ((addr_list = rn->info))
283 SET_FLAG(ifc->flags, ZEBRA_IFA_SECONDARY);
284 else {
285 UNSET_FLAG(ifc->flags, ZEBRA_IFA_SECONDARY);
286 rn->info = addr_list = list_new();
287 route_lock_node(rn);
288 }
289
290 /* Tie address at the tail of address list. */
291 listnode_add(addr_list, ifc);
292
293 /* Return list element count. */
294 return (addr_list->count);
295 }
296
297 /* Untie an interface address from its derived subnet list of addresses. */
298 int if_subnet_delete(struct interface *ifp, struct connected *ifc)
299 {
300 struct route_node *rn;
301 struct zebra_if *zebra_if;
302 struct list *addr_list;
303 struct prefix cp;
304
305 assert(ifp && ifp->info && ifc);
306 zebra_if = ifp->info;
307
308 cp = *CONNECTED_PREFIX(ifc);
309 apply_mask(&cp);
310
311 /* Get address derived subnet node. */
312 rn = route_node_lookup(zebra_if->ipv4_subnets, &cp);
313 if (!(rn && rn->info)) {
314 zlog_warn(
315 "Trying to remove an address from an unknown subnet."
316 " (please report this bug)");
317 return -1;
318 }
319 route_unlock_node(rn);
320
321 /* Untie address from subnet's address list. */
322 addr_list = rn->info;
323
324 /* Deleting an address that is not registered is a bug.
325 * In any case, we shouldn't decrement the lock counter if the address
326 * is unknown. */
327 if (!listnode_lookup(addr_list, ifc)) {
328 zlog_warn(
329 "Trying to remove an address from a subnet where it is not"
330 " currently registered. (please report this bug)");
331 return -1;
332 }
333
334 listnode_delete(addr_list, ifc);
335 route_unlock_node(rn);
336
337 /* Return list element count, if not empty. */
338 if (addr_list->count) {
339 /* If deleted address is primary, mark subsequent one as such
340 * and distribute. */
341 if (!CHECK_FLAG(ifc->flags, ZEBRA_IFA_SECONDARY)) {
342 ifc = listgetdata(
343 (struct listnode *)listhead(addr_list));
344 zebra_interface_address_delete_update(ifp, ifc);
345 UNSET_FLAG(ifc->flags, ZEBRA_IFA_SECONDARY);
346 /* XXX: Linux kernel removes all the secondary addresses
347 * when the primary
348 * address is removed. We could try to work around that,
349 * though this is
350 * non-trivial. */
351 zebra_interface_address_add_update(ifp, ifc);
352 }
353
354 return addr_list->count;
355 }
356
357 /* Otherwise, free list and route node. */
358 list_delete_and_null(&addr_list);
359 rn->info = NULL;
360 route_unlock_node(rn);
361
362 return 0;
363 }
364
365 /* if_flags_mangle: A place for hacks that require mangling
366 * or tweaking the interface flags.
367 *
368 * ******************** Solaris flags hacks **************************
369 *
370 * Solaris IFF_UP flag reflects only the primary interface as the
371 * routing socket only sends IFINFO for the primary interface. Hence
372 * ~IFF_UP does not per se imply all the logical interfaces are also
373 * down - which we only know of as addresses. Instead we must determine
374 * whether the interface really is up or not according to how many
375 * addresses are still attached. (Solaris always sends RTM_DELADDR if
376 * an interface, logical or not, goes ~IFF_UP).
377 *
378 * Ie, we mangle IFF_UP to *additionally* reflect whether or not there
379 * are addresses left in struct connected, not just the actual underlying
380 * IFF_UP flag.
381 *
382 * We must hence remember the real state of IFF_UP, which we do in
383 * struct zebra_if.primary_state.
384 *
385 * Setting IFF_UP within zebra to administratively shutdown the
386 * interface will affect only the primary interface/address on Solaris.
387 ************************End Solaris flags hacks ***********************
388 */
389 static void if_flags_mangle(struct interface *ifp, uint64_t *newflags)
390 {
391 #ifdef SUNOS_5
392 struct zebra_if *zif = ifp->info;
393
394 zif->primary_state = *newflags & (IFF_UP & 0xff);
395
396 if (CHECK_FLAG(zif->primary_state, IFF_UP)
397 || listcount(ifp->connected) > 0)
398 SET_FLAG(*newflags, IFF_UP);
399 else
400 UNSET_FLAG(*newflags, IFF_UP);
401 #endif /* SUNOS_5 */
402 }
403
404 /* Update the flags field of the ifp with the new flag set provided.
405 * Take whatever actions are required for any changes in flags we care
406 * about.
407 *
408 * newflags should be the raw value, as obtained from the OS.
409 */
410 void if_flags_update(struct interface *ifp, uint64_t newflags)
411 {
412 if_flags_mangle(ifp, &newflags);
413
414 if (if_is_no_ptm_operative(ifp)) {
415 /* operative -> inoperative? */
416 ifp->flags = newflags;
417 if (!if_is_operative(ifp))
418 if_down(ifp);
419 } else {
420 /* inoperative -> operative? */
421 ifp->flags = newflags;
422 if (if_is_operative(ifp))
423 if_up(ifp);
424 }
425 }
426
427 /* Wake up configured address if it is not in current kernel
428 address. */
429 static void if_addr_wakeup(struct interface *ifp)
430 {
431 struct listnode *node, *nnode;
432 struct connected *ifc;
433 struct prefix *p;
434 int ret;
435
436 for (ALL_LIST_ELEMENTS(ifp->connected, node, nnode, ifc)) {
437 p = ifc->address;
438
439 if (CHECK_FLAG(ifc->conf, ZEBRA_IFC_CONFIGURED)
440 && !CHECK_FLAG(ifc->conf, ZEBRA_IFC_QUEUED)) {
441 /* Address check. */
442 if (p->family == AF_INET) {
443 if (!if_is_up(ifp)) {
444 /* Assume zebra is configured like
445 * following:
446 *
447 * interface gre0
448 * ip addr 192.0.2.1/24
449 * !
450 *
451 * As soon as zebra becomes first aware
452 * that gre0 exists in the
453 * kernel, it will set gre0 up and
454 * configure its addresses.
455 *
456 * (This may happen at startup when the
457 * interface already exists
458 * or during runtime when the interface
459 * is added to the kernel)
460 *
461 * XXX: IRDP code is calling here via
462 * if_add_update - this seems
463 * somewhat weird.
464 * XXX: RUNNING is not a settable flag
465 * on any system
466 * I (paulj) am aware of.
467 */
468 if_set_flags(ifp, IFF_UP | IFF_RUNNING);
469 if_refresh(ifp);
470 }
471
472 ret = if_set_prefix(ifp, ifc);
473 if (ret < 0) {
474 zlog_warn(
475 "Can't set interface's address: %s",
476 safe_strerror(errno));
477 continue;
478 }
479
480 SET_FLAG(ifc->conf, ZEBRA_IFC_QUEUED);
481 /* The address will be advertised to zebra
482 * clients when the notification
483 * from the kernel has been received.
484 * It will also be added to the interface's
485 * subnet list then. */
486 }
487 if (p->family == AF_INET6) {
488 if (!if_is_up(ifp)) {
489 /* See long comment above */
490 if_set_flags(ifp, IFF_UP | IFF_RUNNING);
491 if_refresh(ifp);
492 }
493
494 ret = if_prefix_add_ipv6(ifp, ifc);
495 if (ret < 0) {
496 zlog_warn(
497 "Can't set interface's address: %s",
498 safe_strerror(errno));
499 continue;
500 }
501
502 SET_FLAG(ifc->conf, ZEBRA_IFC_QUEUED);
503 /* The address will be advertised to zebra
504 * clients when the notification
505 * from the kernel has been received. */
506 }
507 }
508 }
509 }
510
511 /* Handle interface addition */
512 void if_add_update(struct interface *ifp)
513 {
514 struct zebra_if *if_data;
515
516 if_link_per_ns(zebra_ns_lookup(NS_DEFAULT), ifp);
517
518 if_data = ifp->info;
519 assert(if_data);
520
521 if (if_data->multicast == IF_ZEBRA_MULTICAST_ON)
522 if_set_flags(ifp, IFF_MULTICAST);
523 else if (if_data->multicast == IF_ZEBRA_MULTICAST_OFF)
524 if_unset_flags(ifp, IFF_MULTICAST);
525
526 zebra_ptm_if_set_ptm_state(ifp, if_data);
527
528 zebra_interface_add_update(ifp);
529
530 if (!CHECK_FLAG(ifp->status, ZEBRA_INTERFACE_ACTIVE)) {
531 SET_FLAG(ifp->status, ZEBRA_INTERFACE_ACTIVE);
532
533 if (if_data->shutdown == IF_ZEBRA_SHUTDOWN_ON) {
534 if (IS_ZEBRA_DEBUG_KERNEL)
535 zlog_debug(
536 "interface %s vrf %u index %d is shutdown. "
537 "Won't wake it up.",
538 ifp->name, ifp->vrf_id, ifp->ifindex);
539 return;
540 }
541
542 if_addr_wakeup(ifp);
543
544 if (IS_ZEBRA_DEBUG_KERNEL)
545 zlog_debug(
546 "interface %s vrf %u index %d becomes active.",
547 ifp->name, ifp->vrf_id, ifp->ifindex);
548
549 static_ifindex_update(ifp, true);
550 } else {
551 if (IS_ZEBRA_DEBUG_KERNEL)
552 zlog_debug("interface %s vrf %u index %d is added.",
553 ifp->name, ifp->vrf_id, ifp->ifindex);
554 }
555 }
556
557 /* Install connected routes corresponding to an interface. */
558 static void if_install_connected(struct interface *ifp)
559 {
560 struct listnode *node;
561 struct listnode *next;
562 struct connected *ifc;
563
564 if (ifp->connected) {
565 for (ALL_LIST_ELEMENTS(ifp->connected, node, next, ifc)) {
566 if (CHECK_FLAG(ifc->conf, ZEBRA_IFC_REAL))
567 zebra_interface_address_add_update(ifp, ifc);
568
569 connected_up(ifp, ifc);
570 }
571 }
572 }
573
574 /* Uninstall connected routes corresponding to an interface. */
575 static void if_uninstall_connected(struct interface *ifp)
576 {
577 struct listnode *node;
578 struct listnode *next;
579 struct connected *ifc;
580
581 if (ifp->connected) {
582 for (ALL_LIST_ELEMENTS(ifp->connected, node, next, ifc)) {
583 zebra_interface_address_delete_update(ifp, ifc);
584 connected_down(ifp, ifc);
585 }
586 }
587 }
588
589 /* Uninstall and delete connected routes corresponding to an interface. */
590 /* TODO - Check why IPv4 handling here is different from install or if_down */
591 static void if_delete_connected(struct interface *ifp)
592 {
593 struct connected *ifc;
594 struct prefix cp;
595 struct route_node *rn;
596 struct zebra_if *zebra_if;
597 struct listnode *node;
598 struct listnode *last = NULL;
599
600 zebra_if = ifp->info;
601
602 if (!ifp->connected)
603 return;
604
605 while ((node = (last ? last->next
606 : listhead(ifp->connected)))) {
607 ifc = listgetdata(node);
608
609 cp = *CONNECTED_PREFIX(ifc);
610 apply_mask(&cp);
611
612 if (cp.family == AF_INET
613 && (rn = route_node_lookup(zebra_if->ipv4_subnets,
614 &cp))) {
615 struct listnode *anode;
616 struct listnode *next;
617 struct listnode *first;
618 struct list *addr_list;
619
620 route_unlock_node(rn);
621 addr_list = (struct list *)rn->info;
622
623 /* Remove addresses, secondaries first. */
624 first = listhead(addr_list);
625 if (first)
626 for (anode = first->next; anode || first;
627 anode = next) {
628 if (!anode) {
629 anode = first;
630 first = NULL;
631 }
632 next = anode->next;
633
634 ifc = listgetdata(anode);
635 connected_down(ifp, ifc);
636
637 /* XXX: We have to send notifications
638 * here explicitly, because we destroy
639 * the ifc before receiving the
640 * notification about the address being
641 * deleted.
642 */
643 zebra_interface_address_delete_update(
644 ifp, ifc);
645
646 UNSET_FLAG(ifc->conf, ZEBRA_IFC_REAL);
647 UNSET_FLAG(ifc->conf, ZEBRA_IFC_QUEUED);
648
649 /* Remove from subnet chain. */
650 list_delete_node(addr_list, anode);
651 route_unlock_node(rn);
652
653 /* Remove from interface address list
654 * (unconditionally). */
655 if (!CHECK_FLAG(ifc->conf,
656 ZEBRA_IFC_CONFIGURED)) {
657 listnode_delete(ifp->connected,
658 ifc);
659 connected_free(ifc);
660 } else
661 last = node;
662 }
663
664 /* Free chain list and respective route node. */
665 list_delete_and_null(&addr_list);
666 rn->info = NULL;
667 route_unlock_node(rn);
668 } else if (cp.family == AF_INET6) {
669 connected_down(ifp, ifc);
670
671 zebra_interface_address_delete_update(ifp, ifc);
672
673 UNSET_FLAG(ifc->conf, ZEBRA_IFC_REAL);
674 UNSET_FLAG(ifc->conf, ZEBRA_IFC_QUEUED);
675
676 if (CHECK_FLAG(ifc->conf, ZEBRA_IFC_CONFIGURED))
677 last = node;
678 else {
679 listnode_delete(ifp->connected, ifc);
680 connected_free(ifc);
681 }
682 } else {
683 last = node;
684 }
685 }
686 }
687
688 /* Handle an interface delete event */
689 void if_delete_update(struct interface *ifp)
690 {
691 struct zebra_if *zif;
692
693 if (if_is_up(ifp)) {
694 zlog_err(
695 "interface %s vrf %u index %d is still up while being deleted.",
696 ifp->name, ifp->vrf_id, ifp->ifindex);
697 return;
698 }
699
700 /* Mark interface as inactive */
701 UNSET_FLAG(ifp->status, ZEBRA_INTERFACE_ACTIVE);
702
703 if (IS_ZEBRA_DEBUG_KERNEL)
704 zlog_debug("interface %s vrf %u index %d is now inactive.",
705 ifp->name, ifp->vrf_id, ifp->ifindex);
706
707 static_ifindex_update(ifp, false);
708
709 /* Delete connected routes from the kernel. */
710 if_delete_connected(ifp);
711
712 /* Send out notification on interface delete. */
713 zebra_interface_delete_update(ifp);
714
715 if_unlink_per_ns(ifp);
716
717 /* Update ifindex after distributing the delete message. This is in
718 case any client needs to have the old value of ifindex available
719 while processing the deletion. Each client daemon is responsible
720 for setting ifindex to IFINDEX_INTERNAL after processing the
721 interface deletion message. */
722 if_set_index(ifp, IFINDEX_INTERNAL);
723 ifp->node = NULL;
724
725 /* if the ifp is in a vrf, move it to default so vrf can be deleted if
726 * desired */
727 if (ifp->vrf_id)
728 if_handle_vrf_change(ifp, VRF_DEFAULT);
729
730 /* Reset some zebra interface params to default values. */
731 zif = ifp->info;
732 if (zif) {
733 zif->zif_type = ZEBRA_IF_OTHER;
734 zif->zif_slave_type = ZEBRA_IF_SLAVE_NONE;
735 memset(&zif->l2info, 0, sizeof(union zebra_l2if_info));
736 memset(&zif->brslave_info, 0,
737 sizeof(struct zebra_l2info_brslave));
738 }
739 }
740
741 /* VRF change for an interface */
742 void if_handle_vrf_change(struct interface *ifp, vrf_id_t vrf_id)
743 {
744 vrf_id_t old_vrf_id;
745
746 old_vrf_id = ifp->vrf_id;
747
748 static_ifindex_update(ifp, false);
749
750 /* Uninstall connected routes. */
751 if_uninstall_connected(ifp);
752
753 /* Delete any IPv4 neighbors created to implement RFC 5549 */
754 if_nbr_ipv6ll_to_ipv4ll_neigh_del_all(ifp);
755
756 /* Delete all neighbor addresses learnt through IPv6 RA */
757 if_down_del_nbr_connected(ifp);
758
759 /* Send out notification on interface VRF change. */
760 /* This is to issue an UPDATE or a DELETE, as appropriate. */
761 zebra_interface_vrf_update_del(ifp, vrf_id);
762
763 /* update VRF */
764 if_update_to_new_vrf(ifp, vrf_id);
765
766 /* Send out notification on interface VRF change. */
767 /* This is to issue an ADD, if needed. */
768 zebra_interface_vrf_update_add(ifp, old_vrf_id);
769
770 /* Install connected routes (in new VRF). */
771 if_install_connected(ifp);
772
773 static_ifindex_update(ifp, true);
774
775 /* Due to connected route change, schedule RIB processing for both old
776 * and new VRF.
777 */
778 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
779 zlog_debug("%u: IF %s VRF change, scheduling RIB processing",
780 ifp->vrf_id, ifp->name);
781 rib_update(old_vrf_id, RIB_UPDATE_IF_CHANGE);
782 rib_update(ifp->vrf_id, RIB_UPDATE_IF_CHANGE);
783 }
784
785 static void ipv6_ll_address_to_mac(struct in6_addr *address, u_char *mac)
786 {
787 mac[0] = address->s6_addr[8] ^ 0x02;
788 mac[1] = address->s6_addr[9];
789 mac[2] = address->s6_addr[10];
790 mac[3] = address->s6_addr[13];
791 mac[4] = address->s6_addr[14];
792 mac[5] = address->s6_addr[15];
793 }
794
795 void if_nbr_ipv6ll_to_ipv4ll_neigh_update(struct interface *ifp,
796 struct in6_addr *address, int add)
797 {
798 struct zebra_vrf *zvrf = vrf_info_lookup(ifp->vrf_id);
799 char buf[16] = "169.254.0.1";
800 struct in_addr ipv4_ll;
801 char mac[6];
802
803 inet_pton(AF_INET, buf, &ipv4_ll);
804
805 ipv6_ll_address_to_mac(address, (u_char *)mac);
806
807 /*
808 * Remove existed arp record for the interface as netlink
809 * protocol does not have update message types
810 *
811 * supported message types are RTM_NEWNEIGH and RTM_DELNEIGH
812 */
813 kernel_neigh_update (0, ifp->ifindex, ipv4_ll.s_addr, mac, 6);
814
815 /* Add arp record */
816 kernel_neigh_update (add, ifp->ifindex, ipv4_ll.s_addr, mac, 6);
817 zvrf->neigh_updates++;
818 }
819
820 static void if_nbr_ipv6ll_to_ipv4ll_neigh_add_all(struct interface *ifp)
821 {
822 if (listhead(ifp->nbr_connected)) {
823 struct nbr_connected *nbr_connected;
824 struct listnode *node;
825
826 for (ALL_LIST_ELEMENTS_RO(ifp->nbr_connected, node,
827 nbr_connected))
828 if_nbr_ipv6ll_to_ipv4ll_neigh_update(
829 ifp, &nbr_connected->address->u.prefix6, 1);
830 }
831 }
832
833 void if_nbr_ipv6ll_to_ipv4ll_neigh_del_all(struct interface *ifp)
834 {
835 if (listhead(ifp->nbr_connected)) {
836 struct nbr_connected *nbr_connected;
837 struct listnode *node;
838
839 for (ALL_LIST_ELEMENTS_RO(ifp->nbr_connected, node,
840 nbr_connected))
841 if_nbr_ipv6ll_to_ipv4ll_neigh_update(
842 ifp, &nbr_connected->address->u.prefix6, 0);
843 }
844 }
845
846 static void if_down_del_nbr_connected(struct interface *ifp)
847 {
848 struct nbr_connected *nbr_connected;
849 struct listnode *node, *nnode;
850
851 for (ALL_LIST_ELEMENTS(ifp->nbr_connected, node, nnode,
852 nbr_connected)) {
853 listnode_delete(ifp->nbr_connected, nbr_connected);
854 nbr_connected_free(nbr_connected);
855 }
856 }
857
858 /* Interface is up. */
859 void if_up(struct interface *ifp)
860 {
861 struct zebra_if *zif;
862 struct interface *link_if;
863
864 zif = ifp->info;
865 zif->up_count++;
866 quagga_timestamp(2, zif->up_last, sizeof(zif->up_last));
867
868 /* Notify the protocol daemons. */
869 if (ifp->ptm_enable && (ifp->ptm_status == ZEBRA_PTM_STATUS_DOWN)) {
870 zlog_warn("%s: interface %s hasn't passed ptm check\n",
871 __func__, ifp->name);
872 return;
873 }
874 zebra_interface_up_update(ifp);
875
876 if_nbr_ipv6ll_to_ipv4ll_neigh_add_all(ifp);
877
878 #if defined(HAVE_RTADV)
879 /* Enable fast tx of RA if enabled && RA interval is not in msecs */
880 if (zif->rtadv.AdvSendAdvertisements
881 && (zif->rtadv.MaxRtrAdvInterval >= 1000)) {
882 zif->rtadv.inFastRexmit = 1;
883 zif->rtadv.NumFastReXmitsRemain = RTADV_NUM_FAST_REXMITS;
884 }
885 #endif
886
887 /* Install connected routes to the kernel. */
888 if_install_connected(ifp);
889
890 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
891 zlog_debug("%u: IF %s up, scheduling RIB processing",
892 ifp->vrf_id, ifp->name);
893 rib_update(ifp->vrf_id, RIB_UPDATE_IF_CHANGE);
894
895 /* Handle interface up for specific types for EVPN. Non-VxLAN interfaces
896 * are checked to see if (remote) neighbor entries need to be installed
897 * on them for ARP suppression.
898 */
899 if (IS_ZEBRA_IF_VXLAN(ifp))
900 zebra_vxlan_if_up(ifp);
901 else if (IS_ZEBRA_IF_BRIDGE(ifp)) {
902 link_if = ifp;
903 zebra_vxlan_svi_up(ifp, link_if);
904 } else if (IS_ZEBRA_IF_VLAN(ifp)) {
905 link_if = if_lookup_by_index_per_ns(zebra_ns_lookup(NS_DEFAULT),
906 zif->link_ifindex);
907 if (link_if)
908 zebra_vxlan_svi_up(ifp, link_if);
909 }
910 }
911
912 /* Interface goes down. We have to manage different behavior of based
913 OS. */
914 void if_down(struct interface *ifp)
915 {
916 struct zebra_if *zif;
917 struct interface *link_if;
918
919 zif = ifp->info;
920 zif->down_count++;
921 quagga_timestamp(2, zif->down_last, sizeof(zif->down_last));
922
923 /* Handle interface down for specific types for EVPN. Non-VxLAN
924 * interfaces
925 * are checked to see if (remote) neighbor entries need to be purged
926 * for ARP suppression.
927 */
928 if (IS_ZEBRA_IF_VXLAN(ifp))
929 zebra_vxlan_if_down(ifp);
930 else if (IS_ZEBRA_IF_BRIDGE(ifp)) {
931 link_if = ifp;
932 zebra_vxlan_svi_down(ifp, link_if);
933 } else if (IS_ZEBRA_IF_VLAN(ifp)) {
934 link_if = if_lookup_by_index_per_ns(zebra_ns_lookup(NS_DEFAULT),
935 zif->link_ifindex);
936 if (link_if)
937 zebra_vxlan_svi_down(ifp, link_if);
938 }
939
940
941 /* Notify to the protocol daemons. */
942 zebra_interface_down_update(ifp);
943
944 /* Uninstall connected routes from the kernel. */
945 if_uninstall_connected(ifp);
946
947 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
948 zlog_debug("%u: IF %s down, scheduling RIB processing",
949 ifp->vrf_id, ifp->name);
950 rib_update(ifp->vrf_id, RIB_UPDATE_IF_CHANGE);
951
952 if_nbr_ipv6ll_to_ipv4ll_neigh_del_all(ifp);
953
954 /* Delete all neighbor addresses learnt through IPv6 RA */
955 if_down_del_nbr_connected(ifp);
956 }
957
958 void if_refresh(struct interface *ifp)
959 {
960 if_get_flags(ifp);
961 }
962
963 void zebra_if_update_link(struct interface *ifp, ifindex_t link_ifindex)
964 {
965 struct zebra_if *zif;
966
967 zif = (struct zebra_if *)ifp->info;
968 zif->link_ifindex = link_ifindex;
969 zif->link = if_lookup_by_index_per_ns(zebra_ns_lookup(NS_DEFAULT),
970 link_ifindex);
971 }
972
973
974 /* Output prefix string to vty. */
975 static int prefix_vty_out(struct vty *vty, struct prefix *p)
976 {
977 char str[INET6_ADDRSTRLEN];
978
979 inet_ntop(p->family, &p->u.prefix, str, sizeof(str));
980 vty_out(vty, "%s", str);
981 return strlen(str);
982 }
983
984 /* Dump if address information to vty. */
985 static void connected_dump_vty(struct vty *vty, struct connected *connected)
986 {
987 struct prefix *p;
988
989 /* Print interface address. */
990 p = connected->address;
991 vty_out(vty, " %s ", prefix_family_str(p));
992 prefix_vty_out(vty, p);
993 vty_out(vty, "/%d", p->prefixlen);
994
995 /* If there is destination address, print it. */
996 if (connected->destination) {
997 vty_out(vty,
998 (CONNECTED_PEER(connected) ? " peer " : " broadcast "));
999 prefix_vty_out(vty, connected->destination);
1000 if (CONNECTED_PEER(connected))
1001 vty_out(vty, "/%d", connected->destination->prefixlen);
1002 }
1003
1004 if (CHECK_FLAG(connected->flags, ZEBRA_IFA_SECONDARY))
1005 vty_out(vty, " secondary");
1006
1007 if (CHECK_FLAG(connected->flags, ZEBRA_IFA_UNNUMBERED))
1008 vty_out(vty, " unnumbered");
1009
1010 if (connected->label)
1011 vty_out(vty, " %s", connected->label);
1012
1013 vty_out(vty, "\n");
1014 }
1015
1016 /* Dump interface neighbor address information to vty. */
1017 static void nbr_connected_dump_vty(struct vty *vty,
1018 struct nbr_connected *connected)
1019 {
1020 struct prefix *p;
1021
1022 /* Print interface address. */
1023 p = connected->address;
1024 vty_out(vty, " %s ", prefix_family_str(p));
1025 prefix_vty_out(vty, p);
1026 vty_out(vty, "/%d", p->prefixlen);
1027
1028 vty_out(vty, "\n");
1029 }
1030
1031 static const char *zebra_ziftype_2str(zebra_iftype_t zif_type)
1032 {
1033 switch (zif_type) {
1034 case ZEBRA_IF_OTHER:
1035 return "Other";
1036 break;
1037
1038 case ZEBRA_IF_BRIDGE:
1039 return "Bridge";
1040 break;
1041
1042 case ZEBRA_IF_VLAN:
1043 return "Vlan";
1044 break;
1045
1046 case ZEBRA_IF_VXLAN:
1047 return "Vxlan";
1048 break;
1049
1050 case ZEBRA_IF_VRF:
1051 return "VRF";
1052 break;
1053
1054 default:
1055 return "Unknown";
1056 break;
1057 }
1058 }
1059
1060 /* Interface's information print out to vty interface. */
1061 static void if_dump_vty(struct vty *vty, struct interface *ifp)
1062 {
1063 struct connected *connected;
1064 struct nbr_connected *nbr_connected;
1065 struct listnode *node;
1066 struct route_node *rn;
1067 struct zebra_if *zebra_if;
1068 struct vrf *vrf;
1069
1070 zebra_if = ifp->info;
1071
1072 vty_out(vty, "Interface %s is ", ifp->name);
1073 if (if_is_up(ifp)) {
1074 vty_out(vty, "up, line protocol ");
1075
1076 if (CHECK_FLAG(ifp->status, ZEBRA_INTERFACE_LINKDETECTION)) {
1077 if (if_is_running(ifp))
1078 vty_out(vty, "is up\n");
1079 else
1080 vty_out(vty, "is down\n");
1081 } else {
1082 vty_out(vty, "detection is disabled\n");
1083 }
1084 } else {
1085 vty_out(vty, "down\n");
1086 }
1087
1088 vty_out(vty, " Link ups: %5u last: %s\n", zebra_if->up_count,
1089 zebra_if->up_last[0] ? zebra_if->up_last : "(never)");
1090 vty_out(vty, " Link downs: %5u last: %s\n", zebra_if->down_count,
1091 zebra_if->down_last[0] ? zebra_if->down_last : "(never)");
1092
1093 zebra_ptm_show_status(vty, ifp);
1094
1095 vrf = vrf_lookup_by_id(ifp->vrf_id);
1096 vty_out(vty, " vrf: %s\n", vrf->name);
1097
1098 if (ifp->desc)
1099 vty_out(vty, " Description: %s\n", ifp->desc);
1100 if (ifp->ifindex == IFINDEX_INTERNAL) {
1101 vty_out(vty, " pseudo interface\n");
1102 return;
1103 } else if (!CHECK_FLAG(ifp->status, ZEBRA_INTERFACE_ACTIVE)) {
1104 vty_out(vty, " index %d inactive interface\n", ifp->ifindex);
1105 return;
1106 }
1107
1108 vty_out(vty, " index %d metric %d mtu %d speed %u ", ifp->ifindex,
1109 ifp->metric, ifp->mtu, ifp->speed);
1110 if (ifp->mtu6 != ifp->mtu)
1111 vty_out(vty, "mtu6 %d ", ifp->mtu6);
1112 vty_out(vty, "\n flags: %s\n", if_flag_dump(ifp->flags));
1113
1114 /* Hardware address. */
1115 vty_out(vty, " Type: %s\n", if_link_type_str(ifp->ll_type));
1116 if (ifp->hw_addr_len != 0) {
1117 int i;
1118
1119 vty_out(vty, " HWaddr: ");
1120 for (i = 0; i < ifp->hw_addr_len; i++)
1121 vty_out(vty, "%s%02x", i == 0 ? "" : ":",
1122 ifp->hw_addr[i]);
1123 vty_out(vty, "\n");
1124 }
1125
1126 /* Bandwidth in Mbps */
1127 if (ifp->bandwidth != 0) {
1128 vty_out(vty, " bandwidth %u Mbps", ifp->bandwidth);
1129 vty_out(vty, "\n");
1130 }
1131
1132 for (rn = route_top(zebra_if->ipv4_subnets); rn; rn = route_next(rn)) {
1133 if (!rn->info)
1134 continue;
1135
1136 for (ALL_LIST_ELEMENTS_RO((struct list *)rn->info, node,
1137 connected))
1138 connected_dump_vty(vty, connected);
1139 }
1140
1141 for (ALL_LIST_ELEMENTS_RO(ifp->connected, node, connected)) {
1142 if (CHECK_FLAG(connected->conf, ZEBRA_IFC_REAL)
1143 && (connected->address->family == AF_INET6))
1144 connected_dump_vty(vty, connected);
1145 }
1146
1147 vty_out(vty, " Interface Type %s\n",
1148 zebra_ziftype_2str(zebra_if->zif_type));
1149 if (IS_ZEBRA_IF_BRIDGE(ifp)) {
1150 struct zebra_l2info_bridge *bridge_info;
1151
1152 bridge_info = &zebra_if->l2info.br;
1153 vty_out(vty, " Bridge VLAN-aware: %s\n",
1154 bridge_info->vlan_aware ? "yes" : "no");
1155 } else if (IS_ZEBRA_IF_VLAN(ifp)) {
1156 struct zebra_l2info_vlan *vlan_info;
1157
1158 vlan_info = &zebra_if->l2info.vl;
1159 vty_out(vty, " VLAN Id %u\n", vlan_info->vid);
1160 } else if (IS_ZEBRA_IF_VXLAN(ifp)) {
1161 struct zebra_l2info_vxlan *vxlan_info;
1162
1163 vxlan_info = &zebra_if->l2info.vxl;
1164 vty_out(vty, " VxLAN Id %u", vxlan_info->vni);
1165 if (vxlan_info->vtep_ip.s_addr != INADDR_ANY)
1166 vty_out(vty, " VTEP IP: %s",
1167 inet_ntoa(vxlan_info->vtep_ip));
1168 if (vxlan_info->access_vlan)
1169 vty_out(vty, " Access VLAN Id %u",
1170 vxlan_info->access_vlan);
1171 vty_out(vty, "\n");
1172 }
1173
1174 if (IS_ZEBRA_IF_BRIDGE_SLAVE(ifp)) {
1175 struct zebra_l2info_brslave *br_slave;
1176
1177 br_slave = &zebra_if->brslave_info;
1178 if (br_slave->bridge_ifindex != IFINDEX_INTERNAL)
1179 vty_out(vty, " Master (bridge) ifindex %u\n",
1180 br_slave->bridge_ifindex);
1181 }
1182
1183 if (zebra_if->link_ifindex != IFINDEX_INTERNAL)
1184 vty_out(vty, " Link ifindex %u\n", zebra_if->link_ifindex);
1185
1186 if (HAS_LINK_PARAMS(ifp)) {
1187 int i;
1188 struct if_link_params *iflp = ifp->link_params;
1189 vty_out(vty, " Traffic Engineering Link Parameters:\n");
1190 if (IS_PARAM_SET(iflp, LP_TE_METRIC))
1191 vty_out(vty, " TE metric %u\n", iflp->te_metric);
1192 if (IS_PARAM_SET(iflp, LP_MAX_BW))
1193 vty_out(vty, " Maximum Bandwidth %g (Byte/s)\n",
1194 iflp->max_bw);
1195 if (IS_PARAM_SET(iflp, LP_MAX_RSV_BW))
1196 vty_out(vty,
1197 " Maximum Reservable Bandwidth %g (Byte/s)\n",
1198 iflp->max_rsv_bw);
1199 if (IS_PARAM_SET(iflp, LP_UNRSV_BW)) {
1200 vty_out(vty,
1201 " Unreserved Bandwidth per Class Type in Byte/s:\n");
1202 for (i = 0; i < MAX_CLASS_TYPE; i += 2)
1203 vty_out(vty,
1204 " [%d]: %g (Bytes/sec),\t[%d]: %g (Bytes/sec)\n",
1205 i, iflp->unrsv_bw[i], i + 1,
1206 iflp->unrsv_bw[i + 1]);
1207 }
1208
1209 if (IS_PARAM_SET(iflp, LP_ADM_GRP))
1210 vty_out(vty, " Administrative Group:%u\n",
1211 iflp->admin_grp);
1212 if (IS_PARAM_SET(iflp, LP_DELAY)) {
1213 vty_out(vty, " Link Delay Average: %u (micro-sec.)",
1214 iflp->av_delay);
1215 if (IS_PARAM_SET(iflp, LP_MM_DELAY)) {
1216 vty_out(vty, " Min: %u (micro-sec.)",
1217 iflp->min_delay);
1218 vty_out(vty, " Max: %u (micro-sec.)",
1219 iflp->max_delay);
1220 }
1221 vty_out(vty, "\n");
1222 }
1223 if (IS_PARAM_SET(iflp, LP_DELAY_VAR))
1224 vty_out(vty,
1225 " Link Delay Variation %u (micro-sec.)\n",
1226 iflp->delay_var);
1227 if (IS_PARAM_SET(iflp, LP_PKT_LOSS))
1228 vty_out(vty, " Link Packet Loss %g (in %%)\n",
1229 iflp->pkt_loss);
1230 if (IS_PARAM_SET(iflp, LP_AVA_BW))
1231 vty_out(vty, " Available Bandwidth %g (Byte/s)\n",
1232 iflp->ava_bw);
1233 if (IS_PARAM_SET(iflp, LP_RES_BW))
1234 vty_out(vty, " Residual Bandwidth %g (Byte/s)\n",
1235 iflp->res_bw);
1236 if (IS_PARAM_SET(iflp, LP_USE_BW))
1237 vty_out(vty, " Utilized Bandwidth %g (Byte/s)\n",
1238 iflp->use_bw);
1239 if (IS_PARAM_SET(iflp, LP_RMT_AS))
1240 vty_out(vty, " Neighbor ASBR IP: %s AS: %u \n",
1241 inet_ntoa(iflp->rmt_ip), iflp->rmt_as);
1242 }
1243
1244 hook_call(zebra_if_extra_info, vty, ifp);
1245
1246 if (listhead(ifp->nbr_connected))
1247 vty_out(vty, " Neighbor address(s):\n");
1248 for (ALL_LIST_ELEMENTS_RO(ifp->nbr_connected, node, nbr_connected))
1249 nbr_connected_dump_vty(vty, nbr_connected);
1250
1251 #ifdef HAVE_PROC_NET_DEV
1252 /* Statistics print out using proc file system. */
1253 vty_out(vty,
1254 " %lu input packets (%lu multicast), %lu bytes, "
1255 "%lu dropped\n",
1256 ifp->stats.rx_packets, ifp->stats.rx_multicast,
1257 ifp->stats.rx_bytes, ifp->stats.rx_dropped);
1258
1259 vty_out(vty,
1260 " %lu input errors, %lu length, %lu overrun,"
1261 " %lu CRC, %lu frame\n",
1262 ifp->stats.rx_errors, ifp->stats.rx_length_errors,
1263 ifp->stats.rx_over_errors, ifp->stats.rx_crc_errors,
1264 ifp->stats.rx_frame_errors);
1265
1266 vty_out(vty, " %lu fifo, %lu missed\n", ifp->stats.rx_fifo_errors,
1267 ifp->stats.rx_missed_errors);
1268
1269 vty_out(vty, " %lu output packets, %lu bytes, %lu dropped\n",
1270 ifp->stats.tx_packets, ifp->stats.tx_bytes,
1271 ifp->stats.tx_dropped);
1272
1273 vty_out(vty,
1274 " %lu output errors, %lu aborted, %lu carrier,"
1275 " %lu fifo, %lu heartbeat\n",
1276 ifp->stats.tx_errors, ifp->stats.tx_aborted_errors,
1277 ifp->stats.tx_carrier_errors, ifp->stats.tx_fifo_errors,
1278 ifp->stats.tx_heartbeat_errors);
1279
1280 vty_out(vty, " %lu window, %lu collisions\n",
1281 ifp->stats.tx_window_errors, ifp->stats.collisions);
1282 #endif /* HAVE_PROC_NET_DEV */
1283
1284 #ifdef HAVE_NET_RT_IFLIST
1285 #if defined(__bsdi__) || defined(__NetBSD__)
1286 /* Statistics print out using sysctl (). */
1287 vty_out(vty,
1288 " input packets %llu, bytes %llu, dropped %llu,"
1289 " multicast packets %llu\n",
1290 (unsigned long long)ifp->stats.ifi_ipackets,
1291 (unsigned long long)ifp->stats.ifi_ibytes,
1292 (unsigned long long)ifp->stats.ifi_iqdrops,
1293 (unsigned long long)ifp->stats.ifi_imcasts);
1294
1295 vty_out(vty, " input errors %llu\n",
1296 (unsigned long long)ifp->stats.ifi_ierrors);
1297
1298 vty_out(vty,
1299 " output packets %llu, bytes %llu,"
1300 " multicast packets %llu\n",
1301 (unsigned long long)ifp->stats.ifi_opackets,
1302 (unsigned long long)ifp->stats.ifi_obytes,
1303 (unsigned long long)ifp->stats.ifi_omcasts);
1304
1305 vty_out(vty, " output errors %llu\n",
1306 (unsigned long long)ifp->stats.ifi_oerrors);
1307
1308 vty_out(vty, " collisions %llu\n",
1309 (unsigned long long)ifp->stats.ifi_collisions);
1310 #else
1311 /* Statistics print out using sysctl (). */
1312 vty_out(vty,
1313 " input packets %lu, bytes %lu, dropped %lu,"
1314 " multicast packets %lu\n",
1315 ifp->stats.ifi_ipackets, ifp->stats.ifi_ibytes,
1316 ifp->stats.ifi_iqdrops, ifp->stats.ifi_imcasts);
1317
1318 vty_out(vty, " input errors %lu\n", ifp->stats.ifi_ierrors);
1319
1320 vty_out(vty,
1321 " output packets %lu, bytes %lu, multicast packets %lu\n",
1322 ifp->stats.ifi_opackets, ifp->stats.ifi_obytes,
1323 ifp->stats.ifi_omcasts);
1324
1325 vty_out(vty, " output errors %lu\n", ifp->stats.ifi_oerrors);
1326
1327 vty_out(vty, " collisions %lu\n", ifp->stats.ifi_collisions);
1328 #endif /* __bsdi__ || __NetBSD__ */
1329 #endif /* HAVE_NET_RT_IFLIST */
1330 }
1331
1332 static void interface_update_stats(void)
1333 {
1334 #ifdef HAVE_PROC_NET_DEV
1335 /* If system has interface statistics via proc file system, update
1336 statistics. */
1337 ifstat_update_proc();
1338 #endif /* HAVE_PROC_NET_DEV */
1339 #ifdef HAVE_NET_RT_IFLIST
1340 ifstat_update_sysctl();
1341 #endif /* HAVE_NET_RT_IFLIST */
1342 }
1343
1344 struct cmd_node interface_node = {INTERFACE_NODE, "%s(config-if)# ", 1};
1345
1346 /* Show all interfaces to vty. */
1347 DEFUN (show_interface,
1348 show_interface_cmd,
1349 "show interface [vrf NAME]",
1350 SHOW_STR
1351 "Interface status and configuration\n"
1352 VRF_CMD_HELP_STR)
1353 {
1354 struct vrf *vrf;
1355 struct interface *ifp;
1356 vrf_id_t vrf_id = VRF_DEFAULT;
1357
1358 interface_update_stats();
1359
1360 if (argc > 2)
1361 VRF_GET_ID(vrf_id, argv[3]->arg);
1362
1363 /* All interface print. */
1364 vrf = vrf_lookup_by_id(vrf_id);
1365 FOR_ALL_INTERFACES (vrf, ifp)
1366 if_dump_vty(vty, ifp);
1367
1368 return CMD_SUCCESS;
1369 }
1370
1371
1372 /* Show all interfaces to vty. */
1373 DEFUN (show_interface_vrf_all,
1374 show_interface_vrf_all_cmd,
1375 "show interface vrf all",
1376 SHOW_STR
1377 "Interface status and configuration\n"
1378 VRF_ALL_CMD_HELP_STR)
1379 {
1380 struct vrf *vrf;
1381 struct interface *ifp;
1382
1383 interface_update_stats();
1384
1385 /* All interface print. */
1386 RB_FOREACH (vrf, vrf_name_head, &vrfs_by_name)
1387 FOR_ALL_INTERFACES (vrf, ifp)
1388 if_dump_vty(vty, ifp);
1389
1390 return CMD_SUCCESS;
1391 }
1392
1393 /* Show specified interface to vty. */
1394
1395 DEFUN (show_interface_name_vrf,
1396 show_interface_name_vrf_cmd,
1397 "show interface IFNAME vrf NAME",
1398 SHOW_STR
1399 "Interface status and configuration\n"
1400 "Interface name\n"
1401 VRF_CMD_HELP_STR)
1402 {
1403 int idx_ifname = 2;
1404 int idx_name = 4;
1405 struct interface *ifp;
1406 vrf_id_t vrf_id = VRF_DEFAULT;
1407
1408 interface_update_stats();
1409
1410 VRF_GET_ID(vrf_id, argv[idx_name]->arg);
1411
1412 /* Specified interface print. */
1413 ifp = if_lookup_by_name(argv[idx_ifname]->arg, vrf_id);
1414 if (ifp == NULL) {
1415 vty_out(vty, "%% Can't find interface %s\n",
1416 argv[idx_ifname]->arg);
1417 return CMD_WARNING;
1418 }
1419 if_dump_vty(vty, ifp);
1420
1421 return CMD_SUCCESS;
1422 }
1423
1424 /* Show specified interface to vty. */
1425 DEFUN (show_interface_name_vrf_all,
1426 show_interface_name_vrf_all_cmd,
1427 "show interface IFNAME [vrf all]",
1428 SHOW_STR
1429 "Interface status and configuration\n"
1430 "Interface name\n"
1431 VRF_ALL_CMD_HELP_STR)
1432 {
1433 int idx_ifname = 2;
1434 struct vrf *vrf;
1435 struct interface *ifp;
1436 int found = 0;
1437
1438 interface_update_stats();
1439
1440 /* All interface print. */
1441 RB_FOREACH (vrf, vrf_name_head, &vrfs_by_name) {
1442 /* Specified interface print. */
1443 ifp = if_lookup_by_name(argv[idx_ifname]->arg, vrf->vrf_id);
1444 if (ifp) {
1445 if_dump_vty(vty, ifp);
1446 found++;
1447 }
1448 }
1449
1450 if (!found) {
1451 vty_out(vty, "%% Can't find interface %s\n",
1452 argv[idx_ifname]->arg);
1453 return CMD_WARNING;
1454 }
1455
1456 return CMD_SUCCESS;
1457 }
1458
1459
1460 static void if_show_description(struct vty *vty, vrf_id_t vrf_id)
1461 {
1462 struct vrf *vrf = vrf_lookup_by_id(vrf_id);
1463 struct interface *ifp;
1464
1465 vty_out(vty, "Interface Status Protocol Description\n");
1466 FOR_ALL_INTERFACES (vrf, ifp) {
1467 int len;
1468
1469 len = vty_out(vty, "%s", ifp->name);
1470 vty_out(vty, "%*s", (16 - len), " ");
1471
1472 if (if_is_up(ifp)) {
1473 vty_out(vty, "up ");
1474 if (CHECK_FLAG(ifp->status,
1475 ZEBRA_INTERFACE_LINKDETECTION)) {
1476 if (if_is_running(ifp))
1477 vty_out(vty, "up ");
1478 else
1479 vty_out(vty, "down ");
1480 } else {
1481 vty_out(vty, "unknown ");
1482 }
1483 } else {
1484 vty_out(vty, "down down ");
1485 }
1486
1487 if (ifp->desc)
1488 vty_out(vty, "%s", ifp->desc);
1489 vty_out(vty, "\n");
1490 }
1491 }
1492
1493 DEFUN (show_interface_desc,
1494 show_interface_desc_cmd,
1495 "show interface description [vrf NAME]",
1496 SHOW_STR
1497 "Interface status and configuration\n"
1498 "Interface description\n"
1499 VRF_CMD_HELP_STR)
1500 {
1501 vrf_id_t vrf_id = VRF_DEFAULT;
1502
1503 if (argc > 3)
1504 VRF_GET_ID(vrf_id, argv[4]->arg);
1505
1506 if_show_description(vty, vrf_id);
1507
1508 return CMD_SUCCESS;
1509 }
1510
1511
1512 DEFUN (show_interface_desc_vrf_all,
1513 show_interface_desc_vrf_all_cmd,
1514 "show interface description vrf all",
1515 SHOW_STR
1516 "Interface status and configuration\n"
1517 "Interface description\n"
1518 VRF_ALL_CMD_HELP_STR)
1519 {
1520 struct vrf *vrf;
1521
1522 RB_FOREACH (vrf, vrf_name_head, &vrfs_by_name)
1523 if (!RB_EMPTY (if_name_head, &vrf->ifaces_by_name)) {
1524 vty_out(vty, "\n\tVRF %u\n\n", vrf->vrf_id);
1525 if_show_description(vty, vrf->vrf_id);
1526 }
1527
1528 return CMD_SUCCESS;
1529 }
1530
1531 DEFUN (multicast,
1532 multicast_cmd,
1533 "multicast",
1534 "Set multicast flag to interface\n")
1535 {
1536 VTY_DECLVAR_CONTEXT(interface, ifp);
1537 int ret;
1538 struct zebra_if *if_data;
1539
1540 if (CHECK_FLAG(ifp->status, ZEBRA_INTERFACE_ACTIVE)) {
1541 ret = if_set_flags(ifp, IFF_MULTICAST);
1542 if (ret < 0) {
1543 vty_out(vty, "Can't set multicast flag\n");
1544 return CMD_WARNING_CONFIG_FAILED;
1545 }
1546 if_refresh(ifp);
1547 }
1548 if_data = ifp->info;
1549 if_data->multicast = IF_ZEBRA_MULTICAST_ON;
1550
1551 return CMD_SUCCESS;
1552 }
1553
1554 DEFUN (no_multicast,
1555 no_multicast_cmd,
1556 "no multicast",
1557 NO_STR
1558 "Unset multicast flag to interface\n")
1559 {
1560 VTY_DECLVAR_CONTEXT(interface, ifp);
1561 int ret;
1562 struct zebra_if *if_data;
1563
1564 if (CHECK_FLAG(ifp->status, ZEBRA_INTERFACE_ACTIVE)) {
1565 ret = if_unset_flags(ifp, IFF_MULTICAST);
1566 if (ret < 0) {
1567 vty_out(vty, "Can't unset multicast flag\n");
1568 return CMD_WARNING_CONFIG_FAILED;
1569 }
1570 if_refresh(ifp);
1571 }
1572 if_data = ifp->info;
1573 if_data->multicast = IF_ZEBRA_MULTICAST_OFF;
1574
1575 return CMD_SUCCESS;
1576 }
1577
1578 DEFUN (linkdetect,
1579 linkdetect_cmd,
1580 "link-detect",
1581 "Enable link detection on interface\n")
1582 {
1583 VTY_DECLVAR_CONTEXT(interface, ifp);
1584 int if_was_operative;
1585
1586 if_was_operative = if_is_no_ptm_operative(ifp);
1587 SET_FLAG(ifp->status, ZEBRA_INTERFACE_LINKDETECTION);
1588
1589 /* When linkdetection is enabled, if might come down */
1590 if (!if_is_no_ptm_operative(ifp) && if_was_operative)
1591 if_down(ifp);
1592
1593 /* FIXME: Will defer status change forwarding if interface
1594 does not come down! */
1595
1596 return CMD_SUCCESS;
1597 }
1598
1599
1600 DEFUN (no_linkdetect,
1601 no_linkdetect_cmd,
1602 "no link-detect",
1603 NO_STR
1604 "Disable link detection on interface\n")
1605 {
1606 VTY_DECLVAR_CONTEXT(interface, ifp);
1607 int if_was_operative;
1608
1609 if_was_operative = if_is_no_ptm_operative(ifp);
1610 UNSET_FLAG(ifp->status, ZEBRA_INTERFACE_LINKDETECTION);
1611
1612 /* Interface may come up after disabling link detection */
1613 if (if_is_operative(ifp) && !if_was_operative)
1614 if_up(ifp);
1615
1616 /* FIXME: see linkdetect_cmd */
1617
1618 return CMD_SUCCESS;
1619 }
1620
1621 DEFUN (shutdown_if,
1622 shutdown_if_cmd,
1623 "shutdown",
1624 "Shutdown the selected interface\n")
1625 {
1626 VTY_DECLVAR_CONTEXT(interface, ifp);
1627 int ret;
1628 struct zebra_if *if_data;
1629
1630 if (ifp->ifindex != IFINDEX_INTERNAL) {
1631 ret = if_unset_flags(ifp, IFF_UP);
1632 if (ret < 0) {
1633 vty_out(vty, "Can't shutdown interface\n");
1634 return CMD_WARNING_CONFIG_FAILED;
1635 }
1636 if_refresh(ifp);
1637 }
1638 if_data = ifp->info;
1639 if_data->shutdown = IF_ZEBRA_SHUTDOWN_ON;
1640
1641 return CMD_SUCCESS;
1642 }
1643
1644 DEFUN (no_shutdown_if,
1645 no_shutdown_if_cmd,
1646 "no shutdown",
1647 NO_STR
1648 "Shutdown the selected interface\n")
1649 {
1650 VTY_DECLVAR_CONTEXT(interface, ifp);
1651 int ret;
1652 struct zebra_if *if_data;
1653
1654 if (ifp->ifindex != IFINDEX_INTERNAL) {
1655 ret = if_set_flags(ifp, IFF_UP | IFF_RUNNING);
1656 if (ret < 0) {
1657 vty_out(vty, "Can't up interface\n");
1658 return CMD_WARNING_CONFIG_FAILED;
1659 }
1660 if_refresh(ifp);
1661
1662 /* Some addresses (in particular, IPv6 addresses on Linux) get
1663 * removed when the interface goes down. They need to be
1664 * readded.
1665 */
1666 if_addr_wakeup(ifp);
1667 }
1668
1669 if_data = ifp->info;
1670 if_data->shutdown = IF_ZEBRA_SHUTDOWN_OFF;
1671
1672 return CMD_SUCCESS;
1673 }
1674
1675 DEFUN (bandwidth_if,
1676 bandwidth_if_cmd,
1677 "bandwidth (1-100000)",
1678 "Set bandwidth informational parameter\n"
1679 "Bandwidth in megabits\n")
1680 {
1681 int idx_number = 1;
1682 VTY_DECLVAR_CONTEXT(interface, ifp);
1683 unsigned int bandwidth;
1684
1685 bandwidth = strtol(argv[idx_number]->arg, NULL, 10);
1686
1687 /* bandwidth range is <1-100000> */
1688 if (bandwidth < 1 || bandwidth > 100000) {
1689 vty_out(vty, "Bandwidth is invalid\n");
1690 return CMD_WARNING_CONFIG_FAILED;
1691 }
1692
1693 ifp->bandwidth = bandwidth;
1694
1695 /* force protocols to recalculate routes due to cost change */
1696 if (if_is_operative(ifp))
1697 zebra_interface_up_update(ifp);
1698
1699 return CMD_SUCCESS;
1700 }
1701
1702 DEFUN (no_bandwidth_if,
1703 no_bandwidth_if_cmd,
1704 "no bandwidth [(1-100000)]",
1705 NO_STR
1706 "Set bandwidth informational parameter\n"
1707 "Bandwidth in megabits\n")
1708 {
1709 VTY_DECLVAR_CONTEXT(interface, ifp);
1710
1711 ifp->bandwidth = 0;
1712
1713 /* force protocols to recalculate routes due to cost change */
1714 if (if_is_operative(ifp))
1715 zebra_interface_up_update(ifp);
1716
1717 return CMD_SUCCESS;
1718 }
1719
1720
1721 struct cmd_node link_params_node = {
1722 LINK_PARAMS_NODE, "%s(config-link-params)# ", 1,
1723 };
1724
1725 static void link_param_cmd_set_uint32(struct interface *ifp, uint32_t *field,
1726 uint32_t type, uint32_t value)
1727 {
1728 /* Update field as needed */
1729 if (IS_PARAM_UNSET(ifp->link_params, type) || *field != value) {
1730 *field = value;
1731 SET_PARAM(ifp->link_params, type);
1732
1733 /* force protocols to update LINK STATE due to parameters change
1734 */
1735 if (if_is_operative(ifp))
1736 zebra_interface_parameters_update(ifp);
1737 }
1738 }
1739 static void link_param_cmd_set_float(struct interface *ifp, float *field,
1740 uint32_t type, float value)
1741 {
1742
1743 /* Update field as needed */
1744 if (IS_PARAM_UNSET(ifp->link_params, type) || *field != value) {
1745 *field = value;
1746 SET_PARAM(ifp->link_params, type);
1747
1748 /* force protocols to update LINK STATE due to parameters change
1749 */
1750 if (if_is_operative(ifp))
1751 zebra_interface_parameters_update(ifp);
1752 }
1753 }
1754
1755 static void link_param_cmd_unset(struct interface *ifp, uint32_t type)
1756 {
1757 if (ifp->link_params == NULL)
1758 return;
1759
1760 /* Unset field */
1761 UNSET_PARAM(ifp->link_params, type);
1762
1763 /* force protocols to update LINK STATE due to parameters change */
1764 if (if_is_operative(ifp))
1765 zebra_interface_parameters_update(ifp);
1766 }
1767
1768 DEFUN_NOSH (link_params,
1769 link_params_cmd,
1770 "link-params",
1771 LINK_PARAMS_STR)
1772 {
1773 /* vty->qobj_index stays the same @ interface pointer */
1774 vty->node = LINK_PARAMS_NODE;
1775
1776 return CMD_SUCCESS;
1777 }
1778
1779 DEFUN_NOSH (exit_link_params,
1780 exit_link_params_cmd,
1781 "exit-link-params",
1782 "Exit from Link Params configuration mode\n")
1783 {
1784 if (vty->node == LINK_PARAMS_NODE)
1785 vty->node = INTERFACE_NODE;
1786 return CMD_SUCCESS;
1787 }
1788
1789 /* Specific Traffic Engineering parameters commands */
1790 DEFUN (link_params_enable,
1791 link_params_enable_cmd,
1792 "enable",
1793 "Activate link parameters on this interface\n")
1794 {
1795 VTY_DECLVAR_CONTEXT(interface, ifp);
1796
1797 /* This command could be issue at startup, when activate MPLS TE */
1798 /* on a new interface or after a ON / OFF / ON toggle */
1799 /* In all case, TE parameters are reset to their default factory */
1800 if (IS_ZEBRA_DEBUG_EVENT)
1801 zlog_debug(
1802 "Link-params: enable TE link parameters on interface %s",
1803 ifp->name);
1804
1805 if (!if_link_params_get(ifp)) {
1806 if (IS_ZEBRA_DEBUG_EVENT)
1807 zlog_debug(
1808 "Link-params: failed to init TE link parameters %s",
1809 ifp->name);
1810
1811 return CMD_WARNING_CONFIG_FAILED;
1812 }
1813
1814 /* force protocols to update LINK STATE due to parameters change */
1815 if (if_is_operative(ifp))
1816 zebra_interface_parameters_update(ifp);
1817
1818 return CMD_SUCCESS;
1819 }
1820
1821 DEFUN (no_link_params_enable,
1822 no_link_params_enable_cmd,
1823 "no enable",
1824 NO_STR
1825 "Disable link parameters on this interface\n")
1826 {
1827 VTY_DECLVAR_CONTEXT(interface, ifp);
1828
1829 zlog_debug("MPLS-TE: disable TE link parameters on interface %s",
1830 ifp->name);
1831
1832 if_link_params_free(ifp);
1833
1834 /* force protocols to update LINK STATE due to parameters change */
1835 if (if_is_operative(ifp))
1836 zebra_interface_parameters_update(ifp);
1837
1838 return CMD_SUCCESS;
1839 }
1840
1841 /* STANDARD TE metrics */
1842 DEFUN (link_params_metric,
1843 link_params_metric_cmd,
1844 "metric (0-4294967295)",
1845 "Link metric for MPLS-TE purpose\n"
1846 "Metric value in decimal\n")
1847 {
1848 int idx_number = 1;
1849 VTY_DECLVAR_CONTEXT(interface, ifp);
1850 struct if_link_params *iflp = if_link_params_get(ifp);
1851 u_int32_t metric;
1852
1853 metric = strtoul(argv[idx_number]->arg, NULL, 10);
1854
1855 /* Update TE metric if needed */
1856 link_param_cmd_set_uint32(ifp, &iflp->te_metric, LP_TE_METRIC, metric);
1857
1858 return CMD_SUCCESS;
1859 }
1860
1861 DEFUN (no_link_params_metric,
1862 no_link_params_metric_cmd,
1863 "no metric",
1864 NO_STR
1865 "Disable Link Metric on this interface\n")
1866 {
1867 VTY_DECLVAR_CONTEXT(interface, ifp);
1868
1869 /* Unset TE Metric */
1870 link_param_cmd_unset(ifp, LP_TE_METRIC);
1871
1872 return CMD_SUCCESS;
1873 }
1874
1875 DEFUN (link_params_maxbw,
1876 link_params_maxbw_cmd,
1877 "max-bw BANDWIDTH",
1878 "Maximum bandwidth that can be used\n"
1879 "Bytes/second (IEEE floating point format)\n")
1880 {
1881 int idx_bandwidth = 1;
1882 VTY_DECLVAR_CONTEXT(interface, ifp);
1883 struct if_link_params *iflp = if_link_params_get(ifp);
1884
1885 float bw;
1886
1887 if (sscanf(argv[idx_bandwidth]->arg, "%g", &bw) != 1) {
1888 vty_out(vty, "link_params_maxbw: fscanf: %s\n",
1889 safe_strerror(errno));
1890 return CMD_WARNING_CONFIG_FAILED;
1891 }
1892
1893 /* Check that Maximum bandwidth is not lower than other bandwidth
1894 * parameters */
1895 if ((bw <= iflp->max_rsv_bw) || (bw <= iflp->unrsv_bw[0])
1896 || (bw <= iflp->unrsv_bw[1]) || (bw <= iflp->unrsv_bw[2])
1897 || (bw <= iflp->unrsv_bw[3]) || (bw <= iflp->unrsv_bw[4])
1898 || (bw <= iflp->unrsv_bw[5]) || (bw <= iflp->unrsv_bw[6])
1899 || (bw <= iflp->unrsv_bw[7]) || (bw <= iflp->ava_bw)
1900 || (bw <= iflp->res_bw) || (bw <= iflp->use_bw)) {
1901 vty_out(vty,
1902 "Maximum Bandwidth could not be lower than others bandwidth\n");
1903 return CMD_WARNING_CONFIG_FAILED;
1904 }
1905
1906 /* Update Maximum Bandwidth if needed */
1907 link_param_cmd_set_float(ifp, &iflp->max_bw, LP_MAX_BW, bw);
1908
1909 return CMD_SUCCESS;
1910 }
1911
1912 DEFUN (link_params_max_rsv_bw,
1913 link_params_max_rsv_bw_cmd,
1914 "max-rsv-bw BANDWIDTH",
1915 "Maximum bandwidth that may be reserved\n"
1916 "Bytes/second (IEEE floating point format)\n")
1917 {
1918 int idx_bandwidth = 1;
1919 VTY_DECLVAR_CONTEXT(interface, ifp);
1920 struct if_link_params *iflp = if_link_params_get(ifp);
1921 float bw;
1922
1923 if (sscanf(argv[idx_bandwidth]->arg, "%g", &bw) != 1) {
1924 vty_out(vty, "link_params_max_rsv_bw: fscanf: %s\n",
1925 safe_strerror(errno));
1926 return CMD_WARNING_CONFIG_FAILED;
1927 }
1928
1929 /* Check that bandwidth is not greater than maximum bandwidth parameter
1930 */
1931 if (bw > iflp->max_bw) {
1932 vty_out(vty,
1933 "Maximum Reservable Bandwidth could not be greater than Maximum Bandwidth (%g)\n",
1934 iflp->max_bw);
1935 return CMD_WARNING_CONFIG_FAILED;
1936 }
1937
1938 /* Update Maximum Reservable Bandwidth if needed */
1939 link_param_cmd_set_float(ifp, &iflp->max_rsv_bw, LP_MAX_RSV_BW, bw);
1940
1941 return CMD_SUCCESS;
1942 }
1943
1944 DEFUN (link_params_unrsv_bw,
1945 link_params_unrsv_bw_cmd,
1946 "unrsv-bw (0-7) BANDWIDTH",
1947 "Unreserved bandwidth at each priority level\n"
1948 "Priority\n"
1949 "Bytes/second (IEEE floating point format)\n")
1950 {
1951 int idx_number = 1;
1952 int idx_bandwidth = 2;
1953 VTY_DECLVAR_CONTEXT(interface, ifp);
1954 struct if_link_params *iflp = if_link_params_get(ifp);
1955 int priority;
1956 float bw;
1957
1958 /* We don't have to consider about range check here. */
1959 if (sscanf(argv[idx_number]->arg, "%d", &priority) != 1) {
1960 vty_out(vty, "link_params_unrsv_bw: fscanf: %s\n",
1961 safe_strerror(errno));
1962 return CMD_WARNING_CONFIG_FAILED;
1963 }
1964
1965 if (sscanf(argv[idx_bandwidth]->arg, "%g", &bw) != 1) {
1966 vty_out(vty, "link_params_unrsv_bw: fscanf: %s\n",
1967 safe_strerror(errno));
1968 return CMD_WARNING_CONFIG_FAILED;
1969 }
1970
1971 /* Check that bandwidth is not greater than maximum bandwidth parameter
1972 */
1973 if (bw > iflp->max_bw) {
1974 vty_out(vty,
1975 "UnReserved Bandwidth could not be greater than Maximum Bandwidth (%g)\n",
1976 iflp->max_bw);
1977 return CMD_WARNING_CONFIG_FAILED;
1978 }
1979
1980 /* Update Unreserved Bandwidth if needed */
1981 link_param_cmd_set_float(ifp, &iflp->unrsv_bw[priority], LP_UNRSV_BW,
1982 bw);
1983
1984 return CMD_SUCCESS;
1985 }
1986
1987 DEFUN (link_params_admin_grp,
1988 link_params_admin_grp_cmd,
1989 "admin-grp BITPATTERN",
1990 "Administrative group membership\n"
1991 "32-bit Hexadecimal value (e.g. 0xa1)\n")
1992 {
1993 int idx_bitpattern = 1;
1994 VTY_DECLVAR_CONTEXT(interface, ifp);
1995 struct if_link_params *iflp = if_link_params_get(ifp);
1996 unsigned long value;
1997
1998 if (sscanf(argv[idx_bitpattern]->arg, "0x%lx", &value) != 1) {
1999 vty_out(vty, "link_params_admin_grp: fscanf: %s\n",
2000 safe_strerror(errno));
2001 return CMD_WARNING_CONFIG_FAILED;
2002 }
2003
2004 /* Update Administrative Group if needed */
2005 link_param_cmd_set_uint32(ifp, &iflp->admin_grp, LP_ADM_GRP, value);
2006
2007 return CMD_SUCCESS;
2008 }
2009
2010 DEFUN (no_link_params_admin_grp,
2011 no_link_params_admin_grp_cmd,
2012 "no admin-grp",
2013 NO_STR
2014 "Disable Administrative group membership on this interface\n")
2015 {
2016 VTY_DECLVAR_CONTEXT(interface, ifp);
2017
2018 /* Unset Admin Group */
2019 link_param_cmd_unset(ifp, LP_ADM_GRP);
2020
2021 return CMD_SUCCESS;
2022 }
2023
2024 /* RFC5392 & RFC5316: INTER-AS */
2025 DEFUN (link_params_inter_as,
2026 link_params_inter_as_cmd,
2027 "neighbor A.B.C.D as (1-4294967295)",
2028 "Configure remote ASBR information (Neighbor IP address and AS number)\n"
2029 "Remote IP address in dot decimal A.B.C.D\n"
2030 "Remote AS number\n"
2031 "AS number in the range <1-4294967295>\n")
2032 {
2033 int idx_ipv4 = 1;
2034 int idx_number = 3;
2035
2036 VTY_DECLVAR_CONTEXT(interface, ifp);
2037 struct if_link_params *iflp = if_link_params_get(ifp);
2038 struct in_addr addr;
2039 u_int32_t as;
2040
2041 if (!inet_aton(argv[idx_ipv4]->arg, &addr)) {
2042 vty_out(vty, "Please specify Router-Addr by A.B.C.D\n");
2043 return CMD_WARNING_CONFIG_FAILED;
2044 }
2045
2046 as = strtoul(argv[idx_number]->arg, NULL, 10);
2047
2048 /* Update Remote IP and Remote AS fields if needed */
2049 if (IS_PARAM_UNSET(iflp, LP_RMT_AS) || iflp->rmt_as != as
2050 || iflp->rmt_ip.s_addr != addr.s_addr) {
2051
2052 iflp->rmt_as = as;
2053 iflp->rmt_ip.s_addr = addr.s_addr;
2054 SET_PARAM(iflp, LP_RMT_AS);
2055
2056 /* force protocols to update LINK STATE due to parameters change
2057 */
2058 if (if_is_operative(ifp))
2059 zebra_interface_parameters_update(ifp);
2060 }
2061 return CMD_SUCCESS;
2062 }
2063
2064 DEFUN (no_link_params_inter_as,
2065 no_link_params_inter_as_cmd,
2066 "no neighbor",
2067 NO_STR
2068 "Remove Neighbor IP address and AS number for Inter-AS TE\n")
2069 {
2070 VTY_DECLVAR_CONTEXT(interface, ifp);
2071 struct if_link_params *iflp = if_link_params_get(ifp);
2072
2073 /* Reset Remote IP and AS neighbor */
2074 iflp->rmt_as = 0;
2075 iflp->rmt_ip.s_addr = 0;
2076 UNSET_PARAM(iflp, LP_RMT_AS);
2077
2078 /* force protocols to update LINK STATE due to parameters change */
2079 if (if_is_operative(ifp))
2080 zebra_interface_parameters_update(ifp);
2081
2082 return CMD_SUCCESS;
2083 }
2084
2085 /* RFC7471: OSPF Traffic Engineering (TE) Metric extensions &
2086 * draft-ietf-isis-metric-extensions-07.txt */
2087 DEFUN (link_params_delay,
2088 link_params_delay_cmd,
2089 "delay (0-16777215) [min (0-16777215) max (0-16777215)]",
2090 "Unidirectional Average Link Delay\n"
2091 "Average delay in micro-second as decimal (0...16777215)\n"
2092 "Minimum delay\n"
2093 "Minimum delay in micro-second as decimal (0...16777215)\n"
2094 "Maximum delay\n"
2095 "Maximum delay in micro-second as decimal (0...16777215)\n")
2096 {
2097 /* Get and Check new delay values */
2098 u_int32_t delay = 0, low = 0, high = 0;
2099 delay = strtoul(argv[1]->arg, NULL, 10);
2100 if (argc == 6) {
2101 low = strtoul(argv[3]->arg, NULL, 10);
2102 high = strtoul(argv[5]->arg, NULL, 10);
2103 }
2104
2105 VTY_DECLVAR_CONTEXT(interface, ifp);
2106 struct if_link_params *iflp = if_link_params_get(ifp);
2107 u_int8_t update = 0;
2108
2109 if (argc == 2) {
2110 /* Check new delay value against old Min and Max delays if set
2111 */
2112 if (IS_PARAM_SET(iflp, LP_MM_DELAY)
2113 && (delay <= iflp->min_delay || delay >= iflp->max_delay)) {
2114 vty_out(vty,
2115 "Average delay should be comprise between Min (%d) and Max (%d) delay\n",
2116 iflp->min_delay, iflp->max_delay);
2117 return CMD_WARNING_CONFIG_FAILED;
2118 }
2119 /* Update delay if value is not set or change */
2120 if (IS_PARAM_UNSET(iflp, LP_DELAY) || iflp->av_delay != delay) {
2121 iflp->av_delay = delay;
2122 SET_PARAM(iflp, LP_DELAY);
2123 update = 1;
2124 }
2125 /* Unset Min and Max delays if already set */
2126 if (IS_PARAM_SET(iflp, LP_MM_DELAY)) {
2127 iflp->min_delay = 0;
2128 iflp->max_delay = 0;
2129 UNSET_PARAM(iflp, LP_MM_DELAY);
2130 update = 1;
2131 }
2132 } else {
2133 /* Check new delays value coherency */
2134 if (delay <= low || delay >= high) {
2135 vty_out(vty,
2136 "Average delay should be comprise between Min (%d) and Max (%d) delay\n",
2137 low, high);
2138 return CMD_WARNING_CONFIG_FAILED;
2139 }
2140 /* Update Delays if needed */
2141 if (IS_PARAM_UNSET(iflp, LP_DELAY)
2142 || IS_PARAM_UNSET(iflp, LP_MM_DELAY)
2143 || iflp->av_delay != delay || iflp->min_delay != low
2144 || iflp->max_delay != high) {
2145 iflp->av_delay = delay;
2146 SET_PARAM(iflp, LP_DELAY);
2147 iflp->min_delay = low;
2148 iflp->max_delay = high;
2149 SET_PARAM(iflp, LP_MM_DELAY);
2150 update = 1;
2151 }
2152 }
2153
2154 /* force protocols to update LINK STATE due to parameters change */
2155 if (update == 1 && if_is_operative(ifp))
2156 zebra_interface_parameters_update(ifp);
2157
2158 return CMD_SUCCESS;
2159 }
2160
2161 DEFUN (no_link_params_delay,
2162 no_link_params_delay_cmd,
2163 "no delay",
2164 NO_STR
2165 "Disable Unidirectional Average, Min & Max Link Delay on this interface\n")
2166 {
2167 VTY_DECLVAR_CONTEXT(interface, ifp);
2168 struct if_link_params *iflp = if_link_params_get(ifp);
2169
2170 /* Unset Delays */
2171 iflp->av_delay = 0;
2172 UNSET_PARAM(iflp, LP_DELAY);
2173 iflp->min_delay = 0;
2174 iflp->max_delay = 0;
2175 UNSET_PARAM(iflp, LP_MM_DELAY);
2176
2177 /* force protocols to update LINK STATE due to parameters change */
2178 if (if_is_operative(ifp))
2179 zebra_interface_parameters_update(ifp);
2180
2181 return CMD_SUCCESS;
2182 }
2183
2184 DEFUN (link_params_delay_var,
2185 link_params_delay_var_cmd,
2186 "delay-variation (0-16777215)",
2187 "Unidirectional Link Delay Variation\n"
2188 "delay variation in micro-second as decimal (0...16777215)\n")
2189 {
2190 int idx_number = 1;
2191 VTY_DECLVAR_CONTEXT(interface, ifp);
2192 struct if_link_params *iflp = if_link_params_get(ifp);
2193 u_int32_t value;
2194
2195 value = strtoul(argv[idx_number]->arg, NULL, 10);
2196
2197 /* Update Delay Variation if needed */
2198 link_param_cmd_set_uint32(ifp, &iflp->delay_var, LP_DELAY_VAR, value);
2199
2200 return CMD_SUCCESS;
2201 }
2202
2203 DEFUN (no_link_params_delay_var,
2204 no_link_params_delay_var_cmd,
2205 "no delay-variation",
2206 NO_STR
2207 "Disable Unidirectional Delay Variation on this interface\n")
2208 {
2209 VTY_DECLVAR_CONTEXT(interface, ifp);
2210
2211 /* Unset Delay Variation */
2212 link_param_cmd_unset(ifp, LP_DELAY_VAR);
2213
2214 return CMD_SUCCESS;
2215 }
2216
2217 DEFUN (link_params_pkt_loss,
2218 link_params_pkt_loss_cmd,
2219 "packet-loss PERCENTAGE",
2220 "Unidirectional Link Packet Loss\n"
2221 "percentage of total traffic by 0.000003% step and less than 50.331642%\n")
2222 {
2223 int idx_percentage = 1;
2224 VTY_DECLVAR_CONTEXT(interface, ifp);
2225 struct if_link_params *iflp = if_link_params_get(ifp);
2226 float fval;
2227
2228 if (sscanf(argv[idx_percentage]->arg, "%g", &fval) != 1) {
2229 vty_out(vty, "link_params_pkt_loss: fscanf: %s\n",
2230 safe_strerror(errno));
2231 return CMD_WARNING_CONFIG_FAILED;
2232 }
2233
2234 if (fval > MAX_PKT_LOSS)
2235 fval = MAX_PKT_LOSS;
2236
2237 /* Update Packet Loss if needed */
2238 link_param_cmd_set_float(ifp, &iflp->pkt_loss, LP_PKT_LOSS, fval);
2239
2240 return CMD_SUCCESS;
2241 }
2242
2243 DEFUN (no_link_params_pkt_loss,
2244 no_link_params_pkt_loss_cmd,
2245 "no packet-loss",
2246 NO_STR
2247 "Disable Unidirectional Link Packet Loss on this interface\n")
2248 {
2249 VTY_DECLVAR_CONTEXT(interface, ifp);
2250
2251 /* Unset Packet Loss */
2252 link_param_cmd_unset(ifp, LP_PKT_LOSS);
2253
2254 return CMD_SUCCESS;
2255 }
2256
2257 DEFUN (link_params_res_bw,
2258 link_params_res_bw_cmd,
2259 "res-bw BANDWIDTH",
2260 "Unidirectional Residual Bandwidth\n"
2261 "Bytes/second (IEEE floating point format)\n")
2262 {
2263 int idx_bandwidth = 1;
2264 VTY_DECLVAR_CONTEXT(interface, ifp);
2265 struct if_link_params *iflp = if_link_params_get(ifp);
2266 float bw;
2267
2268 if (sscanf(argv[idx_bandwidth]->arg, "%g", &bw) != 1) {
2269 vty_out(vty, "link_params_res_bw: fscanf: %s\n",
2270 safe_strerror(errno));
2271 return CMD_WARNING_CONFIG_FAILED;
2272 }
2273
2274 /* Check that bandwidth is not greater than maximum bandwidth parameter
2275 */
2276 if (bw > iflp->max_bw) {
2277 vty_out(vty,
2278 "Residual Bandwidth could not be greater than Maximum Bandwidth (%g)\n",
2279 iflp->max_bw);
2280 return CMD_WARNING_CONFIG_FAILED;
2281 }
2282
2283 /* Update Residual Bandwidth if needed */
2284 link_param_cmd_set_float(ifp, &iflp->res_bw, LP_RES_BW, bw);
2285
2286 return CMD_SUCCESS;
2287 }
2288
2289 DEFUN (no_link_params_res_bw,
2290 no_link_params_res_bw_cmd,
2291 "no res-bw",
2292 NO_STR
2293 "Disable Unidirectional Residual Bandwidth on this interface\n")
2294 {
2295 VTY_DECLVAR_CONTEXT(interface, ifp);
2296
2297 /* Unset Residual Bandwidth */
2298 link_param_cmd_unset(ifp, LP_RES_BW);
2299
2300 return CMD_SUCCESS;
2301 }
2302
2303 DEFUN (link_params_ava_bw,
2304 link_params_ava_bw_cmd,
2305 "ava-bw BANDWIDTH",
2306 "Unidirectional Available Bandwidth\n"
2307 "Bytes/second (IEEE floating point format)\n")
2308 {
2309 int idx_bandwidth = 1;
2310 VTY_DECLVAR_CONTEXT(interface, ifp);
2311 struct if_link_params *iflp = if_link_params_get(ifp);
2312 float bw;
2313
2314 if (sscanf(argv[idx_bandwidth]->arg, "%g", &bw) != 1) {
2315 vty_out(vty, "link_params_ava_bw: fscanf: %s\n",
2316 safe_strerror(errno));
2317 return CMD_WARNING_CONFIG_FAILED;
2318 }
2319
2320 /* Check that bandwidth is not greater than maximum bandwidth parameter
2321 */
2322 if (bw > iflp->max_bw) {
2323 vty_out(vty,
2324 "Available Bandwidth could not be greater than Maximum Bandwidth (%g)\n",
2325 iflp->max_bw);
2326 return CMD_WARNING_CONFIG_FAILED;
2327 }
2328
2329 /* Update Residual Bandwidth if needed */
2330 link_param_cmd_set_float(ifp, &iflp->ava_bw, LP_AVA_BW, bw);
2331
2332 return CMD_SUCCESS;
2333 }
2334
2335 DEFUN (no_link_params_ava_bw,
2336 no_link_params_ava_bw_cmd,
2337 "no ava-bw",
2338 NO_STR
2339 "Disable Unidirectional Available Bandwidth on this interface\n")
2340 {
2341 VTY_DECLVAR_CONTEXT(interface, ifp);
2342
2343 /* Unset Available Bandwidth */
2344 link_param_cmd_unset(ifp, LP_AVA_BW);
2345
2346 return CMD_SUCCESS;
2347 }
2348
2349 DEFUN (link_params_use_bw,
2350 link_params_use_bw_cmd,
2351 "use-bw BANDWIDTH",
2352 "Unidirectional Utilised Bandwidth\n"
2353 "Bytes/second (IEEE floating point format)\n")
2354 {
2355 int idx_bandwidth = 1;
2356 VTY_DECLVAR_CONTEXT(interface, ifp);
2357 struct if_link_params *iflp = if_link_params_get(ifp);
2358 float bw;
2359
2360 if (sscanf(argv[idx_bandwidth]->arg, "%g", &bw) != 1) {
2361 vty_out(vty, "link_params_use_bw: fscanf: %s\n",
2362 safe_strerror(errno));
2363 return CMD_WARNING_CONFIG_FAILED;
2364 }
2365
2366 /* Check that bandwidth is not greater than maximum bandwidth parameter
2367 */
2368 if (bw > iflp->max_bw) {
2369 vty_out(vty,
2370 "Utilised Bandwidth could not be greater than Maximum Bandwidth (%g)\n",
2371 iflp->max_bw);
2372 return CMD_WARNING_CONFIG_FAILED;
2373 }
2374
2375 /* Update Utilized Bandwidth if needed */
2376 link_param_cmd_set_float(ifp, &iflp->use_bw, LP_USE_BW, bw);
2377
2378 return CMD_SUCCESS;
2379 }
2380
2381 DEFUN (no_link_params_use_bw,
2382 no_link_params_use_bw_cmd,
2383 "no use-bw",
2384 NO_STR
2385 "Disable Unidirectional Utilised Bandwidth on this interface\n")
2386 {
2387 VTY_DECLVAR_CONTEXT(interface, ifp);
2388
2389 /* Unset Utilised Bandwidth */
2390 link_param_cmd_unset(ifp, LP_USE_BW);
2391
2392 return CMD_SUCCESS;
2393 }
2394
2395 static int ip_address_install(struct vty *vty, struct interface *ifp,
2396 const char *addr_str, const char *peer_str,
2397 const char *label)
2398 {
2399 struct zebra_if *if_data;
2400 struct prefix_ipv4 lp, pp;
2401 struct connected *ifc;
2402 struct prefix_ipv4 *p;
2403 int ret;
2404
2405 if_data = ifp->info;
2406
2407 ret = str2prefix_ipv4(addr_str, &lp);
2408 if (ret <= 0) {
2409 vty_out(vty, "%% Malformed address \n");
2410 return CMD_WARNING_CONFIG_FAILED;
2411 }
2412
2413 if (ipv4_martian(&lp.prefix)) {
2414 vty_out(vty, "%% Invalid address\n");
2415 return CMD_WARNING_CONFIG_FAILED;
2416 }
2417
2418 if (peer_str) {
2419 if (lp.prefixlen != 32) {
2420 vty_out(vty,
2421 "%% Local prefix length for P-t-P address must be /32\n");
2422 return CMD_WARNING_CONFIG_FAILED;
2423 }
2424
2425 ret = str2prefix_ipv4(peer_str, &pp);
2426 if (ret <= 0) {
2427 vty_out(vty, "%% Malformed peer address\n");
2428 return CMD_WARNING_CONFIG_FAILED;
2429 }
2430 }
2431
2432 ifc = connected_check_ptp(ifp, &lp, peer_str ? &pp : NULL);
2433 if (!ifc) {
2434 ifc = connected_new();
2435 ifc->ifp = ifp;
2436
2437 /* Address. */
2438 p = prefix_ipv4_new();
2439 *p = lp;
2440 ifc->address = (struct prefix *)p;
2441
2442 if (peer_str) {
2443 SET_FLAG(ifc->flags, ZEBRA_IFA_PEER);
2444 p = prefix_ipv4_new();
2445 *p = pp;
2446 ifc->destination = (struct prefix *)p;
2447 } else if (p->prefixlen <= IPV4_MAX_PREFIXLEN - 2) {
2448 p = prefix_ipv4_new();
2449 *p = lp;
2450 p->prefix.s_addr = ipv4_broadcast_addr(p->prefix.s_addr,
2451 p->prefixlen);
2452 ifc->destination = (struct prefix *)p;
2453 }
2454
2455 /* Label. */
2456 if (label)
2457 ifc->label = XSTRDUP(MTYPE_CONNECTED_LABEL, label);
2458
2459 /* Add to linked list. */
2460 listnode_add(ifp->connected, ifc);
2461 }
2462
2463 /* This address is configured from zebra. */
2464 if (!CHECK_FLAG(ifc->conf, ZEBRA_IFC_CONFIGURED))
2465 SET_FLAG(ifc->conf, ZEBRA_IFC_CONFIGURED);
2466
2467 /* In case of this route need to install kernel. */
2468 if (!CHECK_FLAG(ifc->conf, ZEBRA_IFC_QUEUED)
2469 && CHECK_FLAG(ifp->status, ZEBRA_INTERFACE_ACTIVE)
2470 && !(if_data && if_data->shutdown == IF_ZEBRA_SHUTDOWN_ON)) {
2471 /* Some system need to up the interface to set IP address. */
2472 if (!if_is_up(ifp)) {
2473 if_set_flags(ifp, IFF_UP | IFF_RUNNING);
2474 if_refresh(ifp);
2475 }
2476
2477 ret = if_set_prefix(ifp, ifc);
2478 if (ret < 0) {
2479 vty_out(vty, "%% Can't set interface IP address: %s.\n",
2480 safe_strerror(errno));
2481 return CMD_WARNING_CONFIG_FAILED;
2482 }
2483
2484 SET_FLAG(ifc->conf, ZEBRA_IFC_QUEUED);
2485 /* The address will be advertised to zebra clients when the
2486 * notification
2487 * from the kernel has been received.
2488 * It will also be added to the subnet chain list, then. */
2489 }
2490
2491 return CMD_SUCCESS;
2492 }
2493
2494 static int ip_address_uninstall(struct vty *vty, struct interface *ifp,
2495 const char *addr_str, const char *peer_str,
2496 const char *label)
2497 {
2498 struct prefix_ipv4 lp, pp;
2499 struct connected *ifc;
2500 int ret;
2501
2502 /* Convert to prefix structure. */
2503 ret = str2prefix_ipv4(addr_str, &lp);
2504 if (ret <= 0) {
2505 vty_out(vty, "%% Malformed address \n");
2506 return CMD_WARNING_CONFIG_FAILED;
2507 }
2508
2509 if (peer_str) {
2510 if (lp.prefixlen != 32) {
2511 vty_out(vty,
2512 "%% Local prefix length for P-t-P address must be /32\n");
2513 return CMD_WARNING_CONFIG_FAILED;
2514 }
2515
2516 ret = str2prefix_ipv4(peer_str, &pp);
2517 if (ret <= 0) {
2518 vty_out(vty, "%% Malformed peer address\n");
2519 return CMD_WARNING_CONFIG_FAILED;
2520 }
2521 }
2522
2523 /* Check current interface address. */
2524 ifc = connected_check_ptp(ifp, &lp, peer_str ? &pp : NULL);
2525 if (!ifc) {
2526 vty_out(vty, "%% Can't find address\n");
2527 return CMD_WARNING_CONFIG_FAILED;
2528 }
2529
2530 /* This is not configured address. */
2531 if (!CHECK_FLAG(ifc->conf, ZEBRA_IFC_CONFIGURED))
2532 return CMD_WARNING_CONFIG_FAILED;
2533
2534 UNSET_FLAG(ifc->conf, ZEBRA_IFC_CONFIGURED);
2535
2536 /* This is not real address or interface is not active. */
2537 if (!CHECK_FLAG(ifc->conf, ZEBRA_IFC_QUEUED)
2538 || !CHECK_FLAG(ifp->status, ZEBRA_INTERFACE_ACTIVE)) {
2539 listnode_delete(ifp->connected, ifc);
2540 connected_free(ifc);
2541 return CMD_WARNING_CONFIG_FAILED;
2542 }
2543
2544 /* This is real route. */
2545 ret = if_unset_prefix(ifp, ifc);
2546 if (ret < 0) {
2547 vty_out(vty, "%% Can't unset interface IP address: %s.\n",
2548 safe_strerror(errno));
2549 return CMD_WARNING_CONFIG_FAILED;
2550 }
2551 UNSET_FLAG(ifc->conf, ZEBRA_IFC_QUEUED);
2552 /* we will receive a kernel notification about this route being removed.
2553 * this will trigger its removal from the connected list. */
2554 return CMD_SUCCESS;
2555 }
2556
2557 DEFUN (ip_address,
2558 ip_address_cmd,
2559 "ip address A.B.C.D/M",
2560 "Interface Internet Protocol config commands\n"
2561 "Set the IP address of an interface\n"
2562 "IP address (e.g. 10.0.0.1/8)\n")
2563 {
2564 int idx_ipv4_prefixlen = 2;
2565 VTY_DECLVAR_CONTEXT(interface, ifp);
2566 return ip_address_install(vty, ifp, argv[idx_ipv4_prefixlen]->arg, NULL,
2567 NULL);
2568 }
2569
2570 DEFUN (no_ip_address,
2571 no_ip_address_cmd,
2572 "no ip address A.B.C.D/M",
2573 NO_STR
2574 "Interface Internet Protocol config commands\n"
2575 "Set the IP address of an interface\n"
2576 "IP Address (e.g. 10.0.0.1/8)\n")
2577 {
2578 int idx_ipv4_prefixlen = 3;
2579 VTY_DECLVAR_CONTEXT(interface, ifp);
2580 return ip_address_uninstall(vty, ifp, argv[idx_ipv4_prefixlen]->arg,
2581 NULL, NULL);
2582 }
2583
2584 DEFUN(ip_address_peer,
2585 ip_address_peer_cmd,
2586 "ip address A.B.C.D peer A.B.C.D/M",
2587 "Interface Internet Protocol config commands\n"
2588 "Set the IP address of an interface\n"
2589 "Local IP (e.g. 10.0.0.1) for P-t-P address\n"
2590 "Specify P-t-P address\n"
2591 "Peer IP address (e.g. 10.0.0.1/8)\n")
2592 {
2593 VTY_DECLVAR_CONTEXT(interface, ifp);
2594 return ip_address_install(vty, ifp, argv[2]->arg, argv[4]->arg, NULL);
2595 }
2596
2597 DEFUN(no_ip_address_peer,
2598 no_ip_address_peer_cmd,
2599 "no ip address A.B.C.D peer A.B.C.D/M",
2600 NO_STR
2601 "Interface Internet Protocol config commands\n"
2602 "Set the IP address of an interface\n"
2603 "Local IP (e.g. 10.0.0.1) for P-t-P address\n"
2604 "Specify P-t-P address\n"
2605 "Peer IP address (e.g. 10.0.0.1/8)\n")
2606 {
2607 VTY_DECLVAR_CONTEXT(interface, ifp);
2608 return ip_address_uninstall(vty, ifp, argv[3]->arg, argv[5]->arg, NULL);
2609 }
2610
2611 #ifdef HAVE_NETLINK
2612 DEFUN (ip_address_label,
2613 ip_address_label_cmd,
2614 "ip address A.B.C.D/M label LINE",
2615 "Interface Internet Protocol config commands\n"
2616 "Set the IP address of an interface\n"
2617 "IP address (e.g. 10.0.0.1/8)\n"
2618 "Label of this address\n"
2619 "Label\n")
2620 {
2621 int idx_ipv4_prefixlen = 2;
2622 int idx_line = 4;
2623 VTY_DECLVAR_CONTEXT(interface, ifp);
2624 return ip_address_install(vty, ifp, argv[idx_ipv4_prefixlen]->arg, NULL,
2625 argv[idx_line]->arg);
2626 }
2627
2628 DEFUN (no_ip_address_label,
2629 no_ip_address_label_cmd,
2630 "no ip address A.B.C.D/M label LINE",
2631 NO_STR
2632 "Interface Internet Protocol config commands\n"
2633 "Set the IP address of an interface\n"
2634 "IP address (e.g. 10.0.0.1/8)\n"
2635 "Label of this address\n"
2636 "Label\n")
2637 {
2638 int idx_ipv4_prefixlen = 3;
2639 int idx_line = 5;
2640 VTY_DECLVAR_CONTEXT(interface, ifp);
2641 return ip_address_uninstall(vty, ifp, argv[idx_ipv4_prefixlen]->arg,
2642 NULL, argv[idx_line]->arg);
2643 }
2644 #endif /* HAVE_NETLINK */
2645
2646 static int ipv6_address_install(struct vty *vty, struct interface *ifp,
2647 const char *addr_str, const char *peer_str,
2648 const char *label, int secondary)
2649 {
2650 struct zebra_if *if_data;
2651 struct prefix_ipv6 cp;
2652 struct connected *ifc;
2653 struct prefix_ipv6 *p;
2654 int ret;
2655
2656 if_data = ifp->info;
2657
2658 ret = str2prefix_ipv6(addr_str, &cp);
2659 if (ret <= 0) {
2660 vty_out(vty, "%% Malformed address \n");
2661 return CMD_WARNING_CONFIG_FAILED;
2662 }
2663
2664 if (ipv6_martian(&cp.prefix)) {
2665 vty_out(vty, "%% Invalid address\n");
2666 return CMD_WARNING_CONFIG_FAILED;
2667 }
2668
2669 ifc = connected_check(ifp, (struct prefix *)&cp);
2670 if (!ifc) {
2671 ifc = connected_new();
2672 ifc->ifp = ifp;
2673
2674 /* Address. */
2675 p = prefix_ipv6_new();
2676 *p = cp;
2677 ifc->address = (struct prefix *)p;
2678
2679 /* Secondary. */
2680 if (secondary)
2681 SET_FLAG(ifc->flags, ZEBRA_IFA_SECONDARY);
2682
2683 /* Label. */
2684 if (label)
2685 ifc->label = XSTRDUP(MTYPE_CONNECTED_LABEL, label);
2686
2687 /* Add to linked list. */
2688 listnode_add(ifp->connected, ifc);
2689 }
2690
2691 /* This address is configured from zebra. */
2692 if (!CHECK_FLAG(ifc->conf, ZEBRA_IFC_CONFIGURED))
2693 SET_FLAG(ifc->conf, ZEBRA_IFC_CONFIGURED);
2694
2695 /* In case of this route need to install kernel. */
2696 if (!CHECK_FLAG(ifc->conf, ZEBRA_IFC_QUEUED)
2697 && CHECK_FLAG(ifp->status, ZEBRA_INTERFACE_ACTIVE)
2698 && !(if_data && if_data->shutdown == IF_ZEBRA_SHUTDOWN_ON)) {
2699 /* Some system need to up the interface to set IP address. */
2700 if (!if_is_up(ifp)) {
2701 if_set_flags(ifp, IFF_UP | IFF_RUNNING);
2702 if_refresh(ifp);
2703 }
2704
2705 ret = if_prefix_add_ipv6(ifp, ifc);
2706
2707 if (ret < 0) {
2708 vty_out(vty, "%% Can't set interface IP address: %s.\n",
2709 safe_strerror(errno));
2710 return CMD_WARNING_CONFIG_FAILED;
2711 }
2712
2713 SET_FLAG(ifc->conf, ZEBRA_IFC_QUEUED);
2714 /* The address will be advertised to zebra clients when the
2715 * notification
2716 * from the kernel has been received. */
2717 }
2718
2719 return CMD_SUCCESS;
2720 }
2721
2722 /* Return true if an ipv6 address is configured on ifp */
2723 int ipv6_address_configured(struct interface *ifp)
2724 {
2725 struct connected *connected;
2726 struct listnode *node;
2727
2728 for (ALL_LIST_ELEMENTS_RO(ifp->connected, node, connected))
2729 if (CHECK_FLAG(connected->conf, ZEBRA_IFC_REAL)
2730 && (connected->address->family == AF_INET6))
2731 return 1;
2732
2733 return 0;
2734 }
2735
2736 static int ipv6_address_uninstall(struct vty *vty, struct interface *ifp,
2737 const char *addr_str, const char *peer_str,
2738 const char *label, int secondry)
2739 {
2740 struct prefix_ipv6 cp;
2741 struct connected *ifc;
2742 int ret;
2743
2744 /* Convert to prefix structure. */
2745 ret = str2prefix_ipv6(addr_str, &cp);
2746 if (ret <= 0) {
2747 vty_out(vty, "%% Malformed address \n");
2748 return CMD_WARNING_CONFIG_FAILED;
2749 }
2750
2751 /* Check current interface address. */
2752 ifc = connected_check(ifp, (struct prefix *)&cp);
2753 if (!ifc) {
2754 vty_out(vty, "%% Can't find address\n");
2755 return CMD_WARNING_CONFIG_FAILED;
2756 }
2757
2758 /* This is not configured address. */
2759 if (!CHECK_FLAG(ifc->conf, ZEBRA_IFC_CONFIGURED))
2760 return CMD_WARNING_CONFIG_FAILED;
2761
2762 UNSET_FLAG(ifc->conf, ZEBRA_IFC_CONFIGURED);
2763
2764 /* This is not real address or interface is not active. */
2765 if (!CHECK_FLAG(ifc->conf, ZEBRA_IFC_QUEUED)
2766 || !CHECK_FLAG(ifp->status, ZEBRA_INTERFACE_ACTIVE)) {
2767 listnode_delete(ifp->connected, ifc);
2768 connected_free(ifc);
2769 return CMD_WARNING_CONFIG_FAILED;
2770 }
2771
2772 /* This is real route. */
2773 ret = if_prefix_delete_ipv6(ifp, ifc);
2774 if (ret < 0) {
2775 vty_out(vty, "%% Can't unset interface IP address: %s.\n",
2776 safe_strerror(errno));
2777 return CMD_WARNING_CONFIG_FAILED;
2778 }
2779
2780 UNSET_FLAG(ifc->conf, ZEBRA_IFC_QUEUED);
2781 /* This information will be propagated to the zclients when the
2782 * kernel notification is received. */
2783 return CMD_SUCCESS;
2784 }
2785
2786 DEFUN (ipv6_address,
2787 ipv6_address_cmd,
2788 "ipv6 address X:X::X:X/M",
2789 "Interface IPv6 config commands\n"
2790 "Set the IP address of an interface\n"
2791 "IPv6 address (e.g. 3ffe:506::1/48)\n")
2792 {
2793 int idx_ipv6_prefixlen = 2;
2794 VTY_DECLVAR_CONTEXT(interface, ifp);
2795 return ipv6_address_install(vty, ifp, argv[idx_ipv6_prefixlen]->arg,
2796 NULL, NULL, 0);
2797 }
2798
2799 DEFUN (no_ipv6_address,
2800 no_ipv6_address_cmd,
2801 "no ipv6 address X:X::X:X/M",
2802 NO_STR
2803 "Interface IPv6 config commands\n"
2804 "Set the IP address of an interface\n"
2805 "IPv6 address (e.g. 3ffe:506::1/48)\n")
2806 {
2807 int idx_ipv6_prefixlen = 3;
2808 VTY_DECLVAR_CONTEXT(interface, ifp);
2809 return ipv6_address_uninstall(vty, ifp, argv[idx_ipv6_prefixlen]->arg,
2810 NULL, NULL, 0);
2811 }
2812
2813 static int link_params_config_write(struct vty *vty, struct interface *ifp)
2814 {
2815 int i;
2816
2817 if ((ifp == NULL) || !HAS_LINK_PARAMS(ifp))
2818 return -1;
2819
2820 struct if_link_params *iflp = ifp->link_params;
2821
2822 vty_out(vty, " link-params\n");
2823 vty_out(vty, " enable\n");
2824 if (IS_PARAM_SET(iflp, LP_TE_METRIC) && iflp->te_metric != ifp->metric)
2825 vty_out(vty, " metric %u\n", iflp->te_metric);
2826 if (IS_PARAM_SET(iflp, LP_MAX_BW) && iflp->max_bw != iflp->default_bw)
2827 vty_out(vty, " max-bw %g\n", iflp->max_bw);
2828 if (IS_PARAM_SET(iflp, LP_MAX_RSV_BW)
2829 && iflp->max_rsv_bw != iflp->default_bw)
2830 vty_out(vty, " max-rsv-bw %g\n", iflp->max_rsv_bw);
2831 if (IS_PARAM_SET(iflp, LP_UNRSV_BW)) {
2832 for (i = 0; i < 8; i++)
2833 if (iflp->unrsv_bw[i] != iflp->default_bw)
2834 vty_out(vty, " unrsv-bw %d %g\n", i,
2835 iflp->unrsv_bw[i]);
2836 }
2837 if (IS_PARAM_SET(iflp, LP_ADM_GRP))
2838 vty_out(vty, " admin-grp 0x%x\n", iflp->admin_grp);
2839 if (IS_PARAM_SET(iflp, LP_DELAY)) {
2840 vty_out(vty, " delay %u", iflp->av_delay);
2841 if (IS_PARAM_SET(iflp, LP_MM_DELAY)) {
2842 vty_out(vty, " min %u", iflp->min_delay);
2843 vty_out(vty, " max %u", iflp->max_delay);
2844 }
2845 vty_out(vty, "\n");
2846 }
2847 if (IS_PARAM_SET(iflp, LP_DELAY_VAR))
2848 vty_out(vty, " delay-variation %u\n", iflp->delay_var);
2849 if (IS_PARAM_SET(iflp, LP_PKT_LOSS))
2850 vty_out(vty, " packet-loss %g\n", iflp->pkt_loss);
2851 if (IS_PARAM_SET(iflp, LP_AVA_BW))
2852 vty_out(vty, " ava-bw %g\n", iflp->ava_bw);
2853 if (IS_PARAM_SET(iflp, LP_RES_BW))
2854 vty_out(vty, " res-bw %g\n", iflp->res_bw);
2855 if (IS_PARAM_SET(iflp, LP_USE_BW))
2856 vty_out(vty, " use-bw %g\n", iflp->use_bw);
2857 if (IS_PARAM_SET(iflp, LP_RMT_AS))
2858 vty_out(vty, " neighbor %s as %u\n", inet_ntoa(iflp->rmt_ip),
2859 iflp->rmt_as);
2860 vty_out(vty, " exit-link-params\n");
2861 return 0;
2862 }
2863
2864 static int if_config_write(struct vty *vty)
2865 {
2866 struct vrf *vrf;
2867 struct interface *ifp;
2868
2869 zebra_ptm_write(vty);
2870
2871 RB_FOREACH (vrf, vrf_name_head, &vrfs_by_name)
2872 FOR_ALL_INTERFACES (vrf, ifp) {
2873 struct zebra_if *if_data;
2874 struct listnode *addrnode;
2875 struct connected *ifc;
2876 struct prefix *p;
2877 struct vrf *vrf;
2878
2879 if_data = ifp->info;
2880 vrf = vrf_lookup_by_id(ifp->vrf_id);
2881
2882 if (ifp->vrf_id == VRF_DEFAULT)
2883 vty_frame(vty, "interface %s\n", ifp->name);
2884 else
2885 vty_frame(vty, "interface %s vrf %s\n",
2886 ifp->name, vrf->name);
2887
2888 if (if_data) {
2889 if (if_data->shutdown == IF_ZEBRA_SHUTDOWN_ON)
2890 vty_out(vty, " shutdown\n");
2891
2892 zebra_ptm_if_write(vty, if_data);
2893 }
2894
2895 if (ifp->desc)
2896 vty_out(vty, " description %s\n", ifp->desc);
2897
2898 /* Assign bandwidth here to avoid unnecessary interface
2899 flap
2900 while processing config script */
2901 if (ifp->bandwidth != 0)
2902 vty_out(vty, " bandwidth %u\n", ifp->bandwidth);
2903
2904 if (!CHECK_FLAG(ifp->status,
2905 ZEBRA_INTERFACE_LINKDETECTION))
2906 vty_out(vty, " no link-detect\n");
2907
2908 for (ALL_LIST_ELEMENTS_RO(ifp->connected, addrnode,
2909 ifc)) {
2910 if (CHECK_FLAG(ifc->conf,
2911 ZEBRA_IFC_CONFIGURED)) {
2912 char buf[INET6_ADDRSTRLEN];
2913 p = ifc->address;
2914 vty_out(vty, " ip%s address %s",
2915 p->family == AF_INET ? ""
2916 : "v6",
2917 inet_ntop(p->family,
2918 &p->u.prefix, buf,
2919 sizeof(buf)));
2920 if (CONNECTED_PEER(ifc)) {
2921 p = ifc->destination;
2922 vty_out(vty, " peer %s",
2923 inet_ntop(p->family,
2924 &p->u.prefix,
2925 buf,
2926 sizeof(buf)));
2927 }
2928 vty_out(vty, "/%d", p->prefixlen);
2929
2930 if (ifc->label)
2931 vty_out(vty, " label %s",
2932 ifc->label);
2933
2934 vty_out(vty, "\n");
2935 }
2936 }
2937
2938 if (if_data) {
2939 if (if_data->multicast
2940 != IF_ZEBRA_MULTICAST_UNSPEC)
2941 vty_out(vty, " %smulticast\n",
2942 if_data->multicast
2943 == IF_ZEBRA_MULTICAST_ON
2944 ? ""
2945 : "no ");
2946 }
2947
2948 hook_call(zebra_if_config_wr, vty, ifp);
2949
2950 link_params_config_write(vty, ifp);
2951
2952 vty_endframe(vty, "!\n");
2953 }
2954 return 0;
2955 }
2956
2957 /* Allocate and initialize interface vector. */
2958 void zebra_if_init(void)
2959 {
2960 /* Initialize interface and new hook. */
2961 hook_register_prio(if_add, 0, if_zebra_new_hook);
2962 hook_register_prio(if_del, 0, if_zebra_delete_hook);
2963
2964 /* Install configuration write function. */
2965 install_node(&interface_node, if_config_write);
2966 install_node(&link_params_node, NULL);
2967 if_cmd_init();
2968
2969 install_element(VIEW_NODE, &show_interface_cmd);
2970 install_element(VIEW_NODE, &show_interface_vrf_all_cmd);
2971 install_element(VIEW_NODE, &show_interface_name_vrf_cmd);
2972 install_element(VIEW_NODE, &show_interface_name_vrf_all_cmd);
2973
2974 install_element(ENABLE_NODE, &show_interface_desc_cmd);
2975 install_element(ENABLE_NODE, &show_interface_desc_vrf_all_cmd);
2976 install_element(INTERFACE_NODE, &multicast_cmd);
2977 install_element(INTERFACE_NODE, &no_multicast_cmd);
2978 install_element(INTERFACE_NODE, &linkdetect_cmd);
2979 install_element(INTERFACE_NODE, &no_linkdetect_cmd);
2980 install_element(INTERFACE_NODE, &shutdown_if_cmd);
2981 install_element(INTERFACE_NODE, &no_shutdown_if_cmd);
2982 install_element(INTERFACE_NODE, &bandwidth_if_cmd);
2983 install_element(INTERFACE_NODE, &no_bandwidth_if_cmd);
2984 install_element(INTERFACE_NODE, &ip_address_cmd);
2985 install_element(INTERFACE_NODE, &no_ip_address_cmd);
2986 install_element(INTERFACE_NODE, &ip_address_peer_cmd);
2987 install_element(INTERFACE_NODE, &no_ip_address_peer_cmd);
2988 install_element(INTERFACE_NODE, &ipv6_address_cmd);
2989 install_element(INTERFACE_NODE, &no_ipv6_address_cmd);
2990 #ifdef HAVE_NETLINK
2991 install_element(INTERFACE_NODE, &ip_address_label_cmd);
2992 install_element(INTERFACE_NODE, &no_ip_address_label_cmd);
2993 #endif /* HAVE_NETLINK */
2994 install_element(INTERFACE_NODE, &link_params_cmd);
2995 install_default(LINK_PARAMS_NODE);
2996 install_element(LINK_PARAMS_NODE, &link_params_enable_cmd);
2997 install_element(LINK_PARAMS_NODE, &no_link_params_enable_cmd);
2998 install_element(LINK_PARAMS_NODE, &link_params_metric_cmd);
2999 install_element(LINK_PARAMS_NODE, &no_link_params_metric_cmd);
3000 install_element(LINK_PARAMS_NODE, &link_params_maxbw_cmd);
3001 install_element(LINK_PARAMS_NODE, &link_params_max_rsv_bw_cmd);
3002 install_element(LINK_PARAMS_NODE, &link_params_unrsv_bw_cmd);
3003 install_element(LINK_PARAMS_NODE, &link_params_admin_grp_cmd);
3004 install_element(LINK_PARAMS_NODE, &no_link_params_admin_grp_cmd);
3005 install_element(LINK_PARAMS_NODE, &link_params_inter_as_cmd);
3006 install_element(LINK_PARAMS_NODE, &no_link_params_inter_as_cmd);
3007 install_element(LINK_PARAMS_NODE, &link_params_delay_cmd);
3008 install_element(LINK_PARAMS_NODE, &no_link_params_delay_cmd);
3009 install_element(LINK_PARAMS_NODE, &link_params_delay_var_cmd);
3010 install_element(LINK_PARAMS_NODE, &no_link_params_delay_var_cmd);
3011 install_element(LINK_PARAMS_NODE, &link_params_pkt_loss_cmd);
3012 install_element(LINK_PARAMS_NODE, &no_link_params_pkt_loss_cmd);
3013 install_element(LINK_PARAMS_NODE, &link_params_ava_bw_cmd);
3014 install_element(LINK_PARAMS_NODE, &no_link_params_ava_bw_cmd);
3015 install_element(LINK_PARAMS_NODE, &link_params_res_bw_cmd);
3016 install_element(LINK_PARAMS_NODE, &no_link_params_res_bw_cmd);
3017 install_element(LINK_PARAMS_NODE, &link_params_use_bw_cmd);
3018 install_element(LINK_PARAMS_NODE, &no_link_params_use_bw_cmd);
3019 install_element(LINK_PARAMS_NODE, &exit_link_params_cmd);
3020 }