-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
187 lines (170 loc) · 5.76 KB
/
main.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
package main
import (
"fmt"
"io"
"log/syslog"
"net"
"os"
"os/exec"
"strconv"
"strings"
"github.com/bonan/dhcp6rd"
"gopkg.in/alecthomas/kingpin.v2"
)
var (
execCommand = exec.Command
app = kingpin.New("sixrd", "dhclient configuration helper for IPv6 rapid deployment (6rd)")
startCmd = app.Command("start", "(re)configure IPv6 connectivity")
logDest = app.Flag("log-dest", "log destination").PlaceHolder("syslog").Default("syslog").Enum("console", "syslog")
sixrdIntf = app.Flag("sixrd-interface", "sit interface to (de)configure").Default("ipv6rd").OverrideDefaultFromEnvar("SIXRD_INTERFACE").String()
lanIntf = app.Flag("lan-interface", "LAN interface to setup routing for").Envar("SIXRD_LAN_INTERFACE").String()
ip = startCmd.Flag("ip", "(newly) received WAN IP address").Required().String()
sixrdOptions = startCmd.Flag("options", "(newly) received 6rd options").Required().String()
sixrdMTU = startCmd.Flag("sixrd-mtu", "MTU for the tunnel").Default("1480").Envar("SIXRD_MTU").String()
stopCmd = app.Command("stop", "teardown IPv6 configuration")
oldIP = stopCmd.Flag("ip", "(old/current) WAN IP address").String()
oldSixrdOptions = stopCmd.Flag("options", "(old/current) 6rd options").String()
dhcpOpts *dhcp6rd.Option6RD
sixrdRelayPrefix string
sixrdIP string
sixrdFullSubnet string
sixrdPrefix string
sixrdPrefixSize int
sixrdSubnet string
sixrdGateway string
errorLogger io.Writer
infoLogger io.Writer
)
// setupLogger sets up where we log to. It needs to setup two destinations
// which need to conform to io.Writer, one for info messaging, one for
// error output
func setupLogger() {
switch *logDest {
case "syslog":
l, err := syslog.New(syslog.LOG_NOTICE, "6rd")
if err != nil {
kingpin.Fatalf("could not setup syslog based logging, is syslog running?")
}
infoLogger = l
l, err = syslog.New(syslog.LOG_NOTICE, "6rd")
if err != nil {
kingpin.Fatalf("could not setup syslog based logging, is syslog running?")
}
errorLogger = l
default:
infoLogger = os.Stdout
errorLogger = os.Stderr
}
// Kingpin by default logs everything to Stderr so set the app.Writer to
// the error logger
app.Writer(errorLogger)
}
func ipCmd(args ...string) *exec.Cmd {
cmd := execCommand("ip", args...)
return cmd
}
// execute logs and executes the specified command
// though not strictly necessary it has the nice benefit of showing exactly
// which commands got run which helps a lot when trying to understand why
// everything's on fire
func execute(cmd *exec.Cmd) {
fmt.Fprintf(infoLogger, "%s: info: executing: %s\n", app.Name, strings.Join(cmd.Args, " "))
app.FatalIfError(cmd.Run(), "failed to execute: "+strings.Join(cmd.Args, " "))
}
func createInterface() {
execute(ipCmd("tunnel", "add", *sixrdIntf, "mode", "sit", "local", *ip, "ttl", "64"))
}
func configureTunnel() {
execute(ipCmd("tunnel", "6rd", "dev", *sixrdIntf, "6rd-prefix", sixrdPrefix, "6rd-relay_prefix", sixrdRelayPrefix))
execute(ipCmd("addr", "add", sixrdIP, "dev", *sixrdIntf))
execute(ipCmd("link", "set", "mtu", *sixrdMTU, "dev", *sixrdIntf))
}
func configureBlackhole() {
if sixrdPrefixSize < 64 || *lanIntf == "" {
execute(ipCmd("route", "add", "blackhole", sixrdFullSubnet, "metric", "1024"))
}
}
func configureLAN() {
execute(ipCmd("addr", "add", sixrdSubnet, "dev", *lanIntf))
}
func upTunnel() {
execute(ipCmd("link", "set", *sixrdIntf, "up"))
}
func addDefaultRoute() {
execute(ipCmd("route", "add", "default", "via", sixrdGateway, "dev", *sixrdIntf))
}
func destroyInterface() {
cmd := ipCmd("tunnel", "del", *sixrdIntf)
fmt.Fprintf(infoLogger, "%s: info: executing: %s\n", app.Name, strings.Join(cmd.Args, " "))
err := cmd.Run()
if err != nil {
if exiterror, ok := err.(*exec.ExitError); ok {
if exiterror.Sys().(interface {
ExitStatus() int
}).ExitStatus() != 1 {
// Exit code of 1 means we tried to delete an interface that
// doesn't exist, which is fine. It's likely that the system
// was rebooted and it managed to properly cleanup before
// shutdown.
app.Fatalf("failed to execute: " + strings.Join(cmd.Args, " ") + ": " + err.Error())
}
} else {
app.Fatalf("failed to execute: " + strings.Join(cmd.Args, " ") + ": " + err.Error())
}
}
}
func deconfigureLAN() {
execute(ipCmd("addr", "del", sixrdSubnet, "dev", *lanIntf))
}
func deconfigureBlackhole() {
if sixrdPrefixSize < 64 || *lanIntf == "" {
execute(ipCmd("route", "del", sixrdFullSubnet, "dev", "lo"))
}
}
func decodeDHCPOptions(opts string, ip string) {
dhcpOpts, err := dhcp6rd.UnmarshalDhclient(opts)
if err != nil {
app.Fatalf("could not parse 6rd options")
}
subnet, err := dhcpOpts.IPNet(net.ParseIP(ip))
if err != nil {
app.Fatalf("could not determine 6rd subnet")
}
_, ipv4net, err := net.ParseCIDR(ip + "/" + strconv.Itoa(dhcpOpts.MaskLen))
if err != nil {
app.Fatalf("could not parse relay prefix")
}
sixrdRelayPrefix = ipv4net.String()
sixrdIP = subnet.IP.String() + "1/128"
sixrdSubnet = subnet.IP.String() + "1/64"
sixrdPrefixSize, _ = subnet.Mask.Size()
sixrdFullSubnet = subnet.String()
sixrdPrefix = dhcpOpts.Prefix.String() + "/" + strconv.Itoa(dhcpOpts.PrefixLen)
sixrdGateway = "::" + dhcpOpts.Relay[0].String()
}
func main() {
switch kingpin.MustParse(app.Parse(os.Args[1:])) {
case startCmd.FullCommand():
setupLogger()
decodeDHCPOptions(*sixrdOptions, *ip)
createInterface()
configureTunnel()
configureBlackhole()
upTunnel()
addDefaultRoute()
if *lanIntf != "" {
configureLAN()
}
case stopCmd.FullCommand():
setupLogger()
destroyInterface()
if *oldSixrdOptions == "" || *oldIP == "" {
return
}
decodeDHCPOptions(*oldSixrdOptions, *oldIP)
deconfigureBlackhole()
if *lanIntf != "" {
deconfigureLAN()
}
}
}