]> git.proxmox.com Git - mirror_iproute2.git/blob - ip/iptoken.c
drop unneeded include of syslog.h
[mirror_iproute2.git] / ip / iptoken.c
1 /*
2 * iptoken.c "ip token"
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
8 *
9 * Authors: Daniel Borkmann, <borkmann@redhat.com>
10 */
11
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <stdbool.h>
15 #include <unistd.h>
16 #include <fcntl.h>
17 #include <string.h>
18 #include <sys/socket.h>
19 #include <netinet/in.h>
20 #include <netinet/ip.h>
21 #include <arpa/inet.h>
22 #include <linux/types.h>
23 #include <linux/if.h>
24
25 #include "rt_names.h"
26 #include "utils.h"
27 #include "ip_common.h"
28
29 extern struct rtnl_handle rth;
30
31 struct rtnl_dump_args {
32 FILE *fp;
33 int ifindex;
34 };
35
36 static void usage(void) __attribute__((noreturn));
37
38 static void usage(void)
39 {
40 fprintf(stderr, "Usage: ip token [ list | set | del | get ] [ TOKEN ] [ dev DEV ]\n");
41 exit(-1);
42 }
43
44 static int print_token(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
45 {
46 struct rtnl_dump_args *args = arg;
47 FILE *fp = args->fp;
48 int ifindex = args->ifindex;
49 struct ifinfomsg *ifi = NLMSG_DATA(n);
50 int len = n->nlmsg_len;
51 struct rtattr *tb[IFLA_MAX + 1];
52 struct rtattr *ltb[IFLA_INET6_MAX + 1];
53
54 if (n->nlmsg_type != RTM_NEWLINK)
55 return -1;
56
57 len -= NLMSG_LENGTH(sizeof(*ifi));
58 if (len < 0)
59 return -1;
60
61 if (ifi->ifi_family != AF_INET6)
62 return -1;
63 if (ifi->ifi_index == 0)
64 return -1;
65 if (ifindex > 0 && ifi->ifi_index != ifindex)
66 return 0;
67 if (ifi->ifi_flags & (IFF_LOOPBACK | IFF_NOARP))
68 return 0;
69
70 parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), len);
71 if (!tb[IFLA_PROTINFO])
72 return -1;
73
74 parse_rtattr_nested(ltb, IFLA_INET6_MAX, tb[IFLA_PROTINFO]);
75 if (!ltb[IFLA_INET6_TOKEN]) {
76 fprintf(stderr, "Seems there's no support for IPv6 token!\n");
77 return -1;
78 }
79
80 fprintf(fp, "token %s dev %s\n",
81 format_host_rta(ifi->ifi_family, ltb[IFLA_INET6_TOKEN]),
82 ll_index_to_name(ifi->ifi_index));
83 fflush(fp);
84
85 return 0;
86 }
87
88 static int iptoken_list(int argc, char **argv)
89 {
90 int af = AF_INET6;
91 struct rtnl_dump_args da = { .fp = stdout };
92
93 while (argc > 0) {
94 if (strcmp(*argv, "dev") == 0) {
95 NEXT_ARG();
96 if ((da.ifindex = ll_name_to_index(*argv)) == 0)
97 invarg("dev is invalid\n", *argv);
98 break;
99 }
100 argc--; argv++;
101 }
102
103 if (rtnl_wilddump_request(&rth, af, RTM_GETLINK) < 0) {
104 perror("Cannot send dump request");
105 return -1;
106 }
107
108 if (rtnl_dump_filter(&rth, print_token, &da) < 0) {
109 fprintf(stderr, "Dump terminated\n");
110 return -1;
111 }
112
113 return 0;
114 }
115
116 static int iptoken_set(int argc, char **argv, bool delete)
117 {
118 struct {
119 struct nlmsghdr n;
120 struct ifinfomsg ifi;
121 char buf[512];
122 } req = {
123 .n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg)),
124 .n.nlmsg_flags = NLM_F_REQUEST,
125 .n.nlmsg_type = RTM_SETLINK,
126 .ifi.ifi_family = AF_INET6,
127 };
128 struct rtattr *afs, *afs6;
129 bool have_token = delete, have_dev = false;
130 inet_prefix addr = { .bytelen = 16, };
131
132 while (argc > 0) {
133 if (strcmp(*argv, "dev") == 0) {
134 NEXT_ARG();
135 if (!have_dev) {
136 if ((req.ifi.ifi_index =
137 ll_name_to_index(*argv)) == 0)
138 invarg("dev is invalid\n", *argv);
139 have_dev = true;
140 }
141 } else {
142 if (matches(*argv, "help") == 0)
143 usage();
144 if (!have_token) {
145 get_prefix(&addr, *argv, req.ifi.ifi_family);
146 have_token = true;
147 }
148 }
149 argc--; argv++;
150 }
151
152 if (!have_token) {
153 fprintf(stderr, "Not enough information: token is required.\n");
154 return -1;
155 }
156 if (!have_dev) {
157 fprintf(stderr, "Not enough information: \"dev\" argument is required.\n");
158 return -1;
159 }
160
161 afs = addattr_nest(&req.n, sizeof(req), IFLA_AF_SPEC);
162 afs6 = addattr_nest(&req.n, sizeof(req), AF_INET6);
163 addattr_l(&req.n, sizeof(req), IFLA_INET6_TOKEN,
164 &addr.data, addr.bytelen);
165 addattr_nest_end(&req.n, afs6);
166 addattr_nest_end(&req.n, afs);
167
168 if (rtnl_talk(&rth, &req.n, NULL) < 0)
169 return -2;
170
171 return 0;
172 }
173
174 int do_iptoken(int argc, char **argv)
175 {
176 ll_init_map(&rth);
177
178 if (argc < 1) {
179 return iptoken_list(0, NULL);
180 } else if (matches(argv[0], "list") == 0 ||
181 matches(argv[0], "lst") == 0 ||
182 matches(argv[0], "show") == 0) {
183 return iptoken_list(argc - 1, argv + 1);
184 } else if (matches(argv[0], "set") == 0 ||
185 matches(argv[0], "add") == 0) {
186 return iptoken_set(argc - 1, argv + 1, false);
187 } else if (matches(argv[0], "delete") == 0) {
188 return iptoken_set(argc - 1, argv + 1, true);
189 } else if (matches(argv[0], "get") == 0) {
190 return iptoken_list(argc - 1, argv + 1);
191 } else if (matches(argv[0], "help") == 0)
192 usage();
193
194 fprintf(stderr, "Command \"%s\" is unknown, try \"ip token help\".\n", *argv);
195 exit(-1);
196 }