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