ether.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. uchar d[6];
  9. uchar s[6];
  10. uchar type[2];
  11. char data[1500];
  12. };
  13. #define ETHERMINTU 60 /* minimum transmit size */
  14. #define ETHERMAXTU 1514 /* maximum transmit size */
  15. #define ETHERHDRSIZE 14 /* size of an ethernet header */
  16. static Mux p_mux[] =
  17. {
  18. {"ip", 0x0800, } ,
  19. {"arp", 0x0806, } ,
  20. {"rarp", 0x0806, } ,
  21. {"ip6", 0x86dd, } ,
  22. {"pppoe_disc", 0x8863, },
  23. {"pppoe_sess", 0x8864, },
  24. {"eapol", 0x888e, },
  25. {"aoe", 0x88a2, } ,
  26. {"cec", 0xbcbc, } ,
  27. {0}
  28. };
  29. enum
  30. {
  31. Os, /* source */
  32. Od, /* destination */
  33. Oa, /* source or destination */
  34. Ot, /* type */
  35. };
  36. static Field p_fields[] =
  37. {
  38. {"s", Fether, Os, "source address", } ,
  39. {"d", Fether, Od, "destination address", } ,
  40. {"a", Fether, Oa, "source|destination address" } ,
  41. {"sd", Fether, Oa, "source|destination address" } ,
  42. {"t", Fnum, Ot, "type" } ,
  43. {0}
  44. };
  45. static void
  46. p_compile(Filter *f)
  47. {
  48. Mux *m;
  49. if(f->op == '='){
  50. compile_cmp(ether.name, f, p_fields);
  51. return;
  52. }
  53. for(m = p_mux; m->name != nil; m++)
  54. if(strcmp(f->s, m->name) == 0){
  55. f->pr = m->pr;
  56. f->ulv = m->val;
  57. f->subop = Ot;
  58. return;
  59. }
  60. sysfatal("unknown ethernet field or protocol: %s", f->s);
  61. }
  62. static int
  63. p_filter(Filter *f, Msg *m)
  64. {
  65. Hdr *h;
  66. if(m->pe - m->ps < ETHERHDRSIZE)
  67. return 0;
  68. h = (Hdr*)m->ps;
  69. m->ps += ETHERHDRSIZE;
  70. switch(f->subop){
  71. case Os:
  72. return memcmp(h->s, f->a, 6) == 0;
  73. case Od:
  74. return memcmp(h->d, f->a, 6) == 0;
  75. case Oa:
  76. return memcmp(h->s, f->a, 6) == 0 || memcmp(h->d, f->a, 6) == 0;
  77. case Ot:
  78. return NetS(h->type) == f->ulv;
  79. }
  80. return 0;
  81. }
  82. static int
  83. p_seprint(Msg *m)
  84. {
  85. int len;
  86. uint t;
  87. Hdr *h;
  88. len = m->pe - m->ps;
  89. if(len < ETHERHDRSIZE)
  90. return -1;
  91. h = (Hdr*)m->ps;
  92. m->ps += ETHERHDRSIZE;
  93. t = NetS(h->type);
  94. demux(p_mux, t, t, m, &dump);
  95. m->p = seprint(m->p, m->e, "s=%E d=%E pr=%4.4ux ln=%d", h->s, h->d,
  96. t, len);
  97. return 0;
  98. }
  99. Proto ether =
  100. {
  101. "ether",
  102. p_compile,
  103. p_filter,
  104. p_seprint,
  105. p_mux,
  106. "%#.4lux",
  107. p_fields,
  108. defaultframer
  109. };