2 * Copyright (c) 2008-2009 Patrick McHardy <kaber@trash.net>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
8 * Development of this code funded by Astaro AG (http://www.astaro.com/)
11 #include <linux/kernel.h>
12 #include <linux/if_vlan.h>
13 #include <linux/init.h>
14 #include <linux/module.h>
15 #include <linux/netlink.h>
16 #include <linux/netfilter.h>
17 #include <linux/netfilter/nf_tables.h>
18 #include <net/netfilter/nf_tables_core.h>
19 #include <net/netfilter/nf_tables.h>
21 /* add vlan header into the user buffer for if tag was removed by offloads */
23 nft_payload_copy_vlan(u32
*d
, const struct sk_buff
*skb
, u8 offset
, u8 len
)
25 int mac_off
= skb_mac_header(skb
) - skb
->data
;
26 u8 vlan_len
, *vlanh
, *dst_u8
= (u8
*) d
;
27 struct vlan_ethhdr veth
;
30 if (offset
< ETH_HLEN
) {
31 u8 ethlen
= min_t(u8
, len
, ETH_HLEN
- offset
);
33 if (skb_copy_bits(skb
, mac_off
, &veth
, ETH_HLEN
))
36 veth
.h_vlan_proto
= skb
->vlan_proto
;
38 memcpy(dst_u8
, vlanh
+ offset
, ethlen
);
46 } else if (offset
>= VLAN_ETH_HLEN
) {
51 veth
.h_vlan_TCI
= htons(skb_vlan_tag_get(skb
));
52 veth
.h_vlan_encapsulated_proto
= skb
->protocol
;
56 vlan_len
= min_t(u8
, len
, VLAN_ETH_HLEN
- offset
);
57 memcpy(dst_u8
, vlanh
, vlan_len
);
65 return skb_copy_bits(skb
, offset
+ mac_off
, dst_u8
, len
) == 0;
68 static void nft_payload_eval(const struct nft_expr
*expr
,
69 struct nft_regs
*regs
,
70 const struct nft_pktinfo
*pkt
)
72 const struct nft_payload
*priv
= nft_expr_priv(expr
);
73 const struct sk_buff
*skb
= pkt
->skb
;
74 u32
*dest
= ®s
->data
[priv
->dreg
];
77 dest
[priv
->len
/ NFT_REG32_SIZE
] = 0;
79 case NFT_PAYLOAD_LL_HEADER
:
80 if (!skb_mac_header_was_set(skb
))
83 if (skb_vlan_tag_present(skb
)) {
84 if (!nft_payload_copy_vlan(dest
, skb
,
85 priv
->offset
, priv
->len
))
89 offset
= skb_mac_header(skb
) - skb
->data
;
91 case NFT_PAYLOAD_NETWORK_HEADER
:
92 offset
= skb_network_offset(skb
);
94 case NFT_PAYLOAD_TRANSPORT_HEADER
:
95 offset
= pkt
->xt
.thoff
;
100 offset
+= priv
->offset
;
102 if (skb_copy_bits(skb
, offset
, dest
, priv
->len
) < 0)
106 regs
->verdict
.code
= NFT_BREAK
;
109 static const struct nla_policy nft_payload_policy
[NFTA_PAYLOAD_MAX
+ 1] = {
110 [NFTA_PAYLOAD_DREG
] = { .type
= NLA_U32
},
111 [NFTA_PAYLOAD_BASE
] = { .type
= NLA_U32
},
112 [NFTA_PAYLOAD_OFFSET
] = { .type
= NLA_U32
},
113 [NFTA_PAYLOAD_LEN
] = { .type
= NLA_U32
},
116 static int nft_payload_init(const struct nft_ctx
*ctx
,
117 const struct nft_expr
*expr
,
118 const struct nlattr
* const tb
[])
120 struct nft_payload
*priv
= nft_expr_priv(expr
);
122 priv
->base
= ntohl(nla_get_be32(tb
[NFTA_PAYLOAD_BASE
]));
123 priv
->offset
= ntohl(nla_get_be32(tb
[NFTA_PAYLOAD_OFFSET
]));
124 priv
->len
= ntohl(nla_get_be32(tb
[NFTA_PAYLOAD_LEN
]));
125 priv
->dreg
= nft_parse_register(tb
[NFTA_PAYLOAD_DREG
]);
127 return nft_validate_register_store(ctx
, priv
->dreg
, NULL
,
128 NFT_DATA_VALUE
, priv
->len
);
131 static int nft_payload_dump(struct sk_buff
*skb
, const struct nft_expr
*expr
)
133 const struct nft_payload
*priv
= nft_expr_priv(expr
);
135 if (nft_dump_register(skb
, NFTA_PAYLOAD_DREG
, priv
->dreg
) ||
136 nla_put_be32(skb
, NFTA_PAYLOAD_BASE
, htonl(priv
->base
)) ||
137 nla_put_be32(skb
, NFTA_PAYLOAD_OFFSET
, htonl(priv
->offset
)) ||
138 nla_put_be32(skb
, NFTA_PAYLOAD_LEN
, htonl(priv
->len
)))
139 goto nla_put_failure
;
146 static struct nft_expr_type nft_payload_type
;
147 static const struct nft_expr_ops nft_payload_ops
= {
148 .type
= &nft_payload_type
,
149 .size
= NFT_EXPR_SIZE(sizeof(struct nft_payload
)),
150 .eval
= nft_payload_eval
,
151 .init
= nft_payload_init
,
152 .dump
= nft_payload_dump
,
155 const struct nft_expr_ops nft_payload_fast_ops
= {
156 .type
= &nft_payload_type
,
157 .size
= NFT_EXPR_SIZE(sizeof(struct nft_payload
)),
158 .eval
= nft_payload_eval
,
159 .init
= nft_payload_init
,
160 .dump
= nft_payload_dump
,
163 static const struct nft_expr_ops
*
164 nft_payload_select_ops(const struct nft_ctx
*ctx
,
165 const struct nlattr
* const tb
[])
167 enum nft_payload_bases base
;
168 unsigned int offset
, len
;
170 if (tb
[NFTA_PAYLOAD_DREG
] == NULL
||
171 tb
[NFTA_PAYLOAD_BASE
] == NULL
||
172 tb
[NFTA_PAYLOAD_OFFSET
] == NULL
||
173 tb
[NFTA_PAYLOAD_LEN
] == NULL
)
174 return ERR_PTR(-EINVAL
);
176 base
= ntohl(nla_get_be32(tb
[NFTA_PAYLOAD_BASE
]));
178 case NFT_PAYLOAD_LL_HEADER
:
179 case NFT_PAYLOAD_NETWORK_HEADER
:
180 case NFT_PAYLOAD_TRANSPORT_HEADER
:
183 return ERR_PTR(-EOPNOTSUPP
);
186 offset
= ntohl(nla_get_be32(tb
[NFTA_PAYLOAD_OFFSET
]));
187 len
= ntohl(nla_get_be32(tb
[NFTA_PAYLOAD_LEN
]));
189 if (len
<= 4 && is_power_of_2(len
) && IS_ALIGNED(offset
, len
) &&
190 base
!= NFT_PAYLOAD_LL_HEADER
)
191 return &nft_payload_fast_ops
;
193 return &nft_payload_ops
;
196 static struct nft_expr_type nft_payload_type __read_mostly
= {
198 .select_ops
= nft_payload_select_ops
,
199 .policy
= nft_payload_policy
,
200 .maxattr
= NFTA_PAYLOAD_MAX
,
201 .owner
= THIS_MODULE
,
204 int __init
nft_payload_module_init(void)
206 return nft_register_expr(&nft_payload_type
);
209 void nft_payload_module_exit(void)
211 nft_unregister_expr(&nft_payload_type
);