xref: /aosp_15_r20/external/libnl/tests/test-create-macsec.c (revision 4dc78e53d49367fa8e61b07018507c90983a077d)
1 /* SPDX-License-Identifier: LGPL-2.1-only */
2 
3 #include "nl-default.h"
4 
5 #include <linux/netlink.h>
6 #include <linux/if_link.h>
7 
8 #include <netlink/route/link/macsec.h>
9 #include <netlink/netlink.h>
10 #include <netlink/route/link.h>
11 
main(int argc,char * argv[])12 int main(int argc, char *argv[])
13 {
14 	struct rtnl_link *link;
15 	struct nl_cache *link_cache;
16 	struct nl_sock *sk;
17 	int err, master_index;
18 
19 	sk = nl_socket_alloc();
20 	if ((err = nl_connect(sk, NETLINK_ROUTE)) < 0) {
21 		nl_perror(err, "Unable to connect socket");
22 		return err;
23 	}
24 
25 	if ((err = rtnl_link_alloc_cache(sk, AF_UNSPEC, &link_cache)) < 0) {
26 		nl_perror(err, "Unable to allocate cache");
27 		return err;
28 	}
29 
30 	if (!(master_index = rtnl_link_name2i(link_cache, "eth0"))) {
31 		fprintf(stderr, "Unable to lookup eth0");
32 		return -1;
33 	}
34 
35 
36 	link = rtnl_link_macsec_alloc();
37 
38 	rtnl_link_set_link(link, master_index);
39 
40 	rtnl_link_macsec_set_port(link, 10);
41 	rtnl_link_macsec_set_encrypt(link, 1);
42 	rtnl_link_macsec_set_replay_protect(link, 1);
43 	rtnl_link_macsec_set_window(link, 200);
44 
45 	if ((err = rtnl_link_add(sk, link, NLM_F_CREATE)) < 0) {
46 		nl_perror(err, "Unable to add link");
47 		return err;
48 	}
49 
50 	rtnl_link_put(link);
51 	nl_close(sk);
52 
53 	return 0;
54 }
55