diff --git a/mud b/mud index 2d4804a..d4546cc 160000 --- a/mud +++ b/mud @@ -1 +1 @@ -Subproject commit 2d4804af5af8d406c117ce44d6ecd21191dd1c74 +Subproject commit d4546ccae0d4960641b261efe1d0e7a8b2b8a1fe diff --git a/src/ctl.h b/src/ctl.h index b4593d3..c3a4ad5 100644 --- a/src/ctl.h +++ b/src/ctl.h @@ -2,8 +2,10 @@ enum ctl_type { CTL_UNKNOWN, + CTL_PATH_ADD, + CTL_PATH_DEL, CTL_PING, - CTL_PONG, + CTL_REPLY, }; struct ctl_msg { @@ -12,6 +14,12 @@ struct ctl_msg { struct { enum ctl_type type; } unknown; + struct { + struct { + char addr[256]; + } add, del; + } path; + int reply; }; }; diff --git a/src/main.c b/src/main.c index 3f97171..73512a8 100644 --- a/src/main.c +++ b/src/main.c @@ -269,7 +269,7 @@ main(int argc, char **argv) int icmp_fd = -1; - if (gt.ipv4 && gt.mtu_auto) { + if (gt.ipv4 && gt.mtu_auto && gt.host) { icmp_fd = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP); if (icmp_fd == -1) @@ -389,8 +389,7 @@ main(int argc, char **argv) } if (FD_ISSET(ctl_fd, &rfds)) { - struct ctl_msg msg; - struct ctl_msg reply; + struct ctl_msg msg, reply = {.type = CTL_REPLY}; struct sockaddr_storage ss; socklen_t sl = sizeof(ss); @@ -400,10 +399,23 @@ main(int argc, char **argv) if (r == (ssize_t)sizeof(msg)) { switch (msg.type) { + case CTL_PATH_ADD: + gt_log("[ctl path add] addr=%s\n", + &msg.path.add.addr[0]); + if (mud_add_path(mud, &msg.path.add.addr[0])) { + reply.reply = errno; + perror("mud_add_path"); + } + break; + case CTL_PATH_DEL: + gt_log("[ctl path del] addr=%s\n", + &msg.path.del.addr[0]); + if (mud_del_path(mud, &msg.path.del.addr[0])) { + reply.reply = errno; + perror("mud_del_path"); + } + break; case CTL_PING: - reply = (struct ctl_msg){ - .type = CTL_PONG, - }; break; default: reply = (struct ctl_msg){ diff --git a/src/mainctl.c b/src/mainctl.c index cd0e12c..3c4e87b 100644 --- a/src/mainctl.c +++ b/src/mainctl.c @@ -14,6 +14,12 @@ static struct { char *dev; int version; + struct { + struct { + int set; + const char *addr; + } add, del; + } path; } gt = { .dev = "tun0", }; @@ -23,10 +29,18 @@ gt_setup_option(int argc, char **argv) { // clang-format off + struct option path_opts[] = { + { "add", >.path.add.addr, option_str }, + { "del", >.path.del.addr, option_str }, + { NULL }, + }; + struct option opts[] = { - { "dev", >.dev, option_str }, - { "version", NULL, option_option }, - { NULL }, + { "dev", >.dev, option_str }, + { "path", &path_opts, option_option }, + { "version", NULL, option_option }, + { NULL }, + }; // clang-format on @@ -34,6 +48,8 @@ gt_setup_option(int argc, char **argv) if (option(opts, argc, argv)) return 1; + gt.path.add.set = option_is_set(path_opts, "add"); + gt.path.del.set = option_is_set(path_opts, "del"); gt.version = option_is_set(opts, "version"); return 0; @@ -62,9 +78,19 @@ main(int argc, char **argv) return 1; } - struct ctl_msg msg = { - .type = CTL_PING, - }; + struct ctl_msg msg; + + if (gt.path.add.set) { + msg = (struct ctl_msg){ + .type = CTL_PATH_ADD, + }; + str_cpy(msg.path.add.addr, sizeof(msg.path.add.addr) - 1, gt.path.add.addr); + } else if (gt.path.del.set) { + msg = (struct ctl_msg){ + .type = CTL_PATH_DEL, + }; + str_cpy(msg.path.del.addr, sizeof(msg.path.del.addr) - 1, gt.path.del.addr); + } if (send(ctl_fd, &msg, sizeof(msg), 0) == -1) { perror("send"); @@ -79,8 +105,11 @@ main(int argc, char **argv) } switch (reply.type) { - case CTL_PONG: - gt_print("PONG!\n"); + case CTL_REPLY: + if (reply.reply) { + errno = reply.reply; + perror("error"); + } break; case CTL_UNKNOWN: gt_print("unknown command: %i\n", reply.unknown.type); diff --git a/systemd/glorytun-setup b/systemd/glorytun-setup index b630886..af95fec 100755 --- a/systemd/glorytun-setup +++ b/systemd/glorytun-setup @@ -51,8 +51,9 @@ PREF=32765 TABLE=200 # keep the current route to HOST -src=$(ip route get "$HOST" | awk '/src/{getline;print $0}' RS=' ') -ip rule add from "$src" table main pref "$((PREF-1))" || true +SRC=$(ip route get "$HOST" | awk '/src/{getline;print $0}' RS=' ') +ip rule add from "$SRC" table main pref "$((PREF-1))" || true +glorytunctl path add "$SRC" dev "$DEV" # forward everything else to the tunnel ip rule add from all table "$TABLE" pref "$PREF" || true