Skip to content

Commit e5db08e

Browse files
committed
base-files: network.sh: fix a number of IPv6 logic flaws
* Change network_get_subnet6() to sensibly guess a suitable prefix Attempt to return the first non-linklocal, non-ula range, then attempt to return the first non-linklocal range and finally fall back to the previous behaviour of simply returning the first found item. * Fix network_get_ipaddrs_all() Instead of replicating the flawed logic appending a fixed ":1" suffix to IPv6 addresses, rely on network_get_ipaddrs() and network_get_ipaddrs6() to build a single list of all interface addresses. * Fix network_get_subnets6() Instead of replicating the flawed logic appending a fixed ":1" suffix to IPv6 addresses, rely on the ipv6-prefix-assignment.local-address field to figure out the proper network address. Signed-off-by: Jo-Philipp Wich <[email protected]>
1 parent 8a42d4d commit e5db08e

File tree

1 file changed

+48
-17
lines changed

1 file changed

+48
-17
lines changed

package/base-files/files/lib/functions/network.sh

Lines changed: 48 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,36 @@ network_get_subnet() {
4545
# 1: destination variable
4646
# 2: interface
4747
network_get_subnet6() {
48-
__network_ifstatus "$1" "$2" "['ipv6-address'][0]['address','mask']" "/"
48+
local __nets __addr
49+
50+
if network_get_subnets6 __nets "$2"; then
51+
# Attempt to return first non-fe80::/10, non-fc::/7 range
52+
for __addr in $__nets; do
53+
case "$__addr" in fe[8ab]?:*|f[cd]??:*)
54+
continue
55+
esac
56+
export "$1=$__addr"
57+
return 0
58+
done
59+
60+
# Attempt to return first non-fe80::/10 range
61+
for __addr in $__nets; do
62+
case "$__addr" in fe[8ab]?:*)
63+
continue
64+
esac
65+
export "$1=$__addr"
66+
return 0
67+
done
68+
69+
# Return first item
70+
for __addr in $__nets; do
71+
export "$1=$__addr"
72+
return 0
73+
done
74+
fi
75+
76+
unset "$1"
77+
return 1
4978
}
5079

5180
# determine first IPv6 prefix of given logical interface
@@ -94,18 +123,13 @@ network_get_ipaddrs6() {
94123
# 1: destination variable
95124
# 2: interface
96125
network_get_ipaddrs_all() {
97-
local __addr
98-
local __list=""
126+
local __addr __addr6
99127

100-
if __network_ifstatus "__addr" "$2" "['ipv4-address','ipv6-address','ipv6-prefix-assignment'][*].address"; then
101-
for __addr in $__addr; do
102-
case "$__addr" in
103-
*:) __list="${__list:+$__list }${__addr}1" ;;
104-
*) __list="${__list:+$__list }${__addr}" ;;
105-
esac
106-
done
128+
network_get_ipaddrs __addr "$2"
129+
network_get_ipaddrs6 __addr6 "$2"
107130

108-
export "$1=$__list"
131+
if [ -n "$__addr" -o -n "$__addr6" ]; then
132+
export "$1=${__addr:+$__addr }$__addr6"
109133
return 0
110134
fi
111135

@@ -124,17 +148,24 @@ network_get_subnets() {
124148
# 1: destination variable
125149
# 2: interface
126150
network_get_subnets6() {
127-
local __addr
151+
local __addr __mask
128152
local __list=""
129153

130-
if __network_ifstatus "__addr" "$2" "['ipv6-address','ipv6-prefix-assignment'][*]['address','mask']" "/ "; then
154+
if __network_ifstatus "__addr" "$2" "['ipv6-address'][*]['address','mask']" "/ "; then
131155
for __addr in $__addr; do
132-
case "$__addr" in
133-
*:/*) __list="${__list:+$__list }${__addr%/*}1/${__addr##*/}" ;;
134-
*) __list="${__list:+$__list }${__addr}" ;;
135-
esac
156+
__list="${__list:+$__list }${__addr}"
136157
done
158+
fi
137159

160+
if __network_ifstatus "__addr" "$2" "['ipv6-prefix-assignment'][*]['local-address'].address" && \
161+
__network_ifstatus "__mask" "$2" "['ipv6-prefix-assignment'][*].mask"; then
162+
for __addr in $__addr; do
163+
__list="${__list:+$__list }${__addr}/${__mask%% *}"
164+
__mask="${__mask#* }"
165+
done
166+
fi
167+
168+
if [ -n "$__list" ]; then
138169
export "$1=$__list"
139170
return 0
140171
fi

0 commit comments

Comments
 (0)