1 /* Copyright (C) 2016 Tomasz Chilinski <tomasz.chilinski@chilan.com>
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 as
5 * published by the Free Software Foundation.
8 /* Kernel module implementing an IP set type: the hash:ip,mac type */
10 #include <linux/jhash.h>
11 #include <linux/module.h>
13 #include <linux/etherdevice.h>
14 #include <linux/skbuff.h>
15 #include <linux/errno.h>
16 #include <linux/random.h>
17 #include <linux/if_ether.h>
20 #include <net/netlink.h>
23 #include <linux/netfilter.h>
24 #include <linux/netfilter/ipset/pfxlen.h>
25 #include <linux/netfilter/ipset/ip_set.h>
26 #include <linux/netfilter/ipset/ip_set_hash.h>
28 #define IPSET_TYPE_REV_MIN 0
29 #define IPSET_TYPE_REV_MAX 0
31 MODULE_LICENSE("GPL");
32 MODULE_AUTHOR("Tomasz Chilinski <tomasz.chilinski@chilan.com>");
33 IP_SET_MODULE_DESC("hash:ip,mac", IPSET_TYPE_REV_MIN
, IPSET_TYPE_REV_MAX
);
34 MODULE_ALIAS("ip_set_hash:ip,mac");
36 /* Type specific function prefix */
37 #define HTYPE hash_ipmac
42 struct hash_ipmac4_elem
{
43 /* Zero valued IP addresses cannot be stored */
46 unsigned char ether
[ETH_ALEN
];
51 /* Common functions */
54 hash_ipmac4_data_equal(const struct hash_ipmac4_elem
*e1
,
55 const struct hash_ipmac4_elem
*e2
,
58 return e1
->ip
== e2
->ip
&& ether_addr_equal(e1
->ether
, e2
->ether
);
62 hash_ipmac4_data_list(struct sk_buff
*skb
, const struct hash_ipmac4_elem
*e
)
64 if (nla_put_ipaddr4(skb
, IPSET_ATTR_IP
, e
->ip
) ||
65 nla_put(skb
, IPSET_ATTR_ETHER
, ETH_ALEN
, e
->ether
))
74 hash_ipmac4_data_next(struct hash_ipmac4_elem
*next
,
75 const struct hash_ipmac4_elem
*e
)
80 #define MTYPE hash_ipmac4
83 #define HKEY_DATALEN sizeof(struct hash_ipmac4_elem)
84 #include "ip_set_hash_gen.h"
87 hash_ipmac4_kadt(struct ip_set
*set
, const struct sk_buff
*skb
,
88 const struct xt_action_param
*par
,
89 enum ipset_adt adt
, struct ip_set_adt_opt
*opt
)
91 ipset_adtfn adtfn
= set
->variant
->adt
[adt
];
92 struct hash_ipmac4_elem e
= { .ip
= 0, { .foo
[0] = 0, .foo
[1] = 0 } };
93 struct ip_set_ext ext
= IP_SET_INIT_KEXT(skb
, opt
, set
);
95 if (skb_mac_header(skb
) < skb
->head
||
96 (skb_mac_header(skb
) + ETH_HLEN
) > skb
->data
)
99 if (opt
->flags
& IPSET_DIM_ONE_SRC
)
100 ether_addr_copy(e
.ether
, eth_hdr(skb
)->h_source
);
102 ether_addr_copy(e
.ether
, eth_hdr(skb
)->h_dest
);
104 if (is_zero_ether_addr(e
.ether
))
107 ip4addrptr(skb
, opt
->flags
& IPSET_DIM_ONE_SRC
, &e
.ip
);
109 return adtfn(set
, &e
, &ext
, &opt
->ext
, opt
->cmdflags
);
113 hash_ipmac4_uadt(struct ip_set
*set
, struct nlattr
*tb
[],
114 enum ipset_adt adt
, u32
*lineno
, u32 flags
, bool retried
)
116 ipset_adtfn adtfn
= set
->variant
->adt
[adt
];
117 struct hash_ipmac4_elem e
= { .ip
= 0, { .foo
[0] = 0, .foo
[1] = 0 } };
118 struct ip_set_ext ext
= IP_SET_INIT_UEXT(set
);
121 if (unlikely(!tb
[IPSET_ATTR_IP
] ||
122 !tb
[IPSET_ATTR_ETHER
] ||
123 nla_len(tb
[IPSET_ATTR_ETHER
]) != ETH_ALEN
||
124 !ip_set_optattr_netorder(tb
, IPSET_ATTR_TIMEOUT
) ||
125 !ip_set_optattr_netorder(tb
, IPSET_ATTR_PACKETS
) ||
126 !ip_set_optattr_netorder(tb
, IPSET_ATTR_BYTES
) ||
127 !ip_set_optattr_netorder(tb
, IPSET_ATTR_SKBMARK
) ||
128 !ip_set_optattr_netorder(tb
, IPSET_ATTR_SKBPRIO
) ||
129 !ip_set_optattr_netorder(tb
, IPSET_ATTR_SKBQUEUE
)))
130 return -IPSET_ERR_PROTOCOL
;
132 if (tb
[IPSET_ATTR_LINENO
])
133 *lineno
= nla_get_u32(tb
[IPSET_ATTR_LINENO
]);
135 ret
= ip_set_get_ipaddr4(tb
[IPSET_ATTR_IP
], &e
.ip
) ||
136 ip_set_get_extensions(set
, tb
, &ext
);
139 memcpy(e
.ether
, nla_data(tb
[IPSET_ATTR_ETHER
]), ETH_ALEN
);
140 if (is_zero_ether_addr(e
.ether
))
141 return -IPSET_ERR_HASH_ELEM
;
143 return adtfn(set
, &e
, &ext
, &ext
, flags
);
148 /* Member elements */
149 struct hash_ipmac6_elem
{
150 /* Zero valued IP addresses cannot be stored */
151 union nf_inet_addr ip
;
153 unsigned char ether
[ETH_ALEN
];
158 /* Common functions */
161 hash_ipmac6_data_equal(const struct hash_ipmac6_elem
*e1
,
162 const struct hash_ipmac6_elem
*e2
,
165 return ipv6_addr_equal(&e1
->ip
.in6
, &e2
->ip
.in6
) &&
166 ether_addr_equal(e1
->ether
, e2
->ether
);
170 hash_ipmac6_data_list(struct sk_buff
*skb
, const struct hash_ipmac6_elem
*e
)
172 if (nla_put_ipaddr6(skb
, IPSET_ATTR_IP
, &e
->ip
.in6
) ||
173 nla_put(skb
, IPSET_ATTR_ETHER
, ETH_ALEN
, e
->ether
))
174 goto nla_put_failure
;
182 hash_ipmac6_data_next(struct hash_ipmac6_elem
*next
,
183 const struct hash_ipmac6_elem
*e
)
192 #define MTYPE hash_ipmac6
194 #define HOST_MASK 128
195 #define HKEY_DATALEN sizeof(struct hash_ipmac6_elem)
196 #define IP_SET_EMIT_CREATE
197 #include "ip_set_hash_gen.h"
200 hash_ipmac6_kadt(struct ip_set
*set
, const struct sk_buff
*skb
,
201 const struct xt_action_param
*par
,
202 enum ipset_adt adt
, struct ip_set_adt_opt
*opt
)
204 ipset_adtfn adtfn
= set
->variant
->adt
[adt
];
205 struct hash_ipmac6_elem e
= {
207 { .foo
[0] = 0, .foo
[1] = 0 }
209 struct ip_set_ext ext
= IP_SET_INIT_KEXT(skb
, opt
, set
);
211 if (skb_mac_header(skb
) < skb
->head
||
212 (skb_mac_header(skb
) + ETH_HLEN
) > skb
->data
)
215 if (opt
->flags
& IPSET_DIM_ONE_SRC
)
216 ether_addr_copy(e
.ether
, eth_hdr(skb
)->h_source
);
218 ether_addr_copy(e
.ether
, eth_hdr(skb
)->h_dest
);
220 if (is_zero_ether_addr(e
.ether
))
223 ip6addrptr(skb
, opt
->flags
& IPSET_DIM_ONE_SRC
, &e
.ip
.in6
);
225 return adtfn(set
, &e
, &ext
, &opt
->ext
, opt
->cmdflags
);
229 hash_ipmac6_uadt(struct ip_set
*set
, struct nlattr
*tb
[],
230 enum ipset_adt adt
, u32
*lineno
, u32 flags
, bool retried
)
232 ipset_adtfn adtfn
= set
->variant
->adt
[adt
];
233 struct hash_ipmac6_elem e
= {
235 { .foo
[0] = 0, .foo
[1] = 0 }
237 struct ip_set_ext ext
= IP_SET_INIT_UEXT(set
);
240 if (unlikely(!tb
[IPSET_ATTR_IP
] ||
241 !tb
[IPSET_ATTR_ETHER
] ||
242 nla_len(tb
[IPSET_ATTR_ETHER
]) != ETH_ALEN
||
243 !ip_set_optattr_netorder(tb
, IPSET_ATTR_TIMEOUT
) ||
244 !ip_set_optattr_netorder(tb
, IPSET_ATTR_PACKETS
) ||
245 !ip_set_optattr_netorder(tb
, IPSET_ATTR_BYTES
) ||
246 !ip_set_optattr_netorder(tb
, IPSET_ATTR_SKBMARK
) ||
247 !ip_set_optattr_netorder(tb
, IPSET_ATTR_SKBPRIO
) ||
248 !ip_set_optattr_netorder(tb
, IPSET_ATTR_SKBQUEUE
)))
249 return -IPSET_ERR_PROTOCOL
;
251 if (tb
[IPSET_ATTR_LINENO
])
252 *lineno
= nla_get_u32(tb
[IPSET_ATTR_LINENO
]);
254 ret
= ip_set_get_ipaddr6(tb
[IPSET_ATTR_IP
], &e
.ip
) ||
255 ip_set_get_extensions(set
, tb
, &ext
);
259 memcpy(e
.ether
, nla_data(tb
[IPSET_ATTR_ETHER
]), ETH_ALEN
);
260 if (is_zero_ether_addr(e
.ether
))
261 return -IPSET_ERR_HASH_ELEM
;
263 return adtfn(set
, &e
, &ext
, &ext
, flags
);
266 static struct ip_set_type hash_ipmac_type __read_mostly
= {
267 .name
= "hash:ip,mac",
268 .protocol
= IPSET_PROTOCOL
,
269 .features
= IPSET_TYPE_IP
| IPSET_TYPE_MAC
,
270 .dimension
= IPSET_DIM_TWO
,
271 .family
= NFPROTO_UNSPEC
,
272 .revision_min
= IPSET_TYPE_REV_MIN
,
273 .revision_max
= IPSET_TYPE_REV_MAX
,
274 .create
= hash_ipmac_create
,
276 [IPSET_ATTR_HASHSIZE
] = { .type
= NLA_U32
},
277 [IPSET_ATTR_MAXELEM
] = { .type
= NLA_U32
},
278 [IPSET_ATTR_PROBES
] = { .type
= NLA_U8
},
279 [IPSET_ATTR_RESIZE
] = { .type
= NLA_U8
},
280 [IPSET_ATTR_TIMEOUT
] = { .type
= NLA_U32
},
281 [IPSET_ATTR_CADT_FLAGS
] = { .type
= NLA_U32
},
284 [IPSET_ATTR_IP
] = { .type
= NLA_NESTED
},
285 [IPSET_ATTR_ETHER
] = { .type
= NLA_BINARY
,
287 [IPSET_ATTR_TIMEOUT
] = { .type
= NLA_U32
},
288 [IPSET_ATTR_LINENO
] = { .type
= NLA_U32
},
289 [IPSET_ATTR_BYTES
] = { .type
= NLA_U64
},
290 [IPSET_ATTR_PACKETS
] = { .type
= NLA_U64
},
291 [IPSET_ATTR_COMMENT
] = { .type
= NLA_NUL_STRING
},
292 [IPSET_ATTR_SKBMARK
] = { .type
= NLA_U64
},
293 [IPSET_ATTR_SKBPRIO
] = { .type
= NLA_U32
},
294 [IPSET_ATTR_SKBQUEUE
] = { .type
= NLA_U16
},
300 hash_ipmac_init(void)
302 return ip_set_type_register(&hash_ipmac_type
);
306 hash_ipmac_fini(void)
308 ip_set_type_unregister(&hash_ipmac_type
);
311 module_init(hash_ipmac_init
);
312 module_exit(hash_ipmac_fini
);