]> git.ipfire.org Git - thirdparty/bird.git/blame - nest/a-set.c
BGP: Support for large communities
[thirdparty/bird.git] / nest / a-set.c
CommitLineData
c0668f36
MM
1/*
2 * BIRD -- Set/Community-list Operations
3 *
4 * (c) 2000 Martin Mares <mj@ucw.cz>
5 * (c) 2000 Pavel Machek <pavel@ucw.cz>
6 *
7 * Can be freely distributed and used under the terms of the GNU GPL.
8 */
9
10#include "nest/bird.h"
11#include "nest/route.h"
c6add07f 12#include "nest/attrs.h"
c0668f36 13#include "lib/resource.h"
c6add07f
MM
14#include "lib/string.h"
15
fdf16eb6
OZ
16/**
17 * int_set_format - format an &set for printing
18 * @set: set attribute to be formatted
19 * @way: style of format (0 for router ID list, 1 for community list)
20 * @from: starting position in set
21 * @buf: destination buffer
22 * @size: size of buffer
23 *
24 * This function takes a set attribute and formats it. @way specifies
25 * the style of format (router ID / community). @from argument can be
26 * used to specify the first printed value for the purpose of printing
27 * untruncated sets even with smaller buffers. If the output fits in
28 * the buffer, 0 is returned, otherwise the position of the first not
29 * printed item is returned. This value can be used as @from argument
30 * in subsequent calls. If truncated output suffices, -1 can be
31 * instead used as @from, in that case " ..." is eventually added at
32 * the buffer to indicate truncation.
33 */
34int
ae80a2de 35int_set_format(struct adata *set, int way, int from, byte *buf, uint size)
c6add07f
MM
36{
37 u32 *z = (u32 *) set->data;
fdf16eb6 38 byte *end = buf + size - 24;
42a0c054 39 int from2 = MAX(from, 0);
fdf16eb6
OZ
40 int to = set->length / 4;
41 int i;
c6add07f 42
42a0c054 43 for (i = from2; i < to; i++)
c6add07f 44 {
c6add07f
MM
45 if (buf > end)
46 {
fdf16eb6
OZ
47 if (from < 0)
48 strcpy(buf, " ...");
49 else
50 *buf = 0;
51 return i;
c6add07f 52 }
aebe06b4 53
42a0c054 54 if (i > from2)
fdf16eb6
OZ
55 *buf++ = ' ';
56
aebe06b4 57 if (way)
fdf16eb6 58 buf += bsprintf(buf, "(%d,%d)", z[i] >> 16, z[i] & 0xffff);
aebe06b4 59 else
fdf16eb6 60 buf += bsprintf(buf, "%R", z[i]);
c6add07f
MM
61 }
62 *buf = 0;
fdf16eb6 63 return 0;
c6add07f 64}
9c400ec9 65
42a0c054
OZ
66int
67ec_format(byte *buf, u64 ec)
68{
69 u32 type, key, val;
70 char tbuf[16], *kind;
71
72 type = ec >> 48;
73 switch (type & 0xf0ff)
74 {
75 case EC_RT: kind = "rt"; break;
76 case EC_RO: kind = "ro"; break;
77
78 default:
79 kind = tbuf;
80 bsprintf(kind, "unknown 0x%x", type);
81 }
82
83 switch (ec >> 56)
84 {
85 /* RFC 4360 3.1. Two-Octet AS Specific Extended Community */
86 case 0x00:
87 case 0x40:
88 key = (ec >> 32) & 0xFFFF;
89 val = ec;
90 return bsprintf(buf, "(%s, %u, %u)", kind, key, val);
91
92 /* RFC 4360 3.2. IPv4 Address Specific Extended Community */
93 case 0x01:
94 case 0x41:
95 key = ec >> 16;
96 val = ec & 0xFFFF;
97 return bsprintf(buf, "(%s, %R, %u)", kind, key, val);
98
99 /* RFC 5668 4-Octet AS Specific BGP Extended Community */
100 case 0x02:
101 case 0x42:
102 key = ec >> 16;
103 val = ec & 0xFFFF;
104 return bsprintf(buf, "(%s, %u, %u)", kind, key, val);
105
106 /* Generic format for unknown kinds of extended communities */
107 default:
108 key = ec >> 32;
109 val = ec;
110 return bsprintf(buf, "(generic, 0x%x, 0x%x)", key, val);
111 }
112
113}
114
115int
ae80a2de 116ec_set_format(struct adata *set, int from, byte *buf, uint size)
42a0c054
OZ
117{
118 u32 *z = int_set_get_data(set);
66dbdbd9 119 byte *end = buf + size - 64;
42a0c054
OZ
120 int from2 = MAX(from, 0);
121 int to = int_set_get_size(set);
122 int i;
123
124 for (i = from2; i < to; i += 2)
125 {
126 if (buf > end)
127 {
128 if (from < 0)
129 strcpy(buf, " ...");
130 else
131 *buf = 0;
132 return i;
133 }
134
135 if (i > from2)
136 *buf++ = ' ';
137
138 buf += ec_format(buf, ec_get(z, i));
139 }
140 *buf = 0;
141 return 0;
142}
143
66dbdbd9
OZ
144int
145lc_format(byte *buf, lcomm lc)
146{
147 return bsprintf(buf, "(%d, %d, %d)", lc.asn, lc.ldp1, lc.ldp2);
148}
149
150int
151lc_set_format(struct adata *set, int from, byte *buf, uint bufsize)
152{
153 u32 *d = (u32 *) set->data;
154 byte *end = buf + bufsize - 64;
155 int from2 = MAX(from, 0);
156 int to = set->length / 4;
157 int i;
158
159 for (i = from2; i < to; i += 3)
160 {
161 if (buf > end)
162 {
163 if (from < 0)
164 strcpy(buf, "...");
165 else
166 buf[-1] = 0;
167 return i;
168 }
169
170 buf += bsprintf(buf, "(%d, %d, %d)", d[i], d[i+1], d[i+2]);
171 *buf++ = ' ';
172 }
173
174 if (i != from2)
175 buf--;
176
177 *buf = 0;
178 return 0;
179}
180
9c400ec9
PM
181int
182int_set_contains(struct adata *list, u32 val)
183{
0267f49f
OZ
184 if (!list)
185 return 0;
186
700bbe60 187 u32 *l = (u32 *) list->data;
42a0c054
OZ
188 int len = int_set_get_size(list);
189 int i;
190
191 for (i = 0; i < len; i++)
9c400ec9
PM
192 if (*l++ == val)
193 return 1;
42a0c054
OZ
194
195 return 0;
196}
197
198int
199ec_set_contains(struct adata *list, u64 val)
200{
201 if (!list)
202 return 0;
203
204 u32 *l = int_set_get_data(list);
205 int len = int_set_get_size(list);
206 u32 eh = ec_hi(val);
207 u32 el = ec_lo(val);
208 int i;
209
210 for (i=0; i < len; i += 2)
211 if (l[i] == eh && l[i+1] == el)
212 return 1;
213
9c400ec9
PM
214 return 0;
215}
216
66dbdbd9
OZ
217int
218lc_set_contains(struct adata *list, lcomm val)
219{
220 if (!list)
221 return 0;
222
223 u32 *l = int_set_get_data(list);
224 int len = int_set_get_size(list);
225 int i;
226
227 for (i = 0; i < len; i += 3)
228 if (lc_match(l, i, val))
229 return 1;
230
231 return 0;
232}
233
234
0267f49f
OZ
235struct adata *
236int_set_add(struct linpool *pool, struct adata *list, u32 val)
237{
238 struct adata *res;
239 int len;
240
241 if (int_set_contains(list, val))
242 return list;
243
244 len = list ? list->length : 0;
42a0c054 245 res = lp_alloc(pool, sizeof(struct adata) + len + 4);
0267f49f
OZ
246 res->length = len + 4;
247 * (u32 *) res->data = val;
248 if (list)
249 memcpy((char *) res->data + 4, list->data, list->length);
250 return res;
251}
252
9c400ec9 253struct adata *
42a0c054 254ec_set_add(struct linpool *pool, struct adata *list, u64 val)
9c400ec9 255{
42a0c054
OZ
256 if (ec_set_contains(list, val))
257 return list;
258
259 int olen = list ? list->length : 0;
260 struct adata *res = lp_alloc(pool, sizeof(struct adata) + olen + 8);
261 res->length = olen + 8;
262
263 if (list)
264 memcpy(res->data, list->data, list->length);
265
66dbdbd9 266 u32 *l = (u32 *) (res->data + olen);
42a0c054
OZ
267 l[0] = ec_hi(val);
268 l[1] = ec_lo(val);
269
270 return res;
271}
272
66dbdbd9
OZ
273struct adata *
274lc_set_add(struct linpool *pool, struct adata *list, lcomm val)
275{
276 if (lc_set_contains(list, val))
277 return list;
278
279 int olen = list ? list->length : 0;
280 struct adata *res = lp_alloc(pool, sizeof(struct adata) + olen + LCOMM_LENGTH);
281 res->length = olen + LCOMM_LENGTH;
282
283 if (list)
284 memcpy(res->data, list->data, list->length);
285
286 lc_put((u32 *) (res->data + olen), val);
287
288 return res;
289}
9c400ec9 290
42a0c054
OZ
291struct adata *
292int_set_del(struct linpool *pool, struct adata *list, u32 val)
293{
9c400ec9
PM
294 if (!int_set_contains(list, val))
295 return list;
296
42a0c054
OZ
297 struct adata *res;
298 res = lp_alloc(pool, sizeof(struct adata) + list->length - 4);
299 res->length = list->length - 4;
300
301 u32 *l = int_set_get_data(list);
302 u32 *k = int_set_get_data(res);
303 int len = int_set_get_size(list);
304 int i;
9c400ec9 305
42a0c054 306 for (i = 0; i < len; i++)
9c400ec9
PM
307 if (l[i] != val)
308 *k++ = l[i];
309
310 return res;
311}
42a0c054
OZ
312
313struct adata *
314ec_set_del(struct linpool *pool, struct adata *list, u64 val)
315{
316 if (!ec_set_contains(list, val))
317 return list;
318
319 struct adata *res;
320 res = lp_alloc(pool, sizeof(struct adata) + list->length - 8);
321 res->length = list->length - 8;
322
323 u32 *l = int_set_get_data(list);
324 u32 *k = int_set_get_data(res);
325 int len = int_set_get_size(list);
326 u32 eh = ec_hi(val);
327 u32 el = ec_lo(val);
328 int i;
329
330 for (i=0; i < len; i += 2)
331 if (! (l[i] == eh && l[i+1] == el))
332 {
333 *k++ = l[i];
334 *k++ = l[i+1];
335 }
336
337 return res;
338}
0888a737 339
66dbdbd9
OZ
340struct adata *
341lc_set_del(struct linpool *pool, struct adata *list, lcomm val)
342{
343 if (!lc_set_contains(list, val))
344 return list;
345
346 struct adata *res;
347 res = lp_alloc(pool, sizeof(struct adata) + list->length - LCOMM_LENGTH);
348 res->length = list->length - LCOMM_LENGTH;
349
350 u32 *l = int_set_get_data(list);
351 u32 *k = int_set_get_data(res);
352 int len = int_set_get_size(list);
353 int i;
354
355 for (i=0; i < len; i += 3)
356 if (! lc_match(l, i, val))
357 k = lc_copy(k, l+i);
358
359 return res;
360}
0888a737
OZ
361
362struct adata *
363int_set_union(struct linpool *pool, struct adata *l1, struct adata *l2)
364{
365 if (!l1)
366 return l2;
367 if (!l2)
368 return l1;
369
370 struct adata *res;
371 int len = int_set_get_size(l2);
372 u32 *l = int_set_get_data(l2);
373 u32 tmp[len];
374 u32 *k = tmp;
375 int i;
376
377 for (i = 0; i < len; i++)
378 if (!int_set_contains(l1, l[i]))
379 *k++ = l[i];
380
381 if (k == tmp)
382 return l1;
383
384 len = (k - tmp) * 4;
385 res = lp_alloc(pool, sizeof(struct adata) + l1->length + len);
386 res->length = l1->length + len;
387 memcpy(res->data, l1->data, l1->length);
388 memcpy(res->data + l1->length, tmp, len);
389 return res;
390}
391
392struct adata *
393ec_set_union(struct linpool *pool, struct adata *l1, struct adata *l2)
394{
395 if (!l1)
396 return l2;
397 if (!l2)
398 return l1;
399
400 struct adata *res;
401 int len = int_set_get_size(l2);
402 u32 *l = int_set_get_data(l2);
403 u32 tmp[len];
404 u32 *k = tmp;
405 int i;
406
407 for (i = 0; i < len; i += 2)
408 if (!ec_set_contains(l1, ec_get(l, i)))
409 {
410 *k++ = l[i];
411 *k++ = l[i+1];
412 }
413
414 if (k == tmp)
415 return l1;
416
417 len = (k - tmp) * 4;
418 res = lp_alloc(pool, sizeof(struct adata) + l1->length + len);
419 res->length = l1->length + len;
420 memcpy(res->data, l1->data, l1->length);
421 memcpy(res->data + l1->length, tmp, len);
422 return res;
423}
66dbdbd9
OZ
424
425struct adata *
426lc_set_union(struct linpool *pool, struct adata *l1, struct adata *l2)
427{
428 if (!l1)
429 return l2;
430 if (!l2)
431 return l1;
432
433 struct adata *res;
434 int len = int_set_get_size(l2);
435 u32 *l = int_set_get_data(l2);
436 u32 tmp[len];
437 u32 *k = tmp;
438 int i;
439
440 for (i = 0; i < len; i += 3)
441 if (!lc_set_contains(l1, lc_get(l, i)))
442 k = lc_copy(k, l+i);
443
444 if (k == tmp)
445 return l1;
446
447 len = (k - tmp) * 4;
448 res = lp_alloc(pool, sizeof(struct adata) + l1->length + len);
449 res->length = l1->length + len;
450 memcpy(res->data, l1->data, l1->length);
451 memcpy(res->data + l1->length, tmp, len);
452 return res;
453}