Commit | Line | Data |
---|---|---|
f9e815b3 HW |
1 | /* Netfilter messages via netlink socket. Allows for user space |
2 | * protocol helpers and general trouble making from userspace. | |
3 | * | |
4 | * (C) 2001 by Jay Schulist <jschlst@samba.org>, | |
5 | * (C) 2002-2005 by Harald Welte <laforge@gnumonks.org> | |
67ca3966 | 6 | * (C) 2005,2007 by Pablo Neira Ayuso <pablo@netfilter.org> |
f9e815b3 HW |
7 | * |
8 | * Initial netfilter messages via netlink development funded and | |
9 | * generally made possible by Network Robots, Inc. (www.networkrobots.com) | |
10 | * | |
11 | * Further development of this code funded by Astaro AG (http://www.astaro.com) | |
12 | * | |
13 | * This software may be used and distributed according to the terms | |
14 | * of the GNU General Public License, incorporated herein by reference. | |
15 | */ | |
16 | ||
f9e815b3 HW |
17 | #include <linux/module.h> |
18 | #include <linux/types.h> | |
19 | #include <linux/socket.h> | |
20 | #include <linux/kernel.h> | |
21 | #include <linux/major.h> | |
f9e815b3 HW |
22 | #include <linux/timer.h> |
23 | #include <linux/string.h> | |
24 | #include <linux/sockios.h> | |
25 | #include <linux/net.h> | |
26 | #include <linux/fcntl.h> | |
27 | #include <linux/skbuff.h> | |
28 | #include <asm/uaccess.h> | |
29 | #include <asm/system.h> | |
30 | #include <net/sock.h> | |
a3c5029c | 31 | #include <net/netlink.h> |
f9e815b3 HW |
32 | #include <linux/init.h> |
33 | #include <linux/spinlock.h> | |
34 | ||
35 | #include <linux/netfilter.h> | |
36 | #include <linux/netlink.h> | |
37 | #include <linux/netfilter/nfnetlink.h> | |
38 | ||
39 | MODULE_LICENSE("GPL"); | |
4fdb3bb7 HW |
40 | MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>"); |
41 | MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_NETFILTER); | |
f9e815b3 HW |
42 | |
43 | static char __initdata nfversion[] = "0.30"; | |
44 | ||
f9e815b3 HW |
45 | static struct sock *nfnl = NULL; |
46 | static struct nfnetlink_subsystem *subsys_table[NFNL_SUBSYS_COUNT]; | |
a3c5029c | 47 | static DEFINE_MUTEX(nfnl_mutex); |
f9e815b3 | 48 | |
a3c5029c | 49 | static void nfnl_lock(void) |
f9e815b3 | 50 | { |
a3c5029c | 51 | mutex_lock(&nfnl_mutex); |
f9e815b3 HW |
52 | } |
53 | ||
a3c5029c | 54 | static int nfnl_trylock(void) |
f9e815b3 | 55 | { |
a3c5029c PM |
56 | return !mutex_trylock(&nfnl_mutex); |
57 | } | |
58 | ||
59 | static void __nfnl_unlock(void) | |
60 | { | |
61 | mutex_unlock(&nfnl_mutex); | |
62 | } | |
63 | ||
64 | static void nfnl_unlock(void) | |
65 | { | |
66 | mutex_unlock(&nfnl_mutex); | |
67 | if (nfnl->sk_receive_queue.qlen) | |
68 | nfnl->sk_data_ready(nfnl, 0); | |
f9e815b3 HW |
69 | } |
70 | ||
71 | int nfnetlink_subsys_register(struct nfnetlink_subsystem *n) | |
72 | { | |
f9e815b3 | 73 | nfnl_lock(); |
0ab43f84 HW |
74 | if (subsys_table[n->subsys_id]) { |
75 | nfnl_unlock(); | |
76 | return -EBUSY; | |
77 | } | |
f9e815b3 HW |
78 | subsys_table[n->subsys_id] = n; |
79 | nfnl_unlock(); | |
80 | ||
81 | return 0; | |
82 | } | |
83 | ||
84 | int nfnetlink_subsys_unregister(struct nfnetlink_subsystem *n) | |
85 | { | |
f9e815b3 HW |
86 | nfnl_lock(); |
87 | subsys_table[n->subsys_id] = NULL; | |
88 | nfnl_unlock(); | |
89 | ||
90 | return 0; | |
91 | } | |
92 | ||
93 | static inline struct nfnetlink_subsystem *nfnetlink_get_subsys(u_int16_t type) | |
94 | { | |
95 | u_int8_t subsys_id = NFNL_SUBSYS_ID(type); | |
96 | ||
97 | if (subsys_id >= NFNL_SUBSYS_COUNT | |
98 | || subsys_table[subsys_id] == NULL) | |
99 | return NULL; | |
100 | ||
101 | return subsys_table[subsys_id]; | |
102 | } | |
103 | ||
104 | static inline struct nfnl_callback * | |
105 | nfnetlink_find_client(u_int16_t type, struct nfnetlink_subsystem *ss) | |
106 | { | |
107 | u_int8_t cb_id = NFNL_MSG_TYPE(type); | |
601e68e1 | 108 | |
67ca3966 | 109 | if (cb_id >= ss->cb_count) |
f9e815b3 | 110 | return NULL; |
f9e815b3 HW |
111 | |
112 | return &ss->cb[cb_id]; | |
113 | } | |
114 | ||
115 | void __nfa_fill(struct sk_buff *skb, int attrtype, int attrlen, | |
116 | const void *data) | |
117 | { | |
118 | struct nfattr *nfa; | |
119 | int size = NFA_LENGTH(attrlen); | |
120 | ||
121 | nfa = (struct nfattr *)skb_put(skb, NFA_ALIGN(size)); | |
122 | nfa->nfa_type = attrtype; | |
123 | nfa->nfa_len = size; | |
124 | memcpy(NFA_DATA(nfa), data, attrlen); | |
080774a2 | 125 | memset(NFA_DATA(nfa) + attrlen, 0, NFA_ALIGN(size) - size); |
f9e815b3 HW |
126 | } |
127 | ||
a2506c04 | 128 | void nfattr_parse(struct nfattr *tb[], int maxattr, struct nfattr *nfa, int len) |
f9e815b3 HW |
129 | { |
130 | memset(tb, 0, sizeof(struct nfattr *) * maxattr); | |
131 | ||
132 | while (NFA_OK(nfa, len)) { | |
ebe0bbf0 | 133 | unsigned flavor = NFA_TYPE(nfa); |
f9e815b3 HW |
134 | if (flavor && flavor <= maxattr) |
135 | tb[flavor-1] = nfa; | |
136 | nfa = NFA_NEXT(nfa, len); | |
137 | } | |
f9e815b3 HW |
138 | } |
139 | ||
140 | /** | |
141 | * nfnetlink_check_attributes - check and parse nfnetlink attributes | |
142 | * | |
143 | * subsys: nfnl subsystem for which this message is to be parsed | |
144 | * nlmsghdr: netlink message to be checked/parsed | |
145 | * cda: array of pointers, needs to be at least subsys->attr_count big | |
146 | * | |
147 | */ | |
148 | static int | |
149 | nfnetlink_check_attributes(struct nfnetlink_subsystem *subsys, | |
150 | struct nlmsghdr *nlh, struct nfattr *cda[]) | |
151 | { | |
152 | int min_len; | |
927ccbcc HW |
153 | u_int16_t attr_count; |
154 | u_int8_t cb_id = NFNL_MSG_TYPE(nlh->nlmsg_type); | |
f9e815b3 | 155 | |
67ca3966 | 156 | if (unlikely(cb_id >= subsys->cb_count)) |
927ccbcc | 157 | return -EINVAL; |
f9e815b3 | 158 | |
3ebbe0cd | 159 | min_len = NLMSG_SPACE(sizeof(struct nfgenmsg)); |
a42827b7 | 160 | if (unlikely(nlh->nlmsg_len < min_len)) |
f9e815b3 HW |
161 | return -EINVAL; |
162 | ||
a42827b7 HW |
163 | attr_count = subsys->cb[cb_id].attr_count; |
164 | memset(cda, 0, sizeof(struct nfattr *) * attr_count); | |
165 | ||
166 | /* check attribute lengths. */ | |
167 | if (likely(nlh->nlmsg_len > min_len)) { | |
f9e815b3 HW |
168 | struct nfattr *attr = NFM_NFA(NLMSG_DATA(nlh)); |
169 | int attrlen = nlh->nlmsg_len - NLMSG_ALIGN(min_len); | |
170 | ||
171 | while (NFA_OK(attr, attrlen)) { | |
ebe0bbf0 | 172 | unsigned flavor = NFA_TYPE(attr); |
f9e815b3 | 173 | if (flavor) { |
927ccbcc | 174 | if (flavor > attr_count) |
f9e815b3 HW |
175 | return -EINVAL; |
176 | cda[flavor - 1] = attr; | |
177 | } | |
178 | attr = NFA_NEXT(attr, attrlen); | |
179 | } | |
a42827b7 HW |
180 | } |
181 | ||
182 | /* implicit: if nlmsg_len == min_len, we return 0, and an empty | |
183 | * (zeroed) cda[] array. The message is valid, but empty. */ | |
f9e815b3 | 184 | |
601e68e1 | 185 | return 0; |
f9e815b3 HW |
186 | } |
187 | ||
a2427692 PM |
188 | int nfnetlink_has_listeners(unsigned int group) |
189 | { | |
190 | return netlink_has_listeners(nfnl, group); | |
191 | } | |
192 | EXPORT_SYMBOL_GPL(nfnetlink_has_listeners); | |
193 | ||
f9e815b3 HW |
194 | int nfnetlink_send(struct sk_buff *skb, u32 pid, unsigned group, int echo) |
195 | { | |
f9e815b3 HW |
196 | int err = 0; |
197 | ||
ac6d439d | 198 | NETLINK_CB(skb).dst_group = group; |
f9e815b3 HW |
199 | if (echo) |
200 | atomic_inc(&skb->users); | |
4498121c | 201 | netlink_broadcast(nfnl, skb, pid, group, gfp_any()); |
f9e815b3 HW |
202 | if (echo) |
203 | err = netlink_unicast(nfnl, skb, pid, MSG_DONTWAIT); | |
204 | ||
205 | return err; | |
206 | } | |
207 | ||
208 | int nfnetlink_unicast(struct sk_buff *skb, u_int32_t pid, int flags) | |
209 | { | |
210 | return netlink_unicast(nfnl, skb, pid, flags); | |
211 | } | |
212 | ||
213 | /* Process one complete nfnetlink message. */ | |
858119e1 | 214 | static int nfnetlink_rcv_msg(struct sk_buff *skb, |
f9e815b3 HW |
215 | struct nlmsghdr *nlh, int *errp) |
216 | { | |
217 | struct nfnl_callback *nc; | |
218 | struct nfnetlink_subsystem *ss; | |
219 | int type, err = 0; | |
220 | ||
c7bdb545 | 221 | if (security_netlink_recv(skb, CAP_NET_ADMIN)) { |
37d2e7a2 HW |
222 | *errp = -EPERM; |
223 | return -1; | |
224 | } | |
225 | ||
f9e815b3 | 226 | /* Only requests are handled by kernel now. */ |
67ca3966 | 227 | if (!(nlh->nlmsg_flags & NLM_F_REQUEST)) |
f9e815b3 | 228 | return 0; |
f9e815b3 HW |
229 | |
230 | /* All the messages must at least contain nfgenmsg */ | |
67ca3966 | 231 | if (nlh->nlmsg_len < NLMSG_SPACE(sizeof(struct nfgenmsg))) |
f9e815b3 | 232 | return 0; |
f9e815b3 HW |
233 | |
234 | type = nlh->nlmsg_type; | |
235 | ss = nfnetlink_get_subsys(type); | |
0ab43f84 HW |
236 | if (!ss) { |
237 | #ifdef CONFIG_KMOD | |
a3c5029c | 238 | /* don't call nfnl_unlock, since it would reenter |
37d2e7a2 | 239 | * with further packet processing */ |
a3c5029c | 240 | __nfnl_unlock(); |
37d2e7a2 | 241 | request_module("nfnetlink-subsys-%d", NFNL_SUBSYS_ID(type)); |
a3c5029c | 242 | nfnl_lock(); |
37d2e7a2 | 243 | ss = nfnetlink_get_subsys(type); |
0ab43f84 HW |
244 | if (!ss) |
245 | #endif | |
ed77de9f | 246 | goto err_inval; |
0ab43f84 | 247 | } |
f9e815b3 HW |
248 | |
249 | nc = nfnetlink_find_client(type, ss); | |
67ca3966 | 250 | if (!nc) |
f9e815b3 | 251 | goto err_inval; |
f9e815b3 | 252 | |
f9e815b3 | 253 | { |
601e68e1 | 254 | u_int16_t attr_count = |
927ccbcc HW |
255 | ss->cb[NFNL_MSG_TYPE(nlh->nlmsg_type)].attr_count; |
256 | struct nfattr *cda[attr_count]; | |
f9e815b3 | 257 | |
927ccbcc | 258 | memset(cda, 0, sizeof(struct nfattr *) * attr_count); |
601e68e1 | 259 | |
f9e815b3 HW |
260 | err = nfnetlink_check_attributes(ss, nlh, cda); |
261 | if (err < 0) | |
262 | goto err_inval; | |
263 | ||
264 | err = nc->call(nfnl, skb, nlh, cda, errp); | |
265 | *errp = err; | |
266 | return err; | |
267 | } | |
268 | ||
269 | err_inval: | |
270 | *errp = -EINVAL; | |
271 | return -1; | |
272 | } | |
273 | ||
f9e815b3 HW |
274 | static void nfnetlink_rcv(struct sock *sk, int len) |
275 | { | |
73c36186 | 276 | unsigned int qlen = 0; |
f9e815b3 | 277 | |
73c36186 | 278 | do { |
a3c5029c | 279 | if (nfnl_trylock()) |
f9e815b3 | 280 | return; |
73c36186 | 281 | netlink_run_queue(sk, &qlen, nfnetlink_rcv_msg); |
a3c5029c | 282 | __nfnl_unlock(); |
73c36186 | 283 | } while (qlen); |
f9e815b3 HW |
284 | } |
285 | ||
395dde20 | 286 | static void __exit nfnetlink_exit(void) |
f9e815b3 HW |
287 | { |
288 | printk("Removing netfilter NETLINK layer.\n"); | |
289 | sock_release(nfnl->sk_socket); | |
290 | return; | |
291 | } | |
292 | ||
395dde20 | 293 | static int __init nfnetlink_init(void) |
f9e815b3 HW |
294 | { |
295 | printk("Netfilter messages via NETLINK v%s.\n", nfversion); | |
296 | ||
06628607 | 297 | nfnl = netlink_kernel_create(NETLINK_NETFILTER, NFNLGRP_MAX, |
601e68e1 | 298 | nfnetlink_rcv, THIS_MODULE); |
f9e815b3 HW |
299 | if (!nfnl) { |
300 | printk(KERN_ERR "cannot initialize nfnetlink!\n"); | |
301 | return -1; | |
302 | } | |
303 | ||
304 | return 0; | |
305 | } | |
306 | ||
307 | module_init(nfnetlink_init); | |
308 | module_exit(nfnetlink_exit); | |
309 | ||
310 | EXPORT_SYMBOL_GPL(nfnetlink_subsys_register); | |
311 | EXPORT_SYMBOL_GPL(nfnetlink_subsys_unregister); | |
312 | EXPORT_SYMBOL_GPL(nfnetlink_send); | |
313 | EXPORT_SYMBOL_GPL(nfnetlink_unicast); | |
314 | EXPORT_SYMBOL_GPL(nfattr_parse); | |
315 | EXPORT_SYMBOL_GPL(__nfa_fill); |