home *** CD-ROM | disk | FTP | other *** search
/ Australian Personal Computer 2003 July / CD 2 Debian 3.0r1 / Debian.iso / isolinux / root.bin / root / etc / dhclient-script next >
Encoding:
Text File  |  2002-03-16  |  5.8 KB  |  190 lines

  1. #!/bin/sh
  2. # dhclient-script for Linux. Dan Halbert, March, 1997.
  3. # Updated for Linux 2.[12] by Brian J. Murrell, January 1999.
  4. # No guarantees about this. I'm a novice at the details of Linux
  5. # networking.
  6.  
  7. # Notes:
  8.  
  9. # 0. This script is based on the netbsd script supplied with dhcp-970306.
  10.  
  11. # 1. ifconfig down apparently deletes all relevant routes and flushes
  12. # the arp cache, so this doesn't need to be done explicitly.
  13.  
  14. # 2. The alias address handling here has not been tested AT ALL.
  15. # I'm just going by the doc of modern Linux ip aliasing, which uses
  16. # notations like eth0:0, eth0:1, for each alias.
  17.  
  18. # 3. I have to calculate the network address, and calculate the broadcast
  19. # address if it is not supplied. This might be much more easily done
  20. # by the dhclient C code, and passed on.
  21.  
  22. # 4. TIMEOUT not tested. ping has a flag I don't know, and I'm suspicious
  23. # of the $1 in its args.
  24.  
  25. # Must be used on exit.   Invokes the local dhcp client exit hooks, if any.
  26. exit_with_hooks() {
  27.   exit_status=$1
  28.   if [ -f /etc/dhclient-exit-hooks ]; then
  29.     . /etc/dhclient-exit-hooks
  30.   fi
  31. # probably should do something with exit status of the local script
  32.   exit $exit_status
  33. }
  34.  
  35. make_resolv_conf() {
  36.   echo search $new_domain_name >/etc/resolv.conf
  37.   for nameserver in $new_domain_name_servers; do
  38.     echo nameserver $nameserver >>/etc/resolv.conf
  39.   done
  40. }
  41.  
  42. # Invoke the local dhcp client enter hooks, if they exist.
  43. if [ -f /etc/dhclient-enter-hooks ]; then
  44.   exit_status=0
  45.   . /etc/dhclient-enter-hooks
  46.   # allow the local script to abort processing of this state
  47.   # local script must set exit_status variable to nonzero.
  48.   if [ $exit_status -ne 0 ]; then
  49.     exit $exit_status
  50.   fi
  51. fi
  52.  
  53. release=`uname -r`
  54.  
  55. relminor=`expr $release : '[0-9]*\.[0-9]*\.\([0-9]*\).*'`
  56. relmajor=`expr $release : '[0-9]*\.\([0-9]*\)\..*'`
  57. release=`expr $release : '\([0-9]*\)\..*'`
  58.  
  59. if [ x$new_broadcast_address != x ]; then
  60.   new_broadcast_arg="broadcast $new_broadcast_address"
  61. fi
  62. if [ x$old_broadcast_address != x ]; then
  63.   old_broadcast_arg="broadcast $old_broadcast_address"
  64. fi
  65. if [ x$new_subnet_mask != x ]; then
  66.   new_subnet_arg="netmask $new_subnet_mask"
  67. fi
  68. if [ x$old_subnet_mask != x ]; then
  69.   old_subnet_arg="netmask $old_subnet_mask"
  70. fi
  71. if [ x$alias_subnet_mask != x ]; then
  72.   alias_subnet_arg="netmask $alias_subnet_mask"
  73. fi
  74.  
  75. if [ x$reason = xMEDIUM ]; then
  76.   # Linux doesn't do mediums (ok, ok, media).
  77.   exit_with_hooks 0
  78. fi
  79.  
  80. if [ x$reason = xPREINIT ]; then
  81.   if [ x$alias_ip_address != x ]; then
  82.     # Bring down alias interface. Its routes will disappear too.
  83.     ifconfig $interface:0- inet 0
  84.   fi
  85.   if [ $relmajor -lt 2 ] || ( [ $relmajor -eq 2 ] && [ $relminor -eq 0 ] )
  86.    then
  87.     ifconfig $interface inet 0.0.0.0 netmask 0.0.0.0 \
  88.         broadcast 255.255.255.255 up
  89.     # Add route to make broadcast work. Do not omit netmask.
  90.     route add default dev $interface netmask 0.0.0.0
  91.   else
  92.     ifconfig $interface up
  93.   fi
  94.  
  95.   # We need to give the kernel some time to get the interface up.
  96.   sleep 1
  97.  
  98.   exit_with_hooks 0
  99. fi
  100.  
  101. if [ x$reason = xARPCHECK ] || [ x$reason = xARPSEND ]; then
  102.   exit_with_hooks 0
  103. fi
  104.   
  105. if [ x$reason = xBOUND ] || [ x$reason = xRENEW ] || \
  106.    [ x$reason = xREBIND ] || [ x$reason = xREBOOT ]; then
  107.   if [ x$old_ip_address != x ] && [ x$alias_ip_address != x ] && \
  108.         [ x$alias_ip_address != x$old_ip_address ]; then
  109.     # Possible new alias. Remove old alias.
  110.     ifconfig $interface:0- inet 0
  111.   fi
  112.   if [ x$old_ip_address != x ] && [ x$old_ip_address != x$new_ip_address ]; then
  113.     # IP address changed. Bringing down the interface will delete all routes,
  114.     # and clear the ARP cache.
  115.     ifconfig $interface inet down
  116.  
  117.   fi
  118.   if [ x$old_ip_address = x ] || [ x$old_ip_address != x$new_ip_address ] || \
  119.      [ x$reason = xBOUND ] || [ x$reason = xREBOOT ]; then
  120.  
  121.     ifconfig $interface inet $new_ip_address $new_subnet_arg \
  122.                             $new_broadcast_arg
  123.     if [ x$new_interface_mtu != x ]; then
  124.         ifconfig $interface mtu $new_interface_mtu
  125.     fi
  126.     # Add a network route to the computed network address.
  127.     if [ $relmajor -lt 2 ] || \
  128.         ( [ $relmajor -eq 2 ] && [ $relminor -eq 0 ] ); then
  129.       route add -net $new_network_number $new_subnet_arg dev $interface
  130.     fi
  131.     for router in $new_routers; do
  132.       route add default gw $router
  133.     done
  134.   fi
  135.   if [ x$new_ip_address != x$alias_ip_address ] && [ x$alias_ip_address != x ];
  136.    then
  137.     ifconfig $interface:0- inet 0
  138.     ifconfig $interface:0 inet $alias_ip_address $alias_subnet_arg
  139.     route add -host $alias_ip_address $interface:0
  140.   fi
  141.   make_resolv_conf
  142.   exit_with_hooks 0
  143. fi
  144.  
  145. if [ x$reason = xEXPIRE ] || [ x$reason = xFAIL ]; then
  146.   if [ x$alias_ip_address != x ]; then
  147.     # Turn off alias interface.
  148.     ifconfig $interface:0- inet 0
  149.   fi
  150.   if [ x$old_ip_address != x ]; then
  151.     # Shut down interface, which will delete routes and clear arp cache.
  152.     ifconfig $interface inet down
  153.   fi
  154.   if [ x$alias_ip_address != x ]; then
  155.     ifconfig $interface:0 inet $alias_ip_address $alias_subnet_arg
  156.     route add -host $alias_ip_address $interface:0
  157.   fi
  158.   exit_with_hooks 0
  159. fi
  160.  
  161. if [ x$reason = xTIMEOUT ]; then
  162.   if [ x$alias_ip_address != x ]; then
  163.     ifconfig $interface:0- inet 0
  164.   fi
  165.   ifconfig $interface inet $new_ip_address $new_subnet_arg \
  166.                     $new_broadcast_arg
  167.   set $new_routers
  168.   ############## what is -w in ping?
  169.   if ping -q -c 1 $1; then
  170.     if [ x$new_ip_address != x$alias_ip_address ] && \
  171.             [ x$alias_ip_address != x ]; then
  172.       ifconfig $interface:0 inet $alias_ip_address $alias_subnet_arg
  173.       route add -host $alias_ip_address dev $interface:0
  174.     fi
  175.     if [ $relmajor -lt 2 ] || \
  176.         ( [ $relmajor -eq 2 ] && [ $relminor -eq 0 ] ); then
  177.       route add -net $new_network_number
  178.     fi
  179.     for router in $new_routers; do
  180.       route add default gw $router
  181.     done
  182.     make_resolv_conf
  183.     exit_with_hooks 0
  184.   fi
  185.   ifconfig $interface inet down
  186.   exit_with_hooks 1
  187. fi
  188.  
  189. exit_with_hooks 0
  190.