Debian Codegraph @

pkg/ipam/ipam_linux.go

raw symbols SCIP
1 // Copyright 2015 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 ipam
16
17 import (
18 "fmt"
19 "net"
20 "os"
21 "time"
22
23 "github.com/vishvananda/netlink"
24
25 current "github.com/containernetworking/cni/pkg/types/100"
26 "github.com/containernetworking/plugins/pkg/ip"
27 "github.com/containernetworking/plugins/pkg/netlinksafe"
28 "github.com/containernetworking/plugins/pkg/utils/sysctl"
29 )
30
31 const (
32 // Note: use slash as separator so we can have dots in interface name (VLANs)
33 DisableIPv6SysctlTemplate = "net/ipv6/conf/%s/disable_ipv6"
34 KeepAddrOnDownSysctlTemplate = "net/ipv6/conf/%s/keep_addr_on_down"
35
36 dadSettleTimeout = 5 * time.Second
37 )
38
39 // ConfigureIface takes the result of IPAM plugin and
40 // applies to the ifName interface
41 func ConfigureIface(ifName string, res *current.Result) error {
42 if len(res.Interfaces) == 0 {
43 return fmt.Errorf("no interfaces to configure")
44 }
45
46 link, err := netlinksafe.LinkByName(ifName)
47 if err != nil {
48 return fmt.Errorf("failed to lookup %q: %v", ifName, err)
49 }
50
51 var v4gw, v6gw net.IP
52 hasEnabledIpv6 := false
53 for _, ipc := range res.IPs {
54 if ipc.Interface == nil {
55 continue
56 }
57 intIdx := *ipc.Interface
58 if intIdx < 0 || intIdx >= len(res.Interfaces) || res.Interfaces[intIdx].Name != ifName {
59 // IP address is for a different interface
60 return fmt.Errorf("failed to add IP addr %v to %q: invalid interface index", ipc, ifName)
61 }
62
63 // Make sure sysctl "disable_ipv6" is 0 and "keep_addr_on_down" is 1
64 // if we are about to add an IPv6 address to the interface
65 if !hasEnabledIpv6 && ipc.Address.IP.To4() == nil {
66 // Enabled IPv6 for loopback "lo" and the interface
67 // being configured
68 for _, iface := range [2]string{"lo", ifName} {
69 ipv6SysctlValueName := fmt.Sprintf(DisableIPv6SysctlTemplate, iface)
70
71 // Read current sysctl value
72 value, err := sysctl.Sysctl(ipv6SysctlValueName)
73 if err != nil {
74 fmt.Fprintf(os.Stderr, "ipam_linux: failed to read sysctl %q: %v\n", ipv6SysctlValueName, err)
75 continue
76 }
77 if value == "0" {
78 continue
79 }
80
81 // Write sysctl to enable IPv6
82 _, err = sysctl.Sysctl(ipv6SysctlValueName, "0")
83 if err != nil {
84 return fmt.Errorf("failed to enable IPv6 for interface %q (%s=%s): %v", iface, ipv6SysctlValueName, value, err)
85 }
86 }
87
88 // Enable "keep_addr_on_down" for the interface being configured
89 // This prevents the kernel from removing the address when the interface is brought down
90 keepAddrOnDownSysctlValueName := fmt.Sprintf(KeepAddrOnDownSysctlTemplate, ifName)
91 _, err = sysctl.Sysctl(keepAddrOnDownSysctlValueName, "1")
92 if err != nil {
93 return fmt.Errorf("failed to enable keep_addr_on_down for interface %q: %v", ifName, err)
94 }
95
96 hasEnabledIpv6 = true
97 }
98
99 addr := &netlink.Addr{IPNet: &ipc.Address, Label: ""}
100 if err = netlink.AddrAdd(link, addr); err != nil {
101 return fmt.Errorf("failed to add IP addr %v to %q: %v", ipc, ifName, err)
102 }
103
104 gwIsV4 := ipc.Gateway.To4() != nil
105 if gwIsV4 && v4gw == nil {
106 v4gw = ipc.Gateway
107 } else if !gwIsV4 && v6gw == nil {
108 v6gw = ipc.Gateway
109 }
110 }
111
112 if err := netlink.LinkSetUp(link); err != nil {
113 return fmt.Errorf("failed to set %q UP: %v", ifName, err)
114 }
115
116 if v6gw != nil {
117 err = ip.SettleAddresses(ifName, dadSettleTimeout)
118 if err != nil {
119 return fmt.Errorf("failed to settle addresses for %q: %v", ifName, err)
120 }
121 }
122
123 for _, r := range res.Routes {
124 routeIsV4 := r.Dst.IP.To4() != nil
125 gw := r.GW
126 if gw == nil {
127 if routeIsV4 && v4gw != nil {
128 gw = v4gw
129 } else if !routeIsV4 && v6gw != nil {
130 gw = v6gw
131 }
132 }
133 route := netlink.Route{
134 Dst: &r.Dst,
135 LinkIndex: link.Attrs().Index,
136 Gw: gw,
137 Priority: r.Priority,
138 }
139
140 if r.Table != nil {
141 route.Table = *r.Table
142 }
143
144 if r.Scope != nil {
145 route.Scope = netlink.Scope(*r.Scope)
146 }
147
148 if err = netlink.RouteAddEcmp(&route); err != nil {
149 return fmt.Errorf("failed to add route '%v via %v dev %v metric %d (Scope: %v, Table: %d)': %v", r.Dst, gw, ifName, r.Priority, route.Scope, route.Table, err)
150 }
151 }
152
153 return nil
154 }