Coverage for birdplan/bird_config/sections/constants.py: 94%
71 statements
« prev ^ index » next coverage.py v7.4.4, created at 2024-04-23 03:27 +0000
« prev ^ index » next coverage.py v7.4.4, created at 2024-04-23 03:27 +0000
1#
2# SPDX-License-Identifier: GPL-3.0-or-later
3#
4# Copyright (c) 2019-2024, AllWorldIT
5#
6# This program is free software: you can redistribute it and/or modify
7# it under the terms of the GNU General Public License as published by
8# the Free Software Foundation, either version 3 of the License, or
9# (at your option) any later version.
10#
11# This program is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14# GNU General Public License for more details.
15#
16# You should have received a copy of the GNU General Public License
17# along with this program. If not, see <http://www.gnu.org/licenses/>.
19"""BIRD constants configuration."""
21from ..globals import BirdConfigGlobals
22from .base import SectionBase
24__all__ = ["SectionConstants"]
27class SectionConstants(SectionBase):
28 """BIRD constants configuration."""
30 _need_bogons: bool
32 def __init__(self, birdconfig_globals: BirdConfigGlobals):
33 """Initialize the object."""
34 super().__init__(birdconfig_globals)
36 # Set section header
37 self._section = "Global Constants"
39 # Add bogon constants to output
40 self._need_bogons = False
42 def configure(self) -> None:
43 """Configure global constants."""
44 super().configure()
46 # Check if we're in debug mode or not
47 if self.birdconfig_globals.debug:
48 self.conf.add("# We're in DEBUG mode")
49 self.conf.add("define DEBUG = true;")
50 else:
51 self.conf.add("# We're not in DEBUG mode")
52 self.conf.add("define DEBUG = false;")
54 self._configure_defaults()
55 # Check if we're adding bogons constants
56 if self.need_bogons:
57 self._configure_bogons_ipv4()
58 self._configure_bogons_ipv6()
60 def _configure_defaults(self) -> None:
61 """Configure default routes."""
62 self.conf.add("# Default routes")
63 self.conf.add("define DEFAULT_ROUTE_V4 = 0.0.0.0/0; # IPv4 default route")
64 self.conf.add("define DEFAULT_ROUTE_V6 = ::/0; # IPv6 default route")
65 self.conf.add("")
67 def _configure_bogons_ipv4(self) -> None:
68 """Configure IPv4 bogons."""
69 self.conf.add("# As per http://bgpfilterguide.nlnog.net/guides/bogon_prefixes/")
70 self.conf.add("define BOGONS_V4 = [")
71 self.conf.add(" 0.0.0.0/8+, # RFC 1122 'this' network")
72 self.conf.add(" 10.0.0.0/8+, # RFC 1918 private space")
73 if self.birdconfig_globals.test_mode:
74 self.conf.add(" # EXCLUDING DUE TO TESTING: 100.64.0.0/10+, # RFC 6598 Carrier grade nat space")
75 else:
76 self.conf.add(" 100.64.0.0/10+, # RFC 6598 Carrier grade nat space")
77 self.conf.add(" 127.0.0.0/8+, # RFC 1122 localhost")
78 self.conf.add(" 169.254.0.0/16+, # RFC 3927 link local")
79 self.conf.add(" 172.16.0.0/12+, # RFC 1918 private space")
80 self.conf.add(" 192.0.2.0/24+, # RFC 5737 TEST-NET-1")
81 self.conf.add(" 192.88.99.0/24+, # RFC 7526 6to4 anycast relay")
82 self.conf.add(" 192.168.0.0/16+, # RFC 1918 private space")
83 self.conf.add(" 198.18.0.0/15+, # RFC 2544 benchmarking")
84 self.conf.add(" 198.51.100.0/24+, # RFC 5737 TEST-NET-2")
85 self.conf.add(" 203.0.113.0/24+, # RFC 5737 TEST-NET-3")
86 self.conf.add(" 224.0.0.0/4+, # multicast")
87 self.conf.add(" 240.0.0.0/4+ # reserved")
88 self.conf.add("];")
89 self.conf.add("")
91 def _configure_bogons_ipv6(self) -> None:
92 """Configure IPv6 bogons."""
93 self.conf.add("# As per http://bgpfilterguide.nlnog.net/guides/bogon_prefixes/")
94 self.conf.add("define BOGONS_V6 = [")
95 self.conf.add(" ::/8+, # RFC 4291 IPv4-compatible, loopback, et al")
96 self.conf.add(" 0100::/64+, # RFC 6666 Discard-Only")
97 self.conf.add(" 2001:2::/48+, # RFC 5180 BMWG")
98 self.conf.add(" 2001:10::/28+, # RFC 4843 ORCHID")
99 self.conf.add(" 2001:db8::/32+, # RFC 3849 documentation")
100 self.conf.add(" 2002::/16+, # RFC 7526 6to4 anycast relay")
101 self.conf.add(" 3ffe::/16+, # RFC 3701 old 6bone")
102 if self.birdconfig_globals.test_mode:
103 self.conf.add(" # EXCLUDING DUE TO TESTING: fc00::/7+, # RFC 4193 unique local unicast")
104 else:
105 self.conf.add(" fc00::/7+, # RFC 4193 unique local unicast")
106 self.conf.add(" fe80::/10+, # RFC 4291 link local unicast")
107 self.conf.add(" fec0::/10+, # RFC 3879 old site local unicast")
108 self.conf.add(" ff00::/8+ # RFC 4291 multicast")
109 self.conf.add("];")
110 self.conf.add("")
112 @property
113 def need_bogons(self) -> bool:
114 """Return if we need bogons or not."""
115 return self._need_bogons
117 @need_bogons.setter
118 def need_bogons(self, need_bogons: bool) -> None:
119 """Add bogons to our output."""
120 self._need_bogons = need_bogons