Skip to content

Commit

Permalink
Improved the devd files further setup-nic.py and auto-switch.py.
Browse files Browse the repository at this point in the history
Added link-up.py to handle the linkup for ethernet with devd.
  • Loading branch information
ericbsd committed Feb 19, 2024
1 parent 7848325 commit 325d94c
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 12 deletions.
2 changes: 2 additions & 0 deletions NetworkMgr/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,8 @@ def update_system(self):
defaultrouter_line = f'defaultrouter="{defaultrouter}"\n'
self.remove_rc_conf_line(defaultrouter_line)
restart_card_network(nic)
# sometimes the inet address isn't available immediately after dhcp is enabled.
start_static_network(nic, inet, netmask)
wait_inet(nic)
restart_rounting_and_dhcp(nic)

Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ def datafilelist(installbase, sourcebase):

networkmgr_share = [
'src/auto-switch.py',
'src/link-up.py',
'src/setup-nic.py'
]

Expand Down
41 changes: 40 additions & 1 deletion src/auto-switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,28 @@

default_nic = Popen(
'netstat -rn | grep default',
stdout=PIPE,
shell=True,
universal_newlines=True
).stdout.read()

nics = Popen(
['ifconfig', '-l', 'ether'],
stdout=PIPE,
close_fds=True,
universal_newlines=True
)

nics_left_over = nics.stdout.read().replace(nic, '').strip()
nic_list = sorted(re.sub(not_nics_regex, '', nics_left_over).strip().split())

# Stop the script if the nic is not valid or not in the default route.
if re.search(not_nics_regex, nic):
exit(0)
elif nic not in default_nic:
exit(0)
elif not nic_list:
exit(0)

nic_ifconfig = Popen(
['ifconfig', nic],
Expand All @@ -35,6 +48,13 @@
universal_newlines=True
).stdout.read()

dhcp = Popen(
['sysrc', '-n', f'ifconfig_{nic}'],
stdout=PIPE,
close_fds=True,
universal_newlines=True
).stdout.read()

active_status = (
'status: active' in nic_ifconfig,
'status: associated' in nic_ifconfig
Expand All @@ -46,4 +66,23 @@
# or associated.
if not any(active_status):
os.system(f'service netif stop {nic}')
os.system('service routing restart')
if dhcp.strip() == 'DHCP':
for current_nic in nic_list:
output = Popen(
['ifconfig', current_nic],
stdout=PIPE,
close_fds=True,
universal_newlines=True
)
nic_ifconfig = output.stdout.read()
status_types = [
'active',
'associated',
]
found_status = re.search(f"status: ({'|'.join(status_types)})", nic_ifconfig)
found_inet = re.search("inet(\s|6)", nic_ifconfig)
if found_status and found_inet:
os.system(f'service dhclient restart {current_nic}')
break
else:
os.system('service routing restart')
58 changes: 58 additions & 0 deletions src/link-up.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#!/usr/local/bin/python3

import os
import re
import sys
from subprocess import Popen, PIPE, run

args = sys.argv
if len(args) != 2:
exit(1)
nic = args[1]

not_nics_regex = "(enc|lo|fwe|fwip|tap|plip|pfsync|pflog|ipfw|tun|sl|faith|" \
"ppp|bridge|wg|wlan)[0-9]+|vm-[a-z]+"

# Stop the script if the nic is not valid.
if re.search(not_nics_regex, nic):
exit(0)

dhcp = Popen(
['sysrc', '-n', f'ifconfig_{nic}'],
stdout=PIPE,
close_fds=True,
universal_newlines=True
).stdout.read()

if os.path.exists(f'/tmp/network-{nic}'):
network = open(f'/tmp/network-{nic}', 'r').read()
if 'attached' in network:
if dhcp.strip() == 'DHCP':
Popen(f'service dhclient quietstart {nic}', shell=True)
else:
Popen(f'service routing restart', shell=True)
with open(f'/tmp/network-{nic}', 'w') as network:
network.writelines(f'linked')
exit(0)

nic_ifconfig = Popen(
['ifconfig', nic],
stdout=PIPE,
close_fds=True,
universal_newlines=True
).stdout.read()

if 'inet ' in nic_ifconfig:
Popen(
f'service routing restart ; '
f'service dhclient restart {nic}',
shell=True
)
else:
Popen(
f'service netif start {nic} ; '
'sleep 1 ; '
f'service routing restart ; '
f'service dhclient restart {nic}',
shell=True
)
2 changes: 1 addition & 1 deletion src/networkmgr.conf
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ notify 100 {
match "system" "IFNET";
match "type" "LINK_UP";
media-type "ethernet";
action "/usr/local/share/networkmgr/setup-nic.py $subsystem";
action "/usr/local/share/networkmgr/link-up.py $subsystem";
};

notify 100 {
Expand Down
15 changes: 5 additions & 10 deletions src/setup-nic.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,12 @@ def file_content(paths):
with rc_conf.open('a') as rc:
rc.writelines(f'wlans_{nic}="wlan{wlanNum}"\n')
rc.writelines(f'ifconfig_wlan{wlanNum}="WPA DHCP"\n')
Popen(f'/etc/pccard_ether {nic} startchildren', shell=True)

break
else:
if f'ifconfig_{nic}=' not in rc_conf_content:
with rc_conf.open('a') as rc:
rc.writelines(f'ifconfig_{nic}="DHCP"\n')
Popen('/etc/pccard_ether {nic} startchildren', shell=True)
else:
Popen(
f'service netif start {nic} ; '
f'service dhclient start {nic} ; '
f'service routing restart',
shell=True
)
with open(f'/tmp/network-{nic}', 'w') as network:
network.writelines(f'attached')

Popen(f'/etc/pccard_ether {nic} startchildren', shell=True)

0 comments on commit 325d94c

Please sign in to comment.