developer | b45f35c | 2023-12-04 09:51:36 +0800 | [diff] [blame^] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 2 | /* |
| 3 | * Copyright (c) 2023 MediaTek Inc. All Rights Reserved. |
| 4 | * |
| 5 | * Author: Ren-Ting Wang <ren-ting.wang@mediatek.com> |
| 6 | */ |
| 7 | |
| 8 | #include "tops/tops_params.h" |
| 9 | |
| 10 | #include "tops/protocol/mac/eth.h" |
| 11 | #include "tops/protocol/network/ip.h" |
| 12 | #include "tops/protocol/transport/udp.h" |
| 13 | |
| 14 | int |
| 15 | mtk_tops_encap_param_setup(struct sk_buff *skb, |
| 16 | struct tops_params *params, |
| 17 | int (*tnl_encap_param_setup)(struct sk_buff *skb, |
| 18 | struct tops_params *params)) |
| 19 | { |
| 20 | return mtk_tops_eth_encap_param_setup(skb, params, tnl_encap_param_setup); |
| 21 | } |
| 22 | |
| 23 | int |
| 24 | mtk_tops_decap_param_setup(struct sk_buff *skb, |
| 25 | struct tops_params *params, |
| 26 | int (*tnl_decap_param_setup)(struct sk_buff *skb, |
| 27 | struct tops_params *params)) |
| 28 | { |
| 29 | return tnl_decap_param_setup(skb, params); |
| 30 | } |
| 31 | |
| 32 | int mtk_tops_transport_decap_param_setup(struct sk_buff *skb, |
| 33 | struct tops_params *params) |
| 34 | { |
| 35 | return mtk_tops_udp_decap_param_setup(skb, params); |
| 36 | } |
| 37 | |
| 38 | int mtk_tops_network_decap_param_setup(struct sk_buff *skb, |
| 39 | struct tops_params *params) |
| 40 | { |
| 41 | /* TODO: IPv6 */ |
| 42 | return mtk_tops_ip_decap_param_setup(skb, params); |
| 43 | } |
| 44 | |
| 45 | int mtk_tops_mac_decap_param_setup(struct sk_buff *skb, |
| 46 | struct tops_params *params) |
| 47 | { |
| 48 | return mtk_tops_eth_decap_param_setup(skb, params); |
| 49 | } |
| 50 | |
| 51 | int mtk_tops_debug_param_proto_peek(const char *buf, int ofs, char *proto) |
| 52 | { |
| 53 | int nchar = 0; |
| 54 | int ret; |
| 55 | |
| 56 | if (!proto) |
| 57 | return -EINVAL; |
| 58 | |
| 59 | ret = sscanf(buf + ofs, "%20s %n", proto, &nchar); |
| 60 | if (ret != 1) |
| 61 | return -EPERM; |
| 62 | |
| 63 | return nchar; |
| 64 | } |
| 65 | |
| 66 | int mtk_tops_debug_param_setup(const char *buf, int *ofs, |
| 67 | struct tops_params *params) |
| 68 | { |
| 69 | char proto[DEBUG_PROTO_LEN]; |
| 70 | int ret; |
| 71 | |
| 72 | memset(proto, 0, sizeof(proto)); |
| 73 | |
| 74 | ret = mtk_tops_debug_param_proto_peek(buf, *ofs, proto); |
| 75 | if (ret < 0) |
| 76 | return ret; |
| 77 | |
| 78 | *ofs += ret; |
| 79 | |
| 80 | if (!strcmp(proto, DEBUG_PROTO_ETH)) |
| 81 | return mtk_tops_eth_debug_param_setup(buf, ofs, params); |
| 82 | |
| 83 | /* not support mac protocols other than Ethernet */ |
| 84 | return -EINVAL; |
| 85 | } |
| 86 | |
| 87 | void mtk_tops_mac_param_dump(struct seq_file *s, struct tops_params *params) |
| 88 | { |
| 89 | if (params->mac.type == TOPS_MAC_ETH) |
| 90 | mtk_tops_eth_param_dump(s, params); |
| 91 | } |
| 92 | |
| 93 | void mtk_tops_network_param_dump(struct seq_file *s, struct tops_params *params) |
| 94 | { |
| 95 | if (params->network.type == TOPS_NETWORK_IPV4) |
| 96 | mtk_tops_ip_param_dump(s, params); |
| 97 | } |
| 98 | |
| 99 | void mtk_tops_transport_param_dump(struct seq_file *s, struct tops_params *params) |
| 100 | { |
| 101 | if (params->transport.type == TOPS_TRANSPORT_UDP) |
| 102 | mtk_tops_udp_param_dump(s, params); |
| 103 | } |
| 104 | |
| 105 | static bool tops_transport_params_match(struct tops_transport_params *t1, |
| 106 | struct tops_transport_params *t2) |
| 107 | { |
| 108 | return !memcmp(t1, t2, sizeof(*t1)); |
| 109 | } |
| 110 | |
| 111 | static bool tops_network_params_match(struct tops_network_params *n1, |
| 112 | struct tops_network_params *n2) |
| 113 | { |
| 114 | if (n1->type != n2->type) |
| 115 | return false; |
| 116 | |
| 117 | if (n1->type == TOPS_NETWORK_IPV4) |
| 118 | return (n1->ip.sip == n2->ip.sip |
| 119 | && n1->ip.dip == n2->ip.dip |
| 120 | && n1->ip.proto == n2->ip.proto |
| 121 | && n1->ip.tos == n2->ip.tos); |
| 122 | |
| 123 | /* TODO: support IPv6 */ |
| 124 | return false; |
| 125 | } |
| 126 | |
| 127 | bool mtk_tops_params_match(struct tops_params *p1, struct tops_params *p2) |
| 128 | { |
| 129 | return (tops_network_params_match(&p1->network, &p2->network) |
| 130 | && tops_transport_params_match(&p1->transport, &p2->transport)); |
| 131 | } |