ether.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. {0}
  26. };
  27. enum
  28. {
  29. Os, // source
  30. Od, // destination
  31. Oa, // source or destination
  32. Ot, // type
  33. };
  34. static Field p_fields[] =
  35. {
  36. {"s", Fether, Os, "source address", } ,
  37. {"d", Fether, Od, "destination address", } ,
  38. {"a", Fether, Oa, "source|destination address" } ,
  39. {"sd", Fether, Oa, "source|destination address" } ,
  40. {"t", Fnum, Ot, "type" } ,
  41. {0}
  42. };
  43. static void
  44. p_compile(Filter *f)
  45. {
  46. Mux *m;
  47. if(f->op == '='){
  48. compile_cmp(ether.name, f, p_fields);
  49. return;
  50. }
  51. for(m = p_mux; m->name != nil; m++)
  52. if(strcmp(f->s, m->name) == 0){
  53. f->pr = m->pr;
  54. f->ulv = m->val;
  55. f->subop = Ot;
  56. return;
  57. }
  58. sysfatal("unknown ethernet field or protocol: %s", f->s);
  59. }
  60. static int
  61. p_filter(Filter *f, Msg *m)
  62. {
  63. Hdr *h;
  64. if(m->pe - m->ps < ETHERHDRSIZE)
  65. return 0;
  66. h = (Hdr*)m->ps;
  67. m->ps += ETHERHDRSIZE;
  68. switch(f->subop){
  69. case Os:
  70. return !memcmp(h->s, f->a, 6);
  71. case Od:
  72. return !memcmp(h->d, f->a, 6);
  73. case Oa:
  74. return memcmp(h->s, f->a, 6) == 0 || memcmp(h->d, f->a, 6) == 0;
  75. case Ot:
  76. return NetS(h->type) == f->ulv;
  77. }
  78. return 0;
  79. }
  80. static int
  81. p_seprint(Msg *m)
  82. {
  83. Hdr *h;
  84. uint t;
  85. int len;
  86. len = m->pe - m->ps;
  87. if(len < ETHERHDRSIZE)
  88. return -1;
  89. h = (Hdr*)m->ps;
  90. m->ps += ETHERHDRSIZE;
  91. t = NetS(h->type);
  92. demux(p_mux, t, t, m, &dump);
  93. m->p = seprint(m->p, m->e, "s=%E d=%E pr=%4.4ux ln=%d", h->s, h->d,
  94. t, len);
  95. return 0;
  96. }
  97. Proto ether =
  98. {
  99. "ether",
  100. p_compile,
  101. p_filter,
  102. p_seprint,
  103. p_mux,
  104. "%#.4lux",
  105. p_fields,
  106. defaultframer
  107. };