aboutsummaryrefslogtreecommitdiffstats
path: root/src/vnet/ipsec/ipsec_if_out.c
blob: 62ff67ac95196cacf7705f7a919dd9163809dc7a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*
 * ipsec_if_out.c : IPSec interface output node
 *
 * Copyright (c) 2015 Cisco and/or its affiliates.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <vnet/vnet.h>
#include <vnet/api_errno.h>
#include <vnet/ip/ip.h>

#include <vnet/ipsec/ipsec.h>

/* Statistics (not really errors) */
#define foreach_ipsec_if_output_error    \
_(TX, "good packets transmitted")

static char *ipsec_if_output_error_strings[] = {
#define _(sym,string) string,
  foreach_ipsec_if_output_error
#undef _
};

typedef enum
{
#define _(sym,str) IPSEC_IF_OUTPUT_ERROR_##sym,
  foreach_ipsec_if_output_error
#undef _
    IPSEC_IF_OUTPUT_N_ERROR,
} ipsec_if_output_error_t;


typedef struct
{
  u32 spi;
  u32 seq;
} ipsec_if_output_trace_t;

u8 *
format_ipsec_if_output_trace (u8 * s, va_list * args)
{
  CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
  CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
  ipsec_if_output_trace_t *t = va_arg (*args, ipsec_if_output_trace_t *);

  s = format (s, "IPSec: spi %u seq %u", t->spi, t->seq);
  return s;
}

static uword
ipsec_if_output_node_fn (vlib_main_t * vm, vlib_node_runtime_t * node,
			 vlib_frame_t * from_frame)
{
  ipsec_main_t *im = &ipsec_main;
  vnet_main_t *vnm = im->vnet_main;
  u32 *from, *to_next = 0, next_index;
  u32 n_left_from, sw_if_index0;

  from = vlib_frame_vector_args (from_frame);
  n_left_from = from_frame->n_vectors;
  next_index = node->cached_next_index;

  while (n_left_from > 0)
    {
      u32 n_left_to_next;

      vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);

      while (n_left_from > 0 && n_left_to_next > 0)
	{
	  u32 bi0, next0;
	  vlib_buffer_t *b0;
	  ipsec_tunnel_if_t *t0;
	  vnet_hw_interface_t *hi0;

	  bi0 = to_next[0] = from[0];
	  from += 1;
	  n_left_from -= 1;
	  to_next += 1;
	  n_left_to_next -= 1;
	  b0 = vlib_get_buffer (vm, bi0);
	  sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_TX];
	  hi0 = vnet_get_sup_hw_interface (vnm, sw_if_index0);
	  t0 = pool_elt_at_index (im->tunnel_interfaces, hi0->dev_instance);
	  vnet_buffer (b0)->ipsec.sad_index = t0->output_sa_index;
	  next0 = im->esp_encrypt_next_index;

	  if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
	    {
	      ipsec_if_output_trace_t *tr =
		vlib_add_trace (vm, node, b0, sizeof (*tr));
	      ipsec_sa_t *sa0 =
		pool_elt_at_index (im->sad, t0->output_sa_index);
	      tr->spi = sa0->spi;
	      tr->seq = sa0->seq;
	    }

	  vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
					   n_left_to_next, bi0, next0);
	}
      vlib_put_next_frame (vm, node, next_index, n_left_to_next);
    }

  vlib_node_increment_counter (vm, ipsec_if_output_node.index,
			       IPSEC_IF_OUTPUT_ERROR_TX,
			       from_frame->n_vectors);

  return from_frame->n_vectors;
}

/* *INDENT-OFF* */
VLIB_REGISTER_NODE (ipsec_if_output_node) = {
  .function = ipsec_if_output_node_fn,
  .name = "ipsec-if-output",
  .vector_size = sizeof (u32),
  .format_trace = format_ipsec_if_output_trace,
  .type = VLIB_NODE_TYPE_INTERNAL,

  .n_errors = ARRAY_LEN(ipsec_if_output_error_strings),
  .error_strings = ipsec_if_output_error_strings,

  .sibling_of = "ipsec-output-ip4",
};
/* *INDENT-ON* */

VLIB_NODE_FUNCTION_MULTIARCH (ipsec_if_output_node, ipsec_if_output_node_fn)
/*
 * fd.io coding-style-patch-verification: ON
 *
 * Local Variables:
 * eval: (c-set-style "gnu")
 * End:
 */
