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