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