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