/
iphost.go
230 lines (207 loc) · 5.42 KB
/
iphost.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
228
/*
MIT License
Copyright (c) 2017 Simon Schmidt
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
package ip
import "net"
import "time"
import "sync"
import "container/list"
var bitbytes = [8]byte{
0,0x80,0xc0,0xe0,0xf0,
0xf8,0xfc,0xfe,
}
type IPv6Prefix struct{
IP [16]byte
Len uint8
}
type IPv6PrefixEntry struct{
Prefix IPv6Prefix
Lifetime uint32
Tstamp time.Time
Onlink bool
Slaac bool
ListSync sync.Mutex
List list.List /* a list of Key6 values */
}
type IPv6AddressEntry struct{
Unicast, SolicitedMulticast Key6
Tentative bool
/*
* The prefix this IPv6 Address was derived from, if any.
*
* This field is nil for link local addresses.
*/
Prefix *IPv6PrefixEntry
}
type IPv4AddressEntry struct{
Addr, Subnetmask, Gateway Key4
}
type IPHost struct {
sync.RWMutex
V4 map[Key4]*IPv4AddressEntry
V6 map[Key6]*IPv6AddressEntry
S6 map[Key6]*IPv6AddressEntry
Prefix6 map[IPv6Prefix]*IPv6PrefixEntry
}
func (i *IPHost) Init() *IPHost{
i.V4 = make(map[Key4]*IPv4AddressEntry)
i.V6 = make(map[Key6]*IPv6AddressEntry)
i.S6 = make(map[Key6]*IPv6AddressEntry)
i.Prefix6 = make(map[IPv6Prefix]*IPv6PrefixEntry)
return i
}
func (i *IPHost) input4(targ net.IP) (my bool) {
var i4 Key4
i4.Decode(targ)
if i4==0xFFFFFFFF { return true }
i.RLock(); defer i.RUnlock()
_,my = i.V4[i4]
return
}
func (i *IPHost) input6(targ net.IP) (my bool) {
var i6 Key6
if targ[0]==0xff { /* IF address is multicast */
switch targ[1]&0xf { /* Multicast Scope. */
/*
* RFC 4291 2.7
*
* Nodes must not originate a packet to a multicast address whose scop
* field contains the reserved value 0; if such a packet is received, it
* must be silently dropped.
*/
// (Case 0)
/*
* RFC 4291 - Errata ID: 3480
*
* Section 2.7 says:
* Interface-Local scope spans only a single interface on a node
* and is useful only for loopback transmission of multicast.
*
* It should say:
* Interface-Local scope spans only a single interface on a node
* and is useful only for loopback transmission of multicast.
* Packets with interface-local scope received from another node
* must be discarded.
*
* It should be explicitly stated that interface-local scoped multicast packets
* received from the link must be discarded.
* The BSD implementation currently does this, but not Linux.
* http://www.ietf.org/mail-archive/web/ipv6/current/msg17154.html
*/
// (Case 1)
case 0,1: return false
default: return true
}
}
i.RLock(); defer i.RUnlock()
i6.Decode(targ)
_,my = i.V6[i6]
return
}
func (i *IPHost) GetTarget6(targ net.IP) (obj *IPv6AddressEntry, my bool) {
var i6 Key6
i6.Decode(targ)
i.RLock(); defer i.RUnlock()
obj,my = i.V6[i6]
return
}
func (i *IPHost) Input(targ net.IP) (my bool) {
switch len(targ) {
case 4:
my = i.input4(targ)
case 16:
my = i.input6(targ)
}
return
}
func (i *IPHost) SlaacFailedV6(addr *IPv6AddressEntry) {
i.Lock(); defer i.Unlock()
delete(i.V6,addr.Unicast)
delete(i.S6,addr.SolicitedMulticast)
}
func (i *IPHost) addIP4Addr(ip, sn, gw net.IP) {
var i4,s4,g4 Key4
i4.Decode(ip)
i.Lock(); defer i.Unlock()
addr,_ := i.V4[i4]
if addr!=nil { return }
if len(sn)==4 {
s4.Decode(sn)
}else{
s4 = 0xFFFFFF00
}
if len(gw)==4 {
g4.Decode(gw)
}else{
g4 = i4&0xFFFFFF00
}
addr = &IPv4AddressEntry{i4,s4,g4}
i.V4[i4] = addr
i.V4[i4|^s4] = addr
}
func (i *IPHost) addIP6Addr(ip net.IP) {
var i6,m6 Key6
i6.Decode(ip)
i.Lock(); defer i.Unlock()
addr,_ := i.V6[i6]
if addr!=nil { return }
/* Solicited Multicast address */
m6.Hi = 0xff02000000000000
m6.Lo = 0x00000001ff000000|(i6.Lo&0xffffff)
addr = new(IPv6AddressEntry)
addr.Unicast = i6
addr.SolicitedMulticast = m6
i.V6[i6] = addr
i.S6[m6] = addr
}
func (i *IPHost) AddIPAddr(ip net.IP) {
switch len(ip) {
case 4:
i.addIP4Addr(ip,nil,nil)
case 16:
i.addIP6Addr(ip)
}
}
func (i *IPHost) extractPrefixes() []IPv6Prefix {
i.RLock(); defer i.RUnlock()
px := make([]IPv6Prefix,0,len(i.Prefix6))
for k := range i.Prefix6 { px = append(px,k) }
return px
}
func (i *IPHost) IsOnLink(ip net.IP) bool {
if ip[0]==0xfe &&
ip[1]==0x80 &&
ip[2]==0 &&
ip[3]==0 &&
ip[4]==0 &&
ip[5]==0 &&
ip[6]==0 &&
ip[7]==0 { return true }
i.RLock(); defer i.RUnlock()
for px,pobj := range i.Prefix6 {
eq := byte(0)
lng := int(px.Len)
hi := lng>>3
lo := lng&7
for i:=0 ; i<hi; i++ { eq |= px.IP[i]^ip[i] }
if lo!=0 { eq |= (px.IP[hi]^ip[hi]) & bitbytes[lo] }
if eq==0 && pobj.Onlink { return true }
}
return false
}