Home | History | Annotate | Download | only in extensions
      1 /* Shared library add-on to iptables to add TCPMSS target support.
      2  *
      3  * Copyright (c) 2000 Marc Boucher
      4 */
      5 #include "config.h"
      6 #include <stdio.h>
      7 #include <xtables.h>
      8 #include <netinet/ip.h>
      9 #include <linux/netfilter/xt_TCPMSS.h>
     10 
     11 enum {
     12 	O_SET_MSS = 0,
     13 	O_CLAMP_MSS,
     14 };
     15 
     16 struct mssinfo {
     17 	struct xt_entry_target t;
     18 	struct xt_tcpmss_info mss;
     19 };
     20 
     21 static void __TCPMSS_help(int hdrsize)
     22 {
     23 	printf(
     24 "TCPMSS target mutually-exclusive options:\n"
     25 "  --set-mss value               explicitly set MSS option to specified value\n"
     26 "  --clamp-mss-to-pmtu           automatically clamp MSS value to (path_MTU - %d)\n",
     27 hdrsize);
     28 }
     29 
     30 static void TCPMSS_help(void)
     31 {
     32 	__TCPMSS_help(sizeof(struct iphdr));
     33 }
     34 
     35 static void TCPMSS_help6(void)
     36 {
     37 	__TCPMSS_help(SIZEOF_STRUCT_IP6_HDR);
     38 }
     39 
     40 static const struct xt_option_entry TCPMSS4_opts[] = {
     41 	{.name = "set-mss", .id = O_SET_MSS, .type = XTTYPE_UINT16,
     42 	 .min = 0, .max = UINT16_MAX - sizeof(struct iphdr),
     43 	 .flags = XTOPT_PUT, XTOPT_POINTER(struct xt_tcpmss_info, mss)},
     44 	{.name = "clamp-mss-to-pmtu", .id = O_CLAMP_MSS, .type = XTTYPE_NONE},
     45 	XTOPT_TABLEEND,
     46 };
     47 
     48 static const struct xt_option_entry TCPMSS6_opts[] = {
     49 	{.name = "set-mss", .id = O_SET_MSS, .type = XTTYPE_UINT16,
     50 	 .min = 0, .max = UINT16_MAX - SIZEOF_STRUCT_IP6_HDR,
     51 	 .flags = XTOPT_PUT, XTOPT_POINTER(struct xt_tcpmss_info, mss)},
     52 	{.name = "clamp-mss-to-pmtu", .id = O_CLAMP_MSS, .type = XTTYPE_NONE},
     53 	XTOPT_TABLEEND,
     54 };
     55 
     56 static void TCPMSS_parse(struct xt_option_call *cb)
     57 {
     58 	struct xt_tcpmss_info *mssinfo = cb->data;
     59 
     60 	xtables_option_parse(cb);
     61 	if (cb->entry->id == O_CLAMP_MSS)
     62 		mssinfo->mss = XT_TCPMSS_CLAMP_PMTU;
     63 }
     64 
     65 static void TCPMSS_check(struct xt_fcheck_call *cb)
     66 {
     67 	if (cb->xflags == 0)
     68 		xtables_error(PARAMETER_PROBLEM,
     69 		           "TCPMSS target: At least one parameter is required");
     70 }
     71 
     72 static void TCPMSS_print(const void *ip, const struct xt_entry_target *target,
     73                          int numeric)
     74 {
     75 	const struct xt_tcpmss_info *mssinfo =
     76 		(const struct xt_tcpmss_info *)target->data;
     77 	if(mssinfo->mss == XT_TCPMSS_CLAMP_PMTU)
     78 		printf(" TCPMSS clamp to PMTU");
     79 	else
     80 		printf(" TCPMSS set %u", mssinfo->mss);
     81 }
     82 
     83 static void TCPMSS_save(const void *ip, const struct xt_entry_target *target)
     84 {
     85 	const struct xt_tcpmss_info *mssinfo =
     86 		(const struct xt_tcpmss_info *)target->data;
     87 
     88 	if(mssinfo->mss == XT_TCPMSS_CLAMP_PMTU)
     89 		printf(" --clamp-mss-to-pmtu");
     90 	else
     91 		printf(" --set-mss %u", mssinfo->mss);
     92 }
     93 
     94 static struct xtables_target tcpmss_tg_reg[] = {
     95 	{
     96 		.family        = NFPROTO_IPV4,
     97 		.name          = "TCPMSS",
     98 		.version       = XTABLES_VERSION,
     99 		.size          = XT_ALIGN(sizeof(struct xt_tcpmss_info)),
    100 		.userspacesize = XT_ALIGN(sizeof(struct xt_tcpmss_info)),
    101 		.help          = TCPMSS_help,
    102 		.print         = TCPMSS_print,
    103 		.save          = TCPMSS_save,
    104 		.x6_parse      = TCPMSS_parse,
    105 		.x6_fcheck     = TCPMSS_check,
    106 		.x6_options    = TCPMSS4_opts,
    107 	},
    108 	{
    109 		.family        = NFPROTO_IPV6,
    110 		.name          = "TCPMSS",
    111 		.version       = XTABLES_VERSION,
    112 		.size          = XT_ALIGN(sizeof(struct xt_tcpmss_info)),
    113 		.userspacesize = XT_ALIGN(sizeof(struct xt_tcpmss_info)),
    114 		.help          = TCPMSS_help6,
    115 		.print         = TCPMSS_print,
    116 		.save          = TCPMSS_save,
    117 		.x6_parse      = TCPMSS_parse,
    118 		.x6_fcheck     = TCPMSS_check,
    119 		.x6_options    = TCPMSS6_opts,
    120 	},
    121 };
    122 
    123 void _init(void)
    124 {
    125 	xtables_register_targets(tcpmss_tg_reg, ARRAY_SIZE(tcpmss_tg_reg));
    126 }
    127