]>
Commit | Line | Data |
---|---|---|
6928747b TH |
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 | ||
26 | static void usage(void) | |
27 | { | |
8bd31d8d TH |
28 | fprintf(stderr, "Usage: ip fou add port PORT " |
29 | "{ ipproto PROTO | gue } [ -6 ]\n"); | |
30 | fprintf(stderr, " ip fou del port PORT [ -6 ]\n"); | |
cf4caf33 | 31 | fprintf(stderr, " ip fou show\n"); |
6928747b TH |
32 | fprintf(stderr, "\n"); |
33 | fprintf(stderr, "Where: PROTO { ipproto-name | 1..255 }\n"); | |
34 | fprintf(stderr, " PORT { 1..65535 }\n"); | |
35 | ||
36 | exit(-1); | |
37 | } | |
38 | ||
39 | /* netlink socket */ | |
40 | static struct rtnl_handle genl_rth = { .fd = -1 }; | |
41 | static int genl_family = -1; | |
42 | ||
43 | #define FOU_REQUEST(_req, _bufsiz, _cmd, _flags) \ | |
44 | GENL_REQUEST(_req, _bufsiz, genl_family, 0, \ | |
45 | FOU_GENL_VERSION, _cmd, _flags) | |
46 | ||
47 | static int fou_parse_opt(int argc, char **argv, struct nlmsghdr *n, | |
48 | bool adding) | |
49 | { | |
50 | __u16 port; | |
51 | int port_set = 0; | |
52 | __u8 ipproto, type; | |
53 | bool gue_set = false; | |
54 | int ipproto_set = 0; | |
8bd31d8d | 55 | unsigned short family = AF_INET; |
6928747b TH |
56 | |
57 | while (argc > 0) { | |
58 | if (!matches(*argv, "port")) { | |
59 | NEXT_ARG(); | |
60 | ||
9f7401fa | 61 | if (get_be16(&port, *argv, 0) || port == 0) |
6928747b | 62 | invarg("invalid port", *argv); |
6928747b TH |
63 | port_set = 1; |
64 | } else if (!matches(*argv, "ipproto")) { | |
65 | struct protoent *servptr; | |
66 | ||
67 | NEXT_ARG(); | |
68 | ||
69 | servptr = getprotobyname(*argv); | |
70 | if (servptr) | |
71 | ipproto = servptr->p_proto; | |
72 | else if (get_u8(&ipproto, *argv, 0) || ipproto == 0) | |
73 | invarg("invalid ipproto", *argv); | |
74 | ipproto_set = 1; | |
75 | } else if (!matches(*argv, "gue")) { | |
76 | gue_set = true; | |
8bd31d8d TH |
77 | } else if (!matches(*argv, "-6")) { |
78 | family = AF_INET6; | |
6928747b TH |
79 | } else { |
80 | fprintf(stderr, "fou: unknown command \"%s\"?\n", *argv); | |
81 | usage(); | |
82 | return -1; | |
83 | } | |
84 | argc--, argv++; | |
85 | } | |
86 | ||
87 | if (!port_set) { | |
88 | fprintf(stderr, "fou: missing port\n"); | |
89 | return -1; | |
90 | } | |
91 | ||
92 | if (!ipproto_set && !gue_set && adding) { | |
93 | fprintf(stderr, "fou: must set ipproto or gue\n"); | |
94 | return -1; | |
95 | } | |
96 | ||
97 | if (ipproto_set && gue_set) { | |
98 | fprintf(stderr, "fou: cannot set ipproto and gue\n"); | |
99 | return -1; | |
100 | } | |
101 | ||
102 | type = gue_set ? FOU_ENCAP_GUE : FOU_ENCAP_DIRECT; | |
103 | ||
104 | addattr16(n, 1024, FOU_ATTR_PORT, port); | |
105 | addattr8(n, 1024, FOU_ATTR_TYPE, type); | |
8bd31d8d | 106 | addattr16(n, 1024, FOU_ATTR_AF, family); |
6928747b TH |
107 | |
108 | if (ipproto_set) | |
109 | addattr8(n, 1024, FOU_ATTR_IPPROTO, ipproto); | |
110 | ||
111 | return 0; | |
112 | } | |
113 | ||
114 | static int do_add(int argc, char **argv) | |
115 | { | |
116 | FOU_REQUEST(req, 1024, FOU_CMD_ADD, NLM_F_REQUEST); | |
117 | ||
118 | fou_parse_opt(argc, argv, &req.n, true); | |
119 | ||
86bf43c7 | 120 | if (rtnl_talk(&genl_rth, &req.n, NULL) < 0) |
6928747b TH |
121 | return -2; |
122 | ||
123 | return 0; | |
124 | } | |
125 | ||
126 | static int do_del(int argc, char **argv) | |
127 | { | |
128 | FOU_REQUEST(req, 1024, FOU_CMD_DEL, NLM_F_REQUEST); | |
129 | ||
130 | fou_parse_opt(argc, argv, &req.n, false); | |
131 | ||
86bf43c7 | 132 | if (rtnl_talk(&genl_rth, &req.n, NULL) < 0) |
6928747b TH |
133 | return -2; |
134 | ||
135 | return 0; | |
136 | } | |
137 | ||
cf4caf33 GG |
138 | static int print_fou_mapping(const struct sockaddr_nl *who, |
139 | struct nlmsghdr *n, void *arg) | |
140 | { | |
141 | FILE *fp = (FILE *)arg; | |
142 | struct genlmsghdr *ghdr; | |
143 | struct rtattr *tb[FOU_ATTR_MAX + 1]; | |
144 | int len = n->nlmsg_len; | |
145 | unsigned family; | |
146 | ||
147 | if (n->nlmsg_type != genl_family) | |
148 | return 0; | |
149 | ||
150 | len -= NLMSG_LENGTH(GENL_HDRLEN); | |
151 | if (len < 0) | |
152 | return -1; | |
153 | ||
154 | ghdr = NLMSG_DATA(n); | |
155 | parse_rtattr(tb, FOU_ATTR_MAX, (void *) ghdr + GENL_HDRLEN, len); | |
156 | ||
157 | if (tb[FOU_ATTR_PORT]) | |
158 | fprintf(fp, "port %u", ntohs(rta_getattr_u16(tb[FOU_ATTR_PORT]))); | |
159 | if (tb[FOU_ATTR_TYPE] && rta_getattr_u8(tb[FOU_ATTR_TYPE]) == FOU_ENCAP_GUE) | |
160 | fprintf(fp, " gue"); | |
161 | else if (tb[FOU_ATTR_IPPROTO]) | |
162 | fprintf(fp, " ipproto %u", rta_getattr_u8(tb[FOU_ATTR_IPPROTO])); | |
163 | if (tb[FOU_ATTR_AF]) { | |
164 | family = rta_getattr_u8(tb[FOU_ATTR_AF]); | |
165 | if (family == AF_INET6) | |
166 | fprintf(fp, " -6"); | |
167 | } | |
168 | fprintf(fp, "\n"); | |
169 | ||
170 | return 0; | |
171 | } | |
172 | ||
173 | static int do_show(int argc, char **argv) | |
174 | { | |
175 | FOU_REQUEST(req, 4096, FOU_CMD_GET, NLM_F_REQUEST | NLM_F_DUMP); | |
176 | ||
177 | if (argc > 0) { | |
178 | fprintf(stderr, "\"ip fou show\" does not take any arguments.\n"); | |
179 | return -1; | |
180 | } | |
181 | ||
182 | if (rtnl_send(&genl_rth, &req.n, req.n.nlmsg_len) < 0) { | |
183 | perror("Cannot send show request"); | |
184 | exit(1); | |
185 | } | |
186 | ||
187 | if (rtnl_dump_filter(&genl_rth, print_fou_mapping, stdout) < 0) { | |
188 | fprintf(stderr, "Dump terminated\n"); | |
189 | return 1; | |
190 | } | |
191 | ||
192 | return 0; | |
193 | } | |
194 | ||
6928747b TH |
195 | int do_ipfou(int argc, char **argv) |
196 | { | |
6928747b TH |
197 | if (argc < 1) |
198 | usage(); | |
199 | ||
d240a0e1 SD |
200 | if (matches(*argv, "help") == 0) |
201 | usage(); | |
202 | ||
203 | if (genl_init_handle(&genl_rth, FOU_GENL_NAME, &genl_family)) | |
204 | exit(1); | |
205 | ||
6928747b TH |
206 | if (matches(*argv, "add") == 0) |
207 | return do_add(argc-1, argv+1); | |
208 | if (matches(*argv, "delete") == 0) | |
209 | return do_del(argc-1, argv+1); | |
cf4caf33 GG |
210 | if (matches(*argv, "show") == 0) |
211 | return do_show(argc-1, argv+1); | |
6928747b TH |
212 | fprintf(stderr, "Command \"%s\" is unknown, try \"ip fou help\".\n", *argv); |
213 | exit(-1); | |
214 | } |