blob: f3187288db234bdae6e743eae6b11c592b20d5a8 [file] [log] [blame]
Bharath M Kumar614bbf82013-08-31 20:18:44 +05301/*
Murat Sezgin84ca6512014-04-14 13:57:24 -07002 **************************************************************************
Stephen Wang3e2dbd12018-03-14 17:28:17 -07003 * Copyright (c) 2014-2018, The Linux Foundation. All rights reserved.
Bharath M Kumar614bbf82013-08-31 20:18:44 +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 Kumar614bbf82013-08-31 20:18:44 +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 Kumar614bbf82013-08-31 20:18:44 +053015 */
16
Radha krishna Simha Jiguru62a29e62014-04-04 17:28:53 +053017#include "nss_tx_rx_common.h"
Bharath M Kumar614bbf82013-08-31 20:18:44 +053018
Bharath M Kumar614bbf82013-08-31 20:18:44 +053019/*
Ken Zhu648cf3d2017-10-06 17:44:53 -070020 * nss_tunipip6_verify_if_num
21 * Verify the interface is a valid interface
22 */
23static bool nss_tunipip6_verify_if_num(uint32_t if_num)
24{
25 return nss_dynamic_interface_get_type(nss_tunipip6_get_context(), if_num) == NSS_DYNAMIC_INTERFACE_TYPE_TUNIPIP6;
26}
27
28/*
Radha krishna Simha Jiguru62a29e62014-04-04 17:28:53 +053029 * nss_tunipip6_handler()
Sol Kavy359ef152014-04-04 11:55:24 -070030 * Handle NSS -> HLOS messages for 6rd tunnel
Bharath M Kumar614bbf82013-08-31 20:18:44 +053031 */
Radha krishna Simha Jiguru62a29e62014-04-04 17:28:53 +053032static void nss_tunipip6_handler(struct nss_ctx_instance *nss_ctx, struct nss_cmn_msg *ncm, __attribute__((unused))void *app_data)
Bharath M Kumar614bbf82013-08-31 20:18:44 +053033{
Radha krishna Simha Jiguru62a29e62014-04-04 17:28:53 +053034 struct nss_tunipip6_msg *ntm = (struct nss_tunipip6_msg *)ncm;
35 void *ctx;
36 nss_tunipip6_msg_callback_t cb;
Bharath M Kumar614bbf82013-08-31 20:18:44 +053037
Ken Zhu648cf3d2017-10-06 17:44:53 -070038 BUG_ON(!nss_tunipip6_verify_if_num(ncm->interface));
39
Bharath M Kumar614bbf82013-08-31 20:18:44 +053040 /*
Radha krishna Simha Jiguru62a29e62014-04-04 17:28:53 +053041 * Is this a valid request/response packet?
Bharath M Kumar614bbf82013-08-31 20:18:44 +053042 */
Radha krishna Simha Jiguru62a29e62014-04-04 17:28:53 +053043 if (ncm->type >= NSS_TUNIPIP6_MAX) {
44 nss_warning("%p: received invalid message %d for DS-Lite interface", nss_ctx, ncm->type);
Bharath M Kumar614bbf82013-08-31 20:18:44 +053045 return;
46 }
47
Suruchi Agarwalef8a8702016-01-08 12:40:08 -080048 if (nss_cmn_get_msg_len(ncm) > sizeof(struct nss_tunipip6_msg)) {
49 nss_warning("%p: Length of message is greater than required: %d", nss_ctx, nss_cmn_get_msg_len(ncm));
Bharath M Kumar614bbf82013-08-31 20:18:44 +053050 return;
51 }
52
53 /*
Radha krishna Simha Jiguru62a29e62014-04-04 17:28:53 +053054 * Update the callback and app_data for NOTIFY messages, tun6rd sends all notify messages
55 * to the same callback/app_data.
Bharath M Kumar614bbf82013-08-31 20:18:44 +053056 */
Radha krishna Simha Jiguru62a29e62014-04-04 17:28:53 +053057 if (ncm->response == NSS_CMM_RESPONSE_NOTIFY) {
Stephen Wangaed46332016-12-12 17:29:03 -080058 ncm->cb = (nss_ptr_t)nss_ctx->nss_top->tunipip6_msg_callback;
Radha krishna Simha Jiguru62a29e62014-04-04 17:28:53 +053059 }
60
61 /*
62 * Log failures
63 */
64 nss_core_log_msg_failures(nss_ctx, ncm);
65
66 /*
67 * Do we have a call back
68 */
69 if (!ncm->cb) {
Bharath M Kumar614bbf82013-08-31 20:18:44 +053070 return;
71 }
72
73 /*
Radha krishna Simha Jiguru62a29e62014-04-04 17:28:53 +053074 * callback
Bharath M Kumar614bbf82013-08-31 20:18:44 +053075 */
Radha krishna Simha Jiguru62a29e62014-04-04 17:28:53 +053076 cb = (nss_tunipip6_msg_callback_t)ncm->cb;
Stephen Wang84e0e992016-09-07 12:31:40 -070077 ctx = nss_ctx->subsys_dp_register[ncm->interface].ndev;
Bharath M Kumar614bbf82013-08-31 20:18:44 +053078
Radha krishna Simha Jiguru62a29e62014-04-04 17:28:53 +053079 /*
80 * call ipip6 tunnel callback
81 */
82 if (!ctx) {
83 nss_warning("%p: Event received for DS-Lite tunnel interface %d before registration", nss_ctx, ncm->interface);
Bharath M Kumar614bbf82013-08-31 20:18:44 +053084 return;
85 }
86
Radha krishna Simha Jiguru62a29e62014-04-04 17:28:53 +053087 cb(ctx, ntm);
Bharath M Kumar614bbf82013-08-31 20:18:44 +053088}
89
90/*
Radha krishna Simha Jiguru62a29e62014-04-04 17:28:53 +053091 * nss_tunipip6_tx()
92 * Transmit a tun6rd message to NSSFW
Bharath M Kumar614bbf82013-08-31 20:18:44 +053093 */
Radha krishna Simha Jiguru62a29e62014-04-04 17:28:53 +053094nss_tx_status_t nss_tunipip6_tx(struct nss_ctx_instance *nss_ctx, struct nss_tunipip6_msg *msg)
Bharath M Kumar614bbf82013-08-31 20:18:44 +053095{
Radha krishna Simha Jiguru62a29e62014-04-04 17:28:53 +053096 struct nss_cmn_msg *ncm = &msg->cm;
Bharath M Kumar614bbf82013-08-31 20:18:44 +053097
Bharath M Kumar614bbf82013-08-31 20:18:44 +053098 /*
Radha krishna Simha Jiguru62a29e62014-04-04 17:28:53 +053099 * Sanity check the message
Bharath M Kumar614bbf82013-08-31 20:18:44 +0530100 */
Ken Zhu648cf3d2017-10-06 17:44:53 -0700101 if (!nss_tunipip6_verify_if_num(ncm->interface)) {
Radha krishna Simha Jiguru62a29e62014-04-04 17:28:53 +0530102 nss_warning("%p: tx request for another interface: %d", nss_ctx, ncm->interface);
103 return NSS_TX_FAILURE;
Bharath M Kumar614bbf82013-08-31 20:18:44 +0530104 }
105
Radha krishna Simha Jiguru62a29e62014-04-04 17:28:53 +0530106 if (ncm->type > NSS_TUNIPIP6_MAX) {
107 nss_warning("%p: message type out of range: %d", nss_ctx, ncm->type);
108 return NSS_TX_FAILURE;
Radhakrishna Jiguru5ad493d2013-11-25 20:17:54 +0530109 }
Radha krishna Simha Jiguru62a29e62014-04-04 17:28:53 +0530110
Stephen Wang3e2dbd12018-03-14 17:28:17 -0700111 return nss_core_send_cmd(nss_ctx, msg, sizeof(*msg), NSS_NBUF_PAYLOAD_SIZE);
Radhakrishna Jiguru5ad493d2013-11-25 20:17:54 +0530112}
Ken Zhu648cf3d2017-10-06 17:44:53 -0700113EXPORT_SYMBOL(nss_tunipip6_tx);
Radhakrishna Jiguru5ad493d2013-11-25 20:17:54 +0530114
115/*
Radha krishna Simha Jiguru62a29e62014-04-04 17:28:53 +0530116 * **********************************
117 * Register/Unregister/Miscellaneous APIs
118 * **********************************
Bharath M Kumar614bbf82013-08-31 20:18:44 +0530119 */
Radha krishna Simha Jiguru62a29e62014-04-04 17:28:53 +0530120
121/*
122 * nss_register_tunipip6_if()
123 */
124struct nss_ctx_instance *nss_register_tunipip6_if(uint32_t if_num,
125 nss_tunipip6_callback_t tunipip6_callback,
Sundarajan Srinivasan70374842014-11-19 15:22:52 -0800126 nss_tunipip6_msg_callback_t event_callback,
127 struct net_device *netdev,
128 uint32_t features)
Bharath M Kumar614bbf82013-08-31 20:18:44 +0530129{
Stephen Wang84e0e992016-09-07 12:31:40 -0700130 struct nss_ctx_instance *nss_ctx = (struct nss_ctx_instance *)&nss_top_main.nss[nss_top_main.tunipip6_handler_id];
131
132 nss_assert(nss_ctx);
Ken Zhu648cf3d2017-10-06 17:44:53 -0700133 nss_assert(nss_tunipip6_verify_if_num(if_num));
Bharath M Kumar614bbf82013-08-31 20:18:44 +0530134
Radha krishna Simha Jiguru62a29e62014-04-04 17:28:53 +0530135 nss_top_main.tunipip6_msg_callback = event_callback;
Jackson Bockus7ca70ec2017-07-17 13:47:29 -0700136 nss_core_register_subsys_dp(nss_ctx, if_num, tunipip6_callback, NULL, NULL, netdev, features);
Ken Zhu648cf3d2017-10-06 17:44:53 -0700137 nss_core_register_handler(nss_ctx, if_num, nss_tunipip6_handler, NULL);
Sundarajan Srinivasan70374842014-11-19 15:22:52 -0800138
Stephen Wang84e0e992016-09-07 12:31:40 -0700139 return nss_ctx;
Bharath M Kumar614bbf82013-08-31 20:18:44 +0530140}
Ken Zhu648cf3d2017-10-06 17:44:53 -0700141EXPORT_SYMBOL(nss_register_tunipip6_if);
Bharath M Kumar614bbf82013-08-31 20:18:44 +0530142
143/*
Radha krishna Simha Jiguru62a29e62014-04-04 17:28:53 +0530144 * nss_unregister_tunipip6_if()
Bharath M Kumar614bbf82013-08-31 20:18:44 +0530145 */
Radha krishna Simha Jiguru62a29e62014-04-04 17:28:53 +0530146void nss_unregister_tunipip6_if(uint32_t if_num)
Bharath M Kumar614bbf82013-08-31 20:18:44 +0530147{
Stephen Wang84e0e992016-09-07 12:31:40 -0700148 struct nss_ctx_instance *nss_ctx = (struct nss_ctx_instance *)&nss_top_main.nss[nss_top_main.tunipip6_handler_id];
149
150 nss_assert(nss_ctx);
Ken Zhu648cf3d2017-10-06 17:44:53 -0700151 nss_assert(nss_tunipip6_verify_if_num(if_num));
Bharath M Kumar614bbf82013-08-31 20:18:44 +0530152
Jackson Bockus7ca70ec2017-07-17 13:47:29 -0700153 nss_core_unregister_subsys_dp(nss_ctx, if_num);
154
Radha krishna Simha Jiguru62a29e62014-04-04 17:28:53 +0530155 nss_top_main.tunipip6_msg_callback = NULL;
Bharath M Kumar614bbf82013-08-31 20:18:44 +0530156}
Ken Zhu648cf3d2017-10-06 17:44:53 -0700157EXPORT_SYMBOL(nss_unregister_tunipip6_if);
Bharath M Kumar614bbf82013-08-31 20:18:44 +0530158
Radha krishna Simha Jiguru62a29e62014-04-04 17:28:53 +0530159/*
Thomas Wu91f4bdf2017-06-09 12:03:02 -0700160 * nss_tunipip6_get_context()
161 */
162struct nss_ctx_instance *nss_tunipip6_get_context(void)
163{
164 return (struct nss_ctx_instance *)&nss_top_main.nss[nss_top_main.tunipip6_handler_id];
165}
Ken Zhu648cf3d2017-10-06 17:44:53 -0700166EXPORT_SYMBOL(nss_tunipip6_get_context);
Thomas Wu91f4bdf2017-06-09 12:03:02 -0700167
168/*
Radha krishna Simha Jiguru62a29e62014-04-04 17:28:53 +0530169 * nss_tunipip6_register_handler()
170 */
171void nss_tunipip6_register_handler()
172{
Thomas Wu91f4bdf2017-06-09 12:03:02 -0700173 struct nss_ctx_instance *nss_ctx = nss_tunipip6_get_context();
174
175 nss_core_register_handler(nss_ctx, NSS_TUNIPIP6_INTERFACE, nss_tunipip6_handler, NULL);
Radha krishna Simha Jiguru62a29e62014-04-04 17:28:53 +0530176}
Bharath M Kumar614bbf82013-08-31 20:18:44 +0530177
Sundarajan Srinivasan02e6c2b2014-10-06 11:51:12 -0700178/*
179 * nss_tunipip6_msg_init()
Thomas Wu91f4bdf2017-06-09 12:03:02 -0700180 * Initialize nss_tunipip6 msg.
Sundarajan Srinivasan02e6c2b2014-10-06 11:51:12 -0700181 */
182void nss_tunipip6_msg_init(struct nss_tunipip6_msg *ntm, uint16_t if_num, uint32_t type, uint32_t len, void *cb, void *app_data)
183{
184 nss_cmn_msg_init(&ntm->cm, if_num, type, len, cb, app_data);
185}
Sundarajan Srinivasan02e6c2b2014-10-06 11:51:12 -0700186EXPORT_SYMBOL(nss_tunipip6_msg_init);