blob: fb8f67bb3af8e01a86fa1a1b0b043280ae521947 [file] [log] [blame]
Bharath M Kumar0d87e912013-08-12 18:32:57 +05301/*
Murat Sezgin84ca6512014-04-14 13:57:24 -07002 **************************************************************************
Murat Sezginf7ddd082017-06-29 15:09:34 -07003 * Copyright (c) 2014-2018, 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"
Bharath M Kumar0d87e912013-08-12 18:32:57 +053018
Bharath M Kumar0d87e912013-08-12 18:32:57 +053019/*
Radha krishna Simha Jiguru62a29e62014-04-04 17:28:53 +053020 * nss_tun6rd_handler()
21 * Handle NSS -> HLOS messages for 6rd tunnel
Bharath M Kumar0d87e912013-08-12 18:32:57 +053022 */
Radha krishna Simha Jiguru62a29e62014-04-04 17:28:53 +053023static 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 +053024{
Radha krishna Simha Jiguru62a29e62014-04-04 17:28:53 +053025 struct nss_tun6rd_msg *ntm = (struct nss_tun6rd_msg *)ncm;
26 void *ctx;
Zhu Ken03082422014-08-11 15:21:42 +080027
Radha krishna Simha Jiguru62a29e62014-04-04 17:28:53 +053028 nss_tun6rd_msg_callback_t cb;
Bharath M Kumar0d87e912013-08-12 18:32:57 +053029
Zhu Ken03082422014-08-11 15:21:42 +080030 BUG_ON(!nss_is_dynamic_interface(ncm->interface));
31
Bharath M Kumar0d87e912013-08-12 18:32:57 +053032 /*
Radha krishna Simha Jiguru62a29e62014-04-04 17:28:53 +053033 * Is this a valid request/response packet?
Bharath M Kumar0d87e912013-08-12 18:32:57 +053034 */
Radha krishna Simha Jiguru62a29e62014-04-04 17:28:53 +053035 if (ncm->type >= NSS_TUN6RD_MAX) {
36 nss_warning("%p: received invalid message %d for Tun6RD interface", nss_ctx, ncm->type);
Bharath M Kumar0d87e912013-08-12 18:32:57 +053037 return;
38 }
39
Suruchi Agarwalef8a8702016-01-08 12:40:08 -080040 if (nss_cmn_get_msg_len(ncm) > sizeof(struct nss_tun6rd_msg)) {
41 nss_warning("%p: Length of message is greater than required: %d", nss_ctx, nss_cmn_get_msg_len(ncm));
Bharath M Kumar0d87e912013-08-12 18:32:57 +053042 return;
43 }
44
Radha krishna Simha Jiguru62a29e62014-04-04 17:28:53 +053045 /*
46 * Update the callback and app_data for NOTIFY messages, tun6rd sends all notify messages
47 * to the same callback/app_data.
48 */
49 if (ncm->response == NSS_CMM_RESPONSE_NOTIFY) {
Stephen Wangaed46332016-12-12 17:29:03 -080050 ncm->cb = (nss_ptr_t)nss_ctx->nss_top->tun6rd_msg_callback;
Radha krishna Simha Jiguru62a29e62014-04-04 17:28:53 +053051 }
Bharath M Kumar0d87e912013-08-12 18:32:57 +053052
53 /*
Radha krishna Simha Jiguru62a29e62014-04-04 17:28:53 +053054 * Log failures
Bharath M Kumar0d87e912013-08-12 18:32:57 +053055 */
Radha krishna Simha Jiguru62a29e62014-04-04 17:28:53 +053056 nss_core_log_msg_failures(nss_ctx, ncm);
Bharath M Kumar0d87e912013-08-12 18:32:57 +053057
58 /*
Radha krishna Simha Jiguru62a29e62014-04-04 17:28:53 +053059 * Do we have a call back
Bharath M Kumar0d87e912013-08-12 18:32:57 +053060 */
Radha krishna Simha Jiguru62a29e62014-04-04 17:28:53 +053061 if (!ncm->cb) {
Bharath M Kumar0d87e912013-08-12 18:32:57 +053062 return;
63 }
64
Radha krishna Simha Jiguru62a29e62014-04-04 17:28:53 +053065 /*
66 * callback
67 */
68 cb = (nss_tun6rd_msg_callback_t)ncm->cb;
Stephen Wang84e0e992016-09-07 12:31:40 -070069 ctx = nss_ctx->subsys_dp_register[ncm->interface].ndev;
Bharath M Kumar0d87e912013-08-12 18:32:57 +053070
71 /*
Radha krishna Simha Jiguru62a29e62014-04-04 17:28:53 +053072 * call 6rd tunnel callback
Bharath M Kumar0d87e912013-08-12 18:32:57 +053073 */
Radha krishna Simha Jiguru62a29e62014-04-04 17:28:53 +053074 if (!ctx) {
75 nss_warning("%p: Event received for 6rd tunnel interface %d before registration", nss_ctx, ncm->interface);
Bharath M Kumar0d87e912013-08-12 18:32:57 +053076 return;
Bharath M Kumar0d87e912013-08-12 18:32:57 +053077 }
78
Radha krishna Simha Jiguru62a29e62014-04-04 17:28:53 +053079 cb(ctx, ntm);
80}
81
Radha krishna Simha Jiguru62a29e62014-04-04 17:28:53 +053082/*
83 * nss_tun6rd_tx()
84 * Transmit a tun6rd message to NSSFW
85 */
86nss_tx_status_t nss_tun6rd_tx(struct nss_ctx_instance *nss_ctx, struct nss_tun6rd_msg *msg)
87{
Radha krishna Simha Jiguru62a29e62014-04-04 17:28:53 +053088 struct nss_cmn_msg *ncm = &msg->cm;
Bharath M Kumar0d87e912013-08-12 18:32:57 +053089
90 /*
Radha krishna Simha Jiguru62a29e62014-04-04 17:28:53 +053091 * Sanity check the message
Bharath M Kumar0d87e912013-08-12 18:32:57 +053092 */
Zhu Ken03082422014-08-11 15:21:42 +080093 if (!nss_is_dynamic_interface(ncm->interface)) {
Radha krishna Simha Jiguru62a29e62014-04-04 17:28:53 +053094 nss_warning("%p: tx request for another interface: %d", nss_ctx, ncm->interface);
95 return NSS_TX_FAILURE;
Bharath M Kumar0d87e912013-08-12 18:32:57 +053096 }
97
Radha krishna Simha Jiguru62a29e62014-04-04 17:28:53 +053098 if (ncm->type > NSS_TUN6RD_MAX) {
99 nss_warning("%p: message type out of range: %d", nss_ctx, ncm->type);
100 return NSS_TX_FAILURE;
101 }
102
Stephen Wang3e2dbd12018-03-14 17:28:17 -0700103 return nss_core_send_cmd(nss_ctx, msg, sizeof(*msg), NSS_NBUF_PAYLOAD_SIZE);
Bharath M Kumar0d87e912013-08-12 18:32:57 +0530104}
105
106/*
Radha krishna Simha Jiguru62a29e62014-04-04 17:28:53 +0530107 ***********************************
108 * Register/Unregister/Miscellaneous APIs
109 ***********************************
Bharath M Kumar0d87e912013-08-12 18:32:57 +0530110 */
Radha krishna Simha Jiguru62a29e62014-04-04 17:28:53 +0530111
112/*
113 * nss_register_tun6rd_if()
114 */
Murat Sezginf7ddd082017-06-29 15:09:34 -0700115struct 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 -0800116 nss_tun6rd_msg_callback_t event_callback, struct net_device *netdev, uint32_t features)
Bharath M Kumar0d87e912013-08-12 18:32:57 +0530117{
Stephen Wang84e0e992016-09-07 12:31:40 -0700118 struct nss_ctx_instance *nss_ctx = (struct nss_ctx_instance *)&nss_top_main.nss[nss_top_main.tun6rd_handler_id];
119
120 nss_assert(nss_ctx);
Zhu Ken03082422014-08-11 15:21:42 +0800121 nss_assert((if_num >= NSS_DYNAMIC_IF_START) && (if_num < NSS_SPECIAL_IF_START));
Bharath M Kumar0d87e912013-08-12 18:32:57 +0530122
Jackson Bockus7ca70ec2017-07-17 13:47:29 -0700123 nss_core_register_subsys_dp(nss_ctx, if_num, tun6rd_callback, NULL, NULL, netdev, features);
Murat Sezginf7ddd082017-06-29 15:09:34 -0700124 nss_ctx->subsys_dp_register[if_num].type = type;
Sundarajan Srinivasan70374842014-11-19 15:22:52 -0800125
Zhu Ken03082422014-08-11 15:21:42 +0800126 nss_top_main.tun6rd_msg_callback = event_callback;
Bharath M Kumar0d87e912013-08-12 18:32:57 +0530127
Thomas Wu91f4bdf2017-06-09 12:03:02 -0700128 nss_core_register_handler(nss_ctx, if_num, nss_tun6rd_handler, NULL);
Zhu Ken03082422014-08-11 15:21:42 +0800129
Stephen Wang84e0e992016-09-07 12:31:40 -0700130 return nss_ctx;
Zhu Ken03082422014-08-11 15:21:42 +0800131}
132
133/*
Thomas Wu91f4bdf2017-06-09 12:03:02 -0700134 * nss_tun6rd_get_context()
Zhu Ken03082422014-08-11 15:21:42 +0800135 */
Thomas Wu91f4bdf2017-06-09 12:03:02 -0700136struct nss_ctx_instance *nss_tun6rd_get_context()
Zhu Ken03082422014-08-11 15:21:42 +0800137{
138 return (struct nss_ctx_instance *)&nss_top_main.nss[nss_top_main.tun6rd_handler_id];
Bharath M Kumar0d87e912013-08-12 18:32:57 +0530139}
140
141/*
Radha krishna Simha Jiguru62a29e62014-04-04 17:28:53 +0530142 * nss_unregister_tun6rd_if()
Bharath M Kumar0d87e912013-08-12 18:32:57 +0530143 */
Radha krishna Simha Jiguru62a29e62014-04-04 17:28:53 +0530144void nss_unregister_tun6rd_if(uint32_t if_num)
Bharath M Kumar0d87e912013-08-12 18:32:57 +0530145{
Stephen Wang84e0e992016-09-07 12:31:40 -0700146 struct nss_ctx_instance *nss_ctx = (struct nss_ctx_instance *)&nss_top_main.nss[nss_top_main.tun6rd_handler_id];
147
148 nss_assert(nss_ctx);
Sakthi Vignesh Radhakrishnan7a8fbe92015-02-23 16:23:29 -0800149 nss_assert(nss_is_dynamic_interface(if_num));
Bharath M Kumar0d87e912013-08-12 18:32:57 +0530150
Jackson Bockus7ca70ec2017-07-17 13:47:29 -0700151 nss_core_unregister_subsys_dp(nss_ctx, if_num);
Murat Sezginf7ddd082017-06-29 15:09:34 -0700152 nss_ctx->subsys_dp_register[if_num].type = 0;
Sundarajan Srinivasan70374842014-11-19 15:22:52 -0800153
Zhu Ken03082422014-08-11 15:21:42 +0800154 nss_top_main.tun6rd_msg_callback = NULL;
155
Thomas Wu91f4bdf2017-06-09 12:03:02 -0700156 nss_core_unregister_handler(nss_ctx, if_num);
Bharath M Kumar0d87e912013-08-12 18:32:57 +0530157}
158
Sundarajan Srinivasan02e6c2b2014-10-06 11:51:12 -0700159/*
160 * nss_tun6rd_msg_init()
161 * Initialize nss_tun6rd msg.
162 */
163void nss_tun6rd_msg_init(struct nss_tun6rd_msg *ncm, uint16_t if_num, uint32_t type, uint32_t len, void *cb, void *app_data)
164{
Thomas Wu91f4bdf2017-06-09 12:03:02 -0700165 nss_cmn_msg_init(&ncm->cm, if_num, type, len, cb, app_data);
Sundarajan Srinivasan02e6c2b2014-10-06 11:51:12 -0700166}
167
Zhu Ken03082422014-08-11 15:21:42 +0800168EXPORT_SYMBOL(nss_tun6rd_get_context);
Radha krishna Simha Jiguru62a29e62014-04-04 17:28:53 +0530169EXPORT_SYMBOL(nss_tun6rd_tx);
170EXPORT_SYMBOL(nss_register_tun6rd_if);
171EXPORT_SYMBOL(nss_unregister_tun6rd_if);
Sundarajan Srinivasan02e6c2b2014-10-06 11:51:12 -0700172EXPORT_SYMBOL(nss_tun6rd_msg_init);