]> git.ipfire.org Git - ipfire-2.x.git/blob - src/initscripts/networking/functions.network
dhcpcd: Force setting MTU through dhcpcd
[ipfire-2.x.git] / src / initscripts / networking / functions.network
1 #!/bin/sh
2 ###############################################################################
3 # #
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2007-2022 IPFire Team <info@ipfire.org> #
6 # #
7 # This program is free software: you can redistribute it and/or modify #
8 # it under the terms of the GNU General Public License as published by #
9 # the Free Software Foundation, either version 3 of the License, or #
10 # (at your option) any later version. #
11 # #
12 # This program is distributed in the hope that it will be useful, #
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
15 # GNU General Public License for more details. #
16 # #
17 # You should have received a copy of the GNU General Public License #
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. #
19 # #
20 ###############################################################################
21
22 . /etc/sysconfig/rc
23 . $rc_functions
24
25 eval $(/usr/local/bin/readhash /var/ipfire/ethernet/settings)
26 eval $(/usr/local/bin/readhash /var/ipfire/dns/settings)
27
28 dhcpcd_get_pid() {
29 # This function returns the pid of a dhcpcd by a given
30 # network device, if a pidfile exists.
31
32 local device="$1"
33 local pidfile="/var/run/dhcpcd/${device}.pid"
34
35 # Check if a pid file exists.
36 if [ -f "${pidfile}" ] ; then
37
38 # Get the pid from the file.
39 local pid="$(<"${pidfile}")"
40
41 echo "${pid}"
42 fi
43 }
44
45 dhcpcd_is_running() {
46 # This functions checks if a dhcpcd is running by a given pid.
47
48 local pid="$1"
49
50 # Check if a dhcpcd is running.
51 if [ -n "${pid}" -a -d "/proc/${pid}" ]; then
52 # Return "0" (True) if a dhcpcd is running.
53 return 0
54 fi
55
56 # Return 1 (False) no dhcpcd is running.
57 return 1
58 }
59
60 dhcpcd_start() {
61 # This function will start a dhcpcd on a speciefied device.
62
63 local device="$1"
64 local dhcp_start=()
65
66 boot_mesg -n "Starting dhcpcd on the ${device} interface..."
67
68 # Check if a dhcpcd is already running.
69 local pid="$(dhcpcd_get_pid "${device}")"
70
71 if dhcpcd_is_running "${pid}"; then
72 boot_mesg "dhcpcd already running!" ${WARNING}
73 echo_warning
74 exit 2
75 fi
76
77 # Check if a DHCP hostname has been set.
78 if [ -n "${RED_DHCP_HOSTNAME}" ]; then
79 dhcp_start+=( "-h" "${RED_DHCP_HOSTNAME}" )
80 fi
81
82 # Tell dhcpcd to use the configured MTU
83 if [ -n "${RED_DHCP_FORCE_MTU}" ]; then
84 dhcp_start+=( "--static" "mtu=${RED_DHCP_FORCE_MTU}" )
85 fi
86
87 # Start dhcpcd.
88 /sbin/dhcpcd "${dhcp_start[@]}" ${device} >/dev/null 2>&1
89 ret="$?"
90
91 if [ "${ret}" -eq 0 ]; then
92 . /var/ipfire/dhcpc/dhcpcd-"${device}".info
93
94 if [ $ip_address ]; then
95 echo ""
96 echo_ok
97 boot_mesg " DHCP Assigned Settings for ${device}:"
98 boot_mesg_flush
99 boot_mesg " IP Address: $ip_address"
100 boot_mesg_flush
101
102 if [ -n "${RED_DHCP_HOSTNAME}" ]; then
103 boot_mesg " Hostname: $RED_DHCP_HOSTNAME"
104 boot_mesg_flush
105 fi
106
107 boot_mesg " Subnet Mask: $subnet_mask"
108 boot_mesg_flush
109 boot_mesg " Default Gateway: $routers"
110 boot_mesg_flush
111 boot_mesg " DNS Server: $domain_name_servers"
112 boot_mesg_flush
113 else
114 echo ""
115 echo_ok
116 boot_mesg "DHCP for ${device} still running..."
117 boot_mesg_flush
118 fi
119 else
120 echo ""
121 $(exit "${ret}")
122 evaluate_retval
123 fi
124 }
125
126 dhcpcd_stop() {
127 # This function stops a previously started dhcpcd on a given device.
128
129 local device="$1"
130 local dhcp_stop="-k"
131 local leaseinfo="/var/ipfire/dhcpc/dhcpcd-${device}.info"
132
133 boot_mesg -n "Stopping dhcpcd on the ${device} interface..."
134
135 # Check if a dhcpcd is running.
136 local pid="$(dhcpcd_get_pid "${device}")"
137
138 if ! dhcpcd_is_running "${pid}"; then
139 boot_mesg " Not running." ${WARNING}
140 echo_warning
141 exit 1
142 fi
143
144 # Stop dhcpcd.
145 /sbin/dhcpcd ${dhcp_stop} ${device} &> /dev/null
146 ret="$?"
147
148 # Wait until dhcpd has stopped.
149 while [ -d "/proc/${pid}" ]; do
150 sleep 1
151 # repeat stop if dhcp was still running
152 /sbin/dhcpcd ${dhcp_stop} ${device} &> /dev/null
153 done
154
155 # Display console message, depended on the exit code
156 # of the stopped dhcpcd.
157 if [ "${ret}" -eq 0 ]; then
158 boot_mesg
159 echo_ok
160 elif [ "${ret}" -eq 1 ]; then
161 boot_mesg "failed to stop dhcpcd!" ${WARNING}
162 echo_warning
163 else
164 boot_mesg
165 echo_failure
166 fi
167 }