summaryrefslogtreecommitdiffhomepage
path: root/dhcp-subscribe.lua
blob: bceac43d1485780fe9b27b765180af10d5240ae0 (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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
#!/usr/bin/lua
-- Copyright (C) 2020-2023 mikma

local json = require("luci.jsonc")
require "ubus"
require "uloop"

local IFACE = 'wg'
local TIMEOUT = 1000

local wg_peers = {}
local wg_ll = {}

local function add_to_set(set, key)
	set[key] = true
end

local function remove_from_set(set, key)
	set[key] = nil
end

local function add_to_allowed_ips(allowed_ips, ip)
	add_to_set(allowed_ips, ip)
end

local function remove_from_allowed_ips(allowed_ips, ip)
	remove_from_set(allowed_ips, ip)
end

local function delete_ip(allowed_ips, ip)
	for k, v in pairs(allowed_ips) do
		if v == ip then
			allowed_ips[k] = nil
			print('deleted ' .. ip)
			return
		end
	end
	print('not deleted ' .. ip)
end

local function lookup_peer(iface, addr)
	local cmd = 'wg show ' .. iface .. ' allowed-ips'
	print('cmd: ' .. cmd .. ' addr: ' .. addr)
	local f = assert(io.popen(cmd, 'r'))

	peers = {}
	while true do
		local line = f:read('*line')
		if line == nil then break end

--		print('line: ' .. line)

		for peer, v in string.gmatch(line, "([^\t]+)\t([^t]+)") do
--			print(peer	.. '#' .. v)
			local allowed_ips = {}
			local found = false
			for allowed_ip in string.gmatch(v, "[^ ]+") do
--				print('addr: "' .. allowed_ip .. '"')
				add_to_allowed_ips(allowed_ips, allowed_ip)
				if addr == allowed_ip then
					print('found ' .. addr)
					found = true
				end
			end
			if found then
				f:close()
				return peer, allowed_ips
			end
			peers[peer] = allowed_ips
		end
	end

	f:close()
	return nil
end

local function set_allowed_ips(iface, peer_key, allowed_ips)
	str = nil

	for ip, _ in pairs(allowed_ips) do
		if str then
			str = str .. ',' .. ip
		else
			str = ip
		end
	end
	cmd = 'wg set ' .. iface .. ' peer ' .. peer_key .. ' allowed-ips ' .. str

	print('cmd: ' .. cmd)
	os.execute(cmd)
end

local function dhcpv4(method, msg)
	print("dhcpv4 " .. method)

	local iface = msg.interface
	local peer_addr = msg['peer-4o6']
	local ip = msg.ip .. '/32'

	print('ack: ' .. iface .. ' ' .. peer_addr .. ' ' .. ip)
	local peer, allowed_ips = lookup_peer(iface, peer_addr .. '/128')
	if not peer then
		return
	end
	print('peer: ' .. peer)
	if method == 'dhcp.ack' then
		add_to_allowed_ips(allowed_ips, ip)
	else if method == 'dhcp.release' then
		remove_from_allowed_ips(allowed_ips, ip)
	end end
	set_allowed_ips(iface, peer, allowed_ips)
end

local function dhcpv6(method, msg)
	print("dhcpv6 " .. method)

	local iface = msg.interface
	local peer_addr = msg.peer
	print('ack: ' .. iface .. ' ' .. peer_addr)
	local peer, allowed_ips = lookup_peer(iface, peer_addr .. '/128')

	if not peer then
		return
	end
	print('peer: ' .. peer)

	local add_ips = nil
	for _, addr in ipairs(msg.ips) do
		ip = addr.ip
		print('ip: ' .. ip)
		if method == 'dhcpv6.ack' then
			add_to_allowed_ips(allowed_ips, ip)
		else if method == 'dhcpv6.release' then
			remove_from_allowed_ips(allowed_ips, ip)
		end end
	end

	set_allowed_ips(iface, peer, allowed_ips)
end

local function dhcpv6_release(msg)
	print("dhcpv6_release")
end

local methods = {}
methods['dhcp.release'] = dhcpv4
methods['dhcp.ack'] = dhcpv4
methods['dhcpv6.release'] = dhcpv6
methods['dhcpv6.ack'] = dhcpv6

local function subscribe_dhcp(conn, iface)
	print("Subscribing to dhcp")
	local sub = {
		notify = function(msg, method)
			local ifname = ifname,
			print(method)
			print(json.stringify(msg))
			if ifname ~= iface then
				print('Unknown interface: ' .. ifname)
				return
			end
			action = methods[method]
			if action then
				action(method, msg)
			else
				print("Unknown method")
			end
		end,
	}
	conn:subscribe("dhcp", sub)
end

function do_tables_match( a, b )
    return table.concat(a) == table.concat(b)
end

-- Scan ipv6 leases
local function scan_leases(conn, iface, name)
	print("Scan: " .. name .. ' ' .. iface)
	local status = conn:call("dhcp", name, {})

	local peers = {}
	local peers_dirty = {}

	for _, v in pairs(status.device[iface].leases) do
		print(v)
		local peer_ll = v.peer

		if not peer_ll then
			peer_ll = v['peer-4o6']
		end
		print("peer=" .. peer_ll)

		local pos = string.find(peer_ll, '%%')
		if pos then
			peer_ll = string.sub(peer_ll, 1, pos - 1)
		end

		local peer_key = wg_ll[peer_ll]
		local wg_peer = wg_peers[peer_key]
		local peer = peers[peer_key]
		if not peer then
			peer = {}
			peers[peer_key] = peer
		end

		print("peer=" .. tostring(peer_ll) .. ' ' .. tostring(peer_key))
		local ipv4_addr = v['address']
		local ipv6_prefix = v['ipv6-prefix']
		local ipv6_addr = v['ipv6-addr']

		if ipv4_addr then
			local ip = ipv4_addr .. '/32'
			local status = 'found'
			if not wg_peer[ip] then
				status = 'not found'
				add_to_set(peers_dirty, peer_key)
				add_to_set(peer, ip)
			end

			print("address=" .. ip .. ' ' .. status)
		end

		if ipv6_addr then
			for _, addr in pairs(ipv6_addr) do
				local ip = addr.address .. '/128'
				local status = 'found'
				if not wg_peer[ip] then
					status = 'not found'
					add_to_set(peers_dirty, peer_key)
					add_to_set(peer, ip)
				end

				print("address=" .. ip .. ' ' .. status)
			end
		end

		if ipv6_prefix then
			for _, prefix in pairs(ipv6_prefix) do
				local ip = prefix.address .. '/' .. prefix['prefix-length']
				local status = 'found'
				if not wg_peer[ip] then
					status = 'not found'
					add_to_set(peers_dirty, peer_key)
					add_to_set(peer, ip)
				end

				print("prefix=" .. ip .. ' ' .. status)
			end
		end
	end

	for peer_key, _ in pairs(peers_dirty) do
		print('Dirty ' .. peer_key)

		local add_allowed_ips = peers[peer_key]
		local allowed_ips = wg_peers[peer_key]

		for allowed_ip, _ in pairs(add_allowed_ips) do
			print('Add ip ' .. allowed_ip)
			add_to_set(allowed_ips, allowed_ip)
		end

		set_allowed_ips(iface, peer_key, allowed_ips)
	end
	print("Done " .. name)
end

-- Scan wg allowed_ips and update wg_peers, and wg_ll
local function scan_wg(iface)
	print("Scan wg")
	local cmd = 'wg show ' .. iface .. ' allowed-ips'
	print('cmd: ' .. cmd)
	local f = assert(io.popen(cmd, 'r'))

	local peers = {}
	local ll = {}
	while true do
		local line = f:read('*line')
		if line == nil then break end

--		print('line: ' .. line)

		for peer, v in string.gmatch(line, "([^\t]+)\t([^t]+)") do
--			print(peer	.. '#' .. v)
			local allowed_ips = {}
			for allowed_ip in string.gmatch(v, "[^ ]+") do
				print('addr: "' .. allowed_ip .. '"')
				add_to_allowed_ips(allowed_ips, allowed_ip)

				print(string.sub(allowed_ip, 1, 6))
				if string.sub(allowed_ip, 1, 6) == 'fe80::' then
					local peer_ll = allowed_ip
					local pos = string.find(peer_ll, '/')
					if pos then
						peer_ll = string.sub(peer_ll, 1, pos - 1)
					end
					print("Found LL " .. peer_ll .. ' = ' .. peer)
					ll[peer_ll] = peer
				end
			end
			peers[peer] = allowed_ips
		end
	end

	wg_peers = peers
	wg_ll = ll
	f:close()
	print("Done wg")
end

local function listen_interface(conn, iface, timeout)
	local event = {}

	event['network.interface'] = function(msg)
		if msg.action == 'ifup' and msg.interface == iface then
			print("Up " .. iface)
			uloop.timer(function() scan_wg(iface); scan_lease(conn, iface, 'ipv4leases'); scan_leases(conn, iface, 'ipv6leases') end, timeout)
		end
	end
	conn:listen(event)
end

uloop.init()

local conn = ubus.connect()
if not conn then
	error("Failed to connect to ubusd")
end

scan_wg(IFACE)

scan_leases(conn, IFACE, 'ipv4leases')
scan_leases(conn, IFACE, 'ipv6leases')

listen_interface(conn, IFACE, TIMEOUT)
subscribe_dhcp(conn)

uloop.run()