Skip to contentSkip to conversation

ipv6: Rework infrastructure for TLV options in extension headers

PATCH In review Tom Herbert 2 participants 7 messages Jan 28, 2019 – Feb 1, 2019

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

v2 cover ipv6: Rework infrastructure for TLV options in extension headers

This patch set implements an infrastructure to allow fine grained control over IPv6 Destination and Hop-by-Hop TLV options. This includes being able to registers handlers for receiving options as well as a set of parameters that sets permissions for who may send a TLV option and the preferred format of a list of options.

The goal of the patch is to enable broader use cases of IPv6 options, including those for which non-privileged users can send options. In order to curtail misuse of options in such cases, a number of requirements on how the option may be sent and formatted is enforced.

Particular features are:

- A single TLV paramters table containing the information about each TLV is defined. Lookups are simple offset accesses to the table based on TLV type. Both receiving and sending properties of TLVs are maintained in the table.
- Allow registration/deregistration of receive handlers for specific TLVs.
- Describe the properties of sending different TLVs in a TLV parameter table. The parameter table can be managed via netlink.
- Allow non-privileged users to send TLVs for which they have been granted permission.
- Provide a deep validation of TLVs and TLV lists to enforce specific limits and permission in order to thwart misuse of TLVs.
- Define a canonical format for sending TLVs that includes a preferred order, option alignment, minimal padding between TLVs.
- Allow individual TLVs to be added or set in txoptions list on a socket.

Tested: Write and read different TLVs on a socket via setsockopt and getsockopt. Flow is add individual TLV, read back options, set read option on new socket as a list. Verify options are properly sent and received. Used a modified ip command in iproute2 to test managing the TLV parameter via netlink.

v2:
- Change the TLV parameters table to be an array of u8s as opposed to pointers. Each entry gives the offset into a second array of TLV parameters structures. This reduces the memory footprint needed for the table but still allows O(1) lookups on TLV type.

Tom Herbert (5):
exthdrs: Create exthdrs_options.c
exthdrs: Registration of TLV handlers and parameters
ip6tlvs: Add netlink interface
ip6tlvs: Validation of TX Destination and Hop-by-Hop options
ip6tlvs: API to set and remove individual TLVs from DO or HBH EH

 include/net/ipv6.h         |   82 ++
 include/uapi/linux/in6.h   |   58 ++
 net/ipv6/Makefile          |    2 +-
 net/ipv6/datagram.c        |   27 +-
 net/ipv6/exthdrs.c         |  389 +---------
 net/ipv6/exthdrs_options.c | 1798 ++++++++++++++++++++++++++++++++++++++++++++
 net/ipv6/ipv6_sockglue.c   |  110 ++-
 7 files changed, 2089 insertions(+), 377 deletions(-)
 create mode 100644 net/ipv6/exthdrs_options.c
Signature · 1 line
2.7.4
#1 lore raw

PATCH v2 1/5 exthdrs: Create exthdrs_options.c

Create exthdrs_options.c to hold code related to Hop-by-Hop and Destination extension header options. Move related functions in exthdrs.c to the new file.

---
 include/net/ipv6.h         |   8 ++
 net/ipv6/Makefile          |   2 +-
 net/ipv6/exthdrs.c         | 342 --------------------------------------------
 net/ipv6/exthdrs_options.c | 346 +++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 355 insertions(+), 343 deletions(-)
 create mode 100644 net/ipv6/exthdrs_options.c
include/net/ipv6.h +8 -0
index daf8086..8abdcdb 100644
@@ -379,6 +379,14 @@ struct ipv6_txoptions *ipv6_renew_options(struct sock *sk,
 struct ipv6_txoptions *ipv6_fixup_options(struct ipv6_txoptions *opt_space,
 					  struct ipv6_txoptions *opt);
 
+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[];
+
 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,
net/ipv6/Makefile +1 -1
index e0026fa..72bd775 100644
@@ -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
 
net/ipv6/exthdrs.c +0 -342

372 lines not on this page · raw message

net/ipv6/exthdrs_options.c new file +346 -0

346 lines not on this page · raw message

Signature · 1 line
2.7.4
#2 lore raw

PATCH v2 2/5 exthdrs: Registration of TLV handlers and parameters

In reply to 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 parameter data structures which follows the array of u8s. TLV. The TLV parameter data structure contains parameters and handler functions for receiving and transmitting TLVs. The receive and transmit properties can be managed independently. The zeroth element in the TLV parameter array provides default parameters for TLVs.

TLV transmit properties include a description of limits, alignment, and preferred ordering. TLV receive properties provide the receiver handler. A class attribute is defined in both receive and transmit properties that 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).

