ether.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include <u.h>
  10. #include <libc.h>
  11. #include <ip.h>
  12. #include "dat.h"
  13. #include "protos.h"
  14. typedef struct Hdr Hdr;
  15. struct Hdr {
  16. uint8_t d[6];
  17. uint8_t s[6];
  18. uint8_t type[2];
  19. char data[1500];
  20. };
  21. #define ETHERMINTU 60 /* minimum transmit size */
  22. #define ETHERMAXTU 1514 /* maximum transmit size */
  23. #define ETHERHDRSIZE 14 /* size of an ethernet header */
  24. static Mux p_mux[] =
  25. {
  26. {"ip", 0x0800, } ,
  27. {"arp", 0x0806, } ,
  28. {"rarp", 0x0806, } ,
  29. {"ip6", 0x86dd, } ,
  30. {"pppoe_disc", 0x8863, },
  31. {"pppoe_sess", 0x8864, },
  32. {"eapol", 0x888e, },
  33. {"aoe", 0x88a2, } ,
  34. {"cec", 0xbcbc, } ,
  35. {0}
  36. };
  37. enum
  38. {
  39. Os, /* source */
  40. Od, /* destination */
  41. Oa, /* source or destination */
  42. Ot, /* type */
  43. };
  44. static Field p_fields[] =
  45. {
  46. {"s", Fether, Os, "source address", } ,
  47. {"d", Fether, Od, "destination address", } ,
  48. {"a", Fether, Oa, "source|destination address" } ,
  49. {"sd", Fether, Oa, "source|destination address" } ,
  50. {"t", Fnum, Ot, "type" } ,
  51. {0}
  52. };
  53. static void
  54. p_compile(Filter *f)
  55. {
  56. Mux *m;
  57. if(f->op == '='){
  58. compile_cmp(ether.name, f, p_fields);
  59. return;
  60. }
  61. for(m = p_mux; m->name != nil; m++)
  62. if(strcmp(f->s, m->name) == 0){
  63. f->pr = m->pr;
  64. f->ulv = m->val;
  65. f->subop = Ot;
  66. return;
  67. }
  68. sysfatal("unknown ethernet field or protocol: %s", f->s);
  69. }
  70. static int
  71. p_filter(Filter *f, Msg *m)
  72. {
  73. Hdr *h;
  74. if(m->pe - m->ps < ETHERHDRSIZE)
  75. return 0;
  76. h = (Hdr*)m->ps;
  77. m->ps += ETHERHDRSIZE;
  78. switch(f->subop){
  79. case Os:
  80. return memcmp(h->s, f->a, 6) == 0;
  81. case Od:
  82. return memcmp(h->d, f->a, 6) == 0;
  83. case Oa:
  84. return memcmp(h->s, f->a, 6) == 0 || memcmp(h->d, f->a, 6) == 0;
  85. case Ot:
  86. return NetS(h->type) == f->ulv;
  87. }
  88. return 0;
  89. }
  90. static int
  91. p_seprint(Msg *m)
  92. {
  93. int len;
  94. uint t;
  95. Hdr *h;
  96. len = m->pe - m->ps;
  97. if(len < ETHERHDRSIZE)
  98. return -1;
  99. h = (Hdr*)m->ps;
  100. m->ps += ETHERHDRSIZE;
  101. t = NetS(h->type);
  102. demux(p_mux, t, t, m, &dump);
  103. m->p = seprint(m->p, m->e, "s=%E d=%E pr=%4.4ux ln=%d", h->s, h->d,
  104. t, len);
  105. return 0;
  106. }
  107. Proto ether =
  108. {
  109. "ether",
  110. p_compile,
  111. p_filter,
  112. p_seprint,
  113. p_mux,
  114. "%#.4lux",
  115. p_fields,
  116. defaultframer
  117. };