1 /* SPDX-License-Identifier: GPL-2.0 */ 2 #ifndef GENL_MAGIC_FUNC_H 3 #define GENL_MAGIC_FUNC_H 4 5 #include <linux/genl_magic_struct.h> 6 7 /* 8 * Magic: declare tla policy {{{1 9 * Magic: declare nested policies 10 * {{{2 11 */ 12 #undef GENL_mc_group 13 #define GENL_mc_group(group) 14 15 #undef GENL_notification 16 #define GENL_notification(op_name, op_num, mcast_group, tla_list) 17 18 #undef GENL_op 19 #define GENL_op(op_name, op_num, handler, tla_list) 20 21 #undef GENL_struct 22 #define GENL_struct(tag_name, tag_number, s_name, s_fields) \ 23 [tag_name] = { .type = NLA_NESTED }, 24 25 static struct nla_policy CONCAT_(GENL_MAGIC_FAMILY, _tla_nl_policy)[] = { 26 #include GENL_MAGIC_INCLUDE_FILE 27 }; 28 29 #undef GENL_struct 30 #define GENL_struct(tag_name, tag_number, s_name, s_fields) \ 31 static struct nla_policy s_name ## _nl_policy[] __read_mostly = \ 32 { s_fields }; 33 34 #undef __field 35 #define __field(attr_nr, attr_flag, name, nla_type, _type, __get, \ 36 __put, __is_signed) \ 37 [attr_nr] = { .type = nla_type }, 38 39 #undef __array 40 #define __array(attr_nr, attr_flag, name, nla_type, _type, maxlen, \ 41 __get, __put, __is_signed) \ 42 [attr_nr] = { .type = nla_type, \ 43 .len = maxlen - (nla_type == NLA_NUL_STRING) }, 44 45 #include GENL_MAGIC_INCLUDE_FILE 46 47 #ifndef __KERNEL__ 48 #ifndef pr_info 49 #define pr_info(args...) fprintf(stderr, args); 50 #endif 51 #endif 52 53 #ifdef GENL_MAGIC_DEBUG 54 static void dprint_field(const char *dir, int nla_type, 55 const char *name, void *valp) 56 { 57 __u64 val = valp ? *(__u32 *)valp : 1; 58 switch (nla_type) { 59 case NLA_U8: val = (__u8)val; 60 case NLA_U16: val = (__u16)val; 61 case NLA_U32: val = (__u32)val; 62 pr_info("%s attr %s: %d 0x%08x\n", dir, 63 name, (int)val, (unsigned)val); 64 break; 65 case NLA_U64: 66 val = *(__u64*)valp; 67 pr_info("%s attr %s: %lld 0x%08llx\n", dir, 68 name, (long long)val, (unsigned long long)val); 69 break; 70 case NLA_FLAG: 71 if (val) 72 pr_info("%s attr %s: set\n", dir, name); 73 break; 74 } 75 } 76 77 static void dprint_array(const char *dir, int nla_type, 78 const char *name, const char *val, unsigned len) 79 { 80 switch (nla_type) { 81 case NLA_NUL_STRING: 82 if (len && val[len-1] == '\0') 83 len--; 84 pr_info("%s attr %s: [len:%u] '%s'\n", dir, name, len, val); 85 break; 86 default: 87 /* we can always show 4 byte, 88 * thats what nlattr are aligned to. */ 89 pr_info("%s attr %s: [len:%u] %02x%02x%02x%02x ...\n", 90 dir, name, len, val[0], val[1], val[2], val[3]); 91 } 92 } 93 94 #define DPRINT_TLA(a, op, b) pr_info("%s %s %s\n", a, op, b); 95 96 /* Name is a member field name of the struct s. 97 * If s is NULL (only parsing, no copy requested in *_from_attrs()), 98 * nla is supposed to point to the attribute containing the information 99 * corresponding to that struct member. */ 100 #define DPRINT_FIELD(dir, nla_type, name, s, nla) \ 101 do { \ 102 if (s) \ 103 dprint_field(dir, nla_type, #name, &s->name); \ 104 else if (nla) \ 105 dprint_field(dir, nla_type, #name, \ 106 (nla_type == NLA_FLAG) ? NULL \ 107 : nla_data(nla)); \ 108 } while (0) 109 110 #define DPRINT_ARRAY(dir, nla_type, name, s, nla) \ 111 do { \ 112 if (s) \ 113 dprint_array(dir, nla_type, #name, \ 114 s->name, s->name ## _len); \ 115 else if (nla) \ 116 dprint_array(dir, nla_type, #name, \ 117 nla_data(nla), nla_len(nla)); \ 118 } while (0) 119 #else 120 #define DPRINT_TLA(a, op, b) do {} while (0) 121 #define DPRINT_FIELD(dir, nla_type, name, s, nla) do {} while (0) 122 #define DPRINT_ARRAY(dir, nla_type, name, s, nla) do {} while (0) 123 #endif 124 125 /* 126 * Magic: provide conversion functions {{{1 127 * populate struct from attribute table: 128 * {{{2 129 */ 130 131 /* processing of generic netlink messages is serialized. 132 * use one static buffer for parsing of nested attributes */ 133 static struct nlattr *nested_attr_tb[128]; 134 135 #ifndef BUILD_BUG_ON 136 /* Force a compilation error if condition is true */ 137 #define BUILD_BUG_ON(condition) ((void)BUILD_BUG_ON_ZERO(condition)) 138 /* Force a compilation error if condition is true, but also produce a 139 result (of value 0 and type size_t), so the expression can be used 140 e.g. in a structure initializer (or where-ever else comma expressions 141 aren't permitted). */ 142 #define BUILD_BUG_ON_ZERO(e) (sizeof(struct { int:-!!(e); })) 143 #define BUILD_BUG_ON_NULL(e) ((void *)sizeof(struct { int:-!!(e); })) 144 #endif 145 146 #undef GENL_struct 147 #define GENL_struct(tag_name, tag_number, s_name, s_fields) \ 148 /* *_from_attrs functions are static, but potentially unused */ \ 149 static int __ ## s_name ## _from_attrs(struct s_name *s, \ 150 struct genl_info *info, bool exclude_invariants) \ 151 { \ 152 const int maxtype = ARRAY_SIZE(s_name ## _nl_policy)-1; \ 153 struct nlattr *tla = info->attrs[tag_number]; \ 154 struct nlattr **ntb = nested_attr_tb; \ 155 struct nlattr *nla; \ 156 int err; \ 157 BUILD_BUG_ON(ARRAY_SIZE(s_name ## _nl_policy) > ARRAY_SIZE(nested_attr_tb)); \ 158 if (!tla) \ 159 return -ENOMSG; \ 160 DPRINT_TLA(#s_name, "<=-", #tag_name); \ 161 err = drbd_nla_parse_nested(ntb, maxtype, tla, s_name ## _nl_policy); \ 162 if (err) \ 163 return err; \ 164 \ 165 s_fields \ 166 return 0; \ 167 } __attribute__((unused)) \ 168 static int s_name ## _from_attrs(struct s_name *s, \ 169 struct genl_info *info) \ 170 { \ 171 return __ ## s_name ## _from_attrs(s, info, false); \ 172 } __attribute__((unused)) \ 173 static int s_name ## _from_attrs_for_change(struct s_name *s, \ 174 struct genl_info *info) \ 175 { \ 176 return __ ## s_name ## _from_attrs(s, info, true); \ 177 } __attribute__((unused)) \ 178 179 #define __assign(attr_nr, attr_flag, name, nla_type, type, assignment...) \ 180 nla = ntb[attr_nr]; \ 181 if (nla) { \ 182 if (exclude_invariants && !!((attr_flag) & DRBD_F_INVARIANT)) { \ 183 pr_info("<< must not change invariant attr: %s\n", #name); \ 184 return -EEXIST; \ 185 } \ 186 assignment; \ 187 } else if (exclude_invariants && !!((attr_flag) & DRBD_F_INVARIANT)) { \ 188 /* attribute missing from payload, */ \ 189 /* which was expected */ \ 190 } else if ((attr_flag) & DRBD_F_REQUIRED) { \ 191 pr_info("<< missing attr: %s\n", #name); \ 192 return -ENOMSG; \ 193 } 194 195 #undef __field 196 #define __field(attr_nr, attr_flag, name, nla_type, type, __get, __put, \ 197 __is_signed) \ 198 __assign(attr_nr, attr_flag, name, nla_type, type, \ 199 if (s) \ 200 s->name = __get(nla); \ 201 DPRINT_FIELD("<<", nla_type, name, s, nla)) 202 203 /* validate_nla() already checked nla_len <= maxlen appropriately. */ 204 #undef __array 205 #define __array(attr_nr, attr_flag, name, nla_type, type, maxlen, \ 206 __get, __put, __is_signed) \ 207 __assign(attr_nr, attr_flag, name, nla_type, type, \ 208 if (s) \ 209 s->name ## _len = \ 210 __get(s->name, nla, maxlen); \ 211 DPRINT_ARRAY("<<", nla_type, name, s, nla)) 212 213 #include GENL_MAGIC_INCLUDE_FILE 214 215 #undef GENL_struct 216 #define GENL_struct(tag_name, tag_number, s_name, s_fields) 217 218 /* 219 * Magic: define op number to op name mapping {{{1 220 * {{{2 221 */ 222 const char *CONCAT_(GENL_MAGIC_FAMILY, _genl_cmd_to_str)(__u8 cmd) 223 { 224 switch (cmd) { 225 #undef GENL_op 226 #define GENL_op(op_name, op_num, handler, tla_list) \ 227 case op_num: return #op_name; 228 #include GENL_MAGIC_INCLUDE_FILE 229 default: 230 return "unknown"; 231 } 232 } 233 234 #ifdef __KERNEL__ 235 #include <linux/stringify.h> 236 /* 237 * Magic: define genl_ops {{{1 238 * {{{2 239 */ 240 241 #undef GENL_op 242 #define GENL_op(op_name, op_num, handler, tla_list) \ 243 { \ 244 handler \ 245 .cmd = op_name, \ 246 .policy = CONCAT_(GENL_MAGIC_FAMILY, _tla_nl_policy), \ 247 }, 248 249 #define ZZZ_genl_ops CONCAT_(GENL_MAGIC_FAMILY, _genl_ops) 250 static struct genl_ops ZZZ_genl_ops[] __read_mostly = { 251 #include GENL_MAGIC_INCLUDE_FILE 252 }; 253 254 #undef GENL_op 255 #define GENL_op(op_name, op_num, handler, tla_list) 256 257 /* 258 * Define the genl_family, multicast groups, {{{1 259 * and provide register/unregister functions. 260 * {{{2 261 */ 262 #define ZZZ_genl_family CONCAT_(GENL_MAGIC_FAMILY, _genl_family) 263 static struct genl_family ZZZ_genl_family; 264 /* 265 * Magic: define multicast groups 266 * Magic: define multicast group registration helper 267 */ 268 #define ZZZ_genl_mcgrps CONCAT_(GENL_MAGIC_FAMILY, _genl_mcgrps) 269 static const struct genl_multicast_group ZZZ_genl_mcgrps[] = { 270 #undef GENL_mc_group 271 #define GENL_mc_group(group) { .name = #group, }, 272 #include GENL_MAGIC_INCLUDE_FILE 273 }; 274 275 enum CONCAT_(GENL_MAGIC_FAMILY, group_ids) { 276 #undef GENL_mc_group 277 #define GENL_mc_group(group) CONCAT_(GENL_MAGIC_FAMILY, _group_ ## group), 278 #include GENL_MAGIC_INCLUDE_FILE 279 }; 280 281 #undef GENL_mc_group 282 #define GENL_mc_group(group) \ 283 static int CONCAT_(GENL_MAGIC_FAMILY, _genl_multicast_ ## group)( \ 284 struct sk_buff *skb, gfp_t flags) \ 285 { \ 286 unsigned int group_id = \ 287 CONCAT_(GENL_MAGIC_FAMILY, _group_ ## group); \ 288 return genlmsg_multicast(&ZZZ_genl_family, skb, 0, \ 289 group_id, flags); \ 290 } 291 292 #include GENL_MAGIC_INCLUDE_FILE 293 294 #undef GENL_mc_group 295 #define GENL_mc_group(group) 296 297 static struct genl_family ZZZ_genl_family __ro_after_init = { 298 .name = __stringify(GENL_MAGIC_FAMILY), 299 .version = GENL_MAGIC_VERSION, 300 #ifdef GENL_MAGIC_FAMILY_HDRSZ 301 .hdrsize = NLA_ALIGN(GENL_MAGIC_FAMILY_HDRSZ), 302 #endif 303 .maxattr = ARRAY_SIZE(drbd_tla_nl_policy)-1, 304 .ops = ZZZ_genl_ops, 305 .n_ops = ARRAY_SIZE(ZZZ_genl_ops), 306 .mcgrps = ZZZ_genl_mcgrps, 307 .n_mcgrps = ARRAY_SIZE(ZZZ_genl_mcgrps), 308 .module = THIS_MODULE, 309 }; 310 311 int CONCAT_(GENL_MAGIC_FAMILY, _genl_register)(void) 312 { 313 return genl_register_family(&ZZZ_genl_family); 314 } 315 316 void CONCAT_(GENL_MAGIC_FAMILY, _genl_unregister)(void) 317 { 318 genl_unregister_family(&ZZZ_genl_family); 319 } 320 321 /* 322 * Magic: provide conversion functions {{{1 323 * populate skb from struct. 324 * {{{2 325 */ 326 327 #undef GENL_op 328 #define GENL_op(op_name, op_num, handler, tla_list) 329 330 #undef GENL_struct 331 #define GENL_struct(tag_name, tag_number, s_name, s_fields) \ 332 static int s_name ## _to_skb(struct sk_buff *skb, struct s_name *s, \ 333 const bool exclude_sensitive) \ 334 { \ 335 struct nlattr *tla = nla_nest_start(skb, tag_number); \ 336 if (!tla) \ 337 goto nla_put_failure; \ 338 DPRINT_TLA(#s_name, "-=>", #tag_name); \ 339 s_fields \ 340 nla_nest_end(skb, tla); \ 341 return 0; \ 342 \ 343 nla_put_failure: \ 344 if (tla) \ 345 nla_nest_cancel(skb, tla); \ 346 return -EMSGSIZE; \ 347 } \ 348 static inline int s_name ## _to_priv_skb(struct sk_buff *skb, \ 349 struct s_name *s) \ 350 { \ 351 return s_name ## _to_skb(skb, s, 0); \ 352 } \ 353 static inline int s_name ## _to_unpriv_skb(struct sk_buff *skb, \ 354 struct s_name *s) \ 355 { \ 356 return s_name ## _to_skb(skb, s, 1); \ 357 } 358 359 360 #undef __field 361 #define __field(attr_nr, attr_flag, name, nla_type, type, __get, __put, \ 362 __is_signed) \ 363 if (!exclude_sensitive || !((attr_flag) & DRBD_F_SENSITIVE)) { \ 364 DPRINT_FIELD(">>", nla_type, name, s, NULL); \ 365 if (__put(skb, attr_nr, s->name)) \ 366 goto nla_put_failure; \ 367 } 368 369 #undef __array 370 #define __array(attr_nr, attr_flag, name, nla_type, type, maxlen, \ 371 __get, __put, __is_signed) \ 372 if (!exclude_sensitive || !((attr_flag) & DRBD_F_SENSITIVE)) { \ 373 DPRINT_ARRAY(">>",nla_type, name, s, NULL); \ 374 if (__put(skb, attr_nr, min_t(int, maxlen, \ 375 s->name ## _len + (nla_type == NLA_NUL_STRING)),\ 376 s->name)) \ 377 goto nla_put_failure; \ 378 } 379 380 #include GENL_MAGIC_INCLUDE_FILE 381 382 383 /* Functions for initializing structs to default values. */ 384 385 #undef __field 386 #define __field(attr_nr, attr_flag, name, nla_type, type, __get, __put, \ 387 __is_signed) 388 #undef __array 389 #define __array(attr_nr, attr_flag, name, nla_type, type, maxlen, \ 390 __get, __put, __is_signed) 391 #undef __u32_field_def 392 #define __u32_field_def(attr_nr, attr_flag, name, default) \ 393 x->name = default; 394 #undef __s32_field_def 395 #define __s32_field_def(attr_nr, attr_flag, name, default) \ 396 x->name = default; 397 #undef __flg_field_def 398 #define __flg_field_def(attr_nr, attr_flag, name, default) \ 399 x->name = default; 400 #undef __str_field_def 401 #define __str_field_def(attr_nr, attr_flag, name, maxlen) \ 402 memset(x->name, 0, sizeof(x->name)); \ 403 x->name ## _len = 0; 404 #undef GENL_struct 405 #define GENL_struct(tag_name, tag_number, s_name, s_fields) \ 406 static void set_ ## s_name ## _defaults(struct s_name *x) __attribute__((unused)); \ 407 static void set_ ## s_name ## _defaults(struct s_name *x) { \ 408 s_fields \ 409 } 410 411 #include GENL_MAGIC_INCLUDE_FILE 412 413 #endif /* __KERNEL__ */ 414 415 /* }}}1 */ 416 #endif /* GENL_MAGIC_FUNC_H */ 417 /* vim: set foldmethod=marker foldlevel=1 nofoldenable : */ 418