-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
175 lines (158 loc) · 3.82 KB
/
main.go
File metadata and controls
175 lines (158 loc) · 3.82 KB
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
package main
import (
"context"
"net"
"net/netip"
"os"
"os/signal"
"syscall"
"github.com/gopacket/gopacket"
"github.com/gopacket/gopacket/afpacket"
"github.com/urfave/cli/v2"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"go4.org/netipx"
)
var logger = func() *zap.Logger {
var lvl zapcore.Level
if environ, ok := os.LookupEnv("NDPRESPONDER_LOG"); ok {
lvl.Set(environ)
}
core := zapcore.NewCore(
zapcore.NewJSONEncoder(zap.NewProductionEncoderConfig()),
os.Stderr,
lvl,
)
return zap.New(core)
}()
var (
netif *net.Interface
targetSubnets *netipx.IPSet
handle *afpacket.TPacket
)
var app = &cli.App{
Name: "ndpresponder",
Description: "IPv6 Neighbor Discovery Responder",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "ifname",
Aliases: []string{"i"},
Usage: "uplink network interface",
Required: true,
},
&cli.StringSliceFlag{
Name: "subnet",
Aliases: []string{"n"},
Usage: "static target subnet",
},
&cli.StringSliceFlag{
Name: "docker-network",
Aliases: []string{"N"},
Usage: "Docker network name",
},
},
HideHelpCommand: true,
Before: func(c *cli.Context) (e error) {
if netif, e = net.InterfaceByName(c.String("ifname")); e != nil {
return cli.Exit(e, 1)
}
var ipset netipx.IPSetBuilder
for _, subnet := range c.StringSlice("subnet") {
prefix, e := netip.ParsePrefix(subnet)
if e != nil {
return cli.Exit(e, 1)
}
ipset.AddPrefix(prefix)
}
targetSubnets, e = ipset.IPSet()
if e != nil {
return cli.Exit(e, 1)
}
dockerNetworks = c.StringSlice("docker-network")
return nil
},
Action: func(c *cli.Context) error {
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer stop()
hi, e := gatherHostInfo()
if e != nil {
return cli.Exit(e, 1)
}
var e2 error
handle, e2 = afpacket.NewTPacket(afpacket.OptInterface(netif.Name))
if e2 != nil {
return cli.Exit(e2, 1)
}
if e2 = handle.SetBPF(bpfFilter); e2 != nil {
return cli.Exit(e2, 1)
}
solicitations := CaptureNeighSolicitation(handle)
if len(dockerNetworks) > 0 {
if e = dockerListen(); e != nil {
return cli.Exit(e, 1)
}
}
sbuf := gopacket.NewSerializeBuffer()
L:
for {
select {
case <-ctx.Done():
return nil
case ns, ok := <-solicitations:
if !ok {
return nil
}
logEntry := logger.With(zap.Stringer("ns", ns))
switch {
case dockerActiveIPs.Load().Contains(ns.TargetIP):
logEntry = logEntry.With(zap.String("reason", "docker"))
case targetSubnets.Contains(ns.TargetIP):
logEntry = logEntry.With(zap.String("reason", "static"))
default:
logEntry.Debug("IGNORE")
continue L
}
if e := ns.Respond(sbuf, hi); e != nil {
logEntry.Warn("RESPOND error", zap.Error(e))
continue L
}
logEntry.Info("RESPOND")
if err := handle.WritePacketData(sbuf.Bytes()); err != nil {
logEntry.Warn("WritePacketData error", zap.Error(err))
}
case ip := <-dockerNewIP:
logEntry := logger.With(zap.Stringer("ip", ip))
if e := Gratuitous(sbuf, hi, ip); e != nil {
logEntry.Warn("GRATUITOUS error", zap.Error(e))
continue L
}
logEntry.Info("GRATUITOUS")
if err := handle.WritePacketData(sbuf.Bytes()); err != nil {
logEntry.Warn("WritePacketData error", zap.Error(err))
}
if !hi.GatewayIP.IsValid() {
break
}
if e := Solicit(sbuf, hi, ip); e != nil {
logEntry.Warn("SOLICIT error", zap.Error(e))
continue L
}
logEntry.Info("SOLICIT")
if err := handle.WritePacketData(sbuf.Bytes()); err != nil {
logEntry.Warn("WritePacketData error", zap.Error(err))
}
}
}
},
After: func(c *cli.Context) error {
if handle != nil {
handle.Close()
}
return nil
},
}
func main() {
if err := app.Run(os.Args); err != nil {
os.Exit(1)
}
}