sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 1 | /* |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 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) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 19 | /** |
| 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 32 | |
| 33 | #include <fcntl.h> /* for open */ |
| 34 | #include <sys/ioctl.h> |
| 35 | #include <sys/socket.h> |
| 36 | #include <sys/stat.h> |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 37 | #include <sys/types.h> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 38 | #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 Ranns | e8bad97 | 2017-08-10 11:34:12 -0700 | [diff] [blame] | 48 | #include <vnet/fib/fib_table.h> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 49 | |
| 50 | #include <vnet/ethernet/ethernet.h> |
Damjan Marion | 8bdc63b | 2016-11-02 14:48:21 +0100 | [diff] [blame] | 51 | #include <vnet/devices/devices.h> |
Damjan Marion | 2231150 | 2016-10-28 20:30:15 +0200 | [diff] [blame] | 52 | #include <vnet/feature/feature.h> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 53 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 54 | static vnet_device_class_t tuntap_dev_class; |
| 55 | static vnet_hw_interface_class_t tuntap_interface_class; |
| 56 | |
| 57 | static void tuntap_punt_frame (vlib_main_t * vm, |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 58 | vlib_node_runtime_t * node, |
| 59 | vlib_frame_t * frame); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 60 | static void tuntap_nopunt_frame (vlib_main_t * vm, |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 61 | vlib_node_runtime_t * node, |
| 62 | vlib_frame_t * frame); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 63 | |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 64 | typedef struct |
| 65 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 66 | u32 sw_if_index; |
| 67 | u8 is_v6; |
| 68 | u8 addr[16]; |
| 69 | } subif_address_t; |
| 70 | |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 71 | /** |
Steven | 4cd2576 | 2017-10-05 00:12:33 -0700 | [diff] [blame] | 72 | * @brief TUNTAP per thread struct |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 73 | */ |
Steven | 4cd2576 | 2017-10-05 00:12:33 -0700 | [diff] [blame] | 74 | typedef struct |
| 75 | { |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 76 | /** Vector of VLIB rx buffers to use. We allocate them in blocks |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 77 | of VLIB_FRAME_SIZE (256). */ |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 78 | u32 *rx_buffers; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 79 | |
Steven | 4cd2576 | 2017-10-05 00:12:33 -0700 | [diff] [blame] | 80 | /** Vector of iovecs for readv/writev calls. */ |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 81 | struct iovec *iovecs; |
Steven | 4cd2576 | 2017-10-05 00:12:33 -0700 | [diff] [blame] | 82 | } tuntap_per_thread_t; |
| 83 | |
| 84 | /** |
| 85 | * @brief TUNTAP node main state |
| 86 | */ |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 87 | typedef struct |
| 88 | { |
Steven | 4cd2576 | 2017-10-05 00:12:33 -0700 | [diff] [blame] | 89 | /** per thread variables */ |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 90 | tuntap_per_thread_t *threads; |
Steven | 4cd2576 | 2017-10-05 00:12:33 -0700 | [diff] [blame] | 91 | |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 92 | /** File descriptors for /dev/net/tun and provisioning socket. */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 93 | int dev_net_tun_fd, dev_tap_fd; |
| 94 | |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 95 | /** Create a "tap" [ethernet] encaps device */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 96 | int is_ether; |
| 97 | |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 98 | /** 1 if a "normal" routed intfc, 0 if a punt/inject interface */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 99 | |
| 100 | int have_normal_interface; |
| 101 | |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 102 | /** tap device destination MAC address. Required, or Linux drops pkts */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 103 | u8 ether_dst_mac[6]; |
| 104 | |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 105 | /** Interface MTU in bytes and # of default sized buffers. */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 106 | u32 mtu_bytes, mtu_buffers; |
| 107 | |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 108 | /** Linux interface name for tun device. */ |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 109 | char *tun_name; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 110 | |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 111 | /** Pool of subinterface addresses */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 112 | subif_address_t *subifs; |
| 113 | |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 114 | /** Hash for subif addresses */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 115 | mhash_t subif_mhash; |
| 116 | |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 117 | /** Unix file index */ |
Damjan Marion | 56dd543 | 2017-09-08 19:52:02 +0200 | [diff] [blame] | 118 | u32 clib_file_index; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 119 | |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 120 | /** For the "normal" interface, if configured */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 121 | u32 hw_if_index, sw_if_index; |
| 122 | |
| 123 | } tuntap_main_t; |
| 124 | |
| 125 | static tuntap_main_t tuntap_main = { |
| 126 | .tun_name = "vnet", |
| 127 | |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 128 | /** Suitable defaults for an Ethernet-like tun/tap device */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 129 | .mtu_bytes = 4096 + 256, |
| 130 | }; |
| 131 | |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 132 | /** |
| 133 | * @brief tuntap_tx |
| 134 | * @node tuntap-tx |
| 135 | * |
| 136 | * Output node, writes the buffers comprising the incoming frame |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 137 | * to the tun/tap device, aka hands them to the Linux kernel stack. |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 138 | * |
| 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 145 | */ |
| 146 | static uword |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 147 | tuntap_tx (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 148 | { |
Damjan Marion | a3d5986 | 2018-11-10 10:23:00 +0100 | [diff] [blame] | 149 | u32 *buffers = vlib_frame_vector_args (frame); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 150 | uword n_packets = frame->n_vectors; |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 151 | tuntap_main_t *tm = &tuntap_main; |
John Lo | 7394b5b | 2016-09-04 08:55:34 -0400 | [diff] [blame] | 152 | vnet_main_t *vnm = vnet_get_main (); |
| 153 | vnet_interface_main_t *im = &vnm->interface_main; |
| 154 | u32 n_bytes = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 155 | int i; |
Damjan Marion | 067cd62 | 2018-07-11 12:47:43 +0200 | [diff] [blame] | 156 | u16 thread_index = vm->thread_index; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 157 | |
| 158 | for (i = 0; i < n_packets; i++) |
| 159 | { |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 160 | struct iovec *iov; |
| 161 | vlib_buffer_t *b; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 162 | uword l; |
| 163 | |
| 164 | b = vlib_get_buffer (vm, buffers[i]); |
| 165 | |
| 166 | if (tm->is_ether && (!tm->have_normal_interface)) |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 167 | { |
| 168 | vlib_buffer_reset (b); |
Dave Barach | 178cf49 | 2018-11-13 16:34:13 -0500 | [diff] [blame] | 169 | clib_memcpy_fast (vlib_buffer_get_current (b), tm->ether_dst_mac, |
| 170 | 6); |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 171 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 172 | |
| 173 | /* Re-set iovecs if present. */ |
Steven | 4cd2576 | 2017-10-05 00:12:33 -0700 | [diff] [blame] | 174 | if (tm->threads[thread_index].iovecs) |
| 175 | _vec_len (tm->threads[thread_index].iovecs) = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 176 | |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 177 | /** VLIB buffer chain -> Unix iovec(s). */ |
Steven | 4cd2576 | 2017-10-05 00:12:33 -0700 | [diff] [blame] | 178 | vec_add2 (tm->threads[thread_index].iovecs, iov, 1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 179 | 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 reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 184 | do |
| 185 | { |
| 186 | b = vlib_get_buffer (vm, b->next_buffer); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 187 | |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 188 | vec_add2 (tm->threads[thread_index].iovecs, iov, 1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 189 | |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 190 | 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 195 | } |
| 196 | |
Steven | 4cd2576 | 2017-10-05 00:12:33 -0700 | [diff] [blame] | 197 | if (writev (tm->dev_net_tun_fd, tm->threads[thread_index].iovecs, |
| 198 | vec_len (tm->threads[thread_index].iovecs)) < l) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 199 | clib_unix_warning ("writev"); |
John Lo | 7394b5b | 2016-09-04 08:55:34 -0400 | [diff] [blame] | 200 | |
| 201 | n_bytes += l; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 202 | } |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 203 | |
John Lo | 7394b5b | 2016-09-04 08:55:34 -0400 | [diff] [blame] | 204 | /* Update tuntap interface output stats. */ |
| 205 | vlib_increment_combined_counter (im->combined_sw_if_counters |
| 206 | + VNET_INTERFACE_COUNTER_TX, |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 207 | vm->thread_index, |
John Lo | 7394b5b | 2016-09-04 08:55:34 -0400 | [diff] [blame] | 208 | tm->sw_if_index, n_packets, n_bytes); |
| 209 | |
| 210 | |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 211 | /** The normal interface path flattens the buffer chain */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 212 | 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) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 216 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 217 | return n_packets; |
| 218 | } |
| 219 | |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 220 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 221 | VLIB_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 reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 227 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 228 | |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 229 | /** |
| 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 240 | static uword |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 241 | tuntap_rx (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 242 | { |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 243 | tuntap_main_t *tm = &tuntap_main; |
| 244 | vlib_buffer_t *b; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 245 | u32 bi; |
Damjan Marion | 1901020 | 2016-03-24 17:17:47 +0100 | [diff] [blame] | 246 | const uword buffer_size = VLIB_BUFFER_DATA_SIZE; |
Damjan Marion | 067cd62 | 2018-07-11 12:47:43 +0200 | [diff] [blame] | 247 | u16 thread_index = vm->thread_index; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 248 | |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 249 | /** Make sure we have some RX buffers. */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 250 | { |
Steven | 4cd2576 | 2017-10-05 00:12:33 -0700 | [diff] [blame] | 251 | uword n_left = vec_len (tm->threads[thread_index].rx_buffers); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 252 | uword n_alloc; |
| 253 | |
| 254 | if (n_left < VLIB_FRAME_SIZE / 2) |
| 255 | { |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 256 | if (!tm->threads[thread_index].rx_buffers) |
Steven | 4cd2576 | 2017-10-05 00:12:33 -0700 | [diff] [blame] | 257 | vec_alloc (tm->threads[thread_index].rx_buffers, VLIB_FRAME_SIZE); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 258 | |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 259 | n_alloc = |
| 260 | vlib_buffer_alloc (vm, |
| 261 | tm->threads[thread_index].rx_buffers + n_left, |
| 262 | VLIB_FRAME_SIZE - n_left); |
Steven | 4cd2576 | 2017-10-05 00:12:33 -0700 | [diff] [blame] | 263 | _vec_len (tm->threads[thread_index].rx_buffers) = n_left + n_alloc; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 264 | } |
| 265 | } |
| 266 | |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 267 | /** Allocate RX buffers from end of rx_buffers. |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 268 | Turn them into iovecs to pass to readv. */ |
| 269 | { |
Steven | 4cd2576 | 2017-10-05 00:12:33 -0700 | [diff] [blame] | 270 | uword i_rx = vec_len (tm->threads[thread_index].rx_buffers) - 1; |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 271 | vlib_buffer_t *b; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 272 | word i, n_bytes_left, n_bytes_in_packet; |
| 273 | |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 274 | /** We should have enough buffers left for an MTU sized packet. */ |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 275 | ASSERT (vec_len (tm->threads[thread_index].rx_buffers) >= |
| 276 | tm->mtu_buffers); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 277 | |
Steven | 4cd2576 | 2017-10-05 00:12:33 -0700 | [diff] [blame] | 278 | vec_validate (tm->threads[thread_index].iovecs, tm->mtu_buffers - 1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 279 | for (i = 0; i < tm->mtu_buffers; i++) |
| 280 | { |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 281 | b = |
| 282 | vlib_get_buffer (vm, |
| 283 | tm->threads[thread_index].rx_buffers[i_rx - i]); |
Steven | 4cd2576 | 2017-10-05 00:12:33 -0700 | [diff] [blame] | 284 | tm->threads[thread_index].iovecs[i].iov_base = b->data; |
| 285 | tm->threads[thread_index].iovecs[i].iov_len = buffer_size; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 286 | } |
| 287 | |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 288 | n_bytes_left = |
| 289 | readv (tm->dev_net_tun_fd, tm->threads[thread_index].iovecs, |
| 290 | tm->mtu_buffers); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 291 | n_bytes_in_packet = n_bytes_left; |
| 292 | if (n_bytes_left <= 0) |
| 293 | { |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 294 | if (errno != EAGAIN) |
| 295 | clib_unix_warning ("readv %d", n_bytes_left); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 296 | return 0; |
| 297 | } |
| 298 | |
Steven | 4cd2576 | 2017-10-05 00:12:33 -0700 | [diff] [blame] | 299 | bi = tm->threads[thread_index].rx_buffers[i_rx]; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 300 | |
| 301 | while (1) |
| 302 | { |
Steven | 4cd2576 | 2017-10-05 00:12:33 -0700 | [diff] [blame] | 303 | b = vlib_get_buffer (vm, tm->threads[thread_index].rx_buffers[i_rx]); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 304 | b->flags = 0; |
| 305 | b->current_data = 0; |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 306 | b->current_length = |
| 307 | n_bytes_left < buffer_size ? n_bytes_left : buffer_size; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 308 | |
| 309 | n_bytes_left -= buffer_size; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 310 | |
| 311 | if (n_bytes_left <= 0) |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 312 | { |
| 313 | break; |
| 314 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 315 | |
| 316 | i_rx--; |
| 317 | b->flags |= VLIB_BUFFER_NEXT_PRESENT; |
Steven | 4cd2576 | 2017-10-05 00:12:33 -0700 | [diff] [blame] | 318 | b->next_buffer = tm->threads[thread_index].rx_buffers[i_rx]; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 319 | } |
| 320 | |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 321 | /** Interface counters for tuntap interface. */ |
| 322 | vlib_increment_combined_counter |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 323 | (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) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 326 | |
Steven | 4cd2576 | 2017-10-05 00:12:33 -0700 | [diff] [blame] | 327 | _vec_len (tm->threads[thread_index].rx_buffers) = i_rx; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 328 | } |
| 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 reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 337 | vnet_buffer (b)->sw_if_index[VLIB_TX] = (u32) ~ 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 338 | |
| 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 reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 345 | b->pre_data[0] = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 346 | |
| 347 | b->error = node->errors[0]; |
| 348 | |
| 349 | if (tm->is_ether) |
| 350 | { |
Damjan Marion | 8bdc63b | 2016-11-02 14:48:21 +0100 | [diff] [blame] | 351 | next_index = VNET_DEVICE_INPUT_NEXT_ETHERNET_INPUT; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 352 | } |
| 353 | else |
| 354 | switch (b->data[0] & 0xf0) |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 355 | { |
| 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 366 | |
| 367 | /* The linux kernel couldn't care less if our interface is up */ |
| 368 | if (tm->have_normal_interface) |
| 369 | { |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 370 | 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 375 | } |
| 376 | |
Damjan Marion | 35af9e5 | 2017-03-06 12:02:50 +0100 | [diff] [blame] | 377 | vnet_feature_start_device_input_x1 (tm->sw_if_index, &next_index, b); |
Damjan Marion | 2231150 | 2016-10-28 20:30:15 +0200 | [diff] [blame] | 378 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 379 | vlib_set_next_frame_buffer (vm, node, next_index, bi); |
| 380 | |
| 381 | if (n_trace > 0) |
| 382 | { |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 383 | vlib_trace_buffer (vm, node, next_index, b, /* follow_chain */ 1); |
| 384 | vlib_set_trace_count (vm, node, n_trace - 1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 385 | } |
| 386 | } |
| 387 | |
| 388 | return 1; |
| 389 | } |
| 390 | |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 391 | /** |
| 392 | * @brief TUNTAP_RX error strings |
| 393 | */ |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 394 | static char *tuntap_rx_error_strings[] = { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 395 | "unknown packet type", |
| 396 | }; |
| 397 | |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 398 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 399 | VLIB_REGISTER_NODE (tuntap_rx_node,static) = { |
| 400 | .function = tuntap_rx, |
| 401 | .name = "tuntap-rx", |
Damjan Marion | 51327ac | 2016-11-09 11:59:42 +0100 | [diff] [blame] | 402 | .sibling_of = "device-input", |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 403 | .type = VLIB_NODE_TYPE_INPUT, |
| 404 | .state = VLIB_NODE_STATE_INTERRUPT, |
| 405 | .vector_size = 4, |
| 406 | .n_errors = 1, |
| 407 | .error_strings = tuntap_rx_error_strings, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 408 | }; |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 409 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 410 | |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 411 | /** |
| 412 | * @brief Gets called when file descriptor is ready from epoll. |
| 413 | * |
Damjan Marion | 56dd543 | 2017-09-08 19:52:02 +0200 | [diff] [blame] | 414 | * @param *uf - clib_file_t |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 415 | * |
| 416 | * @return error - clib_error_t |
| 417 | */ |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 418 | static clib_error_t * |
| 419 | tuntap_read_ready (clib_file_t * uf) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 420 | { |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 421 | vlib_main_t *vm = vlib_get_main (); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 422 | vlib_node_set_interrupt_pending (vm, tuntap_rx_node.index); |
| 423 | return 0; |
| 424 | } |
| 425 | |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 426 | /** |
| 427 | * @brief Clean up the tun/tap device |
| 428 | * |
| 429 | * @param *vm - vlib_main_t |
| 430 | * |
| 431 | * @return error - clib_error_t |
| 432 | * |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 433 | */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 434 | static clib_error_t * |
| 435 | tuntap_exit (vlib_main_t * vm) |
| 436 | { |
| 437 | tuntap_main_t *tm = &tuntap_main; |
| 438 | struct ifreq ifr; |
| 439 | int sfd; |
| 440 | |
| 441 | /* Not present. */ |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 442 | if (!tm->dev_net_tun_fd || tm->dev_net_tun_fd < 0) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 443 | return 0; |
| 444 | |
| 445 | sfd = socket (AF_INET, SOCK_STREAM, 0); |
| 446 | if (sfd < 0) |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 447 | clib_unix_warning ("provisioning socket"); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 448 | |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 449 | clib_memset (&ifr, 0, sizeof (ifr)); |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 450 | strncpy (ifr.ifr_name, tm->tun_name, sizeof (ifr.ifr_name) - 1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 451 | |
| 452 | /* get flags, modify to bring down interface... */ |
| 453 | if (ioctl (sfd, SIOCGIFFLAGS, &ifr) < 0) |
| 454 | clib_unix_warning ("SIOCGIFFLAGS"); |
| 455 | |
| 456 | ifr.ifr_flags &= ~(IFF_UP | IFF_RUNNING); |
| 457 | |
| 458 | if (ioctl (sfd, SIOCSIFFLAGS, &ifr) < 0) |
| 459 | clib_unix_warning ("SIOCSIFFLAGS"); |
| 460 | |
| 461 | /* Turn off persistence */ |
| 462 | if (ioctl (tm->dev_net_tun_fd, TUNSETPERSIST, 0) < 0) |
| 463 | clib_unix_warning ("TUNSETPERSIST"); |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 464 | close (tm->dev_tap_fd); |
Dave Barach | f9c231e | 2016-08-05 10:10:18 -0400 | [diff] [blame] | 465 | if (tm->dev_net_tun_fd >= 0) |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 466 | close (tm->dev_net_tun_fd); |
Dave Barach | 6f6f34f | 2016-08-08 13:05:31 -0400 | [diff] [blame] | 467 | if (sfd >= 0) |
| 468 | close (sfd); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 469 | |
| 470 | return 0; |
| 471 | } |
| 472 | |
| 473 | VLIB_MAIN_LOOP_EXIT_FUNCTION (tuntap_exit); |
| 474 | |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 475 | /** |
| 476 | * @brief CLI function for tun/tap config |
| 477 | * |
| 478 | * @param *vm - vlib_main_t |
| 479 | * @param *input - unformat_input_t |
| 480 | * |
| 481 | * @return error - clib_error_t |
| 482 | * |
| 483 | */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 484 | static clib_error_t * |
| 485 | tuntap_config (vlib_main_t * vm, unformat_input_t * input) |
| 486 | { |
| 487 | tuntap_main_t *tm = &tuntap_main; |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 488 | clib_error_t *error = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 489 | struct ifreq ifr; |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 490 | u8 *name; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 491 | int flags = IFF_TUN | IFF_NO_PI; |
| 492 | int is_enabled = 0, is_ether = 0, have_normal_interface = 0; |
Damjan Marion | 1901020 | 2016-03-24 17:17:47 +0100 | [diff] [blame] | 493 | const uword buffer_size = VLIB_BUFFER_DATA_SIZE; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 494 | |
| 495 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
| 496 | { |
| 497 | if (unformat (input, "mtu %d", &tm->mtu_bytes)) |
| 498 | ; |
| 499 | else if (unformat (input, "enable")) |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 500 | is_enabled = 1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 501 | else if (unformat (input, "disable")) |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 502 | is_enabled = 0; |
| 503 | else if (unformat (input, "ethernet") || unformat (input, "ether")) |
| 504 | is_ether = 1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 505 | else if (unformat (input, "have-normal-interface") || |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 506 | unformat (input, "have-normal")) |
| 507 | have_normal_interface = 1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 508 | else if (unformat (input, "name %s", &name)) |
| 509 | tm->tun_name = (char *) name; |
| 510 | else |
| 511 | return clib_error_return (0, "unknown input `%U'", |
| 512 | format_unformat_error, input); |
| 513 | } |
| 514 | |
| 515 | tm->dev_net_tun_fd = -1; |
| 516 | tm->dev_tap_fd = -1; |
| 517 | |
| 518 | if (is_enabled == 0) |
| 519 | return 0; |
| 520 | |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 521 | if (geteuid ()) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 522 | { |
| 523 | clib_warning ("tuntap disabled: must be superuser"); |
| 524 | return 0; |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 525 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 526 | |
| 527 | tm->is_ether = is_ether; |
| 528 | tm->have_normal_interface = have_normal_interface; |
| 529 | |
| 530 | if (is_ether) |
| 531 | flags = IFF_TAP | IFF_NO_PI; |
| 532 | |
| 533 | if ((tm->dev_net_tun_fd = open ("/dev/net/tun", O_RDWR)) < 0) |
| 534 | { |
| 535 | error = clib_error_return_unix (0, "open /dev/net/tun"); |
| 536 | goto done; |
| 537 | } |
| 538 | |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 539 | clib_memset (&ifr, 0, sizeof (ifr)); |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 540 | strncpy (ifr.ifr_name, tm->tun_name, sizeof (ifr.ifr_name) - 1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 541 | ifr.ifr_flags = flags; |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 542 | if (ioctl (tm->dev_net_tun_fd, TUNSETIFF, (void *) &ifr) < 0) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 543 | { |
| 544 | error = clib_error_return_unix (0, "ioctl TUNSETIFF"); |
| 545 | goto done; |
| 546 | } |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 547 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 548 | /* Make it persistent, at least until we split. */ |
| 549 | if (ioctl (tm->dev_net_tun_fd, TUNSETPERSIST, 1) < 0) |
| 550 | { |
| 551 | error = clib_error_return_unix (0, "TUNSETPERSIST"); |
| 552 | goto done; |
| 553 | } |
| 554 | |
| 555 | /* Open a provisioning socket */ |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 556 | if ((tm->dev_tap_fd = socket (PF_PACKET, SOCK_RAW, htons (ETH_P_ALL))) < 0) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 557 | { |
| 558 | error = clib_error_return_unix (0, "socket"); |
| 559 | goto done; |
| 560 | } |
| 561 | |
| 562 | /* Find the interface index. */ |
| 563 | { |
| 564 | struct ifreq ifr; |
| 565 | struct sockaddr_ll sll; |
| 566 | |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 567 | clib_memset (&ifr, 0, sizeof (ifr)); |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 568 | strncpy (ifr.ifr_name, tm->tun_name, sizeof (ifr.ifr_name) - 1); |
| 569 | if (ioctl (tm->dev_tap_fd, SIOCGIFINDEX, &ifr) < 0) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 570 | { |
| 571 | error = clib_error_return_unix (0, "ioctl SIOCGIFINDEX"); |
| 572 | goto done; |
| 573 | } |
| 574 | |
| 575 | /* Bind the provisioning socket to the interface. */ |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 576 | clib_memset (&sll, 0, sizeof (sll)); |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 577 | sll.sll_family = AF_PACKET; |
| 578 | sll.sll_ifindex = ifr.ifr_ifindex; |
| 579 | sll.sll_protocol = htons (ETH_P_ALL); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 580 | |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 581 | if (bind (tm->dev_tap_fd, (struct sockaddr *) &sll, sizeof (sll)) < 0) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 582 | { |
| 583 | error = clib_error_return_unix (0, "bind"); |
| 584 | goto done; |
| 585 | } |
| 586 | } |
| 587 | |
| 588 | /* non-blocking I/O on /dev/tapX */ |
| 589 | { |
| 590 | int one = 1; |
| 591 | if (ioctl (tm->dev_net_tun_fd, FIONBIO, &one) < 0) |
| 592 | { |
| 593 | error = clib_error_return_unix (0, "ioctl FIONBIO"); |
| 594 | goto done; |
| 595 | } |
| 596 | } |
| 597 | |
| 598 | tm->mtu_buffers = (tm->mtu_bytes + (buffer_size - 1)) / buffer_size; |
| 599 | |
| 600 | ifr.ifr_mtu = tm->mtu_bytes; |
| 601 | if (ioctl (tm->dev_tap_fd, SIOCSIFMTU, &ifr) < 0) |
| 602 | { |
| 603 | error = clib_error_return_unix (0, "ioctl SIOCSIFMTU"); |
| 604 | goto done; |
| 605 | } |
| 606 | |
| 607 | /* get flags, modify to bring up interface... */ |
| 608 | if (ioctl (tm->dev_tap_fd, SIOCGIFFLAGS, &ifr) < 0) |
| 609 | { |
| 610 | error = clib_error_return_unix (0, "ioctl SIOCGIFFLAGS"); |
| 611 | goto done; |
| 612 | } |
| 613 | |
| 614 | ifr.ifr_flags |= (IFF_UP | IFF_RUNNING); |
| 615 | |
| 616 | if (ioctl (tm->dev_tap_fd, SIOCSIFFLAGS, &ifr) < 0) |
| 617 | { |
| 618 | error = clib_error_return_unix (0, "ioctl SIOCSIFFLAGS"); |
| 619 | goto done; |
| 620 | } |
| 621 | |
| 622 | if (is_ether) |
| 623 | { |
| 624 | if (ioctl (tm->dev_tap_fd, SIOCGIFHWADDR, &ifr) < 0) |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 625 | { |
| 626 | error = clib_error_return_unix (0, "ioctl SIOCGIFHWADDR"); |
| 627 | goto done; |
| 628 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 629 | else |
Dave Barach | 178cf49 | 2018-11-13 16:34:13 -0500 | [diff] [blame] | 630 | clib_memcpy_fast (tm->ether_dst_mac, ifr.ifr_hwaddr.sa_data, 6); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 631 | } |
| 632 | |
| 633 | if (have_normal_interface) |
| 634 | { |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 635 | vnet_main_t *vnm = vnet_get_main (); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 636 | error = ethernet_register_interface |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 637 | (vnm, tuntap_dev_class.index, 0 /* device instance */ , |
| 638 | tm->ether_dst_mac /* ethernet address */ , |
| 639 | &tm->hw_if_index, 0 /* flag change */ ); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 640 | if (error) |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 641 | clib_error_report (error); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 642 | tm->sw_if_index = tm->hw_if_index; |
| 643 | vm->os_punt_frame = tuntap_nopunt_frame; |
| 644 | } |
| 645 | else |
| 646 | { |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 647 | vnet_main_t *vnm = vnet_get_main (); |
| 648 | vnet_hw_interface_t *hi; |
| 649 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 650 | vm->os_punt_frame = tuntap_punt_frame; |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 651 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 652 | tm->hw_if_index = vnet_register_interface |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 653 | (vnm, tuntap_dev_class.index, 0 /* device instance */ , |
| 654 | tuntap_interface_class.index, 0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 655 | hi = vnet_get_hw_interface (vnm, tm->hw_if_index); |
| 656 | tm->sw_if_index = hi->sw_if_index; |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 657 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 658 | /* Interface is always up. */ |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 659 | vnet_hw_interface_set_flags (vnm, tm->hw_if_index, |
| 660 | VNET_HW_INTERFACE_FLAG_LINK_UP); |
| 661 | vnet_sw_interface_set_flags (vnm, tm->sw_if_index, |
| 662 | VNET_SW_INTERFACE_FLAG_ADMIN_UP); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 663 | } |
| 664 | |
| 665 | { |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 666 | clib_file_t template = { 0 }; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 667 | template.read_function = tuntap_read_ready; |
| 668 | template.file_descriptor = tm->dev_net_tun_fd; |
Damjan Marion | 56dd543 | 2017-09-08 19:52:02 +0200 | [diff] [blame] | 669 | tm->clib_file_index = clib_file_add (&file_main, &template); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 670 | } |
| 671 | |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 672 | done: |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 673 | if (error) |
| 674 | { |
| 675 | if (tm->dev_net_tun_fd >= 0) |
| 676 | close (tm->dev_net_tun_fd); |
| 677 | if (tm->dev_tap_fd >= 0) |
| 678 | close (tm->dev_tap_fd); |
| 679 | } |
| 680 | |
| 681 | return error; |
| 682 | } |
| 683 | |
| 684 | VLIB_CONFIG_FUNCTION (tuntap_config, "tuntap"); |
| 685 | |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 686 | /** |
| 687 | * @brief Add or Del IP4 address to tun/tap interface |
| 688 | * |
| 689 | * @param *im - ip4_main_t |
| 690 | * @param opaque - uword |
| 691 | * @param sw_if_index - u32 |
| 692 | * @param *address - ip4_address_t |
| 693 | * @param is_delete - u32 |
| 694 | * |
| 695 | */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 696 | void |
| 697 | tuntap_ip4_add_del_interface_address (ip4_main_t * im, |
| 698 | uword opaque, |
| 699 | u32 sw_if_index, |
| 700 | ip4_address_t * address, |
| 701 | u32 address_length, |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 702 | u32 if_address_index, u32 is_delete) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 703 | { |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 704 | tuntap_main_t *tm = &tuntap_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 705 | struct ifreq ifr; |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 706 | subif_address_t subif_addr, *ap; |
| 707 | uword *p; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 708 | |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 709 | /** Tuntap disabled, or using a "normal" interface. */ |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 710 | if (tm->have_normal_interface || tm->dev_tap_fd < 0) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 711 | return; |
| 712 | |
Neale Ranns | e8bad97 | 2017-08-10 11:34:12 -0700 | [diff] [blame] | 713 | /* if the address is being applied to an interface that is not in |
| 714 | * the same table/VRF as this tap, then ignore it. |
Paul Vinciguerra | bdc0e6b | 2018-09-22 05:32:50 -0700 | [diff] [blame] | 715 | * If we don't do this overlapping address spaces in the different tables |
Neale Ranns | e8bad97 | 2017-08-10 11:34:12 -0700 | [diff] [blame] | 716 | * breaks the linux host's routing tables */ |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 717 | if (fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4, |
| 718 | sw_if_index) != |
| 719 | fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4, tm->sw_if_index)) |
| 720 | return; |
Neale Ranns | e8bad97 | 2017-08-10 11:34:12 -0700 | [diff] [blame] | 721 | |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 722 | /** See if we already know about this subif */ |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 723 | clib_memset (&subif_addr, 0, sizeof (subif_addr)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 724 | subif_addr.sw_if_index = sw_if_index; |
Dave Barach | 178cf49 | 2018-11-13 16:34:13 -0500 | [diff] [blame] | 725 | clib_memcpy_fast (&subif_addr.addr, address, sizeof (*address)); |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 726 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 727 | p = mhash_get (&tm->subif_mhash, &subif_addr); |
| 728 | |
| 729 | if (p) |
| 730 | ap = pool_elt_at_index (tm->subifs, p[0]); |
| 731 | else |
| 732 | { |
| 733 | pool_get (tm->subifs, ap); |
| 734 | *ap = subif_addr; |
| 735 | mhash_set (&tm->subif_mhash, ap, ap - tm->subifs, 0); |
| 736 | } |
| 737 | |
| 738 | /* Use subif pool index to select alias device. */ |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 739 | clib_memset (&ifr, 0, sizeof (ifr)); |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 740 | snprintf (ifr.ifr_name, sizeof (ifr.ifr_name), |
| 741 | "%s:%d", tm->tun_name, (int) (ap - tm->subifs)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 742 | |
Igor Mikhailov (imichail) | 80e8816 | 2016-11-04 20:25:00 -0700 | [diff] [blame] | 743 | /* the tuntap punt/inject is enabled for IPv4 RX so long as |
| 744 | * any vpp interface has an IPv4 address. |
| 745 | * this is also ref counted. |
| 746 | */ |
| 747 | ip4_sw_interface_enable_disable (tm->sw_if_index, !is_delete); |
| 748 | |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 749 | if (!is_delete) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 750 | { |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 751 | struct sockaddr_in *sin; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 752 | |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 753 | sin = (struct sockaddr_in *) &ifr.ifr_addr; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 754 | |
| 755 | /* Set ipv4 address, netmask. */ |
| 756 | sin->sin_family = AF_INET; |
Dave Barach | 178cf49 | 2018-11-13 16:34:13 -0500 | [diff] [blame] | 757 | clib_memcpy_fast (&sin->sin_addr.s_addr, address, 4); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 758 | if (ioctl (tm->dev_tap_fd, SIOCSIFADDR, &ifr) < 0) |
| 759 | clib_unix_warning ("ioctl SIOCSIFADDR"); |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 760 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 761 | sin->sin_addr.s_addr = im->fib_masks[address_length]; |
| 762 | if (ioctl (tm->dev_tap_fd, SIOCSIFNETMASK, &ifr) < 0) |
| 763 | clib_unix_warning ("ioctl SIOCSIFNETMASK"); |
| 764 | } |
| 765 | else |
| 766 | { |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 767 | mhash_unset (&tm->subif_mhash, &subif_addr, 0 /* old value ptr */ ); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 768 | pool_put (tm->subifs, ap); |
| 769 | } |
| 770 | |
| 771 | /* get flags, modify to bring up interface... */ |
| 772 | if (ioctl (tm->dev_tap_fd, SIOCGIFFLAGS, &ifr) < 0) |
| 773 | clib_unix_warning ("ioctl SIOCGIFFLAGS"); |
| 774 | |
| 775 | if (is_delete) |
| 776 | ifr.ifr_flags &= ~(IFF_UP | IFF_RUNNING); |
| 777 | else |
| 778 | ifr.ifr_flags |= (IFF_UP | IFF_RUNNING); |
| 779 | |
| 780 | if (ioctl (tm->dev_tap_fd, SIOCSIFFLAGS, &ifr) < 0) |
| 781 | clib_unix_warning ("ioctl SIOCSIFFLAGS"); |
| 782 | } |
| 783 | |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 784 | /** |
Chris Luke | 16bcf7d | 2016-09-01 14:31:46 -0400 | [diff] [blame] | 785 | * @brief workaround for a known include file bug. |
| 786 | * including @c <linux/ipv6.h> causes multiple definitions if |
| 787 | * @c <netinet/in.h is also included. |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 788 | */ |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 789 | struct in6_ifreq |
| 790 | { |
| 791 | struct in6_addr ifr6_addr; |
| 792 | u32 ifr6_prefixlen; |
| 793 | int ifr6_ifindex; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 794 | }; |
| 795 | |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 796 | /** |
Chris Luke | 16bcf7d | 2016-09-01 14:31:46 -0400 | [diff] [blame] | 797 | * @brief Add or Del tun/tap interface address. |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 798 | * |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 799 | * Both the v6 interface address API and the way ifconfig |
Paul Vinciguerra | bdc0e6b | 2018-09-22 05:32:50 -0700 | [diff] [blame] | 800 | * displays subinterfaces differ from their v4 counterparts. |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 801 | * The code given here seems to work but YMMV. |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 802 | * |
| 803 | * @param *im - ip6_main_t |
| 804 | * @param opaque - uword |
| 805 | * @param sw_if_index - u32 |
| 806 | * @param *address - ip6_address_t |
| 807 | * @param address_length - u32 |
| 808 | * @param if_address_index - u32 |
| 809 | * @param is_delete - u32 |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 810 | */ |
| 811 | void |
| 812 | tuntap_ip6_add_del_interface_address (ip6_main_t * im, |
| 813 | uword opaque, |
| 814 | u32 sw_if_index, |
| 815 | ip6_address_t * address, |
| 816 | u32 address_length, |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 817 | u32 if_address_index, u32 is_delete) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 818 | { |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 819 | tuntap_main_t *tm = &tuntap_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 820 | struct ifreq ifr; |
| 821 | struct in6_ifreq ifr6; |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 822 | subif_address_t subif_addr, *ap; |
| 823 | uword *p; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 824 | |
| 825 | /* Tuntap disabled, or using a "normal" interface. */ |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 826 | if (tm->have_normal_interface || tm->dev_tap_fd < 0) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 827 | return; |
| 828 | |
Neale Ranns | e8bad97 | 2017-08-10 11:34:12 -0700 | [diff] [blame] | 829 | /* if the address is being applied to an interface that is not in |
| 830 | * the same table/VRF as this tap, then ignore it. |
Paul Vinciguerra | bdc0e6b | 2018-09-22 05:32:50 -0700 | [diff] [blame] | 831 | * If we don't do this overlapping address spaces in the different tables |
Neale Ranns | e8bad97 | 2017-08-10 11:34:12 -0700 | [diff] [blame] | 832 | * breaks the linux host's routing tables */ |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 833 | if (fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP6, |
| 834 | sw_if_index) != |
| 835 | fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP6, tm->sw_if_index)) |
| 836 | return; |
Neale Ranns | e8bad97 | 2017-08-10 11:34:12 -0700 | [diff] [blame] | 837 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 838 | /* See if we already know about this subif */ |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 839 | clib_memset (&subif_addr, 0, sizeof (subif_addr)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 840 | subif_addr.sw_if_index = sw_if_index; |
| 841 | subif_addr.is_v6 = 1; |
Dave Barach | 178cf49 | 2018-11-13 16:34:13 -0500 | [diff] [blame] | 842 | clib_memcpy_fast (&subif_addr.addr, address, sizeof (*address)); |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 843 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 844 | p = mhash_get (&tm->subif_mhash, &subif_addr); |
| 845 | |
| 846 | if (p) |
| 847 | ap = pool_elt_at_index (tm->subifs, p[0]); |
| 848 | else |
| 849 | { |
| 850 | pool_get (tm->subifs, ap); |
| 851 | *ap = subif_addr; |
| 852 | mhash_set (&tm->subif_mhash, ap, ap - tm->subifs, 0); |
| 853 | } |
| 854 | |
| 855 | /* Use subif pool index to select alias device. */ |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 856 | clib_memset (&ifr, 0, sizeof (ifr)); |
| 857 | clib_memset (&ifr6, 0, sizeof (ifr6)); |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 858 | snprintf (ifr.ifr_name, sizeof (ifr.ifr_name), |
| 859 | "%s:%d", tm->tun_name, (int) (ap - tm->subifs)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 860 | |
Igor Mikhailov (imichail) | 80e8816 | 2016-11-04 20:25:00 -0700 | [diff] [blame] | 861 | /* the tuntap punt/inject is enabled for IPv6 RX so long as |
| 862 | * any vpp interface has an IPv6 address. |
| 863 | * this is also ref counted. |
| 864 | */ |
| 865 | ip6_sw_interface_enable_disable (tm->sw_if_index, !is_delete); |
| 866 | |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 867 | if (!is_delete) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 868 | { |
| 869 | int sockfd = socket (AF_INET6, SOCK_STREAM, 0); |
| 870 | if (sockfd < 0) |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 871 | clib_unix_warning ("get ifindex socket"); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 872 | |
| 873 | if (ioctl (sockfd, SIOGIFINDEX, &ifr) < 0) |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 874 | clib_unix_warning ("get ifindex"); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 875 | |
| 876 | ifr6.ifr6_ifindex = ifr.ifr_ifindex; |
| 877 | ifr6.ifr6_prefixlen = address_length; |
Dave Barach | 178cf49 | 2018-11-13 16:34:13 -0500 | [diff] [blame] | 878 | clib_memcpy_fast (&ifr6.ifr6_addr, address, 16); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 879 | |
| 880 | if (ioctl (sockfd, SIOCSIFADDR, &ifr6) < 0) |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 881 | clib_unix_warning ("set address"); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 882 | |
Dave Barach | 6f6f34f | 2016-08-08 13:05:31 -0400 | [diff] [blame] | 883 | if (sockfd >= 0) |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 884 | close (sockfd); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 885 | } |
| 886 | else |
| 887 | { |
| 888 | int sockfd = socket (AF_INET6, SOCK_STREAM, 0); |
| 889 | if (sockfd < 0) |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 890 | clib_unix_warning ("get ifindex socket"); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 891 | |
| 892 | if (ioctl (sockfd, SIOGIFINDEX, &ifr) < 0) |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 893 | clib_unix_warning ("get ifindex"); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 894 | |
| 895 | ifr6.ifr6_ifindex = ifr.ifr_ifindex; |
| 896 | ifr6.ifr6_prefixlen = address_length; |
Dave Barach | 178cf49 | 2018-11-13 16:34:13 -0500 | [diff] [blame] | 897 | clib_memcpy_fast (&ifr6.ifr6_addr, address, 16); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 898 | |
| 899 | if (ioctl (sockfd, SIOCDIFADDR, &ifr6) < 0) |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 900 | clib_unix_warning ("del address"); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 901 | |
Dave Barach | f9c231e | 2016-08-05 10:10:18 -0400 | [diff] [blame] | 902 | if (sockfd >= 0) |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 903 | close (sockfd); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 904 | |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 905 | mhash_unset (&tm->subif_mhash, &subif_addr, 0 /* old value ptr */ ); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 906 | pool_put (tm->subifs, ap); |
| 907 | } |
| 908 | } |
| 909 | |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 910 | /** |
| 911 | * @brief TX the tun/tap frame |
| 912 | * |
| 913 | * @param *vm - vlib_main_t |
| 914 | * @param *node - vlib_node_runtime_t |
| 915 | * @param *frame - vlib_frame_t |
| 916 | * |
| 917 | */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 918 | static void |
| 919 | tuntap_punt_frame (vlib_main_t * vm, |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 920 | vlib_node_runtime_t * node, vlib_frame_t * frame) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 921 | { |
| 922 | tuntap_tx (vm, node, frame); |
| 923 | vlib_frame_free (vm, node, frame); |
| 924 | } |
| 925 | |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 926 | /** |
| 927 | * @brief Free the tun/tap frame |
| 928 | * |
| 929 | * @param *vm - vlib_main_t |
| 930 | * @param *node - vlib_node_runtime_t |
| 931 | * @param *frame - vlib_frame_t |
| 932 | * |
| 933 | */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 934 | static void |
| 935 | tuntap_nopunt_frame (vlib_main_t * vm, |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 936 | vlib_node_runtime_t * node, vlib_frame_t * frame) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 937 | { |
Damjan Marion | a3d5986 | 2018-11-10 10:23:00 +0100 | [diff] [blame] | 938 | u32 *buffers = vlib_frame_vector_args (frame); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 939 | uword n_packets = frame->n_vectors; |
| 940 | vlib_buffer_free (vm, buffers, n_packets); |
| 941 | vlib_frame_free (vm, node, frame); |
| 942 | } |
| 943 | |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 944 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 945 | VNET_HW_INTERFACE_CLASS (tuntap_interface_class,static) = { |
| 946 | .name = "tuntap", |
Neale Ranns | b80c536 | 2016-10-08 13:03:40 +0100 | [diff] [blame] | 947 | .flags = VNET_HW_INTERFACE_CLASS_FLAG_P2P, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 948 | }; |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 949 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 950 | |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 951 | /** |
| 952 | * @brief Format tun/tap interface name |
| 953 | * |
| 954 | * @param *s - u8 - formatter string |
| 955 | * @param *args - va_list |
| 956 | * |
| 957 | * @return *s - u8 - formatted string |
| 958 | * |
| 959 | */ |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 960 | static u8 * |
| 961 | format_tuntap_interface_name (u8 * s, va_list * args) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 962 | { |
| 963 | u32 i = va_arg (*args, u32); |
| 964 | |
| 965 | s = format (s, "tuntap-%d", i); |
| 966 | return s; |
| 967 | } |
| 968 | |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 969 | /** |
| 970 | * @brief TX packet out tun/tap |
| 971 | * |
| 972 | * @param *vm - vlib_main_t |
| 973 | * @param *node - vlib_node_runtime_t |
| 974 | * @param *frame - vlib_frame_t |
| 975 | * |
| 976 | * @return n_buffers - uword - Packets transmitted |
| 977 | * |
| 978 | */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 979 | static uword |
| 980 | tuntap_intfc_tx (vlib_main_t * vm, |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 981 | vlib_node_runtime_t * node, vlib_frame_t * frame) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 982 | { |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 983 | tuntap_main_t *tm = &tuntap_main; |
Damjan Marion | a3d5986 | 2018-11-10 10:23:00 +0100 | [diff] [blame] | 984 | u32 *buffers = vlib_frame_vector_args (frame); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 985 | uword n_buffers = frame->n_vectors; |
| 986 | |
| 987 | /* Normal interface transmit happens only on the normal interface... */ |
| 988 | if (tm->have_normal_interface) |
| 989 | return tuntap_tx (vm, node, frame); |
| 990 | |
| 991 | vlib_buffer_free (vm, buffers, n_buffers); |
| 992 | return n_buffers; |
| 993 | } |
| 994 | |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 995 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 996 | VNET_DEVICE_CLASS (tuntap_dev_class,static) = { |
| 997 | .name = "tuntap", |
| 998 | .tx_function = tuntap_intfc_tx, |
| 999 | .format_device_name = format_tuntap_interface_name, |
| 1000 | }; |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 1001 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1002 | |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 1003 | /** |
| 1004 | * @brief tun/tap node init |
| 1005 | * |
| 1006 | * @param *vm - vlib_main_t |
| 1007 | * |
| 1008 | * @return error - clib_error_t |
| 1009 | * |
| 1010 | */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1011 | static clib_error_t * |
| 1012 | tuntap_init (vlib_main_t * vm) |
| 1013 | { |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 1014 | clib_error_t *error; |
| 1015 | ip4_main_t *im4 = &ip4_main; |
| 1016 | ip6_main_t *im6 = &ip6_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1017 | ip4_add_del_interface_address_callback_t cb4; |
| 1018 | ip6_add_del_interface_address_callback_t cb6; |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 1019 | tuntap_main_t *tm = &tuntap_main; |
| 1020 | vlib_thread_main_t *m = vlib_get_thread_main (); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1021 | |
| 1022 | error = vlib_call_init_function (vm, ip4_init); |
| 1023 | if (error) |
| 1024 | return error; |
| 1025 | |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 1026 | mhash_init (&tm->subif_mhash, sizeof (u32), sizeof (subif_address_t)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1027 | |
| 1028 | cb4.function = tuntap_ip4_add_del_interface_address; |
| 1029 | cb4.function_opaque = 0; |
| 1030 | vec_add1 (im4->add_del_interface_address_callbacks, cb4); |
| 1031 | |
| 1032 | cb6.function = tuntap_ip6_add_del_interface_address; |
| 1033 | cb6.function_opaque = 0; |
| 1034 | vec_add1 (im6->add_del_interface_address_callbacks, cb6); |
Steven | 4cd2576 | 2017-10-05 00:12:33 -0700 | [diff] [blame] | 1035 | vec_validate_aligned (tm->threads, m->n_vlib_mains - 1, |
| 1036 | CLIB_CACHE_LINE_BYTES); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1037 | |
| 1038 | return 0; |
| 1039 | } |
| 1040 | |
| 1041 | VLIB_INIT_FUNCTION (tuntap_init); |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 1042 | |
| 1043 | /* |
| 1044 | * fd.io coding-style-patch-verification: ON |
| 1045 | * |
| 1046 | * Local Variables: |
| 1047 | * eval: (c-set-style "gnu") |
| 1048 | * End: |
| 1049 | */ |