blob: 72c4f73e6df843c4b94026a8d67263df80020b08 [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)
175 _vec_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
sharath reddy1b0c9832017-11-29 20:08:11 +0530220/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700221VLIB_REGISTER_NODE (tuntap_tx_node,static) = {
222 .function = tuntap_tx,
223 .name = "tuntap-tx",
224 .type = VLIB_NODE_TYPE_INTERNAL,
225 .vector_size = 4,
226};
sharath reddy1b0c9832017-11-29 20:08:11 +0530227/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700228
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700229/**
230 * @brief TUNTAP receive node
231 * @node tuntap-rx
232 *
233 * @param *vm - vlib_main_t
234 * @param *node - vlib_node_runtime_t
235 * @param *frame - vlib_frame_t
236 *
237 * @return rc - uword
238 *
239 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700240static uword
sharath reddy1b0c9832017-11-29 20:08:11 +0530241tuntap_rx (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700242{
sharath reddy1b0c9832017-11-29 20:08:11 +0530243 tuntap_main_t *tm = &tuntap_main;
244 vlib_buffer_t *b;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700245 u32 bi;
Damjan Marion8934a042019-02-09 23:29:26 +0100246 const uword buffer_size = vlib_buffer_get_default_data_size (vm);
Damjan Marion067cd622018-07-11 12:47:43 +0200247 u16 thread_index = vm->thread_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700248
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700249 /** Make sure we have some RX buffers. */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700250 {
Steven4cd25762017-10-05 00:12:33 -0700251 uword n_left = vec_len (tm->threads[thread_index].rx_buffers);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700252 uword n_alloc;
253
254 if (n_left < VLIB_FRAME_SIZE / 2)
255 {
sharath reddy1b0c9832017-11-29 20:08:11 +0530256 if (!tm->threads[thread_index].rx_buffers)
Steven4cd25762017-10-05 00:12:33 -0700257 vec_alloc (tm->threads[thread_index].rx_buffers, VLIB_FRAME_SIZE);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700258
sharath reddy1b0c9832017-11-29 20:08:11 +0530259 n_alloc =
260 vlib_buffer_alloc (vm,
261 tm->threads[thread_index].rx_buffers + n_left,
262 VLIB_FRAME_SIZE - n_left);
Steven4cd25762017-10-05 00:12:33 -0700263 _vec_len (tm->threads[thread_index].rx_buffers) = n_left + n_alloc;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700264 }
265 }
266
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700267 /** Allocate RX buffers from end of rx_buffers.
Ed Warnickecb9cada2015-12-08 15:45:58 -0700268 Turn them into iovecs to pass to readv. */
269 {
Steven4cd25762017-10-05 00:12:33 -0700270 uword i_rx = vec_len (tm->threads[thread_index].rx_buffers) - 1;
sharath reddy1b0c9832017-11-29 20:08:11 +0530271 vlib_buffer_t *b;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700272 word i, n_bytes_left, n_bytes_in_packet;
273
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700274 /** We should have enough buffers left for an MTU sized packet. */
sharath reddy1b0c9832017-11-29 20:08:11 +0530275 ASSERT (vec_len (tm->threads[thread_index].rx_buffers) >=
276 tm->mtu_buffers);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700277
Steven4cd25762017-10-05 00:12:33 -0700278 vec_validate (tm->threads[thread_index].iovecs, tm->mtu_buffers - 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700279 for (i = 0; i < tm->mtu_buffers; i++)
280 {
sharath reddy1b0c9832017-11-29 20:08:11 +0530281 b =
282 vlib_get_buffer (vm,
283 tm->threads[thread_index].rx_buffers[i_rx - i]);
Steven4cd25762017-10-05 00:12:33 -0700284 tm->threads[thread_index].iovecs[i].iov_base = b->data;
285 tm->threads[thread_index].iovecs[i].iov_len = buffer_size;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700286 }
287
sharath reddy1b0c9832017-11-29 20:08:11 +0530288 n_bytes_left =
289 readv (tm->dev_net_tun_fd, tm->threads[thread_index].iovecs,
290 tm->mtu_buffers);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700291 n_bytes_in_packet = n_bytes_left;
292 if (n_bytes_left <= 0)
293 {
sharath reddy1b0c9832017-11-29 20:08:11 +0530294 if (errno != EAGAIN)
295 clib_unix_warning ("readv %d", n_bytes_left);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700296 return 0;
297 }
298
Steven4cd25762017-10-05 00:12:33 -0700299 bi = tm->threads[thread_index].rx_buffers[i_rx];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700300
301 while (1)
302 {
Steven4cd25762017-10-05 00:12:33 -0700303 b = vlib_get_buffer (vm, tm->threads[thread_index].rx_buffers[i_rx]);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700304 b->flags = 0;
305 b->current_data = 0;
sharath reddy1b0c9832017-11-29 20:08:11 +0530306 b->current_length =
307 n_bytes_left < buffer_size ? n_bytes_left : buffer_size;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700308
309 n_bytes_left -= buffer_size;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700310
311 if (n_bytes_left <= 0)
sharath reddy1b0c9832017-11-29 20:08:11 +0530312 {
313 break;
314 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700315
316 i_rx--;
317 b->flags |= VLIB_BUFFER_NEXT_PRESENT;
Steven4cd25762017-10-05 00:12:33 -0700318 b->next_buffer = tm->threads[thread_index].rx_buffers[i_rx];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700319 }
320
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700321 /** Interface counters for tuntap interface. */
322 vlib_increment_combined_counter
sharath reddy1b0c9832017-11-29 20:08:11 +0530323 (vnet_main.interface_main.combined_sw_if_counters
324 + VNET_INTERFACE_COUNTER_RX,
325 thread_index, tm->sw_if_index, 1, n_bytes_in_packet);
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700326
Steven4cd25762017-10-05 00:12:33 -0700327 _vec_len (tm->threads[thread_index].rx_buffers) = i_rx;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700328 }
329
330 b = vlib_get_buffer (vm, bi);
331
332 {
333 u32 next_index;
334 uword n_trace = vlib_get_trace_count (vm, node);
335
336 vnet_buffer (b)->sw_if_index[VLIB_RX] = tm->sw_if_index;
sharath reddy1b0c9832017-11-29 20:08:11 +0530337 vnet_buffer (b)->sw_if_index[VLIB_TX] = (u32) ~ 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700338
339 /*
340 * Turn this on if you run into
341 * "bad monkey" contexts, and you want to know exactly
342 * which nodes they've visited...
343 */
344 if (VLIB_BUFFER_TRACE_TRAJECTORY)
sharath reddy1b0c9832017-11-29 20:08:11 +0530345 b->pre_data[0] = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700346
347 b->error = node->errors[0];
348
349 if (tm->is_ether)
350 {
Damjan Marion8bdc63b2016-11-02 14:48:21 +0100351 next_index = VNET_DEVICE_INPUT_NEXT_ETHERNET_INPUT;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700352 }
353 else
354 switch (b->data[0] & 0xf0)
sharath reddy1b0c9832017-11-29 20:08:11 +0530355 {
356 case 0x40:
357 next_index = VNET_DEVICE_INPUT_NEXT_IP4_INPUT;
358 break;
359 case 0x60:
360 next_index = VNET_DEVICE_INPUT_NEXT_IP6_INPUT;
361 break;
362 default:
363 next_index = VNET_DEVICE_INPUT_NEXT_DROP;
364 break;
365 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700366
367 /* The linux kernel couldn't care less if our interface is up */
368 if (tm->have_normal_interface)
369 {
sharath reddy1b0c9832017-11-29 20:08:11 +0530370 vnet_main_t *vnm = vnet_get_main ();
371 vnet_sw_interface_t *si;
372 si = vnet_get_sw_interface (vnm, tm->sw_if_index);
373 if (!(si->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP))
374 next_index = VNET_DEVICE_INPUT_NEXT_DROP;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700375 }
376
Damjan Marion35af9e52017-03-06 12:02:50 +0100377 vnet_feature_start_device_input_x1 (tm->sw_if_index, &next_index, b);
Damjan Marion22311502016-10-28 20:30:15 +0200378
Ed Warnickecb9cada2015-12-08 15:45:58 -0700379 vlib_set_next_frame_buffer (vm, node, next_index, bi);
380
381 if (n_trace > 0)
382 {
sharath reddy1b0c9832017-11-29 20:08:11 +0530383 vlib_trace_buffer (vm, node, next_index, b, /* follow_chain */ 1);
384 vlib_set_trace_count (vm, node, n_trace - 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700385 }
386 }
387
388 return 1;
389}
390
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700391/**
392 * @brief TUNTAP_RX error strings
393 */
sharath reddy1b0c9832017-11-29 20:08:11 +0530394static char *tuntap_rx_error_strings[] = {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700395 "unknown packet type",
396};
397
sharath reddy1b0c9832017-11-29 20:08:11 +0530398/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700399VLIB_REGISTER_NODE (tuntap_rx_node,static) = {
400 .function = tuntap_rx,
Damjan Marion7ca5aaa2019-09-24 18:10:49 +0200401 .flags = VLIB_NODE_FLAG_TRACE_SUPPORTED,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700402 .name = "tuntap-rx",
Damjan Marion51327ac2016-11-09 11:59:42 +0100403 .sibling_of = "device-input",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700404 .type = VLIB_NODE_TYPE_INPUT,
405 .state = VLIB_NODE_STATE_INTERRUPT,
406 .vector_size = 4,
407 .n_errors = 1,
408 .error_strings = tuntap_rx_error_strings,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700409};
sharath reddy1b0c9832017-11-29 20:08:11 +0530410/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700411
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700412/**
413 * @brief Gets called when file descriptor is ready from epoll.
414 *
Damjan Marion56dd5432017-09-08 19:52:02 +0200415 * @param *uf - clib_file_t
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700416 *
417 * @return error - clib_error_t
418 */
sharath reddy1b0c9832017-11-29 20:08:11 +0530419static clib_error_t *
420tuntap_read_ready (clib_file_t * uf)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700421{
sharath reddy1b0c9832017-11-29 20:08:11 +0530422 vlib_main_t *vm = vlib_get_main ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700423 vlib_node_set_interrupt_pending (vm, tuntap_rx_node.index);
424 return 0;
425}
426
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700427/**
428 * @brief Clean up the tun/tap device
429 *
430 * @param *vm - vlib_main_t
431 *
432 * @return error - clib_error_t
433 *
Ed Warnickecb9cada2015-12-08 15:45:58 -0700434 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700435static clib_error_t *
436tuntap_exit (vlib_main_t * vm)
437{
438 tuntap_main_t *tm = &tuntap_main;
439 struct ifreq ifr;
440 int sfd;
441
442 /* Not present. */
sharath reddy1b0c9832017-11-29 20:08:11 +0530443 if (!tm->dev_net_tun_fd || tm->dev_net_tun_fd < 0)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700444 return 0;
445
446 sfd = socket (AF_INET, SOCK_STREAM, 0);
447 if (sfd < 0)
sharath reddy1b0c9832017-11-29 20:08:11 +0530448 clib_unix_warning ("provisioning socket");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700449
Dave Barachb7b92992018-10-17 10:38:51 -0400450 clib_memset (&ifr, 0, sizeof (ifr));
sharath reddy1b0c9832017-11-29 20:08:11 +0530451 strncpy (ifr.ifr_name, tm->tun_name, sizeof (ifr.ifr_name) - 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700452
453 /* get flags, modify to bring down interface... */
454 if (ioctl (sfd, SIOCGIFFLAGS, &ifr) < 0)
455 clib_unix_warning ("SIOCGIFFLAGS");
456
457 ifr.ifr_flags &= ~(IFF_UP | IFF_RUNNING);
458
459 if (ioctl (sfd, SIOCSIFFLAGS, &ifr) < 0)
460 clib_unix_warning ("SIOCSIFFLAGS");
461
462 /* Turn off persistence */
463 if (ioctl (tm->dev_net_tun_fd, TUNSETPERSIST, 0) < 0)
464 clib_unix_warning ("TUNSETPERSIST");
sharath reddy1b0c9832017-11-29 20:08:11 +0530465 close (tm->dev_tap_fd);
Dave Barachf9c231e2016-08-05 10:10:18 -0400466 if (tm->dev_net_tun_fd >= 0)
sharath reddy1b0c9832017-11-29 20:08:11 +0530467 close (tm->dev_net_tun_fd);
Dave Barach6f6f34f2016-08-08 13:05:31 -0400468 if (sfd >= 0)
469 close (sfd);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700470
471 return 0;
472}
473
474VLIB_MAIN_LOOP_EXIT_FUNCTION (tuntap_exit);
475
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700476/**
477 * @brief CLI function for tun/tap config
478 *
479 * @param *vm - vlib_main_t
480 * @param *input - unformat_input_t
481 *
482 * @return error - clib_error_t
483 *
484 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700485static clib_error_t *
486tuntap_config (vlib_main_t * vm, unformat_input_t * input)
487{
488 tuntap_main_t *tm = &tuntap_main;
sharath reddy1b0c9832017-11-29 20:08:11 +0530489 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700490 struct ifreq ifr;
sharath reddy1b0c9832017-11-29 20:08:11 +0530491 u8 *name;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700492 int flags = IFF_TUN | IFF_NO_PI;
493 int is_enabled = 0, is_ether = 0, have_normal_interface = 0;
Damjan Marion8934a042019-02-09 23:29:26 +0100494 const uword buffer_size = vlib_buffer_get_default_data_size (vm);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700495
496 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
497 {
498 if (unformat (input, "mtu %d", &tm->mtu_bytes))
499 ;
500 else if (unformat (input, "enable"))
sharath reddy1b0c9832017-11-29 20:08:11 +0530501 is_enabled = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700502 else if (unformat (input, "disable"))
sharath reddy1b0c9832017-11-29 20:08:11 +0530503 is_enabled = 0;
504 else if (unformat (input, "ethernet") || unformat (input, "ether"))
505 is_ether = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700506 else if (unformat (input, "have-normal-interface") ||
sharath reddy1b0c9832017-11-29 20:08:11 +0530507 unformat (input, "have-normal"))
508 have_normal_interface = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700509 else if (unformat (input, "name %s", &name))
510 tm->tun_name = (char *) name;
511 else
512 return clib_error_return (0, "unknown input `%U'",
513 format_unformat_error, input);
514 }
515
516 tm->dev_net_tun_fd = -1;
517 tm->dev_tap_fd = -1;
518
519 if (is_enabled == 0)
520 return 0;
521
sharath reddy1b0c9832017-11-29 20:08:11 +0530522 if (geteuid ())
Ed Warnickecb9cada2015-12-08 15:45:58 -0700523 {
524 clib_warning ("tuntap disabled: must be superuser");
525 return 0;
sharath reddy1b0c9832017-11-29 20:08:11 +0530526 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700527
528 tm->is_ether = is_ether;
529 tm->have_normal_interface = have_normal_interface;
530
531 if (is_ether)
532 flags = IFF_TAP | IFF_NO_PI;
533
534 if ((tm->dev_net_tun_fd = open ("/dev/net/tun", O_RDWR)) < 0)
535 {
536 error = clib_error_return_unix (0, "open /dev/net/tun");
537 goto done;
538 }
539
Dave Barachb7b92992018-10-17 10:38:51 -0400540 clib_memset (&ifr, 0, sizeof (ifr));
sharath reddy1b0c9832017-11-29 20:08:11 +0530541 strncpy (ifr.ifr_name, tm->tun_name, sizeof (ifr.ifr_name) - 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700542 ifr.ifr_flags = flags;
sharath reddy1b0c9832017-11-29 20:08:11 +0530543 if (ioctl (tm->dev_net_tun_fd, TUNSETIFF, (void *) &ifr) < 0)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700544 {
545 error = clib_error_return_unix (0, "ioctl TUNSETIFF");
546 goto done;
547 }
sharath reddy1b0c9832017-11-29 20:08:11 +0530548
Ed Warnickecb9cada2015-12-08 15:45:58 -0700549 /* Make it persistent, at least until we split. */
550 if (ioctl (tm->dev_net_tun_fd, TUNSETPERSIST, 1) < 0)
551 {
552 error = clib_error_return_unix (0, "TUNSETPERSIST");
553 goto done;
554 }
555
556 /* Open a provisioning socket */
sharath reddy1b0c9832017-11-29 20:08:11 +0530557 if ((tm->dev_tap_fd = socket (PF_PACKET, SOCK_RAW, htons (ETH_P_ALL))) < 0)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700558 {
559 error = clib_error_return_unix (0, "socket");
560 goto done;
561 }
562
563 /* Find the interface index. */
564 {
565 struct ifreq ifr;
566 struct sockaddr_ll sll;
567
Dave Barachb7b92992018-10-17 10:38:51 -0400568 clib_memset (&ifr, 0, sizeof (ifr));
sharath reddy1b0c9832017-11-29 20:08:11 +0530569 strncpy (ifr.ifr_name, tm->tun_name, sizeof (ifr.ifr_name) - 1);
570 if (ioctl (tm->dev_tap_fd, SIOCGIFINDEX, &ifr) < 0)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700571 {
572 error = clib_error_return_unix (0, "ioctl SIOCGIFINDEX");
573 goto done;
574 }
575
576 /* Bind the provisioning socket to the interface. */
Dave Barachb7b92992018-10-17 10:38:51 -0400577 clib_memset (&sll, 0, sizeof (sll));
sharath reddy1b0c9832017-11-29 20:08:11 +0530578 sll.sll_family = AF_PACKET;
579 sll.sll_ifindex = ifr.ifr_ifindex;
580 sll.sll_protocol = htons (ETH_P_ALL);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700581
sharath reddy1b0c9832017-11-29 20:08:11 +0530582 if (bind (tm->dev_tap_fd, (struct sockaddr *) &sll, sizeof (sll)) < 0)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700583 {
584 error = clib_error_return_unix (0, "bind");
585 goto done;
586 }
587 }
588
589 /* non-blocking I/O on /dev/tapX */
590 {
591 int one = 1;
592 if (ioctl (tm->dev_net_tun_fd, FIONBIO, &one) < 0)
593 {
594 error = clib_error_return_unix (0, "ioctl FIONBIO");
595 goto done;
596 }
597 }
598
599 tm->mtu_buffers = (tm->mtu_bytes + (buffer_size - 1)) / buffer_size;
600
601 ifr.ifr_mtu = tm->mtu_bytes;
602 if (ioctl (tm->dev_tap_fd, SIOCSIFMTU, &ifr) < 0)
603 {
604 error = clib_error_return_unix (0, "ioctl SIOCSIFMTU");
605 goto done;
606 }
607
608 /* get flags, modify to bring up interface... */
609 if (ioctl (tm->dev_tap_fd, SIOCGIFFLAGS, &ifr) < 0)
610 {
611 error = clib_error_return_unix (0, "ioctl SIOCGIFFLAGS");
612 goto done;
613 }
614
615 ifr.ifr_flags |= (IFF_UP | IFF_RUNNING);
616
617 if (ioctl (tm->dev_tap_fd, SIOCSIFFLAGS, &ifr) < 0)
618 {
619 error = clib_error_return_unix (0, "ioctl SIOCSIFFLAGS");
620 goto done;
621 }
622
623 if (is_ether)
624 {
625 if (ioctl (tm->dev_tap_fd, SIOCGIFHWADDR, &ifr) < 0)
sharath reddy1b0c9832017-11-29 20:08:11 +0530626 {
627 error = clib_error_return_unix (0, "ioctl SIOCGIFHWADDR");
628 goto done;
629 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700630 else
Dave Barach178cf492018-11-13 16:34:13 -0500631 clib_memcpy_fast (tm->ether_dst_mac, ifr.ifr_hwaddr.sa_data, 6);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700632 }
633
634 if (have_normal_interface)
635 {
sharath reddy1b0c9832017-11-29 20:08:11 +0530636 vnet_main_t *vnm = vnet_get_main ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700637 error = ethernet_register_interface
sharath reddy1b0c9832017-11-29 20:08:11 +0530638 (vnm, tuntap_dev_class.index, 0 /* device instance */ ,
639 tm->ether_dst_mac /* ethernet address */ ,
640 &tm->hw_if_index, 0 /* flag change */ );
Ed Warnickecb9cada2015-12-08 15:45:58 -0700641 if (error)
sharath reddy1b0c9832017-11-29 20:08:11 +0530642 clib_error_report (error);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700643 tm->sw_if_index = tm->hw_if_index;
644 vm->os_punt_frame = tuntap_nopunt_frame;
645 }
646 else
647 {
sharath reddy1b0c9832017-11-29 20:08:11 +0530648 vnet_main_t *vnm = vnet_get_main ();
649 vnet_hw_interface_t *hi;
650
Ed Warnickecb9cada2015-12-08 15:45:58 -0700651 vm->os_punt_frame = tuntap_punt_frame;
sharath reddy1b0c9832017-11-29 20:08:11 +0530652
Ed Warnickecb9cada2015-12-08 15:45:58 -0700653 tm->hw_if_index = vnet_register_interface
sharath reddy1b0c9832017-11-29 20:08:11 +0530654 (vnm, tuntap_dev_class.index, 0 /* device instance */ ,
655 tuntap_interface_class.index, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700656 hi = vnet_get_hw_interface (vnm, tm->hw_if_index);
657 tm->sw_if_index = hi->sw_if_index;
sharath reddy1b0c9832017-11-29 20:08:11 +0530658
Ed Warnickecb9cada2015-12-08 15:45:58 -0700659 /* Interface is always up. */
sharath reddy1b0c9832017-11-29 20:08:11 +0530660 vnet_hw_interface_set_flags (vnm, tm->hw_if_index,
661 VNET_HW_INTERFACE_FLAG_LINK_UP);
662 vnet_sw_interface_set_flags (vnm, tm->sw_if_index,
663 VNET_SW_INTERFACE_FLAG_ADMIN_UP);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700664 }
665
666 {
sharath reddy1b0c9832017-11-29 20:08:11 +0530667 clib_file_t template = { 0 };
Ed Warnickecb9cada2015-12-08 15:45:58 -0700668 template.read_function = tuntap_read_ready;
669 template.file_descriptor = tm->dev_net_tun_fd;
Damjan Marion56dd5432017-09-08 19:52:02 +0200670 tm->clib_file_index = clib_file_add (&file_main, &template);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700671 }
672
sharath reddy1b0c9832017-11-29 20:08:11 +0530673done:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700674 if (error)
675 {
676 if (tm->dev_net_tun_fd >= 0)
677 close (tm->dev_net_tun_fd);
678 if (tm->dev_tap_fd >= 0)
679 close (tm->dev_tap_fd);
680 }
681
682 return error;
683}
684
685VLIB_CONFIG_FUNCTION (tuntap_config, "tuntap");
686
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700687/**
688 * @brief Add or Del IP4 address to tun/tap interface
689 *
690 * @param *im - ip4_main_t
691 * @param opaque - uword
692 * @param sw_if_index - u32
693 * @param *address - ip4_address_t
694 * @param is_delete - u32
695 *
696 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700697void
698tuntap_ip4_add_del_interface_address (ip4_main_t * im,
699 uword opaque,
700 u32 sw_if_index,
701 ip4_address_t * address,
702 u32 address_length,
sharath reddy1b0c9832017-11-29 20:08:11 +0530703 u32 if_address_index, u32 is_delete)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700704{
sharath reddy1b0c9832017-11-29 20:08:11 +0530705 tuntap_main_t *tm = &tuntap_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700706 struct ifreq ifr;
sharath reddy1b0c9832017-11-29 20:08:11 +0530707 subif_address_t subif_addr, *ap;
708 uword *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700709
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700710 /** Tuntap disabled, or using a "normal" interface. */
sharath reddy1b0c9832017-11-29 20:08:11 +0530711 if (tm->have_normal_interface || tm->dev_tap_fd < 0)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700712 return;
713
Neale Rannse8bad972017-08-10 11:34:12 -0700714 /* if the address is being applied to an interface that is not in
715 * the same table/VRF as this tap, then ignore it.
Paul Vinciguerrabdc0e6b2018-09-22 05:32:50 -0700716 * If we don't do this overlapping address spaces in the different tables
Neale Rannse8bad972017-08-10 11:34:12 -0700717 * breaks the linux host's routing tables */
sharath reddy1b0c9832017-11-29 20:08:11 +0530718 if (fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
719 sw_if_index) !=
720 fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4, tm->sw_if_index))
721 return;
Neale Rannse8bad972017-08-10 11:34:12 -0700722
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700723 /** See if we already know about this subif */
Dave Barachb7b92992018-10-17 10:38:51 -0400724 clib_memset (&subif_addr, 0, sizeof (subif_addr));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700725 subif_addr.sw_if_index = sw_if_index;
Dave Barach178cf492018-11-13 16:34:13 -0500726 clib_memcpy_fast (&subif_addr.addr, address, sizeof (*address));
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700727
Ed Warnickecb9cada2015-12-08 15:45:58 -0700728 p = mhash_get (&tm->subif_mhash, &subif_addr);
729
730 if (p)
731 ap = pool_elt_at_index (tm->subifs, p[0]);
732 else
733 {
734 pool_get (tm->subifs, ap);
735 *ap = subif_addr;
736 mhash_set (&tm->subif_mhash, ap, ap - tm->subifs, 0);
737 }
738
739 /* Use subif pool index to select alias device. */
Dave Barachb7b92992018-10-17 10:38:51 -0400740 clib_memset (&ifr, 0, sizeof (ifr));
sharath reddy1b0c9832017-11-29 20:08:11 +0530741 snprintf (ifr.ifr_name, sizeof (ifr.ifr_name),
742 "%s:%d", tm->tun_name, (int) (ap - tm->subifs));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700743
Igor Mikhailov (imichail)80e88162016-11-04 20:25:00 -0700744 /* the tuntap punt/inject is enabled for IPv4 RX so long as
745 * any vpp interface has an IPv4 address.
746 * this is also ref counted.
747 */
748 ip4_sw_interface_enable_disable (tm->sw_if_index, !is_delete);
749
sharath reddy1b0c9832017-11-29 20:08:11 +0530750 if (!is_delete)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700751 {
sharath reddy1b0c9832017-11-29 20:08:11 +0530752 struct sockaddr_in *sin;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700753
sharath reddy1b0c9832017-11-29 20:08:11 +0530754 sin = (struct sockaddr_in *) &ifr.ifr_addr;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700755
756 /* Set ipv4 address, netmask. */
757 sin->sin_family = AF_INET;
Dave Barach178cf492018-11-13 16:34:13 -0500758 clib_memcpy_fast (&sin->sin_addr.s_addr, address, 4);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700759 if (ioctl (tm->dev_tap_fd, SIOCSIFADDR, &ifr) < 0)
760 clib_unix_warning ("ioctl SIOCSIFADDR");
sharath reddy1b0c9832017-11-29 20:08:11 +0530761
Ed Warnickecb9cada2015-12-08 15:45:58 -0700762 sin->sin_addr.s_addr = im->fib_masks[address_length];
763 if (ioctl (tm->dev_tap_fd, SIOCSIFNETMASK, &ifr) < 0)
764 clib_unix_warning ("ioctl SIOCSIFNETMASK");
765 }
766 else
767 {
sharath reddy1b0c9832017-11-29 20:08:11 +0530768 mhash_unset (&tm->subif_mhash, &subif_addr, 0 /* old value ptr */ );
Ed Warnickecb9cada2015-12-08 15:45:58 -0700769 pool_put (tm->subifs, ap);
770 }
771
772 /* get flags, modify to bring up interface... */
773 if (ioctl (tm->dev_tap_fd, SIOCGIFFLAGS, &ifr) < 0)
774 clib_unix_warning ("ioctl SIOCGIFFLAGS");
775
776 if (is_delete)
777 ifr.ifr_flags &= ~(IFF_UP | IFF_RUNNING);
778 else
779 ifr.ifr_flags |= (IFF_UP | IFF_RUNNING);
780
781 if (ioctl (tm->dev_tap_fd, SIOCSIFFLAGS, &ifr) < 0)
782 clib_unix_warning ("ioctl SIOCSIFFLAGS");
783}
784
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700785/**
Chris Luke16bcf7d2016-09-01 14:31:46 -0400786 * @brief workaround for a known include file bug.
787 * including @c <linux/ipv6.h> causes multiple definitions if
788 * @c <netinet/in.h is also included.
Ed Warnickecb9cada2015-12-08 15:45:58 -0700789 */
sharath reddy1b0c9832017-11-29 20:08:11 +0530790struct in6_ifreq
791{
792 struct in6_addr ifr6_addr;
793 u32 ifr6_prefixlen;
794 int ifr6_ifindex;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700795};
796
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700797/**
Chris Luke16bcf7d2016-09-01 14:31:46 -0400798 * @brief Add or Del tun/tap interface address.
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700799 *
Ed Warnickecb9cada2015-12-08 15:45:58 -0700800 * Both the v6 interface address API and the way ifconfig
Paul Vinciguerrabdc0e6b2018-09-22 05:32:50 -0700801 * displays subinterfaces differ from their v4 counterparts.
Ed Warnickecb9cada2015-12-08 15:45:58 -0700802 * The code given here seems to work but YMMV.
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700803 *
804 * @param *im - ip6_main_t
805 * @param opaque - uword
806 * @param sw_if_index - u32
807 * @param *address - ip6_address_t
808 * @param address_length - u32
809 * @param if_address_index - u32
810 * @param is_delete - u32
Ed Warnickecb9cada2015-12-08 15:45:58 -0700811 */
812void
813tuntap_ip6_add_del_interface_address (ip6_main_t * im,
814 uword opaque,
815 u32 sw_if_index,
816 ip6_address_t * address,
817 u32 address_length,
sharath reddy1b0c9832017-11-29 20:08:11 +0530818 u32 if_address_index, u32 is_delete)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700819{
sharath reddy1b0c9832017-11-29 20:08:11 +0530820 tuntap_main_t *tm = &tuntap_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700821 struct ifreq ifr;
822 struct in6_ifreq ifr6;
sharath reddy1b0c9832017-11-29 20:08:11 +0530823 subif_address_t subif_addr, *ap;
824 uword *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700825
826 /* Tuntap disabled, or using a "normal" interface. */
sharath reddy1b0c9832017-11-29 20:08:11 +0530827 if (tm->have_normal_interface || tm->dev_tap_fd < 0)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700828 return;
829
Neale Rannse8bad972017-08-10 11:34:12 -0700830 /* if the address is being applied to an interface that is not in
831 * the same table/VRF as this tap, then ignore it.
Paul Vinciguerrabdc0e6b2018-09-22 05:32:50 -0700832 * If we don't do this overlapping address spaces in the different tables
Neale Rannse8bad972017-08-10 11:34:12 -0700833 * breaks the linux host's routing tables */
sharath reddy1b0c9832017-11-29 20:08:11 +0530834 if (fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP6,
835 sw_if_index) !=
836 fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP6, tm->sw_if_index))
837 return;
Neale Rannse8bad972017-08-10 11:34:12 -0700838
Ed Warnickecb9cada2015-12-08 15:45:58 -0700839 /* See if we already know about this subif */
Dave Barachb7b92992018-10-17 10:38:51 -0400840 clib_memset (&subif_addr, 0, sizeof (subif_addr));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700841 subif_addr.sw_if_index = sw_if_index;
842 subif_addr.is_v6 = 1;
Dave Barach178cf492018-11-13 16:34:13 -0500843 clib_memcpy_fast (&subif_addr.addr, address, sizeof (*address));
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700844
Ed Warnickecb9cada2015-12-08 15:45:58 -0700845 p = mhash_get (&tm->subif_mhash, &subif_addr);
846
847 if (p)
848 ap = pool_elt_at_index (tm->subifs, p[0]);
849 else
850 {
851 pool_get (tm->subifs, ap);
852 *ap = subif_addr;
853 mhash_set (&tm->subif_mhash, ap, ap - tm->subifs, 0);
854 }
855
856 /* Use subif pool index to select alias device. */
Dave Barachb7b92992018-10-17 10:38:51 -0400857 clib_memset (&ifr, 0, sizeof (ifr));
858 clib_memset (&ifr6, 0, sizeof (ifr6));
sharath reddy1b0c9832017-11-29 20:08:11 +0530859 snprintf (ifr.ifr_name, sizeof (ifr.ifr_name),
860 "%s:%d", tm->tun_name, (int) (ap - tm->subifs));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700861
Igor Mikhailov (imichail)80e88162016-11-04 20:25:00 -0700862 /* the tuntap punt/inject is enabled for IPv6 RX so long as
863 * any vpp interface has an IPv6 address.
864 * this is also ref counted.
865 */
866 ip6_sw_interface_enable_disable (tm->sw_if_index, !is_delete);
867
sharath reddy1b0c9832017-11-29 20:08:11 +0530868 if (!is_delete)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700869 {
870 int sockfd = socket (AF_INET6, SOCK_STREAM, 0);
871 if (sockfd < 0)
sharath reddy1b0c9832017-11-29 20:08:11 +0530872 clib_unix_warning ("get ifindex socket");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700873
874 if (ioctl (sockfd, SIOGIFINDEX, &ifr) < 0)
sharath reddy1b0c9832017-11-29 20:08:11 +0530875 clib_unix_warning ("get ifindex");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700876
877 ifr6.ifr6_ifindex = ifr.ifr_ifindex;
878 ifr6.ifr6_prefixlen = address_length;
Dave Barach178cf492018-11-13 16:34:13 -0500879 clib_memcpy_fast (&ifr6.ifr6_addr, address, 16);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700880
881 if (ioctl (sockfd, SIOCSIFADDR, &ifr6) < 0)
sharath reddy1b0c9832017-11-29 20:08:11 +0530882 clib_unix_warning ("set address");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700883
Dave Barach6f6f34f2016-08-08 13:05:31 -0400884 if (sockfd >= 0)
sharath reddy1b0c9832017-11-29 20:08:11 +0530885 close (sockfd);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700886 }
887 else
888 {
889 int sockfd = socket (AF_INET6, SOCK_STREAM, 0);
890 if (sockfd < 0)
sharath reddy1b0c9832017-11-29 20:08:11 +0530891 clib_unix_warning ("get ifindex socket");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700892
893 if (ioctl (sockfd, SIOGIFINDEX, &ifr) < 0)
sharath reddy1b0c9832017-11-29 20:08:11 +0530894 clib_unix_warning ("get ifindex");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700895
896 ifr6.ifr6_ifindex = ifr.ifr_ifindex;
897 ifr6.ifr6_prefixlen = address_length;
Dave Barach178cf492018-11-13 16:34:13 -0500898 clib_memcpy_fast (&ifr6.ifr6_addr, address, 16);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700899
900 if (ioctl (sockfd, SIOCDIFADDR, &ifr6) < 0)
sharath reddy1b0c9832017-11-29 20:08:11 +0530901 clib_unix_warning ("del address");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700902
Dave Barachf9c231e2016-08-05 10:10:18 -0400903 if (sockfd >= 0)
sharath reddy1b0c9832017-11-29 20:08:11 +0530904 close (sockfd);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700905
sharath reddy1b0c9832017-11-29 20:08:11 +0530906 mhash_unset (&tm->subif_mhash, &subif_addr, 0 /* old value ptr */ );
Ed Warnickecb9cada2015-12-08 15:45:58 -0700907 pool_put (tm->subifs, ap);
908 }
909}
910
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700911/**
912 * @brief TX the tun/tap frame
913 *
914 * @param *vm - vlib_main_t
915 * @param *node - vlib_node_runtime_t
916 * @param *frame - vlib_frame_t
917 *
918 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700919static void
920tuntap_punt_frame (vlib_main_t * vm,
sharath reddy1b0c9832017-11-29 20:08:11 +0530921 vlib_node_runtime_t * node, vlib_frame_t * frame)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700922{
923 tuntap_tx (vm, node, frame);
924 vlib_frame_free (vm, node, frame);
925}
926
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700927/**
928 * @brief Free the tun/tap frame
929 *
930 * @param *vm - vlib_main_t
931 * @param *node - vlib_node_runtime_t
932 * @param *frame - vlib_frame_t
933 *
934 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700935static void
936tuntap_nopunt_frame (vlib_main_t * vm,
sharath reddy1b0c9832017-11-29 20:08:11 +0530937 vlib_node_runtime_t * node, vlib_frame_t * frame)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700938{
Damjan Mariona3d59862018-11-10 10:23:00 +0100939 u32 *buffers = vlib_frame_vector_args (frame);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700940 uword n_packets = frame->n_vectors;
941 vlib_buffer_free (vm, buffers, n_packets);
942 vlib_frame_free (vm, node, frame);
943}
944
sharath reddy1b0c9832017-11-29 20:08:11 +0530945/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700946VNET_HW_INTERFACE_CLASS (tuntap_interface_class,static) = {
947 .name = "tuntap",
Neale Rannsb80c5362016-10-08 13:03:40 +0100948 .flags = VNET_HW_INTERFACE_CLASS_FLAG_P2P,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700949};
sharath reddy1b0c9832017-11-29 20:08:11 +0530950/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700951
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700952/**
953 * @brief Format tun/tap interface name
954 *
955 * @param *s - u8 - formatter string
956 * @param *args - va_list
957 *
958 * @return *s - u8 - formatted string
959 *
960 */
sharath reddy1b0c9832017-11-29 20:08:11 +0530961static u8 *
962format_tuntap_interface_name (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700963{
964 u32 i = va_arg (*args, u32);
965
966 s = format (s, "tuntap-%d", i);
967 return s;
968}
969
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700970/**
971 * @brief TX packet out tun/tap
972 *
973 * @param *vm - vlib_main_t
974 * @param *node - vlib_node_runtime_t
975 * @param *frame - vlib_frame_t
976 *
977 * @return n_buffers - uword - Packets transmitted
978 *
979 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700980static uword
981tuntap_intfc_tx (vlib_main_t * vm,
sharath reddy1b0c9832017-11-29 20:08:11 +0530982 vlib_node_runtime_t * node, vlib_frame_t * frame)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700983{
sharath reddy1b0c9832017-11-29 20:08:11 +0530984 tuntap_main_t *tm = &tuntap_main;
Damjan Mariona3d59862018-11-10 10:23:00 +0100985 u32 *buffers = vlib_frame_vector_args (frame);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700986 uword n_buffers = frame->n_vectors;
987
988 /* Normal interface transmit happens only on the normal interface... */
989 if (tm->have_normal_interface)
990 return tuntap_tx (vm, node, frame);
991
992 vlib_buffer_free (vm, buffers, n_buffers);
993 return n_buffers;
994}
995
sharath reddy1b0c9832017-11-29 20:08:11 +0530996/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700997VNET_DEVICE_CLASS (tuntap_dev_class,static) = {
998 .name = "tuntap",
999 .tx_function = tuntap_intfc_tx,
1000 .format_device_name = format_tuntap_interface_name,
1001};
sharath reddy1b0c9832017-11-29 20:08:11 +05301002/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001003
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -07001004/**
1005 * @brief tun/tap node init
1006 *
1007 * @param *vm - vlib_main_t
1008 *
1009 * @return error - clib_error_t
1010 *
1011 */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001012static clib_error_t *
1013tuntap_init (vlib_main_t * vm)
1014{
sharath reddy1b0c9832017-11-29 20:08:11 +05301015 ip4_main_t *im4 = &ip4_main;
1016 ip6_main_t *im6 = &ip6_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001017 ip4_add_del_interface_address_callback_t cb4;
1018 ip6_add_del_interface_address_callback_t cb6;
sharath reddy1b0c9832017-11-29 20:08:11 +05301019 tuntap_main_t *tm = &tuntap_main;
1020 vlib_thread_main_t *m = vlib_get_thread_main ();
Ed Warnickecb9cada2015-12-08 15:45:58 -07001021
sharath reddy1b0c9832017-11-29 20:08:11 +05301022 mhash_init (&tm->subif_mhash, sizeof (u32), sizeof (subif_address_t));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001023
1024 cb4.function = tuntap_ip4_add_del_interface_address;
1025 cb4.function_opaque = 0;
1026 vec_add1 (im4->add_del_interface_address_callbacks, cb4);
1027
1028 cb6.function = tuntap_ip6_add_del_interface_address;
1029 cb6.function_opaque = 0;
1030 vec_add1 (im6->add_del_interface_address_callbacks, cb6);
Steven4cd25762017-10-05 00:12:33 -07001031 vec_validate_aligned (tm->threads, m->n_vlib_mains - 1,
1032 CLIB_CACHE_LINE_BYTES);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001033
1034 return 0;
1035}
1036
Dave Barachf8d50682019-05-14 18:01:44 -04001037/* *INDENT-OFF* */
1038VLIB_INIT_FUNCTION (tuntap_init) =
1039{
1040 .runs_after = VLIB_INITS("ip4_init"),
1041};
1042/* *INDENT-ON* */
sharath reddy1b0c9832017-11-29 20:08:11 +05301043
1044/*
1045 * fd.io coding-style-patch-verification: ON
1046 *
1047 * Local Variables:
1048 * eval: (c-set-style "gnu")
1049 * End:
1050 */