// File: /workspace/github_c_makefile_star2_O3/open-mesh-mirror[P]batmand/linux/route.c
// Function: add_del_route
void add_del_route(uint32_t dest, uint8_t netmask, uint32_t router, uint32_t src_ip, int32_t ifi, const char *dev, uint8_t rt_table, int8_t route_type, int8_t route_action)
{
	int netlink_sock;
	size_t len;
	uint32_t my_router;
	char buf[4096], str1[16], str2[16], str3[16];
	struct rtattr *rta;
	struct sockaddr_nl nladdr;
	struct iovec iov;
	struct msghdr msg;
	struct nlmsghdr *nh;
	struct req_s {
		struct rtmsg rtm;
		char buff[4 * (sizeof(struct rtattr) + 4)];
	} *req;
	char req_buf[NLMSG_LENGTH(sizeof(struct req_s))] ALIGN_WORD;

	iov.iov_base = buf;
	iov.iov_len  = sizeof(buf);

	inet_ntop(AF_INET, &dest, str1, sizeof(str1));
	inet_ntop(AF_INET, &router, str2, sizeof(str2));
	inet_ntop(AF_INET, &src_ip, str3, sizeof(str3));

	if (policy_routing_script != NULL) {
		dprintf(policy_routing_pipe, "ROUTE %s %s %s %i %s %s %i %s %i\n", (route_action == ROUTE_DEL ? "del" : "add"), route_type_to_string_script[route_type], str1, netmask, str2, str3, ifi, dev, rt_table);
		return;
	}

	/* adding default route */
	if ((router == dest) && (dest == 0)) {

		if (route_type != ROUTE_TYPE_UNREACHABLE) {
			debug_output(3, "%s default route via %s (table %i)\n", (route_action == ROUTE_DEL ? "Deleting" : "Adding"), dev, rt_table);
			debug_output(4, "%s default route via %s (table %i)\n", (route_action == ROUTE_DEL ? "Deleting" : "Adding"), dev, rt_table);
		}

		my_router = router;

	/* single hop neigbor */
	} else if ((router == dest) && (dest != 0)) {

		debug_output(3, "%s route to %s via 0.0.0.0 (table %i - %s)\n", (route_action == ROUTE_DEL ? "Deleting" : "Adding"), str1, rt_table, dev);
		debug_output(4, "%s route to %s via 0.0.0.0 (table %i - %s)\n", (route_action == ROUTE_DEL ? "Deleting" : "Adding"), str1, rt_table, dev);
		my_router = 0;

	/* multihop neighbor */
	} else {

		debug_output(3, "%s %s to %s/%i via %s (table %i - %s)\n", (route_action == ROUTE_DEL ? "Deleting" : "Adding"), route_type_to_string[route_type], str1, netmask, str2, rt_table, dev);
		debug_output(4, "%s %s to %s/%i via %s (table %i - %s)\n", (route_action == ROUTE_DEL ? "Deleting" : "Adding"), route_type_to_string[route_type], str1, netmask, str2, rt_table, dev);
		my_router = router;

	}


	nh = (struct nlmsghdr *)req_buf;
	req = (struct req_s*)NLMSG_DATA(req_buf);
	memset(&nladdr, 0, sizeof(struct sockaddr_nl));
	memset(req_buf, 0, NLMSG_LENGTH(sizeof(struct req_s)));
	memset(&msg, 0, sizeof(struct msghdr));

	nladdr.nl_family = AF_NETLINK;

	len = sizeof(struct rtmsg) + sizeof(struct rtattr) + 4;

	/* additional information for the outgoing interface and the gateway */
	if (route_type == ROUTE_TYPE_UNICAST)
		len += 2 * (sizeof(struct rtattr) + 4);

	if (src_ip != 0)
		len += sizeof(struct rtattr) + 4;

	nh->nlmsg_len = NLMSG_LENGTH(len);
	nh->nlmsg_pid = getpid();
	req->rtm.rtm_family = AF_INET;
	req->rtm.rtm_table = rt_table;
	req->rtm.rtm_dst_len = netmask;

	if (route_action == ROUTE_DEL) {

		nh->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
		nh->nlmsg_type = RTM_DELROUTE;
		req->rtm.rtm_scope = RT_SCOPE_NOWHERE;

	} else {

		nh->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK | NLM_F_CREATE | NLM_F_APPEND;
		nh->nlmsg_type = RTM_NEWROUTE;

		if (route_type == ROUTE_TYPE_UNICAST && my_router == 0 && src_ip != 0)
			req->rtm.rtm_scope = RT_SCOPE_LINK;
		else
			req->rtm.rtm_scope = RT_SCOPE_UNIVERSE;

		req->rtm.rtm_protocol = RTPROT_STATIC; /* may be changed to some batman specific value - see <linux/rtnetlink.h> */

		switch(route_type) {
		case ROUTE_TYPE_UNICAST:
			req->rtm.rtm_type = RTN_UNICAST;
			break;
		case ROUTE_TYPE_THROW:
			req->rtm.rtm_type = RTN_THROW;
			break;
		case ROUTE_TYPE_UNREACHABLE:
			req->rtm.rtm_type = RTN_UNREACHABLE;
			break;
		default:
			debug_output(0, "Error - unknown route type (add_del_route): %i\n", route_type);
			return;
		}
	}

	rta = (struct rtattr *)req->buff;
	rta->rta_type = RTA_DST;
	rta->rta_len = sizeof(struct rtattr) + 4;
	memcpy(((char *)req->buff) + sizeof(struct rtattr), (char *)&dest, 4);

	if (route_type == ROUTE_TYPE_UNICAST) {

		rta = (struct rtattr *)(req->buff + sizeof(struct rtattr) + 4);
		rta->rta_type = RTA_GATEWAY;
		rta->rta_len = sizeof(struct rtattr) + 4;
		memcpy(((char *)req->buff) + 2 * sizeof(struct rtattr) + 4, (char *)&my_router, 4);

		rta = (struct rtattr *)(req->buff + 2 * sizeof(struct rtattr) + 8);
		rta->rta_type = RTA_OIF;
		rta->rta_len = sizeof(struct rtattr) + 4;
		memcpy(((char *)req->buff) + 3 * sizeof(struct rtattr) + 8, (char *)&ifi, 4);

		if (src_ip != 0) {
			rta = (struct rtattr *)(req->buff + 3 * sizeof(struct rtattr) + 12);
			rta->rta_type = RTA_PREFSRC;
			rta->rta_len = sizeof(struct rtattr) + 4;
			memcpy(((char *)req->buff) + 4 * sizeof(struct rtattr) + 12, (char *)&src_ip, 4);
		}

	}

	if ((netlink_sock = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE)) < 0) {

		debug_output(0, "Error - can't create netlink socket for routing table manipulation: %s\n", strerror(errno));
		return;

	}

	if (sendto(netlink_sock, req_buf, nh->nlmsg_len, 0, (struct sockaddr *)&nladdr, sizeof(struct sockaddr_nl)) < 0) {

		debug_output(0, "Error - can't send message to kernel via netlink socket for routing table manipulation: %s\n", strerror(errno));
		close(netlink_sock);
		return;

	}


	msg.msg_name = (void *)&nladdr;
	msg.msg_namelen = sizeof(nladdr);
	msg.msg_iov = &iov;
	msg.msg_iovlen = 1;
	msg.msg_control = NULL;

	len = recvmsg(netlink_sock, &msg, 0);
	nh = (struct nlmsghdr *)buf;

	while (NLMSG_OK(nh, len)) {

		if (nh->nlmsg_type == NLMSG_DONE)
			break;

		if (( nh->nlmsg_type == NLMSG_ERROR) && (((struct nlmsgerr*)NLMSG_DATA(nh))->error != 0))
			debug_output(0, "Error - can't %s %s to %s/%i via %s (table %i): %s\n", (route_action == ROUTE_DEL ? "delete" : "add"), route_type_to_string[route_type], str1, netmask, str2, rt_table, strerror(-((struct nlmsgerr*)NLMSG_DATA(nh))->error));

		nh = NLMSG_NEXT(nh, len);

	}

	close(netlink_sock);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1108, %rsp           # imm = 0x1108
