Blame view

net/netfilter/nft_queue.c 5.35 KB
0aff078d5   Eric Leblond   netfilter: nft: a...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
  /*
   * Copyright (c) 2013 Eric Leblond <eric@regit.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.
   *
   * Development of this code partly funded by OISF
   * (http://www.openinfosecfoundation.org/)
   */
  
  #include <linux/kernel.h>
  #include <linux/init.h>
  #include <linux/module.h>
  #include <linux/netlink.h>
  #include <linux/jhash.h>
  #include <linux/netfilter.h>
  #include <linux/netfilter/nf_tables.h>
  #include <net/netfilter/nf_tables.h>
  #include <net/netfilter/nf_queue.h>
  
  static u32 jhash_initval __read_mostly;
  
  struct nft_queue {
8061bb544   Liping Zhang   netfilter: nft_qu...
25
26
27
28
  	enum nft_registers	sreg_qnum:8;
  	u16			queuenum;
  	u16			queues_total;
  	u16			flags;
0aff078d5   Eric Leblond   netfilter: nft: a...
29
30
31
  };
  
  static void nft_queue_eval(const struct nft_expr *expr,
a55e22e92   Patrick McHardy   netfilter: nf_tab...
32
  			   struct nft_regs *regs,
0aff078d5   Eric Leblond   netfilter: nft: a...
33
34
35
36
37
38
39
40
  			   const struct nft_pktinfo *pkt)
  {
  	struct nft_queue *priv = nft_expr_priv(expr);
  	u32 queue = priv->queuenum;
  	u32 ret;
  
  	if (priv->queues_total > 1) {
  		if (priv->flags & NFT_QUEUE_FLAG_CPU_FANOUT) {
c2e756ff9   Pablo Neira Ayuso   netfilter: nft_qu...
41
  			int cpu = raw_smp_processor_id();
0aff078d5   Eric Leblond   netfilter: nft: a...
42
43
44
45
  
  			queue = priv->queuenum + cpu % priv->queues_total;
  		} else {
  			queue = nfqueue_hash(pkt->skb, queue,
0e5a1c7eb   Pablo Neira Ayuso   netfilter: nf_tab...
46
  					     priv->queues_total, nft_pf(pkt),
0aff078d5   Eric Leblond   netfilter: nft: a...
47
48
49
50
51
52
53
  					     jhash_initval);
  		}
  	}
  
  	ret = NF_QUEUE_NR(queue);
  	if (priv->flags & NFT_QUEUE_FLAG_BYPASS)
  		ret |= NF_VERDICT_FLAG_QUEUE_BYPASS;
a55e22e92   Patrick McHardy   netfilter: nf_tab...
54
  	regs->verdict.code = ret;
0aff078d5   Eric Leblond   netfilter: nft: a...
55
  }
8061bb544   Liping Zhang   netfilter: nft_qu...
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
  static void nft_queue_sreg_eval(const struct nft_expr *expr,
  				struct nft_regs *regs,
  				const struct nft_pktinfo *pkt)
  {
  	struct nft_queue *priv = nft_expr_priv(expr);
  	u32 queue, ret;
  
  	queue = regs->data[priv->sreg_qnum];
  
  	ret = NF_QUEUE_NR(queue);
  	if (priv->flags & NFT_QUEUE_FLAG_BYPASS)
  		ret |= NF_VERDICT_FLAG_QUEUE_BYPASS;
  
  	regs->verdict.code = ret;
  }
0aff078d5   Eric Leblond   netfilter: nft: a...
71
72
73
74
  static const struct nla_policy nft_queue_policy[NFTA_QUEUE_MAX + 1] = {
  	[NFTA_QUEUE_NUM]	= { .type = NLA_U16 },
  	[NFTA_QUEUE_TOTAL]	= { .type = NLA_U16 },
  	[NFTA_QUEUE_FLAGS]	= { .type = NLA_U16 },
8061bb544   Liping Zhang   netfilter: nft_qu...
75
  	[NFTA_QUEUE_SREG_QNUM]	= { .type = NLA_U32 },
0aff078d5   Eric Leblond   netfilter: nft: a...
76
77
78
  };
  
  static int nft_queue_init(const struct nft_ctx *ctx,
8061bb544   Liping Zhang   netfilter: nft_qu...
79
80
  			  const struct nft_expr *expr,
  			  const struct nlattr * const tb[])
