1
0

ip.h 4.8 KB

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