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

Annotation of src/etc/netstart, Revision 1.158

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