summaryrefslogtreecommitdiff
path: root/package/swconfig/src/uci.c
blob: ce544c3d58b76b4429bcfb164f0deacf705f08b3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/*
 * uci.c: UCI binding for the switch configuration utility
 *
 * Copyright (C) 2009 Felix Fietkau <nbd@openwrt.org>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * version 2 as published by the Free Software Foundatio.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <inttypes.h>
#include <errno.h>
#include <stdint.h>
#include <getopt.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <uci.h>

#include <linux/types.h>
#include <linux/netlink.h>
#include <linux/genetlink.h>
#include <netlink/netlink.h>
#include <netlink/genl/genl.h>
#include <netlink/genl/ctrl.h>
#include <linux/switch.h>
#include "swlib.h"

#ifndef ARRAY_SIZE
#define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
#endif

struct swlib_setting {
	struct switch_attr *attr;
	const char *name;
	int port_vlan;
	const char *val;
	struct swlib_setting *next;
};

struct swlib_setting early_settings[] = {
	{ .name = "reset", .val = "1" },
	{ .name = "enable_vlan", .val = "1" },
};

static struct swlib_setting *settings;
static struct swlib_setting **head;

static int
swlib_map_settings(struct switch_dev *dev, int type, int port_vlan, struct uci_section *s)
{
	struct swlib_setting *setting;
	struct switch_attr *attr;
	struct uci_element *e;
	struct uci_option *o;
	int i;

	uci_foreach_element(&s->options, e) {
		o = uci_to_option(e);

		if (o->type != UCI_TYPE_STRING)
			continue;

		if (!strcmp(e->name, "device"))
			continue;

		/* map early settings */
		if (type == SWLIB_ATTR_GROUP_GLOBAL) {
			int i;

			for (i = 0; i < ARRAY_SIZE(early_settings); i++) {
				if (strcmp(e->name, early_settings[i].name) != 0)
					continue;

				early_settings[i].val = o->v.string;
				goto skip;
			}
		}

		attr = swlib_lookup_attr(dev, type, e->name);
		if (!attr)
			continue;

		setting = malloc(sizeof(struct swlib_setting));
		memset(setting, 0, sizeof(struct swlib_setting));
		setting->attr = attr;
		setting->port_vlan = port_vlan;
		setting->val = o->v.string;
		*head = setting;
		head = &setting->next;
skip:
		continue;
	}
}

int swlib_apply_from_uci(struct switch_dev *dev, struct uci_package *p)
{
	struct switch_attr *attr;
	struct uci_context *ctx = p->ctx;
	struct uci_element *e;
	struct uci_section *s;
	struct uci_option *o;
	struct uci_ptr ptr;
	struct switch_val val;
	int i;

	settings = NULL;
	head = &settings;

	uci_foreach_element(&p->sections, e) {
		struct uci_element *n;

		s = uci_to_section(e);

		if (strcmp(s->type, "switch") != 0)
			continue;

		uci_foreach_element(&s->options, n) {
			struct uci_option *o = uci_to_option(n);

			if (strcmp(n->name, "name") != 0)
				continue;

			if (o->type != UCI_TYPE_STRING)
				continue;

			if (!strcmp(o->v.string, dev->dev_name))
				goto found;

			break;
		}

		if (strcmp(e->name, dev->dev_name) != 0)
			continue;

		goto found;
	}

	/* not found */
	return -1;

found:
	/* look up available early options, which need to be taken care
	 * of in the correct order */
	for (i = 0; i < ARRAY_SIZE(early_settings); i++) {
		early_settings[i].attr = swlib_lookup_attr(dev,
			SWLIB_ATTR_GROUP_GLOBAL, early_settings[i].name);
	}
	swlib_map_settings(dev, SWLIB_ATTR_GROUP_GLOBAL, 0, s);

	/* look for port or vlan sections */
	uci_foreach_element(&p->sections, e) {
		struct uci_element *os;
		s = uci_to_section(e);

		if (!strcmp(s->type, "switch_port")) {
			char *devn, *port, *port_err = NULL;
			int port_n;

			uci_foreach_element(&s->options, os) {
				o = uci_to_option(os);
				if (o->type != UCI_TYPE_STRING)
					continue;

				if (!strcmp(os->name, "device")) {
					devn = o->v.string;
					if (strcmp(devn, dev->dev_name) != 0)
						devn = NULL;
				} else if (!strcmp(os->name, "port")) {
					port = o->v.string;
				}
			}
			if (!dev || !port || !port[0])
				continue;

			port_n = strtoul(port, &port_err, 0);
			if (port_err && port_err[0])
				continue;

			swlib_map_settings(dev, SWLIB_ATTR_GROUP_PORT, port_n, s);
		} else if (!strcmp(s->type, "switch_vlan")) {
			char *devn, *vlan, *vlan_err = NULL;
			int vlan_n;

			uci_foreach_element(&s->options, os) {
				o = uci_to_option(os);
				if (o->type != UCI_TYPE_STRING)
					continue;

				if (!strcmp(os->name, "device")) {
					devn = o->v.string;
					if (strcmp(devn, dev->dev_name) != 0)
						devn = NULL;
				} else if (!strcmp(os->name, "vlan")) {
					vlan = o->v.string;
				}
			}
			if (!dev || !vlan || !vlan[0])
				continue;

			vlan_n = strtoul(vlan, &vlan_err, 0);
			if (vlan_err && vlan_err[0])
				continue;

			swlib_map_settings(dev, SWLIB_ATTR_GROUP_VLAN, vlan_n, s);
		}
	}

	for (i = 0; i < ARRAY_SIZE(early_settings); i++) {
		struct swlib_setting *st = &early_settings[i];
		if (!st->attr || !st->val)
			continue;
		swlib_set_attr_string(dev, st->attr, st->port_vlan, st->val);

	}

	while (settings) {
		struct swlib_setting *st = settings;

		swlib_set_attr_string(dev, st->attr, st->port_vlan, st->val);
		st = st->next;
		free(settings);
		settings = st;
	}

	/* Apply the config */
	attr = swlib_lookup_attr(dev, SWLIB_ATTR_GROUP_GLOBAL, "apply");
	if (!attr)
		return 0;

	memset(&val, 0, sizeof(val));
	swlib_set_attr(dev, attr, &val);

	return 0;
}