Lorenzo Colitti | 9353be2 | 2014-12-03 15:18:29 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 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 | * ring.c - packet ring buffer functions |
| 17 | */ |
| 18 | |
Lorenzo Colitti | 9353be2 | 2014-12-03 15:18:29 +0900 | [diff] [blame] | 19 | #include <arpa/inet.h> |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 20 | #include <errno.h> |
Lorenzo Colitti | 9353be2 | 2014-12-03 15:18:29 +0900 | [diff] [blame] | 21 | #include <linux/if.h> |
| 22 | #include <linux/if_packet.h> |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 23 | #include <string.h> |
| 24 | #include <sys/mman.h> |
| 25 | #include <sys/socket.h> |
Lorenzo Colitti | 9353be2 | 2014-12-03 15:18:29 +0900 | [diff] [blame] | 26 | |
Maciej Żenczykowski | 551367e | 2020-06-03 08:08:21 +0000 | [diff] [blame^] | 27 | #include "config.h" |
Lorenzo Colitti | 9353be2 | 2014-12-03 15:18:29 +0900 | [diff] [blame] | 28 | #include "logging.h" |
| 29 | #include "ring.h" |
| 30 | #include "translate.h" |
Lorenzo Colitti | 9353be2 | 2014-12-03 15:18:29 +0900 | [diff] [blame] | 31 | |
| 32 | int ring_create(struct tun_data *tunnel) { |
Maciej Żenczykowski | 69c840f | 2019-11-18 12:00:49 -0800 | [diff] [blame] | 33 | // Will eventually be bound to htons(ETH_P_IPV6) protocol, |
| 34 | // but only after appropriate bpf filter is attached. |
| 35 | int packetsock = socket(AF_PACKET, SOCK_DGRAM | SOCK_CLOEXEC, 0); |
Lorenzo Colitti | 9353be2 | 2014-12-03 15:18:29 +0900 | [diff] [blame] | 36 | if (packetsock < 0) { |
| 37 | logmsg(ANDROID_LOG_FATAL, "packet socket failed: %s", strerror(errno)); |
| 38 | return -1; |
| 39 | } |
| 40 | |
| 41 | int ver = TPACKET_V2; |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 42 | if (setsockopt(packetsock, SOL_PACKET, PACKET_VERSION, (void *)&ver, sizeof(ver))) { |
Lorenzo Colitti | 9353be2 | 2014-12-03 15:18:29 +0900 | [diff] [blame] | 43 | logmsg(ANDROID_LOG_FATAL, "setsockopt(PACKET_VERSION, %d) failed: %s", ver, strerror(errno)); |
| 44 | return -1; |
| 45 | } |
| 46 | |
| 47 | int on = 1; |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 48 | if (setsockopt(packetsock, SOL_PACKET, PACKET_LOSS, (void *)&on, sizeof(on))) { |
Lorenzo Colitti | 9353be2 | 2014-12-03 15:18:29 +0900 | [diff] [blame] | 49 | logmsg(ANDROID_LOG_WARN, "PACKET_LOSS failed: %s", strerror(errno)); |
| 50 | } |
| 51 | |
| 52 | struct packet_ring *ring = &tunnel->ring; |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 53 | ring->numblocks = TP_NUM_BLOCKS; |
Lorenzo Colitti | 9353be2 | 2014-12-03 15:18:29 +0900 | [diff] [blame] | 54 | |
| 55 | int total_frames = TP_FRAMES * ring->numblocks; |
| 56 | |
| 57 | struct tpacket_req req = { |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 58 | .tp_frame_size = TP_FRAME_SIZE, // Frame size. |
| 59 | .tp_block_size = TP_BLOCK_SIZE, // Frames per block. |
| 60 | .tp_block_nr = ring->numblocks, // Number of blocks. |
| 61 | .tp_frame_nr = total_frames, // Total frames. |
Lorenzo Colitti | 9353be2 | 2014-12-03 15:18:29 +0900 | [diff] [blame] | 62 | }; |
| 63 | |
| 64 | if (setsockopt(packetsock, SOL_PACKET, PACKET_RX_RING, &req, sizeof(req)) < 0) { |
| 65 | logmsg(ANDROID_LOG_FATAL, "PACKET_RX_RING failed: %s", strerror(errno)); |
| 66 | return -1; |
| 67 | } |
| 68 | |
| 69 | size_t buflen = TP_BLOCK_SIZE * ring->numblocks; |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 70 | ring->base = mmap(NULL, buflen, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_LOCKED | MAP_POPULATE, |
Lorenzo Colitti | 9353be2 | 2014-12-03 15:18:29 +0900 | [diff] [blame] | 71 | packetsock, 0); |
| 72 | if (ring->base == MAP_FAILED) { |
| 73 | logmsg(ANDROID_LOG_FATAL, "mmap %lu failed: %s", buflen, strerror(errno)); |
| 74 | return -1; |
| 75 | } |
| 76 | |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 77 | ring->block = 0; |
| 78 | ring->slot = 0; |
Lorenzo Colitti | 9353be2 | 2014-12-03 15:18:29 +0900 | [diff] [blame] | 79 | ring->numslots = TP_BLOCK_SIZE / TP_FRAME_SIZE; |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 80 | ring->next = (struct tpacket2_hdr *)ring->base; |
Lorenzo Colitti | 9353be2 | 2014-12-03 15:18:29 +0900 | [diff] [blame] | 81 | |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 82 | logmsg(ANDROID_LOG_INFO, "Using ring buffer with %d frames (%d bytes) at %p", total_frames, |
| 83 | buflen, ring->base); |
Lorenzo Colitti | 9353be2 | 2014-12-03 15:18:29 +0900 | [diff] [blame] | 84 | |
| 85 | return packetsock; |
| 86 | } |
| 87 | |
| 88 | /* function: ring_advance |
| 89 | * advances to the next position in the packet ring |
| 90 | * ring - packet ring buffer |
| 91 | */ |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 92 | static struct tpacket2_hdr *ring_advance(struct packet_ring *ring) { |
| 93 | uint8_t *next = (uint8_t *)ring->next; |
Lorenzo Colitti | 9353be2 | 2014-12-03 15:18:29 +0900 | [diff] [blame] | 94 | |
| 95 | ring->slot++; |
| 96 | next += TP_FRAME_SIZE; |
| 97 | |
| 98 | if (ring->slot == ring->numslots) { |
| 99 | ring->slot = 0; |
| 100 | ring->block++; |
| 101 | |
| 102 | if (ring->block < ring->numblocks) { |
| 103 | next += TP_FRAME_GAP; |
| 104 | } else { |
| 105 | ring->block = 0; |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 106 | next = (uint8_t *)ring->base; |
Lorenzo Colitti | 9353be2 | 2014-12-03 15:18:29 +0900 | [diff] [blame] | 107 | } |
| 108 | } |
| 109 | |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 110 | ring->next = (struct tpacket2_hdr *)next; |
Lorenzo Colitti | 9353be2 | 2014-12-03 15:18:29 +0900 | [diff] [blame] | 111 | return ring->next; |
| 112 | } |
| 113 | |
| 114 | /* function: ring_read |
| 115 | * reads a packet from the ring buffer and translates it |
| 116 | * read_fd - file descriptor to read original packet from |
| 117 | * write_fd - file descriptor to write translated packet to |
| 118 | * to_ipv6 - whether the packet is to be translated to ipv6 or ipv4 |
| 119 | */ |
| 120 | void ring_read(struct packet_ring *ring, int write_fd, int to_ipv6) { |
| 121 | struct tpacket2_hdr *tp = ring->next; |
| 122 | if (tp->tp_status & TP_STATUS_USER) { |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 123 | uint8_t *packet = ((uint8_t *)tp) + tp->tp_net; |
Lorenzo Colitti | 9353be2 | 2014-12-03 15:18:29 +0900 | [diff] [blame] | 124 | translate_packet(write_fd, to_ipv6, packet, tp->tp_len); |
| 125 | tp->tp_status = TP_STATUS_KERNEL; |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 126 | tp = ring_advance(ring); |
Lorenzo Colitti | 9353be2 | 2014-12-03 15:18:29 +0900 | [diff] [blame] | 127 | } |
| 128 | } |