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_netdev_set_pktinfo_ipv4(struct nft_pktinfo *pkt,
20 const struct nf_hook_ops *ops, struct sk_buff *skb,
21 const struct nf_hook_state *state)
23 struct iphdr *iph, _iph;
26 nft_set_pktinfo(pkt, ops, skb, state);
28 iph = skb_header_pointer(skb, skb_network_offset(skb), sizeof(*iph),
34 if (iph->ihl < 5 || iph->version != 4)
37 len = ntohs(iph->tot_len);
44 pkt->tprot = iph->protocol;
45 pkt->xt.thoff = thoff;
46 pkt->xt.fragoff = ntohs(iph->frag_off) & IP_OFFSET;
50 __nft_netdev_set_pktinfo_ipv6(struct nft_pktinfo *pkt,
51 const struct nf_hook_ops *ops,
53 const struct nf_hook_state *state)
55 #if IS_ENABLED(CONFIG_IPV6)
56 struct ipv6hdr *ip6h, _ip6h;
57 unsigned int thoff = 0;
58 unsigned short frag_off;
62 ip6h = skb_header_pointer(skb, skb_network_offset(skb), sizeof(*ip6h),
67 if (ip6h->version != 6)
70 pkt_len = ntohs(ip6h->payload_len);
71 if (pkt_len + sizeof(*ip6h) > skb->len)
74 protohdr = ipv6_find_hdr(pkt->skb, &thoff, -1, &frag_off, NULL);
78 pkt->tprot = protohdr;
79 pkt->xt.thoff = thoff;
80 pkt->xt.fragoff = frag_off;
84 static inline void nft_netdev_set_pktinfo_ipv6(struct nft_pktinfo *pkt,
85 const struct nf_hook_ops *ops,
87 const struct nf_hook_state *state)
89 nft_set_pktinfo(pkt, ops, skb, state);
90 __nft_netdev_set_pktinfo_ipv6(pkt, ops, skb, state);
94 nft_do_chain_netdev(const struct nf_hook_ops *ops, struct sk_buff *skb,
95 const struct nf_hook_state *state)
97 struct nft_pktinfo pkt;
99 switch (eth_hdr(skb)->h_proto) {
100 case htons(ETH_P_IP):
101 nft_netdev_set_pktinfo_ipv4(&pkt, ops, skb, state);
103 case htons(ETH_P_IPV6):
104 nft_netdev_set_pktinfo_ipv6(&pkt, ops, skb, state);
107 nft_set_pktinfo(&pkt, ops, skb, state);
111 return nft_do_chain(&pkt, ops);
114 static struct nft_af_info nft_af_netdev __read_mostly = {
115 .family = NFPROTO_NETDEV,
116 .nhooks = NF_NETDEV_NUMHOOKS,
117 .owner = THIS_MODULE,
118 .flags = NFT_AF_NEEDS_DEV,
121 [NF_NETDEV_INGRESS] = nft_do_chain_netdev,
125 static int nf_tables_netdev_init_net(struct net *net)
127 net->nft.netdev = kmalloc(sizeof(struct nft_af_info), GFP_KERNEL);
128 if (net->nft.netdev == NULL)
131 memcpy(net->nft.netdev, &nft_af_netdev, sizeof(nft_af_netdev));
133 if (nft_register_afinfo(net, net->nft.netdev) < 0)
138 kfree(net->nft.netdev);
142 static void nf_tables_netdev_exit_net(struct net *net)
144 nft_unregister_afinfo(net->nft.netdev);
145 kfree(net->nft.netdev);
148 static struct pernet_operations nf_tables_netdev_net_ops = {
149 .init = nf_tables_netdev_init_net,
150 .exit = nf_tables_netdev_exit_net,
153 static const struct nf_chain_type nft_filter_chain_netdev = {
155 .type = NFT_CHAIN_T_DEFAULT,
156 .family = NFPROTO_NETDEV,
157 .owner = THIS_MODULE,
158 .hook_mask = (1 << NF_NETDEV_INGRESS),
161 static void nft_netdev_event(unsigned long event, struct nft_af_info *afi,
162 struct net_device *dev, struct nft_table *table,
163 struct nft_base_chain *basechain)
166 case NETDEV_REGISTER:
167 if (strcmp(basechain->dev_name, dev->name) != 0)
170 BUG_ON(!(basechain->flags & NFT_BASECHAIN_DISABLED));
173 basechain->ops[0].dev = dev;
174 basechain->flags &= ~NFT_BASECHAIN_DISABLED;
175 if (!(table->flags & NFT_TABLE_F_DORMANT))
176 nft_register_basechain(basechain, afi->nops);
178 case NETDEV_UNREGISTER:
179 if (strcmp(basechain->dev_name, dev->name) != 0)
182 BUG_ON(basechain->flags & NFT_BASECHAIN_DISABLED);
184 if (!(table->flags & NFT_TABLE_F_DORMANT))
185 nft_unregister_basechain(basechain, afi->nops);
187 dev_put(basechain->ops[0].dev);
188 basechain->ops[0].dev = NULL;
189 basechain->flags |= NFT_BASECHAIN_DISABLED;
191 case NETDEV_CHANGENAME:
192 if (dev->ifindex != basechain->ops[0].dev->ifindex)
195 strncpy(basechain->dev_name, dev->name, IFNAMSIZ);
200 static int nf_tables_netdev_event(struct notifier_block *this,
201 unsigned long event, void *ptr)
203 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
204 struct nft_af_info *afi;
205 struct nft_table *table;
206 struct nft_chain *chain;
208 nfnl_lock(NFNL_SUBSYS_NFTABLES);
209 list_for_each_entry(afi, &dev_net(dev)->nft.af_info, list) {
210 if (afi->family != NFPROTO_NETDEV)
213 list_for_each_entry(table, &afi->tables, list) {
214 list_for_each_entry(chain, &table->chains, list) {
215 if (!(chain->flags & NFT_BASE_CHAIN))
218 nft_netdev_event(event, afi, dev, table,
219 nft_base_chain(chain));
223 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
228 static struct notifier_block nf_tables_netdev_notifier = {
229 .notifier_call = nf_tables_netdev_event,
232 static int __init nf_tables_netdev_init(void)
236 nft_register_chain_type(&nft_filter_chain_netdev);
237 ret = register_pernet_subsys(&nf_tables_netdev_net_ops);
239 nft_unregister_chain_type(&nft_filter_chain_netdev);
241 register_netdevice_notifier(&nf_tables_netdev_notifier);
246 static void __exit nf_tables_netdev_exit(void)
248 unregister_netdevice_notifier(&nf_tables_netdev_notifier);
249 unregister_pernet_subsys(&nf_tables_netdev_net_ops);
250 nft_unregister_chain_type(&nft_filter_chain_netdev);
253 module_init(nf_tables_netdev_init);
254 module_exit(nf_tables_netdev_exit);
256 MODULE_LICENSE("GPL");
257 MODULE_AUTHOR("Pablo Neira Ayuso <pablo@netfilter.org>");
258 MODULE_ALIAS_NFT_FAMILY(5); /* NFPROTO_NETDEV */