iproute2/ip/tunnel.c
<<
>>
Prefs
   1/*
   2 * Copyright (C)2006 USAGI/WIDE Project
   3 *
   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.
   8 *
   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.
  13 *
  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>.
  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>
  28#include <errno.h>
  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#include <linux/if_arp.h>
  37
  38#include "utils.h"
  39#include "tunnel.h"
  40#include "json_print.h"
  41
  42const char *tnl_strproto(__u8 proto)
  43{
  44        switch (proto) {
  45        case IPPROTO_IPIP:
  46                return "ip";
  47        case IPPROTO_GRE:
  48                return "gre";
  49        case IPPROTO_IPV6:
  50                return "ipv6";
  51        case IPPROTO_ESP:
  52                return "esp";
  53        case IPPROTO_MPLS:
  54                return "mpls";
  55        case 0:
  56                return "any";
  57        default:
  58                return "unknown";
  59        }
  60}
  61
  62int tnl_get_ioctl(const char *basedev, void *p)
  63{
  64        struct ifreq ifr;
  65        int fd;
  66        int err;
  67
  68        strlcpy(ifr.ifr_name, basedev, IFNAMSIZ);
  69        ifr.ifr_ifru.ifru_data = (void *)p;
  70
  71        fd = socket(preferred_family, SOCK_DGRAM, 0);
  72        if (fd < 0) {
  73                fprintf(stderr, "create socket failed: %s\n", strerror(errno));
  74                return -1;
  75        }
  76
  77        err = ioctl(fd, SIOCGETTUNNEL, &ifr);
  78        if (err)
  79                fprintf(stderr, "get tunnel \"%s\" failed: %s\n", basedev,
  80                        strerror(errno));
  81
  82        close(fd);
  83        return err;
  84}
  85
  86int tnl_add_ioctl(int cmd, const char *basedev, const char *name, void *p)
  87{
  88        struct ifreq ifr;
  89        int fd;
  90        int err;
  91
  92        if (cmd == SIOCCHGTUNNEL && name[0])
  93                strlcpy(ifr.ifr_name, name, IFNAMSIZ);
  94        else
  95                strlcpy(ifr.ifr_name, basedev, IFNAMSIZ);
  96        ifr.ifr_ifru.ifru_data = p;
  97
  98        fd = socket(preferred_family, SOCK_DGRAM, 0);
  99        if (fd < 0) {
 100                fprintf(stderr, "create socket failed: %s\n", strerror(errno));
 101                return -1;
 102        }
 103
 104        err = ioctl(fd, cmd, &ifr);
 105        if (err)
 106                fprintf(stderr, "add tunnel \"%s\" failed: %s\n", ifr.ifr_name,
 107                        strerror(errno));
 108        close(fd);
 109        return err;
 110}
 111
 112int tnl_del_ioctl(const char *basedev, const char *name, void *p)
 113{
 114        struct ifreq ifr;
 115        int fd;
 116        int err;
 117
 118        if (name[0])
 119                strlcpy(ifr.ifr_name, name, IFNAMSIZ);
 120        else
 121                strlcpy(ifr.ifr_name, basedev, IFNAMSIZ);
 122
 123        ifr.ifr_ifru.ifru_data = p;
 124
 125        fd = socket(preferred_family, SOCK_DGRAM, 0);
 126        if (fd < 0) {
 127                fprintf(stderr, "create socket failed: %s\n", strerror(errno));
 128                return -1;
 129        }
 130
 131        err = ioctl(fd, SIOCDELTUNNEL, &ifr);
 132        if (err)
 133                fprintf(stderr, "delete tunnel \"%s\" failed: %s\n",
 134                        ifr.ifr_name, strerror(errno));
 135        close(fd);
 136        return err;
 137}
 138
 139static int tnl_gen_ioctl(int cmd, const char *name,
 140                         void *p, int skiperr)
 141{
 142        struct ifreq ifr;
 143        int fd;
 144        int err;
 145
 146        strlcpy(ifr.ifr_name, name, IFNAMSIZ);
 147        ifr.ifr_ifru.ifru_data = p;
 148
 149        fd = socket(preferred_family, SOCK_DGRAM, 0);
 150        if (fd < 0) {
 151                fprintf(stderr, "create socket failed: %s\n", strerror(errno));
 152                return -1;
 153        }
 154
 155        err = ioctl(fd, cmd, &ifr);
 156        if (err && errno != skiperr)
 157                fprintf(stderr, "%s: ioctl %x failed: %s\n", name,
 158                        cmd, strerror(errno));
 159        close(fd);
 160        return err;
 161}
 162
 163int tnl_prl_ioctl(int cmd, const char *name, void *p)
 164{
 165        return tnl_gen_ioctl(cmd, name, p, -1);
 166}
 167
 168int tnl_6rd_ioctl(int cmd, const char *name, void *p)
 169{
 170        return tnl_gen_ioctl(cmd, name, p, -1);
 171}
 172
 173int tnl_ioctl_get_6rd(const char *name, void *p)
 174{
 175        return tnl_gen_ioctl(SIOCGET6RD, name, p, EINVAL);
 176}
 177
 178__be32 tnl_parse_key(const char *name, const char *key)
 179{
 180        unsigned int uval;
 181
 182        if (strchr(key, '.'))
 183                return get_addr32(key);
 184
 185        if (get_unsigned(&uval, key, 0) < 0) {
 186                fprintf(stderr,
 187                        "invalid value for \"%s\": \"%s\"; it should be an unsigned integer\n",
 188                        name, key);
 189                exit(-1);
 190        }
 191        return htonl(uval);
 192}
 193
 194static const char *tnl_encap_str(const char *name, int enabled, int port)
 195{
 196        static const char ne[][sizeof("no")] = {
 197                [0] = "no",
 198                [1] = "",
 199        };
 200        static char buf[32];
 201        char b1[16];
 202        const char *val;
 203
 204        if (!port) {
 205                val = "auto ";
 206        } else if (port < 0) {
 207                val = "";
 208        } else {
 209                snprintf(b1, sizeof(b1), "%u ", port - 1);
 210                val = b1;
 211        }
 212
 213        snprintf(buf, sizeof(buf), "%sencap-%s %s", ne[!!enabled], name, val);
 214        return buf;
 215}
 216
 217void tnl_print_encap(struct rtattr *tb[],
 218                     int encap_type, int encap_flags,
 219                     int encap_sport, int encap_dport)
 220{
 221        __u16 type, flags, sport, dport;
 222
 223        if (!tb[encap_type])
 224                return;
 225
 226        type = rta_getattr_u16(tb[encap_type]);
 227        if (type == TUNNEL_ENCAP_NONE)
 228                return;
 229
 230        flags = rta_getattr_u16(tb[encap_flags]);
 231        sport = rta_getattr_u16(tb[encap_sport]);
 232        dport = rta_getattr_u16(tb[encap_dport]);
 233
 234        open_json_object("encap");
 235        print_string(PRINT_FP, NULL, "encap ", NULL);
 236
 237        switch (type) {
 238        case TUNNEL_ENCAP_FOU:
 239                print_string(PRINT_ANY, "type", "%s ", "fou");
 240                break;
 241        case TUNNEL_ENCAP_GUE:
 242                print_string(PRINT_ANY, "type", "%s ", "gue");
 243                break;
 244        default:
 245                print_null(PRINT_ANY, "type", "%s ", "unknown");
 246                break;
 247        }
 248
 249        if (is_json_context()) {
 250                print_uint(PRINT_JSON, "sport", NULL, ntohs(sport));
 251                print_uint(PRINT_JSON, "dport", NULL, ntohs(dport));
 252                print_bool(PRINT_JSON, "csum", NULL,
 253                           flags & TUNNEL_ENCAP_FLAG_CSUM);
 254                print_bool(PRINT_JSON, "csum6", NULL,
 255                           flags & TUNNEL_ENCAP_FLAG_CSUM6);
 256                print_bool(PRINT_JSON, "remcsum", NULL,
 257                           flags & TUNNEL_ENCAP_FLAG_REMCSUM);
 258                close_json_object();
 259        } else {
 260                int t;
 261
 262                t = sport ? ntohs(sport) + 1 : 0;
 263                print_string(PRINT_FP, NULL, "%s",
 264                             tnl_encap_str("sport", 1, t));
 265
 266                t = ntohs(dport) + 1;
 267                print_string(PRINT_FP, NULL, "%s",
 268                             tnl_encap_str("dport", 1, t));
 269
 270                t = flags & TUNNEL_ENCAP_FLAG_CSUM;
 271                print_string(PRINT_FP, NULL, "%s",
 272                             tnl_encap_str("csum", t, -1));
 273
 274                t = flags & TUNNEL_ENCAP_FLAG_CSUM6;
 275                print_string(PRINT_FP, NULL, "%s",
 276                             tnl_encap_str("csum6", t, -1));
 277
 278                t = flags & TUNNEL_ENCAP_FLAG_REMCSUM;
 279                print_string(PRINT_FP, NULL, "%s",
 280                             tnl_encap_str("remcsum", t, -1));
 281        }
 282}
 283
 284void tnl_print_endpoint(const char *name, const struct rtattr *rta, int family)
 285{
 286        const char *value;
 287        inet_prefix dst;
 288
 289        if (!rta) {
 290                value = "any";
 291        } else if (get_addr_rta(&dst, rta, family)) {
 292                value = "unknown";
 293        } else if (dst.flags & ADDRTYPE_UNSPEC) {
 294                value = "any";
 295        } else {
 296                value = format_host(family, dst.bytelen, dst.data);
 297                if (!value)
 298                        value = "unknown";
 299        }
 300
 301        if (is_json_context()) {
 302                print_string(PRINT_JSON, name, NULL, value);
 303        } else {
 304                SPRINT_BUF(b1);
 305
 306                snprintf(b1, sizeof(b1), "%s %%s ", name);
 307                print_string(PRINT_FP, NULL, b1, value);
 308        }
 309}
 310
 311void tnl_print_gre_flags(__u8 proto,
 312                         __be16 i_flags, __be16 o_flags,
 313                         __be32 i_key, __be32 o_key)
 314{
 315        if ((i_flags & GRE_KEY) && (o_flags & GRE_KEY) &&
 316            o_key == i_key) {
 317                print_uint(PRINT_ANY, "key", " key %u", ntohl(i_key));
 318        } else {
 319                if (i_flags & GRE_KEY)
 320                        print_uint(PRINT_ANY, "ikey", " ikey %u", ntohl(i_key));
 321                if (o_flags & GRE_KEY)
 322                        print_uint(PRINT_ANY, "okey", " okey %u", ntohl(o_key));
 323        }
 324
 325        if (proto != IPPROTO_GRE)
 326                return;
 327
 328        open_json_array(PRINT_JSON, "flags");
 329        if (i_flags & GRE_SEQ) {
 330                if (is_json_context())
 331                        print_string(PRINT_JSON, NULL, "%s", "rx_drop_ooseq");
 332                else
 333                        printf("%s  Drop packets out of sequence.", _SL_);
 334        }
 335        if (i_flags & GRE_CSUM) {
 336                if (is_json_context())
 337                        print_string(PRINT_JSON, NULL, "%s", "rx_csum");
 338                else
 339                        printf("%s  Checksum in received packet is required.", _SL_);
 340        }
 341        if (o_flags & GRE_SEQ) {
 342                if (is_json_context())
 343                        print_string(PRINT_JSON, NULL, "%s", "tx_seq");
 344                else
 345                        printf("%s  Sequence packets on output.", _SL_);
 346        }
 347        if (o_flags & GRE_CSUM) {
 348                if (is_json_context())
 349                        print_string(PRINT_JSON, NULL, "%s", "tx_csum");
 350                else
 351                        printf("%s  Checksum output packets.", _SL_);
 352        }
 353        close_json_array(PRINT_JSON, NULL);
 354}
 355
 356static void tnl_print_stats(const struct rtnl_link_stats64 *s)
 357{
 358        printf("%s", _SL_);
 359        printf("RX: Packets    Bytes        Errors CsumErrs OutOfSeq Mcasts%s", _SL_);
 360        printf("    %-10lld %-12lld %-6lld %-8lld %-8lld %-8lld%s",
 361               s->rx_packets, s->rx_bytes, s->rx_errors, s->rx_frame_errors,
 362               s->rx_fifo_errors, s->multicast, _SL_);
 363        printf("TX: Packets    Bytes        Errors DeadLoop NoRoute  NoBufs%s", _SL_);
 364        printf("    %-10lld %-12lld %-6lld %-8lld %-8lld %-6lld",
 365               s->tx_packets, s->tx_bytes, s->tx_errors, s->collisions,
 366               s->tx_carrier_errors, s->tx_dropped);
 367}
 368
 369static int print_nlmsg_tunnel(struct nlmsghdr *n, void *arg)
 370{
 371        struct tnl_print_nlmsg_info *info = arg;
 372        struct ifinfomsg *ifi = NLMSG_DATA(n);
 373        struct rtattr *tb[IFLA_MAX+1];
 374        const char *name, *n1;
 375
 376        if (n->nlmsg_type != RTM_NEWLINK && n->nlmsg_type != RTM_DELLINK)
 377                return 0;
 378
 379        if (n->nlmsg_len < NLMSG_LENGTH(sizeof(*ifi)))
 380                return -1;
 381
 382        if (preferred_family == AF_INET) {
 383                switch (ifi->ifi_type) {
 384                case ARPHRD_TUNNEL:
 385                case ARPHRD_IPGRE:
 386                case ARPHRD_SIT:
 387                        break;
 388                default:
 389                        return 0;
 390                }
 391        } else {
 392                switch (ifi->ifi_type) {
 393                case ARPHRD_TUNNEL6:
 394                case ARPHRD_IP6GRE:
 395                        break;
 396                default:
 397                        return 0;
 398                }
 399        }
 400
 401        parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), IFLA_PAYLOAD(n));
 402
 403        if (!tb[IFLA_IFNAME])
 404                return 0;
 405
 406        name = rta_getattr_str(tb[IFLA_IFNAME]);
 407
 408        /* Assume p1->name[IFNAMSIZ] is first field of structure */
 409        n1 = info->p1;
 410        if (n1[0] && strcmp(n1, name))
 411                return 0;
 412
 413        info->ifi = ifi;
 414        info->init(info);
 415
 416        /* TODO: parse netlink attributes */
 417        if (tnl_get_ioctl(name, info->p2))
 418                return 0;
 419
 420        if (!info->match(info))
 421                return 0;
 422
 423        info->print(info->p2);
 424        if (show_stats) {
 425                struct rtnl_link_stats64 s;
 426
 427                if (get_rtnl_link_stats_rta(&s, tb) <= 0)
 428                        return -1;
 429
 430                tnl_print_stats(&s);
 431        }
 432        fputc('\n', stdout);
 433
 434        return 0;
 435}
 436
 437int do_tunnels_list(struct tnl_print_nlmsg_info *info)
 438{
 439        new_json_obj(json);
 440        if (rtnl_linkdump_req(&rth, preferred_family) < 0) {
 441                perror("Cannot send dump request\n");
 442                return -1;
 443        }
 444
 445        if (rtnl_dump_filter(&rth, print_nlmsg_tunnel, info) < 0) {
 446                fprintf(stderr, "Dump terminated\n");
 447                return -1;
 448        }
 449        delete_json_obj();
 450
 451        return 0;
 452}
 453