diff mbox series

[2/4] package/wpa_supplicant: adding ifupdown support

Message ID 20220527103335.1968203-3-angelo@amarulasolutions.com
State Superseded
Headers show
Series Better wifi handling | expand

Commit Message

Angelo Compagnucci May 27, 2022, 10:33 a.m. UTC
Actually, configuring a wifi interface as per "interfaces" man:

auto wlan0
iface wlan0 inet dhcp
wpa-conf /etc/wpa_supplicant.conf

doesn't work on buildroot because the line wpa-conf is ignored due to
the lack of a proper ifupdown script to handle the wpa_supplicant
initialization.

The provided file is a simplified version based on the one available
on debian.

Signed-off-by: Angelo Compagnucci <angelo@amarulasolutions.com>
---
 package/wpa_supplicant/ifupdown.sh       | 71 ++++++++++++++++++++++++
 package/wpa_supplicant/wpa_supplicant.mk | 10 ++++
 2 files changed, 81 insertions(+)
 create mode 100755 package/wpa_supplicant/ifupdown.sh

Comments

Thomas Petazzoni June 1, 2022, 7:25 p.m. UTC | #1
Hello,

On Fri, 27 May 2022 12:33:33 +0200
Angelo Compagnucci <angelo@amarulasolutions.com> wrote:

> Actually, configuring a wifi interface as per "interfaces" man:
> 
> auto wlan0
> iface wlan0 inet dhcp
> wpa-conf /etc/wpa_supplicant.conf

Do you have a link to an interfaces manpage that documents wpa-conf?

Is this supported by the Busybox ifupdown?

> diff --git a/package/wpa_supplicant/ifupdown.sh b/package/wpa_supplicant/ifupdown.sh
> new file mode 100755
> index 0000000000..8eecf73436
> --- /dev/null
> +++ b/package/wpa_supplicant/ifupdown.sh
> @@ -0,0 +1,71 @@
> +#!/bin/sh
> +
> +# This file is executed by ifupdown in pre-up, post-up, pre-down and
> +# post-down phases of network interface configuration.
> +
> +WPA_SUP_BIN="/usr/sbin/wpa_supplicant"
> +
> +if [ -n "$IF_WPA_MAINT_DEBUG" ]; then
> +	set -x
> +fi

Where is IF_WPA_MAINT_DEBUG supposed to be defined?

> +# allow wpa_supplicant interface to be specified via wpa-iface
> +# useful for starting wpa_supplicant on one interface of a bridge
> +if [ -n "$IF_WPA_IFACE" ]; then
> +	WPA_IFACE="$IF_WPA_IFACE"
> +else
> +	WPA_IFACE="$IFACE"
> +fi

I'm curious to understand how wpa-iface ends up in IP_WPA_IFACE. I
guess I'm missing a piece of the puzzla.

> +WPA_SUP_PIDFILE="/run/wpa_supplicant.${WPA_IFACE}.pid"
> +
> +# quit if executables are not installed
> +if [ ! -x "$WPA_SUP_BIN" ]; then
> +	exit 0
> +fi

This can be removed in the context of Buildroot. We tend to not check
for the installation of executables from the same package, as it's
quite useless.

