diff mbox series

[OpenWrt-Devel] wlan factory defaults

Message ID 9146E54F-C4CD-4E4E-AFA0-180B36AFDE59@gmail.com
State Not Applicable
Headers show
Series [OpenWrt-Devel] wlan factory defaults | expand

Commit Message

Ivan Baktsheev Nov. 11, 2019, 6:09 p.m. UTC
Hi,

I want to add new device, but as of now I can upload firmware wirelessly and then I need to connect using wired connection, because in stock OpenWrt image wireless is disabled.

For my router (and probably many others) default WiFi SSID stored in factory partition and it’s quite easy to extract. Unfortunately, wireless setup takes place in different time than wired/gpio/leds setup from /etc/board.d and defaults are hardcoded in /lib/wifi/mac80211.sh

Currently, I have /etc/board.d/04_factory_defaults, which extracts factory defaults for root password and WiFi. WiFi settings written into /etc/factory_80211, which then read at /lib/wifi/mac80211.sh

I would like to integrate this into OpenWrt, please take a look at full example at https://gist.github.com/apla/a81cad0a1dc9fedfeff19941116da369 and share your thoughts. Diff for /lib/wifi/mac80211.sh:

Comments

Adrian Schmutzler Nov. 11, 2019, 6:15 p.m. UTC | #1
Hi,

> -----Original Message-----
> From: openwrt-devel [mailto:openwrt-devel-bounces@lists.openwrt.org] On
> Behalf Of Ivan Baktsheev
> Sent: Montag, 11. November 2019 19:10
> To: openwrt-devel@lists.openwrt.org
> Subject: [OpenWrt-Devel] wlan factory defaults
> 
> Hi,
> 
> I want to add new device, but as of now I can upload firmware wirelessly and
> then I need to connect using wired connection, because in stock OpenWrt image
> wireless is disabled.
> 
> For my router (and probably many others) default WiFi SSID stored in factory
> partition and it’s quite easy to extract. Unfortunately, wireless setup takes place
> in different time than wired/gpio/leds setup from /etc/board.d and defaults are
> hardcoded in /lib/wifi/mac80211.sh

Have a look at the following two Pull Requests in GitHub, dealing with the same problem for some time already:
https://github.com/openwrt/openwrt/pull/2408
https://github.com/openwrt/openwrt/pull/2445

Best

Adrian
Piotr Dymacz Nov. 11, 2019, 6:35 p.m. UTC | #2
Hi Ivan,

On 11.11.2019 19:09, Ivan Baktsheev wrote:
> Hi,
> 
> I want to add new device, but as of now I can upload firmware wirelessly and then I need to connect using wired connection, because in stock OpenWrt image wireless is disabled.
> 
> For my router (and probably many others) default WiFi SSID stored in factory partition and it’s quite easy to extract. Unfortunately, wireless setup takes place in different time than wired/gpio/leds setup from /etc/board.d and defaults are hardcoded in /lib/wifi/mac80211.sh
> 
> Currently, I have /etc/board.d/04_factory_defaults, which extracts factory defaults for root password and WiFi. WiFi settings written into /etc/factory_80211, which then read at /lib/wifi/mac80211.sh
> 
> I would like to integrate this into OpenWrt, please take a look at full example at https://gist.github.com/apla/a81cad0a1dc9fedfeff19941116da369 and share your thoughts. Diff for /lib/wifi/mac80211.sh:

IMHO, that would be just too much bloat. OpenWrt is a generic platform 
(or a distribution), thus _personally_ I don't think we should support 
all the different ways vendors use to keep their software settings. 
Otherwise, we end up with bunch of scripts fetching data for various 
devices and that wouldn't get ever unified and probably at some point, 
unmaintainable. What's more, we can't trust values stored in vendor 
settings - in worse case, without sophisticated data verification, we 
might end up with broken uci configuration.
Ivan Baktsheev Nov. 11, 2019, 7:33 p.m. UTC | #3
Piotr,

