pipex_local.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. /* $OpenBSD: pipex_local.h,v 1.22 2015/04/23 09:45:24 dlg Exp $ */
  2. /*
  3. * Copyright (c) 2009 Internet Initiative Japan Inc.
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  16. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  18. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  21. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  22. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  23. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  24. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  25. * SUCH DAMAGE.
  26. */
  27. #ifdef __OpenBSD__
  28. #define Static
  29. #else
  30. #define Static static
  31. #endif
  32. #define PIPEX_PPTP 1
  33. #define PIPEX_L2TP 1
  34. #define PIPEX_PPPOE 1
  35. #define PIPEX_MPPE 1
  36. #define PIPEX_REWIND_LIMIT 64
  37. #define PIPEX_ENABLED 0x0001
  38. /* compile time option constants */
  39. #ifndef PIPEX_MAX_SESSION
  40. #define PIPEX_MAX_SESSION 512
  41. #endif
  42. #define PIPEX_HASH_DIV 8
  43. #define PIPEX_HASH_SIZE (PIPEX_MAX_SESSION/PIPEX_HASH_DIV)
  44. #define PIPEX_HASH_MASK (PIPEX_HASH_SIZE-1)
  45. #define PIPEX_CLOSE_TIMEOUT 30
  46. #define PIPEX_PPPMINLEN 5
  47. /* minimum PPP header length is 1 and minimum ppp payload length is 4 */
  48. #ifndef NNBY /* usually defined on the <sys/types.h> */
  49. #define NNBY 8 /* number of bits of a byte */
  50. #endif
  51. #define PIPEX_MPPE_NOLDKEY 64 /* should be power of two */
  52. #define PIPEX_MPPE_OLDKEYMASK (PIPEX_MPPE_NOLDKEY - 1)
  53. #ifdef PIPEX_MPPE
  54. /* mppe rc4 key */
  55. struct pipex_mppe {
  56. int16_t stateless:1, /* key change mode */
  57. resetreq:1,
  58. reserved:14;
  59. int16_t keylenbits; /* key length */
  60. int16_t keylen;
  61. uint16_t coher_cnt; /* cohency counter */
  62. struct rc4_ctx rc4ctx;
  63. u_char master_key[PIPEX_MPPE_KEYLEN]; /* master key of MPPE */
  64. u_char session_key[PIPEX_MPPE_KEYLEN]; /* session key of MPPE */
  65. u_char (*old_session_keys)[PIPEX_MPPE_KEYLEN]; /* old session keys */
  66. };
  67. #endif /* PIPEX_MPPE */
  68. #ifdef PIPEX_PPPOE
  69. struct pipex_pppoe_session {
  70. struct ifnet *over_ifp; /* ether interface */
  71. };
  72. #endif /* PIPEX_PPPOE */
  73. #ifdef PIPEX_PPTP
  74. struct pipex_pptp_session {
  75. /* sequence number gap between pipex and userland */
  76. int32_t snd_gap; /* gap of our sequence */
  77. int32_t rcv_gap; /* gap of peer's sequence */
  78. int32_t ul_snd_una; /* userland send acked seq */
  79. uint32_t snd_nxt; /* send next */
  80. uint32_t rcv_nxt; /* receive next */
  81. uint32_t snd_una; /* send acked sequence */
  82. uint32_t rcv_acked; /* recv acked sequence */
  83. int winsz; /* windows size */
  84. int maxwinsz; /* max windows size */
  85. int peer_maxwinsz; /* peer's max windows size */
  86. };
  87. #endif /* PIPEX_PPTP */
  88. #ifdef PIPEX_L2TP
  89. /*
  90. * L2TP Packet headers
  91. *
  92. * +----+---+----+---+----+--------+
  93. * |IPv4|UDP|L2TP|PPP|IPv4|Data....|
  94. * +----+---+----+---+----+--------+
  95. *
  96. * Session Data
  97. *
  98. * IPv4 IP_SRC <-- required for encap.
  99. * IP_DST <-- required for encap.
  100. *
  101. * UDP SPort <-- required for encap.
  102. * DPort <-- required for encap.
  103. *
  104. * L2TP FLAGS <-- only handle TYPE=0 (data)
  105. * Tunnel ID <-- ID per tunnel(NOT a key: differed from RFC)
  106. * Session ID <-- ID per PPP session(KEY to look up session)
  107. * Ns(SEND SEQ) <-- sequence number of packet to send(opt.)
  108. * Nr(RECV SEQ) <-- sequence number of packet to recv(opt.)
  109. *
  110. * - Recv Session lookup key is (Tunnnel ID, Session ID) in RFC.
  111. * - BUT (Session ID) in PIPEX. SESSION ID MUST BE UNIQ.
  112. *
  113. * - We must update (Ns, Nr) of data channel. and we must adjust (Ns, Nr)
  114. * in packets from/to userland.
  115. */
  116. struct pipex_l2tp_session {
  117. /* KEYS for session lookup (host byte order) */
  118. uint16_t tunnel_id; /* our tunnel-id */
  119. uint16_t peer_tunnel_id; /* peer's tunnel-id */
  120. /* protocol options */
  121. uint32_t option_flags;
  122. int16_t ns_gap; /* gap between userland and pipex */
  123. int16_t nr_gap; /* gap between userland and pipex */
  124. uint16_t ul_ns_una; /* unacked sequence number (userland) */
  125. uint16_t ns_nxt; /* next sequence number to send */
  126. uint16_t ns_una; /* unacked sequence number to send*/
  127. uint16_t nr_nxt; /* next sequence number to recv */
  128. uint16_t nr_acked; /* acked sequence number to recv */
  129. uint32_t ipsecflowinfo; /* IPsec SA flow id for NAT-T */
  130. };
  131. #endif /* PIPEX_L2TP */
  132. /* pppac ip-extension sessoin table */
  133. struct pipex_session {
  134. struct radix_node ps4_rn[2]; /* tree glue, and other values */
  135. struct radix_node ps6_rn[2]; /* tree glue, and other values */
  136. LIST_ENTRY(pipex_session) session_list; /* all session chain */
  137. LIST_ENTRY(pipex_session) state_list; /* state list chain */
  138. LIST_ENTRY(pipex_session) id_chain; /* id hash chain */
  139. LIST_ENTRY(pipex_session) peer_addr_chain;
  140. /* peer's address hash chain */
  141. uint16_t state; /* pipex session state */
  142. #define PIPEX_STATE_INITIAL 0x0000
  143. #define PIPEX_STATE_OPENED 0x0001
  144. #define PIPEX_STATE_CLOSE_WAIT 0x0002
  145. #define PIPEX_STATE_CLOSE_WAIT2 0x0003
  146. #define PIPEX_STATE_CLOSED 0x0004
  147. uint16_t ip_forward:1, /* {en|dis}ableIP forwarding */
  148. ip6_forward:1, /* {en|dis}able IPv6 forwarding */
  149. is_multicast:1, /* virtual entry for mutlicast */
  150. reserved:13;
  151. uint16_t protocol; /* tunnel protocol (PK) */
  152. uint16_t session_id; /* session-id (PK) */
  153. uint16_t peer_session_id; /* peer's session-id */
  154. uint16_t peer_mru; /* peer's MRU */
  155. uint32_t timeout_sec; /* idle timeout */
  156. int ppp_id; /* PPP id */
  157. struct sockaddr_in ip_address; /* remote address (AK) */
  158. struct sockaddr_in ip_netmask; /* remote address mask (AK) */
  159. struct sockaddr_in6 ip6_address; /* remote IPv6 address */
  160. int ip6_prefixlen; /* remote IPv6 prefixlen */
  161. struct pipex_iface_context* pipex_iface;/* context for interface */
  162. uint32_t ppp_flags; /* configure flags */
  163. #ifdef PIPEX_MPPE
  164. int ccp_id; /* CCP packet id */
  165. struct pipex_mppe
  166. mppe_recv, /* MPPE context for incoming */
  167. mppe_send; /* MPPE context for outgoing */
  168. #endif /*PIPEXMPPE */
  169. struct pipex_statistics stat; /* statistics */
  170. union {
  171. #ifdef PIPEX_PPPOE
  172. struct pipex_pppoe_session pppoe; /* context for PPPoE */
  173. #endif /* PIPEX_PPPOE */
  174. #ifdef PIPEX_PPTP
  175. struct pipex_pptp_session pptp; /* context for PPTP */
  176. #endif /* PIPEX_PPTP */
  177. #ifdef PIPEX_L2TP
  178. struct pipex_l2tp_session l2tp;
  179. #endif
  180. char _proto_unknown[0];
  181. } proto;
  182. union {
  183. struct sockaddr_in sin4;
  184. struct sockaddr_in6 sin6;
  185. struct sockaddr_dl sdl;
  186. } peer, local;
  187. };
  188. /* gre header */
  189. struct pipex_gre_header {
  190. uint16_t flags; /* flags and version*/
  191. #define PIPEX_GRE_KFLAG 0x2000 /* keys present */
  192. #define PIPEX_GRE_SFLAG 0x1000 /* seq present */
  193. #define PIPEX_GRE_AFLAG 0x0080 /* ack present */
  194. #define PIPEX_GRE_VER 0x0001 /* gre version code */
  195. #define PIPEX_GRE_VERMASK 0x0007 /* gre version mask */
  196. #define PIPEX_GRE_UNUSEDFLAGS 0xcf78 /* unused at pptp. set 0 in rfc2637 */
  197. uint16_t type;
  198. #define PIPEX_GRE_PROTO_PPP 0x880b /* gre/ppp */
  199. uint16_t len; /* length not include gre header */
  200. uint16_t call_id; /* call_id */
  201. } __packed;
  202. /* pppoe header */
  203. struct pipex_pppoe_header {
  204. uint8_t vertype; /* version and type */
  205. #define PIPEX_PPPOE_VERTYPE 0x11 /* version and type code */
  206. uint8_t code; /* code */
  207. #define PIPEX_PPPOE_CODE_SESSION 0x00 /* code session */
  208. uint16_t session_id; /* session id */
  209. uint16_t length; /* length */
  210. } __packed;
  211. /* l2tp header */
  212. struct pipex_l2tp_header {
  213. uint16_t flagsver;
  214. #define PIPEX_L2TP_FLAG_MASK 0xfff0
  215. #define PIPEX_L2TP_FLAG_TYPE 0x8000
  216. #define PIPEX_L2TP_FLAG_LENGTH 0x4000
  217. #define PIPEX_L2TP_FLAG_SEQUENCE 0x0800
  218. #define PIPEX_L2TP_FLAG_OFFSET 0x0200
  219. #define PIPEX_L2TP_FLAG_PRIORITY 0x0100
  220. #define PIPEX_L2TP_VER_MASK 0x000f
  221. #define PIPEX_L2TP_VER 2
  222. uint16_t length; /* optional */
  223. uint16_t tunnel_id;
  224. uint16_t session_id;
  225. /* can be followed by option header */
  226. } __packed;
  227. /* l2tp option header */
  228. struct pipex_l2tp_seq_header {
  229. uint16_t ns;
  230. uint16_t nr;
  231. } __packed;
  232. struct pipex_l2tp_offset_header {
  233. uint16_t offset_size;
  234. /* uint8_t offset_pad[] */
  235. } __packed;
  236. #ifdef PIPEX_DEBUG
  237. #define PIPEX_DBG(a) if (pipex_debug & 1) pipex_session_log a
  238. /* #define PIPEX_MPPE_DBG(a) if (pipex_debug & 1) pipex_session_log a */
  239. #define PIPEX_MPPE_DBG(a)
  240. #else
  241. #define PIPEX_DBG(a)
  242. #define PIPEX_MPPE_DBG(a)
  243. #endif /* PIPEX_DEBUG */
  244. LIST_HEAD(pipex_hash_head, pipex_session);
  245. extern struct pipex_hash_head pipex_session_list;
  246. extern struct pipex_hash_head pipex_close_wait_list;
  247. extern struct pipex_hash_head pipex_peer_addr_hashtable[];
  248. extern struct pipex_hash_head pipex_id_hashtable[];
  249. #define PIPEX_ID_HASHTABLE(key) \
  250. (&pipex_id_hashtable[(key) & PIPEX_HASH_MASK])
  251. #define PIPEX_PEER_ADDR_HASHTABLE(key) \
  252. (&pipex_peer_addr_hashtable[(key) & PIPEX_HASH_MASK])
  253. #define GETCHAR(c, cp) do { \
  254. (c) = *(cp)++; \
  255. } while (0)
  256. #define PUTCHAR(s, cp) do { \
  257. *(cp)++ = (u_char)(s); \
  258. } while (0)
  259. #define GETSHORT(s, cp) do { \
  260. (s) = *(cp)++ << 8; \
  261. (s) |= *(cp)++; \
  262. } while (0)
  263. #define PUTSHORT(s, cp) do { \
  264. *(cp)++ = (u_char) ((s) >> 8); \
  265. *(cp)++ = (u_char) (s); \
  266. } while (0)
  267. #define GETLONG(l, cp) do { \
  268. (l) = *(cp)++ << 8; \
  269. (l) |= *(cp)++; (l) <<= 8; \
  270. (l) |= *(cp)++; (l) <<= 8; \
  271. (l) |= *(cp)++; \
  272. } while (0)
  273. #define PUTLONG(l, cp) do { \
  274. *(cp)++ = (u_char) ((l) >> 24); \
  275. *(cp)++ = (u_char) ((l) >> 16); \
  276. *(cp)++ = (u_char) ((l) >> 8); \
  277. *(cp)++ = (u_char) (l); \
  278. } while (0)
  279. #define PIPEX_PULLUP(m0, l) \
  280. if ((m0)->m_len < (l)) { \
  281. if ((m0)->m_pkthdr.len < (l)) { \
  282. PIPEX_DBG((NULL, LOG_DEBUG, \
  283. "<%s> received packet is too short.", \
  284. __func__)); \
  285. m_freem(m0); \
  286. (m0) = NULL; \
  287. } else { \
  288. (m0) = m_pullup((m0), (l)); \
  289. KASSERT((m0) != NULL); \
  290. } \
  291. }
  292. #define PIPEX_SEEK_NEXTHDR(ptr, len, t) \
  293. ((t) (((char *)ptr) + len))
  294. #define SEQ32_LT(a,b) ((int)((a) - (b)) < 0)
  295. #define SEQ32_LE(a,b) ((int)((a) - (b)) <= 0)
  296. #define SEQ32_GT(a,b) ((int)((a) - (b)) > 0)
  297. #define SEQ32_GE(a,b) ((int)((a) - (b)) >= 0)
  298. #define SEQ32_SUB(a,b) ((int32_t)((a) - (b)))
  299. #define SEQ16_LT(a,b) ((int)((a) - (b)) < 0)
  300. #define SEQ16_LE(a,b) ((int)((a) - (b)) <= 0)
  301. #define SEQ16_GT(a,b) ((int)((a) - (b)) > 0)
  302. #define SEQ16_GE(a,b) ((int)((a) - (b)) >= 0)
  303. #define SEQ16_SUB(a,b) ((int16_t)((a) - (b)))
  304. #define pipex_session_is_acfc_accepted(s) \
  305. (((s)->ppp_flags & PIPEX_PPP_ACFC_ACCEPTED)? 1 : 0)
  306. #define pipex_session_is_pfc_accepted(s) \
  307. (((s)->ppp_flags & PIPEX_PPP_PFC_ACCEPTED)? 1 : 0)
  308. #define pipex_session_is_acfc_enabled(s) \
  309. (((s)->ppp_flags & PIPEX_PPP_ACFC_ENABLED)? 1 : 0)
  310. #define pipex_session_is_pfc_enabled(s) \
  311. (((s)->ppp_flags & PIPEX_PPP_PFC_ENABLED)? 1 : 0)
  312. #define pipex_session_has_acf(s) \
  313. (((s)->ppp_flags & PIPEX_PPP_HAS_ACF)? 1 : 0)
  314. #define pipex_session_is_mppe_accepted(s) \
  315. (((s)->ppp_flags & PIPEX_PPP_MPPE_ACCEPTED)? 1 : 0)
  316. #define pipex_session_is_mppe_enabled(s) \
  317. (((s)->ppp_flags & PIPEX_PPP_MPPE_ENABLED)? 1 : 0)
  318. #define pipex_session_is_mppe_required(s) \
  319. (((s)->ppp_flags & PIPEX_PPP_MPPE_REQUIRED)? 1 : 0)
  320. #define pipex_mppe_rc4_keybits(r) ((r)->keylen << 3)
  321. #define pipex_session_is_l2tp_data_sequencing_on(s) \
  322. (((s)->proto.l2tp.option_flags & PIPEX_L2TP_USE_SEQUENCING) ? 1 : 0)
  323. #define PIPEX_IPGRE_HDRLEN (sizeof(struct ip) + sizeof(struct pipex_gre_header))
  324. #define PIPEX_TCP_OPTLEN 40
  325. #define PIPEX_L2TP_MINLEN 8
  326. /*
  327. * static function prototypes
  328. */
  329. Static void pipex_iface_start (struct pipex_iface_context *);
  330. Static void pipex_iface_stop (struct pipex_iface_context *);
  331. Static int pipex_add_session (struct pipex_session_req *, struct pipex_iface_context *);
  332. Static int pipex_close_session (struct pipex_session_close_req *);
  333. Static int pipex_config_session (struct pipex_session_config_req *);
  334. Static int pipex_get_stat (struct pipex_session_stat_req *);
  335. Static int pipex_get_closed (struct pipex_session_list_req *);
  336. Static int pipex_destroy_session (struct pipex_session *);
  337. Static struct pipex_session *pipex_lookup_by_ip_address (struct in_addr);
  338. Static struct pipex_session *pipex_lookup_by_session_id (int, int);
  339. Static void pipex_ip_output (struct mbuf *, struct pipex_session *);
  340. Static void pipex_ppp_output (struct mbuf *, struct pipex_session *, int);
  341. Static inline int pipex_ppp_proto (struct mbuf *, struct pipex_session *, int, int *);
  342. Static void pipex_ppp_input (struct mbuf *, struct pipex_session *, int);
  343. Static void pipex_ip_input (struct mbuf *, struct pipex_session *);
  344. #ifdef INET6
  345. Static void pipex_ip6_input (struct mbuf *, struct pipex_session *);
  346. #endif
  347. Static struct mbuf *pipex_common_input(struct pipex_session *, struct mbuf *, int, int);
  348. #ifdef PIPEX_PPPOE
  349. Static void pipex_pppoe_output (struct mbuf *, struct pipex_session *);
  350. #endif
  351. #ifdef PIPEX_PPTP
  352. Static void pipex_pptp_output (struct mbuf *, struct pipex_session *, int, int);
  353. Static struct pipex_session *pipex_pptp_userland_lookup_session(struct mbuf *, struct sockaddr *);
  354. #endif
  355. #ifdef PIPEX_L2TP
  356. Static void pipex_l2tp_output (struct mbuf *, struct pipex_session *);
  357. #endif
  358. #ifdef PIPEX_MPPE
  359. Static void pipex_mppe_init (struct pipex_mppe *, int, int, u_char *, int);
  360. Static void GetNewKeyFromSHA (u_char *, u_char *, int, u_char *);
  361. Static void pipex_mppe_reduce_key (struct pipex_mppe *);
  362. Static void mppe_key_change (struct pipex_mppe *);
  363. Static void pipex_mppe_input (struct mbuf *, struct pipex_session *);
  364. Static void pipex_mppe_output (struct mbuf *, struct pipex_session *, uint16_t);
  365. Static void pipex_ccp_input (struct mbuf *, struct pipex_session *);
  366. Static int pipex_ccp_output (struct pipex_session *, int, int);
  367. Static inline int pipex_mppe_setkey(struct pipex_mppe *);
  368. Static inline int pipex_mppe_setoldkey(struct pipex_mppe *, uint16_t);
  369. Static inline void pipex_mppe_crypt(struct pipex_mppe *, int, u_char *, u_char *);
  370. #endif
  371. Static struct mbuf *adjust_tcp_mss (struct mbuf *, int);
  372. Static struct mbuf *ip_is_idle_packet (struct mbuf *, int *);
  373. Static void pipex_session_log (struct pipex_session *, int, const char *, ...) __attribute__((__format__(__printf__,3,4)));
  374. Static uint32_t pipex_sockaddr_hash_key(struct sockaddr *);
  375. Static int pipex_sockaddr_compar_addr(struct sockaddr *, struct sockaddr *);
  376. Static int pipex_ppp_enqueue (struct mbuf *, struct pipex_session *, struct mbuf_queue *);
  377. Static void pipex_ppp_dequeue (void);
  378. Static void pipex_timer_start (void);
  379. Static void pipex_timer_stop (void);
  380. Static void pipex_timer (void *);