1 /* Kernel module to match ESP 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 <linux/netfilter_ipv6/ip6_tables.h>
10 #include <linux/netfilter_ipv6/ip6t_esp.h>
13 MODULE_LICENSE("GPL");
14 MODULE_DESCRIPTION("IPv6 ESP match");
15 MODULE_AUTHOR("Andras Kis-Szabo <kisza@sch.bme.hu>");
20 #define DEBUGP(format, args...)
28 /* Returns 1 if the spi is matched by the range, 0 otherwise */
30 spi_match(u_int32_t min, u_int32_t max, u_int32_t spi, int invert)
33 DEBUGP("esp spi_match:%c 0x%x <= 0x%x <= 0x%x",invert? '!':' ',
35 r=(spi >= min && spi <= max) ^ invert;
36 DEBUGP(" result %s\n",r? "PASS\n" : "FAILED\n");
41 match(const struct sk_buff *skb,
42 const struct net_device *in,
43 const struct net_device *out,
44 const void *matchinfo,
50 struct esphdr *esp = NULL;
51 const struct ip6t_esp *espinfo = matchinfo;
57 /* Make sure this isn't an evil packet */
58 /*DEBUGP("ipv6_esp entered \n");*/
60 /* type of the 1st exthdr */
61 nexthdr = skb->nh.ipv6h->nexthdr;
62 /* pointer to the 1st exthdr */
63 ptr = sizeof(struct ipv6hdr);
64 /* available length */
68 while (ip6t_ext_hdr(nexthdr)) {
69 struct ipv6_opt_hdr *hdr;
72 DEBUGP("ipv6_esp header iteration \n");
74 /* Is there enough space for the next ext header? */
75 if (len < (int)sizeof(struct ipv6_opt_hdr))
77 /* No more exthdr -> evaluate */
78 if (nexthdr == NEXTHDR_NONE) {
82 if (nexthdr == NEXTHDR_ESP) {
87 hdr=(struct ipv6_opt_hdr *)skb->data+ptr;
89 /* Calculate the header length */
90 if (nexthdr == NEXTHDR_FRAGMENT) {
92 } else if (nexthdr == NEXTHDR_AUTH)
93 hdrlen = (hdr->hdrlen+2)<<2;
95 hdrlen = ipv6_optlen(hdr);
100 case NEXTHDR_ROUTING:
101 case NEXTHDR_FRAGMENT:
106 DEBUGP("ipv6_esp match: unknown nextheader %u\n",nexthdr);
111 nexthdr = hdr->nexthdr;
114 if ( ptr > skb->len ) {
115 DEBUGP("ipv6_esp: new pointer too large! \n");
120 /* ESP header not found */
121 if ( temp != MASK_ESP ) return 0;
123 if (len < (int)sizeof(struct esphdr)){
128 esp = (struct esphdr *) (skb->data + ptr);
130 DEBUGP("IPv6 ESP SPI %u %08X\n", ntohl(esp->spi), ntohl(esp->spi));
133 && spi_match(espinfo->spis[0], espinfo->spis[1],
135 !!(espinfo->invflags & IP6T_ESP_INV_SPI));
138 /* Called when user tries to insert an entry of this type. */
140 checkentry(const char *tablename,
141 const struct ip6t_ip6 *ip,
143 unsigned int matchinfosize,
144 unsigned int hook_mask)
146 const struct ip6t_esp *espinfo = matchinfo;
148 if (matchinfosize != IP6T_ALIGN(sizeof(struct ip6t_esp))) {
149 DEBUGP("ip6t_esp: matchsize %u != %u\n",
150 matchinfosize, IP6T_ALIGN(sizeof(struct ip6t_esp)));
153 if (espinfo->invflags & ~IP6T_ESP_INV_MASK) {
154 DEBUGP("ip6t_esp: unknown flags %X\n",
162 static struct ip6t_match esp_match
163 = { { NULL, NULL }, "esp", &match, &checkentry, NULL, THIS_MODULE };
165 static int __init init(void)
167 return ip6t_register_match(&esp_match);
170 static void __exit cleanup(void)
172 ip6t_unregister_match(&esp_match);
176 module_exit(cleanup);