]> git.ipfire.org Git - thirdparty/bird.git/blame - lib/flowspec.h
Minor cleanups and fixes
[thirdparty/bird.git] / lib / flowspec.h
CommitLineData
77234bbb
OZ
1/*
2 * BIRD Library -- Flow specification (RFC 5575)
3 *
4 * (c) 2016 CZ.NIC z.s.p.o.
5 *
6 * Can be freely distributed and used under the terms of the GNU GPL.
7 */
8
9#ifndef _BIRD_FLOWSPEC_H_
10#define _BIRD_FLOWSPEC_H_
11
12#include "nest/bird.h"
13#include "lib/buffer.h"
14#include "lib/net.h"
15
16
734e9fb8
OZ
17/* Flow component operators */
18#define FLOW_OP_TRUE 0x00 /* 0b000 */
19#define FLOW_OP_EQ 0x01 /* 0b001 */
20#define FLOW_OP_GT 0x02 /* 0b010 */
21#define FLOW_OP_GEQ 0x03 /* 0b011 */
22#define FLOW_OP_LT 0x04 /* 0b100 */
23#define FLOW_OP_LEQ 0x05 /* 0b101 */
24#define FLOW_OP_NEQ 0x06 /* 0b110 */
25#define FLOW_OP_FALSE 0x07 /* 0b111 */
26
27#define FLOW_OP_OR 0x00
28#define FLOW_OP_AND 0x40
29
30
77234bbb
OZ
31/* Types of components in flowspec */
32enum flow_type {
33 FLOW_TYPE_DST_PREFIX = 1,
34 FLOW_TYPE_SRC_PREFIX = 2,
35 FLOW_TYPE_IP_PROTOCOL = 3,
36 FLOW_TYPE_NEXT_HEADER = 3, /* IPv6 */
37 FLOW_TYPE_PORT = 4,
38 FLOW_TYPE_DST_PORT = 5,
39 FLOW_TYPE_SRC_PORT = 6,
40 FLOW_TYPE_ICMP_TYPE = 7,
41 FLOW_TYPE_ICMP_CODE = 8,
42 FLOW_TYPE_TCP_FLAGS = 9,
43 FLOW_TYPE_PACKET_LENGTH = 10,
44 FLOW_TYPE_DSCP = 11, /* DiffServ Code Point */
45 FLOW_TYPE_FRAGMENT = 12,
46 FLOW_TYPE_LABEL = 13, /* IPv6 */
47 FLOW_TYPE_MAX
48};
49
50const char *flow_type_str(enum flow_type type, int ipv6);
51
52
53/*
54 * Length
55 */
56
57uint flow_write_length(byte *data, u16 len);
58
ac3ad139
OZ
59static inline u16 flow_hdr_length(const byte *data)
60{ return ((*data & 0xf0) == 0xf0) ? 2 : 1; }
61
77234bbb
OZ
62static inline u16 flow_read_length(const byte *data)
63{ return ((*data & 0xf0) == 0xf0) ? get_u16(data) & 0x0fff : *data; }
64
65static inline u16 flow4_get_length(const net_addr_flow4 *f)
66{ return f->length - sizeof(net_addr_flow4); }
67
68static inline u16 flow6_get_length(const net_addr_flow6 *f)
69{ return f->length - sizeof(net_addr_flow6); }
70
71static inline void flow4_set_length(net_addr_flow4 *f, u16 len)
72{ f->length = sizeof(net_addr_flow4) + flow_write_length(f->data, len) + len; }
73
74static inline void flow6_set_length(net_addr_flow6 *f, u16 len)
75{ f->length = sizeof(net_addr_flow6) + flow_write_length(f->data, len) + len; }
76
77
78/*
79 * Iterators
80 */
81
82const byte *flow4_first_part(const net_addr_flow4 *f);
83const byte *flow6_first_part(const net_addr_flow6 *f);
84const byte *flow4_next_part(const byte *pos, const byte *end);
85const byte *flow6_next_part(const byte *pos, const byte *end);
86
87
88/*
89 * Flowspec Builder
90 */
91
92/* A data structure for keep a state of flow builder */
93struct flow_builder {
94 BUFFER(byte) data;
95 enum flow_type this_type;
96 enum flow_type last_type;
97 u16 last_op_offset; /* Position of last operator in data.data */
98 int ipv6;
99 struct {
100 u16 offset; /* Beginning of a component */
101 u16 length; /* Length of a component */
102 } parts[FLOW_TYPE_MAX]; /* Indexing all components */
103};
104
105struct flow_builder *flow_builder_init(pool *pool);
106void flow_builder_clear(struct flow_builder *fb);
107void flow_builder_set_type(struct flow_builder *fb, enum flow_type p);
108int flow_builder4_add_pfx(struct flow_builder *fb, const net_addr_ip4 *n4);
109int flow_builder6_add_pfx(struct flow_builder *fb, const net_addr_ip6 *n6, u32 offset);
110int flow_builder_add_op_val(struct flow_builder *fb, byte op, u32 value);
111int flow_builder_add_val_mask(struct flow_builder *fb, byte op, u32 value, u32 mask);
112net_addr_flow4 *flow_builder4_finalize(struct flow_builder *fb, linpool *lpool);
113net_addr_flow6 *flow_builder6_finalize(struct flow_builder *fb, linpool *lpool);
114
115
116/*
117 * Validation
118 */
119
120/* Results of validation Flow specification */
121enum flow_validated_state {
122 FLOW_ST_UNKNOWN_COMPONENT,
123 FLOW_ST_VALID,
124 FLOW_ST_NOT_COMPLETE,
125 FLOW_ST_EXCEED_MAX_PREFIX_LENGTH,
126 FLOW_ST_EXCEED_MAX_PREFIX_OFFSET,
127 FLOW_ST_EXCEED_MAX_VALUE_LENGTH,
128 FLOW_ST_BAD_TYPE_ORDER,
129 FLOW_ST_AND_BIT_SHOULD_BE_UNSET,
130 FLOW_ST_ZERO_BIT_SHOULD_BE_UNSED,
131 FLOW_ST_DEST_PREFIX_REQUIRED,
5ca4bd5d 132 FLOW_ST_INVALID_TCP_FLAGS,
77234bbb
OZ
133 FLOW_ST_CANNOT_USE_DONT_FRAGMENT
134};
135
136const char *flow_validated_state_str(enum flow_validated_state code);
137enum flow_validated_state flow4_validate(const byte *nlri, uint len);
138enum flow_validated_state flow6_validate(const byte *nlri, uint len);
139void flow_check_cf_value_length(struct flow_builder *fb, u32 expr);
140void flow_check_cf_bmk_values(struct flow_builder *fb, u8 neg, u32 val, u32 mask);
141void flow4_validate_cf(net_addr_flow4 *f);
142void flow6_validate_cf(net_addr_flow6 *f);
143
144
145/*
146 * Net Formatting
147 */
148
eeba61cc
OZ
149uint flow4_net_format(char *buf, uint blen, const net_addr_flow4 *f);
150uint flow6_net_format(char *buf, uint blen, const net_addr_flow6 *f);
77234bbb
OZ
151
152#endif /* _BIRD_FLOWSPEC_H_ */