> On 11 Nov 2019, at 21:35, Piotr Dymacz <pepe2k@gmail.com> wrote:
> 
> Hi Ivan,
> 
> On 11.11.2019 19:09, Ivan Baktsheev wrote:
>> Hi,
>> I want to add new device, but as of now I can upload firmware wirelessly and then I need to connect using wired connection, because in stock OpenWrt image wireless is disabled.
>> For my router (and probably many others) default WiFi SSID stored in factory partition and it’s quite easy to extract. Unfortunately, wireless setup takes place in different time than wired/gpio/leds setup from /etc/board.d and defaults are hardcoded in /lib/wifi/mac80211.sh
>> Currently, I have /etc/board.d/04_factory_defaults, which extracts factory defaults for root password and WiFi. WiFi settings written into /etc/factory_80211, which then read at /lib/wifi/mac80211.sh
>> I would like to integrate this into OpenWrt, please take a look at full example at https://gist.github.com/apla/a81cad0a1dc9fedfeff19941116da369 and share your thoughts. Diff for /lib/wifi/mac80211.sh:
> 
> IMHO, that would be just too much bloat. OpenWrt is a generic platform (or a distribution), thus _personally_ I don't think we should support all the different ways vendors use to keep their software settings. Otherwise, we end up with bunch of scripts fetching data for various devices and that wouldn't get ever unified and probably at some point, unmaintainable. What's more, we can't trust values stored in vendor settings - in worse case, without sophisticated data verification, we might end up with broken uci configuration.

I think the same. Vendors don’t care about common location to store defaults, this can be changed in very unusual ways. My patch for mac80211.sh just add possibility to override default values for wlan. If specific OpenWrt build contains /etc/factory_80211, then it will be processed and used, otherwise radio will be off, ssid = OpenWrt, no encryption, just like as of today.

How to put this configuration into device? This is not a question for OpenWrt developers, but for people, who build customized OpenWrt firmware. It’s definitely better to customise build using scripts/configurations in /etc, than changing /lib/wifi/mac80211.sh, as I saw in some builds.

