76 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
78 #include <linux/module.h>
79 #include <linux/types.h>
80 #include <linux/string.h>
81 #include <linux/kernel.h>
82 #include <linux/capability.h>
83 #include <linux/socket.h>
85 #include <linux/errno.h>
90 #include <linux/netdevice.h>
93 #include <linux/if_arp.h>
97 #include <linux/stat.h>
99 #include <linux/net.h>
101 #include <linux/slab.h>
103 #include <linux/sysctl.h>
125 static int arp_constructor(
struct neighbour *neigh);
130 static const struct neigh_ops arp_generic_ops = {
132 .solicit = arp_solicit,
133 .error_report = arp_error_report,
138 static const struct neigh_ops arp_hh_ops = {
140 .solicit = arp_solicit,
141 .error_report = arp_error_report,
146 static const struct neigh_ops arp_direct_ops = {
152 static const struct neigh_ops arp_broken_ops = {
154 .solicit = arp_solicit,
155 .error_report = arp_error_report,
164 .constructor = arp_constructor,
165 .proxy_redo = parp_redo,
169 .base_reachable_time = 30 *
HZ,
170 .retrans_time = 1 *
HZ,
171 .gc_staletime = 60 *
HZ,
172 .reachable_time = 30 *
HZ,
173 .delay_probe_time = 5 *
HZ,
174 .queue_len_bytes = 64*1024,
177 .anycast_delay = 1 *
HZ,
178 .proxy_delay = (8 *
HZ) / 10,
195 ip_eth_mc_map(addr, haddr);
198 ip_ib_mc_map(addr, dev->
broadcast, haddr);
201 ip_ipgre_mc_map(addr, dev->
broadcast, haddr);
213 static u32 arp_hash(
const void *
pkey,
217 return arp_hashfn(*(
u32 *)pkey, dev, *hash_rnd);
220 static int arp_constructor(
struct neighbour *neigh)
224 struct in_device *in_dev;
228 in_dev = __in_dev_get_rcu(dev);
229 if (in_dev ==
NULL) {
236 parms = in_dev->arp_parms;
237 __neigh_parms_put(neigh->
parms);
243 neigh->
ops = &arp_direct_ops;
277 #if IS_ENABLED(CONFIG_AX25)
279 #if IS_ENABLED(CONFIG_NETROM)
282 neigh->
ops = &arp_broken_ops;
303 neigh->
ops = &arp_hh_ops;
305 neigh->
ops = &arp_generic_ops;
308 neigh->
output = neigh->
ops->connected_output;
317 dst_link_failure(skb);
328 struct in_device *in_dev;
331 in_dev = __in_dev_get_rcu(dev);
336 switch (IN_DEV_ARP_ANNOUNCE(in_dev)) {
341 saddr = ip_hdr(skb)->saddr;
346 saddr = ip_hdr(skb)->saddr;
362 probes -= neigh->
parms->ucast_probes;
365 pr_debug(
"trying to ucast probe in NUD_INVALID\n");
369 probes -= neigh->
parms->app_probes;
384 static int arp_ignore(
struct in_device *in_dev,
__be32 sip,
__be32 tip)
388 switch (IN_DEV_ARP_IGNORE(in_dev)) {
423 struct net *
net = dev_net(dev);
425 rt = ip_route_output(net, sip, tip, 0, 0);
428 if (rt->
dst.dev != dev) {
447 static int arp_set_predefined(
int addr_hint,
unsigned char *haddr,
452 pr_debug(
"arp called for own IP address\n");
473 pr_debug(
"arp_find is called with dst==NULL\n");
478 paddr = rt_nexthop(skb_rtable(skb), ip_hdr(skb)->
daddr);
479 if (arp_set_predefined(
inet_addr_type(dev_net(dev), paddr), haddr,
483 n = __neigh_lookup(&arp_tbl, &paddr, dev, 1);
488 neigh_ha_snapshot(haddr, n, dev);
504 static inline int arp_fwd_proxy(
struct in_device *in_dev,
507 struct in_device *out_dev;
510 if (rt->
dst.dev == dev)
513 if (!IN_DEV_PROXY_ARP(in_dev))
515 imi = IN_DEV_MEDIUM_ID(in_dev);
523 out_dev = __in_dev_get_rcu(rt->
dst.dev);
525 omi = IN_DEV_MEDIUM_ID(out_dev);
527 return omi != imi && omi != -1;
549 static inline int arp_fwd_pvlan(
struct in_device *in_dev,
554 if (rt->
dst.dev != dev)
561 if (IN_DEV_PROXY_ARP_PVLAN(in_dev))
577 const unsigned char *dest_hw,
578 const unsigned char *src_hw,
579 const unsigned char *target_hw)
583 unsigned char *arp_ptr;
591 skb = alloc_skb(arp_hdr_len(dev) + hlen + tlen,
GFP_ATOMIC);
595 skb_reserve(skb, hlen);
596 skb_reset_network_header(skb);
608 if (dev_hard_header(skb, dev, ptype, dest_hw, src_hw, skb->
len) < 0)
627 #if IS_ENABLED(CONFIG_AX25)
633 #if IS_ENABLED(CONFIG_NETROM)
641 #if IS_ENABLED(CONFIG_FDDI)
653 arp_ptr = (
unsigned char *)(arp + 1);
657 memcpy(arp_ptr, &src_ip, 4);
659 if (target_hw !=
NULL)
664 memcpy(arp_ptr, &dest_ip, 4);
689 const unsigned char *dest_hw,
const unsigned char *src_hw,
690 const unsigned char *target_hw)
701 skb =
arp_create(type, ptype, dest_ip, dev, src_ip,
702 dest_hw, src_hw, target_hw);
714 static int arp_process(
struct sk_buff *skb)
717 struct in_device *in_dev = __in_dev_get_rcu(dev);
719 unsigned char *arp_ptr;
726 struct net *net = dev_net(dev);
781 arp_ptr = (
unsigned char *)(arp + 1);
792 if (ipv4_is_multicast(tip) ||
793 (!IN_DEV_ROUTE_LOCALNET(in_dev) && ipv4_is_loopback(tip)))
823 !arp_ignore(in_dev, sip, tip))
832 rt = skb_rtable(skb);
838 dont_send = arp_ignore(in_dev, sip, tip);
839 if (!dont_send && IN_DEV_ARPFILTER(in_dev))
840 dont_send = arp_filter(sip, tip, dev);
851 }
else if (IN_DEV_FORWARD(in_dev)) {
853 (arp_fwd_proxy(in_dev, dev, rt) ||
854 arp_fwd_pvlan(in_dev, dev, rt, sip, tip) ||
855 (rt->
dst.dev != dev &&
863 in_dev->arp_parms->proxy_delay == 0) {
869 in_dev->arp_parms, skb);
879 n = __neigh_lookup(&arp_tbl, &sip, dev, 0);
881 if (IN_DEV_ARP_ACCEPT(in_dev)) {
890 n = __neigh_lookup(&arp_tbl, &sip, dev, 1);
920 static void parp_redo(
struct sk_buff *skb)
936 if (!pskb_may_pull(skb, arp_hdr_len(dev)))
969 static int arp_req_set_proxy(
struct net *net,
struct net_device *dev,
int on)
972 IPV4_DEVCONF_ALL(net, PROXY_ARP) = on;
975 if (__in_dev_get_rtnl(dev)) {
976 IN_DEV_CONF_SET(__in_dev_get_rtnl(dev), PROXY_ARP, on);
982 static int arp_req_set_public(
struct net *net,
struct arpreq *
r,
988 if (mask && mask !=
htonl(0xFFFFFFFF))
1002 return arp_req_set_proxy(net, dev, 1);
1005 static int arp_req_set(
struct net *net,
struct arpreq *r,
1013 return arp_req_set_public(net, r, dev);
1028 switch (dev->
type) {
1029 #if IS_ENABLED(CONFIG_FDDI)
1049 neigh = __neigh_lookup_errno(&arp_tbl, &ip, dev);
1050 err = PTR_ERR(neigh);
1051 if (!IS_ERR(neigh)) {
1059 neigh_release(neigh);
1064 static unsigned int arp_state_to_flags(
struct neighbour *neigh)
1088 r->
arp_flags = arp_state_to_flags(neigh);
1092 neigh_release(neigh);
1108 neigh_release(neigh);
1115 static int arp_req_delete_public(
struct net *net,
struct arpreq *r,
1121 if (mask ==
htonl(0xFFFFFFFF))
1127 return arp_req_set_proxy(net, dev, 0);
1130 static int arp_req_delete(
struct net *net,
struct arpreq *r,
1136 return arp_req_delete_public(net, r, dev);
1183 htonl(0xFFFFFFFFUL);
1204 err = arp_req_delete(net, &r, dev);
1207 err = arp_req_set(net, &r, dev);
1210 err = arp_req_get(&r, dev);
1238 .notifier_call = arp_netdev_event,
1260 static int arp_proc_init(
void);
1268 #ifdef CONFIG_SYSCTL
1274 #ifdef CONFIG_PROC_FS
1275 #if IS_ENABLED(CONFIG_AX25)
1286 for (n = 0, s = buf; n < 6; n++) {
1303 if (*buf ==
'\0' || *buf ==
'-')
1310 #define HBUFFERLEN 30
1312 static void arp_format_neigh_entry(
struct seq_file *seq,
1315 char hbuffer[HBUFFERLEN];
1319 int hatype = dev->
type;
1323 #if IS_ENABLED(CONFIG_AX25)
1328 for (k = 0, j = 0; k < HBUFFERLEN - 3 && j < dev->
addr_len; j++) {
1336 #if IS_ENABLED(CONFIG_AX25)
1340 seq_printf(seq,
"%-16s 0x%-10x0x%-10x%s * %s\n",
1341 tbuf, hatype, arp_state_to_flags(n), hbuffer, dev->
name);
1345 static void arp_format_pneigh_entry(
struct seq_file *seq,
1349 int hatype = dev ? dev->
type : 0;
1353 seq_printf(seq,
"%-16s 0x%-10x0x%-10x%s * %s\n",
1355 dev ? dev->
name :
"*");
1358 static int arp_seq_show(
struct seq_file *seq,
void *
v)
1361 seq_puts(seq,
"IP address HW type Flags "
1362 "HW address Mask Device\n");
1367 arp_format_pneigh_entry(seq, v);
1369 arp_format_neigh_entry(seq, v);
1375 static void *arp_seq_start(
struct seq_file *seq, loff_t *
pos)
1386 .
start = arp_seq_start,
1389 .show = arp_seq_show,
1400 .open = arp_seq_open,
1407 static int __net_init arp_net_init(
struct net *net)
1414 static void __net_exit arp_net_exit(
struct net *net)
1420 .
init = arp_net_init,
1421 .exit = arp_net_exit,
1424 static int __init arp_proc_init(
void)
1431 static int __init arp_proc_init(
void)