ipv6: Extension header infrastructure
Subsystems (6)
v3 cover ipv6: Extension header infrastructure
This patchset improves the IPv6 extension header infrastructure to make extension headers more usable and scalable.
- Reorganize extension header files to separate out common API components
- Create common TLV handler that will can be used in other use cases (e.g. segment routing TLVs, UDP options)
- 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
v2:
- Fix build errors from missing include file.
v3:
- Fix kbuild issue for ipv6_opt_hdr declared inside parameter list in ipeh.h
Tom Herbert (7):
ipeh: Create exthdrs_options.c and ipeh.h
ipeh: Move generic EH functions to exthdrs_common.c
ipeh: Generic TLV parser
ip6tlvs: Registration of TLV handlers and parameters
ip6tlvs: Add TX parameters
ip6tlvs: Add netlink interface
ip6tlvs: Validation of TX Destination and Hop-by-Hop options
include/net/ipeh.h | 208 ++++++++ include/net/ipv6.h | 12 +- include/uapi/linux/in6.h | 6 + include/uapi/linux/ipeh.h | 53 ++ net/dccp/ipv6.c | 2 +- net/ipv6/Kconfig | 4 + net/ipv6/Makefile | 3 +- net/ipv6/calipso.c | 6 +- net/ipv6/datagram.c | 51 +- net/ipv6/exthdrs.c | 505 ++----------------- net/ipv6/exthdrs_common.c | 1158 ++++++++++++++++++++++++++++++++++++++++++++ net/ipv6/exthdrs_options.c | 342 +++++++++++++ net/ipv6/ipv6_sockglue.c | 39 +- 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 +- 18 files changed, 1881 insertions(+), 518 deletions(-) create mode 100644 include/net/ipeh.h create mode 100644 include/uapi/linux/ipeh.h create mode 100644 net/ipv6/exthdrs_common.c create mode 100644 net/ipv6/exthdrs_options.c
Signature · 1 line
2.7.4
PATCH v3 1/7 ipeh: Create exthdrs_options.c and ipeh.h
From: Tom Herbert <[email protected]>
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.
--- 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
include/net/ipeh.h new file +22 -0
@@ -0,0 +1,22 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _NET_IPEH_H +#define _NET_IPEH_H + +#include <linux/skbuff.h> + +/* +* 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 */
include/net/ipv6.h +1 -0
@@ -20,6 +20,7 @@#include <net/flow_dissector.h> #include <net/snmp.h> #include <net/netns/hash.h> +#include <net/ipeh.h> #define SIN6_LEN_RFC2133 24
net/ipv6/Makefile +1 -1
@@ -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 -204
228 lines not on this page · raw message
net/ipv6/exthdrs_options.c new file +201 -0
201 lines not on this page · raw message
Signature · 1 line
2.7.4
Bowers
PATCH v3 1/7 ipeh: Create exthdrs_options.c and ipeh.h
Quoted 28 lines
-----Original Message-----
From: Intel-wired-lan [mailto:intel-wired-lan-bounces at osuosl.org] On Behalf Of Tom Herbert
Sent: Monday, August 19, 2019 3:44 PM
To: Intel-wired-lan at lists.osuosl.org
Cc: Tom Herbert <[email protected]>; Tom Herbert <[email protected]>
Subject: [Intel-wired-lan] [PATCH v3 net-next 1/7] ipeh: Createexthdrs_options.candipeh.hFrom: Tom Herbert <[email protected]>
Create
exthdrs_options.cto hold code related to specific Hop-by-Hop and Destination extension header options. Move related functions inexthdrs.cto the new file.Create include
net/ipeh.hto contain common definitions for IP extension headers.Signed-off-by Tom Herbert <[email protected]>--- 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
Quoted 1 line
include/net/ipeh.hcreate mode 100644net/ipv6/exthdrs_options.c
PATCH v3 2/7 ipeh: Move generic EH functions to exthdrs_common.c
In reply to Tom HerbertFrom: Tom Herbert <[email protected]>
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_.
Fix kbuild issue.
--- 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
include/net/ipeh.h +12 -0
@@ -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 */
include/net/ipv6.h +0 -9
@@ -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,
net/dccp/ipv6.c +1 -1
@@ -514,7 +514,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 ;
net/ipv6/Kconfig +4 -0
@@ -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).
net/ipv6/Makefile +1 -0
@@ -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)
net/ipv6/calipso.c +3 -3
@@ -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);
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
net/ipv6/ipv6_sockglue.c +1 -1
@@ -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);
net/ipv6/raw.c +1 -1
@@ -888,7 +888,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;
net/ipv6/tcp_ipv6.c +1 -1
@@ -1243,7 +1243,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 ;
net/ipv6/udp.c +1 -1
@@ -1384,7 +1384,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;
net/l2tp/l2tp_ip6.c +1 -1
@@ -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;
net/sctp/ipv6.c +1 -1
@@ -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__); }
Signature · 1 line
2.7.4
Bowers
PATCH v3 2/7 ipeh: Move generic EH functions to exthdrs_common.c
Quoted 39 lines
-----Original Message-----
From: Intel-wired-lan [mailto:intel-wired-lan-bounces at osuosl.org] On Behalf Of Tom Herbert
Sent: Monday, August 19, 2019 3:44 PM
To: Intel-wired-lan at lists.osuosl.org
Cc: Tom Herbert <[email protected]>; Tom Herbert <[email protected]>
Subject: [Intel-wired-lan] [PATCH v3 net-next 2/7] ipeh: Move generic EH functions toexthdrs_common.cFrom: Tom Herbert <[email protected]>
Move generic functions in
exthdrs.cto newexthdrs_common.cso thatexthdrs.conly contains functions that are specific to IPv6 processing, andexthdrs_common.ccontains functions that are generic. These functions include those that will be used with IPv4 extension headers. Generic extension header related functions are prefixed byipeh_.Fix kbuild issue.
Reported-by kbuild test robot <[email protected]>Signed-off-by Tom Herbert <[email protected]>--- 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
Quoted 1 line
net/ipv6/exthdrs_common.c
PATCH v3 3/7 ipeh: Generic TLV parser
In reply to Tom HerbertFrom: Tom Herbert <[email protected]>
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.
--- include/net/ipeh.h | 25 ++++++++ net/ipv6/exthdrs.c | 159 +++++++++++----------------------------------- net/ipv6/exthdrs_common.c | 114 +++++++++++++++++++++++++++++++++ 3 files changed, 177 insertions(+), 121 deletions(-)
include/net/ipeh.h +25 -0
@@ -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 */
net/ipv6/exthdrs.c +38 -121
189 lines not on this page · raw message
net/ipv6/exthdrs_common.c +114 -0
117 lines not on this page · raw message
Signature · 1 line
2.7.4
Bowers
PATCH v3 3/7 ipeh: Generic TLV parser
Quoted 15 lines
-----Original Message-----
From: Intel-wired-lan [mailto:intel-wired-lan-bounces at osuosl.org] On Behalf Of Tom Herbert
Sent: Monday, August 19, 2019 3:44 PM
To: Intel-wired-lan at lists.osuosl.org
Cc: Tom Herbert <[email protected]>; Tom Herbert <[email protected]>
Subject: [Intel-wired-lan] [PATCH v3 net-next 3/7] ipeh: Generic TLV parserFrom: Tom Herbert <[email protected]>
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.
Quoted 7 lines
Signed-off-by Tom Herbert <[email protected]>--- include/net/ipeh.h | 25 ++++++++ net/ipv6/exthdrs.c | 159 +++++++++++----------------------------------- net/ipv6/exthdrs_common.c | 114 +++++++++++++++++++++++++++++++++ 3 files changed, 177 insertions(+), 121 deletions(-)
PATCH v3 4/7 ip6tlvs: Registration of TLV handlers and parameters
In reply to Tom HerbertFrom: Tom Herbert <[email protected]>
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/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
include/net/ipeh.h +100 -7
@@ -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));
include/net/ipv6.h +3 -0
@@ -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,
include/uapi/linux/ipeh.h new file +16 -0
@@ -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 */
net/ipv6/exthdrs.c +11 -3
@@ -47,6 +47,7 @@#ifdef CONFIG_IPV6_SEG6_HMAC #include <net/seg6_hmac.h> #endif +#include <uapi/linux/ipeh.h> #include <linux/uaccess.h>
@@ -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);
net/ipv6/exthdrs_common.c +250 -21
313 lines not on this page · raw message
net/ipv6/exthdrs_options.c +41 -22
107 lines not on this page · raw message
Signature · 1 line
2.7.4
Bowers
PATCH v3 4/7 ip6tlvs: Registration of TLV handlers and parameters
Quoted 43 lines
-----Original Message-----
From: Intel-wired-lan [mailto:intel-wired-lan-bounces at osuosl.org] On Behalf Of Tom Herbert
Sent: Monday, August 19, 2019 3:44 PM
To: Intel-wired-lan at lists.osuosl.org
Cc: Tom Herbert <[email protected]>; Tom Herbert <[email protected]>
Subject: [Intel-wired-lan] [PATCH v3 net-next 4/7] ip6tlvs: Registration of TLV handlers and parametersFrom: Tom Herbert <[email protected]>
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 onlyReceive 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 <[email protected]>--- 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
Quoted 1 line
include/uapi/linux/ipeh.h
PATCH v3 5/7 ip6tlvs: Add TX parameters
In reply to Tom HerbertFrom: Tom Herbert <[email protected]>
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/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(-)
include/net/ipeh.h +18 -0
@@ -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);
include/uapi/linux/ipeh.h +8 -0
@@ -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 */
net/ipv6/exthdrs_common.c +52 -1
93 lines not on this page · raw message
net/ipv6/exthdrs_options.c +45 -0
69 lines not on this page · raw message
Signature · 1 line
2.7.4
Bowers
PATCH v3 5/7 ip6tlvs: Add TX parameters
Quoted 15 lines
-----Original Message-----
From: Intel-wired-lan [mailto:intel-wired-lan-bounces at osuosl.org] On Behalf Of Tom Herbert
Sent: Monday, August 19, 2019 3:44 PM
To: Intel-wired-lan at lists.osuosl.org
Cc: Tom Herbert <[email protected]>; Tom Herbert <[email protected]>
Subject: [Intel-wired-lan] [PATCH v3 net-next 5/7] ip6tlvs: Add TX parametersFrom: Tom Herbert <[email protected]>
Define a number of transmit parameters for TLV Parameter table definitions. These will be used for validating TLVs that are set on a socket.
Quoted 8 lines
Signed-off-by Tom Herbert <[email protected]>--- 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(-)
PATCH v3 6/7 ip6tlvs: Add netlink interface
In reply to Tom HerbertFrom: Tom Herbert <[email protected]>
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/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(-)
include/net/ipeh.h +16 -0
@@ -3,6 +3,7 @@#define _NET_IPEH_H#include <linux/skbuff.h> +#include <net/genetlink.h> /* * 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)
include/net/ipv6.h +1 -0
@@ -14,6 +14,7 @@#include <linux/jhash.h> #include <linux/refcount.h> #include <linux/jump_label_ratelimit.h> +#include <net/genetlink.h> #include <net/if_inet6.h> #include <net/ndisc.h> #include <net/flow.h>
include/uapi/linux/in6.h +6 -0
@@ -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 */
include/uapi/linux/ipeh.h +29 -0
@@ -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 */
net/ipv6/exthdrs_common.c +238 -0
244 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
Bowers
PATCH v3 6/7 ip6tlvs: Add netlink interface
Quoted 17 lines
-----Original Message-----
From: Intel-wired-lan [mailto:intel-wired-lan-bounces at osuosl.org] On Behalf Of Tom Herbert
Sent: Monday, August 19, 2019 3:44 PM
To: Intel-wired-lan at lists.osuosl.org
Cc: Tom Herbert <[email protected]>; Tom Herbert <[email protected]>
Subject: [Intel-wired-lan] [PATCH v3 net-next 6/7] ip6tlvs: Add netlink interfaceFrom: Tom Herbert <[email protected]>
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 <[email protected]>
Quoted 8 lines
--- 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(-)
PATCH v3 7/7 ip6tlvs: Validation of TX Destination and Hop-by-Hop options
In reply to Tom HerbertFrom: Tom Herbert <[email protected]>
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/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(-)
include/net/ipeh.h +22 -0
@@ -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 {
net/ipv6/datagram.c +38 -13
96 lines not on this page · raw message
net/ipv6/exthdrs_common.c +382 -0
394 lines not on this page · raw message
net/ipv6/ipv6_sockglue.c +13 -26
@@ -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 ;
Signature · 1 line
2.7.4
Bowers
PATCH v3 7/7 ip6tlvs: Validation of TX Destination and Hop-by-Hop options
Quoted 34 lines
-----Original Message-----
From: Intel-wired-lan [mailto:intel-wired-lan-bounces at osuosl.org] On Behalf Of Tom Herbert
Sent: Monday, August 19, 2019 3:44 PM
To: Intel-wired-lan at lists.osuosl.org
Cc: Tom Herbert <[email protected]>; Tom Herbert <[email protected]>
Subject: [Intel-wired-lan] [PATCH v3 net-next 7/7] ip6tlvs: Validation of TX Destination and Hop-by-Hop optionsFrom: Tom Herbert <[email protected]>
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.
Quoted 8 lines
Signed-off-by Tom Herbert <[email protected]>--- 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(-)