Debian Codegraph @

pkg/ip/ipforward_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 ip
16
17 import (
18 "bytes"
19 "os"
20
21 current "github.com/containernetworking/cni/pkg/types/100"
22 )
23
24 func EnableIP4Forward() error {
25 return echo1("/proc/sys/net/ipv4/ip_forward")
26 }
27
28 func EnableIP6Forward() error {
29 return echo1("/proc/sys/net/ipv6/conf/all/forwarding")
30 }
31
32 // EnableForward will enable forwarding for all configured
33 // address families
34 func EnableForward(ips []*current.IPConfig) error {
35 v4 := false
36 v6 := false
37
38 for _, ip := range ips {
39 isV4 := ip.Address.IP.To4() != nil
40 if isV4 && !v4 {
41 if err := EnableIP4Forward(); err != nil {
42 return err
43 }
44 v4 = true
45 } else if !isV4 && !v6 {
46 if err := EnableIP6Forward(); err != nil {
47 return err
48 }
49 v6 = true
50 }
51 }
52 return nil
53 }
54
55 func echo1(f string) error {
56 if content, err := os.ReadFile(f); err == nil {
57 if bytes.Equal(bytes.TrimSpace(content), []byte("1")) {
58 return nil
59 }
60 }
61 return os.WriteFile(f, []byte("1"), 0o644)
62 }