]>
Commit | Line | Data |
---|---|---|
3f9c7369 DS |
1 | /** |
2 | * bgp_updgrp_packet.c: BGP update group packet handling routines | |
3 | * | |
4 | * @copyright Copyright (C) 2014 Cumulus Networks, Inc. | |
5 | * | |
6 | * @author Avneesh Sachdev <avneesh@sproute.net> | |
7 | * @author Rajesh Varadarajan <rajesh@sproute.net> | |
8 | * @author Pradosh Mohapatra <pradosh@sproute.net> | |
9 | * | |
10 | * This file is part of GNU Zebra. | |
11 | * | |
12 | * GNU Zebra is free software; you can redistribute it and/or modify it | |
13 | * under the terms of the GNU General Public License as published by the | |
14 | * Free Software Foundation; either version 2, or (at your option) any | |
15 | * later version. | |
16 | * | |
17 | * GNU Zebra is distributed in the hope that it will be useful, but | |
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
20 | * General Public License for more details. | |
21 | * | |
896014f4 DL |
22 | * You should have received a copy of the GNU General Public License along |
23 | * with this program; see the file COPYING; if not, write to the Free Software | |
24 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
3f9c7369 DS |
25 | */ |
26 | ||
27 | #include <zebra.h> | |
28 | ||
29 | #include "prefix.h" | |
30 | #include "thread.h" | |
31 | #include "buffer.h" | |
32 | #include "stream.h" | |
33 | #include "command.h" | |
34 | #include "sockunion.h" | |
35 | #include "network.h" | |
36 | #include "memory.h" | |
37 | #include "filter.h" | |
38 | #include "routemap.h" | |
3f9c7369 DS |
39 | #include "log.h" |
40 | #include "plist.h" | |
41 | #include "linklist.h" | |
42 | #include "workqueue.h" | |
43 | #include "hash.h" | |
44 | #include "queue.h" | |
cd1964ff | 45 | #include "mpls.h" |
3f9c7369 DS |
46 | |
47 | #include "bgpd/bgpd.h" | |
48 | #include "bgpd/bgp_debug.h" | |
14454c9f | 49 | #include "bgpd/bgp_errors.h" |
3f9c7369 DS |
50 | #include "bgpd/bgp_fsm.h" |
51 | #include "bgpd/bgp_route.h" | |
52 | #include "bgpd/bgp_packet.h" | |
53 | #include "bgpd/bgp_advertise.h" | |
54 | #include "bgpd/bgp_updgrp.h" | |
55 | #include "bgpd/bgp_nexthop.h" | |
56 | #include "bgpd/bgp_nht.h" | |
906ad49b | 57 | #include "bgpd/bgp_mplsvpn.h" |
cd1964ff | 58 | #include "bgpd/bgp_label.h" |
3f9c7369 DS |
59 | |
60 | /******************** | |
61 | * PRIVATE FUNCTIONS | |
62 | ********************/ | |
63 | ||
64 | /******************** | |
65 | * PUBLIC FUNCTIONS | |
66 | ********************/ | |
d62a17ae | 67 | struct bpacket *bpacket_alloc() |
3f9c7369 | 68 | { |
d62a17ae | 69 | struct bpacket *pkt; |
3f9c7369 | 70 | |
d62a17ae | 71 | pkt = (struct bpacket *)XCALLOC(MTYPE_BGP_PACKET, |
72 | sizeof(struct bpacket)); | |
3f9c7369 | 73 | |
d62a17ae | 74 | return pkt; |
3f9c7369 DS |
75 | } |
76 | ||
d62a17ae | 77 | void bpacket_free(struct bpacket *pkt) |
3f9c7369 | 78 | { |
d62a17ae | 79 | if (pkt->buffer) |
80 | stream_free(pkt->buffer); | |
81 | pkt->buffer = NULL; | |
82 | XFREE(MTYPE_BGP_PACKET, pkt); | |
3f9c7369 DS |
83 | } |
84 | ||
d62a17ae | 85 | void bpacket_queue_init(struct bpacket_queue *q) |
3f9c7369 | 86 | { |
d62a17ae | 87 | TAILQ_INIT(&(q->pkts)); |
3f9c7369 DS |
88 | } |
89 | ||
90 | /* | |
91 | * bpacket_queue_sanity_check | |
92 | */ | |
d62a17ae | 93 | void bpacket_queue_sanity_check(struct bpacket_queue __attribute__((__unused__)) |
94 | * q) | |
3f9c7369 DS |
95 | { |
96 | #if 0 | |
97 | struct bpacket *pkt; | |
98 | ||
99 | pkt = bpacket_queue_last (q); | |
100 | assert (pkt); | |
101 | assert (!pkt->buffer); | |
102 | ||
103 | /* | |
104 | * Make sure the count of packets is correct. | |
105 | */ | |
106 | int num_pkts = 0; | |
107 | ||
108 | pkt = bpacket_queue_first (q); | |
109 | while (pkt) | |
110 | { | |
111 | num_pkts++; | |
112 | ||
113 | if (num_pkts > q->curr_count) | |
114 | assert (0); | |
115 | ||
116 | pkt = TAILQ_NEXT (pkt, pkt_train); | |
117 | } | |
118 | ||
119 | assert (num_pkts == q->curr_count); | |
120 | #endif | |
121 | } | |
122 | ||
123 | /* | |
124 | * bpacket_queue_add_packet | |
125 | * | |
126 | * Internal function of bpacket_queue - and adds a | |
127 | * packet entry to the end of the list. | |
128 | * | |
129 | * Users of bpacket_queue should use bpacket_queue_add instead. | |
130 | */ | |
d62a17ae | 131 | static void bpacket_queue_add_packet(struct bpacket_queue *q, |
132 | struct bpacket *pkt) | |
3f9c7369 | 133 | { |
d62a17ae | 134 | struct bpacket *last_pkt; |
3f9c7369 | 135 | |
d62a17ae | 136 | if (TAILQ_EMPTY(&(q->pkts))) |
137 | TAILQ_INSERT_TAIL(&(q->pkts), pkt, pkt_train); | |
138 | else { | |
139 | last_pkt = bpacket_queue_last(q); | |
140 | TAILQ_INSERT_AFTER(&(q->pkts), last_pkt, pkt, pkt_train); | |
141 | } | |
142 | q->curr_count++; | |
143 | if (q->hwm_count < q->curr_count) | |
144 | q->hwm_count = q->curr_count; | |
3f9c7369 DS |
145 | } |
146 | ||
147 | /* | |
148 | * Adds a packet to the bpacket_queue. | |
149 | * | |
150 | * The stream passed is consumed by this function. So, the caller should | |
151 | * not free or use the stream after | |
152 | * invoking this function. | |
153 | */ | |
d62a17ae | 154 | struct bpacket *bpacket_queue_add(struct bpacket_queue *q, struct stream *s, |
155 | struct bpacket_attr_vec_arr *vecarrp) | |
3f9c7369 | 156 | { |
d62a17ae | 157 | struct bpacket *pkt; |
158 | struct bpacket *last_pkt; | |
159 | ||
160 | ||
161 | pkt = bpacket_alloc(); | |
162 | if (TAILQ_EMPTY(&(q->pkts))) { | |
163 | pkt->ver = 1; | |
164 | pkt->buffer = s; | |
165 | if (vecarrp) | |
166 | memcpy(&pkt->arr, vecarrp, | |
167 | sizeof(struct bpacket_attr_vec_arr)); | |
168 | else | |
169 | bpacket_attr_vec_arr_reset(&pkt->arr); | |
170 | bpacket_queue_add_packet(q, pkt); | |
171 | bpacket_queue_sanity_check(q); | |
172 | return pkt; | |
173 | } | |
3f9c7369 | 174 | |
d62a17ae | 175 | /* |
176 | * Fill in the new information into the current sentinel and create a | |
177 | * new sentinel. | |
178 | */ | |
179 | bpacket_queue_sanity_check(q); | |
180 | last_pkt = bpacket_queue_last(q); | |
181 | assert(last_pkt->buffer == NULL); | |
182 | last_pkt->buffer = s; | |
183 | if (vecarrp) | |
184 | memcpy(&last_pkt->arr, vecarrp, | |
185 | sizeof(struct bpacket_attr_vec_arr)); | |
186 | else | |
187 | bpacket_attr_vec_arr_reset(&last_pkt->arr); | |
188 | ||
189 | pkt->ver = last_pkt->ver; | |
190 | pkt->ver++; | |
191 | bpacket_queue_add_packet(q, pkt); | |
192 | ||
193 | bpacket_queue_sanity_check(q); | |
194 | return last_pkt; | |
3f9c7369 DS |
195 | } |
196 | ||
d62a17ae | 197 | struct bpacket *bpacket_queue_first(struct bpacket_queue *q) |
3f9c7369 | 198 | { |
d62a17ae | 199 | return (TAILQ_FIRST(&(q->pkts))); |
3f9c7369 DS |
200 | } |
201 | ||
d62a17ae | 202 | struct bpacket *bpacket_queue_last(struct bpacket_queue *q) |
3f9c7369 | 203 | { |
d62a17ae | 204 | return TAILQ_LAST(&(q->pkts), pkt_queue); |
3f9c7369 DS |
205 | } |
206 | ||
d62a17ae | 207 | struct bpacket *bpacket_queue_remove(struct bpacket_queue *q) |
3f9c7369 | 208 | { |
d62a17ae | 209 | struct bpacket *first; |
3f9c7369 | 210 | |
d62a17ae | 211 | first = bpacket_queue_first(q); |
212 | if (first) { | |
213 | TAILQ_REMOVE(&(q->pkts), first, pkt_train); | |
214 | q->curr_count--; | |
215 | } | |
216 | return first; | |
3f9c7369 DS |
217 | } |
218 | ||
d62a17ae | 219 | unsigned int bpacket_queue_length(struct bpacket_queue *q) |
3f9c7369 | 220 | { |
d62a17ae | 221 | return q->curr_count - 1; |
3f9c7369 DS |
222 | } |
223 | ||
d62a17ae | 224 | unsigned int bpacket_queue_hwm_length(struct bpacket_queue *q) |
3f9c7369 | 225 | { |
d62a17ae | 226 | return q->hwm_count - 1; |
3f9c7369 DS |
227 | } |
228 | ||
d62a17ae | 229 | int bpacket_queue_is_full(struct bgp *bgp, struct bpacket_queue *q) |
3f9c7369 | 230 | { |
d62a17ae | 231 | if (q->curr_count >= bgp->default_subgroup_pkt_queue_max) |
232 | return 1; | |
233 | return 0; | |
3f9c7369 DS |
234 | } |
235 | ||
d62a17ae | 236 | void bpacket_add_peer(struct bpacket *pkt, struct peer_af *paf) |
3f9c7369 | 237 | { |
d62a17ae | 238 | if (!pkt || !paf) |
239 | return; | |
3f9c7369 | 240 | |
d62a17ae | 241 | LIST_INSERT_HEAD(&(pkt->peers), paf, pkt_train); |
242 | paf->next_pkt_to_send = pkt; | |
3f9c7369 DS |
243 | } |
244 | ||
245 | /* | |
246 | * bpacket_queue_cleanup | |
247 | */ | |
d62a17ae | 248 | void bpacket_queue_cleanup(struct bpacket_queue *q) |
3f9c7369 | 249 | { |
d62a17ae | 250 | struct bpacket *pkt; |
3f9c7369 | 251 | |
d62a17ae | 252 | while ((pkt = bpacket_queue_remove(q))) { |
253 | bpacket_free(pkt); | |
254 | } | |
3f9c7369 DS |
255 | } |
256 | ||
257 | /* | |
258 | * bpacket_queue_compact | |
259 | * | |
260 | * Delete packets that do not need to be transmitted to any peer from | |
261 | * the queue. | |
262 | * | |
263 | * @return the number of packets deleted. | |
264 | */ | |
d62a17ae | 265 | static int bpacket_queue_compact(struct bpacket_queue *q) |
3f9c7369 | 266 | { |
d62a17ae | 267 | int num_deleted; |
268 | struct bpacket *pkt, *removed_pkt; | |
3f9c7369 | 269 | |
d62a17ae | 270 | num_deleted = 0; |
3f9c7369 | 271 | |
d62a17ae | 272 | while (1) { |
273 | pkt = bpacket_queue_first(q); | |
274 | if (!pkt) | |
275 | break; | |
3f9c7369 | 276 | |
d62a17ae | 277 | /* |
278 | * Don't delete the sentinel. | |
279 | */ | |
280 | if (!pkt->buffer) | |
281 | break; | |
3f9c7369 | 282 | |
d62a17ae | 283 | if (!LIST_EMPTY(&(pkt->peers))) |
284 | break; | |
3f9c7369 | 285 | |
d62a17ae | 286 | removed_pkt = bpacket_queue_remove(q); |
287 | assert(pkt == removed_pkt); | |
288 | bpacket_free(removed_pkt); | |
3f9c7369 | 289 | |
d62a17ae | 290 | num_deleted++; |
291 | } | |
3f9c7369 | 292 | |
d62a17ae | 293 | bpacket_queue_sanity_check(q); |
294 | return num_deleted; | |
3f9c7369 DS |
295 | } |
296 | ||
d62a17ae | 297 | void bpacket_queue_advance_peer(struct peer_af *paf) |
3f9c7369 | 298 | { |
d62a17ae | 299 | struct bpacket *pkt; |
300 | struct bpacket *old_pkt; | |
301 | ||
302 | old_pkt = paf->next_pkt_to_send; | |
303 | if (old_pkt->buffer == NULL) | |
304 | /* Already at end of list */ | |
305 | return; | |
306 | ||
307 | LIST_REMOVE(paf, pkt_train); | |
308 | pkt = TAILQ_NEXT(old_pkt, pkt_train); | |
309 | bpacket_add_peer(pkt, paf); | |
310 | ||
311 | if (!bpacket_queue_compact(PAF_PKTQ(paf))) | |
312 | return; | |
313 | ||
314 | /* | |
315 | * Deleted one or more packets. Check if we can now merge this | |
316 | * peer's subgroup into another subgroup. | |
317 | */ | |
318 | update_subgroup_check_merge(paf->subgroup, "advanced peer in queue"); | |
3f9c7369 DS |
319 | } |
320 | ||
321 | /* | |
322 | * bpacket_queue_remove_peer | |
323 | * | |
324 | * Remove the peer from the packet queue of the subgroup it belongs | |
325 | * to. | |
326 | */ | |
d62a17ae | 327 | void bpacket_queue_remove_peer(struct peer_af *paf) |
3f9c7369 | 328 | { |
d62a17ae | 329 | struct bpacket_queue *q; |
3f9c7369 | 330 | |
d62a17ae | 331 | q = PAF_PKTQ(paf); |
332 | assert(q); | |
333 | if (!q) | |
334 | return; | |
3f9c7369 | 335 | |
d62a17ae | 336 | LIST_REMOVE(paf, pkt_train); |
337 | paf->next_pkt_to_send = NULL; | |
3f9c7369 | 338 | |
d62a17ae | 339 | bpacket_queue_compact(q); |
3f9c7369 DS |
340 | } |
341 | ||
d62a17ae | 342 | unsigned int bpacket_queue_virtual_length(struct peer_af *paf) |
3f9c7369 | 343 | { |
d62a17ae | 344 | struct bpacket *pkt; |
345 | struct bpacket *last; | |
346 | struct bpacket_queue *q; | |
3f9c7369 | 347 | |
d62a17ae | 348 | pkt = paf->next_pkt_to_send; |
349 | if (!pkt || (pkt->buffer == NULL)) | |
350 | /* Already at end of list */ | |
351 | return 0; | |
3f9c7369 | 352 | |
d62a17ae | 353 | q = PAF_PKTQ(paf); |
354 | if (TAILQ_EMPTY(&(q->pkts))) | |
355 | return 0; | |
3f9c7369 | 356 | |
d62a17ae | 357 | last = TAILQ_LAST(&(q->pkts), pkt_queue); |
358 | if (last->ver >= pkt->ver) | |
359 | return last->ver - pkt->ver; | |
3f9c7369 | 360 | |
d62a17ae | 361 | /* sequence # rolled over */ |
362 | return (UINT_MAX - pkt->ver + 1) + last->ver; | |
3f9c7369 DS |
363 | } |
364 | ||
365 | /* | |
366 | * Dump the bpacket queue | |
367 | */ | |
d62a17ae | 368 | void bpacket_queue_show_vty(struct bpacket_queue *q, struct vty *vty) |
3f9c7369 | 369 | { |
d62a17ae | 370 | struct bpacket *pkt; |
371 | struct peer_af *paf; | |
372 | ||
373 | pkt = bpacket_queue_first(q); | |
374 | while (pkt) { | |
375 | vty_out(vty, " Packet %p ver %u buffer %p\n", pkt, pkt->ver, | |
376 | pkt->buffer); | |
377 | ||
a2addae8 | 378 | LIST_FOREACH (paf, &(pkt->peers), pkt_train) { |
d62a17ae | 379 | vty_out(vty, " - %s\n", paf->peer->host); |
380 | } | |
381 | pkt = bpacket_next(pkt); | |
382 | } | |
383 | return; | |
3f9c7369 DS |
384 | } |
385 | ||
d62a17ae | 386 | struct stream *bpacket_reformat_for_peer(struct bpacket *pkt, |
387 | struct peer_af *paf) | |
3f9c7369 | 388 | { |
d62a17ae | 389 | struct stream *s = NULL; |
390 | bpacket_attr_vec *vec; | |
391 | struct peer *peer; | |
392 | char buf[BUFSIZ]; | |
393 | char buf2[BUFSIZ]; | |
394 | ||
395 | s = stream_dup(pkt->buffer); | |
396 | peer = PAF_PEER(paf); | |
397 | ||
398 | vec = &pkt->arr.entries[BGP_ATTR_VEC_NH]; | |
399 | if (CHECK_FLAG(vec->flags, BPKT_ATTRVEC_FLAGS_UPDATED)) { | |
d7c0a89a | 400 | uint8_t nhlen; |
a2b6e694 | 401 | afi_t nhafi; |
d62a17ae | 402 | int route_map_sets_nh; |
403 | nhlen = stream_getc_from(s, vec->offset); | |
404 | if (peer_cap_enhe(peer, paf->afi, paf->safi)) | |
405 | nhafi = AFI_IP6; | |
406 | else | |
407 | nhafi = BGP_NEXTHOP_AFI_FROM_NHLEN(nhlen); | |
408 | ||
409 | if (nhafi == AFI_IP) { | |
410 | struct in_addr v4nh, *mod_v4nh; | |
411 | int nh_modified = 0; | |
412 | size_t offset_nh = vec->offset + 1; | |
413 | ||
414 | route_map_sets_nh = | |
415 | (CHECK_FLAG( | |
416 | vec->flags, | |
417 | BPKT_ATTRVEC_FLAGS_RMAP_IPV4_NH_CHANGED) | |
418 | || CHECK_FLAG( | |
419 | vec->flags, | |
420 | BPKT_ATTRVEC_FLAGS_RMAP_NH_PEER_ADDRESS)); | |
421 | ||
422 | switch (nhlen) { | |
423 | case BGP_ATTR_NHLEN_IPV4: | |
424 | break; | |
425 | case BGP_ATTR_NHLEN_VPNV4: | |
426 | offset_nh += 8; | |
427 | break; | |
428 | default: | |
429 | /* TODO: handle IPv6 nexthops */ | |
78cb6555 DS |
430 | flog_warn(BGP_WARN_INVALID_NEXTHOP_LENGTH, |
431 | "%s: %s: invalid MP nexthop length (AFI IP): %u", | |
432 | __func__, peer->host, nhlen); | |
d62a17ae | 433 | stream_free(s); |
434 | return NULL; | |
435 | } | |
436 | ||
437 | stream_get_from(&v4nh, s, offset_nh, IPV4_MAX_BYTELEN); | |
438 | mod_v4nh = &v4nh; | |
439 | ||
440 | /* | |
441 | * If route-map has set the nexthop, that is always | |
442 | * used; if it is | |
443 | * specified as peer-address, the peering address is | |
444 | * picked up. | |
445 | * Otherwise, if NH is unavailable from attribute, the | |
446 | * peering addr | |
447 | * is picked up; the "NH unavailable" case also covers | |
448 | * next-hop-self | |
449 | * and some other scenarios -- see | |
450 | * subgroup_announce_check(). In | |
451 | * all other cases, use the nexthop carried in the | |
452 | * attribute unless | |
453 | * it is EBGP non-multiaccess and there is no | |
454 | * next-hop-unchanged setting. | |
455 | * Note: It is assumed route-map cannot set the nexthop | |
456 | * to an | |
457 | * invalid value. | |
458 | */ | |
459 | if (route_map_sets_nh) { | |
460 | if (CHECK_FLAG( | |
461 | vec->flags, | |
462 | BPKT_ATTRVEC_FLAGS_RMAP_NH_PEER_ADDRESS)) { | |
463 | mod_v4nh = &peer->nexthop.v4; | |
464 | nh_modified = 1; | |
465 | } | |
466 | } else if (!v4nh.s_addr) { | |
467 | mod_v4nh = &peer->nexthop.v4; | |
468 | nh_modified = 1; | |
469 | } else if ( | |
470 | peer->sort == BGP_PEER_EBGP | |
d62a17ae | 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( | |
ace295a9 | 476 | peer, paf->afi, paf->safi, |
d62a17ae | 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 */ | |
78cb6555 DS |
535 | flog_warn(BGP_WARN_INVALID_NEXTHOP_LENGTH, |
536 | "%s: %s: invalid MP nexthop length (AFI IP6): %u", | |
537 | __func__, peer->host, nhlen); | |
d62a17ae | 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) { | |
af4c2728 | 790 | flog_err( |
14454c9f | 791 | BGP_ERR_UPDGRP_ATTR_LEN, |
d62a17ae | 792 | "u%" PRIu64 ":s%" PRIu64 |
793 | " attributes too long, cannot send UPDATE", | |
794 | subgrp->update_group->id, subgrp->id); | |
795 | ||
796 | /* Flush the FIFO update queue */ | |
797 | while (adv) | |
798 | adv = bgp_advertise_clean_subgroup( | |
799 | subgrp, adj); | |
800 | return NULL; | |
801 | } | |
802 | ||
803 | if (BGP_DEBUG(update, UPDATE_OUT) | |
804 | || BGP_DEBUG(update, UPDATE_PREFIX)) { | |
805 | memset(send_attr_str, 0, BUFSIZ); | |
806 | send_attr_printed = 0; | |
807 | bgp_dump_attr(adv->baa->attr, send_attr_str, | |
808 | BUFSIZ); | |
809 | } | |
810 | } | |
811 | ||
812 | if ((afi == AFI_IP && safi == SAFI_UNICAST) | |
813 | && !peer_cap_enhe(peer, afi, safi)) | |
814 | stream_put_prefix_addpath(s, &rn->p, addpath_encode, | |
815 | addpath_tx_id); | |
816 | else { | |
817 | /* Encode the prefix in MP_REACH_NLRI attribute */ | |
818 | if (rn->prn) | |
819 | prd = (struct prefix_rd *)&rn->prn->p; | |
820 | ||
b57ba6d2 | 821 | if (safi == SAFI_LABELED_UNICAST) { |
d62a17ae | 822 | label = bgp_adv_label(rn, binfo, peer, afi, |
823 | safi); | |
b57ba6d2 MK |
824 | label_pnt = &label; |
825 | num_labels = 1; | |
826 | } else if (binfo && binfo->extra) { | |
827 | label_pnt = &binfo->extra->label[0]; | |
828 | num_labels = binfo->extra->num_labels; | |
829 | } | |
d62a17ae | 830 | |
831 | if (stream_empty(snlri)) | |
832 | mpattrlen_pos = bgp_packet_mpattr_start( | |
833 | snlri, peer, afi, safi, &vecarr, | |
834 | adv->baa->attr); | |
835 | ||
836 | bgp_packet_mpattr_prefix(snlri, afi, safi, &rn->p, prd, | |
b57ba6d2 MK |
837 | label_pnt, num_labels, |
838 | addpath_encode, addpath_tx_id, | |
839 | adv->baa->attr); | |
d62a17ae | 840 | } |
841 | ||
842 | num_pfx++; | |
843 | ||
844 | if (bgp_debug_update(NULL, &rn->p, subgrp->update_group, 0)) { | |
845 | char pfx_buf[BGP_PRD_PATH_STRLEN]; | |
846 | ||
847 | if (!send_attr_printed) { | |
848 | zlog_debug("u%" PRIu64 ":s%" PRIu64 | |
849 | " send UPDATE w/ attr: %s", | |
850 | subgrp->update_group->id, subgrp->id, | |
851 | send_attr_str); | |
852 | if (!stream_empty(snlri)) { | |
853 | iana_afi_t pkt_afi; | |
5c525538 | 854 | iana_safi_t pkt_safi; |
d62a17ae | 855 | |
856 | pkt_afi = afi_int2iana(afi); | |
857 | pkt_safi = safi_int2iana(safi); | |
858 | zlog_debug( | |
859 | "u%" PRIu64 ":s%" PRIu64 | |
860 | " send MP_REACH for afi/safi %d/%d", | |
861 | subgrp->update_group->id, | |
862 | subgrp->id, pkt_afi, pkt_safi); | |
863 | } | |
864 | ||
865 | send_attr_printed = 1; | |
866 | } | |
867 | ||
b57ba6d2 MK |
868 | bgp_debug_rdpfxpath2str(afi, safi, prd, &rn->p, |
869 | label_pnt, num_labels, | |
d62a17ae | 870 | addpath_encode, addpath_tx_id, |
871 | pfx_buf, sizeof(pfx_buf)); | |
872 | zlog_debug("u%" PRIu64 ":s%" PRIu64 " send UPDATE %s", | |
873 | subgrp->update_group->id, subgrp->id, | |
874 | pfx_buf); | |
875 | } | |
876 | ||
877 | /* Synchnorize attribute. */ | |
878 | if (adj->attr) | |
879 | bgp_attr_unintern(&adj->attr); | |
880 | else | |
881 | subgrp->scount++; | |
882 | ||
883 | adj->attr = bgp_attr_intern(adv->baa->attr); | |
884 | ||
885 | adv = bgp_advertise_clean_subgroup(subgrp, adj); | |
3f9c7369 DS |
886 | } |
887 | ||
d62a17ae | 888 | if (!stream_empty(s)) { |
889 | if (!stream_empty(snlri)) { | |
890 | bgp_packet_mpattr_end(snlri, mpattrlen_pos); | |
891 | total_attr_len += stream_get_endp(snlri); | |
892 | } | |
893 | ||
894 | /* set the total attribute length correctly */ | |
895 | stream_putw_at(s, attrlen_pos, total_attr_len); | |
896 | ||
897 | if (!stream_empty(snlri)) { | |
898 | packet = stream_dupcat(s, snlri, mpattr_pos); | |
899 | bpacket_attr_vec_arr_update(&vecarr, mpattr_pos); | |
900 | } else | |
901 | packet = stream_dup(s); | |
902 | bgp_packet_set_size(packet); | |
903 | if (bgp_debug_update(NULL, NULL, subgrp->update_group, 0)) | |
904 | zlog_debug("u%" PRIu64 ":s%" PRIu64 | |
905 | " send UPDATE len %zd numpfx %d", | |
906 | subgrp->update_group->id, subgrp->id, | |
907 | (stream_get_endp(packet) | |
908 | - stream_get_getp(packet)), | |
909 | num_pfx); | |
910 | pkt = bpacket_queue_add(SUBGRP_PKTQ(subgrp), packet, &vecarr); | |
911 | stream_reset(s); | |
912 | stream_reset(snlri); | |
913 | return pkt; | |
3f9c7369 | 914 | } |
d62a17ae | 915 | return NULL; |
3f9c7369 DS |
916 | } |
917 | ||
918 | /* Make BGP withdraw packet. */ | |
919 | /* For ipv4 unicast: | |
920 | 16-octet marker | 2-octet length | 1-octet type | | |
921 | 2-octet withdrawn route length | withdrawn prefixes | 2-octet attrlen (=0) | |
922 | */ | |
923 | /* For other afi/safis: | |
924 | 16-octet marker | 2-octet length | 1-octet type | | |
925 | 2-octet withdrawn route length (=0) | 2-octet attrlen | | |
926 | mp_unreach attr type | attr len | afi | safi | withdrawn prefixes | |
927 | */ | |
d62a17ae | 928 | struct bpacket *subgroup_withdraw_packet(struct update_subgroup *subgrp) |
3f9c7369 | 929 | { |
d62a17ae | 930 | struct bpacket *pkt; |
931 | struct stream *s; | |
932 | struct bgp_adj_out *adj; | |
933 | struct bgp_advertise *adv; | |
934 | struct peer *peer; | |
935 | struct bgp_node *rn; | |
936 | bgp_size_t unfeasible_len; | |
937 | bgp_size_t total_attr_len; | |
938 | size_t mp_start = 0; | |
939 | size_t attrlen_pos = 0; | |
940 | size_t mplen_pos = 0; | |
d7c0a89a | 941 | uint8_t first_time = 1; |
d62a17ae | 942 | afi_t afi; |
943 | safi_t safi; | |
944 | int space_remaining = 0; | |
945 | int space_needed = 0; | |
946 | int num_pfx = 0; | |
947 | int addpath_encode = 0; | |
e386c1d5 | 948 | int addpath_overhead = 0; |
d7c0a89a | 949 | uint32_t addpath_tx_id = 0; |
d62a17ae | 950 | struct prefix_rd *prd = NULL; |
951 | ||
952 | ||
953 | if (!subgrp) | |
954 | return NULL; | |
3f9c7369 | 955 | |
d62a17ae | 956 | if (bpacket_queue_is_full(SUBGRP_INST(subgrp), SUBGRP_PKTQ(subgrp))) |
957 | return NULL; | |
906ad49b | 958 | |
d62a17ae | 959 | peer = SUBGRP_PEER(subgrp); |
960 | afi = SUBGRP_AFI(subgrp); | |
961 | safi = SUBGRP_SAFI(subgrp); | |
962 | s = subgrp->work; | |
963 | stream_reset(s); | |
964 | addpath_encode = bgp_addpath_encode_tx(peer, afi, safi); | |
e386c1d5 | 965 | addpath_overhead = addpath_encode ? BGP_ADDPATH_ID_LEN : 0; |
d62a17ae | 966 | |
967 | while ((adv = BGP_ADV_FIFO_HEAD(&subgrp->sync->withdraw)) != NULL) { | |
968 | assert(adv->rn); | |
969 | adj = adv->adj; | |
970 | rn = adv->rn; | |
971 | addpath_tx_id = adj->addpath_tx_id; | |
972 | ||
973 | space_remaining = | |
2d34fb80 | 974 | STREAM_WRITEABLE(s) - BGP_MAX_PACKET_SIZE_OVERFLOW; |
d62a17ae | 975 | space_needed = |
e386c1d5 | 976 | BGP_NLRI_LENGTH + addpath_overhead + BGP_TOTAL_ATTR_LEN |
d62a17ae | 977 | + bgp_packet_mpattr_prefix_size(afi, safi, &rn->p); |
978 | ||
979 | if (space_remaining < space_needed) | |
980 | break; | |
981 | ||
982 | if (stream_empty(s)) { | |
983 | bgp_packet_set_marker(s, BGP_MSG_UPDATE); | |
984 | stream_putw(s, 0); /* unfeasible routes length */ | |
985 | } else | |
986 | first_time = 0; | |
987 | ||
988 | if (afi == AFI_IP && safi == SAFI_UNICAST | |
989 | && !peer_cap_enhe(peer, afi, safi)) | |
990 | stream_put_prefix_addpath(s, &rn->p, addpath_encode, | |
991 | addpath_tx_id); | |
992 | else { | |
993 | if (rn->prn) | |
994 | prd = (struct prefix_rd *)&rn->prn->p; | |
995 | ||
996 | /* If first time, format the MP_UNREACH header */ | |
997 | if (first_time) { | |
998 | iana_afi_t pkt_afi; | |
5c525538 | 999 | iana_safi_t pkt_safi; |
d62a17ae | 1000 | |
1001 | pkt_afi = afi_int2iana(afi); | |
1002 | pkt_safi = safi_int2iana(safi); | |
1003 | ||
1004 | attrlen_pos = stream_get_endp(s); | |
1005 | /* total attr length = 0 for now. reevaluate | |
1006 | * later */ | |
1007 | stream_putw(s, 0); | |
1008 | mp_start = stream_get_endp(s); | |
1009 | mplen_pos = bgp_packet_mpunreach_start(s, afi, | |
1010 | safi); | |
1011 | if (bgp_debug_update(NULL, NULL, | |
1012 | subgrp->update_group, 0)) | |
1013 | zlog_debug( | |
1014 | "u%" PRIu64 ":s%" PRIu64 | |
1015 | " send MP_UNREACH for afi/safi %d/%d", | |
1016 | subgrp->update_group->id, | |
1017 | subgrp->id, pkt_afi, pkt_safi); | |
1018 | } | |
1019 | ||
1020 | bgp_packet_mpunreach_prefix(s, &rn->p, afi, safi, prd, | |
b57ba6d2 | 1021 | NULL, 0, addpath_encode, |
d62a17ae | 1022 | addpath_tx_id, NULL); |
1023 | } | |
1024 | ||
1025 | num_pfx++; | |
1026 | ||
1027 | if (bgp_debug_update(NULL, &rn->p, subgrp->update_group, 0)) { | |
1028 | char pfx_buf[BGP_PRD_PATH_STRLEN]; | |
1029 | ||
b57ba6d2 | 1030 | bgp_debug_rdpfxpath2str(afi, safi, prd, &rn->p, NULL, 0, |
d62a17ae | 1031 | addpath_encode, addpath_tx_id, |
1032 | pfx_buf, sizeof(pfx_buf)); | |
1033 | zlog_debug("u%" PRIu64 ":s%" PRIu64 | |
1034 | " send UPDATE %s -- unreachable", | |
1035 | subgrp->update_group->id, subgrp->id, | |
1036 | pfx_buf); | |
1037 | } | |
1038 | ||
1039 | subgrp->scount--; | |
1040 | ||
1041 | bgp_adj_out_remove_subgroup(rn, adj, subgrp); | |
1042 | bgp_unlock_node(rn); | |
3f9c7369 DS |
1043 | } |
1044 | ||
d62a17ae | 1045 | if (!stream_empty(s)) { |
1046 | if (afi == AFI_IP && safi == SAFI_UNICAST | |
1047 | && !peer_cap_enhe(peer, afi, safi)) { | |
1048 | unfeasible_len = stream_get_endp(s) - BGP_HEADER_SIZE | |
1049 | - BGP_UNFEASIBLE_LEN; | |
1050 | stream_putw_at(s, BGP_HEADER_SIZE, unfeasible_len); | |
1051 | stream_putw(s, 0); | |
1052 | } else { | |
1053 | /* Set the mp_unreach attr's length */ | |
1054 | bgp_packet_mpunreach_end(s, mplen_pos); | |
1055 | ||
1056 | /* Set total path attribute length. */ | |
1057 | total_attr_len = stream_get_endp(s) - mp_start; | |
1058 | stream_putw_at(s, attrlen_pos, total_attr_len); | |
1059 | } | |
1060 | bgp_packet_set_size(s); | |
1061 | if (bgp_debug_update(NULL, NULL, subgrp->update_group, 0)) | |
1062 | zlog_debug("u%" PRIu64 ":s%" PRIu64 | |
1063 | " send UPDATE (withdraw) len %zd numpfx %d", | |
1064 | subgrp->update_group->id, subgrp->id, | |
1065 | (stream_get_endp(s) - stream_get_getp(s)), | |
1066 | num_pfx); | |
1067 | pkt = bpacket_queue_add(SUBGRP_PKTQ(subgrp), stream_dup(s), | |
1068 | NULL); | |
1069 | stream_reset(s); | |
1070 | return pkt; | |
3f9c7369 | 1071 | } |
3f9c7369 | 1072 | |
d62a17ae | 1073 | return NULL; |
3f9c7369 DS |
1074 | } |
1075 | ||
d62a17ae | 1076 | void subgroup_default_update_packet(struct update_subgroup *subgrp, |
1077 | struct attr *attr, struct peer *from) | |
3f9c7369 | 1078 | { |
d62a17ae | 1079 | struct stream *s; |
1080 | struct peer *peer; | |
1081 | struct prefix p; | |
1082 | unsigned long pos; | |
1083 | bgp_size_t total_attr_len; | |
1084 | afi_t afi; | |
1085 | safi_t safi; | |
1086 | struct bpacket_attr_vec_arr vecarr; | |
1087 | int addpath_encode = 0; | |
1088 | ||
1089 | if (DISABLE_BGP_ANNOUNCE) | |
1090 | return; | |
1091 | ||
1092 | if (!subgrp) | |
1093 | return; | |
1094 | ||
1095 | peer = SUBGRP_PEER(subgrp); | |
1096 | afi = SUBGRP_AFI(subgrp); | |
1097 | safi = SUBGRP_SAFI(subgrp); | |
1098 | bpacket_attr_vec_arr_reset(&vecarr); | |
1099 | addpath_encode = bgp_addpath_encode_tx(peer, afi, safi); | |
1100 | ||
cbb65f5e RW |
1101 | memset(&p, 0, sizeof(p)); |
1102 | p.family = afi2family(afi); | |
1103 | p.prefixlen = 0; | |
d62a17ae | 1104 | |
1105 | /* Logging the attribute. */ | |
1106 | if (bgp_debug_update(NULL, &p, subgrp->update_group, 0)) { | |
1107 | char attrstr[BUFSIZ]; | |
1108 | char buf[PREFIX_STRLEN]; | |
1109 | /* ' with addpath ID ' 17 | |
1110 | * max strlen of uint32 + 10 | |
1111 | * +/- (just in case) + 1 | |
1112 | * null terminator + 1 | |
1113 | * ============================ 29 */ | |
1114 | char tx_id_buf[30]; | |
1115 | ||
1116 | attrstr[0] = '\0'; | |
1117 | ||
1118 | bgp_dump_attr(attr, attrstr, BUFSIZ); | |
1119 | ||
1120 | if (addpath_encode) | |
1121 | snprintf(tx_id_buf, sizeof(tx_id_buf), | |
1122 | " with addpath ID %u", | |
1123 | BGP_ADDPATH_TX_ID_FOR_DEFAULT_ORIGINATE); | |
1124 | ||
1125 | zlog_debug("u%" PRIu64 ":s%" PRIu64 " send UPDATE %s%s %s", | |
1126 | (SUBGRP_UPDGRP(subgrp))->id, subgrp->id, | |
1127 | prefix2str(&p, buf, sizeof(buf)), tx_id_buf, | |
1128 | attrstr); | |
1129 | } | |
3f9c7369 | 1130 | |
d62a17ae | 1131 | s = stream_new(BGP_MAX_PACKET_SIZE); |
3f9c7369 | 1132 | |
d62a17ae | 1133 | /* Make BGP update packet. */ |
1134 | bgp_packet_set_marker(s, BGP_MSG_UPDATE); | |
3f9c7369 | 1135 | |
d62a17ae | 1136 | /* Unfeasible Routes Length. */ |
1137 | stream_putw(s, 0); | |
3f9c7369 | 1138 | |
d62a17ae | 1139 | /* Make place for total attribute length. */ |
1140 | pos = stream_get_endp(s); | |
1141 | stream_putw(s, 0); | |
1142 | total_attr_len = bgp_packet_attribute( | |
1143 | NULL, peer, s, attr, &vecarr, &p, afi, safi, from, NULL, NULL, | |
b57ba6d2 | 1144 | 0, addpath_encode, BGP_ADDPATH_TX_ID_FOR_DEFAULT_ORIGINATE); |
3f9c7369 | 1145 | |
d62a17ae | 1146 | /* Set Total Path Attribute Length. */ |
1147 | stream_putw_at(s, pos, total_attr_len); | |
3f9c7369 | 1148 | |
d62a17ae | 1149 | /* NLRI set. */ |
1150 | if (p.family == AF_INET && safi == SAFI_UNICAST | |
1151 | && !peer_cap_enhe(peer, afi, safi)) | |
1152 | stream_put_prefix_addpath( | |
1153 | s, &p, addpath_encode, | |
1154 | BGP_ADDPATH_TX_ID_FOR_DEFAULT_ORIGINATE); | |
3f9c7369 | 1155 | |
d62a17ae | 1156 | /* Set size. */ |
1157 | bgp_packet_set_size(s); | |
3f9c7369 | 1158 | |
d62a17ae | 1159 | (void)bpacket_queue_add(SUBGRP_PKTQ(subgrp), s, &vecarr); |
2fc102e1 | 1160 | subgroup_trigger_write(subgrp); |
3f9c7369 DS |
1161 | } |
1162 | ||
d62a17ae | 1163 | void subgroup_default_withdraw_packet(struct update_subgroup *subgrp) |
3f9c7369 | 1164 | { |
d62a17ae | 1165 | struct peer *peer; |
1166 | struct stream *s; | |
1167 | struct prefix p; | |
1168 | unsigned long attrlen_pos = 0; | |
1169 | unsigned long cp; | |
1170 | bgp_size_t unfeasible_len; | |
1171 | bgp_size_t total_attr_len = 0; | |
1172 | size_t mp_start = 0; | |
1173 | size_t mplen_pos = 0; | |
1174 | afi_t afi; | |
1175 | safi_t safi; | |
1176 | int addpath_encode = 0; | |
1177 | ||
1178 | if (DISABLE_BGP_ANNOUNCE) | |
1179 | return; | |
1180 | ||
1181 | peer = SUBGRP_PEER(subgrp); | |
1182 | afi = SUBGRP_AFI(subgrp); | |
1183 | safi = SUBGRP_SAFI(subgrp); | |
1184 | addpath_encode = bgp_addpath_encode_tx(peer, afi, safi); | |
1185 | ||
cbb65f5e RW |
1186 | memset(&p, 0, sizeof(p)); |
1187 | p.family = afi2family(afi); | |
1188 | p.prefixlen = 0; | |
d62a17ae | 1189 | |
1190 | if (bgp_debug_update(NULL, &p, subgrp->update_group, 0)) { | |
1191 | char buf[PREFIX_STRLEN]; | |
1192 | /* ' with addpath ID ' 17 | |
1193 | * max strlen of uint32 + 10 | |
1194 | * +/- (just in case) + 1 | |
1195 | * null terminator + 1 | |
1196 | * ============================ 29 */ | |
1197 | char tx_id_buf[30]; | |
1198 | ||
1199 | if (addpath_encode) | |
1200 | snprintf(tx_id_buf, sizeof(tx_id_buf), | |
1201 | " with addpath ID %u", | |
1202 | BGP_ADDPATH_TX_ID_FOR_DEFAULT_ORIGINATE); | |
1203 | ||
1204 | zlog_debug("u%" PRIu64 ":s%" PRIu64 | |
1205 | " send UPDATE %s%s -- unreachable", | |
1206 | (SUBGRP_UPDGRP(subgrp))->id, subgrp->id, | |
1207 | prefix2str(&p, buf, sizeof(buf)), tx_id_buf); | |
1208 | } | |
3f9c7369 | 1209 | |
d62a17ae | 1210 | s = stream_new(BGP_MAX_PACKET_SIZE); |
3f9c7369 | 1211 | |
d62a17ae | 1212 | /* Make BGP update packet. */ |
1213 | bgp_packet_set_marker(s, BGP_MSG_UPDATE); | |
3f9c7369 | 1214 | |
d62a17ae | 1215 | /* Unfeasible Routes Length. */; |
1216 | cp = stream_get_endp(s); | |
1217 | stream_putw(s, 0); | |
3f9c7369 | 1218 | |
d62a17ae | 1219 | /* Withdrawn Routes. */ |
1220 | if (p.family == AF_INET && safi == SAFI_UNICAST | |
1221 | && !peer_cap_enhe(peer, afi, safi)) { | |
1222 | stream_put_prefix_addpath( | |
1223 | s, &p, addpath_encode, | |
1224 | BGP_ADDPATH_TX_ID_FOR_DEFAULT_ORIGINATE); | |
3f9c7369 | 1225 | |
d62a17ae | 1226 | unfeasible_len = stream_get_endp(s) - cp - 2; |
3f9c7369 | 1227 | |
d62a17ae | 1228 | /* Set unfeasible len. */ |
1229 | stream_putw_at(s, cp, unfeasible_len); | |
3f9c7369 | 1230 | |
d62a17ae | 1231 | /* Set total path attribute length. */ |
1232 | stream_putw(s, 0); | |
1233 | } else { | |
1234 | attrlen_pos = stream_get_endp(s); | |
1235 | stream_putw(s, 0); | |
1236 | mp_start = stream_get_endp(s); | |
1237 | mplen_pos = bgp_packet_mpunreach_start(s, afi, safi); | |
1238 | bgp_packet_mpunreach_prefix( | |
b57ba6d2 | 1239 | s, &p, afi, safi, NULL, NULL, 0, addpath_encode, |
d62a17ae | 1240 | BGP_ADDPATH_TX_ID_FOR_DEFAULT_ORIGINATE, NULL); |
3f9c7369 | 1241 | |
d62a17ae | 1242 | /* Set the mp_unreach attr's length */ |
1243 | bgp_packet_mpunreach_end(s, mplen_pos); | |
3f9c7369 | 1244 | |
d62a17ae | 1245 | /* Set total path attribute length. */ |
1246 | total_attr_len = stream_get_endp(s) - mp_start; | |
1247 | stream_putw_at(s, attrlen_pos, total_attr_len); | |
1248 | } | |
1249 | ||
1250 | bgp_packet_set_size(s); | |
1251 | ||
1252 | (void)bpacket_queue_add(SUBGRP_PKTQ(subgrp), s, NULL); | |
2fc102e1 | 1253 | subgroup_trigger_write(subgrp); |
3f9c7369 DS |
1254 | } |
1255 | ||
1256 | static void | |
d62a17ae | 1257 | bpacket_vec_arr_inherit_attr_flags(struct bpacket_attr_vec_arr *vecarr, |
1258 | bpacket_attr_vec_type type, | |
1259 | struct attr *attr) | |
3f9c7369 | 1260 | { |
d62a17ae | 1261 | if (CHECK_FLAG(attr->rmap_change_flags, |
1262 | BATTR_RMAP_NEXTHOP_PEER_ADDRESS)) | |
1263 | SET_FLAG(vecarr->entries[BGP_ATTR_VEC_NH].flags, | |
1264 | BPKT_ATTRVEC_FLAGS_RMAP_NH_PEER_ADDRESS); | |
1265 | ||
1266 | if (CHECK_FLAG(attr->rmap_change_flags, BATTR_REFLECTED)) | |
1267 | SET_FLAG(vecarr->entries[BGP_ATTR_VEC_NH].flags, | |
1268 | BPKT_ATTRVEC_FLAGS_REFLECTED); | |
1269 | ||
1270 | if (CHECK_FLAG(attr->rmap_change_flags, BATTR_RMAP_NEXTHOP_UNCHANGED)) | |
1271 | SET_FLAG(vecarr->entries[BGP_ATTR_VEC_NH].flags, | |
1272 | BPKT_ATTRVEC_FLAGS_RMAP_NH_UNCHANGED); | |
1273 | ||
1274 | if (CHECK_FLAG(attr->rmap_change_flags, BATTR_RMAP_IPV4_NHOP_CHANGED)) | |
1275 | SET_FLAG(vecarr->entries[BGP_ATTR_VEC_NH].flags, | |
1276 | BPKT_ATTRVEC_FLAGS_RMAP_IPV4_NH_CHANGED); | |
1277 | ||
1278 | if (CHECK_FLAG(attr->rmap_change_flags, | |
1279 | BATTR_RMAP_IPV6_GLOBAL_NHOP_CHANGED)) | |
1280 | SET_FLAG(vecarr->entries[BGP_ATTR_VEC_NH].flags, | |
1281 | BPKT_ATTRVEC_FLAGS_RMAP_IPV6_GNH_CHANGED); | |
1282 | ||
1283 | if (CHECK_FLAG(attr->rmap_change_flags, | |
1284 | BATTR_RMAP_IPV6_LL_NHOP_CHANGED)) | |
1285 | SET_FLAG(vecarr->entries[BGP_ATTR_VEC_NH].flags, | |
1286 | BPKT_ATTRVEC_FLAGS_RMAP_IPV6_LNH_CHANGED); | |
3f9c7369 DS |
1287 | } |
1288 | ||
1289 | /* Reset the Attributes vector array. The vector array is used to override | |
1290 | * certain output parameters in the packet for a particular peer | |
1291 | */ | |
d62a17ae | 1292 | void bpacket_attr_vec_arr_reset(struct bpacket_attr_vec_arr *vecarr) |
3f9c7369 | 1293 | { |
d62a17ae | 1294 | int i; |
3f9c7369 | 1295 | |
d62a17ae | 1296 | if (!vecarr) |
1297 | return; | |
3f9c7369 | 1298 | |
d62a17ae | 1299 | i = 0; |
1300 | while (i < BGP_ATTR_VEC_MAX) { | |
1301 | vecarr->entries[i].flags = 0; | |
1302 | vecarr->entries[i].offset = 0; | |
1303 | i++; | |
1304 | } | |
3f9c7369 DS |
1305 | } |
1306 | ||
1307 | /* Setup a particular node entry in the vecarr */ | |
d62a17ae | 1308 | void bpacket_attr_vec_arr_set_vec(struct bpacket_attr_vec_arr *vecarr, |
1309 | bpacket_attr_vec_type type, struct stream *s, | |
1310 | struct attr *attr) | |
3f9c7369 | 1311 | { |
d62a17ae | 1312 | if (!vecarr) |
1313 | return; | |
1314 | assert(type < BGP_ATTR_VEC_MAX); | |
1315 | ||
1316 | SET_FLAG(vecarr->entries[type].flags, BPKT_ATTRVEC_FLAGS_UPDATED); | |
1317 | vecarr->entries[type].offset = stream_get_endp(s); | |
1318 | if (attr) | |
1319 | bpacket_vec_arr_inherit_attr_flags(vecarr, type, attr); | |
3f9c7369 | 1320 | } |