blob: 1ce5b245876aeae850b6dbee7f492603394a487c [file] [log] [blame]
xjb04a4022021-11-25 15:01:52 +08001#include <stdlib.h>
2#include <stdio.h>
3#include <string.h>
4#include <sys/ioctl.h>
5#include <fcntl.h>
6#include <getopt.h>
7#ifndef CONFIG_SUPPORT_OPENWRT
8#include <linux/autoconf.h>
9#endif
10#if defined CONFIG_COLGIN_SUPPORT
11#include "hnat_ioctl.h"
12#else
13#include "hwnat_ioctl.h"
14#endif
15
16int HwNatInvalEntry(unsigned int entry_num)
17{
18 struct hwnat_args opt;
19 int fd;
20
21 opt.entry_num=entry_num;
22
23 fd = open("/dev/"HW_NAT_DEVNAME, O_RDONLY);
24 if (fd < 0)
25 {
26 printf("Open %s pseudo device failed\n","/dev/"HW_NAT_DEVNAME);
27 return HWNAT_FAIL;
28 }
29
30 if(ioctl(fd, HW_NAT_INVALID_ENTRY, &opt)<0) {
31 printf("HW_NAT_API: ioctl error\n");
32 close(fd);
33 return HWNAT_FAIL;
34 }
35
36 close(fd);
37 return HWNAT_SUCCESS;
38}
39
40int HwNatDumpDport(void)
41{
42 int fd;
43 struct hwnat_args opt;
44
45 fd = open("/dev/"HW_NAT_DEVNAME, O_RDONLY);
46 if (fd < 0)
47 {
48 printf("Open %s pseudo device failed\n","/dev/"HW_NAT_DEVNAME);
49 return HWNAT_FAIL;
50 }
51 if(ioctl(fd, HW_NAT_DPORT, &opt)<0) {
52 printf("HW_NAT_API: ioctl error\n");
53 close(fd);
54 return HWNAT_FAIL;
55 }
56
57 close(fd);
58 return HWNAT_SUCCESS;
59}
60#if defined (CONFIG_HW_NAT_IPI)
61int HwNatIPICtrlFromExtIf(struct hwnat_ipi_args *opt)
62{
63 int fd;
64
65 fd = open("/dev/"HW_NAT_DEVNAME, O_RDONLY);
66 if (fd < 0)
67 {
68 printf("Open %s pseudo device failed\n","/dev/"HW_NAT_DEVNAME);
69 return HWNAT_FAIL;
70 }
71
72 if(ioctl(fd, HW_NAT_IPI_CTRL_FROM_EXTIF, opt)<0) {
73 printf("HW_NAT_API: ioctl error\n");
74 close(fd);
75 return HWNAT_FAIL;
76 }
77
78 close(fd);
79 return HWNAT_SUCCESS;
80}
81
82int HwNatIPICtrlFromPPEHit(struct hwnat_ipi_args *opt)
83{
84 int fd;
85
86 fd = open("/dev/"HW_NAT_DEVNAME, O_RDONLY);
87 if (fd < 0)
88 {
89 printf("Open %s pseudo device failed\n","/dev/"HW_NAT_DEVNAME);
90 return HWNAT_FAIL;
91 }
92
93 if(ioctl(fd, HW_NAT_IPI_CTRL_FROM_PPEHIT, opt)<0) {
94 printf("HW_NAT_API: ioctl error\n");
95 close(fd);
96 return HWNAT_FAIL;
97 }
98
99 close(fd);
100 return HWNAT_SUCCESS;
101}
102#endif
103
104int HwNatDelEntry(struct hwnat_tuple *opt)
105{
106 int fd;
107
108 fd = open("/dev/"HW_NAT_DEVNAME, O_RDONLY);
109 if (fd < 0)
110 {
111 printf("Open %s pseudo device failed\n","/dev/"HW_NAT_DEVNAME);
112 return HWNAT_FAIL;
113 }
114 if(ioctl(fd, HW_NAT_DEL_ENTRY, opt)<0) {
115 printf("HW_NAT_API: ioctl error\n");
116 close(fd);
117 return HWNAT_FAIL;
118 }
119
120 close(fd);
121 return HWNAT_SUCCESS;
122
123}
124
125int HwNatAddEntry(struct hwnat_tuple *opt)
126{
127 int fd;
128
129 fd = open("/dev/"HW_NAT_DEVNAME, O_RDONLY);
130 if (fd < 0)
131 {
132 printf("Open %s pseudo device failed\n","/dev/"HW_NAT_DEVNAME);
133 return HWNAT_FAIL;
134 }
135
136 if(ioctl(fd, HW_NAT_ADD_ENTRY, opt)<0) {
137 printf("HW_NAT_API: ioctl error\n");
138 close(fd);
139 return HWNAT_FAIL;
140 }
141
142 close(fd);
143 return HWNAT_SUCCESS;
144
145}
146
147int HwNatHookModify(struct hwnat_tuple *opt, int clear)
148{
149 int fd;
150
151 fd = open("/dev/"HW_NAT_DEVNAME, O_RDONLY);
152 if (fd < 0)
153 {
154 printf("Open %s pseudo device failed\n","/dev/"HW_NAT_DEVNAME);
155 return HWNAT_FAIL;
156 }
157 if(ioctl(fd, (clear == 1) ? HW_NAT_CLEAR_HOOK : HW_NAT_RESTORE_HOOK, opt)<0) {
158 printf("HW_NAT_API: ioctl error\n");
159 close(fd);
160 return HWNAT_FAIL;
161 }
162
163 close(fd);
164 return HWNAT_SUCCESS;
165
166}
167
168
169
170#if defined (CONFIG_PPE_MIB)
171int HwNatMibDumpEntry(unsigned int entry_num)
172{
173 struct hwnat_mib_args opt;
174 int fd;
175 opt.entry_num=entry_num;
176 printf("!!!!!!!!HwNatMibDumpEntry\n");
177 fd = open("/dev/"HW_NAT_DEVNAME, O_RDONLY);
178 if (fd < 0)
179 {
180 printf("Open %s pseudo device failed\n","/dev/"HW_NAT_DEVNAME);
181 return HWNAT_FAIL;
182 }
183
184 if(ioctl(fd, HW_NAT_MIB_DUMP, &opt)<0) {
185 printf("HW_NAT_API: ioctl error\n");
186 close(fd);
187 return HWNAT_FAIL;
188 }
189
190 close(fd);
191 return HWNAT_SUCCESS;
192}
193int HwNatMibDramDumpEntry(unsigned int entry_num)
194{
195 struct hwnat_mib_args opt;
196 int fd;
197
198 opt.entry_num=entry_num;
199
200 fd = open("/dev/"HW_NAT_DEVNAME, O_RDONLY);
201 if (fd < 0)
202 {
203 printf("Open %s pseudo device failed\n","/dev/"HW_NAT_DEVNAME);
204 return HWNAT_FAIL;
205 }
206
207 if(ioctl(fd, HW_NAT_MIB_DRAM_DUMP, &opt)<0) {
208 printf("HW_NAT_API: ioctl error\n");
209 close(fd);
210 return HWNAT_FAIL;
211 }
212
213 close(fd);
214 return HWNAT_SUCCESS;
215}
216
217int HwNatMibGet(struct hwnat_tuple *opt)
218{
219 int fd;
220
221 fd = open("/dev/"HW_NAT_DEVNAME, O_RDONLY);
222 if (fd < 0)
223 {
224 printf("Open %s pseudo device failed\n","/dev/"HW_NAT_DEVNAME);
225 return HWNAT_FAIL;
226 }
227
228 if(ioctl(fd, HW_NAT_MIB_GET, opt)<0) {
229 printf("HW_NAT_API: ioctl error\n");
230 close(fd);
231 return HWNAT_FAIL;
232 }
233
234 close(fd);
235 return HWNAT_SUCCESS;
236
237}
238#if defined CONFIG_COLGIN_SUPPORT
239int HwNatMibGetAllIP(struct hwnat_mib_all_ip_args *opt)
240{
241 int fd;
242
243 fd = open("/dev/"HW_NAT_DEVNAME, O_RDONLY);
244 if (fd < 0)
245 {
246 printf("Open %s pseudo device failed\n","/dev/"HW_NAT_DEVNAME);
247 return HWNAT_FAIL;
248 }
249
250 if(ioctl(fd, HW_NAT_MIB_GET_ALL_IP, opt)<0) {
251 printf("HW_NAT_API: ioctl error\n");
252 close(fd);
253 return HWNAT_FAIL;
254 }
255
256 close(fd);
257 return HWNAT_SUCCESS;
258}
259#endif
260#endif
261
262int HwNatTblClear(void)
263{
264 struct hwnat_args opt;
265 int fd;
266
267 fd = open("/dev/"HW_NAT_DEVNAME, O_RDONLY);
268 if (fd < 0)
269 {
270 printf("Open %s pseudo device failed\n","/dev/"HW_NAT_DEVNAME);
271 return HWNAT_FAIL;
272 }
273
274 if(ioctl(fd, HW_NAT_TBL_CLEAR, &opt)<0) {
275 printf("HW_NAT_API: ioctl error\n");
276 close(fd);
277 return HWNAT_FAIL;
278 }
279
280 close(fd);
281 return HWNAT_SUCCESS;
282}
283
284int HwNatDumpEntry(unsigned int entry_num)
285{
286 struct hwnat_args opt;
287 int fd;
288
289 opt.entry_num=entry_num;
290
291 fd = open("/dev/"HW_NAT_DEVNAME, O_RDONLY);
292 if (fd < 0)
293 {
294 printf("Open %s pseudo device failed\n","/dev/"HW_NAT_DEVNAME);
295 return HWNAT_FAIL;
296 }
297
298 if(ioctl(fd, HW_NAT_DUMP_ENTRY, &opt)<0) {
299 printf("HW_NAT_API: ioctl error\n");
300 close(fd);
301 return HWNAT_FAIL;
302 }
303
304 close(fd);
305 return HWNAT_SUCCESS;
306}
307
308int HwNatBindEntry(unsigned int entry_num)
309{
310 struct hwnat_args opt;
311 int fd;
312
313 opt.entry_num=entry_num;
314
315 fd = open("/dev/"HW_NAT_DEVNAME, O_RDONLY);
316 if (fd < 0)
317 {
318 printf("Open %s pseudo device failed\n","/dev/"HW_NAT_DEVNAME);
319 return HWNAT_FAIL;
320 }
321
322 if(ioctl(fd, HW_NAT_BIND_ENTRY, &opt)<0) {
323 printf("HW_NAT_API: ioctl error\n");
324 close(fd);
325 return HWNAT_FAIL;
326 }
327
328 close(fd);
329 return HWNAT_SUCCESS;
330}
331
332int HwNatUnBindEntry(unsigned int entry_num)
333{
334 struct hwnat_args opt;
335 int fd;
336
337 opt.entry_num=entry_num;
338
339 fd = open("/dev/"HW_NAT_DEVNAME, O_RDONLY);
340 if (fd < 0)
341 {
342 printf("Open %s pseudo device failed\n","/dev/"HW_NAT_DEVNAME);
343 return HWNAT_FAIL;
344 }
345
346 if(ioctl(fd, HW_NAT_UNBIND_ENTRY, &opt)<0) {
347 printf("HW_NAT_API: ioctl error\n");
348 close(fd);
349 return HWNAT_FAIL;
350 }
351
352 close(fd);
353 return HWNAT_SUCCESS;
354}
355
356int HwNatDropEntry(unsigned int entry_num)
357{
358 struct hwnat_args opt;
359 int fd;
360
361 opt.entry_num=entry_num;
362
363 fd = open("/dev/"HW_NAT_DEVNAME, O_RDONLY);
364 if (fd < 0)
365 {
366 printf("Open %s pseudo device failed\n","/dev/"HW_NAT_DEVNAME);
367 return HWNAT_FAIL;
368 }
369
370 if(ioctl(fd, HW_NAT_DROP_ENTRY, &opt)<0) {
371 printf("HW_NAT_API: ioctl error\n");
372 close(fd);
373 return HWNAT_FAIL;
374 }
375
376 close(fd);
377 return HWNAT_SUCCESS;
378}
379
380
381
382int HwNatInvalidEntry(unsigned int entry_num)
383{
384 struct hwnat_args opt;
385 int fd;
386
387 opt.entry_num=entry_num;
388
389 fd = open("/dev/"HW_NAT_DEVNAME, O_RDONLY);
390 if (fd < 0)
391 {
392 printf("Open %s pseudo device failed\n","/dev/"HW_NAT_DEVNAME);
393 return HWNAT_FAIL;
394 }
395
396 if(ioctl(fd, HW_NAT_INVALID_ENTRY, &opt)<0) {
397 printf("HW_NAT_API: ioctl error\n");
398 close(fd);
399 return HWNAT_FAIL;
400 }
401
402 close(fd);
403 return HWNAT_SUCCESS;
404}
405
406int HwNatCacheDumpEntry(void)
407{
408
409 struct hwnat_args opt;
410 int fd;
411
412 fd = open("/dev/"HW_NAT_DEVNAME, O_RDONLY);
413 if (fd < 0)
414 {
415 printf("Open %s pseudo device failed\n","/dev/"HW_NAT_DEVNAME);
416 return HWNAT_FAIL;
417 }
418 if(ioctl(fd, HW_NAT_DUMP_CACHE_ENTRY, &opt)<0) {
419
420
421 printf("HW_NAT_API: ioctl error\n");
422 close(fd);
423 return HWNAT_FAIL;
424 }
425
426 close(fd);
427 return HWNAT_SUCCESS;
428}
429
430int HwNatGetAGCnt(struct hwnat_ac_args *opt)
431{
432 int fd;
433
434 fd = open("/dev/"HW_NAT_DEVNAME, O_RDONLY);
435 if (fd < 0)
436 {
437 printf("Open %s pseudo device failed\n","/dev/"HW_NAT_DEVNAME);
438 return HWNAT_FAIL;
439 }
440
441 if(ioctl(fd, HW_NAT_GET_AC_CNT, opt)<0) {
442 printf("HW_NAT_API: ioctl error\n");
443 close(fd);
444 return HWNAT_FAIL;
445 }
446
447 close(fd);
448 return HWNAT_SUCCESS;
449
450}
451
452int HwNatSetBindThreshold(struct hwnat_config_args *opt)
453{
454 int fd;
455
456 fd = open("/dev/"HW_NAT_DEVNAME, O_RDONLY);
457 if (fd < 0)
458 {
459 printf("Open %s pseudo device failed\n","/dev/"HW_NAT_DEVNAME);
460 return HWNAT_FAIL;
461 }
462
463 if(ioctl(fd, HW_NAT_BIND_THRESHOLD, opt)<0) {
464 printf("HW_NAT_API: ioctl error\n");
465 close(fd);
466 return HWNAT_FAIL;
467 }
468
469 close(fd);
470 return HWNAT_SUCCESS;
471
472}
473
474int HwNatSetMaxEntryRateLimit(struct hwnat_config_args *opt)
475{
476 int fd;
477
478 fd = open("/dev/"HW_NAT_DEVNAME, O_RDONLY);
479 if (fd < 0)
480 {
481 printf("Open %s pseudo device failed\n","/dev/"HW_NAT_DEVNAME);
482 return HWNAT_FAIL;
483 }
484
485 if(ioctl(fd, HW_NAT_MAX_ENTRY_LMT, opt)<0) {
486 printf("HW_NAT_API: ioctl error\n");
487 close(fd);
488 return HWNAT_FAIL;
489 }
490
491 close(fd);
492 return HWNAT_SUCCESS;
493
494}
495
496
497int HwNatSetRuleSize(struct hwnat_config_args *opt)
498{
499 int fd;
500
501 fd = open("/dev/"HW_NAT_DEVNAME, O_RDONLY);
502 if (fd < 0)
503 {
504 printf("Open %s pseudo device failed\n","/dev/"HW_NAT_DEVNAME);
505 return HWNAT_FAIL;
506 }
507
508 if(ioctl(fd, HW_NAT_RULE_SIZE, opt)<0) {
509 printf("HW_NAT_API: ioctl error\n");
510 close(fd);
511 return HWNAT_FAIL;
512 }
513
514 close(fd);
515 return HWNAT_SUCCESS;
516
517}
518
519int HwNatSetKaInterval(struct hwnat_config_args *opt)
520{
521 int fd;
522
523 fd = open("/dev/"HW_NAT_DEVNAME, O_RDONLY);
524 if (fd < 0)
525 {
526 printf("Open %s pseudo device failed\n","/dev/"HW_NAT_DEVNAME);
527 return HWNAT_FAIL;
528 }
529
530 if(ioctl(fd, HW_NAT_KA_INTERVAL, opt)<0) {
531 printf("HW_NAT_API: ioctl error\n");
532 close(fd);
533 return HWNAT_FAIL;
534 }
535
536 close(fd);
537 return HWNAT_SUCCESS;
538
539}
540
541int HwNatSetUnbindLifeTime(struct hwnat_config_args *opt)
542{
543 int fd;
544
545 fd = open("/dev/"HW_NAT_DEVNAME, O_RDONLY);
546 if (fd < 0)
547 {
548 printf("Open %s pseudo device failed\n","/dev/"HW_NAT_DEVNAME);
549 return HWNAT_FAIL;
550 }
551
552 if(ioctl(fd, HW_NAT_UB_LIFETIME, opt)<0) {
553 printf("HW_NAT_API: ioctl error\n");
554 close(fd);
555 return HWNAT_FAIL;
556 }
557
558 close(fd);
559 return HWNAT_SUCCESS;
560
561}
562
563int HwNatSetBindLifeTime(struct hwnat_config_args *opt)
564{
565 int fd;
566
567 fd = open("/dev/"HW_NAT_DEVNAME, O_RDONLY);
568 if (fd < 0)
569 {
570 printf("Open %s pseudo device failed\n","/dev/"HW_NAT_DEVNAME);
571 return HWNAT_FAIL;
572 }
573
574 if(ioctl(fd, HW_NAT_BIND_LIFETIME, opt)<0) {
575 printf("HW_NAT_API: ioctl error\n");
576 close(fd);
577 return HWNAT_FAIL;
578 }
579
580 close(fd);
581 return HWNAT_SUCCESS;
582
583}
584
585int HwNatSetVID(struct hwnat_config_args *opt)
586{
587 int fd;
588
589 fd = open("/dev/"HW_NAT_DEVNAME, O_RDONLY);
590 if (fd < 0)
591 {
592 printf("Open %s pseudo device failed\n","/dev/"HW_NAT_DEVNAME);
593 return HWNAT_FAIL;
594 }
595
596 if(ioctl(fd, HW_NAT_VLAN_ID, opt)<0) {
597 printf("HW_NAT_API: ioctl error\n");
598 close(fd);
599 return HWNAT_FAIL;
600 }
601
602 close(fd);
603 return HWNAT_SUCCESS;
604}
605
606int HwNatSetBindDir(struct hwnat_config_args *opt)
607{
608 int fd;
609
610 fd = open("/dev/"HW_NAT_DEVNAME, O_RDONLY);
611 if (fd < 0)
612 {
613 printf("Open %s pseudo device failed\n","/dev/"HW_NAT_DEVNAME);
614 return HWNAT_FAIL;
615 }
616
617 if(ioctl(fd, HW_NAT_BIND_DIRECTION, opt)<0) {
618 printf("HW_NAT_API: ioctl error\n");
619 close(fd);
620 return HWNAT_FAIL;
621 }
622
623 close(fd);
624 return HWNAT_SUCCESS;
625}
626
627int HwNatGetAllEntries(struct hwnat_args *opt)
628{
629 int fd=0;
630
631 fd = open("/dev/"HW_NAT_DEVNAME, O_RDONLY);
632 if (fd < 0)
633 {
634 printf("Open %s pseudo device failed\n","/dev/"HW_NAT_DEVNAME);
635 return HWNAT_FAIL;
636 }
637
638 if(ioctl(fd, HW_NAT_GET_ALL_ENTRIES, opt)<0) {
639 printf("HW_NAT_API: ioctl error\n");
640 close(fd);
641 return HWNAT_FAIL;
642 }
643
644 close(fd);
645
646
647 return HWNAT_SUCCESS;
648
649}
650
651int HwNatDebug(unsigned int debug)
652{
653 struct hwnat_args opt;
654 int fd;
655
656 opt.debug=debug;
657
658 fd = open("/dev/"HW_NAT_DEVNAME, O_RDONLY);
659 if (fd < 0)
660 {
661 printf("Open %s pseudo device failed\n","/dev/"HW_NAT_DEVNAME);
662 return HWNAT_FAIL;
663 }
664
665 if(ioctl(fd, HW_NAT_DEBUG, &opt)<0) {
666 printf("HW_NAT_API: ioctl error\n");
667 close(fd);
668 return HWNAT_FAIL;
669 }
670
671 close(fd);
672 return HWNAT_SUCCESS;
673}
674
675#if defined (CONFIG_RALINK_MT7620) || defined (CONFIG_RALINK_MT7621)
676int HwNatSwitchDsliteMape(unsigned int swit)
677{
678 struct hwnat_args opt;
679 int fd;
680
681 opt.swit=swit;
682
683 fd = open("/dev/"HW_NAT_DEVNAME, O_RDONLY);
684 if (fd < 0)
685 {
686 printf("Open %s pseudo device failed\n","/dev/"HW_NAT_DEVNAME);
687 return HWNAT_FAIL;
688 }
689
690 if(ioctl(fd, HW_NAT_SWITCH_DSL_MAPE, &opt)<0) {
691 printf("HW_NAT_API: ioctl error\n");
692 close(fd);
693 return HWNAT_FAIL;
694 }
695
696 close(fd);
697 return HWNAT_SUCCESS;
698}
699#endif
700
701#ifdef CONFIG_PPE_MCAST
702int HwNatMcastIns(struct hwnat_mcast_args *opt)
703{
704 int fd;
705
706 fd = open("/dev/"HW_NAT_DEVNAME, O_RDONLY);
707 if (fd < 0)
708 {
709 printf("Open %s pseudo device failed\n","/dev/"HW_NAT_DEVNAME);
710 return HWNAT_FAIL;
711 }
712
713 if(ioctl(fd, HW_NAT_MCAST_INS, opt)<0) {
714 printf("HW_NAT_API: ioctl error\n");
715 close(fd);
716 return HWNAT_FAIL;
717 }
718
719 close(fd);
720 return HWNAT_SUCCESS;
721}
722
723int HwNatMcastDel(struct hwnat_mcast_args *opt)
724{
725 int fd;
726
727 fd = open("/dev/"HW_NAT_DEVNAME, O_RDONLY);
728 if (fd < 0)
729 {
730 printf("Open %s pseudo device failed\n","/dev/"HW_NAT_DEVNAME);
731 return HWNAT_FAIL;
732 }
733
734 if(ioctl(fd, HW_NAT_MCAST_DEL, opt)<0) {
735 printf("HW_NAT_API: ioctl error\n");
736 close(fd);
737 return HWNAT_FAIL;
738 }
739
740 close(fd);
741 return HWNAT_SUCCESS;
742}
743
744int HwNatMcastDump(void)
745{
746 int fd;
747
748 fd = open("/dev/"HW_NAT_DEVNAME, O_RDONLY);
749 if (fd < 0)
750 {
751 printf("Open %s pseudo device failed\n","/dev/"HW_NAT_DEVNAME);
752 return HWNAT_FAIL;
753 }
754
755 if(ioctl(fd, HW_NAT_MCAST_DUMP, NULL)<0) {
756 printf("HW_NAT_API: ioctl error\n");
757 close(fd);
758 return HWNAT_FAIL;
759 }
760
761 close(fd);
762 return HWNAT_SUCCESS;
763}
764#endif