-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathnetif.lua
645 lines (604 loc) · 16.6 KB
/
netif.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
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
-- Copyright (c) 2019 Marcel Kaiser <[email protected]>
--
-- Redistribution and use in source and binary forms, with or without
-- modification, are permitted provided that the following conditions
-- are met:
-- 1. Redistributions of source code must retain the above copyright
-- notice, this list of conditions and the following disclaimer.
-- 2. Redistributions in binary form must reproduce the above copyright
-- notice, this list of conditions and the following disclaimer in the
-- documentation and/or other materials provided with the distribution.
--
-- THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
-- ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
-- IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
-- ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
-- FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
-- DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
-- OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
-- HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
-- LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
-- OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
-- SUCH DAMAGE.
--
local netif = {}
-- Constants for the match_netif_type() function
netif.NETIF_TYPE_WLAN = 1
netif.NETIF_TYPE_ETHER = 2
netif.path_rc_conf = '/etc/rc.conf'
netif.path_zoneinfo = '/var/db/zoneinfo'
netif.path_zone_tab = '/usr/share/zoneinfo/zone.tab'
-- Returns a pair, (true|false, NETIF_TYPE_WLAN|NETIF_TYPE_ETHER|nil),
-- if the given driver name matches an ethernet or wireless device driver.
function netif.match_netif_type(driver)
local m
local wlan_kmods = {
"if_zyd", "if_ath", "if_bwi", "if_bwn",
"if_ipw", "if_iwi", "if_iwm", "if_iwn",
"if_malo", "if_mwl", "if_otus", "if_ral",
"if_rsu", "if_rtwn_usb", "if_rtwn_pci", "if_rum",
"if_run", "if_uath", "if_upgt", "if_ural",
"if_urtw", "if_wi"
}
local ether_kmods = {
"if_ae", "if_age", "if_alc", "if_ale",
"if_aue", "if_axe", "if_bce", "if_bfe",
"if_bge", "if_bnxt", "if_bxe", "if_cas",
"if_cdce", "if_cue", "if_cxgb", "if_dc",
"if_de", "if_ed", "if_edsc", "if_em",
"if_et", "if_fwe", "if_fxp", "if_gem",
"if_hme", "if_ntb", "if_ipheth", "if_ix",
"if_ixl", "if_jme", "if_kue", "if_le",
"if_lge", "if_mos", "if_msk", "if_mxge",
"if_my", "if_nf10bmac", "if_nfe", "if_nge",
"if_pcn", "if_ptnet", "if_qlnxe", "if_qlxgb",
"if_qlxgbe", "if_qlxge", "if_re", "if_rl",
"if_rue", "if_sf", "if_sfxge", "if_sge",
"if_sis", "if_sk", "if_smsc", "if_sn",
"if_ste", "if_stge", "if_tap", "if_ti",
"if_tl", "if_tx", "if_txp", "if_udav",
"if_ure", "if_urndis", "if_vge", "if_vr",
"if_vte", "if_vtnet", "if_wb", "if_xe",
"if_xl"
}
for _, m in pairs(wlan_kmods) do
if driver == m then
return true, netif.NETIF_TYPE_WLAN
end
end
for _, m in pairs(ether_kmods) do
if driver == m then
return true, netif.NETIF_TYPE_ETHER
end
end
return false, nil
end
-- Takes the name of a kernel module, and removes the "if_" prefix and
-- the "_pci" or "_usb" suffix. Returns the new string.
function netif.kmod_to_dev(kmod)
local dev = kmod
if string.match(kmod, ".*_pci$") or string.match(kmod, ".*_usb$") then
dev = string.sub(kmod, 1, string.len(kmod) - 4)
end
if string.match(dev, "^if_.*") then
dev = string.sub(dev, 4)
end
return dev
end
-- Takes a device name without unit number (e.g. ath, rtwn), and a list of
-- network devices. Returns the name of the interface if found, nil otherwise.
function netif.find_netif(dev, netifs)
local d
for _, d in pairs(netifs) do
if d == dev or string.match(d, dev .. "[0-9]+") then
return d
end
end
return nil
end
-- Returns the the unit (X) of a "wlanX" device name to a given
-- parent device, or nil
function wlan_unit_from_parent(pdev)
local l
local proc, e = io.popen("sysctl net.wlan")
if proc == nil then
io.stderr:write(e)
return nil
end
for l in proc:lines() do
if string.match(l, "%%parent") and string.match(l, pdev) then
proc:close()
return tonumber(string.match(l, "net.wlan.([0-9]+)."))
end
end
proc:close()
return nil
end
-- We define a wlan device object which has the following fields:
-- parent ::= parent device name (e.g., "ath0", "rtwn0")
-- child ::= child device unit number ("wlan0" -> unit = 0). If there is no
-- child device yet, this field is nil.
--
-- This function returns a list of available wlan device objects, or an empty
-- list if there are none.
function netif.get_wlan_devs()
local i, l, parent
local pdevs = {}
local wlans = {}
local proc, e = io.popen("sysctl -n net.wlan.devices")
if proc == nil then
io.stderr:write(e)
return nil
end
i = 1
for l in proc:lines() do
for w in string.gmatch(l, "%w+") do
pdevs[i] = w
i = i + 1
end
end
proc:close()
i = 1
for _, parent in pairs(pdevs) do
child = wlan_unit_from_parent(parent)
wlans[i] = {}
wlans[i]["parent"] = parent
wlans[i]["child"] = child
i = i + 1
end
return wlans
end
-- Returns the wlan device object from the given list matching the given
-- parent device pattern, or nil if there was no match.
function netif.find_wlan(parent, wlans)
local w
for _, w in pairs(wlans) do
if string.match(w.parent, parent .. "[0-9]+") then
return w
end
end
return nil
end
function netif.get_ifconfig_if_info(ifname)
local info = {}
local proc, e = io.popen("ifconfig " .. ifname)
if proc == nil then
io.stderr:write(e)
return nil
end
local l
for l in proc:lines() do
table.insert(info, l)
end
proc:close()
return info
end
-- Returns the network interface's media type or nil
function netif.media_type(ifname)
local l
local info = netif.get_ifconfig_if_info(ifname)
if info == nil then
return nil
end
for _, l in pairs(info) do
local type = string.match(l, "^%s+media:%s([%g,%s]+)$")
if type then
if string.match(type, "%s[wW]ireless%s") or
string.match(type, "%s802.11%s") then
return netif.NETIF_TYPE_WLAN
elseif string.match(type, "^[Ee]thernet") then
return netif.NETIF_TYPE_ETHER
else
return nil
end
end
end
return nil
end
function netif.get_ifconfig_iflist()
local l
local iflist = {}
local proc, e = io.popen("ifconfig -l")
if proc == nil then
io.stderr:write(e)
return nil
end
for l in proc:lines() do
for w in string.gmatch(l, "%w+") do
table.insert(iflist, w)
end
end
proc:close()
return iflist
end
-- Returns the list of network interfaces from the output of "ifconfig" as
-- array.
function netif.get_netifs()
local iflist = {}
local all_ifs = netif.get_ifconfig_iflist()
if all_ifs == nil then
return nil
end
for _, i in pairs(all_ifs) do
type = netif.media_type(i)
if type ~= nil then
table.insert(iflist, i)
end
end
return iflist
end
-- Returns the given network interface's status
function netif.link_status(ifname)
local i, status
local info = netif.get_ifconfig_if_info(ifname)
if info == nil then
return nil
end
for _, i in pairs(info) do
if string.match(i, "^[ \t]*status: (%w+)") then
status = string.gsub(i, "^[ \t]*status: ([%w, ]+)$", "%1")
if status ~= nil then
return status
end
end
end
return nil
end
function netif.link_is_up(ifname)
local i, flag, flags
local info = netif.get_ifconfig_if_info(ifname)
if info == nil then
return nil
end
for _, i in pairs(info) do
flags = string.match(i, "^%w+:[ \t]+flags=[0-9]*<([%w, ]+)>")
if flags ~= nil then
for flag in string.gmatch(flags, "([^,]+)") do
if flag == "UP" then
return true
end
end
end
end
return false
end
-- Returns "true" if the given network interface was configured
-- via /etc/rc.conf. The function looks for ifconfig_<ifname><suffix>.
-- E.g.: ifconfig_alc0_ipv6, where suffix is '_ipv6'.
function netif.in_rc_conf(ifname, suffix)
local l
local pattern = "^[ \t]*ifconfig_" .. ifname
local f, e = io.open(netif.path_rc_conf)
if f == nil then
io.stderr:write(e)
return nil
end
if suffix ~= nil then
pattern = pattern .. suffix
end
for l in f:lines() do
if string.match(l, pattern) then
f:close()
return true
end
end
f:close()
return false
end
-- Returns the inet (v4) address of the given interface from the dhclient
-- lease file
function netif.inet_from_lease(ifname)
local l, inet, _inet
local f, e, errno = io.open("/var/db/dhclient.leases." .. ifname)
if f == nil then
if errno == 2 then -- ENOENT
return nil
end
io.stderr:write(e)
return nil
end
inet = nil
-- Get IP address of the last lease record
for l in f:lines() do
_inet = string.match(l, "^%s*fixed.address%s*(.+);$")
if _inet ~= nil then
inet = _inet
end
end
f:close()
return inet
end
-- Get the inet v4 and v6 addresses of the given interface
function netif.get_inet_addr(ifname)
local i, inet4, inet6
local info = netif.get_ifconfig_if_info(ifname)
if info == nil then
return nil, nil
end
for _, i in pairs(info) do
if inet4 == nil then
inet4 = string.match(i, "^%s+inet%s+(%g+)")
end
if inet6 == nil then
inet6 = string.match(i, "^%s+inet6%s+([%x:]+)")
end
if inet4 ~= nil and inet6 ~= nil then
break
end
end
return inet4, inet6
end
-- Returns a list of wlan device objects configured via /etc/rc.conf
function netif.wlans_from_rc_conf()
local i, l
local wlans = {}
local f, e = io.open(netif.path_rc_conf)
if f == nil then
io.stderr:write(e)
return nil
end
i = 1
for l in f:lines() do
local p, c = string.match(l, "^[ \t]*wlans_(%w+)=\"?(%w+)\"?")
if p ~= nil and c ~= nil then
wlans[i] = {}
wlans[i].parent = p
wlans[i].child = tonumber(string.match(c, "wlan(%d+)"))
i = i + 1
end
end
f:close()
return wlans
end
-- Returns "true" if the given wlan device object was configured via
-- /etc/rc.conf, else "false".
function netif.wlan_rc_configured(wlan)
local w
local wlans = netif.wlans_from_rc_conf()
for _, w in pairs(wlans) do
if w.parent == wlan.parent then
return true
end
end
return false
end
-- Returns the unit number of the rc.conf configured
-- (wlans_<parent>=wlan<unit>child) wlan device of a given parent wifi
-- device. If not found, nil is returned.
function netif.get_wlan_child_from_rc_conf(parent)
local w
local wlans = netif.wlans_from_rc_conf()
for _, w in pairs(wlans) do
if w.parent == parent then
return w.child
end
end
return nil
end
-- Returns the country code of the region defined in /var/db/zoneinfo,
-- or nil if not found
function netif.get_wlan_region()
local l, zone, code
local f, e = io.open(netif.path_zoneinfo)
if f == nil then
io.stderr:write(e)
return nil
end
for l in f:lines() do
if string.match(l, "/") then
zone = l
break
end
end
f:close()
if not zone then
return nil
end
f, e = io.open(netif.path_zone_tab)
if f == nil then
io.stderr:write(e)
return nil
end
for l in f:lines() do
code = string.match(l, "^(%u+)%s+[0-9+-]+%s+" .. zone)
if code then
break
end
end
f:close()
return code
end
-- Sleeps n seconds
function netif.sleep(n)
os.execute("sleep " .. tonumber(n))
end
-- Takes a driver name (e.g. if_ath) and waits for not more than "timeout"
-- seconds for the parent device matching the driver to appear in
-- net.wlan.devices. If found, the corresponding wlan device object is
-- returned, else nil.
function netif.wait_for_new_wlan(driver, timeout)
local devname = netif.kmod_to_dev(driver)
local tries = 1
while true do
local wlans = netif.get_wlan_devs()
local w = netif.find_wlan(devname, wlans)
if w == nil then
if tries >= timeout then
return nil
end
netif.sleep(1)
else
return w
end
tries = tries + 1
end
end
local function get_wlan_regdomain_args()
local country = netif.get_wlan_region()
if country == nil then
return ""
end
return "down country " .. country
end
function netif.restart_netif(ifname)
return os.execute("service netif restart " .. ifname)
end
function netif.run_sysrc(var)
return os.execute("sysrc " .. var)
end
function netif.set_rc_conf_var(var, val)
local rc_var = string.format('%s="%s"', var, val)
return netif.run_sysrc(rc_var)
end
function netif.create_wlan_child_dev(parent, child_unit)
local cmd = string.format("ifconfig wlan%d create wlandev %s",
child_unit, parent)
return os.execute(cmd)
end
function netif.add_wlan_to_rc_conf(wlan)
local args
netif.set_rc_conf_var("wlans_" .. wlan.parent, "wlan" .. wlan.child)
if wlan_set_country then
args = get_wlan_regdomain_args()
end
if wlan_create_args ~= nil then
if args == nil then
args = wlan_create_args
else
args = args .. " " .. wlan_create_args
end
end
if args ~= nil then
netif.set_rc_conf_var("create_args_wlan" .. wlan.child, args)
end
netif.set_rc_conf_var("ifconfig_wlan" .. wlan.child, wlan_ifconfig_args)
if enable_ipv6 then
netif.set_rc_conf_var("ifconfig_wlan" .. wlan.child .. "_ipv6",
wlan_ifconfig_ipv6_args)
end
end
-- Creates and configures a new wlan child device (wlanX) for each wlan
-- device object which doesn't have a child device yet.
function netif.create_wlan_devs()
local w, max_unit, dev_is_new
local rc_wlans = netif.wlans_from_rc_conf()
local wlans = netif.get_wlan_devs()
if wlan_ifconfig_args == nil then
wlan_ifconfig_args = "up scan WPA DHCP"
end
if wlan_ifconfig_ipv6_args == nil then
wlan_ifconfig_ipv6_args = "inet6 accept_rtadv"
end
-- Calculate the next available unit number for the child device
max_unit = -1
for _, w in pairs(rc_wlans) do
if w.child ~= nil and w.child > max_unit then
max_unit = w.child
end
end
for _, w in pairs(wlans) do
if w.child ~= nil then
if w.child > max_unit then
max_unit = w.child
end
end
end
-- Create a child device for each parent device which doesn't have
-- a child ("wlanX"), and wasn't configured via /etc/rc.conf with
-- 'wlans_parent="wlan<max_unit>"'
for _, w in pairs(wlans) do
if w.child == nil then
dev_is_new = true
else
dev_is_new = false
end
if ignore_netifs == nil or
netif.find_netif(w.parent, ignore_netifs) == nil then
if not netif.wlan_rc_configured(w) and dev_is_new then
if w.child == nil then
max_unit = max_unit + 1
w.child = max_unit
netif.create_wlan_child_dev(w.parent, max_unit)
end
netif.add_wlan_to_rc_conf(w)
end
if dev_is_new then
w.child = netif.get_wlan_child_from_rc_conf(w.parent)
if w.child ~= nil then
local child = "wlan" .. w.child
local status = netif.link_status(child)
if status == nil or status ~= "associated" then
netif.restart_netif(child)
end
end
end
end
end
end
-- Takes a driver name (e.g. if_alc) and waits for not more than "timeout"
-- seconds for the device matching the driver to appear in the list of
-- network interfaces. If found, the interface name is returned, else nil.
function netif.wait_for_new_ether(driver, timeout)
local devname = netif.kmod_to_dev(driver)
local tries = 1
while true do
local iflist = netif.get_netifs()
local ifname = netif.find_netif(devname, iflist)
if ifname == nil then
if tries >= timeout then
return nil
end
netif.sleep(1)
else
return ifname
end
tries = tries + 1
end
end
-- Starts DHCP on each ethernet device.
function netif.setup_ether_devs()
local i
local iflist = netif.get_netifs()
if ether_ifconfig_args == nil then
ether_ifconfig_args = "up DHCP"
end
if ether_ifconfig_ipv6_args == nil then
ether_ifconfig_ipv6_args = "inet6 accept_rtadv"
end
for _, i in pairs(iflist) do
if ignore_netifs == nil or
netif.find_netif(i, ignore_netifs) == nil then
if not string.match(i, "wlan") then
if not netif.in_rc_conf(i, '') then
netif.set_rc_conf_var('ifconfig_' .. i, ether_ifconfig_args)
end
if not netif.in_rc_conf(i, '_ipv6') and enable_ipv6 then
netif.set_rc_conf_var('ifconfig_' .. i .. "_ipv6",
ether_ifconfig_ipv6_args)
end
local status = netif.link_status(i)
if not netif.link_is_up(i) then
netif.restart_netif(i)
end
end
end
end
end
-- Configures and starts all network interfaces based on the given driver/kmod
-- name.
function netif.config_netif(kmod)
if netif_wait_max == nil then
netif_wait_max = 1
end
local is_netif, iftype = netif.match_netif_type(kmod)
if is_netif and iftype == netif.NETIF_TYPE_WLAN then
if netif.wait_for_new_wlan(kmod, netif_wait_max) ~= nil then
netif.create_wlan_devs()
end
elseif is_netif and iftype == netif.NETIF_TYPE_ETHER then
if netif.wait_for_new_ether(kmod, netif_wait_max) ~= nil then
netif.setup_ether_devs()
end
end
end
return netif