ip6.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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) == 0;
  193. case Od:
  194. return memcmp(h->dst, f->a, IPaddrlen) == 0;
  195. case Osd:
  196. return memcmp(h->src, f->a, IPaddrlen) == 0 ||
  197. memcmp(h->dst, f->a, IPaddrlen) == 0;
  198. case Ot:
  199. return h->proto == f->ulv;
  200. }
  201. return 0;
  202. }
  203. static int
  204. v6hdr_seprint(Msg *m)
  205. {
  206. int plen, len = IP6HDR;
  207. uchar *pkt = m->ps;
  208. Hdr *h = (Hdr *)pkt;
  209. int pktlen = IP6HDR + NetS(h->length);
  210. uchar nexthdr = h->proto;
  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 "
  218. "offset=%d pr=%d more=%d res1=%d res2=%d",
  219. NetL(pkt+4), NetS(pkt+2) & ~7, (int)*pkt,
  220. (int)(*(pkt+3) & 0x1), (int)*(pkt+1),
  221. (int)(*(pkt+3) & 0x6));
  222. len = FRAG_HSZ;
  223. break;
  224. case HBH_HDR:
  225. case ROUT_HDR:
  226. case DEST_HDR:
  227. len = ((int)*(pkt+1) + 1) * 8;
  228. break;
  229. }
  230. if (plen + len > pktlen) {
  231. m->p = seprint(m->p, m->e, "bad pkt");
  232. m->pr = &dump;
  233. return -1;
  234. }
  235. plen += len;
  236. pkt += len;
  237. nexthdr = *pkt;
  238. }
  239. m->ps = pkt;
  240. return 1;
  241. }
  242. static int
  243. p_seprint(Msg *m)
  244. {
  245. int len;
  246. Hdr *h;
  247. if(m->pe - m->ps < IP6HDR)
  248. return -1;
  249. h = (Hdr*)m->ps;
  250. demux(p_mux, h->proto, h->proto, m, &dump);
  251. /* truncate the message if there's extra */
  252. len = NetS(h->length) + IP6HDR;
  253. if(len < m->pe - m->ps)
  254. m->pe = m->ps + len;
  255. m->p = seprint(m->p, m->e, "s=%I d=%I ttl=%3d pr=%d ln=%d",
  256. h->src, h->dst, h->ttl, h->proto, NetS(h->length));
  257. v6hdr_seprint(m);
  258. return 0;
  259. }
  260. Proto ip6 =
  261. {
  262. "ip6",
  263. p_compile,
  264. p_filter,
  265. p_seprint,
  266. p_mux,
  267. "%lud",
  268. p_fields,
  269. defaultframer,
  270. };