> +
> +do_start () {
> +	if [ -n "$IF_WPA_CONF" ] && [ "$IF_WPA_CONF" != "managed" ]; then
> +		if [ ! -s "$IF_WPA_CONF" ]; then
> +			echo "cannot read contents of $IF_WPA_CONF"
> +			exit 1
> +		fi
> +		WPA_SUP_CONF_CTRL_DIR=$(sed -n -e 's/[[:space:]]*#.*//g' -e 's/[[:space:]]\+.*$//g' \
> +			-e 's/^ctrl_interface=\(DIR=\)\?\(.*\)/\2/p' "$IF_WPA_CONF")
> +		if [ -n "$WPA_SUP_CONF_CTRL_DIR" ]; then
> +			WPA_SUP_CONF="-c $IF_WPA_CONF -C $WPA_SUP_CONF_CTRL_DIR"

The manpage of wpa_supplicant says:

       -C ctrl_interface
              Path to ctrl_interface socket (Per interface. Only used if -c is not).

so passing -C when -c is passed does not make sense. Or am I missing
something?

> +		else
> +			WPA_SUP_CONF="-c $IF_WPA_CONF"
> +		fi
> +	else
> +		# specify the default ctrl_interface
> +		WPA_SUP_CONF="-C $WPA_CTRL_DIR"

How is WPA_CTRL_DIR defined?

Thanks!

Thomas
Angelo Compagnucci June 1, 2022, 10:06 p.m. UTC | #2
On Wed, Jun 1, 2022 at 9:25 PM Thomas Petazzoni <
thomas.petazzoni@bootlin.com> wrote:

> Hello,
>
> On Fri, 27 May 2022 12:33:33 +0200
> Angelo Compagnucci <angelo@amarulasolutions.com> wrote:
>
> > Actually, configuring a wifi interface as per "interfaces" man:
> >
> > auto wlan0
> > iface wlan0 inet dhcp
> > wpa-conf /etc/wpa_supplicant.conf
>
> Do you have a link to an interfaces manpage that documents wpa-conf?
>

Not really, I had a look at the source code for busybox and general
debian/ifupdown documentation. The better info I could find is this
stackoverflow page
https://unix.stackexchange.com/questions/262094/where-is-wpa-conf-documented


> Is this supported by the Busybox ifupdown?
>

Yes it is.


>
> > diff --git a/package/wpa_supplicant/ifupdown.sh
> b/package/wpa_supplicant/ifupdown.sh
> > new file mode 100755
> > index 0000000000..8eecf73436
> > --- /dev/null
> > +++ b/package/wpa_supplicant/ifupdown.sh
> > @@ -0,0 +1,71 @@
> > +#!/bin/sh
> > +
> > +# This file is executed by ifupdown in pre-up, post-up, pre-down and
> > +# post-down phases of network interface configuration.
> > +
> > +WPA_SUP_BIN="/usr/sbin/wpa_supplicant"
> > +
> > +if [ -n "$IF_WPA_MAINT_DEBUG" ]; then
> > +     set -x
> > +fi
>
> Where is IF_WPA_MAINT_DEBUG supposed to be defined?
>

In your console before running the script manually if you need to debug it.

> +# allow wpa_supplicant interface to be specified via wpa-iface
> > +# useful for starting wpa_supplicant on one interface of a bridge
> > +if [ -n "$IF_WPA_IFACE" ]; then
> > +     WPA_IFACE="$IF_WPA_IFACE"
> > +else
> > +     WPA_IFACE="$IFACE"
> > +fi
>
> I'm curious to understand how wpa-iface ends up in IP_WPA_IFACE. I
> guess I'm missing a piece of the puzzla.
>

If you add an "echo $@" at the beginning of the script, you could see that
when "wpa-conf" option is defined, those variables are injected into the
environment by busybox while calling the ifup/ifdown scripts.


>
> > +WPA_SUP_PIDFILE="/run/wpa_supplicant.${WPA_IFACE}.pid"
> > +
> > +# quit if executables are not installed
> > +if [ ! -x "$WPA_SUP_BIN" ]; then
> > +     exit 0
> > +fi
>
> This can be removed in the context of Buildroot. We tend to not check
> for the installation of executables from the same package, as it's
> quite useless.
>

Fair enough.


> > +
> > +do_start () {
> > +     if [ -n "$IF_WPA_CONF" ] && [ "$IF_WPA_CONF" != "managed" ]; then
> > +             if [ ! -s "$IF_WPA_CONF" ]; then
> > +                     echo "cannot read contents of $IF_WPA_CONF"
> > +                     exit 1
> > +             fi
> > +             WPA_SUP_CONF_CTRL_DIR=$(sed -n -e 's/[[:space:]]*#.*//g'
> -e 's/[[:space:]]\+.*$//g' \
> > +                     -e 's/^ctrl_interface=\(DIR=\)\?\(.*\)/\2/p'
> "$IF_WPA_CONF")
> > +             if [ -n "$WPA_SUP_CONF_CTRL_DIR" ]; then
> > +                     WPA_SUP_CONF="-c $IF_WPA_CONF -C
> $WPA_SUP_CONF_CTRL_DIR"
>
> The manpage of wpa_supplicant says:
>
>        -C ctrl_interface
>               Path to ctrl_interface socket (Per interface. Only used if
> -c is not).
>
> so passing -C when -c is passed does not make sense. Or am I missing
> something?
>

Not entirely sure, need to check on this.  From my testing, that is working
fine. Let me review that.


> > +             else
> > +                     WPA_SUP_CONF="-c $IF_WPA_CONF"
> > +             fi
> > +     else
> > +             # specify the default ctrl_interface
> > +             WPA_SUP_CONF="-C $WPA_CTRL_DIR"
>
> How is WPA_CTRL_DIR defined?
>

It should be injected too, but I'll do a recheck to be completely sure.


> Thanks!
>
> Thomas
> --
> Thomas Petazzoni, co-owner and CEO, Bootlin
> Embedded Linux and Kernel engineering and training
> https://bootlin.com
>
Nicolas Cavallari June 6, 2022, 3:09 p.m. UTC | #3
Hello,

On 27/05/2022 12:33, Angelo Compagnucci wrote:
> Actually, configuring a wifi interface as per "interfaces" man:
> 
> auto wlan0
> iface wlan0 inet dhcp
> wpa-conf /etc/wpa_supplicant.conf
> 
> doesn't work on buildroot because the line wpa-conf is ignored due to
> the lack of a proper ifupdown script to handle the wpa_supplicant
> initialization.
> 
> The provided file is a simplified version based on the one available
> on debian.
> 
> Signed-off-by: Angelo Compagnucci <angelo@amarulasolutions.com>
> ---
>   package/wpa_supplicant/ifupdown.sh       | 71 ++++++++++++++++++++++++
>   package/wpa_supplicant/wpa_supplicant.mk | 10 ++++
>   2 files changed, 81 insertions(+)
>   create mode 100755 package/wpa_supplicant/ifupdown.sh
> 
> diff --git a/package/wpa_supplicant/ifupdown.sh b/package/wpa_supplicant/ifupdown.sh
> new file mode 100755
> index 0000000000..8eecf73436
> --- /dev/null
> +++ b/package/wpa_supplicant/ifupdown.sh
> @@ -0,0 +1,71 @@
> +#!/bin/sh
> +
> +# This file is executed by ifupdown in pre-up, post-up, pre-down and
> +# post-down phases of network interface configuration.

It is also executed for every interface regardless of if wpa-* options are 
specified or not. ifupdown works by turning all options into IF_* environment 
variables and running all hooks with it.

Right now this hook starts wpa_supplicant on every non-lo interface.
It probably needs a if [ -z "$IF_WPA_CONF" ]; then exit 0; fi somewhere.
The earliest the better.

> +
> +WPA_SUP_BIN="/usr/sbin/wpa_supplicant"
> +
> +if [ -n "$IF_WPA_MAINT_DEBUG" ]; then
> +	set -x
> +fi

It is really necessary to support the "wpa-maint-debug yes" 
/etc/network/interfaces option ?  This script is simple enough, unlike Debian's.

> +
> +# quit if we're called for the loopback
> +if [ "$IFACE" = lo ]; then
> +	exit 0
> +fi

Special-casing lo can be removed by filtering on -z IF_WPA_CONF.  Nobody is 
going to specify a wpa-conf on the lo interface, and if they do, they should 
assume the consequences.

> +
> +# allow wpa_supplicant interface to be specified via wpa-iface
> +# useful for starting wpa_supplicant on one interface of a bridge
> +if [ -n "$IF_WPA_IFACE" ]; then
> +	WPA_IFACE="$IF_WPA_IFACE"
> +else
> +	WPA_IFACE="$IFACE"
> +fi
> +
> +WPA_SUP_PIDFILE="/run/wpa_supplicant.${WPA_IFACE}.pid"
> +
> +# quit if executables are not installed
> +if [ ! -x "$WPA_SUP_BIN" ]; then
> +	exit 0
> +fi
> +
> +do_start () {
> +	if [ -n "$IF_WPA_CONF" ] && [ "$IF_WPA_CONF" != "managed" ]; then
> +		if [ ! -s "$IF_WPA_CONF" ]; then
> +			echo "cannot read contents of $IF_WPA_CONF"
> +			exit 1
> +		fi
> +		WPA_SUP_CONF_CTRL_DIR=$(sed -n -e 's/[[:space:]]*#.*//g' -e 's/[[:space:]]\+.*$//g' \
> +			-e 's/^ctrl_interface=\(DIR=\)\?\(.*\)/\2/p' "$IF_WPA_CONF")
> +		if [ -n "$WPA_SUP_CONF_CTRL_DIR" ]; then
> +			WPA_SUP_CONF="-c $IF_WPA_CONF -C $WPA_SUP_CONF_CTRL_DI > +		else
> +			WPA_SUP_CONF="-c $IF_WPA_CONF"
> +		fi
> +	else
> +		# specify the default ctrl_interface
> +		WPA_SUP_CONF="-C $WPA_CTRL_DIR"
> +	fi
> +}

Debian's ifupdown script need wpa_supplicant to have a ctrl_interface because it 
want to use wpa_cli for all the other wpa-* options, and wpa_cli works by 
connecting to a ctrl_interface.

This script does not use wpa_cli so all the ctrl_interface handling can be 
removed, unless you want to implement the other options later.

By the way, "wpa-conf "managed" is mostly used with the other wpa-* options to 
configure the ssid/passphrase within /etc/network/interfaces, which is something 
your script does not support, so you can remove the first 'if'.
Angelo Compagnucci June 7, 2022, 3 p.m. UTC | #4
On Mon, Jun 6, 2022 at 5:09 PM Nicolas Cavallari <
Nicolas.Cavallari@green-communications.fr> wrote:

> Hello,
>
> On 27/05/2022 12:33, Angelo Compagnucci wrote:
> > Actually, configuring a wifi interface as per "interfaces" man:
> >
> > auto wlan0
> > iface wlan0 inet dhcp
> > wpa-conf /etc/wpa_supplicant.conf
> >
> > doesn't work on buildroot because the line wpa-conf is ignored due to
> > the lack of a proper ifupdown script to handle the wpa_supplicant
> > initialization.
> >
> > The provided file is a simplified version based on the one available
> > on debian.
> >
> > Signed-off-by: Angelo Compagnucci <angelo@amarulasolutions.com>
> > ---
> >   package/wpa_supplicant/ifupdown.sh       | 71 ++++++++++++++++++++++++
> >   package/wpa_supplicant/wpa_supplicant.mk | 10 ++++
> >   2 files changed, 81 insertions(+)
> >   create mode 100755 package/wpa_supplicant/ifupdown.sh
> >
> > diff --git a/package/wpa_supplicant/ifupdown.sh
> b/package/wpa_supplicant/ifupdown.sh
> > new file mode 100755
> > index 0000000000..8eecf73436
> > --- /dev/null
> > +++ b/package/wpa_supplicant/ifupdown.sh
> > @@ -0,0 +1,71 @@
> > +#!/bin/sh
> > +
> > +# This file is executed by ifupdown in pre-up, post-up, pre-down and
> > +# post-down phases of network interface configuration.
>
> It is also executed for every interface regardless of if wpa-* options are
> specified or not. ifupdown works by turning all options into IF_*
> environment
> variables and running all hooks with it.
>
> Right now this hook starts wpa_supplicant on every non-lo interface.
> It probably needs a if [ -z "$IF_WPA_CONF" ]; then exit 0; fi somewhere.
> The earliest the better.
>

Not entirely sure, wpa_supplicant can be used on Ethernet interfaces too.
Better to check if the IF_WPA_CONF has a value or exit silently.


>
> > +
> > +WPA_SUP_BIN="/usr/sbin/wpa_supplicant"
> > +
> > +if [ -n "$IF_WPA_MAINT_DEBUG" ]; then
> > +     set -x
> > +fi
>
> It is really necessary to support the "wpa-maint-debug yes"
> /etc/network/interfaces option ?  This script is simple enough, unlike
> Debian's.
>

Right.


>
> > +
> > +# quit if we're called for the loopback
> > +if [ "$IFACE" = lo ]; then
> > +     exit 0
> > +fi
>
> Special-casing lo can be removed by filtering on -z IF_WPA_CONF.  Nobody
> is
> going to specify a wpa-conf on the lo interface, and if they do, they
> should
> assume the consequences.
>

Right.


>
> > +
> > +# allow wpa_supplicant interface to be specified via wpa-iface
> > +# useful for starting wpa_supplicant on one interface of a bridge
> > +if [ -n "$IF_WPA_IFACE" ]; then
> > +     WPA_IFACE="$IF_WPA_IFACE"
> > +else
> > +     WPA_IFACE="$IFACE"
> > +fi
> > +
> > +WPA_SUP_PIDFILE="/run/wpa_supplicant.${WPA_IFACE}.pid"
> > +
> > +# quit if executables are not installed
> > +if [ ! -x "$WPA_SUP_BIN" ]; then
> > +     exit 0
> > +fi
> > +
> > +do_start () {
> > +     if [ -n "$IF_WPA_CONF" ] && [ "$IF_WPA_CONF" != "managed" ]; then
> > +             if [ ! -s "$IF_WPA_CONF" ]; then
> > +                     echo "cannot read contents of $IF_WPA_CONF"
> > +                     exit 1
> > +             fi
> > +             WPA_SUP_CONF_CTRL_DIR=$(sed -n -e 's/[[:space:]]*#.*//g'
> -e 's/[[:space:]]\+.*$//g' \
> > +                     -e 's/^ctrl_interface=\(DIR=\)\?\(.*\)/\2/p'
> "$IF_WPA_CONF")
> > +             if [ -n "$WPA_SUP_CONF_CTRL_DIR" ]; then
> > +                     WPA_SUP_CONF="-c $IF_WPA_CONF -C
> $WPA_SUP_CONF_CTRL_DI > +              else
> > +                     WPA_SUP_CONF="-c $IF_WPA_CONF"
> > +             fi
> > +     else
> > +             # specify the default ctrl_interface
> > +             WPA_SUP_CONF="-C $WPA_CTRL_DIR"
> > +     fi
> > +}
>
> Debian's ifupdown script need wpa_supplicant to have a ctrl_interface
> because it
> want to use wpa_cli for all the other wpa-* options, and wpa_cli works by
> connecting to a ctrl_interface.
>
> This script does not use wpa_cli so all the ctrl_interface handling can be
> removed, unless you want to implement the other options later.
>
> By the way, "wpa-conf "managed" is mostly used with the other wpa-*
> options to
> configure the ssid/passphrase within /etc/network/interfaces, which is
> something
> your script does not support, so you can remove the first 'if'.
>

Right.
diff mbox series

Patch

diff --git a/package/wpa_supplicant/ifupdown.sh b/package/wpa_supplicant/ifupdown.sh
new file mode 100755
index 0000000000..8eecf73436
--- /dev/null
+++ b/package/wpa_supplicant/ifupdown.sh
@@ -0,0 +1,71 @@ 
+#!/bin/sh
+
+# This file is executed by ifupdown in pre-up, post-up, pre-down and
+# post-down phases of network interface configuration.
+
+WPA_SUP_BIN="/usr/sbin/wpa_supplicant"
+
+if [ -n "$IF_WPA_MAINT_DEBUG" ]; then
+	set -x
+fi
+
+# quit if we're called for the loopback
+if [ "$IFACE" = lo ]; then
+	exit 0
+fi
+
+# allow wpa_supplicant interface to be specified via wpa-iface
+# useful for starting wpa_supplicant on one interface of a bridge
+if [ -n "$IF_WPA_IFACE" ]; then
+	WPA_IFACE="$IF_WPA_IFACE"
+else
+	WPA_IFACE="$IFACE"
+fi
+
+WPA_SUP_PIDFILE="/run/wpa_supplicant.${WPA_IFACE}.pid"
+
+# quit if executables are not installed
+if [ ! -x "$WPA_SUP_BIN" ]; then
+	exit 0
+fi
+
+do_start () {
+	if [ -n "$IF_WPA_CONF" ] && [ "$IF_WPA_CONF" != "managed" ]; then
+		if [ ! -s "$IF_WPA_CONF" ]; then
+			echo "cannot read contents of $IF_WPA_CONF"
+			exit 1
+		fi
+		WPA_SUP_CONF_CTRL_DIR=$(sed -n -e 's/[[:space:]]*#.*//g' -e 's/[[:space:]]\+.*$//g' \
+			-e 's/^ctrl_interface=\(DIR=\)\?\(.*\)/\2/p' "$IF_WPA_CONF")
+		if [ -n "$WPA_SUP_CONF_CTRL_DIR" ]; then
+			WPA_SUP_CONF="-c $IF_WPA_CONF -C $WPA_SUP_CONF_CTRL_DIR"
+		else
+			WPA_SUP_CONF="-c $IF_WPA_CONF"
+		fi
+	else
+		# specify the default ctrl_interface
+		WPA_SUP_CONF="-C $WPA_CTRL_DIR"
+	fi
+}
+
+case "$MODE" in
+	start)
+		do_start
+		case "$PHASE" in
+			post-up)
+				start-stop-daemon -S -q -x ${WPA_SUP_BIN} \
+				-- -B -i ${WPA_IFACE} ${WPA_SUP_CONF} -P ${WPA_SUP_PIDFILE}
+				;;
+		esac
+		;;
+
+	stop)
+		case "$PHASE" in
+			pre-down)
+				start-stop-daemon -K -p ${WPA_SUP_PIDFILE}
+				;;
+		esac
+		;;
+esac
+
+exit 0
diff --git a/package/wpa_supplicant/wpa_supplicant.mk b/package/wpa_supplicant/wpa_supplicant.mk
index c4cfe03371..d38bf572db 100644
--- a/package/wpa_supplicant/wpa_supplicant.mk
+++ b/package/wpa_supplicant/wpa_supplicant.mk
@@ -250,6 +250,14 @@  define WPA_SUPPLICANT_INSTALL_STAGING_CMDS
 	$(WPA_SUPPLICANT_INSTALL_STAGING_WPA_CLIENT_SO)
 endef
 
