]> git.ipfire.org Git - people/ms/network.git/blob - src/hooks/configs/pppoe-server
pppoe-server: prevent multiple configs for the same zone
[people/ms/network.git] / src / hooks / configs / pppoe-server
1 #!/bin/bash
2 ###############################################################################
3 # #
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2010 Michael Tremer & Christian Schmidt #
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 . /usr/lib/network/header-config
23
24 HOOK_CONFIG_SETTINGS="HOOK DNS_SERVERS MTU SERVICE_NAME SUBNET MAX_SESSIONS"
25
26 # Maximum Transmission Unit.
27 MTU=1492
28
29 # Service Name.
30 SERVICE_NAME=
31
32 # A subnet. Addresses from this subnet will be given to the remote hosts.
33 # The net address will be the gateway address for the PPPoE server.
34 SUBNET=
35
36 # Defines the max. number of sessions per MAC address.
37 # 0 = unlimited.
38 MAX_SESSIONS=0
39
40 hook_check_config_settings() {
41 assert isset MTU
42 assert isset SUBNET
43 assert isset MAX_SESSIONS
44
45 local server
46 for server in ${DNS_SERVERS}; do
47 assert ipv4_is_valid "${server}"
48 done
49 }
50
51 hook_new() {
52 local zone=${1}
53 shift
54
55 if zone_config_hook_is_configured ${zone} "pppoe-server"; then
56 log ERROR "You can configure the pppoe-server hook only once for a zone"
57 return ${EXIT_ERROR}
58 fi
59
60 while [ $# -gt 0 ]; do
61 case "${1}" in
62 --dns-server=*)
63 local dns_servers="$(cli_get_val "${1}")"
64
65 local dns_server
66 for dns_server in ${dns_servers}; do
67 if ! ipv4_is_valid "${dns_server}"; then
68 warning "Invalid IPv4 address: ${dns_server}. Skipped."
69 continue
70 fi
71
72 list_append DNS_SERVERS "${dns_server}"
73 done
74 ;;
75 --max-sessions=*)
76 MAX_SESSIONS=$(cli_get_val ${1})
77 if ! isinteger ${MAX_SESSIONS} || ! [ ${MAX_SESSIONS} -ge 0 ]; then
78 error "Invalid value for '--max-session'. This value must be an integer greate or eqal zero."
79 exit ${EXIT_ERROR}
80 fi
81 ;;
82 --mtu=*)
83 MTU=$(cli_get_val ${1})
84 if ! mtu_is_valid "ipv4" ${MTU}; then
85 error "Invalid value for '--mtu'. Cannot be larger then 9000 or smaller than 576"
86 exit ${EXIT_ERROR}
87 fi
88 ;;
89 --service-name=*)
90 SERVICE_NAME=$(cli_get_val ${1})
91 ;;
92 --subnet=*)
93 SUBNET=$(cli_get_val ${1})
94 if ! ipv4_net_is_valid "${SUBNET}"; then
95 error "Invalid IPv4 Subnet ${SUBNET}."
96 exit ${EXIT_ERROR}
97 fi
98 ;;
99 *)
100 warning "Ignoring unknown option '${1}'"
101 ;;
102 esac
103 shift
104 done
105
106 zone_config_settings_write "${zone}" "${HOOK}"
107
108 exit ${EXIT_OK}
109 }
110
111 hook_up() {
112 local zone=${1}
113 local config=${2}
114 shift 2
115
116 # Start the PPPoE server.
117 pppoe_server_start ${zone}
118
119 exit ${EXIT_OK}
120 }
121
122 hook_down() {
123 local zone=${1}
124 local config=${2}
125 shift 2
126
127 if ! device_exists ${zone}; then
128 error "Zone '${zone}' doesn't exist."
129 exit ${EXIT_ERROR}
130 fi
131
132 # Stop the PPPoE server.
133 pppoe_server_stop ${zone}
134
135 exit ${EXIT_OK}
136 }
137
138 hook_status() {
139 local zone=${1}
140 local config=${2}
141 shift 2
142
143 if ! device_exists ${zone}; then
144 error "Zone '${zone}' doesn't exist."
145 exit ${EXIT_ERROR}
146 fi
147
148 zone_config_settings_read "${zone}" "${config}"
149
150 local status
151 if pppoe_server_status ${zone}; then
152 status="${MSG_HOOK_UP}"
153 else
154 status="${MSG_HOOK_DOWN}"
155 fi
156 cli_statusline 3 "PPPoE server" "${status}"
157
158 local gateway=$(ipv4_get_network ${SUBNET})
159 cli_print_fmt1 3 "Gateway" "${gateway}"
160
161 local start_address=$(ipv4_encode ${gateway})
162 start_address=$(( ${start_address} + 1 ))
163 start_address=$(ipv4_decode ${start_address})
164 local end_address=$(ipv4_get_broadcast ${SUBNET})
165
166 cli_print_fmt1 3 "Client range" \
167 "${start_address}-${end_address}"
168 cli_space
169
170 local max_sessions=${MAX_SESSIONS}
171 if [ "${max_sessions}" = "0" ]; then
172 max_sessions="unlimited"
173 fi
174 cli_print_fmt1 3 "${max_sessions} session(s) per MAC"
175 cli_space
176
177 exit ${EXIT_OK}
178 }