1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package android.net.util;
18 
19 import android.net.IpPrefix;
20 import android.net.LinkAddress;
21 import android.net.LinkProperties;
22 
23 import java.net.Inet4Address;
24 import java.net.InetAddress;
25 import java.util.Collections;
26 import java.util.HashSet;
27 import java.util.Set;
28 
29 
30 /**
31  * @hide
32  */
33 public class PrefixUtils {
34     private static final IpPrefix[] MIN_NON_FORWARDABLE_PREFIXES = {
35             pfx("127.0.0.0/8"),     // IPv4 loopback
36             pfx("169.254.0.0/16"),  // IPv4 link-local, RFC3927#section-8
37             pfx("::/3"),
38             pfx("fe80::/64"),       // IPv6 link-local
39             pfx("fc00::/7"),        // IPv6 ULA
40             pfx("ff02::/8"),        // IPv6 link-local multicast
41     };
42 
43     public static final IpPrefix DEFAULT_WIFI_P2P_PREFIX = pfx("192.168.49.0/24");
44 
45     /** Get non forwardable prefixes. */
getNonForwardablePrefixes()46     public static Set<IpPrefix> getNonForwardablePrefixes() {
47         final HashSet<IpPrefix> prefixes = new HashSet<>();
48         addNonForwardablePrefixes(prefixes);
49         return prefixes;
50     }
51 
52     /** Add non forwardable prefixes. */
addNonForwardablePrefixes(Set<IpPrefix> prefixes)53     public static void addNonForwardablePrefixes(Set<IpPrefix> prefixes) {
54         Collections.addAll(prefixes, MIN_NON_FORWARDABLE_PREFIXES);
55     }
56 
57     /** Get local prefixes from |lp|. */
localPrefixesFrom(LinkProperties lp)58     public static Set<IpPrefix> localPrefixesFrom(LinkProperties lp) {
59         final HashSet<IpPrefix> localPrefixes = new HashSet<>();
60         if (lp == null) return localPrefixes;
61 
62         for (LinkAddress addr : lp.getAllLinkAddresses()) {
63             if (addr.getAddress().isLinkLocalAddress()) continue;
64             localPrefixes.add(asIpPrefix(addr));
65         }
66         // TODO: Add directly-connected routes as well (ones from which we did
67         // not also form a LinkAddress)?
68 
69         return localPrefixes;
70     }
71 
72     /** Convert LinkAddress |addr| to IpPrefix. */
asIpPrefix(LinkAddress addr)73     public static IpPrefix asIpPrefix(LinkAddress addr) {
74         return new IpPrefix(addr.getAddress(), addr.getPrefixLength());
75     }
76 
77     /** Convert InetAddress |ip| to IpPrefix. */
ipAddressAsPrefix(InetAddress ip)78     public static IpPrefix ipAddressAsPrefix(InetAddress ip) {
79         final int bitLength = (ip instanceof Inet4Address)
80                 ? NetworkConstants.IPV4_ADDR_BITS
81                 : NetworkConstants.IPV6_ADDR_BITS;
82         return new IpPrefix(ip, bitLength);
83     }
84 
pfx(String prefixStr)85     private static IpPrefix pfx(String prefixStr) {
86         return new IpPrefix(prefixStr);
87     }
88 }
89