]> git.proxmox.com Git - mirror_iproute2.git/blame - ip/ipfou.c
gre6: Support for fou encapsulation
[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{
28 fprintf(stderr, "Usage: ip fou add port PORT { ipproto PROTO | gue }\n");
29 fprintf(stderr, " ip fou del port PORT\n");
30 fprintf(stderr, "\n");
31 fprintf(stderr, "Where: PROTO { ipproto-name | 1..255 }\n");
32 fprintf(stderr, " PORT { 1..65535 }\n");
33
34 exit(-1);
35}
36
37/* netlink socket */
38static struct rtnl_handle genl_rth = { .fd = -1 };
39static int genl_family = -1;
40
41#define FOU_REQUEST(_req, _bufsiz, _cmd, _flags) \
42 GENL_REQUEST(_req, _bufsiz, genl_family, 0, \
43 FOU_GENL_VERSION, _cmd, _flags)
44
45static int fou_parse_opt(int argc, char **argv, struct nlmsghdr *n,
46 bool adding)
47{
48 __u16 port;
49 int port_set = 0;
50 __u8 ipproto, type;
51 bool gue_set = false;
52 int ipproto_set = 0;
53
54 while (argc > 0) {
55 if (!matches(*argv, "port")) {
56 NEXT_ARG();
57
9f7401fa 58 if (get_be16(&port, *argv, 0) || port == 0)
6928747b 59 invarg("invalid port", *argv);
6928747b
TH
60 port_set = 1;
61 } else if (!matches(*argv, "ipproto")) {
62 struct protoent *servptr;
63
64 NEXT_ARG();
65
66 servptr = getprotobyname(*argv);
67 if (servptr)
68 ipproto = servptr->p_proto;
69 else if (get_u8(&ipproto, *argv, 0) || ipproto == 0)
70 invarg("invalid ipproto", *argv);
71 ipproto_set = 1;
72 } else if (!matches(*argv, "gue")) {
73 gue_set = true;
74 } else {
75 fprintf(stderr, "fou: unknown command \"%s\"?\n", *argv);
76 usage();
77 return -1;
78 }
79 argc--, argv++;
80 }
81
82 if (!port_set) {
83 fprintf(stderr, "fou: missing port\n");
84 return -1;
85 }
86
87 if (!ipproto_set && !gue_set && adding) {
88 fprintf(stderr, "fou: must set ipproto or gue\n");
89 return -1;
90 }
91
92 if (ipproto_set && gue_set) {
93 fprintf(stderr, "fou: cannot set ipproto and gue\n");
94 return -1;
95 }
96
97 type = gue_set ? FOU_ENCAP_GUE : FOU_ENCAP_DIRECT;
98
99 addattr16(n, 1024, FOU_ATTR_PORT, port);
100 addattr8(n, 1024, FOU_ATTR_TYPE, type);
101
102 if (ipproto_set)
103 addattr8(n, 1024, FOU_ATTR_IPPROTO, ipproto);
104
105 return 0;
106}
107
108static int do_add(int argc, char **argv)
109{
110 FOU_REQUEST(req, 1024, FOU_CMD_ADD, NLM_F_REQUEST);
111
112 fou_parse_opt(argc, argv, &req.n, true);
113
c079e121 114 if (rtnl_talk(&genl_rth, &req.n, NULL, 0) < 0)
6928747b
TH
115 return -2;
116
117 return 0;
118}
119
120static int do_del(int argc, char **argv)
121{
122 FOU_REQUEST(req, 1024, FOU_CMD_DEL, NLM_F_REQUEST);
123
124 fou_parse_opt(argc, argv, &req.n, false);
125
c079e121 126 if (rtnl_talk(&genl_rth, &req.n, NULL, 0) < 0)
6928747b
TH
127 return -2;
128
129 return 0;
130}
131
132int do_ipfou(int argc, char **argv)
133{
134 if (genl_family < 0) {
135 if (rtnl_open_byproto(&genl_rth, 0, NETLINK_GENERIC) < 0) {
136 fprintf(stderr, "Cannot open generic netlink socket\n");
137 exit(1);
138 }
139
140 genl_family = genl_resolve_family(&genl_rth, FOU_GENL_NAME);
141 if (genl_family < 0)
142 exit(1);
143 }
144
145 if (argc < 1)
146 usage();
147
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 if (matches(*argv, "help") == 0)
153 usage();
154
155 fprintf(stderr, "Command \"%s\" is unknown, try \"ip fou help\".\n", *argv);
156 exit(-1);
157}