hc
2023-02-13 e440ec23c5a540cdd3f7464e8779219be6fd3d95
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
/*
 * Copyright (c) 2015 South Silicon Valley Microelectronics Inc.
 * Copyright (c) 2015 iComm Corporation
 *
 * This program is free software: you can redistribute it and/or modify 
 * it under the terms of the GNU General Public License as published by 
 * the Free Software Foundation, either version 3 of the License, or 
 * (at your option) any later version.
 * This program is distributed in the hope that it will be useful, but 
 * WITHOUT ANY WARRANTY; without even the implied warranty of 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
 * See the GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License 
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
 
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <net/sock.h>
#include <linux/socket.h>
#include <linux/net.h>
#include <asm/types.h>
#include <linux/netlink.h>
#include <linux/skbuff.h>
#include <linux/version.h>
#include <ssv6200.h>
#include "lib.h"
#include "dev.h"
#define NETLINK_SMARTLINK (31)
#define MAX_PAYLOAD (2048)
static struct sock *nl_sk = NULL;
struct ssv_softc *ssv_smartlink_sc = NULL;
EXPORT_SYMBOL(ssv_smartlink_sc);
u32 ssv_smartlink_status=0;
static int _ksmartlink_start_smartlink(u8 *pInBuf, u32 inBufLen, u8 *pOutBuf, u32 *pOutBufLen)
{
#ifdef KSMARTLINK_DEBUG
    printk(KERN_INFO "%s\n", __FUNCTION__);
#endif
    ssv_smartlink_status = 1;
    *pOutBufLen = 0;
    return 0;
}
int ksmartlink_smartlink_started(void)
{
    return ssv_smartlink_status;
}
EXPORT_SYMBOL(ksmartlink_smartlink_started);
static int _ksmartlink_stop_smartlink(u8 *pInBuf, u32 inBufLen, u8 *pOutBuf, u32 *pOutBufLen)
{
#ifdef KSMARTLINK_DEBUG
    printk(KERN_INFO "%s\n", __FUNCTION__);
#endif
    ssv_smartlink_status = 0;
    *pOutBufLen = 0;
    return 0;
}
static int _ksmartlink_set_channel(u8 *pInBuf, u32 inBufLen, u8 *pOutBuf, u32 *pOutBufLen)
{
    int ret=-10;
    int ch=(int)(*pInBuf);
    struct ssv_softc *sc=ssv_smartlink_sc;
#ifdef KSMARTLINK_DEBUG
    printk(KERN_INFO "%s %d\n", __FUNCTION__, ch);
#endif
    if (!sc)
    {
        goto out;
    }
    mutex_lock(&sc->mutex);
    ret = ssv6xxx_set_channel(sc, ch);
    mutex_unlock(&sc->mutex);
    *pOutBufLen = 0;
out:
    return ret;
}
static int _ksmartlink_get_channel(u8 *pInBuf, u32 inBufLen, u8 *pOutBuf, u32 *pOutBufLen)
{
    int ret=-10;
    int ch=0;
    struct ssv_softc *sc=ssv_smartlink_sc;
#ifdef KSMARTLINK_DEBUG
    printk(KERN_INFO "%s\n", __FUNCTION__);
#endif
    if (!sc)
    {
        goto out;
    }
    mutex_lock(&sc->mutex);
    ret = ssv6xxx_get_channel(sc, &ch);
    mutex_unlock(&sc->mutex);
    *pOutBuf = ch;
    *pOutBufLen = 1;
#ifdef KSMARTLINK_DEBUG
    printk(KERN_INFO "%s %d\n", __FUNCTION__, ch);
#endif
out:
    return ret;
}
static int _ksmartlink_set_promisc(u8 *pInBuf, u32 inBufLen, u8 *pOutBuf, u32 *pOutBufLen)
{
    int ret=-10;
    int accept=(int)(*pInBuf);
    struct ssv_softc *sc=ssv_smartlink_sc;
#ifdef KSMARTLINK_DEBUG
    printk(KERN_INFO "%s %d\n", __FUNCTION__, accept);
#endif
    if (!sc)
    {
        goto out;
    }
    mutex_lock(&sc->mutex);
    ret = ssv6xxx_set_promisc(sc, accept);
    mutex_unlock(&sc->mutex);
    *pOutBufLen = 0;
out:
    return ret;
}
static int _ksmartlink_get_promisc(u8 *pInBuf, u32 inBufLen, u8 *pOutBuf, u32 *pOutBufLen)
{
    int ret=-10;
    int accept=(int)(*pInBuf);
    struct ssv_softc *sc=ssv_smartlink_sc;
#ifdef KSMARTLINK_DEBUG
    printk(KERN_INFO "%s\n", __FUNCTION__);
#endif
    if (!sc)
    {
        goto out;
    }
    mutex_lock(&sc->mutex);
    ret = ssv6xxx_get_promisc(sc, &accept);
    mutex_unlock(&sc->mutex);
    *pOutBuf = accept;
    *pOutBufLen = 1;
#ifdef KSMARTLINK_DEBUG
    printk(KERN_INFO "%s %d\n", __FUNCTION__, accept);
#endif
out:
    return ret;
}
#define SMARTLINK_CMD_FIXED_LEN (10)
#define SMARTLINK_CMD_FIXED_TOT_LEN (SMARTLINK_CMD_FIXED_LEN+1)
#define SMARTLINK_RES_FIXED_LEN (SMARTLINK_CMD_FIXED_LEN)
#define SMARTLINK_RES_FIXED_TOT_LEN (SMARTLINK_RES_FIXED_LEN+2)
struct ksmartlink_cmd
{
    char *cmd;
    int (*process_func)(u8 *, u32, u8 *, u32 *);
};
static struct ksmartlink_cmd _ksmartlink_cmd_table[] =
{
    {"startairki", _ksmartlink_start_smartlink},
    {"stopairkis", _ksmartlink_stop_smartlink},
    {"setchannel", _ksmartlink_set_channel},
    {"getchannel", _ksmartlink_get_channel},
    {"setpromisc", _ksmartlink_set_promisc},
    {"getpromisc", _ksmartlink_get_promisc},
};
static u32 _ksmartlink_cmd_table_size=sizeof(_ksmartlink_cmd_table)/sizeof(struct ksmartlink_cmd);
#ifdef KSMARTLINK_DEBUG
static void _ksmartlink_hex_dump(u8 *pInBuf, u32 inBufLen)
{
    u32 i=0;
    printk(KERN_INFO "\nKernel Hex Dump(len=%d):\n", inBufLen);
    printk(KERN_INFO ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
    for (i=0; i<inBufLen; i++)
    {
        if ((i) && ((i & 0xf) == 0))
        {
            printk("\n");
        }
        printk("%02x ", pInBuf[i]);
    }
    printk(KERN_INFO "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
}
#endif
static int _ksmartlink_process_msg(u8 *pInBuf, u32 inBufLen, u8 *pOutBuf, u32 *pOutBufLen)
{
    int ret=0;
    u32 i=0;
    struct ksmartlink_cmd *pCmd;
    if (!pInBuf || !pOutBuf || !pOutBufLen)
    {
        printk(KERN_ERR "NULL pointer\n");
        return -1;
    }
    for (i=0; i<_ksmartlink_cmd_table_size; i++)
    {
        if (!strncmp(_ksmartlink_cmd_table[i].cmd, pInBuf, SMARTLINK_CMD_FIXED_LEN))
        {
            break;
        }
    }
    if (i < _ksmartlink_cmd_table_size)
    {
        pCmd = &_ksmartlink_cmd_table[i];
        if (!pCmd->process_func)
        {
            printk(KERN_ERR "CMD %s has NULL process_func\n", pCmd->cmd);
            return -3;
        }
        ret = pCmd->process_func(pInBuf+SMARTLINK_CMD_FIXED_LEN, inBufLen, pOutBuf, pOutBufLen);
    #ifdef CONFIG_SSV_NETLINK_RESPONSE
        if (ret < 0)
        {
            *pOutBufLen = SMARTLINK_RES_FIXED_TOT_LEN;
        }
        else
        {
            if (*pOutBufLen > 0)
            {
                pOutBuf[SMARTLINK_RES_FIXED_LEN] = (u8)ret;
                pOutBuf[SMARTLINK_RES_FIXED_LEN+1]= *pOutBuf;
            }
            else
            {
                pOutBuf[SMARTLINK_RES_FIXED_LEN] = (u8)ret;
                pOutBuf[SMARTLINK_RES_FIXED_LEN+1]= 0;
            }
            *pOutBufLen = SMARTLINK_RES_FIXED_TOT_LEN;
        }
        memcpy(pOutBuf, pCmd->cmd, SMARTLINK_RES_FIXED_LEN);
    #else
        (void)pOutBuf;
        (void)pOutBufLen;
    #endif
        return 0;
    }
    else
    {
        printk(KERN_INFO "Unknow CMD or Packet?\n");
    }
    return 0;
}
static u8 gkBuf[MAX_PAYLOAD]={0};
static int ssv_usr_pid=0;
void smartlink_nl_recv_msg(struct sk_buff *skb)
{
    struct nlmsghdr *nlh;
#ifdef CONFIG_SSV_NETLINK_RESPONSE
    struct sk_buff *skb_out;
#endif
    int ret=0;
    u8 *pInBuf=NULL;
    u32 inBufLen=0;
    u32 outBufLen=0;
    nlh = (struct nlmsghdr *)skb->data;
    ssv_usr_pid = nlh->nlmsg_pid;
    pInBuf = (u8 *)nlmsg_data(nlh);
    inBufLen = nlmsg_len(nlh);
    #ifdef KSMARTLINK_DEBUG
    _ksmartlink_hex_dump(pInBuf, inBufLen);
    #endif
    outBufLen = 0;
    memset(gkBuf, 0, MAX_PAYLOAD);
    ret = _ksmartlink_process_msg(pInBuf, inBufLen, gkBuf, &outBufLen);
#ifdef CONFIG_SSV_NETLINK_RESPONSE
    if (outBufLen == 0)
    {
        memcpy(gkBuf, "Nothing", 8);
        outBufLen = strlen(gkBuf);
    }
    skb_out = nlmsg_new(outBufLen, 0);
    if (!skb_out)
    {
        printk(KERN_ERR "Failed to allocate new skb\n");
        return;
    }
    nlh = nlmsg_put(skb_out, 0, 0, NLMSG_DONE, outBufLen, 0);
    NETLINK_CB(skb_out).dst_group = 0;
    memcpy(nlmsg_data(nlh), gkBuf, outBufLen);
    ret = nlmsg_unicast(nl_sk, skb_out, ssv_usr_pid);
    if (ret < 0)
    {
        printk(KERN_ERR "Error while sending bak to user\n");
    }
#endif
    return;
}
void smartlink_nl_send_msg(struct sk_buff *skb)
{
    struct nlmsghdr *nlh;
    struct sk_buff *skb_out;
    int ret=0;
    u8 *pOutBuf=skb->data;
    u32 outBufLen=skb->len;
    #ifdef KSMARTLINK_DEBUG
    #endif
    skb_out = nlmsg_new(outBufLen, 0);
    if (!skb_out)
    {
        printk(KERN_ERR "Allocate new skb failed!\n");
        return;
    }
    nlh = nlmsg_put(skb_out, 0, 0, NLMSG_DONE, outBufLen, 0);
    NETLINK_CB(skb_out).dst_group = 0;
    memcpy(nlmsg_data(nlh), pOutBuf, outBufLen);
    ret = nlmsg_unicast(nl_sk, skb_out, ssv_usr_pid);
    if (ret < 0)
    {
        printk(KERN_ERR "nlmsg_unicast failed!\n");
    }
    kfree_skb(skb);
    return;
}
EXPORT_SYMBOL(smartlink_nl_send_msg);
int ksmartlink_init(void)
{
#if LINUX_VERSION_CODE < KERNEL_VERSION(3,6,0)
    nl_sk = netlink_kernel_create(&init_net,
                                  NETLINK_SMARTLINK,
                                  0,
                                  smartlink_nl_recv_msg,
                                  NULL,
                                  THIS_MODULE);
#else
    struct netlink_kernel_cfg cfg =
            {
                .groups = 0,
                .input = smartlink_nl_recv_msg,
            };
    nl_sk = netlink_kernel_create(&init_net,
                                  NETLINK_SMARTLINK,
                                  &cfg);
#endif
    printk(KERN_INFO "***************SmartLink Init-S**************\n");
    if(!nl_sk)
    {
        printk(KERN_ERR "Error creating socket.\n");
        return -10;
    }
    printk(KERN_INFO "***************SmartLink Init-E**************\n");
    return 0;
}
void ksmartlink_exit(void)
{
    printk(KERN_INFO "%s\n", __FUNCTION__);
    if (nl_sk)
    {
        netlink_kernel_release(nl_sk);
        nl_sk = NULL;
    }
}
#if (defined(CONFIG_SSV_SUPPORT_ANDROID)||defined(CONFIG_SSV_BUILD_AS_ONE_KO))
EXPORT_SYMBOL(ksmartlink_init);
EXPORT_SYMBOL(ksmartlink_exit);
#else
module_init(ksmartlink_init);
module_exit(ksmartlink_exit);
#endif