xref: /aosp_15_r20/external/ethtool/libmnl/examples/rtnl/rtnl-link-can.c (revision 1b481fc3bb1b45d4cf28d1ec12969dc1055f555d)
1 /* This example is placed in the public domain. */
2 #include <errno.h>
3 #include <limits.h>
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <string.h>
7 #include <unistd.h>
8 #include <time.h>
9 
10 #include <libmnl/libmnl.h>
11 #include <linux/can/netlink.h>
12 #include <linux/if.h>
13 #include <linux/if_link.h>
14 #include <linux/rtnetlink.h>
15 
16 static void incomplete_command(void) __attribute__((noreturn));
17 
18 #define NEXT_ARG()				\
19 	do { \
20 		if (argc <= 0) incomplete_command();	\
21 		argv++; \
22 		argc--; \
23 	} while (0)
24 
duparg2(const char * key,const char * arg)25 static void duparg2(const char *key, const char *arg)
26 {
27 	fprintf(stderr,
28 		"Error: either \"%s\" is duplicate, or \"%s\" is a garbage.\n",
29 		key, arg);
30 	exit(-1);
31 }
32 
incomplete_command(void)33 static void incomplete_command(void)
34 {
35 	fprintf(stderr, "Command line is not complete. Try option \"help\"\n");
36 	exit(EXIT_FAILURE);
37 }
38 
39 /* Returns false if 'prefix' is a not empty prefix of 'string'.
40  */
matches(const char * prefix,const char * string)41 static bool matches(const char *prefix, const char *string)
42 {
43 	if (!*prefix)
44 		return true;
45 
46 	while (*string && *prefix == *string) {
47 		prefix++;
48 		string++;
49 	}
50 
51 	return !!*prefix;
52 }
53 
get_u16(__u16 * val,const char * arg,int base)54 static int get_u16(__u16 *val, const char *arg, int base)
55 {
56 	unsigned long res;
57 	char *ptr;
58 
59 	if (!arg || !*arg)
60 		return -1;
61 
62 	res = strtoul(arg, &ptr, base);
63 
64 	/* empty string or trailing non-digits */
65 	if (!ptr || ptr == arg || *ptr)
66 		return -1;
67 
68 	/* overflow */
69 	if (res == ULONG_MAX && errno == ERANGE)
70 		return -1;
71 
72 	if (res > 0xFFFFUL)
73 		return -1;
74 
75 	*val = res;
76 	return 0;
77 }
78 
get_u32(__u32 * val,const char * arg,int base)79 static int get_u32(__u32 *val, const char *arg, int base)
80 {
81 	unsigned long res;
82 	char *ptr;
83 
84 	if (!arg || !*arg)
85 		return -1;
86 
87 	res = strtoul(arg, &ptr, base);
88 
89 	/* empty string or trailing non-digits */
90 	if (!ptr || ptr == arg || *ptr)
91 		return -1;
92 
93 	/* overflow */
94 	if (res == ULONG_MAX && errno == ERANGE)
95 		return -1;
96 
97 	/* in case UL > 32 bits */
98 	if (res > 0xFFFFFFFFUL)
99 		return -1;
100 
101 	*val = res;
102 	return 0;
103 }
104 
get_float(float * val,const char * arg)105 static int get_float(float *val, const char *arg)
106 {
107 	float res;
108 	char *ptr;
109 
110 	if (!arg || !*arg)
111 		return -1;
112 
113 	res = strtof(arg, &ptr);
114 	if (!ptr || ptr == arg || *ptr)
115 		return -1;
116 
117 	*val = res;
118 	return 0;
119 }
120 
set_ctrlmode(char * name,char * arg,struct can_ctrlmode * cm,__u32 flags)121 static void set_ctrlmode(char *name, char *arg,
122 			 struct can_ctrlmode *cm, __u32 flags)
123 {
124 	if (strcmp(arg, "on") == 0) {
125 		cm->flags |= flags;
126 	} else if (strcmp(arg, "off") != 0) {
127 		fprintf(stderr,
128 			"Error: argument of \"%s\" must be \"on\" or \"off\", not \"%s\"\n",
129 			name, arg);
130 		exit(EXIT_FAILURE);
131 	}
132 
133 	cm->mask |= flags;
134 }
135 
invarg(const char * msg,const char * arg)136 static void invarg(const char *msg, const char *arg)
137 {
138 	fprintf(stderr, "Error: argument \"%s\" is wrong: %s\n", arg, msg);
139 	exit(-1);
140 }
141 
print_usage(FILE * f)142 static void print_usage(FILE *f)
143 {
144 	fprintf(f,
145 		"Usage: ip link set DEVICE type can\n"
146 		"\t[ bitrate BITRATE [ sample-point SAMPLE-POINT] ] |\n"
147 		"\t[ tq TQ prop-seg PROP_SEG phase-seg1 PHASE-SEG1\n \t  phase-seg2 PHASE-SEG2 [ sjw SJW ] ]\n"
148 		"\n"
149 		"\t[ dbitrate BITRATE [ dsample-point SAMPLE-POINT] ] |\n"
150 		"\t[ dtq TQ dprop-seg PROP_SEG dphase-seg1 PHASE-SEG1\n \t  dphase-seg2 PHASE-SEG2 [ dsjw SJW ] ]\n"
151 		"\n"
152 		"\t[ loopback { on | off } ]\n"
153 		"\t[ listen-only { on | off } ]\n"
154 		"\t[ triple-sampling { on | off } ]\n"
155 		"\t[ one-shot { on | off } ]\n"
156 		"\t[ berr-reporting { on | off } ]\n"
157 		"\t[ fd { on | off } ]\n"
158 		"\t[ fd-non-iso { on | off } ]\n"
159 		"\t[ presume-ack { on | off } ]\n"
160 		"\t[ cc-len8-dlc { on | off } ]\n"
161 		"\n"
162 		"\t[ restart-ms TIME-MS ]\n"
163 		"\t[ restart ]\n"
164 		"\n"
165 		"\t[ termination { 0..65535 } ]\n"
166 		"\n"
167 		"\tWhere: BITRATE	:= { 1..1000000 }\n"
168 		"\t	  SAMPLE-POINT	:= { 0.000..0.999 }\n"
169 		"\t	  TQ		:= { NUMBER }\n"
170 		"\t	  PROP-SEG	:= { 1..8 }\n"
171 		"\t	  PHASE-SEG1	:= { 1..8 }\n"
172 		"\t	  PHASE-SEG2	:= { 1..8 }\n"
173 		"\t	  SJW		:= { 1..4 }\n"
174 		"\t	  RESTART-MS	:= { 0 | NUMBER }\n"
175 		);
176 }
177 
usage(void)178 static void usage(void)
179 {
180 	print_usage(stderr);
181 }
182 
iplink_set_can_parse(int argc,char ** argv,struct nlmsghdr * nlh)183 static int iplink_set_can_parse(int argc, char **argv, struct nlmsghdr *nlh)
184 {
185 	struct can_bittiming bt = {}, dbt = {};
186 	struct can_ctrlmode cm = {};
187 
188 	while (argc > 0) {
189 		if (matches(*argv, "bitrate") == 0) {
190 			NEXT_ARG();
191 			if (get_u32(&bt.bitrate, *argv, 0))
192 				invarg("invalid \"bitrate\" value\n", *argv);
193 		} else if (matches(*argv, "sample-point") == 0) {
194 			float sp;
195 
196 			NEXT_ARG();
197 			if (get_float(&sp, *argv))
198 				invarg("invalid \"sample-point\" value\n",
199 				       *argv);
200 
201 			bt.sample_point = (__u32)(sp * 1000);
202 		} else if (matches(*argv, "tq") == 0) {
203 			NEXT_ARG();
204 			if (get_u32(&bt.tq, *argv, 0))
205 				invarg("invalid \"tq\" value\n", *argv);
206 		} else if (matches(*argv, "prop-seg") == 0) {
207 			NEXT_ARG();
208 			if (get_u32(&bt.prop_seg, *argv, 0))
209 				invarg("invalid \"prop-seg\" value\n", *argv);
210 		} else if (matches(*argv, "phase-seg1") == 0) {
211 			NEXT_ARG();
212 			if (get_u32(&bt.phase_seg1, *argv, 0))
213 				invarg("invalid \"phase-seg1\" value\n", *argv);
214 		} else if (matches(*argv, "phase-seg2") == 0) {
215 			NEXT_ARG();
216 			if (get_u32(&bt.phase_seg2, *argv, 0))
217 				invarg("invalid \"phase-seg2\" value\n", *argv);
218 		} else if (matches(*argv, "sjw") == 0) {
219 			NEXT_ARG();
220 			if (get_u32(&bt.sjw, *argv, 0))
221 				invarg("invalid \"sjw\" value\n", *argv);
222 		} else if (matches(*argv, "dbitrate") == 0) {
223 			NEXT_ARG();
224 			if (get_u32(&dbt.bitrate, *argv, 0))
225 				invarg("invalid \"dbitrate\" value\n", *argv);
226 		} else if (matches(*argv, "dsample-point") == 0) {
227 			float sp;
228 
229 			NEXT_ARG();
230 			if (get_float(&sp, *argv))
231 				invarg("invalid \"dsample-point\" value\n", *argv);
232 			dbt.sample_point = (__u32)(sp * 1000);
233 		} else if (matches(*argv, "dtq") == 0) {
234 			NEXT_ARG();
235 			if (get_u32(&dbt.tq, *argv, 0))
236 				invarg("invalid \"dtq\" value\n", *argv);
237 		} else if (matches(*argv, "dprop-seg") == 0) {
238 			NEXT_ARG();
239 			if (get_u32(&dbt.prop_seg, *argv, 0))
240 				invarg("invalid \"dprop-seg\" value\n", *argv);
241 		} else if (matches(*argv, "dphase-seg1") == 0) {
242 			NEXT_ARG();
243 			if (get_u32(&dbt.phase_seg1, *argv, 0))
244 				invarg("invalid \"dphase-seg1\" value\n", *argv);
245 		} else if (matches(*argv, "dphase-seg2") == 0) {
246 			NEXT_ARG();
247 			if (get_u32(&dbt.phase_seg2, *argv, 0))
248 				invarg("invalid \"dphase-seg2\" value\n", *argv);
249 		} else if (matches(*argv, "dsjw") == 0) {
250 			NEXT_ARG();
251 			if (get_u32(&dbt.sjw, *argv, 0))
252 				invarg("invalid \"dsjw\" value\n", *argv);
253 		} else if (matches(*argv, "loopback") == 0) {
254 			NEXT_ARG();
255 			set_ctrlmode("loopback", *argv, &cm,
256 				     CAN_CTRLMODE_LOOPBACK);
257 		} else if (matches(*argv, "listen-only") == 0) {
258 			NEXT_ARG();
259 			set_ctrlmode("listen-only", *argv, &cm,
260 				     CAN_CTRLMODE_LISTENONLY);
261 		} else if (matches(*argv, "triple-sampling") == 0) {
262 			NEXT_ARG();
263 			set_ctrlmode("triple-sampling", *argv, &cm,
264 				     CAN_CTRLMODE_3_SAMPLES);
265 		} else if (matches(*argv, "one-shot") == 0) {
266 			NEXT_ARG();
267 			set_ctrlmode("one-shot", *argv, &cm,
268 				     CAN_CTRLMODE_ONE_SHOT);
269 		} else if (matches(*argv, "berr-reporting") == 0) {
270 			NEXT_ARG();
271 			set_ctrlmode("berr-reporting", *argv, &cm,
272 				     CAN_CTRLMODE_BERR_REPORTING);
273 		} else if (matches(*argv, "fd") == 0) {
274 			NEXT_ARG();
275 			set_ctrlmode("fd", *argv, &cm,
276 				     CAN_CTRLMODE_FD);
277 		} else if (matches(*argv, "fd-non-iso") == 0) {
278 			NEXT_ARG();
279 			set_ctrlmode("fd-non-iso", *argv, &cm,
280 				     CAN_CTRLMODE_FD_NON_ISO);
281 		} else if (matches(*argv, "presume-ack") == 0) {
282 			NEXT_ARG();
283 			set_ctrlmode("presume-ack", *argv, &cm,
284 				     CAN_CTRLMODE_PRESUME_ACK);
285 #if defined(CAN_CTRLMODE_CC_LEN8_DLC)
286 		} else if (matches(*argv, "cc-len8-dlc") == 0) {
287 			NEXT_ARG();
288 			set_ctrlmode("cc-len8-dlc", *argv, &cm,
289 				     CAN_CTRLMODE_CC_LEN8_DLC);
290 #endif
291 		} else if (matches(*argv, "restart") == 0) {
292 			__u32 val = 1;
293 
294 			mnl_attr_put(nlh, IFLA_CAN_RESTART, sizeof(val), &val);
295 		} else if (matches(*argv, "restart-ms") == 0) {
296 			__u32 val;
297 
298 			NEXT_ARG();
299 			if (get_u32(&val, *argv, 0))
300 				invarg("invalid \"restart-ms\" value\n", *argv);
301 
302 			mnl_attr_put(nlh, IFLA_CAN_RESTART_MS, sizeof(val), &val);
303 		} else if (matches(*argv, "termination") == 0) {
304 			__u16 val;
305 
306 			NEXT_ARG();
307 			if (get_u16(&val, *argv, 0))
308 				invarg("invalid \"termination\" value\n",
309 				       *argv);
310 
311 			mnl_attr_put(nlh, IFLA_CAN_TERMINATION, sizeof(val), &val);
312 		} else {
313 			fprintf(stderr, "unknown option \"%s\"\n", *argv);
314 			usage();
315 			return -1;
316 		}
317 
318 		NEXT_ARG();
319 	}
320 
321 	if (bt.bitrate || bt.tq)
322 		mnl_attr_put(nlh, IFLA_CAN_BITTIMING, sizeof(bt), &bt);
323 
324 	if (cm.mask)
325 		mnl_attr_put(nlh, IFLA_CAN_CTRLMODE, sizeof(cm), &cm);
326 
327 	return 0;
328 }
329 
main(int argc,char * argv[])330 int main(int argc, char *argv[])
331 {
332 	char buf[MNL_SOCKET_BUFFER_SIZE];
333 	struct mnl_socket *nl;
334 	struct nlmsghdr *nlh;
335 	struct ifinfomsg *ifm;
336 	int ret;
337 	unsigned int seq, portid;
338 	struct nlattr *linkinfo, *data;
339 	const char *signatures[] = {
340 		"ip", "link", "set", ""
341 	};
342 	char *type = NULL;
343 	char *dev = NULL;
344 	int i;
345 
346 	NEXT_ARG();
347 	for (i = 0; argc > 0 && signatures[i][0];) {
348 		if (matches(*argv, signatures[i]))
349 			incomplete_command();
350 
351 		NEXT_ARG();
352 		i++;
353 	}
354 
355 	if (argc == 0)
356 		incomplete_command();
357 
358 	nlh = mnl_nlmsg_put_header(buf);
359 	nlh->nlmsg_type	= RTM_NEWLINK;
360 	nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
361 	nlh->nlmsg_seq = seq = time(NULL);
362 	ifm = mnl_nlmsg_put_extra_header(nlh, sizeof(*ifm));
363 	ifm->ifi_family = AF_UNSPEC;
364 	ifm->ifi_change = 0;
365 	ifm->ifi_flags = 0;
366 
367 	while (argc > 0) {
368 		if (matches(*argv, "up") == 0) {
369 			ifm->ifi_change |= IFF_UP;
370 			ifm->ifi_flags |= IFF_UP;
371 		} else if (matches(*argv, "down") == 0) {
372 			ifm->ifi_change |= IFF_UP;
373 			ifm->ifi_flags &= ~IFF_UP;
374 		} else if (matches(*argv, "type") == 0) {
375 			NEXT_ARG();
376 			type = *argv;
377 			NEXT_ARG();
378 			break;
379 		} else if (matches(*argv, "help") == 0) {
380 			usage();
381 			exit(EXIT_FAILURE);
382 		} else {
383 			if (matches(*argv, "dev") == 0)
384 				NEXT_ARG();
385 
386 			if (dev)
387 				duparg2("dev", *argv);
388 
389 			dev = *argv;
390 		}
391 
392 		NEXT_ARG();
393 	}
394 
395 	if (dev)
396 		mnl_attr_put_str(nlh, IFLA_IFNAME, dev);
397 
398 	if (type) {
399 		if (matches(type, "can")) {
400 			fprintf(stderr, "unknown type \"%s\"\n", type);
401 			usage();
402 			exit(EXIT_FAILURE);
403 		}
404 
405 		linkinfo = mnl_attr_nest_start(nlh, IFLA_LINKINFO);
406 		mnl_attr_put_str(nlh, IFLA_INFO_KIND, "can");
407 		data = mnl_attr_nest_start(nlh, IFLA_INFO_DATA);
408 
409 		if (iplink_set_can_parse(argc, argv, nlh))
410 			return -1;
411 
412 		mnl_attr_nest_end(nlh, data);
413 		mnl_attr_nest_end(nlh, linkinfo);
414 	}
415 
416 	nl = mnl_socket_open(NETLINK_ROUTE);
417 	if (nl == NULL) {
418 		perror("mnl_socket_open");
419 		exit(EXIT_FAILURE);
420 	}
421 
422 	if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0) {
423 		perror("mnl_socket_bind");
424 		exit(EXIT_FAILURE);
425 	}
426 
427 	portid = mnl_socket_get_portid(nl);
428 
429 	mnl_nlmsg_fprintf(stdout, nlh, nlh->nlmsg_len,
430 			  sizeof(struct ifinfomsg));
431 
432 	if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) {
433 		perror("mnl_socket_sendto");
434 		exit(EXIT_FAILURE);
435 	}
436 
437 	ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
438 	if (ret == -1) {
439 		perror("mnl_socket_recvfrom");
440 		exit(EXIT_FAILURE);
441 	}
442 
443 	ret = mnl_cb_run(buf, ret, seq, portid, NULL, NULL);
444 	if (ret == -1) {
445 		perror("mnl_cb_run");
446 		exit(EXIT_FAILURE);
447 	}
448 
449 	mnl_socket_close(nl);
450 
451 	return 0;
452 }
453