blob: c66ca9400ab4f03d96bc287381a935546abfe8fa [file] [log] [blame]
Florian Westphal6d4fa852012-07-11 10:56:57 +00001/*
2 * net/sched/em_ipset.c ipset ematch
3 *
4 * Copyright (c) 2012 Florian Westphal <fw@strlen.de>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * version 2 as published by the Free Software Foundation.
9 */
10
11#include <linux/gfp.h>
12#include <linux/module.h>
13#include <linux/types.h>
14#include <linux/kernel.h>
15#include <linux/string.h>
16#include <linux/skbuff.h>
17#include <linux/netfilter/xt_set.h>
18#include <linux/ipv6.h>
19#include <net/ip.h>
20#include <net/pkt_cls.h>
21
John Fastabend82a470f2014-10-05 21:27:53 -070022static int em_ipset_change(struct net *net, void *data, int data_len,
Florian Westphal6d4fa852012-07-11 10:56:57 +000023 struct tcf_ematch *em)
24{
25 struct xt_set_info *set = data;
26 ip_set_id_t index;
27
28 if (data_len != sizeof(*set))
29 return -EINVAL;
30
Vitaly Lavrov1785e8f2013-09-30 17:07:02 +020031 index = ip_set_nfnl_get_byindex(net, set->index);
Florian Westphal6d4fa852012-07-11 10:56:57 +000032 if (index == IPSET_INVALID_ID)
33 return -ENOENT;
34
35 em->datalen = sizeof(*set);
36 em->data = (unsigned long)kmemdup(data, em->datalen, GFP_KERNEL);
37 if (em->data)
38 return 0;
39
Vitaly Lavrov1785e8f2013-09-30 17:07:02 +020040 ip_set_nfnl_put(net, index);
Florian Westphal6d4fa852012-07-11 10:56:57 +000041 return -ENOMEM;
42}
43
John Fastabend82a470f2014-10-05 21:27:53 -070044static void em_ipset_destroy(struct tcf_ematch *em)
Florian Westphal6d4fa852012-07-11 10:56:57 +000045{
46 const struct xt_set_info *set = (const void *) em->data;
47 if (set) {
John Fastabend82a470f2014-10-05 21:27:53 -070048 ip_set_nfnl_put(em->net, set->index);
Florian Westphal6d4fa852012-07-11 10:56:57 +000049 kfree((void *) em->data);
50 }
51}
52
53static int em_ipset_match(struct sk_buff *skb, struct tcf_ematch *em,
54 struct tcf_pkt_info *info)
55{
56 struct ip_set_adt_opt opt;
57 struct xt_action_param acpar;
58 const struct xt_set_info *set = (const void *) em->data;
59 struct net_device *dev, *indev = NULL;
60 int ret, network_offset;
61
Jiri Pirkod8b96052015-01-13 17:13:43 +010062 switch (tc_skb_protocol(skb)) {
Florian Westphal6d4fa852012-07-11 10:56:57 +000063 case htons(ETH_P_IP):
64 acpar.family = NFPROTO_IPV4;
65 if (!pskb_network_may_pull(skb, sizeof(struct iphdr)))
66 return 0;
67 acpar.thoff = ip_hdrlen(skb);
68 break;
69 case htons(ETH_P_IPV6):
70 acpar.family = NFPROTO_IPV6;
71 if (!pskb_network_may_pull(skb, sizeof(struct ipv6hdr)))
72 return 0;
73 /* doesn't call ipv6_find_hdr() because ipset doesn't use thoff, yet */
74 acpar.thoff = sizeof(struct ipv6hdr);
75 break;
76 default:
77 return 0;
78 }
79
80 acpar.hooknum = 0;
81
82 opt.family = acpar.family;
83 opt.dim = set->dim;
84 opt.flags = set->flags;
85 opt.cmdflags = 0;
Jozsef Kadlecsik075e64c2013-04-27 14:28:55 +020086 opt.ext.timeout = ~0u;
Florian Westphal6d4fa852012-07-11 10:56:57 +000087
88 network_offset = skb_network_offset(skb);
89 skb_pull(skb, network_offset);
90
91 dev = skb->dev;
92
93 rcu_read_lock();
94
Eric W. Biederman17cebfd2015-06-17 10:28:17 -050095 if (skb->skb_iif)
96 indev = dev_get_by_index_rcu(em->net, skb->skb_iif);
Florian Westphal6d4fa852012-07-11 10:56:57 +000097
Eric W. Biederman156c1962015-09-18 14:32:58 -050098 acpar.net = em->net;
Florian Westphal6d4fa852012-07-11 10:56:57 +000099 acpar.in = indev ? indev : dev;
100 acpar.out = dev;
101
102 ret = ip_set_test(set->index, skb, &acpar, &opt);
103
104 rcu_read_unlock();
105
106 skb_push(skb, network_offset);
107 return ret;
108}
109
110static struct tcf_ematch_ops em_ipset_ops = {
111 .kind = TCF_EM_IPSET,
112 .change = em_ipset_change,
113 .destroy = em_ipset_destroy,
114 .match = em_ipset_match,
115 .owner = THIS_MODULE,
116 .link = LIST_HEAD_INIT(em_ipset_ops.link)
117};
118
119static int __init init_em_ipset(void)
120{
121 return tcf_em_register(&em_ipset_ops);
122}
123
124static void __exit exit_em_ipset(void)
125{
126 tcf_em_unregister(&em_ipset_ops);
127}
128
129MODULE_LICENSE("GPL");
130MODULE_AUTHOR("Florian Westphal <fw@strlen.de>");
131MODULE_DESCRIPTION("TC extended match for IP sets");
132
133module_init(init_em_ipset);
134module_exit(exit_em_ipset);
135
136MODULE_ALIAS_TCF_EMATCH(TCF_EM_IPSET);