Skip to content

Commit 3faa8f9

Browse files
hdmdaviespcmoore
authored andcommitted
netlabel: Move bitmap manipulation functions to the NetLabel core.
This is to allow the CALIPSO labelling engine to use these. Signed-off-by: Huw Davies <huw@codeweavers.com> Signed-off-by: Paul Moore <paul@paul-moore.com>
1 parent e67ae21 commit 3faa8f9

File tree

3 files changed

+85
-79
lines changed

3 files changed

+85
-79
lines changed

include/net/netlabel.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,12 @@ int netlbl_catmap_setlong(struct netlbl_lsm_catmap **catmap,
434434
unsigned long bitmap,
435435
gfp_t flags);
436436

437+
/* Bitmap functions
438+
*/
439+
int netlbl_bitmap_walk(const unsigned char *bitmap, u32 bitmap_len,
440+
u32 offset, u8 state);
441+
void netlbl_bitmap_setbit(unsigned char *bitmap, u32 bit, u8 state);
442+
437443
/*
438444
* LSM protocol operations (NetLabel LSM/kernel API)
439445
*/

net/ipv4/cipso_ipv4.c

Lines changed: 9 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -134,76 +134,6 @@ int cipso_v4_rbm_strictvalid = 1;
134134
* Helper Functions
135135
*/
136136

