]>
Commit | Line | Data |
---|---|---|
d2912cb1 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
0597f268 HW |
2 | /* |
3 | * This is a module which is used for logging packets to userspace via | |
4 | * nfetlink. | |
5 | * | |
6 | * (C) 2005 by Harald Welte <laforge@netfilter.org> | |
f229f6ce | 7 | * (C) 2006-2012 Patrick McHardy <kaber@trash.net> |
0597f268 HW |
8 | * |
9 | * Based on the old ipv4-only ipt_ULOG.c: | |
10 | * (C) 2000-2004 by Harald Welte <laforge@netfilter.org> | |
0597f268 | 11 | */ |
beacd3e8 ML |
12 | |
13 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt | |
14 | ||
0597f268 HW |
15 | #include <linux/module.h> |
16 | #include <linux/skbuff.h> | |
e035edd1 | 17 | #include <linux/if_arp.h> |
0597f268 HW |
18 | #include <linux/init.h> |
19 | #include <linux/ip.h> | |
20 | #include <linux/ipv6.h> | |
21 | #include <linux/netdevice.h> | |
22 | #include <linux/netfilter.h> | |
c737b7c4 | 23 | #include <linux/netfilter_bridge.h> |
573ce260 | 24 | #include <net/netlink.h> |
0597f268 HW |
25 | #include <linux/netfilter/nfnetlink.h> |
26 | #include <linux/netfilter/nfnetlink_log.h> | |
a29a9a58 | 27 | #include <linux/netfilter/nf_conntrack_common.h> |
0597f268 HW |
28 | #include <linux/spinlock.h> |
29 | #include <linux/sysctl.h> | |
30 | #include <linux/proc_fs.h> | |
31 | #include <linux/security.h> | |
32 | #include <linux/list.h> | |
5a0e3ad6 | 33 | #include <linux/slab.h> |
0597f268 | 34 | #include <net/sock.h> |
f01ffbd6 | 35 | #include <net/netfilter/nf_log.h> |
9368a53c | 36 | #include <net/netns/generic.h> |
0597f268 | 37 | |
60063497 | 38 | #include <linux/atomic.h> |
b54ab92b RE |
39 | #include <linux/refcount.h> |
40 | ||
0597f268 | 41 | |
1109a90c | 42 | #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER) |
fbcd923c HW |
43 | #include "../bridge/br_private.h" |
44 | #endif | |
45 | ||
83ace77f FW |
46 | #if IS_ENABLED(CONFIG_NF_CONNTRACK) |
47 | #include <net/netfilter/nf_conntrack.h> | |
48 | #endif | |
49 | ||
ce20cdf4 | 50 | #define NFULNL_COPY_DISABLED 0xff |
c2db2924 | 51 | #define NFULNL_NLBUFSIZ_DEFAULT NLMSG_GOODSIZE |
2c6764b7 | 52 | #define NFULNL_TIMEOUT_DEFAULT 100 /* every second */ |
0597f268 | 53 | #define NFULNL_QTHRESH_DEFAULT 100 /* 100 packets */ |
c1e7dc91 FW |
54 | /* max packet size is limited by 16-bit struct nfattr nfa_len field */ |
55 | #define NFULNL_COPY_RANGE_MAX (0xFFFF - NLA_HDRLEN) | |
0597f268 HW |
56 | |
57 | #define PRINTR(x, args...) do { if (net_ratelimit()) \ | |
58 | printk(x, ## args); } while (0); | |
59 | ||
0597f268 HW |
60 | struct nfulnl_instance { |
61 | struct hlist_node hlist; /* global list of instances */ | |
62 | spinlock_t lock; | |
b54ab92b | 63 | refcount_t use; /* use count */ |
0597f268 HW |
64 | |
65 | unsigned int qlen; /* number of nlmsgs in skb */ | |
66 | struct sk_buff *skb; /* pre-allocatd skb */ | |
0597f268 | 67 | struct timer_list timer; |
9368a53c | 68 | struct net *net; |
a9382d93 | 69 | netns_tracker ns_tracker; |
9eea9515 | 70 | struct user_namespace *peer_user_ns; /* User namespace of the peer process */ |
cc6bc448 | 71 | u32 peer_portid; /* PORTID of the peer process */ |
0597f268 HW |
72 | |
73 | /* configurable parameters */ | |
74 | unsigned int flushtimeout; /* timeout until queue flush */ | |
75 | unsigned int nlbufsiz; /* netlink buffer allocation size */ | |
76 | unsigned int qthreshold; /* threshold of the queue */ | |
77 | u_int32_t copy_range; | |
0af5f6c1 | 78 | u_int32_t seq; /* instance-local sequential counter */ |
0597f268 | 79 | u_int16_t group_num; /* number of this queue */ |
0af5f6c1 | 80 | u_int16_t flags; |
601e68e1 | 81 | u_int8_t copy_mode; |
bed1be20 | 82 | struct rcu_head rcu; |
0597f268 HW |
83 | }; |
84 | ||
0597f268 | 85 | #define INSTANCE_BUCKETS 16 |
0597f268 | 86 | |
c7d03a00 | 87 | static unsigned int nfnl_log_net_id __read_mostly; |
9368a53c G |
88 | |
89 | struct nfnl_log_net { | |
90 | spinlock_t instances_lock; | |
91 | struct hlist_head instance_table[INSTANCE_BUCKETS]; | |
92 | atomic_t global_seq; | |
93 | }; | |
94 | ||
95 | static struct nfnl_log_net *nfnl_log_pernet(struct net *net) | |
96 | { | |
97 | return net_generic(net, nfnl_log_net_id); | |
98 | } | |
99 | ||
0597f268 HW |
100 | static inline u_int8_t instance_hashfn(u_int16_t group_num) |
101 | { | |
102 | return ((group_num & 0xff) % INSTANCE_BUCKETS); | |
103 | } | |
104 | ||
105 | static struct nfulnl_instance * | |
356e2adb | 106 | __instance_lookup(const struct nfnl_log_net *log, u16 group_num) |
0597f268 | 107 | { |
356e2adb | 108 | const struct hlist_head *head; |
0597f268 HW |
109 | struct nfulnl_instance *inst; |
110 | ||
9368a53c | 111 | head = &log->instance_table[instance_hashfn(group_num)]; |
b67bfe0d | 112 | hlist_for_each_entry_rcu(inst, head, hlist) { |
0597f268 HW |
113 | if (inst->group_num == group_num) |
114 | return inst; | |
115 | } | |
116 | return NULL; | |
117 | } | |
118 | ||
119 | static inline void | |
120 | instance_get(struct nfulnl_instance *inst) | |
121 | { | |
b54ab92b | 122 | refcount_inc(&inst->use); |
0597f268 HW |
123 | } |
124 | ||
125 | static struct nfulnl_instance * | |
356e2adb | 126 | instance_lookup_get_rcu(const struct nfnl_log_net *log, u16 group_num) |
0597f268 HW |
127 | { |
128 | struct nfulnl_instance *inst; | |
129 | ||
9368a53c | 130 | inst = __instance_lookup(log, group_num); |
b54ab92b | 131 | if (inst && !refcount_inc_not_zero(&inst->use)) |
f5c5440d | 132 | inst = NULL; |
356e2adb FW |
133 | |
134 | return inst; | |
135 | } | |
136 | ||
137 | static struct nfulnl_instance * | |
138 | instance_lookup_get(const struct nfnl_log_net *log, u16 group_num) | |
139 | { | |
140 | struct nfulnl_instance *inst; | |
141 | ||
142 | rcu_read_lock(); | |
143 | inst = instance_lookup_get_rcu(log, group_num); | |
144 | rcu_read_unlock(); | |
0597f268 HW |
145 | |
146 | return inst; | |
147 | } | |
148 | ||
bed1be20 ED |
149 | static void nfulnl_instance_free_rcu(struct rcu_head *head) |
150 | { | |
9368a53c G |
151 | struct nfulnl_instance *inst = |
152 | container_of(head, struct nfulnl_instance, rcu); | |
153 | ||
a9382d93 | 154 | put_net_track(inst->net, &inst->ns_tracker); |
9368a53c | 155 | kfree(inst); |
bed1be20 ED |
156 | module_put(THIS_MODULE); |
157 | } | |
158 | ||
0597f268 HW |
159 | static void |
160 | instance_put(struct nfulnl_instance *inst) | |
161 | { | |
b54ab92b | 162 | if (inst && refcount_dec_and_test(&inst->use)) |
c8d1da40 | 163 | call_rcu(&inst->rcu, nfulnl_instance_free_rcu); |
0597f268 HW |
164 | } |
165 | ||
e99e88a9 | 166 | static void nfulnl_timer(struct timer_list *t); |
0597f268 HW |
167 | |
168 | static struct nfulnl_instance * | |
9368a53c | 169 | instance_create(struct net *net, u_int16_t group_num, |
cc6bc448 | 170 | u32 portid, struct user_namespace *user_ns) |
0597f268 HW |
171 | { |
172 | struct nfulnl_instance *inst; | |
9368a53c | 173 | struct nfnl_log_net *log = nfnl_log_pernet(net); |
baab2ce7 | 174 | int err; |
0597f268 | 175 | |
9368a53c G |
176 | spin_lock_bh(&log->instances_lock); |
177 | if (__instance_lookup(log, group_num)) { | |
baab2ce7 | 178 | err = -EEXIST; |
0597f268 HW |
179 | goto out_unlock; |
180 | } | |
181 | ||
10dfdc69 | 182 | inst = kzalloc(sizeof(*inst), GFP_ATOMIC); |
baab2ce7 PM |
183 | if (!inst) { |
184 | err = -ENOMEM; | |
0597f268 | 185 | goto out_unlock; |
baab2ce7 | 186 | } |
0597f268 | 187 | |
aace57e0 MM |
188 | if (!try_module_get(THIS_MODULE)) { |
189 | kfree(inst); | |
baab2ce7 | 190 | err = -EAGAIN; |
aace57e0 MM |
191 | goto out_unlock; |
192 | } | |
193 | ||
0597f268 | 194 | INIT_HLIST_NODE(&inst->hlist); |
181a46a5 | 195 | spin_lock_init(&inst->lock); |
0597f268 | 196 | /* needs to be two, since we _put() after creation */ |
b54ab92b | 197 | refcount_set(&inst->use, 2); |
0597f268 | 198 | |
e99e88a9 | 199 | timer_setup(&inst->timer, nfulnl_timer, 0); |
0597f268 | 200 | |
a9382d93 | 201 | inst->net = get_net_track(net, &inst->ns_tracker, GFP_ATOMIC); |
9eea9515 | 202 | inst->peer_user_ns = user_ns; |
15e47304 | 203 | inst->peer_portid = portid; |
0597f268 HW |
204 | inst->group_num = group_num; |
205 | ||
206 | inst->qthreshold = NFULNL_QTHRESH_DEFAULT; | |
207 | inst->flushtimeout = NFULNL_TIMEOUT_DEFAULT; | |
208 | inst->nlbufsiz = NFULNL_NLBUFSIZ_DEFAULT; | |
209 | inst->copy_mode = NFULNL_COPY_PACKET; | |
6b6ec99a | 210 | inst->copy_range = NFULNL_COPY_RANGE_MAX; |
0597f268 | 211 | |
f5c5440d | 212 | hlist_add_head_rcu(&inst->hlist, |
9368a53c G |
213 | &log->instance_table[instance_hashfn(group_num)]); |
214 | ||
0597f268 | 215 | |
9368a53c | 216 | spin_unlock_bh(&log->instances_lock); |
0597f268 HW |
217 | |
218 | return inst; | |
219 | ||
0597f268 | 220 | out_unlock: |
9368a53c | 221 | spin_unlock_bh(&log->instances_lock); |
baab2ce7 | 222 | return ERR_PTR(err); |
0597f268 HW |
223 | } |
224 | ||
e3567061 | 225 | static void __nfulnl_flush(struct nfulnl_instance *inst); |
0597f268 | 226 | |
f5c5440d | 227 | /* called with BH disabled */ |
0597f268 | 228 | static void |
9afdb00c | 229 | __instance_destroy(struct nfulnl_instance *inst) |
0597f268 HW |
230 | { |
231 | /* first pull it out of the global list */ | |
f5c5440d | 232 | hlist_del_rcu(&inst->hlist); |
0597f268 | 233 | |
0597f268 HW |
234 | /* then flush all pending packets from skb */ |
235 | ||
f5c5440d ED |
236 | spin_lock(&inst->lock); |
237 | ||
238 | /* lockless readers wont be able to use us */ | |
239 | inst->copy_mode = NFULNL_COPY_DISABLED; | |
240 | ||
e3567061 MM |
241 | if (inst->skb) |
242 | __nfulnl_flush(inst); | |
f5c5440d | 243 | spin_unlock(&inst->lock); |
0597f268 HW |
244 | |
245 | /* and finally put the refcount */ | |
246 | instance_put(inst); | |
0597f268 HW |
247 | } |
248 | ||
0597f268 | 249 | static inline void |
9368a53c G |
250 | instance_destroy(struct nfnl_log_net *log, |
251 | struct nfulnl_instance *inst) | |
0597f268 | 252 | { |
9368a53c | 253 | spin_lock_bh(&log->instances_lock); |
9afdb00c | 254 | __instance_destroy(inst); |
9368a53c | 255 | spin_unlock_bh(&log->instances_lock); |
0597f268 HW |
256 | } |
257 | ||
258 | static int | |
259 | nfulnl_set_mode(struct nfulnl_instance *inst, u_int8_t mode, | |
260 | unsigned int range) | |
261 | { | |
262 | int status = 0; | |
263 | ||
264 | spin_lock_bh(&inst->lock); | |
601e68e1 | 265 | |
0597f268 HW |
266 | switch (mode) { |
267 | case NFULNL_COPY_NONE: | |
268 | case NFULNL_COPY_META: | |
269 | inst->copy_mode = mode; | |
270 | inst->copy_range = 0; | |
271 | break; | |
601e68e1 | 272 | |
0597f268 HW |
273 | case NFULNL_COPY_PACKET: |
274 | inst->copy_mode = mode; | |
c1e7dc91 FW |
275 | if (range == 0) |
276 | range = NFULNL_COPY_RANGE_MAX; | |
6b6ec99a MM |
277 | inst->copy_range = min_t(unsigned int, |
278 | range, NFULNL_COPY_RANGE_MAX); | |
0597f268 | 279 | break; |
601e68e1 | 280 | |
0597f268 HW |
281 | default: |
282 | status = -EINVAL; | |
283 | break; | |
284 | } | |
285 | ||
286 | spin_unlock_bh(&inst->lock); | |
287 | ||
288 | return status; | |
289 | } | |
290 | ||
291 | static int | |
292 | nfulnl_set_nlbufsiz(struct nfulnl_instance *inst, u_int32_t nlbufsiz) | |
293 | { | |
294 | int status; | |
295 | ||
296 | spin_lock_bh(&inst->lock); | |
297 | if (nlbufsiz < NFULNL_NLBUFSIZ_DEFAULT) | |
298 | status = -ERANGE; | |
299 | else if (nlbufsiz > 131072) | |
300 | status = -ERANGE; | |
301 | else { | |
302 | inst->nlbufsiz = nlbufsiz; | |
303 | status = 0; | |
304 | } | |
305 | spin_unlock_bh(&inst->lock); | |
306 | ||
307 | return status; | |
308 | } | |
309 | ||
23509fcd | 310 | static void |
0597f268 HW |
311 | nfulnl_set_timeout(struct nfulnl_instance *inst, u_int32_t timeout) |
312 | { | |
313 | spin_lock_bh(&inst->lock); | |
314 | inst->flushtimeout = timeout; | |
315 | spin_unlock_bh(&inst->lock); | |
0597f268 HW |
316 | } |
317 | ||
23509fcd | 318 | static void |
0597f268 HW |
319 | nfulnl_set_qthresh(struct nfulnl_instance *inst, u_int32_t qthresh) |
320 | { | |
321 | spin_lock_bh(&inst->lock); | |
322 | inst->qthreshold = qthresh; | |
323 | spin_unlock_bh(&inst->lock); | |
0597f268 HW |
324 | } |
325 | ||
0af5f6c1 HW |
326 | static int |
327 | nfulnl_set_flags(struct nfulnl_instance *inst, u_int16_t flags) | |
328 | { | |
329 | spin_lock_bh(&inst->lock); | |
ee433530 | 330 | inst->flags = flags; |
0af5f6c1 HW |
331 | spin_unlock_bh(&inst->lock); |
332 | ||
333 | return 0; | |
334 | } | |
335 | ||
c6a8f648 | 336 | static struct sk_buff * |
afff14f6 G |
337 | nfulnl_alloc_skb(struct net *net, u32 peer_portid, unsigned int inst_size, |
338 | unsigned int pkt_size) | |
0597f268 HW |
339 | { |
340 | struct sk_buff *skb; | |
ad2ad0f9 | 341 | unsigned int n; |
0597f268 | 342 | |
0597f268 HW |
343 | /* alloc skb which should be big enough for a whole multipart |
344 | * message. WARNING: has to be <= 128k due to slab restrictions */ | |
345 | ||
ad2ad0f9 | 346 | n = max(inst_size, pkt_size); |
0813fbc9 | 347 | skb = alloc_skb(n, GFP_ATOMIC | __GFP_NOWARN); |
0597f268 | 348 | if (!skb) { |
ad2ad0f9 PM |
349 | if (n > pkt_size) { |
350 | /* try to allocate only as much as we need for current | |
351 | * packet */ | |
0597f268 | 352 | |
905f0a73 | 353 | skb = alloc_skb(pkt_size, GFP_ATOMIC); |
ad2ad0f9 | 354 | } |
0597f268 HW |
355 | } |
356 | ||
357 | return skb; | |
358 | } | |
359 | ||
b51d3fa3 | 360 | static void |
0597f268 HW |
361 | __nfulnl_send(struct nfulnl_instance *inst) |
362 | { | |
d550d095 DM |
363 | if (inst->qlen > 1) { |
364 | struct nlmsghdr *nlh = nlmsg_put(inst->skb, 0, 0, | |
365 | NLMSG_DONE, | |
366 | sizeof(struct nfgenmsg), | |
367 | 0); | |
b51d3fa3 HL |
368 | if (WARN_ONCE(!nlh, "bad nlskb size: %u, tailroom %d\n", |
369 | inst->skb->len, skb_tailroom(inst->skb))) { | |
370 | kfree_skb(inst->skb); | |
d550d095 | 371 | goto out; |
b51d3fa3 | 372 | } |
d550d095 | 373 | } |
ee921183 | 374 | nfnetlink_unicast(inst->skb, inst->net, inst->peer_portid); |
b51d3fa3 | 375 | out: |
0597f268 HW |
376 | inst->qlen = 0; |
377 | inst->skb = NULL; | |
0597f268 HW |
378 | } |
379 | ||
e3567061 MM |
380 | static void |
381 | __nfulnl_flush(struct nfulnl_instance *inst) | |
382 | { | |
383 | /* timer holds a reference */ | |
384 | if (del_timer(&inst->timer)) | |
385 | instance_put(inst); | |
386 | if (inst->skb) | |
387 | __nfulnl_send(inst); | |
388 | } | |
389 | ||
c6a8f648 | 390 | static void |
e99e88a9 | 391 | nfulnl_timer(struct timer_list *t) |
0597f268 | 392 | { |
e99e88a9 | 393 | struct nfulnl_instance *inst = from_timer(inst, t, timer); |
0597f268 | 394 | |
0597f268 | 395 | spin_lock_bh(&inst->lock); |
370e6a87 MM |
396 | if (inst->skb) |
397 | __nfulnl_send(inst); | |
0597f268 | 398 | spin_unlock_bh(&inst->lock); |
05f7b7b3 | 399 | instance_put(inst); |
0597f268 HW |
400 | } |
401 | ||
65af4a10 MB |
402 | static u32 nfulnl_get_bridge_size(const struct sk_buff *skb) |
403 | { | |
404 | u32 size = 0; | |
405 | ||
406 | if (!skb_mac_header_was_set(skb)) | |
407 | return 0; | |
408 | ||
409 | if (skb_vlan_tag_present(skb)) { | |
410 | size += nla_total_size(0); /* nested */ | |
411 | size += nla_total_size(sizeof(u16)); /* id */ | |
412 | size += nla_total_size(sizeof(u16)); /* tag */ | |
413 | } | |
414 | ||
415 | if (skb->network_header > skb->mac_header) | |
416 | size += nla_total_size(skb->network_header - skb->mac_header); | |
417 | ||
418 | return size; | |
419 | } | |
420 | ||
421 | static int nfulnl_put_bridge(struct nfulnl_instance *inst, const struct sk_buff *skb) | |
422 | { | |
423 | if (!skb_mac_header_was_set(skb)) | |
424 | return 0; | |
425 | ||
426 | if (skb_vlan_tag_present(skb)) { | |
427 | struct nlattr *nest; | |
428 | ||
429 | nest = nla_nest_start(inst->skb, NFULA_VLAN); | |
430 | if (!nest) | |
431 | goto nla_put_failure; | |
432 | ||
433 | if (nla_put_be16(inst->skb, NFULA_VLAN_TCI, htons(skb->vlan_tci)) || | |
434 | nla_put_be16(inst->skb, NFULA_VLAN_PROTO, skb->vlan_proto)) | |
435 | goto nla_put_failure; | |
436 | ||
437 | nla_nest_end(inst->skb, nest); | |
438 | } | |
439 | ||
440 | if (skb->mac_header < skb->network_header) { | |
441 | int len = (int)(skb->network_header - skb->mac_header); | |
442 | ||
443 | if (nla_put(inst->skb, NFULA_L2HDR, len, skb_mac_header(skb))) | |
444 | goto nla_put_failure; | |
445 | } | |
446 | ||
447 | return 0; | |
448 | ||
449 | nla_put_failure: | |
450 | return -1; | |
451 | } | |
452 | ||
0af5f6c1 HW |
453 | /* This is an inline function, we don't really care about a long |
454 | * list of arguments */ | |
601e68e1 | 455 | static inline int |
9368a53c G |
456 | __build_packet_message(struct nfnl_log_net *log, |
457 | struct nfulnl_instance *inst, | |
601e68e1 | 458 | const struct sk_buff *skb, |
0597f268 | 459 | unsigned int data_len, |
76108cea | 460 | u_int8_t pf, |
0597f268 HW |
461 | unsigned int hooknum, |
462 | const struct net_device *indev, | |
463 | const struct net_device *outdev, | |
a29a9a58 KM |
464 | const char *prefix, unsigned int plen, |
465 | const struct nfnl_ct_hook *nfnl_ct, | |
466 | struct nf_conn *ct, enum ip_conntrack_info ctinfo) | |
0597f268 | 467 | { |
0597f268 HW |
468 | struct nfulnl_msg_packet_hdr pmsg; |
469 | struct nlmsghdr *nlh; | |
27a884dc | 470 | sk_buff_data_t old_tail = inst->skb->tail; |
0626af31 | 471 | struct sock *sk; |
0c36b48b | 472 | const unsigned char *hwhdrp; |
0597f268 | 473 | |
19c28b13 PNA |
474 | nlh = nfnl_msg_put(inst->skb, 0, 0, |
475 | nfnl_msg_type(NFNL_SUBSYS_ULOG, NFULNL_MSG_PACKET), | |
476 | 0, pf, NFNETLINK_V0, htons(inst->group_num)); | |
d550d095 DM |
477 | if (!nlh) |
478 | return -1; | |
0597f268 | 479 | |
e4d091d7 | 480 | memset(&pmsg, 0, sizeof(pmsg)); |
febf0a43 | 481 | pmsg.hw_protocol = skb->protocol; |
0597f268 HW |
482 | pmsg.hook = hooknum; |
483 | ||
1db20a52 DM |
484 | if (nla_put(inst->skb, NFULA_PACKET_HDR, sizeof(pmsg), &pmsg)) |
485 | goto nla_put_failure; | |
0597f268 | 486 | |
1db20a52 DM |
487 | if (prefix && |
488 | nla_put(inst->skb, NFULA_PREFIX, plen, prefix)) | |
489 | goto nla_put_failure; | |
0597f268 HW |
490 | |
491 | if (indev) { | |
1109a90c | 492 | #if !IS_ENABLED(CONFIG_BRIDGE_NETFILTER) |
1db20a52 DM |
493 | if (nla_put_be32(inst->skb, NFULA_IFINDEX_INDEV, |
494 | htonl(indev->ifindex))) | |
495 | goto nla_put_failure; | |
fbcd923c HW |
496 | #else |
497 | if (pf == PF_BRIDGE) { | |
498 | /* Case 1: outdev is physical input device, we need to | |
499 | * look for bridge group (when called from | |
500 | * netfilter_bridge) */ | |
1db20a52 DM |
501 | if (nla_put_be32(inst->skb, NFULA_IFINDEX_PHYSINDEV, |
502 | htonl(indev->ifindex)) || | |
fbcd923c | 503 | /* this is the bridge group "brX" */ |
e2361cb9 AC |
504 | /* rcu_read_lock()ed by nf_hook_thresh or |
505 | * nf_log_packet. | |
506 | */ | |
1db20a52 DM |
507 | nla_put_be32(inst->skb, NFULA_IFINDEX_INDEV, |
508 | htonl(br_port_get_rcu(indev)->br->dev->ifindex))) | |
509 | goto nla_put_failure; | |
fbcd923c | 510 | } else { |
c737b7c4 FW |
511 | struct net_device *physindev; |
512 | ||
fbcd923c HW |
513 | /* Case 2: indev is bridge group, we need to look for |
514 | * physical device (when called from ipv4) */ | |
1db20a52 DM |
515 | if (nla_put_be32(inst->skb, NFULA_IFINDEX_INDEV, |
516 | htonl(indev->ifindex))) | |
517 | goto nla_put_failure; | |
c737b7c4 FW |
518 | |
519 | physindev = nf_bridge_get_physindev(skb); | |
520 | if (physindev && | |
1db20a52 | 521 | nla_put_be32(inst->skb, NFULA_IFINDEX_PHYSINDEV, |
c737b7c4 | 522 | htonl(physindev->ifindex))) |
1db20a52 | 523 | goto nla_put_failure; |
fbcd923c HW |
524 | } |
525 | #endif | |
0597f268 HW |
526 | } |
527 | ||
528 | if (outdev) { | |
1109a90c | 529 | #if !IS_ENABLED(CONFIG_BRIDGE_NETFILTER) |
1db20a52 DM |
530 | if (nla_put_be32(inst->skb, NFULA_IFINDEX_OUTDEV, |
531 | htonl(outdev->ifindex))) | |
532 | goto nla_put_failure; | |
fbcd923c HW |
533 | #else |
534 | if (pf == PF_BRIDGE) { | |
535 | /* Case 1: outdev is physical output device, we need to | |
536 | * look for bridge group (when called from | |
537 | * netfilter_bridge) */ | |
1db20a52 DM |
538 | if (nla_put_be32(inst->skb, NFULA_IFINDEX_PHYSOUTDEV, |
539 | htonl(outdev->ifindex)) || | |
fbcd923c | 540 | /* this is the bridge group "brX" */ |
e2361cb9 AC |
541 | /* rcu_read_lock()ed by nf_hook_thresh or |
542 | * nf_log_packet. | |
543 | */ | |
1db20a52 DM |
544 | nla_put_be32(inst->skb, NFULA_IFINDEX_OUTDEV, |
545 | htonl(br_port_get_rcu(outdev)->br->dev->ifindex))) | |
546 | goto nla_put_failure; | |
fbcd923c | 547 | } else { |
c737b7c4 FW |
548 | struct net_device *physoutdev; |
549 | ||
fbcd923c HW |
550 | /* Case 2: indev is a bridge group, we need to look |
551 | * for physical device (when called from ipv4) */ | |
1db20a52 DM |
552 | if (nla_put_be32(inst->skb, NFULA_IFINDEX_OUTDEV, |
553 | htonl(outdev->ifindex))) | |
554 | goto nla_put_failure; | |
c737b7c4 FW |
555 | |
556 | physoutdev = nf_bridge_get_physoutdev(skb); | |
557 | if (physoutdev && | |
1db20a52 | 558 | nla_put_be32(inst->skb, NFULA_IFINDEX_PHYSOUTDEV, |
c737b7c4 | 559 | htonl(physoutdev->ifindex))) |
1db20a52 | 560 | goto nla_put_failure; |
fbcd923c HW |
561 | } |
562 | #endif | |
0597f268 HW |
563 | } |
564 | ||
1db20a52 DM |
565 | if (skb->mark && |
566 | nla_put_be32(inst->skb, NFULA_MARK, htonl(skb->mark))) | |
567 | goto nla_put_failure; | |
0597f268 | 568 | |
2c38de4c | 569 | if (indev && skb->dev && |
ebb966d3 IG |
570 | skb_mac_header_was_set(skb) && |
571 | skb_mac_header_len(skb) != 0) { | |
0597f268 | 572 | struct nfulnl_msg_packet_hw phw; |
e4d091d7 DC |
573 | int len; |
574 | ||
575 | memset(&phw, 0, sizeof(phw)); | |
576 | len = dev_parse_header(skb, phw.hw_addr); | |
b95cce35 SH |
577 | if (len > 0) { |
578 | phw.hw_addrlen = htons(len); | |
1db20a52 DM |
579 | if (nla_put(inst->skb, NFULA_HWADDR, sizeof(phw), &phw)) |
580 | goto nla_put_failure; | |
b95cce35 | 581 | } |
0597f268 HW |
582 | } |
583 | ||
72961ecf | 584 | if (indev && skb_mac_header_was_set(skb)) { |
2dba62c3 | 585 | if (nla_put_be16(inst->skb, NFULA_HWTYPE, htons(skb->dev->type)) || |
1db20a52 | 586 | nla_put_be16(inst->skb, NFULA_HWLEN, |
0c36b48b BH |
587 | htons(skb->dev->hard_header_len))) |
588 | goto nla_put_failure; | |
589 | ||
590 | hwhdrp = skb_mac_header(skb); | |
591 | ||
592 | if (skb->dev->type == ARPHRD_SIT) | |
593 | hwhdrp -= ETH_HLEN; | |
594 | ||
595 | if (hwhdrp >= skb->head && | |
596 | nla_put(inst->skb, NFULA_HWHEADER, | |
597 | skb->dev->hard_header_len, hwhdrp)) | |
1db20a52 | 598 | goto nla_put_failure; |
72961ecf EL |
599 | } |
600 | ||
1d85594f MÅ» |
601 | if (hooknum <= NF_INET_FORWARD) { |
602 | struct timespec64 kts = ktime_to_timespec64(skb_tstamp_cond(skb, true)); | |
0597f268 | 603 | struct nfulnl_msg_packet_timestamp ts; |
f6389ecb AB |
604 | ts.sec = cpu_to_be64(kts.tv_sec); |
605 | ts.usec = cpu_to_be64(kts.tv_nsec / NSEC_PER_USEC); | |
0597f268 | 606 | |
1db20a52 DM |
607 | if (nla_put(inst->skb, NFULA_TIMESTAMP, sizeof(ts), &ts)) |
608 | goto nla_put_failure; | |
0597f268 HW |
609 | } |
610 | ||
611 | /* UID */ | |
0626af31 | 612 | sk = skb->sk; |
a8399231 | 613 | if (sk && sk_fullsock(sk)) { |
0626af31 ED |
614 | read_lock_bh(&sk->sk_callback_lock); |
615 | if (sk->sk_socket && sk->sk_socket->file) { | |
616 | struct file *file = sk->sk_socket->file; | |
437589a7 LT |
617 | const struct cred *cred = file->f_cred; |
618 | struct user_namespace *user_ns = inst->peer_user_ns; | |
619 | __be32 uid = htonl(from_kuid_munged(user_ns, cred->fsuid)); | |
620 | __be32 gid = htonl(from_kgid_munged(user_ns, cred->fsgid)); | |
0626af31 | 621 | read_unlock_bh(&sk->sk_callback_lock); |
1db20a52 DM |
622 | if (nla_put_be32(inst->skb, NFULA_UID, uid) || |
623 | nla_put_be32(inst->skb, NFULA_GID, gid)) | |
624 | goto nla_put_failure; | |
0597f268 | 625 | } else |
0626af31 | 626 | read_unlock_bh(&sk->sk_callback_lock); |
0597f268 HW |
627 | } |
628 | ||
0af5f6c1 | 629 | /* local sequence number */ |
1db20a52 DM |
630 | if ((inst->flags & NFULNL_CFG_F_SEQ) && |
631 | nla_put_be32(inst->skb, NFULA_SEQ, htonl(inst->seq++))) | |
632 | goto nla_put_failure; | |
0dfedd28 | 633 | |
0af5f6c1 | 634 | /* global sequence number */ |
1db20a52 DM |
635 | if ((inst->flags & NFULNL_CFG_F_SEQ_GLOBAL) && |
636 | nla_put_be32(inst->skb, NFULA_SEQ_GLOBAL, | |
9368a53c | 637 | htonl(atomic_inc_return(&log->global_seq)))) |
1db20a52 | 638 | goto nla_put_failure; |
0af5f6c1 | 639 | |
a29a9a58 KM |
640 | if (ct && nfnl_ct->build(inst->skb, ct, ctinfo, |
641 | NFULA_CT, NFULA_CT_INFO) < 0) | |
642 | goto nla_put_failure; | |
643 | ||
65af4a10 MB |
644 | if ((pf == NFPROTO_NETDEV || pf == NFPROTO_BRIDGE) && |
645 | nfulnl_put_bridge(inst, skb) < 0) | |
646 | goto nla_put_failure; | |
647 | ||
0597f268 | 648 | if (data_len) { |
df6fb868 PM |
649 | struct nlattr *nla; |
650 | int size = nla_attr_size(data_len); | |
0597f268 | 651 | |
82251615 PNA |
652 | if (skb_tailroom(inst->skb) < nla_total_size(data_len)) |
653 | goto nla_put_failure; | |
0597f268 | 654 | |
4df864c1 | 655 | nla = skb_put(inst->skb, nla_total_size(data_len)); |
df6fb868 PM |
656 | nla->nla_type = NFULA_PAYLOAD; |
657 | nla->nla_len = size; | |
0597f268 | 658 | |
df6fb868 | 659 | if (skb_copy_bits(skb, 0, nla_data(nla), data_len)) |
0597f268 HW |
660 | BUG(); |
661 | } | |
601e68e1 | 662 | |
0597f268 HW |
663 | nlh->nlmsg_len = inst->skb->tail - old_tail; |
664 | return 0; | |
665 | ||
df6fb868 | 666 | nla_put_failure: |
0597f268 HW |
667 | PRINTR(KERN_ERR "nfnetlink_log: error creating log nlmsg\n"); |
668 | return -1; | |
669 | } | |
670 | ||
549d2d41 | 671 | static const struct nf_loginfo default_loginfo = { |
0597f268 HW |
672 | .type = NF_LOG_TYPE_ULOG, |
673 | .u = { | |
674 | .ulog = { | |
675 | .copy_len = 0xffff, | |
676 | .group = 0, | |
677 | .qthreshold = 1, | |
678 | }, | |
679 | }, | |
680 | }; | |
681 | ||
682 | /* log handler for internal netfilter logging api */ | |
ce20cdf4 | 683 | static void |
8cdb46da HS |
684 | nfulnl_log_packet(struct net *net, |
685 | u_int8_t pf, | |
0597f268 HW |
686 | unsigned int hooknum, |
687 | const struct sk_buff *skb, | |
688 | const struct net_device *in, | |
689 | const struct net_device *out, | |
690 | const struct nf_loginfo *li_user, | |
691 | const char *prefix) | |
692 | { | |
a29a9a58 KM |
693 | size_t size; |
694 | unsigned int data_len; | |
0597f268 HW |
695 | struct nfulnl_instance *inst; |
696 | const struct nf_loginfo *li; | |
697 | unsigned int qthreshold; | |
ce20cdf4 | 698 | unsigned int plen = 0; |
9368a53c | 699 | struct nfnl_log_net *log = nfnl_log_pernet(net); |
a29a9a58 | 700 | const struct nfnl_ct_hook *nfnl_ct = NULL; |
2e1d1754 | 701 | enum ip_conntrack_info ctinfo = 0; |
a29a9a58 | 702 | struct nf_conn *ct = NULL; |
0597f268 | 703 | |
601e68e1 | 704 | if (li_user && li_user->type == NF_LOG_TYPE_ULOG) |
0597f268 HW |
705 | li = li_user; |
706 | else | |
707 | li = &default_loginfo; | |
708 | ||
356e2adb | 709 | inst = instance_lookup_get_rcu(log, li->u.ulog.group); |
0597f268 | 710 | if (!inst) |
0597f268 | 711 | return; |
0597f268 | 712 | |
d7a5c324 | 713 | if (prefix) |
881dbfe8 | 714 | plen = strlen(prefix) + 1; |
d7a5c324 | 715 | |
0597f268 HW |
716 | /* FIXME: do we want to make the size calculation conditional based on |
717 | * what is actually present? way more branches and checks, but more | |
718 | * memory efficient... */ | |
7e59b3fe | 719 | size = nlmsg_total_size(sizeof(struct nfgenmsg)) |
df6fb868 PM |
720 | + nla_total_size(sizeof(struct nfulnl_msg_packet_hdr)) |
721 | + nla_total_size(sizeof(u_int32_t)) /* ifindex */ | |
722 | + nla_total_size(sizeof(u_int32_t)) /* ifindex */ | |
1109a90c | 723 | #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER) |
df6fb868 PM |
724 | + nla_total_size(sizeof(u_int32_t)) /* ifindex */ |
725 | + nla_total_size(sizeof(u_int32_t)) /* ifindex */ | |
fbcd923c | 726 | #endif |
df6fb868 PM |
727 | + nla_total_size(sizeof(u_int32_t)) /* mark */ |
728 | + nla_total_size(sizeof(u_int32_t)) /* uid */ | |
76aa1ce1 | 729 | + nla_total_size(sizeof(u_int32_t)) /* gid */ |
df6fb868 PM |
730 | + nla_total_size(plen) /* prefix */ |
731 | + nla_total_size(sizeof(struct nfulnl_msg_packet_hw)) | |
9dfa1dfe FW |
732 | + nla_total_size(sizeof(struct nfulnl_msg_packet_timestamp)) |
733 | + nla_total_size(sizeof(struct nfgenmsg)); /* NLMSG_DONE */ | |
0597f268 | 734 | |
eeff9bee | 735 | if (in && skb_mac_header_was_set(skb)) { |
7e59b3fe | 736 | size += nla_total_size(skb->dev->hard_header_len) |
eeff9bee PNA |
737 | + nla_total_size(sizeof(u_int16_t)) /* hwtype */ |
738 | + nla_total_size(sizeof(u_int16_t)); /* hwlen */ | |
739 | } | |
740 | ||
0597f268 HW |
741 | spin_lock_bh(&inst->lock); |
742 | ||
0af5f6c1 | 743 | if (inst->flags & NFULNL_CFG_F_SEQ) |
df6fb868 | 744 | size += nla_total_size(sizeof(u_int32_t)); |
0af5f6c1 | 745 | if (inst->flags & NFULNL_CFG_F_SEQ_GLOBAL) |
df6fb868 | 746 | size += nla_total_size(sizeof(u_int32_t)); |
83ace77f | 747 | #if IS_ENABLED(CONFIG_NF_CONNTRACK) |
a29a9a58 KM |
748 | if (inst->flags & NFULNL_CFG_F_CONNTRACK) { |
749 | nfnl_ct = rcu_dereference(nfnl_ct_hook); | |
750 | if (nfnl_ct != NULL) { | |
83ace77f | 751 | ct = nf_ct_get(skb, &ctinfo); |
a29a9a58 KM |
752 | if (ct != NULL) |
753 | size += nfnl_ct->build_size(ct); | |
754 | } | |
755 | } | |
83ace77f | 756 | #endif |
65af4a10 MB |
757 | if (pf == NFPROTO_NETDEV || pf == NFPROTO_BRIDGE) |
758 | size += nfulnl_get_bridge_size(skb); | |
0af5f6c1 | 759 | |
0597f268 HW |
760 | qthreshold = inst->qthreshold; |
761 | /* per-rule qthreshold overrides per-instance */ | |
5ca431f9 EL |
762 | if (li->u.ulog.qthreshold) |
763 | if (qthreshold > li->u.ulog.qthreshold) | |
764 | qthreshold = li->u.ulog.qthreshold; | |
765 | ||
601e68e1 | 766 | |
0597f268 HW |
767 | switch (inst->copy_mode) { |
768 | case NFULNL_COPY_META: | |
769 | case NFULNL_COPY_NONE: | |
770 | data_len = 0; | |
771 | break; | |
601e68e1 | 772 | |
0597f268 | 773 | case NFULNL_COPY_PACKET: |
7643507f VP |
774 | data_len = inst->copy_range; |
775 | if ((li->u.ulog.flags & NF_LOG_F_COPY_LEN) && | |
776 | (li->u.ulog.copy_len < data_len)) | |
777 | data_len = li->u.ulog.copy_len; | |
778 | ||
779 | if (data_len > skb->len) | |
0597f268 | 780 | data_len = skb->len; |
601e68e1 | 781 | |
df6fb868 | 782 | size += nla_total_size(data_len); |
0597f268 | 783 | break; |
601e68e1 | 784 | |
f5c5440d | 785 | case NFULNL_COPY_DISABLED: |
0597f268 | 786 | default: |
55b5a91e | 787 | goto unlock_and_release; |
0597f268 HW |
788 | } |
789 | ||
9dfa1dfe | 790 | if (inst->skb && size > skb_tailroom(inst->skb)) { |
0597f268 HW |
791 | /* either the queue len is too high or we don't have |
792 | * enough room in the skb left. flush to userspace. */ | |
e3567061 | 793 | __nfulnl_flush(inst); |
55b5a91e | 794 | } |
0597f268 | 795 | |
55b5a91e | 796 | if (!inst->skb) { |
afff14f6 G |
797 | inst->skb = nfulnl_alloc_skb(net, inst->peer_portid, |
798 | inst->nlbufsiz, size); | |
55b5a91e | 799 | if (!inst->skb) |
0597f268 | 800 | goto alloc_failure; |
0597f268 HW |
801 | } |
802 | ||
0597f268 HW |
803 | inst->qlen++; |
804 | ||
9368a53c | 805 | __build_packet_message(log, inst, skb, data_len, pf, |
a29a9a58 KM |
806 | hooknum, in, out, prefix, plen, |
807 | nfnl_ct, ct, ctinfo); | |
0597f268 | 808 | |
d63b043d MM |
809 | if (inst->qlen >= qthreshold) |
810 | __nfulnl_flush(inst); | |
0597f268 HW |
811 | /* timer_pending always called within inst->lock, so there |
812 | * is no chance of a race here */ | |
d63b043d | 813 | else if (!timer_pending(&inst->timer)) { |
0597f268 HW |
814 | instance_get(inst); |
815 | inst->timer.expires = jiffies + (inst->flushtimeout*HZ/100); | |
816 | add_timer(&inst->timer); | |
817 | } | |
0597f268 | 818 | |
ed32abea MM |
819 | unlock_and_release: |
820 | spin_unlock_bh(&inst->lock); | |
821 | instance_put(inst); | |
0597f268 HW |
822 | return; |
823 | ||
824 | alloc_failure: | |
0597f268 | 825 | /* FIXME: statistics */ |
ed32abea | 826 | goto unlock_and_release; |
0597f268 HW |
827 | } |
828 | ||
829 | static int | |
830 | nfulnl_rcv_nl_event(struct notifier_block *this, | |
831 | unsigned long event, void *ptr) | |
832 | { | |
833 | struct netlink_notify *n = ptr; | |
9368a53c | 834 | struct nfnl_log_net *log = nfnl_log_pernet(n->net); |
0597f268 | 835 | |
dee5817e | 836 | if (event == NETLINK_URELEASE && n->protocol == NETLINK_NETFILTER) { |
0597f268 HW |
837 | int i; |
838 | ||
15e47304 | 839 | /* destroy all instances for this portid */ |
9368a53c | 840 | spin_lock_bh(&log->instances_lock); |
0597f268 | 841 | for (i = 0; i < INSTANCE_BUCKETS; i++) { |
b67bfe0d | 842 | struct hlist_node *t2; |
0597f268 | 843 | struct nfulnl_instance *inst; |
9368a53c | 844 | struct hlist_head *head = &log->instance_table[i]; |
0597f268 | 845 | |
b67bfe0d | 846 | hlist_for_each_entry_safe(inst, t2, head, hlist) { |
9368a53c | 847 | if (n->portid == inst->peer_portid) |
0597f268 HW |
848 | __instance_destroy(inst); |
849 | } | |
850 | } | |
9368a53c | 851 | spin_unlock_bh(&log->instances_lock); |
0597f268 HW |
852 | } |
853 | return NOTIFY_DONE; | |
854 | } | |
855 | ||
856 | static struct notifier_block nfulnl_rtnl_notifier = { | |
857 | .notifier_call = nfulnl_rcv_nl_event, | |
858 | }; | |
859 | ||
a6555365 PNA |
860 | static int nfulnl_recv_unsupp(struct sk_buff *skb, const struct nfnl_info *info, |
861 | const struct nlattr * const nfula[]) | |
0597f268 HW |
862 | { |
863 | return -ENOTSUPP; | |
864 | } | |
865 | ||
ca735b3a | 866 | static struct nf_logger nfulnl_logger __read_mostly = { |
0597f268 | 867 | .name = "nfnetlink_log", |
5962815a | 868 | .type = NF_LOG_TYPE_ULOG, |
d4ef3835 | 869 | .logfn = nfulnl_log_packet, |
0597f268 HW |
870 | .me = THIS_MODULE, |
871 | }; | |
872 | ||
fd8281ad PM |
873 | static const struct nla_policy nfula_cfg_policy[NFULA_CFG_MAX+1] = { |
874 | [NFULA_CFG_CMD] = { .len = sizeof(struct nfulnl_msg_config_cmd) }, | |
875 | [NFULA_CFG_MODE] = { .len = sizeof(struct nfulnl_msg_config_mode) }, | |
876 | [NFULA_CFG_TIMEOUT] = { .type = NLA_U32 }, | |
877 | [NFULA_CFG_QTHRESH] = { .type = NLA_U32 }, | |
878 | [NFULA_CFG_NLBUFSIZ] = { .type = NLA_U32 }, | |
879 | [NFULA_CFG_FLAGS] = { .type = NLA_U16 }, | |
0597f268 HW |
880 | }; |
881 | ||
a6555365 PNA |
882 | static int nfulnl_recv_config(struct sk_buff *skb, const struct nfnl_info *info, |
883 | const struct nlattr * const nfula[]) | |
0597f268 | 884 | { |
a6555365 | 885 | struct nfnl_log_net *log = nfnl_log_pernet(info->net); |
ef4b65e5 | 886 | u_int16_t group_num = ntohs(info->nfmsg->res_id); |
b7047a1c | 887 | struct nfulnl_msg_config_cmd *cmd = NULL; |
a6555365 | 888 | struct nfulnl_instance *inst; |
c872a2d9 | 889 | u16 flags = 0; |
a6555365 | 890 | int ret = 0; |
0597f268 | 891 | |
b7047a1c | 892 | if (nfula[NFULA_CFG_CMD]) { |
ef4b65e5 | 893 | u_int8_t pf = info->nfmsg->nfgen_family; |
b7047a1c PM |
894 | cmd = nla_data(nfula[NFULA_CFG_CMD]); |
895 | ||
896 | /* Commands without queue context */ | |
897 | switch (cmd->command) { | |
898 | case NFULNL_CFG_CMD_PF_BIND: | |
a6555365 | 899 | return nf_log_bind_pf(info->net, pf, &nfulnl_logger); |
b7047a1c | 900 | case NFULNL_CFG_CMD_PF_UNBIND: |
a6555365 | 901 | nf_log_unbind_pf(info->net, pf); |
b7047a1c PM |
902 | return 0; |
903 | } | |
904 | } | |
905 | ||
9368a53c | 906 | inst = instance_lookup_get(log, group_num); |
15e47304 | 907 | if (inst && inst->peer_portid != NETLINK_CB(skb).portid) { |
c0506365 PM |
908 | ret = -EPERM; |
909 | goto out_put; | |
910 | } | |
911 | ||
8cbc8708 PN |
912 | /* Check if we support these flags in first place, dependencies should |
913 | * be there too not to break atomicity. | |
914 | */ | |
915 | if (nfula[NFULA_CFG_FLAGS]) { | |
916 | flags = ntohs(nla_get_be16(nfula[NFULA_CFG_FLAGS])); | |
917 | ||
918 | if ((flags & NFULNL_CFG_F_CONNTRACK) && | |
919 | !rcu_access_pointer(nfnl_ct_hook)) { | |
920 | #ifdef CONFIG_MODULES | |
921 | nfnl_unlock(NFNL_SUBSYS_ULOG); | |
922 | request_module("ip_conntrack_netlink"); | |
923 | nfnl_lock(NFNL_SUBSYS_ULOG); | |
924 | if (rcu_access_pointer(nfnl_ct_hook)) { | |
925 | ret = -EAGAIN; | |
926 | goto out_put; | |
927 | } | |
928 | #endif | |
929 | ret = -EOPNOTSUPP; | |
930 | goto out_put; | |
931 | } | |
932 | } | |
933 | ||
b7047a1c | 934 | if (cmd != NULL) { |
0597f268 HW |
935 | switch (cmd->command) { |
936 | case NFULNL_CFG_CMD_BIND: | |
937 | if (inst) { | |
938 | ret = -EBUSY; | |
939 | goto out_put; | |
940 | } | |
941 | ||
a6555365 | 942 | inst = instance_create(info->net, group_num, |
15e47304 | 943 | NETLINK_CB(skb).portid, |
e32123e5 | 944 | sk_user_ns(NETLINK_CB(skb).sk)); |
baab2ce7 PM |
945 | if (IS_ERR(inst)) { |
946 | ret = PTR_ERR(inst); | |
f414c16c | 947 | goto out; |
0597f268 HW |
948 | } |
949 | break; | |
950 | case NFULNL_CFG_CMD_UNBIND: | |
951 | if (!inst) { | |
952 | ret = -ENODEV; | |
f414c16c | 953 | goto out; |
0597f268 HW |
954 | } |
955 | ||
9368a53c | 956 | instance_destroy(log, inst); |
a49c6503 | 957 | goto out_put; |
0597f268 | 958 | default: |
cd21f0ac | 959 | ret = -ENOTSUPP; |
eb075954 | 960 | goto out_put; |
0597f268 | 961 | } |
336a3b3e PNA |
962 | } else if (!inst) { |
963 | ret = -ENODEV; | |
964 | goto out; | |
0597f268 HW |
965 | } |
966 | ||
df6fb868 | 967 | if (nfula[NFULA_CFG_MODE]) { |
336a3b3e PNA |
968 | struct nfulnl_msg_config_mode *params = |
969 | nla_data(nfula[NFULA_CFG_MODE]); | |
0597f268 HW |
970 | |
971 | nfulnl_set_mode(inst, params->copy_mode, | |
d1208b99 | 972 | ntohl(params->copy_range)); |
0597f268 HW |
973 | } |
974 | ||
df6fb868 | 975 | if (nfula[NFULA_CFG_TIMEOUT]) { |
0dfedd28 | 976 | __be32 timeout = nla_get_be32(nfula[NFULA_CFG_TIMEOUT]); |
0597f268 HW |
977 | |
978 | nfulnl_set_timeout(inst, ntohl(timeout)); | |
979 | } | |
980 | ||
df6fb868 | 981 | if (nfula[NFULA_CFG_NLBUFSIZ]) { |
0dfedd28 | 982 | __be32 nlbufsiz = nla_get_be32(nfula[NFULA_CFG_NLBUFSIZ]); |
0597f268 HW |
983 | |
984 | nfulnl_set_nlbufsiz(inst, ntohl(nlbufsiz)); | |
985 | } | |
986 | ||
df6fb868 | 987 | if (nfula[NFULA_CFG_QTHRESH]) { |
0dfedd28 | 988 | __be32 qthresh = nla_get_be32(nfula[NFULA_CFG_QTHRESH]); |
0597f268 HW |
989 | |
990 | nfulnl_set_qthresh(inst, ntohl(qthresh)); | |
991 | } | |
992 | ||
8cbc8708 | 993 | if (nfula[NFULA_CFG_FLAGS]) |
a29a9a58 | 994 | nfulnl_set_flags(inst, flags); |
0af5f6c1 | 995 | |
0597f268 HW |
996 | out_put: |
997 | instance_put(inst); | |
dd16704e | 998 | out: |
0597f268 HW |
999 | return ret; |
1000 | } | |
1001 | ||
7c8d4cb4 | 1002 | static const struct nfnl_callback nfulnl_cb[NFULNL_MSG_MAX] = { |
50f2db9e PNA |
1003 | [NFULNL_MSG_PACKET] = { |
1004 | .call = nfulnl_recv_unsupp, | |
1005 | .type = NFNL_CB_MUTEX, | |
1006 | .attr_count = NFULA_MAX, | |
1007 | }, | |
1008 | [NFULNL_MSG_CONFIG] = { | |
1009 | .call = nfulnl_recv_config, | |
1010 | .type = NFNL_CB_MUTEX, | |
1011 | .attr_count = NFULA_CFG_MAX, | |
1012 | .policy = nfula_cfg_policy | |
1013 | }, | |
0597f268 HW |
1014 | }; |
1015 | ||
7c8d4cb4 | 1016 | static const struct nfnetlink_subsystem nfulnl_subsys = { |
0597f268 HW |
1017 | .name = "log", |
1018 | .subsys_id = NFNL_SUBSYS_ULOG, | |
1019 | .cb_count = NFULNL_MSG_MAX, | |
0597f268 HW |
1020 | .cb = nfulnl_cb, |
1021 | }; | |
1022 | ||
1023 | #ifdef CONFIG_PROC_FS | |
1024 | struct iter_state { | |
9368a53c | 1025 | struct seq_net_private p; |
0597f268 HW |
1026 | unsigned int bucket; |
1027 | }; | |
1028 | ||
9368a53c | 1029 | static struct hlist_node *get_first(struct net *net, struct iter_state *st) |
0597f268 | 1030 | { |
9368a53c | 1031 | struct nfnl_log_net *log; |
0597f268 HW |
1032 | if (!st) |
1033 | return NULL; | |
1034 | ||
9368a53c G |
1035 | log = nfnl_log_pernet(net); |
1036 | ||
0597f268 | 1037 | for (st->bucket = 0; st->bucket < INSTANCE_BUCKETS; st->bucket++) { |
9368a53c G |
1038 | struct hlist_head *head = &log->instance_table[st->bucket]; |
1039 | ||
1040 | if (!hlist_empty(head)) | |
356e2adb | 1041 | return rcu_dereference(hlist_first_rcu(head)); |
0597f268 HW |
1042 | } |
1043 | return NULL; | |
1044 | } | |
1045 | ||
9368a53c G |
1046 | static struct hlist_node *get_next(struct net *net, struct iter_state *st, |
1047 | struct hlist_node *h) | |
0597f268 | 1048 | { |
356e2adb | 1049 | h = rcu_dereference(hlist_next_rcu(h)); |
0597f268 | 1050 | while (!h) { |
9368a53c G |
1051 | struct nfnl_log_net *log; |
1052 | struct hlist_head *head; | |
1053 | ||
0597f268 HW |
1054 | if (++st->bucket >= INSTANCE_BUCKETS) |
1055 | return NULL; | |
1056 | ||
9368a53c G |
1057 | log = nfnl_log_pernet(net); |
1058 | head = &log->instance_table[st->bucket]; | |
356e2adb | 1059 | h = rcu_dereference(hlist_first_rcu(head)); |
0597f268 HW |
1060 | } |
1061 | return h; | |
1062 | } | |
1063 | ||
9368a53c G |
1064 | static struct hlist_node *get_idx(struct net *net, struct iter_state *st, |
1065 | loff_t pos) | |
0597f268 HW |
1066 | { |
1067 | struct hlist_node *head; | |
9368a53c | 1068 | head = get_first(net, st); |
0597f268 HW |
1069 | |
1070 | if (head) | |
9368a53c | 1071 | while (pos && (head = get_next(net, st, head))) |
0597f268 HW |
1072 | pos--; |
1073 | return pos ? NULL : head; | |
1074 | } | |
1075 | ||
9368a53c | 1076 | static void *seq_start(struct seq_file *s, loff_t *pos) |
356e2adb | 1077 | __acquires(rcu) |
0597f268 | 1078 | { |
356e2adb | 1079 | rcu_read_lock(); |
9368a53c | 1080 | return get_idx(seq_file_net(s), s->private, *pos); |
0597f268 HW |
1081 | } |
1082 | ||
1083 | static void *seq_next(struct seq_file *s, void *v, loff_t *pos) | |
1084 | { | |
1085 | (*pos)++; | |
9368a53c | 1086 | return get_next(seq_file_net(s), s->private, v); |
0597f268 HW |
1087 | } |
1088 | ||
1089 | static void seq_stop(struct seq_file *s, void *v) | |
356e2adb | 1090 | __releases(rcu) |
0597f268 | 1091 | { |
356e2adb | 1092 | rcu_read_unlock(); |
0597f268 HW |
1093 | } |
1094 | ||
1095 | static int seq_show(struct seq_file *s, void *v) | |
1096 | { | |
1097 | const struct nfulnl_instance *inst = v; | |
1098 | ||
20a1d165 | 1099 | seq_printf(s, "%5u %6u %5u %1u %5u %6u %2u\n", |
1ca9e417 JP |
1100 | inst->group_num, |
1101 | inst->peer_portid, inst->qlen, | |
1102 | inst->copy_mode, inst->copy_range, | |
b54ab92b | 1103 | inst->flushtimeout, refcount_read(&inst->use)); |
1ca9e417 JP |
1104 | |
1105 | return 0; | |
0597f268 HW |
1106 | } |
1107 | ||
56b3d975 | 1108 | static const struct seq_operations nful_seq_ops = { |
0597f268 HW |
1109 | .start = seq_start, |
1110 | .next = seq_next, | |
1111 | .stop = seq_stop, | |
1112 | .show = seq_show, | |
1113 | }; | |
0597f268 HW |
1114 | #endif /* PROC_FS */ |
1115 | ||
9368a53c | 1116 | static int __net_init nfnl_log_net_init(struct net *net) |
0597f268 | 1117 | { |
9368a53c G |
1118 | unsigned int i; |
1119 | struct nfnl_log_net *log = nfnl_log_pernet(net); | |
f13f2aee PW |
1120 | #ifdef CONFIG_PROC_FS |
1121 | struct proc_dir_entry *proc; | |
1122 | kuid_t root_uid; | |
1123 | kgid_t root_gid; | |
1124 | #endif | |
601e68e1 | 1125 | |
0597f268 | 1126 | for (i = 0; i < INSTANCE_BUCKETS; i++) |
9368a53c G |
1127 | INIT_HLIST_HEAD(&log->instance_table[i]); |
1128 | spin_lock_init(&log->instances_lock); | |
1129 | ||
1130 | #ifdef CONFIG_PROC_FS | |
c3506372 CH |
1131 | proc = proc_create_net("nfnetlink_log", 0440, net->nf.proc_netfilter, |
1132 | &nful_seq_ops, sizeof(struct iter_state)); | |
f13f2aee | 1133 | if (!proc) |
9368a53c | 1134 | return -ENOMEM; |
f13f2aee PW |
1135 | |
1136 | root_uid = make_kuid(net->user_ns, 0); | |
1137 | root_gid = make_kgid(net->user_ns, 0); | |
1138 | if (uid_valid(root_uid) && gid_valid(root_gid)) | |
1139 | proc_set_user(proc, root_uid, root_gid); | |
9368a53c G |
1140 | #endif |
1141 | return 0; | |
1142 | } | |
1143 | ||
1144 | static void __net_exit nfnl_log_net_exit(struct net *net) | |
1145 | { | |
613d0776 VA |
1146 | struct nfnl_log_net *log = nfnl_log_pernet(net); |
1147 | unsigned int i; | |
1148 | ||
e778f56e | 1149 | #ifdef CONFIG_PROC_FS |
9368a53c | 1150 | remove_proc_entry("nfnetlink_log", net->nf.proc_netfilter); |
e778f56e | 1151 | #endif |
45c2aff6 | 1152 | nf_log_unset(net, &nfulnl_logger); |
613d0776 VA |
1153 | for (i = 0; i < INSTANCE_BUCKETS; i++) |
1154 | WARN_ON_ONCE(!hlist_empty(&log->instance_table[i])); | |
9368a53c G |
1155 | } |
1156 | ||
1157 | static struct pernet_operations nfnl_log_net_ops = { | |
1158 | .init = nfnl_log_net_init, | |
1159 | .exit = nfnl_log_net_exit, | |
1160 | .id = &nfnl_log_net_id, | |
1161 | .size = sizeof(struct nfnl_log_net), | |
1162 | }; | |
1163 | ||
1164 | static int __init nfnetlink_log_init(void) | |
1165 | { | |
3bfe0498 FR |
1166 | int status; |
1167 | ||
1168 | status = register_pernet_subsys(&nfnl_log_net_ops); | |
1169 | if (status < 0) { | |
1170 | pr_err("failed to register pernet ops\n"); | |
1171 | goto out; | |
1172 | } | |
601e68e1 | 1173 | |
0597f268 HW |
1174 | netlink_register_notifier(&nfulnl_rtnl_notifier); |
1175 | status = nfnetlink_subsys_register(&nfulnl_subsys); | |
1176 | if (status < 0) { | |
beacd3e8 | 1177 | pr_err("failed to create netlink socket\n"); |
0597f268 HW |
1178 | goto cleanup_netlink_notifier; |
1179 | } | |
1180 | ||
ca735b3a EL |
1181 | status = nf_log_register(NFPROTO_UNSPEC, &nfulnl_logger); |
1182 | if (status < 0) { | |
beacd3e8 | 1183 | pr_err("failed to register logger\n"); |
ca735b3a EL |
1184 | goto cleanup_subsys; |
1185 | } | |
1186 | ||
0597f268 HW |
1187 | return status; |
1188 | ||
0597f268 | 1189 | cleanup_subsys: |
0597f268 HW |
1190 | nfnetlink_subsys_unregister(&nfulnl_subsys); |
1191 | cleanup_netlink_notifier: | |
1192 | netlink_unregister_notifier(&nfulnl_rtnl_notifier); | |
3bfe0498 FR |
1193 | unregister_pernet_subsys(&nfnl_log_net_ops); |
1194 | out: | |
0597f268 HW |
1195 | return status; |
1196 | } | |
1197 | ||
65b4b4e8 | 1198 | static void __exit nfnetlink_log_fini(void) |
0597f268 | 1199 | { |
32292a7f PM |
1200 | nfnetlink_subsys_unregister(&nfulnl_subsys); |
1201 | netlink_unregister_notifier(&nfulnl_rtnl_notifier); | |
3bfe0498 | 1202 | unregister_pernet_subsys(&nfnl_log_net_ops); |
c83fa196 | 1203 | nf_log_unregister(&nfulnl_logger); |
0597f268 HW |
1204 | } |
1205 | ||
1206 | MODULE_DESCRIPTION("netfilter userspace logging"); | |
1207 | MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>"); | |
1208 | MODULE_LICENSE("GPL"); | |
f682faef | 1209 | MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_ULOG); |
fab4085f PNA |
1210 | MODULE_ALIAS_NF_LOGGER(AF_INET, 1); |
1211 | MODULE_ALIAS_NF_LOGGER(AF_INET6, 1); | |
1212 | MODULE_ALIAS_NF_LOGGER(AF_BRIDGE, 1); | |
2497b846 | 1213 | MODULE_ALIAS_NF_LOGGER(3, 1); /* NFPROTO_ARP */ |
a7647080 | 1214 | MODULE_ALIAS_NF_LOGGER(5, 1); /* NFPROTO_NETDEV */ |
0597f268 | 1215 | |
65b4b4e8 AM |
1216 | module_init(nfnetlink_log_init); |
1217 | module_exit(nfnetlink_log_fini); |