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
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
29 #include "sockunion.h"
36 DEFINE_MTYPE_STATIC(LIB
, NEXTHOP
, "Nexthop")
37 DEFINE_MTYPE_STATIC(LIB
, NH_LABEL
, "Nexthop label")
39 /* check if nexthops are same, non-recursive */
41 nexthop_same_no_recurse (struct nexthop
*next1
, struct nexthop
*next2
)
43 if (next1
->type
!= next2
->type
)
48 case NEXTHOP_TYPE_IPV4
:
49 case NEXTHOP_TYPE_IPV4_IFINDEX
:
50 if (! IPV4_ADDR_SAME (&next1
->gate
.ipv4
, &next2
->gate
.ipv4
))
52 if (next1
->ifindex
&& (next1
->ifindex
!= next2
->ifindex
))
55 case NEXTHOP_TYPE_IFINDEX
:
56 if (next1
->ifindex
!= next2
->ifindex
)
59 case NEXTHOP_TYPE_IPV6
:
60 if (! IPV6_ADDR_SAME (&next1
->gate
.ipv6
, &next2
->gate
.ipv6
))
63 case NEXTHOP_TYPE_IPV6_IFINDEX
:
64 if (! IPV6_ADDR_SAME (&next1
->gate
.ipv6
, &next2
->gate
.ipv6
))
66 if (next1
->ifindex
!= next2
->ifindex
)
80 nexthop_type_to_str (enum nexthop_types_t nh_type
)
82 static const char *desc
[] = {
86 "IPv4 nexthop with ifindex",
88 "IPv6 nexthop with ifindex",
96 * Check if the labels match for the 2 nexthops specified.
99 nexthop_labels_match (struct nexthop
*nh1
, struct nexthop
*nh2
)
101 struct nexthop_label
*nhl1
, *nhl2
;
103 nhl1
= nh1
->nh_label
;
104 nhl2
= nh2
->nh_label
;
105 if ((nhl1
&& !nhl2
) || (!nhl1
&& nhl2
))
108 if (nhl1
->num_labels
!= nhl2
->num_labels
)
111 if (memcmp (nhl1
->label
, nhl2
->label
, nhl1
->num_labels
))
120 return XCALLOC (MTYPE_NEXTHOP
, sizeof (struct nexthop
));
123 /* Add nexthop to the end of a nexthop list. */
125 nexthop_add (struct nexthop
**target
, struct nexthop
*nexthop
)
127 struct nexthop
*last
;
129 for (last
= *target
; last
&& last
->next
; last
= last
->next
)
132 last
->next
= nexthop
;
135 nexthop
->prev
= last
;
139 copy_nexthops (struct nexthop
**tnh
, struct nexthop
*nh
)
141 struct nexthop
*nexthop
;
144 for (nh1
= nh
; nh1
; nh1
= nh1
->next
)
146 nexthop
= nexthop_new();
147 nexthop
->flags
= nh
->flags
;
148 nexthop
->type
= nh
->type
;
149 nexthop
->ifindex
= nh
->ifindex
;
150 memcpy(&(nexthop
->gate
), &(nh
->gate
), sizeof(union g_addr
));
151 memcpy(&(nexthop
->src
), &(nh
->src
), sizeof(union g_addr
));
153 nexthop_add_labels (nexthop
, nh
->nh_label_type
,
154 nh
->nh_label
->num_labels
, &nh
->nh_label
->label
[0]);
155 nexthop_add(tnh
, nexthop
);
157 if (CHECK_FLAG(nh1
->flags
, NEXTHOP_FLAG_RECURSIVE
))
158 copy_nexthops(&nexthop
->resolved
, nh1
->resolved
);
164 nexthop_free (struct nexthop
*nexthop
)
166 nexthop_del_labels (nexthop
);
167 if (nexthop
->resolved
)
168 nexthops_free(nexthop
->resolved
);
169 XFREE (MTYPE_NEXTHOP
, nexthop
);
172 /* Frees a list of nexthops */
174 nexthops_free (struct nexthop
*nexthop
)
176 struct nexthop
*nh
, *next
;
178 for (nh
= nexthop
; nh
; nh
= next
)
185 /* Update nexthop with label information. */
187 nexthop_add_labels (struct nexthop
*nexthop
, enum lsp_types_t type
,
188 u_int8_t num_labels
, mpls_label_t
*label
)
190 struct nexthop_label
*nh_label
;
193 nexthop
->nh_label_type
= type
;
194 nh_label
= XCALLOC (MTYPE_NH_LABEL
, sizeof (struct nexthop_label
) +
195 num_labels
* sizeof (mpls_label_t
));
196 nh_label
->num_labels
= num_labels
;
197 for (i
= 0; i
< num_labels
; i
++)
198 nh_label
->label
[i
] = *(label
+ i
);
199 nexthop
->nh_label
= nh_label
;
202 /* Free label information of nexthop, if present. */
204 nexthop_del_labels (struct nexthop
*nexthop
)
206 if (nexthop
->nh_label
)
208 XFREE (MTYPE_NH_LABEL
, nexthop
->nh_label
);
209 nexthop
->nh_label_type
= ZEBRA_LSP_NONE
;
214 nexthop2str (struct nexthop
*nexthop
, char *str
, int size
)
216 switch (nexthop
->type
)
218 case NEXTHOP_TYPE_IFINDEX
:
219 snprintf (str
, size
, "if %u", nexthop
->ifindex
);
221 case NEXTHOP_TYPE_IPV4
:
222 snprintf (str
, size
, "%s", inet_ntoa (nexthop
->gate
.ipv4
));
224 case NEXTHOP_TYPE_IPV4_IFINDEX
:
225 snprintf (str
, size
, "%s if %u",
226 inet_ntoa (nexthop
->gate
.ipv4
), nexthop
->ifindex
);
228 case NEXTHOP_TYPE_IPV6
:
229 snprintf (str
, size
, "%s", inet6_ntoa (nexthop
->gate
.ipv6
));
231 case NEXTHOP_TYPE_IPV6_IFINDEX
:
232 snprintf (str
, size
, "%s if %u",
233 inet6_ntoa (nexthop
->gate
.ipv6
), nexthop
->ifindex
);
235 case NEXTHOP_TYPE_BLACKHOLE
:
236 snprintf (str
, size
, "blackhole");
239 snprintf (str
, size
, "unknown");