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

Annotation of src/etc/netstart, Revision 1.149

1.1       deraadt     1: #!/bin/sh -
                      2: #
1.149   ! rpe         3: #      $OpenBSD: netstart,v 1.148 2015/07/19 01:37:45 rpe Exp $
1.101     millert     4:
1.146     rpe         5: # Strip comments (and leading/trailing whitespace if IFS is set) from a file
                      6: # and spew to stdout.
1.101     millert     7: stripcom() {
1.108     todd        8:        local _l
1.110     todd        9:        [[ -f $1 ]] || return
1.108     todd       10:        while read _l; do
                     11:                [[ -n ${_l%%#*} ]] && echo $_l
                     12:        done<$1
1.101     millert    13: }
1.45      millert    14:
1.146     rpe        15: # Start the $1 interface.
1.83      miod       16: ifstart() {
1.84      deraadt    17:        if=$1
1.83      miod       18:        # Interface names must be alphanumeric only.  We check to avoid
                     19:        # configuring backup or temp files, and to catch the "*" case.
1.137     rpe        20:        [[ $if != +([[:alpha:]])+([[:digit:]]) ]] && return
1.83      miod       21:
1.119     deraadt    22:        file=/etc/hostname.$if
1.121     todd       23:        if ! [ -f $file ]; then
                     24:                echo "netstart: $file: No such file or directory"
                     25:                return
                     26:        fi
1.146     rpe        27:        # Not using stat(1), we can't rely on having /usr yet.
1.123     sthen      28:        set -A stat -- `ls -nL $file`
                     29:        if [ "${stat[0]#???????} ${stat[2]} ${stat[3]}" != "--- 0 0" ]; then
1.119     deraadt    30:                echo "WARNING: $file is insecure, fixing permissions"
1.122     sthen      31:                chmod -LR o-rwx $file
                     32:                chown -LR root.wheel $file
1.119     deraadt    33:        fi
1.136     rpe        34:        # Check for ifconfig'able interface.
                     35:        (ifconfig $if || ifconfig $if create) >/dev/null 2>&1 || return
1.83      miod       36:
1.146     rpe        37:        # Now parse the hostname.* file.
1.83      miod       38:        while :; do
                     39:                if [ "$cmd2" ]; then
                     40:                        # We are carrying over from the 'read dt dtaddr'
                     41:                        # last time.
                     42:                        set -- $cmd2
                     43:                        af="$1" name="$2" mask="$3" bcaddr="$4" ext1="$5" cmd2=
                     44:                        # Make sure and get any remaining args in ext2,
1.146     rpe        45:                        # like the read below.
1.83      miod       46:                        i=1
1.126     simon      47:                        while [ $i -lt 6 -a -n "$1" ]; do shift; let i=i+1; done
1.83      miod       48:                        ext2="$@"
                     49:                else
                     50:                        # Read the next line or exit the while loop.
                     51:                        read af name mask bcaddr ext1 ext2 || break
                     52:                fi
1.146     rpe        53:                # $af can be "dhcp", "up", "rtsol", an address family, commands,
                     54:                # or a comment.
1.83      miod       55:                case "$af" in
1.146     rpe        56:                "#"*|"") # Skip comments and empty lines.
1.83      miod       57:                        continue
                     58:                        ;;
1.146     rpe        59:                "!"*) # Parse commands.
1.83      miod       60:                        cmd="${af#*!} ${name} ${mask} ${bcaddr} ${ext1} ${ext2}"
                     61:                        ;;
                     62:                "dhcp")
                     63:                        [ "$name" = "NONE" ] && name=
                     64:                        [ "$mask" = "NONE" ] && mask=
                     65:                        [ "$bcaddr" = "NONE" ] && bcaddr=
1.143     claudio    66:                        cmd="ifconfig $if $name $mask $bcaddr $ext1 $ext2 down"
1.106     todd       67:                        cmd="$cmd;dhclient $if"
1.110     todd       68:                        dhcpif="$dhcpif $if"
1.83      miod       69:                        ;;
                     70:                "rtsol")
1.84      deraadt    71:                        rtsolif="$rtsolif $if"
1.106     todd       72:                        cmd="ifconfig $if $name $mask $bcaddr $ext1 $ext2 up"
1.83      miod       73:                        ;;
                     74:                *)
                     75:                        read dt dtaddr
                     76:                        if [ "$name"  = "alias" ]; then
