| 1 | // Copyright 2017 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 mainscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/portmap`/ |
| 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 | "strconvscip-go gomod github.com/golang/go/src go1.24.2 strconv/" |
| 21 | "stringsscip-go gomod github.com/golang/go/src go1.24.2 strings/" |
| 22 | |
| 23 | "github.com/vishvananda/netlinkscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/" |
| 24 | |
| 25 | "github.com/containernetworking/plugins/pkg/utils/sysctlscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/utils/sysctl`/" |
| 26 | ) |
| 27 | |
| 28 | // fmtIpPort correctly formats ip:port literals for iptables and ip6tables - |
| 29 | // need to wrap v6 literals in a [] |
| 30 | func fmtIPPortscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/portmap`/fmtIPPort().(ip netscip-go gomod github.com/golang/go/src go1.24.2 net/.IPscip-go gomod github.com/golang/go/src go1.24.2 net/IP#, port int) string { |
| 31 | if ip.To4scip-go gomod github.com/golang/go/src go1.24.2 net/IP#To4().() == nil { |
| 32 | return fmtscip-go gomod github.com/golang/go/src go1.24.2 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.24.2 fmt/Sprintf().("[%s]:%d", ip.Stringscip-go gomod github.com/golang/go/src go1.24.2 net/IP#String().(), port) |
| 33 | } |
| 34 | return fmtscip-go gomod github.com/golang/go/src go1.24.2 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.24.2 fmt/Sprintf().("%s:%d", ip.Stringscip-go gomod github.com/golang/go/src go1.24.2 net/IP#String().(), port) |
| 35 | } |
| 36 | |
| 37 | // getRoutableHostIF will try and determine which interface routes the container's |
| 38 | // traffic. This is the one on which we disable martian filtering. |
| 39 | func getRoutableHostIFscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/portmap`/getRoutableHostIF().(containerIP netscip-go gomod github.com/golang/go/src go1.24.2 net/.IPscip-go gomod github.com/golang/go/src go1.24.2 net/IP#) string { |
| 40 | routes, err := netlinkscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/.RouteGetscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/RouteGet().(containerIP) |
| 41 | if err != nil { |
| 42 | return "" |
| 43 | } |
| 44 | |
| 45 | for _, route := range routes { |
| 46 | link, err := netlinkscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/.LinkByIndexscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/LinkByIndex().(route.LinkIndexscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/Route#LinkIndex.) |
| 47 | if err != nil { |
| 48 | continue |
| 49 | } |
| 50 | |
| 51 | return link.Attrsscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/Link#Attrs().().Namescip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/LinkAttrs#Name. |
| 52 | } |
| 53 | |
| 54 | return "" |
| 55 | } |
| 56 | |
| 57 | // enableLocalnetRouting tells the kernel not to treat 127/8 as a martian, |
| 58 | // so that connections with a source ip of 127/8 can cross a routing boundary. |
| 59 | func enableLocalnetRoutingscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/portmap`/enableLocalnetRouting().(ifName string) error { |
| 60 | routeLocalnetPath := "net/ipv4/conf/" + ifName + "/route_localnet" |
| 61 | _, err := sysctlscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/utils/sysctl`/.Sysctlscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/utils/sysctl`/Sysctl().(routeLocalnetPath, "1") |
| 62 | return err |
| 63 | } |
| 64 | |
| 65 | // groupByProto groups port numbers by protocol |
| 66 | func groupByProtoscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/portmap`/groupByProto().(entries []PortMapEntryscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/portmap`/PortMapEntry#) map[string][]int { |
| 67 | if len(entries) == 0 { |
| 68 | return map[string][]int{} |
| 69 | } |
| 70 | out := map[string][]int{} |
| 71 | for _, e := range entries { |
| 72 | _, ok := out[e.Protocolscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/portmap`/PortMapEntry#Protocol.] |
| 73 | if ok { |
| 74 | out[e.Protocolscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/portmap`/PortMapEntry#Protocol.] = append(out[e.Protocolscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/portmap`/PortMapEntry#Protocol.], e.HostPortscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/portmap`/PortMapEntry#HostPort.) |
| 75 | } else { |
| 76 | out[e.Protocolscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/portmap`/PortMapEntry#Protocol.] = []int{e.HostPortscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/portmap`/PortMapEntry#HostPort.} |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | return out |
| 81 | } |
| 82 | |
| 83 | // splitPortList splits a list of integers in to one or more comma-separated |
| 84 | // string values, for use by multiport. Multiport only allows up to 15 ports |
| 85 | // per entry. |
| 86 | func splitPortListscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/portmap`/splitPortList().(l []int) []string { |
| 87 | out := []string{} |
| 88 | |
| 89 | acc := []string{} |
| 90 | for _, i := range l { |
| 91 | acc = append(acc, strconvscip-go gomod github.com/golang/go/src go1.24.2 strconv/.Itoascip-go gomod github.com/golang/go/src go1.24.2 strconv/Itoa().(i)) |
| 92 | if len(acc) == 15 { |
| 93 | out = append(out, stringsscip-go gomod github.com/golang/go/src go1.24.2 strings/.Joinscip-go gomod github.com/golang/go/src go1.24.2 strings/Join().(acc, ",")) |
| 94 | acc = []string{} |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | if len(acc) > 0 { |
| 99 | out = append(out, stringsscip-go gomod github.com/golang/go/src go1.24.2 strings/.Joinscip-go gomod github.com/golang/go/src go1.24.2 strings/Join().(acc, ",")) |
| 100 | } |
| 101 | return out |
| 102 | } |
| 103 | |
| 104 | // trimComment makes sure no comment is over the iptables limit of 255 chars |
| 105 | func trimCommentscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/portmap`/trimComment().(val string) string { |
| 106 | if len(val) <= 255 { |
| 107 | return val |
| 108 | } |
| 109 | |
| 110 | return val[0:253] + "..." |
| 111 | } |