Lines Matching +full:cmd +full:- +full:cnt +full:- +full:name
1 // SPDX-License-Identifier: GPL-2.0
59 if (!family->parallel_ops) in genl_op_lock()
65 if (!family->parallel_ops) in genl_op_unlock()
77 * Bit 1 is marked as already used since the drop-monitor code
82 * and the code no longer marks pre-reserved IDs as used.
95 /* We need the last attribute with non-zero ID therefore a 2-entry array */
109 BUILD_BUG_ON(ARRAY_SIZE(genl_policy_reject_all) - 1 != 1); in genl_op_fill_in_reject_policy()
111 if (op->policy || op->cmd < family->resv_start_op) in genl_op_fill_in_reject_policy()
114 op->policy = genl_policy_reject_all; in genl_op_fill_in_reject_policy()
115 op->maxattr = 1; in genl_op_fill_in_reject_policy()
122 if (op->policy) in genl_op_fill_in_reject_policy_split()
125 op->policy = genl_policy_reject_all; in genl_op_fill_in_reject_policy_split()
126 op->maxattr = 1; in genl_op_fill_in_reject_policy_split()
134 static const struct genl_family *genl_family_find_byname(char *name) in genl_family_find_byname() argument
140 if (strcmp(family->name, name) == 0) in genl_family_find_byname()
152 u32 cmd; member
159 *op = family->ops[i]; in genl_op_from_full()
161 if (!op->maxattr) in genl_op_from_full()
162 op->maxattr = family->maxattr; in genl_op_from_full()
163 if (!op->policy) in genl_op_from_full()
164 op->policy = family->policy; in genl_op_from_full()
169 static int genl_get_cmd_full(u32 cmd, const struct genl_family *family, in genl_get_cmd_full() argument
174 for (i = 0; i < family->n_ops; i++) in genl_get_cmd_full()
175 if (family->ops[i].cmd == cmd) { in genl_get_cmd_full()
180 return -ENOENT; in genl_get_cmd_full()
187 op->doit = family->small_ops[i].doit; in genl_op_from_small()
188 op->dumpit = family->small_ops[i].dumpit; in genl_op_from_small()
189 op->cmd = family->small_ops[i].cmd; in genl_op_from_small()
190 op->internal_flags = family->small_ops[i].internal_flags; in genl_op_from_small()
191 op->flags = family->small_ops[i].flags; in genl_op_from_small()
192 op->validate = family->small_ops[i].validate; in genl_op_from_small()
194 op->maxattr = family->maxattr; in genl_op_from_small()
195 op->policy = family->policy; in genl_op_from_small()
200 static int genl_get_cmd_small(u32 cmd, const struct genl_family *family, in genl_get_cmd_small() argument
205 for (i = 0; i < family->n_small_ops; i++) in genl_get_cmd_small()
206 if (family->small_ops[i].cmd == cmd) { in genl_get_cmd_small()
211 return -ENOENT; in genl_get_cmd_small()
216 const struct genl_family *family = iter->family; in genl_op_from_split()
217 int i, cnt = 0; in genl_op_from_split() local
219 i = iter->entry_idx - family->n_ops - family->n_small_ops; in genl_op_from_split()
221 if (family->split_ops[i + cnt].flags & GENL_CMD_CAP_DO) { in genl_op_from_split()
222 iter->doit = family->split_ops[i + cnt]; in genl_op_from_split()
223 genl_op_fill_in_reject_policy_split(family, &iter->doit); in genl_op_from_split()
224 cnt++; in genl_op_from_split()
226 memset(&iter->doit, 0, sizeof(iter->doit)); in genl_op_from_split()
229 if (i + cnt < family->n_split_ops && in genl_op_from_split()
230 family->split_ops[i + cnt].flags & GENL_CMD_CAP_DUMP && in genl_op_from_split()
231 (!cnt || family->split_ops[i + cnt].cmd == iter->doit.cmd)) { in genl_op_from_split()
232 iter->dumpit = family->split_ops[i + cnt]; in genl_op_from_split()
233 genl_op_fill_in_reject_policy_split(family, &iter->dumpit); in genl_op_from_split()
234 cnt++; in genl_op_from_split()
236 memset(&iter->dumpit, 0, sizeof(iter->dumpit)); in genl_op_from_split()
239 WARN_ON(!cnt); in genl_op_from_split()
240 iter->entry_idx += cnt; in genl_op_from_split()
244 genl_get_cmd_split(u32 cmd, u8 flag, const struct genl_family *family, in genl_get_cmd_split() argument
249 for (i = 0; i < family->n_split_ops; i++) in genl_get_cmd_split()
250 if (family->split_ops[i].cmd == cmd && in genl_get_cmd_split()
251 family->split_ops[i].flags & flag) { in genl_get_cmd_split()
252 *op = family->split_ops[i]; in genl_get_cmd_split()
256 return -ENOENT; in genl_get_cmd_split()
264 if ((flags & GENL_CMD_CAP_DO && !full->doit) || in genl_cmd_full_to_split()
265 (flags & GENL_CMD_CAP_DUMP && !full->dumpit)) { in genl_cmd_full_to_split()
267 return -ENOENT; in genl_cmd_full_to_split()
271 op->start = full->start; in genl_cmd_full_to_split()
272 op->dumpit = full->dumpit; in genl_cmd_full_to_split()
273 op->done = full->done; in genl_cmd_full_to_split()
275 op->pre_doit = family->pre_doit; in genl_cmd_full_to_split()
276 op->doit = full->doit; in genl_cmd_full_to_split()
277 op->post_doit = family->post_doit; in genl_cmd_full_to_split()
281 full->validate & GENL_DONT_VALIDATE_DUMP) { in genl_cmd_full_to_split()
282 op->policy = NULL; in genl_cmd_full_to_split()
283 op->maxattr = 0; in genl_cmd_full_to_split()
285 op->policy = full->policy; in genl_cmd_full_to_split()
286 op->maxattr = full->maxattr; in genl_cmd_full_to_split()
289 op->cmd = full->cmd; in genl_cmd_full_to_split()
290 op->internal_flags = full->internal_flags; in genl_cmd_full_to_split()
291 op->flags = full->flags; in genl_cmd_full_to_split()
292 op->validate = full->validate; in genl_cmd_full_to_split()
295 op->flags |= flags; in genl_cmd_full_to_split()
302 genl_get_cmd(u32 cmd, u8 flags, const struct genl_family *family, in genl_get_cmd() argument
308 err = genl_get_cmd_full(cmd, family, &full); in genl_get_cmd()
309 if (err == -ENOENT) in genl_get_cmd()
310 err = genl_get_cmd_small(cmd, family, &full); in genl_get_cmd()
315 err = genl_get_cmd_split(cmd, flags, family, op); in genl_get_cmd()
322 * Fail if both are missing, genl_get_cmd() will zero-init in case of failure.
325 genl_get_cmd_both(u32 cmd, const struct genl_family *family, in genl_get_cmd_both() argument
330 err1 = genl_get_cmd(cmd, GENL_CMD_CAP_DO, family, doit); in genl_get_cmd_both()
331 err2 = genl_get_cmd(cmd, GENL_CMD_CAP_DUMP, family, dumpit); in genl_get_cmd_both()
333 return err1 && err2 ? -ENOENT : 0; in genl_get_cmd_both()
339 iter->family = family; in genl_op_iter_init()
340 iter->cmd_idx = 0; in genl_op_iter_init()
341 iter->entry_idx = 0; in genl_op_iter_init()
343 iter->flags = 0; in genl_op_iter_init()
345 return iter->family->n_ops + in genl_op_iter_init()
346 iter->family->n_small_ops + in genl_op_iter_init()
347 iter->family->n_split_ops; in genl_op_iter_init()
352 const struct genl_family *family = iter->family; in genl_op_iter_next()
356 if (iter->entry_idx < family->n_ops) { in genl_op_iter_next()
357 genl_op_from_full(family, iter->entry_idx, &op); in genl_op_iter_next()
358 } else if (iter->entry_idx < family->n_ops + family->n_small_ops) { in genl_op_iter_next()
359 genl_op_from_small(family, iter->entry_idx - family->n_ops, in genl_op_iter_next()
361 } else if (iter->entry_idx < in genl_op_iter_next()
362 family->n_ops + family->n_small_ops + family->n_split_ops) { in genl_op_iter_next()
370 iter->cmd_idx++; in genl_op_iter_next()
373 iter->entry_idx++; in genl_op_iter_next()
375 genl_cmd_full_to_split(&iter->doit, family, in genl_op_iter_next()
377 genl_cmd_full_to_split(&iter->dumpit, family, in genl_op_iter_next()
381 iter->cmd = iter->doit.cmd | iter->dumpit.cmd; in genl_op_iter_next()
382 iter->flags = iter->doit.flags | iter->dumpit.flags; in genl_op_iter_next()
395 return iter->cmd_idx; in genl_op_iter_idx()
436 return -ENOMEM; in genl_allocate_reserve_groups()
443 return -ENOMEM; in genl_allocate_reserve_groups()
463 int n_groups = family->n_mcgrps; in genl_validate_assign_mc_groups()
471 const struct genl_multicast_group *grp = &family->mcgrps[i]; in genl_validate_assign_mc_groups()
473 if (WARN_ON(grp->name[0] == '\0')) in genl_validate_assign_mc_groups()
474 return -EINVAL; in genl_validate_assign_mc_groups()
475 if (WARN_ON(!string_is_terminated(grp->name, GENL_NAMSIZ))) in genl_validate_assign_mc_groups()
476 return -EINVAL; in genl_validate_assign_mc_groups()
479 /* special-case our own group and hacks */ in genl_validate_assign_mc_groups()
483 } else if (strcmp(family->name, "NET_DM") == 0) { in genl_validate_assign_mc_groups()
486 } else if (family->id == GENL_ID_VFS_DQUOT) { in genl_validate_assign_mc_groups()
489 } else if (family->id == GENL_ID_PMCRAID) { in genl_validate_assign_mc_groups()
499 family->mcgrp_offset = first_id; in genl_validate_assign_mc_groups()
505 if (family->netnsok) { in genl_validate_assign_mc_groups()
511 err = __netlink_change_ngroups(net->genl_sock, in genl_validate_assign_mc_groups()
531 for (i = 0; i < family->n_mcgrps; i++) in genl_validate_assign_mc_groups()
532 clear_bit(family->mcgrp_offset + i, mc_groups); in genl_validate_assign_mc_groups()
546 for (i = 0; i < family->n_mcgrps; i++) in genl_unregister_mc_groups()
548 net->genl_sock, family->mcgrp_offset + i); in genl_unregister_mc_groups()
553 for (i = 0; i < family->n_mcgrps; i++) { in genl_unregister_mc_groups()
554 int grp_id = family->mcgrp_offset + i; in genl_unregister_mc_groups()
559 &family->mcgrps[i], grp_id); in genl_unregister_mc_groups()
565 if (WARN_ON(hweight8(op->flags & (GENL_CMD_CAP_DO | in genl_split_op_check()
576 if (WARN_ON(family->n_ops && !family->ops) || in genl_validate_ops()
577 WARN_ON(family->n_small_ops && !family->small_ops) || in genl_validate_ops()
578 WARN_ON(family->n_split_ops && !family->split_ops)) in genl_validate_ops()
579 return -EINVAL; in genl_validate_ops()
583 return -EINVAL; in genl_validate_ops()
585 if (WARN_ON(i.cmd >= family->resv_start_op && in genl_validate_ops()
587 return -EINVAL; in genl_validate_ops()
591 if (i.cmd == j.cmd) in genl_validate_ops()
592 return -EINVAL; in genl_validate_ops()
596 if (family->n_split_ops) { in genl_validate_ops()
597 if (genl_split_op_check(&family->split_ops[0])) in genl_validate_ops()
598 return -EINVAL; in genl_validate_ops()
601 for (s = 1; s < family->n_split_ops; s++) { in genl_validate_ops()
604 a = &family->split_ops[s - 1]; in genl_validate_ops()
605 b = &family->split_ops[s]; in genl_validate_ops()
608 return -EINVAL; in genl_validate_ops()
611 if (a->cmd < b->cmd) { in genl_validate_ops()
613 } else if (a->cmd > b->cmd) { in genl_validate_ops()
615 return -EINVAL; in genl_validate_ops()
618 if (a->internal_flags != b->internal_flags || in genl_validate_ops()
619 ((a->flags ^ b->flags) & ~(GENL_CMD_CAP_DO | in genl_validate_ops()
622 return -EINVAL; in genl_validate_ops()
625 if ((a->flags & GENL_CMD_CAP_DO) && in genl_validate_ops()
626 (b->flags & GENL_CMD_CAP_DUMP)) in genl_validate_ops()
630 return -EINVAL; in genl_validate_ops()
640 priv = kzalloc(family->sock_priv_size, GFP_KERNEL); in genl_sk_priv_alloc()
642 return ERR_PTR(-ENOMEM); in genl_sk_priv_alloc()
644 if (family->sock_priv_init) in genl_sk_priv_alloc()
645 family->sock_priv_init(priv); in genl_sk_priv_alloc()
652 if (family->sock_priv_destroy) in genl_sk_priv_free()
653 family->sock_priv_destroy(priv); in genl_sk_priv_free()
659 if (!family->sock_priv_size) in genl_sk_privs_alloc()
662 family->sock_privs = kzalloc(sizeof(*family->sock_privs), GFP_KERNEL); in genl_sk_privs_alloc()
663 if (!family->sock_privs) in genl_sk_privs_alloc()
664 return -ENOMEM; in genl_sk_privs_alloc()
665 xa_init(family->sock_privs); in genl_sk_privs_alloc()
674 if (!family->sock_priv_size) in genl_sk_privs_free()
677 xa_for_each(family->sock_privs, id, priv) in genl_sk_privs_free()
680 xa_destroy(family->sock_privs); in genl_sk_privs_free()
681 kfree(family->sock_privs); in genl_sk_privs_free()
689 if (!family->sock_priv_size) in genl_sk_priv_free_by_sock()
691 priv = xa_erase(family->sock_privs, (unsigned long) sk); in genl_sk_priv_free_by_sock()
711 * __genl_sk_priv_get - Get family private pointer for socket, if exists
725 if (WARN_ON_ONCE(!family->sock_privs)) in __genl_sk_priv_get()
726 return ERR_PTR(-EINVAL); in __genl_sk_priv_get()
727 return xa_load(family->sock_privs, (unsigned long) sk); in __genl_sk_priv_get()
731 * genl_sk_priv_get - Get family private pointer for socket
756 old_priv = xa_cmpxchg(family->sock_privs, (unsigned long) sk, NULL, in genl_sk_priv_get()
769 * genl_register_family - register a generic netlink family
773 * family may be registered with the same family name or identifier.
791 if (genl_family_find_byname(family->name)) { in genl_register_family()
792 err = -EEXIST; in genl_register_family()
801 * Sadly, a few cases need to be special-cased in genl_register_family()
810 } else if (strcmp(family->name, "pmcraid") == 0) { in genl_register_family()
812 } else if (strcmp(family->name, "VFS_DQUOT") == 0) { in genl_register_family()
816 family->id = idr_alloc_cyclic(&genl_fam_idr, family, in genl_register_family()
818 if (family->id < 0) { in genl_register_family()
819 err = family->id; in genl_register_family()
831 for (i = 0; i < family->n_mcgrps; i++) in genl_register_family()
833 &family->mcgrps[i], family->mcgrp_offset + i); in genl_register_family()
838 idr_remove(&genl_fam_idr, family->id); in genl_register_family()
848 * genl_unregister_family - unregister generic netlink family
859 if (!genl_family_find_byid(family->id)) { in genl_unregister_family()
861 return -ENOENT; in genl_unregister_family()
866 idr_remove(&genl_fam_idr, family->id); in genl_unregister_family()
883 * genlmsg_put - Add generic netlink header to netlink message
889 * @cmd: generic netlink command
894 const struct genl_family *family, int flags, u8 cmd) in genlmsg_put() argument
899 nlh = nlmsg_put(skb, portid, seq, family->id, GENL_HDRLEN + in genlmsg_put()
900 family->hdrsize, flags); in genlmsg_put()
905 hdr->cmd = cmd; in genlmsg_put()
906 hdr->version = family->version; in genlmsg_put()
907 hdr->reserved = 0; in genlmsg_put()
931 enum netlink_validation validate = ops->validate & no_strict_flag ? in genl_family_rcv_msg_attrs_parse()
937 if (!ops->maxattr) in genl_family_rcv_msg_attrs_parse()
940 attrbuf = kmalloc_array(ops->maxattr + 1, in genl_family_rcv_msg_attrs_parse()
943 return ERR_PTR(-ENOMEM); in genl_family_rcv_msg_attrs_parse()
945 err = __nlmsg_parse(nlh, hdrlen, attrbuf, ops->maxattr, ops->policy, in genl_family_rcv_msg_attrs_parse()
969 struct genl_start_context *ctx = cb->data; in genl_start()
975 ops = ctx->ops; in genl_start()
976 if (!(ops->validate & GENL_DONT_VALIDATE_DUMP) && in genl_start()
977 ctx->nlh->nlmsg_len < nlmsg_msg_size(ctx->hdrlen)) in genl_start()
978 return -EINVAL; in genl_start()
980 attrs = genl_family_rcv_msg_attrs_parse(ctx->family, ctx->nlh, ctx->extack, in genl_start()
981 ops, ctx->hdrlen, in genl_start()
989 return -ENOMEM; in genl_start()
991 info->op = *ops; in genl_start()
992 info->info.family = ctx->family; in genl_start()
993 info->info.snd_seq = cb->nlh->nlmsg_seq; in genl_start()
994 info->info.snd_portid = NETLINK_CB(cb->skb).portid; in genl_start()
995 info->info.nlhdr = cb->nlh; in genl_start()
996 info->info.genlhdr = nlmsg_data(cb->nlh); in genl_start()
997 info->info.attrs = attrs; in genl_start()
998 genl_info_net_set(&info->info, sock_net(cb->skb->sk)); in genl_start()
999 info->info.extack = cb->extack; in genl_start()
1000 memset(&info->info.user_ptr, 0, sizeof(info->info.user_ptr)); in genl_start()
1002 cb->data = info; in genl_start()
1003 if (ops->start) { in genl_start()
1004 genl_op_lock(ctx->family); in genl_start()
1005 rc = ops->start(cb); in genl_start()
1006 genl_op_unlock(ctx->family); in genl_start()
1010 genl_family_rcv_msg_attrs_free(info->info.attrs); in genl_start()
1012 cb->data = NULL; in genl_start()
1019 struct genl_dumpit_info *dump_info = cb->data; in genl_dumpit()
1020 const struct genl_split_ops *ops = &dump_info->op; in genl_dumpit()
1021 struct genl_info *info = &dump_info->info; in genl_dumpit()
1024 info->extack = cb->extack; in genl_dumpit()
1026 genl_op_lock(info->family); in genl_dumpit()
1027 rc = ops->dumpit(skb, cb); in genl_dumpit()
1028 genl_op_unlock(info->family); in genl_dumpit()
1034 struct genl_dumpit_info *dump_info = cb->data; in genl_done()
1035 const struct genl_split_ops *ops = &dump_info->op; in genl_done()
1036 struct genl_info *info = &dump_info->info; in genl_done()
1039 info->extack = cb->extack; in genl_done()
1041 if (ops->done) { in genl_done()
1042 genl_op_lock(info->family); in genl_done()
1043 rc = ops->done(cb); in genl_done()
1044 genl_op_unlock(info->family); in genl_done()
1046 genl_family_rcv_msg_attrs_free(info->attrs); in genl_done()
1060 .module = family->module, in genl_family_rcv_msg_dumpit()
1076 err = __netlink_dump_start(net->genl_sock, skb, nlh, &c); in genl_family_rcv_msg_dumpit()
1099 info.snd_seq = nlh->nlmsg_seq; in genl_family_rcv_msg_doit()
1109 if (ops->pre_doit) { in genl_family_rcv_msg_doit()
1110 err = ops->pre_doit(ops, skb, &info); in genl_family_rcv_msg_doit()
1115 err = ops->doit(skb, &info); in genl_family_rcv_msg_doit()
1117 if (ops->post_doit) in genl_family_rcv_msg_doit()
1118 ops->post_doit(ops, skb, &info); in genl_family_rcv_msg_doit()
1133 if (hdr->cmd < family->resv_start_op) in genl_header_check()
1136 if (hdr->reserved) { in genl_header_check()
1138 return -EINVAL; in genl_header_check()
1144 flags = nlh->nlmsg_flags; in genl_header_check()
1150 return -EINVAL; in genl_header_check()
1161 struct net *net = sock_net(skb->sk); in genl_family_rcv_msg()
1168 if (!family->netnsok && !net_eq(net, &init_net)) in genl_family_rcv_msg()
1169 return -ENOENT; in genl_family_rcv_msg()
1171 hdrlen = GENL_HDRLEN + family->hdrsize; in genl_family_rcv_msg()
1172 if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen)) in genl_family_rcv_msg()
1173 return -EINVAL; in genl_family_rcv_msg()
1176 return -EINVAL; in genl_family_rcv_msg()
1178 flags = (nlh->nlmsg_flags & NLM_F_DUMP) == NLM_F_DUMP ? in genl_family_rcv_msg()
1180 if (genl_get_cmd(hdr->cmd, flags, family, &op)) in genl_family_rcv_msg()
1181 return -EOPNOTSUPP; in genl_family_rcv_msg()
1185 return -EPERM; in genl_family_rcv_msg()
1188 !netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN)) in genl_family_rcv_msg()
1189 return -EPERM; in genl_family_rcv_msg()
1205 family = genl_family_find_byid(nlh->nlmsg_type); in genl_rcv_msg()
1207 return -ENOENT; in genl_rcv_msg()
1230 u32 flags, struct sk_buff *skb, u8 cmd) in ctrl_fill_info() argument
1235 hdr = genlmsg_put(skb, portid, seq, &genl_ctrl, flags, cmd); in ctrl_fill_info()
1237 return -EMSGSIZE; in ctrl_fill_info()
1239 if (nla_put_string(skb, CTRL_ATTR_FAMILY_NAME, family->name) || in ctrl_fill_info()
1240 nla_put_u16(skb, CTRL_ATTR_FAMILY_ID, family->id) || in ctrl_fill_info()
1241 nla_put_u32(skb, CTRL_ATTR_VERSION, family->version) || in ctrl_fill_info()
1242 nla_put_u32(skb, CTRL_ATTR_HDRSIZE, family->hdrsize) || in ctrl_fill_info()
1243 nla_put_u32(skb, CTRL_ATTR_MAXATTR, family->maxattr)) in ctrl_fill_info()
1265 if (nla_put_u32(skb, CTRL_ATTR_OP_ID, i.cmd) || in ctrl_fill_info()
1275 if (family->n_mcgrps) { in ctrl_fill_info()
1283 for (i = 0; i < family->n_mcgrps; i++) { in ctrl_fill_info()
1287 grp = &family->mcgrps[i]; in ctrl_fill_info()
1294 family->mcgrp_offset + i) || in ctrl_fill_info()
1296 grp->name)) in ctrl_fill_info()
1309 return -EMSGSIZE; in ctrl_fill_info()
1315 struct sk_buff *skb, u8 cmd) in ctrl_fill_mcgrp_info() argument
1321 hdr = genlmsg_put(skb, portid, seq, &genl_ctrl, flags, cmd); in ctrl_fill_mcgrp_info()
1323 return -1; in ctrl_fill_mcgrp_info()
1325 if (nla_put_string(skb, CTRL_ATTR_FAMILY_NAME, family->name) || in ctrl_fill_mcgrp_info()
1326 nla_put_u16(skb, CTRL_ATTR_FAMILY_ID, family->id)) in ctrl_fill_mcgrp_info()
1339 grp->name)) in ctrl_fill_mcgrp_info()
1350 return -EMSGSIZE; in ctrl_fill_mcgrp_info()
1357 struct net *net = sock_net(skb->sk); in ctrl_dumpfamily()
1358 int fams_to_skip = cb->args[0]; in ctrl_dumpfamily()
1363 if (!rt->netnsok && !net_eq(net, &init_net)) in ctrl_dumpfamily()
1369 err = ctrl_fill_info(rt, NETLINK_CB(cb->skb).portid, in ctrl_dumpfamily()
1370 cb->nlh->nlmsg_seq, NLM_F_MULTI, in ctrl_dumpfamily()
1373 n--; in ctrl_dumpfamily()
1378 cb->args[0] = n; in ctrl_dumpfamily()
1383 u32 portid, int seq, u8 cmd) in ctrl_build_family_msg() argument
1390 return ERR_PTR(-ENOBUFS); in ctrl_build_family_msg()
1392 err = ctrl_fill_info(family, portid, seq, 0, skb, cmd); in ctrl_build_family_msg()
1404 int grp_id, u32 portid, int seq, u8 cmd) in ctrl_build_mcgrp_msg() argument
1411 return ERR_PTR(-ENOBUFS); in ctrl_build_mcgrp_msg()
1414 seq, 0, skb, cmd); in ctrl_build_mcgrp_msg()
1426 .len = GENL_NAMSIZ - 1 },
1433 int err = -EINVAL; in ctrl_getfamily()
1435 if (info->attrs[CTRL_ATTR_FAMILY_ID]) { in ctrl_getfamily()
1436 u16 id = nla_get_u16(info->attrs[CTRL_ATTR_FAMILY_ID]); in ctrl_getfamily()
1438 err = -ENOENT; in ctrl_getfamily()
1441 if (info->attrs[CTRL_ATTR_FAMILY_NAME]) { in ctrl_getfamily()
1442 char *name; in ctrl_getfamily() local
1444 name = nla_data(info->attrs[CTRL_ATTR_FAMILY_NAME]); in ctrl_getfamily()
1445 res = genl_family_find_byname(name); in ctrl_getfamily()
1450 request_module("net-pf-%d-proto-%d-family-%s", in ctrl_getfamily()
1451 PF_NETLINK, NETLINK_GENERIC, name); in ctrl_getfamily()
1454 res = genl_family_find_byname(name); in ctrl_getfamily()
1457 err = -ENOENT; in ctrl_getfamily()
1463 if (!res->netnsok && !net_eq(genl_info_net(info), &init_net)) { in ctrl_getfamily()
1465 return -ENOENT; in ctrl_getfamily()
1468 msg = ctrl_build_family_msg(res, info->snd_portid, info->snd_seq, in ctrl_getfamily()
1498 return -EINVAL; in genl_ctrl_event()
1504 if (!family->netnsok) in genl_ctrl_event()
1526 .len = GENL_NAMSIZ - 1 },
1533 struct ctrl_dump_policy_ctx *ctx = (void *)cb->ctx; in ctrl_dumppolicy_start()
1534 struct nlattr **tb = info->info.attrs; in ctrl_dumppolicy_start()
1539 BUILD_BUG_ON(sizeof(*ctx) > sizeof(cb->ctx)); in ctrl_dumppolicy_start()
1542 return -EINVAL; in ctrl_dumppolicy_start()
1545 ctx->fam_id = nla_get_u16(tb[CTRL_ATTR_FAMILY_ID]); in ctrl_dumppolicy_start()
1550 return -ENOENT; in ctrl_dumppolicy_start()
1551 ctx->fam_id = rt->id; in ctrl_dumppolicy_start()
1554 rt = genl_family_find_byid(ctx->fam_id); in ctrl_dumppolicy_start()
1556 return -ENOENT; in ctrl_dumppolicy_start()
1558 ctx->rt = rt; in ctrl_dumppolicy_start()
1563 ctx->single_op = true; in ctrl_dumppolicy_start()
1564 ctx->op = nla_get_u32(tb[CTRL_ATTR_OP]); in ctrl_dumppolicy_start()
1566 err = genl_get_cmd_both(ctx->op, rt, &doit, &dump); in ctrl_dumppolicy_start()
1568 NL_SET_BAD_ATTR(cb->extack, tb[CTRL_ATTR_OP]); in ctrl_dumppolicy_start()
1573 err = netlink_policy_dump_add_policy(&ctx->state, in ctrl_dumppolicy_start()
1580 err = netlink_policy_dump_add_policy(&ctx->state, in ctrl_dumppolicy_start()
1587 if (!ctx->state) in ctrl_dumppolicy_start()
1588 return -ENODATA; in ctrl_dumppolicy_start()
1590 ctx->dump_map = 1; in ctrl_dumppolicy_start()
1594 ctx->op_iter = kmalloc(sizeof(*ctx->op_iter), GFP_KERNEL); in ctrl_dumppolicy_start()
1595 if (!ctx->op_iter) in ctrl_dumppolicy_start()
1596 return -ENOMEM; in ctrl_dumppolicy_start()
1598 genl_op_iter_init(rt, ctx->op_iter); in ctrl_dumppolicy_start()
1599 ctx->dump_map = genl_op_iter_next(ctx->op_iter); in ctrl_dumppolicy_start()
1603 err = netlink_policy_dump_add_policy(&ctx->state, in ctrl_dumppolicy_start()
1610 err = netlink_policy_dump_add_policy(&ctx->state, in ctrl_dumppolicy_start()
1618 if (!ctx->state) { in ctrl_dumppolicy_start()
1619 err = -ENODATA; in ctrl_dumppolicy_start()
1625 netlink_policy_dump_free(ctx->state); in ctrl_dumppolicy_start()
1627 kfree(ctx->op_iter); in ctrl_dumppolicy_start()
1634 struct ctrl_dump_policy_ctx *ctx = (void *)cb->ctx; in ctrl_dumppolicy_prep()
1637 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, in ctrl_dumppolicy_prep()
1638 cb->nlh->nlmsg_seq, &genl_ctrl, in ctrl_dumppolicy_prep()
1643 if (nla_put_u16(skb, CTRL_ATTR_FAMILY_ID, ctx->fam_id)) in ctrl_dumppolicy_prep()
1654 struct ctrl_dump_policy_ctx *ctx = (void *)cb->ctx; in ctrl_dumppolicy_put_op()
1660 if (!doit->policy && !dumpit->policy) in ctrl_dumppolicy_put_op()
1665 return -ENOBUFS; in ctrl_dumppolicy_put_op()
1671 nest_op = nla_nest_start(skb, doit->cmd); in ctrl_dumppolicy_put_op()
1675 if (doit->policy) { in ctrl_dumppolicy_put_op()
1676 idx = netlink_policy_dump_get_policy_idx(ctx->state, in ctrl_dumppolicy_put_op()
1677 doit->policy, in ctrl_dumppolicy_put_op()
1678 doit->maxattr); in ctrl_dumppolicy_put_op()
1683 if (dumpit->policy) { in ctrl_dumppolicy_put_op()
1684 idx = netlink_policy_dump_get_policy_idx(ctx->state, in ctrl_dumppolicy_put_op()
1685 dumpit->policy, in ctrl_dumppolicy_put_op()
1686 dumpit->maxattr); in ctrl_dumppolicy_put_op()
1699 return -ENOBUFS; in ctrl_dumppolicy_put_op()
1704 struct ctrl_dump_policy_ctx *ctx = (void *)cb->ctx; in ctrl_dumppolicy()
1707 if (ctx->dump_map) { in ctrl_dumppolicy()
1708 if (ctx->single_op) { in ctrl_dumppolicy()
1711 if (WARN_ON(genl_get_cmd_both(ctx->op, ctx->rt, in ctrl_dumppolicy()
1713 return -ENOENT; in ctrl_dumppolicy()
1716 return skb->len; in ctrl_dumppolicy()
1718 /* done with the per-op policy index list */ in ctrl_dumppolicy()
1719 ctx->dump_map = 0; in ctrl_dumppolicy()
1722 while (ctx->dump_map) { in ctrl_dumppolicy()
1724 &ctx->op_iter->doit, in ctrl_dumppolicy()
1725 &ctx->op_iter->dumpit)) in ctrl_dumppolicy()
1726 return skb->len; in ctrl_dumppolicy()
1728 ctx->dump_map = genl_op_iter_next(ctx->op_iter); in ctrl_dumppolicy()
1732 while (netlink_policy_dump_loop(ctx->state)) { in ctrl_dumppolicy()
1743 if (netlink_policy_dump_write(skb, ctx->state)) in ctrl_dumppolicy()
1751 return skb->len; in ctrl_dumppolicy()
1755 return skb->len; in ctrl_dumppolicy()
1760 struct ctrl_dump_policy_ctx *ctx = (void *)cb->ctx; in ctrl_dumppolicy_done()
1762 kfree(ctx->op_iter); in ctrl_dumppolicy_done()
1763 netlink_policy_dump_free(ctx->state); in ctrl_dumppolicy_done()
1769 .cmd = CTRL_CMD_GETFAMILY,
1772 .maxattr = ARRAY_SIZE(ctrl_policy_family) - 1,
1777 .cmd = CTRL_CMD_GETFAMILY,
1780 .maxattr = ARRAY_SIZE(ctrl_policy_family) - 1,
1785 .cmd = CTRL_CMD_GETPOLICY,
1787 .maxattr = ARRAY_SIZE(ctrl_policy_policy) - 1,
1796 { .name = "notify", },
1807 .name = "nlctrl",
1824 if (family->n_mcgrps == 0) in genl_bind()
1827 i = group - family->mcgrp_offset; in genl_bind()
1828 if (i < 0 || i >= family->n_mcgrps) in genl_bind()
1831 grp = &family->mcgrps[i]; in genl_bind()
1832 if ((grp->flags & GENL_MCAST_CAP_NET_ADMIN) && in genl_bind()
1833 !ns_capable(net->user_ns, CAP_NET_ADMIN)) in genl_bind()
1834 ret = -EPERM; in genl_bind()
1835 if ((grp->flags & GENL_MCAST_CAP_SYS_ADMIN) && in genl_bind()
1836 !ns_capable(net->user_ns, CAP_SYS_ADMIN)) in genl_bind()
1837 ret = -EPERM; in genl_bind()
1839 if (family->bind) in genl_bind()
1840 family->bind(i); in genl_bind()
1859 if (family->n_mcgrps == 0) in genl_unbind()
1862 i = group - family->mcgrp_offset; in genl_unbind()
1863 if (i < 0 || i >= family->n_mcgrps) in genl_unbind()
1866 if (family->unbind) in genl_unbind()
1867 family->unbind(i); in genl_unbind()
1886 net->genl_sock = netlink_kernel_create(net, NETLINK_GENERIC, &cfg); in genl_pernet_init()
1888 if (!net->genl_sock && net_eq(net, &init_net)) in genl_pernet_init()
1891 if (!net->genl_sock) in genl_pernet_init()
1892 return -ENOMEM; in genl_pernet_init()
1899 netlink_kernel_release(net->genl_sock); in genl_pernet_exit()
1900 net->genl_sock = NULL; in genl_pernet_exit()
1940 err = -ENOMEM; in genlmsg_mcast()
1943 err = nlmsg_multicast(prev->genl_sock, tmp, in genlmsg_mcast()
1947 else if (err != -ESRCH) in genlmsg_mcast()
1953 err = nlmsg_multicast(prev->genl_sock, skb, portid, group, GFP_ATOMIC); in genlmsg_mcast()
1959 else if (err != -ESRCH) in genlmsg_mcast()
1961 return delivered ? 0 : -ESRCH; in genlmsg_mcast()
1973 if (WARN_ON_ONCE(group >= family->n_mcgrps)) in genlmsg_multicast_allns()
1974 return -EINVAL; in genlmsg_multicast_allns()
1976 group = family->mcgrp_offset + group; in genlmsg_multicast_allns()
1985 struct sock *sk = net->genl_sock; in genl_notify()
1987 if (WARN_ON_ONCE(group >= family->n_mcgrps)) in genl_notify()
1990 group = family->mcgrp_offset + group; in genl_notify()
1991 nlmsg_notify(sk, skb, info->snd_portid, group, in genl_notify()
1992 nlmsg_report(info->nlhdr), flags); in genl_notify()