print-pflog.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*
  2. * Copyright (c) 1990, 1991, 1993, 1994, 1995, 1996
  3. * The Regents of the University of California. All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that: (1) source code distributions
  7. * retain the above copyright notice and this paragraph in its entirety, (2)
  8. * distributions including binary code include the above copyright notice and
  9. * this paragraph in its entirety in the documentation or other materials
  10. * provided with the distribution, and (3) all advertising materials mentioning
  11. * features or use of this software display the following acknowledgement:
  12. * ``This product includes software developed by the University of California,
  13. * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
  14. * the University nor the names of its contributors may be used to endorse
  15. * or promote products derived from this software without specific prior
  16. * written permission.
  17. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
  18. * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
  19. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  20. */
  21. /* \summary: OpenBSD packet filter log file printer */
  22. #ifdef HAVE_CONFIG_H
  23. #include "config.h"
  24. #endif
  25. #ifndef HAVE_NET_PFVAR_H
  26. #error "No pf headers available"
  27. #endif
  28. #include <sys/types.h>
  29. #include <sys/socket.h>
  30. #include <net/if.h>
  31. #include <net/pfvar.h>
  32. #include <net/if_pflog.h>
  33. #include <netdissect-stdinc.h>
  34. #include "netdissect.h"
  35. #include "extract.h"
  36. static const char tstr[] = "[|pflog]";
  37. static const struct tok pf_reasons[] = {
  38. { 0, "0(match)" },
  39. { 1, "1(bad-offset)" },
  40. { 2, "2(fragment)" },
  41. { 3, "3(short)" },
  42. { 4, "4(normalize)" },
  43. { 5, "5(memory)" },
  44. { 6, "6(bad-timestamp)" },
  45. { 7, "7(congestion)" },
  46. { 8, "8(ip-option)" },
  47. { 9, "9(proto-cksum)" },
  48. { 10, "10(state-mismatch)" },
  49. { 11, "11(state-insert)" },
  50. { 12, "12(state-limit)" },
  51. { 13, "13(src-limit)" },
  52. { 14, "14(synproxy)" },
  53. { 0, NULL }
  54. };
  55. static const struct tok pf_actions[] = {
  56. { PF_PASS, "pass" },
  57. { PF_DROP, "block" },
  58. { PF_SCRUB, "scrub" },
  59. { PF_NAT, "nat" },
  60. { PF_NONAT, "nat" },
  61. { PF_BINAT, "binat" },
  62. { PF_NOBINAT, "binat" },
  63. { PF_RDR, "rdr" },
  64. { PF_NORDR, "rdr" },
  65. { PF_SYNPROXY_DROP, "synproxy-drop" },
  66. { 0, NULL }
  67. };
  68. static const struct tok pf_directions[] = {
  69. { PF_INOUT, "in/out" },
  70. { PF_IN, "in" },
  71. { PF_OUT, "out" },
  72. { 0, NULL }
  73. };
  74. /* For reading capture files on other systems */
  75. #define OPENBSD_AF_INET 2
  76. #define OPENBSD_AF_INET6 24
  77. static void
  78. pflog_print(netdissect_options *ndo, const struct pfloghdr *hdr)
  79. {
  80. uint32_t rulenr, subrulenr;
  81. rulenr = EXTRACT_32BITS(&hdr->rulenr);
  82. subrulenr = EXTRACT_32BITS(&hdr->subrulenr);
  83. if (subrulenr == (uint32_t)-1)
  84. ND_PRINT((ndo, "rule %u/", rulenr));
  85. else
  86. ND_PRINT((ndo, "rule %u.%s.%u/", rulenr, hdr->ruleset, subrulenr));
  87. ND_PRINT((ndo, "%s: %s %s on %s: ",
  88. tok2str(pf_reasons, "unkn(%u)", hdr->reason),
  89. tok2str(pf_actions, "unkn(%u)", hdr->action),
  90. tok2str(pf_directions, "unkn(%u)", hdr->dir),
  91. hdr->ifname));
  92. }
  93. u_int
  94. pflog_if_print(netdissect_options *ndo, const struct pcap_pkthdr *h,
  95. register const u_char *p)
  96. {
  97. u_int length = h->len;
  98. u_int hdrlen;
  99. u_int caplen = h->caplen;
  100. const struct pfloghdr *hdr;
  101. uint8_t af;
  102. /* check length */
  103. if (caplen < sizeof(uint8_t)) {
  104. ND_PRINT((ndo, "%s", tstr));
  105. return (caplen);
  106. }
  107. #define MIN_PFLOG_HDRLEN 45
  108. hdr = (const struct pfloghdr *)p;
  109. if (hdr->length < MIN_PFLOG_HDRLEN) {
  110. ND_PRINT((ndo, "[pflog: invalid header length!]"));
  111. return (hdr->length); /* XXX: not really */
  112. }
  113. hdrlen = BPF_WORDALIGN(hdr->length);
  114. if (caplen < hdrlen) {
  115. ND_PRINT((ndo, "%s", tstr));
  116. return (hdrlen); /* XXX: true? */
  117. }
  118. /* print what we know */
  119. ND_TCHECK(*hdr);
  120. if (ndo->ndo_eflag)
  121. pflog_print(ndo, hdr);
  122. /* skip to the real packet */
  123. af = hdr->af;
  124. length -= hdrlen;
  125. caplen -= hdrlen;
  126. p += hdrlen;
  127. switch (af) {
  128. case AF_INET:
  129. #if OPENBSD_AF_INET != AF_INET
  130. case OPENBSD_AF_INET: /* XXX: read pcap files */
  131. #endif
  132. ip_print(ndo, p, length);
  133. break;
  134. #if defined(AF_INET6) || defined(OPENBSD_AF_INET6)
  135. #ifdef AF_INET6
  136. case AF_INET6:
  137. #endif /* AF_INET6 */
  138. #if !defined(AF_INET6) || OPENBSD_AF_INET6 != AF_INET6
  139. case OPENBSD_AF_INET6: /* XXX: read pcap files */
  140. #endif /* !defined(AF_INET6) || OPENBSD_AF_INET6 != AF_INET6 */
  141. ip6_print(ndo, p, length);
  142. break;
  143. #endif /* defined(AF_INET6) || defined(OPENBSD_AF_INET6) */
  144. default:
  145. /* address family not handled, print raw packet */
  146. if (!ndo->ndo_eflag)
  147. pflog_print(ndo, hdr);
  148. if (!ndo->ndo_suppress_default_print)
  149. ND_DEFAULTPRINT(p, caplen);
  150. }
  151. return (hdrlen);
  152. trunc:
  153. ND_PRINT((ndo, "%s", tstr));
  154. return (hdrlen);
  155. }
  156. /*
  157. * Local Variables:
  158. * c-style: whitesmith
  159. * c-basic-offset: 8
  160. * End:
  161. */