[NETFILTER]: Fix ip6_tables extension header bypass bug
[deliverable/linux.git] / net / ipv6 / netfilter / ip6t_ah.c
CommitLineData
1da177e4
LT
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>
14c85021 12#include <linux/ip.h>
1da177e4
LT
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_ipv6/ip6_tables.h>
19#include <linux/netfilter_ipv6/ip6t_ah.h>
20
21MODULE_LICENSE("GPL");
22MODULE_DESCRIPTION("IPv6 AH match");
23MODULE_AUTHOR("Andras Kis-Szabo <kisza@sch.bme.hu>");
24
25#if 0
26#define DEBUGP printk
27#else
28#define DEBUGP(format, args...)
29#endif
30
31/* Returns 1 if the spi is matched by the range, 0 otherwise */
32static inline int
33spi_match(u_int32_t min, u_int32_t max, u_int32_t spi, int invert)
34{
35 int r=0;
36 DEBUGP("ah spi_match:%c 0x%x <= 0x%x <= 0x%x",invert? '!':' ',
37 min,spi,max);
38 r = (spi >= min && spi <= max) ^ invert;
39 DEBUGP(" result %s\n",r? "PASS\n" : "FAILED\n");
40 return r;
41}
42
43static int
44match(const struct sk_buff *skb,
45 const struct net_device *in,
46 const struct net_device *out,
c4986734 47 const struct xt_match *match,
1da177e4
LT
48 const void *matchinfo,
49 int offset,
50 unsigned int protoff,
51 int *hotdrop)
52{
e674d0f3 53 struct ip_auth_hdr *ah, _ah;
1da177e4 54 const struct ip6t_ah *ahinfo = matchinfo;
1da177e4
LT
55 unsigned int ptr;
56 unsigned int hdrlen = 0;
6d381634 57 int err;
1da177e4 58
6d381634
PM
59 err = ipv6_find_hdr(skb, &ptr, NEXTHDR_AUTH, NULL);
60 if (err < 0) {
61 if (err != -ENOENT)
62 *hotdrop = 1;
1da177e4 63 return 0;
6d381634 64 }
1da177e4 65
e674d0f3
YK
66 ah = skb_header_pointer(skb, ptr, sizeof(_ah), &_ah);
67 if (ah == NULL) {
1da177e4
LT
68 *hotdrop = 1;
69 return 0;
70 }
71
e674d0f3 72 hdrlen = (ah->hdrlen + 2) << 2;
1da177e4
LT
73
74 DEBUGP("IPv6 AH LEN %u %u ", hdrlen, ah->hdrlen);
75 DEBUGP("RES %04X ", ah->reserved);
76 DEBUGP("SPI %u %08X\n", ntohl(ah->spi), ntohl(ah->spi));
77
78 DEBUGP("IPv6 AH spi %02X ",
79 (spi_match(ahinfo->spis[0], ahinfo->spis[1],
80 ntohl(ah->spi),
81 !!(ahinfo->invflags & IP6T_AH_INV_SPI))));
82 DEBUGP("len %02X %04X %02X ",
83 ahinfo->hdrlen, hdrlen,
84 (!ahinfo->hdrlen ||
85 (ahinfo->hdrlen == hdrlen) ^
86 !!(ahinfo->invflags & IP6T_AH_INV_LEN)));
87 DEBUGP("res %02X %04X %02X\n",
88 ahinfo->hdrres, ah->reserved,
89 !(ahinfo->hdrres && ah->reserved));
90
91 return (ah != NULL)
92 &&
93 (spi_match(ahinfo->spis[0], ahinfo->spis[1],
94 ntohl(ah->spi),
95 !!(ahinfo->invflags & IP6T_AH_INV_SPI)))
96 &&
97 (!ahinfo->hdrlen ||
98 (ahinfo->hdrlen == hdrlen) ^
99 !!(ahinfo->invflags & IP6T_AH_INV_LEN))
100 &&
101 !(ahinfo->hdrres && ah->reserved);
102}
103
104/* Called when user tries to insert an entry of this type. */
105static int
106checkentry(const char *tablename,
2e4e6a17 107 const void *entry,
c4986734 108 const struct xt_match *match,
1da177e4 109 void *matchinfo,
1da177e4
LT
110 unsigned int hook_mask)
111{
112 const struct ip6t_ah *ahinfo = matchinfo;
113
1da177e4
LT
114 if (ahinfo->invflags & ~IP6T_AH_INV_MASK) {
115 DEBUGP("ip6t_ah: unknown flags %X\n", ahinfo->invflags);
116 return 0;
117 }
118 return 1;
119}
120
121static struct ip6t_match ah_match = {
122 .name = "ah",
7f939713
PM
123 .match = match,
124 .matchsize = sizeof(struct ip6t_ah),
125 .checkentry = checkentry,
1da177e4
LT
126 .me = THIS_MODULE,
127};
128
65b4b4e8 129static int __init ip6t_ah_init(void)
1da177e4
LT
130{
131 return ip6t_register_match(&ah_match);
132}
133
65b4b4e8 134static void __exit ip6t_ah_fini(void)
1da177e4
LT
135{
136 ip6t_unregister_match(&ah_match);
137}
138
65b4b4e8
AM
139module_init(ip6t_ah_init);
140module_exit(ip6t_ah_fini);
This page took 0.182903 seconds and 5 git commands to generate.