| 1 | /* |
| 2 | * This file is part of the go-nft project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | * |
| 16 | * Copyright 2021 Red Hat, Inc. |
| 17 | * |
| 18 | */ |
| 19 | |
| 20 | package schema |
| 21 | |
| 22 | import ( |
| 23 | "encoding/json" |
| 24 | "fmt" |
| 25 | ) |
| 26 | |
| 27 | type Rule struct { |
| 28 | Family string `json:"family"` |
| 29 | Table string `json:"table"` |
| 30 | Chain string `json:"chain"` |
| 31 | Expr []Statement `json:"expr,omitempty"` |
| 32 | Handle *int `json:"handle,omitempty"` |
| 33 | Index *int `json:"index,omitempty"` |
| 34 | Comment string `json:"comment,omitempty"` |
| 35 | } |
| 36 | |
| 37 | type Statement struct { |
| 38 | Counter *Counter `json:"counter,omitempty"` |
| 39 | Match *Match `json:"match,omitempty"` |
| 40 | Verdict |
| 41 | Nat |
| 42 | } |
| 43 | |
| 44 | type Counter struct { |
| 45 | Packets int `json:"packets"` |
| 46 | Bytes int `json:"bytes"` |
| 47 | } |
| 48 | |
| 49 | type Nat struct { |
| 50 | Snat *Snat `json:"snat,omitempty"` |
| 51 | Dnat *Dnat `json:"dnat,omitempty"` |
| 52 | Masquerade *Masquerade `json:"masquerade,omitempty"` |
| 53 | Redirect *Redirect `json:"redirect,omitempty"` |
| 54 | } |
| 55 | |
| 56 | type Snat struct { |
| 57 | Addr *Expression `json:"addr,omitempty"` |
| 58 | Family *string `json:"family,omitempty"` |
| 59 | Port *Expression `json:"port,omitempty"` |
| 60 | Flags *Flags `json:"flags,omitempty"` |
| 61 | } |
| 62 | |
| 63 | type Dnat struct { |
| 64 | Addr *Expression `json:"addr,omitempty"` |
| 65 | Family *string `json:"family,omitempty"` |
| 66 | Port *Expression `json:"port,omitempty"` |
| 67 | Flags *Flags `json:"flags,omitempty"` |
| 68 | } |
| 69 | |
| 70 | const masquerade = "masquerade" |
| 71 | |
| 72 | type Masquerade struct { |
| 73 | Enabled bool `json:"-"` |
| 74 | Port *Expression `json:"port,omitempty"` |
| 75 | Flags *Flags `json:"flags,omitempty"` |
| 76 | } |
| 77 | |
| 78 | const redirect = "redirect" |
| 79 | |
| 80 | type Redirect struct { |
| 81 | Enabled bool `json:"-"` |
| 82 | Port *Expression `json:"port,omitempty"` |
| 83 | Flags *Flags `json:"flags,omitempty"` |
| 84 | } |
| 85 | |
| 86 | type Flags struct { |
| 87 | Flags []string `json:"-"` |
| 88 | } |
| 89 | |
| 90 | // NAT Flags |
| 91 | const ( |
| 92 | NATFlagRandom = "random" |
| 93 | NATFlagFullyRandom = "fully-random" |
| 94 | NATFlagPersistent = "persistent" |
| 95 | ) |
| 96 | |
| 97 | type Verdict struct { |
| 98 | SimpleVerdict |
| 99 | Jump *ToTarget `json:"jump,omitempty"` |
| 100 | Goto *ToTarget `json:"goto,omitempty"` |
| 101 | } |
| 102 | |
| 103 | type SimpleVerdict struct { |
| 104 | Accept bool `json:"-"` |
| 105 | Continue bool `json:"-"` |
| 106 | Drop bool `json:"-"` |
| 107 | Return bool `json:"-"` |
| 108 | } |
| 109 | |
| 110 | type ToTarget struct { |
| 111 | Target string `json:"target"` |
| 112 | } |
| 113 | |
| 114 | type Match struct { |
| 115 | Op string `json:"op"` |
| 116 | Left Expression `json:"left"` |
| 117 | Right Expression `json:"right"` |
| 118 | } |
| 119 | |
| 120 | type Expression struct { |
| 121 | String *string `json:"-"` |
| 122 | Bool *bool `json:"-"` |
| 123 | Float64 *float64 `json:"-"` |
| 124 | Payload *Payload `json:"payload,omitempty"` |
| 125 | // RowData accepts arbitrary data which cannot be composed from the existing schema. |
| 126 | // Use `json.RawMessage()` or `[]byte()` for the value. |
| 127 | // Example: |
| 128 | // `schema.Expression{RowData: json.RawMessage(`{"meta":{"key":"iifname"}}`)}` |
| 129 | RowData json.RawMessage `json:"-"` |
| 130 | } |
| 131 | |
| 132 | type Payload struct { |
| 133 | Protocol string `json:"protocol"` |
| 134 | Field string `json:"field"` |
| 135 | } |
| 136 | |
| 137 | // Verdict Operations |
| 138 | const ( |
| 139 | VerdictAccept = "accept" |
| 140 | VerdictContinue = "continue" |
| 141 | VerdictDrop = "drop" |
| 142 | VerdictReturn = "return" |
| 143 | ) |
| 144 | |
| 145 | // Match Operators |
| 146 | const ( |
| 147 | OperAND = "&" // Binary AND |
| 148 | OperOR = "|" // Binary OR |
| 149 | OperXOR = "^" // Binary XOR |
| 150 | OperLSH = "<<" // Left shift |
| 151 | OperRSH = ">>" // Right shift |
| 152 | OperEQ = "==" // Equal |
| 153 | OperNEQ = "!=" // Not equal |
| 154 | OperLS = "<" // Less than |
| 155 | OperGR = ">" // Greater than |
| 156 | OperLSE = "<=" // Less than or equal to |
| 157 | OperGRE = ">=" // Greater than or equal to |
| 158 | OperIN = "in" // Perform a lookup, i.e. test if bits on RHS are contained in LHS value |
| 159 | ) |
| 160 | |
| 161 | // Payload Expressions |
| 162 | const ( |
| 163 | PayloadKey = "payload" |
| 164 | // Ethernet |
| 165 | PayloadProtocolEther = "ether" |
| 166 | PayloadFieldEtherDAddr = "daddr" |
| 167 | PayloadFieldEtherSAddr = "saddr" |
| 168 | PayloadFieldEtherType = "type" |
| 169 | |
| 170 | // IP (common) |
| 171 | PayloadFieldIPVer = "version" |
| 172 | PayloadFieldIPDscp = "dscp" |
| 173 | PayloadFieldIPEcn = "ecn" |
| 174 | PayloadFieldIPLen = "length" |
| 175 | PayloadFieldIPSAddr = "saddr" |
| 176 | PayloadFieldIPDAddr = "daddr" |
| 177 | |
| 178 | // IPv4 |
| 179 | PayloadProtocolIP4 = "ip" |
| 180 | PayloadFieldIP4HdrLen = "hdrlength" |
| 181 | PayloadFieldIP4Id = "id" |
| 182 | PayloadFieldIP4FragOff = "frag-off" |
| 183 | PayloadFieldIP4Ttl = "ttl" |
| 184 | PayloadFieldIP4Protocol = "protocol" |
| 185 | PayloadFieldIP4Chksum = "checksum" |
| 186 | |
| 187 | // IPv6 |
| 188 | PayloadProtocolIP6 = "ip6" |
| 189 | PayloadFieldIP6FlowLabel = "flowlabel" |
| 190 | PayloadFieldIP6NextHdr = "nexthdr" |
| 191 | PayloadFieldIP6HopLimit = "hoplimit" |
| 192 | ) |
| 193 | |
| 194 | func (s Statement) MarshalJSON() ([]byte, error) { |
| 195 | type _Statement Statement |
| 196 | statement := _Statement(s) |
| 197 | |
| 198 | // Convert to a dynamic structure |
| 199 | data, err := json.Marshal(statement) |
| 200 | if err != nil { |
| 201 | return nil, err |
| 202 | } |
| 203 | dynamicStructure := make(map[string]json.RawMessage) |
| 204 | if err := json.Unmarshal(data, &dynamicStructure); err != nil { |
| 205 | return nil, err |
| 206 | } |
| 207 | |
| 208 | switch { |
| 209 | case s.Accept: |
| 210 | dynamicStructure[VerdictAccept] = nil |
| 211 | case s.Continue: |
| 212 | dynamicStructure[VerdictContinue] = nil |
| 213 | case s.Drop: |
| 214 | dynamicStructure[VerdictDrop] = nil |
| 215 | case s.Return: |
| 216 | dynamicStructure[VerdictReturn] = nil |
| 217 | case s.Masquerade != nil && s.Masquerade.Enabled && s.Masquerade.Port == nil && s.Masquerade.Flags == nil: |
| 218 | dynamicStructure[masquerade] = nil |
| 219 | case s.Redirect != nil && s.Redirect.Enabled && s.Redirect.Port == nil && s.Redirect.Flags == nil: |
| 220 | dynamicStructure[redirect] = nil |
| 221 | } |
| 222 | |
| 223 | data, err = json.Marshal(dynamicStructure) |
| 224 | if err != nil { |
| 225 | return nil, err |
| 226 | } |
| 227 | return data, nil |
| 228 | } |
| 229 | |
| 230 | func (s *Statement) UnmarshalJSON(data []byte) error { |
| 231 | type _Statement Statement |
| 232 | statement := _Statement{} |
| 233 | |
| 234 | if err := json.Unmarshal(data, &statement); err != nil { |
| 235 | return err |
| 236 | } |
| 237 | *s = Statement(statement) |
| 238 | |
| 239 | dynamicStructure := make(map[string]json.RawMessage) |
| 240 | if err := json.Unmarshal(data, &dynamicStructure); err != nil { |
| 241 | return err |
| 242 | } |
| 243 | _, s.Accept = dynamicStructure[VerdictAccept] |
| 244 | _, s.Continue = dynamicStructure[VerdictContinue] |
| 245 | _, s.Drop = dynamicStructure[VerdictDrop] |
| 246 | _, s.Return = dynamicStructure[VerdictReturn] |
| 247 | |
| 248 | if _, masqueradeDefined := dynamicStructure[masquerade]; s.Masquerade == nil && masqueradeDefined { |
| 249 | s.Masquerade = &Masquerade{Enabled: true} |
| 250 | } |
| 251 | |
| 252 | if _, redirectDefined := dynamicStructure[redirect]; s.Redirect == nil && redirectDefined { |
| 253 | s.Redirect = &Redirect{Enabled: true} |
| 254 | } |
| 255 | |
| 256 | return nil |
| 257 | } |
| 258 | |
| 259 | func (e Expression) MarshalJSON() ([]byte, error) { |
| 260 | var dynamicStruct interface{} |
| 261 | |
| 262 | switch { |
| 263 | case e.RowData != nil: |
| 264 | return e.RowData, nil |
| 265 | case e.String != nil: |
| 266 | dynamicStruct = *e.String |
| 267 | case e.Float64 != nil: |
| 268 | dynamicStruct = *e.Float64 |
| 269 | case e.Bool != nil: |
| 270 | dynamicStruct = *e.Bool |
| 271 | default: |
| 272 | type _Expression Expression |
| 273 | dynamicStruct = _Expression(e) |
| 274 | } |
| 275 | |
| 276 | return json.Marshal(dynamicStruct) |
| 277 | } |
| 278 | |
| 279 | func (e *Expression) UnmarshalJSON(data []byte) error { |
| 280 | var dynamicStruct interface{} |
| 281 | if err := json.Unmarshal(data, &dynamicStruct); err != nil { |
| 282 | return err |
| 283 | } |
| 284 | |
| 285 | switch dynamicStruct.(type) { |
| 286 | case string: |
| 287 | d := dynamicStruct.(string) |
| 288 | e.String = &d |
| 289 | case float64: |
| 290 | d := dynamicStruct.(float64) |
| 291 | e.Float64 = &d |
| 292 | case bool: |
| 293 | d := dynamicStruct.(bool) |
| 294 | e.Bool = &d |
| 295 | case []interface{}: |
| 296 | e.RowData = data |
| 297 | case map[string]interface{}: |
| 298 | type _Expression Expression |
| 299 | expression := _Expression(*e) |
| 300 | if err := json.Unmarshal(data, &expression); err != nil { |
| 301 | return err |
| 302 | } |
| 303 | *e = Expression(expression) |
| 304 | default: |
| 305 | return fmt.Errorf("unsupported field type in expression: %T(%v)", dynamicStruct, dynamicStruct) |
| 306 | } |
| 307 | |
| 308 | if e.String == nil && e.Float64 == nil && e.Bool == nil && e.Payload == nil { |
| 309 | e.RowData = data |
| 310 | } |
| 311 | |
| 312 | return nil |
| 313 | } |
| 314 | |
| 315 | func (f Flags) MarshalJSON() ([]byte, error) { |
| 316 | var dynamicStruct interface{} |
| 317 | |
| 318 | switch flagCount := len(f.Flags); { |
| 319 | case flagCount == 1: |
| 320 | dynamicStruct = f.Flags[0] |
| 321 | case flagCount > 1: |
| 322 | dynamicStruct = f.Flags |
| 323 | } |
| 324 | |
| 325 | return json.Marshal(dynamicStruct) |
| 326 | } |
| 327 | |
| 328 | func (f *Flags) UnmarshalJSON(data []byte) error { |
| 329 | var dynamicStruct interface{} |
| 330 | if err := json.Unmarshal(data, &dynamicStruct); err != nil { |
| 331 | return err |
| 332 | } |
| 333 | |
| 334 | switch v := dynamicStruct.(type) { |
| 335 | case string: |
| 336 | f.Flags = []string{v} |
| 337 | case []interface{}: |
| 338 | for _, val := range v { |
| 339 | stringVal, ok := val.(string) |
| 340 | if !ok { |
| 341 | return fmt.Errorf("flags values require string type: %T(%v)", dynamicStruct, dynamicStruct) |
| 342 | } |
| 343 | f.Flags = append(f.Flags, stringVal) |
| 344 | } |
| 345 | default: |
| 346 | return fmt.Errorf("flags values require string type: %T(%v)", dynamicStruct, dynamicStruct) |
| 347 | } |
| 348 | |
| 349 | return nil |
| 350 | } |
| 351 | |
| 352 | func Accept() Verdict { |
| 353 | return Verdict{SimpleVerdict: SimpleVerdict{Accept: true}} |
| 354 | } |
| 355 | |
| 356 | func Continue() Verdict { |
| 357 | return Verdict{SimpleVerdict: SimpleVerdict{Continue: true}} |
| 358 | } |
| 359 | |
| 360 | func Drop() Verdict { |
| 361 | return Verdict{SimpleVerdict: SimpleVerdict{Drop: true}} |
| 362 | } |
| 363 | |
| 364 | func Return() Verdict { |
| 365 | return Verdict{SimpleVerdict: SimpleVerdict{Return: true}} |
| 366 | } |