mod_tcp_opt.c 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. * mod_tcp_opt.c
  3. *
  4. * Copyright (c) 2001 Dug Song <dugsong@monkey.org>
  5. *
  6. * $Id: mod_tcp_opt.c 2000 2008-04-27 06:17:35Z aturner $
  7. */
  8. #include "config.h"
  9. #include <err.h>
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include "pkt.h"
  14. #include "mod.h"
  15. void *
  16. tcp_opt_close(void *d)
  17. {
  18. if (d != NULL)
  19. free(d);
  20. return (NULL);
  21. }
  22. void *
  23. tcp_opt_open(int argc, char *argv[])
  24. {
  25. struct tcp_opt *opt;
  26. int i;
  27. if (argc < 3)
  28. return (NULL);
  29. if ((opt = calloc(1, sizeof(*opt))) == NULL)
  30. return (NULL);
  31. if (strcasecmp(argv[1], "mss") == 0) {
  32. opt->opt_type = TCP_OPT_MSS;
  33. opt->opt_len = TCP_OPT_LEN + 2;
  34. if ((i = atoi(argv[2])) <= 0 || i > 0xffff) {
  35. warnx("mss <size> must be from 0-65535");
  36. return (tcp_opt_close(opt));
  37. }
  38. opt->opt_data.mss = htons(i);
  39. } else if (strcasecmp(argv[1], "wscale") == 0) {
  40. opt->opt_type = TCP_OPT_WSCALE;
  41. opt->opt_len = TCP_OPT_LEN + 2;
  42. if ((i = atoi(argv[2])) <= 0 || i > 0xff) {
  43. warnx("wscale <size> must be from 0-255");
  44. return (tcp_opt_close(opt));
  45. }
  46. opt->opt_data.wscale = i;
  47. } else
  48. return (tcp_opt_close(opt));
  49. return (opt);
  50. }
  51. int
  52. tcp_opt_apply(void *d, struct pktq *pktq)
  53. {
  54. struct tcp_opt *opt = (struct tcp_opt *)d;
  55. struct pkt *pkt;
  56. size_t len;
  57. TAILQ_FOREACH(pkt, pktq, pkt_next) {
  58. len = ip_add_option(pkt->pkt_ip,
  59. sizeof(pkt->pkt_data) - ETH_HDR_LEN,
  60. IP_PROTO_TCP, opt, opt->opt_len);
  61. if (len > 0) {
  62. pkt->pkt_end += len;
  63. pkt_decorate(pkt);
  64. ip_checksum(pkt->pkt_ip, pkt->pkt_end -
  65. pkt->pkt_eth_data);
  66. }
  67. }
  68. return (0);
  69. }
  70. struct mod mod_tcp_opt = {
  71. "tcp_opt", /* name */
  72. "tcp_opt mss|wscale <size>", /* usage */
  73. tcp_opt_open, /* open */
  74. tcp_opt_apply, /* apply */
  75. tcp_opt_close /* close */
  76. };