From patchwork Thu Oct 8 09:48:01 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Xin Long X-Patchwork-Id: 268965 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-12.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH, MAILING_LIST_MULTI, SIGNED_OFF_BY, SPF_HELO_NONE, SPF_PASS, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id BAB7CC43467 for ; Thu, 8 Oct 2020 09:49:03 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 4C5D921531 for ; Thu, 8 Oct 2020 09:49:03 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="OJKPmxGS" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729273AbgJHJtC (ORCPT ); Thu, 8 Oct 2020 05:49:02 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41644 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729206AbgJHJtC (ORCPT ); Thu, 8 Oct 2020 05:49:02 -0400 Received: from mail-pl1-x631.google.com (mail-pl1-x631.google.com [IPv6:2607:f8b0:4864:20::631]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0CDE7C061755; Thu, 8 Oct 2020 02:49:02 -0700 (PDT) Received: by mail-pl1-x631.google.com with SMTP id m15so2497158pls.8; Thu, 08 Oct 2020 02:49:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :in-reply-to:references; bh=/bc9STVKDhynKYEAx64TcAnGKQ43YQycLHOwGW1mz3I=; b=OJKPmxGSJrR3Q0/4HbUrpisiuBR6bcvh7nDqlIACh+Pdet4KNUXNrETkTvtL/YHtaQ tPCsEkZxJQzjRej0p2S19zvXLBhB0U8+ljEoyLFjui6SJgJDbnOa/KT/oTwk45dJA+Vs SJSEW6HwHIJZ9BD9z6PWjakSzfX/F8QOOXzqLWauJw//3ABwiWVvtvmyiexAjZuzZNhj QmDwiMv5SYR9JqShvowlgmxVHpRyiznXABlLhFJFS7QcAdMp+uK0ca4Uk7ktSSt5ccMM G87RveWpkWV/jZ71InAbXVItg2R71SVDrHHtLUofSrH68cs09p2ngWzF+xZWMS2AMNcd wD3A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:in-reply-to:references; bh=/bc9STVKDhynKYEAx64TcAnGKQ43YQycLHOwGW1mz3I=; b=EP3rpXtMHVgRhxIC3AiyGtayK8IxUHcNJjZ0tDN2DlNxjMglRIyGunaOQa5hnElNYC qqXHtFlFXORAba3lpOmOj9AS+6BBwIUhxke36exoj1nPvWpEFf0ab38FDtvPYiF1IH9G UpzFX83iODDfy+pX/d0eBsxYQVolgjn2QzS69yt4dZQWgbebIEGrbKxQNn/Q0g/U1L6n YPRoiXC4vHpAbTnkgsuLeY1hyn8XcIVgeizqV3vK/4lfcq/0fk+rsKwop0yBr9VdvRM3 iyEef+hzIApbTbYHmFHf6W13OeGnCxSqMrkomVC/yzx4vOw5/Cd0SNzx2CkjBF2HKgtx sVPg== X-Gm-Message-State: AOAM5327tG/t4zROL7gQ6niDKxVNPHNYh86FBE56kJhBjUZ46fEhbN3G /flq81EizSvBio4rhZbaOeCauA647PM= X-Google-Smtp-Source: ABdhPJzSkLOVJXYVI+QHaLIhz856dmSxd7+jt4jP8qFnReJrC98A9YtAr2AZEx/UGUNiGJkI40qsRQ== X-Received: by 2002:a17:902:7d97:b029:d2:8046:f593 with SMTP id a23-20020a1709027d97b02900d28046f593mr6819456plm.43.1602150541285; Thu, 08 Oct 2020 02:49:01 -0700 (PDT) Received: from localhost ([209.132.188.80]) by smtp.gmail.com with ESMTPSA id n5sm6654275pfq.46.2020.10.08.02.49.00 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Thu, 08 Oct 2020 02:49:00 -0700 (PDT) From: Xin Long To: network dev , linux-sctp@vger.kernel.org Cc: Marcelo Ricardo Leitner , Neil Horman , Michael Tuexen , davem@davemloft.net Subject: [PATCHv2 net-next 05/17] sctp: create udp4 sock and add its encap_rcv Date: Thu, 8 Oct 2020 17:48:01 +0800 Message-Id: <6f5a15bba0e2b5d3da6be90fd222c5ee41691d32.1602150362.git.lucien.xin@gmail.com> X-Mailer: git-send-email 2.1.0 In-Reply-To: <17cab00046ea7fe36c8383925a4fc3fbc028c511.1602150362.git.lucien.xin@gmail.com> References: <052acb63198c44df41c5db17f8397eeb7c8bacfe.1602150362.git.lucien.xin@gmail.com> <483d9eec159b22172fe04dacd58d7f88dfc2f301.1602150362.git.lucien.xin@gmail.com> <17cab00046ea7fe36c8383925a4fc3fbc028c511.1602150362.git.lucien.xin@gmail.com> In-Reply-To: References: Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org This patch is to add the functions to create/release udp4 sock, and set the sock's encap_rcv to process the incoming udp encap sctp packets. In sctp_udp_rcv(), as we can see, all we need to do is fix the transport header for sctp_rcv(), then it would implement the part of rfc6951#section-5.4: "When an encapsulated packet is received, the UDP header is removed. Then, the generic lookup is performed, as done by an SCTP stack whenever a packet is received, to find the association for the received SCTP packet" Note that these functions will be called in the last patch of this patchset when enabling this feature. v1->v2: - Add pr_err() when fails to create udp v4 sock. Signed-off-by: Xin Long Reported-by: kernel test robot --- include/net/netns/sctp.h | 5 +++++ include/net/sctp/constants.h | 2 ++ include/net/sctp/sctp.h | 2 ++ net/sctp/protocol.c | 43 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 52 insertions(+) diff --git a/include/net/netns/sctp.h b/include/net/netns/sctp.h index d8d02e4..3d10bef 100644 --- a/include/net/netns/sctp.h +++ b/include/net/netns/sctp.h @@ -22,6 +22,11 @@ struct netns_sctp { */ struct sock *ctl_sock; + /* udp tunneling listening sock. */ + struct sock *udp4_sock; + /* udp tunneling listening port. */ + int udp_port; + /* This is the global local address list. * We actively maintain this complete list of addresses on * the system by catching address add/delete events. diff --git a/include/net/sctp/constants.h b/include/net/sctp/constants.h index 122d9e2..b583166 100644 --- a/include/net/sctp/constants.h +++ b/include/net/sctp/constants.h @@ -286,6 +286,8 @@ enum { SCTP_MAX_GABS = 16 }; * functions simpler to write. */ +#define SCTP_DEFAULT_UDP_PORT 9899 /* default udp tunneling port */ + /* These are the values for pf exposure, UNUSED is to keep compatible with old * applications by default. */ diff --git a/include/net/sctp/sctp.h b/include/net/sctp/sctp.h index 4fc747b..bfd87a0 100644 --- a/include/net/sctp/sctp.h +++ b/include/net/sctp/sctp.h @@ -84,6 +84,8 @@ int sctp_copy_local_addr_list(struct net *net, struct sctp_bind_addr *addr, struct sctp_pf *sctp_get_pf_specific(sa_family_t family); int sctp_register_pf(struct sctp_pf *, sa_family_t); void sctp_addr_wq_mgmt(struct net *, struct sctp_sockaddr_entry *, int); +int sctp_udp_sock_start(struct net *net); +void sctp_udp_sock_stop(struct net *net); /* * sctp/socket.c diff --git a/net/sctp/protocol.c b/net/sctp/protocol.c index 2583323..2b7a3e1 100644 --- a/net/sctp/protocol.c +++ b/net/sctp/protocol.c @@ -44,6 +44,7 @@ #include #include #include +#include #define MAX_SCTP_PORT_HASH_ENTRIES (64 * 1024) @@ -840,6 +841,45 @@ static int sctp_ctl_sock_init(struct net *net) return 0; } +static int sctp_udp_rcv(struct sock *sk, struct sk_buff *skb) +{ + skb_set_transport_header(skb, sizeof(struct udphdr)); + sctp_rcv(skb); + return 0; +} + +int sctp_udp_sock_start(struct net *net) +{ + struct udp_tunnel_sock_cfg tuncfg = {NULL}; + struct udp_port_cfg udp_conf = {0}; + struct socket *sock; + int err; + + udp_conf.family = AF_INET; + udp_conf.local_ip.s_addr = htonl(INADDR_ANY); + udp_conf.local_udp_port = htons(net->sctp.udp_port); + err = udp_sock_create(net, &udp_conf, &sock); + if (err) { + pr_err("Failed to create the SCTP udp tunneling v4 sock\n"); + return err; + } + + tuncfg.encap_type = 1; + tuncfg.encap_rcv = sctp_udp_rcv; + setup_udp_tunnel_sock(net, sock, &tuncfg); + net->sctp.udp4_sock = sock->sk; + + return 0; +} + +void sctp_udp_sock_stop(struct net *net) +{ + if (net->sctp.udp4_sock) { + udp_tunnel_sock_release(net->sctp.udp4_sock->sk_socket); + net->sctp.udp4_sock = NULL; + } +} + /* Register address family specific functions. */ int sctp_register_af(struct sctp_af *af) { @@ -1271,6 +1311,9 @@ static int __net_init sctp_defaults_init(struct net *net) /* Enable ECN by default. */ net->sctp.ecn_enable = 1; + /* Set udp tunneling listening port to default value */ + net->sctp.udp_port = SCTP_DEFAULT_UDP_PORT; + /* Set SCOPE policy to enabled */ net->sctp.scope_policy = SCTP_SCOPE_POLICY_ENABLE;