1 /* Kernel module to match AH 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_ah.h>
13 MODULE_LICENSE("GPL");
14 MODULE_DESCRIPTION("IPv6 AH match");
15 MODULE_AUTHOR("Andras Kis-Szabo <kisza@sch.bme.hu>");
20 #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("ah 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 ahhdr *ah = NULL;
53 const struct ip6t_ah *ahinfo = matchinfo;
58 unsigned int hdrlen = 0;
60 /*DEBUGP("IPv6 AH entered\n");*/
61 /* if (opt->auth == 0) return 0;
62 * It does not filled on output */
64 /* type of the 1st exthdr */
65 nexthdr = skb->nh.ipv6h->nexthdr;
66 /* pointer to the 1st exthdr */
67 ptr = sizeof(struct ipv6hdr);
68 /* available length */
72 while (ip6t_ext_hdr(nexthdr)) {
73 struct ipv6_opt_hdr *hdr;
75 DEBUGP("ipv6_ah header iteration \n");
77 /* Is there enough space for the next ext header? */
78 if (len < (int)sizeof(struct ipv6_opt_hdr))
80 /* No more exthdr -> evaluate */
81 if (nexthdr == NEXTHDR_NONE) {
85 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);
100 if (nexthdr == NEXTHDR_AUTH) {
109 case NEXTHDR_ROUTING:
110 case NEXTHDR_FRAGMENT:
115 DEBUGP("ipv6_ah match: unknown nextheader %u\n",nexthdr);
120 nexthdr = hdr->nexthdr;
123 if ( ptr > skb->len ) {
124 DEBUGP("ipv6_ah: new pointer too large! \n");
129 /* AH header not found */
130 if ( temp != MASK_AH ) return 0;
132 if (len < (int)sizeof(struct ahhdr)){
137 ah = (struct ahhdr *) (skb->data + ptr);
139 DEBUGP("IPv6 AH LEN %u %u ", hdrlen, ah->hdrlen);
140 DEBUGP("RES %04X ", ah->reserved);
141 DEBUGP("SPI %u %08X\n", ntohl(ah->spi), ntohl(ah->spi));
143 DEBUGP("IPv6 AH spi %02X ",
144 (spi_match(ahinfo->spis[0], ahinfo->spis[1],
146 !!(ahinfo->invflags & IP6T_AH_INV_SPI))));
147 DEBUGP("len %02X %04X %02X ",
148 ahinfo->hdrlen, hdrlen,
150 (ahinfo->hdrlen == hdrlen) ^
151 !!(ahinfo->invflags & IP6T_AH_INV_LEN)));
152 DEBUGP("res %02X %04X %02X\n",
153 ahinfo->hdrres, ah->reserved,
154 !(ahinfo->hdrres && ah->reserved));
158 (spi_match(ahinfo->spis[0], ahinfo->spis[1],
160 !!(ahinfo->invflags & IP6T_AH_INV_SPI)))
163 (ahinfo->hdrlen == hdrlen) ^
164 !!(ahinfo->invflags & IP6T_AH_INV_LEN))
166 !(ahinfo->hdrres && ah->reserved);
169 /* Called when user tries to insert an entry of this type. */
171 checkentry(const char *tablename,
172 const struct ip6t_ip6 *ip,
174 unsigned int matchinfosize,
175 unsigned int hook_mask)
177 const struct ip6t_ah *ahinfo = matchinfo;
179 if (matchinfosize != IP6T_ALIGN(sizeof(struct ip6t_ah))) {
180 DEBUGP("ip6t_ah: matchsize %u != %u\n",
181 matchinfosize, IP6T_ALIGN(sizeof(struct ip6t_ah)));
184 if (ahinfo->invflags & ~IP6T_AH_INV_MASK) {
185 DEBUGP("ip6t_ah: unknown flags %X\n",
193 static struct ip6t_match ah_match
194 = { { NULL, NULL }, "ah", &match, &checkentry, NULL, THIS_MODULE };
196 static int __init init(void)
198 return ip6t_register_match(&ah_match);
201 static void __exit cleanup(void)
203 ip6t_unregister_match(&ah_match);
207 module_exit(cleanup);