[NetLabel]: consolidate the struct socket/sock handling to just struct sock
[h-e-n] / net / ipv4 / cipso_ipv4.c
index 60aafb4..fc839f9 100644 (file)
@@ -92,6 +92,33 @@ int cipso_v4_rbm_optfmt = 0;
 int cipso_v4_rbm_strictvalid = 1;
 
 /*
+ * Protocol Constants
+ */
+
+/* Maximum size of the CIPSO IP option, derived from the fact that the maximum
+ * IPv4 header size is 60 bytes and the base IPv4 header is 20 bytes long. */
+#define CIPSO_V4_OPT_LEN_MAX          40
+
+/* Length of the base CIPSO option, this includes the option type (1 byte), the
+ * option length (1 byte), and the DOI (4 bytes). */
+#define CIPSO_V4_HDR_LEN              6
+
+/* Base length of the restrictive category bitmap tag (tag #1). */
+#define CIPSO_V4_TAG_RBM_BLEN         4
+
+/* Base length of the enumerated category tag (tag #2). */
+#define CIPSO_V4_TAG_ENUM_BLEN        4
+
+/* Base length of the ranged categories bitmap tag (tag #5). */
+#define CIPSO_V4_TAG_RNG_BLEN         4
+/* The maximum number of category ranges permitted in the ranged category tag
+ * (tag #5).  You may note that the IETF draft states that the maximum number
+ * of category ranges is 7, but if the low end of the last category range is
+ * zero then it is possibile to fit 8 category ranges because the zero should
+ * be omitted. */
+#define CIPSO_V4_TAG_RNG_CAT_MAX      8
+
+/*
  * Helper Functions
  */
 
@@ -602,7 +629,7 @@ doi_walk_return:
  * @domain: the domain to add
  *
  * Description:
- * Adds the @domain to the the DOI specified by @doi_def, this function
+ * Adds the @domain to the DOI specified by @doi_def, this function
  * should only be called by external functions (i.e. NetLabel).  This function
  * does allocate memory.  Returns zero on success, negative values on failure.
  *
@@ -732,11 +759,12 @@ static int cipso_v4_map_lvl_hton(const struct cipso_v4_doi *doi_def,
                *net_lvl = host_lvl;
                return 0;
        case CIPSO_V4_MAP_STD:
-               if (host_lvl < doi_def->map.std->lvl.local_size) {
+               if (host_lvl < doi_def->map.std->lvl.local_size &&
+                   doi_def->map.std->lvl.local[host_lvl] < CIPSO_V4_INV_LVL) {
                        *net_lvl = doi_def->map.std->lvl.local[host_lvl];
                        return 0;
                }
-               break;
+               return -EPERM;
        }
 
        return -EINVAL;
@@ -771,7 +799,7 @@ static int cipso_v4_map_lvl_ntoh(const struct cipso_v4_doi *doi_def,
                        *host_lvl = doi_def->map.std->lvl.cipso[net_lvl];
                        return 0;
                }
-               break;
+               return -EPERM;
        }
 
        return -EINVAL;
