2 * netifd - network interface daemon
3 * Copyright (C) 2012 Felix Fietkau <nbd@openwrt.org>
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
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.
18 #include <arpa/inet.h>
19 #include <netinet/in.h>
26 __vlist_simple_init(struct vlist_simple_tree *tree, int offset)
28 INIT_LIST_HEAD(&tree->list);
30 tree->head_offset = offset;
34 vlist_simple_delete(struct vlist_simple_tree *tree, struct vlist_simple_node *node)
38 list_del(&node->list);
39 ptr = (char *) node - tree->head_offset;
44 vlist_simple_flush(struct vlist_simple_tree *tree)
46 struct vlist_simple_node *n, *tmp;
48 list_for_each_entry_safe(n, tmp, &tree->list, list) {
49 if ((n->version == tree->version || n->version == -1) &&
53 vlist_simple_delete(tree, n);
58 vlist_simple_replace(struct vlist_simple_tree *dest, struct vlist_simple_tree *old)
60 struct vlist_simple_node *n, *tmp;
62 vlist_simple_update(dest);
63 list_for_each_entry_safe(n, tmp, &old->list, list) {
65 vlist_simple_add(dest, n);
67 vlist_simple_flush(dest);
71 vlist_simple_flush_all(struct vlist_simple_tree *tree)
74 vlist_simple_flush(tree);
78 parse_netmask_string(const char *str, bool v6)
84 if (!strchr(str, '.')) {
85 ret = strtoul(str, &err, 0);
95 if (inet_aton(str, &addr) != 1)
98 return 32 - fls(~(ntohl(addr.s_addr)));
105 split_netmask(char *str, unsigned int *netmask, bool v6)
107 char *delim = strchr(str, '/');
112 *netmask = parse_netmask_string(delim, v6);
118 parse_ip_and_netmask(int af, const char *str, void *addr, unsigned int *netmask)
120 char *astr = alloca(strlen(str) + 1);
123 if (!split_netmask(astr, netmask, af == AF_INET6))
126 if (af == AF_INET6) {
134 return inet_pton(af, astr, addr);
138 format_macaddr(uint8_t *mac)
140 static char str[sizeof("ff:ff:ff:ff:ff:ff ")];
142 snprintf(str, sizeof(str), "%02x:%02x:%02x:%02x:%02x:%02x",
143 mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
151 static uint32_t *crcvals = NULL;
153 crcvals = malloc(sizeof(*crcvals) * 256);
155 for (size_t i = 0; i < 256; ++i) {
157 for (size_t j = 0; j < 8; ++j)
158 c = (c & 1) ? (0xEDB88320 ^ (c >> 1)) : (c >> 1);
165 uint32_t c = 0xFFFFFFFF;
168 len = fread(buf, 1, sizeof(buf), fp);
169 for (size_t i = 0; i < len; ++i)
170 c = crcvals[(c ^ buf[i]) & 0xFF] ^ (c >> 8);
171 } while (len == sizeof(buf));
173 return c ^ 0xFFFFFFFF;
176 bool check_pid_path(int pid, const char *exe)
179 int exe_len = strlen(exe);
182 char proc_exe_buf[PROC_PIDPATHINFO_SIZE];
184 proc_exe_len = proc_pidpath(pid, proc_exe_buf, sizeof(proc_exe_buf));
187 char *proc_exe_buf = alloca(exe_len);
189 sprintf(proc_exe, "/proc/%d/exe", pid);
190 proc_exe_len = readlink(proc_exe, proc_exe_buf, exe_len);
193 if (proc_exe_len != exe_len)
196 return !memcmp(exe, proc_exe_buf, exe_len);
199 static const char * const uci_validate_name[__BLOBMSG_TYPE_LAST] = {
200 [BLOBMSG_TYPE_STRING] = "string",
201 [BLOBMSG_TYPE_ARRAY] = "list(string)",
202 [BLOBMSG_TYPE_INT32] = "uinteger",
203 [BLOBMSG_TYPE_BOOL] = "bool",
207 uci_get_validate_string(const struct uci_blob_param_list *p, int i)
210 return p->validate[i];
212 else if (uci_validate_name[p->params[i].type])
213 return uci_validate_name[p->params[i].type];
215 return p->validate[BLOBMSG_TYPE_STRING];