1 /* 2 * Copyright (C) 2024 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 package android.net.apf; 17 18 /** 19 * The class which declares constants used in ApfFilter and unit tests. 20 */ 21 public final class ApfConstants { 22 ApfConstants()23 private ApfConstants() {} 24 public static final int ETH_HEADER_LEN = 14; 25 public static final int ETH_DEST_ADDR_OFFSET = 0; 26 public static final int ETH_ETHERTYPE_OFFSET = 12; 27 public static final int ETH_TYPE_MIN = 0x0600; 28 public static final int ETH_TYPE_MAX = 0xFFFF; 29 // TODO: Make these offsets relative to end of link-layer header; don't include ETH_HEADER_LEN. 30 public static final int IPV4_TOTAL_LENGTH_OFFSET = ETH_HEADER_LEN + 2; 31 public static final int IPV4_FRAGMENT_OFFSET_OFFSET = ETH_HEADER_LEN + 6; 32 // Endianness is not an issue for this constant because the APF interpreter always operates in 33 // network byte order. 34 public static final int IPV4_FRAGMENT_OFFSET_MASK = 0x1fff; 35 public static final int IPV4_FRAGMENT_MORE_FRAGS_MASK = 0x2000; 36 public static final int IPV4_PROTOCOL_OFFSET = ETH_HEADER_LEN + 9; 37 public static final int IPV4_SRC_ADDR_OFFSET = ETH_HEADER_LEN + 12; 38 public static final int IPV4_DEST_ADDR_OFFSET = ETH_HEADER_LEN + 16; 39 public static final int IPV4_ANY_HOST_ADDRESS = 0; 40 public static final int IPV4_BROADCAST_ADDRESS = -1; // 255.255.255.255 41 42 // Traffic class and Flow label are not byte aligned. Luckily we 43 // don't care about either value so we'll consider bytes 1-3 of the 44 // IPv6 header as don't care. 45 public static final int IPV6_FLOW_LABEL_OFFSET = ETH_HEADER_LEN + 1; 46 public static final int IPV6_FLOW_LABEL_LEN = 3; 47 public static final int IPV6_PAYLOAD_LEN_OFFSET = ETH_HEADER_LEN + 4; 48 public static final int IPV6_NEXT_HEADER_OFFSET = ETH_HEADER_LEN + 6; 49 public static final int IPV6_HOP_LIMIT_OFFSET = ETH_HEADER_LEN + 7; 50 public static final int IPV6_SRC_ADDR_OFFSET = ETH_HEADER_LEN + 8; 51 public static final int IPV6_DEST_ADDR_OFFSET = ETH_HEADER_LEN + 24; 52 public static final int IPV6_HEADER_LEN = 40; 53 // The IPv6 all nodes address ff02::1 54 public static final byte[] IPV6_ALL_NODES_ADDRESS = 55 { (byte) 0xff, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }; 56 // The IPv6 unspecified address :: 57 public static final byte[] IPV6_UNSPECIFIED_ADDRESS = 58 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; 59 // The IPv6 solicited nodes multicast address prefix ff02::1:ffXX:X/104 60 public static final byte[] IPV6_SOLICITED_NODES_PREFIX = 61 { (byte) 0xff, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, (byte) 0xff}; 62 63 public static final int ICMP6_TYPE_OFFSET = ETH_HEADER_LEN + IPV6_HEADER_LEN; 64 public static final int ICMP6_CODE_OFFSET = ETH_HEADER_LEN + IPV6_HEADER_LEN + 1; 65 public static final int ICMP6_CHECKSUM_OFFSET = ETH_HEADER_LEN + IPV6_HEADER_LEN + 2; 66 public static final int ICMP6_NS_TARGET_IP_OFFSET = ICMP6_TYPE_OFFSET + 8; 67 public static final int ICMP6_NS_OPTION_TYPE_OFFSET = ICMP6_NS_TARGET_IP_OFFSET + 16; 68 69 public static final int IPPROTO_HOPOPTS = 0; 70 71 // NOTE: this must be added to the IPv4 header length in MemorySlot.IPV4_HEADER_SIZE 72 public static final int TCP_UDP_SOURCE_PORT_OFFSET = ETH_HEADER_LEN; 73 public static final int TCP_UDP_DESTINATION_PORT_OFFSET = ETH_HEADER_LEN + 2; 74 public static final int UDP_HEADER_LEN = 8; 75 76 public static final int TCP_HEADER_SIZE_OFFSET = 12; 77 78 public static final int DHCP_SERVER_PORT = 67; 79 public static final int DHCP_CLIENT_PORT = 68; 80 81 public static final int ARP_HEADER_OFFSET = ETH_HEADER_LEN; 82 public static final byte[] ARP_IPV4_HEADER = { 83 0, 1, // Hardware type: Ethernet (1) 84 8, 0, // Protocol type: IP (0x0800) 85 6, // Hardware size: 6 86 4, // Protocol size: 4 87 }; 88 public static final int ARP_OPCODE_OFFSET = ARP_HEADER_OFFSET + 6; 89 // Opcode: ARP request (0x0001), ARP reply (0x0002) 90 public static final short ARP_OPCODE_REQUEST = 1; 91 public static final short ARP_OPCODE_REPLY = 2; 92 public static final int ARP_SOURCE_IP_ADDRESS_OFFSET = ARP_HEADER_OFFSET + 14; 93 public static final int ARP_TARGET_IP_ADDRESS_OFFSET = ARP_HEADER_OFFSET + 24; 94 // Limit on the Black List size to cap on program usage for this 95 // TODO: Select a proper max length 96 public static final int APF_MAX_ETH_TYPE_BLACK_LIST_LEN = 20; 97 98 // The ethernet solicited nodes multicast address prefix 33:33:FF:xx:xx:xx 99 public static final byte[] ETH_SOLICITED_NODES_PREFIX = 100 {(byte) 0x33, (byte) 0x33, (byte) 0xff}; 101 public static final byte[] ETH_MULTICAST_IPV6_ALL_NODES_MAC_ADDRESS = 102 { (byte) 0x33, (byte) 0x33, 0, 0, 0, 1}; 103 public static final byte[] ETH_MULTICAST_MDNS_V4_MAC_ADDRESS = 104 {(byte) 0x01, (byte) 0x00, (byte) 0x5e, (byte) 0x00, (byte) 0x00, (byte) 0xfb}; 105 public static final byte[] ETH_MULTICAST_MDNS_V6_MAC_ADDRESS = 106 {(byte) 0x33, (byte) 0x33, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0xfb}; 107 public static final int MDNS_PORT = 5353; 108 109 public static final int ECHO_PORT = 7; 110 public static final int DNS_HEADER_LEN = 12; 111 public static final int DNS_QDCOUNT_OFFSET = 4; 112 // NOTE: this must be added to the IPv4 header length in MemorySlot.IPV4_HEADER_SIZE, or the 113 // IPv6 header length. 114 public static final int DHCP_CLIENT_MAC_OFFSET = ETH_HEADER_LEN + UDP_HEADER_LEN + 28; 115 public static final int MDNS_QDCOUNT_OFFSET = 116 ETH_HEADER_LEN + UDP_HEADER_LEN + DNS_QDCOUNT_OFFSET; 117 public static final int MDNS_QNAME_OFFSET = 118 ETH_HEADER_LEN + UDP_HEADER_LEN + DNS_HEADER_LEN; 119 120 /** 121 * Fixed byte sequence representing the following part of the ARP reply header: 122 * EtherType + HTYPE + PTYPE + HLEN + PLEN + ops reply (0x0002) 123 */ 124 public static final byte[] FIXED_ARP_REPLY_HEADER = 125 new byte[]{0x08, 0x06, 0x00, 0x01, 0x08, 0x00, 0x06, 0x04, 0x00, 0x02}; 126 } 127