]>
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.
18 #include <sys/ioctl.h>
19 #include <sys/socket.h>
20 #include <sys/types.h>
21 #include <asm/types.h>
22 #include <arpa/inet.h>
23 #include <linux/netlink.h>
24 #include <linux/rtnetlink.h>
26 #include <linux/if_arp.h>
27 #include <linux/if_tunnel.h>
28 #include <linux/limits.h>
30 #include "nhrp_protocol.h"
34 static int nhrp_socket_fd
= -1;
38 if (nhrp_socket_fd
< 0)
40 socket(PF_PACKET
, SOCK_DGRAM
, htons(ETH_P_NHRP
));
41 return nhrp_socket_fd
;
44 int os_sendmsg(const uint8_t *buf
, size_t len
, int ifindex
, const uint8_t *addr
,
47 struct sockaddr_ll lladdr
;
49 .iov_base
= (void *)buf
, .iov_len
= len
,
53 .msg_namelen
= sizeof(lladdr
),
59 if (addrlen
> sizeof(lladdr
.sll_addr
))
62 memset(&lladdr
, 0, sizeof(lladdr
));
63 lladdr
.sll_family
= AF_PACKET
;
64 lladdr
.sll_protocol
= htons(ETH_P_NHRP
);
65 lladdr
.sll_ifindex
= ifindex
;
66 lladdr
.sll_halen
= addrlen
;
67 memcpy(lladdr
.sll_addr
, addr
, addrlen
);
69 status
= sendmsg(nhrp_socket_fd
, &msg
, 0);
76 int os_recvmsg(uint8_t *buf
, size_t *len
, int *ifindex
, uint8_t *addr
,
79 struct sockaddr_ll lladdr
;
81 .iov_base
= buf
, .iov_len
= *len
,
85 .msg_namelen
= sizeof(lladdr
),
91 r
= recvmsg(nhrp_socket_fd
, &msg
, MSG_DONTWAIT
);
96 *ifindex
= lladdr
.sll_ifindex
;
98 if (*addrlen
<= (size_t)lladdr
.sll_addr
) {
99 if (memcmp(lladdr
.sll_addr
, "\x00\x00\x00\x00", 4) != 0) {
100 memcpy(addr
, lladdr
.sll_addr
, lladdr
.sll_halen
);
101 *addrlen
= lladdr
.sll_halen
;
110 static int linux_configure_arp(const char *iface
, int on
)
114 strncpy(ifr
.ifr_name
, iface
, IFNAMSIZ
- 1);
115 if (ioctl(nhrp_socket_fd
, SIOCGIFFLAGS
, &ifr
))
119 ifr
.ifr_flags
&= ~IFF_NOARP
;
121 ifr
.ifr_flags
|= IFF_NOARP
;
123 if (ioctl(nhrp_socket_fd
, SIOCSIFFLAGS
, &ifr
))
129 static int linux_icmp_redirect_off(const char *iface
)
131 char fname
[PATH_MAX
];
134 snprintf(fname
, sizeof(fname
),
135 "/proc/sys/net/ipv4/conf/%s/send_redirects", iface
);
136 fd
= open(fname
, O_WRONLY
);
139 if (write(fd
, "0\n", 2) == 2)
146 int os_configure_dmvpn(unsigned int ifindex
, const char *ifname
, int af
)
152 ret
|= linux_icmp_redirect_off("all");
153 ret
|= linux_icmp_redirect_off(ifname
);
156 ret
|= linux_configure_arp(ifname
, 1);
157 ret
|= netlink_configure_arp(ifindex
, af
);