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

Annotation of src/etc/netstart, Revision 1.104

1.1       deraadt     1: #!/bin/sh -
                      2: #
1.104   ! djm         3: #      $OpenBSD: netstart,v 1.103 2005/04/03 19:39:31 deraadt 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() {
                      8:        local _file="$1"
                      9:        local _line
                     10:
                     11:        {
                     12:                while read _line ; do
                     13:                        _line=${_line%%#*}              # strip comments
                     14:                        test -z "$_line" && continue
                     15:                        echo $_line
                     16:                done
                     17:        } < $_file
                     18: }
1.45      millert    19:
                     20: # Returns true if $1 contains only alphanumerics
                     21: isalphanumeric() {
                     22:        local _n
                     23:        _n=$1
                     24:        while [ ${#_n} != 0 ]; do
                     25:                case $_n in
                     26:                        [A-Za-z0-9]*)   ;;
                     27:                        *)              return 1;;
                     28:                esac
                     29:                _n=${_n#?}
                     30:        done
                     31:        return 0
                     32: }
1.81      angelos    33:
1.83      miod       34: # Start the $1 interface
                     35: ifstart() {
1.84      deraadt    36:        if=$1
1.83      miod       37:        # Interface names must be alphanumeric only.  We check to avoid
                     38:        # configuring backup or temp files, and to catch the "*" case.
1.84      deraadt    39:        if ! isalphanumeric "$if"; then
1.83      miod       40:                return
                     41:        fi
                     42:
1.84      deraadt    43:        ifconfig $if > /dev/null 2>&1
1.83      miod       44:        if [ "$?" != "0" ]; then
1.89      markus     45:                # Try to create interface if it does not exist
                     46:                ifconfig $if create > /dev/null 2>&1
                     47:                if [ "$?" != "0" ]; then
                     48:                        return
                     49:                fi
1.83      miod       50:        fi
                     51:
                     52:        # Now parse the hostname.* file
                     53:        while :; do
                     54:                if [ "$cmd2" ]; then
                     55:                        # We are carrying over from the 'read dt dtaddr'
                     56:                        # last time.
                     57:                        set -- $cmd2
                     58:                        af="$1" name="$2" mask="$3" bcaddr="$4" ext1="$5" cmd2=
                     59:                        # Make sure and get any remaining args in ext2,
                     60:                        # like the read below
                     61:                        i=1
                     62:                        while [ i -lt 6 -a -n "$1" ]; do shift; let i=i+1; done
                     63:                        ext2="$@"
                     64:                else
                     65:                        # Read the next line or exit the while loop.
                     66:                        read af name mask bcaddr ext1 ext2 || break
                     67:                fi
                     68:                # $af can be "dhcp", "up", "rtsol", an address family,
                     69:                # commands, or a comment.
                     70:                case "$af" in
                     71:                "#"*|"") # skip comments and empty lines
                     72:                        continue
                     73:                        ;;
                     74:                "!"*) # parse commands
                     75:                        cmd="${af#*!} ${name} ${mask} ${bcaddr} ${ext1} ${ext2}"
                     76:                        ;;
                     77:                "bridge")
1.84      deraadt    78:                        cmd="echo /etc/hostname.$if: bridges now supported via bridgename.* files"
1.83      miod       79:                        ;;
                     80:                "dhcp")
                     81:                        [ "$name" = "NONE" ] && name=
                     82:                        [ "$mask" = "NONE" ] && mask=
                     83:                        [ "$bcaddr" = "NONE" ] && bcaddr=
1.84      deraadt    84:                        ifconfig $if $name $mask $bcaddr $ext1 $ext2 down
                     85:                        cmd="dhclient $if"
1.103     deraadt    86:                        setgateway=N
1.83      miod       87:                        ;;
                     88:                "rtsol")
1.84      deraadt    89:                        ifconfig $if $name $mask $bcaddr $ext1 $ext2 up
                     90:                        rtsolif="$rtsolif $if"
1.83      miod       91:                        cmd=
                     92:                        ;;
                     93:                "up")
1.84      deraadt    94:                        # The only one of these guaranteed to be set is $if.
1.83      miod       95:                        # The remaining ones exist so that media controls work.
1.84      deraadt    96:                        cmd="ifconfig $if $name $mask $bcaddr $ext1 $ext2 up"
1.83      miod       97:                        ;;
                     98:                *)
                     99:                        read dt dtaddr
                    100:                        if [ "$name"  = "alias" ]; then
                    101:                                # perform a 'shift' of sorts
                    102:                                alias=$name
                    103:                                name=$mask
                    104:                                mask=$bcaddr
                    105:                                bcaddr=$ext1
                    106:                                ext1=$ext2
                    107:                                ext2=
                    108:                        else
                    109:                                alias=
                    110:                        fi
