This repository has been archived by the owner on Jul 18, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
parse.go
352 lines (320 loc) · 9.31 KB
/
parse.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
package ovs
import (
"fmt"
"strconv"
"strings"
"time"
)
// ovsFlow represents an OVS flow
type OvsFlow struct {
Table int
Priority int
Created time.Time
Cookie string
Fields []OvsField
Actions []OvsField
}
type OvsField struct {
Name string
Value string
}
const (
minPriority = 0
defaultPriority = 32768
maxPriority = 65535
)
type ParseCmd string
const (
ParseForAdd ParseCmd = "add-flow"
ParseForDelete ParseCmd = "del-flows"
ParseForDump ParseCmd = "dump-flows"
)
func fieldSet(parsed *OvsFlow, field string) bool {
for _, f := range parsed.Fields {
if f.Name == field {
return true
}
}
return false
}
func checkNotAllowedField(flow string, parsed *OvsFlow, field string, cmd ParseCmd) error {
if fieldSet(parsed, field) {
return fmt.Errorf("bad flow %q (field %q not allowed in %s)", flow, field, cmd)
}
return nil
}
func checkUnimplementedField(flow string, parsed *OvsFlow, field string) error {
if fieldSet(parsed, field) {
return fmt.Errorf("bad flow %q (field %q not implemented)", flow, field)
}
return nil
}
func actionToOvsField(action string) (*OvsField, error) {
if action == "" {
return nil, fmt.Errorf("cannot make field from empty action")
}
sep := strings.IndexAny(action, ":(")
if sep == -1 {
return &OvsField{Name: strings.TrimSpace(action)}, nil
} else if sep == len(action)-1 {
return nil, fmt.Errorf("action %q has no value", action)
}
return &OvsField{
Name: strings.TrimSpace(action[:sep]),
Value: strings.Trim(action[sep:], ": "),
}, nil
}
func parseActions(actions string) ([]OvsField, error) {
fields := make([]OvsField, 0)
var parenLevel, braceLevel, idx int
origActions := actions
for actions != "" {
token := strings.IndexAny(actions[idx:], ",([])")
if token == -1 {
if parenLevel > 0 {
return nil, fmt.Errorf("mismatched parentheses in actions %q", origActions)
} else if braceLevel > 0 {
return nil, fmt.Errorf("mismatched braces in actions %q", origActions)
}
field, err := actionToOvsField(actions)
if err != nil {
return nil, err
}
fields = append(fields, *field)
break
}
idx += token
switch actions[idx] {
case ',':
if parenLevel == 0 && braceLevel == 0 {
field, err := actionToOvsField(actions[:idx])
if err != nil {
return nil, err
}
fields = append(fields, *field)
actions = actions[idx+1:]
idx = 0
}
case '(':
parenLevel += 1
case '[':
braceLevel += 1
case ')':
parenLevel -= 1
if parenLevel < 0 {
return nil, fmt.Errorf("mismatched parentheses in actions %q", origActions)
}
case ']':
braceLevel -= 1
if braceLevel < 0 {
return nil, fmt.Errorf("mismatched braces in actions %q", origActions)
}
}
// Advance past token
idx += 1
}
return fields, nil
}
func findField(name string, fields *[]OvsField) (*OvsField, bool) {
for _, f := range *fields {
if f.Name == name {
return &f, true
}
}
return nil, false
}
func (of *OvsFlow) FindField(name string) (*OvsField, bool) {
return findField(name, &of.Fields)
}
func (of *OvsFlow) FindAction(name string) (*OvsField, bool) {
return findField(name, &of.Actions)
}
func (of *OvsFlow) NoteHasPrefix(prefix string) bool {
note, ok := of.FindAction("note")
return ok && strings.HasPrefix(strings.ToLower(note.Value), strings.ToLower(prefix))
}
func ParseFlow(cmd ParseCmd, flow string, args ...interface{}) (*OvsFlow, error) {
if len(args) > 0 {
flow = fmt.Sprintf(flow, args...)
}
// According to the man page, "flow descriptions comprise a series of field=value
// assignments, separated by commas or white space." However, you can also have
// fields with no value (eg, "ip"), and the "actions" field can also have internal
// commas, whitespace, and equals signs (but if it is present, it must be the
// last field specified).
actions := ""
parsed := &OvsFlow{
Table: 0,
Priority: defaultPriority,
Fields: make([]OvsField, 0),
Actions: make([]OvsField, 0),
Created: time.Now(),
Cookie: "0",
}
flow = strings.Trim(flow, " ")
origFlow := flow
for flow != "" {
field := ""
value := ""
end := strings.IndexAny(flow, ", ")
if end == -1 {
end = len(flow)
}
eq := strings.Index(flow, "=")
if eq == -1 || eq > end {
field = flow[:end]
} else {
field = flow[:eq]
if field == "actions" {
end = len(flow)
value = flow[eq+1:]
} else {
valueEnd := end - 1
for flow[valueEnd] == ' ' || flow[valueEnd] == ',' {
valueEnd--
}
value = strings.Trim(flow[eq+1:end], ", ")
}
if value == "" {
return nil, fmt.Errorf("bad flow definition %q (empty field %q)", origFlow, field)
}
}
switch field {
case "table":
table, err := strconv.Atoi(value)
if err != nil {
return nil, fmt.Errorf("bad flow %q (bad table number %q)", origFlow, value)
} else if table < 0 || table > 255 {
return nil, fmt.Errorf("bad flow %q (table number %q out of range)", origFlow, value)
}
parsed.Table = table
case "priority":
priority, err := strconv.Atoi(value)
if err != nil {
return nil, fmt.Errorf("bad flow %q (bad priority %q)", origFlow, value)
} else if priority < minPriority || priority > maxPriority {
return nil, fmt.Errorf("bad flow %q (priority %q out of range)", origFlow, value)
}
parsed.Priority = priority
case "actions":
actions = value
case "cookie":
parsed.Cookie = value
default:
parsed.Fields = append(parsed.Fields, OvsField{field, value})
}
for end < len(flow) && (flow[end] == ',' || flow[end] == ' ') {
end++
}
flow = flow[end:]
}
if actions != "" {
var err error
parsed.Actions, err = parseActions(actions)
if err != nil {
return nil, err
}
}
// Sanity-checking
switch cmd {
case ParseForDump:
fallthrough
case ParseForAdd:
if err := checkNotAllowedField(flow, parsed, "out_port", cmd); err != nil {
return nil, err
}
if err := checkNotAllowedField(flow, parsed, "out_group", cmd); err != nil {
return nil, err
}
if len(parsed.Actions) == 0 {
return nil, fmt.Errorf("bad flow %q (empty actions)", flow)
}
case ParseForDelete:
if err := checkNotAllowedField(flow, parsed, "priority", cmd); err != nil {
return nil, err
}
if err := checkNotAllowedField(flow, parsed, "actions", cmd); err != nil {
return nil, err
}
if err := checkUnimplementedField(flow, parsed, "out_port"); err != nil {
return nil, err
}
if err := checkUnimplementedField(flow, parsed, "out_group"); err != nil {
return nil, err
}
}
if (fieldSet(parsed, "nw_src") || fieldSet(parsed, "nw_dst")) &&
!(fieldSet(parsed, "ip") || fieldSet(parsed, "arp") || fieldSet(parsed, "tcp") || fieldSet(parsed, "udp")) {
return nil, fmt.Errorf("bad flow %q (specified nw_src/nw_dst without ip/arp/tcp/udp)", flow)
}
if (fieldSet(parsed, "arp_spa") || fieldSet(parsed, "arp_tpa") || fieldSet(parsed, "arp_sha") || fieldSet(parsed, "arp_tha")) && !fieldSet(parsed, "arp") {
return nil, fmt.Errorf("bad flow %q (specified arp_spa/arp_tpa/arp_sha/arp_tpa without arp)", flow)
}
if (fieldSet(parsed, "tcp_src") || fieldSet(parsed, "tcp_dst")) && !fieldSet(parsed, "tcp") {
return nil, fmt.Errorf("bad flow %q (specified tcp_src/tcp_dst without tcp)", flow)
}
if (fieldSet(parsed, "udp_src") || fieldSet(parsed, "udp_dst")) && !fieldSet(parsed, "udp") {
return nil, fmt.Errorf("bad flow %q (specified udp_src/udp_dst without udp)", flow)
}
if (fieldSet(parsed, "tp_src") || fieldSet(parsed, "tp_dst")) && !(fieldSet(parsed, "tcp") || fieldSet(parsed, "udp")) {
return nil, fmt.Errorf("bad flow %q (specified tp_src/tp_dst without tcp/udp)", flow)
}
if fieldSet(parsed, "ip_frag") && (fieldSet(parsed, "tcp") || fieldSet(parsed, "udp")) {
return nil, fmt.Errorf("bad flow %q (specified ip_frag with tcp/udp)", flow)
}
return parsed, nil
}
// flowMatches tests if flow matches match. If exact is true, then the table, priority,
// and all fields much match. If exact is false, then the table and any fields specified
// in match must match, but priority is not checked, and there can be additional fields
// in flow that are not in match.
func FlowMatches(flow, match *OvsFlow, exact bool) bool {
if flow.Table != match.Table && (exact || match.Table != 0) {
return false
}
if exact && flow.Priority != match.Priority {
return false
}
if exact && len(flow.Fields) != len(match.Fields) {
return false
}
if match.Cookie != "" && !fieldMatches(flow.Cookie, match.Cookie, exact) {
return false
}
for _, matchField := range match.Fields {
var flowValue *string
for _, flowField := range flow.Fields {
if flowField.Name == matchField.Name {
flowValue = &flowField.Value
break
}
}
if flowValue == nil || !fieldMatches(*flowValue, matchField.Value, exact) {
return false
}
}
return true
}
func fieldMatches(val, match string, exact bool) bool {
if val == match {
return true
}
if exact {
return false
}
// Handle bitfield/mask matches. (Some other syntax like "10.128.0.0/14" might
// get examined here, but it will fail the first ParseUint call and so not
// reach the final check.)
split := strings.Split(match, "/")
if len(split) == 2 {
matchNum, err1 := strconv.ParseUint(split[0], 0, 32)
mask, err2 := strconv.ParseUint(split[1], 0, 32)
valNum, err3 := strconv.ParseUint(val, 0, 32)
if err1 == nil && err2 == nil && err3 == nil {
if (matchNum & mask) == (valNum & mask) {
return true
}
}
}
return false
}