]>
Commit | Line | Data |
---|---|---|
718e3744 | 1 | /* BGP nexthop scan |
896014f4 DL |
2 | * Copyright (C) 2000 Kunihiro Ishiguro |
3 | * | |
4 | * This file is part of GNU Zebra. | |
5 | * | |
6 | * GNU Zebra 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 | * GNU Zebra 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 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 | */ | |
718e3744 | 20 | |
21 | #include <zebra.h> | |
22 | ||
23 | #include "command.h" | |
24 | #include "thread.h" | |
25 | #include "prefix.h" | |
26 | #include "zclient.h" | |
27 | #include "stream.h" | |
28 | #include "network.h" | |
29 | #include "log.h" | |
30 | #include "memory.h" | |
10f9bf3f JBD |
31 | #include "hash.h" |
32 | #include "jhash.h" | |
fb018d25 | 33 | #include "nexthop.h" |
3f9c7369 | 34 | #include "queue.h" |
039f3a34 | 35 | #include "filter.h" |
718e3744 | 36 | |
37 | #include "bgpd/bgpd.h" | |
38 | #include "bgpd/bgp_table.h" | |
39 | #include "bgpd/bgp_route.h" | |
40 | #include "bgpd/bgp_attr.h" | |
41 | #include "bgpd/bgp_nexthop.h" | |
fb018d25 | 42 | #include "bgpd/bgp_nht.h" |
718e3744 | 43 | #include "bgpd/bgp_debug.h" |
44 | #include "bgpd/bgp_damp.h" | |
8ffedcea | 45 | #include "bgpd/bgp_fsm.h" |
8386ac43 | 46 | #include "bgpd/bgp_vty.h" |
718e3744 | 47 | |
330cec3d DS |
48 | DEFINE_MTYPE_STATIC(BGPD, MARTIAN_STRING, "BGP Martian Address Intf String"); |
49 | ||
d62a17ae | 50 | char *bnc_str(struct bgp_nexthop_cache *bnc, char *buf, int size) |
fb018d25 | 51 | { |
d62a17ae | 52 | prefix2str(&(bnc->node->p), buf, size); |
53 | return buf; | |
fb018d25 DS |
54 | } |
55 | ||
d62a17ae | 56 | void bnc_nexthop_free(struct bgp_nexthop_cache *bnc) |
718e3744 | 57 | { |
d62a17ae | 58 | nexthops_free(bnc->nexthop); |
718e3744 | 59 | } |
60 | ||
d62a17ae | 61 | struct bgp_nexthop_cache *bnc_new(void) |
718e3744 | 62 | { |
d62a17ae | 63 | struct bgp_nexthop_cache *bnc; |
fb018d25 | 64 | |
d62a17ae | 65 | bnc = XCALLOC(MTYPE_BGP_NEXTHOP_CACHE, |
66 | sizeof(struct bgp_nexthop_cache)); | |
67 | LIST_INIT(&(bnc->paths)); | |
68 | return bnc; | |
718e3744 | 69 | } |
70 | ||
d62a17ae | 71 | void bnc_free(struct bgp_nexthop_cache *bnc) |
718e3744 | 72 | { |
d62a17ae | 73 | bnc_nexthop_free(bnc); |
74 | XFREE(MTYPE_BGP_NEXTHOP_CACHE, bnc); | |
718e3744 | 75 | } |
6b0655a2 | 76 | |
718e3744 | 77 | /* Reset and free all BGP nexthop cache. */ |
d62a17ae | 78 | static void bgp_nexthop_cache_reset(struct bgp_table *table) |
718e3744 | 79 | { |
d62a17ae | 80 | struct bgp_node *rn; |
81 | struct bgp_nexthop_cache *bnc; | |
82 | ||
14315f2d | 83 | for (rn = bgp_table_top(table); rn; rn = bgp_route_next(rn)) { |
5b8d32bd | 84 | bnc = bgp_node_get_bgp_nexthop_info(rn); |
3d111939 DS |
85 | if (!bnc) |
86 | continue; | |
7f040da1 | 87 | |
3d111939 DS |
88 | while (!LIST_EMPTY(&(bnc->paths))) { |
89 | struct bgp_path_info *path = LIST_FIRST(&(bnc->paths)); | |
7f040da1 | 90 | |
3d111939 | 91 | path_nh_map(path, bnc, false); |
d62a17ae | 92 | } |
3d111939 DS |
93 | |
94 | bnc_free(bnc); | |
5b8d32bd | 95 | bgp_node_set_bgp_nexthop_info(rn, NULL); |
3d111939 | 96 | bgp_unlock_node(rn); |
14315f2d | 97 | } |
718e3744 | 98 | } |
99 | ||
db0e1937 MK |
100 | static void *bgp_tip_hash_alloc(void *p) |
101 | { | |
0291c246 MK |
102 | const struct in_addr *val = (const struct in_addr *)p; |
103 | struct tip_addr *addr; | |
db0e1937 MK |
104 | |
105 | addr = XMALLOC(MTYPE_TIP_ADDR, sizeof(struct tip_addr)); | |
106 | addr->refcnt = 0; | |
107 | addr->addr.s_addr = val->s_addr; | |
108 | ||
109 | return addr; | |
110 | } | |
111 | ||
112 | static void bgp_tip_hash_free(void *addr) | |
113 | { | |
114 | XFREE(MTYPE_TIP_ADDR, addr); | |
115 | } | |
116 | ||
d8b87afe | 117 | static unsigned int bgp_tip_hash_key_make(const void *p) |
db0e1937 | 118 | { |
0291c246 | 119 | const struct tip_addr *addr = p; |
db0e1937 MK |
120 | |
121 | return jhash_1word(addr->addr.s_addr, 0); | |
122 | } | |
123 | ||
74df8d6d | 124 | static bool bgp_tip_hash_cmp(const void *p1, const void *p2) |
db0e1937 | 125 | { |
0291c246 MK |
126 | const struct tip_addr *addr1 = p1; |
127 | const struct tip_addr *addr2 = p2; | |
db0e1937 MK |
128 | |
129 | return addr1->addr.s_addr == addr2->addr.s_addr; | |
130 | } | |
131 | ||
132 | void bgp_tip_hash_init(struct bgp *bgp) | |
133 | { | |
996c9314 | 134 | bgp->tip_hash = hash_create(bgp_tip_hash_key_make, bgp_tip_hash_cmp, |
3f65c5b1 | 135 | "BGP TIP hash"); |
db0e1937 MK |
136 | } |
137 | ||
138 | void bgp_tip_hash_destroy(struct bgp *bgp) | |
139 | { | |
140 | if (bgp->tip_hash == NULL) | |
141 | return; | |
142 | hash_clean(bgp->tip_hash, bgp_tip_hash_free); | |
143 | hash_free(bgp->tip_hash); | |
144 | bgp->tip_hash = NULL; | |
145 | } | |
146 | ||
147 | void bgp_tip_add(struct bgp *bgp, struct in_addr *tip) | |
148 | { | |
0291c246 MK |
149 | struct tip_addr tmp; |
150 | struct tip_addr *addr; | |
db0e1937 MK |
151 | |
152 | tmp.addr = *tip; | |
153 | ||
154 | addr = hash_get(bgp->tip_hash, &tmp, bgp_tip_hash_alloc); | |
155 | if (!addr) | |
156 | return; | |
157 | ||
158 | addr->refcnt++; | |
159 | } | |
160 | ||
161 | void bgp_tip_del(struct bgp *bgp, struct in_addr *tip) | |
162 | { | |
0291c246 MK |
163 | struct tip_addr tmp; |
164 | struct tip_addr *addr; | |
db0e1937 MK |
165 | |
166 | tmp.addr = *tip; | |
167 | ||
168 | addr = hash_lookup(bgp->tip_hash, &tmp); | |
169 | /* may have been deleted earlier by bgp_interface_down() */ | |
170 | if (addr == NULL) | |
171 | return; | |
172 | ||
173 | addr->refcnt--; | |
174 | ||
175 | if (addr->refcnt == 0) { | |
176 | hash_release(bgp->tip_hash, addr); | |
177 | XFREE(MTYPE_TIP_ADDR, addr); | |
178 | } | |
179 | } | |
10f9bf3f | 180 | |
af97a18b DS |
181 | /* BGP own address structure */ |
182 | struct bgp_addr { | |
183 | struct in_addr addr; | |
f4c2fb93 | 184 | struct list *ifp_name_list; |
af97a18b DS |
185 | }; |
186 | ||
e3b78da8 | 187 | static void show_address_entry(struct hash_bucket *bucket, void *args) |
af97a18b DS |
188 | { |
189 | struct vty *vty = (struct vty *)args; | |
e3b78da8 | 190 | struct bgp_addr *addr = (struct bgp_addr *)bucket->data; |
f4c2fb93 DS |
191 | char *name; |
192 | struct listnode *node; | |
af97a18b | 193 | |
f4c2fb93 DS |
194 | vty_out(vty, "addr: %s, count: %d : ", inet_ntoa(addr->addr), |
195 | addr->ifp_name_list->count); | |
196 | ||
197 | for (ALL_LIST_ELEMENTS_RO(addr->ifp_name_list, node, name)) { | |
198 | vty_out(vty, " %s,", name); | |
199 | } | |
200 | ||
201 | vty_out(vty, "\n"); | |
af97a18b DS |
202 | } |
203 | ||
204 | void bgp_nexthop_show_address_hash(struct vty *vty, struct bgp *bgp) | |
205 | { | |
206 | hash_iterate(bgp->address_hash, | |
e3b78da8 | 207 | (void (*)(struct hash_bucket *, void *))show_address_entry, |
af97a18b DS |
208 | vty); |
209 | } | |
210 | ||
f4c2fb93 DS |
211 | static void bgp_address_hash_string_del(void *val) |
212 | { | |
213 | char *data = val; | |
214 | ||
330cec3d | 215 | XFREE(MTYPE_MARTIAN_STRING, data); |
f4c2fb93 DS |
216 | } |
217 | ||
d62a17ae | 218 | static void *bgp_address_hash_alloc(void *p) |
10f9bf3f | 219 | { |
d62a17ae | 220 | const struct in_addr *val = (const struct in_addr *)p; |
221 | struct bgp_addr *addr; | |
10f9bf3f | 222 | |
d62a17ae | 223 | addr = XMALLOC(MTYPE_BGP_ADDR, sizeof(struct bgp_addr)); |
d62a17ae | 224 | addr->addr.s_addr = val->s_addr; |
10f9bf3f | 225 | |
f4c2fb93 DS |
226 | addr->ifp_name_list = list_new(); |
227 | addr->ifp_name_list->del = bgp_address_hash_string_del; | |
228 | ||
d62a17ae | 229 | return addr; |
10f9bf3f JBD |
230 | } |
231 | ||
f4c2fb93 | 232 | static void bgp_address_hash_free(void *data) |
37d361e7 | 233 | { |
f4c2fb93 DS |
234 | struct bgp_addr *addr = data; |
235 | ||
6a154c88 | 236 | list_delete(&addr->ifp_name_list); |
d62a17ae | 237 | XFREE(MTYPE_BGP_ADDR, addr); |
37d361e7 RW |
238 | } |
239 | ||
d8b87afe | 240 | static unsigned int bgp_address_hash_key_make(const void *p) |
10f9bf3f | 241 | { |
d62a17ae | 242 | const struct bgp_addr *addr = p; |
10f9bf3f | 243 | |
d62a17ae | 244 | return jhash_1word(addr->addr.s_addr, 0); |
10f9bf3f JBD |
245 | } |
246 | ||
74df8d6d | 247 | static bool bgp_address_hash_cmp(const void *p1, const void *p2) |
10f9bf3f | 248 | { |
d62a17ae | 249 | const struct bgp_addr *addr1 = p1; |
250 | const struct bgp_addr *addr2 = p2; | |
10f9bf3f | 251 | |
d62a17ae | 252 | return addr1->addr.s_addr == addr2->addr.s_addr; |
10f9bf3f JBD |
253 | } |
254 | ||
d62a17ae | 255 | void bgp_address_init(struct bgp *bgp) |
10f9bf3f | 256 | { |
996c9314 LB |
257 | bgp->address_hash = |
258 | hash_create(bgp_address_hash_key_make, bgp_address_hash_cmp, | |
259 | "BGP Address Hash"); | |
10f9bf3f JBD |
260 | } |
261 | ||
d62a17ae | 262 | void bgp_address_destroy(struct bgp *bgp) |
bb86c601 | 263 | { |
d62a17ae | 264 | if (bgp->address_hash == NULL) |
265 | return; | |
266 | hash_clean(bgp->address_hash, bgp_address_hash_free); | |
267 | hash_free(bgp->address_hash); | |
268 | bgp->address_hash = NULL; | |
bb86c601 LB |
269 | } |
270 | ||
f4c2fb93 DS |
271 | static void bgp_address_add(struct bgp *bgp, struct connected *ifc, |
272 | struct prefix *p) | |
10f9bf3f | 273 | { |
d62a17ae | 274 | struct bgp_addr tmp; |
275 | struct bgp_addr *addr; | |
f4c2fb93 DS |
276 | struct listnode *node; |
277 | char *name; | |
10f9bf3f | 278 | |
d62a17ae | 279 | tmp.addr = p->u.prefix4; |
10f9bf3f | 280 | |
d62a17ae | 281 | addr = hash_get(bgp->address_hash, &tmp, bgp_address_hash_alloc); |
5ce10e92 | 282 | |
f4c2fb93 DS |
283 | for (ALL_LIST_ELEMENTS_RO(addr->ifp_name_list, node, name)) { |
284 | if (strcmp(ifc->ifp->name, name) == 0) | |
285 | break; | |
286 | } | |
287 | if (!node) { | |
330cec3d | 288 | name = XSTRDUP(MTYPE_MARTIAN_STRING, ifc->ifp->name); |
f4c2fb93 DS |
289 | listnode_add(addr->ifp_name_list, name); |
290 | } | |
10f9bf3f JBD |
291 | } |
292 | ||
f4c2fb93 DS |
293 | static void bgp_address_del(struct bgp *bgp, struct connected *ifc, |
294 | struct prefix *p) | |
10f9bf3f | 295 | { |
d62a17ae | 296 | struct bgp_addr tmp; |
297 | struct bgp_addr *addr; | |
f4c2fb93 DS |
298 | struct listnode *node; |
299 | char *name; | |
10f9bf3f | 300 | |
d62a17ae | 301 | tmp.addr = p->u.prefix4; |
10f9bf3f | 302 | |
d62a17ae | 303 | addr = hash_lookup(bgp->address_hash, &tmp); |
304 | /* may have been deleted earlier by bgp_interface_down() */ | |
305 | if (addr == NULL) | |
306 | return; | |
9e47abd8 | 307 | |
f4c2fb93 DS |
308 | for (ALL_LIST_ELEMENTS_RO(addr->ifp_name_list, node, name)) { |
309 | if (strcmp(ifc->ifp->name, name) == 0) | |
310 | break; | |
311 | } | |
10f9bf3f | 312 | |
b9129915 | 313 | if (node) { |
f4c2fb93 | 314 | list_delete_node(addr->ifp_name_list, node); |
b9129915 DS |
315 | XFREE(MTYPE_MARTIAN_STRING, name); |
316 | } | |
f4c2fb93 DS |
317 | |
318 | if (addr->ifp_name_list->count == 0) { | |
d62a17ae | 319 | hash_release(bgp->address_hash, addr); |
6a154c88 | 320 | list_delete(&addr->ifp_name_list); |
d62a17ae | 321 | XFREE(MTYPE_BGP_ADDR, addr); |
322 | } | |
10f9bf3f JBD |
323 | } |
324 | ||
6b0655a2 | 325 | |
d62a17ae | 326 | struct bgp_connected_ref { |
327 | unsigned int refcnt; | |
718e3744 | 328 | }; |
329 | ||
d62a17ae | 330 | void bgp_connected_add(struct bgp *bgp, struct connected *ifc) |
718e3744 | 331 | { |
d62a17ae | 332 | struct prefix p; |
333 | struct prefix *addr; | |
334 | struct bgp_node *rn; | |
335 | struct bgp_connected_ref *bc; | |
336 | struct listnode *node, *nnode; | |
337 | struct peer *peer; | |
338 | ||
339 | addr = ifc->address; | |
340 | ||
341 | p = *(CONNECTED_PREFIX(ifc)); | |
342 | if (addr->family == AF_INET) { | |
343 | apply_mask_ipv4((struct prefix_ipv4 *)&p); | |
344 | ||
345 | if (prefix_ipv4_any((struct prefix_ipv4 *)&p)) | |
346 | return; | |
347 | ||
f4c2fb93 | 348 | bgp_address_add(bgp, ifc, addr); |
d62a17ae | 349 | |
350 | rn = bgp_node_get(bgp->connected_table[AFI_IP], | |
351 | (struct prefix *)&p); | |
cb8c85ab | 352 | bc = bgp_node_get_bgp_connected_ref_info(rn); |
3d9dbdbe | 353 | if (bc) |
d62a17ae | 354 | bc->refcnt++; |
3d9dbdbe | 355 | else { |
d62a17ae | 356 | bc = XCALLOC(MTYPE_BGP_CONN, |
357 | sizeof(struct bgp_connected_ref)); | |
358 | bc->refcnt = 1; | |
cb8c85ab | 359 | bgp_node_set_bgp_connected_ref_info(rn, bc); |
d62a17ae | 360 | } |
8ffedcea | 361 | |
d62a17ae | 362 | for (ALL_LIST_ELEMENTS(bgp->peer, node, nnode, peer)) { |
363 | if (peer->conf_if | |
364 | && (strcmp(peer->conf_if, ifc->ifp->name) == 0) | |
365 | && peer->status != Established | |
366 | && !CHECK_FLAG(peer->flags, | |
367 | PEER_FLAG_IFPEER_V6ONLY)) { | |
368 | if (peer_active(peer)) | |
369 | BGP_EVENT_ADD(peer, BGP_Stop); | |
370 | BGP_EVENT_ADD(peer, BGP_Start); | |
371 | } | |
372 | } | |
373 | } else if (addr->family == AF_INET6) { | |
374 | apply_mask_ipv6((struct prefix_ipv6 *)&p); | |
375 | ||
376 | if (IN6_IS_ADDR_UNSPECIFIED(&p.u.prefix6)) | |
377 | return; | |
378 | ||
379 | if (IN6_IS_ADDR_LINKLOCAL(&p.u.prefix6)) | |
380 | return; | |
381 | ||
382 | rn = bgp_node_get(bgp->connected_table[AFI_IP6], | |
383 | (struct prefix *)&p); | |
3d9dbdbe | 384 | |
cb8c85ab | 385 | bc = bgp_node_get_bgp_connected_ref_info(rn); |
3d9dbdbe | 386 | if (bc) |
d62a17ae | 387 | bc->refcnt++; |
3d9dbdbe | 388 | else { |
d62a17ae | 389 | bc = XCALLOC(MTYPE_BGP_CONN, |
390 | sizeof(struct bgp_connected_ref)); | |
391 | bc->refcnt = 1; | |
cb8c85ab | 392 | bgp_node_set_bgp_connected_ref_info(rn, bc); |
d62a17ae | 393 | } |
718e3744 | 394 | } |
718e3744 | 395 | } |
396 | ||
d62a17ae | 397 | void bgp_connected_delete(struct bgp *bgp, struct connected *ifc) |
718e3744 | 398 | { |
d62a17ae | 399 | struct prefix p; |
400 | struct prefix *addr; | |
f6bdc080 | 401 | struct bgp_node *rn = NULL; |
d62a17ae | 402 | struct bgp_connected_ref *bc; |
718e3744 | 403 | |
d62a17ae | 404 | addr = ifc->address; |
718e3744 | 405 | |
d62a17ae | 406 | p = *(CONNECTED_PREFIX(ifc)); |
f6bdc080 | 407 | apply_mask(&p); |
d62a17ae | 408 | if (addr->family == AF_INET) { |
d62a17ae | 409 | if (prefix_ipv4_any((struct prefix_ipv4 *)&p)) |
410 | return; | |
718e3744 | 411 | |
f4c2fb93 | 412 | bgp_address_del(bgp, ifc, addr); |
10f9bf3f | 413 | |
d62a17ae | 414 | rn = bgp_node_lookup(bgp->connected_table[AFI_IP], &p); |
d62a17ae | 415 | } else if (addr->family == AF_INET6) { |
d62a17ae | 416 | if (IN6_IS_ADDR_UNSPECIFIED(&p.u.prefix6)) |
417 | return; | |
418 | ||
419 | if (IN6_IS_ADDR_LINKLOCAL(&p.u.prefix6)) | |
420 | return; | |
421 | ||
422 | rn = bgp_node_lookup(bgp->connected_table[AFI_IP6], | |
423 | (struct prefix *)&p); | |
f6bdc080 | 424 | } |
d62a17ae | 425 | |
f6bdc080 DS |
426 | if (!rn) |
427 | return; | |
428 | ||
cb8c85ab | 429 | bc = bgp_node_get_bgp_connected_ref_info(rn); |
f6bdc080 DS |
430 | bc->refcnt--; |
431 | if (bc->refcnt == 0) { | |
432 | XFREE(MTYPE_BGP_CONN, bc); | |
cb8c85ab | 433 | bgp_node_set_bgp_connected_ref_info(rn, NULL); |
718e3744 | 434 | } |
f6bdc080 DS |
435 | bgp_unlock_node(rn); |
436 | bgp_unlock_node(rn); | |
718e3744 | 437 | } |
438 | ||
3292693b DS |
439 | static void bgp_connected_cleanup(struct route_table *table, |
440 | struct route_node *rn) | |
441 | { | |
442 | struct bgp_connected_ref *bc; | |
3d9dbdbe | 443 | struct bgp_node *bn = bgp_node_from_rnode(rn); |
3292693b | 444 | |
cb8c85ab | 445 | bc = bgp_node_get_bgp_connected_ref_info(bn); |
3292693b DS |
446 | if (!bc) |
447 | return; | |
448 | ||
449 | bc->refcnt--; | |
450 | if (bc->refcnt == 0) { | |
451 | XFREE(MTYPE_BGP_CONN, bc); | |
cb8c85ab | 452 | bgp_node_set_bgp_connected_ref_info(bn, NULL); |
3292693b DS |
453 | } |
454 | } | |
455 | ||
d62a17ae | 456 | int bgp_nexthop_self(struct bgp *bgp, struct in_addr nh_addr) |
718e3744 | 457 | { |
d62a17ae | 458 | struct bgp_addr tmp, *addr; |
db0e1937 | 459 | struct tip_addr tmp_tip, *tip; |
718e3744 | 460 | |
d62a17ae | 461 | tmp.addr = nh_addr; |
10f9bf3f | 462 | |
d62a17ae | 463 | addr = hash_lookup(bgp->address_hash, &tmp); |
464 | if (addr) | |
465 | return 1; | |
718e3744 | 466 | |
db0e1937 MK |
467 | tmp_tip.addr = nh_addr; |
468 | tip = hash_lookup(bgp->tip_hash, &tmp_tip); | |
469 | if (tip) | |
470 | return 1; | |
471 | ||
d62a17ae | 472 | return 0; |
718e3744 | 473 | } |
6b0655a2 | 474 | |
d62a17ae | 475 | int bgp_multiaccess_check_v4(struct in_addr nexthop, struct peer *peer) |
718e3744 | 476 | { |
d62a17ae | 477 | struct bgp_node *rn1; |
478 | struct bgp_node *rn2; | |
479 | struct prefix p; | |
480 | int ret; | |
481 | ||
482 | p.family = AF_INET; | |
483 | p.prefixlen = IPV4_MAX_BITLEN; | |
484 | p.u.prefix4 = nexthop; | |
485 | ||
486 | rn1 = bgp_node_match(peer->bgp->connected_table[AFI_IP], &p); | |
487 | if (!rn1) | |
488 | return 0; | |
489 | ||
490 | p.family = AF_INET; | |
491 | p.prefixlen = IPV4_MAX_BITLEN; | |
492 | p.u.prefix4 = peer->su.sin.sin_addr; | |
493 | ||
494 | rn2 = bgp_node_match(peer->bgp->connected_table[AFI_IP], &p); | |
495 | if (!rn2) { | |
496 | bgp_unlock_node(rn1); | |
497 | return 0; | |
498 | } | |
718e3744 | 499 | |
d62a17ae | 500 | ret = (rn1 == rn2) ? 1 : 0; |
718e3744 | 501 | |
d62a17ae | 502 | bgp_unlock_node(rn1); |
503 | bgp_unlock_node(rn2); | |
718e3744 | 504 | |
d62a17ae | 505 | return (ret); |
718e3744 | 506 | } |
507 | ||
737af885 BS |
508 | int bgp_multiaccess_check_v6(struct in6_addr nexthop, struct peer *peer) |
509 | { | |
510 | struct bgp_node *rn1; | |
511 | struct bgp_node *rn2; | |
512 | struct prefix p; | |
513 | int ret; | |
514 | ||
515 | p.family = AF_INET6; | |
516 | p.prefixlen = IPV6_MAX_BITLEN; | |
517 | p.u.prefix6 = nexthop; | |
518 | ||
519 | rn1 = bgp_node_match(peer->bgp->connected_table[AFI_IP6], &p); | |
520 | if (!rn1) | |
521 | return 0; | |
522 | ||
523 | p.family = AF_INET6; | |
524 | p.prefixlen = IPV6_MAX_BITLEN; | |
525 | p.u.prefix6 = peer->su.sin6.sin6_addr; | |
526 | ||
527 | rn2 = bgp_node_match(peer->bgp->connected_table[AFI_IP6], &p); | |
528 | if (!rn2) { | |
529 | bgp_unlock_node(rn1); | |
530 | return 0; | |
531 | } | |
532 | ||
533 | ret = (rn1 == rn2) ? 1 : 0; | |
534 | ||
535 | bgp_unlock_node(rn1); | |
536 | bgp_unlock_node(rn2); | |
537 | ||
538 | return ret; | |
539 | } | |
540 | ||
541 | int bgp_subgrp_multiaccess_check_v6(struct in6_addr nexthop, | |
542 | struct update_subgroup *subgrp) | |
543 | { | |
544 | struct bgp_node *rn1 = NULL, *rn2 = NULL; | |
545 | struct peer_af *paf = NULL; | |
546 | struct prefix p = {0}, np = {0}; | |
547 | struct bgp *bgp = NULL; | |
548 | ||
549 | np.family = AF_INET6; | |
550 | np.prefixlen = IPV6_MAX_BITLEN; | |
551 | np.u.prefix6 = nexthop; | |
552 | ||
553 | p.family = AF_INET; | |
554 | p.prefixlen = IPV6_MAX_BITLEN; | |
555 | ||
556 | bgp = SUBGRP_INST(subgrp); | |
557 | rn1 = bgp_node_match(bgp->connected_table[AFI_IP6], &np); | |
558 | if (!rn1) | |
559 | return 0; | |
560 | ||
561 | SUBGRP_FOREACH_PEER (subgrp, paf) { | |
562 | ||
563 | p.u.prefix6 = paf->peer->su.sin6.sin6_addr; | |
564 | rn2 = bgp_node_match(bgp->connected_table[AFI_IP6], &p); | |
565 | if (rn1 == rn2) { | |
566 | bgp_unlock_node(rn1); | |
567 | bgp_unlock_node(rn2); | |
568 | return 1; | |
569 | } | |
570 | ||
571 | if (rn2) | |
572 | bgp_unlock_node(rn2); | |
573 | } | |
574 | ||
575 | bgp_unlock_node(rn1); | |
576 | return 0; | |
577 | } | |
578 | ||
65d4e0c6 DS |
579 | int bgp_subgrp_multiaccess_check_v4(struct in_addr nexthop, |
580 | struct update_subgroup *subgrp) | |
581 | { | |
582 | struct bgp_node *rn1, *rn2; | |
583 | struct peer_af *paf; | |
584 | struct prefix p, np; | |
a2b6e694 | 585 | struct bgp *bgp; |
65d4e0c6 DS |
586 | |
587 | np.family = AF_INET; | |
588 | np.prefixlen = IPV4_MAX_BITLEN; | |
589 | np.u.prefix4 = nexthop; | |
590 | ||
591 | p.family = AF_INET; | |
592 | p.prefixlen = IPV4_MAX_BITLEN; | |
593 | ||
65d4e0c6 | 594 | bgp = SUBGRP_INST(subgrp); |
996c9314 | 595 | rn1 = bgp_node_match(bgp->connected_table[AFI_IP], &np); |
65d4e0c6 DS |
596 | if (!rn1) |
597 | return 0; | |
598 | ||
996c9314 | 599 | SUBGRP_FOREACH_PEER (subgrp, paf) { |
65d4e0c6 DS |
600 | p.u.prefix4 = paf->peer->su.sin.sin_addr; |
601 | ||
996c9314 | 602 | rn2 = bgp_node_match(bgp->connected_table[AFI_IP], &p); |
65d4e0c6 DS |
603 | if (rn1 == rn2) { |
604 | bgp_unlock_node(rn1); | |
605 | bgp_unlock_node(rn2); | |
606 | return 1; | |
607 | } | |
608 | ||
609 | if (rn2) | |
610 | bgp_unlock_node(rn2); | |
611 | } | |
612 | ||
613 | bgp_unlock_node(rn1); | |
614 | return 0; | |
615 | } | |
616 | ||
996c9314 | 617 | static void bgp_show_nexthops_detail(struct vty *vty, struct bgp *bgp, |
e22ac3ee DS |
618 | struct bgp_nexthop_cache *bnc) |
619 | { | |
620 | char buf[PREFIX2STR_BUFFER]; | |
621 | struct nexthop *nexthop; | |
622 | ||
623 | for (nexthop = bnc->nexthop; nexthop; nexthop = nexthop->next) | |
624 | switch (nexthop->type) { | |
625 | case NEXTHOP_TYPE_IPV6: | |
626 | vty_out(vty, " gate %s\n", | |
996c9314 LB |
627 | inet_ntop(AF_INET6, &nexthop->gate.ipv6, buf, |
628 | sizeof(buf))); | |
e22ac3ee DS |
629 | break; |
630 | case NEXTHOP_TYPE_IPV6_IFINDEX: | |
631 | vty_out(vty, " gate %s, if %s\n", | |
996c9314 LB |
632 | inet_ntop(AF_INET6, &nexthop->gate.ipv6, buf, |
633 | sizeof(buf)), | |
634 | ifindex2ifname(nexthop->ifindex, bgp->vrf_id)); | |
e22ac3ee DS |
635 | break; |
636 | case NEXTHOP_TYPE_IPV4: | |
637 | vty_out(vty, " gate %s\n", | |
996c9314 LB |
638 | inet_ntop(AF_INET, &nexthop->gate.ipv4, buf, |
639 | sizeof(buf))); | |
e22ac3ee DS |
640 | break; |
641 | case NEXTHOP_TYPE_IFINDEX: | |
642 | vty_out(vty, " if %s\n", | |
996c9314 | 643 | ifindex2ifname(nexthop->ifindex, bgp->vrf_id)); |
e22ac3ee DS |
644 | break; |
645 | case NEXTHOP_TYPE_IPV4_IFINDEX: | |
646 | vty_out(vty, " gate %s, if %s\n", | |
996c9314 LB |
647 | inet_ntop(AF_INET, &nexthop->gate.ipv4, buf, |
648 | sizeof(buf)), | |
649 | ifindex2ifname(nexthop->ifindex, bgp->vrf_id)); | |
e22ac3ee DS |
650 | break; |
651 | case NEXTHOP_TYPE_BLACKHOLE: | |
652 | vty_out(vty, " blackhole\n"); | |
653 | break; | |
654 | default: | |
996c9314 | 655 | vty_out(vty, " invalid nexthop type %u\n", |
e22ac3ee DS |
656 | nexthop->type); |
657 | } | |
658 | } | |
659 | ||
05e47722 PG |
660 | static void bgp_show_nexthops(struct vty *vty, struct bgp *bgp, int detail, |
661 | bool import_table) | |
fb018d25 | 662 | { |
d62a17ae | 663 | struct bgp_node *rn; |
664 | struct bgp_nexthop_cache *bnc; | |
665 | char buf[PREFIX2STR_BUFFER]; | |
d62a17ae | 666 | time_t tbuf; |
667 | afi_t afi; | |
05e47722 | 668 | struct bgp_table **table; |
d62a17ae | 669 | |
05e47722 PG |
670 | if (import_table) |
671 | vty_out(vty, "Current BGP import check cache:\n"); | |
672 | else | |
673 | vty_out(vty, "Current BGP nexthop cache:\n"); | |
674 | if (import_table) | |
675 | table = bgp->import_check_table; | |
676 | else | |
677 | table = bgp->nexthop_cache_table; | |
d62a17ae | 678 | for (afi = AFI_IP; afi < AFI_MAX; afi++) { |
05e47722 | 679 | if (!table || !table[afi]) |
d62a17ae | 680 | continue; |
05e47722 | 681 | for (rn = bgp_table_top(table[afi]); rn; |
d62a17ae | 682 | rn = bgp_route_next(rn)) { |
5408e68e | 683 | struct peer *peer; |
684 | ||
5b8d32bd | 685 | bnc = bgp_node_get_bgp_nexthop_info(rn); |
14315f2d DS |
686 | if (!bnc) |
687 | continue; | |
5408e68e | 688 | peer = (struct peer *)bnc->nht_info; |
14315f2d DS |
689 | |
690 | if (CHECK_FLAG(bnc->flags, BGP_NEXTHOP_VALID)) { | |
691 | vty_out(vty, | |
5408e68e | 692 | " %s valid [IGP metric %d], #paths %d", |
14315f2d DS |
693 | inet_ntop(rn->p.family, |
694 | &rn->p.u.prefix, buf, | |
695 | sizeof(buf)), | |
696 | bnc->metric, bnc->path_count); | |
5408e68e | 697 | if (peer) |
698 | vty_out(vty, ", peer %s", peer->host); | |
699 | vty_out(vty, "\n"); | |
14315f2d DS |
700 | |
701 | if (!detail) | |
702 | continue; | |
703 | ||
704 | bgp_show_nexthops_detail(vty, bgp, bnc); | |
705 | ||
706 | } else { | |
5408e68e | 707 | vty_out(vty, " %s invalid", |
14315f2d DS |
708 | inet_ntop(rn->p.family, |
709 | &rn->p.u.prefix, buf, | |
710 | sizeof(buf))); | |
5408e68e | 711 | if (peer) |
712 | vty_out(vty, ", peer %s", peer->host); | |
713 | vty_out(vty, "\n"); | |
14315f2d DS |
714 | if (CHECK_FLAG(bnc->flags, |
715 | BGP_NEXTHOP_CONNECTED)) | |
716 | vty_out(vty, " Must be Connected\n"); | |
1ee0a2df DS |
717 | if (!CHECK_FLAG(bnc->flags, |
718 | BGP_NEXTHOP_REGISTERED)) | |
719 | vty_out(vty, " Is not Registered\n"); | |
720 | } | |
14315f2d DS |
721 | tbuf = time(NULL) - (bgp_clock() - bnc->last_update); |
722 | vty_out(vty, " Last update: %s", ctime(&tbuf)); | |
723 | vty_out(vty, "\n"); | |
da11a696 | 724 | } |
fb018d25 | 725 | } |
f186de26 | 726 | } |
727 | ||
d62a17ae | 728 | static int show_ip_bgp_nexthop_table(struct vty *vty, const char *name, |
05e47722 | 729 | int detail, bool import_table) |
f186de26 | 730 | { |
d62a17ae | 731 | struct bgp *bgp; |
732 | ||
733 | if (name) | |
734 | bgp = bgp_lookup_by_name(name); | |
735 | else | |
736 | bgp = bgp_get_default(); | |
737 | if (!bgp) { | |
738 | vty_out(vty, "%% No such BGP instance exist\n"); | |
739 | return CMD_WARNING; | |
740 | } | |
f186de26 | 741 | |
05e47722 | 742 | bgp_show_nexthops(vty, bgp, detail, import_table); |
f186de26 | 743 | |
d62a17ae | 744 | return CMD_SUCCESS; |
fb018d25 DS |
745 | } |
746 | ||
d62a17ae | 747 | static void bgp_show_all_instances_nexthops_vty(struct vty *vty) |
f186de26 | 748 | { |
d62a17ae | 749 | struct listnode *node, *nnode; |
750 | struct bgp *bgp; | |
751 | ||
752 | for (ALL_LIST_ELEMENTS(bm->bgp, node, nnode, bgp)) { | |
753 | vty_out(vty, "\nInstance %s:\n", | |
754 | (bgp->inst_type == BGP_INSTANCE_TYPE_DEFAULT) | |
5742e42b | 755 | ? VRF_DEFAULT_NAME |
d62a17ae | 756 | : bgp->name); |
05e47722 | 757 | bgp_show_nexthops(vty, bgp, 0, false); |
d62a17ae | 758 | } |
f186de26 | 759 | } |
760 | ||
fb018d25 DS |
761 | DEFUN (show_ip_bgp_nexthop, |
762 | show_ip_bgp_nexthop_cmd, | |
18c57037 | 763 | "show [ip] bgp [<view|vrf> VIEWVRFNAME] nexthop [detail]", |
50ef26d4 | 764 | SHOW_STR |
765 | IP_STR | |
766 | BGP_STR | |
8386ac43 | 767 | BGP_INSTANCE_HELP_STR |
3a2d747c QY |
768 | "BGP nexthop table\n" |
769 | "Show detailed information\n") | |
50ef26d4 | 770 | { |
d62a17ae | 771 | int idx = 0; |
b7ada628 DS |
772 | char *vrf = NULL; |
773 | ||
774 | if (argv_find(argv, argc, "view", &idx) | |
775 | || argv_find(argv, argc, "vrf", &idx)) | |
776 | vrf = argv[++idx]->arg; | |
d62a17ae | 777 | int detail = argv_find(argv, argc, "detail", &idx) ? 1 : 0; |
05e47722 PG |
778 | |
779 | return show_ip_bgp_nexthop_table(vty, vrf, detail, false); | |
780 | } | |
781 | ||
782 | DEFUN (show_ip_bgp_import_check, | |
783 | show_ip_bgp_import_check_cmd, | |
784 | "show [ip] bgp [<view|vrf> VIEWVRFNAME] import-check-table [detail]", | |
785 | SHOW_STR | |
786 | IP_STR | |
787 | BGP_STR | |
788 | BGP_INSTANCE_HELP_STR | |
789 | "BGP import check table\n" | |
790 | "Show detailed information\n") | |
791 | { | |
792 | int idx = 0; | |
793 | char *vrf = NULL; | |
794 | ||
795 | if (argv_find(argv, argc, "view", &idx) | |
796 | || argv_find(argv, argc, "vrf", &idx)) | |
797 | vrf = argv[++idx]->arg; | |
798 | int detail = argv_find(argv, argc, "detail", &idx) ? 1 : 0; | |
799 | return show_ip_bgp_nexthop_table(vty, vrf, detail, true); | |
50ef26d4 | 800 | } |
801 | ||
f186de26 | 802 | DEFUN (show_ip_bgp_instance_all_nexthop, |
803 | show_ip_bgp_instance_all_nexthop_cmd, | |
bec37ba5 | 804 | "show [ip] bgp <view|vrf> all nexthop", |
f186de26 | 805 | SHOW_STR |
806 | IP_STR | |
807 | BGP_STR | |
808 | BGP_INSTANCE_ALL_HELP_STR | |
809 | "BGP nexthop table\n") | |
810 | { | |
d62a17ae | 811 | bgp_show_all_instances_nexthops_vty(vty); |
812 | return CMD_SUCCESS; | |
f186de26 | 813 | } |
814 | ||
d62a17ae | 815 | void bgp_scan_init(struct bgp *bgp) |
718e3744 | 816 | { |
d62a17ae | 817 | afi_t afi; |
818 | ||
819 | for (afi = AFI_IP; afi < AFI_MAX; afi++) { | |
820 | bgp->nexthop_cache_table[afi] = | |
960035b2 PZ |
821 | bgp_table_init(bgp, afi, SAFI_UNICAST); |
822 | bgp->connected_table[afi] = bgp_table_init(bgp, afi, | |
823 | SAFI_UNICAST); | |
d62a17ae | 824 | bgp->import_check_table[afi] = |
960035b2 | 825 | bgp_table_init(bgp, afi, SAFI_UNICAST); |
d62a17ae | 826 | } |
fc9a856f DS |
827 | } |
828 | ||
d62a17ae | 829 | void bgp_scan_vty_init(void) |
fc9a856f | 830 | { |
d62a17ae | 831 | install_element(VIEW_NODE, &show_ip_bgp_nexthop_cmd); |
05e47722 | 832 | install_element(VIEW_NODE, &show_ip_bgp_import_check_cmd); |
d62a17ae | 833 | install_element(VIEW_NODE, &show_ip_bgp_instance_all_nexthop_cmd); |
718e3744 | 834 | } |
228da428 | 835 | |
d62a17ae | 836 | void bgp_scan_finish(struct bgp *bgp) |
228da428 | 837 | { |
d62a17ae | 838 | afi_t afi; |
6c88b44d | 839 | |
d62a17ae | 840 | for (afi = AFI_IP; afi < AFI_MAX; afi++) { |
841 | /* Only the current one needs to be reset. */ | |
842 | bgp_nexthop_cache_reset(bgp->nexthop_cache_table[afi]); | |
843 | bgp_table_unlock(bgp->nexthop_cache_table[afi]); | |
844 | bgp->nexthop_cache_table[afi] = NULL; | |
228da428 | 845 | |
3292693b DS |
846 | bgp->connected_table[afi]->route_table->cleanup = |
847 | bgp_connected_cleanup; | |
d62a17ae | 848 | bgp_table_unlock(bgp->connected_table[afi]); |
849 | bgp->connected_table[afi] = NULL; | |
078430f6 | 850 | |
d62a17ae | 851 | bgp_table_unlock(bgp->import_check_table[afi]); |
852 | bgp->import_check_table[afi] = NULL; | |
853 | } | |
228da428 | 854 | } |