| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * Copyright (c) 2018 Fernando Fernandez Mancera <ffmancera@riseup.net> | ||
| 3 | * | ||
| 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 (or any | ||
| 6 | * later) as published by the Free Software Foundation. | ||
| 7 | */ | ||
| 8 | |||
| 9 | #include <nft.h> | ||
| 10 | |||
| 11 | #include <nftables.h> | ||
| 12 | #include <expression.h> | ||
| 13 | #include <utils.h> | ||
| 14 | #include <osf.h> | ||
| 15 | #include <json.h> | ||
| 16 | |||
| 17 | 35 | static const char *osf_ttl_int_to_str(const uint8_t ttl) | |
| 18 | { | ||
| 19 |
2/2✓ Branch 0 (2→3) taken 3 times.
✓ Branch 1 (2→4) taken 32 times.
|
35 | if (ttl == 1) |
| 20 | 3 | return "ttl loose "; | |
| 21 |
2/2✓ Branch 0 (4→5) taken 6 times.
✓ Branch 1 (4→6) taken 26 times.
|
32 | else if (ttl == 2) |
| 22 | 6 | return "ttl skip "; | |
| 23 | |||
| 24 | 26 | return ""; | |
| 25 | } | ||
| 26 | |||
| 27 | 35 | static void osf_expr_print(const struct expr *expr, struct output_ctx *octx) | |
| 28 | { | ||
| 29 | 35 | const char *ttl_str = osf_ttl_int_to_str(expr->osf.ttl); | |
| 30 | |||
| 31 |
2/2✓ Branch 0 (3→4) taken 12 times.
✓ Branch 1 (3→5) taken 23 times.
|
35 | if (expr->osf.flags & NFT_OSF_F_VERSION) |
| 32 | 12 | nft_print(octx, "osf %sversion", ttl_str); | |
| 33 | else | ||
| 34 | 23 | nft_print(octx, "osf %sname", ttl_str); | |
| 35 | 35 | } | |
| 36 | |||
| 37 | 75 | static void osf_expr_clone(struct expr *new, const struct expr *expr) | |
| 38 | { | ||
| 39 | 75 | new->osf.ttl = expr->osf.ttl; | |
| 40 | 75 | new->osf.flags = expr->osf.flags; | |
| 41 | 75 | } | |
| 42 | |||
| 43 | ✗ | static bool osf_expr_cmp(const struct expr *e1, const struct expr *e2) | |
| 44 | { | ||
| 45 | ✗ | return (e1->osf.ttl == e2->osf.ttl) && | |
| 46 | ✗ | (e1->osf.flags == e2->osf.flags); | |
| 47 | } | ||
| 48 | |||
| 49 | 20 | static int osf_expr_build_udata(struct nftnl_udata_buf *udbuf, | |
| 50 | const struct expr *expr) | ||
| 51 | { | ||
| 52 | 20 | return 0; | |
| 53 | } | ||
| 54 | |||
| 55 | 42 | static struct expr *osf_expr_parse_udata(const struct nftnl_udata *attr) | |
| 56 | { | ||
| 57 | 42 | return osf_expr_alloc(&internal_location, 0, 0); | |
| 58 | } | ||
| 59 | |||
| 60 | const struct expr_ops osf_expr_ops = { | ||
| 61 | .type = EXPR_OSF, | ||
| 62 | .name = "osf", | ||
| 63 | .print = osf_expr_print, | ||
| 64 | .clone = osf_expr_clone, | ||
| 65 | .cmp = osf_expr_cmp, | ||
| 66 | .json = osf_expr_json, | ||
| 67 | .parse_udata = osf_expr_parse_udata, | ||
| 68 | .build_udata = osf_expr_build_udata, | ||
| 69 | }; | ||
| 70 | |||
| 71 | 173 | struct expr *osf_expr_alloc(const struct location *loc, const uint8_t ttl, | |
| 72 | const uint32_t flags) | ||
| 73 | { | ||
| 74 | 173 | unsigned int len = NFT_OSF_MAXGENRELEN * BITS_PER_BYTE; | |
| 75 | 173 | const struct datatype *type = &string_type; | |
| 76 | struct expr *expr; | ||
| 77 | |||
| 78 | 173 | expr = expr_alloc(loc, EXPR_OSF, type, | |
| 79 | BYTEORDER_HOST_ENDIAN, len); | ||
| 80 | 173 | expr->osf.ttl = ttl; | |
| 81 | 173 | expr->osf.flags = flags; | |
| 82 | |||
| 83 | 173 | return expr; | |
| 84 | } | ||
| 85 |