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