aboutsummaryrefslogtreecommitdiff
path: root/localsubnetsetd
blob: d955cca72b268d11b3827509763a667fbccfb38f (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
#!/usr/bin/python3
# Copyright 2021 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
# use this file except in compliance with the License. You may obtain a copy of
# the License at
#
#     https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations under
# the License.

from socket import AF_INET, AF_INET6

import nftables
import pyroute2


def send_command(nft, s):
    '''Sends a command to nftables.

    Sends the specified command string to the specified nftables instance. Waits
    for acknowledgement. Raises RuntimeError if the command fails.
    '''
    print(s)
    failed, stdout, stderr = nft.cmd(s)
    if failed:
        raise RuntimeError('nftables command failed: %s' % stderr)


def set_name(family):
    '''Generates the relevant nftables set name for the specified protocol.'''
    if family == AF_INET:
        return 'local_subnets4'
    elif family == AF_INET6:
        return 'local_subnets6'
    raise ValueError("don't have a set for family %d" % family)


def flush(family):
    '''Creates an nftables command to flush the local subnet set.'''
    return 'flush set inet filter %s' % set_name(family)


def add(msg):
    '''Creates an nftables command to add a subnet.

    Processes a netlink message to extract its subnet, and creates an nftables
    command to add that subnet to the appropriate set.
    '''
    for k, v in msg['attrs']:
        if k == 'IFA_ADDRESS':
            subnet = v + '/' + str(msg['prefixlen'])
            break
    else:
        raise ValueError("message lacks 'IFA_ADDRESS' attribute")

    return 'add element inet filter %s { %s }' % (set_name(
        msg['family']), subnet)


def add_current(netlink, family=None):
    '''Creates an nftables command to add all current subnets.'''
    r = []
    for msg in netlink.get_addr():
        if family and msg['family'] != family:
            continue
        r.append(add(msg))
    return '; '.join(r)


def maintain_sets(nft, netlink):
    # Start listening for netlink messages now so we don't miss any while
    # we're querying the current IP address set.
    netlink.bind()

    # Populate the sets with the initial set of IP addresses.
    send_command(
        nft, '; '.join([flush(AF_INET),
                        flush(AF_INET6),
                        add_current(netlink)]))

    # Watch for changes.
    while True:
        for msg in netlink.get():
            if msg['event'] == 'RTM_NEWADDR':
                send_command(nft, add(msg))
            elif msg['event'] == 'RTM_DELADDR':
                # We might still be holding another address on the same subnet,
                # so we can't just delete this subnet. Rebuild the subnet set
                # from scratch.
                fam = msg['family']
                send_command(nft,
                             flush(fam) + '; ' + add_current(netlink, fam))


if __name__ == '__main__':
    nft = nftables.Nftables()
    with pyroute2.IPRoute() as netlink:
        maintain_sets(nft, netlink)