test_flow_dissector.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Inject packets with all sorts of encapsulation into the kernel.
  4. *
  5. * IPv4/IPv6 outer layer 3
  6. * GRE/GUE/BARE outer layer 4, where bare is IPIP/SIT/IPv4-in-IPv6/..
  7. * IPv4/IPv6 inner layer 3
  8. */
  9. #define _GNU_SOURCE
  10. #include <stddef.h>
  11. #include <arpa/inet.h>
  12. #include <asm/byteorder.h>
  13. #include <error.h>
  14. #include <errno.h>
  15. #include <linux/if_packet.h>
  16. #include <linux/if_ether.h>
  17. #include <linux/if_packet.h>
  18. #include <linux/ipv6.h>
  19. #include <netinet/ip.h>
  20. #include <netinet/in.h>
  21. #include <netinet/udp.h>
  22. #include <poll.h>
  23. #include <stdbool.h>
  24. #include <stdlib.h>
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <string.h>
  28. #include <sys/ioctl.h>
  29. #include <sys/socket.h>
  30. #include <sys/stat.h>
  31. #include <sys/time.h>
  32. #include <sys/types.h>
  33. #include <unistd.h>
  34. #define CFG_PORT_INNER 8000
  35. /* Add some protocol definitions that do not exist in userspace */
  36. struct grehdr {
  37. uint16_t unused;
  38. uint16_t protocol;
  39. } __attribute__((packed));
  40. struct guehdr {
  41. union {
  42. struct {
  43. #if defined(__LITTLE_ENDIAN_BITFIELD)
  44. __u8 hlen:5,
  45. control:1,
  46. version:2;
  47. #elif defined (__BIG_ENDIAN_BITFIELD)
  48. __u8 version:2,
  49. control:1,
  50. hlen:5;
  51. #else
  52. #error "Please fix <asm/byteorder.h>"
  53. #endif
  54. __u8 proto_ctype;
  55. __be16 flags;
  56. };
  57. __be32 word;
  58. };
  59. };
  60. static uint8_t cfg_dsfield_inner;
  61. static uint8_t cfg_dsfield_outer;
  62. static uint8_t cfg_encap_proto;
  63. static bool cfg_expect_failure = false;
  64. static int cfg_l3_extra = AF_UNSPEC; /* optional SIT prefix */
  65. static int cfg_l3_inner = AF_UNSPEC;
  66. static int cfg_l3_outer = AF_UNSPEC;
  67. static int cfg_num_pkt = 10;
  68. static int cfg_num_secs = 0;
  69. static char cfg_payload_char = 'a';
  70. static int cfg_payload_len = 100;
  71. static int cfg_port_gue = 6080;
  72. static bool cfg_only_rx;
  73. static bool cfg_only_tx;
  74. static int cfg_src_port = 9;
  75. static char buf[ETH_DATA_LEN];
  76. #define INIT_ADDR4(name, addr4, port) \
  77. static struct sockaddr_in name = { \
  78. .sin_family = AF_INET, \
  79. .sin_port = __constant_htons(port), \
  80. .sin_addr.s_addr = __constant_htonl(addr4), \
  81. };
  82. #define INIT_ADDR6(name, addr6, port) \
  83. static struct sockaddr_in6 name = { \
  84. .sin6_family = AF_INET6, \
  85. .sin6_port = __constant_htons(port), \
  86. .sin6_addr = addr6, \
  87. };
  88. INIT_ADDR4(in_daddr4, INADDR_LOOPBACK, CFG_PORT_INNER)
  89. INIT_ADDR4(in_saddr4, INADDR_LOOPBACK + 2, 0)
  90. INIT_ADDR4(out_daddr4, INADDR_LOOPBACK, 0)
  91. INIT_ADDR4(out_saddr4, INADDR_LOOPBACK + 1, 0)
  92. INIT_ADDR4(extra_daddr4, INADDR_LOOPBACK, 0)
  93. INIT_ADDR4(extra_saddr4, INADDR_LOOPBACK + 1, 0)
  94. INIT_ADDR6(in_daddr6, IN6ADDR_LOOPBACK_INIT, CFG_PORT_INNER)
  95. INIT_ADDR6(in_saddr6, IN6ADDR_LOOPBACK_INIT, 0)
  96. INIT_ADDR6(out_daddr6, IN6ADDR_LOOPBACK_INIT, 0)
  97. INIT_ADDR6(out_saddr6, IN6ADDR_LOOPBACK_INIT, 0)
  98. INIT_ADDR6(extra_daddr6, IN6ADDR_LOOPBACK_INIT, 0)
  99. INIT_ADDR6(extra_saddr6, IN6ADDR_LOOPBACK_INIT, 0)
  100. static unsigned long util_gettime(void)
  101. {
  102. struct timeval tv;
  103. gettimeofday(&tv, NULL);
  104. return (tv.tv_sec * 1000) + (tv.tv_usec / 1000);
  105. }
  106. static void util_printaddr(const char *msg, struct sockaddr *addr)
  107. {
  108. unsigned long off = 0;
  109. char nbuf[INET6_ADDRSTRLEN];
  110. switch (addr->sa_family) {
  111. case PF_INET:
  112. off = __builtin_offsetof(struct sockaddr_in, sin_addr);
  113. break;
  114. case PF_INET6:
  115. off = __builtin_offsetof(struct sockaddr_in6, sin6_addr);
  116. break;
  117. default:
  118. error(1, 0, "printaddr: unsupported family %u\n",
  119. addr->sa_family);
  120. }
  121. if (!inet_ntop(addr->sa_family, ((void *) addr) + off, nbuf,
  122. sizeof(nbuf)))
  123. error(1, errno, "inet_ntop");
  124. fprintf(stderr, "%s: %s\n", msg, nbuf);
  125. }
  126. static unsigned long add_csum_hword(const uint16_t *start, int num_u16)
  127. {
  128. unsigned long sum = 0;
  129. int i;
  130. for (i = 0; i < num_u16; i++)
  131. sum += start[i];
  132. return sum;
  133. }
  134. static uint16_t build_ip_csum(const uint16_t *start, int num_u16,
  135. unsigned long sum)
  136. {
  137. sum += add_csum_hword(start, num_u16);
  138. while (sum >> 16)
  139. sum = (sum & 0xffff) + (sum >> 16);
  140. return ~sum;
  141. }
  142. static void build_ipv4_header(void *header, uint8_t proto,
  143. uint32_t src, uint32_t dst,
  144. int payload_len, uint8_t tos)
  145. {
  146. struct iphdr *iph = header;
  147. iph->ihl = 5;
  148. iph->version = 4;
  149. iph->tos = tos;
  150. iph->ttl = 8;
  151. iph->tot_len = htons(sizeof(*iph) + payload_len);
  152. iph->id = htons(1337);
  153. iph->protocol = proto;
  154. iph->saddr = src;
  155. iph->daddr = dst;
  156. iph->check = build_ip_csum((void *) iph, iph->ihl << 1, 0);
  157. }
  158. static void ipv6_set_dsfield(struct ipv6hdr *ip6h, uint8_t dsfield)
  159. {
  160. uint16_t val, *ptr = (uint16_t *)ip6h;
  161. val = ntohs(*ptr);
  162. val &= 0xF00F;
  163. val |= ((uint16_t) dsfield) << 4;
  164. *ptr = htons(val);
  165. }
  166. static void build_ipv6_header(void *header, uint8_t proto,
  167. struct sockaddr_in6 *src,
  168. struct sockaddr_in6 *dst,
  169. int payload_len, uint8_t dsfield)
  170. {
  171. struct ipv6hdr *ip6h = header;
  172. ip6h->version = 6;
  173. ip6h->payload_len = htons(payload_len);
  174. ip6h->nexthdr = proto;
  175. ip6h->hop_limit = 8;
  176. ipv6_set_dsfield(ip6h, dsfield);
  177. memcpy(&ip6h->saddr, &src->sin6_addr, sizeof(ip6h->saddr));
  178. memcpy(&ip6h->daddr, &dst->sin6_addr, sizeof(ip6h->daddr));
  179. }
  180. static uint16_t build_udp_v4_csum(const struct iphdr *iph,
  181. const struct udphdr *udph,
  182. int num_words)
  183. {
  184. unsigned long pseudo_sum;
  185. int num_u16 = sizeof(iph->saddr); /* halfwords: twice byte len */
  186. pseudo_sum = add_csum_hword((void *) &iph->saddr, num_u16);
  187. pseudo_sum += htons(IPPROTO_UDP);
  188. pseudo_sum += udph->len;
  189. return build_ip_csum((void *) udph, num_words, pseudo_sum);
  190. }
  191. static uint16_t build_udp_v6_csum(const struct ipv6hdr *ip6h,
  192. const struct udphdr *udph,
  193. int num_words)
  194. {
  195. unsigned long pseudo_sum;
  196. int num_u16 = sizeof(ip6h->saddr); /* halfwords: twice byte len */
  197. pseudo_sum = add_csum_hword((void *) &ip6h->saddr, num_u16);
  198. pseudo_sum += htons(ip6h->nexthdr);
  199. pseudo_sum += ip6h->payload_len;
  200. return build_ip_csum((void *) udph, num_words, pseudo_sum);
  201. }
  202. static void build_udp_header(void *header, int payload_len,
  203. uint16_t dport, int family)
  204. {
  205. struct udphdr *udph = header;
  206. int len = sizeof(*udph) + payload_len;
  207. udph->source = htons(cfg_src_port);
  208. udph->dest = htons(dport);
  209. udph->len = htons(len);
  210. udph->check = 0;
  211. if (family == AF_INET)
  212. udph->check = build_udp_v4_csum(header - sizeof(struct iphdr),
  213. udph, len >> 1);
  214. else
  215. udph->check = build_udp_v6_csum(header - sizeof(struct ipv6hdr),
  216. udph, len >> 1);
  217. }
  218. static void build_gue_header(void *header, uint8_t proto)
  219. {
  220. struct guehdr *gueh = header;
  221. gueh->proto_ctype = proto;
  222. }
  223. static void build_gre_header(void *header, uint16_t proto)
  224. {
  225. struct grehdr *greh = header;
  226. greh->protocol = htons(proto);
  227. }
  228. static int l3_length(int family)
  229. {
  230. if (family == AF_INET)
  231. return sizeof(struct iphdr);
  232. else
  233. return sizeof(struct ipv6hdr);
  234. }
  235. static int build_packet(void)
  236. {
  237. int ol3_len = 0, ol4_len = 0, il3_len = 0, il4_len = 0;
  238. int el3_len = 0;
  239. if (cfg_l3_extra)
  240. el3_len = l3_length(cfg_l3_extra);
  241. /* calculate header offsets */
  242. if (cfg_encap_proto) {
  243. ol3_len = l3_length(cfg_l3_outer);
  244. if (cfg_encap_proto == IPPROTO_GRE)
  245. ol4_len = sizeof(struct grehdr);
  246. else if (cfg_encap_proto == IPPROTO_UDP)
  247. ol4_len = sizeof(struct udphdr) + sizeof(struct guehdr);
  248. }
  249. il3_len = l3_length(cfg_l3_inner);
  250. il4_len = sizeof(struct udphdr);
  251. if (el3_len + ol3_len + ol4_len + il3_len + il4_len + cfg_payload_len >=
  252. sizeof(buf))
  253. error(1, 0, "packet too large\n");
  254. /*
  255. * Fill packet from inside out, to calculate correct checksums.
  256. * But create ip before udp headers, as udp uses ip for pseudo-sum.
  257. */
  258. memset(buf + el3_len + ol3_len + ol4_len + il3_len + il4_len,
  259. cfg_payload_char, cfg_payload_len);
  260. /* add zero byte for udp csum padding */
  261. buf[el3_len + ol3_len + ol4_len + il3_len + il4_len + cfg_payload_len] = 0;
  262. switch (cfg_l3_inner) {
  263. case PF_INET:
  264. build_ipv4_header(buf + el3_len + ol3_len + ol4_len,
  265. IPPROTO_UDP,
  266. in_saddr4.sin_addr.s_addr,
  267. in_daddr4.sin_addr.s_addr,
  268. il4_len + cfg_payload_len,
  269. cfg_dsfield_inner);
  270. break;
  271. case PF_INET6:
  272. build_ipv6_header(buf + el3_len + ol3_len + ol4_len,
  273. IPPROTO_UDP,
  274. &in_saddr6, &in_daddr6,
  275. il4_len + cfg_payload_len,
  276. cfg_dsfield_inner);
  277. break;
  278. }
  279. build_udp_header(buf + el3_len + ol3_len + ol4_len + il3_len,
  280. cfg_payload_len, CFG_PORT_INNER, cfg_l3_inner);
  281. if (!cfg_encap_proto)
  282. return il3_len + il4_len + cfg_payload_len;
  283. switch (cfg_l3_outer) {
  284. case PF_INET:
  285. build_ipv4_header(buf + el3_len, cfg_encap_proto,
  286. out_saddr4.sin_addr.s_addr,
  287. out_daddr4.sin_addr.s_addr,
  288. ol4_len + il3_len + il4_len + cfg_payload_len,
  289. cfg_dsfield_outer);
  290. break;
  291. case PF_INET6:
  292. build_ipv6_header(buf + el3_len, cfg_encap_proto,
  293. &out_saddr6, &out_daddr6,
  294. ol4_len + il3_len + il4_len + cfg_payload_len,
  295. cfg_dsfield_outer);
  296. break;
  297. }
  298. switch (cfg_encap_proto) {
  299. case IPPROTO_UDP:
  300. build_gue_header(buf + el3_len + ol3_len + ol4_len -
  301. sizeof(struct guehdr),
  302. cfg_l3_inner == PF_INET ? IPPROTO_IPIP
  303. : IPPROTO_IPV6);
  304. build_udp_header(buf + el3_len + ol3_len,
  305. sizeof(struct guehdr) + il3_len + il4_len +
  306. cfg_payload_len,
  307. cfg_port_gue, cfg_l3_outer);
  308. break;
  309. case IPPROTO_GRE:
  310. build_gre_header(buf + el3_len + ol3_len,
  311. cfg_l3_inner == PF_INET ? ETH_P_IP
  312. : ETH_P_IPV6);
  313. break;
  314. }
  315. switch (cfg_l3_extra) {
  316. case PF_INET:
  317. build_ipv4_header(buf,
  318. cfg_l3_outer == PF_INET ? IPPROTO_IPIP
  319. : IPPROTO_IPV6,
  320. extra_saddr4.sin_addr.s_addr,
  321. extra_daddr4.sin_addr.s_addr,
  322. ol3_len + ol4_len + il3_len + il4_len +
  323. cfg_payload_len, 0);
  324. break;
  325. case PF_INET6:
  326. build_ipv6_header(buf,
  327. cfg_l3_outer == PF_INET ? IPPROTO_IPIP
  328. : IPPROTO_IPV6,
  329. &extra_saddr6, &extra_daddr6,
  330. ol3_len + ol4_len + il3_len + il4_len +
  331. cfg_payload_len, 0);
  332. break;
  333. }
  334. return el3_len + ol3_len + ol4_len + il3_len + il4_len +
  335. cfg_payload_len;
  336. }
  337. /* sender transmits encapsulated over RAW or unencap'd over UDP */
  338. static int setup_tx(void)
  339. {
  340. int family, fd, ret;
  341. if (cfg_l3_extra)
  342. family = cfg_l3_extra;
  343. else if (cfg_l3_outer)
  344. family = cfg_l3_outer;
  345. else
  346. family = cfg_l3_inner;
  347. fd = socket(family, SOCK_RAW, IPPROTO_RAW);
  348. if (fd == -1)
  349. error(1, errno, "socket tx");
  350. if (cfg_l3_extra) {
  351. if (cfg_l3_extra == PF_INET)
  352. ret = connect(fd, (void *) &extra_daddr4,
  353. sizeof(extra_daddr4));
  354. else
  355. ret = connect(fd, (void *) &extra_daddr6,
  356. sizeof(extra_daddr6));
  357. if (ret)
  358. error(1, errno, "connect tx");
  359. } else if (cfg_l3_outer) {
  360. /* connect to destination if not encapsulated */
  361. if (cfg_l3_outer == PF_INET)
  362. ret = connect(fd, (void *) &out_daddr4,
  363. sizeof(out_daddr4));
  364. else
  365. ret = connect(fd, (void *) &out_daddr6,
  366. sizeof(out_daddr6));
  367. if (ret)
  368. error(1, errno, "connect tx");
  369. } else {
  370. /* otherwise using loopback */
  371. if (cfg_l3_inner == PF_INET)
  372. ret = connect(fd, (void *) &in_daddr4,
  373. sizeof(in_daddr4));
  374. else
  375. ret = connect(fd, (void *) &in_daddr6,
  376. sizeof(in_daddr6));
  377. if (ret)
  378. error(1, errno, "connect tx");
  379. }
  380. return fd;
  381. }
  382. /* receiver reads unencapsulated UDP */
  383. static int setup_rx(void)
  384. {
  385. int fd, ret;
  386. fd = socket(cfg_l3_inner, SOCK_DGRAM, 0);
  387. if (fd == -1)
  388. error(1, errno, "socket rx");
  389. if (cfg_l3_inner == PF_INET)
  390. ret = bind(fd, (void *) &in_daddr4, sizeof(in_daddr4));
  391. else
  392. ret = bind(fd, (void *) &in_daddr6, sizeof(in_daddr6));
  393. if (ret)
  394. error(1, errno, "bind rx");
  395. return fd;
  396. }
  397. static int do_tx(int fd, const char *pkt, int len)
  398. {
  399. int ret;
  400. ret = write(fd, pkt, len);
  401. if (ret == -1)
  402. error(1, errno, "send");
  403. if (ret != len)
  404. error(1, errno, "send: len (%d < %d)\n", ret, len);
  405. return 1;
  406. }
  407. static int do_poll(int fd, short events, int timeout)
  408. {
  409. struct pollfd pfd;
  410. int ret;
  411. pfd.fd = fd;
  412. pfd.events = events;
  413. ret = poll(&pfd, 1, timeout);
  414. if (ret == -1)
  415. error(1, errno, "poll");
  416. if (ret && !(pfd.revents & POLLIN))
  417. error(1, errno, "poll: unexpected event 0x%x\n", pfd.revents);
  418. return ret;
  419. }
  420. static int do_rx(int fd)
  421. {
  422. char rbuf;
  423. int ret, num = 0;
  424. while (1) {
  425. ret = recv(fd, &rbuf, 1, MSG_DONTWAIT);
  426. if (ret == -1 && errno == EAGAIN)
  427. break;
  428. if (ret == -1)
  429. error(1, errno, "recv");
  430. if (rbuf != cfg_payload_char)
  431. error(1, 0, "recv: payload mismatch");
  432. num++;
  433. };
  434. return num;
  435. }
  436. static int do_main(void)
  437. {
  438. unsigned long tstop, treport, tcur;
  439. int fdt = -1, fdr = -1, len, tx = 0, rx = 0;
  440. if (!cfg_only_tx)
  441. fdr = setup_rx();
  442. if (!cfg_only_rx)
  443. fdt = setup_tx();
  444. len = build_packet();
  445. tcur = util_gettime();
  446. treport = tcur + 1000;
  447. tstop = tcur + (cfg_num_secs * 1000);
  448. while (1) {
  449. if (!cfg_only_rx)
  450. tx += do_tx(fdt, buf, len);
  451. if (!cfg_only_tx)
  452. rx += do_rx(fdr);
  453. if (cfg_num_secs) {
  454. tcur = util_gettime();
  455. if (tcur >= tstop)
  456. break;
  457. if (tcur >= treport) {
  458. fprintf(stderr, "pkts: tx=%u rx=%u\n", tx, rx);
  459. tx = 0;
  460. rx = 0;
  461. treport = tcur + 1000;
  462. }
  463. } else {
  464. if (tx == cfg_num_pkt)
  465. break;
  466. }
  467. }
  468. /* read straggler packets, if any */
  469. if (rx < tx) {
  470. tstop = util_gettime() + 100;
  471. while (rx < tx) {
  472. tcur = util_gettime();
  473. if (tcur >= tstop)
  474. break;
  475. do_poll(fdr, POLLIN, tstop - tcur);
  476. rx += do_rx(fdr);
  477. }
  478. }
  479. fprintf(stderr, "pkts: tx=%u rx=%u\n", tx, rx);
  480. if (fdr != -1 && close(fdr))
  481. error(1, errno, "close rx");
  482. if (fdt != -1 && close(fdt))
  483. error(1, errno, "close tx");
  484. /*
  485. * success (== 0) only if received all packets
  486. * unless failure is expected, in which case none must arrive.
  487. */
  488. if (cfg_expect_failure)
  489. return rx != 0;
  490. else
  491. return rx != tx;
  492. }
  493. static void __attribute__((noreturn)) usage(const char *filepath)
  494. {
  495. fprintf(stderr, "Usage: %s [-e gre|gue|bare|none] [-i 4|6] [-l len] "
  496. "[-O 4|6] [-o 4|6] [-n num] [-t secs] [-R] [-T] "
  497. "[-s <osrc> [-d <odst>] [-S <isrc>] [-D <idst>] "
  498. "[-x <otos>] [-X <itos>] [-f <isport>] [-F]\n",
  499. filepath);
  500. exit(1);
  501. }
  502. static void parse_addr(int family, void *addr, const char *optarg)
  503. {
  504. int ret;
  505. ret = inet_pton(family, optarg, addr);
  506. if (ret == -1)
  507. error(1, errno, "inet_pton");
  508. if (ret == 0)
  509. error(1, 0, "inet_pton: bad string");
  510. }
  511. static void parse_addr4(struct sockaddr_in *addr, const char *optarg)
  512. {
  513. parse_addr(AF_INET, &addr->sin_addr, optarg);
  514. }
  515. static void parse_addr6(struct sockaddr_in6 *addr, const char *optarg)
  516. {
  517. parse_addr(AF_INET6, &addr->sin6_addr, optarg);
  518. }
  519. static int parse_protocol_family(const char *filepath, const char *optarg)
  520. {
  521. if (!strcmp(optarg, "4"))
  522. return PF_INET;
  523. if (!strcmp(optarg, "6"))
  524. return PF_INET6;
  525. usage(filepath);
  526. }
  527. static void parse_opts(int argc, char **argv)
  528. {
  529. int c;
  530. while ((c = getopt(argc, argv, "d:D:e:f:Fhi:l:n:o:O:Rs:S:t:Tx:X:")) != -1) {
  531. switch (c) {
  532. case 'd':
  533. if (cfg_l3_outer == AF_UNSPEC)
  534. error(1, 0, "-d must be preceded by -o");
  535. if (cfg_l3_outer == AF_INET)
  536. parse_addr4(&out_daddr4, optarg);
  537. else
  538. parse_addr6(&out_daddr6, optarg);
  539. break;
  540. case 'D':
  541. if (cfg_l3_inner == AF_UNSPEC)
  542. error(1, 0, "-D must be preceded by -i");
  543. if (cfg_l3_inner == AF_INET)
  544. parse_addr4(&in_daddr4, optarg);
  545. else
  546. parse_addr6(&in_daddr6, optarg);
  547. break;
  548. case 'e':
  549. if (!strcmp(optarg, "gre"))
  550. cfg_encap_proto = IPPROTO_GRE;
  551. else if (!strcmp(optarg, "gue"))
  552. cfg_encap_proto = IPPROTO_UDP;
  553. else if (!strcmp(optarg, "bare"))
  554. cfg_encap_proto = IPPROTO_IPIP;
  555. else if (!strcmp(optarg, "none"))
  556. cfg_encap_proto = IPPROTO_IP; /* == 0 */
  557. else
  558. usage(argv[0]);
  559. break;
  560. case 'f':
  561. cfg_src_port = strtol(optarg, NULL, 0);
  562. break;
  563. case 'F':
  564. cfg_expect_failure = true;
  565. break;
  566. case 'h':
  567. usage(argv[0]);
  568. break;
  569. case 'i':
  570. if (!strcmp(optarg, "4"))
  571. cfg_l3_inner = PF_INET;
  572. else if (!strcmp(optarg, "6"))
  573. cfg_l3_inner = PF_INET6;
  574. else
  575. usage(argv[0]);
  576. break;
  577. case 'l':
  578. cfg_payload_len = strtol(optarg, NULL, 0);
  579. break;
  580. case 'n':
  581. cfg_num_pkt = strtol(optarg, NULL, 0);
  582. break;
  583. case 'o':
  584. cfg_l3_outer = parse_protocol_family(argv[0], optarg);
  585. break;
  586. case 'O':
  587. cfg_l3_extra = parse_protocol_family(argv[0], optarg);
  588. break;
  589. case 'R':
  590. cfg_only_rx = true;
  591. break;
  592. case 's':
  593. if (cfg_l3_outer == AF_INET)
  594. parse_addr4(&out_saddr4, optarg);
  595. else
  596. parse_addr6(&out_saddr6, optarg);
  597. break;
  598. case 'S':
  599. if (cfg_l3_inner == AF_INET)
  600. parse_addr4(&in_saddr4, optarg);
  601. else
  602. parse_addr6(&in_saddr6, optarg);
  603. break;
  604. case 't':
  605. cfg_num_secs = strtol(optarg, NULL, 0);
  606. break;
  607. case 'T':
  608. cfg_only_tx = true;
  609. break;
  610. case 'x':
  611. cfg_dsfield_outer = strtol(optarg, NULL, 0);
  612. break;
  613. case 'X':
  614. cfg_dsfield_inner = strtol(optarg, NULL, 0);
  615. break;
  616. }
  617. }
  618. if (cfg_only_rx && cfg_only_tx)
  619. error(1, 0, "options: cannot combine rx-only and tx-only");
  620. if (cfg_encap_proto && cfg_l3_outer == AF_UNSPEC)
  621. error(1, 0, "options: must specify outer with encap");
  622. else if ((!cfg_encap_proto) && cfg_l3_outer != AF_UNSPEC)
  623. error(1, 0, "options: cannot combine no-encap and outer");
  624. else if ((!cfg_encap_proto) && cfg_l3_extra != AF_UNSPEC)
  625. error(1, 0, "options: cannot combine no-encap and extra");
  626. if (cfg_l3_inner == AF_UNSPEC)
  627. cfg_l3_inner = AF_INET6;
  628. if (cfg_l3_inner == AF_INET6 && cfg_encap_proto == IPPROTO_IPIP)
  629. cfg_encap_proto = IPPROTO_IPV6;
  630. /* RFC 6040 4.2:
  631. * on decap, if outer encountered congestion (CE == 0x3),
  632. * but inner cannot encode ECN (NoECT == 0x0), then drop packet.
  633. */
  634. if (((cfg_dsfield_outer & 0x3) == 0x3) &&
  635. ((cfg_dsfield_inner & 0x3) == 0x0))
  636. cfg_expect_failure = true;
  637. }
  638. static void print_opts(void)
  639. {
  640. if (cfg_l3_inner == PF_INET6) {
  641. util_printaddr("inner.dest6", (void *) &in_daddr6);
  642. util_printaddr("inner.source6", (void *) &in_saddr6);
  643. } else {
  644. util_printaddr("inner.dest4", (void *) &in_daddr4);
  645. util_printaddr("inner.source4", (void *) &in_saddr4);
  646. }
  647. if (!cfg_l3_outer)
  648. return;
  649. fprintf(stderr, "encap proto: %u\n", cfg_encap_proto);
  650. if (cfg_l3_outer == PF_INET6) {
  651. util_printaddr("outer.dest6", (void *) &out_daddr6);
  652. util_printaddr("outer.source6", (void *) &out_saddr6);
  653. } else {
  654. util_printaddr("outer.dest4", (void *) &out_daddr4);
  655. util_printaddr("outer.source4", (void *) &out_saddr4);
  656. }
  657. if (!cfg_l3_extra)
  658. return;
  659. if (cfg_l3_outer == PF_INET6) {
  660. util_printaddr("extra.dest6", (void *) &extra_daddr6);
  661. util_printaddr("extra.source6", (void *) &extra_saddr6);
  662. } else {
  663. util_printaddr("extra.dest4", (void *) &extra_daddr4);
  664. util_printaddr("extra.source4", (void *) &extra_saddr4);
  665. }
  666. }
  667. int main(int argc, char **argv)
  668. {
  669. parse_opts(argc, argv);
  670. print_opts();
  671. return do_main();
  672. }