blob: f4943ed9a57bf683fd9ee2480ba3c4aaf0b85b58 [file] [log] [blame]
Jorge E. Moreiraae3163f2019-02-12 17:18:55 -08001/*
2 * Copyright (C) 2019 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#include "common/libs/utils/network.h"
18
19#include <linux/if.h>
20#include <linux/if_tun.h>
21#include <string.h>
22
23#include "common/libs/glog/logging.h"
24
25namespace cvd {
Jorge E. Moreirac8745ba2019-03-14 14:35:48 -070026namespace {
27// This should be the size of virtio_net_hdr_v1, from linux/virtio_net.h, but
28// the version of that header that ships with android in Pie does not include
29// that struct (it was added in Q).
30// This is what that struct looks like:
31// struct virtio_net_hdr_v1 {
32// u8 flags;
33// u8 gso_type;
34// u16 hdr_len;
35// u16 gso_size;
36// u16 csum_start;
37// u16 csum_offset;
38// u16 num_buffers;
39// };
40static constexpr int SIZE_OF_VIRTIO_NET_HDR_V1 = 12;
41} // namespace
42
Jorge E. Moreiraae3163f2019-02-12 17:18:55 -080043SharedFD OpenTapInterface(const std::string& interface_name) {
44 constexpr auto TUNTAP_DEV = "/dev/net/tun";
45
Cody Schuffelen5a67de22019-03-07 18:42:37 -080046 auto tap_fd = SharedFD::Open(TUNTAP_DEV, O_RDWR | O_NONBLOCK);
Jorge E. Moreiraae3163f2019-02-12 17:18:55 -080047 if (!tap_fd->IsOpen()) {
48 LOG(ERROR) << "Unable to open tun device: " << tap_fd->StrError();
49 return tap_fd;
50 }
51
52 struct ifreq ifr;
53 memset(&ifr, 0, sizeof(ifr));
Cody Schuffelen5a67de22019-03-07 18:42:37 -080054 ifr.ifr_flags = IFF_TAP | IFF_NO_PI | IFF_VNET_HDR;
Jorge E. Moreiraae3163f2019-02-12 17:18:55 -080055 strncpy(ifr.ifr_name, interface_name.c_str(), IFNAMSIZ);
56
57 int err = tap_fd->Ioctl(TUNSETIFF, &ifr);
58 if (err < 0) {
59 LOG(ERROR) << "Unable to connect to " << interface_name
60 << " tap interface: " << tap_fd->StrError();
61 tap_fd->Close();
62 return cvd::SharedFD();
63 }
64
Jorge E. Moreirac8745ba2019-03-14 14:35:48 -070065 // The interface's configuration may have been modified or just not set
66 // correctly on creation. While qemu checks this and enforces the right
67 // configuration, crosvm does not, so it needs to be set before it's passed to
68 // it.
69 tap_fd->Ioctl(TUNSETOFFLOAD,
70 reinterpret_cast<void*>(TUN_F_CSUM | TUN_F_UFO | TUN_F_TSO4 |
71 TUN_F_TSO6));
72 int len = SIZE_OF_VIRTIO_NET_HDR_V1;
73 tap_fd->Ioctl(TUNSETVNETHDRSZ, &len);
74
Jorge E. Moreiraae3163f2019-02-12 17:18:55 -080075 return tap_fd;
76}
Jorge E. Moreirac8745ba2019-03-14 14:35:48 -070077} // namespace cvd