Linux Audio

Check our new training course

Loading...
v6.13.7
  1// SPDX-License-Identifier: GPL-2.0-or-later
  2/*
  3 * sctp_offload - GRO/GSO Offloading for SCTP
  4 *
  5 * Copyright (C) 2015, Marcelo Ricardo Leitner <marcelo.leitner@gmail.com>
 
 
 
 
 
 
 
 
 
 
  6 */
  7
  8#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  9
 10#include <linux/kernel.h>
 11#include <linux/kprobes.h>
 12#include <linux/socket.h>
 13#include <linux/sctp.h>
 14#include <linux/proc_fs.h>
 15#include <linux/vmalloc.h>
 16#include <linux/module.h>
 17#include <linux/kfifo.h>
 18#include <linux/time.h>
 19#include <net/net_namespace.h>
 20
 21#include <linux/skbuff.h>
 22#include <net/sctp/sctp.h>
 23#include <net/sctp/checksum.h>
 24#include <net/protocol.h>
 25#include <net/gso.h>
 26
 27static __le32 sctp_gso_make_checksum(struct sk_buff *skb)
 28{
 29	skb->ip_summed = CHECKSUM_NONE;
 30	skb->csum_not_inet = 0;
 31	/* csum and csum_start in GSO CB may be needed to do the UDP
 32	 * checksum when it's a UDP tunneling packet.
 33	 */
 34	SKB_GSO_CB(skb)->csum = (__force __wsum)~0;
 35	SKB_GSO_CB(skb)->csum_start = skb_headroom(skb) + skb->len;
 36	return sctp_compute_cksum(skb, skb_transport_offset(skb));
 37}
 38
 39static struct sk_buff *sctp_gso_segment(struct sk_buff *skb,
 40					netdev_features_t features)
 41{
 42	struct sk_buff *segs = ERR_PTR(-EINVAL);
 43	struct sctphdr *sh;
 44
 45	if (!skb_is_gso_sctp(skb))
 46		goto out;
 47
 48	sh = sctp_hdr(skb);
 49	if (!pskb_may_pull(skb, sizeof(*sh)))
 50		goto out;
 51
 52	__skb_pull(skb, sizeof(*sh));
 53
 54	if (skb_gso_ok(skb, features | NETIF_F_GSO_ROBUST)) {
 55		/* Packet is from an untrusted source, reset gso_segs. */
 56		struct skb_shared_info *pinfo = skb_shinfo(skb);
 57		struct sk_buff *frag_iter;
 58
 59		pinfo->gso_segs = 0;
 60		if (skb->len != skb->data_len) {
 61			/* Means we have chunks in here too */
 62			pinfo->gso_segs++;
 63		}
 64
 65		skb_walk_frags(skb, frag_iter)
 66			pinfo->gso_segs++;
 67
 68		segs = NULL;
 69		goto out;
 70	}
 71
 72	segs = skb_segment(skb, (features | NETIF_F_HW_CSUM) & ~NETIF_F_SG);
 73	if (IS_ERR(segs))
 74		goto out;
 75
 76	/* All that is left is update SCTP CRC if necessary */
 77	if (!(features & NETIF_F_SCTP_CRC)) {
 78		for (skb = segs; skb; skb = skb->next) {
 79			if (skb->ip_summed == CHECKSUM_PARTIAL) {
 80				sh = sctp_hdr(skb);
 81				sh->checksum = sctp_gso_make_checksum(skb);
 82			}
 83		}
 84	}
 85
 86out:
 87	return segs;
 88}
 89
 90static const struct net_offload sctp_offload = {
 91	.callbacks = {
 92		.gso_segment = sctp_gso_segment,
 93	},
 94};
 95
 96static const struct net_offload sctp6_offload = {
 97	.callbacks = {
 98		.gso_segment = sctp_gso_segment,
 99	},
100};
101
 
 
 
 
 
