Blame view

net/netfilter/nfnetlink_log.c 28.4 KB
0597f2680   Harald Welte   [NETFILTER]: Add ...
1
2
3
4
5
  /*
   * This is a module which is used for logging packets to userspace via
   * nfetlink.
   *
   * (C) 2005 by Harald Welte <laforge@netfilter.org>
f229f6ce4   Patrick McHardy   netfilter: add my...
6
   * (C) 2006-2012 Patrick McHardy <kaber@trash.net>
0597f2680   Harald Welte   [NETFILTER]: Add ...
7
8
9
10
11
12
13
   *
   * Based on the old ipv4-only ipt_ULOG.c:
   * (C) 2000-2004 by Harald Welte <laforge@netfilter.org>
   *
   * This program is free software; you can redistribute it and/or modify
   * it under the terms of the GNU General Public License version 2 as
   * published by the Free Software Foundation.
0597f2680   Harald Welte   [NETFILTER]: Add ...
14
   */
beacd3e8e   Marcelo Leitner   netfilter: nfnetl...
15
16
  
  #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0597f2680   Harald Welte   [NETFILTER]: Add ...
17
18
  #include <linux/module.h>
  #include <linux/skbuff.h>
e035edd16   Pablo Neira Ayuso   netfilter: nfnetl...
19
  #include <linux/if_arp.h>
0597f2680   Harald Welte   [NETFILTER]: Add ...
20
21
22
23
24
  #include <linux/init.h>
  #include <linux/ip.h>
  #include <linux/ipv6.h>
  #include <linux/netdevice.h>
  #include <linux/netfilter.h>
c737b7c45   Florian Westphal   netfilter: bridge...
25
  #include <linux/netfilter_bridge.h>
573ce260b   Hong zhi guo   net-next: replace...
26
  #include <net/netlink.h>
0597f2680   Harald Welte   [NETFILTER]: Add ...
27
28
  #include <linux/netfilter/nfnetlink.h>
  #include <linux/netfilter/nfnetlink_log.h>
a29a9a585   Ken-ichirou MATSUZAWA   netfilter: nfnetl...
29
  #include <linux/netfilter/nf_conntrack_common.h>
0597f2680   Harald Welte   [NETFILTER]: Add ...
30
31
32
33
34
  #include <linux/spinlock.h>
  #include <linux/sysctl.h>
  #include <linux/proc_fs.h>
  #include <linux/security.h>
  #include <linux/list.h>
5a0e3ad6a   Tejun Heo   include cleanup: ...
35
  #include <linux/slab.h>
0597f2680   Harald Welte   [NETFILTER]: Add ...
36
  #include <net/sock.h>
f01ffbd6e   Patrick McHardy   [NETFILTER]: nf_l...
37
  #include <net/netfilter/nf_log.h>
9368a53c4   Gao feng   netfilter: nfnetl...
38
  #include <net/netns/generic.h>
d9e150071   Patrick McHardy   netfilter: nfnetl...
39
  #include <net/netfilter/nfnetlink_log.h>
0597f2680   Harald Welte   [NETFILTER]: Add ...
40

60063497a   Arun Sharma   atomic: use <linu...
41
  #include <linux/atomic.h>
b54ab92b8   Reshetova, Elena   netfilter: refcou...
42
  #include <linux/refcount.h>
0597f2680   Harald Welte   [NETFILTER]: Add ...
43

1109a90c0   Pablo Neira Ayuso   netfilter: use IS...
44
  #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
fbcd923c3   Harald Welte   [NETFILTER]: add ...
45
46
  #include "../bridge/br_private.h"
  #endif
c2db29243   Holger Eitzenberger   [NETFILTER]: ULOG...
47
  #define NFULNL_NLBUFSIZ_DEFAULT	NLMSG_GOODSIZE
2c6764b74   Eric Leblond   netfilter: nfnetl...
48
  #define NFULNL_TIMEOUT_DEFAULT 	100	/* every second */
0597f2680   Harald Welte   [NETFILTER]: Add ...
49
  #define NFULNL_QTHRESH_DEFAULT 	100	/* 100 packets */
c1e7dc91e   Florian Westphal   netfilter: nfnetl...
50
51
  /* max packet size is limited by 16-bit struct nfattr nfa_len field */
  #define NFULNL_COPY_RANGE_MAX	(0xFFFF - NLA_HDRLEN)
