1 /*
2 * privflags.c - netlink implementation of private flags commands
3 *
4 * Implementation of "ethtool --show-priv-flags <dev>" and
5 * "ethtool --set-priv-flags <dev> ..."
6 */
7
8 #include <errno.h>
9 #include <string.h>
10 #include <stdio.h>
11
12 #include "../internal.h"
13 #include "../common.h"
14 #include "netlink.h"
15 #include "strset.h"
16 #include "bitset.h"
17 #include "parser.h"
18
19 /* PRIVFLAGS_GET */
20
privflags_maxlen_walk_cb(unsigned int idx,const char * name,bool val __maybe_unused,void * data)21 static void privflags_maxlen_walk_cb(unsigned int idx, const char *name,
22 bool val __maybe_unused, void *data)
23 {
24 unsigned int *maxlen = data;
25 unsigned int len, n;
26
27 if (name)
28 len = strlen(name);
29 else {
30 len = 3; /* strlen("bit") */
31 for (n = idx ?: 1; n; n /= 10)
32 len++; /* plus number of ditigs */
33 }
34 if (len > *maxlen)
35 *maxlen = len;
36 }
37
privflags_dump_walk_cb(unsigned int idx,const char * name,bool val,void * data)38 static void privflags_dump_walk_cb(unsigned int idx, const char *name, bool val,
39 void *data)
40 {
41 unsigned int *maxlen = data;
42 char buff[16];
43
44 if (!name) {
45 snprintf(buff, sizeof(buff) - 1, "bit%u", idx);
46 name = buff;
47 }
48 printf("%-*s: %s\n", *maxlen, name, val ? "on" : "off");
49 }
50
privflags_reply_cb(const struct nlmsghdr * nlhdr,void * data)51 int privflags_reply_cb(const struct nlmsghdr *nlhdr, void *data)
52 {
53 const struct nlattr *tb[ETHTOOL_A_PRIVFLAGS_MAX + 1] = {};
54 DECLARE_ATTR_TB_INFO(tb);
55 const struct stringset *flag_names = NULL;
56 struct nl_context *nlctx = data;
57 unsigned int maxlen = 0;
58 bool silent;
59 int err_ret;
60 int ret;
61
62 silent = nlctx->is_dump || nlctx->is_monitor;
63 err_ret = silent ? MNL_CB_OK : MNL_CB_ERROR;
64
65 ret = mnl_attr_parse(nlhdr, GENL_HDRLEN, attr_cb, &tb_info);
66 if (ret < 0 || !tb[ETHTOOL_A_PRIVFLAGS_FLAGS])
67 return err_ret;
68 nlctx->devname = get_dev_name(tb[ETHTOOL_A_PRIVFLAGS_HEADER]);
69 if (!dev_ok(nlctx))
70 return MNL_CB_OK;
71
72 if (bitset_is_compact(tb[ETHTOOL_A_PRIVFLAGS_FLAGS])) {
73 ret = netlink_init_ethnl2_socket(nlctx);
74 if (ret < 0)
75 return err_ret;
76 flag_names = perdev_stringset(nlctx->devname, ETH_SS_PRIV_FLAGS,
77 nlctx->ethnl2_socket);
78 }
79
80 ret = walk_bitset(tb[ETHTOOL_A_PRIVFLAGS_FLAGS], flag_names,
81 privflags_maxlen_walk_cb, &maxlen);
82 if (ret < 0)
83 return err_ret;
84 if (silent)
85 putchar('\n');
86 printf("Private flags for %s:\n", nlctx->devname);
87 ret = walk_bitset(tb[ETHTOOL_A_PRIVFLAGS_FLAGS], flag_names,
88 privflags_dump_walk_cb, &maxlen);
89 return (ret < 0) ? err_ret : MNL_CB_OK;
90 }
91
nl_gprivflags(struct cmd_context * ctx)92 int nl_gprivflags(struct cmd_context *ctx)
93 {
94 struct nl_context *nlctx = ctx->nlctx;
95 struct nl_socket *nlsk = nlctx->ethnl_socket;
96 int ret;
97
98 if (netlink_cmd_check(ctx, ETHTOOL_MSG_PRIVFLAGS_GET, true))
99 return -EOPNOTSUPP;
100 if (ctx->argc > 0) {
101 fprintf(stderr, "ethtool: unexpected parameter '%s'\n",
102 *ctx->argp);
103 return 1;
104 }
105
106 ret = nlsock_prep_get_request(nlsk, ETHTOOL_MSG_PRIVFLAGS_GET,
107 ETHTOOL_A_PRIVFLAGS_HEADER, 0);
108 if (ret < 0)
109 return ret;
110 return nlsock_send_get_request(nlsk, privflags_reply_cb);
111 }
112
113 /* PRIVFLAGS_SET */
114
115 static const struct bitset_parser_data privflags_parser_data = {
116 .force_hex = false,
117 .no_mask = false,
118 };
119
nl_sprivflags(struct cmd_context * ctx)120 int nl_sprivflags(struct cmd_context *ctx)
121 {
122 struct nl_context *nlctx = ctx->nlctx;
123 struct nl_msg_buff *msgbuff;
124 struct nl_socket *nlsk;
125 int ret;
126
127 if (netlink_cmd_check(ctx, ETHTOOL_MSG_PRIVFLAGS_SET, false))
128 return -EOPNOTSUPP;
129
130 nlctx->cmd = "--set-priv-flags";
131 nlctx->argp = ctx->argp;
132 nlctx->argc = ctx->argc;
133 nlctx->devname = ctx->devname;
134 nlsk = nlctx->ethnl_socket;
135 msgbuff = &nlsk->msgbuff;
136
137 ret = msg_init(nlctx, msgbuff, ETHTOOL_MSG_PRIVFLAGS_SET,
138 NLM_F_REQUEST | NLM_F_ACK);
139 if (ret < 0)
140 return 2;
141 if (ethnla_fill_header(msgbuff, ETHTOOL_A_PRIVFLAGS_HEADER,
142 ctx->devname, 0))
143 return -EMSGSIZE;
144
145 ret = nl_parse_bitset(nlctx, ETHTOOL_A_PRIVFLAGS_FLAGS,
146 &privflags_parser_data, msgbuff, NULL);
147 if (ret < 0)
148 return -EINVAL;
149
150 ret = nlsock_sendmsg(nlsk, NULL);
151 if (ret < 0)
152 return 2;
153 ret = nlsock_process_reply(nlsk, nomsg_reply_cb, nlctx);
154 if (ret == 0)
155 return 0;
156 else
157 return nlctx->exit_code ?: 1;
158 }
159