Commit | Line | Data |
---|---|---|
ed6c4136 PNA |
1 | /* |
2 | * Copyright (c) 2015 Pablo Neira Ayuso <pablo@netfilter.org> | |
3 | * | |
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. | |
7 | */ | |
8 | ||
9 | #include <linux/init.h> | |
10 | #include <linux/module.h> | |
835b8033 | 11 | #include <linux/netdevice.h> |
ed6c4136 PNA |
12 | #include <net/netfilter/nf_tables.h> |
13 | #include <linux/ip.h> | |
14 | #include <linux/ipv6.h> | |
15 | #include <net/netfilter/nf_tables_ipv4.h> | |
16 | #include <net/netfilter/nf_tables_ipv6.h> | |
17 | ||
18 | static inline void | |
19 | nft_netdev_set_pktinfo_ipv4(struct nft_pktinfo *pkt, | |
6aa187f2 | 20 | struct sk_buff *skb, |
ed6c4136 PNA |
21 | const struct nf_hook_state *state) |
22 | { | |
23 | struct iphdr *iph, _iph; | |
24 | u32 len, thoff; | |
25 | ||
6aa187f2 | 26 | nft_set_pktinfo(pkt, skb, state); |
ed6c4136 PNA |
27 | |
28 | iph = skb_header_pointer(skb, skb_network_offset(skb), sizeof(*iph), | |
29 | &_iph); | |
30 | if (!iph) | |
31 | return; | |
32 | ||
33 | iph = ip_hdr(skb); | |
34 | if (iph->ihl < 5 || iph->version != 4) | |
35 | return; | |
36 | ||
37 | len = ntohs(iph->tot_len); | |
38 | thoff = iph->ihl * 4; | |
39 | if (skb->len < len) | |
40 | return; | |
41 | else if (len < thoff) | |
42 | return; | |
43 | ||
44 | pkt->tprot = iph->protocol; | |
45 | pkt->xt.thoff = thoff; | |
46 | pkt->xt.fragoff = ntohs(iph->frag_off) & IP_OFFSET; | |
47 | } | |
48 | ||
49 | static inline void | |
50 | __nft_netdev_set_pktinfo_ipv6(struct nft_pktinfo *pkt, | |
ed6c4136 PNA |
51 | struct sk_buff *skb, |
52 | const struct nf_hook_state *state) | |
53 | { | |
54 | #if IS_ENABLED(CONFIG_IPV6) | |
55 | struct ipv6hdr *ip6h, _ip6h; | |
56 | unsigned int thoff = 0; | |
57 | unsigned short frag_off; | |
58 | int protohdr; | |
59 | u32 pkt_len; | |
60 | ||
61 | ip6h = skb_header_pointer(skb, skb_network_offset(skb), sizeof(*ip6h), | |
62 | &_ip6h); | |
63 | if (!ip6h) | |
64 | return; | |
65 | ||
66 | if (ip6h->version != 6) | |
67 | return; | |
68 | ||
69 | pkt_len = ntohs(ip6h->payload_len); | |
70 | if (pkt_len + sizeof(*ip6h) > skb->len) | |
71 | return; | |
72 | ||
73 | protohdr = ipv6_find_hdr(pkt->skb, &thoff, -1, &frag_off, NULL); | |
74 | if (protohdr < 0) | |
75 | return; | |
76 | ||
77 | pkt->tprot = protohdr; | |
78 | pkt->xt.thoff = thoff; | |
79 | pkt->xt.fragoff = frag_off; | |
80 | #endif | |
81 | } | |
82 | ||
83 | static inline void nft_netdev_set_pktinfo_ipv6(struct nft_pktinfo *pkt, | |
ed6c4136 PNA |
84 | struct sk_buff *skb, |
85 | const struct nf_hook_state *state) | |
86 | { | |
6aa187f2 EB |
87 | nft_set_pktinfo(pkt, skb, state); |
88 | __nft_netdev_set_pktinfo_ipv6(pkt, skb, state); | |
ed6c4136 PNA |
89 | } |
90 | ||
91 | static unsigned int | |
06198b34 | 92 | nft_do_chain_netdev(void *priv, struct sk_buff *skb, |
ed6c4136 PNA |
93 | const struct nf_hook_state *state) |
94 | { | |
95 | struct nft_pktinfo pkt; | |
96 | ||
aa47e42c | 97 | switch (skb->protocol) { |
ed6c4136 | 98 | case htons(ETH_P_IP): |
6aa187f2 | 99 | nft_netdev_set_pktinfo_ipv4(&pkt, skb, state); |
ed6c4136 PNA |
100 | break; |
101 | case htons(ETH_P_IPV6): | |
6aa187f2 | 102 | nft_netdev_set_pktinfo_ipv6(&pkt, skb, state); |
ed6c4136 PNA |
103 | break; |
104 | default: | |
6aa187f2 | 105 | nft_set_pktinfo(&pkt, skb, state); |
ed6c4136 PNA |
106 | break; |
107 | } | |
108 | ||
06198b34 | 109 | return nft_do_chain(&pkt, priv); |
ed6c4136 PNA |
110 | } |
111 | ||
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, | |
117 | .nops = 1, | |
118 | .hooks = { | |
119 | [NF_NETDEV_INGRESS] = nft_do_chain_netdev, | |
120 | }, | |
121 | }; | |
122 | ||
123 | static int nf_tables_netdev_init_net(struct net *net) | |
124 | { | |
125 | net->nft.netdev = kmalloc(sizeof(struct nft_af_info), GFP_KERNEL); | |
126 | if (net->nft.netdev == NULL) | |
127 | return -ENOMEM; | |
128 | ||
129 | memcpy(net->nft.netdev, &nft_af_netdev, sizeof(nft_af_netdev)); | |
130 | ||
131 | if (nft_register_afinfo(net, net->nft.netdev) < 0) | |
132 | goto err; | |
133 | ||
134 | return 0; | |
135 | err: | |
136 | kfree(net->nft.netdev); | |
137 | return -ENOMEM; | |
138 | } | |
139 | ||
140 | static void nf_tables_netdev_exit_net(struct net *net) | |
141 | { | |
df05ef87 | 142 | nft_unregister_afinfo(net, net->nft.netdev); |
ed6c4136 PNA |
143 | kfree(net->nft.netdev); |
144 | } | |
145 | ||
146 | static struct pernet_operations nf_tables_netdev_net_ops = { | |
147 | .init = nf_tables_netdev_init_net, | |
148 | .exit = nf_tables_netdev_exit_net, | |
149 | }; | |
150 | ||
151 | static const struct nf_chain_type nft_filter_chain_netdev = { | |
152 | .name = "filter", | |
153 | .type = NFT_CHAIN_T_DEFAULT, | |
154 | .family = NFPROTO_NETDEV, | |
155 | .owner = THIS_MODULE, | |
156 | .hook_mask = (1 << NF_NETDEV_INGRESS), | |
157 | }; | |
158 | ||
5ebe0b0e PNA |
159 | static void nft_netdev_event(unsigned long event, struct net_device *dev, |
160 | struct nft_ctx *ctx) | |
835b8033 | 161 | { |
5ebe0b0e | 162 | struct nft_base_chain *basechain = nft_base_chain(ctx->chain); |
835b8033 | 163 | |
5ebe0b0e | 164 | switch (event) { |
835b8033 PNA |
165 | case NETDEV_UNREGISTER: |
166 | if (strcmp(basechain->dev_name, dev->name) != 0) | |
167 | return; | |
168 | ||
5ebe0b0e | 169 | __nft_release_basechain(ctx); |
835b8033 PNA |
170 | break; |
171 | case NETDEV_CHANGENAME: | |
172 | if (dev->ifindex != basechain->ops[0].dev->ifindex) | |
173 | return; | |
174 | ||
175 | strncpy(basechain->dev_name, dev->name, IFNAMSIZ); | |
176 | break; | |
177 | } | |
178 | } | |
179 | ||
180 | static int nf_tables_netdev_event(struct notifier_block *this, | |
181 | unsigned long event, void *ptr) | |
182 | { | |
183 | struct net_device *dev = netdev_notifier_info_to_dev(ptr); | |
184 | struct nft_af_info *afi; | |
185 | struct nft_table *table; | |
5ebe0b0e PNA |
186 | struct nft_chain *chain, *nr; |
187 | struct nft_ctx ctx = { | |
188 | .net = dev_net(dev), | |
189 | }; | |
190 | ||
191 | if (event != NETDEV_UNREGISTER && | |
192 | event != NETDEV_CHANGENAME) | |
193 | return NOTIFY_DONE; | |
835b8033 PNA |
194 | |
195 | nfnl_lock(NFNL_SUBSYS_NFTABLES); | |
196 | list_for_each_entry(afi, &dev_net(dev)->nft.af_info, list) { | |
5ebe0b0e | 197 | ctx.afi = afi; |
835b8033 PNA |
198 | if (afi->family != NFPROTO_NETDEV) |
199 | continue; | |
200 | ||
201 | list_for_each_entry(table, &afi->tables, list) { | |
5ebe0b0e PNA |
202 | ctx.table = table; |
203 | list_for_each_entry_safe(chain, nr, &table->chains, list) { | |
835b8033 PNA |
204 | if (!(chain->flags & NFT_BASE_CHAIN)) |
205 | continue; | |
206 | ||
5ebe0b0e PNA |
207 | ctx.chain = chain; |
208 | nft_netdev_event(event, dev, &ctx); | |
835b8033 PNA |
209 | } |
210 | } | |
211 | } | |
212 | nfnl_unlock(NFNL_SUBSYS_NFTABLES); | |
213 | ||
214 | return NOTIFY_DONE; | |
215 | } | |
216 | ||
217 | static struct notifier_block nf_tables_netdev_notifier = { | |
218 | .notifier_call = nf_tables_netdev_event, | |
219 | }; | |
220 | ||
ed6c4136 PNA |
221 | static int __init nf_tables_netdev_init(void) |
222 | { | |
223 | int ret; | |
224 | ||
225 | nft_register_chain_type(&nft_filter_chain_netdev); | |
226 | ret = register_pernet_subsys(&nf_tables_netdev_net_ops); | |
35b81539 | 227 | if (ret < 0) { |
ed6c4136 | 228 | nft_unregister_chain_type(&nft_filter_chain_netdev); |
35b81539 PNA |
229 | return ret; |
230 | } | |
835b8033 | 231 | register_netdevice_notifier(&nf_tables_netdev_notifier); |
35b81539 | 232 | return 0; |
ed6c4136 PNA |
233 | } |
234 | ||
235 | static void __exit nf_tables_netdev_exit(void) | |
236 | { | |
835b8033 | 237 | unregister_netdevice_notifier(&nf_tables_netdev_notifier); |
ed6c4136 PNA |
238 | unregister_pernet_subsys(&nf_tables_netdev_net_ops); |
239 | nft_unregister_chain_type(&nft_filter_chain_netdev); | |
240 | } | |
241 | ||
242 | module_init(nf_tables_netdev_init); | |
243 | module_exit(nf_tables_netdev_exit); | |
244 | ||
245 | MODULE_LICENSE("GPL"); | |
246 | MODULE_AUTHOR("Pablo Neira Ayuso <pablo@netfilter.org>"); | |
247 | MODULE_ALIAS_NFT_FAMILY(5); /* NFPROTO_NETDEV */ |