1.146     rpe        77:                                # Perform a 'shift' of sorts.
1.83      miod       78:                                alias=$name
                     79:                                name=$mask
                     80:                                mask=$bcaddr
                     81:                                bcaddr=$ext1
                     82:                                ext1=$ext2
                     83:                                ext2=
                     84:                        else
                     85:                                alias=
                     86:                        fi
1.114     todd       87:                        cmd="ifconfig $if $af $alias $name"
1.83      miod       88:                        case "$dt" in
                     89:                        dest)
                     90:                                cmd="$cmd $dtaddr"
                     91:                                ;;
1.130     todd       92:                        *)
1.83      miod       93:                                cmd2="$dt $dtaddr"
                     94:                                ;;
                     95:                        esac
                     96:                        case $af in
                     97:                        inet)
1.128     todd       98:                                if [ ! -n "$name" ]; then
                     99:                                        echo "/etc/hostname.$if: inet alone is invalid"
                    100:                                        return
                    101:                                fi
1.83      miod      102:                                [ "$mask" ] && cmd="$cmd netmask $mask"
                    103:                                if [ "$bcaddr" -a "X$bcaddr" != "XNONE" ]; then
                    104:                                        cmd="$cmd broadcast $bcaddr"
                    105:                                fi
                    106:                                ;;
1.128     todd      107:                        inet6)
                    108:                                if [ ! -n "$name" ]; then
                    109:                                        echo "/etc/hostname.$if: inet6 alone is invalid"
                    110:                                        return
                    111:                                fi
                    112:                                [ "$mask" ] && cmd="$cmd prefixlen $mask"
1.83      miod      113:                                cmd="$cmd $bcaddr"
                    114:                                ;;
                    115:                        *)
                    116:                                cmd="$cmd $mask $bcaddr"
                    117:                                ;;
                    118:                        esac
1.139     mpi       119:                        cmd="$cmd $ext1 $ext2"
1.83      miod      120:                        ;;
                    121:                esac
                    122:                eval "$cmd"
1.147     rpe       123:        done </etc/hostname.$if
1.83      miod      124: }
                    125:
1.105     todd      126: # Start multiple:
1.146     rpe       127: # Usage: ifmstart "if1 if2" "if3 if4"
                    128: #   Start "$1" interfaces in order or all interfaces if empty.
                    129: #   Don't start "$2" interfaces.