102int __init sctp_offload_init(void)
103{
104	int ret;
105
106	ret = inet_add_offload(&sctp_offload, IPPROTO_SCTP);
107	if (ret)
108		goto out;
109
110	ret = inet6_add_offload(&sctp6_offload, IPPROTO_SCTP);
111	if (ret)
112		goto ipv4;
113
114	crc32c_csum_stub = &sctp_csum_ops;
115	return ret;
116
117ipv4:
118	inet_del_offload(&sctp_offload, IPPROTO_SCTP);
119out:
120	return ret;
121}
v4.17
 
  1/*
  2 * sctp_offload - GRO/GSO Offloading for SCTP
  3 *
  4 * Copyright (C) 2015, Marcelo Ricardo Leitner <marcelo.leitner@gmail.com>
  5 *
  6 * This program is free software; you can redistribute it and/or modify
  7 * it under the terms of the GNU General Public License as published by
  8 * the Free Software Foundation; either version 2 of the License, or
  9 * (at your option) any later version.
 10 *
 11 * This program is distributed in the hope that it will be useful,
 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 14 * GNU General Public License for more details.
 15 */
 16
 17#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
 18
 19#include <linux/kernel.h>
 20#include <linux/kprobes.h>
 21#include <linux/socket.h>
 22#include <linux/sctp.h>
 23#include <linux/proc_fs.h>
 24#include <linux/vmalloc.h>
 25#include <linux/module.h>
 26#include <linux/kfifo.h>
 27#include <linux/time.h>
 28#include <net/net_namespace.h>
 29
 30#include <linux/skbuff.h>
 31#include <net/sctp/sctp.h>
 32#include <net/sctp/checksum.h>
 33#include <net/protocol.h>
 
 34
 35static __le32 sctp_gso_make_checksum(struct sk_buff *skb)
 36{
 37	skb->ip_summed = CHECKSUM_NONE;
 38	skb->csum_not_inet = 0;
 
 
 
 
 
 39	return sctp_compute_cksum(skb, skb_transport_offset(skb));
 40}
 41
 42static struct sk_buff *sctp_gso_segment(struct sk_buff *skb,
 43					netdev_features_t features)
 44{
 45	struct sk_buff *segs = ERR_PTR(-EINVAL);
 46	struct sctphdr *sh;
 47
 48	if (!skb_is_gso_sctp(skb))
 49		goto out;
 50
 51	sh = sctp_hdr(skb);
 52	if (!pskb_may_pull(skb, sizeof(*sh)))
 53		goto out;
 54
 55	__skb_pull(skb, sizeof(*sh));
 56
 57	if (skb_gso_ok(skb, features | NETIF_F_GSO_ROBUST)) {
 58		/* Packet is from an untrusted source, reset gso_segs. */
 59		struct skb_shared_info *pinfo = skb_shinfo(skb);
 60		struct sk_buff *frag_iter;
 61
 62		pinfo->gso_segs = 0;
 63		if (skb->len != skb->data_len) {
 64			/* Means we have chunks in here too */
 65			pinfo->gso_segs++;
 66		}
 67
 68		skb_walk_frags(skb, frag_iter)
 69			pinfo->gso_segs++;
 70
 71		segs = NULL;
 72		goto out;
 73	}
 74
 75	segs = skb_segment(skb, features | NETIF_F_HW_CSUM | NETIF_F_SG);
 76	if (IS_ERR(segs))
 77		goto out;
 78
 79	/* All that is left is update SCTP CRC if necessary */
 80	if (!(features & NETIF_F_SCTP_CRC)) {
 81		for (skb = segs; skb; skb = skb->next) {
 82			if (skb->ip_summed == CHECKSUM_PARTIAL) {
 83				sh = sctp_hdr(skb);
 84				sh->checksum = sctp_gso_make_checksum(skb);
 85			}
 86		}
 87	}
 88
 89out:
 90	return segs;
 91}
 92
 93static const struct net_offload sctp_offload = {
 94	.callbacks = {
 95		.gso_segment = sctp_gso_segment,
 96	},
 97};
 98
 99static const struct net_offload sctp6_offload = {
100	.callbacks = {
101		.gso_segment = sctp_gso_segment,
102	},
103};
104
105static const struct skb_checksum_ops crc32c_csum_ops = {
106	.update  = sctp_csum_update,
107	.combine = sctp_csum_combine,
108};
109
110int __init sctp_offload_init(void)
111{
112	int ret;
113
114	ret = inet_add_offload(&sctp_offload, IPPROTO_SCTP);
115	if (ret)
116		goto out;
117
118	ret = inet6_add_offload(&sctp6_offload, IPPROTO_SCTP);
119	if (ret)
120		goto ipv4;
121
122	crc32c_csum_stub = &crc32c_csum_ops;
123	return ret;
124
125ipv4:
126	inet_del_offload(&sctp_offload, IPPROTO_SCTP);
127out:
128	return ret;
129}