]>
git.proxmox.com Git - mirror_iproute2.git/blob - ip/ipfou.c
2 * ipfou.c FOU (foo over UDP) support
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.
9 * Authors: Tom Herbert <therbert@google.com>
17 #include <linux/fou.h>
18 #include <linux/genetlink.h>
20 #include <arpa/inet.h>
24 #include "ip_common.h"
26 static void usage(void)
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");
31 fprintf(stderr
, "\n");
32 fprintf(stderr
, "Where: PROTO { ipproto-name | 1..255 }\n");
33 fprintf(stderr
, " PORT { 1..65535 }\n");
39 static struct rtnl_handle genl_rth
= { .fd
= -1 };
40 static int genl_family
= -1;
42 #define FOU_REQUEST(_req, _bufsiz, _cmd, _flags) \
43 GENL_REQUEST(_req, _bufsiz, genl_family, 0, \
44 FOU_GENL_VERSION, _cmd, _flags)
46 static int fou_parse_opt(int argc
, char **argv
, struct nlmsghdr
*n
,
54 unsigned short family
= AF_INET
;
57 if (!matches(*argv
, "port")) {
60 if (get_be16(&port
, *argv
, 0) || port
== 0)
61 invarg("invalid port", *argv
);
63 } else if (!matches(*argv
, "ipproto")) {
64 struct protoent
*servptr
;
68 servptr
= getprotobyname(*argv
);
70 ipproto
= servptr
->p_proto
;
71 else if (get_u8(&ipproto
, *argv
, 0) || ipproto
== 0)
72 invarg("invalid ipproto", *argv
);
74 } else if (!matches(*argv
, "gue")) {
76 } else if (!matches(*argv
, "-6")) {
79 fprintf(stderr
, "fou: unknown command \"%s\"?\n", *argv
);
87 fprintf(stderr
, "fou: missing port\n");
91 if (!ipproto_set
&& !gue_set
&& adding
) {
92 fprintf(stderr
, "fou: must set ipproto or gue\n");
96 if (ipproto_set
&& gue_set
) {
97 fprintf(stderr
, "fou: cannot set ipproto and gue\n");
101 type
= gue_set
? FOU_ENCAP_GUE
: FOU_ENCAP_DIRECT
;
103 addattr16(n
, 1024, FOU_ATTR_PORT
, port
);
104 addattr8(n
, 1024, FOU_ATTR_TYPE
, type
);
105 addattr16(n
, 1024, FOU_ATTR_AF
, family
);
108 addattr8(n
, 1024, FOU_ATTR_IPPROTO
, ipproto
);
113 static int do_add(int argc
, char **argv
)
115 FOU_REQUEST(req
, 1024, FOU_CMD_ADD
, NLM_F_REQUEST
);
117 fou_parse_opt(argc
, argv
, &req
.n
, true);
119 if (rtnl_talk(&genl_rth
, &req
.n
, NULL
) < 0)
125 static int do_del(int argc
, char **argv
)
127 FOU_REQUEST(req
, 1024, FOU_CMD_DEL
, NLM_F_REQUEST
);
129 fou_parse_opt(argc
, argv
, &req
.n
, false);
131 if (rtnl_talk(&genl_rth
, &req
.n
, NULL
) < 0)
137 int do_ipfou(int argc
, char **argv
)
142 if (matches(*argv
, "help") == 0)
145 if (genl_init_handle(&genl_rth
, FOU_GENL_NAME
, &genl_family
))
148 if (matches(*argv
, "add") == 0)
149 return do_add(argc
-1, argv
+1);
150 if (matches(*argv
, "delete") == 0)
151 return do_del(argc
-1, argv
+1);
152 fprintf(stderr
, "Command \"%s\" is unknown, try \"ip fou help\".\n", *argv
);