| 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 nft |
| 21 | |
| 22 | import ( |
| 23 | "github.com/networkplumbing/go-nft/nft/schema" |
| 24 | ) |
| 25 | |
| 26 | type ChainType string |
| 27 | type ChainHook string |
| 28 | type ChainPolicy string |
| 29 | |
| 30 | // Chain Types |
| 31 | const ( |
| 32 | TypeFilter ChainType = schema.TypeFilter |
| 33 | TypeNAT ChainType = schema.TypeNAT |
| 34 | TypeRoute ChainType = schema.TypeRoute |
| 35 | ) |
| 36 | |
| 37 | // Chain Hooks |
| 38 | const ( |
| 39 | HookPreRouting ChainHook = schema.HookPreRouting |
| 40 | HookInput ChainHook = schema.HookInput |
| 41 | HookOutput ChainHook = schema.HookOutput |
| 42 | HookForward ChainHook = schema.HookForward |
| 43 | HookPostRouting ChainHook = schema.HookPostRouting |
| 44 | HookIngress ChainHook = schema.HookIngress |
| 45 | ) |
| 46 | |
| 47 | // Chain Policies |
| 48 | const ( |
| 49 | PolicyAccept ChainPolicy = schema.PolicyAccept |
| 50 | PolicyDrop ChainPolicy = schema.PolicyDrop |
| 51 | ) |
| 52 | |
| 53 | // NewRegularChain returns a new schema chain structure for a regular chain. |
| 54 | func NewRegularChain(table *schema.Table, name string) *schema.Chain { |
| 55 | return NewChain(table, name, nil, nil, nil, nil) |
| 56 | } |
| 57 | |
| 58 | // NewChain returns a new schema chain structure for a base chain. |
| 59 | // For base chains, all arguments are required except the policy. |
| 60 | // Missing arguments will cause an error once the config is applied. |
| 61 | func NewChain(table *schema.Table, name string, ctype *ChainType, hook *ChainHook, prio *int, policy *ChainPolicy) *schema.Chain { |
| 62 | c := &schema.Chain{ |
| 63 | Family: table.Family, |
| 64 | Table: table.Name, |
| 65 | Name: name, |
| 66 | } |
| 67 | |
| 68 | if ctype != nil { |
| 69 | c.Type = string(*ctype) |
| 70 | } |
| 71 | if hook != nil { |
| 72 | c.Hook = string(*hook) |
| 73 | } |
| 74 | if prio != nil { |
| 75 | c.Prio = prio |
| 76 | } |
| 77 | if policy != nil { |
| 78 | c.Policy = string(*policy) |
| 79 | } |
| 80 | |
| 81 | return c |
| 82 | } |