user_inpcb.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. /*-
  2. * Copyright (c) 1982, 1986, 1990, 1993
  3. * The Regents of the University of California. All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. Neither the name of the University nor the names of its contributors
  14. * may be used to endorse or promote products derived from this software
  15. * without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  18. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  21. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  23. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  24. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  26. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27. * SUCH DAMAGE.
  28. *
  29. * @(#)in_pcb.h 8.1 (Berkeley) 6/10/93
  30. * $FreeBSD: src/sys/netinet/in_pcb.h,v 1.100.2.1 2007/12/07 05:46:08 kmacy Exp $
  31. */
  32. #ifndef _USER_INPCB_H_
  33. #define _USER_INPCB_H_
  34. #include <user_route.h> /* was <net/route.h> */
  35. struct inpcbpolicy;
  36. /*
  37. * Struct inpcb is the ommon structure pcb for the Internet Protocol
  38. * implementation.
  39. *
  40. * Pointers to local and foreign host table entries, local and foreign socket
  41. * numbers, and pointers up (to a socket structure) and down (to a
  42. * protocol-specific control block) are stored here.
  43. */
  44. LIST_HEAD(inpcbhead, inpcb);
  45. LIST_HEAD(inpcbporthead, inpcbport);
  46. /*
  47. * PCB with AF_INET6 null bind'ed laddr can receive AF_INET input packet.
  48. * So, AF_INET6 null laddr is also used as AF_INET null laddr, by utilizing
  49. * the following structure.
  50. */
  51. struct in_addr_4in6 {
  52. uint32_t ia46_pad32[3];
  53. struct in_addr ia46_addr4;
  54. };
  55. /*
  56. * NOTE: ipv6 addrs should be 64-bit aligned, per RFC 2553. in_conninfo has
  57. * some extra padding to accomplish this.
  58. */
  59. struct in_endpoints {
  60. uint16_t ie_fport; /* foreign port */
  61. uint16_t ie_lport; /* local port */
  62. /* protocol dependent part, local and foreign addr */
  63. union {
  64. /* foreign host table entry */
  65. struct in_addr_4in6 ie46_foreign;
  66. struct in6_addr ie6_foreign;
  67. } ie_dependfaddr;
  68. union {
  69. /* local host table entry */
  70. struct in_addr_4in6 ie46_local;
  71. struct in6_addr ie6_local;
  72. } ie_dependladdr;
  73. #define ie_faddr ie_dependfaddr.ie46_foreign.ia46_addr4
  74. #define ie_laddr ie_dependladdr.ie46_local.ia46_addr4
  75. #define ie6_faddr ie_dependfaddr.ie6_foreign
  76. #define ie6_laddr ie_dependladdr.ie6_local
  77. };
  78. /*
  79. * XXX The defines for inc_* are hacks and should be changed to direct
  80. * references.
  81. */
  82. struct in_conninfo {
  83. uint8_t inc_flags;
  84. uint8_t inc_len;
  85. uint16_t inc_pad; /* XXX alignment for in_endpoints */
  86. /* protocol dependent part */
  87. struct in_endpoints inc_ie;
  88. };
  89. #define inc_isipv6 inc_flags /* temp compatibility */
  90. #define inc_fport inc_ie.ie_fport
  91. #define inc_lport inc_ie.ie_lport
  92. #define inc_faddr inc_ie.ie_faddr
  93. #define inc_laddr inc_ie.ie_laddr
  94. #define inc6_faddr inc_ie.ie6_faddr
  95. #define inc6_laddr inc_ie.ie6_laddr
  96. struct icmp6_filter;
  97. struct inpcb {
  98. LIST_ENTRY(inpcb) inp_hash; /* hash list */
  99. LIST_ENTRY(inpcb) inp_list; /* list for all PCBs of this proto */
  100. void *inp_ppcb; /* pointer to per-protocol pcb */
  101. struct inpcbinfo *inp_pcbinfo; /* PCB list info */
  102. struct socket *inp_socket; /* back pointer to socket */
  103. uint32_t inp_flow;
  104. int inp_flags; /* generic IP/datagram flags */
  105. u_char inp_vflag; /* IP version flag (v4/v6) */
  106. #define INP_IPV4 0x1
  107. #define INP_IPV6 0x2
  108. #define INP_IPV6PROTO 0x4 /* opened under IPv6 protocol */
  109. #define INP_TIMEWAIT 0x8 /* .. probably doesn't go here */
  110. #define INP_ONESBCAST 0x10 /* send all-ones broadcast */
  111. #define INP_DROPPED 0x20 /* protocol drop flag */
  112. #define INP_SOCKREF 0x40 /* strong socket reference */
  113. #define INP_CONN 0x80
  114. u_char inp_ip_ttl; /* time to live proto */
  115. u_char inp_ip_p; /* protocol proto */
  116. u_char inp_ip_minttl; /* minimum TTL or drop */
  117. uint32_t inp_ispare1; /* connection id / queue id */
  118. void *inp_pspare[2]; /* rtentry / general use */
  119. /* Local and foreign ports, local and foreign addr. */
  120. struct in_conninfo inp_inc;
  121. /* list for this PCB's local port */
  122. struct label *inp_label; /* MAC label */
  123. struct inpcbpolicy *inp_sp; /* for IPSEC */
  124. /* Protocol-dependent part; options. */
  125. struct {
  126. u_char inp4_ip_tos; /* type of service proto */
  127. struct mbuf *inp4_options; /* IP options */
  128. struct ip_moptions *inp4_moptions; /* IP multicast options */
  129. } inp_depend4;
  130. #define inp_fport inp_inc.inc_fport
  131. #define inp_lport inp_inc.inc_lport
  132. #define inp_faddr inp_inc.inc_faddr
  133. #define inp_laddr inp_inc.inc_laddr
  134. #define inp_ip_tos inp_depend4.inp4_ip_tos
  135. #define inp_options inp_depend4.inp4_options
  136. #define inp_moptions inp_depend4.inp4_moptions
  137. struct {
  138. /* IP options */
  139. struct mbuf *inp6_options;
  140. /* IP6 options for outgoing packets */
  141. struct ip6_pktopts *inp6_outputopts;
  142. /* IP multicast options */
  143. #if 0
  144. struct ip6_moptions *inp6_moptions;
  145. #endif
  146. /* ICMPv6 code type filter */
  147. struct icmp6_filter *inp6_icmp6filt;
  148. /* IPV6_CHECKSUM setsockopt */
  149. int inp6_cksum;
  150. short inp6_hops;
  151. } inp_depend6;
  152. LIST_ENTRY(inpcb) inp_portlist;
  153. struct inpcbport *inp_phd; /* head of this list */
  154. #define inp_zero_size offsetof(struct inpcb, inp_gencnt)
  155. struct mtx inp_mtx;
  156. #define in6p_faddr inp_inc.inc6_faddr
  157. #define in6p_laddr inp_inc.inc6_laddr
  158. #define in6p_hops inp_depend6.inp6_hops /* default hop limit */
  159. #define in6p_ip6_nxt inp_ip_p
  160. #define in6p_flowinfo inp_flow
  161. #define in6p_vflag inp_vflag
  162. #define in6p_options inp_depend6.inp6_options
  163. #define in6p_outputopts inp_depend6.inp6_outputopts
  164. #if 0
  165. #define in6p_moptions inp_depend6.inp6_moptions
  166. #endif
  167. #define in6p_icmp6filt inp_depend6.inp6_icmp6filt
  168. #define in6p_cksum inp_depend6.inp6_cksum
  169. #define in6p_flags inp_flags /* for KAME src sync over BSD*'s */
  170. #define in6p_socket inp_socket /* for KAME src sync over BSD*'s */
  171. #define in6p_lport inp_lport /* for KAME src sync over BSD*'s */
  172. #define in6p_fport inp_fport /* for KAME src sync over BSD*'s */
  173. #define in6p_ppcb inp_ppcb /* for KAME src sync over BSD*'s */
  174. };
  175. /*
  176. * The range of the generation count, as used in this implementation, is 9e19.
  177. * We would have to create 300 billion connections per second for this number
  178. * to roll over in a year. This seems sufficiently unlikely that we simply
  179. * don't concern ourselves with that possibility.
  180. */
  181. struct inpcbport {
  182. LIST_ENTRY(inpcbport) phd_hash;
  183. struct inpcbhead phd_pcblist;
  184. u_short phd_port;
  185. };
  186. /*
  187. * Global data structure for each high-level protocol (UDP, TCP, ...) in both
  188. * IPv4 and IPv6. Holds inpcb lists and information for managing them.
  189. */
  190. struct inpcbinfo {
  191. /*
  192. * Global list of inpcbs on the protocol.
  193. */
  194. struct inpcbhead *ipi_listhead;
  195. u_int ipi_count;
  196. /*
  197. * Global hash of inpcbs, hashed by local and foreign addresses and
  198. * port numbers.
  199. */
  200. struct inpcbhead *ipi_hashbase;
  201. u_long ipi_hashmask;
  202. /*
  203. * Global hash of inpcbs, hashed by only local port number.
  204. */
  205. struct inpcbporthead *ipi_porthashbase;
  206. u_long ipi_porthashmask;
  207. /*
  208. * Fields associated with port lookup and allocation.
  209. */
  210. u_short ipi_lastport;
  211. u_short ipi_lastlow;
  212. u_short ipi_lasthi;
  213. /*
  214. * UMA zone from which inpcbs are allocated for this protocol.
  215. */
  216. struct uma_zone *ipi_zone;
  217. /*
  218. * Generation count--incremented each time a connection is allocated
  219. * or freed.
  220. */
  221. struct mtx ipi_mtx;
  222. /*
  223. * vimage 1
  224. * general use 1
  225. */
  226. void *ipi_pspare[2];
  227. };
  228. #define INP_LOCK_INIT(inp, d, t) \
  229. mtx_init(&(inp)->inp_mtx, (d), (t), MTX_DEF | MTX_RECURSE | MTX_DUPOK)
  230. #define INP_LOCK_DESTROY(inp) mtx_destroy(&(inp)->inp_mtx)
  231. #define INP_LOCK(inp) mtx_lock(&(inp)->inp_mtx)
  232. #define INP_UNLOCK(inp) mtx_unlock(&(inp)->inp_mtx)
  233. #define INP_LOCK_ASSERT(inp) mtx_assert(&(inp)->inp_mtx, MA_OWNED)
  234. #define INP_UNLOCK_ASSERT(inp) mtx_assert(&(inp)->inp_mtx, MA_NOTOWNED)
  235. #define INP_INFO_LOCK_INIT(ipi, d) \
  236. mtx_init(&(ipi)->ipi_mtx, (d), NULL, MTX_DEF | MTX_RECURSE)
  237. #define INP_INFO_LOCK_DESTROY(ipi) mtx_destroy(&(ipi)->ipi_mtx)
  238. #define INP_INFO_RLOCK(ipi) mtx_lock(&(ipi)->ipi_mtx)
  239. #define INP_INFO_WLOCK(ipi) mtx_lock(&(ipi)->ipi_mtx)
  240. #define INP_INFO_RUNLOCK(ipi) mtx_unlock(&(ipi)->ipi_mtx)
  241. #define INP_INFO_WUNLOCK(ipi) mtx_unlock(&(ipi)->ipi_mtx)
  242. #define INP_INFO_RLOCK_ASSERT(ipi) mtx_assert(&(ipi)->ipi_mtx, MA_OWNED)
  243. #define INP_INFO_WLOCK_ASSERT(ipi) mtx_assert(&(ipi)->ipi_mtx, MA_OWNED)
  244. #define INP_INFO_UNLOCK_ASSERT(ipi) mtx_assert(&(ipi)->ipi_mtx, MA_NOTOWNED)
  245. #define INP_PCBHASH(faddr, lport, fport, mask) \
  246. (((faddr) ^ ((faddr) >> 16) ^ ntohs((lport) ^ (fport))) & (mask))
  247. #define INP_PCBPORTHASH(lport, mask) \
  248. (ntohs((lport)) & (mask))
  249. /* flags in inp_flags: */
  250. #define INP_RECVOPTS 0x01 /* receive incoming IP options */
  251. #define INP_RECVRETOPTS 0x02 /* receive IP options for reply */
  252. #define INP_RECVDSTADDR 0x04 /* receive IP dst address */
  253. #define INP_HDRINCL 0x08 /* user supplies entire IP header */
  254. #define INP_HIGHPORT 0x10 /* user wants "high" port binding */
  255. #define INP_LOWPORT 0x20 /* user wants "low" port binding */
  256. #define INP_ANONPORT 0x40 /* port chosen for user */
  257. #define INP_RECVIF 0x80 /* receive incoming interface */
  258. #define INP_MTUDISC 0x100 /* user can do MTU discovery */
  259. #define INP_FAITH 0x200 /* accept FAITH'ed connections */
  260. #define INP_RECVTTL 0x400 /* receive incoming IP TTL */
  261. #define INP_DONTFRAG 0x800 /* don't fragment packet */
  262. #define IN6P_IPV6_V6ONLY 0x008000 /* restrict AF_INET6 socket for v6 */
  263. #define IN6P_PKTINFO 0x010000 /* receive IP6 dst and I/F */
  264. #define IN6P_HOPLIMIT 0x020000 /* receive hoplimit */
  265. #define IN6P_HOPOPTS 0x040000 /* receive hop-by-hop options */
  266. #define IN6P_DSTOPTS 0x080000 /* receive dst options after rthdr */
  267. #define IN6P_RTHDR 0x100000 /* receive routing header */
  268. #define IN6P_RTHDRDSTOPTS 0x200000 /* receive dstoptions before rthdr */
  269. #define IN6P_TCLASS 0x400000 /* receive traffic class value */
  270. #define IN6P_AUTOFLOWLABEL 0x800000 /* attach flowlabel automatically */
  271. #define IN6P_RFC2292 0x40000000 /* used RFC2292 API on the socket */
  272. #define IN6P_MTU 0x80000000 /* receive path MTU */
  273. #define INP_CONTROLOPTS (INP_RECVOPTS|INP_RECVRETOPTS|INP_RECVDSTADDR|\
  274. INP_RECVIF|INP_RECVTTL|\
  275. IN6P_PKTINFO|IN6P_HOPLIMIT|IN6P_HOPOPTS|\
  276. IN6P_DSTOPTS|IN6P_RTHDR|IN6P_RTHDRDSTOPTS|\
  277. IN6P_TCLASS|IN6P_AUTOFLOWLABEL|IN6P_RFC2292|\
  278. IN6P_MTU)
  279. #define INP_UNMAPPABLEOPTS (IN6P_HOPOPTS|IN6P_DSTOPTS|IN6P_RTHDR|\
  280. IN6P_TCLASS|IN6P_AUTOFLOWLABEL)
  281. /* for KAME src sync over BSD*'s */
  282. #define IN6P_HIGHPORT INP_HIGHPORT
  283. #define IN6P_LOWPORT INP_LOWPORT
  284. #define IN6P_ANONPORT INP_ANONPORT
  285. #define IN6P_RECVIF INP_RECVIF
  286. #define IN6P_MTUDISC INP_MTUDISC
  287. #define IN6P_FAITH INP_FAITH
  288. #define IN6P_CONTROLOPTS INP_CONTROLOPTS
  289. /*
  290. * socket AF version is {newer than,or include}
  291. * actual datagram AF version
  292. */
  293. #define INPLOOKUP_WILDCARD 1
  294. #define sotoinpcb(so) ((struct inpcb *)(so)->so_pcb)
  295. #define INP_SOCKAF(so) so->so_proto->pr_domain->dom_family
  296. #define INP_CHECK_SOCKAF(so, af) (INP_SOCKAF(so) == af)
  297. extern int ipport_reservedhigh;
  298. extern int ipport_reservedlow;
  299. extern int ipport_lowfirstauto;
  300. extern int ipport_lowlastauto;
  301. extern int ipport_firstauto;
  302. extern int ipport_lastauto;
  303. extern int ipport_hifirstauto;
  304. extern int ipport_hilastauto;
  305. extern struct callout ipport_tick_callout;
  306. void in_pcbpurgeif0(struct inpcbinfo *, struct ifnet *);
  307. int in_pcballoc(struct socket *, struct inpcbinfo *);
  308. int in_pcbbind(struct inpcb *, struct sockaddr *, struct ucred *);
  309. int in_pcbconnect(struct inpcb *, struct sockaddr *, struct ucred *);
  310. void in_pcbdetach(struct inpcb *);
  311. void in_pcbdisconnect(struct inpcb *);
  312. void in_pcbdrop(struct inpcb *);
  313. void in_pcbfree(struct inpcb *);
  314. int in_pcbinshash(struct inpcb *);
  315. struct inpcb *
  316. in_pcblookup_local(struct inpcbinfo *,
  317. struct in_addr, u_int, int);
  318. struct inpcb *
  319. in_pcblookup_hash(struct inpcbinfo *, struct in_addr, u_int,
  320. struct in_addr, u_int, int, struct ifnet *);
  321. void in_pcbnotifyall(struct inpcbinfo *pcbinfo, struct in_addr,
  322. int, struct inpcb *(*)(struct inpcb *, int));
  323. void in_pcbrehash(struct inpcb *);
  324. void in_pcbsetsolabel(struct socket *so);
  325. int in_getpeeraddr(struct socket *so, struct sockaddr **nam);
  326. int in_getsockaddr(struct socket *so, struct sockaddr **nam);
  327. void in_pcbsosetlabel(struct socket *so);
  328. void in_pcbremlists(struct inpcb *inp);
  329. void ipport_tick(void *xtp);
  330. /*
  331. * Debugging routines compiled in when DDB is present.
  332. */
  333. void db_print_inpcb(struct inpcb *inp, const char *name, int indent);
  334. #endif /* !_NETINET_IN_PCB_H_ */