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

Annotation of src/etc/netstart, Revision 1.112

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