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

Annotation of src/etc/netstart, Revision 1.136

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