netfilter: nf_nat: fix oops on netns removal
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / net / netfilter / nf_nat_proto_sctp.c
CommitLineData
9d908a69
PM
1/*
2 * Copyright (c) 2008 Patrick McHardy <kaber@trash.net>
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/types.h>
10#include <linux/init.h>
9d908a69 11#include <linux/sctp.h>
3a9a231d 12#include <linux/module.h>
9d908a69
PM
13#include <net/sctp/checksum.h>
14
c7232c99 15#include <net/netfilter/nf_nat_l4proto.h>
9d908a69
PM
16
17static u_int16_t nf_sctp_port_rover;
18
f43dc98b 19static void
c7232c99
PM
20sctp_unique_tuple(const struct nf_nat_l3proto *l3proto,
21 struct nf_conntrack_tuple *tuple,
22 const struct nf_nat_range *range,
9d908a69
PM
23 enum nf_nat_manip_type maniptype,
24 const struct nf_conn *ct)
25{
c7232c99
PM
26 nf_nat_l4proto_unique_tuple(l3proto, tuple, range, maniptype, ct,
27 &nf_sctp_port_rover);
9d908a69
PM
28}
29
f2ea825f 30static bool
9d908a69 31sctp_manip_pkt(struct sk_buff *skb,
c7232c99
PM
32 const struct nf_nat_l3proto *l3proto,
33 unsigned int iphdroff, unsigned int hdroff,
9d908a69
PM
34 const struct nf_conntrack_tuple *tuple,
35 enum nf_nat_manip_type maniptype)
36{
343a9972 37 struct sk_buff *frag;
9d908a69 38 sctp_sctphdr_t *hdr;
eee1d5a1 39 __u32 crc32;
9d908a69
PM
40
41 if (!skb_make_writable(skb, hdroff + sizeof(*hdr)))
f2ea825f 42 return false;
9d908a69 43
9d908a69
PM
44 hdr = (struct sctphdr *)(skb->data + hdroff);
45
cbc9f2f4 46 if (maniptype == NF_NAT_MANIP_SRC) {
c7232c99 47 /* Get rid of src port */
9d908a69
PM
48 hdr->source = tuple->src.u.sctp.port;
49 } else {
c7232c99 50 /* Get rid of dst port */
9d908a69
PM
51 hdr->dest = tuple->dst.u.sctp.port;
52 }
53
54 crc32 = sctp_start_cksum((u8 *)hdr, skb_headlen(skb) - hdroff);
0808dc80 55 skb_walk_frags(skb, frag)
343a9972 56 crc32 = sctp_update_cksum((u8 *)frag->data, skb_headlen(frag),
9d908a69 57 crc32);
eee1d5a1 58 hdr->checksum = sctp_end_cksum(crc32);
9d908a69 59
f2ea825f 60 return true;
9d908a69
PM
61}
62
c7232c99
PM
63static const struct nf_nat_l4proto nf_nat_l4proto_sctp = {
64 .l4proto = IPPROTO_SCTP,
9d908a69 65 .manip_pkt = sctp_manip_pkt,
c7232c99 66 .in_range = nf_nat_l4proto_in_range,
9d908a69
PM
67 .unique_tuple = sctp_unique_tuple,
68#if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
c7232c99 69 .nlattr_to_range = nf_nat_l4proto_nlattr_to_range,
9d908a69
PM
70#endif
71};
72
73static int __init nf_nat_proto_sctp_init(void)
74{
c7232c99
PM
75 int err;
76
77 err = nf_nat_l4proto_register(NFPROTO_IPV4, &nf_nat_l4proto_sctp);
78 if (err < 0)
79 goto err1;
80 err = nf_nat_l4proto_register(NFPROTO_IPV6, &nf_nat_l4proto_sctp);
81 if (err < 0)
82 goto err2;
83 return 0;
84
85err2:
86 nf_nat_l4proto_unregister(NFPROTO_IPV4, &nf_nat_l4proto_sctp);
87err1:
88 return err;
9d908a69
PM
89}
90
91static void __exit nf_nat_proto_sctp_exit(void)
92{
c7232c99
PM
93 nf_nat_l4proto_unregister(NFPROTO_IPV6, &nf_nat_l4proto_sctp);
94 nf_nat_l4proto_unregister(NFPROTO_IPV4, &nf_nat_l4proto_sctp);
9d908a69
PM
95}
96
97module_init(nf_nat_proto_sctp_init);
98module_exit(nf_nat_proto_sctp_exit);
99
100MODULE_LICENSE("GPL");
101MODULE_DESCRIPTION("SCTP NAT protocol helper");
102MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");