Commit 1823a49c authored by Jouke Witteveen's avatar Jouke Witteveen
Browse files

Factor out network interface utilities

parent 99bac267
...@@ -16,7 +16,7 @@ install: ...@@ -16,7 +16,7 @@ install:
install -m644 docs/examples/* $(DESTDIR)/etc/netctl/examples/ install -m644 docs/examples/* $(DESTDIR)/etc/netctl/examples/
# Libs # Libs
install -d $(DESTDIR)/usr/lib/netctl/{connections,dhcp} install -d $(DESTDIR)/usr/lib/netctl/{connections,dhcp}
install -m644 src/lib/{globals,ip,rfkill,wpa} $(DESTDIR)/usr/lib/netctl/ install -m644 src/lib/{globals,interface,ip,rfkill,wpa} $(DESTDIR)/usr/lib/netctl/
install -m644 src/lib/connections/* $(DESTDIR)/usr/lib/netctl/connections/ install -m644 src/lib/connections/* $(DESTDIR)/usr/lib/netctl/connections/
install -m644 src/lib/dhcp/* $(DESTDIR)/usr/lib/netctl/dhcp/ install -m644 src/lib/dhcp/* $(DESTDIR)/usr/lib/netctl/dhcp/
install -m755 src/lib/{auto.action,network} $(DESTDIR)/usr/lib/netctl/ install -m755 src/lib/{auto.action,network} $(DESTDIR)/usr/lib/netctl/
......
#! /bin/bash #! /bin/bash
. /usr/lib/netctl/globals . /usr/lib/netctl/globals
. "$SUBR_DIR/interface"
. "$SUBR_DIR/ip" . "$SUBR_DIR/ip"
export INTERFACE="$1" export INTERFACE="$1"
...@@ -10,6 +11,7 @@ Profile="$3" ...@@ -10,6 +11,7 @@ Profile="$3"
export ACTION="$4" export ACTION="$4"
load_profile "$Profile" load_profile "$Profile"
load_interface_config "$INTERFACE"
case $ACTION in case $ACTION in
CONNECT) CONNECT)
......
...@@ -22,6 +22,7 @@ available. The readily sourced files are: ...@@ -22,6 +22,7 @@ available. The readily sourced files are:
/usr/lib/netctl/network /usr/lib/netctl/network
/usr/lib/netctl/globals /usr/lib/netctl/globals
/usr/lib/netctl/interface
/etc/netctl/<profile> /etc/netctl/<profile>
Here, <profile> is the profile file specifying the desired network Here, <profile> is the profile file specifying the desired network
......
...@@ -22,6 +22,7 @@ The readily sourced files are: ...@@ -22,6 +22,7 @@ The readily sourced files are:
/usr/lib/netctl/network /usr/lib/netctl/network
/usr/lib/netctl/globals /usr/lib/netctl/globals
/usr/lib/netctl/interface
/etc/netctl/<profile> /etc/netctl/<profile>
Here, <profile> is the profile file specifying the desired network Here, <profile> is the profile file specifying the desired network
......
...@@ -109,7 +109,7 @@ list_profiles() { ...@@ -109,7 +109,7 @@ list_profiles() {
find -L "$PROFILE_DIR/" -maxdepth 1 -type f -not -name '.*' -not -name '*~' -not -name $'*\n*' -not -name '*.action' -not -name '*.conf' -not -name '*.service' -printf "%f\n" find -L "$PROFILE_DIR/" -maxdepth 1 -type f -not -name '.*' -not -name '*~' -not -name $'*\n*' -not -name '*.action' -not -name '*.conf' -not -name '*.service' -printf "%f\n"
} }
## Sources all hooks, a profile and any interface hook ## Sources all hooks and a profile (but no interface configuration)
# $1: profile name # $1: profile name
load_profile() { load_profile() {
local hook local hook
...@@ -126,9 +126,6 @@ load_profile() { ...@@ -126,9 +126,6 @@ load_profile() {
if [[ ! -r "${Connection:+$SUBR_DIR/connections/$Connection}" ]]; then if [[ ! -r "${Connection:+$SUBR_DIR/connections/$Connection}" ]]; then
exit_error "Profile '$1' does not specify a valid connection" exit_error "Profile '$1' does not specify a valid connection"
fi fi
if [[ -x "$PROFILE_DIR/interfaces/$Interface" ]]; then
source "$PROFILE_DIR/interfaces/$Interface"
fi
source "$SUBR_DIR/connections/$Connection" source "$SUBR_DIR/connections/$Connection"
} }
......
## /usr/lib/netctl/globals needs to be sourced before this file
## Load interface configuration, if present
# $1: interface name
load_interface_config() {
local config_file="$PROFILE_DIR/interfaces/$1"
if [[ -x $config_file ]]; then
source "$config_file"
fi
}
## Check if a string represents a network interface
# $1: potential interface name
is_interface() {
# Strip any old school alias specifier
[[ -d "/sys/class/net/${1%%:?*}" ]]
}
## Add an interface
# $1: interface type
# $2: interface name
# $3: interface link (optional)
# $4...: additional arguments
interface_add() {
local type="$1" name="$2" link="$3"
do_debug ip link add ${link:+link "$link"} name "$name" type "$type" "${@:4}" $LinkOptions || return
load_interface_config "$name"
}
## Delete an interface
# $1: interface name
interface_delete() {
bring_interface_down "$1"
ip link delete "$1"
}
## Check if an interface is up
# $1: interface name
interface_is_up() {
local flags
read flags < "/sys/class/net/${1%%:?*}/flags"
# IFF_UP is defined as 0x1 in linux/if.h
(( flags & 0x1 ))
}
## Activate an interface
# $1: interface name
bring_interface_up() {
local interface=$1
ip link set dev "$interface" up &>/dev/null
timeout_wait "${TimeoutUp:-5}" 'interface_is_up "$interface"'
}
## Deactivate an interface
# $1: interface name
bring_interface_down() {
local interface=$1
ip link set dev "$interface" down &>/dev/null
# We reuse the up timeout (down normally is faster)
timeout_wait "${TimeoutUp:-5}" '! interface_is_up "$interface"'
}
# vim: ft=sh ts=4 et sw=4:
#! /bin/bash #! /bin/bash
. /usr/lib/netctl/globals . /usr/lib/netctl/globals
. "$SUBR_DIR/interface"
## Check if a string represents a network interface
# $1: potential interface name
is_interface() {
# Strip any old school alias specifier
[[ -d "/sys/class/net/${1%%:?*}" ]]
}
## Add an interface
# $1: interface type
# $2: interface name
# $3: interface link (optional)
# $4...: additional arguments
interface_add() {
local type="$1" name="$2" link="$3"
do_debug ip link add ${link:+link "$link"} name "$name" type "$type" "${@:4}" $LinkOptions || return
if [[ -x "$PROFILE_DIR/interfaces/$name" ]]; then
source "$PROFILE_DIR/interfaces/$name"
fi
}
## Delete an interface
# $1: interface name
interface_delete() {
bring_interface_down "$1"
ip link delete "$1"
}
## Check if an interface is up
# $1: interface name
interface_is_up() {
local flags
read flags < "/sys/class/net/${1%%:?*}/flags"
# IFF_UP is defined as 0x1 in linux/if.h
(( flags & 0x1 ))
}
## Activate an interface
# $1: interface name
bring_interface_up() {
local interface=$1
ip link set dev "$interface" up &>/dev/null
timeout_wait "${TimeoutUp:-5}" 'interface_is_up "$interface"'
}
## Deactivate an interface
# $1: interface name
bring_interface_down() {
local interface=$1
ip link set dev "$interface" down &>/dev/null
# We reuse the up timeout (down normally is faster)
timeout_wait "${TimeoutUp:-5}" '! interface_is_up "$interface"'
}
## Indicate that the network stack for the profile is up ## Indicate that the network stack for the profile is up
network_ready() { network_ready() {
if ! is_yes "${WaitOnline:-no}" && ! is_yes "${NETWORK_READY:-no}"; then if ! is_yes "${WaitOnline:-no}" && ! is_yes "${NETWORK_READY:-no}"; then
...@@ -82,6 +30,7 @@ if [[ $# -eq 2 && $1 == @(start|stop) ]]; then ...@@ -82,6 +30,7 @@ if [[ $# -eq 2 && $1 == @(start|stop) ]]; then
# Expose the profile name # Expose the profile name
Profile=$2 Profile=$2
load_profile "$Profile" load_profile "$Profile"
load_interface_config "$Interface"
elif [[ $# -ne 1 || $1 != "wait-online" ]]; then elif [[ $# -ne 1 || $1 != "wait-online" ]]; then
exit_error "Usage: $0 {start|stop|wait-online} [profile]" exit_error "Usage: $0 {start|stop|wait-online} [profile]"
fi fi
......
...@@ -2,8 +2,9 @@ ...@@ -2,8 +2,9 @@
# Contributed by: Sebastian Wicki <gandro@gmx.net> # Contributed by: Sebastian Wicki <gandro@gmx.net>
. /usr/lib/netctl/globals . /usr/lib/netctl/globals
. "$SUBR_DIR/wpa" . "$SUBR_DIR/interface"
. "$SUBR_DIR/rfkill" . "$SUBR_DIR/rfkill"
. "$SUBR_DIR/wpa"
: ${ACTIOND:=wpa_actiond -p /run/wpa_supplicant} : ${ACTIOND:=wpa_actiond -p /run/wpa_supplicant}
: ${ACTION_SCRIPT:=$SUBR_DIR/auto.action} : ${ACTION_SCRIPT:=$SUBR_DIR/auto.action}
...@@ -200,11 +201,8 @@ start() { ...@@ -200,11 +201,8 @@ start() {
local interface="$1" local interface="$1"
local pidfile="$STATE_DIR/wpa_actiond-$interface.pid" local pidfile="$STATE_DIR/wpa_actiond-$interface.pid"
if wpa_is_active "$interface"; then if interface_is_up "$interface"; then
exit_error "The interface ($interface) is already in use" exit_error "The interface '$interface' is already up"
fi
if [[ -x "$PROFILE_DIR/interfaces/$interface" ]]; then
source "$PROFILE_DIR/interfaces/$interface"
fi fi
if [[ $RFKill ]]; then if [[ $RFKill ]]; then
rf_enable "$interface" "$RFKill" || return 1 rf_enable "$interface" "$RFKill" || return 1
...@@ -240,6 +238,7 @@ start() { ...@@ -240,6 +238,7 @@ start() {
return 0 return 0
fi fi
wpa_stop "$interface" wpa_stop "$interface"
bring_interface_down "$interface"
fi fi
return 1 return 1
} }
...@@ -251,11 +250,8 @@ stop() { ...@@ -251,11 +250,8 @@ stop() {
local pidfile="$STATE_DIR/wpa_actiond-$interface.pid" local pidfile="$STATE_DIR/wpa_actiond-$interface.pid"
[[ -e "$pidfile" ]] && kill "$(< "$pidfile")" [[ -e "$pidfile" ]] && kill "$(< "$pidfile")"
if [[ -x "$PROFILE_DIR/interfaces/$interface" ]]; then
source "$PROFILE_DIR/interfaces/$interface"
fi
timeout_wait 1 '! wpa_is_active "$interface"' || wpa_stop "$interface" timeout_wait 1 '! wpa_is_active "$interface"' || wpa_stop "$interface"
ip link set dev "$interface" down bring_interface_down "$interface"
[[ $RFKill ]] && rf_disable "$interface" "$RFKill" [[ $RFKill ]] && rf_disable "$interface" "$RFKill"
return 0 return 0
} }
...@@ -286,6 +282,7 @@ case $# in ...@@ -286,6 +282,7 @@ case $# in
exit_error "Use 'systemctl $1 netctl-auto@$2' to $1 netctl-auto." exit_error "Use 'systemctl $1 netctl-auto@$2' to $1 netctl-auto."
fi fi
ensure_root "$(basename "$0")" ensure_root "$(basename "$0")"
load_interface_config "$2"
"$1" "$2";; "$1" "$2";;
*) *)
exit_error "$(usage)";; exit_error "$(usage)";;
......
#! /bin/bash #! /bin/bash
. /usr/lib/netctl/globals . /usr/lib/netctl/globals
. "$SUBR_DIR/wpa" . "$SUBR_DIR/interface"
. "$SUBR_DIR/rfkill" . "$SUBR_DIR/rfkill"
. "$SUBR_DIR/wpa"
usage() { usage() {
...@@ -231,15 +232,12 @@ if [[ -z "$INTERFACE" ]]; then ...@@ -231,15 +232,12 @@ if [[ -z "$INTERFACE" ]]; then
fi fi
INTERFACE=${INTERFACE:15:-10} INTERFACE=${INTERFACE:15:-10}
report_debug "Using interface '$INTERFACE'" report_debug "Using interface '$INTERFACE'"
elif ! is_interface "$INTERFACE"; then
exit_error "No such interface: $INTERFACE"
fi fi
if [[ -x "$PROFILE_DIR/interfaces/$INTERFACE" ]]; then load_interface_config "$INTERFACE"
source "$PROFILE_DIR/interfaces/$INTERFACE"
fi
cd / # We do not want to spawn anything that can block unmounting cd / # We do not want to spawn anything that can block unmounting
if [[ ! -d "/sys/class/net/$INTERFACE" ]]; then
exit_error "No such interface: $INTERFACE"
fi
if [[ "$RFKill" && "$(rf_status "$INTERFACE" "$RFKill")" ]]; then if [[ "$RFKill" && "$(rf_status "$INTERFACE" "$RFKill")" ]]; then
if ! rf_enable "$INTERFACE" "$RFKill"; then if ! rf_enable "$INTERFACE" "$RFKill"; then
exit_error "Could not unblock transmission on interface '$INTERFACE'" exit_error "Could not unblock transmission on interface '$INTERFACE'"
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment