diff options
author | Jouke Witteveen <j.witteveen@gmail.com> | 2014-02-25 14:22:22 +0100 |
---|---|---|
committer | Jouke Witteveen <j.witteveen@gmail.com> | 2014-02-27 13:48:49 +0100 |
commit | 7f497011aee03f141a5bd055222ca337383f43a3 (patch) | |
tree | 3e790e15f1e007de95b95e18a71ec020dbc615eb /src/lib | |
parent | 03afcdce237b2dc52a6c1fea72b715cf6c34bf05 (diff) | |
download | netctl-7f497011aee03f141a5bd055222ca337383f43a3.tar.gz netctl-7f497011aee03f141a5bd055222ca337383f43a3.tar.xz |
Factor out DHCP client support
Support for additional DHCP clients is now easy to add.
Diffstat (limited to 'src/lib')
-rw-r--r-- | src/lib/connections/README | 2 | ||||
-rw-r--r-- | src/lib/dhcp/README | 34 | ||||
-rw-r--r-- | src/lib/dhcp/dhclient | 27 | ||||
-rw-r--r-- | src/lib/dhcp/dhcpcd | 28 | ||||
-rw-r--r-- | src/lib/ip | 83 |
5 files changed, 115 insertions, 59 deletions
diff --git a/src/lib/connections/README b/src/lib/connections/README index 6d8db9b..2dd4b7b 100644 --- a/src/lib/connections/README +++ b/src/lib/connections/README @@ -10,7 +10,7 @@ for the aviancarrier connection type will be provided by the file: Files that implement support for a connection type should NOT be executable. Such files should contain valid Bash code, among which two functions, namely <connection_type>_up and <connection_type>_down. For -the aviancarrier file this would be: +the aviancarrier file these would be: aviancarrier_up aviancarrier_down diff --git a/src/lib/dhcp/README b/src/lib/dhcp/README new file mode 100644 index 0000000..f55a06e --- /dev/null +++ b/src/lib/dhcp/README @@ -0,0 +1,34 @@ +Support for dhcp clients is implemented by files in + + /usr/lib/network/dhcp/ + +The file name determines the name of the client for the profile, so +support for a client named dhcpcd is provided by the file: + + /usr/lib/network/connections/dhcpcd + +Files that implement support for a connection type should NOT be +executable. Such files should contain valid Bash code, among which two +functions, namely <client_name>_start and <client_name>_stop. For +the client named dhcpcd these would be: + + dhcpcd_start + dhcpcd_stop + +These functions are responsible for starting and stopping the dhcp +client. When the functions are called, three bash files are already +sourced, so all functions and variables in those files are available. +The readily sourced files are: + + /usr/lib/network/network + /usr/lib/network/globals + /etc/netctl/<profile> + +Here, <profile> is the profile file specifying the desired network +configuration. + +When called, the start and stop functions get as their first argument +the version of the IP protocol that the dhcp client is expected to use. +In the case of starting the client for IPv6, an additional second +argument 'noaddr' may be supplied, which indicates that the dhcp client +should configure everything but an IP address. diff --git a/src/lib/dhcp/dhclient b/src/lib/dhcp/dhclient new file mode 100644 index 0000000..42b5f14 --- /dev/null +++ b/src/lib/dhcp/dhclient @@ -0,0 +1,27 @@ +type dhclient &> /dev/null || return + +dhclient_start() { + local options pidfile="/run/dhclient$1-$Interface.pid" + case $1 in + 4) options=$DhclientOptions;; + 6) options=$DhclientOptions6;; + *) return 1;; + esac + [[ $2 == "noaddr" ]] && options+=" -S" + rm -f "$pidfile" + if ! do_debug dhclient -$1 -q -e "TIMEOUT=${TimeoutDHCP:-30}" -pf "$pidfile" $options "$Interface"; then + report_error "DHCP IPv$1 lease attempt failed on interface '$Interface'" + return 1 + fi +} + +dhclient_stop() { + local stop="-x" pidfile="/run/dhclient$1-$Interface.pid" + if [[ -f $pidfile ]]; then + is_yes "${DHCPReleaseOnStop:-no}" && stop="-r" + do_debug dhclient -$1 -q $stop "$Interface" -pf "$pidfile" > /dev/null + fi +} + + +# vim: ft=sh ts=4 et sw=4: diff --git a/src/lib/dhcp/dhcpcd b/src/lib/dhcp/dhcpcd new file mode 100644 index 0000000..42f33ef --- /dev/null +++ b/src/lib/dhcp/dhcpcd @@ -0,0 +1,28 @@ +type dhcpcd &> /dev/null || return + +dhcpcd_start() { + if [[ $1 != "4" ]]; then + report_error "Using 'dhcpcd' for IPv6 is currently not possible in netctl" + return 1 + fi + rm -f "/run/dhcpcd-$Interface".{pid,cache} + # If using own dns, tell dhcpcd to NOT replace resolv.conf + [[ $DNS ]] && DhcpcdOptions+=" -C resolv.conf" + do_debug dhcpcd -4qL -t "${TimeoutDHCP:-30}" $DhcpcdOptions "$Interface" |& report_debug "$(cat)" + # The first array value of PIPESTATUS is the exit status of dhcpcd + if (( PIPESTATUS != 0 )); then + report_error "DHCP IP lease attempt failed on interface '$Interface'" + return 1 + fi +} + +dhcpcd_stop() { + local stop="-x" + if [[ -f "/run/dhcpcd-$Interface.pid" ]]; then + is_yes "${DHCPReleaseOnStop:-no}" && stop="-k" + do_debug dhcpcd -q $stop "$Interface" > /dev/null + fi +} + + +# vim: ft=sh ts=4 et sw=4: @@ -1,6 +1,26 @@ ## /usr/lib/network/globals needs to be sourced before this file +## Interface a DHCP client +# $1: DHCP client +# $2: command +# $3...: additional arguments +dhcp_call() { + local client="$1" command="$2" + shift 2 + + if [[ ! -r "$SUBR_DIR/dhcp/$client" ]]; then + report_error "DHCP client '$client' is unsupported" + return 127 + fi + if ! source "$SUBR_DIR/dhcp/$client"; then + report_error "DHCP client '$client' is not installed or not ready" + return 127 + fi + "${client}_$command" "$@" +} + + ## Add resolv.conf entries for an interface # $1: interface name # $2...: entries, one line per variable @@ -43,30 +63,7 @@ ip_set() { case $IP in dhcp) - case ${DHCPClient:-dhcpcd} in - dhcpcd) - rm -f "/run/dhcpcd-$Interface".{pid,cache} - # If using own dns, tell dhcpcd to NOT replace resolv.conf - [[ $DNS ]] && DhcpcdOptions+=" -C resolv.conf" - do_debug dhcpcd -4qL -t "${TimeoutDHCP:-30}" $DhcpcdOptions "$Interface" |& report_debug "$(cat)" - # The first array value of PIPESTATUS is the exit status of dhcpcd - if (( PIPESTATUS != 0 )); then - report_error "DHCP IP lease attempt failed on interface '$Interface'" - return 1 - fi - ;; - dhclient) - rm -f "/run/dhclient-${Interface}.pid" - if ! do_debug dhclient -4 -q -e "TIMEOUT=${TimeoutDHCP:-30}" -pf "/run/dhclient-$Interface.pid" $DhclientOptions "$Interface"; then - report_error "DHCP IP lease attempt failed on interface '$Interface'" - return 1 - fi - ;; - *) - report_error "Unsupported DHCP client: '$DHCPClient'" - return 1 - ;; - esac + dhcp_call "${DHCPClient:-dhcpcd}" start 4 || return ;; static) for addr in "${Address[@]}"; do @@ -103,17 +100,8 @@ ip_set() { fi case "$IP6" in - dhcp*) - if ! type dhclient &>/dev/null; then - report_error "You need to install dhclient to use DHCPv6" - return 1 - fi - [[ $IP6 == "dhcp-noaddr" ]] && DhclientOptions6+=" -S" - rm -f "/run/dhclient6-${Interface}.pid" - if ! do_debug dhclient -6 -q -e "TIMEOUT=${TimeoutDHCP:-30}" -pf "/run/dhclient6-${Interface}.pid" $DhclientOptions6 "$Interface"; then - report_error "DHCPv6 IP lease attempt failed on interface '$Interface'" - return 1 - fi + dhcp|dhcp-noaddr) + dhcp_call "${DHCP6Client:-dhclient}" start 6 ${IP6:5} || return ;; stateless|static) for addr in "${Address6[@]}"; do @@ -179,29 +167,8 @@ ip_set() { # $IP: type of IPv4 configuration # $IP6: type of IPv6 configuration ip_unset() { - local stop="-x" - if [[ $IP == "dhcp" ]]; then - case ${DHCPClient:-dhcpcd} in - dhcpcd) - if [[ -f "/run/dhcpcd-$Interface.pid" ]]; then - is_yes "${DHCPReleaseOnStop:-no}" && stop="-k" - do_debug dhcpcd -q $stop "$Interface" >/dev/null - fi - ;; - dhclient) - if [[ -f "/run/dhclient-$Interface.pid" ]]; then - is_yes "${DHCPReleaseOnStop:-no}" && stop="-r" - do_debug dhclient -q $stop "$Interface" -pf "/run/dhclient-$Interface.pid" >/dev/null - fi - ;; - esac - fi - if [[ $IP6 == dhcp* ]]; then - if [[ -f "/run/dhclient6-$Interface.pid" ]]; then - do_debug dhclient -6 -q -x "$Interface" -pf "/run/dhclient6-$Interface.pid" >/dev/null - fi - fi - + [[ $IP == "dhcp" ]] && dhcp_call "${DHCPClient:-dhcpcd}" stop 4 + [[ $IP6 == dhcp* ]] && dhcp_call "${DHCP6Client:-dhclient}" stop 6 [[ $DNS ]] && resolvconf -d "$Interface" ip route flush dev "$Interface" &>/dev/null ip -6 route flush dev "$Interface" &>/dev/null |