]> git.proxmox.com Git - mirror_frr.git/blob - lib/nexthop.c
Revert "lib: Fix handling of poll"
[mirror_frr.git] / lib / nexthop.c
1 /* A generic nexthop structure
2 * Copyright (C) 2013 Cumulus Networks, Inc.
3 *
4 * This file is part of Quagga.
5 *
6 * Quagga is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2, or (at your option) any
9 * later version.
10 *
11 * Quagga is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with Quagga; see the file COPYING. If not, write to the Free
18 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
19 * 02111-1307, USA.
20 */
21 #include <zebra.h>
22
23 #include "prefix.h"
24 #include "table.h"
25 #include "memory.h"
26 #include "str.h"
27 #include "command.h"
28 #include "if.h"
29 #include "log.h"
30 #include "sockunion.h"
31 #include "linklist.h"
32 #include "thread.h"
33 #include "prefix.h"
34 #include "nexthop.h"
35
36 /* check if nexthops are same, non-recursive */
37 int
38 nexthop_same_no_recurse (struct nexthop *next1, struct nexthop *next2)
39 {
40 if (next1->type != next2->type)
41 return 0;
42
43 switch (next1->type)
44 {
45 case NEXTHOP_TYPE_IPV4:
46 case NEXTHOP_TYPE_IPV4_IFINDEX:
47 if (! IPV4_ADDR_SAME (&next1->gate.ipv4, &next2->gate.ipv4))
48 return 0;
49 if (next1->ifindex && (next1->ifindex != next2->ifindex))
50 return 0;
51 break;
52 case NEXTHOP_TYPE_IFINDEX:
53 if (next1->ifindex != next2->ifindex)
54 return 0;
55 break;
56 case NEXTHOP_TYPE_IPV6:
57 if (! IPV6_ADDR_SAME (&next1->gate.ipv6, &next2->gate.ipv6))
58 return 0;
59 break;
60 case NEXTHOP_TYPE_IPV6_IFINDEX:
61 if (! IPV6_ADDR_SAME (&next1->gate.ipv6, &next2->gate.ipv6))
62 return 0;
63 if (next1->ifindex != next2->ifindex)
64 return 0;
65 break;
66 default:
67 /* do nothing */
68 break;
69 }
70 return 1;
71 }
72
73 /*
74 * nexthop_type_to_str
75 */
76 const char *
77 nexthop_type_to_str (enum nexthop_types_t nh_type)
78 {
79 static const char *desc[] = {
80 "none",
81 "Directly connected",
82 "Interface route",
83 "IPv4 nexthop",
84 "IPv4 nexthop with ifindex",
85 "IPv4 nexthop with ifname",
86 "IPv6 nexthop",
87 "IPv6 nexthop with ifindex",
88 "IPv6 nexthop with ifname",
89 "Null0 nexthop",
90 };
91
92 if (nh_type >= ZEBRA_NUM_OF (desc))
93 return "<Invalid nh type>";
94
95 return desc[nh_type];
96 }
97
98 struct nexthop *
99 nexthop_new (void)
100 {
101 return XCALLOC (MTYPE_NEXTHOP, sizeof (struct nexthop));
102 }
103
104 /* Add nexthop to the end of a nexthop list. */
105 void
106 nexthop_add (struct nexthop **target, struct nexthop *nexthop)
107 {
108 struct nexthop *last;
109
110 for (last = *target; last && last->next; last = last->next)
111 ;
112 if (last)
113 last->next = nexthop;
114 else
115 *target = nexthop;
116 nexthop->prev = last;
117 }
118
119 void
120 copy_nexthops (struct nexthop **tnh, struct nexthop *nh)
121 {
122 struct nexthop *nexthop;
123 struct nexthop *nh1;
124
125 for (nh1 = nh; nh1; nh1 = nh1->next)
126 {
127 nexthop = nexthop_new();
128 nexthop->flags = nh->flags;
129 nexthop->type = nh->type;
130 nexthop->ifindex = nh->ifindex;
131 memcpy(&(nexthop->gate), &(nh->gate), sizeof(union g_addr));
132 memcpy(&(nexthop->src), &(nh->src), sizeof(union g_addr));
133 nexthop_add(tnh, nexthop);
134
135 if (CHECK_FLAG(nh1->flags, NEXTHOP_FLAG_RECURSIVE))
136 copy_nexthops(&nexthop->resolved, nh1->resolved);
137 }
138 }
139
140 /* Free nexthop. */
141 void
142 nexthop_free (struct nexthop *nexthop)
143 {
144 if (nexthop->resolved)
145 nexthops_free(nexthop->resolved);
146 XFREE (MTYPE_NEXTHOP, nexthop);
147 }
148
149 /* Frees a list of nexthops */
150 void
151 nexthops_free (struct nexthop *nexthop)
152 {
153 struct nexthop *nh, *next;
154
155 for (nh = nexthop; nh; nh = next)
156 {
157 next = nh->next;
158 nexthop_free (nh);
159 }
160 }