Skip to contentSkip to conversation

exthdrs: Make ext. headers & options useful - Part I

PATCH Superseded Tom Herbert 1 participant 9 messages Apr 29, 2019

Subsystems (3)

v8 of 9 Superseded Latest is v9 →

lore
Share & tools
lore
#0 lore raw

v8 cover exthdrs: Make ext. headers & options useful - Part I

Extension headers are the mechanism of extensibility for the IPv6 protocol, however to date they have only seen limited deployment. The reasons for that are because intermediate devices don't handle them well, and there haven't really be any useful extension headers defined. In particular, Destination and Hop-by-Hop options have not been deployed to any extent.

The landscape may be changing as there are now a number of serious efforts to define and deploy extension headers. In particular, a number of uses for Hop-by-Hop Options are currently being proposed, Some of these are from router vendors so there is hope that they might start start to fix their brokenness. These proposals include IOAM, Path MTU, Firewall and Service Tickets, SRv6, CRH, etc.

Assuming that IPv6 extension headers gain traction, that leaves a noticeable gap in IPv4 support. IPv4 options have long been considered a non-starter for deployment. An alternative being proposed is to enable use of IPv6 options with IPv4 (draft-herbert-ipv4-eh-00).

This series of patch sets endeavours to make extension headers and related options useful and easy to use. The following items will be addressed:

- Reorganize extension header files
- Allow registration of TLV handlers
- Elaborate on the TLV tables to include more characteristics
- Add a netlink interface to set TLV parameters (such as alignment requirements, authorization to send, etc.)
- Enhance validation of TLVs being sent. Validation is strict (unless overridden by admin) following that sending clause of the robustness principle
- Allow non-privileged users to set Hop-by-Hop and Destination Options if authorized by the admin
- Add an API that allows individual Hop-by-Hop and Destination Options to be set or removed for a connected socket. The backend end enforces permissions on what TLVs may be set and merges set TLVs per following the rules in the TLV parameter table (for instance, TLV parameters include a preferred sending order that merging adheres to)
- Add an infrastructure to allow Hop-by-Hop and Destination Options to be processed in the context of a connected socket
- Support for some of the aforementioned options
- Enable IPv4 extension headers

------

In this series:

- Create exhdrs_options.c. Move options specific processing to this file from exthdrs.c (for RA, Jumbo, Calipso, and HAO).
- Create exthdrs_common.c. This holds generic exthdr and TLV related functions that are not IPv6 specific. These functions will also be used with IPv4 extension headers.
- Allow modules to register TLV handlers for Destination and HBH options.
- Add parameters block to TLV type entries that describe characteristics related to the TLV. For the most part, these encode rules about sending each TLV (TLV alignment requirements for instance).
- Add a netlink interface to manage parameters in the TLV table.
- Add validation of HBH and Destination Options that are set on a socket or in ancillary data in sendmsg. The validation applies the rules that are encoded in the TLV parameters.
- TLV parameters includes permissions that may allow non-privileged users to set specific TLVs on a socket HBH options or Destination options. Strong validation can be enabled for this to constrain what the non-privileged user is able to do.

v2:

- Don't rename extension header files with IPv6 specific code before code for IPv4 extension headers is present
- Added patches for creating TLV parameters and validation

v3:

- Fix kbuild errors. Ensure build and operation when IPv6 is disabled.

v4:

- Remove patch that consolidated option cases in option cases in ip6_datagram_send_ctl per feedback

v5:

- Add signoffs.

v6:

- Fix init_module issue from kuild.

v7:

- Create exthdrs_common.c. Use this file for for generic functions that apply to IPv6 and IPv4 extension headers. Don't touch exthdr_core.c to preserve the semantics that that file contains functions that are needed when IPv6 (or IPv4 extension headers) is not enabled.
- A few other minor fixes and cleanup.
- Answered David Ahern's question about why use generic netlink instead of rtnetlink.

v8:

- Don't explicitly set fields to zero when initializing TLV paramter structures.

Tested:

Set Hop-by-Hop options on TCP/UDP socket and verified to be functional.

