github.com/etherbanking/go-etherbanking@v1.7.1-0.20181009210156-cf649bca5aba/p2p/discover/udp.go (about) 1 // Copyright 2015 The go-ethereum Authors 2 // This file is part of the go-ethereum library. 3 // 4 // The go-ethereum library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The go-ethereum library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 16 17 package discover 18 19 import ( 20 "bytes" 21 "container/list" 22 "crypto/ecdsa" 23 "errors" 24 "fmt" 25 "net" 26 "time" 27 28 "github.com/etherbanking/go-etherbanking/crypto" 29 "github.com/etherbanking/go-etherbanking/log" 30 "github.com/etherbanking/go-etherbanking/p2p/nat" 31 "github.com/etherbanking/go-etherbanking/p2p/netutil" 32 "github.com/etherbanking/go-etherbanking/rlp" 33 ) 34 35 const Version = 4 36 37 // Errors 38 var ( 39 errPacketTooSmall = errors.New("too small") 40 errBadHash = errors.New("bad hash") 41 errExpired = errors.New("expired") 42 errUnsolicitedReply = errors.New("unsolicited reply") 43 errUnknownNode = errors.New("unknown node") 44 errTimeout = errors.New("RPC timeout") 45 errClockWarp = errors.New("reply deadline too far in the future") 46 errClosed = errors.New("socket closed") 47 ) 48 49 // Timeouts 50 const ( 51 respTimeout = 500 * time.Millisecond 52 sendTimeout = 500 * time.Millisecond 53 expiration = 20 * time.Second 54 55 ntpFailureThreshold = 32 // Continuous timeouts after which to check NTP 56 ntpWarningCooldown = 10 * time.Minute // Minimum amount of time to pass before repeating NTP warning 57 driftThreshold = 10 * time.Second // Allowed clock drift before warning user 58 ) 59 60 // RPC packet types 61 const ( 62 pingPacket = iota + 1 // zero is 'reserved' 63 pongPacket 64 findnodePacket 65 neighborsPacket 66 ) 67 68 // RPC request structures 69 type ( 70 ping struct { 71 Version uint 72 From, To rpcEndpoint 73 Expiration uint64 74 // Ignore additional fields (for forward compatibility). 75 Rest []rlp.RawValue `rlp:"tail"` 76 } 77 78 // pong is the reply to ping. 79 pong struct { 80 // This field should mirror the UDP envelope address 81 // of the ping packet, which provides a way to discover the 82 // the external address (after NAT). 83 To rpcEndpoint 84 85 ReplyTok []byte // This contains the hash of the ping packet. 86 Expiration uint64 // Absolute timestamp at which the packet becomes invalid. 87 // Ignore additional fields (for forward compatibility). 88 Rest []rlp.RawValue `rlp:"tail"` 89 } 90 91 // findnode is a query for nodes close to the given target. 92 findnode struct { 93 Target NodeID // doesn't need to be an actual public key 94 Expiration uint64 95 // Ignore additional fields (for forward compatibility). 96 Rest []rlp.RawValue `rlp:"tail"` 97 } 98 99 // reply to findnode 100 neighbors struct { 101 Nodes []rpcNode 102 Expiration uint64 103 // Ignore additional fields (for forward compatibility). 104 Rest []rlp.RawValue `rlp:"tail"` 105 } 106 107 rpcNode struct { 108 IP net.IP // len 4 for IPv4 or 16 for IPv6 109 UDP uint16 // for discovery protocol 110 TCP uint16 // for RLPx protocol 111 ID NodeID 112 } 113 114 rpcEndpoint struct { 115 IP net.IP // len 4 for IPv4 or 16 for IPv6 116 UDP uint16 // for discovery protocol 117 TCP uint16 // for RLPx protocol 118 } 119 ) 120 121 func makeEndpoint(addr *net.UDPAddr, tcpPort uint16) rpcEndpoint { 122 ip := addr.IP.To4() 123 if ip == nil { 124 ip = addr.IP.To16() 125 } 126 return rpcEndpoint{IP: ip, UDP: uint16(addr.Port), TCP: tcpPort} 127 } 128 129 func (t *udp) nodeFromRPC(sender *net.UDPAddr, rn rpcNode) (*Node, error) { 130 if rn.UDP <= 1024 { 131 return nil, errors.New("low port") 132 } 133 if err := netutil.CheckRelayIP(sender.IP, rn.IP); err != nil { 134 return nil, err 135 } 136 if t.netrestrict != nil && !t.netrestrict.Contains(rn.IP) { 137 return nil, errors.New("not contained in netrestrict whitelist") 138 } 139 n := NewNode(rn.ID, rn.IP, rn.UDP, rn.TCP) 140 err := n.validateComplete() 141 return n, err 142 } 143 144 func nodeToRPC(n *Node) rpcNode { 145 return rpcNode{ID: n.ID, IP: n.IP, UDP: n.UDP, TCP: n.TCP} 146 } 147 148 type packet interface { 149 handle(t *udp, from *net.UDPAddr, fromID NodeID, mac []byte) error 150 name() string 151 } 152 153 type conn interface { 154 ReadFromUDP(b []byte) (n int, addr *net.UDPAddr, err error) 155 WriteToUDP(b []byte, addr *net.UDPAddr) (n int, err error) 156 Close() error 157 LocalAddr() net.Addr 158 } 159 160 // udp implements the RPC protocol. 161 type udp struct { 162 conn conn 163 netrestrict *netutil.Netlist 164 priv *ecdsa.PrivateKey 165 ourEndpoint rpcEndpoint 166 167 addpending chan *pending 168 gotreply chan reply 169 170 closing chan struct{} 171 nat nat.Interface 172 173 *Table 174 } 175 176 // pending represents a pending reply. 177 // 178 // some implementations of the protocol wish to send more than one 179 // reply packet to findnode. in general, any neighbors packet cannot 180 // be matched up with a specific findnode packet. 181 // 182 // our implementation handles this by storing a callback function for 183 // each pending reply. incoming packets from a node are dispatched 184 // to all the callback functions for that node. 185 type pending struct { 186 // these fields must match in the reply. 187 from NodeID 188 ptype byte 189 190 // time when the request must complete 191 deadline time.Time 192 193 // callback is called when a matching reply arrives. if it returns 194 // true, the callback is removed from the pending reply queue. 195 // if it returns false, the reply is considered incomplete and 196 // the callback will be invoked again for the next matching reply. 197 callback func(resp interface{}) (done bool) 198 199 // errc receives nil when the callback indicates completion or an 200 // error if no further reply is received within the timeout. 201 errc chan<- error 202 } 203 204 type reply struct { 205 from NodeID 206 ptype byte 207 data interface{} 208 // loop indicates whether there was 209 // a matching request by sending on this channel. 210 matched chan<- bool 211 } 212 213 // ListenUDP returns a new table that listens for UDP packets on laddr. 214 func ListenUDP(priv *ecdsa.PrivateKey, laddr string, natm nat.Interface, nodeDBPath string, netrestrict *netutil.Netlist) (*Table, error) { 215 addr, err := net.ResolveUDPAddr("udp", laddr) 216 if err != nil { 217 return nil, err 218 } 219 conn, err := net.ListenUDP("udp", addr) 220 if err != nil { 221 return nil, err 222 } 223 tab, _, err := newUDP(priv, conn, natm, nodeDBPath, netrestrict) 224 if err != nil { 225 return nil, err 226 } 227 log.Info("UDP listener up", "self", tab.self) 228 return tab, nil 229 } 230 231 func newUDP(priv *ecdsa.PrivateKey, c conn, natm nat.Interface, nodeDBPath string, netrestrict *netutil.Netlist) (*Table, *udp, error) { 232 233 udp := &udp{ 234 conn: c, 235 priv: priv, 236 netrestrict: netrestrict, 237 closing: make(chan struct{}), 238 gotreply: make(chan reply), 239 addpending: make(chan *pending), 240 } 241 realaddr := c.LocalAddr().(*net.UDPAddr) 242 if natm != nil { 243 if !realaddr.IP.IsLoopback() { 244 go nat.Map(natm, udp.closing, "udp", realaddr.Port, realaddr.Port, "ethereum discovery") 245 } 246 // TODO: react to external IP changes over time. 247 if ext, err := natm.ExternalIP(); err == nil { 248 realaddr = &net.UDPAddr{IP: ext, Port: realaddr.Port} 249 } 250 } 251 // TODO: separate TCP port 252 udp.ourEndpoint = makeEndpoint(realaddr, uint16(realaddr.Port)) 253 tab, err := newTable(udp, PubkeyID(&priv.PublicKey), realaddr, nodeDBPath) 254 if err != nil { 255 return nil, nil, err 256 } 257 udp.Table = tab 258 259 go udp.loop() 260 go udp.readLoop() 261 return udp.Table, udp, nil 262 } 263 264 func (t *udp) close() { 265 close(t.closing) 266 t.conn.Close() 267 // TODO: wait for the loops to end. 268 } 269 270 // ping sends a ping message to the given node and waits for a reply. 271 func (t *udp) ping(toid NodeID, toaddr *net.UDPAddr) error { 272 // TODO: maybe check for ReplyTo field in callback to measure RTT 273 errc := t.pending(toid, pongPacket, func(interface{}) bool { return true }) 274 t.send(toaddr, pingPacket, &ping{ 275 Version: Version, 276 From: t.ourEndpoint, 277 To: makeEndpoint(toaddr, 0), // TODO: maybe use known TCP port from DB 278 Expiration: uint64(time.Now().Add(expiration).Unix()), 279 }) 280 return <-errc 281 } 282 283 func (t *udp) waitping(from NodeID) error { 284 return <-t.pending(from, pingPacket, func(interface{}) bool { return true }) 285 } 286 287 // findnode sends a findnode request to the given node and waits until 288 // the node has sent up to k neighbors. 289 func (t *udp) findnode(toid NodeID, toaddr *net.UDPAddr, target NodeID) ([]*Node, error) { 290 nodes := make([]*Node, 0, bucketSize) 291 nreceived := 0 292 errc := t.pending(toid, neighborsPacket, func(r interface{}) bool { 293 reply := r.(*neighbors) 294 for _, rn := range reply.Nodes { 295 nreceived++ 296 n, err := t.nodeFromRPC(toaddr, rn) 297 if err != nil { 298 log.Trace("Invalid neighbor node received", "ip", rn.IP, "addr", toaddr, "err", err) 299 continue 300 } 301 nodes = append(nodes, n) 302 } 303 return nreceived >= bucketSize 304 }) 305 t.send(toaddr, findnodePacket, &findnode{ 306 Target: target, 307 Expiration: uint64(time.Now().Add(expiration).Unix()), 308 }) 309 err := <-errc 310 return nodes, err 311 } 312 313 // pending adds a reply callback to the pending reply queue. 314 // see the documentation of type pending for a detailed explanation. 315 func (t *udp) pending(id NodeID, ptype byte, callback func(interface{}) bool) <-chan error { 316 ch := make(chan error, 1) 317 p := &pending{from: id, ptype: ptype, callback: callback, errc: ch} 318 select { 319 case t.addpending <- p: 320 // loop will handle it 321 case <-t.closing: 322 ch <- errClosed 323 } 324 return ch 325 } 326 327 func (t *udp) handleReply(from NodeID, ptype byte, req packet) bool { 328 matched := make(chan bool, 1) 329 select { 330 case t.gotreply <- reply{from, ptype, req, matched}: 331 // loop will handle it 332 return <-matched 333 case <-t.closing: 334 return false 335 } 336 } 337 338 // loop runs in its own goroutine. it keeps track of 339 // the refresh timer and the pending reply queue. 340 func (t *udp) loop() { 341 var ( 342 plist = list.New() 343 timeout = time.NewTimer(0) 344 nextTimeout *pending // head of plist when timeout was last reset 345 contTimeouts = 0 // number of continuous timeouts to do NTP checks 346 ntpWarnTime = time.Unix(0, 0) 347 ) 348 <-timeout.C // ignore first timeout 349 defer timeout.Stop() 350 351 resetTimeout := func() { 352 if plist.Front() == nil || nextTimeout == plist.Front().Value { 353 return 354 } 355 // Start the timer so it fires when the next pending reply has expired. 356 now := time.Now() 357 for el := plist.Front(); el != nil; el = el.Next() { 358 nextTimeout = el.Value.(*pending) 359 if dist := nextTimeout.deadline.Sub(now); dist < 2*respTimeout { 360 timeout.Reset(dist) 361 return 362 } 363 // Remove pending replies whose deadline is too far in the 364 // future. These can occur if the system clock jumped 365 // backwards after the deadline was assigned. 366 nextTimeout.errc <- errClockWarp 367 plist.Remove(el) 368 } 369 nextTimeout = nil 370 timeout.Stop() 371 } 372 373 for { 374 resetTimeout() 375 376 select { 377 case <-t.closing: 378 for el := plist.Front(); el != nil; el = el.Next() { 379 el.Value.(*pending).errc <- errClosed 380 } 381 return 382 383 case p := <-t.addpending: 384 p.deadline = time.Now().Add(respTimeout) 385 plist.PushBack(p) 386 387 case r := <-t.gotreply: 388 var matched bool 389 for el := plist.Front(); el != nil; el = el.Next() { 390 p := el.Value.(*pending) 391 if p.from == r.from && p.ptype == r.ptype { 392 matched = true 393 // Remove the matcher if its callback indicates 394 // that all replies have been received. This is 395 // required for packet types that expect multiple 396 // reply packets. 397 if p.callback(r.data) { 398 p.errc <- nil 399 plist.Remove(el) 400 } 401 // Reset the continuous timeout counter (time drift detection) 402 contTimeouts = 0 403 } 404 } 405 r.matched <- matched 406 407 case now := <-timeout.C: 408 nextTimeout = nil 409 410 // Notify and remove callbacks whose deadline is in the past. 411 for el := plist.Front(); el != nil; el = el.Next() { 412 p := el.Value.(*pending) 413 if now.After(p.deadline) || now.Equal(p.deadline) { 414 p.errc <- errTimeout 415 plist.Remove(el) 416 contTimeouts++ 417 } 418 } 419 // If we've accumulated too many timeouts, do an NTP time sync check 420 if contTimeouts > ntpFailureThreshold { 421 if time.Since(ntpWarnTime) >= ntpWarningCooldown { 422 ntpWarnTime = time.Now() 423 go checkClockDrift() 424 } 425 contTimeouts = 0 426 } 427 } 428 } 429 } 430 431 const ( 432 macSize = 256 / 8 433 sigSize = 520 / 8 434 headSize = macSize + sigSize // space of packet frame data 435 ) 436 437 var ( 438 headSpace = make([]byte, headSize) 439 440 // Neighbors replies are sent across multiple packets to 441 // stay below the 1280 byte limit. We compute the maximum number 442 // of entries by stuffing a packet until it grows too large. 443 maxNeighbors int 444 ) 445 446 func init() { 447 448 p := neighbors{Expiration: ^uint64(0)} 449 maxSizeNode := rpcNode{IP: make(net.IP, 16), UDP: ^uint16(0), TCP: ^uint16(0)} 450 for n := 0; ; n++ { 451 p.Nodes = append(p.Nodes, maxSizeNode) 452 size, _, err := rlp.EncodeToReader(p) 453 if err != nil { 454 // If this ever happens, it will be caught by the unit tests. 455 panic("cannot encode: " + err.Error()) 456 } 457 if headSize+size+1 >= 1280 { 458 maxNeighbors = n 459 break 460 } 461 } 462 } 463 464 func (t *udp) send(toaddr *net.UDPAddr, ptype byte, req packet) error { 465 packet, err := encodePacket(t.priv, ptype, req) 466 if err != nil { 467 return err 468 } 469 _, err = t.conn.WriteToUDP(packet, toaddr) 470 log.Trace(">> "+req.name(), "addr", toaddr, "err", err) 471 return err 472 } 473 474 func encodePacket(priv *ecdsa.PrivateKey, ptype byte, req interface{}) ([]byte, error) { 475 b := new(bytes.Buffer) 476 b.Write(headSpace) 477 b.WriteByte(ptype) 478 if err := rlp.Encode(b, req); err != nil { 479 log.Error("Can't encode discv4 packet", "err", err) 480 return nil, err 481 } 482 packet := b.Bytes() 483 sig, err := crypto.Sign(crypto.Keccak256(packet[headSize:]), priv) 484 if err != nil { 485 log.Error("Can't sign discv4 packet", "err", err) 486 return nil, err 487 } 488 copy(packet[macSize:], sig) 489 // add the hash to the front. Note: this doesn't protect the 490 // packet in any way. Our public key will be part of this hash in 491 // The future. 492 copy(packet, crypto.Keccak256(packet[macSize:])) 493 return packet, nil 494 } 495 496 // readLoop runs in its own goroutine. it handles incoming UDP packets. 497 func (t *udp) readLoop() { 498 defer t.conn.Close() 499 // Discovery packets are defined to be no larger than 1280 bytes. 500 // Packets larger than this size will be cut at the end and treated 501 // as invalid because their hash won't match. 502 buf := make([]byte, 1280) 503 for { 504 nbytes, from, err := t.conn.ReadFromUDP(buf) 505 if netutil.IsTemporaryError(err) { 506 // Ignore temporary read errors. 507 log.Debug("Temporary UDP read error", "err", err) 508 continue 509 } else if err != nil { 510 // Shut down the loop for permament errors. 511 log.Debug("UDP read error", "err", err) 512 return 513 } 514 t.handlePacket(from, buf[:nbytes]) 515 } 516 } 517 518 func (t *udp) handlePacket(from *net.UDPAddr, buf []byte) error { 519 packet, fromID, hash, err := decodePacket(buf) 520 if err != nil { 521 log.Debug("Bad discv4 packet", "addr", from, "err", err) 522 return err 523 } 524 err = packet.handle(t, from, fromID, hash) 525 log.Trace("<< "+packet.name(), "addr", from, "err", err) 526 return err 527 } 528 529 func decodePacket(buf []byte) (packet, NodeID, []byte, error) { 530 if len(buf) < headSize+1 { 531 return nil, NodeID{}, nil, errPacketTooSmall 532 } 533 hash, sig, sigdata := buf[:macSize], buf[macSize:headSize], buf[headSize:] 534 shouldhash := crypto.Keccak256(buf[macSize:]) 535 if !bytes.Equal(hash, shouldhash) { 536 return nil, NodeID{}, nil, errBadHash 537 } 538 fromID, err := recoverNodeID(crypto.Keccak256(buf[headSize:]), sig) 539 if err != nil { 540 return nil, NodeID{}, hash, err 541 } 542 var req packet 543 switch ptype := sigdata[0]; ptype { 544 case pingPacket: 545 req = new(ping) 546 case pongPacket: 547 req = new(pong) 548 case findnodePacket: 549 req = new(findnode) 550 case neighborsPacket: 551 req = new(neighbors) 552 default: 553 return nil, fromID, hash, fmt.Errorf("unknown type: %d", ptype) 554 } 555 s := rlp.NewStream(bytes.NewReader(sigdata[1:]), 0) 556 err = s.Decode(req) 557 return req, fromID, hash, err 558 } 559 560 func (req *ping) handle(t *udp, from *net.UDPAddr, fromID NodeID, mac []byte) error { 561 if expired(req.Expiration) { 562 return errExpired 563 } 564 t.send(from, pongPacket, &pong{ 565 To: makeEndpoint(from, req.From.TCP), 566 ReplyTok: mac, 567 Expiration: uint64(time.Now().Add(expiration).Unix()), 568 }) 569 if !t.handleReply(fromID, pingPacket, req) { 570 // Note: we're ignoring the provided IP address right now 571 go t.bond(true, fromID, from, req.From.TCP) 572 } 573 return nil 574 } 575 576 func (req *ping) name() string { return "PING/v4" } 577 578 func (req *pong) handle(t *udp, from *net.UDPAddr, fromID NodeID, mac []byte) error { 579 if expired(req.Expiration) { 580 return errExpired 581 } 582 if !t.handleReply(fromID, pongPacket, req) { 583 return errUnsolicitedReply 584 } 585 return nil 586 } 587 588 func (req *pong) name() string { return "PONG/v4" } 589 590 func (req *findnode) handle(t *udp, from *net.UDPAddr, fromID NodeID, mac []byte) error { 591 if expired(req.Expiration) { 592 return errExpired 593 } 594 if t.db.node(fromID) == nil { 595 // No bond exists, we don't process the packet. This prevents 596 // an attack vector where the discovery protocol could be used 597 // to amplify traffic in a DDOS attack. A malicious actor 598 // would send a findnode request with the IP address and UDP 599 // port of the target as the source address. The recipient of 600 // the findnode packet would then send a neighbors packet 601 // (which is a much bigger packet than findnode) to the victim. 602 return errUnknownNode 603 } 604 target := crypto.Keccak256Hash(req.Target[:]) 605 t.mutex.Lock() 606 closest := t.closest(target, bucketSize).entries 607 t.mutex.Unlock() 608 609 p := neighbors{Expiration: uint64(time.Now().Add(expiration).Unix())} 610 // Send neighbors in chunks with at most maxNeighbors per packet 611 // to stay below the 1280 byte limit. 612 for i, n := range closest { 613 if netutil.CheckRelayIP(from.IP, n.IP) != nil { 614 continue 615 } 616 p.Nodes = append(p.Nodes, nodeToRPC(n)) 617 if len(p.Nodes) == maxNeighbors || i == len(closest)-1 { 618 t.send(from, neighborsPacket, &p) 619 p.Nodes = p.Nodes[:0] 620 } 621 } 622 return nil 623 } 624 625 func (req *findnode) name() string { return "FINDNODE/v4" } 626 627 func (req *neighbors) handle(t *udp, from *net.UDPAddr, fromID NodeID, mac []byte) error { 628 if expired(req.Expiration) { 629 return errExpired 630 } 631 if !t.handleReply(fromID, neighborsPacket, req) { 632 return errUnsolicitedReply 633 } 634 return nil 635 } 636 637 func (req *neighbors) name() string { return "NEIGHBORS/v4" } 638 639 func expired(ts uint64) bool { 640 return time.Unix(int64(ts), 0).Before(time.Now()) 641 }