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 {0} 25 }; 26 27 enum 28 { 29 Os, // source 30 Od, // destination 31 Oa, // source or destination 32 Ot, // type 33 }; 34 35 static Field p_fields[] = 36 { 37 {"s", Fether, Os, "source address", } , 38 {"d", Fether, Od, "destination address", } , 39 {"a", Fether, Oa, "source|destination address" } , 40 {"t", Fnum, Ot, "type" } , 41 {0} 42 }; 43 44 static void 45 p_compile(Filter *f) 46 { 47 Mux *m; 48 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 63 static int 64 p_filter(Filter *f, Msg *m) 65 { 66 Hdr *h; 67 68 if(m->pe - m->ps < ETHERHDRSIZE) 69 return 0; 70 71 h = (Hdr*)m->ps; 72 m->ps += ETHERHDRSIZE; 73 74 switch(f->subop){ 75 case Os: 76 return !memcmp(h->s, f->a, 6); 77 case Od: 78 return !memcmp(h->d, f->a, 6); 79 case Oa: 80 return memcmp(h->s, f->a, 6) == 0 || memcmp(h->d, f->a, 6) == 0; 81 case Ot: 82 return NetS(h->type) == f->ulv; 83 } 84 return 0; 85 } 86 87 static int 88 p_seprint(Msg *m) 89 { 90 Hdr *h; 91 uint t; 92 int len; 93 94 len = m->pe - m->ps; 95 if(len < ETHERHDRSIZE) 96 return -1; 97 98 h = (Hdr*)m->ps; 99 m->ps += ETHERHDRSIZE; 100 101 t = NetS(h->type); 102 demux(p_mux, t, t, m, &dump); 103 104 m->p = seprint(m->p, m->e, "s=%E d=%E pr=%4.4ux ln=%d", h->s, h->d, 105 t, len); 106 return 0; 107 } 108 109 Proto ether = 110 { 111 "ether", 112 p_compile, 113 p_filter, 114 p_seprint, 115 p_mux, 116 p_fields, 117 defaultframer 118 }; 119