2 * Copyright (c) 2015 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
16 #include <vnet/ipsec/ipsec.h>
20 * Policy packet & bytes counters
22 vlib_combined_counter_main_t ipsec_spd_policy_counters = {
24 .stat_segment_name = "/net/ipsec/policy",
28 ipsec_policy_is_equal (ipsec_policy_t * p1, ipsec_policy_t * p2)
30 if (p1->priority != p2->priority)
32 if (p1->is_outbound != p2->is_outbound)
34 if (p1->policy != p2->policy)
36 if (p1->sa_id != p2->sa_id)
38 if (p1->protocol != p2->protocol)
40 if (p1->lport.start != p2->lport.start)
42 if (p1->lport.stop != p2->lport.stop)
44 if (p1->rport.start != p2->rport.start)
46 if (p1->rport.stop != p2->rport.stop)
48 if (p1->is_ipv6 != p2->is_ipv6)
52 if (p1->laddr.start.ip6.as_u64[0] != p2->laddr.start.ip6.as_u64[0])
54 if (p1->laddr.start.ip6.as_u64[1] != p2->laddr.start.ip6.as_u64[1])
56 if (p1->laddr.stop.ip6.as_u64[0] != p2->laddr.stop.ip6.as_u64[0])
58 if (p1->laddr.stop.ip6.as_u64[1] != p2->laddr.stop.ip6.as_u64[1])
60 if (p1->raddr.start.ip6.as_u64[0] != p2->raddr.start.ip6.as_u64[0])
62 if (p1->raddr.start.ip6.as_u64[1] != p2->raddr.start.ip6.as_u64[1])
64 if (p1->raddr.stop.ip6.as_u64[0] != p2->raddr.stop.ip6.as_u64[0])
66 if (p1->laddr.stop.ip6.as_u64[1] != p2->laddr.stop.ip6.as_u64[1])
71 if (p1->laddr.start.ip4.as_u32 != p2->laddr.start.ip4.as_u32)
73 if (p1->laddr.stop.ip4.as_u32 != p2->laddr.stop.ip4.as_u32)
75 if (p1->raddr.start.ip4.as_u32 != p2->raddr.start.ip4.as_u32)
77 if (p1->raddr.stop.ip4.as_u32 != p2->raddr.stop.ip4.as_u32)
84 ipsec_spd_entry_sort (void *a1, void *a2)
86 ipsec_main_t *im = &ipsec_main;
89 ipsec_policy_t *p1, *p2;
91 p1 = pool_elt_at_index (im->policies, *id1);
92 p2 = pool_elt_at_index (im->policies, *id2);
94 return p2->priority - p1->priority;
100 ipsec_add_del_policy (vlib_main_t * vm,
101 ipsec_policy_t * policy, int is_add, u32 * stat_index)
103 ipsec_main_t *im = &ipsec_main;
104 ipsec_spd_t *spd = 0;
109 clib_warning ("policy-id %u priority %d is_outbound %u", policy->id,
110 policy->priority, policy->is_outbound);
112 if (policy->policy == IPSEC_POLICY_ACTION_PROTECT)
114 p = hash_get (im->sa_index_by_sa_id, policy->sa_id);
116 return VNET_API_ERROR_SYSCALL_ERROR_1;
117 policy->sa_index = p[0];
120 p = hash_get (im->spd_index_by_spd_id, policy->id);
123 return VNET_API_ERROR_SYSCALL_ERROR_1;
126 spd = pool_elt_at_index (im->spds, spd_index);
128 return VNET_API_ERROR_SYSCALL_ERROR_1;
134 pool_get (im->policies, vp);
135 clib_memcpy (vp, policy, sizeof (*vp));
136 policy_index = vp - im->policies;
138 vlib_validate_combined_counter (&ipsec_spd_policy_counters,
140 vlib_zero_combined_counter (&ipsec_spd_policy_counters, policy_index);
142 if (policy->is_outbound)
146 vec_add1 (spd->policies[IPSEC_SPD_POLICY_IP6_OUTBOUND],
148 vec_sort_with_function (spd->policies
149 [IPSEC_SPD_POLICY_IP6_OUTBOUND],
150 ipsec_spd_entry_sort);
154 vec_add1 (spd->policies[IPSEC_SPD_POLICY_IP4_OUTBOUND],
156 vec_sort_with_function (spd->policies
157 [IPSEC_SPD_POLICY_IP4_OUTBOUND],
158 ipsec_spd_entry_sort);
165 if (policy->policy == IPSEC_POLICY_ACTION_PROTECT)
167 vec_add1 (spd->policies
168 [IPSEC_SPD_POLICY_IP6_INBOUND_PROTECT],
170 vec_sort_with_function (spd->policies
171 [IPSEC_SPD_POLICY_IP6_INBOUND_PROTECT],
172 ipsec_spd_entry_sort);
177 (spd->policies[IPSEC_SPD_POLICY_IP6_INBOUND_BYPASS],
179 vec_sort_with_function
180 (spd->policies[IPSEC_SPD_POLICY_IP6_INBOUND_BYPASS],
181 ipsec_spd_entry_sort);
186 if (policy->policy == IPSEC_POLICY_ACTION_PROTECT)
188 vec_add1 (spd->policies
189 [IPSEC_SPD_POLICY_IP4_INBOUND_PROTECT],
191 vec_sort_with_function (spd->policies
192 [IPSEC_SPD_POLICY_IP4_INBOUND_PROTECT],
193 ipsec_spd_entry_sort);
198 (spd->policies[IPSEC_SPD_POLICY_IP4_INBOUND_BYPASS],
200 vec_sort_with_function
201 (spd->policies[IPSEC_SPD_POLICY_IP4_INBOUND_BYPASS],
202 ipsec_spd_entry_sort);
206 *stat_index = policy_index;
210 ipsec_spd_policy_t ptype;
213 FOR_EACH_IPSEC_SPD_POLICY_TYPE (ptype)
215 vec_foreach_index (ii, (spd->policies[ptype]))
217 vp = pool_elt_at_index (im->policies, spd->policies[ptype][ii]);
218 if (ipsec_policy_is_equal (vp, policy))
220 vec_del1 (spd->policies[ptype], ii);
221 pool_put (im->policies, vp);
233 * fd.io coding-style-patch-verification: ON
236 * eval: (c-set-style "gnu")