@@ -1108,16 +1136,15 @@ static int cipso_v4_map_cat_rng_hton(const struct cipso_v4_doi *doi_def,
                                     unsigned char *net_cat,
                                     u32 net_cat_len)
 {
-       /* The constant '16' is not random, it is the maximum number of
-        * high/low category range pairs as permitted by the CIPSO draft based
-        * on a maximum IPv4 header length of 60 bytes - the BUG_ON() assertion
-        * does a sanity check to make sure we don't overflow the array. */
        int iter = -1;
-       u16 array[16];
+       u16 array[CIPSO_V4_TAG_RNG_CAT_MAX * 2];
        u32 array_cnt = 0;
        u32 cat_size = 0;
 
-       BUG_ON(net_cat_len > 30);
+       /* make sure we don't overflow the 'array[]' variable */
+       if (net_cat_len >
+           (CIPSO_V4_OPT_LEN_MAX - CIPSO_V4_HDR_LEN - CIPSO_V4_TAG_RNG_BLEN))
+               return -ENOSPC;
 
        for (;;) {
                iter = netlbl_secattr_catmap_walk(secattr->mls_cat, iter + 1);
@@ -1173,7 +1200,7 @@ static int cipso_v4_map_cat_rng_ntoh(const struct cipso_v4_doi *doi_def,
        u16 cat_low;
        u16 cat_high;
 
-       for(net_iter = 0; net_iter < net_cat_len; net_iter += 4) {
+       for (net_iter = 0; net_iter < net_cat_len; net_iter += 4) {
                cat_high = ntohs(*((__be16 *)&net_cat[net_iter]));
                if ((net_iter + 4) <= net_cat_len)
                        cat_low = ntohs(*((__be16 *)&net_cat[net_iter + 2]));
@@ -1195,9 +1222,6 @@ static int cipso_v4_map_cat_rng_ntoh(const struct cipso_v4_doi *doi_def,
  * Protocol Handling Functions
  */
 
-#define CIPSO_V4_OPT_LEN_MAX          40
-#define CIPSO_V4_HDR_LEN              6
-
 /**
  * cipso_v4_gentag_hdr - Generate a CIPSO option header
  * @doi_def: the DOI definition
@@ -1675,7 +1699,7 @@ validate_return:
  */
 void cipso_v4_error(struct sk_buff *skb, int error, u32 gateway)
 {
-       if (skb->nh.iph->protocol == IPPROTO_ICMP || error != -EACCES)
+       if (ip_hdr(skb)->protocol == IPPROTO_ICMP || error != -EACCES)
                return;
 
        if (gateway)
@@ -1685,22 +1709,22 @@ void cipso_v4_error(struct sk_buff *skb, int error, u32 gateway)
 }
 
 /**
- * cipso_v4_socket_setattr - Add a CIPSO option to a socket
- * @sock: the socket
+ * cipso_v4_sock_setattr - Add a CIPSO option to a socket
+ * @sk: the socket
  * @doi_def: the CIPSO DOI to use
  * @secattr: the specific security attributes of the socket
  *
  * Description:
  * Set the CIPSO option on the given socket using the DOI definition and
  * security attributes passed to the function.  This function requires
- * exclusive access to @sock->sk, which means it either needs to be in the
- * process of being created or locked via lock_sock(sock->sk).  Returns zero on
- * success and negative values on failure.
+ * exclusive access to @sk, which means it either needs to be in the
+ * process of being created or locked.  Returns zero on success and negative
+ * values on failure.
  *
  */
-int cipso_v4_socket_setattr(const struct socket *sock,
-                           const struct cipso_v4_doi *doi_def,
-                           const struct netlbl_lsm_secattr *secattr)
+int cipso_v4_sock_setattr(struct sock *sk,
+                         const struct cipso_v4_doi *doi_def,
+                         const struct netlbl_lsm_secattr *secattr)
 {
        int ret_val = -EPERM;
        u32 iter;
@@ -1708,7 +1732,6 @@ int cipso_v4_socket_setattr(const struct socket *sock,
        u32 buf_len = 0;
        u32 opt_len;
        struct ip_options *opt = NULL;
-       struct sock *sk;
        struct inet_sock *sk_inet;
        struct inet_connection_sock *sk_conn;
 
@@ -1716,7 +1739,6 @@ int cipso_v4_socket_setattr(const struct socket *sock,
         * defined yet but it is not a problem as the only users of these
         * "lite" PF_INET sockets are functions which do an accept() call
         * afterwards so we will label the socket as part of the accept(). */
-       sk = sock->sk;
        if (sk == NULL)
                return 0;
 
@@ -1868,29 +1890,6 @@ int cipso_v4_sock_getattr(struct sock *sk, struct netlbl_lsm_secattr *secattr)
 }
 
 /**
- * cipso_v4_socket_getattr - Get the security attributes from a socket
- * @sock: the socket
- * @secattr: the security attributes
- *
- * Description:
- * Query @sock to see if there is a CIPSO option attached to the socket and if
- * there is return the CIPSO security attributes in @secattr.  Returns zero on
- * success and negative values on failure.
- *
- */
-int cipso_v4_socket_getattr(const struct socket *sock,
-                           struct netlbl_lsm_secattr *secattr)
-{
-       int ret_val;
-
-       lock_sock(sock->sk);
-       ret_val = cipso_v4_sock_getattr(sock->sk, secattr);
-       release_sock(sock->sk);
-
-       return ret_val;
-}
-
-/**
  * cipso_v4_skbuff_getattr - Get the security attributes from the CIPSO option
  * @skb: the packet
  * @secattr: the security attributes
@@ -1932,6 +1931,11 @@ int cipso_v4_skbuff_getattr(const struct sk_buff *skb,
                                                 &cipso_ptr[6],
                                                 secattr);
                break;
+       case CIPSO_V4_TAG_RANGE:
+               ret_val = cipso_v4_parsetag_rng(doi_def,
+                                               &cipso_ptr[6],
+                                               secattr);
+               break;
        }
 
 skbuff_getattr_return: