From patchwork Fri Jul 26 16:12:13 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Denis Kenzior X-Patchwork-Id: 13742977 Received: from mail-ot1-f51.google.com (mail-ot1-f51.google.com [209.85.210.51]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1367829A5 for ; Fri, 26 Jul 2024 16:12:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.51 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722010356; cv=none; b=Y06la8+1LaKUswHmT9OZQ2wqPxuO5JxHz8JhORiUh9dSzcq00aRzfaeJYhmSyso2/xKlTS4j2ghZ9qeunE9HSEmT7yIacwB/obhbOvvXs1VuiIwYt6P4ST8gxhLDq+lX5HjccrRooyfgna+iy1QJQ3XI6Xf89pc2vcVvtG5IlrU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722010356; c=relaxed/simple; bh=ASW8Oiawmb6X/F32LpMEy2Un7XoWwFaJyR/LvbxS1j4=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=DGyMfmlu1mwej4uR3H2iKQi1/lPJBqomgM4sQJDs+R/AUxT+VP36c0aGVD72KevOPcoFwVri9TPjQTm+g04wSoasEybnFNa53oVwuAzffujSJq8qBiaWtPhGrciL8TMET0KylOYgKWDnLX1dZn1bXTHjQG1+X82wr9NBpztPx0g= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=KcxCFRv2; arc=none smtp.client-ip=209.85.210.51 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="KcxCFRv2" Received: by mail-ot1-f51.google.com with SMTP id 46e09a7af769-7093705c708so1101629a34.1 for ; Fri, 26 Jul 2024 09:12:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1722010354; x=1722615154; darn=lists.linux.dev; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=qHAh5ly47GWdHNN0RH2ogz2zlpMBhuZ/VAG+zQXtU1M=; b=KcxCFRv20MHWy7a2Qs3PjK/vcHIka3xeb4g9djQY0pJqP2e+jLP42V6t72RTrMVf9B eW0kJaw7O7jI2z837Yrv5X3f+i8ztfA/fQhqHpAw+Z7xLQhB4TungTJGMYL8+dYo3M/f n2EsFbItOrnrchlaFK1QerPHXXhPpJD386PvBVmfye4a7sgSm8FIQtnvBbFy4Ye8XUio OOAZ6pA1R/b45jtBEV6EtwSF3lOJfLJxjxjc9fxuq047Uc9m7wap3/ZLd7ZQ2/vDFZw9 Bzm6Mx+AlnmCRSKDnGw3CR/o/Fw2Nlas18V1mgGZj08WhMWceCV6yhsm/DvQ95yaO9zD ZVCw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722010354; x=1722615154; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=qHAh5ly47GWdHNN0RH2ogz2zlpMBhuZ/VAG+zQXtU1M=; b=bTYiFVKsqfnzybPOCAx5JsBlRbuJ4ZGsg809SH8L0JNQYWmJ7qintkckoW7mA/Oil+ aWbemfVkxB1NWjE+JyrpELyogB1xv6i7vVasCWEuBplwI71r4cRJzi7BGigSKQaxSidi Gkfm5KRQAbCKlHUiFzdMETvKm/u7Ur3CTRwAEINYmwlh7D7lghTFSwA9vICN1AeC7Cn3 MHVl3jfVX8ljPTu95wmREl8XHOpfxaxGI778tHLmWWRLTLNBS+OPO9pZBGf3cVZv7oxn sjbiEd33dNwiK55qh/BcY9RUyhCS6xb6QfaWurxJ5cOjcot6X76j45c+lYrI93qcCYF1 xizQ== X-Gm-Message-State: AOJu0YwgYl9p81rPhWpDp7uZqkd/TbjyT4UxNNNIplx29mPkhPtb5QWG syweGGFHNds3KEia3gtfQmBW/rHFUfdGE4KrM41dboHTz3GojdKr5HMX/g== X-Google-Smtp-Source: AGHT+IHzJb9FLOTkgfwTG5BOiP7Acp2bM864K1D614+hy4oTTmkfXXk8n7VWB71C04vrUO0yxNwcfw== X-Received: by 2002:a9d:4d87:0:b0:709:3772:8c31 with SMTP id 46e09a7af769-70940c71118mr46142a34.31.1722010354005; Fri, 26 Jul 2024 09:12:34 -0700 (PDT) Received: from localhost.localdomain (syn-070-114-247-242.res.spectrum.com. [70.114.247.242]) by smtp.gmail.com with ESMTPSA id 46e09a7af769-70930718ec9sm792997a34.30.2024.07.26.09.12.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 26 Jul 2024 09:12:33 -0700 (PDT) From: Denis Kenzior To: ell@lists.linux.dev Cc: Denis Kenzior Subject: [PATCH 1/7] netlink: Remove pointless checks Date: Fri, 26 Jul 2024 11:12:13 -0500 Message-ID: <20240726161232.146982-1-denkenz@gmail.com> X-Mailer: git-send-email 2.45.2 Precedence: bulk X-Mailing-List: ell@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 command_queue, command_pending and command_lookup structures are always initialized at construction time and are not destroyed until l_netlink destruction. There's no need to check whether they ar valid. --- ell/netlink.c | 8 -------- 1 file changed, 8 deletions(-) diff --git a/ell/netlink.c b/ell/netlink.c index 024740b62fc3..26c31679e4c7 100644 --- a/ell/netlink.c +++ b/ell/netlink.c @@ -410,10 +410,6 @@ LIB_EXPORT unsigned int l_netlink_send(struct l_netlink *netlink, if (unlikely(!netlink)) return 0; - if (!netlink->command_queue || !netlink->command_pending || - !netlink->command_lookup) - return 0; - if (flags & 0xff) return 0; @@ -471,10 +467,6 @@ LIB_EXPORT bool l_netlink_cancel(struct l_netlink *netlink, unsigned int id) if (unlikely(!netlink || !id)) return false; - if (!netlink->command_queue || !netlink->command_pending || - !netlink->command_lookup) - return false; - command = l_hashmap_remove(netlink->command_lookup, L_UINT_TO_PTR(id)); if (!command) return false; From patchwork Fri Jul 26 16:12:14 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Denis Kenzior X-Patchwork-Id: 13742978 Received: from mail-ot1-f44.google.com (mail-ot1-f44.google.com [209.85.210.44]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A06931EA84 for ; Fri, 26 Jul 2024 16:12:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.44 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722010357; cv=none; b=e+HqivvBLcUgvE7NUZwlZmVZ9pc4ji+EmQt9AkkCM1nF8EnZoVGfnYXy58E0ZIcSB0AZusYxfH7IhPxjR5lwnLaspSpd8WpmIeryuAhc9VSCjT5leaKgJUdmjioJ+I9LR7xtYVNQJcljjwP3CGCJE4c6HzQMdqaLxyBPLcZlbh8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722010357; c=relaxed/simple; bh=SBDuGoPc43t19pnVJbizIlNuze7CMqqCCvxG42npFkA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=awr1GZnBwWR/6oIZXO9XBtKwA1nRFW4Jh+KVdR5dpeQDbXUgsUwhjYKF2F0OfALICkR8d67kCOWdf9eDaSTxGgYNOnxk1LA/oPbk3sZHF8DEUEsTiTCU3X9Dg2R+YxLgnQcazW5x5WdHPqBrOspr4ycAQPiMovBFXlx0wvMWeO0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=I1MUZ9PD; arc=none smtp.client-ip=209.85.210.44 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="I1MUZ9PD" Received: by mail-ot1-f44.google.com with SMTP id 46e09a7af769-70365f905b6so859274a34.0 for ; Fri, 26 Jul 2024 09:12:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1722010355; x=1722615155; darn=lists.linux.dev; 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=xRsn/rfrdCPG+71aVif4B77Y1y1DwE5fnEWM44sYQnQ=; b=I1MUZ9PD2OnVU05Q1vX6Kijkh188ysGCWR0X/h/KxD11rVJ0l5ZVo3PT1Rv0k8hCl8 y9r0EIYLEyicxsdVa9qQAWgjgk+2B1RPBUJewW9ZRo1ZpV+kBxtegfMFdxTI0uNZ6MAi k8U2hgkVHr182gJfx5rsTnjLxGB3rexyeLytv1F4dn77CgSNBnRWH828FW/+hMfBut/k v0BPJXP4yURW4ML+YBABKDLryO8BJnBuZIb/nyvFeZwr76UsYuk0B55qDYH9TonF5cY7 +bOKKu4Nr65sJEXWX6HlBpcumpoJ7eZIoy1meGa+Z3X0+dUV2eoVib8zO6uu95TETbDg CBJQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722010355; x=1722615155; 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=xRsn/rfrdCPG+71aVif4B77Y1y1DwE5fnEWM44sYQnQ=; b=Z2oPb+X7MV8mhCFhCkcGjLrQ87AkIYzWxYKmHyGt4/UAGWB7c4UYovIZXH1X7jZ0Rz Mcus2KCpoYBKbcq/XOy7G/qIdKcc+0qkiJ3N9rwGNv0o9T8vLeMlEjfye2jTvrMdUKLb jM/hYrhW6GAmLP5BpRdDabx0KwT9zqYimzjpkjzU2489KxkOyirnjBKrKdQ2Xn9BVgRr pIh6GMLHz9k+IsQQRSVb6ITIf+nFjbt13P7tj2GYiYasCqe5K6Zyf6XjgMtQzVOubbMb 90W+gdqRnlq3PkmUhp6eip8v7YF9reIQUD5it1fgsacK0hAXZtXfdyE3GzOGSVPXfoob X15g== X-Gm-Message-State: AOJu0YygkTVu3kDHdSuOzaevFgcVRusF1Iko/QMz9OdJlHtLz8Fe2B3D 8RpiIKGZyK7KdssvudTXtwXmSX6HFrMvS/jaqB8NkN6I2RMR7AFnEGtO6A== X-Google-Smtp-Source: AGHT+IFfKJRleWZr1gkPqe82ndbw0V50xLICj2UugiERY4P+UdilHBva8WoZ9srA8rb1sqFthEXCjw== X-Received: by 2002:a05:6830:6208:b0:703:6d69:55b0 with SMTP id 46e09a7af769-70940c9e5admr57141a34.31.1722010354649; Fri, 26 Jul 2024 09:12:34 -0700 (PDT) Received: from localhost.localdomain (syn-070-114-247-242.res.spectrum.com. [70.114.247.242]) by smtp.gmail.com with ESMTPSA id 46e09a7af769-70930718ec9sm792997a34.30.2024.07.26.09.12.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 26 Jul 2024 09:12:34 -0700 (PDT) From: Denis Kenzior To: ell@lists.linux.dev Cc: Denis Kenzior Subject: [PATCH 2/7] netlink: Disallow certain flags in message constructor Date: Fri, 26 Jul 2024 11:12:14 -0500 Message-ID: <20240726161232.146982-2-denkenz@gmail.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240726161232.146982-1-denkenz@gmail.com> References: <20240726161232.146982-1-denkenz@gmail.com> Precedence: bulk X-Mailing-List: ell@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Certain flags are reserved for use by l_netlink when sending messages. Other flags don't make sense for messages that are to be sent to the kernel. Disallow their use. --- ell/netlink.c | 3 +++ unit/test-genl-msg.c | 6 ++++-- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/ell/netlink.c b/ell/netlink.c index 26c31679e4c7..58dfd9779875 100644 --- a/ell/netlink.c +++ b/ell/netlink.c @@ -751,6 +751,9 @@ LIB_EXPORT struct l_netlink_message *l_netlink_message_new_sized(uint16_t type, { struct l_netlink_message *message; + if (flags & 0xff) + return NULL; + message = l_new(struct l_netlink_message, 1); message->size = initial_len + NLMSG_HDRLEN; diff --git a/unit/test-genl-msg.c b/unit/test-genl-msg.c index 3d996f4e242a..d10a87f30e3b 100644 --- a/unit/test-genl-msg.c +++ b/unit/test-genl-msg.c @@ -134,7 +134,7 @@ static void build_set_station_netlink(const void *data) struct l_netlink_message *m; struct genlmsghdr genlhdr = { .cmd = 18, .version = 0, .reserved = 0 }; - m = l_netlink_message_new(0x17, NLM_F_REQUEST | NLM_F_ACK); + m = l_netlink_message_new(0x17, 0); assert(m); assert(!l_netlink_message_add_header(m, &genlhdr, sizeof(genlhdr))); @@ -153,6 +153,7 @@ static void build_set_station_netlink(const void *data) sizeof(set_station_request), do_debug, "[MSG] "); } + m->hdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK; assert(m->hdr->nlmsg_len == sizeof(set_station_request)); assert(!memcmp(m->data, set_station_request, sizeof(set_station_request))); @@ -292,7 +293,7 @@ static void build_set_rekey_offload_netlink(const void *data) struct l_netlink_message *m; struct genlmsghdr genlhdr = { .cmd = 79, .version = 0, .reserved = 0 }; - m = l_netlink_message_new(0x1b, NLM_F_REQUEST | NLM_F_ACK); + m = l_netlink_message_new(0x1b, 0); assert(m); assert(!l_netlink_message_add_header(m, &genlhdr, sizeof(genlhdr))); @@ -315,6 +316,7 @@ static void build_set_rekey_offload_netlink(const void *data) do_debug, "[MSG] "); } + m->hdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK; assert(m->hdr->nlmsg_len == sizeof(set_rekey_offload_request)); assert(!memcmp(m->data, set_rekey_offload_request, sizeof(set_rekey_offload_request))); From patchwork Fri Jul 26 16:12:15 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Denis Kenzior X-Patchwork-Id: 13742979 Received: from mail-ot1-f45.google.com (mail-ot1-f45.google.com [209.85.210.45]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 563F7200AF for ; Fri, 26 Jul 2024 16:12:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.45 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722010357; cv=none; b=pG9uf4YwZxrnXIVfdzfgs3Tcr2JdU79HO31vBrjxkmer7jJcHEv6fQfOr96ePcrDLDMgNq3uTYOrNffpL36purnOBiWqhJ9+5ZLxWANLtkFZnrQ/j8g2i2s+Ht2YDS68ieweeO/s0rkc4PcIIeeWsPkSsdYOIKk/lUu5tXY8gtI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722010357; c=relaxed/simple; bh=s9RflFMYf6MP0p2AF5NlXtTyYOmhwqDUS0WJFqbncOs=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=qZ3iB3e90ISI8RH470pFMu7+rzRbHPOcAq8yECBhupntdWTDTFV4UaJTae3JHTt0JqptL1FY7zg0WfIAwWQk4PlyXLLBkUKUahDcCItm4rUtNKx72ktt7R2ntLO8Khc6XLNoCoVHkqLltTGQgi6kSu1UAkBbvhELKETCmRSiaW4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=Y4oQPaYW; arc=none smtp.client-ip=209.85.210.45 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="Y4oQPaYW" Received: by mail-ot1-f45.google.com with SMTP id 46e09a7af769-708cf5138b6so865371a34.0 for ; Fri, 26 Jul 2024 09:12:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1722010355; x=1722615155; darn=lists.linux.dev; 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=B2a5qlA9RJZBQ/NjhdfqQouKqpdeNi6h4cVFnIVkXCI=; b=Y4oQPaYWN/43KPOiyaoCUQKmx6TPGpALyyZ0bBc3OYUCaO+W4GW/c5DOOtVuQpuk1d fN5d8ymPL7wkFeGqSfjWO1Bs5ZQ9BmF+eUMwpv5t4fFigcGENzgSfVp97DBAtpQ1017U M+sHl8CepDox4AZcMQtjx3cr/wsV5IfMN9fmIIfU2dLp76KBCBRXRhX2MOaGG/J8esAV ULIpxYspJ2xJf8NQgrzLx0TBmZdc51R/UqSeQg6wXSBuJVE4D9FwCZ6xhY/ROv+AUvQ1 MeHge90SSenxhCHJXO7DLUSXcmP1kWFvdJMKBnB+u7UX9zYbudoH0BZzH8NhbfAEjZG/ 7U7Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722010355; x=1722615155; 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=B2a5qlA9RJZBQ/NjhdfqQouKqpdeNi6h4cVFnIVkXCI=; b=F7aUZab1g/IEDEujxbnsrKa0zfSBpYXlVyhNgTf8fJvpxBNkNR65MTGGbmASpf454A UXa9cnzN8SuTM0klP6FtA66ZnL4HC9E958Tr0ywN4T0jrh0OAE9jxKdcl1apC86yUnAZ lAcNHUUBML0SoYGrH+ldtYy0lZrxwuESWO1wWOeiR1Dmo9EYF9NnZQyONhcY6rXblRhB /9ZEvX/ySlv4VJCGgBn2QV8aqieGExoAf7gx3j+noWRUrUcuU/mFlR3IPseqvYh2gQwL l+L2CPtwCEW15QvzQiit3nutFi7ly7krxLhpYSdmEX46Ya8NFQdKzAeAIubKYWL/719c 5CbA== X-Gm-Message-State: AOJu0YwevPqTH7h5tXXf+3yBnbEyPNEvi0WyCDZF0zoM49D5XLA8iAdG uUxnrKFgGvfS3YMKMPbuuBd8VldYXm9keaCSqDyZPhr9OSp5AfrOPGpjSw== X-Google-Smtp-Source: AGHT+IGaCG7xpCeay7OUILy6vmvstUdB5DfJAXcygvhCMvqTAxdsB32BV+eScidvcy0VPlqgMLAFaA== X-Received: by 2002:a9d:77ca:0:b0:709:2721:ad44 with SMTP id 46e09a7af769-70940c0eb28mr74712a34.12.1722010355315; Fri, 26 Jul 2024 09:12:35 -0700 (PDT) Received: from localhost.localdomain (syn-070-114-247-242.res.spectrum.com. [70.114.247.242]) by smtp.gmail.com with ESMTPSA id 46e09a7af769-70930718ec9sm792997a34.30.2024.07.26.09.12.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 26 Jul 2024 09:12:35 -0700 (PDT) From: Denis Kenzior To: ell@lists.linux.dev Cc: Denis Kenzior Subject: [PATCH 3/7] rtnl: Add a builder for RTM_{NEW|DEL}ROUTE messages Date: Fri, 26 Jul 2024 11:12:15 -0500 Message-ID: <20240726161232.146982-3-denkenz@gmail.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240726161232.146982-1-denkenz@gmail.com> References: <20240726161232.146982-1-denkenz@gmail.com> Precedence: bulk X-Mailing-List: ell@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 This builder is based on the new l_netlink_message APIs and is much simpler to understand. Use 'rtm' for struct rtmsg since that is the common convention. --- ell/rtnl-private.h | 4 +++ ell/rtnl.c | 76 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+) diff --git a/ell/rtnl-private.h b/ell/rtnl-private.h index 8af2ca6d52aa..ade43b3f053a 100644 --- a/ell/rtnl-private.h +++ b/ell/rtnl-private.h @@ -28,3 +28,7 @@ struct l_rtnl_route { uint32_t priority; uint8_t preference; }; + +struct l_netlink_message *rtnl_message_from_route(uint16_t type, uint16_t flags, + int ifindex, + const struct l_rtnl_route *rt); diff --git a/ell/rtnl.c b/ell/rtnl.c index ad2dca2f9e3c..dc3f456e0110 100644 --- a/ell/rtnl.c +++ b/ell/rtnl.c @@ -27,6 +27,7 @@ #include "log.h" #include "util.h" #include "time.h" +#include "netlink-private.h" #include "rtnl-private.h" #include "rtnl.h" #include "private.h" @@ -714,6 +715,23 @@ static size_t rta_add_address(void *rta_buf, unsigned short type, return 0; } +static int append_address(struct l_netlink_message *nlm, uint16_t type, + uint8_t family, + const struct in6_addr *v6, + const struct in_addr *v4) +{ + switch (family) { + case AF_INET6: + l_netlink_message_append(nlm, type, v6, sizeof(struct in6_addr)); + return 0; + case AF_INET: + l_netlink_message_append(nlm, type, v4, sizeof(struct in_addr)); + return 0; + } + + return -EAFNOSUPPORT; +} + static void l_rtnl_route_extract(const struct rtmsg *rtmsg, uint32_t len, int family, uint32_t *table, uint32_t *ifindex, uint32_t *priority, uint8_t *pref, @@ -1568,3 +1586,61 @@ LIB_EXPORT struct l_netlink *l_rtnl_get() return rtnl; } + +struct l_netlink_message *rtnl_message_from_route(uint16_t type, uint16_t flags, + int ifindex, + const struct l_rtnl_route *rt) +{ + struct l_netlink_message *nlm = l_netlink_message_new(type, flags); + uint64_t now = l_time_now(); + struct rtmsg rtm; + + memset(&rtm, 0, sizeof(struct rtmsg)); + rtm.rtm_family = rt->family; + rtm.rtm_table = RT_TABLE_MAIN; + rtm.rtm_protocol = rt->protocol; + rtm.rtm_type = RTN_UNICAST; + rtm.rtm_scope = rt->scope; + rtm.rtm_dst_len = rt->dst_prefix_len; + + l_netlink_message_add_header(nlm, &rtm, sizeof(rtm)); + l_netlink_message_append_u32(nlm, RTA_OIF, ifindex); + + if (rt->priority) + l_netlink_message_append_u32(nlm, RTA_PRIORITY, + rt->priority + ifindex); + + if (!address_is_null(rt->family, &rt->gw.in_addr, &rt->gw.in6_addr)) + append_address(nlm, RTA_GATEWAY, rt->family, + &rt->gw.in6_addr, &rt->gw.in_addr); + + if (rt->dst_prefix_len) + append_address(nlm, RTA_DST, rt->family, + &rt->dst.in6_addr, &rt->dst.in_addr); + + if (!address_is_null(rt->family, &rt->prefsrc.in_addr, + &rt->prefsrc.in6_addr)) + append_address(nlm, RTA_PREFSRC, rt->family, + &rt->prefsrc.in6_addr, + &rt->prefsrc.in_addr); + + if (rt->mtu) { + /* + * NOTE: Legacy RTNL messages do not use NLA_F_NESTED flag + * as they should. l_netlink_message_enter_nested does. The + * kernel should still accept this however + */ + l_netlink_message_enter_nested(nlm, RTA_METRICS); + l_netlink_message_append_u32(nlm, RTAX_MTU, rt->mtu); + l_netlink_message_leave_nested(nlm); + } + + if (rt->preference) + l_netlink_message_append_u8(nlm, RTA_PREF, rt->preference); + + if (rt->expiry_time > now) + l_netlink_message_append_u32(nlm, RTA_EXPIRES, + l_time_to_secs(rt->expiry_time - now)); + + return nlm; +} From patchwork Fri Jul 26 16:12:16 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Denis Kenzior X-Patchwork-Id: 13742980 Received: from mail-ot1-f41.google.com (mail-ot1-f41.google.com [209.85.210.41]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3618617BB1F for ; Fri, 26 Jul 2024 16:12:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.41 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722010358; cv=none; b=gJZyu0x5rX10fH63ofUicE2elSQg7EUaASRYOQMNCCMJ/d2Q6qzNziFI9ZJX4xETDXWpxIcc2zjkQjZHqZh92xBpXrW9jWXqg8J5fK+foqhbtRn9IuJtFvVVS03AHHNRCd0YLV2uW+0XHvHm0dQaCuEeJ2cZ6Q3UilCN9LEaKOI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722010358; c=relaxed/simple; bh=RsmyEV2w9iKQdnzsr+ywfsif45L35bUnGSXc1NqLRPQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=tTOopFM1nqaHUrhb77GTNjCZs35MVTXpnITdWxmV2ihH7GE9xn3HDNGMl5upoTjbssOaFKgLWuNjcmnPDpxAMbLwIp1RINnniNeX4qe2vSt+K4oTeB9NiVIGZAhgAyiWdVxi3uDLIpAk5Gff+wWtoUtX5Kbdm1kbioEVWmoX1b8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=Oqn8h/gs; arc=none smtp.client-ip=209.85.210.41 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="Oqn8h/gs" Received: by mail-ot1-f41.google.com with SMTP id 46e09a7af769-7037a208ff5so745189a34.0 for ; Fri, 26 Jul 2024 09:12:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1722010356; x=1722615156; darn=lists.linux.dev; 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=sFr7czbaeoJ1L7Nz/COBeeEwi3Gc7sHep8ZCzITtmcc=; b=Oqn8h/gsJFPjKhmTGwerBIcX1li8Y49wvZYCiS4Ms7xBtYbkw/rG9EQNgava3tbzWQ hI+7FaeLXAZqxzwoew227V6kRzMv7whR3AbIKeks5RGi98IXSKiDYd8ArU+6q6MA+kXb hH7pbnepULGzf3YAS6YuzUKvnm8qILbVKJK4CaPcbObag7oPTtceWcJFaIkQ/xf4iC00 QckkbaiCS32aPIzD7DUbswdin748tDQX1iwPLDSvXRDumxm4WQz3loAd8kpqx6e6rmQO 5TgFTe52tavn+QpMhOtV1Ad79dowMFnOV6DOOzADWtgZaUbYf37dO66knQucIhXr7AAG VNLA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722010356; x=1722615156; 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=sFr7czbaeoJ1L7Nz/COBeeEwi3Gc7sHep8ZCzITtmcc=; b=h49bLaOOOM232X5ZWPf7MFxGOg1FZJ+/m5JzZw0NcQnB7R28cXkhh2wv4FVliBk2fQ FfvfCr6MbdVN2y6wV3iTPNhY3CE3kE74zeg+qxj+ZkJXFnlCeZ265CZ6Y2G8E2bADVq5 0CHdvMuSOnS2rQXGSFppAzcwJvbu/BZtOSLnDYApL9qvcjLRQsLW48o07q94RrYttlk2 8s6tCuP0hqwOXVAXYFn60x89CewXU7hdfqeXA6u2sZiKXVDVpA4hphmGgK48nAXG53mW e9bOR0CHco3ocDTY5zyW0gIzGGDf17YGcTZmtkybqhmetNi+GV82kj/tocCesZpm6GV5 mK5A== X-Gm-Message-State: AOJu0YwRGlDcWFphU450ZyhLYqVT1aSK2s50UnSLA2DewLJLRBSXcMj/ T2ExOyHU8nG58r26uHm3F5bg7SIAxMaGtw/DFNB4+cUM6ONtyzsiLaKs6g== X-Google-Smtp-Source: AGHT+IG3KHj9qbOT3DMt0WU0r0QRV1ol4Zu1+vl3zQB45JGDErFXbz2H7DDEycL/uDs0RNnNconyEw== X-Received: by 2002:a9d:6e14:0:b0:703:5ccb:85f3 with SMTP id 46e09a7af769-70940c701d5mr49415a34.22.1722010356161; Fri, 26 Jul 2024 09:12:36 -0700 (PDT) Received: from localhost.localdomain (syn-070-114-247-242.res.spectrum.com. [70.114.247.242]) by smtp.gmail.com with ESMTPSA id 46e09a7af769-70930718ec9sm792997a34.30.2024.07.26.09.12.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 26 Jul 2024 09:12:35 -0700 (PDT) From: Denis Kenzior To: ell@lists.linux.dev Cc: Denis Kenzior Subject: [PATCH 4/7] rtnl: Add a builder for RTM_{NEW|DEL}ADDR messages Date: Fri, 26 Jul 2024 11:12:16 -0500 Message-ID: <20240726161232.146982-4-denkenz@gmail.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240726161232.146982-1-denkenz@gmail.com> References: <20240726161232.146982-1-denkenz@gmail.com> Precedence: bulk X-Mailing-List: ell@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 This builder is based on the new l_netlink_message APIs and is much simpler to understand. Use 'ifa' for struct ifaddrmsg since that is the common convention. --- ell/rtnl-private.h | 20 +++++++++++++ ell/rtnl.c | 73 +++++++++++++++++++++++++++++++++++----------- 2 files changed, 76 insertions(+), 17 deletions(-) diff --git a/ell/rtnl-private.h b/ell/rtnl-private.h index ade43b3f053a..fe9cbf35b232 100644 --- a/ell/rtnl-private.h +++ b/ell/rtnl-private.h @@ -5,6 +5,23 @@ * SPDX-License-Identifier: LGPL-2.1-or-later */ +struct l_rtnl_address { + uint8_t family; + uint8_t prefix_len; + uint8_t scope; + union { + struct in6_addr in6_addr; + struct in_addr in_addr; + }; + struct in_addr broadcast; + char label[IFNAMSIZ]; + uint32_t preferred_lifetime; + uint32_t valid_lifetime; + uint64_t preferred_expiry_time; + uint64_t valid_expiry_time; + uint32_t flags; +}; + struct l_rtnl_route { uint8_t family; uint8_t scope; @@ -32,3 +49,6 @@ struct l_rtnl_route { struct l_netlink_message *rtnl_message_from_route(uint16_t type, uint16_t flags, int ifindex, const struct l_rtnl_route *rt); +struct l_netlink_message *rtnl_message_from_address(uint16_t type, + uint16_t flags, int ifindex, + const struct l_rtnl_address *addr); diff --git a/ell/rtnl.c b/ell/rtnl.c index dc3f456e0110..90ff61c09d13 100644 --- a/ell/rtnl.c +++ b/ell/rtnl.c @@ -34,23 +34,6 @@ static struct l_netlink *rtnl; -struct l_rtnl_address { - uint8_t family; - uint8_t prefix_len; - uint8_t scope; - union { - struct in6_addr in6_addr; - struct in_addr in_addr; - }; - struct in_addr broadcast; - char label[IFNAMSIZ]; - uint32_t preferred_lifetime; - uint32_t valid_lifetime; - uint64_t preferred_expiry_time; - uint64_t valid_expiry_time; - uint32_t flags; -}; - static inline int address_to_string(int family, const struct in_addr *v4, const struct in6_addr *v6, char *out_address) @@ -1644,3 +1627,59 @@ struct l_netlink_message *rtnl_message_from_route(uint16_t type, uint16_t flags, return nlm; } + +struct l_netlink_message *rtnl_message_from_address(uint16_t type, + uint16_t flags, int ifindex, + const struct l_rtnl_address *addr) +{ + struct l_netlink_message *nlm = l_netlink_message_new(type, flags); + struct ifaddrmsg ifa; + uint64_t now = l_time_now(); + + memset(&ifa, 0, sizeof(ifa)); + ifa.ifa_index = ifindex; + ifa.ifa_family = addr->family; + ifa.ifa_scope = addr->scope; + ifa.ifa_prefixlen = addr->prefix_len; + /* Kernel ignores legacy flags in IFA_FLAGS, so set them here */ + ifa.ifa_flags = addr->flags & 0xff; + + l_netlink_message_add_header(nlm, &ifa, sizeof(ifa)); + + if (addr->family == AF_INET) { + l_netlink_message_append(nlm, IFA_LOCAL, &addr->in_addr, + sizeof(struct in_addr)); + l_netlink_message_append(nlm, IFA_BROADCAST, &addr->broadcast, + sizeof(struct in_addr)); + } else + l_netlink_message_append(nlm, IFA_LOCAL, &addr->in6_addr, + sizeof(struct in6_addr)); + + /* Address & Prefix length are enough to perform deletions */ + if (type == RTM_DELADDR) + goto done; + + if (addr->flags & 0xffffff00) + l_netlink_message_append_u32(nlm, IFA_FLAGS, + addr->flags & 0xffffff00); + + if (addr->label[0]) + l_netlink_message_append(nlm, IFA_LABEL, + addr->label, strlen(addr->label) + 1); + + if (addr->preferred_expiry_time > now || + addr->valid_expiry_time > now) { + struct ifa_cacheinfo cinfo; + + memset(&cinfo, 0, sizeof(cinfo)); + cinfo.ifa_prefered = addr->preferred_expiry_time > now ? + l_time_to_secs(addr->preferred_expiry_time - now) : 0; + cinfo.ifa_valid = addr->valid_expiry_time > now ? + l_time_to_secs(addr->valid_expiry_time - now) : 0; + + l_netlink_message_append(nlm, IFA_CACHEINFO, + &cinfo, sizeof(cinfo)); + } +done: + return nlm; +} From patchwork Fri Jul 26 16:12:17 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Denis Kenzior X-Patchwork-Id: 13742982 Received: from mail-ot1-f47.google.com (mail-ot1-f47.google.com [209.85.210.47]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6509B200AF for ; Fri, 26 Jul 2024 16:12:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.47 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722010360; cv=none; b=OcUjq3ZLzJllZezY/WJWKJm3oRHzaI7U7OOgfERBHZE6K5XpkuwTuNyD6908dFBXqy1KdJaTW6t6Qm1DMh2sQ/wgi/G9lGnWLLXIFSofMMihPc60wiOnAivr+7GgSHZHuGiVjNRYfY+kApzVsDO+s8FR/X55ukq6urENApV2fH0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722010360; c=relaxed/simple; bh=6qzZEigpv5QlG7OpjZ/0MbMZZMTvFY4iVI4yO3v/5JQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=tn4pbxGZ5x7wnBLeECbS0XnFpbmEy6FZQIcvNObfJY6rMvKg/5OKOTw9jOXylgcQEHc7IEdzeF4U5PkvmcsU/ssGjUlobyLB3UlylUO/HrJsM0xPf4ICXrqlHS5LtxoICLenJSjA0kuLqwQ2OPz45UOYLjt9ESvb7xWuNAvw4WY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=igCHzcSP; arc=none smtp.client-ip=209.85.210.47 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="igCHzcSP" Received: by mail-ot1-f47.google.com with SMTP id 46e09a7af769-7093472356dso665465a34.0 for ; Fri, 26 Jul 2024 09:12:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1722010357; x=1722615157; darn=lists.linux.dev; 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=bygahLkMum3pwIQh8kLmkuKwRo3R1qnjWtlLvb6L1gA=; b=igCHzcSP77UxCooX6DPqQzTx3skipmJwWPbAgy76Ywn5pVCR28pNUurSQaJZGauMgH +xBZvdpyuRt+DaVvoODHpavgngWcn4u0YEkn0dQ+hZquiPSjdTOlIU77w2B0HbHOMOHD QmDZSuZDJWEVX5jA67ooFB4ZJ5qqcNoSj16AKnP4y877TpCBewpuclyfT1hH6HHce0E3 LMIO3z8n4//dUA59ajQmZQOzLzPfHHGUdmAM6H62vhEyyJFxZGPYBHXj8Sv+HnjLGZvs S8+6mgPrtmap/b1JqZJMtjcz6Bk1YnwqdgYJoywuxnL/iW49Sf0SSlWYc+S1imbI9g+2 HZRw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722010357; x=1722615157; 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=bygahLkMum3pwIQh8kLmkuKwRo3R1qnjWtlLvb6L1gA=; b=Ccrih+aHetIEFvMqF/HV9qVx3vp5k0bqr7YcqUSFEjnNlbJnDe4NOkqmhuW79isQ61 vL+6LIRZbHHPDmyZexx5JEwAPbzzbiBydchgB3rBuR5LlrQmZK33YMWIpGAV/Z/hOFFl R11z5EBsEopKFkZ9aXgefQycof1YCClv9VQQdsTeWfOrFz9XV80OPCTz4N+EVdwvUbOb kEgFsMUbODjfEQ1pyIj6Rj6Dgg4VhpgfNOB9Wc3Ps1PNf3xQjpeRdwP1pmsz/yU21Vlb AAMB2W9Yp/zafxt0VPxSLKsMdXc0env9SwlFtsvi/pNoCRAnoIzkvtnNpYGboRysYcyH pEwQ== X-Gm-Message-State: AOJu0YxGZjqX/7Gu0yLJZBRkFra3X7qg9dDNd+8t95yO1mT/sK6+XeSv NqF+BgHVbA5y4CkZFPbv4mhNOEZQU/rj2gcaxSKB00ujzgzSq3Zk2yH0Cw== X-Google-Smtp-Source: AGHT+IGR213Egl/bBq2iPZp42kn+wMDar7oRKpUuJB/xQeVwFDoPSUT4aNou4+j0R9KHgzEikGAzDQ== X-Received: by 2002:a05:6830:498f:b0:708:bea8:ae28 with SMTP id 46e09a7af769-70940c04164mr99767a34.2.1722010356992; Fri, 26 Jul 2024 09:12:36 -0700 (PDT) Received: from localhost.localdomain (syn-070-114-247-242.res.spectrum.com. [70.114.247.242]) by smtp.gmail.com with ESMTPSA id 46e09a7af769-70930718ec9sm792997a34.30.2024.07.26.09.12.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 26 Jul 2024 09:12:36 -0700 (PDT) From: Denis Kenzior To: ell@lists.linux.dev Cc: Denis Kenzior Subject: [PATCH 5/7] unit: Add tests for rtnl_message_from_{route|address} Date: Fri, 26 Jul 2024 11:12:17 -0500 Message-ID: <20240726161232.146982-5-denkenz@gmail.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240726161232.146982-1-denkenz@gmail.com> References: <20240726161232.146982-1-denkenz@gmail.com> Precedence: bulk X-Mailing-List: ell@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 --- unit/test-rtnl.c | 278 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 277 insertions(+), 1 deletion(-) diff --git a/unit/test-rtnl.c b/unit/test-rtnl.c index cd944af64de9..25e1859205b4 100644 --- a/unit/test-rtnl.c +++ b/unit/test-rtnl.c @@ -17,12 +17,282 @@ #include #include #include +#include #include #include +#include +#include +#include #include +#include +#include #include -#include "ell/dbus-private.h" +#include "ell/netlink-private.h" +#include "ell/rtnl-private.h" +#include "ell/useful.h" + +static size_t rta_add_u8(void *rta_buf, unsigned short type, uint8_t value) +{ + struct rtattr *rta = rta_buf; + + rta->rta_len = RTA_LENGTH(sizeof(uint8_t)); + rta->rta_type = type; + *((uint8_t *) RTA_DATA(rta)) = value; + + return RTA_SPACE(sizeof(uint8_t)); +} + +static size_t rta_add_u32(void *rta_buf, unsigned short type, uint32_t value) +{ + struct rtattr *rta = rta_buf; + + rta->rta_len = RTA_LENGTH(sizeof(uint32_t)); + rta->rta_type = type; + *((uint32_t *) RTA_DATA(rta)) = value; + + return RTA_SPACE(sizeof(uint32_t)); +} + +static size_t rta_add_data(void *rta_buf, unsigned short type, const void *data, + size_t data_len) +{ + struct rtattr *rta = rta_buf; + + rta->rta_len = RTA_LENGTH(data_len); + rta->rta_type = type; + memcpy(RTA_DATA(rta), data, data_len); + + return RTA_SPACE(data_len); +} + +static size_t rta_add_address(void *rta_buf, unsigned short type, + uint8_t family, + const struct in6_addr *v6, + const struct in_addr *v4) +{ + struct rtattr *rta = rta_buf; + + rta->rta_type = type; + + switch (family) { + case AF_INET6: + rta->rta_len = RTA_LENGTH(sizeof(struct in6_addr)); + memcpy(RTA_DATA(rta), v6, sizeof(struct in6_addr)); + return RTA_SPACE(sizeof(struct in6_addr)); + case AF_INET: + rta->rta_len = RTA_LENGTH(sizeof(struct in_addr)); + memcpy(RTA_DATA(rta), v4, sizeof(struct in_addr)); + return RTA_SPACE(sizeof(struct in_addr)); + } + + return 0; +} + +static int address_is_null(int family, const struct in_addr *v4, + const struct in6_addr *v6) +{ + switch (family) { + case AF_INET: + return v4->s_addr == 0; + case AF_INET6: + return IN6_IS_ADDR_UNSPECIFIED(v6); + } + + return -EAFNOSUPPORT; +} + +static bool messages_equal(const struct l_netlink_message *nlm, + void *other_message, size_t len) +{ + + if (len != nlm->hdr->nlmsg_len - NLMSG_HDRLEN) + return false; + + return !memcmp(nlm->data + NLMSG_HDRLEN, other_message, len); +} + +static const struct l_rtnl_route route = { + .family = AF_INET6, + .scope = RT_SCOPE_UNIVERSE, + .protocol = RTPROT_UNSPEC, + .gw = { .in6_addr = IN6ADDR_LOOPBACK_INIT }, + .dst = { .in6_addr = IN6ADDR_LOOPBACK_INIT }, + .dst_prefix_len = 32, + .prefsrc = { .in6_addr = IN6ADDR_LOOPBACK_INIT }, + .lifetime = INT_MAX, + .expiry_time = 0, /* math performed, will cause messages to differ */ + .mtu = 8096, + .priority = 300, + .preference = ICMPV6_ROUTER_PREF_LOW, +}; + +static struct rtmsg *build_rtmsg(const struct l_rtnl_route *rt, int ifindex, + size_t *out_len) +{ + struct rtmsg *rtmmsg; + size_t bufsize; + void *rta_buf; + uint64_t now = l_time_now(); + + bufsize = NLMSG_ALIGN(sizeof(struct rtmsg)) + + RTA_SPACE(sizeof(uint32_t)) + /* RTA_OIF */ + RTA_SPACE(sizeof(uint32_t)) + /* RTA_PRIORITY */ + RTA_SPACE(sizeof(struct in6_addr)) + /* RTA_GATEWAY */ + RTA_SPACE(sizeof(struct in6_addr)) + /* RTA_DST */ + RTA_SPACE(sizeof(struct in6_addr)) + /* RTA_PREFSRC */ + 256 + /* RTA_METRICS */ + RTA_SPACE(sizeof(uint8_t)) + /* RTA_PREF */ + RTA_SPACE(sizeof(uint32_t)); /* RTA_EXPIRES */ + + rtmmsg = l_malloc(bufsize); + memset(rtmmsg, 0, bufsize); + + rtmmsg->rtm_family = rt->family; + rtmmsg->rtm_table = RT_TABLE_MAIN; + rtmmsg->rtm_protocol = rt->protocol; + rtmmsg->rtm_type = RTN_UNICAST; + rtmmsg->rtm_scope = rt->scope; + + rta_buf = (void *) rtmmsg + NLMSG_ALIGN(sizeof(struct rtmsg)); + rta_buf += rta_add_u32(rta_buf, RTA_OIF, ifindex); + + if (rt->priority) + rta_buf += rta_add_u32(rta_buf, RTA_PRIORITY, + rt->priority + ifindex); + + if (!address_is_null(rt->family, &rt->gw.in_addr, &rt->gw.in6_addr)) + rta_buf += rta_add_address(rta_buf, RTA_GATEWAY, rt->family, + &rt->gw.in6_addr, &rt->gw.in_addr); + + if (rt->dst_prefix_len) { + rtmmsg->rtm_dst_len = rt->dst_prefix_len; + rta_buf += rta_add_address(rta_buf, RTA_DST, rt->family, + &rt->dst.in6_addr, &rt->dst.in_addr); + } + + if (!address_is_null(rt->family, &rt->prefsrc.in_addr, + &rt->prefsrc.in6_addr)) + rta_buf += rta_add_address(rta_buf, RTA_PREFSRC, rt->family, + &rt->prefsrc.in6_addr, + &rt->prefsrc.in_addr); + + if (rt->mtu) { + uint8_t buf[256]; + size_t written = rta_add_u32(buf, RTAX_MTU, rt->mtu); + + rta_buf += rta_add_data(rta_buf, RTA_METRICS | NLA_F_NESTED, + buf, written); + } + + if (rt->preference) + rta_buf += rta_add_u8(rta_buf, RTA_PREF, rt->preference); + + if (rt->expiry_time > now) + rta_buf += rta_add_u32(rta_buf, RTA_EXPIRES, + l_time_to_secs(rt->expiry_time - now)); + + *out_len = rta_buf - (void *) rtmmsg; + return rtmmsg; +} + +static void test_route(const void *data) +{ + static const int ifindex = 3; + size_t rtmsg_len; + _auto_(l_free) void *rtmsg = build_rtmsg(&route, ifindex, &rtmsg_len); + struct l_netlink_message *nlm = + rtnl_message_from_route(RTM_NEWROUTE, + NLM_F_CREATE | NLM_F_REPLACE, + ifindex, &route); + + assert(messages_equal(nlm, rtmsg, rtmsg_len)); + l_netlink_message_unref(nlm); +} + +static const struct l_rtnl_address address = { + .family = AF_INET6, + .prefix_len = 31, + .scope = RT_SCOPE_UNIVERSE, + .in6_addr = IN6ADDR_LOOPBACK_INIT, + .label = { 'f', 'o', 'o', 'b', 'a', 'r', '\0' }, + .flags = IFA_F_PERMANENT | IFA_F_NOPREFIXROUTE, +}; + +static struct ifaddrmsg *build_ifaddrmsg(const struct l_rtnl_address *addr, + int ifindex, size_t *out_len) +{ + struct ifaddrmsg *ifamsg; + void *buf; + size_t bufsize; + uint64_t now = l_time_now(); + + bufsize = NLMSG_ALIGN(sizeof(struct ifaddrmsg)) + + RTA_SPACE(sizeof(struct in6_addr)) + + RTA_SPACE(sizeof(struct in_addr)) + + RTA_SPACE(sizeof(uint32_t)) + + RTA_SPACE(IFNAMSIZ) + + RTA_SPACE(sizeof(struct ifa_cacheinfo)); + + ifamsg = l_malloc(bufsize); + memset(ifamsg, 0, bufsize); + + ifamsg->ifa_index = ifindex; + ifamsg->ifa_family = addr->family; + ifamsg->ifa_scope = addr->scope; + ifamsg->ifa_prefixlen = addr->prefix_len; + /* Kernel ignores legacy flags in IFA_FLAGS, so set them here */ + ifamsg->ifa_flags = addr->flags & 0xff; + + buf = (void *) ifamsg + NLMSG_ALIGN(sizeof(struct ifaddrmsg)); + + if (addr->family == AF_INET) { + buf += rta_add_data(buf, IFA_LOCAL, &addr->in_addr, + sizeof(struct in_addr)); + buf += rta_add_data(buf, IFA_BROADCAST, &addr->broadcast, + sizeof(struct in_addr)); + } else + buf += rta_add_data(buf, IFA_LOCAL, &addr->in6_addr, + sizeof(struct in6_addr)); + + if (addr->flags & 0xffffff00) + buf += rta_add_u32(buf, IFA_FLAGS, addr->flags & 0xffffff00); + + if (addr->label[0]) + buf += rta_add_data(buf, IFA_LABEL, + addr->label, strlen(addr->label) + 1); + + if (addr->preferred_expiry_time > now || + addr->valid_expiry_time > now) { + struct ifa_cacheinfo cinfo; + + memset(&cinfo, 0, sizeof(cinfo)); + cinfo.ifa_prefered = addr->preferred_expiry_time > now ? + l_time_to_secs(addr->preferred_expiry_time - now) : 0; + cinfo.ifa_valid = addr->valid_expiry_time > now ? + l_time_to_secs(addr->valid_expiry_time - now) : 0; + + buf += rta_add_data(buf, IFA_CACHEINFO, &cinfo, sizeof(cinfo)); + } + + *out_len = buf - (void *) ifamsg; + return ifamsg; +} + +static void test_address(const void *data) +{ + static const int ifindex = 3; + size_t ifamsg_len; + _auto_(l_free) void *ifamsg = + build_ifaddrmsg(&address, ifindex, &ifamsg_len); + struct l_netlink_message *nlm = + rtnl_message_from_address(RTM_NEWADDR, + NLM_F_CREATE | NLM_F_REPLACE, + ifindex, &address); + + assert(messages_equal(nlm, ifamsg, ifamsg_len)); + l_netlink_message_unref(nlm); +} static void signal_handler(uint32_t signo, void *user_data) { @@ -234,6 +504,12 @@ int main(int argc, char *argv[]) if (!l_main_init()) return -1; + /* Run the the tests not requiring the main event loop first */ + l_test_init(&argc, &argv); + l_test_add("route", test_route, NULL); + l_test_add("address", test_address, NULL); + l_test_run(); + test_add("Dump IPv4 routing table", test_route4_dump, NULL); test_add("Dump IPv6 routing table", test_route6_dump, NULL); test_add("Dump IPv4 addresses", test_ifaddr4_dump, NULL); From patchwork Fri Jul 26 16:12:18 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Denis Kenzior X-Patchwork-Id: 13742981 Received: from mail-ot1-f41.google.com (mail-ot1-f41.google.com [209.85.210.41]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9607D17BB3B for ; Fri, 26 Jul 2024 16:12:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.41 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722010360; cv=none; b=dS5c/e7B81F8IwJJWc3NxjcfqFoMzWYJ00UTNf43YJbxAP53bkvVZugDb7CBs0CDO2dnCNU4mbI1wy4f3EAIYvBJqiq8UjFRBQBZqcrCZwnnB10yty0sQFn+mplkYxs5SwM+61vLXYes+xBoUqx/TGf1wKR+jK+tHfREzVAVmls= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722010360; c=relaxed/simple; bh=d6I6HLsIb1hMKfhNqL01nz/LfcHbLkdgDk9SbOo3U1I=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Pf0LxgB+1qrbeDYzWX6e6Aa07oiSR9kv7ZJbIY1VU94yjxp1UDUczEZFiqOqmHMBKUSwnAnm+Mo9ZcdymfdyJ7aenvjO4I9AbtfRH5i2/INTla2urY33EB30tAPx5DsjdXGRorL+0XDiF7dUoYDD/NUXHvWN9pGvv1K/yE46zvo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=dEyCx1i5; arc=none smtp.client-ip=209.85.210.41 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="dEyCx1i5" Received: by mail-ot1-f41.google.com with SMTP id 46e09a7af769-7093705c708so1101705a34.1 for ; Fri, 26 Jul 2024 09:12:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1722010357; x=1722615157; darn=lists.linux.dev; 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=tfn2YMD8l/x6G2rpYxzrQt/bBERQh2kasx22+0W/fu0=; b=dEyCx1i5KIDXI81L9NY15zKCLOZ+Y/M4Qbvzs71Yhpkb6aCVUt0EaDplJJkMjyZFU4 VUJmdOEcOb84CvP6/gVjOS4yaPfNMNlnwX1JFKUxVuNvARhlyWUWGfFhR2w2lnlMTQac EpJEfJxTkXd0O2hh/tozR5IulH+EObnXGY0B3uPP8lc63/7eJhWg5L02MxH8LaTvvQT4 /j04Up8aptJW3lYGcdV5gE+Lr89eUM23ruF6FHVtTit0yiEIF9P7m9sQLArNRgUwpvIf aZoBdYxr2FAxRiC2vVceZAflhGikaVRRI7/8axkJ0OgdU8fvhLOq2uRvcS0o0rY4wV3q ZHgg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722010357; x=1722615157; 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=tfn2YMD8l/x6G2rpYxzrQt/bBERQh2kasx22+0W/fu0=; b=NE9ZvWp8PUGIeqEPQ8ELDiyajOBVLmgMnu1rHEeJWWDAojzizjEONcgoPGIZiZJYAL aEkSSPiB/3zfOEJh3zNlHFU+xGnMqLjeecPlyvbfvDVekMireUa8Cuhc42GJIdCG478q SwvsB41el8/wfYMJaLuqzKsfnacL89MXVtiyG2tD4VMvPlU7MIoiiQloOgvoq4/L5XHR 5OCOGKFHUfz8eo0na5bntJdV7Xbn/Qj7FcYceuC7EwbRiUyPa3dsSwXY002Z0m6meyeH mC/wjAJyzsOS53ofl6laTu+2rw19KRvcy9LtJH+uLABWqiY9Tl+iDClitS71DzgGgmu4 qXlQ== X-Gm-Message-State: AOJu0YzZB+/KkQZRl9PyOwsK6BC/n3a7GVbQi3308W7F5nDbBYvS2236 QsAlzzFbVP4qZvvOZsSZZrFtWS+WV2xym2Yjdn0ZpO3iJW4vPVM5ET7B2A== X-Google-Smtp-Source: AGHT+IHuJrwLm3/0N13NUUsR4zwYRK5dIOY45LdwujlcKLhJbKaykZXewypyjpZCBGCC9PPT7S9tgA== X-Received: by 2002:a05:6830:6b05:b0:703:6847:3ab8 with SMTP id 46e09a7af769-70940c5e151mr78975a34.25.1722010357643; Fri, 26 Jul 2024 09:12:37 -0700 (PDT) Received: from localhost.localdomain (syn-070-114-247-242.res.spectrum.com. [70.114.247.242]) by smtp.gmail.com with ESMTPSA id 46e09a7af769-70930718ec9sm792997a34.30.2024.07.26.09.12.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 26 Jul 2024 09:12:37 -0700 (PDT) From: Denis Kenzior To: ell@lists.linux.dev Cc: Denis Kenzior Subject: [PATCH 6/7] netlink: Add sealed flag to l_netlink_message Date: Fri, 26 Jul 2024 11:12:18 -0500 Message-ID: <20240726161232.146982-6-denkenz@gmail.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240726161232.146982-1-denkenz@gmail.com> References: <20240726161232.146982-1-denkenz@gmail.com> Precedence: bulk X-Mailing-List: ell@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 If the sealed flag is set, the message can no longer be modified. This is useful when netlink message ownership is taken over by l_netlink_send to prevent the message from being modified once it has been queued. --- ell/netlink-private.h | 1 + ell/netlink.c | 3 +++ 2 files changed, 4 insertions(+) diff --git a/ell/netlink-private.h b/ell/netlink-private.h index b114f62f6bf3..f7119f6218e9 100644 --- a/ell/netlink-private.h +++ b/ell/netlink-private.h @@ -40,6 +40,7 @@ struct l_netlink_message { }; uint32_t nest_offset[4]; uint8_t nest_level; + bool sealed : 1; }; bool netlink_parse_ext_ack_error(const struct nlmsghdr *nlmsg, diff --git a/ell/netlink.c b/ell/netlink.c index 58dfd9779875..b6d5fb1fd36d 100644 --- a/ell/netlink.c +++ b/ell/netlink.c @@ -685,6 +685,9 @@ static int message_grow(struct l_netlink_message *message, uint32_t needed) { uint32_t grow_to; + if (message->sealed) + return -EPERM; + if (message->size - message->hdr->nlmsg_len >= needed) return 0; From patchwork Fri Jul 26 16:12:19 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Denis Kenzior X-Patchwork-Id: 13742983 Received: from mail-ot1-f51.google.com (mail-ot1-f51.google.com [209.85.210.51]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id AF93317BB1F for ; Fri, 26 Jul 2024 16:12:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.51 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722010362; cv=none; b=NLmb66C0dbOZl6Iz97IxMTcF7uyjga5HfgnfvgXPn0PoVeTw9vHwfwxjSPYoevlA+1QCH3qFZW4ih9ygw7+YUagdPlJJLT4t0V/kLOVY6aOEC4Y5ffViYZwpK8U3ntaV2F34opfmxsqVHV0wUs+73O8gslGVO+RkFKHeDytP+0g= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722010362; c=relaxed/simple; bh=vV2D3zIUFUONy0Gv4sXLvQ4y4JORKB98h4aWQneNznM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=NreYRaE8IGL9XsAJxIB+3o6bPLp5HX0wnQuOZ8RwfSlxW1PkkGcFQPy60pKDznUjvBZ/Gx9dgXlD/lORFwZo/OrNfP9GY4pKP5niN+p1LxM3lk9X9ZZNVrRtwWYcWDzFrJICawTdqUBgNT3teKrtlx/IV65p9XBpJZ4Sc43kC/0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=Djh7A3ie; arc=none smtp.client-ip=209.85.210.51 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="Djh7A3ie" Received: by mail-ot1-f51.google.com with SMTP id 46e09a7af769-7092fb4317dso1041065a34.0 for ; Fri, 26 Jul 2024 09:12:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1722010359; x=1722615159; darn=lists.linux.dev; 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=UaK9tAB1Mc5Cdl6rqfGmVfeOhgn4sWis+4to48bL/Dk=; b=Djh7A3ieZpYvtM+Fr+xDypha55DissDvsAhk/Lv30BJB3KdY5XJlOJnwz5m+WAgzbf E5BxGWftmPfrA6umuGC6Ko4dEGeWr+aMwieD80BtYxwF0DyzZN4Ym03hzZx2Yv+NcUx3 6qbpO6WGGV5LMxrLLT6NZfTwfB89wdG55SnN2rJE6lQUdBL4RzTwPGP5WKud6W+czOzb wJJtACxhUR5WuHBJyURYXSXOxu0UsXTEmhqvZen8nxf+26miY+8CdkFl+byf2Wq4ujTi s9QOduLWjqGBSWOZzc9UByTJ+ZYQLoVSf+13lpXQ4YW4pcfLbyAny5JvVuaCqzgNo/Ie 8N6Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722010359; x=1722615159; 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=UaK9tAB1Mc5Cdl6rqfGmVfeOhgn4sWis+4to48bL/Dk=; b=lVrOc7/rkGg/OWllg/0/aRCkuxv0BUqH5VBjU8ZXA4U8a5UKp+nPV9lLnXMgszswtK 8rhQDo7xW7LDj3OFivKfsLc3bhgD4xKdLtui8QsVpfrln8Br1lB2VvgcywHrRd8aXGTV sxGDiXhFWPi+IsehT08yejJkoscRcBxueaEuSy49iZzrYY2BWFgtPkkNg+4Edzxv2xis 1onoKTu5R11S35rVSxNNJLSHtzNQBKNRKwAzES/+1I3mSsgvH/c1CZSueWJpkXBQefkR 4E8HgalAkXtrhAObP8uQtN2sGZog11420v3Mxwv7Aj6BG9/FlutP+fiO3rNShOL4KQqA 5Wow== X-Gm-Message-State: AOJu0YxDaPqkAUnAXc7IAtAu/xIQfqhSxfgAXa4MUbgfVbppcX0zZn0o X0AFehxvxNj80RPUz7FM165t3DX3WnMbJjzaXjurtkVm7ovifAJwT5zHTw== X-Google-Smtp-Source: AGHT+IFmSKjW1yOIoWfU5l1VP6bWSYy9km8Qd9BoCVycqqK8dpVoMIqwydxiC4jalz2sTy+J94jNKg== X-Received: by 2002:a9d:708f:0:b0:703:69b5:1518 with SMTP id 46e09a7af769-70940c14c11mr66385a34.16.1722010358453; Fri, 26 Jul 2024 09:12:38 -0700 (PDT) Received: from localhost.localdomain (syn-070-114-247-242.res.spectrum.com. [70.114.247.242]) by smtp.gmail.com with ESMTPSA id 46e09a7af769-70930718ec9sm792997a34.30.2024.07.26.09.12.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 26 Jul 2024 09:12:38 -0700 (PDT) From: Denis Kenzior To: ell@lists.linux.dev Cc: Denis Kenzior Subject: [PATCH 7/7] netlink/rtnl: Change l_netlink_send API Date: Fri, 26 Jul 2024 11:12:19 -0500 Message-ID: <20240726161232.146982-7-denkenz@gmail.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240726161232.146982-1-denkenz@gmail.com> References: <20240726161232.146982-1-denkenz@gmail.com> Precedence: bulk X-Mailing-List: ell@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Change l_netlink_send API to operate on newly introduced l_netlink_message objects instead of arbitrary void * buffers. This cuts down the number of arguments to l_netlink_send considerably, making it much easier to use. The change in public API does affect the test-netlink unit test. Fix it up to use the new API as well. --- ell/netlink.c | 80 ++++---- ell/netlink.h | 8 +- ell/rtnl.c | 453 +++++++++++--------------------------------- unit/test-netlink.c | 11 +- 4 files changed, 151 insertions(+), 401 deletions(-) diff --git a/ell/netlink.c b/ell/netlink.c index b6d5fb1fd36d..121a322fccf5 100644 --- a/ell/netlink.c +++ b/ell/netlink.c @@ -24,11 +24,10 @@ struct command { unsigned int id; - uint32_t seq; - uint32_t len; l_netlink_command_func_t handler; l_netlink_destroy_func_t destroy; void *user_data; + struct l_netlink_message *message; }; struct notify { @@ -61,6 +60,7 @@ static void destroy_command(void *data) if (command->destroy) command->destroy(command->user_data); + l_netlink_message_unref(command->message); l_free(command); } @@ -85,8 +85,8 @@ static bool can_write_data(struct l_io *io, void *user_data) { struct l_netlink *netlink = user_data; struct command *command; + struct nlmsghdr *hdr; struct sockaddr_nl addr; - const void *data; ssize_t written; int sk; @@ -94,28 +94,27 @@ static bool can_write_data(struct l_io *io, void *user_data) if (!command) return false; + hdr = command->message->hdr; sk = l_io_get_fd(io); memset(&addr, 0, sizeof(addr)); addr.nl_family = AF_NETLINK; addr.nl_pid = 0; - data = ((void *) command) + NLMSG_ALIGN(sizeof(struct command)); - - written = sendto(sk, data, command->len, 0, - (struct sockaddr *) &addr, sizeof(addr)); - if (written < 0 || (uint32_t) written != command->len) { + written = sendto(sk, hdr, hdr->nlmsg_len, 0, + (struct sockaddr *) &addr, sizeof(addr)); + if (written < 0 || (uint32_t) written != hdr->nlmsg_len) { l_hashmap_remove(netlink->command_lookup, L_UINT_TO_PTR(command->id)); destroy_command(command); return true; } - l_util_hexdump(false, data, command->len, + l_util_hexdump(false, hdr, hdr->nlmsg_len, netlink->debug_handler, netlink->debug_data); l_hashmap_insert(netlink->command_pending, - L_UINT_TO_PTR(command->seq), command); + L_UINT_TO_PTR(hdr->nlmsg_seq), command); return l_queue_length(netlink->command_queue) > 0; } @@ -399,70 +398,55 @@ LIB_EXPORT void l_netlink_destroy(struct l_netlink *netlink) } LIB_EXPORT unsigned int l_netlink_send(struct l_netlink *netlink, - uint16_t type, uint16_t flags, const void *data, - uint32_t len, l_netlink_command_func_t function, - void *user_data, l_netlink_destroy_func_t destroy) + struct l_netlink_message *message, + l_netlink_command_func_t function, + void *user_data, + l_netlink_destroy_func_t destroy) { struct command *command; + uint16_t extra_flags = NLM_F_REQUEST; struct nlmsghdr *nlmsg; - size_t size; if (unlikely(!netlink)) return 0; - if (flags & 0xff) + if (unlikely(message->nest_level)) return 0; if (function) - flags |= NLM_F_ACK; + extra_flags |= NLM_F_ACK; - size = NLMSG_ALIGN(sizeof(struct command)) + - NLMSG_HDRLEN + NLMSG_ALIGN(len); + command = l_new(struct command, 1); - command = l_malloc(size); + if (!l_hashmap_insert(netlink->command_lookup, + L_UINT_TO_PTR(netlink->next_command_id), + command)) { + l_free(command); + return 0; + } - memset(command, 0, size); command->handler = function; command->destroy = destroy; command->user_data = user_data; + command->id = netlink->next_command_id++; + command->message = message; + message->sealed = true; - command->id = netlink->next_command_id; - - if (!l_hashmap_insert(netlink->command_lookup, - L_UINT_TO_PTR(command->id), command)) - goto free_command; - - command->seq = netlink->next_seq++; - command->len = NLMSG_HDRLEN + NLMSG_ALIGN(len); - - nlmsg = ((void *) command) + NLMSG_ALIGN(sizeof(struct command)); - - nlmsg->nlmsg_len = command->len; - nlmsg->nlmsg_type = type; - nlmsg->nlmsg_flags = NLM_F_REQUEST | flags; - nlmsg->nlmsg_seq = command->seq; + nlmsg = message->hdr; + nlmsg->nlmsg_flags |= extra_flags; + nlmsg->nlmsg_seq = netlink->next_seq++; nlmsg->nlmsg_pid = netlink->pid; - if (data && len > 0) - memcpy(((void *) nlmsg) + NLMSG_HDRLEN, data, len); - l_queue_push_tail(netlink->command_queue, command); - l_io_set_write_handler(netlink->io, can_write_data, netlink, NULL); - netlink->next_command_id++; - return command->id; - -free_command: - l_free(command); - - return 0; } LIB_EXPORT bool l_netlink_cancel(struct l_netlink *netlink, unsigned int id) { struct command *command; + struct nlmsghdr *hdr; if (unlikely(!netlink || !id)) return false; @@ -471,9 +455,11 @@ LIB_EXPORT bool l_netlink_cancel(struct l_netlink *netlink, unsigned int id) if (!command) return false; + hdr = command->message->hdr; + if (!l_queue_remove(netlink->command_queue, command)) { l_hashmap_remove(netlink->command_pending, - L_UINT_TO_PTR(command->seq)); + L_UINT_TO_PTR(hdr->nlmsg_seq)); } destroy_command(command); diff --git a/ell/netlink.h b/ell/netlink.h index e7962b9932dc..08aff74fbac2 100644 --- a/ell/netlink.h +++ b/ell/netlink.h @@ -25,14 +25,16 @@ typedef void (*l_netlink_notify_func_t) (uint16_t type, const void *data, typedef void (*l_netlink_destroy_func_t) (void *user_data); struct l_netlink; +struct l_netlink_message; struct l_netlink *l_netlink_new(int protocol); void l_netlink_destroy(struct l_netlink *netlink); unsigned int l_netlink_send(struct l_netlink *netlink, - uint16_t type, uint16_t flags, const void *data, - uint32_t len, l_netlink_command_func_t function, - void *user_data, l_netlink_destroy_func_t destroy); + struct l_netlink_message *message, + l_netlink_command_func_t function, + void *user_data, + l_netlink_destroy_func_t destroy); bool l_netlink_cancel(struct l_netlink *netlink, unsigned int id); unsigned int l_netlink_register(struct l_netlink *netlink, diff --git a/ell/rtnl.c b/ell/rtnl.c index 90ff61c09d13..da5d219feb48 100644 --- a/ell/rtnl.c +++ b/ell/rtnl.c @@ -641,63 +641,6 @@ LIB_EXPORT bool l_rtnl_route_set_scope(struct l_rtnl_route *rt, uint8_t scope) return true; } -static size_t rta_add_u8(void *rta_buf, unsigned short type, uint8_t value) -{ - struct rtattr *rta = rta_buf; - - rta->rta_len = RTA_LENGTH(sizeof(uint8_t)); - rta->rta_type = type; - *((uint8_t *) RTA_DATA(rta)) = value; - - return RTA_SPACE(sizeof(uint8_t)); -} - -static size_t rta_add_u32(void *rta_buf, unsigned short type, uint32_t value) -{ - struct rtattr *rta = rta_buf; - - rta->rta_len = RTA_LENGTH(sizeof(uint32_t)); - rta->rta_type = type; - *((uint32_t *) RTA_DATA(rta)) = value; - - return RTA_SPACE(sizeof(uint32_t)); -} - -static size_t rta_add_data(void *rta_buf, unsigned short type, const void *data, - size_t data_len) -{ - struct rtattr *rta = rta_buf; - - rta->rta_len = RTA_LENGTH(data_len); - rta->rta_type = type; - memcpy(RTA_DATA(rta), data, data_len); - - return RTA_SPACE(data_len); -} - -static size_t rta_add_address(void *rta_buf, unsigned short type, - uint8_t family, - const struct in6_addr *v6, - const struct in_addr *v4) -{ - struct rtattr *rta = rta_buf; - - rta->rta_type = type; - - switch (family) { - case AF_INET6: - rta->rta_len = RTA_LENGTH(sizeof(struct in6_addr)); - memcpy(RTA_DATA(rta), v6, sizeof(struct in6_addr)); - return RTA_SPACE(sizeof(struct in6_addr)); - case AF_INET: - rta->rta_len = RTA_LENGTH(sizeof(struct in_addr)); - memcpy(RTA_DATA(rta), v4, sizeof(struct in_addr)); - return RTA_SPACE(sizeof(struct in_addr)); - } - - return 0; -} - static int append_address(struct l_netlink_message *nlm, uint16_t type, uint8_t family, const struct in6_addr *v6, @@ -786,31 +729,18 @@ LIB_EXPORT uint32_t l_rtnl_set_linkmode_and_operstate(struct l_netlink *rtnl, void *user_data, l_netlink_destroy_func_t destroy) { - struct ifinfomsg *rtmmsg; - void *rta_buf; - size_t bufsize; - uint32_t id; - - bufsize = NLMSG_ALIGN(sizeof(struct ifinfomsg)) + - RTA_SPACE(sizeof(uint8_t)) + RTA_SPACE(sizeof(uint8_t)); - - rtmmsg = l_malloc(bufsize); - memset(rtmmsg, 0, bufsize); - - rtmmsg->ifi_family = AF_UNSPEC; - rtmmsg->ifi_index = ifindex; + struct l_netlink_message *nlm = l_netlink_message_new(RTM_SETLINK, 0); + struct ifinfomsg ifi; - rta_buf = (void *) rtmmsg + NLMSG_ALIGN(sizeof(struct ifinfomsg)); + memset(&ifi, 0, sizeof(ifi)); + ifi.ifi_family = AF_UNSPEC; + ifi.ifi_index = ifindex; - rta_buf += rta_add_u8(rta_buf, IFLA_LINKMODE, linkmode); - rta_buf += rta_add_u8(rta_buf, IFLA_OPERSTATE, operstate); + l_netlink_message_add_header(nlm, &ifi, sizeof(ifi)); + l_netlink_message_append_u8(nlm, IFLA_LINKMODE, linkmode); + l_netlink_message_append_u8(nlm, IFLA_OPERSTATE, operstate); - id = l_netlink_send(rtnl, RTM_SETLINK, 0, rtmmsg, - rta_buf - (void *) rtmmsg, - cb, user_data, destroy); - l_free(rtmmsg); - - return id; + return l_netlink_send(rtnl, nlm, cb, user_data, destroy); } LIB_EXPORT uint32_t l_rtnl_set_mac(struct l_netlink *rtnl, int ifindex, @@ -820,34 +750,22 @@ LIB_EXPORT uint32_t l_rtnl_set_mac(struct l_netlink *rtnl, int ifindex, void *user_data, l_netlink_destroy_func_t destroy) { - struct ifinfomsg *rtmmsg; - void *rta_buf; - size_t bufsize; - uint32_t id; - - bufsize = NLMSG_ALIGN(sizeof(struct ifinfomsg)) + RTA_SPACE(6); + struct l_netlink_message *nlm = l_netlink_message_new(RTM_SETLINK, 0); + struct ifinfomsg ifi; - rtmmsg = l_malloc(bufsize); - memset(rtmmsg, 0, bufsize); - - rtmmsg->ifi_family = AF_UNSPEC; - rtmmsg->ifi_index = ifindex; + memset(&ifi, 0, sizeof(ifi)); + ifi.ifi_family = AF_UNSPEC; + ifi.ifi_index = ifindex; if (power_up) { - rtmmsg->ifi_change = IFF_UP; - rtmmsg->ifi_flags = IFF_UP; + ifi.ifi_change = IFF_UP; + ifi.ifi_flags = IFF_UP; } - rta_buf = (void *) rtmmsg + NLMSG_ALIGN(sizeof(struct ifinfomsg)); - - rta_buf += rta_add_data(rta_buf, IFLA_ADDRESS, (void *) addr, 6); + l_netlink_message_add_header(nlm, &ifi, sizeof(ifi)); + l_netlink_message_append_mac(nlm, IFLA_ADDRESS, addr); - id = l_netlink_send(rtnl, RTM_SETLINK, 0, rtmmsg, - rta_buf - (void *) rtmmsg, - cb, user_data, destroy); - l_free(rtmmsg); - - return id; + return l_netlink_send(rtnl, nlm, cb, user_data, destroy); } LIB_EXPORT uint32_t l_rtnl_set_powered(struct l_netlink *rtnl, int ifindex, @@ -855,25 +773,18 @@ LIB_EXPORT uint32_t l_rtnl_set_powered(struct l_netlink *rtnl, int ifindex, l_netlink_command_func_t cb, void *user_data, l_netlink_destroy_func_t destroy) { - struct ifinfomsg *rtmmsg; - size_t bufsize; - uint32_t id; - - bufsize = NLMSG_ALIGN(sizeof(struct ifinfomsg)); - - rtmmsg = l_malloc(bufsize); - memset(rtmmsg, 0, bufsize); + struct l_netlink_message *nlm = l_netlink_message_new(RTM_SETLINK, 0); + struct ifinfomsg ifi; - rtmmsg->ifi_family = AF_UNSPEC; - rtmmsg->ifi_index = ifindex; - rtmmsg->ifi_change = IFF_UP; - rtmmsg->ifi_flags = powered ? IFF_UP : 0; + memset(&ifi, 0, sizeof(ifi)); + ifi.ifi_family = AF_UNSPEC; + ifi.ifi_index = ifindex; + ifi.ifi_change = IFF_UP; + ifi.ifi_flags = powered ? IFF_UP : 0; - id = l_netlink_send(rtnl, RTM_SETLINK, 0, rtmmsg, bufsize, - cb, user_data, destroy); - l_free(rtmmsg); + l_netlink_message_add_header(nlm, &ifi, sizeof(ifi)); - return id; + return l_netlink_send(rtnl, nlm, cb, user_data, destroy); } LIB_EXPORT void l_rtnl_ifaddr4_extract(const struct ifaddrmsg *ifa, int bytes, @@ -919,21 +830,17 @@ LIB_EXPORT uint32_t l_rtnl_ifaddr4_dump(struct l_netlink *rtnl, void *user_data, l_netlink_destroy_func_t destroy) { - struct ifaddrmsg *rtmmsg; - uint32_t id; - - rtmmsg = l_malloc(sizeof(struct ifaddrmsg)); - memset(rtmmsg, 0, sizeof(struct ifaddrmsg)); - - rtmmsg->ifa_family = AF_INET; + struct ifaddrmsg ifa; + struct l_netlink_message *nlm = + l_netlink_message_new_sized(RTM_GETADDR, + NLM_F_DUMP, sizeof(ifa)); - id = l_netlink_send(rtnl, RTM_GETADDR, NLM_F_DUMP, rtmmsg, - sizeof(struct ifaddrmsg), cb, user_data, - destroy); + memset(&ifa, 0, sizeof(ifa)); + ifa.ifa_family = AF_INET; - l_free(rtmmsg); + l_netlink_message_add_header(nlm, &ifa, sizeof(ifa)); - return id; + return l_netlink_send(rtnl, nlm, cb, user_data, destroy); } LIB_EXPORT uint32_t l_rtnl_ifaddr4_add(struct l_netlink *rtnl, int ifindex, @@ -984,14 +891,17 @@ LIB_EXPORT uint32_t l_rtnl_route4_dump(struct l_netlink *rtnl, l_netlink_command_func_t cb, void *user_data, l_netlink_destroy_func_t destroy) { - struct rtmsg rtmsg; + struct rtmsg rtm; + struct l_netlink_message *nlm = + l_netlink_message_new_sized(RTM_GETROUTE, + NLM_F_DUMP, sizeof(rtm)); - memset(&rtmsg, 0, sizeof(struct rtmsg)); - rtmsg.rtm_family = AF_INET; + memset(&rtm, 0, sizeof(rtm)); + rtm.rtm_family = AF_INET; - return l_netlink_send(rtnl, RTM_GETROUTE, NLM_F_DUMP, &rtmsg, - sizeof(struct rtmsg), cb, user_data, - destroy); + l_netlink_message_add_header(nlm, &rtm, sizeof(rtm)); + + return l_netlink_send(rtnl, nlm, cb, user_data, destroy); } LIB_EXPORT uint32_t l_rtnl_route4_add_connected(struct l_netlink *rtnl, @@ -1075,21 +985,17 @@ LIB_EXPORT uint32_t l_rtnl_ifaddr6_dump(struct l_netlink *rtnl, l_netlink_command_func_t cb, void *user_data, l_netlink_destroy_func_t destroy) { - struct ifaddrmsg *rtmmsg; - uint32_t id; - - rtmmsg = l_malloc(sizeof(struct ifaddrmsg)); - memset(rtmmsg, 0, sizeof(struct ifaddrmsg)); - - rtmmsg->ifa_family = AF_INET6; + struct ifaddrmsg ifa; + struct l_netlink_message *nlm = + l_netlink_message_new_sized(RTM_GETADDR, + NLM_F_DUMP, sizeof(ifa)); - id = l_netlink_send(rtnl, RTM_GETADDR, NLM_F_DUMP, rtmmsg, - sizeof(struct ifaddrmsg), cb, user_data, - destroy); + memset(&ifa, 0, sizeof(ifa)); + ifa.ifa_family = AF_INET6; - l_free(rtmmsg); + l_netlink_message_add_header(nlm, &ifa, sizeof(ifa)); - return id; + return l_netlink_send(rtnl, nlm, cb, user_data, destroy); } LIB_EXPORT uint32_t l_rtnl_ifaddr6_add(struct l_netlink *rtnl, int ifindex, @@ -1131,14 +1037,17 @@ LIB_EXPORT uint32_t l_rtnl_route6_dump(struct l_netlink *rtnl, l_netlink_command_func_t cb, void *user_data, l_netlink_destroy_func_t destroy) { - struct rtmsg rtmsg; + struct rtmsg rtm; + struct l_netlink_message *nlm = + l_netlink_message_new_sized(RTM_GETROUTE, + NLM_F_DUMP, sizeof(rtm)); - memset(&rtmsg, 0, sizeof(struct rtmsg)); - rtmsg.rtm_family = AF_INET6; + memset(&rtm, 0, sizeof(rtm)); + rtm.rtm_family = AF_INET6; - return l_netlink_send(rtnl, RTM_GETROUTE, NLM_F_DUMP, &rtmsg, - sizeof(struct rtmsg), cb, user_data, - destroy); + l_netlink_message_add_header(nlm, &rtm, sizeof(rtm)); + + return l_netlink_send(rtnl, nlm, cb, user_data, destroy); } LIB_EXPORT uint32_t l_rtnl_route6_add_gateway(struct l_netlink *rtnl, @@ -1187,84 +1096,6 @@ LIB_EXPORT uint32_t l_rtnl_route6_delete_gateway(struct l_netlink *rtnl, return r; } -static uint32_t _rtnl_ifaddr_change(struct l_netlink *rtnl, uint16_t nlmsg_type, - int ifindex, - const struct l_rtnl_address *addr, - l_netlink_command_func_t cb, - void *user_data, - l_netlink_destroy_func_t destroy) -{ - struct ifaddrmsg *ifamsg; - void *buf; - size_t bufsize; - uint32_t id; - int flags = 0; - uint64_t now = l_time_now(); - - if (nlmsg_type == RTM_NEWADDR) - flags = NLM_F_CREATE | NLM_F_REPLACE; - - bufsize = NLMSG_ALIGN(sizeof(struct ifaddrmsg)) + - RTA_SPACE(sizeof(struct in6_addr)) + - RTA_SPACE(sizeof(struct in_addr)) + - RTA_SPACE(sizeof(uint32_t)) + - RTA_SPACE(IFNAMSIZ) + - RTA_SPACE(sizeof(struct ifa_cacheinfo)); - - ifamsg = l_malloc(bufsize); - memset(ifamsg, 0, bufsize); - - ifamsg->ifa_index = ifindex; - ifamsg->ifa_family = addr->family; - ifamsg->ifa_scope = addr->scope; - ifamsg->ifa_prefixlen = addr->prefix_len; - /* Kernel ignores legacy flags in IFA_FLAGS, so set them here */ - ifamsg->ifa_flags = addr->flags & 0xff; - - buf = (void *) ifamsg + NLMSG_ALIGN(sizeof(struct ifaddrmsg)); - - if (addr->family == AF_INET) { - buf += rta_add_data(buf, IFA_LOCAL, &addr->in_addr, - sizeof(struct in_addr)); - buf += rta_add_data(buf, IFA_BROADCAST, &addr->broadcast, - sizeof(struct in_addr)); - } else - buf += rta_add_data(buf, IFA_LOCAL, &addr->in6_addr, - sizeof(struct in6_addr)); - - /* Address & Prefix length are enough to perform deletions */ - if (nlmsg_type == RTM_DELADDR) - goto done; - - if (addr->flags & 0xffffff00) - buf += rta_add_u32(buf, IFA_FLAGS, addr->flags & 0xffffff00); - - if (addr->label[0]) - buf += rta_add_data(buf, IFA_LABEL, - addr->label, strlen(addr->label) + 1); - - if (addr->preferred_expiry_time > now || - addr->valid_expiry_time > now) { - struct ifa_cacheinfo cinfo; - - memset(&cinfo, 0, sizeof(cinfo)); - cinfo.ifa_prefered = addr->preferred_expiry_time > now ? - l_time_to_secs(addr->preferred_expiry_time - now) : 0; - cinfo.ifa_valid = addr->valid_expiry_time > now ? - l_time_to_secs(addr->valid_expiry_time - now) : 0; - - buf += rta_add_data(buf, IFA_CACHEINFO, &cinfo, sizeof(cinfo)); - } - -done: - id = l_netlink_send(rtnl, nlmsg_type, flags, - ifamsg, buf - (void *) ifamsg, - cb, user_data, destroy); - l_free(ifamsg); - - return id; -} - LIB_EXPORT struct l_rtnl_address *l_rtnl_ifaddr_extract( const struct ifaddrmsg *ifa, int bytes) @@ -1324,8 +1155,12 @@ LIB_EXPORT uint32_t l_rtnl_ifaddr_add(struct l_netlink *rtnl, int ifindex, void *user_data, l_netlink_destroy_func_t destroy) { - return _rtnl_ifaddr_change(rtnl, RTM_NEWADDR, ifindex, addr, - cb, user_data, destroy); + struct l_netlink_message *nlm = + rtnl_message_from_address(RTM_NEWADDR, + NLM_F_CREATE | NLM_F_REPLACE, + ifindex, addr); + + return l_netlink_send(rtnl, nlm, cb, user_data, destroy); } LIB_EXPORT uint32_t l_rtnl_ifaddr_delete(struct l_netlink *rtnl, int ifindex, @@ -1334,84 +1169,10 @@ LIB_EXPORT uint32_t l_rtnl_ifaddr_delete(struct l_netlink *rtnl, int ifindex, void *user_data, l_netlink_destroy_func_t destroy) { - return _rtnl_ifaddr_change(rtnl, RTM_DELADDR, ifindex, addr, - cb, user_data, destroy); -} + struct l_netlink_message *nlm = + rtnl_message_from_address(RTM_DELADDR, 0, ifindex, addr); -static uint32_t _rtnl_route_change(struct l_netlink *rtnl, - uint16_t nlmsg_type, int ifindex, - const struct l_rtnl_route *rt, - l_netlink_command_func_t cb, - void *user_data, - l_netlink_destroy_func_t destroy) -{ - L_AUTO_FREE_VAR(struct rtmsg *, rtmmsg) = NULL; - size_t bufsize; - void *rta_buf; - uint16_t flags; - uint64_t now = l_time_now(); - - bufsize = NLMSG_ALIGN(sizeof(struct rtmsg)) + - RTA_SPACE(sizeof(uint32_t)) + /* RTA_OIF */ - RTA_SPACE(sizeof(uint32_t)) + /* RTA_PRIORITY */ - RTA_SPACE(sizeof(struct in6_addr)) + /* RTA_GATEWAY */ - RTA_SPACE(sizeof(struct in6_addr)) + /* RTA_DST */ - RTA_SPACE(sizeof(struct in6_addr)) + /* RTA_PREFSRC */ - 256 + /* RTA_METRICS */ - RTA_SPACE(sizeof(uint8_t)) + /* RTA_PREF */ - RTA_SPACE(sizeof(uint32_t)); /* RTA_EXPIRES */ - - rtmmsg = l_malloc(bufsize); - memset(rtmmsg, 0, bufsize); - - rtmmsg->rtm_family = rt->family; - rtmmsg->rtm_table = RT_TABLE_MAIN; - rtmmsg->rtm_protocol = rt->protocol; - rtmmsg->rtm_type = RTN_UNICAST; - rtmmsg->rtm_scope = rt->scope; - - flags = NLM_F_CREATE | NLM_F_REPLACE; - - rta_buf = (void *) rtmmsg + NLMSG_ALIGN(sizeof(struct rtmsg)); - rta_buf += rta_add_u32(rta_buf, RTA_OIF, ifindex); - - if (rt->priority) - rta_buf += rta_add_u32(rta_buf, RTA_PRIORITY, - rt->priority + ifindex); - - if (!address_is_null(rt->family, &rt->gw.in_addr, &rt->gw.in6_addr)) - rta_buf += rta_add_address(rta_buf, RTA_GATEWAY, rt->family, - &rt->gw.in6_addr, &rt->gw.in_addr); - - if (rt->dst_prefix_len) { - rtmmsg->rtm_dst_len = rt->dst_prefix_len; - rta_buf += rta_add_address(rta_buf, RTA_DST, rt->family, - &rt->dst.in6_addr, &rt->dst.in_addr); - } - - if (!address_is_null(rt->family, &rt->prefsrc.in_addr, - &rt->prefsrc.in6_addr)) - rta_buf += rta_add_address(rta_buf, RTA_PREFSRC, rt->family, - &rt->prefsrc.in6_addr, - &rt->prefsrc.in_addr); - - if (rt->mtu) { - uint8_t buf[256]; - size_t written = rta_add_u32(buf, RTAX_MTU, rt->mtu); - - rta_buf += rta_add_data(rta_buf, RTA_METRICS, buf, written); - } - - if (rt->preference) - rta_buf += rta_add_u8(rta_buf, RTA_PREF, rt->preference); - - if (rt->expiry_time > now) - rta_buf += rta_add_u32(rta_buf, RTA_EXPIRES, - l_time_to_secs(rt->expiry_time - now)); - - return l_netlink_send(rtnl, nlmsg_type, flags, rtmmsg, - rta_buf - (void *) rtmmsg, cb, user_data, - destroy); + return l_netlink_send(rtnl, nlm, cb, user_data, destroy); } LIB_EXPORT uint32_t l_rtnl_route_add(struct l_netlink *rtnl, int ifindex, @@ -1420,8 +1181,12 @@ LIB_EXPORT uint32_t l_rtnl_route_add(struct l_netlink *rtnl, int ifindex, void *user_data, l_netlink_destroy_func_t destroy) { - return _rtnl_route_change(rtnl, RTM_NEWROUTE, ifindex, rt, - cb, user_data, destroy); + struct l_netlink_message *nlm = + rtnl_message_from_route(RTM_NEWROUTE, + NLM_F_CREATE | NLM_F_REPLACE, + ifindex, rt); + + return l_netlink_send(rtnl, nlm, cb, user_data, destroy); } LIB_EXPORT uint32_t l_rtnl_route_delete(struct l_netlink *rtnl, int ifindex, @@ -1430,8 +1195,10 @@ LIB_EXPORT uint32_t l_rtnl_route_delete(struct l_netlink *rtnl, int ifindex, void *user_data, l_netlink_destroy_func_t destroy) { - return _rtnl_route_change(rtnl, RTM_DELROUTE, ifindex, rt, - cb, user_data, destroy); + struct l_netlink_message *nlm = + rtnl_message_from_route(RTM_DELROUTE, 0, ifindex, rt); + + return l_netlink_send(rtnl, nlm, cb, user_data, destroy); } struct rtnl_neighbor_get_data { @@ -1500,25 +1267,21 @@ LIB_EXPORT uint32_t l_rtnl_neighbor_get_hwaddr(struct l_netlink *rtnl, void *user_data, l_netlink_destroy_func_t destroy) { - size_t bufsize = NLMSG_ALIGN(sizeof(struct ndmsg)) + - RTA_SPACE(16); /* NDA_DST */ - uint8_t buf[bufsize]; - struct ndmsg *ndmsg = (struct ndmsg *) buf; - void *rta_buf = (void *) ndmsg + NLMSG_ALIGN(sizeof(struct ndmsg)); + struct ndmsg ndm; + struct l_netlink_message *nlm = l_netlink_message_new(RTM_GETNEIGH, 0); __auto_type cb_data = struct_alloc(rtnl_neighbor_get_data, cb, user_data, destroy); - uint32_t ret; + int ret; - memset(buf, 0, bufsize); - ndmsg->ndm_family = family; - ndmsg->ndm_ifindex = ifindex; - ndmsg->ndm_flags = 0; + memset(&ndm, 0, sizeof(ndm)); + ndm.ndm_family = family; + ndm.ndm_ifindex = ifindex; + ndm.ndm_flags = 0; - rta_buf += rta_add_address(rta_buf, NDA_DST, family, ip, ip); + l_netlink_message_add_header(nlm, &ndm, sizeof(ndm)); + append_address(nlm, NDA_DST, family, ip, ip); - ret = l_netlink_send(rtnl, RTM_GETNEIGH, 0, ndmsg, - rta_buf - (void *) ndmsg, - rtnl_neighbor_get_cb, cb_data, + ret = l_netlink_send(rtnl, nlm, rtnl_neighbor_get_cb, cb_data, rtnl_neighbor_get_destroy_cb); if (ret) return ret; @@ -1536,25 +1299,21 @@ LIB_EXPORT uint32_t l_rtnl_neighbor_set_hwaddr(struct l_netlink *rtnl, void *user_data, l_netlink_destroy_func_t destroy) { - size_t bufsize = NLMSG_ALIGN(sizeof(struct ndmsg)) + - RTA_SPACE(16) + /* NDA_DST */ - RTA_SPACE(hwaddr_len); /* NDA_LLADDR */ - uint8_t buf[bufsize]; - struct ndmsg *ndmsg = (struct ndmsg *) buf; - void *rta_buf = (void *) ndmsg + NLMSG_ALIGN(sizeof(struct ndmsg)); - - memset(buf, 0, bufsize); - ndmsg->ndm_family = family; - ndmsg->ndm_ifindex = ifindex; - ndmsg->ndm_flags = 0; - ndmsg->ndm_state = NUD_REACHABLE; - - rta_buf += rta_add_address(rta_buf, NDA_DST, family, ip, ip); - rta_buf += rta_add_data(rta_buf, NDA_LLADDR, hwaddr, hwaddr_len); - - return l_netlink_send(rtnl, RTM_NEWNEIGH, NLM_F_CREATE | NLM_F_REPLACE, - ndmsg, rta_buf - (void *) ndmsg, - cb, user_data, destroy); + struct ndmsg ndm; + struct l_netlink_message *nlm = + l_netlink_message_new(RTM_NEWNEIGH, + NLM_F_CREATE | NLM_F_REPLACE); + memset(&ndm, 0, sizeof(ndm)); + ndm.ndm_family = family; + ndm.ndm_ifindex = ifindex; + ndm.ndm_flags = 0; + ndm.ndm_state = NUD_REACHABLE; + + l_netlink_message_add_header(nlm, &ndm, sizeof(ndm)); + append_address(nlm, NDA_DST, family, ip, ip); + l_netlink_message_append(nlm, NDA_LLADDR, hwaddr, hwaddr_len); + + return l_netlink_send(rtnl, nlm, cb, user_data, destroy); } __attribute__((destructor(32000))) static void free_rtnl() diff --git a/unit/test-netlink.c b/unit/test-netlink.c index 1bcf1e4b274c..da23ee5c9b0e 100644 --- a/unit/test-netlink.c +++ b/unit/test-netlink.c @@ -66,7 +66,10 @@ static void link_notification(uint16_t type, void const * data, int main(int argc, char *argv[]) { struct l_netlink *netlink; - struct ifinfomsg msg; + struct ifinfomsg ifi; + struct l_netlink_message *nlm = + l_netlink_message_new_sized(RTM_GETLINK, + NLM_F_DUMP, sizeof(ifi)); unsigned int link_id; if (!l_main_init()) @@ -78,10 +81,10 @@ int main(int argc, char *argv[]) l_netlink_set_debug(netlink, do_debug, "[NETLINK] ", NULL); - memset(&msg, 0, sizeof(msg)); + memset(&ifi, 0, sizeof(ifi)); + l_netlink_message_add_header(nlm, &ifi, sizeof(ifi)); - l_netlink_send(netlink, RTM_GETLINK, NLM_F_DUMP, &msg, sizeof(msg), - getlink_callback, NULL, NULL); + l_netlink_send(netlink, nlm, getlink_callback, NULL, NULL); link_id = l_netlink_register(netlink, RTNLGRP_LINK, link_notification, NULL, NULL);