1 /* 2 * Copyright (C) 2020 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; 18 19 import android.net.MacAddress; 20 21 import androidx.annotation.NonNull; 22 23 import com.android.net.module.util.Struct; 24 import com.android.net.module.util.Struct.Field; 25 import com.android.net.module.util.Struct.Type; 26 27 import java.net.Inet6Address; 28 import java.net.InetAddress; 29 import java.net.UnknownHostException; 30 import java.util.Arrays; 31 import java.util.Objects; 32 33 /** The key of BpfMap which is used for bpf offload. */ 34 public class TetherDownstream6Key extends Struct { 35 @Field(order = 0, type = Type.U32) 36 public final long iif; // The input interface index. 37 38 @Field(order = 1, type = Type.EUI48, padding = 2) 39 public final MacAddress dstMac; // Destination ethernet mac address (zeroed iff rawip ingress). 40 41 @Field(order = 2, type = Type.ByteArray, arraysize = 16) 42 public final byte[] neigh6; // The destination IPv6 address. 43 TetherDownstream6Key(final long iif, @NonNull final MacAddress dstMac, final byte[] neigh6)44 public TetherDownstream6Key(final long iif, @NonNull final MacAddress dstMac, 45 final byte[] neigh6) { 46 Objects.requireNonNull(dstMac); 47 48 try { 49 final Inet6Address unused = (Inet6Address) InetAddress.getByAddress(neigh6); 50 } catch (ClassCastException | UnknownHostException e) { 51 throw new IllegalArgumentException("Invalid IPv6 address: " 52 + Arrays.toString(neigh6)); 53 } 54 this.iif = iif; 55 this.dstMac = dstMac; 56 this.neigh6 = neigh6; 57 } 58 59 @Override toString()60 public String toString() { 61 try { 62 return String.format("iif: %d, dstMac: %s, neigh: %s", iif, dstMac, 63 Inet6Address.getByAddress(neigh6)); 64 } catch (UnknownHostException e) { 65 // Should not happen because construtor already verify neigh6. 66 throw new IllegalStateException("Invalid TetherDownstream6Key"); 67 } 68 } 69 } 70