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

Annotation of src/etc/netstart, Revision 1.72

1.1       deraadt     1: #!/bin/sh -
                      2: #
1.72    ! todd        3: #      $OpenBSD: netstart,v 1.71 2000/06/18 19:02:24 todd 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.1       deraadt    18:
                     19: # /etc/myname contains my symbolic name
                     20: #
                     21: hostname=`cat /etc/myname`
                     22: hostname $hostname
                     23: if [ -f /etc/defaultdomain ]; then
                     24:        domainname `cat /etc/defaultdomain`
1.4       dm         25: fi
1.30      deraadt    26:
                     27: # pick up option configuration
                     28: . /etc/rc.conf
1.4       dm         29:
                     30: # Configure the IP filter before configuring network interfaces
                     31: if [ X"${ipfilter}" = X"YES" -a -f "${ipfilter_rules}" ]; then
                     32:        echo 'configuring IP filter'
1.68      millert    33:        ipf -Fa -f ${ipfilter_rules}
1.4       dm         34: else
                     35:        ipfilter=NO
1.1       deraadt    36: fi
1.17      kstailey   37:
1.24      kstailey   38: # set the address for the loopback interface
1.52      itojun     39: # it will also initialize IPv6 address for lo0 (::1 and others).
1.24      kstailey   40: ifconfig lo0 inet localhost
1.1       deraadt    41:
1.24      kstailey   42: # use loopback, not the wire
1.67      deraadt    43: route -n add -host $hostname localhost > /dev/null
                     44: route -n add -net 127 127.0.0.1 -reject > /dev/null
1.24      kstailey   45:
1.54      itojun     46: if ifconfig lo0 inet6 >/dev/null 2>&1; then
                     47:        # IPv6 configurations.
                     48:        ip6kernel=YES
                     49:
1.66      itojun     50:        # disallow link-local unicast dest without outgoing scope identifiers.
1.67      deraadt    51:        route add -inet6 fe80:: -prefixlen 10 ::1 -reject > /dev/null
1.66      itojun     52:
                     53:        # disallow site-local unicast dest without outgoing scope identifiers..
                     54:        # If you configure site-locals without scope id (it is permissible
                     55:        # config for routers that are not on scope boundary), you may want
                     56:        # to comment the line out.
1.67      deraadt    57:        route add -inet6 fec0:: -prefixlen 10 ::1 -reject > /dev/null
1.66      itojun     58:
1.54      itojun     59:        # disallow "internal" addresses to appear on the wire.
1.67      deraadt    60:        route add -inet6 ::ffff:0.0.0.0 -prefixlen 96 ::1 -reject > /dev/null
1.66      itojun     61:
                     62:        # disallow packets to malicious IPv4 compatible prefix.
1.67      deraadt    63:        route add -inet6 ::224.0.0.0 -prefixlen 100 ::1 -reject > /dev/null
                     64:        route add -inet6 ::127.0.0.0 -prefixlen 104 ::1 -reject > /dev/null
                     65:        route add -inet6 ::0.0.0.0 -prefixlen 104 ::1 -reject > /dev/null
                     66:        route add -inet6 ::255.0.0.0 -prefixlen 104 ::1 -reject > /dev/null
1.66      itojun     67:
                     68:        # disallow packets to malicious 6to4 prefix.
1.67      deraadt    69:        route add -inet6 2002:e000:: -prefixlen 20 ::1 -reject > /dev/null
                     70:        route add -inet6 2002:7f00:: -prefixlen 24 ::1 -reject > /dev/null
                     71:        route add -inet6 2002:0000:: -prefixlen 24 ::1 -reject > /dev/null
                     72:        route add -inet6 2002:ff00:: -prefixlen 24 ::1 -reject > /dev/null
1.66      itojun     73:
                     74:        # Completely disallow packets to IPv4 compatible prefix.
                     75:        # This may conflict with RFC1933 under following circumstances:
                     76:        # (1) An IPv6-only KAME node tries to originate packets to IPv4
                     77:        #     comatible destination.  The KAME node has no IPv4 compatible
                     78:        #     support.  Under RFC1933, it should transmit native IPv6
                     79:        #     packets toward IPv4 compatible destination, hoping it would
                     80:        #     reach a router that forwards the packet toward auto-tunnel
                     81:        #     interface.
                     82:        # (2) An IPv6-only node originates a packet to IPv4 compatible
                     83:        #     destination.  A KAME node is acting as an IPv6 router, and
                     84:        #     asked to forward it.
                     85:        # Due to rare use of IPv4 compatible address, and security issues
                     86:        # with it, we disable it by default.
1.67      deraadt    87:        route add -inet6 ::0.0.0.0 -prefixlen 96 ::1 -reject > /dev/null
1.56      itojun     88:
                     89:        rtsolif=""
