Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1 | /* |
| 2 | * encap.c : L2TPv3 tunnel encapsulation |
| 3 | * |
| 4 | * Copyright (c) 2013 Cisco and/or its affiliates. |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at: |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | #include <vppinfra/error.h> |
| 19 | #include <vppinfra/hash.h> |
| 20 | #include <vnet/vnet.h> |
| 21 | #include <vnet/ip/ip.h> |
| 22 | #include <vnet/ethernet/ethernet.h> |
| 23 | #include <vnet/l2tp/l2tp.h> |
| 24 | |
| 25 | /* Statistics (not really errors) */ |
| 26 | #define foreach_l2t_encap_error \ |
| 27 | _(NETWORK_TO_USER, "L2TP L2 network to user (ip6) pkts") \ |
Pierre Pfister | 80ee213 | 2016-06-22 12:54:48 +0100 | [diff] [blame] | 28 | _(LOOKUP_FAIL_TO_L3, "L2TP L2 session lookup failed pkts") \ |
| 29 | _(ADMIN_DOWN, "L2TP tunnel is down") |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 30 | |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 31 | static char *l2t_encap_error_strings[] = { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 32 | #define _(sym,string) string, |
| 33 | foreach_l2t_encap_error |
| 34 | #undef _ |
| 35 | }; |
| 36 | |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 37 | typedef enum |
| 38 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 39 | #define _(sym,str) L2T_ENCAP_ERROR_##sym, |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 40 | foreach_l2t_encap_error |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 41 | #undef _ |
| 42 | L2T_ENCAP_N_ERROR, |
| 43 | } l2t_encap_error_t; |
| 44 | |
| 45 | |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 46 | typedef enum |
| 47 | { |
| 48 | L2T_ENCAP_NEXT_DROP, |
| 49 | L2T_ENCAP_NEXT_IP6_LOOKUP, |
| 50 | L2T_ENCAP_N_NEXT, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 51 | } l2t_encap_next_t; |
| 52 | |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 53 | typedef struct |
| 54 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 55 | u32 cached_session_index; |
| 56 | u32 cached_sw_if_index; |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 57 | vnet_main_t *vnet_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 58 | } l2tp_encap_runtime_t; |
| 59 | |
| 60 | vlib_node_registration_t l2t_encap_node; |
| 61 | |
| 62 | #define NSTAGES 3 |
| 63 | |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 64 | static inline void |
| 65 | stage0 (vlib_main_t * vm, vlib_node_runtime_t * node, u32 buffer_index) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 66 | { |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 67 | vlib_buffer_t *b = vlib_get_buffer (vm, buffer_index); |
| 68 | vlib_prefetch_buffer_header (b, STORE); |
| 69 | CLIB_PREFETCH (b->data, 2 * CLIB_CACHE_LINE_BYTES, STORE); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 70 | } |
| 71 | |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 72 | static inline void |
| 73 | stage1 (vlib_main_t * vm, vlib_node_runtime_t * node, u32 bi) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 74 | { |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 75 | l2tp_encap_runtime_t *rt = (void *) node->runtime_data; |
| 76 | vlib_buffer_t *b = vlib_get_buffer (vm, bi); |
| 77 | vnet_hw_interface_t *hi; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 78 | |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 79 | u32 sw_if_index = vnet_buffer (b)->sw_if_index[VLIB_TX]; |
| 80 | u32 session_index = rt->cached_session_index; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 81 | |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 82 | if (PREDICT_FALSE (rt->cached_sw_if_index != sw_if_index)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 83 | { |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 84 | hi = vnet_get_sup_hw_interface (rt->vnet_main, sw_if_index); |
| 85 | session_index = rt->cached_session_index = hi->dev_instance; |
| 86 | rt->cached_sw_if_index = sw_if_index; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 87 | } |
| 88 | |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 89 | /* Remember mapping index, prefetch the mini counter */ |
| 90 | vnet_buffer (b)->l2t.next_index = L2T_ENCAP_NEXT_IP6_LOOKUP; |
| 91 | vnet_buffer (b)->l2t.session_index = session_index; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 92 | |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 93 | /* $$$$ prefetch counter... */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 94 | } |
| 95 | |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 96 | static inline u32 |
| 97 | last_stage (vlib_main_t * vm, vlib_node_runtime_t * node, u32 bi) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 98 | { |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 99 | vlib_buffer_t *b = vlib_get_buffer (vm, bi); |
| 100 | l2t_main_t *lm = &l2t_main; |
| 101 | vlib_node_t *n = vlib_get_node (vm, l2t_encap_node.index); |
| 102 | u32 node_counter_base_index = n->error_heap_index; |
| 103 | vlib_error_main_t *em = &vm->error_main; |
| 104 | l2tpv3_header_t *l2tp; |
| 105 | u32 session_index; |
| 106 | u32 counter_index; |
| 107 | l2t_session_t *s; |
| 108 | ip6_header_t *ip6; |
| 109 | u16 payload_length; |
| 110 | u32 next_index = L2T_ENCAP_NEXT_IP6_LOOKUP; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 111 | |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 112 | /* Other-than-output pkt? We're done... */ |
| 113 | if (vnet_buffer (b)->l2t.next_index != L2T_ENCAP_NEXT_IP6_LOOKUP) |
| 114 | return vnet_buffer (b)->l2t.next_index; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 115 | |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 116 | em->counters[node_counter_base_index + L2T_ENCAP_ERROR_NETWORK_TO_USER] += |
| 117 | 1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 118 | |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 119 | session_index = vnet_buffer (b)->l2t.session_index; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 120 | |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 121 | counter_index = |
| 122 | session_index_to_counter_index (session_index, |
| 123 | SESSION_COUNTER_NETWORK_TO_USER); |
Pierre Pfister | 08e0312 | 2016-07-15 09:19:39 +0100 | [diff] [blame] | 124 | |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 125 | /* per-mapping byte stats include the ethernet header */ |
| 126 | vlib_increment_combined_counter (&lm->counter_main, |
| 127 | os_get_cpu_number (), |
| 128 | counter_index, 1 /* packet_increment */ , |
| 129 | vlib_buffer_length_in_chain (vm, b)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 130 | |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 131 | s = pool_elt_at_index (lm->sessions, session_index); |
| 132 | |
| 133 | vnet_buffer (b)->sw_if_index[VLIB_TX] = s->encap_fib_index; |
| 134 | |
| 135 | /* Paint on an l2tpv3 hdr */ |
| 136 | vlib_buffer_advance (b, -(s->l2tp_hdr_size)); |
| 137 | l2tp = vlib_buffer_get_current (b); |
| 138 | |
| 139 | l2tp->session_id = s->remote_session_id; |
| 140 | l2tp->cookie = s->remote_cookie; |
| 141 | if (PREDICT_FALSE (s->l2_sublayer_present)) |
| 142 | { |
| 143 | l2tp->l2_specific_sublayer = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 144 | } |
| 145 | |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 146 | /* Paint on an ip6 header */ |
| 147 | vlib_buffer_advance (b, -(sizeof (*ip6))); |
| 148 | ip6 = vlib_buffer_get_current (b); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 149 | |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 150 | if (PREDICT_FALSE (!(s->admin_up))) |
| 151 | { |
| 152 | b->error = node->errors[L2T_ENCAP_ERROR_ADMIN_DOWN]; |
| 153 | next_index = L2T_ENCAP_NEXT_DROP; |
| 154 | goto done; |
Pierre Pfister | 80ee213 | 2016-06-22 12:54:48 +0100 | [diff] [blame] | 155 | } |
| 156 | |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 157 | ip6->ip_version_traffic_class_and_flow_label = |
| 158 | clib_host_to_net_u32 (0x6 << 28); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 159 | |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 160 | /* calculate ip6 payload length */ |
| 161 | payload_length = vlib_buffer_length_in_chain (vm, b); |
| 162 | payload_length -= sizeof (*ip6); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 163 | |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 164 | ip6->payload_length = clib_host_to_net_u16 (payload_length); |
| 165 | ip6->protocol = IP_PROTOCOL_L2TP; |
| 166 | ip6->hop_limit = 0xff; |
| 167 | ip6->src_address.as_u64[0] = s->our_address.as_u64[0]; |
| 168 | ip6->src_address.as_u64[1] = s->our_address.as_u64[1]; |
| 169 | ip6->dst_address.as_u64[0] = s->client_address.as_u64[0]; |
| 170 | ip6->dst_address.as_u64[1] = s->client_address.as_u64[1]; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 171 | |
Pierre Pfister | 80ee213 | 2016-06-22 12:54:48 +0100 | [diff] [blame] | 172 | |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 173 | done: |
| 174 | if (PREDICT_FALSE (b->flags & VLIB_BUFFER_IS_TRACED)) |
| 175 | { |
| 176 | l2t_trace_t *t = vlib_add_trace (vm, node, b, sizeof (*t)); |
| 177 | t->is_user_to_network = 0; |
| 178 | t->our_address.as_u64[0] = ip6->src_address.as_u64[0]; |
| 179 | t->our_address.as_u64[1] = ip6->src_address.as_u64[1]; |
| 180 | t->client_address.as_u64[0] = ip6->dst_address.as_u64[0]; |
| 181 | t->client_address.as_u64[1] = ip6->dst_address.as_u64[1]; |
| 182 | t->session_index = session_index; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 183 | } |
| 184 | |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 185 | return next_index; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | #include <vnet/pipeline.h> |
| 189 | |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 190 | uword |
| 191 | l2t_encap_node_fn (vlib_main_t * vm, |
| 192 | vlib_node_runtime_t * node, vlib_frame_t * frame) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 193 | { |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 194 | return dispatch_pipeline (vm, node, frame); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 198 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 199 | VLIB_REGISTER_NODE (l2t_encap_node) = { |
| 200 | .function = l2t_encap_node_fn, |
| 201 | .name = "l2tp-encap", |
| 202 | .vector_size = sizeof (u32), |
| 203 | .format_trace = format_l2t_trace, |
| 204 | .type = VLIB_NODE_TYPE_INTERNAL, |
| 205 | .runtime_data_bytes = sizeof (l2tp_encap_runtime_t), |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 206 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 207 | .n_errors = ARRAY_LEN(l2t_encap_error_strings), |
| 208 | .error_strings = l2t_encap_error_strings, |
| 209 | |
| 210 | .n_next_nodes = L2T_ENCAP_N_NEXT, |
| 211 | |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 212 | /* add dispositions here */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 213 | .next_nodes = { |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 214 | [L2T_ENCAP_NEXT_IP6_LOOKUP] = "ip6-lookup", |
| 215 | [L2T_ENCAP_NEXT_DROP] = "error-drop", |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 216 | }, |
| 217 | }; |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 218 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 219 | |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 220 | VLIB_NODE_FUNCTION_MULTIARCH (l2t_encap_node, l2t_encap_node_fn); |
| 221 | void |
| 222 | l2tp_encap_init (vlib_main_t * vm) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 223 | { |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 224 | l2tp_encap_runtime_t *rt; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 225 | |
| 226 | rt = vlib_node_get_runtime_data (vm, l2t_encap_node.index); |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 227 | rt->vnet_main = vnet_get_main (); |
| 228 | rt->cached_sw_if_index = (u32) ~ 0; |
| 229 | rt->cached_session_index = (u32) ~ 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 230 | } |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 231 | |
| 232 | /* |
| 233 | * fd.io coding-style-patch-verification: ON |
| 234 | * |
| 235 | * Local Variables: |
| 236 | * eval: (c-set-style "gnu") |
| 237 | * End: |
| 238 | */ |