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
,
21 const struct nf_hook_state
*state
)
23 struct iphdr
*iph
, _iph
;
26 nft_set_pktinfo(pkt
, 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
,
52 const struct nf_hook_state
*state
)
54 #if IS_ENABLED(CONFIG_IPV6)
55 struct ipv6hdr
*ip6h
, _ip6h
;
56 unsigned int thoff
= 0;
57 unsigned short frag_off
;
61 ip6h
= skb_header_pointer(skb
, skb_network_offset(skb
), sizeof(*ip6h
),
66 if (ip6h
->version
!= 6)
69 pkt_len
= ntohs(ip6h
->payload_len
);
70 if (pkt_len
+ sizeof(*ip6h
) > skb
->len
)
73 protohdr
= ipv6_find_hdr(pkt
->skb
, &thoff
, -1, &frag_off
, NULL
);
77 pkt
->tprot
= protohdr
;
78 pkt
->xt
.thoff
= thoff
;
79 pkt
->xt
.fragoff
= frag_off
;
83 static inline void nft_netdev_set_pktinfo_ipv6(struct nft_pktinfo
*pkt
,
85 const struct nf_hook_state
*state
)
87 nft_set_pktinfo(pkt
, skb
, state
);
88 __nft_netdev_set_pktinfo_ipv6(pkt
, skb
, state
);
92 nft_do_chain_netdev(void *priv
, struct sk_buff
*skb
,
93 const struct nf_hook_state
*state
)
95 struct nft_pktinfo pkt
;
97 switch (skb
->protocol
) {
99 nft_netdev_set_pktinfo_ipv4(&pkt
, skb
, state
);
101 case htons(ETH_P_IPV6
):
102 nft_netdev_set_pktinfo_ipv6(&pkt
, skb
, state
);
105 nft_set_pktinfo(&pkt
, skb
, state
);
109 return nft_do_chain(&pkt
, priv
);
112 static struct nft_af_info nft_af_netdev __read_mostly
= {
113 .family
= NFPROTO_NETDEV
,
114 .nhooks
= NF_NETDEV_NUMHOOKS
,
115 .owner
= THIS_MODULE
,
116 .flags
= NFT_AF_NEEDS_DEV
,
119 [NF_NETDEV_INGRESS
] = nft_do_chain_netdev
,
123 static int nf_tables_netdev_init_net(struct net
*net
)
125 net
->nft
.netdev
= kmalloc(sizeof(struct nft_af_info
), GFP_KERNEL
);
126 if (net
->nft
.netdev
== NULL
)
129 memcpy(net
->nft
.netdev
, &nft_af_netdev
, sizeof(nft_af_netdev
));
131 if (nft_register_afinfo(net
, net
->nft
.netdev
) < 0)
136 kfree(net
->nft
.netdev
);
140 static void nf_tables_netdev_exit_net(struct net
*net
)
142 nft_unregister_afinfo(net
, net
->nft
.netdev
);
143 kfree(net
->nft
.netdev
);
146 static struct pernet_operations nf_tables_netdev_net_ops
= {
147 .init
= nf_tables_netdev_init_net
,
148 .exit
= nf_tables_netdev_exit_net
,
151 static const struct nf_chain_type nft_filter_chain_netdev
= {
153 .type
= NFT_CHAIN_T_DEFAULT
,
154 .family
= NFPROTO_NETDEV
,
155 .owner
= THIS_MODULE
,
156 .hook_mask
= (1 << NF_NETDEV_INGRESS
),
159 static void nft_netdev_event(unsigned long event
, struct net_device
*dev
,
162 struct nft_base_chain
*basechain
= nft_base_chain(ctx
->chain
);
165 case NETDEV_UNREGISTER
:
166 if (strcmp(basechain
->dev_name
, dev
->name
) != 0)
169 __nft_release_basechain(ctx
);
171 case NETDEV_CHANGENAME
:
172 if (dev
->ifindex
!= basechain
->ops
[0].dev
->ifindex
)
175 strncpy(basechain
->dev_name
, dev
->name
, IFNAMSIZ
);
180 static int nf_tables_netdev_event(struct notifier_block
*this,
181 unsigned long event
, void *ptr
)
183 struct net_device
*dev
= netdev_notifier_info_to_dev(ptr
);
184 struct nft_af_info
*afi
;
185 struct nft_table
*table
;
186 struct nft_chain
*chain
, *nr
;
187 struct nft_ctx ctx
= {
191 if (event
!= NETDEV_UNREGISTER
&&
192 event
!= NETDEV_CHANGENAME
)
195 nfnl_lock(NFNL_SUBSYS_NFTABLES
);
196 list_for_each_entry(afi
, &dev_net(dev
)->nft
.af_info
, list
) {
198 if (afi
->family
!= NFPROTO_NETDEV
)
201 list_for_each_entry(table
, &afi
->tables
, list
) {
203 list_for_each_entry_safe(chain
, nr
, &table
->chains
, list
) {
204 if (!(chain
->flags
& NFT_BASE_CHAIN
))
208 nft_netdev_event(event
, dev
, &ctx
);
212 nfnl_unlock(NFNL_SUBSYS_NFTABLES
);
217 static struct notifier_block nf_tables_netdev_notifier
= {
218 .notifier_call
= nf_tables_netdev_event
,
221 static int __init
nf_tables_netdev_init(void)
225 nft_register_chain_type(&nft_filter_chain_netdev
);
226 ret
= register_pernet_subsys(&nf_tables_netdev_net_ops
);
228 nft_unregister_chain_type(&nft_filter_chain_netdev
);
231 register_netdevice_notifier(&nf_tables_netdev_notifier
);
235 static void __exit
nf_tables_netdev_exit(void)
237 unregister_netdevice_notifier(&nf_tables_netdev_notifier
);
238 unregister_pernet_subsys(&nf_tables_netdev_net_ops
);
239 nft_unregister_chain_type(&nft_filter_chain_netdev
);
242 module_init(nf_tables_netdev_init
);
243 module_exit(nf_tables_netdev_exit
);
245 MODULE_LICENSE("GPL");
246 MODULE_AUTHOR("Pablo Neira Ayuso <pablo@netfilter.org>");
247 MODULE_ALIAS_NFT_FAMILY(5); /* NFPROTO_NETDEV */