[BACK]Return to netstart CVS log [TXT][DIR] Up to [local] / src / etc

Annotation of src/etc/netstart, Revision 1.212

1.1       deraadt     1: #!/bin/sh -
                      2: #
1.212   ! florian     3: #      $OpenBSD: netstart,v 1.211 2020/12/23 17:22:07 krw Exp $
1.153     rpe         4:
                      5: # Turn off Strict Bourne shell mode.
                      6: set +o sh
1.101     millert     7:
1.198     rpe         8: # Show usage of the netstart script and exit.
                      9: usage() {
                     10:        print -u2 "usage: ${0##*/} [[-n] interface ...]"
                     11:        exit 1
                     12: }
                     13:
1.204     kn         14: # Echo file $1 to stdout. Skip comment lines. Strip leading and trailing
1.173     rpe        15: # whitespace if IFS is set.
1.160     rpe        16: # Usage: stripcom /path/to/file
1.101     millert    17: stripcom() {
1.160     rpe        18:        local _file=$1 _line
                     19:
                     20:        [[ -f $_file ]] || return
                     21:
                     22:        while read _line; do
                     23:                [[ -n ${_line%%#*} ]] && print -r -- "$_line"
                     24:        done <$_file
1.101     millert    25: }
1.45      millert    26:
1.177     rpe        27: # Parse and "unpack" a hostname.if(5) line given as positional parameters.
                     28: # Fill the _cmds array with the resulting interface configuration commands.
                     29: parse_hn_line() {
1.211     krw        30:        local _af=0 _name=1 _mask=2 _bc=3 _prefix=2 _c _cmd _prev _daddr _dhcp _i
1.177     rpe        31:        set -A _c -- "$@"
                     32:        set -o noglob
                     33:
                     34:        case ${_c[_af]} in
                     35:        ''|*([[:blank:]])'#'*)
                     36:                return
                     37:                ;;
                     38:        inet)   ((${#_c[*]} > 1)) || return
1.212   ! florian    39:                if [[ ${_c[_name]} == autoconf ]]; then
        !            40:                        _cmds[${#_cmds[*]}]="ifconfig $_if ${_c[@]}"
        !            41:                        V4_DHCPCONF=true
        !            42:                        return
        !            43:                fi
1.177     rpe        44:                [[ ${_c[_name]} == alias ]] && _mask=3 _bc=4
                     45:                [[ -n ${_c[_mask]} ]] && _c[_mask]="netmask ${_c[_mask]}"
                     46:                if [[ -n ${_c[_bc]} ]]; then
                     47:                        _c[_bc]="broadcast ${_c[_bc]}"
                     48:                        [[ ${_c[_bc]} == *NONE ]] && _c[_bc]=
                     49:                fi
                     50:                _cmds[${#_cmds[*]}]="ifconfig $_if ${_c[@]}"
                     51:                ;;
                     52:        inet6)  ((${#_c[*]} > 1)) || return
                     53:                if [[ ${_c[_name]} == autoconf ]]; then
                     54:                        _cmds[${#_cmds[*]}]="ifconfig $_if ${_c[@]}"
1.180     rpe        55:                        V6_AUTOCONF=true
1.177     rpe        56:                        return
                     57:                fi
                     58:                [[ ${_c[_name]} == alias ]] && _prefix=3
                     59:                [[ -n ${_c[_prefix]} ]] && _c[_prefix]="prefixlen ${_c[_prefix]}"
                     60:                _cmds[${#_cmds[*]}]="ifconfig $_if ${_c[@]}"
                     61:                ;;
                     62:        dest)   ((${#_c[*]} == 2)) && _daddr=${_c[1]} || return
                     63:                _prev=$((${#_cmds[*]} - 1))
                     64:                ((_prev >= 0)) || return
                     65:                set -A _c -- ${_cmds[_prev]}
                     66:                _name=3
                     67:                [[ ${_c[_name]} == alias ]] && _name=4
                     68:                _c[_name]="${_c[_name]} $_daddr"
                     69:                _cmds[$_prev]="${_c[@]}"
                     70:                ;;
1.211     krw        71:        dhcp)   unset _c[0]
                     72:                _i=1
                     73:                while [[ ${_c[$_i]} == @(-c|-d|-i|-n|-r|-v) ]]; do
                     74:                        if [[ ${_c[$_i]} == @(-c|-i) ]]; then
                     75:                                _dhcp[${#_dhcp[*]}]=${_c[$_i]}
                     76:                                unset _c[$_i]
                     77:                                (( _i++ ))
                     78:                        fi
                     79:                        _dhcp[${#_dhcp[*]}]=${_c[$_i]}
                     80:                        unset _c[$_i]
                     81:                        (( _i++ ))
                     82:                done
                     83:                ((${#_c[*]} > 0)) && _cmds[${#_cmds[*]}]="ifconfig $_if ${_c[@]}"
                     84:                _cmds[${#_cmds[*]}]="dhclient ${_dhcp[@]} $_if"
1.180     rpe        85:                V4_DHCPCONF=true
1.177     rpe        86:                ;;
                     87:        '!'*)   _cmd=$(print -- "${_c[@]}" | sed 's/\$if/'$_if'/g')
                     88:                _cmds[${#_cmds[*]}]="${_cmd#!}"
                     89:                ;;
                     90:        *)      _cmds[${#_cmds[*]}]="ifconfig $_if ${_c[@]}"
                     91:                ;;
                     92:        esac
                     93:        unset _c
1.178     rpe        94:        set +o noglob
1.177     rpe        95: }
                     96:
1.192     rpe        97: # Create interface $1 if it does not yet exist.
1.195     rpe        98: # Usage: ifcreate if1
1.191     dlg        99: ifcreate() {
                    100:        local _if=$1
                    101:
1.192     rpe       102:        { ifconfig $_if || ifconfig $_if create; } >/dev/null 2>&1
1.191     dlg       103: }
                    104:
1.192     rpe       105: # Create interfaces for network pseudo-devices referred to by hostname.if files.
1.195     rpe       106: # Usage: vifscreate
1.191     dlg       107: vifscreate() {
1.192     rpe       108:        local _vif _hn _if
1.191     dlg       109:
1.192     rpe       110:        for _vif in $(ifconfig -C); do
1.193     rpe       111:                for _hn in /etc/hostname.${_vif}+([[:digit:]]); do
1.191     dlg       112:                        [[ -f $_hn ]] || continue
                    113:                        _if=${_hn#/etc/hostname.}
                    114:
1.193     rpe       115:                        if ! ifcreate $_if; then
1.194     rpe       116:                                print -u2 "${0##*/}: create for '$_if' failed."
1.193     rpe       117:                        fi
1.191     dlg       118:                done
                    119:        done
                    120: }
                    121:
1.160     rpe       122: # Start a single interface.
                    123: # Usage: ifstart if1
1.83      miod      124: ifstart() {
1.187     tb        125:        local _if=$1 _hn=/etc/hostname.$1 _cmds _i=0 _line _stat
1.177     rpe       126:        set -A _cmds
1.174     rpe       127:
1.83      miod      128:        # Interface names must be alphanumeric only.  We check to avoid
                    129:        # configuring backup or temp files, and to catch the "*" case.
1.177     rpe       130:        [[ $_if != +([[:alpha:]])+([[:digit:]]) ]] && return
1.83      miod      131:
1.185     rpe       132:        if [[ ! -f $_hn ]]; then
1.194     rpe       133:                print -u2 "${0##*/}: $_hn: No such file or directory."
1.121     todd      134:                return
                    135:        fi
1.177     rpe       136:
1.146     rpe       137:        # Not using stat(1), we can't rely on having /usr yet.
1.185     rpe       138:        set -A _stat -- $(ls -nL $_hn)
1.183     rpe       139:        if [[ "${_stat[0]}${_stat[2]}${_stat[3]}" != *---00 ]]; then
1.194     rpe       140:                print -u2 "WARNING: $_hn is insecure, fixing permissions."
1.185     rpe       141:                chmod -LR o-rwx $_hn
                    142:                chown -LR root:wheel $_hn
1.119     deraadt   143:        fi
1.83      miod      144:
1.177     rpe       145:        # Check for ifconfig'able interface, except if -n option is specified.
                    146:        if ! $PRINT_ONLY; then
1.191     dlg       147:                ifcreate $_if || return
1.177     rpe       148:        fi
                    149:
                    150:        # Parse the hostname.if(5) file and fill _cmds array with interface
                    151:        # configuration commands.
                    152:        set -o noglob
1.205     kn        153:        while IFS= read -- _line; do
1.177     rpe       154:                parse_hn_line $_line
1.185     rpe       155:        done <$_hn
1.177     rpe       156:
                    157:        # Apply the interface configuration commands stored in _cmds array.
                    158:        while ((_i < ${#_cmds[*]})); do
                    159:                if $PRINT_ONLY; then
                    160:                        print -r -- "${_cmds[_i]}"
1.83      miod      161:                else
1.177     rpe       162:                        eval "${_cmds[_i]}"
1.83      miod      163:                fi
1.177     rpe       164:                ((_i++))
                    165:        done
                    166:        unset _cmds
1.178     rpe       167:        set +o noglob
1.83      miod      168: }
                    169:
1.161     rpe       170: # Start multiple interfaces by driver name.
                    171: # Usage: ifmstart "em iwm" "trunk vlan"
1.146     rpe       172: #   Start "$1" interfaces in order or all interfaces if empty.
1.161     rpe       173: #   Don't start "$2" interfaces. "$2" is optional.
1.105     todd      174: ifmstart() {
1.161     rpe       175:        local _sifs=$1 _xifs=$2 _hn _if _sif _xif
                    176:
                    177:        for _sif in ${_sifs:-ALL}; do
1.193     rpe       178:                for _hn in /etc/hostname.+([[:alpha:]])+([[:digit:]]); do
                    179:                        [[ -f $_hn ]] || continue
1.161     rpe       180:                        _if=${_hn#/etc/hostname.}
1.113     david     181:
1.146     rpe       182:                        # Skip unwanted ifs.
1.161     rpe       183:                        for _xif in $_xifs; do
                    184:                                [[ $_xif == ${_if%%[0-9]*} ]] && continue 2
1.105     todd      185:                        done
                    186:
1.146     rpe       187:                        # Start wanted ifs.
1.161     rpe       188:                        [[ $_sif == @(ALL|${_if%%[0-9]*}) ]] && ifstart $_if
1.105     todd      189:                done
                    190:        done
                    191: }
                    192:
1.195     rpe       193: # Parse /etc/mygate and add default routes for IPv4 and IPv6.
1.172     mpi       194: # Usage: defaultroute
                    195: defaultroute() {
1.209     tb        196:        local _cmd _v4set=false _v6set=false;
1.208     deraadt   197:        set -o noglob
1.188     tb        198:
1.208     deraadt   199:        stripcom /etc/mygate |
1.180     rpe       200:        while read gw; do
1.208     deraadt   201:                case $gw in
                    202:                '!'*)
1.209     tb        203:                        _cmd=$(print -- "$gw")
1.208     deraadt   204:                        _cmd="${_cmd#!}"
                    205:                        ;;
1.209     tb        206:                !(*:*))
                    207:                        ($_v4set || $V4_DHCPCONF) && continue
                    208:                        _cmd="route -qn add -host default $gw"
                    209:                        _v4set=true
                    210:                        ;;
1.208     deraadt   211:                *)
1.209     tb        212:                        ($_v6set || $V6_AUTOCONF) && continue
                    213:                        _cmd="route -qn add -host -inet6 default $gw"
                    214:                        _v6set=true
1.208     deraadt   215:                        ;;
                    216:                esac
1.188     tb        217:                if $PRINT_ONLY; then
1.208     deraadt   218:                        print -r -- "$_cmd"
1.188     tb        219:                else
1.208     deraadt   220:                        $_cmd
1.188     tb        221:                fi
1.172     mpi       222:        done
1.208     deraadt   223:        set +o noglob
1.172     mpi       224: }
1.199     tb        225:
                    226: # Make sure the invoking user has the right privileges.  Check for presence of
                    227: # id(1) to avoid problems with diskless setups.
                    228: if [[ -x /usr/bin/id ]] && (($(id -u) != 0)); then
                    229:        echo "${0##*/}: need root privileges"
                    230:        exit 1
                    231: fi
1.154     sthen     232:
1.151     rpe       233: # Get network related vars from rc.conf using the parsing routine from rc.subr.
                    234: FUNCS_ONLY=1 . /etc/rc.d/rc.subr
1.150     ajacouto  235: _rc_parse_conf
1.177     rpe       236:
                    237: PRINT_ONLY=false
1.180     rpe       238: V4_DHCPCONF=false
                    239: V6_AUTOCONF=false
                    240:
1.177     rpe       241: while getopts ":n" opt; do
                    242:        case $opt in
                    243:        n)      PRINT_ONLY=true;;
1.198     rpe       244:        *)      usage;;
1.177     rpe       245:        esac
                    246: done
                    247: shift $((OPTIND-1))
                    248:
                    249: # Option -n is only supported if interface names are specified as parameters.
1.198     rpe       250: $PRINT_ONLY && (($# == 0)) && usage
1.189     florian   251:
1.195     rpe       252: # Load key material for the generation of IPv6 Semantically Opaque Interface
                    253: # Identifiers (SOII) used for link local and SLAAC addresses.
1.189     florian   254: $PRINT_ONLY || [[ ! -f /etc/soii.key ]] ||
                    255:        sysctl -q "net.inet6.ip6.soiikey=$(</etc/soii.key)"
1.1       deraadt   256:
1.83      miod      257: # If we were invoked with a list of interface names, just reconfigure these
1.172     mpi       258: # interfaces (or bridges), add default routes and return.
1.159     rpe       259: if (($# > 0)); then
                    260:        for _if; do ifstart $_if; done
1.172     mpi       261:        defaultroute
1.83      miod      262:        return
                    263: fi
                    264:
                    265: # Otherwise, process with the complete network initialization.
1.4       dm        266:
1.131     sobrado   267: # Set the address for the loopback interface.  Bringing the interface up,
                    268: # automatically invokes the IPv6 address ::1.
1.129     henning   269: ifconfig lo0 inet 127.0.0.1/8
1.24      kstailey  270:
1.195     rpe       271: # IPv6 configuration.
1.54      itojun    272: if ifconfig lo0 inet6 >/dev/null 2>&1; then
                    273:        ip6kernel=YES
                    274:
1.83      miod      275:        # Disallow link-local unicast dest without outgoing scope identifiers.
1.202     kn        276:        route -qn add -inet6 fe80:: -prefixlen 10 ::1 -reject
1.66      itojun    277:
1.83      miod      278:        # Disallow site-local unicast dest without outgoing scope identifiers.
1.66      itojun    279:        # If you configure site-locals without scope id (it is permissible
                    280:        # config for routers that are not on scope boundary), you may want
                    281:        # to comment the line out.
1.202     kn        282:        route -qn add -inet6 fec0:: -prefixlen 10 ::1 -reject
1.66      itojun    283:
1.83      miod      284:        # Disallow "internal" addresses to appear on the wire.
1.202     kn        285:        route -qn add -inet6 ::ffff:0.0.0.0 -prefixlen 96 ::1 -reject
1.66      itojun    286:
1.83      miod      287:        # Disallow packets to malicious 6to4 prefix.
1.202     kn        288:        route -qn add -inet6 2002:e000:: -prefixlen 20 ::1 -reject
                    289:        route -qn add -inet6 2002:7f00:: -prefixlen 24 ::1 -reject
                    290:        route -qn add -inet6 2002:0000:: -prefixlen 24 ::1 -reject
                    291:        route -qn add -inet6 2002:ff00:: -prefixlen 24 ::1 -reject
1.115     itojun    292:
                    293:        # Disallow packets without scope identifier.
1.202     kn        294:        route -qn add -inet6 ff01:: -prefixlen 16 ::1 -reject
                    295:        route -qn add -inet6 ff02:: -prefixlen 16 ::1 -reject
1.66      itojun    296:
                    297:        # Completely disallow packets to IPv4 compatible prefix.
1.146     rpe       298:        #
1.66      itojun    299:        # This may conflict with RFC1933 under following circumstances:
                    300:        # (1) An IPv6-only KAME node tries to originate packets to IPv4
1.77      deraadt   301:        #     compatible destination.  The KAME node has no IPv4 compatible
1.66      itojun    302:        #     support.  Under RFC1933, it should transmit native IPv6
                    303:        #     packets toward IPv4 compatible destination, hoping it would
                    304:        #     reach a router that forwards the packet toward auto-tunnel
                    305:        #     interface.
1.77      deraadt   306:        # (2) An IPv6-only node originates a packet to an IPv4 compatible
1.66      itojun    307:        #     destination.  A KAME node is acting as an IPv6 router, and
                    308:        #     asked to forward it.
1.146     rpe       309:        #
1.77      deraadt   310:        # Due to rare use of IPv4 compatible addresses, and security issues
1.66      itojun    311:        # with it, we disable it by default.
1.202     kn        312:        route -qn add -inet6 ::0.0.0.0 -prefixlen 96 ::1 -reject
1.54      itojun    313: else
                    314:        ip6kernel=NO
                    315: fi
                    316:
1.195     rpe       317: # Create all the pseudo interfaces up front.
1.191     dlg       318: vifscreate
1.105     todd      319:
                    320: # Configure all the non-loopback interfaces which we know about, but
1.127     deraadt   321: # do not start interfaces which must be delayed. Refer to hostname.if(5)
1.207     jmc       322: ifmstart "" "aggr trunk svlan vlan carp pppoe tun tap gif etherip gre egre pflow wg"
1.56      itojun    323:
1.201     dlg       324: # The aggr and trunk interfaces need to come up first in this list.
1.132     mpf       325: # The (s)vlan interfaces need to come up after trunk.
1.118     brad      326: # Configure all the carp interfaces which we know about before default route.
1.201     dlg       327: ifmstart "aggr trunk svlan vlan carp pppoe"
1.102     mcbride   328:
1.195     rpe       329: # Set default routes for IPv4 and IPv6.
1.172     mpi       330: defaultroute
1.44      deraadt   331:
1.48      niklas    332: # Multicast routing.
1.168     sthen     333: if [[ $multicast != YES ]]; then
                    334:        route -qn delete 224.0.0.0/4 >/dev/null 2>&1
1.147     rpe       335:        route -qn add -net 224.0.0.0/4 -interface 127.0.0.1 -reject >/dev/null
1.168     sthen     336: fi
1.105     todd      337:
1.146     rpe       338: # Reject 127/8 other than 127.0.0.1.
1.147     rpe       339: route -qn add -net 127 127.0.0.1 -reject >/dev/null
1.191     dlg       340:
                    341: # Configure interfaces that rely on routing
1.207     jmc       342: ifmstart "tun tap gif etherip gre egre pflow wg"
1.116     david     343:
1.159     rpe       344: if [[ $ip6kernel == YES ]]; then
1.195     rpe       345:        # Ensure IPv6 Duplicate Address Detection (DAD) is completed.
1.124     markus    346:        count=0
1.159     rpe       347:        while ((count++ < 10 && $(sysctl -n net.inet6.ip6.dad_pending) != 0)); do
1.124     markus    348:                sleep 1
                    349:        done
1.116     david     350: fi