blob: 4789e021263c517c4f0bc38902ad325798cea7a0 [file] [log] [blame]
developereed1a952023-03-01 14:30:18 +08001diff --git a/package/libs/libnfnetlink/patches/101-flowtable-support.patch b/package/libs/libnfnetlink/patches/101-flowtable-support.patch
2new file mode 100644
3index 00000000..2ba78022
4--- /dev/null
5+++ b/package/libs/libnfnetlink/patches/101-flowtable-support.patch
6@@ -0,0 +1,12 @@
7+Index: libnfnetlink-1.0.1/include/libnfnetlink/linux_nfnetlink.h
8+===================================================================
9+--- libnfnetlink-1.0.1.orig/include/libnfnetlink/linux_nfnetlink.h 2008-06-18 20:36:57.000000000 +0800
10++++ libnfnetlink-1.0.1/include/libnfnetlink/linux_nfnetlink.h 2019-08-30 18:04:36.026372012 +0800
11+@@ -47,6 +47,7 @@
12+ #define NFNL_SUBSYS_QUEUE 3
13+ #define NFNL_SUBSYS_ULOG 4
14+ #define NFNL_SUBSYS_COUNT 5
15++#define NFNL_SUBSYS_FLOWTABLE 12
16+
17+ #ifdef __KERNEL__
18+
19diff --git a/package/mtk/applications/flowtable/Makefile b/package/mtk/applications/flowtable/Makefile
20new file mode 100755
21index 00000000..150a1a3b
22--- /dev/null
23+++ b/package/mtk/applications/flowtable/Makefile
24@@ -0,0 +1,51 @@
25+#
26+# Copyright (C) 2009-2013 OpenWrt.org
27+#
28+# This is free software, licensed under the GNU General Public License v2.
29+# See /LICENSE for more information.
30+#
31+
32+include $(TOPDIR)/rules.mk
33+
34+PKG_NAME:=netfilter_flowtable
35+PKG_VERSION:=1.0
36+PKG_RELEASE:=1
37+
38+PKG_LICENSE:=GPL-2.0+
39+#PKG_INSTALL:=1
40+
41+include $(INCLUDE_DIR)/package.mk
42+
43+define Package/netfilter-flowtable
44+ SECTION:=MTK Properties
45+ CATEGORY:=MTK Properties
46+ DEPENDS:=+libnfnetlink +libmnl +kmod-nf-flow-netlink
47+ TITLE:=API to the in-kernel flow offload table
48+ SUBMENU:=Applications
49+endef
50+
51+define Package/netfilter-flowtable/description
52+ API to the in-kernel flow offload table
53+endef
54+
55+TARGET_CFLAGS += $(FPIC)
56+
57+TARGET_CPPFLAGS := \
58+ -D_GNU_SOURCE \
59+ -I$(LINUX_DIR)/user_headers/include \
60+ -I$(PKG_BUILD_DIR) \
61+ $(TARGET_CPPFLAGS) \
62+
63+define Build/Compile
64+ CFLAGS="$(TARGET_CPPFLAGS) $(TARGET_CFLAGS)" \
65+ $(MAKE) -C $(PKG_BUILD_DIR) \
66+ $(TARGET_CONFIGURE_OPTS) \
67+ LIBS="$(TARGET_LDFLAGS) -lnfnetlink -lm"
68+endef
69+
70+define Package/netfilter-flowtable/install
71+ $(INSTALL_DIR) $(1)/usr/bin
72+ $(CP) $(PKG_BUILD_DIR)/ftnl $(1)/usr/bin/
73+endef
74+
75+$(eval $(call BuildPackage,netfilter-flowtable))
76\ No newline at end of file
77diff --git a/package/mtk/applications/flowtable/src/Makefile b/package/mtk/applications/flowtable/src/Makefile
78new file mode 100644
79index 00000000..3f450ad2
80--- /dev/null
81+++ b/package/mtk/applications/flowtable/src/Makefile
82@@ -0,0 +1,9 @@
83+EXEC = ftnl
84+SRC = api.c ftnl.c
85+
86+all:$(EXEC)
87+$(EXEC):$(SRC)
88+ $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $(SRC) $(LDLIBS) $(LIBS)
89+
90+clean:
91+ -rm -f $(EXEC) *.elf *.gdb *.o
92diff --git a/package/mtk/applications/flowtable/src/api.c b/package/mtk/applications/flowtable/src/api.c
93new file mode 100644
94index 00000000..17b88b43
95--- /dev/null
96+++ b/package/mtk/applications/flowtable/src/api.c
97@@ -0,0 +1,106 @@
98+#include <stdio.h>
99+#include <stdlib.h>
100+#include <string.h>
101+#include "netfilter_flowtable.h"
102+
103+static void attr_dump(struct nfattr *attr)
104+{
105+ char *data = nla_data(attr);
106+ int i = 0;
107+
108+ while (i < nal_len(attr)) {
109+ printf("%x ", *(data + i));
110+ i++;
111+ if (i % 16 == 0)
112+ printf("\n");
113+ }
114+ printf("\n");
115+}
116+
117+struct ftnl_handle* ftnl_open(void)
118+{
119+ struct ftnl_handle *h = NULL;
120+
121+ h = malloc(sizeof(struct ftnl_handle));
122+ if (!h)
123+ return NULL;
124+
125+ h->nfnlh = nfnl_open();
126+ if (!h->nfnlh) {
127+ printf("nfnl open fail\n");
128+ free(h);
129+ return NULL;
130+ }
131+
132+ h->ftnlssh = nfnl_subsys_open(h->nfnlh, NFNL_SUBSYS_FLOWTABLE, 1, 0);
133+ if (!h->ftnlssh) {
134+ nfnl_close(h->nfnlh);
135+ printf("subsys open fail\n");
136+ free(h);
137+ return NULL;
138+ }
139+
140+ return h;
141+}
142+
143+void ftnl_close(struct ftnl_handle *h)
144+{
145+ nfnl_subsys_close(h->ftnlssh);
146+ nfnl_close(h->nfnlh);
147+ free(h);
148+}
149+
150+static void build_tuple(struct nlmsghdr *nlh,size_t size, struct flow_tuple *tuple)
151+{
152+ struct nfattr *nest_tuple, *nest_ip, *nest_proto;
153+
154+ nest_tuple = nfnl_nest(nlh, size, FTA_TUPLE);
155+
156+ nest_ip = nfnl_nest(nlh, size, FTA_TUPLE_IP);
157+ nfnl_addattr_l(nlh, size, FTA_IP_V4_SRC, &tuple->sip4, sizeof(uint32_t));
158+ nfnl_addattr_l(nlh, size, FTA_IP_V4_DST, &tuple->dip4, sizeof(uint32_t));
159+ nfnl_nest_end(nlh, nest_ip);
160+
161+ nest_proto = nfnl_nest(nlh, size, FTA_TUPLE_PROTO);
162+ nfnl_addattr_l(nlh, size, FTA_PROTO_NUM, &tuple->proto, sizeof(uint8_t));
163+ nfnl_addattr_l(nlh, size, FTA_PROTO_SPORT, &tuple->sport, sizeof(uint16_t));
164+ nfnl_addattr_l(nlh, size, FTA_PROTO_DPORT, &tuple->dport, sizeof(uint16_t));
165+ nfnl_nest_end(nlh, nest_proto);
166+
167+ nfnl_nest_end(nlh,nest_tuple);
168+// attr_dump(nest_tuple);
169+}
170+
171+int ftnl_flush_table(struct ftnl_handle *h)
172+{
173+ struct nlmsghdr nlh;
174+ int ret;
175+
176+ /* construct msg */
177+ nfnl_fill_hdr(h->ftnlssh, &nlh, 0, AF_INET, 0,
178+ FT_MSG_FLUSH, NLM_F_REQUEST | NLM_F_ACK);
179+
180+ /* send msg */
181+ ret = nfnl_send(h->nfnlh, &nlh);
182+ return ret;
183+}
184+
185+int ftnl_del_flow(struct ftnl_handle *h, struct flow_tuple *tuple)
186+{
187+ const int size = 256;
188+ union {
189+ char buffer[size];
190+ struct nlmsghdr nlh;
191+ } u;
192+ int ret;
193+
194+ /* construct msg */
195+ nfnl_fill_hdr(h->ftnlssh, &u.nlh, 0, AF_INET, 0,
196+ FT_MSG_DEL, NLM_F_REQUEST|NLM_F_ACK);
197+ build_tuple(&u.nlh, size, tuple);
198+
199+ /* send msg */
200+ ret = nfnl_send(h->nfnlh, &u.nlh);
201+
202+ return ret;
203+}
204diff --git a/package/mtk/applications/flowtable/src/ftnl.c b/package/mtk/applications/flowtable/src/ftnl.c
205new file mode 100644
206index 00000000..30352b29
207--- /dev/null
208+++ b/package/mtk/applications/flowtable/src/ftnl.c
209@@ -0,0 +1,92 @@
210+#include <stdio.h>
211+#include <stdlib.h>
212+#include <string.h>
213+#include <errno.h>
214+#include <unistd.h>
215+#include <getopt.h>
216+
217+#include "netfilter_flowtable.h"
218+
219+void usage(void)
220+{
221+ printf("#########flush flow table\n");
222+ printf("ftnl -F\n");
223+ printf("#########del flow from offload table\n");
224+ printf("ftnl -D [sip] [dip] [proto] [sport] [dport]\n");
225+}
226+
227+int main (int argc, char *argv[])
228+{
229+ struct ftnl_handle *h;
230+ struct flow_tuple tuple = {0};
231+ int msg = -1;
232+ int c;
233+ int ret = -1;
234+ const char* optstring = "FD";
235+ struct option opts[] = {
236+ {"sip", required_argument, NULL, 's'},
237+ {"dip", required_argument, NULL, 'd'},
238+ {"proto", required_argument, NULL, 'p'},
239+ {"sport", required_argument, NULL, 'm'},
240+ {"dport", required_argument, NULL, 'n'}
241+ };
242+
243+ /* open netlink socket */
244+ h = ftnl_open();
245+ if (!h)
246+ return ret;
247+
248+ /* parse arg */
249+ while ((c = getopt_long(argc, argv, optstring, opts, NULL)) != -1) {
250+ switch (c) {
251+ case 'F':
252+ msg = FT_MSG_FLUSH;
253+ break;
254+ case 'D':
255+ msg = FT_MSG_DEL;
256+ break;
257+ case 's':
258+ inet_aton(optarg, &tuple.sip4);
259+ break;
260+ case 'd':
261+ inet_aton(optarg, &tuple.dip4);
262+ break;
263+ case 'p':
264+ if (!strcmp(optarg, "tcp"))
265+ tuple.proto = IPPROTO_TCP;
266+ else if (!strcmp(optarg, "udp"))
267+ tuple.proto = IPPROTO_UDP;
268+ else {
269+ printf("proto bad value "
270+ "pls set proto to udp or tcp "
271+ "arg : %s\n", optarg);
272+ goto out;
273+ }
274+ break;
275+ case 'm':
276+ tuple.sport = htons(atoi(optarg));
277+ break;
278+ case 'n':
279+ tuple.dport = htons(atoi(optarg));
280+ break;
281+ default:
282+ usage();
283+ goto out;
284+ }
285+ }
286+
287+ switch (msg) {
288+ case FT_MSG_FLUSH:
289+ ftnl_flush_table(h);
290+ break;
291+ case FT_MSG_DEL:
292+ ftnl_del_flow(h, &tuple);
293+ break;
294+ default:
295+ break;
296+ }
297+
298+out:
299+ ftnl_close(h);
300+ return ret;
301+}
302diff --git a/package/mtk/applications/flowtable/src/netfilter_flowtable.h b/package/mtk/applications/flowtable/src/netfilter_flowtable.h
303new file mode 100644
304index 00000000..3ea8916e
305--- /dev/null
306+++ b/package/mtk/applications/flowtable/src/netfilter_flowtable.h
307@@ -0,0 +1,63 @@
308+#include <netinet/in.h>
309+#include <arpa/inet.h>
310+#include <libnfnetlink/libnfnetlink.h>
311+
312+struct ftnl_handle {
313+ struct nfnl_handle *nfnlh;
314+ struct nfnl_subsys_handle *ftnlssh;
315+};
316+
317+struct flow_tuple {
318+ struct in_addr sip4;
319+ struct in_addr dip4;
320+ unsigned char proto;
321+ unsigned short int sport;
322+ unsigned short int dport;
323+};
324+
325+enum ft_msg_types{
326+ FT_MSG_DEL,
327+ FT_MSG_ADD, //not support now
328+ FT_MSG_FLUSH,
329+ FT_MSG_MAX
330+};
331+
332+enum ftattr_type {
333+ FTA_UNSPEC,
334+ FTA_TUPLE,
335+ __FTA_MAX
336+};
337+#define FTA_MAX (__FTA_MAX - 1)
338+
339+enum ftattr_tuple {
340+ FTA_TUPLE_UNSPEC,
341+ FTA_TUPLE_IP,
342+ FTA_TUPLE_PROTO,
343+ FTA_TUPLE_ZONE,
344+ __FTA_TUPLE_MAX
345+};
346+#define FTA_TUPLE_MAX (__FTA_TUPLE_MAX - 1)
347+
348+enum ftattr_ip {
349+ FTA_IP_UNSPEC,
350+ FTA_IP_V4_SRC,
351+ FTA_IP_V4_DST,
352+ FTA_IP_V6_SRC,
353+ FTA_IP_V6_DST,
354+ __FTA_IP_MAX
355+};
356+#define FTA_IP_MAX (__FTA_IP_MAX - 1)
357+
358+enum ftattr_l4proto {
359+ FTA_PROTO_UNSPEC,
360+ FTA_PROTO_NUM,
361+ FTA_PROTO_SPORT,
362+ FTA_PROTO_DPORT,
363+ __FTA_PROTO_MAX
364+};
365+#define FTA_PROTO_MAX (__FTA_PROTO_MAX - 1)
366+
367+struct ftnl_handle* ftnl_open(void);
368+void ftnl_close(struct ftnl_handle *h);
369+int ftnl_flush_table(struct ftnl_handle *h);
370+int ftnl_del_flow(struct ftnl_handle *h, struct flow_tuple *tuple);
371diff --git a/package/kernel/linux/modules/netfilter.mk b/package/kernel/linux/modules/netfilter.mk
372index 97153e5c..e30484b7 100644
373--- a/package/kernel/linux/modules/netfilter.mk
374+++ b/package/kernel/linux/modules/netfilter.mk
375@@ -161,6 +161,18 @@ endef
376
377 $(eval $(call KernelPackage,nf-flow))
378
379+define KernelPackage/nf-flow-netlink
380+ SUBMENU:=$(NF_MENU)
381+ TITLE:=Netfilter flowtable netlink support
382+ KCONFIG:= \
383+ CONFIG_NF_FLOW_TABLE_NETLINK
384+ DEPENDS:=+kmod-nf-flow +kmod-nfnetlink
385+ FILES:= \
386+ $(LINUX_DIR)/net/netfilter/nf_flow_table_netlink.ko
387+ AUTOLOAD:=$(call AutoProbe,nf_flow_table_netlink)
388+endef
389+
390+$(eval $(call KernelPackage,nf-flow-netlink))
391
392 define AddDepends/ipt
393 SUBMENU:=$(NF_MENU)