From nobody Fri Dec 19 03:43:24 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 549DFEB64DD for ; Mon, 14 Aug 2023 09:25:27 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235627AbjHNJY5 (ORCPT ); Mon, 14 Aug 2023 05:24:57 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57124 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235527AbjHNJY2 (ORCPT ); Mon, 14 Aug 2023 05:24:28 -0400 Received: from mail-lf1-x134.google.com (mail-lf1-x134.google.com [IPv6:2a00:1450:4864:20::134]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 66136113; Mon, 14 Aug 2023 02:24:26 -0700 (PDT) Received: by mail-lf1-x134.google.com with SMTP id 2adb3069b0e04-4fe9f226cdbso2387495e87.0; Mon, 14 Aug 2023 02:24:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1692005065; x=1692609865; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=mKwBqPEgaF7GGJd1uZ0G836C9Z+tCMwn3BXMzLPwzdM=; b=EPCmrUAqbNfcwXjEIIWR5/2CJFasKFl4syMWkPF3FZuE8cWFVzU7C2keep9qhC1Gm3 LEIYXayrlLTjmhmk7BUMYah7XOt02nQSYMzr0mzubOW3lBNpnCPFcc44Px0hDcyZg9I9 ca5Cg1Wa5cqdV+DWu/EZmV3GeAZ5Yz9VUyRHAD4ovB1NwMt+XkhcRR9/7Jr5K9dztGbg OdUnE6lk7/XlfLauNU1Ot8Ij6yIyc+VnZ5uI7EIGbu0LvVCKkX8UY0pugl7iYhFOWtiE ef0LolAiUJaYasQD+zkbM8IxlNdqGnnte2XIKh/xVq2AamAtP0Uxn655GZH8c+UJc6mf syMw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692005065; x=1692609865; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=mKwBqPEgaF7GGJd1uZ0G836C9Z+tCMwn3BXMzLPwzdM=; b=U7hp2znHnealmQuK5q2lJuhEUTEp8ZQuQoAwMn81SseSK7Qn1eklUkkRPBJCcTDJxI GDvl4ji6kXcyhoijZ+0NUki+HmOSbTFdyPKhIHKTSmfT+Go0dTPj9XYwh6EUBDF4cAJt FO3sKP7dCGr4O3XpLagAmpeAwKEgtAA9NfU6SRw4rUYE0vIPUQZNTYlUgXwQWEl4BzGQ rwLf96XAjLeItnJDWO5xvCCGW86UxykwpPGWoqSTaH2p1aD7ImA1UE/PfBG9oPiUjtJK Jq9K67AEdDJ+4tJ8daPqyyZVr/WsStFjFpynx8GFyop2B6o0cMNdjBLxiEADo8JEOemF sOJQ== X-Gm-Message-State: AOJu0Ywf+GHEBH6P9ciI6rHN4NtnY1puobq6oBUUqsmfAOORVHnguJd7 9VXa35h8xHlcfKMQjw1wseM= X-Google-Smtp-Source: AGHT+IGpwp+ZyqfMY5B/+RRrjUgb101LBbd5850Ez4yGUlneGmQzeFNZbbwE46GHa/FFBh0DpJtlUA== X-Received: by 2002:a05:6512:48da:b0:4fe:61f:3025 with SMTP id er26-20020a05651248da00b004fe061f3025mr5557600lfb.61.1692005064559; Mon, 14 Aug 2023 02:24:24 -0700 (PDT) Received: from micheledallerive.home ([2a02:1210:6051:ec00:61e9:3767:83a6:9bd9]) by smtp.gmail.com with ESMTPSA id b5-20020aa7c6c5000000b005224d960e66sm5314879eds.96.2023.08.14.02.24.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 14 Aug 2023 02:24:24 -0700 (PDT) From: Michele Dalle Rive To: Miguel Ojeda , Alex Gaynor , Wedson Almeida Filho , "David S. Miller" Cc: Eric Dumazet , Jakub Kicinski , Paolo Abeni , Boqun Feng , Gary Guo , =?UTF-8?q?Bj=C3=B6rn=20Roy=20Baron?= , Benno Lossin , Alice Ryhl , Davide Rovelli , rust-for-linux@vger.kernel.org, netdev@vger.kernel.org, linux-kernel@vger.kernel.org, patches@lists.linux.dev, Michele Dalle Rive Subject: [RFC PATCH 1/7] rust: net: add net module files and shared enums. Date: Mon, 14 Aug 2023 11:22:56 +0200 Message-ID: <20230814092302.1903203-2-dallerivemichele@gmail.com> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230814092302.1903203-1-dallerivemichele@gmail.com> References: <20230814092302.1903203-1-dallerivemichele@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Create `net` module files and network headers in `bindings_helper.h`. Add `IpProtocol`, `AddressFamily` and `Namespace`. The wrappers added with this patch are shared across the whole network subsystem. For this reason, they are placed in the `net.rs` module file. The enum `IpProtocol`, however, is placed in an individual `ip.rs` submodule, allowing to place together all the ip-related structures, such as wrappers for `iphdr`, `ip_auth_hdr`, etc. Signed-off-by: Michele Dalle Rive --- rust/bindings/bindings_helper.h | 3 + rust/kernel/lib.rs | 2 + rust/kernel/net.rs | 180 ++++++++++++++++++++++++++++++++ rust/kernel/net/ip.rs | 73 +++++++++++++ 4 files changed, 258 insertions(+) create mode 100644 rust/kernel/net.rs create mode 100644 rust/kernel/net/ip.rs diff --git a/rust/bindings/bindings_helper.h b/rust/bindings/bindings_helpe= r.h index 058954961bfc..7cc1bd73c77a 100644 --- a/rust/bindings/bindings_helper.h +++ b/rust/bindings/bindings_helper.h @@ -7,6 +7,9 @@ */ =20 #include +#include +#include +#include #include #include #include diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs index 85b261209977..8e6926d965e1 100644 --- a/rust/kernel/lib.rs +++ b/rust/kernel/lib.rs @@ -34,6 +34,8 @@ pub mod error; pub mod init; pub mod ioctl; +#[cfg(CONFIG_NET)] +pub mod net; pub mod prelude; pub mod print; mod static_assert; diff --git a/rust/kernel/net.rs b/rust/kernel/net.rs new file mode 100644 index 000000000000..1eda336b48e4 --- /dev/null +++ b/rust/kernel/net.rs @@ -0,0 +1,180 @@ +// SPDX-License-Identifier: GPL-2.0 + +//! Network subsystem. +//! +//! This module contains the kernel APIs related to networking that have b= een ported or wrapped for +//! usage by Rust code in the kernel. +//! +//! C header: [`include/linux/net.h`](../../../../include/linux/net.h) and= related + +use crate::error::{code, Error}; +use core::cell::UnsafeCell; +pub mod ip; + +/// The address family. +/// +/// See [`man 7 address families`](https://man7.org/linux/man-pages/man7/a= ddress_families.7.html) for more information. +#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] +pub enum AddressFamily { + /// Unspecified address family. + Unspec =3D bindings::AF_UNSPEC as isize, + /// Local to host (pipes and file-domain). + Unix =3D bindings::AF_UNIX as isize, + /// Internetwork: UDP, TCP, etc. + Inet =3D bindings::AF_INET as isize, + /// Amateur radio AX.25. + Ax25 =3D bindings::AF_AX25 as isize, + /// IPX. + Ipx =3D bindings::AF_IPX as isize, + /// Appletalk DDP. + Appletalk =3D bindings::AF_APPLETALK as isize, + /// AX.25 packet layer protocol. + Netrom =3D bindings::AF_NETROM as isize, + /// Bridge link. + Bridge =3D bindings::AF_BRIDGE as isize, + /// ATM PVCs. + Atmpvc =3D bindings::AF_ATMPVC as isize, + /// X.25 (ISO-8208). + X25 =3D bindings::AF_X25 as isize, + /// IPv6. + Inet6 =3D bindings::AF_INET6 as isize, + /// ROSE protocol. + Rose =3D bindings::AF_ROSE as isize, + /// DECnet protocol. + Decnet =3D bindings::AF_DECnet as isize, + /// 802.2LLC project. + Netbeui =3D bindings::AF_NETBEUI as isize, + /// Firewall hooks. + Security =3D bindings::AF_SECURITY as isize, + /// Key management protocol. + Key =3D bindings::AF_KEY as isize, + /// Netlink. + Netlink =3D bindings::AF_NETLINK as isize, + /// Low-level packet interface. + Packet =3D bindings::AF_PACKET as isize, + /// Acorn Econet protocol. + Econet =3D bindings::AF_ECONET as isize, + /// ATM SVCs. + Atmsvc =3D bindings::AF_ATMSVC as isize, + /// RDS sockets. + Rds =3D bindings::AF_RDS as isize, + /// IRDA sockets. + Irda =3D bindings::AF_IRDA as isize, + /// Generic PPP. + Pppox =3D bindings::AF_PPPOX as isize, + /// Legacy WAN networks protocol. + Wanpipe =3D bindings::AF_WANPIPE as isize, + /// LLC protocol. + Llc =3D bindings::AF_LLC as isize, + /// Infiniband. + Ib =3D bindings::AF_IB as isize, + /// Multiprotocol label switching. + Mpls =3D bindings::AF_MPLS as isize, + /// Controller Area Network. + Can =3D bindings::AF_CAN as isize, + /// TIPC sockets. + Tipc =3D bindings::AF_TIPC as isize, + /// Bluetooth sockets. + Bluetooth =3D bindings::AF_BLUETOOTH as isize, + /// IUCV sockets. + Iucv =3D bindings::AF_IUCV as isize, + /// RxRPC sockets. + Rxrpc =3D bindings::AF_RXRPC as isize, + /// Modular ISDN protocol. + Isdn =3D bindings::AF_ISDN as isize, + /// Nokia cellular modem interface. + Phonet =3D bindings::AF_PHONET as isize, + /// IEEE 802.15.4 sockets. + Ieee802154 =3D bindings::AF_IEEE802154 as isize, + /// CAIF sockets. + Caif =3D bindings::AF_CAIF as isize, + /// Kernel crypto API + Alg =3D bindings::AF_ALG as isize, + /// VMware VSockets. + Vsock =3D bindings::AF_VSOCK as isize, + /// KCM sockets. + Kcm =3D bindings::AF_KCM as isize, + /// Qualcomm IPC router protocol. + Qipcrtr =3D bindings::AF_QIPCRTR as isize, + /// SMC sockets. + Smc =3D bindings::AF_SMC as isize, + /// Express Data Path sockets. + Xdp =3D bindings::AF_XDP as isize, +} + +impl From for isize { + fn from(family: AddressFamily) -> Self { + family as isize + } +} + +impl TryFrom for AddressFamily { + type Error =3D Error; + + fn try_from(value: isize) -> Result { + let val =3D value as u32; + match val { + bindings::AF_UNSPEC =3D> Ok(Self::Unspec), + bindings::AF_UNIX =3D> Ok(Self::Unix), + bindings::AF_INET =3D> Ok(Self::Inet), + bindings::AF_AX25 =3D> Ok(Self::Ax25), + bindings::AF_IPX =3D> Ok(Self::Ipx), + bindings::AF_APPLETALK =3D> Ok(Self::Appletalk), + bindings::AF_NETROM =3D> Ok(Self::Netrom), + bindings::AF_BRIDGE =3D> Ok(Self::Bridge), + bindings::AF_ATMPVC =3D> Ok(Self::Atmpvc), + bindings::AF_X25 =3D> Ok(Self::X25), + bindings::AF_INET6 =3D> Ok(Self::Inet6), + bindings::AF_ROSE =3D> Ok(Self::Rose), + bindings::AF_DECnet =3D> Ok(Self::Decnet), + bindings::AF_NETBEUI =3D> Ok(Self::Netbeui), + bindings::AF_SECURITY =3D> Ok(Self::Security), + bindings::AF_KEY =3D> Ok(Self::Key), + bindings::AF_NETLINK =3D> Ok(Self::Netlink), + bindings::AF_PACKET =3D> Ok(Self::Packet), + bindings::AF_ECONET =3D> Ok(Self::Econet), + bindings::AF_ATMSVC =3D> Ok(Self::Atmsvc), + bindings::AF_RDS =3D> Ok(Self::Rds), + bindings::AF_IRDA =3D> Ok(Self::Irda), + bindings::AF_PPPOX =3D> Ok(Self::Pppox), + bindings::AF_WANPIPE =3D> Ok(Self::Wanpipe), + bindings::AF_LLC =3D> Ok(Self::Llc), + bindings::AF_IB =3D> Ok(Self::Ib), + bindings::AF_MPLS =3D> Ok(Self::Mpls), + bindings::AF_CAN =3D> Ok(Self::Can), + bindings::AF_TIPC =3D> Ok(Self::Tipc), + bindings::AF_BLUETOOTH =3D> Ok(Self::Bluetooth), + bindings::AF_IUCV =3D> Ok(Self::Iucv), + bindings::AF_RXRPC =3D> Ok(Self::Rxrpc), + bindings::AF_ISDN =3D> Ok(Self::Isdn), + bindings::AF_PHONET =3D> Ok(Self::Phonet), + bindings::AF_IEEE802154 =3D> Ok(Self::Ieee802154), + bindings::AF_CAIF =3D> Ok(Self::Caif), + bindings::AF_ALG =3D> Ok(Self::Alg), + bindings::AF_VSOCK =3D> Ok(Self::Vsock), + bindings::AF_KCM =3D> Ok(Self::Kcm), + bindings::AF_QIPCRTR =3D> Ok(Self::Qipcrtr), + bindings::AF_SMC =3D> Ok(Self::Smc), + bindings::AF_XDP =3D> Ok(Self::Xdp), + _ =3D> Err(code::EINVAL), + } + } +} + +/// Network namespace. +/// +/// Wraps the `net` struct. +#[repr(transparent)] +pub struct Namespace(UnsafeCell); + +/// The global network namespace. +/// +/// This is the default and initial namespace. +/// This function replaces the C `init_net` global variable. +pub fn init_net() -> &'static Namespace { + // SAFETY: `init_net` is a global variable and is always valid. + let ptr =3D unsafe { core::ptr::addr_of!(bindings::init_net) }; + // SAFETY: the address of `init_net` is always valid, always points to= initialized memory, + // and is always aligned. + unsafe { &*(ptr.cast()) } +} diff --git a/rust/kernel/net/ip.rs b/rust/kernel/net/ip.rs new file mode 100644 index 000000000000..84f98d356137 --- /dev/null +++ b/rust/kernel/net/ip.rs @@ -0,0 +1,73 @@ +// SPDX-License-Identifier: GPL-2.0 + +//! IP protocol definitions. +//! +//! This module contains the kernel structures and functions related to IP= protocols. +//! +//! C headers: +//! - [`include/linux/in.h`](../../../../include/linux/in.h) +//! - [`include/linux/ip.h`](../../../../include/linux/ip.h) +//! - [`include/uapi/linux/ip.h`](../../../../include/uapi/linux/ip.h) + +/// The Ip protocol. +/// +/// See [`tools/include/uapi/linux/in.h`](../../../../tools/include/uapi/l= inux/in.h) +#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] +pub enum IpProtocol { + /// Dummy protocol for TCP + Ip =3D bindings::IPPROTO_IP as isize, + /// Internet Control Message Protocol + Icmp =3D bindings::IPPROTO_ICMP as isize, + /// Internet Group Management Protocol + Igmp =3D bindings::IPPROTO_IGMP as isize, + /// IPIP tunnels (older KA9Q tunnels use 94) + IpIp =3D bindings::IPPROTO_IPIP as isize, + /// Transmission Control Protocol + Tcp =3D bindings::IPPROTO_TCP as isize, + /// Exterior Gateway Protocol + Egp =3D bindings::IPPROTO_EGP as isize, + /// PUP protocol + Pup =3D bindings::IPPROTO_PUP as isize, + /// User Datagram Protocol + Udp =3D bindings::IPPROTO_UDP as isize, + /// XNS Idp protocol + Idp =3D bindings::IPPROTO_IDP as isize, + /// SO Transport Protocol Class 4 + Tp =3D bindings::IPPROTO_TP as isize, + /// Datagram Congestion Control Protocol + Dccp =3D bindings::IPPROTO_DCCP as isize, + /// Ipv6-in-Ipv4 tunnelling + Ipv6 =3D bindings::IPPROTO_IPV6 as isize, + /// Rsvp Protocol + Rsvp =3D bindings::IPPROTO_RSVP as isize, + /// Cisco GRE tunnels (rfc 1701,1702) + Gre =3D bindings::IPPROTO_GRE as isize, + /// Encapsulation Security Payload protocol + Esp =3D bindings::IPPROTO_ESP as isize, + /// Authentication Header protocol + Ah =3D bindings::IPPROTO_AH as isize, + /// Multicast Transport Protocol + Mtp =3D bindings::IPPROTO_MTP as isize, + /// Ip option pseudo header for BEET + Beetph =3D bindings::IPPROTO_BEETPH as isize, + /// Encapsulation Header + Encap =3D bindings::IPPROTO_ENCAP as isize, + /// Protocol Independent Multicast + Pim =3D bindings::IPPROTO_PIM as isize, + /// Compression Header Protocol + Comp =3D bindings::IPPROTO_COMP as isize, + /// Layer 2 Tunnelling Protocol + L2Tp =3D bindings::IPPROTO_L2TP as isize, + /// Stream Control Transport Protocol + Sctp =3D bindings::IPPROTO_SCTP as isize, + /// Udp-Lite (Rfc 3828) + UdpLite =3D bindings::IPPROTO_UDPLITE as isize, + /// Mpls in Ip (Rfc 4023) + Mpls =3D bindings::IPPROTO_MPLS as isize, + /// Ethernet-within-Ipv6 Encapsulation + Ethernet =3D bindings::IPPROTO_ETHERNET as isize, + /// Raw Ip packets + Raw =3D bindings::IPPROTO_RAW as isize, + /// Multipath Tcp connection + Mptcp =3D bindings::IPPROTO_MPTCP as isize, +} --=20 2.41.0