-
Notifications
You must be signed in to change notification settings - Fork 4
/
ipip.go
227 lines (204 loc) · 5.27 KB
/
ipip.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
// Copyright © 2015-2016 Platina Systems, Inc. All rights reserved.
// Use of this source code is governed by the GPL-2 license described in the
// LICENSE file.
package ipip
import (
"fmt"
"net"
"github.com/platinasystems/goes/cmd/ip/link/add/internal/options"
"github.com/platinasystems/goes/cmd/ip/link/add/internal/request"
"github.com/platinasystems/goes/internal/nl"
"github.com/platinasystems/goes/internal/nl/rtnl"
"github.com/platinasystems/goes/lang"
)
type Command struct{}
func (Command) String() string { return "ipip" }
func (Command) Usage() string {
return "ip link add type ipip [ OPTIONS ]..."
}
func (Command) Apropos() lang.Alt {
return lang.Alt{
lang.EnUS: "add an ipip virtual link",
}
}
func (Command) Man() lang.Alt {
return lang.Alt{
lang.EnUS: `
OPTIONS
remote ADDR
local ADDR
ttl { 1:255 }
tos { 1:8 }
dev DEVICE
no-encap | encap { fou | gue | none } ]
encap-dport PORT
encap-sport { PORT | auto }
mode {
[ ipip | ip4ip4 | ip4/ip4 ] |
[ mplsip | mplsip4 | mpls/ip4 ] |
[ any | anyip4 | any/ip4 ]
}
[no-]encap-csum
[no-]pmtudisc ]`,
}
}
func (Command) Main(args ...string) error {
var info nl.Attrs
var encapflags uint16
opt, args := options.New(args)
args = opt.Flags.More(args,
[]string{"no-encap", "-encap"},
[]string{"encap-csum", "+encap-csum"},
[]string{"no-encap-csum", "-encap-csum"},
[]string{"pmtudisc", "+pmtudisc"},
[]string{"no-pmtudisc", "-pmtudisc"},
)
args = opt.Parms.More(args,
"remote",
"local",
"dev",
[]string{"ttl", "hoplimit"},
[]string{"tos", "tclass", "dstfield"},
"encap",
"encap-sport",
"encap-dport",
"mode",
)
sock, err := nl.NewSock()
if err != nil {
return err
}
defer sock.Close()
sr := nl.NewSockReceiver(sock)
if err = rtnl.MakeIfMaps(sr); err != nil {
return err
}
add, err := request.New(opt, args)
if err != nil {
return err
}
for _, x := range []struct {
name string
t uint16
}{
{"local", rtnl.IFLA_IPTUN_LOCAL},
{"remote", rtnl.IFLA_IPTUN_REMOTE},
} {
if s := opt.Parms.ByName[x.name]; len(s) > 0 {
if ip4 := net.ParseIP(s).To4(); ip4 == nil {
return fmt.Errorf("%s: %q invalid", x.name, s)
} else {
info = append(info, nl.Attr{Type: x.t,
Value: nl.BytesAttr(ip4)})
}
}
}
for _, x := range []struct {
name string
t uint16
}{
{"ttl", rtnl.IFLA_IPTUN_TTL},
{"tos", rtnl.IFLA_IPTUN_TOS},
} {
if s := opt.Parms.ByName[x.name]; len(s) > 0 {
var u8 uint8
if _, err := fmt.Sscan(s, &u8); err != nil {
return fmt.Errorf("%s: %q %v", x.name, s, err)
}
info = append(info, nl.Attr{Type: x.t,
Value: nl.Uint8Attr(u8)})
}
}
if s := opt.Parms.ByName["dev"]; len(s) > 0 {
dev, found := rtnl.If.IndexByName[s]
if !found {
return fmt.Errorf("dev: %q not found", s)
}
info = append(info, nl.Attr{Type: rtnl.IFLA_IPTUN_LINK,
Value: nl.Uint32Attr(dev)})
}
if opt.Flags.ByName["pmtudisc"] {
info = append(info, nl.Attr{Type: rtnl.IFLA_IPTUN_PMTUDISC,
Value: nl.Uint8Attr(1)})
} else if opt.Flags.ByName["no-pmtudisc"] {
info = append(info, nl.Attr{Type: rtnl.IFLA_IPTUN_PMTUDISC,
Value: nl.Uint8Attr(0)})
}
if opt.Flags.ByName["no-encap"] {
info = append(info, nl.Attr{Type: rtnl.IFLA_IPTUN_ENCAP_TYPE,
Value: nl.Uint32Attr(rtnl.TUNNEL_ENCAP_NONE)})
} else {
if s := opt.Parms.ByName["encap"]; len(s) > 0 {
if encap, found := map[string]uint16{
"fou": rtnl.TUNNEL_ENCAP_FOU,
"gue": rtnl.TUNNEL_ENCAP_GUE,
"none": rtnl.TUNNEL_ENCAP_NONE,
}[s]; !found {
return fmt.Errorf("encap: %q unknown", s)
} else {
info = append(info,
nl.Attr{Type: rtnl.IFLA_IPTUN_ENCAP_TYPE,
Value: nl.Uint32Attr(encap)})
}
}
}
for _, x := range []struct {
name string
t uint16
}{
{"encap-sport", rtnl.IFLA_IPTUN_ENCAP_SPORT},
{"encap-dport", rtnl.IFLA_IPTUN_ENCAP_DPORT},
} {
if s := opt.Parms.ByName[x.name]; len(s) > 0 {
var u16 uint16
if s != "any" {
if _, err := fmt.Sscan(s, &u16); err != nil {
return fmt.Errorf("%s: %q %v",
x.name, s, err)
}
}
info = append(info, nl.Attr{
Type: rtnl.IFLA_IPTUN_ENCAP_TYPE,
Value: nl.Be16Attr(u16)})
}
}
switch s := opt.Parms.ByName["mode"]; s {
case "", "any", "anyip4", "any/ip4":
info = append(info, nl.Attr{Type: rtnl.IFLA_IPTUN_PROTO,
Value: nl.Uint8Attr(0)})
case "ipip", "ip4ip4", "ip4/ip4":
info = append(info, nl.Attr{Type: rtnl.IFLA_IPTUN_PROTO,
Value: nl.Uint8Attr(rtnl.IPPROTO_IPIP)})
case "mplsip", "mplsip4", "mpls/ip4":
info = append(info, nl.Attr{Type: rtnl.IFLA_IPTUN_PROTO,
Value: nl.Uint8Attr(rtnl.IPPROTO_MPLS)})
default:
return fmt.Errorf("%q: unknown encap", s)
}
for _, x := range []struct {
set string
unset string
flag uint16
}{
{"encap-csum", "no-encap-csum", rtnl.TUNNEL_ENCAP_FLAG_CSUM},
} {
if opt.Flags.ByName[x.set] {
encapflags |= x.flag
} else if opt.Flags.ByName[x.unset] {
encapflags &^= x.flag
}
}
info = append(info, nl.Attr{Type: rtnl.IFLA_GRE_ENCAP_FLAGS,
Value: nl.Uint16Attr(encapflags)})
add.Attrs = append(add.Attrs, nl.Attr{Type: rtnl.IFLA_LINKINFO,
Value: nl.Attrs{
nl.Attr{Type: rtnl.IFLA_INFO_KIND,
Value: nl.KstringAttr("ipip")},
nl.Attr{Type: rtnl.IFLA_INFO_DATA, Value: info},
}})
req, err := add.Message()
if err == nil {
err = sr.UntilDone(req, nl.DoNothing)
}
return err
}