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