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