iproute2/ip/iplink_bareudp.c
<<
>>
Prefs
   1/* SPDX-License-Identifier: GPL-2.0 */
   2
   3#include <stdio.h>
   4#include <linux/if_ether.h>
   5#include <linux/if_link.h>
   6#include <linux/netlink.h>
   7#include <linux/rtnetlink.h>
   8
   9#include "libnetlink.h"
  10#include "rt_names.h"
  11#include "utils.h"
  12#include "ip_common.h"
  13#include "json_print.h"
  14
  15#define BAREUDP_ATTRSET(attrs, type) (((attrs) & (1L << (type))) != 0)
  16
  17static void print_explain(FILE *f)
  18{
  19        fprintf(f,
  20                "Usage: ... bareudp dstport PORT\n"
  21                "               ethertype PROTO\n"
  22                "               [ srcportmin PORT ]\n"
  23                "               [ [no]multiproto ]\n"
  24                "\n"
  25                "Where: PORT  := UDP_PORT\n"
  26                "       PROTO := ETHERTYPE\n"
  27                "\n"
  28                "Note: ETHERTYPE can be given as number or as protocol name (\"ipv4\", \"ipv6\",\n"
  29                "      \"mpls_uc\", etc.).\n"
  30        );
  31}
  32
  33static void explain(void)
  34{
  35        print_explain(stderr);
  36}
  37
  38static void check_duparg(__u64 *attrs, int type, const char *key,
  39                         const char *argv)
  40{
  41        if (!BAREUDP_ATTRSET(*attrs, type)) {
  42                *attrs |= (1L << type);
  43                return;
  44        }
  45        duparg2(key, argv);
  46}
  47
  48static int bareudp_parse_opt(struct link_util *lu, int argc, char **argv,
  49                             struct nlmsghdr *n)
  50{
  51        bool multiproto = false;
  52        __u16 srcportmin = 0;
  53        __be16 ethertype = 0;
  54        __be16 dstport = 0;
  55        __u64 attrs = 0;
  56
  57        while (argc > 0) {
  58                if (matches(*argv, "dstport") == 0) {
  59                        NEXT_ARG();
  60                        check_duparg(&attrs, IFLA_BAREUDP_PORT, "dstport",
  61                                     *argv);
  62                        if (get_be16(&dstport, *argv, 0))
  63                                invarg("dstport", *argv);
  64                } else if (matches(*argv, "ethertype") == 0)  {
  65                        NEXT_ARG();
  66                        check_duparg(&attrs, IFLA_BAREUDP_ETHERTYPE,
  67                                     "ethertype", *argv);
  68                        if (ll_proto_a2n(&ethertype, *argv))
  69                                invarg("ethertype", *argv);
  70                } else if (matches(*argv, "srcportmin") == 0) {
  71                        NEXT_ARG();
  72                        check_duparg(&attrs, IFLA_BAREUDP_SRCPORT_MIN,
  73                                     "srcportmin", *argv);
  74                        if (get_u16(&srcportmin, *argv, 0))
  75                                invarg("srcportmin", *argv);
  76                } else if (matches(*argv, "multiproto") == 0) {
  77                        check_duparg(&attrs, IFLA_BAREUDP_MULTIPROTO_MODE,
  78                                     *argv, *argv);
  79                        multiproto = true;
  80                } else if (matches(*argv, "nomultiproto") == 0) {
  81                        check_duparg(&attrs, IFLA_BAREUDP_MULTIPROTO_MODE,
  82                                     *argv, *argv);
  83                        multiproto = false;
  84                } else if (matches(*argv, "help") == 0) {
  85                        explain();
  86                        return -1;
  87                } else {
  88                        fprintf(stderr, "bareudp: unknown command \"%s\"?\n",
  89                                *argv);
  90                        explain();
  91                        return -1;
  92                }
  93                argc--, argv++;
  94        }
  95
  96        if (!BAREUDP_ATTRSET(attrs, IFLA_BAREUDP_PORT))
  97                missarg("dstport");
  98        if (!BAREUDP_ATTRSET(attrs, IFLA_BAREUDP_ETHERTYPE))
  99                missarg("ethertype");
 100
 101        addattr16(n, 1024, IFLA_BAREUDP_PORT, dstport);
 102        addattr16(n, 1024, IFLA_BAREUDP_ETHERTYPE, ethertype);
 103        if (BAREUDP_ATTRSET(attrs, IFLA_BAREUDP_SRCPORT_MIN))
 104                addattr16(n, 1024, IFLA_BAREUDP_SRCPORT_MIN, srcportmin);
 105        if (multiproto)
 106                addattr(n, 1024, IFLA_BAREUDP_MULTIPROTO_MODE);
 107
 108        return 0;
 109}
 110
 111static void bareudp_print_opt(struct link_util *lu, FILE *f,
 112                              struct rtattr *tb[])
 113{
 114        if (!tb)
 115                return;
 116
 117        if (tb[IFLA_BAREUDP_PORT])
 118                print_uint(PRINT_ANY, "dstport", "dstport %u ",
 119                           rta_getattr_be16(tb[IFLA_BAREUDP_PORT]));
 120
 121        if (tb[IFLA_BAREUDP_ETHERTYPE]) {
 122                struct rtattr *attr = tb[IFLA_BAREUDP_ETHERTYPE];
 123                SPRINT_BUF(ethertype);
 124
 125                print_string(PRINT_ANY, "ethertype", "ethertype %s ",
 126                             ll_proto_n2a(rta_getattr_u16(attr),
 127                                          ethertype, sizeof(ethertype)));
 128        }
 129
 130        if (tb[IFLA_BAREUDP_SRCPORT_MIN])
 131                print_uint(PRINT_ANY, "srcportmin", "srcportmin %u ",
 132                           rta_getattr_u16(tb[IFLA_BAREUDP_SRCPORT_MIN]));
 133
 134        if (tb[IFLA_BAREUDP_MULTIPROTO_MODE])
 135                print_bool(PRINT_ANY, "multiproto", "multiproto ", true);
 136        else
 137                print_bool(PRINT_ANY, "multiproto", "nomultiproto ", false);
 138}
 139
 140static void bareudp_print_help(struct link_util *lu, int argc, char **argv,
 141                               FILE *f)
 142{
 143        print_explain(f);
 144}
 145
 146struct link_util bareudp_link_util = {
 147        .id             = "bareudp",
 148        .maxattr        = IFLA_BAREUDP_MAX,
 149        .parse_opt      = bareudp_parse_opt,
 150        .print_opt      = bareudp_print_opt,
 151        .print_help     = bareudp_print_help,
 152};
 153