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
)
74 int nexthop_same_firsthop(struct nexthop
*next1
, struct nexthop
*next2
)
76 int type1
= NEXTHOP_FIRSTHOPTYPE(next1
->type
);
77 int type2
= NEXTHOP_FIRSTHOPTYPE(next2
->type
);
82 case NEXTHOP_TYPE_IPV4_IFINDEX
:
83 if (!IPV4_ADDR_SAME(&next1
->gate
.ipv4
, &next2
->gate
.ipv4
))
85 if (next1
->ifindex
!= next2
->ifindex
)
88 case NEXTHOP_TYPE_IFINDEX
:
89 if (next1
->ifindex
!= next2
->ifindex
)
92 case NEXTHOP_TYPE_IPV6_IFINDEX
:
93 if (!IPV6_ADDR_SAME(&next1
->gate
.ipv6
, &next2
->gate
.ipv6
))
95 if (next1
->ifindex
!= next2
->ifindex
)
106 * nexthop_type_to_str
108 const char *nexthop_type_to_str(enum nexthop_types_t nh_type
)
110 static const char *desc
[] = {
111 "none", "Directly connected",
112 "IPv4 nexthop", "IPv4 nexthop with ifindex",
113 "IPv6 nexthop", "IPv6 nexthop with ifindex",
117 return desc
[nh_type
];
121 * Check if the labels match for the 2 nexthops specified.
123 int nexthop_labels_match(struct nexthop
*nh1
, struct nexthop
*nh2
)
125 struct mpls_label_stack
*nhl1
, *nhl2
;
127 nhl1
= nh1
->nh_label
;
128 nhl2
= nh2
->nh_label
;
132 if (nhl1
->num_labels
!= nhl2
->num_labels
)
135 if (memcmp(nhl1
->label
, nhl2
->label
, nhl1
->num_labels
))
141 struct nexthop
*nexthop_new(void)
143 return XCALLOC(MTYPE_NEXTHOP
, sizeof(struct nexthop
));
147 void nexthop_free(struct nexthop
*nexthop
)
149 nexthop_del_labels(nexthop
);
150 if (nexthop
->resolved
)
151 nexthops_free(nexthop
->resolved
);
152 XFREE(MTYPE_NEXTHOP
, nexthop
);
155 /* Frees a list of nexthops */
156 void nexthops_free(struct nexthop
*nexthop
)
158 struct nexthop
*nh
, *next
;
160 for (nh
= nexthop
; nh
; nh
= next
) {
166 bool nexthop_same(const struct nexthop
*nh1
, const struct nexthop
*nh2
)
177 if (nh1
->vrf_id
!= nh2
->vrf_id
)
180 if (nh1
->type
!= nh2
->type
)
184 case NEXTHOP_TYPE_IFINDEX
:
185 if (nh1
->ifindex
!= nh2
->ifindex
)
188 case NEXTHOP_TYPE_IPV4
:
189 if (nh1
->gate
.ipv4
.s_addr
!= nh2
->gate
.ipv4
.s_addr
)
192 case NEXTHOP_TYPE_IPV4_IFINDEX
:
193 if (nh1
->gate
.ipv4
.s_addr
!= nh2
->gate
.ipv4
.s_addr
)
195 if (nh1
->ifindex
!= nh2
->ifindex
)
198 case NEXTHOP_TYPE_IPV6
:
199 if (memcmp(&nh1
->gate
.ipv6
, &nh2
->gate
.ipv6
, 16))
202 case NEXTHOP_TYPE_IPV6_IFINDEX
:
203 if (memcmp(&nh1
->gate
.ipv6
, &nh2
->gate
.ipv6
, 16))
205 if (nh1
->ifindex
!= nh2
->ifindex
)
208 case NEXTHOP_TYPE_BLACKHOLE
:
209 if (nh1
->bh_type
!= nh2
->bh_type
)
217 /* Update nexthop with label information. */
218 void nexthop_add_labels(struct nexthop
*nexthop
, enum lsp_types_t type
,
219 uint8_t num_labels
, mpls_label_t
*label
)
221 struct mpls_label_stack
*nh_label
;
224 nexthop
->nh_label_type
= type
;
225 nh_label
= XCALLOC(MTYPE_NH_LABEL
,
226 sizeof(struct mpls_label_stack
)
227 + num_labels
* sizeof(mpls_label_t
));
228 nh_label
->num_labels
= num_labels
;
229 for (i
= 0; i
< num_labels
; i
++)
230 nh_label
->label
[i
] = *(label
+ i
);
231 nexthop
->nh_label
= nh_label
;
234 /* Free label information of nexthop, if present. */
235 void nexthop_del_labels(struct nexthop
*nexthop
)
237 if (nexthop
->nh_label
) {
238 XFREE(MTYPE_NH_LABEL
, nexthop
->nh_label
);
239 nexthop
->nh_label_type
= ZEBRA_LSP_NONE
;
243 const char *nexthop2str(struct nexthop
*nexthop
, char *str
, int size
)
245 switch (nexthop
->type
) {
246 case NEXTHOP_TYPE_IFINDEX
:
247 snprintf(str
, size
, "if %u", nexthop
->ifindex
);
249 case NEXTHOP_TYPE_IPV4
:
250 snprintf(str
, size
, "%s", inet_ntoa(nexthop
->gate
.ipv4
));
252 case NEXTHOP_TYPE_IPV4_IFINDEX
:
253 snprintf(str
, size
, "%s if %u", inet_ntoa(nexthop
->gate
.ipv4
),
256 case NEXTHOP_TYPE_IPV6
:
257 snprintf(str
, size
, "%s", inet6_ntoa(nexthop
->gate
.ipv6
));
259 case NEXTHOP_TYPE_IPV6_IFINDEX
:
260 snprintf(str
, size
, "%s if %u", inet6_ntoa(nexthop
->gate
.ipv6
),
263 case NEXTHOP_TYPE_BLACKHOLE
:
264 snprintf(str
, size
, "blackhole");
267 snprintf(str
, size
, "unknown");
275 * Iteration step for ALL_NEXTHOPS macro:
276 * This is the tricky part. Check if `nexthop' has
277 * NEXTHOP_FLAG_RECURSIVE set. If yes, this implies that `nexthop' has
278 * at least one nexthop attached to `nexthop->resolved', which will be
281 * If NEXTHOP_FLAG_RECURSIVE is not set, `nexthop' will progress in its
282 * current chain. In case its current chain end is reached, it will move
283 * upwards in the recursion levels and progress there. Whenever a step
284 * forward in a chain is done, recursion will be checked again.
285 * In a nustshell, it's equivalent to a pre-traversal order assuming that
286 * left branch is 'resolved' and right branch is 'next':
287 * https://en.wikipedia.org/wiki/Tree_traversal#/media/File:Sorted_binary_tree_preorder.svg
289 struct nexthop
*nexthop_next(struct nexthop
*nexthop
)
291 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
292 return nexthop
->resolved
;
295 return nexthop
->next
;
297 for (struct nexthop
*par
= nexthop
->rparent
; par
; par
= par
->rparent
)
304 unsigned int nexthop_level(struct nexthop
*nexthop
)
308 for (struct nexthop
*par
= nexthop
->rparent
; par
; par
= par
->rparent
)