From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from lists.gentoo.org (pigeon.gentoo.org [208.92.234.80]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by finch.gentoo.org (Postfix) with ESMTPS id 5285C15800F for ; Sun, 15 Jan 2023 01:51:49 +0000 (UTC) Received: from pigeon.gentoo.org (localhost [127.0.0.1]) by pigeon.gentoo.org (Postfix) with SMTP id 2D72FE07E2; Sun, 15 Jan 2023 01:51:48 +0000 (UTC) Received: from smtp.gentoo.org (woodpecker.gentoo.org [IPv6:2001:470:ea4a:1:5054:ff:fec7:86e4]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by pigeon.gentoo.org (Postfix) with ESMTPS id 10DDFE07E2 for ; Sun, 15 Jan 2023 01:51:47 +0000 (UTC) Received: from oystercatcher.gentoo.org (oystercatcher.gentoo.org [148.251.78.52]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by smtp.gentoo.org (Postfix) with ESMTPS id 07A0B340F01 for ; Sun, 15 Jan 2023 01:51:47 +0000 (UTC) Received: from localhost.localdomain (localhost [IPv6:::1]) by oystercatcher.gentoo.org (Postfix) with ESMTP id 913C77F4 for ; Sun, 15 Jan 2023 01:51:44 +0000 (UTC) From: "Sam James" To: gentoo-commits@lists.gentoo.org Content-Transfer-Encoding: 8bit Content-type: text/plain; charset=UTF-8 Reply-To: gentoo-dev@lists.gentoo.org, "Sam James" Message-ID: <1673747486.b6c67a56b566b0a178fc8eb7cac761e0f6521356.sam@OpenRC> Subject: [gentoo-commits] proj/netifrc:master commit in: net/ X-VCS-Repository: proj/netifrc X-VCS-Files: net/l2tp.sh X-VCS-Directories: net/ X-VCS-Committer: sam X-VCS-Committer-Name: Sam James X-VCS-Revision: b6c67a56b566b0a178fc8eb7cac761e0f6521356 X-VCS-Branch: master Date: Sun, 15 Jan 2023 01:51:44 +0000 (UTC) Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-Id: Gentoo Linux mail X-BeenThere: gentoo-commits@lists.gentoo.org X-Auto-Response-Suppress: DR, RN, NRN, OOF, AutoReply X-Archives-Salt: aa926a73-e25b-42de-98c9-125b172c9344 X-Archives-Hash: 6c57ec548397aa271b18cafe036cb5c7 commit: b6c67a56b566b0a178fc8eb7cac761e0f6521356 Author: Kerin Millar plushkava net> AuthorDate: Sat Jan 14 23:04:46 2023 +0000 Commit: Sam James gentoo org> CommitDate: Sun Jan 15 01:51:26 2023 +0000 URL: https://gitweb.gentoo.org/proj/netifrc.git/commit/?id=b6c67a56 net/l2tp.sh: Re-order function declarations and use a consistent brace style This is how I would initially have submitted it, only I wanted for the initial set of changes to be legible as a unified diff. Signed-off-by: Kerin Millar plushkava.net> Bug: https://bugs.gentoo.org/890238 Signed-off-by: Sam James gentoo.org> net/l2tp.sh | 168 +++++++++++++++++++++++++++++------------------------------- 1 file changed, 82 insertions(+), 86 deletions(-) diff --git a/net/l2tp.sh b/net/l2tp.sh index 51a2331..52b5733 100644 --- a/net/l2tp.sh +++ b/net/l2tp.sh @@ -7,14 +7,92 @@ # Also, SC2034 and SC2316 are muted because they produce false-positives. # shellcheck shell=sh disable=SC3043,SC2034,SC2316 -l2tp_depend() -{ +l2tp_depend() { program ip before bridge interface macchanger } -_l2tp_parse_opts() -{ +l2tp_pre_start() { + local declared_session declared_tunnel l2tpsession l2tptunnel + local name peer_session_id session_id tunnel_id + local encap local peer_tunnel_id remote + local key + + if key="l2tpsession_${IFVAR:?}"; ! eval "[ \${${key}+set} ]"; then + return + elif eval "l2tpsession=\$${key}"; _is_blank "${l2tpsession}"; then + eend 1 "${key} is defined but its value is blank" + elif ! declared_session=$(_l2tp_parse_opts "${l2tpsession}" "peer_session_id session_id tunnel_id" "name"); then + eend 1 "${key} is missing at least one required parameter" + elif eval "${declared_session}"; [ "${name+set}" ]; then + eend 1 "${key} defines a \"name\" parameter, which is forbidden by netifrc" + elif ! modprobe l2tp_eth; then + eend 1 "Couldn't load the l2tp_eth module (perhaps the CONFIG_L2TP_ETH kernel option is disabled)" + elif key="l2tptunnel_${IFVAR}"; eval "[ \${${key}+set} ]"; then + if eval "l2tptunnel=\$${key}"; _is_blank "${l2tptunnel}"; then + eend 1 "${key} is defined but its value is blank" + elif ! declared_tunnel=$(_l2tp_parse_opts "${l2tptunnel}" "local peer_tunnel_id remote tunnel_id" "encap"); then + eend 1 "${key} is missing at least one required parameter" + elif set -- "${tunnel_id}"; eval "${declared_tunnel}"; [ "$1" != "${tunnel_id}" ]; then + eend 1 "${key} defines a \"tunnel_id\" parameter that contradicts l2tpsession_${IFVAR}" + elif _l2tp_should_add_tunnel "${tunnel_id}" "${declared_tunnel}"; set -- $?; [ "$1" -eq 2 ]; then + eend 1 "Tunnel #${tunnel_id} exists but its properties mismatch those defined by ${key}" + elif [ "$1" -eq 1 ]; then + # The config matches an existing tunnel. + true + elif [ "${encap}" = ip ] && ! modprobe l2tp_ip; then + eend 1 "Couldn't load the l2tp_ip module (perhaps the CONFIG_L2TP_IP kernel option is disabled)" + else + ebegin "Creating L2TPv3 tunnel (tunnel_id ${tunnel_id})" + printf %s "l2tp add tunnel ${l2tptunnel}" \ + | xargs -E '' ip + eend $? + fi + elif ! _l2tp_has_tunnel "${tunnel_id}"; then + # A tunnel may incorporate more than one session (link). This + # module allows for the user not to define a tunnel for a given + # session. In that case, it will be expected that the required + # tunnel has already been created to satisfy some other session. + eend 1 "Tunnel #${tunnel_id} not found (defining ${key} may be required)" + fi || return + + ebegin "Creating L2TPv3 session (session_id ${session_id} tunnel_id ${tunnel_id})" + printf %s "l2tp add session ${l2tpsession} name ${IFACE:?}" \ + | xargs -E '' ip && _up + eend $? +} + +l2tp_post_stop() { + local existing_session session_id tunnel_id + + # This function may be invoked for every interface. If not a virtual + # interface, it can't possibly be one that's managed by this module, in + # which case running ip(8) and awk(1) would be a needless expense. + [ -e /sys/devices/virtual/net/"${IFACE:?}" ] \ + && existing_session=$(_l2tp_parse_existing_session 2>/dev/null) \ + || return 0 + + eval "${existing_session}" + set -- session_id "${session_id}" tunnel_id "${tunnel_id}" + ebegin "Destroying L2TPv3 session ($*)" + ip l2tp del session "$@" + eend $? && + if ! _l2tp_in_session "${tunnel_id}"; then + shift 2 + ebegin "Destroying L2TPv3 tunnel ($*)" + ip l2tp del tunnel "$@" + eend $? + fi +} + +_is_blank() ( + LC_CTYPE=C + case $1 in + *[![:blank:]]*) return 1 + esac +) + +_l2tp_parse_opts() { # Parses lt2psession or l2tptunnel options using xargs(1), conveying # them as arguments to awk(1). The awk program interprets the arguments # as a series of key/value pairs and safely prints those specified as @@ -152,85 +230,3 @@ _l2tp_in_session() { } return 1 } - -_is_blank() ( - LC_CTYPE=C - case $1 in - *[![:blank:]]*) return 1 - esac -) - -l2tp_pre_start() -{ - local declared_session declared_tunnel l2tpsession l2tptunnel - local name peer_session_id session_id tunnel_id - local encap local peer_tunnel_id remote - local key - - if key="l2tpsession_${IFVAR:?}"; ! eval "[ \${${key}+set} ]"; then - return - elif eval "l2tpsession=\$${key}"; _is_blank "${l2tpsession}"; then - eend 1 "${key} is defined but its value is blank" - elif ! declared_session=$(_l2tp_parse_opts "${l2tpsession}" "peer_session_id session_id tunnel_id" "name"); then - eend 1 "${key} is missing at least one required parameter" - elif eval "${declared_session}"; [ "${name+set}" ]; then - eend 1 "${key} defines a \"name\" parameter, which is forbidden by netifrc" - elif ! modprobe l2tp_eth; then - eend 1 "Couldn't load the l2tp_eth module (perhaps the CONFIG_L2TP_ETH kernel option is disabled)" - elif key="l2tptunnel_${IFVAR}"; eval "[ \${${key}+set} ]"; then - if eval "l2tptunnel=\$${key}"; _is_blank "${l2tptunnel}"; then - eend 1 "${key} is defined but its value is blank" - elif ! declared_tunnel=$(_l2tp_parse_opts "${l2tptunnel}" "local peer_tunnel_id remote tunnel_id" "encap"); then - eend 1 "${key} is missing at least one required parameter" - elif set -- "${tunnel_id}"; eval "${declared_tunnel}"; [ "$1" != "${tunnel_id}" ]; then - eend 1 "${key} defines a \"tunnel_id\" parameter that contradicts l2tpsession_${IFVAR}" - elif _l2tp_should_add_tunnel "${tunnel_id}" "${declared_tunnel}"; set -- $?; [ "$1" -eq 2 ]; then - eend 1 "Tunnel #${tunnel_id} exists but its properties mismatch those defined by ${key}" - elif [ "$1" -eq 1 ]; then - # The config matches an existing tunnel. - true - elif [ "${encap}" = ip ] && ! modprobe l2tp_ip; then - eend 1 "Couldn't load the l2tp_ip module (perhaps the CONFIG_L2TP_IP kernel option is disabled)" - else - ebegin "Creating L2TPv3 tunnel (tunnel_id ${tunnel_id})" - printf %s "l2tp add tunnel ${l2tptunnel}" \ - | xargs -E '' ip - eend $? - fi - elif ! _l2tp_has_tunnel "${tunnel_id}"; then - # A tunnel may incorporate more than one session (link). This - # module allows for the user not to define a tunnel for a given - # session. In that case, it will be expected that the required - # tunnel has already been created to satisfy some other session. - eend 1 "Tunnel #${tunnel_id} not found (defining ${key} may be required)" - fi || return - - ebegin "Creating L2TPv3 session (session_id ${session_id} tunnel_id ${tunnel_id})" - printf %s "l2tp add session ${l2tpsession} name ${IFACE:?}" \ - | xargs -E '' ip && _up - eend $? -} - -l2tp_post_stop() -{ - local existing_session session_id tunnel_id - - # This function may be invoked for every interface. If not a virtual - # interface, it can't possibly be one that's managed by this module, in - # which case running ip(8) and awk(1) would be a needless expense. - [ -e /sys/devices/virtual/net/"${IFACE:?}" ] \ - && existing_session=$(_l2tp_parse_existing_session 2>/dev/null) \ - || return 0 - - eval "${existing_session}" - set -- session_id "${session_id}" tunnel_id "${tunnel_id}" - ebegin "Destroying L2TPv3 session ($*)" - ip l2tp del session "$@" - eend $? && - if ! _l2tp_in_session "${tunnel_id}"; then - shift 2 - ebegin "Destroying L2TPv3 tunnel ($*)" - ip l2tp del tunnel "$@" - eend $? - fi -}