#! /bin/bash . /usr/lib/network/network report_iproute() { report_fail "$*" bring_interface down "$INTERFACE" exit 1 } ethernet_up() { load_profile "$1" if [[ ! -e "/sys/class/net/$INTERFACE" ]]; then if ! echo "$INTERFACE" | fgrep -q ":"; then report_iproute "Interface $INTERFACE does not exist" fi fi report_debug ethernet_iproute_up ifup bring_interface up "$INTERFACE" if ! checkyesno "${SKIPNOCARRIER:-no}" && ip link show dev "$INTERFACE" | fgrep -q "NO-CARRIER"; then sleep ${CARRIER_TIMEOUT:-2} # Some cards are plain slow to come up. Don't fail immediately. if ip link show dev "$INTERFACE" | fgrep -q "NO-CARRIER"; then report_iproute "No connection" fi fi if checkyesno "${AUTH8021X:-no}"; then . "$SUBR_DIR/8021x" [[ -z "$WPA_CONF" ]] && WPA_CONF="/etc/wpa_supplicant.conf" [[ -z "$WPA_DRIVER" ]] && WPA_DRIVER="wired" report_debug ethernet_iproute_up start_wpa "$INTERFACE" "$WPA_CONF" "$WPA_DRIVER" "$WPA_OPTS" if ! start_wpa "$INTERFACE" "$WPA_CONF" "$WPA_DRIVER" "$WPA_OPTS"; then report_fail "wpa_supplicant did not start, possible configuration error" return 1 fi if ! wpa_check "$INTERFACE" 15 "ASSOCIATED"; then bring_interface down "$INTERFACE" report_fail "WPA Authentication/Association Failed" return 1 fi fi if [[ -z "$IP" && -z "$IP6" ]]; then report_iproute "At least one of IP or IP6 should be specified" return 1 fi case "$IP" in dhcp) if checkyesno "${DHCLIENT:-no}"; then rm -r "/var/run/dhclient-${INTERFACE}.pid" >/dev/null 2>&1 report_debug ethernet_up dhclient -q -e TIMEOUT="${DHCP_TIMEOUT:-10}" -pf "/var/run/dhclient-$INTERFACE.pid" "$INTERFACE" if ! dhclient -q -e TIMEOUT="${DHCP_TIMEOUT:-10}" -pf "/var/run/dhclient-${INTERFACE}.pid" ${DHCLIENT_OPTIONS} "$INTERFACE"; then report_fail "DHCP IP lease attempt failed." stop_80211x return 1 fi else # Clear remaining pid files. rm -f "/var/run/dhcpcd-$INTERFACE".{pid,cache} >/dev/null 2>&1 # If using own dns, tell dhcpcd to NOT replace resolv.conf [[ -n "$DNS1" || -n "$DNS" ]] && DHCP_OPTIONS="-C resolv.conf $DHCP_OPTIONS" # Start dhcpcd report_debug ethernet_up dhcpcd -qL -t "${DHCP_TIMEOUT:-10}" $DHCP_OPTIONS "$INTERFACE" dhcpcd -qL -t "${DHCP_TIMEOUT:-10}" $DHCP_OPTIONS "$INTERFACE" 2>&1 | report_debug "$(cat)" if [[ "$PIPESTATUS" -ne 0 ]]; then report_fail "DHCP IP lease attempt failed." stop_80211x return 1 fi fi ;; static) if [[ -n "$ADDR" ]]; then [[ -z $NETMASK ]] && NETMASK=24 report_debug ethernet_iproute_up ip addr add "$ADDR/$NETMASK" brd + dev "$INTERFACE" if ! ip addr add "$ADDR/$NETMASK" brd + dev "$INTERFACE"; then report_iproute "Could not configure interface" fi fi if [[ -n "$IFOPTS" ]]; then if ! ifconfig "$INTERFACE" $IFOPTS up; then report_iproute "Bringing interface up failed." return 1 fi fi if [[ -n "$ROUTES" ]]; then for route in "${ROUTES[@]}"; do report_debug ethernet_iproute_up ip route add $route dev "$INTERFACE" if ! ip route add $route dev "$INTERFACE" ; then report_iproute "Adding route '$route' failed" fi done fi if [[ -n "$GATEWAY" ]]; then report_debug ethernet_iproute_up ip route add default via "$GATEWAY" if ! ip route add default via "$GATEWAY"; then report_iproute "Adding gateway $GATEWAY failed" fi fi ;; "") ;; *) report_iproute "IP must be either 'dhcp' or 'static'" ;; esac if [[ -n "$IPCFG" ]]; then for line in "${IPCFG[@]}"; do report_debug ethernet_iproute_up ip "$line" if ! ip $line; then report_iproute "Could not configure interface ($line)." fi done fi case "$IP6" in static) if [[ -n "$ADDR6" ]]; then [[ -z $PREFIXLEN ]] && PREFIXLEN=64 report_debug ethernet_iproute_up ip -6 addr add "$ADDR6/$PREFIXLEN" dev "$INTERFACE" if ! ip -6 addr add "$ADDR6/$PREFIXLEN" dev "$INTERFACE"; then report_iproute "Could not configure interface" fi fi if [[ -n "$GATEWAY6" ]]; then report_debug ethernet_iproute_up ip -6 route add default via "$GATEWAY6" if ! ip -6 route add default via "$GATEWAY6"; then report_iproute "Adding gateway $GATEWAY6 failed" fi fi ;; "") ;; *) report_iproute "IP6 must be 'static'" ;; esac # Set hostname if [[ -n "$HOSTNAME" ]]; then report_debug ethernet_iproute_up hostname "$HOSTNAME" if ! echo "$HOSTNAME" > /proc/sys/kernel/hostname; then report_iproute "Cannot set hostname to $HOSTNAME" fi fi # Generate a new resolv.conf if [[ -n "$DNS" ]]; then : >/etc/resolv.conf [[ -n "$DOMAIN" ]] && echo "domain $DOMAIN" >>/etc/resolv.conf [[ -n "$SEARCH" ]] && echo "search $SEARCH" >>/etc/resolv.conf for dns in "${DNS[@]}"; do echo "nameserver $dns" >>/etc/resolv.conf done elif [[ -n "$DNS1" ]]; then # support older 'ethernet' syntax. : >/etc/resolv.conf [[ -n "$DOMAIN" ]] && echo "domain $DOMAIN" >>/etc/resolv.conf [[ -n "$SEARCH" ]] && echo "search $SEARCH" >>/etc/resolv.conf [[ -n "$DNS1" ]] && echo "nameserver $DNS1" >>/etc/resolv.conf [[ -n "$DNS2" ]] && echo "nameserver $DNS2" >>/etc/resolv.conf fi return 0 } ethernet_down() { load_profile "$1" if [[ "$IP" == "dhcp" ]]; then if [[ -f "/var/run/dhcpcd-${INTERFACE}.pid" ]]; then report_debug ethernet_down dhcpcd -qx "$INTERFACE" dhcpcd -qk "$INTERFACE" &>/dev/null fi fi stop_80211x report_debug ethernet_down if_down if [[ "$CONNECTION" == "wireless" ]]; then bring_interface flush "$INTERFACE" else bring_interface down "$INTERFACE" fi return 0 } # Returns status of profile - is it still functional? ethernet_status() { if ! ip link show dev "$INTERFACE" | fgrep -q "state UP"; then return 1 fi } # Stop wpa_supplicant if neccessary stop_80211x() { if checkyesno "${AUTH8021X:-no}"; then . "$SUBR_DIR/8021x" report_debug ethernet_down stop_wpa "$INTERFACE" stop_wpa "$INTERFACE" fi } ethernet_$1 "$2" exit $? # vim: set ts=4 et sw=4: