1
0

ip.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. enum
  10. {
  11. IPaddrlen= 16,
  12. IPv4addrlen= 4,
  13. IPv4off= 12,
  14. IPllen= 4,
  15. IPV4HDR_LEN= 20,
  16. /* vihl & vcf[0] values */
  17. IP_VER4= 0x40,
  18. IP_VER6= 0x60,
  19. };
  20. /*
  21. * for reading /net/ipifc
  22. */
  23. typedef struct Ipifc Ipifc;
  24. typedef struct Iplifc Iplifc;
  25. typedef struct Ipv6rp Ipv6rp;
  26. /* local address */
  27. struct Iplifc
  28. {
  29. Iplifc *next;
  30. /* per address on the ip interface */
  31. uint8_t ip[IPaddrlen];
  32. uint8_t mask[IPaddrlen];
  33. uint8_t net[IPaddrlen]; /* ip & mask */
  34. uint32_t preflt; /* preferred lifetime */
  35. uint32_t validlt; /* valid lifetime */
  36. };
  37. /* default values, one per stack */
  38. struct Ipv6rp
  39. {
  40. int mflag;
  41. int oflag;
  42. int maxraint;
  43. int minraint;
  44. int linkmtu;
  45. int reachtime;
  46. int rxmitra;
  47. int ttl;
  48. int routerlt;
  49. };
  50. /* actual interface */
  51. struct Ipifc
  52. {
  53. Ipifc *next;
  54. Iplifc *lifc;
  55. /* per ip interface */
  56. int index; /* number of interface in ipifc dir */
  57. char dev[64];
  58. uint8_t sendra6; /* on == send router adv */
  59. uint8_t recvra6; /* on == rcv router adv */
  60. int mtu;
  61. uint32_t pktin;
  62. uint32_t pktout;
  63. uint32_t errin;
  64. uint32_t errout;
  65. Ipv6rp rp;
  66. };
  67. #define ISIPV6MCAST(addr) ((addr)[0] == 0xff)
  68. #define ISIPV6LINKLOCAL(addr) ((addr)[0] == 0xfe && ((addr)[1] & 0xc0) == 0x80)
  69. /*
  70. * ipv6 constants
  71. * `ra' is `router advertisement', `rs' is `router solicitation'.
  72. * `na' is `neighbour advertisement'.
  73. */
  74. enum {
  75. IPV6HDR_LEN = 40,
  76. /* neighbour discovery option types */
  77. V6nd_srclladdr = 1,
  78. V6nd_targlladdr = 2,
  79. V6nd_pfxinfo = 3,
  80. V6nd_redirhdr = 4,
  81. V6nd_mtu = 5,
  82. /* new since rfc2461; see iana.org/assignments/icmpv6-parameters */
  83. V6nd_home = 8,
  84. V6nd_srcaddrs = 9, /* rfc3122 */
  85. V6nd_ip = 17,
  86. /* /lib/rfc/drafts/draft-jeong-dnsop-ipv6-dns-discovery-12.txt */
  87. V6nd_rdns = 25,
  88. /* plan 9 extensions */
  89. V6nd_9fs = 250,
  90. V6nd_9auth = 251,
  91. /* Router constants (all times in ms.) */
  92. Maxv6initraintvl= 16000,
  93. Maxv6initras = 3,
  94. Maxv6finalras = 3,
  95. Minv6interradelay= 3000,
  96. Maxv6radelay = 500,
  97. /* Host constants */
  98. Maxv6rsdelay = 1000,
  99. V6rsintvl = 4000,
  100. Maxv6rss = 3,
  101. /* Node constants */
  102. Maxv6mcastrss = 3,
  103. Maxv6unicastrss = 3,
  104. Maxv6anycastdelay= 1000,
  105. Maxv6na = 3,
  106. V6reachabletime = 30000,
  107. V6retranstimer = 1000,
  108. V6initprobedelay= 5000,
  109. };
  110. /* V6 header on the wire */
  111. typedef struct Ip6hdr Ip6hdr;
  112. struct Ip6hdr {
  113. uint8_t vcf[4]; /* version:4, traffic class:8, flow label:20 */
  114. uint8_t ploadlen[2]; /* payload length: packet length - 40 */
  115. uint8_t proto; /* next header type */
  116. uint8_t ttl; /* hop limit */
  117. uint8_t src[IPaddrlen]; /* source address */
  118. uint8_t dst[IPaddrlen]; /* destination address */
  119. uint8_t payload[];
  120. };
  121. /*
  122. * user-level icmpv6 with control message "headers"
  123. */
  124. typedef struct Icmp6hdr Icmp6hdr;
  125. struct Icmp6hdr {
  126. uint8_t _0_[8];
  127. uint8_t laddr[IPaddrlen]; /* local address */
  128. uint8_t raddr[IPaddrlen]; /* remote address */
  129. };
  130. /*
  131. * user level udp headers with control message "headers"
  132. */
  133. enum
  134. {
  135. Udphdrsize= 52, /* size of a Udphdr */
  136. };
  137. typedef struct Udphdr Udphdr;
  138. struct Udphdr
  139. {
  140. uint8_t raddr[IPaddrlen]; /* V6 remote address */
  141. uint8_t laddr[IPaddrlen]; /* V6 local address */
  142. uint8_t ifcaddr[IPaddrlen]; /* V6 ifc addr msg was received on */
  143. uint8_t rport[2]; /* remote port */
  144. uint8_t lport[2]; /* local port */
  145. };
  146. uint8_t* defmask(uint8_t*);
  147. void maskip(uint8_t*, uint8_t*, uint8_t*);
  148. int eipfmt(Fmt*);
  149. int isv4(uint8_t*);
  150. int64_t parseip(uint8_t*, char*);
  151. int64_t parseipmask(uint8_t*, char*);
  152. char* v4parseip(uint8_t*, char*);
  153. char* v4parsecidr(uint8_t*, uint8_t*, char*);
  154. int parseether(uint8_t*, char*);
  155. int myipaddr(uint8_t*, char*);
  156. int myetheraddr(uint8_t*, char*);
  157. int equivip4(uint8_t*, uint8_t*);
  158. int equivip6(uint8_t*, uint8_t*);
  159. Ipifc* readipifc(char*, Ipifc*, int);
  160. void hnputv(void*, uint64_t);
  161. void hnputl(void*, uint);
  162. void hnputs(void*, uint16_t);
  163. uint64_t nhgetv(void*);
  164. uint nhgetl(void*);
  165. uint16_t nhgets(void*);
  166. uint16_t ptclbsum(uint8_t*, int);
  167. int v6tov4(uint8_t*, uint8_t*);
  168. void v4tov6(uint8_t*, uint8_t*);
  169. #define ipcmp(x, y) memcmp(x, y, IPaddrlen)
  170. #define ipmove(x, y) memmove(x, y, IPaddrlen)
  171. extern uint8_t IPv4bcast[IPaddrlen];
  172. extern uint8_t IPv4bcastobs[IPaddrlen];
  173. extern uint8_t IPv4allsys[IPaddrlen];
  174. extern uint8_t IPv4allrouter[IPaddrlen];
  175. extern uint8_t IPnoaddr[IPaddrlen];
  176. extern uint8_t v4prefix[IPaddrlen];
  177. extern uint8_t IPallbits[IPaddrlen];
  178. #define CLASS(p) ((*(uint8_t*)(p))>>6)