137-
/**
138-
* cipso_v4_bitmap_walk - Walk a bitmap looking for a bit
139-
* @bitmap: the bitmap
140-
* @bitmap_len: length in bits
141-
* @offset: starting offset
142-
* @state: if non-zero, look for a set (1) bit else look for a cleared (0) bit
143-
*
144-
* Description:
145-
* Starting at @offset, walk the bitmap from left to right until either the
146-
* desired bit is found or we reach the end. Return the bit offset, -1 if
147-
* not found, or -2 if error.
148-
*/
149-
static int cipso_v4_bitmap_walk(const unsigned char *bitmap,
150-
u32 bitmap_len,
151-
u32 offset,
152-
u8 state)
153-
{
154-
u32 bit_spot;
155-
u32 byte_offset;
156-
unsigned char bitmask;
157-
unsigned char byte;
158-
159-
/* gcc always rounds to zero when doing integer division */
160-
byte_offset = offset / 8;
161-
byte = bitmap[byte_offset];
162-
bit_spot = offset;
163-
bitmask = 0x80 >> (offset % 8);
164-
165-
while (bit_spot < bitmap_len) {
166-
if ((state && (byte & bitmask) == bitmask) ||
167-
(state == 0 && (byte & bitmask) == 0))
168-
return bit_spot;
169-
170-
bit_spot++;
171-
bitmask >>= 1;
172-
if (bitmask == 0) {
173-
byte = bitmap[++byte_offset];
174-
bitmask = 0x80;
175-
}
176-
}
177-
178-
return -1;
179-
}
180-
181-
/**
182-
* cipso_v4_bitmap_setbit - Sets a single bit in a bitmap
183-
* @bitmap: the bitmap
184-
* @bit: the bit
185-
* @state: if non-zero, set the bit (1) else clear the bit (0)
186-
*
187-
* Description:
188-
* Set a single bit in the bitmask. Returns zero on success, negative values
189-
* on error.
190-
*/
191-
static void cipso_v4_bitmap_setbit(unsigned char *bitmap,
192-
u32 bit,
193-
u8 state)
194-
{
195-
u32 byte_spot;
196-
u8 bitmask;
197-
198-
/* gcc always rounds to zero when doing integer division */
199-
byte_spot = bit / 8;
200-
bitmask = 0x80 >> (bit % 8);
201-
if (state)
202-
bitmap[byte_spot] |= bitmask;
203-
else
204-
bitmap[byte_spot] &= ~bitmask;
205-
}
206-
207137
/**
208138
* cipso_v4_cache_entry_free - Frees a cache entry
209139
* @entry: the entry to free
@@ -840,10 +770,10 @@ static int cipso_v4_map_cat_rbm_valid(const struct cipso_v4_doi *doi_def,
840770
cipso_cat_size = doi_def->map.std->cat.cipso_size;
841771
cipso_array = doi_def->map.std->cat.cipso;
842772
for (;;) {
843-
cat = cipso_v4_bitmap_walk(bitmap,
844-
bitmap_len_bits,
845-
cat + 1,
846-
1);
773+
cat = netlbl_bitmap_walk(bitmap,
774+
bitmap_len_bits,
775+
cat + 1,
776+
1);
847777
if (cat < 0)
848778
break;
849779
if (cat >= cipso_cat_size ||
@@ -909,7 +839,7 @@ static int cipso_v4_map_cat_rbm_hton(const struct cipso_v4_doi *doi_def,
909839
}
910840
if (net_spot >= net_clen_bits)
911841
return -ENOSPC;
912-
cipso_v4_bitmap_setbit(net_cat, net_spot, 1);
842+
netlbl_bitmap_setbit(net_cat, net_spot, 1);
913843

914844
if (net_spot > net_spot_max)
915845
net_spot_max = net_spot;
@@ -951,10 +881,10 @@ static int cipso_v4_map_cat_rbm_ntoh(const struct cipso_v4_doi *doi_def,
951881
}
952882

953883
for (;;) {
954-
net_spot = cipso_v4_bitmap_walk(net_cat,
955-
net_clen_bits,
956-
net_spot + 1,
957-
1);
884+
net_spot = netlbl_bitmap_walk(net_cat,
885+
net_clen_bits,
886+
net_spot + 1,
887+
1);
958888
if (net_spot < 0) {
959889
if (net_spot == -2)
960890
return -EFAULT;

net/netlabel/netlabel_kapi.c

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -728,6 +728,76 @@ int netlbl_catmap_setlong(struct netlbl_lsm_catmap **catmap,
728728
return 0;
729729
}
730730

731+
/* Bitmap functions
732+
*/
733+
734+
/**
735+
* netlbl_bitmap_walk - Walk a bitmap looking for a bit
736+
* @bitmap: the bitmap
737+
* @bitmap_len: length in bits
738+
* @offset: starting offset
739+
* @state: if non-zero, look for a set (1) bit else look for a cleared (0) bit
740+
*
741+
* Description:
742+
* Starting at @offset, walk the bitmap from left to right until either the
743+
* desired bit is found or we reach the end. Return the bit offset, -1 if
744+
* not found, or -2 if error.
745+
*/
746+
int netlbl_bitmap_walk(const unsigned char *bitmap, u32 bitmap_len,
747+
u32 offset, u8 state)
748+
{
749+
u32 bit_spot;
750+
u32 byte_offset;
751+
unsigned char bitmask;
752+
unsigned char byte;
753+
754+
byte_offset = offset / 8;
755+
byte = bitmap[byte_offset];
756+
bit_spot = offset;
757+
bitmask = 0x80 >> (offset % 8);
758+
759+
while (bit_spot < bitmap_len) {
760+
if ((state && (byte & bitmask) == bitmask) ||
761+
(state == 0 && (byte & bitmask) == 0))
762+
return bit_spot;
763+
764+
bit_spot++;
765+
bitmask >>= 1;
766+
if (bitmask == 0) {
767+
byte = bitmap[++byte_offset];
768+
bitmask = 0x80;
769+
}
770+
}
771+
772+
return -1;
773+
}
774+
EXPORT_SYMBOL(netlbl_bitmap_walk);
775+
776+
/**
777+
* netlbl_bitmap_setbit - Sets a single bit in a bitmap
778+
* @bitmap: the bitmap
779+
* @bit: the bit
780+
* @state: if non-zero, set the bit (1) else clear the bit (0)
781+
*
782+
* Description:
783+
* Set a single bit in the bitmask. Returns zero on success, negative values
784+
* on error.
785+
*/
786+
void netlbl_bitmap_setbit(unsigned char *bitmap, u32 bit, u8 state)
787+
{
788+
u32 byte_spot;
789+
u8 bitmask;
790+
791+
/* gcc always rounds to zero when doing integer division */
792+
byte_spot = bit / 8;
793+
bitmask = 0x80 >> (bit % 8);
794+
if (state)
795+
bitmap[byte_spot] |= bitmask;
796+
else
797+
bitmap[byte_spot] &= ~bitmask;
798+
}
799+
EXPORT_SYMBOL(netlbl_bitmap_setbit);
800+
731801
/*
732802
* LSM Functions
733803
*/

0 commit comments

Comments
 (0)