Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

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