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