forked from scionproto/scion
/
gen.go
162 lines (151 loc) · 5.5 KB
/
gen.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
// Copyright 2017 ETH Zurich
//
// 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.
// This file provides a generic interface to periodically generate SCION packets
// and send them out.
package main
import (
"time"
log "github.com/inconshreveable/log15"
"github.com/scionproto/scion/go/border/rcmn"
"github.com/scionproto/scion/go/border/rctx"
"github.com/scionproto/scion/go/border/rpkt"
"github.com/scionproto/scion/go/lib/addr"
"github.com/scionproto/scion/go/lib/common"
"github.com/scionproto/scion/go/lib/ctrl"
"github.com/scionproto/scion/go/lib/ctrl/ifid"
"github.com/scionproto/scion/go/lib/ctrl/path_mgmt"
"github.com/scionproto/scion/go/lib/l4"
"github.com/scionproto/scion/go/lib/log"
"github.com/scionproto/scion/go/lib/overlay"
"github.com/scionproto/scion/go/lib/spkt"
"github.com/scionproto/scion/go/lib/topology"
)
const (
// ifIDFreq is how often IFID packets are sent to the neighbouring AS.
ifIDFreq = 1 * time.Second
// ifStateFreq is how often the router will request an Interface State update
// from the beacon service.
ifStateFreq = 30 * time.Second
)
// genPkt is a generic function to generate packets that originate at the router.
func (r *Router) genPkt(dstIA *addr.ISD_AS, dstHost addr.HostAddr, dstL4Port int,
srcAddr *topology.AddrInfo, pld common.Payload) error {
ctx := rctx.Get()
dirTo := rcmn.DirExternal
if dstIA.Eq(ctx.Conf.IA) {
dirTo = rcmn.DirLocal
}
// Create base packet
sp := &spkt.ScnPkt{
DstIA: dstIA, SrcIA: ctx.Conf.IA, DstHost: dstHost, SrcHost: addr.HostFromIP(srcAddr.IP),
}
// TODO(klausman, kormat): What if it isn't UDP? Handle other overlay types
if srcAddr.Overlay.IsUDP() {
sp.L4 = &l4.UDP{SrcPort: uint16(srcAddr.L4Port), DstPort: uint16(dstL4Port)}
}
rp, err := rpkt.RtrPktFromScnPkt(sp, dirTo, ctx)
if err != nil {
return err
}
if err = rp.SetPld(pld); err != nil {
return err
}
if dstIA.Eq(ctx.Conf.IA) {
if dstHost.Type() == addr.HostTypeSVC {
if _, err := rp.RouteResolveSVC(); err != nil {
return err
}
} else {
ai := &topology.AddrInfo{Overlay: srcAddr.Overlay, IP: dstHost.IP(), L4Port: dstL4Port}
if srcAddr.Overlay.IsUDP() {
ai.OverlayPort = overlay.EndhostPort
}
rp.Egress = append(rp.Egress, rpkt.EgressPair{S: ctx.LocSockOut[0], Dst: ai})
}
} else {
ifid, ok := ctx.Conf.Net.IFAddrMap[srcAddr.Key()]
if !ok {
return common.NewBasicError("genPkt: unable to find ifid for address",
nil, "addr", srcAddr)
}
rp.Egress = append(rp.Egress, rpkt.EgressPair{S: ctx.ExtSockOut[ifid]})
}
return rp.Route()
}
// SyncInterface handles generating periodic Interface ID (IFID) packets that are
// sent to the Beacon Service in the neighbouring AS. These function as both
// keep-alives, and to inform the neighbour of the local interface ID.
func (r *Router) SyncInterface() {
defer liblog.LogPanicAndExit()
for range time.Tick(ifIDFreq) {
ctx := rctx.Get()
for ifid := range ctx.Conf.Net.IFs {
r.genIFIDPkt(ifid, ctx)
}
}
}
// genIFIDPkt generates an IFID-packet for the specified interface.
func (r *Router) genIFIDPkt(ifID common.IFIDType, ctx *rctx.Ctx) {
logger := log.New("ifid", ifID)
intf := ctx.Conf.Net.IFs[ifID]
srcAddr := intf.IFAddr.PublicAddrInfo(intf.IFAddr.Overlay)
cpld, err := ctrl.NewPld(&ifid.IFID{OrigIfID: uint64(ifID)}, nil)
if err != nil {
logger.Error("Error generating IFID Ctrl payload", "err", err)
return
}
scpld, err := cpld.SignedPld(ctrl.NullSigner)
if err != nil {
logger.Error("Error generating IFID signed Ctrl payload", "err", err)
return
}
if err := r.genPkt(intf.RemoteIA, addr.HostFromIP(intf.RemoteAddr.IP),
intf.RemoteAddr.L4Port, srcAddr, scpld); err != nil {
logger.Error("Error generating IFID packet", "err", err)
}
}
// IFStateUpdate handles generating periodic Interface State Request (IFStateReq)
// packets that are sent to the local Beacon Service (BS), as well as
// processing the Interface State updates. IFStateReqs are mostly needed on
// startup, to make sure the border router is aware of the status of the local
// interfaces. The BS normally updates the border routers everytime an
// interface state changes, so this is only needed as a fail-safe after
// startup.
func (r *Router) IFStateUpdate() {
defer liblog.LogPanicAndExit()
r.genIFStateReq()
for range time.Tick(ifStateFreq) {
r.genIFStateReq()
}
}
// genIFStateReq generates an Interface State request packet to the local
// beacon service.
func (r *Router) genIFStateReq() {
ctx := rctx.Get()
// Pick first local address from topology as source.
srcAddr := ctx.Conf.Net.LocAddr[0].PublicAddrInfo(ctx.Conf.Net.LocAddr[0].Overlay)
cpld, err := ctrl.NewPathMgmtPld(&path_mgmt.IFStateReq{}, nil, nil)
if err != nil {
log.Error("Error generating IFStateReq Ctrl payload", "err", err)
return
}
scpld, err := cpld.SignedPld(ctrl.NullSigner)
if err != nil {
log.Error("Error generating IFStateReq signed Ctrl payload", "err", err)
return
}
if err := r.genPkt(ctx.Conf.IA, addr.SvcBS.Multicast(), 0, srcAddr, scpld); err != nil {
log.Error("Error generating IFStateReq packet", "err", err)
}
}