blob: b9f39ee4a14348cb546f853f73e19cd3cb928eb8 [file] [log] [blame]
Jan Engelhardt2e73af92011-04-19 15:44:48 +02001#include <stdint.h>
Marc Bouchere6869a82000-03-20 06:03:29 +00002#include <stdio.h>
3#include <netdb.h>
Jan Engelhardt2e73af92011-04-19 15:44:48 +02004#include <arpa/inet.h>
Yasuyuki KOZAKAI17908e42007-07-24 06:56:21 +00005#include <xtables.h>
6#include <linux/netfilter/xt_tcpudp.h>
Marc Bouchere6869a82000-03-20 06:03:29 +00007
Jan Engelhardt2e73af92011-04-19 15:44:48 +02008enum {
9 O_SOURCE_PORT = 0,
10 O_DEST_PORT,
11};
12
Jan Engelhardt181dead2007-10-04 16:27:07 +000013static void udp_help(void)
Marc Bouchere6869a82000-03-20 06:03:29 +000014{
15 printf(
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +020016"udp match options:\n"
Jan Engelhardt96727922008-08-13 14:42:41 +020017"[!] --source-port port[:port]\n"
Marc Bouchere6869a82000-03-20 06:03:29 +000018" --sport ...\n"
19" match source port(s)\n"
Jan Engelhardt96727922008-08-13 14:42:41 +020020"[!] --destination-port port[:port]\n"
Marc Bouchere6869a82000-03-20 06:03:29 +000021" --dport ...\n"
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +020022" match destination port(s)\n");
Marc Bouchere6869a82000-03-20 06:03:29 +000023}
24
Jan Engelhardt2e73af92011-04-19 15:44:48 +020025#define s struct xt_udp
26static const struct xt_option_entry udp_opts[] = {
27 {.name = "source-port", .id = O_SOURCE_PORT, .type = XTTYPE_PORTRC,
28 .flags = XTOPT_INVERT | XTOPT_PUT, XTOPT_POINTER(s, spts)},
29 {.name = "sport", .id = O_SOURCE_PORT, .type = XTTYPE_PORTRC,
30 .flags = XTOPT_INVERT | XTOPT_PUT, XTOPT_POINTER(s, spts)},
31 {.name = "destination-port", .id = O_DEST_PORT, .type = XTTYPE_PORTRC,
32 .flags = XTOPT_INVERT | XTOPT_PUT, XTOPT_POINTER(s, dpts)},
33 {.name = "dport", .id = O_DEST_PORT, .type = XTTYPE_PORTRC,
34 .flags = XTOPT_INVERT | XTOPT_PUT, XTOPT_POINTER(s, dpts)},
35 XTOPT_TABLEEND,
Marc Bouchere6869a82000-03-20 06:03:29 +000036};
Jan Engelhardt2e73af92011-04-19 15:44:48 +020037#undef s
Marc Bouchere6869a82000-03-20 06:03:29 +000038
Jan Engelhardt181dead2007-10-04 16:27:07 +000039static void udp_init(struct xt_entry_match *m)
Marc Bouchere6869a82000-03-20 06:03:29 +000040{
Yasuyuki KOZAKAI17908e42007-07-24 06:56:21 +000041 struct xt_udp *udpinfo = (struct xt_udp *)m->data;
Marc Bouchere6869a82000-03-20 06:03:29 +000042
43 udpinfo->spts[1] = udpinfo->dpts[1] = 0xFFFF;
44}
45
Jan Engelhardt2e73af92011-04-19 15:44:48 +020046static void udp_parse(struct xt_option_call *cb)
Marc Bouchere6869a82000-03-20 06:03:29 +000047{
Jan Engelhardt2e73af92011-04-19 15:44:48 +020048 struct xt_udp *udpinfo = cb->data;
Marc Bouchere6869a82000-03-20 06:03:29 +000049
Jan Engelhardt2e73af92011-04-19 15:44:48 +020050 xtables_option_parse(cb);
51 switch (cb->entry->id) {
52 case O_SOURCE_PORT:
53 if (cb->invert)
Yasuyuki KOZAKAI17908e42007-07-24 06:56:21 +000054 udpinfo->invflags |= XT_UDP_INV_SRCPT;
Marc Bouchere6869a82000-03-20 06:03:29 +000055 break;
Jan Engelhardt2e73af92011-04-19 15:44:48 +020056 case O_DEST_PORT:
57 if (cb->invert)
Yasuyuki KOZAKAI17908e42007-07-24 06:56:21 +000058 udpinfo->invflags |= XT_UDP_INV_DSTPT;
Marc Bouchere6869a82000-03-20 06:03:29 +000059 break;
Marc Bouchere6869a82000-03-20 06:03:29 +000060 }
Marc Bouchere6869a82000-03-20 06:03:29 +000061}
62
Jan Engelhardtdd6e4b92011-05-07 00:05:24 +020063static const char *
Marc Bouchere6869a82000-03-20 06:03:29 +000064port_to_service(int port)
65{
Jan Engelhardtdd6e4b92011-05-07 00:05:24 +020066 const struct servent *service;
Marc Bouchere6869a82000-03-20 06:03:29 +000067
68 if ((service = getservbyport(htons(port), "udp")))
69 return service->s_name;
70
71 return NULL;
72}
73
74static void
Jan Engelhardt7ac40522011-01-07 12:34:04 +010075print_port(uint16_t port, int numeric)
Marc Bouchere6869a82000-03-20 06:03:29 +000076{
Jan Engelhardtdd6e4b92011-05-07 00:05:24 +020077 const char *service;
Marc Bouchere6869a82000-03-20 06:03:29 +000078
79 if (numeric || (service = port_to_service(port)) == NULL)
80 printf("%u", port);
81 else
82 printf("%s", service);
83}
84
85static void
Jan Engelhardt7ac40522011-01-07 12:34:04 +010086print_ports(const char *name, uint16_t min, uint16_t max,
Marc Bouchere6869a82000-03-20 06:03:29 +000087 int invert, int numeric)
88{
89 const char *inv = invert ? "!" : "";
90
91 if (min != 0 || max != 0xFFFF || invert) {
Jan Engelhardt73866352010-12-18 02:04:59 +010092 printf(" %s", name);
Marc Bouchere6869a82000-03-20 06:03:29 +000093 if (min == max) {
94 printf(":%s", inv);
95 print_port(min, numeric);
96 } else {
97 printf("s:%s", inv);
98 print_port(min, numeric);
99 printf(":");
100 print_port(max, numeric);
101 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000102 }
103}
104
Marc Bouchere6869a82000-03-20 06:03:29 +0000105static void
Jan Engelhardt181dead2007-10-04 16:27:07 +0000106udp_print(const void *ip, const struct xt_entry_match *match, int numeric)
Marc Bouchere6869a82000-03-20 06:03:29 +0000107{
Yasuyuki KOZAKAI17908e42007-07-24 06:56:21 +0000108 const struct xt_udp *udp = (struct xt_udp *)match->data;
Marc Bouchere6869a82000-03-20 06:03:29 +0000109
Jan Engelhardt73866352010-12-18 02:04:59 +0100110 printf(" udp");
Marc Bouchere6869a82000-03-20 06:03:29 +0000111 print_ports("spt", udp->spts[0], udp->spts[1],
Yasuyuki KOZAKAI17908e42007-07-24 06:56:21 +0000112 udp->invflags & XT_UDP_INV_SRCPT,
Marc Bouchere6869a82000-03-20 06:03:29 +0000113 numeric);
114 print_ports("dpt", udp->dpts[0], udp->dpts[1],
Yasuyuki KOZAKAI17908e42007-07-24 06:56:21 +0000115 udp->invflags & XT_UDP_INV_DSTPT,
Marc Bouchere6869a82000-03-20 06:03:29 +0000116 numeric);
Yasuyuki KOZAKAI17908e42007-07-24 06:56:21 +0000117 if (udp->invflags & ~XT_UDP_INV_MASK)
Jan Engelhardt73866352010-12-18 02:04:59 +0100118 printf(" Unknown invflags: 0x%X",
Yasuyuki KOZAKAI17908e42007-07-24 06:56:21 +0000119 udp->invflags & ~XT_UDP_INV_MASK);
Marc Bouchere6869a82000-03-20 06:03:29 +0000120}
121
Jan Engelhardt181dead2007-10-04 16:27:07 +0000122static void udp_save(const void *ip, const struct xt_entry_match *match)
Marc Bouchere6869a82000-03-20 06:03:29 +0000123{
Yasuyuki KOZAKAI17908e42007-07-24 06:56:21 +0000124 const struct xt_udp *udpinfo = (struct xt_udp *)match->data;
Marc Bouchere6869a82000-03-20 06:03:29 +0000125
126 if (udpinfo->spts[0] != 0
Rusty Russell73f72f52000-07-03 10:17:57 +0000127 || udpinfo->spts[1] != 0xFFFF) {
Yasuyuki KOZAKAI17908e42007-07-24 06:56:21 +0000128 if (udpinfo->invflags & XT_UDP_INV_SRCPT)
Jan Engelhardt73866352010-12-18 02:04:59 +0100129 printf(" !");
Marc Bouchere6869a82000-03-20 06:03:29 +0000130 if (udpinfo->spts[0]
131 != udpinfo->spts[1])
Jan Engelhardt73866352010-12-18 02:04:59 +0100132 printf(" --sport %u:%u",
Marc Boucher9f2009c2000-04-07 17:30:28 +0000133 udpinfo->spts[0],
Marc Boucher2382c8c2000-04-07 17:32:49 +0000134 udpinfo->spts[1]);
Marc Bouchere6869a82000-03-20 06:03:29 +0000135 else
Jan Engelhardt73866352010-12-18 02:04:59 +0100136 printf(" --sport %u",
Marc Boucher9f2009c2000-04-07 17:30:28 +0000137 udpinfo->spts[0]);
Marc Bouchere6869a82000-03-20 06:03:29 +0000138 }
139
140 if (udpinfo->dpts[0] != 0
Rusty Russell73f72f52000-07-03 10:17:57 +0000141 || udpinfo->dpts[1] != 0xFFFF) {
Yasuyuki KOZAKAI17908e42007-07-24 06:56:21 +0000142 if (udpinfo->invflags & XT_UDP_INV_DSTPT)
Jan Engelhardt73866352010-12-18 02:04:59 +0100143 printf(" !");
Marc Bouchere6869a82000-03-20 06:03:29 +0000144 if (udpinfo->dpts[0]
145 != udpinfo->dpts[1])
Jan Engelhardt73866352010-12-18 02:04:59 +0100146 printf(" --dport %u:%u",
Marc Boucher9f2009c2000-04-07 17:30:28 +0000147 udpinfo->dpts[0],
148 udpinfo->dpts[1]);
Marc Bouchere6869a82000-03-20 06:03:29 +0000149 else
Jan Engelhardt73866352010-12-18 02:04:59 +0100150 printf(" --dport %u",
Marc Boucher9f2009c2000-04-07 17:30:28 +0000151 udpinfo->dpts[0]);
Marc Bouchere6869a82000-03-20 06:03:29 +0000152 }
153}
154
Jan Engelhardt181dead2007-10-04 16:27:07 +0000155static struct xtables_match udp_match = {
Jan Engelhardtc5e85732009-06-12 20:55:44 +0200156 .family = NFPROTO_UNSPEC,
Yasuyuki KOZAKAI17908e42007-07-24 06:56:21 +0000157 .name = "udp",
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200158 .version = XTABLES_VERSION,
Yasuyuki KOZAKAI17908e42007-07-24 06:56:21 +0000159 .size = XT_ALIGN(sizeof(struct xt_udp)),
160 .userspacesize = XT_ALIGN(sizeof(struct xt_udp)),
Jan Engelhardt181dead2007-10-04 16:27:07 +0000161 .help = udp_help,
162 .init = udp_init,
Jan Engelhardt181dead2007-10-04 16:27:07 +0000163 .print = udp_print,
164 .save = udp_save,
Jan Engelhardt2e73af92011-04-19 15:44:48 +0200165 .x6_parse = udp_parse,
166 .x6_options = udp_opts,
Marc Bouchere6869a82000-03-20 06:03:29 +0000167};
168
169void
170_init(void)
171{
Jan Engelhardt181dead2007-10-04 16:27:07 +0000172 xtables_register_match(&udp_match);
Marc Bouchere6869a82000-03-20 06:03:29 +0000173}