ip6.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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 vcf[4]; /* Version and header length */
  10. uchar length[2]; /* packet length */
  11. uchar proto; /* Protocol */
  12. uchar ttl; /* Time to live */
  13. uchar src[IPaddrlen]; /* IP source */
  14. uchar dst[IPaddrlen]; /* IP destination */
  15. };
  16. enum
  17. {
  18. IP6HDR = 40, /* sizeof(Iphdr) */
  19. IP_VER = 0x60, /* Using IP version 4 */
  20. HBH_HDR = 0,
  21. ROUT_HDR = 43,
  22. FRAG_HDR = 44,
  23. FRAG_HSZ = 8, /* in bytes */
  24. DEST_HDR = 60,
  25. };
  26. static Mux p_mux[] =
  27. {
  28. { "igmp", 2, },
  29. { "ggp", 3, },
  30. { "ip", 4, },
  31. { "st", 5, },
  32. { "tcp", 6, },
  33. { "ucl", 7, },
  34. { "egp", 8, },
  35. { "igp", 9, },
  36. { "bbn-rcc-mon", 10, },
  37. { "nvp-ii", 11, },
  38. { "pup", 12, },
  39. { "argus", 13, },
  40. { "emcon", 14, },
  41. { "xnet", 15, },
  42. { "chaos", 16, },
  43. { "udp", 17, },
  44. { "mux", 18, },
  45. { "dcn-meas", 19, },
  46. { "hmp", 20, },
  47. { "prm", 21, },
  48. { "xns-idp", 22, },
  49. { "trunk-1", 23, },
  50. { "trunk-2", 24, },
  51. { "leaf-1", 25, },
  52. { "leaf-2", 26, },
  53. { "rdp", 27, },
  54. { "irtp", 28, },
  55. { "iso-tp4", 29, },
  56. { "netblt", 30, },
  57. { "mfe-nsp", 31, },
  58. { "merit-inp", 32, },
  59. { "sep", 33, },
  60. { "3pc", 34, },
  61. { "idpr", 35, },
  62. { "xtp", 36, },
  63. { "ddp", 37, },
  64. { "idpr-cmtp", 38, },
  65. { "tp++", 39, },
  66. { "il", 40, },
  67. { "sip", 41, },
  68. { "sdrp", 42, },
  69. { "idrp", 45, },
  70. { "rsvp", 46, },
  71. { "gre", 47, },
  72. { "mhrp", 48, },
  73. { "bna", 49, },
  74. { "sipp-esp", 50, },
  75. { "sipp-ah", 51, },
  76. { "i-nlsp", 52, },
  77. { "swipe", 53, },
  78. { "nhrp", 54, },
  79. { "icmp6", 58, },
  80. { "any", 61, },
  81. { "cftp", 62, },
  82. { "any", 63, },
  83. { "sat-expak", 64, },
  84. { "kryptolan", 65, },
  85. { "rvd", 66, },
  86. { "ippc", 67, },
  87. { "any", 68, },
  88. { "sat-mon", 69, },
  89. { "visa", 70, },
  90. { "ipcv", 71, },
  91. { "cpnx", 72, },
  92. { "cphb", 73, },
  93. { "wsn", 74, },
  94. { "pvp", 75, },
  95. { "br-sat-mon", 76, },
  96. { "sun-nd", 77, },
  97. { "wb-mon", 78, },
  98. { "wb-expak", 79, },
  99. { "iso-ip", 80, },
  100. { "vmtp", 81, },
  101. { "secure-vmtp", 82, },
  102. { "vines", 83, },
  103. { "ttp", 84, },
  104. { "nsfnet-igp", 85, },
  105. { "dgp", 86, },
  106. { "tcf", 87, },
  107. { "igrp", 88, },
  108. { "ospf", 89, },
  109. { "sprite-rpc", 90, },
  110. { "larp", 91, },
  111. { "mtp", 92, },
  112. { "ax.25", 93, },
  113. { "ipip", 94, },
  114. { "micp", 95, },
  115. { "scc-sp", 96, },
  116. { "etherip", 97, },
  117. { "encap", 98, },
  118. { "any", 99, },
  119. { "gmtp", 100, },
  120. { "rudp", 254, },
  121. { 0 }
  122. };
  123. enum
  124. {
  125. Os, // source
  126. Od, // destination
  127. Osd, // source or destination
  128. Ot, // type
  129. };
  130. static Field p_fields[] =
  131. {
  132. {"s", Fv6ip, Os, "source address", } ,
  133. {"d", Fv6ip, Od, "destination address", } ,
  134. {"a", Fv6ip, Osd, "source|destination address",} ,
  135. {"t", Fnum, Ot, "sub protocol number", } ,
  136. {0}
  137. };
  138. static void
  139. p_compile(Filter *f)
  140. {
  141. Mux *m;
  142. if(f->op == '='){
  143. compile_cmp(ip6.name, f, p_fields);
  144. return;
  145. }
  146. for(m = p_mux; m->name != nil; m++)
  147. if(strcmp(f->s, m->name) == 0){
  148. f->pr = m->pr;
  149. f->ulv = m->val;
  150. f->subop = Ot;
  151. return;
  152. }
  153. sysfatal("unknown ip6 field or protocol: %s", f->s);
  154. }
  155. static int
  156. v6hdrlen(Hdr *h)
  157. {
  158. int plen, len = IP6HDR;
  159. int pktlen = IP6HDR + NetS(h->length);
  160. uchar nexthdr = h->proto;
  161. uchar *pkt = (uchar*) h;
  162. pkt += len;
  163. plen = len;
  164. while ( (nexthdr == HBH_HDR) || (nexthdr == ROUT_HDR) ||
  165. (nexthdr == FRAG_HDR) || (nexthdr == DEST_HDR) ) {
  166. if (nexthdr == FRAG_HDR)
  167. len = FRAG_HSZ;
  168. else
  169. len = ( ((int) *(pkt+1)) + 1) * 8;
  170. if (plen + len > pktlen)
  171. return -1;
  172. pkt += len;
  173. nexthdr = *pkt;
  174. plen += len;
  175. }
  176. return plen;
  177. }
  178. static int
  179. p_filter(Filter *f, Msg *m)
  180. {
  181. Hdr *h;
  182. int hlen;
  183. if(m->pe - m->ps < IP6HDR)
  184. return 0;
  185. h = (Hdr*)m->ps;
  186. if ((hlen = v6hdrlen(h)) < 0)
  187. return 0;
  188. else
  189. m->ps += hlen;
  190. switch(f->subop){
  191. case Os:
  192. return !memcmp(h->src, f->a, IPaddrlen);
  193. case Od:
  194. return !memcmp(h->dst, f->a, IPaddrlen);
  195. case Osd:
  196. return !memcmp(h->src, f->a, IPaddrlen) || !memcmp(h->dst, f->a, IPaddrlen);
  197. case Ot:
  198. return h->proto == f->ulv;
  199. }
  200. return 0;
  201. }
  202. static int
  203. v6hdr_seprint(Msg *m)
  204. {
  205. int len = IP6HDR;
  206. uchar *pkt = m->ps;
  207. Hdr *h = (Hdr *) pkt;
  208. int pktlen = IP6HDR + NetS(h->length);
  209. uchar nexthdr = h->proto;
  210. int plen;
  211. pkt += len;
  212. plen = len;
  213. while ( (nexthdr == HBH_HDR) || (nexthdr == ROUT_HDR) ||
  214. (nexthdr == FRAG_HDR) || (nexthdr == DEST_HDR) ) {
  215. switch (nexthdr) {
  216. case FRAG_HDR:
  217. m->p = seprint(m->p, m->e, "\n xthdr=frag id=%d offset=%d pr=%d more=%d res1=%d res2=%d",
  218. NetL(pkt+4),
  219. NetS(pkt+2) & ~7,
  220. (int) (*pkt),
  221. (int) (*(pkt+3) & 0x1),
  222. (int) *(pkt+1),
  223. (int) (*(pkt+3) & 0x6)
  224. );
  225. len = FRAG_HSZ;
  226. break;
  227. case HBH_HDR:
  228. case ROUT_HDR:
  229. case DEST_HDR:
  230. len = ( ((int) *(pkt+1)) + 1) * 8;
  231. break;
  232. }
  233. if (plen + len > pktlen) {
  234. m->p = seprint(m->p, m->e, "bad pkt");
  235. m->pr = &dump;
  236. return -1;
  237. }
  238. plen += len;
  239. pkt += len;
  240. nexthdr = *pkt;
  241. }
  242. m->ps = pkt;
  243. return 1;
  244. }
  245. static int
  246. p_seprint(Msg *m)
  247. {
  248. Hdr *h;
  249. int len;
  250. if(m->pe - m->ps < IP6HDR)
  251. return -1;
  252. h = (Hdr*)m->ps;
  253. demux(p_mux, h->proto, h->proto, m, &dump);
  254. /* truncate the message if there's extra */
  255. len = NetS(h->length) + IP6HDR;
  256. if(len < m->pe - m->ps)
  257. m->pe = m->ps + len;
  258. m->p = seprint(m->p, m->e, "s=%I d=%I ttl=%3d pr=%d ln=%d",
  259. h->src, h->dst,
  260. h->ttl,
  261. h->proto,
  262. NetS(h->length)
  263. );
  264. v6hdr_seprint(m);
  265. return 0;
  266. }
  267. Proto ip6 =
  268. {
  269. "ip6",
  270. p_compile,
  271. p_filter,
  272. p_seprint,
  273. p_mux,
  274. "%lud",
  275. p_fields,
  276. defaultframer,
  277. };