> -- 
> Cheers,
> Piotr
> 
>> diff --git a/package/kernel/mac80211/files/lib/wifi/mac80211.sh b/package/kernel/mac80211/files/lib/wifi/mac80211.sh
>> index be9c537..3c88c81 100644
>> --- a/package/kernel/mac80211/files/lib/wifi/mac80211.sh
>> +++ b/package/kernel/mac80211/files/lib/wifi/mac80211.sh
>> @@ -1,10 +1,12 @@
>>  #!/bin/sh
>>  . /lib/netifd/mac80211.sh
>>    append DRIVERS "mac80211"
>>  +. /etc/factory_80211
>> +
>>  lookup_phy() {
>>  	[ -n "$phy" ] && {
>>  		[ -d /sys/class/ieee80211/$phy ] && return
>>  	}
>>  @@ -95,25 +97,49 @@ detect_mac80211() {
>>  			dev_id="set wireless.radio${devidx}.path='$path'"
>>  		else
>>  			dev_id="set wireless.radio${devidx}.macaddr=$(cat /sys/class/ieee80211/${dev}/macaddress)"
>>  		fi
>>  +		eval radio_i_disabled=\$radio${devidx}_disabled
>> +		radio_i_disabled=${radio_i_disabled:-${radio_disabled:-1}}
>> +		eval radio_i_country=\$radio${devidx}_country
>> +		radio_i_country=${radio_i_country:-$radio_country}
>> +		if [ ! -z "$radio_i_country" ] ; then
>> +			uci_radio_country="set wireless.radio${devidx}.country=${radio_i_country}"
>> +		else
>> +			uci_radio_country=
>> +		fi
>> +
>> +		eval wlan_i_ssid=\$wlan${devidx}_ssid
>> +		wlan_i_ssid=${wlan_i_ssid:-${wlan_ssid:-OpenWrt}}
>> +		eval wlan_i_encryption=\$wlan${devidx}_encryption
>> +		wlan_i_encryption=${wlan_i_encryption:-${wlan_encryption:-none}}
>> +		eval wlan_i_key=\$wlan${devidx}_key
>> +		wlan_i_key=${wlan_i_key:-$wlan_key}
>> +		if [ ! -z "$wlan_i_key" ] ; then
>> +			uci_wlan_key="set wireless.default_radio${devidx}.key=${wlan_i_key}"
>> +		else
>> +			uci_wlan_key=
>> +		fi
>> +
>>  		uci -q batch <<-EOF
>>  			set wireless.radio${devidx}=wifi-device
>>  			set wireless.radio${devidx}.type=mac80211
>>  			set wireless.radio${devidx}.channel=${channel}
>>  			set wireless.radio${devidx}.hwmode=11${mode_band}
>>  			${dev_id}
>>  			${ht_capab}
>> -			set wireless.radio${devidx}.disabled=1
>> +			set wireless.radio${devidx}.disabled=${radio_i_disabled}
>> +			${uci_radio_country}
>>    			set wireless.default_radio${devidx}=wifi-iface
>>  			set wireless.default_radio${devidx}.device=radio${devidx}
>>  			set wireless.default_radio${devidx}.network=lan
>>  			set wireless.default_radio${devidx}.mode=ap
>> -			set wireless.default_radio${devidx}.ssid=OpenWrt
>> -			set wireless.default_radio${devidx}.encryption=none
>> +			set wireless.default_radio${devidx}.ssid=${wlan_i_ssid}
>> +			set wireless.default_radio${devidx}.encryption=${wlan_i_encryption}
>> +			${uci_wlan_key}
>>  EOF
>>  		uci -q commit wireless
>>    		devidx=$(($devidx + 1))
>>  	done
>> _______________________________________________
>> openwrt-devel mailing list
>> openwrt-devel@lists.openwrt.org
>> https://lists.openwrt.org/mailman/listinfo/openwrt-devel
Piotr Dymacz Nov. 11, 2019, 7:46 p.m. UTC | #4
Hi Ivan,

On 11.11.2019 20:33, Ivan Baktsheev wrote:
> Piotr,
> 
>> On 11 Nov 2019, at 21:35, Piotr Dymacz <pepe2k@gmail.com> wrote:
>> 
>> Hi Ivan,
>> 
>> On 11.11.2019 19:09, Ivan Baktsheev wrote:
>>> Hi,
>>> I want to add new device, but as of now I can upload firmware wirelessly and then I need to connect using wired connection, because in stock OpenWrt image wireless is disabled.
>>> For my router (and probably many others) default WiFi SSID stored in factory partition and it’s quite easy to extract. Unfortunately, wireless setup takes place in different time than wired/gpio/leds setup from /etc/board.d and defaults are hardcoded in /lib/wifi/mac80211.sh
>>> Currently, I have /etc/board.d/04_factory_defaults, which extracts factory defaults for root password and WiFi. WiFi settings written into /etc/factory_80211, which then read at /lib/wifi/mac80211.sh
>>> I would like to integrate this into OpenWrt, please take a look at full example at https://gist.github.com/apla/a81cad0a1dc9fedfeff19941116da369 and share your thoughts. Diff for /lib/wifi/mac80211.sh:
>> 
>> IMHO, that would be just too much bloat. OpenWrt is a generic platform (or a distribution), thus _personally_ I don't think we should support all the different ways vendors use to keep their software settings. Otherwise, we end up with bunch of scripts fetching data for various devices and that wouldn't get ever unified and probably at some point, unmaintainable. What's more, we can't trust values stored in vendor settings - in worse case, without sophisticated data verification, we might end up with broken uci configuration.
> 
> I think the same. Vendors don’t care about common location to store defaults, this can be changed in very unusual ways. My patch for mac80211.sh just add possibility to override default values for wlan. If specific OpenWrt build contains /etc/factory_80211, then it will be processed and used, otherwise radio will be off, ssid = OpenWrt, no encryption, just like as of today.
> 
> How to put this configuration into device? This is not a question for OpenWrt developers, but for people, who build customized OpenWrt firmware. It’s definitely better to customise build using scripts/configurations in /etc, than changing /lib/wifi/mac80211.sh, as I saw in some builds.

A custom 'uci-defaults' script included in image, using './files' 
approach should do the job, see the Wiki for more information:

https://openwrt.org/docs/guide-developer/build-system/use-buildsystem#custom_files

https://openwrt.org/docs/guide-developer/uci-defaults
Baptiste Jonglez Nov. 11, 2019, 7:57 p.m. UTC | #5
Hi,

On 11-11-19, Piotr Dymacz wrote:
> On 11.11.2019 20:33, Ivan Baktsheev wrote:
> > How to put this configuration into device? This is not a question for OpenWrt developers, but for people, who build customized OpenWrt firmware. It’s definitely better to customise build using scripts/configurations in /etc, than changing /lib/wifi/mac80211.sh, as I saw in some builds.
> 
> A custom 'uci-defaults' script included in image, using './files' approach
> should do the job, see the Wiki for more information:
> 
> https://openwrt.org/docs/guide-developer/build-system/use-buildsystem#custom_files
> 
> https://openwrt.org/docs/guide-developer/uci-defaults

+1, I was also messing with /lib/wifi/mac80211.sh until recently, but
using a uci-defaults script is much easier and works the same with all
versions of OpenWrt.

Here is the script I am using: https://gist.github.com/zorun/86c825c80599af244bcfce742b6b3ba2

I am writing the Wi-Fi password into the image with the imagebuilder, but
you could obtain your configuration parameters any way you want.

Baptiste
diff mbox series

Patch

diff --git a/package/kernel/mac80211/files/lib/wifi/mac80211.sh b/package/kernel/mac80211/files/lib/wifi/mac80211.sh
index be9c537..3c88c81 100644
--- a/package/kernel/mac80211/files/lib/wifi/mac80211.sh
+++ b/package/kernel/mac80211/files/lib/wifi/mac80211.sh
@@ -1,10 +1,12 @@ 
 #!/bin/sh
 . /lib/netifd/mac80211.sh
 
 append DRIVERS "mac80211"
 
+. /etc/factory_80211
+
 lookup_phy() {
 	[ -n "$phy" ] && {
 		[ -d /sys/class/ieee80211/$phy ] && return
 	}
 
@@ -95,25 +97,49 @@  detect_mac80211() {
 			dev_id="set wireless.radio${devidx}.path='$path'"
 		else
 			dev_id="set wireless.radio${devidx}.macaddr=$(cat /sys/class/ieee80211/${dev}/macaddress)"
 		fi
 
+		eval radio_i_disabled=\$radio${devidx}_disabled
+		radio_i_disabled=${radio_i_disabled:-${radio_disabled:-1}}
+		eval radio_i_country=\$radio${devidx}_country
+		radio_i_country=${radio_i_country:-$radio_country}
+		if [ ! -z "$radio_i_country" ] ; then
+			uci_radio_country="set wireless.radio${devidx}.country=${radio_i_country}"
+		else
+			uci_radio_country=
+		fi
+
+		eval wlan_i_ssid=\$wlan${devidx}_ssid
+		wlan_i_ssid=${wlan_i_ssid:-${wlan_ssid:-OpenWrt}}
+		eval wlan_i_encryption=\$wlan${devidx}_encryption
+		wlan_i_encryption=${wlan_i_encryption:-${wlan_encryption:-none}}
+		eval wlan_i_key=\$wlan${devidx}_key
+		wlan_i_key=${wlan_i_key:-$wlan_key}
+		if [ ! -z "$wlan_i_key" ] ; then
+			uci_wlan_key="set wireless.default_radio${devidx}.key=${wlan_i_key}"
+		else
+			uci_wlan_key=
+		fi
+
 		uci -q batch <<-EOF
 			set wireless.radio${devidx}=wifi-device
 			set wireless.radio${devidx}.type=mac80211
 			set wireless.radio${devidx}.channel=${channel}
 			set wireless.radio${devidx}.hwmode=11${mode_band}
 			${dev_id}
 			${ht_capab}
-			set wireless.radio${devidx}.disabled=1
+			set wireless.radio${devidx}.disabled=${radio_i_disabled}
+			${uci_radio_country}
 
 			set wireless.default_radio${devidx}=wifi-iface
 			set wireless.default_radio${devidx}.device=radio${devidx}
 			set wireless.default_radio${devidx}.network=lan
 			set wireless.default_radio${devidx}.mode=ap
-			set wireless.default_radio${devidx}.ssid=OpenWrt
-			set wireless.default_radio${devidx}.encryption=none
+			set wireless.default_radio${devidx}.ssid=${wlan_i_ssid}
+			set wireless.default_radio${devidx}.encryption=${wlan_i_encryption}
+			${uci_wlan_key}
 EOF
 		uci -q commit wireless
 
 		devidx=$(($devidx + 1))
 	done