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 com.android.networkstack.tethering.util;
18 
19 import android.net.IpPrefix;
20 import android.net.LinkAddress;
21 import android.net.LinkProperties;
22 import android.net.util.NetworkConstants;
23 
24 import java.net.Inet4Address;
25 import java.net.InetAddress;
26 import java.util.Collections;
27 import java.util.HashSet;
28 import java.util.Set;
29 
30 
31 /**
32  * @hide
33  */
34 public class PrefixUtils {
35     private static final IpPrefix[] MIN_NON_FORWARDABLE_PREFIXES = {
36             pfx("127.0.0.0/8"),     // IPv4 loopback
37             pfx("169.254.0.0/16"),  // IPv4 link-local, RFC3927#section-8
38             pfx("::/3"),
39             pfx("fe80::/64"),       // IPv6 link-local
40             pfx("fc00::/7"),        // IPv6 ULA
41             pfx("ff02::/8"),        // IPv6 link-local multicast
42     };
43 
44     public static final IpPrefix DEFAULT_WIFI_P2P_PREFIX = pfx("192.168.49.0/24");
45 
46     /** Get non forwardable prefixes. */
getNonForwardablePrefixes()47     public static Set<IpPrefix> getNonForwardablePrefixes() {
48         final HashSet<IpPrefix> prefixes = new HashSet<>();
49         addNonForwardablePrefixes(prefixes);
50         return prefixes;
51     }
52 
53     /** Add non forwardable prefixes. */
addNonForwardablePrefixes(Set<IpPrefix> prefixes)54     public static void addNonForwardablePrefixes(Set<IpPrefix> prefixes) {
55         Collections.addAll(prefixes, MIN_NON_FORWARDABLE_PREFIXES);
56     }
57 
58     /** Get local prefixes from |lp|. */
localPrefixesFrom(LinkProperties lp)59     public static Set<IpPrefix> localPrefixesFrom(LinkProperties lp) {
60         final HashSet<IpPrefix> localPrefixes = new HashSet<>();
61         if (lp == null) return localPrefixes;
62 
63         for (LinkAddress addr : lp.getAllLinkAddresses()) {
64             if (addr.getAddress().isLinkLocalAddress()) continue;
65             localPrefixes.add(asIpPrefix(addr));
66         }
67         // TODO: Add directly-connected routes as well (ones from which we did
68         // not also form a LinkAddress)?
69 
70         return localPrefixes;
71     }
72 
73     /** Convert LinkAddress |addr| to IpPrefix. */
asIpPrefix(LinkAddress addr)74     public static IpPrefix asIpPrefix(LinkAddress addr) {
75         return new IpPrefix(addr.getAddress(), addr.getPrefixLength());
76     }
77 
78     /** Convert InetAddress |ip| to IpPrefix. */
ipAddressAsPrefix(InetAddress ip)79     public static IpPrefix ipAddressAsPrefix(InetAddress ip) {
80         final int bitLength = (ip instanceof Inet4Address)
81                 ? NetworkConstants.IPV4_ADDR_BITS
82                 : NetworkConstants.IPV6_ADDR_BITS;
83         return new IpPrefix(ip, bitLength);
84     }
85 
pfx(String prefixStr)86     private static IpPrefix pfx(String prefixStr) {
87         return new IpPrefix(prefixStr);
88     }
89 }
90