PageRenderTime 91ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/sbin/setkey/test-pfkey.c

https://bitbucket.org/freebsd/freebsd-base
C | 533 lines | 394 code | 78 blank | 61 comment | 13 complexity | efd8d6a46941435792358b3301238212 MD5 | raw file
  1. /* $FreeBSD$ */
  2. /* $KAME: test-pfkey.c,v 1.4 2000/06/07 00:29:14 itojun Exp $ */
  3. /*-
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. *
  6. * Copyright (C) 1995, 1996, 1997, 1998, and 1999 WIDE Project.
  7. * All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in the
  16. * documentation and/or other materials provided with the distribution.
  17. * 3. Neither the name of the project nor the names of its contributors
  18. * may be used to endorse or promote products derived from this software
  19. * without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
  22. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24. * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
  25. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31. * SUCH DAMAGE.
  32. */
  33. #include <sys/types.h>
  34. #include <sys/param.h>
  35. #include <sys/socket.h>
  36. #include <net/route.h>
  37. #include <net/pfkeyv2.h>
  38. #include <netinet/in.h>
  39. #include <netipsec/keydb.h>
  40. #include <netipsec/key_var.h>
  41. #include <netipsec/key_debug.h>
  42. #include <stdio.h>
  43. #include <stdlib.h>
  44. #include <limits.h>
  45. #include <string.h>
  46. #include <ctype.h>
  47. #include <unistd.h>
  48. #include <errno.h>
  49. #include <netdb.h>
  50. u_char m_buf[BUFSIZ];
  51. u_int m_len;
  52. char *pname;
  53. void Usage(void);
  54. int sendkeymsg(void);
  55. void key_setsadbmsg(u_int);
  56. void key_setsadbsens(void);
  57. void key_setsadbprop(void);
  58. void key_setsadbid(u_int, caddr_t);
  59. void key_setsadblft(u_int, u_int);
  60. void key_setspirange(void);
  61. void key_setsadbkey(u_int, caddr_t);
  62. void key_setsadbsa(void);
  63. void key_setsadbaddr(u_int, u_int, caddr_t);
  64. void key_setsadbextbuf(caddr_t, int, caddr_t, int, caddr_t, int);
  65. void
  66. Usage()
  67. {
  68. printf("Usage:\t%s number\n", pname);
  69. exit(0);
  70. }
  71. int
  72. main(ac, av)
  73. int ac;
  74. char **av;
  75. {
  76. pname = *av;
  77. if (ac == 1) Usage();
  78. key_setsadbmsg(atoi(*(av+1)));
  79. sendkeymsg();
  80. exit(0);
  81. }
  82. /* %%% */
  83. int
  84. sendkeymsg()
  85. {
  86. u_char rbuf[1024 * 32]; /* XXX: Enough ? Should I do MSG_PEEK ? */
  87. int so, len;
  88. if ((so = socket(PF_KEY, SOCK_RAW, PF_KEY_V2)) < 0) {
  89. perror("socket(PF_KEY)");
  90. goto end;
  91. }
  92. #if 0
  93. {
  94. #include <sys/time.h>
  95. struct timeval tv;
  96. tv.tv_sec = 1;
  97. tv.tv_usec = 0;
  98. if (setsockopt(so, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) < 0) {
  99. perror("setsockopt");
  100. goto end;
  101. }
  102. }
  103. #endif
  104. pfkey_sadump((struct sadb_msg *)m_buf);
  105. if ((len = send(so, m_buf, m_len, 0)) < 0) {
  106. perror("send");
  107. goto end;
  108. }
  109. if ((len = recv(so, rbuf, sizeof(rbuf), 0)) < 0) {
  110. perror("recv");
  111. goto end;
  112. }
  113. pfkey_sadump((struct sadb_msg *)rbuf);
  114. end:
  115. (void)close(so);
  116. return(0);
  117. }
  118. void
  119. key_setsadbmsg(type)
  120. u_int type;
  121. {
  122. struct sadb_msg m_msg;
  123. memset(&m_msg, 0, sizeof(m_msg));
  124. m_msg.sadb_msg_version = PF_KEY_V2;
  125. m_msg.sadb_msg_type = type;
  126. m_msg.sadb_msg_errno = 0;
  127. m_msg.sadb_msg_satype = SADB_SATYPE_ESP;
  128. #if 0
  129. m_msg.sadb_msg_reserved = 0;
  130. #endif
  131. m_msg.sadb_msg_seq = 0;
  132. m_msg.sadb_msg_pid = getpid();
  133. m_len = sizeof(struct sadb_msg);
  134. memcpy(m_buf, &m_msg, m_len);
  135. switch (type) {
  136. case SADB_GETSPI:
  137. /*<base, address(SD), SPI range>*/
  138. key_setsadbaddr(SADB_EXT_ADDRESS_SRC, AF_INET, "10.0.3.4");
  139. key_setsadbaddr(SADB_EXT_ADDRESS_DST, AF_INET, "127.0.0.1");
  140. key_setspirange();
  141. /*<base, SA(*), address(SD)>*/
  142. break;
  143. case SADB_ADD:
  144. /* <base, SA, (lifetime(HSC),) address(SD), (address(P),)
  145. key(AE), (identity(SD),) (sensitivity)> */
  146. key_setsadbaddr(SADB_EXT_ADDRESS_PROXY, AF_INET6, "3ffe::1");
  147. case SADB_UPDATE:
  148. key_setsadbsa();
  149. key_setsadblft(SADB_EXT_LIFETIME_HARD, 10);
  150. key_setsadblft(SADB_EXT_LIFETIME_SOFT, 5);
  151. key_setsadbaddr(SADB_EXT_ADDRESS_SRC, AF_INET, "192.168.1.1");
  152. key_setsadbaddr(SADB_EXT_ADDRESS_DST, AF_INET, "10.0.3.4");
  153. /* XXX key_setsadbkey(SADB_EXT_KEY_AUTH, "abcde"); */
  154. key_setsadbkey(SADB_EXT_KEY_AUTH, "1234567812345678");
  155. key_setsadbkey(SADB_EXT_KEY_ENCRYPT, "12345678");
  156. key_setsadbid(SADB_EXT_IDENTITY_SRC, "hoge1234@hoge.com");
  157. key_setsadbid(SADB_EXT_IDENTITY_DST, "hage5678@hage.net");
  158. key_setsadbsens();
  159. /* <base, SA, (lifetime(HSC),) address(SD), (address(P),)
  160. (identity(SD),) (sensitivity)> */
  161. break;
  162. case SADB_DELETE:
  163. /* <base, SA(*), address(SDP)> */
  164. key_setsadbsa();
  165. key_setsadbaddr(SADB_EXT_ADDRESS_SRC, AF_INET, "192.168.1.1");
  166. key_setsadbaddr(SADB_EXT_ADDRESS_DST, AF_INET, "10.0.3.4");
  167. key_setsadbaddr(SADB_EXT_ADDRESS_PROXY, AF_INET6, "3ffe::1");
  168. /* <base, SA(*), address(SDP)> */
  169. break;
  170. case SADB_GET:
  171. /* <base, SA(*), address(SDP)> */
  172. key_setsadbsa();
  173. key_setsadbaddr(SADB_EXT_ADDRESS_SRC, AF_INET, "192.168.1.1");
  174. key_setsadbaddr(SADB_EXT_ADDRESS_DST, AF_INET, "10.0.3.4");
  175. key_setsadbaddr(SADB_EXT_ADDRESS_PROXY, AF_INET6, "3ffe::1");
  176. /* <base, SA, (lifetime(HSC),) address(SD), (address(P),)
  177. key(AE), (identity(SD),) (sensitivity)> */
  178. break;
  179. case SADB_ACQUIRE:
  180. /* <base, address(SD), (address(P),) (identity(SD),)
  181. (sensitivity,) proposal> */
  182. key_setsadbaddr(SADB_EXT_ADDRESS_SRC, AF_INET, "192.168.1.1");
  183. key_setsadbaddr(SADB_EXT_ADDRESS_DST, AF_INET, "10.0.3.4");
  184. key_setsadbaddr(SADB_EXT_ADDRESS_PROXY, AF_INET6, "3ffe::1");
  185. key_setsadbid(SADB_EXT_IDENTITY_SRC, "hoge1234@hoge.com");
  186. key_setsadbid(SADB_EXT_IDENTITY_DST, "hage5678@hage.net");
  187. key_setsadbsens();
  188. key_setsadbprop();
  189. /* <base, address(SD), (address(P),) (identity(SD),)
  190. (sensitivity,) proposal> */
  191. break;
  192. case SADB_REGISTER:
  193. /* <base> */
  194. /* <base, supported> */
  195. break;
  196. case SADB_EXPIRE:
  197. case SADB_FLUSH:
  198. break;
  199. case SADB_DUMP:
  200. break;
  201. case SADB_X_PROMISC:
  202. /* <base> */
  203. /* <base, base(, others)> */
  204. break;
  205. case SADB_X_PCHANGE:
  206. break;
  207. /* for SPD management */
  208. case SADB_X_SPDFLUSH:
  209. case SADB_X_SPDDUMP:
  210. break;
  211. case SADB_X_SPDADD:
  212. #if 0
  213. {
  214. struct sadb_x_policy m_policy;
  215. m_policy.sadb_x_policy_len = PFKEY_UNIT64(sizeof(m_policy));
  216. m_policy.sadb_x_policy_exttype = SADB_X_EXT_POLICY;
  217. m_policy.sadb_x_policy_type = SADB_X_PL_IPSEC;
  218. m_policy.sadb_x_policy_esp_trans = 1;
  219. m_policy.sadb_x_policy_ah_trans = 2;
  220. m_policy.sadb_x_policy_esp_network = 3;
  221. m_policy.sadb_x_policy_ah_network = 4;
  222. m_policy.sadb_x_policy_reserved = 0;
  223. memcpy(m_buf + m_len, &m_policy, sizeof(struct sadb_x_policy));
  224. m_len += sizeof(struct sadb_x_policy);
  225. }
  226. #endif
  227. case SADB_X_SPDDELETE:
  228. key_setsadbaddr(SADB_EXT_ADDRESS_SRC, AF_INET, "192.168.1.1");
  229. key_setsadbaddr(SADB_EXT_ADDRESS_DST, AF_INET, "10.0.3.4");
  230. break;
  231. }
  232. ((struct sadb_msg *)m_buf)->sadb_msg_len = PFKEY_UNIT64(m_len);
  233. return;
  234. }
  235. void
  236. key_setsadbsens()
  237. {
  238. struct sadb_sens m_sens;
  239. u_char buf[64];
  240. u_int s, i, slen, ilen, len;
  241. /* make sens & integ */
  242. s = htonl(0x01234567);
  243. i = htonl(0x89abcdef);
  244. slen = sizeof(s);
  245. ilen = sizeof(i);
  246. memcpy(buf, &s, slen);
  247. memcpy(buf + slen, &i, ilen);
  248. len = sizeof(m_sens) + PFKEY_ALIGN8(slen) + PFKEY_ALIGN8(ilen);
  249. m_sens.sadb_sens_len = PFKEY_UNIT64(len);
  250. m_sens.sadb_sens_exttype = SADB_EXT_SENSITIVITY;
  251. m_sens.sadb_sens_dpd = 1;
  252. m_sens.sadb_sens_sens_level = 2;
  253. m_sens.sadb_sens_sens_len = PFKEY_ALIGN8(slen);
  254. m_sens.sadb_sens_integ_level = 3;
  255. m_sens.sadb_sens_integ_len = PFKEY_ALIGN8(ilen);
  256. m_sens.sadb_sens_reserved = 0;
  257. key_setsadbextbuf(m_buf, m_len,
  258. (caddr_t)&m_sens, sizeof(struct sadb_sens),
  259. buf, slen + ilen);
  260. m_len += len;
  261. return;
  262. }
  263. void
  264. key_setsadbprop()
  265. {
  266. struct sadb_prop m_prop;
  267. struct sadb_comb *m_comb;
  268. u_char buf[256];
  269. u_int len = sizeof(m_prop) + sizeof(m_comb) * 2;
  270. /* make prop & comb */
  271. m_prop.sadb_prop_len = PFKEY_UNIT64(len);
  272. m_prop.sadb_prop_exttype = SADB_EXT_PROPOSAL;
  273. m_prop.sadb_prop_replay = 0;
  274. m_prop.sadb_prop_reserved[0] = 0;
  275. m_prop.sadb_prop_reserved[1] = 0;
  276. m_prop.sadb_prop_reserved[2] = 0;
  277. /* the 1st is ESP DES-CBC HMAC-MD5 */
  278. m_comb = (struct sadb_comb *)buf;
  279. m_comb->sadb_comb_auth = SADB_AALG_MD5HMAC;
  280. m_comb->sadb_comb_encrypt = SADB_EALG_DESCBC;
  281. m_comb->sadb_comb_flags = 0;
  282. m_comb->sadb_comb_auth_minbits = 8;
  283. m_comb->sadb_comb_auth_maxbits = 96;
  284. m_comb->sadb_comb_encrypt_minbits = 64;
  285. m_comb->sadb_comb_encrypt_maxbits = 64;
  286. m_comb->sadb_comb_reserved = 0;
  287. m_comb->sadb_comb_soft_allocations = 0;
  288. m_comb->sadb_comb_hard_allocations = 0;
  289. m_comb->sadb_comb_soft_bytes = 0;
  290. m_comb->sadb_comb_hard_bytes = 0;
  291. m_comb->sadb_comb_soft_addtime = 0;
  292. m_comb->sadb_comb_hard_addtime = 0;
  293. m_comb->sadb_comb_soft_usetime = 0;
  294. m_comb->sadb_comb_hard_usetime = 0;
  295. /* the 2st is ESP 3DES-CBC and AH HMAC-SHA1 */
  296. m_comb = (struct sadb_comb *)(buf + sizeof(*m_comb));
  297. m_comb->sadb_comb_auth = SADB_AALG_SHA1HMAC;
  298. m_comb->sadb_comb_encrypt = SADB_EALG_3DESCBC;
  299. m_comb->sadb_comb_flags = 0;
  300. m_comb->sadb_comb_auth_minbits = 8;
  301. m_comb->sadb_comb_auth_maxbits = 96;
  302. m_comb->sadb_comb_encrypt_minbits = 64;
  303. m_comb->sadb_comb_encrypt_maxbits = 64;
  304. m_comb->sadb_comb_reserved = 0;
  305. m_comb->sadb_comb_soft_allocations = 0;
  306. m_comb->sadb_comb_hard_allocations = 0;
  307. m_comb->sadb_comb_soft_bytes = 0;
  308. m_comb->sadb_comb_hard_bytes = 0;
  309. m_comb->sadb_comb_soft_addtime = 0;
  310. m_comb->sadb_comb_hard_addtime = 0;
  311. m_comb->sadb_comb_soft_usetime = 0;
  312. m_comb->sadb_comb_hard_usetime = 0;
  313. key_setsadbextbuf(m_buf, m_len,
  314. (caddr_t)&m_prop, sizeof(struct sadb_prop),
  315. buf, sizeof(*m_comb) * 2);
  316. m_len += len;
  317. return;
  318. }
  319. void
  320. key_setsadbid(ext, str)
  321. u_int ext;
  322. caddr_t str;
  323. {
  324. struct sadb_ident m_id;
  325. u_int idlen = strlen(str), len;
  326. len = sizeof(m_id) + PFKEY_ALIGN8(idlen);
  327. m_id.sadb_ident_len = PFKEY_UNIT64(len);
  328. m_id.sadb_ident_exttype = ext;
  329. m_id.sadb_ident_type = SADB_IDENTTYPE_USERFQDN;
  330. m_id.sadb_ident_reserved = 0;
  331. m_id.sadb_ident_id = getpid();
  332. key_setsadbextbuf(m_buf, m_len,
  333. (caddr_t)&m_id, sizeof(struct sadb_ident),
  334. str, idlen);
  335. m_len += len;
  336. return;
  337. }
  338. void
  339. key_setsadblft(ext, time)
  340. u_int ext, time;
  341. {
  342. struct sadb_lifetime m_lft;
  343. m_lft.sadb_lifetime_len = PFKEY_UNIT64(sizeof(m_lft));
  344. m_lft.sadb_lifetime_exttype = ext;
  345. m_lft.sadb_lifetime_allocations = 0x2;
  346. m_lft.sadb_lifetime_bytes = 0x1000;
  347. m_lft.sadb_lifetime_addtime = time;
  348. m_lft.sadb_lifetime_usetime = 0x0020;
  349. memcpy(m_buf + m_len, &m_lft, sizeof(struct sadb_lifetime));
  350. m_len += sizeof(struct sadb_lifetime);
  351. return;
  352. }
  353. void
  354. key_setspirange()
  355. {
  356. struct sadb_spirange m_spi;
  357. m_spi.sadb_spirange_len = PFKEY_UNIT64(sizeof(m_spi));
  358. m_spi.sadb_spirange_exttype = SADB_EXT_SPIRANGE;
  359. m_spi.sadb_spirange_min = 0x00001000;
  360. m_spi.sadb_spirange_max = 0x00002000;
  361. m_spi.sadb_spirange_reserved = 0;
  362. memcpy(m_buf + m_len, &m_spi, sizeof(struct sadb_spirange));
  363. m_len += sizeof(struct sadb_spirange);
  364. return;
  365. }
  366. void
  367. key_setsadbkey(ext, str)
  368. u_int ext;
  369. caddr_t str;
  370. {
  371. struct sadb_key m_key;
  372. u_int keylen = strlen(str);
  373. u_int len;
  374. len = sizeof(struct sadb_key) + PFKEY_ALIGN8(keylen);
  375. m_key.sadb_key_len = PFKEY_UNIT64(len);
  376. m_key.sadb_key_exttype = ext;
  377. m_key.sadb_key_bits = keylen * 8;
  378. m_key.sadb_key_reserved = 0;
  379. key_setsadbextbuf(m_buf, m_len,
  380. (caddr_t)&m_key, sizeof(struct sadb_key),
  381. str, keylen);
  382. m_len += len;
  383. return;
  384. }
  385. void
  386. key_setsadbsa()
  387. {
  388. struct sadb_sa m_sa;
  389. m_sa.sadb_sa_len = PFKEY_UNIT64(sizeof(struct sadb_sa));
  390. m_sa.sadb_sa_exttype = SADB_EXT_SA;
  391. m_sa.sadb_sa_spi = htonl(0x12345678);
  392. m_sa.sadb_sa_replay = 4;
  393. m_sa.sadb_sa_state = 0;
  394. m_sa.sadb_sa_auth = SADB_AALG_MD5HMAC;
  395. m_sa.sadb_sa_encrypt = SADB_EALG_DESCBC;
  396. m_sa.sadb_sa_flags = 0;
  397. memcpy(m_buf + m_len, &m_sa, sizeof(struct sadb_sa));
  398. m_len += sizeof(struct sadb_sa);
  399. return;
  400. }
  401. void
  402. key_setsadbaddr(ext, af, str)
  403. u_int ext, af;
  404. caddr_t str;
  405. {
  406. struct sadb_address m_addr;
  407. u_int len;
  408. struct addrinfo hints, *res;
  409. const char *serv;
  410. int plen;
  411. switch (af) {
  412. case AF_INET:
  413. plen = sizeof(struct in_addr) << 3;
  414. break;
  415. case AF_INET6:
  416. plen = sizeof(struct in6_addr) << 3;
  417. break;
  418. default:
  419. /* XXX bark */
  420. exit(1);
  421. }
  422. /* make sockaddr buffer */
  423. memset(&hints, 0, sizeof(hints));
  424. hints.ai_family = af;
  425. hints.ai_socktype = SOCK_DGRAM; /*dummy*/
  426. hints.ai_flags = AI_NUMERICHOST;
  427. serv = (ext == SADB_EXT_ADDRESS_PROXY ? "0" : "4660"); /*0x1234*/
  428. if (getaddrinfo(str, serv, &hints, &res) != 0 || res->ai_next) {
  429. /* XXX bark */
  430. exit(1);
  431. }
  432. len = sizeof(struct sadb_address) + PFKEY_ALIGN8(res->ai_addrlen);
  433. m_addr.sadb_address_len = PFKEY_UNIT64(len);
  434. m_addr.sadb_address_exttype = ext;
  435. m_addr.sadb_address_proto =
  436. (ext == SADB_EXT_ADDRESS_PROXY ? 0 : IPPROTO_TCP);
  437. m_addr.sadb_address_prefixlen = plen;
  438. m_addr.sadb_address_reserved = 0;
  439. key_setsadbextbuf(m_buf, m_len,
  440. (caddr_t)&m_addr, sizeof(struct sadb_address),
  441. (caddr_t)res->ai_addr, res->ai_addrlen);
  442. m_len += len;
  443. freeaddrinfo(res);
  444. return;
  445. }
  446. void
  447. key_setsadbextbuf(dst, off, ebuf, elen, vbuf, vlen)
  448. caddr_t dst, ebuf, vbuf;
  449. int off, elen, vlen;
  450. {
  451. memset(dst + off, 0, elen + vlen);
  452. memcpy(dst + off, (caddr_t)ebuf, elen);
  453. memcpy(dst + off + elen, vbuf, vlen);
  454. return;
  455. }