summaryrefslogtreecommitdiffstats
path: root/lib/netfilter/nfnl.c
blob: ddce4b989d2fe9b8a3bf49903fa041fa6b72fa72 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/*
 * lib/netfilter/nfnl.c		Netfilter Netlink
 *
 *	This library is free software; you can redistribute it and/or
 *	modify it under the terms of the GNU Lesser General Public
 *	License as published by the Free Software Foundation version 2.1
 *	of the License.
 *
 * Copyright (c) 2003-2008 Thomas Graf <tgraf@suug.ch>
 * Copyright (c) 2007 Philip Craig <philipc@snapgear.com>
 * Copyright (c) 2007 Secure Computing Corporation
 */

/**
 * @defgroup nfnl Netfilter Netlink
 *
 * @par Message Format
 * @code
 *  <------- NLMSG_ALIGN(hlen) ------> <---- NLMSG_ALIGN(len) --->
 * +----------------------------+- - -+- - - - - - - - - - -+- - -+
 * |           Header           | Pad |       Payload       | Pad |
 * |      struct nlmsghdr       |     |                     |     |
 * +----------------------------+- - -+- - - - - - - - - - -+- - -+
 * @endcode
 * @code
 *  <-------- NFNL_HDRLEN --------->
 * +--------------------------+- - -+------------+
 * | Netfilter Netlink Header | Pad | Attributes |
 * |    struct nfgenmsg       |     |            |
 * +--------------------------+- - -+------------+
 * nfnlmsg_attrdata(nfg, hdrlen)-----^
 * @endcode
 *
 * @par 1) Creating a new netfilter netlink message
 * @code
 * struct nl_msg *msg;
 *
 * // Create a new empty netlink message
 * msg = nlmsg_alloc();
 *
 * // Append the netlink and netfilter netlink message header
 * hdr = nfnlmsg_put(msg, PID, SEQ, SUBSYS, TYPE, NLM_F_ECHO,
 *                   FAMILY, RES_ID);
 *
 * // Append the attributes.
 * nla_put_u32(msg, 1, 0x10);
 *
 * // Message is ready to be sent.
 * nl_send_auto_complete(sk, msg);
 *
 * // All done? Free the message.
 * nlmsg_free(msg);
 * @endcode
 *
 * @par 2) Sending of trivial messages
 * @code
 * // For trivial messages not requiring any subsys specific header or
 * // attributes, nfnl_send_simple() may be used to send messages directly.
 * nfnl_send_simple(sk, SUBSYS, TYPE, 0, FAMILY, RES_ID);
 * @endcode
 * @{
 */

#include <netlink-local.h>
#include <netlink/netlink.h>
#include <netlink/netfilter/nfnl.h>

/**
 * @name Socket Creating
 * @{
 */

/**
 * Create and connect netfilter netlink socket.
 * @arg sk		Netlink socket.
 *
 * Creates a NETLINK_NETFILTER netlink socket, binds the socket and
 * issues a connection attempt.
 *
 * @see nl_connect()
 *
 * @return 0 on success or a negative error code.
 */
int nfnl_connect(struct nl_sock *sk)
{
	return nl_connect(sk, NETLINK_NETFILTER);
}

/** @} */

/**
 * @name Sending
 * @{
 */

/**
 * Send trivial netfilter netlink message
 * @arg sk		Netlink socket.
 * @arg subsys_id	nfnetlink subsystem
 * @arg type		nfnetlink message type
 * @arg flags		message flags
 * @arg family		nfnetlink address family
 * @arg res_id		nfnetlink resource id
 *
 * @return Newly allocated netlink message or NULL.
 */
int nfnl_send_simple(struct nl_sock *sk, uint8_t subsys_id, uint8_t type,
		     int flags, uint8_t family, uint16_t res_id)
{
	struct nfgenmsg hdr = {
		.nfgen_family = family,
		.version = NFNETLINK_V0,
		.res_id = htons(res_id),
	};

	return nl_send_simple(sk, NFNLMSG_TYPE(subsys_id, type), flags,
			      &hdr, sizeof(hdr));
}

/** @} */

/**
 * @name Message Parsing
 * @{
 */

/**
 * Get netfilter subsystem id from message
 * @arg nlh	netlink messsage header
 */
uint8_t nfnlmsg_subsys(struct nlmsghdr *nlh)
{
	return NFNL_SUBSYS_ID(nlh->nlmsg_type);
}

/**
 * Get netfilter message type from message
 * @arg nlh	netlink messsage header
 */
uint8_t nfnlmsg_subtype(struct nlmsghdr *nlh)
{
	return NFNL_MSG_TYPE(nlh->nlmsg_type);
}

/**
 * Get netfilter family from message
 * @arg nlh	netlink messsage header
 */
uint8_t nfnlmsg_family(struct nlmsghdr *nlh)
{
	struct nfgenmsg *nfg = nlmsg_data(nlh);

	return nfg->nfgen_family;
}

/**
 * Get netfilter resource id from message
 * @arg nlh	netlink messsage header
 */
uint16_t nfnlmsg_res_id(struct nlmsghdr *nlh)
{
	struct nfgenmsg *nfg = nlmsg_data(nlh);

	return ntohs(nfg->res_id);
}

/** @} */

/**
 * @name Message Building
 * @{
 */

static int nfnlmsg_append(struct nl_msg *msg, uint8_t family, uint16_t res_id)
{
	struct nfgenmsg *nfg;

	nfg = nlmsg_reserve(msg, sizeof(*nfg), NLMSG_ALIGNTO);
	if (nfg == NULL)
		return -NLE_NOMEM;

	nfg->nfgen_family = family;
	nfg->version = NFNETLINK_V0;
	nfg->res_id = htons(res_id);
	NL_DBG(2, "msg %p: Added nfnetlink header family=%d res_id=%d\n",
	       msg, family, res_id);
	return 0;
}

/**
 * Allocate a new netfilter netlink message
 * @arg subsys_id	nfnetlink subsystem
 * @arg type		nfnetlink message type
 * @arg flags		message flags
 * @arg family		nfnetlink address family
 * @arg res_id		nfnetlink resource id
 *
 * @return Newly allocated netlink message or NULL.
 */
struct nl_msg *nfnlmsg_alloc_simple(uint8_t subsys_id, uint8_t type, int flags,
				    uint8_t family, uint16_t res_id)
{
	struct nl_msg *msg;

	msg = nlmsg_alloc_simple(NFNLMSG_TYPE(subsys_id, type), flags);
	if (msg == NULL)
		return NULL;

	if (nfnlmsg_append(msg, family, res_id) < 0)
		goto nla_put_failure;

	return msg;

nla_put_failure:
	nlmsg_free(msg);
	return NULL;
}

/**
 * Add netlink and netfilter netlink headers to netlink message
 * @arg msg		netlink message
 * @arg pid		netlink process id
 * @arg seq		sequence number of message
 * @arg subsys_id	nfnetlink subsystem
 * @arg type		nfnetlink message type
 * @arg flags		message flags
 * @arg family		nfnetlink address family
 * @arg res_id		nfnetlink resource id
 */
int nfnlmsg_put(struct nl_msg *msg, uint32_t pid, uint32_t seq,
		uint8_t subsys_id, uint8_t type, int flags, uint8_t family,
		uint16_t res_id)
{
	struct nlmsghdr *nlh;

	nlh = nlmsg_put(msg, pid, seq, NFNLMSG_TYPE(subsys_id, type), 0, flags);
	if (nlh == NULL)
		return -NLE_MSGSIZE;

	return nfnlmsg_append(msg, family, res_id);
}

/** @} */

/** @} */