2 * Copyright (c) 2008 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 <asm/unaligned.h>
12 #include <linux/kernel.h>
13 #include <linux/netlink.h>
14 #include <linux/netfilter.h>
15 #include <linux/netfilter/nf_tables.h>
16 #include <net/netfilter/nf_tables_core.h>
17 #include <net/netfilter/nf_tables.h>
25 enum nft_registers dreg
:8;
26 enum nft_registers sreg
:8;
30 static unsigned int optlen(const u8
*opt
, unsigned int offset
)
32 /* Beware zero-length options: make finite progress */
33 if (opt
[offset
] <= TCPOPT_NOP
|| opt
[offset
+ 1] == 0)
36 return opt
[offset
+ 1];
39 static void nft_exthdr_ipv6_eval(const struct nft_expr
*expr
,
40 struct nft_regs
*regs
,
41 const struct nft_pktinfo
*pkt
)
43 struct nft_exthdr
*priv
= nft_expr_priv(expr
);
44 u32
*dest
= ®s
->data
[priv
->dreg
];
45 unsigned int offset
= 0;
48 err
= ipv6_find_hdr(pkt
->skb
, &offset
, priv
->type
, NULL
, NULL
);
49 if (priv
->flags
& NFT_EXTHDR_F_PRESENT
) {
55 offset
+= priv
->offset
;
57 dest
[priv
->len
/ NFT_REG32_SIZE
] = 0;
58 if (skb_copy_bits(pkt
->skb
, offset
, dest
, priv
->len
) < 0)
62 regs
->verdict
.code
= NFT_BREAK
;
66 nft_tcp_header_pointer(const struct nft_pktinfo
*pkt
,
67 unsigned int len
, void *buffer
, unsigned int *tcphdr_len
)
71 if (!pkt
->tprot_set
|| pkt
->tprot
!= IPPROTO_TCP
)
74 tcph
= skb_header_pointer(pkt
->skb
, pkt
->xt
.thoff
, sizeof(*tcph
), buffer
);
78 *tcphdr_len
= __tcp_hdrlen(tcph
);
79 if (*tcphdr_len
< sizeof(*tcph
) || *tcphdr_len
> len
)
82 return skb_header_pointer(pkt
->skb
, pkt
->xt
.thoff
, *tcphdr_len
, buffer
);
85 static void nft_exthdr_tcp_eval(const struct nft_expr
*expr
,
86 struct nft_regs
*regs
,
87 const struct nft_pktinfo
*pkt
)
89 u8 buff
[sizeof(struct tcphdr
) + MAX_TCP_OPTION_SPACE
];
90 struct nft_exthdr
*priv
= nft_expr_priv(expr
);
91 unsigned int i
, optl
, tcphdr_len
, offset
;
92 u32
*dest
= ®s
->data
[priv
->dreg
];
96 tcph
= nft_tcp_header_pointer(pkt
, sizeof(buff
), buff
, &tcphdr_len
);
101 for (i
= sizeof(*tcph
); i
< tcphdr_len
- 1; i
+= optl
) {
102 optl
= optlen(opt
, i
);
104 if (priv
->type
!= opt
[i
])
107 if (i
+ optl
> tcphdr_len
|| priv
->len
+ priv
->offset
> optl
)
110 offset
= i
+ priv
->offset
;
111 if (priv
->flags
& NFT_EXTHDR_F_PRESENT
) {
114 dest
[priv
->len
/ NFT_REG32_SIZE
] = 0;
115 memcpy(dest
, opt
+ offset
, priv
->len
);
122 if (priv
->flags
& NFT_EXTHDR_F_PRESENT
)
125 regs
->verdict
.code
= NFT_BREAK
;
128 static void nft_exthdr_tcp_set_eval(const struct nft_expr
*expr
,
129 struct nft_regs
*regs
,
130 const struct nft_pktinfo
*pkt
)
132 u8 buff
[sizeof(struct tcphdr
) + MAX_TCP_OPTION_SPACE
];
133 struct nft_exthdr
*priv
= nft_expr_priv(expr
);
134 unsigned int i
, optl
, tcphdr_len
, offset
;
139 tcph
= nft_tcp_header_pointer(pkt
, sizeof(buff
), buff
, &tcphdr_len
);
144 for (i
= sizeof(*tcph
); i
< tcphdr_len
- 1; i
+= optl
) {
151 optl
= optlen(opt
, i
);
153 if (priv
->type
!= opt
[i
])
156 if (i
+ optl
> tcphdr_len
|| priv
->len
+ priv
->offset
> optl
)
159 if (!skb_make_writable(pkt
->skb
, pkt
->xt
.thoff
+ i
+ priv
->len
))
162 tcph
= nft_tcp_header_pointer(pkt
, sizeof(buff
), buff
,
167 src
= regs
->data
[priv
->sreg
];
168 offset
= i
+ priv
->offset
;
172 old
.v16
= get_unaligned((u16
*)(opt
+ offset
));
175 switch (priv
->type
) {
177 /* increase can cause connection to stall */
178 if (ntohs(old
.v16
) <= ntohs(new.v16
))
183 if (old
.v16
== new.v16
)
186 put_unaligned(new.v16
, (u16
*)(opt
+ offset
));
187 inet_proto_csum_replace2(&tcph
->check
, pkt
->skb
,
188 old
.v16
, new.v16
, false);
192 old
.v32
= get_unaligned((u32
*)(opt
+ offset
));
194 if (old
.v32
== new.v32
)
197 put_unaligned(new.v32
, (u32
*)(opt
+ offset
));
198 inet_proto_csum_replace4(&tcph
->check
, pkt
->skb
,
199 old
.v32
, new.v32
, false);
210 static const struct nla_policy nft_exthdr_policy
[NFTA_EXTHDR_MAX
+ 1] = {
211 [NFTA_EXTHDR_DREG
] = { .type
= NLA_U32
},
212 [NFTA_EXTHDR_TYPE
] = { .type
= NLA_U8
},
213 [NFTA_EXTHDR_OFFSET
] = { .type
= NLA_U32
},
214 [NFTA_EXTHDR_LEN
] = { .type
= NLA_U32
},
215 [NFTA_EXTHDR_FLAGS
] = { .type
= NLA_U32
},
216 [NFTA_EXTHDR_OP
] = { .type
= NLA_U32
},
217 [NFTA_EXTHDR_SREG
] = { .type
= NLA_U32
},
220 static int nft_exthdr_init(const struct nft_ctx
*ctx
,
221 const struct nft_expr
*expr
,
222 const struct nlattr
* const tb
[])
224 struct nft_exthdr
*priv
= nft_expr_priv(expr
);
225 u32 offset
, len
, flags
= 0, op
= NFT_EXTHDR_OP_IPV6
;
228 if (!tb
[NFTA_EXTHDR_DREG
] ||
229 !tb
[NFTA_EXTHDR_TYPE
] ||
230 !tb
[NFTA_EXTHDR_OFFSET
] ||
231 !tb
[NFTA_EXTHDR_LEN
])
234 err
= nft_parse_u32_check(tb
[NFTA_EXTHDR_OFFSET
], U8_MAX
, &offset
);
238 err
= nft_parse_u32_check(tb
[NFTA_EXTHDR_LEN
], U8_MAX
, &len
);
242 if (tb
[NFTA_EXTHDR_FLAGS
]) {
243 err
= nft_parse_u32_check(tb
[NFTA_EXTHDR_FLAGS
], U8_MAX
, &flags
);
247 if (flags
& ~NFT_EXTHDR_F_PRESENT
)
251 if (tb
[NFTA_EXTHDR_OP
]) {
252 err
= nft_parse_u32_check(tb
[NFTA_EXTHDR_OP
], U8_MAX
, &op
);
257 priv
->type
= nla_get_u8(tb
[NFTA_EXTHDR_TYPE
]);
258 priv
->offset
= offset
;
260 priv
->dreg
= nft_parse_register(tb
[NFTA_EXTHDR_DREG
]);
264 return nft_validate_register_store(ctx
, priv
->dreg
, NULL
,
265 NFT_DATA_VALUE
, priv
->len
);
268 static int nft_exthdr_tcp_set_init(const struct nft_ctx
*ctx
,
269 const struct nft_expr
*expr
,
270 const struct nlattr
* const tb
[])
272 struct nft_exthdr
*priv
= nft_expr_priv(expr
);
273 u32 offset
, len
, flags
= 0, op
= NFT_EXTHDR_OP_IPV6
;
276 if (!tb
[NFTA_EXTHDR_SREG
] ||
277 !tb
[NFTA_EXTHDR_TYPE
] ||
278 !tb
[NFTA_EXTHDR_OFFSET
] ||
279 !tb
[NFTA_EXTHDR_LEN
])
282 if (tb
[NFTA_EXTHDR_DREG
] || tb
[NFTA_EXTHDR_FLAGS
])
285 err
= nft_parse_u32_check(tb
[NFTA_EXTHDR_OFFSET
], U8_MAX
, &offset
);
289 err
= nft_parse_u32_check(tb
[NFTA_EXTHDR_LEN
], U8_MAX
, &len
);
303 err
= nft_parse_u32_check(tb
[NFTA_EXTHDR_OP
], U8_MAX
, &op
);
307 priv
->type
= nla_get_u8(tb
[NFTA_EXTHDR_TYPE
]);
308 priv
->offset
= offset
;
310 priv
->sreg
= nft_parse_register(tb
[NFTA_EXTHDR_SREG
]);
314 return nft_validate_register_load(priv
->sreg
, priv
->len
);
317 static int nft_exthdr_dump_common(struct sk_buff
*skb
, const struct nft_exthdr
*priv
)
319 if (nla_put_u8(skb
, NFTA_EXTHDR_TYPE
, priv
->type
))
320 goto nla_put_failure
;
321 if (nla_put_be32(skb
, NFTA_EXTHDR_OFFSET
, htonl(priv
->offset
)))
322 goto nla_put_failure
;
323 if (nla_put_be32(skb
, NFTA_EXTHDR_LEN
, htonl(priv
->len
)))
324 goto nla_put_failure
;
325 if (nla_put_be32(skb
, NFTA_EXTHDR_FLAGS
, htonl(priv
->flags
)))
326 goto nla_put_failure
;
327 if (nla_put_be32(skb
, NFTA_EXTHDR_OP
, htonl(priv
->op
)))
328 goto nla_put_failure
;
335 static int nft_exthdr_dump(struct sk_buff
*skb
, const struct nft_expr
*expr
)
337 const struct nft_exthdr
*priv
= nft_expr_priv(expr
);
339 if (nft_dump_register(skb
, NFTA_EXTHDR_DREG
, priv
->dreg
))
342 return nft_exthdr_dump_common(skb
, priv
);
345 static int nft_exthdr_dump_set(struct sk_buff
*skb
, const struct nft_expr
*expr
)
347 const struct nft_exthdr
*priv
= nft_expr_priv(expr
);
349 if (nft_dump_register(skb
, NFTA_EXTHDR_SREG
, priv
->sreg
))
352 return nft_exthdr_dump_common(skb
, priv
);
355 static const struct nft_expr_ops nft_exthdr_ipv6_ops
= {
356 .type
= &nft_exthdr_type
,
357 .size
= NFT_EXPR_SIZE(sizeof(struct nft_exthdr
)),
358 .eval
= nft_exthdr_ipv6_eval
,
359 .init
= nft_exthdr_init
,
360 .dump
= nft_exthdr_dump
,
363 static const struct nft_expr_ops nft_exthdr_tcp_ops
= {
364 .type
= &nft_exthdr_type
,
365 .size
= NFT_EXPR_SIZE(sizeof(struct nft_exthdr
)),
366 .eval
= nft_exthdr_tcp_eval
,
367 .init
= nft_exthdr_init
,
368 .dump
= nft_exthdr_dump
,
371 static const struct nft_expr_ops nft_exthdr_tcp_set_ops
= {
372 .type
= &nft_exthdr_type
,
373 .size
= NFT_EXPR_SIZE(sizeof(struct nft_exthdr
)),
374 .eval
= nft_exthdr_tcp_set_eval
,
375 .init
= nft_exthdr_tcp_set_init
,
376 .dump
= nft_exthdr_dump_set
,
379 static const struct nft_expr_ops
*
380 nft_exthdr_select_ops(const struct nft_ctx
*ctx
,
381 const struct nlattr
* const tb
[])
385 if (!tb
[NFTA_EXTHDR_OP
])
386 return &nft_exthdr_ipv6_ops
;
388 if (tb
[NFTA_EXTHDR_SREG
] && tb
[NFTA_EXTHDR_DREG
])
389 return ERR_PTR(-EOPNOTSUPP
);
391 op
= ntohl(nla_get_be32(tb
[NFTA_EXTHDR_OP
]));
393 case NFT_EXTHDR_OP_TCPOPT
:
394 if (tb
[NFTA_EXTHDR_SREG
])
395 return &nft_exthdr_tcp_set_ops
;
396 if (tb
[NFTA_EXTHDR_DREG
])
397 return &nft_exthdr_tcp_ops
;
399 case NFT_EXTHDR_OP_IPV6
:
400 if (tb
[NFTA_EXTHDR_DREG
])
401 return &nft_exthdr_ipv6_ops
;
405 return ERR_PTR(-EOPNOTSUPP
);
408 struct nft_expr_type nft_exthdr_type __read_mostly
= {
410 .select_ops
= nft_exthdr_select_ops
,
411 .policy
= nft_exthdr_policy
,
412 .maxattr
= NFTA_EXTHDR_MAX
,
413 .owner
= THIS_MODULE
,