1 /* linux/net/ipv4/arp.c
2 *
3 * Copyright (C) 1994 by Florian La Roche
4 *
5 * This module implements the Address Resolution Protocol ARP (RFC 826),
6 * which is used to convert IP addresses (or in the future maybe other
7 * high-level addresses) into a low-level hardware address (like an Ethernet
8 * address).
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version
13 * 2 of the License, or (at your option) any later version.
14 *
15 * Fixes:
16 * Alan Cox : Removed the Ethernet assumptions in
17 * Florian's code
18 * Alan Cox : Fixed some small errors in the ARP
19 * logic
20 * Alan Cox : Allow >4K in /proc
21 * Alan Cox : Make ARP add its own protocol entry
22 * Ross Martin : Rewrote arp_rcv() and arp_get_info()
23 * Stephen Henson : Add AX25 support to arp_get_info()
24 * Alan Cox : Drop data when a device is downed.
25 * Alan Cox : Use init_timer().
26 * Alan Cox : Double lock fixes.
27 * Martin Seine : Move the arphdr structure
28 * to if_arp.h for compatibility.
29 * with BSD based programs.
30 * Andrew Tridgell : Added ARP netmask code and
31 * re-arranged proxy handling.
32 * Alan Cox : Changed to use notifiers.
33 * Niibe Yutaka : Reply for this device or proxies only.
34 * Alan Cox : Don't proxy across hardware types!
35 * Jonathan Naylor : Added support for NET/ROM.
36 * Mike Shaver : RFC1122 checks.
37 * Jonathan Naylor : Only lookup the hardware address for
38 * the correct hardware type.
39 * Germano Caronni : Assorted subtle races.
40 * Craig Schlenter : Don't modify permanent entry
41 * during arp_rcv.
42 * Russ Nelson : Tidied up a few bits.
43 * Alexey Kuznetsov: Major changes to caching and behaviour,
44 * eg intelligent arp probing and
45 * generation
46 * of host down events.
47 * Alan Cox : Missing unlock in device events.
48 * Eckes : ARP ioctl control errors.
49 * Alexey Kuznetsov: Arp free fix.
50 * Manuel Rodriguez: Gratuitous ARP.
51 * Jonathan Layes : Added arpd support through kerneld
52 * message queue (960314)
53 * Mike Shaver : /proc/sys/net/ipv4/arp_* support
54 * Mike McLagan : Routing by source
55 * Stuart Cheshire : Metricom and grat arp fixes
56 * *** FOR 2.1 clean this up ***
57 * Lawrence V. Stefani: (08/12/96) Added FDDI support.
58 * Alan Cox : Took the AP1000 nasty FDDI hack and
59 * folded into the mainstream FDDI code.
60 * Ack spit, Linus how did you allow that
61 * one in...
62 * Jes Sorensen : Make FDDI work again in 2.1.x and
63 * clean up the APFDDI & gen. FDDI bits.
64 * Alexey Kuznetsov: new arp state machine;
65 * now it is in net/core/neighbour.c.
66 * Krzysztof Halasa: Added Frame Relay ARP support.
67 * Arnaldo C. Melo : convert /proc/net/arp to seq_file
68 * Shmulik Hen: Split arp_send to arp_create and
69 * arp_xmit so intermediate drivers like
70 * bonding can change the skb before
71 * sending (e.g. insert 8021q tag).
72 * Harald Welte : convert to make use of jenkins hash
73 * Jesper D. Brouer: Proxy ARP PVLAN RFC 3069 support.
74 */
75
76 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
77
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>
84 #include <linux/sockios.h>
85 #include <linux/errno.h>
86 #include <linux/in.h>
87 #include <linux/mm.h>
88 #include <linux/inet.h>
89 #include <linux/inetdevice.h>
90 #include <linux/netdevice.h>
91 #include <linux/etherdevice.h>
92 #include <linux/fddidevice.h>
93 #include <linux/if_arp.h>
94 #include <linux/skbuff.h>
95 #include <linux/proc_fs.h>
96 #include <linux/seq_file.h>
97 #include <linux/stat.h>
98 #include <linux/init.h>
99 #include <linux/net.h>
100 #include <linux/rcupdate.h>
101 #include <linux/slab.h>
102 #ifdef CONFIG_SYSCTL
103 #include <linux/sysctl.h>
104 #endif
105
106 #include <net/net_namespace.h>
107 #include <net/ip.h>
108 #include <net/icmp.h>
109 #include <net/route.h>
110 #include <net/protocol.h>
111 #include <net/tcp.h>
112 #include <net/sock.h>
113 #include <net/arp.h>
114 #include <net/ax25.h>
115 #include <net/netrom.h>
116
117 #include <linux/uaccess.h>
118
119 #include <linux/netfilter_arp.h>
120
121 /*
122 * Interface to generic neighbour cache.
123 */
124 static u32 arp_hash(const void *pkey, const struct net_device *dev, __u32 *hash_rnd);
125 static int arp_constructor(struct neighbour *neigh);
126 static void arp_solicit(struct neighbour *neigh, struct sk_buff *skb);
127 static void arp_error_report(struct neighbour *neigh, struct sk_buff *skb);
128 static void parp_redo(struct sk_buff *skb);
129
130 static const struct neigh_ops arp_generic_ops = {
131 .family = AF_INET,
132 .solicit = arp_solicit,
133 .error_report = arp_error_report,
134 .output = neigh_resolve_output,
135 .connected_output = neigh_connected_output,
136 };
137
138 static const struct neigh_ops arp_hh_ops = {
139 .family = AF_INET,
140 .solicit = arp_solicit,
141 .error_report = arp_error_report,
142 .output = neigh_resolve_output,
143 .connected_output = neigh_resolve_output,
144 };
145
146 static const struct neigh_ops arp_direct_ops = {
147 .family = AF_INET,
148 .output = neigh_direct_output,
149 .connected_output = neigh_direct_output,
150 };
151
152 static const struct neigh_ops arp_broken_ops = {
153 .family = AF_INET,
154 .solicit = arp_solicit,
155 .error_report = arp_error_report,
156 .output = neigh_compat_output,
157 .connected_output = neigh_compat_output,
158 };
159
160 struct neigh_table arp_tbl = {
161 .family = AF_INET,
162 .key_len = 4,
163 .hash = arp_hash,
164 .constructor = arp_constructor,
165 .proxy_redo = parp_redo,
166 .id = "arp_cache",
167 .parms = {
168 .tbl = &arp_tbl,
169 .reachable_time = 30 * HZ,
170 .data = {
171 [NEIGH_VAR_MCAST_PROBES] = 3,
172 [NEIGH_VAR_UCAST_PROBES] = 3,
173 [NEIGH_VAR_RETRANS_TIME] = 1 * HZ,
174 [NEIGH_VAR_BASE_REACHABLE_TIME] = 30 * HZ,
175 [NEIGH_VAR_DELAY_PROBE_TIME] = 5 * HZ,
176 [NEIGH_VAR_GC_STALETIME] = 60 * HZ,
177 [NEIGH_VAR_QUEUE_LEN_BYTES] = 64 * 1024,
178 [NEIGH_VAR_PROXY_QLEN] = 64,
179 [NEIGH_VAR_ANYCAST_DELAY] = 1 * HZ,
180 [NEIGH_VAR_PROXY_DELAY] = (8 * HZ) / 10,
181 [NEIGH_VAR_LOCKTIME] = 1 * HZ,
182 },
183 },
184 .gc_interval = 30 * HZ,
185 .gc_thresh1 = 128,
186 .gc_thresh2 = 512,
187 .gc_thresh3 = 1024,
188 };
189 EXPORT_SYMBOL(arp_tbl);
190
arp_mc_map(__be32 addr,u8 * haddr,struct net_device * dev,int dir)191 int arp_mc_map(__be32 addr, u8 *haddr, struct net_device *dev, int dir)
192 {
193 switch (dev->type) {
194 case ARPHRD_ETHER:
195 case ARPHRD_FDDI:
196 case ARPHRD_IEEE802:
197 ip_eth_mc_map(addr, haddr);
198 return 0;
199 case ARPHRD_INFINIBAND:
200 ip_ib_mc_map(addr, dev->broadcast, haddr);
201 return 0;
202 case ARPHRD_IPGRE:
203 ip_ipgre_mc_map(addr, dev->broadcast, haddr);
204 return 0;
205 default:
206 if (dir) {
207 memcpy(haddr, dev->broadcast, dev->addr_len);
208 return 0;
209 }
210 }
211 return -EINVAL;
212 }
213
214
arp_hash(const void * pkey,const struct net_device * dev,__u32 * hash_rnd)215 static u32 arp_hash(const void *pkey,
216 const struct net_device *dev,
217 __u32 *hash_rnd)
218 {
219 return arp_hashfn(*(u32 *)pkey, dev, *hash_rnd);
220 }
221
arp_constructor(struct neighbour * neigh)222 static int arp_constructor(struct neighbour *neigh)
223 {
224 __be32 addr;
225 struct net_device *dev = neigh->dev;
226 struct in_device *in_dev;
227 struct neigh_parms *parms;
228 u32 inaddr_any = INADDR_ANY;
229
230 if (dev->flags & (IFF_LOOPBACK | IFF_POINTOPOINT))
231 memcpy(neigh->primary_key, &inaddr_any, arp_tbl.key_len);
232
233 addr = *(__be32 *)neigh->primary_key;
234 rcu_read_lock();
235 in_dev = __in_dev_get_rcu(dev);
236 if (in_dev == NULL) {
237 rcu_read_unlock();
238 return -EINVAL;
239 }
240
241 neigh->type = inet_addr_type(dev_net(dev), addr);
242
243 parms = in_dev->arp_parms;
244 __neigh_parms_put(neigh->parms);
245 neigh->parms = neigh_parms_clone(parms);
246 rcu_read_unlock();
247
248 if (!dev->header_ops) {
249 neigh->nud_state = NUD_NOARP;
250 neigh->ops = &arp_direct_ops;
251 neigh->output = neigh_direct_output;
252 } else {
253 /* Good devices (checked by reading texts, but only Ethernet is
254 tested)
255
256 ARPHRD_ETHER: (ethernet, apfddi)
257 ARPHRD_FDDI: (fddi)
258 ARPHRD_IEEE802: (tr)
259 ARPHRD_METRICOM: (strip)
260 ARPHRD_ARCNET:
261 etc. etc. etc.
262
263 ARPHRD_IPDDP will also work, if author repairs it.
264 I did not it, because this driver does not work even
265 in old paradigm.
266 */
267
268 #if 1
269 /* So... these "amateur" devices are hopeless.
270 The only thing, that I can say now:
271 It is very sad that we need to keep ugly obsolete
272 code to make them happy.
273
274 They should be moved to more reasonable state, now
275 they use rebuild_header INSTEAD OF hard_start_xmit!!!
276 Besides that, they are sort of out of date
277 (a lot of redundant clones/copies, useless in 2.1),
278 I wonder why people believe that they work.
279 */
280 switch (dev->type) {
281 default:
282 break;
283 case ARPHRD_ROSE:
284 #if IS_ENABLED(CONFIG_AX25)
285 case ARPHRD_AX25:
286 #if IS_ENABLED(CONFIG_NETROM)
287 case ARPHRD_NETROM:
288 #endif
289 neigh->ops = &arp_broken_ops;
290 neigh->output = neigh->ops->output;
291 return 0;
292 #else
293 break;
294 #endif
295 }
296 #endif
297 if (neigh->type == RTN_MULTICAST) {
298 neigh->nud_state = NUD_NOARP;
299 arp_mc_map(addr, neigh->ha, dev, 1);
300 } else if (dev->flags & (IFF_NOARP | IFF_LOOPBACK)) {
301 neigh->nud_state = NUD_NOARP;
302 memcpy(neigh->ha, dev->dev_addr, dev->addr_len);
303 } else if (neigh->type == RTN_BROADCAST ||
304 (dev->flags & IFF_POINTOPOINT)) {
305 neigh->nud_state = NUD_NOARP;
306 memcpy(neigh->ha, dev->broadcast, dev->addr_len);
307 }
308
309 if (dev->header_ops->cache)
310 neigh->ops = &arp_hh_ops;
311 else
312 neigh->ops = &arp_generic_ops;
313
314 if (neigh->nud_state & NUD_VALID)
315 neigh->output = neigh->ops->connected_output;
316 else
317 neigh->output = neigh->ops->output;
318 }
319 return 0;
320 }
321
arp_error_report(struct neighbour * neigh,struct sk_buff * skb)322 static void arp_error_report(struct neighbour *neigh, struct sk_buff *skb)
323 {
324 dst_link_failure(skb);
325 kfree_skb(skb);
326 }
327
arp_solicit(struct neighbour * neigh,struct sk_buff * skb)328 static void arp_solicit(struct neighbour *neigh, struct sk_buff *skb)
329 {
330 __be32 saddr = 0;
331 u8 dst_ha[MAX_ADDR_LEN], *dst_hw = NULL;
332 struct net_device *dev = neigh->dev;
333 __be32 target = *(__be32 *)neigh->primary_key;
334 int probes = atomic_read(&neigh->probes);
335 struct in_device *in_dev;
336
337 rcu_read_lock();
338 in_dev = __in_dev_get_rcu(dev);
339 if (!in_dev) {
340 rcu_read_unlock();
341 return;
342 }
343 switch (IN_DEV_ARP_ANNOUNCE(in_dev)) {
344 default:
345 case 0: /* By default announce any local IP */
346 if (skb && inet_addr_type(dev_net(dev),
347 ip_hdr(skb)->saddr) == RTN_LOCAL)
348 saddr = ip_hdr(skb)->saddr;
349 break;
350 case 1: /* Restrict announcements of saddr in same subnet */
351 if (!skb)
352 break;
353 saddr = ip_hdr(skb)->saddr;
354 if (inet_addr_type(dev_net(dev), saddr) == RTN_LOCAL) {
355 /* saddr should be known to target */
356 if (inet_addr_onlink(in_dev, target, saddr))
357 break;
358 }
359 saddr = 0;
360 break;
361 case 2: /* Avoid secondary IPs, get a primary/preferred one */
362 break;
363 }
364 rcu_read_unlock();
365
366 if (!saddr)
367 saddr = inet_select_addr(dev, target, RT_SCOPE_LINK);
368
369 probes -= NEIGH_VAR(neigh->parms, UCAST_PROBES);
370 if (probes < 0) {
371 if (!(neigh->nud_state & NUD_VALID))
372 pr_debug("trying to ucast probe in NUD_INVALID\n");
373 neigh_ha_snapshot(dst_ha, neigh, dev);
374 dst_hw = dst_ha;
375 } else {
376 probes -= NEIGH_VAR(neigh->parms, APP_PROBES);
377 if (probes < 0) {
378 neigh_app_ns(neigh);
379 return;
380 }
381 }
382
383 arp_send(ARPOP_REQUEST, ETH_P_ARP, target, dev, saddr,
384 dst_hw, dev->dev_addr, NULL);
385 }
386
arp_ignore(struct in_device * in_dev,__be32 sip,__be32 tip)387 static int arp_ignore(struct in_device *in_dev, __be32 sip, __be32 tip)
388 {
389 struct net *net = dev_net(in_dev->dev);
390 int scope;
391
392 switch (IN_DEV_ARP_IGNORE(in_dev)) {
393 case 0: /* Reply, the tip is already validated */
394 return 0;
395 case 1: /* Reply only if tip is configured on the incoming interface */
396 sip = 0;
397 scope = RT_SCOPE_HOST;
398 break;
399 case 2: /*
400 * Reply only if tip is configured on the incoming interface
401 * and is in same subnet as sip
402 */
403 scope = RT_SCOPE_HOST;
404 break;
405 case 3: /* Do not reply for scope host addresses */
406 sip = 0;
407 scope = RT_SCOPE_LINK;
408 in_dev = NULL;
409 break;
410 case 4: /* Reserved */
411 case 5:
412 case 6:
413 case 7:
414 return 0;
415 case 8: /* Do not reply */
416 return 1;
417 default:
418 return 0;
419 }
420 return !inet_confirm_addr(net, in_dev, sip, tip, scope);
421 }
422
arp_filter(__be32 sip,__be32 tip,struct net_device * dev)423 static int arp_filter(__be32 sip, __be32 tip, struct net_device *dev)
424 {
425 struct rtable *rt;
426 int flag = 0;
427 /*unsigned long now; */
428 struct net *net = dev_net(dev);
429
430 rt = ip_route_output(net, sip, tip, 0, 0);
431 if (IS_ERR(rt))
432 return 1;
433 if (rt->dst.dev != dev) {
434 NET_INC_STATS_BH(net, LINUX_MIB_ARPFILTER);
435 flag = 1;
436 }
437 ip_rt_put(rt);
438 return flag;
439 }
440
441 /* OBSOLETE FUNCTIONS */
442
443 /*
444 * Find an arp mapping in the cache. If not found, post a request.
445 *
446 * It is very UGLY routine: it DOES NOT use skb->dst->neighbour,
447 * even if it exists. It is supposed that skb->dev was mangled
448 * by a virtual device (eql, shaper). Nobody but broken devices
449 * is allowed to use this function, it is scheduled to be removed. --ANK
450 */
451
arp_set_predefined(int addr_hint,unsigned char * haddr,__be32 paddr,struct net_device * dev)452 static int arp_set_predefined(int addr_hint, unsigned char *haddr,
453 __be32 paddr, struct net_device *dev)
454 {
455 switch (addr_hint) {
456 case RTN_LOCAL:
457 pr_debug("arp called for own IP address\n");
458 memcpy(haddr, dev->dev_addr, dev->addr_len);
459 return 1;
460 case RTN_MULTICAST:
461 arp_mc_map(paddr, haddr, dev, 1);
462 return 1;
463 case RTN_BROADCAST:
464 memcpy(haddr, dev->broadcast, dev->addr_len);
465 return 1;
466 }
467 return 0;
468 }
469
470
arp_find(unsigned char * haddr,struct sk_buff * skb)471 int arp_find(unsigned char *haddr, struct sk_buff *skb)
472 {
473 struct net_device *dev = skb->dev;
474 __be32 paddr;
475 struct neighbour *n;
476
477 if (!skb_dst(skb)) {
478 pr_debug("arp_find is called with dst==NULL\n");
479 kfree_skb(skb);
480 return 1;
481 }
482
483 paddr = rt_nexthop(skb_rtable(skb), ip_hdr(skb)->daddr);
484 if (arp_set_predefined(inet_addr_type(dev_net(dev), paddr), haddr,
485 paddr, dev))
486 return 0;
487
488 n = __neigh_lookup(&arp_tbl, &paddr, dev, 1);
489
490 if (n) {
491 n->used = jiffies;
492 if (n->nud_state & NUD_VALID || neigh_event_send(n, skb) == 0) {
493 neigh_ha_snapshot(haddr, n, dev);
494 neigh_release(n);
495 return 0;
496 }
497 neigh_release(n);
498 } else
499 kfree_skb(skb);
500 return 1;
501 }
502 EXPORT_SYMBOL(arp_find);
503
504 /* END OF OBSOLETE FUNCTIONS */
505
506 /*
507 * Check if we can use proxy ARP for this path
508 */
arp_fwd_proxy(struct in_device * in_dev,struct net_device * dev,struct rtable * rt)509 static inline int arp_fwd_proxy(struct in_device *in_dev,
510 struct net_device *dev, struct rtable *rt)
511 {
512 struct in_device *out_dev;
513 int imi, omi = -1;
514
515 if (rt->dst.dev == dev)
516 return 0;
517
518 if (!IN_DEV_PROXY_ARP(in_dev))
519 return 0;
520 imi = IN_DEV_MEDIUM_ID(in_dev);
521 if (imi == 0)
522 return 1;
523 if (imi == -1)
524 return 0;
525
526 /* place to check for proxy_arp for routes */
527
528 out_dev = __in_dev_get_rcu(rt->dst.dev);
529 if (out_dev)
530 omi = IN_DEV_MEDIUM_ID(out_dev);
531
532 return omi != imi && omi != -1;
533 }
534
535 /*
536 * Check for RFC3069 proxy arp private VLAN (allow to send back to same dev)
537 *
538 * RFC3069 supports proxy arp replies back to the same interface. This
539 * is done to support (ethernet) switch features, like RFC 3069, where
540 * the individual ports are not allowed to communicate with each
541 * other, BUT they are allowed to talk to the upstream router. As
542 * described in RFC 3069, it is possible to allow these hosts to
543 * communicate through the upstream router, by proxy_arp'ing.
544 *
545 * RFC 3069: "VLAN Aggregation for Efficient IP Address Allocation"
546 *
547 * This technology is known by different names:
548 * In RFC 3069 it is called VLAN Aggregation.
549 * Cisco and Allied Telesyn call it Private VLAN.
550 * Hewlett-Packard call it Source-Port filtering or port-isolation.
551 * Ericsson call it MAC-Forced Forwarding (RFC Draft).
552 *
553 */
arp_fwd_pvlan(struct in_device * in_dev,struct net_device * dev,struct rtable * rt,__be32 sip,__be32 tip)554 static inline int arp_fwd_pvlan(struct in_device *in_dev,
555 struct net_device *dev, struct rtable *rt,
556 __be32 sip, __be32 tip)
557 {
558 /* Private VLAN is only concerned about the same ethernet segment */
559 if (rt->dst.dev != dev)
560 return 0;
561
562 /* Don't reply on self probes (often done by windowz boxes)*/
563 if (sip == tip)
564 return 0;
565
566 if (IN_DEV_PROXY_ARP_PVLAN(in_dev))
567 return 1;
568 else
569 return 0;
570 }
571
572 /*
573 * Interface to link layer: send routine and receive handler.
574 */
575
576 /*
577 * Create an arp packet. If (dest_hw == NULL), we create a broadcast
578 * message.
579 */
arp_create(int type,int ptype,__be32 dest_ip,struct net_device * dev,__be32 src_ip,const unsigned char * dest_hw,const unsigned char * src_hw,const unsigned char * target_hw)580 struct sk_buff *arp_create(int type, int ptype, __be32 dest_ip,
581 struct net_device *dev, __be32 src_ip,
582 const unsigned char *dest_hw,
583 const unsigned char *src_hw,
584 const unsigned char *target_hw)
585 {
586 struct sk_buff *skb;
587 struct arphdr *arp;
588 unsigned char *arp_ptr;
589 int hlen = LL_RESERVED_SPACE(dev);
590 int tlen = dev->needed_tailroom;
591
592 /*
593 * Allocate a buffer
594 */
595
596 skb = alloc_skb(arp_hdr_len(dev) + hlen + tlen, GFP_ATOMIC);
597 if (skb == NULL)
598 return NULL;
599
600 skb_reserve(skb, hlen);
601 skb_reset_network_header(skb);
602 arp = (struct arphdr *) skb_put(skb, arp_hdr_len(dev));
603 skb->dev = dev;
604 skb->protocol = htons(ETH_P_ARP);
605 if (src_hw == NULL)
606 src_hw = dev->dev_addr;
607 if (dest_hw == NULL)
608 dest_hw = dev->broadcast;
609
610 /*
611 * Fill the device header for the ARP frame
612 */
613 if (dev_hard_header(skb, dev, ptype, dest_hw, src_hw, skb->len) < 0)
614 goto out;
615
616 /*
617 * Fill out the arp protocol part.
618 *
619 * The arp hardware type should match the device type, except for FDDI,
620 * which (according to RFC 1390) should always equal 1 (Ethernet).
621 */
622 /*
623 * Exceptions everywhere. AX.25 uses the AX.25 PID value not the
624 * DIX code for the protocol. Make these device structure fields.
625 */
626 switch (dev->type) {
627 default:
628 arp->ar_hrd = htons(dev->type);
629 arp->ar_pro = htons(ETH_P_IP);
630 break;
631
632 #if IS_ENABLED(CONFIG_AX25)
633 case ARPHRD_AX25:
634 arp->ar_hrd = htons(ARPHRD_AX25);
635 arp->ar_pro = htons(AX25_P_IP);
636 break;
637
638 #if IS_ENABLED(CONFIG_NETROM)
639 case ARPHRD_NETROM:
640 arp->ar_hrd = htons(ARPHRD_NETROM);
641 arp->ar_pro = htons(AX25_P_IP);
642 break;
643 #endif
644 #endif
645
646 #if IS_ENABLED(CONFIG_FDDI)
647 case ARPHRD_FDDI:
648 arp->ar_hrd = htons(ARPHRD_ETHER);
649 arp->ar_pro = htons(ETH_P_IP);
650 break;
651 #endif
652 }
653
654 arp->ar_hln = dev->addr_len;
655 arp->ar_pln = 4;
656 arp->ar_op = htons(type);
657
658 arp_ptr = (unsigned char *)(arp + 1);
659
660 memcpy(arp_ptr, src_hw, dev->addr_len);
661 arp_ptr += dev->addr_len;
662 memcpy(arp_ptr, &src_ip, 4);
663 arp_ptr += 4;
664
665 switch (dev->type) {
666 #if IS_ENABLED(CONFIG_FIREWIRE_NET)
667 case ARPHRD_IEEE1394:
668 break;
669 #endif
670 default:
671 if (target_hw != NULL)
672 memcpy(arp_ptr, target_hw, dev->addr_len);
673 else
674 memset(arp_ptr, 0, dev->addr_len);
675 arp_ptr += dev->addr_len;
676 }
677 memcpy(arp_ptr, &dest_ip, 4);
678
679 return skb;
680
681 out:
682 kfree_skb(skb);
683 return NULL;
684 }
685 EXPORT_SYMBOL(arp_create);
686
687 /*
688 * Send an arp packet.
689 */
arp_xmit(struct sk_buff * skb)690 void arp_xmit(struct sk_buff *skb)
691 {
692 /* Send it off, maybe filter it using firewalling first. */
693 NF_HOOK(NFPROTO_ARP, NF_ARP_OUT, skb, NULL, skb->dev, dev_queue_xmit);
694 }
695 EXPORT_SYMBOL(arp_xmit);
696
697 /*
698 * Create and send an arp packet.
699 */
arp_send(int type,int ptype,__be32 dest_ip,struct net_device * dev,__be32 src_ip,const unsigned char * dest_hw,const unsigned char * src_hw,const unsigned char * target_hw)700 void arp_send(int type, int ptype, __be32 dest_ip,
701 struct net_device *dev, __be32 src_ip,
702 const unsigned char *dest_hw, const unsigned char *src_hw,
703 const unsigned char *target_hw)
704 {
705 struct sk_buff *skb;
706
707 /*
708 * No arp on this interface.
709 */
710
711 if (dev->flags&IFF_NOARP)
712 return;
713
714 skb = arp_create(type, ptype, dest_ip, dev, src_ip,
715 dest_hw, src_hw, target_hw);
716 if (skb == NULL)
717 return;
718
719 arp_xmit(skb);
720 }
721 EXPORT_SYMBOL(arp_send);
722
723 /*
724 * Process an arp request.
725 */
726
arp_process(struct sk_buff * skb)727 static int arp_process(struct sk_buff *skb)
728 {
729 struct net_device *dev = skb->dev;
730 struct in_device *in_dev = __in_dev_get_rcu(dev);
731 struct arphdr *arp;
732 unsigned char *arp_ptr;
733 struct rtable *rt;
734 unsigned char *sha;
735 __be32 sip, tip;
736 u16 dev_type = dev->type;
737 int addr_type;
738 struct neighbour *n;
739 struct net *net = dev_net(dev);
740 bool is_garp = false;
741
742 /* arp_rcv below verifies the ARP header and verifies the device
743 * is ARP'able.
744 */
745
746 if (in_dev == NULL)
747 goto out;
748
749 arp = arp_hdr(skb);
750
751 switch (dev_type) {
752 default:
753 if (arp->ar_pro != htons(ETH_P_IP) ||
754 htons(dev_type) != arp->ar_hrd)
755 goto out;
756 break;
757 case ARPHRD_ETHER:
758 case ARPHRD_FDDI:
759 case ARPHRD_IEEE802:
760 /*
761 * ETHERNET, and Fibre Channel (which are IEEE 802
762 * devices, according to RFC 2625) devices will accept ARP
763 * hardware types of either 1 (Ethernet) or 6 (IEEE 802.2).
764 * This is the case also of FDDI, where the RFC 1390 says that
765 * FDDI devices should accept ARP hardware of (1) Ethernet,
766 * however, to be more robust, we'll accept both 1 (Ethernet)
767 * or 6 (IEEE 802.2)
768 */
769 if ((arp->ar_hrd != htons(ARPHRD_ETHER) &&
770 arp->ar_hrd != htons(ARPHRD_IEEE802)) ||
771 arp->ar_pro != htons(ETH_P_IP))
772 goto out;
773 break;
774 case ARPHRD_AX25:
775 if (arp->ar_pro != htons(AX25_P_IP) ||
776 arp->ar_hrd != htons(ARPHRD_AX25))
777 goto out;
778 break;
779 case ARPHRD_NETROM:
780 if (arp->ar_pro != htons(AX25_P_IP) ||
781 arp->ar_hrd != htons(ARPHRD_NETROM))
782 goto out;
783 break;
784 }
785
786 /* Understand only these message types */
787
788 if (arp->ar_op != htons(ARPOP_REPLY) &&
789 arp->ar_op != htons(ARPOP_REQUEST))
790 goto out;
791
792 /*
793 * Extract fields
794 */
795 arp_ptr = (unsigned char *)(arp + 1);
796 sha = arp_ptr;
797 arp_ptr += dev->addr_len;
798 memcpy(&sip, arp_ptr, 4);
799 arp_ptr += 4;
800 switch (dev_type) {
801 #if IS_ENABLED(CONFIG_FIREWIRE_NET)
802 case ARPHRD_IEEE1394:
803 break;
804 #endif
805 default:
806 arp_ptr += dev->addr_len;
807 }
808 memcpy(&tip, arp_ptr, 4);
809 /*
810 * Check for bad requests for 127.x.x.x and requests for multicast
811 * addresses. If this is one such, delete it.
812 */
813 if (ipv4_is_multicast(tip) ||
814 (!IN_DEV_ROUTE_LOCALNET(in_dev) && ipv4_is_loopback(tip)))
815 goto out;
816
817 /*
818 * Special case: We must set Frame Relay source Q.922 address
819 */
820 if (dev_type == ARPHRD_DLCI)
821 sha = dev->broadcast;
822
823 /*
824 * Process entry. The idea here is we want to send a reply if it is a
825 * request for us or if it is a request for someone else that we hold
826 * a proxy for. We want to add an entry to our cache if it is a reply
827 * to us or if it is a request for our address.
828 * (The assumption for this last is that if someone is requesting our
829 * address, they are probably intending to talk to us, so it saves time
830 * if we cache their address. Their address is also probably not in
831 * our cache, since ours is not in their cache.)
832 *
833 * Putting this another way, we only care about replies if they are to
834 * us, in which case we add them to the cache. For requests, we care
835 * about those for us and those for our proxies. We reply to both,
836 * and in the case of requests for us we add the requester to the arp
837 * cache.
838 */
839
840 /* Special case: IPv4 duplicate address detection packet (RFC2131) */
841 if (sip == 0) {
842 if (arp->ar_op == htons(ARPOP_REQUEST) &&
843 inet_addr_type(net, tip) == RTN_LOCAL &&
844 !arp_ignore(in_dev, sip, tip))
845 arp_send(ARPOP_REPLY, ETH_P_ARP, sip, dev, tip, sha,
846 dev->dev_addr, sha);
847 goto out;
848 }
849
850 if (arp->ar_op == htons(ARPOP_REQUEST) &&
851 ip_route_input_noref(skb, tip, sip, 0, dev) == 0) {
852
853 rt = skb_rtable(skb);
854 addr_type = rt->rt_type;
855
856 if (addr_type == RTN_LOCAL) {
857 int dont_send;
858
859 dont_send = arp_ignore(in_dev, sip, tip);
860 if (!dont_send && IN_DEV_ARPFILTER(in_dev))
861 dont_send = arp_filter(sip, tip, dev);
862 if (!dont_send) {
863 n = neigh_event_ns(&arp_tbl, sha, &sip, dev);
864 if (n) {
865 arp_send(ARPOP_REPLY, ETH_P_ARP, sip,
866 dev, tip, sha, dev->dev_addr,
867 sha);
868 neigh_release(n);
869 }
870 }
871 goto out;
872 } else if (IN_DEV_FORWARD(in_dev)) {
873 if (addr_type == RTN_UNICAST &&
874 (arp_fwd_proxy(in_dev, dev, rt) ||
875 arp_fwd_pvlan(in_dev, dev, rt, sip, tip) ||
876 (rt->dst.dev != dev &&
877 pneigh_lookup(&arp_tbl, net, &tip, dev, 0)))) {
878 n = neigh_event_ns(&arp_tbl, sha, &sip, dev);
879 if (n)
880 neigh_release(n);
881
882 if (NEIGH_CB(skb)->flags & LOCALLY_ENQUEUED ||
883 skb->pkt_type == PACKET_HOST ||
884 NEIGH_VAR(in_dev->arp_parms, PROXY_DELAY) == 0) {
885 arp_send(ARPOP_REPLY, ETH_P_ARP, sip,
886 dev, tip, sha, dev->dev_addr,
887 sha);
888 } else {
889 pneigh_enqueue(&arp_tbl,
890 in_dev->arp_parms, skb);
891 return 0;
892 }
893 goto out;
894 }
895 }
896 }
897
898 /* Update our ARP tables */
899
900 n = __neigh_lookup(&arp_tbl, &sip, dev, 0);
901
902 if (IN_DEV_ARP_ACCEPT(in_dev)) {
903 /* Unsolicited ARP is not accepted by default.
904 It is possible, that this option should be enabled for some
905 devices (strip is candidate)
906 */
907 is_garp = arp->ar_op == htons(ARPOP_REQUEST) && tip == sip &&
908 inet_addr_type(net, sip) == RTN_UNICAST;
909
910 if (n == NULL &&
911 ((arp->ar_op == htons(ARPOP_REPLY) &&
912 inet_addr_type(net, sip) == RTN_UNICAST) || is_garp))
913 n = __neigh_lookup(&arp_tbl, &sip, dev, 1);
914 }
915
916 if (n) {
917 int state = NUD_REACHABLE;
918 int override;
919
920 /* If several different ARP replies follows back-to-back,
921 use the FIRST one. It is possible, if several proxy
922 agents are active. Taking the first reply prevents
923 arp trashing and chooses the fastest router.
924 */
925 override = time_after(jiffies,
926 n->updated +
927 NEIGH_VAR(n->parms, LOCKTIME)) ||
928 is_garp;
929
930 /* Broadcast replies and request packets
931 do not assert neighbour reachability.
932 */
933 if (arp->ar_op != htons(ARPOP_REPLY) ||
934 skb->pkt_type != PACKET_HOST)
935 state = NUD_STALE;
936 neigh_update(n, sha, state,
937 override ? NEIGH_UPDATE_F_OVERRIDE : 0);
938 neigh_release(n);
939 }
940
941 out:
942 consume_skb(skb);
943 return 0;
944 }
945
parp_redo(struct sk_buff * skb)946 static void parp_redo(struct sk_buff *skb)
947 {
948 arp_process(skb);
949 }
950
951
952 /*
953 * Receive an arp request from the device layer.
954 */
955
arp_rcv(struct sk_buff * skb,struct net_device * dev,struct packet_type * pt,struct net_device * orig_dev)956 static int arp_rcv(struct sk_buff *skb, struct net_device *dev,
957 struct packet_type *pt, struct net_device *orig_dev)
958 {
959 const struct arphdr *arp;
960
961 /* do not tweak dropwatch on an ARP we will ignore */
962 if (dev->flags & IFF_NOARP ||
963 skb->pkt_type == PACKET_OTHERHOST ||
964 skb->pkt_type == PACKET_LOOPBACK)
965 goto consumeskb;
966
967 skb = skb_share_check(skb, GFP_ATOMIC);
968 if (!skb)
969 goto out_of_mem;
970
971 /* ARP header, plus 2 device addresses, plus 2 IP addresses. */
972 if (!pskb_may_pull(skb, arp_hdr_len(dev)))
973 goto freeskb;
974
975 arp = arp_hdr(skb);
976 if (arp->ar_hln != dev->addr_len || arp->ar_pln != 4)
977 goto freeskb;
978
979 memset(NEIGH_CB(skb), 0, sizeof(struct neighbour_cb));
980
981 return NF_HOOK(NFPROTO_ARP, NF_ARP_IN, skb, dev, NULL, arp_process);
982
983 consumeskb:
984 consume_skb(skb);
985 return 0;
986 freeskb:
987 kfree_skb(skb);
988 out_of_mem:
989 return 0;
990 }
991
992 /*
993 * User level interface (ioctl)
994 */
995
996 /*
997 * Set (create) an ARP cache entry.
998 */
999
arp_req_set_proxy(struct net * net,struct net_device * dev,int on)1000 static int arp_req_set_proxy(struct net *net, struct net_device *dev, int on)
1001 {
1002 if (dev == NULL) {
1003 IPV4_DEVCONF_ALL(net, PROXY_ARP) = on;
1004 return 0;
1005 }
1006 if (__in_dev_get_rtnl(dev)) {
1007 IN_DEV_CONF_SET(__in_dev_get_rtnl(dev), PROXY_ARP, on);
1008 return 0;
1009 }
1010 return -ENXIO;
1011 }
1012
arp_req_set_public(struct net * net,struct arpreq * r,struct net_device * dev)1013 static int arp_req_set_public(struct net *net, struct arpreq *r,
1014 struct net_device *dev)
1015 {
1016 __be32 ip = ((struct sockaddr_in *)&r->arp_pa)->sin_addr.s_addr;
1017 __be32 mask = ((struct sockaddr_in *)&r->arp_netmask)->sin_addr.s_addr;
1018
1019 if (mask && mask != htonl(0xFFFFFFFF))
1020 return -EINVAL;
1021 if (!dev && (r->arp_flags & ATF_COM)) {
1022 dev = dev_getbyhwaddr_rcu(net, r->arp_ha.sa_family,
1023 r->arp_ha.sa_data);
1024 if (!dev)
1025 return -ENODEV;
1026 }
1027 if (mask) {
1028 if (pneigh_lookup(&arp_tbl, net, &ip, dev, 1) == NULL)
1029 return -ENOBUFS;
1030 return 0;
1031 }
1032
1033 return arp_req_set_proxy(net, dev, 1);
1034 }
1035
arp_req_set(struct net * net,struct arpreq * r,struct net_device * dev)1036 static int arp_req_set(struct net *net, struct arpreq *r,
1037 struct net_device *dev)
1038 {
1039 __be32 ip;
1040 struct neighbour *neigh;
1041 int err;
1042
1043 if (r->arp_flags & ATF_PUBL)
1044 return arp_req_set_public(net, r, dev);
1045
1046 ip = ((struct sockaddr_in *)&r->arp_pa)->sin_addr.s_addr;
1047 if (r->arp_flags & ATF_PERM)
1048 r->arp_flags |= ATF_COM;
1049 if (dev == NULL) {
1050 struct rtable *rt = ip_route_output(net, ip, 0, RTO_ONLINK, 0);
1051
1052 if (IS_ERR(rt))
1053 return PTR_ERR(rt);
1054 dev = rt->dst.dev;
1055 ip_rt_put(rt);
1056 if (!dev)
1057 return -EINVAL;
1058 }
1059 switch (dev->type) {
1060 #if IS_ENABLED(CONFIG_FDDI)
1061 case ARPHRD_FDDI:
1062 /*
1063 * According to RFC 1390, FDDI devices should accept ARP
1064 * hardware types of 1 (Ethernet). However, to be more
1065 * robust, we'll accept hardware types of either 1 (Ethernet)
1066 * or 6 (IEEE 802.2).
1067 */
1068 if (r->arp_ha.sa_family != ARPHRD_FDDI &&
1069 r->arp_ha.sa_family != ARPHRD_ETHER &&
1070 r->arp_ha.sa_family != ARPHRD_IEEE802)
1071 return -EINVAL;
1072 break;
1073 #endif
1074 default:
1075 if (r->arp_ha.sa_family != dev->type)
1076 return -EINVAL;
1077 break;
1078 }
1079
1080 neigh = __neigh_lookup_errno(&arp_tbl, &ip, dev);
1081 err = PTR_ERR(neigh);
1082 if (!IS_ERR(neigh)) {
1083 unsigned int state = NUD_STALE;
1084 if (r->arp_flags & ATF_PERM)
1085 state = NUD_PERMANENT;
1086 err = neigh_update(neigh, (r->arp_flags & ATF_COM) ?
1087 r->arp_ha.sa_data : NULL, state,
1088 NEIGH_UPDATE_F_OVERRIDE |
1089 NEIGH_UPDATE_F_ADMIN);
1090 neigh_release(neigh);
1091 }
1092 return err;
1093 }
1094
arp_state_to_flags(struct neighbour * neigh)1095 static unsigned int arp_state_to_flags(struct neighbour *neigh)
1096 {
1097 if (neigh->nud_state&NUD_PERMANENT)
1098 return ATF_PERM | ATF_COM;
1099 else if (neigh->nud_state&NUD_VALID)
1100 return ATF_COM;
1101 else
1102 return 0;
1103 }
1104
1105 /*
1106 * Get an ARP cache entry.
1107 */
1108
arp_req_get(struct arpreq * r,struct net_device * dev)1109 static int arp_req_get(struct arpreq *r, struct net_device *dev)
1110 {
1111 __be32 ip = ((struct sockaddr_in *) &r->arp_pa)->sin_addr.s_addr;
1112 struct neighbour *neigh;
1113 int err = -ENXIO;
1114
1115 neigh = neigh_lookup(&arp_tbl, &ip, dev);
1116 if (neigh) {
1117 read_lock_bh(&neigh->lock);
1118 memcpy(r->arp_ha.sa_data, neigh->ha, dev->addr_len);
1119 r->arp_flags = arp_state_to_flags(neigh);
1120 read_unlock_bh(&neigh->lock);
1121 r->arp_ha.sa_family = dev->type;
1122 strlcpy(r->arp_dev, dev->name, sizeof(r->arp_dev));
1123 neigh_release(neigh);
1124 err = 0;
1125 }
1126 return err;
1127 }
1128
arp_invalidate(struct net_device * dev,__be32 ip)1129 static int arp_invalidate(struct net_device *dev, __be32 ip)
1130 {
1131 struct neighbour *neigh = neigh_lookup(&arp_tbl, &ip, dev);
1132 int err = -ENXIO;
1133
1134 if (neigh) {
1135 if (neigh->nud_state & ~NUD_NOARP)
1136 err = neigh_update(neigh, NULL, NUD_FAILED,
1137 NEIGH_UPDATE_F_OVERRIDE|
1138 NEIGH_UPDATE_F_ADMIN);
1139 neigh_release(neigh);
1140 }
1141
1142 return err;
1143 }
1144
arp_req_delete_public(struct net * net,struct arpreq * r,struct net_device * dev)1145 static int arp_req_delete_public(struct net *net, struct arpreq *r,
1146 struct net_device *dev)
1147 {
1148 __be32 ip = ((struct sockaddr_in *) &r->arp_pa)->sin_addr.s_addr;
1149 __be32 mask = ((struct sockaddr_in *)&r->arp_netmask)->sin_addr.s_addr;
1150
1151 if (mask == htonl(0xFFFFFFFF))
1152 return pneigh_delete(&arp_tbl, net, &ip, dev);
1153
1154 if (mask)
1155 return -EINVAL;
1156
1157 return arp_req_set_proxy(net, dev, 0);
1158 }
1159
arp_req_delete(struct net * net,struct arpreq * r,struct net_device * dev)1160 static int arp_req_delete(struct net *net, struct arpreq *r,
1161 struct net_device *dev)
1162 {
1163 __be32 ip;
1164
1165 if (r->arp_flags & ATF_PUBL)
1166 return arp_req_delete_public(net, r, dev);
1167
1168 ip = ((struct sockaddr_in *)&r->arp_pa)->sin_addr.s_addr;
1169 if (dev == NULL) {
1170 struct rtable *rt = ip_route_output(net, ip, 0, RTO_ONLINK, 0);
1171 if (IS_ERR(rt))
1172 return PTR_ERR(rt);
1173 dev = rt->dst.dev;
1174 ip_rt_put(rt);
1175 if (!dev)
1176 return -EINVAL;
1177 }
1178 return arp_invalidate(dev, ip);
1179 }
1180
1181 /*
1182 * Handle an ARP layer I/O control request.
1183 */
1184
arp_ioctl(struct net * net,unsigned int cmd,void __user * arg)1185 int arp_ioctl(struct net *net, unsigned int cmd, void __user *arg)
1186 {
1187 int err;
1188 struct arpreq r;
1189 struct net_device *dev = NULL;
1190
1191 switch (cmd) {
1192 case SIOCDARP:
1193 case SIOCSARP:
1194 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
1195 return -EPERM;
1196 case SIOCGARP:
1197 err = copy_from_user(&r, arg, sizeof(struct arpreq));
1198 if (err)
1199 return -EFAULT;
1200 break;
1201 default:
1202 return -EINVAL;
1203 }
1204
1205 if (r.arp_pa.sa_family != AF_INET)
1206 return -EPFNOSUPPORT;
1207
1208 if (!(r.arp_flags & ATF_PUBL) &&
1209 (r.arp_flags & (ATF_NETMASK | ATF_DONTPUB)))
1210 return -EINVAL;
1211 if (!(r.arp_flags & ATF_NETMASK))
1212 ((struct sockaddr_in *)&r.arp_netmask)->sin_addr.s_addr =
1213 htonl(0xFFFFFFFFUL);
1214 rtnl_lock();
1215 if (r.arp_dev[0]) {
1216 err = -ENODEV;
1217 dev = __dev_get_by_name(net, r.arp_dev);
1218 if (dev == NULL)
1219 goto out;
1220
1221 /* Mmmm... It is wrong... ARPHRD_NETROM==0 */
1222 if (!r.arp_ha.sa_family)
1223 r.arp_ha.sa_family = dev->type;
1224 err = -EINVAL;
1225 if ((r.arp_flags & ATF_COM) && r.arp_ha.sa_family != dev->type)
1226 goto out;
1227 } else if (cmd == SIOCGARP) {
1228 err = -ENODEV;
1229 goto out;
1230 }
1231
1232 switch (cmd) {
1233 case SIOCDARP:
1234 err = arp_req_delete(net, &r, dev);
1235 break;
1236 case SIOCSARP:
1237 err = arp_req_set(net, &r, dev);
1238 break;
1239 case SIOCGARP:
1240 err = arp_req_get(&r, dev);
1241 break;
1242 }
1243 out:
1244 rtnl_unlock();
1245 if (cmd == SIOCGARP && !err && copy_to_user(arg, &r, sizeof(r)))
1246 err = -EFAULT;
1247 return err;
1248 }
1249
arp_netdev_event(struct notifier_block * this,unsigned long event,void * ptr)1250 static int arp_netdev_event(struct notifier_block *this, unsigned long event,
1251 void *ptr)
1252 {
1253 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1254 struct netdev_notifier_change_info *change_info;
1255
1256 switch (event) {
1257 case NETDEV_CHANGEADDR:
1258 neigh_changeaddr(&arp_tbl, dev);
1259 rt_cache_flush(dev_net(dev));
1260 break;
1261 case NETDEV_CHANGE:
1262 change_info = ptr;
1263 if (change_info->flags_changed & IFF_NOARP)
1264 neigh_changeaddr(&arp_tbl, dev);
1265 break;
1266 default:
1267 break;
1268 }
1269
1270 return NOTIFY_DONE;
1271 }
1272
1273 static struct notifier_block arp_netdev_notifier = {
1274 .notifier_call = arp_netdev_event,
1275 };
1276
1277 /* Note, that it is not on notifier chain.
1278 It is necessary, that this routine was called after route cache will be
1279 flushed.
1280 */
arp_ifdown(struct net_device * dev)1281 void arp_ifdown(struct net_device *dev)
1282 {
1283 neigh_ifdown(&arp_tbl, dev);
1284 }
1285
1286
1287 /*
1288 * Called once on startup.
1289 */
1290
1291 static struct packet_type arp_packet_type __read_mostly = {
1292 .type = cpu_to_be16(ETH_P_ARP),
1293 .func = arp_rcv,
1294 };
1295
1296 static int arp_proc_init(void);
1297
arp_init(void)1298 void __init arp_init(void)
1299 {
1300 neigh_table_init(&arp_tbl);
1301
1302 dev_add_pack(&arp_packet_type);
1303 arp_proc_init();
1304 #ifdef CONFIG_SYSCTL
1305 neigh_sysctl_register(NULL, &arp_tbl.parms, NULL);
1306 #endif
1307 register_netdevice_notifier(&arp_netdev_notifier);
1308 }
1309
1310 #ifdef CONFIG_PROC_FS
1311 #if IS_ENABLED(CONFIG_AX25)
1312
1313 /* ------------------------------------------------------------------------ */
1314 /*
1315 * ax25 -> ASCII conversion
1316 */
ax2asc2(ax25_address * a,char * buf)1317 static char *ax2asc2(ax25_address *a, char *buf)
1318 {
1319 char c, *s;
1320 int n;
1321
1322 for (n = 0, s = buf; n < 6; n++) {
1323 c = (a->ax25_call[n] >> 1) & 0x7F;
1324
1325 if (c != ' ')
1326 *s++ = c;
1327 }
1328
1329 *s++ = '-';
1330 n = (a->ax25_call[6] >> 1) & 0x0F;
1331 if (n > 9) {
1332 *s++ = '1';
1333 n -= 10;
1334 }
1335
1336 *s++ = n + '0';
1337 *s++ = '\0';
1338
1339 if (*buf == '\0' || *buf == '-')
1340 return "*";
1341
1342 return buf;
1343 }
1344 #endif /* CONFIG_AX25 */
1345
1346 #define HBUFFERLEN 30
1347
arp_format_neigh_entry(struct seq_file * seq,struct neighbour * n)1348 static void arp_format_neigh_entry(struct seq_file *seq,
1349 struct neighbour *n)
1350 {
1351 char hbuffer[HBUFFERLEN];
1352 int k, j;
1353 char tbuf[16];
1354 struct net_device *dev = n->dev;
1355 int hatype = dev->type;
1356
1357 read_lock(&n->lock);
1358 /* Convert hardware address to XX:XX:XX:XX ... form. */
1359 #if IS_ENABLED(CONFIG_AX25)
1360 if (hatype == ARPHRD_AX25 || hatype == ARPHRD_NETROM)
1361 ax2asc2((ax25_address *)n->ha, hbuffer);
1362 else {
1363 #endif
1364 for (k = 0, j = 0; k < HBUFFERLEN - 3 && j < dev->addr_len; j++) {
1365 hbuffer[k++] = hex_asc_hi(n->ha[j]);
1366 hbuffer[k++] = hex_asc_lo(n->ha[j]);
1367 hbuffer[k++] = ':';
1368 }
1369 if (k != 0)
1370 --k;
1371 hbuffer[k] = 0;
1372 #if IS_ENABLED(CONFIG_AX25)
1373 }
1374 #endif
1375 sprintf(tbuf, "%pI4", n->primary_key);
1376 seq_printf(seq, "%-16s 0x%-10x0x%-10x%s * %s\n",
1377 tbuf, hatype, arp_state_to_flags(n), hbuffer, dev->name);
1378 read_unlock(&n->lock);
1379 }
1380
arp_format_pneigh_entry(struct seq_file * seq,struct pneigh_entry * n)1381 static void arp_format_pneigh_entry(struct seq_file *seq,
1382 struct pneigh_entry *n)
1383 {
1384 struct net_device *dev = n->dev;
1385 int hatype = dev ? dev->type : 0;
1386 char tbuf[16];
1387
1388 sprintf(tbuf, "%pI4", n->key);
1389 seq_printf(seq, "%-16s 0x%-10x0x%-10x%s * %s\n",
1390 tbuf, hatype, ATF_PUBL | ATF_PERM, "00:00:00:00:00:00",
1391 dev ? dev->name : "*");
1392 }
1393
arp_seq_show(struct seq_file * seq,void * v)1394 static int arp_seq_show(struct seq_file *seq, void *v)
1395 {
1396 if (v == SEQ_START_TOKEN) {
1397 seq_puts(seq, "IP address HW type Flags "
1398 "HW address Mask Device\n");
1399 } else {
1400 struct neigh_seq_state *state = seq->private;
1401
1402 if (state->flags & NEIGH_SEQ_IS_PNEIGH)
1403 arp_format_pneigh_entry(seq, v);
1404 else
1405 arp_format_neigh_entry(seq, v);
1406 }
1407
1408 return 0;
1409 }
1410
arp_seq_start(struct seq_file * seq,loff_t * pos)1411 static void *arp_seq_start(struct seq_file *seq, loff_t *pos)
1412 {
1413 /* Don't want to confuse "arp -a" w/ magic entries,
1414 * so we tell the generic iterator to skip NUD_NOARP.
1415 */
1416 return neigh_seq_start(seq, pos, &arp_tbl, NEIGH_SEQ_SKIP_NOARP);
1417 }
1418
1419 /* ------------------------------------------------------------------------ */
1420
1421 static const struct seq_operations arp_seq_ops = {
1422 .start = arp_seq_start,
1423 .next = neigh_seq_next,
1424 .stop = neigh_seq_stop,
1425 .show = arp_seq_show,
1426 };
1427
arp_seq_open(struct inode * inode,struct file * file)1428 static int arp_seq_open(struct inode *inode, struct file *file)
1429 {
1430 return seq_open_net(inode, file, &arp_seq_ops,
1431 sizeof(struct neigh_seq_state));
1432 }
1433
1434 static const struct file_operations arp_seq_fops = {
1435 .owner = THIS_MODULE,
1436 .open = arp_seq_open,
1437 .read = seq_read,
1438 .llseek = seq_lseek,
1439 .release = seq_release_net,
1440 };
1441
1442
arp_net_init(struct net * net)1443 static int __net_init arp_net_init(struct net *net)
1444 {
1445 if (!proc_create("arp", S_IRUGO, net->proc_net, &arp_seq_fops))
1446 return -ENOMEM;
1447 return 0;
1448 }
1449
arp_net_exit(struct net * net)1450 static void __net_exit arp_net_exit(struct net *net)
1451 {
1452 remove_proc_entry("arp", net->proc_net);
1453 }
1454
1455 static struct pernet_operations arp_net_ops = {
1456 .init = arp_net_init,
1457 .exit = arp_net_exit,
1458 };
1459
arp_proc_init(void)1460 static int __init arp_proc_init(void)
1461 {
1462 return register_pernet_subsys(&arp_net_ops);
1463 }
1464
1465 #else /* CONFIG_PROC_FS */
1466
arp_proc_init(void)1467 static int __init arp_proc_init(void)
1468 {
1469 return 0;
1470 }
1471
1472 #endif /* CONFIG_PROC_FS */
1473