blob: 00dbe150710d2135bf62dfb47bd7c61168259133 [file] [log] [blame]
Tom Herbert69287472014-11-05 10:06:24 -08001/*
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{
Tom Herbert8bd31d82016-08-04 13:34:57 -070028 fprintf(stderr, "Usage: ip fou add port PORT "
29 "{ ipproto PROTO | gue } [ -6 ]\n");
30 fprintf(stderr, " ip fou del port PORT [ -6 ]\n");
Tom Herbert69287472014-11-05 10:06:24 -080031 fprintf(stderr, "\n");
32 fprintf(stderr, "Where: PROTO { ipproto-name | 1..255 }\n");
33 fprintf(stderr, " PORT { 1..65535 }\n");
34
35 exit(-1);
36}
37
38/* netlink socket */
39static struct rtnl_handle genl_rth = { .fd = -1 };
40static int genl_family = -1;
41
42#define FOU_REQUEST(_req, _bufsiz, _cmd, _flags) \
43 GENL_REQUEST(_req, _bufsiz, genl_family, 0, \
44 FOU_GENL_VERSION, _cmd, _flags)
45
46static int fou_parse_opt(int argc, char **argv, struct nlmsghdr *n,
47 bool adding)
48{
49 __u16 port;
50 int port_set = 0;
51 __u8 ipproto, type;
52 bool gue_set = false;
53 int ipproto_set = 0;
Tom Herbert8bd31d82016-08-04 13:34:57 -070054 unsigned short family = AF_INET;
Tom Herbert69287472014-11-05 10:06:24 -080055
56 while (argc > 0) {
57 if (!matches(*argv, "port")) {
58 NEXT_ARG();
59
Sabrina Dubroca9f7401f2016-06-03 16:45:46 +020060 if (get_be16(&port, *argv, 0) || port == 0)
Tom Herbert69287472014-11-05 10:06:24 -080061 invarg("invalid port", *argv);
Tom Herbert69287472014-11-05 10:06:24 -080062 port_set = 1;
63 } else if (!matches(*argv, "ipproto")) {
64 struct protoent *servptr;
65
66 NEXT_ARG();
67
68 servptr = getprotobyname(*argv);
69 if (servptr)
70 ipproto = servptr->p_proto;
71 else if (get_u8(&ipproto, *argv, 0) || ipproto == 0)
72 invarg("invalid ipproto", *argv);
73 ipproto_set = 1;
74 } else if (!matches(*argv, "gue")) {
75 gue_set = true;
Tom Herbert8bd31d82016-08-04 13:34:57 -070076 } else if (!matches(*argv, "-6")) {
77 family = AF_INET6;
Tom Herbert69287472014-11-05 10:06:24 -080078 } else {
79 fprintf(stderr, "fou: unknown command \"%s\"?\n", *argv);
80 usage();
81 return -1;
82 }
83 argc--, argv++;
84 }
85
86 if (!port_set) {
87 fprintf(stderr, "fou: missing port\n");
88 return -1;
89 }
90
91 if (!ipproto_set && !gue_set && adding) {
92 fprintf(stderr, "fou: must set ipproto or gue\n");
93 return -1;
94 }
95
96 if (ipproto_set && gue_set) {
97 fprintf(stderr, "fou: cannot set ipproto and gue\n");
98 return -1;
99 }
100
101 type = gue_set ? FOU_ENCAP_GUE : FOU_ENCAP_DIRECT;
102
103 addattr16(n, 1024, FOU_ATTR_PORT, port);
104 addattr8(n, 1024, FOU_ATTR_TYPE, type);
Tom Herbert8bd31d82016-08-04 13:34:57 -0700105 addattr16(n, 1024, FOU_ATTR_AF, family);
Tom Herbert69287472014-11-05 10:06:24 -0800106
107 if (ipproto_set)
108 addattr8(n, 1024, FOU_ATTR_IPPROTO, ipproto);
109
110 return 0;
111}
112
113static int do_add(int argc, char **argv)
114{
115 FOU_REQUEST(req, 1024, FOU_CMD_ADD, NLM_F_REQUEST);
116
117 fou_parse_opt(argc, argv, &req.n, true);
118
Stephen Hemmingerc079e122015-05-27 12:26:14 -0700119 if (rtnl_talk(&genl_rth, &req.n, NULL, 0) < 0)
Tom Herbert69287472014-11-05 10:06:24 -0800120 return -2;
121
122 return 0;
123}
124
125static int do_del(int argc, char **argv)
126{
127 FOU_REQUEST(req, 1024, FOU_CMD_DEL, NLM_F_REQUEST);
128
129 fou_parse_opt(argc, argv, &req.n, false);
130
Stephen Hemmingerc079e122015-05-27 12:26:14 -0700131 if (rtnl_talk(&genl_rth, &req.n, NULL, 0) < 0)
Tom Herbert69287472014-11-05 10:06:24 -0800132 return -2;
133
134 return 0;
135}
136
137int do_ipfou(int argc, char **argv)
138{
Tom Herbert69287472014-11-05 10:06:24 -0800139 if (argc < 1)
140 usage();
141
Sabrina Dubrocad240a0e2016-08-16 16:26:57 +0200142 if (matches(*argv, "help") == 0)
143 usage();
144
145 if (genl_init_handle(&genl_rth, FOU_GENL_NAME, &genl_family))
146 exit(1);
147
Tom Herbert69287472014-11-05 10:06:24 -0800148 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);
Tom Herbert69287472014-11-05 10:06:24 -0800152 fprintf(stderr, "Command \"%s\" is unknown, try \"ip fou help\".\n", *argv);
153 exit(-1);
154}