| 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 main |
| 16 | |
| 17 | import ( |
| 18 | "fmt" |
| 19 | "net" |
| 20 | "strconv" |
| 21 | "strings" |
| 22 | |
| 23 | "github.com/vishvananda/netlink" |
| 24 | |
| 25 | "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 fmtIPPort(ip net.IP, port int) string { |
| 31 | if ip.To4() == nil { |
| 32 | return fmt.Sprintf("[%s]:%d", ip.String(), port) |
| 33 | } |
| 34 | return fmt.Sprintf("%s:%d", 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 getRoutableHostIF(containerIP net.IP) string { |
| 40 | routes, err := netlink.RouteGet(containerIP) |
| 41 | if err != nil { |
| 42 | return "" |
| 43 | } |
| 44 | |
| 45 | for _, route := range routes { |
| 46 | link, err := netlink.LinkByIndex(route.LinkIndex) |
| 47 | if err != nil { |
| 48 | continue |
| 49 | } |
| 50 | |
| 51 | return link.Attrs().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 enableLocalnetRouting(ifName string) error { |
| 60 | routeLocalnetPath := "net/ipv4/conf/" + ifName + "/route_localnet" |
| 61 | _, err := sysctl.Sysctl(routeLocalnetPath, "1") |
| 62 | return err |
| 63 | } |
| 64 | |
| 65 | // groupByProto groups port numbers by protocol |
| 66 | func groupByProto(entries []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.Protocol] |
| 73 | if ok { |
| 74 | out[e.Protocol] = append(out[e.Protocol], e.HostPort) |
| 75 | } else { |
| 76 | out[e.Protocol] = []int{e.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 splitPortList(l []int) []string { |
| 87 | out := []string{} |
| 88 | |
| 89 | acc := []string{} |
| 90 | for _, i := range l { |
| 91 | acc = append(acc, strconv.Itoa(i)) |
| 92 | if len(acc) == 15 { |
| 93 | out = append(out, strings.Join(acc, ",")) |
| 94 | acc = []string{} |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | if len(acc) > 0 { |
| 99 | out = append(out, 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 trimComment(val string) string { |
| 106 | if len(val) <= 255 { |
| 107 | return val |
| 108 | } |
| 109 | |
| 110 | return val[0:253] + "..." |
| 111 | } |