1.54      itojun     90: else
                     91:        ip6kernel=NO
                     92: fi
                     93:
1.24      kstailey   94: # configure all of the non-loopback interfaces which we know about.
1.50      deraadt    95: # refer to hostname.if(5) and bridgename.if(5)
1.47      niklas     96: for hn in /etc/hostname.*; do
                     97:     # Strip off /etc/hostname. prefix
                     98:     if=${hn#/etc/hostname.}
                     99:
                    100:     # Interface names must be alphanumeric only.  We check to avoid
                    101:     # configuring backup or temp files, and to catch the "*" case.
                    102:     if ! isalphanumeric "$if"; then
1.55      todd      103:        continue
1.49      angelos   104:     fi
1.50      deraadt   105:     ifconfig $if > /dev/null 2>&1
1.49      angelos   106:     if [ "$?" != "0" ]; then
                    107:        continue
1.47      niklas    108:     fi
                    109:
                    110:     # Now parse the hostname.* file
1.55      todd      111:     while :; do
                    112:        if [ "$cmd2" ]; then
                    113:            # we are carrying over from the 'read dt dtaddr' last time
                    114:            set -- $cmd2
                    115:            af="$1" name="$2" mask="$3" bcaddr="$4" ext1="$5" ext2="$6"
                    116:            cmd2=
                    117:        else
                    118:            # read the next line or exit the while loop
                    119:            read af name mask bcaddr ext1 ext2 || break
                    120:        fi
1.63      todd      121:        # $af can be "dhcp", "up", "rtsol", an address family, commands, or
                    122:        # a comment.
1.47      niklas    123:        case "$af" in
1.63      todd      124:        "#"*) # skip comments
                    125:            continue
                    126:            ;;
                    127:        "!"*) # parse commands
                    128:            cmd="${af#*!} ${name} ${mask} ${bcaddr} ${ext1} ${ext2}"
                    129:            ;;
1.47      niklas    130:        "bridge")
1.50      deraadt   131:            cmd="echo ${hn}: bridges now supported via bridgename.* files"
                    132:            ;;
1.47      niklas    133:        "dhcp")
1.70      todd      134:            [ "$name" = "NONE" ] && name=
                    135:            [ "$mask" = "NONE" ] && mask=
                    136:            [ "$bcaddr" = "NONE" ] && bcaddr=
1.55      todd      137:            ifconfig $if $name $mask $bcaddr $ext1 $ext2 down
1.50      deraadt   138:            cmd="dhclient $if"
1.47      niklas    139:            ;;
1.56      itojun    140:        "rtsol")
1.62      deraadt   141:            ifconfig $if $name $mask $bcaddr $ext1 $ext2 up
1.56      itojun    142:            rtsolif="$rtsolif $if"
1.59      todd      143:            cmd=
1.56      itojun    144:            ;;
1.47      niklas    145:        "up")
                    146:            # The only one of these guaranteed to be set is $if
1.50      deraadt   147:            # the remaining ones exist so that media controls work
1.55      todd      148:            cmd="ifconfig $if $name $mask $bcaddr $ext1 $ext2 up"
                    149:            ;;
                    150:        *)
                    151:            read dt dtaddr
                    152:            if [ "$name"  = "alias" ]; then
                    153:                # perform a 'shift' of sorts
                    154:                alias=$name
                    155:                name=$mask
                    156:                mask=$bcaddr
                    157:                bcaddr=$ext1
                    158:                ext1=$ext2
                    159:                ext2=
1.72    ! todd      160:            else
        !           161:                alias=
1.47      niklas    162:            fi
1.55      todd      163:            cmd="ifconfig $if $af $alias $name "
1.64      todd      164:            case "$dt" in
1.55      todd      165:            dest)
                    166:                cmd="$cmd $dtaddr"
                    167:                ;;
1.64      todd      168:            [a-z!]*)
1.55      todd      169:                cmd2="$dt $dtaddr"
                    170:                ;;
1.60      itojun    171:            esac
                    172:            if [ ! -n "$name" ]; then
1.55      todd      173:                    echo "/etc/hostname.$if: invalid network configuration file"
                    174:                return
1.60      itojun    175:            fi
                    176:            case $af in
                    177:            inet)
1.55      todd      178:                [ "$mask" ] && cmd="$cmd netmask $mask"
                    179:                if [ "$bcaddr" -a "X$bcaddr" != "XNONE" ]; then
                    180:                    cmd="$cmd broadcast $bcaddr"
                    181:                fi
                    182:                [ "$alias" ] && rtcmd="; route -n add -host $name 127.0.0.1"
1.61      itojun    183:                ;;
1.60      itojun    184:            inet6) [ "$mask" ] && cmd="$cmd prefixlen $mask"
1.55      todd      185:                cmd="$cmd $bcaddr"
                    186:                ;;
