blob: af1a4ac2fc2877315d18dc4293460d17c75250ce [file] [log] [blame]
Bharath M Kumar0d87e912013-08-12 18:32:57 +05301/*
Murat Sezgin84ca6512014-04-14 13:57:24 -07002 **************************************************************************
Kyle Swensondd7b2962021-03-16 13:46:32 -06003 * Copyright (c) 2014-2018, 2020, The Linux Foundation. All rights reserved.
Bharath M Kumar0d87e912013-08-12 18:32:57 +05304 * Permission to use, copy, modify, and/or distribute this software for
5 * any purpose with or without fee is hereby granted, provided that the
Murat Sezgin84ca6512014-04-14 13:57:24 -07006 * above copyright notice and this permission notice appear in all copies.
Bharath M Kumar0d87e912013-08-12 18:32:57 +05307 * 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.
Murat Sezgin84ca6512014-04-14 13:57:24 -070014 **************************************************************************
Bharath M Kumar0d87e912013-08-12 18:32:57 +053015 */
16
Radha krishna Simha Jiguru62a29e62014-04-04 17:28:53 +053017#include "nss_tx_rx_common.h"
Sachin Shashidhar47430452018-08-13 16:36:18 -070018#include "nss_tun6rd_log.h"
Bharath M Kumar0d87e912013-08-12 18:32:57 +053019
Bharath M Kumar0d87e912013-08-12 18:32:57 +053020/*
Radha krishna Simha Jiguru62a29e62014-04-04 17:28:53 +053021 * nss_tun6rd_handler()
22 * Handle NSS -> HLOS messages for 6rd tunnel
Bharath M Kumar0d87e912013-08-12 18:32:57 +053023 */
Radha krishna Simha Jiguru62a29e62014-04-04 17:28:53 +053024static void nss_tun6rd_handler(struct nss_ctx_instance *nss_ctx, struct nss_cmn_msg *ncm, __attribute__((unused))void *app_data)
Bharath M Kumar0d87e912013-08-12 18:32:57 +053025{
Radha krishna Simha Jiguru62a29e62014-04-04 17:28:53 +053026 struct nss_tun6rd_msg *ntm = (struct nss_tun6rd_msg *)ncm;
27 void *ctx;
Zhu Ken03082422014-08-11 15:21:42 +080028
Radha krishna Simha Jiguru62a29e62014-04-04 17:28:53 +053029 nss_tun6rd_msg_callback_t cb;
Bharath M Kumar0d87e912013-08-12 18:32:57 +053030
Zhu Ken03082422014-08-11 15:21:42 +080031 BUG_ON(!nss_is_dynamic_interface(ncm->interface));
32
Bharath M Kumar0d87e912013-08-12 18:32:57 +053033 /*
Sachin Shashidhar47430452018-08-13 16:36:18 -070034 * Trace Messages
35 */
36 nss_tun6rd_log_rx_msg(ntm);
37
38 /*
Radha krishna Simha Jiguru62a29e62014-04-04 17:28:53 +053039 * Is this a valid request/response packet?
Bharath M Kumar0d87e912013-08-12 18:32:57 +053040 */
Radha krishna Simha Jiguru62a29e62014-04-04 17:28:53 +053041 if (ncm->type >= NSS_TUN6RD_MAX) {
Kyle Swensondd7b2962021-03-16 13:46:32 -060042 nss_warning("%px: received invalid message %d for Tun6RD interface", nss_ctx, ncm->type);
Bharath M Kumar0d87e912013-08-12 18:32:57 +053043 return;
44 }
45
Suruchi Agarwalef8a8702016-01-08 12:40:08 -080046 if (nss_cmn_get_msg_len(ncm) > sizeof(struct nss_tun6rd_msg)) {
Kyle Swensondd7b2962021-03-16 13:46:32 -060047 nss_warning("%px: Length of message is greater than required: %d", nss_ctx, nss_cmn_get_msg_len(ncm));
Bharath M Kumar0d87e912013-08-12 18:32:57 +053048 return;
49 }
50
Radha krishna Simha Jiguru62a29e62014-04-04 17:28:53 +053051 /*
52 * Update the callback and app_data for NOTIFY messages, tun6rd sends all notify messages
53 * to the same callback/app_data.
54 */
Suruchi Agarwale4ad24a2018-06-11 12:03:46 +053055 if (ncm->response == NSS_CMN_RESPONSE_NOTIFY) {
Stephen Wangaed46332016-12-12 17:29:03 -080056 ncm->cb = (nss_ptr_t)nss_ctx->nss_top->tun6rd_msg_callback;
Radha krishna Simha Jiguru62a29e62014-04-04 17:28:53 +053057 }
Bharath M Kumar0d87e912013-08-12 18:32:57 +053058
59 /*
Radha krishna Simha Jiguru62a29e62014-04-04 17:28:53 +053060 * Log failures
Bharath M Kumar0d87e912013-08-12 18:32:57 +053061 */
Radha krishna Simha Jiguru62a29e62014-04-04 17:28:53 +053062 nss_core_log_msg_failures(nss_ctx, ncm);
Bharath M Kumar0d87e912013-08-12 18:32:57 +053063
64 /*
Radha krishna Simha Jiguru62a29e62014-04-04 17:28:53 +053065 * Do we have a call back
Bharath M Kumar0d87e912013-08-12 18:32:57 +053066 */
Radha krishna Simha Jiguru62a29e62014-04-04 17:28:53 +053067 if (!ncm->cb) {
Bharath M Kumar0d87e912013-08-12 18:32:57 +053068 return;
69 }
70
Radha krishna Simha Jiguru62a29e62014-04-04 17:28:53 +053071 /*
72 * callback
73 */
74 cb = (nss_tun6rd_msg_callback_t)ncm->cb;
Stephen Wang84e0e992016-09-07 12:31:40 -070075 ctx = nss_ctx->subsys_dp_register[ncm->interface].ndev;
Bharath M Kumar0d87e912013-08-12 18:32:57 +053076
77 /*
Radha krishna Simha Jiguru62a29e62014-04-04 17:28:53 +053078 * call 6rd tunnel callback
Bharath M Kumar0d87e912013-08-12 18:32:57 +053079 */
Radha krishna Simha Jiguru62a29e62014-04-04 17:28:53 +053080 if (!ctx) {
Kyle Swensondd7b2962021-03-16 13:46:32 -060081 nss_warning("%px: Event received for 6rd tunnel interface %d before registration", nss_ctx, ncm->interface);
Bharath M Kumar0d87e912013-08-12 18:32:57 +053082 return;
Bharath M Kumar0d87e912013-08-12 18:32:57 +053083 }
84
Radha krishna Simha Jiguru62a29e62014-04-04 17:28:53 +053085 cb(ctx, ntm);
86}
87
Radha krishna Simha Jiguru62a29e62014-04-04 17:28:53 +053088/*
89 * nss_tun6rd_tx()
90 * Transmit a tun6rd message to NSSFW
91 */
92nss_tx_status_t nss_tun6rd_tx(struct nss_ctx_instance *nss_ctx, struct nss_tun6rd_msg *msg)
93{
Radha krishna Simha Jiguru62a29e62014-04-04 17:28:53 +053094 struct nss_cmn_msg *ncm = &msg->cm;
Bharath M Kumar0d87e912013-08-12 18:32:57 +053095
96 /*
Sachin Shashidhar47430452018-08-13 16:36:18 -070097 * Trace Messages
98 */
99 nss_tun6rd_log_tx_msg(msg);
100
101 /*
Radha krishna Simha Jiguru62a29e62014-04-04 17:28:53 +0530102 * Sanity check the message
Bharath M Kumar0d87e912013-08-12 18:32:57 +0530103 */
Zhu Ken03082422014-08-11 15:21:42 +0800104 if (!nss_is_dynamic_interface(ncm->interface)) {
Kyle Swensondd7b2962021-03-16 13:46:32 -0600105 nss_warning("%px: tx request for another interface: %d", nss_ctx, ncm->interface);
Radha krishna Simha Jiguru62a29e62014-04-04 17:28:53 +0530106 return NSS_TX_FAILURE;
Bharath M Kumar0d87e912013-08-12 18:32:57 +0530107 }
108
Radha krishna Simha Jiguru62a29e62014-04-04 17:28:53 +0530109 if (ncm->type > NSS_TUN6RD_MAX) {
Kyle Swensondd7b2962021-03-16 13:46:32 -0600110 nss_warning("%px: message type out of range: %d", nss_ctx, ncm->type);
Radha krishna Simha Jiguru62a29e62014-04-04 17:28:53 +0530111 return NSS_TX_FAILURE;
112 }
113
Stephen Wang3e2dbd12018-03-14 17:28:17 -0700114 return nss_core_send_cmd(nss_ctx, msg, sizeof(*msg), NSS_NBUF_PAYLOAD_SIZE);
Bharath M Kumar0d87e912013-08-12 18:32:57 +0530115}
116
117/*
Radha krishna Simha Jiguru62a29e62014-04-04 17:28:53 +0530118 ***********************************
119 * Register/Unregister/Miscellaneous APIs
120 ***********************************
Bharath M Kumar0d87e912013-08-12 18:32:57 +0530121 */
Radha krishna Simha Jiguru62a29e62014-04-04 17:28:53 +0530122
123/*
124 * nss_register_tun6rd_if()
125 */
Murat Sezginf7ddd082017-06-29 15:09:34 -0700126struct nss_ctx_instance *nss_register_tun6rd_if(uint32_t if_num, uint32_t type, nss_tun6rd_callback_t tun6rd_callback,
Sundarajan Srinivasan70374842014-11-19 15:22:52 -0800127 nss_tun6rd_msg_callback_t event_callback, struct net_device *netdev, uint32_t features)
Bharath M Kumar0d87e912013-08-12 18:32:57 +0530128{
Stephen Wang84e0e992016-09-07 12:31:40 -0700129 struct nss_ctx_instance *nss_ctx = (struct nss_ctx_instance *)&nss_top_main.nss[nss_top_main.tun6rd_handler_id];
130
131 nss_assert(nss_ctx);
Zhu Ken03082422014-08-11 15:21:42 +0800132 nss_assert((if_num >= NSS_DYNAMIC_IF_START) && (if_num < NSS_SPECIAL_IF_START));
Bharath M Kumar0d87e912013-08-12 18:32:57 +0530133
Jackson Bockus7ca70ec2017-07-17 13:47:29 -0700134 nss_core_register_subsys_dp(nss_ctx, if_num, tun6rd_callback, NULL, NULL, netdev, features);
Murat Sezginf7ddd082017-06-29 15:09:34 -0700135 nss_ctx->subsys_dp_register[if_num].type = type;
Sundarajan Srinivasan70374842014-11-19 15:22:52 -0800136
Zhu Ken03082422014-08-11 15:21:42 +0800137 nss_top_main.tun6rd_msg_callback = event_callback;
Bharath M Kumar0d87e912013-08-12 18:32:57 +0530138
Thomas Wu91f4bdf2017-06-09 12:03:02 -0700139 nss_core_register_handler(nss_ctx, if_num, nss_tun6rd_handler, NULL);
Zhu Ken03082422014-08-11 15:21:42 +0800140
Stephen Wang84e0e992016-09-07 12:31:40 -0700141 return nss_ctx;
Zhu Ken03082422014-08-11 15:21:42 +0800142}
143
144/*
Thomas Wu91f4bdf2017-06-09 12:03:02 -0700145 * nss_tun6rd_get_context()
Zhu Ken03082422014-08-11 15:21:42 +0800146 */
Thomas Wu91f4bdf2017-06-09 12:03:02 -0700147struct nss_ctx_instance *nss_tun6rd_get_context()
Zhu Ken03082422014-08-11 15:21:42 +0800148{
149 return (struct nss_ctx_instance *)&nss_top_main.nss[nss_top_main.tun6rd_handler_id];
Bharath M Kumar0d87e912013-08-12 18:32:57 +0530150}
151
152/*
Radha krishna Simha Jiguru62a29e62014-04-04 17:28:53 +0530153 * nss_unregister_tun6rd_if()
Bharath M Kumar0d87e912013-08-12 18:32:57 +0530154 */
Radha krishna Simha Jiguru62a29e62014-04-04 17:28:53 +0530155void nss_unregister_tun6rd_if(uint32_t if_num)
Bharath M Kumar0d87e912013-08-12 18:32:57 +0530156{
Stephen Wang84e0e992016-09-07 12:31:40 -0700157 struct nss_ctx_instance *nss_ctx = (struct nss_ctx_instance *)&nss_top_main.nss[nss_top_main.tun6rd_handler_id];
158
159 nss_assert(nss_ctx);
Sakthi Vignesh Radhakrishnan7a8fbe92015-02-23 16:23:29 -0800160 nss_assert(nss_is_dynamic_interface(if_num));
Bharath M Kumar0d87e912013-08-12 18:32:57 +0530161
Jackson Bockus7ca70ec2017-07-17 13:47:29 -0700162 nss_core_unregister_subsys_dp(nss_ctx, if_num);
Murat Sezginf7ddd082017-06-29 15:09:34 -0700163 nss_ctx->subsys_dp_register[if_num].type = 0;
Sundarajan Srinivasan70374842014-11-19 15:22:52 -0800164
Zhu Ken03082422014-08-11 15:21:42 +0800165 nss_top_main.tun6rd_msg_callback = NULL;
166
Thomas Wu91f4bdf2017-06-09 12:03:02 -0700167 nss_core_unregister_handler(nss_ctx, if_num);
Bharath M Kumar0d87e912013-08-12 18:32:57 +0530168}
169
Sundarajan Srinivasan02e6c2b2014-10-06 11:51:12 -0700170/*
171 * nss_tun6rd_msg_init()
172 * Initialize nss_tun6rd msg.
173 */
174void nss_tun6rd_msg_init(struct nss_tun6rd_msg *ncm, uint16_t if_num, uint32_t type, uint32_t len, void *cb, void *app_data)
175{
Thomas Wu91f4bdf2017-06-09 12:03:02 -0700176 nss_cmn_msg_init(&ncm->cm, if_num, type, len, cb, app_data);
Sundarajan Srinivasan02e6c2b2014-10-06 11:51:12 -0700177}
178
Zhu Ken03082422014-08-11 15:21:42 +0800179EXPORT_SYMBOL(nss_tun6rd_get_context);
Radha krishna Simha Jiguru62a29e62014-04-04 17:28:53 +0530180EXPORT_SYMBOL(nss_tun6rd_tx);
181EXPORT_SYMBOL(nss_register_tun6rd_if);
182EXPORT_SYMBOL(nss_unregister_tun6rd_if);
Sundarajan Srinivasan02e6c2b2014-10-06 11:51:12 -0700183EXPORT_SYMBOL(nss_tun6rd_msg_init);