blob: e836a55d73566028a229a3a930f3a7f110cbf466 [file] [log] [blame]
Lorenzo Colitti9353be22014-12-03 15:18:29 +09001/*
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 Colitti9353be22014-12-03 15:18:29 +090019#include <arpa/inet.h>
junyulaic4e591a2018-11-26 22:36:10 +090020#include <errno.h>
Lorenzo Colitti9353be22014-12-03 15:18:29 +090021#include <linux/if.h>
22#include <linux/if_packet.h>
junyulaic4e591a2018-11-26 22:36:10 +090023#include <string.h>
24#include <sys/mman.h>
25#include <sys/socket.h>
Lorenzo Colitti9353be22014-12-03 15:18:29 +090026
27#include "logging.h"
28#include "ring.h"
29#include "translate.h"
30#include "tun.h"
31
32int ring_create(struct tun_data *tunnel) {
Maciej Żenczykowski60bce372019-04-09 01:58:52 -070033 int packetsock = socket(AF_PACKET, SOCK_DGRAM | SOCK_CLOEXEC, htons(ETH_P_IPV6));
Lorenzo Colitti9353be22014-12-03 15:18:29 +090034 if (packetsock < 0) {
35 logmsg(ANDROID_LOG_FATAL, "packet socket failed: %s", strerror(errno));
36 return -1;
37 }
38
39 int ver = TPACKET_V2;
junyulaic4e591a2018-11-26 22:36:10 +090040 if (setsockopt(packetsock, SOL_PACKET, PACKET_VERSION, (void *)&ver, sizeof(ver))) {
Lorenzo Colitti9353be22014-12-03 15:18:29 +090041 logmsg(ANDROID_LOG_FATAL, "setsockopt(PACKET_VERSION, %d) failed: %s", ver, strerror(errno));
42 return -1;
43 }
44
45 int on = 1;
junyulaic4e591a2018-11-26 22:36:10 +090046 if (setsockopt(packetsock, SOL_PACKET, PACKET_LOSS, (void *)&on, sizeof(on))) {
Lorenzo Colitti9353be22014-12-03 15:18:29 +090047 logmsg(ANDROID_LOG_WARN, "PACKET_LOSS failed: %s", strerror(errno));
48 }
49
50 struct packet_ring *ring = &tunnel->ring;
junyulaic4e591a2018-11-26 22:36:10 +090051 ring->numblocks = TP_NUM_BLOCKS;
Lorenzo Colitti9353be22014-12-03 15:18:29 +090052
53 int total_frames = TP_FRAMES * ring->numblocks;
54
55 struct tpacket_req req = {
junyulaic4e591a2018-11-26 22:36:10 +090056 .tp_frame_size = TP_FRAME_SIZE, // Frame size.
57 .tp_block_size = TP_BLOCK_SIZE, // Frames per block.
58 .tp_block_nr = ring->numblocks, // Number of blocks.
59 .tp_frame_nr = total_frames, // Total frames.
Lorenzo Colitti9353be22014-12-03 15:18:29 +090060 };
61
62 if (setsockopt(packetsock, SOL_PACKET, PACKET_RX_RING, &req, sizeof(req)) < 0) {
63 logmsg(ANDROID_LOG_FATAL, "PACKET_RX_RING failed: %s", strerror(errno));
64 return -1;
65 }
66
67 size_t buflen = TP_BLOCK_SIZE * ring->numblocks;
junyulaic4e591a2018-11-26 22:36:10 +090068 ring->base = mmap(NULL, buflen, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_LOCKED | MAP_POPULATE,
Lorenzo Colitti9353be22014-12-03 15:18:29 +090069 packetsock, 0);
70 if (ring->base == MAP_FAILED) {
71 logmsg(ANDROID_LOG_FATAL, "mmap %lu failed: %s", buflen, strerror(errno));
72 return -1;
73 }
74
junyulaic4e591a2018-11-26 22:36:10 +090075 ring->block = 0;
76 ring->slot = 0;
Lorenzo Colitti9353be22014-12-03 15:18:29 +090077 ring->numslots = TP_BLOCK_SIZE / TP_FRAME_SIZE;
junyulaic4e591a2018-11-26 22:36:10 +090078 ring->next = (struct tpacket2_hdr *)ring->base;
Lorenzo Colitti9353be22014-12-03 15:18:29 +090079
junyulaic4e591a2018-11-26 22:36:10 +090080 logmsg(ANDROID_LOG_INFO, "Using ring buffer with %d frames (%d bytes) at %p", total_frames,
81 buflen, ring->base);
Lorenzo Colitti9353be22014-12-03 15:18:29 +090082
83 return packetsock;
84}
85
86/* function: ring_advance
87 * advances to the next position in the packet ring
88 * ring - packet ring buffer
89 */
junyulaic4e591a2018-11-26 22:36:10 +090090static struct tpacket2_hdr *ring_advance(struct packet_ring *ring) {
91 uint8_t *next = (uint8_t *)ring->next;
Lorenzo Colitti9353be22014-12-03 15:18:29 +090092
93 ring->slot++;
94 next += TP_FRAME_SIZE;
95
96 if (ring->slot == ring->numslots) {
97 ring->slot = 0;
98 ring->block++;
99
100 if (ring->block < ring->numblocks) {
101 next += TP_FRAME_GAP;
102 } else {
103 ring->block = 0;
junyulaic4e591a2018-11-26 22:36:10 +0900104 next = (uint8_t *)ring->base;
Lorenzo Colitti9353be22014-12-03 15:18:29 +0900105 }
106 }
107
junyulaic4e591a2018-11-26 22:36:10 +0900108 ring->next = (struct tpacket2_hdr *)next;
Lorenzo Colitti9353be22014-12-03 15:18:29 +0900109 return ring->next;
110}
111
112/* function: ring_read
113 * reads a packet from the ring buffer and translates it
114 * read_fd - file descriptor to read original packet from
115 * write_fd - file descriptor to write translated packet to
116 * to_ipv6 - whether the packet is to be translated to ipv6 or ipv4
117 */
118void ring_read(struct packet_ring *ring, int write_fd, int to_ipv6) {
119 struct tpacket2_hdr *tp = ring->next;
120 if (tp->tp_status & TP_STATUS_USER) {
junyulaic4e591a2018-11-26 22:36:10 +0900121 uint8_t *packet = ((uint8_t *)tp) + tp->tp_net;
Lorenzo Colitti9353be22014-12-03 15:18:29 +0900122 translate_packet(write_fd, to_ipv6, packet, tp->tp_len);
123 tp->tp_status = TP_STATUS_KERNEL;
junyulaic4e591a2018-11-26 22:36:10 +0900124 tp = ring_advance(ring);
Lorenzo Colitti9353be22014-12-03 15:18:29 +0900125 }
126}