blob: 113c8ec74f88309388c397eec692288cfea123f9 [file] [log] [blame]
xf.lif1aed282024-02-06 00:31:51 -08001/*
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
19#include <errno.h>
20#include <string.h>
21#include <arpa/inet.h>
22#include <sys/socket.h>
23#include <sys/mman.h>
24#include <linux/if.h>
25#include <linux/if_packet.h>
26
27#include "logging.h"
28#include "ring.h"
29#include "translate.h"
30#include "tun.h"
31
32int ring_create(struct tun_data *tunnel) {
33 int packetsock = socket(AF_PACKET, SOCK_DGRAM, htons(ETH_P_IPV6));
34 if (packetsock < 0) {
35 logmsg(ANDROID_LOG_FATAL, "packet socket failed: %s", strerror(errno));
36 return -1;
37 }
38
39 int ver = TPACKET_V2;
40 if (setsockopt(packetsock, SOL_PACKET, PACKET_VERSION, (void *) &ver, sizeof(ver))) {
41 logmsg(ANDROID_LOG_FATAL, "setsockopt(PACKET_VERSION, %d) failed: %s", ver, strerror(errno));
xf.li7ccf8372024-03-07 00:08:02 -080042 close(packetsock);
xf.lif1aed282024-02-06 00:31:51 -080043 return -1;
44 }
45
46 int on = 1;
47 if (setsockopt(packetsock, SOL_PACKET, PACKET_LOSS, (void *) &on, sizeof(on))) {
48 logmsg(ANDROID_LOG_WARN, "PACKET_LOSS failed: %s", strerror(errno));
49 }
50
51 struct packet_ring *ring = &tunnel->ring;
52 ring->numblocks = TP_NUM_BLOCKS;
53
54 int total_frames = TP_FRAMES * ring->numblocks;
55
56 struct tpacket_req req = {
57 .tp_frame_size = TP_FRAME_SIZE, // Frame size.
58 .tp_block_size = TP_BLOCK_SIZE, // Frames per block.
59 .tp_block_nr = ring->numblocks, // Number of blocks.
60 .tp_frame_nr = total_frames, // Total frames.
61 };
62
63 if (setsockopt(packetsock, SOL_PACKET, PACKET_RX_RING, &req, sizeof(req)) < 0) {
64 logmsg(ANDROID_LOG_FATAL, "PACKET_RX_RING failed: %s", strerror(errno));
xf.li7ccf8372024-03-07 00:08:02 -080065 close(packetsock);
xf.lif1aed282024-02-06 00:31:51 -080066 return -1;
67 }
68
69 size_t buflen = TP_BLOCK_SIZE * ring->numblocks;
70 ring->base = mmap(NULL, buflen, PROT_READ|PROT_WRITE, MAP_SHARED|MAP_LOCKED|MAP_POPULATE,
71 packetsock, 0);
72 if (ring->base == MAP_FAILED) {
73 logmsg(ANDROID_LOG_FATAL, "mmap %lu failed: %s", buflen, strerror(errno));
xf.li7ccf8372024-03-07 00:08:02 -080074 close(packetsock);
xf.lif1aed282024-02-06 00:31:51 -080075 return -1;
76 }
77
78 ring->block = 0;
79 ring->slot = 0;
80 ring->numslots = TP_BLOCK_SIZE / TP_FRAME_SIZE;
81 ring->next = (struct tpacket2_hdr *) ring->base;
82
83 logmsg(ANDROID_LOG_INFO, "Using ring buffer with %d frames (%d bytes) at %p",
84 total_frames, buflen, ring->base);
85
86 return packetsock;
87}
88
89/* function: ring_advance
90 * advances to the next position in the packet ring
91 * ring - packet ring buffer
92 */
93static struct tpacket2_hdr* ring_advance(struct packet_ring *ring) {
94 uint8_t *next = (uint8_t *) ring->next;
95
96 ring->slot++;
97 next += TP_FRAME_SIZE;
98
99 if (ring->slot == ring->numslots) {
100 ring->slot = 0;
101 ring->block++;
102
103 if (ring->block < ring->numblocks) {
104 next += TP_FRAME_GAP;
105 } else {
106 ring->block = 0;
107 next = (uint8_t *) ring->base;
108 }
109 }
110
111 ring->next = (struct tpacket2_hdr *) next;
112 return ring->next;
113}
114
115/* function: ring_read
116 * reads a packet from the ring buffer and translates it
117 * read_fd - file descriptor to read original packet from
118 * write_fd - file descriptor to write translated packet to
119 * to_ipv6 - whether the packet is to be translated to ipv6 or ipv4
120 */
121void ring_read(struct packet_ring *ring, int write_fd, int to_ipv6) {
122 struct tpacket2_hdr *tp = ring->next;
123 if (tp->tp_status & TP_STATUS_USER) {
124 uint8_t *packet = ((uint8_t *) tp) + tp->tp_net;
125 translate_packet(write_fd, to_ipv6, packet, tp->tp_len);
126 tp->tp_status = TP_STATUS_KERNEL;
127 tp = ring_advance(ring);
128 }
129}