ip.h 4.3 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. u8 ip[IPaddrlen];
  32. u8 mask[IPaddrlen];
  33. u8 net[IPaddrlen]; /* ip & mask */
  34. u32 preflt; /* preferred lifetime */
  35. u32 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. u8 sendra6; /* on == send router adv */
  59. u8 recvra6; /* on == rcv router adv */
  60. int mtu;
  61. u32 pktin;
  62. u32 pktout;
  63. u32 errin;
  64. u32 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. u8 vcf[4]; /* version:4, traffic class:8, flow label:20 */
  114. u8 ploadlen[2]; /* payload length: packet length - 40 */
  115. u8 proto; /* next header type */
  116. u8 ttl; /* hop limit */
  117. u8 src[IPaddrlen]; /* source address */
  118. u8 dst[IPaddrlen]; /* destination address */
  119. u8 payload[];
  120. };
  121. /*
  122. * user-level icmpv6 with control message "headers"
  123. */
  124. typedef struct Icmp6hdr Icmp6hdr;
  125. struct Icmp6hdr {
  126. u8 _0_[8];
  127. u8 laddr[IPaddrlen]; /* local address */
  128. u8 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. u8 raddr[IPaddrlen]; /* V6 remote address */
  141. u8 laddr[IPaddrlen]; /* V6 local address */
  142. u8 ifcaddr[IPaddrlen]; /* V6 ifc addr msg was received on */
  143. u8 rport[2]; /* remote port */
  144. u8 lport[2]; /* local port */
  145. };
  146. u8 * defmask(u8*);
  147. void maskip(u8*, u8*, u8*);
  148. int eipfmt(Fmt*);
  149. int isv4(u8*);
  150. i64 parseip(u8*, char*);
  151. i64 parseipmask(u8*, char*);
  152. char* v4parseip(u8*, char*);
  153. char* v4parsecidr(u8*, u8*, char*);
  154. int parseether(u8*, char*);
  155. int myipaddr(u8*, char*);
  156. int myetheraddr(u8*, char*);
  157. int equivip4(u8*, u8*);
  158. int equivip6(u8*, u8*);
  159. Ipifc* readipifc(char*, Ipifc*, int);
  160. void hnputv(void*, u64);
  161. void hnputl(void*, u32);
  162. void hnputs(void*, u16);
  163. u64 nhgetv(void*);
  164. u32 nhgetl(void*);
  165. u16 nhgets(void*);
  166. u16 ptclbsum(u8*, int);
  167. int v6tov4(u8*, u8*);
  168. void v4tov6(u8*, u8*);
  169. #define ipcmp(x, y) memcmp(x, y, IPaddrlen)
  170. #define ipmove(x, y) memmove(x, y, IPaddrlen)
  171. extern u8 IPv4bcast[IPaddrlen];
  172. extern u8 IPv4bcastobs[IPaddrlen];
  173. extern u8 IPv4allsys[IPaddrlen];
  174. extern u8 IPv4allrouter[IPaddrlen];
  175. extern u8 IPnoaddr[IPaddrlen];
  176. extern u8 v4prefix[IPaddrlen];
  177. extern u8 IPallbits[IPaddrlen];
  178. #define CLASS(p) ((*(u8*)(p))>>6)