Florian Westphal | 6d4fa85 | 2012-07-11 10:56:57 +0000 | [diff] [blame] | 1 | /* |
| 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 | |
| 22 | static int em_ipset_change(struct tcf_proto *tp, void *data, int data_len, |
| 23 | struct tcf_ematch *em) |
| 24 | { |
| 25 | struct xt_set_info *set = data; |
| 26 | ip_set_id_t index; |
stephen hemminger | b1eda2a | 2013-10-16 17:29:34 -0700 | [diff] [blame] | 27 | struct net *net = dev_net(qdisc_dev(tp->q)); |
Florian Westphal | 6d4fa85 | 2012-07-11 10:56:57 +0000 | [diff] [blame] | 28 | |
| 29 | if (data_len != sizeof(*set)) |
| 30 | return -EINVAL; |
| 31 | |
Vitaly Lavrov | 1785e8f | 2013-09-30 17:07:02 +0200 | [diff] [blame] | 32 | index = ip_set_nfnl_get_byindex(net, set->index); |
Florian Westphal | 6d4fa85 | 2012-07-11 10:56:57 +0000 | [diff] [blame] | 33 | if (index == IPSET_INVALID_ID) |
| 34 | return -ENOENT; |
| 35 | |
| 36 | em->datalen = sizeof(*set); |
| 37 | em->data = (unsigned long)kmemdup(data, em->datalen, GFP_KERNEL); |
| 38 | if (em->data) |
| 39 | return 0; |
| 40 | |
Vitaly Lavrov | 1785e8f | 2013-09-30 17:07:02 +0200 | [diff] [blame] | 41 | ip_set_nfnl_put(net, index); |
Florian Westphal | 6d4fa85 | 2012-07-11 10:56:57 +0000 | [diff] [blame] | 42 | return -ENOMEM; |
| 43 | } |
| 44 | |
| 45 | static void em_ipset_destroy(struct tcf_proto *p, struct tcf_ematch *em) |
| 46 | { |
| 47 | const struct xt_set_info *set = (const void *) em->data; |
| 48 | if (set) { |
stephen hemminger | b1eda2a | 2013-10-16 17:29:34 -0700 | [diff] [blame] | 49 | ip_set_nfnl_put(dev_net(qdisc_dev(p->q)), set->index); |
Florian Westphal | 6d4fa85 | 2012-07-11 10:56:57 +0000 | [diff] [blame] | 50 | kfree((void *) em->data); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | static int em_ipset_match(struct sk_buff *skb, struct tcf_ematch *em, |
| 55 | struct tcf_pkt_info *info) |
| 56 | { |
| 57 | struct ip_set_adt_opt opt; |
| 58 | struct xt_action_param acpar; |
| 59 | const struct xt_set_info *set = (const void *) em->data; |
| 60 | struct net_device *dev, *indev = NULL; |
| 61 | int ret, network_offset; |
| 62 | |
| 63 | switch (skb->protocol) { |
| 64 | case htons(ETH_P_IP): |
| 65 | acpar.family = NFPROTO_IPV4; |
| 66 | if (!pskb_network_may_pull(skb, sizeof(struct iphdr))) |
| 67 | return 0; |
| 68 | acpar.thoff = ip_hdrlen(skb); |
| 69 | break; |
| 70 | case htons(ETH_P_IPV6): |
| 71 | acpar.family = NFPROTO_IPV6; |
| 72 | if (!pskb_network_may_pull(skb, sizeof(struct ipv6hdr))) |
| 73 | return 0; |
| 74 | /* doesn't call ipv6_find_hdr() because ipset doesn't use thoff, yet */ |
| 75 | acpar.thoff = sizeof(struct ipv6hdr); |
| 76 | break; |
| 77 | default: |
| 78 | return 0; |
| 79 | } |
| 80 | |
| 81 | acpar.hooknum = 0; |
| 82 | |
| 83 | opt.family = acpar.family; |
| 84 | opt.dim = set->dim; |
| 85 | opt.flags = set->flags; |
| 86 | opt.cmdflags = 0; |
Jozsef Kadlecsik | 075e64c | 2013-04-27 14:28:55 +0200 | [diff] [blame] | 87 | opt.ext.timeout = ~0u; |
Florian Westphal | 6d4fa85 | 2012-07-11 10:56:57 +0000 | [diff] [blame] | 88 | |
| 89 | network_offset = skb_network_offset(skb); |
| 90 | skb_pull(skb, network_offset); |
| 91 | |
| 92 | dev = skb->dev; |
| 93 | |
| 94 | rcu_read_lock(); |
| 95 | |
| 96 | if (dev && skb->skb_iif) |
| 97 | indev = dev_get_by_index_rcu(dev_net(dev), skb->skb_iif); |
| 98 | |
| 99 | 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 | |
| 110 | static 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 | |
| 119 | static int __init init_em_ipset(void) |
| 120 | { |
| 121 | return tcf_em_register(&em_ipset_ops); |
| 122 | } |
| 123 | |
| 124 | static void __exit exit_em_ipset(void) |
| 125 | { |
| 126 | tcf_em_unregister(&em_ipset_ops); |
| 127 | } |
| 128 | |
| 129 | MODULE_LICENSE("GPL"); |
| 130 | MODULE_AUTHOR("Florian Westphal <fw@strlen.de>"); |
| 131 | MODULE_DESCRIPTION("TC extended match for IP sets"); |
| 132 | |
| 133 | module_init(init_em_ipset); |
| 134 | module_exit(exit_em_ipset); |
| 135 | |
| 136 | MODULE_ALIAS_TCF_EMATCH(TCF_EM_IPSET); |