Tom Herbert (8):
exthdrs: Move generic EH functions to exthdrs_common.c
exthdrs: Registration of TLV handlers and parameters
exthdrs: Add TX parameters
ip6tlvs: Add netlink interface
ip6tlvs: Validation of TX Destination and Hop-by-Hop options
ipv6tlvs: opt_update function
ipv6tlvs: Infrastructure for manipulating individual TLVs
ipv6tlvs: API for manipuateling TLVs on a connect socket

 include/net/ipv6.h         |  147 +++-
 include/uapi/linux/in6.h   |   58 ++
 net/ipv6/Kconfig           |    4 +
 net/ipv6/Makefile          |    1 +
 net/ipv6/datagram.c        |   51 +-
 net/ipv6/exthdrs.c         |  190 +----
 net/ipv6/exthdrs_common.c  | 1682 ++++++++++++++++++++++++++++++++++++++++++++
 net/ipv6/exthdrs_options.c |  188 ++++-
 net/ipv6/ipv6_sockglue.c   |   82 ++-
 9 files changed, 2179 insertions(+), 224 deletions(-)
 create mode 100644 net/ipv6/exthdrs_common.c
Signature · 1 line
2.7.4
#1 lore raw

PATCH v8 1/8 exthdrs: Move generic EH functions to exthdrs_common.c

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.

---
 net/ipv6/Kconfig          |   4 ++
 net/ipv6/Makefile         |   1 +
 net/ipv6/exthdrs.c        | 138 --------------------------------------------
 net/ipv6/exthdrs_common.c | 144 ++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 149 insertions(+), 138 deletions(-)
 create mode 100644 net/ipv6/exthdrs_common.c
net/ipv6/Kconfig +4 -0
index 613282c..c88fc9b 100644
@@ -2,9 +2,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).
net/ipv6/Makefile +1 -0
index 72bd775..22438ca 100644
@@ -48,6 +48,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)
net/ipv6/exthdrs.c +0 -138

144 lines not on this page · raw message

net/ipv6/exthdrs_common.c new file +144 -0

144 lines not on this page · raw message

Signature · 1 line
2.7.4
#2 lore raw

PATCH v8 2/8 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 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.

---
 include/net/ipv6.h         |  60 +++++++++-
 include/uapi/linux/in6.h   |  10 ++
 net/ipv6/exthdrs.c         |  52 +++++----
 net/ipv6/exthdrs_common.c  | 278 +++++++++++++++++++++++++++++++++++++++++++++
 net/ipv6/exthdrs_options.c |  62 ++++++----
 5 files changed, 415 insertions(+), 47 deletions(-)
include/net/ipv6.h +55 -5
index e36c2c1..bb667ed 100644
@@ -386,13 +386,63 @@ struct ipv6_txoptions *ipv6_fixup_options(struct ipv6_txoptions *opt_space,
  *     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_params {
+	unsigned char rx_class : 3;
+};
+
+struct tlv_proc {
+	struct tlv_ops ops;
+	struct tlv_params params;
+};
+
+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_proc procs[0];
+};
+
+struct tlv_param_table {
+	struct tlv_param_table_data __rcu *data;
+};
+
+extern struct tlv_param_table ipv6_tlv_param_table;
+
+int tlv_set_proc(struct tlv_param_table *tlv_param_table,
+		 unsigned char type, const struct tlv_proc *proc);
+int tlv_unset_proc(struct tlv_param_table *tlv_param_table, unsigned char type);
+int tlv_set_params(struct tlv_param_table *tlv_param_table,
+		   unsigned char type, const struct tlv_params *params);
+int tlv_unset_params(struct tlv_param_table *tlv_param_table,
+		     unsigned char type);
+
+int exthdrs_init(struct tlv_param_table *tlv_param_table,
+		 const struct tlv_proc_init *init_params,
+		 int num_init_params);
+void exthdrs_fini(struct tlv_param_table *tlv_param_table);
+
+int ipv6_exthdrs_options_init(void);
+void ipv6_exthdrs_options_exit(void);
+
+/* tlv_get_proc assumes rcu_read_lock is held */
+static inline struct tlv_proc *tlv_get_proc(
+		struct tlv_param_table *tlv_param_table,
+		unsigned int type)
+{
+	struct tlv_param_table_data *tpt =
+				rcu_dereference(tlv_param_table->data);
+
+	return &tpt->procs[tpt->entries[type]];
+}
 
 bool ipv6_opt_accepted(const struct sock *sk, const struct sk_buff *skb,
 		       const struct inet6_skb_parm *opt);
