-
Notifications
You must be signed in to change notification settings - Fork 13
/
ip.lua
547 lines (422 loc) · 12.6 KB
/
ip.lua
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
-- SPDX-License-Identifier: MIT
-- Author: Jianhui Zhao <zhaojh329@gmail.com>
local hex = require 'eco.encoding.hex'
local socket = require 'eco.socket'
local rtnl = require 'eco.rtnl'
local sys = require 'eco.sys'
local nl = require 'eco.nl'
local M = {}
local function __rtnl_send(sock, msg)
local ok, err = sock:send(msg)
if not ok then
return nil, err
end
msg, err = sock:recv()
if not msg then
return nil, err
end
local nlh = msg:next()
if not nlh then
return nil, 'no ack'
end
if nlh.type == nl.NLMSG_ERROR then
err = msg:parse_error()
if err == 0 then
return true
end
return nil, 'RTNETLINK answers: ' .. sys.strerror(-err)
end
return nil, 'no ack'
end
local function rtnl_send(msg)
local sock, err = nl.open(nl.NETLINK_ROUTE)
if not sock then
return nil, err
end
local ok, err = __rtnl_send(sock, msg)
sock:close()
if ok then
return true
end
return nil, err
end
local function is_valid_mac(mac)
if type(mac) ~= 'string' then
return false
end
return not not mac:match('^%x%x:%x%x:%x%x:%x%x:%x%x:%x%x$')
end
local link = {}
--[[
syntax:
local link = require 'eco.ip'.link
local ok, err = link.set('eth0', { up = true })
In case of success, it returns true; in case of error, it returns
nil with a string describing the error.
Supported attributes:
up: boolean
down: boolean
arp: boolean
dynamic: boolean
multicast: boolean
allmulticast: boolean
promisc: boolean
carrier: boolean
txqueuelen: number
address: string
broadcast: string
mtu: number
alias: string
master: string
nomaster: boolean
--]]
function link.set(dev, attrs)
local dev_index = socket.if_nametoindex(dev)
if not dev_index then
return nil, 'no such device'
end
attrs = attrs or {}
local msg = nl.nlmsg(rtnl.RTM_SETLINK, nl.NLM_F_REQUEST | nl.NLM_F_ACK)
local change = 0
local flags = 0
if attrs.up then
change = change | rtnl.IFF_UP
flags = flags | rtnl.IFF_UP
elseif attrs.down then
change = change | rtnl.IFF_UP
end
if attrs.arp ~= nil then
change = change | rtnl.IFF_NOARP
if not attrs.arp then
flags = flags | rtnl.IFF_NOARP
end
end
if attrs.dynamic ~= nil then
change = change | rtnl.IFF_DYNAMIC
if attrs.dynamic then
flags = flags | rtnl.IFF_DYNAMIC
end
end
if attrs.multicast ~= nil then
change = change | rtnl.IFF_MULTICAST
if attrs.multicast then
flags = flags | rtnl.IFF_MULTICAST
end
end
if attrs.allmulticast ~= nil then
change = change | rtnl.IFF_MULTICAST
if attrs.allmulticast then
flags = flags | rtnl.IFF_ALLMULTI
end
end
if attrs.promisc ~= nil then
change = change | rtnl.IFF_PROMISC
if attrs.promisc then
flags = flags | rtnl.IFF_PROMISC
end
end
msg:put(rtnl.ifinfomsg({
family = socket.AF_UNSPEC,
index = dev_index,
change = change,
flags = flags
}))
if attrs.carrier ~= nil then
msg:put_attr_u8(rtnl.IFLA_CARRIER, attrs.carrier and 1 or 0)
end
if attrs.txqueuelen then
msg:put_attr_u32(rtnl.IFLA_TXQLEN, attrs.txqueuelen)
end
if attrs.address then
assert(is_valid_mac(attrs.address), 'not a valid address')
msg:put_attr(rtnl.IFLA_ADDRESS, hex.decode(attrs.address:gsub(':', '')))
end
if attrs.broadcast then
assert(is_valid_mac(attrs.broadcast), 'not a valid broadcast')
msg:put_attr(rtnl.IFLA_BROADCAST, hex.decode(attrs.broadcast:gsub(':', '')))
end
if attrs.mtu then
msg:put_attr_u32(rtnl.IFLA_MTU, attrs.mtu)
end
if attrs.alias then
msg:put_attr_str(rtnl.IFLA_IFALIAS, attrs.alias)
end
if attrs.master then
local master = attrs.master
local index = socket.if_nametoindex(master)
if not index then
return nil, 'Device does not exist: ' .. master
end
msg:put_attr_u32(rtnl.IFLA_MASTER, index)
end
if attrs.nomaster then
msg:put_attr_u32(rtnl.IFLA_MASTER, 0)
end
return rtnl_send(msg)
end
local function link_get(sock, ifindex)
local msg = nl.nlmsg(rtnl.RTM_GETLINK, nl.NLM_F_REQUEST)
msg:put(rtnl.ifinfomsg({ family = socket.AF_UNSPEC, index = ifindex }))
local ok, err = sock:send(msg)
if not ok then
return nil, err
end
msg, err = sock:recv()
if not msg then
return nil, err
end
local nlh = msg:next()
if not nlh then
return nil, 'no ack'
end
if nlh.type == nl.NLMSG_ERROR then
err = msg:parse_error()
if err == 0 then
return true
end
return nil, 'RTNETLINK answers: ' .. sys.strerror(-err)
end
local res = {
up = false,
running = false,
arp = true,
dynamic = false,
multicast = false,
allmulticast = false,
promisc = false
}
local info = rtnl.parse_ifinfomsg(msg)
res.type = info.type
if info.flags & rtnl.IFF_UP > 0 then
res.up = true
end
if info.flags & rtnl.IFF_RUNNING > 0 then
res.running = true
end
if info.flags & rtnl.IFF_NOARP > 0 then
res.arp = false
end
if info.flags & rtnl.IFF_DYNAMIC > 0 then
res.dynamic = true
end
if info.flags & rtnl.IFF_MULTICAST > 0 then
res.multicast = true
end
if info.flags & rtnl.IFF_ALLMULTI > 0 then
res.allmulticast = true
end
if info.flags & rtnl.IFF_PROMISC > 0 then
res.promisc = true
end
local attrs = msg:parse_attr(rtnl.IFINFOMSG_SIZE)
if attrs[rtnl.IFLA_CARRIER] then
res.carrier = nl.attr_get_u8(attrs[rtnl.IFLA_CARRIER]) == 1 and true or false
end
if attrs[rtnl.IFLA_IFNAME] then
res.ifname = nl.attr_get_str(attrs[rtnl.IFLA_IFNAME])
end
if attrs[rtnl.IFLA_IFALIAS] then
res.alias = nl.attr_get_str(attrs[rtnl.IFLA_IFALIAS])
end
if attrs[rtnl.IFLA_MASTER] then
local idx = nl.attr_get_u32(attrs[rtnl.IFLA_MASTER])
res.master = socket.if_indextoname(idx)
end
if attrs[rtnl.IFLA_MTU] then
res.mtu = nl.attr_get_u32(attrs[rtnl.IFLA_MTU])
end
if attrs[rtnl.IFLA_TXQLEN] then
res.txqueuelen = nl.attr_get_u32(attrs[rtnl.IFLA_TXQLEN])
end
if attrs[rtnl.IFLA_ADDRESS] then
local addr = nl.attr_get_payload(attrs[rtnl.IFLA_ADDRESS])
res.address = hex.encode(addr, ':')
end
if attrs[rtnl.IFLA_BROADCAST] then
local addr = nl.attr_get_payload(attrs[rtnl.IFLA_BROADCAST])
res.broadcast = hex.encode(addr, ':')
end
return res
end
function link.get(dev)
local ifindex = socket.if_nametoindex(dev)
if not ifindex then
return nil, 'no such device'
end
local sock, err = nl.open(nl.NETLINK_ROUTE)
if not sock then
return nil, err
end
local res, err = link_get(sock, ifindex)
sock:close()
if res then
return res
end
return nil, err
end
M.link = link
local address = {}
local rtscope_to_num = {
global = rtnl.RT_SCOPE_UNIVERSE,
nowhere = rtnl.RT_SCOPE_NOWHERE,
host = rtnl.RT_SCOPE_HOST,
link = rtnl.RT_SCOPE_LINK,
site = rtnl.RT_SCOPE_SITE
}
local rtscope_to_name = {
[rtnl.RT_SCOPE_UNIVERSE] = 'global',
[rtnl.RT_SCOPE_NOWHERE] = 'nowhere',
[rtnl.RT_SCOPE_HOST] = 'host',
[rtnl.RT_SCOPE_LINK] = 'link',
[rtnl.RT_SCOPE_SITE] = 'site'
}
local function do_address(action, dev, addr)
local dev_index = socket.if_nametoindex(dev)
if not dev_index then
return nil, 'no such device'
end
local family = socket.AF_INET
local local_addr = addr.address
local prefix = addr.prefix
if local_addr:find('/') then
local_addr, prefix = local_addr:match('(%d+%.%d+%.%d+%.%d+)/(%d+)')
if not local_addr then
return nil, 'invalid local address'
end
end
prefix = tonumber(prefix or 32)
if prefix > 32 then
return nil, 'invalid prefix length'
end
local local_addr = socket.inet_pton(family, local_addr)
if not local_addr then
return nil, 'invalid local address'
end
local scope = rtnl.RT_SCOPE_UNIVERSE
if addr.scope then
if not rtscope_to_num[addr.scope] then
return nil, 'invalid scope'
end
scope = rtscope_to_num[addr.scope]
end
local msg_type
if action == 'add' then
msg_type = rtnl.RTM_NEWADDR
elseif action == 'del' then
msg_type = rtnl.RTM_DELADDR
else
error('invalid action')
end
local msg = nl.nlmsg(msg_type, nl.NLM_F_REQUEST | nl.NLM_F_ACK)
msg:put(rtnl.ifaddrmsg({
family = family,
index = dev_index,
prefixlen = prefix,
scope = scope
}))
msg:put_attr(rtnl.IFA_LOCAL, local_addr)
if addr.broadcast then
local in_addr = socket.inet_pton(family, addr.broadcast)
if not in_addr then
return nil, 'invalid broadcast address'
end
msg:put_attr(rtnl.IFA_BROADCAST, in_addr)
end
if addr.label then
msg:put_attr_str(rtnl.IFA_LABEL, addr.label)
end
if rtnl.IFA_RT_PRIORITY then
if addr.metric then
msg:put_attr_u32(rtnl.IFA_RT_PRIORITY, addr.metric)
end
if addr.priority then
msg:put_attr_u32(rtnl.IFA_RT_PRIORITY, addr.priority)
end
end
return rtnl_send(msg)
end
function address.add(dev, addr)
return do_address('add', dev, addr)
end
function address.del(dev, addr)
return do_address('del', dev, addr)
end
local function address_get(sock, ifindex)
local msg = nl.nlmsg(rtnl.RTM_GETADDR, nl.NLM_F_REQUEST | nl.NLM_F_DUMP)
msg:put(rtnl.ifaddrmsg({ family = socket.AF_UNSPEC }))
local ok, err = sock:send(msg)
if not ok then
return nil, err
end
local reses = {}
while true do
msg, err = sock:recv()
if not msg then
return nil, err
end
while true do
local nlh = msg:next()
if not nlh then
break
end
if nlh.type == rtnl.RTM_NEWADDR then
local res = {}
local info = rtnl.parse_ifaddrmsg(msg)
local family = info.family
if not ifindex or ifindex == info.index then
res.ifname = socket.if_indextoname(info.index)
res.scope = rtscope_to_name[info.scope]
res.family = family
local attrs = msg:parse_attr(rtnl.IFADDRMSG_SIZE)
local addr_attr
if family == socket.AF_INET then
addr_attr = attrs[rtnl.IFA_LOCAL]
elseif family == socket.AF_INET6 then
addr_attr = attrs[rtnl.IFA_ADDRESS]
end
if addr_attr then
res.address = socket.inet_ntop(family, nl.attr_get_payload(addr_attr))
if attrs[rtnl.IFA_BROADCAST] then
res.broadcast = socket.inet_ntop(family, nl.attr_get_payload(attrs[rtnl.IFA_BROADCAST]))
end
if attrs[rtnl.IFA_LABEL] then
res.label = nl.attr_get_str(attrs[rtnl.IFA_LABEL])
end
reses[#reses + 1] = res
end
end
elseif nlh.type == nl.NLMSG_ERROR then
err = msg:parse_error()
if err < 0 then
return nil, 'RTNETLINK answers: ' .. sys.strerror(-err)
end
elseif nlh.type == nl.NLMSG_DONE then
if ifindex and #reses < 1 then
return nil, 'not found'
end
return reses
end
end
end
end
function address.get(dev)
local ifindex
if dev then
ifindex = socket.if_nametoindex(dev)
if not ifindex then
return nil, 'no such device'
end
end
local sock, err = nl.open(nl.NETLINK_ROUTE)
if not sock then
return nil, err
end
local res, err = address_get(sock, ifindex)
sock:close()
if res then
return res
end
return nil, err
end
M.address = address
return M