jrouter/router/nbp.go

183 lines
5.2 KiB
Go
Raw Normal View History

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 (
"fmt"
"log"
2024-04-15 17:49:27 +10:00
"gitea.drjosh.dev/josh/jrouter/atalk"
2024-04-14 18:29:29 +10:00
"gitea.drjosh.dev/josh/jrouter/atalk/nbp"
"github.com/sfiera/multitalk/pkg/ddp"
"github.com/sfiera/multitalk/pkg/ethernet"
"github.com/sfiera/multitalk/pkg/ethertalk"
)
2024-04-19 14:57:25 +10:00
func (rtr *Router) HandleNBP(srcHWAddr ethernet.Addr, 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...
tuple := nbpkt.Tuples[0]
if tuple.Object != "jrouter" && tuple.Object != "=" {
return nil
}
if tuple.Type != "AppleRouter" && tuple.Type != "=" {
return nil
}
2024-04-19 14:57:25 +10:00
if tuple.Zone != rtr.Config.EtherTalk.ZoneName && tuple.Zone != "*" && tuple.Zone != "" {
2024-04-14 18:29:29 +10:00
return nil
}
respPkt := &nbp.Packet{
Function: nbp.FunctionLkUpReply,
NBPID: nbpkt.NBPID,
Tuples: []nbp.Tuple{
{
2024-04-19 14:57:25 +10:00
Network: rtr.MyDDPAddr.Network,
Node: rtr.MyDDPAddr.Node,
2024-04-14 18:29:29 +10:00
Socket: 253,
Enumerator: 0,
Object: "jrouter",
Type: "AppleRouter",
2024-04-19 14:57:25 +10:00
Zone: rtr.Config.EtherTalk.ZoneName,
2024-04-14 18:29:29 +10:00
},
},
}
respRaw, err := respPkt.Marshal()
if err != nil {
return fmt.Errorf("couldn't marshal LkUp-Reply: %v", err)
}
2024-04-15 17:49:27 +10:00
outDDP := ddp.ExtPacket{
ExtHeader: ddp.ExtHeader{
Size: uint16(len(respRaw)) + atalk.DDPExtHeaderSize,
Cksum: 0,
DstNet: ddpkt.SrcNet,
DstNode: ddpkt.SrcNode,
DstSocket: ddpkt.SrcSocket,
2024-04-19 14:57:25 +10:00
SrcNet: rtr.MyDDPAddr.Network,
SrcNode: rtr.MyDDPAddr.Node,
2024-04-15 17:49:27 +10:00
SrcSocket: 2,
},
Data: respRaw,
}
2024-04-19 14:57:25 +10:00
outFrame, err := ethertalk.AppleTalk(rtr.MyHWAddr, outDDP)
2024-04-14 18:29:29 +10:00
if err != nil {
return err
}
outFrame.Dst = srcHWAddr
outFrameRaw, err := ethertalk.Marshal(*outFrame)
if err != nil {
return err
}
2024-04-19 14:57:25 +10:00
return rtr.PcapHandle.WritePacketData(outFrameRaw)
2024-04-14 18:29:29 +10:00
case nbp.FunctionBrRq:
// There must be 1!
2024-04-15 18:08:45 +10:00
tuple := &nbpkt.Tuples[0]
2024-04-15 20:48:40 +10:00
ethDst := ethertalk.AppleTalkBroadcast
if tuple.Zone != "*" && tuple.Zone != "" {
ethDst = atalk.MulticastAddr(tuple.Zone)
}
2024-04-15 18:08:45 +10:00
2024-04-19 14:57:25 +10:00
zones := rtr.ZoneTable.LookupName(tuple.Zone)
2024-04-15 18:08:45 +10:00
for _, z := range zones {
if z.Local {
// If it's for the local zone, translate it to a LkUp and broadcast it back
// out the 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
nbpRaw, err := nbpkt.Marshal()
if err != nil {
return fmt.Errorf("couldn't marshal LkUp: %v", err)
}
outDDP := *ddpkt
outDDP.Size = uint16(len(nbpRaw)) + atalk.DDPExtHeaderSize
outDDP.DstNode = 0xFF // Broadcast node address within the dest network
outDDP.Data = nbpRaw
2024-04-19 14:57:25 +10:00
outFrame, err := ethertalk.AppleTalk(rtr.MyHWAddr, outDDP)
2024-04-15 18:08:45 +10:00
if err != nil {
return err
}
2024-04-15 20:48:40 +10:00
outFrame.Dst = ethDst
2024-04-15 18:08:45 +10:00
outFrameRaw, err := ethertalk.Marshal(*outFrame)
if err != nil {
return err
}
2024-04-19 14:57:25 +10:00
if err := rtr.PcapHandle.WritePacketData(outFrameRaw); err != nil {
2024-04-15 18:08:45 +10:00
return err
}
continue
}
2024-04-19 14:57:25 +10:00
route := rtr.RouteTable.LookupRoute(z.Network)
2024-04-15 18:08:45 +10:00
if route == nil {
return fmt.Errorf("no route for network %d", z.Network)
}
peer := route.Peer
if peer == nil {
return fmt.Errorf("nil peer for route for network %d", z.Network)
}
// Translate it into a FwdReq and route it to the
2024-04-14 18:29:29 +10:00
// routers with the appropriate zone(s).
2024-04-15 18:08:45 +10:00
nbpkt.Function = nbp.FunctionFwdReq
nbpRaw, err := nbpkt.Marshal()
if err != nil {
return fmt.Errorf("couldn't marshal FwdReq: %v", err)
}
outDDP := *ddpkt
outDDP.Size = uint16(len(nbpRaw)) + atalk.DDPExtHeaderSize
outDDP.DstNet = z.Network
outDDP.DstNode = 0x00 // Router node address for the dest network
outDDP.Data = nbpRaw
outDDPRaw, err := ddp.ExtMarshal(outDDP)
if err != nil {
return err
}
2024-04-19 14:57:25 +10:00
if _, err := peer.Send(peer.Transport.NewAppleTalkPacket(outDDPRaw)); err != nil {
2024-04-15 18:08:45 +10:00
return fmt.Errorf("sending FwdReq on to peer: %w", err)
}
2024-04-14 18:29:29 +10:00
}
default:
return fmt.Errorf("TODO: handle function %v", nbpkt.Function)
}
2024-04-15 18:09:43 +10:00
return nil
2024-04-14 18:29:29 +10:00
}