1 /* 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>
9 #include <asm/byteorder.h>
11 #include <linux/netfilter_ipv6/ip6_tables.h>
12 #include <linux/netfilter_ipv6/ip6t_opts.h>
17 MODULE_LICENSE("GPL");
19 MODULE_DESCRIPTION("IPv6 HbH match");
21 MODULE_DESCRIPTION("IPv6 DST match");
23 MODULE_AUTHOR("Andras Kis-Szabo <kisza@sch.bme.hu>");
28 #define DEBUGP(format, args...)
34 * 1 -> must drop the packet
35 * 2 -> send ICMP PARM PROB regardless and drop packet
36 * 3 -> Send ICMP if not a multicast address and drop packet
39 * 1 -> can change the routing
41 * 0 -> Pad1 (only 1 byte!)
42 * 1 -> PadN LENGTH info (total length = length + 2)
43 * C0 | 2 -> JUMBO 4 x x x x ( xxxx > 64k )
48 match(const struct sk_buff *skb,
49 const struct net_device *in,
50 const struct net_device *out,
51 const void *matchinfo,
57 struct ipv6_opt_hdr *optsh = NULL;
58 const struct ip6t_opts *optinfo = matchinfo;
63 unsigned int hdrlen = 0;
68 /* type of the 1st exthdr */
69 nexthdr = skb->nh.ipv6h->nexthdr;
70 /* pointer to the 1st exthdr */
71 ptr = sizeof(struct ipv6hdr);
72 /* available length */
76 while (ip6t_ext_hdr(nexthdr)) {
77 struct ipv6_opt_hdr *hdr;
79 DEBUGP("ipv6_opts header iteration \n");
81 /* Is there enough space for the next ext header? */
82 if (len < (int)sizeof(struct ipv6_opt_hdr))
84 /* No more exthdr -> evaluate */
85 if (nexthdr == NEXTHDR_NONE) {
89 if (nexthdr == NEXTHDR_ESP) {
93 hdr = (void *)(skb->data + ptr);
95 /* Calculate the header length */
96 if (nexthdr == NEXTHDR_FRAGMENT) {
98 } else if (nexthdr == NEXTHDR_AUTH)
99 hdrlen = (hdr->hdrlen+2)<<2;
101 hdrlen = ipv6_optlen(hdr);
103 /* OPTS -> evaluate */
105 if (nexthdr == NEXTHDR_HOP) {
106 temp |= MASK_HOPOPTS;
108 if (nexthdr == NEXTHDR_DEST) {
109 temp |= MASK_DSTOPTS;
118 case NEXTHDR_ROUTING:
119 case NEXTHDR_FRAGMENT:
124 DEBUGP("ipv6_opts match: unknown nextheader %u\n",nexthdr);
129 nexthdr = hdr->nexthdr;
132 if ( ptr > skb->len ) {
133 DEBUGP("ipv6_opts: new pointer is too large! \n");
138 /* OPTIONS header not found */
140 if ( temp != MASK_HOPOPTS ) return 0;
142 if ( temp != MASK_DSTOPTS ) return 0;
145 if (len < (int)sizeof(struct ipv6_opt_hdr)){
151 /* Packet smaller than it's length field */
155 optsh = (void *)(skb->data + ptr);
157 DEBUGP("IPv6 OPTS LEN %u %u ", hdrlen, optsh->hdrlen);
159 DEBUGP("len %02X %04X %02X ",
160 optinfo->hdrlen, hdrlen,
161 (!(optinfo->flags & IP6T_OPTS_LEN) ||
162 ((optinfo->hdrlen == hdrlen) ^
163 !!(optinfo->invflags & IP6T_OPTS_INV_LEN))));
165 ret = (optsh != NULL)
167 (!(optinfo->flags & IP6T_OPTS_LEN) ||
168 ((optinfo->hdrlen == hdrlen) ^
169 !!(optinfo->invflags & IP6T_OPTS_INV_LEN)));
173 if ( !(optinfo->flags & IP6T_OPTS_OPTS) ){
175 } else if (optinfo->flags & IP6T_OPTS_NSTRICT) {
176 DEBUGP("Not strict - not implemented");
179 DEBUGP("#%d ",optinfo->optsnr);
180 for(temp=0; temp<optinfo->optsnr; temp++){
181 /* type field exists ? */
182 if (ptr > skb->len - 1 || hdrlen < 1)
184 opttype = (void *)(skb->data + ptr);
187 if (*opttype != (optinfo->opts[temp] & 0xFF00)>>8){
188 DEBUGP("Tbad %02X %02X\n",
190 (optinfo->opts[temp] & 0xFF00)>>8);
199 /* length field exists ? */
200 if (ptr > skb->len - 2 || hdrlen < 2)
202 optlen = *((u8 *)(skb->data + ptr + 1));
203 spec_len = optinfo->opts[temp] & 0x00FF;
205 if (spec_len != 0x00FF && spec_len != optlen) {
206 DEBUGP("Lbad %02X %04X\n", optlen,
217 /* Step to the next */
218 DEBUGP("len%04X \n", optlen);
220 if ((ptr > skb->len - optlen || hdrlen < optlen) &&
221 (temp < optinfo->optsnr - 1)) {
222 DEBUGP("new pointer is too large! \n");
228 if (temp == optinfo->optsnr)
236 /* Called when user tries to insert an entry of this type. */
238 checkentry(const char *tablename,
239 const struct ip6t_ip6 *ip,
241 unsigned int matchinfosize,
242 unsigned int hook_mask)
244 const struct ip6t_opts *optsinfo = matchinfo;
246 if (matchinfosize != IP6T_ALIGN(sizeof(struct ip6t_opts))) {
247 DEBUGP("ip6t_opts: matchsize %u != %u\n",
248 matchinfosize, IP6T_ALIGN(sizeof(struct ip6t_opts)));
251 if (optsinfo->invflags & ~IP6T_OPTS_INV_MASK) {
252 DEBUGP("ip6t_opts: unknown flags %X\n",
260 static struct ip6t_match opts_match
262 = { { NULL, NULL }, "hbh", &match, &checkentry, NULL, THIS_MODULE };
264 = { { NULL, NULL }, "dst", &match, &checkentry, NULL, THIS_MODULE };
267 static int __init init(void)
269 return ip6t_register_match(&opts_match);
272 static void __exit cleanup(void)
274 ip6t_unregister_match(&opts_match);
278 module_exit(cleanup);