diff --git a/data/templates/accel-ppp/ipoe.config.tmpl b/data/templates/accel-ppp/ipoe.config.tmpl index 0599b5501..976cddd52 100644 --- a/data/templates/accel-ppp/ipoe.config.tmpl +++ b/data/templates/accel-ppp/ipoe.config.tmpl @@ -1,146 +1,155 @@ ### generated by ipoe.py ### [modules] log_syslog ipoe shaper ipv6pool ipv6_nd ipv6_dhcp ippool {% if auth_mode == 'radius' %} radius {% elif auth_mode == 'local' %} chap-secrets {% endif %} [core] thread-count={{ thread_cnt }} [log] syslog=accel-ipoe,daemon copy=1 level=5 [ipoe] verbose=1 {% for interface in interfaces %} {% set ifname = interface.name %} {% if interface.vlan_mon %} {% set ifname = 're:^' ~ interface.name ~ '\.' ~ interface.vlan_mon | range_to_regex ~ '$' %} {% endif %} interface={{ ifname }},shared={{ interface.shared }},mode={{ interface.mode }},ifcfg={{ interface.ifcfg }}{{ ',range=' ~ interface.range if interface.range is defined and interface.range is not none }},start={{ interface.sess_start }},ipv6=1 {% endfor %} {% if auth_mode == 'noauth' %} noauth=1 -{% if client_named_ip_pool %} -{% for pool in client_named_ip_pool %} -{% if pool.subnet is defined %} -ip-pool={{ pool.name }} -{% endif %} -{% if pool.gateway_address is defined %} -gw-ip-address={{ pool.gateway_address }}/{{ pool.subnet.split('/')[1] }} -{% endif %} -{% endfor%} -{% endif %} {% elif auth_mode == 'local' %} username=ifname password=csid +{% elif auth_mode == 'radius' %} +attr-dhcp-client-ip=Framed-IP-Address +attr-dhcp-mask=Framed-IP-Netmask +{% endif %} +{% if gateway_address %} +{% for gw in gateway_address %} +gw-ip-address={{ gw }} +{% endfor %} +{% endif %} +{% if client_named_ip_pool %} +{% for pool in client_named_ip_pool %} +{% if pool.subnet is defined %} +ip-pool={{ pool.name }} +{% endif %} +{% if pool.gateway_address is defined %} +gw-ip-address={{ pool.gateway_address }}/{{ pool.subnet.split('/')[1] }} +{% endif %} +{% endfor%} {% endif %} + proxy-arp=1 {% for interface in interfaces %} {% if (interface.shared == '0') and (interface.vlan_mon) %} vlan-mon={{ interface.name }},{{ interface.vlan_mon | join(',') }} {% endif %} {% endfor %} {% if dnsv4 %} [dns] {% for dns in dnsv4 %} dns{{ loop.index }}={{ dns }} {% endfor %} {% endif %} {% if dnsv6 %} [ipv6-dns] {% for dns in dnsv6 %} {{ dns }} {% endfor %} {% endif %} [ipv6-nd] verbose=1 [ipv6-dhcp] verbose=1 {% if client_named_ip_pool %} [ip-pool] {% for pool in client_named_ip_pool %} {% if pool.subnet is defined %} {{ pool.subnet }},name={{ pool.name }} {% endif %} {% if pool.gateway_address is defined %} gw-ip-address={{ pool.gateway_address }}/{{ pool.subnet.split('/')[1] }} {% endif %} {% endfor%} {% endif %} {% if client_ipv6_pool %} [ipv6-pool] {% for p in client_ipv6_pool %} {{ p.prefix }},{{ p.mask }} {% endfor %} {% for p in client_ipv6_delegate_prefix %} delegate={{ p.prefix }},{{ p.mask }} {% endfor %} {% endif %} {% if auth_mode == 'local' %} [chap-secrets] chap-secrets={{ chap_secrets_file }} {% elif auth_mode == 'radius' %} [radius] verbose=1 {% for r in radius_server %} server={{ r.server }},{{ r.key }},auth-port={{ r.port }},acct-port={{ r.acct_port }},req-limit=0,fail-time={{ r.fail_time }} {% endfor %} {% if radius_acct_inter_jitter %} acct-interim-jitter={{ radius_acct_inter_jitter }} {% endif %} acct-timeout={{ radius_acct_tmo }} timeout={{ radius_timeout }} max-try={{ radius_max_try }} {% if radius_nas_id %} nas-identifier={{ radius_nas_id }} {% endif %} {% if radius_nas_ip %} nas-ip-address={{ radius_nas_ip }} {% endif %} {% if radius_source_address %} bind={{ radius_source_address }} {% endif %} {% if radius_dynamic_author %} dae-server={{ radius_dynamic_author.server }}:{{ radius_dynamic_author.port }},{{ radius_dynamic_author.key }} {% endif %} {% if radius_shaper_enable %} [shaper] verbose=1 {% if radius_shaper_attr %} attr={{ radius_shaper_attr }} {% endif %} {% if radius_shaper_multiplier %} rate-multiplier={{ radius_shaper_multiplier }} {% endif %} {% if radius_shaper_vendor %} vendor={{ radius_shaper_vendor }} {% endif %} {% endif %} {% endif %} [cli] tcp=127.0.0.1:2002 diff --git a/interface-definitions/include/accel-ppp/gateway-address-multi.xml.i b/interface-definitions/include/accel-ppp/gateway-address-multi.xml.i new file mode 100644 index 000000000..dcc58b97a --- /dev/null +++ b/interface-definitions/include/accel-ppp/gateway-address-multi.xml.i @@ -0,0 +1,17 @@ +<!-- include start from accel-ppp/gateway-address-multi.xml.i --> +<leafNode name="gateway-address"> + <properties> + <help>Gateway IP address</help> + <constraintErrorMessage>invalid IPv4 address</constraintErrorMessage> + <valueHelp> + <format>ipv4net</format> + <description>Default Gateway, mask send to the client</description> + </valueHelp> + <constraint> + <validator name="ipv4-prefix"/> + <validator name="ipv4-host"/> + </constraint> + <multi/> + </properties> +</leafNode> +<!-- include end --> diff --git a/interface-definitions/service_ipoe-server.xml.in b/interface-definitions/service_ipoe-server.xml.in index d81ec99f9..a084d8c07 100644 --- a/interface-definitions/service_ipoe-server.xml.in +++ b/interface-definitions/service_ipoe-server.xml.in @@ -1,221 +1,222 @@ <?xml version="1.0"?> <interfaceDefinition> <node name="service"> <children> <node name="ipoe-server" owner="${vyos_conf_scripts_dir}/service_ipoe-server.py"> <properties> <help>Internet Protocol over Ethernet (IPoE) Server</help> <priority>900</priority> </properties> <children> <tagNode name="interface"> <properties> <help>Network interface to server IPoE</help> <completionHelp> <script>${vyos_completion_dir}/list_interfaces.py</script> </completionHelp> </properties> <children> <leafNode name="network-mode"> <properties> <help>Network Layer IPoE serves on</help> <completionHelp> <list>L2 L3</list> </completionHelp> <constraint> <regex>^(L2|L3)$</regex> </constraint> <valueHelp> <format>L2</format> <description>client share the same subnet</description> </valueHelp> <valueHelp> <format>L3</format> <description>clients are behind this router</description> </valueHelp> </properties> </leafNode> <leafNode name="network"> <properties> <help>Enables clients to share the same network or each client has its own vlan</help> <completionHelp> <list>shared vlan</list> </completionHelp> <constraint> <regex>^(shared|vlan)$</regex> </constraint> <valueHelp> <format>shared</format> <description>Multiple clients share the same network</description> </valueHelp> <valueHelp> <format>vlan</format> <description>One VLAN per client</description> </valueHelp> </properties> </leafNode> <leafNode name="client-subnet"> <properties> <help>Client address pool</help> <valueHelp> <format>ipv4net</format> <description>IPv4 address and prefix length</description> </valueHelp> <constraint> <validator name="ipv4-prefix"/> </constraint> </properties> </leafNode> <node name="external-dhcp"> <properties> <help>DHCP requests will be forwarded</help> </properties> <children> <leafNode name="dhcp-relay"> <properties> <help>DHCP Server the request will be redirected to.</help> <valueHelp> <format>ipv4</format> <description>IPv4 address of the DHCP Server</description> </valueHelp> <constraint> <validator name="ipv4-address"/> </constraint> </properties> </leafNode> <leafNode name="giaddr"> <properties> <help>address of the relay agent (Relay Agent IP Address)</help> </properties> </leafNode> </children> </node> <leafNode name="vlan-id"> <properties> <help>VLAN monitor for the automatic creation of vlans (user per vlan)</help> <constraint> <validator name="numeric" argument="--range 1-4096"/> </constraint> <constraintErrorMessage>VLAN ID needs to be between 1 and 4096</constraintErrorMessage> <multi/> </properties> </leafNode> <leafNode name="vlan-range"> <properties> <help>VLAN monitor for the automatic creation of vlans (user per vlan)</help> <constraint> <regex>(409[0-6]|40[0-8][0-9]|[1-3][0-9]{3}|[1-9][0-9]{0,2})-(409[0-6]|40[0-8][0-9]|[1-3][0-9]{3}|[1-9][0-9]{0,2})</regex> </constraint> <multi/> </properties> </leafNode> </children> </tagNode> #include <include/name-server-ipv4-ipv6.xml.i> <node name="client-ip-pool"> <properties> <help>Client IP pools and gateway setting</help> </properties> <children> #include <include/accel-ppp/client-ip-pool-name.xml.i> </children> </node> #include <include/accel-ppp/client-ipv6-pool.xml.i> + #include <include/accel-ppp/gateway-address-multi.xml.i> <node name="authentication"> <properties> <help>Client authentication methods</help> </properties> <children> <leafNode name="mode"> <properties> <help>Authetication mode</help> <completionHelp> <list>local radius noauth</list> </completionHelp> <constraint> <regex>^(local|radius|noauth)$</regex> </constraint> <valueHelp> <format>local</format> <description>Authentication based on local definition</description> </valueHelp> <valueHelp> <format>radius</format> <description>Authentication based on a RADIUS server</description> </valueHelp> <valueHelp> <format>noauth</format> <description>Authentication disabled</description> </valueHelp> </properties> </leafNode> <tagNode name="interface"> <properties> <help>Network interface the client mac will appear on</help> <completionHelp> <script>${vyos_completion_dir}/list_interfaces.py</script> </completionHelp> </properties> <children> <tagNode name="mac-address"> <properties> <help>Client mac address allowed to receive an IP address</help> <valueHelp> <format>macaddr</format> <description>Hardware (MAC) address</description> </valueHelp> <constraint> <validator name="mac-address"/> </constraint> </properties> <children> <node name="rate-limit"> <properties> <help>Upload/Download speed limits</help> </properties> <children> <leafNode name="upload"> <properties> <help>Upload bandwidth limit in kbits/sec</help> <constraint> <validator name="numeric" argument="--range 1-65535"/> </constraint> </properties> </leafNode> <leafNode name="download"> <properties> <help>Download bandwidth limit in kbits/sec</help> <constraint> <validator name="numeric" argument="--range 1-65535"/> </constraint> </properties> </leafNode> </children> </node> <leafNode name="vlan-id"> <properties> <help>VLAN-ID of the client network</help> <constraint> <validator name="numeric" argument="--range 1-4096"/> </constraint> <constraintErrorMessage>VLAN ID needs to be between 1 and 4096</constraintErrorMessage> </properties> </leafNode> </children> </tagNode> </children> </tagNode> <node name="radius"> <children> #include <include/accel-ppp/radius-additions-rate-limit.xml.i> </children> </node> #include <include/radius-server-ipv4.xml.i> #include <include/accel-ppp/radius-additions.xml.i> </children> </node> </children> </node> </children> </node> </interfaceDefinition> diff --git a/src/conf_mode/service_ipoe-server.py b/src/conf_mode/service_ipoe-server.py index 42cfd0f12..86185f37f 100755 --- a/src/conf_mode/service_ipoe-server.py +++ b/src/conf_mode/service_ipoe-server.py @@ -1,345 +1,358 @@ #!/usr/bin/env python3 # # Copyright (C) 2018-2022 VyOS maintainers and contributors # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License version 2 or later as # published by the Free Software Foundation. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. import os import re from copy import deepcopy from stat import S_IRUSR, S_IWUSR, S_IRGRP from sys import exit from vyos.config import Config from vyos.template import render from vyos.template import is_ipv4 from vyos.template import is_ipv6 from vyos.util import call, get_half_cpus from vyos import ConfigError from vyos import airbag airbag.enable() ipoe_conf = '/run/accel-pppd/ipoe.conf' ipoe_chap_secrets = '/run/accel-pppd/ipoe.chap-secrets' default_config_data = { 'auth_mode': 'local', 'auth_interfaces': [], 'chap_secrets_file': ipoe_chap_secrets, # used in Jinja2 template 'interfaces': [], 'dnsv4': [], 'dnsv6': [], 'client_named_ip_pool': [], 'client_ipv6_pool': [], 'client_ipv6_delegate_prefix': [], + 'gateway_address':[], 'radius_server': [], 'radius_acct_inter_jitter': '', 'radius_acct_tmo': '3', 'radius_max_try': '3', 'radius_timeout': '3', 'radius_nas_id': '', 'radius_nas_ip': '', 'radius_source_address': '', 'radius_shaper_attr': '', 'radius_shaper_enable': False, 'radius_shaper_multiplier': '', 'radius_shaper_vendor': '', 'radius_dynamic_author': '', 'thread_cnt': get_half_cpus() } def get_config(config=None): if config: conf = config else: conf = Config() base_path = ['service', 'ipoe-server'] if not conf.exists(base_path): return None conf.set_level(base_path) ipoe = deepcopy(default_config_data) for interface in conf.list_nodes(['interface']): tmp = { 'mode': 'L2', 'name': interface, 'shared': '1', # may need a config option, can be dhcpv4 or up for unclassified pkts 'sess_start': 'dhcpv4', 'range': None, 'ifcfg': '1', 'vlan_mon': [] } conf.set_level(base_path + ['interface', interface]) if conf.exists(['network-mode']): tmp['mode'] = conf.return_value(['network-mode']) if conf.exists(['network']): mode = conf.return_value(['network']) if mode == 'vlan': tmp['shared'] = '0' if conf.exists(['vlan-id']): tmp['vlan_mon'] += conf.return_values(['vlan-id']) if conf.exists(['vlan-range']): tmp['vlan_mon'] += conf.return_values(['vlan-range']) if conf.exists(['client-subnet']): tmp['range'] = conf.return_value(['client-subnet']) ipoe['interfaces'].append(tmp) conf.set_level(base_path) if conf.exists(['name-server']): for name_server in conf.return_values(['name-server']): if is_ipv4(name_server): ipoe['dnsv4'].append(name_server) else: ipoe['dnsv6'].append(name_server) if conf.exists(['authentication', 'mode']): ipoe['auth_mode'] = conf.return_value(['authentication', 'mode']) if conf.exists(['authentication', 'interface']): for interface in conf.list_nodes(['authentication', 'interface']): tmp = { 'name': interface, 'mac': [] } for mac in conf.list_nodes(['authentication', 'interface', interface, 'mac-address']): client = { 'address': mac, 'rate_download': '', 'rate_upload': '', 'vlan_id': '' } conf.set_level(base_path + ['authentication', 'interface', interface, 'mac-address', mac]) if conf.exists(['rate-limit', 'download']): client['rate_download'] = conf.return_value(['rate-limit', 'download']) if conf.exists(['rate-limit', 'upload']): client['rate_upload'] = conf.return_value(['rate-limit', 'upload']) if conf.exists(['vlan-id']): client['vlan'] = conf.return_value(['vlan-id']) tmp['mac'].append(client) ipoe['auth_interfaces'].append(tmp) conf.set_level(base_path) # # authentication mode radius servers and settings if conf.exists(['authentication', 'mode', 'radius']): for server in conf.list_nodes(['authentication', 'radius', 'server']): radius = { 'server' : server, 'key' : '', 'fail_time' : 0, 'port' : '1812', 'acct_port' : '1813' } conf.set_level(base_path + ['authentication', 'radius', 'server', server]) if conf.exists(['fail-time']): radius['fail_time'] = conf.return_value(['fail-time']) if conf.exists(['port']): radius['port'] = conf.return_value(['port']) if conf.exists(['acct-port']): radius['acct_port'] = conf.return_value(['acct-port']) if conf.exists(['key']): radius['key'] = conf.return_value(['key']) if not conf.exists(['disable']): ipoe['radius_server'].append(radius) # # advanced radius-setting conf.set_level(base_path + ['authentication', 'radius']) if conf.exists(['acct-interim-jitter']): ipoe['radius_acct_inter_jitter'] = conf.return_value(['acct-interim-jitter']) if conf.exists(['acct-timeout']): ipoe['radius_acct_tmo'] = conf.return_value(['acct-timeout']) if conf.exists(['max-try']): ipoe['radius_max_try'] = conf.return_value(['max-try']) if conf.exists(['timeout']): ipoe['radius_timeout'] = conf.return_value(['timeout']) if conf.exists(['nas-identifier']): ipoe['radius_nas_id'] = conf.return_value(['nas-identifier']) if conf.exists(['nas-ip-address']): ipoe['radius_nas_ip'] = conf.return_value(['nas-ip-address']) if conf.exists(['rate-limit', 'attribute']): ipoe['radius_shaper_attr'] = conf.return_value(['rate-limit', 'attribute']) if conf.exists(['rate-limit', 'enable']): ipoe['radius_shaper_enable'] = True if conf.exists(['rate-limit', 'multiplier']): ipoe['radius_shaper_multiplier'] = conf.return_value(['rate-limit', 'multiplier']) if conf.exists(['rate-limit', 'vendor']): ipoe['radius_shaper_vendor'] = conf.return_value(['rate-limit', 'vendor']) if conf.exists(['source-address']): ipoe['radius_source_address'] = conf.return_value(['source-address']) # Dynamic Authorization Extensions (DOA)/Change Of Authentication (COA) if conf.exists(['dynamic-author']): dae = { 'port' : '', 'server' : '', 'key' : '' } if conf.exists(['dynamic-author', 'server']): dae['server'] = conf.return_value(['dynamic-author', 'server']) if conf.exists(['dynamic-author', 'port']): dae['port'] = conf.return_value(['dynamic-author', 'port']) if conf.exists(['dynamic-author', 'key']): dae['key'] = conf.return_value(['dynamic-author', 'key']) ipoe['radius_dynamic_author'] = dae conf.set_level(base_path) # Named client-ip-pool if conf.exists(['client-ip-pool', 'name']): for name in conf.list_nodes(['client-ip-pool', 'name']): tmp = { 'name': name, 'gateway_address': '', 'subnet': '' } if conf.exists(['client-ip-pool', 'name', name, 'gateway-address']): tmp['gateway_address'] += conf.return_value(['client-ip-pool', 'name', name, 'gateway-address']) if conf.exists(['client-ip-pool', 'name', name, 'subnet']): tmp['subnet'] += conf.return_value(['client-ip-pool', 'name', name, 'subnet']) ipoe['client_named_ip_pool'].append(tmp) if conf.exists(['client-ipv6-pool', 'prefix']): for prefix in conf.list_nodes(['client-ipv6-pool', 'prefix']): tmp = { 'prefix': prefix, 'mask': '64' } if conf.exists(['client-ipv6-pool', 'prefix', prefix, 'mask']): tmp['mask'] = conf.return_value(['client-ipv6-pool', 'prefix', prefix, 'mask']) ipoe['client_ipv6_pool'].append(tmp) if conf.exists(['client-ipv6-pool', 'delegate']): for prefix in conf.list_nodes(['client-ipv6-pool', 'delegate']): tmp = { 'prefix': prefix, 'mask': '' } if conf.exists(['client-ipv6-pool', 'delegate', prefix, 'delegation-prefix']): tmp['mask'] = conf.return_value(['client-ipv6-pool', 'delegate', prefix, 'delegation-prefix']) ipoe['client_ipv6_delegate_prefix'].append(tmp) + if conf.exists(['gateway-address']): + for gw in conf.return_values(['gateway-address']): + ipoe['gateway_address'].append(gw) + return ipoe + def verify(ipoe): if not ipoe: return None if not ipoe['interfaces']: raise ConfigError('No IPoE interface configured') if len(ipoe['dnsv4']) > 2: raise ConfigError('Not more then two IPv4 DNS name-servers can be configured') if len(ipoe['dnsv6']) > 3: raise ConfigError('Not more then three IPv6 DNS name-servers can be configured') if ipoe['auth_mode'] == 'radius': if len(ipoe['radius_server']) == 0: raise ConfigError('RADIUS authentication requires at least one server') for radius in ipoe['radius_server']: if not radius['key']: server = radius['server'] raise ConfigError(f'Missing RADIUS secret key for server "{ server }"') if ipoe['client_ipv6_delegate_prefix'] and not ipoe['client_ipv6_pool']: raise ConfigError('IPoE IPv6 deletate-prefix requires IPv6 prefix to be configured!') + if ipoe['gateway_address']: + if ipoe['client_named_ip_pool']: + ipoe_gateways = ' '.join(ipoe['gateway_address']) + for pool in ipoe['client_named_ip_pool']: + if f'{pool["gateway_address"]}/' in ipoe_gateways: + raise ConfigError( + 'IPoE "gateway-address" exists in IPoE "client-ip-pool"!') return None def generate(ipoe): if not ipoe: return None render(ipoe_conf, 'accel-ppp/ipoe.config.tmpl', ipoe) if ipoe['auth_mode'] == 'local': render(ipoe_chap_secrets, 'accel-ppp/chap-secrets.ipoe.tmpl', ipoe) os.chmod(ipoe_chap_secrets, S_IRUSR | S_IWUSR | S_IRGRP) else: if os.path.exists(ipoe_chap_secrets): os.unlink(ipoe_chap_secrets) return None def apply(ipoe): if ipoe == None: call('systemctl stop accel-ppp@ipoe.service') for file in [ipoe_conf, ipoe_chap_secrets]: if os.path.exists(file): os.unlink(file) return None call('systemctl restart accel-ppp@ipoe.service') if __name__ == '__main__': try: c = get_config() verify(c) generate(c) apply(c) except ConfigError as e: print(e) exit(1)