]> git.proxmox.com Git - mirror_iproute2.git/blame - ip/tunnel.c
iptunnel/ip6tunnel: Code cleanups
[mirror_iproute2.git] / ip / tunnel.c
CommitLineData
d9bd1bd9
MN
1/*
2 * Copyright (C)2006 USAGI/WIDE Project
ae665a52 3 *
d9bd1bd9
MN
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
ae665a52 8 *
d9bd1bd9
MN
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
ae665a52 13 *
d9bd1bd9 14 * You should have received a copy of the GNU General Public License
4d98ab00 15 * along with this program; if not, see <http://www.gnu.org/licenses>.
d9bd1bd9
MN
16 */
17/*
18 * split from ip_tunnel.c
19 */
20/*
21 * Author:
22 * Masahide NAKAMURA @USAGI
23 */
24
25#include <stdio.h>
26#include <string.h>
27#include <unistd.h>
3979ef91 28#include <errno.h>
d9bd1bd9
MN
29#include <sys/types.h>
30#include <sys/socket.h>
31#include <sys/ioctl.h>
32#include <netinet/in.h>
33#include <linux/if.h>
34#include <linux/ip.h>
35#include <linux/if_tunnel.h>
36
37#include "utils.h"
38#include "tunnel.h"
bad76e6b 39#include "json_print.h"
d9bd1bd9
MN
40
41const char *tnl_strproto(__u8 proto)
42{
d9bd1bd9
MN
43 switch (proto) {
44 case IPPROTO_IPIP:
c9391f12 45 return "ip";
d9bd1bd9 46 case IPPROTO_GRE:
c9391f12 47 return "gre";
d9bd1bd9 48 case IPPROTO_IPV6:
c9391f12 49 return "ipv6";
14e97673 50 case IPPROTO_ESP:
c9391f12 51 return "esp";
f005b700 52 case IPPROTO_MPLS:
c9391f12 53 return "mpls";
0b959b0f 54 case 0:
c9391f12 55 return "any";
d9bd1bd9 56 default:
c9391f12 57 return "unknown";
d9bd1bd9 58 }
d9bd1bd9
MN
59}
60
d9bd1bd9
MN
61int tnl_get_ioctl(const char *basedev, void *p)
62{
63 struct ifreq ifr;
64 int fd;
65 int err;
66
67 strncpy(ifr.ifr_name, basedev, IFNAMSIZ);
56f5daac 68 ifr.ifr_ifru.ifru_data = (void *)p;
cb89c7c7 69
d9bd1bd9 70 fd = socket(preferred_family, SOCK_DGRAM, 0);
cb89c7c7
ZS
71 if (fd < 0) {
72 fprintf(stderr, "create socket failed: %s\n", strerror(errno));
73 return -1;
74 }
75
d9bd1bd9
MN
76 err = ioctl(fd, SIOCGETTUNNEL, &ifr);
77 if (err)
14645ec2 78 fprintf(stderr, "get tunnel \"%s\" failed: %s\n", basedev,
ea71beac
SH
79 strerror(errno));
80
d9bd1bd9
MN
81 close(fd);
82 return err;
83}
84
85int tnl_add_ioctl(int cmd, const char *basedev, const char *name, void *p)
86{
87 struct ifreq ifr;
88 int fd;
89 int err;
90
91 if (cmd == SIOCCHGTUNNEL && name[0])
92 strncpy(ifr.ifr_name, name, IFNAMSIZ);
93 else
94 strncpy(ifr.ifr_name, basedev, IFNAMSIZ);
95 ifr.ifr_ifru.ifru_data = p;
cb89c7c7 96
d9bd1bd9 97 fd = socket(preferred_family, SOCK_DGRAM, 0);
cb89c7c7
ZS
98 if (fd < 0) {
99 fprintf(stderr, "create socket failed: %s\n", strerror(errno));
100 return -1;
101 }
102
d9bd1bd9
MN
103 err = ioctl(fd, cmd, &ifr);
104 if (err)
14645ec2 105 fprintf(stderr, "add tunnel \"%s\" failed: %s\n", ifr.ifr_name,
ea71beac 106 strerror(errno));
d9bd1bd9
MN
107 close(fd);
108 return err;
109}
110
111int tnl_del_ioctl(const char *basedev, const char *name, void *p)
112{
113 struct ifreq ifr;
114 int fd;
115 int err;
116
117 if (name[0])
118 strncpy(ifr.ifr_name, name, IFNAMSIZ);
119 else
120 strncpy(ifr.ifr_name, basedev, IFNAMSIZ);
ea71beac 121
d9bd1bd9 122 ifr.ifr_ifru.ifru_data = p;
cb89c7c7 123
d9bd1bd9 124 fd = socket(preferred_family, SOCK_DGRAM, 0);
cb89c7c7
ZS
125 if (fd < 0) {
126 fprintf(stderr, "create socket failed: %s\n", strerror(errno));
127 return -1;
128 }
129
d9bd1bd9
MN
130 err = ioctl(fd, SIOCDELTUNNEL, &ifr);
131 if (err)
14645ec2 132 fprintf(stderr, "delete tunnel \"%s\" failed: %s\n",
ea71beac 133 ifr.ifr_name, strerror(errno));
d9bd1bd9
MN
134 close(fd);
135 return err;
136}
a07e9912 137
0612519e 138static int tnl_gen_ioctl(int cmd, const char *name,
ea71beac 139 void *p, int skiperr)
a07e9912
SH
140{
141 struct ifreq ifr;
142 int fd;
143 int err;
144
145 strncpy(ifr.ifr_name, name, IFNAMSIZ);
146 ifr.ifr_ifru.ifru_data = p;
cb89c7c7 147
a07e9912 148 fd = socket(preferred_family, SOCK_DGRAM, 0);
cb89c7c7
ZS
149 if (fd < 0) {
150 fprintf(stderr, "create socket failed: %s\n", strerror(errno));
151 return -1;
152 }
153
a07e9912 154 err = ioctl(fd, cmd, &ifr);
3979ef91 155 if (err && errno != skiperr)
ea71beac
SH
156 fprintf(stderr, "%s: ioctl %x failed: %s\n", name,
157 cmd, strerror(errno));
a07e9912
SH
158 close(fd);
159 return err;
160}
b88215c4
AC
161
162int tnl_prl_ioctl(int cmd, const char *name, void *p)
163{
3979ef91 164 return tnl_gen_ioctl(cmd, name, p, -1);
b88215c4
AC
165}
166
167int tnl_6rd_ioctl(int cmd, const char *name, void *p)
168{
3979ef91 169 return tnl_gen_ioctl(cmd, name, p, -1);
b88215c4
AC
170}
171
172int tnl_ioctl_get_6rd(const char *name, void *p)
173{
3979ef91 174 return tnl_gen_ioctl(SIOCGET6RD, name, p, EINVAL);
b88215c4 175}
a7ed1520
PS
176
177__be32 tnl_parse_key(const char *name, const char *key)
178{
56f5daac 179 unsigned int uval;
a7ed1520
PS
180
181 if (strchr(key, '.'))
182 return get_addr32(key);
183
184 if (get_unsigned(&uval, key, 0) < 0) {
1f44b937
SP
185 fprintf(stderr,
186 "invalid value for \"%s\": \"%s\"; it should be an unsigned integer\n",
187 name, key);
a7ed1520
PS
188 exit(-1);
189 }
190 return htonl(uval);
191}
7d6aadcd 192
bad76e6b
SP
193static const char *tnl_encap_str(const char *name, int enabled, int port)
194{
195 static const char ne[][sizeof("no")] = {
196 [0] = "no",
197 [1] = "",
198 };
199 static char buf[32];
200 char b1[16];
201 const char *val;
202
203 if (!port) {
b53835de 204 val = "auto ";
bad76e6b
SP
205 } else if (port < 0) {
206 val = "";
207 } else {
b53835de 208 snprintf(b1, sizeof(b1), "%u ", port - 1);
bad76e6b
SP
209 val = b1;
210 }
211
212 snprintf(buf, sizeof(buf), "%sencap-%s %s", ne[!!enabled], name, val);
213 return buf;
214}
215
216void tnl_print_encap(struct rtattr *tb[],
217 int encap_type, int encap_flags,
218 int encap_sport, int encap_dport)
219{
220 __u16 type, flags, sport, dport;
221
222 if (!tb[encap_type])
223 return;
224
225 type = rta_getattr_u16(tb[encap_type]);
226 if (type == TUNNEL_ENCAP_NONE)
227 return;
228
229 flags = rta_getattr_u16(tb[encap_flags]);
230 sport = rta_getattr_u16(tb[encap_sport]);
231 dport = rta_getattr_u16(tb[encap_dport]);
232
233 open_json_object("encap");
234 print_string(PRINT_FP, NULL, "encap ", NULL);
235
236 switch (type) {
237 case TUNNEL_ENCAP_FOU:
238 print_string(PRINT_ANY, "type", "%s ", "fou");
239 break;
240 case TUNNEL_ENCAP_GUE:
241 print_string(PRINT_ANY, "type", "%s ", "gue");
242 break;
243 default:
244 print_null(PRINT_ANY, "type", "%s ", "unknown");
245 break;
246 }
247
248 if (is_json_context()) {
249 print_uint(PRINT_JSON, "sport", NULL, ntohs(sport));
250 print_uint(PRINT_JSON, "dport", NULL, ntohs(dport));
251 print_bool(PRINT_JSON, "csum", NULL,
252 flags & TUNNEL_ENCAP_FLAG_CSUM);
253 print_bool(PRINT_JSON, "csum6", NULL,
254 flags & TUNNEL_ENCAP_FLAG_CSUM6);
255 print_bool(PRINT_JSON, "remcsum", NULL,
256 flags & TUNNEL_ENCAP_FLAG_REMCSUM);
257 close_json_object();
258 } else {
259 int t;
260
261 t = sport ? ntohs(sport) + 1 : 0;
262 print_string(PRINT_FP, NULL, "%s",
263 tnl_encap_str("sport", 1, t));
264
265 t = ntohs(dport) + 1;
266 print_string(PRINT_FP, NULL, "%s",
267 tnl_encap_str("dport", 1, t));
268
269 t = flags & TUNNEL_ENCAP_FLAG_CSUM;
270 print_string(PRINT_FP, NULL, "%s",
271 tnl_encap_str("csum", t, -1));
272
273 t = flags & TUNNEL_ENCAP_FLAG_CSUM6;
274 print_string(PRINT_FP, NULL, "%s",
275 tnl_encap_str("csum6", t, -1));
276
277 t = flags & TUNNEL_ENCAP_FLAG_REMCSUM;
278 print_string(PRINT_FP, NULL, "%s",
279 tnl_encap_str("remcsum", t, -1));
280 }
281}
282
b761fc41
SP
283void tnl_print_endpoint(const char *name, const struct rtattr *rta, int family)
284{
285 const char *value;
286 inet_prefix dst;
287
288 if (!rta) {
289 value = "any";
290 } else if (get_addr_rta(&dst, rta, family)) {
291 value = "unknown";
292 } else if (dst.flags & ADDRTYPE_UNSPEC) {
293 value = "any";
294 } else {
295 value = format_host(family, dst.bytelen, dst.data);
296 if (!value)
297 value = "unknown";
298 }
299
300 if (is_json_context()) {
301 print_string(PRINT_JSON, name, NULL, value);
302 } else {
303 SPRINT_BUF(b1);
304
305 snprintf(b1, sizeof(b1), "%s %%s ", name);
306 print_string(PRINT_FP, NULL, b1, value);
307 }
308}
309
affb3617 310int tnl_get_stats(const char *buf, struct rtnl_link_stats64 *s)
7d6aadcd 311{
affb3617
SP
312 /* rx */
313 __u64 *rx_bytes = &s->rx_bytes;
314 __u64 *rx_packets = &s->rx_packets;
315 __u64 *rx_errs = &s->rx_errors;
316 __u64 *rx_drops = &s->rx_dropped;
317 __u64 *rx_fifo = &s->rx_fifo_errors;
318 __u64 *rx_frame = &s->rx_frame_errors;
319 __u64 *rx_multi = &s->multicast;
320 /* tx */
321 __u64 *tx_bytes = &s->tx_bytes;
322 __u64 *tx_packets = &s->tx_packets;
323 __u64 *tx_errs = &s->tx_errors;
324 __u64 *tx_drops = &s->tx_dropped;
325 __u64 *tx_fifo = &s->tx_fifo_errors;
326 __u64 *tx_carrier = &s->tx_carrier_errors;
327 __u64 *tx_colls = &s->collisions;
328
329 if (sscanf(buf,
330 "%llu%llu%llu%llu%llu%llu%llu%*d%llu%llu%llu%llu%llu%llu%llu",
331 rx_bytes, rx_packets, rx_errs, rx_drops,
332 rx_fifo, rx_frame, rx_multi,
333 tx_bytes, tx_packets, tx_errs, tx_drops,
334 tx_fifo, tx_colls, tx_carrier) != 14)
335 return -1;
7d6aadcd 336
affb3617
SP
337 return 0;
338}
339
340void tnl_print_stats(const struct rtnl_link_stats64 *s)
341{
7d6aadcd
PS
342 printf("%s", _SL_);
343 printf("RX: Packets Bytes Errors CsumErrs OutOfSeq Mcasts%s", _SL_);
affb3617
SP
344 printf(" %-10lld %-12lld %-6lld %-8lld %-8lld %-8lld%s",
345 s->rx_packets, s->rx_bytes, s->rx_errors, s->rx_frame_errors,
346 s->rx_fifo_errors, s->multicast, _SL_);
7d6aadcd 347 printf("TX: Packets Bytes Errors DeadLoop NoRoute NoBufs%s", _SL_);
affb3617
SP
348 printf(" %-10lld %-12lld %-6lld %-8lld %-8lld %-6lld",
349 s->tx_packets, s->tx_bytes, s->tx_errors, s->collisions,
350 s->tx_carrier_errors, s->tx_dropped);
7d6aadcd 351}