+ifeq ($(BR2_PACKAGE_IFUPDOWN_SCRIPTS),y)
+define WPA_SUPPLICANT_INSTALL_IFUP_SCRIPTS
+	$(INSTALL) -m 0755 -D package/wpa_supplicant/ifupdown.sh \
+		$(TARGET_DIR)/etc/network/if-up.d/wpasupplicant
+	ln -sf ../if-up.d/wpasupplicant $(TARGET_DIR)/etc/network/if-down.d/wpasupplicant
+endef
+endif
+
 define WPA_SUPPLICANT_INSTALL_TARGET_CMDS
 	$(INSTALL) -m 0755 -D $(@D)/$(WPA_SUPPLICANT_SUBDIR)/wpa_supplicant \
 		$(TARGET_DIR)/usr/sbin/wpa_supplicant
@@ -259,8 +267,10 @@  define WPA_SUPPLICANT_INSTALL_TARGET_CMDS
 	$(WPA_SUPPLICANT_INSTALL_PASSPHRASE)
 	$(WPA_SUPPLICANT_INSTALL_DBUS)
 	$(WPA_SUPPLICANT_INSTALL_WPA_CLIENT_SO)
+	$(WPA_SUPPLICANT_INSTALL_IFUP_SCRIPTS)
 endef
 
+
 define WPA_SUPPLICANT_INSTALL_INIT_SYSTEMD
 	$(INSTALL) -m 0644 -D $(@D)/$(WPA_SUPPLICANT_SUBDIR)/systemd/wpa_supplicant.service \
 		$(TARGET_DIR)/usr/lib/systemd/system/wpa_supplicant.service