]> git.ipfire.org Git - thirdparty/dhcp.git/blob - client/scripts/linux
- Support for compressed 'domain name list' style DHCP option contents, and
[thirdparty/dhcp.git] / client / scripts / linux
1 #!/bin/bash
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 make_resolv_conf() {
26 if [ x"$new_domain_name_servers" != x ]; then
27 cat /dev/null > /etc/resolv.conf.dhclient
28 chmod 644 /etc/resolv.conf.dhclient
29 if [ x"$new_domain_search" != x ]; then
30 echo search $new_domain_search >> /etc/resolv.conf.dhclient
31 elif [ x"$new_domain_name" != x ]; then
32 # Note that the DHCP 'Domain Name Option' is really just a domain
33 # name, and that this practice of using the domain name option as
34 # a search path is both nonstandard and deprecated.
35 echo search $new_domain_name >> /etc/resolv.conf.dhclient
36 fi
37 for nameserver in $new_domain_name_servers; do
38 echo nameserver $nameserver >>/etc/resolv.conf.dhclient
39 done
40
41 mv /etc/resolv.conf.dhclient /etc/resolv.conf
42 fi
43 }
44
45 # Must be used on exit. Invokes the local dhcp client exit hooks, if any.
46 exit_with_hooks() {
47 exit_status=$1
48 if [ -f /etc/dhclient-exit-hooks ]; then
49 . /etc/dhclient-exit-hooks
50 fi
51 # probably should do something with exit status of the local script
52 exit $exit_status
53 }
54
55 # Invoke the local dhcp client enter hooks, if they exist.
56 if [ -f /etc/dhclient-enter-hooks ]; then
57 exit_status=0
58 . /etc/dhclient-enter-hooks
59 # allow the local script to abort processing of this state
60 # local script must set exit_status variable to nonzero.
61 if [ $exit_status -ne 0 ]; then
62 exit $exit_status
63 fi
64 fi
65
66 release=`uname -r`
67 release=`expr $release : '\(.*\)\..*'`
68 relminor=`echo $release |sed -e 's/[0-9]*\.\([0-9][0-9]*\)\(\..*\)*$/\1/'`
69 relmajor=`echo $release |sed -e 's/\([0-9][0-9]*\)\..*$/\1/'`
70
71 if [ x$new_broadcast_address != x ]; then
72 new_broadcast_arg="broadcast $new_broadcast_address"
73 fi
74 if [ x$old_broadcast_address != x ]; then
75 old_broadcast_arg="broadcast $old_broadcast_address"
76 fi
77 if [ x$new_subnet_mask != x ]; then
78 new_subnet_arg="netmask $new_subnet_mask"
79 fi
80 if [ x$old_subnet_mask != x ]; then
81 old_subnet_arg="netmask $old_subnet_mask"
82 fi
83 if [ x$alias_subnet_mask != x ]; then
84 alias_subnet_arg="netmask $alias_subnet_mask"
85 fi
86
87 if [ x$reason = xMEDIUM ]; then
88 # Linux doesn't do mediums (ok, ok, media).
89 exit_with_hooks 0
90 fi
91
92 if [ x$reason = xPREINIT ]; then
93 if [ x$alias_ip_address != x ]; then
94 # Bring down alias interface. Its routes will disappear too.
95 ifconfig $interface:0- inet 0
96 fi
97 if [ $relmajor -lt 2 ] || ( [ $relmajor -eq 2 ] && [ $relminor -eq 0 ] )
98 then
99 ifconfig $interface inet 0.0.0.0 netmask 0.0.0.0 \
100 broadcast 255.255.255.255 up
101 # Add route to make broadcast work. Do not omit netmask.
102 route add default dev $interface netmask 0.0.0.0
103 else
104 ifconfig $interface 0 up
105 fi
106
107 # We need to give the kernel some time to get the interface up.
108 sleep 1
109
110 exit_with_hooks 0
111 fi
112
113 if [ x$reason = xARPCHECK ] || [ x$reason = xARPSEND ]; then
114 exit_with_hooks 0
115 fi
116
117 if [ x$reason = xBOUND ] || [ x$reason = xRENEW ] || \
118 [ x$reason = xREBIND ] || [ x$reason = xREBOOT ]; then
119 current_hostname=`hostname`
120 if [ x$current_hostname = x ] || \
121 [ x$current_hostname = x$old_host_name ]; then
122 if [ x$current_hostname = x ] || \
123 [ x$new_host_name != x$old_host_name ]; then
124 hostname $new_host_name
125 fi
126 fi
127
128 if [ x$old_ip_address != x ] && [ x$alias_ip_address != x ] && \
129 [ x$alias_ip_address != x$old_ip_address ]; then
130 # Possible new alias. Remove old alias.
131 ifconfig $interface:0- inet 0
132 fi
133 if [ x$old_ip_address != x ] && [ x$old_ip_address != x$new_ip_address ]; then
134 # IP address changed. Bringing down the interface will delete all routes,
135 # and clear the ARP cache.
136 ifconfig $interface inet 0 down
137
138 fi
139 if [ x$old_ip_address = x ] || [ x$old_ip_address != x$new_ip_address ] || \
140 [ x$reason = xBOUND ] || [ x$reason = xREBOOT ]; then
141
142 ifconfig $interface inet $new_ip_address $new_subnet_arg \
143 $new_broadcast_arg
144 # Add a network route to the computed network address.
145 if [ $relmajor -lt 2 ] || \
146 ( [ $relmajor -eq 2 ] && [ $relminor -eq 0 ] ); then
147 route add -net $new_network_number $new_subnet_arg dev $interface
148 fi
149 for router in $new_routers; do
150 route add default gw $router
151 done
152 fi
153 if [ x$new_ip_address != x$alias_ip_address ] && [ x$alias_ip_address != x ];
154 then
155 ifconfig $interface:0- inet 0
156 ifconfig $interface:0 inet $alias_ip_address $alias_subnet_arg
157 route add -host $alias_ip_address $interface:0
158 fi
159 make_resolv_conf
160 exit_with_hooks 0
161 fi
162
163 if [ x$reason = xEXPIRE ] || [ x$reason = xFAIL ] || [ x$reason = xRELEASE ] \
164 || [ x$reason = xSTOP ]; then
165 if [ x$alias_ip_address != x ]; then
166 # Turn off alias interface.
167 ifconfig $interface:0- inet 0
168 fi
169 if [ x$old_ip_address != x ]; then
170 # Shut down interface, which will delete routes and clear arp cache.
171 ifconfig $interface inet 0 down
172 fi
173 if [ x$alias_ip_address != x ]; then
174 ifconfig $interface:0 inet $alias_ip_address $alias_subnet_arg
175 route add -host $alias_ip_address $interface:0
176 fi
177 exit_with_hooks 0
178 fi
179
180 if [ x$reason = xTIMEOUT ]; then
181 if [ x$alias_ip_address != x ]; then
182 ifconfig $interface:0- inet 0
183 fi
184 ifconfig $interface inet $new_ip_address $new_subnet_arg \
185 $new_broadcast_arg
186 set $new_routers
187 ############## what is -w in ping?
188 if ping -q -c 1 $1; then
189 if [ x$new_ip_address != x$alias_ip_address ] && \
190 [ x$alias_ip_address != x ]; then
191 ifconfig $interface:0 inet $alias_ip_address $alias_subnet_arg
192 route add -host $alias_ip_address dev $interface:0
193 fi
194 if [ $relmajor -lt 2 ] || \
195 ( [ $relmajor -eq 2 ] && [ $relminor -eq 0 ] ); then
196 route add -net $new_network_number
197 fi
198 for router in $new_routers; do
199 route add default gw $router
200 done
201 make_resolv_conf
202 exit_with_hooks 0
203 fi
204 ifconfig $interface inet 0 down
205 exit_with_hooks 1
206 fi
207
208 exit_with_hooks 0