blob: ac674653df4be906d53359890ff7532c5e065050 [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
2 *------------------------------------------------------------------
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>
48
49#include <vnet/ethernet/ethernet.h>
Damjan Marion8bdc63b2016-11-02 14:48:21 +010050#include <vnet/devices/devices.h>
Damjan Marion22311502016-10-28 20:30:15 +020051#include <vnet/feature/feature.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070052
Ed Warnickecb9cada2015-12-08 15:45:58 -070053static vnet_device_class_t tuntap_dev_class;
54static vnet_hw_interface_class_t tuntap_interface_class;
55
56static void tuntap_punt_frame (vlib_main_t * vm,
57 vlib_node_runtime_t * node,
58 vlib_frame_t * frame);
59static void tuntap_nopunt_frame (vlib_main_t * vm,
60 vlib_node_runtime_t * node,
61 vlib_frame_t * frame);
62
Ed Warnickecb9cada2015-12-08 15:45:58 -070063typedef struct {
64 u32 sw_if_index;
65 u8 is_v6;
66 u8 addr[16];
67} subif_address_t;
68
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -070069/**
70 * @brief TUNTAP node main state
71 */
Ed Warnickecb9cada2015-12-08 15:45:58 -070072typedef struct {
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -070073 /** Vector of iovecs for readv/writev calls. */
Ed Warnickecb9cada2015-12-08 15:45:58 -070074 struct iovec * iovecs;
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). */
78 u32 * rx_buffers;
79
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -070080 /** File descriptors for /dev/net/tun and provisioning socket. */
Ed Warnickecb9cada2015-12-08 15:45:58 -070081 int dev_net_tun_fd, dev_tap_fd;
82
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -070083 /** Create a "tap" [ethernet] encaps device */
Ed Warnickecb9cada2015-12-08 15:45:58 -070084 int is_ether;
85
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -070086 /** 1 if a "normal" routed intfc, 0 if a punt/inject interface */
Ed Warnickecb9cada2015-12-08 15:45:58 -070087
88 int have_normal_interface;
89
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -070090 /** tap device destination MAC address. Required, or Linux drops pkts */
Ed Warnickecb9cada2015-12-08 15:45:58 -070091 u8 ether_dst_mac[6];
92
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -070093 /** Interface MTU in bytes and # of default sized buffers. */
Ed Warnickecb9cada2015-12-08 15:45:58 -070094 u32 mtu_bytes, mtu_buffers;
95
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -070096 /** Linux interface name for tun device. */
Ed Warnickecb9cada2015-12-08 15:45:58 -070097 char * tun_name;
98
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -070099 /** Pool of subinterface addresses */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700100 subif_address_t *subifs;
101
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700102 /** Hash for subif addresses */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700103 mhash_t subif_mhash;
104
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700105 /** Unix file index */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700106 u32 unix_file_index;
107
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700108 /** For the "normal" interface, if configured */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700109 u32 hw_if_index, sw_if_index;
110
111} tuntap_main_t;
112
113static tuntap_main_t tuntap_main = {
114 .tun_name = "vnet",
115
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700116 /** Suitable defaults for an Ethernet-like tun/tap device */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700117 .mtu_bytes = 4096 + 256,
118};
119
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700120/**
121 * @brief tuntap_tx
122 * @node tuntap-tx
123 *
124 * Output node, writes the buffers comprising the incoming frame
Ed Warnickecb9cada2015-12-08 15:45:58 -0700125 * to the tun/tap device, aka hands them to the Linux kernel stack.
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700126 *
127 * @param *vm - vlib_main_t
128 * @param *node - vlib_node_runtime_t
129 * @param *frame - vlib_frame_t
130 *
131 * @return rc - uword
132 *
Ed Warnickecb9cada2015-12-08 15:45:58 -0700133 */
134static uword
135tuntap_tx (vlib_main_t * vm,
136 vlib_node_runtime_t * node,
137 vlib_frame_t * frame)
138{
139 u32 * buffers = vlib_frame_args (frame);
140 uword n_packets = frame->n_vectors;
141 tuntap_main_t * tm = &tuntap_main;
John Lo7394b5b2016-09-04 08:55:34 -0400142 vnet_main_t *vnm = vnet_get_main ();
143 vnet_interface_main_t *im = &vnm->interface_main;
144 u32 n_bytes = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700145 int i;
146
147 for (i = 0; i < n_packets; i++)
148 {
149 struct iovec * iov;
150 vlib_buffer_t * b;
151 uword l;
152
153 b = vlib_get_buffer (vm, buffers[i]);
154
155 if (tm->is_ether && (!tm->have_normal_interface))
156 {
157 vlib_buffer_reset(b);
Damjan Marionf1213b82016-03-13 02:22:06 +0100158 clib_memcpy (vlib_buffer_get_current (b), tm->ether_dst_mac, 6);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700159 }
160
161 /* Re-set iovecs if present. */
162 if (tm->iovecs)
163 _vec_len (tm->iovecs) = 0;
164
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700165 /** VLIB buffer chain -> Unix iovec(s). */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700166 vec_add2 (tm->iovecs, iov, 1);
167 iov->iov_base = b->data + b->current_data;
168 iov->iov_len = l = b->current_length;
169
170 if (PREDICT_FALSE (b->flags & VLIB_BUFFER_NEXT_PRESENT))
171 {
172 do {
173 b = vlib_get_buffer (vm, b->next_buffer);
174
175 vec_add2 (tm->iovecs, iov, 1);
176
177 iov->iov_base = b->data + b->current_data;
178 iov->iov_len = b->current_length;
179 l += b->current_length;
180 } while (b->flags & VLIB_BUFFER_NEXT_PRESENT);
181 }
182
183 if (writev (tm->dev_net_tun_fd, tm->iovecs, vec_len (tm->iovecs)) < l)
184 clib_unix_warning ("writev");
John Lo7394b5b2016-09-04 08:55:34 -0400185
186 n_bytes += l;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700187 }
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700188
John Lo7394b5b2016-09-04 08:55:34 -0400189 /* Update tuntap interface output stats. */
190 vlib_increment_combined_counter (im->combined_sw_if_counters
191 + VNET_INTERFACE_COUNTER_TX,
Damjan Marion586afd72017-04-05 19:18:20 +0200192 vm->thread_index,
John Lo7394b5b2016-09-04 08:55:34 -0400193 tm->sw_if_index, n_packets, n_bytes);
194
195
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700196 /** The normal interface path flattens the buffer chain */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700197 if (tm->have_normal_interface)
198 vlib_buffer_free_no_next (vm, buffers, n_packets);
199 else
200 vlib_buffer_free (vm, buffers, n_packets);
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700201
Ed Warnickecb9cada2015-12-08 15:45:58 -0700202 return n_packets;
203}
204
205VLIB_REGISTER_NODE (tuntap_tx_node,static) = {
206 .function = tuntap_tx,
207 .name = "tuntap-tx",
208 .type = VLIB_NODE_TYPE_INTERNAL,
209 .vector_size = 4,
210};
211
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700212/**
213 * @brief TUNTAP receive node
214 * @node tuntap-rx
215 *
216 * @param *vm - vlib_main_t
217 * @param *node - vlib_node_runtime_t
218 * @param *frame - vlib_frame_t
219 *
220 * @return rc - uword
221 *
222 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700223static uword
224tuntap_rx (vlib_main_t * vm,
225 vlib_node_runtime_t * node,
226 vlib_frame_t * frame)
227{
228 tuntap_main_t * tm = &tuntap_main;
229 vlib_buffer_t * b;
230 u32 bi;
Damjan Marion19010202016-03-24 17:17:47 +0100231 const uword buffer_size = VLIB_BUFFER_DATA_SIZE;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700232
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700233 /** Make sure we have some RX buffers. */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700234 {
235 uword n_left = vec_len (tm->rx_buffers);
236 uword n_alloc;
237
238 if (n_left < VLIB_FRAME_SIZE / 2)
239 {
240 if (! tm->rx_buffers)
241 vec_alloc (tm->rx_buffers, VLIB_FRAME_SIZE);
242
Damjan Marion67655492016-11-15 12:50:28 +0100243 n_alloc = vlib_buffer_alloc (vm, tm->rx_buffers + n_left, VLIB_FRAME_SIZE - n_left);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700244 _vec_len (tm->rx_buffers) = n_left + n_alloc;
245 }
246 }
247
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700248 /** Allocate RX buffers from end of rx_buffers.
Ed Warnickecb9cada2015-12-08 15:45:58 -0700249 Turn them into iovecs to pass to readv. */
250 {
251 uword i_rx = vec_len (tm->rx_buffers) - 1;
252 vlib_buffer_t * b;
253 word i, n_bytes_left, n_bytes_in_packet;
254
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700255 /** We should have enough buffers left for an MTU sized packet. */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700256 ASSERT (vec_len (tm->rx_buffers) >= tm->mtu_buffers);
257
258 vec_validate (tm->iovecs, tm->mtu_buffers - 1);
259 for (i = 0; i < tm->mtu_buffers; i++)
260 {
261 b = vlib_get_buffer (vm, tm->rx_buffers[i_rx - i]);
262 tm->iovecs[i].iov_base = b->data;
263 tm->iovecs[i].iov_len = buffer_size;
264 }
265
266 n_bytes_left = readv (tm->dev_net_tun_fd, tm->iovecs, tm->mtu_buffers);
267 n_bytes_in_packet = n_bytes_left;
268 if (n_bytes_left <= 0)
269 {
270 if (errno != EAGAIN)
271 clib_unix_warning ("readv %d", n_bytes_left);
272 return 0;
273 }
274
275 bi = tm->rx_buffers[i_rx];
276
277 while (1)
278 {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700279 b = vlib_get_buffer (vm, tm->rx_buffers[i_rx]);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700280 b->flags = 0;
281 b->current_data = 0;
282 b->current_length = n_bytes_left < buffer_size ? n_bytes_left : buffer_size;
283
284 n_bytes_left -= buffer_size;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700285
286 if (n_bytes_left <= 0)
287 {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700288 break;
289 }
290
291 i_rx--;
292 b->flags |= VLIB_BUFFER_NEXT_PRESENT;
293 b->next_buffer = tm->rx_buffers[i_rx];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700294 }
295
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700296 /** Interface counters for tuntap interface. */
297 vlib_increment_combined_counter
Ed Warnickecb9cada2015-12-08 15:45:58 -0700298 (vnet_main.interface_main.combined_sw_if_counters
299 + VNET_INTERFACE_COUNTER_RX,
Damjan Marion586afd72017-04-05 19:18:20 +0200300 vlib_get_thread_index(),
Ed Warnickecb9cada2015-12-08 15:45:58 -0700301 tm->sw_if_index,
302 1, n_bytes_in_packet);
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700303
Ed Warnickecb9cada2015-12-08 15:45:58 -0700304 _vec_len (tm->rx_buffers) = i_rx;
305 }
306
307 b = vlib_get_buffer (vm, bi);
308
309 {
310 u32 next_index;
311 uword n_trace = vlib_get_trace_count (vm, node);
312
313 vnet_buffer (b)->sw_if_index[VLIB_RX] = tm->sw_if_index;
314 vnet_buffer (b)->sw_if_index[VLIB_TX] = (u32)~0;
315
316 /*
317 * Turn this on if you run into
318 * "bad monkey" contexts, and you want to know exactly
319 * which nodes they've visited...
320 */
321 if (VLIB_BUFFER_TRACE_TRAJECTORY)
322 b->pre_data[0] = 0;
323
324 b->error = node->errors[0];
325
326 if (tm->is_ether)
327 {
Damjan Marion8bdc63b2016-11-02 14:48:21 +0100328 next_index = VNET_DEVICE_INPUT_NEXT_ETHERNET_INPUT;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700329 }
330 else
331 switch (b->data[0] & 0xf0)
332 {
333 case 0x40:
Damjan Marion8bdc63b2016-11-02 14:48:21 +0100334 next_index = VNET_DEVICE_INPUT_NEXT_IP4_INPUT;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700335 break;
336 case 0x60:
Damjan Marion8bdc63b2016-11-02 14:48:21 +0100337 next_index = VNET_DEVICE_INPUT_NEXT_IP6_INPUT;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700338 break;
339 default:
Damjan Marion8bdc63b2016-11-02 14:48:21 +0100340 next_index = VNET_DEVICE_INPUT_NEXT_DROP;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700341 break;
342 }
343
344 /* The linux kernel couldn't care less if our interface is up */
345 if (tm->have_normal_interface)
346 {
347 vnet_main_t *vnm = vnet_get_main();
348 vnet_sw_interface_t * si;
349 si = vnet_get_sw_interface (vnm, tm->sw_if_index);
350 if (!(si->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP))
Damjan Marion8bdc63b2016-11-02 14:48:21 +0100351 next_index = VNET_DEVICE_INPUT_NEXT_DROP;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700352 }
353
Damjan Marion35af9e52017-03-06 12:02:50 +0100354 vnet_feature_start_device_input_x1 (tm->sw_if_index, &next_index, b);
Damjan Marion22311502016-10-28 20:30:15 +0200355
Ed Warnickecb9cada2015-12-08 15:45:58 -0700356 vlib_set_next_frame_buffer (vm, node, next_index, bi);
357
358 if (n_trace > 0)
359 {
360 vlib_trace_buffer (vm, node, next_index,
361 b, /* follow_chain */ 1);
362 vlib_set_trace_count (vm, node, n_trace - 1);
363 }
364 }
365
366 return 1;
367}
368
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700369/**
370 * @brief TUNTAP_RX error strings
371 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700372static char * tuntap_rx_error_strings[] = {
373 "unknown packet type",
374};
375
376VLIB_REGISTER_NODE (tuntap_rx_node,static) = {
377 .function = tuntap_rx,
378 .name = "tuntap-rx",
Damjan Marion51327ac2016-11-09 11:59:42 +0100379 .sibling_of = "device-input",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700380 .type = VLIB_NODE_TYPE_INPUT,
381 .state = VLIB_NODE_STATE_INTERRUPT,
382 .vector_size = 4,
383 .n_errors = 1,
384 .error_strings = tuntap_rx_error_strings,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700385};
386
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700387/**
388 * @brief Gets called when file descriptor is ready from epoll.
389 *
390 * @param *uf - unix_file_t
391 *
392 * @return error - clib_error_t
393 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700394static clib_error_t * tuntap_read_ready (unix_file_t * uf)
395{
396 vlib_main_t * vm = vlib_get_main();
397 vlib_node_set_interrupt_pending (vm, tuntap_rx_node.index);
398 return 0;
399}
400
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700401/**
402 * @brief Clean up the tun/tap device
403 *
404 * @param *vm - vlib_main_t
405 *
406 * @return error - clib_error_t
407 *
Ed Warnickecb9cada2015-12-08 15:45:58 -0700408 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700409static clib_error_t *
410tuntap_exit (vlib_main_t * vm)
411{
412 tuntap_main_t *tm = &tuntap_main;
413 struct ifreq ifr;
414 int sfd;
415
416 /* Not present. */
417 if (! tm->dev_net_tun_fd || tm->dev_net_tun_fd < 0)
418 return 0;
419
420 sfd = socket (AF_INET, SOCK_STREAM, 0);
421 if (sfd < 0)
422 clib_unix_warning("provisioning socket");
423
424 memset(&ifr, 0, sizeof (ifr));
425 strncpy (ifr.ifr_name, tm->tun_name, sizeof (ifr.ifr_name)-1);
426
427 /* get flags, modify to bring down interface... */
428 if (ioctl (sfd, SIOCGIFFLAGS, &ifr) < 0)
429 clib_unix_warning ("SIOCGIFFLAGS");
430
431 ifr.ifr_flags &= ~(IFF_UP | IFF_RUNNING);
432
433 if (ioctl (sfd, SIOCSIFFLAGS, &ifr) < 0)
434 clib_unix_warning ("SIOCSIFFLAGS");
435
436 /* Turn off persistence */
437 if (ioctl (tm->dev_net_tun_fd, TUNSETPERSIST, 0) < 0)
438 clib_unix_warning ("TUNSETPERSIST");
439 close(tm->dev_tap_fd);
Dave Barachf9c231e2016-08-05 10:10:18 -0400440 if (tm->dev_net_tun_fd >= 0)
Dave Barach6f6f34f2016-08-08 13:05:31 -0400441 close(tm->dev_net_tun_fd);
442 if (sfd >= 0)
443 close (sfd);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700444
445 return 0;
446}
447
448VLIB_MAIN_LOOP_EXIT_FUNCTION (tuntap_exit);
449
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700450/**
451 * @brief CLI function for tun/tap config
452 *
453 * @param *vm - vlib_main_t
454 * @param *input - unformat_input_t
455 *
456 * @return error - clib_error_t
457 *
458 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700459static clib_error_t *
460tuntap_config (vlib_main_t * vm, unformat_input_t * input)
461{
462 tuntap_main_t *tm = &tuntap_main;
463 clib_error_t * error = 0;
464 struct ifreq ifr;
465 u8 * name;
466 int flags = IFF_TUN | IFF_NO_PI;
467 int is_enabled = 0, is_ether = 0, have_normal_interface = 0;
Damjan Marion19010202016-03-24 17:17:47 +0100468 const uword buffer_size = VLIB_BUFFER_DATA_SIZE;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700469
470 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
471 {
472 if (unformat (input, "mtu %d", &tm->mtu_bytes))
473 ;
474 else if (unformat (input, "enable"))
475 is_enabled = 1;
476 else if (unformat (input, "disable"))
477 is_enabled = 0;
478 else if (unformat (input, "ethernet") ||
479 unformat (input, "ether"))
480 is_ether = 1;
481 else if (unformat (input, "have-normal-interface") ||
482 unformat (input, "have-normal"))
483 have_normal_interface = 1;
484 else if (unformat (input, "name %s", &name))
485 tm->tun_name = (char *) name;
486 else
487 return clib_error_return (0, "unknown input `%U'",
488 format_unformat_error, input);
489 }
490
491 tm->dev_net_tun_fd = -1;
492 tm->dev_tap_fd = -1;
493
494 if (is_enabled == 0)
495 return 0;
496
497 if (geteuid())
498 {
499 clib_warning ("tuntap disabled: must be superuser");
500 return 0;
501 }
502
503 tm->is_ether = is_ether;
504 tm->have_normal_interface = have_normal_interface;
505
506 if (is_ether)
507 flags = IFF_TAP | IFF_NO_PI;
508
509 if ((tm->dev_net_tun_fd = open ("/dev/net/tun", O_RDWR)) < 0)
510 {
511 error = clib_error_return_unix (0, "open /dev/net/tun");
512 goto done;
513 }
514
515 memset (&ifr, 0, sizeof (ifr));
516 strncpy(ifr.ifr_name, tm->tun_name, sizeof(ifr.ifr_name)-1);
517 ifr.ifr_flags = flags;
518 if (ioctl (tm->dev_net_tun_fd, TUNSETIFF, (void *)&ifr) < 0)
519 {
520 error = clib_error_return_unix (0, "ioctl TUNSETIFF");
521 goto done;
522 }
523
524 /* Make it persistent, at least until we split. */
525 if (ioctl (tm->dev_net_tun_fd, TUNSETPERSIST, 1) < 0)
526 {
527 error = clib_error_return_unix (0, "TUNSETPERSIST");
528 goto done;
529 }
530
531 /* Open a provisioning socket */
532 if ((tm->dev_tap_fd = socket(PF_PACKET, SOCK_RAW,
533 htons(ETH_P_ALL))) < 0 )
534 {
535 error = clib_error_return_unix (0, "socket");
536 goto done;
537 }
538
539 /* Find the interface index. */
540 {
541 struct ifreq ifr;
542 struct sockaddr_ll sll;
543
544 memset (&ifr, 0, sizeof(ifr));
545 strncpy (ifr.ifr_name, tm->tun_name, sizeof(ifr.ifr_name)-1);
546 if (ioctl (tm->dev_tap_fd, SIOCGIFINDEX, &ifr) < 0 )
547 {
548 error = clib_error_return_unix (0, "ioctl SIOCGIFINDEX");
549 goto done;
550 }
551
552 /* Bind the provisioning socket to the interface. */
553 memset(&sll, 0, sizeof(sll));
554 sll.sll_family = AF_PACKET;
555 sll.sll_ifindex = ifr.ifr_ifindex;
556 sll.sll_protocol = htons(ETH_P_ALL);
557
558 if (bind(tm->dev_tap_fd, (struct sockaddr*) &sll, sizeof(sll)) < 0)
559 {
560 error = clib_error_return_unix (0, "bind");
561 goto done;
562 }
563 }
564
565 /* non-blocking I/O on /dev/tapX */
566 {
567 int one = 1;
568 if (ioctl (tm->dev_net_tun_fd, FIONBIO, &one) < 0)
569 {
570 error = clib_error_return_unix (0, "ioctl FIONBIO");
571 goto done;
572 }
573 }
574
575 tm->mtu_buffers = (tm->mtu_bytes + (buffer_size - 1)) / buffer_size;
576
577 ifr.ifr_mtu = tm->mtu_bytes;
578 if (ioctl (tm->dev_tap_fd, SIOCSIFMTU, &ifr) < 0)
579 {
580 error = clib_error_return_unix (0, "ioctl SIOCSIFMTU");
581 goto done;
582 }
583
584 /* get flags, modify to bring up interface... */
585 if (ioctl (tm->dev_tap_fd, SIOCGIFFLAGS, &ifr) < 0)
586 {
587 error = clib_error_return_unix (0, "ioctl SIOCGIFFLAGS");
588 goto done;
589 }
590
591 ifr.ifr_flags |= (IFF_UP | IFF_RUNNING);
592
593 if (ioctl (tm->dev_tap_fd, SIOCSIFFLAGS, &ifr) < 0)
594 {
595 error = clib_error_return_unix (0, "ioctl SIOCSIFFLAGS");
596 goto done;
597 }
598
599 if (is_ether)
600 {
601 if (ioctl (tm->dev_tap_fd, SIOCGIFHWADDR, &ifr) < 0)
602 {
603 error = clib_error_return_unix (0, "ioctl SIOCGIFHWADDR");
604 goto done;
605 }
606 else
Damjan Marionf1213b82016-03-13 02:22:06 +0100607 clib_memcpy (tm->ether_dst_mac, ifr.ifr_hwaddr.sa_data, 6);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700608 }
609
610 if (have_normal_interface)
611 {
612 vnet_main_t *vnm = vnet_get_main();
613 error = ethernet_register_interface
614 (vnm,
615 tuntap_dev_class.index,
616 0 /* device instance */,
617 tm->ether_dst_mac /* ethernet address */,
618 &tm->hw_if_index,
619 0 /* flag change */);
620 if (error)
621 clib_error_report (error);
622 tm->sw_if_index = tm->hw_if_index;
623 vm->os_punt_frame = tuntap_nopunt_frame;
624 }
625 else
626 {
627 vnet_main_t *vnm = vnet_get_main();
628 vnet_hw_interface_t * hi;
629
630 vm->os_punt_frame = tuntap_punt_frame;
631
632 tm->hw_if_index = vnet_register_interface
633 (vnm,
634 tuntap_dev_class.index, 0 /* device instance */,
635 tuntap_interface_class.index, 0);
636 hi = vnet_get_hw_interface (vnm, tm->hw_if_index);
637 tm->sw_if_index = hi->sw_if_index;
638
639 /* Interface is always up. */
640 vnet_hw_interface_set_flags (vnm, tm->hw_if_index,
641 VNET_HW_INTERFACE_FLAG_LINK_UP);
642 vnet_sw_interface_set_flags (vnm, tm->sw_if_index,
643 VNET_SW_INTERFACE_FLAG_ADMIN_UP);
644 }
645
646 {
647 unix_file_t template = {0};
648 template.read_function = tuntap_read_ready;
649 template.file_descriptor = tm->dev_net_tun_fd;
650 tm->unix_file_index = unix_file_add (&unix_main, &template);
651 }
652
653 done:
654 if (error)
655 {
656 if (tm->dev_net_tun_fd >= 0)
657 close (tm->dev_net_tun_fd);
658 if (tm->dev_tap_fd >= 0)
659 close (tm->dev_tap_fd);
660 }
661
662 return error;
663}
664
665VLIB_CONFIG_FUNCTION (tuntap_config, "tuntap");
666
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700667/**
668 * @brief Add or Del IP4 address to tun/tap interface
669 *
670 * @param *im - ip4_main_t
671 * @param opaque - uword
672 * @param sw_if_index - u32
673 * @param *address - ip4_address_t
674 * @param is_delete - u32
675 *
676 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700677void
678tuntap_ip4_add_del_interface_address (ip4_main_t * im,
679 uword opaque,
680 u32 sw_if_index,
681 ip4_address_t * address,
682 u32 address_length,
683 u32 if_address_index,
684 u32 is_delete)
685{
686 tuntap_main_t * tm = &tuntap_main;
687 struct ifreq ifr;
688 subif_address_t subif_addr, * ap;
689 uword * p;
690
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700691 /** Tuntap disabled, or using a "normal" interface. */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700692 if (tm->have_normal_interface || tm->dev_tap_fd < 0)
693 return;
694
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700695 /** See if we already know about this subif */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700696 memset (&subif_addr, 0, sizeof (subif_addr));
697 subif_addr.sw_if_index = sw_if_index;
Damjan Marionf1213b82016-03-13 02:22:06 +0100698 clib_memcpy (&subif_addr.addr, address, sizeof (*address));
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700699
Ed Warnickecb9cada2015-12-08 15:45:58 -0700700 p = mhash_get (&tm->subif_mhash, &subif_addr);
701
702 if (p)
703 ap = pool_elt_at_index (tm->subifs, p[0]);
704 else
705 {
706 pool_get (tm->subifs, ap);
707 *ap = subif_addr;
708 mhash_set (&tm->subif_mhash, ap, ap - tm->subifs, 0);
709 }
710
711 /* Use subif pool index to select alias device. */
712 memset (&ifr, 0, sizeof (ifr));
713 snprintf (ifr.ifr_name, sizeof(ifr.ifr_name),
714 "%s:%d", tm->tun_name, (int)(ap - tm->subifs));
715
Igor Mikhailov (imichail)80e88162016-11-04 20:25:00 -0700716 /* the tuntap punt/inject is enabled for IPv4 RX so long as
717 * any vpp interface has an IPv4 address.
718 * this is also ref counted.
719 */
720 ip4_sw_interface_enable_disable (tm->sw_if_index, !is_delete);
721
Ed Warnickecb9cada2015-12-08 15:45:58 -0700722 if (! is_delete)
723 {
724 struct sockaddr_in * sin;
725
726 sin = (struct sockaddr_in *)&ifr.ifr_addr;
727
728 /* Set ipv4 address, netmask. */
729 sin->sin_family = AF_INET;
Damjan Marionf1213b82016-03-13 02:22:06 +0100730 clib_memcpy (&sin->sin_addr.s_addr, address, 4);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700731 if (ioctl (tm->dev_tap_fd, SIOCSIFADDR, &ifr) < 0)
732 clib_unix_warning ("ioctl SIOCSIFADDR");
733
734 sin->sin_addr.s_addr = im->fib_masks[address_length];
735 if (ioctl (tm->dev_tap_fd, SIOCSIFNETMASK, &ifr) < 0)
736 clib_unix_warning ("ioctl SIOCSIFNETMASK");
737 }
738 else
739 {
740 mhash_unset (&tm->subif_mhash, &subif_addr, 0 /* old value ptr */);
741 pool_put (tm->subifs, ap);
742 }
743
744 /* get flags, modify to bring up interface... */
745 if (ioctl (tm->dev_tap_fd, SIOCGIFFLAGS, &ifr) < 0)
746 clib_unix_warning ("ioctl SIOCGIFFLAGS");
747
748 if (is_delete)
749 ifr.ifr_flags &= ~(IFF_UP | IFF_RUNNING);
750 else
751 ifr.ifr_flags |= (IFF_UP | IFF_RUNNING);
752
753 if (ioctl (tm->dev_tap_fd, SIOCSIFFLAGS, &ifr) < 0)
754 clib_unix_warning ("ioctl SIOCSIFFLAGS");
755}
756
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700757/**
Chris Luke16bcf7d2016-09-01 14:31:46 -0400758 * @brief workaround for a known include file bug.
759 * including @c <linux/ipv6.h> causes multiple definitions if
760 * @c <netinet/in.h is also included.
Ed Warnickecb9cada2015-12-08 15:45:58 -0700761 */
762struct in6_ifreq {
763 struct in6_addr ifr6_addr;
764 u32 ifr6_prefixlen;
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700765 int ifr6_ifindex;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700766};
767
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700768/**
Chris Luke16bcf7d2016-09-01 14:31:46 -0400769 * @brief Add or Del tun/tap interface address.
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700770 *
Ed Warnickecb9cada2015-12-08 15:45:58 -0700771 * Both the v6 interface address API and the way ifconfig
772 * displays subinterfaces differ from their v4 couterparts.
773 * The code given here seems to work but YMMV.
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700774 *
775 * @param *im - ip6_main_t
776 * @param opaque - uword
777 * @param sw_if_index - u32
778 * @param *address - ip6_address_t
779 * @param address_length - u32
780 * @param if_address_index - u32
781 * @param is_delete - u32
Ed Warnickecb9cada2015-12-08 15:45:58 -0700782 */
783void
784tuntap_ip6_add_del_interface_address (ip6_main_t * im,
785 uword opaque,
786 u32 sw_if_index,
787 ip6_address_t * address,
788 u32 address_length,
789 u32 if_address_index,
790 u32 is_delete)
791{
792 tuntap_main_t * tm = &tuntap_main;
793 struct ifreq ifr;
794 struct in6_ifreq ifr6;
795 subif_address_t subif_addr, * ap;
796 uword * p;
797
798 /* Tuntap disabled, or using a "normal" interface. */
799 if (tm->have_normal_interface || tm->dev_tap_fd < 0)
800 return;
801
802 /* See if we already know about this subif */
803 memset (&subif_addr, 0, sizeof (subif_addr));
804 subif_addr.sw_if_index = sw_if_index;
805 subif_addr.is_v6 = 1;
Damjan Marionf1213b82016-03-13 02:22:06 +0100806 clib_memcpy (&subif_addr.addr, address, sizeof (*address));
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700807
Ed Warnickecb9cada2015-12-08 15:45:58 -0700808 p = mhash_get (&tm->subif_mhash, &subif_addr);
809
810 if (p)
811 ap = pool_elt_at_index (tm->subifs, p[0]);
812 else
813 {
814 pool_get (tm->subifs, ap);
815 *ap = subif_addr;
816 mhash_set (&tm->subif_mhash, ap, ap - tm->subifs, 0);
817 }
818
819 /* Use subif pool index to select alias device. */
820 memset (&ifr, 0, sizeof (ifr));
821 memset (&ifr6, 0, sizeof (ifr6));
822 snprintf (ifr.ifr_name, sizeof(ifr.ifr_name),
823 "%s:%d", tm->tun_name, (int)(ap - tm->subifs));
824
Igor Mikhailov (imichail)80e88162016-11-04 20:25:00 -0700825 /* the tuntap punt/inject is enabled for IPv6 RX so long as
826 * any vpp interface has an IPv6 address.
827 * this is also ref counted.
828 */
829 ip6_sw_interface_enable_disable (tm->sw_if_index, !is_delete);
830
Ed Warnickecb9cada2015-12-08 15:45:58 -0700831 if (! is_delete)
832 {
833 int sockfd = socket (AF_INET6, SOCK_STREAM, 0);
834 if (sockfd < 0)
835 clib_unix_warning ("get ifindex socket");
836
837 if (ioctl (sockfd, SIOGIFINDEX, &ifr) < 0)
838 clib_unix_warning ("get ifindex");
839
840 ifr6.ifr6_ifindex = ifr.ifr_ifindex;
841 ifr6.ifr6_prefixlen = address_length;
Damjan Marionf1213b82016-03-13 02:22:06 +0100842 clib_memcpy (&ifr6.ifr6_addr, address, 16);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700843
844 if (ioctl (sockfd, SIOCSIFADDR, &ifr6) < 0)
845 clib_unix_warning ("set address");
846
Dave Barach6f6f34f2016-08-08 13:05:31 -0400847 if (sockfd >= 0)
848 close (sockfd);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700849 }
850 else
851 {
852 int sockfd = socket (AF_INET6, SOCK_STREAM, 0);
853 if (sockfd < 0)
854 clib_unix_warning ("get ifindex socket");
855
856 if (ioctl (sockfd, SIOGIFINDEX, &ifr) < 0)
857 clib_unix_warning ("get ifindex");
858
859 ifr6.ifr6_ifindex = ifr.ifr_ifindex;
860 ifr6.ifr6_prefixlen = address_length;
Damjan Marionf1213b82016-03-13 02:22:06 +0100861 clib_memcpy (&ifr6.ifr6_addr, address, 16);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700862
863 if (ioctl (sockfd, SIOCDIFADDR, &ifr6) < 0)
864 clib_unix_warning ("del address");
865
Dave Barachf9c231e2016-08-05 10:10:18 -0400866 if (sockfd >= 0)
867 close (sockfd);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700868
869 mhash_unset (&tm->subif_mhash, &subif_addr, 0 /* old value ptr */);
870 pool_put (tm->subifs, ap);
871 }
872}
873
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700874/**
875 * @brief TX the tun/tap frame
876 *
877 * @param *vm - vlib_main_t
878 * @param *node - vlib_node_runtime_t
879 * @param *frame - vlib_frame_t
880 *
881 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700882static void
883tuntap_punt_frame (vlib_main_t * vm,
884 vlib_node_runtime_t * node,
885 vlib_frame_t * frame)
886{
887 tuntap_tx (vm, node, frame);
888 vlib_frame_free (vm, node, frame);
889}
890
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700891/**
892 * @brief Free the tun/tap frame
893 *
894 * @param *vm - vlib_main_t
895 * @param *node - vlib_node_runtime_t
896 * @param *frame - vlib_frame_t
897 *
898 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700899static void
900tuntap_nopunt_frame (vlib_main_t * vm,
901 vlib_node_runtime_t * node,
902 vlib_frame_t * frame)
903{
904 u32 * buffers = vlib_frame_args (frame);
905 uword n_packets = frame->n_vectors;
906 vlib_buffer_free (vm, buffers, n_packets);
907 vlib_frame_free (vm, node, frame);
908}
909
910VNET_HW_INTERFACE_CLASS (tuntap_interface_class,static) = {
911 .name = "tuntap",
Neale Rannsb80c5362016-10-08 13:03:40 +0100912 .flags = VNET_HW_INTERFACE_CLASS_FLAG_P2P,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700913};
914
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700915/**
916 * @brief Format tun/tap interface name
917 *
918 * @param *s - u8 - formatter string
919 * @param *args - va_list
920 *
921 * @return *s - u8 - formatted string
922 *
923 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700924static u8 * format_tuntap_interface_name (u8 * s, va_list * args)
925{
926 u32 i = va_arg (*args, u32);
927
928 s = format (s, "tuntap-%d", i);
929 return s;
930}
931
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700932/**
933 * @brief TX packet out tun/tap
934 *
935 * @param *vm - vlib_main_t
936 * @param *node - vlib_node_runtime_t
937 * @param *frame - vlib_frame_t
938 *
939 * @return n_buffers - uword - Packets transmitted
940 *
941 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700942static uword
943tuntap_intfc_tx (vlib_main_t * vm,
944 vlib_node_runtime_t * node,
945 vlib_frame_t * frame)
946{
947 tuntap_main_t * tm = &tuntap_main;
948 u32 * buffers = vlib_frame_args (frame);
949 uword n_buffers = frame->n_vectors;
950
951 /* Normal interface transmit happens only on the normal interface... */
952 if (tm->have_normal_interface)
953 return tuntap_tx (vm, node, frame);
954
955 vlib_buffer_free (vm, buffers, n_buffers);
956 return n_buffers;
957}
958
959VNET_DEVICE_CLASS (tuntap_dev_class,static) = {
960 .name = "tuntap",
961 .tx_function = tuntap_intfc_tx,
962 .format_device_name = format_tuntap_interface_name,
963};
964
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700965/**
966 * @brief tun/tap node init
967 *
968 * @param *vm - vlib_main_t
969 *
970 * @return error - clib_error_t
971 *
972 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700973static clib_error_t *
974tuntap_init (vlib_main_t * vm)
975{
976 clib_error_t * error;
977 ip4_main_t * im4 = &ip4_main;
978 ip6_main_t * im6 = &ip6_main;
979 ip4_add_del_interface_address_callback_t cb4;
980 ip6_add_del_interface_address_callback_t cb6;
981 tuntap_main_t * tm = &tuntap_main;
982
983 error = vlib_call_init_function (vm, ip4_init);
984 if (error)
985 return error;
986
987 mhash_init (&tm->subif_mhash, sizeof (u32), sizeof(subif_address_t));
988
989 cb4.function = tuntap_ip4_add_del_interface_address;
990 cb4.function_opaque = 0;
991 vec_add1 (im4->add_del_interface_address_callbacks, cb4);
992
993 cb6.function = tuntap_ip6_add_del_interface_address;
994 cb6.function_opaque = 0;
995 vec_add1 (im6->add_del_interface_address_callbacks, cb6);
996
997 return 0;
998}
999
1000VLIB_INIT_FUNCTION (tuntap_init);