1 /* Kernel module to match ESP parameters. */
2 /* (C) 2001-2002 Andras Kis-Szabo <kisza@sch.bme.hu>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
10 #include <linux/module.h>
11 #include <linux/skbuff.h>
12 #include <linux/ipv6.h>
13 #include <linux/types.h>
14 #include <net/checksum.h>
17 #include <linux/netfilter_ipv6/ip6_tables.h>
18 #include <linux/netfilter_ipv6/ip6t_esp.h>
20 MODULE_LICENSE("GPL");
21 MODULE_DESCRIPTION("IPv6 ESP match");
22 MODULE_AUTHOR("Andras Kis-Szabo <kisza@sch.bme.hu>");
27 #define DEBUGP(format, args...)
30 /* Returns 1 if the spi is matched by the range, 0 otherwise */
32 spi_match(u_int32_t min
, u_int32_t max
, u_int32_t spi
, int invert
)
35 DEBUGP("esp spi_match:%c 0x%x <= 0x%x <= 0x%x",invert
? '!':' ',
37 r
=(spi
>= min
&& spi
<= max
) ^ invert
;
38 DEBUGP(" result %s\n",r
? "PASS\n" : "FAILED\n");
43 match(const struct sk_buff
*skb
,
44 const struct net_device
*in
,
45 const struct net_device
*out
,
46 const void *matchinfo
,
52 struct ip_esp_hdr
*esp
= NULL
;
53 const struct ip6t_esp
*espinfo
= matchinfo
;
59 /* Make sure this isn't an evil packet */
60 /*DEBUGP("ipv6_esp entered \n");*/
62 /* type of the 1st exthdr */
63 nexthdr
= skb
->nh
.ipv6h
->nexthdr
;
64 /* pointer to the 1st exthdr */
65 ptr
= sizeof(struct ipv6hdr
);
66 /* available length */
70 while (ip6t_ext_hdr(nexthdr
)) {
71 struct ipv6_opt_hdr
*hdr
;
74 DEBUGP("ipv6_esp header iteration \n");
76 /* Is there enough space for the next ext header? */
77 if (len
< (int)sizeof(struct ipv6_opt_hdr
))
79 /* No more exthdr -> evaluate */
80 if (nexthdr
== NEXTHDR_NONE
) {
84 if (nexthdr
== NEXTHDR_ESP
) {
89 hdr
=(struct ipv6_opt_hdr
*)skb
->data
+ptr
;
91 /* Calculate the header length */
92 if (nexthdr
== NEXTHDR_FRAGMENT
) {
94 } else if (nexthdr
== NEXTHDR_AUTH
)
95 hdrlen
= (hdr
->hdrlen
+2)<<2;
97 hdrlen
= ipv6_optlen(hdr
);
102 case NEXTHDR_ROUTING
:
103 case NEXTHDR_FRAGMENT
:
108 DEBUGP("ipv6_esp match: unknown nextheader %u\n",nexthdr
);
113 nexthdr
= hdr
->nexthdr
;
116 if ( ptr
> skb
->len
) {
117 DEBUGP("ipv6_esp: new pointer too large! \n");
122 /* ESP header not found */
123 if ( temp
!= MASK_ESP
) return 0;
125 if (len
< (int)sizeof(struct ip_esp_hdr
)){
130 esp
= (struct ip_esp_hdr
*) (skb
->data
+ ptr
);
132 DEBUGP("IPv6 ESP SPI %u %08X\n", ntohl(esp
->spi
), ntohl(esp
->spi
));
135 && spi_match(espinfo
->spis
[0], espinfo
->spis
[1],
137 !!(espinfo
->invflags
& IP6T_ESP_INV_SPI
));
140 /* Called when user tries to insert an entry of this type. */
142 checkentry(const char *tablename
,
143 const struct ip6t_ip6
*ip
,
145 unsigned int matchinfosize
,
146 unsigned int hook_mask
)
148 const struct ip6t_esp
*espinfo
= matchinfo
;
150 if (matchinfosize
!= IP6T_ALIGN(sizeof(struct ip6t_esp
))) {
151 DEBUGP("ip6t_esp: matchsize %u != %u\n",
152 matchinfosize
, IP6T_ALIGN(sizeof(struct ip6t_esp
)));
155 if (espinfo
->invflags
& ~IP6T_ESP_INV_MASK
) {
156 DEBUGP("ip6t_esp: unknown flags %X\n",
164 static struct ip6t_match esp_match
= {
167 .checkentry
= &checkentry
,
171 static int __init
init(void)
173 return ip6t_register_match(&esp_match
);
176 static void __exit
cleanup(void)
178 ip6t_unregister_match(&esp_match
);
182 module_exit(cleanup
);