forked from scionproto/scion
-
Notifications
You must be signed in to change notification settings - Fork 1
/
validator.go
101 lines (90 loc) · 3.11 KB
/
validator.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
// Copyright 2019 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.
package registration
import (
"github.com/scionproto/scion/go/lib/addr"
"github.com/scionproto/scion/go/lib/common"
"github.com/scionproto/scion/go/lib/ctrl/path_mgmt"
"github.com/scionproto/scion/go/lib/ctrl/seg"
"github.com/scionproto/scion/go/lib/hiddenpath"
"github.com/scionproto/scion/go/proto"
)
const (
ErrNoSegments common.ErrMsg = "no segments"
ErrMissingExtn common.ErrMsg = "missing HiddenPathSeg extension"
ErrWrongSegType common.ErrMsg = "segment must be an up- or down-segment"
ErrUnknownGroup common.ErrMsg = "group not known to HPS"
ErrNotRegistry common.ErrMsg = "HPS is not a Registry of this group"
ErrNotWriter common.ErrMsg = "peer is not a writer of this group"
ErrNotReader common.ErrMsg = "peer is not a reader of this group"
)
var _ Validator = (*DefaultValidator)(nil)
// DefaultValidator validates hidden path registrations based on hidden path group configurations
type DefaultValidator struct {
localIA addr.IA
groups map[hiddenpath.GroupId]*hiddenpath.Group
}
// NewDefaultValidator creates a new DefaultValidator
func NewDefaultValidator(localIA addr.IA,
groups map[hiddenpath.GroupId]*hiddenpath.Group) *DefaultValidator {
return &DefaultValidator{
localIA: localIA,
groups: groups,
}
}
// Validate validates a hpSegReg with regard to the provided HP Group
func (v *DefaultValidator) Validate(hpSegReg *path_mgmt.HPSegReg, peer addr.IA) error {
id := hiddenpath.IdFromMsg(hpSegReg.GroupId)
if err := v.checkGroupPermissions(id, peer); err != nil {
return common.NewBasicError("Group configuration error", err, "group", id)
}
if err := v.checkSegments(hpSegReg.Recs); err != nil {
return common.NewBasicError("Invalid hidden segment", err)
}
return nil
}
func (v *DefaultValidator) checkGroupPermissions(groupId hiddenpath.GroupId, peer addr.IA) error {
group, ok := v.groups[groupId]
if !ok {
return ErrUnknownGroup
}
if !group.HasRegistry(v.localIA) {
return ErrNotRegistry
}
if peer != group.Owner && !group.HasWriter(peer) {
return ErrNotWriter
}
return nil
}
func (v *DefaultValidator) checkSegments(recs []*seg.Meta) error {
for _, seg := range recs {
if !checkHiddenSegExtn(seg) {
return ErrMissingExtn
}
if seg.Type != proto.PathSegType_up && seg.Type != proto.PathSegType_down {
return ErrWrongSegType
}
}
return nil
}
func checkHiddenSegExtn(s *seg.Meta) bool {
if s.Segment.MaxAEIdx() < 0 {
return false
}
lastASEntry := s.Segment.ASEntries[s.Segment.MaxAEIdx()]
if lastASEntry.Exts.HiddenPathSeg == nil {
return false
}
return lastASEntry.Exts.HiddenPathSeg.Set
}