]> git.proxmox.com Git - mirror_frr.git/blob - nhrpd/linux.c
lib: enforce vrf_name_to_id by returning default_vrf when name is null
[mirror_frr.git] / nhrpd / linux.c
1 /* NHRP daemon Linux specific glue
2 * Copyright (c) 2014-2015 Timo Teräs
3 *
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.
8 */
9
10 #ifdef HAVE_CONFIG_H
11 #include "config.h"
12 #endif
13
14 #include <fcntl.h>
15 #include <stdio.h>
16 #include <unistd.h>
17 #include <string.h>
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>
25 #include <linux/ip.h>
26 #include <linux/if_arp.h>
27 #include <linux/if_tunnel.h>
28
29 #include "nhrp_protocol.h"
30 #include "os.h"
31 #include "netlink.h"
32
33 static int nhrp_socket_fd = -1;
34
35 int os_socket(void)
36 {
37 if (nhrp_socket_fd < 0)
38 nhrp_socket_fd =
39 socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_NHRP));
40 return nhrp_socket_fd;
41 }
42
43 int os_sendmsg(const uint8_t *buf, size_t len, int ifindex, const uint8_t *addr,
44 size_t addrlen)
45 {
46 struct sockaddr_ll lladdr;
47 struct iovec iov = {
48 .iov_base = (void *)buf, .iov_len = len,
49 };
50 struct msghdr msg = {
51 .msg_name = &lladdr,
52 .msg_namelen = sizeof(lladdr),
53 .msg_iov = &iov,
54 .msg_iovlen = 1,
55 };
56 int status;
57
58 if (addrlen > sizeof(lladdr.sll_addr))
59 return -1;
60
61 memset(&lladdr, 0, sizeof(lladdr));
62 lladdr.sll_family = AF_PACKET;
63 lladdr.sll_protocol = htons(ETH_P_NHRP);
64 lladdr.sll_ifindex = ifindex;
65 lladdr.sll_halen = addrlen;
66 memcpy(lladdr.sll_addr, addr, addrlen);
67
68 status = sendmsg(nhrp_socket_fd, &msg, 0);
69 if (status < 0)
70 return -1;
71
72 return 0;
73 }
74
75 int os_recvmsg(uint8_t *buf, size_t *len, int *ifindex, uint8_t *addr,
76 size_t *addrlen)
77 {
78 struct sockaddr_ll lladdr;
79 struct iovec iov = {
80 .iov_base = buf, .iov_len = *len,
81 };
82 struct msghdr msg = {
83 .msg_name = &lladdr,
84 .msg_namelen = sizeof(lladdr),
85 .msg_iov = &iov,
86 .msg_iovlen = 1,
87 };
88 int r;
89
90 r = recvmsg(nhrp_socket_fd, &msg, MSG_DONTWAIT);
91 if (r < 0)
92 return r;
93
94 *len = r;
95 *ifindex = lladdr.sll_ifindex;
96
97 if (*addrlen <= (size_t)lladdr.sll_addr) {
98 if (memcmp(lladdr.sll_addr, "\x00\x00\x00\x00", 4) != 0) {
99 memcpy(addr, lladdr.sll_addr, lladdr.sll_halen);
100 *addrlen = lladdr.sll_halen;
101 } else {
102 *addrlen = 0;
103 }
104 }
105
106 return 0;
107 }
108
109 static int linux_configure_arp(const char *iface, int on)
110 {
111 struct ifreq ifr;
112
113 strncpy(ifr.ifr_name, iface, IFNAMSIZ - 1);
114 if (ioctl(nhrp_socket_fd, SIOCGIFFLAGS, &ifr))
115 return -1;
116
117 if (on)
118 ifr.ifr_flags &= ~IFF_NOARP;
119 else
120 ifr.ifr_flags |= IFF_NOARP;
121
122 if (ioctl(nhrp_socket_fd, SIOCSIFFLAGS, &ifr))
123 return -1;
124
125 return 0;
126 }
127
128 static int linux_icmp_redirect_off(const char *iface)
129 {
130 char fname[256];
131 int fd, ret = -1;
132
133 sprintf(fname, "/proc/sys/net/ipv4/conf/%s/send_redirects", iface);
134 fd = open(fname, O_WRONLY);
135 if (fd < 0)
136 return -1;
137 if (write(fd, "0\n", 2) == 2)
138 ret = 0;
139 close(fd);
140
141 return ret;
142 }
143
144 int os_configure_dmvpn(unsigned int ifindex, const char *ifname, int af)
145 {
146 int ret = 0;
147
148 switch (af) {
149 case AF_INET:
150 ret |= linux_icmp_redirect_off("all");
151 ret |= linux_icmp_redirect_off(ifname);
152 break;
153 }
154 ret |= linux_configure_arp(ifname, 1);
155 ret |= netlink_configure_arp(ifindex, af);
156
157 return ret;
158 }