Lars Ellenberg | ec2c35a | 2011-03-07 10:20:08 +0100 | [diff] [blame] | 1 | #ifndef GENL_MAGIC_STRUCT_H |
| 2 | #define GENL_MAGIC_STRUCT_H |
| 3 | |
| 4 | #ifndef GENL_MAGIC_FAMILY |
| 5 | # error "you need to define GENL_MAGIC_FAMILY before inclusion" |
| 6 | #endif |
| 7 | |
| 8 | #ifndef GENL_MAGIC_VERSION |
| 9 | # error "you need to define GENL_MAGIC_VERSION before inclusion" |
| 10 | #endif |
| 11 | |
| 12 | #ifndef GENL_MAGIC_INCLUDE_FILE |
| 13 | # error "you need to define GENL_MAGIC_INCLUDE_FILE before inclusion" |
| 14 | #endif |
| 15 | |
| 16 | #include <linux/genetlink.h> |
| 17 | #include <linux/types.h> |
| 18 | |
| 19 | #define CONCAT__(a,b) a ## b |
| 20 | #define CONCAT_(a,b) CONCAT__(a,b) |
| 21 | |
| 22 | extern int CONCAT_(GENL_MAGIC_FAMILY, _genl_register)(void); |
| 23 | extern void CONCAT_(GENL_MAGIC_FAMILY, _genl_unregister)(void); |
| 24 | |
| 25 | /* |
| 26 | * Extension of genl attribute validation policies {{{2 |
| 27 | */ |
| 28 | |
Andreas Gruenbacher | 5f93592 | 2011-05-19 17:39:28 +0200 | [diff] [blame] | 29 | /* |
| 30 | * @DRBD_GENLA_F_MANDATORY: By default, netlink ignores attributes it does not |
| 31 | * know about. This flag can be set in nlattr->nla_type to indicate that this |
| 32 | * attribute must not be ignored. |
Lars Ellenberg | ec2c35a | 2011-03-07 10:20:08 +0100 | [diff] [blame] | 33 | * |
Andreas Gruenbacher | 5f93592 | 2011-05-19 17:39:28 +0200 | [diff] [blame] | 34 | * We check and remove this flag in drbd_nla_check_mandatory() before |
| 35 | * validating the attribute types and lengths via nla_parse_nested(). |
Lars Ellenberg | ec2c35a | 2011-03-07 10:20:08 +0100 | [diff] [blame] | 36 | */ |
Andreas Gruenbacher | 5f93592 | 2011-05-19 17:39:28 +0200 | [diff] [blame] | 37 | #define DRBD_GENLA_F_MANDATORY (1 << 14) |
Lars Ellenberg | ec2c35a | 2011-03-07 10:20:08 +0100 | [diff] [blame] | 38 | |
Andreas Gruenbacher | 5f93592 | 2011-05-19 17:39:28 +0200 | [diff] [blame] | 39 | /* |
| 40 | * Flags specific to drbd and not visible at the netlink layer, used in |
| 41 | * <struct>_from_attrs and <struct>_to_skb: |
| 42 | * |
| 43 | * @DRBD_F_REQUIRED: Attribute is required; a request without this attribute is |
| 44 | * invalid. |
| 45 | * |
| 46 | * @DRBD_F_SENSITIVE: Attribute includes sensitive information and must not be |
| 47 | * included in unpriviledged get requests or broadcasts. |
| 48 | * |
| 49 | * @DRBD_F_INVARIANT: Attribute is set when an object is initially created, but |
| 50 | * cannot subsequently be changed. |
| 51 | */ |
| 52 | #define DRBD_F_REQUIRED (1 << 0) |
| 53 | #define DRBD_F_SENSITIVE (1 << 1) |
| 54 | #define DRBD_F_INVARIANT (1 << 2) |
Lars Ellenberg | f399002 | 2011-03-23 14:31:09 +0100 | [diff] [blame] | 55 | |
Andreas Gruenbacher | 5f93592 | 2011-05-19 17:39:28 +0200 | [diff] [blame] | 56 | #define __nla_type(x) ((__u16)((x) & NLA_TYPE_MASK & ~DRBD_GENLA_F_MANDATORY)) |
Lars Ellenberg | ec2c35a | 2011-03-07 10:20:08 +0100 | [diff] [blame] | 57 | |
| 58 | /* }}}1 |
| 59 | * MAGIC |
| 60 | * multi-include macro expansion magic starts here |
| 61 | */ |
| 62 | |
| 63 | /* MAGIC helpers {{{2 */ |
| 64 | |
Nicolas Dichtel | 1dee3f5 | 2016-05-09 11:40:20 +0200 | [diff] [blame] | 65 | static inline int nla_put_u64_0pad(struct sk_buff *skb, int attrtype, u64 value) |
| 66 | { |
| 67 | return nla_put_64bit(skb, attrtype, sizeof(u64), &value, 0); |
| 68 | } |
| 69 | |
Lars Ellenberg | ec2c35a | 2011-03-07 10:20:08 +0100 | [diff] [blame] | 70 | /* possible field types */ |
| 71 | #define __flg_field(attr_nr, attr_flag, name) \ |
Andreas Gruenbacher | a5d8e1f | 2011-05-04 16:06:51 +0200 | [diff] [blame] | 72 | __field(attr_nr, attr_flag, name, NLA_U8, char, \ |
Andreas Gruenbacher | 26ec928 | 2012-07-11 20:36:03 +0200 | [diff] [blame] | 73 | nla_get_u8, nla_put_u8, false) |
Lars Ellenberg | ec2c35a | 2011-03-07 10:20:08 +0100 | [diff] [blame] | 74 | #define __u8_field(attr_nr, attr_flag, name) \ |
| 75 | __field(attr_nr, attr_flag, name, NLA_U8, unsigned char, \ |
Andreas Gruenbacher | 26ec928 | 2012-07-11 20:36:03 +0200 | [diff] [blame] | 76 | nla_get_u8, nla_put_u8, false) |
Lars Ellenberg | ec2c35a | 2011-03-07 10:20:08 +0100 | [diff] [blame] | 77 | #define __u16_field(attr_nr, attr_flag, name) \ |
| 78 | __field(attr_nr, attr_flag, name, NLA_U16, __u16, \ |
Andreas Gruenbacher | 26ec928 | 2012-07-11 20:36:03 +0200 | [diff] [blame] | 79 | nla_get_u16, nla_put_u16, false) |
Lars Ellenberg | ec2c35a | 2011-03-07 10:20:08 +0100 | [diff] [blame] | 80 | #define __u32_field(attr_nr, attr_flag, name) \ |
| 81 | __field(attr_nr, attr_flag, name, NLA_U32, __u32, \ |
Andreas Gruenbacher | 26ec928 | 2012-07-11 20:36:03 +0200 | [diff] [blame] | 82 | nla_get_u32, nla_put_u32, false) |
Lars Ellenberg | 563e4cf | 2011-05-04 10:33:52 +0200 | [diff] [blame] | 83 | #define __s32_field(attr_nr, attr_flag, name) \ |
| 84 | __field(attr_nr, attr_flag, name, NLA_U32, __s32, \ |
Andreas Gruenbacher | 26ec928 | 2012-07-11 20:36:03 +0200 | [diff] [blame] | 85 | nla_get_u32, nla_put_u32, true) |
Lars Ellenberg | ec2c35a | 2011-03-07 10:20:08 +0100 | [diff] [blame] | 86 | #define __u64_field(attr_nr, attr_flag, name) \ |
| 87 | __field(attr_nr, attr_flag, name, NLA_U64, __u64, \ |
Nicolas Dichtel | 1dee3f5 | 2016-05-09 11:40:20 +0200 | [diff] [blame] | 88 | nla_get_u64, nla_put_u64_0pad, false) |
Lars Ellenberg | ec2c35a | 2011-03-07 10:20:08 +0100 | [diff] [blame] | 89 | #define __str_field(attr_nr, attr_flag, name, maxlen) \ |
| 90 | __array(attr_nr, attr_flag, name, NLA_NUL_STRING, char, maxlen, \ |
Andreas Gruenbacher | 26ec928 | 2012-07-11 20:36:03 +0200 | [diff] [blame] | 91 | nla_strlcpy, nla_put, false) |
Lars Ellenberg | ec2c35a | 2011-03-07 10:20:08 +0100 | [diff] [blame] | 92 | #define __bin_field(attr_nr, attr_flag, name, maxlen) \ |
| 93 | __array(attr_nr, attr_flag, name, NLA_BINARY, char, maxlen, \ |
Andreas Gruenbacher | 26ec928 | 2012-07-11 20:36:03 +0200 | [diff] [blame] | 94 | nla_memcpy, nla_put, false) |
Lars Ellenberg | ec2c35a | 2011-03-07 10:20:08 +0100 | [diff] [blame] | 95 | |
Andreas Gruenbacher | b966b5d | 2011-05-03 14:56:09 +0200 | [diff] [blame] | 96 | /* fields with default values */ |
| 97 | #define __flg_field_def(attr_nr, attr_flag, name, default) \ |
| 98 | __flg_field(attr_nr, attr_flag, name) |
| 99 | #define __u32_field_def(attr_nr, attr_flag, name, default) \ |
| 100 | __u32_field(attr_nr, attr_flag, name) |
Andreas Gruenbacher | 3a45abd | 2011-05-12 12:02:54 +0200 | [diff] [blame] | 101 | #define __s32_field_def(attr_nr, attr_flag, name, default) \ |
| 102 | __s32_field(attr_nr, attr_flag, name) |
Andreas Gruenbacher | b966b5d | 2011-05-03 14:56:09 +0200 | [diff] [blame] | 103 | #define __str_field_def(attr_nr, attr_flag, name, maxlen) \ |
| 104 | __str_field(attr_nr, attr_flag, name, maxlen) |
| 105 | |
Lars Ellenberg | ec2c35a | 2011-03-07 10:20:08 +0100 | [diff] [blame] | 106 | #define GENL_op_init(args...) args |
| 107 | #define GENL_doit(handler) \ |
| 108 | .doit = handler, \ |
| 109 | .flags = GENL_ADMIN_PERM, |
| 110 | #define GENL_dumpit(handler) \ |
| 111 | .dumpit = handler, \ |
| 112 | .flags = GENL_ADMIN_PERM, |
| 113 | |
| 114 | /* }}}1 |
| 115 | * Magic: define the enum symbols for genl_ops |
| 116 | * Magic: define the enum symbols for top level attributes |
| 117 | * Magic: define the enum symbols for nested attributes |
| 118 | * {{{2 |
| 119 | */ |
| 120 | |
| 121 | #undef GENL_struct |
| 122 | #define GENL_struct(tag_name, tag_number, s_name, s_fields) |
| 123 | |
| 124 | #undef GENL_mc_group |
| 125 | #define GENL_mc_group(group) |
| 126 | |
| 127 | #undef GENL_notification |
| 128 | #define GENL_notification(op_name, op_num, mcast_group, tla_list) \ |
| 129 | op_name = op_num, |
| 130 | |
| 131 | #undef GENL_op |
| 132 | #define GENL_op(op_name, op_num, handler, tla_list) \ |
| 133 | op_name = op_num, |
| 134 | |
| 135 | enum { |
| 136 | #include GENL_MAGIC_INCLUDE_FILE |
| 137 | }; |
| 138 | |
| 139 | #undef GENL_notification |
| 140 | #define GENL_notification(op_name, op_num, mcast_group, tla_list) |
| 141 | |
| 142 | #undef GENL_op |
| 143 | #define GENL_op(op_name, op_num, handler, attr_list) |
| 144 | |
| 145 | #undef GENL_struct |
| 146 | #define GENL_struct(tag_name, tag_number, s_name, s_fields) \ |
| 147 | tag_name = tag_number, |
| 148 | |
| 149 | enum { |
| 150 | #include GENL_MAGIC_INCLUDE_FILE |
| 151 | }; |
| 152 | |
| 153 | #undef GENL_struct |
| 154 | #define GENL_struct(tag_name, tag_number, s_name, s_fields) \ |
| 155 | enum { \ |
| 156 | s_fields \ |
| 157 | }; |
| 158 | |
| 159 | #undef __field |
Andreas Gruenbacher | 509100e | 2011-05-17 13:29:46 +0200 | [diff] [blame] | 160 | #define __field(attr_nr, attr_flag, name, nla_type, type, \ |
| 161 | __get, __put, __is_signed) \ |
Andreas Gruenbacher | 5f93592 | 2011-05-19 17:39:28 +0200 | [diff] [blame] | 162 | T_ ## name = (__u16)(attr_nr | ((attr_flag) & DRBD_GENLA_F_MANDATORY)), |
Lars Ellenberg | ec2c35a | 2011-03-07 10:20:08 +0100 | [diff] [blame] | 163 | |
| 164 | #undef __array |
Andreas Gruenbacher | 509100e | 2011-05-17 13:29:46 +0200 | [diff] [blame] | 165 | #define __array(attr_nr, attr_flag, name, nla_type, type, \ |
| 166 | maxlen, __get, __put, __is_signed) \ |
Andreas Gruenbacher | 5f93592 | 2011-05-19 17:39:28 +0200 | [diff] [blame] | 167 | T_ ## name = (__u16)(attr_nr | ((attr_flag) & DRBD_GENLA_F_MANDATORY)), |
Lars Ellenberg | ec2c35a | 2011-03-07 10:20:08 +0100 | [diff] [blame] | 168 | |
| 169 | #include GENL_MAGIC_INCLUDE_FILE |
| 170 | |
| 171 | /* }}}1 |
| 172 | * Magic: compile time assert unique numbers for operations |
| 173 | * Magic: -"- unique numbers for top level attributes |
| 174 | * Magic: -"- unique numbers for nested attributes |
| 175 | * {{{2 |
| 176 | */ |
| 177 | |
| 178 | #undef GENL_struct |
| 179 | #define GENL_struct(tag_name, tag_number, s_name, s_fields) |
| 180 | |
| 181 | #undef GENL_op |
| 182 | #define GENL_op(op_name, op_num, handler, attr_list) \ |
| 183 | case op_name: |
| 184 | |
| 185 | #undef GENL_notification |
| 186 | #define GENL_notification(op_name, op_num, mcast_group, tla_list) \ |
| 187 | case op_name: |
| 188 | |
| 189 | static inline void ct_assert_unique_operations(void) |
| 190 | { |
| 191 | switch (0) { |
| 192 | #include GENL_MAGIC_INCLUDE_FILE |
| 193 | ; |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | #undef GENL_op |
| 198 | #define GENL_op(op_name, op_num, handler, attr_list) |
| 199 | |
| 200 | #undef GENL_notification |
| 201 | #define GENL_notification(op_name, op_num, mcast_group, tla_list) |
| 202 | |
| 203 | #undef GENL_struct |
| 204 | #define GENL_struct(tag_name, tag_number, s_name, s_fields) \ |
| 205 | case tag_number: |
| 206 | |
| 207 | static inline void ct_assert_unique_top_level_attributes(void) |
| 208 | { |
| 209 | switch (0) { |
| 210 | #include GENL_MAGIC_INCLUDE_FILE |
| 211 | ; |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | #undef GENL_struct |
| 216 | #define GENL_struct(tag_name, tag_number, s_name, s_fields) \ |
| 217 | static inline void ct_assert_unique_ ## s_name ## _attributes(void) \ |
| 218 | { \ |
| 219 | switch (0) { \ |
| 220 | s_fields \ |
| 221 | ; \ |
| 222 | } \ |
| 223 | } |
| 224 | |
| 225 | #undef __field |
Andreas Gruenbacher | 509100e | 2011-05-17 13:29:46 +0200 | [diff] [blame] | 226 | #define __field(attr_nr, attr_flag, name, nla_type, type, __get, __put, \ |
| 227 | __is_signed) \ |
Lars Ellenberg | ec2c35a | 2011-03-07 10:20:08 +0100 | [diff] [blame] | 228 | case attr_nr: |
| 229 | |
| 230 | #undef __array |
Andreas Gruenbacher | 509100e | 2011-05-17 13:29:46 +0200 | [diff] [blame] | 231 | #define __array(attr_nr, attr_flag, name, nla_type, type, maxlen, \ |
| 232 | __get, __put, __is_signed) \ |
Lars Ellenberg | ec2c35a | 2011-03-07 10:20:08 +0100 | [diff] [blame] | 233 | case attr_nr: |
| 234 | |
| 235 | #include GENL_MAGIC_INCLUDE_FILE |
| 236 | |
| 237 | /* }}}1 |
| 238 | * Magic: declare structs |
| 239 | * struct <name> { |
| 240 | * fields |
| 241 | * }; |
| 242 | * {{{2 |
| 243 | */ |
| 244 | |
| 245 | #undef GENL_struct |
| 246 | #define GENL_struct(tag_name, tag_number, s_name, s_fields) \ |
| 247 | struct s_name { s_fields }; |
| 248 | |
| 249 | #undef __field |
Andreas Gruenbacher | 509100e | 2011-05-17 13:29:46 +0200 | [diff] [blame] | 250 | #define __field(attr_nr, attr_flag, name, nla_type, type, __get, __put, \ |
| 251 | __is_signed) \ |
Lars Ellenberg | ec2c35a | 2011-03-07 10:20:08 +0100 | [diff] [blame] | 252 | type name; |
| 253 | |
| 254 | #undef __array |
Andreas Gruenbacher | 509100e | 2011-05-17 13:29:46 +0200 | [diff] [blame] | 255 | #define __array(attr_nr, attr_flag, name, nla_type, type, maxlen, \ |
| 256 | __get, __put, __is_signed) \ |
Lars Ellenberg | ec2c35a | 2011-03-07 10:20:08 +0100 | [diff] [blame] | 257 | type name[maxlen]; \ |
| 258 | __u32 name ## _len; |
| 259 | |
| 260 | #include GENL_MAGIC_INCLUDE_FILE |
| 261 | |
Andreas Gruenbacher | 509100e | 2011-05-17 13:29:46 +0200 | [diff] [blame] | 262 | #undef GENL_struct |
| 263 | #define GENL_struct(tag_name, tag_number, s_name, s_fields) \ |
| 264 | enum { \ |
| 265 | s_fields \ |
| 266 | }; |
| 267 | |
| 268 | #undef __field |
| 269 | #define __field(attr_nr, attr_flag, name, nla_type, type, __get, __put, \ |
| 270 | is_signed) \ |
| 271 | F_ ## name ## _IS_SIGNED = is_signed, |
| 272 | |
| 273 | #undef __array |
| 274 | #define __array(attr_nr, attr_flag, name, nla_type, type, maxlen, \ |
| 275 | __get, __put, is_signed) \ |
| 276 | F_ ## name ## _IS_SIGNED = is_signed, |
| 277 | |
| 278 | #include GENL_MAGIC_INCLUDE_FILE |
| 279 | |
Lars Ellenberg | ec2c35a | 2011-03-07 10:20:08 +0100 | [diff] [blame] | 280 | /* }}}1 */ |
| 281 | #endif /* GENL_MAGIC_STRUCT_H */ |
| 282 | /* vim: set foldmethod=marker nofoldenable : */ |