Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1 | /* |
| 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> |
| 48 | |
| 49 | #include <vnet/ethernet/ethernet.h> |
Damjan Marion | 8bdc63b | 2016-11-02 14:48:21 +0100 | [diff] [blame] | 50 | #include <vnet/devices/devices.h> |
Damjan Marion | 2231150 | 2016-10-28 20:30:15 +0200 | [diff] [blame] | 51 | #include <vnet/feature/feature.h> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 52 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 53 | static vnet_device_class_t tuntap_dev_class; |
| 54 | static vnet_hw_interface_class_t tuntap_interface_class; |
| 55 | |
| 56 | static void tuntap_punt_frame (vlib_main_t * vm, |
| 57 | vlib_node_runtime_t * node, |
| 58 | vlib_frame_t * frame); |
| 59 | static void tuntap_nopunt_frame (vlib_main_t * vm, |
| 60 | vlib_node_runtime_t * node, |
| 61 | vlib_frame_t * frame); |
| 62 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 63 | typedef struct { |
| 64 | u32 sw_if_index; |
| 65 | u8 is_v6; |
| 66 | u8 addr[16]; |
| 67 | } subif_address_t; |
| 68 | |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 69 | /** |
| 70 | * @brief TUNTAP node main state |
| 71 | */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 72 | typedef struct { |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 73 | /** Vector of iovecs for readv/writev calls. */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 74 | struct iovec * iovecs; |
| 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). */ |
| 78 | u32 * rx_buffers; |
| 79 | |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 80 | /** File descriptors for /dev/net/tun and provisioning socket. */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 81 | int dev_net_tun_fd, dev_tap_fd; |
| 82 | |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 83 | /** Create a "tap" [ethernet] encaps device */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 84 | int is_ether; |
| 85 | |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 86 | /** 1 if a "normal" routed intfc, 0 if a punt/inject interface */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 87 | |
| 88 | int have_normal_interface; |
| 89 | |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 90 | /** tap device destination MAC address. Required, or Linux drops pkts */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 91 | u8 ether_dst_mac[6]; |
| 92 | |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 93 | /** Interface MTU in bytes and # of default sized buffers. */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 94 | u32 mtu_bytes, mtu_buffers; |
| 95 | |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 96 | /** Linux interface name for tun device. */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 97 | char * tun_name; |
| 98 | |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 99 | /** Pool of subinterface addresses */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 100 | subif_address_t *subifs; |
| 101 | |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 102 | /** Hash for subif addresses */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 103 | mhash_t subif_mhash; |
| 104 | |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 105 | /** Unix file index */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 106 | u32 unix_file_index; |
| 107 | |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 108 | /** For the "normal" interface, if configured */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 109 | u32 hw_if_index, sw_if_index; |
| 110 | |
| 111 | } tuntap_main_t; |
| 112 | |
| 113 | static tuntap_main_t tuntap_main = { |
| 114 | .tun_name = "vnet", |
| 115 | |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 116 | /** Suitable defaults for an Ethernet-like tun/tap device */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 117 | .mtu_bytes = 4096 + 256, |
| 118 | }; |
| 119 | |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 120 | /** |
| 121 | * @brief tuntap_tx |
| 122 | * @node tuntap-tx |
| 123 | * |
| 124 | * Output node, writes the buffers comprising the incoming frame |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 125 | * 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] | 126 | * |
| 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 133 | */ |
| 134 | static uword |
| 135 | tuntap_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 Lo | 7394b5b | 2016-09-04 08:55:34 -0400 | [diff] [blame] | 142 | vnet_main_t *vnm = vnet_get_main (); |
| 143 | vnet_interface_main_t *im = &vnm->interface_main; |
| 144 | u32 n_bytes = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 145 | 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 Marion | f1213b8 | 2016-03-13 02:22:06 +0100 | [diff] [blame] | 158 | clib_memcpy (vlib_buffer_get_current (b), tm->ether_dst_mac, 6); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | /* Re-set iovecs if present. */ |
| 162 | if (tm->iovecs) |
| 163 | _vec_len (tm->iovecs) = 0; |
| 164 | |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 165 | /** VLIB buffer chain -> Unix iovec(s). */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 166 | 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 Lo | 7394b5b | 2016-09-04 08:55:34 -0400 | [diff] [blame] | 185 | |
| 186 | n_bytes += l; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 187 | } |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 188 | |
John Lo | 7394b5b | 2016-09-04 08:55:34 -0400 | [diff] [blame] | 189 | /* Update tuntap interface output stats. */ |
| 190 | vlib_increment_combined_counter (im->combined_sw_if_counters |
| 191 | + VNET_INTERFACE_COUNTER_TX, |
| 192 | vm->cpu_index, |
| 193 | tm->sw_if_index, n_packets, n_bytes); |
| 194 | |
| 195 | |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 196 | /** The normal interface path flattens the buffer chain */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 197 | 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) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 201 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 202 | return n_packets; |
| 203 | } |
| 204 | |
| 205 | VLIB_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) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 212 | /** |
| 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 223 | static uword |
| 224 | tuntap_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 Marion | 1901020 | 2016-03-24 17:17:47 +0100 | [diff] [blame] | 231 | const uword buffer_size = VLIB_BUFFER_DATA_SIZE; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 232 | |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 233 | /** Make sure we have some RX buffers. */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 234 | { |
| 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 Marion | 6765549 | 2016-11-15 12:50:28 +0100 | [diff] [blame] | 243 | n_alloc = vlib_buffer_alloc (vm, tm->rx_buffers + n_left, VLIB_FRAME_SIZE - n_left); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 244 | _vec_len (tm->rx_buffers) = n_left + n_alloc; |
| 245 | } |
| 246 | } |
| 247 | |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 248 | /** Allocate RX buffers from end of rx_buffers. |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 249 | 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) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 255 | /** We should have enough buffers left for an MTU sized packet. */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 256 | 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 279 | b = vlib_get_buffer (vm, tm->rx_buffers[i_rx]); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 280 | 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 285 | |
| 286 | if (n_bytes_left <= 0) |
| 287 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 288 | break; |
| 289 | } |
| 290 | |
| 291 | i_rx--; |
| 292 | b->flags |= VLIB_BUFFER_NEXT_PRESENT; |
| 293 | b->next_buffer = tm->rx_buffers[i_rx]; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 294 | } |
| 295 | |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 296 | /** Interface counters for tuntap interface. */ |
| 297 | vlib_increment_combined_counter |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 298 | (vnet_main.interface_main.combined_sw_if_counters |
| 299 | + VNET_INTERFACE_COUNTER_RX, |
| 300 | os_get_cpu_number(), |
| 301 | tm->sw_if_index, |
| 302 | 1, n_bytes_in_packet); |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 303 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 304 | _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 Marion | 8bdc63b | 2016-11-02 14:48:21 +0100 | [diff] [blame] | 328 | next_index = VNET_DEVICE_INPUT_NEXT_ETHERNET_INPUT; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 329 | } |
| 330 | else |
| 331 | switch (b->data[0] & 0xf0) |
| 332 | { |
| 333 | case 0x40: |
Damjan Marion | 8bdc63b | 2016-11-02 14:48:21 +0100 | [diff] [blame] | 334 | next_index = VNET_DEVICE_INPUT_NEXT_IP4_INPUT; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 335 | break; |
| 336 | case 0x60: |
Damjan Marion | 8bdc63b | 2016-11-02 14:48:21 +0100 | [diff] [blame] | 337 | next_index = VNET_DEVICE_INPUT_NEXT_IP6_INPUT; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 338 | break; |
| 339 | default: |
Damjan Marion | 8bdc63b | 2016-11-02 14:48:21 +0100 | [diff] [blame] | 340 | next_index = VNET_DEVICE_INPUT_NEXT_DROP; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 341 | 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 Marion | 8bdc63b | 2016-11-02 14:48:21 +0100 | [diff] [blame] | 351 | next_index = VNET_DEVICE_INPUT_NEXT_DROP; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 352 | } |
| 353 | |
Damjan Marion | 87cd119 | 2016-11-04 11:00:27 +0100 | [diff] [blame] | 354 | vnet_feature_start_device_input_x1 (tm->sw_if_index, &next_index, b, 0); |
Damjan Marion | 2231150 | 2016-10-28 20:30:15 +0200 | [diff] [blame] | 355 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 356 | 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) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 369 | /** |
| 370 | * @brief TUNTAP_RX error strings |
| 371 | */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 372 | static char * tuntap_rx_error_strings[] = { |
| 373 | "unknown packet type", |
| 374 | }; |
| 375 | |
| 376 | VLIB_REGISTER_NODE (tuntap_rx_node,static) = { |
| 377 | .function = tuntap_rx, |
| 378 | .name = "tuntap-rx", |
Damjan Marion | 51327ac | 2016-11-09 11:59:42 +0100 | [diff] [blame] | 379 | .sibling_of = "device-input", |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 380 | .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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 385 | }; |
| 386 | |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 387 | /** |
| 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 394 | static 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) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 401 | /** |
| 402 | * @brief Clean up the tun/tap device |
| 403 | * |
| 404 | * @param *vm - vlib_main_t |
| 405 | * |
| 406 | * @return error - clib_error_t |
| 407 | * |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 408 | */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 409 | static clib_error_t * |
| 410 | tuntap_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 Barach | f9c231e | 2016-08-05 10:10:18 -0400 | [diff] [blame] | 440 | if (tm->dev_net_tun_fd >= 0) |
Dave Barach | 6f6f34f | 2016-08-08 13:05:31 -0400 | [diff] [blame] | 441 | close(tm->dev_net_tun_fd); |
| 442 | if (sfd >= 0) |
| 443 | close (sfd); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 444 | |
| 445 | return 0; |
| 446 | } |
| 447 | |
| 448 | VLIB_MAIN_LOOP_EXIT_FUNCTION (tuntap_exit); |
| 449 | |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 450 | /** |
| 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 459 | static clib_error_t * |
| 460 | tuntap_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 Marion | 1901020 | 2016-03-24 17:17:47 +0100 | [diff] [blame] | 468 | const uword buffer_size = VLIB_BUFFER_DATA_SIZE; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 469 | |
| 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 Marion | f1213b8 | 2016-03-13 02:22:06 +0100 | [diff] [blame] | 607 | clib_memcpy (tm->ether_dst_mac, ifr.ifr_hwaddr.sa_data, 6); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 608 | } |
| 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 | |
| 665 | VLIB_CONFIG_FUNCTION (tuntap_config, "tuntap"); |
| 666 | |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 667 | /** |
| 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 677 | void |
| 678 | tuntap_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) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 691 | /** Tuntap disabled, or using a "normal" interface. */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 692 | if (tm->have_normal_interface || tm->dev_tap_fd < 0) |
| 693 | return; |
| 694 | |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 695 | /** See if we already know about this subif */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 696 | memset (&subif_addr, 0, sizeof (subif_addr)); |
| 697 | subif_addr.sw_if_index = sw_if_index; |
Damjan Marion | f1213b8 | 2016-03-13 02:22:06 +0100 | [diff] [blame] | 698 | clib_memcpy (&subif_addr.addr, address, sizeof (*address)); |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 699 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 700 | 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) | 80e8816 | 2016-11-04 20:25:00 -0700 | [diff] [blame] | 716 | /* 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 722 | 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 Marion | f1213b8 | 2016-03-13 02:22:06 +0100 | [diff] [blame] | 730 | clib_memcpy (&sin->sin_addr.s_addr, address, 4); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 731 | 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) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 757 | /** |
Chris Luke | 16bcf7d | 2016-09-01 14:31:46 -0400 | [diff] [blame] | 758 | * @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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 761 | */ |
| 762 | struct in6_ifreq { |
| 763 | struct in6_addr ifr6_addr; |
| 764 | u32 ifr6_prefixlen; |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 765 | int ifr6_ifindex; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 766 | }; |
| 767 | |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 768 | /** |
Chris Luke | 16bcf7d | 2016-09-01 14:31:46 -0400 | [diff] [blame] | 769 | * @brief Add or Del tun/tap interface address. |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 770 | * |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 771 | * 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) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 774 | * |
| 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 782 | */ |
| 783 | void |
| 784 | tuntap_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 Marion | f1213b8 | 2016-03-13 02:22:06 +0100 | [diff] [blame] | 806 | clib_memcpy (&subif_addr.addr, address, sizeof (*address)); |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 807 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 808 | 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) | 80e8816 | 2016-11-04 20:25:00 -0700 | [diff] [blame] | 825 | /* 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 831 | 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 Marion | f1213b8 | 2016-03-13 02:22:06 +0100 | [diff] [blame] | 842 | clib_memcpy (&ifr6.ifr6_addr, address, 16); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 843 | |
| 844 | if (ioctl (sockfd, SIOCSIFADDR, &ifr6) < 0) |
| 845 | clib_unix_warning ("set address"); |
| 846 | |
Dave Barach | 6f6f34f | 2016-08-08 13:05:31 -0400 | [diff] [blame] | 847 | if (sockfd >= 0) |
| 848 | close (sockfd); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 849 | } |
| 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 Marion | f1213b8 | 2016-03-13 02:22:06 +0100 | [diff] [blame] | 861 | clib_memcpy (&ifr6.ifr6_addr, address, 16); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 862 | |
| 863 | if (ioctl (sockfd, SIOCDIFADDR, &ifr6) < 0) |
| 864 | clib_unix_warning ("del address"); |
| 865 | |
Dave Barach | f9c231e | 2016-08-05 10:10:18 -0400 | [diff] [blame] | 866 | if (sockfd >= 0) |
| 867 | close (sockfd); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 868 | |
| 869 | mhash_unset (&tm->subif_mhash, &subif_addr, 0 /* old value ptr */); |
| 870 | pool_put (tm->subifs, ap); |
| 871 | } |
| 872 | } |
| 873 | |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 874 | /** |
| 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 882 | static void |
| 883 | tuntap_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) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 891 | /** |
| 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 899 | static void |
| 900 | tuntap_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 | |
| 910 | VNET_HW_INTERFACE_CLASS (tuntap_interface_class,static) = { |
| 911 | .name = "tuntap", |
Neale Ranns | b80c536 | 2016-10-08 13:03:40 +0100 | [diff] [blame] | 912 | .flags = VNET_HW_INTERFACE_CLASS_FLAG_P2P, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 913 | }; |
| 914 | |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 915 | /** |
| 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 924 | static 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) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 932 | /** |
| 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 942 | static uword |
| 943 | tuntap_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 | |
| 959 | VNET_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) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 965 | /** |
| 966 | * @brief tun/tap node init |
| 967 | * |
| 968 | * @param *vm - vlib_main_t |
| 969 | * |
| 970 | * @return error - clib_error_t |
| 971 | * |
| 972 | */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 973 | static clib_error_t * |
| 974 | tuntap_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 | |
| 1000 | VLIB_INIT_FUNCTION (tuntap_init); |