arp.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <ip.h>
  4. #include "dat.h"
  5. #include "protos.h"
  6. typedef struct Hdr Hdr;
  7. struct Hdr
  8. {
  9. uchar hrd[2];
  10. uchar pro[2];
  11. uchar hln;
  12. uchar pln;
  13. uchar op[2];
  14. uchar sha[6];
  15. uchar spa[4];
  16. uchar tha[6];
  17. uchar tpa[4];
  18. };
  19. enum
  20. {
  21. ARPLEN= 28,
  22. };
  23. enum
  24. {
  25. Ospa,
  26. Otpa,
  27. Ostpa,
  28. Osha,
  29. Otha,
  30. Ostha,
  31. Opa,
  32. };
  33. static Field p_fields[] =
  34. {
  35. {"spa", Fv4ip, Ospa, "protocol source", } ,
  36. {"tpa", Fv4ip, Otpa, "protocol target", } ,
  37. {"a", Fv4ip, Ostpa, "protocol source/target", } ,
  38. {"sha", Fba, Osha, "hardware source", } ,
  39. {"tha", Fba, Otha, "hardware target", } ,
  40. {"ah", Fba, Ostha, "hardware source/target", } ,
  41. {0}
  42. };
  43. static void
  44. p_compile(Filter *f)
  45. {
  46. if(f->op == '='){
  47. compile_cmp(arp.name, f, p_fields);
  48. return;
  49. }
  50. sysfatal("unknown arp field: %s", f->s);
  51. }
  52. static int
  53. p_filter(Filter *f, Msg *m)
  54. {
  55. Hdr *h;
  56. if(m->pe - m->ps < ARPLEN)
  57. return 0;
  58. h = (Hdr*)m->ps;
  59. m->ps += ARPLEN;
  60. switch(f->subop){
  61. case Ospa:
  62. return h->pln == 4 && NetL(h->spa) == f->ulv;
  63. case Otpa:
  64. return h->pln == 4 && NetL(h->tpa) == f->ulv;
  65. case Ostpa:
  66. return h->pln == 4 && (NetL(h->tpa) == f->ulv ||
  67. NetL(h->spa) == f->ulv);
  68. case Osha:
  69. return memcmp(h->sha, f->a, h->hln) == 0;
  70. case Otha:
  71. return memcmp(h->tha, f->a, h->hln) == 0;
  72. case Ostha:
  73. return memcmp(h->sha, f->a, h->hln)==0
  74. ||memcmp(h->tha, f->a, h->hln)==0;
  75. }
  76. return 0;
  77. }
  78. static int
  79. p_seprint(Msg *m)
  80. {
  81. Hdr *h;
  82. if(m->pe - m->ps < ARPLEN)
  83. return -1;
  84. h = (Hdr*)m->ps;
  85. m->ps += ARPLEN;
  86. /* no next protocol */
  87. m->pr = nil;
  88. m->p = seprint(m->p, m->e, "op=%1d len=%1d/%1d spa=%V sha=%E tpa=%V tha=%E",
  89. NetS(h->op), h->pln, h->hln,
  90. h->spa, h->sha, h->tpa, h->tha);
  91. return 0;
  92. }
  93. Proto arp =
  94. {
  95. "arp",
  96. p_compile,
  97. p_filter,
  98. p_seprint,
  99. nil,
  100. nil,
  101. p_fields,
  102. defaultframer,
  103. };
  104. Proto rarp =
  105. {
  106. "rarp",
  107. p_compile,
  108. p_filter,
  109. p_seprint,
  110. nil,
  111. nil,
  112. p_fields,
  113. defaultframer,
  114. };