Thomas Gleixner | 2874c5f | 2019-05-27 08:55:01 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | * Fixes: |
| 10 | * Vitaly E. Lavrov releasing NULL neighbor in neigh_add. |
| 11 | * Harald Welte Add neighbour cache statistics like rtstat |
| 12 | */ |
| 13 | |
Joe Perches | e005d19 | 2012-05-16 19:58:40 +0000 | [diff] [blame] | 14 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 15 | |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 16 | #include <linux/slab.h> |
Konstantin Khlebnikov | 85704cb | 2019-01-08 12:30:00 +0300 | [diff] [blame] | 17 | #include <linux/kmemleak.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include <linux/types.h> |
| 19 | #include <linux/kernel.h> |
| 20 | #include <linux/module.h> |
| 21 | #include <linux/socket.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | #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. Biederman | 457c4cb | 2007-09-12 12:01:34 +0200 | [diff] [blame] | 28 | #include <net/net_namespace.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | #include <net/neighbour.h> |
David Ahern | 4b2a2bf | 2019-05-01 18:18:42 -0700 | [diff] [blame] | 30 | #include <net/arp.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | #include <net/dst.h> |
| 32 | #include <net/sock.h> |
Tom Tucker | 8d71740 | 2006-07-30 20:43:36 -0700 | [diff] [blame] | 33 | #include <net/netevent.h> |
Thomas Graf | a14a49d | 2006-08-07 17:53:08 -0700 | [diff] [blame] | 34 | #include <net/netlink.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | #include <linux/rtnetlink.h> |
| 36 | #include <linux/random.h> |
Paulo Marques | 543537b | 2005-06-23 00:09:02 -0700 | [diff] [blame] | 37 | #include <linux/string.h> |
vignesh babu | c3609d5 | 2007-08-24 22:27:55 -0700 | [diff] [blame] | 38 | #include <linux/log2.h> |
Jiri Pirko | 1d4c8c2 | 2013-12-07 19:26:56 +0100 | [diff] [blame] | 39 | #include <linux/inetdevice.h> |
Jiri Pirko | bba2489 | 2013-12-07 19:26:57 +0100 | [diff] [blame] | 40 | #include <net/addrconf.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | |
Roopa Prabhu | 56dd18a | 2019-02-14 09:15:11 -0800 | [diff] [blame] | 42 | #include <trace/events/neigh.h> |
| 43 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | #define NEIGH_DEBUG 1 |
Joe Perches | d5d427c | 2013-04-15 15:17:19 +0000 | [diff] [blame] | 45 | #define neigh_dbg(level, fmt, ...) \ |
| 46 | do { \ |
| 47 | if (level <= NEIGH_DEBUG) \ |
| 48 | pr_debug(fmt, ##__VA_ARGS__); \ |
| 49 | } while (0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | |
| 51 | #define PNEIGH_HASHMASK 0xF |
| 52 | |
Kees Cook | e99e88a | 2017-10-16 14:43:17 -0700 | [diff] [blame] | 53 | static void neigh_timer_handler(struct timer_list *t); |
Roopa Prabhu | 7b8f7a4 | 2017-03-19 22:01:28 -0700 | [diff] [blame] | 54 | static void __neigh_notify(struct neighbour *n, int type, int flags, |
| 55 | u32 pid); |
| 56 | static void neigh_update_notify(struct neighbour *neigh, u32 nlmsg_pid); |
Wolfgang Bumiller | 53b76cd | 2018-04-12 10:46:55 +0200 | [diff] [blame] | 57 | static int pneigh_ifdown_and_unlock(struct neigh_table *tbl, |
| 58 | struct net_device *dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | |
Amos Waterland | 45fc3b1 | 2005-09-24 16:53:16 -0700 | [diff] [blame] | 60 | #ifdef CONFIG_PROC_FS |
Christoph Hellwig | 71a5053 | 2018-04-15 10:16:41 +0200 | [diff] [blame] | 61 | static const struct seq_operations neigh_stat_seq_ops; |
Amos Waterland | 45fc3b1 | 2005-09-24 16:53:16 -0700 | [diff] [blame] | 62 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | |
| 64 | /* |
| 65 | Neighbour hash table buckets are protected with rwlock tbl->lock. |
| 66 | |
| 67 | - All the scans/updates to hash buckets MUST be made under this lock. |
| 68 | - NOTHING clever should be made under this lock: no callbacks |
| 69 | to protocol backends, no attempts to send something to network. |
| 70 | It will result in deadlocks, if backend/driver wants to use neighbour |
| 71 | cache. |
| 72 | - If the entry requires some non-trivial actions, increase |
| 73 | its reference count and release table lock. |
| 74 | |
| 75 | Neighbour entries are protected: |
| 76 | - with reference count. |
| 77 | - with rwlock neigh->lock |
| 78 | |
| 79 | Reference count prevents destruction. |
| 80 | |
| 81 | neigh->lock mainly serializes ll address data and its validity state. |
| 82 | However, the same lock is used to protect another entry fields: |
| 83 | - timer |
| 84 | - resolution queue |
| 85 | |
| 86 | Again, nothing clever shall be made under neigh->lock, |
| 87 | the most complicated procedure, which we allow is dev->hard_header. |
| 88 | It is supposed, that dev->hard_header is simplistic and does |
| 89 | not make callbacks to neighbour tables. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | */ |
| 91 | |
David S. Miller | 8f40b16 | 2011-07-17 13:34:11 -0700 | [diff] [blame] | 92 | static int neigh_blackhole(struct neighbour *neigh, struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | { |
| 94 | kfree_skb(skb); |
| 95 | return -ENETDOWN; |
| 96 | } |
| 97 | |
Thomas Graf | 4f49455 | 2007-08-08 23:12:36 -0700 | [diff] [blame] | 98 | static void neigh_cleanup_and_release(struct neighbour *neigh) |
| 99 | { |
Roopa Prabhu | 56dd18a | 2019-02-14 09:15:11 -0800 | [diff] [blame] | 100 | trace_neigh_cleanup_and_release(neigh, 0); |
Roopa Prabhu | 7b8f7a4 | 2017-03-19 22:01:28 -0700 | [diff] [blame] | 101 | __neigh_notify(neigh, RTM_DELNEIGH, 0, 0); |
Ido Schimmel | 53f800e | 2016-12-23 09:32:48 +0100 | [diff] [blame] | 102 | call_netevent_notifiers(NETEVENT_NEIGH_UPDATE, neigh); |
Thomas Graf | 4f49455 | 2007-08-08 23:12:36 -0700 | [diff] [blame] | 103 | neigh_release(neigh); |
| 104 | } |
| 105 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | /* |
| 107 | * It is random distribution in the interval (1/2)*base...(3/2)*base. |
| 108 | * It corresponds to default IPv6 settings and is not overridable, |
| 109 | * because it is really reasonable choice. |
| 110 | */ |
| 111 | |
| 112 | unsigned long neigh_rand_reach_time(unsigned long base) |
| 113 | { |
Aruna-Hewapathirane | 63862b5 | 2014-01-11 07:15:59 -0500 | [diff] [blame] | 114 | return base ? (prandom_u32() % base) + (base >> 1) : 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | } |
YOSHIFUJI Hideaki | 0a20450 | 2008-03-24 18:39:10 +0900 | [diff] [blame] | 116 | EXPORT_SYMBOL(neigh_rand_reach_time); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | |
David Ahern | 5895631 | 2018-12-07 12:24:57 -0800 | [diff] [blame] | 118 | static void neigh_mark_dead(struct neighbour *n) |
| 119 | { |
| 120 | n->dead = 1; |
| 121 | if (!list_empty(&n->gc_list)) { |
| 122 | list_del_init(&n->gc_list); |
| 123 | atomic_dec(&n->tbl->gc_entries); |
| 124 | } |
| 125 | } |
| 126 | |
David Ahern | 9c29a2f | 2018-12-11 18:57:21 -0700 | [diff] [blame] | 127 | static void neigh_update_gc_list(struct neighbour *n) |
David Ahern | 5895631 | 2018-12-07 12:24:57 -0800 | [diff] [blame] | 128 | { |
David Ahern | e997f8a | 2018-12-11 18:57:25 -0700 | [diff] [blame] | 129 | bool on_gc_list, exempt_from_gc; |
David Ahern | 5895631 | 2018-12-07 12:24:57 -0800 | [diff] [blame] | 130 | |
David Ahern | 9c29a2f | 2018-12-11 18:57:21 -0700 | [diff] [blame] | 131 | write_lock_bh(&n->tbl->lock); |
| 132 | write_lock(&n->lock); |
David Ahern | 5895631 | 2018-12-07 12:24:57 -0800 | [diff] [blame] | 133 | |
Chinmay Agarwal | eefb45e | 2021-04-22 01:12:22 +0530 | [diff] [blame] | 134 | if (n->dead) |
| 135 | goto out; |
| 136 | |
David Ahern | e997f8a | 2018-12-11 18:57:25 -0700 | [diff] [blame] | 137 | /* remove from the gc list if new state is permanent or if neighbor |
| 138 | * is externally learned; otherwise entry should be on the gc list |
David Ahern | 5895631 | 2018-12-07 12:24:57 -0800 | [diff] [blame] | 139 | */ |
David Ahern | e997f8a | 2018-12-11 18:57:25 -0700 | [diff] [blame] | 140 | exempt_from_gc = n->nud_state & NUD_PERMANENT || |
| 141 | n->flags & NTF_EXT_LEARNED; |
David Ahern | 9c29a2f | 2018-12-11 18:57:21 -0700 | [diff] [blame] | 142 | on_gc_list = !list_empty(&n->gc_list); |
David Ahern | 8cc196d | 2018-12-10 13:54:07 -0800 | [diff] [blame] | 143 | |
David Ahern | e997f8a | 2018-12-11 18:57:25 -0700 | [diff] [blame] | 144 | if (exempt_from_gc && on_gc_list) { |
David Ahern | 9c29a2f | 2018-12-11 18:57:21 -0700 | [diff] [blame] | 145 | list_del_init(&n->gc_list); |
David Ahern | 5895631 | 2018-12-07 12:24:57 -0800 | [diff] [blame] | 146 | atomic_dec(&n->tbl->gc_entries); |
David Ahern | e997f8a | 2018-12-11 18:57:25 -0700 | [diff] [blame] | 147 | } else if (!exempt_from_gc && !on_gc_list) { |
David Ahern | 5895631 | 2018-12-07 12:24:57 -0800 | [diff] [blame] | 148 | /* add entries to the tail; cleaning removes from the front */ |
| 149 | list_add_tail(&n->gc_list, &n->tbl->gc_list); |
| 150 | atomic_inc(&n->tbl->gc_entries); |
| 151 | } |
David Ahern | 9c29a2f | 2018-12-11 18:57:21 -0700 | [diff] [blame] | 152 | |
Chinmay Agarwal | eefb45e | 2021-04-22 01:12:22 +0530 | [diff] [blame] | 153 | out: |
David Ahern | 9c29a2f | 2018-12-11 18:57:21 -0700 | [diff] [blame] | 154 | write_unlock(&n->lock); |
| 155 | write_unlock_bh(&n->tbl->lock); |
David Ahern | 5895631 | 2018-12-07 12:24:57 -0800 | [diff] [blame] | 156 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | |
David Ahern | e997f8a | 2018-12-11 18:57:25 -0700 | [diff] [blame] | 158 | static bool neigh_update_ext_learned(struct neighbour *neigh, u32 flags, |
David Ahern | 526f1b5 | 2018-12-11 18:57:24 -0700 | [diff] [blame] | 159 | int *notify) |
| 160 | { |
David Ahern | e997f8a | 2018-12-11 18:57:25 -0700 | [diff] [blame] | 161 | bool rc = false; |
David Ahern | 526f1b5 | 2018-12-11 18:57:24 -0700 | [diff] [blame] | 162 | u8 ndm_flags; |
| 163 | |
| 164 | if (!(flags & NEIGH_UPDATE_F_ADMIN)) |
David Ahern | e997f8a | 2018-12-11 18:57:25 -0700 | [diff] [blame] | 165 | return rc; |
David Ahern | 526f1b5 | 2018-12-11 18:57:24 -0700 | [diff] [blame] | 166 | |
| 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 Ahern | e997f8a | 2018-12-11 18:57:25 -0700 | [diff] [blame] | 173 | rc = true; |
David Ahern | 526f1b5 | 2018-12-11 18:57:24 -0700 | [diff] [blame] | 174 | *notify = 1; |
| 175 | } |
David Ahern | e997f8a | 2018-12-11 18:57:25 -0700 | [diff] [blame] | 176 | |
| 177 | return rc; |
David Ahern | 526f1b5 | 2018-12-11 18:57:24 -0700 | [diff] [blame] | 178 | } |
| 179 | |
David Ahern | 7e6f182 | 2018-12-11 18:57:23 -0700 | [diff] [blame] | 180 | static bool neigh_del(struct neighbour *n, struct neighbour __rcu **np, |
| 181 | struct neigh_table *tbl) |
Sowmini Varadhan | 5071034 | 2017-06-02 09:01:49 -0700 | [diff] [blame] | 182 | { |
| 183 | bool retval = false; |
| 184 | |
| 185 | write_lock(&n->lock); |
David Ahern | 7e6f182 | 2018-12-11 18:57:23 -0700 | [diff] [blame] | 186 | if (refcount_read(&n->refcnt) == 1) { |
Sowmini Varadhan | 5071034 | 2017-06-02 09:01:49 -0700 | [diff] [blame] | 187 | struct neighbour *neigh; |
| 188 | |
| 189 | neigh = rcu_dereference_protected(n->next, |
| 190 | lockdep_is_held(&tbl->lock)); |
| 191 | rcu_assign_pointer(*np, neigh); |
David Ahern | 5895631 | 2018-12-07 12:24:57 -0800 | [diff] [blame] | 192 | neigh_mark_dead(n); |
Sowmini Varadhan | 5071034 | 2017-06-02 09:01:49 -0700 | [diff] [blame] | 193 | retval = true; |
| 194 | } |
| 195 | write_unlock(&n->lock); |
| 196 | if (retval) |
| 197 | neigh_cleanup_and_release(n); |
| 198 | return retval; |
| 199 | } |
| 200 | |
| 201 | bool 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 Ahern | 7e6f182 | 2018-12-11 18:57:23 -0700 | [diff] [blame] | 218 | return neigh_del(n, np, tbl); |
Sowmini Varadhan | 5071034 | 2017-06-02 09:01:49 -0700 | [diff] [blame] | 219 | np = &n->next; |
| 220 | } |
| 221 | return false; |
| 222 | } |
| 223 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | static int neigh_forced_gc(struct neigh_table *tbl) |
| 225 | { |
David Ahern | 5895631 | 2018-12-07 12:24:57 -0800 | [diff] [blame] | 226 | int max_clean = atomic_read(&tbl->gc_entries) - tbl->gc_thresh2; |
| 227 | unsigned long tref = jiffies - 5 * HZ; |
David Ahern | 5895631 | 2018-12-07 12:24:57 -0800 | [diff] [blame] | 228 | struct neighbour *n, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | int shrunk = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | |
| 231 | NEIGH_CACHE_STAT_INC(tbl, forced_gc_runs); |
| 232 | |
| 233 | write_lock_bh(&tbl->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | |
David Ahern | 5895631 | 2018-12-07 12:24:57 -0800 | [diff] [blame] | 235 | 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 Ahern | 758a7f0 | 2018-12-11 18:57:22 -0700 | [diff] [blame] | 240 | if ((n->nud_state == NUD_FAILED) || |
David Ahern | 7a6b1ab | 2021-06-07 11:35:30 -0600 | [diff] [blame] | 241 | (n->nud_state == NUD_NOARP) || |
Jeff Dike | 8cf8821 | 2020-11-12 20:58:15 -0500 | [diff] [blame] | 242 | (tbl->is_multicast && |
| 243 | tbl->is_multicast(n->primary_key)) || |
David Ahern | e997f8a | 2018-12-11 18:57:25 -0700 | [diff] [blame] | 244 | time_after(tref, n->updated)) |
David Ahern | 5895631 | 2018-12-07 12:24:57 -0800 | [diff] [blame] | 245 | remove = true; |
| 246 | write_unlock(&n->lock); |
| 247 | |
| 248 | if (remove && neigh_remove_one(n, tbl)) |
| 249 | shrunk++; |
| 250 | if (shrunk >= max_clean) |
| 251 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | } |
| 253 | } |
| 254 | |
| 255 | tbl->last_flush = jiffies; |
| 256 | |
| 257 | write_unlock_bh(&tbl->lock); |
| 258 | |
| 259 | return shrunk; |
| 260 | } |
| 261 | |
Pavel Emelyanov | a43d899 | 2007-12-20 15:49:05 -0800 | [diff] [blame] | 262 | static void neigh_add_timer(struct neighbour *n, unsigned long when) |
| 263 | { |
| 264 | neigh_hold(n); |
| 265 | if (unlikely(mod_timer(&n->timer, when))) { |
| 266 | printk("NEIGH: BUG, double timer add, state is %x\n", |
| 267 | n->nud_state); |
| 268 | dump_stack(); |
| 269 | } |
| 270 | } |
| 271 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | static int neigh_del_timer(struct neighbour *n) |
| 273 | { |
| 274 | if ((n->nud_state & NUD_IN_TIMER) && |
| 275 | del_timer(&n->timer)) { |
| 276 | neigh_release(n); |
| 277 | return 1; |
| 278 | } |
| 279 | return 0; |
| 280 | } |
| 281 | |
| 282 | static void pneigh_queue_purge(struct sk_buff_head *list) |
| 283 | { |
| 284 | struct sk_buff *skb; |
| 285 | |
| 286 | while ((skb = skb_dequeue(list)) != NULL) { |
| 287 | dev_put(skb->dev); |
| 288 | kfree_skb(skb); |
| 289 | } |
| 290 | } |
| 291 | |
David Ahern | 859bd2e | 2018-10-11 20:33:49 -0700 | [diff] [blame] | 292 | static void neigh_flush_dev(struct neigh_table *tbl, struct net_device *dev, |
| 293 | bool skip_perm) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | { |
| 295 | int i; |
Eric Dumazet | d6bf781 | 2010-10-04 06:15:44 +0000 | [diff] [blame] | 296 | struct neigh_hash_table *nht; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | |
Eric Dumazet | d6bf781 | 2010-10-04 06:15:44 +0000 | [diff] [blame] | 298 | nht = rcu_dereference_protected(tbl->nht, |
| 299 | lockdep_is_held(&tbl->lock)); |
| 300 | |
David S. Miller | cd08933 | 2011-07-11 01:28:12 -0700 | [diff] [blame] | 301 | for (i = 0; i < (1 << nht->hash_shift); i++) { |
Eric Dumazet | 767e97e | 2010-10-06 17:49:21 -0700 | [diff] [blame] | 302 | struct neighbour *n; |
| 303 | struct neighbour __rcu **np = &nht->hash_buckets[i]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | |
Eric Dumazet | 767e97e | 2010-10-06 17:49:21 -0700 | [diff] [blame] | 305 | while ((n = rcu_dereference_protected(*np, |
| 306 | lockdep_is_held(&tbl->lock))) != NULL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | if (dev && n->dev != dev) { |
| 308 | np = &n->next; |
| 309 | continue; |
| 310 | } |
David Ahern | 859bd2e | 2018-10-11 20:33:49 -0700 | [diff] [blame] | 311 | if (skip_perm && n->nud_state & NUD_PERMANENT) { |
| 312 | np = &n->next; |
| 313 | continue; |
| 314 | } |
Eric Dumazet | 767e97e | 2010-10-06 17:49:21 -0700 | [diff] [blame] | 315 | rcu_assign_pointer(*np, |
| 316 | rcu_dereference_protected(n->next, |
| 317 | lockdep_is_held(&tbl->lock))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 318 | write_lock(&n->lock); |
| 319 | neigh_del_timer(n); |
David Ahern | 5895631 | 2018-12-07 12:24:57 -0800 | [diff] [blame] | 320 | neigh_mark_dead(n); |
Reshetova, Elena | 9f23743 | 2017-06-30 13:07:55 +0300 | [diff] [blame] | 321 | if (refcount_read(&n->refcnt) != 1) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 322 | /* The most unpleasant situation. |
| 323 | We must destroy neighbour entry, |
| 324 | but someone still uses it. |
| 325 | |
| 326 | The destroy will be delayed until |
| 327 | the last user releases us, but |
| 328 | we must kill timers etc. and move |
| 329 | it to safe state. |
| 330 | */ |
Eric Dumazet | c9ab4d8 | 2013-06-28 02:37:42 -0700 | [diff] [blame] | 331 | __skb_queue_purge(&n->arp_queue); |
Eric Dumazet | 8b5c171 | 2011-11-09 12:07:14 +0000 | [diff] [blame] | 332 | n->arp_queue_len_bytes = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | n->output = neigh_blackhole; |
| 334 | if (n->nud_state & NUD_VALID) |
| 335 | n->nud_state = NUD_NOARP; |
| 336 | else |
| 337 | n->nud_state = NUD_NONE; |
Joe Perches | d5d427c | 2013-04-15 15:17:19 +0000 | [diff] [blame] | 338 | neigh_dbg(2, "neigh %p is stray\n", n); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 339 | } |
| 340 | write_unlock(&n->lock); |
Thomas Graf | 4f49455 | 2007-08-08 23:12:36 -0700 | [diff] [blame] | 341 | neigh_cleanup_and_release(n); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 342 | } |
| 343 | } |
Herbert Xu | 49636bb | 2005-10-23 17:18:00 +1000 | [diff] [blame] | 344 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 345 | |
Herbert Xu | 49636bb | 2005-10-23 17:18:00 +1000 | [diff] [blame] | 346 | void neigh_changeaddr(struct neigh_table *tbl, struct net_device *dev) |
| 347 | { |
| 348 | write_lock_bh(&tbl->lock); |
David Ahern | 859bd2e | 2018-10-11 20:33:49 -0700 | [diff] [blame] | 349 | neigh_flush_dev(tbl, dev, false); |
Herbert Xu | 49636bb | 2005-10-23 17:18:00 +1000 | [diff] [blame] | 350 | write_unlock_bh(&tbl->lock); |
| 351 | } |
YOSHIFUJI Hideaki | 0a20450 | 2008-03-24 18:39:10 +0900 | [diff] [blame] | 352 | EXPORT_SYMBOL(neigh_changeaddr); |
Herbert Xu | 49636bb | 2005-10-23 17:18:00 +1000 | [diff] [blame] | 353 | |
David Ahern | 859bd2e | 2018-10-11 20:33:49 -0700 | [diff] [blame] | 354 | static int __neigh_ifdown(struct neigh_table *tbl, struct net_device *dev, |
| 355 | bool skip_perm) |
Herbert Xu | 49636bb | 2005-10-23 17:18:00 +1000 | [diff] [blame] | 356 | { |
| 357 | write_lock_bh(&tbl->lock); |
David Ahern | 859bd2e | 2018-10-11 20:33:49 -0700 | [diff] [blame] | 358 | neigh_flush_dev(tbl, dev, skip_perm); |
Wolfgang Bumiller | 53b76cd | 2018-04-12 10:46:55 +0200 | [diff] [blame] | 359 | pneigh_ifdown_and_unlock(tbl, dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 360 | |
| 361 | del_timer_sync(&tbl->proxy_timer); |
| 362 | pneigh_queue_purge(&tbl->proxy_queue); |
| 363 | return 0; |
| 364 | } |
David Ahern | 859bd2e | 2018-10-11 20:33:49 -0700 | [diff] [blame] | 365 | |
| 366 | int neigh_carrier_down(struct neigh_table *tbl, struct net_device *dev) |
| 367 | { |
| 368 | __neigh_ifdown(tbl, dev, true); |
| 369 | return 0; |
| 370 | } |
| 371 | EXPORT_SYMBOL(neigh_carrier_down); |
| 372 | |
| 373 | int neigh_ifdown(struct neigh_table *tbl, struct net_device *dev) |
| 374 | { |
| 375 | __neigh_ifdown(tbl, dev, false); |
| 376 | return 0; |
| 377 | } |
YOSHIFUJI Hideaki | 0a20450 | 2008-03-24 18:39:10 +0900 | [diff] [blame] | 378 | EXPORT_SYMBOL(neigh_ifdown); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 379 | |
David Ahern | 5895631 | 2018-12-07 12:24:57 -0800 | [diff] [blame] | 380 | static struct neighbour *neigh_alloc(struct neigh_table *tbl, |
| 381 | struct net_device *dev, |
David Ahern | e997f8a | 2018-12-11 18:57:25 -0700 | [diff] [blame] | 382 | bool exempt_from_gc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 383 | { |
| 384 | struct neighbour *n = NULL; |
| 385 | unsigned long now = jiffies; |
| 386 | int entries; |
| 387 | |
David Ahern | e997f8a | 2018-12-11 18:57:25 -0700 | [diff] [blame] | 388 | if (exempt_from_gc) |
David Ahern | 5895631 | 2018-12-07 12:24:57 -0800 | [diff] [blame] | 389 | goto do_alloc; |
| 390 | |
| 391 | entries = atomic_inc_return(&tbl->gc_entries) - 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 | if (entries >= tbl->gc_thresh3 || |
| 393 | (entries >= tbl->gc_thresh2 && |
| 394 | time_after(now, tbl->last_flush + 5 * HZ))) { |
| 395 | if (!neigh_forced_gc(tbl) && |
Rick Jones | fb81139 | 2015-08-07 11:10:37 -0700 | [diff] [blame] | 396 | entries >= tbl->gc_thresh3) { |
| 397 | net_info_ratelimited("%s: neighbor table overflow!\n", |
| 398 | tbl->id); |
| 399 | NEIGH_CACHE_STAT_INC(tbl, table_fulls); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 400 | goto out_entries; |
Rick Jones | fb81139 | 2015-08-07 11:10:37 -0700 | [diff] [blame] | 401 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 402 | } |
| 403 | |
David Ahern | 5895631 | 2018-12-07 12:24:57 -0800 | [diff] [blame] | 404 | do_alloc: |
YOSHIFUJI Hideaki / 吉藤英明 | 08433ef | 2013-01-24 00:44:23 +0000 | [diff] [blame] | 405 | n = kzalloc(tbl->entry_size + dev->neigh_priv_len, GFP_ATOMIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 406 | if (!n) |
| 407 | goto out_entries; |
| 408 | |
Eric Dumazet | c9ab4d8 | 2013-06-28 02:37:42 -0700 | [diff] [blame] | 409 | __skb_queue_head_init(&n->arp_queue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 410 | rwlock_init(&n->lock); |
Eric Dumazet | 0ed8ddf | 2010-10-07 10:44:07 +0000 | [diff] [blame] | 411 | seqlock_init(&n->ha_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 412 | n->updated = n->used = now; |
| 413 | n->nud_state = NUD_NONE; |
| 414 | n->output = neigh_blackhole; |
David S. Miller | f6b72b62 | 2011-07-14 07:53:20 -0700 | [diff] [blame] | 415 | seqlock_init(&n->hh.hh_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 416 | n->parms = neigh_parms_clone(&tbl->parms); |
Kees Cook | e99e88a | 2017-10-16 14:43:17 -0700 | [diff] [blame] | 417 | timer_setup(&n->timer, neigh_timer_handler, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 418 | |
| 419 | NEIGH_CACHE_STAT_INC(tbl, allocs); |
| 420 | n->tbl = tbl; |
Reshetova, Elena | 9f23743 | 2017-06-30 13:07:55 +0300 | [diff] [blame] | 421 | refcount_set(&n->refcnt, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 422 | n->dead = 1; |
David Ahern | 8cc196d | 2018-12-10 13:54:07 -0800 | [diff] [blame] | 423 | INIT_LIST_HEAD(&n->gc_list); |
David Ahern | 5895631 | 2018-12-07 12:24:57 -0800 | [diff] [blame] | 424 | |
| 425 | atomic_inc(&tbl->entries); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 426 | out: |
| 427 | return n; |
| 428 | |
| 429 | out_entries: |
David Ahern | e997f8a | 2018-12-11 18:57:25 -0700 | [diff] [blame] | 430 | if (!exempt_from_gc) |
David Ahern | 5895631 | 2018-12-07 12:24:57 -0800 | [diff] [blame] | 431 | atomic_dec(&tbl->gc_entries); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 432 | goto out; |
| 433 | } |
| 434 | |
David S. Miller | 2c2aba6 | 2011-12-28 15:06:58 -0500 | [diff] [blame] | 435 | static void neigh_get_hash_rnd(u32 *x) |
| 436 | { |
Jason A. Donenfeld | b3d0f78 | 2017-06-07 23:00:05 -0400 | [diff] [blame] | 437 | *x = get_random_u32() | 1; |
David S. Miller | 2c2aba6 | 2011-12-28 15:06:58 -0500 | [diff] [blame] | 438 | } |
| 439 | |
David S. Miller | cd08933 | 2011-07-11 01:28:12 -0700 | [diff] [blame] | 440 | static struct neigh_hash_table *neigh_hash_alloc(unsigned int shift) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 441 | { |
David S. Miller | cd08933 | 2011-07-11 01:28:12 -0700 | [diff] [blame] | 442 | size_t size = (1 << shift) * sizeof(struct neighbour *); |
Eric Dumazet | d6bf781 | 2010-10-04 06:15:44 +0000 | [diff] [blame] | 443 | struct neigh_hash_table *ret; |
Eric Dumazet | 6193d2b | 2011-01-19 22:02:47 +0000 | [diff] [blame] | 444 | struct neighbour __rcu **buckets; |
David S. Miller | 2c2aba6 | 2011-12-28 15:06:58 -0500 | [diff] [blame] | 445 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 446 | |
Eric Dumazet | d6bf781 | 2010-10-04 06:15:44 +0000 | [diff] [blame] | 447 | ret = kmalloc(sizeof(*ret), GFP_ATOMIC); |
| 448 | if (!ret) |
| 449 | return NULL; |
Konstantin Khlebnikov | 85704cb | 2019-01-08 12:30:00 +0300 | [diff] [blame] | 450 | if (size <= PAGE_SIZE) { |
Eric Dumazet | d6bf781 | 2010-10-04 06:15:44 +0000 | [diff] [blame] | 451 | buckets = kzalloc(size, GFP_ATOMIC); |
Konstantin Khlebnikov | 85704cb | 2019-01-08 12:30:00 +0300 | [diff] [blame] | 452 | } else { |
Eric Dumazet | 6193d2b | 2011-01-19 22:02:47 +0000 | [diff] [blame] | 453 | buckets = (struct neighbour __rcu **) |
Eric Dumazet | d6bf781 | 2010-10-04 06:15:44 +0000 | [diff] [blame] | 454 | __get_free_pages(GFP_ATOMIC | __GFP_ZERO, |
| 455 | get_order(size)); |
Konstantin Khlebnikov | 01b833a | 2019-01-14 13:38:43 +0300 | [diff] [blame] | 456 | kmemleak_alloc(buckets, size, 1, GFP_ATOMIC); |
Konstantin Khlebnikov | 85704cb | 2019-01-08 12:30:00 +0300 | [diff] [blame] | 457 | } |
Eric Dumazet | d6bf781 | 2010-10-04 06:15:44 +0000 | [diff] [blame] | 458 | if (!buckets) { |
| 459 | kfree(ret); |
| 460 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 461 | } |
Eric Dumazet | 6193d2b | 2011-01-19 22:02:47 +0000 | [diff] [blame] | 462 | ret->hash_buckets = buckets; |
David S. Miller | cd08933 | 2011-07-11 01:28:12 -0700 | [diff] [blame] | 463 | ret->hash_shift = shift; |
David S. Miller | 2c2aba6 | 2011-12-28 15:06:58 -0500 | [diff] [blame] | 464 | for (i = 0; i < NEIGH_NUM_HASH_RND; i++) |
| 465 | neigh_get_hash_rnd(&ret->hash_rnd[i]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 466 | return ret; |
| 467 | } |
| 468 | |
Eric Dumazet | d6bf781 | 2010-10-04 06:15:44 +0000 | [diff] [blame] | 469 | static void neigh_hash_free_rcu(struct rcu_head *head) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 470 | { |
Eric Dumazet | d6bf781 | 2010-10-04 06:15:44 +0000 | [diff] [blame] | 471 | struct neigh_hash_table *nht = container_of(head, |
| 472 | struct neigh_hash_table, |
| 473 | rcu); |
David S. Miller | cd08933 | 2011-07-11 01:28:12 -0700 | [diff] [blame] | 474 | size_t size = (1 << nht->hash_shift) * sizeof(struct neighbour *); |
Eric Dumazet | 6193d2b | 2011-01-19 22:02:47 +0000 | [diff] [blame] | 475 | struct neighbour __rcu **buckets = nht->hash_buckets; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 476 | |
Konstantin Khlebnikov | 85704cb | 2019-01-08 12:30:00 +0300 | [diff] [blame] | 477 | if (size <= PAGE_SIZE) { |
Eric Dumazet | d6bf781 | 2010-10-04 06:15:44 +0000 | [diff] [blame] | 478 | kfree(buckets); |
Konstantin Khlebnikov | 85704cb | 2019-01-08 12:30:00 +0300 | [diff] [blame] | 479 | } else { |
| 480 | kmemleak_free(buckets); |
Eric Dumazet | d6bf781 | 2010-10-04 06:15:44 +0000 | [diff] [blame] | 481 | free_pages((unsigned long)buckets, get_order(size)); |
Konstantin Khlebnikov | 85704cb | 2019-01-08 12:30:00 +0300 | [diff] [blame] | 482 | } |
Eric Dumazet | d6bf781 | 2010-10-04 06:15:44 +0000 | [diff] [blame] | 483 | kfree(nht); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 484 | } |
| 485 | |
Eric Dumazet | d6bf781 | 2010-10-04 06:15:44 +0000 | [diff] [blame] | 486 | static struct neigh_hash_table *neigh_hash_grow(struct neigh_table *tbl, |
David S. Miller | cd08933 | 2011-07-11 01:28:12 -0700 | [diff] [blame] | 487 | unsigned long new_shift) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 488 | { |
Eric Dumazet | d6bf781 | 2010-10-04 06:15:44 +0000 | [diff] [blame] | 489 | unsigned int i, hash; |
| 490 | struct neigh_hash_table *new_nht, *old_nht; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 491 | |
| 492 | NEIGH_CACHE_STAT_INC(tbl, hash_grows); |
| 493 | |
Eric Dumazet | d6bf781 | 2010-10-04 06:15:44 +0000 | [diff] [blame] | 494 | old_nht = rcu_dereference_protected(tbl->nht, |
| 495 | lockdep_is_held(&tbl->lock)); |
David S. Miller | cd08933 | 2011-07-11 01:28:12 -0700 | [diff] [blame] | 496 | new_nht = neigh_hash_alloc(new_shift); |
Eric Dumazet | d6bf781 | 2010-10-04 06:15:44 +0000 | [diff] [blame] | 497 | if (!new_nht) |
| 498 | return old_nht; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 499 | |
David S. Miller | cd08933 | 2011-07-11 01:28:12 -0700 | [diff] [blame] | 500 | for (i = 0; i < (1 << old_nht->hash_shift); i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 501 | struct neighbour *n, *next; |
| 502 | |
Eric Dumazet | 767e97e | 2010-10-06 17:49:21 -0700 | [diff] [blame] | 503 | for (n = rcu_dereference_protected(old_nht->hash_buckets[i], |
| 504 | lockdep_is_held(&tbl->lock)); |
Eric Dumazet | d6bf781 | 2010-10-04 06:15:44 +0000 | [diff] [blame] | 505 | n != NULL; |
| 506 | n = next) { |
| 507 | hash = tbl->hash(n->primary_key, n->dev, |
| 508 | new_nht->hash_rnd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 509 | |
David S. Miller | cd08933 | 2011-07-11 01:28:12 -0700 | [diff] [blame] | 510 | hash >>= (32 - new_nht->hash_shift); |
Eric Dumazet | 767e97e | 2010-10-06 17:49:21 -0700 | [diff] [blame] | 511 | next = rcu_dereference_protected(n->next, |
| 512 | lockdep_is_held(&tbl->lock)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 513 | |
Eric Dumazet | 767e97e | 2010-10-06 17:49:21 -0700 | [diff] [blame] | 514 | rcu_assign_pointer(n->next, |
| 515 | rcu_dereference_protected( |
| 516 | new_nht->hash_buckets[hash], |
| 517 | lockdep_is_held(&tbl->lock))); |
| 518 | rcu_assign_pointer(new_nht->hash_buckets[hash], n); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 519 | } |
| 520 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 521 | |
Eric Dumazet | d6bf781 | 2010-10-04 06:15:44 +0000 | [diff] [blame] | 522 | rcu_assign_pointer(tbl->nht, new_nht); |
| 523 | call_rcu(&old_nht->rcu, neigh_hash_free_rcu); |
| 524 | return new_nht; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 525 | } |
| 526 | |
| 527 | struct neighbour *neigh_lookup(struct neigh_table *tbl, const void *pkey, |
| 528 | struct net_device *dev) |
| 529 | { |
| 530 | struct neighbour *n; |
YOSHIFUJI Hideaki | 4ec93ed | 2007-02-09 23:24:36 +0900 | [diff] [blame] | 531 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 532 | NEIGH_CACHE_STAT_INC(tbl, lookups); |
| 533 | |
Eric Dumazet | d6bf781 | 2010-10-04 06:15:44 +0000 | [diff] [blame] | 534 | rcu_read_lock_bh(); |
Eric W. Biederman | 60395a2 | 2015-03-03 17:10:44 -0600 | [diff] [blame] | 535 | n = __neigh_lookup_noref(tbl, pkey, dev); |
| 536 | if (n) { |
Reshetova, Elena | 9f23743 | 2017-06-30 13:07:55 +0300 | [diff] [blame] | 537 | if (!refcount_inc_not_zero(&n->refcnt)) |
Eric W. Biederman | 60395a2 | 2015-03-03 17:10:44 -0600 | [diff] [blame] | 538 | n = NULL; |
| 539 | NEIGH_CACHE_STAT_INC(tbl, hits); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 540 | } |
Eric Dumazet | 767e97e | 2010-10-06 17:49:21 -0700 | [diff] [blame] | 541 | |
Eric Dumazet | d6bf781 | 2010-10-04 06:15:44 +0000 | [diff] [blame] | 542 | rcu_read_unlock_bh(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 543 | return n; |
| 544 | } |
YOSHIFUJI Hideaki | 0a20450 | 2008-03-24 18:39:10 +0900 | [diff] [blame] | 545 | EXPORT_SYMBOL(neigh_lookup); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 546 | |
Eric W. Biederman | 426b530 | 2008-01-24 00:13:18 -0800 | [diff] [blame] | 547 | struct neighbour *neigh_lookup_nodev(struct neigh_table *tbl, struct net *net, |
| 548 | const void *pkey) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 549 | { |
| 550 | struct neighbour *n; |
Alexey Dobriyan | 01ccdf1 | 2017-09-23 23:03:04 +0300 | [diff] [blame] | 551 | unsigned int key_len = tbl->key_len; |
Pavel Emelyanov | bc4bf5f | 2008-02-23 19:57:02 -0800 | [diff] [blame] | 552 | u32 hash_val; |
Eric Dumazet | d6bf781 | 2010-10-04 06:15:44 +0000 | [diff] [blame] | 553 | struct neigh_hash_table *nht; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 554 | |
| 555 | NEIGH_CACHE_STAT_INC(tbl, lookups); |
| 556 | |
Eric Dumazet | d6bf781 | 2010-10-04 06:15:44 +0000 | [diff] [blame] | 557 | rcu_read_lock_bh(); |
| 558 | nht = rcu_dereference_bh(tbl->nht); |
David S. Miller | cd08933 | 2011-07-11 01:28:12 -0700 | [diff] [blame] | 559 | hash_val = tbl->hash(pkey, NULL, nht->hash_rnd) >> (32 - nht->hash_shift); |
Eric Dumazet | 767e97e | 2010-10-06 17:49:21 -0700 | [diff] [blame] | 560 | |
| 561 | for (n = rcu_dereference_bh(nht->hash_buckets[hash_val]); |
| 562 | n != NULL; |
| 563 | n = rcu_dereference_bh(n->next)) { |
Eric W. Biederman | 426b530 | 2008-01-24 00:13:18 -0800 | [diff] [blame] | 564 | if (!memcmp(n->primary_key, pkey, key_len) && |
YOSHIFUJI Hideaki | 878628f | 2008-03-26 03:57:35 +0900 | [diff] [blame] | 565 | net_eq(dev_net(n->dev), net)) { |
Reshetova, Elena | 9f23743 | 2017-06-30 13:07:55 +0300 | [diff] [blame] | 566 | if (!refcount_inc_not_zero(&n->refcnt)) |
Eric Dumazet | 767e97e | 2010-10-06 17:49:21 -0700 | [diff] [blame] | 567 | n = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 568 | NEIGH_CACHE_STAT_INC(tbl, hits); |
| 569 | break; |
| 570 | } |
| 571 | } |
Eric Dumazet | 767e97e | 2010-10-06 17:49:21 -0700 | [diff] [blame] | 572 | |
Eric Dumazet | d6bf781 | 2010-10-04 06:15:44 +0000 | [diff] [blame] | 573 | rcu_read_unlock_bh(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 574 | return n; |
| 575 | } |
YOSHIFUJI Hideaki | 0a20450 | 2008-03-24 18:39:10 +0900 | [diff] [blame] | 576 | EXPORT_SYMBOL(neigh_lookup_nodev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 577 | |
David Ahern | 5895631 | 2018-12-07 12:24:57 -0800 | [diff] [blame] | 578 | static struct neighbour *___neigh_create(struct neigh_table *tbl, |
| 579 | const void *pkey, |
| 580 | struct net_device *dev, |
David Ahern | e997f8a | 2018-12-11 18:57:25 -0700 | [diff] [blame] | 581 | bool exempt_from_gc, bool want_ref) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 582 | { |
David Ahern | e997f8a | 2018-12-11 18:57:25 -0700 | [diff] [blame] | 583 | struct neighbour *n1, *rc, *n = neigh_alloc(tbl, dev, exempt_from_gc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 584 | u32 hash_val; |
Alexey Dobriyan | 01ccdf1 | 2017-09-23 23:03:04 +0300 | [diff] [blame] | 585 | unsigned int key_len = tbl->key_len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 586 | int error; |
Eric Dumazet | d6bf781 | 2010-10-04 06:15:44 +0000 | [diff] [blame] | 587 | struct neigh_hash_table *nht; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 588 | |
David Ahern | fc65100 | 2019-05-22 12:22:21 -0700 | [diff] [blame] | 589 | trace_neigh_create(tbl, dev, pkey, n, exempt_from_gc); |
| 590 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 591 | if (!n) { |
| 592 | rc = ERR_PTR(-ENOBUFS); |
| 593 | goto out; |
| 594 | } |
| 595 | |
| 596 | memcpy(n->primary_key, pkey, key_len); |
| 597 | n->dev = dev; |
| 598 | dev_hold(dev); |
| 599 | |
| 600 | /* Protocol specific setup. */ |
| 601 | if (tbl->constructor && (error = tbl->constructor(n)) < 0) { |
| 602 | rc = ERR_PTR(error); |
| 603 | goto out_neigh_release; |
| 604 | } |
| 605 | |
David Miller | da6a8fa | 2011-07-25 00:01:38 +0000 | [diff] [blame] | 606 | if (dev->netdev_ops->ndo_neigh_construct) { |
Jiri Pirko | 503eebc | 2016-07-05 11:27:37 +0200 | [diff] [blame] | 607 | error = dev->netdev_ops->ndo_neigh_construct(dev, n); |
David Miller | da6a8fa | 2011-07-25 00:01:38 +0000 | [diff] [blame] | 608 | if (error < 0) { |
| 609 | rc = ERR_PTR(error); |
| 610 | goto out_neigh_release; |
| 611 | } |
| 612 | } |
| 613 | |
David S. Miller | 447f219 | 2011-12-19 15:04:41 -0500 | [diff] [blame] | 614 | /* Device specific setup. */ |
| 615 | if (n->parms->neigh_setup && |
| 616 | (error = n->parms->neigh_setup(n)) < 0) { |
| 617 | rc = ERR_PTR(error); |
| 618 | goto out_neigh_release; |
| 619 | } |
| 620 | |
Jiri Pirko | 1f9248e | 2013-12-07 19:26:53 +0100 | [diff] [blame] | 621 | n->confirmed = jiffies - (NEIGH_VAR(n->parms, BASE_REACHABLE_TIME) << 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 622 | |
| 623 | write_lock_bh(&tbl->lock); |
Eric Dumazet | d6bf781 | 2010-10-04 06:15:44 +0000 | [diff] [blame] | 624 | nht = rcu_dereference_protected(tbl->nht, |
| 625 | lockdep_is_held(&tbl->lock)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 626 | |
David S. Miller | cd08933 | 2011-07-11 01:28:12 -0700 | [diff] [blame] | 627 | if (atomic_read(&tbl->entries) > (1 << nht->hash_shift)) |
| 628 | nht = neigh_hash_grow(tbl, nht->hash_shift + 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 629 | |
Jim Westfall | 096b985 | 2018-01-14 04:18:50 -0800 | [diff] [blame] | 630 | hash_val = tbl->hash(n->primary_key, dev, nht->hash_rnd) >> (32 - nht->hash_shift); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 631 | |
| 632 | if (n->parms->dead) { |
| 633 | rc = ERR_PTR(-EINVAL); |
| 634 | goto out_tbl_unlock; |
| 635 | } |
| 636 | |
Eric Dumazet | 767e97e | 2010-10-06 17:49:21 -0700 | [diff] [blame] | 637 | for (n1 = rcu_dereference_protected(nht->hash_buckets[hash_val], |
| 638 | lockdep_is_held(&tbl->lock)); |
| 639 | n1 != NULL; |
| 640 | n1 = rcu_dereference_protected(n1->next, |
| 641 | lockdep_is_held(&tbl->lock))) { |
Jim Westfall | 096b985 | 2018-01-14 04:18:50 -0800 | [diff] [blame] | 642 | if (dev == n1->dev && !memcmp(n1->primary_key, n->primary_key, key_len)) { |
David S. Miller | a263b30 | 2012-07-02 02:02:15 -0700 | [diff] [blame] | 643 | if (want_ref) |
| 644 | neigh_hold(n1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 645 | rc = n1; |
| 646 | goto out_tbl_unlock; |
| 647 | } |
| 648 | } |
| 649 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 650 | n->dead = 0; |
David Ahern | e997f8a | 2018-12-11 18:57:25 -0700 | [diff] [blame] | 651 | if (!exempt_from_gc) |
David Ahern | 8cc196d | 2018-12-10 13:54:07 -0800 | [diff] [blame] | 652 | list_add_tail(&n->gc_list, &n->tbl->gc_list); |
| 653 | |
David S. Miller | a263b30 | 2012-07-02 02:02:15 -0700 | [diff] [blame] | 654 | if (want_ref) |
| 655 | neigh_hold(n); |
Eric Dumazet | 767e97e | 2010-10-06 17:49:21 -0700 | [diff] [blame] | 656 | rcu_assign_pointer(n->next, |
| 657 | rcu_dereference_protected(nht->hash_buckets[hash_val], |
| 658 | lockdep_is_held(&tbl->lock))); |
| 659 | rcu_assign_pointer(nht->hash_buckets[hash_val], n); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 660 | write_unlock_bh(&tbl->lock); |
Joe Perches | d5d427c | 2013-04-15 15:17:19 +0000 | [diff] [blame] | 661 | neigh_dbg(2, "neigh %p is created\n", n); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 662 | rc = n; |
| 663 | out: |
| 664 | return rc; |
| 665 | out_tbl_unlock: |
| 666 | write_unlock_bh(&tbl->lock); |
| 667 | out_neigh_release: |
David Ahern | 64c6f4b | 2019-05-01 18:08:34 -0700 | [diff] [blame] | 668 | if (!exempt_from_gc) |
| 669 | atomic_dec(&tbl->gc_entries); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 670 | neigh_release(n); |
| 671 | goto out; |
| 672 | } |
David Ahern | 5895631 | 2018-12-07 12:24:57 -0800 | [diff] [blame] | 673 | |
| 674 | struct neighbour *__neigh_create(struct neigh_table *tbl, const void *pkey, |
| 675 | struct net_device *dev, bool want_ref) |
| 676 | { |
| 677 | return ___neigh_create(tbl, pkey, dev, false, want_ref); |
| 678 | } |
David S. Miller | a263b30 | 2012-07-02 02:02:15 -0700 | [diff] [blame] | 679 | EXPORT_SYMBOL(__neigh_create); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 680 | |
Alexey Dobriyan | 01ccdf1 | 2017-09-23 23:03:04 +0300 | [diff] [blame] | 681 | static u32 pneigh_hash(const void *pkey, unsigned int key_len) |
Pavel Emelyanov | fa86d32 | 2008-03-24 14:48:59 -0700 | [diff] [blame] | 682 | { |
Pavel Emelyanov | fa86d32 | 2008-03-24 14:48:59 -0700 | [diff] [blame] | 683 | u32 hash_val = *(u32 *)(pkey + key_len - 4); |
Pavel Emelyanov | fa86d32 | 2008-03-24 14:48:59 -0700 | [diff] [blame] | 684 | hash_val ^= (hash_val >> 16); |
| 685 | hash_val ^= hash_val >> 8; |
| 686 | hash_val ^= hash_val >> 4; |
| 687 | hash_val &= PNEIGH_HASHMASK; |
YOSHIFUJI Hideaki | be01d65 | 2008-03-28 12:46:53 +0900 | [diff] [blame] | 688 | return hash_val; |
| 689 | } |
Pavel Emelyanov | fa86d32 | 2008-03-24 14:48:59 -0700 | [diff] [blame] | 690 | |
YOSHIFUJI Hideaki | be01d65 | 2008-03-28 12:46:53 +0900 | [diff] [blame] | 691 | static struct pneigh_entry *__pneigh_lookup_1(struct pneigh_entry *n, |
| 692 | struct net *net, |
| 693 | const void *pkey, |
Alexey Dobriyan | 01ccdf1 | 2017-09-23 23:03:04 +0300 | [diff] [blame] | 694 | unsigned int key_len, |
YOSHIFUJI Hideaki | be01d65 | 2008-03-28 12:46:53 +0900 | [diff] [blame] | 695 | struct net_device *dev) |
| 696 | { |
| 697 | while (n) { |
Pavel Emelyanov | fa86d32 | 2008-03-24 14:48:59 -0700 | [diff] [blame] | 698 | if (!memcmp(n->key, pkey, key_len) && |
YOSHIFUJI Hideaki | be01d65 | 2008-03-28 12:46:53 +0900 | [diff] [blame] | 699 | net_eq(pneigh_net(n), net) && |
Pavel Emelyanov | fa86d32 | 2008-03-24 14:48:59 -0700 | [diff] [blame] | 700 | (n->dev == dev || !n->dev)) |
YOSHIFUJI Hideaki | be01d65 | 2008-03-28 12:46:53 +0900 | [diff] [blame] | 701 | return n; |
| 702 | n = n->next; |
Pavel Emelyanov | fa86d32 | 2008-03-24 14:48:59 -0700 | [diff] [blame] | 703 | } |
YOSHIFUJI Hideaki | be01d65 | 2008-03-28 12:46:53 +0900 | [diff] [blame] | 704 | return NULL; |
| 705 | } |
Pavel Emelyanov | fa86d32 | 2008-03-24 14:48:59 -0700 | [diff] [blame] | 706 | |
YOSHIFUJI Hideaki | be01d65 | 2008-03-28 12:46:53 +0900 | [diff] [blame] | 707 | struct pneigh_entry *__pneigh_lookup(struct neigh_table *tbl, |
| 708 | struct net *net, const void *pkey, struct net_device *dev) |
| 709 | { |
Alexey Dobriyan | 01ccdf1 | 2017-09-23 23:03:04 +0300 | [diff] [blame] | 710 | unsigned int key_len = tbl->key_len; |
YOSHIFUJI Hideaki | be01d65 | 2008-03-28 12:46:53 +0900 | [diff] [blame] | 711 | u32 hash_val = pneigh_hash(pkey, key_len); |
| 712 | |
| 713 | return __pneigh_lookup_1(tbl->phash_buckets[hash_val], |
| 714 | net, pkey, key_len, dev); |
Pavel Emelyanov | fa86d32 | 2008-03-24 14:48:59 -0700 | [diff] [blame] | 715 | } |
YOSHIFUJI Hideaki | 0a20450 | 2008-03-24 18:39:10 +0900 | [diff] [blame] | 716 | EXPORT_SYMBOL_GPL(__pneigh_lookup); |
Pavel Emelyanov | fa86d32 | 2008-03-24 14:48:59 -0700 | [diff] [blame] | 717 | |
Eric W. Biederman | 426b530 | 2008-01-24 00:13:18 -0800 | [diff] [blame] | 718 | struct pneigh_entry * pneigh_lookup(struct neigh_table *tbl, |
| 719 | struct net *net, const void *pkey, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 720 | struct net_device *dev, int creat) |
| 721 | { |
| 722 | struct pneigh_entry *n; |
Alexey Dobriyan | 01ccdf1 | 2017-09-23 23:03:04 +0300 | [diff] [blame] | 723 | unsigned int key_len = tbl->key_len; |
YOSHIFUJI Hideaki | be01d65 | 2008-03-28 12:46:53 +0900 | [diff] [blame] | 724 | u32 hash_val = pneigh_hash(pkey, key_len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 725 | |
| 726 | read_lock_bh(&tbl->lock); |
YOSHIFUJI Hideaki | be01d65 | 2008-03-28 12:46:53 +0900 | [diff] [blame] | 727 | n = __pneigh_lookup_1(tbl->phash_buckets[hash_val], |
| 728 | net, pkey, key_len, dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 729 | read_unlock_bh(&tbl->lock); |
YOSHIFUJI Hideaki | be01d65 | 2008-03-28 12:46:53 +0900 | [diff] [blame] | 730 | |
| 731 | if (n || !creat) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 732 | goto out; |
| 733 | |
Pavel Emelyanov | 4ae2894 | 2007-10-15 12:54:15 -0700 | [diff] [blame] | 734 | ASSERT_RTNL(); |
| 735 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 736 | n = kmalloc(sizeof(*n) + key_len, GFP_KERNEL); |
| 737 | if (!n) |
| 738 | goto out; |
| 739 | |
David Ahern | 754d5da | 2018-12-19 15:53:22 -0800 | [diff] [blame] | 740 | n->protocol = 0; |
Eric W. Biederman | efd7ef1 | 2015-03-11 23:04:08 -0500 | [diff] [blame] | 741 | write_pnet(&n->net, net); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 742 | memcpy(n->key, pkey, key_len); |
| 743 | n->dev = dev; |
| 744 | if (dev) |
| 745 | dev_hold(dev); |
| 746 | |
| 747 | if (tbl->pconstructor && tbl->pconstructor(n)) { |
| 748 | if (dev) |
| 749 | dev_put(dev); |
| 750 | kfree(n); |
| 751 | n = NULL; |
| 752 | goto out; |
| 753 | } |
| 754 | |
| 755 | write_lock_bh(&tbl->lock); |
| 756 | n->next = tbl->phash_buckets[hash_val]; |
| 757 | tbl->phash_buckets[hash_val] = n; |
| 758 | write_unlock_bh(&tbl->lock); |
| 759 | out: |
| 760 | return n; |
| 761 | } |
YOSHIFUJI Hideaki | 0a20450 | 2008-03-24 18:39:10 +0900 | [diff] [blame] | 762 | EXPORT_SYMBOL(pneigh_lookup); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 763 | |
| 764 | |
Eric W. Biederman | 426b530 | 2008-01-24 00:13:18 -0800 | [diff] [blame] | 765 | int pneigh_delete(struct neigh_table *tbl, struct net *net, const void *pkey, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 766 | struct net_device *dev) |
| 767 | { |
| 768 | struct pneigh_entry *n, **np; |
Alexey Dobriyan | 01ccdf1 | 2017-09-23 23:03:04 +0300 | [diff] [blame] | 769 | unsigned int key_len = tbl->key_len; |
YOSHIFUJI Hideaki | be01d65 | 2008-03-28 12:46:53 +0900 | [diff] [blame] | 770 | u32 hash_val = pneigh_hash(pkey, key_len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 771 | |
| 772 | write_lock_bh(&tbl->lock); |
| 773 | for (np = &tbl->phash_buckets[hash_val]; (n = *np) != NULL; |
| 774 | np = &n->next) { |
Eric W. Biederman | 426b530 | 2008-01-24 00:13:18 -0800 | [diff] [blame] | 775 | if (!memcmp(n->key, pkey, key_len) && n->dev == dev && |
YOSHIFUJI Hideaki | 878628f | 2008-03-26 03:57:35 +0900 | [diff] [blame] | 776 | net_eq(pneigh_net(n), net)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 777 | *np = n->next; |
| 778 | write_unlock_bh(&tbl->lock); |
| 779 | if (tbl->pdestructor) |
| 780 | tbl->pdestructor(n); |
| 781 | if (n->dev) |
| 782 | dev_put(n->dev); |
| 783 | kfree(n); |
| 784 | return 0; |
| 785 | } |
| 786 | } |
| 787 | write_unlock_bh(&tbl->lock); |
| 788 | return -ENOENT; |
| 789 | } |
| 790 | |
Wolfgang Bumiller | 53b76cd | 2018-04-12 10:46:55 +0200 | [diff] [blame] | 791 | static int pneigh_ifdown_and_unlock(struct neigh_table *tbl, |
| 792 | struct net_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 793 | { |
Wolfgang Bumiller | 53b76cd | 2018-04-12 10:46:55 +0200 | [diff] [blame] | 794 | struct pneigh_entry *n, **np, *freelist = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 795 | u32 h; |
| 796 | |
| 797 | for (h = 0; h <= PNEIGH_HASHMASK; h++) { |
| 798 | np = &tbl->phash_buckets[h]; |
| 799 | while ((n = *np) != NULL) { |
| 800 | if (!dev || n->dev == dev) { |
| 801 | *np = n->next; |
Wolfgang Bumiller | 53b76cd | 2018-04-12 10:46:55 +0200 | [diff] [blame] | 802 | n->next = freelist; |
| 803 | freelist = n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 804 | continue; |
| 805 | } |
| 806 | np = &n->next; |
| 807 | } |
| 808 | } |
Wolfgang Bumiller | 53b76cd | 2018-04-12 10:46:55 +0200 | [diff] [blame] | 809 | write_unlock_bh(&tbl->lock); |
| 810 | while ((n = freelist)) { |
| 811 | freelist = n->next; |
| 812 | n->next = NULL; |
| 813 | if (tbl->pdestructor) |
| 814 | tbl->pdestructor(n); |
| 815 | if (n->dev) |
| 816 | dev_put(n->dev); |
| 817 | kfree(n); |
| 818 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 819 | return -ENOENT; |
| 820 | } |
| 821 | |
Denis V. Lunev | 06f0511 | 2008-01-24 00:30:58 -0800 | [diff] [blame] | 822 | static void neigh_parms_destroy(struct neigh_parms *parms); |
| 823 | |
| 824 | static inline void neigh_parms_put(struct neigh_parms *parms) |
| 825 | { |
Reshetova, Elena | 6343944 | 2017-06-30 13:07:56 +0300 | [diff] [blame] | 826 | if (refcount_dec_and_test(&parms->refcnt)) |
Denis V. Lunev | 06f0511 | 2008-01-24 00:30:58 -0800 | [diff] [blame] | 827 | neigh_parms_destroy(parms); |
| 828 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 829 | |
| 830 | /* |
| 831 | * neighbour must already be out of the table; |
| 832 | * |
| 833 | */ |
| 834 | void neigh_destroy(struct neighbour *neigh) |
| 835 | { |
David Miller | da6a8fa | 2011-07-25 00:01:38 +0000 | [diff] [blame] | 836 | struct net_device *dev = neigh->dev; |
| 837 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 838 | NEIGH_CACHE_STAT_INC(neigh->tbl, destroys); |
| 839 | |
| 840 | if (!neigh->dead) { |
Joe Perches | e005d19 | 2012-05-16 19:58:40 +0000 | [diff] [blame] | 841 | pr_warn("Destroying alive neighbour %p\n", neigh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 842 | dump_stack(); |
| 843 | return; |
| 844 | } |
| 845 | |
| 846 | if (neigh_del_timer(neigh)) |
Joe Perches | e005d19 | 2012-05-16 19:58:40 +0000 | [diff] [blame] | 847 | pr_warn("Impossible event\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 848 | |
Eric Dumazet | c9ab4d8 | 2013-06-28 02:37:42 -0700 | [diff] [blame] | 849 | write_lock_bh(&neigh->lock); |
| 850 | __skb_queue_purge(&neigh->arp_queue); |
| 851 | write_unlock_bh(&neigh->lock); |
Eric Dumazet | 8b5c171 | 2011-11-09 12:07:14 +0000 | [diff] [blame] | 852 | neigh->arp_queue_len_bytes = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 853 | |
David S. Miller | 447f219 | 2011-12-19 15:04:41 -0500 | [diff] [blame] | 854 | if (dev->netdev_ops->ndo_neigh_destroy) |
Jiri Pirko | 503eebc | 2016-07-05 11:27:37 +0200 | [diff] [blame] | 855 | dev->netdev_ops->ndo_neigh_destroy(dev, neigh); |
David S. Miller | 447f219 | 2011-12-19 15:04:41 -0500 | [diff] [blame] | 856 | |
David Miller | da6a8fa | 2011-07-25 00:01:38 +0000 | [diff] [blame] | 857 | dev_put(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 858 | neigh_parms_put(neigh->parms); |
| 859 | |
Joe Perches | d5d427c | 2013-04-15 15:17:19 +0000 | [diff] [blame] | 860 | neigh_dbg(2, "neigh %p is destroyed\n", neigh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 861 | |
| 862 | atomic_dec(&neigh->tbl->entries); |
David Miller | 5b8b006 | 2011-07-25 00:01:22 +0000 | [diff] [blame] | 863 | kfree_rcu(neigh, rcu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 864 | } |
YOSHIFUJI Hideaki | 0a20450 | 2008-03-24 18:39:10 +0900 | [diff] [blame] | 865 | EXPORT_SYMBOL(neigh_destroy); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 866 | |
| 867 | /* Neighbour state is suspicious; |
| 868 | disable fast path. |
| 869 | |
| 870 | Called with write_locked neigh. |
| 871 | */ |
| 872 | static void neigh_suspect(struct neighbour *neigh) |
| 873 | { |
Joe Perches | d5d427c | 2013-04-15 15:17:19 +0000 | [diff] [blame] | 874 | neigh_dbg(2, "neigh %p is suspected\n", neigh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 875 | |
| 876 | neigh->output = neigh->ops->output; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 877 | } |
| 878 | |
| 879 | /* Neighbour state is OK; |
| 880 | enable fast path. |
| 881 | |
| 882 | Called with write_locked neigh. |
| 883 | */ |
| 884 | static void neigh_connect(struct neighbour *neigh) |
| 885 | { |
Joe Perches | d5d427c | 2013-04-15 15:17:19 +0000 | [diff] [blame] | 886 | neigh_dbg(2, "neigh %p is connected\n", neigh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 887 | |
| 888 | neigh->output = neigh->ops->connected_output; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 889 | } |
| 890 | |
Eric Dumazet | e4c4e44 | 2009-07-30 03:15:07 +0000 | [diff] [blame] | 891 | static void neigh_periodic_work(struct work_struct *work) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 892 | { |
Eric Dumazet | e4c4e44 | 2009-07-30 03:15:07 +0000 | [diff] [blame] | 893 | struct neigh_table *tbl = container_of(work, struct neigh_table, gc_work.work); |
Eric Dumazet | 767e97e | 2010-10-06 17:49:21 -0700 | [diff] [blame] | 894 | struct neighbour *n; |
| 895 | struct neighbour __rcu **np; |
Eric Dumazet | e4c4e44 | 2009-07-30 03:15:07 +0000 | [diff] [blame] | 896 | unsigned int i; |
Eric Dumazet | d6bf781 | 2010-10-04 06:15:44 +0000 | [diff] [blame] | 897 | struct neigh_hash_table *nht; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 898 | |
| 899 | NEIGH_CACHE_STAT_INC(tbl, periodic_gc_runs); |
| 900 | |
Eric Dumazet | e4c4e44 | 2009-07-30 03:15:07 +0000 | [diff] [blame] | 901 | write_lock_bh(&tbl->lock); |
Eric Dumazet | d6bf781 | 2010-10-04 06:15:44 +0000 | [diff] [blame] | 902 | nht = rcu_dereference_protected(tbl->nht, |
| 903 | lockdep_is_held(&tbl->lock)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 904 | |
| 905 | /* |
| 906 | * periodically recompute ReachableTime from random function |
| 907 | */ |
| 908 | |
Eric Dumazet | e4c4e44 | 2009-07-30 03:15:07 +0000 | [diff] [blame] | 909 | if (time_after(jiffies, tbl->last_rand + 300 * HZ)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 910 | struct neigh_parms *p; |
Eric Dumazet | e4c4e44 | 2009-07-30 03:15:07 +0000 | [diff] [blame] | 911 | tbl->last_rand = jiffies; |
Nicolas Dichtel | 75fbfd3 | 2014-10-29 19:29:31 +0100 | [diff] [blame] | 912 | list_for_each_entry(p, &tbl->parms_list, list) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 913 | p->reachable_time = |
Jiri Pirko | 1f9248e | 2013-12-07 19:26:53 +0100 | [diff] [blame] | 914 | neigh_rand_reach_time(NEIGH_VAR(p, BASE_REACHABLE_TIME)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 915 | } |
| 916 | |
Duan Jiong | feff9ab | 2014-02-27 17:14:41 +0800 | [diff] [blame] | 917 | if (atomic_read(&tbl->entries) < tbl->gc_thresh1) |
| 918 | goto out; |
| 919 | |
David S. Miller | cd08933 | 2011-07-11 01:28:12 -0700 | [diff] [blame] | 920 | for (i = 0 ; i < (1 << nht->hash_shift); i++) { |
Eric Dumazet | d6bf781 | 2010-10-04 06:15:44 +0000 | [diff] [blame] | 921 | np = &nht->hash_buckets[i]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 922 | |
Eric Dumazet | 767e97e | 2010-10-06 17:49:21 -0700 | [diff] [blame] | 923 | while ((n = rcu_dereference_protected(*np, |
| 924 | lockdep_is_held(&tbl->lock))) != NULL) { |
Eric Dumazet | e4c4e44 | 2009-07-30 03:15:07 +0000 | [diff] [blame] | 925 | unsigned int state; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 926 | |
Eric Dumazet | e4c4e44 | 2009-07-30 03:15:07 +0000 | [diff] [blame] | 927 | write_lock(&n->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 928 | |
Eric Dumazet | e4c4e44 | 2009-07-30 03:15:07 +0000 | [diff] [blame] | 929 | state = n->nud_state; |
Roopa Prabhu | 9ce33e4 | 2018-04-24 13:49:34 -0700 | [diff] [blame] | 930 | if ((state & (NUD_PERMANENT | NUD_IN_TIMER)) || |
| 931 | (n->flags & NTF_EXT_LEARNED)) { |
Eric Dumazet | e4c4e44 | 2009-07-30 03:15:07 +0000 | [diff] [blame] | 932 | write_unlock(&n->lock); |
| 933 | goto next_elt; |
| 934 | } |
| 935 | |
| 936 | if (time_before(n->used, n->confirmed)) |
| 937 | n->used = n->confirmed; |
| 938 | |
Reshetova, Elena | 9f23743 | 2017-06-30 13:07:55 +0300 | [diff] [blame] | 939 | if (refcount_read(&n->refcnt) == 1 && |
Eric Dumazet | e4c4e44 | 2009-07-30 03:15:07 +0000 | [diff] [blame] | 940 | (state == NUD_FAILED || |
Jiri Pirko | 1f9248e | 2013-12-07 19:26:53 +0100 | [diff] [blame] | 941 | time_after(jiffies, n->used + NEIGH_VAR(n->parms, GC_STALETIME)))) { |
Eric Dumazet | e4c4e44 | 2009-07-30 03:15:07 +0000 | [diff] [blame] | 942 | *np = n->next; |
David Ahern | 5895631 | 2018-12-07 12:24:57 -0800 | [diff] [blame] | 943 | neigh_mark_dead(n); |
Eric Dumazet | e4c4e44 | 2009-07-30 03:15:07 +0000 | [diff] [blame] | 944 | write_unlock(&n->lock); |
| 945 | neigh_cleanup_and_release(n); |
| 946 | continue; |
| 947 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 948 | write_unlock(&n->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 949 | |
| 950 | next_elt: |
Eric Dumazet | e4c4e44 | 2009-07-30 03:15:07 +0000 | [diff] [blame] | 951 | np = &n->next; |
| 952 | } |
| 953 | /* |
| 954 | * It's fine to release lock here, even if hash table |
| 955 | * grows while we are preempted. |
| 956 | */ |
| 957 | write_unlock_bh(&tbl->lock); |
| 958 | cond_resched(); |
| 959 | write_lock_bh(&tbl->lock); |
Michel Machado | 84338a6 | 2012-02-21 16:04:13 -0500 | [diff] [blame] | 960 | nht = rcu_dereference_protected(tbl->nht, |
| 961 | lockdep_is_held(&tbl->lock)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 962 | } |
YOSHIFUJI Hideaki / 吉藤英明 | 2724680 | 2013-01-22 05:20:05 +0000 | [diff] [blame] | 963 | out: |
Jiri Pirko | 1f9248e | 2013-12-07 19:26:53 +0100 | [diff] [blame] | 964 | /* Cycle through all hash buckets every BASE_REACHABLE_TIME/2 ticks. |
| 965 | * ARP entry timeouts range from 1/2 BASE_REACHABLE_TIME to 3/2 |
| 966 | * BASE_REACHABLE_TIME. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 967 | */ |
viresh kumar | f618002 | 2014-01-22 12:23:33 +0530 | [diff] [blame] | 968 | queue_delayed_work(system_power_efficient_wq, &tbl->gc_work, |
Jiri Pirko | 1f9248e | 2013-12-07 19:26:53 +0100 | [diff] [blame] | 969 | NEIGH_VAR(&tbl->parms, BASE_REACHABLE_TIME) >> 1); |
Eric Dumazet | e4c4e44 | 2009-07-30 03:15:07 +0000 | [diff] [blame] | 970 | write_unlock_bh(&tbl->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 971 | } |
| 972 | |
| 973 | static __inline__ int neigh_max_probes(struct neighbour *n) |
| 974 | { |
| 975 | struct neigh_parms *p = n->parms; |
YOSHIFUJI Hideaki/吉藤英明 | 8da8646 | 2015-03-19 22:41:46 +0900 | [diff] [blame] | 976 | return NEIGH_VAR(p, UCAST_PROBES) + NEIGH_VAR(p, APP_PROBES) + |
| 977 | (n->nud_state & NUD_PROBE ? NEIGH_VAR(p, MCAST_REPROBES) : |
| 978 | NEIGH_VAR(p, MCAST_PROBES)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 979 | } |
| 980 | |
Timo Teras | 5ef12d9 | 2009-06-11 04:16:28 -0700 | [diff] [blame] | 981 | static void neigh_invalidate(struct neighbour *neigh) |
Eric Dumazet | 0a14150 | 2010-03-09 19:40:54 +0000 | [diff] [blame] | 982 | __releases(neigh->lock) |
| 983 | __acquires(neigh->lock) |
Timo Teras | 5ef12d9 | 2009-06-11 04:16:28 -0700 | [diff] [blame] | 984 | { |
| 985 | struct sk_buff *skb; |
| 986 | |
| 987 | NEIGH_CACHE_STAT_INC(neigh->tbl, res_failed); |
Joe Perches | d5d427c | 2013-04-15 15:17:19 +0000 | [diff] [blame] | 988 | neigh_dbg(2, "neigh %p is failed\n", neigh); |
Timo Teras | 5ef12d9 | 2009-06-11 04:16:28 -0700 | [diff] [blame] | 989 | neigh->updated = jiffies; |
| 990 | |
| 991 | /* It is very thin place. report_unreachable is very complicated |
| 992 | routine. Particularly, it can hit the same neighbour entry! |
| 993 | |
| 994 | So that, we try to be accurate and avoid dead loop. --ANK |
| 995 | */ |
| 996 | while (neigh->nud_state == NUD_FAILED && |
| 997 | (skb = __skb_dequeue(&neigh->arp_queue)) != NULL) { |
| 998 | write_unlock(&neigh->lock); |
| 999 | neigh->ops->error_report(neigh, skb); |
| 1000 | write_lock(&neigh->lock); |
| 1001 | } |
Eric Dumazet | c9ab4d8 | 2013-06-28 02:37:42 -0700 | [diff] [blame] | 1002 | __skb_queue_purge(&neigh->arp_queue); |
Eric Dumazet | 8b5c171 | 2011-11-09 12:07:14 +0000 | [diff] [blame] | 1003 | neigh->arp_queue_len_bytes = 0; |
Timo Teras | 5ef12d9 | 2009-06-11 04:16:28 -0700 | [diff] [blame] | 1004 | } |
| 1005 | |
Eric Dumazet | cd28ca0 | 2011-08-09 08:15:58 +0000 | [diff] [blame] | 1006 | static void neigh_probe(struct neighbour *neigh) |
| 1007 | __releases(neigh->lock) |
| 1008 | { |
Hannes Frederic Sowa | 4ed377e | 2013-09-21 06:32:34 +0200 | [diff] [blame] | 1009 | struct sk_buff *skb = skb_peek_tail(&neigh->arp_queue); |
Eric Dumazet | cd28ca0 | 2011-08-09 08:15:58 +0000 | [diff] [blame] | 1010 | /* keep skb alive even if arp_queue overflows */ |
| 1011 | if (skb) |
Martin Zhang | 19125c1 | 2015-11-17 20:49:30 +0800 | [diff] [blame] | 1012 | skb = skb_clone(skb, GFP_ATOMIC); |
Eric Dumazet | cd28ca0 | 2011-08-09 08:15:58 +0000 | [diff] [blame] | 1013 | write_unlock(&neigh->lock); |
Eric Dumazet | 48481c8 | 2017-03-23 12:39:21 -0700 | [diff] [blame] | 1014 | if (neigh->ops->solicit) |
| 1015 | neigh->ops->solicit(neigh, skb); |
Eric Dumazet | cd28ca0 | 2011-08-09 08:15:58 +0000 | [diff] [blame] | 1016 | atomic_inc(&neigh->probes); |
Yang Wei | 87fff3ca | 2019-01-17 23:11:30 +0800 | [diff] [blame] | 1017 | consume_skb(skb); |
Eric Dumazet | cd28ca0 | 2011-08-09 08:15:58 +0000 | [diff] [blame] | 1018 | } |
| 1019 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1020 | /* Called when a timer expires for a neighbour entry. */ |
| 1021 | |
Kees Cook | e99e88a | 2017-10-16 14:43:17 -0700 | [diff] [blame] | 1022 | static void neigh_timer_handler(struct timer_list *t) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1023 | { |
| 1024 | unsigned long now, next; |
Kees Cook | e99e88a | 2017-10-16 14:43:17 -0700 | [diff] [blame] | 1025 | struct neighbour *neigh = from_timer(neigh, t, timer); |
Eric Dumazet | 95c9617 | 2012-04-15 05:58:06 +0000 | [diff] [blame] | 1026 | unsigned int state; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1027 | int notify = 0; |
| 1028 | |
| 1029 | write_lock(&neigh->lock); |
| 1030 | |
| 1031 | state = neigh->nud_state; |
| 1032 | now = jiffies; |
| 1033 | next = now + HZ; |
| 1034 | |
David S. Miller | 045f7b3 | 2011-11-01 17:45:55 -0400 | [diff] [blame] | 1035 | if (!(state & NUD_IN_TIMER)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1036 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1037 | |
| 1038 | if (state & NUD_REACHABLE) { |
YOSHIFUJI Hideaki | 4ec93ed | 2007-02-09 23:24:36 +0900 | [diff] [blame] | 1039 | if (time_before_eq(now, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1040 | neigh->confirmed + neigh->parms->reachable_time)) { |
Joe Perches | d5d427c | 2013-04-15 15:17:19 +0000 | [diff] [blame] | 1041 | neigh_dbg(2, "neigh %p is still alive\n", neigh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1042 | next = neigh->confirmed + neigh->parms->reachable_time; |
| 1043 | } else if (time_before_eq(now, |
Jiri Pirko | 1f9248e | 2013-12-07 19:26:53 +0100 | [diff] [blame] | 1044 | neigh->used + |
| 1045 | NEIGH_VAR(neigh->parms, DELAY_PROBE_TIME))) { |
Joe Perches | d5d427c | 2013-04-15 15:17:19 +0000 | [diff] [blame] | 1046 | neigh_dbg(2, "neigh %p is delayed\n", neigh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1047 | neigh->nud_state = NUD_DELAY; |
YOSHIFUJI Hideaki | 955aaa2 | 2006-03-20 16:52:52 -0800 | [diff] [blame] | 1048 | neigh->updated = jiffies; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1049 | neigh_suspect(neigh); |
Jiri Pirko | 1f9248e | 2013-12-07 19:26:53 +0100 | [diff] [blame] | 1050 | next = now + NEIGH_VAR(neigh->parms, DELAY_PROBE_TIME); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1051 | } else { |
Joe Perches | d5d427c | 2013-04-15 15:17:19 +0000 | [diff] [blame] | 1052 | neigh_dbg(2, "neigh %p is suspected\n", neigh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1053 | neigh->nud_state = NUD_STALE; |
YOSHIFUJI Hideaki | 955aaa2 | 2006-03-20 16:52:52 -0800 | [diff] [blame] | 1054 | neigh->updated = jiffies; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1055 | neigh_suspect(neigh); |
Tom Tucker | 8d71740 | 2006-07-30 20:43:36 -0700 | [diff] [blame] | 1056 | notify = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1057 | } |
| 1058 | } else if (state & NUD_DELAY) { |
YOSHIFUJI Hideaki | 4ec93ed | 2007-02-09 23:24:36 +0900 | [diff] [blame] | 1059 | if (time_before_eq(now, |
Jiri Pirko | 1f9248e | 2013-12-07 19:26:53 +0100 | [diff] [blame] | 1060 | neigh->confirmed + |
| 1061 | NEIGH_VAR(neigh->parms, DELAY_PROBE_TIME))) { |
Joe Perches | d5d427c | 2013-04-15 15:17:19 +0000 | [diff] [blame] | 1062 | neigh_dbg(2, "neigh %p is now reachable\n", neigh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1063 | neigh->nud_state = NUD_REACHABLE; |
YOSHIFUJI Hideaki | 955aaa2 | 2006-03-20 16:52:52 -0800 | [diff] [blame] | 1064 | neigh->updated = jiffies; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1065 | neigh_connect(neigh); |
Tom Tucker | 8d71740 | 2006-07-30 20:43:36 -0700 | [diff] [blame] | 1066 | notify = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1067 | next = neigh->confirmed + neigh->parms->reachable_time; |
| 1068 | } else { |
Joe Perches | d5d427c | 2013-04-15 15:17:19 +0000 | [diff] [blame] | 1069 | neigh_dbg(2, "neigh %p is probed\n", neigh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1070 | neigh->nud_state = NUD_PROBE; |
YOSHIFUJI Hideaki | 955aaa2 | 2006-03-20 16:52:52 -0800 | [diff] [blame] | 1071 | neigh->updated = jiffies; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1072 | atomic_set(&neigh->probes, 0); |
Erik Kline | 765c9c6 | 2015-05-18 19:44:41 +0900 | [diff] [blame] | 1073 | notify = 1; |
Hangbin Liu | 19e16d2 | 2020-04-01 14:46:20 +0800 | [diff] [blame] | 1074 | next = now + max(NEIGH_VAR(neigh->parms, RETRANS_TIME), |
| 1075 | HZ/100); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1076 | } |
| 1077 | } else { |
| 1078 | /* NUD_PROBE|NUD_INCOMPLETE */ |
Hangbin Liu | 19e16d2 | 2020-04-01 14:46:20 +0800 | [diff] [blame] | 1079 | next = now + max(NEIGH_VAR(neigh->parms, RETRANS_TIME), HZ/100); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1080 | } |
| 1081 | |
| 1082 | if ((neigh->nud_state & (NUD_INCOMPLETE | NUD_PROBE)) && |
| 1083 | atomic_read(&neigh->probes) >= neigh_max_probes(neigh)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1084 | neigh->nud_state = NUD_FAILED; |
| 1085 | notify = 1; |
Timo Teras | 5ef12d9 | 2009-06-11 04:16:28 -0700 | [diff] [blame] | 1086 | neigh_invalidate(neigh); |
Duan Jiong | 5e2c21d | 2014-02-27 17:03:03 +0800 | [diff] [blame] | 1087 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1088 | } |
| 1089 | |
| 1090 | if (neigh->nud_state & NUD_IN_TIMER) { |
Hangbin Liu | 96d10d5 | 2020-05-28 15:15:13 +0800 | [diff] [blame] | 1091 | if (time_before(next, jiffies + HZ/100)) |
| 1092 | next = jiffies + HZ/100; |
Herbert Xu | 6fb9974 | 2005-10-23 16:37:48 +1000 | [diff] [blame] | 1093 | if (!mod_timer(&neigh->timer, next)) |
| 1094 | neigh_hold(neigh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1095 | } |
| 1096 | if (neigh->nud_state & (NUD_INCOMPLETE | NUD_PROBE)) { |
Eric Dumazet | cd28ca0 | 2011-08-09 08:15:58 +0000 | [diff] [blame] | 1097 | neigh_probe(neigh); |
David S. Miller | 9ff5660 | 2008-02-17 18:39:54 -0800 | [diff] [blame] | 1098 | } else { |
David S. Miller | 69cc64d | 2008-02-11 21:45:44 -0800 | [diff] [blame] | 1099 | out: |
David S. Miller | 9ff5660 | 2008-02-17 18:39:54 -0800 | [diff] [blame] | 1100 | write_unlock(&neigh->lock); |
| 1101 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1102 | |
Thomas Graf | d961db3 | 2007-08-08 23:12:56 -0700 | [diff] [blame] | 1103 | if (notify) |
Roopa Prabhu | 7b8f7a4 | 2017-03-19 22:01:28 -0700 | [diff] [blame] | 1104 | neigh_update_notify(neigh, 0); |
Thomas Graf | d961db3 | 2007-08-08 23:12:56 -0700 | [diff] [blame] | 1105 | |
Roopa Prabhu | 56dd18a | 2019-02-14 09:15:11 -0800 | [diff] [blame] | 1106 | trace_neigh_timer_handler(neigh, 0); |
| 1107 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1108 | neigh_release(neigh); |
| 1109 | } |
| 1110 | |
| 1111 | int __neigh_event_send(struct neighbour *neigh, struct sk_buff *skb) |
| 1112 | { |
| 1113 | int rc; |
Eric Dumazet | cd28ca0 | 2011-08-09 08:15:58 +0000 | [diff] [blame] | 1114 | bool immediate_probe = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1115 | |
| 1116 | write_lock_bh(&neigh->lock); |
| 1117 | |
| 1118 | rc = 0; |
| 1119 | if (neigh->nud_state & (NUD_CONNECTED | NUD_DELAY | NUD_PROBE)) |
| 1120 | goto out_unlock_bh; |
Julian Anastasov | 2c51a97 | 2015-06-16 22:56:39 +0300 | [diff] [blame] | 1121 | if (neigh->dead) |
| 1122 | goto out_dead; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1123 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1124 | if (!(neigh->nud_state & (NUD_STALE | NUD_INCOMPLETE))) { |
Jiri Pirko | 1f9248e | 2013-12-07 19:26:53 +0100 | [diff] [blame] | 1125 | if (NEIGH_VAR(neigh->parms, MCAST_PROBES) + |
| 1126 | NEIGH_VAR(neigh->parms, APP_PROBES)) { |
Eric Dumazet | cd28ca0 | 2011-08-09 08:15:58 +0000 | [diff] [blame] | 1127 | unsigned long next, now = jiffies; |
| 1128 | |
Jiri Pirko | 1f9248e | 2013-12-07 19:26:53 +0100 | [diff] [blame] | 1129 | atomic_set(&neigh->probes, |
| 1130 | NEIGH_VAR(neigh->parms, UCAST_PROBES)); |
Lorenzo Bianconi | 071c379 | 2019-07-14 23:36:11 +0200 | [diff] [blame] | 1131 | neigh_del_timer(neigh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1132 | neigh->nud_state = NUD_INCOMPLETE; |
Eric Dumazet | cd28ca0 | 2011-08-09 08:15:58 +0000 | [diff] [blame] | 1133 | neigh->updated = now; |
Jiri Pirko | 1f9248e | 2013-12-07 19:26:53 +0100 | [diff] [blame] | 1134 | next = now + max(NEIGH_VAR(neigh->parms, RETRANS_TIME), |
Hangbin Liu | 19e16d2 | 2020-04-01 14:46:20 +0800 | [diff] [blame] | 1135 | HZ/100); |
Eric Dumazet | cd28ca0 | 2011-08-09 08:15:58 +0000 | [diff] [blame] | 1136 | neigh_add_timer(neigh, next); |
| 1137 | immediate_probe = true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1138 | } else { |
| 1139 | neigh->nud_state = NUD_FAILED; |
YOSHIFUJI Hideaki | 955aaa2 | 2006-03-20 16:52:52 -0800 | [diff] [blame] | 1140 | neigh->updated = jiffies; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1141 | write_unlock_bh(&neigh->lock); |
| 1142 | |
Wei Yongjun | f3fbbe0 | 2009-02-25 00:37:32 +0000 | [diff] [blame] | 1143 | kfree_skb(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1144 | return 1; |
| 1145 | } |
| 1146 | } else if (neigh->nud_state & NUD_STALE) { |
Joe Perches | d5d427c | 2013-04-15 15:17:19 +0000 | [diff] [blame] | 1147 | neigh_dbg(2, "neigh %p is delayed\n", neigh); |
Lorenzo Bianconi | 071c379 | 2019-07-14 23:36:11 +0200 | [diff] [blame] | 1148 | neigh_del_timer(neigh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1149 | neigh->nud_state = NUD_DELAY; |
YOSHIFUJI Hideaki | 955aaa2 | 2006-03-20 16:52:52 -0800 | [diff] [blame] | 1150 | neigh->updated = jiffies; |
Jiri Pirko | 1f9248e | 2013-12-07 19:26:53 +0100 | [diff] [blame] | 1151 | neigh_add_timer(neigh, jiffies + |
| 1152 | NEIGH_VAR(neigh->parms, DELAY_PROBE_TIME)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1153 | } |
| 1154 | |
| 1155 | if (neigh->nud_state == NUD_INCOMPLETE) { |
| 1156 | if (skb) { |
Eric Dumazet | 8b5c171 | 2011-11-09 12:07:14 +0000 | [diff] [blame] | 1157 | while (neigh->arp_queue_len_bytes + skb->truesize > |
Jiri Pirko | 1f9248e | 2013-12-07 19:26:53 +0100 | [diff] [blame] | 1158 | NEIGH_VAR(neigh->parms, QUEUE_LEN_BYTES)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1159 | struct sk_buff *buff; |
Eric Dumazet | 8b5c171 | 2011-11-09 12:07:14 +0000 | [diff] [blame] | 1160 | |
David S. Miller | f72051b | 2008-09-23 01:11:18 -0700 | [diff] [blame] | 1161 | buff = __skb_dequeue(&neigh->arp_queue); |
Eric Dumazet | 8b5c171 | 2011-11-09 12:07:14 +0000 | [diff] [blame] | 1162 | if (!buff) |
| 1163 | break; |
| 1164 | neigh->arp_queue_len_bytes -= buff->truesize; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1165 | kfree_skb(buff); |
Neil Horman | 9a6d276 | 2008-07-16 20:50:49 -0700 | [diff] [blame] | 1166 | NEIGH_CACHE_STAT_INC(neigh->tbl, unres_discards); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1167 | } |
Eric Dumazet | a473113 | 2010-05-27 16:09:39 -0700 | [diff] [blame] | 1168 | skb_dst_force(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1169 | __skb_queue_tail(&neigh->arp_queue, skb); |
Eric Dumazet | 8b5c171 | 2011-11-09 12:07:14 +0000 | [diff] [blame] | 1170 | neigh->arp_queue_len_bytes += skb->truesize; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1171 | } |
| 1172 | rc = 1; |
| 1173 | } |
| 1174 | out_unlock_bh: |
Eric Dumazet | cd28ca0 | 2011-08-09 08:15:58 +0000 | [diff] [blame] | 1175 | if (immediate_probe) |
| 1176 | neigh_probe(neigh); |
| 1177 | else |
| 1178 | write_unlock(&neigh->lock); |
| 1179 | local_bh_enable(); |
Roopa Prabhu | 56dd18a | 2019-02-14 09:15:11 -0800 | [diff] [blame] | 1180 | trace_neigh_event_send_done(neigh, rc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1181 | return rc; |
Julian Anastasov | 2c51a97 | 2015-06-16 22:56:39 +0300 | [diff] [blame] | 1182 | |
| 1183 | out_dead: |
| 1184 | if (neigh->nud_state & NUD_STALE) |
| 1185 | goto out_unlock_bh; |
| 1186 | write_unlock_bh(&neigh->lock); |
| 1187 | kfree_skb(skb); |
Roopa Prabhu | 56dd18a | 2019-02-14 09:15:11 -0800 | [diff] [blame] | 1188 | trace_neigh_event_send_dead(neigh, 1); |
Julian Anastasov | 2c51a97 | 2015-06-16 22:56:39 +0300 | [diff] [blame] | 1189 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1190 | } |
YOSHIFUJI Hideaki | 0a20450 | 2008-03-24 18:39:10 +0900 | [diff] [blame] | 1191 | EXPORT_SYMBOL(__neigh_event_send); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1192 | |
David S. Miller | f6b72b62 | 2011-07-14 07:53:20 -0700 | [diff] [blame] | 1193 | static void neigh_update_hhs(struct neighbour *neigh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1194 | { |
| 1195 | struct hh_cache *hh; |
Stephen Hemminger | 3b04ddd | 2007-10-09 01:40:57 -0700 | [diff] [blame] | 1196 | void (*update)(struct hh_cache*, const struct net_device*, const unsigned char *) |
Doug Kehn | 91a72a7 | 2010-07-14 18:02:16 -0700 | [diff] [blame] | 1197 | = NULL; |
| 1198 | |
| 1199 | if (neigh->dev->header_ops) |
| 1200 | update = neigh->dev->header_ops->cache_update; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1201 | |
| 1202 | if (update) { |
David S. Miller | f6b72b62 | 2011-07-14 07:53:20 -0700 | [diff] [blame] | 1203 | hh = &neigh->hh; |
Eric Dumazet | c305c6ae | 2019-11-07 18:29:11 -0800 | [diff] [blame] | 1204 | if (READ_ONCE(hh->hh_len)) { |
Stephen Hemminger | 3644f0c | 2006-12-07 15:08:17 -0800 | [diff] [blame] | 1205 | write_seqlock_bh(&hh->hh_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1206 | update(hh, neigh->dev, neigh->ha); |
Stephen Hemminger | 3644f0c | 2006-12-07 15:08:17 -0800 | [diff] [blame] | 1207 | write_sequnlock_bh(&hh->hh_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1208 | } |
| 1209 | } |
| 1210 | } |
| 1211 | |
| 1212 | |
| 1213 | |
| 1214 | /* Generic update routine. |
| 1215 | -- lladdr is new lladdr or NULL, if it is not supplied. |
| 1216 | -- new is new state. |
| 1217 | -- flags |
| 1218 | NEIGH_UPDATE_F_OVERRIDE allows to override existing lladdr, |
| 1219 | if it is different. |
| 1220 | NEIGH_UPDATE_F_WEAK_OVERRIDE will suspect existing "connected" |
YOSHIFUJI Hideaki | 4ec93ed | 2007-02-09 23:24:36 +0900 | [diff] [blame] | 1221 | lladdr instead of overriding it |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1222 | if it is different. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1223 | NEIGH_UPDATE_F_ADMIN means that the change is administrative. |
| 1224 | |
YOSHIFUJI Hideaki | 4ec93ed | 2007-02-09 23:24:36 +0900 | [diff] [blame] | 1225 | NEIGH_UPDATE_F_OVERRIDE_ISROUTER allows to override existing |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1226 | NTF_ROUTER flag. |
| 1227 | NEIGH_UPDATE_F_ISROUTER indicates if the neighbour is known as |
| 1228 | a router. |
| 1229 | |
| 1230 | Caller MUST hold reference count on the entry. |
| 1231 | */ |
| 1232 | |
David Ahern | 7a35a50 | 2018-12-05 20:02:29 -0800 | [diff] [blame] | 1233 | static int __neigh_update(struct neighbour *neigh, const u8 *lladdr, |
| 1234 | u8 new, u32 flags, u32 nlmsg_pid, |
| 1235 | struct netlink_ext_ack *extack) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1236 | { |
David Ahern | e997f8a | 2018-12-11 18:57:25 -0700 | [diff] [blame] | 1237 | bool ext_learn_change = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1238 | u8 old; |
| 1239 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1240 | int notify = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1241 | struct net_device *dev; |
| 1242 | int update_isrouter = 0; |
| 1243 | |
Roopa Prabhu | 56dd18a | 2019-02-14 09:15:11 -0800 | [diff] [blame] | 1244 | trace_neigh_update(neigh, lladdr, new, flags, nlmsg_pid); |
| 1245 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1246 | write_lock_bh(&neigh->lock); |
| 1247 | |
| 1248 | dev = neigh->dev; |
| 1249 | old = neigh->nud_state; |
| 1250 | err = -EPERM; |
| 1251 | |
Chinmay Agarwal | eb4e8fa | 2021-01-27 22:24:54 +0530 | [diff] [blame] | 1252 | if (neigh->dead) { |
| 1253 | NL_SET_ERR_MSG(extack, "Neighbor entry is now dead"); |
| 1254 | new = old; |
| 1255 | goto out; |
| 1256 | } |
YOSHIFUJI Hideaki | 4ec93ed | 2007-02-09 23:24:36 +0900 | [diff] [blame] | 1257 | if (!(flags & NEIGH_UPDATE_F_ADMIN) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1258 | (old & (NUD_NOARP | NUD_PERMANENT))) |
| 1259 | goto out; |
| 1260 | |
David Ahern | e997f8a | 2018-12-11 18:57:25 -0700 | [diff] [blame] | 1261 | ext_learn_change = neigh_update_ext_learned(neigh, flags, ¬ify); |
Roopa Prabhu | 9ce33e4 | 2018-04-24 13:49:34 -0700 | [diff] [blame] | 1262 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1263 | if (!(new & NUD_VALID)) { |
| 1264 | neigh_del_timer(neigh); |
| 1265 | if (old & NUD_CONNECTED) |
| 1266 | neigh_suspect(neigh); |
David Ahern | 9c29a2f | 2018-12-11 18:57:21 -0700 | [diff] [blame] | 1267 | neigh->nud_state = new; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1268 | err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1269 | notify = old & NUD_VALID; |
Roopa Prabhu | d2fb4fb | 2018-10-20 18:09:31 -0700 | [diff] [blame] | 1270 | if ((old & (NUD_INCOMPLETE | NUD_PROBE)) && |
Timo Teras | 5ef12d9 | 2009-06-11 04:16:28 -0700 | [diff] [blame] | 1271 | (new & NUD_FAILED)) { |
| 1272 | neigh_invalidate(neigh); |
| 1273 | notify = 1; |
| 1274 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1275 | goto out; |
| 1276 | } |
| 1277 | |
| 1278 | /* Compare new lladdr with cached one */ |
| 1279 | if (!dev->addr_len) { |
| 1280 | /* First case: device needs no address. */ |
| 1281 | lladdr = neigh->ha; |
| 1282 | } else if (lladdr) { |
| 1283 | /* The second case: if something is already cached |
| 1284 | and a new address is proposed: |
| 1285 | - compare new & old |
| 1286 | - if they are different, check override flag |
| 1287 | */ |
YOSHIFUJI Hideaki | 4ec93ed | 2007-02-09 23:24:36 +0900 | [diff] [blame] | 1288 | if ((old & NUD_VALID) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1289 | !memcmp(lladdr, neigh->ha, dev->addr_len)) |
| 1290 | lladdr = neigh->ha; |
| 1291 | } else { |
| 1292 | /* No address is supplied; if we know something, |
| 1293 | use it, otherwise discard the request. |
| 1294 | */ |
| 1295 | err = -EINVAL; |
David Ahern | 7a35a50 | 2018-12-05 20:02:29 -0800 | [diff] [blame] | 1296 | if (!(old & NUD_VALID)) { |
| 1297 | NL_SET_ERR_MSG(extack, "No link layer address given"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1298 | goto out; |
David Ahern | 7a35a50 | 2018-12-05 20:02:29 -0800 | [diff] [blame] | 1299 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1300 | lladdr = neigh->ha; |
| 1301 | } |
| 1302 | |
Vasily Khoruzhick | f0e0d04 | 2018-09-13 11:12:03 -0700 | [diff] [blame] | 1303 | /* Update confirmed timestamp for neighbour entry after we |
| 1304 | * received ARP packet even if it doesn't change IP to MAC binding. |
| 1305 | */ |
| 1306 | if (new & NUD_CONNECTED) |
| 1307 | neigh->confirmed = jiffies; |
| 1308 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1309 | /* If entry was valid and address is not changed, |
| 1310 | do not change entry state, if new one is STALE. |
| 1311 | */ |
| 1312 | err = 0; |
| 1313 | update_isrouter = flags & NEIGH_UPDATE_F_OVERRIDE_ISROUTER; |
| 1314 | if (old & NUD_VALID) { |
| 1315 | if (lladdr != neigh->ha && !(flags & NEIGH_UPDATE_F_OVERRIDE)) { |
| 1316 | update_isrouter = 0; |
| 1317 | if ((flags & NEIGH_UPDATE_F_WEAK_OVERRIDE) && |
| 1318 | (old & NUD_CONNECTED)) { |
| 1319 | lladdr = neigh->ha; |
| 1320 | new = NUD_STALE; |
| 1321 | } else |
| 1322 | goto out; |
| 1323 | } else { |
Julian Anastasov | 0e7bbcc | 2016-07-27 09:56:50 +0300 | [diff] [blame] | 1324 | if (lladdr == neigh->ha && new == NUD_STALE && |
| 1325 | !(flags & NEIGH_UPDATE_F_ADMIN)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1326 | new = old; |
| 1327 | } |
| 1328 | } |
| 1329 | |
Vasily Khoruzhick | f0e0d04 | 2018-09-13 11:12:03 -0700 | [diff] [blame] | 1330 | /* Update timestamp only once we know we will make a change to the |
Ihar Hrachyshka | 77d7123 | 2017-05-16 08:44:24 -0700 | [diff] [blame] | 1331 | * neighbour entry. Otherwise we risk to move the locktime window with |
| 1332 | * noop updates and ignore relevant ARP updates. |
| 1333 | */ |
Vasily Khoruzhick | f0e0d04 | 2018-09-13 11:12:03 -0700 | [diff] [blame] | 1334 | if (new != old || lladdr != neigh->ha) |
Ihar Hrachyshka | 77d7123 | 2017-05-16 08:44:24 -0700 | [diff] [blame] | 1335 | neigh->updated = jiffies; |
Ihar Hrachyshka | 77d7123 | 2017-05-16 08:44:24 -0700 | [diff] [blame] | 1336 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1337 | if (new != old) { |
| 1338 | neigh_del_timer(neigh); |
Erik Kline | 765c9c6 | 2015-05-18 19:44:41 +0900 | [diff] [blame] | 1339 | if (new & NUD_PROBE) |
| 1340 | atomic_set(&neigh->probes, 0); |
Pavel Emelyanov | a43d899 | 2007-12-20 15:49:05 -0800 | [diff] [blame] | 1341 | if (new & NUD_IN_TIMER) |
YOSHIFUJI Hideaki | 4ec93ed | 2007-02-09 23:24:36 +0900 | [diff] [blame] | 1342 | neigh_add_timer(neigh, (jiffies + |
| 1343 | ((new & NUD_REACHABLE) ? |
David S. Miller | 667347f | 2005-09-27 12:07:44 -0700 | [diff] [blame] | 1344 | neigh->parms->reachable_time : |
| 1345 | 0))); |
David Ahern | 9c29a2f | 2018-12-11 18:57:21 -0700 | [diff] [blame] | 1346 | neigh->nud_state = new; |
Bob Gilligan | 53385d2 | 2013-12-15 13:39:56 -0800 | [diff] [blame] | 1347 | notify = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1348 | } |
| 1349 | |
| 1350 | if (lladdr != neigh->ha) { |
Eric Dumazet | 0ed8ddf | 2010-10-07 10:44:07 +0000 | [diff] [blame] | 1351 | write_seqlock(&neigh->ha_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1352 | memcpy(&neigh->ha, lladdr, dev->addr_len); |
Eric Dumazet | 0ed8ddf | 2010-10-07 10:44:07 +0000 | [diff] [blame] | 1353 | write_sequnlock(&neigh->ha_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1354 | neigh_update_hhs(neigh); |
| 1355 | if (!(new & NUD_CONNECTED)) |
| 1356 | neigh->confirmed = jiffies - |
Jiri Pirko | 1f9248e | 2013-12-07 19:26:53 +0100 | [diff] [blame] | 1357 | (NEIGH_VAR(neigh->parms, BASE_REACHABLE_TIME) << 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1358 | notify = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1359 | } |
| 1360 | if (new == old) |
| 1361 | goto out; |
| 1362 | if (new & NUD_CONNECTED) |
| 1363 | neigh_connect(neigh); |
| 1364 | else |
| 1365 | neigh_suspect(neigh); |
| 1366 | if (!(old & NUD_VALID)) { |
| 1367 | struct sk_buff *skb; |
| 1368 | |
| 1369 | /* Again: avoid dead loop if something went wrong */ |
| 1370 | |
| 1371 | while (neigh->nud_state & NUD_VALID && |
| 1372 | (skb = __skb_dequeue(&neigh->arp_queue)) != NULL) { |
David S. Miller | 69cce1d | 2011-07-17 23:09:49 -0700 | [diff] [blame] | 1373 | struct dst_entry *dst = skb_dst(skb); |
| 1374 | struct neighbour *n2, *n1 = neigh; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1375 | write_unlock_bh(&neigh->lock); |
roy.qing.li@gmail.com | e049f28 | 2011-10-17 22:32:42 +0000 | [diff] [blame] | 1376 | |
| 1377 | rcu_read_lock(); |
David S. Miller | 13a43d9 | 2012-07-02 22:15:37 -0700 | [diff] [blame] | 1378 | |
| 1379 | /* Why not just use 'neigh' as-is? The problem is that |
| 1380 | * things such as shaper, eql, and sch_teql can end up |
| 1381 | * using alternative, different, neigh objects to output |
| 1382 | * the packet in the output path. So what we need to do |
| 1383 | * here is re-lookup the top-level neigh in the path so |
| 1384 | * we can reinject the packet there. |
| 1385 | */ |
| 1386 | n2 = NULL; |
Tong Zhu | d47ec7a | 2021-03-19 14:33:37 -0400 | [diff] [blame] | 1387 | if (dst && dst->obsolete != DST_OBSOLETE_DEAD) { |
David S. Miller | 13a43d9 | 2012-07-02 22:15:37 -0700 | [diff] [blame] | 1388 | n2 = dst_neigh_lookup_skb(dst, skb); |
| 1389 | if (n2) |
| 1390 | n1 = n2; |
| 1391 | } |
David S. Miller | 8f40b16 | 2011-07-17 13:34:11 -0700 | [diff] [blame] | 1392 | n1->output(n1, skb); |
David S. Miller | 13a43d9 | 2012-07-02 22:15:37 -0700 | [diff] [blame] | 1393 | if (n2) |
| 1394 | neigh_release(n2); |
roy.qing.li@gmail.com | e049f28 | 2011-10-17 22:32:42 +0000 | [diff] [blame] | 1395 | rcu_read_unlock(); |
| 1396 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1397 | write_lock_bh(&neigh->lock); |
| 1398 | } |
Eric Dumazet | c9ab4d8 | 2013-06-28 02:37:42 -0700 | [diff] [blame] | 1399 | __skb_queue_purge(&neigh->arp_queue); |
Eric Dumazet | 8b5c171 | 2011-11-09 12:07:14 +0000 | [diff] [blame] | 1400 | neigh->arp_queue_len_bytes = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1401 | } |
| 1402 | out: |
Roopa Prabhu | fc6e807 | 2018-09-22 21:26:20 -0700 | [diff] [blame] | 1403 | if (update_isrouter) |
| 1404 | neigh_update_is_router(neigh, flags, ¬ify); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1405 | write_unlock_bh(&neigh->lock); |
Tom Tucker | 8d71740 | 2006-07-30 20:43:36 -0700 | [diff] [blame] | 1406 | |
David Ahern | e997f8a | 2018-12-11 18:57:25 -0700 | [diff] [blame] | 1407 | if (((new ^ old) & NUD_PERMANENT) || ext_learn_change) |
David Ahern | 9c29a2f | 2018-12-11 18:57:21 -0700 | [diff] [blame] | 1408 | neigh_update_gc_list(neigh); |
| 1409 | |
Tom Tucker | 8d71740 | 2006-07-30 20:43:36 -0700 | [diff] [blame] | 1410 | if (notify) |
Roopa Prabhu | 7b8f7a4 | 2017-03-19 22:01:28 -0700 | [diff] [blame] | 1411 | neigh_update_notify(neigh, nlmsg_pid); |
Thomas Graf | d961db3 | 2007-08-08 23:12:56 -0700 | [diff] [blame] | 1412 | |
Roopa Prabhu | 56dd18a | 2019-02-14 09:15:11 -0800 | [diff] [blame] | 1413 | trace_neigh_update_done(neigh, err); |
| 1414 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1415 | return err; |
| 1416 | } |
David Ahern | 7a35a50 | 2018-12-05 20:02:29 -0800 | [diff] [blame] | 1417 | |
| 1418 | int neigh_update(struct neighbour *neigh, const u8 *lladdr, u8 new, |
| 1419 | u32 flags, u32 nlmsg_pid) |
| 1420 | { |
| 1421 | return __neigh_update(neigh, lladdr, new, flags, nlmsg_pid, NULL); |
| 1422 | } |
YOSHIFUJI Hideaki | 0a20450 | 2008-03-24 18:39:10 +0900 | [diff] [blame] | 1423 | EXPORT_SYMBOL(neigh_update); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1424 | |
Jiri Benc | 7e98056 | 2013-12-11 13:48:20 +0100 | [diff] [blame] | 1425 | /* Update the neigh to listen temporarily for probe responses, even if it is |
| 1426 | * in a NUD_FAILED state. The caller has to hold neigh->lock for writing. |
| 1427 | */ |
| 1428 | void __neigh_set_probe_once(struct neighbour *neigh) |
| 1429 | { |
Julian Anastasov | 2c51a97 | 2015-06-16 22:56:39 +0300 | [diff] [blame] | 1430 | if (neigh->dead) |
| 1431 | return; |
Jiri Benc | 7e98056 | 2013-12-11 13:48:20 +0100 | [diff] [blame] | 1432 | neigh->updated = jiffies; |
| 1433 | if (!(neigh->nud_state & NUD_FAILED)) |
| 1434 | return; |
Duan Jiong | 2176d5d | 2014-05-09 13:16:48 +0800 | [diff] [blame] | 1435 | neigh->nud_state = NUD_INCOMPLETE; |
| 1436 | atomic_set(&neigh->probes, neigh_max_probes(neigh)); |
Jiri Benc | 7e98056 | 2013-12-11 13:48:20 +0100 | [diff] [blame] | 1437 | neigh_add_timer(neigh, |
Hangbin Liu | 19e16d2 | 2020-04-01 14:46:20 +0800 | [diff] [blame] | 1438 | jiffies + max(NEIGH_VAR(neigh->parms, RETRANS_TIME), |
| 1439 | HZ/100)); |
Jiri Benc | 7e98056 | 2013-12-11 13:48:20 +0100 | [diff] [blame] | 1440 | } |
| 1441 | EXPORT_SYMBOL(__neigh_set_probe_once); |
| 1442 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1443 | struct neighbour *neigh_event_ns(struct neigh_table *tbl, |
| 1444 | u8 *lladdr, void *saddr, |
| 1445 | struct net_device *dev) |
| 1446 | { |
| 1447 | struct neighbour *neigh = __neigh_lookup(tbl, saddr, dev, |
| 1448 | lladdr || !dev->addr_len); |
| 1449 | if (neigh) |
YOSHIFUJI Hideaki | 4ec93ed | 2007-02-09 23:24:36 +0900 | [diff] [blame] | 1450 | neigh_update(neigh, lladdr, NUD_STALE, |
Roopa Prabhu | 7b8f7a4 | 2017-03-19 22:01:28 -0700 | [diff] [blame] | 1451 | NEIGH_UPDATE_F_OVERRIDE, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1452 | return neigh; |
| 1453 | } |
YOSHIFUJI Hideaki | 0a20450 | 2008-03-24 18:39:10 +0900 | [diff] [blame] | 1454 | EXPORT_SYMBOL(neigh_event_ns); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1455 | |
Eric Dumazet | 34d101d | 2010-10-11 09:16:57 -0700 | [diff] [blame] | 1456 | /* called with read_lock_bh(&n->lock); */ |
Eric W. Biederman | bdf53c5 | 2015-03-02 00:13:22 -0600 | [diff] [blame] | 1457 | static void neigh_hh_init(struct neighbour *n) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1458 | { |
Eric W. Biederman | bdf53c5 | 2015-03-02 00:13:22 -0600 | [diff] [blame] | 1459 | struct net_device *dev = n->dev; |
| 1460 | __be16 prot = n->tbl->protocol; |
David S. Miller | f6b72b62 | 2011-07-14 07:53:20 -0700 | [diff] [blame] | 1461 | struct hh_cache *hh = &n->hh; |
Eric Dumazet | 0ed8ddf | 2010-10-07 10:44:07 +0000 | [diff] [blame] | 1462 | |
| 1463 | write_lock_bh(&n->lock); |
Eric Dumazet | 34d101d | 2010-10-11 09:16:57 -0700 | [diff] [blame] | 1464 | |
David S. Miller | f6b72b62 | 2011-07-14 07:53:20 -0700 | [diff] [blame] | 1465 | /* Only one thread can come in here and initialize the |
| 1466 | * hh_cache entry. |
| 1467 | */ |
David S. Miller | b23b545 | 2011-07-16 17:45:02 -0700 | [diff] [blame] | 1468 | if (!hh->hh_len) |
| 1469 | dev->header_ops->cache(n, hh, prot); |
David S. Miller | f6b72b62 | 2011-07-14 07:53:20 -0700 | [diff] [blame] | 1470 | |
Eric Dumazet | 0ed8ddf | 2010-10-07 10:44:07 +0000 | [diff] [blame] | 1471 | write_unlock_bh(&n->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1472 | } |
| 1473 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1474 | /* Slow and careful. */ |
| 1475 | |
David S. Miller | 8f40b16 | 2011-07-17 13:34:11 -0700 | [diff] [blame] | 1476 | int neigh_resolve_output(struct neighbour *neigh, struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1477 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1478 | int rc = 0; |
| 1479 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1480 | if (!neigh_event_send(neigh, skb)) { |
| 1481 | int err; |
| 1482 | struct net_device *dev = neigh->dev; |
Eric Dumazet | 0ed8ddf | 2010-10-07 10:44:07 +0000 | [diff] [blame] | 1483 | unsigned int seq; |
Eric Dumazet | 34d101d | 2010-10-11 09:16:57 -0700 | [diff] [blame] | 1484 | |
Eric Dumazet | c305c6ae | 2019-11-07 18:29:11 -0800 | [diff] [blame] | 1485 | if (dev->header_ops->cache && !READ_ONCE(neigh->hh.hh_len)) |
Eric W. Biederman | bdf53c5 | 2015-03-02 00:13:22 -0600 | [diff] [blame] | 1486 | neigh_hh_init(neigh); |
Eric Dumazet | 34d101d | 2010-10-11 09:16:57 -0700 | [diff] [blame] | 1487 | |
Eric Dumazet | 0ed8ddf | 2010-10-07 10:44:07 +0000 | [diff] [blame] | 1488 | do { |
ramesh.nagappa@gmail.com | e1f1650 | 2012-10-05 19:10:15 +0000 | [diff] [blame] | 1489 | __skb_pull(skb, skb_network_offset(skb)); |
Eric Dumazet | 0ed8ddf | 2010-10-07 10:44:07 +0000 | [diff] [blame] | 1490 | seq = read_seqbegin(&neigh->ha_lock); |
| 1491 | err = dev_hard_header(skb, dev, ntohs(skb->protocol), |
| 1492 | neigh->ha, NULL, skb->len); |
| 1493 | } while (read_seqretry(&neigh->ha_lock, seq)); |
Eric Dumazet | 34d101d | 2010-10-11 09:16:57 -0700 | [diff] [blame] | 1494 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1495 | if (err >= 0) |
David S. Miller | 542d4d6 | 2011-07-16 18:06:24 -0700 | [diff] [blame] | 1496 | rc = dev_queue_xmit(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1497 | else |
| 1498 | goto out_kfree_skb; |
| 1499 | } |
| 1500 | out: |
| 1501 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1502 | out_kfree_skb: |
| 1503 | rc = -EINVAL; |
| 1504 | kfree_skb(skb); |
| 1505 | goto out; |
| 1506 | } |
YOSHIFUJI Hideaki | 0a20450 | 2008-03-24 18:39:10 +0900 | [diff] [blame] | 1507 | EXPORT_SYMBOL(neigh_resolve_output); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1508 | |
| 1509 | /* As fast as possible without hh cache */ |
| 1510 | |
David S. Miller | 8f40b16 | 2011-07-17 13:34:11 -0700 | [diff] [blame] | 1511 | int neigh_connected_output(struct neighbour *neigh, struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1512 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1513 | struct net_device *dev = neigh->dev; |
Eric Dumazet | 0ed8ddf | 2010-10-07 10:44:07 +0000 | [diff] [blame] | 1514 | unsigned int seq; |
David S. Miller | 8f40b16 | 2011-07-17 13:34:11 -0700 | [diff] [blame] | 1515 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1516 | |
Eric Dumazet | 0ed8ddf | 2010-10-07 10:44:07 +0000 | [diff] [blame] | 1517 | do { |
ramesh.nagappa@gmail.com | e1f1650 | 2012-10-05 19:10:15 +0000 | [diff] [blame] | 1518 | __skb_pull(skb, skb_network_offset(skb)); |
Eric Dumazet | 0ed8ddf | 2010-10-07 10:44:07 +0000 | [diff] [blame] | 1519 | seq = read_seqbegin(&neigh->ha_lock); |
| 1520 | err = dev_hard_header(skb, dev, ntohs(skb->protocol), |
| 1521 | neigh->ha, NULL, skb->len); |
| 1522 | } while (read_seqretry(&neigh->ha_lock, seq)); |
| 1523 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1524 | if (err >= 0) |
David S. Miller | 542d4d6 | 2011-07-16 18:06:24 -0700 | [diff] [blame] | 1525 | err = dev_queue_xmit(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1526 | else { |
| 1527 | err = -EINVAL; |
| 1528 | kfree_skb(skb); |
| 1529 | } |
| 1530 | return err; |
| 1531 | } |
YOSHIFUJI Hideaki | 0a20450 | 2008-03-24 18:39:10 +0900 | [diff] [blame] | 1532 | EXPORT_SYMBOL(neigh_connected_output); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1533 | |
David S. Miller | 8f40b16 | 2011-07-17 13:34:11 -0700 | [diff] [blame] | 1534 | int neigh_direct_output(struct neighbour *neigh, struct sk_buff *skb) |
| 1535 | { |
| 1536 | return dev_queue_xmit(skb); |
| 1537 | } |
| 1538 | EXPORT_SYMBOL(neigh_direct_output); |
| 1539 | |
Kees Cook | e99e88a | 2017-10-16 14:43:17 -0700 | [diff] [blame] | 1540 | static void neigh_proxy_process(struct timer_list *t) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1541 | { |
Kees Cook | e99e88a | 2017-10-16 14:43:17 -0700 | [diff] [blame] | 1542 | struct neigh_table *tbl = from_timer(tbl, t, proxy_timer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1543 | long sched_next = 0; |
| 1544 | unsigned long now = jiffies; |
David S. Miller | f72051b | 2008-09-23 01:11:18 -0700 | [diff] [blame] | 1545 | struct sk_buff *skb, *n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1546 | |
| 1547 | spin_lock(&tbl->proxy_queue.lock); |
| 1548 | |
David S. Miller | f72051b | 2008-09-23 01:11:18 -0700 | [diff] [blame] | 1549 | skb_queue_walk_safe(&tbl->proxy_queue, skb, n) { |
| 1550 | long tdif = NEIGH_CB(skb)->sched_next - now; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1551 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1552 | if (tdif <= 0) { |
David S. Miller | f72051b | 2008-09-23 01:11:18 -0700 | [diff] [blame] | 1553 | struct net_device *dev = skb->dev; |
Eric Dumazet | 20e6074 | 2011-08-22 19:32:42 +0000 | [diff] [blame] | 1554 | |
David S. Miller | f72051b | 2008-09-23 01:11:18 -0700 | [diff] [blame] | 1555 | __skb_unlink(skb, &tbl->proxy_queue); |
Eric Dumazet | 20e6074 | 2011-08-22 19:32:42 +0000 | [diff] [blame] | 1556 | if (tbl->proxy_redo && netif_running(dev)) { |
| 1557 | rcu_read_lock(); |
David S. Miller | f72051b | 2008-09-23 01:11:18 -0700 | [diff] [blame] | 1558 | tbl->proxy_redo(skb); |
Eric Dumazet | 20e6074 | 2011-08-22 19:32:42 +0000 | [diff] [blame] | 1559 | rcu_read_unlock(); |
| 1560 | } else { |
David S. Miller | f72051b | 2008-09-23 01:11:18 -0700 | [diff] [blame] | 1561 | kfree_skb(skb); |
Eric Dumazet | 20e6074 | 2011-08-22 19:32:42 +0000 | [diff] [blame] | 1562 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1563 | |
| 1564 | dev_put(dev); |
| 1565 | } else if (!sched_next || tdif < sched_next) |
| 1566 | sched_next = tdif; |
| 1567 | } |
| 1568 | del_timer(&tbl->proxy_timer); |
| 1569 | if (sched_next) |
| 1570 | mod_timer(&tbl->proxy_timer, jiffies + sched_next); |
| 1571 | spin_unlock(&tbl->proxy_queue.lock); |
| 1572 | } |
| 1573 | |
| 1574 | void pneigh_enqueue(struct neigh_table *tbl, struct neigh_parms *p, |
| 1575 | struct sk_buff *skb) |
| 1576 | { |
weichenchen | a533b70 | 2020-12-25 13:44:45 +0800 | [diff] [blame] | 1577 | unsigned long sched_next = jiffies + |
| 1578 | prandom_u32_max(NEIGH_VAR(p, PROXY_DELAY)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1579 | |
Jiri Pirko | 1f9248e | 2013-12-07 19:26:53 +0100 | [diff] [blame] | 1580 | if (tbl->proxy_queue.qlen > NEIGH_VAR(p, PROXY_QLEN)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1581 | kfree_skb(skb); |
| 1582 | return; |
| 1583 | } |
Patrick McHardy | a61bbcf | 2005-08-14 17:24:31 -0700 | [diff] [blame] | 1584 | |
| 1585 | NEIGH_CB(skb)->sched_next = sched_next; |
| 1586 | NEIGH_CB(skb)->flags |= LOCALLY_ENQUEUED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1587 | |
| 1588 | spin_lock(&tbl->proxy_queue.lock); |
| 1589 | if (del_timer(&tbl->proxy_timer)) { |
| 1590 | if (time_before(tbl->proxy_timer.expires, sched_next)) |
| 1591 | sched_next = tbl->proxy_timer.expires; |
| 1592 | } |
Eric Dumazet | adf3090 | 2009-06-02 05:19:30 +0000 | [diff] [blame] | 1593 | skb_dst_drop(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1594 | dev_hold(skb->dev); |
| 1595 | __skb_queue_tail(&tbl->proxy_queue, skb); |
| 1596 | mod_timer(&tbl->proxy_timer, sched_next); |
| 1597 | spin_unlock(&tbl->proxy_queue.lock); |
| 1598 | } |
YOSHIFUJI Hideaki | 0a20450 | 2008-03-24 18:39:10 +0900 | [diff] [blame] | 1599 | EXPORT_SYMBOL(pneigh_enqueue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1600 | |
Tobias Klauser | 97fd5bc | 2009-07-13 11:17:49 -0700 | [diff] [blame] | 1601 | static inline struct neigh_parms *lookup_neigh_parms(struct neigh_table *tbl, |
Eric W. Biederman | 426b530 | 2008-01-24 00:13:18 -0800 | [diff] [blame] | 1602 | struct net *net, int ifindex) |
| 1603 | { |
| 1604 | struct neigh_parms *p; |
| 1605 | |
Nicolas Dichtel | 75fbfd3 | 2014-10-29 19:29:31 +0100 | [diff] [blame] | 1606 | list_for_each_entry(p, &tbl->parms_list, list) { |
YOSHIFUJI Hideaki | 878628f | 2008-03-26 03:57:35 +0900 | [diff] [blame] | 1607 | if ((p->dev && p->dev->ifindex == ifindex && net_eq(neigh_parms_net(p), net)) || |
Gao feng | 170d6f9 | 2013-06-20 10:01:33 +0800 | [diff] [blame] | 1608 | (!p->dev && !ifindex && net_eq(net, &init_net))) |
Eric W. Biederman | 426b530 | 2008-01-24 00:13:18 -0800 | [diff] [blame] | 1609 | return p; |
| 1610 | } |
| 1611 | |
| 1612 | return NULL; |
| 1613 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1614 | |
| 1615 | struct neigh_parms *neigh_parms_alloc(struct net_device *dev, |
| 1616 | struct neigh_table *tbl) |
| 1617 | { |
Gao feng | cf89d6b | 2013-06-20 10:01:32 +0800 | [diff] [blame] | 1618 | struct neigh_parms *p; |
Stephen Hemminger | 0082982 | 2008-11-20 20:14:53 -0800 | [diff] [blame] | 1619 | struct net *net = dev_net(dev); |
| 1620 | const struct net_device_ops *ops = dev->netdev_ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1621 | |
Gao feng | cf89d6b | 2013-06-20 10:01:32 +0800 | [diff] [blame] | 1622 | p = kmemdup(&tbl->parms, sizeof(*p), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1623 | if (p) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1624 | p->tbl = tbl; |
Reshetova, Elena | 6343944 | 2017-06-30 13:07:56 +0300 | [diff] [blame] | 1625 | refcount_set(&p->refcnt, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1626 | p->reachable_time = |
Jiri Pirko | 1f9248e | 2013-12-07 19:26:53 +0100 | [diff] [blame] | 1627 | neigh_rand_reach_time(NEIGH_VAR(p, BASE_REACHABLE_TIME)); |
Denis V. Lunev | 486b51d | 2008-01-14 22:59:59 -0800 | [diff] [blame] | 1628 | dev_hold(dev); |
| 1629 | p->dev = dev; |
Eric W. Biederman | efd7ef1 | 2015-03-11 23:04:08 -0500 | [diff] [blame] | 1630 | write_pnet(&p->net, net); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1631 | p->sysctl_table = NULL; |
Veaceslav Falico | 6313480 | 2013-08-02 19:07:38 +0200 | [diff] [blame] | 1632 | |
| 1633 | if (ops->ndo_neigh_setup && ops->ndo_neigh_setup(dev, p)) { |
Veaceslav Falico | 6313480 | 2013-08-02 19:07:38 +0200 | [diff] [blame] | 1634 | dev_put(dev); |
| 1635 | kfree(p); |
| 1636 | return NULL; |
| 1637 | } |
| 1638 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1639 | write_lock_bh(&tbl->lock); |
Nicolas Dichtel | 75fbfd3 | 2014-10-29 19:29:31 +0100 | [diff] [blame] | 1640 | list_add(&p->list, &tbl->parms.list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1641 | write_unlock_bh(&tbl->lock); |
Jiri Pirko | 1d4c8c2 | 2013-12-07 19:26:56 +0100 | [diff] [blame] | 1642 | |
| 1643 | neigh_parms_data_state_cleanall(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1644 | } |
| 1645 | return p; |
| 1646 | } |
YOSHIFUJI Hideaki | 0a20450 | 2008-03-24 18:39:10 +0900 | [diff] [blame] | 1647 | EXPORT_SYMBOL(neigh_parms_alloc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1648 | |
| 1649 | static void neigh_rcu_free_parms(struct rcu_head *head) |
| 1650 | { |
| 1651 | struct neigh_parms *parms = |
| 1652 | container_of(head, struct neigh_parms, rcu_head); |
| 1653 | |
| 1654 | neigh_parms_put(parms); |
| 1655 | } |
| 1656 | |
| 1657 | void neigh_parms_release(struct neigh_table *tbl, struct neigh_parms *parms) |
| 1658 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1659 | if (!parms || parms == &tbl->parms) |
| 1660 | return; |
| 1661 | write_lock_bh(&tbl->lock); |
Nicolas Dichtel | 75fbfd3 | 2014-10-29 19:29:31 +0100 | [diff] [blame] | 1662 | list_del(&parms->list); |
| 1663 | parms->dead = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1664 | write_unlock_bh(&tbl->lock); |
Nicolas Dichtel | 75fbfd3 | 2014-10-29 19:29:31 +0100 | [diff] [blame] | 1665 | if (parms->dev) |
| 1666 | dev_put(parms->dev); |
| 1667 | call_rcu(&parms->rcu_head, neigh_rcu_free_parms); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1668 | } |
YOSHIFUJI Hideaki | 0a20450 | 2008-03-24 18:39:10 +0900 | [diff] [blame] | 1669 | EXPORT_SYMBOL(neigh_parms_release); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1670 | |
Denis V. Lunev | 06f0511 | 2008-01-24 00:30:58 -0800 | [diff] [blame] | 1671 | static void neigh_parms_destroy(struct neigh_parms *parms) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1672 | { |
| 1673 | kfree(parms); |
| 1674 | } |
| 1675 | |
Pavel Emelianov | c2ecba7 | 2007-04-17 12:45:31 -0700 | [diff] [blame] | 1676 | static struct lock_class_key neigh_table_proxy_queue_class; |
| 1677 | |
WANG Cong | d7480fd | 2014-11-10 15:59:36 -0800 | [diff] [blame] | 1678 | static struct neigh_table *neigh_tables[NEIGH_NR_TABLES] __read_mostly; |
| 1679 | |
| 1680 | void neigh_table_init(int index, struct neigh_table *tbl) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1681 | { |
| 1682 | unsigned long now = jiffies; |
| 1683 | unsigned long phsize; |
| 1684 | |
Nicolas Dichtel | 75fbfd3 | 2014-10-29 19:29:31 +0100 | [diff] [blame] | 1685 | INIT_LIST_HEAD(&tbl->parms_list); |
David Ahern | 5895631 | 2018-12-07 12:24:57 -0800 | [diff] [blame] | 1686 | INIT_LIST_HEAD(&tbl->gc_list); |
Nicolas Dichtel | 75fbfd3 | 2014-10-29 19:29:31 +0100 | [diff] [blame] | 1687 | list_add(&tbl->parms.list, &tbl->parms_list); |
Eric Dumazet | e42ea98 | 2008-11-12 00:54:54 -0800 | [diff] [blame] | 1688 | write_pnet(&tbl->parms.net, &init_net); |
Reshetova, Elena | 6343944 | 2017-06-30 13:07:56 +0300 | [diff] [blame] | 1689 | refcount_set(&tbl->parms.refcnt, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1690 | tbl->parms.reachable_time = |
Jiri Pirko | 1f9248e | 2013-12-07 19:26:53 +0100 | [diff] [blame] | 1691 | neigh_rand_reach_time(NEIGH_VAR(&tbl->parms, BASE_REACHABLE_TIME)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1692 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1693 | tbl->stats = alloc_percpu(struct neigh_statistics); |
| 1694 | if (!tbl->stats) |
| 1695 | panic("cannot create neighbour cache statistics"); |
YOSHIFUJI Hideaki | 4ec93ed | 2007-02-09 23:24:36 +0900 | [diff] [blame] | 1696 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1697 | #ifdef CONFIG_PROC_FS |
Christoph Hellwig | 71a5053 | 2018-04-15 10:16:41 +0200 | [diff] [blame] | 1698 | if (!proc_create_seq_data(tbl->id, 0, init_net.proc_net_stat, |
| 1699 | &neigh_stat_seq_ops, tbl)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1700 | panic("cannot create neighbour proc dir entry"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1701 | #endif |
| 1702 | |
David S. Miller | cd08933 | 2011-07-11 01:28:12 -0700 | [diff] [blame] | 1703 | RCU_INIT_POINTER(tbl->nht, neigh_hash_alloc(3)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1704 | |
| 1705 | phsize = (PNEIGH_HASHMASK + 1) * sizeof(struct pneigh_entry *); |
Andrew Morton | 77d04bd | 2006-04-07 14:52:59 -0700 | [diff] [blame] | 1706 | tbl->phash_buckets = kzalloc(phsize, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1707 | |
Eric Dumazet | d6bf781 | 2010-10-04 06:15:44 +0000 | [diff] [blame] | 1708 | if (!tbl->nht || !tbl->phash_buckets) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1709 | panic("cannot allocate neighbour cache hashes"); |
| 1710 | |
YOSHIFUJI Hideaki / 吉藤英明 | 08433ef | 2013-01-24 00:44:23 +0000 | [diff] [blame] | 1711 | if (!tbl->entry_size) |
| 1712 | tbl->entry_size = ALIGN(offsetof(struct neighbour, primary_key) + |
| 1713 | tbl->key_len, NEIGH_PRIV_ALIGN); |
| 1714 | else |
| 1715 | WARN_ON(tbl->entry_size % NEIGH_PRIV_ALIGN); |
| 1716 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1717 | rwlock_init(&tbl->lock); |
Tejun Heo | 203b42f | 2012-08-21 13:18:23 -0700 | [diff] [blame] | 1718 | INIT_DEFERRABLE_WORK(&tbl->gc_work, neigh_periodic_work); |
viresh kumar | f618002 | 2014-01-22 12:23:33 +0530 | [diff] [blame] | 1719 | queue_delayed_work(system_power_efficient_wq, &tbl->gc_work, |
| 1720 | tbl->parms.reachable_time); |
Kees Cook | e99e88a | 2017-10-16 14:43:17 -0700 | [diff] [blame] | 1721 | timer_setup(&tbl->proxy_timer, neigh_proxy_process, 0); |
Pavel Emelianov | c2ecba7 | 2007-04-17 12:45:31 -0700 | [diff] [blame] | 1722 | skb_queue_head_init_class(&tbl->proxy_queue, |
| 1723 | &neigh_table_proxy_queue_class); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1724 | |
| 1725 | tbl->last_flush = now; |
| 1726 | tbl->last_rand = now + tbl->parms.reachable_time * 20; |
Simon Kelley | bd89efc | 2006-05-12 14:56:08 -0700 | [diff] [blame] | 1727 | |
WANG Cong | d7480fd | 2014-11-10 15:59:36 -0800 | [diff] [blame] | 1728 | neigh_tables[index] = tbl; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1729 | } |
YOSHIFUJI Hideaki | 0a20450 | 2008-03-24 18:39:10 +0900 | [diff] [blame] | 1730 | EXPORT_SYMBOL(neigh_table_init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1731 | |
WANG Cong | d7480fd | 2014-11-10 15:59:36 -0800 | [diff] [blame] | 1732 | int neigh_table_clear(int index, struct neigh_table *tbl) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1733 | { |
WANG Cong | d7480fd | 2014-11-10 15:59:36 -0800 | [diff] [blame] | 1734 | neigh_tables[index] = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1735 | /* It is not clean... Fix it to unload IPv6 module safely */ |
Tejun Heo | a5c30b3 | 2010-10-19 06:04:42 +0000 | [diff] [blame] | 1736 | cancel_delayed_work_sync(&tbl->gc_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1737 | del_timer_sync(&tbl->proxy_timer); |
| 1738 | pneigh_queue_purge(&tbl->proxy_queue); |
| 1739 | neigh_ifdown(tbl, NULL); |
| 1740 | if (atomic_read(&tbl->entries)) |
Joe Perches | e005d19 | 2012-05-16 19:58:40 +0000 | [diff] [blame] | 1741 | pr_crit("neighbour leakage\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1742 | |
Eric Dumazet | 6193d2b | 2011-01-19 22:02:47 +0000 | [diff] [blame] | 1743 | call_rcu(&rcu_dereference_protected(tbl->nht, 1)->rcu, |
| 1744 | neigh_hash_free_rcu); |
Eric Dumazet | d6bf781 | 2010-10-04 06:15:44 +0000 | [diff] [blame] | 1745 | tbl->nht = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1746 | |
| 1747 | kfree(tbl->phash_buckets); |
| 1748 | tbl->phash_buckets = NULL; |
| 1749 | |
Alexey Dobriyan | 3f192b5 | 2007-11-05 21:28:13 -0800 | [diff] [blame] | 1750 | remove_proc_entry(tbl->id, init_net.proc_net_stat); |
| 1751 | |
Kirill Korotaev | 3fcde74 | 2006-09-01 01:34:10 -0700 | [diff] [blame] | 1752 | free_percpu(tbl->stats); |
| 1753 | tbl->stats = NULL; |
| 1754 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1755 | return 0; |
| 1756 | } |
YOSHIFUJI Hideaki | 0a20450 | 2008-03-24 18:39:10 +0900 | [diff] [blame] | 1757 | EXPORT_SYMBOL(neigh_table_clear); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1758 | |
WANG Cong | d7480fd | 2014-11-10 15:59:36 -0800 | [diff] [blame] | 1759 | static struct neigh_table *neigh_find_table(int family) |
| 1760 | { |
| 1761 | struct neigh_table *tbl = NULL; |
| 1762 | |
| 1763 | switch (family) { |
| 1764 | case AF_INET: |
| 1765 | tbl = neigh_tables[NEIGH_ARP_TABLE]; |
| 1766 | break; |
| 1767 | case AF_INET6: |
| 1768 | tbl = neigh_tables[NEIGH_ND_TABLE]; |
| 1769 | break; |
| 1770 | case AF_DECnet: |
| 1771 | tbl = neigh_tables[NEIGH_DN_TABLE]; |
| 1772 | break; |
| 1773 | } |
| 1774 | |
| 1775 | return tbl; |
| 1776 | } |
| 1777 | |
Roopa Prabhu | 82cbb5c | 2018-12-19 12:51:38 -0800 | [diff] [blame] | 1778 | const struct nla_policy nda_policy[NDA_MAX+1] = { |
Roopa Prabhu | 1274e1c | 2020-05-21 22:26:14 -0700 | [diff] [blame] | 1779 | [NDA_UNSPEC] = { .strict_start_type = NDA_NH_ID }, |
Roopa Prabhu | 82cbb5c | 2018-12-19 12:51:38 -0800 | [diff] [blame] | 1780 | [NDA_DST] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN }, |
| 1781 | [NDA_LLADDR] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN }, |
| 1782 | [NDA_CACHEINFO] = { .len = sizeof(struct nda_cacheinfo) }, |
| 1783 | [NDA_PROBES] = { .type = NLA_U32 }, |
| 1784 | [NDA_VLAN] = { .type = NLA_U16 }, |
| 1785 | [NDA_PORT] = { .type = NLA_U16 }, |
| 1786 | [NDA_VNI] = { .type = NLA_U32 }, |
| 1787 | [NDA_IFINDEX] = { .type = NLA_U32 }, |
| 1788 | [NDA_MASTER] = { .type = NLA_U32 }, |
David Ahern | a9cd343 | 2018-12-19 20:02:36 -0800 | [diff] [blame] | 1789 | [NDA_PROTOCOL] = { .type = NLA_U8 }, |
Roopa Prabhu | 1274e1c | 2020-05-21 22:26:14 -0700 | [diff] [blame] | 1790 | [NDA_NH_ID] = { .type = NLA_U32 }, |
Nikolay Aleksandrov | 899426b | 2020-06-23 23:47:16 +0300 | [diff] [blame] | 1791 | [NDA_FDB_EXT_ATTRS] = { .type = NLA_NESTED }, |
Roopa Prabhu | 82cbb5c | 2018-12-19 12:51:38 -0800 | [diff] [blame] | 1792 | }; |
| 1793 | |
David Ahern | c21ef3e | 2017-04-16 09:48:24 -0700 | [diff] [blame] | 1794 | static int neigh_delete(struct sk_buff *skb, struct nlmsghdr *nlh, |
| 1795 | struct netlink_ext_ack *extack) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1796 | { |
YOSHIFUJI Hideaki | 3b1e0a6 | 2008-03-26 02:26:21 +0900 | [diff] [blame] | 1797 | struct net *net = sock_net(skb->sk); |
Thomas Graf | a14a49d | 2006-08-07 17:53:08 -0700 | [diff] [blame] | 1798 | struct ndmsg *ndm; |
| 1799 | struct nlattr *dst_attr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1800 | struct neigh_table *tbl; |
WANG Cong | d7480fd | 2014-11-10 15:59:36 -0800 | [diff] [blame] | 1801 | struct neighbour *neigh; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1802 | struct net_device *dev = NULL; |
Thomas Graf | a14a49d | 2006-08-07 17:53:08 -0700 | [diff] [blame] | 1803 | int err = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1804 | |
Eric Dumazet | 110b249 | 2010-10-04 04:27:36 +0000 | [diff] [blame] | 1805 | ASSERT_RTNL(); |
Thomas Graf | a14a49d | 2006-08-07 17:53:08 -0700 | [diff] [blame] | 1806 | if (nlmsg_len(nlh) < sizeof(*ndm)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1807 | goto out; |
| 1808 | |
Thomas Graf | a14a49d | 2006-08-07 17:53:08 -0700 | [diff] [blame] | 1809 | dst_attr = nlmsg_find_attr(nlh, sizeof(*ndm), NDA_DST); |
David Ahern | 7a35a50 | 2018-12-05 20:02:29 -0800 | [diff] [blame] | 1810 | if (!dst_attr) { |
| 1811 | NL_SET_ERR_MSG(extack, "Network address not specified"); |
Thomas Graf | a14a49d | 2006-08-07 17:53:08 -0700 | [diff] [blame] | 1812 | goto out; |
David Ahern | 7a35a50 | 2018-12-05 20:02:29 -0800 | [diff] [blame] | 1813 | } |
Thomas Graf | a14a49d | 2006-08-07 17:53:08 -0700 | [diff] [blame] | 1814 | |
| 1815 | ndm = nlmsg_data(nlh); |
| 1816 | if (ndm->ndm_ifindex) { |
Eric Dumazet | 110b249 | 2010-10-04 04:27:36 +0000 | [diff] [blame] | 1817 | dev = __dev_get_by_index(net, ndm->ndm_ifindex); |
Thomas Graf | a14a49d | 2006-08-07 17:53:08 -0700 | [diff] [blame] | 1818 | if (dev == NULL) { |
| 1819 | err = -ENODEV; |
| 1820 | goto out; |
| 1821 | } |
| 1822 | } |
| 1823 | |
WANG Cong | d7480fd | 2014-11-10 15:59:36 -0800 | [diff] [blame] | 1824 | tbl = neigh_find_table(ndm->ndm_family); |
| 1825 | if (tbl == NULL) |
| 1826 | return -EAFNOSUPPORT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1827 | |
David Ahern | 7a35a50 | 2018-12-05 20:02:29 -0800 | [diff] [blame] | 1828 | if (nla_len(dst_attr) < (int)tbl->key_len) { |
| 1829 | NL_SET_ERR_MSG(extack, "Invalid network address"); |
WANG Cong | d7480fd | 2014-11-10 15:59:36 -0800 | [diff] [blame] | 1830 | goto out; |
David Ahern | 7a35a50 | 2018-12-05 20:02:29 -0800 | [diff] [blame] | 1831 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1832 | |
WANG Cong | d7480fd | 2014-11-10 15:59:36 -0800 | [diff] [blame] | 1833 | if (ndm->ndm_flags & NTF_PROXY) { |
| 1834 | err = pneigh_delete(tbl, net, nla_data(dst_attr), dev); |
Eric Dumazet | 110b249 | 2010-10-04 04:27:36 +0000 | [diff] [blame] | 1835 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1836 | } |
WANG Cong | d7480fd | 2014-11-10 15:59:36 -0800 | [diff] [blame] | 1837 | |
| 1838 | if (dev == NULL) |
| 1839 | goto out; |
| 1840 | |
| 1841 | neigh = neigh_lookup(tbl, nla_data(dst_attr), dev); |
| 1842 | if (neigh == NULL) { |
| 1843 | err = -ENOENT; |
| 1844 | goto out; |
| 1845 | } |
| 1846 | |
David Ahern | 7a35a50 | 2018-12-05 20:02:29 -0800 | [diff] [blame] | 1847 | err = __neigh_update(neigh, NULL, NUD_FAILED, |
| 1848 | NEIGH_UPDATE_F_OVERRIDE | NEIGH_UPDATE_F_ADMIN, |
| 1849 | NETLINK_CB(skb).portid, extack); |
Sowmini Varadhan | 5071034 | 2017-06-02 09:01:49 -0700 | [diff] [blame] | 1850 | write_lock_bh(&tbl->lock); |
WANG Cong | d7480fd | 2014-11-10 15:59:36 -0800 | [diff] [blame] | 1851 | neigh_release(neigh); |
Sowmini Varadhan | 5071034 | 2017-06-02 09:01:49 -0700 | [diff] [blame] | 1852 | neigh_remove_one(neigh, tbl); |
| 1853 | write_unlock_bh(&tbl->lock); |
Thomas Graf | a14a49d | 2006-08-07 17:53:08 -0700 | [diff] [blame] | 1854 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1855 | out: |
| 1856 | return err; |
| 1857 | } |
| 1858 | |
David Ahern | c21ef3e | 2017-04-16 09:48:24 -0700 | [diff] [blame] | 1859 | static int neigh_add(struct sk_buff *skb, struct nlmsghdr *nlh, |
| 1860 | struct netlink_ext_ack *extack) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1861 | { |
Roopa Prabhu | f7aa74e | 2018-09-22 21:26:19 -0700 | [diff] [blame] | 1862 | int flags = NEIGH_UPDATE_F_ADMIN | NEIGH_UPDATE_F_OVERRIDE | |
| 1863 | NEIGH_UPDATE_F_OVERRIDE_ISROUTER; |
YOSHIFUJI Hideaki | 3b1e0a6 | 2008-03-26 02:26:21 +0900 | [diff] [blame] | 1864 | struct net *net = sock_net(skb->sk); |
Thomas Graf | 5208deb | 2006-08-07 17:55:40 -0700 | [diff] [blame] | 1865 | struct ndmsg *ndm; |
| 1866 | struct nlattr *tb[NDA_MAX+1]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1867 | struct neigh_table *tbl; |
| 1868 | struct net_device *dev = NULL; |
WANG Cong | d7480fd | 2014-11-10 15:59:36 -0800 | [diff] [blame] | 1869 | struct neighbour *neigh; |
| 1870 | void *dst, *lladdr; |
David Ahern | df9b0e3 | 2018-12-15 14:09:06 -0800 | [diff] [blame] | 1871 | u8 protocol = 0; |
Thomas Graf | 5208deb | 2006-08-07 17:55:40 -0700 | [diff] [blame] | 1872 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1873 | |
Eric Dumazet | 110b249 | 2010-10-04 04:27:36 +0000 | [diff] [blame] | 1874 | ASSERT_RTNL(); |
Johannes Berg | 8cb0817 | 2019-04-26 14:07:28 +0200 | [diff] [blame] | 1875 | err = nlmsg_parse_deprecated(nlh, sizeof(*ndm), tb, NDA_MAX, |
| 1876 | nda_policy, extack); |
Thomas Graf | 5208deb | 2006-08-07 17:55:40 -0700 | [diff] [blame] | 1877 | if (err < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1878 | goto out; |
| 1879 | |
Thomas Graf | 5208deb | 2006-08-07 17:55:40 -0700 | [diff] [blame] | 1880 | err = -EINVAL; |
David Ahern | 7a35a50 | 2018-12-05 20:02:29 -0800 | [diff] [blame] | 1881 | if (!tb[NDA_DST]) { |
| 1882 | NL_SET_ERR_MSG(extack, "Network address not specified"); |
Thomas Graf | 5208deb | 2006-08-07 17:55:40 -0700 | [diff] [blame] | 1883 | goto out; |
David Ahern | 7a35a50 | 2018-12-05 20:02:29 -0800 | [diff] [blame] | 1884 | } |
Thomas Graf | 5208deb | 2006-08-07 17:55:40 -0700 | [diff] [blame] | 1885 | |
| 1886 | ndm = nlmsg_data(nlh); |
| 1887 | if (ndm->ndm_ifindex) { |
Eric Dumazet | 110b249 | 2010-10-04 04:27:36 +0000 | [diff] [blame] | 1888 | dev = __dev_get_by_index(net, ndm->ndm_ifindex); |
Thomas Graf | 5208deb | 2006-08-07 17:55:40 -0700 | [diff] [blame] | 1889 | if (dev == NULL) { |
| 1890 | err = -ENODEV; |
| 1891 | goto out; |
| 1892 | } |
| 1893 | |
David Ahern | 7a35a50 | 2018-12-05 20:02:29 -0800 | [diff] [blame] | 1894 | if (tb[NDA_LLADDR] && nla_len(tb[NDA_LLADDR]) < dev->addr_len) { |
| 1895 | NL_SET_ERR_MSG(extack, "Invalid link address"); |
Eric Dumazet | 110b249 | 2010-10-04 04:27:36 +0000 | [diff] [blame] | 1896 | goto out; |
David Ahern | 7a35a50 | 2018-12-05 20:02:29 -0800 | [diff] [blame] | 1897 | } |
Thomas Graf | 5208deb | 2006-08-07 17:55:40 -0700 | [diff] [blame] | 1898 | } |
| 1899 | |
WANG Cong | d7480fd | 2014-11-10 15:59:36 -0800 | [diff] [blame] | 1900 | tbl = neigh_find_table(ndm->ndm_family); |
| 1901 | if (tbl == NULL) |
| 1902 | return -EAFNOSUPPORT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1903 | |
David Ahern | 7a35a50 | 2018-12-05 20:02:29 -0800 | [diff] [blame] | 1904 | if (nla_len(tb[NDA_DST]) < (int)tbl->key_len) { |
| 1905 | NL_SET_ERR_MSG(extack, "Invalid network address"); |
WANG Cong | d7480fd | 2014-11-10 15:59:36 -0800 | [diff] [blame] | 1906 | goto out; |
David Ahern | 7a35a50 | 2018-12-05 20:02:29 -0800 | [diff] [blame] | 1907 | } |
| 1908 | |
WANG Cong | d7480fd | 2014-11-10 15:59:36 -0800 | [diff] [blame] | 1909 | dst = nla_data(tb[NDA_DST]); |
| 1910 | lladdr = tb[NDA_LLADDR] ? nla_data(tb[NDA_LLADDR]) : NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1911 | |
David Ahern | a9cd343 | 2018-12-19 20:02:36 -0800 | [diff] [blame] | 1912 | if (tb[NDA_PROTOCOL]) |
David Ahern | df9b0e3 | 2018-12-15 14:09:06 -0800 | [diff] [blame] | 1913 | protocol = nla_get_u8(tb[NDA_PROTOCOL]); |
David Ahern | df9b0e3 | 2018-12-15 14:09:06 -0800 | [diff] [blame] | 1914 | |
WANG Cong | d7480fd | 2014-11-10 15:59:36 -0800 | [diff] [blame] | 1915 | if (ndm->ndm_flags & NTF_PROXY) { |
| 1916 | struct pneigh_entry *pn; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1917 | |
WANG Cong | d7480fd | 2014-11-10 15:59:36 -0800 | [diff] [blame] | 1918 | err = -ENOBUFS; |
| 1919 | pn = pneigh_lookup(tbl, net, dst, dev, 1); |
| 1920 | if (pn) { |
| 1921 | pn->flags = ndm->ndm_flags; |
David Ahern | df9b0e3 | 2018-12-15 14:09:06 -0800 | [diff] [blame] | 1922 | if (protocol) |
| 1923 | pn->protocol = protocol; |
Eric Biederman | 0c5c2d3 | 2009-03-04 00:03:08 -0800 | [diff] [blame] | 1924 | err = 0; |
WANG Cong | d7480fd | 2014-11-10 15:59:36 -0800 | [diff] [blame] | 1925 | } |
Eric Dumazet | 110b249 | 2010-10-04 04:27:36 +0000 | [diff] [blame] | 1926 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1927 | } |
| 1928 | |
David Ahern | 7a35a50 | 2018-12-05 20:02:29 -0800 | [diff] [blame] | 1929 | if (!dev) { |
| 1930 | NL_SET_ERR_MSG(extack, "Device not specified"); |
WANG Cong | d7480fd | 2014-11-10 15:59:36 -0800 | [diff] [blame] | 1931 | goto out; |
David Ahern | 7a35a50 | 2018-12-05 20:02:29 -0800 | [diff] [blame] | 1932 | } |
WANG Cong | d7480fd | 2014-11-10 15:59:36 -0800 | [diff] [blame] | 1933 | |
David Ahern | b8fb1ab | 2019-04-16 17:31:43 -0700 | [diff] [blame] | 1934 | if (tbl->allow_add && !tbl->allow_add(dev, extack)) { |
| 1935 | err = -EINVAL; |
| 1936 | goto out; |
| 1937 | } |
| 1938 | |
WANG Cong | d7480fd | 2014-11-10 15:59:36 -0800 | [diff] [blame] | 1939 | neigh = neigh_lookup(tbl, dst, dev); |
| 1940 | if (neigh == NULL) { |
David Ahern | e997f8a | 2018-12-11 18:57:25 -0700 | [diff] [blame] | 1941 | bool exempt_from_gc; |
| 1942 | |
WANG Cong | d7480fd | 2014-11-10 15:59:36 -0800 | [diff] [blame] | 1943 | if (!(nlh->nlmsg_flags & NLM_F_CREATE)) { |
| 1944 | err = -ENOENT; |
| 1945 | goto out; |
| 1946 | } |
| 1947 | |
David Ahern | e997f8a | 2018-12-11 18:57:25 -0700 | [diff] [blame] | 1948 | exempt_from_gc = ndm->ndm_state & NUD_PERMANENT || |
| 1949 | ndm->ndm_flags & NTF_EXT_LEARNED; |
| 1950 | neigh = ___neigh_create(tbl, dst, dev, exempt_from_gc, true); |
WANG Cong | d7480fd | 2014-11-10 15:59:36 -0800 | [diff] [blame] | 1951 | if (IS_ERR(neigh)) { |
| 1952 | err = PTR_ERR(neigh); |
| 1953 | goto out; |
| 1954 | } |
| 1955 | } else { |
| 1956 | if (nlh->nlmsg_flags & NLM_F_EXCL) { |
| 1957 | err = -EEXIST; |
| 1958 | neigh_release(neigh); |
| 1959 | goto out; |
| 1960 | } |
| 1961 | |
| 1962 | if (!(nlh->nlmsg_flags & NLM_F_REPLACE)) |
Roopa Prabhu | f7aa74e | 2018-09-22 21:26:19 -0700 | [diff] [blame] | 1963 | flags &= ~(NEIGH_UPDATE_F_OVERRIDE | |
| 1964 | NEIGH_UPDATE_F_OVERRIDE_ISROUTER); |
WANG Cong | d7480fd | 2014-11-10 15:59:36 -0800 | [diff] [blame] | 1965 | } |
| 1966 | |
Roman Mashak | 38212bb | 2020-05-01 21:34:18 -0400 | [diff] [blame] | 1967 | if (protocol) |
| 1968 | neigh->protocol = protocol; |
| 1969 | |
Roopa Prabhu | 9ce33e4 | 2018-04-24 13:49:34 -0700 | [diff] [blame] | 1970 | if (ndm->ndm_flags & NTF_EXT_LEARNED) |
| 1971 | flags |= NEIGH_UPDATE_F_EXT_LEARNED; |
| 1972 | |
Roopa Prabhu | f7aa74e | 2018-09-22 21:26:19 -0700 | [diff] [blame] | 1973 | if (ndm->ndm_flags & NTF_ROUTER) |
| 1974 | flags |= NEIGH_UPDATE_F_ISROUTER; |
| 1975 | |
WANG Cong | d7480fd | 2014-11-10 15:59:36 -0800 | [diff] [blame] | 1976 | if (ndm->ndm_flags & NTF_USE) { |
| 1977 | neigh_event_send(neigh, NULL); |
| 1978 | err = 0; |
| 1979 | } else |
David Ahern | 7a35a50 | 2018-12-05 20:02:29 -0800 | [diff] [blame] | 1980 | err = __neigh_update(neigh, lladdr, ndm->ndm_state, flags, |
| 1981 | NETLINK_CB(skb).portid, extack); |
David Ahern | df9b0e3 | 2018-12-15 14:09:06 -0800 | [diff] [blame] | 1982 | |
WANG Cong | d7480fd | 2014-11-10 15:59:36 -0800 | [diff] [blame] | 1983 | neigh_release(neigh); |
| 1984 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1985 | out: |
| 1986 | return err; |
| 1987 | } |
| 1988 | |
Thomas Graf | c7fb64d | 2005-06-18 22:50:55 -0700 | [diff] [blame] | 1989 | static int neightbl_fill_parms(struct sk_buff *skb, struct neigh_parms *parms) |
| 1990 | { |
Thomas Graf | ca860fb | 2006-08-07 18:00:18 -0700 | [diff] [blame] | 1991 | struct nlattr *nest; |
| 1992 | |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 1993 | nest = nla_nest_start_noflag(skb, NDTA_PARMS); |
Thomas Graf | ca860fb | 2006-08-07 18:00:18 -0700 | [diff] [blame] | 1994 | if (nest == NULL) |
| 1995 | return -ENOBUFS; |
Thomas Graf | c7fb64d | 2005-06-18 22:50:55 -0700 | [diff] [blame] | 1996 | |
David S. Miller | 9a6308d | 2012-04-01 20:06:28 -0400 | [diff] [blame] | 1997 | if ((parms->dev && |
| 1998 | nla_put_u32(skb, NDTPA_IFINDEX, parms->dev->ifindex)) || |
Reshetova, Elena | 6343944 | 2017-06-30 13:07:56 +0300 | [diff] [blame] | 1999 | nla_put_u32(skb, NDTPA_REFCNT, refcount_read(&parms->refcnt)) || |
Jiri Pirko | 1f9248e | 2013-12-07 19:26:53 +0100 | [diff] [blame] | 2000 | nla_put_u32(skb, NDTPA_QUEUE_LENBYTES, |
| 2001 | NEIGH_VAR(parms, QUEUE_LEN_BYTES)) || |
David S. Miller | 9a6308d | 2012-04-01 20:06:28 -0400 | [diff] [blame] | 2002 | /* approximative value for deprecated QUEUE_LEN (in packets) */ |
| 2003 | nla_put_u32(skb, NDTPA_QUEUE_LEN, |
Jiri Pirko | 1f9248e | 2013-12-07 19:26:53 +0100 | [diff] [blame] | 2004 | NEIGH_VAR(parms, QUEUE_LEN_BYTES) / SKB_TRUESIZE(ETH_FRAME_LEN)) || |
| 2005 | nla_put_u32(skb, NDTPA_PROXY_QLEN, NEIGH_VAR(parms, PROXY_QLEN)) || |
| 2006 | nla_put_u32(skb, NDTPA_APP_PROBES, NEIGH_VAR(parms, APP_PROBES)) || |
| 2007 | nla_put_u32(skb, NDTPA_UCAST_PROBES, |
| 2008 | NEIGH_VAR(parms, UCAST_PROBES)) || |
| 2009 | nla_put_u32(skb, NDTPA_MCAST_PROBES, |
| 2010 | NEIGH_VAR(parms, MCAST_PROBES)) || |
YOSHIFUJI Hideaki/吉藤英明 | 8da8646 | 2015-03-19 22:41:46 +0900 | [diff] [blame] | 2011 | nla_put_u32(skb, NDTPA_MCAST_REPROBES, |
| 2012 | NEIGH_VAR(parms, MCAST_REPROBES)) || |
Nicolas Dichtel | 2175d87 | 2016-04-22 17:31:21 +0200 | [diff] [blame] | 2013 | nla_put_msecs(skb, NDTPA_REACHABLE_TIME, parms->reachable_time, |
| 2014 | NDTPA_PAD) || |
David S. Miller | 9a6308d | 2012-04-01 20:06:28 -0400 | [diff] [blame] | 2015 | nla_put_msecs(skb, NDTPA_BASE_REACHABLE_TIME, |
Nicolas Dichtel | 2175d87 | 2016-04-22 17:31:21 +0200 | [diff] [blame] | 2016 | NEIGH_VAR(parms, BASE_REACHABLE_TIME), NDTPA_PAD) || |
Jiri Pirko | 1f9248e | 2013-12-07 19:26:53 +0100 | [diff] [blame] | 2017 | nla_put_msecs(skb, NDTPA_GC_STALETIME, |
Nicolas Dichtel | 2175d87 | 2016-04-22 17:31:21 +0200 | [diff] [blame] | 2018 | NEIGH_VAR(parms, GC_STALETIME), NDTPA_PAD) || |
David S. Miller | 9a6308d | 2012-04-01 20:06:28 -0400 | [diff] [blame] | 2019 | nla_put_msecs(skb, NDTPA_DELAY_PROBE_TIME, |
Nicolas Dichtel | 2175d87 | 2016-04-22 17:31:21 +0200 | [diff] [blame] | 2020 | NEIGH_VAR(parms, DELAY_PROBE_TIME), NDTPA_PAD) || |
Jiri Pirko | 1f9248e | 2013-12-07 19:26:53 +0100 | [diff] [blame] | 2021 | nla_put_msecs(skb, NDTPA_RETRANS_TIME, |
Nicolas Dichtel | 2175d87 | 2016-04-22 17:31:21 +0200 | [diff] [blame] | 2022 | NEIGH_VAR(parms, RETRANS_TIME), NDTPA_PAD) || |
Jiri Pirko | 1f9248e | 2013-12-07 19:26:53 +0100 | [diff] [blame] | 2023 | nla_put_msecs(skb, NDTPA_ANYCAST_DELAY, |
Nicolas Dichtel | 2175d87 | 2016-04-22 17:31:21 +0200 | [diff] [blame] | 2024 | NEIGH_VAR(parms, ANYCAST_DELAY), NDTPA_PAD) || |
Jiri Pirko | 1f9248e | 2013-12-07 19:26:53 +0100 | [diff] [blame] | 2025 | nla_put_msecs(skb, NDTPA_PROXY_DELAY, |
Nicolas Dichtel | 2175d87 | 2016-04-22 17:31:21 +0200 | [diff] [blame] | 2026 | NEIGH_VAR(parms, PROXY_DELAY), NDTPA_PAD) || |
Jiri Pirko | 1f9248e | 2013-12-07 19:26:53 +0100 | [diff] [blame] | 2027 | nla_put_msecs(skb, NDTPA_LOCKTIME, |
Nicolas Dichtel | 2175d87 | 2016-04-22 17:31:21 +0200 | [diff] [blame] | 2028 | NEIGH_VAR(parms, LOCKTIME), NDTPA_PAD)) |
David S. Miller | 9a6308d | 2012-04-01 20:06:28 -0400 | [diff] [blame] | 2029 | goto nla_put_failure; |
Thomas Graf | ca860fb | 2006-08-07 18:00:18 -0700 | [diff] [blame] | 2030 | return nla_nest_end(skb, nest); |
Thomas Graf | c7fb64d | 2005-06-18 22:50:55 -0700 | [diff] [blame] | 2031 | |
Thomas Graf | ca860fb | 2006-08-07 18:00:18 -0700 | [diff] [blame] | 2032 | nla_put_failure: |
Thomas Graf | bc3ed28 | 2008-06-03 16:36:54 -0700 | [diff] [blame] | 2033 | nla_nest_cancel(skb, nest); |
| 2034 | return -EMSGSIZE; |
Thomas Graf | c7fb64d | 2005-06-18 22:50:55 -0700 | [diff] [blame] | 2035 | } |
| 2036 | |
Thomas Graf | ca860fb | 2006-08-07 18:00:18 -0700 | [diff] [blame] | 2037 | static int neightbl_fill_info(struct sk_buff *skb, struct neigh_table *tbl, |
| 2038 | u32 pid, u32 seq, int type, int flags) |
Thomas Graf | c7fb64d | 2005-06-18 22:50:55 -0700 | [diff] [blame] | 2039 | { |
| 2040 | struct nlmsghdr *nlh; |
| 2041 | struct ndtmsg *ndtmsg; |
| 2042 | |
Thomas Graf | ca860fb | 2006-08-07 18:00:18 -0700 | [diff] [blame] | 2043 | nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndtmsg), flags); |
| 2044 | if (nlh == NULL) |
Patrick McHardy | 2693256 | 2007-01-31 23:16:40 -0800 | [diff] [blame] | 2045 | return -EMSGSIZE; |
Thomas Graf | c7fb64d | 2005-06-18 22:50:55 -0700 | [diff] [blame] | 2046 | |
Thomas Graf | ca860fb | 2006-08-07 18:00:18 -0700 | [diff] [blame] | 2047 | ndtmsg = nlmsg_data(nlh); |
Thomas Graf | c7fb64d | 2005-06-18 22:50:55 -0700 | [diff] [blame] | 2048 | |
| 2049 | read_lock_bh(&tbl->lock); |
| 2050 | ndtmsg->ndtm_family = tbl->family; |
Patrick McHardy | 9ef1d4c | 2005-06-28 12:55:30 -0700 | [diff] [blame] | 2051 | ndtmsg->ndtm_pad1 = 0; |
| 2052 | ndtmsg->ndtm_pad2 = 0; |
Thomas Graf | c7fb64d | 2005-06-18 22:50:55 -0700 | [diff] [blame] | 2053 | |
David S. Miller | 9a6308d | 2012-04-01 20:06:28 -0400 | [diff] [blame] | 2054 | if (nla_put_string(skb, NDTA_NAME, tbl->id) || |
Nicolas Dichtel | 2175d87 | 2016-04-22 17:31:21 +0200 | [diff] [blame] | 2055 | nla_put_msecs(skb, NDTA_GC_INTERVAL, tbl->gc_interval, NDTA_PAD) || |
David S. Miller | 9a6308d | 2012-04-01 20:06:28 -0400 | [diff] [blame] | 2056 | nla_put_u32(skb, NDTA_THRESH1, tbl->gc_thresh1) || |
| 2057 | nla_put_u32(skb, NDTA_THRESH2, tbl->gc_thresh2) || |
| 2058 | nla_put_u32(skb, NDTA_THRESH3, tbl->gc_thresh3)) |
| 2059 | goto nla_put_failure; |
Thomas Graf | c7fb64d | 2005-06-18 22:50:55 -0700 | [diff] [blame] | 2060 | { |
| 2061 | unsigned long now = jiffies; |
Eric Dumazet | 9d027e3 | 2019-11-05 14:11:49 -0800 | [diff] [blame] | 2062 | long flush_delta = now - tbl->last_flush; |
| 2063 | long rand_delta = now - tbl->last_rand; |
Eric Dumazet | d6bf781 | 2010-10-04 06:15:44 +0000 | [diff] [blame] | 2064 | struct neigh_hash_table *nht; |
Thomas Graf | c7fb64d | 2005-06-18 22:50:55 -0700 | [diff] [blame] | 2065 | struct ndt_config ndc = { |
| 2066 | .ndtc_key_len = tbl->key_len, |
| 2067 | .ndtc_entry_size = tbl->entry_size, |
| 2068 | .ndtc_entries = atomic_read(&tbl->entries), |
| 2069 | .ndtc_last_flush = jiffies_to_msecs(flush_delta), |
| 2070 | .ndtc_last_rand = jiffies_to_msecs(rand_delta), |
Thomas Graf | c7fb64d | 2005-06-18 22:50:55 -0700 | [diff] [blame] | 2071 | .ndtc_proxy_qlen = tbl->proxy_queue.qlen, |
| 2072 | }; |
| 2073 | |
Eric Dumazet | d6bf781 | 2010-10-04 06:15:44 +0000 | [diff] [blame] | 2074 | rcu_read_lock_bh(); |
| 2075 | nht = rcu_dereference_bh(tbl->nht); |
David S. Miller | 2c2aba6 | 2011-12-28 15:06:58 -0500 | [diff] [blame] | 2076 | ndc.ndtc_hash_rnd = nht->hash_rnd[0]; |
David S. Miller | cd08933 | 2011-07-11 01:28:12 -0700 | [diff] [blame] | 2077 | ndc.ndtc_hash_mask = ((1 << nht->hash_shift) - 1); |
Eric Dumazet | d6bf781 | 2010-10-04 06:15:44 +0000 | [diff] [blame] | 2078 | rcu_read_unlock_bh(); |
| 2079 | |
David S. Miller | 9a6308d | 2012-04-01 20:06:28 -0400 | [diff] [blame] | 2080 | if (nla_put(skb, NDTA_CONFIG, sizeof(ndc), &ndc)) |
| 2081 | goto nla_put_failure; |
Thomas Graf | c7fb64d | 2005-06-18 22:50:55 -0700 | [diff] [blame] | 2082 | } |
| 2083 | |
| 2084 | { |
| 2085 | int cpu; |
| 2086 | struct ndt_stats ndst; |
| 2087 | |
| 2088 | memset(&ndst, 0, sizeof(ndst)); |
| 2089 | |
KAMEZAWA Hiroyuki | 6f91204 | 2006-04-10 22:52:50 -0700 | [diff] [blame] | 2090 | for_each_possible_cpu(cpu) { |
Thomas Graf | c7fb64d | 2005-06-18 22:50:55 -0700 | [diff] [blame] | 2091 | struct neigh_statistics *st; |
| 2092 | |
Thomas Graf | c7fb64d | 2005-06-18 22:50:55 -0700 | [diff] [blame] | 2093 | st = per_cpu_ptr(tbl->stats, cpu); |
| 2094 | ndst.ndts_allocs += st->allocs; |
| 2095 | ndst.ndts_destroys += st->destroys; |
| 2096 | ndst.ndts_hash_grows += st->hash_grows; |
| 2097 | ndst.ndts_res_failed += st->res_failed; |
| 2098 | ndst.ndts_lookups += st->lookups; |
| 2099 | ndst.ndts_hits += st->hits; |
| 2100 | ndst.ndts_rcv_probes_mcast += st->rcv_probes_mcast; |
| 2101 | ndst.ndts_rcv_probes_ucast += st->rcv_probes_ucast; |
| 2102 | ndst.ndts_periodic_gc_runs += st->periodic_gc_runs; |
| 2103 | ndst.ndts_forced_gc_runs += st->forced_gc_runs; |
Rick Jones | fb81139 | 2015-08-07 11:10:37 -0700 | [diff] [blame] | 2104 | ndst.ndts_table_fulls += st->table_fulls; |
Thomas Graf | c7fb64d | 2005-06-18 22:50:55 -0700 | [diff] [blame] | 2105 | } |
| 2106 | |
Nicolas Dichtel | b676338 | 2016-04-26 10:06:17 +0200 | [diff] [blame] | 2107 | if (nla_put_64bit(skb, NDTA_STATS, sizeof(ndst), &ndst, |
| 2108 | NDTA_PAD)) |
David S. Miller | 9a6308d | 2012-04-01 20:06:28 -0400 | [diff] [blame] | 2109 | goto nla_put_failure; |
Thomas Graf | c7fb64d | 2005-06-18 22:50:55 -0700 | [diff] [blame] | 2110 | } |
| 2111 | |
| 2112 | BUG_ON(tbl->parms.dev); |
| 2113 | if (neightbl_fill_parms(skb, &tbl->parms) < 0) |
Thomas Graf | ca860fb | 2006-08-07 18:00:18 -0700 | [diff] [blame] | 2114 | goto nla_put_failure; |
Thomas Graf | c7fb64d | 2005-06-18 22:50:55 -0700 | [diff] [blame] | 2115 | |
| 2116 | read_unlock_bh(&tbl->lock); |
Johannes Berg | 053c095 | 2015-01-16 22:09:00 +0100 | [diff] [blame] | 2117 | nlmsg_end(skb, nlh); |
| 2118 | return 0; |
Thomas Graf | c7fb64d | 2005-06-18 22:50:55 -0700 | [diff] [blame] | 2119 | |
Thomas Graf | ca860fb | 2006-08-07 18:00:18 -0700 | [diff] [blame] | 2120 | nla_put_failure: |
Thomas Graf | c7fb64d | 2005-06-18 22:50:55 -0700 | [diff] [blame] | 2121 | read_unlock_bh(&tbl->lock); |
Patrick McHardy | 2693256 | 2007-01-31 23:16:40 -0800 | [diff] [blame] | 2122 | nlmsg_cancel(skb, nlh); |
| 2123 | return -EMSGSIZE; |
Thomas Graf | c7fb64d | 2005-06-18 22:50:55 -0700 | [diff] [blame] | 2124 | } |
| 2125 | |
Thomas Graf | ca860fb | 2006-08-07 18:00:18 -0700 | [diff] [blame] | 2126 | static int neightbl_fill_param_info(struct sk_buff *skb, |
| 2127 | struct neigh_table *tbl, |
Thomas Graf | c7fb64d | 2005-06-18 22:50:55 -0700 | [diff] [blame] | 2128 | struct neigh_parms *parms, |
Thomas Graf | ca860fb | 2006-08-07 18:00:18 -0700 | [diff] [blame] | 2129 | u32 pid, u32 seq, int type, |
| 2130 | unsigned int flags) |
Thomas Graf | c7fb64d | 2005-06-18 22:50:55 -0700 | [diff] [blame] | 2131 | { |
| 2132 | struct ndtmsg *ndtmsg; |
| 2133 | struct nlmsghdr *nlh; |
| 2134 | |
Thomas Graf | ca860fb | 2006-08-07 18:00:18 -0700 | [diff] [blame] | 2135 | nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndtmsg), flags); |
| 2136 | if (nlh == NULL) |
Patrick McHardy | 2693256 | 2007-01-31 23:16:40 -0800 | [diff] [blame] | 2137 | return -EMSGSIZE; |
Thomas Graf | c7fb64d | 2005-06-18 22:50:55 -0700 | [diff] [blame] | 2138 | |
Thomas Graf | ca860fb | 2006-08-07 18:00:18 -0700 | [diff] [blame] | 2139 | ndtmsg = nlmsg_data(nlh); |
Thomas Graf | c7fb64d | 2005-06-18 22:50:55 -0700 | [diff] [blame] | 2140 | |
| 2141 | read_lock_bh(&tbl->lock); |
| 2142 | ndtmsg->ndtm_family = tbl->family; |
Patrick McHardy | 9ef1d4c | 2005-06-28 12:55:30 -0700 | [diff] [blame] | 2143 | ndtmsg->ndtm_pad1 = 0; |
| 2144 | ndtmsg->ndtm_pad2 = 0; |
Thomas Graf | c7fb64d | 2005-06-18 22:50:55 -0700 | [diff] [blame] | 2145 | |
Thomas Graf | ca860fb | 2006-08-07 18:00:18 -0700 | [diff] [blame] | 2146 | if (nla_put_string(skb, NDTA_NAME, tbl->id) < 0 || |
| 2147 | neightbl_fill_parms(skb, parms) < 0) |
| 2148 | goto errout; |
Thomas Graf | c7fb64d | 2005-06-18 22:50:55 -0700 | [diff] [blame] | 2149 | |
| 2150 | read_unlock_bh(&tbl->lock); |
Johannes Berg | 053c095 | 2015-01-16 22:09:00 +0100 | [diff] [blame] | 2151 | nlmsg_end(skb, nlh); |
| 2152 | return 0; |
Thomas Graf | ca860fb | 2006-08-07 18:00:18 -0700 | [diff] [blame] | 2153 | errout: |
Thomas Graf | c7fb64d | 2005-06-18 22:50:55 -0700 | [diff] [blame] | 2154 | read_unlock_bh(&tbl->lock); |
Patrick McHardy | 2693256 | 2007-01-31 23:16:40 -0800 | [diff] [blame] | 2155 | nlmsg_cancel(skb, nlh); |
| 2156 | return -EMSGSIZE; |
Thomas Graf | c7fb64d | 2005-06-18 22:50:55 -0700 | [diff] [blame] | 2157 | } |
YOSHIFUJI Hideaki | 4ec93ed | 2007-02-09 23:24:36 +0900 | [diff] [blame] | 2158 | |
Patrick McHardy | ef7c79e | 2007-06-05 12:38:30 -0700 | [diff] [blame] | 2159 | static const struct nla_policy nl_neightbl_policy[NDTA_MAX+1] = { |
Thomas Graf | 6b3f867 | 2006-08-07 17:58:53 -0700 | [diff] [blame] | 2160 | [NDTA_NAME] = { .type = NLA_STRING }, |
| 2161 | [NDTA_THRESH1] = { .type = NLA_U32 }, |
| 2162 | [NDTA_THRESH2] = { .type = NLA_U32 }, |
| 2163 | [NDTA_THRESH3] = { .type = NLA_U32 }, |
| 2164 | [NDTA_GC_INTERVAL] = { .type = NLA_U64 }, |
| 2165 | [NDTA_PARMS] = { .type = NLA_NESTED }, |
| 2166 | }; |
| 2167 | |
Patrick McHardy | ef7c79e | 2007-06-05 12:38:30 -0700 | [diff] [blame] | 2168 | static const struct nla_policy nl_ntbl_parm_policy[NDTPA_MAX+1] = { |
Thomas Graf | 6b3f867 | 2006-08-07 17:58:53 -0700 | [diff] [blame] | 2169 | [NDTPA_IFINDEX] = { .type = NLA_U32 }, |
| 2170 | [NDTPA_QUEUE_LEN] = { .type = NLA_U32 }, |
| 2171 | [NDTPA_PROXY_QLEN] = { .type = NLA_U32 }, |
| 2172 | [NDTPA_APP_PROBES] = { .type = NLA_U32 }, |
| 2173 | [NDTPA_UCAST_PROBES] = { .type = NLA_U32 }, |
| 2174 | [NDTPA_MCAST_PROBES] = { .type = NLA_U32 }, |
YOSHIFUJI Hideaki/吉藤英明 | 8da8646 | 2015-03-19 22:41:46 +0900 | [diff] [blame] | 2175 | [NDTPA_MCAST_REPROBES] = { .type = NLA_U32 }, |
Thomas Graf | 6b3f867 | 2006-08-07 17:58:53 -0700 | [diff] [blame] | 2176 | [NDTPA_BASE_REACHABLE_TIME] = { .type = NLA_U64 }, |
| 2177 | [NDTPA_GC_STALETIME] = { .type = NLA_U64 }, |
| 2178 | [NDTPA_DELAY_PROBE_TIME] = { .type = NLA_U64 }, |
| 2179 | [NDTPA_RETRANS_TIME] = { .type = NLA_U64 }, |
| 2180 | [NDTPA_ANYCAST_DELAY] = { .type = NLA_U64 }, |
| 2181 | [NDTPA_PROXY_DELAY] = { .type = NLA_U64 }, |
| 2182 | [NDTPA_LOCKTIME] = { .type = NLA_U64 }, |
| 2183 | }; |
| 2184 | |
David Ahern | c21ef3e | 2017-04-16 09:48:24 -0700 | [diff] [blame] | 2185 | static int neightbl_set(struct sk_buff *skb, struct nlmsghdr *nlh, |
| 2186 | struct netlink_ext_ack *extack) |
Thomas Graf | c7fb64d | 2005-06-18 22:50:55 -0700 | [diff] [blame] | 2187 | { |
YOSHIFUJI Hideaki | 3b1e0a6 | 2008-03-26 02:26:21 +0900 | [diff] [blame] | 2188 | struct net *net = sock_net(skb->sk); |
Thomas Graf | c7fb64d | 2005-06-18 22:50:55 -0700 | [diff] [blame] | 2189 | struct neigh_table *tbl; |
Thomas Graf | 6b3f867 | 2006-08-07 17:58:53 -0700 | [diff] [blame] | 2190 | struct ndtmsg *ndtmsg; |
| 2191 | struct nlattr *tb[NDTA_MAX+1]; |
WANG Cong | d7480fd | 2014-11-10 15:59:36 -0800 | [diff] [blame] | 2192 | bool found = false; |
| 2193 | int err, tidx; |
Thomas Graf | c7fb64d | 2005-06-18 22:50:55 -0700 | [diff] [blame] | 2194 | |
Johannes Berg | 8cb0817 | 2019-04-26 14:07:28 +0200 | [diff] [blame] | 2195 | err = nlmsg_parse_deprecated(nlh, sizeof(*ndtmsg), tb, NDTA_MAX, |
| 2196 | nl_neightbl_policy, extack); |
Thomas Graf | 6b3f867 | 2006-08-07 17:58:53 -0700 | [diff] [blame] | 2197 | if (err < 0) |
| 2198 | goto errout; |
Thomas Graf | c7fb64d | 2005-06-18 22:50:55 -0700 | [diff] [blame] | 2199 | |
Thomas Graf | 6b3f867 | 2006-08-07 17:58:53 -0700 | [diff] [blame] | 2200 | if (tb[NDTA_NAME] == NULL) { |
| 2201 | err = -EINVAL; |
| 2202 | goto errout; |
| 2203 | } |
| 2204 | |
| 2205 | ndtmsg = nlmsg_data(nlh); |
WANG Cong | d7480fd | 2014-11-10 15:59:36 -0800 | [diff] [blame] | 2206 | |
| 2207 | for (tidx = 0; tidx < NEIGH_NR_TABLES; tidx++) { |
| 2208 | tbl = neigh_tables[tidx]; |
| 2209 | if (!tbl) |
| 2210 | continue; |
Thomas Graf | c7fb64d | 2005-06-18 22:50:55 -0700 | [diff] [blame] | 2211 | if (ndtmsg->ndtm_family && tbl->family != ndtmsg->ndtm_family) |
| 2212 | continue; |
WANG Cong | d7480fd | 2014-11-10 15:59:36 -0800 | [diff] [blame] | 2213 | if (nla_strcmp(tb[NDTA_NAME], tbl->id) == 0) { |
| 2214 | found = true; |
Thomas Graf | c7fb64d | 2005-06-18 22:50:55 -0700 | [diff] [blame] | 2215 | break; |
WANG Cong | d7480fd | 2014-11-10 15:59:36 -0800 | [diff] [blame] | 2216 | } |
Thomas Graf | c7fb64d | 2005-06-18 22:50:55 -0700 | [diff] [blame] | 2217 | } |
| 2218 | |
WANG Cong | d7480fd | 2014-11-10 15:59:36 -0800 | [diff] [blame] | 2219 | if (!found) |
| 2220 | return -ENOENT; |
Thomas Graf | c7fb64d | 2005-06-18 22:50:55 -0700 | [diff] [blame] | 2221 | |
YOSHIFUJI Hideaki | 4ec93ed | 2007-02-09 23:24:36 +0900 | [diff] [blame] | 2222 | /* |
Thomas Graf | c7fb64d | 2005-06-18 22:50:55 -0700 | [diff] [blame] | 2223 | * We acquire tbl->lock to be nice to the periodic timers and |
| 2224 | * make sure they always see a consistent set of values. |
| 2225 | */ |
| 2226 | write_lock_bh(&tbl->lock); |
| 2227 | |
Thomas Graf | 6b3f867 | 2006-08-07 17:58:53 -0700 | [diff] [blame] | 2228 | if (tb[NDTA_PARMS]) { |
| 2229 | struct nlattr *tbp[NDTPA_MAX+1]; |
Thomas Graf | c7fb64d | 2005-06-18 22:50:55 -0700 | [diff] [blame] | 2230 | struct neigh_parms *p; |
Thomas Graf | 6b3f867 | 2006-08-07 17:58:53 -0700 | [diff] [blame] | 2231 | int i, ifindex = 0; |
Thomas Graf | c7fb64d | 2005-06-18 22:50:55 -0700 | [diff] [blame] | 2232 | |
Johannes Berg | 8cb0817 | 2019-04-26 14:07:28 +0200 | [diff] [blame] | 2233 | err = nla_parse_nested_deprecated(tbp, NDTPA_MAX, |
| 2234 | tb[NDTA_PARMS], |
| 2235 | nl_ntbl_parm_policy, extack); |
Thomas Graf | 6b3f867 | 2006-08-07 17:58:53 -0700 | [diff] [blame] | 2236 | if (err < 0) |
| 2237 | goto errout_tbl_lock; |
Thomas Graf | c7fb64d | 2005-06-18 22:50:55 -0700 | [diff] [blame] | 2238 | |
Thomas Graf | 6b3f867 | 2006-08-07 17:58:53 -0700 | [diff] [blame] | 2239 | if (tbp[NDTPA_IFINDEX]) |
| 2240 | ifindex = nla_get_u32(tbp[NDTPA_IFINDEX]); |
Thomas Graf | c7fb64d | 2005-06-18 22:50:55 -0700 | [diff] [blame] | 2241 | |
Tobias Klauser | 97fd5bc | 2009-07-13 11:17:49 -0700 | [diff] [blame] | 2242 | p = lookup_neigh_parms(tbl, net, ifindex); |
Thomas Graf | c7fb64d | 2005-06-18 22:50:55 -0700 | [diff] [blame] | 2243 | if (p == NULL) { |
| 2244 | err = -ENOENT; |
Thomas Graf | 6b3f867 | 2006-08-07 17:58:53 -0700 | [diff] [blame] | 2245 | goto errout_tbl_lock; |
Thomas Graf | c7fb64d | 2005-06-18 22:50:55 -0700 | [diff] [blame] | 2246 | } |
Thomas Graf | c7fb64d | 2005-06-18 22:50:55 -0700 | [diff] [blame] | 2247 | |
Thomas Graf | 6b3f867 | 2006-08-07 17:58:53 -0700 | [diff] [blame] | 2248 | for (i = 1; i <= NDTPA_MAX; i++) { |
| 2249 | if (tbp[i] == NULL) |
| 2250 | continue; |
Thomas Graf | c7fb64d | 2005-06-18 22:50:55 -0700 | [diff] [blame] | 2251 | |
Thomas Graf | 6b3f867 | 2006-08-07 17:58:53 -0700 | [diff] [blame] | 2252 | switch (i) { |
| 2253 | case NDTPA_QUEUE_LEN: |
Jiri Pirko | 1f9248e | 2013-12-07 19:26:53 +0100 | [diff] [blame] | 2254 | NEIGH_VAR_SET(p, QUEUE_LEN_BYTES, |
| 2255 | nla_get_u32(tbp[i]) * |
| 2256 | SKB_TRUESIZE(ETH_FRAME_LEN)); |
Eric Dumazet | 8b5c171 | 2011-11-09 12:07:14 +0000 | [diff] [blame] | 2257 | break; |
| 2258 | case NDTPA_QUEUE_LENBYTES: |
Jiri Pirko | 1f9248e | 2013-12-07 19:26:53 +0100 | [diff] [blame] | 2259 | NEIGH_VAR_SET(p, QUEUE_LEN_BYTES, |
| 2260 | nla_get_u32(tbp[i])); |
Thomas Graf | 6b3f867 | 2006-08-07 17:58:53 -0700 | [diff] [blame] | 2261 | break; |
| 2262 | case NDTPA_PROXY_QLEN: |
Jiri Pirko | 1f9248e | 2013-12-07 19:26:53 +0100 | [diff] [blame] | 2263 | NEIGH_VAR_SET(p, PROXY_QLEN, |
| 2264 | nla_get_u32(tbp[i])); |
Thomas Graf | 6b3f867 | 2006-08-07 17:58:53 -0700 | [diff] [blame] | 2265 | break; |
| 2266 | case NDTPA_APP_PROBES: |
Jiri Pirko | 1f9248e | 2013-12-07 19:26:53 +0100 | [diff] [blame] | 2267 | NEIGH_VAR_SET(p, APP_PROBES, |
| 2268 | nla_get_u32(tbp[i])); |
Thomas Graf | 6b3f867 | 2006-08-07 17:58:53 -0700 | [diff] [blame] | 2269 | break; |
| 2270 | case NDTPA_UCAST_PROBES: |
Jiri Pirko | 1f9248e | 2013-12-07 19:26:53 +0100 | [diff] [blame] | 2271 | NEIGH_VAR_SET(p, UCAST_PROBES, |
| 2272 | nla_get_u32(tbp[i])); |
Thomas Graf | 6b3f867 | 2006-08-07 17:58:53 -0700 | [diff] [blame] | 2273 | break; |
| 2274 | case NDTPA_MCAST_PROBES: |
Jiri Pirko | 1f9248e | 2013-12-07 19:26:53 +0100 | [diff] [blame] | 2275 | NEIGH_VAR_SET(p, MCAST_PROBES, |
| 2276 | nla_get_u32(tbp[i])); |
Thomas Graf | 6b3f867 | 2006-08-07 17:58:53 -0700 | [diff] [blame] | 2277 | break; |
YOSHIFUJI Hideaki/吉藤英明 | 8da8646 | 2015-03-19 22:41:46 +0900 | [diff] [blame] | 2278 | case NDTPA_MCAST_REPROBES: |
| 2279 | NEIGH_VAR_SET(p, MCAST_REPROBES, |
| 2280 | nla_get_u32(tbp[i])); |
| 2281 | break; |
Thomas Graf | 6b3f867 | 2006-08-07 17:58:53 -0700 | [diff] [blame] | 2282 | case NDTPA_BASE_REACHABLE_TIME: |
Jiri Pirko | 1f9248e | 2013-12-07 19:26:53 +0100 | [diff] [blame] | 2283 | NEIGH_VAR_SET(p, BASE_REACHABLE_TIME, |
| 2284 | nla_get_msecs(tbp[i])); |
Jean-Francois Remy | 4bf6980 | 2015-01-14 04:22:39 +0100 | [diff] [blame] | 2285 | /* update reachable_time as well, otherwise, the change will |
| 2286 | * only be effective after the next time neigh_periodic_work |
| 2287 | * decides to recompute it (can be multiple minutes) |
| 2288 | */ |
| 2289 | p->reachable_time = |
| 2290 | neigh_rand_reach_time(NEIGH_VAR(p, BASE_REACHABLE_TIME)); |
Thomas Graf | 6b3f867 | 2006-08-07 17:58:53 -0700 | [diff] [blame] | 2291 | break; |
| 2292 | case NDTPA_GC_STALETIME: |
Jiri Pirko | 1f9248e | 2013-12-07 19:26:53 +0100 | [diff] [blame] | 2293 | NEIGH_VAR_SET(p, GC_STALETIME, |
| 2294 | nla_get_msecs(tbp[i])); |
Thomas Graf | 6b3f867 | 2006-08-07 17:58:53 -0700 | [diff] [blame] | 2295 | break; |
| 2296 | case NDTPA_DELAY_PROBE_TIME: |
Jiri Pirko | 1f9248e | 2013-12-07 19:26:53 +0100 | [diff] [blame] | 2297 | NEIGH_VAR_SET(p, DELAY_PROBE_TIME, |
| 2298 | nla_get_msecs(tbp[i])); |
Ido Schimmel | 2a4501a | 2016-07-05 11:27:42 +0200 | [diff] [blame] | 2299 | call_netevent_notifiers(NETEVENT_DELAY_PROBE_TIME_UPDATE, p); |
Thomas Graf | 6b3f867 | 2006-08-07 17:58:53 -0700 | [diff] [blame] | 2300 | break; |
| 2301 | case NDTPA_RETRANS_TIME: |
Jiri Pirko | 1f9248e | 2013-12-07 19:26:53 +0100 | [diff] [blame] | 2302 | NEIGH_VAR_SET(p, RETRANS_TIME, |
| 2303 | nla_get_msecs(tbp[i])); |
Thomas Graf | 6b3f867 | 2006-08-07 17:58:53 -0700 | [diff] [blame] | 2304 | break; |
| 2305 | case NDTPA_ANYCAST_DELAY: |
Jiri Pirko | 3977458 | 2014-01-14 15:46:07 +0100 | [diff] [blame] | 2306 | NEIGH_VAR_SET(p, ANYCAST_DELAY, |
| 2307 | nla_get_msecs(tbp[i])); |
Thomas Graf | 6b3f867 | 2006-08-07 17:58:53 -0700 | [diff] [blame] | 2308 | break; |
| 2309 | case NDTPA_PROXY_DELAY: |
Jiri Pirko | 3977458 | 2014-01-14 15:46:07 +0100 | [diff] [blame] | 2310 | NEIGH_VAR_SET(p, PROXY_DELAY, |
| 2311 | nla_get_msecs(tbp[i])); |
Thomas Graf | 6b3f867 | 2006-08-07 17:58:53 -0700 | [diff] [blame] | 2312 | break; |
| 2313 | case NDTPA_LOCKTIME: |
Jiri Pirko | 3977458 | 2014-01-14 15:46:07 +0100 | [diff] [blame] | 2314 | NEIGH_VAR_SET(p, LOCKTIME, |
| 2315 | nla_get_msecs(tbp[i])); |
Thomas Graf | 6b3f867 | 2006-08-07 17:58:53 -0700 | [diff] [blame] | 2316 | break; |
| 2317 | } |
| 2318 | } |
Thomas Graf | c7fb64d | 2005-06-18 22:50:55 -0700 | [diff] [blame] | 2319 | } |
| 2320 | |
Gao feng | dc25c67 | 2013-06-20 10:01:34 +0800 | [diff] [blame] | 2321 | err = -ENOENT; |
| 2322 | if ((tb[NDTA_THRESH1] || tb[NDTA_THRESH2] || |
| 2323 | tb[NDTA_THRESH3] || tb[NDTA_GC_INTERVAL]) && |
| 2324 | !net_eq(net, &init_net)) |
| 2325 | goto errout_tbl_lock; |
| 2326 | |
Thomas Graf | 6b3f867 | 2006-08-07 17:58:53 -0700 | [diff] [blame] | 2327 | if (tb[NDTA_THRESH1]) |
| 2328 | tbl->gc_thresh1 = nla_get_u32(tb[NDTA_THRESH1]); |
| 2329 | |
| 2330 | if (tb[NDTA_THRESH2]) |
| 2331 | tbl->gc_thresh2 = nla_get_u32(tb[NDTA_THRESH2]); |
| 2332 | |
| 2333 | if (tb[NDTA_THRESH3]) |
| 2334 | tbl->gc_thresh3 = nla_get_u32(tb[NDTA_THRESH3]); |
| 2335 | |
| 2336 | if (tb[NDTA_GC_INTERVAL]) |
| 2337 | tbl->gc_interval = nla_get_msecs(tb[NDTA_GC_INTERVAL]); |
| 2338 | |
Thomas Graf | c7fb64d | 2005-06-18 22:50:55 -0700 | [diff] [blame] | 2339 | err = 0; |
| 2340 | |
Thomas Graf | 6b3f867 | 2006-08-07 17:58:53 -0700 | [diff] [blame] | 2341 | errout_tbl_lock: |
Thomas Graf | c7fb64d | 2005-06-18 22:50:55 -0700 | [diff] [blame] | 2342 | write_unlock_bh(&tbl->lock); |
Thomas Graf | 6b3f867 | 2006-08-07 17:58:53 -0700 | [diff] [blame] | 2343 | errout: |
Thomas Graf | c7fb64d | 2005-06-18 22:50:55 -0700 | [diff] [blame] | 2344 | return err; |
| 2345 | } |
| 2346 | |
David Ahern | 9632d47 | 2018-10-07 20:16:37 -0700 | [diff] [blame] | 2347 | static int neightbl_valid_dump_info(const struct nlmsghdr *nlh, |
| 2348 | struct netlink_ext_ack *extack) |
| 2349 | { |
| 2350 | struct ndtmsg *ndtm; |
| 2351 | |
| 2352 | if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ndtm))) { |
| 2353 | NL_SET_ERR_MSG(extack, "Invalid header for neighbor table dump request"); |
| 2354 | return -EINVAL; |
| 2355 | } |
| 2356 | |
| 2357 | ndtm = nlmsg_data(nlh); |
| 2358 | if (ndtm->ndtm_pad1 || ndtm->ndtm_pad2) { |
| 2359 | NL_SET_ERR_MSG(extack, "Invalid values in header for neighbor table dump request"); |
| 2360 | return -EINVAL; |
| 2361 | } |
| 2362 | |
| 2363 | if (nlmsg_attrlen(nlh, sizeof(*ndtm))) { |
| 2364 | NL_SET_ERR_MSG(extack, "Invalid data after header in neighbor table dump request"); |
| 2365 | return -EINVAL; |
| 2366 | } |
| 2367 | |
| 2368 | return 0; |
| 2369 | } |
| 2370 | |
Thomas Graf | c8822a4 | 2007-03-22 11:50:06 -0700 | [diff] [blame] | 2371 | static int neightbl_dump_info(struct sk_buff *skb, struct netlink_callback *cb) |
Thomas Graf | c7fb64d | 2005-06-18 22:50:55 -0700 | [diff] [blame] | 2372 | { |
David Ahern | 9632d47 | 2018-10-07 20:16:37 -0700 | [diff] [blame] | 2373 | const struct nlmsghdr *nlh = cb->nlh; |
YOSHIFUJI Hideaki | 3b1e0a6 | 2008-03-26 02:26:21 +0900 | [diff] [blame] | 2374 | struct net *net = sock_net(skb->sk); |
Thomas Graf | ca860fb | 2006-08-07 18:00:18 -0700 | [diff] [blame] | 2375 | int family, tidx, nidx = 0; |
| 2376 | int tbl_skip = cb->args[0]; |
| 2377 | int neigh_skip = cb->args[1]; |
Thomas Graf | c7fb64d | 2005-06-18 22:50:55 -0700 | [diff] [blame] | 2378 | struct neigh_table *tbl; |
| 2379 | |
David Ahern | 9632d47 | 2018-10-07 20:16:37 -0700 | [diff] [blame] | 2380 | if (cb->strict_check) { |
| 2381 | int err = neightbl_valid_dump_info(nlh, cb->extack); |
| 2382 | |
| 2383 | if (err < 0) |
| 2384 | return err; |
| 2385 | } |
| 2386 | |
| 2387 | family = ((struct rtgenmsg *)nlmsg_data(nlh))->rtgen_family; |
Thomas Graf | c7fb64d | 2005-06-18 22:50:55 -0700 | [diff] [blame] | 2388 | |
WANG Cong | d7480fd | 2014-11-10 15:59:36 -0800 | [diff] [blame] | 2389 | for (tidx = 0; tidx < NEIGH_NR_TABLES; tidx++) { |
Thomas Graf | c7fb64d | 2005-06-18 22:50:55 -0700 | [diff] [blame] | 2390 | struct neigh_parms *p; |
| 2391 | |
WANG Cong | d7480fd | 2014-11-10 15:59:36 -0800 | [diff] [blame] | 2392 | tbl = neigh_tables[tidx]; |
| 2393 | if (!tbl) |
| 2394 | continue; |
| 2395 | |
Thomas Graf | ca860fb | 2006-08-07 18:00:18 -0700 | [diff] [blame] | 2396 | if (tidx < tbl_skip || (family && tbl->family != family)) |
Thomas Graf | c7fb64d | 2005-06-18 22:50:55 -0700 | [diff] [blame] | 2397 | continue; |
| 2398 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 2399 | if (neightbl_fill_info(skb, tbl, NETLINK_CB(cb->skb).portid, |
David Ahern | 9632d47 | 2018-10-07 20:16:37 -0700 | [diff] [blame] | 2400 | nlh->nlmsg_seq, RTM_NEWNEIGHTBL, |
David S. Miller | 7b46a64 | 2015-01-18 23:36:08 -0500 | [diff] [blame] | 2401 | NLM_F_MULTI) < 0) |
Thomas Graf | c7fb64d | 2005-06-18 22:50:55 -0700 | [diff] [blame] | 2402 | break; |
| 2403 | |
Nicolas Dichtel | 75fbfd3 | 2014-10-29 19:29:31 +0100 | [diff] [blame] | 2404 | nidx = 0; |
| 2405 | p = list_next_entry(&tbl->parms, list); |
| 2406 | list_for_each_entry_from(p, &tbl->parms_list, list) { |
YOSHIFUJI Hideaki | 878628f | 2008-03-26 03:57:35 +0900 | [diff] [blame] | 2407 | if (!net_eq(neigh_parms_net(p), net)) |
Eric W. Biederman | 426b530 | 2008-01-24 00:13:18 -0800 | [diff] [blame] | 2408 | continue; |
| 2409 | |
Gautam Kachroo | efc683f | 2009-02-06 00:52:04 -0800 | [diff] [blame] | 2410 | if (nidx < neigh_skip) |
| 2411 | goto next; |
Thomas Graf | c7fb64d | 2005-06-18 22:50:55 -0700 | [diff] [blame] | 2412 | |
Thomas Graf | ca860fb | 2006-08-07 18:00:18 -0700 | [diff] [blame] | 2413 | if (neightbl_fill_param_info(skb, tbl, p, |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 2414 | NETLINK_CB(cb->skb).portid, |
David Ahern | 9632d47 | 2018-10-07 20:16:37 -0700 | [diff] [blame] | 2415 | nlh->nlmsg_seq, |
Thomas Graf | ca860fb | 2006-08-07 18:00:18 -0700 | [diff] [blame] | 2416 | RTM_NEWNEIGHTBL, |
David S. Miller | 7b46a64 | 2015-01-18 23:36:08 -0500 | [diff] [blame] | 2417 | NLM_F_MULTI) < 0) |
Thomas Graf | c7fb64d | 2005-06-18 22:50:55 -0700 | [diff] [blame] | 2418 | goto out; |
Gautam Kachroo | efc683f | 2009-02-06 00:52:04 -0800 | [diff] [blame] | 2419 | next: |
| 2420 | nidx++; |
Thomas Graf | c7fb64d | 2005-06-18 22:50:55 -0700 | [diff] [blame] | 2421 | } |
| 2422 | |
Thomas Graf | ca860fb | 2006-08-07 18:00:18 -0700 | [diff] [blame] | 2423 | neigh_skip = 0; |
Thomas Graf | c7fb64d | 2005-06-18 22:50:55 -0700 | [diff] [blame] | 2424 | } |
| 2425 | out: |
Thomas Graf | ca860fb | 2006-08-07 18:00:18 -0700 | [diff] [blame] | 2426 | cb->args[0] = tidx; |
| 2427 | cb->args[1] = nidx; |
Thomas Graf | c7fb64d | 2005-06-18 22:50:55 -0700 | [diff] [blame] | 2428 | |
| 2429 | return skb->len; |
| 2430 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2431 | |
Thomas Graf | 8b8aec5 | 2006-08-07 17:56:37 -0700 | [diff] [blame] | 2432 | static int neigh_fill_info(struct sk_buff *skb, struct neighbour *neigh, |
| 2433 | u32 pid, u32 seq, int type, unsigned int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2434 | { |
| 2435 | unsigned long now = jiffies; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2436 | struct nda_cacheinfo ci; |
Thomas Graf | 8b8aec5 | 2006-08-07 17:56:37 -0700 | [diff] [blame] | 2437 | struct nlmsghdr *nlh; |
| 2438 | struct ndmsg *ndm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2439 | |
Thomas Graf | 8b8aec5 | 2006-08-07 17:56:37 -0700 | [diff] [blame] | 2440 | nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), flags); |
| 2441 | if (nlh == NULL) |
Patrick McHardy | 2693256 | 2007-01-31 23:16:40 -0800 | [diff] [blame] | 2442 | return -EMSGSIZE; |
Thomas Graf | 8b8aec5 | 2006-08-07 17:56:37 -0700 | [diff] [blame] | 2443 | |
| 2444 | ndm = nlmsg_data(nlh); |
| 2445 | ndm->ndm_family = neigh->ops->family; |
Patrick McHardy | 9ef1d4c | 2005-06-28 12:55:30 -0700 | [diff] [blame] | 2446 | ndm->ndm_pad1 = 0; |
| 2447 | ndm->ndm_pad2 = 0; |
Thomas Graf | 8b8aec5 | 2006-08-07 17:56:37 -0700 | [diff] [blame] | 2448 | ndm->ndm_flags = neigh->flags; |
| 2449 | ndm->ndm_type = neigh->type; |
| 2450 | ndm->ndm_ifindex = neigh->dev->ifindex; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2451 | |
David S. Miller | 9a6308d | 2012-04-01 20:06:28 -0400 | [diff] [blame] | 2452 | if (nla_put(skb, NDA_DST, neigh->tbl->key_len, neigh->primary_key)) |
| 2453 | goto nla_put_failure; |
Thomas Graf | 8b8aec5 | 2006-08-07 17:56:37 -0700 | [diff] [blame] | 2454 | |
| 2455 | read_lock_bh(&neigh->lock); |
| 2456 | ndm->ndm_state = neigh->nud_state; |
Eric Dumazet | 0ed8ddf | 2010-10-07 10:44:07 +0000 | [diff] [blame] | 2457 | if (neigh->nud_state & NUD_VALID) { |
| 2458 | char haddr[MAX_ADDR_LEN]; |
| 2459 | |
| 2460 | neigh_ha_snapshot(haddr, neigh, neigh->dev); |
| 2461 | if (nla_put(skb, NDA_LLADDR, neigh->dev->addr_len, haddr) < 0) { |
| 2462 | read_unlock_bh(&neigh->lock); |
| 2463 | goto nla_put_failure; |
| 2464 | } |
Thomas Graf | 8b8aec5 | 2006-08-07 17:56:37 -0700 | [diff] [blame] | 2465 | } |
| 2466 | |
Stephen Hemminger | b9f5f52 | 2008-06-03 16:03:15 -0700 | [diff] [blame] | 2467 | ci.ndm_used = jiffies_to_clock_t(now - neigh->used); |
| 2468 | ci.ndm_confirmed = jiffies_to_clock_t(now - neigh->confirmed); |
| 2469 | ci.ndm_updated = jiffies_to_clock_t(now - neigh->updated); |
Reshetova, Elena | 9f23743 | 2017-06-30 13:07:55 +0300 | [diff] [blame] | 2470 | ci.ndm_refcnt = refcount_read(&neigh->refcnt) - 1; |
Thomas Graf | 8b8aec5 | 2006-08-07 17:56:37 -0700 | [diff] [blame] | 2471 | read_unlock_bh(&neigh->lock); |
| 2472 | |
David S. Miller | 9a6308d | 2012-04-01 20:06:28 -0400 | [diff] [blame] | 2473 | if (nla_put_u32(skb, NDA_PROBES, atomic_read(&neigh->probes)) || |
| 2474 | nla_put(skb, NDA_CACHEINFO, sizeof(ci), &ci)) |
| 2475 | goto nla_put_failure; |
Thomas Graf | 8b8aec5 | 2006-08-07 17:56:37 -0700 | [diff] [blame] | 2476 | |
David Ahern | df9b0e3 | 2018-12-15 14:09:06 -0800 | [diff] [blame] | 2477 | if (neigh->protocol && nla_put_u8(skb, NDA_PROTOCOL, neigh->protocol)) |
| 2478 | goto nla_put_failure; |
| 2479 | |
Johannes Berg | 053c095 | 2015-01-16 22:09:00 +0100 | [diff] [blame] | 2480 | nlmsg_end(skb, nlh); |
| 2481 | return 0; |
Thomas Graf | 8b8aec5 | 2006-08-07 17:56:37 -0700 | [diff] [blame] | 2482 | |
| 2483 | nla_put_failure: |
Patrick McHardy | 2693256 | 2007-01-31 23:16:40 -0800 | [diff] [blame] | 2484 | nlmsg_cancel(skb, nlh); |
| 2485 | return -EMSGSIZE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2486 | } |
| 2487 | |
Tony Zelenoff | 84920c1 | 2012-01-26 22:28:58 +0000 | [diff] [blame] | 2488 | static int pneigh_fill_info(struct sk_buff *skb, struct pneigh_entry *pn, |
| 2489 | u32 pid, u32 seq, int type, unsigned int flags, |
| 2490 | struct neigh_table *tbl) |
| 2491 | { |
| 2492 | struct nlmsghdr *nlh; |
| 2493 | struct ndmsg *ndm; |
| 2494 | |
| 2495 | nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), flags); |
| 2496 | if (nlh == NULL) |
| 2497 | return -EMSGSIZE; |
| 2498 | |
| 2499 | ndm = nlmsg_data(nlh); |
| 2500 | ndm->ndm_family = tbl->family; |
| 2501 | ndm->ndm_pad1 = 0; |
| 2502 | ndm->ndm_pad2 = 0; |
| 2503 | ndm->ndm_flags = pn->flags | NTF_PROXY; |
Jun Zhao | 545469f | 2014-07-26 00:38:59 +0800 | [diff] [blame] | 2504 | ndm->ndm_type = RTN_UNICAST; |
Konstantin Khlebnikov | 6adc5fd | 2015-12-01 01:14:48 +0300 | [diff] [blame] | 2505 | ndm->ndm_ifindex = pn->dev ? pn->dev->ifindex : 0; |
Tony Zelenoff | 84920c1 | 2012-01-26 22:28:58 +0000 | [diff] [blame] | 2506 | ndm->ndm_state = NUD_NONE; |
| 2507 | |
David S. Miller | 9a6308d | 2012-04-01 20:06:28 -0400 | [diff] [blame] | 2508 | if (nla_put(skb, NDA_DST, tbl->key_len, pn->key)) |
| 2509 | goto nla_put_failure; |
Tony Zelenoff | 84920c1 | 2012-01-26 22:28:58 +0000 | [diff] [blame] | 2510 | |
David Ahern | df9b0e3 | 2018-12-15 14:09:06 -0800 | [diff] [blame] | 2511 | if (pn->protocol && nla_put_u8(skb, NDA_PROTOCOL, pn->protocol)) |
| 2512 | goto nla_put_failure; |
| 2513 | |
Johannes Berg | 053c095 | 2015-01-16 22:09:00 +0100 | [diff] [blame] | 2514 | nlmsg_end(skb, nlh); |
| 2515 | return 0; |
Tony Zelenoff | 84920c1 | 2012-01-26 22:28:58 +0000 | [diff] [blame] | 2516 | |
| 2517 | nla_put_failure: |
| 2518 | nlmsg_cancel(skb, nlh); |
| 2519 | return -EMSGSIZE; |
| 2520 | } |
| 2521 | |
Roopa Prabhu | 7b8f7a4 | 2017-03-19 22:01:28 -0700 | [diff] [blame] | 2522 | static void neigh_update_notify(struct neighbour *neigh, u32 nlmsg_pid) |
Thomas Graf | d961db3 | 2007-08-08 23:12:56 -0700 | [diff] [blame] | 2523 | { |
| 2524 | call_netevent_notifiers(NETEVENT_NEIGH_UPDATE, neigh); |
Roopa Prabhu | 7b8f7a4 | 2017-03-19 22:01:28 -0700 | [diff] [blame] | 2525 | __neigh_notify(neigh, RTM_NEWNEIGH, 0, nlmsg_pid); |
Thomas Graf | d961db3 | 2007-08-08 23:12:56 -0700 | [diff] [blame] | 2526 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2527 | |
David Ahern | 21fdd09 | 2015-09-29 09:32:03 -0700 | [diff] [blame] | 2528 | static bool neigh_master_filtered(struct net_device *dev, int master_idx) |
| 2529 | { |
| 2530 | struct net_device *master; |
| 2531 | |
| 2532 | if (!master_idx) |
| 2533 | return false; |
| 2534 | |
Eric Dumazet | aab456d | 2018-10-26 09:33:27 -0700 | [diff] [blame] | 2535 | master = dev ? netdev_master_upper_dev_get(dev) : NULL; |
David Ahern | 21fdd09 | 2015-09-29 09:32:03 -0700 | [diff] [blame] | 2536 | if (!master || master->ifindex != master_idx) |
| 2537 | return true; |
| 2538 | |
| 2539 | return false; |
| 2540 | } |
| 2541 | |
David Ahern | 16660f0 | 2015-10-03 11:43:46 -0700 | [diff] [blame] | 2542 | static bool neigh_ifindex_filtered(struct net_device *dev, int filter_idx) |
| 2543 | { |
Eric Dumazet | aab456d | 2018-10-26 09:33:27 -0700 | [diff] [blame] | 2544 | if (filter_idx && (!dev || dev->ifindex != filter_idx)) |
David Ahern | 16660f0 | 2015-10-03 11:43:46 -0700 | [diff] [blame] | 2545 | return true; |
| 2546 | |
| 2547 | return false; |
| 2548 | } |
| 2549 | |
David Ahern | 6f52f80 | 2018-10-03 15:33:12 -0700 | [diff] [blame] | 2550 | struct neigh_dump_filter { |
| 2551 | int master_idx; |
| 2552 | int dev_idx; |
| 2553 | }; |
| 2554 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2555 | static int neigh_dump_table(struct neigh_table *tbl, struct sk_buff *skb, |
David Ahern | 6f52f80 | 2018-10-03 15:33:12 -0700 | [diff] [blame] | 2556 | struct netlink_callback *cb, |
| 2557 | struct neigh_dump_filter *filter) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2558 | { |
Eric Dumazet | 767e97e | 2010-10-06 17:49:21 -0700 | [diff] [blame] | 2559 | struct net *net = sock_net(skb->sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2560 | struct neighbour *n; |
| 2561 | int rc, h, s_h = cb->args[1]; |
| 2562 | int idx, s_idx = idx = cb->args[2]; |
Eric Dumazet | d6bf781 | 2010-10-04 06:15:44 +0000 | [diff] [blame] | 2563 | struct neigh_hash_table *nht; |
David Ahern | 21fdd09 | 2015-09-29 09:32:03 -0700 | [diff] [blame] | 2564 | unsigned int flags = NLM_F_MULTI; |
David Ahern | 21fdd09 | 2015-09-29 09:32:03 -0700 | [diff] [blame] | 2565 | |
David Ahern | 6f52f80 | 2018-10-03 15:33:12 -0700 | [diff] [blame] | 2566 | if (filter->dev_idx || filter->master_idx) |
| 2567 | flags |= NLM_F_DUMP_FILTERED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2568 | |
Eric Dumazet | d6bf781 | 2010-10-04 06:15:44 +0000 | [diff] [blame] | 2569 | rcu_read_lock_bh(); |
| 2570 | nht = rcu_dereference_bh(tbl->nht); |
| 2571 | |
Eric Dumazet | 4bd6683b | 2012-06-07 04:58:35 +0000 | [diff] [blame] | 2572 | for (h = s_h; h < (1 << nht->hash_shift); h++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2573 | if (h > s_h) |
| 2574 | s_idx = 0; |
Eric Dumazet | 767e97e | 2010-10-06 17:49:21 -0700 | [diff] [blame] | 2575 | for (n = rcu_dereference_bh(nht->hash_buckets[h]), idx = 0; |
| 2576 | n != NULL; |
| 2577 | n = rcu_dereference_bh(n->next)) { |
Zhang Shengju | 18502ac | 2016-11-30 11:24:42 +0800 | [diff] [blame] | 2578 | if (idx < s_idx || !net_eq(dev_net(n->dev), net)) |
| 2579 | goto next; |
David Ahern | 6f52f80 | 2018-10-03 15:33:12 -0700 | [diff] [blame] | 2580 | if (neigh_ifindex_filtered(n->dev, filter->dev_idx) || |
| 2581 | neigh_master_filtered(n->dev, filter->master_idx)) |
Gautam Kachroo | efc683f | 2009-02-06 00:52:04 -0800 | [diff] [blame] | 2582 | goto next; |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 2583 | if (neigh_fill_info(skb, n, NETLINK_CB(cb->skb).portid, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2584 | cb->nlh->nlmsg_seq, |
Jamal Hadi Salim | b6544c0 | 2005-06-18 22:54:12 -0700 | [diff] [blame] | 2585 | RTM_NEWNEIGH, |
David Ahern | 21fdd09 | 2015-09-29 09:32:03 -0700 | [diff] [blame] | 2586 | flags) < 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2587 | rc = -1; |
| 2588 | goto out; |
| 2589 | } |
Eric Dumazet | 767e97e | 2010-10-06 17:49:21 -0700 | [diff] [blame] | 2590 | next: |
Gautam Kachroo | efc683f | 2009-02-06 00:52:04 -0800 | [diff] [blame] | 2591 | idx++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2592 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2593 | } |
| 2594 | rc = skb->len; |
| 2595 | out: |
Eric Dumazet | d6bf781 | 2010-10-04 06:15:44 +0000 | [diff] [blame] | 2596 | rcu_read_unlock_bh(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2597 | cb->args[1] = h; |
| 2598 | cb->args[2] = idx; |
| 2599 | return rc; |
| 2600 | } |
| 2601 | |
Tony Zelenoff | 84920c1 | 2012-01-26 22:28:58 +0000 | [diff] [blame] | 2602 | static int pneigh_dump_table(struct neigh_table *tbl, struct sk_buff *skb, |
David Ahern | 6f52f80 | 2018-10-03 15:33:12 -0700 | [diff] [blame] | 2603 | struct netlink_callback *cb, |
| 2604 | struct neigh_dump_filter *filter) |
Tony Zelenoff | 84920c1 | 2012-01-26 22:28:58 +0000 | [diff] [blame] | 2605 | { |
| 2606 | struct pneigh_entry *n; |
| 2607 | struct net *net = sock_net(skb->sk); |
| 2608 | int rc, h, s_h = cb->args[3]; |
| 2609 | int idx, s_idx = idx = cb->args[4]; |
David Ahern | 6f52f80 | 2018-10-03 15:33:12 -0700 | [diff] [blame] | 2610 | unsigned int flags = NLM_F_MULTI; |
| 2611 | |
| 2612 | if (filter->dev_idx || filter->master_idx) |
| 2613 | flags |= NLM_F_DUMP_FILTERED; |
Tony Zelenoff | 84920c1 | 2012-01-26 22:28:58 +0000 | [diff] [blame] | 2614 | |
| 2615 | read_lock_bh(&tbl->lock); |
| 2616 | |
Eric Dumazet | 4bd6683b | 2012-06-07 04:58:35 +0000 | [diff] [blame] | 2617 | for (h = s_h; h <= PNEIGH_HASHMASK; h++) { |
Tony Zelenoff | 84920c1 | 2012-01-26 22:28:58 +0000 | [diff] [blame] | 2618 | if (h > s_h) |
| 2619 | s_idx = 0; |
| 2620 | for (n = tbl->phash_buckets[h], idx = 0; n; n = n->next) { |
Zhang Shengju | 18502ac | 2016-11-30 11:24:42 +0800 | [diff] [blame] | 2621 | if (idx < s_idx || pneigh_net(n) != net) |
Tony Zelenoff | 84920c1 | 2012-01-26 22:28:58 +0000 | [diff] [blame] | 2622 | goto next; |
David Ahern | 6f52f80 | 2018-10-03 15:33:12 -0700 | [diff] [blame] | 2623 | if (neigh_ifindex_filtered(n->dev, filter->dev_idx) || |
| 2624 | neigh_master_filtered(n->dev, filter->master_idx)) |
| 2625 | goto next; |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 2626 | if (pneigh_fill_info(skb, n, NETLINK_CB(cb->skb).portid, |
Tony Zelenoff | 84920c1 | 2012-01-26 22:28:58 +0000 | [diff] [blame] | 2627 | cb->nlh->nlmsg_seq, |
David Ahern | 6f52f80 | 2018-10-03 15:33:12 -0700 | [diff] [blame] | 2628 | RTM_NEWNEIGH, flags, tbl) < 0) { |
Tony Zelenoff | 84920c1 | 2012-01-26 22:28:58 +0000 | [diff] [blame] | 2629 | read_unlock_bh(&tbl->lock); |
| 2630 | rc = -1; |
| 2631 | goto out; |
| 2632 | } |
| 2633 | next: |
| 2634 | idx++; |
| 2635 | } |
| 2636 | } |
| 2637 | |
| 2638 | read_unlock_bh(&tbl->lock); |
| 2639 | rc = skb->len; |
| 2640 | out: |
| 2641 | cb->args[3] = h; |
| 2642 | cb->args[4] = idx; |
| 2643 | return rc; |
| 2644 | |
| 2645 | } |
| 2646 | |
David Ahern | 51183d2 | 2018-10-07 20:16:36 -0700 | [diff] [blame] | 2647 | static int neigh_valid_dump_req(const struct nlmsghdr *nlh, |
| 2648 | bool strict_check, |
| 2649 | struct neigh_dump_filter *filter, |
| 2650 | struct netlink_ext_ack *extack) |
| 2651 | { |
| 2652 | struct nlattr *tb[NDA_MAX + 1]; |
| 2653 | int err, i; |
| 2654 | |
| 2655 | if (strict_check) { |
| 2656 | struct ndmsg *ndm; |
| 2657 | |
| 2658 | if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ndm))) { |
| 2659 | NL_SET_ERR_MSG(extack, "Invalid header for neighbor dump request"); |
| 2660 | return -EINVAL; |
| 2661 | } |
| 2662 | |
| 2663 | ndm = nlmsg_data(nlh); |
| 2664 | if (ndm->ndm_pad1 || ndm->ndm_pad2 || ndm->ndm_ifindex || |
David Ahern | c0fde87 | 2018-12-19 16:54:38 -0800 | [diff] [blame] | 2665 | ndm->ndm_state || ndm->ndm_type) { |
David Ahern | 51183d2 | 2018-10-07 20:16:36 -0700 | [diff] [blame] | 2666 | NL_SET_ERR_MSG(extack, "Invalid values in header for neighbor dump request"); |
| 2667 | return -EINVAL; |
| 2668 | } |
| 2669 | |
David Ahern | c0fde87 | 2018-12-19 16:54:38 -0800 | [diff] [blame] | 2670 | if (ndm->ndm_flags & ~NTF_PROXY) { |
| 2671 | NL_SET_ERR_MSG(extack, "Invalid flags in header for neighbor dump request"); |
| 2672 | return -EINVAL; |
| 2673 | } |
| 2674 | |
Johannes Berg | 8cb0817 | 2019-04-26 14:07:28 +0200 | [diff] [blame] | 2675 | err = nlmsg_parse_deprecated_strict(nlh, sizeof(struct ndmsg), |
| 2676 | tb, NDA_MAX, nda_policy, |
| 2677 | extack); |
David Ahern | 51183d2 | 2018-10-07 20:16:36 -0700 | [diff] [blame] | 2678 | } else { |
Johannes Berg | 8cb0817 | 2019-04-26 14:07:28 +0200 | [diff] [blame] | 2679 | err = nlmsg_parse_deprecated(nlh, sizeof(struct ndmsg), tb, |
| 2680 | NDA_MAX, nda_policy, extack); |
David Ahern | 51183d2 | 2018-10-07 20:16:36 -0700 | [diff] [blame] | 2681 | } |
| 2682 | if (err < 0) |
| 2683 | return err; |
| 2684 | |
| 2685 | for (i = 0; i <= NDA_MAX; ++i) { |
| 2686 | if (!tb[i]) |
| 2687 | continue; |
| 2688 | |
| 2689 | /* all new attributes should require strict_check */ |
| 2690 | switch (i) { |
| 2691 | case NDA_IFINDEX: |
David Ahern | 51183d2 | 2018-10-07 20:16:36 -0700 | [diff] [blame] | 2692 | filter->dev_idx = nla_get_u32(tb[i]); |
| 2693 | break; |
| 2694 | case NDA_MASTER: |
David Ahern | 51183d2 | 2018-10-07 20:16:36 -0700 | [diff] [blame] | 2695 | filter->master_idx = nla_get_u32(tb[i]); |
| 2696 | break; |
| 2697 | default: |
| 2698 | if (strict_check) { |
| 2699 | NL_SET_ERR_MSG(extack, "Unsupported attribute in neighbor dump request"); |
| 2700 | return -EINVAL; |
| 2701 | } |
| 2702 | } |
| 2703 | } |
| 2704 | |
| 2705 | return 0; |
| 2706 | } |
| 2707 | |
Thomas Graf | c8822a4 | 2007-03-22 11:50:06 -0700 | [diff] [blame] | 2708 | static int neigh_dump_info(struct sk_buff *skb, struct netlink_callback *cb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2709 | { |
David Ahern | 6f52f80 | 2018-10-03 15:33:12 -0700 | [diff] [blame] | 2710 | const struct nlmsghdr *nlh = cb->nlh; |
| 2711 | struct neigh_dump_filter filter = {}; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2712 | struct neigh_table *tbl; |
| 2713 | int t, family, s_t; |
Tony Zelenoff | 84920c1 | 2012-01-26 22:28:58 +0000 | [diff] [blame] | 2714 | int proxy = 0; |
Eric Dumazet | 4bd6683b | 2012-06-07 04:58:35 +0000 | [diff] [blame] | 2715 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2716 | |
David Ahern | 6f52f80 | 2018-10-03 15:33:12 -0700 | [diff] [blame] | 2717 | family = ((struct rtgenmsg *)nlmsg_data(nlh))->rtgen_family; |
Tony Zelenoff | 84920c1 | 2012-01-26 22:28:58 +0000 | [diff] [blame] | 2718 | |
| 2719 | /* check for full ndmsg structure presence, family member is |
| 2720 | * the same for both structures |
| 2721 | */ |
David Ahern | 6f52f80 | 2018-10-03 15:33:12 -0700 | [diff] [blame] | 2722 | if (nlmsg_len(nlh) >= sizeof(struct ndmsg) && |
| 2723 | ((struct ndmsg *)nlmsg_data(nlh))->ndm_flags == NTF_PROXY) |
Tony Zelenoff | 84920c1 | 2012-01-26 22:28:58 +0000 | [diff] [blame] | 2724 | proxy = 1; |
| 2725 | |
David Ahern | 51183d2 | 2018-10-07 20:16:36 -0700 | [diff] [blame] | 2726 | err = neigh_valid_dump_req(nlh, cb->strict_check, &filter, cb->extack); |
| 2727 | if (err < 0 && cb->strict_check) |
| 2728 | return err; |
| 2729 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2730 | s_t = cb->args[0]; |
| 2731 | |
WANG Cong | d7480fd | 2014-11-10 15:59:36 -0800 | [diff] [blame] | 2732 | for (t = 0; t < NEIGH_NR_TABLES; t++) { |
| 2733 | tbl = neigh_tables[t]; |
| 2734 | |
| 2735 | if (!tbl) |
| 2736 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2737 | if (t < s_t || (family && tbl->family != family)) |
| 2738 | continue; |
| 2739 | if (t > s_t) |
| 2740 | memset(&cb->args[1], 0, sizeof(cb->args) - |
| 2741 | sizeof(cb->args[0])); |
Tony Zelenoff | 84920c1 | 2012-01-26 22:28:58 +0000 | [diff] [blame] | 2742 | if (proxy) |
David Ahern | 6f52f80 | 2018-10-03 15:33:12 -0700 | [diff] [blame] | 2743 | err = pneigh_dump_table(tbl, skb, cb, &filter); |
Tony Zelenoff | 84920c1 | 2012-01-26 22:28:58 +0000 | [diff] [blame] | 2744 | else |
David Ahern | 6f52f80 | 2018-10-03 15:33:12 -0700 | [diff] [blame] | 2745 | err = neigh_dump_table(tbl, skb, cb, &filter); |
Eric Dumazet | 4bd6683b | 2012-06-07 04:58:35 +0000 | [diff] [blame] | 2746 | if (err < 0) |
| 2747 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2748 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2749 | |
| 2750 | cb->args[0] = t; |
| 2751 | return skb->len; |
| 2752 | } |
| 2753 | |
Roopa Prabhu | 82cbb5c | 2018-12-19 12:51:38 -0800 | [diff] [blame] | 2754 | static int neigh_valid_get_req(const struct nlmsghdr *nlh, |
| 2755 | struct neigh_table **tbl, |
| 2756 | void **dst, int *dev_idx, u8 *ndm_flags, |
| 2757 | struct netlink_ext_ack *extack) |
| 2758 | { |
| 2759 | struct nlattr *tb[NDA_MAX + 1]; |
| 2760 | struct ndmsg *ndm; |
| 2761 | int err, i; |
| 2762 | |
| 2763 | if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ndm))) { |
| 2764 | NL_SET_ERR_MSG(extack, "Invalid header for neighbor get request"); |
| 2765 | return -EINVAL; |
| 2766 | } |
| 2767 | |
| 2768 | ndm = nlmsg_data(nlh); |
| 2769 | if (ndm->ndm_pad1 || ndm->ndm_pad2 || ndm->ndm_state || |
| 2770 | ndm->ndm_type) { |
| 2771 | NL_SET_ERR_MSG(extack, "Invalid values in header for neighbor get request"); |
| 2772 | return -EINVAL; |
| 2773 | } |
| 2774 | |
| 2775 | if (ndm->ndm_flags & ~NTF_PROXY) { |
| 2776 | NL_SET_ERR_MSG(extack, "Invalid flags in header for neighbor get request"); |
| 2777 | return -EINVAL; |
| 2778 | } |
| 2779 | |
Johannes Berg | 8cb0817 | 2019-04-26 14:07:28 +0200 | [diff] [blame] | 2780 | err = nlmsg_parse_deprecated_strict(nlh, sizeof(struct ndmsg), tb, |
| 2781 | NDA_MAX, nda_policy, extack); |
Roopa Prabhu | 82cbb5c | 2018-12-19 12:51:38 -0800 | [diff] [blame] | 2782 | if (err < 0) |
| 2783 | return err; |
| 2784 | |
| 2785 | *ndm_flags = ndm->ndm_flags; |
| 2786 | *dev_idx = ndm->ndm_ifindex; |
| 2787 | *tbl = neigh_find_table(ndm->ndm_family); |
| 2788 | if (*tbl == NULL) { |
| 2789 | NL_SET_ERR_MSG(extack, "Unsupported family in header for neighbor get request"); |
| 2790 | return -EAFNOSUPPORT; |
| 2791 | } |
| 2792 | |
| 2793 | for (i = 0; i <= NDA_MAX; ++i) { |
| 2794 | if (!tb[i]) |
| 2795 | continue; |
| 2796 | |
| 2797 | switch (i) { |
| 2798 | case NDA_DST: |
| 2799 | if (nla_len(tb[i]) != (int)(*tbl)->key_len) { |
| 2800 | NL_SET_ERR_MSG(extack, "Invalid network address in neighbor get request"); |
| 2801 | return -EINVAL; |
| 2802 | } |
| 2803 | *dst = nla_data(tb[i]); |
| 2804 | break; |
| 2805 | default: |
| 2806 | NL_SET_ERR_MSG(extack, "Unsupported attribute in neighbor get request"); |
| 2807 | return -EINVAL; |
| 2808 | } |
| 2809 | } |
| 2810 | |
| 2811 | return 0; |
| 2812 | } |
| 2813 | |
| 2814 | static inline size_t neigh_nlmsg_size(void) |
| 2815 | { |
| 2816 | return NLMSG_ALIGN(sizeof(struct ndmsg)) |
| 2817 | + nla_total_size(MAX_ADDR_LEN) /* NDA_DST */ |
| 2818 | + nla_total_size(MAX_ADDR_LEN) /* NDA_LLADDR */ |
| 2819 | + nla_total_size(sizeof(struct nda_cacheinfo)) |
| 2820 | + nla_total_size(4) /* NDA_PROBES */ |
| 2821 | + nla_total_size(1); /* NDA_PROTOCOL */ |
| 2822 | } |
| 2823 | |
| 2824 | static int neigh_get_reply(struct net *net, struct neighbour *neigh, |
| 2825 | u32 pid, u32 seq) |
| 2826 | { |
| 2827 | struct sk_buff *skb; |
| 2828 | int err = 0; |
| 2829 | |
| 2830 | skb = nlmsg_new(neigh_nlmsg_size(), GFP_KERNEL); |
| 2831 | if (!skb) |
| 2832 | return -ENOBUFS; |
| 2833 | |
| 2834 | err = neigh_fill_info(skb, neigh, pid, seq, RTM_NEWNEIGH, 0); |
| 2835 | if (err) { |
| 2836 | kfree_skb(skb); |
| 2837 | goto errout; |
| 2838 | } |
| 2839 | |
| 2840 | err = rtnl_unicast(skb, net, pid); |
| 2841 | errout: |
| 2842 | return err; |
| 2843 | } |
| 2844 | |
| 2845 | static inline size_t pneigh_nlmsg_size(void) |
| 2846 | { |
| 2847 | return NLMSG_ALIGN(sizeof(struct ndmsg)) |
Colin Ian King | 463561e | 2018-12-20 16:50:50 +0000 | [diff] [blame] | 2848 | + nla_total_size(MAX_ADDR_LEN) /* NDA_DST */ |
Roopa Prabhu | 82cbb5c | 2018-12-19 12:51:38 -0800 | [diff] [blame] | 2849 | + nla_total_size(1); /* NDA_PROTOCOL */ |
| 2850 | } |
| 2851 | |
| 2852 | static int pneigh_get_reply(struct net *net, struct pneigh_entry *neigh, |
| 2853 | u32 pid, u32 seq, struct neigh_table *tbl) |
| 2854 | { |
| 2855 | struct sk_buff *skb; |
| 2856 | int err = 0; |
| 2857 | |
| 2858 | skb = nlmsg_new(pneigh_nlmsg_size(), GFP_KERNEL); |
| 2859 | if (!skb) |
| 2860 | return -ENOBUFS; |
| 2861 | |
| 2862 | err = pneigh_fill_info(skb, neigh, pid, seq, RTM_NEWNEIGH, 0, tbl); |
| 2863 | if (err) { |
| 2864 | kfree_skb(skb); |
| 2865 | goto errout; |
| 2866 | } |
| 2867 | |
| 2868 | err = rtnl_unicast(skb, net, pid); |
| 2869 | errout: |
| 2870 | return err; |
| 2871 | } |
| 2872 | |
| 2873 | static int neigh_get(struct sk_buff *in_skb, struct nlmsghdr *nlh, |
| 2874 | struct netlink_ext_ack *extack) |
| 2875 | { |
| 2876 | struct net *net = sock_net(in_skb->sk); |
| 2877 | struct net_device *dev = NULL; |
| 2878 | struct neigh_table *tbl = NULL; |
| 2879 | struct neighbour *neigh; |
| 2880 | void *dst = NULL; |
| 2881 | u8 ndm_flags = 0; |
| 2882 | int dev_idx = 0; |
| 2883 | int err; |
| 2884 | |
| 2885 | err = neigh_valid_get_req(nlh, &tbl, &dst, &dev_idx, &ndm_flags, |
| 2886 | extack); |
| 2887 | if (err < 0) |
| 2888 | return err; |
| 2889 | |
| 2890 | if (dev_idx) { |
| 2891 | dev = __dev_get_by_index(net, dev_idx); |
| 2892 | if (!dev) { |
| 2893 | NL_SET_ERR_MSG(extack, "Unknown device ifindex"); |
| 2894 | return -ENODEV; |
| 2895 | } |
| 2896 | } |
| 2897 | |
| 2898 | if (!dst) { |
| 2899 | NL_SET_ERR_MSG(extack, "Network address not specified"); |
| 2900 | return -EINVAL; |
| 2901 | } |
| 2902 | |
| 2903 | if (ndm_flags & NTF_PROXY) { |
| 2904 | struct pneigh_entry *pn; |
| 2905 | |
| 2906 | pn = pneigh_lookup(tbl, net, dst, dev, 0); |
| 2907 | if (!pn) { |
| 2908 | NL_SET_ERR_MSG(extack, "Proxy neighbour entry not found"); |
| 2909 | return -ENOENT; |
| 2910 | } |
| 2911 | return pneigh_get_reply(net, pn, NETLINK_CB(in_skb).portid, |
| 2912 | nlh->nlmsg_seq, tbl); |
| 2913 | } |
| 2914 | |
| 2915 | if (!dev) { |
| 2916 | NL_SET_ERR_MSG(extack, "No device specified"); |
| 2917 | return -EINVAL; |
| 2918 | } |
| 2919 | |
| 2920 | neigh = neigh_lookup(tbl, dst, dev); |
| 2921 | if (!neigh) { |
| 2922 | NL_SET_ERR_MSG(extack, "Neighbour entry not found"); |
| 2923 | return -ENOENT; |
| 2924 | } |
| 2925 | |
| 2926 | err = neigh_get_reply(net, neigh, NETLINK_CB(in_skb).portid, |
| 2927 | nlh->nlmsg_seq); |
| 2928 | |
| 2929 | neigh_release(neigh); |
| 2930 | |
| 2931 | return err; |
| 2932 | } |
| 2933 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2934 | void neigh_for_each(struct neigh_table *tbl, void (*cb)(struct neighbour *, void *), void *cookie) |
| 2935 | { |
| 2936 | int chain; |
Eric Dumazet | d6bf781 | 2010-10-04 06:15:44 +0000 | [diff] [blame] | 2937 | struct neigh_hash_table *nht; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2938 | |
Eric Dumazet | d6bf781 | 2010-10-04 06:15:44 +0000 | [diff] [blame] | 2939 | rcu_read_lock_bh(); |
| 2940 | nht = rcu_dereference_bh(tbl->nht); |
| 2941 | |
Eric Dumazet | 767e97e | 2010-10-06 17:49:21 -0700 | [diff] [blame] | 2942 | read_lock(&tbl->lock); /* avoid resizes */ |
David S. Miller | cd08933 | 2011-07-11 01:28:12 -0700 | [diff] [blame] | 2943 | for (chain = 0; chain < (1 << nht->hash_shift); chain++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2944 | struct neighbour *n; |
| 2945 | |
Eric Dumazet | 767e97e | 2010-10-06 17:49:21 -0700 | [diff] [blame] | 2946 | for (n = rcu_dereference_bh(nht->hash_buckets[chain]); |
| 2947 | n != NULL; |
| 2948 | n = rcu_dereference_bh(n->next)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2949 | cb(n, cookie); |
| 2950 | } |
Eric Dumazet | d6bf781 | 2010-10-04 06:15:44 +0000 | [diff] [blame] | 2951 | read_unlock(&tbl->lock); |
| 2952 | rcu_read_unlock_bh(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2953 | } |
| 2954 | EXPORT_SYMBOL(neigh_for_each); |
| 2955 | |
| 2956 | /* The tbl->lock must be held as a writer and BH disabled. */ |
| 2957 | void __neigh_for_each_release(struct neigh_table *tbl, |
| 2958 | int (*cb)(struct neighbour *)) |
| 2959 | { |
| 2960 | int chain; |
Eric Dumazet | d6bf781 | 2010-10-04 06:15:44 +0000 | [diff] [blame] | 2961 | struct neigh_hash_table *nht; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2962 | |
Eric Dumazet | d6bf781 | 2010-10-04 06:15:44 +0000 | [diff] [blame] | 2963 | nht = rcu_dereference_protected(tbl->nht, |
| 2964 | lockdep_is_held(&tbl->lock)); |
David S. Miller | cd08933 | 2011-07-11 01:28:12 -0700 | [diff] [blame] | 2965 | for (chain = 0; chain < (1 << nht->hash_shift); chain++) { |
Eric Dumazet | 767e97e | 2010-10-06 17:49:21 -0700 | [diff] [blame] | 2966 | struct neighbour *n; |
| 2967 | struct neighbour __rcu **np; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2968 | |
Eric Dumazet | d6bf781 | 2010-10-04 06:15:44 +0000 | [diff] [blame] | 2969 | np = &nht->hash_buckets[chain]; |
Eric Dumazet | 767e97e | 2010-10-06 17:49:21 -0700 | [diff] [blame] | 2970 | while ((n = rcu_dereference_protected(*np, |
| 2971 | lockdep_is_held(&tbl->lock))) != NULL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2972 | int release; |
| 2973 | |
| 2974 | write_lock(&n->lock); |
| 2975 | release = cb(n); |
| 2976 | if (release) { |
Eric Dumazet | 767e97e | 2010-10-06 17:49:21 -0700 | [diff] [blame] | 2977 | rcu_assign_pointer(*np, |
| 2978 | rcu_dereference_protected(n->next, |
| 2979 | lockdep_is_held(&tbl->lock))); |
David Ahern | 5895631 | 2018-12-07 12:24:57 -0800 | [diff] [blame] | 2980 | neigh_mark_dead(n); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2981 | } else |
| 2982 | np = &n->next; |
| 2983 | write_unlock(&n->lock); |
Thomas Graf | 4f49455 | 2007-08-08 23:12:36 -0700 | [diff] [blame] | 2984 | if (release) |
| 2985 | neigh_cleanup_and_release(n); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2986 | } |
| 2987 | } |
| 2988 | } |
| 2989 | EXPORT_SYMBOL(__neigh_for_each_release); |
| 2990 | |
Eric W. Biederman | b79bda3 | 2015-03-07 16:25:56 -0600 | [diff] [blame] | 2991 | int neigh_xmit(int index, struct net_device *dev, |
Eric W. Biederman | 4fd3d7d | 2015-03-03 17:11:16 -0600 | [diff] [blame] | 2992 | const void *addr, struct sk_buff *skb) |
| 2993 | { |
Eric W. Biederman | b79bda3 | 2015-03-07 16:25:56 -0600 | [diff] [blame] | 2994 | int err = -EAFNOSUPPORT; |
| 2995 | if (likely(index < NEIGH_NR_TABLES)) { |
Eric W. Biederman | 4fd3d7d | 2015-03-03 17:11:16 -0600 | [diff] [blame] | 2996 | struct neigh_table *tbl; |
| 2997 | struct neighbour *neigh; |
| 2998 | |
Eric W. Biederman | b79bda3 | 2015-03-07 16:25:56 -0600 | [diff] [blame] | 2999 | tbl = neigh_tables[index]; |
Eric W. Biederman | 4fd3d7d | 2015-03-03 17:11:16 -0600 | [diff] [blame] | 3000 | if (!tbl) |
| 3001 | goto out; |
David Barroso | b560f03 | 2016-06-28 11:16:43 +0300 | [diff] [blame] | 3002 | rcu_read_lock_bh(); |
David Ahern | 4b2a2bf | 2019-05-01 18:18:42 -0700 | [diff] [blame] | 3003 | if (index == NEIGH_ARP_TABLE) { |
| 3004 | u32 key = *((u32 *)addr); |
| 3005 | |
| 3006 | neigh = __ipv4_neigh_lookup_noref(dev, key); |
| 3007 | } else { |
| 3008 | neigh = __neigh_lookup_noref(tbl, addr, dev); |
| 3009 | } |
Eric W. Biederman | 4fd3d7d | 2015-03-03 17:11:16 -0600 | [diff] [blame] | 3010 | if (!neigh) |
| 3011 | neigh = __neigh_create(tbl, addr, dev, false); |
| 3012 | err = PTR_ERR(neigh); |
David Barroso | b560f03 | 2016-06-28 11:16:43 +0300 | [diff] [blame] | 3013 | if (IS_ERR(neigh)) { |
| 3014 | rcu_read_unlock_bh(); |
Eric W. Biederman | 4fd3d7d | 2015-03-03 17:11:16 -0600 | [diff] [blame] | 3015 | goto out_kfree_skb; |
David Barroso | b560f03 | 2016-06-28 11:16:43 +0300 | [diff] [blame] | 3016 | } |
Eric W. Biederman | 4fd3d7d | 2015-03-03 17:11:16 -0600 | [diff] [blame] | 3017 | err = neigh->output(neigh, skb); |
David Barroso | b560f03 | 2016-06-28 11:16:43 +0300 | [diff] [blame] | 3018 | rcu_read_unlock_bh(); |
Eric W. Biederman | 4fd3d7d | 2015-03-03 17:11:16 -0600 | [diff] [blame] | 3019 | } |
Eric W. Biederman | b79bda3 | 2015-03-07 16:25:56 -0600 | [diff] [blame] | 3020 | else if (index == NEIGH_LINK_TABLE) { |
| 3021 | err = dev_hard_header(skb, dev, ntohs(skb->protocol), |
| 3022 | addr, NULL, skb->len); |
| 3023 | if (err < 0) |
| 3024 | goto out_kfree_skb; |
| 3025 | err = dev_queue_xmit(skb); |
| 3026 | } |
Eric W. Biederman | 4fd3d7d | 2015-03-03 17:11:16 -0600 | [diff] [blame] | 3027 | out: |
| 3028 | return err; |
| 3029 | out_kfree_skb: |
| 3030 | kfree_skb(skb); |
| 3031 | goto out; |
| 3032 | } |
| 3033 | EXPORT_SYMBOL(neigh_xmit); |
| 3034 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3035 | #ifdef CONFIG_PROC_FS |
| 3036 | |
| 3037 | static struct neighbour *neigh_get_first(struct seq_file *seq) |
| 3038 | { |
| 3039 | struct neigh_seq_state *state = seq->private; |
YOSHIFUJI Hideaki | 1218854 | 2008-03-26 02:36:06 +0900 | [diff] [blame] | 3040 | struct net *net = seq_file_net(seq); |
Eric Dumazet | d6bf781 | 2010-10-04 06:15:44 +0000 | [diff] [blame] | 3041 | struct neigh_hash_table *nht = state->nht; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3042 | struct neighbour *n = NULL; |
Colin Ian King | f530eed | 2019-07-26 10:46:11 +0100 | [diff] [blame] | 3043 | int bucket; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3044 | |
| 3045 | state->flags &= ~NEIGH_SEQ_IS_PNEIGH; |
David S. Miller | cd08933 | 2011-07-11 01:28:12 -0700 | [diff] [blame] | 3046 | for (bucket = 0; bucket < (1 << nht->hash_shift); bucket++) { |
Eric Dumazet | 767e97e | 2010-10-06 17:49:21 -0700 | [diff] [blame] | 3047 | n = rcu_dereference_bh(nht->hash_buckets[bucket]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3048 | |
| 3049 | while (n) { |
YOSHIFUJI Hideaki | 878628f | 2008-03-26 03:57:35 +0900 | [diff] [blame] | 3050 | if (!net_eq(dev_net(n->dev), net)) |
Eric W. Biederman | 426b530 | 2008-01-24 00:13:18 -0800 | [diff] [blame] | 3051 | goto next; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3052 | if (state->neigh_sub_iter) { |
| 3053 | loff_t fakep = 0; |
| 3054 | void *v; |
| 3055 | |
| 3056 | v = state->neigh_sub_iter(state, n, &fakep); |
| 3057 | if (!v) |
| 3058 | goto next; |
| 3059 | } |
| 3060 | if (!(state->flags & NEIGH_SEQ_SKIP_NOARP)) |
| 3061 | break; |
| 3062 | if (n->nud_state & ~NUD_NOARP) |
| 3063 | break; |
Eric Dumazet | 767e97e | 2010-10-06 17:49:21 -0700 | [diff] [blame] | 3064 | next: |
| 3065 | n = rcu_dereference_bh(n->next); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3066 | } |
| 3067 | |
| 3068 | if (n) |
| 3069 | break; |
| 3070 | } |
| 3071 | state->bucket = bucket; |
| 3072 | |
| 3073 | return n; |
| 3074 | } |
| 3075 | |
| 3076 | static struct neighbour *neigh_get_next(struct seq_file *seq, |
| 3077 | struct neighbour *n, |
| 3078 | loff_t *pos) |
| 3079 | { |
| 3080 | struct neigh_seq_state *state = seq->private; |
YOSHIFUJI Hideaki | 1218854 | 2008-03-26 02:36:06 +0900 | [diff] [blame] | 3081 | struct net *net = seq_file_net(seq); |
Eric Dumazet | d6bf781 | 2010-10-04 06:15:44 +0000 | [diff] [blame] | 3082 | struct neigh_hash_table *nht = state->nht; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3083 | |
| 3084 | if (state->neigh_sub_iter) { |
| 3085 | void *v = state->neigh_sub_iter(state, n, pos); |
| 3086 | if (v) |
| 3087 | return n; |
| 3088 | } |
Eric Dumazet | 767e97e | 2010-10-06 17:49:21 -0700 | [diff] [blame] | 3089 | n = rcu_dereference_bh(n->next); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3090 | |
| 3091 | while (1) { |
| 3092 | while (n) { |
YOSHIFUJI Hideaki | 878628f | 2008-03-26 03:57:35 +0900 | [diff] [blame] | 3093 | if (!net_eq(dev_net(n->dev), net)) |
Eric W. Biederman | 426b530 | 2008-01-24 00:13:18 -0800 | [diff] [blame] | 3094 | goto next; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3095 | if (state->neigh_sub_iter) { |
| 3096 | void *v = state->neigh_sub_iter(state, n, pos); |
| 3097 | if (v) |
| 3098 | return n; |
| 3099 | goto next; |
| 3100 | } |
| 3101 | if (!(state->flags & NEIGH_SEQ_SKIP_NOARP)) |
| 3102 | break; |
| 3103 | |
| 3104 | if (n->nud_state & ~NUD_NOARP) |
| 3105 | break; |
Eric Dumazet | 767e97e | 2010-10-06 17:49:21 -0700 | [diff] [blame] | 3106 | next: |
| 3107 | n = rcu_dereference_bh(n->next); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3108 | } |
| 3109 | |
| 3110 | if (n) |
| 3111 | break; |
| 3112 | |
David S. Miller | cd08933 | 2011-07-11 01:28:12 -0700 | [diff] [blame] | 3113 | if (++state->bucket >= (1 << nht->hash_shift)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3114 | break; |
| 3115 | |
Eric Dumazet | 767e97e | 2010-10-06 17:49:21 -0700 | [diff] [blame] | 3116 | n = rcu_dereference_bh(nht->hash_buckets[state->bucket]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3117 | } |
| 3118 | |
| 3119 | if (n && pos) |
| 3120 | --(*pos); |
| 3121 | return n; |
| 3122 | } |
| 3123 | |
| 3124 | static struct neighbour *neigh_get_idx(struct seq_file *seq, loff_t *pos) |
| 3125 | { |
| 3126 | struct neighbour *n = neigh_get_first(seq); |
| 3127 | |
| 3128 | if (n) { |
Chris Larson | 745e203 | 2008-08-03 01:10:55 -0700 | [diff] [blame] | 3129 | --(*pos); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3130 | while (*pos) { |
| 3131 | n = neigh_get_next(seq, n, pos); |
| 3132 | if (!n) |
| 3133 | break; |
| 3134 | } |
| 3135 | } |
| 3136 | return *pos ? NULL : n; |
| 3137 | } |
| 3138 | |
| 3139 | static struct pneigh_entry *pneigh_get_first(struct seq_file *seq) |
| 3140 | { |
| 3141 | struct neigh_seq_state *state = seq->private; |
YOSHIFUJI Hideaki | 1218854 | 2008-03-26 02:36:06 +0900 | [diff] [blame] | 3142 | struct net *net = seq_file_net(seq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3143 | struct neigh_table *tbl = state->tbl; |
| 3144 | struct pneigh_entry *pn = NULL; |
Yang Li | 48de7c0 | 2021-05-08 18:03:05 +0800 | [diff] [blame] | 3145 | int bucket; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3146 | |
| 3147 | state->flags |= NEIGH_SEQ_IS_PNEIGH; |
| 3148 | for (bucket = 0; bucket <= PNEIGH_HASHMASK; bucket++) { |
| 3149 | pn = tbl->phash_buckets[bucket]; |
YOSHIFUJI Hideaki | 878628f | 2008-03-26 03:57:35 +0900 | [diff] [blame] | 3150 | while (pn && !net_eq(pneigh_net(pn), net)) |
Eric W. Biederman | 426b530 | 2008-01-24 00:13:18 -0800 | [diff] [blame] | 3151 | pn = pn->next; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3152 | if (pn) |
| 3153 | break; |
| 3154 | } |
| 3155 | state->bucket = bucket; |
| 3156 | |
| 3157 | return pn; |
| 3158 | } |
| 3159 | |
| 3160 | static struct pneigh_entry *pneigh_get_next(struct seq_file *seq, |
| 3161 | struct pneigh_entry *pn, |
| 3162 | loff_t *pos) |
| 3163 | { |
| 3164 | struct neigh_seq_state *state = seq->private; |
YOSHIFUJI Hideaki | 1218854 | 2008-03-26 02:36:06 +0900 | [diff] [blame] | 3165 | struct net *net = seq_file_net(seq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3166 | struct neigh_table *tbl = state->tbl; |
| 3167 | |
Jorge Boncompte [DTI2] | df07a94 | 2011-11-25 13:24:49 -0500 | [diff] [blame] | 3168 | do { |
| 3169 | pn = pn->next; |
| 3170 | } while (pn && !net_eq(pneigh_net(pn), net)); |
| 3171 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3172 | while (!pn) { |
| 3173 | if (++state->bucket > PNEIGH_HASHMASK) |
| 3174 | break; |
| 3175 | pn = tbl->phash_buckets[state->bucket]; |
YOSHIFUJI Hideaki | 878628f | 2008-03-26 03:57:35 +0900 | [diff] [blame] | 3176 | while (pn && !net_eq(pneigh_net(pn), net)) |
Eric W. Biederman | 426b530 | 2008-01-24 00:13:18 -0800 | [diff] [blame] | 3177 | pn = pn->next; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3178 | if (pn) |
| 3179 | break; |
| 3180 | } |
| 3181 | |
| 3182 | if (pn && pos) |
| 3183 | --(*pos); |
| 3184 | |
| 3185 | return pn; |
| 3186 | } |
| 3187 | |
| 3188 | static struct pneigh_entry *pneigh_get_idx(struct seq_file *seq, loff_t *pos) |
| 3189 | { |
| 3190 | struct pneigh_entry *pn = pneigh_get_first(seq); |
| 3191 | |
| 3192 | if (pn) { |
Chris Larson | 745e203 | 2008-08-03 01:10:55 -0700 | [diff] [blame] | 3193 | --(*pos); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3194 | while (*pos) { |
| 3195 | pn = pneigh_get_next(seq, pn, pos); |
| 3196 | if (!pn) |
| 3197 | break; |
| 3198 | } |
| 3199 | } |
| 3200 | return *pos ? NULL : pn; |
| 3201 | } |
| 3202 | |
| 3203 | static void *neigh_get_idx_any(struct seq_file *seq, loff_t *pos) |
| 3204 | { |
| 3205 | struct neigh_seq_state *state = seq->private; |
| 3206 | void *rc; |
Chris Larson | 745e203 | 2008-08-03 01:10:55 -0700 | [diff] [blame] | 3207 | loff_t idxpos = *pos; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3208 | |
Chris Larson | 745e203 | 2008-08-03 01:10:55 -0700 | [diff] [blame] | 3209 | rc = neigh_get_idx(seq, &idxpos); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3210 | if (!rc && !(state->flags & NEIGH_SEQ_NEIGH_ONLY)) |
Chris Larson | 745e203 | 2008-08-03 01:10:55 -0700 | [diff] [blame] | 3211 | rc = pneigh_get_idx(seq, &idxpos); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3212 | |
| 3213 | return rc; |
| 3214 | } |
| 3215 | |
| 3216 | void *neigh_seq_start(struct seq_file *seq, loff_t *pos, struct neigh_table *tbl, unsigned int neigh_seq_flags) |
Eric Dumazet | f3e92cb | 2019-06-15 16:28:48 -0700 | [diff] [blame] | 3217 | __acquires(tbl->lock) |
Eric Dumazet | d6bf781 | 2010-10-04 06:15:44 +0000 | [diff] [blame] | 3218 | __acquires(rcu_bh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3219 | { |
| 3220 | struct neigh_seq_state *state = seq->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3221 | |
| 3222 | state->tbl = tbl; |
| 3223 | state->bucket = 0; |
| 3224 | state->flags = (neigh_seq_flags & ~NEIGH_SEQ_IS_PNEIGH); |
| 3225 | |
Eric Dumazet | d6bf781 | 2010-10-04 06:15:44 +0000 | [diff] [blame] | 3226 | rcu_read_lock_bh(); |
| 3227 | state->nht = rcu_dereference_bh(tbl->nht); |
Eric Dumazet | f3e92cb | 2019-06-15 16:28:48 -0700 | [diff] [blame] | 3228 | read_lock(&tbl->lock); |
Eric Dumazet | 767e97e | 2010-10-06 17:49:21 -0700 | [diff] [blame] | 3229 | |
Chris Larson | 745e203 | 2008-08-03 01:10:55 -0700 | [diff] [blame] | 3230 | return *pos ? neigh_get_idx_any(seq, pos) : SEQ_START_TOKEN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3231 | } |
| 3232 | EXPORT_SYMBOL(neigh_seq_start); |
| 3233 | |
| 3234 | void *neigh_seq_next(struct seq_file *seq, void *v, loff_t *pos) |
| 3235 | { |
| 3236 | struct neigh_seq_state *state; |
| 3237 | void *rc; |
| 3238 | |
| 3239 | if (v == SEQ_START_TOKEN) { |
Chris Larson | bff6973 | 2008-08-03 01:02:41 -0700 | [diff] [blame] | 3240 | rc = neigh_get_first(seq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3241 | goto out; |
| 3242 | } |
| 3243 | |
| 3244 | state = seq->private; |
| 3245 | if (!(state->flags & NEIGH_SEQ_IS_PNEIGH)) { |
| 3246 | rc = neigh_get_next(seq, v, NULL); |
| 3247 | if (rc) |
| 3248 | goto out; |
| 3249 | if (!(state->flags & NEIGH_SEQ_NEIGH_ONLY)) |
| 3250 | rc = pneigh_get_first(seq); |
| 3251 | } else { |
| 3252 | BUG_ON(state->flags & NEIGH_SEQ_NEIGH_ONLY); |
| 3253 | rc = pneigh_get_next(seq, v, NULL); |
| 3254 | } |
| 3255 | out: |
| 3256 | ++(*pos); |
| 3257 | return rc; |
| 3258 | } |
| 3259 | EXPORT_SYMBOL(neigh_seq_next); |
| 3260 | |
| 3261 | void neigh_seq_stop(struct seq_file *seq, void *v) |
Eric Dumazet | f3e92cb | 2019-06-15 16:28:48 -0700 | [diff] [blame] | 3262 | __releases(tbl->lock) |
Eric Dumazet | d6bf781 | 2010-10-04 06:15:44 +0000 | [diff] [blame] | 3263 | __releases(rcu_bh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3264 | { |
Eric Dumazet | f3e92cb | 2019-06-15 16:28:48 -0700 | [diff] [blame] | 3265 | struct neigh_seq_state *state = seq->private; |
| 3266 | struct neigh_table *tbl = state->tbl; |
| 3267 | |
| 3268 | read_unlock(&tbl->lock); |
Eric Dumazet | d6bf781 | 2010-10-04 06:15:44 +0000 | [diff] [blame] | 3269 | rcu_read_unlock_bh(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3270 | } |
| 3271 | EXPORT_SYMBOL(neigh_seq_stop); |
| 3272 | |
| 3273 | /* statistics via seq_file */ |
| 3274 | |
| 3275 | static void *neigh_stat_seq_start(struct seq_file *seq, loff_t *pos) |
| 3276 | { |
Christoph Hellwig | 71a5053 | 2018-04-15 10:16:41 +0200 | [diff] [blame] | 3277 | struct neigh_table *tbl = PDE_DATA(file_inode(seq->file)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3278 | int cpu; |
| 3279 | |
| 3280 | if (*pos == 0) |
| 3281 | return SEQ_START_TOKEN; |
YOSHIFUJI Hideaki | 4ec93ed | 2007-02-09 23:24:36 +0900 | [diff] [blame] | 3282 | |
Rusty Russell | 0f23174a | 2008-12-29 12:23:42 +0000 | [diff] [blame] | 3283 | for (cpu = *pos-1; cpu < nr_cpu_ids; ++cpu) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3284 | if (!cpu_possible(cpu)) |
| 3285 | continue; |
| 3286 | *pos = cpu+1; |
| 3287 | return per_cpu_ptr(tbl->stats, cpu); |
| 3288 | } |
| 3289 | return NULL; |
| 3290 | } |
| 3291 | |
| 3292 | static void *neigh_stat_seq_next(struct seq_file *seq, void *v, loff_t *pos) |
| 3293 | { |
Christoph Hellwig | 71a5053 | 2018-04-15 10:16:41 +0200 | [diff] [blame] | 3294 | struct neigh_table *tbl = PDE_DATA(file_inode(seq->file)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3295 | int cpu; |
| 3296 | |
Rusty Russell | 0f23174a | 2008-12-29 12:23:42 +0000 | [diff] [blame] | 3297 | for (cpu = *pos; cpu < nr_cpu_ids; ++cpu) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3298 | if (!cpu_possible(cpu)) |
| 3299 | continue; |
| 3300 | *pos = cpu+1; |
| 3301 | return per_cpu_ptr(tbl->stats, cpu); |
| 3302 | } |
Vasily Averin | 1e3f9f0 | 2020-01-23 10:11:28 +0300 | [diff] [blame] | 3303 | (*pos)++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3304 | return NULL; |
| 3305 | } |
| 3306 | |
| 3307 | static void neigh_stat_seq_stop(struct seq_file *seq, void *v) |
| 3308 | { |
| 3309 | |
| 3310 | } |
| 3311 | |
| 3312 | static int neigh_stat_seq_show(struct seq_file *seq, void *v) |
| 3313 | { |
Christoph Hellwig | 71a5053 | 2018-04-15 10:16:41 +0200 | [diff] [blame] | 3314 | struct neigh_table *tbl = PDE_DATA(file_inode(seq->file)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3315 | struct neigh_statistics *st = v; |
| 3316 | |
| 3317 | if (v == SEQ_START_TOKEN) { |
Rick Jones | fb81139 | 2015-08-07 11:10:37 -0700 | [diff] [blame] | 3318 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3319 | return 0; |
| 3320 | } |
| 3321 | |
| 3322 | seq_printf(seq, "%08x %08lx %08lx %08lx %08lx %08lx %08lx " |
Rick Jones | fb81139 | 2015-08-07 11:10:37 -0700 | [diff] [blame] | 3323 | "%08lx %08lx %08lx %08lx %08lx %08lx\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3324 | atomic_read(&tbl->entries), |
| 3325 | |
| 3326 | st->allocs, |
| 3327 | st->destroys, |
| 3328 | st->hash_grows, |
| 3329 | |
| 3330 | st->lookups, |
| 3331 | st->hits, |
| 3332 | |
| 3333 | st->res_failed, |
| 3334 | |
| 3335 | st->rcv_probes_mcast, |
| 3336 | st->rcv_probes_ucast, |
| 3337 | |
| 3338 | st->periodic_gc_runs, |
Neil Horman | 9a6d276 | 2008-07-16 20:50:49 -0700 | [diff] [blame] | 3339 | st->forced_gc_runs, |
Rick Jones | fb81139 | 2015-08-07 11:10:37 -0700 | [diff] [blame] | 3340 | st->unres_discards, |
| 3341 | st->table_fulls |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3342 | ); |
| 3343 | |
| 3344 | return 0; |
| 3345 | } |
| 3346 | |
Stephen Hemminger | f690808 | 2007-03-12 14:34:29 -0700 | [diff] [blame] | 3347 | static const struct seq_operations neigh_stat_seq_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3348 | .start = neigh_stat_seq_start, |
| 3349 | .next = neigh_stat_seq_next, |
| 3350 | .stop = neigh_stat_seq_stop, |
| 3351 | .show = neigh_stat_seq_show, |
| 3352 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3353 | #endif /* CONFIG_PROC_FS */ |
| 3354 | |
Roopa Prabhu | 7b8f7a4 | 2017-03-19 22:01:28 -0700 | [diff] [blame] | 3355 | static void __neigh_notify(struct neighbour *n, int type, int flags, |
| 3356 | u32 pid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3357 | { |
YOSHIFUJI Hideaki | c346dca | 2008-03-25 21:47:49 +0900 | [diff] [blame] | 3358 | struct net *net = dev_net(n->dev); |
Thomas Graf | 8b8aec5 | 2006-08-07 17:56:37 -0700 | [diff] [blame] | 3359 | struct sk_buff *skb; |
Thomas Graf | b867331 | 2006-08-15 00:33:14 -0700 | [diff] [blame] | 3360 | int err = -ENOBUFS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3361 | |
Thomas Graf | 339bf98 | 2006-11-10 14:10:15 -0800 | [diff] [blame] | 3362 | skb = nlmsg_new(neigh_nlmsg_size(), GFP_ATOMIC); |
Thomas Graf | 8b8aec5 | 2006-08-07 17:56:37 -0700 | [diff] [blame] | 3363 | if (skb == NULL) |
Thomas Graf | b867331 | 2006-08-15 00:33:14 -0700 | [diff] [blame] | 3364 | goto errout; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3365 | |
Roopa Prabhu | 7b8f7a4 | 2017-03-19 22:01:28 -0700 | [diff] [blame] | 3366 | err = neigh_fill_info(skb, n, pid, 0, type, flags); |
Patrick McHardy | 2693256 | 2007-01-31 23:16:40 -0800 | [diff] [blame] | 3367 | if (err < 0) { |
| 3368 | /* -EMSGSIZE implies BUG in neigh_nlmsg_size() */ |
| 3369 | WARN_ON(err == -EMSGSIZE); |
| 3370 | kfree_skb(skb); |
| 3371 | goto errout; |
| 3372 | } |
Pablo Neira Ayuso | 1ce85fe | 2009-02-24 23:18:28 -0800 | [diff] [blame] | 3373 | rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC); |
| 3374 | return; |
Thomas Graf | b867331 | 2006-08-15 00:33:14 -0700 | [diff] [blame] | 3375 | errout: |
| 3376 | if (err < 0) |
Eric W. Biederman | 426b530 | 2008-01-24 00:13:18 -0800 | [diff] [blame] | 3377 | rtnl_set_sk_err(net, RTNLGRP_NEIGH, err); |
Thomas Graf | b867331 | 2006-08-15 00:33:14 -0700 | [diff] [blame] | 3378 | } |
| 3379 | |
| 3380 | void neigh_app_ns(struct neighbour *n) |
| 3381 | { |
Roopa Prabhu | 7b8f7a4 | 2017-03-19 22:01:28 -0700 | [diff] [blame] | 3382 | __neigh_notify(n, RTM_GETNEIGH, NLM_F_REQUEST, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3383 | } |
YOSHIFUJI Hideaki | 0a20450 | 2008-03-24 18:39:10 +0900 | [diff] [blame] | 3384 | EXPORT_SYMBOL(neigh_app_ns); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3385 | |
| 3386 | #ifdef CONFIG_SYSCTL |
Cong Wang | b93196d | 2012-12-06 10:04:04 +0800 | [diff] [blame] | 3387 | static int unres_qlen_max = INT_MAX / SKB_TRUESIZE(ETH_FRAME_LEN); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3388 | |
Joe Perches | fe2c633 | 2013-06-11 23:04:25 -0700 | [diff] [blame] | 3389 | static int proc_unres_qlen(struct ctl_table *ctl, int write, |
Christoph Hellwig | 3292739 | 2020-04-24 08:43:38 +0200 | [diff] [blame] | 3390 | void *buffer, size_t *lenp, loff_t *ppos) |
Eric Dumazet | 8b5c171 | 2011-11-09 12:07:14 +0000 | [diff] [blame] | 3391 | { |
| 3392 | int size, ret; |
Joe Perches | fe2c633 | 2013-06-11 23:04:25 -0700 | [diff] [blame] | 3393 | struct ctl_table tmp = *ctl; |
Eric Dumazet | 8b5c171 | 2011-11-09 12:07:14 +0000 | [diff] [blame] | 3394 | |
Matteo Croce | eec4844 | 2019-07-18 15:58:50 -0700 | [diff] [blame] | 3395 | tmp.extra1 = SYSCTL_ZERO; |
Shan Wei | ce46cc6 | 2012-12-04 18:49:15 +0000 | [diff] [blame] | 3396 | tmp.extra2 = &unres_qlen_max; |
Eric Dumazet | 8b5c171 | 2011-11-09 12:07:14 +0000 | [diff] [blame] | 3397 | tmp.data = &size; |
Shan Wei | ce46cc6 | 2012-12-04 18:49:15 +0000 | [diff] [blame] | 3398 | |
| 3399 | size = *(int *)ctl->data / SKB_TRUESIZE(ETH_FRAME_LEN); |
| 3400 | ret = proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos); |
| 3401 | |
Eric Dumazet | 8b5c171 | 2011-11-09 12:07:14 +0000 | [diff] [blame] | 3402 | if (write && !ret) |
| 3403 | *(int *)ctl->data = size * SKB_TRUESIZE(ETH_FRAME_LEN); |
| 3404 | return ret; |
| 3405 | } |
| 3406 | |
Jiri Pirko | 1d4c8c2 | 2013-12-07 19:26:56 +0100 | [diff] [blame] | 3407 | static struct neigh_parms *neigh_get_dev_parms_rcu(struct net_device *dev, |
| 3408 | int family) |
| 3409 | { |
Jiri Pirko | bba2489 | 2013-12-07 19:26:57 +0100 | [diff] [blame] | 3410 | switch (family) { |
| 3411 | case AF_INET: |
Jiri Pirko | 1d4c8c2 | 2013-12-07 19:26:56 +0100 | [diff] [blame] | 3412 | return __in_dev_arp_parms_get_rcu(dev); |
Jiri Pirko | bba2489 | 2013-12-07 19:26:57 +0100 | [diff] [blame] | 3413 | case AF_INET6: |
| 3414 | return __in6_dev_nd_parms_get_rcu(dev); |
| 3415 | } |
Jiri Pirko | 1d4c8c2 | 2013-12-07 19:26:56 +0100 | [diff] [blame] | 3416 | return NULL; |
| 3417 | } |
| 3418 | |
| 3419 | static void neigh_copy_dflt_parms(struct net *net, struct neigh_parms *p, |
| 3420 | int index) |
| 3421 | { |
| 3422 | struct net_device *dev; |
| 3423 | int family = neigh_parms_family(p); |
| 3424 | |
| 3425 | rcu_read_lock(); |
| 3426 | for_each_netdev_rcu(net, dev) { |
| 3427 | struct neigh_parms *dst_p = |
| 3428 | neigh_get_dev_parms_rcu(dev, family); |
| 3429 | |
| 3430 | if (dst_p && !test_bit(index, dst_p->data_state)) |
| 3431 | dst_p->data[index] = p->data[index]; |
| 3432 | } |
| 3433 | rcu_read_unlock(); |
| 3434 | } |
| 3435 | |
| 3436 | static void neigh_proc_update(struct ctl_table *ctl, int write) |
| 3437 | { |
| 3438 | struct net_device *dev = ctl->extra1; |
| 3439 | struct neigh_parms *p = ctl->extra2; |
Jiri Pirko | 77d47af | 2013-12-10 23:55:07 +0100 | [diff] [blame] | 3440 | struct net *net = neigh_parms_net(p); |
Jiri Pirko | 1d4c8c2 | 2013-12-07 19:26:56 +0100 | [diff] [blame] | 3441 | int index = (int *) ctl->data - p->data; |
| 3442 | |
| 3443 | if (!write) |
| 3444 | return; |
| 3445 | |
| 3446 | set_bit(index, p->data_state); |
Marcus Huewe | 7627ae6 | 2017-02-15 01:00:36 +0100 | [diff] [blame] | 3447 | if (index == NEIGH_VAR_DELAY_PROBE_TIME) |
| 3448 | call_netevent_notifiers(NETEVENT_DELAY_PROBE_TIME_UPDATE, p); |
Jiri Pirko | 1d4c8c2 | 2013-12-07 19:26:56 +0100 | [diff] [blame] | 3449 | if (!dev) /* NULL dev means this is default value */ |
| 3450 | neigh_copy_dflt_parms(net, p, index); |
| 3451 | } |
| 3452 | |
Jiri Pirko | 1f9248e | 2013-12-07 19:26:53 +0100 | [diff] [blame] | 3453 | static int neigh_proc_dointvec_zero_intmax(struct ctl_table *ctl, int write, |
Christoph Hellwig | 3292739 | 2020-04-24 08:43:38 +0200 | [diff] [blame] | 3454 | void *buffer, size_t *lenp, |
| 3455 | loff_t *ppos) |
Jiri Pirko | 1f9248e | 2013-12-07 19:26:53 +0100 | [diff] [blame] | 3456 | { |
| 3457 | struct ctl_table tmp = *ctl; |
Jiri Pirko | 1d4c8c2 | 2013-12-07 19:26:56 +0100 | [diff] [blame] | 3458 | int ret; |
Jiri Pirko | 1f9248e | 2013-12-07 19:26:53 +0100 | [diff] [blame] | 3459 | |
Matteo Croce | eec4844 | 2019-07-18 15:58:50 -0700 | [diff] [blame] | 3460 | tmp.extra1 = SYSCTL_ZERO; |
| 3461 | tmp.extra2 = SYSCTL_INT_MAX; |
Jiri Pirko | 1f9248e | 2013-12-07 19:26:53 +0100 | [diff] [blame] | 3462 | |
Jiri Pirko | 1d4c8c2 | 2013-12-07 19:26:56 +0100 | [diff] [blame] | 3463 | ret = proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos); |
| 3464 | neigh_proc_update(ctl, write); |
| 3465 | return ret; |
Jiri Pirko | 1f9248e | 2013-12-07 19:26:53 +0100 | [diff] [blame] | 3466 | } |
| 3467 | |
Christoph Hellwig | 3292739 | 2020-04-24 08:43:38 +0200 | [diff] [blame] | 3468 | int neigh_proc_dointvec(struct ctl_table *ctl, int write, void *buffer, |
| 3469 | size_t *lenp, loff_t *ppos) |
Jiri Pirko | cb5b09c | 2013-12-07 19:26:54 +0100 | [diff] [blame] | 3470 | { |
Jiri Pirko | 1d4c8c2 | 2013-12-07 19:26:56 +0100 | [diff] [blame] | 3471 | int ret = proc_dointvec(ctl, write, buffer, lenp, ppos); |
| 3472 | |
| 3473 | neigh_proc_update(ctl, write); |
| 3474 | return ret; |
Jiri Pirko | cb5b09c | 2013-12-07 19:26:54 +0100 | [diff] [blame] | 3475 | } |
| 3476 | EXPORT_SYMBOL(neigh_proc_dointvec); |
| 3477 | |
Christoph Hellwig | 3292739 | 2020-04-24 08:43:38 +0200 | [diff] [blame] | 3478 | int neigh_proc_dointvec_jiffies(struct ctl_table *ctl, int write, void *buffer, |
Jiri Pirko | cb5b09c | 2013-12-07 19:26:54 +0100 | [diff] [blame] | 3479 | size_t *lenp, loff_t *ppos) |
| 3480 | { |
Jiri Pirko | 1d4c8c2 | 2013-12-07 19:26:56 +0100 | [diff] [blame] | 3481 | int ret = proc_dointvec_jiffies(ctl, write, buffer, lenp, ppos); |
| 3482 | |
| 3483 | neigh_proc_update(ctl, write); |
| 3484 | return ret; |
Jiri Pirko | cb5b09c | 2013-12-07 19:26:54 +0100 | [diff] [blame] | 3485 | } |
| 3486 | EXPORT_SYMBOL(neigh_proc_dointvec_jiffies); |
| 3487 | |
| 3488 | static int neigh_proc_dointvec_userhz_jiffies(struct ctl_table *ctl, int write, |
Christoph Hellwig | 3292739 | 2020-04-24 08:43:38 +0200 | [diff] [blame] | 3489 | void *buffer, size_t *lenp, |
| 3490 | loff_t *ppos) |
Jiri Pirko | cb5b09c | 2013-12-07 19:26:54 +0100 | [diff] [blame] | 3491 | { |
Jiri Pirko | 1d4c8c2 | 2013-12-07 19:26:56 +0100 | [diff] [blame] | 3492 | int ret = proc_dointvec_userhz_jiffies(ctl, write, buffer, lenp, ppos); |
| 3493 | |
| 3494 | neigh_proc_update(ctl, write); |
| 3495 | return ret; |
Jiri Pirko | cb5b09c | 2013-12-07 19:26:54 +0100 | [diff] [blame] | 3496 | } |
| 3497 | |
| 3498 | int neigh_proc_dointvec_ms_jiffies(struct ctl_table *ctl, int write, |
Christoph Hellwig | 3292739 | 2020-04-24 08:43:38 +0200 | [diff] [blame] | 3499 | void *buffer, size_t *lenp, loff_t *ppos) |
Jiri Pirko | cb5b09c | 2013-12-07 19:26:54 +0100 | [diff] [blame] | 3500 | { |
Jiri Pirko | 1d4c8c2 | 2013-12-07 19:26:56 +0100 | [diff] [blame] | 3501 | int ret = proc_dointvec_ms_jiffies(ctl, write, buffer, lenp, ppos); |
| 3502 | |
| 3503 | neigh_proc_update(ctl, write); |
| 3504 | return ret; |
Jiri Pirko | cb5b09c | 2013-12-07 19:26:54 +0100 | [diff] [blame] | 3505 | } |
| 3506 | EXPORT_SYMBOL(neigh_proc_dointvec_ms_jiffies); |
| 3507 | |
| 3508 | static int neigh_proc_dointvec_unres_qlen(struct ctl_table *ctl, int write, |
Christoph Hellwig | 3292739 | 2020-04-24 08:43:38 +0200 | [diff] [blame] | 3509 | void *buffer, size_t *lenp, |
| 3510 | loff_t *ppos) |
Jiri Pirko | cb5b09c | 2013-12-07 19:26:54 +0100 | [diff] [blame] | 3511 | { |
Jiri Pirko | 1d4c8c2 | 2013-12-07 19:26:56 +0100 | [diff] [blame] | 3512 | int ret = proc_unres_qlen(ctl, write, buffer, lenp, ppos); |
| 3513 | |
| 3514 | neigh_proc_update(ctl, write); |
| 3515 | return ret; |
Jiri Pirko | cb5b09c | 2013-12-07 19:26:54 +0100 | [diff] [blame] | 3516 | } |
| 3517 | |
Jean-Francois Remy | 4bf6980 | 2015-01-14 04:22:39 +0100 | [diff] [blame] | 3518 | static int neigh_proc_base_reachable_time(struct ctl_table *ctl, int write, |
Christoph Hellwig | 3292739 | 2020-04-24 08:43:38 +0200 | [diff] [blame] | 3519 | void *buffer, size_t *lenp, |
| 3520 | loff_t *ppos) |
Jean-Francois Remy | 4bf6980 | 2015-01-14 04:22:39 +0100 | [diff] [blame] | 3521 | { |
| 3522 | struct neigh_parms *p = ctl->extra2; |
| 3523 | int ret; |
| 3524 | |
| 3525 | if (strcmp(ctl->procname, "base_reachable_time") == 0) |
| 3526 | ret = neigh_proc_dointvec_jiffies(ctl, write, buffer, lenp, ppos); |
| 3527 | else if (strcmp(ctl->procname, "base_reachable_time_ms") == 0) |
| 3528 | ret = neigh_proc_dointvec_ms_jiffies(ctl, write, buffer, lenp, ppos); |
| 3529 | else |
| 3530 | ret = -1; |
| 3531 | |
| 3532 | if (write && ret == 0) { |
| 3533 | /* update reachable_time as well, otherwise, the change will |
| 3534 | * only be effective after the next time neigh_periodic_work |
| 3535 | * decides to recompute it |
| 3536 | */ |
| 3537 | p->reachable_time = |
| 3538 | neigh_rand_reach_time(NEIGH_VAR(p, BASE_REACHABLE_TIME)); |
| 3539 | } |
| 3540 | return ret; |
| 3541 | } |
| 3542 | |
Jiri Pirko | 1f9248e | 2013-12-07 19:26:53 +0100 | [diff] [blame] | 3543 | #define NEIGH_PARMS_DATA_OFFSET(index) \ |
| 3544 | (&((struct neigh_parms *) 0)->data[index]) |
| 3545 | |
| 3546 | #define NEIGH_SYSCTL_ENTRY(attr, data_attr, name, mval, proc) \ |
| 3547 | [NEIGH_VAR_ ## attr] = { \ |
| 3548 | .procname = name, \ |
| 3549 | .data = NEIGH_PARMS_DATA_OFFSET(NEIGH_VAR_ ## data_attr), \ |
| 3550 | .maxlen = sizeof(int), \ |
| 3551 | .mode = mval, \ |
| 3552 | .proc_handler = proc, \ |
| 3553 | } |
| 3554 | |
| 3555 | #define NEIGH_SYSCTL_ZERO_INTMAX_ENTRY(attr, name) \ |
| 3556 | NEIGH_SYSCTL_ENTRY(attr, attr, name, 0644, neigh_proc_dointvec_zero_intmax) |
| 3557 | |
| 3558 | #define NEIGH_SYSCTL_JIFFIES_ENTRY(attr, name) \ |
Jiri Pirko | cb5b09c | 2013-12-07 19:26:54 +0100 | [diff] [blame] | 3559 | NEIGH_SYSCTL_ENTRY(attr, attr, name, 0644, neigh_proc_dointvec_jiffies) |
Jiri Pirko | 1f9248e | 2013-12-07 19:26:53 +0100 | [diff] [blame] | 3560 | |
| 3561 | #define NEIGH_SYSCTL_USERHZ_JIFFIES_ENTRY(attr, name) \ |
Jiri Pirko | cb5b09c | 2013-12-07 19:26:54 +0100 | [diff] [blame] | 3562 | NEIGH_SYSCTL_ENTRY(attr, attr, name, 0644, neigh_proc_dointvec_userhz_jiffies) |
Jiri Pirko | 1f9248e | 2013-12-07 19:26:53 +0100 | [diff] [blame] | 3563 | |
Jiri Pirko | 1f9248e | 2013-12-07 19:26:53 +0100 | [diff] [blame] | 3564 | #define NEIGH_SYSCTL_MS_JIFFIES_REUSED_ENTRY(attr, data_attr, name) \ |
Jiri Pirko | cb5b09c | 2013-12-07 19:26:54 +0100 | [diff] [blame] | 3565 | NEIGH_SYSCTL_ENTRY(attr, data_attr, name, 0644, neigh_proc_dointvec_ms_jiffies) |
Jiri Pirko | 1f9248e | 2013-12-07 19:26:53 +0100 | [diff] [blame] | 3566 | |
| 3567 | #define NEIGH_SYSCTL_UNRES_QLEN_REUSED_ENTRY(attr, data_attr, name) \ |
Jiri Pirko | cb5b09c | 2013-12-07 19:26:54 +0100 | [diff] [blame] | 3568 | NEIGH_SYSCTL_ENTRY(attr, data_attr, name, 0644, neigh_proc_dointvec_unres_qlen) |
Eric W. Biederman | 54716e3 | 2010-02-14 03:27:03 +0000 | [diff] [blame] | 3569 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3570 | static struct neigh_sysctl_table { |
| 3571 | struct ctl_table_header *sysctl_header; |
Eric Dumazet | 8b5c171 | 2011-11-09 12:07:14 +0000 | [diff] [blame] | 3572 | struct ctl_table neigh_vars[NEIGH_VAR_MAX + 1]; |
Brian Haley | ab32ea5 | 2006-09-22 14:15:41 -0700 | [diff] [blame] | 3573 | } neigh_sysctl_template __read_mostly = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3574 | .neigh_vars = { |
Jiri Pirko | 1f9248e | 2013-12-07 19:26:53 +0100 | [diff] [blame] | 3575 | NEIGH_SYSCTL_ZERO_INTMAX_ENTRY(MCAST_PROBES, "mcast_solicit"), |
| 3576 | NEIGH_SYSCTL_ZERO_INTMAX_ENTRY(UCAST_PROBES, "ucast_solicit"), |
| 3577 | NEIGH_SYSCTL_ZERO_INTMAX_ENTRY(APP_PROBES, "app_solicit"), |
YOSHIFUJI Hideaki/吉藤英明 | 8da8646 | 2015-03-19 22:41:46 +0900 | [diff] [blame] | 3578 | NEIGH_SYSCTL_ZERO_INTMAX_ENTRY(MCAST_REPROBES, "mcast_resolicit"), |
Jiri Pirko | 1f9248e | 2013-12-07 19:26:53 +0100 | [diff] [blame] | 3579 | NEIGH_SYSCTL_USERHZ_JIFFIES_ENTRY(RETRANS_TIME, "retrans_time"), |
| 3580 | NEIGH_SYSCTL_JIFFIES_ENTRY(BASE_REACHABLE_TIME, "base_reachable_time"), |
| 3581 | NEIGH_SYSCTL_JIFFIES_ENTRY(DELAY_PROBE_TIME, "delay_first_probe_time"), |
| 3582 | NEIGH_SYSCTL_JIFFIES_ENTRY(GC_STALETIME, "gc_stale_time"), |
| 3583 | NEIGH_SYSCTL_ZERO_INTMAX_ENTRY(QUEUE_LEN_BYTES, "unres_qlen_bytes"), |
| 3584 | NEIGH_SYSCTL_ZERO_INTMAX_ENTRY(PROXY_QLEN, "proxy_qlen"), |
| 3585 | NEIGH_SYSCTL_USERHZ_JIFFIES_ENTRY(ANYCAST_DELAY, "anycast_delay"), |
| 3586 | NEIGH_SYSCTL_USERHZ_JIFFIES_ENTRY(PROXY_DELAY, "proxy_delay"), |
| 3587 | NEIGH_SYSCTL_USERHZ_JIFFIES_ENTRY(LOCKTIME, "locktime"), |
| 3588 | NEIGH_SYSCTL_UNRES_QLEN_REUSED_ENTRY(QUEUE_LEN, QUEUE_LEN_BYTES, "unres_qlen"), |
| 3589 | NEIGH_SYSCTL_MS_JIFFIES_REUSED_ENTRY(RETRANS_TIME_MS, RETRANS_TIME, "retrans_time_ms"), |
| 3590 | NEIGH_SYSCTL_MS_JIFFIES_REUSED_ENTRY(BASE_REACHABLE_TIME_MS, BASE_REACHABLE_TIME, "base_reachable_time_ms"), |
Eric Dumazet | 8b5c171 | 2011-11-09 12:07:14 +0000 | [diff] [blame] | 3591 | [NEIGH_VAR_GC_INTERVAL] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3592 | .procname = "gc_interval", |
| 3593 | .maxlen = sizeof(int), |
| 3594 | .mode = 0644, |
Alexey Dobriyan | 6d9f239 | 2008-11-03 18:21:05 -0800 | [diff] [blame] | 3595 | .proc_handler = proc_dointvec_jiffies, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3596 | }, |
Eric Dumazet | 8b5c171 | 2011-11-09 12:07:14 +0000 | [diff] [blame] | 3597 | [NEIGH_VAR_GC_THRESH1] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3598 | .procname = "gc_thresh1", |
| 3599 | .maxlen = sizeof(int), |
| 3600 | .mode = 0644, |
Matteo Croce | eec4844 | 2019-07-18 15:58:50 -0700 | [diff] [blame] | 3601 | .extra1 = SYSCTL_ZERO, |
| 3602 | .extra2 = SYSCTL_INT_MAX, |
Francesco Fusco | 555445c | 2013-07-24 10:39:06 +0200 | [diff] [blame] | 3603 | .proc_handler = proc_dointvec_minmax, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3604 | }, |
Eric Dumazet | 8b5c171 | 2011-11-09 12:07:14 +0000 | [diff] [blame] | 3605 | [NEIGH_VAR_GC_THRESH2] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3606 | .procname = "gc_thresh2", |
| 3607 | .maxlen = sizeof(int), |
| 3608 | .mode = 0644, |
Matteo Croce | eec4844 | 2019-07-18 15:58:50 -0700 | [diff] [blame] | 3609 | .extra1 = SYSCTL_ZERO, |
| 3610 | .extra2 = SYSCTL_INT_MAX, |
Francesco Fusco | 555445c | 2013-07-24 10:39:06 +0200 | [diff] [blame] | 3611 | .proc_handler = proc_dointvec_minmax, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3612 | }, |
Eric Dumazet | 8b5c171 | 2011-11-09 12:07:14 +0000 | [diff] [blame] | 3613 | [NEIGH_VAR_GC_THRESH3] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3614 | .procname = "gc_thresh3", |
| 3615 | .maxlen = sizeof(int), |
| 3616 | .mode = 0644, |
Matteo Croce | eec4844 | 2019-07-18 15:58:50 -0700 | [diff] [blame] | 3617 | .extra1 = SYSCTL_ZERO, |
| 3618 | .extra2 = SYSCTL_INT_MAX, |
Francesco Fusco | 555445c | 2013-07-24 10:39:06 +0200 | [diff] [blame] | 3619 | .proc_handler = proc_dointvec_minmax, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3620 | }, |
Pavel Emelyanov | c3bac5a | 2007-12-02 00:08:16 +1100 | [diff] [blame] | 3621 | {}, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3622 | }, |
| 3623 | }; |
| 3624 | |
| 3625 | int neigh_sysctl_register(struct net_device *dev, struct neigh_parms *p, |
Jiri Pirko | 73af614 | 2013-12-07 19:26:55 +0100 | [diff] [blame] | 3626 | proc_handler *handler) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3627 | { |
Jiri Pirko | 1f9248e | 2013-12-07 19:26:53 +0100 | [diff] [blame] | 3628 | int i; |
Pavel Emelyanov | 3c607bb | 2007-12-02 00:06:34 +1100 | [diff] [blame] | 3629 | struct neigh_sysctl_table *t; |
Jiri Pirko | 1f9248e | 2013-12-07 19:26:53 +0100 | [diff] [blame] | 3630 | const char *dev_name_source; |
Eric W. Biederman | 8f40a1f | 2012-04-19 13:38:03 +0000 | [diff] [blame] | 3631 | char neigh_path[ sizeof("net//neigh/") + IFNAMSIZ + IFNAMSIZ ]; |
Jiri Pirko | 73af614 | 2013-12-07 19:26:55 +0100 | [diff] [blame] | 3632 | char *p_name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3633 | |
Pavel Emelyanov | 3c607bb | 2007-12-02 00:06:34 +1100 | [diff] [blame] | 3634 | t = kmemdup(&neigh_sysctl_template, sizeof(*t), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3635 | if (!t) |
Pavel Emelyanov | 3c607bb | 2007-12-02 00:06:34 +1100 | [diff] [blame] | 3636 | goto err; |
| 3637 | |
Jiri Pirko | b194c1f | 2014-02-21 14:52:57 +0100 | [diff] [blame] | 3638 | for (i = 0; i < NEIGH_VAR_GC_INTERVAL; i++) { |
Jiri Pirko | 1f9248e | 2013-12-07 19:26:53 +0100 | [diff] [blame] | 3639 | t->neigh_vars[i].data += (long) p; |
Jiri Pirko | cb5b09c | 2013-12-07 19:26:54 +0100 | [diff] [blame] | 3640 | t->neigh_vars[i].extra1 = dev; |
Jiri Pirko | 1d4c8c2 | 2013-12-07 19:26:56 +0100 | [diff] [blame] | 3641 | t->neigh_vars[i].extra2 = p; |
Jiri Pirko | cb5b09c | 2013-12-07 19:26:54 +0100 | [diff] [blame] | 3642 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3643 | |
| 3644 | if (dev) { |
| 3645 | dev_name_source = dev->name; |
Eric W. Biederman | d12af67 | 2007-10-18 03:05:25 -0700 | [diff] [blame] | 3646 | /* Terminate the table early */ |
Eric Dumazet | 8b5c171 | 2011-11-09 12:07:14 +0000 | [diff] [blame] | 3647 | memset(&t->neigh_vars[NEIGH_VAR_GC_INTERVAL], 0, |
| 3648 | sizeof(t->neigh_vars[NEIGH_VAR_GC_INTERVAL])); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3649 | } else { |
Mathias Krause | 9ecf07a | 2014-07-12 22:36:44 +0200 | [diff] [blame] | 3650 | struct neigh_table *tbl = p->tbl; |
Eric W. Biederman | 8f40a1f | 2012-04-19 13:38:03 +0000 | [diff] [blame] | 3651 | dev_name_source = "default"; |
Mathias Krause | 9ecf07a | 2014-07-12 22:36:44 +0200 | [diff] [blame] | 3652 | t->neigh_vars[NEIGH_VAR_GC_INTERVAL].data = &tbl->gc_interval; |
| 3653 | t->neigh_vars[NEIGH_VAR_GC_THRESH1].data = &tbl->gc_thresh1; |
| 3654 | t->neigh_vars[NEIGH_VAR_GC_THRESH2].data = &tbl->gc_thresh2; |
| 3655 | t->neigh_vars[NEIGH_VAR_GC_THRESH3].data = &tbl->gc_thresh3; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3656 | } |
| 3657 | |
Eric W. Biederman | f8572d8 | 2009-11-05 13:32:03 -0800 | [diff] [blame] | 3658 | if (handler) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3659 | /* RetransTime */ |
Eric Dumazet | 8b5c171 | 2011-11-09 12:07:14 +0000 | [diff] [blame] | 3660 | t->neigh_vars[NEIGH_VAR_RETRANS_TIME].proc_handler = handler; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3661 | /* ReachableTime */ |
Eric Dumazet | 8b5c171 | 2011-11-09 12:07:14 +0000 | [diff] [blame] | 3662 | t->neigh_vars[NEIGH_VAR_BASE_REACHABLE_TIME].proc_handler = handler; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3663 | /* RetransTime (in milliseconds)*/ |
Eric Dumazet | 8b5c171 | 2011-11-09 12:07:14 +0000 | [diff] [blame] | 3664 | t->neigh_vars[NEIGH_VAR_RETRANS_TIME_MS].proc_handler = handler; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3665 | /* ReachableTime (in milliseconds) */ |
Eric Dumazet | 8b5c171 | 2011-11-09 12:07:14 +0000 | [diff] [blame] | 3666 | t->neigh_vars[NEIGH_VAR_BASE_REACHABLE_TIME_MS].proc_handler = handler; |
Jean-Francois Remy | 4bf6980 | 2015-01-14 04:22:39 +0100 | [diff] [blame] | 3667 | } else { |
| 3668 | /* Those handlers will update p->reachable_time after |
| 3669 | * base_reachable_time(_ms) is set to ensure the new timer starts being |
| 3670 | * applied after the next neighbour update instead of waiting for |
| 3671 | * neigh_periodic_work to update its value (can be multiple minutes) |
| 3672 | * So any handler that replaces them should do this as well |
| 3673 | */ |
| 3674 | /* ReachableTime */ |
| 3675 | t->neigh_vars[NEIGH_VAR_BASE_REACHABLE_TIME].proc_handler = |
| 3676 | neigh_proc_base_reachable_time; |
| 3677 | /* ReachableTime (in milliseconds) */ |
| 3678 | t->neigh_vars[NEIGH_VAR_BASE_REACHABLE_TIME_MS].proc_handler = |
| 3679 | neigh_proc_base_reachable_time; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3680 | } |
| 3681 | |
Eric W. Biederman | 464dc80 | 2012-11-16 03:02:59 +0000 | [diff] [blame] | 3682 | /* Don't export sysctls to unprivileged users */ |
| 3683 | if (neigh_parms_net(p)->user_ns != &init_user_ns) |
| 3684 | t->neigh_vars[0].procname = NULL; |
| 3685 | |
Jiri Pirko | 73af614 | 2013-12-07 19:26:55 +0100 | [diff] [blame] | 3686 | switch (neigh_parms_family(p)) { |
| 3687 | case AF_INET: |
| 3688 | p_name = "ipv4"; |
| 3689 | break; |
| 3690 | case AF_INET6: |
| 3691 | p_name = "ipv6"; |
| 3692 | break; |
| 3693 | default: |
| 3694 | BUG(); |
| 3695 | } |
| 3696 | |
Eric W. Biederman | 8f40a1f | 2012-04-19 13:38:03 +0000 | [diff] [blame] | 3697 | snprintf(neigh_path, sizeof(neigh_path), "net/%s/neigh/%s", |
| 3698 | p_name, dev_name_source); |
Denis V. Lunev | 4ab438f | 2008-02-28 20:48:01 -0800 | [diff] [blame] | 3699 | t->sysctl_header = |
Eric W. Biederman | 8f40a1f | 2012-04-19 13:38:03 +0000 | [diff] [blame] | 3700 | register_net_sysctl(neigh_parms_net(p), neigh_path, t->neigh_vars); |
Pavel Emelyanov | 3c607bb | 2007-12-02 00:06:34 +1100 | [diff] [blame] | 3701 | if (!t->sysctl_header) |
Eric W. Biederman | 8f40a1f | 2012-04-19 13:38:03 +0000 | [diff] [blame] | 3702 | goto free; |
Pavel Emelyanov | 3c607bb | 2007-12-02 00:06:34 +1100 | [diff] [blame] | 3703 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3704 | p->sysctl_table = t; |
| 3705 | return 0; |
| 3706 | |
Pavel Emelyanov | 3c607bb | 2007-12-02 00:06:34 +1100 | [diff] [blame] | 3707 | free: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3708 | kfree(t); |
Pavel Emelyanov | 3c607bb | 2007-12-02 00:06:34 +1100 | [diff] [blame] | 3709 | err: |
| 3710 | return -ENOBUFS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3711 | } |
YOSHIFUJI Hideaki | 0a20450 | 2008-03-24 18:39:10 +0900 | [diff] [blame] | 3712 | EXPORT_SYMBOL(neigh_sysctl_register); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3713 | |
| 3714 | void neigh_sysctl_unregister(struct neigh_parms *p) |
| 3715 | { |
| 3716 | if (p->sysctl_table) { |
| 3717 | struct neigh_sysctl_table *t = p->sysctl_table; |
| 3718 | p->sysctl_table = NULL; |
Eric W. Biederman | 5dd3df1 | 2012-04-19 13:24:33 +0000 | [diff] [blame] | 3719 | unregister_net_sysctl_table(t->sysctl_header); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3720 | kfree(t); |
| 3721 | } |
| 3722 | } |
YOSHIFUJI Hideaki | 0a20450 | 2008-03-24 18:39:10 +0900 | [diff] [blame] | 3723 | EXPORT_SYMBOL(neigh_sysctl_unregister); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3724 | |
| 3725 | #endif /* CONFIG_SYSCTL */ |
| 3726 | |
Thomas Graf | c8822a4 | 2007-03-22 11:50:06 -0700 | [diff] [blame] | 3727 | static int __init neigh_init(void) |
| 3728 | { |
Florian Westphal | b97bac6 | 2017-08-09 20:41:48 +0200 | [diff] [blame] | 3729 | rtnl_register(PF_UNSPEC, RTM_NEWNEIGH, neigh_add, NULL, 0); |
| 3730 | rtnl_register(PF_UNSPEC, RTM_DELNEIGH, neigh_delete, NULL, 0); |
Roopa Prabhu | 82cbb5c | 2018-12-19 12:51:38 -0800 | [diff] [blame] | 3731 | rtnl_register(PF_UNSPEC, RTM_GETNEIGH, neigh_get, neigh_dump_info, 0); |
Thomas Graf | c8822a4 | 2007-03-22 11:50:06 -0700 | [diff] [blame] | 3732 | |
Greg Rose | c7ac867 | 2011-06-10 01:27:09 +0000 | [diff] [blame] | 3733 | rtnl_register(PF_UNSPEC, RTM_GETNEIGHTBL, NULL, neightbl_dump_info, |
Florian Westphal | b97bac6 | 2017-08-09 20:41:48 +0200 | [diff] [blame] | 3734 | 0); |
| 3735 | rtnl_register(PF_UNSPEC, RTM_SETNEIGHTBL, neightbl_set, NULL, 0); |
Thomas Graf | c8822a4 | 2007-03-22 11:50:06 -0700 | [diff] [blame] | 3736 | |
| 3737 | return 0; |
| 3738 | } |
| 3739 | |
| 3740 | subsys_initcall(neigh_init); |