exthdrs: Make ext. headers & options useful - Part I
Subsystems (3)
v3 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)
- Move generic functions exthdrs_core.c.
- 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.
Tested:
Set Hop-by-Hop options on TCP/UDP socket and verified to be functional.
Tom Herbert (7):
exthdrs: Create exthdrs_options.c
exthdrs: Move generic EH functions to exthdrs_core.c
ipv6: Consolidate option cases in ip6_datagram_send_ctl
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
include/net/ipv6.h | 127 ++++++ include/uapi/linux/in6.h | 49 ++ net/ipv6/Makefile | 2 +- net/ipv6/datagram.c | 89 ++-- net/ipv6/exthdrs.c | 388 ++-------------- net/ipv6/exthdrs_core.c | 1091 ++++++++++++++++++++++++++++++++++++++++++++ net/ipv6/exthdrs_options.c | 347 ++++++++++++++ net/ipv6/ipv6_sockglue.c | 43 +- 8 files changed, 1699 insertions(+), 437 deletions(-) create mode 100644 net/ipv6/exthdrs_options.c
Signature · 1 line
2.7.4
PATCH v3 1/7 exthdrs: Create exthdrs_options.c
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.
--- include/net/ipv6.h | 15 ++++ net/ipv6/Makefile | 2 +- net/ipv6/exthdrs.c | 204 --------------------------------------------- net/ipv6/exthdrs_options.c | 201 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 217 insertions(+), 205 deletions(-) create mode 100644 net/ipv6/exthdrs_options.c
include/net/ipv6.h +15 -0
@@ -379,6 +379,21 @@ struct ipv6_txoptions *ipv6_renew_options(struct sock *sk,struct ipv6_txoptions *ipv6_fixup_options (struct ipv6_txoptions *opt_space,struct ipv6_txoptions *opt); +/* +* 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[]; +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
@@ -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
PATCH v3 2/7 exthdrs: Move generic EH functions to exthdrs_core.c
In reply to Tom HerbertMove generic functions in exthdrs.c to exthdrs_core.c so that exthdrs.c only contains functions that are specific to IPv6 processing, and exthdrs_core.c contains functions that are generic.
--- net/ipv6/exthdrs.c | 138 ----------------------------------------------- net/ipv6/exthdrs_core.c | 139 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 139 insertions(+), 138 deletions(-)
net/ipv6/exthdrs.c +0 -138
@@ -788,144 +788,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.
net/ipv6/exthdrs_core.c +139 -0
142 lines not on this page · raw message
Signature · 1 line
2.7.4
PATCH v3 3/7 ipv6: Consolidate option cases in ip6_datagram_send_ctl
In reply to Tom HerbertConsolidate cases for IPV6_2292HOPOPTS, IPV6_HOPOPTS, IPV6_2292DSTOPTS, IPV6_DSTOPTS, and IPV6_RTHDRDSTOPTS. Most of the work and verifications are common for all these case, individual differences in processing can be implemented with an embedded switch statement.
--- net/ipv6/datagram.c | 66 ++++++++++++++++++----------------------------------- 1 file changed, 22 insertions(+), 44 deletions(-)
net/ipv6/datagram.c +22 -44
@@ -842,49 +842,7 @@ int ip6_datagram_send_ctl(struct net *net, struct sock *sk,case IPV6_2292HOPOPTS :case IPV6_HOPOPTS : -if (opt->hopopt || cmsg->cmsg_len <CMSG_LEN (sizeof (struct ipv6_opt_hdr ))) { - err = -EINVAL ; - goto exit_f; - } - - hdr = (struct ipv6_opt_hdr *)CMSG_DATA (cmsg); - len = ((hdr->hdrlen +1 ) <<3 ); -if (cmsg->cmsg_len <CMSG_LEN (len)) { - err = -EINVAL ; - goto exit_f; - } -if (!ns_capable (net->user_ns,CAP_NET_RAW )) { - err = -EPERM ; - goto exit_f; - } - opt->opt_nflen += len; - opt->hopopt = hdr; -break ; -case IPV6_2292DSTOPTS : -if (cmsg->cmsg_len <CMSG_LEN (sizeof (struct ipv6_opt_hdr ))) { - err = -EINVAL ; - goto exit_f; - } - - hdr = (struct ipv6_opt_hdr *)CMSG_DATA (cmsg); - len = ((hdr->hdrlen +1 ) <<3 ); -if (cmsg->cmsg_len <CMSG_LEN (len)) { - err = -EINVAL ; - goto exit_f; - } -if (!ns_capable (net->user_ns,CAP_NET_RAW )) { - err = -EPERM ; - goto exit_f; - } -if (opt->dst1opt) { - err = -EINVAL ; - goto exit_f; - } - opt->opt_flen += len; - opt->dst1opt = hdr; -break ; -case IPV6_DSTOPTS :case IPV6_RTHDRDSTOPTS :if (cmsg->cmsg_len <CMSG_LEN (sizeof (struct ipv6_opt_hdr ))) {
@@ -902,13 +860,33 @@ int ip6_datagram_send_ctl(struct net *net, struct sock *sk, err = -EPERM ; goto exit_f; } -if (cmsg->cmsg_type ==IPV6_DSTOPTS ) { + +switch (cmsg->cmsg_type) { +case IPV6_2292HOPOPTS : +case IPV6_HOPOPTS : +if (opt->hopopt) { + err = -EINVAL ; + goto exit_f; + } + opt->opt_nflen += len; + opt->hopopt = hdr; +break ; +case IPV6_2292DSTOPTS : +if (opt->dst1opt) { + err = -EINVAL ; + goto exit_f; + } +/* Fallthrough */ +case IPV6_DSTOPTS : opt->opt_flen += len; opt->dst1opt = hdr; - }else { +break ; +case IPV6_RTHDRDSTOPTS : opt->opt_nflen += len; opt->dst0opt = hdr; +break ; } +break ;case IPV6_2292RTHDR :
Signature · 1 line
2.7.4
David Miller
maintainerRe: [PATCH v3 net-next 3/7] ipv6: Consolidate option cases in ip6_datagram_send_ctl
From: Tom Herbert <[email protected]>
Date: Mon, 15 Apr 2019 10:52:16 -0700
net/ipv6/datagram.c int ip6_datagram_send_ctl(struct net *net, struct sock *sk, · line 848
Quoted 6 lines
net/ipv6/datagram.c +0
-4case IPV6_2292HOPOPTS :case IPV6_HOPOPTS : -if (opt->hopopt || cmsg->cmsg_len <CMSG_LEN (sizeof (struct ipv6_opt_hdr ))) { - err = -EINVAL ; - goto exit_f; - }
You're consolidated version of this code checks opt->hopopt after things like ns_capable() thus changing the priorities of the error conditions. You code get -EPERM when previously -EINVAL would have been seen.
I really don't like changes like this, it's so irritating double checking all of the possible user visible side effects in all of these details.
PATCH v3 4/7 exthdrs: Registration of TLV handlers and parameters
In reply to Tom HerbertCreate 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 | 57 ++++++++- include/uapi/linux/in6.h | 10 ++ net/ipv6/exthdrs.c | 46 ++++---- net/ipv6/exthdrs_core.c | 279 +++++++++++++++++++++++++++++++++++++++++++++ net/ipv6/exthdrs_options.c | 64 +++++++---- 5 files changed, 409 insertions(+), 47 deletions(-)
include/net/ipv6.h +52 -5
@@ -386,13 +386,60 @@ 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); + +/* 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
@@ -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 +26 -20
96 lines not on this page · raw message
net/ipv6/exthdrs_core.c +279 -0
282 lines not on this page · raw message
net/ipv6/exthdrs_options.c +42 -22
104 lines not on this page · raw message
Signature · 1 line
2.7.4
PATCH v3 5/7 exthdrs: Add TX parameters
In reply to Tom HerbertDefine 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_core.c | 22 +++++++++++++++++- net/ipv6/exthdrs_options.c | 57 ++++++++++++++++++++++++++++++++++++++++++---- 5 files changed, 108 insertions(+), 7 deletions(-)
include/net/ipv6.h +25 -1
@@ -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
@@ -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
@@ -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_core.c +21 -1
@@ -431,7 +431,18 @@ 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.rx_class =0 , + .params.r.class =0 , + + .params.t.preferred_order =0 , + .params.t.admin_perm =IPV6_TLV_PERM_NO_CHECK , + .params.t.user_perm =IPV6_TLV_PERM_NONE , + .params.t.class =0 , + .params.t.align_mult = (4 -1 ),/* Default alignment: 4n + 2 */ + .params.t.align_off =2 , + .params.t.min_data_len =0 , + .params.t.max_data_len =255 , + .params.t.data_len_mult = (1 -1 ),/* No default length align */ + .params.t.data_len_off =0 , };static size_t tlv_param_table_size (unsigned char count)
@@ -650,10 +661,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 );
@@ -667,6 +681,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 )) {
@@ -680,6 +695,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
PATCH v3 6/7 ip6tlvs: Add netlink interface
In reply to Tom HerbertAdd 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_core.c | 280 +++++++++++++++++++++++++++++++++++++++++++++ net/ipv6/exthdrs_options.c | 81 ++++++++++++- 4 files changed, 408 insertions(+), 2 deletions(-)
include/net/ipv6.h +18 -0
@@ -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
@@ -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_core.c +280 -0
293 lines not on this page · raw message
net/ipv6/exthdrs_options.c +79 -2
100 lines not on this page · raw message
Signature · 1 line
2.7.4
PATCH v3 7/7 ip6tlvs: Validation of TX Destination and Hop-by-Hop options
In reply to Tom HerbertValidate 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 | 27 +++- net/ipv6/exthdrs_core.c | 373 +++++++++++++++++++++++++++++++++++++++++++++++ net/ipv6/ipv6_sockglue.c | 43 ++---- 4 files changed, 434 insertions(+), 32 deletions(-)
include/net/ipv6.h +23 -0
@@ -472,6 +472,20 @@ int exthdrs_init(struct tlv_param_table *tlv_param_table, int num_init_params);void exthdrs_fini (struct tlv_param_table *tlv_param_table); +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 sock *sk,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,
@@ -488,6 +502,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 +23 -4
@@ -844,7 +844,10 @@ int ip6_datagram_send_ctl(struct net *net, struct sock *sk,case IPV6_HOPOPTS :case IPV6_2292DSTOPTS :case IPV6_DSTOPTS : -case IPV6_RTHDRDSTOPTS : +case IPV6_RTHDRDSTOPTS : { +int type =IPV6_DSTOPTS ; +unsigned int max_len, max_cnt; +if (cmsg->cmsg_len <CMSG_LEN (sizeof (struct ipv6_opt_hdr ))) { err = -EINVAL ; goto exit_f;
@@ -856,11 +859,26 @@ 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 ; - goto exit_f; + +if (cmsg->cmsg_type ==IPV6_2292HOPOPTS || + cmsg->cmsg_type ==IPV6_HOPOPTS ) { + type =IPV6_HOPOPTS ; + max_len = net->ipv6.sysctl.max_hbh_opts_len; + max_cnt = net->ipv6.sysctl.max_hbh_opts_cnt; + }else { + type =IPV6_DSTOPTS ; + max_len = net->ipv6.sysctl.max_dst_opts_len; + max_cnt = net->ipv6.sysctl.max_dst_opts_cnt; } + err =ipv6_opt_validate_tlvs (net, &ipv6_tlv_param_table, + hdr, type, +ns_capable (net->user_ns, +CAP_NET_RAW ), + max_len, max_cnt); +if (err <0 ) + goto exit_f; +switch (cmsg->cmsg_type) {case IPV6_2292HOPOPTS :case IPV6_HOPOPTS :
@@ -888,6 +906,7 @@ int ip6_datagram_send_ctl(struct net *net, struct sock *sk, }break ; + }case IPV6_2292RTHDR :case IPV6_RTHDR :
net/ipv6/exthdrs_core.c +373 -0
385 lines not on this page · raw message
net/ipv6/ipv6_sockglue.c +15 -28
54 lines not on this page · raw message
Signature · 1 line
2.7.4