^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) - RFC3173.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (c) 2003 James Morris <jmorris@intercode.com.au>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Copyright (c) 2003-2008 Herbert Xu <herbert@gondor.apana.org.au>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * Todo:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * - Tunable compression parameters.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) * - Compression stats.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) * - Adaptive compression.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/crypto.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/list.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/mutex.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/percpu.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <linux/smp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include <linux/vmalloc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include <net/ip.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #include <net/ipcomp.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)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) struct ipcomp_tfms {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) struct list_head list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) struct crypto_comp * __percpu *tfms;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) int users;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) static DEFINE_MUTEX(ipcomp_resource_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) static void * __percpu *ipcomp_scratches;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) static int ipcomp_scratch_users;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) static LIST_HEAD(ipcomp_tfms_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) static int ipcomp_decompress(struct xfrm_state *x, struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) struct ipcomp_data *ipcd = x->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) const int plen = skb->len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) int dlen = IPCOMP_SCRATCH_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) const u8 *start = skb->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) const int cpu = get_cpu();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) u8 *scratch = *per_cpu_ptr(ipcomp_scratches, cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) struct crypto_comp *tfm = *per_cpu_ptr(ipcd->tfms, cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) int err = crypto_comp_decompress(tfm, start, plen, scratch, &dlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) int len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) if (dlen < (plen + sizeof(struct ip_comp_hdr))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) len = dlen - plen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) if (len > skb_tailroom(skb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) len = skb_tailroom(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) __skb_put(skb, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) len += plen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) skb_copy_to_linear_data(skb, scratch, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) while ((scratch += len, dlen -= len) > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) skb_frag_t *frag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) struct page *page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) err = -EMSGSIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) if (WARN_ON(skb_shinfo(skb)->nr_frags >= MAX_SKB_FRAGS))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) frag = skb_shinfo(skb)->frags + skb_shinfo(skb)->nr_frags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) page = alloc_page(GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) if (!page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) __skb_frag_set_page(frag, page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) len = PAGE_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) if (dlen < len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) len = dlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) skb_frag_off_set(frag, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) skb_frag_size_set(frag, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) memcpy(skb_frag_address(frag), scratch, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) skb->truesize += len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) skb->data_len += len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) skb->len += len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) skb_shinfo(skb)->nr_frags++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) err = 0;
^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) put_cpu();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) int ipcomp_input(struct xfrm_state *x, struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) int nexthdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) int err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) struct ip_comp_hdr *ipch;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) if (skb_linearize_cow(skb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) skb->ip_summed = CHECKSUM_NONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) /* Remove ipcomp header and decompress original payload */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) ipch = (void *)skb->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) nexthdr = ipch->nexthdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) skb->transport_header = skb->network_header + sizeof(*ipch);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) __skb_pull(skb, sizeof(*ipch));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) err = ipcomp_decompress(x, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) err = nexthdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) EXPORT_SYMBOL_GPL(ipcomp_input);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) static int ipcomp_compress(struct xfrm_state *x, struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) struct ipcomp_data *ipcd = x->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) const int plen = skb->len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) int dlen = IPCOMP_SCRATCH_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) u8 *start = skb->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) struct crypto_comp *tfm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) u8 *scratch;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) local_bh_disable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) scratch = *this_cpu_ptr(ipcomp_scratches);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) tfm = *this_cpu_ptr(ipcd->tfms);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) err = crypto_comp_compress(tfm, start, plen, scratch, &dlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) if ((dlen + sizeof(struct ip_comp_hdr)) >= plen) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) err = -EMSGSIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) memcpy(start + sizeof(struct ip_comp_hdr), scratch, dlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) local_bh_enable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) pskb_trim(skb, dlen + sizeof(struct ip_comp_hdr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) local_bh_enable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) int ipcomp_output(struct xfrm_state *x, struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) struct ip_comp_hdr *ipch;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) struct ipcomp_data *ipcd = x->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) if (skb->len < ipcd->threshold) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) /* Don't bother compressing */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) goto out_ok;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) if (skb_linearize_cow(skb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) goto out_ok;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) err = ipcomp_compress(x, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) goto out_ok;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) /* Install ipcomp header, convert into ipcomp datagram. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) ipch = ip_comp_hdr(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) ipch->nexthdr = *skb_mac_header(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) ipch->flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) ipch->cpi = htons((u16 )ntohl(x->id.spi));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) *skb_mac_header(skb) = IPPROTO_COMP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) out_ok:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) skb_push(skb, -skb_network_offset(skb));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) EXPORT_SYMBOL_GPL(ipcomp_output);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) static void ipcomp_free_scratches(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) void * __percpu *scratches;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) if (--ipcomp_scratch_users)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) scratches = ipcomp_scratches;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) if (!scratches)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) for_each_possible_cpu(i)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) vfree(*per_cpu_ptr(scratches, i));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) free_percpu(scratches);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) static void * __percpu *ipcomp_alloc_scratches(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) void * __percpu *scratches;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) if (ipcomp_scratch_users++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) return ipcomp_scratches;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) scratches = alloc_percpu(void *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) if (!scratches)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) ipcomp_scratches = scratches;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) for_each_possible_cpu(i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) void *scratch;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) scratch = vmalloc_node(IPCOMP_SCRATCH_SIZE, cpu_to_node(i));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) if (!scratch)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) *per_cpu_ptr(scratches, i) = scratch;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) return scratches;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) static void ipcomp_free_tfms(struct crypto_comp * __percpu *tfms)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) struct ipcomp_tfms *pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) int cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) list_for_each_entry(pos, &ipcomp_tfms_list, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) if (pos->tfms == tfms)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) WARN_ON(list_entry_is_head(pos, &ipcomp_tfms_list, list));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) if (--pos->users)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) list_del(&pos->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) kfree(pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) if (!tfms)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) for_each_possible_cpu(cpu) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) struct crypto_comp *tfm = *per_cpu_ptr(tfms, cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) crypto_free_comp(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) free_percpu(tfms);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) static struct crypto_comp * __percpu *ipcomp_alloc_tfms(const char *alg_name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) struct ipcomp_tfms *pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) struct crypto_comp * __percpu *tfms;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) int cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) list_for_each_entry(pos, &ipcomp_tfms_list, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) struct crypto_comp *tfm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) /* This can be any valid CPU ID so we don't need locking. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) tfm = this_cpu_read(*pos->tfms);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) if (!strcmp(crypto_comp_name(tfm), alg_name)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) pos->users++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) return pos->tfms;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) pos = kmalloc(sizeof(*pos), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) if (!pos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) pos->users = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) INIT_LIST_HEAD(&pos->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) list_add(&pos->list, &ipcomp_tfms_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) pos->tfms = tfms = alloc_percpu(struct crypto_comp *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) if (!tfms)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) for_each_possible_cpu(cpu) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) struct crypto_comp *tfm = crypto_alloc_comp(alg_name, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) CRYPTO_ALG_ASYNC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) if (IS_ERR(tfm))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) *per_cpu_ptr(tfms, cpu) = tfm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) return tfms;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) ipcomp_free_tfms(tfms);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) static void ipcomp_free_data(struct ipcomp_data *ipcd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) if (ipcd->tfms)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) ipcomp_free_tfms(ipcd->tfms);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) ipcomp_free_scratches();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) void ipcomp_destroy(struct xfrm_state *x)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) struct ipcomp_data *ipcd = x->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) if (!ipcd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) xfrm_state_delete_tunnel(x);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) mutex_lock(&ipcomp_resource_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) ipcomp_free_data(ipcd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) mutex_unlock(&ipcomp_resource_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) kfree(ipcd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) EXPORT_SYMBOL_GPL(ipcomp_destroy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) int ipcomp_init_state(struct xfrm_state *x)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) struct ipcomp_data *ipcd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) struct xfrm_algo_desc *calg_desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) if (!x->calg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) if (x->encap)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) ipcd = kzalloc(sizeof(*ipcd), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) if (!ipcd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) mutex_lock(&ipcomp_resource_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) if (!ipcomp_alloc_scratches())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) ipcd->tfms = ipcomp_alloc_tfms(x->calg->alg_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) if (!ipcd->tfms)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) mutex_unlock(&ipcomp_resource_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) calg_desc = xfrm_calg_get_byname(x->calg->alg_name, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) BUG_ON(!calg_desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) ipcd->threshold = calg_desc->uinfo.comp.threshold;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) x->data = ipcd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) ipcomp_free_data(ipcd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) mutex_unlock(&ipcomp_resource_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) kfree(ipcd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) EXPORT_SYMBOL_GPL(ipcomp_init_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) MODULE_DESCRIPTION("IP Payload Compression Protocol (IPComp) - RFC3173");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) MODULE_AUTHOR("James Morris <jmorris@intercode.com.au>");