2 * Copyright (c) 2011, 2012 Patrick McHardy <kaber@trash.net>
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.
9 #include <linux/module.h>
10 #include <linux/skbuff.h>
11 #include <linux/ipv6.h>
12 #include <linux/netfilter.h>
13 #include <linux/netfilter_ipv6.h>
14 #include <linux/netfilter_ipv6/ip6t_NPT.h>
15 #include <linux/netfilter/x_tables.h>
17 static int ip6t_npt_checkentry(const struct xt_tgchk_param
*par
)
19 struct ip6t_npt_tginfo
*npt
= par
->targinfo
;
20 __wsum src_sum
= 0, dst_sum
= 0;
23 if (npt
->src_pfx_len
> 64 || npt
->dst_pfx_len
> 64)
26 for (i
= 0; i
< ARRAY_SIZE(npt
->src_pfx
.in6
.s6_addr16
); i
++) {
27 src_sum
= csum_add(src_sum
,
28 (__force __wsum
)npt
->src_pfx
.in6
.s6_addr16
[i
]);
29 dst_sum
= csum_add(dst_sum
,
30 (__force __wsum
)npt
->dst_pfx
.in6
.s6_addr16
[i
]);
33 npt
->adjustment
= (__force __sum16
) csum_sub(src_sum
, dst_sum
);
37 static bool ip6t_npt_map_pfx(const struct ip6t_npt_tginfo
*npt
,
38 struct in6_addr
*addr
)
45 pfx_len
= max(npt
->src_pfx_len
, npt
->dst_pfx_len
);
46 for (i
= 0; i
< pfx_len
; i
+= 32) {
47 if (pfx_len
- i
>= 32)
50 mask
= htonl(~((1 << (pfx_len
- i
)) - 1));
53 addr
->s6_addr32
[idx
] &= mask
;
54 addr
->s6_addr32
[idx
] |= npt
->dst_pfx
.in6
.s6_addr32
[idx
];
60 for (idx
= 4; idx
< ARRAY_SIZE(addr
->s6_addr16
); idx
++) {
61 if ((__force __sum16
)addr
->s6_addr16
[idx
] !=
65 if (idx
== ARRAY_SIZE(addr
->s6_addr16
))
69 sum
= (__force __sum16
) csum_add((__force __wsum
)addr
->s6_addr16
[idx
],
71 if (sum
== CSUM_MANGLED_0
)
73 *(__force __sum16
*)&addr
->s6_addr16
[idx
] = sum
;
79 ip6t_snpt_tg(struct sk_buff
*skb
, const struct xt_action_param
*par
)
81 const struct ip6t_npt_tginfo
*npt
= par
->targinfo
;
83 if (!ip6t_npt_map_pfx(npt
, &ipv6_hdr(skb
)->saddr
)) {
84 icmpv6_send(skb
, ICMPV6_PARAMPROB
, ICMPV6_HDR_FIELD
,
85 offsetof(struct ipv6hdr
, saddr
));
92 ip6t_dnpt_tg(struct sk_buff
*skb
, const struct xt_action_param
*par
)
94 const struct ip6t_npt_tginfo
*npt
= par
->targinfo
;
96 if (!ip6t_npt_map_pfx(npt
, &ipv6_hdr(skb
)->daddr
)) {
97 icmpv6_send(skb
, ICMPV6_PARAMPROB
, ICMPV6_HDR_FIELD
,
98 offsetof(struct ipv6hdr
, daddr
));
104 static struct xt_target ip6t_npt_target_reg
[] __read_mostly
= {
107 .target
= ip6t_snpt_tg
,
108 .targetsize
= sizeof(struct ip6t_npt_tginfo
),
109 .checkentry
= ip6t_npt_checkentry
,
110 .family
= NFPROTO_IPV6
,
111 .hooks
= (1 << NF_INET_LOCAL_IN
) |
112 (1 << NF_INET_POST_ROUTING
),
117 .target
= ip6t_dnpt_tg
,
118 .targetsize
= sizeof(struct ip6t_npt_tginfo
),
119 .checkentry
= ip6t_npt_checkentry
,
120 .family
= NFPROTO_IPV6
,
121 .hooks
= (1 << NF_INET_PRE_ROUTING
) |
122 (1 << NF_INET_LOCAL_OUT
),
127 static int __init
ip6t_npt_init(void)
129 return xt_register_targets(ip6t_npt_target_reg
,
130 ARRAY_SIZE(ip6t_npt_target_reg
));
133 static void __exit
ip6t_npt_exit(void)
135 xt_unregister_targets(ip6t_npt_target_reg
,
136 ARRAY_SIZE(ip6t_npt_target_reg
));
139 module_init(ip6t_npt_init
);
140 module_exit(ip6t_npt_exit
);
142 MODULE_LICENSE("GPL");
143 MODULE_DESCRIPTION("IPv6-to-IPv6 Network Prefix Translation (RFC 6296)");
144 MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
145 MODULE_ALIAS("ip6t_SNPT");
146 MODULE_ALIAS("ip6t_DNPT");