Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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>

authored by

Huw Davies and committed by
Paul Moore
3faa8f98 e67ae213

+85 -79
+6
include/net/netlabel.h
··· 434 434 unsigned long bitmap, 435 435 gfp_t flags); 436 436 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 + 437 443 /* 438 444 * LSM protocol operations (NetLabel LSM/kernel API) 439 445 */
+9 -79
net/ipv4/cipso_ipv4.c
··· 135 135 */ 136 136 137 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 - 207 - /** 208 138 * cipso_v4_cache_entry_free - Frees a cache entry 209 139 * @entry: the entry to free 210 140 * ··· 770 840 cipso_cat_size = doi_def->map.std->cat.cipso_size; 771 841 cipso_array = doi_def->map.std->cat.cipso; 772 842 for (;;) { 773 - cat = cipso_v4_bitmap_walk(bitmap, 774 - bitmap_len_bits, 775 - cat + 1, 776 - 1); 843 + cat = netlbl_bitmap_walk(bitmap, 844 + bitmap_len_bits, 845 + cat + 1, 846 + 1); 777 847 if (cat < 0) 778 848 break; 779 849 if (cat >= cipso_cat_size || ··· 839 909 } 840 910 if (net_spot >= net_clen_bits) 841 911 return -ENOSPC; 842 - cipso_v4_bitmap_setbit(net_cat, net_spot, 1); 912 + netlbl_bitmap_setbit(net_cat, net_spot, 1); 843 913 844 914 if (net_spot > net_spot_max) 845 915 net_spot_max = net_spot; ··· 881 951 } 882 952 883 953 for (;;) { 884 - net_spot = cipso_v4_bitmap_walk(net_cat, 885 - net_clen_bits, 886 - net_spot + 1, 887 - 1); 954 + net_spot = netlbl_bitmap_walk(net_cat, 955 + net_clen_bits, 956 + net_spot + 1, 957 + 1); 888 958 if (net_spot < 0) { 889 959 if (net_spot == -2) 890 960 return -EFAULT;
+70
net/netlabel/netlabel_kapi.c
··· 728 728 return 0; 729 729 } 730 730 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 + 731 801 /* 732 802 * LSM Functions 733 803 */