let ipkg fail when a package file to be installed is not found
[openwrt.git] / openwrt / package / ppp / patches / 310-precompiled_filter.patch
1 diff -urN ppp.old/pppd/Makefile.linux ppp.dev/pppd/Makefile.linux
2 --- ppp.old/pppd/Makefile.linux 2006-03-18 15:58:00.000000000 +0100
3 +++ ppp.dev/pppd/Makefile.linux 2006-03-18 16:52:01.000000000 +0100
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 @@ -177,6 +180,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 -urN ppp.old/pppd/demand.c ppp.dev/pppd/demand.c
30 --- ppp.old/pppd/demand.c       2006-03-18 15:58:00.000000000 +0100
31 +++ ppp.dev/pppd/demand.c       2006-03-18 18:09:16.000000000 +0100
32 @@ -438,12 +438,14 @@
33         return 0;
34      proto = PPP_PROTOCOL(p);
35  #ifdef PPP_FILTER
36 +       *p = 1; /* set outbound for the filter rule */
37      if (pass_filter.bf_len != 0
38         && bpf_filter(pass_filter.bf_insns, p, len, len) == 0)
39         return 0;
40      if (active_filter.bf_len != 0
41         && bpf_filter(active_filter.bf_insns, p, len, len) == 0)
42         return 0;
43 +       *p = 0xff; /* restore original ppp header */
44  #endif
45      for (i = 0; (protp = protocols[i]) != NULL; ++i) {
46         if (protp->protocol < 0xC000 && (protp->protocol & ~0x8000) == proto) {
47 diff -urN ppp.old/pppd/options.c ppp.dev/pppd/options.c
48 --- ppp.old/pppd/options.c      2006-03-18 15:58:00.000000000 +0100
49 +++ ppp.dev/pppd/options.c      2006-03-18 18:05:58.000000000 +0100
50 @@ -57,14 +57,7 @@
51  
52  #ifdef PPP_FILTER
53  #include <pcap.h>
54 -/*
55 - * DLT_PPP_WITH_DIRECTION is in current libpcap cvs, and should be in
56 - * libpcap-0.8.4.  Until that is released, use DLT_PPP - but that means
57 - * we lose the inbound and outbound qualifiers.
58 - */
59 -#ifndef DLT_PPP_WITH_DIRECTION
60 -#define DLT_PPP_WITH_DIRECTION DLT_PPP
61 -#endif
62 +#include <pcap-bpf.h>
63  #endif
64  
65  #include "pppd.h"
66 @@ -155,6 +148,13 @@
67  static int loadplugin __P((char **));
68  #endif
69  
70 +#ifdef PPP_PRECOMPILED_FILTER
71 +#include "pcap_pcc.h"
72 +static int setprecompiledpassfilter __P((char **));
73 +static int setprecompiledactivefilter __P((char **));
74 +#undef PPP_FILTER
75 +#endif
76 +
77  #ifdef PPP_FILTER
78  static int setpassfilter __P((char **));
79  static int setactivefilter __P((char **));
80 @@ -312,6 +312,14 @@
81        "set filter for active pkts", OPT_PRIO },
82  #endif
83  
84 +#ifdef PPP_PRECOMPILED_FILTER
85 +    { "precompiled-pass-filter", 1, setprecompiledpassfilter,
86 +      "set precompiled filter for packets to pass", OPT_PRIO },
87 +
88 +    { "precompiled-active-filter", 1, setprecompiledactivefilter,
89 +      "set precompiled filter for active pkts", OPT_PRIO },
90 +#endif
91 +
92  #ifdef MAXOCTETS
93      { "maxoctets", o_int, &maxoctets,
94        "Set connection traffic limit",
95 @@ -1447,6 +1455,29 @@
96      return ok;
97  }
98  
99 +#ifdef PPP_PRECOMPILED_FILTER
100 +/*
101 + * setprecompiledpassfilter - Set the pass filter for packets using a
102 + * precompiled expression
103 + */
104 +static int
105 +setprecompiledpassfilter(argv)
106 +    char **argv;
107 +{
108 +    return pcap_pre_compiled (*argv, &pass_filter);
109 +}
110 +
111 +/*
112 + * setactivefilter - Set the active filter for packets
113 + */
114 +static int
115 +setprecompiledactivefilter(argv)
116 +    char **argv;
117 +{
118 +    return pcap_pre_compiled (*argv, &active_filter);
119 +}
120 +#endif
121 +
122  #ifdef PPP_FILTER
123  /*
124   * setpassfilter - Set the pass filter for packets
125 @@ -1458,7 +1489,7 @@
126      pcap_t *pc;
127      int ret = 0;
128  
129 -    pc = pcap_open_dead(DLT_PPP_WITH_DIRECTION, 65535);
130 +    pc = pcap_open_dead(DLT_PPP_PPPD, 65535);
131      if (pcap_compile(pc, &pass_filter, *argv, 1, netmask) == -1) {
132         option_error("error in pass-filter expression: %s\n",
133                      pcap_geterr(pc));
134 @@ -1479,7 +1510,7 @@
135      pcap_t *pc;
136      int ret = 0;
137  
138 -    pc = pcap_open_dead(DLT_PPP_WITH_DIRECTION, 65535);
139 +    pc = pcap_open_dead(DLT_PPP_PPPD, 65535);
140      if (pcap_compile(pc, &active_filter, *argv, 1, netmask) == -1) {
141         option_error("error in active-filter expression: %s\n",
142                      pcap_geterr(pc));
143 diff -urN ppp.old/pppd/pcap_pcc.c ppp.dev/pppd/pcap_pcc.c
144 --- ppp.old/pppd/pcap_pcc.c     1970-01-01 01:00:00.000000000 +0100
145 +++ ppp.dev/pppd/pcap_pcc.c     2006-03-18 16:51:31.000000000 +0100
146 @@ -0,0 +1,74 @@
147 +#include <pcap.h>
148 +#include <pcap-bpf.h>
149 +#include <stdio.h>
150 +#include <stdlib.h>
151 +#include <string.h>
152 +#include <errno.h>
153 +#include "pppd.h"
154 +
155 +int pcap_pre_compiled (char * fname, struct bpf_program *p)
156 +{
157 +    char buf[128];
158 +    int line = 0, size = 0, index=0, ret=1;
159 +    FILE *f = fopen (fname, "r");
160 +    if (!f)
161 +    {
162 +       option_error("error opening precompiled active-filter '%s': %s",
163 +                    fname, strerror (errno));
164 +       return 0;
165 +    }
166 +    while (fgets (buf, 127, f))
167 +    {
168 +       line++;
169 +       if (*buf == '#')
170 +           continue;
171 +       if (size)
172 +       {
173 +           /*
174 +             struct bpf_insn {
175 +             u_short   code;
176 +             u_char    jt;
177 +             u_char    jf;
178 +             bpf_int32 k;
179 +             }
180 +           */
181 +           struct bpf_insn * insn = & p->bf_insns[index];
182 +           unsigned code, jt, jf, k;
183 +           if (sscanf (buf, "%u %u %u %u", &code, &jt, &jf, &k) != 4)
184 +           {
185 +               goto err;
186 +           }
187 +           insn->code = code;
188 +           insn->jt = jt;
189 +           insn->jf = jf;
190 +           insn->k  = k;
191 +           index++;
192 +       }
193 +       else
194 +       {
195 +           if (sscanf (buf, "%u", &size) != 1)
196 +           {
197 +               goto err;
198 +           }
199 +           p->bf_len = size;
200 +           p->bf_insns = (struct bpf_insn *) 
201 +               malloc (size * sizeof (struct bpf_insn));
202 +       }
203 +    } 
204 +    if (size != index)
205 +    {
206 +       option_error("error in precompiled active-filter,"
207 +                    " expected %d expressions, got %dn",
208 +                    size, index);
209 +       ret = 0;
210 +    }
211 +    fclose(f);
212 +    return ret;
213 +
214 +err:
215 +  option_error("error in precompiled active-filter"
216 +              " expression line %s:%d (wrong size)\n", 
217 +              fname, line);
218 +  fclose (f);
219 +  return 0;
220 +}
221 diff -urN ppp.old/pppd/pcap_pcc.h ppp.dev/pppd/pcap_pcc.h
222 --- ppp.old/pppd/pcap_pcc.h     1970-01-01 01:00:00.000000000 +0100
223 +++ ppp.dev/pppd/pcap_pcc.h     2006-03-18 15:59:14.000000000 +0100
224 @@ -0,0 +1,7 @@
225 +#ifndef PCAP_PCC_H
226 +#define PCAP_PCC_H
227 +
228 +#include <pcap.h>
229 +
230 +int pcap_pre_compiled (char * fname, struct bpf_program *p);
231 +#endif /* PCAP_PCC_H */