From patchwork Wed Mar 7 09:10:42 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Serhey Popovych X-Patchwork-Id: 882484 X-Patchwork-Delegate: pablo@netfilter.org Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=vger.kernel.org (client-ip=209.132.180.67; helo=vger.kernel.org; envelope-from=netfilter-devel-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="kpKMHBnh"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 3zx7Cz4M72z9sf8 for ; Wed, 7 Mar 2018 20:10:55 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751204AbeCGJKz (ORCPT ); Wed, 7 Mar 2018 04:10:55 -0500 Received: from mail-lf0-f65.google.com ([209.85.215.65]:44508 "EHLO mail-lf0-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750989AbeCGJKx (ORCPT ); Wed, 7 Mar 2018 04:10:53 -0500 Received: by mail-lf0-f65.google.com with SMTP id v9-v6so2122922lfa.11 for ; Wed, 07 Mar 2018 01:10:52 -0800 (PST) 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; bh=+GsOODgRP1zGOZPSKr6z8QERLg5R8N/+yDu0uFB0UUM=; b=kpKMHBnhk1QJnctSUVadXSW6+uK/4Uj94quxwnD1pYaCmtY48x3fqrB71Ej4AhJ8Gb ksKgqDYTmw9/SfQMrwxl/Wt5Fdw+sosVibOLFYYXt0lLw+0Jt1fqoMnEVzREGfn0K54p Oap00bCIfaPFSkAnBOQHngJn3+Ea7uImO+o5rpMPfJRJR56bbZnjLbVXERoqBXsTnBWu eWpF9YJC6awOUWKIrbBZ6ixEAfwnXBCPTb8Ciu1Ssrp5HNPBwQ6w3X00cdfZ9LW0ap6n CCfwGukDt0cyPEn2n2Ec7auQ1opUGiptORagrIbHErPUUTbttXTEaO/hz3KKyQfk89DB XhVQ== 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; bh=+GsOODgRP1zGOZPSKr6z8QERLg5R8N/+yDu0uFB0UUM=; b=I4YzeVOp8WIcqKhfj1l9vvCSVmRUvEMHT76+KZfekBYzUa7ADpqXgUOYMIoOq5pNN0 WVh4u9phMNq+tXs7bpXFdHrLNPvSi+dWlJFDUYclH7H/O8N+D/g9qDW7pCGXUUX69Sqc LXuu4V6s6B9f7t90QoN8MpvD8uum7ZmgcvL4HK4/1qYbsiF5ZUY0zYuGXZ8U9fezc7eU 6uOzheymXcRDblmAps7t8Wr4qRlGWaEMxxSnQ8q+p4yhvdjEjlq1EcD8gvh0tQXnibNp naGAm5toef9Ey28kQREQpnEQ/SDE3z4wvsvfjTZA9B6sQA1VLPDvODheUPXMRX96SQfH foPA== X-Gm-Message-State: APf1xPCJj2EvoOYgHOHGNljwFKhm1dczItVfxyQbobUBmHRhcenfXy0S Bwfhs/+ljcGY56uJCJrkHJTLpg== X-Google-Smtp-Source: AG47ELuFP1Sqy2nEVUEGJ1b2EtWoGcmIwhN/ptC+/G1pius12R98YA+mGKPh9w208LWNYWjvAn5GRg== X-Received: by 10.46.33.230 with SMTP id h99mr15838232lji.47.1520413851531; Wed, 07 Mar 2018 01:10:51 -0800 (PST) Received: from tuxracer.localdomain ([2a01:6d80::195:20:96:53]) by smtp.gmail.com with ESMTPSA id j7sm3643490lfh.46.2018.03.07.01.10.50 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 07 Mar 2018 01:10:50 -0800 (PST) From: Serhey Popovych To: netfilter-devel@vger.kernel.org Cc: willem.j.debruijn@gmail.com Subject: [PATCH iptables 3/4] xtables: Register all match/target revisions supported by us and kernel Date: Wed, 7 Mar 2018 11:10:42 +0200 Message-Id: <1520413843-24456-4-git-send-email-serhe.popovych@gmail.com> X-Mailer: git-send-email 1.7.10.4 In-Reply-To: <1520413843-24456-1-git-send-email-serhe.popovych@gmail.com> References: <1520413843-24456-1-git-send-email-serhe.popovych@gmail.com> Sender: netfilter-devel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netfilter-devel@vger.kernel.org Keep the order of matches by appending them; keep order between revisions of same match from most to least recent. All of this keeps xtables_find_match() happy to find most recent supported by kernel revision in the given order. Apply the same for targets, except prepend targets; order between revisions preserved too. All this needed to fix nasty bug related to iptables package update and broken print/save output. After this change all supported revisions of match/target stored in corresponding list with following pattern: xt_matches xt_targets ========== ========== m1 m2 m3 mN tN t1 t2 t3 +-----+--+---+---~~~---+ +---~~~---+---+----+--+ |43210|10|210|revisions| |revisions|210|3210|10| +-----+--+---+---~~~---+ +---~~~---+---+----+--+ Where new [m]atches added to the list tail and new [t]argets added to the list head to preserve previous behaviour. Multiple revisions of single match/target type are grouped together and sorted in descending order. Both this ensures xtables_find_match() and xtables_find_target() behaviour remains the same after change: find highest supported match/target revision given by it's name. Signed-off-by: Serhey Popovych --- libxtables/xtables.c | 95 +++++++++++++++++++++++++++++++++++++------------- 1 file changed, 71 insertions(+), 24 deletions(-) diff --git a/libxtables/xtables.c b/libxtables/xtables.c index 33fc158..5a115ff 100644 --- a/libxtables/xtables.c +++ b/libxtables/xtables.c @@ -963,7 +963,7 @@ static int xtables_target_prefer(const struct xtables_target *a, static bool xtables_fully_register_pending_match(struct xtables_match *me) { - struct xtables_match **i, *old; + struct xtables_match **i, *old, *pos = NULL; const char *rn; int compare; @@ -973,7 +973,7 @@ static bool xtables_fully_register_pending_match(struct xtables_match *me) return false; old = xtables_find_match(me->name, XTF_DURING_LOAD, NULL); - if (old) { + while (old) { compare = xtables_match_prefer(old, me); if (compare == 0) { fprintf(stderr, @@ -984,18 +984,41 @@ static bool xtables_fully_register_pending_match(struct xtables_match *me) /* Now we have two (or more) options, check compatibility. */ rn = (old->real_name != NULL) ? old->real_name : old->name; - if (compare > 0 && - compatible_match_revision(rn, old->revision)) - return true; + if (compare > 0) { + /* Kernel tells old isn't compatible anymore??? */ + if (!compatible_match_revision(rn, old->revision)) { + /* Delete old one. */ + for (i = &xtables_matches; *i != old;) + i = &(*i)->next; + *i = old->next; + } + pos = old; + old = old->next; + if (!old) + break; + if (!extension_cmp(me->name, old->name, old->family)) + break; + continue; + } - /* Delete old one. */ - for (i = &xtables_matches; *i!=old; i = &(*i)->next); - *i = old->next; + /* Found right old */ + pos = old; + break; + } + + if (!pos) { + /* Append to list. */ + for (i = &xtables_matches; *i; i = &(*i)->next); + } else if (compare < 0) { + /* Prepend it */ + for (i = &xtables_matches; *i != pos; i = &(*i)->next); + } else if (compare > 0) { + /* Append it */ + i = &pos->next; + pos = pos->next; } - /* Append to list. */ - for (i = &xtables_matches; *i; i = &(*i)->next); - me->next = NULL; + me->next = pos; *i = me; me->m = NULL; @@ -1069,7 +1092,7 @@ void xtables_register_target(struct xtables_target *me) static bool xtables_fully_register_pending_target(struct xtables_target *me) { - struct xtables_target *old; + struct xtables_target **i, *old, *pos = NULL; const char *rn; int compare; @@ -1081,9 +1104,7 @@ static bool xtables_fully_register_pending_target(struct xtables_target *me) } old = xtables_find_target(me->name, XTF_DURING_LOAD); - if (old) { - struct xtables_target **i; - + while (old) { compare = xtables_target_prefer(old, me); if (compare == 0) { fprintf(stderr, @@ -1094,18 +1115,44 @@ static bool xtables_fully_register_pending_target(struct xtables_target *me) /* Now we have two (or more) options, check compatibility. */ rn = (old->real_name != NULL) ? old->real_name : old->name; - if (compare > 0 && - compatible_target_revision(rn, old->revision)) - return true; + if (compare > 0) { + /* Kernel tells old isn't compatible anymore??? */ + if (!compatible_target_revision(rn, old->revision)) { + /* Delete old one. */ + for (i = &xtables_targets; *i != old;) + i = &(*i)->next; + *i = old->next; + } + pos = old; + old = old->next; + if (!old) + break; + if (!extension_cmp(me->name, old->name, old->family)) + break; + continue; + } - /* Delete old one. */ - for (i = &xtables_targets; *i!=old; i = &(*i)->next); - *i = old->next; + /* Found right old */ + pos = old; + break; } - /* Prepend to list. */ - me->next = xtables_targets; - xtables_targets = me; + if (!pos) { + /* Prepend to list. */ + i = &xtables_targets; + pos = xtables_targets; + } else if (compare < 0) { + /* Prepend it */ + for (i = &xtables_targets; *i != pos; i = &(*i)->next); + } else if (compare > 0) { + /* Append it */ + i = &pos->next; + pos = pos->next; + } + + me->next = pos; + *i = me; + me->t = NULL; me->tflags = 0;