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