]>
git.proxmox.com Git - mirror_frr.git/blob - nhrpd/linux.c
1 /* NHRP daemon Linux specific glue
2 * Copyright (c) 2014-2015 Timo Teräs
4 * This file is free software: you may copy, redistribute and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
19 #include <sys/ioctl.h>
20 #include <sys/socket.h>
21 #include <sys/types.h>
22 #include <asm/types.h>
23 #include <arpa/inet.h>
24 #include <linux/netlink.h>
25 #include <linux/rtnetlink.h>
27 #include <linux/if_arp.h>
28 #include <linux/if_tunnel.h>
29 #include <linux/limits.h>
31 #include "nhrp_protocol.h"
36 size_t strlcpy(char *__restrict dest
,
37 const char *__restrict src
, size_t destsize
);
40 static int nhrp_socket_fd
= -1;
44 if (nhrp_socket_fd
< 0)
46 socket(PF_PACKET
, SOCK_DGRAM
, htons(ETH_P_NHRP
));
47 return nhrp_socket_fd
;
50 int os_sendmsg(const uint8_t *buf
, size_t len
, int ifindex
, const uint8_t *addr
,
51 size_t addrlen
, uint16_t protocol
)
53 struct sockaddr_ll lladdr
;
55 .iov_base
= (void *)buf
, .iov_len
= len
,
59 .msg_namelen
= sizeof(lladdr
),
65 if (addrlen
> sizeof(lladdr
.sll_addr
))
68 memset(&lladdr
, 0, sizeof(lladdr
));
69 lladdr
.sll_family
= AF_PACKET
;
70 lladdr
.sll_protocol
= htons(protocol
);
71 lladdr
.sll_ifindex
= ifindex
;
72 lladdr
.sll_halen
= addrlen
;
73 memcpy(lladdr
.sll_addr
, addr
, addrlen
);
79 status
= sendmsg(fd
, &msg
, 0);
86 int os_recvmsg(uint8_t *buf
, size_t *len
, int *ifindex
, uint8_t *addr
,
89 struct sockaddr_ll lladdr
;
91 .iov_base
= buf
, .iov_len
= *len
,
95 .msg_namelen
= sizeof(lladdr
),
101 r
= recvmsg(nhrp_socket_fd
, &msg
, MSG_DONTWAIT
);
106 *ifindex
= lladdr
.sll_ifindex
;
108 if (*addrlen
<= (size_t)lladdr
.sll_addr
) {
109 if (memcmp(lladdr
.sll_addr
, "\x00\x00\x00\x00", 4) != 0) {
110 memcpy(addr
, lladdr
.sll_addr
, lladdr
.sll_halen
);
111 *addrlen
= lladdr
.sll_halen
;
120 static int linux_configure_arp(const char *iface
, int on
)
124 strlcpy(ifr
.ifr_name
, iface
, IFNAMSIZ
);
125 if (ioctl(nhrp_socket_fd
, SIOCGIFFLAGS
, &ifr
))
129 ifr
.ifr_flags
&= ~IFF_NOARP
;
131 ifr
.ifr_flags
|= IFF_NOARP
;
133 if (ioctl(nhrp_socket_fd
, SIOCSIFFLAGS
, &ifr
))
139 static int linux_icmp_redirect_off(const char *iface
)
141 char fname
[PATH_MAX
];
144 snprintf(fname
, sizeof(fname
),
145 "/proc/sys/net/ipv4/conf/%s/send_redirects", iface
);
146 fd
= open(fname
, O_WRONLY
);
149 if (write(fd
, "0\n", 2) == 2)
156 int os_configure_dmvpn(unsigned int ifindex
, const char *ifname
, int af
)
162 ret
|= linux_icmp_redirect_off("all");
163 ret
|= linux_icmp_redirect_off(ifname
);
166 ret
|= linux_configure_arp(ifname
, 1);