1df930be7Sderaadt#!/bin/sh - 2df930be7Sderaadt# 3*19d144d8Stodd# $OpenBSD: netstart,v 1.106 2005/09/28 17:40:30 todd Exp $ 48fc5e153Smillert 58fc5e153Smillert# Strip comments (and leading/trailing whitespace if IFS is set) 68fc5e153Smillert# from a file and spew to stdout 78fc5e153Smillertstripcom() { 88fc5e153Smillert local _file="$1" 98fc5e153Smillert local _line 108fc5e153Smillert 118fc5e153Smillert { 128fc5e153Smillert while read _line ; do 138fc5e153Smillert _line=${_line%%#*} # strip comments 148fc5e153Smillert test -z "$_line" && continue 158fc5e153Smillert echo $_line 168fc5e153Smillert done 178fc5e153Smillert } < $_file 188fc5e153Smillert} 1904e0ac27Smillert 2004e0ac27Smillert# Returns true if $1 contains only alphanumerics 2104e0ac27Smillertisalphanumeric() { 2204e0ac27Smillert local _n 2304e0ac27Smillert _n=$1 2404e0ac27Smillert while [ ${#_n} != 0 ]; do 2504e0ac27Smillert case $_n in 2604e0ac27Smillert [A-Za-z0-9]*) ;; 2704e0ac27Smillert *) return 1;; 2804e0ac27Smillert esac 2904e0ac27Smillert _n=${_n#?} 3004e0ac27Smillert done 3104e0ac27Smillert return 0 3204e0ac27Smillert} 33df930be7Sderaadt 34dfc209d0Smiod# Start the $1 interface 35dfc209d0Smiodifstart() { 36f5319bdcSderaadt if=$1 37dfc209d0Smiod # Interface names must be alphanumeric only. We check to avoid 38dfc209d0Smiod # configuring backup or temp files, and to catch the "*" case. 39f5319bdcSderaadt if ! isalphanumeric "$if"; then 40dfc209d0Smiod return 41dfc209d0Smiod fi 42dfc209d0Smiod 43f5319bdcSderaadt ifconfig $if > /dev/null 2>&1 44dfc209d0Smiod if [ "$?" != "0" ]; then 45e087dc57Smarkus # Try to create interface if it does not exist 46e087dc57Smarkus ifconfig $if create > /dev/null 2>&1 47e087dc57Smarkus if [ "$?" != "0" ]; then 48dfc209d0Smiod return 49dfc209d0Smiod fi 50e087dc57Smarkus fi 51dfc209d0Smiod 52dfc209d0Smiod # Now parse the hostname.* file 53dfc209d0Smiod while :; do 54dfc209d0Smiod if [ "$cmd2" ]; then 55dfc209d0Smiod # We are carrying over from the 'read dt dtaddr' 56dfc209d0Smiod # last time. 57dfc209d0Smiod set -- $cmd2 58dfc209d0Smiod af="$1" name="$2" mask="$3" bcaddr="$4" ext1="$5" cmd2= 59dfc209d0Smiod # Make sure and get any remaining args in ext2, 60dfc209d0Smiod # like the read below 61dfc209d0Smiod i=1 62dfc209d0Smiod while [ i -lt 6 -a -n "$1" ]; do shift; let i=i+1; done 63dfc209d0Smiod ext2="$@" 64dfc209d0Smiod else 65dfc209d0Smiod # Read the next line or exit the while loop. 66dfc209d0Smiod read af name mask bcaddr ext1 ext2 || break 67dfc209d0Smiod fi 68dfc209d0Smiod # $af can be "dhcp", "up", "rtsol", an address family, 69dfc209d0Smiod # commands, or a comment. 70dfc209d0Smiod case "$af" in 71dfc209d0Smiod "#"*|"") # skip comments and empty lines 72dfc209d0Smiod continue 73dfc209d0Smiod ;; 74dfc209d0Smiod "!"*) # parse commands 75dfc209d0Smiod cmd="${af#*!} ${name} ${mask} ${bcaddr} ${ext1} ${ext2}" 76dfc209d0Smiod ;; 77dfc209d0Smiod "bridge") 78f5319bdcSderaadt cmd="echo /etc/hostname.$if: bridges now supported via bridgename.* files" 79dfc209d0Smiod ;; 80dfc209d0Smiod "dhcp") 81dfc209d0Smiod [ "$name" = "NONE" ] && name= 82dfc209d0Smiod [ "$mask" = "NONE" ] && mask= 83dfc209d0Smiod [ "$bcaddr" = "NONE" ] && bcaddr= 84*19d144d8Stodd cmd="ifconfig $if $name $mask $bcaddr $ext1 $ext2 down" 85*19d144d8Stodd cmd="$cmd;dhclient $if" 8649664b60Sderaadt setgateway=N 87dfc209d0Smiod ;; 88dfc209d0Smiod "rtsol") 89f5319bdcSderaadt rtsolif="$rtsolif $if" 90*19d144d8Stodd cmd="ifconfig $if $name $mask $bcaddr $ext1 $ext2 up" 91dfc209d0Smiod ;; 92dfc209d0Smiod "up") 93f5319bdcSderaadt # The only one of these guaranteed to be set is $if. 94dfc209d0Smiod # The remaining ones exist so that media controls work. 95f5319bdcSderaadt cmd="ifconfig $if $name $mask $bcaddr $ext1 $ext2 up" 96dfc209d0Smiod ;; 97dfc209d0Smiod *) 98dfc209d0Smiod read dt dtaddr 99dfc209d0Smiod if [ "$name" = "alias" ]; then 100dfc209d0Smiod # perform a 'shift' of sorts 101dfc209d0Smiod alias=$name 102dfc209d0Smiod name=$mask 103dfc209d0Smiod mask=$bcaddr 104dfc209d0Smiod bcaddr=$ext1 105dfc209d0Smiod ext1=$ext2 106dfc209d0Smiod ext2= 107dfc209d0Smiod else 108dfc209d0Smiod alias= 109dfc209d0Smiod fi 110f5319bdcSderaadt cmd="ifconfig $if $af $alias $name " 111dfc209d0Smiod case "$dt" in 112dfc209d0Smiod dest) 113dfc209d0Smiod cmd="$cmd $dtaddr" 114dfc209d0Smiod ;; 115dfc209d0Smiod [a-z!]*) 116dfc209d0Smiod cmd2="$dt $dtaddr" 117dfc209d0Smiod ;; 118dfc209d0Smiod esac 119dfc209d0Smiod if [ ! -n "$name" ]; then 120f5319bdcSderaadt echo "/etc/hostname.$if: invalid network configuration file" 121dfc209d0Smiod return 122dfc209d0Smiod fi 123dfc209d0Smiod case $af in 124dfc209d0Smiod inet) 125dfc209d0Smiod [ "$mask" ] && cmd="$cmd netmask $mask" 126dfc209d0Smiod if [ "$bcaddr" -a "X$bcaddr" != "XNONE" ]; then 127dfc209d0Smiod cmd="$cmd broadcast $bcaddr" 128dfc209d0Smiod fi 129f4b4b73bSderaadt [ "$alias" ] && rtcmd=";route -qn add -host $name 127.0.0.1" 130dfc209d0Smiod ;; 131dfc209d0Smiod inet6) [ "$mask" ] && cmd="$cmd prefixlen $mask" 132dfc209d0Smiod cmd="$cmd $bcaddr" 133dfc209d0Smiod ;; 134dfc209d0Smiod *) 135dfc209d0Smiod cmd="$cmd $mask $bcaddr" 136dfc209d0Smiod ;; 137dfc209d0Smiod esac 138dfc209d0Smiod cmd="$cmd $ext1 $ext2$rtcmd" rtcmd= 139dfc209d0Smiod ;; 140dfc209d0Smiod esac 141dfc209d0Smiod eval "$cmd" 142f5319bdcSderaadt done < /etc/hostname.$if 143dfc209d0Smiod} 144dfc209d0Smiod 1459ac6b043Stodd# Start multiple: 1469ac6b043Stodd# start "$1" interfaces in order or all interfaces if empty 1479ac6b043Stodd# don't start "$2" interfaces 1489ac6b043Stoddifmstart() { 1499ac6b043Stodd for sif in ${1:-ALL}; do 1509ac6b043Stodd for hn in /etc/hostname.*; do 1519ac6b043Stodd # Strip off /etc/hostname. prefix 1529ac6b043Stodd if=${hn#/etc/hostname.} 1539ac6b043Stodd test "$if" = "*" && continue 1549ac6b043Stodd 1559ac6b043Stodd # Skip unwanted ifs 1569ac6b043Stodd s="" 1579ac6b043Stodd for xf in $2; do 1589ac6b043Stodd test "$xf" = "${if%%[0-9]*}" && s="1" && break 1599ac6b043Stodd done 1609ac6b043Stodd test "$s" = "1" && continue 1619ac6b043Stodd 1629ac6b043Stodd # Start wanted ifs 1639ac6b043Stodd test "$sif" = "ALL" -o \ 1649ac6b043Stodd "$sif" = "${if%%[0-9]*}" \ 1659ac6b043Stodd && ifstart $if 1669ac6b043Stodd done 1679ac6b043Stodd done 1689ac6b043Stodd} 1699ac6b043Stodd 170dfc209d0Smiod# Start the $1 bridge 171dfc209d0Smiodbridgestart() { 172dfc209d0Smiod # Interface names must be alphanumeric only. We check to avoid 173dfc209d0Smiod # configuring backup or temp files, and to catch the "*" case. 174dfc209d0Smiod if ! isalphanumeric "$1"; then 175dfc209d0Smiod return 176dfc209d0Smiod fi 177dfc209d0Smiod brconfig $1 > /dev/null 2>&1 178dfc209d0Smiod if [ "$?" != "0" ]; then 179e5eec468Smillert # Try to create interface if it does not exist 180e5eec468Smillert ifconfig $if create > /dev/null 2>&1 181e5eec468Smillert if [ "$?" != "0" ]; then 182dfc209d0Smiod return 183dfc209d0Smiod fi 184e5eec468Smillert fi 185dfc209d0Smiod 186dfc209d0Smiod # Now parse the bridgename.* file 187dfc209d0Smiod # All lines are run as brconfig(8) commands. 188dfc209d0Smiod while read line ; do 189dfc209d0Smiod line=${line%%#*} # strip comments 190dfc209d0Smiod test -z "$line" && continue 191dfc209d0Smiod case "$line" in 192dfc209d0Smiod "!"*) 193dfc209d0Smiod cmd="${line#*!}" 194dfc209d0Smiod ;; 195dfc209d0Smiod *) 196dfc209d0Smiod cmd="brconfig $1 $line" 197dfc209d0Smiod ;; 198dfc209d0Smiod esac 199dfc209d0Smiod eval "$cmd" 200dfc209d0Smiod done < /etc/bridgename.$1 201dfc209d0Smiod} 202dfc209d0Smiod 2030dc37902Sangelos# Re-read /etc/rc.conf 2040dc37902Sangelos. /etc/rc.conf 2050dc37902Sangelos 206dfc209d0Smiod# If we were invoked with a list of interface names, just reconfigure these 207dfc209d0Smiod# interfaces (or bridges) and return. 208dfc209d0Smiodif [ $1x = autobootx ]; then 209dfc209d0Smiod shift 210dfc209d0Smiodfi 211dfc209d0Smiodif [ $# -gt 0 ]; then 212dfc209d0Smiod while [ $# -gt 0 ]; do 213dfc209d0Smiod if [ -f /etc/bridgename.$1 ]; then 214dfc209d0Smiod bridgestart $1 215dfc209d0Smiod else 216dfc209d0Smiod ifstart $1 217dfc209d0Smiod fi 218dfc209d0Smiod shift 219dfc209d0Smiod done 220dfc209d0Smiod return 221dfc209d0Smiodfi 222dfc209d0Smiod 223dfc209d0Smiod# Otherwise, process with the complete network initialization. 224dfc209d0Smiod 225df930be7Sderaadt# /etc/myname contains my symbolic name 22637bbdc83Shenningif [ -f /etc/myname ]; then 2273de81825Smillert hostname=`stripcom /etc/myname` 228df930be7Sderaadt hostname $hostname 22937bbdc83Shenningelse 23037bbdc83Shenning hostname=`hostname` 23137bbdc83Shenningfi 23237bbdc83Shenning 233df930be7Sderaadtif [ -f /etc/defaultdomain ]; then 2343de81825Smillert domainname `stripcom /etc/defaultdomain` 235df930be7Sderaadtfi 236df930be7Sderaadt 237bb101bd8Sderaadt# Set the address for the loopback interface. Bringing the 238bb101bd8Sderaadt# interface up, automatically invokes the IPv6 address ::1) 2398f8fdbefSderaadtifconfig lo0 inet 127.0.0.1 24098c28033Skstailey 2413d8fed7cSitojunif ifconfig lo0 inet6 >/dev/null 2>&1; then 2423d8fed7cSitojun # IPv6 configurations. 2433d8fed7cSitojun ip6kernel=YES 2443d8fed7cSitojun 245dfc209d0Smiod # Disallow link-local unicast dest without outgoing scope identifiers. 24603056e2eSderaadt route -qn add -inet6 fe80:: -prefixlen 10 ::1 -reject > /dev/null 24792aceabbSitojun 248dfc209d0Smiod # Disallow site-local unicast dest without outgoing scope identifiers. 24992aceabbSitojun # If you configure site-locals without scope id (it is permissible 25092aceabbSitojun # config for routers that are not on scope boundary), you may want 25192aceabbSitojun # to comment the line out. 25203056e2eSderaadt route -qn add -inet6 fec0:: -prefixlen 10 ::1 -reject > /dev/null 25392aceabbSitojun 254dfc209d0Smiod # Disallow "internal" addresses to appear on the wire. 25503056e2eSderaadt route -qn add -inet6 ::ffff:0.0.0.0 -prefixlen 96 ::1 -reject > /dev/null 25692aceabbSitojun 257dfc209d0Smiod # Disallow packets to malicious IPv4 compatible prefix. 25803056e2eSderaadt route -qn add -inet6 ::224.0.0.0 -prefixlen 100 ::1 -reject > /dev/null 25903056e2eSderaadt route -qn add -inet6 ::127.0.0.0 -prefixlen 104 ::1 -reject > /dev/null 26003056e2eSderaadt route -qn add -inet6 ::0.0.0.0 -prefixlen 104 ::1 -reject > /dev/null 26103056e2eSderaadt route -qn add -inet6 ::255.0.0.0 -prefixlen 104 ::1 -reject > /dev/null 26292aceabbSitojun 263dfc209d0Smiod # Disallow packets to malicious 6to4 prefix. 26403056e2eSderaadt route -qn add -inet6 2002:e000:: -prefixlen 20 ::1 -reject > /dev/null 26503056e2eSderaadt route -qn add -inet6 2002:7f00:: -prefixlen 24 ::1 -reject > /dev/null 26603056e2eSderaadt route -qn add -inet6 2002:0000:: -prefixlen 24 ::1 -reject > /dev/null 26703056e2eSderaadt route -qn add -inet6 2002:ff00:: -prefixlen 24 ::1 -reject > /dev/null 26892aceabbSitojun 26992aceabbSitojun # Completely disallow packets to IPv4 compatible prefix. 27092aceabbSitojun # This may conflict with RFC1933 under following circumstances: 27192aceabbSitojun # (1) An IPv6-only KAME node tries to originate packets to IPv4 2725e268fadSderaadt # compatible destination. The KAME node has no IPv4 compatible 27392aceabbSitojun # support. Under RFC1933, it should transmit native IPv6 27492aceabbSitojun # packets toward IPv4 compatible destination, hoping it would 27592aceabbSitojun # reach a router that forwards the packet toward auto-tunnel 27692aceabbSitojun # interface. 2775e268fadSderaadt # (2) An IPv6-only node originates a packet to an IPv4 compatible 27892aceabbSitojun # destination. A KAME node is acting as an IPv6 router, and 27992aceabbSitojun # asked to forward it. 2805e268fadSderaadt # Due to rare use of IPv4 compatible addresses, and security issues 28192aceabbSitojun # with it, we disable it by default. 28203056e2eSderaadt route -qn add -inet6 ::0.0.0.0 -prefixlen 96 ::1 -reject > /dev/null 28382c17b75Sitojun 28482c17b75Sitojun rtsolif="" 2853d8fed7cSitojunelse 2863d8fed7cSitojun ip6kernel=NO 2873d8fed7cSitojunfi 2883d8fed7cSitojun 289df930be7Sderaadt 2909ac6b043Stodd# Configure all the non-loopback interfaces which we know about, but 2919ac6b043Stodd# do not start interfaces which must be delayed. 2929ac6b043Stodd# Refer to hostname.if(5) and bridgename.if(5) 2939ac6b043Stoddifmstart "" "carp gif gre pfsync pppoe" 29482c17b75Sitojun 29582c17b75Sitojunif [ "$ip6kernel" = "YES" -a "x$rtsolif" != "x" ]; then 29682c17b75Sitojun fw=`sysctl -n net.inet6.ip6.forwarding` 29782c17b75Sitojun ra=`sysctl -n net.inet6.ip6.accept_rtadv` 29882c17b75Sitojun if [ "x$fw" = "x0" -a "x$ra" = "x1" ]; then 299d56849f2Sitojun echo "IPv6 autoconf:$rtsolif" 30082c17b75Sitojun rtsol $rtsolif 30182c17b75Sitojun else 30282c17b75Sitojun echo "WARNING: inconsistent config - check /etc/sysctl.conf for IPv6 autoconf" 30382c17b75Sitojun fi 30482c17b75Sitojunfi 305d56849f2Sitojunif [ "$ip6kernel" = "YES" ]; then 306d56849f2Sitojun # this is to make sure DAD is completed before going further. 307d56849f2Sitojun sleep `sysctl -n net.inet6.ip6.dad_count` 308d56849f2Sitojunfi 30982c17b75Sitojun 31072aeac0bSmcbride# The pfsync interface needs to come up before carp. 31172aeac0bSmcbride# Configure all the carp interfaces which we know about. 31272aeac0bSmcbride# They must come up after pfsync but before default route. 3139ac6b043Stoddifmstart "pfsync carp" 31472aeac0bSmcbride 315d747464dSderaadt# /etc/mygate, if it exists, contains the name of my gateway host 316d747464dSderaadt# that name must be in /etc/hosts. 317ad1e6353Sdjmif [ "X${setgateway}" != X"N" -a -f /etc/mygate ]; then 3182fcef345Sderaadt route -qn delete default > /dev/null 2>&1 3193de81825Smillert route -qn add -host default `stripcom /etc/mygate` 320a4f0e6c1Sdownsjfi 321cf3860a5Sderaadt 322745634aaSniklas# Multicast routing. 323745634aaSniklas# 324745634aaSniklas# The routing to the 224.0.0.0/4 net is setup according to these rules: 325745634aaSniklas# multicast_host multicast_router route comment 326745634aaSniklas# NO NO -reject no multicast 327745634aaSniklas# NO YES none installed daemon will run 328745634aaSniklas# YES/interface NO -interface YES=def. iface 329745634aaSniklas# Any other combination -reject config error 330745634aaSniklascase "$multicast_host:$multicast_router" in 331745634aaSniklasNO:NO) 332f4b4b73bSderaadt route -qn add -net 224.0.0.0/4 -interface 127.0.0.1 -reject > /dev/null 333fe32e9eaSderaadt ;; 334745634aaSniklasNO:YES) 335745634aaSniklas ;; 336745634aaSniklas*:NO) 337745634aaSniklas set `if [ $multicast_host = YES ]; then 338f4b4b73bSderaadt ed -s '!route -qn show -inet' <<EOF 339745634aaSniklas/^default/p 340745634aaSniklasEOF 341745634aaSniklas else 342745634aaSniklas ed -s "!ifconfig $multicast_host" <<EOF 343745634aaSniklas/^ inet /p 344745634aaSniklasEOF 345745634aaSniklas fi` 346f4b4b73bSderaadt route -qn add -net 224.0.0.0/4 -interface $2 > /dev/null 347fe32e9eaSderaadt ;; 348745634aaSniklas*:*) 349745634aaSniklas echo 'config error, multicasting disabled until rc.conf is fixed' 350f4b4b73bSderaadt route -qn add -net 224.0.0.0/4 -interface 127.0.0.1 -reject > /dev/null 351fe32e9eaSderaadt ;; 352745634aaSniklasesac 353dfc209d0Smiod 354dfc209d0Smiod 3559ac6b043Stodd# Configure PPPoE, GIF, GRE interfaces, delayed because they require routes 3569ac6b043Stodd# to be set. PPPoE must be first, as GIF and GRE may depend on it. 3579ac6b043Stoddifmstart "pppoe gif gre" 358dfc209d0Smiod 35925d2fb80Sitojun# reject 127/8 other than 127.0.0.1 3608f8fdbefSderaadtroute -qn add -net 127 127.0.0.1 -reject > /dev/null 3618f8fdbefSderaadt 362dfc209d0Smiod# Configure all the bridges. 363dfc209d0Smiodfor bn in /etc/bridgename.*; do 364dfc209d0Smiod # Strip off /etc/bridgename. prefix 365dfc209d0Smiod if=${bn#/etc/bridgename.} 3667fd32e5eStodd test "$if" = "*" && continue 367dfc209d0Smiod 368dfc209d0Smiod bridgestart $if 369dfc209d0Smioddone 370