libnl  3.2.24-rc1
nl-pktloc-lookup.c
1 /*
2  * src/nl-pktloc-lookup.c Lookup packet location alias
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation version 2.1
7  * of the License.
8  *
9  * Copyright (c) 2010 Thomas Graf <tgraf@suug.ch>
10  */
11 
12 #include <netlink/cli/utils.h>
13 #include <netlink/route/pktloc.h>
14 
15 static void print_usage(void)
16 {
17 printf(
18 "Usage: nl-pktloc-lookup [OPTIONS] <name>\n"
19 "\n"
20 "OPTIONS\n"
21 " -h, --help Show this help text.\n"
22 " -v, --version Show versioning information.\n"
23 " -l, --list List all packet location definitions.\n"
24 " --u32=VALUE Print in iproute2's u32 selector style\n"
25 "\n"
26 "\n"
27 "EXAMPLE\n"
28 " $ nl-pktloc-lookup ip.dst\n"
29 " $ nl-pktloc-lookup --list\n"
30 "\n"
31 );
32  exit(0);
33 }
34 
35 static const char *align_txt[] = {
36  [TCF_EM_ALIGN_U8] = "u8",
37  [TCF_EM_ALIGN_U16] = "u16",
38  [TCF_EM_ALIGN_U32] = "u32"
39 };
40 
41 static uint32_t align_mask[] = {
42  [TCF_EM_ALIGN_U8] = 0xff,
43  [TCF_EM_ALIGN_U16] = 0xffff,
44  [TCF_EM_ALIGN_U32] = 0xffffffff,
45 };
46 
47 static const char *layer_txt[] = {
48  [TCF_LAYER_LINK] = "eth",
49  [TCF_LAYER_NETWORK] = "ip",
50  [TCF_LAYER_TRANSPORT] = "tcp"
51 };
52 
53 static void dump_u32_style(struct rtnl_pktloc *loc, uint32_t value)
54 {
55  if (loc->align > 4)
56  nl_cli_fatal(EINVAL, "u32 only supports alignments u8|u16|u32.");
57 
58  if (loc->layer == TCF_LAYER_LINK)
59  nl_cli_fatal(EINVAL, "u32 does not support link "
60  "layer locations.");
61 
62  if (loc->shift > 0)
63  nl_cli_fatal(EINVAL, "u32 does not support shifting.");
64 
65  printf("%s %x %x at %s%u\n",
66  align_txt[loc->align],
67  value, loc->mask ? loc->mask : align_mask[loc->align],
68  loc->layer == TCF_LAYER_TRANSPORT ? "nexthdr+" : "",
69  loc->offset);
70 }
71 
72 static char *get_align_txt(struct rtnl_pktloc *loc)
73 {
74  static char buf[16];
75 
76  if (loc->align <= 4)
77  strcpy(buf, align_txt[loc->align]);
78  else
79  snprintf(buf, sizeof(buf), "%u", loc->align);
80 
81  return buf;
82 }
83 
84 static void dump_loc(struct rtnl_pktloc *loc)
85 {
86  printf("%s = %s at %s+%u & %#x >> %u\n",
87  loc->name, get_align_txt(loc), layer_txt[loc->layer],
88  loc->offset, loc->mask, loc->shift);
89 }
90 
91 static void list_cb(struct rtnl_pktloc *loc, void *arg)
92 {
93  printf("%-26s %-5s %3s+%-4u %#-10x %-8u %u\n",
94  loc->name, get_align_txt(loc), layer_txt[loc->layer],
95  loc->offset, loc->mask, loc->shift, loc->refcnt);
96 }
97 
98 static void do_list(void)
99 {
100  printf(
101 "name align offset mask shift refcnt\n");
102  printf("---------------------------------------------------------\n");
103 
104  rtnl_pktloc_foreach(&list_cb, NULL);
105 }
106 
107 int main(int argc, char *argv[])
108 {
109  struct rtnl_pktloc *loc;
110  int err, ustyle = 0;
111  uint32_t uvalue = 0;
112 
113  for (;;) {
114  int c, optidx = 0;
115  enum {
116  ARG_U32 = 257,
117  };
118  static struct option long_opts[] = {
119  { "help", 0, 0, 'h' },
120  { "version", 0, 0, 'v' },
121  { "list", 0, 0, 'l' },
122  { "u32", 1, 0, ARG_U32 },
123  { 0, 0, 0, 0 }
124  };
125 
126  c = getopt_long(argc, argv, "hvl", long_opts, &optidx);
127  if (c == -1)
128  break;
129 
130  switch (c) {
131  case 'h': print_usage(); break;
132  case 'v': nl_cli_print_version(); break;
133  case 'l': do_list(); exit(0);
134  case ARG_U32:
135  ustyle = 1;
136  uvalue = nl_cli_parse_u32(optarg);
137  break;
138  }
139  }
140 
141  if (optind >= argc)
142  print_usage();
143 
144  if ((err = rtnl_pktloc_lookup(argv[optind++], &loc)) < 0)
145  nl_cli_fatal(err, "Unable to lookup packet location: %s",
146  nl_geterror(err));
147 
148  if (ustyle)
149  dump_u32_style(loc, uvalue);
150  else
151  dump_loc(loc);
152 
153  return 0;
154 }