]>
Commit | Line | Data |
---|---|---|
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 | ||
17 | /* Types of components in flowspec */ | |
18 | enum flow_type { | |
19 | FLOW_TYPE_DST_PREFIX = 1, | |
20 | FLOW_TYPE_SRC_PREFIX = 2, | |
21 | FLOW_TYPE_IP_PROTOCOL = 3, | |
22 | FLOW_TYPE_NEXT_HEADER = 3, /* IPv6 */ | |
23 | FLOW_TYPE_PORT = 4, | |
24 | FLOW_TYPE_DST_PORT = 5, | |
25 | FLOW_TYPE_SRC_PORT = 6, | |
26 | FLOW_TYPE_ICMP_TYPE = 7, | |
27 | FLOW_TYPE_ICMP_CODE = 8, | |
28 | FLOW_TYPE_TCP_FLAGS = 9, | |
29 | FLOW_TYPE_PACKET_LENGTH = 10, | |
30 | FLOW_TYPE_DSCP = 11, /* DiffServ Code Point */ | |
31 | FLOW_TYPE_FRAGMENT = 12, | |
32 | FLOW_TYPE_LABEL = 13, /* IPv6 */ | |
33 | FLOW_TYPE_MAX | |
34 | }; | |
35 | ||
36 | const char *flow_type_str(enum flow_type type, int ipv6); | |
37 | ||
38 | ||
39 | /* | |
40 | * Length | |
41 | */ | |
42 | ||
43 | uint flow_write_length(byte *data, u16 len); | |
44 | ||
45 | static inline u16 flow_read_length(const byte *data) | |
46 | { return ((*data & 0xf0) == 0xf0) ? get_u16(data) & 0x0fff : *data; } | |
47 | ||
48 | static inline u16 flow4_get_length(const net_addr_flow4 *f) | |
49 | { return f->length - sizeof(net_addr_flow4); } | |
50 | ||
51 | static inline u16 flow6_get_length(const net_addr_flow6 *f) | |
52 | { return f->length - sizeof(net_addr_flow6); } | |
53 | ||
54 | static inline void flow4_set_length(net_addr_flow4 *f, u16 len) | |
55 | { f->length = sizeof(net_addr_flow4) + flow_write_length(f->data, len) + len; } | |
56 | ||
57 | static inline void flow6_set_length(net_addr_flow6 *f, u16 len) | |
58 | { f->length = sizeof(net_addr_flow6) + flow_write_length(f->data, len) + len; } | |
59 | ||
60 | ||
61 | /* | |
62 | * Iterators | |
63 | */ | |
64 | ||
65 | const byte *flow4_first_part(const net_addr_flow4 *f); | |
66 | const byte *flow6_first_part(const net_addr_flow6 *f); | |
67 | const byte *flow4_next_part(const byte *pos, const byte *end); | |
68 | const byte *flow6_next_part(const byte *pos, const byte *end); | |
69 | ||
70 | ||
71 | /* | |
72 | * Flowspec Builder | |
73 | */ | |
74 | ||
75 | /* A data structure for keep a state of flow builder */ | |
76 | struct flow_builder { | |
77 | BUFFER(byte) data; | |
78 | enum flow_type this_type; | |
79 | enum flow_type last_type; | |
80 | u16 last_op_offset; /* Position of last operator in data.data */ | |
81 | int ipv6; | |
82 | struct { | |
83 | u16 offset; /* Beginning of a component */ | |
84 | u16 length; /* Length of a component */ | |
85 | } parts[FLOW_TYPE_MAX]; /* Indexing all components */ | |
86 | }; | |
87 | ||
88 | struct flow_builder *flow_builder_init(pool *pool); | |
89 | void flow_builder_clear(struct flow_builder *fb); | |
90 | void flow_builder_set_type(struct flow_builder *fb, enum flow_type p); | |
91 | int flow_builder4_add_pfx(struct flow_builder *fb, const net_addr_ip4 *n4); | |
92 | int flow_builder6_add_pfx(struct flow_builder *fb, const net_addr_ip6 *n6, u32 offset); | |
93 | int flow_builder_add_op_val(struct flow_builder *fb, byte op, u32 value); | |
94 | int flow_builder_add_val_mask(struct flow_builder *fb, byte op, u32 value, u32 mask); | |
95 | net_addr_flow4 *flow_builder4_finalize(struct flow_builder *fb, linpool *lpool); | |
96 | net_addr_flow6 *flow_builder6_finalize(struct flow_builder *fb, linpool *lpool); | |
97 | ||
98 | ||
99 | /* | |
100 | * Validation | |
101 | */ | |
102 | ||
103 | /* Results of validation Flow specification */ | |
104 | enum flow_validated_state { | |
105 | FLOW_ST_UNKNOWN_COMPONENT, | |
106 | FLOW_ST_VALID, | |
107 | FLOW_ST_NOT_COMPLETE, | |
108 | FLOW_ST_EXCEED_MAX_PREFIX_LENGTH, | |
109 | FLOW_ST_EXCEED_MAX_PREFIX_OFFSET, | |
110 | FLOW_ST_EXCEED_MAX_VALUE_LENGTH, | |
111 | FLOW_ST_BAD_TYPE_ORDER, | |
112 | FLOW_ST_AND_BIT_SHOULD_BE_UNSET, | |
113 | FLOW_ST_ZERO_BIT_SHOULD_BE_UNSED, | |
114 | FLOW_ST_DEST_PREFIX_REQUIRED, | |
115 | FLOW_ST_CANNOT_USE_DONT_FRAGMENT | |
116 | }; | |
117 | ||
118 | const char *flow_validated_state_str(enum flow_validated_state code); | |
119 | enum flow_validated_state flow4_validate(const byte *nlri, uint len); | |
120 | enum flow_validated_state flow6_validate(const byte *nlri, uint len); | |
121 | void flow_check_cf_value_length(struct flow_builder *fb, u32 expr); | |
122 | void flow_check_cf_bmk_values(struct flow_builder *fb, u8 neg, u32 val, u32 mask); | |
123 | void flow4_validate_cf(net_addr_flow4 *f); | |
124 | void flow6_validate_cf(net_addr_flow6 *f); | |
125 | ||
126 | ||
127 | /* | |
128 | * Net Formatting | |
129 | */ | |
130 | ||
131 | int flow4_net_format(char *buf, uint blen, const net_addr_flow4 *f); | |
132 | int flow6_net_format(char *buf, uint blen, const net_addr_flow6 *f); | |
133 | ||
134 | #endif /* _BIRD_FLOWSPEC_H_ */ |