netifd: vxlan: refactor mapping of boolean attrs
[project/netifd.git] / system.c
1 /*
2 * netifd - network interface daemon
3 * Copyright (C) 2012 Felix Fietkau <nbd@openwrt.org>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2
7 * as published by the Free Software Foundation
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 */
14 #include "netifd.h"
15 #include "system.h"
16 #include <fcntl.h>
17
18 static const struct blobmsg_policy tunnel_attrs[__TUNNEL_ATTR_MAX] = {
19 [TUNNEL_ATTR_TYPE] = { .name = "mode", .type = BLOBMSG_TYPE_STRING },
20 [TUNNEL_ATTR_LOCAL] = { .name = "local", .type = BLOBMSG_TYPE_STRING },
21 [TUNNEL_ATTR_REMOTE] = { .name = "remote", .type = BLOBMSG_TYPE_STRING },
22 [TUNNEL_ATTR_MTU] = { .name = "mtu", .type = BLOBMSG_TYPE_INT32 },
23 [TUNNEL_ATTR_DF] = { .name = "df", .type = BLOBMSG_TYPE_BOOL },
24 [TUNNEL_ATTR_TTL] = { .name = "ttl", .type = BLOBMSG_TYPE_INT32 },
25 [TUNNEL_ATTR_TOS] = { .name = "tos", .type = BLOBMSG_TYPE_STRING },
26 [TUNNEL_ATTR_LINK] = { .name = "link", .type = BLOBMSG_TYPE_STRING },
27 [TUNNEL_ATTR_DATA] = { .name = "data", .type = BLOBMSG_TYPE_TABLE },
28 };
29
30 const struct uci_blob_param_list tunnel_attr_list = {
31 .n_params = __TUNNEL_ATTR_MAX,
32 .params = tunnel_attrs,
33 };
34
35 static const struct blobmsg_policy vxlan_data_attrs[__VXLAN_DATA_ATTR_MAX] = {
36 [VXLAN_DATA_ATTR_ID] = { .name = "id", .type = BLOBMSG_TYPE_INT32 },
37 [VXLAN_DATA_ATTR_PORT] = { .name = "port", .type = BLOBMSG_TYPE_INT32 },
38 [VXLAN_DATA_ATTR_MACADDR] = { .name = "macaddr", .type = BLOBMSG_TYPE_STRING },
39 [VXLAN_DATA_ATTR_RXCSUM] = { .name = "rxcsum", .type = BLOBMSG_TYPE_BOOL },
40 [VXLAN_DATA_ATTR_TXCSUM] = { .name = "txcsum", .type = BLOBMSG_TYPE_BOOL },
41 [VXLAN_DATA_ATTR_SRCPORTMIN] = { .name = "srcportmin", .type = BLOBMSG_TYPE_INT32 },
42 [VXLAN_DATA_ATTR_SRCPORTMAX] = { .name = "srcportmax", .type = BLOBMSG_TYPE_INT32 },
43 };
44
45 const struct uci_blob_param_list vxlan_data_attr_list = {
46 .n_params = __VXLAN_DATA_ATTR_MAX,
47 .params = vxlan_data_attrs,
48 };
49
50 static const struct blobmsg_policy gre_data_attrs[__GRE_DATA_ATTR_MAX] = {
51 [GRE_DATA_IKEY] = { .name = "ikey", .type = BLOBMSG_TYPE_INT32 },
52 [GRE_DATA_OKEY] = { .name = "okey", .type = BLOBMSG_TYPE_INT32 },
53 [GRE_DATA_ICSUM] = { .name = "icsum", .type = BLOBMSG_TYPE_BOOL },
54 [GRE_DATA_OCSUM] = { .name = "ocsum", .type = BLOBMSG_TYPE_BOOL },
55 [GRE_DATA_ISEQNO] = { .name = "iseqno", .type = BLOBMSG_TYPE_BOOL },
56 [GRE_DATA_OSEQNO] = { .name = "oseqno", .type = BLOBMSG_TYPE_BOOL },
57 [GRE_DATA_ENCAPLIMIT] = { .name = "encaplimit", .type = BLOBMSG_TYPE_STRING },
58 };
59
60 const struct uci_blob_param_list gre_data_attr_list = {
61 .n_params = __GRE_DATA_ATTR_MAX,
62 .params = gre_data_attrs,
63 };
64
65 static const struct blobmsg_policy vti_data_attrs[__VTI_DATA_ATTR_MAX] = {
66 [VTI_DATA_IKEY] = { .name = "ikey", .type = BLOBMSG_TYPE_INT32 },
67 [VTI_DATA_OKEY] = { .name = "okey", .type = BLOBMSG_TYPE_INT32 },
68 };
69
70 const struct uci_blob_param_list vti_data_attr_list = {
71 .n_params = __VTI_DATA_ATTR_MAX,
72 .params = vti_data_attrs,
73 };
74
75 static const struct blobmsg_policy xfrm_data_attrs[__XFRM_DATA_ATTR_MAX] = {
76 [XFRM_DATA_IF_ID] = { .name = "ifid", .type = BLOBMSG_TYPE_INT32 },
77 };
78
79 const struct uci_blob_param_list xfrm_data_attr_list = {
80 .n_params = __XFRM_DATA_ATTR_MAX,
81 .params = xfrm_data_attrs,
82 };
83
84 static const struct blobmsg_policy sixrd_data_attrs[__SIXRD_DATA_ATTR_MAX] = {
85 [SIXRD_DATA_PREFIX] = { .name = "prefix", .type = BLOBMSG_TYPE_STRING },
86 [SIXRD_DATA_RELAY_PREFIX] = { .name = "relay-prefix", .type = BLOBMSG_TYPE_STRING },
87 };
88
89 const struct uci_blob_param_list sixrd_data_attr_list = {
90 .n_params = __SIXRD_DATA_ATTR_MAX,
91 .params = sixrd_data_attrs,
92 };
93
94 static const struct blobmsg_policy ipip6_data_attrs[__SIXRD_DATA_ATTR_MAX] = {
95 [IPIP6_DATA_ENCAPLIMIT] = { .name = "encaplimit", .type = BLOBMSG_TYPE_STRING },
96 [IPIP6_DATA_FMRS] = { .name = "fmrs", .type = BLOBMSG_TYPE_ARRAY },
97 };
98
99 const struct uci_blob_param_list ipip6_data_attr_list = {
100 .n_params = __IPIP6_DATA_ATTR_MAX,
101 .params = ipip6_data_attrs,
102 };
103
104 static const struct blobmsg_policy fmr_data_attrs[__FMR_DATA_ATTR_MAX] = {
105 [FMR_DATA_PREFIX6] = { .name = "prefix6", .type = BLOBMSG_TYPE_STRING },
106 [FMR_DATA_PREFIX4] = { .name = "prefix4", .type = BLOBMSG_TYPE_STRING },
107 [FMR_DATA_EALEN] = { .name = "ealen", .type = BLOBMSG_TYPE_INT32 },
108 [FMR_DATA_OFFSET] = { .name = "offset", .type = BLOBMSG_TYPE_INT32 },
109 };
110
111 const struct uci_blob_param_list fmr_data_attr_list = {
112 .n_params = __FMR_DATA_ATTR_MAX,
113 .params = fmr_data_attrs,
114 };
115
116 void system_fd_set_cloexec(int fd)
117 {
118 #ifdef FD_CLOEXEC
119 fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC);
120 #endif
121 }