From patchwork Thu Oct 3 21:57:58 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom Herbert X-Patchwork-Id: 1171425 X-Patchwork-Delegate: davem@davemloft.net Return-Path: X-Original-To: patchwork-incoming-netdev@ozlabs.org Delivered-To: patchwork-incoming-netdev@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=netdev-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=herbertland.com Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=herbertland-com.20150623.gappssmtp.com header.i=@herbertland-com.20150623.gappssmtp.com header.b="ea74grYz"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 46kn2M542Rz9sNw for ; Fri, 4 Oct 2019 07:58:35 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730963AbfJCV6e (ORCPT ); Thu, 3 Oct 2019 17:58:34 -0400 Received: from mail-pl1-f194.google.com ([209.85.214.194]:33492 "EHLO mail-pl1-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726002AbfJCV6e (ORCPT ); Thu, 3 Oct 2019 17:58:34 -0400 Received: by mail-pl1-f194.google.com with SMTP id d22so2155777pls.0 for ; Thu, 03 Oct 2019 14:58:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=herbertland-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=mps6q5hYlpULRcG79FBzG15B2AoG2afrXwlHInjloGI=; b=ea74grYz/XPydhCiB3dTRPOAkvC0FCjQ7edNQIXsSLjUa2fK/gp7vpSYrxCJ0L83N3 UfKULoQZrkYa9Nvr4Xpdfse3JVW/ANuRms+3zRGb73xAhWZf0/YoNZTxCxuweiddk/yn oeJXfYPSsQy6KXC+vro+nDiNq61tujoylPmeRYYvDPO2XJdk9V0PQflIXI5nDzzxFaRT opHPu5REklW3yiSHmI9UxalPwf4mXBTlFxsuKIqNa3tzp3xsTymUbpiZ6wwAVORW/Q93 NiH1C4JLLUtnDqqiW7B3i73053U85qo5Axru0Oemi5OCdRRXQN/T+WF064xEi0yGAkVd iTdA== 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=mps6q5hYlpULRcG79FBzG15B2AoG2afrXwlHInjloGI=; b=nv0IyB2zkscaJvJ+VaAMlE2J+o3Cakct9+BUX4hd3hYah7bt5pTyo/Xta/zfi9Eu+r FHoTh7kzE2QsuVWJ+qSpJ263YOGag8l/rfFXnzp+OkKEv7M3u0dt6uG3mdY6wY2fvz7D VbHCuLA2VJL3JXybkGAVj7yWX420lLnpZ1w/bZ1jrFveQIS3F+6yl1TEjKRtWctg/A/u AKvh0Bh1ncHaF53b1I31SDk7mb5F0EVJu27r3LAwUT6jXDmtG9Ijx/AAPM4M2tlOrU7f oL4+bISRCLC9jibIx/HOTY3T1gyFz4nhwnpY0REkH6rI/a4zzkQ0kyRb+HKiJPdQitlT VZBw== X-Gm-Message-State: APjAAAVDF1+NyXNKJuPQhYSTkhImVaZ8JeuCGFYm5LFEZVKSYAsmVA2E llTcFRe64xqMbZ5nL9l+MRIz8YAIWADuvA== X-Google-Smtp-Source: APXvYqwlYgiHXCxH0A+uFxe5XWp6v7eLZuQpfqnm/xZbcIoEFTgWLP0+0RjS52mkZWBxDUGxz1RiHg== X-Received: by 2002:a17:902:560f:: with SMTP id h15mr12046079pli.145.1570139911845; Thu, 03 Oct 2019 14:58:31 -0700 (PDT) Received: from localhost.localdomain (c-73-202-182-113.hsd1.ca.comcast.net. [73.202.182.113]) by smtp.gmail.com with ESMTPSA id r18sm3889905pfc.3.2019.10.03.14.58.30 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Thu, 03 Oct 2019 14:58:31 -0700 (PDT) From: Tom Herbert To: davem@davemloft.net, netdev@vger.kernel.org Cc: Tom Herbert , Tom Herbert Subject: [PATCH v5 net-next 1/7] ipeh: Create exthdrs_options.c and ipeh.h Date: Thu, 3 Oct 2019 14:57:58 -0700 Message-Id: <1570139884-20183-2-git-send-email-tom@herbertland.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1570139884-20183-1-git-send-email-tom@herbertland.com> References: <1570139884-20183-1-git-send-email-tom@herbertland.com> Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org From: Tom Herbert Create exthdrs_options.c to hold code related to specific Hop-by-Hop and Destination extension header options. Move related functions in exthdrs.c to the new file. Create include net/ipeh.h to contain common definitions for IP extension headers. Signed-off-by: Tom Herbert Reviewed-by: Simon Horman --- include/net/ipeh.h | 22 +++++ include/net/ipv6.h | 1 + net/ipv6/Makefile | 2 +- net/ipv6/exthdrs.c | 204 --------------------------------------------- net/ipv6/exthdrs_options.c | 201 ++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 225 insertions(+), 205 deletions(-) create mode 100644 include/net/ipeh.h create mode 100644 net/ipv6/exthdrs_options.c diff --git a/include/net/ipeh.h b/include/net/ipeh.h new file mode 100644 index 0000000..ec2d186 --- /dev/null +++ b/include/net/ipeh.h @@ -0,0 +1,22 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _NET_IPEH_H +#define _NET_IPEH_H + +#include + +/* + * Parsing tlv encoded headers. + * + * Parsing function "func" returns true, if parsing succeed + * and false, if it failed. + * It MUST NOT touch skb->h. + */ +struct tlvtype_proc { + int type; + bool (*func)(struct sk_buff *skb, int offset); +}; + +extern const struct tlvtype_proc tlvprocdestopt_lst[]; +extern const struct tlvtype_proc tlvprochopopt_lst[]; + +#endif /* _NET_IPEH_H */ diff --git a/include/net/ipv6.h b/include/net/ipv6.h index d04b7ab..6fc584c 100644 --- a/include/net/ipv6.h +++ b/include/net/ipv6.h @@ -20,6 +20,7 @@ #include #include #include +#include #define SIN6_LEN_RFC2133 24 diff --git a/net/ipv6/Makefile b/net/ipv6/Makefile index 8ccf355..df3919b 100644 --- a/net/ipv6/Makefile +++ b/net/ipv6/Makefile @@ -10,7 +10,7 @@ ipv6-objs := af_inet6.o anycast.o ip6_output.o ip6_input.o addrconf.o \ route.o ip6_fib.o ipv6_sockglue.o ndisc.o udp.o udplite.o \ raw.o icmp.o mcast.o reassembly.o tcp_ipv6.o ping.o \ exthdrs.o datagram.o ip6_flowlabel.o inet6_connection_sock.o \ - udp_offload.o seg6.o fib6_notifier.o + udp_offload.o seg6.o fib6_notifier.o exthdrs_options.o ipv6-offload := ip6_offload.o tcpv6_offload.o exthdrs_offload.o diff --git a/net/ipv6/exthdrs.c b/net/ipv6/exthdrs.c index ab5add0..664491e 100644 --- a/net/ipv6/exthdrs.c +++ b/net/ipv6/exthdrs.c @@ -39,7 +39,6 @@ #include #include #include -#include #if IS_ENABLED(CONFIG_IPV6_MIP6) #include #endif @@ -51,19 +50,6 @@ #include -/* - * Parsing tlv encoded headers. - * - * Parsing function "func" returns true, if parsing succeed - * and false, if it failed. - * It MUST NOT touch skb->h. - */ - -struct tlvtype_proc { - int type; - bool (*func)(struct sk_buff *skb, int offset); -}; - /********************* Generic functions *********************/ @@ -200,80 +186,6 @@ static bool ip6_parse_tlv(const struct tlvtype_proc *procs, return false; } -/***************************** - Destination options header. - *****************************/ - -#if IS_ENABLED(CONFIG_IPV6_MIP6) -static bool ipv6_dest_hao(struct sk_buff *skb, int optoff) -{ - struct ipv6_destopt_hao *hao; - struct inet6_skb_parm *opt = IP6CB(skb); - struct ipv6hdr *ipv6h = ipv6_hdr(skb); - int ret; - - if (opt->dsthao) { - net_dbg_ratelimited("hao duplicated\n"); - goto discard; - } - opt->dsthao = opt->dst1; - opt->dst1 = 0; - - hao = (struct ipv6_destopt_hao *)(skb_network_header(skb) + optoff); - - if (hao->length != 16) { - net_dbg_ratelimited("hao invalid option length = %d\n", - hao->length); - goto discard; - } - - if (!(ipv6_addr_type(&hao->addr) & IPV6_ADDR_UNICAST)) { - net_dbg_ratelimited("hao is not an unicast addr: %pI6\n", - &hao->addr); - goto discard; - } - - ret = xfrm6_input_addr(skb, (xfrm_address_t *)&ipv6h->daddr, - (xfrm_address_t *)&hao->addr, IPPROTO_DSTOPTS); - if (unlikely(ret < 0)) - goto discard; - - if (skb_cloned(skb)) { - if (pskb_expand_head(skb, 0, 0, GFP_ATOMIC)) - goto discard; - - /* update all variable using below by copied skbuff */ - hao = (struct ipv6_destopt_hao *)(skb_network_header(skb) + - optoff); - ipv6h = ipv6_hdr(skb); - } - - if (skb->ip_summed == CHECKSUM_COMPLETE) - skb->ip_summed = CHECKSUM_NONE; - - swap(ipv6h->saddr, hao->addr); - - if (skb->tstamp == 0) - __net_timestamp(skb); - - return true; - - discard: - kfree_skb(skb); - return false; -} -#endif - -static const struct tlvtype_proc tlvprocdestopt_lst[] = { -#if IS_ENABLED(CONFIG_IPV6_MIP6) - { - .type = IPV6_TLV_HAO, - .func = ipv6_dest_hao, - }, -#endif - {-1, NULL} -}; - static int ipv6_destopt_rcv(struct sk_buff *skb) { struct inet6_dev *idev = __in6_dev_get(skb->dev); @@ -702,122 +614,6 @@ void ipv6_exthdrs_exit(void) inet6_del_protocol(&rthdr_protocol, IPPROTO_ROUTING); } -/********************************** - Hop-by-hop options. - **********************************/ - -/* - * Note: we cannot rely on skb_dst(skb) before we assign it in ip6_route_input(). - */ -static inline struct inet6_dev *ipv6_skb_idev(struct sk_buff *skb) -{ - return skb_dst(skb) ? ip6_dst_idev(skb_dst(skb)) : __in6_dev_get(skb->dev); -} - -static inline struct net *ipv6_skb_net(struct sk_buff *skb) -{ - return skb_dst(skb) ? dev_net(skb_dst(skb)->dev) : dev_net(skb->dev); -} - -/* Router Alert as of RFC 2711 */ - -static bool ipv6_hop_ra(struct sk_buff *skb, int optoff) -{ - const unsigned char *nh = skb_network_header(skb); - - if (nh[optoff + 1] == 2) { - IP6CB(skb)->flags |= IP6SKB_ROUTERALERT; - memcpy(&IP6CB(skb)->ra, nh + optoff + 2, sizeof(IP6CB(skb)->ra)); - return true; - } - net_dbg_ratelimited("ipv6_hop_ra: wrong RA length %d\n", - nh[optoff + 1]); - kfree_skb(skb); - return false; -} - -/* Jumbo payload */ - -static bool ipv6_hop_jumbo(struct sk_buff *skb, int optoff) -{ - const unsigned char *nh = skb_network_header(skb); - struct inet6_dev *idev = __in6_dev_get_safely(skb->dev); - struct net *net = ipv6_skb_net(skb); - u32 pkt_len; - - if (nh[optoff + 1] != 4 || (optoff & 3) != 2) { - net_dbg_ratelimited("ipv6_hop_jumbo: wrong jumbo opt length/alignment %d\n", - nh[optoff+1]); - __IP6_INC_STATS(net, idev, IPSTATS_MIB_INHDRERRORS); - goto drop; - } - - pkt_len = ntohl(*(__be32 *)(nh + optoff + 2)); - if (pkt_len <= IPV6_MAXPLEN) { - __IP6_INC_STATS(net, idev, IPSTATS_MIB_INHDRERRORS); - icmpv6_param_prob(skb, ICMPV6_HDR_FIELD, optoff+2); - return false; - } - if (ipv6_hdr(skb)->payload_len) { - __IP6_INC_STATS(net, idev, IPSTATS_MIB_INHDRERRORS); - icmpv6_param_prob(skb, ICMPV6_HDR_FIELD, optoff); - return false; - } - - if (pkt_len > skb->len - sizeof(struct ipv6hdr)) { - __IP6_INC_STATS(net, idev, IPSTATS_MIB_INTRUNCATEDPKTS); - goto drop; - } - - if (pskb_trim_rcsum(skb, pkt_len + sizeof(struct ipv6hdr))) - goto drop; - - IP6CB(skb)->flags |= IP6SKB_JUMBOGRAM; - return true; - -drop: - kfree_skb(skb); - return false; -} - -/* CALIPSO RFC 5570 */ - -static bool ipv6_hop_calipso(struct sk_buff *skb, int optoff) -{ - const unsigned char *nh = skb_network_header(skb); - - if (nh[optoff + 1] < 8) - goto drop; - - if (nh[optoff + 6] * 4 + 8 > nh[optoff + 1]) - goto drop; - - if (!calipso_validate(skb, nh + optoff)) - goto drop; - - return true; - -drop: - kfree_skb(skb); - return false; -} - -static const struct tlvtype_proc tlvprochopopt_lst[] = { - { - .type = IPV6_TLV_ROUTERALERT, - .func = ipv6_hop_ra, - }, - { - .type = IPV6_TLV_JUMBO, - .func = ipv6_hop_jumbo, - }, - { - .type = IPV6_TLV_CALIPSO, - .func = ipv6_hop_calipso, - }, - { -1, } -}; - int ipv6_parse_hopopts(struct sk_buff *skb) { struct inet6_skb_parm *opt = IP6CB(skb); diff --git a/net/ipv6/exthdrs_options.c b/net/ipv6/exthdrs_options.c new file mode 100644 index 0000000..032e072 --- /dev/null +++ b/net/ipv6/exthdrs_options.c @@ -0,0 +1,201 @@ +// SPDX-License-Identifier: GPL-2.0 +#include +#include +#include +#include +#include +#include +#include +#include +#include +#if IS_ENABLED(CONFIG_IPV6_MIP6) +#include +#endif + +/* Destination options header */ + +#if IS_ENABLED(CONFIG_IPV6_MIP6) +static bool ipv6_dest_hao(struct sk_buff *skb, int optoff) +{ + struct ipv6_destopt_hao *hao; + struct inet6_skb_parm *opt = IP6CB(skb); + struct ipv6hdr *ipv6h = ipv6_hdr(skb); + int ret; + + if (opt->dsthao) { + net_dbg_ratelimited("hao duplicated\n"); + goto discard; + } + opt->dsthao = opt->dst1; + opt->dst1 = 0; + + hao = (struct ipv6_destopt_hao *)(skb_network_header(skb) + optoff); + + if (hao->length != 16) { + net_dbg_ratelimited("hao invalid option length = %d\n", + hao->length); + goto discard; + } + + if (!(ipv6_addr_type(&hao->addr) & IPV6_ADDR_UNICAST)) { + net_dbg_ratelimited("hao is not an unicast addr: %pI6\n", + &hao->addr); + goto discard; + } + + ret = xfrm6_input_addr(skb, (xfrm_address_t *)&ipv6h->daddr, + (xfrm_address_t *)&hao->addr, IPPROTO_DSTOPTS); + if (unlikely(ret < 0)) + goto discard; + + if (skb_cloned(skb)) { + if (pskb_expand_head(skb, 0, 0, GFP_ATOMIC)) + goto discard; + + /* update all variable using below by copied skbuff */ + hao = (struct ipv6_destopt_hao *)(skb_network_header(skb) + + optoff); + ipv6h = ipv6_hdr(skb); + } + + if (skb->ip_summed == CHECKSUM_COMPLETE) + skb->ip_summed = CHECKSUM_NONE; + + swap(ipv6h->saddr, hao->addr); + + if (skb->tstamp == 0) + __net_timestamp(skb); + + return true; + + discard: + kfree_skb(skb); + return false; +} +#endif + +const struct tlvtype_proc tlvprocdestopt_lst[] = { +#if IS_ENABLED(CONFIG_IPV6_MIP6) + { + .type = IPV6_TLV_HAO, + .func = ipv6_dest_hao, + }, +#endif + {-1, NULL} +}; + +/* Hop-by-hop options */ + +/* Note: we cannot rely on skb_dst(skb) before we assign it in + * ip6_route_input(). + */ +static inline struct inet6_dev *ipv6_skb_idev(struct sk_buff *skb) +{ + return skb_dst(skb) ? ip6_dst_idev(skb_dst(skb)) : + __in6_dev_get(skb->dev); +} + +static inline struct net *ipv6_skb_net(struct sk_buff *skb) +{ + return skb_dst(skb) ? dev_net(skb_dst(skb)->dev) : dev_net(skb->dev); +} + +/* Router Alert as of RFC 2711 */ + +static bool ipv6_hop_ra(struct sk_buff *skb, int optoff) +{ + const unsigned char *nh = skb_network_header(skb); + + if (nh[optoff + 1] == 2) { + IP6CB(skb)->flags |= IP6SKB_ROUTERALERT; + memcpy(&IP6CB(skb)->ra, nh + optoff + 2, + sizeof(IP6CB(skb)->ra)); + return true; + } + net_dbg_ratelimited("%s: wrong RA length %d\n", + __func__, nh[optoff + 1]); + kfree_skb(skb); + return false; +} + +/* Jumbo payload */ + +static bool ipv6_hop_jumbo(struct sk_buff *skb, int optoff) +{ + const unsigned char *nh = skb_network_header(skb); + struct inet6_dev *idev = __in6_dev_get_safely(skb->dev); + struct net *net = ipv6_skb_net(skb); + u32 pkt_len; + + if (nh[optoff + 1] != 4 || (optoff & 3) != 2) { + net_dbg_ratelimited("%s: wrong jumbo opt length/alignment %d\n", + __func__, nh[optoff + 1]); + __IP6_INC_STATS(net, idev, IPSTATS_MIB_INHDRERRORS); + goto drop; + } + + pkt_len = ntohl(*(__be32 *)(nh + optoff + 2)); + if (pkt_len <= IPV6_MAXPLEN) { + __IP6_INC_STATS(net, idev, IPSTATS_MIB_INHDRERRORS); + icmpv6_param_prob(skb, ICMPV6_HDR_FIELD, optoff + 2); + return false; + } + if (ipv6_hdr(skb)->payload_len) { + __IP6_INC_STATS(net, idev, IPSTATS_MIB_INHDRERRORS); + icmpv6_param_prob(skb, ICMPV6_HDR_FIELD, optoff); + return false; + } + + if (pkt_len > skb->len - sizeof(struct ipv6hdr)) { + __IP6_INC_STATS(net, idev, IPSTATS_MIB_INTRUNCATEDPKTS); + goto drop; + } + + if (pskb_trim_rcsum(skb, pkt_len + sizeof(struct ipv6hdr))) + goto drop; + + IP6CB(skb)->flags |= IP6SKB_JUMBOGRAM; + return true; + +drop: + kfree_skb(skb); + return false; +} + +/* CALIPSO RFC 5570 */ + +static bool ipv6_hop_calipso(struct sk_buff *skb, int optoff) +{ + const unsigned char *nh = skb_network_header(skb); + + if (nh[optoff + 1] < 8) + goto drop; + + if (nh[optoff + 6] * 4 + 8 > nh[optoff + 1]) + goto drop; + + if (!calipso_validate(skb, nh + optoff)) + goto drop; + + return true; + +drop: + kfree_skb(skb); + return false; +} + +const struct tlvtype_proc tlvprochopopt_lst[] = { + { + .type = IPV6_TLV_ROUTERALERT, + .func = ipv6_hop_ra, + }, + { + .type = IPV6_TLV_JUMBO, + .func = ipv6_hop_jumbo, + }, + { + .type = IPV6_TLV_CALIPSO, + .func = ipv6_hop_calipso, + }, + { -1, } +}; From patchwork Thu Oct 3 21:57:59 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom Herbert X-Patchwork-Id: 1171426 X-Patchwork-Delegate: davem@davemloft.net Return-Path: X-Original-To: patchwork-incoming-netdev@ozlabs.org Delivered-To: patchwork-incoming-netdev@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=netdev-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=herbertland.com Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=herbertland-com.20150623.gappssmtp.com header.i=@herbertland-com.20150623.gappssmtp.com header.b="koRa1gLo"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 46kn2Q3xsnz9sNw for ; Fri, 4 Oct 2019 07:58:38 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1732409AbfJCV6h (ORCPT ); Thu, 3 Oct 2019 17:58:37 -0400 Received: from mail-pl1-f193.google.com ([209.85.214.193]:44608 "EHLO mail-pl1-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726002AbfJCV6h (ORCPT ); Thu, 3 Oct 2019 17:58:37 -0400 Received: by mail-pl1-f193.google.com with SMTP id q15so2123971pll.11 for ; Thu, 03 Oct 2019 14:58:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=herbertland-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=LGjEUz+K++VP0PksFceAgdMlJcK9XqS8nVRG/71Mh2o=; b=koRa1gLoWq4wF6MSLQKsmfaDQTj6224n9mYhOHFiSNAR7Yvyh5F67xixphmMR/oPj5 na9BCxDB0JlPzArnDxFfaYGDkykxTLxxWY4zBTljQFWTJzognG/eA+MlUMacBMpfba0P c0bWmE8Rb7j9R3VwYyKvNVPx88OjS1Z4dJ32LZzr7Wa7zz4pbP9AV+BYnj4Ns/U+dZec Q1mZZguF7jcqDjyhHK6I/NTwMBwtl1ygfi9l4/WuVlMjbPM43EsxO6lMNaiiZJjrxZCa DRnL1BMMEJGuvbS9yyA76+pzSkaV5IPaZ3YdKf2RBfluqwj//KTZQQ+lOkODdqrkfSKz ze9w== 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=LGjEUz+K++VP0PksFceAgdMlJcK9XqS8nVRG/71Mh2o=; b=bFnJ2gnNB+njR8xL/gQSEZ6oGlIj9B9JYdMpQqpS7Krg60hQ1bFRxBdSTtBAzx708d 7tr2cRVJ5mGxWDiwDv2yxq45sbJoGBwAmBlwIxhJM3YdTZyBYfOaFGEjm8/MbWSCsV1i mkpU0EklNxWQVm4ecKFsWnH2uwLvc4Qs4CfkBUo5ljW6IYxF5tMYbSxkd2h4oBEzsdB9 Ni31Q1EznfdxDF/3fnFENt7Ir1tKJekv5yvairRCUPcQ54cmmnWYkqyfc5WWwggFcJVa tvAzbxHd7nBGk1nuM78Mn8cjYnP3R9TxZnMxMUgFyDF1tBRWh+74TM+eEJOToqH2QC2L VzWA== X-Gm-Message-State: APjAAAUyGgUeCXu+Puz5zROEHi62y53QemuEaNDfIGLg4rIHk923XGbP jR6jIiskeWfF3HG0jpBU1lfIEg== X-Google-Smtp-Source: APXvYqxlPKVEmAGEImRW8LEs3DvFRc7GF9Qp12wQSc95B5GJzS+mAAylZb0I3qefxKhe6Au0owdyLA== X-Received: by 2002:a17:902:209:: with SMTP id 9mr12072468plc.1.1570139914739; Thu, 03 Oct 2019 14:58:34 -0700 (PDT) Received: from localhost.localdomain (c-73-202-182-113.hsd1.ca.comcast.net. [73.202.182.113]) by smtp.gmail.com with ESMTPSA id r18sm3889905pfc.3.2019.10.03.14.58.33 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Thu, 03 Oct 2019 14:58:34 -0700 (PDT) From: Tom Herbert To: davem@davemloft.net, netdev@vger.kernel.org Cc: Tom Herbert , Tom Herbert Subject: [PATCH v5 net-next 2/7] ipeh: Move generic EH functions to exthdrs_common.c Date: Thu, 3 Oct 2019 14:57:59 -0700 Message-Id: <1570139884-20183-3-git-send-email-tom@herbertland.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1570139884-20183-1-git-send-email-tom@herbertland.com> References: <1570139884-20183-1-git-send-email-tom@herbertland.com> Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org From: Tom Herbert Move generic functions in exthdrs.c to new exthdrs_common.c so that exthdrs.c only contains functions that are specific to IPv6 processing, and exthdrs_common.c contains functions that are generic. These functions include those that will be used with IPv4 extension headers. Generic extension header related functions are prefixed by ipeh_. Signed-off-by: Tom Herbert --- include/net/ipeh.h | 12 ++++ include/net/ipv6.h | 9 --- net/dccp/ipv6.c | 2 +- net/ipv6/Kconfig | 4 ++ net/ipv6/Makefile | 1 + net/ipv6/calipso.c | 6 +- net/ipv6/exthdrs.c | 138 -------------------------------------------- net/ipv6/exthdrs_common.c | 144 ++++++++++++++++++++++++++++++++++++++++++++++ net/ipv6/ipv6_sockglue.c | 2 +- net/ipv6/raw.c | 2 +- net/ipv6/tcp_ipv6.c | 2 +- net/ipv6/udp.c | 2 +- net/l2tp/l2tp_ip6.c | 2 +- net/sctp/ipv6.c | 2 +- 14 files changed, 171 insertions(+), 157 deletions(-) create mode 100644 net/ipv6/exthdrs_common.c diff --git a/include/net/ipeh.h b/include/net/ipeh.h index ec2d186..3b24831 100644 --- a/include/net/ipeh.h +++ b/include/net/ipeh.h @@ -19,4 +19,16 @@ struct tlvtype_proc { extern const struct tlvtype_proc tlvprocdestopt_lst[]; extern const struct tlvtype_proc tlvprochopopt_lst[]; +struct ipv6_txoptions; +struct ipv6_opt_hdr; + +struct ipv6_txoptions *ipeh_dup_options(struct sock *sk, + struct ipv6_txoptions *opt); +struct ipv6_txoptions *ipeh_renew_options(struct sock *sk, + struct ipv6_txoptions *opt, + int newtype, + struct ipv6_opt_hdr *newopt); +struct ipv6_txoptions *ipeh_fixup_options(struct ipv6_txoptions *opt_space, + struct ipv6_txoptions *opt); + #endif /* _NET_IPEH_H */ diff --git a/include/net/ipv6.h b/include/net/ipv6.h index 6fc584c..d34c36c 100644 --- a/include/net/ipv6.h +++ b/include/net/ipv6.h @@ -429,15 +429,6 @@ int ip6_ra_control(struct sock *sk, int sel); int ipv6_parse_hopopts(struct sk_buff *skb); -struct ipv6_txoptions *ipv6_dup_options(struct sock *sk, - struct ipv6_txoptions *opt); -struct ipv6_txoptions *ipv6_renew_options(struct sock *sk, - struct ipv6_txoptions *opt, - int newtype, - struct ipv6_opt_hdr *newopt); -struct ipv6_txoptions *ipv6_fixup_options(struct ipv6_txoptions *opt_space, - struct ipv6_txoptions *opt); - bool ipv6_opt_accepted(const struct sock *sk, const struct sk_buff *skb, const struct inet6_skb_parm *opt); struct ipv6_txoptions *ipv6_update_options(struct sock *sk, diff --git a/net/dccp/ipv6.c b/net/dccp/ipv6.c index 25aab67..b8843c1 100644 --- a/net/dccp/ipv6.c +++ b/net/dccp/ipv6.c @@ -515,7 +515,7 @@ static struct sock *dccp_v6_request_recv_sock(const struct sock *sk, if (!opt) opt = rcu_dereference(np->opt); if (opt) { - opt = ipv6_dup_options(newsk, opt); + opt = ipeh_dup_options(newsk, opt); RCU_INIT_POINTER(newnp->opt, opt); } inet_csk(newsk)->icsk_ext_hdr_len = 0; diff --git a/net/ipv6/Kconfig b/net/ipv6/Kconfig index ae1344e..700fcea 100644 --- a/net/ipv6/Kconfig +++ b/net/ipv6/Kconfig @@ -3,9 +3,13 @@ # IPv6 configuration # +config EXTHDRS + bool + # IPv6 as module will cause a CRASH if you try to unload it menuconfig IPV6 tristate "The IPv6 protocol" + select EXTHDRS default y ---help--- Support for IP version 6 (IPv6). diff --git a/net/ipv6/Makefile b/net/ipv6/Makefile index df3919b..0bcab81 100644 --- a/net/ipv6/Makefile +++ b/net/ipv6/Makefile @@ -44,6 +44,7 @@ obj-$(CONFIG_IPV6_SIT) += sit.o obj-$(CONFIG_IPV6_TUNNEL) += ip6_tunnel.o obj-$(CONFIG_IPV6_GRE) += ip6_gre.o obj-$(CONFIG_IPV6_FOU) += fou6.o +obj-$(CONFIG_EXTHDRS) += exthdrs_common.o obj-y += addrconf_core.o exthdrs_core.o ip6_checksum.o ip6_icmp.o obj-$(CONFIG_INET) += output_core.o protocol.o $(ipv6-offload) diff --git a/net/ipv6/calipso.c b/net/ipv6/calipso.c index 221c81f..9c84848 100644 --- a/net/ipv6/calipso.c +++ b/net/ipv6/calipso.c @@ -785,7 +785,7 @@ static int calipso_opt_update(struct sock *sk, struct ipv6_opt_hdr *hop) { struct ipv6_txoptions *old = txopt_get(inet6_sk(sk)), *txopts; - txopts = ipv6_renew_options(sk, old, IPV6_HOPOPTS, hop); + txopts = ipeh_renew_options(sk, old, IPV6_HOPOPTS, hop); txopt_put(old); if (IS_ERR(txopts)) return PTR_ERR(txopts); @@ -1207,7 +1207,7 @@ static int calipso_req_setattr(struct request_sock *req, if (IS_ERR(new)) return PTR_ERR(new); - txopts = ipv6_renew_options(sk, req_inet->ipv6_opt, IPV6_HOPOPTS, new); + txopts = ipeh_renew_options(sk, req_inet->ipv6_opt, IPV6_HOPOPTS, new); kfree(new); @@ -1244,7 +1244,7 @@ static void calipso_req_delattr(struct request_sock *req) if (calipso_opt_del(req_inet->ipv6_opt->hopopt, &new)) return; /* Nothing to do */ - txopts = ipv6_renew_options(sk, req_inet->ipv6_opt, IPV6_HOPOPTS, new); + txopts = ipeh_renew_options(sk, req_inet->ipv6_opt, IPV6_HOPOPTS, new); if (!IS_ERR(txopts)) { txopts = xchg(&req_inet->ipv6_opt, txopts); diff --git a/net/ipv6/exthdrs.c b/net/ipv6/exthdrs.c index 664491e..e12d3a5 100644 --- a/net/ipv6/exthdrs.c +++ b/net/ipv6/exthdrs.c @@ -784,144 +784,6 @@ void ipv6_push_frag_opts(struct sk_buff *skb, struct ipv6_txoptions *opt, u8 *pr } EXPORT_SYMBOL(ipv6_push_frag_opts); -struct ipv6_txoptions * -ipv6_dup_options(struct sock *sk, struct ipv6_txoptions *opt) -{ - struct ipv6_txoptions *opt2; - - opt2 = sock_kmalloc(sk, opt->tot_len, GFP_ATOMIC); - if (opt2) { - long dif = (char *)opt2 - (char *)opt; - memcpy(opt2, opt, opt->tot_len); - if (opt2->hopopt) - *((char **)&opt2->hopopt) += dif; - if (opt2->dst0opt) - *((char **)&opt2->dst0opt) += dif; - if (opt2->dst1opt) - *((char **)&opt2->dst1opt) += dif; - if (opt2->srcrt) - *((char **)&opt2->srcrt) += dif; - refcount_set(&opt2->refcnt, 1); - } - return opt2; -} -EXPORT_SYMBOL_GPL(ipv6_dup_options); - -static void ipv6_renew_option(int renewtype, - struct ipv6_opt_hdr **dest, - struct ipv6_opt_hdr *old, - struct ipv6_opt_hdr *new, - int newtype, char **p) -{ - struct ipv6_opt_hdr *src; - - src = (renewtype == newtype ? new : old); - if (!src) - return; - - memcpy(*p, src, ipv6_optlen(src)); - *dest = (struct ipv6_opt_hdr *)*p; - *p += CMSG_ALIGN(ipv6_optlen(*dest)); -} - -/** - * ipv6_renew_options - replace a specific ext hdr with a new one. - * - * @sk: sock from which to allocate memory - * @opt: original options - * @newtype: option type to replace in @opt - * @newopt: new option of type @newtype to replace (user-mem) - * @newoptlen: length of @newopt - * - * Returns a new set of options which is a copy of @opt with the - * option type @newtype replaced with @newopt. - * - * @opt may be NULL, in which case a new set of options is returned - * containing just @newopt. - * - * @newopt may be NULL, in which case the specified option type is - * not copied into the new set of options. - * - * The new set of options is allocated from the socket option memory - * buffer of @sk. - */ -struct ipv6_txoptions * -ipv6_renew_options(struct sock *sk, struct ipv6_txoptions *opt, - int newtype, struct ipv6_opt_hdr *newopt) -{ - int tot_len = 0; - char *p; - struct ipv6_txoptions *opt2; - - if (opt) { - if (newtype != IPV6_HOPOPTS && opt->hopopt) - tot_len += CMSG_ALIGN(ipv6_optlen(opt->hopopt)); - if (newtype != IPV6_RTHDRDSTOPTS && opt->dst0opt) - tot_len += CMSG_ALIGN(ipv6_optlen(opt->dst0opt)); - if (newtype != IPV6_RTHDR && opt->srcrt) - tot_len += CMSG_ALIGN(ipv6_optlen(opt->srcrt)); - if (newtype != IPV6_DSTOPTS && opt->dst1opt) - tot_len += CMSG_ALIGN(ipv6_optlen(opt->dst1opt)); - } - - if (newopt) - tot_len += CMSG_ALIGN(ipv6_optlen(newopt)); - - if (!tot_len) - return NULL; - - tot_len += sizeof(*opt2); - opt2 = sock_kmalloc(sk, tot_len, GFP_ATOMIC); - if (!opt2) - return ERR_PTR(-ENOBUFS); - - memset(opt2, 0, tot_len); - refcount_set(&opt2->refcnt, 1); - opt2->tot_len = tot_len; - p = (char *)(opt2 + 1); - - ipv6_renew_option(IPV6_HOPOPTS, &opt2->hopopt, - (opt ? opt->hopopt : NULL), - newopt, newtype, &p); - ipv6_renew_option(IPV6_RTHDRDSTOPTS, &opt2->dst0opt, - (opt ? opt->dst0opt : NULL), - newopt, newtype, &p); - ipv6_renew_option(IPV6_RTHDR, - (struct ipv6_opt_hdr **)&opt2->srcrt, - (opt ? (struct ipv6_opt_hdr *)opt->srcrt : NULL), - newopt, newtype, &p); - ipv6_renew_option(IPV6_DSTOPTS, &opt2->dst1opt, - (opt ? opt->dst1opt : NULL), - newopt, newtype, &p); - - opt2->opt_nflen = (opt2->hopopt ? ipv6_optlen(opt2->hopopt) : 0) + - (opt2->dst0opt ? ipv6_optlen(opt2->dst0opt) : 0) + - (opt2->srcrt ? ipv6_optlen(opt2->srcrt) : 0); - opt2->opt_flen = (opt2->dst1opt ? ipv6_optlen(opt2->dst1opt) : 0); - - return opt2; -} - -struct ipv6_txoptions *ipv6_fixup_options(struct ipv6_txoptions *opt_space, - struct ipv6_txoptions *opt) -{ - /* - * ignore the dest before srcrt unless srcrt is being included. - * --yoshfuji - */ - if (opt && opt->dst0opt && !opt->srcrt) { - if (opt_space != opt) { - memcpy(opt_space, opt, sizeof(*opt_space)); - opt = opt_space; - } - opt->opt_nflen -= ipv6_optlen(opt->dst0opt); - opt->dst0opt = NULL; - } - - return opt; -} -EXPORT_SYMBOL_GPL(ipv6_fixup_options); - /** * fl6_update_dst - update flowi destination address with info given * by srcrt option, if any. diff --git a/net/ipv6/exthdrs_common.c b/net/ipv6/exthdrs_common.c new file mode 100644 index 0000000..2c68184 --- /dev/null +++ b/net/ipv6/exthdrs_common.c @@ -0,0 +1,144 @@ +// SPDX-License-Identifier: GPL-2.0 + +/* Extension header and TLV library code that is not specific to IPv6. */ +#include +#include + +struct ipv6_txoptions * +ipeh_dup_options(struct sock *sk, struct ipv6_txoptions *opt) +{ + struct ipv6_txoptions *opt2; + + opt2 = sock_kmalloc(sk, opt->tot_len, GFP_ATOMIC); + if (opt2) { + long dif = (char *)opt2 - (char *)opt; + + memcpy(opt2, opt, opt->tot_len); + if (opt2->hopopt) + *((char **)&opt2->hopopt) += dif; + if (opt2->dst0opt) + *((char **)&opt2->dst0opt) += dif; + if (opt2->dst1opt) + *((char **)&opt2->dst1opt) += dif; + if (opt2->srcrt) + *((char **)&opt2->srcrt) += dif; + refcount_set(&opt2->refcnt, 1); + } + return opt2; +} +EXPORT_SYMBOL_GPL(ipeh_dup_options); + +static void ipeh_renew_option(int renewtype, + struct ipv6_opt_hdr **dest, + struct ipv6_opt_hdr *old, + struct ipv6_opt_hdr *new, + int newtype, char **p) +{ + struct ipv6_opt_hdr *src; + + src = (renewtype == newtype ? new : old); + if (!src) + return; + + memcpy(*p, src, ipv6_optlen(src)); + *dest = (struct ipv6_opt_hdr *)*p; + *p += CMSG_ALIGN(ipv6_optlen(*dest)); +} + +/** + * ipeh_renew_options - replace a specific ext hdr with a new one. + * + * @sk: sock from which to allocate memory + * @opt: original options + * @newtype: option type to replace in @opt + * @newopt: new option of type @newtype to replace (user-mem) + * @newoptlen: length of @newopt + * + * Returns a new set of options which is a copy of @opt with the + * option type @newtype replaced with @newopt. + * + * @opt may be NULL, in which case a new set of options is returned + * containing just @newopt. + * + * @newopt may be NULL, in which case the specified option type is + * not copied into the new set of options. + * + * The new set of options is allocated from the socket option memory + * buffer of @sk. + */ +struct ipv6_txoptions * +ipeh_renew_options(struct sock *sk, struct ipv6_txoptions *opt, + int newtype, struct ipv6_opt_hdr *newopt) +{ + int tot_len = 0; + char *p; + struct ipv6_txoptions *opt2; + + if (opt) { + if (newtype != IPV6_HOPOPTS && opt->hopopt) + tot_len += CMSG_ALIGN(ipv6_optlen(opt->hopopt)); + if (newtype != IPV6_RTHDRDSTOPTS && opt->dst0opt) + tot_len += CMSG_ALIGN(ipv6_optlen(opt->dst0opt)); + if (newtype != IPV6_RTHDR && opt->srcrt) + tot_len += CMSG_ALIGN(ipv6_optlen(opt->srcrt)); + if (newtype != IPV6_DSTOPTS && opt->dst1opt) + tot_len += CMSG_ALIGN(ipv6_optlen(opt->dst1opt)); + } + + if (newopt) + tot_len += CMSG_ALIGN(ipv6_optlen(newopt)); + + if (!tot_len) + return NULL; + + tot_len += sizeof(*opt2); + opt2 = sock_kmalloc(sk, tot_len, GFP_ATOMIC); + if (!opt2) + return ERR_PTR(-ENOBUFS); + + memset(opt2, 0, tot_len); + refcount_set(&opt2->refcnt, 1); + opt2->tot_len = tot_len; + p = (char *)(opt2 + 1); + + ipeh_renew_option(IPV6_HOPOPTS, &opt2->hopopt, + (opt ? opt->hopopt : NULL), + newopt, newtype, &p); + ipeh_renew_option(IPV6_RTHDRDSTOPTS, &opt2->dst0opt, + (opt ? opt->dst0opt : NULL), + newopt, newtype, &p); + ipeh_renew_option(IPV6_RTHDR, + (struct ipv6_opt_hdr **)&opt2->srcrt, + (opt ? (struct ipv6_opt_hdr *)opt->srcrt : NULL), + newopt, newtype, &p); + ipeh_renew_option(IPV6_DSTOPTS, &opt2->dst1opt, + (opt ? opt->dst1opt : NULL), + newopt, newtype, &p); + + opt2->opt_nflen = (opt2->hopopt ? ipv6_optlen(opt2->hopopt) : 0) + + (opt2->dst0opt ? ipv6_optlen(opt2->dst0opt) : 0) + + (opt2->srcrt ? ipv6_optlen(opt2->srcrt) : 0); + opt2->opt_flen = (opt2->dst1opt ? ipv6_optlen(opt2->dst1opt) : 0); + + return opt2; +} +EXPORT_SYMBOL(ipeh_renew_options); + +struct ipv6_txoptions *ipeh_fixup_options(struct ipv6_txoptions *opt_space, + struct ipv6_txoptions *opt) +{ + /* ignore the dest before srcrt unless srcrt is being included. + * --yoshfuji + */ + if (opt && opt->dst0opt && !opt->srcrt) { + if (opt_space != opt) { + memcpy(opt_space, opt, sizeof(*opt_space)); + opt = opt_space; + } + opt->opt_nflen -= ipv6_optlen(opt->dst0opt); + opt->dst0opt = NULL; + } + + return opt; +} +EXPORT_SYMBOL_GPL(ipeh_fixup_options); diff --git a/net/ipv6/ipv6_sockglue.c b/net/ipv6/ipv6_sockglue.c index 264c292..8755ecc 100644 --- a/net/ipv6/ipv6_sockglue.c +++ b/net/ipv6/ipv6_sockglue.c @@ -427,7 +427,7 @@ static int do_ipv6_setsockopt(struct sock *sk, int level, int optname, opt = rcu_dereference_protected(np->opt, lockdep_sock_is_held(sk)); - opt = ipv6_renew_options(sk, opt, optname, new); + opt = ipeh_renew_options(sk, opt, optname, new); kfree(new); if (IS_ERR(opt)) { retv = PTR_ERR(opt); diff --git a/net/ipv6/raw.c b/net/ipv6/raw.c index 6e1888e..ace11b7 100644 --- a/net/ipv6/raw.c +++ b/net/ipv6/raw.c @@ -889,7 +889,7 @@ static int rawv6_sendmsg(struct sock *sk, struct msghdr *msg, size_t len) } if (flowlabel) opt = fl6_merge_options(&opt_space, flowlabel, opt); - opt = ipv6_fixup_options(&opt_space, opt); + opt = ipeh_fixup_options(&opt_space, opt); fl6.flowi6_proto = proto; fl6.flowi6_mark = ipc6.sockc.mark; diff --git a/net/ipv6/tcp_ipv6.c b/net/ipv6/tcp_ipv6.c index e3d9f45..9c761c7 100644 --- a/net/ipv6/tcp_ipv6.c +++ b/net/ipv6/tcp_ipv6.c @@ -1249,7 +1249,7 @@ static struct sock *tcp_v6_syn_recv_sock(const struct sock *sk, struct sk_buff * if (!opt) opt = rcu_dereference(np->opt); if (opt) { - opt = ipv6_dup_options(newsk, opt); + opt = ipeh_dup_options(newsk, opt); RCU_INIT_POINTER(newnp->opt, opt); } inet_csk(newsk)->icsk_ext_hdr_len = 0; diff --git a/net/ipv6/udp.c b/net/ipv6/udp.c index aae4938..a1fbd47 100644 --- a/net/ipv6/udp.c +++ b/net/ipv6/udp.c @@ -1386,7 +1386,7 @@ int udpv6_sendmsg(struct sock *sk, struct msghdr *msg, size_t len) } if (flowlabel) opt = fl6_merge_options(&opt_space, flowlabel, opt); - opt = ipv6_fixup_options(&opt_space, opt); + opt = ipeh_fixup_options(&opt_space, opt); ipc6.opt = opt; fl6.flowi6_proto = sk->sk_protocol; diff --git a/net/l2tp/l2tp_ip6.c b/net/l2tp/l2tp_ip6.c index 687e23a..bc4925c 100644 --- a/net/l2tp/l2tp_ip6.c +++ b/net/l2tp/l2tp_ip6.c @@ -590,7 +590,7 @@ static int l2tp_ip6_sendmsg(struct sock *sk, struct msghdr *msg, size_t len) } if (flowlabel) opt = fl6_merge_options(&opt_space, flowlabel, opt); - opt = ipv6_fixup_options(&opt_space, opt); + opt = ipeh_fixup_options(&opt_space, opt); ipc6.opt = opt; fl6.flowi6_proto = sk->sk_protocol; diff --git a/net/sctp/ipv6.c b/net/sctp/ipv6.c index dd860fe..39d0eec 100644 --- a/net/sctp/ipv6.c +++ b/net/sctp/ipv6.c @@ -441,7 +441,7 @@ static void sctp_v6_copy_ip_options(struct sock *sk, struct sock *newsk) rcu_read_lock(); opt = rcu_dereference(np->opt); if (opt) { - opt = ipv6_dup_options(newsk, opt); + opt = ipeh_dup_options(newsk, opt); if (!opt) pr_err("%s: Failed to copy ip options\n", __func__); } From patchwork Thu Oct 3 21:58:00 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom Herbert X-Patchwork-Id: 1171427 X-Patchwork-Delegate: davem@davemloft.net Return-Path: X-Original-To: patchwork-incoming-netdev@ozlabs.org Delivered-To: patchwork-incoming-netdev@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=netdev-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=herbertland.com Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=herbertland-com.20150623.gappssmtp.com header.i=@herbertland-com.20150623.gappssmtp.com header.b="T8C1nal4"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 46kn2S39Kjz9sNw for ; Fri, 4 Oct 2019 07:58:40 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1732831AbfJCV6j (ORCPT ); Thu, 3 Oct 2019 17:58:39 -0400 Received: from mail-pf1-f194.google.com ([209.85.210.194]:43574 "EHLO mail-pf1-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726002AbfJCV6i (ORCPT ); Thu, 3 Oct 2019 17:58:38 -0400 Received: by mail-pf1-f194.google.com with SMTP id a2so2574046pfo.10 for ; Thu, 03 Oct 2019 14:58:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=herbertland-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=Qg3ELsCDd2TlkD3X4Kus7eMR+Pz9DIIAqMi2dpjbmxg=; b=T8C1nal4TrvV34iV3O4fa3XFwZ8dawbI0HaUFbEknYCeZQlc5TwfwmjbwDFNZOFco+ L9PbhhDdgNpIpOyTiELTOzf/6N7rHXXJ6L2GklZ7wIOJf8fpyl+FZiOrLeYL2QBjqudY wETlMxG9Lydq/+nfE9vNyX8DBZdMJ6LAKh+BHqJd3NGvSXwA2DM3MwCZSV2ZaqtlkM3p 4fBUu6WD0QPn5PzzrWVx/UniI9crzwWIZOROc9pqwcRpRcV/py+dZqXQJNtl4nye1jes KWFjbRRpBWY3v0ps3A/tpYubRAyxEuspYvYD7QjQI++6o9bekKCg2MMYpuP3qHv/nI0s s25A== 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=Qg3ELsCDd2TlkD3X4Kus7eMR+Pz9DIIAqMi2dpjbmxg=; b=W6Wd3RGNjjlYAcROZT4Ylq3awGZKkCDYo4IdKNDOLll3Ambpih+woD3eVGaOFgENoY Aw3Jw6s8VC4/qicuUSt0iUVkY+/T6WCjzvpIxcwADxSCexKf1N9GWZGKBHnsz26P/NdG 4K8QZrw7JFwrHe2RF2w7H0tby/LkveAmZVcHrebM8faHYX/F0zldQy3USR2NCeEebc4y zi9DxGNYtvItCG9r6YyPBYAT7bMEX2XFRdQ3ZpF/9T1adzrQKcrEBY9LxN7MTPYEhcL8 ruj2rB2WcVMcZd3Pr/1C8mTQ9Q5d6O8XDiXEtRhkTpFfh5yzdle8goyGZzRe1zY3y7Bt lZjA== X-Gm-Message-State: APjAAAXFpvm3dqox5F+o0pXJURIC6u94KrAaYTp6UZMepd25Dt5/VNXO dGH7IgDA/1CAP7RNRKoK2m4rWmRWP5zJNQ== X-Google-Smtp-Source: APXvYqyGwUKT0qjxy+KvnfajRtJyUZEo/uuvAvxWNCL+T2x2F4JYWMFEszPo0Wo9oL4F4walcPWatw== X-Received: by 2002:aa7:928b:: with SMTP id j11mr13583049pfa.237.1570139917389; Thu, 03 Oct 2019 14:58:37 -0700 (PDT) Received: from localhost.localdomain (c-73-202-182-113.hsd1.ca.comcast.net. [73.202.182.113]) by smtp.gmail.com with ESMTPSA id r18sm3889905pfc.3.2019.10.03.14.58.36 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Thu, 03 Oct 2019 14:58:36 -0700 (PDT) From: Tom Herbert To: davem@davemloft.net, netdev@vger.kernel.org Cc: Tom Herbert , Tom Herbert Subject: [PATCH v5 net-next 3/7] ipeh: Generic TLV parser Date: Thu, 3 Oct 2019 14:58:00 -0700 Message-Id: <1570139884-20183-4-git-send-email-tom@herbertland.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1570139884-20183-1-git-send-email-tom@herbertland.com> References: <1570139884-20183-1-git-send-email-tom@herbertland.com> Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org From: Tom Herbert Create a generic TLV parser. This will be used with various extension headers that carry options including Destination, Hop-by-Hop, Segment Routing TLVs, and other cases of simple stateless parsing. Signed-off-by: Tom Herbert --- include/net/ipeh.h | 25 ++++++++ net/ipv6/exthdrs.c | 159 +++++++++++----------------------------------- net/ipv6/exthdrs_common.c | 114 +++++++++++++++++++++++++++++++++ 3 files changed, 177 insertions(+), 121 deletions(-) diff --git a/include/net/ipeh.h b/include/net/ipeh.h index 3b24831..c1aa7b6 100644 --- a/include/net/ipeh.h +++ b/include/net/ipeh.h @@ -31,4 +31,29 @@ struct ipv6_txoptions *ipeh_renew_options(struct sock *sk, struct ipv6_txoptions *ipeh_fixup_options(struct ipv6_txoptions *opt_space, struct ipv6_txoptions *opt); +/* Generic extension header TLV parser */ + +enum ipeh_parse_errors { + IPEH_PARSE_ERR_PAD1, /* Excessive PAD1 */ + IPEH_PARSE_ERR_PADN, /* Excessive PADN */ + IPEH_PARSE_ERR_PADNZ, /* Non-zero padding data */ + IPEH_PARSE_ERR_EH_TOOBIG, /* Length of EH exceeds limit */ + IPEH_PARSE_ERR_OPT_TOOBIG, /* Option size exceeds limit */ + IPEH_PARSE_ERR_OPT_TOOMANY, /* Option count exceeds limit */ + IPEH_PARSE_ERR_OPT_UNK_DISALW, /* Unknown option disallowed */ + IPEH_PARSE_ERR_OPT_UNK, /* Unknown option */ +}; + +/* The generic TLV parser assumes that the type value of PAD1 is 0, and PADN + * is 1. This is true for Destination, Hop-by-Hop and current definition + * of Segment Routing TLVs. + */ +#define IPEH_TLV_PAD1 0 +#define IPEH_TLV_PADN 1 + +bool ipeh_parse_tlv(const struct tlvtype_proc *procs, struct sk_buff *skb, + int max_count, int off, int len, + bool (*parse_error)(struct sk_buff *skb, + int off, enum ipeh_parse_errors error)); + #endif /* _NET_IPEH_H */ diff --git a/net/ipv6/exthdrs.c b/net/ipv6/exthdrs.c index e12d3a5..939d27c 100644 --- a/net/ipv6/exthdrs.c +++ b/net/ipv6/exthdrs.c @@ -54,135 +54,50 @@ Generic functions *********************/ -/* An unknown option is detected, decide what to do */ - -static bool ip6_tlvopt_unknown(struct sk_buff *skb, int optoff, - bool disallow_unknowns) +/* Handle parse errors from ipeh generic TLV parser */ +static bool ipv6_parse_error(struct sk_buff *skb, int off, + enum ipeh_parse_errors error) { - if (disallow_unknowns) { - /* If unknown TLVs are disallowed by configuration - * then always silently drop packet. Note this also - * means no ICMP parameter problem is sent which - * could be a good property to mitigate a reflection DOS - * attack. - */ - - goto drop; - } - - switch ((skb_network_header(skb)[optoff] & 0xC0) >> 6) { - case 0: /* ignore */ - return true; - - case 1: /* drop packet */ - break; - - case 3: /* Send ICMP if not a multicast address and drop packet */ - /* Actually, it is redundant check. icmp_send - will recheck in any case. - */ - if (ipv6_addr_is_multicast(&ipv6_hdr(skb)->daddr)) + switch (error) { + case IPEH_PARSE_ERR_OPT_UNK_DISALW: + /* Disallow unknown skip */ + if (((skb_network_header(skb)[off] & 0xC0) >> 6) == 0) { + /* Silent drop */ break; + } /* fall through */ - case 2: /* send ICMP PARM PROB regardless and drop packet */ - icmpv6_param_prob(skb, ICMPV6_UNK_OPTION, optoff); - return false; - } - -drop: - kfree_skb(skb); - return false; -} + case IPEH_PARSE_ERR_OPT_UNK: + switch ((skb_network_header(skb)[off] & 0xC0) >> 6) { + case 0: /* ignore */ + return true; -/* Parse tlv encoded option header (hop-by-hop or destination) */ - -static bool ip6_parse_tlv(const struct tlvtype_proc *procs, - struct sk_buff *skb, - int max_count) -{ - int len = (skb_transport_header(skb)[1] + 1) << 3; - const unsigned char *nh = skb_network_header(skb); - int off = skb_network_header_len(skb); - const struct tlvtype_proc *curr; - bool disallow_unknowns = false; - int tlv_count = 0; - int padlen = 0; - - if (unlikely(max_count < 0)) { - disallow_unknowns = true; - max_count = -max_count; - } - - if (skb_transport_offset(skb) + len > skb_headlen(skb)) - goto bad; - - off += 2; - len -= 2; - - while (len > 0) { - int optlen = nh[off + 1] + 2; - int i; - - switch (nh[off]) { - case IPV6_TLV_PAD1: - optlen = 1; - padlen++; - if (padlen > 7) - goto bad; + case 1: /* drop packet */ break; - case IPV6_TLV_PADN: - /* RFC 2460 states that the purpose of PadN is - * to align the containing header to multiples - * of 8. 7 is therefore the highest valid value. - * See also RFC 4942, Section 2.1.9.5. - */ - padlen += optlen; - if (padlen > 7) - goto bad; - /* RFC 4942 recommends receiving hosts to - * actively check PadN payload to contain - * only zeroes. + case 3: /* Send ICMP if not a multicast address and drop packet + * + * Actually, it is redundant check. icmp_send + * will recheck in any case. */ - for (i = 2; i < optlen; i++) { - if (nh[off + i] != 0) - goto bad; - } - break; + if (ipv6_addr_is_multicast(&ipv6_hdr(skb)->daddr)) + break; - default: /* Other TLV code so scan list */ - if (optlen > len) - goto bad; - - tlv_count++; - if (tlv_count > max_count) - goto bad; - - for (curr = procs; curr->type >= 0; curr++) { - if (curr->type == nh[off]) { - /* type specific length/alignment - checks will be performed in the - func(). */ - if (curr->func(skb, off) == false) - return false; - break; - } - } - if (curr->type < 0 && - !ip6_tlvopt_unknown(skb, off, disallow_unknowns)) - return false; - - padlen = 0; + /* fall through */ + case 2: /* send ICMP PARM PROB regardless and drop packet */ + icmpv6_send(skb, ICMPV6_PARAMPROB, + ICMPV6_UNK_OPTION, off); break; } - off += optlen; - len -= optlen; + break; + default: + break; } - if (len == 0) - return true; -bad: - kfree_skb(skb); + /* Will be dropping packet */ + + __IP6_INC_STATS(dev_net(skb->dev), __in6_dev_get(skb->dev), + IPSTATS_MIB_INHDRERRORS); + return false; } @@ -216,8 +131,9 @@ static int ipv6_destopt_rcv(struct sk_buff *skb) dstbuf = opt->dst1; #endif - if (ip6_parse_tlv(tlvprocdestopt_lst, skb, - init_net.ipv6.sysctl.max_dst_opts_cnt)) { + if (ipeh_parse_tlv(tlvprocdestopt_lst, skb, + init_net.ipv6.sysctl.max_dst_opts_cnt, + 2, extlen - 2, ipv6_parse_error)) { skb->transport_header += extlen; opt = IP6CB(skb); #if IS_ENABLED(CONFIG_IPV6_MIP6) @@ -639,8 +555,9 @@ int ipv6_parse_hopopts(struct sk_buff *skb) goto fail_and_free; opt->flags |= IP6SKB_HOPBYHOP; - if (ip6_parse_tlv(tlvprochopopt_lst, skb, - init_net.ipv6.sysctl.max_hbh_opts_cnt)) { + if (ipeh_parse_tlv(tlvprochopopt_lst, skb, + init_net.ipv6.sysctl.max_hbh_opts_cnt, + 2, extlen - 2, ipv6_parse_error)) { skb->transport_header += extlen; opt = IP6CB(skb); opt->nhoff = sizeof(struct ipv6hdr); diff --git a/net/ipv6/exthdrs_common.c b/net/ipv6/exthdrs_common.c index 2c68184..99a0911 100644 --- a/net/ipv6/exthdrs_common.c +++ b/net/ipv6/exthdrs_common.c @@ -142,3 +142,117 @@ struct ipv6_txoptions *ipeh_fixup_options(struct ipv6_txoptions *opt_space, return opt; } EXPORT_SYMBOL_GPL(ipeh_fixup_options); + +/* Generic extension header TLV parser + * + * Arguments: + * - skb_transport_header points to the extension header containing options + * - off is offset from skb_transport_header where first TLV is + * - len is length of TLV block + */ +bool ipeh_parse_tlv(const struct tlvtype_proc *procs, struct sk_buff *skb, + int max_count, int off, int len, + bool (*parse_error)(struct sk_buff *skb, + int off, enum ipeh_parse_errors error)) +{ + const unsigned char *nh = skb_network_header(skb); + const struct tlvtype_proc *curr; + bool disallow_unknowns = false; + int tlv_count = 0; + int padlen = 0; + + if (unlikely(max_count < 0)) { + disallow_unknowns = true; + max_count = -max_count; + } + + if (skb_transport_offset(skb) + off + len > skb_headlen(skb)) { + if (!parse_error(skb, skb_transport_offset(skb), + IPEH_PARSE_ERR_EH_TOOBIG)) + goto bad; + + len = skb_headlen(skb) - skb_transport_offset(skb) - off; + } + + /* ops function based offset on network header */ + off += skb_network_header_len(skb); + + while (len > 0) { + int optlen = nh[off + 1] + 2; + int i; + + switch (nh[off]) { + case IPEH_TLV_PAD1: + optlen = 1; + padlen++; + if (padlen > 7 && + !parse_error(skb, off, IPEH_PARSE_ERR_PAD1)) + goto bad; + + break; + + case IPEH_TLV_PADN: + /* RFC 2460 states that the purpose of PadN is + * to align the containing header to multiples + * of 8. 7 is therefore the highest valid value. + * See also RFC 4942, Section 2.1.9.5. + */ + padlen += optlen; + if (padlen > 7 && + !parse_error(skb, off, IPEH_PARSE_ERR_PADN)) + goto bad; + + /* RFC 4942 recommends receiving hosts to + * actively check PadN payload to contain + * only zeroes. + */ + for (i = 2; i < optlen; i++) { + if (nh[off + i] != 0 && + !parse_error(skb, off + i, + IPEH_PARSE_ERR_PADNZ)) + goto bad; + } + break; + + default: /* Other TLV code so scan list */ + if (optlen > len && + !parse_error(skb, off, IPEH_PARSE_ERR_OPT_TOOBIG)) + goto bad; + + tlv_count++; + if (tlv_count > max_count && + parse_error(skb, off, IPEH_PARSE_ERR_OPT_TOOMANY)) + goto bad; + + for (curr = procs; curr->type >= 0; curr++) { + if (curr->type == nh[off]) { + /* type specific length/alignment + * checks will be performed in the + * func(). + */ + if (curr->func(skb, off) == false) + return false; + break; + } + } + if (curr->type < 0 && + !parse_error(skb, off, + disallow_unknowns ? + IPEH_PARSE_ERR_OPT_UNK_DISALW : + IPEH_PARSE_ERR_OPT_UNK)) + goto bad; + + padlen = 0; + break; + } + off += optlen; + len -= optlen; + } + + if (len == 0) + return true; +bad: + kfree_skb(skb); + return false; +} +EXPORT_SYMBOL(ipeh_parse_tlv); From patchwork Thu Oct 3 21:58:01 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom Herbert X-Patchwork-Id: 1171428 X-Patchwork-Delegate: davem@davemloft.net Return-Path: X-Original-To: patchwork-incoming-netdev@ozlabs.org Delivered-To: patchwork-incoming-netdev@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=netdev-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=herbertland.com Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=herbertland-com.20150623.gappssmtp.com header.i=@herbertland-com.20150623.gappssmtp.com header.b="C/8QZWbQ"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 46kn2V481Gz9sNw for ; Fri, 4 Oct 2019 07:58:42 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1733079AbfJCV6l (ORCPT ); Thu, 3 Oct 2019 17:58:41 -0400 Received: from mail-pf1-f194.google.com ([209.85.210.194]:35939 "EHLO mail-pf1-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726002AbfJCV6k (ORCPT ); Thu, 3 Oct 2019 17:58:40 -0400 Received: by mail-pf1-f194.google.com with SMTP id y22so2605641pfr.3 for ; Thu, 03 Oct 2019 14:58:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=herbertland-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=Tq9AauF2LOPybyzaNFtTFk0EFI1sy3r+evVGRCFCNI4=; b=C/8QZWbQ6HxlyTjRPuFZV2kHr7kqLVPlqoF8m0lw5Sq9W7neyn6f3txPwUuXjdaWWf nwVXOdN/J0NHOMLTAtqID2UaqrdQrZ2w1SO2WsKiSwKzV8xqZMO2x85Jo8DZJGUB9py3 uR+F3RshsjzwKY0wH+HU9LztDNod+/TSnvqnLs4NUso1scoDdfjvBTQvvbi4ki0sjdX9 k+9Ll2azAF/iMj3vePJN86S/QcF4kvuQgBbpxd0SGjhJz9dI9PBEnz2h4Zm0lVZbhQLH Wb1v+nDkNA4cjUhLcXI/Qs3PE7cxgk1yibvM1jJd3jHVNk8SDkEH42Kf9Z55Xq0lXgn4 OlRw== 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=Tq9AauF2LOPybyzaNFtTFk0EFI1sy3r+evVGRCFCNI4=; b=sWrvc7WV/BYOshkxUJeQEEcoPL9np8SLlD5tMqd6P3dqJpSlfdWln9iVY/38C1S33H SnGSslaqfhFR3jLPhV4JoUzrdQDxYUT9zy7MY4osCF7W5QrOfYhj7KcyO9oWKtkhhm/t L/SjTcPoUxEdsIuzEmydUYeC/7kyTYv3lotV8yVVLKQ3YVEwtFsa9fgnf4YPpDeKEYZG CA1O4wp0hDc/OcyzW0U86yzNVwVGC9LQlmIHSWwB4HPqeBbiCx9UNtCSRWB2DT6l3QUj VRFF7aqRyMnYdaZGphKM+EecRP15rhBsBm4RSq1gR4qEvVqlzHO4LoPcWdTcYOQr/1CQ JUcQ== X-Gm-Message-State: APjAAAXU3UH6kYrgFhIfZL0cljj4v7wfAGREcdvUOu/tewChaIa4qQU0 PVacOsN9Kts/GabIbUKaG5gDDA== X-Google-Smtp-Source: APXvYqzKShh9zjKFFk/DV/7Z/xeWy50jFPnlWUHTtydPerorU0ILvNBLKEsW5U3U2rSK7DKt89eEUA== X-Received: by 2002:aa7:9a92:: with SMTP id w18mr13849989pfi.244.1570139919860; Thu, 03 Oct 2019 14:58:39 -0700 (PDT) Received: from localhost.localdomain (c-73-202-182-113.hsd1.ca.comcast.net. [73.202.182.113]) by smtp.gmail.com with ESMTPSA id r18sm3889905pfc.3.2019.10.03.14.58.38 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Thu, 03 Oct 2019 14:58:39 -0700 (PDT) From: Tom Herbert To: davem@davemloft.net, netdev@vger.kernel.org Cc: Tom Herbert , Tom Herbert Subject: [PATCH v5 net-next 4/7] ip6tlvs: Registration of TLV handlers and parameters Date: Thu, 3 Oct 2019 14:58:01 -0700 Message-Id: <1570139884-20183-5-git-send-email-tom@herbertland.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1570139884-20183-1-git-send-email-tom@herbertland.com> References: <1570139884-20183-1-git-send-email-tom@herbertland.com> Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org From: Tom Herbert Create a single TLV parameter table that holds meta information for IPv6 Hop-by-Hop and Destination TLVs. The data structure is composed of a 256 element array of u8's (one entry for each TLV type to allow O(1) lookup). Each entry provides an offset into an array of TLV proc data structures which follows the array of u8s. The TLV proc data structure contains parameters and handler functions for receiving and transmitting TLVs. The zeroth element in the TLV proc array provides default parameters for TLVs. A class attribute indicates the type of extension header in which the TLV may be used (e.g. Hop-by-Hop options, Destination options, or Destination options before the routing header). Functions are defined to manipulate entries in the TLV parameter table. * tlv_{set|unset}_proc set a TLV proc entry (ops and parameters) * tlv_{set|unset}_params set parameters only Receive TLV lookup and processing is modified to be a lookup in the TLV parameter table. An init table containing parameters for TLVs supported by the kernel is used to initialize the TLV table. Signed-off-by: Tom Herbert Reviewed-by: Simon Horman --- include/net/ipeh.h | 107 ++++++++++++++++-- include/net/ipv6.h | 3 + include/uapi/linux/ipeh.h | 16 +++ net/ipv6/exthdrs.c | 14 ++- net/ipv6/exthdrs_common.c | 271 +++++++++++++++++++++++++++++++++++++++++---- net/ipv6/exthdrs_options.c | 63 +++++++---- 6 files changed, 421 insertions(+), 53 deletions(-) create mode 100644 include/uapi/linux/ipeh.h diff --git a/include/net/ipeh.h b/include/net/ipeh.h index c1aa7b6..aaa2910 100644 --- a/include/net/ipeh.h +++ b/include/net/ipeh.h @@ -11,13 +11,105 @@ * and false, if it failed. * It MUST NOT touch skb->h. */ -struct tlvtype_proc { - int type; - bool (*func)(struct sk_buff *skb, int offset); +struct tlv_ops { + bool (*func)(unsigned int class, struct sk_buff *skb, int offset); }; -extern const struct tlvtype_proc tlvprocdestopt_lst[]; -extern const struct tlvtype_proc tlvprochopopt_lst[]; +struct tlv_rx_params { + unsigned char class : 4; +}; + +struct tlv_tx_params { +}; + +struct tlv_params { + struct tlv_rx_params r; + struct tlv_tx_params t; +}; + +struct tlv_proc { + struct tlv_ops ops; + struct tlv_params params; +}; + +struct tlv_type { + struct tlv_proc proc; +}; + +struct tlv_proc_init { + int type; + struct tlv_proc proc; +}; + +struct tlv_param_table_data { + unsigned char entries[256]; + unsigned char count; + struct rcu_head rcu; + struct tlv_type types[0]; +}; + +struct tlv_param_table { + struct tlv_param_table_data __rcu *data; +}; + +extern struct tlv_param_table ipv6_tlv_param_table; + +int __ipeh_tlv_set(struct tlv_param_table *tlv_param_table, + unsigned char type, const struct tlv_params *params, + const struct tlv_ops *ops); + +static inline int ipeh_tlv_set_params(struct tlv_param_table *tlv_param_table, + unsigned char type, + const struct tlv_params *params) +{ + return __ipeh_tlv_set(tlv_param_table, type, params, NULL); +} + +static inline int ipeh_tlv_set_proc(struct tlv_param_table *tlv_param_table, + unsigned char type, + const struct tlv_proc *proc) +{ + return __ipeh_tlv_set(tlv_param_table, type, + &proc->params, &proc->ops); +} + +int __ipeh_tlv_unset(struct tlv_param_table *tlv_param_table, + unsigned char type, bool params_only); + +static inline int ipeh_tlv_unset_params(struct tlv_param_table *tlv_param_table, + unsigned char type) +{ + return __ipeh_tlv_unset(tlv_param_table, type, true); +} + +static inline int ipeh_tlv_unset_proc(struct tlv_param_table *tlv_param_table, + unsigned char type) +{ + return __ipeh_tlv_unset(tlv_param_table, type, false); +} + +/* ipeh_tlv_get_proc_by_type assumes rcu_read_lock is held */ +static inline struct tlv_proc *ipeh_tlv_get_proc_by_type( + struct tlv_param_table *tlv_param_table, unsigned char type) +{ + struct tlv_param_table_data *tpt = + rcu_dereference(tlv_param_table->data); + + return &tpt->types[tpt->entries[type]].proc; +} + +/* ipeh_tlv_get_proc assumes rcu_read_lock is held */ +static inline struct tlv_proc *ipeh_tlv_get_proc( + struct tlv_param_table *tlv_param_table, + const __u8 *tlv) +{ + return ipeh_tlv_get_proc_by_type(tlv_param_table, tlv[0]); +} + +int ipeh_exthdrs_init(struct tlv_param_table *tlv_param_table, + const struct tlv_proc_init *init_params, + int num_init_params); +void ipeh_exthdrs_fini(struct tlv_param_table *tlv_param_table); struct ipv6_txoptions; struct ipv6_opt_hdr; @@ -51,8 +143,9 @@ enum ipeh_parse_errors { #define IPEH_TLV_PAD1 0 #define IPEH_TLV_PADN 1 -bool ipeh_parse_tlv(const struct tlvtype_proc *procs, struct sk_buff *skb, - int max_count, int off, int len, +bool ipeh_parse_tlv(unsigned int class, + struct tlv_param_table *tlv_param_table, + struct sk_buff *skb, int max_count, int off, int len, bool (*parse_error)(struct sk_buff *skb, int off, enum ipeh_parse_errors error)); diff --git a/include/net/ipv6.h b/include/net/ipv6.h index d34c36c..7cefdf8 100644 --- a/include/net/ipv6.h +++ b/include/net/ipv6.h @@ -429,6 +429,9 @@ int ip6_ra_control(struct sock *sk, int sel); int ipv6_parse_hopopts(struct sk_buff *skb); +int ipv6_exthdrs_options_init(void); +void ipv6_exthdrs_options_exit(void); + bool ipv6_opt_accepted(const struct sock *sk, const struct sk_buff *skb, const struct inet6_skb_parm *opt); struct ipv6_txoptions *ipv6_update_options(struct sock *sk, diff --git a/include/uapi/linux/ipeh.h b/include/uapi/linux/ipeh.h new file mode 100644 index 0000000..c4302b7 --- /dev/null +++ b/include/uapi/linux/ipeh.h @@ -0,0 +1,16 @@ +/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */ +/* ipeh.h - IP extension header TLV management */ + +#ifndef _UAPI_LINUX_IPEH_H +#define _UAPI_LINUX_IPEH_H + +/* Flags for EH type that can use a TLV option */ +#define IPEH_TLV_CLASS_FLAG_HOPOPT BIT(0) +#define IPEH_TLV_CLASS_FLAG_RTRDSTOPT BIT(1) +#define IPEH_TLV_CLASS_FLAG_DSTOPT BIT(2) + +#define IPEH_TLV_CLASS_FLAG_MASK (IPEH_TLV_CLASS_FLAG_HOPOPT | \ + IPEH_TLV_CLASS_FLAG_RTRDSTOPT | \ + IPEH_TLV_CLASS_FLAG_DSTOPT) + +#endif /* _UAPI_LINUX_IPEH_H */ diff --git a/net/ipv6/exthdrs.c b/net/ipv6/exthdrs.c index 939d27c..0847d49 100644 --- a/net/ipv6/exthdrs.c +++ b/net/ipv6/exthdrs.c @@ -47,6 +47,7 @@ #ifdef CONFIG_IPV6_SEG6_HMAC #include #endif +#include #include @@ -131,7 +132,8 @@ static int ipv6_destopt_rcv(struct sk_buff *skb) dstbuf = opt->dst1; #endif - if (ipeh_parse_tlv(tlvprocdestopt_lst, skb, + if (ipeh_parse_tlv(IPEH_TLV_CLASS_FLAG_DSTOPT, + &ipv6_tlv_param_table, skb, init_net.ipv6.sysctl.max_dst_opts_cnt, 2, extlen - 2, ipv6_parse_error)) { skb->transport_header += extlen; @@ -514,8 +516,13 @@ int __init ipv6_exthdrs_init(void) if (ret) goto out_destopt; + ret = ipv6_exthdrs_options_init(); + if (ret) + goto out_nodata; out: return ret; +out_nodata: + inet6_del_protocol(&nodata_protocol, IPPROTO_NONE); out_destopt: inet6_del_protocol(&destopt_protocol, IPPROTO_DSTOPTS); out_rthdr: @@ -525,6 +532,7 @@ int __init ipv6_exthdrs_init(void) void ipv6_exthdrs_exit(void) { + ipv6_exthdrs_options_exit(); inet6_del_protocol(&nodata_protocol, IPPROTO_NONE); inet6_del_protocol(&destopt_protocol, IPPROTO_DSTOPTS); inet6_del_protocol(&rthdr_protocol, IPPROTO_ROUTING); @@ -555,8 +563,8 @@ int ipv6_parse_hopopts(struct sk_buff *skb) goto fail_and_free; opt->flags |= IP6SKB_HOPBYHOP; - if (ipeh_parse_tlv(tlvprochopopt_lst, skb, - init_net.ipv6.sysctl.max_hbh_opts_cnt, + if (ipeh_parse_tlv(IPEH_TLV_CLASS_FLAG_HOPOPT, &ipv6_tlv_param_table, + skb, init_net.ipv6.sysctl.max_hbh_opts_cnt, 2, extlen - 2, ipv6_parse_error)) { skb->transport_header += extlen; opt = IP6CB(skb); diff --git a/net/ipv6/exthdrs_common.c b/net/ipv6/exthdrs_common.c index 99a0911..43737fc 100644 --- a/net/ipv6/exthdrs_common.c +++ b/net/ipv6/exthdrs_common.c @@ -150,14 +150,15 @@ EXPORT_SYMBOL_GPL(ipeh_fixup_options); * - off is offset from skb_transport_header where first TLV is * - len is length of TLV block */ -bool ipeh_parse_tlv(const struct tlvtype_proc *procs, struct sk_buff *skb, - int max_count, int off, int len, +bool ipeh_parse_tlv(unsigned int class, + struct tlv_param_table *tlv_param_table, + struct sk_buff *skb, int max_count, int off, int len, bool (*parse_error)(struct sk_buff *skb, int off, enum ipeh_parse_errors error)) { const unsigned char *nh = skb_network_header(skb); - const struct tlvtype_proc *curr; bool disallow_unknowns = false; + const struct tlv_proc *curr; int tlv_count = 0; int padlen = 0; @@ -168,8 +169,10 @@ bool ipeh_parse_tlv(const struct tlvtype_proc *procs, struct sk_buff *skb, if (skb_transport_offset(skb) + off + len > skb_headlen(skb)) { if (!parse_error(skb, skb_transport_offset(skb), - IPEH_PARSE_ERR_EH_TOOBIG)) - goto bad; + IPEH_PARSE_ERR_EH_TOOBIG)) { + kfree_skb(skb); + return false; + } len = skb_headlen(skb) - skb_transport_offset(skb) - off; } @@ -177,6 +180,8 @@ bool ipeh_parse_tlv(const struct tlvtype_proc *procs, struct sk_buff *skb, /* ops function based offset on network header */ off += skb_network_header_len(skb); + rcu_read_lock(); + while (len > 0) { int optlen = nh[off + 1] + 2; int i; @@ -221,26 +226,22 @@ bool ipeh_parse_tlv(const struct tlvtype_proc *procs, struct sk_buff *skb, tlv_count++; if (tlv_count > max_count && - parse_error(skb, off, IPEH_PARSE_ERR_OPT_TOOMANY)) + !parse_error(skb, off, IPEH_PARSE_ERR_OPT_TOOMANY)) goto bad; - for (curr = procs; curr->type >= 0; curr++) { - if (curr->type == nh[off]) { - /* type specific length/alignment - * checks will be performed in the - * func(). - */ - if (curr->func(skb, off) == false) - return false; - break; - } - } - if (curr->type < 0 && - !parse_error(skb, off, + curr = ipeh_tlv_get_proc(tlv_param_table, &nh[off]); + if ((curr->params.r.class & class) && curr->ops.func) { + /* Handler will apply additional checks to + * the TLV + */ + if (!curr->ops.func(class, skb, off)) + return false; + } else if (!parse_error(skb, off, disallow_unknowns ? IPEH_PARSE_ERR_OPT_UNK_DISALW : - IPEH_PARSE_ERR_OPT_UNK)) + IPEH_PARSE_ERR_OPT_UNK)) { goto bad; + } padlen = 0; break; @@ -249,10 +250,238 @@ bool ipeh_parse_tlv(const struct tlvtype_proc *procs, struct sk_buff *skb, len -= optlen; } - if (len == 0) + if (len == 0) { + rcu_read_unlock(); return true; + } bad: + rcu_read_unlock(); kfree_skb(skb); return false; } EXPORT_SYMBOL(ipeh_parse_tlv); + +/* TLV parameter table functions and structures */ + +/* Default (unset) values for TLV parameters */ +static const struct tlv_proc tlv_default_proc = { +}; + +static DEFINE_MUTEX(tlv_mutex); + +static size_t tlv_param_table_size(unsigned char count) +{ + return sizeof(struct tlv_param_table_data) + + (count * sizeof(struct tlv_type)); +} + +static void tlv_param_table_release(struct rcu_head *rcu) +{ + struct tlv_param_table_data *tpt = + container_of(rcu, struct tlv_param_table_data, rcu); + + kvfree(tpt); +} + +/* mutex held */ +static int __tlv_set_one(struct tlv_param_table *tlv_param_table, + unsigned char type, const struct tlv_params *params, + const struct tlv_ops *ops) +{ + struct tlv_param_table_data *tpt, *told; + struct tlv_type *ttype; + + told = rcu_dereference_protected(tlv_param_table->data, + lockdep_is_held(&tlv_mutex)); + + /* Create new TLV table. If there is no exsiting entry then we are + * adding a new one to the table, else we're modifying an entry. + */ + tpt = kvmalloc(tlv_param_table_size(told->count + !told->entries[type]), + GFP_KERNEL); + if (!tpt) + return -ENOMEM; + + memcpy(tpt, told, tlv_param_table_size(told->count)); + + if (!told->entries[type]) { + memset(&tpt->types[told->count], 0, sizeof(struct tlv_type)); + tpt->entries[type] = told->count; + tpt->count = told->count + 1; + } + + ttype = &tpt->types[tpt->entries[type]]; + + ttype->proc.params = *params; + ttype->proc.ops = ops ? *ops : tlv_default_proc.ops; + + rcu_assign_pointer(tlv_param_table->data, tpt); + call_rcu(&told->rcu, tlv_param_table_release); + + return 0; +} + +int __ipeh_tlv_set(struct tlv_param_table *tlv_param_table, unsigned char type, + const struct tlv_params *params, const struct tlv_ops *ops) +{ + int retv; + + if (type < 2) + return -EINVAL; + + mutex_lock(&tlv_mutex); + retv = __tlv_set_one(tlv_param_table, type, params, ops); + mutex_unlock(&tlv_mutex); + + return retv; +} +EXPORT_SYMBOL(__ipeh_tlv_set); + +/* mutex held */ +static int __tlv_unset_one(struct tlv_param_table *tlv_param_table, + unsigned char type) +{ + struct tlv_param_table_data *tpt, *told; + unsigned int i, pos; + + told = rcu_dereference_protected(tlv_param_table->data, + lockdep_is_held(&tlv_mutex)); + + if (!told->entries[type]) + return 0; + + tpt = kvmalloc(tlv_param_table_size(told->count - 1), + GFP_KERNEL); + if (!tpt) + return -ENOMEM; + + pos = told->entries[type]; + + memcpy(tpt->types, told->types, pos * sizeof(struct tlv_type)); + memcpy(&tpt->types[pos], &told->types[pos + 1], + (told->count - pos - 1) * sizeof(struct tlv_type)); + + for (i = 0; i < 256; i++) { + if (told->entries[i] > pos) + tpt->entries[i] = told->entries[i] - 1; + else + tpt->entries[i] = told->entries[i]; + } + + /* Clear entry for type being unset (point to default params) */ + tpt->entries[type] = 0; + + tpt->count = told->count - 1; + + rcu_assign_pointer(tlv_param_table->data, tpt); + call_rcu(&told->rcu, tlv_param_table_release); + + return 0; +} + +/* tlv_internal_proc_type is used to check it the TLV proc was set + * internally. This is deduced by checking if any operations are defined. + */ +static bool tlv_internal_proc_type(struct tlv_proc *proc) +{ + return !!proc->ops.func; +} + +int __ipeh_tlv_unset(struct tlv_param_table *tlv_param_table, + unsigned char type, bool params_only) +{ + struct tlv_proc *tproc; + int retv; + + if (type < 2) + return -EINVAL; + + mutex_lock(&tlv_mutex); + + tproc = ipeh_tlv_get_proc_by_type(tlv_param_table, type); + + if (params_only && tlv_internal_proc_type(tproc)) { + /* TLV was set by internal source, so maintain the + * non-parameter fields (i.e. the operations). + */ + retv = __tlv_set_one(tlv_param_table, type, + &tlv_default_proc.params, + &tproc->ops); + } else { + retv = __tlv_unset_one(tlv_param_table, type); + } + + mutex_unlock(&tlv_mutex); + + return retv; +} +EXPORT_SYMBOL(__ipeh_tlv_unset); + +int ipeh_exthdrs_init(struct tlv_param_table *tlv_param_table, + const struct tlv_proc_init *tlv_init_params, + int num_init_params) +{ + struct tlv_param_table_data *tpt; + int pos = 0, i; + size_t tsize; + + tsize = tlv_param_table_size(num_init_params + 1); + + tpt = kvmalloc(tsize, GFP_KERNEL); + if (!tpt) + return -ENOMEM; + + memset(tpt, 0, tsize); + + /* Zeroth TLV proc entry is default */ + tpt->types[pos++].proc = tlv_default_proc; + + for (i = 0; i < num_init_params; i++, pos++) { + const struct tlv_proc_init *tpi = &tlv_init_params[i]; + + if (WARN_ON(tpi->type < 2)) { + /* Padding TLV initialized? */ + goto err_inval; + } + if (WARN_ON(tpt->entries[tpi->type])) { + /* TLV type already set */ + goto err_inval; + } + + tpt->types[pos].proc = tpi->proc; + tpt->entries[tpi->type] = pos; + } + + tpt->count = pos; + + RCU_INIT_POINTER(tlv_param_table->data, tpt); + + return 0; + +err_inval: + kvfree(tpt); + return -EINVAL; +} +EXPORT_SYMBOL(ipeh_exthdrs_init); + +static void tlv_destroy_param_table(struct tlv_param_table *tlv_param_table) +{ + struct tlv_param_table_data *tpt; + + mutex_lock(&tlv_mutex); + + tpt = rcu_dereference_protected(tlv_param_table->data, + lockdep_is_held(&tlv_mutex)); + if (tpt) { + rcu_assign_pointer(tlv_param_table->data, NULL); + call_rcu(&tpt->rcu, tlv_param_table_release); + } + + mutex_unlock(&tlv_mutex); +} + +void ipeh_exthdrs_fini(struct tlv_param_table *tlv_param_table) +{ + tlv_destroy_param_table(tlv_param_table); +} +EXPORT_SYMBOL(ipeh_exthdrs_fini); diff --git a/net/ipv6/exthdrs_options.c b/net/ipv6/exthdrs_options.c index 032e072..d4b373e 100644 --- a/net/ipv6/exthdrs_options.c +++ b/net/ipv6/exthdrs_options.c @@ -11,11 +11,12 @@ #if IS_ENABLED(CONFIG_IPV6_MIP6) #include #endif +#include /* Destination options header */ #if IS_ENABLED(CONFIG_IPV6_MIP6) -static bool ipv6_dest_hao(struct sk_buff *skb, int optoff) +static bool ipv6_dest_hao(unsigned int class, struct sk_buff *skb, int optoff) { struct ipv6_destopt_hao *hao; struct inet6_skb_parm *opt = IP6CB(skb); @@ -74,16 +75,6 @@ static bool ipv6_dest_hao(struct sk_buff *skb, int optoff) } #endif -const struct tlvtype_proc tlvprocdestopt_lst[] = { -#if IS_ENABLED(CONFIG_IPV6_MIP6) - { - .type = IPV6_TLV_HAO, - .func = ipv6_dest_hao, - }, -#endif - {-1, NULL} -}; - /* Hop-by-hop options */ /* Note: we cannot rely on skb_dst(skb) before we assign it in @@ -102,7 +93,7 @@ static inline struct net *ipv6_skb_net(struct sk_buff *skb) /* Router Alert as of RFC 2711 */ -static bool ipv6_hop_ra(struct sk_buff *skb, int optoff) +static bool ipv6_hop_ra(unsigned int class, struct sk_buff *skb, int optoff) { const unsigned char *nh = skb_network_header(skb); @@ -120,7 +111,7 @@ static bool ipv6_hop_ra(struct sk_buff *skb, int optoff) /* Jumbo payload */ -static bool ipv6_hop_jumbo(struct sk_buff *skb, int optoff) +static bool ipv6_hop_jumbo(unsigned int class, struct sk_buff *skb, int optoff) { const unsigned char *nh = skb_network_header(skb); struct inet6_dev *idev = __in6_dev_get_safely(skb->dev); @@ -164,7 +155,8 @@ static bool ipv6_hop_jumbo(struct sk_buff *skb, int optoff) /* CALIPSO RFC 5570 */ -static bool ipv6_hop_calipso(struct sk_buff *skb, int optoff) +static bool ipv6_hop_calipso(unsigned int class, struct sk_buff *skb, + int optoff) { const unsigned char *nh = skb_network_header(skb); @@ -184,18 +176,45 @@ static bool ipv6_hop_calipso(struct sk_buff *skb, int optoff) return false; } -const struct tlvtype_proc tlvprochopopt_lst[] = { +static const struct tlv_proc_init tlv_ipv6_init_params[] __initconst = { +#if IS_ENABLED(CONFIG_IPV6_MIP6) { - .type = IPV6_TLV_ROUTERALERT, - .func = ipv6_hop_ra, + .type = IPV6_TLV_HAO, + + .proc.ops.func = ipv6_dest_hao, + .proc.params.r.class = IPEH_TLV_CLASS_FLAG_DSTOPT, }, +#endif { - .type = IPV6_TLV_JUMBO, - .func = ipv6_hop_jumbo, + .type = IPV6_TLV_ROUTERALERT, + + .proc.ops.func = ipv6_hop_ra, + .proc.params.r.class = IPEH_TLV_CLASS_FLAG_HOPOPT, }, { - .type = IPV6_TLV_CALIPSO, - .func = ipv6_hop_calipso, + .type = IPV6_TLV_JUMBO, + + .proc.ops.func = ipv6_hop_jumbo, + .proc.params.r.class = IPEH_TLV_CLASS_FLAG_HOPOPT, + }, + { + .type = IPV6_TLV_CALIPSO, + + .proc.ops.func = ipv6_hop_calipso, + .proc.params.r.class = IPEH_TLV_CLASS_FLAG_HOPOPT, }, - { -1, } }; + +struct tlv_param_table __rcu ipv6_tlv_param_table; +EXPORT_SYMBOL(ipv6_tlv_param_table); + +int __init ipv6_exthdrs_options_init(void) +{ + return ipeh_exthdrs_init(&ipv6_tlv_param_table, tlv_ipv6_init_params, + ARRAY_SIZE(tlv_ipv6_init_params)); +} + +void ipv6_exthdrs_options_exit(void) +{ + ipeh_exthdrs_fini(&ipv6_tlv_param_table); +} From patchwork Thu Oct 3 21:58:02 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom Herbert X-Patchwork-Id: 1171429 X-Patchwork-Delegate: davem@davemloft.net Return-Path: X-Original-To: patchwork-incoming-netdev@ozlabs.org Delivered-To: patchwork-incoming-netdev@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=netdev-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=herbertland.com Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=herbertland-com.20150623.gappssmtp.com header.i=@herbertland-com.20150623.gappssmtp.com header.b="ducWEbcP"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 46kn2X5Ml3z9sNw for ; Fri, 4 Oct 2019 07:58:44 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1733163AbfJCV6n (ORCPT ); Thu, 3 Oct 2019 17:58:43 -0400 Received: from mail-pg1-f195.google.com ([209.85.215.195]:37662 "EHLO mail-pg1-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726002AbfJCV6m (ORCPT ); Thu, 3 Oct 2019 17:58:42 -0400 Received: by mail-pg1-f195.google.com with SMTP id p1so769111pgi.4 for ; Thu, 03 Oct 2019 14:58:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=herbertland-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=ZXaHRK5tNfYJxJE5z6skP2lwJDvsVTrOUi+z4zSSC94=; b=ducWEbcPFxFNi90pDLuESPqHygZMEt3HXdRpwl5RelAhYSo+fNBRY+zPciFPlC3fNh iIpC+q0sdnPOYvhbfi9ddv65R3/eX9LcP7Vu5cKqvFv/JM5hXXtu+TXxq7sFsxxUuKfB xYhRzNrTxYQ4tXoH+Sja9xrpi4BUXuBp8h58Ytp2TbtB8e2KICzKWq57GKf/nBsQg/ip rUcgsSvNe1FRJ6CCA3PG0AKHNbi3vOsXjtr8idAMnu1RFxzZ9uEJJJxyI5SfkJeSxr2x S/7PuKLWLQiIdgciypPdakToR4VWUuSPmiGzkiOVhnGrot2Bh4ZCgzRoGNeGS89HeTg7 QCug== 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=ZXaHRK5tNfYJxJE5z6skP2lwJDvsVTrOUi+z4zSSC94=; b=A+GRG4DKkqMHWz72pRD356nknmsnR0XAhZ2JUsDpTJiv9S9jRaOQ2ehsGPvu2+kYOZ mD0eco4vvGMAJ0b7d9u31pYKjLcGGHBA3XYF0XvafFqj+Tadeem3viKYqGB5Cg7lx74H tql+ob2geMF+Bx8h41wA9P/b5P0azusqv89qxdlgskUI5H0Txc9NHZgqcE2P0mqHRfxw lDWL1qbKv5NCL3f8NC5Qtf2ZeX/s00X9sMVhystFlAzY6j2Zn1jJD9xewCGY2umCDV7R 0JdCWxJf2ZpaxtNZYJSXOMUZVa10Jz80PJqMjHrDbiaMxuS1gT7QLt9bp8E+rgNMRM6l N+Ng== X-Gm-Message-State: APjAAAWKm5BcuSidPqoCdRxk4FpRFF8quzQIEgpUnCFfx9ZiePH7EL09 Ilyy/Lq8g+qZfesb2xugBFXtnIK+reJdZA== X-Google-Smtp-Source: APXvYqzaO6szK6kGGGnPSL+eWIr9Zexg7h45TXr7gWBfMCn+HDrd4dgloPYOWCi5z+Nv0tBO8SugiA== X-Received: by 2002:aa7:96ab:: with SMTP id g11mr13644196pfk.61.1570139922050; Thu, 03 Oct 2019 14:58:42 -0700 (PDT) Received: from localhost.localdomain (c-73-202-182-113.hsd1.ca.comcast.net. [73.202.182.113]) by smtp.gmail.com with ESMTPSA id r18sm3889905pfc.3.2019.10.03.14.58.40 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Thu, 03 Oct 2019 14:58:41 -0700 (PDT) From: Tom Herbert To: davem@davemloft.net, netdev@vger.kernel.org Cc: Tom Herbert , Tom Herbert Subject: [PATCH v5 net-next 5/7] ip6tlvs: Add TX parameters Date: Thu, 3 Oct 2019 14:58:02 -0700 Message-Id: <1570139884-20183-6-git-send-email-tom@herbertland.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1570139884-20183-1-git-send-email-tom@herbertland.com> References: <1570139884-20183-1-git-send-email-tom@herbertland.com> Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org From: Tom Herbert Define a number of transmit parameters for TLV Parameter table definitions. These will be used for validating TLVs that are set on a socket. Signed-off-by: Tom Herbert --- include/net/ipeh.h | 18 ++++++++++++++++ include/uapi/linux/ipeh.h | 8 +++++++ net/ipv6/exthdrs_common.c | 53 +++++++++++++++++++++++++++++++++++++++++++++- net/ipv6/exthdrs_options.c | 45 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 123 insertions(+), 1 deletion(-) diff --git a/include/net/ipeh.h b/include/net/ipeh.h index aaa2910..de6d9d0 100644 --- a/include/net/ipeh.h +++ b/include/net/ipeh.h @@ -20,6 +20,17 @@ struct tlv_rx_params { }; struct tlv_tx_params { + unsigned char admin_perm : 2; + unsigned char user_perm : 2; + unsigned char class : 3; + unsigned char rsvd : 1; + unsigned char align_mult : 4; + unsigned char align_off : 4; + unsigned char data_len_mult : 4; + unsigned char data_len_off : 4; + unsigned char min_data_len; + unsigned char max_data_len; + unsigned short preferred_order; }; struct tlv_params { @@ -54,6 +65,13 @@ struct tlv_param_table { extern struct tlv_param_table ipv6_tlv_param_table; +/* Preferred TLV ordering for HBH and Dest options (placed by increasing order) + */ +#define IPEH_TLV_PREF_ORDER_HAO 10 +#define IPEH_TLV_PREF_ORDER_ROUTERALERT 20 +#define IPEH_TLV_PREF_ORDER_JUMBO 30 +#define IPEH_TLV_PREF_ORDER_CALIPSO 40 + int __ipeh_tlv_set(struct tlv_param_table *tlv_param_table, unsigned char type, const struct tlv_params *params, const struct tlv_ops *ops); diff --git a/include/uapi/linux/ipeh.h b/include/uapi/linux/ipeh.h index c4302b7..dbf0728 100644 --- a/include/uapi/linux/ipeh.h +++ b/include/uapi/linux/ipeh.h @@ -13,4 +13,12 @@ IPEH_TLV_CLASS_FLAG_RTRDSTOPT | \ IPEH_TLV_CLASS_FLAG_DSTOPT) +/* TLV permissions values */ +enum { + IPEH_TLV_PERM_NONE, + IPEH_TLV_PERM_WITH_CHECK, + IPEH_TLV_PERM_NO_CHECK, + IPEH_TLV_PERM_MAX = IPEH_TLV_PERM_NO_CHECK +}; + #endif /* _UAPI_LINUX_IPEH_H */ diff --git a/net/ipv6/exthdrs_common.c b/net/ipv6/exthdrs_common.c index 43737fc..feaa4a6 100644 --- a/net/ipv6/exthdrs_common.c +++ b/net/ipv6/exthdrs_common.c @@ -3,6 +3,7 @@ /* Extension header and TLV library code that is not specific to IPv6. */ #include #include +#include struct ipv6_txoptions * ipeh_dup_options(struct sock *sk, struct ipv6_txoptions *opt) @@ -265,6 +266,13 @@ EXPORT_SYMBOL(ipeh_parse_tlv); /* Default (unset) values for TLV parameters */ static const struct tlv_proc tlv_default_proc = { + .params.t = { + .admin_perm = IPEH_TLV_PERM_NO_CHECK, + .user_perm = IPEH_TLV_PERM_NONE, + .align_mult = (4 - 1), /* Default alignment: 4n + 2 */ + .align_off = 2, + .max_data_len = 255, + }, }; static DEFINE_MUTEX(tlv_mutex); @@ -284,16 +292,45 @@ static void tlv_param_table_release(struct rcu_head *rcu) } /* mutex held */ +static int check_order(struct tlv_param_table_data *tpt, unsigned char type, + unsigned short order) +{ + int i; + + if (!order) + return -EINVAL; + + for (i = 2; i < 256; i++) { + struct tlv_type *ttype = &tpt->types[tpt->entries[i]]; + + if (!tpt->entries[i]) + continue; + + if (order == ttype->proc.params.t.preferred_order && + i != type) + return -EALREADY; + } + + return 0; +} + +/* mutex held */ static int __tlv_set_one(struct tlv_param_table *tlv_param_table, unsigned char type, const struct tlv_params *params, const struct tlv_ops *ops) { struct tlv_param_table_data *tpt, *told; struct tlv_type *ttype; + int retv; told = rcu_dereference_protected(tlv_param_table->data, lockdep_is_held(&tlv_mutex)); + /* Check preferred order */ + retv = check_order(told, type, params->t.preferred_order); + if (retv) + return retv; + /* Create new TLV table. If there is no exsiting entry then we are * adding a new one to the table, else we're modifying an entry. */ @@ -422,7 +459,7 @@ int ipeh_exthdrs_init(struct tlv_param_table *tlv_param_table, int num_init_params) { struct tlv_param_table_data *tpt; - int pos = 0, i; + int pos = 0, i, j; size_t tsize; tsize = tlv_param_table_size(num_init_params + 1); @@ -448,6 +485,20 @@ int ipeh_exthdrs_init(struct tlv_param_table *tlv_param_table, goto err_inval; } + if (WARN_ON(!tpi->proc.params.t.preferred_order)) { + /* Preferred order must be non-zero */ + goto err_inval; + } + + for (j = 0; j < i; j++) { + const struct tlv_proc_init *tpix = &tlv_init_params[j]; + + if (WARN_ON(tpi->proc.params.t.preferred_order == + tpix->proc.params.t.preferred_order)) { + /* Preferred order must be unique */ + goto err_inval; + } + } tpt->types[pos].proc = tpi->proc; tpt->entries[tpi->type] = pos; } diff --git a/net/ipv6/exthdrs_options.c b/net/ipv6/exthdrs_options.c index d4b373e..3b50b58 100644 --- a/net/ipv6/exthdrs_options.c +++ b/net/ipv6/exthdrs_options.c @@ -183,6 +183,17 @@ static const struct tlv_proc_init tlv_ipv6_init_params[] __initconst = { .proc.ops.func = ipv6_dest_hao, .proc.params.r.class = IPEH_TLV_CLASS_FLAG_DSTOPT, + + .proc.params.t = { + .preferred_order = IPEH_TLV_PREF_ORDER_HAO, + .admin_perm = IPEH_TLV_PERM_NO_CHECK, + .user_perm = IPEH_TLV_PERM_NONE, + .class = IPEH_TLV_CLASS_FLAG_DSTOPT, + .align_mult = (8 - 1), /* Align to 8n + 6 */ + .align_off = 6, + .min_data_len = 16, + .max_data_len = 16, + }, }, #endif { @@ -190,18 +201,52 @@ static const struct tlv_proc_init tlv_ipv6_init_params[] __initconst = { .proc.ops.func = ipv6_hop_ra, .proc.params.r.class = IPEH_TLV_CLASS_FLAG_HOPOPT, + + .proc.params.t = { + .preferred_order = IPEH_TLV_PREF_ORDER_ROUTERALERT, + .admin_perm = IPEH_TLV_PERM_NO_CHECK, + .user_perm = IPEH_TLV_PERM_NONE, + .class = IPEH_TLV_CLASS_FLAG_HOPOPT, + .align_mult = (2 - 1), /* Align to 2n */ + .min_data_len = 2, + .max_data_len = 2, + }, }, { .type = IPV6_TLV_JUMBO, .proc.ops.func = ipv6_hop_jumbo, .proc.params.r.class = IPEH_TLV_CLASS_FLAG_HOPOPT, + + .proc.params.t = { + .preferred_order = IPEH_TLV_PREF_ORDER_JUMBO, + .admin_perm = IPEH_TLV_PERM_NO_CHECK, + .user_perm = IPEH_TLV_PERM_NONE, + .class = IPEH_TLV_CLASS_FLAG_HOPOPT, + .align_mult = (4 - 1), /* Align to 4n + 2 */ + .align_off = 2, + .min_data_len = 4, + .max_data_len = 4, + }, }, { .type = IPV6_TLV_CALIPSO, .proc.ops.func = ipv6_hop_calipso, .proc.params.r.class = IPEH_TLV_CLASS_FLAG_HOPOPT, + + .proc.params.t = { + .preferred_order = IPEH_TLV_PREF_ORDER_CALIPSO, + .admin_perm = IPEH_TLV_PERM_NO_CHECK, + .user_perm = IPEH_TLV_PERM_NONE, + .class = IPEH_TLV_CLASS_FLAG_HOPOPT, + .align_mult = (4 - 1), /* Align to 4n + 2 */ + .align_off = 2, + .min_data_len = 8, + .max_data_len = 252, + .data_len_mult = (4 - 1), + /* Length is multiple of 4 */ + }, }, }; From patchwork Thu Oct 3 21:58:03 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom Herbert X-Patchwork-Id: 1171430 X-Patchwork-Delegate: davem@davemloft.net Return-Path: X-Original-To: patchwork-incoming-netdev@ozlabs.org Delivered-To: patchwork-incoming-netdev@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=netdev-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=herbertland.com Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=herbertland-com.20150623.gappssmtp.com header.i=@herbertland-com.20150623.gappssmtp.com header.b="llka3nlv"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 46kn2Z4Bdwz9sNw for ; Fri, 4 Oct 2019 07:58:46 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1733239AbfJCV6p (ORCPT ); Thu, 3 Oct 2019 17:58:45 -0400 Received: from mail-pg1-f195.google.com ([209.85.215.195]:45282 "EHLO mail-pg1-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726002AbfJCV6p (ORCPT ); Thu, 3 Oct 2019 17:58:45 -0400 Received: by mail-pg1-f195.google.com with SMTP id q7so2516979pgi.12 for ; Thu, 03 Oct 2019 14:58:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=herbertland-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=8zMRZ5zxIfNy2H6TePf29FlXEB1XGcNKVPTfluwZZZA=; b=llka3nlvbJ3Sn/PUTTDid0wJD+fmB9bb+oA0fAdwrIWRYWQ5//RVeiakjznfGiSE/V afMmbDuZ9wsMTyIQikUbeGmojHqTc+SXnA3BQXy1Oc5sI9MES7xy611fnwZ+5l+X4r7Y s0xL37K4Ve9u4djYcokaRVLqtFNPPdOCbyovD1n2+1GDDb/6kn4XULWiW69RTFFgrIsU LeB/0opm+DcdtkD8v5rPC5PWtwDZ5WLBzdqUkG5ZuhLgJ0Ak+n+HmM3gEutRtj2+B4oy WAzN8HZlu1/+4aVubfmlzbU+isb44bTQ05mbmLpitlwKkrPnpZ9Tp2IM8AQfO9D4paJ2 7Tvg== 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=8zMRZ5zxIfNy2H6TePf29FlXEB1XGcNKVPTfluwZZZA=; b=f+2QpX1ZTERxpWpeIRSVawltTOY6qtLkesqOJi8bhvag38VNmHI05e+baIScmpU3a7 +eRvhYy9ofyaDE/7Q0PtzZix4neg72p/R405OjoiHgaauBBq+8afyi2eEOds9Wk908YH I2kFYV9FvWngl6pfz6QYJ5HvhHkO4q1z5UPkQPuwoDBMcu1SD2ghFD1sga7fUE2afwV0 fKx4qyjUgbNp3p7fw+FgxQ3FYz4DOpsHvjnz/BQXY6c4VjeYH8RT7bQzaizYkzJCcLLJ okj/NTnOI3KYf+XFePCxZ1ClGJQjBG2gsWGEozuBLM4BN3PxTXTYSQzlxt7xE/ify7uO 0jHQ== X-Gm-Message-State: APjAAAU26ELsi1za5Y5LuNAKqke9Ii8gPRHfH+j5tDKd+hCvnHUX28Jn mHb4qQRizdLqwfuzs+UFGsRZIA== X-Google-Smtp-Source: APXvYqwmAOBW9ZAPb1STH1aqk3Csc+nQ1T1WgLoEkJ2y4mHINvvDAKtZQXunUtqWW7/qfpqHdq4axA== X-Received: by 2002:a62:b40a:: with SMTP id h10mr13278892pfn.88.1570139924227; Thu, 03 Oct 2019 14:58:44 -0700 (PDT) Received: from localhost.localdomain (c-73-202-182-113.hsd1.ca.comcast.net. [73.202.182.113]) by smtp.gmail.com with ESMTPSA id r18sm3889905pfc.3.2019.10.03.14.58.43 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Thu, 03 Oct 2019 14:58:43 -0700 (PDT) From: Tom Herbert To: davem@davemloft.net, netdev@vger.kernel.org Cc: Tom Herbert , Tom Herbert Subject: [PATCH v5 net-next 6/7] ip6tlvs: Add netlink interface Date: Thu, 3 Oct 2019 14:58:03 -0700 Message-Id: <1570139884-20183-7-git-send-email-tom@herbertland.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1570139884-20183-1-git-send-email-tom@herbertland.com> References: <1570139884-20183-1-git-send-email-tom@herbertland.com> Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org From: Tom Herbert Add a netlink interface to manage the TX TLV parameters. Managed parameters include those for validating and sending TLVs being sent such as alignment, TLV ordering, length limits, etc. Signed-off-by: Tom Herbert --- include/net/ipeh.h | 16 +++ include/net/ipv6.h | 1 + include/uapi/linux/in6.h | 6 ++ include/uapi/linux/ipeh.h | 29 ++++++ net/ipv6/exthdrs_common.c | 238 +++++++++++++++++++++++++++++++++++++++++++++ net/ipv6/exthdrs_options.c | 81 ++++++++++++++- 6 files changed, 369 insertions(+), 2 deletions(-) diff --git a/include/net/ipeh.h b/include/net/ipeh.h index de6d9d0..8474a43 100644 --- a/include/net/ipeh.h +++ b/include/net/ipeh.h @@ -3,6 +3,7 @@ #define _NET_IPEH_H #include +#include /* * Parsing tlv encoded headers. @@ -106,6 +107,21 @@ static inline int ipeh_tlv_unset_proc(struct tlv_param_table *tlv_param_table, return __ipeh_tlv_unset(tlv_param_table, type, false); } +extern const struct nla_policy ipeh_tlv_nl_policy[]; + +int ipeh_tlv_nl_cmd_set(struct tlv_param_table *tlv_param_table, + struct genl_family *tlv_nl_family, + struct sk_buff *skb, struct genl_info *info); +int ipeh_tlv_nl_cmd_unset(struct tlv_param_table *tlv_param_table, + struct genl_family *tlv_nl_family, + struct sk_buff *skb, struct genl_info *info); +int ipeh_tlv_nl_cmd_get(struct tlv_param_table *tlv_param_table, + struct genl_family *tlv_nl_family, + struct sk_buff *skb, struct genl_info *info); +int ipeh_tlv_nl_dump(struct tlv_param_table *tlv_param_table, + struct genl_family *tlv_nl_family, + struct sk_buff *skb, struct netlink_callback *cb); + /* ipeh_tlv_get_proc_by_type assumes rcu_read_lock is held */ static inline struct tlv_proc *ipeh_tlv_get_proc_by_type( struct tlv_param_table *tlv_param_table, unsigned char type) diff --git a/include/net/ipv6.h b/include/net/ipv6.h index 7cefdf8..54b6e53 100644 --- a/include/net/ipv6.h +++ b/include/net/ipv6.h @@ -14,6 +14,7 @@ #include #include #include +#include #include #include #include diff --git a/include/uapi/linux/in6.h b/include/uapi/linux/in6.h index 9f2273a..d5fe3d9 100644 --- a/include/uapi/linux/in6.h +++ b/include/uapi/linux/in6.h @@ -297,4 +297,10 @@ struct in6_flowlabel_req { * ... * MRT6_MAX */ + + /* NETLINK_GENERIC related info for IPv6 TLVs */ + +#define IPV6_TLV_GENL_NAME "ipv6-tlv" +#define IPV6_TLV_GENL_VERSION 0x1 + #endif /* _UAPI_LINUX_IN6_H */ diff --git a/include/uapi/linux/ipeh.h b/include/uapi/linux/ipeh.h index dbf0728..bac36a7 100644 --- a/include/uapi/linux/ipeh.h +++ b/include/uapi/linux/ipeh.h @@ -21,4 +21,33 @@ enum { IPEH_TLV_PERM_MAX = IPEH_TLV_PERM_NO_CHECK }; +/* NETLINK_GENERIC related info for IP TLVs */ + +enum { + IPEH_TLV_ATTR_UNSPEC, + IPEH_TLV_ATTR_TYPE, /* u8, > 1 */ + IPEH_TLV_ATTR_ORDER, /* u16 */ + IPEH_TLV_ATTR_ADMIN_PERM, /* u8, perm value */ + IPEH_TLV_ATTR_USER_PERM, /* u8, perm value */ + IPEH_TLV_ATTR_CLASS, /* u8, 3 bit flags */ + IPEH_TLV_ATTR_ALIGN_MULT, /* u8, 1 to 16 */ + IPEH_TLV_ATTR_ALIGN_OFF, /* u8, 0 to 15 */ + IPEH_TLV_ATTR_MIN_DATA_LEN, /* u8 (option data length) */ + IPEH_TLV_ATTR_MAX_DATA_LEN, /* u8 (option data length) */ + IPEH_TLV_ATTR_DATA_LEN_MULT, /* u8, 1 to 16 */ + IPEH_TLV_ATTR_DATA_LEN_OFF, /* u8, 0 to 15 */ + + __IPEH_TLV_ATTR_MAX, +}; + +#define IPEH_TLV_ATTR_MAX (__IPEH_TLV_ATTR_MAX - 1) + +enum { + IPEH_TLV_CMD_SET, + IPEH_TLV_CMD_UNSET, + IPEH_TLV_CMD_GET, + + __IPEH_TLV_CMD_MAX, +}; + #endif /* _UAPI_LINUX_IPEH_H */ diff --git a/net/ipv6/exthdrs_common.c b/net/ipv6/exthdrs_common.c index feaa4a6..e3c1f33 100644 --- a/net/ipv6/exthdrs_common.c +++ b/net/ipv6/exthdrs_common.c @@ -454,6 +454,244 @@ int __ipeh_tlv_unset(struct tlv_param_table *tlv_param_table, } EXPORT_SYMBOL(__ipeh_tlv_unset); +const struct nla_policy ipeh_tlv_nl_policy[IPEH_TLV_ATTR_MAX + 1] = { + [IPEH_TLV_ATTR_TYPE] = { .type = NLA_U8, }, + [IPEH_TLV_ATTR_ORDER] = { .type = NLA_U16, }, + [IPEH_TLV_ATTR_ADMIN_PERM] = { .type = NLA_U8, }, + [IPEH_TLV_ATTR_USER_PERM] = { .type = NLA_U8, }, + [IPEH_TLV_ATTR_CLASS] = { .type = NLA_U8, }, + [IPEH_TLV_ATTR_ALIGN_MULT] = { .type = NLA_U8, }, + [IPEH_TLV_ATTR_ALIGN_OFF] = { .type = NLA_U8, }, + [IPEH_TLV_ATTR_MIN_DATA_LEN] = { .type = NLA_U8, }, + [IPEH_TLV_ATTR_MAX_DATA_LEN] = { .type = NLA_U8, }, + [IPEH_TLV_ATTR_DATA_LEN_OFF] = { .type = NLA_U8, }, + [IPEH_TLV_ATTR_DATA_LEN_MULT] = { .type = NLA_U8, }, +}; +EXPORT_SYMBOL(ipeh_tlv_nl_policy); + +int ipeh_tlv_nl_cmd_set(struct tlv_param_table *tlv_param_table, + struct genl_family *tlv_nl_family, + struct sk_buff *skb, struct genl_info *info) +{ + struct tlv_params new_params; + struct tlv_proc *tproc; + unsigned char type; + unsigned int v; + int retv = -EINVAL; + + if (!info->attrs[IPEH_TLV_ATTR_TYPE]) + return -EINVAL; + + type = nla_get_u8(info->attrs[IPEH_TLV_ATTR_TYPE]); + if (type < 2) + return -EINVAL; + + rcu_read_lock(); + + /* Base new parameters on existing ones */ + tproc = ipeh_tlv_get_proc_by_type(tlv_param_table, type); + new_params = tproc->params; + + if (info->attrs[IPEH_TLV_ATTR_ORDER]) { + v = nla_get_u16(info->attrs[IPEH_TLV_ATTR_ORDER]); + new_params.t.preferred_order = v; + } + + if (info->attrs[IPEH_TLV_ATTR_ADMIN_PERM]) { + v = nla_get_u8(info->attrs[IPEH_TLV_ATTR_ADMIN_PERM]); + if (v > IPEH_TLV_PERM_MAX) + goto out; + new_params.t.admin_perm = v; + } + + if (info->attrs[IPEH_TLV_ATTR_USER_PERM]) { + v = nla_get_u8(info->attrs[IPEH_TLV_ATTR_USER_PERM]); + if (v > IPEH_TLV_PERM_MAX) + goto out; + new_params.t.user_perm = v; + } + + if (info->attrs[IPEH_TLV_ATTR_CLASS]) { + v = nla_get_u8(info->attrs[IPEH_TLV_ATTR_CLASS]); + if (!v || (v & ~IPEH_TLV_CLASS_FLAG_MASK)) + goto out; + new_params.t.class = v; + } + + if (info->attrs[IPEH_TLV_ATTR_ALIGN_MULT]) { + v = nla_get_u8(info->attrs[IPEH_TLV_ATTR_ALIGN_MULT]); + if (v > 16 || v < 1) + goto out; + new_params.t.align_mult = v - 1; + } + + if (info->attrs[IPEH_TLV_ATTR_ALIGN_OFF]) { + v = nla_get_u8(info->attrs[IPEH_TLV_ATTR_ALIGN_OFF]); + if (v > 15) + goto out; + new_params.t.align_off = v; + } + + if (info->attrs[IPEH_TLV_ATTR_MAX_DATA_LEN]) + new_params.t.max_data_len = + nla_get_u8(info->attrs[IPEH_TLV_ATTR_MAX_DATA_LEN]); + + if (info->attrs[IPEH_TLV_ATTR_MIN_DATA_LEN]) + new_params.t.min_data_len = + nla_get_u8(info->attrs[IPEH_TLV_ATTR_MIN_DATA_LEN]); + + if (info->attrs[IPEH_TLV_ATTR_DATA_LEN_MULT]) { + v = nla_get_u8(info->attrs[IPEH_TLV_ATTR_DATA_LEN_MULT]); + if (v > 16 || v < 1) + goto out; + new_params.t.data_len_mult = v - 1; + } + + if (info->attrs[IPEH_TLV_ATTR_DATA_LEN_OFF]) { + v = nla_get_u8(info->attrs[IPEH_TLV_ATTR_DATA_LEN_OFF]); + if (v > 15) + goto out; + new_params.t.data_len_off = v; + } + + retv = ipeh_tlv_set_params(tlv_param_table, type, &new_params); + +out: + rcu_read_unlock(); + return retv; +} +EXPORT_SYMBOL(ipeh_tlv_nl_cmd_set); + +int ipeh_tlv_nl_cmd_unset(struct tlv_param_table *tlv_param_table, + struct genl_family *tlv_nl_family, + struct sk_buff *skb, struct genl_info *info) +{ + unsigned char type; + + if (!info->attrs[IPEH_TLV_ATTR_TYPE]) + return -EINVAL; + + type = nla_get_u8(info->attrs[IPEH_TLV_ATTR_TYPE]); + if (type < 2) + return -EINVAL; + + return ipeh_tlv_unset_params(tlv_param_table, type); +} +EXPORT_SYMBOL(ipeh_tlv_nl_cmd_unset); + +static int tlv_fill_info(struct tlv_proc *tproc, unsigned char type, + struct sk_buff *msg, bool admin) +{ + struct tlv_params *tp = &tproc->params; + int ret = 0; + + if (nla_put_u8(msg, IPEH_TLV_ATTR_TYPE, type) || + nla_put_u16(msg, IPEH_TLV_ATTR_ORDER, tp->t.preferred_order) || + nla_put_u8(msg, IPEH_TLV_ATTR_USER_PERM, tp->t.user_perm) || + (admin && nla_put_u8(msg, IPEH_TLV_ATTR_ADMIN_PERM, + tp->t.admin_perm)) || + nla_put_u8(msg, IPEH_TLV_ATTR_CLASS, tp->t.class) || + nla_put_u8(msg, IPEH_TLV_ATTR_ALIGN_MULT, tp->t.align_mult + 1) || + nla_put_u8(msg, IPEH_TLV_ATTR_ALIGN_OFF, tp->t.align_off) || + nla_put_u8(msg, IPEH_TLV_ATTR_MIN_DATA_LEN, tp->t.min_data_len) || + nla_put_u8(msg, IPEH_TLV_ATTR_MAX_DATA_LEN, tp->t.max_data_len) || + nla_put_u8(msg, IPEH_TLV_ATTR_DATA_LEN_MULT, + tp->t.data_len_mult + 1) || + nla_put_u8(msg, IPEH_TLV_ATTR_DATA_LEN_OFF, tp->t.data_len_off)) + ret = -1; + + return ret; +} + +static int tlv_dump_info(struct tlv_proc *tproc, unsigned char type, + struct genl_family *tlv_nl_family, u32 portid, + u32 seq, u32 flags, struct sk_buff *skb, u8 cmd, + bool admin) +{ + void *hdr; + + hdr = genlmsg_put(skb, portid, seq, tlv_nl_family, flags, cmd); + if (!hdr) + return -ENOMEM; + + if (tlv_fill_info(tproc, type, skb, admin) < 0) { + genlmsg_cancel(skb, hdr); + return -EMSGSIZE; + } + + genlmsg_end(skb, hdr); + + return 0; +} + +int ipeh_tlv_nl_cmd_get(struct tlv_param_table *tlv_param_table, + struct genl_family *tlv_nl_family, + struct sk_buff *skb, struct genl_info *info) +{ + struct tlv_proc *tproc; + struct sk_buff *msg; + unsigned char type; + int ret; + + if (!info->attrs[IPEH_TLV_ATTR_TYPE]) + return -EINVAL; + + type = nla_get_u8(info->attrs[IPEH_TLV_ATTR_TYPE]); + if (type < 2) + return -EINVAL; + + msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); + if (!msg) + return -ENOMEM; + + rcu_read_lock(); + + tproc = ipeh_tlv_get_proc_by_type(tlv_param_table, type); + ret = tlv_dump_info(tproc, type, tlv_nl_family, info->snd_portid, + info->snd_seq, 0, msg, info->genlhdr->cmd, + netlink_capable(skb, CAP_NET_ADMIN)); + + rcu_read_unlock(); + + if (ret < 0) { + nlmsg_free(msg); + return ret; + } + + return genlmsg_reply(msg, info); +} +EXPORT_SYMBOL(ipeh_tlv_nl_cmd_get); + +int ipeh_tlv_nl_dump(struct tlv_param_table *tlv_param_table, + struct genl_family *tlv_nl_family, + struct sk_buff *skb, struct netlink_callback *cb) +{ + struct tlv_proc *tproc; + int idx = 0, ret, i; + + rcu_read_lock(); + + for (i = 2; i < 256; i++) { + if (idx++ < cb->args[0]) + continue; + + tproc = ipeh_tlv_get_proc_by_type(tlv_param_table, i); + ret = tlv_dump_info(tproc, i, tlv_nl_family, + NETLINK_CB(cb->skb).portid, + cb->nlh->nlmsg_seq, NLM_F_MULTI, + skb, IPEH_TLV_CMD_GET, + netlink_capable(cb->skb, CAP_NET_ADMIN)); + if (ret) + break; + } + + rcu_read_unlock(); + + cb->args[0] = idx; + return skb->len; +} +EXPORT_SYMBOL(ipeh_tlv_nl_dump); + int ipeh_exthdrs_init(struct tlv_param_table *tlv_param_table, const struct tlv_proc_init *tlv_init_params, int num_init_params) diff --git a/net/ipv6/exthdrs_options.c b/net/ipv6/exthdrs_options.c index 3b50b58..c1889f6 100644 --- a/net/ipv6/exthdrs_options.c +++ b/net/ipv6/exthdrs_options.c @@ -6,6 +6,7 @@ #include #include #include +#include #include #include #if IS_ENABLED(CONFIG_IPV6_MIP6) @@ -253,13 +254,89 @@ static const struct tlv_proc_init tlv_ipv6_init_params[] __initconst = { struct tlv_param_table __rcu ipv6_tlv_param_table; EXPORT_SYMBOL(ipv6_tlv_param_table); +static int ipv6_tlv_nl_cmd_set(struct sk_buff *skb, struct genl_info *info); +static int ipv6_tlv_nl_cmd_unset(struct sk_buff *skb, struct genl_info *info); +static int ipv6_tlv_nl_cmd_get(struct sk_buff *skb, struct genl_info *info); +static int ipv6_tlv_nl_dump(struct sk_buff *skb, struct netlink_callback *cb); + +static const struct genl_ops ipv6_tlv_nl_ops[] = { +{ + .cmd = IPEH_TLV_CMD_SET, + .doit = ipv6_tlv_nl_cmd_set, + .flags = GENL_ADMIN_PERM, +}, +{ + .cmd = IPEH_TLV_CMD_UNSET, + .doit = ipv6_tlv_nl_cmd_unset, + .flags = GENL_ADMIN_PERM, +}, +{ + .cmd = IPEH_TLV_CMD_GET, + .doit = ipv6_tlv_nl_cmd_get, + .dumpit = ipv6_tlv_nl_dump, +}, +}; + +struct genl_family ipv6_tlv_nl_family __ro_after_init = { + .hdrsize = 0, + .name = IPV6_TLV_GENL_NAME, + .version = IPV6_TLV_GENL_VERSION, + .maxattr = IPEH_TLV_ATTR_MAX, + .policy = ipeh_tlv_nl_policy, + .netnsok = true, + .parallel_ops = true, + .ops = ipv6_tlv_nl_ops, + .n_ops = ARRAY_SIZE(ipv6_tlv_nl_ops), + .module = THIS_MODULE, +}; + +static int ipv6_tlv_nl_cmd_set(struct sk_buff *skb, struct genl_info *info) +{ + return ipeh_tlv_nl_cmd_set(&ipv6_tlv_param_table, &ipv6_tlv_nl_family, + skb, info); +} + +static int ipv6_tlv_nl_cmd_unset(struct sk_buff *skb, struct genl_info *info) +{ + return ipeh_tlv_nl_cmd_unset(&ipv6_tlv_param_table, &ipv6_tlv_nl_family, + skb, info); +} + +static int ipv6_tlv_nl_cmd_get(struct sk_buff *skb, struct genl_info *info) +{ + return ipeh_tlv_nl_cmd_get(&ipv6_tlv_param_table, &ipv6_tlv_nl_family, + skb, info); +} + +static int ipv6_tlv_nl_dump(struct sk_buff *skb, struct netlink_callback *cb) +{ + return ipeh_tlv_nl_dump(&ipv6_tlv_param_table, &ipv6_tlv_nl_family, + skb, cb); +} + int __init ipv6_exthdrs_options_init(void) { - return ipeh_exthdrs_init(&ipv6_tlv_param_table, tlv_ipv6_init_params, - ARRAY_SIZE(tlv_ipv6_init_params)); + int err; + + err = genl_register_family(&ipv6_tlv_nl_family); + if (err) + goto genl_fail; + + ipeh_exthdrs_init(&ipv6_tlv_param_table, tlv_ipv6_init_params, + ARRAY_SIZE(tlv_ipv6_init_params)); + if (err) + goto ipv6_fail; + + return 0; + +ipv6_fail: + genl_unregister_family(&ipv6_tlv_nl_family); +genl_fail: + return err; } void ipv6_exthdrs_options_exit(void) { ipeh_exthdrs_fini(&ipv6_tlv_param_table); + genl_unregister_family(&ipv6_tlv_nl_family); } From patchwork Thu Oct 3 21:58:04 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom Herbert X-Patchwork-Id: 1171431 X-Patchwork-Delegate: davem@davemloft.net Return-Path: X-Original-To: patchwork-incoming-netdev@ozlabs.org Delivered-To: patchwork-incoming-netdev@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=netdev-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=herbertland.com Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=herbertland-com.20150623.gappssmtp.com header.i=@herbertland-com.20150623.gappssmtp.com header.b="2OxPbIoR"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 46kn2f58PQz9sNw for ; Fri, 4 Oct 2019 07:58:50 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2387495AbfJCV6u (ORCPT ); Thu, 3 Oct 2019 17:58:50 -0400 Received: from mail-pg1-f196.google.com ([209.85.215.196]:46854 "EHLO mail-pg1-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726002AbfJCV6t (ORCPT ); Thu, 3 Oct 2019 17:58:49 -0400 Received: by mail-pg1-f196.google.com with SMTP id a3so2509852pgm.13 for ; Thu, 03 Oct 2019 14:58:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=herbertland-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=/thsByp6mTBTVSdNYFnEyXZbSPtwdw2AwHm8AOIT4w0=; b=2OxPbIoRg7LalRcp1lf059i3mGvW3Eikrh7hFsfic+ykM2JW06d+kxfF3CSEwBqguZ OEQD7prGYTzEjzrmOogYZGKvxrqvC+J9nZwi2v3m/zClKjlg5QfYEZT3sHJIDCqoy2Qw 1D0xfPqPCm7ILIA84m/KzLsALG3tbt0dsCMD5HVkExfUZC06g9CdvE4WO8sArK4o94zL +3i5EMeryvh6oV55EGgDW/OhU83tp1obHNEDvUl47/LpGT8k/J9czW2G7Zo8XXL8vD8N zahHBlpiyIy4EU/xLRVAiprgiaqijZEWf2KhSQ3Djc0TkfpBJaStxGBSxYjvdP+DziAC 3bJQ== 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=/thsByp6mTBTVSdNYFnEyXZbSPtwdw2AwHm8AOIT4w0=; b=dZdPl4hNFk7y08eSy6jaifrgECcMDSGihO4dud/Stou0x45JWC4gCn86p9DYldcA5D WHRs2QpCUVuLiejHlGc2iM8tgLbyLztJuLjalBif4N3XPMSNXkA7YdcEKpGpQ+Cnwxk2 RXUS/DpBLGcUdZoASMQ6Atgk62tI5gl1pWeDKqInvGV+Eh7NthwLlzLgJuS9Szgashnw S6r8LViIChcx+bza6bEvZP3Drq+A5OrWtsl8j9Qnpp7bwwQ/VF2GVuvoS5czs6NdGIVB QoSf6/zDaI3CG/dJl1bFkOj560mKyZ89FPRRBpUwjFI7dVRwuLB7T+qYsSGqlMTJPzSh oOYw== X-Gm-Message-State: APjAAAX3JSTIASL9QQrJHKF9XM4O3ibNkyfVeoxLxD5BwDuomo6mSQ9c +9Oes/BJuaiMdUuiKn/ZCYCg9Q== X-Google-Smtp-Source: APXvYqzVOReZA1P/6atuX8vwwQY6lVHyqs0oQGfclLi3LiQ7pgXjl4u/nY4JlVCfgAaV/mdfwSOCmg== X-Received: by 2002:a17:90a:94ca:: with SMTP id j10mr12775027pjw.24.1570139927729; Thu, 03 Oct 2019 14:58:47 -0700 (PDT) Received: from localhost.localdomain (c-73-202-182-113.hsd1.ca.comcast.net. [73.202.182.113]) by smtp.gmail.com with ESMTPSA id r18sm3889905pfc.3.2019.10.03.14.58.46 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Thu, 03 Oct 2019 14:58:47 -0700 (PDT) From: Tom Herbert To: davem@davemloft.net, netdev@vger.kernel.org Cc: Tom Herbert , Tom Herbert Subject: [PATCH v5 net-next 7/7] ip6tlvs: Validation of TX Destination and Hop-by-Hop options Date: Thu, 3 Oct 2019 14:58:04 -0700 Message-Id: <1570139884-20183-8-git-send-email-tom@herbertland.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1570139884-20183-1-git-send-email-tom@herbertland.com> References: <1570139884-20183-1-git-send-email-tom@herbertland.com> Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org From: Tom Herbert Validate Destination and Hop-by-Hop options. This uses the information in the TLV parameters table to validate various aspects of both individual TLVs as well as a list of TLVs in an extension header. There are two levels of validation that can be performed: simple checks and deep checks. Simple checks validate only the most basic properties such as that the TLV list fits into the EH. Deep checks do a fine grained validation that includes perferred ordering, length limits, and length alignment. With proper permissions set in the TLV parameter table, this patch allows non-privileged users to send TLVs. Given that TLVs are open ended and potentially a source of DOS attack, deep checks are performed to limit the format that a non-privileged user can send. If deep checks are enabled, a canonical format for sending TLVs is enforced (in adherence with the robustness principle). A TLV must be well ordered with respect to the preferred order for the TLV. Each TLV must be aligned as described in the parameter table. Minimal padding (one padding TLV) is used to align TLVs. The length of the extension header as well as the count of non-padding TLVs is checked against max_*_opts_len and max_*_opts_cnt. For individual TLVs, length limits and length alignment is checked. Signed-off-by: Tom Herbert --- include/net/ipeh.h | 22 +++ net/ipv6/datagram.c | 51 +++++-- net/ipv6/exthdrs_common.c | 382 ++++++++++++++++++++++++++++++++++++++++++++++ net/ipv6/ipv6_sockglue.c | 39 ++--- 4 files changed, 455 insertions(+), 39 deletions(-) diff --git a/include/net/ipeh.h b/include/net/ipeh.h index 8474a43..e58a0f4 100644 --- a/include/net/ipeh.h +++ b/include/net/ipeh.h @@ -157,6 +157,28 @@ struct ipv6_txoptions *ipeh_renew_options(struct sock *sk, struct ipv6_txoptions *ipeh_fixup_options(struct ipv6_txoptions *opt_space, struct ipv6_txoptions *opt); +int ipeh_opt_validate_tlvs(struct net *net, + struct tlv_param_table *tlv_param_table, + struct ipv6_opt_hdr *opt, + unsigned int optname, bool admin, + unsigned int max_len, unsigned int max_cnt); +int ipeh_opt_validate_single_tlv(struct net *net, + struct tlv_param_table *tlv_param_table, + unsigned int optname, const __u8 *tlv, + size_t len, bool deleting, bool admin); +int ipeh_opt_check_perm(struct net *net, + struct tlv_param_table *tlv_param_table, + struct ipv6_txoptions *txopt, int optname, bool admin); + +struct ipv6_txoptions *ipeh_txopt_from_opt(struct sock *sk, + struct tlv_param_table + *tlv_param_table, + struct ipv6_txoptions *opt, + int optname, char __user *optval, + unsigned int optlen, + unsigned int max_len, + unsigned int max_cnt); + /* Generic extension header TLV parser */ enum ipeh_parse_errors { diff --git a/net/ipv6/datagram.c b/net/ipv6/datagram.c index 96f9392..f9856a7 100644 --- a/net/ipv6/datagram.c +++ b/net/ipv6/datagram.c @@ -839,7 +839,10 @@ int ip6_datagram_send_ctl(struct net *net, struct sock *sk, break; case IPV6_2292HOPOPTS: - case IPV6_HOPOPTS: + case IPV6_HOPOPTS: { + int max_len = net->ipv6.sysctl.max_hbh_opts_len; + int max_cnt = net->ipv6.sysctl.max_hbh_opts_cnt; + if (opt->hopopt || cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_opt_hdr))) { err = -EINVAL; goto exit_f; @@ -851,15 +854,24 @@ int ip6_datagram_send_ctl(struct net *net, struct sock *sk, err = -EINVAL; goto exit_f; } - if (!ns_capable(net->user_ns, CAP_NET_RAW)) { - err = -EPERM; + + err = ipeh_opt_validate_tlvs(net, &ipv6_tlv_param_table, + hdr, IPV6_HOPOPTS, + ns_capable(net->user_ns, + CAP_NET_RAW), + max_len, max_cnt); + if (err < 0) goto exit_f; - } + opt->opt_nflen += len; opt->hopopt = hdr; break; + } + + case IPV6_2292DSTOPTS: { + int max_len = net->ipv6.sysctl.max_dst_opts_len; + int max_cnt = net->ipv6.sysctl.max_dst_opts_cnt; - case IPV6_2292DSTOPTS: if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_opt_hdr))) { err = -EINVAL; goto exit_f; @@ -871,10 +883,14 @@ int ip6_datagram_send_ctl(struct net *net, struct sock *sk, err = -EINVAL; goto exit_f; } - if (!ns_capable(net->user_ns, CAP_NET_RAW)) { - err = -EPERM; + err = ipeh_opt_validate_tlvs(net, &ipv6_tlv_param_table, + hdr, IPV6_DSTOPTS, + ns_capable(net->user_ns, + CAP_NET_RAW), + max_len, max_cnt); + if (err < 0) goto exit_f; - } + if (opt->dst1opt) { err = -EINVAL; goto exit_f; @@ -882,9 +898,13 @@ int ip6_datagram_send_ctl(struct net *net, struct sock *sk, opt->opt_flen += len; opt->dst1opt = hdr; break; + } case IPV6_DSTOPTS: - case IPV6_RTHDRDSTOPTS: + case IPV6_RTHDRDSTOPTS: { + int max_len = net->ipv6.sysctl.max_dst_opts_len; + int max_cnt = net->ipv6.sysctl.max_dst_opts_cnt; + if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_opt_hdr))) { err = -EINVAL; goto exit_f; @@ -896,10 +916,15 @@ int ip6_datagram_send_ctl(struct net *net, struct sock *sk, err = -EINVAL; goto exit_f; } - if (!ns_capable(net->user_ns, CAP_NET_RAW)) { - err = -EPERM; + + err = ipeh_opt_validate_tlvs(net, &ipv6_tlv_param_table, + hdr, IPV6_DSTOPTS, + ns_capable(net->user_ns, + CAP_NET_RAW), + max_len, max_cnt); + if (err < 0) goto exit_f; - } + if (cmsg->cmsg_type == IPV6_DSTOPTS) { opt->opt_flen += len; opt->dst1opt = hdr; @@ -908,7 +933,7 @@ int ip6_datagram_send_ctl(struct net *net, struct sock *sk, opt->dst0opt = hdr; } break; - + } case IPV6_2292RTHDR: case IPV6_RTHDR: if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_rt_hdr))) { diff --git a/net/ipv6/exthdrs_common.c b/net/ipv6/exthdrs_common.c index e3c1f33..c3047df 100644 --- a/net/ipv6/exthdrs_common.c +++ b/net/ipv6/exthdrs_common.c @@ -262,6 +262,318 @@ bool ipeh_parse_tlv(unsigned int class, } EXPORT_SYMBOL(ipeh_parse_tlv); +/* TLV validation functions */ + +/* Validate a single non-padding TLV */ +static int __ipeh_opt_validate_single_tlv(struct net *net, const __u8 *tlv, + struct tlv_proc *tproc, + unsigned int class, bool *deep_check, + bool deleting, bool admin) +{ + struct tlv_tx_params *tptx = &tproc->params.t; + + if (tlv[0] < 2) /* Must be non-padding */ + return -EINVAL; + + /* Check permissions */ + switch (admin ? tptx->admin_perm : tptx->user_perm) { + case IPEH_TLV_PERM_NO_CHECK: + /* Allowed with no deep checks */ + *deep_check = false; + return 0; + case IPEH_TLV_PERM_WITH_CHECK: + /* Allowed with deep checks */ + *deep_check = true; + break; + default: + /* No permission */ + return -EPERM; + } + + /* Perform deep checks on the TLV */ + + /* Check class */ + if ((tptx->class & class) != class) + return -EINVAL; + + /* Don't bother checking lengths when deleting, the TLV is only + * needed here for lookup + */ + if (deleting) { + /* Don't bother with deep checks when deleting */ + *deep_check = false; + } else { + /* Check length */ + if (tlv[1] < tptx->min_data_len || tlv[1] > tptx->max_data_len) + return -EINVAL; + + /* Check length alignment */ + if ((tlv[1] % (tptx->data_len_mult + 1)) != tptx->data_len_off) + return -EINVAL; + } + + return 0; +} + +static unsigned int optname_to_tlv_class(int optname) +{ + switch (optname) { + case IPV6_HOPOPTS: + return IPEH_TLV_CLASS_FLAG_HOPOPT; + case IPV6_RTHDRDSTOPTS: + return IPEH_TLV_CLASS_FLAG_RTRDSTOPT; + case IPV6_DSTOPTS: + return IPEH_TLV_CLASS_FLAG_DSTOPT; + default: + return -1U; + } +} + +static int __ipeh_opt_validate_tlvs(struct net *net, + struct tlv_param_table *tlv_param_table, + struct ipv6_opt_hdr *opt, + unsigned int optname, bool deleting, + bool admin, unsigned int max_len, + unsigned int max_cnt) +{ + bool deep_check = !admin, did_deep_check = false; + unsigned int opt_len, tlv_len, offset; + unsigned int padding = 0, numpad = 0; + unsigned short prev_tlv_order = 0; + unsigned int class, cnt = 0; + struct tlv_tx_params *tptx; + int retc, ret = -EINVAL; + __u8 *tlv = (__u8 *)opt; + struct tlv_proc *tproc; + + opt_len = ipv6_optlen(opt); + offset = sizeof(*opt); + + class = optname_to_tlv_class(optname); + + rcu_read_lock(); + + while (offset < opt_len) { + switch (tlv[offset]) { + case IPV6_TLV_PAD1: + tlv_len = 1; + padding++; + numpad++; + break; + case IPV6_TLV_PADN: + if (offset + 1 >= opt_len) + goto out; + + tlv_len = tlv[offset + 1] + 2; + + if (offset + tlv_len > opt_len) + goto out; + + padding += tlv_len; + numpad++; + break; + default: + if (offset + 1 >= opt_len) + goto out; + + tlv_len = tlv[offset + 1] + 2; + + if (offset + tlv_len > opt_len) + goto out; + + tproc = ipeh_tlv_get_proc(tlv_param_table, + &tlv[offset]); + tptx = &tproc->params.t; + + retc = __ipeh_opt_validate_single_tlv(net, &tlv[offset], + tproc, class, + &deep_check, + deleting, admin); + if (retc < 0) { + ret = retc; + goto out; + } + + if (deep_check) { + /* Check for too many options */ + if (++cnt > max_cnt) { + ret = -E2BIG; + goto out; + } + + /* Check order */ + if (tptx->preferred_order < prev_tlv_order) + goto out; + + /* Check alignment */ + if ((offset % (tptx->align_mult + 1)) != + tptx->align_off) + goto out; + + /* Check for right amount of padding */ + if (numpad > 1 || padding > tptx->align_mult) + goto out; + + prev_tlv_order = tptx->preferred_order; + + did_deep_check = true; + } + padding = 0; + numpad = 0; + } + offset += tlv_len; + } + + /* Check trailing padding. Note this covers the case option list + * only contains padding. + */ + if (deep_check && (numpad > 1 || padding > 7)) + goto out; + + /* If we did at least one deep check apply length limit */ + if (did_deep_check && opt_len > max_len) { + ret = -EMSGSIZE; + goto out; + } + + /* All good */ + ret = 0; +out: + rcu_read_unlock(); + + return ret; +} + +/** + * ipeh_opt_validate_tlvs - Validate TLVs. + * @net: Current net + * @tlv_param_table: TLV parameter table + * @opt: The option header + * @optname: IPV6_HOPOPTS, IPV6_RTHDRDSTOPTS, or IPV6_DSTOPTS + * @admin: Set for privileged user + * @max_len: Maximum length for TLV + * @max_cnt: Maximum number of non-padding TLVs + * + * Description: + * Walks the TLVs in a list to verify that the TLV lengths and other + * parameters are in bounds for a Destination or Hop-by-Hop option. + * Return -EINVAL is there is a problem, zero otherwise. + */ +int ipeh_opt_validate_tlvs(struct net *net, + struct tlv_param_table *tlv_param_table, + struct ipv6_opt_hdr *opt, unsigned int optname, + bool admin, unsigned int max_len, + unsigned int max_cnt) +{ + return __ipeh_opt_validate_tlvs(net, tlv_param_table, opt, optname, + false, admin, max_len, max_cnt); +} +EXPORT_SYMBOL(ipeh_opt_validate_tlvs); + +/** + * ipeh_opt_validate_single_tlv - Check that a single TLV is valid. + * @net: Current net + * @tlv_param_table: TLV parameter table + * @optname: IPV6_HOPOPTS, IPV6_RTHDRDSTOPTS, or IPV6_DSTOPTS + * @tlv: The TLV as array of bytes + * @len: Length of buffer holding TLV + * @deleting: TLV is being deleted + * @admin: Set for privileged user + * + * Description: + * Validates a single TLV. The TLV must be non-padding type. The length + * of the TLV (as determined by the second byte that gives length of the + * option data) must match @len. + */ +int ipeh_opt_validate_single_tlv(struct net *net, + struct tlv_param_table *tlv_param_table, + unsigned int optname, const __u8 *tlv, + size_t len, bool deleting, bool admin) +{ + struct tlv_proc *tproc; + unsigned int class; + bool deep_check; + int ret = 0; + + class = optname_to_tlv_class(optname); + + if (tlv[0] < 2) + return -EINVAL; + + if (len < 2) + return -EINVAL; + + if (tlv[1] + 2 != len) + return -EINVAL; + + rcu_read_lock(); + + tproc = ipeh_tlv_get_proc(tlv_param_table, tlv); + + ret = __ipeh_opt_validate_single_tlv(net, tlv, tproc, class, + &deep_check, deleting, admin); + + rcu_read_unlock(); + + return ret; +} +EXPORT_SYMBOL(ipeh_opt_validate_single_tlv); + +/** + * ipeh_opt_check_perm - Check that current capabilities allows modifying + * txopts. + * @net: Current net + * @tlv_param_table: TLV parameter table + * @txopts: TX options from the socket + * @optname: IPV6_HOPOPTS, IPV6_RTHDRDSTOPTS, or IPV6_DSTOPTS + * @admin: Set for privileged user + * + * Description: + * + * Checks whether the permissions of TLV that are set on a socket permit + * modificationr. + * + */ +int ipeh_opt_check_perm(struct net *net, + struct tlv_param_table *tlv_param_table, + struct ipv6_txoptions *txopt, int optname, bool admin) +{ + struct ipv6_opt_hdr *opt; + int retv = -EPERM; + + if (!txopt) + return 0; + + switch (optname) { + case IPV6_HOPOPTS: + opt = txopt->hopopt; + break; + case IPV6_RTHDRDSTOPTS: + opt = txopt->dst0opt; + break; + case IPV6_DSTOPTS: + opt = txopt->dst1opt; + break; + default: + goto out; + } + + if (!opt) { + retv = 0; + goto out; + } + + /* Just call the validate function on the options as being + * deleted. + */ + retv = __ipeh_opt_validate_tlvs(net, tlv_param_table, opt, optname, + true, admin, -1U, -1U); + +out: + return retv; +} +EXPORT_SYMBOL(ipeh_opt_check_perm); + /* TLV parameter table functions and structures */ /* Default (unset) values for TLV parameters */ @@ -454,6 +766,76 @@ int __ipeh_tlv_unset(struct tlv_param_table *tlv_param_table, } EXPORT_SYMBOL(__ipeh_tlv_unset); +/* Utility function tp create TX options from a setsockopt that is setting + * options on a socket. + */ +struct ipv6_txoptions *ipeh_txopt_from_opt(struct sock *sk, + struct tlv_param_table + *tlv_param_table, + struct ipv6_txoptions *opt, + int optname, char __user *optval, + unsigned int optlen, + unsigned int max_len, + unsigned int max_cnt) +{ + struct ipv6_opt_hdr *new = NULL; + struct net *net = sock_net(sk); + int retv; + + /* remove any sticky options header with a zero option + * length, per RFC3542. + */ + if (optlen == 0) { + optval = NULL; + } else if (!optval) { + return ERR_PTR(-EINVAL); + } else if (optlen < sizeof(struct ipv6_opt_hdr) || + optlen & 0x7 || optlen > 8 * 255) { + return ERR_PTR(-EINVAL); + } else { + new = memdup_user(optval, optlen); + if (IS_ERR(new)) + return (struct ipv6_txoptions *)new; + if (unlikely(ipv6_optlen(new) > optlen)) { + kfree(new); + return ERR_PTR(-EINVAL); + } + } + + if (optname != IPV6_RTHDR) { + bool cap = ns_capable(net->user_ns, CAP_NET_RAW); + + /* First check if we have permission to delete + * the existing options on the socket. + */ + retv = ipeh_opt_check_perm(net, tlv_param_table, + opt, optname, cap); + if (retv < 0) { + kfree(new); + return ERR_PTR(retv); + } + + /* Check permissions and other validations on new + * TLVs + */ + if (new) { + retv = ipeh_opt_validate_tlvs(net, tlv_param_table, + new, optname, cap, + max_len, max_cnt); + if (retv < 0) { + kfree(new); + return ERR_PTR(retv); + } + } + } + + opt = ipeh_renew_options(sk, opt, optname, new); + kfree(new); + + return opt; +} +EXPORT_SYMBOL(ipeh_txopt_from_opt); + const struct nla_policy ipeh_tlv_nl_policy[IPEH_TLV_ATTR_MAX + 1] = { [IPEH_TLV_ATTR_TYPE] = { .type = NLA_U8, }, [IPEH_TLV_ATTR_ORDER] = { .type = NLA_U16, }, diff --git a/net/ipv6/ipv6_sockglue.c b/net/ipv6/ipv6_sockglue.c index 8755ecc..b8e007ca 100644 --- a/net/ipv6/ipv6_sockglue.c +++ b/net/ipv6/ipv6_sockglue.c @@ -395,40 +395,27 @@ static int do_ipv6_setsockopt(struct sock *sk, int level, int optname, case IPV6_RTHDR: case IPV6_DSTOPTS: { + unsigned int max_len = -1U, max_cnt = -1U; struct ipv6_txoptions *opt; - struct ipv6_opt_hdr *new = NULL; - /* hop-by-hop / destination options are privileged option */ - retv = -EPERM; - if (optname != IPV6_RTHDR && !ns_capable(net->user_ns, CAP_NET_RAW)) + switch (optname) { + case IPV6_HOPOPTS: + max_len = net->ipv6.sysctl.max_hbh_opts_len; + max_cnt = net->ipv6.sysctl.max_hbh_opts_cnt; break; - - /* remove any sticky options header with a zero option - * length, per RFC3542. - */ - if (optlen == 0) - optval = NULL; - else if (!optval) - goto e_inval; - else if (optlen < sizeof(struct ipv6_opt_hdr) || - optlen & 0x7 || optlen > 8 * 255) - goto e_inval; - else { - new = memdup_user(optval, optlen); - if (IS_ERR(new)) { - retv = PTR_ERR(new); + case IPV6_RTHDRDSTOPTS: + case IPV6_DSTOPTS: + max_len = net->ipv6.sysctl.max_dst_opts_len; + max_cnt = net->ipv6.sysctl.max_dst_opts_cnt; break; - } - if (unlikely(ipv6_optlen(new) > optlen)) { - kfree(new); - goto e_inval; - } } opt = rcu_dereference_protected(np->opt, lockdep_sock_is_held(sk)); - opt = ipeh_renew_options(sk, opt, optname, new); - kfree(new); + opt = ipeh_txopt_from_opt(sk, &ipv6_tlv_param_table, opt, + optname, optval, optlen, max_len, + max_cnt); + if (IS_ERR(opt)) { retv = PTR_ERR(opt); break;