1#   Copyright 2020 - The Android Open Source Project
2#
3#   Licensed under the Apache License, Version 2.0 (the "License");
4#   you may not use this file except in compliance with the License.
5#   You may obtain a copy of the License at
6#
7#       http://www.apache.org/licenses/LICENSE-2.0
8#
9#   Unless required by applicable law or agreed to in writing, software
10#   distributed under the License is distributed on an "AS IS" BASIS,
11#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12#   See the License for the specific language governing permissions and
13#   limitations under the License.
14
15import collections
16
17
18class RadvdConfig(object):
19    """The root settings for the router advertisement daemon.
20
21    All the settings for a router advertisement daemon.
22    """
23    def __init__(self,
24                 prefix,
25                 clients=[],
26                 route=None,
27                 rdnss=[],
28                 ignore_if_missing=None,
29                 adv_send_advert=None,
30                 unicast_only=None,
31                 max_rtr_adv_interval=None,
32                 min_rtr_adv_interval=None,
33                 min_delay_between_ras=None,
34                 adv_managed_flag=None,
35                 adv_other_config_flag=None,
36                 adv_link_mtu=None,
37                 adv_reachable_time=None,
38                 adv_retrans_timer=None,
39                 adv_cur_hop_limit=None,
40                 adv_default_lifetime=None,
41                 adv_default_preference=None,
42                 adv_source_ll_address=None,
43                 adv_home_agent_flag=None,
44                 adv_home_agent_info=None,
45                 home_agent_lifetime=None,
46                 home_agent_preference=None,
47                 adv_mob_rtr_support_flag=None,
48                 adv_interval_opt=None,
49                 adv_on_link=None,
50                 adv_autonomous=None,
51                 adv_router_addr=None,
52                 adv_valid_lifetime=None,
53                 adv_preferred_lifetime=None,
54                 base_6to4_interface=None,
55                 adv_route_lifetime=None,
56                 adv_route_preference=None,
57                 adv_rdnss_preference=None,
58                 adv_rdnss_open=None,
59                 adv_rdnss_lifetime=None):
60        """Construct a RadvdConfig.
61
62        Args:
63            prefix: IPv6 prefix and length, ie fd::/64
64            clients: A list of IPv6 link local addresses that will be the only
65                clients served.  All other IPv6 addresses will be ignored if
66                this list is present.
67            route: A route for the router advertisement with prefix.
68            rdnss: A list of recursive DNS servers
69            ignore_if_missing: A flag indicating whether or not the interface
70                is ignored if it does not exist at start-up. By default,
71                radvd exits.
72            adv_send_advert: A flag indicating whether or not the router sends
73                periodic router advertisements and responds to router
74                solicitations.
75            unicast_only: Indicates that the interface link type only supports
76                unicast.
77            max_rtr_adv_interval:The maximum time allowed between sending
78                unsolicited multicast router advertisements from the interface,
79                in seconds. Must be no less than 4 seconds and no greater than
80                1800 seconds.
81            min_rtr_adv_interval: The minimum time allowed between sending
82                unsolicited multicast router advertisements from the interface,
83                in seconds. Must be no less than 3 seconds and no greater than
84                0.75 * max_rtr_adv_interval.
85            min_delay_between_ras: The minimum time allowed between sending
86                multicast router advertisements from the interface, in seconds.,
87            adv_managed_flag: When set, hosts use the administered (stateful)
88                protocol for address autoconfiguration in addition to any
89                addresses autoconfigured using stateless address
90                autoconfiguration. The use of this flag is described in
91                RFC 4862.
92            adv_other_config_flag: When set, hosts use the administered
93                (stateful) protocol for autoconfiguration of other (non-address)
94                information. The use of this flag is described in RFC 4862.
95            adv_link_mtu: The MTU option is used in router advertisement
96                messages to insure that all nodes on a link use the same MTU
97                value in those cases where the link MTU is not well known.
98            adv_reachable_time: The time, in milliseconds, that a node assumes
99                a neighbor is reachable after having received a reachability
100                confirmation. Used by the Neighbor Unreachability Detection
101                algorithm (see Section 7.3 of RFC 4861). A value of zero means
102                unspecified (by this router).
103            adv_retrans_timer: The time, in milliseconds, between retransmitted
104                Neighbor Solicitation messages. Used by address resolution and
105                the Neighbor Unreachability Detection algorithm (see Sections
106                7.2 and 7.3 of RFC 4861). A value of zero means unspecified
107                (by this router).
108            adv_cur_hop_limit: The default value that should be placed in the
109                Hop Count field of the IP header for outgoing (unicast) IP
110                packets. The value should be set to the current diameter of the
111                Internet. The value zero means unspecified (by this router).
112            adv_default_lifetime: The lifetime associated with the default
113                router in units of seconds. The maximum value corresponds to
114                18.2 hours. A lifetime of 0 indicates that the router is not a
115                default router and should not appear on the default router list.
116                The router lifetime applies only to the router's usefulness as
117                a default router; it does not apply to information contained in
118                other message fields or options. Options that need time limits
119                for their information include their own lifetime fields.
120            adv_default_preference: The preference associated with the default
121                router, as either "low", "medium", or "high".
122            adv_source_ll_address: When set, the link-layer address of the
123                outgoing interface is included in the RA.
124            adv_home_agent_flag: When set, indicates that sending router is able
125                to serve as Mobile IPv6 Home Agent. When set, minimum limits
126                specified by Mobile IPv6 are used for MinRtrAdvInterval and
127                MaxRtrAdvInterval.
128            adv_home_agent_info: When set, Home Agent Information Option
129                (specified by Mobile IPv6) is included in Router Advertisements.
130                adv_home_agent_flag must also be set when using this option.
131            home_agent_lifetime: The length of time in seconds (relative to the
132                time the packet is sent) that the router is offering Mobile IPv6
133                 Home Agent services. A value 0 must not be used. The maximum
134                 lifetime is 65520 seconds (18.2 hours). This option is ignored,
135                 if adv_home_agent_info is not set.
136            home_agent_preference: The preference for the Home Agent sending
137                this Router Advertisement. Values greater than 0 indicate more
138                preferable Home Agent, values less than 0 indicate less
139                preferable Home Agent. This option is ignored, if
140                adv_home_agent_info is not set.
141            adv_mob_rtr_support_flag: When set, the Home Agent signals it
142                supports Mobile Router registrations (specified by NEMO Basic).
143                adv_home_agent_info must also be set when using this option.
144            adv_interval_opt: When set, Advertisement Interval Option
145                (specified by Mobile IPv6) is included in Router Advertisements.
146                When set, minimum limits specified by Mobile IPv6 are used for
147                MinRtrAdvInterval and MaxRtrAdvInterval.
148            adv_on_linkWhen set, indicates that this prefix can be used for
149                on-link determination. When not set the advertisement makes no
150                statement about on-link or off-link properties of the prefix.
151                For instance, the prefix might be used for address configuration
152                 with some of the addresses belonging to the prefix being
153                 on-link and others being off-link.
154            adv_autonomous: When set, indicates that this prefix can be used for
155                autonomous address configuration as specified in RFC 4862.
156            adv_router_addr: When set, indicates that the address of interface
157                is sent instead of network prefix, as is required by Mobile
158                IPv6. When set, minimum limits specified by Mobile IPv6 are used
159                for MinRtrAdvInterval and MaxRtrAdvInterval.
160            adv_valid_lifetime: The length of time in seconds (relative to the
161                time the packet is sent) that the prefix is valid for the
162                purpose of on-link determination. The symbolic value infinity
163                represents infinity (i.e. a value of all one bits (0xffffffff)).
164                 The valid lifetime is also used by RFC 4862.
165            adv_preferred_lifetimeThe length of time in seconds (relative to the
166                time the packet is sent) that addresses generated from the
167                prefix via stateless address autoconfiguration remain preferred.
168                The symbolic value infinity represents infinity (i.e. a value of
169                all one bits (0xffffffff)). See RFC 4862.
170            base_6to4_interface: If this option is specified, this prefix will
171                be combined with the IPv4 address of interface name to produce
172                a valid 6to4 prefix. The first 16 bits of this prefix will be
173                replaced by 2002 and the next 32 bits of this prefix will be
174                replaced by the IPv4 address assigned to interface name at
175                configuration time. The remaining 80 bits of the prefix
176                (including the SLA ID) will be advertised as specified in the
177                configuration file.
178            adv_route_lifetime: The lifetime associated with the route in units
179                of seconds. The symbolic value infinity represents infinity
180                (i.e. a value of all one bits (0xffffffff)).
181            adv_route_preference: The preference associated with the default
182                router, as either "low", "medium", or "high".
183            adv_rdnss_preference: The preference of the DNS server, compared to
184                other DNS servers advertised and used. 0 to 7 means less
185                important than manually configured nameservers in resolv.conf,
186                while 12 to 15 means more important.
187            adv_rdnss_open: "Service Open" flag. When set, indicates that RDNSS
188                continues to be available to hosts even if they moved to a
189                different subnet.
190            adv_rdnss_lifetime: The maximum duration how long the RDNSS entries
191                are used for name resolution. A value of 0 means the nameserver
192                should no longer be used. The maximum duration how long the
193                RDNSS entries are used for name resolution. A value of 0 means
194                the nameserver should no longer be used. The value, if not 0,
195                must be at least max_rtr_adv_interval. To ensure stale RDNSS
196                info gets removed in a timely fashion, this should not be
197                greater than 2*max_rtr_adv_interval.
198        """
199        self._prefix = prefix
200        self._clients = clients
201        self._route = route
202        self._rdnss = rdnss
203        self._ignore_if_missing = ignore_if_missing
204        self._adv_send_advert = adv_send_advert
205        self._unicast_only = unicast_only
206        self._max_rtr_adv_interval = max_rtr_adv_interval
207        self._min_rtr_adv_interval = min_rtr_adv_interval
208        self._min_delay_between_ras = min_delay_between_ras
209        self._adv_managed_flag = adv_managed_flag
210        self._adv_other_config_flag = adv_other_config_flag
211        self._adv_link_mtu = adv_link_mtu
212        self._adv_reachable_time = adv_reachable_time
213        self._adv_retrans_timer = adv_retrans_timer
214        self._adv_cur_hop_limit = adv_cur_hop_limit
215        self._adv_default_lifetime = adv_default_lifetime
216        self._adv_default_preference = adv_default_preference
217        self._adv_source_ll_address = adv_source_ll_address
218        self._adv_home_agent_flag = adv_home_agent_flag
219        self._adv_home_agent_info = adv_home_agent_info
220        self._home_agent_lifetime = home_agent_lifetime
221        self._home_agent_preference = home_agent_preference
222        self._adv_mob_rtr_support_flag = adv_mob_rtr_support_flag
223        self._adv_interval_opt = adv_interval_opt
224        self._adv_on_link = adv_on_link
225        self._adv_autonomous = adv_autonomous
226        self._adv_router_addr = adv_router_addr
227        self._adv_valid_lifetime = adv_valid_lifetime
228        self._adv_preferred_lifetime = adv_preferred_lifetime
229        self._base_6to4_interface = base_6to4_interface
230        self._adv_route_lifetime = adv_route_lifetime
231        self._adv_route_preference = adv_route_preference
232        self._adv_rdnss_preference = adv_rdnss_preference
233        self._adv_rdnss_open = adv_rdnss_open
234        self._adv_rdnss_lifetime = adv_rdnss_lifetime
235
236    def package_configs(self):
237        conf = dict()
238        conf['prefix'] = self._prefix
239        conf['clients'] = self._clients
240        conf['route'] = self._route
241        conf['rdnss'] = self._rdnss
242
243        conf['interface_options'] = collections.OrderedDict(
244            filter(lambda pair: pair[1] is not None,
245                   (('IgnoreIfMissing', self._ignore_if_missing),
246                    ('AdvSendAdvert', self._adv_send_advert),
247                    ('UnicastOnly', self._unicast_only),
248                    ('MaxRtrAdvInterval', self._max_rtr_adv_interval),
249                    ('MinRtrAdvInterval', self._min_rtr_adv_interval),
250                    ('MinDelayBetweenRAs', self._min_delay_between_ras),
251                    ('AdvManagedFlag', self._adv_managed_flag),
252                    ('AdvOtherConfigFlag', self._adv_other_config_flag),
253                    ('AdvLinkMTU', self._adv_link_mtu),
254                    ('AdvReachableTime', self._adv_reachable_time),
255                    ('AdvRetransTimer', self._adv_retrans_timer),
256                    ('AdvCurHopLimit', self._adv_cur_hop_limit),
257                    ('AdvDefaultLifetime', self._adv_default_lifetime),
258                    ('AdvDefaultPreference', self._adv_default_preference),
259                    ('AdvSourceLLAddress', self._adv_source_ll_address),
260                    ('AdvHomeAgentFlag', self._adv_home_agent_flag),
261                    ('AdvHomeAgentInfo', self._adv_home_agent_info),
262                    ('HomeAgentLifetime', self._home_agent_lifetime),
263                    ('HomeAgentPreference', self._home_agent_preference),
264                    ('AdvMobRtrSupportFlag', self._adv_mob_rtr_support_flag),
265                    ('AdvIntervalOpt', self._adv_interval_opt))))
266
267        conf['prefix_options'] = collections.OrderedDict(
268            filter(lambda pair: pair[1] is not None,
269                   (('AdvOnLink', self._adv_on_link),
270                    ('AdvAutonomous', self._adv_autonomous),
271                    ('AdvRouterAddr', self._adv_router_addr),
272                    ('AdvValidLifetime', self._adv_valid_lifetime),
273                    ('AdvPreferredLifetime', self._adv_preferred_lifetime),
274                    ('Base6to4Interface', self._base_6to4_interface))))
275
276        conf['route_options'] = collections.OrderedDict(
277            filter(lambda pair: pair[1] is not None,
278                   (('AdvRouteLifetime', self._adv_route_lifetime),
279                    ('AdvRoutePreference', self._adv_route_preference))))
280
281        conf['rdnss_options'] = collections.OrderedDict(
282            filter(lambda pair: pair[1] is not None,
283                   (('AdvRDNSSPreference', self._adv_rdnss_preference),
284                    ('AdvRDNSSOpen', self._adv_rdnss_open),
285                    ('AdvRDNSSLifetime', self._adv_rdnss_lifetime))))
286
287        return conf
288