blob: f1102dc321e85d5ad939829b73d03b4e6ba796a4 [file] [log] [blame]
sharath reddy1b0c9832017-11-29 20:08:11 +05301/*
Ed Warnickecb9cada2015-12-08 15:45:58 -07002 *------------------------------------------------------------------
3 * tuntap.c - kernel stack (reverse) punt/inject path
4 *
5 * Copyright (c) 2009 Cisco and/or its affiliates.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at:
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *------------------------------------------------------------------
18 */
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -070019/**
20 * @file
21 * @brief TunTap Kernel stack (reverse) punt/inject path.
22 *
23 * This driver runs in one of two distinct modes:
24 * - "punt/inject" mode, where we send pkts not otherwise processed
25 * by the forwarding to the Linux kernel stack, and
26 *
27 * - "normal interface" mode, where we treat the Linux kernel stack
28 * as a peer.
29 *
30 * By default, we select punt/inject mode.
31 */
Ed Warnickecb9cada2015-12-08 15:45:58 -070032
33#include <fcntl.h> /* for open */
34#include <sys/ioctl.h>
35#include <sys/socket.h>
36#include <sys/stat.h>
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -070037#include <sys/types.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070038#include <sys/uio.h> /* for iovec */
39#include <netinet/in.h>
40
41#include <linux/if_arp.h>
42#include <linux/if_tun.h>
43
44#include <vlib/vlib.h>
45#include <vlib/unix/unix.h>
46
47#include <vnet/ip/ip.h>
Neale Rannse8bad972017-08-10 11:34:12 -070048#include <vnet/fib/fib_table.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070049
50#include <vnet/ethernet/ethernet.h>
Damjan Marion8bdc63b2016-11-02 14:48:21 +010051#include <vnet/devices/devices.h>
Damjan Marion22311502016-10-28 20:30:15 +020052#include <vnet/feature/feature.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070053
Ed Warnickecb9cada2015-12-08 15:45:58 -070054static vnet_device_class_t tuntap_dev_class;
55static vnet_hw_interface_class_t tuntap_interface_class;
56
57static void tuntap_punt_frame (vlib_main_t * vm,
sharath reddy1b0c9832017-11-29 20:08:11 +053058 vlib_node_runtime_t * node,
59 vlib_frame_t * frame);
Ed Warnickecb9cada2015-12-08 15:45:58 -070060static void tuntap_nopunt_frame (vlib_main_t * vm,
sharath reddy1b0c9832017-11-29 20:08:11 +053061 vlib_node_runtime_t * node,
62 vlib_frame_t * frame);
Ed Warnickecb9cada2015-12-08 15:45:58 -070063
sharath reddy1b0c9832017-11-29 20:08:11 +053064typedef struct
65{
Ed Warnickecb9cada2015-12-08 15:45:58 -070066 u32 sw_if_index;
67 u8 is_v6;
68 u8 addr[16];
69} subif_address_t;
70
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -070071/**
Steven4cd25762017-10-05 00:12:33 -070072 * @brief TUNTAP per thread struct
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -070073 */
Steven4cd25762017-10-05 00:12:33 -070074typedef struct
75{
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -070076 /** Vector of VLIB rx buffers to use. We allocate them in blocks
Ed Warnickecb9cada2015-12-08 15:45:58 -070077 of VLIB_FRAME_SIZE (256). */
sharath reddy1b0c9832017-11-29 20:08:11 +053078 u32 *rx_buffers;
Ed Warnickecb9cada2015-12-08 15:45:58 -070079
Steven4cd25762017-10-05 00:12:33 -070080 /** Vector of iovecs for readv/writev calls. */
sharath reddy1b0c9832017-11-29 20:08:11 +053081 struct iovec *iovecs;
Steven4cd25762017-10-05 00:12:33 -070082} tuntap_per_thread_t;
83
84/**
85 * @brief TUNTAP node main state
86 */
sharath reddy1b0c9832017-11-29 20:08:11 +053087typedef struct
88{
Steven4cd25762017-10-05 00:12:33 -070089 /** per thread variables */
sharath reddy1b0c9832017-11-29 20:08:11 +053090 tuntap_per_thread_t *threads;
Steven4cd25762017-10-05 00:12:33 -070091
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -070092 /** File descriptors for /dev/net/tun and provisioning socket. */
Ed Warnickecb9cada2015-12-08 15:45:58 -070093 int dev_net_tun_fd, dev_tap_fd;
94
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -070095 /** Create a "tap" [ethernet] encaps device */
Ed Warnickecb9cada2015-12-08 15:45:58 -070096 int is_ether;
97
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -070098 /** 1 if a "normal" routed intfc, 0 if a punt/inject interface */
Ed Warnickecb9cada2015-12-08 15:45:58 -070099
100 int have_normal_interface;
101
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700102 /** tap device destination MAC address. Required, or Linux drops pkts */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700103 u8 ether_dst_mac[6];
104
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700105 /** Interface MTU in bytes and # of default sized buffers. */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700106 u32 mtu_bytes, mtu_buffers;
107
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700108 /** Linux interface name for tun device. */
sharath reddy1b0c9832017-11-29 20:08:11 +0530109 char *tun_name;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700110
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700111 /** Pool of subinterface addresses */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700112 subif_address_t *subifs;
113
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700114 /** Hash for subif addresses */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700115 mhash_t subif_mhash;
116
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700117 /** Unix file index */
Damjan Marion56dd5432017-09-08 19:52:02 +0200118 u32 clib_file_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700119
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700120 /** For the "normal" interface, if configured */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700121 u32 hw_if_index, sw_if_index;
122
123} tuntap_main_t;
124
125static tuntap_main_t tuntap_main = {
126 .tun_name = "vnet",
127
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700128 /** Suitable defaults for an Ethernet-like tun/tap device */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700129 .mtu_bytes = 4096 + 256,
130};
131
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700132/**
133 * @brief tuntap_tx
134 * @node tuntap-tx
135 *
136 * Output node, writes the buffers comprising the incoming frame
Ed Warnickecb9cada2015-12-08 15:45:58 -0700137 * to the tun/tap device, aka hands them to the Linux kernel stack.
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700138 *
139 * @param *vm - vlib_main_t
140 * @param *node - vlib_node_runtime_t
141 * @param *frame - vlib_frame_t
142 *
143 * @return rc - uword
144 *
Ed Warnickecb9cada2015-12-08 15:45:58 -0700145 */
146static uword
sharath reddy1b0c9832017-11-29 20:08:11 +0530147tuntap_tx (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700148{
Damjan Mariona3d59862018-11-10 10:23:00 +0100149 u32 *buffers = vlib_frame_vector_args (frame);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700150 uword n_packets = frame->n_vectors;
sharath reddy1b0c9832017-11-29 20:08:11 +0530151 tuntap_main_t *tm = &tuntap_main;
John Lo7394b5b2016-09-04 08:55:34 -0400152 vnet_main_t *vnm = vnet_get_main ();
153 vnet_interface_main_t *im = &vnm->interface_main;
154 u32 n_bytes = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700155 int i;
Damjan Marion067cd622018-07-11 12:47:43 +0200156 u16 thread_index = vm->thread_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700157
158 for (i = 0; i < n_packets; i++)
159 {
sharath reddy1b0c9832017-11-29 20:08:11 +0530160 struct iovec *iov;
161 vlib_buffer_t *b;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700162 uword l;
163
164 b = vlib_get_buffer (vm, buffers[i]);
165
166 if (tm->is_ether && (!tm->have_normal_interface))
sharath reddy1b0c9832017-11-29 20:08:11 +0530167 {
168 vlib_buffer_reset (b);
Dave Barach178cf492018-11-13 16:34:13 -0500169 clib_memcpy_fast (vlib_buffer_get_current (b), tm->ether_dst_mac,
170 6);
sharath reddy1b0c9832017-11-29 20:08:11 +0530171 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700172
173 /* Re-set iovecs if present. */
Steven4cd25762017-10-05 00:12:33 -0700174 if (tm->threads[thread_index].iovecs)
Damjan Marion8bea5892022-04-04 22:40:45 +0200175 vec_set_len (tm->threads[thread_index].iovecs, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700176
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700177 /** VLIB buffer chain -> Unix iovec(s). */
Steven4cd25762017-10-05 00:12:33 -0700178 vec_add2 (tm->threads[thread_index].iovecs, iov, 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700179 iov->iov_base = b->data + b->current_data;
180 iov->iov_len = l = b->current_length;
181
182 if (PREDICT_FALSE (b->flags & VLIB_BUFFER_NEXT_PRESENT))
183 {
sharath reddy1b0c9832017-11-29 20:08:11 +0530184 do
185 {
186 b = vlib_get_buffer (vm, b->next_buffer);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700187
sharath reddy1b0c9832017-11-29 20:08:11 +0530188 vec_add2 (tm->threads[thread_index].iovecs, iov, 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700189
sharath reddy1b0c9832017-11-29 20:08:11 +0530190 iov->iov_base = b->data + b->current_data;
191 iov->iov_len = b->current_length;
192 l += b->current_length;
193 }
194 while (b->flags & VLIB_BUFFER_NEXT_PRESENT);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700195 }
196
Steven4cd25762017-10-05 00:12:33 -0700197 if (writev (tm->dev_net_tun_fd, tm->threads[thread_index].iovecs,
198 vec_len (tm->threads[thread_index].iovecs)) < l)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700199 clib_unix_warning ("writev");
John Lo7394b5b2016-09-04 08:55:34 -0400200
201 n_bytes += l;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700202 }
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700203
John Lo7394b5b2016-09-04 08:55:34 -0400204 /* Update tuntap interface output stats. */
205 vlib_increment_combined_counter (im->combined_sw_if_counters
206 + VNET_INTERFACE_COUNTER_TX,
Damjan Marion586afd72017-04-05 19:18:20 +0200207 vm->thread_index,
John Lo7394b5b2016-09-04 08:55:34 -0400208 tm->sw_if_index, n_packets, n_bytes);
209
210
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700211 /** The normal interface path flattens the buffer chain */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700212 if (tm->have_normal_interface)
213 vlib_buffer_free_no_next (vm, buffers, n_packets);
214 else
215 vlib_buffer_free (vm, buffers, n_packets);
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700216
Ed Warnickecb9cada2015-12-08 15:45:58 -0700217 return n_packets;
218}
219
220VLIB_REGISTER_NODE (tuntap_tx_node,static) = {
221 .function = tuntap_tx,
222 .name = "tuntap-tx",
223 .type = VLIB_NODE_TYPE_INTERNAL,
224 .vector_size = 4,
225};
226
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700227/**
228 * @brief TUNTAP receive node
229 * @node tuntap-rx
230 *
231 * @param *vm - vlib_main_t
232 * @param *node - vlib_node_runtime_t
233 * @param *frame - vlib_frame_t
234 *
235 * @return rc - uword
236 *
237 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700238static uword
sharath reddy1b0c9832017-11-29 20:08:11 +0530239tuntap_rx (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700240{
sharath reddy1b0c9832017-11-29 20:08:11 +0530241 tuntap_main_t *tm = &tuntap_main;
242 vlib_buffer_t *b;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700243 u32 bi;
Damjan Marion8934a042019-02-09 23:29:26 +0100244 const uword buffer_size = vlib_buffer_get_default_data_size (vm);
Damjan Marion067cd622018-07-11 12:47:43 +0200245 u16 thread_index = vm->thread_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700246
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700247 /** Make sure we have some RX buffers. */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700248 {
Steven4cd25762017-10-05 00:12:33 -0700249 uword n_left = vec_len (tm->threads[thread_index].rx_buffers);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700250 uword n_alloc;
251
252 if (n_left < VLIB_FRAME_SIZE / 2)
253 {
sharath reddy1b0c9832017-11-29 20:08:11 +0530254 if (!tm->threads[thread_index].rx_buffers)
Steven4cd25762017-10-05 00:12:33 -0700255 vec_alloc (tm->threads[thread_index].rx_buffers, VLIB_FRAME_SIZE);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700256
sharath reddy1b0c9832017-11-29 20:08:11 +0530257 n_alloc =
258 vlib_buffer_alloc (vm,
259 tm->threads[thread_index].rx_buffers + n_left,
260 VLIB_FRAME_SIZE - n_left);
Damjan Marion8bea5892022-04-04 22:40:45 +0200261 vec_set_len (tm->threads[thread_index].rx_buffers, n_left + n_alloc);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700262 }
263 }
264
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700265 /** Allocate RX buffers from end of rx_buffers.
Ed Warnickecb9cada2015-12-08 15:45:58 -0700266 Turn them into iovecs to pass to readv. */
267 {
Steven4cd25762017-10-05 00:12:33 -0700268 uword i_rx = vec_len (tm->threads[thread_index].rx_buffers) - 1;
sharath reddy1b0c9832017-11-29 20:08:11 +0530269 vlib_buffer_t *b;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700270 word i, n_bytes_left, n_bytes_in_packet;
271
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700272 /** We should have enough buffers left for an MTU sized packet. */
sharath reddy1b0c9832017-11-29 20:08:11 +0530273 ASSERT (vec_len (tm->threads[thread_index].rx_buffers) >=
274 tm->mtu_buffers);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700275
Steven4cd25762017-10-05 00:12:33 -0700276 vec_validate (tm->threads[thread_index].iovecs, tm->mtu_buffers - 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700277 for (i = 0; i < tm->mtu_buffers; i++)
278 {
sharath reddy1b0c9832017-11-29 20:08:11 +0530279 b =
280 vlib_get_buffer (vm,
281 tm->threads[thread_index].rx_buffers[i_rx - i]);
Steven4cd25762017-10-05 00:12:33 -0700282 tm->threads[thread_index].iovecs[i].iov_base = b->data;
283 tm->threads[thread_index].iovecs[i].iov_len = buffer_size;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700284 }
285
sharath reddy1b0c9832017-11-29 20:08:11 +0530286 n_bytes_left =
287 readv (tm->dev_net_tun_fd, tm->threads[thread_index].iovecs,
288 tm->mtu_buffers);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700289 n_bytes_in_packet = n_bytes_left;
290 if (n_bytes_left <= 0)
291 {
sharath reddy1b0c9832017-11-29 20:08:11 +0530292 if (errno != EAGAIN)
293 clib_unix_warning ("readv %d", n_bytes_left);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700294 return 0;
295 }
296
Steven4cd25762017-10-05 00:12:33 -0700297 bi = tm->threads[thread_index].rx_buffers[i_rx];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700298
299 while (1)
300 {
Steven4cd25762017-10-05 00:12:33 -0700301 b = vlib_get_buffer (vm, tm->threads[thread_index].rx_buffers[i_rx]);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700302 b->flags = 0;
303 b->current_data = 0;
sharath reddy1b0c9832017-11-29 20:08:11 +0530304 b->current_length =
305 n_bytes_left < buffer_size ? n_bytes_left : buffer_size;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700306
307 n_bytes_left -= buffer_size;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700308
309 if (n_bytes_left <= 0)
sharath reddy1b0c9832017-11-29 20:08:11 +0530310 {
311 break;
312 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700313
314 i_rx--;
315 b->flags |= VLIB_BUFFER_NEXT_PRESENT;
Steven4cd25762017-10-05 00:12:33 -0700316 b->next_buffer = tm->threads[thread_index].rx_buffers[i_rx];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700317 }
318
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700319 /** Interface counters for tuntap interface. */
320 vlib_increment_combined_counter
sharath reddy1b0c9832017-11-29 20:08:11 +0530321 (vnet_main.interface_main.combined_sw_if_counters
322 + VNET_INTERFACE_COUNTER_RX,
323 thread_index, tm->sw_if_index, 1, n_bytes_in_packet);
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700324
Damjan Marion8bea5892022-04-04 22:40:45 +0200325 vec_set_len (tm->threads[thread_index].rx_buffers, i_rx);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700326 }
327
328 b = vlib_get_buffer (vm, bi);
329
330 {
331 u32 next_index;
332 uword n_trace = vlib_get_trace_count (vm, node);
333
334 vnet_buffer (b)->sw_if_index[VLIB_RX] = tm->sw_if_index;
sharath reddy1b0c9832017-11-29 20:08:11 +0530335 vnet_buffer (b)->sw_if_index[VLIB_TX] = (u32) ~ 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700336
Ed Warnickecb9cada2015-12-08 15:45:58 -0700337 b->error = node->errors[0];
338
339 if (tm->is_ether)
340 {
Damjan Marion8bdc63b2016-11-02 14:48:21 +0100341 next_index = VNET_DEVICE_INPUT_NEXT_ETHERNET_INPUT;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700342 }
343 else
344 switch (b->data[0] & 0xf0)
sharath reddy1b0c9832017-11-29 20:08:11 +0530345 {
346 case 0x40:
347 next_index = VNET_DEVICE_INPUT_NEXT_IP4_INPUT;
348 break;
349 case 0x60:
350 next_index = VNET_DEVICE_INPUT_NEXT_IP6_INPUT;
351 break;
352 default:
353 next_index = VNET_DEVICE_INPUT_NEXT_DROP;
354 break;
355 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700356
357 /* The linux kernel couldn't care less if our interface is up */
358 if (tm->have_normal_interface)
359 {
sharath reddy1b0c9832017-11-29 20:08:11 +0530360 vnet_main_t *vnm = vnet_get_main ();
361 vnet_sw_interface_t *si;
362 si = vnet_get_sw_interface (vnm, tm->sw_if_index);
363 if (!(si->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP))
364 next_index = VNET_DEVICE_INPUT_NEXT_DROP;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700365 }
366
Damjan Mariond0ffa262023-10-14 08:41:54 +0000367 vnet_feature_start_device_input (tm->sw_if_index, &next_index, b);
Damjan Marion22311502016-10-28 20:30:15 +0200368
Ed Warnickecb9cada2015-12-08 15:45:58 -0700369 vlib_set_next_frame_buffer (vm, node, next_index, bi);
370
Benoît Ganne9a3973e2020-10-02 19:36:57 +0200371 if (PREDICT_FALSE (n_trace > 0 && vlib_trace_buffer (vm, node, next_index, b, /* follow_chain */
372 1)))
373 vlib_set_trace_count (vm, node, n_trace - 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700374 }
375
376 return 1;
377}
378
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700379/**
380 * @brief TUNTAP_RX error strings
381 */
sharath reddy1b0c9832017-11-29 20:08:11 +0530382static char *tuntap_rx_error_strings[] = {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700383 "unknown packet type",
384};
385
386VLIB_REGISTER_NODE (tuntap_rx_node,static) = {
387 .function = tuntap_rx,
Damjan Marion7ca5aaa2019-09-24 18:10:49 +0200388 .flags = VLIB_NODE_FLAG_TRACE_SUPPORTED,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700389 .name = "tuntap-rx",
Damjan Marion51327ac2016-11-09 11:59:42 +0100390 .sibling_of = "device-input",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700391 .type = VLIB_NODE_TYPE_INPUT,
392 .state = VLIB_NODE_STATE_INTERRUPT,
393 .vector_size = 4,
394 .n_errors = 1,
395 .error_strings = tuntap_rx_error_strings,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700396};
397
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700398/**
399 * @brief Gets called when file descriptor is ready from epoll.
400 *
Damjan Marion56dd5432017-09-08 19:52:02 +0200401 * @param *uf - clib_file_t
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700402 *
403 * @return error - clib_error_t
404 */
sharath reddy1b0c9832017-11-29 20:08:11 +0530405static clib_error_t *
406tuntap_read_ready (clib_file_t * uf)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700407{
sharath reddy1b0c9832017-11-29 20:08:11 +0530408 vlib_main_t *vm = vlib_get_main ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700409 vlib_node_set_interrupt_pending (vm, tuntap_rx_node.index);
410 return 0;
411}
412
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700413/**
414 * @brief Clean up the tun/tap device
415 *
416 * @param *vm - vlib_main_t
417 *
418 * @return error - clib_error_t
419 *
Ed Warnickecb9cada2015-12-08 15:45:58 -0700420 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700421static clib_error_t *
422tuntap_exit (vlib_main_t * vm)
423{
424 tuntap_main_t *tm = &tuntap_main;
425 struct ifreq ifr;
426 int sfd;
427
428 /* Not present. */
sharath reddy1b0c9832017-11-29 20:08:11 +0530429 if (!tm->dev_net_tun_fd || tm->dev_net_tun_fd < 0)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700430 return 0;
431
432 sfd = socket (AF_INET, SOCK_STREAM, 0);
433 if (sfd < 0)
sharath reddy1b0c9832017-11-29 20:08:11 +0530434 clib_unix_warning ("provisioning socket");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700435
Dave Barachb7b92992018-10-17 10:38:51 -0400436 clib_memset (&ifr, 0, sizeof (ifr));
sharath reddy1b0c9832017-11-29 20:08:11 +0530437 strncpy (ifr.ifr_name, tm->tun_name, sizeof (ifr.ifr_name) - 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700438
439 /* get flags, modify to bring down interface... */
440 if (ioctl (sfd, SIOCGIFFLAGS, &ifr) < 0)
441 clib_unix_warning ("SIOCGIFFLAGS");
442
443 ifr.ifr_flags &= ~(IFF_UP | IFF_RUNNING);
444
445 if (ioctl (sfd, SIOCSIFFLAGS, &ifr) < 0)
446 clib_unix_warning ("SIOCSIFFLAGS");
447
448 /* Turn off persistence */
449 if (ioctl (tm->dev_net_tun_fd, TUNSETPERSIST, 0) < 0)
450 clib_unix_warning ("TUNSETPERSIST");
sharath reddy1b0c9832017-11-29 20:08:11 +0530451 close (tm->dev_tap_fd);
Dave Barachf9c231e2016-08-05 10:10:18 -0400452 if (tm->dev_net_tun_fd >= 0)
sharath reddy1b0c9832017-11-29 20:08:11 +0530453 close (tm->dev_net_tun_fd);
Dave Barach6f6f34f2016-08-08 13:05:31 -0400454 if (sfd >= 0)
455 close (sfd);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700456
457 return 0;
458}
459
460VLIB_MAIN_LOOP_EXIT_FUNCTION (tuntap_exit);
461
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700462/**
463 * @brief CLI function for tun/tap config
464 *
465 * @param *vm - vlib_main_t
466 * @param *input - unformat_input_t
467 *
468 * @return error - clib_error_t
469 *
470 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700471static clib_error_t *
472tuntap_config (vlib_main_t * vm, unformat_input_t * input)
473{
474 tuntap_main_t *tm = &tuntap_main;
sharath reddy1b0c9832017-11-29 20:08:11 +0530475 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700476 struct ifreq ifr;
sharath reddy1b0c9832017-11-29 20:08:11 +0530477 u8 *name;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700478 int flags = IFF_TUN | IFF_NO_PI;
479 int is_enabled = 0, is_ether = 0, have_normal_interface = 0;
Damjan Marion8934a042019-02-09 23:29:26 +0100480 const uword buffer_size = vlib_buffer_get_default_data_size (vm);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700481
482 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
483 {
484 if (unformat (input, "mtu %d", &tm->mtu_bytes))
485 ;
486 else if (unformat (input, "enable"))
sharath reddy1b0c9832017-11-29 20:08:11 +0530487 is_enabled = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700488 else if (unformat (input, "disable"))
sharath reddy1b0c9832017-11-29 20:08:11 +0530489 is_enabled = 0;
490 else if (unformat (input, "ethernet") || unformat (input, "ether"))
491 is_ether = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700492 else if (unformat (input, "have-normal-interface") ||
sharath reddy1b0c9832017-11-29 20:08:11 +0530493 unformat (input, "have-normal"))
494 have_normal_interface = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700495 else if (unformat (input, "name %s", &name))
496 tm->tun_name = (char *) name;
497 else
498 return clib_error_return (0, "unknown input `%U'",
499 format_unformat_error, input);
500 }
501
502 tm->dev_net_tun_fd = -1;
503 tm->dev_tap_fd = -1;
504
505 if (is_enabled == 0)
506 return 0;
507
sharath reddy1b0c9832017-11-29 20:08:11 +0530508 if (geteuid ())
Ed Warnickecb9cada2015-12-08 15:45:58 -0700509 {
510 clib_warning ("tuntap disabled: must be superuser");
511 return 0;
sharath reddy1b0c9832017-11-29 20:08:11 +0530512 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700513
514 tm->is_ether = is_ether;
515 tm->have_normal_interface = have_normal_interface;
516
517 if (is_ether)
518 flags = IFF_TAP | IFF_NO_PI;
519
520 if ((tm->dev_net_tun_fd = open ("/dev/net/tun", O_RDWR)) < 0)
521 {
522 error = clib_error_return_unix (0, "open /dev/net/tun");
523 goto done;
524 }
525
Dave Barachb7b92992018-10-17 10:38:51 -0400526 clib_memset (&ifr, 0, sizeof (ifr));
sharath reddy1b0c9832017-11-29 20:08:11 +0530527 strncpy (ifr.ifr_name, tm->tun_name, sizeof (ifr.ifr_name) - 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700528 ifr.ifr_flags = flags;
sharath reddy1b0c9832017-11-29 20:08:11 +0530529 if (ioctl (tm->dev_net_tun_fd, TUNSETIFF, (void *) &ifr) < 0)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700530 {
531 error = clib_error_return_unix (0, "ioctl TUNSETIFF");
532 goto done;
533 }
sharath reddy1b0c9832017-11-29 20:08:11 +0530534
Ed Warnickecb9cada2015-12-08 15:45:58 -0700535 /* Make it persistent, at least until we split. */
536 if (ioctl (tm->dev_net_tun_fd, TUNSETPERSIST, 1) < 0)
537 {
538 error = clib_error_return_unix (0, "TUNSETPERSIST");
539 goto done;
540 }
541
542 /* Open a provisioning socket */
sharath reddy1b0c9832017-11-29 20:08:11 +0530543 if ((tm->dev_tap_fd = socket (PF_PACKET, SOCK_RAW, htons (ETH_P_ALL))) < 0)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700544 {
545 error = clib_error_return_unix (0, "socket");
546 goto done;
547 }
548
549 /* Find the interface index. */
550 {
551 struct ifreq ifr;
552 struct sockaddr_ll sll;
553
Dave Barachb7b92992018-10-17 10:38:51 -0400554 clib_memset (&ifr, 0, sizeof (ifr));
sharath reddy1b0c9832017-11-29 20:08:11 +0530555 strncpy (ifr.ifr_name, tm->tun_name, sizeof (ifr.ifr_name) - 1);
556 if (ioctl (tm->dev_tap_fd, SIOCGIFINDEX, &ifr) < 0)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700557 {
558 error = clib_error_return_unix (0, "ioctl SIOCGIFINDEX");
559 goto done;
560 }
561
562 /* Bind the provisioning socket to the interface. */
Dave Barachb7b92992018-10-17 10:38:51 -0400563 clib_memset (&sll, 0, sizeof (sll));
sharath reddy1b0c9832017-11-29 20:08:11 +0530564 sll.sll_family = AF_PACKET;
565 sll.sll_ifindex = ifr.ifr_ifindex;
566 sll.sll_protocol = htons (ETH_P_ALL);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700567
sharath reddy1b0c9832017-11-29 20:08:11 +0530568 if (bind (tm->dev_tap_fd, (struct sockaddr *) &sll, sizeof (sll)) < 0)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700569 {
570 error = clib_error_return_unix (0, "bind");
571 goto done;
572 }
573 }
574
575 /* non-blocking I/O on /dev/tapX */
576 {
577 int one = 1;
578 if (ioctl (tm->dev_net_tun_fd, FIONBIO, &one) < 0)
579 {
580 error = clib_error_return_unix (0, "ioctl FIONBIO");
581 goto done;
582 }
583 }
584
585 tm->mtu_buffers = (tm->mtu_bytes + (buffer_size - 1)) / buffer_size;
586
587 ifr.ifr_mtu = tm->mtu_bytes;
588 if (ioctl (tm->dev_tap_fd, SIOCSIFMTU, &ifr) < 0)
589 {
590 error = clib_error_return_unix (0, "ioctl SIOCSIFMTU");
591 goto done;
592 }
593
594 /* get flags, modify to bring up interface... */
595 if (ioctl (tm->dev_tap_fd, SIOCGIFFLAGS, &ifr) < 0)
596 {
597 error = clib_error_return_unix (0, "ioctl SIOCGIFFLAGS");
598 goto done;
599 }
600
601 ifr.ifr_flags |= (IFF_UP | IFF_RUNNING);
602
603 if (ioctl (tm->dev_tap_fd, SIOCSIFFLAGS, &ifr) < 0)
604 {
605 error = clib_error_return_unix (0, "ioctl SIOCSIFFLAGS");
606 goto done;
607 }
608
609 if (is_ether)
610 {
611 if (ioctl (tm->dev_tap_fd, SIOCGIFHWADDR, &ifr) < 0)
sharath reddy1b0c9832017-11-29 20:08:11 +0530612 {
613 error = clib_error_return_unix (0, "ioctl SIOCGIFHWADDR");
614 goto done;
615 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700616 else
Dave Barach178cf492018-11-13 16:34:13 -0500617 clib_memcpy_fast (tm->ether_dst_mac, ifr.ifr_hwaddr.sa_data, 6);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700618 }
619
620 if (have_normal_interface)
621 {
sharath reddy1b0c9832017-11-29 20:08:11 +0530622 vnet_main_t *vnm = vnet_get_main ();
Damjan Marion5c954c42022-01-06 20:36:14 +0100623 vnet_eth_interface_registration_t eir = {};
624
625 eir.dev_class_index = tuntap_dev_class.index;
626 eir.address = tm->ether_dst_mac;
627 tm->hw_if_index = vnet_eth_register_interface (vnm, &eir);
628
Ed Warnickecb9cada2015-12-08 15:45:58 -0700629 tm->sw_if_index = tm->hw_if_index;
630 vm->os_punt_frame = tuntap_nopunt_frame;
631 }
632 else
633 {
sharath reddy1b0c9832017-11-29 20:08:11 +0530634 vnet_main_t *vnm = vnet_get_main ();
635 vnet_hw_interface_t *hi;
636
Ed Warnickecb9cada2015-12-08 15:45:58 -0700637 vm->os_punt_frame = tuntap_punt_frame;
sharath reddy1b0c9832017-11-29 20:08:11 +0530638
Ed Warnickecb9cada2015-12-08 15:45:58 -0700639 tm->hw_if_index = vnet_register_interface
sharath reddy1b0c9832017-11-29 20:08:11 +0530640 (vnm, tuntap_dev_class.index, 0 /* device instance */ ,
641 tuntap_interface_class.index, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700642 hi = vnet_get_hw_interface (vnm, tm->hw_if_index);
643 tm->sw_if_index = hi->sw_if_index;
sharath reddy1b0c9832017-11-29 20:08:11 +0530644
Ed Warnickecb9cada2015-12-08 15:45:58 -0700645 /* Interface is always up. */
sharath reddy1b0c9832017-11-29 20:08:11 +0530646 vnet_hw_interface_set_flags (vnm, tm->hw_if_index,
647 VNET_HW_INTERFACE_FLAG_LINK_UP);
648 vnet_sw_interface_set_flags (vnm, tm->sw_if_index,
649 VNET_SW_INTERFACE_FLAG_ADMIN_UP);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700650 }
651
652 {
sharath reddy1b0c9832017-11-29 20:08:11 +0530653 clib_file_t template = { 0 };
Ed Warnickecb9cada2015-12-08 15:45:58 -0700654 template.read_function = tuntap_read_ready;
655 template.file_descriptor = tm->dev_net_tun_fd;
Paul Vinciguerra5481ad42020-01-28 14:47:17 -0500656 template.description = format (0, "vnet tuntap");
Damjan Marion56dd5432017-09-08 19:52:02 +0200657 tm->clib_file_index = clib_file_add (&file_main, &template);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700658 }
659
sharath reddy1b0c9832017-11-29 20:08:11 +0530660done:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700661 if (error)
662 {
663 if (tm->dev_net_tun_fd >= 0)
664 close (tm->dev_net_tun_fd);
665 if (tm->dev_tap_fd >= 0)
666 close (tm->dev_tap_fd);
667 }
668
669 return error;
670}
671
672VLIB_CONFIG_FUNCTION (tuntap_config, "tuntap");
673
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700674/**
675 * @brief Add or Del IP4 address to tun/tap interface
676 *
677 * @param *im - ip4_main_t
678 * @param opaque - uword
679 * @param sw_if_index - u32
680 * @param *address - ip4_address_t
681 * @param is_delete - u32
682 *
683 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700684void
685tuntap_ip4_add_del_interface_address (ip4_main_t * im,
686 uword opaque,
687 u32 sw_if_index,
688 ip4_address_t * address,
689 u32 address_length,
sharath reddy1b0c9832017-11-29 20:08:11 +0530690 u32 if_address_index, u32 is_delete)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700691{
sharath reddy1b0c9832017-11-29 20:08:11 +0530692 tuntap_main_t *tm = &tuntap_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700693 struct ifreq ifr;
sharath reddy1b0c9832017-11-29 20:08:11 +0530694 subif_address_t subif_addr, *ap;
695 uword *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700696
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700697 /** Tuntap disabled, or using a "normal" interface. */
sharath reddy1b0c9832017-11-29 20:08:11 +0530698 if (tm->have_normal_interface || tm->dev_tap_fd < 0)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700699 return;
700
Neale Rannse8bad972017-08-10 11:34:12 -0700701 /* if the address is being applied to an interface that is not in
702 * the same table/VRF as this tap, then ignore it.
Paul Vinciguerrabdc0e6b2018-09-22 05:32:50 -0700703 * If we don't do this overlapping address spaces in the different tables
Neale Rannse8bad972017-08-10 11:34:12 -0700704 * breaks the linux host's routing tables */
sharath reddy1b0c9832017-11-29 20:08:11 +0530705 if (fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
706 sw_if_index) !=
707 fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4, tm->sw_if_index))
708 return;
Neale Rannse8bad972017-08-10 11:34:12 -0700709
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700710 /** See if we already know about this subif */
Dave Barachb7b92992018-10-17 10:38:51 -0400711 clib_memset (&subif_addr, 0, sizeof (subif_addr));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700712 subif_addr.sw_if_index = sw_if_index;
Dave Barach178cf492018-11-13 16:34:13 -0500713 clib_memcpy_fast (&subif_addr.addr, address, sizeof (*address));
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700714
Ed Warnickecb9cada2015-12-08 15:45:58 -0700715 p = mhash_get (&tm->subif_mhash, &subif_addr);
716
717 if (p)
718 ap = pool_elt_at_index (tm->subifs, p[0]);
719 else
720 {
721 pool_get (tm->subifs, ap);
722 *ap = subif_addr;
723 mhash_set (&tm->subif_mhash, ap, ap - tm->subifs, 0);
724 }
725
726 /* Use subif pool index to select alias device. */
Dave Barachb7b92992018-10-17 10:38:51 -0400727 clib_memset (&ifr, 0, sizeof (ifr));
sharath reddy1b0c9832017-11-29 20:08:11 +0530728 snprintf (ifr.ifr_name, sizeof (ifr.ifr_name),
729 "%s:%d", tm->tun_name, (int) (ap - tm->subifs));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700730
Igor Mikhailov (imichail)80e88162016-11-04 20:25:00 -0700731 /* the tuntap punt/inject is enabled for IPv4 RX so long as
732 * any vpp interface has an IPv4 address.
733 * this is also ref counted.
734 */
735 ip4_sw_interface_enable_disable (tm->sw_if_index, !is_delete);
736
sharath reddy1b0c9832017-11-29 20:08:11 +0530737 if (!is_delete)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700738 {
sharath reddy1b0c9832017-11-29 20:08:11 +0530739 struct sockaddr_in *sin;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700740
sharath reddy1b0c9832017-11-29 20:08:11 +0530741 sin = (struct sockaddr_in *) &ifr.ifr_addr;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700742
743 /* Set ipv4 address, netmask. */
744 sin->sin_family = AF_INET;
Dave Barach178cf492018-11-13 16:34:13 -0500745 clib_memcpy_fast (&sin->sin_addr.s_addr, address, 4);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700746 if (ioctl (tm->dev_tap_fd, SIOCSIFADDR, &ifr) < 0)
747 clib_unix_warning ("ioctl SIOCSIFADDR");
sharath reddy1b0c9832017-11-29 20:08:11 +0530748
Ed Warnickecb9cada2015-12-08 15:45:58 -0700749 sin->sin_addr.s_addr = im->fib_masks[address_length];
750 if (ioctl (tm->dev_tap_fd, SIOCSIFNETMASK, &ifr) < 0)
751 clib_unix_warning ("ioctl SIOCSIFNETMASK");
752 }
753 else
754 {
sharath reddy1b0c9832017-11-29 20:08:11 +0530755 mhash_unset (&tm->subif_mhash, &subif_addr, 0 /* old value ptr */ );
Ed Warnickecb9cada2015-12-08 15:45:58 -0700756 pool_put (tm->subifs, ap);
757 }
758
759 /* get flags, modify to bring up interface... */
760 if (ioctl (tm->dev_tap_fd, SIOCGIFFLAGS, &ifr) < 0)
761 clib_unix_warning ("ioctl SIOCGIFFLAGS");
762
763 if (is_delete)
764 ifr.ifr_flags &= ~(IFF_UP | IFF_RUNNING);
765 else
766 ifr.ifr_flags |= (IFF_UP | IFF_RUNNING);
767
768 if (ioctl (tm->dev_tap_fd, SIOCSIFFLAGS, &ifr) < 0)
769 clib_unix_warning ("ioctl SIOCSIFFLAGS");
770}
771
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700772/**
Chris Luke16bcf7d2016-09-01 14:31:46 -0400773 * @brief workaround for a known include file bug.
774 * including @c <linux/ipv6.h> causes multiple definitions if
775 * @c <netinet/in.h is also included.
Ed Warnickecb9cada2015-12-08 15:45:58 -0700776 */
sharath reddy1b0c9832017-11-29 20:08:11 +0530777struct in6_ifreq
778{
779 struct in6_addr ifr6_addr;
780 u32 ifr6_prefixlen;
781 int ifr6_ifindex;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700782};
783
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700784/**
Chris Luke16bcf7d2016-09-01 14:31:46 -0400785 * @brief Add or Del tun/tap interface address.
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700786 *
Ed Warnickecb9cada2015-12-08 15:45:58 -0700787 * Both the v6 interface address API and the way ifconfig
Paul Vinciguerrabdc0e6b2018-09-22 05:32:50 -0700788 * displays subinterfaces differ from their v4 counterparts.
Ed Warnickecb9cada2015-12-08 15:45:58 -0700789 * The code given here seems to work but YMMV.
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700790 *
791 * @param *im - ip6_main_t
792 * @param opaque - uword
793 * @param sw_if_index - u32
794 * @param *address - ip6_address_t
795 * @param address_length - u32
796 * @param if_address_index - u32
797 * @param is_delete - u32
Ed Warnickecb9cada2015-12-08 15:45:58 -0700798 */
799void
800tuntap_ip6_add_del_interface_address (ip6_main_t * im,
801 uword opaque,
802 u32 sw_if_index,
803 ip6_address_t * address,
804 u32 address_length,
sharath reddy1b0c9832017-11-29 20:08:11 +0530805 u32 if_address_index, u32 is_delete)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700806{
sharath reddy1b0c9832017-11-29 20:08:11 +0530807 tuntap_main_t *tm = &tuntap_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700808 struct ifreq ifr;
809 struct in6_ifreq ifr6;
sharath reddy1b0c9832017-11-29 20:08:11 +0530810 subif_address_t subif_addr, *ap;
811 uword *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700812
813 /* Tuntap disabled, or using a "normal" interface. */
sharath reddy1b0c9832017-11-29 20:08:11 +0530814 if (tm->have_normal_interface || tm->dev_tap_fd < 0)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700815 return;
816
Neale Rannse8bad972017-08-10 11:34:12 -0700817 /* if the address is being applied to an interface that is not in
818 * the same table/VRF as this tap, then ignore it.
Paul Vinciguerrabdc0e6b2018-09-22 05:32:50 -0700819 * If we don't do this overlapping address spaces in the different tables
Neale Rannse8bad972017-08-10 11:34:12 -0700820 * breaks the linux host's routing tables */
sharath reddy1b0c9832017-11-29 20:08:11 +0530821 if (fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP6,
822 sw_if_index) !=
823 fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP6, tm->sw_if_index))
824 return;
Neale Rannse8bad972017-08-10 11:34:12 -0700825
Ed Warnickecb9cada2015-12-08 15:45:58 -0700826 /* See if we already know about this subif */
Dave Barachb7b92992018-10-17 10:38:51 -0400827 clib_memset (&subif_addr, 0, sizeof (subif_addr));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700828 subif_addr.sw_if_index = sw_if_index;
829 subif_addr.is_v6 = 1;
Dave Barach178cf492018-11-13 16:34:13 -0500830 clib_memcpy_fast (&subif_addr.addr, address, sizeof (*address));
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700831
Ed Warnickecb9cada2015-12-08 15:45:58 -0700832 p = mhash_get (&tm->subif_mhash, &subif_addr);
833
834 if (p)
835 ap = pool_elt_at_index (tm->subifs, p[0]);
836 else
837 {
838 pool_get (tm->subifs, ap);
839 *ap = subif_addr;
840 mhash_set (&tm->subif_mhash, ap, ap - tm->subifs, 0);
841 }
842
843 /* Use subif pool index to select alias device. */
Dave Barachb7b92992018-10-17 10:38:51 -0400844 clib_memset (&ifr, 0, sizeof (ifr));
845 clib_memset (&ifr6, 0, sizeof (ifr6));
sharath reddy1b0c9832017-11-29 20:08:11 +0530846 snprintf (ifr.ifr_name, sizeof (ifr.ifr_name),
847 "%s:%d", tm->tun_name, (int) (ap - tm->subifs));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700848
Igor Mikhailov (imichail)80e88162016-11-04 20:25:00 -0700849 /* the tuntap punt/inject is enabled for IPv6 RX so long as
850 * any vpp interface has an IPv6 address.
851 * this is also ref counted.
852 */
853 ip6_sw_interface_enable_disable (tm->sw_if_index, !is_delete);
854
sharath reddy1b0c9832017-11-29 20:08:11 +0530855 if (!is_delete)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700856 {
857 int sockfd = socket (AF_INET6, SOCK_STREAM, 0);
858 if (sockfd < 0)
sharath reddy1b0c9832017-11-29 20:08:11 +0530859 clib_unix_warning ("get ifindex socket");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700860
861 if (ioctl (sockfd, SIOGIFINDEX, &ifr) < 0)
sharath reddy1b0c9832017-11-29 20:08:11 +0530862 clib_unix_warning ("get ifindex");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700863
864 ifr6.ifr6_ifindex = ifr.ifr_ifindex;
865 ifr6.ifr6_prefixlen = address_length;
Dave Barach178cf492018-11-13 16:34:13 -0500866 clib_memcpy_fast (&ifr6.ifr6_addr, address, 16);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700867
868 if (ioctl (sockfd, SIOCSIFADDR, &ifr6) < 0)
sharath reddy1b0c9832017-11-29 20:08:11 +0530869 clib_unix_warning ("set address");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700870
Dave Barach6f6f34f2016-08-08 13:05:31 -0400871 if (sockfd >= 0)
sharath reddy1b0c9832017-11-29 20:08:11 +0530872 close (sockfd);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700873 }
874 else
875 {
876 int sockfd = socket (AF_INET6, SOCK_STREAM, 0);
877 if (sockfd < 0)
sharath reddy1b0c9832017-11-29 20:08:11 +0530878 clib_unix_warning ("get ifindex socket");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700879
880 if (ioctl (sockfd, SIOGIFINDEX, &ifr) < 0)
sharath reddy1b0c9832017-11-29 20:08:11 +0530881 clib_unix_warning ("get ifindex");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700882
883 ifr6.ifr6_ifindex = ifr.ifr_ifindex;
884 ifr6.ifr6_prefixlen = address_length;
Dave Barach178cf492018-11-13 16:34:13 -0500885 clib_memcpy_fast (&ifr6.ifr6_addr, address, 16);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700886
887 if (ioctl (sockfd, SIOCDIFADDR, &ifr6) < 0)
sharath reddy1b0c9832017-11-29 20:08:11 +0530888 clib_unix_warning ("del address");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700889
Dave Barachf9c231e2016-08-05 10:10:18 -0400890 if (sockfd >= 0)
sharath reddy1b0c9832017-11-29 20:08:11 +0530891 close (sockfd);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700892
sharath reddy1b0c9832017-11-29 20:08:11 +0530893 mhash_unset (&tm->subif_mhash, &subif_addr, 0 /* old value ptr */ );
Ed Warnickecb9cada2015-12-08 15:45:58 -0700894 pool_put (tm->subifs, ap);
895 }
896}
897
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700898/**
899 * @brief TX the tun/tap frame
900 *
901 * @param *vm - vlib_main_t
902 * @param *node - vlib_node_runtime_t
903 * @param *frame - vlib_frame_t
904 *
905 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700906static void
907tuntap_punt_frame (vlib_main_t * vm,
sharath reddy1b0c9832017-11-29 20:08:11 +0530908 vlib_node_runtime_t * node, vlib_frame_t * frame)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700909{
910 tuntap_tx (vm, node, frame);
Dmitry Valter9f5b3692022-09-05 15:30:18 +0000911 vlib_frame_free (vm, frame);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700912}
913
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700914/**
915 * @brief Free the tun/tap frame
916 *
917 * @param *vm - vlib_main_t
918 * @param *node - vlib_node_runtime_t
919 * @param *frame - vlib_frame_t
920 *
921 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700922static void
923tuntap_nopunt_frame (vlib_main_t * vm,
sharath reddy1b0c9832017-11-29 20:08:11 +0530924 vlib_node_runtime_t * node, vlib_frame_t * frame)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700925{
Damjan Mariona3d59862018-11-10 10:23:00 +0100926 u32 *buffers = vlib_frame_vector_args (frame);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700927 uword n_packets = frame->n_vectors;
928 vlib_buffer_free (vm, buffers, n_packets);
Dmitry Valter9f5b3692022-09-05 15:30:18 +0000929 vlib_frame_free (vm, frame);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700930}
931
932VNET_HW_INTERFACE_CLASS (tuntap_interface_class,static) = {
933 .name = "tuntap",
Neale Rannsb80c5362016-10-08 13:03:40 +0100934 .flags = VNET_HW_INTERFACE_CLASS_FLAG_P2P,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700935};
936
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700937/**
938 * @brief Format tun/tap interface name
939 *
940 * @param *s - u8 - formatter string
941 * @param *args - va_list
942 *
943 * @return *s - u8 - formatted string
944 *
945 */
sharath reddy1b0c9832017-11-29 20:08:11 +0530946static u8 *
947format_tuntap_interface_name (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700948{
949 u32 i = va_arg (*args, u32);
950
951 s = format (s, "tuntap-%d", i);
952 return s;
953}
954
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700955/**
956 * @brief TX packet out tun/tap
957 *
958 * @param *vm - vlib_main_t
959 * @param *node - vlib_node_runtime_t
960 * @param *frame - vlib_frame_t
961 *
962 * @return n_buffers - uword - Packets transmitted
963 *
964 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700965static uword
966tuntap_intfc_tx (vlib_main_t * vm,
sharath reddy1b0c9832017-11-29 20:08:11 +0530967 vlib_node_runtime_t * node, vlib_frame_t * frame)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700968{
sharath reddy1b0c9832017-11-29 20:08:11 +0530969 tuntap_main_t *tm = &tuntap_main;
Damjan Mariona3d59862018-11-10 10:23:00 +0100970 u32 *buffers = vlib_frame_vector_args (frame);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700971 uword n_buffers = frame->n_vectors;
972
973 /* Normal interface transmit happens only on the normal interface... */
974 if (tm->have_normal_interface)
975 return tuntap_tx (vm, node, frame);
976
977 vlib_buffer_free (vm, buffers, n_buffers);
978 return n_buffers;
979}
980
981VNET_DEVICE_CLASS (tuntap_dev_class,static) = {
982 .name = "tuntap",
983 .tx_function = tuntap_intfc_tx,
984 .format_device_name = format_tuntap_interface_name,
985};
986
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700987/**
988 * @brief tun/tap node init
989 *
990 * @param *vm - vlib_main_t
991 *
992 * @return error - clib_error_t
993 *
994 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700995static clib_error_t *
996tuntap_init (vlib_main_t * vm)
997{
sharath reddy1b0c9832017-11-29 20:08:11 +0530998 ip4_main_t *im4 = &ip4_main;
999 ip6_main_t *im6 = &ip6_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001000 ip4_add_del_interface_address_callback_t cb4;
1001 ip6_add_del_interface_address_callback_t cb6;
sharath reddy1b0c9832017-11-29 20:08:11 +05301002 tuntap_main_t *tm = &tuntap_main;
1003 vlib_thread_main_t *m = vlib_get_thread_main ();
Ed Warnickecb9cada2015-12-08 15:45:58 -07001004
sharath reddy1b0c9832017-11-29 20:08:11 +05301005 mhash_init (&tm->subif_mhash, sizeof (u32), sizeof (subif_address_t));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001006
1007 cb4.function = tuntap_ip4_add_del_interface_address;
1008 cb4.function_opaque = 0;
1009 vec_add1 (im4->add_del_interface_address_callbacks, cb4);
1010
1011 cb6.function = tuntap_ip6_add_del_interface_address;
1012 cb6.function_opaque = 0;
1013 vec_add1 (im6->add_del_interface_address_callbacks, cb6);
Steven4cd25762017-10-05 00:12:33 -07001014 vec_validate_aligned (tm->threads, m->n_vlib_mains - 1,
1015 CLIB_CACHE_LINE_BYTES);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001016
1017 return 0;
1018}
1019
Dave Barachf8d50682019-05-14 18:01:44 -04001020VLIB_INIT_FUNCTION (tuntap_init) =
1021{
1022 .runs_after = VLIB_INITS("ip4_init"),
1023};
sharath reddy1b0c9832017-11-29 20:08:11 +05301024
1025/*
1026 * fd.io coding-style-patch-verification: ON
1027 *
1028 * Local Variables:
1029 * eval: (c-set-style "gnu")
1030 * End:
1031 */