1.105     todd      130: ifmstart() {
                    131:        for sif in ${1:-ALL}; do
                    132:                for hn in /etc/hostname.*; do
1.146     rpe       133:                        # Strip off /etc/hostname. prefix.
1.105     todd      134:                        if=${hn#/etc/hostname.}
1.149   ! rpe       135:                        [ "$if" = "*" ] && continue
1.113     david     136:
1.146     rpe       137:                        # Skip unwanted ifs.
1.113     david     138:                        s=""
1.105     todd      139:                        for xf in $2; do
1.149   ! rpe       140:                                [ "$xf" = "${if%%[0-9]*}" ] && s="1" && break
1.105     todd      141:                        done
1.149   ! rpe       142:                        [ "$s" = "1" ] && continue
1.105     todd      143:
1.146     rpe       144:                        # Start wanted ifs.
1.149   ! rpe       145:                        [ "$sif" = "ALL" -o "$sif" = "${if%%[0-9]*}" ] &&
        !           146:                                ifstart $if
1.105     todd      147:                done
                    148:        done
                    149: }
                    150:
1.148     rpe       151: # Source rc.subr and parse rc.conf only if we are not inside /etc/rc.
                    152: if [ -z "${INRC}" ]; then
                    153:        FUNCS_ONLY=1 . /etc/rc.d/rc.subr
                    154:        _rc_parse_conf
                    155: fi
1.1       deraadt   156:
1.83      miod      157: # If we were invoked with a list of interface names, just reconfigure these
                    158: # interfaces (or bridges) and return.
1.135     rpe       159: if [[ $1 == autoboot ]]; then
1.83      miod      160:        shift
                    161: fi
                    162: if [ $# -gt 0 ]; then
                    163:        while [ $# -gt 0 ]; do
1.127     deraadt   164:                ifstart $1
1.83      miod      165:                shift
                    166:        done
                    167:        return
                    168: fi
                    169:
                    170: # Otherwise, process with the complete network initialization.
                    171:
1.146     rpe       172: # /etc/myname contains my symbolic name.
1.87      henning   173: if [ -f /etc/myname ]; then
1.100     millert   174:        hostname=`stripcom /etc/myname`
1.87      henning   175:        hostname $hostname
                    176: else
                    177:        hostname=`hostname`
1.4       dm        178: fi
                    179:
1.131     sobrado   180: # Set the address for the loopback interface.  Bringing the interface up,
                    181: # automatically invokes the IPv6 address ::1.
1.129     henning   182: ifconfig lo0 inet 127.0.0.1/8
1.24      kstailey  183:
1.54      itojun    184: if ifconfig lo0 inet6 >/dev/null 2>&1; then
                    185:        # IPv6 configurations.
                    186:        ip6kernel=YES
                    187:
1.83      miod      188:        # Disallow link-local unicast dest without outgoing scope identifiers.
1.147     rpe       189:        route -qn add -inet6 fe80:: -prefixlen 10 ::1 -reject >/dev/null
1.66      itojun    190:
1.83      miod      191:        # Disallow site-local unicast dest without outgoing scope identifiers.
1.66      itojun    192:        # If you configure site-locals without scope id (it is permissible
                    193:        # config for routers that are not on scope boundary), you may want
                    194:        # to comment the line out.
1.147     rpe       195:        route -qn add -inet6 fec0:: -prefixlen 10 ::1 -reject >/dev/null
1.66      itojun    196:
1.83      miod      197:        # Disallow "internal" addresses to appear on the wire.
1.147     rpe       198:        route -qn add -inet6 ::ffff:0.0.0.0 -prefixlen 96 ::1 -reject >/dev/null
1.66      itojun    199:
1.83      miod      200:        # Disallow packets to malicious IPv4 compatible prefix.
1.147     rpe       201:        route -qn add -inet6 ::224.0.0.0 -prefixlen 100 ::1 -reject >/dev/null
                    202:        route -qn add -inet6 ::127.0.0.0 -prefixlen 104 ::1 -reject >/dev/null
                    203:        route -qn add -inet6 ::0.0.0.0 -prefixlen 104 ::1 -reject >/dev/null
                    204:        route -qn add -inet6 ::255.0.0.0 -prefixlen 104 ::1 -reject >/dev/null
1.66      itojun    205:
1.83      miod      206:        # Disallow packets to malicious 6to4 prefix.
1.147     rpe       207:        route -qn add -inet6 2002:e000:: -prefixlen 20 ::1 -reject >/dev/null
                    208:        route -qn add -inet6 2002:7f00:: -prefixlen 24 ::1 -reject >/dev/null
                    209:        route -qn add -inet6 2002:0000:: -prefixlen 24 ::1 -reject >/dev/null
                    210:        route -qn add -inet6 2002:ff00:: -prefixlen 24 ::1 -reject >/dev/null
1.115     itojun    211:
                    212:        # Disallow packets without scope identifier.
1.147     rpe       213:        route -qn add -inet6 ff01:: -prefixlen 16 ::1 -reject >/dev/null
                    214:        route -qn add -inet6 ff02:: -prefixlen 16 ::1 -reject >/dev/null
1.66      itojun    215:
                    216:        # Completely disallow packets to IPv4 compatible prefix.
1.146     rpe       217:        #
1.66      itojun    218:        # This may conflict with RFC1933 under following circumstances:
                    219:        # (1) An IPv6-only KAME node tries to originate packets to IPv4
1.77      deraadt   220:        #     compatible destination.  The KAME node has no IPv4 compatible
1.66      itojun    221:        #     support.  Under RFC1933, it should transmit native IPv6
                    222:        #     packets toward IPv4 compatible destination, hoping it would
                    223:        #     reach a router that forwards the packet toward auto-tunnel
                    224:        #     interface.
1.77      deraadt   225:        # (2) An IPv6-only node originates a packet to an IPv4 compatible
1.66      itojun    226:        #     destination.  A KAME node is acting as an IPv6 router, and
                    227:        #     asked to forward it.
1.146     rpe       228:        #
1.77      deraadt   229:        # Due to rare use of IPv4 compatible addresses, and security issues
1.66      itojun    230:        # with it, we disable it by default.
1.147     rpe       231:        route -qn add -inet6 ::0.0.0.0 -prefixlen 96 ::1 -reject >/dev/null
1.56      itojun    232:
                    233:        rtsolif=""
1.54      itojun    234: else
                    235:        ip6kernel=NO
                    236: fi
                    237:
1.105     todd      238:
                    239: # Configure all the non-loopback interfaces which we know about, but
1.127     deraadt   240: # do not start interfaces which must be delayed. Refer to hostname.if(5)
1.132     mpf       241: ifmstart "" "trunk svlan vlan carp gif gre pfsync pppoe tun bridge"
1.56      itojun    242:
1.118     brad      243: # The trunk interfaces need to come up first in this list.
1.132     mpf       244: # The (s)vlan interfaces need to come up after trunk.
1.118     brad      245: # Configure all the carp interfaces which we know about before default route.
1.132     mpf       246: ifmstart "trunk svlan vlan carp"
1.118     brad      247:
1.56      itojun    248: if [ "$ip6kernel" = "YES" -a "x$rtsolif" != "x" ]; then
1.145     florian   249:        echo "IPv6 autoconf:$rtsolif"
                    250:        ifconfig $rtsolif inet6 autoconf
1.57      itojun    251: fi
1.102     mcbride   252:
1.138     todd      253: # Look for default routes in /etc/mygate.
1.110     todd      254: [[ -z $dhcpif ]] && stripcom /etc/mygate | while read gw; do
                    255:                [[ $gw == @(*:*) ]] && continue
1.147     rpe       256:                route -qn delete default >/dev/null 2>&1
1.110     todd      257:                route -qn add -host default $gw && break
                    258: done
                    259: [[ -z $rtsolif ]] && stripcom /etc/mygate | while read gw; do
                    260:                [[ $gw == !(*:*) ]] && continue
1.147     rpe       261:                route -qn delete -inet6 default >/dev/null 2>&1
1.110     todd      262:                route -qn add -host -inet6 default $gw && break
                    263: done
1.44      deraadt   264:
1.48      niklas    265: # Multicast routing.
                    266: #
                    267: # The routing to the 224.0.0.0/4 net is setup according to these rules:
                    268: # multicast_host       multicast_router        route           comment
                    269: # NO                   NO                      -reject         no multicast
                    270: # NO                   YES                     none installed  daemon will run
                    271: # YES/interface                NO                      -interface      YES=def. iface
                    272: #         Any other combination                -reject         config error
1.147     rpe       273: route -qn delete 224.0.0.0/4 >/dev/null 2>&1
1.48      niklas    274: case "$multicast_host:$multicast_router" in
                    275: NO:NO)
1.147     rpe       276:        route -qn add -net 224.0.0.0/4 -interface 127.0.0.1 -reject >/dev/null
1.67      deraadt   277:        ;;
1.48      niklas    278: NO:YES)
                    279:        ;;
                    280: *:NO)
