]> git.proxmox.com Git - mirror_iproute2.git/blame - ip/ipfou.c
fou: break long lines
[mirror_iproute2.git] / ip / ipfou.c
CommitLineData
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
26static void usage(void)
27{
5c92c2ee
SH
28 fprintf(stderr,
29 "Usage: ip fou add port PORT { ipproto PROTO | gue } [ -6 ]\n"
30 " ip fou del port PORT [ -6 ]\n"
31 " ip fou show\n"
32 "\n"
33 "Where: PROTO { ipproto-name | 1..255 }\n"
34 " PORT { 1..65535 }\n");
6928747b
TH
35
36 exit(-1);
37}
38
39/* netlink socket */
40static struct rtnl_handle genl_rth = { .fd = -1 };
41static 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
47static 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;
33f6dd23 55 __u8 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 79 } else {
5c92c2ee
SH
80 fprintf(stderr
81 , "fou: unknown command \"%s\"?\n", *argv);
6928747b
TH
82 usage();
83 return -1;
84 }
85 argc--, argv++;
86 }
87
88 if (!port_set) {
89 fprintf(stderr, "fou: missing port\n");
90 return -1;
91 }
92
93 if (!ipproto_set && !gue_set && adding) {
94 fprintf(stderr, "fou: must set ipproto or gue\n");
95 return -1;
96 }
97
98 if (ipproto_set && gue_set) {
99 fprintf(stderr, "fou: cannot set ipproto and gue\n");
100 return -1;
101 }
102
103 type = gue_set ? FOU_ENCAP_GUE : FOU_ENCAP_DIRECT;
104
105 addattr16(n, 1024, FOU_ATTR_PORT, port);
106 addattr8(n, 1024, FOU_ATTR_TYPE, type);
33f6dd23 107 addattr8(n, 1024, FOU_ATTR_AF, family);
6928747b
TH
108
109 if (ipproto_set)
110 addattr8(n, 1024, FOU_ATTR_IPPROTO, ipproto);
111
112 return 0;
113}
114
115static int do_add(int argc, char **argv)
116{
117 FOU_REQUEST(req, 1024, FOU_CMD_ADD, NLM_F_REQUEST);
118
119 fou_parse_opt(argc, argv, &req.n, true);
120
86bf43c7 121 if (rtnl_talk(&genl_rth, &req.n, NULL) < 0)
6928747b
TH
122 return -2;
123
124 return 0;
125}
126
127static int do_del(int argc, char **argv)
128{
129 FOU_REQUEST(req, 1024, FOU_CMD_DEL, NLM_F_REQUEST);
130
131 fou_parse_opt(argc, argv, &req.n, false);
132
86bf43c7 133 if (rtnl_talk(&genl_rth, &req.n, NULL) < 0)
6928747b
TH
134 return -2;
135
136 return 0;
137}
138
cf4caf33
GG
139static int print_fou_mapping(const struct sockaddr_nl *who,
140 struct nlmsghdr *n, void *arg)
141{
142 FILE *fp = (FILE *)arg;
143 struct genlmsghdr *ghdr;
144 struct rtattr *tb[FOU_ATTR_MAX + 1];
145 int len = n->nlmsg_len;
5c92c2ee 146 unsigned int family;
cf4caf33
GG
147
148 if (n->nlmsg_type != genl_family)
149 return 0;
150
151 len -= NLMSG_LENGTH(GENL_HDRLEN);
152 if (len < 0)
153 return -1;
154
155 ghdr = NLMSG_DATA(n);
156 parse_rtattr(tb, FOU_ATTR_MAX, (void *) ghdr + GENL_HDRLEN, len);
157
158 if (tb[FOU_ATTR_PORT])
159 fprintf(fp, "port %u", ntohs(rta_getattr_u16(tb[FOU_ATTR_PORT])));
160 if (tb[FOU_ATTR_TYPE] && rta_getattr_u8(tb[FOU_ATTR_TYPE]) == FOU_ENCAP_GUE)
161 fprintf(fp, " gue");
162 else if (tb[FOU_ATTR_IPPROTO])
163 fprintf(fp, " ipproto %u", rta_getattr_u8(tb[FOU_ATTR_IPPROTO]));
164 if (tb[FOU_ATTR_AF]) {
165 family = rta_getattr_u8(tb[FOU_ATTR_AF]);
166 if (family == AF_INET6)
167 fprintf(fp, " -6");
168 }
169 fprintf(fp, "\n");
170
171 return 0;
172}
173
174static int do_show(int argc, char **argv)
175{
176 FOU_REQUEST(req, 4096, FOU_CMD_GET, NLM_F_REQUEST | NLM_F_DUMP);
177
178 if (argc > 0) {
5c92c2ee
SH
179 fprintf(stderr,
180 "\"ip fou show\" does not take any arguments.\n");
cf4caf33
GG
181 return -1;
182 }
183
184 if (rtnl_send(&genl_rth, &req.n, req.n.nlmsg_len) < 0) {
185 perror("Cannot send show request");
186 exit(1);
187 }
188
189 if (rtnl_dump_filter(&genl_rth, print_fou_mapping, stdout) < 0) {
190 fprintf(stderr, "Dump terminated\n");
191 return 1;
192 }
193
194 return 0;
195}
196
6928747b
TH
197int do_ipfou(int argc, char **argv)
198{
6928747b
TH
199 if (argc < 1)
200 usage();
201
d240a0e1
SD
202 if (matches(*argv, "help") == 0)
203 usage();
204
205 if (genl_init_handle(&genl_rth, FOU_GENL_NAME, &genl_family))
206 exit(1);
207
6928747b
TH
208 if (matches(*argv, "add") == 0)
209 return do_add(argc-1, argv+1);
210 if (matches(*argv, "delete") == 0)
211 return do_del(argc-1, argv+1);
cf4caf33
GG
212 if (matches(*argv, "show") == 0)
213 return do_show(argc-1, argv+1);
5c92c2ee
SH
214
215 fprintf(stderr,
216 "Command \"%s\" is unknown, try \"ip fou help\".\n", *argv);
6928747b
TH
217 exit(-1);
218}