]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * ipfou.c FOU (foo over UDP) support | |
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: Tom Herbert <therbert@google.com> | |
10 | */ | |
11 | ||
12 | #include <netdb.h> | |
13 | #include <stdio.h> | |
14 | #include <stdlib.h> | |
15 | #include <string.h> | |
16 | #include <net/if.h> | |
17 | #include <linux/fou.h> | |
18 | #include <linux/genetlink.h> | |
19 | #include <linux/ip.h> | |
20 | #include <arpa/inet.h> | |
21 | ||
22 | #include "libgenl.h" | |
23 | #include "utils.h" | |
24 | #include "ip_common.h" | |
25 | #include "json_print.h" | |
26 | ||
27 | static void usage(void) | |
28 | { | |
29 | fprintf(stderr, | |
30 | "Usage: ip fou add port PORT { ipproto PROTO | gue } [ -6 ]\n" | |
31 | " ip fou del port PORT [ -6 ]\n" | |
32 | " ip fou show\n" | |
33 | "\n" | |
34 | "Where: PROTO { ipproto-name | 1..255 }\n" | |
35 | " PORT { 1..65535 }\n"); | |
36 | ||
37 | exit(-1); | |
38 | } | |
39 | ||
40 | /* netlink socket */ | |
41 | static struct rtnl_handle genl_rth = { .fd = -1 }; | |
42 | static int genl_family = -1; | |
43 | ||
44 | #define FOU_REQUEST(_req, _bufsiz, _cmd, _flags) \ | |
45 | GENL_REQUEST(_req, _bufsiz, genl_family, 0, \ | |
46 | FOU_GENL_VERSION, _cmd, _flags) | |
47 | ||
48 | static int fou_parse_opt(int argc, char **argv, struct nlmsghdr *n, | |
49 | bool adding) | |
50 | { | |
51 | __u16 port; | |
52 | int port_set = 0; | |
53 | __u8 ipproto, type; | |
54 | bool gue_set = false; | |
55 | int ipproto_set = 0; | |
56 | __u8 family = AF_INET; | |
57 | ||
58 | while (argc > 0) { | |
59 | if (!matches(*argv, "port")) { | |
60 | NEXT_ARG(); | |
61 | ||
62 | if (get_be16(&port, *argv, 0) || port == 0) | |
63 | invarg("invalid port", *argv); | |
64 | port_set = 1; | |
65 | } else if (!matches(*argv, "ipproto")) { | |
66 | struct protoent *servptr; | |
67 | ||
68 | NEXT_ARG(); | |
69 | ||
70 | servptr = getprotobyname(*argv); | |
71 | if (servptr) | |
72 | ipproto = servptr->p_proto; | |
73 | else if (get_u8(&ipproto, *argv, 0) || ipproto == 0) | |
74 | invarg("invalid ipproto", *argv); | |
75 | ipproto_set = 1; | |
76 | } else if (!matches(*argv, "gue")) { | |
77 | gue_set = true; | |
78 | } else if (!matches(*argv, "-6")) { | |
79 | family = AF_INET6; | |
80 | } else { | |
81 | fprintf(stderr | |
82 | , "fou: unknown command \"%s\"?\n", *argv); | |
83 | usage(); | |
84 | return -1; | |
85 | } | |
86 | argc--, argv++; | |
87 | } | |
88 | ||
89 | if (!port_set) { | |
90 | fprintf(stderr, "fou: missing port\n"); | |
91 | return -1; | |
92 | } | |
93 | ||
94 | if (!ipproto_set && !gue_set && adding) { | |
95 | fprintf(stderr, "fou: must set ipproto or gue\n"); | |
96 | return -1; | |
97 | } | |
98 | ||
99 | if (ipproto_set && gue_set) { | |
100 | fprintf(stderr, "fou: cannot set ipproto and gue\n"); | |
101 | return -1; | |
102 | } | |
103 | ||
104 | type = gue_set ? FOU_ENCAP_GUE : FOU_ENCAP_DIRECT; | |
105 | ||
106 | addattr16(n, 1024, FOU_ATTR_PORT, port); | |
107 | addattr8(n, 1024, FOU_ATTR_TYPE, type); | |
108 | addattr8(n, 1024, FOU_ATTR_AF, family); | |
109 | ||
110 | if (ipproto_set) | |
111 | addattr8(n, 1024, FOU_ATTR_IPPROTO, ipproto); | |
112 | ||
113 | return 0; | |
114 | } | |
115 | ||
116 | static int do_add(int argc, char **argv) | |
117 | { | |
118 | FOU_REQUEST(req, 1024, FOU_CMD_ADD, NLM_F_REQUEST); | |
119 | ||
120 | fou_parse_opt(argc, argv, &req.n, true); | |
121 | ||
122 | if (rtnl_talk(&genl_rth, &req.n, NULL) < 0) | |
123 | return -2; | |
124 | ||
125 | return 0; | |
126 | } | |
127 | ||
128 | static int do_del(int argc, char **argv) | |
129 | { | |
130 | FOU_REQUEST(req, 1024, FOU_CMD_DEL, NLM_F_REQUEST); | |
131 | ||
132 | fou_parse_opt(argc, argv, &req.n, false); | |
133 | ||
134 | if (rtnl_talk(&genl_rth, &req.n, NULL) < 0) | |
135 | return -2; | |
136 | ||
137 | return 0; | |
138 | } | |
139 | ||
140 | static int print_fou_mapping(struct nlmsghdr *n, void *arg) | |
141 | { | |
142 | struct genlmsghdr *ghdr; | |
143 | struct rtattr *tb[FOU_ATTR_MAX + 1]; | |
144 | int len = n->nlmsg_len; | |
145 | ||
146 | if (n->nlmsg_type != genl_family) | |
147 | return 0; | |
148 | ||
149 | len -= NLMSG_LENGTH(GENL_HDRLEN); | |
150 | if (len < 0) | |
151 | return -1; | |
152 | ||
153 | ghdr = NLMSG_DATA(n); | |
154 | parse_rtattr(tb, FOU_ATTR_MAX, (void *) ghdr + GENL_HDRLEN, len); | |
155 | ||
156 | open_json_object(NULL); | |
157 | if (tb[FOU_ATTR_PORT]) | |
158 | print_uint(PRINT_ANY, "port", "port %u", | |
159 | ntohs(rta_getattr_u16(tb[FOU_ATTR_PORT]))); | |
160 | ||
161 | if (tb[FOU_ATTR_TYPE] && | |
162 | rta_getattr_u8(tb[FOU_ATTR_TYPE]) == FOU_ENCAP_GUE) | |
163 | print_null(PRINT_ANY, "gue", " gue", NULL); | |
164 | else if (tb[FOU_ATTR_IPPROTO]) | |
165 | print_uint(PRINT_ANY, "ipproto", | |
166 | " ipproto %u", rta_getattr_u8(tb[FOU_ATTR_IPPROTO])); | |
167 | ||
168 | if (tb[FOU_ATTR_AF]) { | |
169 | __u8 family = rta_getattr_u8(tb[FOU_ATTR_AF]); | |
170 | ||
171 | print_string(PRINT_JSON, "family", NULL, | |
172 | family_name(family)); | |
173 | ||
174 | if (family == AF_INET6) | |
175 | print_string(PRINT_FP, NULL, | |
176 | " -6", NULL); | |
177 | } | |
178 | print_string(PRINT_FP, NULL, "\n", NULL); | |
179 | close_json_object(); | |
180 | ||
181 | return 0; | |
182 | } | |
183 | ||
184 | static int do_show(int argc, char **argv) | |
185 | { | |
186 | FOU_REQUEST(req, 4096, FOU_CMD_GET, NLM_F_REQUEST | NLM_F_DUMP); | |
187 | ||
188 | if (argc > 0) { | |
189 | fprintf(stderr, | |
190 | "\"ip fou show\" does not take any arguments.\n"); | |
191 | return -1; | |
192 | } | |
193 | ||
194 | if (rtnl_send(&genl_rth, &req.n, req.n.nlmsg_len) < 0) { | |
195 | perror("Cannot send show request"); | |
196 | exit(1); | |
197 | } | |
198 | ||
199 | new_json_obj(json); | |
200 | if (rtnl_dump_filter(&genl_rth, print_fou_mapping, stdout) < 0) { | |
201 | fprintf(stderr, "Dump terminated\n"); | |
202 | return 1; | |
203 | } | |
204 | delete_json_obj(); | |
205 | fflush(stdout); | |
206 | ||
207 | return 0; | |
208 | } | |
209 | ||
210 | int do_ipfou(int argc, char **argv) | |
211 | { | |
212 | if (argc < 1) | |
213 | usage(); | |
214 | ||
215 | if (matches(*argv, "help") == 0) | |
216 | usage(); | |
217 | ||
218 | if (genl_init_handle(&genl_rth, FOU_GENL_NAME, &genl_family)) | |
219 | exit(1); | |
220 | ||
221 | if (matches(*argv, "add") == 0) | |
222 | return do_add(argc-1, argv+1); | |
223 | if (matches(*argv, "delete") == 0) | |
224 | return do_del(argc-1, argv+1); | |
225 | if (matches(*argv, "show") == 0) | |
226 | return do_show(argc-1, argv+1); | |
227 | ||
228 | fprintf(stderr, | |
229 | "Command \"%s\" is unknown, try \"ip fou help\".\n", *argv); | |
230 | exit(-1); | |
231 | } |