mod_tcp_opt.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. * mod_tcp_opt.c
  3. *
  4. * Copyright (c) 2001 Dug Song <dugsong@monkey.org>
  5. *
  6. * $Id$
  7. */
  8. #include "defines.h"
  9. #include "config.h"
  10. #include "common.h"
  11. #include "iputil.h"
  12. #include "mod.h"
  13. #include "pkt.h"
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <string.h>
  17. void *
  18. tcp_opt_close(void *d)
  19. {
  20. if (d != NULL)
  21. free(d);
  22. return (NULL);
  23. }
  24. void *
  25. tcp_opt_open(int argc, char *argv[])
  26. {
  27. struct tcp_opt *opt;
  28. int i;
  29. if (argc < 3)
  30. return (NULL);
  31. if ((opt = calloc(1, sizeof(*opt))) == NULL)
  32. return (NULL);
  33. if (strcasecmp(argv[1], "mss") == 0) {
  34. opt->opt_type = TCP_OPT_MSS;
  35. opt->opt_len = TCP_OPT_LEN + 2;
  36. if ((i = (int)strtol(argv[2], NULL, 10)) <= 0 || i > 0xffff) {
  37. warn("mss <size> must be from 0-65535");
  38. return (tcp_opt_close(opt));
  39. }
  40. opt->opt_data.mss = htons(i);
  41. } else if (strcasecmp(argv[1], "wscale") == 0) {
  42. opt->opt_type = TCP_OPT_WSCALE;
  43. opt->opt_len = TCP_OPT_LEN + 2;
  44. if ((i = (int)strtol(argv[2], NULL, 10)) <= 0 || i > 0xff) {
  45. warn("wscale <size> must be from 0-255");
  46. return (tcp_opt_close(opt));
  47. }
  48. opt->opt_data.wscale = i;
  49. } else if (strcasecmp(argv[1], "raw") == 0) {
  50. if (raw_ip_opt_parse(argc - 2,
  51. &argv[2],
  52. &opt->opt_type,
  53. &opt->opt_len,
  54. &opt->opt_data.data8[0],
  55. sizeof(opt->opt_data.data8)) != 0)
  56. return (tcp_opt_close(opt));
  57. } else
  58. return (tcp_opt_close(opt));
  59. return (opt);
  60. }
  61. int
  62. tcp_opt_apply(void *d, struct pktq *pktq)
  63. {
  64. struct tcp_opt *opt = (struct tcp_opt *)d;
  65. struct pkt *pkt;
  66. TAILQ_FOREACH(pkt, pktq, pkt_next)
  67. {
  68. size_t len;
  69. uint16_t eth_type = htons(pkt->pkt_eth->eth_type);
  70. len = inet_add_option(eth_type,
  71. pkt->pkt_ip,
  72. sizeof(pkt->pkt_data) - ETH_HDR_LEN,
  73. IP_PROTO_TCP,
  74. opt,
  75. opt->opt_len);
  76. if (len > 0) {
  77. pkt->pkt_end += len;
  78. pkt_decorate(pkt);
  79. inet_checksum(eth_type, pkt->pkt_ip, pkt->pkt_end - pkt->pkt_eth_data);
  80. }
  81. }
  82. return (0);
  83. }
  84. struct mod mod_tcp_opt = {
  85. "tcp_opt", /* name */
  86. "tcp_opt mss|wscale <size>|raw <byte stream>", /* usage */
  87. tcp_opt_open, /* open */
  88. tcp_opt_apply, /* apply */
  89. tcp_opt_close /* close */
  90. };