0aff078d5   Eric Leblond   netfilter: nft: a...
81
82
  {
  	struct nft_queue *priv = nft_expr_priv(expr);
fe01111d2   Liping Zhang   netfilter: nft_qu...
83
  	u32 maxid;
0aff078d5   Eric Leblond   netfilter: nft: a...
84

0aff078d5   Eric Leblond   netfilter: nft: a...
85
  	priv->queuenum = ntohs(nla_get_be16(tb[NFTA_QUEUE_NUM]));
8061bb544   Liping Zhang   netfilter: nft_qu...
86
  	if (tb[NFTA_QUEUE_TOTAL])
0aff078d5   Eric Leblond   netfilter: nft: a...
87
  		priv->queues_total = ntohs(nla_get_be16(tb[NFTA_QUEUE_TOTAL]));
fe01111d2   Liping Zhang   netfilter: nft_qu...
88
89
90
91
92
93
94
95
96
  	else
  		priv->queues_total = 1;
  
  	if (priv->queues_total == 0)
  		return -EINVAL;
  
  	maxid = priv->queues_total - 1 + priv->queuenum;
  	if (maxid > U16_MAX)
  		return -ERANGE;
8061bb544   Liping Zhang   netfilter: nft_qu...
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
  	if (tb[NFTA_QUEUE_FLAGS]) {
  		priv->flags = ntohs(nla_get_be16(tb[NFTA_QUEUE_FLAGS]));
  		if (priv->flags & ~NFT_QUEUE_FLAG_MASK)
  			return -EINVAL;
  	}
  	return 0;
  }
  
  static int nft_queue_sreg_init(const struct nft_ctx *ctx,
  			       const struct nft_expr *expr,
  			       const struct nlattr * const tb[])
  {
  	struct nft_queue *priv = nft_expr_priv(expr);
  	int err;
  
  	priv->sreg_qnum = nft_parse_register(tb[NFTA_QUEUE_SREG_QNUM]);
  	err = nft_validate_register_load(priv->sreg_qnum, sizeof(u32));
  	if (err < 0)
  		return err;
  
  	if (tb[NFTA_QUEUE_FLAGS]) {
0aff078d5   Eric Leblond   netfilter: nft: a...
118
119
120
  		priv->flags = ntohs(nla_get_be16(tb[NFTA_QUEUE_FLAGS]));
  		if (priv->flags & ~NFT_QUEUE_FLAG_MASK)
  			return -EINVAL;
8061bb544   Liping Zhang   netfilter: nft_qu...
121
122
  		if (priv->flags & NFT_QUEUE_FLAG_CPU_FANOUT)
  			return -EOPNOTSUPP;
0aff078d5   Eric Leblond   netfilter: nft: a...
123
  	}
8061bb544   Liping Zhang   netfilter: nft_qu...
124

0aff078d5   Eric Leblond   netfilter: nft: a...
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
  	return 0;
  }
  
  static int nft_queue_dump(struct sk_buff *skb, const struct nft_expr *expr)
  {
  	const struct nft_queue *priv = nft_expr_priv(expr);
  
  	if (nla_put_be16(skb, NFTA_QUEUE_NUM, htons(priv->queuenum)) ||
  	    nla_put_be16(skb, NFTA_QUEUE_TOTAL, htons(priv->queues_total)) ||
  	    nla_put_be16(skb, NFTA_QUEUE_FLAGS, htons(priv->flags)))
  		goto nla_put_failure;
  
  	return 0;
  
  nla_put_failure:
  	return -1;
  }
8061bb544   Liping Zhang   netfilter: nft_qu...
142
143
144
145
146
147
148
149
150
151
152
153
154
155
  static int
  nft_queue_sreg_dump(struct sk_buff *skb, const struct nft_expr *expr)
  {
  	const struct nft_queue *priv = nft_expr_priv(expr);
  
  	if (nft_dump_register(skb, NFTA_QUEUE_SREG_QNUM, priv->sreg_qnum) ||
  	    nla_put_be16(skb, NFTA_QUEUE_FLAGS, htons(priv->flags)))
  		goto nla_put_failure;
  
  	return 0;
  
  nla_put_failure:
  	return -1;
  }
0aff078d5   Eric Leblond   netfilter: nft: a...
156
157
158
159
160
161
162
163
  static struct nft_expr_type nft_queue_type;
  static const struct nft_expr_ops nft_queue_ops = {
  	.type		= &nft_queue_type,
  	.size		= NFT_EXPR_SIZE(sizeof(struct nft_queue)),
  	.eval		= nft_queue_eval,
  	.init		= nft_queue_init,
  	.dump		= nft_queue_dump,
  };
8061bb544   Liping Zhang   netfilter: nft_qu...
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
  static const struct nft_expr_ops nft_queue_sreg_ops = {
  	.type		= &nft_queue_type,
  	.size		= NFT_EXPR_SIZE(sizeof(struct nft_queue)),
  	.eval		= nft_queue_sreg_eval,
  	.init		= nft_queue_sreg_init,
  	.dump		= nft_queue_sreg_dump,
  };
  
  static const struct nft_expr_ops *
  nft_queue_select_ops(const struct nft_ctx *ctx,
  		     const struct nlattr * const tb[])
  {
  	if (tb[NFTA_QUEUE_NUM] && tb[NFTA_QUEUE_SREG_QNUM])
  		return ERR_PTR(-EINVAL);
  
  	init_hashrandom(&jhash_initval);
  
  	if (tb[NFTA_QUEUE_NUM])
  		return &nft_queue_ops;
  
  	if (tb[NFTA_QUEUE_SREG_QNUM])
  		return &nft_queue_sreg_ops;
  
  	return ERR_PTR(-EINVAL);
  }
0aff078d5   Eric Leblond   netfilter: nft: a...
189
190
  static struct nft_expr_type nft_queue_type __read_mostly = {
  	.name		= "queue",
d4ef38354   Arushi Singhal   netfilter: Remove...
191
  	.select_ops	= nft_queue_select_ops,
0aff078d5   Eric Leblond   netfilter: nft: a...
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
  	.policy		= nft_queue_policy,
  	.maxattr	= NFTA_QUEUE_MAX,
  	.owner		= THIS_MODULE,
  };
  
  static int __init nft_queue_module_init(void)
  {
  	return nft_register_expr(&nft_queue_type);
  }
  
  static void __exit nft_queue_module_exit(void)
  {
  	nft_unregister_expr(&nft_queue_type);
  }
  
  module_init(nft_queue_module_init);
  module_exit(nft_queue_module_exit);
  
  MODULE_LICENSE("GPL");
  MODULE_AUTHOR("Eric Leblond <eric@regit.org>");
  MODULE_ALIAS_NFT_EXPR("queue");