blob: 5eb2182de0963d365a3ddff02680baae41475a08 [file] [log] [blame]
Pradosh Mohapatra60cc9592015-11-09 20:21:41 -05001/* 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
36/* check if nexthops are same, non-recursive */
37int
38nexthop_same_no_recurse (struct nexthop *next1, struct nexthop *next2)
39{
40 if (next1->type != next2->type)
41 return 0;
42
43 switch (next1->type)
44 {
45 case NEXTHOP_TYPE_IPV4:
46 case NEXTHOP_TYPE_IPV4_IFINDEX:
47 if (! IPV4_ADDR_SAME (&next1->gate.ipv4, &next2->gate.ipv4))
48 return 0;
49 if (next1->ifindex && (next1->ifindex != next2->ifindex))
50 return 0;
51 break;
52 case NEXTHOP_TYPE_IFINDEX:
53 case NEXTHOP_TYPE_IFNAME:
54 if (next1->ifindex != next2->ifindex)
55 return 0;
56 break;
57#ifdef HAVE_IPV6
58 case NEXTHOP_TYPE_IPV6:
59 if (! IPV6_ADDR_SAME (&next1->gate.ipv6, &next2->gate.ipv6))
60 return 0;
61 break;
62 case NEXTHOP_TYPE_IPV6_IFINDEX:
63 case NEXTHOP_TYPE_IPV6_IFNAME:
64 if (! IPV6_ADDR_SAME (&next1->gate.ipv6, &next2->gate.ipv6))
65 return 0;
66 if (next1->ifindex != next2->ifindex)
67 return 0;
68 break;
69#endif /* HAVE_IPV6 */
70 default:
71 /* do nothing */
72 break;
73 }
74 return 1;
75}
76
77/*
78 * nexthop_type_to_str
79 */
80const char *
81nexthop_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 "IPv4 nexthop with ifname",
90 "IPv6 nexthop",
91 "IPv6 nexthop with ifindex",
92 "IPv6 nexthop with ifname",
93 "Null0 nexthop",
94 };
95
96 if (nh_type >= ZEBRA_NUM_OF (desc))
97 return "<Invalid nh type>";
98
99 return desc[nh_type];
100}
101
102struct nexthop *
103nexthop_new (void)
104{
105 return XCALLOC (MTYPE_NEXTHOP, sizeof (struct nexthop));
106}
107
108/* Add nexthop to the end of a nexthop list. */
109void
110nexthop_add (struct nexthop **target, struct nexthop *nexthop)
111{
112 struct nexthop *last;
113
114 for (last = *target; last && last->next; last = last->next)
115 ;
116 if (last)
117 last->next = nexthop;
118 else
119 *target = nexthop;
120 nexthop->prev = last;
121}
122
123void
124copy_nexthops (struct nexthop **tnh, struct nexthop *nh)
125{
126 struct nexthop *nexthop;
127 struct nexthop *nh1;
128
129 for (nh1 = nh; nh1; nh1 = nh1->next)
130 {
131 nexthop = nexthop_new();
132 nexthop->flags = nh->flags;
133 nexthop->type = nh->type;
134 nexthop->ifindex = nh->ifindex;
135 if (nh->ifname)
136 nexthop->ifname = XSTRDUP(0, nh->ifname);
137 memcpy(&(nexthop->gate), &(nh->gate), sizeof(union g_addr));
138 memcpy(&(nexthop->src), &(nh->src), sizeof(union g_addr));
139 nexthop_add(tnh, nexthop);
140
141 if (CHECK_FLAG(nh1->flags, NEXTHOP_FLAG_RECURSIVE))
142 copy_nexthops(&nexthop->resolved, nh1->resolved);
143 }
144}
145
146/* Free nexthop. */
147void
148nexthop_free (struct nexthop *nexthop)
149{
150 if (nexthop->ifname)
151 XFREE (0, nexthop->ifname);
152 if (nexthop->resolved)
153 nexthops_free(nexthop->resolved);
154 XFREE (MTYPE_NEXTHOP, nexthop);
155}
156
157/* Frees a list of nexthops */
158void
159nexthops_free (struct nexthop *nexthop)
160{
161 struct nexthop *nh, *next;
162
163 for (nh = nexthop; nh; nh = next)
164 {
165 next = nh->next;
166 nexthop_free (nh);
167 }
168}