1.60      itojun    187:            *) cmd="$cmd $mask $bcaddr"
                    188:            esac
                    189:            cmd="$cmd $ext1 $ext2$rtcmd" rtcmd=
                    190:            ;;
1.47      niklas    191:        esac
                    192:        eval "$cmd"
1.55      todd      193:     done < /etc/hostname.$if
1.50      deraadt   194: done
1.56      itojun    195:
                    196: if [ "$ip6kernel" = "YES" -a "x$rtsolif" != "x" ]; then
                    197:        fw=`sysctl -n net.inet6.ip6.forwarding`
                    198:        ra=`sysctl -n net.inet6.ip6.accept_rtadv`
                    199:        if [ "x$fw" = "x0" -a "x$ra" = "x1" ]; then
1.57      itojun    200:                echo "IPv6 autoconf:$rtsolif"
1.56      itojun    201:                rtsol $rtsolif
                    202:        else
                    203:                echo "WARNING: inconsistent config - check /etc/sysctl.conf for IPv6 autoconf"
                    204:        fi
1.57      itojun    205: fi
                    206: if [ "$ip6kernel" = "YES" ]; then
                    207:        # this is to make sure DAD is completed before going further.
                    208:        sleep `sysctl -n net.inet6.ip6.dad_count`
                    209:        sleep 1
1.56      itojun    210: fi
                    211:
1.50      deraadt   212: for bn in /etc/bridgename.*; do
                    213:     # Strip off /etc/bridgename. prefix
                    214:     if=${bn#/etc/bridgename.}
                    215:
                    216:     # Interface names must be alphanumeric only.  We check to avoid
                    217:     # configuring backup or temp files, and to catch the "*" case.
                    218:     if ! isalphanumeric "$if"; then
                    219:         continue
                    220:     fi
                    221:     brconfig $if > /dev/null 2>&1
                    222:     if [ "$?" != "0" ]; then
                    223:        continue
                    224:     fi
                    225:
                    226:     # Now parse the bridgename.* file
                    227:     {
                    228:        # All lines are run as brconfig(8) commands.
                    229:        while read line ; do
1.51      deraadt   230:            line=${line%%#*}            # strip comments
                    231:            test -z "$line" && continue
1.50      deraadt   232:            brconfig $if $line
                    233:        done
                    234:     } < /etc/bridgename.$if
1.47      niklas    235: done
1.1       deraadt   236:
1.14      deraadt   237: # /etc/mygate, if it exists, contains the name of my gateway host
                    238: # that name must be in /etc/hosts.
1.42      marc      239: if [ -f /etc/mygate ]; then
1.38      deraadt   240:        route -n add -host default `cat /etc/mygate`
1.14      deraadt   241: fi
1.44      deraadt   242:
1.48      niklas    243: # Multicast routing.
                    244: #
                    245: # The routing to the 224.0.0.0/4 net is setup according to these rules:
                    246: # multicast_host       multicast_router        route           comment
                    247: # NO                   NO                      -reject         no multicast
                    248: # NO                   YES                     none installed  daemon will run
                    249: # YES/interface                NO                      -interface      YES=def. iface
                    250: #         Any other combination                -reject         config error
                    251: case "$multicast_host:$multicast_router" in
                    252: NO:NO)
1.67      deraadt   253:        route -n add -net 224.0.0.0/4 -interface 127.0.0.1 -reject> /dev/null
                    254:        ;;
1.48      niklas    255: NO:YES)
                    256:        ;;
                    257: *:NO)
                    258:        set `if [ $multicast_host = YES ]; then
                    259:                ed -s '!route -n show' <<EOF
                    260: /^default/p
                    261: EOF
                    262:        else
                    263:                ed -s "!ifconfig $multicast_host" <<EOF
                    264: /^     inet /p
                    265: EOF
                    266:        fi`
1.67      deraadt   267:        route -n add -net 224.0.0.0/4 -interface $2 > /dev/null
                    268:        ;;
1.48      niklas    269: *:*)
                    270:        echo 'config error, multicasting disabled until rc.conf is fixed'
1.67      deraadt   271:        route -n add -net 224.0.0.0/4 -interface 127.0.0.1 -reject > /dev/null
                    272:        ;;
1.48      niklas    273: esac
1.53      itojun    274:
1.44      deraadt   275: # Configure NAT after configuring network interfaces
                    276: if [ "${ipnat}" = "YES" -a "${ipfilter}" = "YES" -a -f "${ipnat_rules}" ]; then
                    277:        echo 'configuring NAT'
                    278:        ipnat -CF -f ${ipnat_rules}
                    279: else
                    280:        ipnat=NO
                    281: fi