summaryrefslogtreecommitdiff
path: root/recipes-core/mlinux-scripts/mlinux-scripts-1.0/mlinux-wifi-ap
blob: ddbec95b0798de86b378301b1b8fb002daee7037 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#!/bin/bash

# Copyright (C) 2014 Multi-Tech Systems

# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:

# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.

# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

dhcpd_file=/etc/udhcpd.conf
hostapd_file=/etc/hostapd.conf
interface=wlan0
bridge_mode=0

do_start() {
	if [ "$bridge_mode" = 1 ]; then
		interface=$b
		if ! grep -E -q "^bridge=$interface\s*$" $hostapd_file; then
			echo "Enabling bridge=br0 in $hostapd_file"
			sed -r -i "s/^#?bridge=.*$/bridge=$interface/" $hostapd_file 
		fi
	else
		if grep -E -q "^bridge=.*$" $hostapd_file; then
			echo "Disabling bridge in $hostapd_file"
			sed -r -i "s/^bridge=/#bridge=/" $hostapd_file 
		fi
	fi

	echo "Starting hostap daemon"
	/etc/init.d/hostapd start

	if [ "$bridge_mode" != 1 ]; then
		echo "Setting IP address to $ip"
		ifconfig $interface $ip
		# strip off end of IP address to get subnet
		# assumes subnet of /24
		subnet=${ip%.*}
		# escape periods for regex
		subnet_regex=${subnet//./\\.}
		ip_regex=${ip//./\\.}
		# set default address range for dhcpd
		addr_start=100
		addr_end=254
		if ! grep -E -q "^start\s+$subnet_regex\." $dhcpd_file; then
			echo "Changing dhcpd start to $subnet.$addr_start"
			sed -r -i "s/^start\s+.*$/start $subnet.$addr_start/" $dhcpd_file 
		fi
		if ! grep -E -q "^end\s+$subnet_regex\." $dhcpd_file; then
			echo "Changing dhcpd end to $subnet.$addr_end"
			sed -r -i "s/^end\s+.*$/end $subnet.$addr_end/" $dhcpd_file 
		fi
		# update dhcpd addresses if needed
		if ! grep -E -q "^option\s+router\s+$ip_regex" $dhcpd_file; then
			echo "Changing dhcpd router to $ip"
			sed -r -i "s/^option\s+router\s+.*$/option router $ip/" $dhcpd_file 
		fi
	else
		# unset ip address for bridge mode
		ifconfig wlan0 0.0.0.0
	fi

	if ! grep -E -q "^interface\s+$interface" $dhcpd_file; then
		echo "Changing dhcpd interface to $interface"
		sed -r -i "s/^interface\s+.*$/interface $interface/" $dhcpd_file 
	fi
	mlinux-dhcpd start
}

do_stop() {
	echo "Stopping hostap daemon"
	/etc/init.d/hostapd stop
	mlinux-dhcpd stop
}

usage() {
	echo "Usage: $(basename $0) start|stop|restart [options]"
	echo "  options:"
	echo "    -a <address>    Sets AP IP address (defaults to 192.168.3.1)"
	echo "    -b <interface>  Add AP to specified bridge interface (conflicts with -a)"
	exit 1
}

# main
if [[ $# < 1 ]]; then
	usage
fi

cmd=$1
shift

while getopts "a:b:h" opt; do
	case "$opt" in
		a)
			a=$OPTARG
			;;
		b)
			b=$OPTARG
			bridge_mode=1
			;;
		h)
			usage
			;;
		*)
			usage
			;;
	esac
done

# can't specify both address and bridge mode
if [ -n "$a" ] && [ -n "$b" ]; then
	usage
fi

if [ -n "$a" ]; then
	ret=1
	if [[ $a =~ ^([0-9]{1,3}\.){3,3}[0-9]{1,3}$ ]]
	then
		OIFS=$IFS
		IFS='.'
		ip=($a)
		IFS=$OFIS
		[[ ${ip[0]} -le 255 && ${ip[1]} -le 255 && ${ip[2]} -le 255 && ${ip[3]} -le 255 ]]
		ret=$?
	fi
	if [[ ret -ne 0 ]]
	then
		echo "invalid IP address"
		exit 1
	fi
	ip=$a
else
	ip="192.168.3.1"
fi

case $cmd in
	start)
		do_start
		;;
	stop)
		do_stop
		;;
	restart)
		do_stop
		sleep 1
		do_start
		;;
	*)
		usage
		;;
esac

exit 0