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 */
40 nexthop_same_no_recurse (struct nexthop
*next1
, struct nexthop
*next2
)
42 if (next1
->type
!= next2
->type
)
47 case NEXTHOP_TYPE_IPV4
:
48 case NEXTHOP_TYPE_IPV4_IFINDEX
:
49 if (! IPV4_ADDR_SAME (&next1
->gate
.ipv4
, &next2
->gate
.ipv4
))
51 if (next1
->ifindex
&& (next1
->ifindex
!= next2
->ifindex
))
54 case NEXTHOP_TYPE_IFINDEX
:
55 if (next1
->ifindex
!= next2
->ifindex
)
58 case NEXTHOP_TYPE_IPV6
:
59 if (! IPV6_ADDR_SAME (&next1
->gate
.ipv6
, &next2
->gate
.ipv6
))
62 case NEXTHOP_TYPE_IPV6_IFINDEX
:
63 if (! IPV6_ADDR_SAME (&next1
->gate
.ipv6
, &next2
->gate
.ipv6
))
65 if (next1
->ifindex
!= next2
->ifindex
)
79 nexthop_type_to_str (enum nexthop_types_t nh_type
)
81 static const char *desc
[] = {
85 "IPv4 nexthop with ifindex",
87 "IPv6 nexthop with ifindex",
95 * Check if the labels match for the 2 nexthops specified.
98 nexthop_labels_match (struct nexthop
*nh1
, struct nexthop
*nh2
)
100 struct nexthop_label
*nhl1
, *nhl2
;
102 nhl1
= nh1
->nh_label
;
103 nhl2
= nh2
->nh_label
;
104 if ((nhl1
&& !nhl2
) || (!nhl1
&& nhl2
))
107 if (nhl1
->num_labels
!= nhl2
->num_labels
)
110 if (memcmp (nhl1
->label
, nhl2
->label
, nhl1
->num_labels
))
119 return XCALLOC (MTYPE_NEXTHOP
, sizeof (struct nexthop
));
122 /* Add nexthop to the end of a nexthop list. */
124 nexthop_add (struct nexthop
**target
, struct nexthop
*nexthop
)
126 struct nexthop
*last
;
128 for (last
= *target
; last
&& last
->next
; last
= last
->next
)
131 last
->next
= nexthop
;
134 nexthop
->prev
= last
;
138 copy_nexthops (struct nexthop
**tnh
, struct nexthop
*nh
)
140 struct nexthop
*nexthop
;
143 for (nh1
= nh
; nh1
; nh1
= nh1
->next
)
145 nexthop
= nexthop_new();
146 nexthop
->flags
= nh
->flags
;
147 nexthop
->type
= nh
->type
;
148 nexthop
->ifindex
= nh
->ifindex
;
149 memcpy(&(nexthop
->gate
), &(nh
->gate
), sizeof(union g_addr
));
150 memcpy(&(nexthop
->src
), &(nh
->src
), sizeof(union g_addr
));
152 nexthop_add_labels (nexthop
, nh
->nh_label_type
,
153 nh
->nh_label
->num_labels
, &nh
->nh_label
->label
[0]);
154 nexthop_add(tnh
, nexthop
);
156 if (CHECK_FLAG(nh1
->flags
, NEXTHOP_FLAG_RECURSIVE
))
157 copy_nexthops(&nexthop
->resolved
, nh1
->resolved
);
163 nexthop_free (struct nexthop
*nexthop
)
165 nexthop_del_labels (nexthop
);
166 if (nexthop
->resolved
)
167 nexthops_free(nexthop
->resolved
);
168 XFREE (MTYPE_NEXTHOP
, nexthop
);
171 /* Frees a list of nexthops */
173 nexthops_free (struct nexthop
*nexthop
)
175 struct nexthop
*nh
, *next
;
177 for (nh
= nexthop
; nh
; nh
= next
)
184 /* Update nexthop with label information. */
186 nexthop_add_labels (struct nexthop
*nexthop
, enum lsp_types_t type
,
187 u_int8_t num_labels
, mpls_label_t
*label
)
189 struct nexthop_label
*nh_label
;
192 nexthop
->nh_label_type
= type
;
193 nh_label
= XCALLOC (MTYPE_NH_LABEL
, sizeof (struct nexthop_label
) +
194 num_labels
* sizeof (mpls_label_t
));
195 nh_label
->num_labels
= num_labels
;
196 for (i
= 0; i
< num_labels
; i
++)
197 nh_label
->label
[i
] = *(label
+ i
);
198 nexthop
->nh_label
= nh_label
;
201 /* Free label information of nexthop, if present. */
203 nexthop_del_labels (struct nexthop
*nexthop
)
205 if (nexthop
->nh_label
)
207 XFREE (MTYPE_NH_LABEL
, nexthop
->nh_label
);
208 nexthop
->nh_label_type
= ZEBRA_LSP_NONE
;
213 nexthop2str (struct nexthop
*nexthop
, char *str
, int size
)
215 switch (nexthop
->type
)
217 case NEXTHOP_TYPE_IFINDEX
:
218 snprintf (str
, size
, "if %u", nexthop
->ifindex
);
220 case NEXTHOP_TYPE_IPV4
:
221 snprintf (str
, size
, "%s", inet_ntoa (nexthop
->gate
.ipv4
));
223 case NEXTHOP_TYPE_IPV4_IFINDEX
:
224 snprintf (str
, size
, "%s if %u",
225 inet_ntoa (nexthop
->gate
.ipv4
), nexthop
->ifindex
);
227 case NEXTHOP_TYPE_IPV6
:
228 snprintf (str
, size
, "%s", inet6_ntoa (nexthop
->gate
.ipv6
));
230 case NEXTHOP_TYPE_IPV6_IFINDEX
:
231 snprintf (str
, size
, "%s if %u",
232 inet6_ntoa (nexthop
->gate
.ipv6
), nexthop
->ifindex
);
234 case NEXTHOP_TYPE_BLACKHOLE
:
235 snprintf (str
, size
, "blackhole");
238 snprintf (str
, size
, "unknown");