359
359
*
360
360
* Return: -1 on no match, bit position on 'match_only', 0 otherwise.
361
361
*/
362
- int pipapo_refill (unsigned long * map , int len , int rules , unsigned long * dst ,
362
+ int pipapo_refill (unsigned long * map , unsigned int len , unsigned int rules ,
363
+ unsigned long * dst ,
363
364
const union nft_pipapo_map_bucket * mt , bool match_only )
364
365
{
365
366
unsigned long bitset ;
366
- int k , ret = -1 ;
367
+ unsigned int k ;
368
+ int ret = -1 ;
367
369
368
370
for (k = 0 ; k < len ; k ++ ) {
369
371
bitset = map [k ];
@@ -631,13 +633,17 @@ nft_pipapo_get(const struct net *net, const struct nft_set *set,
631
633
*
632
634
* Return: 0 on success, -ENOMEM on allocation failure.
633
635
*/
634
- static int pipapo_resize (struct nft_pipapo_field * f , int old_rules , int rules )
636
+ static int pipapo_resize (struct nft_pipapo_field * f ,
637
+ unsigned int old_rules , unsigned int rules )
635
638
{
636
639
long * new_lt = NULL , * new_p , * old_lt = f -> lt , * old_p ;
637
640
union nft_pipapo_map_bucket * new_mt , * old_mt = f -> mt ;
638
- size_t new_bucket_size , copy ;
641
+ unsigned int new_bucket_size , copy ;
639
642
int group , bucket ;
640
643
644
+ if (rules >= NFT_PIPAPO_RULE0_MAX )
645
+ return - ENOSPC ;
646
+
641
647
new_bucket_size = DIV_ROUND_UP (rules , BITS_PER_LONG );
642
648
#ifdef NFT_PIPAPO_ALIGN
643
649
new_bucket_size = roundup (new_bucket_size ,
@@ -690,7 +696,7 @@ static int pipapo_resize(struct nft_pipapo_field *f, int old_rules, int rules)
690
696
691
697
if (new_lt ) {
692
698
f -> bsize = new_bucket_size ;
693
- NFT_PIPAPO_LT_ASSIGN ( f , new_lt ) ;
699
+ f -> lt = new_lt ;
694
700
kvfree (old_lt );
695
701
}
696
702
@@ -847,8 +853,8 @@ static void pipapo_lt_8b_to_4b(int old_groups, int bsize,
847
853
*/
848
854
static void pipapo_lt_bits_adjust (struct nft_pipapo_field * f )
849
855
{
856
+ unsigned int groups , bb ;
850
857
unsigned long * new_lt ;
851
- int groups , bb ;
852
858
size_t lt_size ;
853
859
854
860
lt_size = f -> groups * NFT_PIPAPO_BUCKETS (f -> bb ) * f -> bsize *
@@ -898,7 +904,7 @@ static void pipapo_lt_bits_adjust(struct nft_pipapo_field *f)
898
904
f -> groups = groups ;
899
905
f -> bb = bb ;
900
906
kvfree (f -> lt );
901
- NFT_PIPAPO_LT_ASSIGN ( f , new_lt ) ;
907
+ f -> lt = new_lt ;
902
908
}
903
909
904
910
/**
@@ -915,7 +921,7 @@ static void pipapo_lt_bits_adjust(struct nft_pipapo_field *f)
915
921
static int pipapo_insert (struct nft_pipapo_field * f , const uint8_t * k ,
916
922
int mask_bits )
917
923
{
918
- int rule = f -> rules , group , ret , bit_offset = 0 ;
924
+ unsigned int rule = f -> rules , group , ret , bit_offset = 0 ;
919
925
920
926
ret = pipapo_resize (f , f -> rules , f -> rules + 1 );
921
927
if (ret )
@@ -1255,8 +1261,14 @@ static int nft_pipapo_insert(const struct net *net, const struct nft_set *set,
1255
1261
/* Validate */
1256
1262
start_p = start ;
1257
1263
end_p = end ;
1264
+
1265
+ /* some helpers return -1, or 0 >= for valid rule pos,
1266
+ * so we cannot support more than INT_MAX rules at this time.
1267
+ */
1268
+ BUILD_BUG_ON (NFT_PIPAPO_RULE0_MAX > INT_MAX );
1269
+
1258
1270
nft_pipapo_for_each_field (f , i , m ) {
1259
- if (f -> rules >= ( unsigned long ) NFT_PIPAPO_RULE0_MAX )
1271
+ if (f -> rules >= NFT_PIPAPO_RULE0_MAX )
1260
1272
return - ENOSPC ;
1261
1273
1262
1274
if (memcmp (start_p , end_p ,
@@ -1362,7 +1374,7 @@ static struct nft_pipapo_match *pipapo_clone(struct nft_pipapo_match *old)
1362
1374
if (!new_lt )
1363
1375
goto out_lt ;
1364
1376
1365
- NFT_PIPAPO_LT_ASSIGN ( dst , new_lt ) ;
1377
+ dst -> lt = new_lt ;
1366
1378
1367
1379
memcpy (NFT_PIPAPO_LT_ALIGN (new_lt ),
1368
1380
NFT_PIPAPO_LT_ALIGN (src -> lt ),
@@ -1433,10 +1445,10 @@ static struct nft_pipapo_match *pipapo_clone(struct nft_pipapo_match *old)
1433
1445
*
1434
1446
* Return: Number of rules that originated from the same entry as @first.
1435
1447
*/
1436
- static int pipapo_rules_same_key (struct nft_pipapo_field * f , int first )
1448
+ static unsigned int pipapo_rules_same_key (struct nft_pipapo_field * f , unsigned int first )
1437
1449
{
1438
1450
struct nft_pipapo_elem * e = NULL ; /* Keep gcc happy */
1439
- int r ;
1451
+ unsigned int r ;
1440
1452
1441
1453
for (r = first ; r < f -> rules ; r ++ ) {
1442
1454
if (r != first && e != f -> mt [r ].e )
@@ -1489,8 +1501,9 @@ static int pipapo_rules_same_key(struct nft_pipapo_field *f, int first)
1489
1501
* 0 1 2
1490
1502
* element pointers: 0x42 0x42 0x44
1491
1503
*/
1492
- static void pipapo_unmap (union nft_pipapo_map_bucket * mt , int rules ,
1493
- int start , int n , int to_offset , bool is_last )
1504
+ static void pipapo_unmap (union nft_pipapo_map_bucket * mt , unsigned int rules ,
1505
+ unsigned int start , unsigned int n ,
1506
+ unsigned int to_offset , bool is_last )
1494
1507
{
1495
1508
int i ;
1496
1509
@@ -1596,8 +1609,8 @@ static void pipapo_gc(struct nft_set *set, struct nft_pipapo_match *m)
1596
1609
{
1597
1610
struct nft_pipapo * priv = nft_set_priv (set );
1598
1611
struct net * net = read_pnet (& set -> net );
1612
+ unsigned int rules_f0 , first_rule = 0 ;
1599
1613
u64 tstamp = nft_net_tstamp (net );
1600
- int rules_f0 , first_rule = 0 ;
1601
1614
struct nft_pipapo_elem * e ;
1602
1615
struct nft_trans_gc * gc ;
1603
1616
@@ -1608,7 +1621,7 @@ static void pipapo_gc(struct nft_set *set, struct nft_pipapo_match *m)
1608
1621
while ((rules_f0 = pipapo_rules_same_key (m -> f , first_rule ))) {
1609
1622
union nft_pipapo_map_bucket rulemap [NFT_PIPAPO_MAX_FIELDS ];
1610
1623
const struct nft_pipapo_field * f ;
1611
- int i , start , rules_fx ;
1624
+ unsigned int i , start , rules_fx ;
1612
1625
1613
1626
start = first_rule ;
1614
1627
rules_fx = rules_f0 ;
@@ -1986,7 +1999,7 @@ static void nft_pipapo_remove(const struct net *net, const struct nft_set *set,
1986
1999
{
1987
2000
struct nft_pipapo * priv = nft_set_priv (set );
1988
2001
struct nft_pipapo_match * m = priv -> clone ;
1989
- int rules_f0 , first_rule = 0 ;
2002
+ unsigned int rules_f0 , first_rule = 0 ;
1990
2003
struct nft_pipapo_elem * e ;
1991
2004
const u8 * data ;
1992
2005
@@ -2051,7 +2064,7 @@ static void nft_pipapo_walk(const struct nft_ctx *ctx, struct nft_set *set,
2051
2064
struct net * net = read_pnet (& set -> net );
2052
2065
const struct nft_pipapo_match * m ;
2053
2066
const struct nft_pipapo_field * f ;
2054
- int i , r ;
2067
+ unsigned int i , r ;
2055
2068
2056
2069
rcu_read_lock ();
2057
2070
if (iter -> genmask == nft_genmask_cur (net ))
@@ -2155,6 +2168,9 @@ static int nft_pipapo_init(const struct nft_set *set,
2155
2168
2156
2169
field_count = desc -> field_count ? : 1 ;
2157
2170
2171
+ BUILD_BUG_ON (NFT_PIPAPO_MAX_FIELDS > 255 );
2172
+ BUILD_BUG_ON (NFT_PIPAPO_MAX_FIELDS != NFT_REG32_COUNT );
2173
+
2158
2174
if (field_count > NFT_PIPAPO_MAX_FIELDS )
2159
2175
return - EINVAL ;
2160
2176
@@ -2176,7 +2192,11 @@ static int nft_pipapo_init(const struct nft_set *set,
2176
2192
rcu_head_init (& m -> rcu );
2177
2193
2178
2194
nft_pipapo_for_each_field (f , i , m ) {
2179
- int len = desc -> field_len [i ] ? : set -> klen ;
2195
+ unsigned int len = desc -> field_len [i ] ? : set -> klen ;
2196
+
2197
+ /* f->groups is u8 */
2198
+ BUILD_BUG_ON ((NFT_PIPAPO_MAX_BYTES *
2199
+ BITS_PER_BYTE / NFT_PIPAPO_GROUP_BITS_LARGE_SET ) >= 256 );
2180
2200
2181
2201
f -> bb = NFT_PIPAPO_GROUP_BITS_INIT ;
2182
2202
f -> groups = len * NFT_PIPAPO_GROUPS_PER_BYTE (f );
@@ -2185,7 +2205,7 @@ static int nft_pipapo_init(const struct nft_set *set,
2185
2205
2186
2206
f -> bsize = 0 ;
2187
2207
f -> rules = 0 ;
2188
- NFT_PIPAPO_LT_ASSIGN ( f , NULL ) ;
2208
+ f -> lt = NULL ;
2189
2209
f -> mt = NULL ;
2190
2210
}
2191
2211
@@ -2221,7 +2241,7 @@ static void nft_set_pipapo_match_destroy(const struct nft_ctx *ctx,
2221
2241
struct nft_pipapo_match * m )
2222
2242
{
2223
2243
struct nft_pipapo_field * f ;
2224
- int i , r ;
2244
+ unsigned int i , r ;
2225
2245
2226
2246
for (i = 0 , f = m -> f ; i < m -> field_count - 1 ; i ++ , f ++ )
2227
2247
;
0 commit comments