| 1 | // Copyright 2020 CNI authors |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package utilsscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/utils`/ |
| 16 | |
| 17 | import ( |
| 18 | "fmtscip-go gomod github.com/golang/go/src go1.24.2 fmt/" |
| 19 | "netscip-go gomod github.com/golang/go/src go1.24.2 net/" |
| 20 | |
| 21 | "github.com/vishvananda/netlinkscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/" |
| 22 | "golang.org/x/sys/unixscip-go gomod golang.org/x/sys v0.35.0 `golang.org/x/sys/unix`/" |
| 23 | |
| 24 | "github.com/containernetworking/plugins/pkg/netlinksafescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/netlinksafe`/" |
| 25 | ) |
| 26 | |
| 27 | // Assigned Internet Protocol Numbers |
| 28 | // https://www.iana.org/assignments/protocol-numbers/protocol-numbers.xhtml |
| 29 | const ( |
| 30 | PROTOCOL_TCPscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/utils`/PROTOCOL_TCP. = 6 |
| 31 | PROTOCOL_UDPscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/utils`/PROTOCOL_UDP. = 17 |
| 32 | PROTOCOL_SCTPscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/utils`/PROTOCOL_SCTP. = 132 |
| 33 | ) |
| 34 | |
| 35 | // getNetlinkFamily returns the Netlink IP family constant |
| 36 | func getNetlinkFamilyscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/utils`/getNetlinkFamily().(isIPv6 bool) netlinkscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/.InetFamilyscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/InetFamily# { |
| 37 | if isIPv6 { |
| 38 | return unixscip-go gomod golang.org/x/sys v0.35.0 `golang.org/x/sys/unix`/.AF_INET6scip-go gomod golang.org/x/sys v0.35.0 `golang.org/x/sys/unix`/AF_INET6. |
| 39 | } |
| 40 | return unixscip-go gomod golang.org/x/sys v0.35.0 `golang.org/x/sys/unix`/.AF_INETscip-go gomod golang.org/x/sys v0.35.0 `golang.org/x/sys/unix`/AF_INET. |
| 41 | } |
| 42 | |
| 43 | // DeleteConntrackEntriesForDstIP delete the conntrack entries for the connections |
| 44 | // specified by the given destination IP and protocol |
| 45 | func DeleteConntrackEntriesForDstIPscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/utils`/DeleteConntrackEntriesForDstIP().(dstIP string, protocol uint8) error { |
| 46 | ip := netscip-go gomod github.com/golang/go/src go1.24.2 net/.ParseIPscip-go gomod github.com/golang/go/src go1.24.2 net/ParseIP().(dstIP) |
| 47 | if ip == nil { |
| 48 | return fmtscip-go gomod github.com/golang/go/src go1.24.2 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.24.2 fmt/Errorf().("error deleting connection tracking state, bad IP %s", ip) |
| 49 | } |
| 50 | family := getNetlinkFamilyscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/utils`/getNetlinkFamily().(ip.To4scip-go gomod github.com/golang/go/src go1.24.2 net/IP#To4().() == nil) |
| 51 | |
| 52 | filter := &netlinkscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/.ConntrackFilterscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/ConntrackFilter#{} |
| 53 | filter.AddIPscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/ConntrackFilter#AddIP().(netlinkscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/.ConntrackOrigDstIPscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/ConntrackOrigDstIP., ip) |
| 54 | filter.AddProtocolscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/ConntrackFilter#AddProtocol().(protocol) |
| 55 | |
| 56 | _, err := netlinksafescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/netlinksafe`/.ConntrackDeleteFiltersscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/netlinksafe`/ConntrackDeleteFilters().(netlinkscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/.ConntrackTablescip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/ConntrackTable., family, filter) |
| 57 | if err != nil { |
| 58 | return fmtscip-go gomod github.com/golang/go/src go1.24.2 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.24.2 fmt/Errorf().("error deleting connection tracking state for protocol: %d IP: %s, error: %v", protocol, ip, err) |
| 59 | } |
| 60 | return nil |
| 61 | } |
| 62 | |
| 63 | // DeleteConntrackEntriesForDstPort delete the conntrack entries for the connections specified |
| 64 | // by the given destination port, protocol and IP family |
| 65 | func DeleteConntrackEntriesForDstPortscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/utils`/DeleteConntrackEntriesForDstPort().(port uint16, protocol uint8, family netlinkscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/.InetFamilyscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/InetFamily#) error { |
| 66 | filter := &netlinkscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/.ConntrackFilterscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/ConntrackFilter#{} |
| 67 | filter.AddProtocolscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/ConntrackFilter#AddProtocol().(protocol) |
| 68 | filter.AddPortscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/ConntrackFilter#AddPort().(netlinkscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/.ConntrackOrigDstPortscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/ConntrackOrigDstPort., port) |
| 69 | |
| 70 | _, err := netlinksafescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/netlinksafe`/.ConntrackDeleteFiltersscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/netlinksafe`/ConntrackDeleteFilters().(netlinkscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/.ConntrackTablescip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/ConntrackTable., family, filter) |
| 71 | if err != nil { |
| 72 | return fmtscip-go gomod github.com/golang/go/src go1.24.2 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.24.2 fmt/Errorf().("error deleting connection tracking state for protocol: %d Port: %d, error: %v", protocol, port, err) |
| 73 | } |
| 74 | return nil |
| 75 | } |