]> git.proxmox.com Git - mirror_frr.git/blob - lib/nexthop.c
Merge pull request #1078 from dwalton76/ospfd-network-cmd-warning
[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 along
17 * with this program; see the file COPYING; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20 #include <zebra.h>
21
22 #include "prefix.h"
23 #include "table.h"
24 #include "memory.h"
25 #include "command.h"
26 #include "if.h"
27 #include "log.h"
28 #include "sockunion.h"
29 #include "linklist.h"
30 #include "thread.h"
31 #include "prefix.h"
32 #include "nexthop.h"
33 #include "mpls.h"
34
35 DEFINE_MTYPE_STATIC(LIB, NEXTHOP, "Nexthop")
36 DEFINE_MTYPE_STATIC(LIB, NH_LABEL, "Nexthop label")
37
38 /* check if nexthops are same, non-recursive */
39 int nexthop_same_no_recurse(const struct nexthop *next1,
40 const struct nexthop *next2)
41 {
42 if (next1->type != next2->type)
43 return 0;
44
45 switch (next1->type) {
46 case NEXTHOP_TYPE_IPV4:
47 case NEXTHOP_TYPE_IPV4_IFINDEX:
48 if (!IPV4_ADDR_SAME(&next1->gate.ipv4, &next2->gate.ipv4))
49 return 0;
50 if (next1->ifindex && (next1->ifindex != next2->ifindex))
51 return 0;
52 break;
53 case NEXTHOP_TYPE_IFINDEX:
54 if (next1->ifindex != next2->ifindex)
55 return 0;
56 break;
57 case NEXTHOP_TYPE_IPV6:
58 if (!IPV6_ADDR_SAME(&next1->gate.ipv6, &next2->gate.ipv6))
59 return 0;
60 break;
61 case NEXTHOP_TYPE_IPV6_IFINDEX:
62 if (!IPV6_ADDR_SAME(&next1->gate.ipv6, &next2->gate.ipv6))
63 return 0;
64 if (next1->ifindex != next2->ifindex)
65 return 0;
66 break;
67 default:
68 /* do nothing */
69 break;
70 }
71 return 1;
72 }
73
74 /*
75 * nexthop_type_to_str
76 */
77 const char *nexthop_type_to_str(enum nexthop_types_t nh_type)
78 {
79 static const char *desc[] = {
80 "none", "Directly connected",
81 "IPv4 nexthop", "IPv4 nexthop with ifindex",
82 "IPv6 nexthop", "IPv6 nexthop with ifindex",
83 "Null0 nexthop",
84 };
85
86 return desc[nh_type];
87 }
88
89 /*
90 * Check if the labels match for the 2 nexthops specified.
91 */
92 int nexthop_labels_match(struct nexthop *nh1, struct nexthop *nh2)
93 {
94 struct nexthop_label *nhl1, *nhl2;
95
96 nhl1 = nh1->nh_label;
97 nhl2 = nh2->nh_label;
98 if ((nhl1 && !nhl2) || (!nhl1 && nhl2))
99 return 0;
100
101 if (nhl1->num_labels != nhl2->num_labels)
102 return 0;
103
104 if (memcmp(nhl1->label, nhl2->label, nhl1->num_labels))
105 return 0;
106
107 return 1;
108 }
109
110 struct nexthop *nexthop_new(void)
111 {
112 return XCALLOC(MTYPE_NEXTHOP, sizeof(struct nexthop));
113 }
114
115 /* Add nexthop to the end of a nexthop list. */
116 void nexthop_add(struct nexthop **target, struct nexthop *nexthop)
117 {
118 struct nexthop *last;
119
120 for (last = *target; last && last->next; last = last->next)
121 ;
122 if (last)
123 last->next = nexthop;
124 else
125 *target = nexthop;
126 nexthop->prev = last;
127 }
128
129 void copy_nexthops(struct nexthop **tnh, struct nexthop *nh,
130 struct nexthop *rparent)
131 {
132 struct nexthop *nexthop;
133 struct nexthop *nh1;
134
135 for (nh1 = nh; nh1; nh1 = nh1->next) {
136 nexthop = nexthop_new();
137 nexthop->ifindex = nh1->ifindex;
138 nexthop->type = nh1->type;
139 nexthop->flags = nh1->flags;
140 memcpy(&nexthop->gate, &nh1->gate, sizeof(nh1->gate));
141 memcpy(&nexthop->src, &nh1->src, sizeof(nh1->src));
142 memcpy(&nexthop->rmap_src, &nh1->rmap_src, sizeof(nh1->rmap_src));
143 nexthop->rparent = rparent;
144 if (nh1->nh_label)
145 nexthop_add_labels(nexthop, nh1->nh_label_type,
146 nh1->nh_label->num_labels,
147 &nh1->nh_label->label[0]);
148 nexthop_add(tnh, nexthop);
149
150 if (CHECK_FLAG(nh1->flags, NEXTHOP_FLAG_RECURSIVE))
151 copy_nexthops(&nexthop->resolved, nh1->resolved,
152 nexthop);
153 }
154 }
155
156 /* Free nexthop. */
157 void nexthop_free(struct nexthop *nexthop)
158 {
159 nexthop_del_labels(nexthop);
160 if (nexthop->resolved)
161 nexthops_free(nexthop->resolved);
162 XFREE(MTYPE_NEXTHOP, nexthop);
163 }
164
165 /* Frees a list of nexthops */
166 void nexthops_free(struct nexthop *nexthop)
167 {
168 struct nexthop *nh, *next;
169
170 for (nh = nexthop; nh; nh = next) {
171 next = nh->next;
172 nexthop_free(nh);
173 }
174 }
175
176 /* Update nexthop with label information. */
177 void nexthop_add_labels(struct nexthop *nexthop, enum lsp_types_t type,
178 u_int8_t num_labels, mpls_label_t *label)
179 {
180 struct nexthop_label *nh_label;
181 int i;
182
183 nexthop->nh_label_type = type;
184 nh_label = XCALLOC(MTYPE_NH_LABEL,
185 sizeof(struct nexthop_label)
186 + num_labels * sizeof(mpls_label_t));
187 nh_label->num_labels = num_labels;
188 for (i = 0; i < num_labels; i++)
189 nh_label->label[i] = *(label + i);
190 nexthop->nh_label = nh_label;
191 }
192
193 /* Free label information of nexthop, if present. */
194 void nexthop_del_labels(struct nexthop *nexthop)
195 {
196 if (nexthop->nh_label) {
197 XFREE(MTYPE_NH_LABEL, nexthop->nh_label);
198 nexthop->nh_label_type = ZEBRA_LSP_NONE;
199 }
200 }
201
202 const char *nexthop2str(struct nexthop *nexthop, char *str, int size)
203 {
204 switch (nexthop->type) {
205 case NEXTHOP_TYPE_IFINDEX:
206 snprintf(str, size, "if %u", nexthop->ifindex);
207 break;
208 case NEXTHOP_TYPE_IPV4:
209 snprintf(str, size, "%s", inet_ntoa(nexthop->gate.ipv4));
210 break;
211 case NEXTHOP_TYPE_IPV4_IFINDEX:
212 snprintf(str, size, "%s if %u", inet_ntoa(nexthop->gate.ipv4),
213 nexthop->ifindex);
214 break;
215 case NEXTHOP_TYPE_IPV6:
216 snprintf(str, size, "%s", inet6_ntoa(nexthop->gate.ipv6));
217 break;
218 case NEXTHOP_TYPE_IPV6_IFINDEX:
219 snprintf(str, size, "%s if %u", inet6_ntoa(nexthop->gate.ipv6),
220 nexthop->ifindex);
221 break;
222 case NEXTHOP_TYPE_BLACKHOLE:
223 snprintf(str, size, "blackhole");
224 break;
225 default:
226 snprintf(str, size, "unknown");
227 break;
228 }
229
230 return str;
231 }
232
233 /*
234 * Iteration step for ALL_NEXTHOPS macro:
235 * This is the tricky part. Check if `nexthop' has
236 * NEXTHOP_FLAG_RECURSIVE set. If yes, this implies that `nexthop' has
237 * at least one nexthop attached to `nexthop->resolved', which will be
238 * the next one.
239 *
240 * If NEXTHOP_FLAG_RECURSIVE is not set, `nexthop' will progress in its
241 * current chain. In case its current chain end is reached, it will move
242 * upwards in the recursion levels and progress there. Whenever a step
243 * forward in a chain is done, recursion will be checked again.
244 * In a nustshell, it's equivalent to a pre-traversal order assuming that
245 * left branch is 'resolved' and right branch is 'next':
246 * https://en.wikipedia.org/wiki/Tree_traversal#/media/File:Sorted_binary_tree_preorder.svg
247 */
248 struct nexthop *nexthop_next(struct nexthop *nexthop)
249 {
250 if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_RECURSIVE))
251 return nexthop->resolved;
252
253 if (nexthop->next)
254 return nexthop->next;
255
256 for (struct nexthop *par = nexthop->rparent; par; par = par->rparent)
257 if (par->next)
258 return par->next;
259
260 return NULL;
261 }
262
263 unsigned int nexthop_level(struct nexthop *nexthop)
264 {
265 unsigned int rv = 0;
266
267 for (struct nexthop *par = nexthop->rparent; par; par = par->rparent)
268 rv++;
269
270 return rv;
271 }