^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * IP Payload Compression Protocol (IPComp) for IPv6 - RFC3173
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (C)2003 USAGI/WIDE Project
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * Author Mitsuru KANDA <mk@linux-ipv6.org>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) * [Memo]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) * Outbound:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) * The compression of IP datagram MUST be done before AH/ESP processing,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) * fragmentation, and the addition of Hop-by-Hop/Routing header.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) * Inbound:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) * The decompression of IP datagram MUST be done after the reassembly,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) * AH/ESP processing.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #define pr_fmt(fmt) "IPv6: " fmt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #include <net/ip.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #include <net/xfrm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #include <net/ipcomp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #include <linux/crypto.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #include <linux/err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #include <linux/pfkeyv2.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) #include <linux/random.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) #include <linux/percpu.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) #include <linux/smp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) #include <linux/list.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) #include <linux/vmalloc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) #include <linux/rtnetlink.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) #include <net/ip6_route.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) #include <net/icmp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) #include <net/ipv6.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) #include <net/protocol.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) #include <linux/ipv6.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) #include <linux/icmpv6.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) #include <linux/mutex.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) static int ipcomp6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) u8 type, u8 code, int offset, __be32 info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) struct net *net = dev_net(skb->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) __be32 spi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) const struct ipv6hdr *iph = (const struct ipv6hdr *)skb->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) struct ip_comp_hdr *ipcomph =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) (struct ip_comp_hdr *)(skb->data + offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) struct xfrm_state *x;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) if (type != ICMPV6_PKT_TOOBIG &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) type != NDISC_REDIRECT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) spi = htonl(ntohs(ipcomph->cpi));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) x = xfrm_state_lookup(net, skb->mark, (const xfrm_address_t *)&iph->daddr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) spi, IPPROTO_COMP, AF_INET6);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) if (!x)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) if (type == NDISC_REDIRECT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) ip6_redirect(skb, net, skb->dev->ifindex, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) sock_net_uid(net, NULL));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) ip6_update_pmtu(skb, net, info, 0, 0, sock_net_uid(net, NULL));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) xfrm_state_put(x);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) static struct xfrm_state *ipcomp6_tunnel_create(struct xfrm_state *x)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) struct net *net = xs_net(x);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) struct xfrm_state *t = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) t = xfrm_state_alloc(net);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) if (!t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) t->id.proto = IPPROTO_IPV6;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) t->id.spi = xfrm6_tunnel_alloc_spi(net, (xfrm_address_t *)&x->props.saddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) if (!t->id.spi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) memcpy(t->id.daddr.a6, x->id.daddr.a6, sizeof(struct in6_addr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) memcpy(&t->sel, &x->sel, sizeof(t->sel));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) t->props.family = AF_INET6;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) t->props.mode = x->props.mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) memcpy(t->props.saddr.a6, x->props.saddr.a6, sizeof(struct in6_addr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) memcpy(&t->mark, &x->mark, sizeof(t->mark));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) t->if_id = x->if_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) if (xfrm_init_state(t))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) atomic_set(&t->tunnel_users, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) return t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) t->km.state = XFRM_STATE_DEAD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) xfrm_state_put(t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) t = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) static int ipcomp6_tunnel_attach(struct xfrm_state *x)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) struct net *net = xs_net(x);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) struct xfrm_state *t = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) __be32 spi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) u32 mark = x->mark.m & x->mark.v;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) spi = xfrm6_tunnel_spi_lookup(net, (xfrm_address_t *)&x->props.saddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) if (spi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) t = xfrm_state_lookup(net, mark, (xfrm_address_t *)&x->id.daddr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) spi, IPPROTO_IPV6, AF_INET6);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) if (!t) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) t = ipcomp6_tunnel_create(x);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) if (!t) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) xfrm_state_insert(t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) xfrm_state_hold(t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) x->tunnel = t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) atomic_inc(&t->tunnel_users);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) static int ipcomp6_init_state(struct xfrm_state *x)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) int err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) x->props.header_len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) switch (x->props.mode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) case XFRM_MODE_TRANSPORT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) case XFRM_MODE_TUNNEL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) x->props.header_len += sizeof(struct ipv6hdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) err = ipcomp_init_state(x);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) if (x->props.mode == XFRM_MODE_TUNNEL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) err = ipcomp6_tunnel_attach(x);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) static int ipcomp6_rcv_cb(struct sk_buff *skb, int err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) static const struct xfrm_type ipcomp6_type = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) .description = "IPCOMP6",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) .owner = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) .proto = IPPROTO_COMP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) .init_state = ipcomp6_init_state,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) .destructor = ipcomp_destroy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) .input = ipcomp_input,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) .output = ipcomp_output,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) .hdr_offset = xfrm6_find_1stfragopt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) static struct xfrm6_protocol ipcomp6_protocol = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) .handler = xfrm6_rcv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) .input_handler = xfrm_input,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) .cb_handler = ipcomp6_rcv_cb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) .err_handler = ipcomp6_err,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) .priority = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) static int __init ipcomp6_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) if (xfrm_register_type(&ipcomp6_type, AF_INET6) < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) pr_info("%s: can't add xfrm type\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) return -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) if (xfrm6_protocol_register(&ipcomp6_protocol, IPPROTO_COMP) < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) pr_info("%s: can't add protocol\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) xfrm_unregister_type(&ipcomp6_type, AF_INET6);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) return -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) static void __exit ipcomp6_fini(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) if (xfrm6_protocol_deregister(&ipcomp6_protocol, IPPROTO_COMP) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) pr_info("%s: can't remove protocol\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) xfrm_unregister_type(&ipcomp6_type, AF_INET6);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) module_init(ipcomp6_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) module_exit(ipcomp6_fini);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) MODULE_DESCRIPTION("IP Payload Compression Protocol (IPComp) for IPv6 - RFC3173");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) MODULE_AUTHOR("Mitsuru KANDA <mk@linux-ipv6.org>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) MODULE_ALIAS_XFRM_TYPE(AF_INET6, XFRM_PROTO_COMP);