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,
51 struct ip_esp_hdr _esp, *eh = NULL;
52 const struct ip6t_esp *espinfo = matchinfo;
58 /* Make sure this isn't an evil packet */
59 /*DEBUGP("ipv6_esp entered \n");*/
61 /* type of the 1st exthdr */
62 nexthdr = skb->nh.ipv6h->nexthdr;
63 /* pointer to the 1st exthdr */
64 ptr = sizeof(struct ipv6hdr);
65 /* available length */
69 while (ip6t_ext_hdr(nexthdr)) {
70 struct ipv6_opt_hdr _hdr, *hp;
73 DEBUGP("ipv6_esp header iteration \n");
75 /* Is there enough space for the next ext header? */
76 if (len < sizeof(struct ipv6_opt_hdr))
78 /* No more exthdr -> evaluate */
79 if (nexthdr == NEXTHDR_NONE)
82 if (nexthdr == NEXTHDR_ESP) {
87 hp = skb_header_pointer(skb, ptr, sizeof(_hdr), &_hdr);
90 /* Calculate the header length */
91 if (nexthdr == NEXTHDR_FRAGMENT)
93 else if (nexthdr == NEXTHDR_AUTH)
94 hdrlen = (hp->hdrlen+2)<<2;
96 hdrlen = ipv6_optlen(hp);
101 case NEXTHDR_ROUTING:
102 case NEXTHDR_FRAGMENT:
107 DEBUGP("ipv6_esp match: unknown nextheader %u\n",nexthdr);
111 nexthdr = hp->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)
124 if (len < sizeof(struct ip_esp_hdr)) {
129 eh = skb_header_pointer(skb, ptr, sizeof(_esp), &_esp);
132 DEBUGP("IPv6 ESP SPI %u %08X\n", ntohl(eh->spi), ntohl(eh->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",
163 static struct ip6t_match esp_match = {
166 .checkentry = &checkentry,
170 static int __init init(void)
172 return ip6t_register_match(&esp_match);
175 static void __exit cleanup(void)
177 ip6t_unregister_match(&esp_match);
181 module_exit(cleanup);