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