network 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. #! /bin/sh
  2. #
  3. # network 1.87 2004/07/17 04:48:37 (David Hinds)
  4. #
  5. # Initialize or shutdown a PCMCIA ethernet adapter
  6. #
  7. # This script should be invoked with two arguments. The first is the
  8. # action to be taken, either "start", "stop", or "restart". The
  9. # second is the network interface name.
  10. #
  11. # The script passes an extended device address to 'network.opts' in
  12. # the ADDRESS variable, to retrieve device-specific configuration
  13. # options. The address format is "scheme,socket,instance,hwaddr"
  14. # where "scheme" is the current PCMCIA device configuration scheme,
  15. # "socket" is the socket number, "instance" is used to number multiple
  16. # interfaces in a single socket, and "hwaddr" is the card's hardware
  17. # ethernet address.
  18. #
  19. if [ -r ./shared ] ; then . ./shared ; else . /etc/pcmcia/shared ; fi
  20. # Get device attributes
  21. get_info $DEVICE
  22. if [ -d /var/lib/pcmcia ] ; then
  23. NAMEFILE=/var/lib/pcmcia/nameif.${SOCKET}.${INSTANCE}
  24. else
  25. NAMEFILE=/var/run/nameif.${SOCKET}.${INSTANCE}
  26. fi
  27. if [ -r "$NAMEFILE" -a "$ACTION" != "start" ] ; then
  28. DEVICE=`cat $NAMEFILE`
  29. fi
  30. HWADDR=`/sbin/ifconfig $DEVICE | sed -ne 's/.*\(..:..:..:..:..:..\).*/\1/p'`
  31. # Load site-specific settings
  32. ADDRESS="$SCHEME,$SOCKET,$INSTANCE,$HWADDR"
  33. start_fn () { return; }
  34. stop_fn () { return; }
  35. . $0.opts
  36. if [ "$ACTION" = "start" ] ; then
  37. if [ -r "$NAMEFILE" ] ; then rm $NAMEFILE ; fi
  38. if [ -n "$NAMEIF" ] ; then
  39. log /sbin/nameif $NAMEIF $HWADDR || exit 1
  40. echo $NAMEIF > $NAMEFILE ; DEVICE=$NAMEIF
  41. fi
  42. fi
  43. RESOLV=/etc/resolv.conf
  44. # Now, run the specific script for Wireless LAN interfaces
  45. # Note : we need the wireless parameters to be set up before IP parameters,
  46. # so that we can perform DHCP over the Wireless link if needed. Jean II
  47. O_INFO=$INFO ; INFO=
  48. if [ -x ./wireless ] ; then
  49. . ./wireless
  50. else
  51. . /etc/pcmcia/wireless
  52. fi
  53. INFO=$O_INFO
  54. bootp_setup ()
  55. {
  56. if match `uname -r` "2.[2-9].*" ; then
  57. log /sbin/ifconfig $DEVICE up
  58. else
  59. log /sbin/ifconfig $DEVICE up 0.0.0.0
  60. log /sbin/route add default dev $DEVICE netmask 0.0.0.0
  61. fi
  62. }
  63. case "$ACTION" in
  64. 'start')
  65. [ -n "$VERBOSE" -a -n "$INFO" ] && echo "$INFO"
  66. [ -n "$IF_PORT" ] && /sbin/ifport $DEVICE $IF_PORT
  67. if is_true $PUMP ; then
  68. log "/sbin/pump -i $DEVICE > /dev/null" || exit 1
  69. elif is_true $BOOTP ; then
  70. bootp_setup
  71. if [ -x /sbin/bootpc ] ; then
  72. eval `/sbin/bootpc --bootfile '' --returniffail \
  73. --timeoutwait 10 --dev $DEVICE`
  74. log /sbin/ifconfig $DEVICE down
  75. if [ -n "$GATEWAYS" ] ; then
  76. set - $GATEWAYS ; GATEWAY=$1
  77. fi
  78. elif [ -x /sbin/pump ] ; then
  79. log "/sbin/pump -i $DEVICE > /dev/null" || exit 1
  80. else
  81. exit 1
  82. fi
  83. elif is_true $DHCP ; then
  84. bootp_setup
  85. HN=${DHCP_HOSTNAME:+-h $DHCP_HOSTNAME}
  86. if [ -x /sbin/dhcpcd ] ; then
  87. # This is a version check: I know it looks weird
  88. if /sbin/dhcpcd -XYZZY 2>&1 | grep -q DHCP ; then
  89. log "/sbin/dhcpcd -d $HN $DEVICE >/dev/null 2>&1" || exit 1
  90. else
  91. # Jump through hoops for lame 0.70-era dhcpcd
  92. L=/var/run/dhcp-lock-$DEVICE
  93. /bin/echo "#!/bin/sh\nrm $L" > $L ; chmod +x $L
  94. log "/sbin/dhcpcd $HN -c $L $DEVICE >/dev/null 2>&1"
  95. for t in 0 1 2 3 4 5 6 7 8 9 ; do
  96. sleep 2 ; if [ ! -e $L ] ; then break ; fi
  97. done
  98. rm -f $L
  99. if [ -e /etc/dhcpc/resolv.conf ] ; then
  100. echo "# $DEVICE begin" > $RESOLV.N
  101. chmod og+r $RESOLV.N
  102. cat /etc/dhcpc/resolv.conf >> $RESOLV.N
  103. echo "# $DEVICE end" >> $RESOLV.N
  104. cat $RESOLV >> $RESOLV.N ; mv $RESOLV.N $RESOLV
  105. fi
  106. fi
  107. elif [ -x /sbin/dhclient ] ; then
  108. log "/sbin/dhclient $DEVICE >/dev/null 2>&1" || exit 1
  109. elif [ -x /sbin/pump ] ; then
  110. log "/sbin/pump $HN -i $DEVICE > /dev/null" || exit 1
  111. else
  112. exit 1
  113. fi
  114. fi
  115. if [ -n "$IPADDR" ] ; then
  116. # Basic network setup
  117. NM=${NETMASK:+netmask $NETMASK}
  118. BC=${BROADCAST:+broadcast $BROADCAST}
  119. MTU=${MTU:+mtu $MTU}
  120. log /sbin/ifconfig $DEVICE up $IPADDR $NM $BC $MTU
  121. if [ -n "$NETWORK" ] ; then
  122. /sbin/ifuser $DEVICE $NETWORK || \
  123. log /sbin/route add -net $NETWORK $NM dev $DEVICE
  124. elif [ -n "$GATEWAY" ] ; then
  125. /sbin/ifuser $DEVICE $GATEWAY || \
  126. log /sbin/route add $GATEWAY $DEVICE
  127. fi
  128. [ -n "$GATEWAY" ] && /sbin/route add default gw $GATEWAY metric 1
  129. fi
  130. # Update DNS stuff
  131. if [ -n "$DOMAIN$SEARCH$DNSSRVS$DNS_1$DNS_2$DNS_3" ] ; then
  132. echo "# $DEVICE begin" > $RESOLV.N
  133. chmod og+r $RESOLV.N
  134. [ -n "$DOMAIN" ] && echo "domain $DOMAIN" >> $RESOLV.N
  135. [ -n "$SEARCH" ] && echo "search $SEARCH" >> $RESOLV.N
  136. for DNS in $DNSSRVS $DNS_1 $DNS_2 $DNS_3 ; do
  137. echo "nameserver $DNS" >> $RESOLV.N
  138. done
  139. echo "# $DEVICE end" >> $RESOLV.N
  140. sed -e "/# $DEVICE begin/,/# $DEVICE end/d" $RESOLV >> $RESOLV.N
  141. mv $RESOLV.N $RESOLV
  142. fi
  143. # Handle NFS mounts
  144. if [ -n "$MOUNTS" ] ; then
  145. for MT in $MOUNTS ; do log mount -v $MT ; done
  146. fi
  147. if [ -n "$IPX_NETNUM" ] ; then
  148. log ipx_interface add $DEVICE $IPX_FRAME $IPX_NETNUM
  149. fi
  150. start_fn $DEVICE
  151. ;;
  152. 'stop')
  153. if [ -r "$NAMEFILE" ] ; then rm $NAMEFILE ; fi
  154. stop_fn $DEVICE
  155. if is_true $PUMP || is_true $BOOTP || is_true $DHCP || \
  156. [ -n "$IPADDR" ] ; then
  157. # Shut down all NFS mounts on this interface
  158. nfsstop ()
  159. {
  160. local HOST MT
  161. if read HOST MT ; then
  162. nfsstop
  163. if /sbin/ifuser $DEVICE $HOST ; then
  164. do_fuser -k -m $MT > /dev/null
  165. log umount -f -v $MT
  166. fi
  167. fi
  168. }
  169. mount -t nfs | sed -e 's/:.* on \(.*\) type .*/ \1/' | nfsstop
  170. [ -n "$IPX_NETNUM" ] && ipx_interface del $DEVICE $IPX_FRAME
  171. if is_true $PUMP ; then
  172. log pump -r -i $DEVICE
  173. elif is_true $DHCP ; then
  174. if [ -x /sbin/dhcpcd ] ; then
  175. PID=`cat /var/run/dhcpcd-$DEVICE.pid 2>/dev/null`
  176. if [ -n "$PID" ] ; then
  177. kill -TERM $PID
  178. sleep 2
  179. fi
  180. /sbin/dhcpcd -XYZZY 2>&1 | grep -q DHCP || \
  181. rm -f /var/run/dhcpcd-$DEVICE.pid
  182. elif [ -x /sbin/dhclient ] ; then
  183. PID=`cat /var/run/dhclient.pid 2>/dev/null`
  184. if [ -n "$PID" ] ; then kill -TERM $PID ; fi
  185. elif [ -x /sbin/pump ] ; then
  186. log /sbin/pump -r -i $DEVICE
  187. fi
  188. fi
  189. fi
  190. # Remove nameservers
  191. if grep -q "# $DEVICE begin" $RESOLV ; then
  192. sed -e "/# $DEVICE begin/,/# $DEVICE end/d" $RESOLV > $RESOLV.N
  193. mv $RESOLV.N $RESOLV
  194. fi
  195. log /sbin/ifconfig $DEVICE down
  196. ;;
  197. 'check')
  198. is_true $NO_CHECK && exit 0
  199. /sbin/ifconfig $DEVICE | grep -q RUNNING || exit 0
  200. # Check for any in-use NFS mounts
  201. nfscheck ()
  202. {
  203. while read HOST MT ; do
  204. /sbin/ifuser $DEVICE $HOST && do_fuser -sm $MT && exit 1
  205. done
  206. }
  207. mount -t nfs | sed -e 's/:.* on \(.*\) type .*/ \1/' | nfscheck
  208. # Check for active TCP or UDP connections
  209. getdests ()
  210. {
  211. IFS=" :" ; read A ; read A
  212. while read A B C D E HOST PORT STATE ; do
  213. if [ "$STATE" != "FIN_WAIT1" -a "$STATE" != "FIN_WAIT2" \
  214. -a "$STATE" != "CLOSE_WAIT" -a "$STATE" != "TIME_WAIT" \
  215. -a "$HOST" != "127.0.0.1" -a "$HOST" != "0.0.0.0" \
  216. -a "$STATE" != "CLOSE" -a "$PORT" != "*" ] ; then\
  217. echo $HOST
  218. fi
  219. done
  220. }
  221. DESTS=`netstat -ntuw | getdests`
  222. /sbin/ifuser $DEVICE $DESTS && exit 1
  223. ;;
  224. 'cksum')
  225. if [ $WIRELESS = 1 ] ; then exit 1 ; fi
  226. chk_simple "$NEW_SCHEME,$SOCKET,$INSTANCE,$HWADDR" || exit 1
  227. ;;
  228. 'restart')
  229. [ -n "$IPADDR" ] && /sbin/ifconfig $DEVICE down up
  230. ;;
  231. 'suspend'|'resume')
  232. ;;
  233. *)
  234. usage
  235. ;;
  236. esac
  237. exit 0