| 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 | "errors" |
| 21 | "fmt" |
| 22 | "os/exec" |
| 23 | "strings" |
| 24 | "syscall" |
| 25 | ) |
| 26 | |
| 27 | type nftablesError struct { |
| 28 | wrapped error |
| 29 | msg string |
| 30 | errno syscall.Errno |
| 31 | } |
| 32 | |
| 33 | // wrapError wraps an error resulting from running nft |
| 34 | func wrapError(err error) error { |
| 35 | nerr := &nftablesError{wrapped: err, msg: err.Error()} |
| 36 | ee := &exec.ExitError{} |
| 37 | if errors.As(err, &ee) { |
| 38 | if len(ee.Stderr) > 0 { |
| 39 | nerr.msg = string(ee.Stderr) |
| 40 | eol := strings.Index(nerr.msg, "\n") |
| 41 | // The nft binary does not call setlocale() and so will return |
| 42 | // English error strings regardless of the locale. |
| 43 | enoent := strings.Index(nerr.msg, "No such file or directory") |
| 44 | eexist := strings.Index(nerr.msg, "File exists") |
| 45 | if enoent != -1 && (enoent < eol || eol == -1) { |
| 46 | nerr.errno = syscall.ENOENT |
| 47 | } else if eexist != -1 && (eexist < eol || eol == -1) { |
| 48 | nerr.errno = syscall.EEXIST |
| 49 | } |
| 50 | } |
| 51 | } |
| 52 | return nerr |
| 53 | } |
| 54 | |
| 55 | // notFoundError returns an nftablesError with the given message for which IsNotFound will |
| 56 | // return true. |
| 57 | func notFoundError(format string, args ...interface{}) error { |
| 58 | return &nftablesError{msg: fmt.Sprintf(format, args...), errno: syscall.ENOENT} |
| 59 | } |
| 60 | |
| 61 | // existsError returns an nftablesError with the given message for which IsAlreadyExists |
| 62 | // will return true. |
| 63 | func existsError(format string, args ...interface{}) error { |
| 64 | return &nftablesError{msg: fmt.Sprintf(format, args...), errno: syscall.EEXIST} |
| 65 | } |
| 66 | |
| 67 | func (nerr *nftablesError) Error() string { |
| 68 | return nerr.msg |
| 69 | } |
| 70 | |
| 71 | func (nerr *nftablesError) Unwrap() error { |
| 72 | return nerr.wrapped |
| 73 | } |
| 74 | |
| 75 | // IsNotFound tests if err corresponds to an nftables "not found" error of any sort. |
| 76 | // (e.g., in response to a "delete rule" command, this might indicate that the rule |
| 77 | // doesn't exist, or the chain doesn't exist, or the table doesn't exist.) |
| 78 | func IsNotFound(err error) bool { |
| 79 | var nerr *nftablesError |
| 80 | if errors.As(err, &nerr) { |
| 81 | return nerr.errno == syscall.ENOENT |
| 82 | } |
| 83 | return false |
| 84 | } |
| 85 | |
| 86 | // IsAlreadyExists tests if err corresponds to an nftables "already exists" error (e.g. |
| 87 | // when doing a "create" rather than an "add"). |
| 88 | func IsAlreadyExists(err error) bool { |
| 89 | var nerr *nftablesError |
| 90 | if errors.As(err, &nerr) { |
| 91 | return nerr.errno == syscall.EEXIST |
| 92 | } |
| 93 | return false |
| 94 | } |