| 1 |
/* SPDX-License-Identifier: GPL-2.0 */ |
| 2 |
#include <linux/types.h> |
| 3 |
#include <linux/ip.h> |
| 4 |
#include <linux/netfilter.h> |
| 5 |
#include <linux/netfilter_ipv6.h> |
| 6 |
#include <linux/netfilter_bridge.h> |
| 7 |
#include <linux/module.h> |
| 8 |
#include <linux/skbuff.h> |
| 9 |
#include <linux/icmp.h> |
| 10 |
#include <linux/sysctl.h> |
| 11 |
#include <net/route.h> |
| 12 |
#include <net/ip.h> |
| 13 |
|
| 14 |
#include <net/netfilter/nf_conntrack.h> |
| 15 |
#include <net/netfilter/nf_conntrack_core.h> |
| 16 |
#include <net/netfilter/nf_conntrack_helper.h> |
| 17 |
#include <net/netfilter/nf_conntrack_bridge.h> |
| 18 |
|
| 19 |
#include <linux/netfilter_ipv4.h> |
| 20 |
|
| 21 |
#include "../br_private.h" |
| 22 |
|
| 23 |
/* Best effort variant of ip_do_fragment which preserves geometry, unless skbuff |
| 24 |
* has been linearized or cloned. |
| 25 |
*/ |
| 26 |
static int nf_br_ip_fragment(struct net *net, struct sock *sk, |
| 27 |
struct sk_buff *skb, |
| 28 |
struct nf_bridge_frag_data *data, |
| 29 |
int (*output)(struct net *, struct sock *sk, |
| 30 |
const struct nf_bridge_frag_data *data, |
| 31 |
struct sk_buff *)) |
| 32 |
{ |
| 33 |
int frag_max_size = BR_INPUT_SKB_CB(skb)->frag_max_size; |
| 34 |
u8 tstamp_type = skb->tstamp_type; |
| 35 |
unsigned int hlen, ll_rs, mtu; |
| 36 |
ktime_t tstamp = skb->tstamp; |
| 37 |
struct ip_frag_state state; |
| 38 |
struct iphdr *iph; |
| 39 |
int err = 0; |
| 40 |
|
| 41 |
/* for offloaded checksums cleanup checksum before fragmentation */ |
| 42 |
if (skb->ip_summed == CHECKSUM_PARTIAL && |
| 43 |
(err = skb_checksum_help(skb))) |
| 44 |
goto blackhole; |
| 45 |
|
| 46 |
iph = ip_hdr(skb); |
| 47 |
|
| 48 |
/* |
| 49 |
* Setup starting values |
| 50 |
*/ |
| 51 |
|
| 52 |
hlen = iph->ihl * 4; |
| 53 |
frag_max_size -= hlen; |
| 54 |
ll_rs = LL_RESERVED_SPACE(skb->dev); |
| 55 |
mtu = skb->dev->mtu; |
| 56 |
|
| 57 |
if (skb_has_frag_list(skb)) { |
| 58 |
unsigned int first_len = skb_pagelen(skb); |
| 59 |
struct ip_fraglist_iter iter; |
| 60 |
struct sk_buff *frag; |
| 61 |
|
| 62 |
if (first_len - hlen > mtu) |
| 63 |
goto blackhole; |
| 64 |
|
| 65 |
if (skb_cloned(skb) || |
| 66 |
skb_headroom(skb) < ll_rs) |
| 67 |
goto slow_path; |
| 68 |
|
| 69 |
skb_walk_frags(skb, frag) { |
| 70 |
if (frag->len > mtu) |
| 71 |
goto blackhole; |
| 72 |
|
| 73 |
if (skb_shared(frag) || |
| 74 |
skb_headroom(frag) < hlen + ll_rs) |
| 75 |
goto slow_path; |
| 76 |
} |
| 77 |
|
| 78 |
ip_fraglist_init(skb, iph, hlen, &iter); |
| 79 |
|
| 80 |
for (;;) { |
| 81 |
if (iter.frag) |
| 82 |
ip_fraglist_prepare(skb, &iter); |
| 83 |
|
| 84 |
skb_set_delivery_time(skb, tstamp, tstamp_type); |
| 85 |
err = output(net, sk, data, skb); |
| 86 |
if (err || !iter.frag) |
| 87 |
break; |
| 88 |
|
| 89 |
skb = ip_fraglist_next(&iter); |
| 90 |
} |
| 91 |
|
| 92 |
if (!err) |
| 93 |
return 0; |
| 94 |
|
| 95 |
kfree_skb_list(iter.frag); |
| 96 |
|
| 97 |
return err; |
| 98 |
} |
| 99 |
slow_path: |
| 100 |
/* This is a linearized skbuff, the original geometry is lost for us. |
| 101 |
* This may also be a clone skbuff, we could preserve the geometry for |
| 102 |
* the copies but probably not worth the effort. |
| 103 |
*/ |
| 104 |
ip_frag_init(skb, hlen, ll_rs, frag_max_size, false, &state); |
| 105 |
|
| 106 |
while (state.left > 0) { |
| 107 |
struct sk_buff *skb2; |
| 108 |
|
| 109 |
skb2 = ip_frag_next(skb, &state); |
| 110 |
if (IS_ERR(skb2)) { |
| 111 |
err = PTR_ERR(skb2); |
| 112 |
goto blackhole; |
| 113 |
} |
| 114 |
|
| 115 |
skb_set_delivery_time(skb2, tstamp, tstamp_type); |
| 116 |
err = output(net, sk, data, skb2); |
| 117 |
if (err) |
| 118 |
goto blackhole; |
| 119 |
} |
| 120 |
consume_skb(skb); |
| 121 |
return err; |
| 122 |
|
| 123 |
blackhole: |
| 124 |
kfree_skb(skb); |
| 125 |
return 0; |
| 126 |
} |
| 127 |
|
| 128 |
/* ip_defrag() expects IPCB() in place. */ |
| 129 |
static void br_skb_cb_save(struct sk_buff *skb, struct br_input_skb_cb *cb, |
| 130 |
size_t inet_skb_parm_size) |
| 131 |
{ |
| 132 |
memcpy(cb, skb->cb, sizeof(*cb)); |
| 133 |
memset(skb->cb, 0, inet_skb_parm_size); |
| 134 |
} |
| 135 |
|
| 136 |
static void br_skb_cb_restore(struct sk_buff *skb, |
| 137 |
const struct br_input_skb_cb *cb, |
| 138 |
u16 fragsz) |
| 139 |
{ |
| 140 |
memcpy(skb->cb, cb, sizeof(*cb)); |
| 141 |
BR_INPUT_SKB_CB(skb)->frag_max_size = fragsz; |
| 142 |
} |
| 143 |
|
| 144 |
static unsigned int nf_ct_br_defrag4(struct sk_buff *skb, |
| 145 |
const struct nf_hook_state *state) |
| 146 |
{ |
| 147 |
u16 zone_id = NF_CT_DEFAULT_ZONE_ID; |
| 148 |
enum ip_conntrack_info ctinfo; |
| 149 |
struct br_input_skb_cb cb; |
| 150 |
const struct nf_conn *ct; |
| 151 |
int err; |
| 152 |
|
| 153 |
if (!ip_is_fragment(ip_hdr(skb))) |
| 154 |
return NF_ACCEPT; |
| 155 |
|
| 156 |
ct = nf_ct_get(skb, &ctinfo); |
| 157 |
if (ct) |
| 158 |
zone_id = nf_ct_zone_id(nf_ct_zone(ct), CTINFO2DIR(ctinfo)); |
| 159 |
|
| 160 |
br_skb_cb_save(skb, &cb, sizeof(struct inet_skb_parm)); |
| 161 |
local_bh_disable(); |
| 162 |
err = ip_defrag(state->net, skb, |
| 163 |
IP_DEFRAG_CONNTRACK_BRIDGE_IN + zone_id); |
| 164 |
local_bh_enable(); |
| 165 |
if (!err) { |
| 166 |
br_skb_cb_restore(skb, &cb, IPCB(skb)->frag_max_size); |
| 167 |
skb->ignore_df = 1; |
| 168 |
return NF_ACCEPT; |
| 169 |
} |
| 170 |
|
| 171 |
return NF_STOLEN; |
| 172 |
} |
| 173 |
|
| 174 |
static unsigned int nf_ct_br_defrag6(struct sk_buff *skb, |
| 175 |
const struct nf_hook_state *state) |
| 176 |
{ |
| 177 |
#if IS_ENABLED(CONFIG_NF_DEFRAG_IPV6) |
| 178 |
u16 zone_id = NF_CT_DEFAULT_ZONE_ID; |
| 179 |
enum ip_conntrack_info ctinfo; |
| 180 |
struct br_input_skb_cb cb; |
| 181 |
const struct nf_conn *ct; |
| 182 |
int err; |
| 183 |
|
| 184 |
ct = nf_ct_get(skb, &ctinfo); |
| 185 |
if (ct) |
| 186 |
zone_id = nf_ct_zone_id(nf_ct_zone(ct), CTINFO2DIR(ctinfo)); |
| 187 |
|
| 188 |
br_skb_cb_save(skb, &cb, sizeof(struct inet6_skb_parm)); |
| 189 |
|
| 190 |
err = nf_ct_frag6_gather(state->net, skb, |
| 191 |
IP_DEFRAG_CONNTRACK_BRIDGE_IN + zone_id); |
| 192 |
/* queued */ |
| 193 |
if (err == -EINPROGRESS) |
| 194 |
return NF_STOLEN; |
| 195 |
|
| 196 |
br_skb_cb_restore(skb, &cb, IP6CB(skb)->frag_max_size); |
| 197 |
return err == 0 ? NF_ACCEPT : NF_DROP; |
| 198 |
#else |
| 199 |
return NF_ACCEPT; |
| 200 |
#endif |
| 201 |
} |
| 202 |
|
| 203 |
static int nf_ct_br_ip_check(const struct sk_buff *skb) |
| 204 |
{ |
| 205 |
const struct iphdr *iph; |
| 206 |
int nhoff, len; |
| 207 |
|
| 208 |
nhoff = skb_network_offset(skb); |
| 209 |
iph = ip_hdr(skb); |
| 210 |
if (iph->ihl < 5 || |
| 211 |
iph->version != 4) |
| 212 |
return -1; |
| 213 |
|
| 214 |
len = skb_ip_totlen(skb); |
| 215 |
if (skb->len < nhoff + len || |
| 216 |
len < (iph->ihl * 4)) |
| 217 |
return -1; |
| 218 |
|
| 219 |
return 0; |
| 220 |
} |
| 221 |
|
| 222 |
static int nf_ct_br_ipv6_check(const struct sk_buff *skb) |
| 223 |
{ |
| 224 |
const struct ipv6hdr *hdr; |
| 225 |
int nhoff, len; |
| 226 |
|
| 227 |
nhoff = skb_network_offset(skb); |
| 228 |
hdr = ipv6_hdr(skb); |
| 229 |
if (hdr->version != 6) |
| 230 |
return -1; |
| 231 |
|
| 232 |
len = ipv6_payload_len(skb, hdr) + sizeof(struct ipv6hdr) + nhoff; |
| 233 |
if (skb->len < len) |
| 234 |
return -1; |
| 235 |
|
| 236 |
return 0; |
| 237 |
} |
| 238 |
|
| 239 |
static unsigned int nf_ct_bridge_pre(void *priv, struct sk_buff *skb, |
| 240 |
const struct nf_hook_state *state) |
| 241 |
{ |
| 242 |
struct nf_hook_state bridge_state = *state; |
| 243 |
enum ip_conntrack_info ctinfo; |
| 244 |
struct nf_conn *ct; |
| 245 |
u32 len; |
| 246 |
int ret; |
| 247 |
|
| 248 |
ct = nf_ct_get(skb, &ctinfo); |
| 249 |
if ((ct && !nf_ct_is_template(ct)) || |
| 250 |
ctinfo == IP_CT_UNTRACKED) |
| 251 |
return NF_ACCEPT; |
| 252 |
|
| 253 |
switch (skb->protocol) { |
| 254 |
case htons(ETH_P_IP): |
| 255 |
if (!pskb_may_pull(skb, sizeof(struct iphdr))) |
| 256 |
return NF_ACCEPT; |
| 257 |
|
| 258 |
len = skb_ip_totlen(skb); |
| 259 |
if (pskb_trim_rcsum(skb, len)) |
| 260 |
return NF_ACCEPT; |
| 261 |
|
| 262 |
if (nf_ct_br_ip_check(skb)) |
| 263 |
return NF_ACCEPT; |
| 264 |
|
| 265 |
bridge_state.pf = NFPROTO_IPV4; |
| 266 |
ret = nf_ct_br_defrag4(skb, &bridge_state); |
| 267 |
break; |
| 268 |
case htons(ETH_P_IPV6): |
| 269 |
if (!pskb_may_pull(skb, sizeof(struct ipv6hdr))) |
| 270 |
return NF_ACCEPT; |
| 271 |
|
| 272 |
len = sizeof(struct ipv6hdr) + skb_ipv6_payload_len(skb); |
| 273 |
if (pskb_trim_rcsum(skb, len)) |
| 274 |
return NF_ACCEPT; |
| 275 |
|
| 276 |
if (nf_ct_br_ipv6_check(skb)) |
| 277 |
return NF_ACCEPT; |
| 278 |
|
| 279 |
bridge_state.pf = NFPROTO_IPV6; |
| 280 |
ret = nf_ct_br_defrag6(skb, &bridge_state); |
| 281 |
break; |
| 282 |
default: |
| 283 |
nf_ct_set(skb, NULL, IP_CT_UNTRACKED); |
| 284 |
return NF_ACCEPT; |
| 285 |
} |
| 286 |
|
| 287 |
if (ret != NF_ACCEPT) |
| 288 |
return ret; |
| 289 |
|
| 290 |
return nf_conntrack_in(skb, &bridge_state); |
| 291 |
} |
| 292 |
|
| 293 |
static unsigned int nf_ct_bridge_in(void *priv, struct sk_buff *skb, |
| 294 |
const struct nf_hook_state *state) |
| 295 |
{ |
| 296 |
bool promisc = BR_INPUT_SKB_CB(skb)->promisc; |
| 297 |
struct nf_conntrack *nfct = skb_nfct(skb); |
| 298 |
struct nf_conn *ct; |
| 299 |
|
| 300 |
if (promisc) { |
| 301 |
nf_reset_ct(skb); |
| 302 |
return NF_ACCEPT; |
| 303 |
} |
| 304 |
|
| 305 |
if (!nfct || skb->pkt_type == PACKET_HOST) |
| 306 |
return NF_ACCEPT; |
| 307 |
|
| 308 |
/* nf_conntrack_confirm() cannot handle concurrent clones, |
| 309 |
* this happens for broad/multicast frames with e.g. macvlan on top |
| 310 |
* of the bridge device. |
| 311 |
*/ |
| 312 |
ct = container_of(nfct, struct nf_conn, ct_general); |
| 313 |
if (nf_ct_is_confirmed(ct) || nf_ct_is_template(ct)) |
| 314 |
return NF_ACCEPT; |
| 315 |
|
| 316 |
/* let inet prerouting call conntrack again */ |
| 317 |
skb->_nfct = 0; |
| 318 |
nf_ct_put(ct); |
| 319 |
|
| 320 |
return NF_ACCEPT; |
| 321 |
} |
| 322 |
|
| 323 |
static void nf_ct_bridge_frag_save(struct sk_buff *skb, |
| 324 |
struct nf_bridge_frag_data *data) |
| 325 |
{ |
| 326 |
if (skb_vlan_tag_present(skb)) { |
| 327 |
data->vlan_present = true; |
| 328 |
data->vlan_tci = skb->vlan_tci; |
| 329 |
data->vlan_proto = skb->vlan_proto; |
| 330 |
} else { |
| 331 |
data->vlan_present = false; |
| 332 |
} |
| 333 |
skb_copy_from_linear_data_offset(skb, -ETH_HLEN, data->mac, ETH_HLEN); |
| 334 |
} |
| 335 |
|
| 336 |
static unsigned int |
| 337 |
nf_ct_bridge_refrag(struct sk_buff *skb, const struct nf_hook_state *state, |
| 338 |
int (*output)(struct net *, struct sock *sk, |
| 339 |
const struct nf_bridge_frag_data *data, |
| 340 |
struct sk_buff *)) |
| 341 |
{ |
| 342 |
struct nf_bridge_frag_data data; |
| 343 |
|
| 344 |
if (!BR_INPUT_SKB_CB(skb)->frag_max_size) |
| 345 |
return NF_ACCEPT; |
| 346 |
|
| 347 |
nf_ct_bridge_frag_save(skb, &data); |
| 348 |
switch (skb->protocol) { |
| 349 |
case htons(ETH_P_IP): |
| 350 |
nf_br_ip_fragment(state->net, state->sk, skb, &data, output); |
| 351 |
break; |
| 352 |
case htons(ETH_P_IPV6): |
| 353 |
nf_br_ip6_fragment(state->net, state->sk, skb, &data, output); |
| 354 |
break; |
| 355 |
default: |
| 356 |
WARN_ON_ONCE(1); |
| 357 |
return NF_DROP; |
| 358 |
} |
| 359 |
|
| 360 |
return NF_STOLEN; |
| 361 |
} |
| 362 |
|
| 363 |
/* Actually only slow path refragmentation needs this. */ |
| 364 |
static int nf_ct_bridge_frag_restore(struct sk_buff *skb, |
| 365 |
const struct nf_bridge_frag_data *data) |
| 366 |
{ |
| 367 |
int err; |
| 368 |
|
| 369 |
err = skb_cow_head(skb, ETH_HLEN); |
| 370 |
if (err) { |
| 371 |
kfree_skb(skb); |
| 372 |
return -ENOMEM; |
| 373 |
} |
| 374 |
if (data->vlan_present) |
| 375 |
__vlan_hwaccel_put_tag(skb, data->vlan_proto, data->vlan_tci); |
| 376 |
else if (skb_vlan_tag_present(skb)) |
| 377 |
__vlan_hwaccel_clear_tag(skb); |
| 378 |
|
| 379 |
skb_copy_to_linear_data_offset(skb, -ETH_HLEN, data->mac, ETH_HLEN); |
| 380 |
skb_reset_mac_header(skb); |
| 381 |
|
| 382 |
return 0; |
| 383 |
} |
| 384 |
|
| 385 |
static int nf_ct_bridge_refrag_post(struct net *net, struct sock *sk, |
| 386 |
const struct nf_bridge_frag_data *data, |
| 387 |
struct sk_buff *skb) |
| 388 |
{ |
| 389 |
int err; |
| 390 |
|
| 391 |
err = nf_ct_bridge_frag_restore(skb, data); |
| 392 |
if (err < 0) |
| 393 |
return err; |
| 394 |
|
| 395 |
return br_dev_queue_push_xmit(net, sk, skb); |
| 396 |
} |
| 397 |
|
| 398 |
static unsigned int nf_ct_bridge_post(void *priv, struct sk_buff *skb, |
| 399 |
const struct nf_hook_state *state) |
| 400 |
{ |
| 401 |
int ret; |
| 402 |
|
| 403 |
ret = nf_confirm(priv, skb, state); |
| 404 |
if (ret != NF_ACCEPT) |
| 405 |
return ret; |
| 406 |
|
| 407 |
return nf_ct_bridge_refrag(skb, state, nf_ct_bridge_refrag_post); |
| 408 |
} |
| 409 |
|
| 410 |
static struct nf_hook_ops nf_ct_bridge_hook_ops[] __read_mostly = { |
| 411 |
{ |
| 412 |
.hook = nf_ct_bridge_pre, |
| 413 |
.pf = NFPROTO_BRIDGE, |
| 414 |
.hooknum = NF_BR_PRE_ROUTING, |
| 415 |
.priority = NF_IP_PRI_CONNTRACK, |
| 416 |
}, |
| 417 |
{ |
| 418 |
.hook = nf_ct_bridge_in, |
| 419 |
.pf = NFPROTO_BRIDGE, |
| 420 |
.hooknum = NF_BR_LOCAL_IN, |
| 421 |
.priority = NF_IP_PRI_CONNTRACK_CONFIRM, |
| 422 |
}, |
| 423 |
{ |
| 424 |
.hook = nf_ct_bridge_post, |
| 425 |
.pf = NFPROTO_BRIDGE, |
| 426 |
.hooknum = NF_BR_POST_ROUTING, |
| 427 |
.priority = NF_IP_PRI_CONNTRACK_CONFIRM, |
| 428 |
}, |
| 429 |
}; |
| 430 |
|
| 431 |
static struct nf_ct_bridge_info bridge_info = { |
| 432 |
.ops = nf_ct_bridge_hook_ops, |
| 433 |
.ops_size = ARRAY_SIZE(nf_ct_bridge_hook_ops), |
| 434 |
.me = THIS_MODULE, |
| 435 |
}; |
| 436 |
|
| 437 |
static int __init nf_conntrack_l3proto_bridge_init(void) |
| 438 |
{ |
| 439 |
nf_ct_bridge_register(&bridge_info); |
| 440 |
|
| 441 |
return 0; |
| 442 |
} |
| 443 |
|
| 444 |
static void __exit nf_conntrack_l3proto_bridge_fini(void) |
| 445 |
{ |
| 446 |
nf_ct_bridge_unregister(&bridge_info); |
| 447 |
} |
| 448 |
|
| 449 |
module_init(nf_conntrack_l3proto_bridge_init); |
| 450 |
module_exit(nf_conntrack_l3proto_bridge_fini); |
| 451 |
|
| 452 |
MODULE_ALIAS("nf_conntrack-" __stringify(AF_BRIDGE)); |
| 453 |
MODULE_LICENSE("GPL"); |
| 454 |
MODULE_DESCRIPTION("Bridge IPv4 and IPv6 connection tracking"); |