Skip to content

Commit

Permalink
Ordering improvements in IP configuration
Browse files Browse the repository at this point in the history
- Add static routes prior to setting the default IP4 gateway
  (github/pull/43)
- Set sysctl property net.ipv6.conf.<interface>.accept_ra earlier
  (FS#35788)
  • Loading branch information
joukewitteveen committed Jun 16, 2013
1 parent 277b1cf commit 4d8cb70
Showing 1 changed file with 30 additions and 33 deletions.
63 changes: 30 additions & 33 deletions src/lib/ip
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,24 @@ ip_set() {
return 1
fi

# Load ipv6 module if necessary
case "$IP6" in
dhcp*|stateless|static)
[[ -d "/proc/sys/net/ipv6" ]] || modprobe ipv6
[[ $IP6 == "static" ]]
sysctl -q -w "net.ipv6.conf.$interface_sysctl.accept_ra=$?"
;;
no)
[[ -d "/proc/sys/net/ipv6" ]] && sysctl -q -w "net.ipv6.conf.$interface_sysctl.accept_ra=0"
;;
"") # undefined IP6 does not prevent RA's from being received -> nop
;;
*)
report_error "IP6 must be 'dhcp', 'dhcp-noaddr', 'stateless', 'static' or 'no'"
return 1
;;
esac

case $IP in
dhcp)
case ${DHCPClient:-dhcpcd} in
Expand Down Expand Up @@ -57,12 +75,6 @@ ip_set() {
return 1
fi
done
if [[ $Gateway ]]; then
if ! do_debug ip route add default via "$Gateway" dev "$Interface"; then
report_error "Could not set gateway '$Gateway' on interface '$Interface'"
return 1
fi
fi
;;
""|no)
;;
Expand All @@ -80,44 +92,29 @@ ip_set() {
return 1
fi
done
fi

# Load ipv6 module if necessary
case "$IP6" in
dhcp*|stateless|static)
[[ -d "/proc/sys/net/ipv6" ]] || modprobe ipv6
;;
no)
[[ -d "/proc/sys/net/ipv6" ]] && sysctl -q -w "net.ipv6.conf.$interface_sysctl.accept_ra=0"
;;
"") # undefined IP6 does not prevent RA's from being received -> nop
;;
*)
report_error "IP6 must be 'dhcp', 'dhcp-noaddr', 'stateless', 'static' or 'no'"
return 1
;;
esac
# Set a custom gateway after static routes are added
if [[ $IP == "static" && $Gateway ]]; then
if ! do_debug ip route add default via "$Gateway" dev "$Interface"; then
report_error "Could not set gateway '$Gateway' on interface '$Interface'"
return 1
fi
fi
fi

case "$IP6" in
dhcp*)
if ! type dhclient &>/dev/null; then
report_error "You need to install dhclient to use DHCPv6"
return 1
fi
sysctl -q -w "net.ipv6.conf.$interface_sysctl.accept_ra=1"
[[ $IP6 == "dhcp-noaddr" ]] && DhclientOptions6+=" -S"
rm -f "/run/dhclient6-${Interface}.pid"
if ! do_debug dhclient -6 -q -e "TIMEOUT=${TimeoutDHCP:-10}" -pf "/run/dhclient6-${Interface}.pid" $DhclientOptions6 "$Interface"; then
report_error "DHCPv6 IP lease attempt failed on interface '$Interface'"
return 1
fi
;;
stateless)
sysctl -q -w "net.ipv6.conf.$interface_sysctl.accept_ra=1"
;;&
static)
sysctl -q -w "net.ipv6.conf.$interface_sysctl.accept_ra=0"
;;&
stateless|static)
for addr in "${Address6[@]}"; do
if ! do_debug ip -6 addr add $addr dev "$Interface"; then
Expand All @@ -134,15 +131,15 @@ ip_set() {
return 1
fi

# Add static IPv6 routes
# Add static IPv6 routes after DAD has finished
for route in "${Routes6[@]}"; do
if ! do_debug ip -6 route add $route dev "$Interface"; then
report_error "Could not add route '$route' to interface '$Interface'"
return 1
fi
done

# Set a custom gateway after DAD has finished
# Set a custom gateway after static routes are added
if [[ $IP6 == @(stateless|static) && $Gateway6 ]]; then
if ! do_debug ip -6 route replace default via "$Gateway6" dev "$Interface"; then
report_error "Could not set gateway '$Gateway6' on interface '$Interface'"
Expand Down Expand Up @@ -180,7 +177,7 @@ ip_set() {
# $IP: type of IPv4 configuration
# $IP6: type of IPv6 configuration
ip_unset() {
if [[ "$IP" == "dhcp" ]]; then
if [[ $IP == "dhcp" ]]; then
case ${DHCPClient:-dhcpcd} in
dhcpcd)
if [[ -f "/run/dhcpcd-$Interface.pid" ]]; then
Expand All @@ -195,7 +192,7 @@ ip_unset() {
;;
esac
fi
if [[ "$IP6" == dhcp* ]]; then
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
Expand Down

0 comments on commit 4d8cb70

Please sign in to comment.