0597f2680   Harald Welte   [NETFILTER]: Add ...
52
53
54
  
  #define PRINTR(x, args...)	do { if (net_ratelimit()) \
  				     printk(x, ## args); } while (0);
0597f2680   Harald Welte   [NETFILTER]: Add ...
55
56
57
  struct nfulnl_instance {
  	struct hlist_node hlist;	/* global list of instances */
  	spinlock_t lock;
b54ab92b8   Reshetova, Elena   netfilter: refcou...
58
  	refcount_t use;			/* use count */
0597f2680   Harald Welte   [NETFILTER]: Add ...
59
60
61
  
  	unsigned int qlen;		/* number of nlmsgs in skb */
  	struct sk_buff *skb;		/* pre-allocatd skb */
0597f2680   Harald Welte   [NETFILTER]: Add ...
62
  	struct timer_list timer;
9368a53c4   Gao feng   netfilter: nfnetl...
63
  	struct net *net;
9eea9515c   Eric W. Biederman   userns: nfnetlink...
64
  	struct user_namespace *peer_user_ns;	/* User namespace of the peer process */
cc6bc4486   Richard Weinberger   netfilter: Fix po...
65
  	u32 peer_portid;		/* PORTID of the peer process */
0597f2680   Harald Welte   [NETFILTER]: Add ...
66
67
68
69
70
71
  
  	/* configurable parameters */
  	unsigned int flushtimeout;	/* timeout until queue flush */
  	unsigned int nlbufsiz;		/* netlink buffer allocation size */
  	unsigned int qthreshold;	/* threshold of the queue */
  	u_int32_t copy_range;
0af5f6c1e   Harald Welte   [NETFILTER] nfnet...
72
  	u_int32_t seq;			/* instance-local sequential counter */
0597f2680   Harald Welte   [NETFILTER]: Add ...
73
  	u_int16_t group_num;		/* number of this queue */
0af5f6c1e   Harald Welte   [NETFILTER] nfnet...
74
  	u_int16_t flags;
601e68e10   YOSHIFUJI Hideaki   [NETFILTER]: Fix ...
75
  	u_int8_t copy_mode;
bed1be208   Eric Dumazet   netfilter: nfnetl...
76
  	struct rcu_head rcu;
0597f2680   Harald Welte   [NETFILTER]: Add ...
77
  };
0597f2680   Harald Welte   [NETFILTER]: Add ...
78
  #define INSTANCE_BUCKETS	16
0597f2680   Harald Welte   [NETFILTER]: Add ...
79

c7d03a00b   Alexey Dobriyan   netns: make struc...
80
  static unsigned int nfnl_log_net_id __read_mostly;
9368a53c4   Gao feng   netfilter: nfnetl...
81
82
83
84
85
86
87
88
89
90
91
  
  struct nfnl_log_net {
  	spinlock_t instances_lock;
  	struct hlist_head instance_table[INSTANCE_BUCKETS];
  	atomic_t global_seq;
  };
  
  static struct nfnl_log_net *nfnl_log_pernet(struct net *net)
  {
  	return net_generic(net, nfnl_log_net_id);
  }
0597f2680   Harald Welte   [NETFILTER]: Add ...
92
93
94
95
96
97
  static inline u_int8_t instance_hashfn(u_int16_t group_num)
  {
  	return ((group_num & 0xff) % INSTANCE_BUCKETS);
  }
  
  static struct nfulnl_instance *
9368a53c4   Gao feng   netfilter: nfnetl...
98
  __instance_lookup(struct nfnl_log_net *log, u_int16_t group_num)
0597f2680   Harald Welte   [NETFILTER]: Add ...
99
100
  {
  	struct hlist_head *head;
0597f2680   Harald Welte   [NETFILTER]: Add ...
101
  	struct nfulnl_instance *inst;
9368a53c4   Gao feng   netfilter: nfnetl...
102
  	head = &log->instance_table[instance_hashfn(group_num)];
b67bfe0d4   Sasha Levin   hlist: drop the n...
103
  	hlist_for_each_entry_rcu(inst, head, hlist) {
0597f2680   Harald Welte   [NETFILTER]: Add ...
104
105
106
107
108
109
110
111
112
  		if (inst->group_num == group_num)
  			return inst;
  	}
  	return NULL;
  }
  
  static inline void
  instance_get(struct nfulnl_instance *inst)
  {
b54ab92b8   Reshetova, Elena   netfilter: refcou...
113
  	refcount_inc(&inst->use);
0597f2680   Harald Welte   [NETFILTER]: Add ...
114
115
116
  }
  
  static struct nfulnl_instance *
9368a53c4   Gao feng   netfilter: nfnetl...
117
  instance_lookup_get(struct nfnl_log_net *log, u_int16_t group_num)
0597f2680   Harald Welte   [NETFILTER]: Add ...
118
119
  {
  	struct nfulnl_instance *inst;
bed1be208   Eric Dumazet   netfilter: nfnetl...
120
  	rcu_read_lock_bh();
9368a53c4   Gao feng   netfilter: nfnetl...
121
  	inst = __instance_lookup(log, group_num);
b54ab92b8   Reshetova, Elena   netfilter: refcou...
122
  	if (inst && !refcount_inc_not_zero(&inst->use))
f5c5440d4   Eric Dumazet   netfilter: nfnetl...
123
  		inst = NULL;
bed1be208   Eric Dumazet   netfilter: nfnetl...
124
  	rcu_read_unlock_bh();
0597f2680   Harald Welte   [NETFILTER]: Add ...
125
126
127
  
  	return inst;
  }
bed1be208   Eric Dumazet   netfilter: nfnetl...
128
129
  static void nfulnl_instance_free_rcu(struct rcu_head *head)
  {
9368a53c4   Gao feng   netfilter: nfnetl...
130
131
132
133
134
  	struct nfulnl_instance *inst =
  		container_of(head, struct nfulnl_instance, rcu);
  
  	put_net(inst->net);
  	kfree(inst);
bed1be208   Eric Dumazet   netfilter: nfnetl...
135
136
  	module_put(THIS_MODULE);
  }
0597f2680   Harald Welte   [NETFILTER]: Add ...
137
138
139
  static void
  instance_put(struct nfulnl_instance *inst)
  {
b54ab92b8   Reshetova, Elena   netfilter: refcou...
140
  	if (inst && refcount_dec_and_test(&inst->use))
bed1be208   Eric Dumazet   netfilter: nfnetl...
141
  		call_rcu_bh(&inst->rcu, nfulnl_instance_free_rcu);
0597f2680   Harald Welte   [NETFILTER]: Add ...
142
143
144
145
146
  }
  
  static void nfulnl_timer(unsigned long data);
  
  static struct nfulnl_instance *
9368a53c4   Gao feng   netfilter: nfnetl...
147
  instance_create(struct net *net, u_int16_t group_num,
cc6bc4486   Richard Weinberger   netfilter: Fix po...
148
  		u32 portid, struct user_namespace *user_ns)
0597f2680   Harald Welte   [NETFILTER]: Add ...
149
150
  {
  	struct nfulnl_instance *inst;
9368a53c4   Gao feng   netfilter: nfnetl...
151
  	struct nfnl_log_net *log = nfnl_log_pernet(net);
baab2ce7d   Patrick McHardy   [NETFILTER]: nfne...
152
  	int err;
0597f2680   Harald Welte   [NETFILTER]: Add ...
153

9368a53c4   Gao feng   netfilter: nfnetl...
154
155
  	spin_lock_bh(&log->instances_lock);
  	if (__instance_lookup(log, group_num)) {
baab2ce7d   Patrick McHardy   [NETFILTER]: nfne...
156
  		err = -EEXIST;
0597f2680   Harald Welte   [NETFILTER]: Add ...
157
158
  		goto out_unlock;
  	}
10dfdc69e   Harald Welte   [NETFILTER] nfnet...
159
  	inst = kzalloc(sizeof(*inst), GFP_ATOMIC);
baab2ce7d   Patrick McHardy   [NETFILTER]: nfne...
160
161
  	if (!inst) {
  		err = -ENOMEM;
0597f2680   Harald Welte   [NETFILTER]: Add ...
162
  		goto out_unlock;
baab2ce7d   Patrick McHardy   [NETFILTER]: nfne...
163
  	}
0597f2680   Harald Welte   [NETFILTER]: Add ...
164

aace57e05   Michal Miroslaw   [NETFILTER]: nfne...
165
166
  	if (!try_module_get(THIS_MODULE)) {
  		kfree(inst);
baab2ce7d   Patrick McHardy   [NETFILTER]: nfne...
167
  		err = -EAGAIN;
aace57e05   Michal Miroslaw   [NETFILTER]: nfne...
168
169
  		goto out_unlock;
  	}
0597f2680   Harald Welte   [NETFILTER]: Add ...
170
  	INIT_HLIST_NODE(&inst->hlist);
181a46a56   YOSHIFUJI Hideaki   [NETFILTER]: Use ...
171
  	spin_lock_init(&inst->lock);
0597f2680   Harald Welte   [NETFILTER]: Add ...
172
  	/* needs to be two, since we _put() after creation */
b54ab92b8   Reshetova, Elena   netfilter: refcou...
173
  	refcount_set(&inst->use, 2);
0597f2680   Harald Welte   [NETFILTER]: Add ...
174

e6f689db5   Patrick McHardy   [NETFILTER]: Use ...
175
  	setup_timer(&inst->timer, nfulnl_timer, (unsigned long)inst);
0597f2680   Harald Welte   [NETFILTER]: Add ...
176

9368a53c4   Gao feng   netfilter: nfnetl...
177
  	inst->net = get_net(net);
9eea9515c   Eric W. Biederman   userns: nfnetlink...
178
  	inst->peer_user_ns = user_ns;
15e473046   Eric W. Biederman   netlink: Rename p...
179
  	inst->peer_portid = portid;
0597f2680   Harald Welte   [NETFILTER]: Add ...
180
181
182
183
184
185
  	inst->group_num = group_num;
  
  	inst->qthreshold 	= NFULNL_QTHRESH_DEFAULT;
  	inst->flushtimeout 	= NFULNL_TIMEOUT_DEFAULT;
  	inst->nlbufsiz 		= NFULNL_NLBUFSIZ_DEFAULT;
  	inst->copy_mode 	= NFULNL_COPY_PACKET;
6b6ec99a0   Michal Miroslaw   [NETFILTER]: nfne...
186
  	inst->copy_range 	= NFULNL_COPY_RANGE_MAX;
0597f2680   Harald Welte   [NETFILTER]: Add ...
187

f5c5440d4   Eric Dumazet   netfilter: nfnetl...
188
  	hlist_add_head_rcu(&inst->hlist,
9368a53c4   Gao feng   netfilter: nfnetl...
189
  		       &log->instance_table[instance_hashfn(group_num)]);
0597f2680   Harald Welte   [NETFILTER]: Add ...
190

9368a53c4   Gao feng   netfilter: nfnetl...
191
  	spin_unlock_bh(&log->instances_lock);
0597f2680   Harald Welte   [NETFILTER]: Add ...
192
193
  
  	return inst;
0597f2680   Harald Welte   [NETFILTER]: Add ...
194
  out_unlock:
9368a53c4   Gao feng   netfilter: nfnetl...
195
  	spin_unlock_bh(&log->instances_lock);
baab2ce7d   Patrick McHardy   [NETFILTER]: nfne...
196
  	return ERR_PTR(err);
0597f2680   Harald Welte   [NETFILTER]: Add ...
197
  }
e35670614   Michal Miroslaw   [NETFILTER]: nfne...
198
  static void __nfulnl_flush(struct nfulnl_instance *inst);
0597f2680   Harald Welte   [NETFILTER]: Add ...
199

f5c5440d4   Eric Dumazet   netfilter: nfnetl...
200
  /* called with BH disabled */
0597f2680   Harald Welte   [NETFILTER]: Add ...
201
  static void
9afdb00c8   Patrick McHardy   [NETFILTER]: nfne...
202
  __instance_destroy(struct nfulnl_instance *inst)
0597f2680   Harald Welte   [NETFILTER]: Add ...
203
204
  {
  	/* first pull it out of the global list */
f5c5440d4   Eric Dumazet   netfilter: nfnetl...
205
  	hlist_del_rcu(&inst->hlist);
0597f2680   Harald Welte   [NETFILTER]: Add ...
206

0597f2680   Harald Welte   [NETFILTER]: Add ...
207
  	/* then flush all pending packets from skb */
f5c5440d4   Eric Dumazet   netfilter: nfnetl...
208
209
210
211
  	spin_lock(&inst->lock);
  
  	/* lockless readers wont be able to use us */
  	inst->copy_mode = NFULNL_COPY_DISABLED;
e35670614   Michal Miroslaw   [NETFILTER]: nfne...
212
213
  	if (inst->skb)
  		__nfulnl_flush(inst);
f5c5440d4   Eric Dumazet   netfilter: nfnetl...
214
  	spin_unlock(&inst->lock);
0597f2680   Harald Welte   [NETFILTER]: Add ...
215
216
217
  
  	/* and finally put the refcount */
  	instance_put(inst);
0597f2680   Harald Welte   [NETFILTER]: Add ...
218
219
220
  }
  
  static inline void
9368a53c4   Gao feng   netfilter: nfnetl...
221
222
  instance_destroy(struct nfnl_log_net *log,
  		 struct nfulnl_instance *inst)
0597f2680   Harald Welte   [NETFILTER]: Add ...
223
  {
9368a53c4   Gao feng   netfilter: nfnetl...
224
  	spin_lock_bh(&log->instances_lock);
9afdb00c8   Patrick McHardy   [NETFILTER]: nfne...
225
  	__instance_destroy(inst);
9368a53c4   Gao feng   netfilter: nfnetl...
226
  	spin_unlock_bh(&log->instances_lock);
0597f2680   Harald Welte   [NETFILTER]: Add ...
227
228
229
230
231
232
233
234
235
  }
  
  static int
  nfulnl_set_mode(struct nfulnl_instance *inst, u_int8_t mode,
  		  unsigned int range)
  {
  	int status = 0;
  
  	spin_lock_bh(&inst->lock);
601e68e10   YOSHIFUJI Hideaki   [NETFILTER]: Fix ...
236

0597f2680   Harald Welte   [NETFILTER]: Add ...
237
238
239
240
241
242
  	switch (mode) {
  	case NFULNL_COPY_NONE:
  	case NFULNL_COPY_META:
  		inst->copy_mode = mode;
  		inst->copy_range = 0;
  		break;
601e68e10   YOSHIFUJI Hideaki   [NETFILTER]: Fix ...
243

0597f2680   Harald Welte   [NETFILTER]: Add ...
244
245
  	case NFULNL_COPY_PACKET:
  		inst->copy_mode = mode;
c1e7dc91e   Florian Westphal   netfilter: nfnetl...
246
247
  		if (range == 0)
  			range = NFULNL_COPY_RANGE_MAX;
6b6ec99a0   Michal Miroslaw   [NETFILTER]: nfne...
248
249
  		inst->copy_range = min_t(unsigned int,
  					 range, NFULNL_COPY_RANGE_MAX);
0597f2680   Harald Welte   [NETFILTER]: Add ...
250
  		break;
601e68e10   YOSHIFUJI Hideaki   [NETFILTER]: Fix ...
251

0597f2680   Harald Welte   [NETFILTER]: Add ...
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
  	default:
  		status = -EINVAL;
  		break;
  	}
  
  	spin_unlock_bh(&inst->lock);
  
  	return status;
  }
  
  static int
  nfulnl_set_nlbufsiz(struct nfulnl_instance *inst, u_int32_t nlbufsiz)
  {
  	int status;
  
  	spin_lock_bh(&inst->lock);
  	if (nlbufsiz < NFULNL_NLBUFSIZ_DEFAULT)
  		status = -ERANGE;
  	else if (nlbufsiz > 131072)
  		status = -ERANGE;
  	else {
  		inst->nlbufsiz = nlbufsiz;
  		status = 0;
  	}
  	spin_unlock_bh(&inst->lock);
  
  	return status;
  }
23509fcd4   Rosen, Rami   netfilter: nfnetl...
280
  static void
0597f2680   Harald Welte   [NETFILTER]: Add ...
281
282
283
284
285
  nfulnl_set_timeout(struct nfulnl_instance *inst, u_int32_t timeout)
  {
  	spin_lock_bh(&inst->lock);
  	inst->flushtimeout = timeout;
  	spin_unlock_bh(&inst->lock);
0597f2680   Harald Welte   [NETFILTER]: Add ...
286
  }
23509fcd4   Rosen, Rami   netfilter: nfnetl...
287
  static void
0597f2680   Harald Welte   [NETFILTER]: Add ...
288
289
290
291
292
  nfulnl_set_qthresh(struct nfulnl_instance *inst, u_int32_t qthresh)
  {
  	spin_lock_bh(&inst->lock);
  	inst->qthreshold = qthresh;
  	spin_unlock_bh(&inst->lock);
0597f2680   Harald Welte   [NETFILTER]: Add ...
293
  }
0af5f6c1e   Harald Welte   [NETFILTER] nfnet...
294
295
296
297
  static int
  nfulnl_set_flags(struct nfulnl_instance *inst, u_int16_t flags)
  {
  	spin_lock_bh(&inst->lock);
ee433530d   Patrick McHardy   [NETFILTER]: nfne...
298
  	inst->flags = flags;
0af5f6c1e   Harald Welte   [NETFILTER] nfnet...
299
300
301
302
  	spin_unlock_bh(&inst->lock);
  
  	return 0;
  }
c6a8f6483   Michal Miroslaw   [NETFILTER]: nfne...
303
  static struct sk_buff *
afff14f60   Gao feng   netfilter: nfnetl...
304
305
  nfulnl_alloc_skb(struct net *net, u32 peer_portid, unsigned int inst_size,
  		 unsigned int pkt_size)
0597f2680   Harald Welte   [NETFILTER]: Add ...
306
307
  {
  	struct sk_buff *skb;
ad2ad0f96   Patrick McHardy   [NETFILTER]: Fix ...
308
  	unsigned int n;
0597f2680   Harald Welte   [NETFILTER]: Add ...
309

0597f2680   Harald Welte   [NETFILTER]: Add ...
310
311
  	/* alloc skb which should be big enough for a whole multipart
  	 * message.  WARNING: has to be <= 128k due to slab restrictions */
ad2ad0f96   Patrick McHardy   [NETFILTER]: Fix ...
312
  	n = max(inst_size, pkt_size);
0813fbc91   Calvin Owens   netfilter: nfnetl...
313
  	skb = alloc_skb(n, GFP_ATOMIC | __GFP_NOWARN);
0597f2680   Harald Welte   [NETFILTER]: Add ...
314
  	if (!skb) {
ad2ad0f96   Patrick McHardy   [NETFILTER]: Fix ...
315
316
317
  		if (n > pkt_size) {
  			/* try to allocate only as much as we need for current
  			 * packet */
0597f2680   Harald Welte   [NETFILTER]: Add ...
318

905f0a739   Florian Westphal   nfnetlink: remove...
319
  			skb = alloc_skb(pkt_size, GFP_ATOMIC);
ad2ad0f96   Patrick McHardy   [NETFILTER]: Fix ...
320
  		}
0597f2680   Harald Welte   [NETFILTER]: Add ...
321
322
323
324
  	}
  
  	return skb;
  }
b51d3fa36   Houcheng Lin   netfilter: nf_log...
325
  static void
0597f2680   Harald Welte   [NETFILTER]: Add ...
326
327
  __nfulnl_send(struct nfulnl_instance *inst)
  {
d550d0958   David S. Miller   netfilter: nfnetl...
328
329
330
331
332
  	if (inst->qlen > 1) {
  		struct nlmsghdr *nlh = nlmsg_put(inst->skb, 0, 0,
  						 NLMSG_DONE,
  						 sizeof(struct nfgenmsg),
  						 0);
b51d3fa36   Houcheng Lin   netfilter: nf_log...
333
334
335
336
  		if (WARN_ONCE(!nlh, "bad nlskb size: %u, tailroom %d
  ",
  			      inst->skb->len, skb_tailroom(inst->skb))) {
  			kfree_skb(inst->skb);
d550d0958   David S. Miller   netfilter: nfnetl...
337
  			goto out;
b51d3fa36   Houcheng Lin   netfilter: nf_log...
338
  		}
d550d0958   David S. Miller   netfilter: nfnetl...
339
  	}
b51d3fa36   Houcheng Lin   netfilter: nf_log...
340
341
342
  	nfnetlink_unicast(inst->skb, inst->net, inst->peer_portid,
  			  MSG_DONTWAIT);
  out:
0597f2680   Harald Welte   [NETFILTER]: Add ...
343
344
  	inst->qlen = 0;
  	inst->skb = NULL;
0597f2680   Harald Welte   [NETFILTER]: Add ...
345
  }
e35670614   Michal Miroslaw   [NETFILTER]: nfne...
346
347
348
349
350
351
352
353
354
  static void
  __nfulnl_flush(struct nfulnl_instance *inst)
  {
  	/* timer holds a reference */
  	if (del_timer(&inst->timer))
  		instance_put(inst);
  	if (inst->skb)
  		__nfulnl_send(inst);
  }
c6a8f6483   Michal Miroslaw   [NETFILTER]: nfne...
355
356
  static void
  nfulnl_timer(unsigned long data)
0597f2680   Harald Welte   [NETFILTER]: Add ...
357
  {
601e68e10   YOSHIFUJI Hideaki   [NETFILTER]: Fix ...
358
  	struct nfulnl_instance *inst = (struct nfulnl_instance *)data;
0597f2680   Harald Welte   [NETFILTER]: Add ...
359

0597f2680   Harald Welte   [NETFILTER]: Add ...
360
  	spin_lock_bh(&inst->lock);
370e6a878   Michal Miroslaw   [NETFILTER]: nfne...
361
362
  	if (inst->skb)
  		__nfulnl_send(inst);
0597f2680   Harald Welte   [NETFILTER]: Add ...
363
  	spin_unlock_bh(&inst->lock);
05f7b7b36   Michal Miroslaw   [NETFILTER]: nfne...
364
  	instance_put(inst);
0597f2680   Harald Welte   [NETFILTER]: Add ...
365
  }
0af5f6c1e   Harald Welte   [NETFILTER] nfnet...
366
367
  /* This is an inline function, we don't really care about a long
   * list of arguments */
601e68e10   YOSHIFUJI Hideaki   [NETFILTER]: Fix ...
368
  static inline int
9368a53c4   Gao feng   netfilter: nfnetl...
369
370
  __build_packet_message(struct nfnl_log_net *log,
  			struct nfulnl_instance *inst,
601e68e10   YOSHIFUJI Hideaki   [NETFILTER]: Fix ...
371
  			const struct sk_buff *skb,
0597f2680   Harald Welte   [NETFILTER]: Add ...
372
  			unsigned int data_len,
76108cea0   Jan Engelhardt   netfilter: Use un...
373
  			u_int8_t pf,
0597f2680   Harald Welte   [NETFILTER]: Add ...
374
375
376
  			unsigned int hooknum,
  			const struct net_device *indev,
  			const struct net_device *outdev,
a29a9a585   Ken-ichirou MATSUZAWA   netfilter: nfnetl...
377
378
379
  			const char *prefix, unsigned int plen,
  			const struct nfnl_ct_hook *nfnl_ct,
  			struct nf_conn *ct, enum ip_conntrack_info ctinfo)
0597f2680   Harald Welte   [NETFILTER]: Add ...
380
  {
0597f2680   Harald Welte   [NETFILTER]: Add ...
381
382
383
  	struct nfulnl_msg_packet_hdr pmsg;
  	struct nlmsghdr *nlh;
  	struct nfgenmsg *nfmsg;
27a884dc3   Arnaldo Carvalho de Melo   [SK_BUFF]: Conver...
384
  	sk_buff_data_t old_tail = inst->skb->tail;
0626af313   Eric Dumazet   netfilter: take c...
385
  	struct sock *sk;
0c36b48b3   Bob Hockney   netfilter: nfnetl...
386
  	const unsigned char *hwhdrp;
0597f2680   Harald Welte   [NETFILTER]: Add ...
387

d550d0958   David S. Miller   netfilter: nfnetl...
388
  	nlh = nlmsg_put(inst->skb, 0, 0,
dedb67c4b   Pablo Neira Ayuso   netfilter: Add nf...
389
  			nfnl_msg_type(NFNL_SUBSYS_ULOG, NFULNL_MSG_PACKET),
d550d0958   David S. Miller   netfilter: nfnetl...
390
391
392
393
  			sizeof(struct nfgenmsg), 0);
  	if (!nlh)
  		return -1;
  	nfmsg = nlmsg_data(nlh);
0597f2680   Harald Welte   [NETFILTER]: Add ...
394
395
396
  	nfmsg->nfgen_family = pf;
  	nfmsg->version = NFNETLINK_V0;
  	nfmsg->res_id = htons(inst->group_num);
e4d091d7b   Dan Carpenter   netfilter: nfnetl...
397
  	memset(&pmsg, 0, sizeof(pmsg));
febf0a431   Al Viro   [NETFILTER] bug: ...
398
  	pmsg.hw_protocol	= skb->protocol;
0597f2680   Harald Welte   [NETFILTER]: Add ...
399
  	pmsg.hook		= hooknum;
1db20a529   David S. Miller   nfnetlink_log: St...
400
401
  	if (nla_put(inst->skb, NFULA_PACKET_HDR, sizeof(pmsg), &pmsg))
  		goto nla_put_failure;
0597f2680   Harald Welte   [NETFILTER]: Add ...
402

1db20a529   David S. Miller   nfnetlink_log: St...
403
404
405
  	if (prefix &&
  	    nla_put(inst->skb, NFULA_PREFIX, plen, prefix))
  		goto nla_put_failure;
0597f2680   Harald Welte   [NETFILTER]: Add ...
406
407
  
  	if (indev) {
1109a90c0   Pablo Neira Ayuso   netfilter: use IS...
408
  #if !IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
1db20a529   David S. Miller   nfnetlink_log: St...
409
410
411
  		if (nla_put_be32(inst->skb, NFULA_IFINDEX_INDEV,
  				 htonl(indev->ifindex)))
  			goto nla_put_failure;
fbcd923c3   Harald Welte   [NETFILTER]: add ...
412
413
414
415
416
  #else
  		if (pf == PF_BRIDGE) {
  			/* Case 1: outdev is physical input device, we need to
  			 * look for bridge group (when called from
  			 * netfilter_bridge) */
1db20a529   David S. Miller   nfnetlink_log: St...
417
418
  			if (nla_put_be32(inst->skb, NFULA_IFINDEX_PHYSINDEV,
  					 htonl(indev->ifindex)) ||
fbcd923c3   Harald Welte   [NETFILTER]: add ...
419
  			/* this is the bridge group "brX" */
e2361cb90   Aaron Conole   netfilter: Remove...
420
421
422
  			/* rcu_read_lock()ed by nf_hook_thresh or
  			 * nf_log_packet.
  			 */
1db20a529   David S. Miller   nfnetlink_log: St...
423
424
425
  			    nla_put_be32(inst->skb, NFULA_IFINDEX_INDEV,
  					 htonl(br_port_get_rcu(indev)->br->dev->ifindex)))
  				goto nla_put_failure;
fbcd923c3   Harald Welte   [NETFILTER]: add ...
426
  		} else {
c737b7c45   Florian Westphal   netfilter: bridge...
427
  			struct net_device *physindev;
fbcd923c3   Harald Welte   [NETFILTER]: add ...
428
429
  			/* Case 2: indev is bridge group, we need to look for
  			 * physical device (when called from ipv4) */
1db20a529   David S. Miller   nfnetlink_log: St...
430
431
432
  			if (nla_put_be32(inst->skb, NFULA_IFINDEX_INDEV,
  					 htonl(indev->ifindex)))
  				goto nla_put_failure;
c737b7c45   Florian Westphal   netfilter: bridge...
433
434
435
  
  			physindev = nf_bridge_get_physindev(skb);
  			if (physindev &&
1db20a529   David S. Miller   nfnetlink_log: St...
436
  			    nla_put_be32(inst->skb, NFULA_IFINDEX_PHYSINDEV,
c737b7c45   Florian Westphal   netfilter: bridge...
437
  					 htonl(physindev->ifindex)))
1db20a529   David S. Miller   nfnetlink_log: St...
438
  				goto nla_put_failure;
fbcd923c3   Harald Welte   [NETFILTER]: add ...
439
440
  		}
  #endif
0597f2680   Harald Welte   [NETFILTER]: Add ...
441
442
443
  	}
  
  	if (outdev) {
1109a90c0   Pablo Neira Ayuso   netfilter: use IS...
444
  #if !IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
1db20a529   David S. Miller   nfnetlink_log: St...
445
446
447
  		if (nla_put_be32(inst->skb, NFULA_IFINDEX_OUTDEV,
  				 htonl(outdev->ifindex)))
  			goto nla_put_failure;
fbcd923c3   Harald Welte   [NETFILTER]: add ...
448
449
450
451
452
  #else
  		if (pf == PF_BRIDGE) {
  			/* Case 1: outdev is physical output device, we need to
  			 * look for bridge group (when called from
  			 * netfilter_bridge) */
1db20a529   David S. Miller   nfnetlink_log: St...
453
454
  			if (nla_put_be32(inst->skb, NFULA_IFINDEX_PHYSOUTDEV,
  					 htonl(outdev->ifindex)) ||
fbcd923c3   Harald Welte   [NETFILTER]: add ...
455
  			/* this is the bridge group "brX" */
e2361cb90   Aaron Conole   netfilter: Remove...
456
457
458
  			/* rcu_read_lock()ed by nf_hook_thresh or
  			 * nf_log_packet.
  			 */
1db20a529   David S. Miller   nfnetlink_log: St...
459
460
461
  			    nla_put_be32(inst->skb, NFULA_IFINDEX_OUTDEV,
  					 htonl(br_port_get_rcu(outdev)->br->dev->ifindex)))
  				goto nla_put_failure;
fbcd923c3   Harald Welte   [NETFILTER]: add ...
462
  		} else {
c737b7c45   Florian Westphal   netfilter: bridge...
463
  			struct net_device *physoutdev;
fbcd923c3   Harald Welte   [NETFILTER]: add ...
464
465
  			/* Case 2: indev is a bridge group, we need to look
  			 * for physical device (when called from ipv4) */
1db20a529   David S. Miller   nfnetlink_log: St...
466
467
468
  			if (nla_put_be32(inst->skb, NFULA_IFINDEX_OUTDEV,
  					 htonl(outdev->ifindex)))
  				goto nla_put_failure;
c737b7c45   Florian Westphal   netfilter: bridge...
469
470
471
  
  			physoutdev = nf_bridge_get_physoutdev(skb);
  			if (physoutdev &&
1db20a529   David S. Miller   nfnetlink_log: St...
472
  			    nla_put_be32(inst->skb, NFULA_IFINDEX_PHYSOUTDEV,
c737b7c45   Florian Westphal   netfilter: bridge...
473
  					 htonl(physoutdev->ifindex)))
1db20a529   David S. Miller   nfnetlink_log: St...
474
  				goto nla_put_failure;
fbcd923c3   Harald Welte   [NETFILTER]: add ...
475
476
  		}
  #endif
0597f2680   Harald Welte   [NETFILTER]: Add ...
477
  	}
1db20a529   David S. Miller   nfnetlink_log: St...
478
479
480
  	if (skb->mark &&
  	    nla_put_be32(inst->skb, NFULA_MARK, htonl(skb->mark)))
  		goto nla_put_failure;
0597f2680   Harald Welte   [NETFILTER]: Add ...
481

2c38de4c1   Nicolas Cavallari   netfilter: fix lo...
482
483
  	if (indev && skb->dev &&
  	    skb->mac_header != skb->network_header) {
0597f2680   Harald Welte   [NETFILTER]: Add ...
484
  		struct nfulnl_msg_packet_hw phw;
e4d091d7b   Dan Carpenter   netfilter: nfnetl...
485
486
487
488
  		int len;
  
  		memset(&phw, 0, sizeof(phw));
  		len = dev_parse_header(skb, phw.hw_addr);
b95cce357   Stephen Hemminger   [NET]: Wrap hard_...
489
490
  		if (len > 0) {
  			phw.hw_addrlen = htons(len);
1db20a529   David S. Miller   nfnetlink_log: St...
491
492
  			if (nla_put(inst->skb, NFULA_HWADDR, sizeof(phw), &phw))
  				goto nla_put_failure;
b95cce357   Stephen Hemminger   [NET]: Wrap hard_...
493
  		}
0597f2680   Harald Welte   [NETFILTER]: Add ...
494
  	}
72961ecf8   Eric Leblond   netfilter: nfnetl...
495
  	if (indev && skb_mac_header_was_set(skb)) {
2dba62c30   Patrick McHardy   netfilter: nfnetl...
496
  		if (nla_put_be16(inst->skb, NFULA_HWTYPE, htons(skb->dev->type)) ||
1db20a529   David S. Miller   nfnetlink_log: St...
497
  		    nla_put_be16(inst->skb, NFULA_HWLEN,
0c36b48b3   Bob Hockney   netfilter: nfnetl...
498
499
500
501
502
503
504
505
506
507
508
  				 htons(skb->dev->hard_header_len)))
  			goto nla_put_failure;
  
  		hwhdrp = skb_mac_header(skb);
  
  		if (skb->dev->type == ARPHRD_SIT)
  			hwhdrp -= ETH_HLEN;
  
  		if (hwhdrp >= skb->head &&
  		    nla_put(inst->skb, NFULA_HWHEADER,
  			    skb->dev->hard_header_len, hwhdrp))
1db20a529   David S. Miller   nfnetlink_log: St...
509
  			goto nla_put_failure;
72961ecf8   Eric Leblond   netfilter: nfnetl...
510
  	}
2456e8553   Thomas Gleixner   ktime: Get rid of...
511
  	if (skb->tstamp) {
0597f2680   Harald Welte   [NETFILTER]: Add ...
512
  		struct nfulnl_msg_packet_timestamp ts;
f6389ecbc   Arnd Bergmann   nfnetlink: use y2...
513
514
515
  		struct timespec64 kts = ktime_to_timespec64(skb->tstamp);
  		ts.sec = cpu_to_be64(kts.tv_sec);
  		ts.usec = cpu_to_be64(kts.tv_nsec / NSEC_PER_USEC);
0597f2680   Harald Welte   [NETFILTER]: Add ...
516

1db20a529   David S. Miller   nfnetlink_log: St...
517
518
  		if (nla_put(inst->skb, NFULA_TIMESTAMP, sizeof(ts), &ts))
  			goto nla_put_failure;
0597f2680   Harald Welte   [NETFILTER]: Add ...
519
520
521
  	}
  
  	/* UID */
0626af313   Eric Dumazet   netfilter: take c...
522
  	sk = skb->sk;
a8399231f   Eric Dumazet   netfilter: use sk...
523
  	if (sk && sk_fullsock(sk)) {
0626af313   Eric Dumazet   netfilter: take c...
524
525
526
  		read_lock_bh(&sk->sk_callback_lock);
  		if (sk->sk_socket && sk->sk_socket->file) {
  			struct file *file = sk->sk_socket->file;
437589a74   Linus Torvalds   Merge branch 'for...
527
528
529
530
  			const struct cred *cred = file->f_cred;
  			struct user_namespace *user_ns = inst->peer_user_ns;
  			__be32 uid = htonl(from_kuid_munged(user_ns, cred->fsuid));
  			__be32 gid = htonl(from_kgid_munged(user_ns, cred->fsgid));
0626af313   Eric Dumazet   netfilter: take c...
531
  			read_unlock_bh(&sk->sk_callback_lock);
1db20a529   David S. Miller   nfnetlink_log: St...
532
533
534
  			if (nla_put_be32(inst->skb, NFULA_UID, uid) ||
  			    nla_put_be32(inst->skb, NFULA_GID, gid))
  				goto nla_put_failure;
0597f2680   Harald Welte   [NETFILTER]: Add ...
535
  		} else
0626af313   Eric Dumazet   netfilter: take c...
536
  			read_unlock_bh(&sk->sk_callback_lock);
0597f2680   Harald Welte   [NETFILTER]: Add ...
537
  	}
0af5f6c1e   Harald Welte   [NETFILTER] nfnet...
538
  	/* local sequence number */
1db20a529   David S. Miller   nfnetlink_log: St...
539
540
541
  	if ((inst->flags & NFULNL_CFG_F_SEQ) &&
  	    nla_put_be32(inst->skb, NFULA_SEQ, htonl(inst->seq++)))
  		goto nla_put_failure;
0dfedd287   Patrick McHardy   [NETFILTER]: nfne...
542

0af5f6c1e   Harald Welte   [NETFILTER] nfnet...
543
  	/* global sequence number */
1db20a529   David S. Miller   nfnetlink_log: St...
544
545
  	if ((inst->flags & NFULNL_CFG_F_SEQ_GLOBAL) &&
  	    nla_put_be32(inst->skb, NFULA_SEQ_GLOBAL,
9368a53c4   Gao feng   netfilter: nfnetl...
546
  			 htonl(atomic_inc_return(&log->global_seq))))
1db20a529   David S. Miller   nfnetlink_log: St...
547
  		goto nla_put_failure;
0af5f6c1e   Harald Welte   [NETFILTER] nfnet...
548

a29a9a585   Ken-ichirou MATSUZAWA   netfilter: nfnetl...
549
550
551
  	if (ct && nfnl_ct->build(inst->skb, ct, ctinfo,
  				 NFULA_CT, NFULA_CT_INFO) < 0)
  		goto nla_put_failure;
0597f2680   Harald Welte   [NETFILTER]: Add ...
552
  	if (data_len) {
df6fb868d   Patrick McHardy   [NETFILTER]: nfne...
553
554
  		struct nlattr *nla;
  		int size = nla_attr_size(data_len);
0597f2680   Harald Welte   [NETFILTER]: Add ...
555

822516154   Pablo Neira Ayuso   netfilter: nfnetl...
556
557
  		if (skb_tailroom(inst->skb) < nla_total_size(data_len))
  			goto nla_put_failure;
0597f2680   Harald Welte   [NETFILTER]: Add ...
558

4df864c1d   Johannes Berg   networking: make ...
559
  		nla = skb_put(inst->skb, nla_total_size(data_len));
df6fb868d   Patrick McHardy   [NETFILTER]: nfne...
560
561
  		nla->nla_type = NFULA_PAYLOAD;
  		nla->nla_len = size;
0597f2680   Harald Welte   [NETFILTER]: Add ...
562

df6fb868d   Patrick McHardy   [NETFILTER]: nfne...
563
  		if (skb_copy_bits(skb, 0, nla_data(nla), data_len))
0597f2680   Harald Welte   [NETFILTER]: Add ...
564
565
  			BUG();
  	}
601e68e10   YOSHIFUJI Hideaki   [NETFILTER]: Fix ...
566

0597f2680   Harald Welte   [NETFILTER]: Add ...
567
568
  	nlh->nlmsg_len = inst->skb->tail - old_tail;
  	return 0;
df6fb868d   Patrick McHardy   [NETFILTER]: nfne...
569
  nla_put_failure:
0597f2680   Harald Welte   [NETFILTER]: Add ...
570
571
572
573
  	PRINTR(KERN_ERR "nfnetlink_log: error creating log nlmsg
  ");
  	return -1;
  }
549d2d41c   Julia Lawall   netfilter: consti...
574
  static const struct nf_loginfo default_loginfo = {
0597f2680   Harald Welte   [NETFILTER]: Add ...
575
576
577
578
579
580
581
582
583
584
585
  	.type =		NF_LOG_TYPE_ULOG,
  	.u = {
  		.ulog = {
  			.copy_len	= 0xffff,
  			.group		= 0,
  			.qthreshold	= 1,
  		},
  	},
  };
  
  /* log handler for internal netfilter logging api */
5f7340eff   Eric Leblond   netfilter: xt_NFL...
586
  void
8cdb46da0   Hans Schillstrom   netfilter: log: n...
587
588
  nfulnl_log_packet(struct net *net,
  		  u_int8_t pf,
0597f2680   Harald Welte   [NETFILTER]: Add ...
589
590
591
592
593
594
595
  		  unsigned int hooknum,
  		  const struct sk_buff *skb,
  		  const struct net_device *in,
  		  const struct net_device *out,
  		  const struct nf_loginfo *li_user,
  		  const char *prefix)
  {
a29a9a585   Ken-ichirou MATSUZAWA   netfilter: nfnetl...
596
597
  	size_t size;
  	unsigned int data_len;
0597f2680   Harald Welte   [NETFILTER]: Add ...
598
599
600
  	struct nfulnl_instance *inst;
  	const struct nf_loginfo *li;
  	unsigned int qthreshold;
d7a5c3244   Patrick McHardy   [NETFILTER]: nfne...
601
  	unsigned int plen;
9368a53c4   Gao feng   netfilter: nfnetl...
602
  	struct nfnl_log_net *log = nfnl_log_pernet(net);
a29a9a585   Ken-ichirou MATSUZAWA   netfilter: nfnetl...
603
604
605
  	const struct nfnl_ct_hook *nfnl_ct = NULL;
  	struct nf_conn *ct = NULL;
  	enum ip_conntrack_info uninitialized_var(ctinfo);
0597f2680   Harald Welte   [NETFILTER]: Add ...
606

601e68e10   YOSHIFUJI Hideaki   [NETFILTER]: Fix ...
607
  	if (li_user && li_user->type == NF_LOG_TYPE_ULOG)
0597f2680   Harald Welte   [NETFILTER]: Add ...
608
609
610
  		li = li_user;
  	else
  		li = &default_loginfo;
9368a53c4   Gao feng   netfilter: nfnetl...
611
  	inst = instance_lookup_get(log, li->u.ulog.group);
0597f2680   Harald Welte   [NETFILTER]: Add ...
612
  	if (!inst)
0597f2680   Harald Welte   [NETFILTER]: Add ...
613
  		return;
0597f2680   Harald Welte   [NETFILTER]: Add ...
614

d7a5c3244   Patrick McHardy   [NETFILTER]: nfne...
615
616
  	plen = 0;
  	if (prefix)
881dbfe8a   Patrick McHardy   [NETFILTER]: nfne...
617
  		plen = strlen(prefix) + 1;
d7a5c3244   Patrick McHardy   [NETFILTER]: nfne...
618

0597f2680   Harald Welte   [NETFILTER]: Add ...
619
620
621
  	/* FIXME: do we want to make the size calculation conditional based on
  	 * what is actually present?  way more branches and checks, but more
  	 * memory efficient... */
573ce260b   Hong zhi guo   net-next: replace...
622
  	size =    nlmsg_total_size(sizeof(struct nfgenmsg))
df6fb868d   Patrick McHardy   [NETFILTER]: nfne...
623
624
625
  		+ nla_total_size(sizeof(struct nfulnl_msg_packet_hdr))
  		+ nla_total_size(sizeof(u_int32_t))	/* ifindex */
  		+ nla_total_size(sizeof(u_int32_t))	/* ifindex */
1109a90c0   Pablo Neira Ayuso   netfilter: use IS...
626
  #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
df6fb868d   Patrick McHardy   [NETFILTER]: nfne...
627
628
  		+ nla_total_size(sizeof(u_int32_t))	/* ifindex */
  		+ nla_total_size(sizeof(u_int32_t))	/* ifindex */
fbcd923c3   Harald Welte   [NETFILTER]: add ...
629
  #endif
df6fb868d   Patrick McHardy   [NETFILTER]: nfne...
630
631
  		+ nla_total_size(sizeof(u_int32_t))	/* mark */
  		+ nla_total_size(sizeof(u_int32_t))	/* uid */
76aa1ce13   Patrick McHardy   [NETFILTER]: nfne...
632
  		+ nla_total_size(sizeof(u_int32_t))	/* gid */
df6fb868d   Patrick McHardy   [NETFILTER]: nfne...
633
634
  		+ nla_total_size(plen)			/* prefix */
  		+ nla_total_size(sizeof(struct nfulnl_msg_packet_hw))
9dfa1dfe4   Florian Westphal   netfilter: nf_log...
635
636
  		+ nla_total_size(sizeof(struct nfulnl_msg_packet_timestamp))
  		+ nla_total_size(sizeof(struct nfgenmsg));	/* NLMSG_DONE */
0597f2680   Harald Welte   [NETFILTER]: Add ...
637

eeff9beec   Pablo Neira Ayuso   netfilter: nfnetl...
638
639
640
641
642
  	if (in && skb_mac_header_was_set(skb)) {
  		size +=   nla_total_size(skb->dev->hard_header_len)
  			+ nla_total_size(sizeof(u_int16_t))	/* hwtype */
  			+ nla_total_size(sizeof(u_int16_t));	/* hwlen */
  	}
0597f2680   Harald Welte   [NETFILTER]: Add ...
643
  	spin_lock_bh(&inst->lock);
0af5f6c1e   Harald Welte   [NETFILTER] nfnet...
644
  	if (inst->flags & NFULNL_CFG_F_SEQ)
df6fb868d   Patrick McHardy   [NETFILTER]: nfne...
645
  		size += nla_total_size(sizeof(u_int32_t));
0af5f6c1e   Harald Welte   [NETFILTER] nfnet...
646
  	if (inst->flags & NFULNL_CFG_F_SEQ_GLOBAL)
df6fb868d   Patrick McHardy   [NETFILTER]: nfne...
647
  		size += nla_total_size(sizeof(u_int32_t));
a29a9a585   Ken-ichirou MATSUZAWA   netfilter: nfnetl...
648
649
650
651
652
653
654
655
  	if (inst->flags & NFULNL_CFG_F_CONNTRACK) {
  		nfnl_ct = rcu_dereference(nfnl_ct_hook);
  		if (nfnl_ct != NULL) {
  			ct = nfnl_ct->get_ct(skb, &ctinfo);
  			if (ct != NULL)
  				size += nfnl_ct->build_size(ct);
  		}
  	}
0af5f6c1e   Harald Welte   [NETFILTER] nfnet...
656

0597f2680   Harald Welte   [NETFILTER]: Add ...
657
658
  	qthreshold = inst->qthreshold;
  	/* per-rule qthreshold overrides per-instance */
5ca431f9a   Eric Leblond   netfilter: nfnetl...
659
660
661
  	if (li->u.ulog.qthreshold)
  		if (qthreshold > li->u.ulog.qthreshold)
  			qthreshold = li->u.ulog.qthreshold;
601e68e10   YOSHIFUJI Hideaki   [NETFILTER]: Fix ...
662

0597f2680   Harald Welte   [NETFILTER]: Add ...
663
664
665
666
667
  	switch (inst->copy_mode) {
  	case NFULNL_COPY_META:
  	case NFULNL_COPY_NONE:
  		data_len = 0;
  		break;
601e68e10   YOSHIFUJI Hideaki   [NETFILTER]: Fix ...
668

0597f2680   Harald Welte   [NETFILTER]: Add ...
669
  	case NFULNL_COPY_PACKET:
7643507fe   Vishwanath Pai   netfilter: xt_NFL...
670
671
672
673
674
675
  		data_len = inst->copy_range;
  		if ((li->u.ulog.flags & NF_LOG_F_COPY_LEN) &&
  		    (li->u.ulog.copy_len < data_len))
  			data_len = li->u.ulog.copy_len;
  
  		if (data_len > skb->len)
0597f2680   Harald Welte   [NETFILTER]: Add ...
676
  			data_len = skb->len;
601e68e10   YOSHIFUJI Hideaki   [NETFILTER]: Fix ...
677

df6fb868d   Patrick McHardy   [NETFILTER]: nfne...
678
  		size += nla_total_size(data_len);
0597f2680   Harald Welte   [NETFILTER]: Add ...
679
  		break;
601e68e10   YOSHIFUJI Hideaki   [NETFILTER]: Fix ...
680

f5c5440d4   Eric Dumazet   netfilter: nfnetl...
681
  	case NFULNL_COPY_DISABLED:
0597f2680   Harald Welte   [NETFILTER]: Add ...
682
  	default:
55b5a91e1   Michal Miroslaw   [NETFILTER]: nfne...
683
  		goto unlock_and_release;
0597f2680   Harald Welte   [NETFILTER]: Add ...
684
  	}
9dfa1dfe4   Florian Westphal   netfilter: nf_log...
685
  	if (inst->skb && size > skb_tailroom(inst->skb)) {
0597f2680   Harald Welte   [NETFILTER]: Add ...
686
687
  		/* either the queue len is too high or we don't have
  		 * enough room in the skb left. flush to userspace. */
e35670614   Michal Miroslaw   [NETFILTER]: nfne...
688
  		__nfulnl_flush(inst);
55b5a91e1   Michal Miroslaw   [NETFILTER]: nfne...
689
  	}
0597f2680   Harald Welte   [NETFILTER]: Add ...
690

55b5a91e1   Michal Miroslaw   [NETFILTER]: nfne...
691
  	if (!inst->skb) {
afff14f60   Gao feng   netfilter: nfnetl...
692
693
  		inst->skb = nfulnl_alloc_skb(net, inst->peer_portid,
  					     inst->nlbufsiz, size);
55b5a91e1   Michal Miroslaw   [NETFILTER]: nfne...
694
  		if (!inst->skb)
0597f2680   Harald Welte   [NETFILTER]: Add ...
695
  			goto alloc_failure;
0597f2680   Harald Welte   [NETFILTER]: Add ...
696
  	}
0597f2680   Harald Welte   [NETFILTER]: Add ...
697
  	inst->qlen++;
9368a53c4   Gao feng   netfilter: nfnetl...
698
  	__build_packet_message(log, inst, skb, data_len, pf,
a29a9a585   Ken-ichirou MATSUZAWA   netfilter: nfnetl...
699
700
  				hooknum, in, out, prefix, plen,
  				nfnl_ct, ct, ctinfo);
0597f2680   Harald Welte   [NETFILTER]: Add ...
701

d63b043d9   Michal Miroslaw   [NETFILTER]: nfne...
702
703
  	if (inst->qlen >= qthreshold)
  		__nfulnl_flush(inst);
0597f2680   Harald Welte   [NETFILTER]: Add ...
704
705
  	/* timer_pending always called within inst->lock, so there
  	 * is no chance of a race here */
d63b043d9   Michal Miroslaw   [NETFILTER]: nfne...
706
  	else if (!timer_pending(&inst->timer)) {
0597f2680   Harald Welte   [NETFILTER]: Add ...
707
708
709
710
  		instance_get(inst);
  		inst->timer.expires = jiffies + (inst->flushtimeout*HZ/100);
  		add_timer(&inst->timer);
  	}
0597f2680   Harald Welte   [NETFILTER]: Add ...
711

ed32abeaf   Michal Miroslaw   [NETFILTER]: nfne...
712
713
714
  unlock_and_release:
  	spin_unlock_bh(&inst->lock);
  	instance_put(inst);
0597f2680   Harald Welte   [NETFILTER]: Add ...
715
716
717
  	return;
  
  alloc_failure:
0597f2680   Harald Welte   [NETFILTER]: Add ...
718
  	/* FIXME: statistics */
ed32abeaf   Michal Miroslaw   [NETFILTER]: nfne...
719
  	goto unlock_and_release;
0597f2680   Harald Welte   [NETFILTER]: Add ...
720
  }
5f7340eff   Eric Leblond   netfilter: xt_NFL...
721
  EXPORT_SYMBOL_GPL(nfulnl_log_packet);
0597f2680   Harald Welte   [NETFILTER]: Add ...
722
723
724
725
726
727
  
  static int
  nfulnl_rcv_nl_event(struct notifier_block *this,
  		   unsigned long event, void *ptr)
  {
  	struct netlink_notify *n = ptr;
9368a53c4   Gao feng   netfilter: nfnetl...
728
  	struct nfnl_log_net *log = nfnl_log_pernet(n->net);
0597f2680   Harald Welte   [NETFILTER]: Add ...
729

dee5817e8   Patrick McHardy   netfilter: remove...
730
  	if (event == NETLINK_URELEASE && n->protocol == NETLINK_NETFILTER) {
0597f2680   Harald Welte   [NETFILTER]: Add ...
731
  		int i;
15e473046   Eric W. Biederman   netlink: Rename p...
732
  		/* destroy all instances for this portid */
9368a53c4   Gao feng   netfilter: nfnetl...
733
  		spin_lock_bh(&log->instances_lock);
0597f2680   Harald Welte   [NETFILTER]: Add ...
734
  		for  (i = 0; i < INSTANCE_BUCKETS; i++) {
b67bfe0d4   Sasha Levin   hlist: drop the n...
735
  			struct hlist_node *t2;
0597f2680   Harald Welte   [NETFILTER]: Add ...
736
  			struct nfulnl_instance *inst;
9368a53c4   Gao feng   netfilter: nfnetl...
737
  			struct hlist_head *head = &log->instance_table[i];
0597f2680   Harald Welte   [NETFILTER]: Add ...
738

b67bfe0d4   Sasha Levin   hlist: drop the n...
739
  			hlist_for_each_entry_safe(inst, t2, head, hlist) {
9368a53c4   Gao feng   netfilter: nfnetl...
740
  				if (n->portid == inst->peer_portid)
0597f2680   Harald Welte   [NETFILTER]: Add ...
741
742
743
  					__instance_destroy(inst);
  			}
  		}
9368a53c4   Gao feng   netfilter: nfnetl...
744
  		spin_unlock_bh(&log->instances_lock);
0597f2680   Harald Welte   [NETFILTER]: Add ...
745
746
747
748
749
750
751
  	}
  	return NOTIFY_DONE;
  }
  
  static struct notifier_block nfulnl_rtnl_notifier = {
  	.notifier_call	= nfulnl_rcv_nl_event,
  };
7b8002a15   Pablo Neira Ayuso   netfilter: nfnetl...
752
753
  static int nfulnl_recv_unsupp(struct net *net, struct sock *ctnl,
  			      struct sk_buff *skb, const struct nlmsghdr *nlh,
04ba724b6   Pablo Neira Ayuso   netfilter: nfnetl...
754
755
  			      const struct nlattr * const nfqa[],
  			      struct netlink_ext_ack *extack)
0597f2680   Harald Welte   [NETFILTER]: Add ...
756
757
758
  {
  	return -ENOTSUPP;
  }
ca735b3aa   Eric Leblond   netfilter: use a ...
759
  static struct nf_logger nfulnl_logger __read_mostly = {
0597f2680   Harald Welte   [NETFILTER]: Add ...
760
  	.name	= "nfnetlink_log",
5962815a6   Pablo Neira Ayuso   netfilter: nf_log...
761
  	.type	= NF_LOG_TYPE_ULOG,
d4ef38354   Arushi Singhal   netfilter: Remove...
762
  	.logfn	= nfulnl_log_packet,
0597f2680   Harald Welte   [NETFILTER]: Add ...
763
764
  	.me	= THIS_MODULE,
  };
fd8281ada   Patrick McHardy   [NETFILTER]: nfne...
765
766
767
768
769
770
771
  static const struct nla_policy nfula_cfg_policy[NFULA_CFG_MAX+1] = {
  	[NFULA_CFG_CMD]		= { .len = sizeof(struct nfulnl_msg_config_cmd) },
  	[NFULA_CFG_MODE]	= { .len = sizeof(struct nfulnl_msg_config_mode) },
  	[NFULA_CFG_TIMEOUT]	= { .type = NLA_U32 },
  	[NFULA_CFG_QTHRESH]	= { .type = NLA_U32 },
  	[NFULA_CFG_NLBUFSIZ]	= { .type = NLA_U32 },
  	[NFULA_CFG_FLAGS]	= { .type = NLA_U16 },
0597f2680   Harald Welte   [NETFILTER]: Add ...
772
  };
7b8002a15   Pablo Neira Ayuso   netfilter: nfnetl...
773
774
  static int nfulnl_recv_config(struct net *net, struct sock *ctnl,
  			      struct sk_buff *skb, const struct nlmsghdr *nlh,
04ba724b6   Pablo Neira Ayuso   netfilter: nfnetl...
775
776
  			      const struct nlattr * const nfula[],
  			      struct netlink_ext_ack *extack)
0597f2680   Harald Welte   [NETFILTER]: Add ...
777
  {
d550d0958   David S. Miller   netfilter: nfnetl...
778
  	struct nfgenmsg *nfmsg = nlmsg_data(nlh);
0597f2680   Harald Welte   [NETFILTER]: Add ...
779
780
  	u_int16_t group_num = ntohs(nfmsg->res_id);
  	struct nfulnl_instance *inst;
b7047a1c8   Patrick McHardy   [NETFILTER]: nfne...
781
  	struct nfulnl_msg_config_cmd *cmd = NULL;
9368a53c4   Gao feng   netfilter: nfnetl...
782
  	struct nfnl_log_net *log = nfnl_log_pernet(net);
0597f2680   Harald Welte   [NETFILTER]: Add ...
783
  	int ret = 0;
c872a2d9e   Arnd Bergmann   netfilter: nfnetl...
784
  	u16 flags = 0;
0597f2680   Harald Welte   [NETFILTER]: Add ...
785

b7047a1c8   Patrick McHardy   [NETFILTER]: nfne...
786
787
788
789
790
791
792
  	if (nfula[NFULA_CFG_CMD]) {
  		u_int8_t pf = nfmsg->nfgen_family;
  		cmd = nla_data(nfula[NFULA_CFG_CMD]);
  
  		/* Commands without queue context */
  		switch (cmd->command) {
  		case NFULNL_CFG_CMD_PF_BIND:
30e0c6a6b   Gao feng   netfilter: nf_log...
793
  			return nf_log_bind_pf(net, pf, &nfulnl_logger);
b7047a1c8   Patrick McHardy   [NETFILTER]: nfne...
794
  		case NFULNL_CFG_CMD_PF_UNBIND:
30e0c6a6b   Gao feng   netfilter: nf_log...
795
  			nf_log_unbind_pf(net, pf);
b7047a1c8   Patrick McHardy   [NETFILTER]: nfne...
796
797
798
  			return 0;
  		}
  	}
9368a53c4   Gao feng   netfilter: nfnetl...
799
  	inst = instance_lookup_get(log, group_num);
15e473046   Eric W. Biederman   netlink: Rename p...
800
  	if (inst && inst->peer_portid != NETLINK_CB(skb).portid) {
c0506365a   Patrick McHardy   [NETFILTER]: nfne...
801
802
803
  		ret = -EPERM;
  		goto out_put;
  	}
8cbc87082   Pablo Neira   netfilter: nfnetl...
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
  	/* Check if we support these flags in first place, dependencies should
  	 * be there too not to break atomicity.
  	 */
  	if (nfula[NFULA_CFG_FLAGS]) {
  		flags = ntohs(nla_get_be16(nfula[NFULA_CFG_FLAGS]));
  
  		if ((flags & NFULNL_CFG_F_CONNTRACK) &&
  		    !rcu_access_pointer(nfnl_ct_hook)) {
  #ifdef CONFIG_MODULES
  			nfnl_unlock(NFNL_SUBSYS_ULOG);
  			request_module("ip_conntrack_netlink");
  			nfnl_lock(NFNL_SUBSYS_ULOG);
  			if (rcu_access_pointer(nfnl_ct_hook)) {
  				ret = -EAGAIN;
  				goto out_put;
  			}
  #endif
  			ret = -EOPNOTSUPP;
  			goto out_put;
  		}
  	}
b7047a1c8   Patrick McHardy   [NETFILTER]: nfne...
825
  	if (cmd != NULL) {
0597f2680   Harald Welte   [NETFILTER]: Add ...
826
827
828
829
830
831
  		switch (cmd->command) {
  		case NFULNL_CFG_CMD_BIND:
  			if (inst) {
  				ret = -EBUSY;
  				goto out_put;
  			}
9368a53c4   Gao feng   netfilter: nfnetl...
832
  			inst = instance_create(net, group_num,
15e473046   Eric W. Biederman   netlink: Rename p...
833
  					       NETLINK_CB(skb).portid,
e32123e59   Patrick McHardy   netlink: rename s...
834
  					       sk_user_ns(NETLINK_CB(skb).sk));
baab2ce7d   Patrick McHardy   [NETFILTER]: nfne...
835
836
  			if (IS_ERR(inst)) {
  				ret = PTR_ERR(inst);
f414c16c0   Michal Miroslaw   [NETFILTER]: nfne...
837
  				goto out;
0597f2680   Harald Welte   [NETFILTER]: Add ...
838
839
840
841
842
  			}
  			break;
  		case NFULNL_CFG_CMD_UNBIND:
  			if (!inst) {
  				ret = -ENODEV;
f414c16c0   Michal Miroslaw   [NETFILTER]: nfne...
843
  				goto out;
0597f2680   Harald Welte   [NETFILTER]: Add ...
844
  			}
9368a53c4   Gao feng   netfilter: nfnetl...
845
  			instance_destroy(log, inst);
a49c65037   Alexey Dobriyan   netfilter: nfnetl...
846
  			goto out_put;
0597f2680   Harald Welte   [NETFILTER]: Add ...
847
  		default:
cd21f0ac4   Patrick McHardy   [NETFILTER]: nfne...
848
  			ret = -ENOTSUPP;
eb075954e   Ken-ichirou MATSUZAWA   netfilter: nfnetl...
849
  			goto out_put;
0597f2680   Harald Welte   [NETFILTER]: Add ...
850
  		}
336a3b3ee   Pablo Neira Ayuso   netfilter: nfnetl...
851
852
853
  	} else if (!inst) {
  		ret = -ENODEV;
  		goto out;
0597f2680   Harald Welte   [NETFILTER]: Add ...
854
  	}
df6fb868d   Patrick McHardy   [NETFILTER]: nfne...
855
  	if (nfula[NFULA_CFG_MODE]) {
336a3b3ee   Pablo Neira Ayuso   netfilter: nfnetl...
856
857
  		struct nfulnl_msg_config_mode *params =
  			nla_data(nfula[NFULA_CFG_MODE]);
0597f2680   Harald Welte   [NETFILTER]: Add ...
858
859
  
  		nfulnl_set_mode(inst, params->copy_mode,
d1208b999   Al Viro   [NETFILTER] bug: ...
860
  				ntohl(params->copy_range));
0597f2680   Harald Welte   [NETFILTER]: Add ...
861
  	}
df6fb868d   Patrick McHardy   [NETFILTER]: nfne...
862
  	if (nfula[NFULA_CFG_TIMEOUT]) {
0dfedd287   Patrick McHardy   [NETFILTER]: nfne...
863
  		__be32 timeout = nla_get_be32(nfula[NFULA_CFG_TIMEOUT]);
0597f2680   Harald Welte   [NETFILTER]: Add ...
864
865
866
  
  		nfulnl_set_timeout(inst, ntohl(timeout));
  	}
df6fb868d   Patrick McHardy   [NETFILTER]: nfne...
867
  	if (nfula[NFULA_CFG_NLBUFSIZ]) {
0dfedd287   Patrick McHardy   [NETFILTER]: nfne...
868
  		__be32 nlbufsiz = nla_get_be32(nfula[NFULA_CFG_NLBUFSIZ]);
0597f2680   Harald Welte   [NETFILTER]: Add ...
869
870
871
  
  		nfulnl_set_nlbufsiz(inst, ntohl(nlbufsiz));
  	}
df6fb868d   Patrick McHardy   [NETFILTER]: nfne...
872
  	if (nfula[NFULA_CFG_QTHRESH]) {
0dfedd287   Patrick McHardy   [NETFILTER]: nfne...
873
  		__be32 qthresh = nla_get_be32(nfula[NFULA_CFG_QTHRESH]);
0597f2680   Harald Welte   [NETFILTER]: Add ...
874
875
876
  
  		nfulnl_set_qthresh(inst, ntohl(qthresh));
  	}
8cbc87082   Pablo Neira   netfilter: nfnetl...
877
  	if (nfula[NFULA_CFG_FLAGS])
a29a9a585   Ken-ichirou MATSUZAWA   netfilter: nfnetl...
878
  		nfulnl_set_flags(inst, flags);
0af5f6c1e   Harald Welte   [NETFILTER] nfnet...
879

0597f2680   Harald Welte   [NETFILTER]: Add ...
880
881
  out_put:
  	instance_put(inst);
dd16704eb   Michal Miroslaw   [NETFILTER]: nfne...
882
  out:
0597f2680   Harald Welte   [NETFILTER]: Add ...
883
884
  	return ret;
  }
7c8d4cb41   Patrick McHardy   [NETFILTER]: nfne...
885
  static const struct nfnl_callback nfulnl_cb[NFULNL_MSG_MAX] = {
0597f2680   Harald Welte   [NETFILTER]: Add ...
886
  	[NFULNL_MSG_PACKET]	= { .call = nfulnl_recv_unsupp,
37d2e7a20   Harald Welte   [NETFILTER] nfnet...
887
  				    .attr_count = NFULA_MAX, },
0597f2680   Harald Welte   [NETFILTER]: Add ...
888
  	[NFULNL_MSG_CONFIG]	= { .call = nfulnl_recv_config,
fd8281ada   Patrick McHardy   [NETFILTER]: nfne...
889
890
  				    .attr_count = NFULA_CFG_MAX,
  				    .policy = nfula_cfg_policy },
0597f2680   Harald Welte   [NETFILTER]: Add ...
891
  };
7c8d4cb41   Patrick McHardy   [NETFILTER]: nfne...
892
  static const struct nfnetlink_subsystem nfulnl_subsys = {
0597f2680   Harald Welte   [NETFILTER]: Add ...
893
894
895
  	.name		= "log",
  	.subsys_id	= NFNL_SUBSYS_ULOG,
  	.cb_count	= NFULNL_MSG_MAX,
0597f2680   Harald Welte   [NETFILTER]: Add ...
896
897
898
899
900
  	.cb		= nfulnl_cb,
  };
  
  #ifdef CONFIG_PROC_FS
  struct iter_state {
9368a53c4   Gao feng   netfilter: nfnetl...
901
  	struct seq_net_private p;
0597f2680   Harald Welte   [NETFILTER]: Add ...
902
903
  	unsigned int bucket;
  };
9368a53c4   Gao feng   netfilter: nfnetl...
904
  static struct hlist_node *get_first(struct net *net, struct iter_state *st)
0597f2680   Harald Welte   [NETFILTER]: Add ...
905
  {
9368a53c4   Gao feng   netfilter: nfnetl...
906
  	struct nfnl_log_net *log;
0597f2680   Harald Welte   [NETFILTER]: Add ...
907
908
  	if (!st)
  		return NULL;
9368a53c4   Gao feng   netfilter: nfnetl...
909
  	log = nfnl_log_pernet(net);
0597f2680   Harald Welte   [NETFILTER]: Add ...
910
  	for (st->bucket = 0; st->bucket < INSTANCE_BUCKETS; st->bucket++) {
9368a53c4   Gao feng   netfilter: nfnetl...
911
912
913
914
  		struct hlist_head *head = &log->instance_table[st->bucket];
  
  		if (!hlist_empty(head))
  			return rcu_dereference_bh(hlist_first_rcu(head));
0597f2680   Harald Welte   [NETFILTER]: Add ...
915
916
917
  	}
  	return NULL;
  }
9368a53c4   Gao feng   netfilter: nfnetl...
918
919
  static struct hlist_node *get_next(struct net *net, struct iter_state *st,
  				   struct hlist_node *h)
0597f2680   Harald Welte   [NETFILTER]: Add ...
920
  {
0e60ebe04   Eric Dumazet   netfilter: add __...
921
  	h = rcu_dereference_bh(hlist_next_rcu(h));
0597f2680   Harald Welte   [NETFILTER]: Add ...
922
  	while (!h) {
9368a53c4   Gao feng   netfilter: nfnetl...
923
924
  		struct nfnl_log_net *log;
  		struct hlist_head *head;
0597f2680   Harald Welte   [NETFILTER]: Add ...
925
926
  		if (++st->bucket >= INSTANCE_BUCKETS)
  			return NULL;
9368a53c4   Gao feng   netfilter: nfnetl...
927
928
929
  		log = nfnl_log_pernet(net);
  		head = &log->instance_table[st->bucket];
  		h = rcu_dereference_bh(hlist_first_rcu(head));
0597f2680   Harald Welte   [NETFILTER]: Add ...
930
931
932
  	}
  	return h;
  }
9368a53c4   Gao feng   netfilter: nfnetl...
933
934
  static struct hlist_node *get_idx(struct net *net, struct iter_state *st,
  				  loff_t pos)
0597f2680   Harald Welte   [NETFILTER]: Add ...
935
936
  {
  	struct hlist_node *head;
9368a53c4   Gao feng   netfilter: nfnetl...
937
  	head = get_first(net, st);
0597f2680   Harald Welte   [NETFILTER]: Add ...
938
939
  
  	if (head)
9368a53c4   Gao feng   netfilter: nfnetl...
940
  		while (pos && (head = get_next(net, st, head)))
0597f2680   Harald Welte   [NETFILTER]: Add ...
941
942
943
  			pos--;
  	return pos ? NULL : head;
  }
9368a53c4   Gao feng   netfilter: nfnetl...
944
  static void *seq_start(struct seq_file *s, loff_t *pos)
bed1be208   Eric Dumazet   netfilter: nfnetl...
945
  	__acquires(rcu_bh)
0597f2680   Harald Welte   [NETFILTER]: Add ...
946
  {
bed1be208   Eric Dumazet   netfilter: nfnetl...
947
  	rcu_read_lock_bh();
9368a53c4   Gao feng   netfilter: nfnetl...
948
  	return get_idx(seq_file_net(s), s->private, *pos);
0597f2680   Harald Welte   [NETFILTER]: Add ...
949
950
951
952
953
  }
  
  static void *seq_next(struct seq_file *s, void *v, loff_t *pos)
  {
  	(*pos)++;
9368a53c4   Gao feng   netfilter: nfnetl...
954
  	return get_next(seq_file_net(s), s->private, v);
0597f2680   Harald Welte   [NETFILTER]: Add ...
955
956
957
  }
  
  static void seq_stop(struct seq_file *s, void *v)
bed1be208   Eric Dumazet   netfilter: nfnetl...
958
  	__releases(rcu_bh)
0597f2680   Harald Welte   [NETFILTER]: Add ...
959
  {
bed1be208   Eric Dumazet   netfilter: nfnetl...
960
  	rcu_read_unlock_bh();
0597f2680   Harald Welte   [NETFILTER]: Add ...
961
962
963
964
965
  }
  
  static int seq_show(struct seq_file *s, void *v)
  {
  	const struct nfulnl_instance *inst = v;
20a1d1652   Richard Weinberger   netfilter: Fix fo...
966
967
  	seq_printf(s, "%5u %6u %5u %1u %5u %6u %2u
  ",
1ca9e4177   Joe Perches   netfilter: Remove...
968
969
970
  		   inst->group_num,
  		   inst->peer_portid, inst->qlen,
  		   inst->copy_mode, inst->copy_range,
b54ab92b8   Reshetova, Elena   netfilter: refcou...
971
  		   inst->flushtimeout, refcount_read(&inst->use));
1ca9e4177   Joe Perches   netfilter: Remove...
972
973
  
  	return 0;
0597f2680   Harald Welte   [NETFILTER]: Add ...
974
  }
56b3d975b   Philippe De Muyter   [NET]: Make all i...
975
  static const struct seq_operations nful_seq_ops = {
0597f2680   Harald Welte   [NETFILTER]: Add ...
976
977
978
979
980
981
982
983
  	.start	= seq_start,
  	.next	= seq_next,
  	.stop	= seq_stop,
  	.show	= seq_show,
  };
  
  static int nful_open(struct inode *inode, struct file *file)
  {
9368a53c4   Gao feng   netfilter: nfnetl...
984
985
  	return seq_open_net(inode, file, &nful_seq_ops,
  			    sizeof(struct iter_state));
0597f2680   Harald Welte   [NETFILTER]: Add ...
986
  }
da7071d7e   Arjan van de Ven   [PATCH] mark stru...
987
  static const struct file_operations nful_file_ops = {
0597f2680   Harald Welte   [NETFILTER]: Add ...
988
989
990
991
  	.owner	 = THIS_MODULE,
  	.open	 = nful_open,
  	.read	 = seq_read,
  	.llseek	 = seq_lseek,
9368a53c4   Gao feng   netfilter: nfnetl...
992
  	.release = seq_release_net,
0597f2680   Harald Welte   [NETFILTER]: Add ...
993
994
995
  };
  
  #endif /* PROC_FS */
9368a53c4   Gao feng   netfilter: nfnetl...
996
  static int __net_init nfnl_log_net_init(struct net *net)
0597f2680   Harald Welte   [NETFILTER]: Add ...
997
  {
9368a53c4   Gao feng   netfilter: nfnetl...
998
999
  	unsigned int i;
  	struct nfnl_log_net *log = nfnl_log_pernet(net);
f13f2aeed   Philip Whineray   netfilter: Set /p...
1000
1001
1002
1003
1004
  #ifdef CONFIG_PROC_FS
  	struct proc_dir_entry *proc;
  	kuid_t root_uid;
  	kgid_t root_gid;
  #endif
601e68e10   YOSHIFUJI Hideaki   [NETFILTER]: Fix ...
1005

0597f2680   Harald Welte   [NETFILTER]: Add ...
1006
  	for (i = 0; i < INSTANCE_BUCKETS; i++)
9368a53c4   Gao feng   netfilter: nfnetl...
1007
1008
1009
1010
  		INIT_HLIST_HEAD(&log->instance_table[i]);
  	spin_lock_init(&log->instances_lock);
  
  #ifdef CONFIG_PROC_FS
f13f2aeed   Philip Whineray   netfilter: Set /p...
1011
1012
1013
  	proc = proc_create("nfnetlink_log", 0440,
  			   net->nf.proc_netfilter, &nful_file_ops);
  	if (!proc)
9368a53c4   Gao feng   netfilter: nfnetl...
1014
  		return -ENOMEM;
f13f2aeed   Philip Whineray   netfilter: Set /p...
1015
1016
1017
1018
1019
  
  	root_uid = make_kuid(net->user_ns, 0);
  	root_gid = make_kgid(net->user_ns, 0);
  	if (uid_valid(root_uid) && gid_valid(root_gid))
  		proc_set_user(proc, root_uid, root_gid);
9368a53c4   Gao feng   netfilter: nfnetl...
1020
1021
1022
1023
1024
1025
  #endif
  	return 0;
  }
  
  static void __net_exit nfnl_log_net_exit(struct net *net)
  {
e778f56e2   Pablo Neira Ayuso   netfilter: nf_{lo...
1026
  #ifdef CONFIG_PROC_FS
9368a53c4   Gao feng   netfilter: nfnetl...
1027
  	remove_proc_entry("nfnetlink_log", net->nf.proc_netfilter);
e778f56e2   Pablo Neira Ayuso   netfilter: nf_{lo...
1028
  #endif
45c2aff64   Gao feng   netfilter: nfnetl...
1029
  	nf_log_unset(net, &nfulnl_logger);
9368a53c4   Gao feng   netfilter: nfnetl...
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
  }
  
  static struct pernet_operations nfnl_log_net_ops = {
  	.init	= nfnl_log_net_init,
  	.exit	= nfnl_log_net_exit,
  	.id	= &nfnl_log_net_id,
  	.size	= sizeof(struct nfnl_log_net),
  };
  
  static int __init nfnetlink_log_init(void)
  {
3bfe04980   Francesco Ruggeri   netfilter: nfnetl...
1041
1042
1043
1044
1045
1046
1047
1048
  	int status;
  
  	status = register_pernet_subsys(&nfnl_log_net_ops);
  	if (status < 0) {
  		pr_err("failed to register pernet ops
  ");
  		goto out;
  	}
601e68e10   YOSHIFUJI Hideaki   [NETFILTER]: Fix ...
1049

0597f2680   Harald Welte   [NETFILTER]: Add ...
1050
1051
1052
  	netlink_register_notifier(&nfulnl_rtnl_notifier);
  	status = nfnetlink_subsys_register(&nfulnl_subsys);
  	if (status < 0) {
beacd3e8e   Marcelo Leitner   netfilter: nfnetl...
1053
1054
  		pr_err("failed to create netlink socket
  ");
0597f2680   Harald Welte   [NETFILTER]: Add ...
1055
1056
  		goto cleanup_netlink_notifier;
  	}
ca735b3aa   Eric Leblond   netfilter: use a ...
1057
1058
  	status = nf_log_register(NFPROTO_UNSPEC, &nfulnl_logger);
  	if (status < 0) {
beacd3e8e   Marcelo Leitner   netfilter: nfnetl...
1059
1060
  		pr_err("failed to register logger
  ");
ca735b3aa   Eric Leblond   netfilter: use a ...
1061
1062
  		goto cleanup_subsys;
  	}
0597f2680   Harald Welte   [NETFILTER]: Add ...
1063
  	return status;
0597f2680   Harald Welte   [NETFILTER]: Add ...
1064
  cleanup_subsys:
0597f2680   Harald Welte   [NETFILTER]: Add ...
1065
1066
1067
  	nfnetlink_subsys_unregister(&nfulnl_subsys);
  cleanup_netlink_notifier:
  	netlink_unregister_notifier(&nfulnl_rtnl_notifier);
3bfe04980   Francesco Ruggeri   netfilter: nfnetl...
1068
1069
  	unregister_pernet_subsys(&nfnl_log_net_ops);
  out:
0597f2680   Harald Welte   [NETFILTER]: Add ...
1070
1071
  	return status;
  }
65b4b4e81   Andrew Morton   [NETFILTER]: Rena...
1072
  static void __exit nfnetlink_log_fini(void)
0597f2680   Harald Welte   [NETFILTER]: Add ...
1073
  {
32292a7ff   Patrick McHardy   [NETFILTER]: Fix ...
1074
1075
  	nfnetlink_subsys_unregister(&nfulnl_subsys);
  	netlink_unregister_notifier(&nfulnl_rtnl_notifier);
3bfe04980   Francesco Ruggeri   netfilter: nfnetl...
1076
  	unregister_pernet_subsys(&nfnl_log_net_ops);
c83fa1960   Florian Westphal   netfilter: nf_log...
1077
  	nf_log_unregister(&nfulnl_logger);
0597f2680   Harald Welte   [NETFILTER]: Add ...
1078
1079
1080
1081
1082
  }
  
  MODULE_DESCRIPTION("netfilter userspace logging");
  MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
  MODULE_LICENSE("GPL");
f682faefb   Harald Welte   [NETFILTER]: fix ...
1083
  MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_ULOG);
fab4085f4   Pablo Neira Ayuso   netfilter: log: n...
1084
1085
1086
  MODULE_ALIAS_NF_LOGGER(AF_INET, 1);
  MODULE_ALIAS_NF_LOGGER(AF_INET6, 1);
  MODULE_ALIAS_NF_LOGGER(AF_BRIDGE, 1);
2497b8462   Liping Zhang   netfilter: nfnetl...
1087
  MODULE_ALIAS_NF_LOGGER(3, 1); /* NFPROTO_ARP */
a7647080d   Liping Zhang   netfilter: nfnetl...
1088
  MODULE_ALIAS_NF_LOGGER(5, 1); /* NFPROTO_NETDEV */
0597f2680   Harald Welte   [NETFILTER]: Add ...
1089

65b4b4e81   Andrew Morton   [NETFILTER]: Rena...
1090
1091
  module_init(nfnetlink_log_init);
  module_exit(nfnetlink_log_fini);