mod_tcp_opt.c 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*
  2. * mod_tcp_opt.c
  3. *
  4. * Copyright (c) 2001 Dug Song <dugsong@monkey.org>
  5. *
  6. * $Id$
  7. */
  8. #include "config.h"
  9. #include "defines.h"
  10. #include "common.h"
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include "pkt.h"
  15. #include "mod.h"
  16. #include "iputil.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 = atoi(argv[2])) <= 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 = atoi(argv[2])) <= 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, &argv[2], &opt->opt_type, &opt->opt_len,
  51. &opt->opt_data.data8[0], sizeof(opt->opt_data.data8)) != 0)
  52. return (tcp_opt_close(opt));
  53. } else
  54. return (tcp_opt_close(opt));
  55. return (opt);
  56. }
  57. int
  58. tcp_opt_apply(void *d, struct pktq *pktq)
  59. {
  60. struct tcp_opt *opt = (struct tcp_opt *)d;
  61. struct pkt *pkt;
  62. TAILQ_FOREACH(pkt, pktq, pkt_next) {
  63. size_t len;
  64. uint16_t eth_type = htons(pkt->pkt_eth->eth_type);
  65. len = inet_add_option(eth_type, pkt->pkt_ip,
  66. sizeof(pkt->pkt_data) - ETH_HDR_LEN,
  67. IP_PROTO_TCP, opt, opt->opt_len);
  68. if (len > 0) {
  69. pkt->pkt_end += len;
  70. pkt_decorate(pkt);
  71. inet_checksum(eth_type, pkt->pkt_ip, pkt->pkt_end - pkt->pkt_eth_data);
  72. }
  73. }
  74. return (0);
  75. }
  76. struct mod mod_tcp_opt = {
  77. "tcp_opt", /* name */
  78. "tcp_opt mss|wscale <size>|raw <byte stream>", /* usage */
  79. tcp_opt_open, /* open */
  80. tcp_opt_apply, /* apply */
  81. tcp_opt_close /* close */
  82. };