1.84      deraadt   111:                        cmd="ifconfig $if $af $alias $name "
1.83      miod      112:                        case "$dt" in
                    113:                        dest)
                    114:                                cmd="$cmd $dtaddr"
                    115:                                ;;
                    116:                        [a-z!]*)
                    117:                                cmd2="$dt $dtaddr"
                    118:                                ;;
                    119:                        esac
                    120:                        if [ ! -n "$name" ]; then
1.84      deraadt   121:                                echo "/etc/hostname.$if: invalid network configuration file"
1.83      miod      122:                                return
                    123:                        fi
                    124:                        case $af in
                    125:                        inet)
                    126:                                [ "$mask" ] && cmd="$cmd netmask $mask"
                    127:                                if [ "$bcaddr" -a "X$bcaddr" != "XNONE" ]; then
                    128:                                        cmd="$cmd broadcast $bcaddr"
                    129:                                fi
1.91      deraadt   130:                                [ "$alias" ] && rtcmd=";route -qn add -host $name 127.0.0.1"
1.83      miod      131:                                ;;
                    132:                        inet6) [ "$mask" ] && cmd="$cmd prefixlen $mask"
                    133:                                cmd="$cmd $bcaddr"
                    134:                                ;;
                    135:                        *)
                    136:                                cmd="$cmd $mask $bcaddr"
                    137:                                ;;
                    138:                        esac
                    139:                        cmd="$cmd $ext1 $ext2$rtcmd" rtcmd=
                    140:                        ;;
                    141:                esac
                    142:                eval "$cmd"
1.84      deraadt   143:        done < /etc/hostname.$if
1.83      miod      144: }
                    145:
                    146: # Start the $1 bridge
                    147: bridgestart() {
                    148:        # Interface names must be alphanumeric only.  We check to avoid
                    149:        # configuring backup or temp files, and to catch the "*" case.
                    150:        if ! isalphanumeric "$1"; then
                    151:                return
                    152:        fi
                    153:        brconfig $1 > /dev/null 2>&1
                    154:        if [ "$?" != "0" ]; then
1.90      millert   155:                # Try to create interface if it does not exist
                    156:                ifconfig $if create > /dev/null 2>&1
                    157:                if [ "$?" != "0" ]; then
                    158:                        return
                    159:                fi
1.83      miod      160:        fi
                    161:
                    162:        # Now parse the bridgename.* file
                    163:        # All lines are run as brconfig(8) commands.
                    164:        while read line ; do
                    165:                line=${line%%#*}                # strip comments
                    166:                test -z "$line" && continue
                    167:                case "$line" in
                    168:                "!"*)
                    169:                        cmd="${line#*!}"
                    170:                        ;;
                    171:                *)
                    172:                        cmd="brconfig $1 $line"
                    173:                        ;;
                    174:                esac
                    175:                eval "$cmd"
                    176:        done < /etc/bridgename.$1
                    177: }
                    178:
1.81      angelos   179: # Re-read /etc/rc.conf
                    180: . /etc/rc.conf
