blob: 6179648a014ea1d77330a9b3121f7f9737386bd4 [file] [log] [blame]
Harald Weltef9e815b2005-08-09 19:30:24 -07001#ifndef _NFNETLINK_H
2#define _NFNETLINK_H
3#include <linux/types.h>
4
Patrick McHardyac6d4392005-08-14 19:29:52 -07005#ifndef __KERNEL__
6/* nfnetlink groups: Up to 32 maximum - backwards compatibility for userspace */
Harald Weltef9e815b2005-08-09 19:30:24 -07007#define NF_NETLINK_CONNTRACK_NEW 0x00000001
8#define NF_NETLINK_CONNTRACK_UPDATE 0x00000002
9#define NF_NETLINK_CONNTRACK_DESTROY 0x00000004
10#define NF_NETLINK_CONNTRACK_EXP_NEW 0x00000008
11#define NF_NETLINK_CONNTRACK_EXP_UPDATE 0x00000010
12#define NF_NETLINK_CONNTRACK_EXP_DESTROY 0x00000020
Patrick McHardyac6d4392005-08-14 19:29:52 -070013#endif
14
15enum nfnetlink_groups {
16 NFNLGRP_NONE,
17#define NFNLGRP_NONE NFNLGRP_NONE
18 NFNLGRP_CONNTRACK_NEW,
19#define NFNLGRP_CONNTRACK_NEW NFNLGRP_CONNTRACK_NEW
20 NFNLGRP_CONNTRACK_UPDATE,
21#define NFNLGRP_CONNTRACK_UPDATE NFNLGRP_CONNTRACK_UPDATE
22 NFNLGRP_CONNTRACK_DESTROY,
23#define NFNLGRP_CONNTRACK_DESTROY NFNLGRP_CONNTRACK_DESTROY
24 NFNLGRP_CONNTRACK_EXP_NEW,
25#define NFNLGRP_CONNTRACK_EXP_NEW NFNLGRP_CONNTRACK_EXP_NEW
26 NFNLGRP_CONNTRACK_EXP_UPDATE,
27#define NFNLGRP_CONNTRACK_EXP_UPDATE NFNLGRP_CONNTRACK_EXP_UPDATE
28 NFNLGRP_CONNTRACK_EXP_DESTROY,
29#define NFNLGRP_CONNTRACK_EXP_DESTROY NFNLGRP_CONNTRACK_EXP_DESTROY
30 __NFNLGRP_MAX,
31};
32#define NFNLGRP_MAX (__NFNLGRP_MAX - 1)
Harald Weltef9e815b2005-08-09 19:30:24 -070033
34/* Generic structure for encapsulation optional netfilter information.
35 * It is reminiscent of sockaddr, but with sa_family replaced
36 * with attribute type.
37 * ! This should someday be put somewhere generic as now rtnetlink and
38 * ! nfnetlink use the same attributes methods. - J. Schulist.
39 */
40
41struct nfattr
42{
43 u_int16_t nfa_len;
Harald Welteebe0bbf2005-10-10 20:52:19 -070044 u_int16_t nfa_type; /* we use 15 bits for the type, and the highest
45 * bit to indicate whether the payload is nested */
Patrick McHardy53e26652006-08-22 00:43:20 -070046};
Harald Weltef9e815b2005-08-09 19:30:24 -070047
Harald Welteebe0bbf2005-10-10 20:52:19 -070048/* FIXME: Apart from NFNL_NFA_NESTED shamelessly copy and pasted from
49 * rtnetlink.h, it's time to put this in a generic file */
50
51#define NFNL_NFA_NEST 0x8000
52#define NFA_TYPE(attr) ((attr)->nfa_type & 0x7fff)
Harald Weltef9e815b2005-08-09 19:30:24 -070053
54#define NFA_ALIGNTO 4
55#define NFA_ALIGN(len) (((len) + NFA_ALIGNTO - 1) & ~(NFA_ALIGNTO - 1))
56#define NFA_OK(nfa,len) ((len) > 0 && (nfa)->nfa_len >= sizeof(struct nfattr) \
57 && (nfa)->nfa_len <= (len))
58#define NFA_NEXT(nfa,attrlen) ((attrlen) -= NFA_ALIGN((nfa)->nfa_len), \
59 (struct nfattr *)(((char *)(nfa)) + NFA_ALIGN((nfa)->nfa_len)))
60#define NFA_LENGTH(len) (NFA_ALIGN(sizeof(struct nfattr)) + (len))
61#define NFA_SPACE(len) NFA_ALIGN(NFA_LENGTH(len))
62#define NFA_DATA(nfa) ((void *)(((char *)(nfa)) + NFA_LENGTH(0)))
63#define NFA_PAYLOAD(nfa) ((int)((nfa)->nfa_len) - NFA_LENGTH(0))
64#define NFA_NEST(skb, type) \
65({ struct nfattr *__start = (struct nfattr *) (skb)->tail; \
Harald Welteebe0bbf2005-10-10 20:52:19 -070066 NFA_PUT(skb, (NFNL_NFA_NEST | type), 0, NULL); \
Harald Weltef9e815b2005-08-09 19:30:24 -070067 __start; })
68#define NFA_NEST_END(skb, start) \
69({ (start)->nfa_len = ((skb)->tail - (unsigned char *) (start)); \
70 (skb)->len; })
71#define NFA_NEST_CANCEL(skb, start) \
72({ if (start) \
73 skb_trim(skb, (unsigned char *) (start) - (skb)->data); \
74 -1; })
75
76/* General form of address family dependent message.
77 */
78struct nfgenmsg {
79 u_int8_t nfgen_family; /* AF_xxx */
80 u_int8_t version; /* nfnetlink version */
Al Viro98a4a862006-11-08 00:26:51 -080081 __be16 res_id; /* resource id */
Patrick McHardy53e26652006-08-22 00:43:20 -070082};
Harald Weltef9e815b2005-08-09 19:30:24 -070083
Harald Welte080774a2005-08-09 19:32:58 -070084#define NFNETLINK_V0 0
Harald Weltef9e815b2005-08-09 19:30:24 -070085
86#define NFM_NFA(n) ((struct nfattr *)(((char *)(n)) \
87 + NLMSG_ALIGN(sizeof(struct nfgenmsg))))
88#define NFM_PAYLOAD(n) NLMSG_PAYLOAD(n, sizeof(struct nfgenmsg))
89
90/* netfilter netlink message types are split in two pieces:
91 * 8 bit subsystem, 8bit operation.
92 */
93
94#define NFNL_SUBSYS_ID(x) ((x & 0xff00) >> 8)
95#define NFNL_MSG_TYPE(x) (x & 0x00ff)
96
Harald Welte0ab43f82005-08-09 19:43:44 -070097/* No enum here, otherwise __stringify() trick of MODULE_ALIAS_NFNL_SUBSYS()
98 * won't work anymore */
99#define NFNL_SUBSYS_NONE 0
100#define NFNL_SUBSYS_CTNETLINK 1
101#define NFNL_SUBSYS_CTNETLINK_EXP 2
102#define NFNL_SUBSYS_QUEUE 3
103#define NFNL_SUBSYS_ULOG 4
104#define NFNL_SUBSYS_COUNT 5
Harald Weltef9e815b2005-08-09 19:30:24 -0700105
106#ifdef __KERNEL__
107
Harald Welte080774a2005-08-09 19:32:58 -0700108#include <linux/netlink.h>
Harald Weltef9e815b2005-08-09 19:30:24 -0700109#include <linux/capability.h>
110
111struct nfnl_callback
112{
Harald Weltef9e815b2005-08-09 19:30:24 -0700113 int (*call)(struct sock *nl, struct sk_buff *skb,
114 struct nlmsghdr *nlh, struct nfattr *cda[], int *errp);
Harald Welte927ccbc2005-08-09 20:03:40 -0700115 u_int16_t attr_count; /* number of nfattr's */
Harald Weltef9e815b2005-08-09 19:30:24 -0700116};
117
118struct nfnetlink_subsystem
119{
120 const char *name;
121 __u8 subsys_id; /* nfnetlink subsystem ID */
122 __u8 cb_count; /* number of callbacks */
Harald Weltef9e815b2005-08-09 19:30:24 -0700123 struct nfnl_callback *cb; /* callback for individual types */
124};
125
126extern void __nfa_fill(struct sk_buff *skb, int attrtype,
127 int attrlen, const void *data);
128#define NFA_PUT(skb, attrtype, attrlen, data) \
129({ if (skb_tailroom(skb) < (int)NFA_SPACE(attrlen)) goto nfattr_failure; \
130 __nfa_fill(skb, attrtype, attrlen, data); })
131
Harald Weltef9e815b2005-08-09 19:30:24 -0700132extern int nfnetlink_subsys_register(struct nfnetlink_subsystem *n);
133extern int nfnetlink_subsys_unregister(struct nfnetlink_subsystem *n);
134
Harald Weltea2506c02005-11-09 12:59:13 -0800135extern void nfattr_parse(struct nfattr *tb[], int maxattr,
Harald Weltef9e815b2005-08-09 19:30:24 -0700136 struct nfattr *nfa, int len);
137
138#define nfattr_parse_nested(tb, max, nfa) \
139 nfattr_parse((tb), (max), NFA_DATA((nfa)), NFA_PAYLOAD((nfa)))
140
141#define nfattr_bad_size(tb, max, cta_min) \
142({ int __i, __res = 0; \
Pablo Neira Ayusoc0400c42005-11-14 15:21:41 -0800143 for (__i=0; __i<max; __i++) { \
144 if (!cta_min[__i]) \
145 continue; \
Harald Weltef9e815b2005-08-09 19:30:24 -0700146 if (tb[__i] && NFA_PAYLOAD(tb[__i]) < cta_min[__i]){ \
147 __res = 1; \
148 break; \
149 } \
Pablo Neira Ayusoc0400c42005-11-14 15:21:41 -0800150 } \
Harald Weltef9e815b2005-08-09 19:30:24 -0700151 __res; \
152})
153
Patrick McHardya2427692006-03-20 18:03:59 -0800154extern int nfnetlink_has_listeners(unsigned int group);
Harald Weltef9e815b2005-08-09 19:30:24 -0700155extern int nfnetlink_send(struct sk_buff *skb, u32 pid, unsigned group,
156 int echo);
157extern int nfnetlink_unicast(struct sk_buff *skb, u_int32_t pid, int flags);
158
Harald Welte0ab43f82005-08-09 19:43:44 -0700159#define MODULE_ALIAS_NFNL_SUBSYS(subsys) \
160 MODULE_ALIAS("nfnetlink-subsys-" __stringify(subsys))
161
Harald Weltef9e815b2005-08-09 19:30:24 -0700162#endif /* __KERNEL__ */
163#endif /* _NFNETLINK_H */