eed04af9b75e56b6c33d0887cdefa4c8f827251e
[linux-2.6-block.git] / include / net / netfilter / nf_conntrack_l4proto.h
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * Header for use in defining a given L4 protocol for connection tracking.
4  *
5  * 16 Dec 2003: Yasuyuki Kozakai @USAGI <yasuyuki.kozakai@toshiba.co.jp>
6  *      - generalized L3 protocol dependent part.
7  *
8  * Derived from include/linux/netfiter_ipv4/ip_conntrack_protcol.h
9  */
10
11 #ifndef _NF_CONNTRACK_L4PROTO_H
12 #define _NF_CONNTRACK_L4PROTO_H
13 #include <linux/netlink.h>
14 #include <net/netlink.h>
15 #include <net/netfilter/nf_conntrack.h>
16 #include <net/netns/generic.h>
17
18 struct seq_file;
19
20 struct nf_conntrack_l4proto {
21         /* L4 Protocol number. */
22         u_int8_t l4proto;
23
24         /* Resolve clashes on insertion races. */
25         bool allow_clash;
26
27         /* protoinfo nlattr size, closes a hole */
28         u16 nlattr_size;
29
30         /* Try to fill in the third arg: dataoff is offset past network protocol
31            hdr.  Return true if possible. */
32         bool (*pkt_to_tuple)(const struct sk_buff *skb, unsigned int dataoff,
33                              struct net *net, struct nf_conntrack_tuple *tuple);
34
35         /* Invert the per-proto part of the tuple: ie. turn xmit into reply.
36          * Only used by icmp, most protocols use a generic version.
37          */
38         bool (*invert_tuple)(struct nf_conntrack_tuple *inverse,
39                              const struct nf_conntrack_tuple *orig);
40
41         /* Returns verdict for packet, or -1 for invalid. */
42         int (*packet)(struct nf_conn *ct,
43                       struct sk_buff *skb,
44                       unsigned int dataoff,
45                       enum ip_conntrack_info ctinfo,
46                       const struct nf_hook_state *state);
47
48         /* Called when a conntrack entry is destroyed */
49         void (*destroy)(struct nf_conn *ct);
50
51         /* called by gc worker if table is full */
52         bool (*can_early_drop)(const struct nf_conn *ct);
53
54         /* convert protoinfo to nfnetink attributes */
55         int (*to_nlattr)(struct sk_buff *skb, struct nlattr *nla,
56                          struct nf_conn *ct);
57
58         /* convert nfnetlink attributes to protoinfo */
59         int (*from_nlattr)(struct nlattr *tb[], struct nf_conn *ct);
60
61         int (*tuple_to_nlattr)(struct sk_buff *skb,
62                                const struct nf_conntrack_tuple *t);
63         /* Calculate tuple nlattr size */
64         unsigned int (*nlattr_tuple_size)(void);
65         int (*nlattr_to_tuple)(struct nlattr *tb[],
66                                struct nf_conntrack_tuple *t);
67         const struct nla_policy *nla_policy;
68
69         struct {
70                 int (*nlattr_to_obj)(struct nlattr *tb[],
71                                      struct net *net, void *data);
72                 int (*obj_to_nlattr)(struct sk_buff *skb, const void *data);
73
74                 u16 obj_size;
75                 u16 nlattr_max;
76                 const struct nla_policy *nla_policy;
77         } ctnl_timeout;
78 #ifdef CONFIG_NF_CONNTRACK_PROCFS
79         /* Print out the private part of the conntrack. */
80         void (*print_conntrack)(struct seq_file *s, struct nf_conn *);
81 #endif
82         unsigned int    *net_id;
83         /* Init l4proto pernet data */
84         int (*init_net)(struct net *net);
85
86         /* Return the per-net protocol part. */
87         struct nf_proto_net *(*get_net_proto)(struct net *net);
88
89         /* Module (if any) which this is connected to. */
90         struct module *me;
91 };
92
93 int nf_conntrack_icmpv4_error(struct nf_conn *tmpl,
94                               struct sk_buff *skb,
95                               unsigned int dataoff,
96                               const struct nf_hook_state *state);
97
98 int nf_conntrack_icmpv6_error(struct nf_conn *tmpl,
99                               struct sk_buff *skb,
100                               unsigned int dataoff,
101                               const struct nf_hook_state *state);
102 /* Existing built-in generic protocol */
103 extern const struct nf_conntrack_l4proto nf_conntrack_l4proto_generic;
104
105 #define MAX_NF_CT_PROTO IPPROTO_UDPLITE
106
107 const struct nf_conntrack_l4proto *__nf_ct_l4proto_find(u8 l4proto);
108
109 const struct nf_conntrack_l4proto *nf_ct_l4proto_find_get(u8 l4proto);
110 void nf_ct_l4proto_put(const struct nf_conntrack_l4proto *p);
111
112 /* Protocol pernet registration. */
113 int nf_ct_l4proto_pernet_register_one(struct net *net,
114                                 const struct nf_conntrack_l4proto *proto);
115 void nf_ct_l4proto_pernet_unregister_one(struct net *net,
116                                 const struct nf_conntrack_l4proto *proto);
117 int nf_ct_l4proto_pernet_register(struct net *net,
118                                   const struct nf_conntrack_l4proto *const proto[],
119                                   unsigned int num_proto);
120 void nf_ct_l4proto_pernet_unregister(struct net *net,
121                                 const struct nf_conntrack_l4proto *const proto[],
122                                 unsigned int num_proto);
123
124 /* Protocol global registration. */
125 int nf_ct_l4proto_register_one(const struct nf_conntrack_l4proto *proto);
126 void nf_ct_l4proto_unregister_one(const struct nf_conntrack_l4proto *proto);
127
128 /* Generic netlink helpers */
129 int nf_ct_port_tuple_to_nlattr(struct sk_buff *skb,
130                                const struct nf_conntrack_tuple *tuple);
131 int nf_ct_port_nlattr_to_tuple(struct nlattr *tb[],
132                                struct nf_conntrack_tuple *t);
133 unsigned int nf_ct_port_nlattr_tuple_size(void);
134 extern const struct nla_policy nf_ct_port_nla_policy[];
135
136 #ifdef CONFIG_SYSCTL
137 __printf(3, 4) __cold
138 void nf_ct_l4proto_log_invalid(const struct sk_buff *skb,
139                                const struct nf_conn *ct,
140                                const char *fmt, ...);
141 __printf(5, 6) __cold
142 void nf_l4proto_log_invalid(const struct sk_buff *skb,
143                             struct net *net,
144                             u16 pf, u8 protonum,
145                             const char *fmt, ...);
146 #else
147 static inline __printf(5, 6) __cold
148 void nf_l4proto_log_invalid(const struct sk_buff *skb, struct net *net,
149                             u16 pf, u8 protonum, const char *fmt, ...) {}
150 static inline __printf(3, 4) __cold
151 void nf_ct_l4proto_log_invalid(const struct sk_buff *skb,
152                                const struct nf_conn *ct,
153                                const char *fmt, ...) { }
154 #endif /* CONFIG_SYSCTL */
155
156 #endif /*_NF_CONNTRACK_PROTOCOL_H*/