Sundarajan Srinivasan | 1b03fe2 | 2014-12-02 13:20:56 -0800 | [diff] [blame] | 1 | /* |
| 2 | ************************************************************************** |
Tushar Mathur | d09246f | 2015-02-24 15:53:16 +0530 | [diff] [blame] | 3 | * Copyright (c) 2014,2015, The Linux Foundation. All rights reserved. |
Sundarajan Srinivasan | 1b03fe2 | 2014-12-02 13:20:56 -0800 | [diff] [blame] | 4 | * Permission to use, copy, modify, and/or distribute this software for |
| 5 | * any purpose with or without fee is hereby granted, provided that the |
| 6 | * above copyright notice and this permission notice appear in all copies. |
| 7 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 8 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 9 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
| 10 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 11 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 12 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT |
| 13 | * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 14 | ************************************************************************** |
| 15 | */ |
| 16 | |
| 17 | /* nss_ipsecmgr.c |
| 18 | * NSS to HLOS IPSec Manager |
| 19 | */ |
| 20 | #include <linux/types.h> |
| 21 | #include <linux/ip.h> |
Samarjeet Banerjee | 07c392c | 2015-09-18 21:26:12 +0530 | [diff] [blame] | 22 | #include <linux/ipv6.h> |
Sundarajan Srinivasan | 1b03fe2 | 2014-12-02 13:20:56 -0800 | [diff] [blame] | 23 | #include <linux/skbuff.h> |
| 24 | #include <linux/module.h> |
| 25 | #include <linux/netdevice.h> |
| 26 | #include <linux/rtnetlink.h> |
| 27 | #include <asm/atomic.h> |
| 28 | #include <nss_api_if.h> |
| 29 | #include <nss_ipsec.h> |
Amit Gupta | 2c5439c | 2015-08-21 20:52:02 +0530 | [diff] [blame] | 30 | #include <nss_ipsecmgr.h> |
Sundarajan Srinivasan | 1b03fe2 | 2014-12-02 13:20:56 -0800 | [diff] [blame] | 31 | |
| 32 | #if defined(CONFIG_DYNAMIC_DEBUG) |
| 33 | /* |
| 34 | * Compile messages for dynamic enable/disable |
| 35 | */ |
| 36 | #define nss_ipsecmgr_error(s, ...) pr_debug("%s[%d]:" s, __FUNCTION__, __LINE__, ##__VA_ARGS__) |
| 37 | #define nss_ipsecmgr_warn(s, ...) pr_debug("%s[%d]:" s, __FUNCTION__, __LINE__, ##__VA_ARGS__) |
| 38 | #define nss_ipsecmgr_info(s, ...) pr_debug("%s[%d]:" s, __FUNCTION__, __LINE__, ##__VA_ARGS__) |
| 39 | #define nss_ipsecmgr_trace(s, ...) pr_debug("%s[%d]:" s, __FUNCTION__, __LINE__, ##__VA_ARGS__) |
| 40 | |
| 41 | #else |
| 42 | /* |
| 43 | * Statically compile messages at different levels |
| 44 | */ |
| 45 | #define nss_ipsecmgr_error(s, ...) { \ |
| 46 | if (NSS_IPSECMGR_DEBUG_LEVEL < NSS_IPSECMGR_DEBUG_LVL_ERROR) { \ |
| 47 | pr_alert("%s[%d]:" s, __FUNCTION__, __LINE__, ##__VA_ARGS__); \ |
| 48 | } \ |
| 49 | } |
| 50 | #define nss_ipsecmgr_warn(s, ...) { \ |
| 51 | if (NSS_IPSECMGR_DEBUG_LEVEL < NSS_IPSECMGR_DEBUG_LVL_WARN) { \ |
| 52 | pr_warn("%s[%d]:" s, __FUNCTION__, __LINE__, ##__VA_ARGS__); \ |
| 53 | } \ |
| 54 | } |
| 55 | #define nss_ipsecmgr_info(s, ...) { \ |
| 56 | if (NSS_IPSECMGR_DEBUG_LEVEL < NSS_IPSECMGR_DEBUG_LVL_INFO) { \ |
| 57 | pr_notice("%s[%d]:" s, __FUNCTION__, __LINE__, ##__VA_ARGS__); \ |
| 58 | } \ |
| 59 | } |
| 60 | #define nss_ipsecmgr_trace(s, ...) { \ |
| 61 | if (NSS_IPSECMGR_DEBUG_LEVEL < NSS_IPSECMGR_DEBUG_LVL_TRACE) { \ |
| 62 | pr_info("%s[%d]:" s, __FUNCTION__, __LINE__, ##__VA_ARGS__); \ |
| 63 | } \ |
| 64 | } |
| 65 | |
| 66 | #endif /* !CONFIG_DYNAMIC_DEBUG */ |
| 67 | |
| 68 | /* NSS IPsec entry state */ |
| 69 | enum nss_ipsecmgr_entry_state { |
| 70 | NSS_IPSECMGR_ENTRY_STATE_INIT = 0, /* init state of the entry */ |
| 71 | NSS_IPSECMGR_ENTRY_STATE_VALID = 1, /* entry is valid */ |
| 72 | NSS_IPSECMGR_ENTRY_STATE_INVALID = 2, /* entry is invalid */ |
| 73 | NSS_IPSECMGR_ENTRY_STATE_MAX |
| 74 | }; |
| 75 | |
| 76 | /* IPsec table entry */ |
| 77 | struct nss_ipsecmgr_tbl_entry { |
| 78 | struct nss_ipsec_rule_sel sel; /* rule selector */ |
| 79 | uint32_t sa_idx; /* index into SA table for this rule */ |
| 80 | enum nss_ipsecmgr_entry_state state; /* state */ |
| 81 | }; |
| 82 | |
| 83 | /* NSS IPsec table type */ |
| 84 | struct nss_ipsecmgr_tbl { |
| 85 | uint32_t total_tx; /* total packets tx'ed from ENCAP/DECAP */ |
| 86 | uint32_t total_rx; /* total packets rx'ed at ENCAP/DECAP */ |
| 87 | uint32_t total_dropped; /* total dropped packets at ENCAP/DECAP */ |
| 88 | struct nss_ipsecmgr_tbl_entry entry[NSS_IPSEC_MAX_RULES]; /* table entry */ |
| 89 | uint32_t count; /* number of entries */ |
| 90 | spinlock_t lock; |
| 91 | }; |
| 92 | |
| 93 | /* NSS IPsec SA */ |
| 94 | struct nss_ipsec_sa { |
| 95 | atomic_t user; /* reference count */ |
| 96 | uint32_t esp_spi; /* ESP SPI */ |
| 97 | |
| 98 | enum nss_ipsecmgr_rule_type type; /* IPsec rule type (ENCAP/DECAP) */ |
| 99 | |
| 100 | struct nss_ipsec_rule_data data; /* IPsec rule data */ |
| 101 | struct nss_ipsec_sa_stats stats; /* SA statistics */ |
| 102 | }; |
| 103 | |
| 104 | /* NSS IPsec manager private structure */ |
| 105 | struct nss_ipsecmgr_priv { |
| 106 | struct nss_ipsecmgr_tbl encap; /* encap table */ |
| 107 | struct nss_ipsecmgr_tbl decap; /* decap table */ |
| 108 | struct nss_ipsec_sa sa_tbl[NSS_IPSEC_MAX_SA]; /* global SA table */ |
| 109 | |
| 110 | void *cb_ctx; /* callback context */ |
| 111 | nss_ipsecmgr_data_cb_t data_cb; /* data callback function */ |
| 112 | nss_ipsecmgr_event_cb_t event_cb; /* event callback function */ |
| 113 | struct nss_ctx_instance *nss_ctx; /* NSS context */ |
| 114 | uint32_t nss_ifnum; /* NSS if num for data */ |
| 115 | }; |
| 116 | |
| 117 | typedef bool (*nss_ipsecmgr_op_t)(struct net_device *dev, struct nss_ipsecmgr_tbl *tbl, struct nss_ipsec_msg *nim); |
| 118 | |
| 119 | /* NSS IPsec manager operation (message types) complete routines */ |
| 120 | struct nss_ipsecmgr_ops { |
| 121 | nss_ipsecmgr_op_t verify_fn; /* verify function for the op */ |
| 122 | nss_ipsecmgr_op_t commit_fn; /* commit function for the op */ |
| 123 | }; |
| 124 | |
| 125 | /* driver global info */ |
| 126 | struct nss_ipsecmgr_drv { |
| 127 | atomic_t sa_count; /* number of SA's available for */ |
| 128 | }; |
| 129 | |
| 130 | /* |
| 131 | ********************** |
| 132 | * globals |
| 133 | ********************** |
| 134 | */ |
| 135 | |
| 136 | static struct nss_ipsecmgr_drv gbl_drv_ctx; /* global driver context */ |
| 137 | |
| 138 | /* |
| 139 | ********************** |
| 140 | * Helper Functions |
| 141 | ********************** |
| 142 | */ |
| 143 | |
| 144 | /* |
| 145 | * nss_ipsecmgr_sa_init() |
| 146 | * Initialize SA |
| 147 | */ |
| 148 | static void nss_ipsecmgr_sa_init(struct nss_ipsec_sa *sa) |
| 149 | { |
| 150 | memset(sa, 0, sizeof(struct nss_ipsec_sa)); |
| 151 | atomic_set(&sa->user, 0); |
| 152 | } |
| 153 | |
| 154 | /* |
| 155 | * nss_ipsecmgr_sa_get() |
| 156 | * Get reference to SA at given index |
| 157 | */ |
| 158 | static struct nss_ipsec_sa *nss_ipsecmgr_sa_get(struct nss_ipsec_sa sa_tbl[], uint32_t sa_idx) |
| 159 | { |
| 160 | struct nss_ipsec_sa *sa = &sa_tbl[sa_idx]; |
| 161 | |
| 162 | atomic_inc(&sa->user); |
| 163 | |
| 164 | return sa; |
| 165 | } |
| 166 | |
| 167 | /* |
| 168 | * nss_ipsecmgr_sa_put() |
| 169 | * Release reference to SA at given index |
| 170 | */ |
| 171 | static void nss_ipsecmgr_sa_put(struct nss_ipsec_sa sa_tbl[], uint32_t sa_idx) |
| 172 | { |
| 173 | struct nss_ipsec_sa *sa = &sa_tbl[sa_idx]; |
| 174 | |
| 175 | if (atomic_read(&sa->user) == 0) { |
| 176 | nss_ipsecmgr_error("SA already freed for (%d)\n", sa_idx); |
| 177 | return; |
| 178 | } |
| 179 | |
| 180 | if (atomic_dec_and_test(&sa->user)) { |
| 181 | nss_ipsecmgr_sa_init(sa); |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | /* |
| 186 | * nss_ipsecmgr_get_type() |
| 187 | * Get IPsec manager rule type (encap/decap) |
| 188 | */ |
| 189 | static enum nss_ipsecmgr_rule_type nss_ipsecmgr_get_type(uint16_t if_num) |
| 190 | { |
| 191 | switch(if_num) { |
| 192 | case NSS_IPSEC_ENCAP_IF_NUMBER: |
| 193 | return NSS_IPSECMGR_RULE_TYPE_ENCAP; |
| 194 | |
| 195 | case NSS_IPSEC_DECAP_IF_NUMBER: |
| 196 | return NSS_IPSECMGR_RULE_TYPE_DECAP; |
| 197 | |
| 198 | default: |
| 199 | return NSS_IPSECMGR_RULE_TYPE_NONE; |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | /* |
| 204 | * nss_ipsecmgr_get_tbl() |
| 205 | * return the table associated with the interface number |
| 206 | */ |
| 207 | static inline struct nss_ipsecmgr_tbl *nss_ipsecmgr_get_tbl(struct nss_ipsecmgr_priv *priv, uint16_t if_num) |
| 208 | { |
| 209 | switch (if_num) { |
| 210 | case NSS_IPSEC_ENCAP_IF_NUMBER: |
| 211 | return &priv->encap; |
| 212 | |
| 213 | case NSS_IPSEC_DECAP_IF_NUMBER: |
| 214 | return &priv->decap; |
| 215 | |
| 216 | default: |
| 217 | return NULL; |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | /* |
| 222 | * nss_ipsecmgr_copy_encap_add() |
| 223 | * prepare the IPsec message for encap add operation |
| 224 | */ |
| 225 | static void nss_ipsecmgr_copy_encap_add(struct net_device *dev, struct nss_ipsec_rule *msg, union nss_ipsecmgr_rule *rule) |
| 226 | { |
| 227 | struct nss_ipsecmgr_encap_add *encap = &rule->encap_add; |
| 228 | struct nss_ipsec_rule_sel *sel = &msg->sel; |
| 229 | struct nss_ipsec_rule_oip *oip = &msg->oip; |
| 230 | struct nss_ipsec_rule_data *data = &msg->data; |
| 231 | |
| 232 | /* |
| 233 | * Populate the selectors for encap direction |
| 234 | */ |
| 235 | sel->ipv4_src = encap->inner_ipv4_src; |
| 236 | sel->ipv4_dst = encap->inner_ipv4_dst; |
| 237 | |
| 238 | sel->src_port = encap->inner_src_port; |
| 239 | sel->dst_port = encap->inner_dst_port; |
| 240 | |
| 241 | sel->ipv4_proto = encap->inner_ipv4_proto; |
| 242 | |
| 243 | /* |
| 244 | * Populate the outer IP data for encap direction |
| 245 | */ |
| 246 | oip->ipv4_dst = encap->outer_ipv4_dst; |
| 247 | oip->ipv4_src = encap->outer_ipv4_src; |
| 248 | |
| 249 | oip->esp_spi = encap->esp_spi; |
| 250 | |
| 251 | oip->ipv4_ttl = encap->outer_ipv4_ttl; |
| 252 | |
| 253 | /* |
| 254 | * Populate the data part |
| 255 | */ |
| 256 | data->cipher_algo = encap->cipher_algo; |
| 257 | data->esp_seq_skip = (encap->esp_seq_skip == 1); |
| 258 | data->esp_tail_skip = (encap->esp_tail_skip == 1); |
| 259 | |
| 260 | data->esp_icv_len = encap->esp_icv_len; |
| 261 | data->auth_algo = encap->auth_algo; |
| 262 | |
| 263 | data->crypto_index = encap->crypto_index; |
| 264 | |
| 265 | data->nat_t_req = encap->nat_t_req; |
| 266 | data->use_pattern = encap->use_pattern; |
| 267 | } |
| 268 | |
| 269 | /* |
| 270 | * nss_ipsecmgr_copy_decap_add() |
| 271 | * prepare the IPsec message for decap add operation |
| 272 | */ |
| 273 | static void nss_ipsecmgr_copy_decap_add(struct net_device *dev, struct nss_ipsec_rule *msg, union nss_ipsecmgr_rule *rule) |
| 274 | { |
| 275 | struct nss_ipsecmgr_decap_add *decap = &rule->decap_add; |
| 276 | struct nss_ipsec_rule_sel *sel = &msg->sel; |
| 277 | struct nss_ipsec_rule_data *data = &msg->data; |
| 278 | /* |
| 279 | * Populate the selectors for encap direction |
| 280 | */ |
| 281 | sel->ipv4_src = decap->outer_ipv4_src; |
| 282 | sel->ipv4_dst = decap->outer_ipv4_dst; |
| 283 | |
| 284 | sel->esp_spi = decap->esp_spi; |
| 285 | |
| 286 | sel->ipv4_proto = IPPROTO_ESP; |
| 287 | |
| 288 | /* |
| 289 | * Populate the data part |
| 290 | */ |
| 291 | data->cipher_algo = decap->cipher_algo; |
| 292 | |
| 293 | data->esp_icv_len = decap->esp_icv_len; |
| 294 | data->esp_seq_skip = (decap->esp_seq_skip == 1); |
| 295 | data->esp_tail_skip = (decap->esp_tail_skip == 1); |
| 296 | data->auth_algo = decap->auth_algo; |
| 297 | |
| 298 | data->crypto_index = decap->crypto_index; |
| 299 | |
| 300 | data->nat_t_req = decap->nat_t_req; |
| 301 | if (data->nat_t_req) { |
| 302 | sel->dst_port = NSS_IPSECMGR_NATT_PORT_DATA; |
| 303 | } |
| 304 | |
| 305 | data->window_size = decap->window_size; |
| 306 | } |
| 307 | |
| 308 | /* |
| 309 | * nss_ipsecmgr_copy_encap_del() |
| 310 | * prepare the IPsec message for encap del operation |
| 311 | */ |
| 312 | static void nss_ipsecmgr_copy_encap_del(struct net_device *dev, struct nss_ipsec_rule *msg, union nss_ipsecmgr_rule *rule) |
| 313 | { |
| 314 | struct nss_ipsecmgr_encap_del *encap = &rule->encap_del; |
| 315 | struct nss_ipsec_rule_sel *sel = &msg->sel; |
Tushar Mathur | d09246f | 2015-02-24 15:53:16 +0530 | [diff] [blame] | 316 | struct nss_ipsec_rule_data *data = &msg->data; |
Sundarajan Srinivasan | 1b03fe2 | 2014-12-02 13:20:56 -0800 | [diff] [blame] | 317 | |
| 318 | /* |
| 319 | * Populate the selectors for encap direction |
| 320 | */ |
| 321 | sel->ipv4_src = encap->inner_ipv4_src; |
| 322 | sel->ipv4_dst = encap->inner_ipv4_dst; |
| 323 | |
| 324 | sel->src_port = encap->inner_src_port; |
| 325 | sel->dst_port = encap->inner_dst_port; |
| 326 | |
| 327 | sel->ipv4_proto = encap->inner_ipv4_proto; |
Tushar Mathur | d09246f | 2015-02-24 15:53:16 +0530 | [diff] [blame] | 328 | |
| 329 | /* |
| 330 | * Populate the data part |
| 331 | */ |
| 332 | data->use_pattern = encap->use_pattern; |
Sundarajan Srinivasan | 1b03fe2 | 2014-12-02 13:20:56 -0800 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | /* |
| 336 | * nss_ipsecmgr_copy_decap_del() |
| 337 | * prepare the IPsec message for decap del operation |
| 338 | */ |
| 339 | static void nss_ipsecmgr_copy_decap_del(struct net_device *dev, struct nss_ipsec_rule *msg, union nss_ipsecmgr_rule *rule) |
| 340 | { |
| 341 | struct nss_ipsecmgr_decap_del *decap = &rule->decap_del; |
| 342 | struct nss_ipsec_rule_sel *sel = &msg->sel; |
| 343 | /* |
| 344 | * Populate the selectors for encap direction |
| 345 | */ |
| 346 | sel->ipv4_src = decap->outer_ipv4_src; |
| 347 | sel->ipv4_dst = decap->outer_ipv4_dst; |
| 348 | |
| 349 | sel->esp_spi = decap->esp_spi; |
| 350 | |
| 351 | sel->ipv4_proto = IPPROTO_ESP; |
| 352 | } |
| 353 | |
| 354 | /* |
| 355 | ********************** |
| 356 | * message handlers |
| 357 | ********************** |
| 358 | */ |
| 359 | /* |
| 360 | * nss_ipsecmgr_verify_add() |
| 361 | * verify the Add operation before committing |
| 362 | */ |
| 363 | static bool nss_ipsecmgr_verify_add(struct net_device *dev, struct nss_ipsecmgr_tbl *tbl, struct nss_ipsec_msg *nim) |
| 364 | { |
| 365 | struct nss_ipsec_rule *rule = &nim->msg.push; |
| 366 | struct nss_ipsecmgr_tbl_entry *entry; |
| 367 | uint32_t tbl_idx; |
Tushar Mathur | 26e87f1 | 2015-02-09 23:20:10 +0530 | [diff] [blame] | 368 | uint32_t sa_idx; |
| 369 | |
| 370 | if (nim->cm.response != NSS_CMN_RESPONSE_ACK) { |
| 371 | return false; |
| 372 | } |
Sundarajan Srinivasan | 1b03fe2 | 2014-12-02 13:20:56 -0800 | [diff] [blame] | 373 | |
| 374 | tbl_idx = rule->rule_idx; |
| 375 | if (tbl_idx >= NSS_IPSEC_MAX_RULES) { |
| 376 | nss_ipsecmgr_error("table index out of range\n"); |
| 377 | return false; |
| 378 | } |
| 379 | |
| 380 | entry = &tbl->entry[tbl_idx]; |
| 381 | |
| 382 | /* |
| 383 | * XXX:Duplicate hit or hash collision; We need to handle |
| 384 | * hash collision |
| 385 | */ |
| 386 | if (entry->state == NSS_IPSECMGR_ENTRY_STATE_VALID) { |
| 387 | return false; |
| 388 | } |
| 389 | |
| 390 | /* |
| 391 | * Table full, XXX:must increment stats |
| 392 | */ |
| 393 | if ((tbl->count + 1) >= NSS_IPSEC_MAX_RULES) { |
| 394 | return false; |
| 395 | } |
| 396 | |
Tushar Mathur | 26e87f1 | 2015-02-09 23:20:10 +0530 | [diff] [blame] | 397 | sa_idx = rule->sa_idx; |
| 398 | if (sa_idx >= NSS_IPSEC_MAX_SA) { |
| 399 | nss_ipsecmgr_error("unable to add the sa_idx:%d\n", sa_idx); |
| 400 | return false; |
| 401 | } |
| 402 | |
Sundarajan Srinivasan | 1b03fe2 | 2014-12-02 13:20:56 -0800 | [diff] [blame] | 403 | return true; |
| 404 | } |
| 405 | |
| 406 | /* |
| 407 | * nss_ipsecmgr_commit_add() |
| 408 | * commit the Add operation |
| 409 | */ |
| 410 | static bool nss_ipsecmgr_commit_add(struct net_device *dev, struct nss_ipsecmgr_tbl *tbl, struct nss_ipsec_msg *nim) |
| 411 | { |
| 412 | struct nss_ipsec_rule *rule = &nim->msg.push; |
| 413 | struct nss_ipsecmgr_tbl_entry *entry; |
| 414 | struct nss_ipsecmgr_priv *priv; |
| 415 | struct nss_ipsec_sa *sa; |
| 416 | uint32_t tbl_idx; |
| 417 | uint32_t sa_idx; |
| 418 | |
| 419 | priv = netdev_priv(dev); |
| 420 | |
| 421 | /* Reduce the number of availabe SA(s) as we have successfully added one */ |
| 422 | atomic_dec(&gbl_drv_ctx.sa_count); |
| 423 | |
| 424 | tbl_idx = rule->rule_idx; |
| 425 | entry = &tbl->entry[tbl_idx]; |
| 426 | |
| 427 | tbl->count++; |
| 428 | |
| 429 | memcpy(&entry->sel, &rule->sel, sizeof(struct nss_ipsec_rule_sel)); |
| 430 | entry->state = NSS_IPSECMGR_ENTRY_STATE_VALID; |
| 431 | |
| 432 | /* increment SA use count */ |
| 433 | sa_idx = rule->sa_idx; |
| 434 | sa = nss_ipsecmgr_sa_get(priv->sa_tbl, sa_idx); |
| 435 | |
| 436 | /* copy rule data to SA */ |
| 437 | memcpy(&sa->data, &rule->data, sizeof(struct nss_ipsec_rule_data)); |
| 438 | |
| 439 | /* save rule type (encap/decap) */ |
| 440 | sa->type = nss_ipsecmgr_get_type(nim->cm.interface); |
| 441 | |
| 442 | /* save ESP SPI in SA */ |
| 443 | switch (nim->cm.interface) { |
| 444 | case NSS_IPSEC_ENCAP_IF_NUMBER: |
| 445 | sa->esp_spi = rule->oip.esp_spi; |
| 446 | break; |
| 447 | |
| 448 | case NSS_IPSEC_DECAP_IF_NUMBER: |
| 449 | sa->esp_spi = rule->sel.esp_spi; |
| 450 | break; |
| 451 | |
| 452 | default: |
| 453 | nss_ipsecmgr_error("invalid interface num = %d\n", nim->cm.interface); |
| 454 | return false; |
| 455 | } |
| 456 | |
| 457 | /* save SA table index for this rule entry */ |
| 458 | entry->sa_idx = sa_idx; |
| 459 | |
| 460 | return true; |
| 461 | } |
| 462 | |
| 463 | /* |
| 464 | * nss_ipsecmgr_verify_del() |
| 465 | * verify the del operation before committing |
| 466 | */ |
| 467 | static bool nss_ipsecmgr_verify_del(struct net_device *dev, struct nss_ipsecmgr_tbl *tbl, struct nss_ipsec_msg *nim) |
| 468 | { |
| 469 | struct nss_ipsec_rule *rule = &nim->msg.push; |
| 470 | struct nss_ipsecmgr_tbl_entry *entry; |
| 471 | uint32_t tbl_idx; |
Tushar Mathur | 26e87f1 | 2015-02-09 23:20:10 +0530 | [diff] [blame] | 472 | uint32_t sa_idx; |
| 473 | |
| 474 | if (nim->cm.response != NSS_CMN_RESPONSE_ACK) { |
| 475 | return false; |
| 476 | } |
Sundarajan Srinivasan | 1b03fe2 | 2014-12-02 13:20:56 -0800 | [diff] [blame] | 477 | |
| 478 | tbl_idx = rule->rule_idx; |
| 479 | if (tbl_idx >= NSS_IPSEC_MAX_RULES) { |
| 480 | nss_ipsecmgr_error("table index out of range\n"); |
| 481 | return false; |
| 482 | } |
| 483 | |
| 484 | entry = &tbl->entry[tbl_idx]; |
| 485 | |
| 486 | /* |
| 487 | * Entry already deleted, XXX:must increment stats |
| 488 | */ |
| 489 | if (entry->state == NSS_IPSECMGR_ENTRY_STATE_INVALID) { |
| 490 | return false; |
| 491 | } |
| 492 | |
| 493 | /* |
| 494 | * Entry never existed, XXX:must increment stats |
| 495 | */ |
| 496 | if (entry->state == NSS_IPSECMGR_ENTRY_STATE_INIT) { |
| 497 | return false; |
| 498 | } |
| 499 | |
| 500 | /* |
| 501 | * Table empty, XXX:must increment stats |
| 502 | */ |
| 503 | if (tbl->count == 0) { |
| 504 | return false; |
| 505 | } |
| 506 | |
Tushar Mathur | 26e87f1 | 2015-02-09 23:20:10 +0530 | [diff] [blame] | 507 | sa_idx = rule->sa_idx; |
| 508 | if (sa_idx >= NSS_IPSEC_MAX_SA) { |
| 509 | nss_ipsecmgr_error("unable to delete the sa_idx:%d\n", sa_idx); |
| 510 | return false; |
| 511 | } |
| 512 | |
| 513 | |
Sundarajan Srinivasan | 1b03fe2 | 2014-12-02 13:20:56 -0800 | [diff] [blame] | 514 | return true; |
| 515 | } |
| 516 | |
| 517 | /* |
| 518 | * nss_ipsecmgr_commit_del() |
| 519 | * commit the Del operation |
| 520 | */ |
| 521 | static bool nss_ipsecmgr_commit_del(struct net_device *dev, struct nss_ipsecmgr_tbl *tbl, struct nss_ipsec_msg *nim) |
| 522 | { |
| 523 | struct nss_ipsec_rule *rule = &nim->msg.push; |
| 524 | struct nss_ipsecmgr_tbl_entry *entry; |
| 525 | struct nss_ipsecmgr_priv *priv; |
| 526 | uint32_t tbl_idx; |
| 527 | uint32_t sa_idx; |
| 528 | |
| 529 | priv = netdev_priv(dev); |
| 530 | |
| 531 | /* Increase the number of available SA(s) as we have successfully freed one */ |
| 532 | atomic_inc(&gbl_drv_ctx.sa_count); |
| 533 | |
| 534 | tbl_idx = rule->rule_idx; |
| 535 | entry = &tbl->entry[tbl_idx]; |
| 536 | |
| 537 | tbl->count--; |
| 538 | |
| 539 | memset(&entry->sel, 0, sizeof(struct nss_ipsec_rule_sel)); |
| 540 | entry->state = NSS_IPSECMGR_ENTRY_STATE_INVALID; |
| 541 | |
| 542 | /* decrement SA use count */ |
| 543 | sa_idx = rule->sa_idx; |
| 544 | nss_ipsecmgr_sa_put(priv->sa_tbl, sa_idx); |
| 545 | |
| 546 | return true; |
| 547 | } |
| 548 | |
| 549 | /* |
| 550 | * nss_ipsecmgr_verify_stats() |
| 551 | * verify stats update operation |
| 552 | */ |
| 553 | static bool nss_ipsecmgr_verify_stats(struct net_device *dev, struct nss_ipsecmgr_tbl *tbl, struct nss_ipsec_msg *nim) |
| 554 | { |
Tushar Mathur | 26e87f1 | 2015-02-09 23:20:10 +0530 | [diff] [blame] | 555 | struct nss_ipsec_sa_stats *msg_stats = &nim->msg.stats; |
Sundarajan Srinivasan | 1b03fe2 | 2014-12-02 13:20:56 -0800 | [diff] [blame] | 556 | |
| 557 | /* |
| 558 | * Table empty, nothing to update |
| 559 | */ |
| 560 | if (tbl->count == 0) { |
| 561 | return false; |
| 562 | } |
| 563 | |
Tushar Mathur | 26e87f1 | 2015-02-09 23:20:10 +0530 | [diff] [blame] | 564 | if (msg_stats->sa_idx >= NSS_CRYPTO_MAX_IDXS) { |
Sundarajan Srinivasan | 1b03fe2 | 2014-12-02 13:20:56 -0800 | [diff] [blame] | 565 | return false; |
| 566 | } |
| 567 | |
| 568 | return true; |
| 569 | } |
| 570 | |
| 571 | /* |
| 572 | * nss_ipsecmgr_commit_stats() |
| 573 | * commit stats for the SA |
| 574 | */ |
| 575 | static bool nss_ipsecmgr_commit_stats(struct net_device *dev, struct nss_ipsecmgr_tbl *tbl, struct nss_ipsec_msg *nim) |
| 576 | { |
| 577 | struct nss_ipsec_sa_stats *msg_stats = &nim->msg.stats; |
| 578 | struct nss_ipsec_sa_stats *sa_stats; |
| 579 | struct nss_ipsec_sa *sa; |
| 580 | struct nss_ipsecmgr_priv *priv; |
| 581 | struct nss_ipsecmgr_event event = {.type = NSS_IPSECMGR_EVENT_SA_STATS}; |
| 582 | struct nss_ipsecmgr_sa_stats *event_stats = &event.data.stats; |
| 583 | nss_ipsecmgr_event_cb_t event_cb; |
| 584 | void *cb_ctx; |
| 585 | uint32_t sa_idx; |
| 586 | |
| 587 | priv = netdev_priv(dev); |
| 588 | |
| 589 | /* get reference to SA stats */ |
| 590 | sa_idx = msg_stats->sa_idx; |
| 591 | sa = nss_ipsecmgr_sa_get(priv->sa_tbl, sa_idx); |
| 592 | |
| 593 | sa_stats = &sa->stats; |
| 594 | |
| 595 | /* copy SA statistics */ |
| 596 | sa_stats->seqnum = msg_stats->seqnum; |
| 597 | memcpy(&sa_stats->pkts, &msg_stats->pkts, sizeof(struct nss_ipsec_pkt_stats)); |
| 598 | |
| 599 | /* execute event callback to send SA stats event */ |
| 600 | event_cb = priv->event_cb; |
| 601 | cb_ctx = priv->cb_ctx; |
| 602 | if (event_cb && cb_ctx) { |
| 603 | event_stats->esp_spi = sa->esp_spi; |
| 604 | event_stats->seqnum = sa_stats->seqnum; |
| 605 | event_stats->crypto_index = sa->data.crypto_index; |
| 606 | event_stats->type = sa->type; |
| 607 | |
| 608 | event_stats->pkts_processed = sa_stats->pkts.processed; |
| 609 | event_stats->pkts_dropped = sa_stats->pkts.dropped; |
| 610 | event_stats->pkts_failed = sa_stats->pkts.failed; |
| 611 | |
| 612 | event_cb(cb_ctx, &event); |
| 613 | } |
| 614 | |
| 615 | /* decrement SA use count */ |
| 616 | nss_ipsecmgr_sa_put(priv->sa_tbl, sa_idx); |
| 617 | |
| 618 | return true; |
| 619 | } |
| 620 | |
| 621 | /* |
| 622 | * nss_ipsecmgr_verify_flush() |
| 623 | * verify the flush operation |
| 624 | */ |
| 625 | static bool nss_ipsecmgr_verify_flush(struct net_device *dev, struct nss_ipsecmgr_tbl *tbl, struct nss_ipsec_msg *nim) |
| 626 | { |
| 627 | return true; |
| 628 | } |
| 629 | |
| 630 | /* |
| 631 | * nss_ipsecmgr_commit_flush() |
| 632 | * commit the flush operation |
| 633 | */ |
| 634 | static bool nss_ipsecmgr_commit_flush(struct net_device *dev, struct nss_ipsecmgr_tbl *tbl, struct nss_ipsec_msg *nim) |
| 635 | { |
| 636 | struct nss_ipsecmgr_priv *priv; |
| 637 | struct nss_ipsec_sa *sa; |
| 638 | int32_t count; |
| 639 | |
| 640 | priv = netdev_priv(dev); |
| 641 | |
Amit Gupta | 2c5439c | 2015-08-21 20:52:02 +0530 | [diff] [blame] | 642 | atomic_add(tbl->count, &gbl_drv_ctx.sa_count); |
Sundarajan Srinivasan | 1b03fe2 | 2014-12-02 13:20:56 -0800 | [diff] [blame] | 643 | tbl->count = 0; |
| 644 | |
| 645 | for (count = 0, sa = &priv->sa_tbl[0]; count < NSS_IPSEC_MAX_SA; count++, sa++) { |
| 646 | nss_ipsecmgr_sa_init(sa); |
| 647 | } |
| 648 | |
| 649 | return true; |
| 650 | } |
| 651 | |
| 652 | /* |
| 653 | * callback operation |
| 654 | */ |
| 655 | const static struct nss_ipsecmgr_ops cb_ops[NSS_IPSEC_MSG_TYPE_MAX] = { |
| 656 | [NSS_IPSEC_MSG_TYPE_ADD_RULE] = {nss_ipsecmgr_verify_add, nss_ipsecmgr_commit_add}, |
| 657 | [NSS_IPSEC_MSG_TYPE_DEL_RULE] = {nss_ipsecmgr_verify_del, nss_ipsecmgr_commit_del}, |
| 658 | [NSS_IPSEC_MSG_TYPE_SYNC_STATS] = {nss_ipsecmgr_verify_stats, nss_ipsecmgr_commit_stats}, |
| 659 | [NSS_IPSEC_MSG_TYPE_FLUSH_TUN] = {nss_ipsecmgr_verify_flush, nss_ipsecmgr_commit_flush} |
| 660 | }; |
| 661 | |
| 662 | /* |
| 663 | * nss_ipsecmgr_op_receive() |
| 664 | * asynchronous event reception |
| 665 | */ |
| 666 | static void nss_ipsecmgr_op_receive(void *app_data, struct nss_ipsec_msg *nim) |
| 667 | { |
| 668 | struct net_device *tun_dev = (struct net_device *)app_data; |
| 669 | struct nss_ipsecmgr_priv *priv; |
| 670 | struct nss_ipsecmgr_tbl *tbl; |
| 671 | nss_ipsecmgr_op_t verify_fn; |
| 672 | nss_ipsecmgr_op_t commit_fn; |
| 673 | struct net_device *dev; |
| 674 | |
| 675 | BUG_ON(tun_dev == NULL); |
| 676 | BUG_ON(nim == NULL); |
| 677 | |
| 678 | /* this holds the ref_cnt for the device */ |
| 679 | dev = dev_get_by_index(&init_net, nim->tunnel_id); |
| 680 | if (dev == NULL) { |
| 681 | nss_ipsecmgr_error("event received on deallocated I/F (%d)\n", nim->tunnel_id); |
| 682 | return; |
| 683 | } |
| 684 | |
| 685 | if (dev != tun_dev) { |
| 686 | nss_ipsecmgr_error("event received on incorrect I/F (%d)\n", nim->tunnel_id); |
| 687 | goto done; |
| 688 | } |
| 689 | |
| 690 | priv = netdev_priv(dev); |
| 691 | |
| 692 | tbl = nss_ipsecmgr_get_tbl(priv, nim->cm.interface); |
| 693 | if (tbl == NULL) { |
| 694 | nss_ipsecmgr_error("invalid interface number(%d)\n", nim->cm.interface); |
| 695 | goto done; |
| 696 | } |
| 697 | |
| 698 | verify_fn = cb_ops[nim->cm.type].verify_fn; |
| 699 | commit_fn = cb_ops[nim->cm.type].commit_fn; |
| 700 | |
| 701 | if (!verify_fn || !commit_fn) { |
| 702 | nss_ipsecmgr_error("unhandled type (%d)\n", nim->cm.type); |
| 703 | goto done; |
| 704 | } |
| 705 | |
| 706 | /* |
| 707 | * Now we can operate on the entry of the table |
| 708 | */ |
| 709 | spin_lock_bh(&tbl->lock); |
| 710 | |
| 711 | if (verify_fn(dev, tbl, nim) == true) { |
| 712 | commit_fn(dev, tbl, nim); |
| 713 | } |
| 714 | |
| 715 | spin_unlock_bh(&tbl->lock); |
| 716 | done: |
| 717 | /* release the device as we are done */ |
| 718 | dev_put(dev); |
| 719 | } |
| 720 | |
| 721 | /* |
| 722 | * nss_ipsecmgr_op_send() |
| 723 | * Push a IPsec rule to NSS |
| 724 | */ |
| 725 | static bool nss_ipsecmgr_op_send(struct net_device *dev, struct nss_ipsec_msg *nim, uint32_t if_num, enum nss_ipsec_msg_type type) |
| 726 | { |
| 727 | struct nss_ipsecmgr_priv *priv; |
| 728 | nss_tx_status_t status; |
| 729 | |
| 730 | priv = netdev_priv(dev); |
| 731 | if (priv->nss_ctx == NULL) { |
| 732 | nss_ipsecmgr_error("Tunnel registration or de-registration is underway\n"); |
| 733 | return false; |
| 734 | } |
| 735 | |
| 736 | nim->tunnel_id = dev->ifindex; |
| 737 | |
Sundarajan Srinivasan | 3933aae | 2015-01-30 10:44:26 -0800 | [diff] [blame] | 738 | nss_ipsec_msg_init(nim, if_num, type, NSS_IPSEC_MSG_LEN, nss_ipsecmgr_op_receive, dev); |
Sundarajan Srinivasan | 1b03fe2 | 2014-12-02 13:20:56 -0800 | [diff] [blame] | 739 | |
| 740 | status = nss_ipsec_tx_msg(priv->nss_ctx, nim); |
| 741 | if (status != NSS_TX_SUCCESS) { |
| 742 | nss_ipsecmgr_error("unable to push rule(%d) for %s\n", type, dev->name); |
| 743 | return false; |
| 744 | } |
| 745 | |
| 746 | return true; |
| 747 | } |
| 748 | |
| 749 | /* |
| 750 | * nss_ipsecmgr_buf_receive() |
| 751 | * receive NSS exception packets |
| 752 | */ |
| 753 | static void nss_ipsecmgr_buf_receive(struct net_device *dev, struct sk_buff *skb, __attribute((unused)) struct napi_struct *napi) |
| 754 | { |
| 755 | struct nss_ipsecmgr_priv *priv; |
| 756 | nss_ipsecmgr_data_cb_t cb_fn; |
| 757 | void *cb_ctx; |
| 758 | struct iphdr *ip; |
| 759 | |
| 760 | BUG_ON(dev == NULL); |
| 761 | BUG_ON(skb == NULL); |
| 762 | |
| 763 | /* hold the device till we process it */ |
| 764 | dev_hold(dev); |
| 765 | |
| 766 | /* |
| 767 | * XXX:need to ensure that the dev being accessed is not deleted |
| 768 | */ |
| 769 | priv = netdev_priv(dev); |
| 770 | |
| 771 | skb->dev = dev; |
| 772 | |
| 773 | cb_fn = priv->data_cb; |
| 774 | cb_ctx = priv->cb_ctx; |
| 775 | |
| 776 | /* |
| 777 | * if tunnel creator gave a callback then send the packet without |
| 778 | * any modifications to him |
| 779 | */ |
| 780 | if (cb_fn && cb_ctx) { |
| 781 | cb_fn(cb_ctx, skb); |
| 782 | goto done; |
| 783 | } |
| 784 | |
| 785 | ip = (struct iphdr *)skb->data; |
Amit Gupta | 2c5439c | 2015-08-21 20:52:02 +0530 | [diff] [blame] | 786 | if (unlikely((ip->version != IPVERSION) || (ip->ihl != 5))) { |
Sundarajan Srinivasan | 1b03fe2 | 2014-12-02 13:20:56 -0800 | [diff] [blame] | 787 | nss_ipsecmgr_error("dropping packets(IP version:%x, Header len:%x)\n", ip->version, ip->ihl); |
| 788 | dev_kfree_skb_any(skb); |
| 789 | goto done; |
| 790 | } |
| 791 | |
Amit Gupta | 2c5439c | 2015-08-21 20:52:02 +0530 | [diff] [blame] | 792 | /* |
| 793 | * Receiving an ESP packet indicates that NSS has performed the encapsulation |
| 794 | * but the post-routing rule is not present. This condition can't be taken care |
| 795 | * in Host we should flush the ENCAP rules and free the packet. This will force |
| 796 | * subsequent packets to follow the Slow path IPsec thus recreating the rules |
| 797 | */ |
| 798 | if (unlikely(ip->protocol == IPPROTO_ESP)) { |
| 799 | nss_ipsecmgr_sa_flush(dev, NSS_IPSECMGR_RULE_TYPE_ENCAP); |
| 800 | dev_kfree_skb_any(skb); |
| 801 | goto done; |
| 802 | } |
| 803 | |
Sundarajan Srinivasan | 1b03fe2 | 2014-12-02 13:20:56 -0800 | [diff] [blame] | 804 | skb_reset_network_header(skb); |
| 805 | skb_reset_mac_header(skb); |
| 806 | |
| 807 | skb->pkt_type = PACKET_HOST; |
| 808 | skb->protocol = cpu_to_be16(ETH_P_IP); |
| 809 | skb->skb_iif = dev->ifindex; |
| 810 | |
| 811 | netif_receive_skb(skb); |
| 812 | done: |
| 813 | /* release the device as we are done */ |
| 814 | dev_put(dev); |
| 815 | } |
| 816 | |
| 817 | /* |
| 818 | ********************** |
| 819 | * Netdev ops |
| 820 | ********************** |
| 821 | */ |
| 822 | |
| 823 | /* |
| 824 | * nss_ipsecmgr_tunnel_init() |
| 825 | * initiallizes the tunnel |
| 826 | */ |
| 827 | static int nss_ipsecmgr_tunnel_init(struct net_device *dev) |
| 828 | { |
| 829 | struct nss_ipsecmgr_priv *priv; |
| 830 | |
| 831 | priv = netdev_priv(dev); |
| 832 | |
| 833 | priv->nss_ctx = nss_ipsec_get_context(); |
| 834 | |
| 835 | return 0; |
| 836 | } |
| 837 | |
| 838 | /* |
| 839 | * nss_ipsecmgr_tunnel_exit() |
| 840 | * deinitiallizes the tunnel |
| 841 | */ |
| 842 | static void nss_ipsecmgr_tunnel_exit(struct net_device *dev) |
| 843 | { |
| 844 | struct nss_ipsecmgr_priv *priv; |
| 845 | |
| 846 | priv = netdev_priv(dev); |
| 847 | |
| 848 | priv->nss_ctx = NULL; |
| 849 | } |
| 850 | |
| 851 | /* |
| 852 | * nss_ipsecmgr_tunnel_open() |
| 853 | * open the tunnel for usage |
| 854 | */ |
| 855 | static int nss_ipsecmgr_tunnel_open(struct net_device *dev) |
| 856 | { |
| 857 | struct nss_ipsecmgr_priv *priv; |
| 858 | |
| 859 | priv = netdev_priv(dev); |
| 860 | |
| 861 | netif_start_queue(dev); |
| 862 | |
| 863 | return 0; |
| 864 | } |
| 865 | |
| 866 | /* |
| 867 | * nss_ipsecmgr_tunnel_stop() |
| 868 | * stop the IPsec tunnel |
| 869 | */ |
| 870 | static int nss_ipsecmgr_tunnel_stop(struct net_device *dev) |
| 871 | { |
| 872 | struct nss_ipsecmgr_priv *priv; |
| 873 | |
| 874 | priv = netdev_priv(dev); |
| 875 | |
| 876 | netif_stop_queue(dev); |
| 877 | |
| 878 | return 0; |
| 879 | } |
| 880 | |
| 881 | /* |
| 882 | * nss_ipsecmgr_tunnel_xmit() |
| 883 | * tunnel transmit function |
| 884 | */ |
| 885 | static netdev_tx_t nss_ipsecmgr_tunnel_xmit(struct sk_buff *skb, struct net_device *dev) |
| 886 | { |
Sundarajan Srinivasan | 1b03fe2 | 2014-12-02 13:20:56 -0800 | [diff] [blame] | 887 | struct nss_ipsecmgr_priv *priv; |
| 888 | struct nss_ipsecmgr_tbl *encap; |
Samarjeet Banerjee | 07c392c | 2015-09-18 21:26:12 +0530 | [diff] [blame] | 889 | int nhead, ntail; |
| 890 | uint32_t if_num; |
Sundarajan Srinivasan | 1b03fe2 | 2014-12-02 13:20:56 -0800 | [diff] [blame] | 891 | |
| 892 | if_num = NSS_IPSEC_ENCAP_IF_NUMBER; |
| 893 | priv = netdev_priv(dev); |
| 894 | encap = &priv->encap; |
| 895 | |
Samarjeet Banerjee | 07c392c | 2015-09-18 21:26:12 +0530 | [diff] [blame] | 896 | nhead = dev->needed_headroom; |
| 897 | ntail = dev->needed_tailroom; |
| 898 | |
Sundarajan Srinivasan | 1b03fe2 | 2014-12-02 13:20:56 -0800 | [diff] [blame] | 899 | if (skb_is_nonlinear(skb)) { |
| 900 | nss_ipsecmgr_error("%p: NSS IPSEC does not support fragments %p\n", priv->nss_ctx, skb); |
| 901 | goto fail; |
| 902 | } |
| 903 | |
| 904 | /* |
| 905 | * Check if skb is shared |
| 906 | */ |
| 907 | if (unlikely(skb_shared(skb))) { |
| 908 | nss_ipsecmgr_error("%p: Shared skb is not supported: %p\n", priv->nss_ctx, skb); |
| 909 | goto fail; |
| 910 | } |
| 911 | |
| 912 | /* |
Samarjeet Banerjee | 07c392c | 2015-09-18 21:26:12 +0530 | [diff] [blame] | 913 | * Check and expand the packet head room and tail room |
Sundarajan Srinivasan | 1b03fe2 | 2014-12-02 13:20:56 -0800 | [diff] [blame] | 914 | */ |
Samarjeet Banerjee | 07c392c | 2015-09-18 21:26:12 +0530 | [diff] [blame] | 915 | if ((skb_headroom(skb) < nhead) && pskb_expand_head(skb, nhead, ntail, GFP_KERNEL)) { |
| 916 | nss_ipsecmgr_error("%p: unable to expand headroom & tailroom(0x%p)\n", priv->nss_ctx, skb); |
Sundarajan Srinivasan | 1b03fe2 | 2014-12-02 13:20:56 -0800 | [diff] [blame] | 917 | goto fail; |
| 918 | } |
| 919 | |
Samarjeet Banerjee | 07c392c | 2015-09-18 21:26:12 +0530 | [diff] [blame] | 920 | /* |
| 921 | * Check and expand the packet tail room only |
| 922 | */ |
| 923 | if ((skb_tailroom(skb) < ntail) && pskb_expand_head(skb, 0, ntail, GFP_KERNEL)) { |
| 924 | nss_ipsecmgr_error("%p: unable to expand tailroom(0x%p)\n", priv->nss_ctx, skb); |
Sundarajan Srinivasan | 1b03fe2 | 2014-12-02 13:20:56 -0800 | [diff] [blame] | 925 | goto fail; |
| 926 | } |
| 927 | |
| 928 | /* |
| 929 | * Check if packet is given starting from network header |
| 930 | */ |
| 931 | if (skb->data != skb_network_header(skb)) { |
| 932 | nss_ipsecmgr_error("%p: 'Skb data is not starting from IP header", priv->nss_ctx); |
| 933 | goto fail; |
| 934 | } |
| 935 | |
Samarjeet Banerjee | 07c392c | 2015-09-18 21:26:12 +0530 | [diff] [blame] | 936 | switch (skb->protocol) { |
| 937 | case htons(ETH_P_IP): |
| 938 | BUG_ON(ip_hdr(skb)->ttl == 0); |
| 939 | break; |
Sundarajan Srinivasan | 1b03fe2 | 2014-12-02 13:20:56 -0800 | [diff] [blame] | 940 | |
Samarjeet Banerjee | 07c392c | 2015-09-18 21:26:12 +0530 | [diff] [blame] | 941 | case htons(ETH_P_IPV6): |
| 942 | BUG_ON(ipv6_hdr(skb)->hop_limit == 0); |
| 943 | break; |
Sundarajan Srinivasan | 1b03fe2 | 2014-12-02 13:20:56 -0800 | [diff] [blame] | 944 | |
Samarjeet Banerjee | 07c392c | 2015-09-18 21:26:12 +0530 | [diff] [blame] | 945 | default: |
Sundarajan Srinivasan | 1b03fe2 | 2014-12-02 13:20:56 -0800 | [diff] [blame] | 946 | goto fail; |
| 947 | } |
| 948 | |
| 949 | /* |
| 950 | * Send the packet down |
| 951 | */ |
| 952 | if (nss_ipsec_tx_buf(skb, if_num) != 0) { |
| 953 | /* |
| 954 | * TODO: NEED TO STOP THE QUEUE |
| 955 | */ |
| 956 | goto fail; |
| 957 | } |
| 958 | |
| 959 | /* |
| 960 | * Increment the tx count |
| 961 | */ |
| 962 | encap->total_tx++; |
| 963 | |
| 964 | return NETDEV_TX_OK; |
| 965 | |
| 966 | fail: |
| 967 | return NETDEV_TX_BUSY; |
| 968 | } |
| 969 | |
| 970 | /* |
| 971 | * nss_ipsecmgr_tunnel_stats() |
| 972 | * get tunnel statistics |
| 973 | */ |
| 974 | static struct net_device_stats *nss_ipsecmgr_tunnel_stats(struct net_device *dev) |
| 975 | { |
| 976 | struct net_device_stats *stats = &dev->stats; |
| 977 | struct nss_ipsecmgr_priv *priv; |
| 978 | struct nss_ipsec_sa *sa; |
| 979 | uint32_t i; |
| 980 | |
| 981 | memset(stats, 0, sizeof(struct net_device_stats)); |
| 982 | |
| 983 | priv = netdev_priv(dev); |
| 984 | |
| 985 | for (i = 0, sa = &priv->sa_tbl[0]; i < NSS_IPSEC_MAX_SA; i++, sa++) { |
| 986 | if (atomic_read(&sa->user) == 0) { |
| 987 | continue; |
| 988 | } |
| 989 | |
| 990 | switch (sa->type) { |
| 991 | case NSS_IPSECMGR_RULE_TYPE_ENCAP: |
| 992 | stats->tx_packets += sa->stats.pkts.processed; |
| 993 | stats->tx_dropped += sa->stats.pkts.dropped; |
| 994 | break; |
| 995 | |
| 996 | case NSS_IPSECMGR_RULE_TYPE_DECAP: |
| 997 | stats->rx_packets += sa->stats.pkts.processed; |
| 998 | stats->rx_dropped += sa->stats.pkts.dropped; |
| 999 | break; |
| 1000 | |
| 1001 | default: |
| 1002 | nss_ipsecmgr_error("unknown ipsec rule type\n"); |
| 1003 | break; |
| 1004 | } |
| 1005 | } |
| 1006 | |
| 1007 | return stats; |
| 1008 | } |
| 1009 | |
| 1010 | /* NSS IPsec tunnel operation */ |
| 1011 | static const struct net_device_ops nss_ipsecmgr_tunnel_ops = { |
| 1012 | .ndo_init = nss_ipsecmgr_tunnel_init, |
| 1013 | .ndo_uninit = nss_ipsecmgr_tunnel_exit, |
| 1014 | .ndo_start_xmit = nss_ipsecmgr_tunnel_xmit, |
| 1015 | .ndo_open = nss_ipsecmgr_tunnel_open, |
| 1016 | .ndo_stop = nss_ipsecmgr_tunnel_stop, |
| 1017 | .ndo_get_stats = nss_ipsecmgr_tunnel_stats, |
| 1018 | }; |
| 1019 | |
| 1020 | /* |
| 1021 | * nss_ipsecmgr_tunnel_free() |
| 1022 | * free an existing IPsec tunnel interface |
| 1023 | */ |
| 1024 | static void nss_ipsecmgr_tunnel_free(struct net_device *dev) |
| 1025 | { |
| 1026 | struct nss_ipsecmgr_priv *priv = netdev_priv(dev); |
| 1027 | struct nss_ipsecmgr_tbl *encap_tbl; |
| 1028 | struct nss_ipsecmgr_tbl *decap_tbl; |
| 1029 | |
| 1030 | encap_tbl = &priv->encap; |
| 1031 | decap_tbl = &priv->decap; |
| 1032 | |
| 1033 | nss_ipsecmgr_info("IPsec tunnel device(%s) freed\n", dev->name); |
| 1034 | nss_ipsecmgr_info("Entries left: encap(%d), decap(%d)\n", encap_tbl->count, decap_tbl->count); |
| 1035 | |
| 1036 | free_netdev(dev); |
| 1037 | } |
| 1038 | |
| 1039 | /* |
| 1040 | * nss_ipsecmr_setup_tunnel() |
| 1041 | * setup the IPsec tunnel |
| 1042 | */ |
| 1043 | static void nss_ipsecmgr_tunnel_setup(struct net_device *dev) |
| 1044 | { |
| 1045 | dev->addr_len = ETH_ALEN; |
| 1046 | dev->mtu = NSS_IPSECMGR_TUN_MTU(ETH_DATA_LEN); |
| 1047 | |
| 1048 | dev->hard_header_len = NSS_IPSECMGR_TUN_MAX_HDR_LEN; |
| 1049 | dev->needed_headroom = NSS_IPSECMGR_TUN_HEADROOM; |
| 1050 | dev->needed_tailroom = NSS_IPSECMGR_TUN_TAILROOM; |
| 1051 | |
| 1052 | dev->type = NSS_IPSEC_ARPHRD_IPSEC; |
| 1053 | |
| 1054 | dev->ethtool_ops = NULL; |
| 1055 | dev->header_ops = NULL; |
| 1056 | dev->netdev_ops = &nss_ipsecmgr_tunnel_ops; |
| 1057 | |
| 1058 | dev->destructor = nss_ipsecmgr_tunnel_free; |
| 1059 | |
| 1060 | /* |
| 1061 | * XXX:should get the MAC address from the ethernet device |
| 1062 | */ |
| 1063 | memcpy(dev->dev_addr, "\xaa\xbb\xcc\xdd\xee\xff", dev->addr_len); |
| 1064 | memset(dev->broadcast, 0xff, dev->addr_len); |
| 1065 | memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len); |
| 1066 | } |
| 1067 | |
| 1068 | /* |
| 1069 | ********************** |
| 1070 | * Exported Functions |
| 1071 | ********************** |
| 1072 | */ |
| 1073 | |
| 1074 | /* |
| 1075 | * nss_ipsecmgr_tunnel_add() |
| 1076 | * add a IPsec pseudo tunnel device |
| 1077 | */ |
| 1078 | struct net_device *nss_ipsecmgr_tunnel_add(void *cb_ctx, nss_ipsecmgr_data_cb_t data_cb, nss_ipsecmgr_event_cb_t event_cb) |
| 1079 | { |
| 1080 | struct net_device *dev; |
| 1081 | struct nss_ipsecmgr_priv *priv; |
| 1082 | struct nss_ipsec_sa *sa; |
| 1083 | int status; |
| 1084 | int count; |
| 1085 | int32_t if_num; |
| 1086 | |
| 1087 | /* Features denote the skb types supported */ |
| 1088 | uint32_t features = 0; |
| 1089 | |
| 1090 | dev = alloc_netdev(sizeof(struct nss_ipsecmgr_priv), NSS_IPSECMGR_TUN_NAME, nss_ipsecmgr_tunnel_setup); |
| 1091 | if (!dev) { |
| 1092 | nss_ipsecmgr_error("unable to allocate a tunnel device\n"); |
| 1093 | return NULL; |
| 1094 | } |
| 1095 | |
| 1096 | priv = netdev_priv(dev); |
| 1097 | |
| 1098 | priv->cb_ctx = cb_ctx; |
| 1099 | priv->data_cb = data_cb; |
| 1100 | priv->event_cb = event_cb; |
| 1101 | |
| 1102 | /* initialize SA stats table for this tunnel */ |
| 1103 | for (count = 0, sa = &priv->sa_tbl[0]; count < NSS_IPSEC_MAX_SA; count++, sa++) { |
| 1104 | nss_ipsecmgr_sa_init(sa); |
| 1105 | } |
| 1106 | |
| 1107 | spin_lock_init(&priv->encap.lock); |
| 1108 | spin_lock_init(&priv->decap.lock); |
| 1109 | |
| 1110 | status = rtnl_is_locked() ? register_netdevice(dev) : register_netdev(dev); |
| 1111 | if (status < 0) { |
| 1112 | goto fail; |
| 1113 | } |
| 1114 | |
| 1115 | if_num = nss_ipsec_get_interface(priv->nss_ctx); |
| 1116 | if (if_num < 0) { |
| 1117 | nss_ipsecmgr_error("Invalid nss interface :%d\n", if_num); |
| 1118 | return NULL; |
| 1119 | } |
| 1120 | |
| 1121 | nss_ipsec_data_register(if_num, nss_ipsecmgr_buf_receive, dev, features); |
| 1122 | |
| 1123 | /* |
| 1124 | * Store data interface number in private data |
| 1125 | */ |
| 1126 | priv->nss_ifnum = if_num; |
| 1127 | |
| 1128 | nss_ipsec_notify_register(NSS_IPSEC_ENCAP_IF_NUMBER, nss_ipsecmgr_op_receive, dev); |
| 1129 | nss_ipsec_notify_register(NSS_IPSEC_DECAP_IF_NUMBER, nss_ipsecmgr_op_receive, dev); |
| 1130 | |
| 1131 | return dev; |
| 1132 | |
| 1133 | fail: |
| 1134 | free_netdev(dev); |
| 1135 | |
| 1136 | return NULL; |
| 1137 | } |
| 1138 | EXPORT_SYMBOL(nss_ipsecmgr_tunnel_add); |
| 1139 | |
| 1140 | /* |
| 1141 | * nss_ipsecmgr_del_tunnel() |
| 1142 | * delete an existing IPsec tunnel |
| 1143 | */ |
| 1144 | bool nss_ipsecmgr_tunnel_del(struct net_device *dev) |
| 1145 | { |
| 1146 | struct nss_ipsecmgr_priv *priv; |
Sundarajan Srinivasan | 1b03fe2 | 2014-12-02 13:20:56 -0800 | [diff] [blame] | 1147 | bool status; |
| 1148 | |
| 1149 | priv = netdev_priv(dev); |
| 1150 | |
Sundarajan Srinivasan | 1b03fe2 | 2014-12-02 13:20:56 -0800 | [diff] [blame] | 1151 | /* |
| 1152 | * Unregister the callbacks from the HLOS as we are no longer |
| 1153 | * interested in exception data & async messages |
| 1154 | */ |
| 1155 | nss_ipsec_data_unregister(priv->nss_ctx, priv->nss_ifnum); |
| 1156 | |
| 1157 | nss_ipsec_notify_unregister(priv->nss_ctx, NSS_IPSEC_ENCAP_IF_NUMBER); |
| 1158 | nss_ipsec_notify_unregister(priv->nss_ctx, NSS_IPSEC_DECAP_IF_NUMBER); |
| 1159 | |
| 1160 | priv->data_cb = NULL; |
| 1161 | priv->event_cb = NULL; |
| 1162 | |
Amit Gupta | 2c5439c | 2015-08-21 20:52:02 +0530 | [diff] [blame] | 1163 | status = nss_ipsecmgr_sa_flush(dev, NSS_IPSECMGR_RULE_TYPE_ENCAP); |
Sundarajan Srinivasan | 1b03fe2 | 2014-12-02 13:20:56 -0800 | [diff] [blame] | 1164 | if (status != true) { |
| 1165 | nss_ipsecmgr_error("unable complete tunnel deletion, for ingress rules\n"); |
| 1166 | goto fail; |
| 1167 | } |
| 1168 | |
Amit Gupta | 2c5439c | 2015-08-21 20:52:02 +0530 | [diff] [blame] | 1169 | status = nss_ipsecmgr_sa_flush(dev, NSS_IPSECMGR_RULE_TYPE_DECAP); |
Sundarajan Srinivasan | 1b03fe2 | 2014-12-02 13:20:56 -0800 | [diff] [blame] | 1170 | if (status != true) { |
| 1171 | nss_ipsecmgr_error("unable complete tunnel deletion, for egress rules\n"); |
| 1172 | goto fail; |
| 1173 | } |
| 1174 | |
Sundarajan Srinivasan | 1b03fe2 | 2014-12-02 13:20:56 -0800 | [diff] [blame] | 1175 | /* |
| 1176 | * The unregister should start here but the expectation is that the free would |
| 1177 | * happen when the reference count goes down to '0' |
| 1178 | */ |
| 1179 | rtnl_is_locked() ? unregister_netdevice(dev) : unregister_netdev(dev); |
| 1180 | |
| 1181 | return true; |
| 1182 | fail: |
| 1183 | /* caller must retry */ |
| 1184 | return false; |
| 1185 | } |
| 1186 | EXPORT_SYMBOL(nss_ipsecmgr_tunnel_del); |
| 1187 | |
| 1188 | /* |
| 1189 | * nss_ipsecmgr_sa_add() |
| 1190 | * add a new SA to the tunnel |
| 1191 | */ |
| 1192 | bool nss_ipsecmgr_sa_add(struct net_device *dev, union nss_ipsecmgr_rule *rule, enum nss_ipsecmgr_rule_type type) |
| 1193 | { |
| 1194 | struct nss_ipsecmgr_priv *priv; |
| 1195 | struct nss_ipsec_msg nim; |
| 1196 | uint32_t if_num; |
| 1197 | |
| 1198 | priv = netdev_priv(dev); |
| 1199 | |
| 1200 | /* check if there are free SA(s) */ |
| 1201 | if (atomic_read(&gbl_drv_ctx.sa_count) == 0) { |
| 1202 | return false; |
| 1203 | } |
| 1204 | |
| 1205 | memset(&nim, 0, sizeof(struct nss_ipsec_msg)); |
| 1206 | |
| 1207 | /* XXX: some basic validation of the passed rule needs to happen */ |
| 1208 | |
| 1209 | switch (type) { |
| 1210 | case NSS_IPSECMGR_RULE_TYPE_ENCAP: |
| 1211 | if_num = NSS_IPSEC_ENCAP_IF_NUMBER; |
| 1212 | nss_ipsecmgr_copy_encap_add(dev, &nim.msg.push, rule); |
| 1213 | break; |
| 1214 | |
| 1215 | case NSS_IPSECMGR_RULE_TYPE_DECAP: |
| 1216 | if_num = NSS_IPSEC_DECAP_IF_NUMBER; |
| 1217 | nss_ipsecmgr_copy_decap_add(dev, &nim.msg.push, rule); |
| 1218 | break; |
| 1219 | |
| 1220 | default: |
| 1221 | nss_ipsecmgr_error("Unknown rule type(%d) for Add operation\n", type); |
| 1222 | return false; |
| 1223 | } |
| 1224 | |
| 1225 | return nss_ipsecmgr_op_send(dev, &nim, if_num, NSS_IPSEC_MSG_TYPE_ADD_RULE); |
| 1226 | |
| 1227 | } |
| 1228 | EXPORT_SYMBOL(nss_ipsecmgr_sa_add); |
| 1229 | |
| 1230 | /* |
| 1231 | * nss_ipsecmgr_sa_del() |
| 1232 | * |
| 1233 | */ |
| 1234 | bool nss_ipsecmgr_sa_del(struct net_device *dev, union nss_ipsecmgr_rule *rule, enum nss_ipsecmgr_rule_type type) |
| 1235 | { |
| 1236 | struct nss_ipsecmgr_priv *priv; |
| 1237 | struct nss_ipsec_msg nim; |
| 1238 | uint32_t if_num; |
| 1239 | |
| 1240 | priv = netdev_priv(dev); |
| 1241 | |
| 1242 | /* check if all SA(s) are already freed */ |
| 1243 | if (atomic_read(&gbl_drv_ctx.sa_count) == NSS_IPSEC_MAX_RULES) { |
| 1244 | return false; |
| 1245 | } |
| 1246 | |
| 1247 | memset(&nim, 0, sizeof(struct nss_ipsec_msg)); |
| 1248 | |
| 1249 | /* XXX: some basic validation of the passed rule needs to happen */ |
| 1250 | |
| 1251 | switch (type) { |
| 1252 | case NSS_IPSECMGR_RULE_TYPE_ENCAP: |
| 1253 | if_num = NSS_IPSEC_ENCAP_IF_NUMBER; |
| 1254 | nss_ipsecmgr_copy_encap_del(dev, &nim.msg.push, rule); |
| 1255 | break; |
| 1256 | |
| 1257 | case NSS_IPSECMGR_RULE_TYPE_DECAP: |
| 1258 | if_num = NSS_IPSEC_DECAP_IF_NUMBER; |
| 1259 | nss_ipsecmgr_copy_decap_del(dev, &nim.msg.push, rule); |
| 1260 | break; |
| 1261 | |
| 1262 | default: |
| 1263 | nss_ipsecmgr_error("Unknown rule type(%d) for Del operation\n", type); |
| 1264 | return false; |
| 1265 | } |
| 1266 | return nss_ipsecmgr_op_send(dev, &nim, if_num, NSS_IPSEC_MSG_TYPE_DEL_RULE); |
| 1267 | } |
| 1268 | EXPORT_SYMBOL(nss_ipsecmgr_sa_del); |
| 1269 | |
Amit Gupta | 2c5439c | 2015-08-21 20:52:02 +0530 | [diff] [blame] | 1270 | /* |
| 1271 | * nss_ipsecmgr_sa_flush() |
| 1272 | * flush NSS rules for all sa of a specific tunnel |
| 1273 | */ |
| 1274 | bool nss_ipsecmgr_sa_flush(struct net_device *dev, enum nss_ipsecmgr_rule_type type) |
| 1275 | { |
| 1276 | struct nss_ipsec_msg nim; |
| 1277 | int status; |
| 1278 | uint32_t if_num; |
| 1279 | |
| 1280 | BUG_ON(dev == NULL); |
| 1281 | |
| 1282 | /* |
| 1283 | * prepare to flush all SA(s) associated with the tunne |
| 1284 | */ |
| 1285 | memset(&nim, 0, sizeof(struct nss_ipsec_msg)); |
| 1286 | |
| 1287 | switch (type) { |
| 1288 | case NSS_IPSECMGR_RULE_TYPE_ENCAP: |
| 1289 | if_num = NSS_IPSEC_ENCAP_IF_NUMBER; |
| 1290 | break; |
| 1291 | case NSS_IPSECMGR_RULE_TYPE_DECAP: |
| 1292 | if_num = NSS_IPSEC_DECAP_IF_NUMBER; |
| 1293 | break; |
| 1294 | default: |
| 1295 | nss_ipsecmgr_error("Invalid rule type!: %d\n", type); |
| 1296 | return false; |
| 1297 | } |
| 1298 | status = nss_ipsecmgr_op_send(dev, &nim, if_num, NSS_IPSEC_MSG_TYPE_FLUSH_TUN); |
| 1299 | if (status != true) { |
| 1300 | nss_ipsecmgr_error("unable to flush sa rules for %d rule\n", type); |
| 1301 | return false; |
| 1302 | } |
| 1303 | |
| 1304 | return true; |
| 1305 | } |
| 1306 | EXPORT_SYMBOL(nss_ipsecmgr_sa_flush); |
| 1307 | |
Sundarajan Srinivasan | 1b03fe2 | 2014-12-02 13:20:56 -0800 | [diff] [blame] | 1308 | static int __init nss_ipsecmgr_init(void) |
| 1309 | { |
| 1310 | nss_ipsecmgr_info("NSS IPsec manager loaded: Build date %s\n", __DATE__); |
| 1311 | |
| 1312 | memset(&gbl_drv_ctx, 0, sizeof(struct nss_ipsecmgr_drv)); |
| 1313 | |
| 1314 | atomic_set(&gbl_drv_ctx.sa_count, NSS_IPSEC_MAX_RULES); |
| 1315 | |
| 1316 | return 0; |
| 1317 | } |
| 1318 | |
| 1319 | |
| 1320 | static void __exit nss_ipsecmgr_exit(void) |
| 1321 | { |
| 1322 | nss_ipsecmgr_info("NSS IPsec manager unloader\n"); |
| 1323 | } |
| 1324 | |
| 1325 | module_init(nss_ipsecmgr_init); |
| 1326 | module_exit(nss_ipsecmgr_exit); |