]> git.proxmox.com Git - mirror_frr.git/blob - lib/if.c
Merge branch 'cmaster-next' into vtysh-grammar
[mirror_frr.git] / lib / if.c
1
2 /*
3 * Interface functions.
4 * Copyright (C) 1997, 98 Kunihiro Ishiguro
5 *
6 * This file is part of GNU Zebra.
7 *
8 * GNU Zebra is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published
10 * by the Free Software Foundation; either version 2, or (at your
11 * option) any later version.
12 *
13 * GNU Zebra is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with GNU Zebra; see the file COPYING. If not, write to the
20 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 * Boston, MA 02111-1307, USA.
22 */
23
24 #include <zebra.h>
25
26 #include "linklist.h"
27 #include "vector.h"
28 #include "vty.h"
29 #include "command.h"
30 #include "vrf.h"
31 #include "if.h"
32 #include "sockunion.h"
33 #include "prefix.h"
34 #include "memory.h"
35 #include "table.h"
36 #include "buffer.h"
37 #include "str.h"
38 #include "log.h"
39
40 DEFINE_MTYPE( LIB, IF, "Interface")
41 DEFINE_MTYPE_STATIC(LIB, CONNECTED, "Connected")
42 DEFINE_MTYPE_STATIC(LIB, NBR_CONNECTED, "Neighbor Connected")
43 DEFINE_MTYPE( LIB, CONNECTED_LABEL, "Connected interface label")
44 DEFINE_MTYPE_STATIC(LIB, IF_LINK_PARAMS, "Informational Link Parameters")
45
46 DEFINE_QOBJ_TYPE(interface)
47
48 /* List of interfaces in only the default VRF */
49 int ptm_enable = 0;
50
51 /* One for each program. This structure is needed to store hooks. */
52 struct if_master
53 {
54 int (*if_new_hook) (struct interface *);
55 int (*if_delete_hook) (struct interface *);
56 } if_master = {0,};
57
58 /* Compare interface names, returning an integer greater than, equal to, or
59 * less than 0, (following the strcmp convention), according to the
60 * relationship between ifp1 and ifp2. Interface names consist of an
61 * alphabetic prefix and a numeric suffix. The primary sort key is
62 * lexicographic by name, and then numeric by number. No number sorts
63 * before all numbers. Examples: de0 < de1, de100 < fxp0 < xl0, devpty <
64 * devpty0, de0 < del0
65 */
66 int
67 if_cmp_name_func (char *p1, char *p2)
68 {
69 unsigned int l1, l2;
70 long int x1, x2;
71 int res;
72
73 while (*p1 && *p2) {
74 /* look up to any number */
75 l1 = strcspn(p1, "0123456789");
76 l2 = strcspn(p2, "0123456789");
77
78 /* name lengths are different -> compare names */
79 if (l1 != l2)
80 return (strcmp(p1, p2));
81
82 /* Note that this relies on all numbers being less than all letters, so
83 * that de0 < del0.
84 */
85 res = strncmp(p1, p2, l1);
86
87 /* names are different -> compare them */
88 if (res)
89 return res;
90
91 /* with identical name part, go to numeric part */
92 p1 += l1;
93 p2 += l1;
94
95 if (!*p1)
96 return -1;
97 if (!*p2)
98 return 1;
99
100 x1 = strtol(p1, &p1, 10);
101 x2 = strtol(p2, &p2, 10);
102
103 /* let's compare numbers now */
104 if (x1 < x2)
105 return -1;
106 if (x1 > x2)
107 return 1;
108
109 /* numbers were equal, lets do it again..
110 (it happens with name like "eth123.456:789") */
111 }
112 if (*p1)
113 return 1;
114 if (*p2)
115 return -1;
116 return 0;
117 }
118
119 static int
120 if_cmp_func (struct interface *ifp1, struct interface *ifp2)
121 {
122 return if_cmp_name_func (ifp1->name, ifp2->name);
123 }
124
125 /* Create new interface structure. */
126 struct interface *
127 if_create_vrf (const char *name, int namelen, vrf_id_t vrf_id)
128 {
129 struct interface *ifp;
130 struct list *intf_list = vrf_iflist_get (vrf_id);
131
132 ifp = XCALLOC (MTYPE_IF, sizeof (struct interface));
133 ifp->ifindex = IFINDEX_INTERNAL;
134
135 assert (name);
136 assert (namelen <= INTERFACE_NAMSIZ); /* Need space for '\0' at end. */
137 strncpy (ifp->name, name, namelen);
138 ifp->name[namelen] = '\0';
139 ifp->vrf_id = vrf_id;
140 if (if_lookup_by_name_vrf (ifp->name, vrf_id) == NULL)
141 listnode_add_sort (intf_list, ifp);
142 else
143 zlog_err("if_create(%s): corruption detected -- interface with this "
144 "name exists already in VRF %u!", ifp->name, vrf_id);
145 ifp->connected = list_new ();
146 ifp->connected->del = (void (*) (void *)) connected_free;
147
148 ifp->nbr_connected = list_new ();
149 ifp->nbr_connected->del = (void (*) (void *)) nbr_connected_free;
150
151 /* Enable Link-detection by default */
152 SET_FLAG(ifp->status, ZEBRA_INTERFACE_LINKDETECTION);
153
154 QOBJ_REG (ifp, interface);
155
156 if (if_master.if_new_hook)
157 (*if_master.if_new_hook) (ifp);
158
159 return ifp;
160 }
161
162 struct interface *
163 if_create (const char *name, int namelen)
164 {
165 return if_create_vrf (name, namelen, VRF_DEFAULT);
166 }
167
168 /* Create new interface structure. */
169 void
170 if_update_vrf (struct interface *ifp, const char *name, int namelen, vrf_id_t vrf_id)
171 {
172 struct list *intf_list = vrf_iflist_get (vrf_id);
173
174 /* remove interface from old master vrf list */
175 if (vrf_iflist (ifp->vrf_id))
176 listnode_delete (vrf_iflist (ifp->vrf_id), ifp);
177
178 assert (name);
179 assert (namelen <= INTERFACE_NAMSIZ); /* Need space for '\0' at end. */
180 strncpy (ifp->name, name, namelen);
181 ifp->name[namelen] = '\0';
182 ifp->vrf_id = vrf_id;
183 if (if_lookup_by_name_vrf (ifp->name, vrf_id) == NULL)
184 listnode_add_sort (intf_list, ifp);
185 else
186 zlog_err("if_create(%s): corruption detected -- interface with this "
187 "name exists already in VRF %u!", ifp->name, vrf_id);
188
189 return;
190 }
191
192
193 /* Delete interface structure. */
194 void
195 if_delete_retain (struct interface *ifp)
196 {
197 if (if_master.if_delete_hook)
198 (*if_master.if_delete_hook) (ifp);
199
200 QOBJ_UNREG (ifp);
201
202 /* Free connected address list */
203 list_delete_all_node (ifp->connected);
204
205 /* Free connected nbr address list */
206 list_delete_all_node (ifp->nbr_connected);
207 }
208
209 /* Delete and free interface structure. */
210 void
211 if_delete (struct interface *ifp)
212 {
213 listnode_delete (vrf_iflist (ifp->vrf_id), ifp);
214
215 if_delete_retain(ifp);
216
217 list_free (ifp->connected);
218 list_free (ifp->nbr_connected);
219
220 if_link_params_free (ifp);
221
222 XFREE (MTYPE_IF, ifp);
223 }
224
225 /* Add hook to interface master. */
226 void
227 if_add_hook (int type, int (*func)(struct interface *ifp))
228 {
229 switch (type) {
230 case IF_NEW_HOOK:
231 if_master.if_new_hook = func;
232 break;
233 case IF_DELETE_HOOK:
234 if_master.if_delete_hook = func;
235 break;
236 default:
237 break;
238 }
239 }
240
241 /* Interface existance check by index. */
242 struct interface *
243 if_lookup_by_index_vrf (ifindex_t ifindex, vrf_id_t vrf_id)
244 {
245 struct listnode *node;
246 struct interface *ifp;
247
248 for (ALL_LIST_ELEMENTS_RO (vrf_iflist (vrf_id), node, ifp))
249 {
250 if (ifp->ifindex == ifindex)
251 return ifp;
252 }
253 return NULL;
254 }
255
256 struct interface *
257 if_lookup_by_index (ifindex_t ifindex)
258 {
259 return if_lookup_by_index_vrf (ifindex, VRF_DEFAULT);
260 }
261
262 const char *
263 ifindex2ifname_vrf (ifindex_t ifindex, vrf_id_t vrf_id)
264 {
265 struct interface *ifp;
266
267 return ((ifp = if_lookup_by_index_vrf (ifindex, vrf_id)) != NULL) ?
268 ifp->name : "unknown";
269 }
270
271 const char *
272 ifindex2ifname (ifindex_t ifindex)
273 {
274 return ifindex2ifname_vrf (ifindex, VRF_DEFAULT);
275 }
276
277 ifindex_t
278 ifname2ifindex_vrf (const char *name, vrf_id_t vrf_id)
279 {
280 struct interface *ifp;
281
282 return ((ifp = if_lookup_by_name_vrf (name, vrf_id)) != NULL) ? ifp->ifindex
283 : IFINDEX_INTERNAL;
284 }
285
286 ifindex_t
287 ifname2ifindex (const char *name)
288 {
289 return ifname2ifindex_vrf (name, VRF_DEFAULT);
290 }
291
292 /* Interface existance check by interface name. */
293 struct interface *
294 if_lookup_by_name_vrf (const char *name, vrf_id_t vrf_id)
295 {
296 struct listnode *node;
297 struct interface *ifp;
298
299 if (name)
300 for (ALL_LIST_ELEMENTS_RO (vrf_iflist (vrf_id), node, ifp))
301 {
302 if (strcmp(name, ifp->name) == 0)
303 return ifp;
304 }
305 return NULL;
306 }
307
308 struct interface *
309 if_lookup_by_name_all_vrf (const char *name)
310 {
311 struct interface *ifp;
312 struct vrf *vrf = NULL;
313 vrf_iter_t iter;
314
315 for (iter = vrf_first (); iter != VRF_ITER_INVALID; iter = vrf_next (iter))
316 {
317 vrf = vrf_iter2vrf (iter);
318 ifp = if_lookup_by_name_vrf (name, vrf->vrf_id);
319 if (ifp)
320 return ifp;
321 }
322
323 return NULL;
324 }
325
326 struct interface *
327 if_lookup_by_name (const char *name)
328 {
329 return if_lookup_by_name_vrf (name, VRF_DEFAULT);
330 }
331
332 struct interface *
333 if_lookup_by_name_len_vrf (const char *name, size_t namelen, vrf_id_t vrf_id)
334 {
335 struct listnode *node;
336 struct interface *ifp;
337
338 if (namelen > INTERFACE_NAMSIZ)
339 return NULL;
340
341 for (ALL_LIST_ELEMENTS_RO (vrf_iflist (vrf_id), node, ifp))
342 {
343 if (!memcmp(name, ifp->name, namelen) && (ifp->name[namelen] == '\0'))
344 return ifp;
345 }
346 return NULL;
347 }
348
349 struct interface *
350 if_lookup_by_name_len(const char *name, size_t namelen)
351 {
352 return if_lookup_by_name_len_vrf (name, namelen, VRF_DEFAULT);
353 }
354
355 /* Lookup interface by IPv4 address. */
356 struct interface *
357 if_lookup_exact_address_vrf (void *src, int family, vrf_id_t vrf_id)
358 {
359 struct listnode *node;
360 struct listnode *cnode;
361 struct interface *ifp;
362 struct prefix *p;
363 struct connected *c;
364
365 for (ALL_LIST_ELEMENTS_RO (vrf_iflist (vrf_id), node, ifp))
366 {
367 for (ALL_LIST_ELEMENTS_RO (ifp->connected, cnode, c))
368 {
369 p = c->address;
370
371 if (p && (p->family == family))
372 {
373 if (family == AF_INET)
374 {
375 if (IPV4_ADDR_SAME (&p->u.prefix4, (struct in_addr *)src))
376 return ifp;
377 }
378 else if (family == AF_INET6)
379 {
380 if (IPV6_ADDR_SAME (&p->u.prefix4, (struct in6_addr *)src))
381 return ifp;
382 }
383 }
384 }
385 }
386 return NULL;
387 }
388
389 struct interface *
390 if_lookup_exact_address (void *src, int family)
391 {
392 return if_lookup_exact_address_vrf (src, family, VRF_DEFAULT);
393 }
394
395 /* Lookup interface by IPv4 address. */
396 struct interface *
397 if_lookup_address_vrf (void *matchaddr, int family, vrf_id_t vrf_id)
398 {
399 struct listnode *node;
400 struct prefix addr;
401 int bestlen = 0;
402 struct listnode *cnode;
403 struct interface *ifp;
404 struct connected *c;
405 struct interface *match;
406
407 if (family == AF_INET)
408 {
409 addr.family = AF_INET;
410 addr.u.prefix4 = *((struct in_addr *)matchaddr);
411 addr.prefixlen = IPV4_MAX_BITLEN;
412 }
413 else if (family == AF_INET6)
414 {
415 addr.family = AF_INET6;
416 addr.u.prefix6 = *((struct in6_addr *)matchaddr);
417 addr.prefixlen = IPV6_MAX_BITLEN;
418 }
419
420 match = NULL;
421
422 for (ALL_LIST_ELEMENTS_RO (vrf_iflist (vrf_id), node, ifp))
423 {
424 for (ALL_LIST_ELEMENTS_RO (ifp->connected, cnode, c))
425 {
426 if (c->address && (c->address->family == AF_INET) &&
427 prefix_match(CONNECTED_PREFIX(c), &addr) &&
428 (c->address->prefixlen > bestlen))
429 {
430 bestlen = c->address->prefixlen;
431 match = ifp;
432 }
433 }
434 }
435 return match;
436 }
437
438 struct interface *
439 if_lookup_address (void *matchaddr, int family)
440 {
441 return if_lookup_address_vrf (matchaddr, family, VRF_DEFAULT);
442 }
443
444 /* Lookup interface by prefix */
445 struct interface *
446 if_lookup_prefix_vrf (struct prefix *prefix, vrf_id_t vrf_id)
447 {
448 struct listnode *node;
449 struct listnode *cnode;
450 struct interface *ifp;
451 struct connected *c;
452
453 for (ALL_LIST_ELEMENTS_RO (vrf_iflist (vrf_id), node, ifp))
454 {
455 for (ALL_LIST_ELEMENTS_RO (ifp->connected, cnode, c))
456 {
457 if (prefix_cmp(c->address, prefix) == 0)
458 {
459 return ifp;
460 }
461 }
462 }
463 return NULL;
464 }
465
466 struct interface *
467 if_lookup_prefix (struct prefix *prefix)
468 {
469 return if_lookup_prefix_vrf (prefix, VRF_DEFAULT);
470 }
471
472 /* Get interface by name if given name interface doesn't exist create
473 one. */
474 struct interface *
475 if_get_by_name_vrf (const char *name, vrf_id_t vrf_id)
476 {
477 struct interface *ifp;
478
479 return ((ifp = if_lookup_by_name_vrf (name, vrf_id)) != NULL) ? ifp :
480 if_create_vrf (name, strlen(name), vrf_id);
481 }
482
483 struct interface *
484 if_get_by_name (const char *name)
485 {
486 return if_get_by_name_vrf (name, VRF_DEFAULT);
487 }
488
489 struct interface *
490 if_get_by_name_len_vrf (const char *name, size_t namelen, vrf_id_t vrf_id, int vty)
491 {
492 struct interface *ifp;
493 struct listnode *node;
494 struct vrf *vrf = NULL;
495 vrf_iter_t iter;
496
497 ifp = if_lookup_by_name_len_vrf (name, namelen, vrf_id);
498 if (ifp)
499 return ifp;
500
501 /* Didn't find the interface on that vrf. Defined on a different one? */
502 for (iter = vrf_first (); iter != VRF_ITER_INVALID; iter = vrf_next (iter))
503 {
504 vrf = vrf_iter2vrf(iter);
505 for (ALL_LIST_ELEMENTS_RO (vrf_iflist (vrf->vrf_id), node, ifp))
506 {
507 if (!memcmp(name, ifp->name, namelen) && (ifp->name[namelen] == '\0'))
508 {
509 /* Found a match. If the interface command was entered in vty without a
510 * VRF (passed as VRF_DEFAULT), accept the ifp we found. If a vrf was
511 * entered and there is a mismatch, reject it if from vty. If it came
512 * from the kernel by way of zclient, believe it and update
513 * the ifp accordingly.
514 */
515 if (vty)
516 {
517 if (vrf_id == VRF_DEFAULT)
518 return ifp;
519 return NULL;
520 }
521 else
522 {
523 if_update_vrf (ifp, name, namelen, vrf_id);
524 return ifp;
525 }
526 }
527 }
528 }
529 return (if_create_vrf (name, namelen, vrf_id));
530 }
531
532 struct interface *
533 if_get_by_name_len (const char *name, size_t namelen)
534 {
535 return if_get_by_name_len_vrf (name, namelen, VRF_DEFAULT, 0);
536 }
537
538 /* Does interface up ? */
539 int
540 if_is_up (struct interface *ifp)
541 {
542 return ifp->flags & IFF_UP;
543 }
544
545 /* Is interface running? */
546 int
547 if_is_running (struct interface *ifp)
548 {
549 return ifp->flags & IFF_RUNNING;
550 }
551
552 /* Is the interface operative, eg. either UP & RUNNING
553 or UP & !ZEBRA_INTERFACE_LINK_DETECTION and
554 if ptm checking is enabled, then ptm check has passed */
555 int
556 if_is_operative (struct interface *ifp)
557 {
558 return ((ifp->flags & IFF_UP) &&
559 (((ifp->flags & IFF_RUNNING) &&
560 (ifp->ptm_status || !ifp->ptm_enable)) ||
561 !CHECK_FLAG(ifp->status, ZEBRA_INTERFACE_LINKDETECTION)));
562 }
563
564 /* Is the interface operative, eg. either UP & RUNNING
565 or UP & !ZEBRA_INTERFACE_LINK_DETECTION, without PTM check */
566 int
567 if_is_no_ptm_operative (struct interface *ifp)
568 {
569 return ((ifp->flags & IFF_UP) &&
570 ((ifp->flags & IFF_RUNNING) ||
571 !CHECK_FLAG(ifp->status, ZEBRA_INTERFACE_LINKDETECTION)));
572 }
573
574 /* Is this loopback interface ? */
575 int
576 if_is_loopback (struct interface *ifp)
577 {
578 /* XXX: Do this better, eg what if IFF_WHATEVER means X on platform M
579 * but Y on platform N?
580 */
581 return (ifp->flags & (IFF_LOOPBACK|IFF_NOXMIT|IFF_VIRTUAL));
582 }
583
584 /* Does this interface support broadcast ? */
585 int
586 if_is_broadcast (struct interface *ifp)
587 {
588 return ifp->flags & IFF_BROADCAST;
589 }
590
591 /* Does this interface support broadcast ? */
592 int
593 if_is_pointopoint (struct interface *ifp)
594 {
595 return ifp->flags & IFF_POINTOPOINT;
596 }
597
598 /* Does this interface support multicast ? */
599 int
600 if_is_multicast (struct interface *ifp)
601 {
602 return ifp->flags & IFF_MULTICAST;
603 }
604
605 /* Printout flag information into log */
606 const char *
607 if_flag_dump (unsigned long flag)
608 {
609 int separator = 0;
610 static char logbuf[BUFSIZ];
611
612 #define IFF_OUT_LOG(X,STR) \
613 if (flag & (X)) \
614 { \
615 if (separator) \
616 strlcat (logbuf, ",", BUFSIZ); \
617 else \
618 separator = 1; \
619 strlcat (logbuf, STR, BUFSIZ); \
620 }
621
622 strlcpy (logbuf, "<", BUFSIZ);
623 IFF_OUT_LOG (IFF_UP, "UP");
624 IFF_OUT_LOG (IFF_BROADCAST, "BROADCAST");
625 IFF_OUT_LOG (IFF_DEBUG, "DEBUG");
626 IFF_OUT_LOG (IFF_LOOPBACK, "LOOPBACK");
627 IFF_OUT_LOG (IFF_POINTOPOINT, "POINTOPOINT");
628 IFF_OUT_LOG (IFF_NOTRAILERS, "NOTRAILERS");
629 IFF_OUT_LOG (IFF_RUNNING, "RUNNING");
630 IFF_OUT_LOG (IFF_NOARP, "NOARP");
631 IFF_OUT_LOG (IFF_PROMISC, "PROMISC");
632 IFF_OUT_LOG (IFF_ALLMULTI, "ALLMULTI");
633 IFF_OUT_LOG (IFF_OACTIVE, "OACTIVE");
634 IFF_OUT_LOG (IFF_SIMPLEX, "SIMPLEX");
635 IFF_OUT_LOG (IFF_LINK0, "LINK0");
636 IFF_OUT_LOG (IFF_LINK1, "LINK1");
637 IFF_OUT_LOG (IFF_LINK2, "LINK2");
638 IFF_OUT_LOG (IFF_MULTICAST, "MULTICAST");
639 IFF_OUT_LOG (IFF_NOXMIT, "NOXMIT");
640 IFF_OUT_LOG (IFF_NORTEXCH, "NORTEXCH");
641 IFF_OUT_LOG (IFF_VIRTUAL, "VIRTUAL");
642 IFF_OUT_LOG (IFF_IPV4, "IPv4");
643 IFF_OUT_LOG (IFF_IPV6, "IPv6");
644
645 strlcat (logbuf, ">", BUFSIZ);
646
647 return logbuf;
648 #undef IFF_OUT_LOG
649 }
650
651 /* For debugging */
652 static void
653 if_dump (const struct interface *ifp)
654 {
655 struct listnode *node;
656 struct connected *c __attribute__((unused));
657
658 for (ALL_LIST_ELEMENTS_RO (ifp->connected, node, c))
659 zlog_info ("Interface %s vrf %u index %d metric %d mtu %d "
660 "mtu6 %d %s",
661 ifp->name, ifp->vrf_id, ifp->ifindex, ifp->metric, ifp->mtu,
662 ifp->mtu6, if_flag_dump (ifp->flags));
663 }
664
665 /* Interface printing for all interface. */
666 void
667 if_dump_all (void)
668 {
669 struct list *intf_list;
670 struct listnode *node;
671 void *p;
672 vrf_iter_t iter;
673
674 for (iter = vrf_first (); iter != VRF_ITER_INVALID; iter = vrf_next (iter))
675 if ((intf_list = vrf_iter2iflist (iter)) != NULL)
676 for (ALL_LIST_ELEMENTS_RO (intf_list, node, p))
677 if_dump (p);
678 }
679
680 DEFUN (interface_desc,
681 interface_desc_cmd,
682 "description LINE...",
683 "Interface specific description\n"
684 "Characters describing this interface\n")
685 {
686 int idx_line = 1;
687 VTY_DECLVAR_CONTEXT (interface, ifp);
688
689 if (ifp->desc)
690 XFREE (MTYPE_TMP, ifp->desc);
691 ifp->desc = argv_concat(argv, argc, idx_line);
692
693 return CMD_SUCCESS;
694 }
695
696 DEFUN (no_interface_desc,
697 no_interface_desc_cmd,
698 "no description",
699 NO_STR
700 "Interface specific description\n")
701 {
702 VTY_DECLVAR_CONTEXT (interface, ifp);
703
704 if (ifp->desc)
705 XFREE (MTYPE_TMP, ifp->desc);
706 ifp->desc = NULL;
707
708 return CMD_SUCCESS;
709 }
710
711 #ifdef SUNOS_5
712 /* Need to handle upgrade from SUNWzebra to Quagga. SUNWzebra created
713 * a seperate struct interface for each logical interface, so config
714 * file may be full of 'interface fooX:Y'. Solaris however does not
715 * expose logical interfaces via PF_ROUTE, so trying to track logical
716 * interfaces can be fruitless, for that reason Quagga only tracks
717 * the primary IP interface.
718 *
719 * We try accomodate SUNWzebra by:
720 * - looking up the interface name, to see whether it exists, if so
721 * its useable
722 * - for protocol daemons, this could only because zebra told us of
723 * the interface
724 * - for zebra, only because it learnt from kernel
725 * - if not:
726 * - search the name to see if it contains a sub-ipif / logical interface
727 * seperator, the ':' char. If it does:
728 * - text up to that char must be the primary name - get that name.
729 * if not:
730 * - no idea, just get the name in its entirety.
731 */
732 static struct interface *
733 if_sunwzebra_get (const char *name, size_t nlen, vrf_id_t vrf_id)
734 {
735 struct interface *ifp;
736 size_t seppos = 0;
737
738 if ( (ifp = if_lookup_by_name_len_vrf (name, nlen, vrf_id)) != NULL)
739 return ifp;
740
741 /* hunt the primary interface name... */
742 while (seppos < nlen && name[seppos] != ':')
743 seppos++;
744
745 /* Wont catch seperator as last char, e.g. 'foo0:' but thats invalid */
746 if (seppos < nlen)
747 return if_get_by_name_len_vrf (name, seppos, vrf_id, 1);
748 else
749 return if_get_by_name_len_vrf (name, nlen, vrf_id, 1);
750 }
751 #endif /* SUNOS_5 */
752
753 DEFUN (interface,
754 interface_cmd,
755 "interface IFNAME [vrf NAME]",
756 "Select an interface to configure\n"
757 "Interface's name\n"
758 VRF_CMD_HELP_STR)
759 {
760 int idx_ifname = 1;
761 int idx_vrf = 3;
762 const char *ifname = argv[idx_ifname]->arg;
763 const char *vrfname = (argc > 2) ? argv[idx_vrf]->arg : NULL;
764
765 struct interface *ifp;
766 size_t sl;
767 vrf_id_t vrf_id = VRF_DEFAULT;
768
769 if ((sl = strlen(ifname)) > INTERFACE_NAMSIZ)
770 {
771 vty_out (vty, "%% Interface name %s is invalid: length exceeds "
772 "%d characters%s",
773 ifname, INTERFACE_NAMSIZ, VTY_NEWLINE);
774 return CMD_WARNING;
775 }
776
777 /*Pending: need proper vrf name based lookup/(possible creation of VRF)
778 Imagine forward reference of a vrf by name in this interface config */
779 if (vrfname)
780 VRF_GET_ID (vrf_id, vrfname);
781
782 #ifdef SUNOS_5
783 ifp = if_sunwzebra_get (ifname, sl, vrf_id);
784 #else
785 ifp = if_get_by_name_len_vrf (ifname, sl, vrf_id, 1);
786 #endif /* SUNOS_5 */
787
788 if (!ifp)
789 {
790 vty_out (vty, "%% interface %s not in %s%s", ifname, vrfname, VTY_NEWLINE);
791 return CMD_WARNING;
792 }
793 VTY_PUSH_CONTEXT_COMPAT (INTERFACE_NODE, ifp);
794
795 return CMD_SUCCESS;
796 }
797
798 DEFUN_NOSH (no_interface,
799 no_interface_cmd,
800 "no interface IFNAME [vrf NAME]",
801 NO_STR
802 "Delete a pseudo interface's configuration\n"
803 "Interface's name\n"
804 VRF_CMD_HELP_STR)
805 {
806 const char *ifname = argv[2]->arg;
807 const char *vrfname = (argc > 3) ? argv[3]->arg : NULL;
808
809 // deleting interface
810 struct interface *ifp;
811 vrf_id_t vrf_id = VRF_DEFAULT;
812
813 if (argc > 3)
814 VRF_GET_ID (vrf_id, vrfname);
815
816 ifp = if_lookup_by_name_vrf (ifname, vrf_id);
817
818 if (ifp == NULL)
819 {
820 vty_out (vty, "%% Interface %s does not exist%s", ifname, VTY_NEWLINE);
821 return CMD_WARNING;
822 }
823
824 if (CHECK_FLAG (ifp->status, ZEBRA_INTERFACE_ACTIVE))
825 {
826 vty_out (vty, "%% Only inactive interfaces can be deleted%s",
827 VTY_NEWLINE);
828 return CMD_WARNING;
829 }
830
831 if_delete(ifp);
832
833 return CMD_SUCCESS;
834 }
835
836 DEFUN (vrf,
837 vrf_cmd,
838 "vrf NAME",
839 "Select a VRF to configure\n"
840 "VRF's name\n")
841 {
842 int idx_name = 1;
843 const char *vrfname = argv[idx_name]->arg;
844
845 struct vrf *vrfp;
846 size_t sl;
847
848 if ((sl = strlen(vrfname)) > VRF_NAMSIZ)
849 {
850 vty_out (vty, "%% VRF name %s is invalid: length exceeds "
851 "%d characters%s",
852 vrfname, VRF_NAMSIZ, VTY_NEWLINE);
853 return CMD_WARNING;
854 }
855
856 vrfp = vrf_get (VRF_UNKNOWN, vrfname);
857
858 VTY_PUSH_CONTEXT_COMPAT (VRF_NODE, vrfp);
859
860 return CMD_SUCCESS;
861 }
862
863 DEFUN_NOSH (no_vrf,
864 no_vrf_cmd,
865 "no vrf NAME",
866 NO_STR
867 "Delete a pseudo VRF's configuration\n"
868 "VRF's name\n")
869 {
870 const char *vrfname = argv[2]->arg;
871
872 struct vrf *vrfp;
873
874 vrfp = vrf_list_lookup_by_name (vrfname);;
875
876 if (vrfp == NULL)
877 {
878 vty_out (vty, "%% VRF %s does not exist%s", vrfname, VTY_NEWLINE);
879 return CMD_WARNING;
880 }
881
882 if (CHECK_FLAG (vrfp->status, VRF_ACTIVE))
883 {
884 vty_out (vty, "%% Only inactive VRFs can be deleted%s",
885 VTY_NEWLINE);
886 return CMD_WARNING;
887 }
888
889 vrf_delete(vrfp);
890
891 return CMD_SUCCESS;
892 }
893
894
895 /* For debug purpose. */
896 DEFUN (show_address,
897 show_address_cmd,
898 "show address [vrf NAME]",
899 SHOW_STR
900 "address\n"
901 VRF_CMD_HELP_STR)
902 {
903 int idx_vrf = 3;
904 struct listnode *node;
905 struct listnode *node2;
906 struct interface *ifp;
907 struct connected *ifc;
908 struct prefix *p;
909 vrf_id_t vrf_id = VRF_DEFAULT;
910
911 if (argc > 2)
912 VRF_GET_ID (vrf_id, argv[idx_vrf]->arg);
913
914 for (ALL_LIST_ELEMENTS_RO (vrf_iflist (vrf_id), node, ifp))
915 {
916 for (ALL_LIST_ELEMENTS_RO (ifp->connected, node2, ifc))
917 {
918 p = ifc->address;
919
920 if (p->family == AF_INET)
921 vty_out (vty, "%s/%d%s", inet_ntoa (p->u.prefix4), p->prefixlen,
922 VTY_NEWLINE);
923 }
924 }
925 return CMD_SUCCESS;
926 }
927
928 DEFUN (show_address_vrf_all,
929 show_address_vrf_all_cmd,
930 "show address vrf all",
931 SHOW_STR
932 "address\n"
933 VRF_ALL_CMD_HELP_STR)
934 {
935 struct list *intf_list;
936 struct listnode *node;
937 struct listnode *node2;
938 struct interface *ifp;
939 struct connected *ifc;
940 struct prefix *p;
941 vrf_iter_t iter;
942
943 for (iter = vrf_first (); iter != VRF_ITER_INVALID; iter = vrf_next (iter))
944 {
945 intf_list = vrf_iter2iflist (iter);
946 if (!intf_list || !listcount (intf_list))
947 continue;
948
949 vty_out (vty, "%sVRF %u%s%s", VTY_NEWLINE, vrf_iter2id (iter),
950 VTY_NEWLINE, VTY_NEWLINE);
951
952 for (ALL_LIST_ELEMENTS_RO (intf_list, node, ifp))
953 {
954 for (ALL_LIST_ELEMENTS_RO (ifp->connected, node2, ifc))
955 {
956 p = ifc->address;
957
958 if (p->family == AF_INET)
959 vty_out (vty, "%s/%d%s", inet_ntoa (p->u.prefix4), p->prefixlen,
960 VTY_NEWLINE);
961 }
962 }
963 }
964 return CMD_SUCCESS;
965 }
966
967 /* Allocate connected structure. */
968 struct connected *
969 connected_new (void)
970 {
971 return XCALLOC (MTYPE_CONNECTED, sizeof (struct connected));
972 }
973
974 /* Allocate nbr connected structure. */
975 struct nbr_connected *
976 nbr_connected_new (void)
977 {
978 return XCALLOC (MTYPE_NBR_CONNECTED, sizeof (struct nbr_connected));
979 }
980
981 /* Free connected structure. */
982 void
983 connected_free (struct connected *connected)
984 {
985 if (connected->address)
986 prefix_free (connected->address);
987
988 if (connected->destination)
989 prefix_free (connected->destination);
990
991 if (connected->label)
992 XFREE (MTYPE_CONNECTED_LABEL, connected->label);
993
994 XFREE (MTYPE_CONNECTED, connected);
995 }
996
997 /* Free nbr connected structure. */
998 void
999 nbr_connected_free (struct nbr_connected *connected)
1000 {
1001 if (connected->address)
1002 prefix_free (connected->address);
1003
1004 XFREE (MTYPE_NBR_CONNECTED, connected);
1005 }
1006
1007 /* If same interface nbr address already exists... */
1008 struct nbr_connected *
1009 nbr_connected_check (struct interface *ifp, struct prefix *p)
1010 {
1011 struct nbr_connected *ifc;
1012 struct listnode *node;
1013
1014 for (ALL_LIST_ELEMENTS_RO (ifp->nbr_connected, node, ifc))
1015 if (prefix_same (ifc->address, p))
1016 return ifc;
1017
1018 return NULL;
1019 }
1020
1021 /* Print if_addr structure. */
1022 static void __attribute__ ((unused))
1023 connected_log (struct connected *connected, char *str)
1024 {
1025 struct prefix *p;
1026 struct interface *ifp;
1027 char logbuf[BUFSIZ];
1028 char buf[BUFSIZ];
1029
1030 ifp = connected->ifp;
1031 p = connected->address;
1032
1033 snprintf (logbuf, BUFSIZ, "%s interface %s vrf %u %s %s/%d ",
1034 str, ifp->name, ifp->vrf_id, prefix_family_str (p),
1035 inet_ntop (p->family, &p->u.prefix, buf, BUFSIZ),
1036 p->prefixlen);
1037
1038 p = connected->destination;
1039 if (p)
1040 {
1041 strncat (logbuf, inet_ntop (p->family, &p->u.prefix, buf, BUFSIZ),
1042 BUFSIZ - strlen(logbuf));
1043 }
1044 zlog (NULL, LOG_INFO, "%s", logbuf);
1045 }
1046
1047 /* Print if_addr structure. */
1048 static void __attribute__ ((unused))
1049 nbr_connected_log (struct nbr_connected *connected, char *str)
1050 {
1051 struct prefix *p;
1052 struct interface *ifp;
1053 char logbuf[BUFSIZ];
1054 char buf[BUFSIZ];
1055
1056 ifp = connected->ifp;
1057 p = connected->address;
1058
1059 snprintf (logbuf, BUFSIZ, "%s interface %s %s %s/%d ",
1060 str, ifp->name, prefix_family_str (p),
1061 inet_ntop (p->family, &p->u.prefix, buf, BUFSIZ),
1062 p->prefixlen);
1063
1064 zlog (NULL, LOG_INFO, "%s", logbuf);
1065 }
1066
1067 /* If two connected address has same prefix return 1. */
1068 static int
1069 connected_same_prefix (struct prefix *p1, struct prefix *p2)
1070 {
1071 if (p1->family == p2->family)
1072 {
1073 if (p1->family == AF_INET &&
1074 IPV4_ADDR_SAME (&p1->u.prefix4, &p2->u.prefix4))
1075 return 1;
1076 #ifdef HAVE_IPV6
1077 if (p1->family == AF_INET6 &&
1078 IPV6_ADDR_SAME (&p1->u.prefix6, &p2->u.prefix6))
1079 return 1;
1080 #endif /* HAVE_IPV6 */
1081 }
1082 return 0;
1083 }
1084
1085 struct connected *
1086 connected_lookup_prefix_exact (struct interface *ifp, struct prefix *p)
1087 {
1088 struct listnode *node;
1089 struct listnode *next;
1090 struct connected *ifc;
1091
1092 for (node = listhead (ifp->connected); node; node = next)
1093 {
1094 ifc = listgetdata (node);
1095 next = node->next;
1096
1097 if (connected_same_prefix (ifc->address, p))
1098 return ifc;
1099 }
1100 return NULL;
1101 }
1102
1103 struct connected *
1104 connected_delete_by_prefix (struct interface *ifp, struct prefix *p)
1105 {
1106 struct listnode *node;
1107 struct listnode *next;
1108 struct connected *ifc;
1109
1110 /* In case of same prefix come, replace it with new one. */
1111 for (node = listhead (ifp->connected); node; node = next)
1112 {
1113 ifc = listgetdata (node);
1114 next = node->next;
1115
1116 if (connected_same_prefix (ifc->address, p))
1117 {
1118 listnode_delete (ifp->connected, ifc);
1119 return ifc;
1120 }
1121 }
1122 return NULL;
1123 }
1124
1125 /* Find the address on our side that will be used when packets
1126 are sent to dst. */
1127 struct connected *
1128 connected_lookup_prefix (struct interface *ifp, struct prefix *addr)
1129 {
1130 struct listnode *cnode;
1131 struct connected *c;
1132 struct connected *match;
1133
1134 match = NULL;
1135
1136 for (ALL_LIST_ELEMENTS_RO (ifp->connected, cnode, c))
1137 {
1138 if (c->address && (c->address->family == addr->family) &&
1139 prefix_match(CONNECTED_PREFIX(c), addr) &&
1140 (!match || (c->address->prefixlen > match->address->prefixlen)))
1141 match = c;
1142 }
1143 return match;
1144 }
1145
1146 struct connected *
1147 connected_add_by_prefix (struct interface *ifp, struct prefix *p,
1148 struct prefix *destination)
1149 {
1150 struct connected *ifc;
1151
1152 /* Allocate new connected address. */
1153 ifc = connected_new ();
1154 ifc->ifp = ifp;
1155
1156 /* Fetch interface address */
1157 ifc->address = prefix_new();
1158 memcpy (ifc->address, p, sizeof(struct prefix));
1159
1160 /* Fetch dest address */
1161 if (destination)
1162 {
1163 ifc->destination = prefix_new();
1164 memcpy (ifc->destination, destination, sizeof(struct prefix));
1165 }
1166
1167 /* Add connected address to the interface. */
1168 listnode_add (ifp->connected, ifc);
1169 return ifc;
1170 }
1171
1172 #ifndef HAVE_IF_NAMETOINDEX
1173 ifindex_t
1174 if_nametoindex (const char *name)
1175 {
1176 struct interface *ifp;
1177
1178 return ((ifp = if_lookup_by_name_len(name, strnlen(name, IFNAMSIZ))) != NULL)
1179 ? ifp->ifindex : 0;
1180 }
1181 #endif
1182
1183 #ifndef HAVE_IF_INDEXTONAME
1184 char *
1185 if_indextoname (ifindex_t ifindex, char *name)
1186 {
1187 struct interface *ifp;
1188
1189 if (!(ifp = if_lookup_by_index(ifindex)))
1190 return NULL;
1191 strncpy (name, ifp->name, IFNAMSIZ);
1192 return ifp->name;
1193 }
1194 #endif
1195
1196 #if 0 /* this route_table of struct connected's is unused
1197 * however, it would be good to use a route_table rather than
1198 * a list..
1199 */
1200 /* Interface looking up by interface's address. */
1201 /* Interface's IPv4 address reverse lookup table. */
1202 struct route_table *ifaddr_ipv4_table;
1203 /* struct route_table *ifaddr_ipv6_table; */
1204
1205 static void
1206 ifaddr_ipv4_add (struct in_addr *ifaddr, struct interface *ifp)
1207 {
1208 struct route_node *rn;
1209 struct prefix_ipv4 p;
1210
1211 p.family = AF_INET;
1212 p.prefixlen = IPV4_MAX_PREFIXLEN;
1213 p.prefix = *ifaddr;
1214
1215 rn = route_node_get (ifaddr_ipv4_table, (struct prefix *) &p);
1216 if (rn)
1217 {
1218 route_unlock_node (rn);
1219 zlog_info ("ifaddr_ipv4_add(): address %s is already added",
1220 inet_ntoa (*ifaddr));
1221 return;
1222 }
1223 rn->info = ifp;
1224 }
1225
1226 static void
1227 ifaddr_ipv4_delete (struct in_addr *ifaddr, struct interface *ifp)
1228 {
1229 struct route_node *rn;
1230 struct prefix_ipv4 p;
1231
1232 p.family = AF_INET;
1233 p.prefixlen = IPV4_MAX_PREFIXLEN;
1234 p.prefix = *ifaddr;
1235
1236 rn = route_node_lookup (ifaddr_ipv4_table, (struct prefix *) &p);
1237 if (! rn)
1238 {
1239 zlog_info ("ifaddr_ipv4_delete(): can't find address %s",
1240 inet_ntoa (*ifaddr));
1241 return;
1242 }
1243 rn->info = NULL;
1244 route_unlock_node (rn);
1245 route_unlock_node (rn);
1246 }
1247
1248 /* Lookup interface by interface's IP address or interface index. */
1249 static struct interface *
1250 ifaddr_ipv4_lookup (struct in_addr *addr, ifindex_t ifindex)
1251 {
1252 struct prefix_ipv4 p;
1253 struct route_node *rn;
1254 struct interface *ifp;
1255
1256 if (addr)
1257 {
1258 p.family = AF_INET;
1259 p.prefixlen = IPV4_MAX_PREFIXLEN;
1260 p.prefix = *addr;
1261
1262 rn = route_node_lookup (ifaddr_ipv4_table, (struct prefix *) &p);
1263 if (! rn)
1264 return NULL;
1265
1266 ifp = rn->info;
1267 route_unlock_node (rn);
1268 return ifp;
1269 }
1270 else
1271 return if_lookup_by_index(ifindex);
1272 }
1273 #endif /* ifaddr_ipv4_table */
1274
1275 /* Initialize interface list. */
1276 void
1277 if_init (struct list **intf_list)
1278 {
1279 *intf_list = list_new ();
1280 #if 0
1281 ifaddr_ipv4_table = route_table_init ();
1282 #endif /* ifaddr_ipv4_table */
1283
1284 (*intf_list)->cmp = (int (*)(void *, void *))if_cmp_func;
1285 }
1286
1287 void
1288 if_terminate (struct list **intf_list)
1289 {
1290 for (;;)
1291 {
1292 struct interface *ifp;
1293
1294 ifp = listnode_head (*intf_list);
1295 if (ifp == NULL)
1296 break;
1297
1298 if (ifp->node)
1299 {
1300 ifp->node->info = NULL;
1301 route_unlock_node (ifp->node);
1302 }
1303
1304 if_delete (ifp);
1305 }
1306
1307 list_delete (*intf_list);
1308 *intf_list = NULL;
1309 }
1310
1311 const char *
1312 if_link_type_str (enum zebra_link_type llt)
1313 {
1314 switch (llt)
1315 {
1316 #define llts(T,S) case (T): return (S)
1317 llts(ZEBRA_LLT_UNKNOWN, "Unknown");
1318 llts(ZEBRA_LLT_ETHER, "Ethernet");
1319 llts(ZEBRA_LLT_EETHER, "Experimental Ethernet");
1320 llts(ZEBRA_LLT_AX25, "AX.25 Level 2");
1321 llts(ZEBRA_LLT_PRONET, "PROnet token ring");
1322 llts(ZEBRA_LLT_IEEE802, "IEEE 802.2 Ethernet/TR/TB");
1323 llts(ZEBRA_LLT_ARCNET, "ARCnet");
1324 llts(ZEBRA_LLT_APPLETLK, "AppleTalk");
1325 llts(ZEBRA_LLT_DLCI, "Frame Relay DLCI");
1326 llts(ZEBRA_LLT_ATM, "ATM");
1327 llts(ZEBRA_LLT_METRICOM, "Metricom STRIP");
1328 llts(ZEBRA_LLT_IEEE1394, "IEEE 1394 IPv4");
1329 llts(ZEBRA_LLT_EUI64, "EUI-64");
1330 llts(ZEBRA_LLT_INFINIBAND, "InfiniBand");
1331 llts(ZEBRA_LLT_SLIP, "SLIP");
1332 llts(ZEBRA_LLT_CSLIP, "Compressed SLIP");
1333 llts(ZEBRA_LLT_SLIP6, "SLIPv6");
1334 llts(ZEBRA_LLT_CSLIP6, "Compressed SLIPv6");
1335 llts(ZEBRA_LLT_ROSE, "ROSE packet radio");
1336 llts(ZEBRA_LLT_X25, "CCITT X.25");
1337 llts(ZEBRA_LLT_PPP, "PPP");
1338 llts(ZEBRA_LLT_CHDLC, "Cisco HDLC");
1339 llts(ZEBRA_LLT_RAWHDLC, "Raw HDLC");
1340 llts(ZEBRA_LLT_LAPB, "LAPB");
1341 llts(ZEBRA_LLT_IPIP, "IPIP Tunnel");
1342 llts(ZEBRA_LLT_IPIP6, "IPIP6 Tunnel");
1343 llts(ZEBRA_LLT_FRAD, "FRAD");
1344 llts(ZEBRA_LLT_SKIP, "SKIP vif");
1345 llts(ZEBRA_LLT_LOOPBACK, "Loopback");
1346 llts(ZEBRA_LLT_LOCALTLK, "Localtalk");
1347 llts(ZEBRA_LLT_FDDI, "FDDI");
1348 llts(ZEBRA_LLT_SIT, "IPv6-in-IPv4 SIT");
1349 llts(ZEBRA_LLT_IPDDP, "IP-in-DDP tunnel");
1350 llts(ZEBRA_LLT_IPGRE, "GRE over IP");
1351 llts(ZEBRA_LLT_PIMREG, "PIMSM registration");
1352 llts(ZEBRA_LLT_HIPPI, "HiPPI");
1353 llts(ZEBRA_LLT_IRDA, "IrDA");
1354 llts(ZEBRA_LLT_FCPP, "Fibre-Channel PtP");
1355 llts(ZEBRA_LLT_FCAL, "Fibre-Channel Arbitrated Loop");
1356 llts(ZEBRA_LLT_FCPL, "Fibre-Channel Public Loop");
1357 llts(ZEBRA_LLT_FCFABRIC, "Fibre-Channel Fabric");
1358 llts(ZEBRA_LLT_IEEE802_TR, "IEEE 802.2 Token Ring");
1359 llts(ZEBRA_LLT_IEEE80211, "IEEE 802.11");
1360 llts(ZEBRA_LLT_IEEE80211_RADIOTAP, "IEEE 802.11 Radiotap");
1361 llts(ZEBRA_LLT_IEEE802154, "IEEE 802.15.4");
1362 llts(ZEBRA_LLT_IEEE802154_PHY, "IEEE 802.15.4 Phy");
1363 default:
1364 zlog_warn ("Unknown value %d", llt);
1365 return "Unknown type!";
1366 #undef llts
1367 }
1368 return NULL;
1369 }
1370
1371 struct if_link_params *
1372 if_link_params_get (struct interface *ifp)
1373 {
1374 int i;
1375
1376 if (ifp->link_params != NULL)
1377 return ifp->link_params;
1378
1379 struct if_link_params *iflp = XCALLOC(MTYPE_IF_LINK_PARAMS,
1380 sizeof (struct if_link_params));
1381 if (iflp == NULL) return NULL;
1382
1383 /* Set TE metric == standard metric */
1384 iflp->te_metric = ifp->metric;
1385
1386 /* Compute default bandwidth based on interface */
1387 int bw = (float)((ifp->bandwidth ? ifp->bandwidth : DEFAULT_BANDWIDTH)
1388 * TE_KILO_BIT / TE_BYTE);
1389
1390 /* Set Max, Reservable and Unreserved Bandwidth */
1391 iflp->max_bw = bw;
1392 iflp->max_rsv_bw = bw;
1393 for (i = 0; i < MAX_CLASS_TYPE; i++)
1394 iflp->unrsv_bw[i] = bw;
1395
1396 /* Update Link parameters status */
1397 iflp->lp_status = LP_TE | LP_MAX_BW | LP_MAX_RSV_BW | LP_UNRSV_BW;
1398
1399 /* Finally attach newly created Link Parameters */
1400 ifp->link_params = iflp;
1401
1402 return iflp;
1403 }
1404
1405 void
1406 if_link_params_free (struct interface *ifp)
1407 {
1408 if (ifp->link_params == NULL) return;
1409 XFREE(MTYPE_IF_LINK_PARAMS, ifp->link_params);
1410 ifp->link_params = NULL;
1411 }