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

Annotation of src/etc/netstart, Revision 1.85

1.1       deraadt     1: #!/bin/sh -
                      2: #
1.85    ! todd        3: #      $OpenBSD: netstart,v 1.84 2002/02/23 01:55:24 deraadt 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
                    178: hostname=`cat /etc/myname`
                    179: hostname $hostname
                    180: if [ -f /etc/defaultdomain ]; then
                    181:        domainname `cat /etc/defaultdomain`
1.4       dm        182: fi
                    183:
1.83      miod      184: # Set the address for the loopback interface.
                    185: # It will also initialize IPv6 address for lo0 (::1 and others).
1.24      kstailey  186: ifconfig lo0 inet localhost
1.1       deraadt   187:
1.83      miod      188: # Use loopback, not the wire.
1.67      deraadt   189: route -n add -host $hostname localhost > /dev/null
                    190: route -n add -net 127 127.0.0.1 -reject > /dev/null
1.24      kstailey  191:
1.54      itojun    192: if ifconfig lo0 inet6 >/dev/null 2>&1; then
                    193:        # IPv6 configurations.
                    194:        ip6kernel=YES
                    195:
1.83      miod      196:        # Disallow link-local unicast dest without outgoing scope identifiers.
1.67      deraadt   197:        route add -inet6 fe80:: -prefixlen 10 ::1 -reject > /dev/null
1.66      itojun    198:
1.83      miod      199:        # Disallow site-local unicast dest without outgoing scope identifiers.
1.66      itojun    200:        # If you configure site-locals without scope id (it is permissible
                    201:        # config for routers that are not on scope boundary), you may want
                    202:        # to comment the line out.
1.67      deraadt   203:        route add -inet6 fec0:: -prefixlen 10 ::1 -reject > /dev/null
1.66      itojun    204:
1.83      miod      205:        # Disallow "internal" addresses to appear on the wire.
1.67      deraadt   206:        route add -inet6 ::ffff:0.0.0.0 -prefixlen 96 ::1 -reject > /dev/null
1.66      itojun    207:
1.83      miod      208:        # Disallow packets to malicious IPv4 compatible prefix.
1.67      deraadt   209:        route add -inet6 ::224.0.0.0 -prefixlen 100 ::1 -reject > /dev/null
                    210:        route add -inet6 ::127.0.0.0 -prefixlen 104 ::1 -reject > /dev/null
                    211:        route add -inet6 ::0.0.0.0 -prefixlen 104 ::1 -reject > /dev/null
                    212:        route add -inet6 ::255.0.0.0 -prefixlen 104 ::1 -reject > /dev/null
1.66      itojun    213:
1.83      miod      214:        # Disallow packets to malicious 6to4 prefix.
1.67      deraadt   215:        route add -inet6 2002:e000:: -prefixlen 20 ::1 -reject > /dev/null
                    216:        route add -inet6 2002:7f00:: -prefixlen 24 ::1 -reject > /dev/null
                    217:        route add -inet6 2002:0000:: -prefixlen 24 ::1 -reject > /dev/null
                    218:        route add -inet6 2002:ff00:: -prefixlen 24 ::1 -reject > /dev/null
1.66      itojun    219:
                    220:        # Completely disallow packets to IPv4 compatible prefix.
                    221:        # This may conflict with RFC1933 under following circumstances:
                    222:        # (1) An IPv6-only KAME node tries to originate packets to IPv4
1.77      deraadt   223:        #     compatible destination.  The KAME node has no IPv4 compatible
1.66      itojun    224:        #     support.  Under RFC1933, it should transmit native IPv6
                    225:        #     packets toward IPv4 compatible destination, hoping it would
                    226:        #     reach a router that forwards the packet toward auto-tunnel
                    227:        #     interface.
1.77      deraadt   228:        # (2) An IPv6-only node originates a packet to an IPv4 compatible
1.66      itojun    229:        #     destination.  A KAME node is acting as an IPv6 router, and
                    230:        #     asked to forward it.
1.77      deraadt   231:        # Due to rare use of IPv4 compatible addresses, and security issues
1.66      itojun    232:        # with it, we disable it by default.
1.67      deraadt   233:        route add -inet6 ::0.0.0.0 -prefixlen 96 ::1 -reject > /dev/null
1.56      itojun    234:
                    235:        rtsolif=""
1.54      itojun    236: else
                    237:        ip6kernel=NO
                    238: fi
                    239:
1.83      miod      240: # Configure all the non-loopback interfaces which we know about.
                    241: # Refer to hostname.if(5) and bridgename.if(5)
