]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - net/ipv6/netfilter/ip6t_frag.c
[NETFILTER]: Remove redundant parentheses/braces
[mirror_ubuntu-bionic-kernel.git] / net / ipv6 / netfilter / ip6t_frag.c
CommitLineData
1da177e4
LT
1/* Kernel module to match FRAG 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/ipv6.h>
13#include <linux/types.h>
14#include <net/checksum.h>
15#include <net/ipv6.h>
16
6709dbbb 17#include <linux/netfilter/x_tables.h>
1da177e4
LT
18#include <linux/netfilter_ipv6/ip6_tables.h>
19#include <linux/netfilter_ipv6/ip6t_frag.h>
20
21MODULE_LICENSE("GPL");
22MODULE_DESCRIPTION("IPv6 FRAG 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 id is matched by the range, 0 otherwise */
1d93a9cb
JE
32static inline bool
33id_match(u_int32_t min, u_int32_t max, u_int32_t id, bool invert)
1da177e4 34{
1d93a9cb 35 bool r;
f0daaa65
YK
36 DEBUGP("frag id_match:%c 0x%x <= 0x%x <= 0x%x", invert ? '!' : ' ',
37 min, id, max);
38 r = (id >= min && id <= max) ^ invert;
39 DEBUGP(" result %s\n", r ? "PASS" : "FAILED");
40 return r;
1da177e4
LT
41}
42
1d93a9cb 43static bool
1da177e4
LT
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,
cff533ac 51 bool *hotdrop)
1da177e4 52{
a47362a2
JE
53 struct frag_hdr _frag;
54 const struct frag_hdr *fh;
f0daaa65
YK
55 const struct ip6t_frag *fraginfo = matchinfo;
56 unsigned int ptr;
6d381634 57 int err;
1da177e4 58
6d381634
PM
59 err = ipv6_find_hdr(skb, &ptr, NEXTHDR_FRAGMENT, NULL);
60 if (err < 0) {
61 if (err != -ENOENT)
cff533ac 62 *hotdrop = true;
1d93a9cb 63 return false;
6d381634 64 }
1da177e4 65
e674d0f3 66 fh = skb_header_pointer(skb, ptr, sizeof(_frag), &_frag);
f0daaa65 67 if (fh == NULL) {
cff533ac 68 *hotdrop = true;
1d93a9cb 69 return false;
e674d0f3 70 }
1da177e4 71
f0daaa65
YK
72 DEBUGP("INFO %04X ", fh->frag_off);
73 DEBUGP("OFFSET %04X ", ntohs(fh->frag_off) & ~0x7);
74 DEBUGP("RES %02X %04X", fh->reserved, ntohs(fh->frag_off) & 0x6);
75 DEBUGP("MF %04X ", fh->frag_off & htons(IP6_MF));
76 DEBUGP("ID %u %08X\n", ntohl(fh->identification),
77 ntohl(fh->identification));
78
79 DEBUGP("IPv6 FRAG id %02X ",
7c4e36bc 80 id_match(fraginfo->ids[0], fraginfo->ids[1],
f0daaa65 81 ntohl(fh->identification),
7c4e36bc 82 !!(fraginfo->invflags & IP6T_FRAG_INV_IDS)));
f0daaa65 83 DEBUGP("res %02X %02X%04X %02X ",
7c4e36bc 84 fraginfo->flags & IP6T_FRAG_RES, fh->reserved,
f0daaa65
YK
85 ntohs(fh->frag_off) & 0x6,
86 !((fraginfo->flags & IP6T_FRAG_RES)
87 && (fh->reserved || (ntohs(fh->frag_off) & 0x06))));
88 DEBUGP("first %02X %02X %02X ",
7c4e36bc 89 fraginfo->flags & IP6T_FRAG_FST,
f0daaa65
YK
90 ntohs(fh->frag_off) & ~0x7,
91 !((fraginfo->flags & IP6T_FRAG_FST)
92 && (ntohs(fh->frag_off) & ~0x7)));
93 DEBUGP("mf %02X %02X %02X ",
7c4e36bc 94 fraginfo->flags & IP6T_FRAG_MF,
f0daaa65
YK
95 ntohs(fh->frag_off) & IP6_MF,
96 !((fraginfo->flags & IP6T_FRAG_MF)
97 && !((ntohs(fh->frag_off) & IP6_MF))));
98 DEBUGP("last %02X %02X %02X\n",
7c4e36bc 99 fraginfo->flags & IP6T_FRAG_NMF,
f0daaa65
YK
100 ntohs(fh->frag_off) & IP6_MF,
101 !((fraginfo->flags & IP6T_FRAG_NMF)
102 && (ntohs(fh->frag_off) & IP6_MF)));
103
104 return (fh != NULL)
105 &&
7c4e36bc
JE
106 id_match(fraginfo->ids[0], fraginfo->ids[1],
107 ntohl(fh->identification),
108 !!(fraginfo->invflags & IP6T_FRAG_INV_IDS))
f0daaa65
YK
109 &&
110 !((fraginfo->flags & IP6T_FRAG_RES)
111 && (fh->reserved || (ntohs(fh->frag_off) & 0x6)))
112 &&
113 !((fraginfo->flags & IP6T_FRAG_FST)
114 && (ntohs(fh->frag_off) & ~0x7))
115 &&
116 !((fraginfo->flags & IP6T_FRAG_MF)
117 && !(ntohs(fh->frag_off) & IP6_MF))
118 &&
119 !((fraginfo->flags & IP6T_FRAG_NMF)
120 && (ntohs(fh->frag_off) & IP6_MF));
1da177e4
LT
121}
122
123/* Called when user tries to insert an entry of this type. */
ccb79bdc 124static bool
1da177e4 125checkentry(const char *tablename,
f0daaa65 126 const void *ip,
c4986734 127 const struct xt_match *match,
f0daaa65 128 void *matchinfo,
f0daaa65 129 unsigned int hook_mask)
1da177e4 130{
f0daaa65
YK
131 const struct ip6t_frag *fraginfo = matchinfo;
132
f0daaa65
YK
133 if (fraginfo->invflags & ~IP6T_FRAG_INV_MASK) {
134 DEBUGP("ip6t_frag: unknown flags %X\n", fraginfo->invflags);
ccb79bdc 135 return false;
f0daaa65 136 }
ccb79bdc 137 return true;
1da177e4
LT
138}
139
6709dbbb 140static struct xt_match frag_match = {
1da177e4 141 .name = "frag",
6709dbbb 142 .family = AF_INET6,
7f939713
PM
143 .match = match,
144 .matchsize = sizeof(struct ip6t_frag),
145 .checkentry = checkentry,
1da177e4
LT
146 .me = THIS_MODULE,
147};
148
65b4b4e8 149static int __init ip6t_frag_init(void)
1da177e4 150{
6709dbbb 151 return xt_register_match(&frag_match);
1da177e4
LT
152}
153
65b4b4e8 154static void __exit ip6t_frag_fini(void)
1da177e4 155{
6709dbbb 156 xt_unregister_match(&frag_match);
1da177e4
LT
157}
158
65b4b4e8
AM
159module_init(ip6t_frag_init);
160module_exit(ip6t_frag_fini);