blob: 4a9bd46c31c37bbd6fac4414f2197cf98cd86662 [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{
sharath reddy1b0c9832017-11-29 20:08:11 +0530149 u32 *buffers = vlib_frame_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);
169 clib_memcpy (vlib_buffer_get_current (b), tm->ether_dst_mac, 6);
170 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700171
172 /* Re-set iovecs if present. */
Steven4cd25762017-10-05 00:12:33 -0700173 if (tm->threads[thread_index].iovecs)
174 _vec_len (tm->threads[thread_index].iovecs) = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700175
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700176 /** VLIB buffer chain -> Unix iovec(s). */
Steven4cd25762017-10-05 00:12:33 -0700177 vec_add2 (tm->threads[thread_index].iovecs, iov, 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700178 iov->iov_base = b->data + b->current_data;
179 iov->iov_len = l = b->current_length;
180
181 if (PREDICT_FALSE (b->flags & VLIB_BUFFER_NEXT_PRESENT))
182 {
sharath reddy1b0c9832017-11-29 20:08:11 +0530183 do
184 {
185 b = vlib_get_buffer (vm, b->next_buffer);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700186
sharath reddy1b0c9832017-11-29 20:08:11 +0530187 vec_add2 (tm->threads[thread_index].iovecs, iov, 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700188
sharath reddy1b0c9832017-11-29 20:08:11 +0530189 iov->iov_base = b->data + b->current_data;
190 iov->iov_len = b->current_length;
191 l += b->current_length;
192 }
193 while (b->flags & VLIB_BUFFER_NEXT_PRESENT);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700194 }
195
Steven4cd25762017-10-05 00:12:33 -0700196 if (writev (tm->dev_net_tun_fd, tm->threads[thread_index].iovecs,
197 vec_len (tm->threads[thread_index].iovecs)) < l)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700198 clib_unix_warning ("writev");
John Lo7394b5b2016-09-04 08:55:34 -0400199
200 n_bytes += l;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700201 }
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700202
John Lo7394b5b2016-09-04 08:55:34 -0400203 /* Update tuntap interface output stats. */
204 vlib_increment_combined_counter (im->combined_sw_if_counters
205 + VNET_INTERFACE_COUNTER_TX,
Damjan Marion586afd72017-04-05 19:18:20 +0200206 vm->thread_index,
John Lo7394b5b2016-09-04 08:55:34 -0400207 tm->sw_if_index, n_packets, n_bytes);
208
209
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700210 /** The normal interface path flattens the buffer chain */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700211 if (tm->have_normal_interface)
212 vlib_buffer_free_no_next (vm, buffers, n_packets);
213 else
214 vlib_buffer_free (vm, buffers, n_packets);
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700215
Ed Warnickecb9cada2015-12-08 15:45:58 -0700216 return n_packets;
217}
218
sharath reddy1b0c9832017-11-29 20:08:11 +0530219/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700220VLIB_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};
sharath reddy1b0c9832017-11-29 20:08:11 +0530226/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700227
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700228/**
229 * @brief TUNTAP receive node
230 * @node tuntap-rx
231 *
232 * @param *vm - vlib_main_t
233 * @param *node - vlib_node_runtime_t
234 * @param *frame - vlib_frame_t
235 *
236 * @return rc - uword
237 *
238 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700239static uword
sharath reddy1b0c9832017-11-29 20:08:11 +0530240tuntap_rx (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700241{
sharath reddy1b0c9832017-11-29 20:08:11 +0530242 tuntap_main_t *tm = &tuntap_main;
243 vlib_buffer_t *b;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700244 u32 bi;
Damjan Marion19010202016-03-24 17:17:47 +0100245 const uword buffer_size = VLIB_BUFFER_DATA_SIZE;
Damjan Marion067cd622018-07-11 12:47:43 +0200246 u16 thread_index = vm->thread_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700247
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700248 /** Make sure we have some RX buffers. */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700249 {
Steven4cd25762017-10-05 00:12:33 -0700250 uword n_left = vec_len (tm->threads[thread_index].rx_buffers);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700251 uword n_alloc;
252
253 if (n_left < VLIB_FRAME_SIZE / 2)
254 {
sharath reddy1b0c9832017-11-29 20:08:11 +0530255 if (!tm->threads[thread_index].rx_buffers)
Steven4cd25762017-10-05 00:12:33 -0700256 vec_alloc (tm->threads[thread_index].rx_buffers, VLIB_FRAME_SIZE);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700257
sharath reddy1b0c9832017-11-29 20:08:11 +0530258 n_alloc =
259 vlib_buffer_alloc (vm,
260 tm->threads[thread_index].rx_buffers + n_left,
261 VLIB_FRAME_SIZE - n_left);
Steven4cd25762017-10-05 00:12:33 -0700262 _vec_len (tm->threads[thread_index].rx_buffers) = n_left + n_alloc;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700263 }
264 }
265
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700266 /** Allocate RX buffers from end of rx_buffers.
Ed Warnickecb9cada2015-12-08 15:45:58 -0700267 Turn them into iovecs to pass to readv. */
268 {
Steven4cd25762017-10-05 00:12:33 -0700269 uword i_rx = vec_len (tm->threads[thread_index].rx_buffers) - 1;
sharath reddy1b0c9832017-11-29 20:08:11 +0530270 vlib_buffer_t *b;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700271 word i, n_bytes_left, n_bytes_in_packet;
272
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700273 /** We should have enough buffers left for an MTU sized packet. */
sharath reddy1b0c9832017-11-29 20:08:11 +0530274 ASSERT (vec_len (tm->threads[thread_index].rx_buffers) >=
275 tm->mtu_buffers);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700276
Steven4cd25762017-10-05 00:12:33 -0700277 vec_validate (tm->threads[thread_index].iovecs, tm->mtu_buffers - 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700278 for (i = 0; i < tm->mtu_buffers; i++)
279 {
sharath reddy1b0c9832017-11-29 20:08:11 +0530280 b =
281 vlib_get_buffer (vm,
282 tm->threads[thread_index].rx_buffers[i_rx - i]);
Steven4cd25762017-10-05 00:12:33 -0700283 tm->threads[thread_index].iovecs[i].iov_base = b->data;
284 tm->threads[thread_index].iovecs[i].iov_len = buffer_size;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700285 }
286
sharath reddy1b0c9832017-11-29 20:08:11 +0530287 n_bytes_left =
288 readv (tm->dev_net_tun_fd, tm->threads[thread_index].iovecs,
289 tm->mtu_buffers);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700290 n_bytes_in_packet = n_bytes_left;
291 if (n_bytes_left <= 0)
292 {
sharath reddy1b0c9832017-11-29 20:08:11 +0530293 if (errno != EAGAIN)
294 clib_unix_warning ("readv %d", n_bytes_left);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700295 return 0;
296 }
297
Steven4cd25762017-10-05 00:12:33 -0700298 bi = tm->threads[thread_index].rx_buffers[i_rx];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700299
300 while (1)
301 {
Steven4cd25762017-10-05 00:12:33 -0700302 b = vlib_get_buffer (vm, tm->threads[thread_index].rx_buffers[i_rx]);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700303 b->flags = 0;
304 b->current_data = 0;
sharath reddy1b0c9832017-11-29 20:08:11 +0530305 b->current_length =
306 n_bytes_left < buffer_size ? n_bytes_left : buffer_size;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700307
308 n_bytes_left -= buffer_size;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700309
310 if (n_bytes_left <= 0)
sharath reddy1b0c9832017-11-29 20:08:11 +0530311 {
312 break;
313 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700314
315 i_rx--;
316 b->flags |= VLIB_BUFFER_NEXT_PRESENT;
Steven4cd25762017-10-05 00:12:33 -0700317 b->next_buffer = tm->threads[thread_index].rx_buffers[i_rx];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700318 }
319
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700320 /** Interface counters for tuntap interface. */
321 vlib_increment_combined_counter
sharath reddy1b0c9832017-11-29 20:08:11 +0530322 (vnet_main.interface_main.combined_sw_if_counters
323 + VNET_INTERFACE_COUNTER_RX,
324 thread_index, tm->sw_if_index, 1, n_bytes_in_packet);
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700325
Steven4cd25762017-10-05 00:12:33 -0700326 _vec_len (tm->threads[thread_index].rx_buffers) = i_rx;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700327 }
328
329 b = vlib_get_buffer (vm, bi);
330
331 {
332 u32 next_index;
333 uword n_trace = vlib_get_trace_count (vm, node);
334
335 vnet_buffer (b)->sw_if_index[VLIB_RX] = tm->sw_if_index;
sharath reddy1b0c9832017-11-29 20:08:11 +0530336 vnet_buffer (b)->sw_if_index[VLIB_TX] = (u32) ~ 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700337
338 /*
339 * Turn this on if you run into
340 * "bad monkey" contexts, and you want to know exactly
341 * which nodes they've visited...
342 */
343 if (VLIB_BUFFER_TRACE_TRAJECTORY)
sharath reddy1b0c9832017-11-29 20:08:11 +0530344 b->pre_data[0] = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700345
346 b->error = node->errors[0];
347
348 if (tm->is_ether)
349 {
Damjan Marion8bdc63b2016-11-02 14:48:21 +0100350 next_index = VNET_DEVICE_INPUT_NEXT_ETHERNET_INPUT;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700351 }
352 else
353 switch (b->data[0] & 0xf0)
sharath reddy1b0c9832017-11-29 20:08:11 +0530354 {
355 case 0x40:
356 next_index = VNET_DEVICE_INPUT_NEXT_IP4_INPUT;
357 break;
358 case 0x60:
359 next_index = VNET_DEVICE_INPUT_NEXT_IP6_INPUT;
360 break;
361 default:
362 next_index = VNET_DEVICE_INPUT_NEXT_DROP;
363 break;
364 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700365
366 /* The linux kernel couldn't care less if our interface is up */
367 if (tm->have_normal_interface)
368 {
sharath reddy1b0c9832017-11-29 20:08:11 +0530369 vnet_main_t *vnm = vnet_get_main ();
370 vnet_sw_interface_t *si;
371 si = vnet_get_sw_interface (vnm, tm->sw_if_index);
372 if (!(si->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP))
373 next_index = VNET_DEVICE_INPUT_NEXT_DROP;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700374 }
375
Damjan Marion35af9e52017-03-06 12:02:50 +0100376 vnet_feature_start_device_input_x1 (tm->sw_if_index, &next_index, b);
Damjan Marion22311502016-10-28 20:30:15 +0200377
Ed Warnickecb9cada2015-12-08 15:45:58 -0700378 vlib_set_next_frame_buffer (vm, node, next_index, bi);
379
380 if (n_trace > 0)
381 {
sharath reddy1b0c9832017-11-29 20:08:11 +0530382 vlib_trace_buffer (vm, node, next_index, b, /* follow_chain */ 1);
383 vlib_set_trace_count (vm, node, n_trace - 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700384 }
385 }
386
387 return 1;
388}
389
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700390/**
391 * @brief TUNTAP_RX error strings
392 */
sharath reddy1b0c9832017-11-29 20:08:11 +0530393static char *tuntap_rx_error_strings[] = {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700394 "unknown packet type",
395};
396
sharath reddy1b0c9832017-11-29 20:08:11 +0530397/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700398VLIB_REGISTER_NODE (tuntap_rx_node,static) = {
399 .function = tuntap_rx,
400 .name = "tuntap-rx",
Damjan Marion51327ac2016-11-09 11:59:42 +0100401 .sibling_of = "device-input",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700402 .type = VLIB_NODE_TYPE_INPUT,
403 .state = VLIB_NODE_STATE_INTERRUPT,
404 .vector_size = 4,
405 .n_errors = 1,
406 .error_strings = tuntap_rx_error_strings,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700407};
sharath reddy1b0c9832017-11-29 20:08:11 +0530408/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700409
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700410/**
411 * @brief Gets called when file descriptor is ready from epoll.
412 *
Damjan Marion56dd5432017-09-08 19:52:02 +0200413 * @param *uf - clib_file_t
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700414 *
415 * @return error - clib_error_t
416 */
sharath reddy1b0c9832017-11-29 20:08:11 +0530417static clib_error_t *
418tuntap_read_ready (clib_file_t * uf)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700419{
sharath reddy1b0c9832017-11-29 20:08:11 +0530420 vlib_main_t *vm = vlib_get_main ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700421 vlib_node_set_interrupt_pending (vm, tuntap_rx_node.index);
422 return 0;
423}
424
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700425/**
426 * @brief Clean up the tun/tap device
427 *
428 * @param *vm - vlib_main_t
429 *
430 * @return error - clib_error_t
431 *
Ed Warnickecb9cada2015-12-08 15:45:58 -0700432 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700433static clib_error_t *
434tuntap_exit (vlib_main_t * vm)
435{
436 tuntap_main_t *tm = &tuntap_main;
437 struct ifreq ifr;
438 int sfd;
439
440 /* Not present. */
sharath reddy1b0c9832017-11-29 20:08:11 +0530441 if (!tm->dev_net_tun_fd || tm->dev_net_tun_fd < 0)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700442 return 0;
443
444 sfd = socket (AF_INET, SOCK_STREAM, 0);
445 if (sfd < 0)
sharath reddy1b0c9832017-11-29 20:08:11 +0530446 clib_unix_warning ("provisioning socket");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700447
sharath reddy1b0c9832017-11-29 20:08:11 +0530448 memset (&ifr, 0, sizeof (ifr));
449 strncpy (ifr.ifr_name, tm->tun_name, sizeof (ifr.ifr_name) - 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700450
451 /* get flags, modify to bring down interface... */
452 if (ioctl (sfd, SIOCGIFFLAGS, &ifr) < 0)
453 clib_unix_warning ("SIOCGIFFLAGS");
454
455 ifr.ifr_flags &= ~(IFF_UP | IFF_RUNNING);
456
457 if (ioctl (sfd, SIOCSIFFLAGS, &ifr) < 0)
458 clib_unix_warning ("SIOCSIFFLAGS");
459
460 /* Turn off persistence */
461 if (ioctl (tm->dev_net_tun_fd, TUNSETPERSIST, 0) < 0)
462 clib_unix_warning ("TUNSETPERSIST");
sharath reddy1b0c9832017-11-29 20:08:11 +0530463 close (tm->dev_tap_fd);
Dave Barachf9c231e2016-08-05 10:10:18 -0400464 if (tm->dev_net_tun_fd >= 0)
sharath reddy1b0c9832017-11-29 20:08:11 +0530465 close (tm->dev_net_tun_fd);
Dave Barach6f6f34f2016-08-08 13:05:31 -0400466 if (sfd >= 0)
467 close (sfd);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700468
469 return 0;
470}
471
472VLIB_MAIN_LOOP_EXIT_FUNCTION (tuntap_exit);
473
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700474/**
475 * @brief CLI function for tun/tap config
476 *
477 * @param *vm - vlib_main_t
478 * @param *input - unformat_input_t
479 *
480 * @return error - clib_error_t
481 *
482 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700483static clib_error_t *
484tuntap_config (vlib_main_t * vm, unformat_input_t * input)
485{
486 tuntap_main_t *tm = &tuntap_main;
sharath reddy1b0c9832017-11-29 20:08:11 +0530487 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700488 struct ifreq ifr;
sharath reddy1b0c9832017-11-29 20:08:11 +0530489 u8 *name;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700490 int flags = IFF_TUN | IFF_NO_PI;
491 int is_enabled = 0, is_ether = 0, have_normal_interface = 0;
Damjan Marion19010202016-03-24 17:17:47 +0100492 const uword buffer_size = VLIB_BUFFER_DATA_SIZE;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700493
494 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
495 {
496 if (unformat (input, "mtu %d", &tm->mtu_bytes))
497 ;
498 else if (unformat (input, "enable"))
sharath reddy1b0c9832017-11-29 20:08:11 +0530499 is_enabled = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700500 else if (unformat (input, "disable"))
sharath reddy1b0c9832017-11-29 20:08:11 +0530501 is_enabled = 0;
502 else if (unformat (input, "ethernet") || unformat (input, "ether"))
503 is_ether = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700504 else if (unformat (input, "have-normal-interface") ||
sharath reddy1b0c9832017-11-29 20:08:11 +0530505 unformat (input, "have-normal"))
506 have_normal_interface = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700507 else if (unformat (input, "name %s", &name))
508 tm->tun_name = (char *) name;
509 else
510 return clib_error_return (0, "unknown input `%U'",
511 format_unformat_error, input);
512 }
513
514 tm->dev_net_tun_fd = -1;
515 tm->dev_tap_fd = -1;
516
517 if (is_enabled == 0)
518 return 0;
519
sharath reddy1b0c9832017-11-29 20:08:11 +0530520 if (geteuid ())
Ed Warnickecb9cada2015-12-08 15:45:58 -0700521 {
522 clib_warning ("tuntap disabled: must be superuser");
523 return 0;
sharath reddy1b0c9832017-11-29 20:08:11 +0530524 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700525
526 tm->is_ether = is_ether;
527 tm->have_normal_interface = have_normal_interface;
528
529 if (is_ether)
530 flags = IFF_TAP | IFF_NO_PI;
531
532 if ((tm->dev_net_tun_fd = open ("/dev/net/tun", O_RDWR)) < 0)
533 {
534 error = clib_error_return_unix (0, "open /dev/net/tun");
535 goto done;
536 }
537
538 memset (&ifr, 0, sizeof (ifr));
sharath reddy1b0c9832017-11-29 20:08:11 +0530539 strncpy (ifr.ifr_name, tm->tun_name, sizeof (ifr.ifr_name) - 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700540 ifr.ifr_flags = flags;
sharath reddy1b0c9832017-11-29 20:08:11 +0530541 if (ioctl (tm->dev_net_tun_fd, TUNSETIFF, (void *) &ifr) < 0)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700542 {
543 error = clib_error_return_unix (0, "ioctl TUNSETIFF");
544 goto done;
545 }
sharath reddy1b0c9832017-11-29 20:08:11 +0530546
Ed Warnickecb9cada2015-12-08 15:45:58 -0700547 /* Make it persistent, at least until we split. */
548 if (ioctl (tm->dev_net_tun_fd, TUNSETPERSIST, 1) < 0)
549 {
550 error = clib_error_return_unix (0, "TUNSETPERSIST");
551 goto done;
552 }
553
554 /* Open a provisioning socket */
sharath reddy1b0c9832017-11-29 20:08:11 +0530555 if ((tm->dev_tap_fd = socket (PF_PACKET, SOCK_RAW, htons (ETH_P_ALL))) < 0)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700556 {
557 error = clib_error_return_unix (0, "socket");
558 goto done;
559 }
560
561 /* Find the interface index. */
562 {
563 struct ifreq ifr;
564 struct sockaddr_ll sll;
565
sharath reddy1b0c9832017-11-29 20:08:11 +0530566 memset (&ifr, 0, sizeof (ifr));
567 strncpy (ifr.ifr_name, tm->tun_name, sizeof (ifr.ifr_name) - 1);
568 if (ioctl (tm->dev_tap_fd, SIOCGIFINDEX, &ifr) < 0)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700569 {
570 error = clib_error_return_unix (0, "ioctl SIOCGIFINDEX");
571 goto done;
572 }
573
574 /* Bind the provisioning socket to the interface. */
sharath reddy1b0c9832017-11-29 20:08:11 +0530575 memset (&sll, 0, sizeof (sll));
576 sll.sll_family = AF_PACKET;
577 sll.sll_ifindex = ifr.ifr_ifindex;
578 sll.sll_protocol = htons (ETH_P_ALL);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700579
sharath reddy1b0c9832017-11-29 20:08:11 +0530580 if (bind (tm->dev_tap_fd, (struct sockaddr *) &sll, sizeof (sll)) < 0)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700581 {
582 error = clib_error_return_unix (0, "bind");
583 goto done;
584 }
585 }
586
587 /* non-blocking I/O on /dev/tapX */
588 {
589 int one = 1;
590 if (ioctl (tm->dev_net_tun_fd, FIONBIO, &one) < 0)
591 {
592 error = clib_error_return_unix (0, "ioctl FIONBIO");
593 goto done;
594 }
595 }
596
597 tm->mtu_buffers = (tm->mtu_bytes + (buffer_size - 1)) / buffer_size;
598
599 ifr.ifr_mtu = tm->mtu_bytes;
600 if (ioctl (tm->dev_tap_fd, SIOCSIFMTU, &ifr) < 0)
601 {
602 error = clib_error_return_unix (0, "ioctl SIOCSIFMTU");
603 goto done;
604 }
605
606 /* get flags, modify to bring up interface... */
607 if (ioctl (tm->dev_tap_fd, SIOCGIFFLAGS, &ifr) < 0)
608 {
609 error = clib_error_return_unix (0, "ioctl SIOCGIFFLAGS");
610 goto done;
611 }
612
613 ifr.ifr_flags |= (IFF_UP | IFF_RUNNING);
614
615 if (ioctl (tm->dev_tap_fd, SIOCSIFFLAGS, &ifr) < 0)
616 {
617 error = clib_error_return_unix (0, "ioctl SIOCSIFFLAGS");
618 goto done;
619 }
620
621 if (is_ether)
622 {
623 if (ioctl (tm->dev_tap_fd, SIOCGIFHWADDR, &ifr) < 0)
sharath reddy1b0c9832017-11-29 20:08:11 +0530624 {
625 error = clib_error_return_unix (0, "ioctl SIOCGIFHWADDR");
626 goto done;
627 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700628 else
sharath reddy1b0c9832017-11-29 20:08:11 +0530629 clib_memcpy (tm->ether_dst_mac, ifr.ifr_hwaddr.sa_data, 6);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700630 }
631
632 if (have_normal_interface)
633 {
sharath reddy1b0c9832017-11-29 20:08:11 +0530634 vnet_main_t *vnm = vnet_get_main ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700635 error = ethernet_register_interface
sharath reddy1b0c9832017-11-29 20:08:11 +0530636 (vnm, tuntap_dev_class.index, 0 /* device instance */ ,
637 tm->ether_dst_mac /* ethernet address */ ,
638 &tm->hw_if_index, 0 /* flag change */ );
Ed Warnickecb9cada2015-12-08 15:45:58 -0700639 if (error)
sharath reddy1b0c9832017-11-29 20:08:11 +0530640 clib_error_report (error);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700641 tm->sw_if_index = tm->hw_if_index;
642 vm->os_punt_frame = tuntap_nopunt_frame;
643 }
644 else
645 {
sharath reddy1b0c9832017-11-29 20:08:11 +0530646 vnet_main_t *vnm = vnet_get_main ();
647 vnet_hw_interface_t *hi;
648
Ed Warnickecb9cada2015-12-08 15:45:58 -0700649 vm->os_punt_frame = tuntap_punt_frame;
sharath reddy1b0c9832017-11-29 20:08:11 +0530650
Ed Warnickecb9cada2015-12-08 15:45:58 -0700651 tm->hw_if_index = vnet_register_interface
sharath reddy1b0c9832017-11-29 20:08:11 +0530652 (vnm, tuntap_dev_class.index, 0 /* device instance */ ,
653 tuntap_interface_class.index, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700654 hi = vnet_get_hw_interface (vnm, tm->hw_if_index);
655 tm->sw_if_index = hi->sw_if_index;
sharath reddy1b0c9832017-11-29 20:08:11 +0530656
Ed Warnickecb9cada2015-12-08 15:45:58 -0700657 /* Interface is always up. */
sharath reddy1b0c9832017-11-29 20:08:11 +0530658 vnet_hw_interface_set_flags (vnm, tm->hw_if_index,
659 VNET_HW_INTERFACE_FLAG_LINK_UP);
660 vnet_sw_interface_set_flags (vnm, tm->sw_if_index,
661 VNET_SW_INTERFACE_FLAG_ADMIN_UP);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700662 }
663
664 {
sharath reddy1b0c9832017-11-29 20:08:11 +0530665 clib_file_t template = { 0 };
Ed Warnickecb9cada2015-12-08 15:45:58 -0700666 template.read_function = tuntap_read_ready;
667 template.file_descriptor = tm->dev_net_tun_fd;
Damjan Marion56dd5432017-09-08 19:52:02 +0200668 tm->clib_file_index = clib_file_add (&file_main, &template);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700669 }
670
sharath reddy1b0c9832017-11-29 20:08:11 +0530671done:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700672 if (error)
673 {
674 if (tm->dev_net_tun_fd >= 0)
675 close (tm->dev_net_tun_fd);
676 if (tm->dev_tap_fd >= 0)
677 close (tm->dev_tap_fd);
678 }
679
680 return error;
681}
682
683VLIB_CONFIG_FUNCTION (tuntap_config, "tuntap");
684
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700685/**
686 * @brief Add or Del IP4 address to tun/tap interface
687 *
688 * @param *im - ip4_main_t
689 * @param opaque - uword
690 * @param sw_if_index - u32
691 * @param *address - ip4_address_t
692 * @param is_delete - u32
693 *
694 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700695void
696tuntap_ip4_add_del_interface_address (ip4_main_t * im,
697 uword opaque,
698 u32 sw_if_index,
699 ip4_address_t * address,
700 u32 address_length,
sharath reddy1b0c9832017-11-29 20:08:11 +0530701 u32 if_address_index, u32 is_delete)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700702{
sharath reddy1b0c9832017-11-29 20:08:11 +0530703 tuntap_main_t *tm = &tuntap_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700704 struct ifreq ifr;
sharath reddy1b0c9832017-11-29 20:08:11 +0530705 subif_address_t subif_addr, *ap;
706 uword *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700707
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700708 /** Tuntap disabled, or using a "normal" interface. */
sharath reddy1b0c9832017-11-29 20:08:11 +0530709 if (tm->have_normal_interface || tm->dev_tap_fd < 0)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700710 return;
711
Neale Rannse8bad972017-08-10 11:34:12 -0700712 /* if the address is being applied to an interface that is not in
713 * the same table/VRF as this tap, then ignore it.
Paul Vinciguerrabdc0e6b2018-09-22 05:32:50 -0700714 * If we don't do this overlapping address spaces in the different tables
Neale Rannse8bad972017-08-10 11:34:12 -0700715 * breaks the linux host's routing tables */
sharath reddy1b0c9832017-11-29 20:08:11 +0530716 if (fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
717 sw_if_index) !=
718 fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4, tm->sw_if_index))
719 return;
Neale Rannse8bad972017-08-10 11:34:12 -0700720
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700721 /** See if we already know about this subif */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700722 memset (&subif_addr, 0, sizeof (subif_addr));
723 subif_addr.sw_if_index = sw_if_index;
Damjan Marionf1213b82016-03-13 02:22:06 +0100724 clib_memcpy (&subif_addr.addr, address, sizeof (*address));
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700725
Ed Warnickecb9cada2015-12-08 15:45:58 -0700726 p = mhash_get (&tm->subif_mhash, &subif_addr);
727
728 if (p)
729 ap = pool_elt_at_index (tm->subifs, p[0]);
730 else
731 {
732 pool_get (tm->subifs, ap);
733 *ap = subif_addr;
734 mhash_set (&tm->subif_mhash, ap, ap - tm->subifs, 0);
735 }
736
737 /* Use subif pool index to select alias device. */
738 memset (&ifr, 0, sizeof (ifr));
sharath reddy1b0c9832017-11-29 20:08:11 +0530739 snprintf (ifr.ifr_name, sizeof (ifr.ifr_name),
740 "%s:%d", tm->tun_name, (int) (ap - tm->subifs));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700741
Igor Mikhailov (imichail)80e88162016-11-04 20:25:00 -0700742 /* the tuntap punt/inject is enabled for IPv4 RX so long as
743 * any vpp interface has an IPv4 address.
744 * this is also ref counted.
745 */
746 ip4_sw_interface_enable_disable (tm->sw_if_index, !is_delete);
747
sharath reddy1b0c9832017-11-29 20:08:11 +0530748 if (!is_delete)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700749 {
sharath reddy1b0c9832017-11-29 20:08:11 +0530750 struct sockaddr_in *sin;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700751
sharath reddy1b0c9832017-11-29 20:08:11 +0530752 sin = (struct sockaddr_in *) &ifr.ifr_addr;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700753
754 /* Set ipv4 address, netmask. */
755 sin->sin_family = AF_INET;
Damjan Marionf1213b82016-03-13 02:22:06 +0100756 clib_memcpy (&sin->sin_addr.s_addr, address, 4);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700757 if (ioctl (tm->dev_tap_fd, SIOCSIFADDR, &ifr) < 0)
758 clib_unix_warning ("ioctl SIOCSIFADDR");
sharath reddy1b0c9832017-11-29 20:08:11 +0530759
Ed Warnickecb9cada2015-12-08 15:45:58 -0700760 sin->sin_addr.s_addr = im->fib_masks[address_length];
761 if (ioctl (tm->dev_tap_fd, SIOCSIFNETMASK, &ifr) < 0)
762 clib_unix_warning ("ioctl SIOCSIFNETMASK");
763 }
764 else
765 {
sharath reddy1b0c9832017-11-29 20:08:11 +0530766 mhash_unset (&tm->subif_mhash, &subif_addr, 0 /* old value ptr */ );
Ed Warnickecb9cada2015-12-08 15:45:58 -0700767 pool_put (tm->subifs, ap);
768 }
769
770 /* get flags, modify to bring up interface... */
771 if (ioctl (tm->dev_tap_fd, SIOCGIFFLAGS, &ifr) < 0)
772 clib_unix_warning ("ioctl SIOCGIFFLAGS");
773
774 if (is_delete)
775 ifr.ifr_flags &= ~(IFF_UP | IFF_RUNNING);
776 else
777 ifr.ifr_flags |= (IFF_UP | IFF_RUNNING);
778
779 if (ioctl (tm->dev_tap_fd, SIOCSIFFLAGS, &ifr) < 0)
780 clib_unix_warning ("ioctl SIOCSIFFLAGS");
781}
782
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700783/**
Chris Luke16bcf7d2016-09-01 14:31:46 -0400784 * @brief workaround for a known include file bug.
785 * including @c <linux/ipv6.h> causes multiple definitions if
786 * @c <netinet/in.h is also included.
Ed Warnickecb9cada2015-12-08 15:45:58 -0700787 */
sharath reddy1b0c9832017-11-29 20:08:11 +0530788struct in6_ifreq
789{
790 struct in6_addr ifr6_addr;
791 u32 ifr6_prefixlen;
792 int ifr6_ifindex;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700793};
794
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700795/**
Chris Luke16bcf7d2016-09-01 14:31:46 -0400796 * @brief Add or Del tun/tap interface address.
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700797 *
Ed Warnickecb9cada2015-12-08 15:45:58 -0700798 * Both the v6 interface address API and the way ifconfig
Paul Vinciguerrabdc0e6b2018-09-22 05:32:50 -0700799 * displays subinterfaces differ from their v4 counterparts.
Ed Warnickecb9cada2015-12-08 15:45:58 -0700800 * The code given here seems to work but YMMV.
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700801 *
802 * @param *im - ip6_main_t
803 * @param opaque - uword
804 * @param sw_if_index - u32
805 * @param *address - ip6_address_t
806 * @param address_length - u32
807 * @param if_address_index - u32
808 * @param is_delete - u32
Ed Warnickecb9cada2015-12-08 15:45:58 -0700809 */
810void
811tuntap_ip6_add_del_interface_address (ip6_main_t * im,
812 uword opaque,
813 u32 sw_if_index,
814 ip6_address_t * address,
815 u32 address_length,
sharath reddy1b0c9832017-11-29 20:08:11 +0530816 u32 if_address_index, u32 is_delete)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700817{
sharath reddy1b0c9832017-11-29 20:08:11 +0530818 tuntap_main_t *tm = &tuntap_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700819 struct ifreq ifr;
820 struct in6_ifreq ifr6;
sharath reddy1b0c9832017-11-29 20:08:11 +0530821 subif_address_t subif_addr, *ap;
822 uword *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700823
824 /* Tuntap disabled, or using a "normal" interface. */
sharath reddy1b0c9832017-11-29 20:08:11 +0530825 if (tm->have_normal_interface || tm->dev_tap_fd < 0)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700826 return;
827
Neale Rannse8bad972017-08-10 11:34:12 -0700828 /* if the address is being applied to an interface that is not in
829 * the same table/VRF as this tap, then ignore it.
Paul Vinciguerrabdc0e6b2018-09-22 05:32:50 -0700830 * If we don't do this overlapping address spaces in the different tables
Neale Rannse8bad972017-08-10 11:34:12 -0700831 * breaks the linux host's routing tables */
sharath reddy1b0c9832017-11-29 20:08:11 +0530832 if (fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP6,
833 sw_if_index) !=
834 fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP6, tm->sw_if_index))
835 return;
Neale Rannse8bad972017-08-10 11:34:12 -0700836
Ed Warnickecb9cada2015-12-08 15:45:58 -0700837 /* See if we already know about this subif */
838 memset (&subif_addr, 0, sizeof (subif_addr));
839 subif_addr.sw_if_index = sw_if_index;
840 subif_addr.is_v6 = 1;
Damjan Marionf1213b82016-03-13 02:22:06 +0100841 clib_memcpy (&subif_addr.addr, address, sizeof (*address));
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700842
Ed Warnickecb9cada2015-12-08 15:45:58 -0700843 p = mhash_get (&tm->subif_mhash, &subif_addr);
844
845 if (p)
846 ap = pool_elt_at_index (tm->subifs, p[0]);
847 else
848 {
849 pool_get (tm->subifs, ap);
850 *ap = subif_addr;
851 mhash_set (&tm->subif_mhash, ap, ap - tm->subifs, 0);
852 }
853
854 /* Use subif pool index to select alias device. */
855 memset (&ifr, 0, sizeof (ifr));
856 memset (&ifr6, 0, sizeof (ifr6));
sharath reddy1b0c9832017-11-29 20:08:11 +0530857 snprintf (ifr.ifr_name, sizeof (ifr.ifr_name),
858 "%s:%d", tm->tun_name, (int) (ap - tm->subifs));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700859
Igor Mikhailov (imichail)80e88162016-11-04 20:25:00 -0700860 /* the tuntap punt/inject is enabled for IPv6 RX so long as
861 * any vpp interface has an IPv6 address.
862 * this is also ref counted.
863 */
864 ip6_sw_interface_enable_disable (tm->sw_if_index, !is_delete);
865
sharath reddy1b0c9832017-11-29 20:08:11 +0530866 if (!is_delete)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700867 {
868 int sockfd = socket (AF_INET6, SOCK_STREAM, 0);
869 if (sockfd < 0)
sharath reddy1b0c9832017-11-29 20:08:11 +0530870 clib_unix_warning ("get ifindex socket");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700871
872 if (ioctl (sockfd, SIOGIFINDEX, &ifr) < 0)
sharath reddy1b0c9832017-11-29 20:08:11 +0530873 clib_unix_warning ("get ifindex");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700874
875 ifr6.ifr6_ifindex = ifr.ifr_ifindex;
876 ifr6.ifr6_prefixlen = address_length;
Damjan Marionf1213b82016-03-13 02:22:06 +0100877 clib_memcpy (&ifr6.ifr6_addr, address, 16);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700878
879 if (ioctl (sockfd, SIOCSIFADDR, &ifr6) < 0)
sharath reddy1b0c9832017-11-29 20:08:11 +0530880 clib_unix_warning ("set address");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700881
Dave Barach6f6f34f2016-08-08 13:05:31 -0400882 if (sockfd >= 0)
sharath reddy1b0c9832017-11-29 20:08:11 +0530883 close (sockfd);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700884 }
885 else
886 {
887 int sockfd = socket (AF_INET6, SOCK_STREAM, 0);
888 if (sockfd < 0)
sharath reddy1b0c9832017-11-29 20:08:11 +0530889 clib_unix_warning ("get ifindex socket");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700890
891 if (ioctl (sockfd, SIOGIFINDEX, &ifr) < 0)
sharath reddy1b0c9832017-11-29 20:08:11 +0530892 clib_unix_warning ("get ifindex");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700893
894 ifr6.ifr6_ifindex = ifr.ifr_ifindex;
895 ifr6.ifr6_prefixlen = address_length;
Damjan Marionf1213b82016-03-13 02:22:06 +0100896 clib_memcpy (&ifr6.ifr6_addr, address, 16);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700897
898 if (ioctl (sockfd, SIOCDIFADDR, &ifr6) < 0)
sharath reddy1b0c9832017-11-29 20:08:11 +0530899 clib_unix_warning ("del address");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700900
Dave Barachf9c231e2016-08-05 10:10:18 -0400901 if (sockfd >= 0)
sharath reddy1b0c9832017-11-29 20:08:11 +0530902 close (sockfd);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700903
sharath reddy1b0c9832017-11-29 20:08:11 +0530904 mhash_unset (&tm->subif_mhash, &subif_addr, 0 /* old value ptr */ );
Ed Warnickecb9cada2015-12-08 15:45:58 -0700905 pool_put (tm->subifs, ap);
906 }
907}
908
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700909/**
910 * @brief TX the tun/tap frame
911 *
912 * @param *vm - vlib_main_t
913 * @param *node - vlib_node_runtime_t
914 * @param *frame - vlib_frame_t
915 *
916 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700917static void
918tuntap_punt_frame (vlib_main_t * vm,
sharath reddy1b0c9832017-11-29 20:08:11 +0530919 vlib_node_runtime_t * node, vlib_frame_t * frame)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700920{
921 tuntap_tx (vm, node, frame);
922 vlib_frame_free (vm, node, frame);
923}
924
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700925/**
926 * @brief Free the tun/tap frame
927 *
928 * @param *vm - vlib_main_t
929 * @param *node - vlib_node_runtime_t
930 * @param *frame - vlib_frame_t
931 *
932 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700933static void
934tuntap_nopunt_frame (vlib_main_t * vm,
sharath reddy1b0c9832017-11-29 20:08:11 +0530935 vlib_node_runtime_t * node, vlib_frame_t * frame)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700936{
sharath reddy1b0c9832017-11-29 20:08:11 +0530937 u32 *buffers = vlib_frame_args (frame);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700938 uword n_packets = frame->n_vectors;
939 vlib_buffer_free (vm, buffers, n_packets);
940 vlib_frame_free (vm, node, frame);
941}
942
sharath reddy1b0c9832017-11-29 20:08:11 +0530943/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700944VNET_HW_INTERFACE_CLASS (tuntap_interface_class,static) = {
945 .name = "tuntap",
Neale Rannsb80c5362016-10-08 13:03:40 +0100946 .flags = VNET_HW_INTERFACE_CLASS_FLAG_P2P,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700947};
sharath reddy1b0c9832017-11-29 20:08:11 +0530948/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700949
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700950/**
951 * @brief Format tun/tap interface name
952 *
953 * @param *s - u8 - formatter string
954 * @param *args - va_list
955 *
956 * @return *s - u8 - formatted string
957 *
958 */
sharath reddy1b0c9832017-11-29 20:08:11 +0530959static u8 *
960format_tuntap_interface_name (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700961{
962 u32 i = va_arg (*args, u32);
963
964 s = format (s, "tuntap-%d", i);
965 return s;
966}
967
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700968/**
969 * @brief TX packet out tun/tap
970 *
971 * @param *vm - vlib_main_t
972 * @param *node - vlib_node_runtime_t
973 * @param *frame - vlib_frame_t
974 *
975 * @return n_buffers - uword - Packets transmitted
976 *
977 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700978static uword
979tuntap_intfc_tx (vlib_main_t * vm,
sharath reddy1b0c9832017-11-29 20:08:11 +0530980 vlib_node_runtime_t * node, vlib_frame_t * frame)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700981{
sharath reddy1b0c9832017-11-29 20:08:11 +0530982 tuntap_main_t *tm = &tuntap_main;
983 u32 *buffers = vlib_frame_args (frame);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700984 uword n_buffers = frame->n_vectors;
985
986 /* Normal interface transmit happens only on the normal interface... */
987 if (tm->have_normal_interface)
988 return tuntap_tx (vm, node, frame);
989
990 vlib_buffer_free (vm, buffers, n_buffers);
991 return n_buffers;
992}
993
sharath reddy1b0c9832017-11-29 20:08:11 +0530994/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700995VNET_DEVICE_CLASS (tuntap_dev_class,static) = {
996 .name = "tuntap",
997 .tx_function = tuntap_intfc_tx,
998 .format_device_name = format_tuntap_interface_name,
999};
sharath reddy1b0c9832017-11-29 20:08:11 +05301000/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001001
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -07001002/**
1003 * @brief tun/tap node init
1004 *
1005 * @param *vm - vlib_main_t
1006 *
1007 * @return error - clib_error_t
1008 *
1009 */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001010static clib_error_t *
1011tuntap_init (vlib_main_t * vm)
1012{
sharath reddy1b0c9832017-11-29 20:08:11 +05301013 clib_error_t *error;
1014 ip4_main_t *im4 = &ip4_main;
1015 ip6_main_t *im6 = &ip6_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001016 ip4_add_del_interface_address_callback_t cb4;
1017 ip6_add_del_interface_address_callback_t cb6;
sharath reddy1b0c9832017-11-29 20:08:11 +05301018 tuntap_main_t *tm = &tuntap_main;
1019 vlib_thread_main_t *m = vlib_get_thread_main ();
Ed Warnickecb9cada2015-12-08 15:45:58 -07001020
1021 error = vlib_call_init_function (vm, ip4_init);
1022 if (error)
1023 return error;
1024
sharath reddy1b0c9832017-11-29 20:08:11 +05301025 mhash_init (&tm->subif_mhash, sizeof (u32), sizeof (subif_address_t));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001026
1027 cb4.function = tuntap_ip4_add_del_interface_address;
1028 cb4.function_opaque = 0;
1029 vec_add1 (im4->add_del_interface_address_callbacks, cb4);
1030
1031 cb6.function = tuntap_ip6_add_del_interface_address;
1032 cb6.function_opaque = 0;
1033 vec_add1 (im6->add_del_interface_address_callbacks, cb6);
Steven4cd25762017-10-05 00:12:33 -07001034 vec_validate_aligned (tm->threads, m->n_vlib_mains - 1,
1035 CLIB_CACHE_LINE_BYTES);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001036
1037 return 0;
1038}
1039
1040VLIB_INIT_FUNCTION (tuntap_init);
sharath reddy1b0c9832017-11-29 20:08:11 +05301041
1042/*
1043 * fd.io coding-style-patch-verification: ON
1044 *
1045 * Local Variables:
1046 * eval: (c-set-style "gnu")
1047 * End:
1048 */