blob: fb4372cb1de1f2fdabdb2cfb9a8b2305234c939b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Generic address resolution entity
3 *
4 * Authors:
5 * Pedro Roque <roque@di.fc.ul.pt>
6 * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version
11 * 2 of the License, or (at your option) any later version.
12 *
13 * Fixes:
14 * Vitaly E. Lavrov releasing NULL neighbor in neigh_add.
15 * Harald Welte Add neighbour cache statistics like rtstat
16 */
17
Joe Perchese005d192012-05-16 19:58:40 +000018#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
19
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090020#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/types.h>
22#include <linux/kernel.h>
23#include <linux/module.h>
24#include <linux/socket.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/netdevice.h>
26#include <linux/proc_fs.h>
27#ifdef CONFIG_SYSCTL
28#include <linux/sysctl.h>
29#endif
30#include <linux/times.h>
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020031#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <net/neighbour.h>
33#include <net/dst.h>
34#include <net/sock.h>
Tom Tucker8d717402006-07-30 20:43:36 -070035#include <net/netevent.h>
Thomas Grafa14a49d2006-08-07 17:53:08 -070036#include <net/netlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <linux/rtnetlink.h>
38#include <linux/random.h>
Paulo Marques543537b2005-06-23 00:09:02 -070039#include <linux/string.h>
vignesh babuc3609d52007-08-24 22:27:55 -070040#include <linux/log2.h>
Jiri Pirko1d4c8c22013-12-07 19:26:56 +010041#include <linux/inetdevice.h>
Jiri Pirkobba24892013-12-07 19:26:57 +010042#include <net/addrconf.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Joe Perchesd5d427c2013-04-15 15:17:19 +000044#define DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#define NEIGH_DEBUG 1
Joe Perchesd5d427c2013-04-15 15:17:19 +000046#define neigh_dbg(level, fmt, ...) \
47do { \
48 if (level <= NEIGH_DEBUG) \
49 pr_debug(fmt, ##__VA_ARGS__); \
50} while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
52#define PNEIGH_HASHMASK 0xF
53
Kees Cooke99e88a2017-10-16 14:43:17 -070054static void neigh_timer_handler(struct timer_list *t);
Roopa Prabhu7b8f7a42017-03-19 22:01:28 -070055static void __neigh_notify(struct neighbour *n, int type, int flags,
56 u32 pid);
57static void neigh_update_notify(struct neighbour *neigh, u32 nlmsg_pid);
Wolfgang Bumiller53b76cd2018-04-12 10:46:55 +020058static int pneigh_ifdown_and_unlock(struct neigh_table *tbl,
59 struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
Amos Waterland45fc3b12005-09-24 16:53:16 -070061#ifdef CONFIG_PROC_FS
Christoph Hellwig71a50532018-04-15 10:16:41 +020062static const struct seq_operations neigh_stat_seq_ops;
Amos Waterland45fc3b12005-09-24 16:53:16 -070063#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
65/*
66 Neighbour hash table buckets are protected with rwlock tbl->lock.
67
68 - All the scans/updates to hash buckets MUST be made under this lock.
69 - NOTHING clever should be made under this lock: no callbacks
70 to protocol backends, no attempts to send something to network.
71 It will result in deadlocks, if backend/driver wants to use neighbour
72 cache.
73 - If the entry requires some non-trivial actions, increase
74 its reference count and release table lock.
75
76 Neighbour entries are protected:
77 - with reference count.
78 - with rwlock neigh->lock
79
80 Reference count prevents destruction.
81
82 neigh->lock mainly serializes ll address data and its validity state.
83 However, the same lock is used to protect another entry fields:
84 - timer
85 - resolution queue
86
87 Again, nothing clever shall be made under neigh->lock,
88 the most complicated procedure, which we allow is dev->hard_header.
89 It is supposed, that dev->hard_header is simplistic and does
90 not make callbacks to neighbour tables.
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 */
92
David S. Miller8f40b162011-07-17 13:34:11 -070093static int neigh_blackhole(struct neighbour *neigh, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -070094{
95 kfree_skb(skb);
96 return -ENETDOWN;
97}
98
Thomas Graf4f494552007-08-08 23:12:36 -070099static void neigh_cleanup_and_release(struct neighbour *neigh)
100{
101 if (neigh->parms->neigh_cleanup)
102 neigh->parms->neigh_cleanup(neigh);
103
Roopa Prabhu7b8f7a42017-03-19 22:01:28 -0700104 __neigh_notify(neigh, RTM_DELNEIGH, 0, 0);
Ido Schimmel53f800e2016-12-23 09:32:48 +0100105 call_netevent_notifiers(NETEVENT_NEIGH_UPDATE, neigh);
Thomas Graf4f494552007-08-08 23:12:36 -0700106 neigh_release(neigh);
107}
108
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109/*
110 * It is random distribution in the interval (1/2)*base...(3/2)*base.
111 * It corresponds to default IPv6 settings and is not overridable,
112 * because it is really reasonable choice.
113 */
114
115unsigned long neigh_rand_reach_time(unsigned long base)
116{
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -0500117 return base ? (prandom_u32() % base) + (base >> 1) : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118}
YOSHIFUJI Hideaki0a204502008-03-24 18:39:10 +0900119EXPORT_SYMBOL(neigh_rand_reach_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
David Ahern58956312018-12-07 12:24:57 -0800121static void neigh_mark_dead(struct neighbour *n)
122{
123 n->dead = 1;
124 if (!list_empty(&n->gc_list)) {
125 list_del_init(&n->gc_list);
126 atomic_dec(&n->tbl->gc_entries);
127 }
128}
129
David Ahern9c29a2f2018-12-11 18:57:21 -0700130static void neigh_update_gc_list(struct neighbour *n)
David Ahern58956312018-12-07 12:24:57 -0800131{
David Aherne997f8a2018-12-11 18:57:25 -0700132 bool on_gc_list, exempt_from_gc;
David Ahern58956312018-12-07 12:24:57 -0800133
David Ahern9c29a2f2018-12-11 18:57:21 -0700134 write_lock_bh(&n->tbl->lock);
135 write_lock(&n->lock);
David Ahern58956312018-12-07 12:24:57 -0800136
David Aherne997f8a2018-12-11 18:57:25 -0700137 /* remove from the gc list if new state is permanent or if neighbor
138 * is externally learned; otherwise entry should be on the gc list
David Ahern58956312018-12-07 12:24:57 -0800139 */
David Aherne997f8a2018-12-11 18:57:25 -0700140 exempt_from_gc = n->nud_state & NUD_PERMANENT ||
141 n->flags & NTF_EXT_LEARNED;
David Ahern9c29a2f2018-12-11 18:57:21 -0700142 on_gc_list = !list_empty(&n->gc_list);
David Ahern8cc196d2018-12-10 13:54:07 -0800143
David Aherne997f8a2018-12-11 18:57:25 -0700144 if (exempt_from_gc && on_gc_list) {
David Ahern9c29a2f2018-12-11 18:57:21 -0700145 list_del_init(&n->gc_list);
David Ahern58956312018-12-07 12:24:57 -0800146 atomic_dec(&n->tbl->gc_entries);
David Aherne997f8a2018-12-11 18:57:25 -0700147 } else if (!exempt_from_gc && !on_gc_list) {
David Ahern58956312018-12-07 12:24:57 -0800148 /* add entries to the tail; cleaning removes from the front */
149 list_add_tail(&n->gc_list, &n->tbl->gc_list);
150 atomic_inc(&n->tbl->gc_entries);
151 }
David Ahern9c29a2f2018-12-11 18:57:21 -0700152
153 write_unlock(&n->lock);
154 write_unlock_bh(&n->tbl->lock);
David Ahern58956312018-12-07 12:24:57 -0800155}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
David Aherne997f8a2018-12-11 18:57:25 -0700157static bool neigh_update_ext_learned(struct neighbour *neigh, u32 flags,
David Ahern526f1b52018-12-11 18:57:24 -0700158 int *notify)
159{
David Aherne997f8a2018-12-11 18:57:25 -0700160 bool rc = false;
David Ahern526f1b52018-12-11 18:57:24 -0700161 u8 ndm_flags;
162
163 if (!(flags & NEIGH_UPDATE_F_ADMIN))
David Aherne997f8a2018-12-11 18:57:25 -0700164 return rc;
David Ahern526f1b52018-12-11 18:57:24 -0700165
166 ndm_flags = (flags & NEIGH_UPDATE_F_EXT_LEARNED) ? NTF_EXT_LEARNED : 0;
167 if ((neigh->flags ^ ndm_flags) & NTF_EXT_LEARNED) {
168 if (ndm_flags & NTF_EXT_LEARNED)
169 neigh->flags |= NTF_EXT_LEARNED;
170 else
171 neigh->flags &= ~NTF_EXT_LEARNED;
David Aherne997f8a2018-12-11 18:57:25 -0700172 rc = true;
David Ahern526f1b52018-12-11 18:57:24 -0700173 *notify = 1;
174 }
David Aherne997f8a2018-12-11 18:57:25 -0700175
176 return rc;
David Ahern526f1b52018-12-11 18:57:24 -0700177}
178
David Ahern7e6f1822018-12-11 18:57:23 -0700179static bool neigh_del(struct neighbour *n, struct neighbour __rcu **np,
180 struct neigh_table *tbl)
Sowmini Varadhan50710342017-06-02 09:01:49 -0700181{
182 bool retval = false;
183
184 write_lock(&n->lock);
David Ahern7e6f1822018-12-11 18:57:23 -0700185 if (refcount_read(&n->refcnt) == 1) {
Sowmini Varadhan50710342017-06-02 09:01:49 -0700186 struct neighbour *neigh;
187
188 neigh = rcu_dereference_protected(n->next,
189 lockdep_is_held(&tbl->lock));
190 rcu_assign_pointer(*np, neigh);
David Ahern58956312018-12-07 12:24:57 -0800191 neigh_mark_dead(n);
Sowmini Varadhan50710342017-06-02 09:01:49 -0700192 retval = true;
193 }
194 write_unlock(&n->lock);
195 if (retval)
196 neigh_cleanup_and_release(n);
197 return retval;
198}
199
200bool neigh_remove_one(struct neighbour *ndel, struct neigh_table *tbl)
201{
202 struct neigh_hash_table *nht;
203 void *pkey = ndel->primary_key;
204 u32 hash_val;
205 struct neighbour *n;
206 struct neighbour __rcu **np;
207
208 nht = rcu_dereference_protected(tbl->nht,
209 lockdep_is_held(&tbl->lock));
210 hash_val = tbl->hash(pkey, ndel->dev, nht->hash_rnd);
211 hash_val = hash_val >> (32 - nht->hash_shift);
212
213 np = &nht->hash_buckets[hash_val];
214 while ((n = rcu_dereference_protected(*np,
215 lockdep_is_held(&tbl->lock)))) {
216 if (n == ndel)
David Ahern7e6f1822018-12-11 18:57:23 -0700217 return neigh_del(n, np, tbl);
Sowmini Varadhan50710342017-06-02 09:01:49 -0700218 np = &n->next;
219 }
220 return false;
221}
222
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223static int neigh_forced_gc(struct neigh_table *tbl)
224{
David Ahern58956312018-12-07 12:24:57 -0800225 int max_clean = atomic_read(&tbl->gc_entries) - tbl->gc_thresh2;
226 unsigned long tref = jiffies - 5 * HZ;
David Ahern58956312018-12-07 12:24:57 -0800227 struct neighbour *n, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 int shrunk = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
230 NEIGH_CACHE_STAT_INC(tbl, forced_gc_runs);
231
232 write_lock_bh(&tbl->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
David Ahern58956312018-12-07 12:24:57 -0800234 list_for_each_entry_safe(n, tmp, &tbl->gc_list, gc_list) {
235 if (refcount_read(&n->refcnt) == 1) {
236 bool remove = false;
237
238 write_lock(&n->lock);
David Ahern758a7f02018-12-11 18:57:22 -0700239 if ((n->nud_state == NUD_FAILED) ||
David Aherne997f8a2018-12-11 18:57:25 -0700240 time_after(tref, n->updated))
David Ahern58956312018-12-07 12:24:57 -0800241 remove = true;
242 write_unlock(&n->lock);
243
244 if (remove && neigh_remove_one(n, tbl))
245 shrunk++;
246 if (shrunk >= max_clean)
247 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 }
249 }
250
251 tbl->last_flush = jiffies;
252
253 write_unlock_bh(&tbl->lock);
254
255 return shrunk;
256}
257
Pavel Emelyanova43d8992007-12-20 15:49:05 -0800258static void neigh_add_timer(struct neighbour *n, unsigned long when)
259{
260 neigh_hold(n);
261 if (unlikely(mod_timer(&n->timer, when))) {
262 printk("NEIGH: BUG, double timer add, state is %x\n",
263 n->nud_state);
264 dump_stack();
265 }
266}
267
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268static int neigh_del_timer(struct neighbour *n)
269{
270 if ((n->nud_state & NUD_IN_TIMER) &&
271 del_timer(&n->timer)) {
272 neigh_release(n);
273 return 1;
274 }
275 return 0;
276}
277
278static void pneigh_queue_purge(struct sk_buff_head *list)
279{
280 struct sk_buff *skb;
281
282 while ((skb = skb_dequeue(list)) != NULL) {
283 dev_put(skb->dev);
284 kfree_skb(skb);
285 }
286}
287
David Ahern859bd2e2018-10-11 20:33:49 -0700288static void neigh_flush_dev(struct neigh_table *tbl, struct net_device *dev,
289 bool skip_perm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290{
291 int i;
Eric Dumazetd6bf7812010-10-04 06:15:44 +0000292 struct neigh_hash_table *nht;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
Eric Dumazetd6bf7812010-10-04 06:15:44 +0000294 nht = rcu_dereference_protected(tbl->nht,
295 lockdep_is_held(&tbl->lock));
296
David S. Millercd089332011-07-11 01:28:12 -0700297 for (i = 0; i < (1 << nht->hash_shift); i++) {
Eric Dumazet767e97e2010-10-06 17:49:21 -0700298 struct neighbour *n;
299 struct neighbour __rcu **np = &nht->hash_buckets[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
Eric Dumazet767e97e2010-10-06 17:49:21 -0700301 while ((n = rcu_dereference_protected(*np,
302 lockdep_is_held(&tbl->lock))) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 if (dev && n->dev != dev) {
304 np = &n->next;
305 continue;
306 }
David Ahern859bd2e2018-10-11 20:33:49 -0700307 if (skip_perm && n->nud_state & NUD_PERMANENT) {
308 np = &n->next;
309 continue;
310 }
Eric Dumazet767e97e2010-10-06 17:49:21 -0700311 rcu_assign_pointer(*np,
312 rcu_dereference_protected(n->next,
313 lockdep_is_held(&tbl->lock)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 write_lock(&n->lock);
315 neigh_del_timer(n);
David Ahern58956312018-12-07 12:24:57 -0800316 neigh_mark_dead(n);
Reshetova, Elena9f237432017-06-30 13:07:55 +0300317 if (refcount_read(&n->refcnt) != 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 /* The most unpleasant situation.
319 We must destroy neighbour entry,
320 but someone still uses it.
321
322 The destroy will be delayed until
323 the last user releases us, but
324 we must kill timers etc. and move
325 it to safe state.
326 */
Eric Dumazetc9ab4d82013-06-28 02:37:42 -0700327 __skb_queue_purge(&n->arp_queue);
Eric Dumazet8b5c1712011-11-09 12:07:14 +0000328 n->arp_queue_len_bytes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 n->output = neigh_blackhole;
330 if (n->nud_state & NUD_VALID)
331 n->nud_state = NUD_NOARP;
332 else
333 n->nud_state = NUD_NONE;
Joe Perchesd5d427c2013-04-15 15:17:19 +0000334 neigh_dbg(2, "neigh %p is stray\n", n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 }
336 write_unlock(&n->lock);
Thomas Graf4f494552007-08-08 23:12:36 -0700337 neigh_cleanup_and_release(n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 }
339 }
Herbert Xu49636bb2005-10-23 17:18:00 +1000340}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
Herbert Xu49636bb2005-10-23 17:18:00 +1000342void neigh_changeaddr(struct neigh_table *tbl, struct net_device *dev)
343{
344 write_lock_bh(&tbl->lock);
David Ahern859bd2e2018-10-11 20:33:49 -0700345 neigh_flush_dev(tbl, dev, false);
Herbert Xu49636bb2005-10-23 17:18:00 +1000346 write_unlock_bh(&tbl->lock);
347}
YOSHIFUJI Hideaki0a204502008-03-24 18:39:10 +0900348EXPORT_SYMBOL(neigh_changeaddr);
Herbert Xu49636bb2005-10-23 17:18:00 +1000349
David Ahern859bd2e2018-10-11 20:33:49 -0700350static int __neigh_ifdown(struct neigh_table *tbl, struct net_device *dev,
351 bool skip_perm)
Herbert Xu49636bb2005-10-23 17:18:00 +1000352{
353 write_lock_bh(&tbl->lock);
David Ahern859bd2e2018-10-11 20:33:49 -0700354 neigh_flush_dev(tbl, dev, skip_perm);
Wolfgang Bumiller53b76cd2018-04-12 10:46:55 +0200355 pneigh_ifdown_and_unlock(tbl, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
357 del_timer_sync(&tbl->proxy_timer);
358 pneigh_queue_purge(&tbl->proxy_queue);
359 return 0;
360}
David Ahern859bd2e2018-10-11 20:33:49 -0700361
362int neigh_carrier_down(struct neigh_table *tbl, struct net_device *dev)
363{
364 __neigh_ifdown(tbl, dev, true);
365 return 0;
366}
367EXPORT_SYMBOL(neigh_carrier_down);
368
369int neigh_ifdown(struct neigh_table *tbl, struct net_device *dev)
370{
371 __neigh_ifdown(tbl, dev, false);
372 return 0;
373}
YOSHIFUJI Hideaki0a204502008-03-24 18:39:10 +0900374EXPORT_SYMBOL(neigh_ifdown);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
David Ahern58956312018-12-07 12:24:57 -0800376static struct neighbour *neigh_alloc(struct neigh_table *tbl,
377 struct net_device *dev,
David Aherne997f8a2018-12-11 18:57:25 -0700378 bool exempt_from_gc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379{
380 struct neighbour *n = NULL;
381 unsigned long now = jiffies;
382 int entries;
383
David Aherne997f8a2018-12-11 18:57:25 -0700384 if (exempt_from_gc)
David Ahern58956312018-12-07 12:24:57 -0800385 goto do_alloc;
386
387 entries = atomic_inc_return(&tbl->gc_entries) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 if (entries >= tbl->gc_thresh3 ||
389 (entries >= tbl->gc_thresh2 &&
390 time_after(now, tbl->last_flush + 5 * HZ))) {
391 if (!neigh_forced_gc(tbl) &&
Rick Jonesfb811392015-08-07 11:10:37 -0700392 entries >= tbl->gc_thresh3) {
393 net_info_ratelimited("%s: neighbor table overflow!\n",
394 tbl->id);
395 NEIGH_CACHE_STAT_INC(tbl, table_fulls);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 goto out_entries;
Rick Jonesfb811392015-08-07 11:10:37 -0700397 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 }
399
David Ahern58956312018-12-07 12:24:57 -0800400do_alloc:
YOSHIFUJI Hideaki / 吉藤英明08433ef2013-01-24 00:44:23 +0000401 n = kzalloc(tbl->entry_size + dev->neigh_priv_len, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 if (!n)
403 goto out_entries;
404
Eric Dumazetc9ab4d82013-06-28 02:37:42 -0700405 __skb_queue_head_init(&n->arp_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 rwlock_init(&n->lock);
Eric Dumazet0ed8ddf2010-10-07 10:44:07 +0000407 seqlock_init(&n->ha_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 n->updated = n->used = now;
409 n->nud_state = NUD_NONE;
410 n->output = neigh_blackhole;
David S. Millerf6b72b622011-07-14 07:53:20 -0700411 seqlock_init(&n->hh.hh_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 n->parms = neigh_parms_clone(&tbl->parms);
Kees Cooke99e88a2017-10-16 14:43:17 -0700413 timer_setup(&n->timer, neigh_timer_handler, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
415 NEIGH_CACHE_STAT_INC(tbl, allocs);
416 n->tbl = tbl;
Reshetova, Elena9f237432017-06-30 13:07:55 +0300417 refcount_set(&n->refcnt, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 n->dead = 1;
David Ahern8cc196d2018-12-10 13:54:07 -0800419 INIT_LIST_HEAD(&n->gc_list);
David Ahern58956312018-12-07 12:24:57 -0800420
421 atomic_inc(&tbl->entries);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422out:
423 return n;
424
425out_entries:
David Aherne997f8a2018-12-11 18:57:25 -0700426 if (!exempt_from_gc)
David Ahern58956312018-12-07 12:24:57 -0800427 atomic_dec(&tbl->gc_entries);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 goto out;
429}
430
David S. Miller2c2aba62011-12-28 15:06:58 -0500431static void neigh_get_hash_rnd(u32 *x)
432{
Jason A. Donenfeldb3d0f782017-06-07 23:00:05 -0400433 *x = get_random_u32() | 1;
David S. Miller2c2aba62011-12-28 15:06:58 -0500434}
435
David S. Millercd089332011-07-11 01:28:12 -0700436static struct neigh_hash_table *neigh_hash_alloc(unsigned int shift)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437{
David S. Millercd089332011-07-11 01:28:12 -0700438 size_t size = (1 << shift) * sizeof(struct neighbour *);
Eric Dumazetd6bf7812010-10-04 06:15:44 +0000439 struct neigh_hash_table *ret;
Eric Dumazet6193d2b2011-01-19 22:02:47 +0000440 struct neighbour __rcu **buckets;
David S. Miller2c2aba62011-12-28 15:06:58 -0500441 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
Eric Dumazetd6bf7812010-10-04 06:15:44 +0000443 ret = kmalloc(sizeof(*ret), GFP_ATOMIC);
444 if (!ret)
445 return NULL;
446 if (size <= PAGE_SIZE)
447 buckets = kzalloc(size, GFP_ATOMIC);
448 else
Eric Dumazet6193d2b2011-01-19 22:02:47 +0000449 buckets = (struct neighbour __rcu **)
Eric Dumazetd6bf7812010-10-04 06:15:44 +0000450 __get_free_pages(GFP_ATOMIC | __GFP_ZERO,
451 get_order(size));
452 if (!buckets) {
453 kfree(ret);
454 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 }
Eric Dumazet6193d2b2011-01-19 22:02:47 +0000456 ret->hash_buckets = buckets;
David S. Millercd089332011-07-11 01:28:12 -0700457 ret->hash_shift = shift;
David S. Miller2c2aba62011-12-28 15:06:58 -0500458 for (i = 0; i < NEIGH_NUM_HASH_RND; i++)
459 neigh_get_hash_rnd(&ret->hash_rnd[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 return ret;
461}
462
Eric Dumazetd6bf7812010-10-04 06:15:44 +0000463static void neigh_hash_free_rcu(struct rcu_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464{
Eric Dumazetd6bf7812010-10-04 06:15:44 +0000465 struct neigh_hash_table *nht = container_of(head,
466 struct neigh_hash_table,
467 rcu);
David S. Millercd089332011-07-11 01:28:12 -0700468 size_t size = (1 << nht->hash_shift) * sizeof(struct neighbour *);
Eric Dumazet6193d2b2011-01-19 22:02:47 +0000469 struct neighbour __rcu **buckets = nht->hash_buckets;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
471 if (size <= PAGE_SIZE)
Eric Dumazetd6bf7812010-10-04 06:15:44 +0000472 kfree(buckets);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 else
Eric Dumazetd6bf7812010-10-04 06:15:44 +0000474 free_pages((unsigned long)buckets, get_order(size));
475 kfree(nht);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476}
477
Eric Dumazetd6bf7812010-10-04 06:15:44 +0000478static struct neigh_hash_table *neigh_hash_grow(struct neigh_table *tbl,
David S. Millercd089332011-07-11 01:28:12 -0700479 unsigned long new_shift)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480{
Eric Dumazetd6bf7812010-10-04 06:15:44 +0000481 unsigned int i, hash;
482 struct neigh_hash_table *new_nht, *old_nht;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483
484 NEIGH_CACHE_STAT_INC(tbl, hash_grows);
485
Eric Dumazetd6bf7812010-10-04 06:15:44 +0000486 old_nht = rcu_dereference_protected(tbl->nht,
487 lockdep_is_held(&tbl->lock));
David S. Millercd089332011-07-11 01:28:12 -0700488 new_nht = neigh_hash_alloc(new_shift);
Eric Dumazetd6bf7812010-10-04 06:15:44 +0000489 if (!new_nht)
490 return old_nht;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
David S. Millercd089332011-07-11 01:28:12 -0700492 for (i = 0; i < (1 << old_nht->hash_shift); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 struct neighbour *n, *next;
494
Eric Dumazet767e97e2010-10-06 17:49:21 -0700495 for (n = rcu_dereference_protected(old_nht->hash_buckets[i],
496 lockdep_is_held(&tbl->lock));
Eric Dumazetd6bf7812010-10-04 06:15:44 +0000497 n != NULL;
498 n = next) {
499 hash = tbl->hash(n->primary_key, n->dev,
500 new_nht->hash_rnd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
David S. Millercd089332011-07-11 01:28:12 -0700502 hash >>= (32 - new_nht->hash_shift);
Eric Dumazet767e97e2010-10-06 17:49:21 -0700503 next = rcu_dereference_protected(n->next,
504 lockdep_is_held(&tbl->lock));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
Eric Dumazet767e97e2010-10-06 17:49:21 -0700506 rcu_assign_pointer(n->next,
507 rcu_dereference_protected(
508 new_nht->hash_buckets[hash],
509 lockdep_is_held(&tbl->lock)));
510 rcu_assign_pointer(new_nht->hash_buckets[hash], n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 }
512 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
Eric Dumazetd6bf7812010-10-04 06:15:44 +0000514 rcu_assign_pointer(tbl->nht, new_nht);
515 call_rcu(&old_nht->rcu, neigh_hash_free_rcu);
516 return new_nht;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517}
518
519struct neighbour *neigh_lookup(struct neigh_table *tbl, const void *pkey,
520 struct net_device *dev)
521{
522 struct neighbour *n;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900523
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 NEIGH_CACHE_STAT_INC(tbl, lookups);
525
Eric Dumazetd6bf7812010-10-04 06:15:44 +0000526 rcu_read_lock_bh();
Eric W. Biederman60395a22015-03-03 17:10:44 -0600527 n = __neigh_lookup_noref(tbl, pkey, dev);
528 if (n) {
Reshetova, Elena9f237432017-06-30 13:07:55 +0300529 if (!refcount_inc_not_zero(&n->refcnt))
Eric W. Biederman60395a22015-03-03 17:10:44 -0600530 n = NULL;
531 NEIGH_CACHE_STAT_INC(tbl, hits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 }
Eric Dumazet767e97e2010-10-06 17:49:21 -0700533
Eric Dumazetd6bf7812010-10-04 06:15:44 +0000534 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 return n;
536}
YOSHIFUJI Hideaki0a204502008-03-24 18:39:10 +0900537EXPORT_SYMBOL(neigh_lookup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
Eric W. Biederman426b5302008-01-24 00:13:18 -0800539struct neighbour *neigh_lookup_nodev(struct neigh_table *tbl, struct net *net,
540 const void *pkey)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541{
542 struct neighbour *n;
Alexey Dobriyan01ccdf12017-09-23 23:03:04 +0300543 unsigned int key_len = tbl->key_len;
Pavel Emelyanovbc4bf5f2008-02-23 19:57:02 -0800544 u32 hash_val;
Eric Dumazetd6bf7812010-10-04 06:15:44 +0000545 struct neigh_hash_table *nht;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
547 NEIGH_CACHE_STAT_INC(tbl, lookups);
548
Eric Dumazetd6bf7812010-10-04 06:15:44 +0000549 rcu_read_lock_bh();
550 nht = rcu_dereference_bh(tbl->nht);
David S. Millercd089332011-07-11 01:28:12 -0700551 hash_val = tbl->hash(pkey, NULL, nht->hash_rnd) >> (32 - nht->hash_shift);
Eric Dumazet767e97e2010-10-06 17:49:21 -0700552
553 for (n = rcu_dereference_bh(nht->hash_buckets[hash_val]);
554 n != NULL;
555 n = rcu_dereference_bh(n->next)) {
Eric W. Biederman426b5302008-01-24 00:13:18 -0800556 if (!memcmp(n->primary_key, pkey, key_len) &&
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +0900557 net_eq(dev_net(n->dev), net)) {
Reshetova, Elena9f237432017-06-30 13:07:55 +0300558 if (!refcount_inc_not_zero(&n->refcnt))
Eric Dumazet767e97e2010-10-06 17:49:21 -0700559 n = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 NEIGH_CACHE_STAT_INC(tbl, hits);
561 break;
562 }
563 }
Eric Dumazet767e97e2010-10-06 17:49:21 -0700564
Eric Dumazetd6bf7812010-10-04 06:15:44 +0000565 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 return n;
567}
YOSHIFUJI Hideaki0a204502008-03-24 18:39:10 +0900568EXPORT_SYMBOL(neigh_lookup_nodev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
David Ahern58956312018-12-07 12:24:57 -0800570static struct neighbour *___neigh_create(struct neigh_table *tbl,
571 const void *pkey,
572 struct net_device *dev,
David Aherne997f8a2018-12-11 18:57:25 -0700573 bool exempt_from_gc, bool want_ref)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574{
David Aherne997f8a2018-12-11 18:57:25 -0700575 struct neighbour *n1, *rc, *n = neigh_alloc(tbl, dev, exempt_from_gc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 u32 hash_val;
Alexey Dobriyan01ccdf12017-09-23 23:03:04 +0300577 unsigned int key_len = tbl->key_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 int error;
Eric Dumazetd6bf7812010-10-04 06:15:44 +0000579 struct neigh_hash_table *nht;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
581 if (!n) {
582 rc = ERR_PTR(-ENOBUFS);
583 goto out;
584 }
585
586 memcpy(n->primary_key, pkey, key_len);
587 n->dev = dev;
588 dev_hold(dev);
589
590 /* Protocol specific setup. */
591 if (tbl->constructor && (error = tbl->constructor(n)) < 0) {
592 rc = ERR_PTR(error);
593 goto out_neigh_release;
594 }
595
David Millerda6a8fa2011-07-25 00:01:38 +0000596 if (dev->netdev_ops->ndo_neigh_construct) {
Jiri Pirko503eebc2016-07-05 11:27:37 +0200597 error = dev->netdev_ops->ndo_neigh_construct(dev, n);
David Millerda6a8fa2011-07-25 00:01:38 +0000598 if (error < 0) {
599 rc = ERR_PTR(error);
600 goto out_neigh_release;
601 }
602 }
603
David S. Miller447f2192011-12-19 15:04:41 -0500604 /* Device specific setup. */
605 if (n->parms->neigh_setup &&
606 (error = n->parms->neigh_setup(n)) < 0) {
607 rc = ERR_PTR(error);
608 goto out_neigh_release;
609 }
610
Jiri Pirko1f9248e2013-12-07 19:26:53 +0100611 n->confirmed = jiffies - (NEIGH_VAR(n->parms, BASE_REACHABLE_TIME) << 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
613 write_lock_bh(&tbl->lock);
Eric Dumazetd6bf7812010-10-04 06:15:44 +0000614 nht = rcu_dereference_protected(tbl->nht,
615 lockdep_is_held(&tbl->lock));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
David S. Millercd089332011-07-11 01:28:12 -0700617 if (atomic_read(&tbl->entries) > (1 << nht->hash_shift))
618 nht = neigh_hash_grow(tbl, nht->hash_shift + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
Jim Westfall096b9852018-01-14 04:18:50 -0800620 hash_val = tbl->hash(n->primary_key, dev, nht->hash_rnd) >> (32 - nht->hash_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
622 if (n->parms->dead) {
623 rc = ERR_PTR(-EINVAL);
624 goto out_tbl_unlock;
625 }
626
Eric Dumazet767e97e2010-10-06 17:49:21 -0700627 for (n1 = rcu_dereference_protected(nht->hash_buckets[hash_val],
628 lockdep_is_held(&tbl->lock));
629 n1 != NULL;
630 n1 = rcu_dereference_protected(n1->next,
631 lockdep_is_held(&tbl->lock))) {
Jim Westfall096b9852018-01-14 04:18:50 -0800632 if (dev == n1->dev && !memcmp(n1->primary_key, n->primary_key, key_len)) {
David S. Millera263b302012-07-02 02:02:15 -0700633 if (want_ref)
634 neigh_hold(n1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 rc = n1;
636 goto out_tbl_unlock;
637 }
638 }
639
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 n->dead = 0;
David Aherne997f8a2018-12-11 18:57:25 -0700641 if (!exempt_from_gc)
David Ahern8cc196d2018-12-10 13:54:07 -0800642 list_add_tail(&n->gc_list, &n->tbl->gc_list);
643
David S. Millera263b302012-07-02 02:02:15 -0700644 if (want_ref)
645 neigh_hold(n);
Eric Dumazet767e97e2010-10-06 17:49:21 -0700646 rcu_assign_pointer(n->next,
647 rcu_dereference_protected(nht->hash_buckets[hash_val],
648 lockdep_is_held(&tbl->lock)));
649 rcu_assign_pointer(nht->hash_buckets[hash_val], n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 write_unlock_bh(&tbl->lock);
Joe Perchesd5d427c2013-04-15 15:17:19 +0000651 neigh_dbg(2, "neigh %p is created\n", n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 rc = n;
653out:
654 return rc;
655out_tbl_unlock:
656 write_unlock_bh(&tbl->lock);
657out_neigh_release:
658 neigh_release(n);
659 goto out;
660}
David Ahern58956312018-12-07 12:24:57 -0800661
662struct neighbour *__neigh_create(struct neigh_table *tbl, const void *pkey,
663 struct net_device *dev, bool want_ref)
664{
665 return ___neigh_create(tbl, pkey, dev, false, want_ref);
666}
David S. Millera263b302012-07-02 02:02:15 -0700667EXPORT_SYMBOL(__neigh_create);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
Alexey Dobriyan01ccdf12017-09-23 23:03:04 +0300669static u32 pneigh_hash(const void *pkey, unsigned int key_len)
Pavel Emelyanovfa86d322008-03-24 14:48:59 -0700670{
Pavel Emelyanovfa86d322008-03-24 14:48:59 -0700671 u32 hash_val = *(u32 *)(pkey + key_len - 4);
Pavel Emelyanovfa86d322008-03-24 14:48:59 -0700672 hash_val ^= (hash_val >> 16);
673 hash_val ^= hash_val >> 8;
674 hash_val ^= hash_val >> 4;
675 hash_val &= PNEIGH_HASHMASK;
YOSHIFUJI Hideakibe01d652008-03-28 12:46:53 +0900676 return hash_val;
677}
Pavel Emelyanovfa86d322008-03-24 14:48:59 -0700678
YOSHIFUJI Hideakibe01d652008-03-28 12:46:53 +0900679static struct pneigh_entry *__pneigh_lookup_1(struct pneigh_entry *n,
680 struct net *net,
681 const void *pkey,
Alexey Dobriyan01ccdf12017-09-23 23:03:04 +0300682 unsigned int key_len,
YOSHIFUJI Hideakibe01d652008-03-28 12:46:53 +0900683 struct net_device *dev)
684{
685 while (n) {
Pavel Emelyanovfa86d322008-03-24 14:48:59 -0700686 if (!memcmp(n->key, pkey, key_len) &&
YOSHIFUJI Hideakibe01d652008-03-28 12:46:53 +0900687 net_eq(pneigh_net(n), net) &&
Pavel Emelyanovfa86d322008-03-24 14:48:59 -0700688 (n->dev == dev || !n->dev))
YOSHIFUJI Hideakibe01d652008-03-28 12:46:53 +0900689 return n;
690 n = n->next;
Pavel Emelyanovfa86d322008-03-24 14:48:59 -0700691 }
YOSHIFUJI Hideakibe01d652008-03-28 12:46:53 +0900692 return NULL;
693}
Pavel Emelyanovfa86d322008-03-24 14:48:59 -0700694
YOSHIFUJI Hideakibe01d652008-03-28 12:46:53 +0900695struct pneigh_entry *__pneigh_lookup(struct neigh_table *tbl,
696 struct net *net, const void *pkey, struct net_device *dev)
697{
Alexey Dobriyan01ccdf12017-09-23 23:03:04 +0300698 unsigned int key_len = tbl->key_len;
YOSHIFUJI Hideakibe01d652008-03-28 12:46:53 +0900699 u32 hash_val = pneigh_hash(pkey, key_len);
700
701 return __pneigh_lookup_1(tbl->phash_buckets[hash_val],
702 net, pkey, key_len, dev);
Pavel Emelyanovfa86d322008-03-24 14:48:59 -0700703}
YOSHIFUJI Hideaki0a204502008-03-24 18:39:10 +0900704EXPORT_SYMBOL_GPL(__pneigh_lookup);
Pavel Emelyanovfa86d322008-03-24 14:48:59 -0700705
Eric W. Biederman426b5302008-01-24 00:13:18 -0800706struct pneigh_entry * pneigh_lookup(struct neigh_table *tbl,
707 struct net *net, const void *pkey,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 struct net_device *dev, int creat)
709{
710 struct pneigh_entry *n;
Alexey Dobriyan01ccdf12017-09-23 23:03:04 +0300711 unsigned int key_len = tbl->key_len;
YOSHIFUJI Hideakibe01d652008-03-28 12:46:53 +0900712 u32 hash_val = pneigh_hash(pkey, key_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
714 read_lock_bh(&tbl->lock);
YOSHIFUJI Hideakibe01d652008-03-28 12:46:53 +0900715 n = __pneigh_lookup_1(tbl->phash_buckets[hash_val],
716 net, pkey, key_len, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 read_unlock_bh(&tbl->lock);
YOSHIFUJI Hideakibe01d652008-03-28 12:46:53 +0900718
719 if (n || !creat)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 goto out;
721
Pavel Emelyanov4ae28942007-10-15 12:54:15 -0700722 ASSERT_RTNL();
723
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 n = kmalloc(sizeof(*n) + key_len, GFP_KERNEL);
725 if (!n)
726 goto out;
727
Eric W. Biedermanefd7ef12015-03-11 23:04:08 -0500728 write_pnet(&n->net, net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 memcpy(n->key, pkey, key_len);
730 n->dev = dev;
731 if (dev)
732 dev_hold(dev);
733
734 if (tbl->pconstructor && tbl->pconstructor(n)) {
735 if (dev)
736 dev_put(dev);
737 kfree(n);
738 n = NULL;
739 goto out;
740 }
741
742 write_lock_bh(&tbl->lock);
743 n->next = tbl->phash_buckets[hash_val];
744 tbl->phash_buckets[hash_val] = n;
745 write_unlock_bh(&tbl->lock);
746out:
747 return n;
748}
YOSHIFUJI Hideaki0a204502008-03-24 18:39:10 +0900749EXPORT_SYMBOL(pneigh_lookup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750
751
Eric W. Biederman426b5302008-01-24 00:13:18 -0800752int pneigh_delete(struct neigh_table *tbl, struct net *net, const void *pkey,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 struct net_device *dev)
754{
755 struct pneigh_entry *n, **np;
Alexey Dobriyan01ccdf12017-09-23 23:03:04 +0300756 unsigned int key_len = tbl->key_len;
YOSHIFUJI Hideakibe01d652008-03-28 12:46:53 +0900757 u32 hash_val = pneigh_hash(pkey, key_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758
759 write_lock_bh(&tbl->lock);
760 for (np = &tbl->phash_buckets[hash_val]; (n = *np) != NULL;
761 np = &n->next) {
Eric W. Biederman426b5302008-01-24 00:13:18 -0800762 if (!memcmp(n->key, pkey, key_len) && n->dev == dev &&
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +0900763 net_eq(pneigh_net(n), net)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 *np = n->next;
765 write_unlock_bh(&tbl->lock);
766 if (tbl->pdestructor)
767 tbl->pdestructor(n);
768 if (n->dev)
769 dev_put(n->dev);
770 kfree(n);
771 return 0;
772 }
773 }
774 write_unlock_bh(&tbl->lock);
775 return -ENOENT;
776}
777
Wolfgang Bumiller53b76cd2018-04-12 10:46:55 +0200778static int pneigh_ifdown_and_unlock(struct neigh_table *tbl,
779 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780{
Wolfgang Bumiller53b76cd2018-04-12 10:46:55 +0200781 struct pneigh_entry *n, **np, *freelist = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 u32 h;
783
784 for (h = 0; h <= PNEIGH_HASHMASK; h++) {
785 np = &tbl->phash_buckets[h];
786 while ((n = *np) != NULL) {
787 if (!dev || n->dev == dev) {
788 *np = n->next;
Wolfgang Bumiller53b76cd2018-04-12 10:46:55 +0200789 n->next = freelist;
790 freelist = n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 continue;
792 }
793 np = &n->next;
794 }
795 }
Wolfgang Bumiller53b76cd2018-04-12 10:46:55 +0200796 write_unlock_bh(&tbl->lock);
797 while ((n = freelist)) {
798 freelist = n->next;
799 n->next = NULL;
800 if (tbl->pdestructor)
801 tbl->pdestructor(n);
802 if (n->dev)
803 dev_put(n->dev);
804 kfree(n);
805 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 return -ENOENT;
807}
808
Denis V. Lunev06f05112008-01-24 00:30:58 -0800809static void neigh_parms_destroy(struct neigh_parms *parms);
810
811static inline void neigh_parms_put(struct neigh_parms *parms)
812{
Reshetova, Elena63439442017-06-30 13:07:56 +0300813 if (refcount_dec_and_test(&parms->refcnt))
Denis V. Lunev06f05112008-01-24 00:30:58 -0800814 neigh_parms_destroy(parms);
815}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816
817/*
818 * neighbour must already be out of the table;
819 *
820 */
821void neigh_destroy(struct neighbour *neigh)
822{
David Millerda6a8fa2011-07-25 00:01:38 +0000823 struct net_device *dev = neigh->dev;
824
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 NEIGH_CACHE_STAT_INC(neigh->tbl, destroys);
826
827 if (!neigh->dead) {
Joe Perchese005d192012-05-16 19:58:40 +0000828 pr_warn("Destroying alive neighbour %p\n", neigh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 dump_stack();
830 return;
831 }
832
833 if (neigh_del_timer(neigh))
Joe Perchese005d192012-05-16 19:58:40 +0000834 pr_warn("Impossible event\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835
Eric Dumazetc9ab4d82013-06-28 02:37:42 -0700836 write_lock_bh(&neigh->lock);
837 __skb_queue_purge(&neigh->arp_queue);
838 write_unlock_bh(&neigh->lock);
Eric Dumazet8b5c1712011-11-09 12:07:14 +0000839 neigh->arp_queue_len_bytes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840
David S. Miller447f2192011-12-19 15:04:41 -0500841 if (dev->netdev_ops->ndo_neigh_destroy)
Jiri Pirko503eebc2016-07-05 11:27:37 +0200842 dev->netdev_ops->ndo_neigh_destroy(dev, neigh);
David S. Miller447f2192011-12-19 15:04:41 -0500843
David Millerda6a8fa2011-07-25 00:01:38 +0000844 dev_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 neigh_parms_put(neigh->parms);
846
Joe Perchesd5d427c2013-04-15 15:17:19 +0000847 neigh_dbg(2, "neigh %p is destroyed\n", neigh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
849 atomic_dec(&neigh->tbl->entries);
David Miller5b8b0062011-07-25 00:01:22 +0000850 kfree_rcu(neigh, rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851}
YOSHIFUJI Hideaki0a204502008-03-24 18:39:10 +0900852EXPORT_SYMBOL(neigh_destroy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853
854/* Neighbour state is suspicious;
855 disable fast path.
856
857 Called with write_locked neigh.
858 */
859static void neigh_suspect(struct neighbour *neigh)
860{
Joe Perchesd5d427c2013-04-15 15:17:19 +0000861 neigh_dbg(2, "neigh %p is suspected\n", neigh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862
863 neigh->output = neigh->ops->output;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864}
865
866/* Neighbour state is OK;
867 enable fast path.
868
869 Called with write_locked neigh.
870 */
871static void neigh_connect(struct neighbour *neigh)
872{
Joe Perchesd5d427c2013-04-15 15:17:19 +0000873 neigh_dbg(2, "neigh %p is connected\n", neigh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874
875 neigh->output = neigh->ops->connected_output;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876}
877
Eric Dumazete4c4e442009-07-30 03:15:07 +0000878static void neigh_periodic_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879{
Eric Dumazete4c4e442009-07-30 03:15:07 +0000880 struct neigh_table *tbl = container_of(work, struct neigh_table, gc_work.work);
Eric Dumazet767e97e2010-10-06 17:49:21 -0700881 struct neighbour *n;
882 struct neighbour __rcu **np;
Eric Dumazete4c4e442009-07-30 03:15:07 +0000883 unsigned int i;
Eric Dumazetd6bf7812010-10-04 06:15:44 +0000884 struct neigh_hash_table *nht;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885
886 NEIGH_CACHE_STAT_INC(tbl, periodic_gc_runs);
887
Eric Dumazete4c4e442009-07-30 03:15:07 +0000888 write_lock_bh(&tbl->lock);
Eric Dumazetd6bf7812010-10-04 06:15:44 +0000889 nht = rcu_dereference_protected(tbl->nht,
890 lockdep_is_held(&tbl->lock));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891
892 /*
893 * periodically recompute ReachableTime from random function
894 */
895
Eric Dumazete4c4e442009-07-30 03:15:07 +0000896 if (time_after(jiffies, tbl->last_rand + 300 * HZ)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 struct neigh_parms *p;
Eric Dumazete4c4e442009-07-30 03:15:07 +0000898 tbl->last_rand = jiffies;
Nicolas Dichtel75fbfd32014-10-29 19:29:31 +0100899 list_for_each_entry(p, &tbl->parms_list, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 p->reachable_time =
Jiri Pirko1f9248e2013-12-07 19:26:53 +0100901 neigh_rand_reach_time(NEIGH_VAR(p, BASE_REACHABLE_TIME));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 }
903
Duan Jiongfeff9ab2014-02-27 17:14:41 +0800904 if (atomic_read(&tbl->entries) < tbl->gc_thresh1)
905 goto out;
906
David S. Millercd089332011-07-11 01:28:12 -0700907 for (i = 0 ; i < (1 << nht->hash_shift); i++) {
Eric Dumazetd6bf7812010-10-04 06:15:44 +0000908 np = &nht->hash_buckets[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909
Eric Dumazet767e97e2010-10-06 17:49:21 -0700910 while ((n = rcu_dereference_protected(*np,
911 lockdep_is_held(&tbl->lock))) != NULL) {
Eric Dumazete4c4e442009-07-30 03:15:07 +0000912 unsigned int state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913
Eric Dumazete4c4e442009-07-30 03:15:07 +0000914 write_lock(&n->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915
Eric Dumazete4c4e442009-07-30 03:15:07 +0000916 state = n->nud_state;
Roopa Prabhu9ce33e42018-04-24 13:49:34 -0700917 if ((state & (NUD_PERMANENT | NUD_IN_TIMER)) ||
918 (n->flags & NTF_EXT_LEARNED)) {
Eric Dumazete4c4e442009-07-30 03:15:07 +0000919 write_unlock(&n->lock);
920 goto next_elt;
921 }
922
923 if (time_before(n->used, n->confirmed))
924 n->used = n->confirmed;
925
Reshetova, Elena9f237432017-06-30 13:07:55 +0300926 if (refcount_read(&n->refcnt) == 1 &&
Eric Dumazete4c4e442009-07-30 03:15:07 +0000927 (state == NUD_FAILED ||
Jiri Pirko1f9248e2013-12-07 19:26:53 +0100928 time_after(jiffies, n->used + NEIGH_VAR(n->parms, GC_STALETIME)))) {
Eric Dumazete4c4e442009-07-30 03:15:07 +0000929 *np = n->next;
David Ahern58956312018-12-07 12:24:57 -0800930 neigh_mark_dead(n);
Eric Dumazete4c4e442009-07-30 03:15:07 +0000931 write_unlock(&n->lock);
932 neigh_cleanup_and_release(n);
933 continue;
934 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 write_unlock(&n->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936
937next_elt:
Eric Dumazete4c4e442009-07-30 03:15:07 +0000938 np = &n->next;
939 }
940 /*
941 * It's fine to release lock here, even if hash table
942 * grows while we are preempted.
943 */
944 write_unlock_bh(&tbl->lock);
945 cond_resched();
946 write_lock_bh(&tbl->lock);
Michel Machado84338a62012-02-21 16:04:13 -0500947 nht = rcu_dereference_protected(tbl->nht,
948 lockdep_is_held(&tbl->lock));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 }
YOSHIFUJI Hideaki / 吉藤英明27246802013-01-22 05:20:05 +0000950out:
Jiri Pirko1f9248e2013-12-07 19:26:53 +0100951 /* Cycle through all hash buckets every BASE_REACHABLE_TIME/2 ticks.
952 * ARP entry timeouts range from 1/2 BASE_REACHABLE_TIME to 3/2
953 * BASE_REACHABLE_TIME.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 */
viresh kumarf6180022014-01-22 12:23:33 +0530955 queue_delayed_work(system_power_efficient_wq, &tbl->gc_work,
Jiri Pirko1f9248e2013-12-07 19:26:53 +0100956 NEIGH_VAR(&tbl->parms, BASE_REACHABLE_TIME) >> 1);
Eric Dumazete4c4e442009-07-30 03:15:07 +0000957 write_unlock_bh(&tbl->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958}
959
960static __inline__ int neigh_max_probes(struct neighbour *n)
961{
962 struct neigh_parms *p = n->parms;
YOSHIFUJI Hideaki/吉藤英明8da86462015-03-19 22:41:46 +0900963 return NEIGH_VAR(p, UCAST_PROBES) + NEIGH_VAR(p, APP_PROBES) +
964 (n->nud_state & NUD_PROBE ? NEIGH_VAR(p, MCAST_REPROBES) :
965 NEIGH_VAR(p, MCAST_PROBES));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966}
967
Timo Teras5ef12d92009-06-11 04:16:28 -0700968static void neigh_invalidate(struct neighbour *neigh)
Eric Dumazet0a141502010-03-09 19:40:54 +0000969 __releases(neigh->lock)
970 __acquires(neigh->lock)
Timo Teras5ef12d92009-06-11 04:16:28 -0700971{
972 struct sk_buff *skb;
973
974 NEIGH_CACHE_STAT_INC(neigh->tbl, res_failed);
Joe Perchesd5d427c2013-04-15 15:17:19 +0000975 neigh_dbg(2, "neigh %p is failed\n", neigh);
Timo Teras5ef12d92009-06-11 04:16:28 -0700976 neigh->updated = jiffies;
977
978 /* It is very thin place. report_unreachable is very complicated
979 routine. Particularly, it can hit the same neighbour entry!
980
981 So that, we try to be accurate and avoid dead loop. --ANK
982 */
983 while (neigh->nud_state == NUD_FAILED &&
984 (skb = __skb_dequeue(&neigh->arp_queue)) != NULL) {
985 write_unlock(&neigh->lock);
986 neigh->ops->error_report(neigh, skb);
987 write_lock(&neigh->lock);
988 }
Eric Dumazetc9ab4d82013-06-28 02:37:42 -0700989 __skb_queue_purge(&neigh->arp_queue);
Eric Dumazet8b5c1712011-11-09 12:07:14 +0000990 neigh->arp_queue_len_bytes = 0;
Timo Teras5ef12d92009-06-11 04:16:28 -0700991}
992
Eric Dumazetcd28ca02011-08-09 08:15:58 +0000993static void neigh_probe(struct neighbour *neigh)
994 __releases(neigh->lock)
995{
Hannes Frederic Sowa4ed377e2013-09-21 06:32:34 +0200996 struct sk_buff *skb = skb_peek_tail(&neigh->arp_queue);
Eric Dumazetcd28ca02011-08-09 08:15:58 +0000997 /* keep skb alive even if arp_queue overflows */
998 if (skb)
Martin Zhang19125c12015-11-17 20:49:30 +0800999 skb = skb_clone(skb, GFP_ATOMIC);
Eric Dumazetcd28ca02011-08-09 08:15:58 +00001000 write_unlock(&neigh->lock);
Eric Dumazet48481c82017-03-23 12:39:21 -07001001 if (neigh->ops->solicit)
1002 neigh->ops->solicit(neigh, skb);
Eric Dumazetcd28ca02011-08-09 08:15:58 +00001003 atomic_inc(&neigh->probes);
1004 kfree_skb(skb);
1005}
1006
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007/* Called when a timer expires for a neighbour entry. */
1008
Kees Cooke99e88a2017-10-16 14:43:17 -07001009static void neigh_timer_handler(struct timer_list *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010{
1011 unsigned long now, next;
Kees Cooke99e88a2017-10-16 14:43:17 -07001012 struct neighbour *neigh = from_timer(neigh, t, timer);
Eric Dumazet95c96172012-04-15 05:58:06 +00001013 unsigned int state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 int notify = 0;
1015
1016 write_lock(&neigh->lock);
1017
1018 state = neigh->nud_state;
1019 now = jiffies;
1020 next = now + HZ;
1021
David S. Miller045f7b32011-11-01 17:45:55 -04001022 if (!(state & NUD_IN_TIMER))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024
1025 if (state & NUD_REACHABLE) {
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001026 if (time_before_eq(now,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 neigh->confirmed + neigh->parms->reachable_time)) {
Joe Perchesd5d427c2013-04-15 15:17:19 +00001028 neigh_dbg(2, "neigh %p is still alive\n", neigh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 next = neigh->confirmed + neigh->parms->reachable_time;
1030 } else if (time_before_eq(now,
Jiri Pirko1f9248e2013-12-07 19:26:53 +01001031 neigh->used +
1032 NEIGH_VAR(neigh->parms, DELAY_PROBE_TIME))) {
Joe Perchesd5d427c2013-04-15 15:17:19 +00001033 neigh_dbg(2, "neigh %p is delayed\n", neigh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 neigh->nud_state = NUD_DELAY;
YOSHIFUJI Hideaki955aaa22006-03-20 16:52:52 -08001035 neigh->updated = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 neigh_suspect(neigh);
Jiri Pirko1f9248e2013-12-07 19:26:53 +01001037 next = now + NEIGH_VAR(neigh->parms, DELAY_PROBE_TIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 } else {
Joe Perchesd5d427c2013-04-15 15:17:19 +00001039 neigh_dbg(2, "neigh %p is suspected\n", neigh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 neigh->nud_state = NUD_STALE;
YOSHIFUJI Hideaki955aaa22006-03-20 16:52:52 -08001041 neigh->updated = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 neigh_suspect(neigh);
Tom Tucker8d717402006-07-30 20:43:36 -07001043 notify = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 }
1045 } else if (state & NUD_DELAY) {
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001046 if (time_before_eq(now,
Jiri Pirko1f9248e2013-12-07 19:26:53 +01001047 neigh->confirmed +
1048 NEIGH_VAR(neigh->parms, DELAY_PROBE_TIME))) {
Joe Perchesd5d427c2013-04-15 15:17:19 +00001049 neigh_dbg(2, "neigh %p is now reachable\n", neigh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 neigh->nud_state = NUD_REACHABLE;
YOSHIFUJI Hideaki955aaa22006-03-20 16:52:52 -08001051 neigh->updated = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 neigh_connect(neigh);
Tom Tucker8d717402006-07-30 20:43:36 -07001053 notify = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 next = neigh->confirmed + neigh->parms->reachable_time;
1055 } else {
Joe Perchesd5d427c2013-04-15 15:17:19 +00001056 neigh_dbg(2, "neigh %p is probed\n", neigh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 neigh->nud_state = NUD_PROBE;
YOSHIFUJI Hideaki955aaa22006-03-20 16:52:52 -08001058 neigh->updated = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 atomic_set(&neigh->probes, 0);
Erik Kline765c9c62015-05-18 19:44:41 +09001060 notify = 1;
Jiri Pirko1f9248e2013-12-07 19:26:53 +01001061 next = now + NEIGH_VAR(neigh->parms, RETRANS_TIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 }
1063 } else {
1064 /* NUD_PROBE|NUD_INCOMPLETE */
Jiri Pirko1f9248e2013-12-07 19:26:53 +01001065 next = now + NEIGH_VAR(neigh->parms, RETRANS_TIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 }
1067
1068 if ((neigh->nud_state & (NUD_INCOMPLETE | NUD_PROBE)) &&
1069 atomic_read(&neigh->probes) >= neigh_max_probes(neigh)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 neigh->nud_state = NUD_FAILED;
1071 notify = 1;
Timo Teras5ef12d92009-06-11 04:16:28 -07001072 neigh_invalidate(neigh);
Duan Jiong5e2c21d2014-02-27 17:03:03 +08001073 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 }
1075
1076 if (neigh->nud_state & NUD_IN_TIMER) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 if (time_before(next, jiffies + HZ/2))
1078 next = jiffies + HZ/2;
Herbert Xu6fb99742005-10-23 16:37:48 +10001079 if (!mod_timer(&neigh->timer, next))
1080 neigh_hold(neigh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 }
1082 if (neigh->nud_state & (NUD_INCOMPLETE | NUD_PROBE)) {
Eric Dumazetcd28ca02011-08-09 08:15:58 +00001083 neigh_probe(neigh);
David S. Miller9ff56602008-02-17 18:39:54 -08001084 } else {
David S. Miller69cc64d2008-02-11 21:45:44 -08001085out:
David S. Miller9ff56602008-02-17 18:39:54 -08001086 write_unlock(&neigh->lock);
1087 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088
Thomas Grafd961db32007-08-08 23:12:56 -07001089 if (notify)
Roopa Prabhu7b8f7a42017-03-19 22:01:28 -07001090 neigh_update_notify(neigh, 0);
Thomas Grafd961db32007-08-08 23:12:56 -07001091
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 neigh_release(neigh);
1093}
1094
1095int __neigh_event_send(struct neighbour *neigh, struct sk_buff *skb)
1096{
1097 int rc;
Eric Dumazetcd28ca02011-08-09 08:15:58 +00001098 bool immediate_probe = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099
1100 write_lock_bh(&neigh->lock);
1101
1102 rc = 0;
1103 if (neigh->nud_state & (NUD_CONNECTED | NUD_DELAY | NUD_PROBE))
1104 goto out_unlock_bh;
Julian Anastasov2c51a972015-06-16 22:56:39 +03001105 if (neigh->dead)
1106 goto out_dead;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 if (!(neigh->nud_state & (NUD_STALE | NUD_INCOMPLETE))) {
Jiri Pirko1f9248e2013-12-07 19:26:53 +01001109 if (NEIGH_VAR(neigh->parms, MCAST_PROBES) +
1110 NEIGH_VAR(neigh->parms, APP_PROBES)) {
Eric Dumazetcd28ca02011-08-09 08:15:58 +00001111 unsigned long next, now = jiffies;
1112
Jiri Pirko1f9248e2013-12-07 19:26:53 +01001113 atomic_set(&neigh->probes,
1114 NEIGH_VAR(neigh->parms, UCAST_PROBES));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 neigh->nud_state = NUD_INCOMPLETE;
Eric Dumazetcd28ca02011-08-09 08:15:58 +00001116 neigh->updated = now;
Jiri Pirko1f9248e2013-12-07 19:26:53 +01001117 next = now + max(NEIGH_VAR(neigh->parms, RETRANS_TIME),
1118 HZ/2);
Eric Dumazetcd28ca02011-08-09 08:15:58 +00001119 neigh_add_timer(neigh, next);
1120 immediate_probe = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 } else {
1122 neigh->nud_state = NUD_FAILED;
YOSHIFUJI Hideaki955aaa22006-03-20 16:52:52 -08001123 neigh->updated = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 write_unlock_bh(&neigh->lock);
1125
Wei Yongjunf3fbbe02009-02-25 00:37:32 +00001126 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 return 1;
1128 }
1129 } else if (neigh->nud_state & NUD_STALE) {
Joe Perchesd5d427c2013-04-15 15:17:19 +00001130 neigh_dbg(2, "neigh %p is delayed\n", neigh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 neigh->nud_state = NUD_DELAY;
YOSHIFUJI Hideaki955aaa22006-03-20 16:52:52 -08001132 neigh->updated = jiffies;
Jiri Pirko1f9248e2013-12-07 19:26:53 +01001133 neigh_add_timer(neigh, jiffies +
1134 NEIGH_VAR(neigh->parms, DELAY_PROBE_TIME));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 }
1136
1137 if (neigh->nud_state == NUD_INCOMPLETE) {
1138 if (skb) {
Eric Dumazet8b5c1712011-11-09 12:07:14 +00001139 while (neigh->arp_queue_len_bytes + skb->truesize >
Jiri Pirko1f9248e2013-12-07 19:26:53 +01001140 NEIGH_VAR(neigh->parms, QUEUE_LEN_BYTES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 struct sk_buff *buff;
Eric Dumazet8b5c1712011-11-09 12:07:14 +00001142
David S. Millerf72051b2008-09-23 01:11:18 -07001143 buff = __skb_dequeue(&neigh->arp_queue);
Eric Dumazet8b5c1712011-11-09 12:07:14 +00001144 if (!buff)
1145 break;
1146 neigh->arp_queue_len_bytes -= buff->truesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 kfree_skb(buff);
Neil Horman9a6d2762008-07-16 20:50:49 -07001148 NEIGH_CACHE_STAT_INC(neigh->tbl, unres_discards);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 }
Eric Dumazeta4731132010-05-27 16:09:39 -07001150 skb_dst_force(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 __skb_queue_tail(&neigh->arp_queue, skb);
Eric Dumazet8b5c1712011-11-09 12:07:14 +00001152 neigh->arp_queue_len_bytes += skb->truesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 }
1154 rc = 1;
1155 }
1156out_unlock_bh:
Eric Dumazetcd28ca02011-08-09 08:15:58 +00001157 if (immediate_probe)
1158 neigh_probe(neigh);
1159 else
1160 write_unlock(&neigh->lock);
1161 local_bh_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 return rc;
Julian Anastasov2c51a972015-06-16 22:56:39 +03001163
1164out_dead:
1165 if (neigh->nud_state & NUD_STALE)
1166 goto out_unlock_bh;
1167 write_unlock_bh(&neigh->lock);
1168 kfree_skb(skb);
1169 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170}
YOSHIFUJI Hideaki0a204502008-03-24 18:39:10 +09001171EXPORT_SYMBOL(__neigh_event_send);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172
David S. Millerf6b72b622011-07-14 07:53:20 -07001173static void neigh_update_hhs(struct neighbour *neigh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174{
1175 struct hh_cache *hh;
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -07001176 void (*update)(struct hh_cache*, const struct net_device*, const unsigned char *)
Doug Kehn91a72a72010-07-14 18:02:16 -07001177 = NULL;
1178
1179 if (neigh->dev->header_ops)
1180 update = neigh->dev->header_ops->cache_update;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181
1182 if (update) {
David S. Millerf6b72b622011-07-14 07:53:20 -07001183 hh = &neigh->hh;
1184 if (hh->hh_len) {
Stephen Hemminger3644f0c2006-12-07 15:08:17 -08001185 write_seqlock_bh(&hh->hh_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 update(hh, neigh->dev, neigh->ha);
Stephen Hemminger3644f0c2006-12-07 15:08:17 -08001187 write_sequnlock_bh(&hh->hh_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 }
1189 }
1190}
1191
1192
1193
1194/* Generic update routine.
1195 -- lladdr is new lladdr or NULL, if it is not supplied.
1196 -- new is new state.
1197 -- flags
1198 NEIGH_UPDATE_F_OVERRIDE allows to override existing lladdr,
1199 if it is different.
1200 NEIGH_UPDATE_F_WEAK_OVERRIDE will suspect existing "connected"
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001201 lladdr instead of overriding it
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 if it is different.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 NEIGH_UPDATE_F_ADMIN means that the change is administrative.
1204
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001205 NEIGH_UPDATE_F_OVERRIDE_ISROUTER allows to override existing
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 NTF_ROUTER flag.
1207 NEIGH_UPDATE_F_ISROUTER indicates if the neighbour is known as
1208 a router.
1209
1210 Caller MUST hold reference count on the entry.
1211 */
1212
David Ahern7a35a502018-12-05 20:02:29 -08001213static int __neigh_update(struct neighbour *neigh, const u8 *lladdr,
1214 u8 new, u32 flags, u32 nlmsg_pid,
1215 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216{
David Aherne997f8a2018-12-11 18:57:25 -07001217 bool ext_learn_change = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 u8 old;
1219 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 int notify = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 struct net_device *dev;
1222 int update_isrouter = 0;
1223
1224 write_lock_bh(&neigh->lock);
1225
1226 dev = neigh->dev;
1227 old = neigh->nud_state;
1228 err = -EPERM;
1229
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001230 if (!(flags & NEIGH_UPDATE_F_ADMIN) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 (old & (NUD_NOARP | NUD_PERMANENT)))
1232 goto out;
David Ahern7a35a502018-12-05 20:02:29 -08001233 if (neigh->dead) {
1234 NL_SET_ERR_MSG(extack, "Neighbor entry is now dead");
Julian Anastasov2c51a972015-06-16 22:56:39 +03001235 goto out;
David Ahern7a35a502018-12-05 20:02:29 -08001236 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237
David Aherne997f8a2018-12-11 18:57:25 -07001238 ext_learn_change = neigh_update_ext_learned(neigh, flags, &notify);
Roopa Prabhu9ce33e42018-04-24 13:49:34 -07001239
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 if (!(new & NUD_VALID)) {
1241 neigh_del_timer(neigh);
1242 if (old & NUD_CONNECTED)
1243 neigh_suspect(neigh);
David Ahern9c29a2f2018-12-11 18:57:21 -07001244 neigh->nud_state = new;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 notify = old & NUD_VALID;
Roopa Prabhud2fb4fb2018-10-20 18:09:31 -07001247 if ((old & (NUD_INCOMPLETE | NUD_PROBE)) &&
Timo Teras5ef12d92009-06-11 04:16:28 -07001248 (new & NUD_FAILED)) {
1249 neigh_invalidate(neigh);
1250 notify = 1;
1251 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 goto out;
1253 }
1254
1255 /* Compare new lladdr with cached one */
1256 if (!dev->addr_len) {
1257 /* First case: device needs no address. */
1258 lladdr = neigh->ha;
1259 } else if (lladdr) {
1260 /* The second case: if something is already cached
1261 and a new address is proposed:
1262 - compare new & old
1263 - if they are different, check override flag
1264 */
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001265 if ((old & NUD_VALID) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 !memcmp(lladdr, neigh->ha, dev->addr_len))
1267 lladdr = neigh->ha;
1268 } else {
1269 /* No address is supplied; if we know something,
1270 use it, otherwise discard the request.
1271 */
1272 err = -EINVAL;
David Ahern7a35a502018-12-05 20:02:29 -08001273 if (!(old & NUD_VALID)) {
1274 NL_SET_ERR_MSG(extack, "No link layer address given");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 goto out;
David Ahern7a35a502018-12-05 20:02:29 -08001276 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 lladdr = neigh->ha;
1278 }
1279
Vasily Khoruzhickf0e0d042018-09-13 11:12:03 -07001280 /* Update confirmed timestamp for neighbour entry after we
1281 * received ARP packet even if it doesn't change IP to MAC binding.
1282 */
1283 if (new & NUD_CONNECTED)
1284 neigh->confirmed = jiffies;
1285
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 /* If entry was valid and address is not changed,
1287 do not change entry state, if new one is STALE.
1288 */
1289 err = 0;
1290 update_isrouter = flags & NEIGH_UPDATE_F_OVERRIDE_ISROUTER;
1291 if (old & NUD_VALID) {
1292 if (lladdr != neigh->ha && !(flags & NEIGH_UPDATE_F_OVERRIDE)) {
1293 update_isrouter = 0;
1294 if ((flags & NEIGH_UPDATE_F_WEAK_OVERRIDE) &&
1295 (old & NUD_CONNECTED)) {
1296 lladdr = neigh->ha;
1297 new = NUD_STALE;
1298 } else
1299 goto out;
1300 } else {
Julian Anastasov0e7bbcc2016-07-27 09:56:50 +03001301 if (lladdr == neigh->ha && new == NUD_STALE &&
1302 !(flags & NEIGH_UPDATE_F_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 new = old;
1304 }
1305 }
1306
Vasily Khoruzhickf0e0d042018-09-13 11:12:03 -07001307 /* Update timestamp only once we know we will make a change to the
Ihar Hrachyshka77d71232017-05-16 08:44:24 -07001308 * neighbour entry. Otherwise we risk to move the locktime window with
1309 * noop updates and ignore relevant ARP updates.
1310 */
Vasily Khoruzhickf0e0d042018-09-13 11:12:03 -07001311 if (new != old || lladdr != neigh->ha)
Ihar Hrachyshka77d71232017-05-16 08:44:24 -07001312 neigh->updated = jiffies;
Ihar Hrachyshka77d71232017-05-16 08:44:24 -07001313
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 if (new != old) {
1315 neigh_del_timer(neigh);
Erik Kline765c9c62015-05-18 19:44:41 +09001316 if (new & NUD_PROBE)
1317 atomic_set(&neigh->probes, 0);
Pavel Emelyanova43d8992007-12-20 15:49:05 -08001318 if (new & NUD_IN_TIMER)
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001319 neigh_add_timer(neigh, (jiffies +
1320 ((new & NUD_REACHABLE) ?
David S. Miller667347f2005-09-27 12:07:44 -07001321 neigh->parms->reachable_time :
1322 0)));
David Ahern9c29a2f2018-12-11 18:57:21 -07001323 neigh->nud_state = new;
Bob Gilligan53385d22013-12-15 13:39:56 -08001324 notify = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 }
1326
1327 if (lladdr != neigh->ha) {
Eric Dumazet0ed8ddf2010-10-07 10:44:07 +00001328 write_seqlock(&neigh->ha_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 memcpy(&neigh->ha, lladdr, dev->addr_len);
Eric Dumazet0ed8ddf2010-10-07 10:44:07 +00001330 write_sequnlock(&neigh->ha_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 neigh_update_hhs(neigh);
1332 if (!(new & NUD_CONNECTED))
1333 neigh->confirmed = jiffies -
Jiri Pirko1f9248e2013-12-07 19:26:53 +01001334 (NEIGH_VAR(neigh->parms, BASE_REACHABLE_TIME) << 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 notify = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 }
1337 if (new == old)
1338 goto out;
1339 if (new & NUD_CONNECTED)
1340 neigh_connect(neigh);
1341 else
1342 neigh_suspect(neigh);
1343 if (!(old & NUD_VALID)) {
1344 struct sk_buff *skb;
1345
1346 /* Again: avoid dead loop if something went wrong */
1347
1348 while (neigh->nud_state & NUD_VALID &&
1349 (skb = __skb_dequeue(&neigh->arp_queue)) != NULL) {
David S. Miller69cce1d2011-07-17 23:09:49 -07001350 struct dst_entry *dst = skb_dst(skb);
1351 struct neighbour *n2, *n1 = neigh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 write_unlock_bh(&neigh->lock);
roy.qing.li@gmail.come049f282011-10-17 22:32:42 +00001353
1354 rcu_read_lock();
David S. Miller13a43d92012-07-02 22:15:37 -07001355
1356 /* Why not just use 'neigh' as-is? The problem is that
1357 * things such as shaper, eql, and sch_teql can end up
1358 * using alternative, different, neigh objects to output
1359 * the packet in the output path. So what we need to do
1360 * here is re-lookup the top-level neigh in the path so
1361 * we can reinject the packet there.
1362 */
1363 n2 = NULL;
1364 if (dst) {
1365 n2 = dst_neigh_lookup_skb(dst, skb);
1366 if (n2)
1367 n1 = n2;
1368 }
David S. Miller8f40b162011-07-17 13:34:11 -07001369 n1->output(n1, skb);
David S. Miller13a43d92012-07-02 22:15:37 -07001370 if (n2)
1371 neigh_release(n2);
roy.qing.li@gmail.come049f282011-10-17 22:32:42 +00001372 rcu_read_unlock();
1373
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 write_lock_bh(&neigh->lock);
1375 }
Eric Dumazetc9ab4d82013-06-28 02:37:42 -07001376 __skb_queue_purge(&neigh->arp_queue);
Eric Dumazet8b5c1712011-11-09 12:07:14 +00001377 neigh->arp_queue_len_bytes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 }
1379out:
Roopa Prabhufc6e8072018-09-22 21:26:20 -07001380 if (update_isrouter)
1381 neigh_update_is_router(neigh, flags, &notify);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 write_unlock_bh(&neigh->lock);
Tom Tucker8d717402006-07-30 20:43:36 -07001383
David Aherne997f8a2018-12-11 18:57:25 -07001384 if (((new ^ old) & NUD_PERMANENT) || ext_learn_change)
David Ahern9c29a2f2018-12-11 18:57:21 -07001385 neigh_update_gc_list(neigh);
1386
Tom Tucker8d717402006-07-30 20:43:36 -07001387 if (notify)
Roopa Prabhu7b8f7a42017-03-19 22:01:28 -07001388 neigh_update_notify(neigh, nlmsg_pid);
Thomas Grafd961db32007-08-08 23:12:56 -07001389
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 return err;
1391}
David Ahern7a35a502018-12-05 20:02:29 -08001392
1393int neigh_update(struct neighbour *neigh, const u8 *lladdr, u8 new,
1394 u32 flags, u32 nlmsg_pid)
1395{
1396 return __neigh_update(neigh, lladdr, new, flags, nlmsg_pid, NULL);
1397}
YOSHIFUJI Hideaki0a204502008-03-24 18:39:10 +09001398EXPORT_SYMBOL(neigh_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399
Jiri Benc7e980562013-12-11 13:48:20 +01001400/* Update the neigh to listen temporarily for probe responses, even if it is
1401 * in a NUD_FAILED state. The caller has to hold neigh->lock for writing.
1402 */
1403void __neigh_set_probe_once(struct neighbour *neigh)
1404{
Julian Anastasov2c51a972015-06-16 22:56:39 +03001405 if (neigh->dead)
1406 return;
Jiri Benc7e980562013-12-11 13:48:20 +01001407 neigh->updated = jiffies;
1408 if (!(neigh->nud_state & NUD_FAILED))
1409 return;
Duan Jiong2176d5d2014-05-09 13:16:48 +08001410 neigh->nud_state = NUD_INCOMPLETE;
1411 atomic_set(&neigh->probes, neigh_max_probes(neigh));
Jiri Benc7e980562013-12-11 13:48:20 +01001412 neigh_add_timer(neigh,
1413 jiffies + NEIGH_VAR(neigh->parms, RETRANS_TIME));
1414}
1415EXPORT_SYMBOL(__neigh_set_probe_once);
1416
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417struct neighbour *neigh_event_ns(struct neigh_table *tbl,
1418 u8 *lladdr, void *saddr,
1419 struct net_device *dev)
1420{
1421 struct neighbour *neigh = __neigh_lookup(tbl, saddr, dev,
1422 lladdr || !dev->addr_len);
1423 if (neigh)
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001424 neigh_update(neigh, lladdr, NUD_STALE,
Roopa Prabhu7b8f7a42017-03-19 22:01:28 -07001425 NEIGH_UPDATE_F_OVERRIDE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 return neigh;
1427}
YOSHIFUJI Hideaki0a204502008-03-24 18:39:10 +09001428EXPORT_SYMBOL(neigh_event_ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429
Eric Dumazet34d101d2010-10-11 09:16:57 -07001430/* called with read_lock_bh(&n->lock); */
Eric W. Biedermanbdf53c52015-03-02 00:13:22 -06001431static void neigh_hh_init(struct neighbour *n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432{
Eric W. Biedermanbdf53c52015-03-02 00:13:22 -06001433 struct net_device *dev = n->dev;
1434 __be16 prot = n->tbl->protocol;
David S. Millerf6b72b622011-07-14 07:53:20 -07001435 struct hh_cache *hh = &n->hh;
Eric Dumazet0ed8ddf2010-10-07 10:44:07 +00001436
1437 write_lock_bh(&n->lock);
Eric Dumazet34d101d2010-10-11 09:16:57 -07001438
David S. Millerf6b72b622011-07-14 07:53:20 -07001439 /* Only one thread can come in here and initialize the
1440 * hh_cache entry.
1441 */
David S. Millerb23b5452011-07-16 17:45:02 -07001442 if (!hh->hh_len)
1443 dev->header_ops->cache(n, hh, prot);
David S. Millerf6b72b622011-07-14 07:53:20 -07001444
Eric Dumazet0ed8ddf2010-10-07 10:44:07 +00001445 write_unlock_bh(&n->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446}
1447
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448/* Slow and careful. */
1449
David S. Miller8f40b162011-07-17 13:34:11 -07001450int neigh_resolve_output(struct neighbour *neigh, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 int rc = 0;
1453
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 if (!neigh_event_send(neigh, skb)) {
1455 int err;
1456 struct net_device *dev = neigh->dev;
Eric Dumazet0ed8ddf2010-10-07 10:44:07 +00001457 unsigned int seq;
Eric Dumazet34d101d2010-10-11 09:16:57 -07001458
David S. Millerf6b72b622011-07-14 07:53:20 -07001459 if (dev->header_ops->cache && !neigh->hh.hh_len)
Eric W. Biedermanbdf53c52015-03-02 00:13:22 -06001460 neigh_hh_init(neigh);
Eric Dumazet34d101d2010-10-11 09:16:57 -07001461
Eric Dumazet0ed8ddf2010-10-07 10:44:07 +00001462 do {
ramesh.nagappa@gmail.come1f16502012-10-05 19:10:15 +00001463 __skb_pull(skb, skb_network_offset(skb));
Eric Dumazet0ed8ddf2010-10-07 10:44:07 +00001464 seq = read_seqbegin(&neigh->ha_lock);
1465 err = dev_hard_header(skb, dev, ntohs(skb->protocol),
1466 neigh->ha, NULL, skb->len);
1467 } while (read_seqretry(&neigh->ha_lock, seq));
Eric Dumazet34d101d2010-10-11 09:16:57 -07001468
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 if (err >= 0)
David S. Miller542d4d62011-07-16 18:06:24 -07001470 rc = dev_queue_xmit(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 else
1472 goto out_kfree_skb;
1473 }
1474out:
1475 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476out_kfree_skb:
1477 rc = -EINVAL;
1478 kfree_skb(skb);
1479 goto out;
1480}
YOSHIFUJI Hideaki0a204502008-03-24 18:39:10 +09001481EXPORT_SYMBOL(neigh_resolve_output);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482
1483/* As fast as possible without hh cache */
1484
David S. Miller8f40b162011-07-17 13:34:11 -07001485int neigh_connected_output(struct neighbour *neigh, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 struct net_device *dev = neigh->dev;
Eric Dumazet0ed8ddf2010-10-07 10:44:07 +00001488 unsigned int seq;
David S. Miller8f40b162011-07-17 13:34:11 -07001489 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490
Eric Dumazet0ed8ddf2010-10-07 10:44:07 +00001491 do {
ramesh.nagappa@gmail.come1f16502012-10-05 19:10:15 +00001492 __skb_pull(skb, skb_network_offset(skb));
Eric Dumazet0ed8ddf2010-10-07 10:44:07 +00001493 seq = read_seqbegin(&neigh->ha_lock);
1494 err = dev_hard_header(skb, dev, ntohs(skb->protocol),
1495 neigh->ha, NULL, skb->len);
1496 } while (read_seqretry(&neigh->ha_lock, seq));
1497
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 if (err >= 0)
David S. Miller542d4d62011-07-16 18:06:24 -07001499 err = dev_queue_xmit(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 else {
1501 err = -EINVAL;
1502 kfree_skb(skb);
1503 }
1504 return err;
1505}
YOSHIFUJI Hideaki0a204502008-03-24 18:39:10 +09001506EXPORT_SYMBOL(neigh_connected_output);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507
David S. Miller8f40b162011-07-17 13:34:11 -07001508int neigh_direct_output(struct neighbour *neigh, struct sk_buff *skb)
1509{
1510 return dev_queue_xmit(skb);
1511}
1512EXPORT_SYMBOL(neigh_direct_output);
1513
Kees Cooke99e88a2017-10-16 14:43:17 -07001514static void neigh_proxy_process(struct timer_list *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515{
Kees Cooke99e88a2017-10-16 14:43:17 -07001516 struct neigh_table *tbl = from_timer(tbl, t, proxy_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 long sched_next = 0;
1518 unsigned long now = jiffies;
David S. Millerf72051b2008-09-23 01:11:18 -07001519 struct sk_buff *skb, *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520
1521 spin_lock(&tbl->proxy_queue.lock);
1522
David S. Millerf72051b2008-09-23 01:11:18 -07001523 skb_queue_walk_safe(&tbl->proxy_queue, skb, n) {
1524 long tdif = NEIGH_CB(skb)->sched_next - now;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 if (tdif <= 0) {
David S. Millerf72051b2008-09-23 01:11:18 -07001527 struct net_device *dev = skb->dev;
Eric Dumazet20e60742011-08-22 19:32:42 +00001528
David S. Millerf72051b2008-09-23 01:11:18 -07001529 __skb_unlink(skb, &tbl->proxy_queue);
Eric Dumazet20e60742011-08-22 19:32:42 +00001530 if (tbl->proxy_redo && netif_running(dev)) {
1531 rcu_read_lock();
David S. Millerf72051b2008-09-23 01:11:18 -07001532 tbl->proxy_redo(skb);
Eric Dumazet20e60742011-08-22 19:32:42 +00001533 rcu_read_unlock();
1534 } else {
David S. Millerf72051b2008-09-23 01:11:18 -07001535 kfree_skb(skb);
Eric Dumazet20e60742011-08-22 19:32:42 +00001536 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537
1538 dev_put(dev);
1539 } else if (!sched_next || tdif < sched_next)
1540 sched_next = tdif;
1541 }
1542 del_timer(&tbl->proxy_timer);
1543 if (sched_next)
1544 mod_timer(&tbl->proxy_timer, jiffies + sched_next);
1545 spin_unlock(&tbl->proxy_queue.lock);
1546}
1547
1548void pneigh_enqueue(struct neigh_table *tbl, struct neigh_parms *p,
1549 struct sk_buff *skb)
1550{
1551 unsigned long now = jiffies;
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -05001552
1553 unsigned long sched_next = now + (prandom_u32() %
Jiri Pirko1f9248e2013-12-07 19:26:53 +01001554 NEIGH_VAR(p, PROXY_DELAY));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555
Jiri Pirko1f9248e2013-12-07 19:26:53 +01001556 if (tbl->proxy_queue.qlen > NEIGH_VAR(p, PROXY_QLEN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 kfree_skb(skb);
1558 return;
1559 }
Patrick McHardya61bbcf2005-08-14 17:24:31 -07001560
1561 NEIGH_CB(skb)->sched_next = sched_next;
1562 NEIGH_CB(skb)->flags |= LOCALLY_ENQUEUED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563
1564 spin_lock(&tbl->proxy_queue.lock);
1565 if (del_timer(&tbl->proxy_timer)) {
1566 if (time_before(tbl->proxy_timer.expires, sched_next))
1567 sched_next = tbl->proxy_timer.expires;
1568 }
Eric Dumazetadf30902009-06-02 05:19:30 +00001569 skb_dst_drop(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 dev_hold(skb->dev);
1571 __skb_queue_tail(&tbl->proxy_queue, skb);
1572 mod_timer(&tbl->proxy_timer, sched_next);
1573 spin_unlock(&tbl->proxy_queue.lock);
1574}
YOSHIFUJI Hideaki0a204502008-03-24 18:39:10 +09001575EXPORT_SYMBOL(pneigh_enqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576
Tobias Klauser97fd5bc2009-07-13 11:17:49 -07001577static inline struct neigh_parms *lookup_neigh_parms(struct neigh_table *tbl,
Eric W. Biederman426b5302008-01-24 00:13:18 -08001578 struct net *net, int ifindex)
1579{
1580 struct neigh_parms *p;
1581
Nicolas Dichtel75fbfd32014-10-29 19:29:31 +01001582 list_for_each_entry(p, &tbl->parms_list, list) {
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09001583 if ((p->dev && p->dev->ifindex == ifindex && net_eq(neigh_parms_net(p), net)) ||
Gao feng170d6f92013-06-20 10:01:33 +08001584 (!p->dev && !ifindex && net_eq(net, &init_net)))
Eric W. Biederman426b5302008-01-24 00:13:18 -08001585 return p;
1586 }
1587
1588 return NULL;
1589}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590
1591struct neigh_parms *neigh_parms_alloc(struct net_device *dev,
1592 struct neigh_table *tbl)
1593{
Gao fengcf89d6b2013-06-20 10:01:32 +08001594 struct neigh_parms *p;
Stephen Hemminger00829822008-11-20 20:14:53 -08001595 struct net *net = dev_net(dev);
1596 const struct net_device_ops *ops = dev->netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597
Gao fengcf89d6b2013-06-20 10:01:32 +08001598 p = kmemdup(&tbl->parms, sizeof(*p), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 if (p) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 p->tbl = tbl;
Reshetova, Elena63439442017-06-30 13:07:56 +03001601 refcount_set(&p->refcnt, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 p->reachable_time =
Jiri Pirko1f9248e2013-12-07 19:26:53 +01001603 neigh_rand_reach_time(NEIGH_VAR(p, BASE_REACHABLE_TIME));
Denis V. Lunev486b51d2008-01-14 22:59:59 -08001604 dev_hold(dev);
1605 p->dev = dev;
Eric W. Biedermanefd7ef12015-03-11 23:04:08 -05001606 write_pnet(&p->net, net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 p->sysctl_table = NULL;
Veaceslav Falico63134802013-08-02 19:07:38 +02001608
1609 if (ops->ndo_neigh_setup && ops->ndo_neigh_setup(dev, p)) {
Veaceslav Falico63134802013-08-02 19:07:38 +02001610 dev_put(dev);
1611 kfree(p);
1612 return NULL;
1613 }
1614
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 write_lock_bh(&tbl->lock);
Nicolas Dichtel75fbfd32014-10-29 19:29:31 +01001616 list_add(&p->list, &tbl->parms.list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 write_unlock_bh(&tbl->lock);
Jiri Pirko1d4c8c22013-12-07 19:26:56 +01001618
1619 neigh_parms_data_state_cleanall(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 }
1621 return p;
1622}
YOSHIFUJI Hideaki0a204502008-03-24 18:39:10 +09001623EXPORT_SYMBOL(neigh_parms_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624
1625static void neigh_rcu_free_parms(struct rcu_head *head)
1626{
1627 struct neigh_parms *parms =
1628 container_of(head, struct neigh_parms, rcu_head);
1629
1630 neigh_parms_put(parms);
1631}
1632
1633void neigh_parms_release(struct neigh_table *tbl, struct neigh_parms *parms)
1634{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 if (!parms || parms == &tbl->parms)
1636 return;
1637 write_lock_bh(&tbl->lock);
Nicolas Dichtel75fbfd32014-10-29 19:29:31 +01001638 list_del(&parms->list);
1639 parms->dead = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 write_unlock_bh(&tbl->lock);
Nicolas Dichtel75fbfd32014-10-29 19:29:31 +01001641 if (parms->dev)
1642 dev_put(parms->dev);
1643 call_rcu(&parms->rcu_head, neigh_rcu_free_parms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644}
YOSHIFUJI Hideaki0a204502008-03-24 18:39:10 +09001645EXPORT_SYMBOL(neigh_parms_release);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646
Denis V. Lunev06f05112008-01-24 00:30:58 -08001647static void neigh_parms_destroy(struct neigh_parms *parms)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648{
1649 kfree(parms);
1650}
1651
Pavel Emelianovc2ecba72007-04-17 12:45:31 -07001652static struct lock_class_key neigh_table_proxy_queue_class;
1653
WANG Congd7480fd2014-11-10 15:59:36 -08001654static struct neigh_table *neigh_tables[NEIGH_NR_TABLES] __read_mostly;
1655
1656void neigh_table_init(int index, struct neigh_table *tbl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657{
1658 unsigned long now = jiffies;
1659 unsigned long phsize;
1660
Nicolas Dichtel75fbfd32014-10-29 19:29:31 +01001661 INIT_LIST_HEAD(&tbl->parms_list);
David Ahern58956312018-12-07 12:24:57 -08001662 INIT_LIST_HEAD(&tbl->gc_list);
Nicolas Dichtel75fbfd32014-10-29 19:29:31 +01001663 list_add(&tbl->parms.list, &tbl->parms_list);
Eric Dumazete42ea982008-11-12 00:54:54 -08001664 write_pnet(&tbl->parms.net, &init_net);
Reshetova, Elena63439442017-06-30 13:07:56 +03001665 refcount_set(&tbl->parms.refcnt, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 tbl->parms.reachable_time =
Jiri Pirko1f9248e2013-12-07 19:26:53 +01001667 neigh_rand_reach_time(NEIGH_VAR(&tbl->parms, BASE_REACHABLE_TIME));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 tbl->stats = alloc_percpu(struct neigh_statistics);
1670 if (!tbl->stats)
1671 panic("cannot create neighbour cache statistics");
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001672
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673#ifdef CONFIG_PROC_FS
Christoph Hellwig71a50532018-04-15 10:16:41 +02001674 if (!proc_create_seq_data(tbl->id, 0, init_net.proc_net_stat,
1675 &neigh_stat_seq_ops, tbl))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 panic("cannot create neighbour proc dir entry");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677#endif
1678
David S. Millercd089332011-07-11 01:28:12 -07001679 RCU_INIT_POINTER(tbl->nht, neigh_hash_alloc(3));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680
1681 phsize = (PNEIGH_HASHMASK + 1) * sizeof(struct pneigh_entry *);
Andrew Morton77d04bd2006-04-07 14:52:59 -07001682 tbl->phash_buckets = kzalloc(phsize, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683
Eric Dumazetd6bf7812010-10-04 06:15:44 +00001684 if (!tbl->nht || !tbl->phash_buckets)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 panic("cannot allocate neighbour cache hashes");
1686
YOSHIFUJI Hideaki / 吉藤英明08433ef2013-01-24 00:44:23 +00001687 if (!tbl->entry_size)
1688 tbl->entry_size = ALIGN(offsetof(struct neighbour, primary_key) +
1689 tbl->key_len, NEIGH_PRIV_ALIGN);
1690 else
1691 WARN_ON(tbl->entry_size % NEIGH_PRIV_ALIGN);
1692
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 rwlock_init(&tbl->lock);
Tejun Heo203b42f2012-08-21 13:18:23 -07001694 INIT_DEFERRABLE_WORK(&tbl->gc_work, neigh_periodic_work);
viresh kumarf6180022014-01-22 12:23:33 +05301695 queue_delayed_work(system_power_efficient_wq, &tbl->gc_work,
1696 tbl->parms.reachable_time);
Kees Cooke99e88a2017-10-16 14:43:17 -07001697 timer_setup(&tbl->proxy_timer, neigh_proxy_process, 0);
Pavel Emelianovc2ecba72007-04-17 12:45:31 -07001698 skb_queue_head_init_class(&tbl->proxy_queue,
1699 &neigh_table_proxy_queue_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700
1701 tbl->last_flush = now;
1702 tbl->last_rand = now + tbl->parms.reachable_time * 20;
Simon Kelleybd89efc2006-05-12 14:56:08 -07001703
WANG Congd7480fd2014-11-10 15:59:36 -08001704 neigh_tables[index] = tbl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705}
YOSHIFUJI Hideaki0a204502008-03-24 18:39:10 +09001706EXPORT_SYMBOL(neigh_table_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707
WANG Congd7480fd2014-11-10 15:59:36 -08001708int neigh_table_clear(int index, struct neigh_table *tbl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709{
WANG Congd7480fd2014-11-10 15:59:36 -08001710 neigh_tables[index] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 /* It is not clean... Fix it to unload IPv6 module safely */
Tejun Heoa5c30b32010-10-19 06:04:42 +00001712 cancel_delayed_work_sync(&tbl->gc_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 del_timer_sync(&tbl->proxy_timer);
1714 pneigh_queue_purge(&tbl->proxy_queue);
1715 neigh_ifdown(tbl, NULL);
1716 if (atomic_read(&tbl->entries))
Joe Perchese005d192012-05-16 19:58:40 +00001717 pr_crit("neighbour leakage\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718
Eric Dumazet6193d2b2011-01-19 22:02:47 +00001719 call_rcu(&rcu_dereference_protected(tbl->nht, 1)->rcu,
1720 neigh_hash_free_rcu);
Eric Dumazetd6bf7812010-10-04 06:15:44 +00001721 tbl->nht = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722
1723 kfree(tbl->phash_buckets);
1724 tbl->phash_buckets = NULL;
1725
Alexey Dobriyan3f192b52007-11-05 21:28:13 -08001726 remove_proc_entry(tbl->id, init_net.proc_net_stat);
1727
Kirill Korotaev3fcde742006-09-01 01:34:10 -07001728 free_percpu(tbl->stats);
1729 tbl->stats = NULL;
1730
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 return 0;
1732}
YOSHIFUJI Hideaki0a204502008-03-24 18:39:10 +09001733EXPORT_SYMBOL(neigh_table_clear);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734
WANG Congd7480fd2014-11-10 15:59:36 -08001735static struct neigh_table *neigh_find_table(int family)
1736{
1737 struct neigh_table *tbl = NULL;
1738
1739 switch (family) {
1740 case AF_INET:
1741 tbl = neigh_tables[NEIGH_ARP_TABLE];
1742 break;
1743 case AF_INET6:
1744 tbl = neigh_tables[NEIGH_ND_TABLE];
1745 break;
1746 case AF_DECnet:
1747 tbl = neigh_tables[NEIGH_DN_TABLE];
1748 break;
1749 }
1750
1751 return tbl;
1752}
1753
David Ahernc21ef3e2017-04-16 09:48:24 -07001754static int neigh_delete(struct sk_buff *skb, struct nlmsghdr *nlh,
1755 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001757 struct net *net = sock_net(skb->sk);
Thomas Grafa14a49d2006-08-07 17:53:08 -07001758 struct ndmsg *ndm;
1759 struct nlattr *dst_attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 struct neigh_table *tbl;
WANG Congd7480fd2014-11-10 15:59:36 -08001761 struct neighbour *neigh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 struct net_device *dev = NULL;
Thomas Grafa14a49d2006-08-07 17:53:08 -07001763 int err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764
Eric Dumazet110b2492010-10-04 04:27:36 +00001765 ASSERT_RTNL();
Thomas Grafa14a49d2006-08-07 17:53:08 -07001766 if (nlmsg_len(nlh) < sizeof(*ndm))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 goto out;
1768
Thomas Grafa14a49d2006-08-07 17:53:08 -07001769 dst_attr = nlmsg_find_attr(nlh, sizeof(*ndm), NDA_DST);
David Ahern7a35a502018-12-05 20:02:29 -08001770 if (!dst_attr) {
1771 NL_SET_ERR_MSG(extack, "Network address not specified");
Thomas Grafa14a49d2006-08-07 17:53:08 -07001772 goto out;
David Ahern7a35a502018-12-05 20:02:29 -08001773 }
Thomas Grafa14a49d2006-08-07 17:53:08 -07001774
1775 ndm = nlmsg_data(nlh);
1776 if (ndm->ndm_ifindex) {
Eric Dumazet110b2492010-10-04 04:27:36 +00001777 dev = __dev_get_by_index(net, ndm->ndm_ifindex);
Thomas Grafa14a49d2006-08-07 17:53:08 -07001778 if (dev == NULL) {
1779 err = -ENODEV;
1780 goto out;
1781 }
1782 }
1783
WANG Congd7480fd2014-11-10 15:59:36 -08001784 tbl = neigh_find_table(ndm->ndm_family);
1785 if (tbl == NULL)
1786 return -EAFNOSUPPORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787
David Ahern7a35a502018-12-05 20:02:29 -08001788 if (nla_len(dst_attr) < (int)tbl->key_len) {
1789 NL_SET_ERR_MSG(extack, "Invalid network address");
WANG Congd7480fd2014-11-10 15:59:36 -08001790 goto out;
David Ahern7a35a502018-12-05 20:02:29 -08001791 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792
WANG Congd7480fd2014-11-10 15:59:36 -08001793 if (ndm->ndm_flags & NTF_PROXY) {
1794 err = pneigh_delete(tbl, net, nla_data(dst_attr), dev);
Eric Dumazet110b2492010-10-04 04:27:36 +00001795 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 }
WANG Congd7480fd2014-11-10 15:59:36 -08001797
1798 if (dev == NULL)
1799 goto out;
1800
1801 neigh = neigh_lookup(tbl, nla_data(dst_attr), dev);
1802 if (neigh == NULL) {
1803 err = -ENOENT;
1804 goto out;
1805 }
1806
David Ahern7a35a502018-12-05 20:02:29 -08001807 err = __neigh_update(neigh, NULL, NUD_FAILED,
1808 NEIGH_UPDATE_F_OVERRIDE | NEIGH_UPDATE_F_ADMIN,
1809 NETLINK_CB(skb).portid, extack);
Sowmini Varadhan50710342017-06-02 09:01:49 -07001810 write_lock_bh(&tbl->lock);
WANG Congd7480fd2014-11-10 15:59:36 -08001811 neigh_release(neigh);
Sowmini Varadhan50710342017-06-02 09:01:49 -07001812 neigh_remove_one(neigh, tbl);
1813 write_unlock_bh(&tbl->lock);
Thomas Grafa14a49d2006-08-07 17:53:08 -07001814
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815out:
1816 return err;
1817}
1818
David Ahernc21ef3e2017-04-16 09:48:24 -07001819static int neigh_add(struct sk_buff *skb, struct nlmsghdr *nlh,
1820 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821{
Roopa Prabhuf7aa74e2018-09-22 21:26:19 -07001822 int flags = NEIGH_UPDATE_F_ADMIN | NEIGH_UPDATE_F_OVERRIDE |
1823 NEIGH_UPDATE_F_OVERRIDE_ISROUTER;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001824 struct net *net = sock_net(skb->sk);
Thomas Graf5208deb2006-08-07 17:55:40 -07001825 struct ndmsg *ndm;
1826 struct nlattr *tb[NDA_MAX+1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 struct neigh_table *tbl;
1828 struct net_device *dev = NULL;
WANG Congd7480fd2014-11-10 15:59:36 -08001829 struct neighbour *neigh;
1830 void *dst, *lladdr;
David Aherndf9b0e32018-12-15 14:09:06 -08001831 u8 protocol = 0;
Thomas Graf5208deb2006-08-07 17:55:40 -07001832 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833
Eric Dumazet110b2492010-10-04 04:27:36 +00001834 ASSERT_RTNL();
David Ahernc21ef3e2017-04-16 09:48:24 -07001835 err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL, extack);
Thomas Graf5208deb2006-08-07 17:55:40 -07001836 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 goto out;
1838
Thomas Graf5208deb2006-08-07 17:55:40 -07001839 err = -EINVAL;
David Ahern7a35a502018-12-05 20:02:29 -08001840 if (!tb[NDA_DST]) {
1841 NL_SET_ERR_MSG(extack, "Network address not specified");
Thomas Graf5208deb2006-08-07 17:55:40 -07001842 goto out;
David Ahern7a35a502018-12-05 20:02:29 -08001843 }
Thomas Graf5208deb2006-08-07 17:55:40 -07001844
1845 ndm = nlmsg_data(nlh);
1846 if (ndm->ndm_ifindex) {
Eric Dumazet110b2492010-10-04 04:27:36 +00001847 dev = __dev_get_by_index(net, ndm->ndm_ifindex);
Thomas Graf5208deb2006-08-07 17:55:40 -07001848 if (dev == NULL) {
1849 err = -ENODEV;
1850 goto out;
1851 }
1852
David Ahern7a35a502018-12-05 20:02:29 -08001853 if (tb[NDA_LLADDR] && nla_len(tb[NDA_LLADDR]) < dev->addr_len) {
1854 NL_SET_ERR_MSG(extack, "Invalid link address");
Eric Dumazet110b2492010-10-04 04:27:36 +00001855 goto out;
David Ahern7a35a502018-12-05 20:02:29 -08001856 }
Thomas Graf5208deb2006-08-07 17:55:40 -07001857 }
1858
WANG Congd7480fd2014-11-10 15:59:36 -08001859 tbl = neigh_find_table(ndm->ndm_family);
1860 if (tbl == NULL)
1861 return -EAFNOSUPPORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862
David Ahern7a35a502018-12-05 20:02:29 -08001863 if (nla_len(tb[NDA_DST]) < (int)tbl->key_len) {
1864 NL_SET_ERR_MSG(extack, "Invalid network address");
WANG Congd7480fd2014-11-10 15:59:36 -08001865 goto out;
David Ahern7a35a502018-12-05 20:02:29 -08001866 }
1867
WANG Congd7480fd2014-11-10 15:59:36 -08001868 dst = nla_data(tb[NDA_DST]);
1869 lladdr = tb[NDA_LLADDR] ? nla_data(tb[NDA_LLADDR]) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870
David Aherndf9b0e32018-12-15 14:09:06 -08001871 if (tb[NDA_PROTOCOL]) {
1872 if (nla_len(tb[NDA_PROTOCOL]) != sizeof(u8)) {
1873 NL_SET_ERR_MSG(extack, "Invalid protocol attribute");
1874 goto out;
1875 }
1876 protocol = nla_get_u8(tb[NDA_PROTOCOL]);
1877 }
1878
WANG Congd7480fd2014-11-10 15:59:36 -08001879 if (ndm->ndm_flags & NTF_PROXY) {
1880 struct pneigh_entry *pn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881
WANG Congd7480fd2014-11-10 15:59:36 -08001882 err = -ENOBUFS;
1883 pn = pneigh_lookup(tbl, net, dst, dev, 1);
1884 if (pn) {
1885 pn->flags = ndm->ndm_flags;
David Aherndf9b0e32018-12-15 14:09:06 -08001886 if (protocol)
1887 pn->protocol = protocol;
Eric Biederman0c5c2d32009-03-04 00:03:08 -08001888 err = 0;
WANG Congd7480fd2014-11-10 15:59:36 -08001889 }
Eric Dumazet110b2492010-10-04 04:27:36 +00001890 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 }
1892
David Ahern7a35a502018-12-05 20:02:29 -08001893 if (!dev) {
1894 NL_SET_ERR_MSG(extack, "Device not specified");
WANG Congd7480fd2014-11-10 15:59:36 -08001895 goto out;
David Ahern7a35a502018-12-05 20:02:29 -08001896 }
WANG Congd7480fd2014-11-10 15:59:36 -08001897
1898 neigh = neigh_lookup(tbl, dst, dev);
1899 if (neigh == NULL) {
David Aherne997f8a2018-12-11 18:57:25 -07001900 bool exempt_from_gc;
1901
WANG Congd7480fd2014-11-10 15:59:36 -08001902 if (!(nlh->nlmsg_flags & NLM_F_CREATE)) {
1903 err = -ENOENT;
1904 goto out;
1905 }
1906
David Aherne997f8a2018-12-11 18:57:25 -07001907 exempt_from_gc = ndm->ndm_state & NUD_PERMANENT ||
1908 ndm->ndm_flags & NTF_EXT_LEARNED;
1909 neigh = ___neigh_create(tbl, dst, dev, exempt_from_gc, true);
WANG Congd7480fd2014-11-10 15:59:36 -08001910 if (IS_ERR(neigh)) {
1911 err = PTR_ERR(neigh);
1912 goto out;
1913 }
1914 } else {
1915 if (nlh->nlmsg_flags & NLM_F_EXCL) {
1916 err = -EEXIST;
1917 neigh_release(neigh);
1918 goto out;
1919 }
1920
1921 if (!(nlh->nlmsg_flags & NLM_F_REPLACE))
Roopa Prabhuf7aa74e2018-09-22 21:26:19 -07001922 flags &= ~(NEIGH_UPDATE_F_OVERRIDE |
1923 NEIGH_UPDATE_F_OVERRIDE_ISROUTER);
WANG Congd7480fd2014-11-10 15:59:36 -08001924 }
1925
Roopa Prabhu9ce33e42018-04-24 13:49:34 -07001926 if (ndm->ndm_flags & NTF_EXT_LEARNED)
1927 flags |= NEIGH_UPDATE_F_EXT_LEARNED;
1928
Roopa Prabhuf7aa74e2018-09-22 21:26:19 -07001929 if (ndm->ndm_flags & NTF_ROUTER)
1930 flags |= NEIGH_UPDATE_F_ISROUTER;
1931
WANG Congd7480fd2014-11-10 15:59:36 -08001932 if (ndm->ndm_flags & NTF_USE) {
1933 neigh_event_send(neigh, NULL);
1934 err = 0;
1935 } else
David Ahern7a35a502018-12-05 20:02:29 -08001936 err = __neigh_update(neigh, lladdr, ndm->ndm_state, flags,
1937 NETLINK_CB(skb).portid, extack);
David Aherndf9b0e32018-12-15 14:09:06 -08001938
1939 if (protocol)
1940 neigh->protocol = protocol;
1941
WANG Congd7480fd2014-11-10 15:59:36 -08001942 neigh_release(neigh);
1943
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944out:
1945 return err;
1946}
1947
Thomas Grafc7fb64d2005-06-18 22:50:55 -07001948static int neightbl_fill_parms(struct sk_buff *skb, struct neigh_parms *parms)
1949{
Thomas Grafca860fb2006-08-07 18:00:18 -07001950 struct nlattr *nest;
1951
1952 nest = nla_nest_start(skb, NDTA_PARMS);
1953 if (nest == NULL)
1954 return -ENOBUFS;
Thomas Grafc7fb64d2005-06-18 22:50:55 -07001955
David S. Miller9a6308d2012-04-01 20:06:28 -04001956 if ((parms->dev &&
1957 nla_put_u32(skb, NDTPA_IFINDEX, parms->dev->ifindex)) ||
Reshetova, Elena63439442017-06-30 13:07:56 +03001958 nla_put_u32(skb, NDTPA_REFCNT, refcount_read(&parms->refcnt)) ||
Jiri Pirko1f9248e2013-12-07 19:26:53 +01001959 nla_put_u32(skb, NDTPA_QUEUE_LENBYTES,
1960 NEIGH_VAR(parms, QUEUE_LEN_BYTES)) ||
David S. Miller9a6308d2012-04-01 20:06:28 -04001961 /* approximative value for deprecated QUEUE_LEN (in packets) */
1962 nla_put_u32(skb, NDTPA_QUEUE_LEN,
Jiri Pirko1f9248e2013-12-07 19:26:53 +01001963 NEIGH_VAR(parms, QUEUE_LEN_BYTES) / SKB_TRUESIZE(ETH_FRAME_LEN)) ||
1964 nla_put_u32(skb, NDTPA_PROXY_QLEN, NEIGH_VAR(parms, PROXY_QLEN)) ||
1965 nla_put_u32(skb, NDTPA_APP_PROBES, NEIGH_VAR(parms, APP_PROBES)) ||
1966 nla_put_u32(skb, NDTPA_UCAST_PROBES,
1967 NEIGH_VAR(parms, UCAST_PROBES)) ||
1968 nla_put_u32(skb, NDTPA_MCAST_PROBES,
1969 NEIGH_VAR(parms, MCAST_PROBES)) ||
YOSHIFUJI Hideaki/吉藤英明8da86462015-03-19 22:41:46 +09001970 nla_put_u32(skb, NDTPA_MCAST_REPROBES,
1971 NEIGH_VAR(parms, MCAST_REPROBES)) ||
Nicolas Dichtel2175d872016-04-22 17:31:21 +02001972 nla_put_msecs(skb, NDTPA_REACHABLE_TIME, parms->reachable_time,
1973 NDTPA_PAD) ||
David S. Miller9a6308d2012-04-01 20:06:28 -04001974 nla_put_msecs(skb, NDTPA_BASE_REACHABLE_TIME,
Nicolas Dichtel2175d872016-04-22 17:31:21 +02001975 NEIGH_VAR(parms, BASE_REACHABLE_TIME), NDTPA_PAD) ||
Jiri Pirko1f9248e2013-12-07 19:26:53 +01001976 nla_put_msecs(skb, NDTPA_GC_STALETIME,
Nicolas Dichtel2175d872016-04-22 17:31:21 +02001977 NEIGH_VAR(parms, GC_STALETIME), NDTPA_PAD) ||
David S. Miller9a6308d2012-04-01 20:06:28 -04001978 nla_put_msecs(skb, NDTPA_DELAY_PROBE_TIME,
Nicolas Dichtel2175d872016-04-22 17:31:21 +02001979 NEIGH_VAR(parms, DELAY_PROBE_TIME), NDTPA_PAD) ||
Jiri Pirko1f9248e2013-12-07 19:26:53 +01001980 nla_put_msecs(skb, NDTPA_RETRANS_TIME,
Nicolas Dichtel2175d872016-04-22 17:31:21 +02001981 NEIGH_VAR(parms, RETRANS_TIME), NDTPA_PAD) ||
Jiri Pirko1f9248e2013-12-07 19:26:53 +01001982 nla_put_msecs(skb, NDTPA_ANYCAST_DELAY,
Nicolas Dichtel2175d872016-04-22 17:31:21 +02001983 NEIGH_VAR(parms, ANYCAST_DELAY), NDTPA_PAD) ||
Jiri Pirko1f9248e2013-12-07 19:26:53 +01001984 nla_put_msecs(skb, NDTPA_PROXY_DELAY,
Nicolas Dichtel2175d872016-04-22 17:31:21 +02001985 NEIGH_VAR(parms, PROXY_DELAY), NDTPA_PAD) ||
Jiri Pirko1f9248e2013-12-07 19:26:53 +01001986 nla_put_msecs(skb, NDTPA_LOCKTIME,
Nicolas Dichtel2175d872016-04-22 17:31:21 +02001987 NEIGH_VAR(parms, LOCKTIME), NDTPA_PAD))
David S. Miller9a6308d2012-04-01 20:06:28 -04001988 goto nla_put_failure;
Thomas Grafca860fb2006-08-07 18:00:18 -07001989 return nla_nest_end(skb, nest);
Thomas Grafc7fb64d2005-06-18 22:50:55 -07001990
Thomas Grafca860fb2006-08-07 18:00:18 -07001991nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07001992 nla_nest_cancel(skb, nest);
1993 return -EMSGSIZE;
Thomas Grafc7fb64d2005-06-18 22:50:55 -07001994}
1995
Thomas Grafca860fb2006-08-07 18:00:18 -07001996static int neightbl_fill_info(struct sk_buff *skb, struct neigh_table *tbl,
1997 u32 pid, u32 seq, int type, int flags)
Thomas Grafc7fb64d2005-06-18 22:50:55 -07001998{
1999 struct nlmsghdr *nlh;
2000 struct ndtmsg *ndtmsg;
2001
Thomas Grafca860fb2006-08-07 18:00:18 -07002002 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndtmsg), flags);
2003 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -08002004 return -EMSGSIZE;
Thomas Grafc7fb64d2005-06-18 22:50:55 -07002005
Thomas Grafca860fb2006-08-07 18:00:18 -07002006 ndtmsg = nlmsg_data(nlh);
Thomas Grafc7fb64d2005-06-18 22:50:55 -07002007
2008 read_lock_bh(&tbl->lock);
2009 ndtmsg->ndtm_family = tbl->family;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -07002010 ndtmsg->ndtm_pad1 = 0;
2011 ndtmsg->ndtm_pad2 = 0;
Thomas Grafc7fb64d2005-06-18 22:50:55 -07002012
David S. Miller9a6308d2012-04-01 20:06:28 -04002013 if (nla_put_string(skb, NDTA_NAME, tbl->id) ||
Nicolas Dichtel2175d872016-04-22 17:31:21 +02002014 nla_put_msecs(skb, NDTA_GC_INTERVAL, tbl->gc_interval, NDTA_PAD) ||
David S. Miller9a6308d2012-04-01 20:06:28 -04002015 nla_put_u32(skb, NDTA_THRESH1, tbl->gc_thresh1) ||
2016 nla_put_u32(skb, NDTA_THRESH2, tbl->gc_thresh2) ||
2017 nla_put_u32(skb, NDTA_THRESH3, tbl->gc_thresh3))
2018 goto nla_put_failure;
Thomas Grafc7fb64d2005-06-18 22:50:55 -07002019 {
2020 unsigned long now = jiffies;
2021 unsigned int flush_delta = now - tbl->last_flush;
2022 unsigned int rand_delta = now - tbl->last_rand;
Eric Dumazetd6bf7812010-10-04 06:15:44 +00002023 struct neigh_hash_table *nht;
Thomas Grafc7fb64d2005-06-18 22:50:55 -07002024 struct ndt_config ndc = {
2025 .ndtc_key_len = tbl->key_len,
2026 .ndtc_entry_size = tbl->entry_size,
2027 .ndtc_entries = atomic_read(&tbl->entries),
2028 .ndtc_last_flush = jiffies_to_msecs(flush_delta),
2029 .ndtc_last_rand = jiffies_to_msecs(rand_delta),
Thomas Grafc7fb64d2005-06-18 22:50:55 -07002030 .ndtc_proxy_qlen = tbl->proxy_queue.qlen,
2031 };
2032
Eric Dumazetd6bf7812010-10-04 06:15:44 +00002033 rcu_read_lock_bh();
2034 nht = rcu_dereference_bh(tbl->nht);
David S. Miller2c2aba62011-12-28 15:06:58 -05002035 ndc.ndtc_hash_rnd = nht->hash_rnd[0];
David S. Millercd089332011-07-11 01:28:12 -07002036 ndc.ndtc_hash_mask = ((1 << nht->hash_shift) - 1);
Eric Dumazetd6bf7812010-10-04 06:15:44 +00002037 rcu_read_unlock_bh();
2038
David S. Miller9a6308d2012-04-01 20:06:28 -04002039 if (nla_put(skb, NDTA_CONFIG, sizeof(ndc), &ndc))
2040 goto nla_put_failure;
Thomas Grafc7fb64d2005-06-18 22:50:55 -07002041 }
2042
2043 {
2044 int cpu;
2045 struct ndt_stats ndst;
2046
2047 memset(&ndst, 0, sizeof(ndst));
2048
KAMEZAWA Hiroyuki6f912042006-04-10 22:52:50 -07002049 for_each_possible_cpu(cpu) {
Thomas Grafc7fb64d2005-06-18 22:50:55 -07002050 struct neigh_statistics *st;
2051
Thomas Grafc7fb64d2005-06-18 22:50:55 -07002052 st = per_cpu_ptr(tbl->stats, cpu);
2053 ndst.ndts_allocs += st->allocs;
2054 ndst.ndts_destroys += st->destroys;
2055 ndst.ndts_hash_grows += st->hash_grows;
2056 ndst.ndts_res_failed += st->res_failed;
2057 ndst.ndts_lookups += st->lookups;
2058 ndst.ndts_hits += st->hits;
2059 ndst.ndts_rcv_probes_mcast += st->rcv_probes_mcast;
2060 ndst.ndts_rcv_probes_ucast += st->rcv_probes_ucast;
2061 ndst.ndts_periodic_gc_runs += st->periodic_gc_runs;
2062 ndst.ndts_forced_gc_runs += st->forced_gc_runs;
Rick Jonesfb811392015-08-07 11:10:37 -07002063 ndst.ndts_table_fulls += st->table_fulls;
Thomas Grafc7fb64d2005-06-18 22:50:55 -07002064 }
2065
Nicolas Dichtelb6763382016-04-26 10:06:17 +02002066 if (nla_put_64bit(skb, NDTA_STATS, sizeof(ndst), &ndst,
2067 NDTA_PAD))
David S. Miller9a6308d2012-04-01 20:06:28 -04002068 goto nla_put_failure;
Thomas Grafc7fb64d2005-06-18 22:50:55 -07002069 }
2070
2071 BUG_ON(tbl->parms.dev);
2072 if (neightbl_fill_parms(skb, &tbl->parms) < 0)
Thomas Grafca860fb2006-08-07 18:00:18 -07002073 goto nla_put_failure;
Thomas Grafc7fb64d2005-06-18 22:50:55 -07002074
2075 read_unlock_bh(&tbl->lock);
Johannes Berg053c0952015-01-16 22:09:00 +01002076 nlmsg_end(skb, nlh);
2077 return 0;
Thomas Grafc7fb64d2005-06-18 22:50:55 -07002078
Thomas Grafca860fb2006-08-07 18:00:18 -07002079nla_put_failure:
Thomas Grafc7fb64d2005-06-18 22:50:55 -07002080 read_unlock_bh(&tbl->lock);
Patrick McHardy26932562007-01-31 23:16:40 -08002081 nlmsg_cancel(skb, nlh);
2082 return -EMSGSIZE;
Thomas Grafc7fb64d2005-06-18 22:50:55 -07002083}
2084
Thomas Grafca860fb2006-08-07 18:00:18 -07002085static int neightbl_fill_param_info(struct sk_buff *skb,
2086 struct neigh_table *tbl,
Thomas Grafc7fb64d2005-06-18 22:50:55 -07002087 struct neigh_parms *parms,
Thomas Grafca860fb2006-08-07 18:00:18 -07002088 u32 pid, u32 seq, int type,
2089 unsigned int flags)
Thomas Grafc7fb64d2005-06-18 22:50:55 -07002090{
2091 struct ndtmsg *ndtmsg;
2092 struct nlmsghdr *nlh;
2093
Thomas Grafca860fb2006-08-07 18:00:18 -07002094 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndtmsg), flags);
2095 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -08002096 return -EMSGSIZE;
Thomas Grafc7fb64d2005-06-18 22:50:55 -07002097
Thomas Grafca860fb2006-08-07 18:00:18 -07002098 ndtmsg = nlmsg_data(nlh);
Thomas Grafc7fb64d2005-06-18 22:50:55 -07002099
2100 read_lock_bh(&tbl->lock);
2101 ndtmsg->ndtm_family = tbl->family;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -07002102 ndtmsg->ndtm_pad1 = 0;
2103 ndtmsg->ndtm_pad2 = 0;
Thomas Grafc7fb64d2005-06-18 22:50:55 -07002104
Thomas Grafca860fb2006-08-07 18:00:18 -07002105 if (nla_put_string(skb, NDTA_NAME, tbl->id) < 0 ||
2106 neightbl_fill_parms(skb, parms) < 0)
2107 goto errout;
Thomas Grafc7fb64d2005-06-18 22:50:55 -07002108
2109 read_unlock_bh(&tbl->lock);
Johannes Berg053c0952015-01-16 22:09:00 +01002110 nlmsg_end(skb, nlh);
2111 return 0;
Thomas Grafca860fb2006-08-07 18:00:18 -07002112errout:
Thomas Grafc7fb64d2005-06-18 22:50:55 -07002113 read_unlock_bh(&tbl->lock);
Patrick McHardy26932562007-01-31 23:16:40 -08002114 nlmsg_cancel(skb, nlh);
2115 return -EMSGSIZE;
Thomas Grafc7fb64d2005-06-18 22:50:55 -07002116}
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09002117
Patrick McHardyef7c79e2007-06-05 12:38:30 -07002118static const struct nla_policy nl_neightbl_policy[NDTA_MAX+1] = {
Thomas Graf6b3f8672006-08-07 17:58:53 -07002119 [NDTA_NAME] = { .type = NLA_STRING },
2120 [NDTA_THRESH1] = { .type = NLA_U32 },
2121 [NDTA_THRESH2] = { .type = NLA_U32 },
2122 [NDTA_THRESH3] = { .type = NLA_U32 },
2123 [NDTA_GC_INTERVAL] = { .type = NLA_U64 },
2124 [NDTA_PARMS] = { .type = NLA_NESTED },
2125};
2126
Patrick McHardyef7c79e2007-06-05 12:38:30 -07002127static const struct nla_policy nl_ntbl_parm_policy[NDTPA_MAX+1] = {
Thomas Graf6b3f8672006-08-07 17:58:53 -07002128 [NDTPA_IFINDEX] = { .type = NLA_U32 },
2129 [NDTPA_QUEUE_LEN] = { .type = NLA_U32 },
2130 [NDTPA_PROXY_QLEN] = { .type = NLA_U32 },
2131 [NDTPA_APP_PROBES] = { .type = NLA_U32 },
2132 [NDTPA_UCAST_PROBES] = { .type = NLA_U32 },
2133 [NDTPA_MCAST_PROBES] = { .type = NLA_U32 },
YOSHIFUJI Hideaki/吉藤英明8da86462015-03-19 22:41:46 +09002134 [NDTPA_MCAST_REPROBES] = { .type = NLA_U32 },
Thomas Graf6b3f8672006-08-07 17:58:53 -07002135 [NDTPA_BASE_REACHABLE_TIME] = { .type = NLA_U64 },
2136 [NDTPA_GC_STALETIME] = { .type = NLA_U64 },
2137 [NDTPA_DELAY_PROBE_TIME] = { .type = NLA_U64 },
2138 [NDTPA_RETRANS_TIME] = { .type = NLA_U64 },
2139 [NDTPA_ANYCAST_DELAY] = { .type = NLA_U64 },
2140 [NDTPA_PROXY_DELAY] = { .type = NLA_U64 },
2141 [NDTPA_LOCKTIME] = { .type = NLA_U64 },
2142};
2143
David Ahernc21ef3e2017-04-16 09:48:24 -07002144static int neightbl_set(struct sk_buff *skb, struct nlmsghdr *nlh,
2145 struct netlink_ext_ack *extack)
Thomas Grafc7fb64d2005-06-18 22:50:55 -07002146{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002147 struct net *net = sock_net(skb->sk);
Thomas Grafc7fb64d2005-06-18 22:50:55 -07002148 struct neigh_table *tbl;
Thomas Graf6b3f8672006-08-07 17:58:53 -07002149 struct ndtmsg *ndtmsg;
2150 struct nlattr *tb[NDTA_MAX+1];
WANG Congd7480fd2014-11-10 15:59:36 -08002151 bool found = false;
2152 int err, tidx;
Thomas Grafc7fb64d2005-06-18 22:50:55 -07002153
Thomas Graf6b3f8672006-08-07 17:58:53 -07002154 err = nlmsg_parse(nlh, sizeof(*ndtmsg), tb, NDTA_MAX,
David Ahernc21ef3e2017-04-16 09:48:24 -07002155 nl_neightbl_policy, extack);
Thomas Graf6b3f8672006-08-07 17:58:53 -07002156 if (err < 0)
2157 goto errout;
Thomas Grafc7fb64d2005-06-18 22:50:55 -07002158
Thomas Graf6b3f8672006-08-07 17:58:53 -07002159 if (tb[NDTA_NAME] == NULL) {
2160 err = -EINVAL;
2161 goto errout;
2162 }
2163
2164 ndtmsg = nlmsg_data(nlh);
WANG Congd7480fd2014-11-10 15:59:36 -08002165
2166 for (tidx = 0; tidx < NEIGH_NR_TABLES; tidx++) {
2167 tbl = neigh_tables[tidx];
2168 if (!tbl)
2169 continue;
Thomas Grafc7fb64d2005-06-18 22:50:55 -07002170 if (ndtmsg->ndtm_family && tbl->family != ndtmsg->ndtm_family)
2171 continue;
WANG Congd7480fd2014-11-10 15:59:36 -08002172 if (nla_strcmp(tb[NDTA_NAME], tbl->id) == 0) {
2173 found = true;
Thomas Grafc7fb64d2005-06-18 22:50:55 -07002174 break;
WANG Congd7480fd2014-11-10 15:59:36 -08002175 }
Thomas Grafc7fb64d2005-06-18 22:50:55 -07002176 }
2177
WANG Congd7480fd2014-11-10 15:59:36 -08002178 if (!found)
2179 return -ENOENT;
Thomas Grafc7fb64d2005-06-18 22:50:55 -07002180
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09002181 /*
Thomas Grafc7fb64d2005-06-18 22:50:55 -07002182 * We acquire tbl->lock to be nice to the periodic timers and
2183 * make sure they always see a consistent set of values.
2184 */
2185 write_lock_bh(&tbl->lock);
2186
Thomas Graf6b3f8672006-08-07 17:58:53 -07002187 if (tb[NDTA_PARMS]) {
2188 struct nlattr *tbp[NDTPA_MAX+1];
Thomas Grafc7fb64d2005-06-18 22:50:55 -07002189 struct neigh_parms *p;
Thomas Graf6b3f8672006-08-07 17:58:53 -07002190 int i, ifindex = 0;
Thomas Grafc7fb64d2005-06-18 22:50:55 -07002191
Thomas Graf6b3f8672006-08-07 17:58:53 -07002192 err = nla_parse_nested(tbp, NDTPA_MAX, tb[NDTA_PARMS],
David Ahernc21ef3e2017-04-16 09:48:24 -07002193 nl_ntbl_parm_policy, extack);
Thomas Graf6b3f8672006-08-07 17:58:53 -07002194 if (err < 0)
2195 goto errout_tbl_lock;
Thomas Grafc7fb64d2005-06-18 22:50:55 -07002196
Thomas Graf6b3f8672006-08-07 17:58:53 -07002197 if (tbp[NDTPA_IFINDEX])
2198 ifindex = nla_get_u32(tbp[NDTPA_IFINDEX]);
Thomas Grafc7fb64d2005-06-18 22:50:55 -07002199
Tobias Klauser97fd5bc2009-07-13 11:17:49 -07002200 p = lookup_neigh_parms(tbl, net, ifindex);
Thomas Grafc7fb64d2005-06-18 22:50:55 -07002201 if (p == NULL) {
2202 err = -ENOENT;
Thomas Graf6b3f8672006-08-07 17:58:53 -07002203 goto errout_tbl_lock;
Thomas Grafc7fb64d2005-06-18 22:50:55 -07002204 }
Thomas Grafc7fb64d2005-06-18 22:50:55 -07002205
Thomas Graf6b3f8672006-08-07 17:58:53 -07002206 for (i = 1; i <= NDTPA_MAX; i++) {
2207 if (tbp[i] == NULL)
2208 continue;
Thomas Grafc7fb64d2005-06-18 22:50:55 -07002209
Thomas Graf6b3f8672006-08-07 17:58:53 -07002210 switch (i) {
2211 case NDTPA_QUEUE_LEN:
Jiri Pirko1f9248e2013-12-07 19:26:53 +01002212 NEIGH_VAR_SET(p, QUEUE_LEN_BYTES,
2213 nla_get_u32(tbp[i]) *
2214 SKB_TRUESIZE(ETH_FRAME_LEN));
Eric Dumazet8b5c1712011-11-09 12:07:14 +00002215 break;
2216 case NDTPA_QUEUE_LENBYTES:
Jiri Pirko1f9248e2013-12-07 19:26:53 +01002217 NEIGH_VAR_SET(p, QUEUE_LEN_BYTES,
2218 nla_get_u32(tbp[i]));
Thomas Graf6b3f8672006-08-07 17:58:53 -07002219 break;
2220 case NDTPA_PROXY_QLEN:
Jiri Pirko1f9248e2013-12-07 19:26:53 +01002221 NEIGH_VAR_SET(p, PROXY_QLEN,
2222 nla_get_u32(tbp[i]));
Thomas Graf6b3f8672006-08-07 17:58:53 -07002223 break;
2224 case NDTPA_APP_PROBES:
Jiri Pirko1f9248e2013-12-07 19:26:53 +01002225 NEIGH_VAR_SET(p, APP_PROBES,
2226 nla_get_u32(tbp[i]));
Thomas Graf6b3f8672006-08-07 17:58:53 -07002227 break;
2228 case NDTPA_UCAST_PROBES:
Jiri Pirko1f9248e2013-12-07 19:26:53 +01002229 NEIGH_VAR_SET(p, UCAST_PROBES,
2230 nla_get_u32(tbp[i]));
Thomas Graf6b3f8672006-08-07 17:58:53 -07002231 break;
2232 case NDTPA_MCAST_PROBES:
Jiri Pirko1f9248e2013-12-07 19:26:53 +01002233 NEIGH_VAR_SET(p, MCAST_PROBES,
2234 nla_get_u32(tbp[i]));
Thomas Graf6b3f8672006-08-07 17:58:53 -07002235 break;
YOSHIFUJI Hideaki/吉藤英明8da86462015-03-19 22:41:46 +09002236 case NDTPA_MCAST_REPROBES:
2237 NEIGH_VAR_SET(p, MCAST_REPROBES,
2238 nla_get_u32(tbp[i]));
2239 break;
Thomas Graf6b3f8672006-08-07 17:58:53 -07002240 case NDTPA_BASE_REACHABLE_TIME:
Jiri Pirko1f9248e2013-12-07 19:26:53 +01002241 NEIGH_VAR_SET(p, BASE_REACHABLE_TIME,
2242 nla_get_msecs(tbp[i]));
Jean-Francois Remy4bf69802015-01-14 04:22:39 +01002243 /* update reachable_time as well, otherwise, the change will
2244 * only be effective after the next time neigh_periodic_work
2245 * decides to recompute it (can be multiple minutes)
2246 */
2247 p->reachable_time =
2248 neigh_rand_reach_time(NEIGH_VAR(p, BASE_REACHABLE_TIME));
Thomas Graf6b3f8672006-08-07 17:58:53 -07002249 break;
2250 case NDTPA_GC_STALETIME:
Jiri Pirko1f9248e2013-12-07 19:26:53 +01002251 NEIGH_VAR_SET(p, GC_STALETIME,
2252 nla_get_msecs(tbp[i]));
Thomas Graf6b3f8672006-08-07 17:58:53 -07002253 break;
2254 case NDTPA_DELAY_PROBE_TIME:
Jiri Pirko1f9248e2013-12-07 19:26:53 +01002255 NEIGH_VAR_SET(p, DELAY_PROBE_TIME,
2256 nla_get_msecs(tbp[i]));
Ido Schimmel2a4501a2016-07-05 11:27:42 +02002257 call_netevent_notifiers(NETEVENT_DELAY_PROBE_TIME_UPDATE, p);
Thomas Graf6b3f8672006-08-07 17:58:53 -07002258 break;
2259 case NDTPA_RETRANS_TIME:
Jiri Pirko1f9248e2013-12-07 19:26:53 +01002260 NEIGH_VAR_SET(p, RETRANS_TIME,
2261 nla_get_msecs(tbp[i]));
Thomas Graf6b3f8672006-08-07 17:58:53 -07002262 break;
2263 case NDTPA_ANYCAST_DELAY:
Jiri Pirko39774582014-01-14 15:46:07 +01002264 NEIGH_VAR_SET(p, ANYCAST_DELAY,
2265 nla_get_msecs(tbp[i]));
Thomas Graf6b3f8672006-08-07 17:58:53 -07002266 break;
2267 case NDTPA_PROXY_DELAY:
Jiri Pirko39774582014-01-14 15:46:07 +01002268 NEIGH_VAR_SET(p, PROXY_DELAY,
2269 nla_get_msecs(tbp[i]));
Thomas Graf6b3f8672006-08-07 17:58:53 -07002270 break;
2271 case NDTPA_LOCKTIME:
Jiri Pirko39774582014-01-14 15:46:07 +01002272 NEIGH_VAR_SET(p, LOCKTIME,
2273 nla_get_msecs(tbp[i]));
Thomas Graf6b3f8672006-08-07 17:58:53 -07002274 break;
2275 }
2276 }
Thomas Grafc7fb64d2005-06-18 22:50:55 -07002277 }
2278
Gao fengdc25c672013-06-20 10:01:34 +08002279 err = -ENOENT;
2280 if ((tb[NDTA_THRESH1] || tb[NDTA_THRESH2] ||
2281 tb[NDTA_THRESH3] || tb[NDTA_GC_INTERVAL]) &&
2282 !net_eq(net, &init_net))
2283 goto errout_tbl_lock;
2284
Thomas Graf6b3f8672006-08-07 17:58:53 -07002285 if (tb[NDTA_THRESH1])
2286 tbl->gc_thresh1 = nla_get_u32(tb[NDTA_THRESH1]);
2287
2288 if (tb[NDTA_THRESH2])
2289 tbl->gc_thresh2 = nla_get_u32(tb[NDTA_THRESH2]);
2290
2291 if (tb[NDTA_THRESH3])
2292 tbl->gc_thresh3 = nla_get_u32(tb[NDTA_THRESH3]);
2293
2294 if (tb[NDTA_GC_INTERVAL])
2295 tbl->gc_interval = nla_get_msecs(tb[NDTA_GC_INTERVAL]);
2296
Thomas Grafc7fb64d2005-06-18 22:50:55 -07002297 err = 0;
2298
Thomas Graf6b3f8672006-08-07 17:58:53 -07002299errout_tbl_lock:
Thomas Grafc7fb64d2005-06-18 22:50:55 -07002300 write_unlock_bh(&tbl->lock);
Thomas Graf6b3f8672006-08-07 17:58:53 -07002301errout:
Thomas Grafc7fb64d2005-06-18 22:50:55 -07002302 return err;
2303}
2304
David Ahern9632d472018-10-07 20:16:37 -07002305static int neightbl_valid_dump_info(const struct nlmsghdr *nlh,
2306 struct netlink_ext_ack *extack)
2307{
2308 struct ndtmsg *ndtm;
2309
2310 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ndtm))) {
2311 NL_SET_ERR_MSG(extack, "Invalid header for neighbor table dump request");
2312 return -EINVAL;
2313 }
2314
2315 ndtm = nlmsg_data(nlh);
2316 if (ndtm->ndtm_pad1 || ndtm->ndtm_pad2) {
2317 NL_SET_ERR_MSG(extack, "Invalid values in header for neighbor table dump request");
2318 return -EINVAL;
2319 }
2320
2321 if (nlmsg_attrlen(nlh, sizeof(*ndtm))) {
2322 NL_SET_ERR_MSG(extack, "Invalid data after header in neighbor table dump request");
2323 return -EINVAL;
2324 }
2325
2326 return 0;
2327}
2328
Thomas Grafc8822a42007-03-22 11:50:06 -07002329static int neightbl_dump_info(struct sk_buff *skb, struct netlink_callback *cb)
Thomas Grafc7fb64d2005-06-18 22:50:55 -07002330{
David Ahern9632d472018-10-07 20:16:37 -07002331 const struct nlmsghdr *nlh = cb->nlh;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002332 struct net *net = sock_net(skb->sk);
Thomas Grafca860fb2006-08-07 18:00:18 -07002333 int family, tidx, nidx = 0;
2334 int tbl_skip = cb->args[0];
2335 int neigh_skip = cb->args[1];
Thomas Grafc7fb64d2005-06-18 22:50:55 -07002336 struct neigh_table *tbl;
2337
David Ahern9632d472018-10-07 20:16:37 -07002338 if (cb->strict_check) {
2339 int err = neightbl_valid_dump_info(nlh, cb->extack);
2340
2341 if (err < 0)
2342 return err;
2343 }
2344
2345 family = ((struct rtgenmsg *)nlmsg_data(nlh))->rtgen_family;
Thomas Grafc7fb64d2005-06-18 22:50:55 -07002346
WANG Congd7480fd2014-11-10 15:59:36 -08002347 for (tidx = 0; tidx < NEIGH_NR_TABLES; tidx++) {
Thomas Grafc7fb64d2005-06-18 22:50:55 -07002348 struct neigh_parms *p;
2349
WANG Congd7480fd2014-11-10 15:59:36 -08002350 tbl = neigh_tables[tidx];
2351 if (!tbl)
2352 continue;
2353
Thomas Grafca860fb2006-08-07 18:00:18 -07002354 if (tidx < tbl_skip || (family && tbl->family != family))
Thomas Grafc7fb64d2005-06-18 22:50:55 -07002355 continue;
2356
Eric W. Biederman15e47302012-09-07 20:12:54 +00002357 if (neightbl_fill_info(skb, tbl, NETLINK_CB(cb->skb).portid,
David Ahern9632d472018-10-07 20:16:37 -07002358 nlh->nlmsg_seq, RTM_NEWNEIGHTBL,
David S. Miller7b46a642015-01-18 23:36:08 -05002359 NLM_F_MULTI) < 0)
Thomas Grafc7fb64d2005-06-18 22:50:55 -07002360 break;
2361
Nicolas Dichtel75fbfd32014-10-29 19:29:31 +01002362 nidx = 0;
2363 p = list_next_entry(&tbl->parms, list);
2364 list_for_each_entry_from(p, &tbl->parms_list, list) {
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09002365 if (!net_eq(neigh_parms_net(p), net))
Eric W. Biederman426b5302008-01-24 00:13:18 -08002366 continue;
2367
Gautam Kachrooefc683f2009-02-06 00:52:04 -08002368 if (nidx < neigh_skip)
2369 goto next;
Thomas Grafc7fb64d2005-06-18 22:50:55 -07002370
Thomas Grafca860fb2006-08-07 18:00:18 -07002371 if (neightbl_fill_param_info(skb, tbl, p,
Eric W. Biederman15e47302012-09-07 20:12:54 +00002372 NETLINK_CB(cb->skb).portid,
David Ahern9632d472018-10-07 20:16:37 -07002373 nlh->nlmsg_seq,
Thomas Grafca860fb2006-08-07 18:00:18 -07002374 RTM_NEWNEIGHTBL,
David S. Miller7b46a642015-01-18 23:36:08 -05002375 NLM_F_MULTI) < 0)
Thomas Grafc7fb64d2005-06-18 22:50:55 -07002376 goto out;
Gautam Kachrooefc683f2009-02-06 00:52:04 -08002377 next:
2378 nidx++;
Thomas Grafc7fb64d2005-06-18 22:50:55 -07002379 }
2380
Thomas Grafca860fb2006-08-07 18:00:18 -07002381 neigh_skip = 0;
Thomas Grafc7fb64d2005-06-18 22:50:55 -07002382 }
2383out:
Thomas Grafca860fb2006-08-07 18:00:18 -07002384 cb->args[0] = tidx;
2385 cb->args[1] = nidx;
Thomas Grafc7fb64d2005-06-18 22:50:55 -07002386
2387 return skb->len;
2388}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389
Thomas Graf8b8aec52006-08-07 17:56:37 -07002390static int neigh_fill_info(struct sk_buff *skb, struct neighbour *neigh,
2391 u32 pid, u32 seq, int type, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392{
2393 unsigned long now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 struct nda_cacheinfo ci;
Thomas Graf8b8aec52006-08-07 17:56:37 -07002395 struct nlmsghdr *nlh;
2396 struct ndmsg *ndm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397
Thomas Graf8b8aec52006-08-07 17:56:37 -07002398 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), flags);
2399 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -08002400 return -EMSGSIZE;
Thomas Graf8b8aec52006-08-07 17:56:37 -07002401
2402 ndm = nlmsg_data(nlh);
2403 ndm->ndm_family = neigh->ops->family;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -07002404 ndm->ndm_pad1 = 0;
2405 ndm->ndm_pad2 = 0;
Thomas Graf8b8aec52006-08-07 17:56:37 -07002406 ndm->ndm_flags = neigh->flags;
2407 ndm->ndm_type = neigh->type;
2408 ndm->ndm_ifindex = neigh->dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409
David S. Miller9a6308d2012-04-01 20:06:28 -04002410 if (nla_put(skb, NDA_DST, neigh->tbl->key_len, neigh->primary_key))
2411 goto nla_put_failure;
Thomas Graf8b8aec52006-08-07 17:56:37 -07002412
2413 read_lock_bh(&neigh->lock);
2414 ndm->ndm_state = neigh->nud_state;
Eric Dumazet0ed8ddf2010-10-07 10:44:07 +00002415 if (neigh->nud_state & NUD_VALID) {
2416 char haddr[MAX_ADDR_LEN];
2417
2418 neigh_ha_snapshot(haddr, neigh, neigh->dev);
2419 if (nla_put(skb, NDA_LLADDR, neigh->dev->addr_len, haddr) < 0) {
2420 read_unlock_bh(&neigh->lock);
2421 goto nla_put_failure;
2422 }
Thomas Graf8b8aec52006-08-07 17:56:37 -07002423 }
2424
Stephen Hemmingerb9f5f522008-06-03 16:03:15 -07002425 ci.ndm_used = jiffies_to_clock_t(now - neigh->used);
2426 ci.ndm_confirmed = jiffies_to_clock_t(now - neigh->confirmed);
2427 ci.ndm_updated = jiffies_to_clock_t(now - neigh->updated);
Reshetova, Elena9f237432017-06-30 13:07:55 +03002428 ci.ndm_refcnt = refcount_read(&neigh->refcnt) - 1;
Thomas Graf8b8aec52006-08-07 17:56:37 -07002429 read_unlock_bh(&neigh->lock);
2430
David S. Miller9a6308d2012-04-01 20:06:28 -04002431 if (nla_put_u32(skb, NDA_PROBES, atomic_read(&neigh->probes)) ||
2432 nla_put(skb, NDA_CACHEINFO, sizeof(ci), &ci))
2433 goto nla_put_failure;
Thomas Graf8b8aec52006-08-07 17:56:37 -07002434
David Aherndf9b0e32018-12-15 14:09:06 -08002435 if (neigh->protocol && nla_put_u8(skb, NDA_PROTOCOL, neigh->protocol))
2436 goto nla_put_failure;
2437
Johannes Berg053c0952015-01-16 22:09:00 +01002438 nlmsg_end(skb, nlh);
2439 return 0;
Thomas Graf8b8aec52006-08-07 17:56:37 -07002440
2441nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -08002442 nlmsg_cancel(skb, nlh);
2443 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444}
2445
Tony Zelenoff84920c12012-01-26 22:28:58 +00002446static int pneigh_fill_info(struct sk_buff *skb, struct pneigh_entry *pn,
2447 u32 pid, u32 seq, int type, unsigned int flags,
2448 struct neigh_table *tbl)
2449{
2450 struct nlmsghdr *nlh;
2451 struct ndmsg *ndm;
2452
2453 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), flags);
2454 if (nlh == NULL)
2455 return -EMSGSIZE;
2456
2457 ndm = nlmsg_data(nlh);
2458 ndm->ndm_family = tbl->family;
2459 ndm->ndm_pad1 = 0;
2460 ndm->ndm_pad2 = 0;
2461 ndm->ndm_flags = pn->flags | NTF_PROXY;
Jun Zhao545469f2014-07-26 00:38:59 +08002462 ndm->ndm_type = RTN_UNICAST;
Konstantin Khlebnikov6adc5fd2015-12-01 01:14:48 +03002463 ndm->ndm_ifindex = pn->dev ? pn->dev->ifindex : 0;
Tony Zelenoff84920c12012-01-26 22:28:58 +00002464 ndm->ndm_state = NUD_NONE;
2465
David S. Miller9a6308d2012-04-01 20:06:28 -04002466 if (nla_put(skb, NDA_DST, tbl->key_len, pn->key))
2467 goto nla_put_failure;
Tony Zelenoff84920c12012-01-26 22:28:58 +00002468
David Aherndf9b0e32018-12-15 14:09:06 -08002469 if (pn->protocol && nla_put_u8(skb, NDA_PROTOCOL, pn->protocol))
2470 goto nla_put_failure;
2471
Johannes Berg053c0952015-01-16 22:09:00 +01002472 nlmsg_end(skb, nlh);
2473 return 0;
Tony Zelenoff84920c12012-01-26 22:28:58 +00002474
2475nla_put_failure:
2476 nlmsg_cancel(skb, nlh);
2477 return -EMSGSIZE;
2478}
2479
Roopa Prabhu7b8f7a42017-03-19 22:01:28 -07002480static void neigh_update_notify(struct neighbour *neigh, u32 nlmsg_pid)
Thomas Grafd961db32007-08-08 23:12:56 -07002481{
2482 call_netevent_notifiers(NETEVENT_NEIGH_UPDATE, neigh);
Roopa Prabhu7b8f7a42017-03-19 22:01:28 -07002483 __neigh_notify(neigh, RTM_NEWNEIGH, 0, nlmsg_pid);
Thomas Grafd961db32007-08-08 23:12:56 -07002484}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485
David Ahern21fdd092015-09-29 09:32:03 -07002486static bool neigh_master_filtered(struct net_device *dev, int master_idx)
2487{
2488 struct net_device *master;
2489
2490 if (!master_idx)
2491 return false;
2492
Eric Dumazetaab456d2018-10-26 09:33:27 -07002493 master = dev ? netdev_master_upper_dev_get(dev) : NULL;
David Ahern21fdd092015-09-29 09:32:03 -07002494 if (!master || master->ifindex != master_idx)
2495 return true;
2496
2497 return false;
2498}
2499
David Ahern16660f02015-10-03 11:43:46 -07002500static bool neigh_ifindex_filtered(struct net_device *dev, int filter_idx)
2501{
Eric Dumazetaab456d2018-10-26 09:33:27 -07002502 if (filter_idx && (!dev || dev->ifindex != filter_idx))
David Ahern16660f02015-10-03 11:43:46 -07002503 return true;
2504
2505 return false;
2506}
2507
David Ahern6f52f802018-10-03 15:33:12 -07002508struct neigh_dump_filter {
2509 int master_idx;
2510 int dev_idx;
2511};
2512
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513static int neigh_dump_table(struct neigh_table *tbl, struct sk_buff *skb,
David Ahern6f52f802018-10-03 15:33:12 -07002514 struct netlink_callback *cb,
2515 struct neigh_dump_filter *filter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516{
Eric Dumazet767e97e2010-10-06 17:49:21 -07002517 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518 struct neighbour *n;
2519 int rc, h, s_h = cb->args[1];
2520 int idx, s_idx = idx = cb->args[2];
Eric Dumazetd6bf7812010-10-04 06:15:44 +00002521 struct neigh_hash_table *nht;
David Ahern21fdd092015-09-29 09:32:03 -07002522 unsigned int flags = NLM_F_MULTI;
David Ahern21fdd092015-09-29 09:32:03 -07002523
David Ahern6f52f802018-10-03 15:33:12 -07002524 if (filter->dev_idx || filter->master_idx)
2525 flags |= NLM_F_DUMP_FILTERED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526
Eric Dumazetd6bf7812010-10-04 06:15:44 +00002527 rcu_read_lock_bh();
2528 nht = rcu_dereference_bh(tbl->nht);
2529
Eric Dumazet4bd6683b2012-06-07 04:58:35 +00002530 for (h = s_h; h < (1 << nht->hash_shift); h++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531 if (h > s_h)
2532 s_idx = 0;
Eric Dumazet767e97e2010-10-06 17:49:21 -07002533 for (n = rcu_dereference_bh(nht->hash_buckets[h]), idx = 0;
2534 n != NULL;
2535 n = rcu_dereference_bh(n->next)) {
Zhang Shengju18502ac2016-11-30 11:24:42 +08002536 if (idx < s_idx || !net_eq(dev_net(n->dev), net))
2537 goto next;
David Ahern6f52f802018-10-03 15:33:12 -07002538 if (neigh_ifindex_filtered(n->dev, filter->dev_idx) ||
2539 neigh_master_filtered(n->dev, filter->master_idx))
Gautam Kachrooefc683f2009-02-06 00:52:04 -08002540 goto next;
Eric W. Biederman15e47302012-09-07 20:12:54 +00002541 if (neigh_fill_info(skb, n, NETLINK_CB(cb->skb).portid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542 cb->nlh->nlmsg_seq,
Jamal Hadi Salimb6544c02005-06-18 22:54:12 -07002543 RTM_NEWNEIGH,
David Ahern21fdd092015-09-29 09:32:03 -07002544 flags) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545 rc = -1;
2546 goto out;
2547 }
Eric Dumazet767e97e2010-10-06 17:49:21 -07002548next:
Gautam Kachrooefc683f2009-02-06 00:52:04 -08002549 idx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551 }
2552 rc = skb->len;
2553out:
Eric Dumazetd6bf7812010-10-04 06:15:44 +00002554 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555 cb->args[1] = h;
2556 cb->args[2] = idx;
2557 return rc;
2558}
2559
Tony Zelenoff84920c12012-01-26 22:28:58 +00002560static int pneigh_dump_table(struct neigh_table *tbl, struct sk_buff *skb,
David Ahern6f52f802018-10-03 15:33:12 -07002561 struct netlink_callback *cb,
2562 struct neigh_dump_filter *filter)
Tony Zelenoff84920c12012-01-26 22:28:58 +00002563{
2564 struct pneigh_entry *n;
2565 struct net *net = sock_net(skb->sk);
2566 int rc, h, s_h = cb->args[3];
2567 int idx, s_idx = idx = cb->args[4];
David Ahern6f52f802018-10-03 15:33:12 -07002568 unsigned int flags = NLM_F_MULTI;
2569
2570 if (filter->dev_idx || filter->master_idx)
2571 flags |= NLM_F_DUMP_FILTERED;
Tony Zelenoff84920c12012-01-26 22:28:58 +00002572
2573 read_lock_bh(&tbl->lock);
2574
Eric Dumazet4bd6683b2012-06-07 04:58:35 +00002575 for (h = s_h; h <= PNEIGH_HASHMASK; h++) {
Tony Zelenoff84920c12012-01-26 22:28:58 +00002576 if (h > s_h)
2577 s_idx = 0;
2578 for (n = tbl->phash_buckets[h], idx = 0; n; n = n->next) {
Zhang Shengju18502ac2016-11-30 11:24:42 +08002579 if (idx < s_idx || pneigh_net(n) != net)
Tony Zelenoff84920c12012-01-26 22:28:58 +00002580 goto next;
David Ahern6f52f802018-10-03 15:33:12 -07002581 if (neigh_ifindex_filtered(n->dev, filter->dev_idx) ||
2582 neigh_master_filtered(n->dev, filter->master_idx))
2583 goto next;
Eric W. Biederman15e47302012-09-07 20:12:54 +00002584 if (pneigh_fill_info(skb, n, NETLINK_CB(cb->skb).portid,
Tony Zelenoff84920c12012-01-26 22:28:58 +00002585 cb->nlh->nlmsg_seq,
David Ahern6f52f802018-10-03 15:33:12 -07002586 RTM_NEWNEIGH, flags, tbl) < 0) {
Tony Zelenoff84920c12012-01-26 22:28:58 +00002587 read_unlock_bh(&tbl->lock);
2588 rc = -1;
2589 goto out;
2590 }
2591 next:
2592 idx++;
2593 }
2594 }
2595
2596 read_unlock_bh(&tbl->lock);
2597 rc = skb->len;
2598out:
2599 cb->args[3] = h;
2600 cb->args[4] = idx;
2601 return rc;
2602
2603}
2604
David Ahern51183d22018-10-07 20:16:36 -07002605static int neigh_valid_dump_req(const struct nlmsghdr *nlh,
2606 bool strict_check,
2607 struct neigh_dump_filter *filter,
2608 struct netlink_ext_ack *extack)
2609{
2610 struct nlattr *tb[NDA_MAX + 1];
2611 int err, i;
2612
2613 if (strict_check) {
2614 struct ndmsg *ndm;
2615
2616 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ndm))) {
2617 NL_SET_ERR_MSG(extack, "Invalid header for neighbor dump request");
2618 return -EINVAL;
2619 }
2620
2621 ndm = nlmsg_data(nlh);
2622 if (ndm->ndm_pad1 || ndm->ndm_pad2 || ndm->ndm_ifindex ||
2623 ndm->ndm_state || ndm->ndm_flags || ndm->ndm_type) {
2624 NL_SET_ERR_MSG(extack, "Invalid values in header for neighbor dump request");
2625 return -EINVAL;
2626 }
2627
2628 err = nlmsg_parse_strict(nlh, sizeof(struct ndmsg), tb, NDA_MAX,
2629 NULL, extack);
2630 } else {
2631 err = nlmsg_parse(nlh, sizeof(struct ndmsg), tb, NDA_MAX,
2632 NULL, extack);
2633 }
2634 if (err < 0)
2635 return err;
2636
2637 for (i = 0; i <= NDA_MAX; ++i) {
2638 if (!tb[i])
2639 continue;
2640
2641 /* all new attributes should require strict_check */
2642 switch (i) {
2643 case NDA_IFINDEX:
2644 if (nla_len(tb[i]) != sizeof(u32)) {
2645 NL_SET_ERR_MSG(extack, "Invalid IFINDEX attribute in neighbor dump request");
2646 return -EINVAL;
2647 }
2648 filter->dev_idx = nla_get_u32(tb[i]);
2649 break;
2650 case NDA_MASTER:
2651 if (nla_len(tb[i]) != sizeof(u32)) {
2652 NL_SET_ERR_MSG(extack, "Invalid MASTER attribute in neighbor dump request");
2653 return -EINVAL;
2654 }
2655 filter->master_idx = nla_get_u32(tb[i]);
2656 break;
2657 default:
2658 if (strict_check) {
2659 NL_SET_ERR_MSG(extack, "Unsupported attribute in neighbor dump request");
2660 return -EINVAL;
2661 }
2662 }
2663 }
2664
2665 return 0;
2666}
2667
Thomas Grafc8822a42007-03-22 11:50:06 -07002668static int neigh_dump_info(struct sk_buff *skb, struct netlink_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669{
David Ahern6f52f802018-10-03 15:33:12 -07002670 const struct nlmsghdr *nlh = cb->nlh;
2671 struct neigh_dump_filter filter = {};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672 struct neigh_table *tbl;
2673 int t, family, s_t;
Tony Zelenoff84920c12012-01-26 22:28:58 +00002674 int proxy = 0;
Eric Dumazet4bd6683b2012-06-07 04:58:35 +00002675 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676
David Ahern6f52f802018-10-03 15:33:12 -07002677 family = ((struct rtgenmsg *)nlmsg_data(nlh))->rtgen_family;
Tony Zelenoff84920c12012-01-26 22:28:58 +00002678
2679 /* check for full ndmsg structure presence, family member is
2680 * the same for both structures
2681 */
David Ahern6f52f802018-10-03 15:33:12 -07002682 if (nlmsg_len(nlh) >= sizeof(struct ndmsg) &&
2683 ((struct ndmsg *)nlmsg_data(nlh))->ndm_flags == NTF_PROXY)
Tony Zelenoff84920c12012-01-26 22:28:58 +00002684 proxy = 1;
2685
David Ahern51183d22018-10-07 20:16:36 -07002686 err = neigh_valid_dump_req(nlh, cb->strict_check, &filter, cb->extack);
2687 if (err < 0 && cb->strict_check)
2688 return err;
2689
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690 s_t = cb->args[0];
2691
WANG Congd7480fd2014-11-10 15:59:36 -08002692 for (t = 0; t < NEIGH_NR_TABLES; t++) {
2693 tbl = neigh_tables[t];
2694
2695 if (!tbl)
2696 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697 if (t < s_t || (family && tbl->family != family))
2698 continue;
2699 if (t > s_t)
2700 memset(&cb->args[1], 0, sizeof(cb->args) -
2701 sizeof(cb->args[0]));
Tony Zelenoff84920c12012-01-26 22:28:58 +00002702 if (proxy)
David Ahern6f52f802018-10-03 15:33:12 -07002703 err = pneigh_dump_table(tbl, skb, cb, &filter);
Tony Zelenoff84920c12012-01-26 22:28:58 +00002704 else
David Ahern6f52f802018-10-03 15:33:12 -07002705 err = neigh_dump_table(tbl, skb, cb, &filter);
Eric Dumazet4bd6683b2012-06-07 04:58:35 +00002706 if (err < 0)
2707 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709
2710 cb->args[0] = t;
2711 return skb->len;
2712}
2713
2714void neigh_for_each(struct neigh_table *tbl, void (*cb)(struct neighbour *, void *), void *cookie)
2715{
2716 int chain;
Eric Dumazetd6bf7812010-10-04 06:15:44 +00002717 struct neigh_hash_table *nht;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718
Eric Dumazetd6bf7812010-10-04 06:15:44 +00002719 rcu_read_lock_bh();
2720 nht = rcu_dereference_bh(tbl->nht);
2721
Eric Dumazet767e97e2010-10-06 17:49:21 -07002722 read_lock(&tbl->lock); /* avoid resizes */
David S. Millercd089332011-07-11 01:28:12 -07002723 for (chain = 0; chain < (1 << nht->hash_shift); chain++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724 struct neighbour *n;
2725
Eric Dumazet767e97e2010-10-06 17:49:21 -07002726 for (n = rcu_dereference_bh(nht->hash_buckets[chain]);
2727 n != NULL;
2728 n = rcu_dereference_bh(n->next))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729 cb(n, cookie);
2730 }
Eric Dumazetd6bf7812010-10-04 06:15:44 +00002731 read_unlock(&tbl->lock);
2732 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733}
2734EXPORT_SYMBOL(neigh_for_each);
2735
2736/* The tbl->lock must be held as a writer and BH disabled. */
2737void __neigh_for_each_release(struct neigh_table *tbl,
2738 int (*cb)(struct neighbour *))
2739{
2740 int chain;
Eric Dumazetd6bf7812010-10-04 06:15:44 +00002741 struct neigh_hash_table *nht;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742
Eric Dumazetd6bf7812010-10-04 06:15:44 +00002743 nht = rcu_dereference_protected(tbl->nht,
2744 lockdep_is_held(&tbl->lock));
David S. Millercd089332011-07-11 01:28:12 -07002745 for (chain = 0; chain < (1 << nht->hash_shift); chain++) {
Eric Dumazet767e97e2010-10-06 17:49:21 -07002746 struct neighbour *n;
2747 struct neighbour __rcu **np;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748
Eric Dumazetd6bf7812010-10-04 06:15:44 +00002749 np = &nht->hash_buckets[chain];
Eric Dumazet767e97e2010-10-06 17:49:21 -07002750 while ((n = rcu_dereference_protected(*np,
2751 lockdep_is_held(&tbl->lock))) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752 int release;
2753
2754 write_lock(&n->lock);
2755 release = cb(n);
2756 if (release) {
Eric Dumazet767e97e2010-10-06 17:49:21 -07002757 rcu_assign_pointer(*np,
2758 rcu_dereference_protected(n->next,
2759 lockdep_is_held(&tbl->lock)));
David Ahern58956312018-12-07 12:24:57 -08002760 neigh_mark_dead(n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761 } else
2762 np = &n->next;
2763 write_unlock(&n->lock);
Thomas Graf4f494552007-08-08 23:12:36 -07002764 if (release)
2765 neigh_cleanup_and_release(n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766 }
2767 }
2768}
2769EXPORT_SYMBOL(__neigh_for_each_release);
2770
Eric W. Biedermanb79bda32015-03-07 16:25:56 -06002771int neigh_xmit(int index, struct net_device *dev,
Eric W. Biederman4fd3d7d2015-03-03 17:11:16 -06002772 const void *addr, struct sk_buff *skb)
2773{
Eric W. Biedermanb79bda32015-03-07 16:25:56 -06002774 int err = -EAFNOSUPPORT;
2775 if (likely(index < NEIGH_NR_TABLES)) {
Eric W. Biederman4fd3d7d2015-03-03 17:11:16 -06002776 struct neigh_table *tbl;
2777 struct neighbour *neigh;
2778
Eric W. Biedermanb79bda32015-03-07 16:25:56 -06002779 tbl = neigh_tables[index];
Eric W. Biederman4fd3d7d2015-03-03 17:11:16 -06002780 if (!tbl)
2781 goto out;
David Barrosob560f032016-06-28 11:16:43 +03002782 rcu_read_lock_bh();
Eric W. Biederman4fd3d7d2015-03-03 17:11:16 -06002783 neigh = __neigh_lookup_noref(tbl, addr, dev);
2784 if (!neigh)
2785 neigh = __neigh_create(tbl, addr, dev, false);
2786 err = PTR_ERR(neigh);
David Barrosob560f032016-06-28 11:16:43 +03002787 if (IS_ERR(neigh)) {
2788 rcu_read_unlock_bh();
Eric W. Biederman4fd3d7d2015-03-03 17:11:16 -06002789 goto out_kfree_skb;
David Barrosob560f032016-06-28 11:16:43 +03002790 }
Eric W. Biederman4fd3d7d2015-03-03 17:11:16 -06002791 err = neigh->output(neigh, skb);
David Barrosob560f032016-06-28 11:16:43 +03002792 rcu_read_unlock_bh();
Eric W. Biederman4fd3d7d2015-03-03 17:11:16 -06002793 }
Eric W. Biedermanb79bda32015-03-07 16:25:56 -06002794 else if (index == NEIGH_LINK_TABLE) {
2795 err = dev_hard_header(skb, dev, ntohs(skb->protocol),
2796 addr, NULL, skb->len);
2797 if (err < 0)
2798 goto out_kfree_skb;
2799 err = dev_queue_xmit(skb);
2800 }
Eric W. Biederman4fd3d7d2015-03-03 17:11:16 -06002801out:
2802 return err;
2803out_kfree_skb:
2804 kfree_skb(skb);
2805 goto out;
2806}
2807EXPORT_SYMBOL(neigh_xmit);
2808
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809#ifdef CONFIG_PROC_FS
2810
2811static struct neighbour *neigh_get_first(struct seq_file *seq)
2812{
2813 struct neigh_seq_state *state = seq->private;
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09002814 struct net *net = seq_file_net(seq);
Eric Dumazetd6bf7812010-10-04 06:15:44 +00002815 struct neigh_hash_table *nht = state->nht;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816 struct neighbour *n = NULL;
2817 int bucket = state->bucket;
2818
2819 state->flags &= ~NEIGH_SEQ_IS_PNEIGH;
David S. Millercd089332011-07-11 01:28:12 -07002820 for (bucket = 0; bucket < (1 << nht->hash_shift); bucket++) {
Eric Dumazet767e97e2010-10-06 17:49:21 -07002821 n = rcu_dereference_bh(nht->hash_buckets[bucket]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822
2823 while (n) {
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09002824 if (!net_eq(dev_net(n->dev), net))
Eric W. Biederman426b5302008-01-24 00:13:18 -08002825 goto next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826 if (state->neigh_sub_iter) {
2827 loff_t fakep = 0;
2828 void *v;
2829
2830 v = state->neigh_sub_iter(state, n, &fakep);
2831 if (!v)
2832 goto next;
2833 }
2834 if (!(state->flags & NEIGH_SEQ_SKIP_NOARP))
2835 break;
2836 if (n->nud_state & ~NUD_NOARP)
2837 break;
Eric Dumazet767e97e2010-10-06 17:49:21 -07002838next:
2839 n = rcu_dereference_bh(n->next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840 }
2841
2842 if (n)
2843 break;
2844 }
2845 state->bucket = bucket;
2846
2847 return n;
2848}
2849
2850static struct neighbour *neigh_get_next(struct seq_file *seq,
2851 struct neighbour *n,
2852 loff_t *pos)
2853{
2854 struct neigh_seq_state *state = seq->private;
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09002855 struct net *net = seq_file_net(seq);
Eric Dumazetd6bf7812010-10-04 06:15:44 +00002856 struct neigh_hash_table *nht = state->nht;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857
2858 if (state->neigh_sub_iter) {
2859 void *v = state->neigh_sub_iter(state, n, pos);
2860 if (v)
2861 return n;
2862 }
Eric Dumazet767e97e2010-10-06 17:49:21 -07002863 n = rcu_dereference_bh(n->next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864
2865 while (1) {
2866 while (n) {
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09002867 if (!net_eq(dev_net(n->dev), net))
Eric W. Biederman426b5302008-01-24 00:13:18 -08002868 goto next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869 if (state->neigh_sub_iter) {
2870 void *v = state->neigh_sub_iter(state, n, pos);
2871 if (v)
2872 return n;
2873 goto next;
2874 }
2875 if (!(state->flags & NEIGH_SEQ_SKIP_NOARP))
2876 break;
2877
2878 if (n->nud_state & ~NUD_NOARP)
2879 break;
Eric Dumazet767e97e2010-10-06 17:49:21 -07002880next:
2881 n = rcu_dereference_bh(n->next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882 }
2883
2884 if (n)
2885 break;
2886
David S. Millercd089332011-07-11 01:28:12 -07002887 if (++state->bucket >= (1 << nht->hash_shift))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888 break;
2889
Eric Dumazet767e97e2010-10-06 17:49:21 -07002890 n = rcu_dereference_bh(nht->hash_buckets[state->bucket]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891 }
2892
2893 if (n && pos)
2894 --(*pos);
2895 return n;
2896}
2897
2898static struct neighbour *neigh_get_idx(struct seq_file *seq, loff_t *pos)
2899{
2900 struct neighbour *n = neigh_get_first(seq);
2901
2902 if (n) {
Chris Larson745e2032008-08-03 01:10:55 -07002903 --(*pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904 while (*pos) {
2905 n = neigh_get_next(seq, n, pos);
2906 if (!n)
2907 break;
2908 }
2909 }
2910 return *pos ? NULL : n;
2911}
2912
2913static struct pneigh_entry *pneigh_get_first(struct seq_file *seq)
2914{
2915 struct neigh_seq_state *state = seq->private;
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09002916 struct net *net = seq_file_net(seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917 struct neigh_table *tbl = state->tbl;
2918 struct pneigh_entry *pn = NULL;
2919 int bucket = state->bucket;
2920
2921 state->flags |= NEIGH_SEQ_IS_PNEIGH;
2922 for (bucket = 0; bucket <= PNEIGH_HASHMASK; bucket++) {
2923 pn = tbl->phash_buckets[bucket];
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09002924 while (pn && !net_eq(pneigh_net(pn), net))
Eric W. Biederman426b5302008-01-24 00:13:18 -08002925 pn = pn->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926 if (pn)
2927 break;
2928 }
2929 state->bucket = bucket;
2930
2931 return pn;
2932}
2933
2934static struct pneigh_entry *pneigh_get_next(struct seq_file *seq,
2935 struct pneigh_entry *pn,
2936 loff_t *pos)
2937{
2938 struct neigh_seq_state *state = seq->private;
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09002939 struct net *net = seq_file_net(seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940 struct neigh_table *tbl = state->tbl;
2941
Jorge Boncompte [DTI2]df07a942011-11-25 13:24:49 -05002942 do {
2943 pn = pn->next;
2944 } while (pn && !net_eq(pneigh_net(pn), net));
2945
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946 while (!pn) {
2947 if (++state->bucket > PNEIGH_HASHMASK)
2948 break;
2949 pn = tbl->phash_buckets[state->bucket];
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09002950 while (pn && !net_eq(pneigh_net(pn), net))
Eric W. Biederman426b5302008-01-24 00:13:18 -08002951 pn = pn->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952 if (pn)
2953 break;
2954 }
2955
2956 if (pn && pos)
2957 --(*pos);
2958
2959 return pn;
2960}
2961
2962static struct pneigh_entry *pneigh_get_idx(struct seq_file *seq, loff_t *pos)
2963{
2964 struct pneigh_entry *pn = pneigh_get_first(seq);
2965
2966 if (pn) {
Chris Larson745e2032008-08-03 01:10:55 -07002967 --(*pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968 while (*pos) {
2969 pn = pneigh_get_next(seq, pn, pos);
2970 if (!pn)
2971 break;
2972 }
2973 }
2974 return *pos ? NULL : pn;
2975}
2976
2977static void *neigh_get_idx_any(struct seq_file *seq, loff_t *pos)
2978{
2979 struct neigh_seq_state *state = seq->private;
2980 void *rc;
Chris Larson745e2032008-08-03 01:10:55 -07002981 loff_t idxpos = *pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982
Chris Larson745e2032008-08-03 01:10:55 -07002983 rc = neigh_get_idx(seq, &idxpos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984 if (!rc && !(state->flags & NEIGH_SEQ_NEIGH_ONLY))
Chris Larson745e2032008-08-03 01:10:55 -07002985 rc = pneigh_get_idx(seq, &idxpos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986
2987 return rc;
2988}
2989
2990void *neigh_seq_start(struct seq_file *seq, loff_t *pos, struct neigh_table *tbl, unsigned int neigh_seq_flags)
Eric Dumazetd6bf7812010-10-04 06:15:44 +00002991 __acquires(rcu_bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992{
2993 struct neigh_seq_state *state = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994
2995 state->tbl = tbl;
2996 state->bucket = 0;
2997 state->flags = (neigh_seq_flags & ~NEIGH_SEQ_IS_PNEIGH);
2998
Eric Dumazetd6bf7812010-10-04 06:15:44 +00002999 rcu_read_lock_bh();
3000 state->nht = rcu_dereference_bh(tbl->nht);
Eric Dumazet767e97e2010-10-06 17:49:21 -07003001
Chris Larson745e2032008-08-03 01:10:55 -07003002 return *pos ? neigh_get_idx_any(seq, pos) : SEQ_START_TOKEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003}
3004EXPORT_SYMBOL(neigh_seq_start);
3005
3006void *neigh_seq_next(struct seq_file *seq, void *v, loff_t *pos)
3007{
3008 struct neigh_seq_state *state;
3009 void *rc;
3010
3011 if (v == SEQ_START_TOKEN) {
Chris Larsonbff69732008-08-03 01:02:41 -07003012 rc = neigh_get_first(seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013 goto out;
3014 }
3015
3016 state = seq->private;
3017 if (!(state->flags & NEIGH_SEQ_IS_PNEIGH)) {
3018 rc = neigh_get_next(seq, v, NULL);
3019 if (rc)
3020 goto out;
3021 if (!(state->flags & NEIGH_SEQ_NEIGH_ONLY))
3022 rc = pneigh_get_first(seq);
3023 } else {
3024 BUG_ON(state->flags & NEIGH_SEQ_NEIGH_ONLY);
3025 rc = pneigh_get_next(seq, v, NULL);
3026 }
3027out:
3028 ++(*pos);
3029 return rc;
3030}
3031EXPORT_SYMBOL(neigh_seq_next);
3032
3033void neigh_seq_stop(struct seq_file *seq, void *v)
Eric Dumazetd6bf7812010-10-04 06:15:44 +00003034 __releases(rcu_bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035{
Eric Dumazetd6bf7812010-10-04 06:15:44 +00003036 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037}
3038EXPORT_SYMBOL(neigh_seq_stop);
3039
3040/* statistics via seq_file */
3041
3042static void *neigh_stat_seq_start(struct seq_file *seq, loff_t *pos)
3043{
Christoph Hellwig71a50532018-04-15 10:16:41 +02003044 struct neigh_table *tbl = PDE_DATA(file_inode(seq->file));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045 int cpu;
3046
3047 if (*pos == 0)
3048 return SEQ_START_TOKEN;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003049
Rusty Russell0f23174a2008-12-29 12:23:42 +00003050 for (cpu = *pos-1; cpu < nr_cpu_ids; ++cpu) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051 if (!cpu_possible(cpu))
3052 continue;
3053 *pos = cpu+1;
3054 return per_cpu_ptr(tbl->stats, cpu);
3055 }
3056 return NULL;
3057}
3058
3059static void *neigh_stat_seq_next(struct seq_file *seq, void *v, loff_t *pos)
3060{
Christoph Hellwig71a50532018-04-15 10:16:41 +02003061 struct neigh_table *tbl = PDE_DATA(file_inode(seq->file));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062 int cpu;
3063
Rusty Russell0f23174a2008-12-29 12:23:42 +00003064 for (cpu = *pos; cpu < nr_cpu_ids; ++cpu) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065 if (!cpu_possible(cpu))
3066 continue;
3067 *pos = cpu+1;
3068 return per_cpu_ptr(tbl->stats, cpu);
3069 }
3070 return NULL;
3071}
3072
3073static void neigh_stat_seq_stop(struct seq_file *seq, void *v)
3074{
3075
3076}
3077
3078static int neigh_stat_seq_show(struct seq_file *seq, void *v)
3079{
Christoph Hellwig71a50532018-04-15 10:16:41 +02003080 struct neigh_table *tbl = PDE_DATA(file_inode(seq->file));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081 struct neigh_statistics *st = v;
3082
3083 if (v == SEQ_START_TOKEN) {
Rick Jonesfb811392015-08-07 11:10:37 -07003084 seq_printf(seq, "entries allocs destroys hash_grows lookups hits res_failed rcv_probes_mcast rcv_probes_ucast periodic_gc_runs forced_gc_runs unresolved_discards table_fulls\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085 return 0;
3086 }
3087
3088 seq_printf(seq, "%08x %08lx %08lx %08lx %08lx %08lx %08lx "
Rick Jonesfb811392015-08-07 11:10:37 -07003089 "%08lx %08lx %08lx %08lx %08lx %08lx\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090 atomic_read(&tbl->entries),
3091
3092 st->allocs,
3093 st->destroys,
3094 st->hash_grows,
3095
3096 st->lookups,
3097 st->hits,
3098
3099 st->res_failed,
3100
3101 st->rcv_probes_mcast,
3102 st->rcv_probes_ucast,
3103
3104 st->periodic_gc_runs,
Neil Horman9a6d2762008-07-16 20:50:49 -07003105 st->forced_gc_runs,
Rick Jonesfb811392015-08-07 11:10:37 -07003106 st->unres_discards,
3107 st->table_fulls
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108 );
3109
3110 return 0;
3111}
3112
Stephen Hemmingerf6908082007-03-12 14:34:29 -07003113static const struct seq_operations neigh_stat_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114 .start = neigh_stat_seq_start,
3115 .next = neigh_stat_seq_next,
3116 .stop = neigh_stat_seq_stop,
3117 .show = neigh_stat_seq_show,
3118};
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119#endif /* CONFIG_PROC_FS */
3120
Thomas Graf339bf982006-11-10 14:10:15 -08003121static inline size_t neigh_nlmsg_size(void)
3122{
3123 return NLMSG_ALIGN(sizeof(struct ndmsg))
3124 + nla_total_size(MAX_ADDR_LEN) /* NDA_DST */
3125 + nla_total_size(MAX_ADDR_LEN) /* NDA_LLADDR */
3126 + nla_total_size(sizeof(struct nda_cacheinfo))
David Aherndf9b0e32018-12-15 14:09:06 -08003127 + nla_total_size(4) /* NDA_PROBES */
3128 + nla_total_size(1); /* NDA_PROTOCOL */
Thomas Graf339bf982006-11-10 14:10:15 -08003129}
3130
Roopa Prabhu7b8f7a42017-03-19 22:01:28 -07003131static void __neigh_notify(struct neighbour *n, int type, int flags,
3132 u32 pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133{
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09003134 struct net *net = dev_net(n->dev);
Thomas Graf8b8aec52006-08-07 17:56:37 -07003135 struct sk_buff *skb;
Thomas Grafb8673312006-08-15 00:33:14 -07003136 int err = -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003137
Thomas Graf339bf982006-11-10 14:10:15 -08003138 skb = nlmsg_new(neigh_nlmsg_size(), GFP_ATOMIC);
Thomas Graf8b8aec52006-08-07 17:56:37 -07003139 if (skb == NULL)
Thomas Grafb8673312006-08-15 00:33:14 -07003140 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141
Roopa Prabhu7b8f7a42017-03-19 22:01:28 -07003142 err = neigh_fill_info(skb, n, pid, 0, type, flags);
Patrick McHardy26932562007-01-31 23:16:40 -08003143 if (err < 0) {
3144 /* -EMSGSIZE implies BUG in neigh_nlmsg_size() */
3145 WARN_ON(err == -EMSGSIZE);
3146 kfree_skb(skb);
3147 goto errout;
3148 }
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -08003149 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
3150 return;
Thomas Grafb8673312006-08-15 00:33:14 -07003151errout:
3152 if (err < 0)
Eric W. Biederman426b5302008-01-24 00:13:18 -08003153 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
Thomas Grafb8673312006-08-15 00:33:14 -07003154}
3155
3156void neigh_app_ns(struct neighbour *n)
3157{
Roopa Prabhu7b8f7a42017-03-19 22:01:28 -07003158 __neigh_notify(n, RTM_GETNEIGH, NLM_F_REQUEST, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159}
YOSHIFUJI Hideaki0a204502008-03-24 18:39:10 +09003160EXPORT_SYMBOL(neigh_app_ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161
3162#ifdef CONFIG_SYSCTL
Cong Wangb93196d2012-12-06 10:04:04 +08003163static int zero;
Francesco Fusco555445c2013-07-24 10:39:06 +02003164static int int_max = INT_MAX;
Cong Wangb93196d2012-12-06 10:04:04 +08003165static int unres_qlen_max = INT_MAX / SKB_TRUESIZE(ETH_FRAME_LEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003166
Joe Perchesfe2c6332013-06-11 23:04:25 -07003167static int proc_unres_qlen(struct ctl_table *ctl, int write,
3168 void __user *buffer, size_t *lenp, loff_t *ppos)
Eric Dumazet8b5c1712011-11-09 12:07:14 +00003169{
3170 int size, ret;
Joe Perchesfe2c6332013-06-11 23:04:25 -07003171 struct ctl_table tmp = *ctl;
Eric Dumazet8b5c1712011-11-09 12:07:14 +00003172
Shan Weice46cc62012-12-04 18:49:15 +00003173 tmp.extra1 = &zero;
3174 tmp.extra2 = &unres_qlen_max;
Eric Dumazet8b5c1712011-11-09 12:07:14 +00003175 tmp.data = &size;
Shan Weice46cc62012-12-04 18:49:15 +00003176
3177 size = *(int *)ctl->data / SKB_TRUESIZE(ETH_FRAME_LEN);
3178 ret = proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos);
3179
Eric Dumazet8b5c1712011-11-09 12:07:14 +00003180 if (write && !ret)
3181 *(int *)ctl->data = size * SKB_TRUESIZE(ETH_FRAME_LEN);
3182 return ret;
3183}
3184
Jiri Pirko1d4c8c22013-12-07 19:26:56 +01003185static struct neigh_parms *neigh_get_dev_parms_rcu(struct net_device *dev,
3186 int family)
3187{
Jiri Pirkobba24892013-12-07 19:26:57 +01003188 switch (family) {
3189 case AF_INET:
Jiri Pirko1d4c8c22013-12-07 19:26:56 +01003190 return __in_dev_arp_parms_get_rcu(dev);
Jiri Pirkobba24892013-12-07 19:26:57 +01003191 case AF_INET6:
3192 return __in6_dev_nd_parms_get_rcu(dev);
3193 }
Jiri Pirko1d4c8c22013-12-07 19:26:56 +01003194 return NULL;
3195}
3196
3197static void neigh_copy_dflt_parms(struct net *net, struct neigh_parms *p,
3198 int index)
3199{
3200 struct net_device *dev;
3201 int family = neigh_parms_family(p);
3202
3203 rcu_read_lock();
3204 for_each_netdev_rcu(net, dev) {
3205 struct neigh_parms *dst_p =
3206 neigh_get_dev_parms_rcu(dev, family);
3207
3208 if (dst_p && !test_bit(index, dst_p->data_state))
3209 dst_p->data[index] = p->data[index];
3210 }
3211 rcu_read_unlock();
3212}
3213
3214static void neigh_proc_update(struct ctl_table *ctl, int write)
3215{
3216 struct net_device *dev = ctl->extra1;
3217 struct neigh_parms *p = ctl->extra2;
Jiri Pirko77d47af2013-12-10 23:55:07 +01003218 struct net *net = neigh_parms_net(p);
Jiri Pirko1d4c8c22013-12-07 19:26:56 +01003219 int index = (int *) ctl->data - p->data;
3220
3221 if (!write)
3222 return;
3223
3224 set_bit(index, p->data_state);
Marcus Huewe7627ae62017-02-15 01:00:36 +01003225 if (index == NEIGH_VAR_DELAY_PROBE_TIME)
3226 call_netevent_notifiers(NETEVENT_DELAY_PROBE_TIME_UPDATE, p);
Jiri Pirko1d4c8c22013-12-07 19:26:56 +01003227 if (!dev) /* NULL dev means this is default value */
3228 neigh_copy_dflt_parms(net, p, index);
3229}
3230
Jiri Pirko1f9248e2013-12-07 19:26:53 +01003231static int neigh_proc_dointvec_zero_intmax(struct ctl_table *ctl, int write,
3232 void __user *buffer,
3233 size_t *lenp, loff_t *ppos)
3234{
3235 struct ctl_table tmp = *ctl;
Jiri Pirko1d4c8c22013-12-07 19:26:56 +01003236 int ret;
Jiri Pirko1f9248e2013-12-07 19:26:53 +01003237
3238 tmp.extra1 = &zero;
3239 tmp.extra2 = &int_max;
3240
Jiri Pirko1d4c8c22013-12-07 19:26:56 +01003241 ret = proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos);
3242 neigh_proc_update(ctl, write);
3243 return ret;
Jiri Pirko1f9248e2013-12-07 19:26:53 +01003244}
3245
Jiri Pirkocb5b09c2013-12-07 19:26:54 +01003246int neigh_proc_dointvec(struct ctl_table *ctl, int write,
3247 void __user *buffer, size_t *lenp, loff_t *ppos)
3248{
Jiri Pirko1d4c8c22013-12-07 19:26:56 +01003249 int ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
3250
3251 neigh_proc_update(ctl, write);
3252 return ret;
Jiri Pirkocb5b09c2013-12-07 19:26:54 +01003253}
3254EXPORT_SYMBOL(neigh_proc_dointvec);
3255
3256int neigh_proc_dointvec_jiffies(struct ctl_table *ctl, int write,
3257 void __user *buffer,
3258 size_t *lenp, loff_t *ppos)
3259{
Jiri Pirko1d4c8c22013-12-07 19:26:56 +01003260 int ret = proc_dointvec_jiffies(ctl, write, buffer, lenp, ppos);
3261
3262 neigh_proc_update(ctl, write);
3263 return ret;
Jiri Pirkocb5b09c2013-12-07 19:26:54 +01003264}
3265EXPORT_SYMBOL(neigh_proc_dointvec_jiffies);
3266
3267static int neigh_proc_dointvec_userhz_jiffies(struct ctl_table *ctl, int write,
3268 void __user *buffer,
3269 size_t *lenp, loff_t *ppos)
3270{
Jiri Pirko1d4c8c22013-12-07 19:26:56 +01003271 int ret = proc_dointvec_userhz_jiffies(ctl, write, buffer, lenp, ppos);
3272
3273 neigh_proc_update(ctl, write);
3274 return ret;
Jiri Pirkocb5b09c2013-12-07 19:26:54 +01003275}
3276
3277int neigh_proc_dointvec_ms_jiffies(struct ctl_table *ctl, int write,
3278 void __user *buffer,
3279 size_t *lenp, loff_t *ppos)
3280{
Jiri Pirko1d4c8c22013-12-07 19:26:56 +01003281 int ret = proc_dointvec_ms_jiffies(ctl, write, buffer, lenp, ppos);
3282
3283 neigh_proc_update(ctl, write);
3284 return ret;
Jiri Pirkocb5b09c2013-12-07 19:26:54 +01003285}
3286EXPORT_SYMBOL(neigh_proc_dointvec_ms_jiffies);
3287
3288static int neigh_proc_dointvec_unres_qlen(struct ctl_table *ctl, int write,
3289 void __user *buffer,
3290 size_t *lenp, loff_t *ppos)
3291{
Jiri Pirko1d4c8c22013-12-07 19:26:56 +01003292 int ret = proc_unres_qlen(ctl, write, buffer, lenp, ppos);
3293
3294 neigh_proc_update(ctl, write);
3295 return ret;
Jiri Pirkocb5b09c2013-12-07 19:26:54 +01003296}
3297
Jean-Francois Remy4bf69802015-01-14 04:22:39 +01003298static int neigh_proc_base_reachable_time(struct ctl_table *ctl, int write,
3299 void __user *buffer,
3300 size_t *lenp, loff_t *ppos)
3301{
3302 struct neigh_parms *p = ctl->extra2;
3303 int ret;
3304
3305 if (strcmp(ctl->procname, "base_reachable_time") == 0)
3306 ret = neigh_proc_dointvec_jiffies(ctl, write, buffer, lenp, ppos);
3307 else if (strcmp(ctl->procname, "base_reachable_time_ms") == 0)
3308 ret = neigh_proc_dointvec_ms_jiffies(ctl, write, buffer, lenp, ppos);
3309 else
3310 ret = -1;
3311
3312 if (write && ret == 0) {
3313 /* update reachable_time as well, otherwise, the change will
3314 * only be effective after the next time neigh_periodic_work
3315 * decides to recompute it
3316 */
3317 p->reachable_time =
3318 neigh_rand_reach_time(NEIGH_VAR(p, BASE_REACHABLE_TIME));
3319 }
3320 return ret;
3321}
3322
Jiri Pirko1f9248e2013-12-07 19:26:53 +01003323#define NEIGH_PARMS_DATA_OFFSET(index) \
3324 (&((struct neigh_parms *) 0)->data[index])
3325
3326#define NEIGH_SYSCTL_ENTRY(attr, data_attr, name, mval, proc) \
3327 [NEIGH_VAR_ ## attr] = { \
3328 .procname = name, \
3329 .data = NEIGH_PARMS_DATA_OFFSET(NEIGH_VAR_ ## data_attr), \
3330 .maxlen = sizeof(int), \
3331 .mode = mval, \
3332 .proc_handler = proc, \
3333 }
3334
3335#define NEIGH_SYSCTL_ZERO_INTMAX_ENTRY(attr, name) \
3336 NEIGH_SYSCTL_ENTRY(attr, attr, name, 0644, neigh_proc_dointvec_zero_intmax)
3337
3338#define NEIGH_SYSCTL_JIFFIES_ENTRY(attr, name) \
Jiri Pirkocb5b09c2013-12-07 19:26:54 +01003339 NEIGH_SYSCTL_ENTRY(attr, attr, name, 0644, neigh_proc_dointvec_jiffies)
Jiri Pirko1f9248e2013-12-07 19:26:53 +01003340
3341#define NEIGH_SYSCTL_USERHZ_JIFFIES_ENTRY(attr, name) \
Jiri Pirkocb5b09c2013-12-07 19:26:54 +01003342 NEIGH_SYSCTL_ENTRY(attr, attr, name, 0644, neigh_proc_dointvec_userhz_jiffies)
Jiri Pirko1f9248e2013-12-07 19:26:53 +01003343
3344#define NEIGH_SYSCTL_MS_JIFFIES_ENTRY(attr, name) \
Jiri Pirkocb5b09c2013-12-07 19:26:54 +01003345 NEIGH_SYSCTL_ENTRY(attr, attr, name, 0644, neigh_proc_dointvec_ms_jiffies)
Jiri Pirko1f9248e2013-12-07 19:26:53 +01003346
3347#define NEIGH_SYSCTL_MS_JIFFIES_REUSED_ENTRY(attr, data_attr, name) \
Jiri Pirkocb5b09c2013-12-07 19:26:54 +01003348 NEIGH_SYSCTL_ENTRY(attr, data_attr, name, 0644, neigh_proc_dointvec_ms_jiffies)
Jiri Pirko1f9248e2013-12-07 19:26:53 +01003349
3350#define NEIGH_SYSCTL_UNRES_QLEN_REUSED_ENTRY(attr, data_attr, name) \
Jiri Pirkocb5b09c2013-12-07 19:26:54 +01003351 NEIGH_SYSCTL_ENTRY(attr, data_attr, name, 0644, neigh_proc_dointvec_unres_qlen)
Eric W. Biederman54716e32010-02-14 03:27:03 +00003352
Linus Torvalds1da177e2005-04-16 15:20:36 -07003353static struct neigh_sysctl_table {
3354 struct ctl_table_header *sysctl_header;
Eric Dumazet8b5c1712011-11-09 12:07:14 +00003355 struct ctl_table neigh_vars[NEIGH_VAR_MAX + 1];
Brian Haleyab32ea52006-09-22 14:15:41 -07003356} neigh_sysctl_template __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357 .neigh_vars = {
Jiri Pirko1f9248e2013-12-07 19:26:53 +01003358 NEIGH_SYSCTL_ZERO_INTMAX_ENTRY(MCAST_PROBES, "mcast_solicit"),
3359 NEIGH_SYSCTL_ZERO_INTMAX_ENTRY(UCAST_PROBES, "ucast_solicit"),
3360 NEIGH_SYSCTL_ZERO_INTMAX_ENTRY(APP_PROBES, "app_solicit"),
YOSHIFUJI Hideaki/吉藤英明8da86462015-03-19 22:41:46 +09003361 NEIGH_SYSCTL_ZERO_INTMAX_ENTRY(MCAST_REPROBES, "mcast_resolicit"),
Jiri Pirko1f9248e2013-12-07 19:26:53 +01003362 NEIGH_SYSCTL_USERHZ_JIFFIES_ENTRY(RETRANS_TIME, "retrans_time"),
3363 NEIGH_SYSCTL_JIFFIES_ENTRY(BASE_REACHABLE_TIME, "base_reachable_time"),
3364 NEIGH_SYSCTL_JIFFIES_ENTRY(DELAY_PROBE_TIME, "delay_first_probe_time"),
3365 NEIGH_SYSCTL_JIFFIES_ENTRY(GC_STALETIME, "gc_stale_time"),
3366 NEIGH_SYSCTL_ZERO_INTMAX_ENTRY(QUEUE_LEN_BYTES, "unres_qlen_bytes"),
3367 NEIGH_SYSCTL_ZERO_INTMAX_ENTRY(PROXY_QLEN, "proxy_qlen"),
3368 NEIGH_SYSCTL_USERHZ_JIFFIES_ENTRY(ANYCAST_DELAY, "anycast_delay"),
3369 NEIGH_SYSCTL_USERHZ_JIFFIES_ENTRY(PROXY_DELAY, "proxy_delay"),
3370 NEIGH_SYSCTL_USERHZ_JIFFIES_ENTRY(LOCKTIME, "locktime"),
3371 NEIGH_SYSCTL_UNRES_QLEN_REUSED_ENTRY(QUEUE_LEN, QUEUE_LEN_BYTES, "unres_qlen"),
3372 NEIGH_SYSCTL_MS_JIFFIES_REUSED_ENTRY(RETRANS_TIME_MS, RETRANS_TIME, "retrans_time_ms"),
3373 NEIGH_SYSCTL_MS_JIFFIES_REUSED_ENTRY(BASE_REACHABLE_TIME_MS, BASE_REACHABLE_TIME, "base_reachable_time_ms"),
Eric Dumazet8b5c1712011-11-09 12:07:14 +00003374 [NEIGH_VAR_GC_INTERVAL] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375 .procname = "gc_interval",
3376 .maxlen = sizeof(int),
3377 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08003378 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379 },
Eric Dumazet8b5c1712011-11-09 12:07:14 +00003380 [NEIGH_VAR_GC_THRESH1] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003381 .procname = "gc_thresh1",
3382 .maxlen = sizeof(int),
3383 .mode = 0644,
Francesco Fusco555445c2013-07-24 10:39:06 +02003384 .extra1 = &zero,
3385 .extra2 = &int_max,
3386 .proc_handler = proc_dointvec_minmax,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003387 },
Eric Dumazet8b5c1712011-11-09 12:07:14 +00003388 [NEIGH_VAR_GC_THRESH2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389 .procname = "gc_thresh2",
3390 .maxlen = sizeof(int),
3391 .mode = 0644,
Francesco Fusco555445c2013-07-24 10:39:06 +02003392 .extra1 = &zero,
3393 .extra2 = &int_max,
3394 .proc_handler = proc_dointvec_minmax,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003395 },
Eric Dumazet8b5c1712011-11-09 12:07:14 +00003396 [NEIGH_VAR_GC_THRESH3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003397 .procname = "gc_thresh3",
3398 .maxlen = sizeof(int),
3399 .mode = 0644,
Francesco Fusco555445c2013-07-24 10:39:06 +02003400 .extra1 = &zero,
3401 .extra2 = &int_max,
3402 .proc_handler = proc_dointvec_minmax,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003403 },
Pavel Emelyanovc3bac5a2007-12-02 00:08:16 +11003404 {},
Linus Torvalds1da177e2005-04-16 15:20:36 -07003405 },
3406};
3407
3408int neigh_sysctl_register(struct net_device *dev, struct neigh_parms *p,
Jiri Pirko73af6142013-12-07 19:26:55 +01003409 proc_handler *handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410{
Jiri Pirko1f9248e2013-12-07 19:26:53 +01003411 int i;
Pavel Emelyanov3c607bb2007-12-02 00:06:34 +11003412 struct neigh_sysctl_table *t;
Jiri Pirko1f9248e2013-12-07 19:26:53 +01003413 const char *dev_name_source;
Eric W. Biederman8f40a1f2012-04-19 13:38:03 +00003414 char neigh_path[ sizeof("net//neigh/") + IFNAMSIZ + IFNAMSIZ ];
Jiri Pirko73af6142013-12-07 19:26:55 +01003415 char *p_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003416
Pavel Emelyanov3c607bb2007-12-02 00:06:34 +11003417 t = kmemdup(&neigh_sysctl_template, sizeof(*t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003418 if (!t)
Pavel Emelyanov3c607bb2007-12-02 00:06:34 +11003419 goto err;
3420
Jiri Pirkob194c1f2014-02-21 14:52:57 +01003421 for (i = 0; i < NEIGH_VAR_GC_INTERVAL; i++) {
Jiri Pirko1f9248e2013-12-07 19:26:53 +01003422 t->neigh_vars[i].data += (long) p;
Jiri Pirkocb5b09c2013-12-07 19:26:54 +01003423 t->neigh_vars[i].extra1 = dev;
Jiri Pirko1d4c8c22013-12-07 19:26:56 +01003424 t->neigh_vars[i].extra2 = p;
Jiri Pirkocb5b09c2013-12-07 19:26:54 +01003425 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003426
3427 if (dev) {
3428 dev_name_source = dev->name;
Eric W. Biedermand12af672007-10-18 03:05:25 -07003429 /* Terminate the table early */
Eric Dumazet8b5c1712011-11-09 12:07:14 +00003430 memset(&t->neigh_vars[NEIGH_VAR_GC_INTERVAL], 0,
3431 sizeof(t->neigh_vars[NEIGH_VAR_GC_INTERVAL]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003432 } else {
Mathias Krause9ecf07a2014-07-12 22:36:44 +02003433 struct neigh_table *tbl = p->tbl;
Eric W. Biederman8f40a1f2012-04-19 13:38:03 +00003434 dev_name_source = "default";
Mathias Krause9ecf07a2014-07-12 22:36:44 +02003435 t->neigh_vars[NEIGH_VAR_GC_INTERVAL].data = &tbl->gc_interval;
3436 t->neigh_vars[NEIGH_VAR_GC_THRESH1].data = &tbl->gc_thresh1;
3437 t->neigh_vars[NEIGH_VAR_GC_THRESH2].data = &tbl->gc_thresh2;
3438 t->neigh_vars[NEIGH_VAR_GC_THRESH3].data = &tbl->gc_thresh3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003439 }
3440
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08003441 if (handler) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003442 /* RetransTime */
Eric Dumazet8b5c1712011-11-09 12:07:14 +00003443 t->neigh_vars[NEIGH_VAR_RETRANS_TIME].proc_handler = handler;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003444 /* ReachableTime */
Eric Dumazet8b5c1712011-11-09 12:07:14 +00003445 t->neigh_vars[NEIGH_VAR_BASE_REACHABLE_TIME].proc_handler = handler;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003446 /* RetransTime (in milliseconds)*/
Eric Dumazet8b5c1712011-11-09 12:07:14 +00003447 t->neigh_vars[NEIGH_VAR_RETRANS_TIME_MS].proc_handler = handler;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003448 /* ReachableTime (in milliseconds) */
Eric Dumazet8b5c1712011-11-09 12:07:14 +00003449 t->neigh_vars[NEIGH_VAR_BASE_REACHABLE_TIME_MS].proc_handler = handler;
Jean-Francois Remy4bf69802015-01-14 04:22:39 +01003450 } else {
3451 /* Those handlers will update p->reachable_time after
3452 * base_reachable_time(_ms) is set to ensure the new timer starts being
3453 * applied after the next neighbour update instead of waiting for
3454 * neigh_periodic_work to update its value (can be multiple minutes)
3455 * So any handler that replaces them should do this as well
3456 */
3457 /* ReachableTime */
3458 t->neigh_vars[NEIGH_VAR_BASE_REACHABLE_TIME].proc_handler =
3459 neigh_proc_base_reachable_time;
3460 /* ReachableTime (in milliseconds) */
3461 t->neigh_vars[NEIGH_VAR_BASE_REACHABLE_TIME_MS].proc_handler =
3462 neigh_proc_base_reachable_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003463 }
3464
Eric W. Biederman464dc802012-11-16 03:02:59 +00003465 /* Don't export sysctls to unprivileged users */
3466 if (neigh_parms_net(p)->user_ns != &init_user_ns)
3467 t->neigh_vars[0].procname = NULL;
3468
Jiri Pirko73af6142013-12-07 19:26:55 +01003469 switch (neigh_parms_family(p)) {
3470 case AF_INET:
3471 p_name = "ipv4";
3472 break;
3473 case AF_INET6:
3474 p_name = "ipv6";
3475 break;
3476 default:
3477 BUG();
3478 }
3479
Eric W. Biederman8f40a1f2012-04-19 13:38:03 +00003480 snprintf(neigh_path, sizeof(neigh_path), "net/%s/neigh/%s",
3481 p_name, dev_name_source);
Denis V. Lunev4ab438f2008-02-28 20:48:01 -08003482 t->sysctl_header =
Eric W. Biederman8f40a1f2012-04-19 13:38:03 +00003483 register_net_sysctl(neigh_parms_net(p), neigh_path, t->neigh_vars);
Pavel Emelyanov3c607bb2007-12-02 00:06:34 +11003484 if (!t->sysctl_header)
Eric W. Biederman8f40a1f2012-04-19 13:38:03 +00003485 goto free;
Pavel Emelyanov3c607bb2007-12-02 00:06:34 +11003486
Linus Torvalds1da177e2005-04-16 15:20:36 -07003487 p->sysctl_table = t;
3488 return 0;
3489
Pavel Emelyanov3c607bb2007-12-02 00:06:34 +11003490free:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003491 kfree(t);
Pavel Emelyanov3c607bb2007-12-02 00:06:34 +11003492err:
3493 return -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003494}
YOSHIFUJI Hideaki0a204502008-03-24 18:39:10 +09003495EXPORT_SYMBOL(neigh_sysctl_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003496
3497void neigh_sysctl_unregister(struct neigh_parms *p)
3498{
3499 if (p->sysctl_table) {
3500 struct neigh_sysctl_table *t = p->sysctl_table;
3501 p->sysctl_table = NULL;
Eric W. Biederman5dd3df12012-04-19 13:24:33 +00003502 unregister_net_sysctl_table(t->sysctl_header);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003503 kfree(t);
3504 }
3505}
YOSHIFUJI Hideaki0a204502008-03-24 18:39:10 +09003506EXPORT_SYMBOL(neigh_sysctl_unregister);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003507
3508#endif /* CONFIG_SYSCTL */
3509
Thomas Grafc8822a42007-03-22 11:50:06 -07003510static int __init neigh_init(void)
3511{
Florian Westphalb97bac62017-08-09 20:41:48 +02003512 rtnl_register(PF_UNSPEC, RTM_NEWNEIGH, neigh_add, NULL, 0);
3513 rtnl_register(PF_UNSPEC, RTM_DELNEIGH, neigh_delete, NULL, 0);
3514 rtnl_register(PF_UNSPEC, RTM_GETNEIGH, NULL, neigh_dump_info, 0);
Thomas Grafc8822a42007-03-22 11:50:06 -07003515
Greg Rosec7ac8672011-06-10 01:27:09 +00003516 rtnl_register(PF_UNSPEC, RTM_GETNEIGHTBL, NULL, neightbl_dump_info,
Florian Westphalb97bac62017-08-09 20:41:48 +02003517 0);
3518 rtnl_register(PF_UNSPEC, RTM_SETNEIGHTBL, neightbl_set, NULL, 0);
Thomas Grafc8822a42007-03-22 11:50:06 -07003519
3520 return 0;
3521}
3522
3523subsys_initcall(neigh_init);