1.1       deraadt   181:
1.83      miod      182: # If we were invoked with a list of interface names, just reconfigure these
                    183: # interfaces (or bridges) and return.
                    184: if [ $1x = autobootx ]; then
                    185:        shift
                    186: fi
                    187: if [ $# -gt 0 ]; then
                    188:        while [ $# -gt 0 ]; do
                    189:                if [ -f /etc/bridgename.$1 ]; then
                    190:                        bridgestart $1
                    191:                else
                    192:                        ifstart $1
                    193:                fi
                    194:                shift
                    195:        done
                    196:        return
                    197: fi
                    198:
                    199: # Otherwise, process with the complete network initialization.
                    200:
1.1       deraadt   201: # /etc/myname contains my symbolic name
1.87      henning   202: if [ -f /etc/myname ]; then
1.100     millert   203:        hostname=`stripcom /etc/myname`
1.87      henning   204:        hostname $hostname
                    205: else
                    206:        hostname=`hostname`
                    207: fi
                    208:
1.1       deraadt   209: if [ -f /etc/defaultdomain ]; then
1.100     millert   210:        domainname `stripcom /etc/defaultdomain`
1.4       dm        211: fi
                    212:
1.93      deraadt   213: # Set the address for the loopback interface.  Bringing the
                    214: # interface up, automatically invokes the IPv6 address ::1)
1.92      deraadt   215: ifconfig lo0 inet 127.0.0.1
1.24      kstailey  216:
1.54      itojun    217: if ifconfig lo0 inet6 >/dev/null 2>&1; then
                    218:        # IPv6 configurations.
                    219:        ip6kernel=YES
                    220:
1.83      miod      221:        # Disallow link-local unicast dest without outgoing scope identifiers.
1.98      deraadt   222:        route -qn add -inet6 fe80:: -prefixlen 10 ::1 -reject > /dev/null
1.66      itojun    223:
1.83      miod      224:        # Disallow site-local unicast dest without outgoing scope identifiers.
1.66      itojun    225:        # If you configure site-locals without scope id (it is permissible
                    226:        # config for routers that are not on scope boundary), you may want
                    227:        # to comment the line out.
1.98      deraadt   228:        route -qn add -inet6 fec0:: -prefixlen 10 ::1 -reject > /dev/null
1.66      itojun    229:
1.83      miod      230:        # Disallow "internal" addresses to appear on the wire.
1.98      deraadt   231:        route -qn add -inet6 ::ffff:0.0.0.0 -prefixlen 96 ::1 -reject > /dev/null
1.66      itojun    232:
1.83      miod      233:        # Disallow packets to malicious IPv4 compatible prefix.
1.98      deraadt   234:        route -qn add -inet6 ::224.0.0.0 -prefixlen 100 ::1 -reject > /dev/null
                    235:        route -qn add -inet6 ::127.0.0.0 -prefixlen 104 ::1 -reject > /dev/null
                    236:        route -qn add -inet6 ::0.0.0.0 -prefixlen 104 ::1 -reject > /dev/null
                    237:        route -qn add -inet6 ::255.0.0.0 -prefixlen 104 ::1 -reject > /dev/null
1.66      itojun    238:
1.83      miod      239:        # Disallow packets to malicious 6to4 prefix.
1.98      deraadt   240:        route -qn add -inet6 2002:e000:: -prefixlen 20 ::1 -reject > /dev/null
                    241:        route -qn add -inet6 2002:7f00:: -prefixlen 24 ::1 -reject > /dev/null
                    242:        route -qn add -inet6 2002:0000:: -prefixlen 24 ::1 -reject > /dev/null
                    243:        route -qn add -inet6 2002:ff00:: -prefixlen 24 ::1 -reject > /dev/null
1.66      itojun    244:
                    245:        # Completely disallow packets to IPv4 compatible prefix.
                    246:        # This may conflict with RFC1933 under following circumstances:
                    247:        # (1) An IPv6-only KAME node tries to originate packets to IPv4
1.77      deraadt   248:        #     compatible destination.  The KAME node has no IPv4 compatible
1.66      itojun    249:        #     support.  Under RFC1933, it should transmit native IPv6
                    250:        #     packets toward IPv4 compatible destination, hoping it would
                    251:        #     reach a router that forwards the packet toward auto-tunnel
                    252:        #     interface.
1.77      deraadt   253:        # (2) An IPv6-only node originates a packet to an IPv4 compatible
1.66      itojun    254:        #     destination.  A KAME node is acting as an IPv6 router, and
                    255:        #     asked to forward it.
1.77      deraadt   256:        # Due to rare use of IPv4 compatible addresses, and security issues
1.66      itojun    257:        # with it, we disable it by default.
1.98      deraadt   258:        route -qn add -inet6 ::0.0.0.0 -prefixlen 96 ::1 -reject > /dev/null
1.56      itojun    259:
                    260:        rtsolif=""
1.54      itojun    261: else
                    262:        ip6kernel=NO
                    263: fi
                    264:
1.83      miod      265: # Configure all the non-loopback interfaces which we know about.
                    266: # Refer to hostname.if(5) and bridgename.if(5)
1.47      niklas    267: for hn in /etc/hostname.*; do
1.83      miod      268:        # Strip off /etc/hostname. prefix
                    269:        if=${hn#/etc/hostname.}
1.85      todd      270:        test "$if" = "*" && continue
1.47      niklas    271:
1.83      miod      272:        case $if in
1.95      mcbride   273:        "carp"*|"gif"*|"gre"*|"pfsync"*)
                    274:                # CARP, GIF, GRE and PFSYNC interfaces need the routes to be setup
1.88      david     275:                # before they are configured.
1.83      miod      276:                continue
                    277:                ;;
1.55      todd      278:        *)
1.83      miod      279:                ifstart $if
1.55      todd      280:                ;;
1.47      niklas    281:        esac
1.50      deraadt   282: done
1.56      itojun    283:
                    284: if [ "$ip6kernel" = "YES" -a "x$rtsolif" != "x" ]; then
                    285:        fw=`sysctl -n net.inet6.ip6.forwarding`
                    286:        ra=`sysctl -n net.inet6.ip6.accept_rtadv`
                    287:        if [ "x$fw" = "x0" -a "x$ra" = "x1" ]; then
1.57      itojun    288:                echo "IPv6 autoconf:$rtsolif"
1.56      itojun    289:                rtsol $rtsolif
                    290:        else
                    291:                echo "WARNING: inconsistent config - check /etc/sysctl.conf for IPv6 autoconf"
                    292:        fi
1.57      itojun    293: fi
                    294: if [ "$ip6kernel" = "YES" ]; then
                    295:        # this is to make sure DAD is completed before going further.
                    296:        sleep `sysctl -n net.inet6.ip6.dad_count`
1.56      itojun    297: fi
                    298:
1.102     mcbride   299: # The pfsync interface needs to come up before carp.
                    300: if [ -f /etc/hostname.pfsync0 ]; then
                    301:        ifstart pfsync0
                    302: fi
                    303:
                    304: # Configure all the carp interfaces which we know about.
                    305: # They must come up after pfsync but before default route.
                    306: for hn in /etc/hostname.*; do
                    307:        # Strip off /etc/hostname. prefix
                    308:        if=${hn#/etc/hostname.}
                    309:        test "$if" = "*" && continue
                    310:
                    311:        case $if in
                    312:        "carp"*)
                    313:                ifstart $if
                    314:                ;;
                    315:        *)
                    316:                # Regular interfaces have already been configured.
                    317:                continue
                    318:                ;;
                    319:        esac
                    320: done
                    321:
