summaryrefslogtreecommitdiff
path: root/src/conf_mode/firewall_options.py
blob: 90f004bc49fef528e632f2f04c5bb7bbfab7da73 (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
#!/usr/bin/env python3
#
# Copyright (C) 2018 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 sys
import os
import copy

from vyos.config import Config
from vyos import ConfigError
from vyos.util import run


default_config_data = {
    'intf_opts': [],
    'new_chain4': False,
    'new_chain6': False
}

def get_config():
    opts = copy.deepcopy(default_config_data)
    conf = Config()
    if not conf.exists('firewall options'):
        # bail out early
        return opts
    else:
        conf.set_level('firewall options')

        # Parse configuration of each individual instance
        if conf.exists('interface'):
            for intf in conf.list_nodes('interface'):
                conf.set_level('firewall options interface {0}'.format(intf))
                config = {
                    'intf': intf,
                    'disabled': False,
                    'mss4': '',
                    'mss6': ''
                }

                # Check if individual option is disabled
                if conf.exists('disable'):
                    config['disabled'] = True

                #
                # Get MSS value IPv4
                #
                if conf.exists('adjust-mss'):
                    config['mss4'] = conf.return_value('adjust-mss')

                    # We need a marker that a new iptables chain needs to be generated
                    if not opts['new_chain4']:
                        opts['new_chain4'] = True

                #
                # Get MSS value IPv6
                #
                if conf.exists('adjust-mss6'):
                    config['mss6'] = conf.return_value('adjust-mss6')

                    # We need a marker that a new ip6tables chain needs to be generated
                    if not opts['new_chain6']:
                        opts['new_chain6'] = True

                # Append interface options to global list
                opts['intf_opts'].append(config)

    return opts

def verify(tcp):
    # syntax verification is done via cli
    return None

def apply(tcp):
    target = 'VYOS_FW_OPTIONS'

    # always cleanup iptables
    run('iptables --table mangle --delete FORWARD --jump {} >&/dev/null'.format(target))
    run('iptables --table mangle --flush {} >&/dev/null'.format(target))
    run('iptables --table mangle --delete-chain {} >&/dev/null'.format(target))

    # always cleanup ip6tables
    run('ip6tables --table mangle --delete FORWARD --jump {} >&/dev/null'.format(target))
    run('ip6tables --table mangle --flush {} >&/dev/null'.format(target))
    run('ip6tables --table mangle --delete-chain {} >&/dev/null'.format(target))

    # Setup new iptables rules
    if tcp['new_chain4']:
        run('iptables --table mangle --new-chain {} >&/dev/null'.format(target))
        run('iptables --table mangle --append FORWARD --jump {} >&/dev/null'.format(target))

        for opts in tcp['intf_opts']:
            intf = opts['intf']
            mss = opts['mss4']

            # Check if this rule iis disabled
            if opts['disabled']:
                continue

            # adjust TCP MSS per interface
            if mss:
                run('iptables --table mangle --append {} --out-interface {} --protocol tcp ' \
                          '--tcp-flags SYN,RST SYN --jump TCPMSS --set-mss {} >&/dev/null'.format(target, intf, mss))

    # Setup new ip6tables rules
    if tcp['new_chain6']:
        run('ip6tables --table mangle --new-chain {} >&/dev/null'.format(target))
        run('ip6tables --table mangle --append FORWARD --jump {} >&/dev/null'.format(target))

        for opts in tcp['intf_opts']:
            intf = opts['intf']
            mss = opts['mss6']

            # Check if this rule iis disabled
            if opts['disabled']:
                continue

            # adjust TCP MSS per interface
            if mss:
                run('ip6tables --table mangle --append {} --out-interface {} --protocol tcp '
                    '--tcp-flags SYN,RST SYN --jump TCPMSS --set-mss {} >&/dev/null'.format(target, intf, mss))

    return None

if __name__ == '__main__':

    try:
        c = get_config()
        verify(c)
        apply(c)
    except ConfigError as e:
        print(e)
        sys.exit(1)