]>
Commit | Line | Data |
---|---|---|
7c40bf39 | 1 | /* BGP FlowSpec for packet handling |
2 | * Portions: | |
3 | * Copyright (C) 2017 ChinaTelecom SDN Group | |
4 | * Copyright (C) 2018 6WIND | |
5 | * | |
6 | * FRRouting is free software; you can redistribute it and/or modify it | |
7 | * under the terms of the GNU General Public License as published by the | |
8 | * Free Software Foundation; either version 2, or (at your option) any | |
9 | * later version. | |
10 | * | |
11 | * FRRouting is distributed in the hope that it will be useful, but | |
12 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | * General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU General Public License along | |
17 | * with this program; see the file COPYING; if not, write to the Free Software | |
18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
19 | */ | |
20 | ||
21 | #include "math.h" | |
22 | ||
23 | #include <zebra.h> | |
24 | #include "prefix.h" | |
02705213 | 25 | #include "lib_errors.h" |
7c40bf39 | 26 | |
27 | #include "bgpd/bgpd.h" | |
28 | #include "bgpd/bgp_route.h" | |
29 | #include "bgpd/bgp_flowspec.h" | |
fc836540 | 30 | #include "bgpd/bgp_flowspec_util.h" |
7c40bf39 | 31 | #include "bgpd/bgp_flowspec_private.h" |
268e1b9b PG |
32 | #include "bgpd/bgp_ecommunity.h" |
33 | #include "bgpd/bgp_debug.h" | |
4f3be667 | 34 | #include "bgpd/bgp_errors.h" |
7c40bf39 | 35 | |
fc836540 PG |
36 | static int bgp_fs_nlri_validate(uint8_t *nlri_content, uint32_t len) |
37 | { | |
38 | uint32_t offset = 0; | |
39 | int type; | |
40 | int ret = 0, error = 0; | |
41 | ||
42 | while (offset < len-1) { | |
43 | type = nlri_content[offset]; | |
44 | offset++; | |
45 | switch (type) { | |
46 | case FLOWSPEC_DEST_PREFIX: | |
47 | case FLOWSPEC_SRC_PREFIX: | |
48 | ret = bgp_flowspec_ip_address( | |
49 | BGP_FLOWSPEC_VALIDATE_ONLY, | |
50 | nlri_content + offset, | |
51 | len - offset, NULL, &error); | |
52 | break; | |
53 | case FLOWSPEC_IP_PROTOCOL: | |
54 | case FLOWSPEC_PORT: | |
55 | case FLOWSPEC_DEST_PORT: | |
56 | case FLOWSPEC_SRC_PORT: | |
57 | case FLOWSPEC_ICMP_TYPE: | |
58 | case FLOWSPEC_ICMP_CODE: | |
59 | ret = bgp_flowspec_op_decode(BGP_FLOWSPEC_VALIDATE_ONLY, | |
60 | nlri_content + offset, | |
61 | len - offset, NULL, &error); | |
62 | break; | |
63 | case FLOWSPEC_TCP_FLAGS: | |
588ec356 PG |
64 | case FLOWSPEC_FRAGMENT: |
65 | ret = bgp_flowspec_bitmask_decode( | |
fc836540 PG |
66 | BGP_FLOWSPEC_VALIDATE_ONLY, |
67 | nlri_content + offset, | |
68 | len - offset, NULL, &error); | |
69 | break; | |
70 | case FLOWSPEC_PKT_LEN: | |
71 | case FLOWSPEC_DSCP: | |
72 | ret = bgp_flowspec_op_decode( | |
73 | BGP_FLOWSPEC_VALIDATE_ONLY, | |
74 | nlri_content + offset, | |
75 | len - offset, NULL, &error); | |
76 | break; | |
fc836540 PG |
77 | default: |
78 | error = -1; | |
79 | break; | |
80 | } | |
81 | offset += ret; | |
82 | if (error < 0) | |
83 | break; | |
84 | } | |
85 | return error; | |
86 | } | |
87 | ||
7c40bf39 | 88 | int bgp_nlri_parse_flowspec(struct peer *peer, struct attr *attr, |
89 | struct bgp_nlri *packet, int withdraw) | |
90 | { | |
91 | uint8_t *pnt; | |
92 | uint8_t *lim; | |
93 | afi_t afi; | |
fc836540 | 94 | safi_t safi; |
7c40bf39 | 95 | int psize = 0; |
7c40bf39 | 96 | struct prefix p; |
fc836540 PG |
97 | int ret; |
98 | void *temp; | |
7c40bf39 | 99 | |
100 | /* Start processing the NLRI - there may be multiple in the MP_REACH */ | |
101 | pnt = packet->nlri; | |
102 | lim = pnt + packet->length; | |
103 | afi = packet->afi; | |
fc836540 | 104 | safi = packet->safi; |
7c40bf39 | 105 | |
106 | if (afi == AFI_IP6) { | |
af4c2728 | 107 | flog_err(LIB_ERR_DEVELOPMENT, |
02705213 | 108 | "BGP flowspec IPv6 not supported"); |
7c40bf39 | 109 | return -1; |
110 | } | |
111 | ||
112 | if (packet->length >= FLOWSPEC_NLRI_SIZELIMIT) { | |
af4c2728 | 113 | flog_err(BGP_ERR_FLOWSPEC_PACKET, |
4f3be667 DS |
114 | "BGP flowspec nlri length maximum reached (%u)", |
115 | packet->length); | |
7c40bf39 | 116 | return -1; |
117 | } | |
118 | ||
119 | for (; pnt < lim; pnt += psize) { | |
120 | /* Clear prefix structure. */ | |
121 | memset(&p, 0, sizeof(struct prefix)); | |
122 | ||
123 | /* All FlowSpec NLRI begin with length. */ | |
124 | if (pnt + 1 > lim) | |
125 | return -1; | |
126 | ||
3f54c705 | 127 | psize = *pnt++; |
7c40bf39 | 128 | |
129 | /* When packet overflow occur return immediately. */ | |
130 | if (pnt + psize > lim) { | |
af4c2728 | 131 | flog_err(BGP_ERR_FLOWSPEC_PACKET, |
4f3be667 DS |
132 | "Flowspec NLRI length inconsistent ( size %u seen)", |
133 | psize); | |
7c40bf39 | 134 | return -1; |
135 | } | |
fc836540 | 136 | if (bgp_fs_nlri_validate(pnt, psize) < 0) { |
af4c2728 | 137 | flog_err(BGP_ERR_FLOWSPEC_PACKET, |
4f3be667 | 138 | "Bad flowspec format or NLRI options not supported"); |
fc836540 PG |
139 | return -1; |
140 | } | |
141 | p.family = AF_FLOWSPEC; | |
142 | p.prefixlen = 0; | |
143 | /* Flowspec encoding is in bytes */ | |
144 | p.u.prefix_flowspec.prefixlen = psize; | |
145 | temp = XCALLOC(MTYPE_TMP, psize); | |
146 | memcpy(temp, pnt, psize); | |
147 | p.u.prefix_flowspec.ptr = (uintptr_t) temp; | |
268e1b9b PG |
148 | |
149 | if (BGP_DEBUG(flowspec, FLOWSPEC)) { | |
150 | char return_string[BGP_FLOWSPEC_NLRI_STRING_MAX]; | |
a2dc7057 | 151 | char local_string[BGP_FLOWSPEC_NLRI_STRING_MAX*2+16]; |
268e1b9b PG |
152 | char ec_string[BGP_FLOWSPEC_NLRI_STRING_MAX]; |
153 | char *s = NULL; | |
154 | ||
155 | bgp_fs_nlri_get_string((unsigned char *) | |
156 | p.u.prefix_flowspec.ptr, | |
157 | p.u.prefix_flowspec.prefixlen, | |
158 | return_string, | |
d33fc23b | 159 | NLRI_STRING_FORMAT_MIN, NULL); |
ff44f570 | 160 | snprintf(ec_string, sizeof(ec_string), |
268e1b9b PG |
161 | "EC{none}"); |
162 | if (attr && attr->ecommunity) { | |
163 | s = ecommunity_ecom2str(attr->ecommunity, | |
164 | ECOMMUNITY_FORMAT_ROUTE_MAP, 0); | |
ff44f570 | 165 | snprintf(ec_string, sizeof(ec_string), |
268e1b9b PG |
166 | "EC{%s}", |
167 | s == NULL ? "none" : s); | |
c7ee6c35 DS |
168 | |
169 | if (s) | |
170 | ecommunity_strfree(&s); | |
268e1b9b | 171 | } |
ff44f570 | 172 | snprintf(local_string, sizeof(local_string), |
268e1b9b PG |
173 | "FS Rx %s %s %s %s", withdraw ? |
174 | "Withdraw":"Update", | |
175 | afi2str(afi), return_string, | |
176 | attr != NULL ? ec_string : ""); | |
177 | zlog_info("%s", local_string); | |
178 | } | |
fc836540 PG |
179 | /* Process the route. */ |
180 | if (!withdraw) | |
181 | ret = bgp_update(peer, &p, 0, attr, | |
182 | afi, safi, | |
183 | ZEBRA_ROUTE_BGP, BGP_ROUTE_NORMAL, | |
184 | NULL, NULL, 0, 0, NULL); | |
185 | else | |
186 | ret = bgp_withdraw(peer, &p, 0, attr, | |
187 | afi, safi, | |
188 | ZEBRA_ROUTE_BGP, BGP_ROUTE_NORMAL, | |
189 | NULL, NULL, 0, NULL); | |
190 | if (ret) { | |
af4c2728 | 191 | flog_err(BGP_ERR_FLOWSPEC_INSTALLATION, |
4f3be667 DS |
192 | "Flowspec NLRI failed to be %s.", |
193 | attr ? "added" : "withdrawn"); | |
fc836540 PG |
194 | return -1; |
195 | } | |
7c40bf39 | 196 | } |
197 | return 0; | |
198 | } |