calipso: Set the calipso socket label to match the secattr.
Subsystems (5)
RFC PATCH v2 10/18 calipso: Set the calipso socket label to match the secattr.
CALIPSO is a hop-by-hop IPv6 option. A lot of this patch is based on the equivalent CISPO code. The main difference is due to manipulating the options in the hop-by-hop header.
--- include/net/ipv6.h | 2 + include/net/netlabel.h | 9 + include/uapi/linux/in6.h | 1 + net/ipv6/calipso.c | 592 ++++++++++++++++++++++++++++++++++++++++ net/ipv6/ipv6_sockglue.c | 1 - net/netlabel/Kconfig | 1 + net/netlabel/netlabel_calipso.c | 64 +++++ net/netlabel/netlabel_calipso.h | 5 + net/netlabel/netlabel_kapi.c | 64 ++++- security/selinux/netlabel.c | 2 +- 10 files changed, 730 insertions(+), 11 deletions(-)
include/net/ipv6.h +2 -0
@@ -315,6 +315,8 @@ struct ipv6_txoptions *ipv6_fixup_options(struct ipv6_txoptions *opt_space,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, +struct ipv6_txoptions *opt);static inline bool ipv6_accept_ra (struct inet6_dev *idev) {
include/net/netlabel.h +9 -0
@@ -226,6 +226,9 @@ struct netlbl_lsm_secattr {* @doi_getdef: returns a reference to a DOI * @doi_putdef: releases a reference of a DOI * @doi_walk: enumerate the DOI list +* @sock_getattr: retrieve the socket's attr +* @sock_setattr: set the socket's attr +* @sock_delattr: remove the socket's attr * * Description: * This structure is filled out by the CALIPSO engine and passed
@@ -243,6 +246,12 @@ struct netlbl_calipso_ops {int (*doi_walk)(u32 *skip_cnt,int (*callback)(struct calipso_doi *doi_def,void *arg),void *cb_arg); +int (*sock_getattr)(struct sock *sk, +struct netlbl_lsm_secattr *secattr); +int (*sock_setattr)(struct sock *sk, +const struct calipso_doi *doi_def, +const struct netlbl_lsm_secattr *secattr); +void (*sock_delattr)(struct sock *sk); };/*
include/uapi/linux/in6.h +1 -0
@@ -143,6 +143,7 @@ struct in6_flowlabel_req {#define IPV6_TLV_PAD1 0#define IPV6_TLV_PADN 1#define IPV6_TLV_ROUTERALERT 5 +#define IPV6_TLV_CALIPSO 7/* RFC 5570 */ #define IPV6_TLV_JUMBO 194#define IPV6_TLV_HAO 201/* home address option */
net/ipv6/calipso.c +592 -0
610 lines not on this page · raw message
net/ipv6/ipv6_sockglue.c +0 -1
@@ -98,7 +98,6 @@ int ip6_ra_control(struct sock *sk, int sel)return 0 ; } -static struct ipv6_txoptions *ipv6_update_options (struct sock *sk,struct ipv6_txoptions *opt) {
net/netlabel/Kconfig +1 -0
@@ -5,6 +5,7 @@ config NETLABEL bool "NetLabel subsystem support" depends on SECURITY + select CRC_CCITT if IPV6 default n ---help--- NetLabel provides support for explicit network packet labeling
net/netlabel/netlabel_calipso.c +64 -0
67 lines not on this page · raw message
net/netlabel/netlabel_calipso.h +5 -0
@@ -130,5 +130,10 @@ void calipso_doi_putdef(struct calipso_doi *doi_def);int calipso_doi_walk (u32 *skip_cnt,int (*callback)(struct calipso_doi *doi_def,void *arg),void *cb_arg); +int calipso_sock_getattr (struct sock *sk,struct netlbl_lsm_secattr *secattr); +int calipso_sock_setattr (struct sock *sk, +const struct calipso_doi *doi_def, +const struct netlbl_lsm_secattr *secattr); +void calipso_sock_delattr (struct sock *sk);#endif
net/netlabel/netlabel_kapi.c +55 -9
124 lines not on this page · raw message
security/selinux/netlabel.c +1 -1
@@ -333,7 +333,7 @@ int selinux_netlbl_socket_post_create(struct sock *sk, u16 family)struct sk_security_struct *sksec = sk->sk_security;struct netlbl_lsm_secattr *secattr; -if (family !=PF_INET ) +if (family !=PF_INET && family !=PF_INET6 )return 0 ; secattr =selinux_netlbl_sock_genattr (sk);
Signature · 1 line
1.8.0
Quoted 5 lines · Huw Davies
CALIPSO is a hop-by-hop IPv6 option. A lot of this patch is based on the equivalent CISPO code. The main difference is due to manipulating the options in the hop-by-hop header.
Signed-off-by Huw Davies <[email protected]>
...
Quoted 23 lines
net/ipv6/calipso.c +11
-0@@ -44,6 +44,17 @@#include <linux/atomic.h> #include <linux/bug.h> #include <asm/unaligned.h> +#include <linux/crc-ccitt.h> + +/* Maximium size of the calipso option including +* the two-byte TLV header. +*/ +#define CALIPSO_OPT_LEN_MAX (2 + 252) + +/* Size of the minimum calipso option including +* the two-byte TLV header. +*/ +#define CALIPSO_HDR_LEN (2 + 8)/* List of available DOI definitions */ static DEFINE_SPINLOCK (calipso_doi_list_lock);
net/ipv6/calipso.c doi_walk_return: · line 315
Quoted 8 lines
net/ipv6/calipso.c +5
-0@@ -297,6 +308,584 @@ doi_walk_return:return ret_val; } +/** +* calipso_map_cat_hton - Perform a category mapping from host to network +* @doi_def: the DOI definition +* @secattr: the security attributes +* @net_cat: the zero'd out category bitmap in network/CIPSO format
I think that should be CALIPSO ;)
I didn't notice that mistake anywhere else but it is very possible I missed it; a quick 'grep -i "cipso"' on your patches might be a good idea.
Quoted 16 lines
net/ipv6/calipso.c +10
-0+/** +* calipso_genopt - Generate a CALIPSO option +* @buf: the option buffer +* @start: offset from which to write +* @buf_len: the size of opt_buf +* @doi_def: the CALIPSO DOI to use +* @secattr: the security attributes +* +* Description: +* Generate a CALIPSO option using the DOI definition and security attributes + * passed to the function. This also generates upto three bytes of leading + * padding that ensures that the option is 4n + 2 aligned. It returns the + * number of bytes written (including any initial padding).
+ */net/ipv6/calipso.c +2
-0+static int calipso_genopt(unsigned char *buf, u32 start, u32 buf_len, + const struct calipso_doi *doi_def,
net/ipv6/calipso.c doi_walk_return: · line 450
Quoted 10 lines
net/ipv6/calipso.c +10
-0+ const struct netlbl_lsm_secattr *secattr) +{ +int ret_val; +u32 len, pad; +u16 crc; +static const unsigned char padding[4 ] = {2 ,1 ,0 ,3 }; +unsigned char *calipso; + +/* CALIPSO has 4n + 2 alignment */ + pad = padding[start %4 ];
It's probably quicker to use a bitmask than a modulus operation.
Quoted 10 lines
net/ipv6/calipso.c +10
-0+if (buf_len <= start + pad +CALIPSO_HDR_LEN ) +return -ENOSPC ; + +if ((secattr->flags &NETLBL_SECATTR_MLS_LVL ) ==0 ) +return -EPERM ; + + len =CALIPSO_HDR_LEN ; + +if (secattr->flags &NETLBL_SECATTR_MLS_CAT ) { + ret_val = calipso_map_cat_hton(doi_def,
net/ipv6/calipso.c doi_walk_return: · line 470
Quoted 10 lines
net/ipv6/calipso.c +10
-0+ secattr, + buf + start + pad + len, + buf_len - start - pad - len); +if (ret_val <0 ) +return ret_val; + len += ret_val; + } + +calipso_pad_write (buf, start, pad); + calipso = buf + start + pad;
Help me understand why we would need to pad before the CALIPSO option? Assuming any preceding options are properly aligned, or there are no preceding options at all, this should never be needed, yes?
net/ipv6/calipso.c doi_walk_return: · line 478
Quoted 7 lines
net/ipv6/calipso.c +7
-0+ calipso[0 ] =IPV6_TLV_CALIPSO ; + calipso[1 ] = len -2 ; + *(__be32 *)(calipso +2 ) =htonl (doi_def->doi); + calipso[6 ] = (len -CALIPSO_HDR_LEN ) /4 ; + calipso[7 ] = secattr->attr.mls.lvl, + crc =crc_ccitt (0xffff , calipso, len); + crc = ~crc;
Why not just "crc = ~crc_ccitt(...);"?
net/ipv6/calipso.c doi_walk_return: · line 480
Quoted 2 lines
net/ipv6/calipso.c +2
-0+ calipso[8 ] = crc &0xff ; + calipso[9 ] = (crc >>8 ) &0xff ;
Do we need to convert the checksum to network byte order?
Quoted 22 lines
net/ipv6/calipso.c +22
-0+return pad + len; +} + +/* Hop-by-hop hdr helper functions +*/ + +/** +* calipso_opt_update - Replaces socket's hop options with a new set +* @sk: the socket +* @hop: new hop options +* +* Description: +* Replaces @sk's hop options with @hop. @hop may be NULL to leave +* the socket with no hop options. +* +*/ +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_kern (sk, old,IPV6_HOPOPTS , + hop, hop ?ipv6_optlen (hop) :0 );
net/ipv6/calipso.c doi_walk_return: · line 512
Quoted 10 lines
net/ipv6/calipso.c +10
-0+txopt_put (old); +if (IS_ERR (txopts)) +return PTR_ERR (txopts); + + txopts =ipv6_update_options (sk, txopts); + +if (txopts) { +atomic_sub (txopts->tot_len, &sk->sk_omem_alloc); +txopt_put (txopts); + }
Vertical whitespace between the txopts assignment and the if conditional.
Quoted 6 lines
net/ipv6/calipso.c +6
-0+return 0 ; +} + +/** +* calipso_tlv_len - Returns the length of the TLV. +* @tlv: the TLV
net/ipv6/calipso.c doi_walk_return: · line 529
Quoted 10 lines
net/ipv6/calipso.c +10
-0+* +* Description: +* Returns the length of the provided TLV option. +*/ +static int calipso_tlv_len (unsigned char *tlv) +{ +if (tlv[0 ] ==IPV6_TLV_PAD1 ) +return 1 ; +return tlv[1 ] +2 ; +}
Perhaps rename this to calipso_opt_len() and change the argument from tlv to opt?
There may also be a desire to make this a generic ipv6 function; to be honest I'm surprised there isn't one to do this already.
Quoted 19 lines
net/ipv6/calipso.c +19
-0+/** +* calipso_opt_find - Finds the CALIPSO option in an IPv6 hop ... +* @hop: the hop options header +* @start: on return holds the offset of any leading padding +* @end: on return holds the offset of the first non-pad TLV after CALIPSO +* +* Description: +* Finds the space occupied by a CALIPSO option (including any leading and +* trailing padding). +* +* If a CALIPSO option exists set @start and @end to the +* offsets within @hop of the start of padding before the first +* CALIPSO option and the end of padding after the first CALIPSO +* option. In this case the function returns 0. +* +* In the absence of a CALIPSO option, @start and @end will be +* set to the start and end of any trailing padding in the header. +* This is useful when appending a new option, as the caller may want +* to overwrite some of this padding. In this case the function will
net/ipv6/calipso.c doi_walk_return: · line 559
Quoted 10 lines
net/ipv6/calipso.c +10
-0+* return -ENOENT. +*/ +static int calipso_opt_find (struct ipv6_opt_hdr *hop,unsigned int *start, +unsigned int *end) +{ +unsigned int opt_len =ipv6_optlen (hop), offset; +unsigned char *p; +bool found = false, found_next = false; + + *start = *end =0 ;
Ungh. I'm being very, very nit-picky here but I never liked this double assignment style.
net/ipv6/calipso.c doi_walk_return: · line 561
Quoted 1 line
net/ipv6/calipso.c +1
-0+ p = (unsigned char *)hop;
It seems like "opts" or something similar would be more descriptive than "p".
net/ipv6/calipso.c doi_walk_return: · line 565
Quoted 4 lines
net/ipv6/calipso.c +4
-0+ offset =2 ; +while (offset < opt_len) { +u8 val_type = p[offset]; +u8 val_len =calipso_tlv_len (p + offset);
Be consistent, "p[offset]" and not "p + offset" please.
Quoted 25 lines
net/ipv6/calipso.c +25
-0+ +if (offset + val_len > opt_len) +return -EINVAL ; +switch (val_type) { +case IPV6_TLV_CALIPSO : +if (found) { + found_next = true; +break ; + } + found = true; +if (!*start) + *start = offset; + *end = offset + val_len; +break ; +case IPV6_TLV_PAD1 : +case IPV6_TLV_PADN : +if (!found && !*start) + *start = offset; +if (found && !found_next) + *end = offset + val_len; +break ; +default : +if (!found) + *start =0 ; +else
net/ipv6/calipso.c doi_walk_return: · line 600
Quoted 10 lines
net/ipv6/calipso.c +10
-0+ found_next = true; + } + offset += val_len; + } +if (!*start) + *start = opt_len; +if (!*end) + *end = opt_len; + +return found ?0 : -ENOENT ;
Okay, this whole function seems unnecessarily complex. I understand that some of this is due to the tracking of the padding, but what about something like what is shown below? My apologies if I missed some subtle behavior in the original code.
int calipso_opt_find(struct ipv6_opt_hdr *hop,
unsigned int *start,
unsigned int *end)
{
int rc = -ENOENT;
unsigned int opt_len;
unsigned int spot, spot_s, spot_e;
unsigned char *opt = hop;
opt_len = ipv6_optlen(hop);
while (opt_iter <= opt_len) {
switch (opt[spot]) {
case IPV6_TLV_PAD1:
case IPv6_TLV_PADN:
if (spot_e)
spot_e = spot;
break;
case IPV6_TLV_CALIPSO:
rc = 0;
spot_e = spot;
break;
default:
if (spot_e == 0)
spot_s = spot;
else
goto out;
}
spot += calipso_tlv_len(opt[spot]);
}out:
if (spot_s) *start = spot_s + calipso_tlv_len(opt[spot_s]); else *start = 0; if (spot_e) *end = spot_e + calipso_tlv_len(opt[spot_e]); else *end = opt_len; return rc; }
Quoted 8 lines
net/ipv6/calipso.c +8
-0+/** +* calipso_opt_insert - Inserts a CALIPSO option into an IPv6 hop opt hdr. +* @old: the original hop options header +* @doi_def: the CALIPSO DOI to use +* @secattr: the specific security attributes of the socket +* +* Description: +* Creates a new hop options header based on @old with a
net/ipv6/calipso.c doi_walk_return: · line 620
Quoted 10 lines
net/ipv6/calipso.c +10
-0+* CALIPSO option added to it. If @old already contains a CALIPSO +* option this is overwritten, otherwise the new option is appended +* after any existing options. If @old is NULL then the new header +* will contain just the CALIPSO option and any needed padding. +* +*/ +static struct ipv6_opt_hdr * +calipso_opt_insert (struct ipv6_opt_hdr *old, +const struct calipso_doi *doi_def, +const struct netlbl_lsm_secattr *secattr)
How about renaming "old" to "hop"?
Quoted 28 lines
net/ipv6/calipso.c +28
-0+{ +unsigned int start, end, next_opt, buf_len, pad; +struct ipv6_opt_hdr *new; +int ret_val; + +if (old) { + ret_val =calipso_opt_find (old, &start, &end); +if (ret_val && ret_val != -ENOENT ) +return ERR_PTR (ret_val); +if (end !=ipv6_optlen (old)) + next_opt = end; +else + next_opt =0 ; + buf_len =ipv6_optlen (old) - ((end - start) & ~7 ) + +CALIPSO_OPT_LEN_MAX ; + }else { + start =0 ; + next_opt =0 ; + buf_len =sizeof (*old) +CALIPSO_OPT_LEN_MAX ; + } + + new =kzalloc (buf_len,GFP_ATOMIC ); +if (!new) +return ERR_PTR (-ENOMEM ); + +if (start >2 ) +memcpy (new, old, start); + ret_val = calipso_genopt((unsigned char *)new, start, buf_len, doi_def,
net/ipv6/calipso.c doi_walk_return: · line 658
Quoted 10 lines
net/ipv6/calipso.c +10
-0+ secattr); +if (ret_val <0 ) +return ERR_PTR (ret_val); + + end = start + ret_val; + +if (WARN_ON_ONCE (end &3 )) { +kfree (new); +return ERR_PTR (-EINVAL ); + }
Vertical whitespace.
Also, considering the code in calipso_genopt(), are we ever going to see the buffer not properly sized? In other words, beyond early development, is there any chance of the WARN_ON_ONCE ever triggering?
net/ipv6/calipso.c doi_walk_return: · line 668
Quoted 9 lines
net/ipv6/calipso.c +9
-0+ pad = ((end &7 ) + (next_opt &7 )) &7 ; + +calipso_pad_write ((unsigned char *)new, end, pad); + buf_len = end + pad; + +if (next_opt) { +memcpy ((char *)new + end + pad, (char *)old + next_opt, +ipv6_optlen (old) - next_opt); + buf_len +=ipv6_optlen (old) - next_opt;
It seems like the code might be simplified if we always put the CALIPSO at the end, what do you think?
Quoted 32 lines
net/ipv6/calipso.c +32
-0+ } + + new->nexthdr =0 ; + new->hdrlen = buf_len /8 -1 ; + +return new; +} + +/** +* calipso_opt_del - Removes the CALIPSO option from an option header +* @old: the original header +* @new: the new header +* +* Description: +* Creates a new header based on @old without any CALIPSO option. If @old +* doesn't contain a CALIPSO option it returns -ENOENT. If @old contains +* no other non-padding options, it returns zero with @new set to NULL. +* Otherwise it returns zero, creates a new header without the CALIPSO +* option (and removing as much padding as possible) and returns with +* @new set to that header. +* +*/ +static int calipso_opt_del (struct ipv6_opt_hdr *old, +struct ipv6_opt_hdr **new) +{ +int ret_val; +unsigned int start, end, delta, pad; + + ret_val =calipso_opt_find (old, &start, &end); +if (ret_val) +return ret_val; +
net/ipv6/calipso.c doi_walk_return: · line 710
Quoted 10 lines
net/ipv6/calipso.c +10
-0+if (start ==sizeof (*old) && end ==ipv6_optlen (old)) { +/* There's no other option in the header so return NULL */ + *new =NULL ; +return 0 ; + } + + delta = (end - start) & ~7 ; + *new =kzalloc (ipv6_optlen (old) - delta,GFP_ATOMIC ); +if (!*new) +return -ENOMEM ;
Why not simply use the option buffer that has already been allocated since we are guaranteed that the new length will be shorter? What am I missing?
Quoted 10 lines
net/ipv6/calipso.c +10
-0+memcpy (*new, old, start); + (*new)->hdrlen -= delta /8 ; + pad = (end - start) &7 ; +calipso_pad_write ((unsigned char *)*new, start, pad); +if (end !=ipv6_optlen (old)) +memcpy ((char *)*new + start + pad, (char *)old + end, +ipv6_optlen (old) - end); + +return 0 ; +}
Signature · 2 lines
paul moore security @ redhat
Quoted 21 lines · Paul Moore
Quoted 20 lines · Huw Davies
+/**
+ *calipso_genopt- Generate a CALIPSO option
+ * @buf: the option buffer
+ * @start: offset from which to write
+ * @buf_len: the size ofopt_buf
+ * @doi_def: the CALIPSO DOI to use
+ * @secattr: the security attributes
+ *
+ * Description:
+ * Generate a CALIPSO option using the DOI definition and security attributes + * passed to the function. This also generates upto three bytes of leading + * padding that ensures that the option is 4n + 2 aligned. It returns the + * number of bytes written (including any initial padding).
+ */net/ipv6/calipso.c +6
-0+static int calipso_genopt (unsigned char *buf,u32 start,u32 buf_len, +const struct calipso_doi *doi_def, +const struct netlbl_lsm_secattr *secattr) +{ +int ret_val; +u32 len, pad;
Quoted 8 lines
net/ipv6/calipso.c doi_walk_return: · line 450
Quoted 6 lines
net/ipv6/calipso.c +6
-0+u16 crc; +static const unsigned char padding[4 ] = {2 ,1 ,0 ,3 }; +unsigned char *calipso; + +/* CALIPSO has 4n + 2 alignment */ + pad = padding[start %4 ];It's probably quicker to use a bitmask than a modulus operation.
Ok.
Quoted 16 lines
Quoted 16 lines
net/ipv6/calipso.c +16
-0+if (buf_len <= start + pad +CALIPSO_HDR_LEN ) +return -ENOSPC ; + +if ((secattr->flags &NETLBL_SECATTR_MLS_LVL ) ==0 ) +return -EPERM ; + + len =CALIPSO_HDR_LEN ; + +if (secattr->flags &NETLBL_SECATTR_MLS_CAT ) { + ret_val =calipso_map_cat_hton (doi_def, + secattr, + buf + start + pad + len, + buf_len - start - pad - len); +if (ret_val <0 ) +return ret_val; + len += ret_val;
Quoted 8 lines
net/ipv6/calipso.c doi_walk_return: · line 470
Quoted 4 lines
net/ipv6/calipso.c +4
-0+ } + +calipso_pad_write (buf, start, pad); + calipso = buf + start + pad;Help me understand why we would need to pad before the CALIPSO option? Assuming any preceding options are properly aligned, or there are no preceding options at all, this should never be needed, yes?
Options may end at any alignment, so the preceding option might end at 4n+1, say, followed by three bytes of padding. What we'd like to do is replace those three bytes with one byte and start the CALIPSO option at 4n+2. So in this case, we need to write a single padding byte before the CALIPSO option.
Quoted 9 lines
net/ipv6/calipso.c doi_walk_return: · line 478
Quoted 7 lines
net/ipv6/calipso.c +7
-0+ calipso[0 ] =IPV6_TLV_CALIPSO ; + calipso[1 ] = len -2 ; + *(__be32 *)(calipso +2 ) =htonl (doi_def->doi); + calipso[6 ] = (len -CALIPSO_HDR_LEN ) /4 ; + calipso[7 ] = secattr->attr.mls.lvl, + crc =crc_ccitt (0xffff , calipso, len); + crc = ~crc;Why not just "crc = ~crc_ccitt(...);"?
No reason, will fix.
Quoted 4 lines
net/ipv6/calipso.c doi_walk_return: · line 480
Quoted 2 lines
net/ipv6/calipso.c +2
-0+ calipso[8 ] = crc &0xff ; + calipso[9 ] = (crc >>8 ) &0xff ;Do we need to convert the checksum to network byte order?
Not according to https://tools.ietf.org/html/rfc1662#section-3.1
(which is referenced from rfc 5570 when describing the CRC-16):
Frame Check Sequence (FCS) Field
The Frame Check Sequence field defaults to 16 bits (two octets). The FCS is transmitted least significant octet first, which contains the coefficient of the highest term.
Also, I obtained a packet capture from a Trusted Solaris box, and this appears to be consistant with that.
Quoted 8 lines
Quoted 8 lines
net/ipv6/calipso.c +8
-0+/** +* calipso_tlv_len - Returns the length of the TLV. +* @tlv: the TLV +* +* Description: +* Returns the length of the provided TLV option. +*/ +static int calipso_tlv_len (unsigned char *tlv)
Quoted 8 lines
net/ipv6/calipso.c doi_walk_return: · line 529
Quoted 5 lines
net/ipv6/calipso.c +5
-0+{ +if (tlv[0 ] ==IPV6_TLV_PAD1 ) +return 1 ; +return tlv[1 ] +2 ; +}Perhaps rename this to
calipso_opt_len()and change the argument from tlv to opt?
That would be confusing w.r.t. ipv6_optlen(). By calling it _tlv_len I'm trying to distinguish the length of a TLV value from the length of the entire hop option. Other suggestions are welcome.
Quoted 2 lines
There may also be a desire to make this a generic ipv6 function; to be honest I'm surprised there isn't one to do this already.
I'll leave it internal for now; if the network guys want it, they can shout.
Actually, I need to tweak this function. We can't guarantee that we can access tlv[1] without knowing the hop option length. I'm thinking of something like this, which also checks that the entire TLV value sits within the hop opt.
static int calipso_tlv_len(struct ipv6_opt_hdr *opt, unsigned int offset)
{
unsigned char *tlv = (unsigned char *)opt;
unsigned int opt_len = ipv6_optlen(opt), tlv_len;
if (offset < sizeof(*opt) || offset >= opt_len)
return -EINVAL;
if (tlv[offset] == IPV6_TLV_PAD1)
return 1;
if (offset + 1 >= opt_len)
return -EINVAL;
tlv_len = tlv[offset + 1] + 2;
if (offset + tlv_len > opt_len)
return -EINVAL;
return tlv_len;
}Quoted 37 lines
[
calipso_opt_find]
Okay, this whole function seems unnecessarily complex. I understand that some of this is due to the tracking of the padding, but what about something like what is shown below? My apologies if I missed some subtle behavior in the original code.int calipso_opt_find(struct
ipv6_opt_hdr*hop,
unsigned int *start,
unsigned int *end){ int rc = -ENOENT; unsigned int opt_len; unsigned int spot, spot_s, spot_e; unsigned char *opt = hop; opt_len = ipv6_optlen(hop); while (opt_iter <= opt_len) { switch (opt[spot]) { case IPV6_TLV_PAD1: case IPv6_TLV_PADN: if (spot_e) spot_e = spot; break; case IPV6_TLV_CALIPSO: rc = 0; spot_e = spot; break; default: if (spot_e == 0) spot_s = spot; else goto out; } spot += calipso_tlv_len(opt[spot]); }out:
Quoted 11 lines
if (spot_s) *start = spot_s + calipso_tlv_len(opt[spot_s]); else *start = 0; if (spot_e) *end = spot_e + calipso_tlv_len(opt[spot_e]); else *end = opt_len; return rc; }
Yes, a version like this is much nicer, thanks! Though I can't deal with 'spot' and 'opt' ;-)
Quoted 12 lines
Quoted 12 lines
+/**
+ *calipso_opt_insert- Inserts a CALIPSO option into an IPv6 hop opt hdr.
+ * @old: the original hop options header
+ * @doi_def: the CALIPSO DOI to use
+ * @secattr: the specific security attributes of the socket
+ *
+ * Description:
+ * Creates a new hop options header based on @old with a
+ * CALIPSO option added to it. If @old already contains a CALIPSO
+ * option this is overwritten, otherwise the new option is appended
+ * after any existing options. If @old is NULL then the new header
+ * will contain just the CALIPSO option and any needed padding.
Quoted 8 lines
net/ipv6/calipso.c doi_walk_return: · line 620
Quoted 6 lines
+ *
+ */net/ipv6/calipso.c +4
-0+static struct ipv6_opt_hdr * +calipso_opt_insert (struct ipv6_opt_hdr *old, +const struct calipso_doi *doi_def, +const struct netlbl_lsm_secattr *secattr)How about renaming "old" to "hop"?
Sure.
Quoted 41 lines
Quoted 28 lines
net/ipv6/calipso.c +28
-0+{ +unsigned int start, end, next_opt, buf_len, pad; +struct ipv6_opt_hdr *new; +int ret_val; + +if (old) { + ret_val =calipso_opt_find (old, &start, &end); +if (ret_val && ret_val != -ENOENT ) +return ERR_PTR (ret_val); +if (end !=ipv6_optlen (old)) + next_opt = end; +else + next_opt =0 ; + buf_len =ipv6_optlen (old) - ((end - start) & ~7 ) + +CALIPSO_OPT_LEN_MAX ; + }else { + start =0 ; + next_opt =0 ; + buf_len =sizeof (*old) +CALIPSO_OPT_LEN_MAX ; + } + + new =kzalloc (buf_len,GFP_ATOMIC ); +if (!new) +return ERR_PTR (-ENOMEM ); + +if (start >2 ) +memcpy (new, old, start); + ret_val = calipso_genopt((unsigned char *)new, start, buf_len, doi_def,net/ipv6/calipso.c doi_walk_return: · line 658
Quoted 10 lines
net/ipv6/calipso.c +10
-0+ secattr); +if (ret_val <0 ) +return ERR_PTR (ret_val); + + end = start + ret_val; + +if (WARN_ON_ONCE (end &3 )) { +kfree (new); +return ERR_PTR (-EINVAL ); + }Vertical whitespace.
Quoted 3 lines
Also, considering the code in
calipso_genopt(), are we ever going to see the buffer not properly sized? In other words, beyond early development, is there any chance of theWARN_ON_ONCEever triggering?
I'll remove them.
Quoted 4 lines
Quoted 4 lines
net/ipv6/calipso.c +4
-0+ pad = ((end &7 ) + (next_opt &7 )) &7 ; + +calipso_pad_write ((unsigned char *)new, end, pad); + buf_len = end + pad;
Quoted 8 lines
net/ipv6/calipso.c doi_walk_return: · line 668
Quoted 5 lines
net/ipv6/calipso.c +5
-0+ +if (next_opt) { +memcpy ((char *)new + end + pad, (char *)old + next_opt, +ipv6_optlen (old) - next_opt); + buf_len +=ipv6_optlen (old) - next_opt;It seems like the code might be simplified if we always put the CALIPSO at the end, what do you think?
I tried and it actually makes life more complicated. That's primarily because you have to figure out how many pad bytes there are at the end of the existing hop option. If we stick with replacing the existing one, we get all the information we need from calipso_opt_find().
I have however made this function a bit simpler by removing 'next_opt'.
Quoted 37 lines
Quoted 37 lines
net/ipv6/calipso.c +37
-0+ } + + new->nexthdr =0 ; + new->hdrlen = buf_len /8 -1 ; + +return new; +} + +/** +* calipso_opt_del - Removes the CALIPSO option from an option header +* @old: the original header +* @new: the new header +* +* Description: +* Creates a new header based on @old without any CALIPSO option. If @old +* doesn't contain a CALIPSO option it returns -ENOENT. If @old contains +* no other non-padding options, it returns zero with @new set to NULL. +* Otherwise it returns zero, creates a new header without the CALIPSO +* option (and removing as much padding as possible) and returns with +* @new set to that header. +* +*/ +static int calipso_opt_del (struct ipv6_opt_hdr *old, +struct ipv6_opt_hdr **new) +{ +int ret_val; +unsigned int start, end, delta, pad; + + ret_val =calipso_opt_find (old, &start, &end); +if (ret_val) +return ret_val; + +if (start ==sizeof (*old) && end ==ipv6_optlen (old)) { +/* There's no other option in the header so return NULL */ + *new =NULL ; +return 0 ; + }
Quoted 8 lines
net/ipv6/calipso.c doi_walk_return: · line 710
Quoted 5 lines
net/ipv6/calipso.c +5
-0+ + delta = (end - start) & ~7 ; + *new =kzalloc (ipv6_optlen (old) - delta,GFP_ATOMIC ); +if (!*new) +return -ENOMEM ;Why not simply use the option buffer that has already been allocated since we are guaranteed that the new length will be shorter? What am I missing?
The buffer is actually part of a bigger buffer that holds all of the txopts. In principle we could do it this way, but we've have to update all of the ptrs in that structure. That's going to be more complicated - we could do that at a later stage if it's needed.
Huw.