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