1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (C) 2014 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
5 /* Kernel module implementing an IP set type: the hash:mac type */
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>
15 #include <linux/netfilter.h>
16 #include <linux/netfilter/ipset/ip_set.h>
17 #include <linux/netfilter/ipset/ip_set_hash.h>
19 #define IPSET_TYPE_REV_MIN 0
20 #define IPSET_TYPE_REV_MAX 0
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");
27 /* Type specific function prefix */
28 #define HTYPE hash_mac
31 struct hash_mac4_elem
{
32 /* Zero valued IP addresses cannot be stored */
34 unsigned char ether
[ETH_ALEN
];
39 /* Common functions */
42 hash_mac4_data_equal(const struct hash_mac4_elem
*e1
,
43 const struct hash_mac4_elem
*e2
,
46 return ether_addr_equal(e1
->ether
, e2
->ether
);
50 hash_mac4_data_list(struct sk_buff
*skb
, const struct hash_mac4_elem
*e
)
52 if (nla_put(skb
, IPSET_ATTR_ETHER
, ETH_ALEN
, e
->ether
))
61 hash_mac4_data_next(struct hash_mac4_elem
*next
,
62 const struct hash_mac4_elem
*e
)
66 #define MTYPE hash_mac4
68 #define IP_SET_EMIT_CREATE
69 #define IP_SET_PROTO_UNDEF
70 #include "ip_set_hash_gen.h"
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
)
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
);
81 if (skb_mac_header(skb
) < skb
->head
||
82 (skb_mac_header(skb
) + ETH_HLEN
) > skb
->data
)
85 if (opt
->flags
& IPSET_DIM_ONE_SRC
)
86 ether_addr_copy(e
.ether
, eth_hdr(skb
)->h_source
);
88 ether_addr_copy(e
.ether
, eth_hdr(skb
)->h_dest
);
90 if (is_zero_ether_addr(e
.ether
))
92 return adtfn(set
, &e
, &ext
, &opt
->ext
, opt
->cmdflags
);
96 hash_mac4_uadt(struct ip_set
*set
, struct nlattr
*tb
[],
97 enum ipset_adt adt
, u32
*lineno
, u32 flags
, bool retried
)
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
);
104 if (tb
[IPSET_ATTR_LINENO
])
105 *lineno
= nla_get_u32(tb
[IPSET_ATTR_LINENO
]);
107 if (unlikely(!tb
[IPSET_ATTR_ETHER
] ||
108 nla_len(tb
[IPSET_ATTR_ETHER
]) != ETH_ALEN
))
109 return -IPSET_ERR_PROTOCOL
;
111 ret
= ip_set_get_extensions(set
, tb
, &ext
);
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
;
118 return adtfn(set
, &e
, &ext
, &ext
, flags
);
121 static struct ip_set_type hash_mac_type __read_mostly
= {
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
,
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
},
139 [IPSET_ATTR_ETHER
] = { .type
= NLA_BINARY
,
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
},
157 return ip_set_type_register(&hash_mac_type
);
164 ip_set_type_unregister(&hash_mac_type
);
167 module_init(hash_mac_init
);
168 module_exit(hash_mac_fini
);