summaryrefslogtreecommitdiffhomepage
path: root/tunnel/tools/libwg-go/dhcp.go
blob: b7919151580f18b8938374fd359a503703bd8d0a (plain)
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
package main

import (
        "context"
        "net"
        "net/netip"
        "time"

        "github.com/insomniacslk/dhcp/dhcpv4"
        "github.com/insomniacslk/dhcp/dhcpv6"
        "github.com/insomniacslk/dhcp/dhcpv6/nclient6"
        "github.com/insomniacslk/dhcp/iana"

	gen "golang.zx2c4.com/wireguard/android/gen"
)

const (
        ENABLE_PD = true
)

type dhcp struct {
        fqdn string
        hwAddr net.HardwareAddr
        conn *net.UDPConn
        serverAddr net.UDPAddr
        client *nclient6.Client
        linkAddr net.IP
        peerAddr net.IP
}

func newClientIDOpt(duid dhcpv6.DUID) dhcpv4.Option {
	iaid := []byte{0, 0, 0, 3}
	ident := []byte{255}                     // Type IAID+DUID
	ident = append(ident, iaid...)           // IAID
	ident = append(ident, duid.ToBytes()...) // DUID
	return dhcpv4.OptClientIdentifier(ident)
}

func getDuid(hwAddr net.HardwareAddr) dhcpv6.DUID {
	duid := &dhcpv6.DUIDLLT{
		HWType:        iana.HWTypeEthernet,
		Time:          uint32(time.Now().Unix()),
		LinkLayerAddr: hwAddr,
	}

        return duid
}

func (d *dhcp) encapSendAndRead(ctx context.Context, msg *dhcpv6.Message, match nclient6.Matcher) (*dhcpv6.Message, error) {
	packet, err := dhcpv6.EncapsulateRelay(msg, dhcpv6.MessageTypeRelayForward, d.linkAddr, d.peerAddr)
	if err != nil {
		return nil, err
	}

        relay, err := d.client.SendAndReadRelay(ctx, &d.serverAddr, packet, match)
	if err != nil {
		return nil, err
	}

        inner, err := relay.GetInnerMessage()
        if err != nil {
                return nil, err
        }

        return inner, nil
}

// isRelayMessageType returns a matcher that checks for the message type.
func isRelayMessageType(t dhcpv6.MessageType, tt ...dhcpv6.MessageType) nclient6.Matcher {
	return func(p dhcpv6.DHCPv6) bool {
                inner, err := p.GetInnerMessage()
                if err != nil {
                        return false
                }
		if inner.Type() == t {
			return true
		}
		for _, mt := range tt {
			if inner.Type() == mt {
				return true
			}
		}
		return false
	}
}

// func New() *dhcp {
// }

func RunDhcp(ctx context.Context, laddr, raddr netip.Addr) ([]*gen.Lease, error) {
        d := &dhcp{}
        
        d.linkAddr = net.ParseIP("fe80::101")
	d.peerAddr = net.ParseIP("::1")

        // TODO generate hostname and hwAddr from public key
	hostName := "foobar"
	d.fqdn = hostName + ".m7n.se"
        d.hwAddr = []byte{41, 42, 43, 44, 45, 46}


        src := net.UDPAddr{IP: laddr.AsSlice(),
                Port: 0,        // Use non-restrict UDP source port
        }


        d.serverAddr = net.UDPAddr{IP: raddr.AsSlice(),
                Port: 547,
        }

        err := d.Start(&src)
        if err != nil {
                return nil, err
        }

        defer d.Close()

        reply, err := d.ObtainLease(ctx) // Use reply
        if err != nil {
                return nil, err
        }

        return getAddressesFromReply(reply), nil
}

func getAddressesFromReply(reply *dhcpv6.Message) []*gen.Lease{
        var leases []*gen.Lease = make([]*gen.Lease, 0, 4)

        if opt := reply.GetOneOption(dhcpv6.OptionIANA); opt != nil {
                iana := opt.(*dhcpv6.OptIANA)
                ianaOpts := iana.Options.Get(dhcpv6.OptionIAAddr)

                for _, opt := range ianaOpts {
                        addr :=opt.(*dhcpv6.OptIAAddress).IPv6Addr
                        lease := &gen.Lease{
                                Address: &gen.InetAddress{
                                        Address: addr,
                                },
                                // PreferredLifetime: ,
                                // ValidLifetime: ,
                        }
                        leases = append(leases, lease)
                }
        }

        return leases
}

func (d *dhcp) Start(localAddr *net.UDPAddr) error {
        conn, err := net.ListenUDP("udp6", localAddr)
	if err != nil {
                return err
        }
        
	d.client, err = nclient6.NewWithConn(conn, d.hwAddr, nclient6.WithDebugLogger())
	return err
}

func (d *dhcp) Close() error {
        err := d.client.Close()

        d.client = nil
        return err
}

func (d *dhcp) ObtainLease(ctx context.Context) (*dhcpv6.Message, error){
        duidOpt := dhcpv6.WithClientID(getDuid(d.hwAddr))
	fqdnOpt := dhcpv6.WithFQDN(0x1, d.fqdn)

        solicit, err := dhcpv6.NewSolicit(d.hwAddr, duidOpt, fqdnOpt, dhcpv6.WithRapidCommit)
        if err != nil {
                return nil, err
        }

        msg, err := d.encapSendAndRead(ctx, solicit, isRelayMessageType(dhcpv6.MessageTypeReply, dhcpv6.MessageTypeAdvertise))
        if err != nil {
                return nil, err
        }

        if msg.Type() == dhcpv6.MessageTypeReply {
		// We got RapidCommitted.
		return msg, nil
        }

        // We didn't get RapidCommitted. Request regular lease.
        req, err := dhcpv6.NewRequestFromAdvertise(msg, fqdnOpt)
        if err != nil {
                return nil, err
        }

        return d.encapSendAndRead(ctx, req, nil)
}