blob: 652da63690376b52d084a901310db39f4258aca3 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * Generic address resolution entity
4 *
5 * Authors:
6 * Pedro Roque <roque@di.fc.ul.pt>
7 * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
8 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 * Fixes:
10 * Vitaly E. Lavrov releasing NULL neighbor in neigh_add.
11 * Harald Welte Add neighbour cache statistics like rtstat
12 */
13
Joe Perchese005d192012-05-16 19:58:40 +000014#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090016#include <linux/slab.h>
Konstantin Khlebnikov85704cb2019-01-08 12:30:00 +030017#include <linux/kmemleak.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/types.h>
19#include <linux/kernel.h>
20#include <linux/module.h>
21#include <linux/socket.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/netdevice.h>
23#include <linux/proc_fs.h>
24#ifdef CONFIG_SYSCTL
25#include <linux/sysctl.h>
26#endif
27#include <linux/times.h>
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020028#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <net/neighbour.h>
David Ahern4b2a2bf2019-05-01 18:18:42 -070030#include <net/arp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <net/dst.h>
32#include <net/sock.h>
Tom Tucker8d717402006-07-30 20:43:36 -070033#include <net/netevent.h>
Thomas Grafa14a49d2006-08-07 17:53:08 -070034#include <net/netlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <linux/rtnetlink.h>
36#include <linux/random.h>
Paulo Marques543537b2005-06-23 00:09:02 -070037#include <linux/string.h>
vignesh babuc3609d52007-08-24 22:27:55 -070038#include <linux/log2.h>
Jiri Pirko1d4c8c22013-12-07 19:26:56 +010039#include <linux/inetdevice.h>
Jiri Pirkobba24892013-12-07 19:26:57 +010040#include <net/addrconf.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Roopa Prabhu56dd18a2019-02-14 09:15:11 -080042#include <trace/events/neigh.h>
43
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 Prabhu56dd18a2019-02-14 09:15:11 -0800104 trace_neigh_cleanup_and_release(neigh, 0);
Roopa Prabhu7b8f7a42017-03-19 22:01:28 -0700105 __neigh_notify(neigh, RTM_DELNEIGH, 0, 0);
Ido Schimmel53f800e2016-12-23 09:32:48 +0100106 call_netevent_notifiers(NETEVENT_NEIGH_UPDATE, neigh);
Thomas Graf4f494552007-08-08 23:12:36 -0700107 neigh_release(neigh);
108}
109
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110/*
111 * It is random distribution in the interval (1/2)*base...(3/2)*base.
112 * It corresponds to default IPv6 settings and is not overridable,
113 * because it is really reasonable choice.
114 */
115
116unsigned long neigh_rand_reach_time(unsigned long base)
117{
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -0500118 return base ? (prandom_u32() % base) + (base >> 1) : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119}
YOSHIFUJI Hideaki0a204502008-03-24 18:39:10 +0900120EXPORT_SYMBOL(neigh_rand_reach_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
David Ahern58956312018-12-07 12:24:57 -0800122static void neigh_mark_dead(struct neighbour *n)
123{
124 n->dead = 1;
125 if (!list_empty(&n->gc_list)) {
126 list_del_init(&n->gc_list);
127 atomic_dec(&n->tbl->gc_entries);
128 }
129}
130
David Ahern9c29a2f2018-12-11 18:57:21 -0700131static void neigh_update_gc_list(struct neighbour *n)
David Ahern58956312018-12-07 12:24:57 -0800132{
David Aherne997f8a2018-12-11 18:57:25 -0700133 bool on_gc_list, exempt_from_gc;
David Ahern58956312018-12-07 12:24:57 -0800134
David Ahern9c29a2f2018-12-11 18:57:21 -0700135 write_lock_bh(&n->tbl->lock);
136 write_lock(&n->lock);
David Ahern58956312018-12-07 12:24:57 -0800137
David Aherne997f8a2018-12-11 18:57:25 -0700138 /* remove from the gc list if new state is permanent or if neighbor
139 * is externally learned; otherwise entry should be on the gc list
David Ahern58956312018-12-07 12:24:57 -0800140 */
David Aherne997f8a2018-12-11 18:57:25 -0700141 exempt_from_gc = n->nud_state & NUD_PERMANENT ||
142 n->flags & NTF_EXT_LEARNED;
David Ahern9c29a2f2018-12-11 18:57:21 -0700143 on_gc_list = !list_empty(&n->gc_list);
David Ahern8cc196d2018-12-10 13:54:07 -0800144
David Aherne997f8a2018-12-11 18:57:25 -0700145 if (exempt_from_gc && on_gc_list) {
David Ahern9c29a2f2018-12-11 18:57:21 -0700146 list_del_init(&n->gc_list);
David Ahern58956312018-12-07 12:24:57 -0800147 atomic_dec(&n->tbl->gc_entries);
David Aherne997f8a2018-12-11 18:57:25 -0700148 } else if (!exempt_from_gc && !on_gc_list) {
David Ahern58956312018-12-07 12:24:57 -0800149 /* add entries to the tail; cleaning removes from the front */
150 list_add_tail(&n->gc_list, &n->tbl->gc_list);
151 atomic_inc(&n->tbl->gc_entries);
152 }
David Ahern9c29a2f2018-12-11 18:57:21 -0700153
154 write_unlock(&n->lock);
155 write_unlock_bh(&n->tbl->lock);
David Ahern58956312018-12-07 12:24:57 -0800156}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
David Aherne997f8a2018-12-11 18:57:25 -0700158static bool neigh_update_ext_learned(struct neighbour *neigh, u32 flags,
David Ahern526f1b52018-12-11 18:57:24 -0700159 int *notify)
160{
David Aherne997f8a2018-12-11 18:57:25 -0700161 bool rc = false;
David Ahern526f1b52018-12-11 18:57:24 -0700162 u8 ndm_flags;
163
164 if (!(flags & NEIGH_UPDATE_F_ADMIN))
David Aherne997f8a2018-12-11 18:57:25 -0700165 return rc;
David Ahern526f1b52018-12-11 18:57:24 -0700166
167 ndm_flags = (flags & NEIGH_UPDATE_F_EXT_LEARNED) ? NTF_EXT_LEARNED : 0;
168 if ((neigh->flags ^ ndm_flags) & NTF_EXT_LEARNED) {
169 if (ndm_flags & NTF_EXT_LEARNED)
170 neigh->flags |= NTF_EXT_LEARNED;
171 else
172 neigh->flags &= ~NTF_EXT_LEARNED;
David Aherne997f8a2018-12-11 18:57:25 -0700173 rc = true;
David Ahern526f1b52018-12-11 18:57:24 -0700174 *notify = 1;
175 }
David Aherne997f8a2018-12-11 18:57:25 -0700176
177 return rc;
David Ahern526f1b52018-12-11 18:57:24 -0700178}
179
David Ahern7e6f1822018-12-11 18:57:23 -0700180static bool neigh_del(struct neighbour *n, struct neighbour __rcu **np,
181 struct neigh_table *tbl)
Sowmini Varadhan50710342017-06-02 09:01:49 -0700182{
183 bool retval = false;
184
185 write_lock(&n->lock);
David Ahern7e6f1822018-12-11 18:57:23 -0700186 if (refcount_read(&n->refcnt) == 1) {
Sowmini Varadhan50710342017-06-02 09:01:49 -0700187 struct neighbour *neigh;
188
189 neigh = rcu_dereference_protected(n->next,
190 lockdep_is_held(&tbl->lock));
191 rcu_assign_pointer(*np, neigh);
David Ahern58956312018-12-07 12:24:57 -0800192 neigh_mark_dead(n);
Sowmini Varadhan50710342017-06-02 09:01:49 -0700193 retval = true;
194 }
195 write_unlock(&n->lock);
196 if (retval)
197 neigh_cleanup_and_release(n);
198 return retval;
199}
200
201bool neigh_remove_one(struct neighbour *ndel, struct neigh_table *tbl)
202{
203 struct neigh_hash_table *nht;
204 void *pkey = ndel->primary_key;
205 u32 hash_val;
206 struct neighbour *n;
207 struct neighbour __rcu **np;
208
209 nht = rcu_dereference_protected(tbl->nht,
210 lockdep_is_held(&tbl->lock));
211 hash_val = tbl->hash(pkey, ndel->dev, nht->hash_rnd);
212 hash_val = hash_val >> (32 - nht->hash_shift);
213
214 np = &nht->hash_buckets[hash_val];
215 while ((n = rcu_dereference_protected(*np,
216 lockdep_is_held(&tbl->lock)))) {
217 if (n == ndel)
David Ahern7e6f1822018-12-11 18:57:23 -0700218 return neigh_del(n, np, tbl);
Sowmini Varadhan50710342017-06-02 09:01:49 -0700219 np = &n->next;
220 }
221 return false;
222}
223
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224static int neigh_forced_gc(struct neigh_table *tbl)
225{
David Ahern58956312018-12-07 12:24:57 -0800226 int max_clean = atomic_read(&tbl->gc_entries) - tbl->gc_thresh2;
227 unsigned long tref = jiffies - 5 * HZ;
David Ahern58956312018-12-07 12:24:57 -0800228 struct neighbour *n, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 int shrunk = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
231 NEIGH_CACHE_STAT_INC(tbl, forced_gc_runs);
232
233 write_lock_bh(&tbl->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
David Ahern58956312018-12-07 12:24:57 -0800235 list_for_each_entry_safe(n, tmp, &tbl->gc_list, gc_list) {
236 if (refcount_read(&n->refcnt) == 1) {
237 bool remove = false;
238
239 write_lock(&n->lock);
David Ahern758a7f02018-12-11 18:57:22 -0700240 if ((n->nud_state == NUD_FAILED) ||
David Aherne997f8a2018-12-11 18:57:25 -0700241 time_after(tref, n->updated))
David Ahern58956312018-12-07 12:24:57 -0800242 remove = true;
243 write_unlock(&n->lock);
244
245 if (remove && neigh_remove_one(n, tbl))
246 shrunk++;
247 if (shrunk >= max_clean)
248 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 }
250 }
251
252 tbl->last_flush = jiffies;
253
254 write_unlock_bh(&tbl->lock);
255
256 return shrunk;
257}
258
Pavel Emelyanova43d8992007-12-20 15:49:05 -0800259static void neigh_add_timer(struct neighbour *n, unsigned long when)
260{
261 neigh_hold(n);
262 if (unlikely(mod_timer(&n->timer, when))) {
263 printk("NEIGH: BUG, double timer add, state is %x\n",
264 n->nud_state);
265 dump_stack();
266 }
267}
268
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269static int neigh_del_timer(struct neighbour *n)
270{
271 if ((n->nud_state & NUD_IN_TIMER) &&
272 del_timer(&n->timer)) {
273 neigh_release(n);
274 return 1;
275 }
276 return 0;
277}
278
279static void pneigh_queue_purge(struct sk_buff_head *list)
280{
281 struct sk_buff *skb;
282
283 while ((skb = skb_dequeue(list)) != NULL) {
284 dev_put(skb->dev);
285 kfree_skb(skb);
286 }
287}
288
David Ahern859bd2e2018-10-11 20:33:49 -0700289static void neigh_flush_dev(struct neigh_table *tbl, struct net_device *dev,
290 bool skip_perm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291{
292 int i;
Eric Dumazetd6bf7812010-10-04 06:15:44 +0000293 struct neigh_hash_table *nht;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294
Eric Dumazetd6bf7812010-10-04 06:15:44 +0000295 nht = rcu_dereference_protected(tbl->nht,
296 lockdep_is_held(&tbl->lock));
297
David S. Millercd089332011-07-11 01:28:12 -0700298 for (i = 0; i < (1 << nht->hash_shift); i++) {
Eric Dumazet767e97e2010-10-06 17:49:21 -0700299 struct neighbour *n;
300 struct neighbour __rcu **np = &nht->hash_buckets[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
Eric Dumazet767e97e2010-10-06 17:49:21 -0700302 while ((n = rcu_dereference_protected(*np,
303 lockdep_is_held(&tbl->lock))) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 if (dev && n->dev != dev) {
305 np = &n->next;
306 continue;
307 }
David Ahern859bd2e2018-10-11 20:33:49 -0700308 if (skip_perm && n->nud_state & NUD_PERMANENT) {
309 np = &n->next;
310 continue;
311 }
Eric Dumazet767e97e2010-10-06 17:49:21 -0700312 rcu_assign_pointer(*np,
313 rcu_dereference_protected(n->next,
314 lockdep_is_held(&tbl->lock)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 write_lock(&n->lock);
316 neigh_del_timer(n);
David Ahern58956312018-12-07 12:24:57 -0800317 neigh_mark_dead(n);
Reshetova, Elena9f237432017-06-30 13:07:55 +0300318 if (refcount_read(&n->refcnt) != 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 /* The most unpleasant situation.
320 We must destroy neighbour entry,
321 but someone still uses it.
322
323 The destroy will be delayed until
324 the last user releases us, but
325 we must kill timers etc. and move
326 it to safe state.
327 */
Eric Dumazetc9ab4d82013-06-28 02:37:42 -0700328 __skb_queue_purge(&n->arp_queue);
Eric Dumazet8b5c1712011-11-09 12:07:14 +0000329 n->arp_queue_len_bytes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 n->output = neigh_blackhole;
331 if (n->nud_state & NUD_VALID)
332 n->nud_state = NUD_NOARP;
333 else
334 n->nud_state = NUD_NONE;
Joe Perchesd5d427c2013-04-15 15:17:19 +0000335 neigh_dbg(2, "neigh %p is stray\n", n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 }
337 write_unlock(&n->lock);
Thomas Graf4f494552007-08-08 23:12:36 -0700338 neigh_cleanup_and_release(n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 }
340 }
Herbert Xu49636bb2005-10-23 17:18:00 +1000341}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
Herbert Xu49636bb2005-10-23 17:18:00 +1000343void neigh_changeaddr(struct neigh_table *tbl, struct net_device *dev)
344{
345 write_lock_bh(&tbl->lock);
David Ahern859bd2e2018-10-11 20:33:49 -0700346 neigh_flush_dev(tbl, dev, false);
Herbert Xu49636bb2005-10-23 17:18:00 +1000347 write_unlock_bh(&tbl->lock);
348}
YOSHIFUJI Hideaki0a204502008-03-24 18:39:10 +0900349EXPORT_SYMBOL(neigh_changeaddr);
Herbert Xu49636bb2005-10-23 17:18:00 +1000350
David Ahern859bd2e2018-10-11 20:33:49 -0700351static int __neigh_ifdown(struct neigh_table *tbl, struct net_device *dev,
352 bool skip_perm)
Herbert Xu49636bb2005-10-23 17:18:00 +1000353{
354 write_lock_bh(&tbl->lock);
David Ahern859bd2e2018-10-11 20:33:49 -0700355 neigh_flush_dev(tbl, dev, skip_perm);
Wolfgang Bumiller53b76cd2018-04-12 10:46:55 +0200356 pneigh_ifdown_and_unlock(tbl, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
358 del_timer_sync(&tbl->proxy_timer);
359 pneigh_queue_purge(&tbl->proxy_queue);
360 return 0;
361}
David Ahern859bd2e2018-10-11 20:33:49 -0700362
363int neigh_carrier_down(struct neigh_table *tbl, struct net_device *dev)
364{
365 __neigh_ifdown(tbl, dev, true);
366 return 0;
367}
368EXPORT_SYMBOL(neigh_carrier_down);
369
370int neigh_ifdown(struct neigh_table *tbl, struct net_device *dev)
371{
372 __neigh_ifdown(tbl, dev, false);
373 return 0;
374}
YOSHIFUJI Hideaki0a204502008-03-24 18:39:10 +0900375EXPORT_SYMBOL(neigh_ifdown);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
David Ahern58956312018-12-07 12:24:57 -0800377static struct neighbour *neigh_alloc(struct neigh_table *tbl,
378 struct net_device *dev,
David Aherne997f8a2018-12-11 18:57:25 -0700379 bool exempt_from_gc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380{
381 struct neighbour *n = NULL;
382 unsigned long now = jiffies;
383 int entries;
384
David Aherne997f8a2018-12-11 18:57:25 -0700385 if (exempt_from_gc)
David Ahern58956312018-12-07 12:24:57 -0800386 goto do_alloc;
387
388 entries = atomic_inc_return(&tbl->gc_entries) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 if (entries >= tbl->gc_thresh3 ||
390 (entries >= tbl->gc_thresh2 &&
391 time_after(now, tbl->last_flush + 5 * HZ))) {
392 if (!neigh_forced_gc(tbl) &&
Rick Jonesfb811392015-08-07 11:10:37 -0700393 entries >= tbl->gc_thresh3) {
394 net_info_ratelimited("%s: neighbor table overflow!\n",
395 tbl->id);
396 NEIGH_CACHE_STAT_INC(tbl, table_fulls);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 goto out_entries;
Rick Jonesfb811392015-08-07 11:10:37 -0700398 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 }
400
David Ahern58956312018-12-07 12:24:57 -0800401do_alloc:
YOSHIFUJI Hideaki / 吉藤英明08433ef2013-01-24 00:44:23 +0000402 n = kzalloc(tbl->entry_size + dev->neigh_priv_len, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 if (!n)
404 goto out_entries;
405
Eric Dumazetc9ab4d82013-06-28 02:37:42 -0700406 __skb_queue_head_init(&n->arp_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 rwlock_init(&n->lock);
Eric Dumazet0ed8ddf2010-10-07 10:44:07 +0000408 seqlock_init(&n->ha_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 n->updated = n->used = now;
410 n->nud_state = NUD_NONE;
411 n->output = neigh_blackhole;
David S. Millerf6b72b622011-07-14 07:53:20 -0700412 seqlock_init(&n->hh.hh_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 n->parms = neigh_parms_clone(&tbl->parms);
Kees Cooke99e88a2017-10-16 14:43:17 -0700414 timer_setup(&n->timer, neigh_timer_handler, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
416 NEIGH_CACHE_STAT_INC(tbl, allocs);
417 n->tbl = tbl;
Reshetova, Elena9f237432017-06-30 13:07:55 +0300418 refcount_set(&n->refcnt, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 n->dead = 1;
David Ahern8cc196d2018-12-10 13:54:07 -0800420 INIT_LIST_HEAD(&n->gc_list);
David Ahern58956312018-12-07 12:24:57 -0800421
422 atomic_inc(&tbl->entries);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423out:
424 return n;
425
426out_entries:
David Aherne997f8a2018-12-11 18:57:25 -0700427 if (!exempt_from_gc)
David Ahern58956312018-12-07 12:24:57 -0800428 atomic_dec(&tbl->gc_entries);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 goto out;
430}
431
David S. Miller2c2aba62011-12-28 15:06:58 -0500432static void neigh_get_hash_rnd(u32 *x)
433{
Jason A. Donenfeldb3d0f782017-06-07 23:00:05 -0400434 *x = get_random_u32() | 1;
David S. Miller2c2aba62011-12-28 15:06:58 -0500435}
436
David S. Millercd089332011-07-11 01:28:12 -0700437static struct neigh_hash_table *neigh_hash_alloc(unsigned int shift)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438{
David S. Millercd089332011-07-11 01:28:12 -0700439 size_t size = (1 << shift) * sizeof(struct neighbour *);
Eric Dumazetd6bf7812010-10-04 06:15:44 +0000440 struct neigh_hash_table *ret;
Eric Dumazet6193d2b2011-01-19 22:02:47 +0000441 struct neighbour __rcu **buckets;
David S. Miller2c2aba62011-12-28 15:06:58 -0500442 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
Eric Dumazetd6bf7812010-10-04 06:15:44 +0000444 ret = kmalloc(sizeof(*ret), GFP_ATOMIC);
445 if (!ret)
446 return NULL;
Konstantin Khlebnikov85704cb2019-01-08 12:30:00 +0300447 if (size <= PAGE_SIZE) {
Eric Dumazetd6bf7812010-10-04 06:15:44 +0000448 buckets = kzalloc(size, GFP_ATOMIC);
Konstantin Khlebnikov85704cb2019-01-08 12:30:00 +0300449 } else {
Eric Dumazet6193d2b2011-01-19 22:02:47 +0000450 buckets = (struct neighbour __rcu **)
Eric Dumazetd6bf7812010-10-04 06:15:44 +0000451 __get_free_pages(GFP_ATOMIC | __GFP_ZERO,
452 get_order(size));
Konstantin Khlebnikov01b833a2019-01-14 13:38:43 +0300453 kmemleak_alloc(buckets, size, 1, GFP_ATOMIC);
Konstantin Khlebnikov85704cb2019-01-08 12:30:00 +0300454 }
Eric Dumazetd6bf7812010-10-04 06:15:44 +0000455 if (!buckets) {
456 kfree(ret);
457 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 }
Eric Dumazet6193d2b2011-01-19 22:02:47 +0000459 ret->hash_buckets = buckets;
David S. Millercd089332011-07-11 01:28:12 -0700460 ret->hash_shift = shift;
David S. Miller2c2aba62011-12-28 15:06:58 -0500461 for (i = 0; i < NEIGH_NUM_HASH_RND; i++)
462 neigh_get_hash_rnd(&ret->hash_rnd[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 return ret;
464}
465
Eric Dumazetd6bf7812010-10-04 06:15:44 +0000466static void neigh_hash_free_rcu(struct rcu_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467{
Eric Dumazetd6bf7812010-10-04 06:15:44 +0000468 struct neigh_hash_table *nht = container_of(head,
469 struct neigh_hash_table,
470 rcu);
David S. Millercd089332011-07-11 01:28:12 -0700471 size_t size = (1 << nht->hash_shift) * sizeof(struct neighbour *);
Eric Dumazet6193d2b2011-01-19 22:02:47 +0000472 struct neighbour __rcu **buckets = nht->hash_buckets;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
Konstantin Khlebnikov85704cb2019-01-08 12:30:00 +0300474 if (size <= PAGE_SIZE) {
Eric Dumazetd6bf7812010-10-04 06:15:44 +0000475 kfree(buckets);
Konstantin Khlebnikov85704cb2019-01-08 12:30:00 +0300476 } else {
477 kmemleak_free(buckets);
Eric Dumazetd6bf7812010-10-04 06:15:44 +0000478 free_pages((unsigned long)buckets, get_order(size));
Konstantin Khlebnikov85704cb2019-01-08 12:30:00 +0300479 }
Eric Dumazetd6bf7812010-10-04 06:15:44 +0000480 kfree(nht);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481}
482
Eric Dumazetd6bf7812010-10-04 06:15:44 +0000483static struct neigh_hash_table *neigh_hash_grow(struct neigh_table *tbl,
David S. Millercd089332011-07-11 01:28:12 -0700484 unsigned long new_shift)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485{
Eric Dumazetd6bf7812010-10-04 06:15:44 +0000486 unsigned int i, hash;
487 struct neigh_hash_table *new_nht, *old_nht;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
489 NEIGH_CACHE_STAT_INC(tbl, hash_grows);
490
Eric Dumazetd6bf7812010-10-04 06:15:44 +0000491 old_nht = rcu_dereference_protected(tbl->nht,
492 lockdep_is_held(&tbl->lock));
David S. Millercd089332011-07-11 01:28:12 -0700493 new_nht = neigh_hash_alloc(new_shift);
Eric Dumazetd6bf7812010-10-04 06:15:44 +0000494 if (!new_nht)
495 return old_nht;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
David S. Millercd089332011-07-11 01:28:12 -0700497 for (i = 0; i < (1 << old_nht->hash_shift); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 struct neighbour *n, *next;
499
Eric Dumazet767e97e2010-10-06 17:49:21 -0700500 for (n = rcu_dereference_protected(old_nht->hash_buckets[i],
501 lockdep_is_held(&tbl->lock));
Eric Dumazetd6bf7812010-10-04 06:15:44 +0000502 n != NULL;
503 n = next) {
504 hash = tbl->hash(n->primary_key, n->dev,
505 new_nht->hash_rnd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506
David S. Millercd089332011-07-11 01:28:12 -0700507 hash >>= (32 - new_nht->hash_shift);
Eric Dumazet767e97e2010-10-06 17:49:21 -0700508 next = rcu_dereference_protected(n->next,
509 lockdep_is_held(&tbl->lock));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
Eric Dumazet767e97e2010-10-06 17:49:21 -0700511 rcu_assign_pointer(n->next,
512 rcu_dereference_protected(
513 new_nht->hash_buckets[hash],
514 lockdep_is_held(&tbl->lock)));
515 rcu_assign_pointer(new_nht->hash_buckets[hash], n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 }
517 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
Eric Dumazetd6bf7812010-10-04 06:15:44 +0000519 rcu_assign_pointer(tbl->nht, new_nht);
520 call_rcu(&old_nht->rcu, neigh_hash_free_rcu);
521 return new_nht;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522}
523
524struct neighbour *neigh_lookup(struct neigh_table *tbl, const void *pkey,
525 struct net_device *dev)
526{
527 struct neighbour *n;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900528
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 NEIGH_CACHE_STAT_INC(tbl, lookups);
530
Eric Dumazetd6bf7812010-10-04 06:15:44 +0000531 rcu_read_lock_bh();
Eric W. Biederman60395a22015-03-03 17:10:44 -0600532 n = __neigh_lookup_noref(tbl, pkey, dev);
533 if (n) {
Reshetova, Elena9f237432017-06-30 13:07:55 +0300534 if (!refcount_inc_not_zero(&n->refcnt))
Eric W. Biederman60395a22015-03-03 17:10:44 -0600535 n = NULL;
536 NEIGH_CACHE_STAT_INC(tbl, hits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 }
Eric Dumazet767e97e2010-10-06 17:49:21 -0700538
Eric Dumazetd6bf7812010-10-04 06:15:44 +0000539 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 return n;
541}
YOSHIFUJI Hideaki0a204502008-03-24 18:39:10 +0900542EXPORT_SYMBOL(neigh_lookup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
Eric W. Biederman426b5302008-01-24 00:13:18 -0800544struct neighbour *neigh_lookup_nodev(struct neigh_table *tbl, struct net *net,
545 const void *pkey)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546{
547 struct neighbour *n;
Alexey Dobriyan01ccdf12017-09-23 23:03:04 +0300548 unsigned int key_len = tbl->key_len;
Pavel Emelyanovbc4bf5f2008-02-23 19:57:02 -0800549 u32 hash_val;
Eric Dumazetd6bf7812010-10-04 06:15:44 +0000550 struct neigh_hash_table *nht;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
552 NEIGH_CACHE_STAT_INC(tbl, lookups);
553
Eric Dumazetd6bf7812010-10-04 06:15:44 +0000554 rcu_read_lock_bh();
555 nht = rcu_dereference_bh(tbl->nht);
David S. Millercd089332011-07-11 01:28:12 -0700556 hash_val = tbl->hash(pkey, NULL, nht->hash_rnd) >> (32 - nht->hash_shift);
Eric Dumazet767e97e2010-10-06 17:49:21 -0700557
558 for (n = rcu_dereference_bh(nht->hash_buckets[hash_val]);
559 n != NULL;
560 n = rcu_dereference_bh(n->next)) {
Eric W. Biederman426b5302008-01-24 00:13:18 -0800561 if (!memcmp(n->primary_key, pkey, key_len) &&
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +0900562 net_eq(dev_net(n->dev), net)) {
Reshetova, Elena9f237432017-06-30 13:07:55 +0300563 if (!refcount_inc_not_zero(&n->refcnt))
Eric Dumazet767e97e2010-10-06 17:49:21 -0700564 n = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 NEIGH_CACHE_STAT_INC(tbl, hits);
566 break;
567 }
568 }
Eric Dumazet767e97e2010-10-06 17:49:21 -0700569
Eric Dumazetd6bf7812010-10-04 06:15:44 +0000570 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 return n;
572}
YOSHIFUJI Hideaki0a204502008-03-24 18:39:10 +0900573EXPORT_SYMBOL(neigh_lookup_nodev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
David Ahern58956312018-12-07 12:24:57 -0800575static struct neighbour *___neigh_create(struct neigh_table *tbl,
576 const void *pkey,
577 struct net_device *dev,
David Aherne997f8a2018-12-11 18:57:25 -0700578 bool exempt_from_gc, bool want_ref)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579{
David Aherne997f8a2018-12-11 18:57:25 -0700580 struct neighbour *n1, *rc, *n = neigh_alloc(tbl, dev, exempt_from_gc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 u32 hash_val;
Alexey Dobriyan01ccdf12017-09-23 23:03:04 +0300582 unsigned int key_len = tbl->key_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 int error;
Eric Dumazetd6bf7812010-10-04 06:15:44 +0000584 struct neigh_hash_table *nht;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
David Ahernfc651002019-05-22 12:22:21 -0700586 trace_neigh_create(tbl, dev, pkey, n, exempt_from_gc);
587
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 if (!n) {
589 rc = ERR_PTR(-ENOBUFS);
590 goto out;
591 }
592
593 memcpy(n->primary_key, pkey, key_len);
594 n->dev = dev;
595 dev_hold(dev);
596
597 /* Protocol specific setup. */
598 if (tbl->constructor && (error = tbl->constructor(n)) < 0) {
599 rc = ERR_PTR(error);
600 goto out_neigh_release;
601 }
602
David Millerda6a8fa2011-07-25 00:01:38 +0000603 if (dev->netdev_ops->ndo_neigh_construct) {
Jiri Pirko503eebc2016-07-05 11:27:37 +0200604 error = dev->netdev_ops->ndo_neigh_construct(dev, n);
David Millerda6a8fa2011-07-25 00:01:38 +0000605 if (error < 0) {
606 rc = ERR_PTR(error);
607 goto out_neigh_release;
608 }
609 }
610
David S. Miller447f2192011-12-19 15:04:41 -0500611 /* Device specific setup. */
612 if (n->parms->neigh_setup &&
613 (error = n->parms->neigh_setup(n)) < 0) {
614 rc = ERR_PTR(error);
615 goto out_neigh_release;
616 }
617
Jiri Pirko1f9248e2013-12-07 19:26:53 +0100618 n->confirmed = jiffies - (NEIGH_VAR(n->parms, BASE_REACHABLE_TIME) << 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
620 write_lock_bh(&tbl->lock);
Eric Dumazetd6bf7812010-10-04 06:15:44 +0000621 nht = rcu_dereference_protected(tbl->nht,
622 lockdep_is_held(&tbl->lock));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
David S. Millercd089332011-07-11 01:28:12 -0700624 if (atomic_read(&tbl->entries) > (1 << nht->hash_shift))
625 nht = neigh_hash_grow(tbl, nht->hash_shift + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
Jim Westfall096b9852018-01-14 04:18:50 -0800627 hash_val = tbl->hash(n->primary_key, dev, nht->hash_rnd) >> (32 - nht->hash_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
629 if (n->parms->dead) {
630 rc = ERR_PTR(-EINVAL);
631 goto out_tbl_unlock;
632 }
633
Eric Dumazet767e97e2010-10-06 17:49:21 -0700634 for (n1 = rcu_dereference_protected(nht->hash_buckets[hash_val],
635 lockdep_is_held(&tbl->lock));
636 n1 != NULL;
637 n1 = rcu_dereference_protected(n1->next,
638 lockdep_is_held(&tbl->lock))) {
Jim Westfall096b9852018-01-14 04:18:50 -0800639 if (dev == n1->dev && !memcmp(n1->primary_key, n->primary_key, key_len)) {
David S. Millera263b302012-07-02 02:02:15 -0700640 if (want_ref)
641 neigh_hold(n1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 rc = n1;
643 goto out_tbl_unlock;
644 }
645 }
646
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 n->dead = 0;
David Aherne997f8a2018-12-11 18:57:25 -0700648 if (!exempt_from_gc)
David Ahern8cc196d2018-12-10 13:54:07 -0800649 list_add_tail(&n->gc_list, &n->tbl->gc_list);
650
David S. Millera263b302012-07-02 02:02:15 -0700651 if (want_ref)
652 neigh_hold(n);
Eric Dumazet767e97e2010-10-06 17:49:21 -0700653 rcu_assign_pointer(n->next,
654 rcu_dereference_protected(nht->hash_buckets[hash_val],
655 lockdep_is_held(&tbl->lock)));
656 rcu_assign_pointer(nht->hash_buckets[hash_val], n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 write_unlock_bh(&tbl->lock);
Joe Perchesd5d427c2013-04-15 15:17:19 +0000658 neigh_dbg(2, "neigh %p is created\n", n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 rc = n;
660out:
661 return rc;
662out_tbl_unlock:
663 write_unlock_bh(&tbl->lock);
664out_neigh_release:
David Ahern64c6f4b2019-05-01 18:08:34 -0700665 if (!exempt_from_gc)
666 atomic_dec(&tbl->gc_entries);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 neigh_release(n);
668 goto out;
669}
David Ahern58956312018-12-07 12:24:57 -0800670
671struct neighbour *__neigh_create(struct neigh_table *tbl, const void *pkey,
672 struct net_device *dev, bool want_ref)
673{
674 return ___neigh_create(tbl, pkey, dev, false, want_ref);
675}
David S. Millera263b302012-07-02 02:02:15 -0700676EXPORT_SYMBOL(__neigh_create);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
Alexey Dobriyan01ccdf12017-09-23 23:03:04 +0300678static u32 pneigh_hash(const void *pkey, unsigned int key_len)
Pavel Emelyanovfa86d322008-03-24 14:48:59 -0700679{
Pavel Emelyanovfa86d322008-03-24 14:48:59 -0700680 u32 hash_val = *(u32 *)(pkey + key_len - 4);
Pavel Emelyanovfa86d322008-03-24 14:48:59 -0700681 hash_val ^= (hash_val >> 16);
682 hash_val ^= hash_val >> 8;
683 hash_val ^= hash_val >> 4;
684 hash_val &= PNEIGH_HASHMASK;
YOSHIFUJI Hideakibe01d652008-03-28 12:46:53 +0900685 return hash_val;
686}
Pavel Emelyanovfa86d322008-03-24 14:48:59 -0700687
YOSHIFUJI Hideakibe01d652008-03-28 12:46:53 +0900688static struct pneigh_entry *__pneigh_lookup_1(struct pneigh_entry *n,
689 struct net *net,
690 const void *pkey,
Alexey Dobriyan01ccdf12017-09-23 23:03:04 +0300691 unsigned int key_len,
YOSHIFUJI Hideakibe01d652008-03-28 12:46:53 +0900692 struct net_device *dev)
693{
694 while (n) {
Pavel Emelyanovfa86d322008-03-24 14:48:59 -0700695 if (!memcmp(n->key, pkey, key_len) &&
YOSHIFUJI Hideakibe01d652008-03-28 12:46:53 +0900696 net_eq(pneigh_net(n), net) &&
Pavel Emelyanovfa86d322008-03-24 14:48:59 -0700697 (n->dev == dev || !n->dev))
YOSHIFUJI Hideakibe01d652008-03-28 12:46:53 +0900698 return n;
699 n = n->next;
Pavel Emelyanovfa86d322008-03-24 14:48:59 -0700700 }
YOSHIFUJI Hideakibe01d652008-03-28 12:46:53 +0900701 return NULL;
702}
Pavel Emelyanovfa86d322008-03-24 14:48:59 -0700703
YOSHIFUJI Hideakibe01d652008-03-28 12:46:53 +0900704struct pneigh_entry *__pneigh_lookup(struct neigh_table *tbl,
705 struct net *net, const void *pkey, struct net_device *dev)
706{
Alexey Dobriyan01ccdf12017-09-23 23:03:04 +0300707 unsigned int key_len = tbl->key_len;
YOSHIFUJI Hideakibe01d652008-03-28 12:46:53 +0900708 u32 hash_val = pneigh_hash(pkey, key_len);
709
710 return __pneigh_lookup_1(tbl->phash_buckets[hash_val],
711 net, pkey, key_len, dev);
Pavel Emelyanovfa86d322008-03-24 14:48:59 -0700712}
YOSHIFUJI Hideaki0a204502008-03-24 18:39:10 +0900713EXPORT_SYMBOL_GPL(__pneigh_lookup);
Pavel Emelyanovfa86d322008-03-24 14:48:59 -0700714
Eric W. Biederman426b5302008-01-24 00:13:18 -0800715struct pneigh_entry * pneigh_lookup(struct neigh_table *tbl,
716 struct net *net, const void *pkey,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 struct net_device *dev, int creat)
718{
719 struct pneigh_entry *n;
Alexey Dobriyan01ccdf12017-09-23 23:03:04 +0300720 unsigned int key_len = tbl->key_len;
YOSHIFUJI Hideakibe01d652008-03-28 12:46:53 +0900721 u32 hash_val = pneigh_hash(pkey, key_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
723 read_lock_bh(&tbl->lock);
YOSHIFUJI Hideakibe01d652008-03-28 12:46:53 +0900724 n = __pneigh_lookup_1(tbl->phash_buckets[hash_val],
725 net, pkey, key_len, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 read_unlock_bh(&tbl->lock);
YOSHIFUJI Hideakibe01d652008-03-28 12:46:53 +0900727
728 if (n || !creat)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 goto out;
730
Pavel Emelyanov4ae28942007-10-15 12:54:15 -0700731 ASSERT_RTNL();
732
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 n = kmalloc(sizeof(*n) + key_len, GFP_KERNEL);
734 if (!n)
735 goto out;
736
David Ahern754d5da2018-12-19 15:53:22 -0800737 n->protocol = 0;
Eric W. Biedermanefd7ef12015-03-11 23:04:08 -0500738 write_pnet(&n->net, net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 memcpy(n->key, pkey, key_len);
740 n->dev = dev;
741 if (dev)
742 dev_hold(dev);
743
744 if (tbl->pconstructor && tbl->pconstructor(n)) {
745 if (dev)
746 dev_put(dev);
747 kfree(n);
748 n = NULL;
749 goto out;
750 }
751
752 write_lock_bh(&tbl->lock);
753 n->next = tbl->phash_buckets[hash_val];
754 tbl->phash_buckets[hash_val] = n;
755 write_unlock_bh(&tbl->lock);
756out:
757 return n;
758}
YOSHIFUJI Hideaki0a204502008-03-24 18:39:10 +0900759EXPORT_SYMBOL(pneigh_lookup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760
761
Eric W. Biederman426b5302008-01-24 00:13:18 -0800762int pneigh_delete(struct neigh_table *tbl, struct net *net, const void *pkey,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 struct net_device *dev)
764{
765 struct pneigh_entry *n, **np;
Alexey Dobriyan01ccdf12017-09-23 23:03:04 +0300766 unsigned int key_len = tbl->key_len;
YOSHIFUJI Hideakibe01d652008-03-28 12:46:53 +0900767 u32 hash_val = pneigh_hash(pkey, key_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768
769 write_lock_bh(&tbl->lock);
770 for (np = &tbl->phash_buckets[hash_val]; (n = *np) != NULL;
771 np = &n->next) {
Eric W. Biederman426b5302008-01-24 00:13:18 -0800772 if (!memcmp(n->key, pkey, key_len) && n->dev == dev &&
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +0900773 net_eq(pneigh_net(n), net)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 *np = n->next;
775 write_unlock_bh(&tbl->lock);
776 if (tbl->pdestructor)
777 tbl->pdestructor(n);
778 if (n->dev)
779 dev_put(n->dev);
780 kfree(n);
781 return 0;
782 }
783 }
784 write_unlock_bh(&tbl->lock);
785 return -ENOENT;
786}
787
Wolfgang Bumiller53b76cd2018-04-12 10:46:55 +0200788static int pneigh_ifdown_and_unlock(struct neigh_table *tbl,
789 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790{
Wolfgang Bumiller53b76cd2018-04-12 10:46:55 +0200791 struct pneigh_entry *n, **np, *freelist = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 u32 h;
793
794 for (h = 0; h <= PNEIGH_HASHMASK; h++) {
795 np = &tbl->phash_buckets[h];
796 while ((n = *np) != NULL) {
797 if (!dev || n->dev == dev) {
798 *np = n->next;
Wolfgang Bumiller53b76cd2018-04-12 10:46:55 +0200799 n->next = freelist;
800 freelist = n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 continue;
802 }
803 np = &n->next;
804 }
805 }
Wolfgang Bumiller53b76cd2018-04-12 10:46:55 +0200806 write_unlock_bh(&tbl->lock);
807 while ((n = freelist)) {
808 freelist = n->next;
809 n->next = NULL;
810 if (tbl->pdestructor)
811 tbl->pdestructor(n);
812 if (n->dev)
813 dev_put(n->dev);
814 kfree(n);
815 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 return -ENOENT;
817}
818
Denis V. Lunev06f05112008-01-24 00:30:58 -0800819static void neigh_parms_destroy(struct neigh_parms *parms);
820
821static inline void neigh_parms_put(struct neigh_parms *parms)
822{
Reshetova, Elena63439442017-06-30 13:07:56 +0300823 if (refcount_dec_and_test(&parms->refcnt))
Denis V. Lunev06f05112008-01-24 00:30:58 -0800824 neigh_parms_destroy(parms);
825}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826
827/*
828 * neighbour must already be out of the table;
829 *
830 */
831void neigh_destroy(struct neighbour *neigh)
832{
David Millerda6a8fa2011-07-25 00:01:38 +0000833 struct net_device *dev = neigh->dev;
834
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 NEIGH_CACHE_STAT_INC(neigh->tbl, destroys);
836
837 if (!neigh->dead) {
Joe Perchese005d192012-05-16 19:58:40 +0000838 pr_warn("Destroying alive neighbour %p\n", neigh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 dump_stack();
840 return;
841 }
842
843 if (neigh_del_timer(neigh))
Joe Perchese005d192012-05-16 19:58:40 +0000844 pr_warn("Impossible event\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845
Eric Dumazetc9ab4d82013-06-28 02:37:42 -0700846 write_lock_bh(&neigh->lock);
847 __skb_queue_purge(&neigh->arp_queue);
848 write_unlock_bh(&neigh->lock);
Eric Dumazet8b5c1712011-11-09 12:07:14 +0000849 neigh->arp_queue_len_bytes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850
David S. Miller447f2192011-12-19 15:04:41 -0500851 if (dev->netdev_ops->ndo_neigh_destroy)
Jiri Pirko503eebc2016-07-05 11:27:37 +0200852 dev->netdev_ops->ndo_neigh_destroy(dev, neigh);
David S. Miller447f2192011-12-19 15:04:41 -0500853
David Millerda6a8fa2011-07-25 00:01:38 +0000854 dev_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 neigh_parms_put(neigh->parms);
856
Joe Perchesd5d427c2013-04-15 15:17:19 +0000857 neigh_dbg(2, "neigh %p is destroyed\n", neigh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858
859 atomic_dec(&neigh->tbl->entries);
David Miller5b8b0062011-07-25 00:01:22 +0000860 kfree_rcu(neigh, rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861}
YOSHIFUJI Hideaki0a204502008-03-24 18:39:10 +0900862EXPORT_SYMBOL(neigh_destroy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863
864/* Neighbour state is suspicious;
865 disable fast path.
866
867 Called with write_locked neigh.
868 */
869static void neigh_suspect(struct neighbour *neigh)
870{
Joe Perchesd5d427c2013-04-15 15:17:19 +0000871 neigh_dbg(2, "neigh %p is suspected\n", neigh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872
873 neigh->output = neigh->ops->output;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874}
875
876/* Neighbour state is OK;
877 enable fast path.
878
879 Called with write_locked neigh.
880 */
881static void neigh_connect(struct neighbour *neigh)
882{
Joe Perchesd5d427c2013-04-15 15:17:19 +0000883 neigh_dbg(2, "neigh %p is connected\n", neigh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884
885 neigh->output = neigh->ops->connected_output;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886}
887
Eric Dumazete4c4e442009-07-30 03:15:07 +0000888static void neigh_periodic_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889{
Eric Dumazete4c4e442009-07-30 03:15:07 +0000890 struct neigh_table *tbl = container_of(work, struct neigh_table, gc_work.work);
Eric Dumazet767e97e2010-10-06 17:49:21 -0700891 struct neighbour *n;
892 struct neighbour __rcu **np;
Eric Dumazete4c4e442009-07-30 03:15:07 +0000893 unsigned int i;
Eric Dumazetd6bf7812010-10-04 06:15:44 +0000894 struct neigh_hash_table *nht;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895
896 NEIGH_CACHE_STAT_INC(tbl, periodic_gc_runs);
897
Eric Dumazete4c4e442009-07-30 03:15:07 +0000898 write_lock_bh(&tbl->lock);
Eric Dumazetd6bf7812010-10-04 06:15:44 +0000899 nht = rcu_dereference_protected(tbl->nht,
900 lockdep_is_held(&tbl->lock));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901
902 /*
903 * periodically recompute ReachableTime from random function
904 */
905
Eric Dumazete4c4e442009-07-30 03:15:07 +0000906 if (time_after(jiffies, tbl->last_rand + 300 * HZ)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 struct neigh_parms *p;
Eric Dumazete4c4e442009-07-30 03:15:07 +0000908 tbl->last_rand = jiffies;
Nicolas Dichtel75fbfd32014-10-29 19:29:31 +0100909 list_for_each_entry(p, &tbl->parms_list, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 p->reachable_time =
Jiri Pirko1f9248e2013-12-07 19:26:53 +0100911 neigh_rand_reach_time(NEIGH_VAR(p, BASE_REACHABLE_TIME));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 }
913
Duan Jiongfeff9ab2014-02-27 17:14:41 +0800914 if (atomic_read(&tbl->entries) < tbl->gc_thresh1)
915 goto out;
916
David S. Millercd089332011-07-11 01:28:12 -0700917 for (i = 0 ; i < (1 << nht->hash_shift); i++) {
Eric Dumazetd6bf7812010-10-04 06:15:44 +0000918 np = &nht->hash_buckets[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919
Eric Dumazet767e97e2010-10-06 17:49:21 -0700920 while ((n = rcu_dereference_protected(*np,
921 lockdep_is_held(&tbl->lock))) != NULL) {
Eric Dumazete4c4e442009-07-30 03:15:07 +0000922 unsigned int state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923
Eric Dumazete4c4e442009-07-30 03:15:07 +0000924 write_lock(&n->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925
Eric Dumazete4c4e442009-07-30 03:15:07 +0000926 state = n->nud_state;
Roopa Prabhu9ce33e42018-04-24 13:49:34 -0700927 if ((state & (NUD_PERMANENT | NUD_IN_TIMER)) ||
928 (n->flags & NTF_EXT_LEARNED)) {
Eric Dumazete4c4e442009-07-30 03:15:07 +0000929 write_unlock(&n->lock);
930 goto next_elt;
931 }
932
933 if (time_before(n->used, n->confirmed))
934 n->used = n->confirmed;
935
Reshetova, Elena9f237432017-06-30 13:07:55 +0300936 if (refcount_read(&n->refcnt) == 1 &&
Eric Dumazete4c4e442009-07-30 03:15:07 +0000937 (state == NUD_FAILED ||
Jiri Pirko1f9248e2013-12-07 19:26:53 +0100938 time_after(jiffies, n->used + NEIGH_VAR(n->parms, GC_STALETIME)))) {
Eric Dumazete4c4e442009-07-30 03:15:07 +0000939 *np = n->next;
David Ahern58956312018-12-07 12:24:57 -0800940 neigh_mark_dead(n);
Eric Dumazete4c4e442009-07-30 03:15:07 +0000941 write_unlock(&n->lock);
942 neigh_cleanup_and_release(n);
943 continue;
944 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 write_unlock(&n->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946
947next_elt:
Eric Dumazete4c4e442009-07-30 03:15:07 +0000948 np = &n->next;
949 }
950 /*
951 * It's fine to release lock here, even if hash table
952 * grows while we are preempted.
953 */
954 write_unlock_bh(&tbl->lock);
955 cond_resched();
956 write_lock_bh(&tbl->lock);
Michel Machado84338a62012-02-21 16:04:13 -0500957 nht = rcu_dereference_protected(tbl->nht,
958 lockdep_is_held(&tbl->lock));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 }
YOSHIFUJI Hideaki / 吉藤英明27246802013-01-22 05:20:05 +0000960out:
Jiri Pirko1f9248e2013-12-07 19:26:53 +0100961 /* Cycle through all hash buckets every BASE_REACHABLE_TIME/2 ticks.
962 * ARP entry timeouts range from 1/2 BASE_REACHABLE_TIME to 3/2
963 * BASE_REACHABLE_TIME.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 */
viresh kumarf6180022014-01-22 12:23:33 +0530965 queue_delayed_work(system_power_efficient_wq, &tbl->gc_work,
Jiri Pirko1f9248e2013-12-07 19:26:53 +0100966 NEIGH_VAR(&tbl->parms, BASE_REACHABLE_TIME) >> 1);
Eric Dumazete4c4e442009-07-30 03:15:07 +0000967 write_unlock_bh(&tbl->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968}
969
970static __inline__ int neigh_max_probes(struct neighbour *n)
971{
972 struct neigh_parms *p = n->parms;
YOSHIFUJI Hideaki/吉藤英明8da86462015-03-19 22:41:46 +0900973 return NEIGH_VAR(p, UCAST_PROBES) + NEIGH_VAR(p, APP_PROBES) +
974 (n->nud_state & NUD_PROBE ? NEIGH_VAR(p, MCAST_REPROBES) :
975 NEIGH_VAR(p, MCAST_PROBES));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976}
977
Timo Teras5ef12d92009-06-11 04:16:28 -0700978static void neigh_invalidate(struct neighbour *neigh)
Eric Dumazet0a141502010-03-09 19:40:54 +0000979 __releases(neigh->lock)
980 __acquires(neigh->lock)
Timo Teras5ef12d92009-06-11 04:16:28 -0700981{
982 struct sk_buff *skb;
983
984 NEIGH_CACHE_STAT_INC(neigh->tbl, res_failed);
Joe Perchesd5d427c2013-04-15 15:17:19 +0000985 neigh_dbg(2, "neigh %p is failed\n", neigh);
Timo Teras5ef12d92009-06-11 04:16:28 -0700986 neigh->updated = jiffies;
987
988 /* It is very thin place. report_unreachable is very complicated
989 routine. Particularly, it can hit the same neighbour entry!
990
991 So that, we try to be accurate and avoid dead loop. --ANK
992 */
993 while (neigh->nud_state == NUD_FAILED &&
994 (skb = __skb_dequeue(&neigh->arp_queue)) != NULL) {
995 write_unlock(&neigh->lock);
996 neigh->ops->error_report(neigh, skb);
997 write_lock(&neigh->lock);
998 }
Eric Dumazetc9ab4d82013-06-28 02:37:42 -0700999 __skb_queue_purge(&neigh->arp_queue);
Eric Dumazet8b5c1712011-11-09 12:07:14 +00001000 neigh->arp_queue_len_bytes = 0;
Timo Teras5ef12d92009-06-11 04:16:28 -07001001}
1002
Eric Dumazetcd28ca02011-08-09 08:15:58 +00001003static void neigh_probe(struct neighbour *neigh)
1004 __releases(neigh->lock)
1005{
Hannes Frederic Sowa4ed377e2013-09-21 06:32:34 +02001006 struct sk_buff *skb = skb_peek_tail(&neigh->arp_queue);
Eric Dumazetcd28ca02011-08-09 08:15:58 +00001007 /* keep skb alive even if arp_queue overflows */
1008 if (skb)
Martin Zhang19125c12015-11-17 20:49:30 +08001009 skb = skb_clone(skb, GFP_ATOMIC);
Eric Dumazetcd28ca02011-08-09 08:15:58 +00001010 write_unlock(&neigh->lock);
Eric Dumazet48481c82017-03-23 12:39:21 -07001011 if (neigh->ops->solicit)
1012 neigh->ops->solicit(neigh, skb);
Eric Dumazetcd28ca02011-08-09 08:15:58 +00001013 atomic_inc(&neigh->probes);
Yang Wei87fff3ca2019-01-17 23:11:30 +08001014 consume_skb(skb);
Eric Dumazetcd28ca02011-08-09 08:15:58 +00001015}
1016
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017/* Called when a timer expires for a neighbour entry. */
1018
Kees Cooke99e88a2017-10-16 14:43:17 -07001019static void neigh_timer_handler(struct timer_list *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020{
1021 unsigned long now, next;
Kees Cooke99e88a2017-10-16 14:43:17 -07001022 struct neighbour *neigh = from_timer(neigh, t, timer);
Eric Dumazet95c96172012-04-15 05:58:06 +00001023 unsigned int state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 int notify = 0;
1025
1026 write_lock(&neigh->lock);
1027
1028 state = neigh->nud_state;
1029 now = jiffies;
1030 next = now + HZ;
1031
David S. Miller045f7b32011-11-01 17:45:55 -04001032 if (!(state & NUD_IN_TIMER))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034
1035 if (state & NUD_REACHABLE) {
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001036 if (time_before_eq(now,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 neigh->confirmed + neigh->parms->reachable_time)) {
Joe Perchesd5d427c2013-04-15 15:17:19 +00001038 neigh_dbg(2, "neigh %p is still alive\n", neigh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 next = neigh->confirmed + neigh->parms->reachable_time;
1040 } else if (time_before_eq(now,
Jiri Pirko1f9248e2013-12-07 19:26:53 +01001041 neigh->used +
1042 NEIGH_VAR(neigh->parms, DELAY_PROBE_TIME))) {
Joe Perchesd5d427c2013-04-15 15:17:19 +00001043 neigh_dbg(2, "neigh %p is delayed\n", neigh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 neigh->nud_state = NUD_DELAY;
YOSHIFUJI Hideaki955aaa22006-03-20 16:52:52 -08001045 neigh->updated = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 neigh_suspect(neigh);
Jiri Pirko1f9248e2013-12-07 19:26:53 +01001047 next = now + NEIGH_VAR(neigh->parms, DELAY_PROBE_TIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 } else {
Joe Perchesd5d427c2013-04-15 15:17:19 +00001049 neigh_dbg(2, "neigh %p is suspected\n", neigh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 neigh->nud_state = NUD_STALE;
YOSHIFUJI Hideaki955aaa22006-03-20 16:52:52 -08001051 neigh->updated = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 neigh_suspect(neigh);
Tom Tucker8d717402006-07-30 20:43:36 -07001053 notify = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 }
1055 } else if (state & NUD_DELAY) {
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001056 if (time_before_eq(now,
Jiri Pirko1f9248e2013-12-07 19:26:53 +01001057 neigh->confirmed +
1058 NEIGH_VAR(neigh->parms, DELAY_PROBE_TIME))) {
Joe Perchesd5d427c2013-04-15 15:17:19 +00001059 neigh_dbg(2, "neigh %p is now reachable\n", neigh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 neigh->nud_state = NUD_REACHABLE;
YOSHIFUJI Hideaki955aaa22006-03-20 16:52:52 -08001061 neigh->updated = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 neigh_connect(neigh);
Tom Tucker8d717402006-07-30 20:43:36 -07001063 notify = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 next = neigh->confirmed + neigh->parms->reachable_time;
1065 } else {
Joe Perchesd5d427c2013-04-15 15:17:19 +00001066 neigh_dbg(2, "neigh %p is probed\n", neigh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 neigh->nud_state = NUD_PROBE;
YOSHIFUJI Hideaki955aaa22006-03-20 16:52:52 -08001068 neigh->updated = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 atomic_set(&neigh->probes, 0);
Erik Kline765c9c62015-05-18 19:44:41 +09001070 notify = 1;
Jiri Pirko1f9248e2013-12-07 19:26:53 +01001071 next = now + NEIGH_VAR(neigh->parms, RETRANS_TIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 }
1073 } else {
1074 /* NUD_PROBE|NUD_INCOMPLETE */
Jiri Pirko1f9248e2013-12-07 19:26:53 +01001075 next = now + NEIGH_VAR(neigh->parms, RETRANS_TIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 }
1077
1078 if ((neigh->nud_state & (NUD_INCOMPLETE | NUD_PROBE)) &&
1079 atomic_read(&neigh->probes) >= neigh_max_probes(neigh)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 neigh->nud_state = NUD_FAILED;
1081 notify = 1;
Timo Teras5ef12d92009-06-11 04:16:28 -07001082 neigh_invalidate(neigh);
Duan Jiong5e2c21d2014-02-27 17:03:03 +08001083 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 }
1085
1086 if (neigh->nud_state & NUD_IN_TIMER) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 if (time_before(next, jiffies + HZ/2))
1088 next = jiffies + HZ/2;
Herbert Xu6fb99742005-10-23 16:37:48 +10001089 if (!mod_timer(&neigh->timer, next))
1090 neigh_hold(neigh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 }
1092 if (neigh->nud_state & (NUD_INCOMPLETE | NUD_PROBE)) {
Eric Dumazetcd28ca02011-08-09 08:15:58 +00001093 neigh_probe(neigh);
David S. Miller9ff56602008-02-17 18:39:54 -08001094 } else {
David S. Miller69cc64d2008-02-11 21:45:44 -08001095out:
David S. Miller9ff56602008-02-17 18:39:54 -08001096 write_unlock(&neigh->lock);
1097 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098
Thomas Grafd961db32007-08-08 23:12:56 -07001099 if (notify)
Roopa Prabhu7b8f7a42017-03-19 22:01:28 -07001100 neigh_update_notify(neigh, 0);
Thomas Grafd961db32007-08-08 23:12:56 -07001101
Roopa Prabhu56dd18a2019-02-14 09:15:11 -08001102 trace_neigh_timer_handler(neigh, 0);
1103
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 neigh_release(neigh);
1105}
1106
1107int __neigh_event_send(struct neighbour *neigh, struct sk_buff *skb)
1108{
1109 int rc;
Eric Dumazetcd28ca02011-08-09 08:15:58 +00001110 bool immediate_probe = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111
1112 write_lock_bh(&neigh->lock);
1113
1114 rc = 0;
1115 if (neigh->nud_state & (NUD_CONNECTED | NUD_DELAY | NUD_PROBE))
1116 goto out_unlock_bh;
Julian Anastasov2c51a972015-06-16 22:56:39 +03001117 if (neigh->dead)
1118 goto out_dead;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 if (!(neigh->nud_state & (NUD_STALE | NUD_INCOMPLETE))) {
Jiri Pirko1f9248e2013-12-07 19:26:53 +01001121 if (NEIGH_VAR(neigh->parms, MCAST_PROBES) +
1122 NEIGH_VAR(neigh->parms, APP_PROBES)) {
Eric Dumazetcd28ca02011-08-09 08:15:58 +00001123 unsigned long next, now = jiffies;
1124
Jiri Pirko1f9248e2013-12-07 19:26:53 +01001125 atomic_set(&neigh->probes,
1126 NEIGH_VAR(neigh->parms, UCAST_PROBES));
Lorenzo Bianconi071c3792019-07-14 23:36:11 +02001127 neigh_del_timer(neigh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 neigh->nud_state = NUD_INCOMPLETE;
Eric Dumazetcd28ca02011-08-09 08:15:58 +00001129 neigh->updated = now;
Jiri Pirko1f9248e2013-12-07 19:26:53 +01001130 next = now + max(NEIGH_VAR(neigh->parms, RETRANS_TIME),
1131 HZ/2);
Eric Dumazetcd28ca02011-08-09 08:15:58 +00001132 neigh_add_timer(neigh, next);
1133 immediate_probe = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 } else {
1135 neigh->nud_state = NUD_FAILED;
YOSHIFUJI Hideaki955aaa22006-03-20 16:52:52 -08001136 neigh->updated = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 write_unlock_bh(&neigh->lock);
1138
Wei Yongjunf3fbbe02009-02-25 00:37:32 +00001139 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 return 1;
1141 }
1142 } else if (neigh->nud_state & NUD_STALE) {
Joe Perchesd5d427c2013-04-15 15:17:19 +00001143 neigh_dbg(2, "neigh %p is delayed\n", neigh);
Lorenzo Bianconi071c3792019-07-14 23:36:11 +02001144 neigh_del_timer(neigh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 neigh->nud_state = NUD_DELAY;
YOSHIFUJI Hideaki955aaa22006-03-20 16:52:52 -08001146 neigh->updated = jiffies;
Jiri Pirko1f9248e2013-12-07 19:26:53 +01001147 neigh_add_timer(neigh, jiffies +
1148 NEIGH_VAR(neigh->parms, DELAY_PROBE_TIME));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 }
1150
1151 if (neigh->nud_state == NUD_INCOMPLETE) {
1152 if (skb) {
Eric Dumazet8b5c1712011-11-09 12:07:14 +00001153 while (neigh->arp_queue_len_bytes + skb->truesize >
Jiri Pirko1f9248e2013-12-07 19:26:53 +01001154 NEIGH_VAR(neigh->parms, QUEUE_LEN_BYTES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 struct sk_buff *buff;
Eric Dumazet8b5c1712011-11-09 12:07:14 +00001156
David S. Millerf72051b2008-09-23 01:11:18 -07001157 buff = __skb_dequeue(&neigh->arp_queue);
Eric Dumazet8b5c1712011-11-09 12:07:14 +00001158 if (!buff)
1159 break;
1160 neigh->arp_queue_len_bytes -= buff->truesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 kfree_skb(buff);
Neil Horman9a6d2762008-07-16 20:50:49 -07001162 NEIGH_CACHE_STAT_INC(neigh->tbl, unres_discards);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 }
Eric Dumazeta4731132010-05-27 16:09:39 -07001164 skb_dst_force(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 __skb_queue_tail(&neigh->arp_queue, skb);
Eric Dumazet8b5c1712011-11-09 12:07:14 +00001166 neigh->arp_queue_len_bytes += skb->truesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 }
1168 rc = 1;
1169 }
1170out_unlock_bh:
Eric Dumazetcd28ca02011-08-09 08:15:58 +00001171 if (immediate_probe)
1172 neigh_probe(neigh);
1173 else
1174 write_unlock(&neigh->lock);
1175 local_bh_enable();
Roopa Prabhu56dd18a2019-02-14 09:15:11 -08001176 trace_neigh_event_send_done(neigh, rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 return rc;
Julian Anastasov2c51a972015-06-16 22:56:39 +03001178
1179out_dead:
1180 if (neigh->nud_state & NUD_STALE)
1181 goto out_unlock_bh;
1182 write_unlock_bh(&neigh->lock);
1183 kfree_skb(skb);
Roopa Prabhu56dd18a2019-02-14 09:15:11 -08001184 trace_neigh_event_send_dead(neigh, 1);
Julian Anastasov2c51a972015-06-16 22:56:39 +03001185 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186}
YOSHIFUJI Hideaki0a204502008-03-24 18:39:10 +09001187EXPORT_SYMBOL(__neigh_event_send);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188
David S. Millerf6b72b622011-07-14 07:53:20 -07001189static void neigh_update_hhs(struct neighbour *neigh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190{
1191 struct hh_cache *hh;
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -07001192 void (*update)(struct hh_cache*, const struct net_device*, const unsigned char *)
Doug Kehn91a72a72010-07-14 18:02:16 -07001193 = NULL;
1194
1195 if (neigh->dev->header_ops)
1196 update = neigh->dev->header_ops->cache_update;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197
1198 if (update) {
David S. Millerf6b72b622011-07-14 07:53:20 -07001199 hh = &neigh->hh;
Eric Dumazetc305c6ae2019-11-07 18:29:11 -08001200 if (READ_ONCE(hh->hh_len)) {
Stephen Hemminger3644f0c2006-12-07 15:08:17 -08001201 write_seqlock_bh(&hh->hh_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 update(hh, neigh->dev, neigh->ha);
Stephen Hemminger3644f0c2006-12-07 15:08:17 -08001203 write_sequnlock_bh(&hh->hh_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 }
1205 }
1206}
1207
1208
1209
1210/* Generic update routine.
1211 -- lladdr is new lladdr or NULL, if it is not supplied.
1212 -- new is new state.
1213 -- flags
1214 NEIGH_UPDATE_F_OVERRIDE allows to override existing lladdr,
1215 if it is different.
1216 NEIGH_UPDATE_F_WEAK_OVERRIDE will suspect existing "connected"
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001217 lladdr instead of overriding it
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 if it is different.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 NEIGH_UPDATE_F_ADMIN means that the change is administrative.
1220
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001221 NEIGH_UPDATE_F_OVERRIDE_ISROUTER allows to override existing
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 NTF_ROUTER flag.
1223 NEIGH_UPDATE_F_ISROUTER indicates if the neighbour is known as
1224 a router.
1225
1226 Caller MUST hold reference count on the entry.
1227 */
1228
David Ahern7a35a502018-12-05 20:02:29 -08001229static int __neigh_update(struct neighbour *neigh, const u8 *lladdr,
1230 u8 new, u32 flags, u32 nlmsg_pid,
1231 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232{
David Aherne997f8a2018-12-11 18:57:25 -07001233 bool ext_learn_change = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 u8 old;
1235 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 int notify = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 struct net_device *dev;
1238 int update_isrouter = 0;
1239
Roopa Prabhu56dd18a2019-02-14 09:15:11 -08001240 trace_neigh_update(neigh, lladdr, new, flags, nlmsg_pid);
1241
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 write_lock_bh(&neigh->lock);
1243
1244 dev = neigh->dev;
1245 old = neigh->nud_state;
1246 err = -EPERM;
1247
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001248 if (!(flags & NEIGH_UPDATE_F_ADMIN) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 (old & (NUD_NOARP | NUD_PERMANENT)))
1250 goto out;
David Ahern7a35a502018-12-05 20:02:29 -08001251 if (neigh->dead) {
1252 NL_SET_ERR_MSG(extack, "Neighbor entry is now dead");
Julian Anastasov2c51a972015-06-16 22:56:39 +03001253 goto out;
David Ahern7a35a502018-12-05 20:02:29 -08001254 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255
David Aherne997f8a2018-12-11 18:57:25 -07001256 ext_learn_change = neigh_update_ext_learned(neigh, flags, &notify);
Roopa Prabhu9ce33e42018-04-24 13:49:34 -07001257
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 if (!(new & NUD_VALID)) {
1259 neigh_del_timer(neigh);
1260 if (old & NUD_CONNECTED)
1261 neigh_suspect(neigh);
David Ahern9c29a2f2018-12-11 18:57:21 -07001262 neigh->nud_state = new;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 notify = old & NUD_VALID;
Roopa Prabhud2fb4fb2018-10-20 18:09:31 -07001265 if ((old & (NUD_INCOMPLETE | NUD_PROBE)) &&
Timo Teras5ef12d92009-06-11 04:16:28 -07001266 (new & NUD_FAILED)) {
1267 neigh_invalidate(neigh);
1268 notify = 1;
1269 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 goto out;
1271 }
1272
1273 /* Compare new lladdr with cached one */
1274 if (!dev->addr_len) {
1275 /* First case: device needs no address. */
1276 lladdr = neigh->ha;
1277 } else if (lladdr) {
1278 /* The second case: if something is already cached
1279 and a new address is proposed:
1280 - compare new & old
1281 - if they are different, check override flag
1282 */
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001283 if ((old & NUD_VALID) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 !memcmp(lladdr, neigh->ha, dev->addr_len))
1285 lladdr = neigh->ha;
1286 } else {
1287 /* No address is supplied; if we know something,
1288 use it, otherwise discard the request.
1289 */
1290 err = -EINVAL;
David Ahern7a35a502018-12-05 20:02:29 -08001291 if (!(old & NUD_VALID)) {
1292 NL_SET_ERR_MSG(extack, "No link layer address given");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 goto out;
David Ahern7a35a502018-12-05 20:02:29 -08001294 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 lladdr = neigh->ha;
1296 }
1297
Vasily Khoruzhickf0e0d042018-09-13 11:12:03 -07001298 /* Update confirmed timestamp for neighbour entry after we
1299 * received ARP packet even if it doesn't change IP to MAC binding.
1300 */
1301 if (new & NUD_CONNECTED)
1302 neigh->confirmed = jiffies;
1303
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 /* If entry was valid and address is not changed,
1305 do not change entry state, if new one is STALE.
1306 */
1307 err = 0;
1308 update_isrouter = flags & NEIGH_UPDATE_F_OVERRIDE_ISROUTER;
1309 if (old & NUD_VALID) {
1310 if (lladdr != neigh->ha && !(flags & NEIGH_UPDATE_F_OVERRIDE)) {
1311 update_isrouter = 0;
1312 if ((flags & NEIGH_UPDATE_F_WEAK_OVERRIDE) &&
1313 (old & NUD_CONNECTED)) {
1314 lladdr = neigh->ha;
1315 new = NUD_STALE;
1316 } else
1317 goto out;
1318 } else {
Julian Anastasov0e7bbcc2016-07-27 09:56:50 +03001319 if (lladdr == neigh->ha && new == NUD_STALE &&
1320 !(flags & NEIGH_UPDATE_F_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 new = old;
1322 }
1323 }
1324
Vasily Khoruzhickf0e0d042018-09-13 11:12:03 -07001325 /* Update timestamp only once we know we will make a change to the
Ihar Hrachyshka77d71232017-05-16 08:44:24 -07001326 * neighbour entry. Otherwise we risk to move the locktime window with
1327 * noop updates and ignore relevant ARP updates.
1328 */
Vasily Khoruzhickf0e0d042018-09-13 11:12:03 -07001329 if (new != old || lladdr != neigh->ha)
Ihar Hrachyshka77d71232017-05-16 08:44:24 -07001330 neigh->updated = jiffies;
Ihar Hrachyshka77d71232017-05-16 08:44:24 -07001331
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 if (new != old) {
1333 neigh_del_timer(neigh);
Erik Kline765c9c62015-05-18 19:44:41 +09001334 if (new & NUD_PROBE)
1335 atomic_set(&neigh->probes, 0);
Pavel Emelyanova43d8992007-12-20 15:49:05 -08001336 if (new & NUD_IN_TIMER)
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001337 neigh_add_timer(neigh, (jiffies +
1338 ((new & NUD_REACHABLE) ?
David S. Miller667347f2005-09-27 12:07:44 -07001339 neigh->parms->reachable_time :
1340 0)));
David Ahern9c29a2f2018-12-11 18:57:21 -07001341 neigh->nud_state = new;
Bob Gilligan53385d22013-12-15 13:39:56 -08001342 notify = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 }
1344
1345 if (lladdr != neigh->ha) {
Eric Dumazet0ed8ddf2010-10-07 10:44:07 +00001346 write_seqlock(&neigh->ha_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 memcpy(&neigh->ha, lladdr, dev->addr_len);
Eric Dumazet0ed8ddf2010-10-07 10:44:07 +00001348 write_sequnlock(&neigh->ha_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 neigh_update_hhs(neigh);
1350 if (!(new & NUD_CONNECTED))
1351 neigh->confirmed = jiffies -
Jiri Pirko1f9248e2013-12-07 19:26:53 +01001352 (NEIGH_VAR(neigh->parms, BASE_REACHABLE_TIME) << 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 notify = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 }
1355 if (new == old)
1356 goto out;
1357 if (new & NUD_CONNECTED)
1358 neigh_connect(neigh);
1359 else
1360 neigh_suspect(neigh);
1361 if (!(old & NUD_VALID)) {
1362 struct sk_buff *skb;
1363
1364 /* Again: avoid dead loop if something went wrong */
1365
1366 while (neigh->nud_state & NUD_VALID &&
1367 (skb = __skb_dequeue(&neigh->arp_queue)) != NULL) {
David S. Miller69cce1d2011-07-17 23:09:49 -07001368 struct dst_entry *dst = skb_dst(skb);
1369 struct neighbour *n2, *n1 = neigh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 write_unlock_bh(&neigh->lock);
roy.qing.li@gmail.come049f282011-10-17 22:32:42 +00001371
1372 rcu_read_lock();
David S. Miller13a43d92012-07-02 22:15:37 -07001373
1374 /* Why not just use 'neigh' as-is? The problem is that
1375 * things such as shaper, eql, and sch_teql can end up
1376 * using alternative, different, neigh objects to output
1377 * the packet in the output path. So what we need to do
1378 * here is re-lookup the top-level neigh in the path so
1379 * we can reinject the packet there.
1380 */
1381 n2 = NULL;
1382 if (dst) {
1383 n2 = dst_neigh_lookup_skb(dst, skb);
1384 if (n2)
1385 n1 = n2;
1386 }
David S. Miller8f40b162011-07-17 13:34:11 -07001387 n1->output(n1, skb);
David S. Miller13a43d92012-07-02 22:15:37 -07001388 if (n2)
1389 neigh_release(n2);
roy.qing.li@gmail.come049f282011-10-17 22:32:42 +00001390 rcu_read_unlock();
1391
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 write_lock_bh(&neigh->lock);
1393 }
Eric Dumazetc9ab4d82013-06-28 02:37:42 -07001394 __skb_queue_purge(&neigh->arp_queue);
Eric Dumazet8b5c1712011-11-09 12:07:14 +00001395 neigh->arp_queue_len_bytes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 }
1397out:
Roopa Prabhufc6e8072018-09-22 21:26:20 -07001398 if (update_isrouter)
1399 neigh_update_is_router(neigh, flags, &notify);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 write_unlock_bh(&neigh->lock);
Tom Tucker8d717402006-07-30 20:43:36 -07001401
David Aherne997f8a2018-12-11 18:57:25 -07001402 if (((new ^ old) & NUD_PERMANENT) || ext_learn_change)
David Ahern9c29a2f2018-12-11 18:57:21 -07001403 neigh_update_gc_list(neigh);
1404
Tom Tucker8d717402006-07-30 20:43:36 -07001405 if (notify)
Roopa Prabhu7b8f7a42017-03-19 22:01:28 -07001406 neigh_update_notify(neigh, nlmsg_pid);
Thomas Grafd961db32007-08-08 23:12:56 -07001407
Roopa Prabhu56dd18a2019-02-14 09:15:11 -08001408 trace_neigh_update_done(neigh, err);
1409
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 return err;
1411}
David Ahern7a35a502018-12-05 20:02:29 -08001412
1413int neigh_update(struct neighbour *neigh, const u8 *lladdr, u8 new,
1414 u32 flags, u32 nlmsg_pid)
1415{
1416 return __neigh_update(neigh, lladdr, new, flags, nlmsg_pid, NULL);
1417}
YOSHIFUJI Hideaki0a204502008-03-24 18:39:10 +09001418EXPORT_SYMBOL(neigh_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419
Jiri Benc7e980562013-12-11 13:48:20 +01001420/* Update the neigh to listen temporarily for probe responses, even if it is
1421 * in a NUD_FAILED state. The caller has to hold neigh->lock for writing.
1422 */
1423void __neigh_set_probe_once(struct neighbour *neigh)
1424{
Julian Anastasov2c51a972015-06-16 22:56:39 +03001425 if (neigh->dead)
1426 return;
Jiri Benc7e980562013-12-11 13:48:20 +01001427 neigh->updated = jiffies;
1428 if (!(neigh->nud_state & NUD_FAILED))
1429 return;
Duan Jiong2176d5d2014-05-09 13:16:48 +08001430 neigh->nud_state = NUD_INCOMPLETE;
1431 atomic_set(&neigh->probes, neigh_max_probes(neigh));
Jiri Benc7e980562013-12-11 13:48:20 +01001432 neigh_add_timer(neigh,
1433 jiffies + NEIGH_VAR(neigh->parms, RETRANS_TIME));
1434}
1435EXPORT_SYMBOL(__neigh_set_probe_once);
1436
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437struct neighbour *neigh_event_ns(struct neigh_table *tbl,
1438 u8 *lladdr, void *saddr,
1439 struct net_device *dev)
1440{
1441 struct neighbour *neigh = __neigh_lookup(tbl, saddr, dev,
1442 lladdr || !dev->addr_len);
1443 if (neigh)
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001444 neigh_update(neigh, lladdr, NUD_STALE,
Roopa Prabhu7b8f7a42017-03-19 22:01:28 -07001445 NEIGH_UPDATE_F_OVERRIDE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 return neigh;
1447}
YOSHIFUJI Hideaki0a204502008-03-24 18:39:10 +09001448EXPORT_SYMBOL(neigh_event_ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449
Eric Dumazet34d101d2010-10-11 09:16:57 -07001450/* called with read_lock_bh(&n->lock); */
Eric W. Biedermanbdf53c52015-03-02 00:13:22 -06001451static void neigh_hh_init(struct neighbour *n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452{
Eric W. Biedermanbdf53c52015-03-02 00:13:22 -06001453 struct net_device *dev = n->dev;
1454 __be16 prot = n->tbl->protocol;
David S. Millerf6b72b622011-07-14 07:53:20 -07001455 struct hh_cache *hh = &n->hh;
Eric Dumazet0ed8ddf2010-10-07 10:44:07 +00001456
1457 write_lock_bh(&n->lock);
Eric Dumazet34d101d2010-10-11 09:16:57 -07001458
David S. Millerf6b72b622011-07-14 07:53:20 -07001459 /* Only one thread can come in here and initialize the
1460 * hh_cache entry.
1461 */
David S. Millerb23b5452011-07-16 17:45:02 -07001462 if (!hh->hh_len)
1463 dev->header_ops->cache(n, hh, prot);
David S. Millerf6b72b622011-07-14 07:53:20 -07001464
Eric Dumazet0ed8ddf2010-10-07 10:44:07 +00001465 write_unlock_bh(&n->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466}
1467
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468/* Slow and careful. */
1469
David S. Miller8f40b162011-07-17 13:34:11 -07001470int neigh_resolve_output(struct neighbour *neigh, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 int rc = 0;
1473
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 if (!neigh_event_send(neigh, skb)) {
1475 int err;
1476 struct net_device *dev = neigh->dev;
Eric Dumazet0ed8ddf2010-10-07 10:44:07 +00001477 unsigned int seq;
Eric Dumazet34d101d2010-10-11 09:16:57 -07001478
Eric Dumazetc305c6ae2019-11-07 18:29:11 -08001479 if (dev->header_ops->cache && !READ_ONCE(neigh->hh.hh_len))
Eric W. Biedermanbdf53c52015-03-02 00:13:22 -06001480 neigh_hh_init(neigh);
Eric Dumazet34d101d2010-10-11 09:16:57 -07001481
Eric Dumazet0ed8ddf2010-10-07 10:44:07 +00001482 do {
ramesh.nagappa@gmail.come1f16502012-10-05 19:10:15 +00001483 __skb_pull(skb, skb_network_offset(skb));
Eric Dumazet0ed8ddf2010-10-07 10:44:07 +00001484 seq = read_seqbegin(&neigh->ha_lock);
1485 err = dev_hard_header(skb, dev, ntohs(skb->protocol),
1486 neigh->ha, NULL, skb->len);
1487 } while (read_seqretry(&neigh->ha_lock, seq));
Eric Dumazet34d101d2010-10-11 09:16:57 -07001488
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 if (err >= 0)
David S. Miller542d4d62011-07-16 18:06:24 -07001490 rc = dev_queue_xmit(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 else
1492 goto out_kfree_skb;
1493 }
1494out:
1495 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496out_kfree_skb:
1497 rc = -EINVAL;
1498 kfree_skb(skb);
1499 goto out;
1500}
YOSHIFUJI Hideaki0a204502008-03-24 18:39:10 +09001501EXPORT_SYMBOL(neigh_resolve_output);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502
1503/* As fast as possible without hh cache */
1504
David S. Miller8f40b162011-07-17 13:34:11 -07001505int neigh_connected_output(struct neighbour *neigh, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 struct net_device *dev = neigh->dev;
Eric Dumazet0ed8ddf2010-10-07 10:44:07 +00001508 unsigned int seq;
David S. Miller8f40b162011-07-17 13:34:11 -07001509 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510
Eric Dumazet0ed8ddf2010-10-07 10:44:07 +00001511 do {
ramesh.nagappa@gmail.come1f16502012-10-05 19:10:15 +00001512 __skb_pull(skb, skb_network_offset(skb));
Eric Dumazet0ed8ddf2010-10-07 10:44:07 +00001513 seq = read_seqbegin(&neigh->ha_lock);
1514 err = dev_hard_header(skb, dev, ntohs(skb->protocol),
1515 neigh->ha, NULL, skb->len);
1516 } while (read_seqretry(&neigh->ha_lock, seq));
1517
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 if (err >= 0)
David S. Miller542d4d62011-07-16 18:06:24 -07001519 err = dev_queue_xmit(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 else {
1521 err = -EINVAL;
1522 kfree_skb(skb);
1523 }
1524 return err;
1525}
YOSHIFUJI Hideaki0a204502008-03-24 18:39:10 +09001526EXPORT_SYMBOL(neigh_connected_output);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527
David S. Miller8f40b162011-07-17 13:34:11 -07001528int neigh_direct_output(struct neighbour *neigh, struct sk_buff *skb)
1529{
1530 return dev_queue_xmit(skb);
1531}
1532EXPORT_SYMBOL(neigh_direct_output);
1533
Kees Cooke99e88a2017-10-16 14:43:17 -07001534static void neigh_proxy_process(struct timer_list *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535{
Kees Cooke99e88a2017-10-16 14:43:17 -07001536 struct neigh_table *tbl = from_timer(tbl, t, proxy_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 long sched_next = 0;
1538 unsigned long now = jiffies;
David S. Millerf72051b2008-09-23 01:11:18 -07001539 struct sk_buff *skb, *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540
1541 spin_lock(&tbl->proxy_queue.lock);
1542
David S. Millerf72051b2008-09-23 01:11:18 -07001543 skb_queue_walk_safe(&tbl->proxy_queue, skb, n) {
1544 long tdif = NEIGH_CB(skb)->sched_next - now;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 if (tdif <= 0) {
David S. Millerf72051b2008-09-23 01:11:18 -07001547 struct net_device *dev = skb->dev;
Eric Dumazet20e60742011-08-22 19:32:42 +00001548
David S. Millerf72051b2008-09-23 01:11:18 -07001549 __skb_unlink(skb, &tbl->proxy_queue);
Eric Dumazet20e60742011-08-22 19:32:42 +00001550 if (tbl->proxy_redo && netif_running(dev)) {
1551 rcu_read_lock();
David S. Millerf72051b2008-09-23 01:11:18 -07001552 tbl->proxy_redo(skb);
Eric Dumazet20e60742011-08-22 19:32:42 +00001553 rcu_read_unlock();
1554 } else {
David S. Millerf72051b2008-09-23 01:11:18 -07001555 kfree_skb(skb);
Eric Dumazet20e60742011-08-22 19:32:42 +00001556 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557
1558 dev_put(dev);
1559 } else if (!sched_next || tdif < sched_next)
1560 sched_next = tdif;
1561 }
1562 del_timer(&tbl->proxy_timer);
1563 if (sched_next)
1564 mod_timer(&tbl->proxy_timer, jiffies + sched_next);
1565 spin_unlock(&tbl->proxy_queue.lock);
1566}
1567
1568void pneigh_enqueue(struct neigh_table *tbl, struct neigh_parms *p,
1569 struct sk_buff *skb)
1570{
1571 unsigned long now = jiffies;
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -05001572
1573 unsigned long sched_next = now + (prandom_u32() %
Jiri Pirko1f9248e2013-12-07 19:26:53 +01001574 NEIGH_VAR(p, PROXY_DELAY));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575
Jiri Pirko1f9248e2013-12-07 19:26:53 +01001576 if (tbl->proxy_queue.qlen > NEIGH_VAR(p, PROXY_QLEN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 kfree_skb(skb);
1578 return;
1579 }
Patrick McHardya61bbcf2005-08-14 17:24:31 -07001580
1581 NEIGH_CB(skb)->sched_next = sched_next;
1582 NEIGH_CB(skb)->flags |= LOCALLY_ENQUEUED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583
1584 spin_lock(&tbl->proxy_queue.lock);
1585 if (del_timer(&tbl->proxy_timer)) {
1586 if (time_before(tbl->proxy_timer.expires, sched_next))
1587 sched_next = tbl->proxy_timer.expires;
1588 }
Eric Dumazetadf30902009-06-02 05:19:30 +00001589 skb_dst_drop(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 dev_hold(skb->dev);
1591 __skb_queue_tail(&tbl->proxy_queue, skb);
1592 mod_timer(&tbl->proxy_timer, sched_next);
1593 spin_unlock(&tbl->proxy_queue.lock);
1594}
YOSHIFUJI Hideaki0a204502008-03-24 18:39:10 +09001595EXPORT_SYMBOL(pneigh_enqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596
Tobias Klauser97fd5bc2009-07-13 11:17:49 -07001597static inline struct neigh_parms *lookup_neigh_parms(struct neigh_table *tbl,
Eric W. Biederman426b5302008-01-24 00:13:18 -08001598 struct net *net, int ifindex)
1599{
1600 struct neigh_parms *p;
1601
Nicolas Dichtel75fbfd32014-10-29 19:29:31 +01001602 list_for_each_entry(p, &tbl->parms_list, list) {
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09001603 if ((p->dev && p->dev->ifindex == ifindex && net_eq(neigh_parms_net(p), net)) ||
Gao feng170d6f92013-06-20 10:01:33 +08001604 (!p->dev && !ifindex && net_eq(net, &init_net)))
Eric W. Biederman426b5302008-01-24 00:13:18 -08001605 return p;
1606 }
1607
1608 return NULL;
1609}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610
1611struct neigh_parms *neigh_parms_alloc(struct net_device *dev,
1612 struct neigh_table *tbl)
1613{
Gao fengcf89d6b2013-06-20 10:01:32 +08001614 struct neigh_parms *p;
Stephen Hemminger00829822008-11-20 20:14:53 -08001615 struct net *net = dev_net(dev);
1616 const struct net_device_ops *ops = dev->netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617
Gao fengcf89d6b2013-06-20 10:01:32 +08001618 p = kmemdup(&tbl->parms, sizeof(*p), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 if (p) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 p->tbl = tbl;
Reshetova, Elena63439442017-06-30 13:07:56 +03001621 refcount_set(&p->refcnt, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 p->reachable_time =
Jiri Pirko1f9248e2013-12-07 19:26:53 +01001623 neigh_rand_reach_time(NEIGH_VAR(p, BASE_REACHABLE_TIME));
Denis V. Lunev486b51d2008-01-14 22:59:59 -08001624 dev_hold(dev);
1625 p->dev = dev;
Eric W. Biedermanefd7ef12015-03-11 23:04:08 -05001626 write_pnet(&p->net, net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 p->sysctl_table = NULL;
Veaceslav Falico63134802013-08-02 19:07:38 +02001628
1629 if (ops->ndo_neigh_setup && ops->ndo_neigh_setup(dev, p)) {
Veaceslav Falico63134802013-08-02 19:07:38 +02001630 dev_put(dev);
1631 kfree(p);
1632 return NULL;
1633 }
1634
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 write_lock_bh(&tbl->lock);
Nicolas Dichtel75fbfd32014-10-29 19:29:31 +01001636 list_add(&p->list, &tbl->parms.list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 write_unlock_bh(&tbl->lock);
Jiri Pirko1d4c8c22013-12-07 19:26:56 +01001638
1639 neigh_parms_data_state_cleanall(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 }
1641 return p;
1642}
YOSHIFUJI Hideaki0a204502008-03-24 18:39:10 +09001643EXPORT_SYMBOL(neigh_parms_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644
1645static void neigh_rcu_free_parms(struct rcu_head *head)
1646{
1647 struct neigh_parms *parms =
1648 container_of(head, struct neigh_parms, rcu_head);
1649
1650 neigh_parms_put(parms);
1651}
1652
1653void neigh_parms_release(struct neigh_table *tbl, struct neigh_parms *parms)
1654{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 if (!parms || parms == &tbl->parms)
1656 return;
1657 write_lock_bh(&tbl->lock);
Nicolas Dichtel75fbfd32014-10-29 19:29:31 +01001658 list_del(&parms->list);
1659 parms->dead = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 write_unlock_bh(&tbl->lock);
Nicolas Dichtel75fbfd32014-10-29 19:29:31 +01001661 if (parms->dev)
1662 dev_put(parms->dev);
1663 call_rcu(&parms->rcu_head, neigh_rcu_free_parms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664}
YOSHIFUJI Hideaki0a204502008-03-24 18:39:10 +09001665EXPORT_SYMBOL(neigh_parms_release);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666
Denis V. Lunev06f05112008-01-24 00:30:58 -08001667static void neigh_parms_destroy(struct neigh_parms *parms)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668{
1669 kfree(parms);
1670}
1671
Pavel Emelianovc2ecba72007-04-17 12:45:31 -07001672static struct lock_class_key neigh_table_proxy_queue_class;
1673
WANG Congd7480fd2014-11-10 15:59:36 -08001674static struct neigh_table *neigh_tables[NEIGH_NR_TABLES] __read_mostly;
1675
1676void neigh_table_init(int index, struct neigh_table *tbl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677{
1678 unsigned long now = jiffies;
1679 unsigned long phsize;
1680
Nicolas Dichtel75fbfd32014-10-29 19:29:31 +01001681 INIT_LIST_HEAD(&tbl->parms_list);
David Ahern58956312018-12-07 12:24:57 -08001682 INIT_LIST_HEAD(&tbl->gc_list);
Nicolas Dichtel75fbfd32014-10-29 19:29:31 +01001683 list_add(&tbl->parms.list, &tbl->parms_list);
Eric Dumazete42ea982008-11-12 00:54:54 -08001684 write_pnet(&tbl->parms.net, &init_net);
Reshetova, Elena63439442017-06-30 13:07:56 +03001685 refcount_set(&tbl->parms.refcnt, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 tbl->parms.reachable_time =
Jiri Pirko1f9248e2013-12-07 19:26:53 +01001687 neigh_rand_reach_time(NEIGH_VAR(&tbl->parms, BASE_REACHABLE_TIME));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 tbl->stats = alloc_percpu(struct neigh_statistics);
1690 if (!tbl->stats)
1691 panic("cannot create neighbour cache statistics");
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001692
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693#ifdef CONFIG_PROC_FS
Christoph Hellwig71a50532018-04-15 10:16:41 +02001694 if (!proc_create_seq_data(tbl->id, 0, init_net.proc_net_stat,
1695 &neigh_stat_seq_ops, tbl))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 panic("cannot create neighbour proc dir entry");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697#endif
1698
David S. Millercd089332011-07-11 01:28:12 -07001699 RCU_INIT_POINTER(tbl->nht, neigh_hash_alloc(3));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700
1701 phsize = (PNEIGH_HASHMASK + 1) * sizeof(struct pneigh_entry *);
Andrew Morton77d04bd2006-04-07 14:52:59 -07001702 tbl->phash_buckets = kzalloc(phsize, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703
Eric Dumazetd6bf7812010-10-04 06:15:44 +00001704 if (!tbl->nht || !tbl->phash_buckets)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 panic("cannot allocate neighbour cache hashes");
1706
YOSHIFUJI Hideaki / 吉藤英明08433ef2013-01-24 00:44:23 +00001707 if (!tbl->entry_size)
1708 tbl->entry_size = ALIGN(offsetof(struct neighbour, primary_key) +
1709 tbl->key_len, NEIGH_PRIV_ALIGN);
1710 else
1711 WARN_ON(tbl->entry_size % NEIGH_PRIV_ALIGN);
1712
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 rwlock_init(&tbl->lock);
Tejun Heo203b42f2012-08-21 13:18:23 -07001714 INIT_DEFERRABLE_WORK(&tbl->gc_work, neigh_periodic_work);
viresh kumarf6180022014-01-22 12:23:33 +05301715 queue_delayed_work(system_power_efficient_wq, &tbl->gc_work,
1716 tbl->parms.reachable_time);
Kees Cooke99e88a2017-10-16 14:43:17 -07001717 timer_setup(&tbl->proxy_timer, neigh_proxy_process, 0);
Pavel Emelianovc2ecba72007-04-17 12:45:31 -07001718 skb_queue_head_init_class(&tbl->proxy_queue,
1719 &neigh_table_proxy_queue_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720
1721 tbl->last_flush = now;
1722 tbl->last_rand = now + tbl->parms.reachable_time * 20;
Simon Kelleybd89efc2006-05-12 14:56:08 -07001723
WANG Congd7480fd2014-11-10 15:59:36 -08001724 neigh_tables[index] = tbl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725}
YOSHIFUJI Hideaki0a204502008-03-24 18:39:10 +09001726EXPORT_SYMBOL(neigh_table_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727
WANG Congd7480fd2014-11-10 15:59:36 -08001728int neigh_table_clear(int index, struct neigh_table *tbl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729{
WANG Congd7480fd2014-11-10 15:59:36 -08001730 neigh_tables[index] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 /* It is not clean... Fix it to unload IPv6 module safely */
Tejun Heoa5c30b32010-10-19 06:04:42 +00001732 cancel_delayed_work_sync(&tbl->gc_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 del_timer_sync(&tbl->proxy_timer);
1734 pneigh_queue_purge(&tbl->proxy_queue);
1735 neigh_ifdown(tbl, NULL);
1736 if (atomic_read(&tbl->entries))
Joe Perchese005d192012-05-16 19:58:40 +00001737 pr_crit("neighbour leakage\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738
Eric Dumazet6193d2b2011-01-19 22:02:47 +00001739 call_rcu(&rcu_dereference_protected(tbl->nht, 1)->rcu,
1740 neigh_hash_free_rcu);
Eric Dumazetd6bf7812010-10-04 06:15:44 +00001741 tbl->nht = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742
1743 kfree(tbl->phash_buckets);
1744 tbl->phash_buckets = NULL;
1745
Alexey Dobriyan3f192b52007-11-05 21:28:13 -08001746 remove_proc_entry(tbl->id, init_net.proc_net_stat);
1747
Kirill Korotaev3fcde742006-09-01 01:34:10 -07001748 free_percpu(tbl->stats);
1749 tbl->stats = NULL;
1750
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 return 0;
1752}
YOSHIFUJI Hideaki0a204502008-03-24 18:39:10 +09001753EXPORT_SYMBOL(neigh_table_clear);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754
WANG Congd7480fd2014-11-10 15:59:36 -08001755static struct neigh_table *neigh_find_table(int family)
1756{
1757 struct neigh_table *tbl = NULL;
1758
1759 switch (family) {
1760 case AF_INET:
1761 tbl = neigh_tables[NEIGH_ARP_TABLE];
1762 break;
1763 case AF_INET6:
1764 tbl = neigh_tables[NEIGH_ND_TABLE];
1765 break;
1766 case AF_DECnet:
1767 tbl = neigh_tables[NEIGH_DN_TABLE];
1768 break;
1769 }
1770
1771 return tbl;
1772}
1773
Roopa Prabhu82cbb5c2018-12-19 12:51:38 -08001774const struct nla_policy nda_policy[NDA_MAX+1] = {
1775 [NDA_DST] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1776 [NDA_LLADDR] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1777 [NDA_CACHEINFO] = { .len = sizeof(struct nda_cacheinfo) },
1778 [NDA_PROBES] = { .type = NLA_U32 },
1779 [NDA_VLAN] = { .type = NLA_U16 },
1780 [NDA_PORT] = { .type = NLA_U16 },
1781 [NDA_VNI] = { .type = NLA_U32 },
1782 [NDA_IFINDEX] = { .type = NLA_U32 },
1783 [NDA_MASTER] = { .type = NLA_U32 },
David Aherna9cd3432018-12-19 20:02:36 -08001784 [NDA_PROTOCOL] = { .type = NLA_U8 },
Roopa Prabhu82cbb5c2018-12-19 12:51:38 -08001785};
1786
David Ahernc21ef3e2017-04-16 09:48:24 -07001787static int neigh_delete(struct sk_buff *skb, struct nlmsghdr *nlh,
1788 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001790 struct net *net = sock_net(skb->sk);
Thomas Grafa14a49d2006-08-07 17:53:08 -07001791 struct ndmsg *ndm;
1792 struct nlattr *dst_attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 struct neigh_table *tbl;
WANG Congd7480fd2014-11-10 15:59:36 -08001794 struct neighbour *neigh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 struct net_device *dev = NULL;
Thomas Grafa14a49d2006-08-07 17:53:08 -07001796 int err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797
Eric Dumazet110b2492010-10-04 04:27:36 +00001798 ASSERT_RTNL();
Thomas Grafa14a49d2006-08-07 17:53:08 -07001799 if (nlmsg_len(nlh) < sizeof(*ndm))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 goto out;
1801
Thomas Grafa14a49d2006-08-07 17:53:08 -07001802 dst_attr = nlmsg_find_attr(nlh, sizeof(*ndm), NDA_DST);
David Ahern7a35a502018-12-05 20:02:29 -08001803 if (!dst_attr) {
1804 NL_SET_ERR_MSG(extack, "Network address not specified");
Thomas Grafa14a49d2006-08-07 17:53:08 -07001805 goto out;
David Ahern7a35a502018-12-05 20:02:29 -08001806 }
Thomas Grafa14a49d2006-08-07 17:53:08 -07001807
1808 ndm = nlmsg_data(nlh);
1809 if (ndm->ndm_ifindex) {
Eric Dumazet110b2492010-10-04 04:27:36 +00001810 dev = __dev_get_by_index(net, ndm->ndm_ifindex);
Thomas Grafa14a49d2006-08-07 17:53:08 -07001811 if (dev == NULL) {
1812 err = -ENODEV;
1813 goto out;
1814 }
1815 }
1816
WANG Congd7480fd2014-11-10 15:59:36 -08001817 tbl = neigh_find_table(ndm->ndm_family);
1818 if (tbl == NULL)
1819 return -EAFNOSUPPORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820
David Ahern7a35a502018-12-05 20:02:29 -08001821 if (nla_len(dst_attr) < (int)tbl->key_len) {
1822 NL_SET_ERR_MSG(extack, "Invalid network address");
WANG Congd7480fd2014-11-10 15:59:36 -08001823 goto out;
David Ahern7a35a502018-12-05 20:02:29 -08001824 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825
WANG Congd7480fd2014-11-10 15:59:36 -08001826 if (ndm->ndm_flags & NTF_PROXY) {
1827 err = pneigh_delete(tbl, net, nla_data(dst_attr), dev);
Eric Dumazet110b2492010-10-04 04:27:36 +00001828 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 }
WANG Congd7480fd2014-11-10 15:59:36 -08001830
1831 if (dev == NULL)
1832 goto out;
1833
1834 neigh = neigh_lookup(tbl, nla_data(dst_attr), dev);
1835 if (neigh == NULL) {
1836 err = -ENOENT;
1837 goto out;
1838 }
1839
David Ahern7a35a502018-12-05 20:02:29 -08001840 err = __neigh_update(neigh, NULL, NUD_FAILED,
1841 NEIGH_UPDATE_F_OVERRIDE | NEIGH_UPDATE_F_ADMIN,
1842 NETLINK_CB(skb).portid, extack);
Sowmini Varadhan50710342017-06-02 09:01:49 -07001843 write_lock_bh(&tbl->lock);
WANG Congd7480fd2014-11-10 15:59:36 -08001844 neigh_release(neigh);
Sowmini Varadhan50710342017-06-02 09:01:49 -07001845 neigh_remove_one(neigh, tbl);
1846 write_unlock_bh(&tbl->lock);
Thomas Grafa14a49d2006-08-07 17:53:08 -07001847
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848out:
1849 return err;
1850}
1851
David Ahernc21ef3e2017-04-16 09:48:24 -07001852static int neigh_add(struct sk_buff *skb, struct nlmsghdr *nlh,
1853 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854{
Roopa Prabhuf7aa74e2018-09-22 21:26:19 -07001855 int flags = NEIGH_UPDATE_F_ADMIN | NEIGH_UPDATE_F_OVERRIDE |
1856 NEIGH_UPDATE_F_OVERRIDE_ISROUTER;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001857 struct net *net = sock_net(skb->sk);
Thomas Graf5208deb2006-08-07 17:55:40 -07001858 struct ndmsg *ndm;
1859 struct nlattr *tb[NDA_MAX+1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 struct neigh_table *tbl;
1861 struct net_device *dev = NULL;
WANG Congd7480fd2014-11-10 15:59:36 -08001862 struct neighbour *neigh;
1863 void *dst, *lladdr;
David Aherndf9b0e32018-12-15 14:09:06 -08001864 u8 protocol = 0;
Thomas Graf5208deb2006-08-07 17:55:40 -07001865 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866
Eric Dumazet110b2492010-10-04 04:27:36 +00001867 ASSERT_RTNL();
Johannes Berg8cb08172019-04-26 14:07:28 +02001868 err = nlmsg_parse_deprecated(nlh, sizeof(*ndm), tb, NDA_MAX,
1869 nda_policy, extack);
Thomas Graf5208deb2006-08-07 17:55:40 -07001870 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 goto out;
1872
Thomas Graf5208deb2006-08-07 17:55:40 -07001873 err = -EINVAL;
David Ahern7a35a502018-12-05 20:02:29 -08001874 if (!tb[NDA_DST]) {
1875 NL_SET_ERR_MSG(extack, "Network address not specified");
Thomas Graf5208deb2006-08-07 17:55:40 -07001876 goto out;
David Ahern7a35a502018-12-05 20:02:29 -08001877 }
Thomas Graf5208deb2006-08-07 17:55:40 -07001878
1879 ndm = nlmsg_data(nlh);
1880 if (ndm->ndm_ifindex) {
Eric Dumazet110b2492010-10-04 04:27:36 +00001881 dev = __dev_get_by_index(net, ndm->ndm_ifindex);
Thomas Graf5208deb2006-08-07 17:55:40 -07001882 if (dev == NULL) {
1883 err = -ENODEV;
1884 goto out;
1885 }
1886
David Ahern7a35a502018-12-05 20:02:29 -08001887 if (tb[NDA_LLADDR] && nla_len(tb[NDA_LLADDR]) < dev->addr_len) {
1888 NL_SET_ERR_MSG(extack, "Invalid link address");
Eric Dumazet110b2492010-10-04 04:27:36 +00001889 goto out;
David Ahern7a35a502018-12-05 20:02:29 -08001890 }
Thomas Graf5208deb2006-08-07 17:55:40 -07001891 }
1892
WANG Congd7480fd2014-11-10 15:59:36 -08001893 tbl = neigh_find_table(ndm->ndm_family);
1894 if (tbl == NULL)
1895 return -EAFNOSUPPORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896
David Ahern7a35a502018-12-05 20:02:29 -08001897 if (nla_len(tb[NDA_DST]) < (int)tbl->key_len) {
1898 NL_SET_ERR_MSG(extack, "Invalid network address");
WANG Congd7480fd2014-11-10 15:59:36 -08001899 goto out;
David Ahern7a35a502018-12-05 20:02:29 -08001900 }
1901
WANG Congd7480fd2014-11-10 15:59:36 -08001902 dst = nla_data(tb[NDA_DST]);
1903 lladdr = tb[NDA_LLADDR] ? nla_data(tb[NDA_LLADDR]) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904
David Aherna9cd3432018-12-19 20:02:36 -08001905 if (tb[NDA_PROTOCOL])
David Aherndf9b0e32018-12-15 14:09:06 -08001906 protocol = nla_get_u8(tb[NDA_PROTOCOL]);
David Aherndf9b0e32018-12-15 14:09:06 -08001907
WANG Congd7480fd2014-11-10 15:59:36 -08001908 if (ndm->ndm_flags & NTF_PROXY) {
1909 struct pneigh_entry *pn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910
WANG Congd7480fd2014-11-10 15:59:36 -08001911 err = -ENOBUFS;
1912 pn = pneigh_lookup(tbl, net, dst, dev, 1);
1913 if (pn) {
1914 pn->flags = ndm->ndm_flags;
David Aherndf9b0e32018-12-15 14:09:06 -08001915 if (protocol)
1916 pn->protocol = protocol;
Eric Biederman0c5c2d32009-03-04 00:03:08 -08001917 err = 0;
WANG Congd7480fd2014-11-10 15:59:36 -08001918 }
Eric Dumazet110b2492010-10-04 04:27:36 +00001919 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 }
1921
David Ahern7a35a502018-12-05 20:02:29 -08001922 if (!dev) {
1923 NL_SET_ERR_MSG(extack, "Device not specified");
WANG Congd7480fd2014-11-10 15:59:36 -08001924 goto out;
David Ahern7a35a502018-12-05 20:02:29 -08001925 }
WANG Congd7480fd2014-11-10 15:59:36 -08001926
David Ahernb8fb1ab2019-04-16 17:31:43 -07001927 if (tbl->allow_add && !tbl->allow_add(dev, extack)) {
1928 err = -EINVAL;
1929 goto out;
1930 }
1931
WANG Congd7480fd2014-11-10 15:59:36 -08001932 neigh = neigh_lookup(tbl, dst, dev);
1933 if (neigh == NULL) {
David Aherne997f8a2018-12-11 18:57:25 -07001934 bool exempt_from_gc;
1935
WANG Congd7480fd2014-11-10 15:59:36 -08001936 if (!(nlh->nlmsg_flags & NLM_F_CREATE)) {
1937 err = -ENOENT;
1938 goto out;
1939 }
1940
David Aherne997f8a2018-12-11 18:57:25 -07001941 exempt_from_gc = ndm->ndm_state & NUD_PERMANENT ||
1942 ndm->ndm_flags & NTF_EXT_LEARNED;
1943 neigh = ___neigh_create(tbl, dst, dev, exempt_from_gc, true);
WANG Congd7480fd2014-11-10 15:59:36 -08001944 if (IS_ERR(neigh)) {
1945 err = PTR_ERR(neigh);
1946 goto out;
1947 }
1948 } else {
1949 if (nlh->nlmsg_flags & NLM_F_EXCL) {
1950 err = -EEXIST;
1951 neigh_release(neigh);
1952 goto out;
1953 }
1954
1955 if (!(nlh->nlmsg_flags & NLM_F_REPLACE))
Roopa Prabhuf7aa74e2018-09-22 21:26:19 -07001956 flags &= ~(NEIGH_UPDATE_F_OVERRIDE |
1957 NEIGH_UPDATE_F_OVERRIDE_ISROUTER);
WANG Congd7480fd2014-11-10 15:59:36 -08001958 }
1959
Roopa Prabhu9ce33e42018-04-24 13:49:34 -07001960 if (ndm->ndm_flags & NTF_EXT_LEARNED)
1961 flags |= NEIGH_UPDATE_F_EXT_LEARNED;
1962
Roopa Prabhuf7aa74e2018-09-22 21:26:19 -07001963 if (ndm->ndm_flags & NTF_ROUTER)
1964 flags |= NEIGH_UPDATE_F_ISROUTER;
1965
WANG Congd7480fd2014-11-10 15:59:36 -08001966 if (ndm->ndm_flags & NTF_USE) {
1967 neigh_event_send(neigh, NULL);
1968 err = 0;
1969 } else
David Ahern7a35a502018-12-05 20:02:29 -08001970 err = __neigh_update(neigh, lladdr, ndm->ndm_state, flags,
1971 NETLINK_CB(skb).portid, extack);
David Aherndf9b0e32018-12-15 14:09:06 -08001972
1973 if (protocol)
1974 neigh->protocol = protocol;
1975
WANG Congd7480fd2014-11-10 15:59:36 -08001976 neigh_release(neigh);
1977
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978out:
1979 return err;
1980}
1981
Thomas Grafc7fb64d2005-06-18 22:50:55 -07001982static int neightbl_fill_parms(struct sk_buff *skb, struct neigh_parms *parms)
1983{
Thomas Grafca860fb2006-08-07 18:00:18 -07001984 struct nlattr *nest;
1985
Michal Kubecekae0be8d2019-04-26 11:13:06 +02001986 nest = nla_nest_start_noflag(skb, NDTA_PARMS);
Thomas Grafca860fb2006-08-07 18:00:18 -07001987 if (nest == NULL)
1988 return -ENOBUFS;
Thomas Grafc7fb64d2005-06-18 22:50:55 -07001989
David S. Miller9a6308d2012-04-01 20:06:28 -04001990 if ((parms->dev &&
1991 nla_put_u32(skb, NDTPA_IFINDEX, parms->dev->ifindex)) ||
Reshetova, Elena63439442017-06-30 13:07:56 +03001992 nla_put_u32(skb, NDTPA_REFCNT, refcount_read(&parms->refcnt)) ||
Jiri Pirko1f9248e2013-12-07 19:26:53 +01001993 nla_put_u32(skb, NDTPA_QUEUE_LENBYTES,
1994 NEIGH_VAR(parms, QUEUE_LEN_BYTES)) ||
David S. Miller9a6308d2012-04-01 20:06:28 -04001995 /* approximative value for deprecated QUEUE_LEN (in packets) */
1996 nla_put_u32(skb, NDTPA_QUEUE_LEN,
Jiri Pirko1f9248e2013-12-07 19:26:53 +01001997 NEIGH_VAR(parms, QUEUE_LEN_BYTES) / SKB_TRUESIZE(ETH_FRAME_LEN)) ||
1998 nla_put_u32(skb, NDTPA_PROXY_QLEN, NEIGH_VAR(parms, PROXY_QLEN)) ||
1999 nla_put_u32(skb, NDTPA_APP_PROBES, NEIGH_VAR(parms, APP_PROBES)) ||
2000 nla_put_u32(skb, NDTPA_UCAST_PROBES,
2001 NEIGH_VAR(parms, UCAST_PROBES)) ||
2002 nla_put_u32(skb, NDTPA_MCAST_PROBES,
2003 NEIGH_VAR(parms, MCAST_PROBES)) ||
YOSHIFUJI Hideaki/吉藤英明8da86462015-03-19 22:41:46 +09002004 nla_put_u32(skb, NDTPA_MCAST_REPROBES,
2005 NEIGH_VAR(parms, MCAST_REPROBES)) ||
Nicolas Dichtel2175d872016-04-22 17:31:21 +02002006 nla_put_msecs(skb, NDTPA_REACHABLE_TIME, parms->reachable_time,
2007 NDTPA_PAD) ||
David S. Miller9a6308d2012-04-01 20:06:28 -04002008 nla_put_msecs(skb, NDTPA_BASE_REACHABLE_TIME,
Nicolas Dichtel2175d872016-04-22 17:31:21 +02002009 NEIGH_VAR(parms, BASE_REACHABLE_TIME), NDTPA_PAD) ||
Jiri Pirko1f9248e2013-12-07 19:26:53 +01002010 nla_put_msecs(skb, NDTPA_GC_STALETIME,
Nicolas Dichtel2175d872016-04-22 17:31:21 +02002011 NEIGH_VAR(parms, GC_STALETIME), NDTPA_PAD) ||
David S. Miller9a6308d2012-04-01 20:06:28 -04002012 nla_put_msecs(skb, NDTPA_DELAY_PROBE_TIME,
Nicolas Dichtel2175d872016-04-22 17:31:21 +02002013 NEIGH_VAR(parms, DELAY_PROBE_TIME), NDTPA_PAD) ||
Jiri Pirko1f9248e2013-12-07 19:26:53 +01002014 nla_put_msecs(skb, NDTPA_RETRANS_TIME,
Nicolas Dichtel2175d872016-04-22 17:31:21 +02002015 NEIGH_VAR(parms, RETRANS_TIME), NDTPA_PAD) ||
Jiri Pirko1f9248e2013-12-07 19:26:53 +01002016 nla_put_msecs(skb, NDTPA_ANYCAST_DELAY,
Nicolas Dichtel2175d872016-04-22 17:31:21 +02002017 NEIGH_VAR(parms, ANYCAST_DELAY), NDTPA_PAD) ||
Jiri Pirko1f9248e2013-12-07 19:26:53 +01002018 nla_put_msecs(skb, NDTPA_PROXY_DELAY,
Nicolas Dichtel2175d872016-04-22 17:31:21 +02002019 NEIGH_VAR(parms, PROXY_DELAY), NDTPA_PAD) ||
Jiri Pirko1f9248e2013-12-07 19:26:53 +01002020 nla_put_msecs(skb, NDTPA_LOCKTIME,
Nicolas Dichtel2175d872016-04-22 17:31:21 +02002021 NEIGH_VAR(parms, LOCKTIME), NDTPA_PAD))
David S. Miller9a6308d2012-04-01 20:06:28 -04002022 goto nla_put_failure;
Thomas Grafca860fb2006-08-07 18:00:18 -07002023 return nla_nest_end(skb, nest);
Thomas Grafc7fb64d2005-06-18 22:50:55 -07002024
Thomas Grafca860fb2006-08-07 18:00:18 -07002025nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07002026 nla_nest_cancel(skb, nest);
2027 return -EMSGSIZE;
Thomas Grafc7fb64d2005-06-18 22:50:55 -07002028}
2029
Thomas Grafca860fb2006-08-07 18:00:18 -07002030static int neightbl_fill_info(struct sk_buff *skb, struct neigh_table *tbl,
2031 u32 pid, u32 seq, int type, int flags)
Thomas Grafc7fb64d2005-06-18 22:50:55 -07002032{
2033 struct nlmsghdr *nlh;
2034 struct ndtmsg *ndtmsg;
2035
Thomas Grafca860fb2006-08-07 18:00:18 -07002036 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndtmsg), flags);
2037 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -08002038 return -EMSGSIZE;
Thomas Grafc7fb64d2005-06-18 22:50:55 -07002039
Thomas Grafca860fb2006-08-07 18:00:18 -07002040 ndtmsg = nlmsg_data(nlh);
Thomas Grafc7fb64d2005-06-18 22:50:55 -07002041
2042 read_lock_bh(&tbl->lock);
2043 ndtmsg->ndtm_family = tbl->family;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -07002044 ndtmsg->ndtm_pad1 = 0;
2045 ndtmsg->ndtm_pad2 = 0;
Thomas Grafc7fb64d2005-06-18 22:50:55 -07002046
David S. Miller9a6308d2012-04-01 20:06:28 -04002047 if (nla_put_string(skb, NDTA_NAME, tbl->id) ||
Nicolas Dichtel2175d872016-04-22 17:31:21 +02002048 nla_put_msecs(skb, NDTA_GC_INTERVAL, tbl->gc_interval, NDTA_PAD) ||
David S. Miller9a6308d2012-04-01 20:06:28 -04002049 nla_put_u32(skb, NDTA_THRESH1, tbl->gc_thresh1) ||
2050 nla_put_u32(skb, NDTA_THRESH2, tbl->gc_thresh2) ||
2051 nla_put_u32(skb, NDTA_THRESH3, tbl->gc_thresh3))
2052 goto nla_put_failure;
Thomas Grafc7fb64d2005-06-18 22:50:55 -07002053 {
2054 unsigned long now = jiffies;
Eric Dumazet9d027e32019-11-05 14:11:49 -08002055 long flush_delta = now - tbl->last_flush;
2056 long rand_delta = now - tbl->last_rand;
Eric Dumazetd6bf7812010-10-04 06:15:44 +00002057 struct neigh_hash_table *nht;
Thomas Grafc7fb64d2005-06-18 22:50:55 -07002058 struct ndt_config ndc = {
2059 .ndtc_key_len = tbl->key_len,
2060 .ndtc_entry_size = tbl->entry_size,
2061 .ndtc_entries = atomic_read(&tbl->entries),
2062 .ndtc_last_flush = jiffies_to_msecs(flush_delta),
2063 .ndtc_last_rand = jiffies_to_msecs(rand_delta),
Thomas Grafc7fb64d2005-06-18 22:50:55 -07002064 .ndtc_proxy_qlen = tbl->proxy_queue.qlen,
2065 };
2066
Eric Dumazetd6bf7812010-10-04 06:15:44 +00002067 rcu_read_lock_bh();
2068 nht = rcu_dereference_bh(tbl->nht);
David S. Miller2c2aba62011-12-28 15:06:58 -05002069 ndc.ndtc_hash_rnd = nht->hash_rnd[0];
David S. Millercd089332011-07-11 01:28:12 -07002070 ndc.ndtc_hash_mask = ((1 << nht->hash_shift) - 1);
Eric Dumazetd6bf7812010-10-04 06:15:44 +00002071 rcu_read_unlock_bh();
2072
David S. Miller9a6308d2012-04-01 20:06:28 -04002073 if (nla_put(skb, NDTA_CONFIG, sizeof(ndc), &ndc))
2074 goto nla_put_failure;
Thomas Grafc7fb64d2005-06-18 22:50:55 -07002075 }
2076
2077 {
2078 int cpu;
2079 struct ndt_stats ndst;
2080
2081 memset(&ndst, 0, sizeof(ndst));
2082
KAMEZAWA Hiroyuki6f912042006-04-10 22:52:50 -07002083 for_each_possible_cpu(cpu) {
Thomas Grafc7fb64d2005-06-18 22:50:55 -07002084 struct neigh_statistics *st;
2085
Thomas Grafc7fb64d2005-06-18 22:50:55 -07002086 st = per_cpu_ptr(tbl->stats, cpu);
2087 ndst.ndts_allocs += st->allocs;
2088 ndst.ndts_destroys += st->destroys;
2089 ndst.ndts_hash_grows += st->hash_grows;
2090 ndst.ndts_res_failed += st->res_failed;
2091 ndst.ndts_lookups += st->lookups;
2092 ndst.ndts_hits += st->hits;
2093 ndst.ndts_rcv_probes_mcast += st->rcv_probes_mcast;
2094 ndst.ndts_rcv_probes_ucast += st->rcv_probes_ucast;
2095 ndst.ndts_periodic_gc_runs += st->periodic_gc_runs;
2096 ndst.ndts_forced_gc_runs += st->forced_gc_runs;
Rick Jonesfb811392015-08-07 11:10:37 -07002097 ndst.ndts_table_fulls += st->table_fulls;
Thomas Grafc7fb64d2005-06-18 22:50:55 -07002098 }
2099
Nicolas Dichtelb6763382016-04-26 10:06:17 +02002100 if (nla_put_64bit(skb, NDTA_STATS, sizeof(ndst), &ndst,
2101 NDTA_PAD))
David S. Miller9a6308d2012-04-01 20:06:28 -04002102 goto nla_put_failure;
Thomas Grafc7fb64d2005-06-18 22:50:55 -07002103 }
2104
2105 BUG_ON(tbl->parms.dev);
2106 if (neightbl_fill_parms(skb, &tbl->parms) < 0)
Thomas Grafca860fb2006-08-07 18:00:18 -07002107 goto nla_put_failure;
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 Grafc7fb64d2005-06-18 22:50:55 -07002112
Thomas Grafca860fb2006-08-07 18:00:18 -07002113nla_put_failure:
Thomas Grafc7fb64d2005-06-18 22:50:55 -07002114 read_unlock_bh(&tbl->lock);
Patrick McHardy26932562007-01-31 23:16:40 -08002115 nlmsg_cancel(skb, nlh);
2116 return -EMSGSIZE;
Thomas Grafc7fb64d2005-06-18 22:50:55 -07002117}
2118
Thomas Grafca860fb2006-08-07 18:00:18 -07002119static int neightbl_fill_param_info(struct sk_buff *skb,
2120 struct neigh_table *tbl,
Thomas Grafc7fb64d2005-06-18 22:50:55 -07002121 struct neigh_parms *parms,
Thomas Grafca860fb2006-08-07 18:00:18 -07002122 u32 pid, u32 seq, int type,
2123 unsigned int flags)
Thomas Grafc7fb64d2005-06-18 22:50:55 -07002124{
2125 struct ndtmsg *ndtmsg;
2126 struct nlmsghdr *nlh;
2127
Thomas Grafca860fb2006-08-07 18:00:18 -07002128 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndtmsg), flags);
2129 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -08002130 return -EMSGSIZE;
Thomas Grafc7fb64d2005-06-18 22:50:55 -07002131
Thomas Grafca860fb2006-08-07 18:00:18 -07002132 ndtmsg = nlmsg_data(nlh);
Thomas Grafc7fb64d2005-06-18 22:50:55 -07002133
2134 read_lock_bh(&tbl->lock);
2135 ndtmsg->ndtm_family = tbl->family;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -07002136 ndtmsg->ndtm_pad1 = 0;
2137 ndtmsg->ndtm_pad2 = 0;
Thomas Grafc7fb64d2005-06-18 22:50:55 -07002138
Thomas Grafca860fb2006-08-07 18:00:18 -07002139 if (nla_put_string(skb, NDTA_NAME, tbl->id) < 0 ||
2140 neightbl_fill_parms(skb, parms) < 0)
2141 goto errout;
Thomas Grafc7fb64d2005-06-18 22:50:55 -07002142
2143 read_unlock_bh(&tbl->lock);
Johannes Berg053c0952015-01-16 22:09:00 +01002144 nlmsg_end(skb, nlh);
2145 return 0;
Thomas Grafca860fb2006-08-07 18:00:18 -07002146errout:
Thomas Grafc7fb64d2005-06-18 22:50:55 -07002147 read_unlock_bh(&tbl->lock);
Patrick McHardy26932562007-01-31 23:16:40 -08002148 nlmsg_cancel(skb, nlh);
2149 return -EMSGSIZE;
Thomas Grafc7fb64d2005-06-18 22:50:55 -07002150}
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09002151
Patrick McHardyef7c79e2007-06-05 12:38:30 -07002152static const struct nla_policy nl_neightbl_policy[NDTA_MAX+1] = {
Thomas Graf6b3f8672006-08-07 17:58:53 -07002153 [NDTA_NAME] = { .type = NLA_STRING },
2154 [NDTA_THRESH1] = { .type = NLA_U32 },
2155 [NDTA_THRESH2] = { .type = NLA_U32 },
2156 [NDTA_THRESH3] = { .type = NLA_U32 },
2157 [NDTA_GC_INTERVAL] = { .type = NLA_U64 },
2158 [NDTA_PARMS] = { .type = NLA_NESTED },
2159};
2160
Patrick McHardyef7c79e2007-06-05 12:38:30 -07002161static const struct nla_policy nl_ntbl_parm_policy[NDTPA_MAX+1] = {
Thomas Graf6b3f8672006-08-07 17:58:53 -07002162 [NDTPA_IFINDEX] = { .type = NLA_U32 },
2163 [NDTPA_QUEUE_LEN] = { .type = NLA_U32 },
2164 [NDTPA_PROXY_QLEN] = { .type = NLA_U32 },
2165 [NDTPA_APP_PROBES] = { .type = NLA_U32 },
2166 [NDTPA_UCAST_PROBES] = { .type = NLA_U32 },
2167 [NDTPA_MCAST_PROBES] = { .type = NLA_U32 },
YOSHIFUJI Hideaki/吉藤英明8da86462015-03-19 22:41:46 +09002168 [NDTPA_MCAST_REPROBES] = { .type = NLA_U32 },
Thomas Graf6b3f8672006-08-07 17:58:53 -07002169 [NDTPA_BASE_REACHABLE_TIME] = { .type = NLA_U64 },
2170 [NDTPA_GC_STALETIME] = { .type = NLA_U64 },
2171 [NDTPA_DELAY_PROBE_TIME] = { .type = NLA_U64 },
2172 [NDTPA_RETRANS_TIME] = { .type = NLA_U64 },
2173 [NDTPA_ANYCAST_DELAY] = { .type = NLA_U64 },
2174 [NDTPA_PROXY_DELAY] = { .type = NLA_U64 },
2175 [NDTPA_LOCKTIME] = { .type = NLA_U64 },
2176};
2177
David Ahernc21ef3e2017-04-16 09:48:24 -07002178static int neightbl_set(struct sk_buff *skb, struct nlmsghdr *nlh,
2179 struct netlink_ext_ack *extack)
Thomas Grafc7fb64d2005-06-18 22:50:55 -07002180{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002181 struct net *net = sock_net(skb->sk);
Thomas Grafc7fb64d2005-06-18 22:50:55 -07002182 struct neigh_table *tbl;
Thomas Graf6b3f8672006-08-07 17:58:53 -07002183 struct ndtmsg *ndtmsg;
2184 struct nlattr *tb[NDTA_MAX+1];
WANG Congd7480fd2014-11-10 15:59:36 -08002185 bool found = false;
2186 int err, tidx;
Thomas Grafc7fb64d2005-06-18 22:50:55 -07002187
Johannes Berg8cb08172019-04-26 14:07:28 +02002188 err = nlmsg_parse_deprecated(nlh, sizeof(*ndtmsg), tb, NDTA_MAX,
2189 nl_neightbl_policy, extack);
Thomas Graf6b3f8672006-08-07 17:58:53 -07002190 if (err < 0)
2191 goto errout;
Thomas Grafc7fb64d2005-06-18 22:50:55 -07002192
Thomas Graf6b3f8672006-08-07 17:58:53 -07002193 if (tb[NDTA_NAME] == NULL) {
2194 err = -EINVAL;
2195 goto errout;
2196 }
2197
2198 ndtmsg = nlmsg_data(nlh);
WANG Congd7480fd2014-11-10 15:59:36 -08002199
2200 for (tidx = 0; tidx < NEIGH_NR_TABLES; tidx++) {
2201 tbl = neigh_tables[tidx];
2202 if (!tbl)
2203 continue;
Thomas Grafc7fb64d2005-06-18 22:50:55 -07002204 if (ndtmsg->ndtm_family && tbl->family != ndtmsg->ndtm_family)
2205 continue;
WANG Congd7480fd2014-11-10 15:59:36 -08002206 if (nla_strcmp(tb[NDTA_NAME], tbl->id) == 0) {
2207 found = true;
Thomas Grafc7fb64d2005-06-18 22:50:55 -07002208 break;
WANG Congd7480fd2014-11-10 15:59:36 -08002209 }
Thomas Grafc7fb64d2005-06-18 22:50:55 -07002210 }
2211
WANG Congd7480fd2014-11-10 15:59:36 -08002212 if (!found)
2213 return -ENOENT;
Thomas Grafc7fb64d2005-06-18 22:50:55 -07002214
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09002215 /*
Thomas Grafc7fb64d2005-06-18 22:50:55 -07002216 * We acquire tbl->lock to be nice to the periodic timers and
2217 * make sure they always see a consistent set of values.
2218 */
2219 write_lock_bh(&tbl->lock);
2220
Thomas Graf6b3f8672006-08-07 17:58:53 -07002221 if (tb[NDTA_PARMS]) {
2222 struct nlattr *tbp[NDTPA_MAX+1];
Thomas Grafc7fb64d2005-06-18 22:50:55 -07002223 struct neigh_parms *p;
Thomas Graf6b3f8672006-08-07 17:58:53 -07002224 int i, ifindex = 0;
Thomas Grafc7fb64d2005-06-18 22:50:55 -07002225
Johannes Berg8cb08172019-04-26 14:07:28 +02002226 err = nla_parse_nested_deprecated(tbp, NDTPA_MAX,
2227 tb[NDTA_PARMS],
2228 nl_ntbl_parm_policy, extack);
Thomas Graf6b3f8672006-08-07 17:58:53 -07002229 if (err < 0)
2230 goto errout_tbl_lock;
Thomas Grafc7fb64d2005-06-18 22:50:55 -07002231
Thomas Graf6b3f8672006-08-07 17:58:53 -07002232 if (tbp[NDTPA_IFINDEX])
2233 ifindex = nla_get_u32(tbp[NDTPA_IFINDEX]);
Thomas Grafc7fb64d2005-06-18 22:50:55 -07002234
Tobias Klauser97fd5bc2009-07-13 11:17:49 -07002235 p = lookup_neigh_parms(tbl, net, ifindex);
Thomas Grafc7fb64d2005-06-18 22:50:55 -07002236 if (p == NULL) {
2237 err = -ENOENT;
Thomas Graf6b3f8672006-08-07 17:58:53 -07002238 goto errout_tbl_lock;
Thomas Grafc7fb64d2005-06-18 22:50:55 -07002239 }
Thomas Grafc7fb64d2005-06-18 22:50:55 -07002240
Thomas Graf6b3f8672006-08-07 17:58:53 -07002241 for (i = 1; i <= NDTPA_MAX; i++) {
2242 if (tbp[i] == NULL)
2243 continue;
Thomas Grafc7fb64d2005-06-18 22:50:55 -07002244
Thomas Graf6b3f8672006-08-07 17:58:53 -07002245 switch (i) {
2246 case NDTPA_QUEUE_LEN:
Jiri Pirko1f9248e2013-12-07 19:26:53 +01002247 NEIGH_VAR_SET(p, QUEUE_LEN_BYTES,
2248 nla_get_u32(tbp[i]) *
2249 SKB_TRUESIZE(ETH_FRAME_LEN));
Eric Dumazet8b5c1712011-11-09 12:07:14 +00002250 break;
2251 case NDTPA_QUEUE_LENBYTES:
Jiri Pirko1f9248e2013-12-07 19:26:53 +01002252 NEIGH_VAR_SET(p, QUEUE_LEN_BYTES,
2253 nla_get_u32(tbp[i]));
Thomas Graf6b3f8672006-08-07 17:58:53 -07002254 break;
2255 case NDTPA_PROXY_QLEN:
Jiri Pirko1f9248e2013-12-07 19:26:53 +01002256 NEIGH_VAR_SET(p, PROXY_QLEN,
2257 nla_get_u32(tbp[i]));
Thomas Graf6b3f8672006-08-07 17:58:53 -07002258 break;
2259 case NDTPA_APP_PROBES:
Jiri Pirko1f9248e2013-12-07 19:26:53 +01002260 NEIGH_VAR_SET(p, APP_PROBES,
2261 nla_get_u32(tbp[i]));
Thomas Graf6b3f8672006-08-07 17:58:53 -07002262 break;
2263 case NDTPA_UCAST_PROBES:
Jiri Pirko1f9248e2013-12-07 19:26:53 +01002264 NEIGH_VAR_SET(p, UCAST_PROBES,
2265 nla_get_u32(tbp[i]));
Thomas Graf6b3f8672006-08-07 17:58:53 -07002266 break;
2267 case NDTPA_MCAST_PROBES:
Jiri Pirko1f9248e2013-12-07 19:26:53 +01002268 NEIGH_VAR_SET(p, MCAST_PROBES,
2269 nla_get_u32(tbp[i]));
Thomas Graf6b3f8672006-08-07 17:58:53 -07002270 break;
YOSHIFUJI Hideaki/吉藤英明8da86462015-03-19 22:41:46 +09002271 case NDTPA_MCAST_REPROBES:
2272 NEIGH_VAR_SET(p, MCAST_REPROBES,
2273 nla_get_u32(tbp[i]));
2274 break;
Thomas Graf6b3f8672006-08-07 17:58:53 -07002275 case NDTPA_BASE_REACHABLE_TIME:
Jiri Pirko1f9248e2013-12-07 19:26:53 +01002276 NEIGH_VAR_SET(p, BASE_REACHABLE_TIME,
2277 nla_get_msecs(tbp[i]));
Jean-Francois Remy4bf69802015-01-14 04:22:39 +01002278 /* update reachable_time as well, otherwise, the change will
2279 * only be effective after the next time neigh_periodic_work
2280 * decides to recompute it (can be multiple minutes)
2281 */
2282 p->reachable_time =
2283 neigh_rand_reach_time(NEIGH_VAR(p, BASE_REACHABLE_TIME));
Thomas Graf6b3f8672006-08-07 17:58:53 -07002284 break;
2285 case NDTPA_GC_STALETIME:
Jiri Pirko1f9248e2013-12-07 19:26:53 +01002286 NEIGH_VAR_SET(p, GC_STALETIME,
2287 nla_get_msecs(tbp[i]));
Thomas Graf6b3f8672006-08-07 17:58:53 -07002288 break;
2289 case NDTPA_DELAY_PROBE_TIME:
Jiri Pirko1f9248e2013-12-07 19:26:53 +01002290 NEIGH_VAR_SET(p, DELAY_PROBE_TIME,
2291 nla_get_msecs(tbp[i]));
Ido Schimmel2a4501a2016-07-05 11:27:42 +02002292 call_netevent_notifiers(NETEVENT_DELAY_PROBE_TIME_UPDATE, p);
Thomas Graf6b3f8672006-08-07 17:58:53 -07002293 break;
2294 case NDTPA_RETRANS_TIME:
Jiri Pirko1f9248e2013-12-07 19:26:53 +01002295 NEIGH_VAR_SET(p, RETRANS_TIME,
2296 nla_get_msecs(tbp[i]));
Thomas Graf6b3f8672006-08-07 17:58:53 -07002297 break;
2298 case NDTPA_ANYCAST_DELAY:
Jiri Pirko39774582014-01-14 15:46:07 +01002299 NEIGH_VAR_SET(p, ANYCAST_DELAY,
2300 nla_get_msecs(tbp[i]));
Thomas Graf6b3f8672006-08-07 17:58:53 -07002301 break;
2302 case NDTPA_PROXY_DELAY:
Jiri Pirko39774582014-01-14 15:46:07 +01002303 NEIGH_VAR_SET(p, PROXY_DELAY,
2304 nla_get_msecs(tbp[i]));
Thomas Graf6b3f8672006-08-07 17:58:53 -07002305 break;
2306 case NDTPA_LOCKTIME:
Jiri Pirko39774582014-01-14 15:46:07 +01002307 NEIGH_VAR_SET(p, LOCKTIME,
2308 nla_get_msecs(tbp[i]));
Thomas Graf6b3f8672006-08-07 17:58:53 -07002309 break;
2310 }
2311 }
Thomas Grafc7fb64d2005-06-18 22:50:55 -07002312 }
2313
Gao fengdc25c672013-06-20 10:01:34 +08002314 err = -ENOENT;
2315 if ((tb[NDTA_THRESH1] || tb[NDTA_THRESH2] ||
2316 tb[NDTA_THRESH3] || tb[NDTA_GC_INTERVAL]) &&
2317 !net_eq(net, &init_net))
2318 goto errout_tbl_lock;
2319
Thomas Graf6b3f8672006-08-07 17:58:53 -07002320 if (tb[NDTA_THRESH1])
2321 tbl->gc_thresh1 = nla_get_u32(tb[NDTA_THRESH1]);
2322
2323 if (tb[NDTA_THRESH2])
2324 tbl->gc_thresh2 = nla_get_u32(tb[NDTA_THRESH2]);
2325
2326 if (tb[NDTA_THRESH3])
2327 tbl->gc_thresh3 = nla_get_u32(tb[NDTA_THRESH3]);
2328
2329 if (tb[NDTA_GC_INTERVAL])
2330 tbl->gc_interval = nla_get_msecs(tb[NDTA_GC_INTERVAL]);
2331
Thomas Grafc7fb64d2005-06-18 22:50:55 -07002332 err = 0;
2333
Thomas Graf6b3f8672006-08-07 17:58:53 -07002334errout_tbl_lock:
Thomas Grafc7fb64d2005-06-18 22:50:55 -07002335 write_unlock_bh(&tbl->lock);
Thomas Graf6b3f8672006-08-07 17:58:53 -07002336errout:
Thomas Grafc7fb64d2005-06-18 22:50:55 -07002337 return err;
2338}
2339
David Ahern9632d472018-10-07 20:16:37 -07002340static int neightbl_valid_dump_info(const struct nlmsghdr *nlh,
2341 struct netlink_ext_ack *extack)
2342{
2343 struct ndtmsg *ndtm;
2344
2345 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ndtm))) {
2346 NL_SET_ERR_MSG(extack, "Invalid header for neighbor table dump request");
2347 return -EINVAL;
2348 }
2349
2350 ndtm = nlmsg_data(nlh);
2351 if (ndtm->ndtm_pad1 || ndtm->ndtm_pad2) {
2352 NL_SET_ERR_MSG(extack, "Invalid values in header for neighbor table dump request");
2353 return -EINVAL;
2354 }
2355
2356 if (nlmsg_attrlen(nlh, sizeof(*ndtm))) {
2357 NL_SET_ERR_MSG(extack, "Invalid data after header in neighbor table dump request");
2358 return -EINVAL;
2359 }
2360
2361 return 0;
2362}
2363
Thomas Grafc8822a42007-03-22 11:50:06 -07002364static int neightbl_dump_info(struct sk_buff *skb, struct netlink_callback *cb)
Thomas Grafc7fb64d2005-06-18 22:50:55 -07002365{
David Ahern9632d472018-10-07 20:16:37 -07002366 const struct nlmsghdr *nlh = cb->nlh;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002367 struct net *net = sock_net(skb->sk);
Thomas Grafca860fb2006-08-07 18:00:18 -07002368 int family, tidx, nidx = 0;
2369 int tbl_skip = cb->args[0];
2370 int neigh_skip = cb->args[1];
Thomas Grafc7fb64d2005-06-18 22:50:55 -07002371 struct neigh_table *tbl;
2372
David Ahern9632d472018-10-07 20:16:37 -07002373 if (cb->strict_check) {
2374 int err = neightbl_valid_dump_info(nlh, cb->extack);
2375
2376 if (err < 0)
2377 return err;
2378 }
2379
2380 family = ((struct rtgenmsg *)nlmsg_data(nlh))->rtgen_family;
Thomas Grafc7fb64d2005-06-18 22:50:55 -07002381
WANG Congd7480fd2014-11-10 15:59:36 -08002382 for (tidx = 0; tidx < NEIGH_NR_TABLES; tidx++) {
Thomas Grafc7fb64d2005-06-18 22:50:55 -07002383 struct neigh_parms *p;
2384
WANG Congd7480fd2014-11-10 15:59:36 -08002385 tbl = neigh_tables[tidx];
2386 if (!tbl)
2387 continue;
2388
Thomas Grafca860fb2006-08-07 18:00:18 -07002389 if (tidx < tbl_skip || (family && tbl->family != family))
Thomas Grafc7fb64d2005-06-18 22:50:55 -07002390 continue;
2391
Eric W. Biederman15e47302012-09-07 20:12:54 +00002392 if (neightbl_fill_info(skb, tbl, NETLINK_CB(cb->skb).portid,
David Ahern9632d472018-10-07 20:16:37 -07002393 nlh->nlmsg_seq, RTM_NEWNEIGHTBL,
David S. Miller7b46a642015-01-18 23:36:08 -05002394 NLM_F_MULTI) < 0)
Thomas Grafc7fb64d2005-06-18 22:50:55 -07002395 break;
2396
Nicolas Dichtel75fbfd32014-10-29 19:29:31 +01002397 nidx = 0;
2398 p = list_next_entry(&tbl->parms, list);
2399 list_for_each_entry_from(p, &tbl->parms_list, list) {
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09002400 if (!net_eq(neigh_parms_net(p), net))
Eric W. Biederman426b5302008-01-24 00:13:18 -08002401 continue;
2402
Gautam Kachrooefc683f2009-02-06 00:52:04 -08002403 if (nidx < neigh_skip)
2404 goto next;
Thomas Grafc7fb64d2005-06-18 22:50:55 -07002405
Thomas Grafca860fb2006-08-07 18:00:18 -07002406 if (neightbl_fill_param_info(skb, tbl, p,
Eric W. Biederman15e47302012-09-07 20:12:54 +00002407 NETLINK_CB(cb->skb).portid,
David Ahern9632d472018-10-07 20:16:37 -07002408 nlh->nlmsg_seq,
Thomas Grafca860fb2006-08-07 18:00:18 -07002409 RTM_NEWNEIGHTBL,
David S. Miller7b46a642015-01-18 23:36:08 -05002410 NLM_F_MULTI) < 0)
Thomas Grafc7fb64d2005-06-18 22:50:55 -07002411 goto out;
Gautam Kachrooefc683f2009-02-06 00:52:04 -08002412 next:
2413 nidx++;
Thomas Grafc7fb64d2005-06-18 22:50:55 -07002414 }
2415
Thomas Grafca860fb2006-08-07 18:00:18 -07002416 neigh_skip = 0;
Thomas Grafc7fb64d2005-06-18 22:50:55 -07002417 }
2418out:
Thomas Grafca860fb2006-08-07 18:00:18 -07002419 cb->args[0] = tidx;
2420 cb->args[1] = nidx;
Thomas Grafc7fb64d2005-06-18 22:50:55 -07002421
2422 return skb->len;
2423}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424
Thomas Graf8b8aec52006-08-07 17:56:37 -07002425static int neigh_fill_info(struct sk_buff *skb, struct neighbour *neigh,
2426 u32 pid, u32 seq, int type, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427{
2428 unsigned long now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429 struct nda_cacheinfo ci;
Thomas Graf8b8aec52006-08-07 17:56:37 -07002430 struct nlmsghdr *nlh;
2431 struct ndmsg *ndm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432
Thomas Graf8b8aec52006-08-07 17:56:37 -07002433 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), flags);
2434 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -08002435 return -EMSGSIZE;
Thomas Graf8b8aec52006-08-07 17:56:37 -07002436
2437 ndm = nlmsg_data(nlh);
2438 ndm->ndm_family = neigh->ops->family;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -07002439 ndm->ndm_pad1 = 0;
2440 ndm->ndm_pad2 = 0;
Thomas Graf8b8aec52006-08-07 17:56:37 -07002441 ndm->ndm_flags = neigh->flags;
2442 ndm->ndm_type = neigh->type;
2443 ndm->ndm_ifindex = neigh->dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444
David S. Miller9a6308d2012-04-01 20:06:28 -04002445 if (nla_put(skb, NDA_DST, neigh->tbl->key_len, neigh->primary_key))
2446 goto nla_put_failure;
Thomas Graf8b8aec52006-08-07 17:56:37 -07002447
2448 read_lock_bh(&neigh->lock);
2449 ndm->ndm_state = neigh->nud_state;
Eric Dumazet0ed8ddf2010-10-07 10:44:07 +00002450 if (neigh->nud_state & NUD_VALID) {
2451 char haddr[MAX_ADDR_LEN];
2452
2453 neigh_ha_snapshot(haddr, neigh, neigh->dev);
2454 if (nla_put(skb, NDA_LLADDR, neigh->dev->addr_len, haddr) < 0) {
2455 read_unlock_bh(&neigh->lock);
2456 goto nla_put_failure;
2457 }
Thomas Graf8b8aec52006-08-07 17:56:37 -07002458 }
2459
Stephen Hemmingerb9f5f522008-06-03 16:03:15 -07002460 ci.ndm_used = jiffies_to_clock_t(now - neigh->used);
2461 ci.ndm_confirmed = jiffies_to_clock_t(now - neigh->confirmed);
2462 ci.ndm_updated = jiffies_to_clock_t(now - neigh->updated);
Reshetova, Elena9f237432017-06-30 13:07:55 +03002463 ci.ndm_refcnt = refcount_read(&neigh->refcnt) - 1;
Thomas Graf8b8aec52006-08-07 17:56:37 -07002464 read_unlock_bh(&neigh->lock);
2465
David S. Miller9a6308d2012-04-01 20:06:28 -04002466 if (nla_put_u32(skb, NDA_PROBES, atomic_read(&neigh->probes)) ||
2467 nla_put(skb, NDA_CACHEINFO, sizeof(ci), &ci))
2468 goto nla_put_failure;
Thomas Graf8b8aec52006-08-07 17:56:37 -07002469
David Aherndf9b0e32018-12-15 14:09:06 -08002470 if (neigh->protocol && nla_put_u8(skb, NDA_PROTOCOL, neigh->protocol))
2471 goto nla_put_failure;
2472
Johannes Berg053c0952015-01-16 22:09:00 +01002473 nlmsg_end(skb, nlh);
2474 return 0;
Thomas Graf8b8aec52006-08-07 17:56:37 -07002475
2476nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -08002477 nlmsg_cancel(skb, nlh);
2478 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479}
2480
Tony Zelenoff84920c12012-01-26 22:28:58 +00002481static int pneigh_fill_info(struct sk_buff *skb, struct pneigh_entry *pn,
2482 u32 pid, u32 seq, int type, unsigned int flags,
2483 struct neigh_table *tbl)
2484{
2485 struct nlmsghdr *nlh;
2486 struct ndmsg *ndm;
2487
2488 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), flags);
2489 if (nlh == NULL)
2490 return -EMSGSIZE;
2491
2492 ndm = nlmsg_data(nlh);
2493 ndm->ndm_family = tbl->family;
2494 ndm->ndm_pad1 = 0;
2495 ndm->ndm_pad2 = 0;
2496 ndm->ndm_flags = pn->flags | NTF_PROXY;
Jun Zhao545469f2014-07-26 00:38:59 +08002497 ndm->ndm_type = RTN_UNICAST;
Konstantin Khlebnikov6adc5fd2015-12-01 01:14:48 +03002498 ndm->ndm_ifindex = pn->dev ? pn->dev->ifindex : 0;
Tony Zelenoff84920c12012-01-26 22:28:58 +00002499 ndm->ndm_state = NUD_NONE;
2500
David S. Miller9a6308d2012-04-01 20:06:28 -04002501 if (nla_put(skb, NDA_DST, tbl->key_len, pn->key))
2502 goto nla_put_failure;
Tony Zelenoff84920c12012-01-26 22:28:58 +00002503
David Aherndf9b0e32018-12-15 14:09:06 -08002504 if (pn->protocol && nla_put_u8(skb, NDA_PROTOCOL, pn->protocol))
2505 goto nla_put_failure;
2506
Johannes Berg053c0952015-01-16 22:09:00 +01002507 nlmsg_end(skb, nlh);
2508 return 0;
Tony Zelenoff84920c12012-01-26 22:28:58 +00002509
2510nla_put_failure:
2511 nlmsg_cancel(skb, nlh);
2512 return -EMSGSIZE;
2513}
2514
Roopa Prabhu7b8f7a42017-03-19 22:01:28 -07002515static void neigh_update_notify(struct neighbour *neigh, u32 nlmsg_pid)
Thomas Grafd961db32007-08-08 23:12:56 -07002516{
2517 call_netevent_notifiers(NETEVENT_NEIGH_UPDATE, neigh);
Roopa Prabhu7b8f7a42017-03-19 22:01:28 -07002518 __neigh_notify(neigh, RTM_NEWNEIGH, 0, nlmsg_pid);
Thomas Grafd961db32007-08-08 23:12:56 -07002519}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520
David Ahern21fdd092015-09-29 09:32:03 -07002521static bool neigh_master_filtered(struct net_device *dev, int master_idx)
2522{
2523 struct net_device *master;
2524
2525 if (!master_idx)
2526 return false;
2527
Eric Dumazetaab456d2018-10-26 09:33:27 -07002528 master = dev ? netdev_master_upper_dev_get(dev) : NULL;
David Ahern21fdd092015-09-29 09:32:03 -07002529 if (!master || master->ifindex != master_idx)
2530 return true;
2531
2532 return false;
2533}
2534
David Ahern16660f02015-10-03 11:43:46 -07002535static bool neigh_ifindex_filtered(struct net_device *dev, int filter_idx)
2536{
Eric Dumazetaab456d2018-10-26 09:33:27 -07002537 if (filter_idx && (!dev || dev->ifindex != filter_idx))
David Ahern16660f02015-10-03 11:43:46 -07002538 return true;
2539
2540 return false;
2541}
2542
David Ahern6f52f802018-10-03 15:33:12 -07002543struct neigh_dump_filter {
2544 int master_idx;
2545 int dev_idx;
2546};
2547
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548static int neigh_dump_table(struct neigh_table *tbl, struct sk_buff *skb,
David Ahern6f52f802018-10-03 15:33:12 -07002549 struct netlink_callback *cb,
2550 struct neigh_dump_filter *filter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551{
Eric Dumazet767e97e2010-10-06 17:49:21 -07002552 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553 struct neighbour *n;
2554 int rc, h, s_h = cb->args[1];
2555 int idx, s_idx = idx = cb->args[2];
Eric Dumazetd6bf7812010-10-04 06:15:44 +00002556 struct neigh_hash_table *nht;
David Ahern21fdd092015-09-29 09:32:03 -07002557 unsigned int flags = NLM_F_MULTI;
David Ahern21fdd092015-09-29 09:32:03 -07002558
David Ahern6f52f802018-10-03 15:33:12 -07002559 if (filter->dev_idx || filter->master_idx)
2560 flags |= NLM_F_DUMP_FILTERED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561
Eric Dumazetd6bf7812010-10-04 06:15:44 +00002562 rcu_read_lock_bh();
2563 nht = rcu_dereference_bh(tbl->nht);
2564
Eric Dumazet4bd6683b2012-06-07 04:58:35 +00002565 for (h = s_h; h < (1 << nht->hash_shift); h++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566 if (h > s_h)
2567 s_idx = 0;
Eric Dumazet767e97e2010-10-06 17:49:21 -07002568 for (n = rcu_dereference_bh(nht->hash_buckets[h]), idx = 0;
2569 n != NULL;
2570 n = rcu_dereference_bh(n->next)) {
Zhang Shengju18502ac2016-11-30 11:24:42 +08002571 if (idx < s_idx || !net_eq(dev_net(n->dev), net))
2572 goto next;
David Ahern6f52f802018-10-03 15:33:12 -07002573 if (neigh_ifindex_filtered(n->dev, filter->dev_idx) ||
2574 neigh_master_filtered(n->dev, filter->master_idx))
Gautam Kachrooefc683f2009-02-06 00:52:04 -08002575 goto next;
Eric W. Biederman15e47302012-09-07 20:12:54 +00002576 if (neigh_fill_info(skb, n, NETLINK_CB(cb->skb).portid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577 cb->nlh->nlmsg_seq,
Jamal Hadi Salimb6544c02005-06-18 22:54:12 -07002578 RTM_NEWNEIGH,
David Ahern21fdd092015-09-29 09:32:03 -07002579 flags) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580 rc = -1;
2581 goto out;
2582 }
Eric Dumazet767e97e2010-10-06 17:49:21 -07002583next:
Gautam Kachrooefc683f2009-02-06 00:52:04 -08002584 idx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586 }
2587 rc = skb->len;
2588out:
Eric Dumazetd6bf7812010-10-04 06:15:44 +00002589 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590 cb->args[1] = h;
2591 cb->args[2] = idx;
2592 return rc;
2593}
2594
Tony Zelenoff84920c12012-01-26 22:28:58 +00002595static int pneigh_dump_table(struct neigh_table *tbl, struct sk_buff *skb,
David Ahern6f52f802018-10-03 15:33:12 -07002596 struct netlink_callback *cb,
2597 struct neigh_dump_filter *filter)
Tony Zelenoff84920c12012-01-26 22:28:58 +00002598{
2599 struct pneigh_entry *n;
2600 struct net *net = sock_net(skb->sk);
2601 int rc, h, s_h = cb->args[3];
2602 int idx, s_idx = idx = cb->args[4];
David Ahern6f52f802018-10-03 15:33:12 -07002603 unsigned int flags = NLM_F_MULTI;
2604
2605 if (filter->dev_idx || filter->master_idx)
2606 flags |= NLM_F_DUMP_FILTERED;
Tony Zelenoff84920c12012-01-26 22:28:58 +00002607
2608 read_lock_bh(&tbl->lock);
2609
Eric Dumazet4bd6683b2012-06-07 04:58:35 +00002610 for (h = s_h; h <= PNEIGH_HASHMASK; h++) {
Tony Zelenoff84920c12012-01-26 22:28:58 +00002611 if (h > s_h)
2612 s_idx = 0;
2613 for (n = tbl->phash_buckets[h], idx = 0; n; n = n->next) {
Zhang Shengju18502ac2016-11-30 11:24:42 +08002614 if (idx < s_idx || pneigh_net(n) != net)
Tony Zelenoff84920c12012-01-26 22:28:58 +00002615 goto next;
David Ahern6f52f802018-10-03 15:33:12 -07002616 if (neigh_ifindex_filtered(n->dev, filter->dev_idx) ||
2617 neigh_master_filtered(n->dev, filter->master_idx))
2618 goto next;
Eric W. Biederman15e47302012-09-07 20:12:54 +00002619 if (pneigh_fill_info(skb, n, NETLINK_CB(cb->skb).portid,
Tony Zelenoff84920c12012-01-26 22:28:58 +00002620 cb->nlh->nlmsg_seq,
David Ahern6f52f802018-10-03 15:33:12 -07002621 RTM_NEWNEIGH, flags, tbl) < 0) {
Tony Zelenoff84920c12012-01-26 22:28:58 +00002622 read_unlock_bh(&tbl->lock);
2623 rc = -1;
2624 goto out;
2625 }
2626 next:
2627 idx++;
2628 }
2629 }
2630
2631 read_unlock_bh(&tbl->lock);
2632 rc = skb->len;
2633out:
2634 cb->args[3] = h;
2635 cb->args[4] = idx;
2636 return rc;
2637
2638}
2639
David Ahern51183d22018-10-07 20:16:36 -07002640static int neigh_valid_dump_req(const struct nlmsghdr *nlh,
2641 bool strict_check,
2642 struct neigh_dump_filter *filter,
2643 struct netlink_ext_ack *extack)
2644{
2645 struct nlattr *tb[NDA_MAX + 1];
2646 int err, i;
2647
2648 if (strict_check) {
2649 struct ndmsg *ndm;
2650
2651 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ndm))) {
2652 NL_SET_ERR_MSG(extack, "Invalid header for neighbor dump request");
2653 return -EINVAL;
2654 }
2655
2656 ndm = nlmsg_data(nlh);
2657 if (ndm->ndm_pad1 || ndm->ndm_pad2 || ndm->ndm_ifindex ||
David Ahernc0fde872018-12-19 16:54:38 -08002658 ndm->ndm_state || ndm->ndm_type) {
David Ahern51183d22018-10-07 20:16:36 -07002659 NL_SET_ERR_MSG(extack, "Invalid values in header for neighbor dump request");
2660 return -EINVAL;
2661 }
2662
David Ahernc0fde872018-12-19 16:54:38 -08002663 if (ndm->ndm_flags & ~NTF_PROXY) {
2664 NL_SET_ERR_MSG(extack, "Invalid flags in header for neighbor dump request");
2665 return -EINVAL;
2666 }
2667
Johannes Berg8cb08172019-04-26 14:07:28 +02002668 err = nlmsg_parse_deprecated_strict(nlh, sizeof(struct ndmsg),
2669 tb, NDA_MAX, nda_policy,
2670 extack);
David Ahern51183d22018-10-07 20:16:36 -07002671 } else {
Johannes Berg8cb08172019-04-26 14:07:28 +02002672 err = nlmsg_parse_deprecated(nlh, sizeof(struct ndmsg), tb,
2673 NDA_MAX, nda_policy, extack);
David Ahern51183d22018-10-07 20:16:36 -07002674 }
2675 if (err < 0)
2676 return err;
2677
2678 for (i = 0; i <= NDA_MAX; ++i) {
2679 if (!tb[i])
2680 continue;
2681
2682 /* all new attributes should require strict_check */
2683 switch (i) {
2684 case NDA_IFINDEX:
David Ahern51183d22018-10-07 20:16:36 -07002685 filter->dev_idx = nla_get_u32(tb[i]);
2686 break;
2687 case NDA_MASTER:
David Ahern51183d22018-10-07 20:16:36 -07002688 filter->master_idx = nla_get_u32(tb[i]);
2689 break;
2690 default:
2691 if (strict_check) {
2692 NL_SET_ERR_MSG(extack, "Unsupported attribute in neighbor dump request");
2693 return -EINVAL;
2694 }
2695 }
2696 }
2697
2698 return 0;
2699}
2700
Thomas Grafc8822a42007-03-22 11:50:06 -07002701static int neigh_dump_info(struct sk_buff *skb, struct netlink_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702{
David Ahern6f52f802018-10-03 15:33:12 -07002703 const struct nlmsghdr *nlh = cb->nlh;
2704 struct neigh_dump_filter filter = {};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705 struct neigh_table *tbl;
2706 int t, family, s_t;
Tony Zelenoff84920c12012-01-26 22:28:58 +00002707 int proxy = 0;
Eric Dumazet4bd6683b2012-06-07 04:58:35 +00002708 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709
David Ahern6f52f802018-10-03 15:33:12 -07002710 family = ((struct rtgenmsg *)nlmsg_data(nlh))->rtgen_family;
Tony Zelenoff84920c12012-01-26 22:28:58 +00002711
2712 /* check for full ndmsg structure presence, family member is
2713 * the same for both structures
2714 */
David Ahern6f52f802018-10-03 15:33:12 -07002715 if (nlmsg_len(nlh) >= sizeof(struct ndmsg) &&
2716 ((struct ndmsg *)nlmsg_data(nlh))->ndm_flags == NTF_PROXY)
Tony Zelenoff84920c12012-01-26 22:28:58 +00002717 proxy = 1;
2718
David Ahern51183d22018-10-07 20:16:36 -07002719 err = neigh_valid_dump_req(nlh, cb->strict_check, &filter, cb->extack);
2720 if (err < 0 && cb->strict_check)
2721 return err;
2722
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723 s_t = cb->args[0];
2724
WANG Congd7480fd2014-11-10 15:59:36 -08002725 for (t = 0; t < NEIGH_NR_TABLES; t++) {
2726 tbl = neigh_tables[t];
2727
2728 if (!tbl)
2729 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730 if (t < s_t || (family && tbl->family != family))
2731 continue;
2732 if (t > s_t)
2733 memset(&cb->args[1], 0, sizeof(cb->args) -
2734 sizeof(cb->args[0]));
Tony Zelenoff84920c12012-01-26 22:28:58 +00002735 if (proxy)
David Ahern6f52f802018-10-03 15:33:12 -07002736 err = pneigh_dump_table(tbl, skb, cb, &filter);
Tony Zelenoff84920c12012-01-26 22:28:58 +00002737 else
David Ahern6f52f802018-10-03 15:33:12 -07002738 err = neigh_dump_table(tbl, skb, cb, &filter);
Eric Dumazet4bd6683b2012-06-07 04:58:35 +00002739 if (err < 0)
2740 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742
2743 cb->args[0] = t;
2744 return skb->len;
2745}
2746
Roopa Prabhu82cbb5c2018-12-19 12:51:38 -08002747static int neigh_valid_get_req(const struct nlmsghdr *nlh,
2748 struct neigh_table **tbl,
2749 void **dst, int *dev_idx, u8 *ndm_flags,
2750 struct netlink_ext_ack *extack)
2751{
2752 struct nlattr *tb[NDA_MAX + 1];
2753 struct ndmsg *ndm;
2754 int err, i;
2755
2756 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ndm))) {
2757 NL_SET_ERR_MSG(extack, "Invalid header for neighbor get request");
2758 return -EINVAL;
2759 }
2760
2761 ndm = nlmsg_data(nlh);
2762 if (ndm->ndm_pad1 || ndm->ndm_pad2 || ndm->ndm_state ||
2763 ndm->ndm_type) {
2764 NL_SET_ERR_MSG(extack, "Invalid values in header for neighbor get request");
2765 return -EINVAL;
2766 }
2767
2768 if (ndm->ndm_flags & ~NTF_PROXY) {
2769 NL_SET_ERR_MSG(extack, "Invalid flags in header for neighbor get request");
2770 return -EINVAL;
2771 }
2772
Johannes Berg8cb08172019-04-26 14:07:28 +02002773 err = nlmsg_parse_deprecated_strict(nlh, sizeof(struct ndmsg), tb,
2774 NDA_MAX, nda_policy, extack);
Roopa Prabhu82cbb5c2018-12-19 12:51:38 -08002775 if (err < 0)
2776 return err;
2777
2778 *ndm_flags = ndm->ndm_flags;
2779 *dev_idx = ndm->ndm_ifindex;
2780 *tbl = neigh_find_table(ndm->ndm_family);
2781 if (*tbl == NULL) {
2782 NL_SET_ERR_MSG(extack, "Unsupported family in header for neighbor get request");
2783 return -EAFNOSUPPORT;
2784 }
2785
2786 for (i = 0; i <= NDA_MAX; ++i) {
2787 if (!tb[i])
2788 continue;
2789
2790 switch (i) {
2791 case NDA_DST:
2792 if (nla_len(tb[i]) != (int)(*tbl)->key_len) {
2793 NL_SET_ERR_MSG(extack, "Invalid network address in neighbor get request");
2794 return -EINVAL;
2795 }
2796 *dst = nla_data(tb[i]);
2797 break;
2798 default:
2799 NL_SET_ERR_MSG(extack, "Unsupported attribute in neighbor get request");
2800 return -EINVAL;
2801 }
2802 }
2803
2804 return 0;
2805}
2806
2807static inline size_t neigh_nlmsg_size(void)
2808{
2809 return NLMSG_ALIGN(sizeof(struct ndmsg))
2810 + nla_total_size(MAX_ADDR_LEN) /* NDA_DST */
2811 + nla_total_size(MAX_ADDR_LEN) /* NDA_LLADDR */
2812 + nla_total_size(sizeof(struct nda_cacheinfo))
2813 + nla_total_size(4) /* NDA_PROBES */
2814 + nla_total_size(1); /* NDA_PROTOCOL */
2815}
2816
2817static int neigh_get_reply(struct net *net, struct neighbour *neigh,
2818 u32 pid, u32 seq)
2819{
2820 struct sk_buff *skb;
2821 int err = 0;
2822
2823 skb = nlmsg_new(neigh_nlmsg_size(), GFP_KERNEL);
2824 if (!skb)
2825 return -ENOBUFS;
2826
2827 err = neigh_fill_info(skb, neigh, pid, seq, RTM_NEWNEIGH, 0);
2828 if (err) {
2829 kfree_skb(skb);
2830 goto errout;
2831 }
2832
2833 err = rtnl_unicast(skb, net, pid);
2834errout:
2835 return err;
2836}
2837
2838static inline size_t pneigh_nlmsg_size(void)
2839{
2840 return NLMSG_ALIGN(sizeof(struct ndmsg))
Colin Ian King463561e2018-12-20 16:50:50 +00002841 + nla_total_size(MAX_ADDR_LEN) /* NDA_DST */
Roopa Prabhu82cbb5c2018-12-19 12:51:38 -08002842 + nla_total_size(1); /* NDA_PROTOCOL */
2843}
2844
2845static int pneigh_get_reply(struct net *net, struct pneigh_entry *neigh,
2846 u32 pid, u32 seq, struct neigh_table *tbl)
2847{
2848 struct sk_buff *skb;
2849 int err = 0;
2850
2851 skb = nlmsg_new(pneigh_nlmsg_size(), GFP_KERNEL);
2852 if (!skb)
2853 return -ENOBUFS;
2854
2855 err = pneigh_fill_info(skb, neigh, pid, seq, RTM_NEWNEIGH, 0, tbl);
2856 if (err) {
2857 kfree_skb(skb);
2858 goto errout;
2859 }
2860
2861 err = rtnl_unicast(skb, net, pid);
2862errout:
2863 return err;
2864}
2865
2866static int neigh_get(struct sk_buff *in_skb, struct nlmsghdr *nlh,
2867 struct netlink_ext_ack *extack)
2868{
2869 struct net *net = sock_net(in_skb->sk);
2870 struct net_device *dev = NULL;
2871 struct neigh_table *tbl = NULL;
2872 struct neighbour *neigh;
2873 void *dst = NULL;
2874 u8 ndm_flags = 0;
2875 int dev_idx = 0;
2876 int err;
2877
2878 err = neigh_valid_get_req(nlh, &tbl, &dst, &dev_idx, &ndm_flags,
2879 extack);
2880 if (err < 0)
2881 return err;
2882
2883 if (dev_idx) {
2884 dev = __dev_get_by_index(net, dev_idx);
2885 if (!dev) {
2886 NL_SET_ERR_MSG(extack, "Unknown device ifindex");
2887 return -ENODEV;
2888 }
2889 }
2890
2891 if (!dst) {
2892 NL_SET_ERR_MSG(extack, "Network address not specified");
2893 return -EINVAL;
2894 }
2895
2896 if (ndm_flags & NTF_PROXY) {
2897 struct pneigh_entry *pn;
2898
2899 pn = pneigh_lookup(tbl, net, dst, dev, 0);
2900 if (!pn) {
2901 NL_SET_ERR_MSG(extack, "Proxy neighbour entry not found");
2902 return -ENOENT;
2903 }
2904 return pneigh_get_reply(net, pn, NETLINK_CB(in_skb).portid,
2905 nlh->nlmsg_seq, tbl);
2906 }
2907
2908 if (!dev) {
2909 NL_SET_ERR_MSG(extack, "No device specified");
2910 return -EINVAL;
2911 }
2912
2913 neigh = neigh_lookup(tbl, dst, dev);
2914 if (!neigh) {
2915 NL_SET_ERR_MSG(extack, "Neighbour entry not found");
2916 return -ENOENT;
2917 }
2918
2919 err = neigh_get_reply(net, neigh, NETLINK_CB(in_skb).portid,
2920 nlh->nlmsg_seq);
2921
2922 neigh_release(neigh);
2923
2924 return err;
2925}
2926
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927void neigh_for_each(struct neigh_table *tbl, void (*cb)(struct neighbour *, void *), void *cookie)
2928{
2929 int chain;
Eric Dumazetd6bf7812010-10-04 06:15:44 +00002930 struct neigh_hash_table *nht;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931
Eric Dumazetd6bf7812010-10-04 06:15:44 +00002932 rcu_read_lock_bh();
2933 nht = rcu_dereference_bh(tbl->nht);
2934
Eric Dumazet767e97e2010-10-06 17:49:21 -07002935 read_lock(&tbl->lock); /* avoid resizes */
David S. Millercd089332011-07-11 01:28:12 -07002936 for (chain = 0; chain < (1 << nht->hash_shift); chain++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937 struct neighbour *n;
2938
Eric Dumazet767e97e2010-10-06 17:49:21 -07002939 for (n = rcu_dereference_bh(nht->hash_buckets[chain]);
2940 n != NULL;
2941 n = rcu_dereference_bh(n->next))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942 cb(n, cookie);
2943 }
Eric Dumazetd6bf7812010-10-04 06:15:44 +00002944 read_unlock(&tbl->lock);
2945 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946}
2947EXPORT_SYMBOL(neigh_for_each);
2948
2949/* The tbl->lock must be held as a writer and BH disabled. */
2950void __neigh_for_each_release(struct neigh_table *tbl,
2951 int (*cb)(struct neighbour *))
2952{
2953 int chain;
Eric Dumazetd6bf7812010-10-04 06:15:44 +00002954 struct neigh_hash_table *nht;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955
Eric Dumazetd6bf7812010-10-04 06:15:44 +00002956 nht = rcu_dereference_protected(tbl->nht,
2957 lockdep_is_held(&tbl->lock));
David S. Millercd089332011-07-11 01:28:12 -07002958 for (chain = 0; chain < (1 << nht->hash_shift); chain++) {
Eric Dumazet767e97e2010-10-06 17:49:21 -07002959 struct neighbour *n;
2960 struct neighbour __rcu **np;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961
Eric Dumazetd6bf7812010-10-04 06:15:44 +00002962 np = &nht->hash_buckets[chain];
Eric Dumazet767e97e2010-10-06 17:49:21 -07002963 while ((n = rcu_dereference_protected(*np,
2964 lockdep_is_held(&tbl->lock))) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965 int release;
2966
2967 write_lock(&n->lock);
2968 release = cb(n);
2969 if (release) {
Eric Dumazet767e97e2010-10-06 17:49:21 -07002970 rcu_assign_pointer(*np,
2971 rcu_dereference_protected(n->next,
2972 lockdep_is_held(&tbl->lock)));
David Ahern58956312018-12-07 12:24:57 -08002973 neigh_mark_dead(n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974 } else
2975 np = &n->next;
2976 write_unlock(&n->lock);
Thomas Graf4f494552007-08-08 23:12:36 -07002977 if (release)
2978 neigh_cleanup_and_release(n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979 }
2980 }
2981}
2982EXPORT_SYMBOL(__neigh_for_each_release);
2983
Eric W. Biedermanb79bda32015-03-07 16:25:56 -06002984int neigh_xmit(int index, struct net_device *dev,
Eric W. Biederman4fd3d7d2015-03-03 17:11:16 -06002985 const void *addr, struct sk_buff *skb)
2986{
Eric W. Biedermanb79bda32015-03-07 16:25:56 -06002987 int err = -EAFNOSUPPORT;
2988 if (likely(index < NEIGH_NR_TABLES)) {
Eric W. Biederman4fd3d7d2015-03-03 17:11:16 -06002989 struct neigh_table *tbl;
2990 struct neighbour *neigh;
2991
Eric W. Biedermanb79bda32015-03-07 16:25:56 -06002992 tbl = neigh_tables[index];
Eric W. Biederman4fd3d7d2015-03-03 17:11:16 -06002993 if (!tbl)
2994 goto out;
David Barrosob560f032016-06-28 11:16:43 +03002995 rcu_read_lock_bh();
David Ahern4b2a2bf2019-05-01 18:18:42 -07002996 if (index == NEIGH_ARP_TABLE) {
2997 u32 key = *((u32 *)addr);
2998
2999 neigh = __ipv4_neigh_lookup_noref(dev, key);
3000 } else {
3001 neigh = __neigh_lookup_noref(tbl, addr, dev);
3002 }
Eric W. Biederman4fd3d7d2015-03-03 17:11:16 -06003003 if (!neigh)
3004 neigh = __neigh_create(tbl, addr, dev, false);
3005 err = PTR_ERR(neigh);
David Barrosob560f032016-06-28 11:16:43 +03003006 if (IS_ERR(neigh)) {
3007 rcu_read_unlock_bh();
Eric W. Biederman4fd3d7d2015-03-03 17:11:16 -06003008 goto out_kfree_skb;
David Barrosob560f032016-06-28 11:16:43 +03003009 }
Eric W. Biederman4fd3d7d2015-03-03 17:11:16 -06003010 err = neigh->output(neigh, skb);
David Barrosob560f032016-06-28 11:16:43 +03003011 rcu_read_unlock_bh();
Eric W. Biederman4fd3d7d2015-03-03 17:11:16 -06003012 }
Eric W. Biedermanb79bda32015-03-07 16:25:56 -06003013 else if (index == NEIGH_LINK_TABLE) {
3014 err = dev_hard_header(skb, dev, ntohs(skb->protocol),
3015 addr, NULL, skb->len);
3016 if (err < 0)
3017 goto out_kfree_skb;
3018 err = dev_queue_xmit(skb);
3019 }
Eric W. Biederman4fd3d7d2015-03-03 17:11:16 -06003020out:
3021 return err;
3022out_kfree_skb:
3023 kfree_skb(skb);
3024 goto out;
3025}
3026EXPORT_SYMBOL(neigh_xmit);
3027
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028#ifdef CONFIG_PROC_FS
3029
3030static struct neighbour *neigh_get_first(struct seq_file *seq)
3031{
3032 struct neigh_seq_state *state = seq->private;
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09003033 struct net *net = seq_file_net(seq);
Eric Dumazetd6bf7812010-10-04 06:15:44 +00003034 struct neigh_hash_table *nht = state->nht;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035 struct neighbour *n = NULL;
Colin Ian Kingf530eed2019-07-26 10:46:11 +01003036 int bucket;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037
3038 state->flags &= ~NEIGH_SEQ_IS_PNEIGH;
David S. Millercd089332011-07-11 01:28:12 -07003039 for (bucket = 0; bucket < (1 << nht->hash_shift); bucket++) {
Eric Dumazet767e97e2010-10-06 17:49:21 -07003040 n = rcu_dereference_bh(nht->hash_buckets[bucket]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041
3042 while (n) {
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09003043 if (!net_eq(dev_net(n->dev), net))
Eric W. Biederman426b5302008-01-24 00:13:18 -08003044 goto next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045 if (state->neigh_sub_iter) {
3046 loff_t fakep = 0;
3047 void *v;
3048
3049 v = state->neigh_sub_iter(state, n, &fakep);
3050 if (!v)
3051 goto next;
3052 }
3053 if (!(state->flags & NEIGH_SEQ_SKIP_NOARP))
3054 break;
3055 if (n->nud_state & ~NUD_NOARP)
3056 break;
Eric Dumazet767e97e2010-10-06 17:49:21 -07003057next:
3058 n = rcu_dereference_bh(n->next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059 }
3060
3061 if (n)
3062 break;
3063 }
3064 state->bucket = bucket;
3065
3066 return n;
3067}
3068
3069static struct neighbour *neigh_get_next(struct seq_file *seq,
3070 struct neighbour *n,
3071 loff_t *pos)
3072{
3073 struct neigh_seq_state *state = seq->private;
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09003074 struct net *net = seq_file_net(seq);
Eric Dumazetd6bf7812010-10-04 06:15:44 +00003075 struct neigh_hash_table *nht = state->nht;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076
3077 if (state->neigh_sub_iter) {
3078 void *v = state->neigh_sub_iter(state, n, pos);
3079 if (v)
3080 return n;
3081 }
Eric Dumazet767e97e2010-10-06 17:49:21 -07003082 n = rcu_dereference_bh(n->next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083
3084 while (1) {
3085 while (n) {
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09003086 if (!net_eq(dev_net(n->dev), net))
Eric W. Biederman426b5302008-01-24 00:13:18 -08003087 goto next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088 if (state->neigh_sub_iter) {
3089 void *v = state->neigh_sub_iter(state, n, pos);
3090 if (v)
3091 return n;
3092 goto next;
3093 }
3094 if (!(state->flags & NEIGH_SEQ_SKIP_NOARP))
3095 break;
3096
3097 if (n->nud_state & ~NUD_NOARP)
3098 break;
Eric Dumazet767e97e2010-10-06 17:49:21 -07003099next:
3100 n = rcu_dereference_bh(n->next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101 }
3102
3103 if (n)
3104 break;
3105
David S. Millercd089332011-07-11 01:28:12 -07003106 if (++state->bucket >= (1 << nht->hash_shift))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107 break;
3108
Eric Dumazet767e97e2010-10-06 17:49:21 -07003109 n = rcu_dereference_bh(nht->hash_buckets[state->bucket]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110 }
3111
3112 if (n && pos)
3113 --(*pos);
3114 return n;
3115}
3116
3117static struct neighbour *neigh_get_idx(struct seq_file *seq, loff_t *pos)
3118{
3119 struct neighbour *n = neigh_get_first(seq);
3120
3121 if (n) {
Chris Larson745e2032008-08-03 01:10:55 -07003122 --(*pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123 while (*pos) {
3124 n = neigh_get_next(seq, n, pos);
3125 if (!n)
3126 break;
3127 }
3128 }
3129 return *pos ? NULL : n;
3130}
3131
3132static struct pneigh_entry *pneigh_get_first(struct seq_file *seq)
3133{
3134 struct neigh_seq_state *state = seq->private;
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09003135 struct net *net = seq_file_net(seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136 struct neigh_table *tbl = state->tbl;
3137 struct pneigh_entry *pn = NULL;
3138 int bucket = state->bucket;
3139
3140 state->flags |= NEIGH_SEQ_IS_PNEIGH;
3141 for (bucket = 0; bucket <= PNEIGH_HASHMASK; bucket++) {
3142 pn = tbl->phash_buckets[bucket];
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09003143 while (pn && !net_eq(pneigh_net(pn), net))
Eric W. Biederman426b5302008-01-24 00:13:18 -08003144 pn = pn->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003145 if (pn)
3146 break;
3147 }
3148 state->bucket = bucket;
3149
3150 return pn;
3151}
3152
3153static struct pneigh_entry *pneigh_get_next(struct seq_file *seq,
3154 struct pneigh_entry *pn,
3155 loff_t *pos)
3156{
3157 struct neigh_seq_state *state = seq->private;
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09003158 struct net *net = seq_file_net(seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159 struct neigh_table *tbl = state->tbl;
3160
Jorge Boncompte [DTI2]df07a942011-11-25 13:24:49 -05003161 do {
3162 pn = pn->next;
3163 } while (pn && !net_eq(pneigh_net(pn), net));
3164
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165 while (!pn) {
3166 if (++state->bucket > PNEIGH_HASHMASK)
3167 break;
3168 pn = tbl->phash_buckets[state->bucket];
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09003169 while (pn && !net_eq(pneigh_net(pn), net))
Eric W. Biederman426b5302008-01-24 00:13:18 -08003170 pn = pn->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171 if (pn)
3172 break;
3173 }
3174
3175 if (pn && pos)
3176 --(*pos);
3177
3178 return pn;
3179}
3180
3181static struct pneigh_entry *pneigh_get_idx(struct seq_file *seq, loff_t *pos)
3182{
3183 struct pneigh_entry *pn = pneigh_get_first(seq);
3184
3185 if (pn) {
Chris Larson745e2032008-08-03 01:10:55 -07003186 --(*pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187 while (*pos) {
3188 pn = pneigh_get_next(seq, pn, pos);
3189 if (!pn)
3190 break;
3191 }
3192 }
3193 return *pos ? NULL : pn;
3194}
3195
3196static void *neigh_get_idx_any(struct seq_file *seq, loff_t *pos)
3197{
3198 struct neigh_seq_state *state = seq->private;
3199 void *rc;
Chris Larson745e2032008-08-03 01:10:55 -07003200 loff_t idxpos = *pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003201
Chris Larson745e2032008-08-03 01:10:55 -07003202 rc = neigh_get_idx(seq, &idxpos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203 if (!rc && !(state->flags & NEIGH_SEQ_NEIGH_ONLY))
Chris Larson745e2032008-08-03 01:10:55 -07003204 rc = pneigh_get_idx(seq, &idxpos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205
3206 return rc;
3207}
3208
3209void *neigh_seq_start(struct seq_file *seq, loff_t *pos, struct neigh_table *tbl, unsigned int neigh_seq_flags)
Eric Dumazetf3e92cb2019-06-15 16:28:48 -07003210 __acquires(tbl->lock)
Eric Dumazetd6bf7812010-10-04 06:15:44 +00003211 __acquires(rcu_bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212{
3213 struct neigh_seq_state *state = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214
3215 state->tbl = tbl;
3216 state->bucket = 0;
3217 state->flags = (neigh_seq_flags & ~NEIGH_SEQ_IS_PNEIGH);
3218
Eric Dumazetd6bf7812010-10-04 06:15:44 +00003219 rcu_read_lock_bh();
3220 state->nht = rcu_dereference_bh(tbl->nht);
Eric Dumazetf3e92cb2019-06-15 16:28:48 -07003221 read_lock(&tbl->lock);
Eric Dumazet767e97e2010-10-06 17:49:21 -07003222
Chris Larson745e2032008-08-03 01:10:55 -07003223 return *pos ? neigh_get_idx_any(seq, pos) : SEQ_START_TOKEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224}
3225EXPORT_SYMBOL(neigh_seq_start);
3226
3227void *neigh_seq_next(struct seq_file *seq, void *v, loff_t *pos)
3228{
3229 struct neigh_seq_state *state;
3230 void *rc;
3231
3232 if (v == SEQ_START_TOKEN) {
Chris Larsonbff69732008-08-03 01:02:41 -07003233 rc = neigh_get_first(seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234 goto out;
3235 }
3236
3237 state = seq->private;
3238 if (!(state->flags & NEIGH_SEQ_IS_PNEIGH)) {
3239 rc = neigh_get_next(seq, v, NULL);
3240 if (rc)
3241 goto out;
3242 if (!(state->flags & NEIGH_SEQ_NEIGH_ONLY))
3243 rc = pneigh_get_first(seq);
3244 } else {
3245 BUG_ON(state->flags & NEIGH_SEQ_NEIGH_ONLY);
3246 rc = pneigh_get_next(seq, v, NULL);
3247 }
3248out:
3249 ++(*pos);
3250 return rc;
3251}
3252EXPORT_SYMBOL(neigh_seq_next);
3253
3254void neigh_seq_stop(struct seq_file *seq, void *v)
Eric Dumazetf3e92cb2019-06-15 16:28:48 -07003255 __releases(tbl->lock)
Eric Dumazetd6bf7812010-10-04 06:15:44 +00003256 __releases(rcu_bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257{
Eric Dumazetf3e92cb2019-06-15 16:28:48 -07003258 struct neigh_seq_state *state = seq->private;
3259 struct neigh_table *tbl = state->tbl;
3260
3261 read_unlock(&tbl->lock);
Eric Dumazetd6bf7812010-10-04 06:15:44 +00003262 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263}
3264EXPORT_SYMBOL(neigh_seq_stop);
3265
3266/* statistics via seq_file */
3267
3268static void *neigh_stat_seq_start(struct seq_file *seq, loff_t *pos)
3269{
Christoph Hellwig71a50532018-04-15 10:16:41 +02003270 struct neigh_table *tbl = PDE_DATA(file_inode(seq->file));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271 int cpu;
3272
3273 if (*pos == 0)
3274 return SEQ_START_TOKEN;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003275
Rusty Russell0f23174a2008-12-29 12:23:42 +00003276 for (cpu = *pos-1; cpu < nr_cpu_ids; ++cpu) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003277 if (!cpu_possible(cpu))
3278 continue;
3279 *pos = cpu+1;
3280 return per_cpu_ptr(tbl->stats, cpu);
3281 }
3282 return NULL;
3283}
3284
3285static void *neigh_stat_seq_next(struct seq_file *seq, void *v, loff_t *pos)
3286{
Christoph Hellwig71a50532018-04-15 10:16:41 +02003287 struct neigh_table *tbl = PDE_DATA(file_inode(seq->file));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003288 int cpu;
3289
Rusty Russell0f23174a2008-12-29 12:23:42 +00003290 for (cpu = *pos; cpu < nr_cpu_ids; ++cpu) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003291 if (!cpu_possible(cpu))
3292 continue;
3293 *pos = cpu+1;
3294 return per_cpu_ptr(tbl->stats, cpu);
3295 }
3296 return NULL;
3297}
3298
3299static void neigh_stat_seq_stop(struct seq_file *seq, void *v)
3300{
3301
3302}
3303
3304static int neigh_stat_seq_show(struct seq_file *seq, void *v)
3305{
Christoph Hellwig71a50532018-04-15 10:16:41 +02003306 struct neigh_table *tbl = PDE_DATA(file_inode(seq->file));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003307 struct neigh_statistics *st = v;
3308
3309 if (v == SEQ_START_TOKEN) {
Rick Jonesfb811392015-08-07 11:10:37 -07003310 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 -07003311 return 0;
3312 }
3313
3314 seq_printf(seq, "%08x %08lx %08lx %08lx %08lx %08lx %08lx "
Rick Jonesfb811392015-08-07 11:10:37 -07003315 "%08lx %08lx %08lx %08lx %08lx %08lx\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316 atomic_read(&tbl->entries),
3317
3318 st->allocs,
3319 st->destroys,
3320 st->hash_grows,
3321
3322 st->lookups,
3323 st->hits,
3324
3325 st->res_failed,
3326
3327 st->rcv_probes_mcast,
3328 st->rcv_probes_ucast,
3329
3330 st->periodic_gc_runs,
Neil Horman9a6d2762008-07-16 20:50:49 -07003331 st->forced_gc_runs,
Rick Jonesfb811392015-08-07 11:10:37 -07003332 st->unres_discards,
3333 st->table_fulls
Linus Torvalds1da177e2005-04-16 15:20:36 -07003334 );
3335
3336 return 0;
3337}
3338
Stephen Hemmingerf6908082007-03-12 14:34:29 -07003339static const struct seq_operations neigh_stat_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003340 .start = neigh_stat_seq_start,
3341 .next = neigh_stat_seq_next,
3342 .stop = neigh_stat_seq_stop,
3343 .show = neigh_stat_seq_show,
3344};
Linus Torvalds1da177e2005-04-16 15:20:36 -07003345#endif /* CONFIG_PROC_FS */
3346
Roopa Prabhu7b8f7a42017-03-19 22:01:28 -07003347static void __neigh_notify(struct neighbour *n, int type, int flags,
3348 u32 pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003349{
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09003350 struct net *net = dev_net(n->dev);
Thomas Graf8b8aec52006-08-07 17:56:37 -07003351 struct sk_buff *skb;
Thomas Grafb8673312006-08-15 00:33:14 -07003352 int err = -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003353
Thomas Graf339bf982006-11-10 14:10:15 -08003354 skb = nlmsg_new(neigh_nlmsg_size(), GFP_ATOMIC);
Thomas Graf8b8aec52006-08-07 17:56:37 -07003355 if (skb == NULL)
Thomas Grafb8673312006-08-15 00:33:14 -07003356 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357
Roopa Prabhu7b8f7a42017-03-19 22:01:28 -07003358 err = neigh_fill_info(skb, n, pid, 0, type, flags);
Patrick McHardy26932562007-01-31 23:16:40 -08003359 if (err < 0) {
3360 /* -EMSGSIZE implies BUG in neigh_nlmsg_size() */
3361 WARN_ON(err == -EMSGSIZE);
3362 kfree_skb(skb);
3363 goto errout;
3364 }
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -08003365 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
3366 return;
Thomas Grafb8673312006-08-15 00:33:14 -07003367errout:
3368 if (err < 0)
Eric W. Biederman426b5302008-01-24 00:13:18 -08003369 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
Thomas Grafb8673312006-08-15 00:33:14 -07003370}
3371
3372void neigh_app_ns(struct neighbour *n)
3373{
Roopa Prabhu7b8f7a42017-03-19 22:01:28 -07003374 __neigh_notify(n, RTM_GETNEIGH, NLM_F_REQUEST, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375}
YOSHIFUJI Hideaki0a204502008-03-24 18:39:10 +09003376EXPORT_SYMBOL(neigh_app_ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003377
3378#ifdef CONFIG_SYSCTL
Cong Wangb93196d2012-12-06 10:04:04 +08003379static int unres_qlen_max = INT_MAX / SKB_TRUESIZE(ETH_FRAME_LEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003380
Joe Perchesfe2c6332013-06-11 23:04:25 -07003381static int proc_unres_qlen(struct ctl_table *ctl, int write,
3382 void __user *buffer, size_t *lenp, loff_t *ppos)
Eric Dumazet8b5c1712011-11-09 12:07:14 +00003383{
3384 int size, ret;
Joe Perchesfe2c6332013-06-11 23:04:25 -07003385 struct ctl_table tmp = *ctl;
Eric Dumazet8b5c1712011-11-09 12:07:14 +00003386
Matteo Croceeec48442019-07-18 15:58:50 -07003387 tmp.extra1 = SYSCTL_ZERO;
Shan Weice46cc62012-12-04 18:49:15 +00003388 tmp.extra2 = &unres_qlen_max;
Eric Dumazet8b5c1712011-11-09 12:07:14 +00003389 tmp.data = &size;
Shan Weice46cc62012-12-04 18:49:15 +00003390
3391 size = *(int *)ctl->data / SKB_TRUESIZE(ETH_FRAME_LEN);
3392 ret = proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos);
3393
Eric Dumazet8b5c1712011-11-09 12:07:14 +00003394 if (write && !ret)
3395 *(int *)ctl->data = size * SKB_TRUESIZE(ETH_FRAME_LEN);
3396 return ret;
3397}
3398
Jiri Pirko1d4c8c22013-12-07 19:26:56 +01003399static struct neigh_parms *neigh_get_dev_parms_rcu(struct net_device *dev,
3400 int family)
3401{
Jiri Pirkobba24892013-12-07 19:26:57 +01003402 switch (family) {
3403 case AF_INET:
Jiri Pirko1d4c8c22013-12-07 19:26:56 +01003404 return __in_dev_arp_parms_get_rcu(dev);
Jiri Pirkobba24892013-12-07 19:26:57 +01003405 case AF_INET6:
3406 return __in6_dev_nd_parms_get_rcu(dev);
3407 }
Jiri Pirko1d4c8c22013-12-07 19:26:56 +01003408 return NULL;
3409}
3410
3411static void neigh_copy_dflt_parms(struct net *net, struct neigh_parms *p,
3412 int index)
3413{
3414 struct net_device *dev;
3415 int family = neigh_parms_family(p);
3416
3417 rcu_read_lock();
3418 for_each_netdev_rcu(net, dev) {
3419 struct neigh_parms *dst_p =
3420 neigh_get_dev_parms_rcu(dev, family);
3421
3422 if (dst_p && !test_bit(index, dst_p->data_state))
3423 dst_p->data[index] = p->data[index];
3424 }
3425 rcu_read_unlock();
3426}
3427
3428static void neigh_proc_update(struct ctl_table *ctl, int write)
3429{
3430 struct net_device *dev = ctl->extra1;
3431 struct neigh_parms *p = ctl->extra2;
Jiri Pirko77d47af2013-12-10 23:55:07 +01003432 struct net *net = neigh_parms_net(p);
Jiri Pirko1d4c8c22013-12-07 19:26:56 +01003433 int index = (int *) ctl->data - p->data;
3434
3435 if (!write)
3436 return;
3437
3438 set_bit(index, p->data_state);
Marcus Huewe7627ae62017-02-15 01:00:36 +01003439 if (index == NEIGH_VAR_DELAY_PROBE_TIME)
3440 call_netevent_notifiers(NETEVENT_DELAY_PROBE_TIME_UPDATE, p);
Jiri Pirko1d4c8c22013-12-07 19:26:56 +01003441 if (!dev) /* NULL dev means this is default value */
3442 neigh_copy_dflt_parms(net, p, index);
3443}
3444
Jiri Pirko1f9248e2013-12-07 19:26:53 +01003445static int neigh_proc_dointvec_zero_intmax(struct ctl_table *ctl, int write,
3446 void __user *buffer,
3447 size_t *lenp, loff_t *ppos)
3448{
3449 struct ctl_table tmp = *ctl;
Jiri Pirko1d4c8c22013-12-07 19:26:56 +01003450 int ret;
Jiri Pirko1f9248e2013-12-07 19:26:53 +01003451
Matteo Croceeec48442019-07-18 15:58:50 -07003452 tmp.extra1 = SYSCTL_ZERO;
3453 tmp.extra2 = SYSCTL_INT_MAX;
Jiri Pirko1f9248e2013-12-07 19:26:53 +01003454
Jiri Pirko1d4c8c22013-12-07 19:26:56 +01003455 ret = proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos);
3456 neigh_proc_update(ctl, write);
3457 return ret;
Jiri Pirko1f9248e2013-12-07 19:26:53 +01003458}
3459
Jiri Pirkocb5b09c2013-12-07 19:26:54 +01003460int neigh_proc_dointvec(struct ctl_table *ctl, int write,
3461 void __user *buffer, size_t *lenp, loff_t *ppos)
3462{
Jiri Pirko1d4c8c22013-12-07 19:26:56 +01003463 int ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
3464
3465 neigh_proc_update(ctl, write);
3466 return ret;
Jiri Pirkocb5b09c2013-12-07 19:26:54 +01003467}
3468EXPORT_SYMBOL(neigh_proc_dointvec);
3469
3470int neigh_proc_dointvec_jiffies(struct ctl_table *ctl, int write,
3471 void __user *buffer,
3472 size_t *lenp, loff_t *ppos)
3473{
Jiri Pirko1d4c8c22013-12-07 19:26:56 +01003474 int ret = proc_dointvec_jiffies(ctl, write, buffer, lenp, ppos);
3475
3476 neigh_proc_update(ctl, write);
3477 return ret;
Jiri Pirkocb5b09c2013-12-07 19:26:54 +01003478}
3479EXPORT_SYMBOL(neigh_proc_dointvec_jiffies);
3480
3481static int neigh_proc_dointvec_userhz_jiffies(struct ctl_table *ctl, int write,
3482 void __user *buffer,
3483 size_t *lenp, loff_t *ppos)
3484{
Jiri Pirko1d4c8c22013-12-07 19:26:56 +01003485 int ret = proc_dointvec_userhz_jiffies(ctl, write, buffer, lenp, ppos);
3486
3487 neigh_proc_update(ctl, write);
3488 return ret;
Jiri Pirkocb5b09c2013-12-07 19:26:54 +01003489}
3490
3491int neigh_proc_dointvec_ms_jiffies(struct ctl_table *ctl, int write,
3492 void __user *buffer,
3493 size_t *lenp, loff_t *ppos)
3494{
Jiri Pirko1d4c8c22013-12-07 19:26:56 +01003495 int ret = proc_dointvec_ms_jiffies(ctl, write, buffer, lenp, ppos);
3496
3497 neigh_proc_update(ctl, write);
3498 return ret;
Jiri Pirkocb5b09c2013-12-07 19:26:54 +01003499}
3500EXPORT_SYMBOL(neigh_proc_dointvec_ms_jiffies);
3501
3502static int neigh_proc_dointvec_unres_qlen(struct ctl_table *ctl, int write,
3503 void __user *buffer,
3504 size_t *lenp, loff_t *ppos)
3505{
Jiri Pirko1d4c8c22013-12-07 19:26:56 +01003506 int ret = proc_unres_qlen(ctl, write, buffer, lenp, ppos);
3507
3508 neigh_proc_update(ctl, write);
3509 return ret;
Jiri Pirkocb5b09c2013-12-07 19:26:54 +01003510}
3511
Jean-Francois Remy4bf69802015-01-14 04:22:39 +01003512static int neigh_proc_base_reachable_time(struct ctl_table *ctl, int write,
3513 void __user *buffer,
3514 size_t *lenp, loff_t *ppos)
3515{
3516 struct neigh_parms *p = ctl->extra2;
3517 int ret;
3518
3519 if (strcmp(ctl->procname, "base_reachable_time") == 0)
3520 ret = neigh_proc_dointvec_jiffies(ctl, write, buffer, lenp, ppos);
3521 else if (strcmp(ctl->procname, "base_reachable_time_ms") == 0)
3522 ret = neigh_proc_dointvec_ms_jiffies(ctl, write, buffer, lenp, ppos);
3523 else
3524 ret = -1;
3525
3526 if (write && ret == 0) {
3527 /* update reachable_time as well, otherwise, the change will
3528 * only be effective after the next time neigh_periodic_work
3529 * decides to recompute it
3530 */
3531 p->reachable_time =
3532 neigh_rand_reach_time(NEIGH_VAR(p, BASE_REACHABLE_TIME));
3533 }
3534 return ret;
3535}
3536
Jiri Pirko1f9248e2013-12-07 19:26:53 +01003537#define NEIGH_PARMS_DATA_OFFSET(index) \
3538 (&((struct neigh_parms *) 0)->data[index])
3539
3540#define NEIGH_SYSCTL_ENTRY(attr, data_attr, name, mval, proc) \
3541 [NEIGH_VAR_ ## attr] = { \
3542 .procname = name, \
3543 .data = NEIGH_PARMS_DATA_OFFSET(NEIGH_VAR_ ## data_attr), \
3544 .maxlen = sizeof(int), \
3545 .mode = mval, \
3546 .proc_handler = proc, \
3547 }
3548
3549#define NEIGH_SYSCTL_ZERO_INTMAX_ENTRY(attr, name) \
3550 NEIGH_SYSCTL_ENTRY(attr, attr, name, 0644, neigh_proc_dointvec_zero_intmax)
3551
3552#define NEIGH_SYSCTL_JIFFIES_ENTRY(attr, name) \
Jiri Pirkocb5b09c2013-12-07 19:26:54 +01003553 NEIGH_SYSCTL_ENTRY(attr, attr, name, 0644, neigh_proc_dointvec_jiffies)
Jiri Pirko1f9248e2013-12-07 19:26:53 +01003554
3555#define NEIGH_SYSCTL_USERHZ_JIFFIES_ENTRY(attr, name) \
Jiri Pirkocb5b09c2013-12-07 19:26:54 +01003556 NEIGH_SYSCTL_ENTRY(attr, attr, name, 0644, neigh_proc_dointvec_userhz_jiffies)
Jiri Pirko1f9248e2013-12-07 19:26:53 +01003557
3558#define NEIGH_SYSCTL_MS_JIFFIES_ENTRY(attr, name) \
Jiri Pirkocb5b09c2013-12-07 19:26:54 +01003559 NEIGH_SYSCTL_ENTRY(attr, attr, name, 0644, neigh_proc_dointvec_ms_jiffies)
Jiri Pirko1f9248e2013-12-07 19:26:53 +01003560
3561#define NEIGH_SYSCTL_MS_JIFFIES_REUSED_ENTRY(attr, data_attr, name) \
Jiri Pirkocb5b09c2013-12-07 19:26:54 +01003562 NEIGH_SYSCTL_ENTRY(attr, data_attr, name, 0644, neigh_proc_dointvec_ms_jiffies)
Jiri Pirko1f9248e2013-12-07 19:26:53 +01003563
3564#define NEIGH_SYSCTL_UNRES_QLEN_REUSED_ENTRY(attr, data_attr, name) \
Jiri Pirkocb5b09c2013-12-07 19:26:54 +01003565 NEIGH_SYSCTL_ENTRY(attr, data_attr, name, 0644, neigh_proc_dointvec_unres_qlen)
Eric W. Biederman54716e32010-02-14 03:27:03 +00003566
Linus Torvalds1da177e2005-04-16 15:20:36 -07003567static struct neigh_sysctl_table {
3568 struct ctl_table_header *sysctl_header;
Eric Dumazet8b5c1712011-11-09 12:07:14 +00003569 struct ctl_table neigh_vars[NEIGH_VAR_MAX + 1];
Brian Haleyab32ea52006-09-22 14:15:41 -07003570} neigh_sysctl_template __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003571 .neigh_vars = {
Jiri Pirko1f9248e2013-12-07 19:26:53 +01003572 NEIGH_SYSCTL_ZERO_INTMAX_ENTRY(MCAST_PROBES, "mcast_solicit"),
3573 NEIGH_SYSCTL_ZERO_INTMAX_ENTRY(UCAST_PROBES, "ucast_solicit"),
3574 NEIGH_SYSCTL_ZERO_INTMAX_ENTRY(APP_PROBES, "app_solicit"),
YOSHIFUJI Hideaki/吉藤英明8da86462015-03-19 22:41:46 +09003575 NEIGH_SYSCTL_ZERO_INTMAX_ENTRY(MCAST_REPROBES, "mcast_resolicit"),
Jiri Pirko1f9248e2013-12-07 19:26:53 +01003576 NEIGH_SYSCTL_USERHZ_JIFFIES_ENTRY(RETRANS_TIME, "retrans_time"),
3577 NEIGH_SYSCTL_JIFFIES_ENTRY(BASE_REACHABLE_TIME, "base_reachable_time"),
3578 NEIGH_SYSCTL_JIFFIES_ENTRY(DELAY_PROBE_TIME, "delay_first_probe_time"),
3579 NEIGH_SYSCTL_JIFFIES_ENTRY(GC_STALETIME, "gc_stale_time"),
3580 NEIGH_SYSCTL_ZERO_INTMAX_ENTRY(QUEUE_LEN_BYTES, "unres_qlen_bytes"),
3581 NEIGH_SYSCTL_ZERO_INTMAX_ENTRY(PROXY_QLEN, "proxy_qlen"),
3582 NEIGH_SYSCTL_USERHZ_JIFFIES_ENTRY(ANYCAST_DELAY, "anycast_delay"),
3583 NEIGH_SYSCTL_USERHZ_JIFFIES_ENTRY(PROXY_DELAY, "proxy_delay"),
3584 NEIGH_SYSCTL_USERHZ_JIFFIES_ENTRY(LOCKTIME, "locktime"),
3585 NEIGH_SYSCTL_UNRES_QLEN_REUSED_ENTRY(QUEUE_LEN, QUEUE_LEN_BYTES, "unres_qlen"),
3586 NEIGH_SYSCTL_MS_JIFFIES_REUSED_ENTRY(RETRANS_TIME_MS, RETRANS_TIME, "retrans_time_ms"),
3587 NEIGH_SYSCTL_MS_JIFFIES_REUSED_ENTRY(BASE_REACHABLE_TIME_MS, BASE_REACHABLE_TIME, "base_reachable_time_ms"),
Eric Dumazet8b5c1712011-11-09 12:07:14 +00003588 [NEIGH_VAR_GC_INTERVAL] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003589 .procname = "gc_interval",
3590 .maxlen = sizeof(int),
3591 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08003592 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003593 },
Eric Dumazet8b5c1712011-11-09 12:07:14 +00003594 [NEIGH_VAR_GC_THRESH1] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003595 .procname = "gc_thresh1",
3596 .maxlen = sizeof(int),
3597 .mode = 0644,
Matteo Croceeec48442019-07-18 15:58:50 -07003598 .extra1 = SYSCTL_ZERO,
3599 .extra2 = SYSCTL_INT_MAX,
Francesco Fusco555445c2013-07-24 10:39:06 +02003600 .proc_handler = proc_dointvec_minmax,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003601 },
Eric Dumazet8b5c1712011-11-09 12:07:14 +00003602 [NEIGH_VAR_GC_THRESH2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003603 .procname = "gc_thresh2",
3604 .maxlen = sizeof(int),
3605 .mode = 0644,
Matteo Croceeec48442019-07-18 15:58:50 -07003606 .extra1 = SYSCTL_ZERO,
3607 .extra2 = SYSCTL_INT_MAX,
Francesco Fusco555445c2013-07-24 10:39:06 +02003608 .proc_handler = proc_dointvec_minmax,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003609 },
Eric Dumazet8b5c1712011-11-09 12:07:14 +00003610 [NEIGH_VAR_GC_THRESH3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003611 .procname = "gc_thresh3",
3612 .maxlen = sizeof(int),
3613 .mode = 0644,
Matteo Croceeec48442019-07-18 15:58:50 -07003614 .extra1 = SYSCTL_ZERO,
3615 .extra2 = SYSCTL_INT_MAX,
Francesco Fusco555445c2013-07-24 10:39:06 +02003616 .proc_handler = proc_dointvec_minmax,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003617 },
Pavel Emelyanovc3bac5a2007-12-02 00:08:16 +11003618 {},
Linus Torvalds1da177e2005-04-16 15:20:36 -07003619 },
3620};
3621
3622int neigh_sysctl_register(struct net_device *dev, struct neigh_parms *p,
Jiri Pirko73af6142013-12-07 19:26:55 +01003623 proc_handler *handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003624{
Jiri Pirko1f9248e2013-12-07 19:26:53 +01003625 int i;
Pavel Emelyanov3c607bb2007-12-02 00:06:34 +11003626 struct neigh_sysctl_table *t;
Jiri Pirko1f9248e2013-12-07 19:26:53 +01003627 const char *dev_name_source;
Eric W. Biederman8f40a1f2012-04-19 13:38:03 +00003628 char neigh_path[ sizeof("net//neigh/") + IFNAMSIZ + IFNAMSIZ ];
Jiri Pirko73af6142013-12-07 19:26:55 +01003629 char *p_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003630
Pavel Emelyanov3c607bb2007-12-02 00:06:34 +11003631 t = kmemdup(&neigh_sysctl_template, sizeof(*t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003632 if (!t)
Pavel Emelyanov3c607bb2007-12-02 00:06:34 +11003633 goto err;
3634
Jiri Pirkob194c1f2014-02-21 14:52:57 +01003635 for (i = 0; i < NEIGH_VAR_GC_INTERVAL; i++) {
Jiri Pirko1f9248e2013-12-07 19:26:53 +01003636 t->neigh_vars[i].data += (long) p;
Jiri Pirkocb5b09c2013-12-07 19:26:54 +01003637 t->neigh_vars[i].extra1 = dev;
Jiri Pirko1d4c8c22013-12-07 19:26:56 +01003638 t->neigh_vars[i].extra2 = p;
Jiri Pirkocb5b09c2013-12-07 19:26:54 +01003639 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003640
3641 if (dev) {
3642 dev_name_source = dev->name;
Eric W. Biedermand12af672007-10-18 03:05:25 -07003643 /* Terminate the table early */
Eric Dumazet8b5c1712011-11-09 12:07:14 +00003644 memset(&t->neigh_vars[NEIGH_VAR_GC_INTERVAL], 0,
3645 sizeof(t->neigh_vars[NEIGH_VAR_GC_INTERVAL]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003646 } else {
Mathias Krause9ecf07a2014-07-12 22:36:44 +02003647 struct neigh_table *tbl = p->tbl;
Eric W. Biederman8f40a1f2012-04-19 13:38:03 +00003648 dev_name_source = "default";
Mathias Krause9ecf07a2014-07-12 22:36:44 +02003649 t->neigh_vars[NEIGH_VAR_GC_INTERVAL].data = &tbl->gc_interval;
3650 t->neigh_vars[NEIGH_VAR_GC_THRESH1].data = &tbl->gc_thresh1;
3651 t->neigh_vars[NEIGH_VAR_GC_THRESH2].data = &tbl->gc_thresh2;
3652 t->neigh_vars[NEIGH_VAR_GC_THRESH3].data = &tbl->gc_thresh3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003653 }
3654
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08003655 if (handler) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003656 /* RetransTime */
Eric Dumazet8b5c1712011-11-09 12:07:14 +00003657 t->neigh_vars[NEIGH_VAR_RETRANS_TIME].proc_handler = handler;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003658 /* ReachableTime */
Eric Dumazet8b5c1712011-11-09 12:07:14 +00003659 t->neigh_vars[NEIGH_VAR_BASE_REACHABLE_TIME].proc_handler = handler;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003660 /* RetransTime (in milliseconds)*/
Eric Dumazet8b5c1712011-11-09 12:07:14 +00003661 t->neigh_vars[NEIGH_VAR_RETRANS_TIME_MS].proc_handler = handler;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003662 /* ReachableTime (in milliseconds) */
Eric Dumazet8b5c1712011-11-09 12:07:14 +00003663 t->neigh_vars[NEIGH_VAR_BASE_REACHABLE_TIME_MS].proc_handler = handler;
Jean-Francois Remy4bf69802015-01-14 04:22:39 +01003664 } else {
3665 /* Those handlers will update p->reachable_time after
3666 * base_reachable_time(_ms) is set to ensure the new timer starts being
3667 * applied after the next neighbour update instead of waiting for
3668 * neigh_periodic_work to update its value (can be multiple minutes)
3669 * So any handler that replaces them should do this as well
3670 */
3671 /* ReachableTime */
3672 t->neigh_vars[NEIGH_VAR_BASE_REACHABLE_TIME].proc_handler =
3673 neigh_proc_base_reachable_time;
3674 /* ReachableTime (in milliseconds) */
3675 t->neigh_vars[NEIGH_VAR_BASE_REACHABLE_TIME_MS].proc_handler =
3676 neigh_proc_base_reachable_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003677 }
3678
Eric W. Biederman464dc802012-11-16 03:02:59 +00003679 /* Don't export sysctls to unprivileged users */
3680 if (neigh_parms_net(p)->user_ns != &init_user_ns)
3681 t->neigh_vars[0].procname = NULL;
3682
Jiri Pirko73af6142013-12-07 19:26:55 +01003683 switch (neigh_parms_family(p)) {
3684 case AF_INET:
3685 p_name = "ipv4";
3686 break;
3687 case AF_INET6:
3688 p_name = "ipv6";
3689 break;
3690 default:
3691 BUG();
3692 }
3693
Eric W. Biederman8f40a1f2012-04-19 13:38:03 +00003694 snprintf(neigh_path, sizeof(neigh_path), "net/%s/neigh/%s",
3695 p_name, dev_name_source);
Denis V. Lunev4ab438f2008-02-28 20:48:01 -08003696 t->sysctl_header =
Eric W. Biederman8f40a1f2012-04-19 13:38:03 +00003697 register_net_sysctl(neigh_parms_net(p), neigh_path, t->neigh_vars);
Pavel Emelyanov3c607bb2007-12-02 00:06:34 +11003698 if (!t->sysctl_header)
Eric W. Biederman8f40a1f2012-04-19 13:38:03 +00003699 goto free;
Pavel Emelyanov3c607bb2007-12-02 00:06:34 +11003700
Linus Torvalds1da177e2005-04-16 15:20:36 -07003701 p->sysctl_table = t;
3702 return 0;
3703
Pavel Emelyanov3c607bb2007-12-02 00:06:34 +11003704free:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003705 kfree(t);
Pavel Emelyanov3c607bb2007-12-02 00:06:34 +11003706err:
3707 return -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003708}
YOSHIFUJI Hideaki0a204502008-03-24 18:39:10 +09003709EXPORT_SYMBOL(neigh_sysctl_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003710
3711void neigh_sysctl_unregister(struct neigh_parms *p)
3712{
3713 if (p->sysctl_table) {
3714 struct neigh_sysctl_table *t = p->sysctl_table;
3715 p->sysctl_table = NULL;
Eric W. Biederman5dd3df12012-04-19 13:24:33 +00003716 unregister_net_sysctl_table(t->sysctl_header);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003717 kfree(t);
3718 }
3719}
YOSHIFUJI Hideaki0a204502008-03-24 18:39:10 +09003720EXPORT_SYMBOL(neigh_sysctl_unregister);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003721
3722#endif /* CONFIG_SYSCTL */
3723
Thomas Grafc8822a42007-03-22 11:50:06 -07003724static int __init neigh_init(void)
3725{
Florian Westphalb97bac62017-08-09 20:41:48 +02003726 rtnl_register(PF_UNSPEC, RTM_NEWNEIGH, neigh_add, NULL, 0);
3727 rtnl_register(PF_UNSPEC, RTM_DELNEIGH, neigh_delete, NULL, 0);
Roopa Prabhu82cbb5c2018-12-19 12:51:38 -08003728 rtnl_register(PF_UNSPEC, RTM_GETNEIGH, neigh_get, neigh_dump_info, 0);
Thomas Grafc8822a42007-03-22 11:50:06 -07003729
Greg Rosec7ac8672011-06-10 01:27:09 +00003730 rtnl_register(PF_UNSPEC, RTM_GETNEIGHTBL, NULL, neightbl_dump_info,
Florian Westphalb97bac62017-08-09 20:41:48 +02003731 0);
3732 rtnl_register(PF_UNSPEC, RTM_SETNEIGHTBL, neightbl_set, NULL, 0);
Thomas Grafc8822a42007-03-22 11:50:06 -07003733
3734 return 0;
3735}
3736
3737subsys_initcall(neigh_init);