1 /* A generic nexthop structure
2 * Copyright (C) 2013 Cumulus Networks, Inc.
4 * This file is part of Quagga.
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
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.
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
28 #include "sockunion.h"
35 DEFINE_MTYPE_STATIC(LIB
, NEXTHOP
, "Nexthop")
36 DEFINE_MTYPE_STATIC(LIB
, NH_LABEL
, "Nexthop label")
38 /* check if nexthops are same, non-recursive */
39 int nexthop_same_no_recurse(const struct nexthop
*next1
,
40 const struct nexthop
*next2
)
42 if (next1
->type
!= next2
->type
)
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
))
50 if (next1
->ifindex
&& (next1
->ifindex
!= next2
->ifindex
))
53 case NEXTHOP_TYPE_IFINDEX
:
54 if (next1
->ifindex
!= next2
->ifindex
)
57 case NEXTHOP_TYPE_IPV6
:
58 if (!IPV6_ADDR_SAME(&next1
->gate
.ipv6
, &next2
->gate
.ipv6
))
61 case NEXTHOP_TYPE_IPV6_IFINDEX
:
62 if (!IPV6_ADDR_SAME(&next1
->gate
.ipv6
, &next2
->gate
.ipv6
))
64 if (next1
->ifindex
!= next2
->ifindex
)
75 nexthop_same_firsthop (struct nexthop
*next1
, struct nexthop
*next2
)
77 int type1
= NEXTHOP_FIRSTHOPTYPE(next1
->type
);
78 int type2
= NEXTHOP_FIRSTHOPTYPE(next2
->type
);
84 case NEXTHOP_TYPE_IPV4_IFINDEX
:
85 if (! IPV4_ADDR_SAME (&next1
->gate
.ipv4
, &next2
->gate
.ipv4
))
87 if (next1
->ifindex
!= next2
->ifindex
)
90 case NEXTHOP_TYPE_IFINDEX
:
91 if (next1
->ifindex
!= next2
->ifindex
)
94 case NEXTHOP_TYPE_IPV6_IFINDEX
:
95 if (! IPV6_ADDR_SAME (&next1
->gate
.ipv6
, &next2
->gate
.ipv6
))
97 if (next1
->ifindex
!= next2
->ifindex
)
108 * nexthop_type_to_str
110 const char *nexthop_type_to_str(enum nexthop_types_t nh_type
)
112 static const char *desc
[] = {
113 "none", "Directly connected",
114 "IPv4 nexthop", "IPv4 nexthop with ifindex",
115 "IPv6 nexthop", "IPv6 nexthop with ifindex",
119 return desc
[nh_type
];
123 * Check if the labels match for the 2 nexthops specified.
125 int nexthop_labels_match(struct nexthop
*nh1
, struct nexthop
*nh2
)
127 struct nexthop_label
*nhl1
, *nhl2
;
129 nhl1
= nh1
->nh_label
;
130 nhl2
= nh2
->nh_label
;
134 if (nhl1
->num_labels
!= nhl2
->num_labels
)
137 if (memcmp(nhl1
->label
, nhl2
->label
, nhl1
->num_labels
))
143 struct nexthop
*nexthop_new(void)
145 return XCALLOC(MTYPE_NEXTHOP
, sizeof(struct nexthop
));
148 /* Add nexthop to the end of a nexthop list. */
149 void nexthop_add(struct nexthop
**target
, struct nexthop
*nexthop
)
151 struct nexthop
*last
;
153 for (last
= *target
; last
&& last
->next
; last
= last
->next
)
156 last
->next
= nexthop
;
159 nexthop
->prev
= last
;
162 void copy_nexthops(struct nexthop
**tnh
, struct nexthop
*nh
,
163 struct nexthop
*rparent
)
165 struct nexthop
*nexthop
;
168 for (nh1
= nh
; nh1
; nh1
= nh1
->next
) {
169 nexthop
= nexthop_new();
170 nexthop
->ifindex
= nh1
->ifindex
;
171 nexthop
->type
= nh1
->type
;
172 nexthop
->flags
= nh1
->flags
;
173 memcpy(&nexthop
->gate
, &nh1
->gate
, sizeof(nh1
->gate
));
174 memcpy(&nexthop
->src
, &nh1
->src
, sizeof(nh1
->src
));
175 memcpy(&nexthop
->rmap_src
, &nh1
->rmap_src
, sizeof(nh1
->rmap_src
));
176 nexthop
->rparent
= rparent
;
178 nexthop_add_labels(nexthop
, nh1
->nh_label_type
,
179 nh1
->nh_label
->num_labels
,
180 &nh1
->nh_label
->label
[0]);
181 nexthop_add(tnh
, nexthop
);
183 if (CHECK_FLAG(nh1
->flags
, NEXTHOP_FLAG_RECURSIVE
))
184 copy_nexthops(&nexthop
->resolved
, nh1
->resolved
,
190 void nexthop_free(struct nexthop
*nexthop
)
192 nexthop_del_labels(nexthop
);
193 if (nexthop
->resolved
)
194 nexthops_free(nexthop
->resolved
);
195 XFREE(MTYPE_NEXTHOP
, nexthop
);
198 /* Frees a list of nexthops */
199 void nexthops_free(struct nexthop
*nexthop
)
201 struct nexthop
*nh
, *next
;
203 for (nh
= nexthop
; nh
; nh
= next
) {
209 /* Update nexthop with label information. */
210 void nexthop_add_labels(struct nexthop
*nexthop
, enum lsp_types_t type
,
211 u_int8_t num_labels
, mpls_label_t
*label
)
213 struct nexthop_label
*nh_label
;
216 nexthop
->nh_label_type
= type
;
217 nh_label
= XCALLOC(MTYPE_NH_LABEL
,
218 sizeof(struct nexthop_label
)
219 + num_labels
* sizeof(mpls_label_t
));
220 nh_label
->num_labels
= num_labels
;
221 for (i
= 0; i
< num_labels
; i
++)
222 nh_label
->label
[i
] = *(label
+ i
);
223 nexthop
->nh_label
= nh_label
;
226 /* Free label information of nexthop, if present. */
227 void nexthop_del_labels(struct nexthop
*nexthop
)
229 if (nexthop
->nh_label
) {
230 XFREE(MTYPE_NH_LABEL
, nexthop
->nh_label
);
231 nexthop
->nh_label_type
= ZEBRA_LSP_NONE
;
235 const char *nexthop2str(struct nexthop
*nexthop
, char *str
, int size
)
237 switch (nexthop
->type
) {
238 case NEXTHOP_TYPE_IFINDEX
:
239 snprintf(str
, size
, "if %u", nexthop
->ifindex
);
241 case NEXTHOP_TYPE_IPV4
:
242 snprintf(str
, size
, "%s", inet_ntoa(nexthop
->gate
.ipv4
));
244 case NEXTHOP_TYPE_IPV4_IFINDEX
:
245 snprintf(str
, size
, "%s if %u", inet_ntoa(nexthop
->gate
.ipv4
),
248 case NEXTHOP_TYPE_IPV6
:
249 snprintf(str
, size
, "%s", inet6_ntoa(nexthop
->gate
.ipv6
));
251 case NEXTHOP_TYPE_IPV6_IFINDEX
:
252 snprintf(str
, size
, "%s if %u", inet6_ntoa(nexthop
->gate
.ipv6
),
255 case NEXTHOP_TYPE_BLACKHOLE
:
256 snprintf(str
, size
, "blackhole");
259 snprintf(str
, size
, "unknown");
267 * Iteration step for ALL_NEXTHOPS macro:
268 * This is the tricky part. Check if `nexthop' has
269 * NEXTHOP_FLAG_RECURSIVE set. If yes, this implies that `nexthop' has
270 * at least one nexthop attached to `nexthop->resolved', which will be
273 * If NEXTHOP_FLAG_RECURSIVE is not set, `nexthop' will progress in its
274 * current chain. In case its current chain end is reached, it will move
275 * upwards in the recursion levels and progress there. Whenever a step
276 * forward in a chain is done, recursion will be checked again.
277 * In a nustshell, it's equivalent to a pre-traversal order assuming that
278 * left branch is 'resolved' and right branch is 'next':
279 * https://en.wikipedia.org/wiki/Tree_traversal#/media/File:Sorted_binary_tree_preorder.svg
281 struct nexthop
*nexthop_next(struct nexthop
*nexthop
)
283 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
284 return nexthop
->resolved
;
287 return nexthop
->next
;
289 for (struct nexthop
*par
= nexthop
->rparent
; par
; par
= par
->rparent
)
296 unsigned int nexthop_level(struct nexthop
*nexthop
)
300 for (struct nexthop
*par
= nexthop
->rparent
; par
; par
= par
->rparent
)