xref: /linux/net/netfilter/ipset/ip_set_hash_mac.c (revision 44f57d78)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (C) 2014 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
3  */
4 
5 /* Kernel module implementing an IP set type: the hash:mac type */
6 
7 #include <linux/jhash.h>
8 #include <linux/module.h>
9 #include <linux/etherdevice.h>
10 #include <linux/skbuff.h>
11 #include <linux/errno.h>
12 #include <linux/if_ether.h>
13 #include <net/netlink.h>
14 
15 #include <linux/netfilter.h>
16 #include <linux/netfilter/ipset/ip_set.h>
17 #include <linux/netfilter/ipset/ip_set_hash.h>
18 
19 #define IPSET_TYPE_REV_MIN	0
20 #define IPSET_TYPE_REV_MAX	0
21 
22 MODULE_LICENSE("GPL");
23 MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
24 IP_SET_MODULE_DESC("hash:mac", IPSET_TYPE_REV_MIN, IPSET_TYPE_REV_MAX);
25 MODULE_ALIAS("ip_set_hash:mac");
26 
27 /* Type specific function prefix */
28 #define HTYPE		hash_mac
29 
30 /* Member elements */
31 struct hash_mac4_elem {
32 	/* Zero valued IP addresses cannot be stored */
33 	union {
34 		unsigned char ether[ETH_ALEN];
35 		__be32 foo[2];
36 	};
37 };
38 
39 /* Common functions */
40 
41 static inline bool
42 hash_mac4_data_equal(const struct hash_mac4_elem *e1,
43 		     const struct hash_mac4_elem *e2,
44 		     u32 *multi)
45 {
46 	return ether_addr_equal(e1->ether, e2->ether);
47 }
48 
49 static inline bool
50 hash_mac4_data_list(struct sk_buff *skb, const struct hash_mac4_elem *e)
51 {
52 	if (nla_put(skb, IPSET_ATTR_ETHER, ETH_ALEN, e->ether))
53 		goto nla_put_failure;
54 	return false;
55 
56 nla_put_failure:
57 	return true;
58 }
59 
60 static inline void
61 hash_mac4_data_next(struct hash_mac4_elem *next,
62 		    const struct hash_mac4_elem *e)
63 {
64 }
65 
66 #define MTYPE		hash_mac4
67 #define HOST_MASK	32
68 #define IP_SET_EMIT_CREATE
69 #define IP_SET_PROTO_UNDEF
70 #include "ip_set_hash_gen.h"
71 
72 static int
73 hash_mac4_kadt(struct ip_set *set, const struct sk_buff *skb,
74 	       const struct xt_action_param *par,
75 	       enum ipset_adt adt, struct ip_set_adt_opt *opt)
76 {
77 	ipset_adtfn adtfn = set->variant->adt[adt];
78 	struct hash_mac4_elem e = { { .foo[0] = 0, .foo[1] = 0 } };
79 	struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
80 
81 	if (skb_mac_header(skb) < skb->head ||
82 	    (skb_mac_header(skb) + ETH_HLEN) > skb->data)
83 		return -EINVAL;
84 
85 	if (opt->flags & IPSET_DIM_ONE_SRC)
86 		ether_addr_copy(e.ether, eth_hdr(skb)->h_source);
87 	else
88 		ether_addr_copy(e.ether, eth_hdr(skb)->h_dest);
89 
90 	if (is_zero_ether_addr(e.ether))
91 		return -EINVAL;
92 	return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags);
93 }
94 
95 static int
96 hash_mac4_uadt(struct ip_set *set, struct nlattr *tb[],
97 	       enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
98 {
99 	ipset_adtfn adtfn = set->variant->adt[adt];
100 	struct hash_mac4_elem e = { { .foo[0] = 0, .foo[1] = 0 } };
101 	struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
102 	int ret;
103 
104 	if (tb[IPSET_ATTR_LINENO])
105 		*lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
106 
107 	if (unlikely(!tb[IPSET_ATTR_ETHER] ||
108 		     nla_len(tb[IPSET_ATTR_ETHER]) != ETH_ALEN))
109 		return -IPSET_ERR_PROTOCOL;
110 
111 	ret = ip_set_get_extensions(set, tb, &ext);
112 	if (ret)
113 		return ret;
114 	ether_addr_copy(e.ether, nla_data(tb[IPSET_ATTR_ETHER]));
115 	if (is_zero_ether_addr(e.ether))
116 		return -IPSET_ERR_HASH_ELEM;
117 
118 	return adtfn(set, &e, &ext, &ext, flags);
119 }
120 
121 static struct ip_set_type hash_mac_type __read_mostly = {
122 	.name		= "hash:mac",
123 	.protocol	= IPSET_PROTOCOL,
124 	.features	= IPSET_TYPE_MAC,
125 	.dimension	= IPSET_DIM_ONE,
126 	.family		= NFPROTO_UNSPEC,
127 	.revision_min	= IPSET_TYPE_REV_MIN,
128 	.revision_max	= IPSET_TYPE_REV_MAX,
129 	.create		= hash_mac_create,
130 	.create_policy	= {
131 		[IPSET_ATTR_HASHSIZE]	= { .type = NLA_U32 },
132 		[IPSET_ATTR_MAXELEM]	= { .type = NLA_U32 },
133 		[IPSET_ATTR_PROBES]	= { .type = NLA_U8 },
134 		[IPSET_ATTR_RESIZE]	= { .type = NLA_U8  },
135 		[IPSET_ATTR_TIMEOUT]	= { .type = NLA_U32 },
136 		[IPSET_ATTR_CADT_FLAGS]	= { .type = NLA_U32 },
137 	},
138 	.adt_policy	= {
139 		[IPSET_ATTR_ETHER]	= { .type = NLA_BINARY,
140 					    .len  = ETH_ALEN },
141 		[IPSET_ATTR_TIMEOUT]	= { .type = NLA_U32 },
142 		[IPSET_ATTR_LINENO]	= { .type = NLA_U32 },
143 		[IPSET_ATTR_BYTES]	= { .type = NLA_U64 },
144 		[IPSET_ATTR_PACKETS]	= { .type = NLA_U64 },
145 		[IPSET_ATTR_COMMENT]	= { .type = NLA_NUL_STRING,
146 					    .len  = IPSET_MAX_COMMENT_SIZE },
147 		[IPSET_ATTR_SKBMARK]	= { .type = NLA_U64 },
148 		[IPSET_ATTR_SKBPRIO]	= { .type = NLA_U32 },
149 		[IPSET_ATTR_SKBQUEUE]	= { .type = NLA_U16 },
150 	},
151 	.me		= THIS_MODULE,
152 };
153 
154 static int __init
155 hash_mac_init(void)
156 {
157 	return ip_set_type_register(&hash_mac_type);
158 }
159 
160 static void __exit
161 hash_mac_fini(void)
162 {
163 	rcu_barrier();
164 	ip_set_type_unregister(&hash_mac_type);
165 }
166 
167 module_init(hash_mac_init);
168 module_exit(hash_mac_fini);
169