blob: c2c05fc98650087ee06efb328b4afabd6f73caae [file] [log] [blame]
lh9ed821d2023-04-07 01:36:19 -07001/**
2 * @file Netapi.c
3 * @brief Implementation of Sanechips
4 *
5 * Copyright (C) 2017 Sanechips Technology Co., Ltd.
6 * @author linxu Gebin
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
11 *
12 */
13
14#include <stdio.h>
15#include <string.h>
16#include <stdlib.h>
17#include <assert.h>
18#include <syslog.h>
19#include <sys/klog.h>
20#include <sys/msg.h>
21#include <sys/socket.h>
22#include <linux/sockios.h>
23#include <sys/un.h>
24#include <errno.h>
25#include <sys/types.h>
26#include <sys/wait.h>
27#include "softap_api.h"
28#include <net/if.h>
29
30/************************ ÍøÂç½Ó¿Ú ************************/
31
32
33
34//¸ù¾ÝÍøÂçÉ豸Ãû»ñÈ¡¶ÔÓ¦µÄÉ豸ID,°´ÕÕÓÅÏȼ¶ÓëNVÖеÄÉ豸Ãû½øÐÐÆ¥Å䣬ʧ°Ü·µ»Ø-1
35int getDevId_byname(char *dev_name)
36{
37 char ps_wan[NETIF_NAME_LEN] = {0};
38 char psext_wan1[NETIF_NAME_LEN] = {0};
39 char psext_wan2[NETIF_NAME_LEN] = {0};
40 char psext_wan3[NETIF_NAME_LEN] = {0};
41 char psext_wan4[NETIF_NAME_LEN] = {0};
42 char psext_wan5[NETIF_NAME_LEN] = {0};
43 char psext_wan6[NETIF_NAME_LEN] = {0};
44 char psext_wan7[NETIF_NAME_LEN] = {0};
45 char psext_wan8[NETIF_NAME_LEN] = {0};
46 char swwanstr[NETIF_NAME_LEN] = {0};
47 char swlanstr[NETIF_NAME_LEN] = {0};
48 char eth_wan[NETIF_NAME_LEN] = {0};
49 char wifi_wan[NETIF_NAME_LEN] = {0};
50 char eth_lan[NETIF_NAME_LEN] = {0};
51 char wifi_lan[NETIF_NAME_LEN] = {0};
52 char usb_lan[NETIF_NAME_LEN] = {0};
53 char sw_name[NV_NAME_LEN] = {0};
54 char lan_enable[NV_NAME_LEN] = {0};
55 printf("netapi:getDevId_byname start!dev_name=%s\n", dev_name);
56
57 //ȱʡÍâÍø¿ÚΪ¿ÕʱºòÒ²¸øÖ÷¿Ø·¢ÏûÏ¢£¬add by zpm 20160628 begin
58 if (strlen(dev_name) == 0) {
59 return NO_DEV;
60 }
61 //ȱʡÍâÍø¿ÚΪ¿ÕʱºòÒ²¸øÖ÷¿Ø·¢ÏûÏ¢£¬add by zpm 20160628 end
62
63 sc_cfg_get("pswan", ps_wan, sizeof(ps_wan));
64 sc_cfg_get("ps_ext1", psext_wan1, sizeof(psext_wan1));
65 sc_cfg_get("ps_ext2", psext_wan2, sizeof(psext_wan2));
66 sc_cfg_get("ps_ext3", psext_wan3, sizeof(psext_wan3));
67 sc_cfg_get("ps_ext4", psext_wan4, sizeof(psext_wan4));
68 sc_cfg_get("ps_ext5", psext_wan5, sizeof(psext_wan1));
69 sc_cfg_get("ps_ext6", psext_wan6, sizeof(psext_wan2));
70 sc_cfg_get("ps_ext7", psext_wan7, sizeof(psext_wan3));
71 sc_cfg_get("ps_ext8", psext_wan8, sizeof(psext_wan4));
72 sc_cfg_get("wifiwan", wifi_wan, sizeof(wifi_wan));
73 sc_cfg_get("swwanstr", swwanstr, sizeof(swwanstr));
74 sc_cfg_get("swlanstr", swlanstr, sizeof(swlanstr));
75 sc_cfg_get("ethwan", eth_wan, sizeof(eth_wan));
76 sc_cfg_get("ethlan", eth_lan, sizeof(eth_lan));
77 sc_cfg_get("wifilan", wifi_lan, sizeof(wifi_lan));
78 sc_cfg_get("usblan", usb_lan, sizeof(usb_lan));
79 sc_cfg_get("sw", sw_name, sizeof(sw_name));
80 sc_cfg_get("LanEnable", lan_enable, sizeof(lan_enable));
81
82 if (!strncmp(dev_name, ps_wan, strlen(dev_name)))
83 return PS_NET_DEV;
84 if ((0 != strcmp(lan_enable, "0")) && !strncmp(dev_name, usb_lan, strlen(dev_name)))
85 return USB_LAN_DEV;
86 if (!strncmp(dev_name, psext_wan1, strlen(dev_name)))
87 return PS_EXT_DEV1;
88 if (!strncmp(dev_name, psext_wan2, strlen(dev_name)))
89 return PS_EXT_DEV2;
90 if (!strncmp(dev_name, psext_wan3, strlen(dev_name)))
91 return PS_EXT_DEV3;
92 if (!strncmp(dev_name, psext_wan4, strlen(dev_name)))
93 return PS_EXT_DEV4;
94 if (!strncmp(dev_name, psext_wan5, strlen(dev_name)))
95 return PS_EXT_DEV5;
96 if (!strncmp(dev_name, psext_wan6, strlen(dev_name)))
97 return PS_EXT_DEV6;
98 if (!strncmp(dev_name, psext_wan7, strlen(dev_name)))
99 return PS_EXT_DEV7;
100 if (!strncmp(dev_name, psext_wan8, strlen(dev_name)))
101 return PS_EXT_DEV8;
102 //µ±½öÒ»¸öRJ45Íø¿Úʱ£¬Ê¼ÖÕµ±ÍâÍø¿ÚÓã¬ÔÙ¼ÓÉÏeth_type£¬¿ÉÒÔʵÏÖÄÚÍø¹¦ÄÜ
103 if (!strncmp(dev_name, eth_wan, strlen(dev_name)) || !strncmp(dev_name, swwanstr, strlen(dev_name)))
104 return RJ45_WAN_DEV;
105 //µ±´æÔÚÁ½¸öRJ45Íø¿Úʱ£¬ÔÚ´ËÉèÖÃÒ»¸ö¹Ì¶¨µÄÄÚÍøRJ45Íø¿Ú
106 if (!strncmp(dev_name, eth_lan, strlen(dev_name)) || !strncmp(dev_name, swlanstr, strlen(dev_name)))
107 return RJ45_LAN_DEV;
108 if (!strncmp(dev_name, wifi_wan, strlen(dev_name)))
109 return WIFI_WAN_DEV;
110 if (!strncmp(dev_name, wifi_lan, strlen(dev_name)))
111 return WIFI_LAN_DEV;
112
113 return -1;
114}
115
116/**************************************************************************
117* º¯ÊýÃû³Æ£º set_pswanstate
118* ¹¦ÄÜÃèÊö£º ÉèÖÃpsÍøÂçÉ豸IPv6״̬NVÖµ
119* ²ÎÊý˵Ã÷£º c_id(IN) :PDP¼¤»îµÄc_id
120* state(IN):ÍøÂçÉ豸״̬
121* ·µ »Ø Öµ£º
122* ÆäËü˵Ã÷£º
123**************************************************************************/
124
125static void set_pswan_v6state(int c_id, int state)
126{
127 char ps_wan[NV_NAME_LEN] = {0};
128 char pswan_state[NETIF_NAME_LEN] = {0};
129
130 sc_cfg_get("pswan", ps_wan, sizeof(ps_wan));
131
132 snprintf(pswan_state, sizeof(pswan_state), "%s%d_ipv6_state", ps_wan, c_id);
133
134 if (state == DEV_STATE_WORKING) {
135 sc_cfg_set(pswan_state, "working");
136 return;
137 }
138
139 if (state == DEV_STATE_CONNECT) {
140 sc_cfg_set(pswan_state, "connect");
141 return;
142 }
143 if (state == DEV_STATE_DEAD) {
144 sc_cfg_set(pswan_state, "dead");
145 return;
146 }
147}
148
149static int pdp_state_check(struct pdp_active_info *actinfo)
150{
151 char pdp_act_state[NV_NAME_LEN] = {0};
152 char nv_pdp_state[NV_NAME_LEN] = {0};
153 sprintf(nv_pdp_state, "pdp_act_state_%d", actinfo->c_id);
154 sc_cfg_get(nv_pdp_state, pdp_act_state, sizeof(pdp_act_state));
155 if (0 != strcmp(pdp_act_state, "act")) {
156 sc_cfg_set(nv_pdp_state, "acting");
157 return 0;
158 }
159 return 1;
160}
161
162static void plugin_state_set(unsigned char netdev_id)
163{
164 int plugstate;
165 char rj45_pluginstate[NV_NAME_LEN] = {0};
166 char temp[NV_NAME_LEN] = {0};
167 sc_cfg_get("rj45_pluginstate", rj45_pluginstate, sizeof(rj45_pluginstate));
168
169 plugstate = atoi(rj45_pluginstate);
170 if (netdev_id == RJ45_WAN_DEV || netdev_id == SW_WAN_DEV) {
171 plugstate |= RJ45_WAN_IN;
172 sprintf(temp, "%d", plugstate);
173 sc_cfg_set("rj45_pluginstate", temp);
174 } else if (netdev_id == RJ45_LAN_DEV || netdev_id == SW_LAN_DEV) {
175 plugstate |= RJ45_LAN_IN;
176 sprintf(temp, "%d", plugstate);
177 sc_cfg_set("rj45_pluginstate", temp);
178 } else if (netdev_id == USB_LAN_DEV) {
179 plugstate = USB_LAN_IN;
180 sprintf(temp, "%d", plugstate);
181 sc_cfg_set("usb_pluginstate", temp);
182 } else if (netdev_id >= PS_EXT_DEV1 && netdev_id <= PS_EXT_DEV8) {
183 sprintf(temp, "psext%d_pluginstate", netdev_id);
184 sc_cfg_set(temp, "1");
185 }
186}
187
188static void plugout_state_set(unsigned char netdev_id)
189{
190 int plugstate;
191 char rj45_pluginstate[NV_NAME_LEN] = {0};
192 char temp[NV_NAME_LEN] = {0};
193
194 sc_cfg_get("rj45_pluginstate", rj45_pluginstate, sizeof(rj45_pluginstate));
195 plugstate = atoi(rj45_pluginstate);
196
197 if (netdev_id == RJ45_WAN_DEV || netdev_id == SW_WAN_DEV) {
198 plugstate &= ~RJ45_WAN_IN;
199 sprintf(temp, "%d", plugstate);
200 sc_cfg_set("rj45_pluginstate", temp);
201 } else if (netdev_id == RJ45_LAN_DEV || netdev_id == SW_LAN_DEV) {
202 plugstate &= ~RJ45_LAN_IN;
203 sprintf(temp, "%d", plugstate);
204 sc_cfg_set("rj45_pluginstate", temp);
205 } else if (netdev_id == USB_LAN_DEV) {
206 plugstate = USB_LAN_OUT;
207 sprintf(temp, "%d", plugstate);
208 sc_cfg_set("usb_pluginstate", temp);
209 } else if (netdev_id >= PS_EXT_DEV1 && netdev_id <= PS_EXT_DEV8) {
210 sprintf(temp, "psext%d_pluginstate", netdev_id);
211 sc_cfg_set(temp, "0");
212 }
213}
214
215/**************************************************************************
216* º¯ÊýÃû³Æ£º pdp_act_proc
217* ¹¦ÄÜÃèÊö£º PSÍøÂçÉ豸¼¤»î½Ó¿Ú
218* ²ÎÊý˵Ã÷£º pdp_active_info(IN):PDP¼¤»îÐÅÏ¢
219* ·µ »Ø Öµ£º ²Ù×÷³É¹¦·µ»Ø1.
220* ÆäËü˵Ã÷£º atÏà¹ØÓ¦Óýø³Ìµ÷Óã¬Èçatserver¡¢pcserver¡¢VoLTE¡¢²ÊÐŵÈÓ¦Óýø³Ì£»
221* ½Ó¿ÚÄÚ²¿ÊµÏÖ¶ÔÓÚµÄPSÉ豸µÄ¼¤»î¼°ÏàÓ¦µÄÍøÂçÅäÖÃ
222**************************************************************************/
223int ppp_act_proc(char *ttyname)
224{
225 char cmd[200] = {0};
226 char path_sh[50] = {0};
227
228 slog(NET_PRINT, SLOG_ERR, "netapi:calling ppp_act_proc, %s\n", ttyname);
229 sc_cfg_set("ppp_pdpstate", "");
230 sc_cfg_get("path_sh", path_sh, sizeof(path_sh));
231 if (!ttyname)
232 return -1;
233 sprintf(cmd, "%s/pppd_up.sh %s", path_sh, ttyname);
234 slog(NET_PRINT, SLOG_ERR, "cmd is %s\n", cmd);
235 soft_system(cmd);
236 return 0;
237}
238
239int pdp_act_proc(struct pdp_active_info *actinfo, int src_id)
240{
241 int ret = 0;
242 //klocwork
243 //int c_id = actinfo->c_id;
244 //unsigned char ip46flag = actinfo->act_info.ip46flag;
245
246 system("cat /proc/uptime 1>dev/console 2>&1");
247
248 if (!actinfo || actinfo->act_info.ip46flag == V4V6_INVALID) {
249 slog(NET_PRINT, SLOG_ERR, "pdp_act_proc, src_id = %d, actinfo->act_info.ip46flag == V4V6_INVALID\n", src_id);
250
251 softap_assert("ip46flag error"); //klocwork
252 return -1;
253 }
254 slog(NET_PRINT, SLOG_ERR, "netapi:pdp_act_proc: src_id=0x%x, cid=%d pdp_type=%u ip46flag=%u\n", src_id, actinfo->c_id, actinfo->pdp_type, actinfo->act_info.ip46flag);
255
256#if 1 // cov M actinfo->pdp_type is unsigned, could not compare with 0
257 if (PDP_PPP < actinfo->pdp_type) {
258#else
259 if (PDP_PPP < actinfo->pdp_type || PDP_LOCAL > actinfo->pdp_type) {
260#endif
261
262 slog(NET_PRINT, SLOG_ERR, "pdp_act_proc, src_id = %d, pdp_type is error: cid=%d pdp_type=%u ip46flag=%u\n", src_id, actinfo->c_id, actinfo->pdp_type, actinfo->act_info.ip46flag);
263 softap_assert("pdp_type=%u error", actinfo->pdp_type);
264 return -1;
265 }
266
267 if (1 == pdp_state_check(actinfo)) {
268
269 slog(NET_PRINT, SLOG_ERR, "pdp_act_init: already act c_id = %d, src_id = %d\n", actinfo->c_id, src_id);
270 //softap_assert("already act");
271 return -1;
272 }
273
274 if (actinfo->act_info.ip46flag == V6_VALID || actinfo->act_info.ip46flag == V46_VALID) {
275 set_pswan_v6state(actinfo->c_id, DEV_STATE_CONNECT);
276 }
277
278 ret = ipc_send_message(src_id, MODULE_ID_MAIN_CTRL, MSG_CMD_NET_PDP_ACT, sizeof(struct pdp_active_info), (UCHAR *)actinfo, 0); /*lint !e26 !e55*/
279 if (0 != ret) {
280 softap_assert("send msg to zte_router error");
281 return -1;
282 }
283
284 slog(NET_PRINT, SLOG_ERR, "pdp_act_proc: success!\n");
285 return 0;
286}
287
288/**************************************************************************
289* º¯ÊýÃû³Æ£º pdp_deact_proc
290* ¹¦ÄÜÃèÊö£º PSÍøÂçÉ豸ȥ»î½Ó¿Ú
291* ²ÎÊý˵Ã÷£º c_id(IN):PDP¼¤»îc_id.
292* ·µ »Ø Öµ£º ²Ù×÷³É¹¦·µ»Ø1.
293* ÆäËü˵Ã÷£º atÏà¹ØÓ¦Óýø³Ìµ÷Óã¬Èçatserver¡¢pcserver¡¢VoLTE¡¢²ÊÐŵÈÓ¦Óýø³Ì£»
294½Ó¿ÚÄÚ²¿ÊµÏÖ¶ÔÓÚµÄPSÉ豸ȥ»î£»Ö§³Ö46µ¥Õ»È¥»î£¬ËùÒÔÈë²ÎÐèҪָʾ46Çé¿ö
295**************************************************************************/
296int pdp_deact_proc(int c_id, unsigned char ip46flag, int src_id)
297{
298 int ret = 0;
299 struct pdp_deactive_info temp;
300 char nv_pdp_state[NV_NAME_LEN] = {0};
301
302 slog(NET_PRINT, SLOG_ERR, "netapi:pdp_deact_proc: src_id=%d, cid=%d ip46flag=%u\n", src_id, c_id, ip46flag);
303
304 sprintf(nv_pdp_state, "pdp_act_state_%d", c_id);
305
306 temp.c_id = c_id;
307 temp.ip46flag = ip46flag;
308
309 if (ip46flag == V4V6_INVALID) {
310 slog(NET_PRINT, SLOG_ERR, "pdp_deact_proc,src_id = %d, ip46flag is error: cid=%d ip46flag=%u\n", src_id, c_id, ip46flag);
311 softap_assert("ip46flag=%u error", ip46flag);
312 return -1;
313 }
314
315 sc_cfg_set(nv_pdp_state, "deacting");
316 ret = ipc_send_message(src_id, MODULE_ID_MAIN_CTRL, MSG_CMD_NET_PDP_DEACT, sizeof(struct pdp_deactive_info), (UCHAR *)&temp, 0); /*lint !e26 !e55*/
317 if (0 != ret) {
318 softap_assert("send msg to zte_router error");
319 return -1;
320 }
321 slog(NET_PRINT, SLOG_ERR, "pdp_deact_proc: success!\n");
322 return 0;
323}
324
325/**************************************************************************
326* º¯ÊýÃû³Æ£º netdev_connect_internet
327* ¹¦ÄÜÃèÊö£º webui·¢Ë͵ÄÁ¬½ÓÇëÇó,½Ó¿ÚÄÚ²¿½øÐÐifconfig up£¬²¢Æô¶¯ÏàÓ¦µÄµØÖ··ÖÅä½ø³Ì
328* ²ÎÊý˵Ã÷£º netdev_id£ºÍøÂçÉ豸¶ÔÓ¦µÄºêÃû£¬²Î¼ûUSB_NET_DEVµÈºêÖµ
329* ·µ »Ø Öµ£º ²Ù×÷³É¹¦·µ»Ø1.
330* ÆäËü˵Ã÷£º É豸Ö÷¿ØÄ£¿éµ÷Óô˽ӿÚǰ£¬ÐèÒª¼ì²âµ±Ç°É豸ÎïÀíͨ·ÒÑͨ£¬·ñÔò²»Ó¦¸Ãµ÷Óô˽ӿڣ»
331* Ö÷¿ØÄ£¿éÐèÒªÑÏ¿ØÖظ´µÄµ÷Óøýӿڣ¬ÒòΪÿµ÷ÓÃÒ»´Î¸Ã½Ó¿Ú£¬ÄÚ²¿½Ô»áÖØÐÂÖ´ÐÐÒ»´ÎÍêÕûµÄµØÖ··ÖÅäÁ÷³Ì£»
332**************************************************************************/
333int netdev_connect_internet(unsigned char netdev_id, int src_id)
334{
335 int ret = 0;
336
337 slog(NET_PRINT, SLOG_ERR, "netapi:netdev_connect_internet: netdev_id=%u\n", netdev_id);
338
339 if (netdev_id != RJ45_WAN_DEV && netdev_id != WIFI_WAN_DEV && netdev_id != RJ45_LAN_DEV && netdev_id != WIFI_LAN_DEV) {
340 slog(NET_PRINT, SLOG_ERR, "connect_internet:%u don't support connect op!\n", netdev_id);
341 softap_assert("netdev_id=%d error", netdev_id);
342 return 0;
343 }
344
345 ret = ipc_send_message(src_id, MODULE_ID_MAIN_CTRL, MSG_CMD_NET_CONNECT, sizeof(unsigned char), (UCHAR *)&netdev_id, 0); /*lint !e26 !e55*/
346 if (0 != ret) {
347 softap_assert("send msg to zte_router error");
348 return 0;
349 }
350 return 1;
351}
352
353/**************************************************************************
354* º¯ÊýÃû³Æ£º netdev_disconnect_internet
355* ¹¦ÄÜÃèÊö£º webui·¢Ë͵ĶϿªÁ¬½ÓÇëÇó,½Ó¿ÚÄÚ²¿½øÐÐifconfig down£¬²¢É±µôÏàÓ¦µÄµØÖ··ÖÅä½ø³Ì
356* ²ÎÊý˵Ã÷£º netdev_id£ºÍøÂçÉ豸¶ÔÓ¦µÄºêÃû£¬²Î¼ûUSB_NET_DEVµÈºêÖµ
357* ·µ »Ø Öµ£º ²Ù×÷³É¹¦·µ»Ø1.
358* ÆäËü˵Ã÷£º É豸Ö÷¿ØÄ£¿éµ÷Óô˽ӿÚ
359**************************************************************************/
360int netdev_disconnect_internet(unsigned char netdev_id, int src_id)
361{
362 int ret = 0;
363
364 printf("netapi:netdev_disconnect_internet start!netdev_id=%u\n", netdev_id);
365 slog(NET_PRINT, SLOG_ERR, "netapi:netdev_disconnect_internet: netdev_id=%u\n", netdev_id);
366
367 if (netdev_id != RJ45_WAN_DEV && netdev_id != WIFI_WAN_DEV && netdev_id != RJ45_LAN_DEV && netdev_id != WIFI_LAN_DEV) {
368 slog(NET_PRINT, SLOG_ERR, "disconnect_internet:%u don't support disconnect op!\n", netdev_id);
369 softap_assert("netdev_id=%d error", netdev_id);
370
371 return 0;
372 }
373
374 ret = ipc_send_message(src_id, MODULE_ID_MAIN_CTRL, MSG_CMD_NET_DISCON, sizeof(unsigned char), (UCHAR *)&netdev_id, 0); /*lint !e26 !e55*/
375 if (0 != ret) {
376 softap_assert("send msg to zte_router error");
377 return 0;
378 }
379 return 1;
380}
381
382/**************************************************************************
383* º¯ÊýÃû³Æ£º netdev_plugin_proc_byname
384* ¹¦ÄÜÃèÊö£º ÈȲå°ÎÉ豸²åÈë,½Ó¿ÚÄÚ²¿¼ì²âÊÇ·ñÐèÒª½øÐÐ×Ô¶¯²¦ºÅ£¬ÈçÐèÒª£¬Ö´ÐеØÖ··ÖÅä¡¢×éÍøµÈÁ÷³Ì
385* ²ÎÊý˵Ã÷£º netdev_name£ºÍøÂçÉ豸Ãû£¬source_id plug¼ì²âÄ£¿éID
386* ·µ »Ø Öµ£º ²Ù×÷³É¹¦·µ»Ø1.
387* ÆäËü˵Ã÷£º É豸Ö÷¿Øµ÷Óô˽ӿÚʱ£¬±íÃ÷¸ÃÍøÂçÉ豸ÎïÀíͨ·ÒÑͨ£¬¿ÉÒÔ½øÐÐIPÊý¾Ý°üµÄÊÕ·¢ÁË
388**************************************************************************/
389int netdev_plugin_proc_byname(char *netdev_name, int src_id)
390{
391 int netdev_id = 0;
392 slog(NET_PRINT, SLOG_ERR, "netdev_plugin_byname, netdev %s is plugin, src_id = %d\n", netdev_name, src_id);
393 netdev_id = getDevId_byname(netdev_name);
394
395 if (netdev_id == -1) {
396 slog(NET_PRINT, SLOG_ERR, "netdev_plugin_proc_byname,netdev_name is error,please connect main control");
397 return -1;
398 } else {
399 return netdev_plugin_proc(netdev_id, src_id);
400 }
401}
402
403/**************************************************************************
404* º¯ÊýÃû³Æ£º netdev_plugout_proc
405* ¹¦ÄÜÃèÊö£º ÈȲå°ÎÉ豸°Î³ö,½Ó¿ÚÄÚ²¿½øÐÐifconfig down£¬²¢É±µôÏàÓ¦µÄµØÖ··ÖÅä½ø³Ì
406* ²ÎÊý˵Ã÷£º netdev_name£ºÍøÂçÉ豸Ãû£¬source_id plug¼ì²âÄ£¿éID
407* ·µ »Ø Öµ£º ²Ù×÷³É¹¦·µ»Ø1.
408* ÆäËü˵Ã÷£º É豸Ö÷¿Øµ÷Óô˽ӿÚʱ£¬±íÃ÷¸ÃÍøÂçÉ豸ÎïÀíͨ·ÒѶϣ¬²»ÒªÔÙ½øÐÐIPÊý¾Ý°üÊÕ·¢
409**************************************************************************/
410int netdev_plugout_proc_byname(char *netdev_name, int src_id)
411{
412 int netdev_id = 0;
413 slog(NET_PRINT, SLOG_ERR, "netdev_plugout_byname, netdev %s is plugout, src_id = %d\n", netdev_name, src_id);
414 netdev_id = getDevId_byname(netdev_name);
415
416 if (netdev_id == -1) {
417 slog(NET_PRINT, SLOG_ERR, "netdev_plugout_proc_byname,netdev_name is error,please connect main control");
418 return -1;
419 } else {
420 return netdev_plugout_proc(netdev_id, src_id);
421 }
422}
423
424/**************************************************************************
425* º¯ÊýÃû³Æ£º netdev_plugin_proc
426* ¹¦ÄÜÃèÊö£º ÈȲå°ÎÉ豸²åÈë,½Ó¿ÚÄÚ²¿¼ì²âÊÇ·ñÐèÒª½øÐÐ×Ô¶¯²¦ºÅ£¬ÈçÐèÒª£¬Ö´ÐеØÖ··ÖÅä¡¢×éÍøµÈÁ÷³Ì
427* ²ÎÊý˵Ã÷£º netdev_id£ºÍøÂçÉ豸¶ÔÓ¦µÄºêÃû£¬²Î¼ûUSB_NET_DEVµÈºêÖµ
428* ·µ »Ø Öµ£º ²Ù×÷³É¹¦·µ»Ø1.
429* ÆäËü˵Ã÷£º É豸Ö÷¿Øµ÷Óô˽ӿÚʱ£¬±íÃ÷¸ÃÍøÂçÉ豸ÎïÀíͨ·ÒÑͨ£¬¿ÉÒÔ½øÐÐIPÊý¾Ý°üµÄÊÕ·¢ÁË
430**************************************************************************/
431int netdev_plugin_proc(unsigned char netdev_id, int src_id)
432{
433 int ret = 0;
434
435 printf("netapi:netdev_plugin_proc start!netdev_id=%u\n", netdev_id);
436 slog(NET_PRINT, SLOG_ERR, "netapi:netdev_plugin_proc: netdev_id=%u\n", netdev_id);
437 plugin_state_set(netdev_id);
438 ret = ipc_send_message2(src_id, MODULE_ID_MAIN_CTRL, MSG_CMD_NET_PLUGIN, sizeof(unsigned char), (UCHAR *)&netdev_id, 0); /*lint !e26 !e55*/
439
440 if (0 != ret) {
441 softap_assert("send msg to zte_router error");
442 return 0;
443 }
444#ifdef _USE_MBIM
445 slog(NET_PRINT, SLOG_ERR, "netapi:netdev_plugin_proc: send MSG_CMD_NET_PLUGIN to MBIM\n");
446 ipc_send_message2(src_id, MODULE_ID_MBIM, MSG_CMD_NET_PLUGIN, sizeof(unsigned char), (UCHAR *)&netdev_id, 0);
447#endif
448 return 1;
449}
450
451/**************************************************************************
452* º¯ÊýÃû³Æ£º netdev_plugout_proc
453* ¹¦ÄÜÃèÊö£º ÈȲå°ÎÉ豸°Î³ö,½Ó¿ÚÄÚ²¿½øÐÐifconfig down£¬²¢É±µôÏàÓ¦µÄµØÖ··ÖÅä½ø³Ì
454* ²ÎÊý˵Ã÷£º netdev_id£ºÍøÂçÉ豸¶ÔÓ¦µÄºêÃû£¬²Î¼ûUSB_NET_DEVµÈºêÖµ
455* ·µ »Ø Öµ£º ²Ù×÷³É¹¦·µ»Ø1.
456* ÆäËü˵Ã÷£º É豸Ö÷¿Øµ÷Óô˽ӿÚʱ£¬±íÃ÷¸ÃÍøÂçÉ豸ÎïÀíͨ·ÒѶϣ¬²»ÒªÔÙ½øÐÐIPÊý¾Ý°üÊÕ·¢
457**************************************************************************/
458int netdev_plugout_proc(unsigned char netdev_id, int src_id)
459{
460 int ret = 0;
461
462 printf("netapi:netdev_plugout_proc start!netdev_id=%u\n", netdev_id);
463 slog(NET_PRINT, SLOG_ERR, "netapi:netdev_plugout_proc: netdev_id=%u\n", netdev_id);
464 plugout_state_set(netdev_id);
465 slog(NET_PRINT, SLOG_ERR, "netdev_plugout_proc: netdev_id=%u\n", netdev_id);
466 ret = ipc_send_message2(src_id, MODULE_ID_MAIN_CTRL, MSG_CMD_NET_PLUGOUT, sizeof(unsigned char), (UCHAR *)&netdev_id, 0); /*lint !e26 !e55*/
467
468 if (0 != ret) {
469 softap_assert("send msg to zte_router error");
470 return 0;
471 }
472#ifdef _USE_MBIM
473 ipc_send_message2(src_id, MODULE_ID_MBIM, MSG_CMD_NET_PLUGOUT, sizeof(unsigned char), (UCHAR *)&netdev_id, 0);
474#endif
475 return 1;
476}
477/********************************************************************
478* º¯ÊýÃû³Æ£º tcpip_reset_configure
479* ¹¦ÄÜÃèÊö£º »Ö¸´³ö³§ÉèÖÃʱÓÉååzte_blc½ø³Ìµ÷Óã¬É¾³ý³ÌÐòÔËÐÐÖÐÉú³ÉµÄÎļþ
480* ²ÎÊý˵Ã÷£º ÎÞ
481* ·µ »Ø Öµ£º ÎÞ
482* ÆäËü˵Ã÷£º
483**********************************************************************/
484void tcpip_reset_configure()
485{
486 char cmd[200] = {0};
487 char path_script[50] = {0};
488 char path_conf[50] = {0};
489 char path_file[200] = {0};
490 printf("netapi:tcpip_reset_configure start!\n");
491 sc_cfg_get("path_conf", path_conf, sizeof(path_conf));
492 sprintf(path_file, "%s/hostname_mac_file", path_conf);
493
494
495 //»Ö¸´³ö³§ÉèÖÃʱɾ³ýhostname_mac_fileÎļþ
496 sprintf(cmd, "/bin/rm -rf %s", path_file);
497 zxic_system(cmd);
498 //»Ö¸´³ö³§ÉèÖÃʱɾ³ýstatic_macip_file Îļþ
499 memset(path_file, 0, sizeof(path_file));
500 sprintf(path_file, "%s/static_macip_file", path_conf);
501 memset(cmd, 0, sizeof(cmd));
502 sprintf(cmd, "/bin/rm -rf %s", path_file);
503 zxic_system(cmd);
504
505 memset(path_file, 0, sizeof(path_file));
506 sprintf(path_file, "%s/children_device_file", path_conf);
507 memset(cmd, 0, sizeof(cmd));
508 sprintf(cmd, "/bin/rm -rf %s", path_file);
509 zxic_system(cmd);
510
511 memset(path_file, 0, sizeof(path_file));
512 sprintf(path_file, "%s/white_site_file", path_conf);
513 memset(cmd, 0, sizeof(cmd));
514 sprintf(cmd, "/bin/rm -rf %s", path_file);
515 zxic_system(cmd);
516
517 /*reset DDNS status file*/
518 memset(path_file, 0, sizeof(path_file));
519 sprintf(path_file, "%s/inadyn.status", path_conf);
520 memset(cmd, 0, sizeof(cmd));
521 sprintf(cmd, "/bin/rm -rf %s", path_file);
522 zxic_system(cmd);
523
524 //»Ö¸´³ö³§ÉèÖÃʱ½«MAC-IP°ó¶¨¹æÔòÇå¿Õ
525 sc_cfg_get("path_sh", path_script, sizeof(path_script));
526 memset(cmd, 0, sizeof(cmd));
527 sprintf(cmd, "%s/config-udhcpd.sh \"lan\" -S", path_script);
528 soft_system(cmd);
529}
530
531/**************************************************************************
532* º¯ÊýÃû³Æ£º tc_control
533* ¹¦ÄÜÃèÊö£º Á÷Á¿¿ØÖÆ
534* ²ÎÊý˵Ã÷£º ÐèÒªÁ÷Á¿¼à¿ØµÄÉÏÏÂÐÐÁ¿£¬Êý¾Ý¿¨ÐèÒªÓÐcid£¬ufiģʽ²»ÐèÒª
535* 0±íʾ²»ÐèÒªÁ÷¿Ø£¬µ¥Î»:bit£¬Òòlinux±ê×¼ÃüÁîÊǶÔ×Ö½Ú´¦Àí£¬ËùÒÔ×îÖÕÅäÖÃÖµ»áÕû³ý8
536* ·µ »Ø Öµ£º ²Ù×÷³É¹¦·µ»Ø1.
537* ÆäËü˵Ã÷£º ¸øÐèÒªÁ÷Á¿¿ØÖƵÄÓ¦Óõ÷Óã¬ÈçÉÏÐÐÐèÒªÁ÷¿Ø£¬ÇëµÈÄÜÉÏÍâÍøºóÔÙµ÷Ó㬷ñÔòÎÞ·¨»ñÈ¡ÍâÍø¿Ú½øÐÐÁ÷¿Ø
538* Õý³£¹Ø»úÖØÐ¿ª»ú£¬Ö®Ç°ÉèÖõÄÁ÷¿ØÒÀÈ»ÓÐЧ
539**************************************************************************/
540int tc_control(struct tc_control_info *tcinfo, int src_id)
541{
542 int ret = 0;
543
544 printf("netapi:tc_control start! c_id:%d, down:%u, up:%u\n, src:%d \n", tcinfo->c_id, tcinfo->tc_downlink, tcinfo->tc_uplink, src_id);
545
546 ret = ipc_send_message(src_id, MODULE_ID_MAIN_CTRL, MSG_CMD_NET_TC_CTRL, sizeof(struct tc_control_info), (UCHAR *)tcinfo, 0);
547 if (0 != ret) {
548 softap_assert("send msg to zte_router error");
549 return 0;
550 }
551 return 1;
552}
553
554#define SIOCDEVPRIVATE 0X89F0
555#define SIOCVLAN SIOCDEVPRIVATE+0x0
556
557/**************************************************************************
558* º¯ÊýÃû³Æ£º set_ethwan_mode
559* ¹¦ÄÜÃèÊö£º ÉèÖÃswitchÉ豸eth0.200Íø¿Ú¶ÔÓ¦µÄport3Ϊwan¿Ú»¹ÊÇlan¿Ú
560* ²ÎÊý˵Ã÷£º mode:1 => wan¿Ú£¬0 => lan¿Ú
561* ·µ »Ø Öµ£º ÎÞ.
562* ÆäËü˵Ã÷£º
563**************************************************************************/
564void set_ethwan_port_mode(int mode)
565{
566 struct ifreq ifr;
567 int fd;
568 char eth_name[IFNAMSIZ] = {'e','t','h','0'};
569
570 if(mode != 0 && mode != 1)
571 {
572 return;
573 }
574 slog(NET_PRINT, SLOG_ERR, "set_ethwan_port_mode() mode=%d\n", mode);
575 if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
576 slog(NET_PRINT, SLOG_ERR, "set_eth200_mode() socket(AF_INET,SOCK_STREAM) fail");
577 return;
578 }
579 memset(&ifr, 0, sizeof(ifr));
580 memcpy((void*)ifr.ifr_name, (void*)eth_name, strlen("eth0"));//gmacÍø¿ÚÃû³Æeth0£¬ºóÐø¿¼ÂÇÊÇ·ñͨ¹ýÐÂÔönvÏî»ñÈ¡
581 ifr.ifr_data = (void*)&mode;
582
583 if (ioctl(fd, SIOCVLAN, &ifr) < 0 && errno != EINVAL) {
584 slog(NET_PRINT, SLOG_ERR, "set_eth200_mode() ioctl fail");
585 close(fd);
586 return;
587 }
588
589 close(fd);
590
591}
592
xf.lie31de8b2023-12-26 23:38:58 -0800593/*
594 * ether_to_eui64 - Convert 48-bit Ethernet address into 64-bit EUI
595 *
596 * convert the 48-bit MAC address of eth0 into EUI 64. caller also assumes
597 * that the system has a properly configured Ethernet interface for this
598 * function to return non-zero.
599 */
600int netapi_ether_to_eui64(const char *dev_name, netapi_eui64_t *p_eui64)
601{
xf.li84027492024-04-09 00:17:51 -0700602 struct ifreq ifr = {0};
xf.lie31de8b2023-12-26 23:38:58 -0800603 int skfd;
604 const unsigned char *ptr;
605
606 skfd = socket(PF_INET6, SOCK_DGRAM, 0);
607 if(skfd == -1)
608 {
609 slog(NET_PRINT, SLOG_ERR, "eui64 open IPv6 socket fail");
610 return 0;
611 }
612
xf.li84027492024-04-09 00:17:51 -0700613 strncpy(ifr.ifr_name, dev_name, sizeof(ifr.ifr_name)-1);//kw
xf.lie31de8b2023-12-26 23:38:58 -0800614 if(ioctl(skfd, SIOCGIFHWADDR, &ifr) < 0)
615 {
616 close(skfd);
617 slog(NET_PRINT, SLOG_ERR, "eui64 obtain hardware address for %s fail", dev_name);
618 return 0;
619 }
620 close(skfd);
621
622 /*
623 * And convert the EUI-48 into EUI-64, per RFC 2472 [sec 4.1]
624 */
625 ptr = ifr.ifr_hwaddr.sa_data;
626 p_eui64->e8[0] = ptr[0] | 0x02;
627 p_eui64->e8[1] = ptr[1];
628 p_eui64->e8[2] = ptr[2];
629 p_eui64->e8[3] = 0xFF;
630 p_eui64->e8[4] = 0xFE;
631 p_eui64->e8[5] = ptr[3];
632 p_eui64->e8[6] = ptr[4];
633 p_eui64->e8[7] = ptr[5];
634
635 return 1;
636}
lh9ed821d2023-04-07 01:36:19 -0700637