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