[package] update ppp to v2.4.4 (#5102)
[openwrt.git] / package / ppp / patches / 310-precompile_filter.patch
1 diff -Naur ppp-2.4.4.orig/pppd/Makefile.linux ppp-2.4.4/pppd/Makefile.linux
2 --- ppp-2.4.4.orig/pppd/Makefile.linux  2009-05-07 22:31:54.000000000 -0400
3 +++ ppp-2.4.4/pppd/Makefile.linux       2009-05-07 22:33:12.000000000 -0400
4 @@ -50,6 +50,9 @@
5  # and that the kernel driver support PPP packet filtering.
6  #FILTER=y
7  
8 +# Support for precompiled filters
9 +PRECOMPILED_FILTER=y
10 +
11  # Uncomment the next line to enable multilink PPP (enabled by default)
12  # Linux distributions: Please leave multilink ENABLED in your builds
13  # of pppd!
14 @@ -175,6 +178,14 @@
15  endif
16  endif
17  
18 +ifdef PRECOMPILED_FILTER
19 +PPPDSRCS += pcap_pcc.c
20 +HEADERS  += pcap_pcc.h
21 +PPPDOBJS += pcap_pcc.o
22 +LIBS   += $(STAGING_DIR)/usr/lib/libpcap.a
23 +CFLAGS += -DPPP_FILTER -DPPP_PRECOMPILED_FILTER -I$(STAGING_DIR)/usr/include
24 +endif
25 +
26  ifdef HAVE_INET6
27       PPPDSRCS += ipv6cp.c eui64.c
28       HEADERS  += ipv6cp.h eui64.h
29 diff -Naur ppp-2.4.4.orig/pppd/options.c ppp-2.4.4/pppd/options.c
30 --- ppp-2.4.4.orig/pppd/options.c       2009-05-07 22:25:24.000000000 -0400
31 +++ ppp-2.4.4/pppd/options.c    2009-05-07 22:38:28.000000000 -0400
32 @@ -57,6 +57,7 @@
33  
34  #ifdef PPP_FILTER
35  #include <pcap.h>
36 +#include <pcap-bpf.h>
37  /*
38   * There have been 3 or 4 different names for this in libpcap CVS, but
39   * this seems to be what they have settled on...
40 @@ -160,6 +161,13 @@
41  static int loadplugin __P((char **));
42  #endif
43  
44 +#ifdef PPP_PRECOMPILED_FILTER
45 +#include "pcap_pcc.h"
46 +static int setprecompiledpassfilter __P((char **));
47 +static int setprecompiledactivefilter __P((char **));
48 +#undef PPP_FILTER
49 +#endif
50 +
51  #ifdef PPP_FILTER
52  static int setpassfilter __P((char **));
53  static int setactivefilter __P((char **));
54 @@ -317,6 +325,14 @@
55        "set filter for active pkts", OPT_PRIO },
56  #endif
57  
58 +#ifdef PPP_PRECOMPILED_FILTER
59 +    { "precompiled-pass-filter", 1, setprecompiledpassfilter,
60 +      "set precompiled filter for packets to pass", OPT_PRIO },
61 +
62 +    { "precompiled-active-filter", 1, setprecompiledactivefilter,
63 +      "set precompiled filter for active pkts", OPT_PRIO },
64 +#endif
65 +
66  #ifdef MAXOCTETS
67      { "maxoctets", o_int, &maxoctets,
68        "Set connection traffic limit",
69 @@ -1456,6 +1472,29 @@
70      return ok;
71  }
72  
73 +#ifdef PPP_PRECOMPILED_FILTER
74 +/*
75 + * setprecompiledpassfilter - Set the pass filter for packets using a
76 + * precompiled expression
77 + */
78 +static int
79 +setprecompiledpassfilter(argv)
80 +    char **argv;
81 +{
82 +    return pcap_pre_compiled (*argv, &pass_filter);
83 +}
84 +
85 +/*
86 + * setactivefilter - Set the active filter for packets
87 + */
88 +static int
89 +setprecompiledactivefilter(argv)
90 +    char **argv;
91 +{
92 +    return pcap_pre_compiled (*argv, &active_filter);
93 +}
94 +#endif
95 +
96  #ifdef PPP_FILTER
97  /*
98   * setpassfilter - Set the pass filter for packets
99 diff -Naur ppp-2.4.4.orig/pppd/pcap_pcc.c ppp-2.4.4/pppd/pcap_pcc.c
100 --- ppp-2.4.4.orig/pppd/pcap_pcc.c      1969-12-31 19:00:00.000000000 -0500
101 +++ ppp-2.4.4/pppd/pcap_pcc.c   2009-05-07 22:33:12.000000000 -0400
102 @@ -0,0 +1,74 @@
103 +#include <pcap.h>
104 +#include <pcap-bpf.h>
105 +#include <stdio.h>
106 +#include <stdlib.h>
107 +#include <string.h>
108 +#include <errno.h>
109 +#include "pppd.h"
110 +
111 +int pcap_pre_compiled (char * fname, struct bpf_program *p)
112 +{
113 +    char buf[128];
114 +    int line = 0, size = 0, index=0, ret=1;
115 +    FILE *f = fopen (fname, "r");
116 +    if (!f)
117 +    {
118 +       option_error("error opening precompiled active-filter '%s': %s",
119 +                    fname, strerror (errno));
120 +       return 0;
121 +    }
122 +    while (fgets (buf, 127, f))
123 +    {
124 +       line++;
125 +       if (*buf == '#')
126 +           continue;
127 +       if (size)
128 +       {
129 +           /*
130 +             struct bpf_insn {
131 +             u_short   code;
132 +             u_char    jt;
133 +             u_char    jf;
134 +             bpf_int32 k;
135 +             }
136 +           */
137 +           struct bpf_insn * insn = & p->bf_insns[index];
138 +           unsigned code, jt, jf, k;
139 +           if (sscanf (buf, "%u %u %u %u", &code, &jt, &jf, &k) != 4)
140 +           {
141 +               goto err;
142 +           }
143 +           insn->code = code;
144 +           insn->jt = jt;
145 +           insn->jf = jf;
146 +           insn->k  = k;
147 +           index++;
148 +       }
149 +       else
150 +       {
151 +           if (sscanf (buf, "%u", &size) != 1)
152 +           {
153 +               goto err;
154 +           }
155 +           p->bf_len = size;
156 +           p->bf_insns = (struct bpf_insn *) 
157 +               malloc (size * sizeof (struct bpf_insn));
158 +       }
159 +    } 
160 +    if (size != index)
161 +    {
162 +       option_error("error in precompiled active-filter,"
163 +                    " expected %d expressions, got %dn",
164 +                    size, index);
165 +       ret = 0;
166 +    }
167 +    fclose(f);
168 +    return ret;
169 +
170 +err:
171 +  option_error("error in precompiled active-filter"
172 +              " expression line %s:%d (wrong size)\n", 
173 +              fname, line);
174 +  fclose (f);
175 +  return 0;
176 +}
177 diff -Naur ppp-2.4.4.orig/pppd/pcap_pcc.h ppp-2.4.4/pppd/pcap_pcc.h
178 --- ppp-2.4.4.orig/pppd/pcap_pcc.h      1969-12-31 19:00:00.000000000 -0500
179 +++ ppp-2.4.4/pppd/pcap_pcc.h   2009-05-07 22:33:12.000000000 -0400
180 @@ -0,0 +1,7 @@
181 +#ifndef PCAP_PCC_H
182 +#define PCAP_PCC_H
183 +
184 +#include <pcap.h>
185 +
186 +int pcap_pre_compiled (char * fname, struct bpf_program *p);
187 +#endif /* PCAP_PCC_H */
188 diff -Naur ppp-2.4.4.orig/pppd/Makefile.linux ppp-2.4.4/pppd/Makefile.linux
189 --- ppp-2.4.4.orig/pppd/Makefile.linux  2009-05-07 22:31:54.000000000 -0400
190 +++ ppp-2.4.4/pppd/Makefile.linux       2009-05-07 22:33:12.000000000 -0400
191 @@ -50,6 +50,9 @@
192  # and that the kernel driver support PPP packet filtering.
193  #FILTER=y
194  
195 +# Support for precompiled filters
196 +PRECOMPILED_FILTER=y
197 +
198  # Uncomment the next line to enable multilink PPP (enabled by default)
199  # Linux distributions: Please leave multilink ENABLED in your builds
200  # of pppd!
201 @@ -175,6 +178,14 @@
202  endif
203  endif
204  
205 +ifdef PRECOMPILED_FILTER
206 +PPPDSRCS += pcap_pcc.c
207 +HEADERS  += pcap_pcc.h
208 +PPPDOBJS += pcap_pcc.o
209 +LIBS   += $(STAGING_DIR)/usr/lib/libpcap.a
210 +CFLAGS += -DPPP_FILTER -DPPP_PRECOMPILED_FILTER -I$(STAGING_DIR)/usr/include
211 +endif
212 +
213  ifdef HAVE_INET6
214       PPPDSRCS += ipv6cp.c eui64.c
215       HEADERS  += ipv6cp.h eui64.h
216 diff -Naur ppp-2.4.4.orig/pppd/options.c ppp-2.4.4/pppd/options.c
217 --- ppp-2.4.4.orig/pppd/options.c       2009-05-07 22:25:24.000000000 -0400
218 +++ ppp-2.4.4/pppd/options.c    2009-05-07 22:38:28.000000000 -0400
219 @@ -57,6 +57,7 @@
220  
221  #ifdef PPP_FILTER
222  #include <pcap.h>
223 +#include <pcap-bpf.h>
224  /*
225   * There have been 3 or 4 different names for this in libpcap CVS, but
226   * this seems to be what they have settled on...
227 @@ -160,6 +161,13 @@
228  static int loadplugin __P((char **));
229  #endif
230  
231 +#ifdef PPP_PRECOMPILED_FILTER
232 +#include "pcap_pcc.h"
233 +static int setprecompiledpassfilter __P((char **));
234 +static int setprecompiledactivefilter __P((char **));
235 +#undef PPP_FILTER
236 +#endif
237 +
238  #ifdef PPP_FILTER
239  static int setpassfilter __P((char **));
240  static int setactivefilter __P((char **));
241 @@ -317,6 +325,14 @@
242        "set filter for active pkts", OPT_PRIO },
243  #endif
244  
245 +#ifdef PPP_PRECOMPILED_FILTER
246 +    { "precompiled-pass-filter", 1, setprecompiledpassfilter,
247 +      "set precompiled filter for packets to pass", OPT_PRIO },
248 +
249 +    { "precompiled-active-filter", 1, setprecompiledactivefilter,
250 +      "set precompiled filter for active pkts", OPT_PRIO },
251 +#endif
252 +
253  #ifdef MAXOCTETS
254      { "maxoctets", o_int, &maxoctets,
255        "Set connection traffic limit",
256 @@ -1456,6 +1472,29 @@
257      return ok;
258  }
259  
260 +#ifdef PPP_PRECOMPILED_FILTER
261 +/*
262 + * setprecompiledpassfilter - Set the pass filter for packets using a
263 + * precompiled expression
264 + */
265 +static int
266 +setprecompiledpassfilter(argv)
267 +    char **argv;
268 +{
269 +    return pcap_pre_compiled (*argv, &pass_filter);
270 +}
271 +
272 +/*
273 + * setactivefilter - Set the active filter for packets
274 + */
275 +static int
276 +setprecompiledactivefilter(argv)
277 +    char **argv;
278 +{
279 +    return pcap_pre_compiled (*argv, &active_filter);
280 +}
281 +#endif
282 +
283  #ifdef PPP_FILTER
284  /*
285   * setpassfilter - Set the pass filter for packets
286 diff -Naur ppp-2.4.4.orig/pppd/pcap_pcc.c ppp-2.4.4/pppd/pcap_pcc.c
287 --- ppp-2.4.4.orig/pppd/pcap_pcc.c      1969-12-31 19:00:00.000000000 -0500
288 +++ ppp-2.4.4/pppd/pcap_pcc.c   2009-05-07 22:33:12.000000000 -0400
289 @@ -0,0 +1,74 @@
290 +#include <pcap.h>
291 +#include <pcap-bpf.h>
292 +#include <stdio.h>
293 +#include <stdlib.h>
294 +#include <string.h>
295 +#include <errno.h>
296 +#include "pppd.h"
297 +
298 +int pcap_pre_compiled (char * fname, struct bpf_program *p)
299 +{
300 +    char buf[128];
301 +    int line = 0, size = 0, index=0, ret=1;
302 +    FILE *f = fopen (fname, "r");
303 +    if (!f)
304 +    {
305 +       option_error("error opening precompiled active-filter '%s': %s",
306 +                    fname, strerror (errno));
307 +       return 0;
308 +    }
309 +    while (fgets (buf, 127, f))
310 +    {
311 +       line++;
312 +       if (*buf == '#')
313 +           continue;
314 +       if (size)
315 +       {
316 +           /*
317 +             struct bpf_insn {
318 +             u_short   code;
319 +             u_char    jt;
320 +             u_char    jf;
321 +             bpf_int32 k;
322 +             }
323 +           */
324 +           struct bpf_insn * insn = & p->bf_insns[index];
325 +           unsigned code, jt, jf, k;
326 +           if (sscanf (buf, "%u %u %u %u", &code, &jt, &jf, &k) != 4)
327 +           {
328 +               goto err;
329 +           }
330 +           insn->code = code;
331 +           insn->jt = jt;
332 +           insn->jf = jf;
333 +           insn->k  = k;
334 +           index++;
335 +       }
336 +       else
337 +       {
338 +           if (sscanf (buf, "%u", &size) != 1)
339 +           {
340 +               goto err;
341 +           }
342 +           p->bf_len = size;
343 +           p->bf_insns = (struct bpf_insn *) 
344 +               malloc (size * sizeof (struct bpf_insn));
345 +       }
346 +    } 
347 +    if (size != index)
348 +    {
349 +       option_error("error in precompiled active-filter,"
350 +                    " expected %d expressions, got %dn",
351 +                    size, index);
352 +       ret = 0;
353 +    }
354 +    fclose(f);
355 +    return ret;
356 +
357 +err:
358 +  option_error("error in precompiled active-filter"
359 +              " expression line %s:%d (wrong size)\n", 
360 +              fname, line);
361 +  fclose (f);
362 +  return 0;
363 +}
364 diff -Naur ppp-2.4.4.orig/pppd/pcap_pcc.h ppp-2.4.4/pppd/pcap_pcc.h
365 --- ppp-2.4.4.orig/pppd/pcap_pcc.h      1969-12-31 19:00:00.000000000 -0500
366 +++ ppp-2.4.4/pppd/pcap_pcc.h   2009-05-07 22:33:12.000000000 -0400
367 @@ -0,0 +1,7 @@
368 +#ifndef PCAP_PCC_H
369 +#define PCAP_PCC_H
370 +
371 +#include <pcap.h>
372 +
373 +int pcap_pre_compiled (char * fname, struct bpf_program *p);
374 +#endif /* PCAP_PCC_H */