]> git.proxmox.com Git - mirror_frr.git/blob - lib/nexthop.c
mpls: add support for LDP LSPs
[mirror_frr.git] / lib / nexthop.c
1 /* A generic nexthop structure
2 * Copyright (C) 2013 Cumulus Networks, Inc.
3 *
4 * This file is part of Quagga.
5 *
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
9 * later version.
10 *
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.
15 *
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
19 * 02111-1307, USA.
20 */
21 #include <zebra.h>
22
23 #include "prefix.h"
24 #include "table.h"
25 #include "memory.h"
26 #include "str.h"
27 #include "command.h"
28 #include "if.h"
29 #include "log.h"
30 #include "sockunion.h"
31 #include "linklist.h"
32 #include "thread.h"
33 #include "prefix.h"
34 #include "nexthop.h"
35 #include "mpls.h"
36
37 DEFINE_MTYPE_STATIC(LIB, NEXTHOP, "Nexthop")
38 DEFINE_MTYPE_STATIC(LIB, NH_LABEL, "Nexthop label")
39
40 /* check if nexthops are same, non-recursive */
41 int
42 nexthop_same_no_recurse (struct nexthop *next1, struct nexthop *next2)
43 {
44 if (next1->type != next2->type)
45 return 0;
46
47 switch (next1->type)
48 {
49 case NEXTHOP_TYPE_IPV4:
50 case NEXTHOP_TYPE_IPV4_IFINDEX:
51 if (! IPV4_ADDR_SAME (&next1->gate.ipv4, &next2->gate.ipv4))
52 return 0;
53 if (next1->ifindex && (next1->ifindex != next2->ifindex))
54 return 0;
55 break;
56 case NEXTHOP_TYPE_IFINDEX:
57 if (next1->ifindex != next2->ifindex)
58 return 0;
59 break;
60 case NEXTHOP_TYPE_IPV6:
61 if (! IPV6_ADDR_SAME (&next1->gate.ipv6, &next2->gate.ipv6))
62 return 0;
63 break;
64 case NEXTHOP_TYPE_IPV6_IFINDEX:
65 if (! IPV6_ADDR_SAME (&next1->gate.ipv6, &next2->gate.ipv6))
66 return 0;
67 if (next1->ifindex != next2->ifindex)
68 return 0;
69 break;
70 default:
71 /* do nothing */
72 break;
73 }
74 return 1;
75 }
76
77 /*
78 * nexthop_type_to_str
79 */
80 const char *
81 nexthop_type_to_str (enum nexthop_types_t nh_type)
82 {
83 static const char *desc[] = {
84 "none",
85 "Directly connected",
86 "Interface route",
87 "IPv4 nexthop",
88 "IPv4 nexthop with ifindex",
89 "IPv6 nexthop",
90 "IPv6 nexthop with ifindex",
91 "Null0 nexthop",
92 };
93
94 return desc[nh_type];
95 }
96
97 struct nexthop *
98 nexthop_new (void)
99 {
100 return XCALLOC (MTYPE_NEXTHOP, sizeof (struct nexthop));
101 }
102
103 /* Add nexthop to the end of a nexthop list. */
104 void
105 nexthop_add (struct nexthop **target, struct nexthop *nexthop)
106 {
107 struct nexthop *last;
108
109 for (last = *target; last && last->next; last = last->next)
110 ;
111 if (last)
112 last->next = nexthop;
113 else
114 *target = nexthop;
115 nexthop->prev = last;
116 }
117
118 void
119 copy_nexthops (struct nexthop **tnh, struct nexthop *nh)
120 {
121 struct nexthop *nexthop;
122 struct nexthop *nh1;
123
124 for (nh1 = nh; nh1; nh1 = nh1->next)
125 {
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 if (nh->nh_label)
133 nexthop_add_labels (nexthop, nh->nh_label_type,
134 nh->nh_label->num_labels, &nh->nh_label->label[0]);
135 nexthop_add(tnh, nexthop);
136
137 if (CHECK_FLAG(nh1->flags, NEXTHOP_FLAG_RECURSIVE))
138 copy_nexthops(&nexthop->resolved, nh1->resolved);
139 }
140 }
141
142 /* Free nexthop. */
143 void
144 nexthop_free (struct nexthop *nexthop)
145 {
146 nexthop_del_labels (nexthop);
147 if (nexthop->resolved)
148 nexthops_free(nexthop->resolved);
149 XFREE (MTYPE_NEXTHOP, nexthop);
150 }
151
152 /* Frees a list of nexthops */
153 void
154 nexthops_free (struct nexthop *nexthop)
155 {
156 struct nexthop *nh, *next;
157
158 for (nh = nexthop; nh; nh = next)
159 {
160 next = nh->next;
161 nexthop_free (nh);
162 }
163 }
164
165 /* Update nexthop with label information. */
166 void
167 nexthop_add_labels (struct nexthop *nexthop, enum lsp_types_t type,
168 u_int8_t num_labels, mpls_label_t *label)
169 {
170 struct nexthop_label *nh_label;
171 int i;
172
173 nexthop->nh_label_type = type;
174 nh_label = XCALLOC (MTYPE_NH_LABEL, sizeof (struct nexthop_label));
175 nh_label->num_labels = num_labels;
176 for (i = 0; i < num_labels; i++)
177 nh_label->label[i] = *(label + i);
178 nexthop->nh_label = nh_label;
179 }
180
181 /* Free label information of nexthop, if present. */
182 void
183 nexthop_del_labels (struct nexthop *nexthop)
184 {
185 if (nexthop->nh_label)
186 {
187 XFREE (MTYPE_NH_LABEL, nexthop->nh_label);
188 nexthop->nh_label_type = ZEBRA_LSP_NONE;
189 }
190 }
191
192 const char *
193 nexthop2str (struct nexthop *nexthop, char *str, int size)
194 {
195 switch (nexthop->type)
196 {
197 case NEXTHOP_TYPE_IFINDEX:
198 snprintf (str, size, "if %u", nexthop->ifindex);
199 break;
200 case NEXTHOP_TYPE_IPV4:
201 snprintf (str, size, "%s", inet_ntoa (nexthop->gate.ipv4));
202 break;
203 case NEXTHOP_TYPE_IPV4_IFINDEX:
204 snprintf (str, size, "%s if %u",
205 inet_ntoa (nexthop->gate.ipv4), nexthop->ifindex);
206 break;
207 case NEXTHOP_TYPE_IPV6:
208 snprintf (str, size, "%s", inet6_ntoa (nexthop->gate.ipv6));
209 break;
210 case NEXTHOP_TYPE_IPV6_IFINDEX:
211 snprintf (str, size, "%s if %u",
212 inet6_ntoa (nexthop->gate.ipv6), nexthop->ifindex);
213 break;
214 case NEXTHOP_TYPE_BLACKHOLE:
215 snprintf (str, size, "blackhole");
216 break;
217 default:
218 snprintf (str, size, "unknown");
219 break;
220 }
221
222 return str;
223 }