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

Annotation of src/etc/netstart, Revision 1.88

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