1 /*
2 * (C) 2005-2011 by Pablo Neira Ayuso <pablo@netfilter.org>
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 */
9
10 #include "internal/internal.h"
11
__parse_expect_message_type(const struct nlmsghdr * nlh)12 int __parse_expect_message_type(const struct nlmsghdr *nlh)
13 {
14 uint16_t type = NFNL_MSG_TYPE(nlh->nlmsg_type);
15 uint16_t flags = nlh->nlmsg_flags;
16 int ret = NFCT_T_UNKNOWN;
17
18 if (type == IPCTNL_MSG_EXP_NEW) {
19 if (flags & (NLM_F_CREATE|NLM_F_EXCL))
20 ret = NFCT_T_NEW;
21 else
22 ret = NFCT_T_UPDATE;
23 } else if (type == IPCTNL_MSG_EXP_DELETE)
24 ret = NFCT_T_DESTROY;
25
26 return ret;
27 }
28
__parse_expect(const struct nlmsghdr * nlh,struct nfattr * cda[],struct nf_expect * exp)29 void __parse_expect(const struct nlmsghdr *nlh,
30 struct nfattr *cda[],
31 struct nf_expect *exp)
32 {
33 struct nfgenmsg *nfhdr = NLMSG_DATA(nlh);
34
35 /* XXX: this is ugly, clean it up, please */
36 exp->expected.orig.l3protonum = nfhdr->nfgen_family;
37 set_bit(ATTR_ORIG_L3PROTO, exp->expected.set);
38
39 exp->mask.orig.l3protonum = nfhdr->nfgen_family;
40 set_bit(ATTR_ORIG_L3PROTO, exp->mask.set);
41
42 exp->master.orig.l3protonum = nfhdr->nfgen_family;
43 set_bit(ATTR_ORIG_L3PROTO, exp->master.set);
44
45 if (cda[CTA_EXPECT_MASTER-1]) {
46 __parse_tuple(cda[CTA_EXPECT_MASTER-1],
47 &exp->master.orig,
48 __DIR_ORIG,
49 exp->master.set);
50 set_bit(ATTR_EXP_MASTER, exp->set);
51 }
52 if (cda[CTA_EXPECT_TUPLE-1]) {
53 __parse_tuple(cda[CTA_EXPECT_TUPLE-1],
54 &exp->expected.orig,
55 __DIR_ORIG,
56 exp->expected.set);
57 set_bit(ATTR_EXP_EXPECTED, exp->set);
58 }
59 if (cda[CTA_EXPECT_MASK-1]) {
60 __parse_tuple(cda[CTA_EXPECT_MASK-1],
61 &exp->mask.orig,
62 __DIR_ORIG,
63 exp->mask.set);
64 set_bit(ATTR_EXP_MASK, exp->set);
65 }
66 if (cda[CTA_EXPECT_TIMEOUT-1]) {
67 exp->timeout =
68 ntohl(*(uint32_t *)NFA_DATA(cda[CTA_EXPECT_TIMEOUT-1]));
69 set_bit(ATTR_EXP_TIMEOUT, exp->set);
70 }
71
72 if (cda[CTA_EXPECT_ZONE-1]) {
73 exp->zone =
74 ntohs(*(uint16_t *)NFA_DATA(cda[CTA_EXPECT_ZONE-1]));
75 set_bit(ATTR_EXP_ZONE, exp->set);
76 }
77 if (cda[CTA_EXPECT_FLAGS-1]) {
78 exp->flags =
79 ntohl(*(uint32_t *)NFA_DATA(cda[CTA_EXPECT_FLAGS-1]));
80 set_bit(ATTR_EXP_FLAGS, exp->set);
81 }
82 if (cda[CTA_EXPECT_HELP_NAME-1]) {
83 strncpy(exp->helper_name, NFA_DATA(cda[CTA_EXPECT_HELP_NAME-1]),
84 NFA_PAYLOAD(cda[CTA_EXPECT_HELP_NAME-1]));
85 set_bit(ATTR_EXP_HELPER_NAME, exp->set);
86 }
87 if (cda[CTA_EXPECT_CLASS-1]) {
88 exp->class =
89 ntohl(*(uint32_t *)NFA_DATA(cda[CTA_EXPECT_CLASS-1]));
90 set_bit(ATTR_EXP_CLASS, exp->set);
91 }
92 if (cda[CTA_EXPECT_NAT-1]) {
93 struct nfattr *tb[CTA_EXPECT_NAT_MAX];
94
95 exp->nat.orig.l3protonum = nfhdr->nfgen_family;
96 set_bit(ATTR_ORIG_L3PROTO, exp->nat.set);
97
98 nfnl_parse_nested(tb, CTA_EXPECT_NAT_MAX,
99 cda[CTA_EXPECT_NAT-1]);
100
101 if (tb[CTA_EXPECT_NAT_TUPLE-1]) {
102 __parse_tuple(tb[CTA_EXPECT_NAT_TUPLE-1],
103 &exp->nat.orig,
104 __DIR_ORIG,
105 exp->nat.set);
106 set_bit(ATTR_EXP_NAT_TUPLE, exp->set);
107 }
108 if (tb[CTA_EXPECT_NAT_DIR-1]) {
109 exp->nat_dir =
110 ntohl(*((uint32_t *)
111 NFA_DATA(tb[CTA_EXPECT_NAT_DIR-1])));
112 set_bit(ATTR_EXP_NAT_DIR, exp->set);
113 }
114 }
115 if (cda[CTA_EXPECT_FN-1]) {
116 strcpy(exp->expectfn, NFA_DATA(cda[CTA_EXPECT_FN-1]));
117 exp->expectfn[__NFCT_EXPECTFN_MAX-1] = '\0';
118 set_bit(ATTR_EXP_FN, exp->set);
119 }
120 }
121