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 bd7b7ce96db4487bb77692a85ee4489fd2c395df 412 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 = drbd_nla_parse_nested(ntb, maxtype, tla, s_name ## _nl_policy); \ 153 if (err) \ 154 return err; \ 155 \ 156 s_fields \ 157 return 0; \ 158} __attribute__((unused)) \ 159static int s_name ## _from_attrs(struct s_name *s, \ 160 struct genl_info *info) \ 161{ \ 162 return __ ## s_name ## _from_attrs(s, info, false); \ 163} __attribute__((unused)) \ 164static int s_name ## _from_attrs_for_change(struct s_name *s, \ 165 struct genl_info *info) \ 166{ \ 167 return __ ## s_name ## _from_attrs(s, info, true); \ 168} __attribute__((unused)) \ 169 170#define __assign(attr_nr, attr_flag, name, nla_type, type, assignment...) \ 171 nla = ntb[attr_nr]; \ 172 if (nla) { \ 173 if (exclude_invariants && !!((attr_flag) & DRBD_F_INVARIANT)) { \ 174 pr_info("<< must not change invariant attr: %s\n", #name); \ 175 return -EEXIST; \ 176 } \ 177 assignment; \ 178 } else if (exclude_invariants && !!((attr_flag) & DRBD_F_INVARIANT)) { \ 179 /* attribute missing from payload, */ \ 180 /* which was expected */ \ 181 } else if ((attr_flag) & DRBD_F_REQUIRED) { \ 182 pr_info("<< missing attr: %s\n", #name); \ 183 return -ENOMSG; \ 184 } 185 186#undef __field 187#define __field(attr_nr, attr_flag, name, nla_type, type, __get, __put, \ 188 __is_signed) \ 189 __assign(attr_nr, attr_flag, name, nla_type, type, \ 190 if (s) \ 191 s->name = __get(nla); \ 192 DPRINT_FIELD("<<", nla_type, name, s, nla)) 193 194/* validate_nla() already checked nla_len <= maxlen appropriately. */ 195#undef __array 196#define __array(attr_nr, attr_flag, name, nla_type, type, maxlen, \ 197 __get, __put, __is_signed) \ 198 __assign(attr_nr, attr_flag, name, nla_type, type, \ 199 if (s) \ 200 s->name ## _len = \ 201 __get(s->name, nla, maxlen); \ 202 DPRINT_ARRAY("<<", nla_type, name, s, nla)) 203 204#include GENL_MAGIC_INCLUDE_FILE 205 206#undef GENL_struct 207#define GENL_struct(tag_name, tag_number, s_name, s_fields) 208 209/* 210 * Magic: define op number to op name mapping {{{1 211 * {{{2 212 */ 213static const char *CONCATENATE(GENL_MAGIC_FAMILY, _genl_cmd_to_str)(__u8 cmd) 214{ 215 switch (cmd) { 216#undef GENL_op 217#define GENL_op(op_name, op_num, handler, tla_list) \ 218 case op_num: return #op_name; 219#include GENL_MAGIC_INCLUDE_FILE 220 default: 221 return "unknown"; 222 } 223} 224 225#ifdef __KERNEL__ 226#include <linux/stringify.h> 227/* 228 * Magic: define genl_ops {{{1 229 * {{{2 230 */ 231 232#undef GENL_op 233#define GENL_op(op_name, op_num, handler, tla_list) \ 234{ \ 235 handler \ 236 .cmd = op_name, \ 237}, 238 239#define ZZZ_genl_ops CONCATENATE(GENL_MAGIC_FAMILY, _genl_ops) 240static struct genl_ops ZZZ_genl_ops[] __read_mostly = { 241#include GENL_MAGIC_INCLUDE_FILE 242}; 243 244#undef GENL_op 245#define GENL_op(op_name, op_num, handler, tla_list) 246 247/* 248 * Define the genl_family, multicast groups, {{{1 249 * and provide register/unregister functions. 250 * {{{2 251 */ 252#define ZZZ_genl_family CONCATENATE(GENL_MAGIC_FAMILY, _genl_family) 253static struct genl_family ZZZ_genl_family; 254/* 255 * Magic: define multicast groups 256 * Magic: define multicast group registration helper 257 */ 258#define ZZZ_genl_mcgrps CONCATENATE(GENL_MAGIC_FAMILY, _genl_mcgrps) 259static const struct genl_multicast_group ZZZ_genl_mcgrps[] = { 260#undef GENL_mc_group 261#define GENL_mc_group(group) { .name = #group, }, 262#include GENL_MAGIC_INCLUDE_FILE 263}; 264 265enum CONCATENATE(GENL_MAGIC_FAMILY, group_ids) { 266#undef GENL_mc_group 267#define GENL_mc_group(group) CONCATENATE(GENL_MAGIC_FAMILY, _group_ ## group), 268#include GENL_MAGIC_INCLUDE_FILE 269}; 270 271#undef GENL_mc_group 272#define GENL_mc_group(group) \ 273static int CONCATENATE(GENL_MAGIC_FAMILY, _genl_multicast_ ## group)( \ 274 struct sk_buff *skb, gfp_t flags) \ 275{ \ 276 unsigned int group_id = \ 277 CONCATENATE(GENL_MAGIC_FAMILY, _group_ ## group); \ 278 return genlmsg_multicast(&ZZZ_genl_family, skb, 0, \ 279 group_id, flags); \ 280} 281 282#include GENL_MAGIC_INCLUDE_FILE 283 284#undef GENL_mc_group 285#define GENL_mc_group(group) 286 287static struct genl_family ZZZ_genl_family __ro_after_init = { 288 .name = __stringify(GENL_MAGIC_FAMILY), 289 .version = GENL_MAGIC_VERSION, 290#ifdef GENL_MAGIC_FAMILY_HDRSZ 291 .hdrsize = NLA_ALIGN(GENL_MAGIC_FAMILY_HDRSZ), 292#endif 293 .maxattr = ARRAY_SIZE(CONCATENATE(GENL_MAGIC_FAMILY, _tla_nl_policy))-1, 294 .policy = CONCATENATE(GENL_MAGIC_FAMILY, _tla_nl_policy), 295#ifdef GENL_MAGIC_FAMILY_PRE_DOIT 296 .pre_doit = GENL_MAGIC_FAMILY_PRE_DOIT, 297 .post_doit = GENL_MAGIC_FAMILY_POST_DOIT, 298#endif 299 .ops = ZZZ_genl_ops, 300 .n_ops = ARRAY_SIZE(ZZZ_genl_ops), 301 .mcgrps = ZZZ_genl_mcgrps, 302 .resv_start_op = 42, /* drbd is currently the only user */ 303 .n_mcgrps = ARRAY_SIZE(ZZZ_genl_mcgrps), 304 .module = THIS_MODULE, 305}; 306 307int CONCATENATE(GENL_MAGIC_FAMILY, _genl_register)(void) 308{ 309 return genl_register_family(&ZZZ_genl_family); 310} 311 312void CONCATENATE(GENL_MAGIC_FAMILY, _genl_unregister)(void) 313{ 314 genl_unregister_family(&ZZZ_genl_family); 315} 316 317/* 318 * Magic: provide conversion functions {{{1 319 * populate skb from struct. 320 * {{{2 321 */ 322 323#undef GENL_op 324#define GENL_op(op_name, op_num, handler, tla_list) 325 326#undef GENL_struct 327#define GENL_struct(tag_name, tag_number, s_name, s_fields) \ 328static int s_name ## _to_skb(struct sk_buff *skb, struct s_name *s, \ 329 const bool exclude_sensitive) \ 330{ \ 331 struct nlattr *tla = nla_nest_start(skb, tag_number); \ 332 if (!tla) \ 333 goto nla_put_failure; \ 334 DPRINT_TLA(#s_name, "-=>", #tag_name); \ 335 s_fields \ 336 nla_nest_end(skb, tla); \ 337 return 0; \ 338 \ 339nla_put_failure: \ 340 if (tla) \ 341 nla_nest_cancel(skb, tla); \ 342 return -EMSGSIZE; \ 343} \ 344static inline int s_name ## _to_priv_skb(struct sk_buff *skb, \ 345 struct s_name *s) \ 346{ \ 347 return s_name ## _to_skb(skb, s, 0); \ 348} \ 349static inline int s_name ## _to_unpriv_skb(struct sk_buff *skb, \ 350 struct s_name *s) \ 351{ \ 352 return s_name ## _to_skb(skb, s, 1); \ 353} 354 355 356#undef __field 357#define __field(attr_nr, attr_flag, name, nla_type, type, __get, __put, \ 358 __is_signed) \ 359 if (!exclude_sensitive || !((attr_flag) & DRBD_F_SENSITIVE)) { \ 360 DPRINT_FIELD(">>", nla_type, name, s, NULL); \ 361 if (__put(skb, attr_nr, s->name)) \ 362 goto nla_put_failure; \ 363 } 364 365#undef __array 366#define __array(attr_nr, attr_flag, name, nla_type, type, maxlen, \ 367 __get, __put, __is_signed) \ 368 if (!exclude_sensitive || !((attr_flag) & DRBD_F_SENSITIVE)) { \ 369 DPRINT_ARRAY(">>",nla_type, name, s, NULL); \ 370 if (__put(skb, attr_nr, min_t(int, maxlen, \ 371 s->name ## _len + (nla_type == NLA_NUL_STRING)),\ 372 s->name)) \ 373 goto nla_put_failure; \ 374 } 375 376#include GENL_MAGIC_INCLUDE_FILE 377 378 379/* Functions for initializing structs to default values. */ 380 381#undef __field 382#define __field(attr_nr, attr_flag, name, nla_type, type, __get, __put, \ 383 __is_signed) 384#undef __array 385#define __array(attr_nr, attr_flag, name, nla_type, type, maxlen, \ 386 __get, __put, __is_signed) 387#undef __u32_field_def 388#define __u32_field_def(attr_nr, attr_flag, name, default) \ 389 x->name = default; 390#undef __s32_field_def 391#define __s32_field_def(attr_nr, attr_flag, name, default) \ 392 x->name = default; 393#undef __flg_field_def 394#define __flg_field_def(attr_nr, attr_flag, name, default) \ 395 x->name = default; 396#undef __str_field_def 397#define __str_field_def(attr_nr, attr_flag, name, maxlen) \ 398 memset(x->name, 0, sizeof(x->name)); \ 399 x->name ## _len = 0; 400#undef GENL_struct 401#define GENL_struct(tag_name, tag_number, s_name, s_fields) \ 402static void set_ ## s_name ## _defaults(struct s_name *x) __attribute__((unused)); \ 403static void set_ ## s_name ## _defaults(struct s_name *x) { \ 404s_fields \ 405} 406 407#include GENL_MAGIC_INCLUDE_FILE 408 409#endif /* __KERNEL__ */ 410 411/* }}}1 */ 412#endif /* GENL_MAGIC_FUNC_H */