]>
Commit | Line | Data |
---|---|---|
acddc0ed | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
7c40bf39 | 2 | /* BGP FlowSpec for packet handling |
3 | * Portions: | |
4 | * Copyright (C) 2017 ChinaTelecom SDN Group | |
5 | * Copyright (C) 2018 6WIND | |
7c40bf39 | 6 | */ |
7 | ||
7c40bf39 | 8 | #include <zebra.h> |
b45ac5f5 DL |
9 | #include <math.h> |
10 | ||
7c40bf39 | 11 | #include "prefix.h" |
02705213 | 12 | #include "lib_errors.h" |
7c40bf39 | 13 | |
14 | #include "bgpd/bgpd.h" | |
15 | #include "bgpd/bgp_route.h" | |
16 | #include "bgpd/bgp_flowspec.h" | |
fc836540 | 17 | #include "bgpd/bgp_flowspec_util.h" |
7c40bf39 | 18 | #include "bgpd/bgp_flowspec_private.h" |
268e1b9b PG |
19 | #include "bgpd/bgp_ecommunity.h" |
20 | #include "bgpd/bgp_debug.h" | |
4f3be667 | 21 | #include "bgpd/bgp_errors.h" |
7c40bf39 | 22 | |
1840384b PG |
23 | static int bgp_fs_nlri_validate(uint8_t *nlri_content, uint32_t len, |
24 | afi_t afi) | |
fc836540 PG |
25 | { |
26 | uint32_t offset = 0; | |
27 | int type; | |
28 | int ret = 0, error = 0; | |
29 | ||
30 | while (offset < len-1) { | |
31 | type = nlri_content[offset]; | |
32 | offset++; | |
33 | switch (type) { | |
34 | case FLOWSPEC_DEST_PREFIX: | |
35 | case FLOWSPEC_SRC_PREFIX: | |
36 | ret = bgp_flowspec_ip_address( | |
37 | BGP_FLOWSPEC_VALIDATE_ONLY, | |
38 | nlri_content + offset, | |
1840384b | 39 | len - offset, NULL, &error, |
9cec4121 | 40 | afi, NULL); |
fc836540 | 41 | break; |
40881800 PG |
42 | case FLOWSPEC_FLOW_LABEL: |
43 | if (afi == AFI_IP) | |
44 | return -1; | |
45 | ret = bgp_flowspec_op_decode(BGP_FLOWSPEC_VALIDATE_ONLY, | |
46 | nlri_content + offset, | |
47 | len - offset, NULL, &error); | |
48 | break; | |
fc836540 PG |
49 | case FLOWSPEC_IP_PROTOCOL: |
50 | case FLOWSPEC_PORT: | |
51 | case FLOWSPEC_DEST_PORT: | |
52 | case FLOWSPEC_SRC_PORT: | |
53 | case FLOWSPEC_ICMP_TYPE: | |
54 | case FLOWSPEC_ICMP_CODE: | |
55 | ret = bgp_flowspec_op_decode(BGP_FLOWSPEC_VALIDATE_ONLY, | |
56 | nlri_content + offset, | |
57 | len - offset, NULL, &error); | |
58 | break; | |
59 | case FLOWSPEC_TCP_FLAGS: | |
588ec356 PG |
60 | case FLOWSPEC_FRAGMENT: |
61 | ret = bgp_flowspec_bitmask_decode( | |
fc836540 PG |
62 | BGP_FLOWSPEC_VALIDATE_ONLY, |
63 | nlri_content + offset, | |
64 | len - offset, NULL, &error); | |
65 | break; | |
66 | case FLOWSPEC_PKT_LEN: | |
67 | case FLOWSPEC_DSCP: | |
68 | ret = bgp_flowspec_op_decode( | |
69 | BGP_FLOWSPEC_VALIDATE_ONLY, | |
70 | nlri_content + offset, | |
71 | len - offset, NULL, &error); | |
72 | break; | |
fc836540 PG |
73 | default: |
74 | error = -1; | |
75 | break; | |
76 | } | |
77 | offset += ret; | |
78 | if (error < 0) | |
79 | break; | |
80 | } | |
81 | return error; | |
82 | } | |
83 | ||
7c40bf39 | 84 | int bgp_nlri_parse_flowspec(struct peer *peer, struct attr *attr, |
aa056a2a | 85 | struct bgp_nlri *packet, bool withdraw) |
7c40bf39 | 86 | { |
87 | uint8_t *pnt; | |
88 | uint8_t *lim; | |
89 | afi_t afi; | |
fc836540 | 90 | safi_t safi; |
7c40bf39 | 91 | int psize = 0; |
7c40bf39 | 92 | struct prefix p; |
fc836540 | 93 | void *temp; |
7c40bf39 | 94 | |
95 | /* Start processing the NLRI - there may be multiple in the MP_REACH */ | |
96 | pnt = packet->nlri; | |
97 | lim = pnt + packet->length; | |
98 | afi = packet->afi; | |
fc836540 | 99 | safi = packet->safi; |
7c40bf39 | 100 | |
cfd04dcb DS |
101 | /* |
102 | * All other AFI/SAFI's treat no attribute as a implicit | |
103 | * withdraw. Flowspec should as well. | |
104 | */ | |
105 | if (!attr) | |
aa056a2a | 106 | withdraw = true; |
cfd04dcb | 107 | |
3255e756 | 108 | if (packet->length >= FLOWSPEC_NLRI_SIZELIMIT_EXTENDED) { |
e50f7cfd | 109 | flog_err(EC_BGP_FLOWSPEC_PACKET, |
1c50c1c0 QY |
110 | "BGP flowspec nlri length maximum reached (%u)", |
111 | packet->length); | |
513386b5 | 112 | return BGP_NLRI_PARSE_ERROR_FLOWSPEC_NLRI_SIZELIMIT; |
7c40bf39 | 113 | } |
114 | ||
115 | for (; pnt < lim; pnt += psize) { | |
116 | /* Clear prefix structure. */ | |
6006b807 | 117 | memset(&p, 0, sizeof(p)); |
7c40bf39 | 118 | |
119 | /* All FlowSpec NLRI begin with length. */ | |
120 | if (pnt + 1 > lim) | |
513386b5 | 121 | return BGP_NLRI_PARSE_ERROR_PACKET_OVERFLOW; |
7c40bf39 | 122 | |
3f54c705 | 123 | psize = *pnt++; |
3255e756 PG |
124 | if (psize >= FLOWSPEC_NLRI_SIZELIMIT) { |
125 | psize &= 0x0f; | |
126 | psize = psize << 8; | |
127 | psize |= *pnt++; | |
128 | } | |
7c40bf39 | 129 | /* When packet overflow occur return immediately. */ |
130 | if (pnt + psize > lim) { | |
1c50c1c0 QY |
131 | flog_err( |
132 | EC_BGP_FLOWSPEC_PACKET, | |
133 | "Flowspec NLRI length inconsistent ( size %u seen)", | |
134 | psize); | |
513386b5 | 135 | return BGP_NLRI_PARSE_ERROR_PACKET_OVERFLOW; |
7c40bf39 | 136 | } |
0b999c88 DS |
137 | |
138 | if (psize == 0) { | |
139 | flog_err(EC_BGP_FLOWSPEC_PACKET, | |
140 | "Flowspec NLRI length 0 which makes no sense"); | |
141 | return BGP_NLRI_PARSE_ERROR_PACKET_OVERFLOW; | |
142 | } | |
143 | ||
1840384b | 144 | if (bgp_fs_nlri_validate(pnt, psize, afi) < 0) { |
1c50c1c0 QY |
145 | flog_err( |
146 | EC_BGP_FLOWSPEC_PACKET, | |
147 | "Bad flowspec format or NLRI options not supported"); | |
513386b5 | 148 | return BGP_NLRI_PARSE_ERROR_FLOWSPEC_BAD_FORMAT; |
fc836540 PG |
149 | } |
150 | p.family = AF_FLOWSPEC; | |
151 | p.prefixlen = 0; | |
152 | /* Flowspec encoding is in bytes */ | |
153 | p.u.prefix_flowspec.prefixlen = psize; | |
1840384b | 154 | p.u.prefix_flowspec.family = afi2family(afi); |
fc836540 PG |
155 | temp = XCALLOC(MTYPE_TMP, psize); |
156 | memcpy(temp, pnt, psize); | |
157 | p.u.prefix_flowspec.ptr = (uintptr_t) temp; | |
268e1b9b PG |
158 | |
159 | if (BGP_DEBUG(flowspec, FLOWSPEC)) { | |
160 | char return_string[BGP_FLOWSPEC_NLRI_STRING_MAX]; | |
a2dc7057 | 161 | char local_string[BGP_FLOWSPEC_NLRI_STRING_MAX*2+16]; |
268e1b9b PG |
162 | char ec_string[BGP_FLOWSPEC_NLRI_STRING_MAX]; |
163 | char *s = NULL; | |
164 | ||
165 | bgp_fs_nlri_get_string((unsigned char *) | |
166 | p.u.prefix_flowspec.ptr, | |
167 | p.u.prefix_flowspec.prefixlen, | |
168 | return_string, | |
1840384b PG |
169 | NLRI_STRING_FORMAT_MIN, NULL, |
170 | afi); | |
ff44f570 | 171 | snprintf(ec_string, sizeof(ec_string), |
268e1b9b | 172 | "EC{none}"); |
b53e67a3 DA |
173 | if (attr && bgp_attr_get_ecommunity(attr)) { |
174 | s = ecommunity_ecom2str( | |
175 | bgp_attr_get_ecommunity(attr), | |
176 | ECOMMUNITY_FORMAT_ROUTE_MAP, 0); | |
ff44f570 | 177 | snprintf(ec_string, sizeof(ec_string), |
268e1b9b PG |
178 | "EC{%s}", |
179 | s == NULL ? "none" : s); | |
c7ee6c35 DS |
180 | |
181 | if (s) | |
182 | ecommunity_strfree(&s); | |
268e1b9b | 183 | } |
ff44f570 | 184 | snprintf(local_string, sizeof(local_string), |
268e1b9b PG |
185 | "FS Rx %s %s %s %s", withdraw ? |
186 | "Withdraw":"Update", | |
187 | afi2str(afi), return_string, | |
188 | attr != NULL ? ec_string : ""); | |
189 | zlog_info("%s", local_string); | |
190 | } | |
fc836540 PG |
191 | /* Process the route. */ |
192 | if (!withdraw) | |
367b458c DS |
193 | bgp_update(peer, &p, 0, attr, afi, safi, |
194 | ZEBRA_ROUTE_BGP, BGP_ROUTE_NORMAL, NULL, | |
195 | NULL, 0, 0, NULL); | |
fc836540 | 196 | else |
bf0c6163 DA |
197 | bgp_withdraw(peer, &p, 0, afi, safi, ZEBRA_ROUTE_BGP, |
198 | BGP_ROUTE_NORMAL, NULL, NULL, 0, NULL); | |
7c40bf39 | 199 | } |
513386b5 | 200 | return BGP_NLRI_PARSE_OK; |
7c40bf39 | 201 | } |