2024-04-14 18:29:29 +10:00
|
|
|
/*
|
|
|
|
Copyright 2024 Josh Deprez
|
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2024-04-19 14:57:25 +10:00
|
|
|
package router
|
2024-04-14 18:29:29 +10:00
|
|
|
|
|
|
|
import (
|
2024-05-03 16:13:59 +10:00
|
|
|
"context"
|
2024-04-14 18:29:29 +10:00
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
|
2024-10-18 13:20:34 +11:00
|
|
|
"drjosh.dev/jrouter/atalk"
|
|
|
|
"drjosh.dev/jrouter/atalk/nbp"
|
2024-04-14 18:29:29 +10:00
|
|
|
"github.com/sfiera/multitalk/pkg/ddp"
|
|
|
|
)
|
|
|
|
|
2024-05-03 16:13:59 +10:00
|
|
|
func (port *EtherTalkPort) HandleNBP(ctx context.Context, ddpkt *ddp.ExtPacket) error {
|
2024-04-14 18:29:29 +10:00
|
|
|
if ddpkt.Proto != ddp.ProtoNBP {
|
|
|
|
return fmt.Errorf("invalid DDP type %d on socket 2", ddpkt.Proto)
|
|
|
|
}
|
|
|
|
|
|
|
|
nbpkt, err := nbp.Unmarshal(ddpkt.Data)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("invalid packet: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("NBP: Got %v id %d with tuples %v", nbpkt.Function, nbpkt.NBPID, nbpkt.Tuples)
|
|
|
|
|
|
|
|
switch nbpkt.Function {
|
|
|
|
case nbp.FunctionLkUp:
|
|
|
|
// when in AppleTalk, do as Apple Internet Router does...
|
2024-05-03 18:32:12 +10:00
|
|
|
outDDP, err := port.helloWorldThisIsMe(nbpkt.NBPID, &nbpkt.Tuples[0])
|
2024-04-21 17:47:58 +10:00
|
|
|
if err != nil || outDDP == nil {
|
2024-04-14 18:29:29 +10:00
|
|
|
return err
|
|
|
|
}
|
2024-04-21 17:53:47 +10:00
|
|
|
log.Print("NBP: Replying to LkUp with LkUp-Reply for myself")
|
2024-05-03 16:13:59 +10:00
|
|
|
// Note: AARP can block
|
|
|
|
return port.Send(ctx, outDDP)
|
|
|
|
|
|
|
|
case nbp.FunctionFwdReq:
|
2024-05-03 18:32:12 +10:00
|
|
|
return port.Router.handleNBPFwdReq(ctx, ddpkt, nbpkt)
|
2024-04-14 18:29:29 +10:00
|
|
|
|
|
|
|
case nbp.FunctionBrRq:
|
2024-05-03 16:13:59 +10:00
|
|
|
return port.handleNBPBrRq(ctx, ddpkt, nbpkt)
|
2024-04-21 17:47:58 +10:00
|
|
|
|
2024-05-03 16:13:59 +10:00
|
|
|
default:
|
|
|
|
return fmt.Errorf("TODO: handle function %v", nbpkt.Function)
|
|
|
|
}
|
|
|
|
}
|
2024-04-15 18:08:45 +10:00
|
|
|
|
2024-05-03 16:13:59 +10:00
|
|
|
func (port *EtherTalkPort) handleNBPBrRq(ctx context.Context, ddpkt *ddp.ExtPacket, nbpkt *nbp.Packet) error {
|
2024-05-03 18:32:12 +10:00
|
|
|
// A BrRq was addressed to us. The sender (on a local network) is aware that
|
|
|
|
// the network is extended and routed, and instead of broadcasting a LkUp
|
|
|
|
// itself, is asking us to do it.
|
|
|
|
|
2024-05-03 16:13:59 +10:00
|
|
|
// There must be 1!
|
|
|
|
tuple := &nbpkt.Tuples[0]
|
|
|
|
|
2024-05-03 18:32:12 +10:00
|
|
|
// This logic would be required on a non-extended network:
|
|
|
|
// if tuple.Zone == "" || tuple.Zone == "*" {
|
|
|
|
// tuple.Zone = port.DefaultZoneName
|
|
|
|
// }
|
2024-04-15 18:08:45 +10:00
|
|
|
|
2024-05-05 17:01:23 +10:00
|
|
|
routes := port.Router.RouteTable.RoutesForZone(tuple.Zone)
|
2024-05-03 16:13:59 +10:00
|
|
|
|
2024-05-05 17:01:23 +10:00
|
|
|
for _, route := range routes {
|
|
|
|
if outPort := route.EtherTalkDirect; outPort != nil {
|
2024-05-03 16:13:59 +10:00
|
|
|
// If it's for a local zone, translate it to a LkUp and broadcast
|
|
|
|
// out the corresponding EtherTalk port.
|
|
|
|
// "Note: On an internet, nodes on extended networks performing lookups in
|
|
|
|
// their own zone must replace a zone name of asterisk (*) with their actual
|
|
|
|
// zone name before sending the packet to A-ROUTER. All nodes performing
|
|
|
|
// lookups in their own zone will receive LkUp packets from themselves
|
|
|
|
// (actually sent by a router). The node's NBP process should expect to
|
|
|
|
// receive these packets and must reply to them."
|
|
|
|
nbpkt.Function = nbp.FunctionLkUp
|
2024-04-15 18:08:45 +10:00
|
|
|
nbpRaw, err := nbpkt.Marshal()
|
|
|
|
if err != nil {
|
2024-05-03 16:13:59 +10:00
|
|
|
return fmt.Errorf("couldn't marshal LkUp: %v", err)
|
2024-04-15 18:08:45 +10:00
|
|
|
}
|
|
|
|
|
2024-04-23 13:22:43 +10:00
|
|
|
outDDP := ddp.ExtPacket{
|
|
|
|
ExtHeader: ddp.ExtHeader{
|
|
|
|
Size: atalk.DDPExtHeaderSize + uint16(len(nbpRaw)),
|
|
|
|
Cksum: 0,
|
2024-05-04 16:46:44 +10:00
|
|
|
SrcNet: port.MyAddr.Network,
|
|
|
|
SrcNode: port.MyAddr.Node,
|
|
|
|
SrcSocket: 2,
|
2024-05-03 16:13:59 +10:00
|
|
|
DstNet: 0x0000, // Local network broadcast
|
|
|
|
DstNode: 0xFF, // Broadcast node address within the dest network
|
2024-04-23 13:22:43 +10:00
|
|
|
DstSocket: 2,
|
2024-04-23 13:23:04 +10:00
|
|
|
Proto: ddp.ProtoNBP,
|
2024-04-23 13:22:43 +10:00
|
|
|
},
|
|
|
|
Data: nbpRaw,
|
|
|
|
}
|
2024-04-15 18:08:45 +10:00
|
|
|
|
2024-05-03 16:13:59 +10:00
|
|
|
log.Printf("NBP: zone multicasting LkUp for tuple %v", tuple)
|
|
|
|
if err := outPort.ZoneMulticast(tuple.Zone, &outDDP); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// But also...if we match the query, reply as though it was a LkUp
|
|
|
|
// This uses the *input* port information.
|
2024-05-03 18:32:12 +10:00
|
|
|
outDDP2, err := port.helloWorldThisIsMe(nbpkt.NBPID, tuple)
|
2024-04-15 18:08:45 +10:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-05-03 16:13:59 +10:00
|
|
|
if outDDP2 == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
log.Print("NBP: Replying to BrRq directly with LkUp-Reply for myself")
|
2024-05-03 18:32:12 +10:00
|
|
|
// Can reply to this BrRq on the same port we got it, because it
|
|
|
|
// wasn't routed
|
2024-05-03 16:13:59 +10:00
|
|
|
if err := port.Send(ctx, outDDP2); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-04-15 18:08:45 +10:00
|
|
|
|
2024-05-03 16:13:59 +10:00
|
|
|
continue
|
|
|
|
}
|
2024-04-23 11:46:39 +10:00
|
|
|
|
2024-05-03 16:13:59 +10:00
|
|
|
// The zone table row is *not* for a local network.
|
|
|
|
// Translate it into a FwdReq and route that to the routers that do have
|
|
|
|
// that zone as a local network.
|
|
|
|
nbpkt.Function = nbp.FunctionFwdReq
|
|
|
|
nbpRaw, err := nbpkt.Marshal()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("couldn't marshal FwdReq: %v", err)
|
2024-04-14 18:29:29 +10:00
|
|
|
}
|
|
|
|
|
2024-05-03 16:13:59 +10:00
|
|
|
outDDP := &ddp.ExtPacket{
|
|
|
|
ExtHeader: ddp.ExtHeader{
|
|
|
|
Size: atalk.DDPExtHeaderSize + uint16(len(nbpRaw)),
|
|
|
|
Cksum: 0,
|
|
|
|
SrcNet: ddpkt.SrcNet,
|
|
|
|
SrcNode: ddpkt.SrcNode,
|
|
|
|
SrcSocket: ddpkt.SrcSocket,
|
2024-05-05 17:01:23 +10:00
|
|
|
DstNet: route.NetStart,
|
2024-05-03 16:13:59 +10:00
|
|
|
DstNode: 0x00, // Any router for the dest network
|
|
|
|
DstSocket: 2,
|
|
|
|
Proto: ddp.ProtoNBP,
|
|
|
|
},
|
|
|
|
Data: nbpRaw,
|
|
|
|
}
|
2024-04-15 18:09:43 +10:00
|
|
|
|
2024-05-04 15:36:33 +10:00
|
|
|
if err := port.Router.Output(ctx, outDDP); err != nil {
|
2024-05-03 16:13:59 +10:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2024-04-15 18:09:43 +10:00
|
|
|
return nil
|
2024-04-14 18:29:29 +10:00
|
|
|
}
|
2024-04-21 17:47:58 +10:00
|
|
|
|
2024-05-04 16:46:44 +10:00
|
|
|
func (rtr *Router) handleNBPFwdReq(ctx context.Context, ddpkt *ddp.ExtPacket, nbpkt *nbp.Packet) error {
|
|
|
|
// A FwdReq was addressed to us. That means a remote router thinks the
|
|
|
|
// zone is available on one or more of our local networks.
|
|
|
|
|
|
|
|
// There must be 1!
|
|
|
|
tuple := &nbpkt.Tuples[0]
|
|
|
|
|
|
|
|
for _, outPort := range rtr.Ports {
|
2024-05-05 17:59:49 +10:00
|
|
|
if !outPort.AvailableZones.Contains(tuple.Zone) {
|
2024-05-04 16:46:44 +10:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
log.Printf("NBP: Converting FwdReq to LkUp (%v)", tuple)
|
|
|
|
|
|
|
|
// Convert it to a LkUp and broadcast on the corresponding port
|
|
|
|
nbpkt.Function = nbp.FunctionLkUp
|
|
|
|
nbpRaw, err := nbpkt.Marshal()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("couldn't marshal LkUp: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Inside AppleTalk SE, pp 8-20:
|
|
|
|
// "If the destination network is extended, however, the router must also
|
|
|
|
// change the destination network number to $0000, so that the packet is
|
|
|
|
// received by all nodes on the network (within the correct zone multicast
|
|
|
|
// address)."
|
|
|
|
ddpkt.DstNet = 0x0000
|
|
|
|
ddpkt.DstNode = 0xFF // Broadcast node address within the dest network
|
|
|
|
ddpkt.Data = nbpRaw
|
|
|
|
|
|
|
|
if err := outPort.ZoneMulticast(tuple.Zone, ddpkt); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// But also... if it matches us, reply directly with a LkUp-Reply of our own
|
|
|
|
outDDP, err := outPort.helloWorldThisIsMe(nbpkt.NBPID, tuple)
|
|
|
|
if err != nil || outDDP == nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := rtr.Output(ctx, outDDP); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-05-03 16:13:59 +10:00
|
|
|
// Returns an NBP LkUp-Reply for the router itself, with the address from this port.
|
2024-05-03 18:32:12 +10:00
|
|
|
func (port *EtherTalkPort) helloWorldThisIsMe(nbpID uint8, tuple *nbp.Tuple) (*ddp.ExtPacket, error) {
|
2024-04-21 17:47:58 +10:00
|
|
|
if tuple.Object != "jrouter" && tuple.Object != "=" {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
if tuple.Type != "AppleRouter" && tuple.Type != "=" {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2024-05-03 16:13:59 +10:00
|
|
|
if tuple.Zone != port.DefaultZoneName && tuple.Zone != "*" && tuple.Zone != "" {
|
2024-04-21 17:47:58 +10:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
respPkt := &nbp.Packet{
|
|
|
|
Function: nbp.FunctionLkUpReply,
|
|
|
|
NBPID: nbpID,
|
|
|
|
Tuples: []nbp.Tuple{
|
|
|
|
{
|
2024-05-03 16:13:59 +10:00
|
|
|
Network: port.MyAddr.Network,
|
|
|
|
Node: port.MyAddr.Node,
|
2024-04-21 17:47:58 +10:00
|
|
|
Socket: 253,
|
|
|
|
Enumerator: 0,
|
|
|
|
Object: "jrouter",
|
|
|
|
Type: "AppleRouter",
|
2024-05-03 16:13:59 +10:00
|
|
|
Zone: port.DefaultZoneName,
|
2024-04-21 17:47:58 +10:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
respRaw, err := respPkt.Marshal()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("couldn't marshal LkUp-Reply: %v", err)
|
|
|
|
}
|
2024-05-03 18:32:12 +10:00
|
|
|
|
|
|
|
// Inside AppleTalk SE, pp 7-16:
|
|
|
|
// "In BrRq, FwdReq, and LkUp packets, which carry only a single tuple, the
|
|
|
|
// address field contains the internet address of the requester, allowing
|
|
|
|
// the responder to address the LkUp-Reply datagram."
|
|
|
|
// Inside AppleTalk SE, pp 8-20:
|
|
|
|
// "Note: NBP is defined so that the router's NBP process does not
|
|
|
|
// participate in the NBP response process; the response is sent directly to
|
|
|
|
// the original requester through DDP. It is important that the original
|
|
|
|
// requester's field be obtained from the address field of the NBP tuple."
|
2024-04-21 17:47:58 +10:00
|
|
|
return &ddp.ExtPacket{
|
|
|
|
ExtHeader: ddp.ExtHeader{
|
|
|
|
Size: uint16(len(respRaw)) + atalk.DDPExtHeaderSize,
|
|
|
|
Cksum: 0,
|
2024-05-03 18:32:12 +10:00
|
|
|
DstNet: tuple.Network,
|
|
|
|
DstNode: tuple.Node,
|
|
|
|
DstSocket: tuple.Socket,
|
2024-05-03 16:13:59 +10:00
|
|
|
SrcNet: port.MyAddr.Network,
|
|
|
|
SrcNode: port.MyAddr.Node,
|
2024-04-21 17:47:58 +10:00
|
|
|
SrcSocket: 2,
|
2024-04-21 18:16:11 +10:00
|
|
|
Proto: ddp.ProtoNBP,
|
2024-04-21 17:47:58 +10:00
|
|
|
},
|
|
|
|
Data: respRaw,
|
|
|
|
}, nil
|
|
|
|
}
|