include/uapi/linux/in6.h +10 -0
index 9f2273a..8b9ac7f 100644
@@ -297,4 +297,14 @@ struct in6_flowlabel_req {
  * ...
  * MRT6_MAX
  */
+
+/* Flags for EH type that can use a TLV option */
+#define IPV6_TLV_CLASS_FLAG_HOPOPT	BIT(0)
+#define IPV6_TLV_CLASS_FLAG_RTRDSTOPT	BIT(1)
+#define IPV6_TLV_CLASS_FLAG_DSTOPT	BIT(2)
+#define IPV6_TLV_CLASS_MAX		((1 << 3) - 1)
+
+#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 +32 -20

116 lines not on this page · raw message

net/ipv6/exthdrs_common.c +278 -0

281 lines not on this page · raw message

net/ipv6/exthdrs_options.c +40 -22

102 lines not on this page · raw message

Signature · 1 line
2.7.4
#3 lore raw

PATCH v8 3/8 exthdrs: Add TX parameters

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

---
 include/net/ipv6.h         | 26 ++++++++++++++++++++-
 include/uapi/linux/in6.h   |  8 +++++++
 net/ipv6/exthdrs.c         |  2 +-
 net/ipv6/exthdrs_common.c  | 17 ++++++++++++++
 net/ipv6/exthdrs_options.c | 57 ++++++++++++++++++++++++++++++++++++++++++----
 5 files changed, 104 insertions(+), 6 deletions(-)
include/net/ipv6.h +25 -1
index bb667ed..4cebc48 100644
@@ -390,8 +390,26 @@ struct tlv_ops {
 	bool	(*func)(unsigned int class, struct sk_buff *skb, int offset);
 };
 
+struct tlv_rx_params {
+	unsigned char class : 3;
+};
+
+struct tlv_tx_params {
+	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;
+	unsigned char preferred_order;
+};
+
 struct tlv_params {
-	unsigned char rx_class : 3;
+	struct tlv_rx_params r;
+	struct tlv_tx_params t;
 };
 
 struct tlv_proc {
@@ -417,6 +435,12 @@ struct tlv_param_table {
 
 extern struct tlv_param_table ipv6_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
+
 int tlv_set_proc(struct tlv_param_table *tlv_param_table,
 		 unsigned char type, const struct tlv_proc *proc);
 int tlv_unset_proc(struct tlv_param_table *tlv_param_table, unsigned char type);
include/uapi/linux/in6.h +8 -0
index 8b9ac7f..6a99ee1 100644
@@ -307,4 +307,12 @@ struct in6_flowlabel_req {
 #define IPV6_TLV_CLASS_ANY_DSTOPT      (IPV6_TLV_CLASS_FLAG_RTRDSTOPT | \
 					IPV6_TLV_CLASS_FLAG_DSTOPT)
 
+/* 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
+};
+
 #endif /* _UAPI_LINUX_IN6_H */
net/ipv6/exthdrs.c +1 -1
index 71a12a7..92a777f 100644
@@ -166,7 +166,7 @@ static bool ip6_parse_tlv(unsigned int class, struct sk_buff *skb,
 				goto bad;
 
 			curr = tlv_get_proc(&ipv6_tlv_param_table, nh[off]);
-			if ((curr->params.rx_class & class) && curr->ops.func) {
+			if ((curr->params.r.class & class) && curr->ops.func) {
 				/* Handler will apply additional checks to
 				 * the TLV
 				 */
net/ipv6/exthdrs_common.c +17 -0
index 0c0e32d..cda4fb8 100644
@@ -155,6 +155,14 @@ static void tlv_param_table_release(struct rcu_head *rcu)
 
 /* Default (unset) values for TLV parameters */
 static const struct tlv_proc tlv_default_proc = {
+	.params.t = {
+		.preferred_order = 0,
+		.admin_perm = IPV6_TLV_PERM_NO_CHECK,
+		.user_perm = IPV6_TLV_PERM_NONE,
+		.align_mult = (4 - 1), /* Default alignment: 4n + 2 */
+		.align_off = 2,
+		.max_data_len = 255,
+	},
 };
 
 static size_t tlv_param_table_size(unsigned char count)
@@ -373,10 +381,13 @@ int exthdrs_init(struct tlv_param_table *tlv_param_table,
 		 const struct tlv_proc_init *tlv_init_params,
 		 int num_init_params)
 {
+	unsigned long check_map[BITS_TO_LONGS(256)];
 	struct tlv_param_table_data *tpt;
 	size_t tsize;
 	int i;
 
+	memset(check_map, 0, sizeof(check_map));
+
 	tsize = tlv_param_table_size(num_init_params + 1);
 
 	tpt = kvmalloc(tsize, GFP_KERNEL);
@@ -390,6 +401,7 @@ int exthdrs_init(struct tlv_param_table *tlv_param_table,
 
 	for (i = 0; i < num_init_params; i++) {
 		const struct tlv_proc_init *tpi = &tlv_init_params[i];
+		unsigned int order = tpi->proc.params.t.preferred_order;
 		struct tlv_proc *tp = &tpt->procs[i + 1];
 
 		if (WARN_ON(tpi->type < 2)) {
@@ -403,6 +415,11 @@ int exthdrs_init(struct tlv_param_table *tlv_param_table,
 			return -EINVAL;
 		}
 
+		if (order) {
+			WARN_ON(test_bit(order, check_map));
+			set_bit(order, check_map);
+		}
+
 		*tp = tpi->proc;
 		tpt->entries[tpi->type] = i + 1;
 	}
net/ipv6/exthdrs_options.c +53 -4

79 lines not on this page · raw message

Signature · 1 line
2.7.4
#4 lore raw

PATCH v8 4/8 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/net/ipv6.h         |  18 +++
 include/uapi/linux/in6.h   |  31 +++++
 net/ipv6/exthdrs_common.c  | 278 +++++++++++++++++++++++++++++++++++++++++++++
 net/ipv6/exthdrs_options.c |  81 ++++++++++++-
 4 files changed, 406 insertions(+), 2 deletions(-)
include/net/ipv6.h +18 -0
index 4cebc48..edf718f 100644
@@ -17,6 +17,7 @@
 #include <linux/hardirq.h>
 #include <linux/jhash.h>
 #include <linux/refcount.h>
+#include <net/genetlink.h>
 #include <net/if_inet6.h>
 #include <net/ndisc.h>
 #include <net/flow.h>
@@ -449,6 +450,23 @@ int tlv_set_params(struct tlv_param_table *tlv_param_table,
 int tlv_unset_params(struct tlv_param_table *tlv_param_table,
 		     unsigned char type);
 
+extern const struct nla_policy tlv_nl_policy[];
+
+int 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 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 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 tlv_fill_info(struct tlv_param_table *tlv_param_table,
+		  int tlv_type, struct sk_buff *msg, bool admin);
+int tlv_nl_dump(struct tlv_param_table *tlv_param_table,
+		struct genl_family *tlv_nl_family,
+		struct sk_buff *skb, struct netlink_callback *cb);
+
 int exthdrs_init(struct tlv_param_table *tlv_param_table,
 		 const struct tlv_proc_init *init_params,
 		 int num_init_params);
include/uapi/linux/in6.h +31 -0
index 6a99ee1..1c79361 100644
@@ -306,6 +306,37 @@ struct in6_flowlabel_req {
 
 #define IPV6_TLV_CLASS_ANY_DSTOPT      (IPV6_TLV_CLASS_FLAG_RTRDSTOPT | \
 					IPV6_TLV_CLASS_FLAG_DSTOPT)
+/* 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 {
net/ipv6/exthdrs_common.c +278 -0

284 lines not on this page · raw message

net/ipv6/exthdrs_options.c +79 -2

98 lines not on this page · raw message

Signature · 1 line
2.7.4
#5 lore raw

PATCH v8 5/8 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 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.

---
 include/net/ipv6.h        |  23 +++
 net/ipv6/datagram.c       |  51 +++++--
 net/ipv6/exthdrs_common.c | 376 ++++++++++++++++++++++++++++++++++++++++++++++
 net/ipv6/ipv6_sockglue.c  |  39 ++---
 4 files changed, 450 insertions(+), 39 deletions(-)
include/net/ipv6.h +23 -0
index edf718f..8c19c6f 100644
@@ -475,6 +475,20 @@ void exthdrs_fini(struct tlv_param_table *tlv_param_table);
 int ipv6_exthdrs_options_init(void);
 void ipv6_exthdrs_options_exit(void);
 
+int ipv6_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 ipv6_opt_validate_single_tlv(struct net *net,
+				 struct tlv_param_table *tlv_param_table,
+				 unsigned int optname,
+				 unsigned char *tlv, size_t len,
+				 bool deleting, bool admin);
+int ipv6_opt_check_perm(struct net *net,
+			struct tlv_param_table *tlv_param_table,
+			struct ipv6_txoptions *txopt, int optname, bool admin);
+
 /* tlv_get_proc assumes rcu_read_lock is held */
 static inline struct tlv_proc *tlv_get_proc(
 		struct tlv_param_table *tlv_param_table,
@@ -491,6 +505,15 @@ bool ipv6_opt_accepted(const struct sock *sk, const struct sk_buff *skb,
 struct ipv6_txoptions *ipv6_update_options(struct sock *sk,
 					   struct ipv6_txoptions *opt);
 
+struct ipv6_txoptions *txoptions_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);
+
 static inline bool ipv6_accept_ra(struct inet6_dev *idev)
 {
 	/* If forwarding is enabled, RA are not accepted unless the special
net/ipv6/datagram.c +38 -13

96 lines not on this page · raw message

net/ipv6/exthdrs_common.c +376 -0

388 lines not on this page · raw message

net/ipv6/ipv6_sockglue.c +13 -26
index 40f21fe..5045818 100644
@@ -398,39 +398,26 @@ static int do_ipv6_setsockopt(struct sock *sk, int level, int optname,
 	case IPV6_DSTOPTS:
 	{
 		struct ipv6_txoptions *opt;
-		struct ipv6_opt_hdr *new = NULL;
+		unsigned int max_len = -1U, max_cnt = -1U;
 
-		/* 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 = ipv6_renew_options(sk, opt, optname, new);
-		kfree(new);
+		opt = txoptions_from_opt(sk, &ipv6_tlv_param_table, opt,
+					 optname, optval, optlen, max_len,
+					 max_cnt);
+
 		if (IS_ERR(opt)) {
 			retv = PTR_ERR(opt);
 			break;
Signature · 1 line
2.7.4
#6 lore raw

PATCH v8 6/8 ipv6tlvs: opt_update function

In reply to Tom Herbert

Add a utility function to replace socket's options with a new set.

---
 include/net/ipv6.h       |  2 ++
 net/ipv6/ipv6_sockglue.c | 16 ++++++++++++++++
 2 files changed, 18 insertions(+)
include/net/ipv6.h +2 -0
index 8c19c6f..a8c1e6c 100644
@@ -379,6 +379,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);
 
 /*
  *     Parsing tlv encoded headers.
net/ipv6/ipv6_sockglue.c +16 -0
index 5045818..b8ef0ea 100644
@@ -118,6 +118,22 @@ struct ipv6_txoptions *ipv6_update_options(struct sock *sk,
 	return opt;
 }
 
+int ipv6_opt_update(struct sock *sk, struct ipv6_txoptions *opt,
+		    int which, struct ipv6_opt_hdr *new)
+{
+	opt = ipv6_renew_options(sk, opt, which, new);
+	if (IS_ERR(opt))
+		return PTR_ERR(opt);
+
+	opt = ipv6_update_options(sk, opt);
+	if (opt) {
+		atomic_sub(opt->tot_len, &sk->sk_omem_alloc);
+		txopt_put(opt);
+	}
+
+	return 0;
+}
+
 static bool setsockopt_needs_rtnl(int optname)
 {
 	switch (optname) {
Signature · 1 line
2.7.4
#7 lore raw

PATCH v8 7/8 ipv6tlvs: Infrastructure for manipulating individual TLVs

In reply to Tom Herbert

Add utility functions in exthdrs_common.c to manipulate individual TVLs in Hop-by-Hop or Destination Options. This includes functions to find, insert, and delete in singleton TLV from on IPv6 options header.

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

---
 include/net/ipv6.h        |  13 ++
 net/ipv6/exthdrs_common.c | 519 ++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 532 insertions(+)
include/net/ipv6.h +13 -0
index a8c1e6c..bf6e593f 100644
@@ -491,6 +491,19 @@ int ipv6_opt_check_perm(struct net *net,
 			struct tlv_param_table *tlv_param_table,
 			struct ipv6_txoptions *txopt, int optname, bool admin);
 
+int ipv6_opt_tlv_find(struct tlv_param_table *tlv_param_table,
+		      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 tlv_param_table *tlv_param_table,
+				struct ipv6_opt_hdr *opt,
+				int optname, unsigned char *tlv,
+				bool admin);
+struct ipv6_opt_hdr *ipv6_opt_tlv_delete(struct net *net,
+				struct tlv_param_table *tlv_param_table,
+				struct ipv6_opt_hdr *opt,
+				unsigned char *tlv, bool admin);
+
 /* tlv_get_proc assumes rcu_read_lock is held */
 static inline struct tlv_proc *tlv_get_proc(
 		struct tlv_param_table *tlv_param_table,
net/ipv6/exthdrs_common.c +519 -0

525 lines not on this page · raw message

Signature · 1 line
2.7.4
#8 lore raw

PATCH v8 8/8 ipv6tlvs: API for manipuateling TLVs on a connect socket

In reply to Tom Herbert

This patch provides an interface for adding a deleting individual Hop-by-Hop or Destination Options on a socket.

The following IPv6 socket options are created:
IPV6_HOPOPTS_TLV
IPV6_RTHDRDSTOPTS_TLV
IPV6_DSTOPTS_TLV
IPV6_HOPOPTS_DEL_TLV
IPV6_RTHDRDSTOPTS_DEL_TLV
IPV6_DSTOPTS_DEL_TLV

The function txoptions_from_tlv_opt does the heavy lifting to copy an options from userspace, validate the option, and call insert of delete TLV function.

---
 include/net/ipv6.h        |  7 +++++
 include/uapi/linux/in6.h  |  9 ++++++
 net/ipv6/exthdrs_common.c | 70 +++++++++++++++++++++++++++++++++++++++++++++++
 net/ipv6/ipv6_sockglue.c  | 27 ++++++++++++++++++
 4 files changed, 113 insertions(+)
include/net/ipv6.h +7 -0
index bf6e593f..6aa4d8f 100644
@@ -529,6 +529,13 @@ struct ipv6_txoptions *txoptions_from_opt(struct sock *sk,
 					  unsigned int max_len,
 					  unsigned int max_cnt);
 
+struct ipv6_opt_hdr *txoptions_from_tlv_opt(struct sock *sk,
+					    struct tlv_param_table
+						*tlv_param_table,
+					    struct ipv6_txoptions *opt,
+					    int optname, char __user *optval,
+					    unsigned int optlen, int *which);
+
 static inline bool ipv6_accept_ra(struct inet6_dev *idev)
 {
 	/* If forwarding is enabled, RA are not accepted unless the special
include/uapi/linux/in6.h +9 -0
index 1c79361..018fc6f 100644
@@ -289,6 +289,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_common.c +70 -0

76 lines not on this page · raw message

net/ipv6/ipv6_sockglue.c +27 -0
index b8ef0ea..3e4c0eb 100644
@@ -476,6 +476,33 @@ static int do_ipv6_setsockopt(struct sock *sk, int level, int optname,
 		break;
 	}
 
+	case IPV6_HOPOPTS_TLV:
+	case IPV6_RTHDRDSTOPTS_TLV:
+	case IPV6_DSTOPTS_TLV:
+	case IPV6_HOPOPTS_DEL_TLV:
+	case IPV6_RTHDRDSTOPTS_DEL_TLV:
+	case IPV6_DSTOPTS_DEL_TLV:
+	{
+		struct ipv6_txoptions *opt;
+		struct ipv6_opt_hdr *new;
+		int which;
+
+		opt = rcu_dereference_protected(np->opt,
+						lockdep_sock_is_held(sk));
+		new = txoptions_from_tlv_opt(sk, &ipv6_tlv_param_table, opt,
+					     optname, optval, optlen, &which);
+		if (IS_ERR(new)) {
+			retv = PTR_ERR(new);
+			break;
+		}
+
+		retv = ipv6_opt_update(sk, opt, which, new);
+
+		kfree(new);
+
+		break;
+	}
+
 	case IPV6_PKTINFO:
 	{
 		struct in6_pktinfo pkt;
Signature · 1 line
2.7.4