ip.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. {
  9. uchar vihl; /* Version and header length */
  10. uchar tos; /* Type of service */
  11. uchar length[2]; /* packet length */
  12. uchar id[2]; /* ip->identification */
  13. uchar frag[2]; /* Fragment information */
  14. uchar ttl; /* Time to live */
  15. uchar proto; /* Protocol */
  16. uchar cksum[2]; /* Header checksum */
  17. uchar src[4]; /* IP source */
  18. uchar dst[4]; /* IP destination */
  19. };
  20. enum
  21. {
  22. IPHDR = 20, /* sizeof(Iphdr) */
  23. IP_VER = 0x40, /* Using IP version 4 */
  24. IP_DF = 0x4000, /* Don't fragment */
  25. IP_MF = 0x2000, /* More fragments */
  26. };
  27. static Mux p_mux[] =
  28. {
  29. { "icmp", 1, },
  30. { "igmp", 2, },
  31. { "ggp", 3, },
  32. { "ip", 4, },
  33. { "st", 5, },
  34. { "tcp", 6, },
  35. { "ucl", 7, },
  36. { "egp", 8, },
  37. { "igp", 9, },
  38. { "bbn-rcc-mon", 10, },
  39. { "nvp-ii", 11, },
  40. { "pup", 12, },
  41. { "argus", 13, },
  42. { "emcon", 14, },
  43. { "xnet", 15, },
  44. { "chaos", 16, },
  45. { "udp", 17, },
  46. { "mux", 18, },
  47. { "dcn-meas", 19, },
  48. { "hmp", 20, },
  49. { "prm", 21, },
  50. { "xns-idp", 22, },
  51. { "trunk-1", 23, },
  52. { "trunk-2", 24, },
  53. { "leaf-1", 25, },
  54. { "leaf-2", 26, },
  55. { "rdp", 27, },
  56. { "irtp", 28, },
  57. { "iso-tp4", 29, },
  58. { "netblt", 30, },
  59. { "mfe-nsp", 31, },
  60. { "merit-inp", 32, },
  61. { "sep", 33, },
  62. { "3pc", 34, },
  63. { "idpr", 35, },
  64. { "xtp", 36, },
  65. { "ddp", 37, },
  66. { "idpr-cmtp", 38, },
  67. { "tp++", 39, },
  68. { "il", 40, },
  69. { "sip", 41, },
  70. { "sdrp", 42, },
  71. { "sip-sr", 43, },
  72. { "sip-frag", 44, },
  73. { "idrp", 45, },
  74. { "rsvp", 46, },
  75. { "gre", 47, },
  76. { "mhrp", 48, },
  77. { "bna", 49, },
  78. { "sipp-esp", 50, },
  79. { "sipp-ah", 51, },
  80. { "i-nlsp", 52, },
  81. { "swipe", 53, },
  82. { "nhrp", 54, },
  83. { "any", 61, },
  84. { "cftp", 62, },
  85. { "any", 63, },
  86. { "sat-expak", 64, },
  87. { "kryptolan", 65, },
  88. { "rvd", 66, },
  89. { "ippc", 67, },
  90. { "any", 68, },
  91. { "sat-mon", 69, },
  92. { "visa", 70, },
  93. { "ipcv", 71, },
  94. { "cpnx", 72, },
  95. { "cphb", 73, },
  96. { "wsn", 74, },
  97. { "pvp", 75, },
  98. { "br-sat-mon", 76, },
  99. { "sun-nd", 77, },
  100. { "wb-mon", 78, },
  101. { "wb-expak", 79, },
  102. { "iso-ip", 80, },
  103. { "vmtp", 81, },
  104. { "secure-vmtp", 82, },
  105. { "vines", 83, },
  106. { "ttp", 84, },
  107. { "nsfnet-igp", 85, },
  108. { "dgp", 86, },
  109. { "tcf", 87, },
  110. { "igrp", 88, },
  111. { "ospf", 89, },
  112. { "sprite-rpc", 90, },
  113. { "larp", 91, },
  114. { "mtp", 92, },
  115. { "ax.25", 93, },
  116. { "ipip", 94, },
  117. { "micp", 95, },
  118. { "scc-sp", 96, },
  119. { "etherip", 97, },
  120. { "encap", 98, },
  121. { "any", 99, },
  122. { "gmtp", 100, },
  123. { "rudp", 254, },
  124. { 0 }
  125. };
  126. enum
  127. {
  128. Os, // source
  129. Od, // destination
  130. Osd, // source or destination
  131. Ot, // type
  132. };
  133. static Field p_fields[] =
  134. {
  135. {"s", Fv4ip, Os, "source address", } ,
  136. {"d", Fv4ip, Od, "destination address", } ,
  137. {"a", Fv4ip, Osd, "source|destination address",} ,
  138. {"sd", Fv4ip, Osd, "source|destination address",} ,
  139. {"t", Fnum, Ot, "sub protocol number", } ,
  140. {0}
  141. };
  142. static void
  143. p_compile(Filter *f)
  144. {
  145. Mux *m;
  146. if(f->op == '='){
  147. compile_cmp(ip.name, f, p_fields);
  148. return;
  149. }
  150. for(m = p_mux; m->name != nil; m++)
  151. if(strcmp(f->s, m->name) == 0){
  152. f->pr = m->pr;
  153. f->ulv = m->val;
  154. f->subop = Ot;
  155. return;
  156. }
  157. sysfatal("unknown ip field or protocol: %s", f->s);
  158. }
  159. static int
  160. p_filter(Filter *f, Msg *m)
  161. {
  162. Hdr *h;
  163. if(m->pe - m->ps < IPHDR)
  164. return 0;
  165. h = (Hdr*)m->ps;
  166. m->ps += ((h->vihl&0xf)<<2);
  167. switch(f->subop){
  168. case Os:
  169. return NetL(h->src) == f->ulv;
  170. case Od:
  171. return NetL(h->dst) == f->ulv;
  172. case Osd:
  173. return NetL(h->src) == f->ulv || NetL(h->dst) == f->ulv;
  174. case Ot:
  175. return h->proto == f->ulv;
  176. }
  177. return 0;
  178. }
  179. static int
  180. p_seprint(Msg *m)
  181. {
  182. Hdr *h;
  183. int f;
  184. int len;
  185. if(m->pe - m->ps < IPHDR)
  186. return -1;
  187. h = (Hdr*)m->ps;
  188. /* next protocol, just dump unless this is the first fragment */
  189. m->pr = &dump;
  190. f = NetS(h->frag);
  191. if((f & ~(IP_DF|IP_MF)) == 0)
  192. demux(p_mux, h->proto, h->proto, m, &dump);
  193. /* truncate the message if there's extra */
  194. len = NetS(h->length);
  195. if(len < m->pe - m->ps)
  196. m->pe = m->ps + len;
  197. /* next header */
  198. m->ps += ((h->vihl&0xf)<<2);
  199. m->p = seprint(m->p, m->e, "s=%V d=%V id=%4.4ux frag=%4.4ux ttl=%3d pr=%d ln=%d",
  200. h->src, h->dst,
  201. NetS(h->id),
  202. NetS(h->frag),
  203. h->ttl,
  204. h->proto,
  205. NetS(h->length)
  206. );
  207. return 0;
  208. }
  209. Proto ip =
  210. {
  211. "ip",
  212. p_compile,
  213. p_filter,
  214. p_seprint,
  215. p_mux,
  216. "%lud",
  217. p_fields,
  218. defaultframer,
  219. };