Skip to content

Commit d9e0017

Browse files
authored
Merge pull request #1932 from RaspAP/maint/hostapd-json-cfg
Maintenance: Retrieve hostapd mode settings from defaults.json
2 parents c3cc4ff + 75577ec commit d9e0017

File tree

2 files changed

+133
-92
lines changed

2 files changed

+133
-92
lines changed

config/defaults.json

Lines changed: 107 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,114 @@
11
{
2-
"dhcp": {
3-
"wlan0": {
4-
"static ip_address": [ "10.3.141.1/24" ],
5-
"static routers": [ "10.3.141.1" ],
6-
"static domain_name_server": [ "1.1.1.1 8.8.8.8" ],
7-
"subnetmask": [ "255.255.255.0" ]
2+
"hostapd":{
3+
"modes":{
4+
"n":{
5+
"settings":[
6+
"hw_mode=g",
7+
"ieee80211n=1",
8+
"wmm_enabled=1"
9+
]
10+
},
11+
"ac":{
12+
"settings":[
13+
"hw_mode=a",
14+
"# N",
15+
"ieee80211n=1",
16+
"require_ht=1",
17+
"ht_capab=[MAX-AMSDU-3839][HT40+][SHORT-GI-20][SHORT-GI-40][DSSS_CCK-40]",
18+
"# AC",
19+
"ieee80211ac=1",
20+
"require_vht=1",
21+
"ieee80211d=0",
22+
"ieee80211h=0",
23+
"vht_capab=[MAX-AMSDU-3839][SHORT-GI-80]",
24+
"vht_oper_chwidth=1",
25+
"vht_oper_centr_freq_seg0_idx={VHT_FREQ_IDX}"
26+
]
27+
},
28+
"g":{
29+
"settings":[
30+
"hw_mode=g",
31+
"ieee80211n=0"
32+
]
33+
},
34+
"a":{
35+
"settings":[
36+
"hw_mode=a",
37+
"ieee80211n=0"
38+
]
39+
},
40+
"b":{
41+
"settings":[
42+
"hw_mode=b",
43+
"ieee80211n=0"
44+
]
45+
}
846
},
9-
"wlan1": {
10-
"static ip_address": [ "10.9.141.1/24" ],
11-
"static routers": [ "10.9.141.1" ],
12-
"static domain_name_server": [ "1.1.1.1 8.8.8.8" ],
13-
"subnetmask": [ "255.255.255.0" ]
47+
"dhcp":{
48+
"wlan0":{
49+
"static ip_address":[ "10.3.141.1/24" ],
50+
"static routers":[ "10.3.141.1" ],
51+
"static domain_name_server":[ "1.1.1.1 8.8.8.8" ],
52+
"subnetmask":[ "255.255.255.0" ]
53+
},
54+
"wlan1":{
55+
"static ip_address":[ "10.9.141.1/24" ],
56+
"static routers":[ "10.9.141.1" ],
57+
"static domain_name_server":[ "1.1.1.1 8.8.8.8" ],
58+
"subnetmask":[ "255.255.255.0" ]
59+
},
60+
"uap0":{
61+
"static ip_address":[ "192.168.50.1/24" ],
62+
"static routers":[ "192.168.50.1" ],
63+
"static domain_name_server":[ "1.1.1.1 8.8.8.8" ],
64+
"subnetmask":[ "255.255.255.0" ]
65+
},
66+
"options":{
67+
"# RaspAP default configuration":null,
68+
"hostname":null,
69+
"clientid":null,
70+
"persistent":null,
71+
"option rapid_commit":null,
72+
"option domain_name_servers, domain_name, domain_search, host_name":null,
73+
"option classless_static_routes":null,
74+
"option ntp_servers":null,
75+
"require dhcp_server_identifier":null,
76+
"slaac private":null,
77+
"nohook lookup-hostname":null
78+
}
1479
},
15-
"uap0": {
16-
"static ip_address": [ "192.168.50.1/24" ],
17-
"static routers": [ "192.168.50.1" ],
18-
"static domain_name_server": [ "1.1.1.1 8.8.8.8" ],
19-
"subnetmask": [ "255.255.255.0" ]
80+
"dnsmasq":{
81+
"wlan0":{
82+
"dhcp-range":[ "10.3.141.50,10.3.141.254,255.255.255.0,12h" ]
83+
},
84+
"wlan1":{
85+
"dhcp-range":[ "10.9.141.50,10.9.141.254,255.255.255.0,12h" ]
86+
},
87+
"uap0":{
88+
"dhcp-range":[ "192.168.50.50,192.168.50.150,12h" ]
89+
}
2090
},
21-
"options": {
22-
"# RaspAP default configuration": null,
23-
"hostname": null,
24-
"clientid": null,
25-
"persistent": null,
26-
"option rapid_commit": null,
27-
"option domain_name_servers, domain_name, domain_search, host_name": null,
28-
"option classless_static_routes": null,
29-
"option ntp_servers": null,
30-
"require dhcp_server_identifier": null,
31-
"slaac private": null,
32-
"nohook lookup-hostname": null
33-
}
34-
},
35-
"dnsmasq": {
36-
"wlan0": {
37-
"dhcp-range": [ "10.3.141.50,10.3.141.254,255.255.255.0,12h" ]
38-
},
39-
"wlan1": {
40-
"dhcp-range": [ "10.9.141.50,10.9.141.254,255.255.255.0,12h" ]
41-
},
42-
"uap0": {
43-
"dhcp-range": [ "192.168.50.50,192.168.50.150,12h" ]
44-
}
45-
},
46-
"wireguard": {
47-
"server": {
48-
"Address": [ "10.8.2.1/24" ],
49-
"ListenPort": [ "51820" ],
50-
"DNS": [ "9.9.9.9" ],
51-
"PostUp": [ "iptables -A FORWARD -i wlan0 -o wg0 -j ACCEPT; iptables -A FORWARD -i wg0 -o wlan0 -m state --state RELATED,ESTABLISHED -j ACCEPT; iptables -t nat -A POSTROUTING -o wg0 -j MASQUERADE" ],
52-
"PostDown": [ "iptables -D FORWARD -i wlan0 -o wg0 -j ACCEPT; iptables -D FORWARD -i wg0 -o wlan0 -m state --state RELATED,ESTABLISHED -j ACCEPT; iptables -t nat -D POSTROUTING -o wg0 -j MASQUERADE" ],
53-
"PostUpEx": [ "iptables -I OUTPUT ! -o %i -m mark ! --mark $(wg show %i fwmark) -m addrtype ! --dst-type LOCAL ! -d %s -j REJECT" ],
54-
"PreDown": [ "iptables -D OUTPUT ! -o %i -m mark ! --mark $(wg show %i fwmark) -m addrtype ! --dst-type LOCAL ! -d %s -j REJECT" ]
55-
},
56-
"peer": {
57-
"Address": [ "10.8.1.2/24" ],
58-
"Endpoint": [ "10.8.2.1:51820" ],
59-
"ListenPort": [ "21841" ],
60-
"AllowedIPs": ["10.8.2.0/24"],
61-
"PersistentKeepalive": [ "15" ]
62-
}
63-
},
64-
"txpower": {
65-
"dbm": [ "auto", "30", "20", "17", "10", "6", "3", "1", "0" ]
91+
"wireguard":{
92+
"server":{
93+
"Address":[ "10.8.2.1/24" ],
94+
"ListenPort":[ "51820" ],
95+
"DNS":[ "9.9.9.9" ],
96+
"PostUp":[ "iptables -A FORWARD -i wlan0 -o wg0 -j ACCEPT; iptables -A FORWARD -i wg0 -o wlan0 -m state --state RELATED,ESTABLISHED -j ACCEPT; iptables -t nat -A POSTROUTING -o wg0 -j MASQUERADE" ],
97+
"PostDown":[ "iptables -D FORWARD -i wlan0 -o wg0 -j ACCEPT; iptables -D FORWARD -i wg0 -o wlan0 -m state --state RELATED,ESTABLISHED -j ACCEPT; iptables -t nat -D POSTROUTING -o wg0 -j MASQUERADE" ],
98+
"PostUpEx":[ "iptables -I OUTPUT ! -o %i -m mark ! --mark $(wg show %i fwmark) -m addrtype ! --dst-type LOCAL ! -d %s -j REJECT" ],
99+
"PreDown":[ "iptables -D OUTPUT ! -o %i -m mark ! --mark $(wg show %i fwmark) -m addrtype ! --dst-type LOCAL ! -d %s -j REJECT" ]
100+
},
101+
"peer":{
102+
"Address":[ "10.8.1.2/24" ],
103+
"Endpoint":[ "10.8.2.1:51820" ],
104+
"ListenPort":[ "21841" ],
105+
"AllowedIPs":[ "10.8.2.0/24" ],
106+
"PersistentKeepalive":[ "15" ]
107+
}
108+
},
109+
"txpower": {
110+
"dbm": [ "auto", "30", "20", "17", "10", "6", "3", "1", "0" ]
111+
}
66112
}
67113
}
68114

src/RaspAP/Networking/Hotspot/HostapdManager.php

Lines changed: 26 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,8 @@ public function validate(
143143
public function buildConfig(array $params, StatusMessage $status): string
144144
{
145145
$config = [];
146+
147+
// core static values
146148
$config[] = 'driver=nl80211';
147149
$config[] = 'ctrl_interface=' . RASPI_HOSTAPD_CTRL_INTERFACE;
148150
$config[] = 'ctrl_interface_group=0';
@@ -172,49 +174,41 @@ public function buildConfig(array $params, StatusMessage $status): string
172174
$config[] = 'wpa_key_mgmt=' . $wpa_key_mgmt;
173175

174176
if (!empty($params['beacon_interval'])) {
175-
$config[] = 'beacon_int=' . $params['beacon_interval'];
177+
$config[] = 'beacon_int=' . intval($params['beacon_interval']);
176178
}
177179

178180
if (!empty($params['disassoc_low_ack'])) {
179181
$config[] = 'disassoc_low_ack=0';
180182
}
181183

184+
// SSID and channel (required)
182185
$config[] = 'ssid=' . $params['ssid'];
183186
$config[] = 'channel=' . $params['channel'];
184187

185-
// Choose VHT segment index (fallback only if required)
188+
// choose VHT segment index (fallback only if required)
186189
$vht_freq_idx = ($params['channel'] < RASPI_5GHZ_CHANNEL_MIN) ? 42 : 155;
190+
$hwMode = isset($params['hw_mode']) ? $params['hw_mode'] : '';
191+
192+
// fetch settings for selected mode
193+
$modeSettings = getDefaultNetOpts('hostapd', 'modes', $hwMode);
194+
$settings = $modeSettings[$hwMode]['settings'] ?? [];
187195

188-
switch ($params['hw_mode']) {
189-
case 'n':
190-
$config[] = 'hw_mode=g';
191-
$config[] = 'ieee80211n=1';
192-
$config[] = 'wmm_enabled=1';
193-
break;
194-
case 'ac':
195-
$config[] = 'hw_mode=a';
196-
$config[] = '# N';
197-
$config[] = 'ieee80211n=1';
198-
$config[] = 'require_ht=1';
199-
$config[] = 'ht_capab=[MAX-AMSDU-3839][HT40+][SHORT-GI-20][SHORT-GI-40][DSSS_CCK-40]';
200-
$config[] = '# AC';
201-
$config[] = 'ieee80211ac=1';
202-
$config[] = 'require_vht=1';
203-
$config[] = 'ieee80211d=0';
204-
$config[] = 'ieee80211h=0';
205-
$config[] = 'vht_capab=[MAX-AMSDU-3839][SHORT-GI-80]';
206-
$config[] = 'vht_oper_chwidth=1';
207-
$config[] = 'vht_oper_centr_freq_seg0_idx=' . $vht_freq_idx;
208-
break;
209-
default:
210-
$config[] = 'hw_mode=' . $params['hw_mode'];
211-
$config[] = 'ieee80211n=0';
196+
if (!empty($settings)) {
197+
foreach ($settings as $line) {
198+
if (!is_string($line)) {
199+
continue;
200+
}
201+
$replaced = str_replace('{VHT_FREQ_IDX}', (string) $vht_freq_idx ?? '',$line);
202+
$config[] = $replaced;
203+
}
212204
}
213205

214-
if ($params['wpa'] !== 'none') {
206+
// WPA passphrase
207+
if ($wpa_numeric !== 'none' && !empty($params['wpa_passphrase'])) {
215208
$config[] = 'wpa_passphrase=' . $params['wpa_passphrase'];
216209
}
217210

211+
// bridge handling
218212
if (!empty($params['bridge'])) {
219213
$config[] = 'interface=' . $params['interface'];
220214
$config[] = 'bridge=' . $params['bridge'];
@@ -223,17 +217,18 @@ public function buildConfig(array $params, StatusMessage $status): string
223217
}
224218

225219
$config[] = 'wpa=' . $wpa;
226-
$config[] = 'wpa_pairwise=' . $params['wpa_pairwise'];
227-
$config[] = 'country_code=' . $params['country_code'];
228-
$config[] = 'ignore_broadcast_ssid=' . $params['hiddenSSID'];
220+
$config[] = 'wpa_pairwise=' . ($params['wpa_pairwise'] ?? '');
221+
$config[] = 'country_code=' . ($params['country_code'] ?? '');
222+
$config[] = 'ignore_broadcast_ssid=' . ($params['hiddenSSID'] ?? 0);
223+
229224
if (!empty($params['max_num_sta'])) {
230225
$config[] = 'max_num_sta=' . (int)$params['max_num_sta'];
231226
}
232227

233228
// optional additional user config
234229
$config[] = $this->parseUserHostapdCfg();
235230

236-
return implode(PHP_EOL, $config) . PHP_EOL;
231+
return implode(PHP_EOL, array_filter($config, function ($v) { return $v !== null && $v !== ''; })) . PHP_EOL;
237232
}
238233

239234
/**

0 commit comments

Comments
 (0)