1 /****************************************************************************
2  * Copyright (C) 2014-2021 Cisco and/or its affiliates. All rights reserved.
3  * Copyright (C) 2008-2013 Sourcefire, Inc.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License Version 2 as
7  * published by the Free Software Foundation.  You may not use, modify or
8  * distribute this program under any other version of the GNU General
9  * Public License.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
19  *
20  ****************************************************************************/
21 
22 #ifndef _SF_POLICY_USER_DATA_H_
23 #define _SF_POLICY_USER_DATA_H_
24 
25 #include "sf_ip.h"
26 #include "ipv6_port.h"
27 #include "sfPolicy.h"
28 /*SharedObjectAddStarts
29 #include "sf_dynamic_preprocessor.h"
30 SharedObjectAddEnds */
31 
32 typedef struct
33 {
34     /**policy id of configuration file or packet being processed.
35     */
36     tSfPolicyId  currentPolicyId;
37 
38     /**Number of policies currently allocated.
39      */
40     unsigned int numAllocatedPolicies;
41 
42     /**Number of policies active. Since we use an array of policy pointers,
43      * number of allocated policies may be more than active policies. */
44     unsigned int numActivePolicies;
45 
46     /**user configuration for a policy. This is a pointer to an array of pointers
47      * to user configuration.
48     */
49     void **userConfig;
50 
51 } tSfPolicyUserContext;
52 
53 typedef tSfPolicyUserContext * tSfPolicyUserContextId;
54 
55 //SharedObjectDeleteBegins
56 // index for default nap & ips polices is 0
57 // so single function to return the default idx
getDefaultPolicy(void)58 static inline tSfPolicyId getDefaultPolicy(void)
59 {
60     return SF_DEFAULT_POLICY_ID;
61 }
62 //SharedObjectDeleteEnds
63 
64 tSfPolicyUserContextId sfPolicyConfigCreate( void );
65 void sfPolicyConfigDelete( tSfPolicyUserContextId pContext );
66 
67 //Functions for setting, getting and clearing policy ids
sfPolicyUserPolicySet(tSfPolicyUserContextId pContext,tSfPolicyId policyId)68 static inline void sfPolicyUserPolicySet ( tSfPolicyUserContextId pContext, tSfPolicyId policyId )
69 {
70     pContext->currentPolicyId = policyId;
71 }
72 
sfPolicyUserPolicyGet(tSfPolicyUserContextId pContext)73 static inline tSfPolicyId sfPolicyUserPolicyGet ( tSfPolicyUserContextId pContext )
74 {
75     return pContext->currentPolicyId;
76 }
77 
sfPolicyUserPolicyGetActive(tSfPolicyUserContextId pContext)78 static inline unsigned int sfPolicyUserPolicyGetActive ( tSfPolicyUserContextId pContext )
79 {
80     return (pContext->numActivePolicies);
81 }
82 
83 //Functions for setting, getting and clearing user data specific to policies.
84 int sfPolicyUserDataSet ( tSfPolicyUserContextId pContext, tSfPolicyId policyId, void *config );
85 
sfPolicyUserDataGet(tSfPolicyUserContextId pContext,tSfPolicyId policyId)86 static inline void * sfPolicyUserDataGet ( tSfPolicyUserContextId pContext, tSfPolicyId policyId )
87 {
88     if (pContext && policyId < pContext->numAllocatedPolicies)
89         return pContext->userConfig[policyId];
90 
91     return NULL;
92 }
93 
sfPolicyUserDataSetDefault(tSfPolicyUserContextId pContext,void * config)94 static inline int sfPolicyUserDataSetDefault ( tSfPolicyUserContextId pContext, void *config )
95 {
96     return sfPolicyUserDataSet (pContext, getDefaultPolicy(), config);
97 }
98 
sfPolicyUserDataGetDefault(tSfPolicyUserContextId pContext)99 static inline void * sfPolicyUserDataGetDefault ( tSfPolicyUserContextId pContext )
100 {
101     return sfPolicyUserDataGet (pContext, getDefaultPolicy());
102 }
103 
sfPolicyUserDataSetCurrent(tSfPolicyUserContextId pContext,void * config)104 static inline int sfPolicyUserDataSetCurrent ( tSfPolicyUserContextId pContext, void *config )
105 {
106     return sfPolicyUserDataSet (pContext, pContext->currentPolicyId, config);
107 }
108 
sfPolicyUserDataGetCurrent(tSfPolicyUserContextId pContext)109 static inline void * sfPolicyUserDataGetCurrent ( tSfPolicyUserContextId pContext )
110 {
111     return sfPolicyUserDataGet (pContext, pContext->currentPolicyId);
112 }
113 
114 void *sfPolicyUserDataClear( tSfPolicyUserContextId pContext, tSfPolicyId policyId );
115 
116 int sfPolicyUserDataIterate( struct _SnortConfig *sc, tSfPolicyUserContextId pContext,
117                              int ( *callback )( struct _SnortConfig *sc,
118                                                 tSfPolicyUserContextId pContext,
119                                                 tSfPolicyId policyId,
120                                                 void *config ) );
121 
122 int sfPolicyUserDataFreeIterate( tSfPolicyUserContextId pContext,
123                                  int ( *callback )( tSfPolicyUserContextId pContext,
124                                                     tSfPolicyId policyId,
125                                                     void *config ) );
126 
127 
128 #endif
129