1.112     reyk      281:        maddr=`if [ "$multicast_host" = "YES" ]; then
1.91      deraadt   282:                ed -s '!route -qn show -inet' <<EOF
1.48      niklas    283: /^default/p
                    284: EOF
                    285:        else
                    286:                ed -s "!ifconfig $multicast_host" <<EOF
                    287: /^     inet /p
                    288: EOF
1.147     rpe       289:        fi 2>/dev/null`
1.112     reyk      290:        if [ "X${maddr}" != "X" ]; then
                    291:                set $maddr
1.147     rpe       292:                route -qn add -net 224.0.0.0/4 -interface $2 >/dev/null
1.112     reyk      293:        else
                    294:                route -qn add -net 224.0.0.0/4 -interface \
1.147     rpe       295:                        127.0.0.1 -reject >/dev/null
1.112     reyk      296:        fi
1.67      deraadt   297:        ;;
1.48      niklas    298: *:*)
                    299:        echo 'config error, multicasting disabled until rc.conf is fixed'
1.147     rpe       300:        route -qn add -net 224.0.0.0/4 -interface 127.0.0.1 -reject >/dev/null
1.67      deraadt   301:        ;;
1.48      niklas    302: esac
1.83      miod      303:
1.105     todd      304:
1.125     jdixon    305: # Configure PPPoE, GIF, GRE and TUN interfaces, delayed because they require
                    306: # routes to be set.  TUN might depend on PPPoE, and GIF or GRE may depend on
                    307: # either of them.
1.127     deraadt   308: ifmstart "pppoe tun gif gre bridge"
1.92      deraadt   309:
1.146     rpe       310: # Reject 127/8 other than 127.0.0.1.
1.147     rpe       311: route -qn add -net 127 127.0.0.1 -reject >/dev/null
1.116     david     312:
                    313: if [ "$ip6kernel" = "YES" ]; then
1.146     rpe       314:        # This is to make sure DAD is completed before going further.
1.124     markus    315:        count=0
                    316:        while [ $((count++)) -lt 10 -a "x"`sysctl -n net.inet6.ip6.dad_pending` != "x0" ]; do
                    317:                sleep 1
                    318:        done
1.116     david     319: fi