movq %r9, %rbx
movl %r8d, %r15d
movl %esi, %r14d
leaq 0xc(%rsp), %rsi
movl %edi, (%rsi)
leaq 0x14(%rsp), %r12
movl %edx, (%r12)
leaq 0x10(%rsp), %r13
movl %ecx, (%r13)
leaq 0x100(%rsp), %rbp
movq %rbp, 0xa8(%rsp)
movq $0x1000, 0xb0(%rsp) # imm = 0x1000
leaq 0x70(%rsp), %rdx
movl $0x2, %edi
movl $0x10, %ecx
callq 0x2030
leaq 0x90(%rsp), %rdx
movl $0x2, %edi
movq %r12, %rsi
movl $0x10, %ecx
callq 0x2030
leaq 0xf0(%rsp), %r12
movl $0x2, %edi
movq %r13, %rsi
movq %r12, %rdx
movl $0x10, %ecx
callq 0x2030
leaq 0xf563(%rip), %rax      # 0x172d8
cmpq $0x0, (%rax)
je 0x7df3
leaq 0xf55e(%rip), %rax      # 0x172e0
movl (%rax), %edi
cmpb $0x1, 0x1150(%rsp)
leaq 0x9158(%rip), %rax      # 0x10eeb
leaq 0x9155(%rip), %rdx      # 0x10eef
cmoveq %rax, %rdx
movsbq 0x1148(%rsp), %rax
leaq 0x90ee(%rip), %rsi      # 0x10e9c
movslq (%rsi,%rax,4), %rcx
addq %rsi, %rcx
movzbl %r14b, %r9d
movzbl 0x1140(%rsp), %r10d
subq $0x8, %rsp
leaq 0x90fc(%rip), %rsi      # 0x10ec9
leaq 0x78(%rsp), %r8
xorl %eax, %eax
pushq %r10
pushq %rbx
pushq %r15
pushq %r12
leaq 0xb8(%rsp), %r10
pushq %r10
callq 0x2200
addq $0x30, %rsp
jmp 0x82e7
movq %rbx, %rsi
movq %r15, 0x18(%rsp)
movl %r14d, 0x20(%rsp)
movl 0x14(%rsp), %r13d
movl 0xc(%rsp), %eax
movl %r13d, %ecx
orl %eax, %ecx
jne 0x7e86
movb 0x1148(%rsp), %bl
cmpb $0x2, %bl
movb 0x1140(%rsp), %r14b
je 0x7fb7
movq %rsi, %r15
cmpb $0x1, 0x1150(%rsp)
leaq 0x90dd(%rip), %rax      # 0x10f17
leaq 0x90df(%rip), %r12      # 0x10f20
cmoveq %rax, %r12
movzbl %r14b, %r13d
leaq 0x90a3(%rip), %rsi      # 0x10ef3
movl $0x3, %edi
movq %r12, %rdx
movq %r15, %rcx
movl %r13d, %r8d
xorl %eax, %eax
callq 0xd88c
movl $0x4, %edi
leaq 0x9082(%rip), %rsi      # 0x10ef3
movq %r12, %rdx
movq %r15, %rcx
movl %r13d, %r8d
xorl %eax, %eax
callq 0xd88c
jmp 0x7fb2
cmpl %eax, %r13d
setne %cl
testl %eax, %eax
sete %al
cmpb $0x1, 0x1150(%rsp)
leaq 0x9077(%rip), %rdx      # 0x10f17
leaq 0x9079(%rip), %r12      # 0x10f20
cmoveq %rdx, %r12
orb %cl, %al
movq %rsi, %r15
jne 0x7f0f
movb 0x1140(%rsp), %r14b
movzbl %r14b, %ebx
leaq 0x9062(%rip), %rsi      # 0x10f27
xorl %r13d, %r13d
leaq 0x70(%rsp), %rcx
movl $0x3, %edi
movq %r12, %rdx
movl %ebx, %r8d
movq %r15, %r9
xorl %eax, %eax
callq 0xd88c
movl $0x4, %edi
leaq 0x9039(%rip), %rsi      # 0x10f27
movq %r12, %rdx
leaq 0x70(%rsp), %rcx
movl %ebx, %r8d
movq %r15, %r9
xorl %eax, %eax
callq 0xd88c
movb 0x1148(%rsp), %bl
jmp 0x7fb7
movb 0x1148(%rsp), %bl
movsbq %bl, %rax
leaq 0xedbf(%rip), %rcx      # 0x16ce0
movq (%rcx,%rax,8), %r13
movzbl 0x20(%rsp), %r9d
movl %r9d, 0x2c(%rsp)
movb 0x1140(%rsp), %r14b
movzbl %r14b, %r10d
movq %r10, 0xa0(%rsp)
subq $0x8, %rsp
leaq 0x9004(%rip), %rsi      # 0x10f53
leaq 0x78(%rsp), %r8
movl $0x3, %edi
movq %r12, %rdx
movq %r13, %rcx
xorl %eax, %eax
pushq %r15
pushq %r10
leaq 0xa8(%rsp), %r10
pushq %r10
callq 0xd88c
addq $0x18, %rsp
movl $0x4, %edi
leaq 0x8fcf(%rip), %rsi      # 0x10f53
movq %r12, %rdx
movq %r13, %rcx
leaq 0x78(%rsp), %r8
movl 0x34(%rsp), %r9d
xorl %eax, %eax
pushq %r15
pushq 0xb0(%rsp)
leaq 0xa8(%rsp), %r10
pushq %r10
callq 0xd88c
addq $0x20, %rsp
movl 0x14(%rsp), %r13d
movq $0x0, 0x80(%rsp)
movl $0x0, 0x88(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x30(%rsp)
movaps %xmm0, 0x40(%rsp)
movaps %xmm0, 0x50(%rsp)
movups %xmm0, 0x5c(%rsp)
movups %xmm0, 0xc0(%rsp)
movups %xmm0, 0xd0(%rsp)
movups %xmm0, 0xe0(%rsp)
xorl %eax, %eax
testb %bl, %bl
sete %al
shll $0x4, %eax
xorl %ecx, %ecx
cmpl $0x0, 0x10(%rsp)
movw $0x10, 0x80(%rsp)
setne %cl
leal (%rax,%rcx,8), %r15d
addl $0x24, %r15d
movl %r15d, 0x30(%rsp)
callq 0x2100
movl %eax, 0x3c(%rsp)
movb $0x2, 0x40(%rsp)
movb %r14b, 0x44(%rsp)
movl 0x20(%rsp), %r12d
movb %r12b, 0x41(%rsp)
cmpb $0x1, 0x1150(%rsp)
jne 0x807f
movl $0x50019, 0x34(%rsp)    # imm = 0x50019
movb $-0x1, 0x46(%rsp)
movl $0x10008, 0x4c(%rsp)    # imm = 0x10008
movl 0xc(%rsp), %eax
movl %eax, 0x50(%rsp)
testb %bl, %bl
jne 0x813c
movl 0x10(%rsp), %eax
movq 0x18(%rsp), %rsi
jmp 0x80c5
testb %bl, %bl
sete %al
movl $0xc050018, 0x34(%rsp)  # imm = 0xC050018
testl %r13d, %r13d
sete %cl
andb %al, %cl
movl 0x10(%rsp), %eax
testl %eax, %eax
setne %dl
andb %cl, %dl
cmpb $0x1, %dl
jne 0x80f0
movw $0xfd04, 0x45(%rsp)     # imm = 0xFD04
movq 0x18(%rsp), %rsi
movb $0x1, 0x47(%rsp)
movl $0x10008, 0x4c(%rsp)    # imm = 0x10008
movl 0xc(%rsp), %ecx
movl %ecx, 0x50(%rsp)
movl $0x50008, 0x54(%rsp)    # imm = 0x50008
movl %r13d, 0x58(%rsp)
movl $0x40008, 0x5c(%rsp)    # imm = 0x40008
movl %esi, 0x60(%rsp)
testl %eax, %eax
je 0x813c
movl $0x70008, 0x64(%rsp)    # imm = 0x70008
movl %eax, 0x68(%rsp)
jmp 0x813c
movsbl %bl, %edx
movw $0x4, 0x45(%rsp)
cmpl $0x2, %edx
je 0x8126
cmpl $0x1, %edx
movq 0x18(%rsp), %rsi
je 0x8122
testl %edx, %edx
je 0x80b0
leaq 0x8e66(%rip), %rsi      # 0x10f7a
xorl %edi, %edi
xorl %eax, %eax
callq 0xd88c
jmp 0x82e7
movb $0x9, %al
jmp 0x8128
movb $0x7, %al
movb %al, 0x47(%rsp)
movl $0x10008, 0x4c(%rsp)    # imm = 0x10008
movl 0xc(%rsp), %eax
movl %eax, 0x50(%rsp)
movl $0x10, %edi
movl $0x2, %esi
xorl %edx, %edx
callq 0x24e0
testl %eax, %eax
js 0x829f
movl %eax, %r14d
movl %r15d, %edx
leaq 0x30(%rsp), %rsi
leaq 0x80(%rsp), %r15
movl %eax, %edi
xorl %ecx, %ecx
movq %r15, %r8
movl $0xc, %r9d
callq 0x21e0
testq %rax, %rax
js 0x82c0
leaq 0xb8(%rsp), %rsi
movq %r15, (%rsi)
movl $0xc, 0x8(%rsi)
leaq 0xa8(%rsp), %rax
movq %rax, 0x10(%rsi)
movq $0x1, 0x18(%rsi)
movq $0x0, 0x20(%rsi)
movl %r14d, %edi
xorl %edx, %edx
callq 0x2360
cmpq $0x10, %rax
jb 0x82df
movq %rax, %r15
cmpb $0x1, 0x1150(%rsp)
leaq 0x8eab(%rip), %rax      # 0x11082
movl %r12d, %ecx
leaq 0x8d0e(%rip), %r12      # 0x10eef
cmoveq %rax, %r12
movsbq 0x1148(%rsp), %rax
movq %rax, 0x18(%rsp)
movzbl %cl, %r13d
movzbl 0x1140(%rsp), %eax
movq %rax, 0x20(%rsp)
movl (%rbp), %eax
cmpq $0x10, %rax
jb 0x82df
cmpq %rax, %r15
jb 0x82df
movzwl 0x4(%rbp), %ecx
cmpl $0x2, %ecx
je 0x822d
cmpl $0x3, %ecx
jne 0x8287
jmp 0x82df
movl 0x10(%rbp), %edi
testl %edi, %edi
je 0x8287
movq 0x18(%rsp), %rax
leaq 0xeaa0(%rip), %rcx      # 0x16ce0
movq (%rcx,%rax,8), %rbx
negl %edi
callq 0x2480
movq %rax, %r10
subq $0x8, %rsp
xorl %edi, %edi
leaq 0x8df3(%rip), %rsi      # 0x1104e
movq %r12, %rdx
movq %rbx, %rcx
leaq 0x78(%rsp), %r8
movl %r13d, %r9d
xorl %eax, %eax
pushq %r10
pushq 0x30(%rsp)
leaq 0xa8(%rsp), %r10
pushq %r10
callq 0xd88c
addq $0x20, %rsp
movl (%rbp), %eax
addl $0x3, %eax
andl $-0x4, %eax
subq %rax, %r15
addq %rax, %rbp
cmpq $0xf, %r15
ja 0x8204
jmp 0x82df
callq 0x2070
movl (%rax), %edi
callq 0x2480
leaq 0x8cf8(%rip), %rsi      # 0x10faa
xorl %edi, %edi
movq %rax, %rdx
xorl %eax, %eax
callq 0xd88c
jmp 0x82e7
callq 0x2070
movl (%rax), %edi
callq 0x2480
leaq 0x8d1f(%rip), %rsi      # 0x10ff2
xorl %edi, %edi
movq %rax, %rdx
xorl %eax, %eax
callq 0xd88c
movl %r14d, %edi
callq 0x21f0
addq $0x1108, %rsp           # imm = 0x1108
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq