Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * net/sched/em_ipset.c ipset ematch
0004  *
0005  * Copyright (c) 2012 Florian Westphal <fw@strlen.de>
0006  */
0007 
0008 #include <linux/gfp.h>
0009 #include <linux/module.h>
0010 #include <linux/types.h>
0011 #include <linux/kernel.h>
0012 #include <linux/string.h>
0013 #include <linux/skbuff.h>
0014 #include <linux/netfilter/xt_set.h>
0015 #include <linux/ipv6.h>
0016 #include <net/ip.h>
0017 #include <net/pkt_cls.h>
0018 
0019 static int em_ipset_change(struct net *net, void *data, int data_len,
0020                struct tcf_ematch *em)
0021 {
0022     struct xt_set_info *set = data;
0023     ip_set_id_t index;
0024 
0025     if (data_len != sizeof(*set))
0026         return -EINVAL;
0027 
0028     index = ip_set_nfnl_get_byindex(net, set->index);
0029     if (index == IPSET_INVALID_ID)
0030         return -ENOENT;
0031 
0032     em->datalen = sizeof(*set);
0033     em->data = (unsigned long)kmemdup(data, em->datalen, GFP_KERNEL);
0034     if (em->data)
0035         return 0;
0036 
0037     ip_set_nfnl_put(net, index);
0038     return -ENOMEM;
0039 }
0040 
0041 static void em_ipset_destroy(struct tcf_ematch *em)
0042 {
0043     const struct xt_set_info *set = (const void *) em->data;
0044     if (set) {
0045         ip_set_nfnl_put(em->net, set->index);
0046         kfree((void *) em->data);
0047     }
0048 }
0049 
0050 static int em_ipset_match(struct sk_buff *skb, struct tcf_ematch *em,
0051               struct tcf_pkt_info *info)
0052 {
0053     struct ip_set_adt_opt opt;
0054     struct xt_action_param acpar;
0055     const struct xt_set_info *set = (const void *) em->data;
0056     struct net_device *dev, *indev = NULL;
0057     struct nf_hook_state state = {
0058         .net    = em->net,
0059     };
0060     int ret, network_offset;
0061 
0062     switch (skb_protocol(skb, true)) {
0063     case htons(ETH_P_IP):
0064         state.pf = NFPROTO_IPV4;
0065         if (!pskb_network_may_pull(skb, sizeof(struct iphdr)))
0066             return 0;
0067         acpar.thoff = ip_hdrlen(skb);
0068         break;
0069     case htons(ETH_P_IPV6):
0070         state.pf = NFPROTO_IPV6;
0071         if (!pskb_network_may_pull(skb, sizeof(struct ipv6hdr)))
0072             return 0;
0073         /* doesn't call ipv6_find_hdr() because ipset doesn't use thoff, yet */
0074         acpar.thoff = sizeof(struct ipv6hdr);
0075         break;
0076     default:
0077         return 0;
0078     }
0079 
0080     opt.family = state.pf;
0081     opt.dim = set->dim;
0082     opt.flags = set->flags;
0083     opt.cmdflags = 0;
0084     opt.ext.timeout = ~0u;
0085 
0086     network_offset = skb_network_offset(skb);
0087     skb_pull(skb, network_offset);
0088 
0089     dev = skb->dev;
0090 
0091     rcu_read_lock();
0092 
0093     if (skb->skb_iif)
0094         indev = dev_get_by_index_rcu(em->net, skb->skb_iif);
0095 
0096     state.in      = indev ? indev : dev;
0097     state.out     = dev;
0098     acpar.state   = &state;
0099 
0100     ret = ip_set_test(set->index, skb, &acpar, &opt);
0101 
0102     rcu_read_unlock();
0103 
0104     skb_push(skb, network_offset);
0105     return ret;
0106 }
0107 
0108 static struct tcf_ematch_ops em_ipset_ops = {
0109     .kind     = TCF_EM_IPSET,
0110     .change   = em_ipset_change,
0111     .destroy  = em_ipset_destroy,
0112     .match    = em_ipset_match,
0113     .owner    = THIS_MODULE,
0114     .link     = LIST_HEAD_INIT(em_ipset_ops.link)
0115 };
0116 
0117 static int __init init_em_ipset(void)
0118 {
0119     return tcf_em_register(&em_ipset_ops);
0120 }
0121 
0122 static void __exit exit_em_ipset(void)
0123 {
0124     tcf_em_unregister(&em_ipset_ops);
0125 }
0126 
0127 MODULE_LICENSE("GPL");
0128 MODULE_AUTHOR("Florian Westphal <fw@strlen.de>");
0129 MODULE_DESCRIPTION("TC extended match for IP sets");
0130 
0131 module_init(init_em_ipset);
0132 module_exit(exit_em_ipset);
0133 
0134 MODULE_ALIAS_TCF_EMATCH(TCF_EM_IPSET);