OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [linux/] [linux-2.4/] [net/] [ipv6/] [netfilter/] [ip6t_dst.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1275 phoenix
/* Kernel module to match Hop-by-Hop and Destination parameters. */
2
#include <linux/module.h>
3
#include <linux/skbuff.h>
4
#include <linux/ipv6.h>
5
#include <linux/types.h>
6
#include <net/checksum.h>
7
#include <net/ipv6.h>
8
 
9
#include <asm/byteorder.h>
10
 
11
#include <linux/netfilter_ipv6/ip6_tables.h>
12
#include <linux/netfilter_ipv6/ip6t_opts.h>
13
 
14
#define LOW(n)          (n & 0x00FF)
15
 
16
#define HOPBYHOP        0
17
 
18
EXPORT_NO_SYMBOLS;
19
MODULE_LICENSE("GPL");
20
#if HOPBYHOP
21
MODULE_DESCRIPTION("IPv6 HbH match");
22
#else
23
MODULE_DESCRIPTION("IPv6 DST match");
24
#endif
25
MODULE_AUTHOR("Andras Kis-Szabo <kisza@sch.bme.hu>");
26
 
27
#if 0
28
#define DEBUGP printk
29
#else
30
#define DEBUGP(format, args...)
31
#endif
32
 
33
/*
34
 * (Type & 0xC0) >> 6
35
 *      0        -> ignorable
36
 *      1       -> must drop the packet
37
 *      2       -> send ICMP PARM PROB regardless and drop packet
38
 *      3       -> Send ICMP if not a multicast address and drop packet
39
 *  (Type & 0x20) >> 5
40
 *      0        -> invariant
41
 *      1       -> can change the routing
42
 *  (Type & 0x1F) Type
43
 *      0       -> PAD0 (only 1 byte!)
44
 *      1       -> PAD1 LENGTH info (total length = length + 2)
45
 *      C0 | 2  -> JUMBO 4 x x x x ( xxxx > 64k )
46
 *      5       -> RTALERT 2 x x
47
 */
48
 
49
static int
50
match(const struct sk_buff *skb,
51
      const struct net_device *in,
52
      const struct net_device *out,
53
      const void *matchinfo,
54
      int offset,
55
      const void *protohdr,
56
      u_int16_t datalen,
57
      int *hotdrop)
58
{
59
       struct ipv6_opt_hdr *optsh = NULL;
60
       const struct ip6t_opts *optinfo = matchinfo;
61
       unsigned int temp;
62
       unsigned int len;
63
       u8 nexthdr;
64
       unsigned int ptr;
65
       unsigned int hdrlen = 0;
66
       unsigned int ret = 0;
67
       u_int16_t *optdesc = NULL;
68
 
69
       /* type of the 1st exthdr */
70
       nexthdr = skb->nh.ipv6h->nexthdr;
71
       /* pointer to the 1st exthdr */
72
       ptr = sizeof(struct ipv6hdr);
73
       /* available length */
74
       len = skb->len - ptr;
75
       temp = 0;
76
 
77
        while (ip6t_ext_hdr(nexthdr)) {
78
               struct ipv6_opt_hdr *hdr;
79
 
80
              DEBUGP("ipv6_opts header iteration \n");
81
 
82
              /* Is there enough space for the next ext header? */
83
                if (len < (int)sizeof(struct ipv6_opt_hdr))
84
                        return 0;
85
              /* No more exthdr -> evaluate */
86
                if (nexthdr == NEXTHDR_NONE) {
87
                     break;
88
              }
89
              /* ESP -> evaluate */
90
                if (nexthdr == NEXTHDR_ESP) {
91
                     break;
92
              }
93
 
94
              hdr=(void *)(skb->data)+ptr;
95
 
96
              /* Calculate the header length */
97
                if (nexthdr == NEXTHDR_FRAGMENT) {
98
                        hdrlen = 8;
99
                } else if (nexthdr == NEXTHDR_AUTH)
100
                        hdrlen = (hdr->hdrlen+2)<<2;
101
                else
102
                        hdrlen = ipv6_optlen(hdr);
103
 
104
              /* OPTS -> evaluate */
105
#if HOPBYHOP
106
                if (nexthdr == NEXTHDR_HOP) {
107
                     temp |= MASK_HOPOPTS;
108
#else
109
                if (nexthdr == NEXTHDR_DEST) {
110
                     temp |= MASK_DSTOPTS;
111
#endif
112
                     break;
113
              }
114
 
115
 
116
              /* set the flag */
117
              switch (nexthdr){
118
                     case NEXTHDR_HOP:
119
                     case NEXTHDR_ROUTING:
120
                     case NEXTHDR_FRAGMENT:
121
                     case NEXTHDR_AUTH:
122
                     case NEXTHDR_DEST:
123
                            break;
124
                     default:
125
                            DEBUGP("ipv6_opts match: unknown nextheader %u\n",nexthdr);
126
                            return 0;
127
                            break;
128
              }
129
 
130
                nexthdr = hdr->nexthdr;
131
                len -= hdrlen;
132
                ptr += hdrlen;
133
                if ( ptr > skb->len ) {
134
                        DEBUGP("ipv6_opts: new pointer is too large! \n");
135
                        break;
136
                }
137
        }
138
 
139
       /* OPTIONS header not found */
140
#if HOPBYHOP
141
       if ( temp != MASK_HOPOPTS ) return 0;
142
#else
143
       if ( temp != MASK_DSTOPTS ) return 0;
144
#endif
145
 
146
       if (len < (int)sizeof(struct ipv6_opt_hdr)){
147
               *hotdrop = 1;
148
                return 0;
149
       }
150
 
151
       if (len < hdrlen){
152
               /* Packet smaller than it's length field */
153
                return 0;
154
       }
155
 
156
       optsh=(void *)(skb->data)+ptr;
157
 
158
       DEBUGP("IPv6 OPTS LEN %u %u ", hdrlen, optsh->hdrlen);
159
 
160
       DEBUGP("len %02X %04X %02X ",
161
                optinfo->hdrlen, hdrlen,
162
                (!(optinfo->flags & IP6T_OPTS_LEN) ||
163
                           ((optinfo->hdrlen == hdrlen) ^
164
                           !!(optinfo->invflags & IP6T_OPTS_INV_LEN))));
165
 
166
       ret = (optsh != NULL)
167
                &&
168
                (!(optinfo->flags & IP6T_OPTS_LEN) ||
169
                           ((optinfo->hdrlen == hdrlen) ^
170
                           !!(optinfo->invflags & IP6T_OPTS_INV_LEN)));
171
 
172
       temp = len = 0;
173
       ptr += 2;
174
       hdrlen -= 2;
175
       if ( !(optinfo->flags & IP6T_OPTS_OPTS) ){
176
               return ret;
177
        } else if (optinfo->flags & IP6T_OPTS_NSTRICT) {
178
                DEBUGP("Not strict - not implemented");
179
        } else {
180
                DEBUGP("Strict ");
181
                DEBUGP("#%d ",optinfo->optsnr);
182
                for(temp=0; temp<optinfo->optsnr; temp++){
183
                        optdesc = (void *)(skb->data)+ptr;
184
                        /* Type check */
185
                        if ( (unsigned char)*optdesc !=
186
                                (optinfo->opts[temp] & 0xFF00)>>8 ){
187
                                DEBUGP("Tbad %02X %02X\n",
188
                                                (unsigned char)*optdesc,
189
                                                (optinfo->opts[temp] &
190
                                                 0xFF00)>>8);
191
                                return 0;
192
                        } else {
193
                                DEBUGP("Tok ");
194
                        }
195
                        /* Length check */
196
                        if (((optinfo->opts[temp] & 0x00FF) != 0xFF) &&
197
                                (unsigned char)*optdesc != 0){
198
                                if ( ntohs((u16)*optdesc) !=
199
                                                optinfo->opts[temp] ){
200
                                        DEBUGP("Lbad %02X %04X %04X\n",
201
                                                        (unsigned char)*optdesc,
202
                                                        ntohs((u16)*optdesc),
203
                                                        optinfo->opts[temp]);
204
                                        return 0;
205
                                } else {
206
                                        DEBUGP("Lok ");
207
                                }
208
                        }
209
                        /* Step to the next */
210
                        if ((unsigned char)*optdesc == 0){
211
                                DEBUGP("PAD0 \n");
212
                                ptr++;
213
                                hdrlen--;
214
                        } else {
215
                                ptr += LOW(ntohs(*optdesc));
216
                                hdrlen -= LOW(ntohs(*optdesc));
217
                                DEBUGP("len%04X \n",
218
                                        LOW(ntohs(*optdesc)));
219
                        }
220
                        if (ptr > skb->len || ( !hdrlen &&
221
                                (temp != optinfo->optsnr - 1))) {
222
                                DEBUGP("new pointer is too large! \n");
223
                                break;
224
                        }
225
                }
226
                if (temp == optinfo->optsnr)
227
                        return ret;
228
                else return 0;
229
        }
230
 
231
        return 0;
232
}
233
 
234
/* Called when user tries to insert an entry of this type. */
235
static int
236
checkentry(const char *tablename,
237
          const struct ip6t_ip6 *ip,
238
          void *matchinfo,
239
          unsigned int matchinfosize,
240
          unsigned int hook_mask)
241
{
242
       const struct ip6t_opts *optsinfo = matchinfo;
243
 
244
       if (matchinfosize != IP6T_ALIGN(sizeof(struct ip6t_opts))) {
245
              DEBUGP("ip6t_opts: matchsize %u != %u\n",
246
                      matchinfosize, IP6T_ALIGN(sizeof(struct ip6t_opts)));
247
              return 0;
248
       }
249
       if (optsinfo->invflags & ~IP6T_OPTS_INV_MASK) {
250
              DEBUGP("ip6t_opts: unknown flags %X\n",
251
                      optsinfo->invflags);
252
              return 0;
253
       }
254
 
255
       return 1;
256
}
257
 
258
static struct ip6t_match opts_match
259
#if HOPBYHOP
260
= { { NULL, NULL }, "hbh", &match, &checkentry, NULL, THIS_MODULE };
261
#else
262
= { { NULL, NULL }, "dst", &match, &checkentry, NULL, THIS_MODULE };
263
#endif
264
 
265
static int __init init(void)
266
{
267
       return ip6t_register_match(&opts_match);
268
}
269
 
270
static void __exit cleanup(void)
271
{
272
       ip6t_unregister_match(&opts_match);
273
}
274
 
275
module_init(init);
276
module_exit(cleanup);

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.