Merge branch 'upstream' of git://ftp.linux-mips.org/pub/scm/upstream-linus
[linux-drm-fsl-dcu.git] / net / ipv6 / netfilter / ip6t_ah.c
1 /* Kernel module to match AH parameters. */
2
3 /* (C) 2001-2002 Andras Kis-Szabo <kisza@sch.bme.hu>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation.
8  */
9
10 #include <linux/module.h>
11 #include <linux/skbuff.h>
12 #include <linux/ip.h>
13 #include <linux/ipv6.h>
14 #include <linux/types.h>
15 #include <net/checksum.h>
16 #include <net/ipv6.h>
17
18 #include <linux/netfilter/x_tables.h>
19 #include <linux/netfilter_ipv6/ip6_tables.h>
20 #include <linux/netfilter_ipv6/ip6t_ah.h>
21
22 MODULE_LICENSE("GPL");
23 MODULE_DESCRIPTION("IPv6 AH match");
24 MODULE_AUTHOR("Andras Kis-Szabo <kisza@sch.bme.hu>");
25
26 #if 0
27 #define DEBUGP printk
28 #else
29 #define DEBUGP(format, args...)
30 #endif
31
32 /* Returns 1 if the spi is matched by the range, 0 otherwise */
33 static inline int
34 spi_match(u_int32_t min, u_int32_t max, u_int32_t spi, int invert)
35 {
36         int r=0;
37         DEBUGP("ah spi_match:%c 0x%x <= 0x%x <= 0x%x",invert? '!':' ',
38                min,spi,max);
39         r = (spi >= min && spi <= max) ^ invert;
40         DEBUGP(" result %s\n",r? "PASS\n" : "FAILED\n");
41         return r;
42 }
43
44 static int
45 match(const struct sk_buff *skb,
46       const struct net_device *in,
47       const struct net_device *out,
48       const struct xt_match *match,
49       const void *matchinfo,
50       int offset,
51       unsigned int protoff,
52       int *hotdrop)
53 {
54         struct ip_auth_hdr *ah, _ah;
55         const struct ip6t_ah *ahinfo = matchinfo;
56         unsigned int ptr;
57         unsigned int hdrlen = 0;
58         int err;
59
60         err = ipv6_find_hdr(skb, &ptr, NEXTHDR_AUTH, NULL);
61         if (err < 0) {
62                 if (err != -ENOENT)
63                         *hotdrop = 1;
64                 return 0;
65         }
66
67         ah = skb_header_pointer(skb, ptr, sizeof(_ah), &_ah);
68         if (ah == NULL) {
69                 *hotdrop = 1;
70                 return 0;
71         }
72
73         hdrlen = (ah->hdrlen + 2) << 2;
74
75         DEBUGP("IPv6 AH LEN %u %u ", hdrlen, ah->hdrlen);
76         DEBUGP("RES %04X ", ah->reserved);
77         DEBUGP("SPI %u %08X\n", ntohl(ah->spi), ntohl(ah->spi));
78
79         DEBUGP("IPv6 AH spi %02X ",
80                (spi_match(ahinfo->spis[0], ahinfo->spis[1],
81                           ntohl(ah->spi),
82                           !!(ahinfo->invflags & IP6T_AH_INV_SPI))));
83         DEBUGP("len %02X %04X %02X ",
84                ahinfo->hdrlen, hdrlen,
85                (!ahinfo->hdrlen ||
86                 (ahinfo->hdrlen == hdrlen) ^
87                 !!(ahinfo->invflags & IP6T_AH_INV_LEN)));
88         DEBUGP("res %02X %04X %02X\n",
89                ahinfo->hdrres, ah->reserved,
90                !(ahinfo->hdrres && ah->reserved));
91
92         return (ah != NULL)
93                &&
94                (spi_match(ahinfo->spis[0], ahinfo->spis[1],
95                           ntohl(ah->spi),
96                           !!(ahinfo->invflags & IP6T_AH_INV_SPI)))
97                &&
98                (!ahinfo->hdrlen ||
99                 (ahinfo->hdrlen == hdrlen) ^
100                 !!(ahinfo->invflags & IP6T_AH_INV_LEN))
101                &&
102                !(ahinfo->hdrres && ah->reserved);
103 }
104
105 /* Called when user tries to insert an entry of this type. */
106 static int
107 checkentry(const char *tablename,
108           const void *entry,
109           const struct xt_match *match,
110           void *matchinfo,
111           unsigned int hook_mask)
112 {
113         const struct ip6t_ah *ahinfo = matchinfo;
114
115         if (ahinfo->invflags & ~IP6T_AH_INV_MASK) {
116                 DEBUGP("ip6t_ah: unknown flags %X\n", ahinfo->invflags);
117                 return 0;
118         }
119         return 1;
120 }
121
122 static struct xt_match ah_match = {
123         .name           = "ah",
124         .family         = AF_INET6,
125         .match          = match,
126         .matchsize      = sizeof(struct ip6t_ah),
127         .checkentry     = checkentry,
128         .me             = THIS_MODULE,
129 };
130
131 static int __init ip6t_ah_init(void)
132 {
133         return xt_register_match(&ah_match);
134 }
135
136 static void __exit ip6t_ah_fini(void)
137 {
138         xt_unregister_match(&ah_match);
139 }
140
141 module_init(ip6t_ah_init);
142 module_exit(ip6t_ah_fini);