]>
git.proxmox.com Git - mirror_iproute2.git/blob - ip/tunnel.c
2 * Copyright (C)2006 USAGI/WIDE Project
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.
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.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see <http://www.gnu.org/licenses>.
18 * split from ip_tunnel.c
22 * Masahide NAKAMURA @USAGI
29 #include <sys/types.h>
30 #include <sys/socket.h>
31 #include <sys/ioctl.h>
32 #include <netinet/in.h>
35 #include <linux/if_tunnel.h>
40 const char *tnl_strproto(__u8 proto
)
64 strcpy(buf
, "unknown");
71 int tnl_get_ioctl(const char *basedev
, void *p
)
77 strncpy(ifr
.ifr_name
, basedev
, IFNAMSIZ
);
78 ifr
.ifr_ifru
.ifru_data
= (void *)p
;
80 fd
= socket(preferred_family
, SOCK_DGRAM
, 0);
82 fprintf(stderr
, "create socket failed: %s\n", strerror(errno
));
86 err
= ioctl(fd
, SIOCGETTUNNEL
, &ifr
);
88 fprintf(stderr
, "get tunnel \"%s\" failed: %s\n", basedev
,
95 int tnl_add_ioctl(int cmd
, const char *basedev
, const char *name
, void *p
)
101 if (cmd
== SIOCCHGTUNNEL
&& name
[0])
102 strncpy(ifr
.ifr_name
, name
, IFNAMSIZ
);
104 strncpy(ifr
.ifr_name
, basedev
, IFNAMSIZ
);
105 ifr
.ifr_ifru
.ifru_data
= p
;
107 fd
= socket(preferred_family
, SOCK_DGRAM
, 0);
109 fprintf(stderr
, "create socket failed: %s\n", strerror(errno
));
113 err
= ioctl(fd
, cmd
, &ifr
);
115 fprintf(stderr
, "add tunnel \"%s\" failed: %s\n", ifr
.ifr_name
,
121 int tnl_del_ioctl(const char *basedev
, const char *name
, void *p
)
128 strncpy(ifr
.ifr_name
, name
, IFNAMSIZ
);
130 strncpy(ifr
.ifr_name
, basedev
, IFNAMSIZ
);
132 ifr
.ifr_ifru
.ifru_data
= p
;
134 fd
= socket(preferred_family
, SOCK_DGRAM
, 0);
136 fprintf(stderr
, "create socket failed: %s\n", strerror(errno
));
140 err
= ioctl(fd
, SIOCDELTUNNEL
, &ifr
);
142 fprintf(stderr
, "delete tunnel \"%s\" failed: %s\n",
143 ifr
.ifr_name
, strerror(errno
));
148 static int tnl_gen_ioctl(int cmd
, const char *name
,
149 void *p
, int skiperr
)
155 strncpy(ifr
.ifr_name
, name
, IFNAMSIZ
);
156 ifr
.ifr_ifru
.ifru_data
= p
;
158 fd
= socket(preferred_family
, SOCK_DGRAM
, 0);
160 fprintf(stderr
, "create socket failed: %s\n", strerror(errno
));
164 err
= ioctl(fd
, cmd
, &ifr
);
165 if (err
&& errno
!= skiperr
)
166 fprintf(stderr
, "%s: ioctl %x failed: %s\n", name
,
167 cmd
, strerror(errno
));
172 int tnl_prl_ioctl(int cmd
, const char *name
, void *p
)
174 return tnl_gen_ioctl(cmd
, name
, p
, -1);
177 int tnl_6rd_ioctl(int cmd
, const char *name
, void *p
)
179 return tnl_gen_ioctl(cmd
, name
, p
, -1);
182 int tnl_ioctl_get_6rd(const char *name
, void *p
)
184 return tnl_gen_ioctl(SIOCGET6RD
, name
, p
, EINVAL
);
187 __be32
tnl_parse_key(const char *name
, const char *key
)
191 if (strchr(key
, '.'))
192 return get_addr32(key
);
194 if (get_unsigned(&uval
, key
, 0) < 0) {
196 "invalid value for \"%s\": \"%s\"; it should be an unsigned integer\n",
203 /* tnl_print_stats - print tunnel statistics
205 * @buf - tunnel interface's line in /proc/net/dev,
206 * starting past the interface name and following colon
208 void tnl_print_stats(const char *buf
)
210 unsigned long rx_bytes
, rx_packets
, rx_errs
, rx_drops
,
212 tx_bytes
, tx_packets
, tx_errs
, tx_drops
,
213 tx_fifo
, tx_colls
, tx_carrier
, rx_multi
;
215 if (sscanf(buf
, "%lu%lu%lu%lu%lu%lu%lu%*d%lu%lu%lu%lu%lu%lu%lu",
216 &rx_bytes
, &rx_packets
, &rx_errs
, &rx_drops
,
217 &rx_fifo
, &rx_frame
, &rx_multi
,
218 &tx_bytes
, &tx_packets
, &tx_errs
, &tx_drops
,
219 &tx_fifo
, &tx_colls
, &tx_carrier
) != 14)
223 printf("RX: Packets Bytes Errors CsumErrs OutOfSeq Mcasts%s", _SL_
);
224 printf(" %-10ld %-12ld %-6ld %-8ld %-8ld %-8ld%s",
225 rx_packets
, rx_bytes
, rx_errs
, rx_frame
, rx_fifo
, rx_multi
, _SL_
);
226 printf("TX: Packets Bytes Errors DeadLoop NoRoute NoBufs%s", _SL_
);
227 printf(" %-10ld %-12ld %-6ld %-8ld %-8ld %-6ld",
228 tx_packets
, tx_bytes
, tx_errs
, tx_colls
, tx_carrier
, tx_drops
);