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
30 #include "sockunion.h"
37 DEFINE_MTYPE_STATIC(LIB
, NEXTHOP
, "Nexthop")
38 DEFINE_MTYPE_STATIC(LIB
, NH_LABEL
, "Nexthop label")
40 /* check if nexthops are same, non-recursive */
42 nexthop_same_no_recurse (struct nexthop
*next1
, struct nexthop
*next2
)
44 if (next1
->type
!= next2
->type
)
49 case NEXTHOP_TYPE_IPV4
:
50 case NEXTHOP_TYPE_IPV4_IFINDEX
:
51 if (! IPV4_ADDR_SAME (&next1
->gate
.ipv4
, &next2
->gate
.ipv4
))
53 if (next1
->ifindex
&& (next1
->ifindex
!= next2
->ifindex
))
56 case NEXTHOP_TYPE_IFINDEX
:
57 if (next1
->ifindex
!= next2
->ifindex
)
60 case NEXTHOP_TYPE_IPV6
:
61 if (! IPV6_ADDR_SAME (&next1
->gate
.ipv6
, &next2
->gate
.ipv6
))
64 case NEXTHOP_TYPE_IPV6_IFINDEX
:
65 if (! IPV6_ADDR_SAME (&next1
->gate
.ipv6
, &next2
->gate
.ipv6
))
67 if (next1
->ifindex
!= next2
->ifindex
)
81 nexthop_type_to_str (enum nexthop_types_t nh_type
)
83 static const char *desc
[] = {
88 "IPv4 nexthop with ifindex",
90 "IPv6 nexthop with ifindex",
100 return XCALLOC (MTYPE_NEXTHOP
, sizeof (struct nexthop
));
103 /* Add nexthop to the end of a nexthop list. */
105 nexthop_add (struct nexthop
**target
, struct nexthop
*nexthop
)
107 struct nexthop
*last
;
109 for (last
= *target
; last
&& last
->next
; last
= last
->next
)
112 last
->next
= nexthop
;
115 nexthop
->prev
= last
;
119 copy_nexthops (struct nexthop
**tnh
, struct nexthop
*nh
)
121 struct nexthop
*nexthop
;
124 for (nh1
= nh
; nh1
; nh1
= nh1
->next
)
126 nexthop
= nexthop_new();
127 nexthop
->flags
= nh
->flags
;
128 nexthop
->type
= nh
->type
;
129 nexthop
->ifindex
= nh
->ifindex
;
130 memcpy(&(nexthop
->gate
), &(nh
->gate
), sizeof(union g_addr
));
131 memcpy(&(nexthop
->src
), &(nh
->src
), sizeof(union g_addr
));
132 nexthop_add(tnh
, nexthop
);
134 if (CHECK_FLAG(nh1
->flags
, NEXTHOP_FLAG_RECURSIVE
))
135 copy_nexthops(&nexthop
->resolved
, nh1
->resolved
);
141 nexthop_free (struct nexthop
*nexthop
)
143 nexthop_del_labels (nexthop
);
144 if (nexthop
->resolved
)
145 nexthops_free(nexthop
->resolved
);
146 XFREE (MTYPE_NEXTHOP
, nexthop
);
149 /* Frees a list of nexthops */
151 nexthops_free (struct nexthop
*nexthop
)
153 struct nexthop
*nh
, *next
;
155 for (nh
= nexthop
; nh
; nh
= next
)
162 /* Update nexthop with label information. */
164 nexthop_add_labels (struct nexthop
*nexthop
, u_int8_t num_labels
,
167 struct nexthop_label
*nh_label
;
170 nh_label
= XCALLOC (MTYPE_NH_LABEL
, sizeof (struct nexthop_label
));
171 nh_label
->num_labels
= num_labels
;
172 for (i
= 0; i
< num_labels
; i
++)
173 nh_label
->label
[i
] = *(label
+ i
);
174 nexthop
->nh_label
= nh_label
;
177 /* Free label information of nexthop, if present. */
179 nexthop_del_labels (struct nexthop
*nexthop
)
181 if (nexthop
->nh_label
)
182 XFREE (MTYPE_NH_LABEL
, nexthop
->nh_label
);
186 nexthop2str (struct nexthop
*nexthop
, char *str
, int size
)
188 switch (nexthop
->type
)
190 case NEXTHOP_TYPE_IFINDEX
:
191 snprintf (str
, size
, "if %u", nexthop
->ifindex
);
193 case NEXTHOP_TYPE_IPV4
:
194 snprintf (str
, size
, "%s", inet_ntoa (nexthop
->gate
.ipv4
));
196 case NEXTHOP_TYPE_IPV4_IFINDEX
:
197 snprintf (str
, size
, "%s if %u",
198 inet_ntoa (nexthop
->gate
.ipv4
), nexthop
->ifindex
);
200 case NEXTHOP_TYPE_IPV6
:
201 snprintf (str
, size
, "%s", inet6_ntoa (nexthop
->gate
.ipv6
));
203 case NEXTHOP_TYPE_IPV6_IFINDEX
:
204 snprintf (str
, size
, "%s if %u",
205 inet6_ntoa (nexthop
->gate
.ipv6
), nexthop
->ifindex
);
207 case NEXTHOP_TYPE_BLACKHOLE
:
208 snprintf (str
, size
, "blackhole");
211 snprintf (str
, size
, "unknown");