]> git.proxmox.com Git - mirror_frr.git/blame - staticd/static_nht.c
Merge pull request #5528 from opensourcerouting/bmp-dns-fixing
[mirror_frr.git] / staticd / static_nht.c
CommitLineData
7e24fdf3
DS
1/*
2 * Static NHT code.
3 * Copyright (C) 2018 Cumulus Networks, Inc.
4 * Donald Sharp
5 *
8d5cbee9
DS
6 * This program 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 Free
8 * Software Foundation; either version 2 of the License, or (at your option)
9 * any later version.
7e24fdf3 10 *
8d5cbee9
DS
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * more details.
7e24fdf3
DS
15 *
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
19 */
20#include <zebra.h>
21
22#include "prefix.h"
23#include "table.h"
24#include "vrf.h"
25#include "nexthop.h"
27da3044 26#include "srcdest_table.h"
7e24fdf3
DS
27
28#include "static_vrf.h"
29#include "static_routes.h"
30#include "static_zebra.h"
31#include "static_nht.h"
32
27da3044
DS
33static void static_nht_update_rn(struct route_node *rn,
34 struct prefix *nhp, uint32_t nh_num,
35 vrf_id_t nh_vrf_id, struct vrf *vrf,
36 safi_t safi)
7e24fdf3 37{
7e24fdf3 38 struct static_route *si;
27da3044
DS
39
40 for (si = rn->info; si; si = si->next) {
41 if (si->nh_vrf_id != nh_vrf_id)
42 continue;
43
44 if (si->type != STATIC_IPV4_GATEWAY
45 && si->type != STATIC_IPV4_GATEWAY_IFNAME
46 && si->type != STATIC_IPV6_GATEWAY
47 && si->type != STATIC_IPV6_GATEWAY_IFNAME)
48 continue;
49
50 if (nhp->family == AF_INET
51 && nhp->u.prefix4.s_addr == si->addr.ipv4.s_addr)
52 si->nh_valid = !!nh_num;
53
54 if (nhp->family == AF_INET6
55 && memcmp(&nhp->u.prefix6, &si->addr.ipv6, 16) == 0)
56 si->nh_valid = !!nh_num;
57
9fafbd15
DS
58 if (si->state == STATIC_START)
59 static_zebra_route_add(rn, si, vrf->vrf_id, safi, true);
27da3044
DS
60 }
61}
62
63static void static_nht_update_safi(struct prefix *sp, struct prefix *nhp,
64 uint32_t nh_num, afi_t afi, safi_t safi,
65 struct vrf *vrf, vrf_id_t nh_vrf_id)
66{
67 struct route_table *stable;
7e24fdf3
DS
68 struct static_vrf *svrf;
69 struct route_node *rn;
7e24fdf3 70
c45fb58d
DS
71 svrf = vrf->info;
72 if (!svrf)
73 return;
7e24fdf3 74
c45fb58d
DS
75 stable = static_vrf_static_table(afi, safi, svrf);
76 if (!stable)
77 return;
7e24fdf3 78
27da3044
DS
79 if (sp) {
80 rn = srcdest_rnode_lookup(stable, sp, NULL);
81 if (rn) {
82 static_nht_update_rn(rn, nhp, nh_num, nh_vrf_id,
83 vrf, safi);
84 route_unlock_node(rn);
7e24fdf3 85 }
27da3044 86 return;
7e24fdf3 87 }
27da3044
DS
88
89 for (rn = route_top(stable); rn; rn = route_next(rn))
90 static_nht_update_rn(rn, nhp, nh_num, nh_vrf_id, vrf, safi);
91
7e24fdf3 92}
c45fb58d 93
27da3044
DS
94void static_nht_update(struct prefix *sp, struct prefix *nhp,
95 uint32_t nh_num, afi_t afi, vrf_id_t nh_vrf_id)
c45fb58d
DS
96{
97
98 struct vrf *vrf;
99
100 RB_FOREACH (vrf, vrf_name_head, &vrfs_by_name) {
27da3044 101 static_nht_update_safi(sp, nhp, nh_num, afi, SAFI_UNICAST,
c45fb58d 102 vrf, nh_vrf_id);
27da3044 103 static_nht_update_safi(sp, nhp, nh_num, afi, SAFI_MULTICAST,
c45fb58d
DS
104 vrf, nh_vrf_id);
105 }
106}
9fafbd15
DS
107
108static void static_nht_reset_start_safi(struct prefix *nhp, afi_t afi,
109 safi_t safi, struct vrf *vrf,
110 vrf_id_t nh_vrf_id)
111{
112 struct static_vrf *svrf;
113 struct route_table *stable;
114 struct static_route *si;
115 struct route_node *rn;
116
117 svrf = vrf->info;
118 if (!svrf)
119 return;
120
121 stable = static_vrf_static_table(afi, safi, svrf);
122 if (!stable)
123 return;
124
125 for (rn = route_top(stable); rn; rn = route_next(rn)) {
126 for (si = rn->info; si; si = si->next) {
127 if (si->nh_vrf_id != nh_vrf_id)
128 continue;
129
130 if (nhp->family == AF_INET
131 && nhp->u.prefix4.s_addr != si->addr.ipv4.s_addr)
132 continue;
133
134 if (nhp->family == AF_INET6
135 && memcmp(&nhp->u.prefix6, &si->addr.ipv6, 16) != 0)
136 continue;
137
138 /*
139 * We've been told that a nexthop we depend
140 * on has changed in some manner, so reset
141 * the state machine to allow us to start
142 * over.
143 */
144 si->state = STATIC_START;
145 }
146 }
147}
148
149void static_nht_reset_start(struct prefix *nhp, afi_t afi, vrf_id_t nh_vrf_id)
150{
151 struct vrf *vrf;
152
153 RB_FOREACH (vrf, vrf_name_head, &vrfs_by_name) {
154 static_nht_reset_start_safi(nhp, afi, SAFI_UNICAST,
155 vrf, nh_vrf_id);
156 static_nht_reset_start_safi(nhp, afi, SAFI_MULTICAST,
157 vrf, nh_vrf_id);
158 }
159}
160
161static void static_nht_mark_state_safi(struct prefix *sp, afi_t afi,
162 safi_t safi, struct vrf *vrf,
163 enum static_install_states state)
164{
165 struct static_vrf *svrf;
166 struct route_table *stable;
167 struct static_route *si;
168 struct route_node *rn;
169
170 svrf = vrf->info;
171 if (!svrf)
172 return;
173
174 stable = static_vrf_static_table(afi, safi, svrf);
175 if (!stable)
176 return;
177
178 rn = srcdest_rnode_lookup(stable, sp, NULL);
179 if (!rn)
180 return;
181
182 for (si = rn->info; si; si = si->next)
183 si->state = state;
184
185 route_unlock_node(rn);
186}
187
188void static_nht_mark_state(struct prefix *sp, vrf_id_t vrf_id,
189 enum static_install_states state)
190{
191 struct vrf *vrf;
192
193 afi_t afi = AFI_IP;
194
195 if (sp->family == AF_INET6)
196 afi = AFI_IP6;
197
198 vrf = vrf_lookup_by_id(vrf_id);
199 if (!vrf || !vrf->info)
200 return;
201
202 static_nht_mark_state_safi(sp, afi, SAFI_UNICAST, vrf, state);
203 static_nht_mark_state_safi(sp, afi, SAFI_MULTICAST, vrf, state);
204}