2 * Copyright (c) 2015 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
16 Copyright (c) 2001, 2002, 2003 Eliot Dresselhaus
18 Permission is hereby granted, free of charge, to any person obtaining
19 a copy of this software and associated documentation files (the
20 "Software"), to deal in the Software without restriction, including
21 without limitation the rights to use, copy, modify, merge, publish,
22 distribute, sublicense, and/or sell copies of the Software, and to
23 permit persons to whom the Software is furnished to do so, subject to
24 the following conditions:
26 The above copyright notice and this permission notice shall be
27 included in all copies or substantial portions of the Software.
29 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
30 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
31 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
32 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
33 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
34 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
35 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
41 # include <linux/unistd.h>
42 # include <linux/signal.h>
45 #else /* ! __KERNEL__ */
51 #define _GNU_SOURCE /* to get REG_* in ucontext.h */
60 #include <sys/socket.h>
64 #include <vppinfra/time.h>
67 #include <linux/types.h>
68 #include <linux/netlink.h>
72 #endif /* ! __KERNEL__ */
76 # include <linux/socket.h>
77 # include <linux/in.h>
78 # include <linux/ip.h>
79 # include <linux/tcp.h>
80 # include <linux/udp.h>
81 # include <linux/icmp.h>
82 # include <linux/if_ether.h>
83 # include <linux/if_arp.h>
85 # include <net/if.h> /* struct ifnet may live here */
86 # include <netinet/in.h>
87 # include <netinet/ip.h>
88 # include <netinet/tcp.h>
89 # include <netinet/udp.h>
90 # include <netinet/ip_icmp.h>
91 # include <netinet/if_ether.h>
92 #endif /* __KERNEL__ */
94 #include <vppinfra/bitops.h> /* foreach_set_bit */
95 #include <vppinfra/format.h>
96 #include <vppinfra/error.h>
98 /* Format unix network address family (e.g. AF_INET). */
99 u8 * format_address_family (u8 * s, va_list * va)
101 uword family = va_arg (*va, uword);
102 u8 * t = (u8 *) "UNKNOWN";
105 #define _(x) case PF_##x: t = (u8 *) #x; break
107 _ (UNIX); /* Unix domain sockets */
108 _ (INET); /* Internet IP Protocol */
110 _ (AX25); /* Amateur Radio AX.25 */
113 _ (IPX); /* Novell IPX */
116 _ (APPLETALK); /* AppleTalk DDP */
119 _ (NETROM); /* Amateur Radio NET/ROM */
122 _ (BRIDGE); /* Multiprotocol bridge */
125 _ (ATMPVC); /* ATM PVCs */
128 _ (X25); /* Reserved for X.25 project */
131 _ (INET6); /* IP version 6 */
134 _ (ROSE); /* Amateur Radio X.25 PLP */
137 _ (DECnet); /* Reserved for DECnet project */
140 _ (NETBEUI); /* Reserved for 802.2LLC project*/
143 _ (SECURITY); /* Security callback pseudo AF */
146 _ (KEY); /* PF_KEY key management API */
152 _ (PACKET); /* Packet family */
158 _ (ECONET); /* Acorn Econet */
161 _ (ATMSVC); /* ATM SVCs */
164 _ (SNA); /* Linux SNA Project */
167 _ (IRDA); /* IRDA sockets */
171 vec_add (s, t, strlen ((char *) t));
175 u8 * format_network_protocol (u8 * s, va_list * args)
177 uword family = va_arg (*args, uword);
178 uword protocol = va_arg (*args, uword);
181 struct protoent * p = getprotobynumber (protocol);
183 ASSERT (family == AF_INET);
185 return format (s, "%s", p->p_name);
187 return format (s, "%d", protocol);
189 return format (s, "%d/%d", family, protocol);
193 u8 * format_network_port (u8 * s, va_list * args)
195 uword proto = va_arg (*args, uword);
196 uword port = va_arg (*args, uword);
199 struct servent * p = getservbyport (port, proto == IPPROTO_UDP ? "udp" : "tcp");
202 return format (s, "%s", p->s_name);
204 return format (s, "%d", port);
206 return format (s, "%s/%d", proto == IPPROTO_UDP ? "udp" : "tcp", port);
210 /* Format generic network address: takes two arguments family and address.
211 Assumes network byte order. */
212 u8 * format_network_address (u8 * s, va_list * args)
214 uword family = va_arg (*args, uword);
215 u8 * addr = va_arg (*args, u8 *);
220 s = format (s, "%d.%d.%d.%d", addr[0], addr[1], addr[2], addr[3]);
224 /* We use AF_UNSPEC for ethernet addresses. */
225 s = format (s, "%02x:%02x:%02x:%02x:%02x:%02x",
226 addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]);
230 clib_error ("unsupported address family %d", family);
236 u8 * format_sockaddr (u8 * s, va_list * args)
238 void * v = va_arg (*args, void *);
239 struct sockaddr * sa = v;
240 static u32 local_counter;
242 switch (sa->sa_family)
246 struct sockaddr_in * i = v;
247 s = format (s, "%U:%U",
248 format_network_address, AF_INET, &i->sin_addr.s_addr,
249 format_network_port, IPPROTO_TCP, ntohs (i->sin_port));
256 * There isn't anything useful to print.
257 * The unix cli world uses the output to make a node name,
258 * so we need to return a unique name.
260 s = format (s, "local:%u", local_counter++);
268 struct sockaddr_nl * n = v;
269 s = format (s, "KERNEL-NETLINK");
271 s = format (s, " (groups 0x%x)", n->nl_groups);
278 s = format (s, "sockaddr family %d", sa->sa_family);
286 u8 * format_tcp4_packet (u8 * s, va_list * args)
288 u8 * p = va_arg (*args, u8 *);
289 struct iphdr * ip = (void *) p;
290 struct tcphdr * tcp = (void *) (ip + 1);
292 s = format (s, "tcp %U:%U -> %U:%U",
293 format_network_address, AF_INET, &ip->saddr,
294 format_network_port, IPPROTO_TCP, ntohs (tcp->source),
295 format_network_address, AF_INET, &ip->daddr,
296 format_network_port, IPPROTO_TCP, ntohs (tcp->dest));
298 s = format (s, ", seq 0x%08x -> 0x%08x", tcp->seq, tcp->ack_seq);
299 #define _(f) if (tcp->f) s = format (s, ", " #f);
300 _ (syn); _ (ack); _ (fin); _ (rst); _ (psh); _ (urg);
304 s = format (s, ", window 0x%04x", tcp->window);
306 s = format (s, ", urg 0x%04x", tcp->urg_ptr);
311 u8 * format_udp4_packet (u8 * s, va_list * args)
313 u8 * p = va_arg (*args, u8 *);
314 struct iphdr * ip = (void *) p;
315 struct udphdr * udp = (void *) (ip + 1);
317 s = format (s, "udp %U:%U -> %U:%U", format_network_address, AF_INET,
318 &ip->saddr, format_network_port, IPPROTO_UDP,
319 ntohs (udp->source), format_network_address, AF_INET, &ip->daddr,
320 format_network_port, IPPROTO_UDP, ntohs (udp->dest));
325 u8 * format_icmp4_type_and_code (u8 * s, va_list * args)
327 uword icmp_type = va_arg (*args, uword);
328 uword icmp_code = va_arg (*args, uword);
332 #define _(f,str) case ICMP_##f: s = format (s, str); break;
333 _ (ECHOREPLY, "echo reply");
334 _ (DEST_UNREACH, "unreachable");
335 _ (SOURCE_QUENCH, "source quench");
336 _ (REDIRECT, "redirect");
337 _ (ECHO, "echo request");
338 _ (TIME_EXCEEDED, "time exceeded");
339 _ (PARAMETERPROB, "parameter problem");
340 _ (TIMESTAMP, "timestamp request");
341 _ (TIMESTAMPREPLY, "timestamp reply");
342 _ (INFO_REQUEST, "information request");
343 _ (INFO_REPLY, "information reply");
344 _ (ADDRESS, "address mask request");
345 _ (ADDRESSREPLY, "address mask reply");
348 s = format (s, "unknown type 0x%x", icmp_type);
351 if (icmp_type == ICMP_DEST_UNREACH)
355 #define _(f,str) case ICMP_##f: s = format (s, " " # str); break;
356 _ (NET_UNREACH, "network");
357 _ (HOST_UNREACH, "host");
358 _ (PROT_UNREACH, "protocol");
359 _ (PORT_UNREACH, "port");
360 _ (FRAG_NEEDED, ": fragmentation needed/DF set");
361 _ (SR_FAILED, "source route failed");
362 _ (NET_UNKNOWN, "network unknown");
363 _ (HOST_UNKNOWN, "host unknown");
364 _ (HOST_ISOLATED, "host isolated");
365 _ (NET_ANO, "network: admin. prohibited");
366 _ (HOST_ANO, "host: admin. prohibited");
367 _ (NET_UNR_TOS, "network for type-of-service");
368 _ (HOST_UNR_TOS, "host for type-of-service");
369 _ (PKT_FILTERED, ": packet filtered");
370 _ (PREC_VIOLATION, "precedence violation");
371 _ (PREC_CUTOFF, "precedence cut off");
374 s = format (s, "unknown code 0x%x", icmp_code);
377 else if (icmp_type == ICMP_REDIRECT)
381 #define _(f,str) case ICMP_##f: s = format (s, " " # str); break;
382 _ (REDIR_NET, "network");
383 _ (REDIR_HOST, "host");
384 _ (REDIR_NETTOS, "network for type-of-service");
385 _ (REDIR_HOSTTOS, "host for type-of-service");
388 s = format (s, "unknown code 0x%x", icmp_code);
391 else if (icmp_type == ICMP_TIME_EXCEEDED)
395 #define _(f,str) case ICMP_##f: s = format (s, " " # str); break;
396 _ (EXC_TTL, "time-to-live zero in transit");
397 _ (EXC_FRAGTIME, "time-to-live zero during reassembly");
400 s = format (s, "unknown code 0x%x", icmp_code);
413 u8 * format_icmp4_packet (u8 * s, va_list * args)
415 u8 * p = va_arg (*args, u8 *);
416 struct iphdr * ip = (void *) p;
417 icmp4_t * icmp = (void *) (ip + 1);
418 s = format (s, "icmp %U %U -> %U",
419 format_icmp4_type_and_code, icmp->type, icmp->code,
420 format_network_address, AF_INET, &ip->saddr,
421 format_network_address, AF_INET, &ip->daddr);
426 u8 * format_ip4_tos_byte (u8 * s, va_list * args)
428 uword tos = va_arg (*args, uword);
430 if (tos & IPTOS_LOWDELAY)
431 s = format (s, "minimize-delay, ");
432 if (tos & IPTOS_MINCOST)
433 s = format (s, "minimize-cost, ");
434 if (tos & IPTOS_THROUGHPUT)
435 s = format (s, "maximize-throughput, ");
436 if (tos & IPTOS_RELIABILITY)
437 s = format (s, "maximize-reliability, ");
439 switch (IPTOS_PREC (tos))
441 #define _(x,y) case IPTOS_PREC_##x: s = format (s, y); break
442 _ (NETCONTROL, "network");
443 _ (INTERNETCONTROL, "internet");
444 _ (CRITIC_ECP, "critical");
446 _ (FLASHOVERRIDE, "flash-override");
447 _ (IMMEDIATE, "immediate");
448 _ (PRIORITY, "priority");
449 _ (ROUTINE, "routine");
456 u8 * format_ip4_packet (u8 * s, va_list * args)
458 u8 * p = va_arg (*args, u8 *);
459 struct iphdr * ip = (void *) p;
461 static format_function_t * f[256];
463 if (! f[IPPROTO_TCP])
465 f[IPPROTO_TCP] = format_tcp4_packet;
466 f[IPPROTO_UDP] = format_udp4_packet;
467 f[IPPROTO_ICMP] = format_icmp4_packet;
471 return format (s, "%U", f[ip->protocol], p);
473 s = format (s, "%U: %U -> %U",
474 format_network_protocol, AF_INET, ip->protocol,
475 format_network_address, AF_INET, &ip->saddr,
476 format_network_address, AF_INET, &ip->daddr);
481 #define foreach_unix_arphrd_type \
531 _ (IEEE802_TR, 800) \
533 _ (IEEE80211_PRISM, 802) \
534 _ (IEEE80211_RADIOTAP, 803) \
538 u8 * format_unix_arphrd (u8 * s, va_list * args)
540 #ifndef __COVERITY__ /* doesn't understand this at all... */
541 u32 x = va_arg (*args, u32);
545 #define _(f,n) case ARPHRD_##f: t = #f; break;
546 foreach_unix_arphrd_type
554 s = format (s, "%s", t);
556 s = format (s, "unknown 0x%x", x);
561 #define foreach_unix_interface_flag \
582 static char * unix_interface_flag_names[] = {
584 foreach_unix_interface_flag
588 u8 * format_unix_interface_flags (u8 * s, va_list * args)
590 u32 x = va_arg (*args, u32);
594 s = format (s, "none");
595 else foreach_set_bit (i, x, ({
596 if (i < ARRAY_LEN (unix_interface_flag_names))
597 s = format (s, "%s", unix_interface_flag_names[i]);
599 s = format (s, "unknown %d", i);
601 s = format (s, ", ");
607 u16 ar_hrd; /* format of hardware address */
608 u16 ar_pro; /* format of protocol address */
609 u8 ar_hln; /* length of hardware address */
610 u8 ar_pln; /* length of protocol address */
611 u16 ar_op; /* ARP opcode (command) */
612 u8 ar_sha[6]; /* sender hardware address */
613 u8 ar_spa[4]; /* sender IP address */
614 u8 ar_tha[6]; /* target hardware address */
615 u8 ar_tpa[4]; /* target IP address */
618 u8 * format_arp_packet (u8 * s, va_list * args)
620 arp_ether_ip4_t * a = va_arg (*args, arp_ether_ip4_t *);
621 char * op = "unknown";
623 if (a->ar_pro != ETH_P_IP ||
624 a->ar_hrd != ARPHRD_ETHER)
629 #define _(f) case ARPOP_##f: op = #f; break;
637 s = format (s, "%s %U %U -> %U %U",
639 format_network_address, AF_INET, a->ar_spa,
640 format_network_address, AF_UNSPEC, a->ar_sha,
641 format_network_address, AF_INET, a->ar_tpa,
642 format_network_address, AF_UNSPEC, a->ar_tha);
646 u8 * format_ethernet_proto (u8 * s, va_list * args)
648 uword type = va_arg (*args, uword);
653 case 0: t = "BPDU"; break;
654 #define _(f) case ETH_P_##f: t = #f; break;
681 #ifdef ETH_P_PPP_DISC
710 vec_add (s, t, strlen (t));
712 s = format (s, "ether-type 0x%x", type);
716 u8 * format_ethernet_packet (u8 * s, va_list * args)
718 struct ethhdr * h = va_arg (*args, struct ethhdr *);
719 uword proto = h->h_proto;
720 u8 * payload = (void *) (h + 1);
723 /* Check for 802.2/802.3 encapsulation. */
724 if (proto < ETH_DATA_LEN)
727 u8 dsap, ssap, control;
731 ethhdr_802_t * h1 = (void *) (h + 1);
733 payload = (void *) (h1 + 1);
736 indent = format_get_indent (s);
738 s = format (s, "%U: %U -> %U",
739 format_ethernet_proto, proto,
740 format_network_address, AF_UNSPEC, h->h_source,
741 format_network_address, AF_UNSPEC, h->h_dest);
746 s = format (s, "\n%U%U",
747 format_white_space, indent,
748 format_arp_packet, payload);
756 u8 * format_hostname (u8 * s, va_list * args)
760 if (gethostname (b, sizeof (buffer)) < 0)
762 return format (s, "%s", b);
767 u8 * format_timeval (u8 * s, va_list * args)
769 char * fmt = va_arg (*args, char *);
770 struct timeval * tv = va_arg (*args, struct timeval *);
776 fmt = "y/m/d H:M:S:F";
780 static struct timeval now;
781 gettimeofday (&now, 0);
785 msec = flt_round_nearest (1e-3 * tv->tv_usec);
787 { msec = 0; tv->tv_sec++; }
790 time_t t = tv->tv_sec;
794 for (f = fmt; *f; f++)
797 char * what_fmt = "%d";
806 what = 1900 + tm->tm_year;
810 what = tm->tm_mon + 1;
835 s = format (s, what_fmt, what);
842 u8 * format_time_float (u8 * s, va_list * args)
844 u8 * fmt = va_arg (*args, u8 *);
845 f64 t = va_arg (*args, f64);
848 t = unix_time_now ();
850 tv.tv_usec = 1e6*(t - tv.tv_sec);
851 return format (s, "%U", format_timeval, fmt, &tv);
854 u8 * format_signal (u8 * s, va_list * args)
856 uword signum = va_arg (*args, uword);
860 #define _(x) case x: t = #x; break;
898 return format (s, "unknown %d", signum);
901 vec_add (s, t, strlen (t));
905 u8 * format_ucontext_pc (u8 * s, va_list * args)
907 ucontext_t * uc __attribute__((unused));
908 unsigned long * regs = 0;
911 uc = va_arg (*args, ucontext_t *);
913 #if defined (powerpc)
914 regs = &uc->uc_mcontext.uc_regs->gregs[0];
915 #elif defined (powerpc64)
916 regs = &uc->uc_mcontext.uc_regs->gp_regs[0];
917 #elif defined (i386) || defined (__x86_64__)
918 regs = (void *) &uc->uc_mcontext.gregs[0];
921 #if defined (powerpc) || defined (powerpc64)
925 #elif defined (__x86_64__)
933 return format (s, "unsupported");
935 return format (s, "%p", regs[reg_no]);
939 unformat_unix_gid (unformat_input_t * input, va_list * args)
941 gid_t *gid = va_arg (*args, gid_t *);
942 struct group *grp = 0;
946 if (unformat (input, "%d", &r))
950 else if (unformat (input, "%s", &s))
952 grp = getgrnam ((char *) s);
963 #endif /* __KERNEL__ */