Linux-2.6.12-rc2
[linux-flexiantxendom0-natty.git] / net / ipv4 / netfilter / ipt_ah.c
1 /* Kernel module to match AH parameters. */
2 /* (C) 1999-2000 Yon Uriarte <yon@astaro.de>
3  *
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.
7  */
8
9 #include <linux/module.h>
10 #include <linux/skbuff.h>
11 #include <linux/ip.h>
12
13 #include <linux/netfilter_ipv4/ipt_ah.h>
14 #include <linux/netfilter_ipv4/ip_tables.h>
15
16 MODULE_LICENSE("GPL");
17 MODULE_AUTHOR("Yon Uriarte <yon@astaro.de>");
18 MODULE_DESCRIPTION("iptables AH SPI match module");
19
20 #ifdef DEBUG_CONNTRACK
21 #define duprintf(format, args...) printk(format , ## args)
22 #else
23 #define duprintf(format, args...)
24 #endif
25
26 /* Returns 1 if the spi is matched by the range, 0 otherwise */
27 static inline int
28 spi_match(u_int32_t min, u_int32_t max, u_int32_t spi, int invert)
29 {
30         int r=0;
31         duprintf("ah spi_match:%c 0x%x <= 0x%x <= 0x%x",invert? '!':' ',
32                 min,spi,max);
33         r=(spi >= min && spi <= max) ^ invert;
34         duprintf(" result %s\n",r? "PASS" : "FAILED");
35         return r;
36 }
37
38 static int
39 match(const struct sk_buff *skb,
40       const struct net_device *in,
41       const struct net_device *out,
42       const void *matchinfo,
43       int offset,
44       int *hotdrop)
45 {
46         struct ip_auth_hdr _ahdr, *ah;
47         const struct ipt_ah *ahinfo = matchinfo;
48
49         /* Must not be a fragment. */
50         if (offset)
51                 return 0;
52
53         ah = skb_header_pointer(skb, skb->nh.iph->ihl * 4,
54                                 sizeof(_ahdr), &_ahdr);
55         if (ah == NULL) {
56                 /* We've been asked to examine this packet, and we
57                  * can't.  Hence, no choice but to drop.
58                  */
59                 duprintf("Dropping evil AH tinygram.\n");
60                 *hotdrop = 1;
61                 return 0;
62         }
63
64         return spi_match(ahinfo->spis[0], ahinfo->spis[1],
65                          ntohl(ah->spi),
66                          !!(ahinfo->invflags & IPT_AH_INV_SPI));
67 }
68
69 /* Called when user tries to insert an entry of this type. */
70 static int
71 checkentry(const char *tablename,
72            const struct ipt_ip *ip,
73            void *matchinfo,
74            unsigned int matchinfosize,
75            unsigned int hook_mask)
76 {
77         const struct ipt_ah *ahinfo = matchinfo;
78
79         /* Must specify proto == AH, and no unknown invflags */
80         if (ip->proto != IPPROTO_AH || (ip->invflags & IPT_INV_PROTO)) {
81                 duprintf("ipt_ah: Protocol %u != %u\n", ip->proto,
82                          IPPROTO_AH);
83                 return 0;
84         }
85         if (matchinfosize != IPT_ALIGN(sizeof(struct ipt_ah))) {
86                 duprintf("ipt_ah: matchsize %u != %u\n",
87                          matchinfosize, IPT_ALIGN(sizeof(struct ipt_ah)));
88                 return 0;
89         }
90         if (ahinfo->invflags & ~IPT_AH_INV_MASK) {
91                 duprintf("ipt_ah: unknown flags %X\n",
92                          ahinfo->invflags);
93                 return 0;
94         }
95
96         return 1;
97 }
98
99 static struct ipt_match ah_match = {
100         .name           = "ah",
101         .match          = &match,
102         .checkentry     = &checkentry,
103         .me             = THIS_MODULE,
104 };
105
106 static int __init init(void)
107 {
108         return ipt_register_match(&ah_match);
109 }
110
111 static void __exit cleanup(void)
112 {
113         ipt_unregister_match(&ah_match);
114 }
115
116 module_init(init);
117 module_exit(cleanup);