Source code pulled from OpenBSD for OpenNTPD. The place to contribute to this code is via the OpenBSD CVS tree.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

388 lines
9.8 KiB

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