diff options
author | Christian Poessinger <christian@poessinger.com> | 2019-11-29 22:07:05 +0100 |
---|---|---|
committer | Christian Poessinger <christian@poessinger.com> | 2020-02-23 14:36:56 +0100 |
commit | 38f31c805d7c79df7405a7b65d6775a4d762eb90 (patch) | |
tree | da7e62070e126a2cc02bd74eab47671d908d5d86 /src | |
parent | ceb4031b7647de742be078918085732783fad78d (diff) | |
download | vyos-1x-38f31c805d7c79df7405a7b65d6775a4d762eb90.tar.gz vyos-1x-38f31c805d7c79df7405a7b65d6775a4d762eb90.zip |
pppoe: T1318: add first version of new XML/Python implementation
vyos@vyos# show interfaces pppoe
pppoe pppoe0 {
default-route force
link eth2.7
mtu 400
name-server auto
password 12345678
user-id vyos@vyos.io
}
Diffstat (limited to 'src')
-rwxr-xr-x | src/conf_mode/interfaces-pppoe.py | 293 | ||||
-rwxr-xr-x | src/migration-scripts/interfaces/4-to-5 | 105 |
2 files changed, 398 insertions, 0 deletions
diff --git a/src/conf_mode/interfaces-pppoe.py b/src/conf_mode/interfaces-pppoe.py new file mode 100755 index 000000000..eb64d094b --- /dev/null +++ b/src/conf_mode/interfaces-pppoe.py @@ -0,0 +1,293 @@ +#!/usr/bin/env python3 +# +# Copyright (C) 2019 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 + +from sys import exit +from copy import deepcopy + +from jinja2 import Template +from psutil import pid_exists +from stat import S_IRUSR, S_IWUSR, S_IXUSR, S_IRGRP, S_IXGRP, S_IROTH, S_IXOTH +from subprocess import Popen, PIPE + +from vyos.config import Config +from vyos import ConfigError +from netifaces import interfaces + +# Please be careful if you edit the template. +config_pppoe_tmpl = """ +### Autogenerated by interfaces-pppoe.py ### + +# Configuration file for PPP, using PPP over Ethernet (PPPOE) +# to connect to a DSL provider. + +# Default parameters not set by Vyatta templates: +# +# Require peer to provide the local IP address if it is not +# specified explicitly in the config file. +noipdefault + +# Don't show the password in logfiles: +hide-password + +# Standard Link Control Protocol (LCP) parameters: +lcp-echo-interval 20 +lcp-echo-failure 3 + +# RFC 2516, paragraph 7 mandates that the following options MUST NOT be +# requested and MUST be rejected if requested by the peer: +# Address-and-Control-Field-Compression (ACFC) +noaccomp + +# Asynchronous-Control-Character-Map (ACCM) +default-asyncmap + +# Override any connect script that may have been set in /etc/ppp/options. +connect /bin/true + +# Don't try to authenticate the remote node +noauth + +# Don't try to proxy ARP for the remote endpoint. User can set proxy +# arp entries up manually if they wish. More importantly, having +# the "proxyarp" parameter set disables the "defaultroute" option. +noproxyarp + +plugin rp-pppoe.so +{{ link }} +persist +ifname {{ intf }} +ipparam {{ intf }} +debug +logfile /var/log/vyatta/ppp_{{ intf }}.log +{% if 'auto' in default_route -%} +defaultroute +{% elif 'force' in default_route -%} +defaultroute +replacedefaultroute +{% endif %} +mtu {{ mtu }} +mru {{ mtu }} +user "{{ user_id }}" +password "{{ password }}" +{% if 'auto' in name_server -%} +usepeerdns +{% endif %} +{% if ipv6_enable -%} ++ipv6 +{% endif %} + +""" + +config_pppoe_autoconf_tmpl = """ +#!/bin/sh + +if [ "$6" != "{{ intf }}" ]; then + exit +fi + +echo 0 > /proc/sys/net/ipv6/conf/$1/forwarding +echo 2 > /proc/sys/net/ipv6/conf/$1/accept_ra +echo 1 > /proc/sys/net/ipv6/conf/$1/autoconf +""" + +default_config_data = { + 'access_concentrator': '', + 'on_demand': False, + 'default_route': 'auto', + 'deleted': False, + 'disable': False, + 'intf': '', + 'idle_timeout': '', + 'ipv6_autoconf': False, + 'ipv6_enable': False, + 'link': '', + 'local_address': '', + 'mtu': '1492', + 'name_server': 'auto', + 'password': '', + 'remote_address': '', + 'service_name': '', + 'user_id': '' +} + +def subprocess_cmd(command): + print(command) + p = Popen(command, stdout=PIPE, shell=True) + p.communicate() + +def get_config(): + pppoe = deepcopy(default_config_data) + conf = Config() + + # determine tagNode instance + try: + pppoe['intf'] = os.environ['VYOS_TAGNODE_VALUE'] + except KeyError as E: + print("Interface not specified") + + # Check if interface has been removed + if not conf.exists('interfaces pppoe ' + pppoe['intf']): + pppoe['deleted'] = True + return pppoe + + # set new configuration level + conf.set_level('interfaces pppoe ' + pppoe['intf']) + + # Access concentrator name (only connect to this concentrator) + if conf.exists('access-concentrator'): + pppoe['access_concentrator'] = conf.return_values('access-concentrator') + + # Access concentrator name (only connect to this concentrator) + if conf.exists('connect-on-demand'): + pppoe['on_demand'] = True + + # Enable/Disable default route to peer when link comes up + if conf.exists('default-route'): + pppoe['default_route'] = conf.return_value('default-route') + + # Disable this interface + if conf.exists('disable'): + pppoe['disable'] = True + + # Delay before disconnecting idle session (in seconds) + if conf.exists('idle-timeout'): + pppoe['idle_timeout'] = conf.return_value('idle-timeout') + + # Enable Stateless Address Autoconfiguration (SLAAC) + if conf.exists('ipv6 address autoconf'): + pppoe['ipv6_autoconf'] = True + + # Activate IPv6 support on this connection + if conf.exists('ipv6 enable'): + pppoe['ipv6_enable'] = True + + # IPv4 address of local end of the PPPoE link + if conf.exists('local-address'): + pppoe['local_address'] = conf.return_value('local-address') + + # Physical Interface used for this PPPoE session + if conf.exists('link'): + pppoe['link'] = conf.return_value('link') + + # Maximum Transmission Unit (MTU) + if conf.exists('mtu'): + pppoe['mtu'] = conf.return_value('mtu') + + # IPv4 address of local end of the PPPoE link + if conf.exists('name-server'): + pppoe['name_server'] = conf.return_value('name-server') + + # Password for authenticating local machine to PPPoE server + if conf.exists('password'): + pppoe['password'] = conf.return_value('password') + + # IPv4 address of local end of the PPPoE link + if conf.exists('remote-address'): + pppoe['remote_address'] = conf.return_value('remote-address') + + # Service name, only connect to access concentrators advertising this + if conf.exists('service-name'): + pppoe['service_name'] = conf.return_value('service-name') + + # Authentication name supplied to PPPoE server + if conf.exists('user-id'): + pppoe['user_id'] = conf.return_value('user-id') + + return pppoe + + +def verify(pppoe): + if pppoe['deleted']: + # bail out early + return None + + return None + + +def generate(pppoe): + config_file_pppoe = '/etc/ppp/peers/{}'.format(pppoe['intf']) + config_file_ifup = '/etc/ppp/ipv6-up.d/50-vyos-{}-autoconf'.format(pppoe['intf']) + + pid = 0 + pidfile = '/var/run/{}.pid'.format(pppoe['intf']) + if os.path.isfile(pidfile): + pid = 0 + with open(pidfile, 'r') as f: + pid = int(f.read()) + + # Always stop OpenVPN service. We can not send a SIGUSR1 for restart of the + # service as the configuration is not re-read. Stop daemon only if it's + # running - it could have died or killed by someone evil + print("pid: {}".format(pid)) + if pid_exists(pid): + cmd = 'start-stop-daemon --stop --quiet' + cmd += ' --pidfile ' + pidfile + subprocess_cmd(cmd) + + if pppoe['deleted']: + # Delete PPP configuration files + if os.path.exists(config_file_pppoe): + os.unlink(config_file_pppoe) + + if os.path.exists(config_file_ifup): + os.unlink(config_file_ifup) + + else: + # Create PPP configuration files + tmpl = Template(config_pppoe_tmpl) + config_text = tmpl.render(pppoe) + with open(config_file_pppoe, 'w') as f: + f.write(config_text) + + tmpl = Template(config_pppoe_autoconf_tmpl) + config_text = tmpl.render(pppoe) + with open(config_file_ifup, 'w') as f: + f.write(config_text) + + os.chmod(config_file_ifup, + S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH) + + if not pppoe['disable']: + # No matching PPP process running - spawn a new one + cmd = 'start-stop-daemon --start --quiet' + cmd += ' --pidfile ' + pidfile + cmd += ' --background' + cmd += ' --exec /usr/sbin/pppd' + # now pass arguments to pppd binary + cmd += ' -- ' + cmd += ' call {}'.format(pppoe['intf']) + + # execute assembled command + subprocess_cmd(cmd) + + return None + + +def apply(pppoe): + return None + + +if __name__ == '__main__': + try: + c = get_config() + verify(c) + generate(c) + apply(c) + except ConfigError as e: + print(e) + exit(1) diff --git a/src/migration-scripts/interfaces/4-to-5 b/src/migration-scripts/interfaces/4-to-5 new file mode 100755 index 000000000..a8f9addcf --- /dev/null +++ b/src/migration-scripts/interfaces/4-to-5 @@ -0,0 +1,105 @@ +#!/usr/bin/env python3 + +# Change syntax of wireless interfaces +# Migrate boolean nodes to valueless + +from sys import exit +from vyos.configtree import ConfigTree + +def migrate_dialer(config, tree, intf): + for pppoe in config.list_nodes(tree): + # assemble string, 0 -> pppoe0 + new_base = ['interfaces', 'pppoe', 'pppoe' + pppoe] + config.set(new_base) + # format as tag node to avoid loading problems + config.set_tag(['interfaces', 'pppoe']) + + default_route = tree + [pppoe, 'default-route'] + print(default_route) + if config.exists(default_route): + tmp = config.return_value(default_route) + config.set(new_base + ['default-route'], value=tmp) + + disable = tree + [pppoe, 'disable'] + if config.exists(disable): + tmp = config.return_value(disable) + config.set(new_base + ['disable'], value=tmp) + + mtu = tree + [pppoe, 'mtu'] + if config.exists(mtu): + tmp = config.return_value(mtu) + config.set(new_base + ['mtu'], value=tmp) + + nameserver = tree + [pppoe, 'name-server'] + if config.exists(nameserver): + tmp = config.return_value(nameserver) + config.set(new_base + ['name-server'], value=tmp) + + password = tree + [pppoe, 'password'] + if config.exists(password): + tmp = config.return_value(password) + config.set(new_base + ['password'], value=tmp) + + userid = tree + [pppoe, 'user-id'] + if config.exists(userid): + tmp = config.return_value(userid) + config.set(new_base + ['user-id'], value=tmp) + + ipv6_enable = tree + [pppoe, 'enable-ipv6'] + if config.exists(ipv6_enable): + config.set(new_base + ['ipv6', 'enable']) + + ipv6_slaac = tree + [pppoe, 'ipv6', 'address', 'autoconf'] + if config.exists(ipv6_slaac): + config.set(new_base + ['ipv6', 'address', 'autoconf']) + + # Delete old PPPoE interface + config.delete(tree + [pppoe]) + + +if __name__ == '__main__': + + if (len(sys.argv) < 1): + print("Must specify file name!") + exit(1) + + file_name = sys.argv[1] + + with open(file_name, 'r') as f: + config_file = f.read() + + config = ConfigTree(config_file) + pppoe_links = ['bonding', 'ethernet'] + + for link_type in pppoe_links: + if not config.exists(['interfaces', link_type]): + continue + + for interface in config.list_nodes(['interfaces', link_type]): + # check if PPPoE exists + pppoe_if = ['interfaces', link_type, interface, 'pppoe'] + if config.exists(pppoe_if): + for dialer in config.list_nodes(pppoe_if): + migrate_dialer(config, pppoe_if, interface) + + # also migrate VLANs + if not config.exists(['interfaces', link_type, interface, 'vif']): + continue + + for vlan in config.list_nodes(['interfaces', link_type, interface, 'vif']): + pppoe_if = ['interfaces', link_type, interface, 'vif', vlan, 'pppoe'] + if config.exists(pppoe_if): + for dialer in config.list_nodes(pppoe_if): + intf = "{}.{}".format(interface, vlan) + migrate_dialer(config, pppoe_if, intf) + + + print(config.to_string()) + exit(1) + + try: + with open(file_name, 'w') as f: + f.write(config.to_string()) + except OSError as e: + print("Failed to save the modified config: {}".format(e)) + exit(1) |