ip6.c 5.6 KB

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