2 * Copyright (c) 2015 Pablo Neira Ayuso <pablo@netfilter.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
9 #include <linux/init.h>
10 #include <linux/module.h>
11 #include <linux/netdevice.h>
12 #include <net/netfilter/nf_tables.h>
14 #include <linux/ipv6.h>
15 #include <net/netfilter/nf_tables_ipv4.h>
16 #include <net/netfilter/nf_tables_ipv6.h>
19 nft_do_chain_netdev(void *priv, struct sk_buff *skb,
20 const struct nf_hook_state *state)
22 struct nft_pktinfo pkt;
24 nft_set_pktinfo(&pkt, skb, state);
26 switch (skb->protocol) {
28 nft_set_pktinfo_ipv4_validate(&pkt, skb);
30 case htons(ETH_P_IPV6):
31 nft_set_pktinfo_ipv6_validate(&pkt, skb);
34 nft_set_pktinfo_unspec(&pkt, skb);
38 return nft_do_chain(&pkt, priv);
41 static struct nft_af_info nft_af_netdev __read_mostly = {
42 .family = NFPROTO_NETDEV,
46 static int nf_tables_netdev_init_net(struct net *net)
48 net->nft.netdev = kmalloc(sizeof(struct nft_af_info), GFP_KERNEL);
49 if (net->nft.netdev == NULL)
52 memcpy(net->nft.netdev, &nft_af_netdev, sizeof(nft_af_netdev));
54 if (nft_register_afinfo(net, net->nft.netdev) < 0)
59 kfree(net->nft.netdev);
63 static void nf_tables_netdev_exit_net(struct net *net)
65 nft_unregister_afinfo(net, net->nft.netdev);
66 kfree(net->nft.netdev);
69 static struct pernet_operations nf_tables_netdev_net_ops = {
70 .init = nf_tables_netdev_init_net,
71 .exit = nf_tables_netdev_exit_net,
74 static const struct nf_chain_type nft_filter_chain_netdev = {
76 .type = NFT_CHAIN_T_DEFAULT,
77 .family = NFPROTO_NETDEV,
79 .hook_mask = (1 << NF_NETDEV_INGRESS),
81 [NF_NETDEV_INGRESS] = nft_do_chain_netdev,
85 static void nft_netdev_event(unsigned long event, struct net_device *dev,
88 struct nft_base_chain *basechain = nft_base_chain(ctx->chain);
91 case NETDEV_UNREGISTER:
92 if (strcmp(basechain->dev_name, dev->name) != 0)
95 __nft_release_basechain(ctx);
97 case NETDEV_CHANGENAME:
98 if (dev->ifindex != basechain->ops.dev->ifindex)
101 strncpy(basechain->dev_name, dev->name, IFNAMSIZ);
106 static int nf_tables_netdev_event(struct notifier_block *this,
107 unsigned long event, void *ptr)
109 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
110 struct nft_table *table;
111 struct nft_chain *chain, *nr;
112 struct nft_ctx ctx = {
116 if (event != NETDEV_UNREGISTER &&
117 event != NETDEV_CHANGENAME)
120 nfnl_lock(NFNL_SUBSYS_NFTABLES);
121 list_for_each_entry(table, &ctx.net->nft.tables, list) {
122 if (table->afi->family != NFPROTO_NETDEV)
125 ctx.family = table->afi->family;
127 list_for_each_entry_safe(chain, nr, &table->chains, list) {
128 if (!nft_is_base_chain(chain))
132 nft_netdev_event(event, dev, &ctx);
135 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
140 static struct notifier_block nf_tables_netdev_notifier = {
141 .notifier_call = nf_tables_netdev_event,
144 static int __init nf_tables_netdev_init(void)
148 ret = nft_register_chain_type(&nft_filter_chain_netdev);
152 ret = register_pernet_subsys(&nf_tables_netdev_net_ops);
156 ret = register_netdevice_notifier(&nf_tables_netdev_notifier);
163 unregister_pernet_subsys(&nf_tables_netdev_net_ops);
165 nft_unregister_chain_type(&nft_filter_chain_netdev);
169 static void __exit nf_tables_netdev_exit(void)
171 unregister_netdevice_notifier(&nf_tables_netdev_notifier);
172 unregister_pernet_subsys(&nf_tables_netdev_net_ops);
173 nft_unregister_chain_type(&nft_filter_chain_netdev);
176 module_init(nf_tables_netdev_init);
177 module_exit(nf_tables_netdev_exit);
179 MODULE_LICENSE("GPL");
180 MODULE_AUTHOR("Pablo Neira Ayuso <pablo@netfilter.org>");
181 MODULE_ALIAS_NFT_FAMILY(5); /* NFPROTO_NETDEV */