| 1 | /* |
| 2 | Copyright 2023 The Kubernetes Authors. |
| 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 | |
| 17 | package knftables |
| 18 | |
| 19 | import ( |
| 20 | "io" |
| 21 | "time" |
| 22 | ) |
| 23 | |
| 24 | const ( |
| 25 | // Maximum length of a table, chain, set, etc, name |
| 26 | NameLengthMax = 256 |
| 27 | |
| 28 | // Maximum length of a comment |
| 29 | CommentLengthMax = 128 |
| 30 | ) |
| 31 | |
| 32 | // Object is the interface for an nftables object. All of the concrete object types |
| 33 | // implement this interface. |
| 34 | type Object interface { |
| 35 | // validate validates an object for an operation |
| 36 | validate(verb verb) error |
| 37 | |
| 38 | // writeOperation writes out an "nft" operation involving the object. It assumes |
| 39 | // that the object has been validated. |
| 40 | writeOperation(verb verb, ctx *nftContext, writer io.Writer) |
| 41 | |
| 42 | // parse is the opposite of writeOperation; it fills Object fields based on an "nft add" |
| 43 | // command. line is the part of the line after "nft add <type> <family> <tablename>" |
| 44 | // (so for most types it starts with the object name). |
| 45 | // If error is returned, Object's fields may be partially filled, therefore Object should not be used. |
| 46 | parse(line string) error |
| 47 | } |
| 48 | |
| 49 | // Family is an nftables family |
| 50 | type Family string |
| 51 | |
| 52 | const ( |
| 53 | // IPv4Family represents the "ip" nftables family, for IPv4 rules. |
| 54 | IPv4Family Family = "ip" |
| 55 | |
| 56 | // IPv6Family represents the "ip6" nftables family, for IPv6 rules. |
| 57 | IPv6Family Family = "ip6" |
| 58 | |
| 59 | // InetFamily represents the "inet" nftables family, for mixed IPv4 and IPv6 rules. |
| 60 | InetFamily Family = "inet" |
| 61 | |
| 62 | // ARPFamily represents the "arp" nftables family, for ARP rules. |
| 63 | ARPFamily Family = "arp" |
| 64 | |
| 65 | // BridgeFamily represents the "bridge" nftables family, for rules operating |
| 66 | // on packets traversing a bridge. |
| 67 | BridgeFamily Family = "bridge" |
| 68 | |
| 69 | // NetDevFamily represents the "netdev" nftables family, for rules operating on |
| 70 | // the device ingress/egress path. |
| 71 | NetDevFamily Family = "netdev" |
| 72 | ) |
| 73 | |
| 74 | // Table represents an nftables table. |
| 75 | type Table struct { |
| 76 | // Comment is an optional comment for the table. (Requires kernel >= 5.10 and |
| 77 | // nft >= 0.9.7; otherwise this field will be silently ignored. Requires |
| 78 | // nft >= 1.0.8 to include comments in List() results.) |
| 79 | Comment *string |
| 80 | |
| 81 | // Handle is an identifier that can be used to uniquely identify an object when |
| 82 | // deleting it. When adding a new object, this must be nil. |
| 83 | Handle *int |
| 84 | } |
| 85 | |
| 86 | // BaseChainType represents the "type" of a "base chain" (ie, a chain that is attached to a hook). |
| 87 | // See https://wiki.nftables.org/wiki-nftables/index.php/Configuring_chains#Base_chain_types |
| 88 | type BaseChainType string |
| 89 | |
| 90 | const ( |
| 91 | // FilterType is the chain type for basic packet filtering. |
| 92 | FilterType BaseChainType = "filter" |
| 93 | |
| 94 | // NATType is the chain type for doing DNAT, SNAT, and masquerading. |
| 95 | // NAT operations are only available from certain hooks. |
| 96 | NATType BaseChainType = "nat" |
| 97 | |
| 98 | // RouteType is the chain type for rules that change the routing of packets. |
| 99 | // Chains of this type can only be added to the "output" hook. |
| 100 | RouteType BaseChainType = "route" |
| 101 | ) |
| 102 | |
| 103 | // BaseChainHook represents the "hook" that a base chain is attached to. |
| 104 | // See https://wiki.nftables.org/wiki-nftables/index.php/Configuring_chains#Base_chain_hooks |
| 105 | // and https://wiki.nftables.org/wiki-nftables/index.php/Netfilter_hooks |
| 106 | type BaseChainHook string |
| 107 | |
| 108 | const ( |
| 109 | // PreroutingHook is the "prerouting" stage of packet processing, which is the |
| 110 | // first stage (after "ingress") for inbound ("input path" and "forward path") |
| 111 | // packets. |
| 112 | PreroutingHook BaseChainHook = "prerouting" |
| 113 | |
| 114 | // InputHook is the "input" stage of packet processing, which happens after |
| 115 | // "prerouting" for inbound packets being delivered to an interface on this host, |
| 116 | // in this network namespace. |
| 117 | InputHook BaseChainHook = "input" |
| 118 | |
| 119 | // ForwardHook is the "forward" stage of packet processing, which happens after |
| 120 | // "prerouting" for inbound packets destined for a non-local IP (i.e. on another |
| 121 | // host or in another network namespace) |
| 122 | ForwardHook BaseChainHook = "forward" |
| 123 | |
| 124 | // OutputHook is the "output" stage of packet processing, which is the first stage |
| 125 | // for outbound packets, regardless of their final destination. |
| 126 | OutputHook BaseChainHook = "output" |
| 127 | |
| 128 | // PostroutingHook is the "postrouting" stage of packet processing, which is the |
| 129 | // final stage (before "egress") for outbound ("forward path" and "output path") |
| 130 | // packets. |
| 131 | PostroutingHook BaseChainHook = "postrouting" |
| 132 | |
| 133 | // IngressHook is the "ingress" stage of packet processing, in the "netdev" family |
| 134 | // or (with kernel >= 5.10 and nft >= 0.9.7) the "inet" family. |
| 135 | IngressHook BaseChainHook = "ingress" |
| 136 | |
| 137 | // EgressHook is the "egress" stage of packet processing, in the "netdev" family |
| 138 | // (with kernel >= 5.16 and nft >= 1.0.1). |
| 139 | EgressHook BaseChainHook = "egress" |
| 140 | ) |
| 141 | |
| 142 | // BaseChainPriority represents the "priority" of a base chain. Lower values run earlier. |
| 143 | // See https://wiki.nftables.org/wiki-nftables/index.php/Configuring_chains#Base_chain_priority |
| 144 | // and https://wiki.nftables.org/wiki-nftables/index.php/Netfilter_hooks#Priority_within_hook |
| 145 | // |
| 146 | // In addition to the const values, you can also use a signed integer value, or an |
| 147 | // arithmetic expression consisting of a const value followed by "+" or "-" and an |
| 148 | // integer. |
| 149 | type BaseChainPriority string |
| 150 | |
| 151 | const ( |
| 152 | // RawPriority is the earliest named priority. In particular, it can be used for |
| 153 | // rules that need to run before conntrack. It is equivalent to the value -300 and |
| 154 | // can be used in the ip, ip6, and inet families. |
| 155 | RawPriority BaseChainPriority = "raw" |
| 156 | |
| 157 | // ManglePriority is the standard priority for packet-rewriting operations. It is |
| 158 | // equivalent to the value -150 and can be used in the ip, ip6, and inet families. |
| 159 | ManglePriority BaseChainPriority = "mangle" |
| 160 | |
| 161 | // DNATPriority is the standard priority for DNAT operations. In the ip, ip6, and |
| 162 | // inet families, it is equivalent to the value -100. In the bridge family it is |
| 163 | // equivalent to the value -300. In both cases it can only be used from the |
| 164 | // prerouting hook. |
| 165 | DNATPriority BaseChainPriority = "dstnat" |
| 166 | |
| 167 | // FilterPriority is the standard priority for filtering operations. In the ip, |
| 168 | // ip6, inet, arp, and netdev families, it is equivalent to the value 0. In the |
| 169 | // bridge family it is equivalent to the value -200. |
| 170 | FilterPriority BaseChainPriority = "filter" |
| 171 | |
| 172 | // OutPriority is FIXME. It is equivalent to the value 300 and can only be used in |
| 173 | // the bridge family. |
| 174 | OutPriority BaseChainPriority = "out" |
| 175 | |
| 176 | // SecurityPriority is the standard priority for security operations ("where |
| 177 | // secmark can be set for example"). It is equivalent to the value 50 and can be |
| 178 | // used in the ip, ip6, and inet families. |
| 179 | SecurityPriority BaseChainPriority = "security" |
| 180 | |
| 181 | // SNATPriority is the standard priority for SNAT operations. In the ip, ip6, and |
| 182 | // inet families, it is equivalent to the value 100. In the bridge family it is |
| 183 | // equivalent to the value 300. In both cases it can only be used from the |
| 184 | // postrouting hook. |
| 185 | SNATPriority BaseChainPriority = "srcnat" |
| 186 | ) |
| 187 | |
| 188 | // Chain represents an nftables chain; either a "base chain" (if Type, Hook, and Priority |
| 189 | // are specified), or a "regular chain" (if they are not). |
| 190 | type Chain struct { |
| 191 | // Name is the name of the chain. |
| 192 | Name string |
| 193 | |
| 194 | // Type is the chain type; this must be set for a base chain and unset for a |
| 195 | // regular chain. |
| 196 | Type *BaseChainType |
| 197 | // Hook is the hook that the chain is connected to; this must be set for a base |
| 198 | // chain and unset for a regular chain. |
| 199 | Hook *BaseChainHook |
| 200 | // Priority is the chain priority; this must be set for a base chain and unset for |
| 201 | // a regular chain. You can call ParsePriority() to convert this to a number. |
| 202 | Priority *BaseChainPriority |
| 203 | |
| 204 | // Device is the network interface that the chain is attached to; this must be set |
| 205 | // for a base chain connected to the "ingress" or "egress" hooks, and unset for |
| 206 | // all other chains. |
| 207 | Device *string |
| 208 | |
| 209 | // Comment is an optional comment for the object. (Requires kernel >= 5.10 and |
| 210 | // nft >= 0.9.7; otherwise this field will be silently ignored. Requires |
| 211 | // nft >= 1.0.8 to include comments in List() results.) |
| 212 | Comment *string |
| 213 | |
| 214 | // Handle is an identifier that can be used to uniquely identify an object when |
| 215 | // deleting it. When adding a new object, this must be nil |
| 216 | Handle *int |
| 217 | } |
| 218 | |
| 219 | // Rule represents a rule in a chain |
| 220 | type Rule struct { |
| 221 | // Chain is the name of the chain that contains this rule |
| 222 | Chain string |
| 223 | |
| 224 | // Rule is the rule in standard nftables syntax. (Should be empty on Delete, but |
| 225 | // is ignored if not.) Note that this does not include any rule comment, which is |
| 226 | // separate from the rule itself. |
| 227 | Rule string |
| 228 | |
| 229 | // Comment is an optional comment for the rule. |
| 230 | Comment *string |
| 231 | |
| 232 | // Index is the number of a rule (counting from 0) to Add this Rule after or |
| 233 | // Insert it before. Cannot be specified along with Handle. If neither Index |
| 234 | // nor Handle is specified then Add appends the rule the end of the chain and |
| 235 | // Insert prepends it to the beginning. |
| 236 | Index *int |
| 237 | |
| 238 | // Handle is a rule handle. In Add or Insert, if set, this is the handle of |
| 239 | // existing rule to put the new rule after/before. In Delete or Replace, this |
| 240 | // indicates the existing rule to delete/replace, and is mandatory. In the result |
| 241 | // of a List, this will indicate the rule's handle that can then be used in a |
| 242 | // later operation. |
| 243 | Handle *int |
| 244 | } |
| 245 | |
| 246 | // SetFlag represents a set or map flag |
| 247 | type SetFlag string |
| 248 | |
| 249 | const ( |
| 250 | // ConstantFlag is a flag indicating that the set/map is constant. FIXME UNDOCUMENTED |
| 251 | ConstantFlag SetFlag = "constant" |
| 252 | |
| 253 | // DynamicFlag is a flag indicating that the set contains stateful objects |
| 254 | // (counters, quotas, or limits) that will be dynamically updated. |
| 255 | DynamicFlag SetFlag = "dynamic" |
| 256 | |
| 257 | // IntervalFlag is a flag indicating that the set contains either CIDR elements or |
| 258 | // IP ranges. |
| 259 | IntervalFlag SetFlag = "interval" |
| 260 | |
| 261 | // TimeoutFlag is a flag indicating that the set/map has a timeout after which |
| 262 | // dynamically added elements will be removed. (It is set automatically if the |
| 263 | // set/map has a Timeout.) |
| 264 | TimeoutFlag SetFlag = "timeout" |
| 265 | ) |
| 266 | |
| 267 | // SetPolicy represents a set or map storage policy |
| 268 | type SetPolicy string |
| 269 | |
| 270 | const ( |
| 271 | // PolicyPerformance FIXME |
| 272 | PerformancePolicy SetPolicy = "performance" |
| 273 | |
| 274 | // PolicyMemory FIXME |
| 275 | MemoryPolicy SetPolicy = "memory" |
| 276 | ) |
| 277 | |
| 278 | // Set represents the definition of an nftables set (but not its elements) |
| 279 | type Set struct { |
| 280 | // Name is the name of the set. |
| 281 | Name string |
| 282 | |
| 283 | // Type is the type of the set key (eg "ipv4_addr"). Either Type or TypeOf, but |
| 284 | // not both, must be non-empty. |
| 285 | Type string |
| 286 | |
| 287 | // TypeOf is the type of the set key as an nftables expression (eg "ip saddr"). |
| 288 | // Either Type or TypeOf, but not both, must be non-empty. (Requires at least nft |
| 289 | // 0.9.4, and newer than that for some types.) |
| 290 | TypeOf string |
| 291 | |
| 292 | // Flags are the set flags |
| 293 | Flags []SetFlag |
| 294 | |
| 295 | // Timeout is the time that an element will stay in the set before being removed. |
| 296 | // (Optional; mandatory for sets that will be added to from the packet path) |
| 297 | Timeout *time.Duration |
| 298 | |
| 299 | // GCInterval is the interval at which timed-out elements will be removed from the |
| 300 | // set. (Optional; FIXME DEFAULT) |
| 301 | GCInterval *time.Duration |
| 302 | |
| 303 | // Size if the maximum numer of elements in the set. |
| 304 | // (Optional; mandatory for sets that will be added to from the packet path) |
| 305 | Size *uint64 |
| 306 | |
| 307 | // Policy is the FIXME |
| 308 | Policy *SetPolicy |
| 309 | |
| 310 | // AutoMerge indicates that adjacent/overlapping set elements should be merged |
| 311 | // together (only for interval sets) |
| 312 | AutoMerge *bool |
| 313 | |
| 314 | // Comment is an optional comment for the object. (Requires kernel >= 5.10 and |
| 315 | // nft >= 0.9.7; otherwise this field will be silently ignored.) |
| 316 | Comment *string |
| 317 | |
| 318 | // Handle is an identifier that can be used to uniquely identify an object when |
| 319 | // deleting it. When adding a new object, this must be nil |
| 320 | Handle *int |
| 321 | } |
| 322 | |
| 323 | // Map represents the definition of an nftables map (but not its elements) |
| 324 | type Map struct { |
| 325 | // Name is the name of the map. |
| 326 | Name string |
| 327 | |
| 328 | // Type is the type of the map key and value (eg "ipv4_addr : verdict"). Either |
| 329 | // Type or TypeOf, but not both, must be non-empty. |
| 330 | Type string |
| 331 | |
| 332 | // TypeOf is the type of the set key as an nftables expression (eg "ip saddr : verdict"). |
| 333 | // Either Type or TypeOf, but not both, must be non-empty. (Requires at least nft 0.9.4, |
| 334 | // and newer than that for some types.) |
| 335 | TypeOf string |
| 336 | |
| 337 | // Flags are the map flags |
| 338 | Flags []SetFlag |
| 339 | |
| 340 | // Timeout is the time that an element will stay in the set before being removed. |
| 341 | // (Optional; mandatory for sets that will be added to from the packet path) |
| 342 | Timeout *time.Duration |
| 343 | |
| 344 | // GCInterval is the interval at which timed-out elements will be removed from the |
| 345 | // set. (Optional; FIXME DEFAULT) |
| 346 | GCInterval *time.Duration |
| 347 | |
| 348 | // Size if the maximum numer of elements in the set. |
| 349 | // (Optional; mandatory for sets that will be added to from the packet path) |
| 350 | Size *uint64 |
| 351 | |
| 352 | // Policy is the FIXME |
| 353 | Policy *SetPolicy |
| 354 | |
| 355 | // Comment is an optional comment for the object. (Requires kernel >= 5.10 and |
| 356 | // nft >= 0.9.7; otherwise this field will be silently ignored.) |
| 357 | Comment *string |
| 358 | |
| 359 | // Handle is an identifier that can be used to uniquely identify an object when |
| 360 | // deleting it. When adding a new object, this must be nil |
| 361 | Handle *int |
| 362 | } |
| 363 | |
| 364 | // Element represents a set or map element |
| 365 | type Element struct { |
| 366 | // Set is the name of the set that contains this element (or the empty string if |
| 367 | // this is a map element.) |
| 368 | Set string |
| 369 | |
| 370 | // Map is the name of the map that contains this element (or the empty string if |
| 371 | // this is a set element.) |
| 372 | Map string |
| 373 | |
| 374 | // Key is the element key. (The list contains a single element for "simple" keys, |
| 375 | // or multiple elements for concatenations.) |
| 376 | Key []string |
| 377 | |
| 378 | // Value is the map element value. As with Key, this may be a single value or |
| 379 | // multiple. For set elements, this must be nil. |
| 380 | Value []string |
| 381 | |
| 382 | // Comment is an optional comment for the element |
| 383 | Comment *string |
| 384 | } |
| 385 | |
| 386 | type FlowtableIngressPriority string |
| 387 | |
| 388 | const ( |
| 389 | // FilterIngressPriority is the priority for the filter value in the Ingress hook |
| 390 | // that stands for 0. |
| 391 | FilterIngressPriority FlowtableIngressPriority = "filter" |
| 392 | ) |
| 393 | |
| 394 | // Flowtable represents an nftables flowtable. |
| 395 | // https://wiki.nftables.org/wiki-nftables/index.php/Flowtables |
| 396 | type Flowtable struct { |
| 397 | // Name is the name of the flowtable. |
| 398 | Name string |
| 399 | |
| 400 | // The Priority can be a signed integer or FlowtableIngressPriority which stands for 0. |
| 401 | // Addition and subtraction can be used to set relative priority, e.g. filter + 5 equals to 5. |
| 402 | Priority *FlowtableIngressPriority |
| 403 | |
| 404 | // The Devices are specified as iifname(s) of the input interface(s) of the traffic |
| 405 | // that should be offloaded. |
| 406 | Devices []string |
| 407 | |
| 408 | // Handle is an identifier that can be used to uniquely identify an object when |
| 409 | // deleting it. When adding a new object, this must be nil |
| 410 | Handle *int |
| 411 | } |