Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 #ifndef GENL_MAGIC_STRUCT_H
0003 #define GENL_MAGIC_STRUCT_H
0004 
0005 #ifndef GENL_MAGIC_FAMILY
0006 # error "you need to define GENL_MAGIC_FAMILY before inclusion"
0007 #endif
0008 
0009 #ifndef GENL_MAGIC_VERSION
0010 # error "you need to define GENL_MAGIC_VERSION before inclusion"
0011 #endif
0012 
0013 #ifndef GENL_MAGIC_INCLUDE_FILE
0014 # error "you need to define GENL_MAGIC_INCLUDE_FILE before inclusion"
0015 #endif
0016 
0017 #include <linux/genetlink.h>
0018 #include <linux/types.h>
0019 
0020 #define CONCAT__(a,b)   a ## b
0021 #define CONCAT_(a,b)    CONCAT__(a,b)
0022 
0023 extern int CONCAT_(GENL_MAGIC_FAMILY, _genl_register)(void);
0024 extern void CONCAT_(GENL_MAGIC_FAMILY, _genl_unregister)(void);
0025 
0026 /*
0027  * Extension of genl attribute validation policies          {{{2
0028  */
0029 
0030 /*
0031  * @DRBD_GENLA_F_MANDATORY: By default, netlink ignores attributes it does not
0032  * know about.  This flag can be set in nlattr->nla_type to indicate that this
0033  * attribute must not be ignored.
0034  *
0035  * We check and remove this flag in drbd_nla_check_mandatory() before
0036  * validating the attribute types and lengths via nla_parse_nested().
0037  */
0038 #define DRBD_GENLA_F_MANDATORY (1 << 14)
0039 
0040 /*
0041  * Flags specific to drbd and not visible at the netlink layer, used in
0042  * <struct>_from_attrs and <struct>_to_skb:
0043  *
0044  * @DRBD_F_REQUIRED: Attribute is required; a request without this attribute is
0045  * invalid.
0046  *
0047  * @DRBD_F_SENSITIVE: Attribute includes sensitive information and must not be
0048  * included in unpriviledged get requests or broadcasts.
0049  *
0050  * @DRBD_F_INVARIANT: Attribute is set when an object is initially created, but
0051  * cannot subsequently be changed.
0052  */
0053 #define DRBD_F_REQUIRED (1 << 0)
0054 #define DRBD_F_SENSITIVE (1 << 1)
0055 #define DRBD_F_INVARIANT (1 << 2)
0056 
0057 #define __nla_type(x)   ((__u16)((x) & NLA_TYPE_MASK & ~DRBD_GENLA_F_MANDATORY))
0058 
0059 /*                                  }}}1
0060  * MAGIC
0061  * multi-include macro expansion magic starts here
0062  */
0063 
0064 /* MAGIC helpers                            {{{2 */
0065 
0066 static inline int nla_put_u64_0pad(struct sk_buff *skb, int attrtype, u64 value)
0067 {
0068     return nla_put_64bit(skb, attrtype, sizeof(u64), &value, 0);
0069 }
0070 
0071 /* possible field types */
0072 #define __flg_field(attr_nr, attr_flag, name) \
0073     __field(attr_nr, attr_flag, name, NLA_U8, char, \
0074             nla_get_u8, nla_put_u8, false)
0075 #define __u8_field(attr_nr, attr_flag, name)    \
0076     __field(attr_nr, attr_flag, name, NLA_U8, unsigned char, \
0077             nla_get_u8, nla_put_u8, false)
0078 #define __u16_field(attr_nr, attr_flag, name)   \
0079     __field(attr_nr, attr_flag, name, NLA_U16, __u16, \
0080             nla_get_u16, nla_put_u16, false)
0081 #define __u32_field(attr_nr, attr_flag, name)   \
0082     __field(attr_nr, attr_flag, name, NLA_U32, __u32, \
0083             nla_get_u32, nla_put_u32, false)
0084 #define __s32_field(attr_nr, attr_flag, name)   \
0085     __field(attr_nr, attr_flag, name, NLA_U32, __s32, \
0086             nla_get_u32, nla_put_u32, true)
0087 #define __u64_field(attr_nr, attr_flag, name)   \
0088     __field(attr_nr, attr_flag, name, NLA_U64, __u64, \
0089             nla_get_u64, nla_put_u64_0pad, false)
0090 #define __str_field(attr_nr, attr_flag, name, maxlen) \
0091     __array(attr_nr, attr_flag, name, NLA_NUL_STRING, char, maxlen, \
0092             nla_strscpy, nla_put, false)
0093 #define __bin_field(attr_nr, attr_flag, name, maxlen) \
0094     __array(attr_nr, attr_flag, name, NLA_BINARY, char, maxlen, \
0095             nla_memcpy, nla_put, false)
0096 
0097 /* fields with default values */
0098 #define __flg_field_def(attr_nr, attr_flag, name, default) \
0099     __flg_field(attr_nr, attr_flag, name)
0100 #define __u32_field_def(attr_nr, attr_flag, name, default) \
0101     __u32_field(attr_nr, attr_flag, name)
0102 #define __s32_field_def(attr_nr, attr_flag, name, default) \
0103     __s32_field(attr_nr, attr_flag, name)
0104 #define __str_field_def(attr_nr, attr_flag, name, maxlen) \
0105     __str_field(attr_nr, attr_flag, name, maxlen)
0106 
0107 #define GENL_op_init(args...)   args
0108 #define GENL_doit(handler)      \
0109     .doit = handler,        \
0110     .flags = GENL_ADMIN_PERM,
0111 #define GENL_dumpit(handler)        \
0112     .dumpit = handler,      \
0113     .flags = GENL_ADMIN_PERM,
0114 
0115 /*                                  }}}1
0116  * Magic: define the enum symbols for genl_ops
0117  * Magic: define the enum symbols for top level attributes
0118  * Magic: define the enum symbols for nested attributes
0119  *                                  {{{2
0120  */
0121 
0122 #undef GENL_struct
0123 #define GENL_struct(tag_name, tag_number, s_name, s_fields)
0124 
0125 #undef GENL_mc_group
0126 #define GENL_mc_group(group)
0127 
0128 #undef GENL_notification
0129 #define GENL_notification(op_name, op_num, mcast_group, tla_list)   \
0130     op_name = op_num,
0131 
0132 #undef GENL_op
0133 #define GENL_op(op_name, op_num, handler, tla_list)         \
0134     op_name = op_num,
0135 
0136 enum {
0137 #include GENL_MAGIC_INCLUDE_FILE
0138 };
0139 
0140 #undef GENL_notification
0141 #define GENL_notification(op_name, op_num, mcast_group, tla_list)
0142 
0143 #undef GENL_op
0144 #define GENL_op(op_name, op_num, handler, attr_list)
0145 
0146 #undef GENL_struct
0147 #define GENL_struct(tag_name, tag_number, s_name, s_fields) \
0148         tag_name = tag_number,
0149 
0150 enum {
0151 #include GENL_MAGIC_INCLUDE_FILE
0152 };
0153 
0154 #undef GENL_struct
0155 #define GENL_struct(tag_name, tag_number, s_name, s_fields) \
0156 enum {                              \
0157     s_fields                        \
0158 };
0159 
0160 #undef __field
0161 #define __field(attr_nr, attr_flag, name, nla_type, type,   \
0162         __get, __put, __is_signed)          \
0163     T_ ## name = (__u16)(attr_nr | ((attr_flag) & DRBD_GENLA_F_MANDATORY)),
0164 
0165 #undef __array
0166 #define __array(attr_nr, attr_flag, name, nla_type, type,   \
0167         maxlen, __get, __put, __is_signed)      \
0168     T_ ## name = (__u16)(attr_nr | ((attr_flag) & DRBD_GENLA_F_MANDATORY)),
0169 
0170 #include GENL_MAGIC_INCLUDE_FILE
0171 
0172 /*                                  }}}1
0173  * Magic: compile time assert unique numbers for operations
0174  * Magic: -"- unique numbers for top level attributes
0175  * Magic: -"- unique numbers for nested attributes
0176  *                                  {{{2
0177  */
0178 
0179 #undef GENL_struct
0180 #define GENL_struct(tag_name, tag_number, s_name, s_fields)
0181 
0182 #undef GENL_op
0183 #define GENL_op(op_name, op_num, handler, attr_list)    \
0184     case op_name:
0185 
0186 #undef GENL_notification
0187 #define GENL_notification(op_name, op_num, mcast_group, tla_list)   \
0188     case op_name:
0189 
0190 static inline void ct_assert_unique_operations(void)
0191 {
0192     switch (0) {
0193 #include GENL_MAGIC_INCLUDE_FILE
0194     case 0:
0195         ;
0196     }
0197 }
0198 
0199 #undef GENL_op
0200 #define GENL_op(op_name, op_num, handler, attr_list)
0201 
0202 #undef GENL_notification
0203 #define GENL_notification(op_name, op_num, mcast_group, tla_list)
0204 
0205 #undef GENL_struct
0206 #define GENL_struct(tag_name, tag_number, s_name, s_fields)     \
0207         case tag_number:
0208 
0209 static inline void ct_assert_unique_top_level_attributes(void)
0210 {
0211     switch (0) {
0212 #include GENL_MAGIC_INCLUDE_FILE
0213     case 0:
0214         ;
0215     }
0216 }
0217 
0218 #undef GENL_struct
0219 #define GENL_struct(tag_name, tag_number, s_name, s_fields)     \
0220 static inline void ct_assert_unique_ ## s_name ## _attributes(void) \
0221 {                                   \
0222     switch (0) {                            \
0223     s_fields                            \
0224     case 0:                             \
0225             ;                       \
0226     }                               \
0227 }
0228 
0229 #undef __field
0230 #define __field(attr_nr, attr_flag, name, nla_type, type, __get, __put, \
0231         __is_signed)                        \
0232     case attr_nr:
0233 
0234 #undef __array
0235 #define __array(attr_nr, attr_flag, name, nla_type, type, maxlen,   \
0236         __get, __put, __is_signed)              \
0237     case attr_nr:
0238 
0239 #include GENL_MAGIC_INCLUDE_FILE
0240 
0241 /*                                  }}}1
0242  * Magic: declare structs
0243  * struct <name> {
0244  *  fields
0245  * };
0246  *                                  {{{2
0247  */
0248 
0249 #undef GENL_struct
0250 #define GENL_struct(tag_name, tag_number, s_name, s_fields)     \
0251 struct s_name { s_fields };
0252 
0253 #undef __field
0254 #define __field(attr_nr, attr_flag, name, nla_type, type, __get, __put, \
0255         __is_signed)                        \
0256     type name;
0257 
0258 #undef __array
0259 #define __array(attr_nr, attr_flag, name, nla_type, type, maxlen,   \
0260         __get, __put, __is_signed)              \
0261     type name[maxlen];  \
0262     __u32 name ## _len;
0263 
0264 #include GENL_MAGIC_INCLUDE_FILE
0265 
0266 #undef GENL_struct
0267 #define GENL_struct(tag_name, tag_number, s_name, s_fields)     \
0268 enum {                                  \
0269     s_fields                            \
0270 };
0271 
0272 #undef __field
0273 #define __field(attr_nr, attr_flag, name, nla_type, type, __get, __put, \
0274         is_signed)                      \
0275     F_ ## name ## _IS_SIGNED = is_signed,
0276 
0277 #undef __array
0278 #define __array(attr_nr, attr_flag, name, nla_type, type, maxlen,   \
0279         __get, __put, is_signed)                \
0280     F_ ## name ## _IS_SIGNED = is_signed,
0281 
0282 #include GENL_MAGIC_INCLUDE_FILE
0283 
0284 /* }}}1 */
0285 #endif /* GENL_MAGIC_STRUCT_H */