forked from scionproto/scion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
205 lines (192 loc) · 5.33 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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
// Copyright 2017 ETH Zurich
// Copyright 2018 ETH Zurich, Anapaya Systems
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"flag"
"fmt"
"io"
_ "net/http/pprof"
"os"
"os/user"
"sync/atomic"
"github.com/BurntSushi/toml"
"github.com/syndtr/gocapability/capability"
"github.com/scionproto/scion/go/lib/common"
"github.com/scionproto/scion/go/lib/env"
"github.com/scionproto/scion/go/lib/fatal"
"github.com/scionproto/scion/go/lib/log"
"github.com/scionproto/scion/go/lib/prom"
"github.com/scionproto/scion/go/lib/serrors"
"github.com/scionproto/scion/go/sig/base"
"github.com/scionproto/scion/go/sig/config"
"github.com/scionproto/scion/go/sig/disp"
"github.com/scionproto/scion/go/sig/egress"
"github.com/scionproto/scion/go/sig/ingress"
"github.com/scionproto/scion/go/sig/internal/sigconfig"
"github.com/scionproto/scion/go/sig/metrics"
"github.com/scionproto/scion/go/sig/sigcmn"
"github.com/scionproto/scion/go/sig/xnet"
)
var (
cfg sigconfig.Config
)
func init() {
flag.Usage = env.Usage
}
func main() {
os.Exit(realMain())
}
func realMain() int {
fatal.Init()
env.AddFlags()
flag.Parse()
if v, ok := env.CheckFlags(&cfg); !ok {
return v
}
if err := setupBasic(); err != nil {
fmt.Fprintln(os.Stderr, err)
return 1
}
defer log.Flush()
defer env.LogAppStopped("SIG", cfg.Sig.ID)
defer log.LogPanicAndExit()
if err := validateConfig(); err != nil {
log.Crit("Validation of config failed", "err", err)
return 1
}
// Setup tun early so that we can drop capabilities before interacting with network etc.
tunIO, err := setupTun()
if err != nil {
log.Crit("Unable to create & configure TUN device", "err", err)
return 1
}
if err := sigcmn.Init(cfg.Sig, cfg.Sciond); err != nil {
log.Crit("Error during initialization", err)
return 1
}
env.SetupEnv(
func() {
success := loadConfig(cfg.Sig.SIGConfig)
// Errors already logged in loadConfig
log.Info("reloadOnSIGHUP: reload done", "success", success)
},
)
disp.Init(sigcmn.CtrlConn)
// Parse sig config
if loadConfig(cfg.Sig.SIGConfig) != true {
log.Crit("Unable to load sig config on startup")
return 1
}
// Reply to probes from other SIGs.
go func() {
defer log.LogPanicAndExit()
base.PollReqHdlr()
}()
egress.Init(tunIO)
ingress.Init(tunIO)
cfg.Metrics.StartPrometheus()
select {
case <-fatal.ShutdownChan():
return 0
case <-fatal.FatalChan():
return 1
}
}
// setupBasic loads the config from file and initializes logging.
func setupBasic() error {
// Load and initialize config.
if _, err := toml.DecodeFile(env.ConfigFile(), &cfg); err != nil {
return err
}
cfg.InitDefaults()
if err := env.InitLogging(&cfg.Logging); err != nil {
return err
}
prom.ExportElementID(cfg.Sig.ID)
return env.LogAppStarted("SIG", cfg.Sig.ID)
}
func validateConfig() error {
if err := cfg.Validate(); err != nil {
return err
}
if cfg.Metrics.Prometheus == "" {
cfg.Metrics.Prometheus = "127.0.0.1:1281"
}
return nil
}
func setupTun() (io.ReadWriteCloser, error) {
if err := checkPerms(); err != nil {
return nil, serrors.New("Permissions checks failed")
}
tunLink, tunIO, err := xnet.ConnectTun(cfg.Sig.Tun)
if err != nil {
return nil, err
}
src := cfg.Sig.SrcIP4
if len(src) == 0 && cfg.Sig.IP.To4() != nil {
src = cfg.Sig.IP
}
if err = xnet.AddRoute(cfg.Sig.TunRTableId, tunLink, sigcmn.DefV4Net, src); err != nil {
return nil,
common.NewBasicError("Unable to add default IPv4 route to SIG routing table", err)
}
src = cfg.Sig.SrcIP6
if len(src) == 0 && cfg.Sig.IP.To16() != nil && cfg.Sig.IP.To4() == nil {
src = cfg.Sig.IP
}
if err = xnet.AddRoute(cfg.Sig.TunRTableId, tunLink, sigcmn.DefV6Net, src); err != nil {
return nil,
common.NewBasicError("Unable to add default IPv6 route to SIG routing table", err)
}
// Now that everything is set up, drop CAP_NET_ADMIN
caps, err := capability.NewPid(0)
if err != nil {
return nil, common.NewBasicError("Error retrieving capabilities", err)
}
caps.Clear(capability.CAPS)
caps.Apply(capability.CAPS)
return tunIO, nil
}
func checkPerms() error {
u, err := user.Current()
if err != nil {
return common.NewBasicError("Error retrieving user", err)
}
if u.Uid == "0" {
return serrors.New("Running as root is not allowed for security reasons")
}
caps, err := capability.NewPid(0)
if err != nil {
return common.NewBasicError("Error retrieving capabilities", err)
}
log.Info("Startup capabilities", "caps", caps)
if !caps.Get(capability.EFFECTIVE, capability.CAP_NET_ADMIN) {
return common.NewBasicError("CAP_NET_ADMIN is required", nil, "caps", caps)
}
return nil
}
func loadConfig(path string) bool {
cfg, err := config.LoadFromFile(path)
if err != nil {
log.Error("loadConfig: Failed", "err", err)
return false
}
ok := egress.ReloadConfig(cfg)
if !ok {
return false
}
atomic.StoreUint64(&metrics.ConfigVersion, cfg.ConfigVersion)
return true
}