="n">src_if.local_mac, src=src_if.remote_mac) / ip_l(src=src_ip, dst=dst_ip) / UDP(sport=1234, dport=1234) / Raw(payload)) info.data = p.copy() size = random.choice(packet_sizes) self.extend_packet(p, size) pkts.append(p) return pkts def verify_capture(self, rx_if, capture, ip_l=IP): """Verify captured input packet stream for defined interface. :param VppInterface rx_if: Interface to verify captured packet stream. :param list capture: Captured packet stream. :param Scapy ip_l: Required IP layer - IP or IPv6. (Default is IP.) """ self.logger.info("Verifying capture on interface %s" % rx_if.name) count = 0 host_counters = {} for host_mac in rx_if._hosts_by_mac: host_counters[host_mac] = 0 for packet in capture: try: ip_received = packet[ip_l] payload_info = self.payload_to_info(packet[Raw]) packet_index = payload_info.index ip_sent = self._packet_infos[packet_index].data[ip_l] self.logger.debug("Got packet on port %s: src=%u (id=%u)" % (rx_if.name, payload_info.src, packet_index)) # Check standard fields self.assertIn(packet.dst, rx_if._hosts_by_mac, "Destination MAC address %s shouldn't be routed " "via interface %s" % (packet.dst, rx_if.name)) self.assertEqual(packet.src, rx_if.local_mac) self.assertEqual(ip_received.src, ip_sent.src) self.assertEqual(ip_received.dst, ip_sent.dst) host_counters[packet.dst] += 1 self._packet_infos.pop(packet_index) except: self.logger.error(ppp("Unexpected or invalid packet:", packet)) raise # We expect packet routed via all host of pg interface for host_mac in host_counters: nr = host_counters[host_mac] self.assertNotEqual( nr, 0, "No packet routed via host %s" % host_mac) self.logger.info("%u packets routed via host %s of %s interface" % (nr, host_mac, rx_if.name)) count += nr self.logger.info("Total amount of %u packets routed via %s interface" % (count, rx_if.name)) return count def create_ip_routes(self, dst_ip_net, dst_prefix_len, is_ipv6=0): """ Create IP routes for defined destination IP network. :param str dst_ip_net: Destination IP network. :param int dst_prefix_len: IP address prefix length. :param int is_ipv6: 0 if an ip4 route, else ip6 """ paths = [] for pg_if in self.pg_interfaces[1:]: for nh_host in pg_if.remote_hosts: nh_host_ip = nh_host.ip4 if is_ipv6 == 0 else nh_host.ip6 paths.append(VppRoutePath(nh_host_ip, pg_if.sw_if_index)) rip = VppIpRoute(self, dst_ip_net, dst_prefix_len, paths) rip.add_vpp_config() self.logger.info("Route via %s on %s created" % (nh_host_ip, pg_if.name)) self.logger.debug(self.vapi.ppcli("show ip fib")) self.logger.debug(self.vapi.ppcli("show ip6 fib")) def test_ip_ecmp(self): """ IP equal-cost multi-path routing test """ src_ip_net = '16.0.0.1' dst_ip_net = '32.0.0.1' ip_prefix_len = 24 self.create_ip_routes(dst_ip_net, ip_prefix_len) pkts = self.create_stream(self.pg0, src_ip_net, dst_ip_net, ip_prefix_len, self.pg_if_packet_sizes) self.pg0.add_stream(pkts) self.pg_enable_capture(self.pg_interfaces) self.pg_start() # We expect packets on pg1, pg2 and pg3, but not on pg0 rx_count = 0 for pg_if in self.pg_interfaces[1:]: capture = pg_if._get_capture(timeout=1) self.assertNotEqual( len(capture), 0, msg="No packets captured on %s" % pg_if.name) rx_count += self.verify_capture(pg_if, capture) self.pg0.assert_nothing_captured(remark="IP packets forwarded on pg0") # Check that all packets were forwarded via pg1, pg2 and pg3 self.assertEqual(rx_count, len(pkts)) def test_ip6_ecmp(self): """ IPv6 equal-cost multi-path routing test """ src_ip_net = '3ffe:51::1' dst_ip_net = '3ffe:71::1' ip_prefix_len = 64 self.create_ip_routes(dst_ip_net, ip_prefix_len, is_ipv6=1) pkts = self.create_stream( self.pg0, src_ip_net, dst_ip_net, ip_prefix_len, self.pg_if_packet_sizes, ip_l=IPv6) self.pg0.add_stream(pkts) self.pg_enable_capture(self.pg_interfaces) self.pg_start() # We expect packets on pg1, pg2 and pg3, but not on pg0 rx_count = 0 for pg_if in self.pg_interfaces[1:]: capture = pg_if._get_capture(timeout=1) self.assertNotEqual( len(capture), 0, msg="No packets captured on %s" % pg_if.name) rx_count += self.verify_capture(pg_if, capture, ip_l=IPv6) self.pg0.assert_nothing_captured(remark="IP packets forwarded on pg0") # Check that all packets were forwarded via pg1, pg2 and pg3 self.assertEqual(rx_count, len(pkts)) if __name__ == '__main__': unittest.main(testRunner=VppTestRunner)