190017accSMarcelo Ricardo Leitner /* 290017accSMarcelo Ricardo Leitner * sctp_offload - GRO/GSO Offloading for SCTP 390017accSMarcelo Ricardo Leitner * 490017accSMarcelo Ricardo Leitner * Copyright (C) 2015, Marcelo Ricardo Leitner <marcelo.leitner@gmail.com> 590017accSMarcelo Ricardo Leitner * 690017accSMarcelo Ricardo Leitner * This program is free software; you can redistribute it and/or modify 790017accSMarcelo Ricardo Leitner * it under the terms of the GNU General Public License as published by 890017accSMarcelo Ricardo Leitner * the Free Software Foundation; either version 2 of the License, or 990017accSMarcelo Ricardo Leitner * (at your option) any later version. 1090017accSMarcelo Ricardo Leitner * 1190017accSMarcelo Ricardo Leitner * This program is distributed in the hope that it will be useful, 1290017accSMarcelo Ricardo Leitner * but WITHOUT ANY WARRANTY; without even the implied warranty of 1390017accSMarcelo Ricardo Leitner * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 1490017accSMarcelo Ricardo Leitner * GNU General Public License for more details. 1590017accSMarcelo Ricardo Leitner */ 1690017accSMarcelo Ricardo Leitner 1790017accSMarcelo Ricardo Leitner #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 1890017accSMarcelo Ricardo Leitner 1990017accSMarcelo Ricardo Leitner #include <linux/kernel.h> 2090017accSMarcelo Ricardo Leitner #include <linux/kprobes.h> 2190017accSMarcelo Ricardo Leitner #include <linux/socket.h> 2290017accSMarcelo Ricardo Leitner #include <linux/sctp.h> 2390017accSMarcelo Ricardo Leitner #include <linux/proc_fs.h> 2490017accSMarcelo Ricardo Leitner #include <linux/vmalloc.h> 2590017accSMarcelo Ricardo Leitner #include <linux/module.h> 2690017accSMarcelo Ricardo Leitner #include <linux/kfifo.h> 2790017accSMarcelo Ricardo Leitner #include <linux/time.h> 2890017accSMarcelo Ricardo Leitner #include <net/net_namespace.h> 2990017accSMarcelo Ricardo Leitner 3090017accSMarcelo Ricardo Leitner #include <linux/skbuff.h> 3190017accSMarcelo Ricardo Leitner #include <net/sctp/sctp.h> 3290017accSMarcelo Ricardo Leitner #include <net/sctp/checksum.h> 3390017accSMarcelo Ricardo Leitner #include <net/protocol.h> 3490017accSMarcelo Ricardo Leitner 3590017accSMarcelo Ricardo Leitner static __le32 sctp_gso_make_checksum(struct sk_buff *skb) 3690017accSMarcelo Ricardo Leitner { 3790017accSMarcelo Ricardo Leitner skb->ip_summed = CHECKSUM_NONE; 38dba00306SDavide Caratti skb->csum_not_inet = 0; 3990017accSMarcelo Ricardo Leitner return sctp_compute_cksum(skb, skb_transport_offset(skb)); 4090017accSMarcelo Ricardo Leitner } 4190017accSMarcelo Ricardo Leitner 4290017accSMarcelo Ricardo Leitner static struct sk_buff *sctp_gso_segment(struct sk_buff *skb, 4390017accSMarcelo Ricardo Leitner netdev_features_t features) 4490017accSMarcelo Ricardo Leitner { 4590017accSMarcelo Ricardo Leitner struct sk_buff *segs = ERR_PTR(-EINVAL); 4690017accSMarcelo Ricardo Leitner struct sctphdr *sh; 4790017accSMarcelo Ricardo Leitner 48*1dd27cdeSDaniel Axtens if (!skb_is_gso_sctp(skb)) 49121d57afSWillem de Bruijn goto out; 50121d57afSWillem de Bruijn 5190017accSMarcelo Ricardo Leitner sh = sctp_hdr(skb); 5290017accSMarcelo Ricardo Leitner if (!pskb_may_pull(skb, sizeof(*sh))) 5390017accSMarcelo Ricardo Leitner goto out; 5490017accSMarcelo Ricardo Leitner 5590017accSMarcelo Ricardo Leitner __skb_pull(skb, sizeof(*sh)); 5690017accSMarcelo Ricardo Leitner 5790017accSMarcelo Ricardo Leitner if (skb_gso_ok(skb, features | NETIF_F_GSO_ROBUST)) { 5890017accSMarcelo Ricardo Leitner /* Packet is from an untrusted source, reset gso_segs. */ 5990017accSMarcelo Ricardo Leitner struct skb_shared_info *pinfo = skb_shinfo(skb); 6090017accSMarcelo Ricardo Leitner struct sk_buff *frag_iter; 6190017accSMarcelo Ricardo Leitner 6290017accSMarcelo Ricardo Leitner pinfo->gso_segs = 0; 6390017accSMarcelo Ricardo Leitner if (skb->len != skb->data_len) { 6490017accSMarcelo Ricardo Leitner /* Means we have chunks in here too */ 6590017accSMarcelo Ricardo Leitner pinfo->gso_segs++; 6690017accSMarcelo Ricardo Leitner } 6790017accSMarcelo Ricardo Leitner 6890017accSMarcelo Ricardo Leitner skb_walk_frags(skb, frag_iter) 6990017accSMarcelo Ricardo Leitner pinfo->gso_segs++; 7090017accSMarcelo Ricardo Leitner 7190017accSMarcelo Ricardo Leitner segs = NULL; 7290017accSMarcelo Ricardo Leitner goto out; 7390017accSMarcelo Ricardo Leitner } 7490017accSMarcelo Ricardo Leitner 755207f399SXin Long segs = skb_segment(skb, features | NETIF_F_HW_CSUM | NETIF_F_SG); 7690017accSMarcelo Ricardo Leitner if (IS_ERR(segs)) 7790017accSMarcelo Ricardo Leitner goto out; 7890017accSMarcelo Ricardo Leitner 7990017accSMarcelo Ricardo Leitner /* All that is left is update SCTP CRC if necessary */ 8090017accSMarcelo Ricardo Leitner if (!(features & NETIF_F_SCTP_CRC)) { 8190017accSMarcelo Ricardo Leitner for (skb = segs; skb; skb = skb->next) { 8290017accSMarcelo Ricardo Leitner if (skb->ip_summed == CHECKSUM_PARTIAL) { 8390017accSMarcelo Ricardo Leitner sh = sctp_hdr(skb); 8490017accSMarcelo Ricardo Leitner sh->checksum = sctp_gso_make_checksum(skb); 8590017accSMarcelo Ricardo Leitner } 8690017accSMarcelo Ricardo Leitner } 8790017accSMarcelo Ricardo Leitner } 8890017accSMarcelo Ricardo Leitner 8990017accSMarcelo Ricardo Leitner out: 9090017accSMarcelo Ricardo Leitner return segs; 9190017accSMarcelo Ricardo Leitner } 9290017accSMarcelo Ricardo Leitner 9390017accSMarcelo Ricardo Leitner static const struct net_offload sctp_offload = { 9490017accSMarcelo Ricardo Leitner .callbacks = { 9590017accSMarcelo Ricardo Leitner .gso_segment = sctp_gso_segment, 9690017accSMarcelo Ricardo Leitner }, 9790017accSMarcelo Ricardo Leitner }; 9890017accSMarcelo Ricardo Leitner 99c5c4e45cSMarcelo Ricardo Leitner static const struct net_offload sctp6_offload = { 100c5c4e45cSMarcelo Ricardo Leitner .callbacks = { 101c5c4e45cSMarcelo Ricardo Leitner .gso_segment = sctp_gso_segment, 102c5c4e45cSMarcelo Ricardo Leitner }, 103c5c4e45cSMarcelo Ricardo Leitner }; 104c5c4e45cSMarcelo Ricardo Leitner 1059617813dSDavide Caratti static const struct skb_checksum_ops crc32c_csum_ops = { 1069617813dSDavide Caratti .update = sctp_csum_update, 1079617813dSDavide Caratti .combine = sctp_csum_combine, 1089617813dSDavide Caratti }; 1099617813dSDavide Caratti 11090017accSMarcelo Ricardo Leitner int __init sctp_offload_init(void) 11190017accSMarcelo Ricardo Leitner { 112c5c4e45cSMarcelo Ricardo Leitner int ret; 113c5c4e45cSMarcelo Ricardo Leitner 114c5c4e45cSMarcelo Ricardo Leitner ret = inet_add_offload(&sctp_offload, IPPROTO_SCTP); 115c5c4e45cSMarcelo Ricardo Leitner if (ret) 116c5c4e45cSMarcelo Ricardo Leitner goto out; 117c5c4e45cSMarcelo Ricardo Leitner 118c5c4e45cSMarcelo Ricardo Leitner ret = inet6_add_offload(&sctp6_offload, IPPROTO_SCTP); 119c5c4e45cSMarcelo Ricardo Leitner if (ret) 120c5c4e45cSMarcelo Ricardo Leitner goto ipv4; 121c5c4e45cSMarcelo Ricardo Leitner 1229617813dSDavide Caratti crc32c_csum_stub = &crc32c_csum_ops; 123c5c4e45cSMarcelo Ricardo Leitner return ret; 124c5c4e45cSMarcelo Ricardo Leitner 125c5c4e45cSMarcelo Ricardo Leitner ipv4: 126c5c4e45cSMarcelo Ricardo Leitner inet_del_offload(&sctp_offload, IPPROTO_SCTP); 127c5c4e45cSMarcelo Ricardo Leitner out: 128c5c4e45cSMarcelo Ricardo Leitner return ret; 12990017accSMarcelo Ricardo Leitner } 130