]>
Commit | Line | Data |
---|---|---|
3f9c7369 DS |
1 | /** |
2 | * bgp_updgrp_packet.c: BGP update group packet handling routines | |
3 | * | |
4 | * @copyright Copyright (C) 2014 Cumulus Networks, Inc. | |
5 | * | |
6 | * @author Avneesh Sachdev <avneesh@sproute.net> | |
7 | * @author Rajesh Varadarajan <rajesh@sproute.net> | |
8 | * @author Pradosh Mohapatra <pradosh@sproute.net> | |
9 | * | |
10 | * This file is part of GNU Zebra. | |
11 | * | |
12 | * GNU Zebra is free software; you can redistribute it and/or modify it | |
13 | * under the terms of the GNU General Public License as published by the | |
14 | * Free Software Foundation; either version 2, or (at your option) any | |
15 | * later version. | |
16 | * | |
17 | * GNU Zebra is distributed in the hope that it will be useful, but | |
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
20 | * General Public License for more details. | |
21 | * | |
896014f4 DL |
22 | * You should have received a copy of the GNU General Public License along |
23 | * with this program; see the file COPYING; if not, write to the Free Software | |
24 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
3f9c7369 DS |
25 | */ |
26 | ||
27 | #include <zebra.h> | |
28 | ||
29 | #include "prefix.h" | |
30 | #include "thread.h" | |
31 | #include "buffer.h" | |
32 | #include "stream.h" | |
33 | #include "command.h" | |
34 | #include "sockunion.h" | |
35 | #include "network.h" | |
36 | #include "memory.h" | |
37 | #include "filter.h" | |
38 | #include "routemap.h" | |
3f9c7369 DS |
39 | #include "log.h" |
40 | #include "plist.h" | |
41 | #include "linklist.h" | |
42 | #include "workqueue.h" | |
43 | #include "hash.h" | |
44 | #include "queue.h" | |
cd1964ff | 45 | #include "mpls.h" |
3f9c7369 DS |
46 | |
47 | #include "bgpd/bgpd.h" | |
48 | #include "bgpd/bgp_debug.h" | |
14454c9f | 49 | #include "bgpd/bgp_errors.h" |
3f9c7369 DS |
50 | #include "bgpd/bgp_fsm.h" |
51 | #include "bgpd/bgp_route.h" | |
52 | #include "bgpd/bgp_packet.h" | |
53 | #include "bgpd/bgp_advertise.h" | |
54 | #include "bgpd/bgp_updgrp.h" | |
55 | #include "bgpd/bgp_nexthop.h" | |
56 | #include "bgpd/bgp_nht.h" | |
906ad49b | 57 | #include "bgpd/bgp_mplsvpn.h" |
cd1964ff | 58 | #include "bgpd/bgp_label.h" |
dcc68b5e | 59 | #include "bgpd/bgp_addpath.h" |
3f9c7369 DS |
60 | |
61 | /******************** | |
62 | * PRIVATE FUNCTIONS | |
63 | ********************/ | |
64 | ||
65 | /******************** | |
66 | * PUBLIC FUNCTIONS | |
67 | ********************/ | |
4d762f26 | 68 | struct bpacket *bpacket_alloc(void) |
3f9c7369 | 69 | { |
d62a17ae | 70 | struct bpacket *pkt; |
3f9c7369 | 71 | |
9f5dc319 | 72 | pkt = XCALLOC(MTYPE_BGP_PACKET, sizeof(struct bpacket)); |
3f9c7369 | 73 | |
d62a17ae | 74 | return pkt; |
3f9c7369 DS |
75 | } |
76 | ||
d62a17ae | 77 | void bpacket_free(struct bpacket *pkt) |
3f9c7369 | 78 | { |
d62a17ae | 79 | if (pkt->buffer) |
80 | stream_free(pkt->buffer); | |
81 | pkt->buffer = NULL; | |
82 | XFREE(MTYPE_BGP_PACKET, pkt); | |
3f9c7369 DS |
83 | } |
84 | ||
d62a17ae | 85 | void bpacket_queue_init(struct bpacket_queue *q) |
3f9c7369 | 86 | { |
d62a17ae | 87 | TAILQ_INIT(&(q->pkts)); |
3f9c7369 DS |
88 | } |
89 | ||
3f9c7369 DS |
90 | /* |
91 | * bpacket_queue_add_packet | |
92 | * | |
93 | * Internal function of bpacket_queue - and adds a | |
94 | * packet entry to the end of the list. | |
95 | * | |
96 | * Users of bpacket_queue should use bpacket_queue_add instead. | |
97 | */ | |
d62a17ae | 98 | static void bpacket_queue_add_packet(struct bpacket_queue *q, |
99 | struct bpacket *pkt) | |
3f9c7369 | 100 | { |
d62a17ae | 101 | struct bpacket *last_pkt; |
3f9c7369 | 102 | |
d62a17ae | 103 | if (TAILQ_EMPTY(&(q->pkts))) |
104 | TAILQ_INSERT_TAIL(&(q->pkts), pkt, pkt_train); | |
105 | else { | |
106 | last_pkt = bpacket_queue_last(q); | |
107 | TAILQ_INSERT_AFTER(&(q->pkts), last_pkt, pkt, pkt_train); | |
108 | } | |
109 | q->curr_count++; | |
110 | if (q->hwm_count < q->curr_count) | |
111 | q->hwm_count = q->curr_count; | |
3f9c7369 DS |
112 | } |
113 | ||
114 | /* | |
115 | * Adds a packet to the bpacket_queue. | |
116 | * | |
117 | * The stream passed is consumed by this function. So, the caller should | |
118 | * not free or use the stream after | |
119 | * invoking this function. | |
120 | */ | |
d62a17ae | 121 | struct bpacket *bpacket_queue_add(struct bpacket_queue *q, struct stream *s, |
122 | struct bpacket_attr_vec_arr *vecarrp) | |
3f9c7369 | 123 | { |
d62a17ae | 124 | struct bpacket *pkt; |
125 | struct bpacket *last_pkt; | |
126 | ||
127 | ||
128 | pkt = bpacket_alloc(); | |
129 | if (TAILQ_EMPTY(&(q->pkts))) { | |
130 | pkt->ver = 1; | |
131 | pkt->buffer = s; | |
132 | if (vecarrp) | |
133 | memcpy(&pkt->arr, vecarrp, | |
134 | sizeof(struct bpacket_attr_vec_arr)); | |
135 | else | |
136 | bpacket_attr_vec_arr_reset(&pkt->arr); | |
137 | bpacket_queue_add_packet(q, pkt); | |
d62a17ae | 138 | return pkt; |
139 | } | |
3f9c7369 | 140 | |
d62a17ae | 141 | /* |
142 | * Fill in the new information into the current sentinel and create a | |
143 | * new sentinel. | |
144 | */ | |
d62a17ae | 145 | last_pkt = bpacket_queue_last(q); |
146 | assert(last_pkt->buffer == NULL); | |
147 | last_pkt->buffer = s; | |
148 | if (vecarrp) | |
149 | memcpy(&last_pkt->arr, vecarrp, | |
150 | sizeof(struct bpacket_attr_vec_arr)); | |
151 | else | |
152 | bpacket_attr_vec_arr_reset(&last_pkt->arr); | |
153 | ||
154 | pkt->ver = last_pkt->ver; | |
155 | pkt->ver++; | |
156 | bpacket_queue_add_packet(q, pkt); | |
157 | ||
d62a17ae | 158 | return last_pkt; |
3f9c7369 DS |
159 | } |
160 | ||
d62a17ae | 161 | struct bpacket *bpacket_queue_first(struct bpacket_queue *q) |
3f9c7369 | 162 | { |
d62a17ae | 163 | return (TAILQ_FIRST(&(q->pkts))); |
3f9c7369 DS |
164 | } |
165 | ||
d62a17ae | 166 | struct bpacket *bpacket_queue_last(struct bpacket_queue *q) |
3f9c7369 | 167 | { |
d62a17ae | 168 | return TAILQ_LAST(&(q->pkts), pkt_queue); |
3f9c7369 DS |
169 | } |
170 | ||
d62a17ae | 171 | struct bpacket *bpacket_queue_remove(struct bpacket_queue *q) |
3f9c7369 | 172 | { |
d62a17ae | 173 | struct bpacket *first; |
3f9c7369 | 174 | |
d62a17ae | 175 | first = bpacket_queue_first(q); |
176 | if (first) { | |
177 | TAILQ_REMOVE(&(q->pkts), first, pkt_train); | |
178 | q->curr_count--; | |
179 | } | |
180 | return first; | |
3f9c7369 DS |
181 | } |
182 | ||
d62a17ae | 183 | unsigned int bpacket_queue_length(struct bpacket_queue *q) |
3f9c7369 | 184 | { |
d62a17ae | 185 | return q->curr_count - 1; |
3f9c7369 DS |
186 | } |
187 | ||
d62a17ae | 188 | unsigned int bpacket_queue_hwm_length(struct bpacket_queue *q) |
3f9c7369 | 189 | { |
d62a17ae | 190 | return q->hwm_count - 1; |
3f9c7369 DS |
191 | } |
192 | ||
3dc339cd | 193 | bool bpacket_queue_is_full(struct bgp *bgp, struct bpacket_queue *q) |
3f9c7369 | 194 | { |
d62a17ae | 195 | if (q->curr_count >= bgp->default_subgroup_pkt_queue_max) |
3dc339cd DA |
196 | return true; |
197 | return false; | |
3f9c7369 DS |
198 | } |
199 | ||
d62a17ae | 200 | void bpacket_add_peer(struct bpacket *pkt, struct peer_af *paf) |
3f9c7369 | 201 | { |
d62a17ae | 202 | if (!pkt || !paf) |
203 | return; | |
3f9c7369 | 204 | |
d62a17ae | 205 | LIST_INSERT_HEAD(&(pkt->peers), paf, pkt_train); |
206 | paf->next_pkt_to_send = pkt; | |
3f9c7369 DS |
207 | } |
208 | ||
209 | /* | |
210 | * bpacket_queue_cleanup | |
211 | */ | |
d62a17ae | 212 | void bpacket_queue_cleanup(struct bpacket_queue *q) |
3f9c7369 | 213 | { |
d62a17ae | 214 | struct bpacket *pkt; |
3f9c7369 | 215 | |
d62a17ae | 216 | while ((pkt = bpacket_queue_remove(q))) { |
217 | bpacket_free(pkt); | |
218 | } | |
3f9c7369 DS |
219 | } |
220 | ||
221 | /* | |
222 | * bpacket_queue_compact | |
223 | * | |
224 | * Delete packets that do not need to be transmitted to any peer from | |
225 | * the queue. | |
226 | * | |
227 | * @return the number of packets deleted. | |
228 | */ | |
d62a17ae | 229 | static int bpacket_queue_compact(struct bpacket_queue *q) |
3f9c7369 | 230 | { |
d62a17ae | 231 | int num_deleted; |
232 | struct bpacket *pkt, *removed_pkt; | |
3f9c7369 | 233 | |
d62a17ae | 234 | num_deleted = 0; |
3f9c7369 | 235 | |
d62a17ae | 236 | while (1) { |
237 | pkt = bpacket_queue_first(q); | |
238 | if (!pkt) | |
239 | break; | |
3f9c7369 | 240 | |
d62a17ae | 241 | /* |
242 | * Don't delete the sentinel. | |
243 | */ | |
244 | if (!pkt->buffer) | |
245 | break; | |
3f9c7369 | 246 | |
d62a17ae | 247 | if (!LIST_EMPTY(&(pkt->peers))) |
248 | break; | |
3f9c7369 | 249 | |
d62a17ae | 250 | removed_pkt = bpacket_queue_remove(q); |
251 | assert(pkt == removed_pkt); | |
252 | bpacket_free(removed_pkt); | |
3f9c7369 | 253 | |
d62a17ae | 254 | num_deleted++; |
255 | } | |
3f9c7369 | 256 | |
d62a17ae | 257 | return num_deleted; |
3f9c7369 DS |
258 | } |
259 | ||
d62a17ae | 260 | void bpacket_queue_advance_peer(struct peer_af *paf) |
3f9c7369 | 261 | { |
d62a17ae | 262 | struct bpacket *pkt; |
263 | struct bpacket *old_pkt; | |
264 | ||
265 | old_pkt = paf->next_pkt_to_send; | |
266 | if (old_pkt->buffer == NULL) | |
267 | /* Already at end of list */ | |
268 | return; | |
269 | ||
270 | LIST_REMOVE(paf, pkt_train); | |
271 | pkt = TAILQ_NEXT(old_pkt, pkt_train); | |
272 | bpacket_add_peer(pkt, paf); | |
273 | ||
274 | if (!bpacket_queue_compact(PAF_PKTQ(paf))) | |
275 | return; | |
276 | ||
277 | /* | |
278 | * Deleted one or more packets. Check if we can now merge this | |
279 | * peer's subgroup into another subgroup. | |
280 | */ | |
281 | update_subgroup_check_merge(paf->subgroup, "advanced peer in queue"); | |
3f9c7369 DS |
282 | } |
283 | ||
284 | /* | |
285 | * bpacket_queue_remove_peer | |
286 | * | |
287 | * Remove the peer from the packet queue of the subgroup it belongs | |
288 | * to. | |
289 | */ | |
d62a17ae | 290 | void bpacket_queue_remove_peer(struct peer_af *paf) |
3f9c7369 | 291 | { |
d62a17ae | 292 | struct bpacket_queue *q; |
3f9c7369 | 293 | |
d62a17ae | 294 | q = PAF_PKTQ(paf); |
295 | assert(q); | |
3f9c7369 | 296 | |
d62a17ae | 297 | LIST_REMOVE(paf, pkt_train); |
298 | paf->next_pkt_to_send = NULL; | |
3f9c7369 | 299 | |
d62a17ae | 300 | bpacket_queue_compact(q); |
3f9c7369 DS |
301 | } |
302 | ||
d62a17ae | 303 | unsigned int bpacket_queue_virtual_length(struct peer_af *paf) |
3f9c7369 | 304 | { |
d62a17ae | 305 | struct bpacket *pkt; |
306 | struct bpacket *last; | |
307 | struct bpacket_queue *q; | |
3f9c7369 | 308 | |
d62a17ae | 309 | pkt = paf->next_pkt_to_send; |
310 | if (!pkt || (pkt->buffer == NULL)) | |
311 | /* Already at end of list */ | |
312 | return 0; | |
3f9c7369 | 313 | |
d62a17ae | 314 | q = PAF_PKTQ(paf); |
315 | if (TAILQ_EMPTY(&(q->pkts))) | |
316 | return 0; | |
3f9c7369 | 317 | |
d62a17ae | 318 | last = TAILQ_LAST(&(q->pkts), pkt_queue); |
319 | if (last->ver >= pkt->ver) | |
320 | return last->ver - pkt->ver; | |
3f9c7369 | 321 | |
d62a17ae | 322 | /* sequence # rolled over */ |
323 | return (UINT_MAX - pkt->ver + 1) + last->ver; | |
3f9c7369 DS |
324 | } |
325 | ||
326 | /* | |
327 | * Dump the bpacket queue | |
328 | */ | |
d62a17ae | 329 | void bpacket_queue_show_vty(struct bpacket_queue *q, struct vty *vty) |
3f9c7369 | 330 | { |
d62a17ae | 331 | struct bpacket *pkt; |
332 | struct peer_af *paf; | |
333 | ||
334 | pkt = bpacket_queue_first(q); | |
335 | while (pkt) { | |
336 | vty_out(vty, " Packet %p ver %u buffer %p\n", pkt, pkt->ver, | |
337 | pkt->buffer); | |
338 | ||
a2addae8 | 339 | LIST_FOREACH (paf, &(pkt->peers), pkt_train) { |
d62a17ae | 340 | vty_out(vty, " - %s\n", paf->peer->host); |
341 | } | |
342 | pkt = bpacket_next(pkt); | |
343 | } | |
344 | return; | |
3f9c7369 DS |
345 | } |
346 | ||
d62a17ae | 347 | struct stream *bpacket_reformat_for_peer(struct bpacket *pkt, |
348 | struct peer_af *paf) | |
3f9c7369 | 349 | { |
d62a17ae | 350 | struct stream *s = NULL; |
351 | bpacket_attr_vec *vec; | |
352 | struct peer *peer; | |
1c875ddb | 353 | struct bgp_filter *filter; |
d62a17ae | 354 | |
355 | s = stream_dup(pkt->buffer); | |
356 | peer = PAF_PEER(paf); | |
357 | ||
358 | vec = &pkt->arr.entries[BGP_ATTR_VEC_NH]; | |
d62a17ae | 359 | |
f041034e DS |
360 | if (!CHECK_FLAG(vec->flags, BPKT_ATTRVEC_FLAGS_UPDATED)) |
361 | return s; | |
362 | ||
363 | uint8_t nhlen; | |
364 | afi_t nhafi; | |
365 | int route_map_sets_nh; | |
366 | ||
367 | nhlen = stream_getc_from(s, vec->offset); | |
368 | filter = &peer->filter[paf->afi][paf->safi]; | |
369 | ||
370 | if (peer_cap_enhe(peer, paf->afi, paf->safi)) | |
371 | nhafi = AFI_IP6; | |
372 | else | |
373 | nhafi = BGP_NEXTHOP_AFI_FROM_NHLEN(nhlen); | |
374 | ||
375 | if (nhafi == AFI_IP) { | |
376 | struct in_addr v4nh, *mod_v4nh; | |
377 | int nh_modified = 0; | |
378 | size_t offset_nh = vec->offset + 1; | |
379 | ||
380 | route_map_sets_nh = | |
381 | (CHECK_FLAG(vec->flags, | |
382 | BPKT_ATTRVEC_FLAGS_RMAP_IPV4_NH_CHANGED) | |
383 | || CHECK_FLAG( | |
384 | vec->flags, | |
385 | BPKT_ATTRVEC_FLAGS_RMAP_NH_PEER_ADDRESS)); | |
386 | ||
387 | switch (nhlen) { | |
388 | case BGP_ATTR_NHLEN_IPV4: | |
389 | break; | |
390 | case BGP_ATTR_NHLEN_VPNV4: | |
391 | offset_nh += 8; | |
392 | break; | |
393 | default: | |
394 | /* TODO: handle IPv6 nexthops */ | |
395 | flog_warn( | |
396 | EC_BGP_INVALID_NEXTHOP_LENGTH, | |
397 | "%s: %s: invalid MP nexthop length (AFI IP): %u", | |
398 | __func__, peer->host, nhlen); | |
399 | stream_free(s); | |
400 | return NULL; | |
401 | } | |
402 | ||
403 | stream_get_from(&v4nh, s, offset_nh, IPV4_MAX_BYTELEN); | |
404 | mod_v4nh = &v4nh; | |
405 | ||
406 | /* | |
407 | * If route-map has set the nexthop, that is normally | |
408 | * used; if it is specified as peer-address, the peering | |
409 | * address is picked up. Otherwise, if NH is unavailable | |
410 | * from attribute, the peering addr is picked up; the | |
411 | * "NH unavailable" case also covers next-hop-self and | |
412 | * some other scenarios - see subgroup_announce_check(). | |
413 | * In all other cases, use the nexthop carried in the | |
414 | * attribute unless it is EBGP non-multiaccess and there | |
415 | * is no next-hop-unchanged setting or the peer is EBGP | |
416 | * and the route-map that changed the next-hop value | |
417 | * was applied inbound rather than outbound. Updates to | |
418 | * an EBGP peer should only modify the next-hop if it | |
419 | * was set in an outbound route-map to that peer. | |
420 | * Note: It is assumed route-map cannot set the nexthop | |
421 | * to an invalid value. | |
422 | */ | |
423 | if (route_map_sets_nh | |
424 | && ((peer->sort != BGP_PEER_EBGP) | |
425 | || ROUTE_MAP_OUT(filter))) { | |
426 | if (CHECK_FLAG( | |
427 | vec->flags, | |
428 | BPKT_ATTRVEC_FLAGS_RMAP_NH_PEER_ADDRESS)) { | |
d62a17ae | 429 | mod_v4nh = &peer->nexthop.v4; |
430 | nh_modified = 1; | |
431 | } | |
f041034e DS |
432 | } else if (v4nh.s_addr == INADDR_ANY) { |
433 | mod_v4nh = &peer->nexthop.v4; | |
434 | nh_modified = 1; | |
435 | } else if (peer->sort == BGP_PEER_EBGP | |
436 | && (bgp_multiaccess_check_v4(v4nh, peer) == 0) | |
437 | && !CHECK_FLAG(vec->flags, | |
438 | BPKT_ATTRVEC_FLAGS_RMAP_NH_UNCHANGED) | |
439 | && !peer_af_flag_check( | |
440 | peer, paf->afi, paf->safi, | |
441 | PEER_FLAG_NEXTHOP_UNCHANGED)) { | |
442 | /* NOTE: not handling case where NH has new AFI | |
443 | */ | |
444 | mod_v4nh = &peer->nexthop.v4; | |
445 | nh_modified = 1; | |
446 | } | |
d62a17ae | 447 | |
f041034e DS |
448 | if (nh_modified) /* allow for VPN RD */ |
449 | stream_put_in_addr_at(s, offset_nh, mod_v4nh); | |
450 | ||
451 | if (bgp_debug_update(peer, NULL, NULL, 0)) | |
23d0a753 DA |
452 | zlog_debug("u%" PRIu64 ":s%" PRIu64 |
453 | " %s send UPDATE w/ nexthop %pI4%s", | |
f041034e | 454 | PAF_SUBGRP(paf)->update_group->id, |
23d0a753 | 455 | PAF_SUBGRP(paf)->id, peer->host, mod_v4nh, |
3ddd6994 DA |
456 | (nhlen == BGP_ATTR_NHLEN_VPNV4 ? " and RD" |
457 | : "")); | |
f041034e DS |
458 | } else if (nhafi == AFI_IP6) { |
459 | struct in6_addr v6nhglobal, *mod_v6nhg; | |
460 | struct in6_addr v6nhlocal, *mod_v6nhl; | |
461 | int gnh_modified, lnh_modified; | |
462 | size_t offset_nhglobal = vec->offset + 1; | |
463 | size_t offset_nhlocal = vec->offset + 1; | |
464 | ||
465 | gnh_modified = lnh_modified = 0; | |
466 | mod_v6nhg = &v6nhglobal; | |
467 | mod_v6nhl = &v6nhlocal; | |
468 | ||
469 | route_map_sets_nh = | |
470 | (CHECK_FLAG(vec->flags, | |
471 | BPKT_ATTRVEC_FLAGS_RMAP_IPV6_GNH_CHANGED) | |
472 | || CHECK_FLAG( | |
473 | vec->flags, | |
474 | BPKT_ATTRVEC_FLAGS_RMAP_NH_PEER_ADDRESS)); | |
d62a17ae | 475 | |
f041034e DS |
476 | /* |
477 | * The logic here is rather similar to that for IPv4, the | |
478 | * additional work being to handle 1 or 2 nexthops. | |
479 | * Also, 3rd party nexthop is not propagated for EBGP | |
480 | * right now. | |
481 | */ | |
482 | switch (nhlen) { | |
483 | case BGP_ATTR_NHLEN_IPV6_GLOBAL: | |
484 | break; | |
485 | case BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL: | |
486 | offset_nhlocal += IPV6_MAX_BYTELEN; | |
487 | break; | |
488 | case BGP_ATTR_NHLEN_VPNV6_GLOBAL: | |
489 | offset_nhglobal += 8; | |
490 | break; | |
491 | case BGP_ATTR_NHLEN_VPNV6_GLOBAL_AND_LL: | |
492 | offset_nhglobal += 8; | |
493 | offset_nhlocal += 8 * 2 + IPV6_MAX_BYTELEN; | |
494 | break; | |
495 | default: | |
496 | /* TODO: handle IPv4 nexthops */ | |
497 | flog_warn( | |
498 | EC_BGP_INVALID_NEXTHOP_LENGTH, | |
499 | "%s: %s: invalid MP nexthop length (AFI IP6): %u", | |
500 | __func__, peer->host, nhlen); | |
501 | stream_free(s); | |
502 | return NULL; | |
503 | } | |
d62a17ae | 504 | |
f041034e DS |
505 | stream_get_from(&v6nhglobal, s, offset_nhglobal, |
506 | IPV6_MAX_BYTELEN); | |
1c875ddb | 507 | |
f041034e DS |
508 | /* |
509 | * Updates to an EBGP peer should only modify the | |
510 | * next-hop if it was set in an outbound route-map | |
511 | * to that peer. | |
512 | */ | |
513 | if (route_map_sets_nh | |
514 | && ((peer->sort != BGP_PEER_EBGP) | |
515 | || ROUTE_MAP_OUT(filter))) { | |
516 | if (CHECK_FLAG( | |
517 | vec->flags, | |
518 | BPKT_ATTRVEC_FLAGS_RMAP_NH_PEER_ADDRESS)) { | |
d62a17ae | 519 | mod_v6nhg = &peer->nexthop.v6_global; |
520 | gnh_modified = 1; | |
521 | } | |
f041034e DS |
522 | } else if (IN6_IS_ADDR_UNSPECIFIED(&v6nhglobal)) { |
523 | mod_v6nhg = &peer->nexthop.v6_global; | |
524 | gnh_modified = 1; | |
525 | } else if ((peer->sort == BGP_PEER_EBGP) | |
526 | && (!bgp_multiaccess_check_v6(v6nhglobal, peer)) | |
527 | && !CHECK_FLAG(vec->flags, | |
528 | BPKT_ATTRVEC_FLAGS_RMAP_NH_UNCHANGED) | |
529 | && !peer_af_flag_check( | |
530 | peer, nhafi, paf->safi, | |
531 | PEER_FLAG_NEXTHOP_UNCHANGED)) { | |
532 | /* NOTE: not handling case where NH has new AFI | |
533 | */ | |
534 | mod_v6nhg = &peer->nexthop.v6_global; | |
535 | gnh_modified = 1; | |
536 | } | |
d62a17ae | 537 | |
92d6f769 | 538 | if (IN6_IS_ADDR_UNSPECIFIED(mod_v6nhg)) { |
3a6290bd | 539 | if (peer->nexthop.v4.s_addr != INADDR_ANY) { |
92d6f769 K |
540 | ipv4_to_ipv4_mapped_ipv6(mod_v6nhg, |
541 | peer->nexthop.v4); | |
542 | } | |
543 | } | |
544 | ||
545 | if (IS_MAPPED_IPV6(&peer->nexthop.v6_global)) { | |
546 | mod_v6nhg = &peer->nexthop.v6_global; | |
547 | gnh_modified = 1; | |
548 | } | |
549 | ||
f041034e DS |
550 | if (nhlen == BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL |
551 | || nhlen == BGP_ATTR_NHLEN_VPNV6_GLOBAL_AND_LL) { | |
552 | stream_get_from(&v6nhlocal, s, offset_nhlocal, | |
553 | IPV6_MAX_BYTELEN); | |
554 | if (IN6_IS_ADDR_UNSPECIFIED(&v6nhlocal)) { | |
555 | mod_v6nhl = &peer->nexthop.v6_local; | |
556 | lnh_modified = 1; | |
d62a17ae | 557 | } |
f041034e | 558 | } |
d62a17ae | 559 | |
f041034e DS |
560 | if (gnh_modified) |
561 | stream_put_in6_addr_at(s, offset_nhglobal, mod_v6nhg); | |
562 | if (lnh_modified) | |
563 | stream_put_in6_addr_at(s, offset_nhlocal, mod_v6nhl); | |
564 | ||
565 | if (bgp_debug_update(peer, NULL, NULL, 0)) { | |
3ddd6994 DA |
566 | if (nhlen == BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL |
567 | || nhlen == BGP_ATTR_NHLEN_VPNV6_GLOBAL_AND_LL) | |
f041034e | 568 | zlog_debug( |
c0d72166 DS |
569 | "u%" PRIu64 ":s%" PRIu64 |
570 | " %s send UPDATE w/ mp_nexthops %pI6, %pI6%s", | |
f041034e DS |
571 | PAF_SUBGRP(paf)->update_group->id, |
572 | PAF_SUBGRP(paf)->id, peer->host, | |
c0d72166 | 573 | mod_v6nhg, mod_v6nhl, |
3ddd6994 DA |
574 | (nhlen == BGP_ATTR_NHLEN_VPNV6_GLOBAL_AND_LL |
575 | ? " and RD" | |
576 | : "")); | |
f041034e | 577 | else |
c0d72166 DS |
578 | zlog_debug( |
579 | "u%" PRIu64 ":s%" PRIu64 | |
580 | " %s send UPDATE w/ mp_nexthop %pI6%s", | |
581 | PAF_SUBGRP(paf)->update_group->id, | |
582 | PAF_SUBGRP(paf)->id, peer->host, | |
583 | mod_v6nhg, | |
584 | (nhlen == BGP_ATTR_NHLEN_VPNV6_GLOBAL | |
585 | ? " and RD" | |
586 | : "")); | |
f041034e DS |
587 | } |
588 | } else if (paf->afi == AFI_L2VPN) { | |
589 | struct in_addr v4nh, *mod_v4nh; | |
590 | int nh_modified = 0; | |
591 | ||
592 | stream_get_from(&v4nh, s, vec->offset + 1, 4); | |
593 | mod_v4nh = &v4nh; | |
594 | ||
595 | /* No route-map changes allowed for EVPN nexthops. */ | |
596 | if (v4nh.s_addr == INADDR_ANY) { | |
597 | mod_v4nh = &peer->nexthop.v4; | |
598 | nh_modified = 1; | |
d62a17ae | 599 | } |
f041034e DS |
600 | |
601 | if (nh_modified) | |
602 | stream_put_in_addr_at(s, vec->offset + 1, mod_v4nh); | |
603 | ||
604 | if (bgp_debug_update(peer, NULL, NULL, 0)) | |
23d0a753 DA |
605 | zlog_debug("u%" PRIu64 ":s%" PRIu64 |
606 | " %s send UPDATE w/ nexthop %pI4", | |
f041034e | 607 | PAF_SUBGRP(paf)->update_group->id, |
23d0a753 | 608 | PAF_SUBGRP(paf)->id, peer->host, mod_v4nh); |
3f9c7369 | 609 | } |
3f9c7369 | 610 | |
d62a17ae | 611 | return s; |
3f9c7369 DS |
612 | } |
613 | ||
614 | /* | |
615 | * Update the vecarr offsets to go beyond 'pos' bytes, i.e. add 'pos' | |
616 | * to each offset. | |
617 | */ | |
d62a17ae | 618 | static void bpacket_attr_vec_arr_update(struct bpacket_attr_vec_arr *vecarr, |
619 | size_t pos) | |
3f9c7369 | 620 | { |
d62a17ae | 621 | int i; |
3f9c7369 | 622 | |
d62a17ae | 623 | if (!vecarr) |
624 | return; | |
3f9c7369 | 625 | |
d62a17ae | 626 | for (i = 0; i < BGP_ATTR_VEC_MAX; i++) |
627 | vecarr->entries[i].offset += pos; | |
3f9c7369 DS |
628 | } |
629 | ||
630 | /* | |
631 | * Return if there are packets to build for this subgroup. | |
632 | */ | |
3dc339cd | 633 | bool subgroup_packets_to_build(struct update_subgroup *subgrp) |
3f9c7369 | 634 | { |
d62a17ae | 635 | struct bgp_advertise *adv; |
3f9c7369 | 636 | |
d62a17ae | 637 | if (!subgrp) |
3dc339cd | 638 | return false; |
3f9c7369 | 639 | |
a274fef8 | 640 | adv = bgp_adv_fifo_first(&subgrp->sync->withdraw); |
d62a17ae | 641 | if (adv) |
3dc339cd | 642 | return true; |
3f9c7369 | 643 | |
a274fef8 | 644 | adv = bgp_adv_fifo_first(&subgrp->sync->update); |
d62a17ae | 645 | if (adv) |
3dc339cd | 646 | return true; |
3f9c7369 | 647 | |
3dc339cd | 648 | return false; |
3f9c7369 DS |
649 | } |
650 | ||
651 | /* Make BGP update packet. */ | |
d62a17ae | 652 | struct bpacket *subgroup_update_packet(struct update_subgroup *subgrp) |
3f9c7369 | 653 | { |
d62a17ae | 654 | struct bpacket_attr_vec_arr vecarr; |
655 | struct bpacket *pkt; | |
656 | struct peer *peer; | |
657 | struct stream *s; | |
658 | struct stream *snlri; | |
659 | struct stream *packet; | |
660 | struct bgp_adj_out *adj; | |
661 | struct bgp_advertise *adv; | |
9bcb3eef | 662 | struct bgp_dest *dest = NULL; |
9b6d8fcf | 663 | struct bgp_path_info *path = NULL; |
d62a17ae | 664 | bgp_size_t total_attr_len = 0; |
665 | unsigned long attrlen_pos = 0; | |
666 | size_t mpattrlen_pos = 0; | |
667 | size_t mpattr_pos = 0; | |
668 | afi_t afi; | |
669 | safi_t safi; | |
670 | int space_remaining = 0; | |
671 | int space_needed = 0; | |
672 | char send_attr_str[BUFSIZ]; | |
673 | int send_attr_printed = 0; | |
674 | int num_pfx = 0; | |
be92fc9f | 675 | bool addpath_capable = false; |
e386c1d5 | 676 | int addpath_overhead = 0; |
d7c0a89a | 677 | uint32_t addpath_tx_id = 0; |
d62a17ae | 678 | struct prefix_rd *prd = NULL; |
b57ba6d2 | 679 | mpls_label_t label = MPLS_INVALID_LABEL, *label_pnt = NULL; |
d7c0a89a | 680 | uint32_t num_labels = 0; |
d62a17ae | 681 | |
682 | if (!subgrp) | |
683 | return NULL; | |
3f9c7369 | 684 | |
d62a17ae | 685 | if (bpacket_queue_is_full(SUBGRP_INST(subgrp), SUBGRP_PKTQ(subgrp))) |
686 | return NULL; | |
3f9c7369 | 687 | |
d62a17ae | 688 | peer = SUBGRP_PEER(subgrp); |
689 | afi = SUBGRP_AFI(subgrp); | |
690 | safi = SUBGRP_SAFI(subgrp); | |
691 | s = subgrp->work; | |
692 | stream_reset(s); | |
693 | snlri = subgrp->scratch; | |
694 | stream_reset(snlri); | |
695 | ||
696 | bpacket_attr_vec_arr_reset(&vecarr); | |
697 | ||
be92fc9f DA |
698 | addpath_capable = bgp_addpath_encode_tx(peer, afi, safi); |
699 | addpath_overhead = addpath_capable ? BGP_ADDPATH_ID_LEN : 0; | |
d62a17ae | 700 | |
a274fef8 | 701 | adv = bgp_adv_fifo_first(&subgrp->sync->update); |
d62a17ae | 702 | while (adv) { |
9bcb3eef | 703 | const struct prefix *dest_p; |
b54892e0 | 704 | |
9bcb3eef DS |
705 | assert(adv->dest); |
706 | dest = adv->dest; | |
707 | dest_p = bgp_dest_get_prefix(dest); | |
d62a17ae | 708 | adj = adv->adj; |
709 | addpath_tx_id = adj->addpath_tx_id; | |
9b6d8fcf | 710 | path = adv->pathi; |
d62a17ae | 711 | |
712 | space_remaining = STREAM_CONCAT_REMAIN(s, snlri, STREAM_SIZE(s)) | |
713 | - BGP_MAX_PACKET_SIZE_OVERFLOW; | |
9bcb3eef DS |
714 | space_needed = |
715 | BGP_NLRI_LENGTH + addpath_overhead | |
716 | + bgp_packet_mpattr_prefix_size(afi, safi, dest_p); | |
d62a17ae | 717 | |
718 | /* When remaining space can't include NLRI and it's length. */ | |
719 | if (space_remaining < space_needed) | |
720 | break; | |
721 | ||
722 | /* If packet is empty, set attribute. */ | |
723 | if (stream_empty(s)) { | |
724 | struct peer *from = NULL; | |
725 | ||
9b6d8fcf DS |
726 | if (path) |
727 | from = path->peer; | |
d62a17ae | 728 | |
729 | /* 1: Write the BGP message header - 16 bytes marker, 2 | |
730 | * bytes length, | |
731 | * one byte message type. | |
732 | */ | |
733 | bgp_packet_set_marker(s, BGP_MSG_UPDATE); | |
734 | ||
735 | /* 2: withdrawn routes length */ | |
736 | stream_putw(s, 0); | |
737 | ||
738 | /* 3: total attributes length - attrlen_pos stores the | |
739 | * position */ | |
740 | attrlen_pos = stream_get_endp(s); | |
741 | stream_putw(s, 0); | |
742 | ||
743 | /* 4: if there is MP_REACH_NLRI attribute, that should | |
744 | * be the first | |
745 | * attribute, according to | |
746 | * draft-ietf-idr-error-handling. Save the | |
747 | * position. | |
748 | */ | |
749 | mpattr_pos = stream_get_endp(s); | |
750 | ||
751 | /* 5: Encode all the attributes, except MP_REACH_NLRI | |
752 | * attr. */ | |
753 | total_attr_len = bgp_packet_attribute( | |
754 | NULL, peer, s, adv->baa->attr, &vecarr, NULL, | |
b57ba6d2 | 755 | afi, safi, from, NULL, NULL, 0, 0, 0); |
d62a17ae | 756 | |
757 | space_remaining = | |
758 | STREAM_CONCAT_REMAIN(s, snlri, STREAM_SIZE(s)) | |
759 | - BGP_MAX_PACKET_SIZE_OVERFLOW; | |
996c9314 LB |
760 | space_needed = BGP_NLRI_LENGTH + addpath_overhead |
761 | + bgp_packet_mpattr_prefix_size( | |
9bcb3eef | 762 | afi, safi, dest_p); |
d62a17ae | 763 | |
764 | /* If the attributes alone do not leave any room for | |
765 | * NLRI then | |
766 | * return */ | |
767 | if (space_remaining < space_needed) { | |
af4c2728 | 768 | flog_err( |
e50f7cfd | 769 | EC_BGP_UPDGRP_ATTR_LEN, |
6cde4b45 | 770 | "u%" PRIu64 ":s%" PRIu64" attributes too long, cannot send UPDATE", |
d62a17ae | 771 | subgrp->update_group->id, subgrp->id); |
772 | ||
773 | /* Flush the FIFO update queue */ | |
774 | while (adv) | |
775 | adv = bgp_advertise_clean_subgroup( | |
776 | subgrp, adj); | |
777 | return NULL; | |
778 | } | |
779 | ||
780 | if (BGP_DEBUG(update, UPDATE_OUT) | |
781 | || BGP_DEBUG(update, UPDATE_PREFIX)) { | |
782 | memset(send_attr_str, 0, BUFSIZ); | |
783 | send_attr_printed = 0; | |
784 | bgp_dump_attr(adv->baa->attr, send_attr_str, | |
5022c833 | 785 | sizeof(send_attr_str)); |
d62a17ae | 786 | } |
787 | } | |
788 | ||
789 | if ((afi == AFI_IP && safi == SAFI_UNICAST) | |
790 | && !peer_cap_enhe(peer, afi, safi)) | |
be92fc9f | 791 | stream_put_prefix_addpath(s, dest_p, addpath_capable, |
d62a17ae | 792 | addpath_tx_id); |
793 | else { | |
794 | /* Encode the prefix in MP_REACH_NLRI attribute */ | |
9bcb3eef DS |
795 | if (dest->pdest) |
796 | prd = (struct prefix_rd *)bgp_dest_get_prefix( | |
797 | dest->pdest); | |
d62a17ae | 798 | |
b57ba6d2 | 799 | if (safi == SAFI_LABELED_UNICAST) { |
9bcb3eef | 800 | label = bgp_adv_label(dest, path, peer, afi, |
d62a17ae | 801 | safi); |
b57ba6d2 MK |
802 | label_pnt = &label; |
803 | num_labels = 1; | |
9b6d8fcf DS |
804 | } else if (path && path->extra) { |
805 | label_pnt = &path->extra->label[0]; | |
806 | num_labels = path->extra->num_labels; | |
b57ba6d2 | 807 | } |
d62a17ae | 808 | |
809 | if (stream_empty(snlri)) | |
810 | mpattrlen_pos = bgp_packet_mpattr_start( | |
811 | snlri, peer, afi, safi, &vecarr, | |
812 | adv->baa->attr); | |
813 | ||
9bcb3eef | 814 | bgp_packet_mpattr_prefix(snlri, afi, safi, dest_p, prd, |
b57ba6d2 | 815 | label_pnt, num_labels, |
be92fc9f | 816 | addpath_capable, addpath_tx_id, |
b57ba6d2 | 817 | adv->baa->attr); |
d62a17ae | 818 | } |
819 | ||
820 | num_pfx++; | |
821 | ||
9bcb3eef | 822 | if (bgp_debug_update(NULL, dest_p, subgrp->update_group, 0)) { |
d62a17ae | 823 | char pfx_buf[BGP_PRD_PATH_STRLEN]; |
824 | ||
825 | if (!send_attr_printed) { | |
6cde4b45 | 826 | zlog_debug("u%" PRIu64 ":s%" PRIu64" send UPDATE w/ attr: %s", |
d62a17ae | 827 | subgrp->update_group->id, subgrp->id, |
828 | send_attr_str); | |
829 | if (!stream_empty(snlri)) { | |
830 | iana_afi_t pkt_afi; | |
5c525538 | 831 | iana_safi_t pkt_safi; |
d62a17ae | 832 | |
833 | pkt_afi = afi_int2iana(afi); | |
834 | pkt_safi = safi_int2iana(safi); | |
835 | zlog_debug( | |
adf086ec DA |
836 | "u%" PRIu64 ":s%" PRIu64 |
837 | " send MP_REACH for afi/safi %s/%s", | |
d62a17ae | 838 | subgrp->update_group->id, |
adf086ec DA |
839 | subgrp->id, |
840 | iana_afi2str(pkt_afi), | |
841 | iana_safi2str(pkt_safi)); | |
d62a17ae | 842 | } |
843 | ||
844 | send_attr_printed = 1; | |
845 | } | |
846 | ||
9bcb3eef DS |
847 | bgp_debug_rdpfxpath2str(afi, safi, prd, dest_p, |
848 | label_pnt, num_labels, | |
be92fc9f | 849 | addpath_capable, addpath_tx_id, |
6c995628 | 850 | &adv->baa->attr->evpn_overlay, |
9bcb3eef | 851 | pfx_buf, sizeof(pfx_buf)); |
d62a17ae | 852 | zlog_debug("u%" PRIu64 ":s%" PRIu64 " send UPDATE %s", |
853 | subgrp->update_group->id, subgrp->id, | |
854 | pfx_buf); | |
855 | } | |
856 | ||
857 | /* Synchnorize attribute. */ | |
858 | if (adj->attr) | |
859 | bgp_attr_unintern(&adj->attr); | |
860 | else | |
861 | subgrp->scount++; | |
862 | ||
863 | adj->attr = bgp_attr_intern(adv->baa->attr); | |
d62a17ae | 864 | adv = bgp_advertise_clean_subgroup(subgrp, adj); |
3f9c7369 DS |
865 | } |
866 | ||
d62a17ae | 867 | if (!stream_empty(s)) { |
868 | if (!stream_empty(snlri)) { | |
869 | bgp_packet_mpattr_end(snlri, mpattrlen_pos); | |
870 | total_attr_len += stream_get_endp(snlri); | |
871 | } | |
872 | ||
873 | /* set the total attribute length correctly */ | |
874 | stream_putw_at(s, attrlen_pos, total_attr_len); | |
875 | ||
876 | if (!stream_empty(snlri)) { | |
877 | packet = stream_dupcat(s, snlri, mpattr_pos); | |
878 | bpacket_attr_vec_arr_update(&vecarr, mpattr_pos); | |
879 | } else | |
880 | packet = stream_dup(s); | |
881 | bgp_packet_set_size(packet); | |
882 | if (bgp_debug_update(NULL, NULL, subgrp->update_group, 0)) | |
ef56aee4 DA |
883 | zlog_debug( |
884 | "u%" PRIu64 ":s%" PRIu64 | |
885 | " send UPDATE len %zd (max message len: %hu) numpfx %d", | |
886 | subgrp->update_group->id, subgrp->id, | |
887 | (stream_get_endp(packet) | |
888 | - stream_get_getp(packet)), | |
889 | peer->max_packet_size, num_pfx); | |
d62a17ae | 890 | pkt = bpacket_queue_add(SUBGRP_PKTQ(subgrp), packet, &vecarr); |
891 | stream_reset(s); | |
892 | stream_reset(snlri); | |
893 | return pkt; | |
3f9c7369 | 894 | } |
d62a17ae | 895 | return NULL; |
3f9c7369 DS |
896 | } |
897 | ||
898 | /* Make BGP withdraw packet. */ | |
899 | /* For ipv4 unicast: | |
900 | 16-octet marker | 2-octet length | 1-octet type | | |
901 | 2-octet withdrawn route length | withdrawn prefixes | 2-octet attrlen (=0) | |
902 | */ | |
903 | /* For other afi/safis: | |
904 | 16-octet marker | 2-octet length | 1-octet type | | |
905 | 2-octet withdrawn route length (=0) | 2-octet attrlen | | |
906 | mp_unreach attr type | attr len | afi | safi | withdrawn prefixes | |
907 | */ | |
d62a17ae | 908 | struct bpacket *subgroup_withdraw_packet(struct update_subgroup *subgrp) |
3f9c7369 | 909 | { |
d62a17ae | 910 | struct bpacket *pkt; |
911 | struct stream *s; | |
912 | struct bgp_adj_out *adj; | |
913 | struct bgp_advertise *adv; | |
914 | struct peer *peer; | |
9bcb3eef | 915 | struct bgp_dest *dest; |
d62a17ae | 916 | bgp_size_t unfeasible_len; |
917 | bgp_size_t total_attr_len; | |
918 | size_t mp_start = 0; | |
919 | size_t attrlen_pos = 0; | |
920 | size_t mplen_pos = 0; | |
d7c0a89a | 921 | uint8_t first_time = 1; |
d62a17ae | 922 | afi_t afi; |
923 | safi_t safi; | |
924 | int space_remaining = 0; | |
925 | int space_needed = 0; | |
926 | int num_pfx = 0; | |
be92fc9f | 927 | bool addpath_capable = false; |
e386c1d5 | 928 | int addpath_overhead = 0; |
d7c0a89a | 929 | uint32_t addpath_tx_id = 0; |
b54892e0 | 930 | const struct prefix_rd *prd = NULL; |
d62a17ae | 931 | |
932 | ||
933 | if (!subgrp) | |
934 | return NULL; | |
3f9c7369 | 935 | |
d62a17ae | 936 | if (bpacket_queue_is_full(SUBGRP_INST(subgrp), SUBGRP_PKTQ(subgrp))) |
937 | return NULL; | |
906ad49b | 938 | |
d62a17ae | 939 | peer = SUBGRP_PEER(subgrp); |
940 | afi = SUBGRP_AFI(subgrp); | |
941 | safi = SUBGRP_SAFI(subgrp); | |
942 | s = subgrp->work; | |
943 | stream_reset(s); | |
be92fc9f DA |
944 | addpath_capable = bgp_addpath_encode_tx(peer, afi, safi); |
945 | addpath_overhead = addpath_capable ? BGP_ADDPATH_ID_LEN : 0; | |
d62a17ae | 946 | |
a274fef8 | 947 | while ((adv = bgp_adv_fifo_first(&subgrp->sync->withdraw)) != NULL) { |
9bcb3eef | 948 | const struct prefix *dest_p; |
b54892e0 | 949 | |
9bcb3eef | 950 | assert(adv->dest); |
d62a17ae | 951 | adj = adv->adj; |
9bcb3eef DS |
952 | dest = adv->dest; |
953 | dest_p = bgp_dest_get_prefix(dest); | |
d62a17ae | 954 | addpath_tx_id = adj->addpath_tx_id; |
955 | ||
956 | space_remaining = | |
2d34fb80 | 957 | STREAM_WRITEABLE(s) - BGP_MAX_PACKET_SIZE_OVERFLOW; |
9bcb3eef DS |
958 | space_needed = |
959 | BGP_NLRI_LENGTH + addpath_overhead + BGP_TOTAL_ATTR_LEN | |
960 | + bgp_packet_mpattr_prefix_size(afi, safi, dest_p); | |
d62a17ae | 961 | |
962 | if (space_remaining < space_needed) | |
963 | break; | |
964 | ||
965 | if (stream_empty(s)) { | |
966 | bgp_packet_set_marker(s, BGP_MSG_UPDATE); | |
967 | stream_putw(s, 0); /* unfeasible routes length */ | |
968 | } else | |
969 | first_time = 0; | |
970 | ||
971 | if (afi == AFI_IP && safi == SAFI_UNICAST | |
972 | && !peer_cap_enhe(peer, afi, safi)) | |
be92fc9f | 973 | stream_put_prefix_addpath(s, dest_p, addpath_capable, |
d62a17ae | 974 | addpath_tx_id); |
975 | else { | |
9bcb3eef DS |
976 | if (dest->pdest) |
977 | prd = (struct prefix_rd *)bgp_dest_get_prefix( | |
978 | dest->pdest); | |
d62a17ae | 979 | |
b54892e0 DS |
980 | /* If first time, format the MP_UNREACH header |
981 | */ | |
d62a17ae | 982 | if (first_time) { |
983 | iana_afi_t pkt_afi; | |
5c525538 | 984 | iana_safi_t pkt_safi; |
d62a17ae | 985 | |
986 | pkt_afi = afi_int2iana(afi); | |
987 | pkt_safi = safi_int2iana(safi); | |
988 | ||
989 | attrlen_pos = stream_get_endp(s); | |
b54892e0 DS |
990 | /* total attr length = 0 for now. |
991 | * reevaluate later */ | |
d62a17ae | 992 | stream_putw(s, 0); |
993 | mp_start = stream_get_endp(s); | |
994 | mplen_pos = bgp_packet_mpunreach_start(s, afi, | |
995 | safi); | |
996 | if (bgp_debug_update(NULL, NULL, | |
997 | subgrp->update_group, 0)) | |
998 | zlog_debug( | |
adf086ec DA |
999 | "u%" PRIu64 ":s%" PRIu64 |
1000 | " send MP_UNREACH for afi/safi %s/%s", | |
d62a17ae | 1001 | subgrp->update_group->id, |
adf086ec DA |
1002 | subgrp->id, |
1003 | iana_afi2str(pkt_afi), | |
1004 | iana_safi2str(pkt_safi)); | |
d62a17ae | 1005 | } |
1006 | ||
9bcb3eef | 1007 | bgp_packet_mpunreach_prefix(s, dest_p, afi, safi, prd, |
be92fc9f | 1008 | NULL, 0, addpath_capable, |
d62a17ae | 1009 | addpath_tx_id, NULL); |
1010 | } | |
1011 | ||
1012 | num_pfx++; | |
1013 | ||
9bcb3eef | 1014 | if (bgp_debug_update(NULL, dest_p, subgrp->update_group, 0)) { |
d62a17ae | 1015 | char pfx_buf[BGP_PRD_PATH_STRLEN]; |
1016 | ||
9bcb3eef | 1017 | bgp_debug_rdpfxpath2str(afi, safi, prd, dest_p, NULL, 0, |
be92fc9f | 1018 | addpath_capable, addpath_tx_id, |
6c995628 | 1019 | NULL, pfx_buf, sizeof(pfx_buf)); |
6cde4b45 | 1020 | zlog_debug("u%" PRIu64 ":s%" PRIu64" send UPDATE %s -- unreachable", |
d62a17ae | 1021 | subgrp->update_group->id, subgrp->id, |
1022 | pfx_buf); | |
1023 | } | |
1024 | ||
1025 | subgrp->scount--; | |
1026 | ||
9bcb3eef | 1027 | bgp_adj_out_remove_subgroup(dest, adj, subgrp); |
3f9c7369 DS |
1028 | } |
1029 | ||
d62a17ae | 1030 | if (!stream_empty(s)) { |
1031 | if (afi == AFI_IP && safi == SAFI_UNICAST | |
1032 | && !peer_cap_enhe(peer, afi, safi)) { | |
1033 | unfeasible_len = stream_get_endp(s) - BGP_HEADER_SIZE | |
1034 | - BGP_UNFEASIBLE_LEN; | |
1035 | stream_putw_at(s, BGP_HEADER_SIZE, unfeasible_len); | |
1036 | stream_putw(s, 0); | |
1037 | } else { | |
1038 | /* Set the mp_unreach attr's length */ | |
1039 | bgp_packet_mpunreach_end(s, mplen_pos); | |
1040 | ||
1041 | /* Set total path attribute length. */ | |
1042 | total_attr_len = stream_get_endp(s) - mp_start; | |
1043 | stream_putw_at(s, attrlen_pos, total_attr_len); | |
1044 | } | |
1045 | bgp_packet_set_size(s); | |
1046 | if (bgp_debug_update(NULL, NULL, subgrp->update_group, 0)) | |
6cde4b45 | 1047 | zlog_debug("u%" PRIu64 ":s%" PRIu64" send UPDATE (withdraw) len %zd numpfx %d", |
d62a17ae | 1048 | subgrp->update_group->id, subgrp->id, |
1049 | (stream_get_endp(s) - stream_get_getp(s)), | |
1050 | num_pfx); | |
1051 | pkt = bpacket_queue_add(SUBGRP_PKTQ(subgrp), stream_dup(s), | |
1052 | NULL); | |
1053 | stream_reset(s); | |
1054 | return pkt; | |
3f9c7369 | 1055 | } |
3f9c7369 | 1056 | |
d62a17ae | 1057 | return NULL; |
3f9c7369 DS |
1058 | } |
1059 | ||
d62a17ae | 1060 | void subgroup_default_update_packet(struct update_subgroup *subgrp, |
1061 | struct attr *attr, struct peer *from) | |
3f9c7369 | 1062 | { |
d62a17ae | 1063 | struct stream *s; |
1064 | struct peer *peer; | |
1065 | struct prefix p; | |
1066 | unsigned long pos; | |
1067 | bgp_size_t total_attr_len; | |
1068 | afi_t afi; | |
1069 | safi_t safi; | |
1070 | struct bpacket_attr_vec_arr vecarr; | |
be92fc9f | 1071 | bool addpath_capable = false; |
d62a17ae | 1072 | |
1073 | if (DISABLE_BGP_ANNOUNCE) | |
1074 | return; | |
1075 | ||
1076 | if (!subgrp) | |
1077 | return; | |
1078 | ||
1079 | peer = SUBGRP_PEER(subgrp); | |
1080 | afi = SUBGRP_AFI(subgrp); | |
1081 | safi = SUBGRP_SAFI(subgrp); | |
1082 | bpacket_attr_vec_arr_reset(&vecarr); | |
be92fc9f | 1083 | addpath_capable = bgp_addpath_encode_tx(peer, afi, safi); |
d62a17ae | 1084 | |
cbb65f5e RW |
1085 | memset(&p, 0, sizeof(p)); |
1086 | p.family = afi2family(afi); | |
1087 | p.prefixlen = 0; | |
d62a17ae | 1088 | |
1089 | /* Logging the attribute. */ | |
1090 | if (bgp_debug_update(NULL, &p, subgrp->update_group, 0)) { | |
1091 | char attrstr[BUFSIZ]; | |
d62a17ae | 1092 | /* ' with addpath ID ' 17 |
1093 | * max strlen of uint32 + 10 | |
1094 | * +/- (just in case) + 1 | |
1095 | * null terminator + 1 | |
1096 | * ============================ 29 */ | |
1097 | char tx_id_buf[30]; | |
1098 | ||
1099 | attrstr[0] = '\0'; | |
1100 | ||
5022c833 | 1101 | bgp_dump_attr(attr, attrstr, sizeof(attrstr)); |
d62a17ae | 1102 | |
be92fc9f | 1103 | if (addpath_capable) |
d62a17ae | 1104 | snprintf(tx_id_buf, sizeof(tx_id_buf), |
1105 | " with addpath ID %u", | |
1106 | BGP_ADDPATH_TX_ID_FOR_DEFAULT_ORIGINATE); | |
cbb263cf DS |
1107 | else |
1108 | tx_id_buf[0] = '\0'; | |
d62a17ae | 1109 | |
2dbe669b DA |
1110 | zlog_debug("u%" PRIu64 ":s%" PRIu64 " send UPDATE %pFX%s %s", |
1111 | (SUBGRP_UPDGRP(subgrp))->id, subgrp->id, &p, | |
1112 | tx_id_buf, attrstr); | |
d62a17ae | 1113 | } |
3f9c7369 | 1114 | |
ef56aee4 | 1115 | s = stream_new(peer->max_packet_size); |
3f9c7369 | 1116 | |
d62a17ae | 1117 | /* Make BGP update packet. */ |
1118 | bgp_packet_set_marker(s, BGP_MSG_UPDATE); | |
3f9c7369 | 1119 | |
d62a17ae | 1120 | /* Unfeasible Routes Length. */ |
1121 | stream_putw(s, 0); | |
3f9c7369 | 1122 | |
d62a17ae | 1123 | /* Make place for total attribute length. */ |
1124 | pos = stream_get_endp(s); | |
1125 | stream_putw(s, 0); | |
1126 | total_attr_len = bgp_packet_attribute( | |
1127 | NULL, peer, s, attr, &vecarr, &p, afi, safi, from, NULL, NULL, | |
be92fc9f | 1128 | 0, addpath_capable, BGP_ADDPATH_TX_ID_FOR_DEFAULT_ORIGINATE); |
3f9c7369 | 1129 | |
d62a17ae | 1130 | /* Set Total Path Attribute Length. */ |
1131 | stream_putw_at(s, pos, total_attr_len); | |
3f9c7369 | 1132 | |
d62a17ae | 1133 | /* NLRI set. */ |
1134 | if (p.family == AF_INET && safi == SAFI_UNICAST | |
1135 | && !peer_cap_enhe(peer, afi, safi)) | |
1136 | stream_put_prefix_addpath( | |
be92fc9f | 1137 | s, &p, addpath_capable, |
d62a17ae | 1138 | BGP_ADDPATH_TX_ID_FOR_DEFAULT_ORIGINATE); |
3f9c7369 | 1139 | |
d62a17ae | 1140 | /* Set size. */ |
1141 | bgp_packet_set_size(s); | |
3f9c7369 | 1142 | |
d62a17ae | 1143 | (void)bpacket_queue_add(SUBGRP_PKTQ(subgrp), s, &vecarr); |
2fc102e1 | 1144 | subgroup_trigger_write(subgrp); |
46c4f05b IS |
1145 | |
1146 | if (!CHECK_FLAG(subgrp->sflags, | |
1147 | SUBGRP_STATUS_PEER_DEFAULT_ORIGINATED)) { | |
1148 | subgrp->scount++; | |
1149 | SET_FLAG(subgrp->sflags, SUBGRP_STATUS_PEER_DEFAULT_ORIGINATED); | |
1150 | } | |
3f9c7369 DS |
1151 | } |
1152 | ||
d62a17ae | 1153 | void subgroup_default_withdraw_packet(struct update_subgroup *subgrp) |
3f9c7369 | 1154 | { |
d62a17ae | 1155 | struct peer *peer; |
1156 | struct stream *s; | |
1157 | struct prefix p; | |
1158 | unsigned long attrlen_pos = 0; | |
1159 | unsigned long cp; | |
1160 | bgp_size_t unfeasible_len; | |
1161 | bgp_size_t total_attr_len = 0; | |
1162 | size_t mp_start = 0; | |
1163 | size_t mplen_pos = 0; | |
1164 | afi_t afi; | |
1165 | safi_t safi; | |
be92fc9f | 1166 | bool addpath_capable = false; |
d62a17ae | 1167 | |
1168 | if (DISABLE_BGP_ANNOUNCE) | |
1169 | return; | |
1170 | ||
1171 | peer = SUBGRP_PEER(subgrp); | |
1172 | afi = SUBGRP_AFI(subgrp); | |
1173 | safi = SUBGRP_SAFI(subgrp); | |
be92fc9f | 1174 | addpath_capable = bgp_addpath_encode_tx(peer, afi, safi); |
d62a17ae | 1175 | |
cbb65f5e RW |
1176 | memset(&p, 0, sizeof(p)); |
1177 | p.family = afi2family(afi); | |
1178 | p.prefixlen = 0; | |
d62a17ae | 1179 | |
1180 | if (bgp_debug_update(NULL, &p, subgrp->update_group, 0)) { | |
d62a17ae | 1181 | /* ' with addpath ID ' 17 |
1182 | * max strlen of uint32 + 10 | |
1183 | * +/- (just in case) + 1 | |
1184 | * null terminator + 1 | |
1185 | * ============================ 29 */ | |
1186 | char tx_id_buf[30]; | |
1187 | ||
be92fc9f | 1188 | if (addpath_capable) |
d62a17ae | 1189 | snprintf(tx_id_buf, sizeof(tx_id_buf), |
1190 | " with addpath ID %u", | |
1191 | BGP_ADDPATH_TX_ID_FOR_DEFAULT_ORIGINATE); | |
1192 | ||
2dbe669b DA |
1193 | zlog_debug("u%" PRIu64 ":s%" PRIu64 |
1194 | " send UPDATE %pFX%s -- unreachable", | |
1195 | (SUBGRP_UPDGRP(subgrp))->id, subgrp->id, &p, | |
1196 | tx_id_buf); | |
d62a17ae | 1197 | } |
3f9c7369 | 1198 | |
ef56aee4 | 1199 | s = stream_new(peer->max_packet_size); |
3f9c7369 | 1200 | |
d62a17ae | 1201 | /* Make BGP update packet. */ |
1202 | bgp_packet_set_marker(s, BGP_MSG_UPDATE); | |
3f9c7369 | 1203 | |
d62a17ae | 1204 | /* Unfeasible Routes Length. */; |
1205 | cp = stream_get_endp(s); | |
1206 | stream_putw(s, 0); | |
3f9c7369 | 1207 | |
d62a17ae | 1208 | /* Withdrawn Routes. */ |
1209 | if (p.family == AF_INET && safi == SAFI_UNICAST | |
1210 | && !peer_cap_enhe(peer, afi, safi)) { | |
1211 | stream_put_prefix_addpath( | |
be92fc9f | 1212 | s, &p, addpath_capable, |
d62a17ae | 1213 | BGP_ADDPATH_TX_ID_FOR_DEFAULT_ORIGINATE); |
3f9c7369 | 1214 | |
d62a17ae | 1215 | unfeasible_len = stream_get_endp(s) - cp - 2; |
3f9c7369 | 1216 | |
d62a17ae | 1217 | /* Set unfeasible len. */ |
1218 | stream_putw_at(s, cp, unfeasible_len); | |
3f9c7369 | 1219 | |
d62a17ae | 1220 | /* Set total path attribute length. */ |
1221 | stream_putw(s, 0); | |
1222 | } else { | |
1223 | attrlen_pos = stream_get_endp(s); | |
1224 | stream_putw(s, 0); | |
1225 | mp_start = stream_get_endp(s); | |
1226 | mplen_pos = bgp_packet_mpunreach_start(s, afi, safi); | |
1227 | bgp_packet_mpunreach_prefix( | |
be92fc9f | 1228 | s, &p, afi, safi, NULL, NULL, 0, addpath_capable, |
d62a17ae | 1229 | BGP_ADDPATH_TX_ID_FOR_DEFAULT_ORIGINATE, NULL); |
3f9c7369 | 1230 | |
d62a17ae | 1231 | /* Set the mp_unreach attr's length */ |
1232 | bgp_packet_mpunreach_end(s, mplen_pos); | |
3f9c7369 | 1233 | |
d62a17ae | 1234 | /* Set total path attribute length. */ |
1235 | total_attr_len = stream_get_endp(s) - mp_start; | |
1236 | stream_putw_at(s, attrlen_pos, total_attr_len); | |
1237 | } | |
1238 | ||
1239 | bgp_packet_set_size(s); | |
1240 | ||
1241 | (void)bpacket_queue_add(SUBGRP_PKTQ(subgrp), s, NULL); | |
2fc102e1 | 1242 | subgroup_trigger_write(subgrp); |
46c4f05b IS |
1243 | |
1244 | if (CHECK_FLAG(subgrp->sflags, SUBGRP_STATUS_PEER_DEFAULT_ORIGINATED)) { | |
1245 | subgrp->scount--; | |
1246 | UNSET_FLAG(subgrp->sflags, | |
1247 | SUBGRP_STATUS_PEER_DEFAULT_ORIGINATED); | |
1248 | } | |
3f9c7369 DS |
1249 | } |
1250 | ||
1251 | static void | |
d62a17ae | 1252 | bpacket_vec_arr_inherit_attr_flags(struct bpacket_attr_vec_arr *vecarr, |
e8e36ff3 | 1253 | enum bpacket_attr_vec_type type, |
d62a17ae | 1254 | struct attr *attr) |
3f9c7369 | 1255 | { |
d62a17ae | 1256 | if (CHECK_FLAG(attr->rmap_change_flags, |
1257 | BATTR_RMAP_NEXTHOP_PEER_ADDRESS)) | |
1258 | SET_FLAG(vecarr->entries[BGP_ATTR_VEC_NH].flags, | |
1259 | BPKT_ATTRVEC_FLAGS_RMAP_NH_PEER_ADDRESS); | |
1260 | ||
1261 | if (CHECK_FLAG(attr->rmap_change_flags, BATTR_REFLECTED)) | |
1262 | SET_FLAG(vecarr->entries[BGP_ATTR_VEC_NH].flags, | |
1263 | BPKT_ATTRVEC_FLAGS_REFLECTED); | |
1264 | ||
1265 | if (CHECK_FLAG(attr->rmap_change_flags, BATTR_RMAP_NEXTHOP_UNCHANGED)) | |
1266 | SET_FLAG(vecarr->entries[BGP_ATTR_VEC_NH].flags, | |
1267 | BPKT_ATTRVEC_FLAGS_RMAP_NH_UNCHANGED); | |
1268 | ||
1269 | if (CHECK_FLAG(attr->rmap_change_flags, BATTR_RMAP_IPV4_NHOP_CHANGED)) | |
1270 | SET_FLAG(vecarr->entries[BGP_ATTR_VEC_NH].flags, | |
1271 | BPKT_ATTRVEC_FLAGS_RMAP_IPV4_NH_CHANGED); | |
1272 | ||
1273 | if (CHECK_FLAG(attr->rmap_change_flags, | |
1274 | BATTR_RMAP_IPV6_GLOBAL_NHOP_CHANGED)) | |
1275 | SET_FLAG(vecarr->entries[BGP_ATTR_VEC_NH].flags, | |
1276 | BPKT_ATTRVEC_FLAGS_RMAP_IPV6_GNH_CHANGED); | |
1277 | ||
1278 | if (CHECK_FLAG(attr->rmap_change_flags, | |
1279 | BATTR_RMAP_IPV6_LL_NHOP_CHANGED)) | |
1280 | SET_FLAG(vecarr->entries[BGP_ATTR_VEC_NH].flags, | |
1281 | BPKT_ATTRVEC_FLAGS_RMAP_IPV6_LNH_CHANGED); | |
3f9c7369 DS |
1282 | } |
1283 | ||
1284 | /* Reset the Attributes vector array. The vector array is used to override | |
1285 | * certain output parameters in the packet for a particular peer | |
1286 | */ | |
d62a17ae | 1287 | void bpacket_attr_vec_arr_reset(struct bpacket_attr_vec_arr *vecarr) |
3f9c7369 | 1288 | { |
d62a17ae | 1289 | int i; |
3f9c7369 | 1290 | |
d62a17ae | 1291 | if (!vecarr) |
1292 | return; | |
3f9c7369 | 1293 | |
d62a17ae | 1294 | i = 0; |
1295 | while (i < BGP_ATTR_VEC_MAX) { | |
1296 | vecarr->entries[i].flags = 0; | |
1297 | vecarr->entries[i].offset = 0; | |
1298 | i++; | |
1299 | } | |
3f9c7369 DS |
1300 | } |
1301 | ||
1302 | /* Setup a particular node entry in the vecarr */ | |
d62a17ae | 1303 | void bpacket_attr_vec_arr_set_vec(struct bpacket_attr_vec_arr *vecarr, |
e8e36ff3 DA |
1304 | enum bpacket_attr_vec_type type, |
1305 | struct stream *s, struct attr *attr) | |
3f9c7369 | 1306 | { |
d62a17ae | 1307 | if (!vecarr) |
1308 | return; | |
1309 | assert(type < BGP_ATTR_VEC_MAX); | |
1310 | ||
1311 | SET_FLAG(vecarr->entries[type].flags, BPKT_ATTRVEC_FLAGS_UPDATED); | |
1312 | vecarr->entries[type].offset = stream_get_endp(s); | |
1313 | if (attr) | |
1314 | bpacket_vec_arr_inherit_attr_flags(vecarr, type, attr); | |
3f9c7369 | 1315 | } |