]>
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; |
a2b6e694 | 400 | afi_t nhafi; |
d62a17ae | 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 | |
d62a17ae | 470 | && (bgp_multiaccess_check_v4(v4nh, peer) == 0) |
471 | && !CHECK_FLAG( | |
472 | vec->flags, | |
473 | BPKT_ATTRVEC_FLAGS_RMAP_NH_UNCHANGED) | |
474 | && !peer_af_flag_check( | |
ace295a9 | 475 | peer, paf->afi, paf->safi, |
d62a17ae | 476 | PEER_FLAG_NEXTHOP_UNCHANGED)) { |
477 | /* NOTE: not handling case where NH has new AFI | |
478 | */ | |
479 | mod_v4nh = &peer->nexthop.v4; | |
480 | nh_modified = 1; | |
481 | } | |
482 | ||
483 | if (nh_modified) /* allow for VPN RD */ | |
484 | stream_put_in_addr_at(s, offset_nh, mod_v4nh); | |
485 | ||
486 | if (bgp_debug_update(peer, NULL, NULL, 0)) | |
487 | zlog_debug("u%" PRIu64 ":s%" PRIu64 | |
488 | " %s send UPDATE w/ nexthop %s%s", | |
489 | PAF_SUBGRP(paf)->update_group->id, | |
490 | PAF_SUBGRP(paf)->id, peer->host, | |
491 | inet_ntoa(*mod_v4nh), | |
492 | (nhlen == 12 ? " and RD" : "")); | |
493 | } else if (nhafi == AFI_IP6) { | |
494 | struct in6_addr v6nhglobal, *mod_v6nhg; | |
495 | struct in6_addr v6nhlocal, *mod_v6nhl; | |
496 | int gnh_modified, lnh_modified; | |
497 | size_t offset_nhglobal = vec->offset + 1; | |
498 | size_t offset_nhlocal = vec->offset + 1; | |
499 | ||
500 | gnh_modified = lnh_modified = 0; | |
501 | mod_v6nhg = &v6nhglobal; | |
502 | mod_v6nhl = &v6nhlocal; | |
503 | ||
504 | route_map_sets_nh = | |
505 | (CHECK_FLAG( | |
506 | vec->flags, | |
507 | BPKT_ATTRVEC_FLAGS_RMAP_IPV6_GNH_CHANGED) | |
508 | || CHECK_FLAG( | |
509 | vec->flags, | |
510 | BPKT_ATTRVEC_FLAGS_RMAP_NH_PEER_ADDRESS)); | |
511 | ||
512 | /* | |
513 | * The logic here is rather similar to that for IPv4, | |
514 | * the | |
515 | * additional work being to handle 1 or 2 nexthops. | |
516 | * Also, 3rd | |
517 | * party nexthop is not propagated for EBGP right now. | |
518 | */ | |
519 | switch (nhlen) { | |
520 | case BGP_ATTR_NHLEN_IPV6_GLOBAL: | |
521 | break; | |
522 | case BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL: | |
523 | offset_nhlocal += IPV6_MAX_BYTELEN; | |
524 | break; | |
525 | case BGP_ATTR_NHLEN_VPNV6_GLOBAL: | |
526 | offset_nhglobal += 8; | |
527 | break; | |
528 | case BGP_ATTR_NHLEN_VPNV6_GLOBAL_AND_LL: | |
529 | offset_nhglobal += 8; | |
530 | offset_nhlocal += 8 * 2 + IPV6_MAX_BYTELEN; | |
531 | break; | |
532 | default: | |
533 | /* TODO: handle IPv4 nexthops */ | |
534 | zlog_warn( | |
535 | "%s: %s: invalid MP nexthop length (AFI IP6): %u", | |
536 | __func__, peer->host, nhlen); | |
537 | stream_free(s); | |
538 | return NULL; | |
539 | } | |
540 | ||
541 | stream_get_from(&v6nhglobal, s, offset_nhglobal, | |
542 | IPV6_MAX_BYTELEN); | |
543 | if (route_map_sets_nh) { | |
544 | if (CHECK_FLAG( | |
545 | vec->flags, | |
546 | BPKT_ATTRVEC_FLAGS_RMAP_NH_PEER_ADDRESS)) { | |
547 | mod_v6nhg = &peer->nexthop.v6_global; | |
548 | gnh_modified = 1; | |
549 | } | |
550 | } else if (IN6_IS_ADDR_UNSPECIFIED(&v6nhglobal)) { | |
551 | mod_v6nhg = &peer->nexthop.v6_global; | |
552 | gnh_modified = 1; | |
553 | } else if ( | |
554 | peer->sort == BGP_PEER_EBGP | |
555 | && !CHECK_FLAG( | |
556 | vec->flags, | |
557 | BPKT_ATTRVEC_FLAGS_RMAP_NH_UNCHANGED) | |
558 | && !peer_af_flag_check( | |
559 | peer, nhafi, paf->safi, | |
560 | PEER_FLAG_NEXTHOP_UNCHANGED)) { | |
561 | /* NOTE: not handling case where NH has new AFI | |
562 | */ | |
563 | mod_v6nhg = &peer->nexthop.v6_global; | |
564 | gnh_modified = 1; | |
565 | } | |
566 | ||
567 | ||
568 | if (nhlen == BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL | |
569 | || nhlen == BGP_ATTR_NHLEN_VPNV6_GLOBAL_AND_LL) { | |
570 | stream_get_from(&v6nhlocal, s, offset_nhlocal, | |
571 | IPV6_MAX_BYTELEN); | |
572 | if (IN6_IS_ADDR_UNSPECIFIED(&v6nhlocal)) { | |
573 | mod_v6nhl = &peer->nexthop.v6_local; | |
574 | lnh_modified = 1; | |
575 | } | |
576 | } | |
577 | ||
578 | if (gnh_modified) | |
579 | stream_put_in6_addr_at(s, offset_nhglobal, | |
580 | mod_v6nhg); | |
581 | if (lnh_modified) | |
582 | stream_put_in6_addr_at(s, offset_nhlocal, | |
583 | mod_v6nhl); | |
584 | ||
585 | if (bgp_debug_update(peer, NULL, NULL, 0)) { | |
586 | if (nhlen == 32 || nhlen == 48) | |
587 | zlog_debug( | |
588 | "u%" PRIu64 ":s%" PRIu64 | |
589 | " %s send UPDATE w/ mp_nexthops %s, %s%s", | |
590 | PAF_SUBGRP(paf) | |
591 | ->update_group->id, | |
592 | PAF_SUBGRP(paf)->id, peer->host, | |
593 | inet_ntop(AF_INET6, mod_v6nhg, | |
594 | buf, BUFSIZ), | |
595 | inet_ntop(AF_INET6, mod_v6nhl, | |
596 | buf2, BUFSIZ), | |
597 | (nhlen == 48 ? " and RD" : "")); | |
598 | else | |
599 | zlog_debug( | |
600 | "u%" PRIu64 ":s%" PRIu64 | |
601 | " %s send UPDATE w/ mp_nexthop %s%s", | |
602 | PAF_SUBGRP(paf) | |
603 | ->update_group->id, | |
604 | PAF_SUBGRP(paf)->id, peer->host, | |
605 | inet_ntop(AF_INET6, mod_v6nhg, | |
606 | buf, BUFSIZ), | |
607 | (nhlen == 24 ? " and RD" : "")); | |
608 | } | |
609 | } else if (paf->afi == AFI_L2VPN) { | |
610 | struct in_addr v4nh, *mod_v4nh; | |
611 | int nh_modified = 0; | |
612 | ||
613 | stream_get_from(&v4nh, s, vec->offset + 1, 4); | |
614 | mod_v4nh = &v4nh; | |
615 | ||
616 | /* No route-map changes allowed for EVPN nexthops. */ | |
617 | if (!v4nh.s_addr) { | |
618 | mod_v4nh = &peer->nexthop.v4; | |
619 | nh_modified = 1; | |
620 | } | |
621 | ||
622 | if (nh_modified) | |
623 | stream_put_in_addr_at(s, vec->offset + 1, | |
624 | mod_v4nh); | |
625 | ||
626 | if (bgp_debug_update(peer, NULL, NULL, 0)) | |
627 | zlog_debug("u%" PRIu64 ":s%" PRIu64 | |
628 | " %s send UPDATE w/ nexthop %s", | |
629 | PAF_SUBGRP(paf)->update_group->id, | |
630 | PAF_SUBGRP(paf)->id, peer->host, | |
631 | inet_ntoa(*mod_v4nh)); | |
632 | } | |
3f9c7369 | 633 | } |
3f9c7369 | 634 | |
d62a17ae | 635 | return s; |
3f9c7369 DS |
636 | } |
637 | ||
638 | /* | |
639 | * Update the vecarr offsets to go beyond 'pos' bytes, i.e. add 'pos' | |
640 | * to each offset. | |
641 | */ | |
d62a17ae | 642 | static void bpacket_attr_vec_arr_update(struct bpacket_attr_vec_arr *vecarr, |
643 | size_t pos) | |
3f9c7369 | 644 | { |
d62a17ae | 645 | int i; |
3f9c7369 | 646 | |
d62a17ae | 647 | if (!vecarr) |
648 | return; | |
3f9c7369 | 649 | |
d62a17ae | 650 | for (i = 0; i < BGP_ATTR_VEC_MAX; i++) |
651 | vecarr->entries[i].offset += pos; | |
3f9c7369 DS |
652 | } |
653 | ||
654 | /* | |
655 | * Return if there are packets to build for this subgroup. | |
656 | */ | |
d62a17ae | 657 | int subgroup_packets_to_build(struct update_subgroup *subgrp) |
3f9c7369 | 658 | { |
d62a17ae | 659 | struct bgp_advertise *adv; |
3f9c7369 | 660 | |
d62a17ae | 661 | if (!subgrp) |
662 | return 0; | |
3f9c7369 | 663 | |
d62a17ae | 664 | adv = BGP_ADV_FIFO_HEAD(&subgrp->sync->withdraw); |
665 | if (adv) | |
666 | return 1; | |
3f9c7369 | 667 | |
d62a17ae | 668 | adv = BGP_ADV_FIFO_HEAD(&subgrp->sync->update); |
669 | if (adv) | |
670 | return 1; | |
3f9c7369 | 671 | |
d62a17ae | 672 | return 0; |
3f9c7369 DS |
673 | } |
674 | ||
675 | /* Make BGP update packet. */ | |
d62a17ae | 676 | struct bpacket *subgroup_update_packet(struct update_subgroup *subgrp) |
3f9c7369 | 677 | { |
d62a17ae | 678 | struct bpacket_attr_vec_arr vecarr; |
679 | struct bpacket *pkt; | |
680 | struct peer *peer; | |
681 | struct stream *s; | |
682 | struct stream *snlri; | |
683 | struct stream *packet; | |
684 | struct bgp_adj_out *adj; | |
685 | struct bgp_advertise *adv; | |
686 | struct bgp_node *rn = NULL; | |
687 | struct bgp_info *binfo = NULL; | |
688 | bgp_size_t total_attr_len = 0; | |
689 | unsigned long attrlen_pos = 0; | |
690 | size_t mpattrlen_pos = 0; | |
691 | size_t mpattr_pos = 0; | |
692 | afi_t afi; | |
693 | safi_t safi; | |
694 | int space_remaining = 0; | |
695 | int space_needed = 0; | |
696 | char send_attr_str[BUFSIZ]; | |
697 | int send_attr_printed = 0; | |
698 | int num_pfx = 0; | |
699 | int addpath_encode = 0; | |
e386c1d5 | 700 | int addpath_overhead = 0; |
d7c0a89a | 701 | uint32_t addpath_tx_id = 0; |
d62a17ae | 702 | struct prefix_rd *prd = NULL; |
b57ba6d2 | 703 | mpls_label_t label = MPLS_INVALID_LABEL, *label_pnt = NULL; |
d7c0a89a | 704 | uint32_t num_labels = 0; |
d62a17ae | 705 | |
706 | if (!subgrp) | |
707 | return NULL; | |
3f9c7369 | 708 | |
d62a17ae | 709 | if (bpacket_queue_is_full(SUBGRP_INST(subgrp), SUBGRP_PKTQ(subgrp))) |
710 | return NULL; | |
3f9c7369 | 711 | |
d62a17ae | 712 | peer = SUBGRP_PEER(subgrp); |
713 | afi = SUBGRP_AFI(subgrp); | |
714 | safi = SUBGRP_SAFI(subgrp); | |
715 | s = subgrp->work; | |
716 | stream_reset(s); | |
717 | snlri = subgrp->scratch; | |
718 | stream_reset(snlri); | |
719 | ||
720 | bpacket_attr_vec_arr_reset(&vecarr); | |
721 | ||
722 | addpath_encode = bgp_addpath_encode_tx(peer, afi, safi); | |
e386c1d5 | 723 | addpath_overhead = addpath_encode ? BGP_ADDPATH_ID_LEN : 0; |
d62a17ae | 724 | |
725 | adv = BGP_ADV_FIFO_HEAD(&subgrp->sync->update); | |
726 | while (adv) { | |
727 | assert(adv->rn); | |
728 | rn = adv->rn; | |
729 | adj = adv->adj; | |
730 | addpath_tx_id = adj->addpath_tx_id; | |
731 | binfo = adv->binfo; | |
732 | ||
733 | space_remaining = STREAM_CONCAT_REMAIN(s, snlri, STREAM_SIZE(s)) | |
734 | - BGP_MAX_PACKET_SIZE_OVERFLOW; | |
996c9314 LB |
735 | space_needed = |
736 | BGP_NLRI_LENGTH + addpath_overhead | |
737 | + bgp_packet_mpattr_prefix_size(afi, safi, &rn->p); | |
d62a17ae | 738 | |
739 | /* When remaining space can't include NLRI and it's length. */ | |
740 | if (space_remaining < space_needed) | |
741 | break; | |
742 | ||
743 | /* If packet is empty, set attribute. */ | |
744 | if (stream_empty(s)) { | |
745 | struct peer *from = NULL; | |
746 | ||
747 | if (binfo) | |
748 | from = binfo->peer; | |
749 | ||
750 | /* 1: Write the BGP message header - 16 bytes marker, 2 | |
751 | * bytes length, | |
752 | * one byte message type. | |
753 | */ | |
754 | bgp_packet_set_marker(s, BGP_MSG_UPDATE); | |
755 | ||
756 | /* 2: withdrawn routes length */ | |
757 | stream_putw(s, 0); | |
758 | ||
759 | /* 3: total attributes length - attrlen_pos stores the | |
760 | * position */ | |
761 | attrlen_pos = stream_get_endp(s); | |
762 | stream_putw(s, 0); | |
763 | ||
764 | /* 4: if there is MP_REACH_NLRI attribute, that should | |
765 | * be the first | |
766 | * attribute, according to | |
767 | * draft-ietf-idr-error-handling. Save the | |
768 | * position. | |
769 | */ | |
770 | mpattr_pos = stream_get_endp(s); | |
771 | ||
772 | /* 5: Encode all the attributes, except MP_REACH_NLRI | |
773 | * attr. */ | |
774 | total_attr_len = bgp_packet_attribute( | |
775 | NULL, peer, s, adv->baa->attr, &vecarr, NULL, | |
b57ba6d2 | 776 | afi, safi, from, NULL, NULL, 0, 0, 0); |
d62a17ae | 777 | |
778 | space_remaining = | |
779 | STREAM_CONCAT_REMAIN(s, snlri, STREAM_SIZE(s)) | |
780 | - BGP_MAX_PACKET_SIZE_OVERFLOW; | |
996c9314 LB |
781 | space_needed = BGP_NLRI_LENGTH + addpath_overhead |
782 | + bgp_packet_mpattr_prefix_size( | |
783 | afi, safi, &rn->p); | |
d62a17ae | 784 | |
785 | /* If the attributes alone do not leave any room for | |
786 | * NLRI then | |
787 | * return */ | |
788 | if (space_remaining < space_needed) { | |
789 | zlog_err( | |
790 | "u%" PRIu64 ":s%" PRIu64 | |
791 | " attributes too long, cannot send UPDATE", | |
792 | subgrp->update_group->id, subgrp->id); | |
793 | ||
794 | /* Flush the FIFO update queue */ | |
795 | while (adv) | |
796 | adv = bgp_advertise_clean_subgroup( | |
797 | subgrp, adj); | |
798 | return NULL; | |
799 | } | |
800 | ||
801 | if (BGP_DEBUG(update, UPDATE_OUT) | |
802 | || BGP_DEBUG(update, UPDATE_PREFIX)) { | |
803 | memset(send_attr_str, 0, BUFSIZ); | |
804 | send_attr_printed = 0; | |
805 | bgp_dump_attr(adv->baa->attr, send_attr_str, | |
806 | BUFSIZ); | |
807 | } | |
808 | } | |
809 | ||
810 | if ((afi == AFI_IP && safi == SAFI_UNICAST) | |
811 | && !peer_cap_enhe(peer, afi, safi)) | |
812 | stream_put_prefix_addpath(s, &rn->p, addpath_encode, | |
813 | addpath_tx_id); | |
814 | else { | |
815 | /* Encode the prefix in MP_REACH_NLRI attribute */ | |
816 | if (rn->prn) | |
817 | prd = (struct prefix_rd *)&rn->prn->p; | |
818 | ||
b57ba6d2 | 819 | if (safi == SAFI_LABELED_UNICAST) { |
d62a17ae | 820 | label = bgp_adv_label(rn, binfo, peer, afi, |
821 | safi); | |
b57ba6d2 MK |
822 | label_pnt = &label; |
823 | num_labels = 1; | |
824 | } else if (binfo && binfo->extra) { | |
825 | label_pnt = &binfo->extra->label[0]; | |
826 | num_labels = binfo->extra->num_labels; | |
827 | } | |
d62a17ae | 828 | |
829 | if (stream_empty(snlri)) | |
830 | mpattrlen_pos = bgp_packet_mpattr_start( | |
831 | snlri, peer, afi, safi, &vecarr, | |
832 | adv->baa->attr); | |
833 | ||
834 | bgp_packet_mpattr_prefix(snlri, afi, safi, &rn->p, prd, | |
b57ba6d2 MK |
835 | label_pnt, num_labels, |
836 | addpath_encode, addpath_tx_id, | |
837 | adv->baa->attr); | |
d62a17ae | 838 | } |
839 | ||
840 | num_pfx++; | |
841 | ||
842 | if (bgp_debug_update(NULL, &rn->p, subgrp->update_group, 0)) { | |
843 | char pfx_buf[BGP_PRD_PATH_STRLEN]; | |
844 | ||
845 | if (!send_attr_printed) { | |
846 | zlog_debug("u%" PRIu64 ":s%" PRIu64 | |
847 | " send UPDATE w/ attr: %s", | |
848 | subgrp->update_group->id, subgrp->id, | |
849 | send_attr_str); | |
850 | if (!stream_empty(snlri)) { | |
851 | iana_afi_t pkt_afi; | |
5c525538 | 852 | iana_safi_t pkt_safi; |
d62a17ae | 853 | |
854 | pkt_afi = afi_int2iana(afi); | |
855 | pkt_safi = safi_int2iana(safi); | |
856 | zlog_debug( | |
857 | "u%" PRIu64 ":s%" PRIu64 | |
858 | " send MP_REACH for afi/safi %d/%d", | |
859 | subgrp->update_group->id, | |
860 | subgrp->id, pkt_afi, pkt_safi); | |
861 | } | |
862 | ||
863 | send_attr_printed = 1; | |
864 | } | |
865 | ||
b57ba6d2 MK |
866 | bgp_debug_rdpfxpath2str(afi, safi, prd, &rn->p, |
867 | label_pnt, num_labels, | |
d62a17ae | 868 | addpath_encode, addpath_tx_id, |
869 | pfx_buf, sizeof(pfx_buf)); | |
870 | zlog_debug("u%" PRIu64 ":s%" PRIu64 " send UPDATE %s", | |
871 | subgrp->update_group->id, subgrp->id, | |
872 | pfx_buf); | |
873 | } | |
874 | ||
875 | /* Synchnorize attribute. */ | |
876 | if (adj->attr) | |
877 | bgp_attr_unintern(&adj->attr); | |
878 | else | |
879 | subgrp->scount++; | |
880 | ||
881 | adj->attr = bgp_attr_intern(adv->baa->attr); | |
882 | ||
883 | adv = bgp_advertise_clean_subgroup(subgrp, adj); | |
3f9c7369 DS |
884 | } |
885 | ||
d62a17ae | 886 | if (!stream_empty(s)) { |
887 | if (!stream_empty(snlri)) { | |
888 | bgp_packet_mpattr_end(snlri, mpattrlen_pos); | |
889 | total_attr_len += stream_get_endp(snlri); | |
890 | } | |
891 | ||
892 | /* set the total attribute length correctly */ | |
893 | stream_putw_at(s, attrlen_pos, total_attr_len); | |
894 | ||
895 | if (!stream_empty(snlri)) { | |
896 | packet = stream_dupcat(s, snlri, mpattr_pos); | |
897 | bpacket_attr_vec_arr_update(&vecarr, mpattr_pos); | |
898 | } else | |
899 | packet = stream_dup(s); | |
900 | bgp_packet_set_size(packet); | |
901 | if (bgp_debug_update(NULL, NULL, subgrp->update_group, 0)) | |
902 | zlog_debug("u%" PRIu64 ":s%" PRIu64 | |
903 | " send UPDATE len %zd numpfx %d", | |
904 | subgrp->update_group->id, subgrp->id, | |
905 | (stream_get_endp(packet) | |
906 | - stream_get_getp(packet)), | |
907 | num_pfx); | |
908 | pkt = bpacket_queue_add(SUBGRP_PKTQ(subgrp), packet, &vecarr); | |
909 | stream_reset(s); | |
910 | stream_reset(snlri); | |
911 | return pkt; | |
3f9c7369 | 912 | } |
d62a17ae | 913 | return NULL; |
3f9c7369 DS |
914 | } |
915 | ||
916 | /* Make BGP withdraw packet. */ | |
917 | /* For ipv4 unicast: | |
918 | 16-octet marker | 2-octet length | 1-octet type | | |
919 | 2-octet withdrawn route length | withdrawn prefixes | 2-octet attrlen (=0) | |
920 | */ | |
921 | /* For other afi/safis: | |
922 | 16-octet marker | 2-octet length | 1-octet type | | |
923 | 2-octet withdrawn route length (=0) | 2-octet attrlen | | |
924 | mp_unreach attr type | attr len | afi | safi | withdrawn prefixes | |
925 | */ | |
d62a17ae | 926 | struct bpacket *subgroup_withdraw_packet(struct update_subgroup *subgrp) |
3f9c7369 | 927 | { |
d62a17ae | 928 | struct bpacket *pkt; |
929 | struct stream *s; | |
930 | struct bgp_adj_out *adj; | |
931 | struct bgp_advertise *adv; | |
932 | struct peer *peer; | |
933 | struct bgp_node *rn; | |
934 | bgp_size_t unfeasible_len; | |
935 | bgp_size_t total_attr_len; | |
936 | size_t mp_start = 0; | |
937 | size_t attrlen_pos = 0; | |
938 | size_t mplen_pos = 0; | |
d7c0a89a | 939 | uint8_t first_time = 1; |
d62a17ae | 940 | afi_t afi; |
941 | safi_t safi; | |
942 | int space_remaining = 0; | |
943 | int space_needed = 0; | |
944 | int num_pfx = 0; | |
945 | int addpath_encode = 0; | |
e386c1d5 | 946 | int addpath_overhead = 0; |
d7c0a89a | 947 | uint32_t addpath_tx_id = 0; |
d62a17ae | 948 | struct prefix_rd *prd = NULL; |
949 | ||
950 | ||
951 | if (!subgrp) | |
952 | return NULL; | |
3f9c7369 | 953 | |
d62a17ae | 954 | if (bpacket_queue_is_full(SUBGRP_INST(subgrp), SUBGRP_PKTQ(subgrp))) |
955 | return NULL; | |
906ad49b | 956 | |
d62a17ae | 957 | peer = SUBGRP_PEER(subgrp); |
958 | afi = SUBGRP_AFI(subgrp); | |
959 | safi = SUBGRP_SAFI(subgrp); | |
960 | s = subgrp->work; | |
961 | stream_reset(s); | |
962 | addpath_encode = bgp_addpath_encode_tx(peer, afi, safi); | |
e386c1d5 | 963 | addpath_overhead = addpath_encode ? BGP_ADDPATH_ID_LEN : 0; |
d62a17ae | 964 | |
965 | while ((adv = BGP_ADV_FIFO_HEAD(&subgrp->sync->withdraw)) != NULL) { | |
966 | assert(adv->rn); | |
967 | adj = adv->adj; | |
968 | rn = adv->rn; | |
969 | addpath_tx_id = adj->addpath_tx_id; | |
970 | ||
971 | space_remaining = | |
2d34fb80 | 972 | STREAM_WRITEABLE(s) - BGP_MAX_PACKET_SIZE_OVERFLOW; |
d62a17ae | 973 | space_needed = |
e386c1d5 | 974 | BGP_NLRI_LENGTH + addpath_overhead + BGP_TOTAL_ATTR_LEN |
d62a17ae | 975 | + bgp_packet_mpattr_prefix_size(afi, safi, &rn->p); |
976 | ||
977 | if (space_remaining < space_needed) | |
978 | break; | |
979 | ||
980 | if (stream_empty(s)) { | |
981 | bgp_packet_set_marker(s, BGP_MSG_UPDATE); | |
982 | stream_putw(s, 0); /* unfeasible routes length */ | |
983 | } else | |
984 | first_time = 0; | |
985 | ||
986 | if (afi == AFI_IP && safi == SAFI_UNICAST | |
987 | && !peer_cap_enhe(peer, afi, safi)) | |
988 | stream_put_prefix_addpath(s, &rn->p, addpath_encode, | |
989 | addpath_tx_id); | |
990 | else { | |
991 | if (rn->prn) | |
992 | prd = (struct prefix_rd *)&rn->prn->p; | |
993 | ||
994 | /* If first time, format the MP_UNREACH header */ | |
995 | if (first_time) { | |
996 | iana_afi_t pkt_afi; | |
5c525538 | 997 | iana_safi_t pkt_safi; |
d62a17ae | 998 | |
999 | pkt_afi = afi_int2iana(afi); | |
1000 | pkt_safi = safi_int2iana(safi); | |
1001 | ||
1002 | attrlen_pos = stream_get_endp(s); | |
1003 | /* total attr length = 0 for now. reevaluate | |
1004 | * later */ | |
1005 | stream_putw(s, 0); | |
1006 | mp_start = stream_get_endp(s); | |
1007 | mplen_pos = bgp_packet_mpunreach_start(s, afi, | |
1008 | safi); | |
1009 | if (bgp_debug_update(NULL, NULL, | |
1010 | subgrp->update_group, 0)) | |
1011 | zlog_debug( | |
1012 | "u%" PRIu64 ":s%" PRIu64 | |
1013 | " send MP_UNREACH for afi/safi %d/%d", | |
1014 | subgrp->update_group->id, | |
1015 | subgrp->id, pkt_afi, pkt_safi); | |
1016 | } | |
1017 | ||
1018 | bgp_packet_mpunreach_prefix(s, &rn->p, afi, safi, prd, | |
b57ba6d2 | 1019 | NULL, 0, addpath_encode, |
d62a17ae | 1020 | addpath_tx_id, NULL); |
1021 | } | |
1022 | ||
1023 | num_pfx++; | |
1024 | ||
1025 | if (bgp_debug_update(NULL, &rn->p, subgrp->update_group, 0)) { | |
1026 | char pfx_buf[BGP_PRD_PATH_STRLEN]; | |
1027 | ||
b57ba6d2 | 1028 | bgp_debug_rdpfxpath2str(afi, safi, prd, &rn->p, NULL, 0, |
d62a17ae | 1029 | addpath_encode, addpath_tx_id, |
1030 | pfx_buf, sizeof(pfx_buf)); | |
1031 | zlog_debug("u%" PRIu64 ":s%" PRIu64 | |
1032 | " send UPDATE %s -- unreachable", | |
1033 | subgrp->update_group->id, subgrp->id, | |
1034 | pfx_buf); | |
1035 | } | |
1036 | ||
1037 | subgrp->scount--; | |
1038 | ||
1039 | bgp_adj_out_remove_subgroup(rn, adj, subgrp); | |
1040 | bgp_unlock_node(rn); | |
3f9c7369 DS |
1041 | } |
1042 | ||
d62a17ae | 1043 | if (!stream_empty(s)) { |
1044 | if (afi == AFI_IP && safi == SAFI_UNICAST | |
1045 | && !peer_cap_enhe(peer, afi, safi)) { | |
1046 | unfeasible_len = stream_get_endp(s) - BGP_HEADER_SIZE | |
1047 | - BGP_UNFEASIBLE_LEN; | |
1048 | stream_putw_at(s, BGP_HEADER_SIZE, unfeasible_len); | |
1049 | stream_putw(s, 0); | |
1050 | } else { | |
1051 | /* Set the mp_unreach attr's length */ | |
1052 | bgp_packet_mpunreach_end(s, mplen_pos); | |
1053 | ||
1054 | /* Set total path attribute length. */ | |
1055 | total_attr_len = stream_get_endp(s) - mp_start; | |
1056 | stream_putw_at(s, attrlen_pos, total_attr_len); | |
1057 | } | |
1058 | bgp_packet_set_size(s); | |
1059 | if (bgp_debug_update(NULL, NULL, subgrp->update_group, 0)) | |
1060 | zlog_debug("u%" PRIu64 ":s%" PRIu64 | |
1061 | " send UPDATE (withdraw) len %zd numpfx %d", | |
1062 | subgrp->update_group->id, subgrp->id, | |
1063 | (stream_get_endp(s) - stream_get_getp(s)), | |
1064 | num_pfx); | |
1065 | pkt = bpacket_queue_add(SUBGRP_PKTQ(subgrp), stream_dup(s), | |
1066 | NULL); | |
1067 | stream_reset(s); | |
1068 | return pkt; | |
3f9c7369 | 1069 | } |
3f9c7369 | 1070 | |
d62a17ae | 1071 | return NULL; |
3f9c7369 DS |
1072 | } |
1073 | ||
d62a17ae | 1074 | void subgroup_default_update_packet(struct update_subgroup *subgrp, |
1075 | struct attr *attr, struct peer *from) | |
3f9c7369 | 1076 | { |
d62a17ae | 1077 | struct stream *s; |
1078 | struct peer *peer; | |
1079 | struct prefix p; | |
1080 | unsigned long pos; | |
1081 | bgp_size_t total_attr_len; | |
1082 | afi_t afi; | |
1083 | safi_t safi; | |
1084 | struct bpacket_attr_vec_arr vecarr; | |
1085 | int addpath_encode = 0; | |
1086 | ||
1087 | if (DISABLE_BGP_ANNOUNCE) | |
1088 | return; | |
1089 | ||
1090 | if (!subgrp) | |
1091 | return; | |
1092 | ||
1093 | peer = SUBGRP_PEER(subgrp); | |
1094 | afi = SUBGRP_AFI(subgrp); | |
1095 | safi = SUBGRP_SAFI(subgrp); | |
1096 | bpacket_attr_vec_arr_reset(&vecarr); | |
1097 | addpath_encode = bgp_addpath_encode_tx(peer, afi, safi); | |
1098 | ||
cbb65f5e RW |
1099 | memset(&p, 0, sizeof(p)); |
1100 | p.family = afi2family(afi); | |
1101 | p.prefixlen = 0; | |
d62a17ae | 1102 | |
1103 | /* Logging the attribute. */ | |
1104 | if (bgp_debug_update(NULL, &p, subgrp->update_group, 0)) { | |
1105 | char attrstr[BUFSIZ]; | |
1106 | char buf[PREFIX_STRLEN]; | |
1107 | /* ' with addpath ID ' 17 | |
1108 | * max strlen of uint32 + 10 | |
1109 | * +/- (just in case) + 1 | |
1110 | * null terminator + 1 | |
1111 | * ============================ 29 */ | |
1112 | char tx_id_buf[30]; | |
1113 | ||
1114 | attrstr[0] = '\0'; | |
1115 | ||
1116 | bgp_dump_attr(attr, attrstr, BUFSIZ); | |
1117 | ||
1118 | if (addpath_encode) | |
1119 | snprintf(tx_id_buf, sizeof(tx_id_buf), | |
1120 | " with addpath ID %u", | |
1121 | BGP_ADDPATH_TX_ID_FOR_DEFAULT_ORIGINATE); | |
1122 | ||
1123 | zlog_debug("u%" PRIu64 ":s%" PRIu64 " send UPDATE %s%s %s", | |
1124 | (SUBGRP_UPDGRP(subgrp))->id, subgrp->id, | |
1125 | prefix2str(&p, buf, sizeof(buf)), tx_id_buf, | |
1126 | attrstr); | |
1127 | } | |
3f9c7369 | 1128 | |
d62a17ae | 1129 | s = stream_new(BGP_MAX_PACKET_SIZE); |
3f9c7369 | 1130 | |
d62a17ae | 1131 | /* Make BGP update packet. */ |
1132 | bgp_packet_set_marker(s, BGP_MSG_UPDATE); | |
3f9c7369 | 1133 | |
d62a17ae | 1134 | /* Unfeasible Routes Length. */ |
1135 | stream_putw(s, 0); | |
3f9c7369 | 1136 | |
d62a17ae | 1137 | /* Make place for total attribute length. */ |
1138 | pos = stream_get_endp(s); | |
1139 | stream_putw(s, 0); | |
1140 | total_attr_len = bgp_packet_attribute( | |
1141 | NULL, peer, s, attr, &vecarr, &p, afi, safi, from, NULL, NULL, | |
b57ba6d2 | 1142 | 0, addpath_encode, BGP_ADDPATH_TX_ID_FOR_DEFAULT_ORIGINATE); |
3f9c7369 | 1143 | |
d62a17ae | 1144 | /* Set Total Path Attribute Length. */ |
1145 | stream_putw_at(s, pos, total_attr_len); | |
3f9c7369 | 1146 | |
d62a17ae | 1147 | /* NLRI set. */ |
1148 | if (p.family == AF_INET && safi == SAFI_UNICAST | |
1149 | && !peer_cap_enhe(peer, afi, safi)) | |
1150 | stream_put_prefix_addpath( | |
1151 | s, &p, addpath_encode, | |
1152 | BGP_ADDPATH_TX_ID_FOR_DEFAULT_ORIGINATE); | |
3f9c7369 | 1153 | |
d62a17ae | 1154 | /* Set size. */ |
1155 | bgp_packet_set_size(s); | |
3f9c7369 | 1156 | |
d62a17ae | 1157 | (void)bpacket_queue_add(SUBGRP_PKTQ(subgrp), s, &vecarr); |
2fc102e1 | 1158 | subgroup_trigger_write(subgrp); |
3f9c7369 DS |
1159 | } |
1160 | ||
d62a17ae | 1161 | void subgroup_default_withdraw_packet(struct update_subgroup *subgrp) |
3f9c7369 | 1162 | { |
d62a17ae | 1163 | struct peer *peer; |
1164 | struct stream *s; | |
1165 | struct prefix p; | |
1166 | unsigned long attrlen_pos = 0; | |
1167 | unsigned long cp; | |
1168 | bgp_size_t unfeasible_len; | |
1169 | bgp_size_t total_attr_len = 0; | |
1170 | size_t mp_start = 0; | |
1171 | size_t mplen_pos = 0; | |
1172 | afi_t afi; | |
1173 | safi_t safi; | |
1174 | int addpath_encode = 0; | |
1175 | ||
1176 | if (DISABLE_BGP_ANNOUNCE) | |
1177 | return; | |
1178 | ||
1179 | peer = SUBGRP_PEER(subgrp); | |
1180 | afi = SUBGRP_AFI(subgrp); | |
1181 | safi = SUBGRP_SAFI(subgrp); | |
1182 | addpath_encode = bgp_addpath_encode_tx(peer, afi, safi); | |
1183 | ||
cbb65f5e RW |
1184 | memset(&p, 0, sizeof(p)); |
1185 | p.family = afi2family(afi); | |
1186 | p.prefixlen = 0; | |
d62a17ae | 1187 | |
1188 | if (bgp_debug_update(NULL, &p, subgrp->update_group, 0)) { | |
1189 | char buf[PREFIX_STRLEN]; | |
1190 | /* ' with addpath ID ' 17 | |
1191 | * max strlen of uint32 + 10 | |
1192 | * +/- (just in case) + 1 | |
1193 | * null terminator + 1 | |
1194 | * ============================ 29 */ | |
1195 | char tx_id_buf[30]; | |
1196 | ||
1197 | if (addpath_encode) | |
1198 | snprintf(tx_id_buf, sizeof(tx_id_buf), | |
1199 | " with addpath ID %u", | |
1200 | BGP_ADDPATH_TX_ID_FOR_DEFAULT_ORIGINATE); | |
1201 | ||
1202 | zlog_debug("u%" PRIu64 ":s%" PRIu64 | |
1203 | " send UPDATE %s%s -- unreachable", | |
1204 | (SUBGRP_UPDGRP(subgrp))->id, subgrp->id, | |
1205 | prefix2str(&p, buf, sizeof(buf)), tx_id_buf); | |
1206 | } | |
3f9c7369 | 1207 | |
d62a17ae | 1208 | s = stream_new(BGP_MAX_PACKET_SIZE); |
3f9c7369 | 1209 | |
d62a17ae | 1210 | /* Make BGP update packet. */ |
1211 | bgp_packet_set_marker(s, BGP_MSG_UPDATE); | |
3f9c7369 | 1212 | |
d62a17ae | 1213 | /* Unfeasible Routes Length. */; |
1214 | cp = stream_get_endp(s); | |
1215 | stream_putw(s, 0); | |
3f9c7369 | 1216 | |
d62a17ae | 1217 | /* Withdrawn Routes. */ |
1218 | if (p.family == AF_INET && safi == SAFI_UNICAST | |
1219 | && !peer_cap_enhe(peer, afi, safi)) { | |
1220 | stream_put_prefix_addpath( | |
1221 | s, &p, addpath_encode, | |
1222 | BGP_ADDPATH_TX_ID_FOR_DEFAULT_ORIGINATE); | |
3f9c7369 | 1223 | |
d62a17ae | 1224 | unfeasible_len = stream_get_endp(s) - cp - 2; |
3f9c7369 | 1225 | |
d62a17ae | 1226 | /* Set unfeasible len. */ |
1227 | stream_putw_at(s, cp, unfeasible_len); | |
3f9c7369 | 1228 | |
d62a17ae | 1229 | /* Set total path attribute length. */ |
1230 | stream_putw(s, 0); | |
1231 | } else { | |
1232 | attrlen_pos = stream_get_endp(s); | |
1233 | stream_putw(s, 0); | |
1234 | mp_start = stream_get_endp(s); | |
1235 | mplen_pos = bgp_packet_mpunreach_start(s, afi, safi); | |
1236 | bgp_packet_mpunreach_prefix( | |
b57ba6d2 | 1237 | s, &p, afi, safi, NULL, NULL, 0, addpath_encode, |
d62a17ae | 1238 | BGP_ADDPATH_TX_ID_FOR_DEFAULT_ORIGINATE, NULL); |
3f9c7369 | 1239 | |
d62a17ae | 1240 | /* Set the mp_unreach attr's length */ |
1241 | bgp_packet_mpunreach_end(s, mplen_pos); | |
3f9c7369 | 1242 | |
d62a17ae | 1243 | /* Set total path attribute length. */ |
1244 | total_attr_len = stream_get_endp(s) - mp_start; | |
1245 | stream_putw_at(s, attrlen_pos, total_attr_len); | |
1246 | } | |
1247 | ||
1248 | bgp_packet_set_size(s); | |
1249 | ||
1250 | (void)bpacket_queue_add(SUBGRP_PKTQ(subgrp), s, NULL); | |
2fc102e1 | 1251 | subgroup_trigger_write(subgrp); |
3f9c7369 DS |
1252 | } |
1253 | ||
1254 | static void | |
d62a17ae | 1255 | bpacket_vec_arr_inherit_attr_flags(struct bpacket_attr_vec_arr *vecarr, |
1256 | bpacket_attr_vec_type type, | |
1257 | struct attr *attr) | |
3f9c7369 | 1258 | { |
d62a17ae | 1259 | if (CHECK_FLAG(attr->rmap_change_flags, |
1260 | BATTR_RMAP_NEXTHOP_PEER_ADDRESS)) | |
1261 | SET_FLAG(vecarr->entries[BGP_ATTR_VEC_NH].flags, | |
1262 | BPKT_ATTRVEC_FLAGS_RMAP_NH_PEER_ADDRESS); | |
1263 | ||
1264 | if (CHECK_FLAG(attr->rmap_change_flags, BATTR_REFLECTED)) | |
1265 | SET_FLAG(vecarr->entries[BGP_ATTR_VEC_NH].flags, | |
1266 | BPKT_ATTRVEC_FLAGS_REFLECTED); | |
1267 | ||
1268 | if (CHECK_FLAG(attr->rmap_change_flags, BATTR_RMAP_NEXTHOP_UNCHANGED)) | |
1269 | SET_FLAG(vecarr->entries[BGP_ATTR_VEC_NH].flags, | |
1270 | BPKT_ATTRVEC_FLAGS_RMAP_NH_UNCHANGED); | |
1271 | ||
1272 | if (CHECK_FLAG(attr->rmap_change_flags, BATTR_RMAP_IPV4_NHOP_CHANGED)) | |
1273 | SET_FLAG(vecarr->entries[BGP_ATTR_VEC_NH].flags, | |
1274 | BPKT_ATTRVEC_FLAGS_RMAP_IPV4_NH_CHANGED); | |
1275 | ||
1276 | if (CHECK_FLAG(attr->rmap_change_flags, | |
1277 | BATTR_RMAP_IPV6_GLOBAL_NHOP_CHANGED)) | |
1278 | SET_FLAG(vecarr->entries[BGP_ATTR_VEC_NH].flags, | |
1279 | BPKT_ATTRVEC_FLAGS_RMAP_IPV6_GNH_CHANGED); | |
1280 | ||
1281 | if (CHECK_FLAG(attr->rmap_change_flags, | |
1282 | BATTR_RMAP_IPV6_LL_NHOP_CHANGED)) | |
1283 | SET_FLAG(vecarr->entries[BGP_ATTR_VEC_NH].flags, | |
1284 | BPKT_ATTRVEC_FLAGS_RMAP_IPV6_LNH_CHANGED); | |
3f9c7369 DS |
1285 | } |
1286 | ||
1287 | /* Reset the Attributes vector array. The vector array is used to override | |
1288 | * certain output parameters in the packet for a particular peer | |
1289 | */ | |
d62a17ae | 1290 | void bpacket_attr_vec_arr_reset(struct bpacket_attr_vec_arr *vecarr) |
3f9c7369 | 1291 | { |
d62a17ae | 1292 | int i; |
3f9c7369 | 1293 | |
d62a17ae | 1294 | if (!vecarr) |
1295 | return; | |
3f9c7369 | 1296 | |
d62a17ae | 1297 | i = 0; |
1298 | while (i < BGP_ATTR_VEC_MAX) { | |
1299 | vecarr->entries[i].flags = 0; | |
1300 | vecarr->entries[i].offset = 0; | |
1301 | i++; | |
1302 | } | |
3f9c7369 DS |
1303 | } |
1304 | ||
1305 | /* Setup a particular node entry in the vecarr */ | |
d62a17ae | 1306 | void bpacket_attr_vec_arr_set_vec(struct bpacket_attr_vec_arr *vecarr, |
1307 | bpacket_attr_vec_type type, struct stream *s, | |
1308 | struct attr *attr) | |
3f9c7369 | 1309 | { |
d62a17ae | 1310 | if (!vecarr) |
1311 | return; | |
1312 | assert(type < BGP_ATTR_VEC_MAX); | |
1313 | ||
1314 | SET_FLAG(vecarr->entries[type].flags, BPKT_ATTRVEC_FLAGS_UPDATED); | |
1315 | vecarr->entries[type].offset = stream_get_endp(s); | |
1316 | if (attr) | |
1317 | bpacket_vec_arr_inherit_attr_flags(vecarr, type, attr); | |
3f9c7369 | 1318 | } |