Receive TLV lookup and processing is modified to be a lookup in the TLV parameter table. tlv_{set,unset}_{rx,tx}_param function can be used to set attributes in the TLV table. A table containing parameters for TLVs supported by the kernel and is used to initialize the TLV table.

---
 include/net/ipv6.h         |  64 +++++++-
 include/uapi/linux/in6.h   |  17 ++
 net/ipv6/exthdrs.c         |  47 +++---
 net/ipv6/exthdrs_options.c | 384 ++++++++++++++++++++++++++++++++++++++++++---
 4 files changed, 465 insertions(+), 47 deletions(-)
include/net/ipv6.h +59 -5
index 8abdcdb..9992f17 100644
@@ -379,13 +379,67 @@ struct ipv6_txoptions *ipv6_renew_options(struct sock *sk,
 struct ipv6_txoptions *ipv6_fixup_options(struct ipv6_txoptions *opt_space,
 					  struct ipv6_txoptions *opt);
 
-struct tlvtype_proc {
-	int	type;
-	bool	(*func)(struct sk_buff *skb, int offset);
+struct tlv_tx_param {
+	unsigned char preferred_order;
+	unsigned char admin_perm : 2;
+	unsigned char user_perm : 2;
+	unsigned char class : 3;
+	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;
 };
 
-extern const struct tlvtype_proc tlvprocdestopt_lst[];
-extern const struct tlvtype_proc tlvprochopopt_lst[];
+struct tlv_rx_param {
+	unsigned char class: 3;
+	bool (*func)(unsigned int class, struct sk_buff *skb, int offset);
+};
+
+struct tlv_param {
+	struct tlv_tx_param tx_params;
+	struct tlv_rx_param rx_params;
+};
+
+struct tlv_param_table {
+	unsigned char entries[256];
+	unsigned char count;
+	struct rcu_head rcu;
+	struct tlv_param params[0];
+};
+
+extern struct tlv_param_table __rcu *tlv_param_table;
+
+/* Preferred TLV ordering (placed by increasing order) */
+#define TLV_PREF_ORDER_HAO		10
+#define TLV_PREF_ORDER_ROUTERALERT	20
+#define TLV_PREF_ORDER_JUMBO		30
+#define TLV_PREF_ORDER_CALIPSO		40
+
+/* tlv_deref_rx_params assume rcu_read_lock is held */
+static inline struct tlv_rx_param *tlv_deref_rx_params(unsigned int type)
+{
+	struct tlv_param_table *tpt = rcu_dereference(tlv_param_table);
+
+	return &tpt->params[tpt->entries[type]].rx_params;
+}
+
+/* tlv_deref_tx_params assume rcu_read_lock is held */
+static inline struct tlv_tx_param *tlv_deref_tx_params(unsigned int type)
+{
+	struct tlv_param_table *tpt = rcu_dereference(tlv_param_table);
+
+	return &tpt->params[tpt->entries[type]].tx_params;
+}
+
+int tlv_set_param(unsigned char type,
+		  const struct tlv_rx_param *rx_param_tmpl,
+		  const struct tlv_tx_param *tx_param_tmpl);
+int tlv_unset_rx_param(unsigned char type);
+int tlv_set_rx_param(unsigned char type, struct tlv_rx_param *rx_param_tmpl);
+int tlv_unset_tx_param(unsigned char type);
+int tlv_set_tx_param(unsigned char type, struct tlv_tx_param *tx_param_tmpl);
 
 bool ipv6_opt_accepted(const struct sock *sk, const struct sk_buff *skb,
 		       const struct inet6_skb_parm *opt);
include/uapi/linux/in6.h +17 -0
index 71d82fe..38e8e63 100644
@@ -296,4 +296,21 @@ struct in6_flowlabel_req {
  * ...
  * MRT6_MAX
  */
+
+/* TLV permissions values */
+enum {
+	IPV6_TLV_PERM_NONE,
+	IPV6_TLV_PERM_WITH_CHECK,
+	IPV6_TLV_PERM_NO_CHECK,
+	IPV6_TLV_PERM_MAX = IPV6_TLV_PERM_NO_CHECK
+};
+
+/* Flags for EH type that can use a TLV option */
+#define IPV6_TLV_CLASS_FLAG_HOPOPT	0x1
+#define IPV6_TLV_CLASS_FLAG_RTRDSTOPT	0x2
+#define IPV6_TLV_CLASS_FLAG_DSTOPT	0x4
+#define IPV6_TLV_CLASS_MAX		0x7
+
+#define IPV6_TLV_CLASS_ANY_DSTOPT	(IPV6_TLV_CLASS_FLAG_RTRDSTOPT | \
+					 IPV6_TLV_CLASS_FLAG_DSTOPT)
 #endif /* _UAPI_LINUX_IN6_H */
net/ipv6/exthdrs.c +27 -20

97 lines not on this page · raw message

net/ipv6/exthdrs_options.c +362 -22

436 lines not on this page · raw message

Signature · 1 line
2.7.4
maintainer
#3 lore raw

Re: [PATCH v2 net-next 2/5] exthdrs: Registration of TLV handlers and parameters

From: Tom Herbert <[email protected]>
Date: Mon, 28 Jan 2019 10:22:52 -0800

Quoted 3 lines
net/ipv6/exthdrs_options.c +3 -0
+	tpt = vmalloc(tlv_param_table_size(old->count - 1));
+	if (!tpt)
+		return -ENOMEM;

...

net/ipv6/exthdrs_options.c static bool ipv6_hop_calipso(struct sk_buff *skb, int optoff) · line 645

Quoted 5 lines
net/ipv6/exthdrs_options.c +5 -0
+	tsize = tlv_param_table_size(ARRAY_SIZE(tlv_init_params) + 1);
+
+	tpt = vmalloc(tsize);
+	if (!tpt)
+		return -ENOMEM;

Most of the time this is going to be well within range for a quick, more efficient, linearly mapped kmalloc allocation.

So please use kvzalloc() or similar for this table.

#4 lore raw

PATCH v2 3/5 ip6tlvs: Add netlink interface

In reply to 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.

---
 include/uapi/linux/in6.h   |  32 +++++
 net/ipv6/exthdrs_options.c | 300 ++++++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 331 insertions(+), 1 deletion(-)
include/uapi/linux/in6.h +32 -0
index 38e8e63..a54cf96 100644
@@ -297,6 +297,38 @@ 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
+
+enum {
+	IPV6_TLV_ATTR_UNSPEC,
+	IPV6_TLV_ATTR_TYPE,			/* u8, > 1 */
+	IPV6_TLV_ATTR_ORDER,			/* u8 */
+	IPV6_TLV_ATTR_ADMIN_PERM,		/* u8, perm value */
+	IPV6_TLV_ATTR_USER_PERM,		/* u8, perm value */
+	IPV6_TLV_ATTR_CLASS,			/* u8, 3 bit flags */
+	IPV6_TLV_ATTR_ALIGN_MULT,		/* u8, 1 to 16 */
+	IPV6_TLV_ATTR_ALIGN_OFF,		/* u8, 0 to 15 */
+	IPV6_TLV_ATTR_MIN_DATA_LEN,		/* u8 (option data length) */
+	IPV6_TLV_ATTR_MAX_DATA_LEN,		/* u8 (option data length) */
+	IPV6_TLV_ATTR_DATA_LEN_MULT,		/* u8, 1 to 16 */
+	IPV6_TLV_ATTR_DATA_LEN_OFF,		/* u8, 0 to 15 */
+
+	__IPV6_TLV_ATTR_MAX,
+};
+
+#define IPV6_TLV_ATTR_MAX              (__IPV6_TLV_ATTR_MAX - 1)
+
+enum {
+	IPV6_TLV_CMD_SET,
+	IPV6_TLV_CMD_UNSET,
+	IPV6_TLV_CMD_GET,
+
+	__IPV6_TLV_CMD_MAX,
+};
+
 /* TLV permissions values */
 enum {
 	IPV6_TLV_PERM_NONE,
net/ipv6/exthdrs_options.c +299 -1

333 lines not on this page · raw message

Signature · 1 line
2.7.4
#5 lore raw

PATCH v2 4/5 ip6tlvs: Validation of TX Destination and Hop-by-Hop options

In reply to 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 on 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, lengths length alignment is checked.

---
 include/net/ipv6.h         |   7 ++
 net/ipv6/datagram.c        |  27 ++--
 net/ipv6/exthdrs_options.c | 298 +++++++++++++++++++++++++++++++++++++++++++++
 net/ipv6/ipv6_sockglue.c   |  30 ++++-
 4 files changed, 348 insertions(+), 14 deletions(-)
include/net/ipv6.h +7 -0
index 9992f17..c5692d6 100644
@@ -379,6 +379,13 @@ struct ipv6_txoptions *ipv6_renew_options(struct sock *sk,
 struct ipv6_txoptions *ipv6_fixup_options(struct ipv6_txoptions *opt_space,
 					  struct ipv6_txoptions *opt);
 
+int ipv6_opt_validate_tlvs(struct net *net, struct ipv6_opt_hdr *opt,
+			   unsigned int optname, bool admin);
+int ipv6_opt_validate_single_tlv(struct net *net, unsigned int optname,
+				 unsigned char *tlv, size_t len,
+				 bool deleting, bool admin);
+int ipv6_opt_check_perm(struct net *net, struct sock *sk,
+			int optname, bool admin);
 struct tlv_tx_param {
 	unsigned char preferred_order;
 	unsigned char admin_perm : 2;
net/ipv6/datagram.c +18 -9
index ee4a4e5..ef50439 100644
@@ -853,10 +853,13 @@ 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 = ipv6_opt_validate_tlvs(net, hdr, IPV6_HOPOPTS,
+						     ns_capable(net->user_ns,
+								CAP_NET_RAW));
+			if (err < 0)
 				goto exit_f;
-			}
+
 			opt->opt_nflen += len;
 			opt->hopopt = hdr;
 			break;
@@ -873,10 +876,13 @@ 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 = ipv6_opt_validate_tlvs(net, hdr, IPV6_DSTOPTS,
+						     ns_capable(net->user_ns,
+								CAP_NET_RAW));
+			if (err < 0)
 				goto exit_f;
-			}
+
 			if (opt->dst1opt) {
 				err = -EINVAL;
 				goto exit_f;
@@ -898,10 +904,13 @@ 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 = ipv6_opt_validate_tlvs(net, hdr, cmsg->cmsg_type,
+						     ns_capable(net->user_ns,
+								CAP_NET_RAW));
+			if (err < 0)
 				goto exit_f;
-			}
+
 			if (cmsg->cmsg_type == IPV6_DSTOPTS) {
 				opt->opt_flen += len;
 				opt->dst1opt = hdr;
net/ipv6/exthdrs_options.c +298 -0

304 lines not on this page · raw message

net/ipv6/ipv6_sockglue.c +25 -5
index 973e215..009c8a4 100644
@@ -400,11 +400,6 @@ static int do_ipv6_setsockopt(struct sock *sk, int level, int optname,
 		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))
-			break;
-
 		/* remove any sticky options header with a zero option
 		 * length, per RFC3542.
 		 */
@@ -427,6 +422,31 @@ static int do_ipv6_setsockopt(struct sock *sk, int level, int optname,
 			}
 		}
 
+		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 = ipv6_opt_check_perm(net, sk, optname, cap);
+			if (retv < 0) {
+				kfree(new);
+				break;
+			}
+
+			/* Check permissions and other validations on new
+			 * TLVs
+			 */
+			if (new) {
+				retv = ipv6_opt_validate_tlvs(net, new,
+							      optname, cap);
+				if (retv < 0) {
+					kfree(new);
+					break;
+				}
+			}
+		}
+
 		opt = rcu_dereference_protected(np->opt,
 						lockdep_sock_is_held(sk));
 		opt = ipv6_renew_options(sk, opt, optname, new);
Signature · 1 line
2.7.4
#6 lore raw

PATCH v2 5/5 ip6tlvs: API to set and remove individual TLVs from DO or HBH EH

In reply to Tom Herbert

Add functions and socket options that allows setting and removing individual TLVs from Hop-by-Hop, Destination, or Routing Header Destination options that are set in txoptions of a socket. When an individual TLV optiosn is set it is merged into the existing options at the position in the list described by preferred order attribute in the TLV parameters table.

This code is based in part on the TLV option handling in calipso.c.

---
 include/net/ipv6.h         |  13 ++
 include/uapi/linux/in6.h   |   9 +
 net/ipv6/exthdrs_options.c | 516 +++++++++++++++++++++++++++++++++++++++++++++
 net/ipv6/ipv6_sockglue.c   |  80 +++++++
 4 files changed, 618 insertions(+)
include/net/ipv6.h +13 -0
index c5692d6..898f5e3 100644
@@ -378,6 +378,8 @@ struct ipv6_txoptions *ipv6_renew_options(struct sock *sk,
 					  struct ipv6_opt_hdr *newopt);
 struct ipv6_txoptions *ipv6_fixup_options(struct ipv6_txoptions *opt_space,
 					  struct ipv6_txoptions *opt);
+int ipv6_opt_update(struct sock *sk, struct ipv6_txoptions *opt,
+		    int which, struct ipv6_opt_hdr *new);
 
 int ipv6_opt_validate_tlvs(struct net *net, struct ipv6_opt_hdr *opt,
 			   unsigned int optname, bool admin);
@@ -386,6 +388,17 @@ int ipv6_opt_validate_single_tlv(struct net *net, unsigned int optname,
 				 bool deleting, bool admin);
 int ipv6_opt_check_perm(struct net *net, struct sock *sk,
 			int optname, bool admin);
+
+int ipv6_opt_tlv_find(struct ipv6_opt_hdr *opt, unsigned char *targ_tlv,
+		      unsigned int *start, unsigned int *end);
+struct ipv6_opt_hdr *ipv6_opt_tlv_insert(struct net *net,
+					 struct ipv6_opt_hdr *opt,
+					 int optname, unsigned char *tlv,
+					 bool admin);
+struct ipv6_opt_hdr *ipv6_opt_tlv_delete(struct net *net,
+					 struct ipv6_opt_hdr *opt,
+					 unsigned char *tlv, bool admin);
+
 struct tlv_tx_param {
 	unsigned char preferred_order;
 	unsigned char admin_perm : 2;
include/uapi/linux/in6.h +9 -0
index a54cf96..f6edf31 100644
@@ -288,6 +288,15 @@ struct in6_flowlabel_req {
 #define IPV6_RECVFRAGSIZE	77
 #define IPV6_FREEBIND		78
 
+/* API to set single Destination or Hop-by-Hop options */
+
+#define IPV6_HOPOPTS_TLV		79
+#define IPV6_RTHDRDSTOPTS_TLV		80
+#define IPV6_DSTOPTS_TLV		81
+#define IPV6_HOPOPTS_DEL_TLV		82
+#define IPV6_RTHDRDSTOPTS_DEL_TLV	83
+#define IPV6_DSTOPTS_DEL_TLV		84
+
 /*
  * Multicast Routing:
  * see include/uapi/linux/mroute6.h.
net/ipv6/exthdrs_options.c +516 -0

528 lines not on this page · raw message

net/ipv6/ipv6_sockglue.c +80 -0

86 lines not on this page · raw message

Signature · 1 line
2.7.4