2 Copyright 2015-2018 Jo-Philipp Wich <jo@mein.io>
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
8 http://www.apache.org/licenses/LICENSE-2.0
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
32 #include <netinet/ether.h>
33 #include <arpa/inet.h>
34 #include <netlink/msg.h>
35 #include <netlink/attr.h>
36 #include <netlink/socket.h>
37 #include <linux/rtnetlink.h>
39 #define LUCI_IP "luci.ip"
40 #define LUCI_IP_CIDR "luci.ip.cidr"
42 #define RTA_INT(x) (*(int *)RTA_DATA(x))
43 #define RTA_U32(x) (*(uint32_t *)RTA_DATA(x))
46 ((f) == AF_INET ? 32 : \
47 ((f) == AF_INET6 ? 128 : \
48 ((f) == AF_PACKET ? 48 : 0)))
51 ((f) == AF_INET ? 4 : \
52 ((f) == AF_INET6 ? 16 : \
53 ((f) == AF_PACKET ? 6 : 0)))
56 static struct nl_sock *sock = NULL;
62 struct ether_addr mac;
82 struct ether_addr mac;
92 struct dump_filter *filter;
96 static int _cidr_new(lua_State *L, int index, int family, bool mask);
98 static cidr_t *L_checkcidr (lua_State *L, int index, cidr_t *p)
100 if (lua_type(L, index) == LUA_TUSERDATA)
101 return luaL_checkudata(L, index, LUCI_IP_CIDR);
103 if (_cidr_new(L, index, p ? p->family : 0, false))
104 return lua_touserdata(L, -1);
106 luaL_error(L, "Invalid operand");
110 static bool parse_mac(const char *mac, struct ether_addr *ea)
116 for (i = 0; i < 6; i++)
120 if (sep == 0 && (mac[0] == ':' || mac[0] == '-'))
123 if (sep == 0 || mac[0] != sep)
129 n = strtoul(mac, &e, 16);
135 ea->ether_addr_octet[i] = n;
144 static bool parse_mask(int family, const char *mask, int16_t *bits)
150 struct ether_addr mac;
154 if (family == AF_INET && inet_pton(AF_INET, mask, &m.v4))
156 for (*bits = 0, m.v4.s_addr = ntohl(m.v4.s_addr);
157 *bits < AF_BITS(AF_INET) && (m.v4.s_addr << *bits) & 0x80000000;
160 else if ((family == AF_INET6 && inet_pton(AF_INET6, mask, &m.v6)) ||
161 (family == AF_PACKET && parse_mac(mask, &m.mac)))
164 *bits < AF_BITS(family) && (m.u8[*bits / 8] << (*bits % 8)) & 128;
169 *bits = strtoul(mask, &e, 10);
171 if (e == mask || *e != 0 || *bits > AF_BITS(family))
178 static bool parse_cidr(const char *dest, cidr_t *pp)
180 char *p, buf[INET6_ADDRSTRLEN * 2 + 2];
182 strncpy(buf, dest, sizeof(buf) - 1);
184 p = strchr(buf, '/');
189 if (inet_pton(AF_INET, buf, &pp->addr.v4))
190 pp->family = AF_INET;
191 else if (inet_pton(AF_INET6, buf, &pp->addr.v6))
192 pp->family = AF_INET6;
193 else if (parse_mac(buf, &pp->addr.mac))
194 pp->family = AF_PACKET;
200 if (!parse_mask(pp->family, p, &pp->bits))
205 pp->bits = AF_BITS(pp->family);
211 static int format_cidr(lua_State *L, cidr_t *p)
213 char buf[INET6_ADDRSTRLEN];
215 if (p->family == AF_PACKET)
217 snprintf(buf, sizeof(buf), "%02X:%02X:%02X:%02X:%02X:%02X",
218 p->addr.mac.ether_addr_octet[0],
219 p->addr.mac.ether_addr_octet[1],
220 p->addr.mac.ether_addr_octet[2],
221 p->addr.mac.ether_addr_octet[3],
222 p->addr.mac.ether_addr_octet[4],
223 p->addr.mac.ether_addr_octet[5]);
225 if (p->bits < AF_BITS(AF_PACKET))
226 lua_pushfstring(L, "%s/%d", buf, p->bits);
228 lua_pushstring(L, buf);
232 if (p->bits < AF_BITS(p->family))
233 lua_pushfstring(L, "%s/%d",
234 inet_ntop(p->family, &p->addr.v6, buf, sizeof(buf)),
238 inet_ntop(p->family, &p->addr.v6, buf, sizeof(buf)));
244 static int L_getint(lua_State *L, int index, const char *name)
248 lua_getfield(L, index, name);
250 if (lua_type(L, -1) == LUA_TNUMBER)
251 rv = lua_tonumber(L, -1);
258 static const char * L_getstr(lua_State *L, int index, const char *name)
260 const char *rv = NULL;
262 lua_getfield(L, index, name);
264 if (lua_type(L, -1) == LUA_TSTRING)
265 rv = lua_tostring(L, -1);
272 static void L_setint(struct lua_State *L, const char *name, uint32_t n)
274 lua_pushinteger(L, n);
275 lua_setfield(L, -2, name);
278 static void L_setbool(struct lua_State *L, const char *name, bool val)
280 lua_pushboolean(L, val);
281 lua_setfield(L, -2, name);
284 static void L_setaddr(struct lua_State *L, const char *name,
285 int family, void *addr, int bits)
292 p = lua_newuserdata(L, sizeof(*p));
297 if (family == AF_INET)
300 p->bits = (bits < 0) ? AF_BITS(AF_INET) : bits;
301 p->addr.v4 = *(struct in_addr *)addr;
303 else if (family == AF_INET6)
305 p->family = AF_INET6;
306 p->bits = (bits < 0) ? AF_BITS(AF_INET6) : bits;
307 p->addr.v6 = *(struct in6_addr *)addr;
311 p->family = AF_PACKET;
312 p->bits = (bits < 0) ? AF_BITS(AF_PACKET) : bits;
313 p->addr.mac = *(struct ether_addr *)addr;
316 luaL_getmetatable(L, LUCI_IP_CIDR);
317 lua_setmetatable(L, -2);
318 lua_setfield(L, -2, name);
321 static void L_setstr(struct lua_State *L, const char *name, const char *val)
323 lua_pushstring(L, val);
324 lua_setfield(L, -2, name);
327 static void L_setdev(struct lua_State *L, const char *name,
332 if (if_indextoname(RTA_INT(attr), buf))
333 L_setstr(L, name, buf);
336 static int L_checkbits(lua_State *L, int index, cidr_t *p)
341 if (lua_gettop(L) < index || lua_isnil(L, index))
345 else if (lua_type(L, index) == LUA_TNUMBER)
347 bits = lua_tointeger(L, index);
349 if (bits < 0 || bits > AF_BITS(p->family))
350 return luaL_error(L, "Invalid prefix size");
352 else if (lua_type(L, index) == LUA_TSTRING)
354 if (!parse_mask(p->family, lua_tostring(L, index), &s16))
355 return luaL_error(L, "Invalid netmask format");
361 return luaL_error(L, "Invalid data type");
367 static int _cidr_new(lua_State *L, int index, int family, bool mask)
371 cidr_t cidr = { }, *cidrp;
373 if (lua_type(L, index) == LUA_TNUMBER)
375 n = htonl(lua_tointeger(L, index));
377 if (family == AF_INET6)
379 cidr.family = AF_INET6;
380 cidr.addr.v6.s6_addr[12] = n;
381 cidr.addr.v6.s6_addr[13] = (n >> 8);
382 cidr.addr.v6.s6_addr[14] = (n >> 16);
383 cidr.addr.v6.s6_addr[15] = (n >> 24);
385 else if (family == AF_INET)
387 cidr.family = AF_INET;
388 cidr.addr.v4.s_addr = n;
392 cidr.family = AF_PACKET;
393 cidr.addr.mac.ether_addr_octet[2] = n;
394 cidr.addr.mac.ether_addr_octet[3] = (n >> 8);
395 cidr.addr.mac.ether_addr_octet[4] = (n >> 16);
396 cidr.addr.mac.ether_addr_octet[5] = (n >> 24);
399 cidr.bits = AF_BITS(cidr.family);
403 addr = luaL_checkstring(L, index);
405 if (!parse_cidr(addr, &cidr))
408 if (family && cidr.family != family)
412 cidr.bits = L_checkbits(L, index + 1, &cidr);
415 if (!(cidrp = lua_newuserdata(L, sizeof(*cidrp))))
419 luaL_getmetatable(L, LUCI_IP_CIDR);
420 lua_setmetatable(L, -2);
424 static int cidr_new(lua_State *L)
426 return _cidr_new(L, 1, 0, true);
429 static int cidr_ipv4(lua_State *L)
431 return _cidr_new(L, 1, AF_INET, true);
434 static int cidr_ipv6(lua_State *L)
436 return _cidr_new(L, 1, AF_INET6, true);
439 static int cidr_mac(lua_State *L)
441 return _cidr_new(L, 1, AF_PACKET, true);
444 static int cidr_check(lua_State *L, int family)
446 cidr_t cidr = { }, *cidrp;
449 if (lua_type(L, 1) == LUA_TSTRING)
451 addr = lua_tostring(L, 1);
453 if (addr && parse_cidr(addr, &cidr) && cidr.family == family)
454 return format_cidr(L, &cidr);
458 cidrp = lua_touserdata(L, 1);
463 if (!lua_getmetatable(L, 1))
466 lua_getfield(L, LUA_REGISTRYINDEX, LUCI_IP_CIDR);
468 if (!lua_rawequal(L, -1, -2))
473 if (cidrp != NULL && cidrp->family == family)
474 return format_cidr(L, cidrp);
480 static int cidr_checkip4(lua_State *L)
482 return cidr_check(L, AF_INET);
485 static int cidr_checkip6(lua_State *L)
487 return cidr_check(L, AF_INET6);
490 static int cidr_checkmac(lua_State *L)
492 return cidr_check(L, AF_PACKET);
495 static int cidr_is4(lua_State *L)
497 cidr_t *p = L_checkcidr(L, 1, NULL);
499 lua_pushboolean(L, p->family == AF_INET);
503 static int cidr_is4rfc1918(lua_State *L)
505 cidr_t *p = L_checkcidr(L, 1, NULL);
506 uint32_t a = htonl(p->addr.v4.s_addr);
508 lua_pushboolean(L, (p->family == AF_INET &&
509 ((a >= 0x0A000000 && a <= 0x0AFFFFFF) ||
510 (a >= 0xAC100000 && a <= 0xAC1FFFFF) ||
511 (a >= 0xC0A80000 && a <= 0xC0A8FFFF))));
516 static int cidr_is4linklocal(lua_State *L)
518 cidr_t *p = L_checkcidr(L, 1, NULL);
519 uint32_t a = htonl(p->addr.v4.s_addr);
521 lua_pushboolean(L, (p->family == AF_INET &&
528 static bool _is_mapped4(cidr_t *p)
530 return (p->family == AF_INET6 &&
531 p->addr.v6.s6_addr[0] == 0 &&
532 p->addr.v6.s6_addr[1] == 0 &&
533 p->addr.v6.s6_addr[2] == 0 &&
534 p->addr.v6.s6_addr[3] == 0 &&
535 p->addr.v6.s6_addr[4] == 0 &&
536 p->addr.v6.s6_addr[5] == 0 &&
537 p->addr.v6.s6_addr[6] == 0 &&
538 p->addr.v6.s6_addr[7] == 0 &&
539 p->addr.v6.s6_addr[8] == 0 &&
540 p->addr.v6.s6_addr[9] == 0 &&
541 p->addr.v6.s6_addr[10] == 0xFF &&
542 p->addr.v6.s6_addr[11] == 0xFF);
545 static int cidr_is6mapped4(lua_State *L)
547 cidr_t *p = L_checkcidr(L, 1, NULL);
549 lua_pushboolean(L, _is_mapped4(p));
553 static int cidr_is6(lua_State *L)
555 cidr_t *p = L_checkcidr(L, 1, NULL);
557 lua_pushboolean(L, p->family == AF_INET6);
561 static int cidr_is6linklocal(lua_State *L)
563 cidr_t *p = L_checkcidr(L, 1, NULL);
565 lua_pushboolean(L, (p->family == AF_INET6 &&
566 p->addr.v6.s6_addr[0] == 0xFE &&
567 p->addr.v6.s6_addr[1] >= 0x80 &&
568 p->addr.v6.s6_addr[1] <= 0xBF));
573 static int cidr_ismac(lua_State *L)
575 cidr_t *p = L_checkcidr(L, 1, NULL);
577 lua_pushboolean(L, p->family == AF_PACKET);
581 static int cidr_ismacmcast(lua_State *L)
583 cidr_t *p = L_checkcidr(L, 1, NULL);
585 lua_pushboolean(L, (p->family == AF_PACKET &&
586 (p->addr.mac.ether_addr_octet[0] & 0x1)));
591 static int cidr_ismaclocal(lua_State *L)
593 cidr_t *p = L_checkcidr(L, 1, NULL);
595 lua_pushboolean(L, (p->family == AF_PACKET &&
596 (p->addr.mac.ether_addr_octet[0] & 0x2)));
601 static int _cidr_cmp(lua_State *L)
603 cidr_t *a = L_checkcidr(L, 1, NULL);
604 cidr_t *b = L_checkcidr(L, 2, NULL);
606 if (a->family != b->family)
607 return (a->family - b->family);
609 return memcmp(&a->addr.v6, &b->addr.v6, AF_BYTES(a->family));
612 static int cidr_lower(lua_State *L)
614 lua_pushboolean(L, _cidr_cmp(L) < 0);
618 static int cidr_higher(lua_State *L)
620 lua_pushboolean(L, _cidr_cmp(L) > 0);
624 static int cidr_equal(lua_State *L)
626 lua_pushboolean(L, _cidr_cmp(L) == 0);
630 static int cidr_lower_equal(lua_State *L)
632 lua_pushboolean(L, _cidr_cmp(L) <= 0);
636 static int cidr_prefix(lua_State *L)
638 cidr_t *p = L_checkcidr(L, 1, NULL);
639 int bits = L_checkbits(L, 2, p);
642 lua_pushinteger(L, p->bits);
646 static void _apply_mask(cidr_t *p, int bits, bool inv)
652 memset(&p->addr.u8, inv * 0xFF, AF_BYTES(p->family));
654 else if (p->family == AF_INET && bits <= AF_BITS(AF_INET))
657 p->addr.v4.s_addr |= ntohl((1 << (AF_BITS(AF_INET) - bits)) - 1);
659 p->addr.v4.s_addr &= ntohl(~((1 << (AF_BITS(AF_INET) - bits)) - 1));
661 else if (bits <= AF_BITS(p->family))
663 for (i = 0; i < AF_BYTES(p->family); i++)
665 b = (bits > 8) ? 8 : bits;
667 p->addr.u8[i] |= ~((uint8_t)(0xFF << (8 - b)));
669 p->addr.u8[i] &= (uint8_t)(0xFF << (8 - b));
675 static int cidr_network(lua_State *L)
677 cidr_t *p1 = L_checkcidr(L, 1, NULL), *p2;
678 int bits = L_checkbits(L, 2, p1);
680 if (!(p2 = lua_newuserdata(L, sizeof(*p2))))
684 p2->bits = AF_BITS(p1->family);
685 _apply_mask(p2, bits, false);
687 luaL_getmetatable(L, LUCI_IP_CIDR);
688 lua_setmetatable(L, -2);
692 static int cidr_host(lua_State *L)
694 cidr_t *p1 = L_checkcidr(L, 1, NULL);
695 cidr_t *p2 = lua_newuserdata(L, sizeof(*p2));
701 p2->bits = AF_BITS(p1->family);
703 luaL_getmetatable(L, LUCI_IP_CIDR);
704 lua_setmetatable(L, -2);
708 static int cidr_mask(lua_State *L)
710 cidr_t *p1 = L_checkcidr(L, 1, NULL), *p2;
711 int bits = L_checkbits(L, 2, p1);
713 if (!(p2 = lua_newuserdata(L, sizeof(*p2))))
716 p2->bits = AF_BITS(p1->family);
717 p2->family = p1->family;
719 memset(&p2->addr.v6.s6_addr, 0xFF, sizeof(p2->addr.v6.s6_addr));
720 _apply_mask(p2, bits, false);
722 luaL_getmetatable(L, LUCI_IP_CIDR);
723 lua_setmetatable(L, -2);
727 static int cidr_broadcast(lua_State *L)
729 cidr_t *p1 = L_checkcidr(L, 1, NULL);
731 int bits = L_checkbits(L, 2, p1);
733 if (p1->family != AF_INET)
736 if (!(p2 = lua_newuserdata(L, sizeof(*p2))))
740 p2->bits = AF_BITS(AF_INET);
741 _apply_mask(p2, bits, true);
743 luaL_getmetatable(L, LUCI_IP_CIDR);
744 lua_setmetatable(L, -2);
748 static int cidr_mapped4(lua_State *L)
750 cidr_t *p1 = L_checkcidr(L, 1, NULL);
753 if (!_is_mapped4(p1))
756 if (!(p2 = lua_newuserdata(L, sizeof(*p2))))
759 p2->family = AF_INET;
760 p2->bits = (p1->bits > AF_BITS(AF_INET)) ? AF_BITS(AF_INET) : p1->bits;
761 memcpy(&p2->addr.v4, p1->addr.v6.s6_addr + 12, sizeof(p2->addr.v4));
763 luaL_getmetatable(L, LUCI_IP_CIDR);
764 lua_setmetatable(L, -2);
768 static int cidr_tolinklocal(lua_State *L)
770 cidr_t *p1 = L_checkcidr(L, 1, NULL);
774 if (p1->family != AF_PACKET)
777 if (!(p2 = lua_newuserdata(L, sizeof(*p2))))
780 p2->family = AF_INET6;
781 p2->bits = AF_BITS(AF_INET6);
782 p2->addr.u8[0] = 0xFE;
783 p2->addr.u8[1] = 0x80;
784 p2->addr.u8[8] = p1->addr.u8[0] ^ 0x02;
785 p2->addr.u8[9] = p1->addr.u8[1];
786 p2->addr.u8[10] = p1->addr.u8[2];
787 p2->addr.u8[11] = 0xFF;
788 p2->addr.u8[12] = 0xFE;
789 p2->addr.u8[13] = p1->addr.u8[3];
790 p2->addr.u8[14] = p1->addr.u8[4];
791 p2->addr.u8[15] = p1->addr.u8[5];
793 luaL_getmetatable(L, LUCI_IP_CIDR);
794 lua_setmetatable(L, -2);
798 static int cidr_tomac(lua_State *L)
800 cidr_t *p1 = L_checkcidr(L, 1, NULL);
804 if (p1->family != AF_INET6 ||
805 p1->addr.u8[0] != 0xFE ||
806 p1->addr.u8[1] != 0x80 ||
807 p1->addr.u8[2] != 0x00 ||
808 p1->addr.u8[3] != 0x00 ||
809 p1->addr.u8[4] != 0x00 ||
810 p1->addr.u8[5] != 0x00 ||
811 p1->addr.u8[6] != 0x00 ||
812 p1->addr.u8[7] != 0x00 ||
813 p1->addr.u8[11] != 0xFF ||
814 p1->addr.u8[12] != 0xFE)
817 if (!(p2 = lua_newuserdata(L, sizeof(*p2))))
820 p2->family = AF_PACKET;
821 p2->bits = AF_BITS(AF_PACKET);
822 p2->addr.u8[0] = p1->addr.u8[8] ^ 0x02;
823 p2->addr.u8[1] = p1->addr.u8[9];
824 p2->addr.u8[2] = p1->addr.u8[10];
825 p2->addr.u8[3] = p1->addr.u8[13];
826 p2->addr.u8[4] = p1->addr.u8[14];
827 p2->addr.u8[5] = p1->addr.u8[15];
829 luaL_getmetatable(L, LUCI_IP_CIDR);
830 lua_setmetatable(L, -2);
834 static int cidr_contains(lua_State *L)
836 cidr_t *p1 = L_checkcidr(L, 1, NULL);
837 cidr_t *p2 = L_checkcidr(L, 2, NULL);
838 cidr_t a = *p1, b = *p2;
841 if (p1->family == p2->family && p1->bits <= p2->bits)
843 _apply_mask(&a, p1->bits, false);
844 _apply_mask(&b, p1->bits, false);
846 rv = !memcmp(&a.addr.v6, &b.addr.v6, AF_BYTES(a.family));
849 lua_pushboolean(L, rv);
854 (a)->addr.u8[AF_BYTES((a)->family) - (i) - 1]
856 static int _cidr_add_sub(lua_State *L, bool add)
858 cidr_t *p1 = L_checkcidr(L, 1, NULL);
859 cidr_t *p2 = L_checkcidr(L, 2, p1);
861 bool inplace = lua_isboolean(L, 3) ? lua_toboolean(L, 3) : false;
866 if (p1->family == p2->family)
868 if (p1->family == AF_INET)
870 a = ntohl(p1->addr.v4.s_addr);
871 b = ntohl(p2->addr.v4.s_addr);
873 /* would over/underflow */
874 if ((add && (UINT_MAX - a) < b) || (!add && a < b))
876 r.addr.v4.s_addr = add * 0xFFFFFFFF;
881 r.addr.v4.s_addr = add ? htonl(a + b) : htonl(a - b);
886 for (i = 0, carry = 0; i < AF_BYTES(p1->family); i++)
890 BYTE(&r, i) = BYTE(p1, i) + BYTE(p2, i) + carry;
891 carry = (BYTE(p1, i) + BYTE(p2, i) + carry) / 256;
895 BYTE(&r, i) = (BYTE(p1, i) - BYTE(p2, i) - carry);
896 carry = (BYTE(p1, i) < (BYTE(p2, i) + carry));
900 /* would over/underflow */
903 memset(&r.addr.u8, add * 0xFF, AF_BYTES(r.family));
916 lua_pushboolean(L, ok);
920 if (!(p1 = lua_newuserdata(L, sizeof(*p1))))
925 luaL_getmetatable(L, LUCI_IP_CIDR);
926 lua_setmetatable(L, -2);
930 static int cidr_add(lua_State *L)
932 return _cidr_add_sub(L, true);
935 static int cidr_sub(lua_State *L)
937 return _cidr_add_sub(L, false);
940 static int cidr_minhost(lua_State *L)
942 cidr_t *p = L_checkcidr(L, 1, NULL);
944 uint8_t i, rest, carry;
946 _apply_mask(&r, r.bits, false);
948 if (r.family == AF_INET && r.bits < AF_BITS(AF_INET))
950 r.bits = AF_BITS(AF_INET);
951 r.addr.v4.s_addr = htonl(ntohl(r.addr.v4.s_addr) + 1);
953 else if (r.bits < AF_BITS(r.family))
955 r.bits = AF_BITS(r.family);
957 for (i = 0, carry = 1; i < AF_BYTES(r.family); i++)
959 rest = (BYTE(&r, i) + carry) > 255;
960 BYTE(&r, i) += carry;
965 if (!(p = lua_newuserdata(L, sizeof(*p))))
970 luaL_getmetatable(L, LUCI_IP_CIDR);
971 lua_setmetatable(L, -2);
975 static int cidr_maxhost(lua_State *L)
977 cidr_t *p = L_checkcidr(L, 1, NULL);
980 _apply_mask(&r, r.bits, true);
982 if (r.family == AF_INET && r.bits < AF_BITS(AF_INET))
984 r.bits = AF_BITS(AF_INET);
985 r.addr.v4.s_addr = htonl(ntohl(r.addr.v4.s_addr) - 1);
989 r.bits = AF_BITS(r.family);
992 if (!(p = lua_newuserdata(L, sizeof(*p))))
997 luaL_getmetatable(L, LUCI_IP_CIDR);
998 lua_setmetatable(L, -2);
1002 static int cidr_gc (lua_State *L)
1007 static int cidr_tostring (lua_State *L)
1009 cidr_t *p = L_checkcidr(L, 1, NULL);
1010 return format_cidr(L, p);
1017 static bool diff_prefix(int family, void *addr, int bits, bool exact, cidr_t *p)
1019 uint8_t i, b, r, *a;
1025 if (!addr || p->family != family || p->bits > bits)
1028 if (family == AF_INET)
1030 m = p->bits ? htonl(~((1 << (AF_BITS(AF_INET) - p->bits)) - 1)) : 0;
1032 if ((((struct in_addr *)addr)->s_addr & m) != (p->addr.v4.s_addr & m))
1037 for (i = 0, a = addr, r = p->bits; i < AF_BYTES(p->family); i++)
1039 b = r ? (0xFF << (8 - ((r > 8) ? 8 : r))) : 0;
1041 if ((a[i] & b) != (p->addr.u8[i] & b))
1044 r -= ((r > 8) ? 8 : r);
1048 return (exact && p->bits != bits);
1051 static int cb_dump_route(struct nl_msg *msg, void *arg)
1053 struct dump_state *s = arg;
1054 struct dump_filter *f = s->filter;
1055 struct nlmsghdr *hdr = nlmsg_hdr(msg);
1056 struct rtmsg *rt = NLMSG_DATA(hdr);
1057 struct nlattr *tb[RTA_MAX+1];
1058 struct in6_addr *src, *dst, *gw, *from, def = { };
1059 int iif, oif, bitlen;
1062 if (hdr->nlmsg_type != RTM_NEWROUTE ||
1063 (rt->rtm_family != AF_INET && rt->rtm_family != AF_INET6))
1066 nlmsg_parse(hdr, sizeof(*rt), tb, RTA_MAX, NULL);
1068 iif = tb[RTA_IIF] ? RTA_INT(tb[RTA_IIF]) : 0;
1069 oif = tb[RTA_OIF] ? RTA_INT(tb[RTA_OIF]) : 0;
1070 table = tb[RTA_TABLE] ? RTA_U32(tb[RTA_TABLE]) : rt->rtm_table;
1071 from = tb[RTA_SRC] ? RTA_DATA(tb[RTA_SRC]) : NULL;
1072 src = tb[RTA_PREFSRC] ? RTA_DATA(tb[RTA_PREFSRC]) : NULL;
1073 dst = tb[RTA_DST] ? RTA_DATA(tb[RTA_DST]) : &def;
1074 gw = tb[RTA_GATEWAY] ? RTA_DATA(tb[RTA_GATEWAY]) : NULL;
1076 bitlen = AF_BITS(rt->rtm_family);
1079 if ((f->type && rt->rtm_type != f->type) ||
1080 (f->family && rt->rtm_family != f->family) ||
1081 (f->proto && rt->rtm_protocol != f->proto) ||
1082 (f->scope && rt->rtm_scope != f->scope) ||
1083 (f->iif && iif != f->iif) ||
1084 (f->oif && oif != f->oif) ||
1085 (f->table && table != f->table) ||
1086 diff_prefix(rt->rtm_family, from, rt->rtm_src_len,
1087 f->from_exact, &f->from) ||
1088 diff_prefix(rt->rtm_family, dst, rt->rtm_dst_len,
1089 f->dst_exact, &f->dst) ||
1090 diff_prefix(rt->rtm_family, gw, bitlen,
1092 diff_prefix(rt->rtm_family, src, bitlen,
1098 lua_pushvalue(s->L, 2);
1102 L_setint(s->L, "type", rt->rtm_type);
1103 L_setint(s->L, "family", (rt->rtm_family == AF_INET) ? 4 : 6);
1105 L_setaddr(s->L, "dest", rt->rtm_family, dst, rt->rtm_dst_len);
1108 L_setaddr(s->L, "gw", rt->rtm_family, gw, -1);
1111 L_setaddr(s->L, "from", rt->rtm_family, from, rt->rtm_src_len);
1114 L_setdev(s->L, "iif", tb[RTA_IIF]);
1117 L_setdev(s->L, "dev", tb[RTA_OIF]);
1119 L_setint(s->L, "table", table);
1120 L_setint(s->L, "proto", rt->rtm_protocol);
1121 L_setint(s->L, "scope", rt->rtm_scope);
1124 L_setaddr(s->L, "src", rt->rtm_family, src, -1);
1126 if (tb[RTA_PRIORITY])
1127 L_setint(s->L, "metric", RTA_U32(tb[RTA_PRIORITY]));
1129 if (rt->rtm_family == AF_INET6 && tb[RTA_CACHEINFO])
1131 struct rta_cacheinfo *ci = RTA_DATA(tb[RTA_CACHEINFO]);
1133 if (ci->rta_expires)
1135 if (ci->rta_expires)
1136 L_setint(s->L, "expires", ci->rta_expires / hz);
1138 if (ci->rta_error != 0)
1139 L_setint(s->L, "error", ci->rta_error);
1146 lua_call(s->L, 1, 0);
1147 else if (hdr->nlmsg_flags & NLM_F_MULTI)
1148 lua_rawseti(s->L, -2, s->index);
1151 s->pending = !!(hdr->nlmsg_flags & NLM_F_MULTI);
1156 cb_done(struct nl_msg *msg, void *arg)
1158 struct dump_state *s = arg;
1164 cb_error(struct sockaddr_nl *nla, struct nlmsgerr *err, void *arg)
1166 struct dump_state *s = arg;
1171 static int _error(lua_State *L, int code, const char *msg)
1174 lua_pushnumber(L, code ? code : errno);
1175 lua_pushstring(L, msg ? msg : strerror(errno));
1180 static int _route_dump(lua_State *L, struct dump_filter *filter)
1182 int flags = NLM_F_REQUEST;
1183 struct dump_state s = {
1187 .callback = lua_isfunction(L, 2),
1192 hz = sysconf(_SC_CLK_TCK);
1196 sock = nl_socket_alloc();
1198 return _error(L, -1, "Out of memory");
1200 if (nl_connect(sock, NETLINK_ROUTE))
1201 return _error(L, 0, NULL);
1205 struct nl_cb *cb = nl_cb_alloc(NL_CB_DEFAULT);
1206 struct rtmsg rtm = {
1207 .rtm_family = filter->family,
1208 .rtm_dst_len = filter->dst.bits,
1209 .rtm_src_len = filter->src.bits
1213 flags |= NLM_F_DUMP;
1215 msg = nlmsg_alloc_simple(RTM_GETROUTE, flags);
1219 nlmsg_append(msg, &rtm, sizeof(rtm), 0);
1222 nla_put(msg, RTA_DST, AF_BYTES(filter->dst.family),
1223 &filter->dst.addr.v6);
1225 if (filter->src.family)
1226 nla_put(msg, RTA_SRC, AF_BYTES(filter->src.family),
1227 &filter->src.addr.v6);
1230 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, cb_dump_route, &s);
1231 nl_cb_set(cb, NL_CB_FINISH, NL_CB_CUSTOM, cb_done, &s);
1232 nl_cb_err(cb, NL_CB_CUSTOM, cb_error, &s);
1234 nl_send_auto_complete(sock, msg);
1236 if (!filter->get && !s.callback)
1239 while (s.pending > 0)
1240 nl_recvmsgs(sock, cb);
1253 return (s.index > 0);
1256 static int route_get(lua_State *L)
1258 struct dump_filter filter = { .get = true };
1259 const char *dest = luaL_checkstring(L, 1);
1260 const char *from = luaL_optstring(L, 2, NULL);
1262 if (!parse_cidr(dest, &filter.dst))
1263 return _error(L, -1, "Invalid destination");
1265 if (from && !parse_cidr(from, &filter.src))
1266 return _error(L, -1, "Invalid source");
1268 if (filter.src.family != 0 &&
1269 filter.src.family != filter.dst.family)
1270 return _error(L, -1, "Different source/destination family");
1272 filter.family = filter.dst.family;
1274 return _route_dump(L, &filter);
1277 static int route_dump(lua_State *L)
1281 struct dump_filter filter = { };
1283 if (lua_type(L, 1) == LUA_TTABLE)
1285 filter.family = L_getint(L, 1, "family");
1287 if (filter.family == 4)
1288 filter.family = AF_INET;
1289 else if (filter.family == 6)
1290 filter.family = AF_INET6;
1294 if ((s = L_getstr(L, 1, "iif")) != NULL)
1295 filter.iif = if_nametoindex(s);
1297 if ((s = L_getstr(L, 1, "oif")) != NULL)
1298 filter.oif = if_nametoindex(s);
1300 filter.type = L_getint(L, 1, "type");
1301 filter.scope = L_getint(L, 1, "scope");
1302 filter.proto = L_getint(L, 1, "proto");
1303 filter.table = L_getint(L, 1, "table");
1305 if ((s = L_getstr(L, 1, "gw")) != NULL && parse_cidr(s, &p))
1308 if ((s = L_getstr(L, 1, "from")) != NULL && parse_cidr(s, &p))
1311 if ((s = L_getstr(L, 1, "src")) != NULL && parse_cidr(s, &p))
1314 if ((s = L_getstr(L, 1, "dest")) != NULL && parse_cidr(s, &p))
1317 if ((s = L_getstr(L, 1, "from_exact")) != NULL && parse_cidr(s, &p))
1318 filter.from = p, filter.from_exact = true;
1320 if ((s = L_getstr(L, 1, "dest_exact")) != NULL && parse_cidr(s, &p))
1321 filter.dst = p, filter.dst_exact = true;
1324 return _route_dump(L, &filter);
1328 static bool diff_macaddr(struct ether_addr *mac1, struct ether_addr *mac2)
1330 struct ether_addr empty = { };
1332 if (!memcmp(mac2, &empty, sizeof(empty)))
1335 if (!mac1 || memcmp(mac1, mac2, sizeof(empty)))
1341 static int cb_dump_neigh(struct nl_msg *msg, void *arg)
1344 struct ether_addr *mac;
1345 struct in6_addr *dst;
1346 struct dump_state *s = arg;
1347 struct dump_filter *f = s->filter;
1348 struct nlmsghdr *hdr = nlmsg_hdr(msg);
1349 struct ndmsg *nd = NLMSG_DATA(hdr);
1350 struct nlattr *tb[NDA_MAX+1];
1353 if (hdr->nlmsg_type != RTM_NEWNEIGH ||
1354 (nd->ndm_family != AF_INET && nd->ndm_family != AF_INET6))
1357 nlmsg_parse(hdr, sizeof(*nd), tb, NDA_MAX, NULL);
1359 mac = tb[NDA_LLADDR] ? RTA_DATA(tb[NDA_LLADDR]) : NULL;
1360 dst = tb[NDA_DST] ? RTA_DATA(tb[NDA_DST]) : NULL;
1362 bitlen = AF_BITS(nd->ndm_family);
1364 if ((f->family && nd->ndm_family != f->family) ||
1365 (f->iif && nd->ndm_ifindex != f->iif) ||
1366 (f->type && !(f->type & nd->ndm_state)) ||
1367 diff_prefix(nd->ndm_family, dst, bitlen, false, &f->dst) ||
1368 diff_macaddr(mac, &f->mac))
1372 lua_pushvalue(s->L, 2);
1376 L_setint(s->L, "family", (nd->ndm_family == AF_INET) ? 4 : 6);
1377 L_setstr(s->L, "dev", if_indextoname(nd->ndm_ifindex, buf));
1379 L_setbool(s->L, "router", (nd->ndm_flags & NTF_ROUTER));
1380 L_setbool(s->L, "proxy", (nd->ndm_flags & NTF_PROXY));
1382 L_setbool(s->L, "incomplete", (nd->ndm_state & NUD_INCOMPLETE));
1383 L_setbool(s->L, "reachable", (nd->ndm_state & NUD_REACHABLE));
1384 L_setbool(s->L, "stale", (nd->ndm_state & NUD_STALE));
1385 L_setbool(s->L, "delay", (nd->ndm_state & NUD_DELAY));
1386 L_setbool(s->L, "probe", (nd->ndm_state & NUD_PROBE));
1387 L_setbool(s->L, "failed", (nd->ndm_state & NUD_FAILED));
1388 L_setbool(s->L, "noarp", (nd->ndm_state & NUD_NOARP));
1389 L_setbool(s->L, "permanent", (nd->ndm_state & NUD_PERMANENT));
1392 L_setaddr(s->L, "dest", nd->ndm_family, dst, -1);
1395 L_setaddr(s->L, "mac", AF_PACKET, mac, -1);
1400 lua_call(s->L, 1, 0);
1401 else if (hdr->nlmsg_flags & NLM_F_MULTI)
1402 lua_rawseti(s->L, -2, s->index);
1405 s->pending = !!(hdr->nlmsg_flags & NLM_F_MULTI);
1409 static int neighbor_dump(lua_State *L)
1413 struct ether_addr *mac;
1414 struct dump_filter filter = { .type = 0xFF & ~NUD_NOARP };
1415 struct dump_state st = {
1416 .callback = lua_isfunction(L, 2),
1422 if (lua_type(L, 1) == LUA_TTABLE)
1424 filter.family = L_getint(L, 1, "family");
1426 if (filter.family == 4)
1427 filter.family = AF_INET;
1428 else if (filter.family == 6)
1429 filter.family = AF_INET6;
1433 if ((s = L_getstr(L, 1, "dev")) != NULL)
1434 filter.iif = if_nametoindex(s);
1436 if ((s = L_getstr(L, 1, "dest")) != NULL && parse_cidr(s, &p))
1439 if ((s = L_getstr(L, 1, "mac")) != NULL &&
1440 (mac = ether_aton(s)) != NULL)
1446 sock = nl_socket_alloc();
1448 return _error(L, -1, "Out of memory");
1450 if (nl_connect(sock, NETLINK_ROUTE))
1451 return _error(L, 0, NULL);
1455 struct nl_cb *cb = nl_cb_alloc(NL_CB_DEFAULT);
1456 struct ndmsg ndm = {
1457 .ndm_family = filter.family
1460 msg = nlmsg_alloc_simple(RTM_GETNEIGH, NLM_F_REQUEST | NLM_F_DUMP);
1464 nlmsg_append(msg, &ndm, sizeof(ndm), 0);
1466 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, cb_dump_neigh, &st);
1467 nl_cb_set(cb, NL_CB_FINISH, NL_CB_CUSTOM, cb_done, &st);
1468 nl_cb_err(cb, NL_CB_CUSTOM, cb_error, &st);
1470 nl_send_auto_complete(sock, msg);
1475 while (st.pending > 0)
1476 nl_recvmsgs(sock, cb);
1482 return (st.callback == 0);
1486 static int cb_dump_link(struct nl_msg *msg, void *arg)
1489 struct dump_state *s = arg;
1490 struct nlmsghdr *hdr = nlmsg_hdr(msg);
1491 struct ifinfomsg *ifm = NLMSG_DATA(hdr);
1492 struct nlattr *tb[IFLA_MAX+1];
1495 if (hdr->nlmsg_type != RTM_NEWLINK)
1498 nlmsg_parse(hdr, sizeof(*ifm), tb, IFLA_MAX, NULL);
1500 L_setbool(s->L, "up", (ifm->ifi_flags & IFF_RUNNING));
1501 L_setint(s->L, "type", ifm->ifi_type);
1502 L_setstr(s->L, "name", if_indextoname(ifm->ifi_index, buf));
1505 L_setint(s->L, "mtu", RTA_U32(tb[IFLA_MTU]));
1507 if (tb[IFLA_TXQLEN])
1508 L_setint(s->L, "qlen", RTA_U32(tb[IFLA_TXQLEN]));
1510 if (tb[IFLA_MASTER])
1511 L_setdev(s->L, "master", tb[IFLA_MASTER]);
1513 if (tb[IFLA_ADDRESS] && nla_len(tb[IFLA_ADDRESS]) == AF_BYTES(AF_PACKET))
1514 L_setaddr(s->L, "mac", AF_PACKET, nla_get_string(tb[IFLA_ADDRESS]), -1);
1520 static int link_get(lua_State *L)
1522 const char *dev = luaL_checkstring(L, 1);
1523 struct dump_state st = {
1530 sock = nl_socket_alloc();
1532 return _error(L, -1, "Out of memory");
1534 if (nl_connect(sock, NETLINK_ROUTE))
1535 return _error(L, 0, NULL);
1538 struct nl_msg *msg = nlmsg_alloc_simple(RTM_GETLINK, NLM_F_REQUEST);
1539 struct nl_cb *cb = nl_cb_alloc(NL_CB_DEFAULT);
1540 struct ifinfomsg ifm = { .ifi_index = if_nametoindex(dev) };
1545 nlmsg_append(msg, &ifm, sizeof(ifm), 0);
1547 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, cb_dump_link, &st);
1548 nl_cb_set(cb, NL_CB_FINISH, NL_CB_CUSTOM, cb_done, &st);
1549 nl_cb_err(cb, NL_CB_CUSTOM, cb_error, &st);
1553 nl_send_auto_complete(sock, msg);
1555 while (st.pending > 0)
1556 nl_recvmsgs(sock, cb);
1565 static const luaL_reg ip_methods[] = {
1566 { "new", cidr_new },
1567 { "IPv4", cidr_ipv4 },
1568 { "IPv6", cidr_ipv6 },
1569 { "MAC", cidr_mac },
1571 { "checkip4", cidr_checkip4 },
1572 { "checkip6", cidr_checkip6 },
1573 { "checkmac", cidr_checkmac },
1575 { "route", route_get },
1576 { "routes", route_dump },
1578 { "neighbors", neighbor_dump },
1580 { "link", link_get },
1585 static const luaL_reg ip_cidr_methods[] = {
1586 { "is4", cidr_is4 },
1587 { "is4rfc1918", cidr_is4rfc1918 },
1588 { "is4linklocal", cidr_is4linklocal },
1589 { "is6", cidr_is6 },
1590 { "is6linklocal", cidr_is6linklocal },
1591 { "is6mapped4", cidr_is6mapped4 },
1592 { "ismac", cidr_ismac },
1593 { "ismaclocal", cidr_ismaclocal },
1594 { "ismacmcast", cidr_ismacmcast },
1595 { "lower", cidr_lower },
1596 { "higher", cidr_higher },
1597 { "equal", cidr_equal },
1598 { "prefix", cidr_prefix },
1599 { "network", cidr_network },
1600 { "host", cidr_host },
1601 { "mask", cidr_mask },
1602 { "broadcast", cidr_broadcast },
1603 { "mapped4", cidr_mapped4 },
1604 { "tomac", cidr_tomac },
1605 { "tolinklocal", cidr_tolinklocal },
1606 { "contains", cidr_contains },
1607 { "add", cidr_add },
1608 { "sub", cidr_sub },
1609 { "minhost", cidr_minhost },
1610 { "maxhost", cidr_maxhost },
1611 { "string", cidr_tostring },
1613 { "__lt", cidr_lower },
1614 { "__le", cidr_lower_equal },
1615 { "__eq", cidr_equal },
1616 { "__add", cidr_add },
1617 { "__sub", cidr_sub },
1618 { "__gc", cidr_gc },
1619 { "__tostring", cidr_tostring },
1624 int luaopen_luci_ip(lua_State *L)
1626 luaL_register(L, LUCI_IP, ip_methods);
1628 luaL_newmetatable(L, LUCI_IP_CIDR);
1629 luaL_register(L, NULL, ip_cidr_methods);
1630 lua_pushvalue(L, -1);
1631 lua_setfield(L, -2, "__index");