tools/padjffs2: use Host/Prepare/Default instead of raw commands
[openwrt/staging/stintel.git] / package / network / services / ppp / patches / 310-precompile_filter.patch
1 pppd: Implement support for precompiled pcap filters
2
3 This patch implements support for precompiled pcap filters which is useful to
4 support dial-on-demand on memory constrained embedded devices without having
5 to link the full libpcap into pppd to generate the filters during runtime.
6
7 Two new options are introduced; "precompiled-pass-filter" specifies a pre-
8 compiled filter file containing rules to match packets which should be passed,
9 "precompiled-active-filter" specifies a filter file containing rules to match
10 packets which are treated as active.
11
12 Signed-off-by: Jo-Philipp Wich <jo@mein.io>
13
14 --- a/pppd/Makefile.linux
15 +++ b/pppd/Makefile.linux
16 @@ -51,6 +51,9 @@ MPPE=y
17 # and that the kernel driver support PPP packet filtering.
18 #FILTER=y
19
20 +# Support for precompiled filters
21 +PRECOMPILED_FILTER=y
22 +
23 # Uncomment the next line to enable multilink PPP (enabled by default)
24 # Linux distributions: Please leave multilink ENABLED in your builds
25 # of pppd!
26 @@ -214,6 +217,14 @@ LIBS += -lpcap -L$(STAGING_DIR)/usr/l
27 CFLAGS += -DPPP_FILTER -I$(STAGING_DIR)/usr/include
28 endif
29
30 +ifdef PRECOMPILED_FILTER
31 +PPPDSRCS += pcap_pcc.c
32 +HEADERS += pcap_pcc.h
33 +PPPDOBJS += pcap_pcc.o
34 +LIBS += $(STAGING_DIR)/usr/lib/libpcap.a
35 +CFLAGS += -DPPP_FILTER -DPPP_PRECOMPILED_FILTER -I$(STAGING_DIR)/usr/include
36 +endif
37 +
38 ifdef HAVE_INET6
39 PPPDSRCS += ipv6cp.c eui64.c
40 HEADERS += ipv6cp.h eui64.h
41 --- a/pppd/options.c
42 +++ b/pppd/options.c
43 @@ -56,6 +56,7 @@
44
45 #ifdef PPP_FILTER
46 #include <pcap.h>
47 +#include <pcap-bpf.h>
48 /*
49 * There have been 3 or 4 different names for this in libpcap CVS, but
50 * this seems to be what they have settled on...
51 @@ -168,6 +169,13 @@ static int setlogfile(char **);
52 static int loadplugin(char **);
53 #endif
54
55 +#ifdef PPP_PRECOMPILED_FILTER
56 +#include "pcap_pcc.h"
57 +static int setprecompiledpassfilter(char **);
58 +static int setprecompiledactivefilter(char **);
59 +#undef PPP_FILTER
60 +#endif
61 +
62 #ifdef PPP_FILTER
63 static int setpassfilter(char **);
64 static int setactivefilter(char **);
65 @@ -360,6 +368,14 @@ option_t general_options[] = {
66 "set filter for active pkts", OPT_PRIO },
67 #endif
68
69 +#ifdef PPP_PRECOMPILED_FILTER
70 + { "precompiled-pass-filter", 1, setprecompiledpassfilter,
71 + "set precompiled filter for packets to pass", OPT_PRIO },
72 +
73 + { "precompiled-active-filter", 1, setprecompiledactivefilter,
74 + "set precompiled filter for active pkts", OPT_PRIO },
75 +#endif
76 +
77 #ifdef MAXOCTETS
78 { "maxoctets", o_int, &maxoctets,
79 "Set connection traffic limit",
80 @@ -1468,6 +1484,27 @@ callfile(char **argv)
81 return ok;
82 }
83
84 +#ifdef PPP_PRECOMPILED_FILTER
85 +/*
86 + * setprecompiledpassfilter - Set the pass filter for packets using a
87 + * precompiled expression
88 + */
89 +static int
90 +setprecompiledpassfilter(char **argv)
91 +{
92 + return pcap_pre_compiled (*argv, &pass_filter);
93 +}
94 +
95 +/*
96 + * setactivefilter - Set the active filter for packets
97 + */
98 +static int
99 +setprecompiledactivefilter(char **argv)
100 +{
101 + return pcap_pre_compiled (*argv, &active_filter);
102 +}
103 +#endif
104 +
105 #ifdef PPP_FILTER
106 /*
107 * setpassfilter - Set the pass filter for packets
108 --- /dev/null
109 +++ b/pppd/pcap_pcc.c
110 @@ -0,0 +1,74 @@
111 +#include <pcap.h>
112 +#include <pcap-bpf.h>
113 +#include <stdio.h>
114 +#include <stdlib.h>
115 +#include <string.h>
116 +#include <errno.h>
117 +#include "pppd.h"
118 +
119 +int pcap_pre_compiled (char * fname, struct bpf_program *p)
120 +{
121 + char buf[128];
122 + int line = 0, size = 0, index=0, ret=1;
123 + FILE *f = fopen (fname, "r");
124 + if (!f)
125 + {
126 + option_error("error opening precompiled active-filter '%s': %s",
127 + fname, strerror (errno));
128 + return 0;
129 + }
130 + while (fgets (buf, 127, f))
131 + {
132 + line++;
133 + if (*buf == '#')
134 + continue;
135 + if (size)
136 + {
137 + /*
138 + struct bpf_insn {
139 + u_short code;
140 + u_char jt;
141 + u_char jf;
142 + bpf_int32 k;
143 + }
144 + */
145 + struct bpf_insn * insn = & p->bf_insns[index];
146 + unsigned code, jt, jf, k;
147 + if (sscanf (buf, "%u %u %u %u", &code, &jt, &jf, &k) != 4)
148 + {
149 + goto err;
150 + }
151 + insn->code = code;
152 + insn->jt = jt;
153 + insn->jf = jf;
154 + insn->k = k;
155 + index++;
156 + }
157 + else
158 + {
159 + if (sscanf (buf, "%u", &size) != 1)
160 + {
161 + goto err;
162 + }
163 + p->bf_len = size;
164 + p->bf_insns = (struct bpf_insn *)
165 + malloc (size * sizeof (struct bpf_insn));
166 + }
167 + }
168 + if (size != index)
169 + {
170 + option_error("error in precompiled active-filter,"
171 + " expected %d expressions, got %dn",
172 + size, index);
173 + ret = 0;
174 + }
175 + fclose(f);
176 + return ret;
177 +
178 +err:
179 + option_error("error in precompiled active-filter"
180 + " expression line %s:%d (wrong size)\n",
181 + fname, line);
182 + fclose (f);
183 + return 0;
184 +}
185 --- /dev/null
186 +++ b/pppd/pcap_pcc.h
187 @@ -0,0 +1,7 @@
188 +#ifndef PCAP_PCC_H
189 +#define PCAP_PCC_H
190 +
191 +#include <pcap.h>
192 +
193 +int pcap_pre_compiled (char * fname, struct bpf_program *p);
194 +#endif /* PCAP_PCC_H */