1.14      deraadt   322: # /etc/mygate, if it exists, contains the name of my gateway host
                    323: # that name must be in /etc/hosts.
1.104   ! djm       324: if [ "X${setgateway}" != X"N" -a -f /etc/mygate ]; then
1.97      deraadt   325:        route -qn delete default > /dev/null 2>&1
1.100     millert   326:        route -qn add -host default `stripcom /etc/mygate`
1.14      deraadt   327: fi
1.44      deraadt   328:
1.48      niklas    329: # Multicast routing.
                    330: #
                    331: # The routing to the 224.0.0.0/4 net is setup according to these rules:
                    332: # multicast_host       multicast_router        route           comment
                    333: # NO                   NO                      -reject         no multicast
                    334: # NO                   YES                     none installed  daemon will run
                    335: # YES/interface                NO                      -interface      YES=def. iface
                    336: #         Any other combination                -reject         config error
                    337: case "$multicast_host:$multicast_router" in
                    338: NO:NO)
1.91      deraadt   339:        route -qn add -net 224.0.0.0/4 -interface 127.0.0.1 -reject > /dev/null
1.67      deraadt   340:        ;;
1.48      niklas    341: NO:YES)
                    342:        ;;
                    343: *:NO)
                    344:        set `if [ $multicast_host = YES ]; then
1.91      deraadt   345:                ed -s '!route -qn show -inet' <<EOF
1.48      niklas    346: /^default/p
                    347: EOF
                    348:        else
                    349:                ed -s "!ifconfig $multicast_host" <<EOF
                    350: /^     inet /p
                    351: EOF
                    352:        fi`
1.91      deraadt   353:        route -qn add -net 224.0.0.0/4 -interface $2 > /dev/null
1.67      deraadt   354:        ;;
1.48      niklas    355: *:*)
                    356:        echo 'config error, multicasting disabled until rc.conf is fixed'
1.91      deraadt   357:        route -qn add -net 224.0.0.0/4 -interface 127.0.0.1 -reject > /dev/null
1.67      deraadt   358:        ;;
1.48      niklas    359: esac
1.83      miod      360:
1.102     mcbride   361: # Configure all the gif and gre interfaces which we know about.
1.83      miod      362: # They were delayed because they require the routes to be set.
                    363: for hn in /etc/hostname.*; do
                    364:        # Strip off /etc/hostname. prefix
                    365:        if=${hn#/etc/hostname.}
1.85      todd      366:        test "$if" = "*" && continue
1.83      miod      367:
                    368:        case $if in
1.102     mcbride   369:        "gif"*|"gre"*)
1.83      miod      370:                ifstart $if
                    371:                ;;
                    372:        *)
                    373:                # Regular interfaces have already been configured.
                    374:                continue
                    375:                ;;
                    376:        esac
                    377: done
1.92      deraadt   378:
1.99      itojun    379: # reject 127/8 other than 127.0.0.1
1.92      deraadt   380: route -qn add -net 127 127.0.0.1 -reject > /dev/null
1.83      miod      381:
                    382: # Configure all the bridges.
                    383: for bn in /etc/bridgename.*; do
                    384:        # Strip off /etc/bridgename. prefix
                    385:        if=${bn#/etc/bridgename.}
1.85      todd      386:        test "$if" = "*" && continue
1.83      miod      387:
                    388:        bridgestart $if
                    389: done