1.47      niklas    242: for hn in /etc/hostname.*; do
1.83      miod      243:        # Strip off /etc/hostname. prefix
                    244:        if=${hn#/etc/hostname.}
1.85    ! todd      245:        test "$if" = "*" && continue
1.47      niklas    246:
1.83      miod      247:        case $if in
                    248:        "gif"*|"gre"*)
                    249:                # GIF and GRE interfaces need the routes to be setup before
                    250:                # they are configured.
                    251:                continue
                    252:                ;;
1.55      todd      253:        *)
1.83      miod      254:                ifstart $if
1.55      todd      255:                ;;
1.47      niklas    256:        esac
1.50      deraadt   257: done
1.56      itojun    258:
                    259: if [ "$ip6kernel" = "YES" -a "x$rtsolif" != "x" ]; then
                    260:        fw=`sysctl -n net.inet6.ip6.forwarding`
                    261:        ra=`sysctl -n net.inet6.ip6.accept_rtadv`
                    262:        if [ "x$fw" = "x0" -a "x$ra" = "x1" ]; then
1.57      itojun    263:                echo "IPv6 autoconf:$rtsolif"
1.56      itojun    264:                rtsol $rtsolif
                    265:        else
                    266:                echo "WARNING: inconsistent config - check /etc/sysctl.conf for IPv6 autoconf"
                    267:        fi
1.57      itojun    268: fi
                    269: if [ "$ip6kernel" = "YES" ]; then
                    270:        # this is to make sure DAD is completed before going further.
                    271:        sleep `sysctl -n net.inet6.ip6.dad_count`
                    272:        sleep 1
1.56      itojun    273: fi
                    274:
1.14      deraadt   275: # /etc/mygate, if it exists, contains the name of my gateway host
                    276: # that name must be in /etc/hosts.
1.42      marc      277: if [ -f /etc/mygate ]; then
1.38      deraadt   278:        route -n add -host default `cat /etc/mygate`
1.14      deraadt   279: fi
1.44      deraadt   280:
1.48      niklas    281: # Multicast routing.
                    282: #
                    283: # The routing to the 224.0.0.0/4 net is setup according to these rules:
                    284: # multicast_host       multicast_router        route           comment
                    285: # NO                   NO                      -reject         no multicast
                    286: # NO                   YES                     none installed  daemon will run
                    287: # YES/interface                NO                      -interface      YES=def. iface
                    288: #         Any other combination                -reject         config error
                    289: case "$multicast_host:$multicast_router" in
                    290: NO:NO)
1.82      hugh      291:        route -n add -net 224.0.0.0/4 -interface 127.0.0.1 -reject > /dev/null
1.67      deraadt   292:        ;;
1.48      niklas    293: NO:YES)
                    294:        ;;
                    295: *:NO)
                    296:        set `if [ $multicast_host = YES ]; then
1.80      angelos   297:                ed -s '!route -n show -inet' <<EOF
1.48      niklas    298: /^default/p
                    299: EOF
                    300:        else
                    301:                ed -s "!ifconfig $multicast_host" <<EOF
                    302: /^     inet /p
                    303: EOF
                    304:        fi`
1.67      deraadt   305:        route -n add -net 224.0.0.0/4 -interface $2 > /dev/null
                    306:        ;;
1.48      niklas    307: *:*)
                    308:        echo 'config error, multicasting disabled until rc.conf is fixed'
1.67      deraadt   309:        route -n add -net 224.0.0.0/4 -interface 127.0.0.1 -reject > /dev/null
                    310:        ;;
1.48      niklas    311: esac
1.83      miod      312:
                    313: # Configure all the gif and gre interfaces which we know about.
                    314: # They were delayed because they require the routes to be set.
                    315: for hn in /etc/hostname.*; do
                    316:        # Strip off /etc/hostname. prefix
                    317:        if=${hn#/etc/hostname.}
1.85    ! todd      318:        test "$if" = "*" && continue
1.83      miod      319:
                    320:        case $if in
                    321:        "gif"*|"gre"*)
                    322:                ifstart $if
                    323:                ;;
                    324:        *)
                    325:                # Regular interfaces have already been configured.
                    326:                continue
                    327:                ;;
                    328:        esac
                    329: done
                    330:
                    331: # Configure all the bridges.
                    332: for bn in /etc/bridgename.*; do
                    333:        # Strip off /etc/bridgename. prefix
                    334:        if=${bn#/etc/bridgename.}
1.85    ! todd      335:        test "$if" = "*" && continue
1.83      miod      336:
                    337:        bridgestart $if
                    338: done