at commit c03c1a14
| 1 | // SPDX-License-Identifier: GPL-2.0-only |
| 2 | /* iptables module for the packet checksum mangling |
| 3 | * |
| 4 | * (C) 2002 by Harald Welte <laforge@netfilter.org> |
| 5 | * (C) 2010 Red Hat, Inc. |
| 6 | * |
| 7 | * Author: Michael S. Tsirkin <mst@redhat.com> |
| 8 | */ |
| 9 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 10 | #include <linux/module.h> |
| 11 | #include <linux/skbuff.h> |
| 12 | |
| 13 | #include <linux/netfilter/x_tables.h> |
| 14 | #include <linux/netfilter/xt_CHECKSUM.h> |
| 15 | |
| 16 | #include <linux/netfilter_ipv4/ip_tables.h> |
| 17 | #include <linux/netfilter_ipv6/ip6_tables.h> |
| 18 | #include <net/ip.h> |
| 19 | |
| 20 | MODULE_LICENSE("GPL"); |
| 21 | MODULE_AUTHOR("Michael S. Tsirkin <mst@redhat.com>"); |
| 22 | MODULE_DESCRIPTION("Xtables: checksum modification"); |
| 23 | MODULE_ALIAS("ipt_CHECKSUM"); |
| 24 | MODULE_ALIAS("ip6t_CHECKSUM"); |
| 25 | |
| 26 | static unsigned int |
| 27 | checksum_tg(struct sk_buff *skb, const struct xt_action_param *par) |
| 28 | { |
| 29 | if (skb->ip_summed == CHECKSUM_PARTIAL && !skb_is_gso(skb)) { |
| 30 | if (xt_family(par) == NFPROTO_IPV4 && ip_check_csum_start(skb)) |
| 31 | return NF_DROP; |
| 32 | skb_checksum_help(skb); |
| 33 | } |
| 34 | |
| 35 | return XT_CONTINUE; |
| 36 | } |
| 37 | |
| 38 | static int checksum_tg_check(const struct xt_tgchk_param *par) |
| 39 | { |
| 40 | const struct xt_CHECKSUM_info *einfo = par->targinfo; |
| 41 | const struct ip6t_ip6 *i6 = par->entryinfo; |
| 42 | const struct ipt_ip *i4 = par->entryinfo; |
| 43 | |
| 44 | if (einfo->operation & ~XT_CHECKSUM_OP_FILL) { |
| 45 | pr_info_ratelimited("unsupported CHECKSUM operation %x\n", |
| 46 | einfo->operation); |
| 47 | return -EINVAL; |
| 48 | } |
| 49 | if (!einfo->operation) |
| 50 | return -EINVAL; |
| 51 | |
| 52 | switch (par->family) { |
| 53 | case NFPROTO_IPV4: |
| 54 | if (i4->proto == IPPROTO_UDP && |
| 55 | (i4->invflags & XT_INV_PROTO) == 0) |
| 56 | return 0; |
| 57 | break; |
| 58 | case NFPROTO_IPV6: |
| 59 | if ((i6->flags & IP6T_F_PROTO) && |
| 60 | i6->proto == IPPROTO_UDP && |
| 61 | (i6->invflags & XT_INV_PROTO) == 0) |
| 62 | return 0; |
| 63 | break; |
| 64 | } |
| 65 | |
| 66 | pr_warn_once("CHECKSUM should be avoided. If really needed, restrict with \"-p udp\" and only use in OUTPUT\n"); |
| 67 | return 0; |
| 68 | } |
| 69 | |
| 70 | static struct xt_target checksum_tg_reg[] __read_mostly = { |
| 71 | { |
| 72 | .name = "CHECKSUM", |
| 73 | .family = NFPROTO_IPV4, |
| 74 | .target = checksum_tg, |
| 75 | .targetsize = sizeof(struct xt_CHECKSUM_info), |
| 76 | .table = "mangle", |
| 77 | .checkentry = checksum_tg_check, |
| 78 | .me = THIS_MODULE, |
| 79 | }, |
| 80 | #if IS_ENABLED(CONFIG_IP6_NF_IPTABLES) |
| 81 | { |
| 82 | .name = "CHECKSUM", |
| 83 | .family = NFPROTO_IPV6, |
| 84 | .target = checksum_tg, |
| 85 | .targetsize = sizeof(struct xt_CHECKSUM_info), |
| 86 | .table = "mangle", |
| 87 | .checkentry = checksum_tg_check, |
| 88 | .me = THIS_MODULE, |
| 89 | }, |
| 90 | #endif |
| 91 | }; |
| 92 | |
| 93 | static int __init checksum_tg_init(void) |
| 94 | { |
| 95 | return xt_register_targets(checksum_tg_reg, ARRAY_SIZE(checksum_tg_reg)); |
| 96 | } |
| 97 | |
| 98 | static void __exit checksum_tg_exit(void) |
| 99 | { |
| 100 | xt_unregister_targets(checksum_tg_reg, ARRAY_SIZE(checksum_tg_reg)); |
| 101 | } |
| 102 | |
| 103 | module_init(checksum_tg_init); |
| 104 | module_exit(checksum_tg_exit); |