blob: eed8b1d2d3021e310be1ff3811fb0f547b2d1eb7 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Common framework for low-level network console, dump, and debugger code
3 *
4 * Sep 8 2003 Matt Mackall <mpm@selenic.com>
5 *
6 * based on the netconsole code from:
7 *
8 * Copyright (C) 2001 Ingo Molnar <mingo@redhat.com>
9 * Copyright (C) 2002 Red Hat, Inc.
10 */
11
Joe Perchese6ec269352012-01-29 15:50:43 +000012#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13
Anton Vorontsovbff38772009-07-08 11:10:56 -070014#include <linux/moduleparam.h>
Andy Shevchenko4cd57732013-06-04 19:46:26 +030015#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/netdevice.h>
17#include <linux/etherdevice.h>
18#include <linux/string.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020019#include <linux/if_arp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/inetdevice.h>
21#include <linux/inet.h>
22#include <linux/interrupt.h>
23#include <linux/netpoll.h>
24#include <linux/sched.h>
25#include <linux/delay.h>
26#include <linux/rcupdate.h>
27#include <linux/workqueue.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090028#include <linux/slab.h>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040029#include <linux/export.h>
Amerigo Wang689971b2012-08-10 01:24:49 +000030#include <linux/if_vlan.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <net/tcp.h>
32#include <net/udp.h>
Cong Wangb3d936f2013-01-07 20:52:41 +000033#include <net/addrconf.h>
34#include <net/ndisc.h>
35#include <net/ip6_checksum.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <asm/unaligned.h>
David S. Miller9cbc1cb2009-06-15 03:02:23 -070037#include <trace/events/napi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
39/*
40 * We maintain a small pool of fully-sized skbs, to make sure the
41 * message gets out even in extreme OOM situations.
42 */
43
44#define MAX_UDP_CHUNK 1460
45#define MAX_SKBS 32
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -080047static struct sk_buff_head skb_pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Eric W. Biedermanad8d4752014-03-14 20:49:43 -070049#ifdef CONFIG_NETPOLL_TRAP
Linus Torvalds1da177e2005-04-16 15:20:36 -070050static atomic_t trapped;
Eric W. Biedermane1bd4d32014-03-14 20:50:58 -070051static void netpoll_neigh_reply(struct sk_buff *skb, struct netpoll_info *npinfo);
Eric W. Biedermanad8d4752014-03-14 20:49:43 -070052#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Lai Jiangshan7f9421c2013-03-15 06:50:52 +000054DEFINE_STATIC_SRCU(netpoll_srcu);
Neil Hormanca99ca12013-02-05 08:05:43 +000055
Stephen Hemminger2bdfe0b2006-10-26 15:46:54 -070056#define USEC_PER_POLL 50
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Joe Perches6f706242012-01-29 15:50:44 +000058#define MAX_SKB_SIZE \
59 (sizeof(struct ethhdr) + \
60 sizeof(struct iphdr) + \
61 sizeof(struct udphdr) + \
62 MAX_UDP_CHUNK)
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
David S. Miller3578b0c2010-08-03 00:24:04 -070064static void zap_completion_queue(void);
Neil Horman2cde6ac2013-02-11 10:25:30 +000065static void netpoll_async_cleanup(struct work_struct *work);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
Anton Vorontsovbff38772009-07-08 11:10:56 -070067static unsigned int carrier_timeout = 4;
68module_param(carrier_timeout, uint, 0644);
69
Joe Perchese6ec269352012-01-29 15:50:43 +000070#define np_info(np, fmt, ...) \
71 pr_info("%s: " fmt, np->name, ##__VA_ARGS__)
72#define np_err(np, fmt, ...) \
73 pr_err("%s: " fmt, np->name, ##__VA_ARGS__)
74#define np_notice(np, fmt, ...) \
75 pr_notice("%s: " fmt, np->name, ##__VA_ARGS__)
76
David Howellsc4028952006-11-22 14:57:56 +000077static void queue_process(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -070078{
David Howells4c1ac1b2006-12-05 14:37:56 +000079 struct netpoll_info *npinfo =
80 container_of(work, struct netpoll_info, tx_work.work);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 struct sk_buff *skb;
Ingo Molnar36405432006-12-12 17:20:42 +010082 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
Stephen Hemminger6c43ff12006-10-26 15:46:53 -070084 while ((skb = skb_dequeue(&npinfo->txq))) {
85 struct net_device *dev = skb->dev;
Stephen Hemminger00829822008-11-20 20:14:53 -080086 const struct net_device_ops *ops = dev->netdev_ops;
David S. Millerfd2ea0a2008-07-17 01:56:23 -070087 struct netdev_queue *txq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
Stephen Hemminger6c43ff12006-10-26 15:46:53 -070089 if (!netif_device_present(dev) || !netif_running(dev)) {
90 __kfree_skb(skb);
91 continue;
92 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
David S. Millerfd2ea0a2008-07-17 01:56:23 -070094 txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb));
95
Ingo Molnar36405432006-12-12 17:20:42 +010096 local_irq_save(flags);
David S. Millerfd2ea0a2008-07-17 01:56:23 -070097 __netif_tx_lock(txq, smp_processor_id());
Tom Herbert734664982011-11-28 16:32:44 +000098 if (netif_xmit_frozen_or_stopped(txq) ||
Stephen Hemminger00829822008-11-20 20:14:53 -080099 ops->ndo_start_xmit(skb, dev) != NETDEV_TX_OK) {
Stephen Hemminger6c43ff12006-10-26 15:46:53 -0700100 skb_queue_head(&npinfo->txq, skb);
David S. Millerfd2ea0a2008-07-17 01:56:23 -0700101 __netif_tx_unlock(txq);
Ingo Molnar36405432006-12-12 17:20:42 +0100102 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
Jarek Poplawski25442ca2007-07-05 17:42:44 -0700104 schedule_delayed_work(&npinfo->tx_work, HZ/10);
Stephen Hemminger6c43ff12006-10-26 15:46:53 -0700105 return;
106 }
David S. Millerfd2ea0a2008-07-17 01:56:23 -0700107 __netif_tx_unlock(txq);
Ingo Molnar36405432006-12-12 17:20:42 +0100108 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 }
110}
111
Eric W. Biedermane1bd4d32014-03-14 20:50:58 -0700112#ifdef CONFIG_NETPOLL_TRAP
Al Virob51655b2006-11-14 21:40:42 -0800113static __sum16 checksum_udp(struct sk_buff *skb, struct udphdr *uh,
114 unsigned short ulen, __be32 saddr, __be32 daddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115{
Al Virod6f5493c2006-11-14 21:26:08 -0800116 __wsum psum;
Herbert Xufb286bb2005-11-10 13:01:24 -0800117
Herbert Xu60476372007-04-09 11:59:39 -0700118 if (uh->check == 0 || skb_csum_unnecessary(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 return 0;
120
Herbert Xufb286bb2005-11-10 13:01:24 -0800121 psum = csum_tcpudp_nofold(saddr, daddr, ulen, IPPROTO_UDP, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
Patrick McHardy84fa7932006-08-29 16:44:56 -0700123 if (skb->ip_summed == CHECKSUM_COMPLETE &&
Al Virod3bc23e2006-11-14 21:24:49 -0800124 !csum_fold(csum_add(psum, skb->csum)))
Herbert Xufb286bb2005-11-10 13:01:24 -0800125 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
Herbert Xufb286bb2005-11-10 13:01:24 -0800127 skb->csum = psum;
128
129 return __skb_checksum_complete(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130}
Eric W. Biedermane1bd4d32014-03-14 20:50:58 -0700131#endif /* CONFIG_NETPOLL_TRAP */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
133/*
134 * Check whether delayed processing was scheduled for our NIC. If so,
135 * we attempt to grab the poll lock and use ->poll() to pump the card.
136 * If this fails, either we've recursed in ->poll() or it's already
137 * running on another CPU.
138 *
139 * Note: we don't mask interrupts with this lock because we're using
140 * trylock here and interrupts are already disabled in the softirq
141 * case. Further, we test the poll_owner to avoid recursion on UP
142 * systems where the lock doesn't exist.
143 *
144 * In cases where there is bi-directional communications, reading only
145 * one message at a time can lead to packets being dropped by the
146 * network adapter, forcing superfluous retries and possibly timeouts.
147 * Thus, we set our budget to greater than 1.
148 */
Eric W. Biedermanb249b512014-03-14 20:44:37 -0700149static int poll_one_napi(struct napi_struct *napi, int budget)
David S. Miller0a7606c2007-10-29 21:28:47 -0700150{
151 int work;
152
153 /* net_rx_action's ->poll() invocations and our's are
154 * synchronized by this test which is only made while
155 * holding the napi->poll_lock.
156 */
157 if (!test_bit(NAPI_STATE_SCHED, &napi->state))
158 return budget;
159
Neil Horman7b363e42008-12-09 23:22:26 -0800160 set_bit(NAPI_STATE_NPSVC, &napi->state);
David S. Miller0a7606c2007-10-29 21:28:47 -0700161
162 work = napi->poll(napi, budget);
Eric W. Biedermane97dc3f2014-03-14 20:47:15 -0700163 WARN_ONCE(work > budget, "%pF exceeded budget in poll\n", napi->poll);
David S. Miller7d18f112009-05-21 23:30:09 -0700164 trace_napi_poll(napi);
David S. Miller0a7606c2007-10-29 21:28:47 -0700165
Neil Horman7b363e42008-12-09 23:22:26 -0800166 clear_bit(NAPI_STATE_NPSVC, &napi->state);
David S. Miller0a7606c2007-10-29 21:28:47 -0700167
168 return budget - work;
169}
170
Eric W. Biederman9852fbe2014-03-14 20:45:17 -0700171static void poll_napi(struct net_device *dev, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172{
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700173 struct napi_struct *napi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
Neil Hormanf13d4932010-10-19 07:04:26 +0000175 list_for_each_entry(napi, &dev->napi_list, dev_list) {
David S. Miller0a7606c2007-10-29 21:28:47 -0700176 if (napi->poll_owner != smp_processor_id() &&
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700177 spin_trylock(&napi->poll_lock)) {
Eric W. Biedermanb249b512014-03-14 20:44:37 -0700178 budget = poll_one_napi(napi, budget);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700179 spin_unlock(&napi->poll_lock);
180 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 }
182}
183
Eric W. Biedermane1bd4d32014-03-14 20:50:58 -0700184#ifdef CONFIG_NETPOLL_TRAP
Eric W. Biederman18b37532014-03-14 20:50:25 -0700185static void service_neigh_queue(struct net_device *dev,
186 struct netpoll_info *npi)
Neil Horman068c6e92006-06-26 00:04:27 -0700187{
Eric W. Biederman18b37532014-03-14 20:50:25 -0700188 struct sk_buff *skb;
189 if (dev->flags & IFF_SLAVE) {
190 struct net_device *bond_dev;
191 struct netpoll_info *bond_ni;
Neil Horman068c6e92006-06-26 00:04:27 -0700192
Eric W. Biederman18b37532014-03-14 20:50:25 -0700193 bond_dev = netdev_master_upper_dev_get_rcu(dev);
194 bond_ni = rcu_dereference_bh(bond_dev->npinfo);
195 while ((skb = skb_dequeue(&npi->neigh_tx))) {
196 skb->dev = bond_dev;
197 skb_queue_tail(&bond_ni->neigh_tx, skb);
198 }
Neil Horman068c6e92006-06-26 00:04:27 -0700199 }
Eric W. Biederman18b37532014-03-14 20:50:25 -0700200 while ((skb = skb_dequeue(&npi->neigh_tx)))
201 netpoll_neigh_reply(skb, npi);
Neil Horman068c6e92006-06-26 00:04:27 -0700202}
Eric W. Biedermane1bd4d32014-03-14 20:50:58 -0700203#else /* !CONFIG_NETPOLL_TRAP */
204static inline void service_neigh_queue(struct net_device *dev,
205 struct netpoll_info *npi)
206{
207}
208#endif /* CONFIG_NETPOLL_TRAP */
Neil Horman068c6e92006-06-26 00:04:27 -0700209
Joe Perches234b9212011-06-30 15:08:57 +0000210static void netpoll_poll_dev(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211{
Pavel Emelyanov5e392732009-05-11 00:36:35 +0000212 const struct net_device_ops *ops;
Amerigo Wang28996562012-08-10 01:24:42 +0000213 struct netpoll_info *ni = rcu_dereference_bh(dev->npinfo);
Eric W. Biedermanb6bacd52014-03-14 20:48:28 -0700214 bool rx_processing = netpoll_rx_processing(ni);
215 int budget = rx_processing? 16 : 0;
Stephen Hemminger51069302007-11-19 19:18:11 -0800216
Neil Hormanca99ca12013-02-05 08:05:43 +0000217 /* Don't do any rx activity if the dev_lock mutex is held
218 * the dev_open/close paths use this to block netpoll activity
219 * while changing device state
220 */
Dan Carpentera3dbbc22013-05-06 02:15:13 +0000221 if (down_trylock(&ni->dev_lock))
Neil Hormanca99ca12013-02-05 08:05:43 +0000222 return;
223
Neil Horman959d5fd2013-02-13 11:32:42 -0500224 if (!netif_running(dev)) {
Neil Hormanbd7c4b62013-04-30 05:35:05 +0000225 up(&ni->dev_lock);
Pavel Emelyanov5e392732009-05-11 00:36:35 +0000226 return;
Neil Horman959d5fd2013-02-13 11:32:42 -0500227 }
Pavel Emelyanov5e392732009-05-11 00:36:35 +0000228
Eric W. Biedermanb6bacd52014-03-14 20:48:28 -0700229 if (rx_processing)
Eric W. Biedermanad8d4752014-03-14 20:49:43 -0700230 netpoll_set_trap(1);
Eric W. Biedermanb249b512014-03-14 20:44:37 -0700231
Pavel Emelyanov5e392732009-05-11 00:36:35 +0000232 ops = dev->netdev_ops;
Neil Horman959d5fd2013-02-13 11:32:42 -0500233 if (!ops->ndo_poll_controller) {
Neil Hormanbd7c4b62013-04-30 05:35:05 +0000234 up(&ni->dev_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 return;
Neil Horman959d5fd2013-02-13 11:32:42 -0500236 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
238 /* Process pending work on NIC */
Stephen Hemmingerd3147742008-11-19 21:32:24 -0800239 ops->ndo_poll_controller(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
Eric W. Biederman9852fbe2014-03-14 20:45:17 -0700241 poll_napi(dev, budget);
Stephen Hemminger51069302007-11-19 19:18:11 -0800242
Eric W. Biedermanb6bacd52014-03-14 20:48:28 -0700243 if (rx_processing)
Eric W. Biedermanad8d4752014-03-14 20:49:43 -0700244 netpoll_set_trap(0);
Eric W. Biedermanb249b512014-03-14 20:44:37 -0700245
Neil Hormanbd7c4b62013-04-30 05:35:05 +0000246 up(&ni->dev_lock);
Neil Hormanca99ca12013-02-05 08:05:43 +0000247
Eric W. Biederman18b37532014-03-14 20:50:25 -0700248 service_neigh_queue(dev, ni);
Neil Horman068c6e92006-06-26 00:04:27 -0700249
David S. Miller3578b0c2010-08-03 00:24:04 -0700250 zap_completion_queue();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251}
252
dingtianhongda6e3782013-05-27 19:53:31 +0000253void netpoll_rx_disable(struct net_device *dev)
Neil Hormanca99ca12013-02-05 08:05:43 +0000254{
255 struct netpoll_info *ni;
256 int idx;
257 might_sleep();
258 idx = srcu_read_lock(&netpoll_srcu);
259 ni = srcu_dereference(dev->npinfo, &netpoll_srcu);
260 if (ni)
Neil Hormanbd7c4b62013-04-30 05:35:05 +0000261 down(&ni->dev_lock);
Neil Hormanca99ca12013-02-05 08:05:43 +0000262 srcu_read_unlock(&netpoll_srcu, idx);
Neil Hormanca99ca12013-02-05 08:05:43 +0000263}
264EXPORT_SYMBOL(netpoll_rx_disable);
265
266void netpoll_rx_enable(struct net_device *dev)
267{
268 struct netpoll_info *ni;
269 rcu_read_lock();
270 ni = rcu_dereference(dev->npinfo);
271 if (ni)
Neil Hormanbd7c4b62013-04-30 05:35:05 +0000272 up(&ni->dev_lock);
Neil Hormanca99ca12013-02-05 08:05:43 +0000273 rcu_read_unlock();
274}
275EXPORT_SYMBOL(netpoll_rx_enable);
276
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277static void refill_skbs(void)
278{
279 struct sk_buff *skb;
280 unsigned long flags;
281
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -0800282 spin_lock_irqsave(&skb_pool.lock, flags);
283 while (skb_pool.qlen < MAX_SKBS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 skb = alloc_skb(MAX_SKB_SIZE, GFP_ATOMIC);
285 if (!skb)
286 break;
287
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -0800288 __skb_queue_tail(&skb_pool, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 }
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -0800290 spin_unlock_irqrestore(&skb_pool.lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291}
292
David S. Miller3578b0c2010-08-03 00:24:04 -0700293static void zap_completion_queue(void)
294{
295 unsigned long flags;
296 struct softnet_data *sd = &get_cpu_var(softnet_data);
297
298 if (sd->completion_queue) {
299 struct sk_buff *clist;
300
301 local_irq_save(flags);
302 clist = sd->completion_queue;
303 sd->completion_queue = NULL;
304 local_irq_restore(flags);
305
306 while (clist != NULL) {
307 struct sk_buff *skb = clist;
308 clist = clist->next;
309 if (skb->destructor) {
310 atomic_inc(&skb->users);
311 dev_kfree_skb_any(skb); /* put this one back */
312 } else {
313 __kfree_skb(skb);
314 }
315 }
316 }
317
318 put_cpu_var(softnet_data);
319}
320
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -0800321static struct sk_buff *find_skb(struct netpoll *np, int len, int reserve)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322{
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -0800323 int count = 0;
324 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
David S. Miller3578b0c2010-08-03 00:24:04 -0700326 zap_completion_queue();
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -0800327 refill_skbs();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328repeat:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
330 skb = alloc_skb(len, GFP_ATOMIC);
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -0800331 if (!skb)
332 skb = skb_dequeue(&skb_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
334 if (!skb) {
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -0800335 if (++count < 10) {
Joe Perches2a49e002011-06-30 15:08:58 +0000336 netpoll_poll_dev(np->dev);
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -0800337 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 }
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -0800339 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 }
341
342 atomic_set(&skb->users, 1);
343 skb_reserve(skb, reserve);
344 return skb;
345}
346
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700347static int netpoll_owner_active(struct net_device *dev)
348{
349 struct napi_struct *napi;
350
351 list_for_each_entry(napi, &dev->napi_list, dev_list) {
352 if (napi->poll_owner == smp_processor_id())
353 return 1;
354 }
355 return 0;
356}
357
Amerigo Wang28996562012-08-10 01:24:42 +0000358/* call with IRQ disabled */
Neil Hormanc2355e12010-10-13 16:01:49 +0000359void netpoll_send_skb_on_dev(struct netpoll *np, struct sk_buff *skb,
360 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361{
Stephen Hemminger2bdfe0b2006-10-26 15:46:54 -0700362 int status = NETDEV_TX_BUSY;
363 unsigned long tries;
Stephen Hemminger00829822008-11-20 20:14:53 -0800364 const struct net_device_ops *ops = dev->netdev_ops;
Herbert Xude85d992010-06-10 16:12:44 +0000365 /* It is up to the caller to keep npinfo alive. */
Amerigo Wang28996562012-08-10 01:24:42 +0000366 struct netpoll_info *npinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
Amerigo Wang28996562012-08-10 01:24:42 +0000368 WARN_ON_ONCE(!irqs_disabled());
369
370 npinfo = rcu_dereference_bh(np->dev->npinfo);
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900371 if (!npinfo || !netif_running(dev) || !netif_device_present(dev)) {
372 __kfree_skb(skb);
373 return;
374 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
Stephen Hemminger2bdfe0b2006-10-26 15:46:54 -0700376 /* don't get messages out of order, and no recursion */
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700377 if (skb_queue_len(&npinfo->txq) == 0 && !netpoll_owner_active(dev)) {
David S. Millerfd2ea0a2008-07-17 01:56:23 -0700378 struct netdev_queue *txq;
Andrew Mortona49f99f2006-12-11 17:24:46 -0800379
Jason Wangf663dd92014-01-10 16:18:26 +0800380 txq = netdev_pick_tx(dev, skb, NULL);
David S. Millerfd2ea0a2008-07-17 01:56:23 -0700381
Stephen Hemminger0db3dc72007-06-27 00:39:42 -0700382 /* try until next clock tick */
383 for (tries = jiffies_to_usecs(1)/USEC_PER_POLL;
384 tries > 0; --tries) {
David S. Millerfd2ea0a2008-07-17 01:56:23 -0700385 if (__netif_tx_trylock(txq)) {
Tom Herbert734664982011-11-28 16:32:44 +0000386 if (!netif_xmit_stopped(txq)) {
Amerigo Wang689971b2012-08-10 01:24:49 +0000387 if (vlan_tx_tag_present(skb) &&
Patrick McHardy86a9bad2013-04-19 02:04:30 +0000388 !vlan_hw_offload_capable(netif_skb_features(skb),
389 skb->vlan_proto)) {
390 skb = __vlan_put_tag(skb, skb->vlan_proto, vlan_tx_tag_get(skb));
David S. Milleraca5f582014-01-02 19:50:52 -0500391 if (unlikely(!skb)) {
392 /* This is actually a packet drop, but we
393 * don't want the code at the end of this
394 * function to try and re-queue a NULL skb.
395 */
396 status = NETDEV_TX_OK;
397 goto unlock_txq;
398 }
Amerigo Wang689971b2012-08-10 01:24:49 +0000399 skb->vlan_tci = 0;
400 }
401
Stephen Hemminger00829822008-11-20 20:14:53 -0800402 status = ops->ndo_start_xmit(skb, dev);
Eric Dumazet08baf562009-05-25 22:58:01 -0700403 if (status == NETDEV_TX_OK)
404 txq_trans_update(txq);
405 }
David S. Milleraca5f582014-01-02 19:50:52 -0500406 unlock_txq:
David S. Millerfd2ea0a2008-07-17 01:56:23 -0700407 __netif_tx_unlock(txq);
Matt Mackallf0d34592005-08-11 19:25:11 -0700408
Andrew Mortone37b8d92006-12-09 14:01:49 -0800409 if (status == NETDEV_TX_OK)
410 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
Andrew Mortone37b8d92006-12-09 14:01:49 -0800412 }
Stephen Hemminger0db3dc72007-06-27 00:39:42 -0700413
414 /* tickle device maybe there is some cleanup */
Joe Perches2a49e002011-06-30 15:08:58 +0000415 netpoll_poll_dev(np->dev);
Stephen Hemminger0db3dc72007-06-27 00:39:42 -0700416
417 udelay(USEC_PER_POLL);
Matt Mackall0db1d6f2005-08-11 19:25:54 -0700418 }
Dongdong Deng79b1bee2009-08-21 03:33:36 +0000419
420 WARN_ONCE(!irqs_disabled(),
Amerigo Wang28996562012-08-10 01:24:42 +0000421 "netpoll_send_skb_on_dev(): %s enabled interrupts in poll (%pF)\n",
Dongdong Deng79b1bee2009-08-21 03:33:36 +0000422 dev->name, ops->ndo_start_xmit);
423
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
Stephen Hemminger2bdfe0b2006-10-26 15:46:54 -0700426 if (status != NETDEV_TX_OK) {
Stephen Hemminger5de4a472006-10-26 15:46:55 -0700427 skb_queue_tail(&npinfo->txq, skb);
David Howells4c1ac1b2006-12-05 14:37:56 +0000428 schedule_delayed_work(&npinfo->tx_work,0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430}
Neil Hormanc2355e12010-10-13 16:01:49 +0000431EXPORT_SYMBOL(netpoll_send_skb_on_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
433void netpoll_send_udp(struct netpoll *np, const char *msg, int len)
434{
Eric Dumazet954fba02012-06-12 19:30:21 +0000435 int total_len, ip_len, udp_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 struct sk_buff *skb;
437 struct udphdr *udph;
438 struct iphdr *iph;
439 struct ethhdr *eth;
Eric Dumazetee130402012-08-24 01:47:26 +0000440 static atomic_t ip_ident;
Cong Wangb3d936f2013-01-07 20:52:41 +0000441 struct ipv6hdr *ip6h;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
443 udp_len = len + sizeof(*udph);
Cong Wangb3d936f2013-01-07 20:52:41 +0000444 if (np->ipv6)
445 ip_len = udp_len + sizeof(*ip6h);
446 else
Cong Wangb7394d22013-01-07 20:52:39 +0000447 ip_len = udp_len + sizeof(*iph);
448
Eric Dumazet954fba02012-06-12 19:30:21 +0000449 total_len = ip_len + LL_RESERVED_SPACE(np->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
Eric Dumazet954fba02012-06-12 19:30:21 +0000451 skb = find_skb(np, total_len + np->dev->needed_tailroom,
452 total_len - len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 if (!skb)
454 return;
455
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -0300456 skb_copy_to_linear_data(skb, msg, len);
Eric Dumazet954fba02012-06-12 19:30:21 +0000457 skb_put(skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458
Arnaldo Carvalho de Melo4bedb452007-03-13 14:28:48 -0300459 skb_push(skb, sizeof(*udph));
460 skb_reset_transport_header(skb);
461 udph = udp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 udph->source = htons(np->local_port);
463 udph->dest = htons(np->remote_port);
464 udph->len = htons(udp_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
Cong Wangb3d936f2013-01-07 20:52:41 +0000466 if (np->ipv6) {
467 udph->check = 0;
468 udph->check = csum_ipv6_magic(&np->local_ip.in6,
469 &np->remote_ip.in6,
470 udp_len, IPPROTO_UDP,
471 csum_partial(udph, udp_len, 0));
472 if (udph->check == 0)
473 udph->check = CSUM_MANGLED_0;
474
475 skb_push(skb, sizeof(*ip6h));
476 skb_reset_network_header(skb);
477 ip6h = ipv6_hdr(skb);
478
479 /* ip6h->version = 6; ip6h->priority = 0; */
480 put_unaligned(0x60, (unsigned char *)ip6h);
481 ip6h->flow_lbl[0] = 0;
482 ip6h->flow_lbl[1] = 0;
483 ip6h->flow_lbl[2] = 0;
484
485 ip6h->payload_len = htons(sizeof(struct udphdr) + len);
486 ip6h->nexthdr = IPPROTO_UDP;
487 ip6h->hop_limit = 32;
488 ip6h->saddr = np->local_ip.in6;
489 ip6h->daddr = np->remote_ip.in6;
490
491 eth = (struct ethhdr *) skb_push(skb, ETH_HLEN);
492 skb_reset_mac_header(skb);
493 skb->protocol = eth->h_proto = htons(ETH_P_IPV6);
494 } else {
Cong Wangb7394d22013-01-07 20:52:39 +0000495 udph->check = 0;
496 udph->check = csum_tcpudp_magic(np->local_ip.ip,
497 np->remote_ip.ip,
498 udp_len, IPPROTO_UDP,
499 csum_partial(udph, udp_len, 0));
500 if (udph->check == 0)
501 udph->check = CSUM_MANGLED_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502
Cong Wangb7394d22013-01-07 20:52:39 +0000503 skb_push(skb, sizeof(*iph));
504 skb_reset_network_header(skb);
505 iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506
Cong Wangb7394d22013-01-07 20:52:39 +0000507 /* iph->version = 4; iph->ihl = 5; */
508 put_unaligned(0x45, (unsigned char *)iph);
509 iph->tos = 0;
510 put_unaligned(htons(ip_len), &(iph->tot_len));
511 iph->id = htons(atomic_inc_return(&ip_ident));
512 iph->frag_off = 0;
513 iph->ttl = 64;
514 iph->protocol = IPPROTO_UDP;
515 iph->check = 0;
516 put_unaligned(np->local_ip.ip, &(iph->saddr));
517 put_unaligned(np->remote_ip.ip, &(iph->daddr));
518 iph->check = ip_fast_csum((unsigned char *)iph, iph->ihl);
519
520 eth = (struct ethhdr *) skb_push(skb, ETH_HLEN);
521 skb_reset_mac_header(skb);
522 skb->protocol = eth->h_proto = htons(ETH_P_IP);
523 }
524
Joe Perchesc62326a2014-01-20 09:52:18 -0800525 ether_addr_copy(eth->h_source, np->dev->dev_addr);
526 ether_addr_copy(eth->h_dest, np->remote_mac);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
528 skb->dev = np->dev;
529
530 netpoll_send_skb(np, skb);
531}
Eric Dumazet9e34a5b2010-07-09 21:22:04 +0000532EXPORT_SYMBOL(netpoll_send_udp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
Eric W. Biedermane1bd4d32014-03-14 20:50:58 -0700534#ifdef CONFIG_NETPOLL_TRAP
Cong Wangb7394d22013-01-07 20:52:39 +0000535static void netpoll_neigh_reply(struct sk_buff *skb, struct netpoll_info *npinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536{
Cong Wangb3d936f2013-01-07 20:52:41 +0000537 int size, type = ARPOP_REPLY;
Al Viro252e33462006-11-14 20:48:11 -0800538 __be32 sip, tip;
Neil Horman47bbec02006-12-08 00:05:55 -0800539 unsigned char *sha;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 struct sk_buff *send_skb;
Daniel Borkmann508e14b2010-01-12 14:27:30 +0000541 struct netpoll *np, *tmp;
542 unsigned long flags;
Herbert Xuae641942011-11-18 02:20:04 +0000543 int hlen, tlen;
Cong Wangb7394d22013-01-07 20:52:39 +0000544 int hits = 0, proto;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
Eric W. Biedermanff607632014-03-14 20:47:49 -0700546 if (!netpoll_rx_processing(npinfo))
Daniel Borkmann508e14b2010-01-12 14:27:30 +0000547 return;
548
549 /* Before checking the packet, we do some early
550 inspection whether this is interesting at all */
551 spin_lock_irqsave(&npinfo->rx_lock, flags);
552 list_for_each_entry_safe(np, tmp, &npinfo->rx_np, rx) {
553 if (np->dev == skb->dev)
554 hits++;
555 }
556 spin_unlock_irqrestore(&npinfo->rx_lock, flags);
557
558 /* No netpoll struct is using this dev */
559 if (!hits)
Jeff Moyer115c1d62005-06-22 22:05:31 -0700560 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
Cong Wangb7394d22013-01-07 20:52:39 +0000562 proto = ntohs(eth_hdr(skb)->h_proto);
Sonic Zhangb0dd6632013-09-11 11:31:53 +0800563 if (proto == ETH_P_ARP) {
Cong Wangb3d936f2013-01-07 20:52:41 +0000564 struct arphdr *arp;
565 unsigned char *arp_ptr;
Cong Wangb7394d22013-01-07 20:52:39 +0000566 /* No arp on this interface */
567 if (skb->dev->flags & IFF_NOARP)
568 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
Cong Wangb7394d22013-01-07 20:52:39 +0000570 if (!pskb_may_pull(skb, arp_hdr_len(skb->dev)))
571 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
Cong Wangb7394d22013-01-07 20:52:39 +0000573 skb_reset_network_header(skb);
574 skb_reset_transport_header(skb);
575 arp = arp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
Cong Wangb7394d22013-01-07 20:52:39 +0000577 if ((arp->ar_hrd != htons(ARPHRD_ETHER) &&
578 arp->ar_hrd != htons(ARPHRD_IEEE802)) ||
579 arp->ar_pro != htons(ETH_P_IP) ||
580 arp->ar_op != htons(ARPOP_REQUEST))
581 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
Cong Wangb7394d22013-01-07 20:52:39 +0000583 arp_ptr = (unsigned char *)(arp+1);
584 /* save the location of the src hw addr */
585 sha = arp_ptr;
586 arp_ptr += skb->dev->addr_len;
587 memcpy(&sip, arp_ptr, 4);
Daniel Borkmann508e14b2010-01-12 14:27:30 +0000588 arp_ptr += 4;
Cong Wangb7394d22013-01-07 20:52:39 +0000589 /* If we actually cared about dst hw addr,
590 it would get copied here */
591 arp_ptr += skb->dev->addr_len;
592 memcpy(&tip, arp_ptr, 4);
Daniel Borkmann508e14b2010-01-12 14:27:30 +0000593
Cong Wangb7394d22013-01-07 20:52:39 +0000594 /* Should we ignore arp? */
595 if (ipv4_is_loopback(tip) || ipv4_is_multicast(tip))
596 return;
Daniel Borkmann508e14b2010-01-12 14:27:30 +0000597
Cong Wangb7394d22013-01-07 20:52:39 +0000598 size = arp_hdr_len(skb->dev);
599
600 spin_lock_irqsave(&npinfo->rx_lock, flags);
601 list_for_each_entry_safe(np, tmp, &npinfo->rx_np, rx) {
602 if (tip != np->local_ip.ip)
603 continue;
604
605 hlen = LL_RESERVED_SPACE(np->dev);
606 tlen = np->dev->needed_tailroom;
607 send_skb = find_skb(np, size + hlen + tlen, hlen);
608 if (!send_skb)
609 continue;
610
611 skb_reset_network_header(send_skb);
612 arp = (struct arphdr *) skb_put(send_skb, size);
613 send_skb->dev = skb->dev;
614 send_skb->protocol = htons(ETH_P_ARP);
615
616 /* Fill the device header for the ARP frame */
Cong Wangb3d936f2013-01-07 20:52:41 +0000617 if (dev_hard_header(send_skb, skb->dev, ETH_P_ARP,
Cong Wangb7394d22013-01-07 20:52:39 +0000618 sha, np->dev->dev_addr,
619 send_skb->len) < 0) {
620 kfree_skb(send_skb);
621 continue;
622 }
623
624 /*
625 * Fill out the arp protocol part.
626 *
627 * we only support ethernet device type,
628 * which (according to RFC 1390) should
629 * always equal 1 (Ethernet).
630 */
631
632 arp->ar_hrd = htons(np->dev->type);
633 arp->ar_pro = htons(ETH_P_IP);
634 arp->ar_hln = np->dev->addr_len;
635 arp->ar_pln = 4;
636 arp->ar_op = htons(type);
637
638 arp_ptr = (unsigned char *)(arp + 1);
639 memcpy(arp_ptr, np->dev->dev_addr, np->dev->addr_len);
640 arp_ptr += np->dev->addr_len;
641 memcpy(arp_ptr, &tip, 4);
642 arp_ptr += 4;
643 memcpy(arp_ptr, sha, np->dev->addr_len);
644 arp_ptr += np->dev->addr_len;
645 memcpy(arp_ptr, &sip, 4);
646
647 netpoll_send_skb(np, send_skb);
648
Antonio Quartulli8fb479a2013-10-23 23:36:30 +0200649 /* If there are several rx_skb_hooks for the same
650 * address we're fine by sending a single reply
651 */
Cong Wangb7394d22013-01-07 20:52:39 +0000652 break;
653 }
654 spin_unlock_irqrestore(&npinfo->rx_lock, flags);
Cong Wangb3d936f2013-01-07 20:52:41 +0000655 } else if( proto == ETH_P_IPV6) {
656#if IS_ENABLED(CONFIG_IPV6)
657 struct nd_msg *msg;
658 u8 *lladdr = NULL;
659 struct ipv6hdr *hdr;
660 struct icmp6hdr *icmp6h;
661 const struct in6_addr *saddr;
662 const struct in6_addr *daddr;
663 struct inet6_dev *in6_dev = NULL;
664 struct in6_addr *target;
665
666 in6_dev = in6_dev_get(skb->dev);
667 if (!in6_dev || !in6_dev->cnf.accept_ra)
668 return;
669
670 if (!pskb_may_pull(skb, skb->len))
671 return;
672
673 msg = (struct nd_msg *)skb_transport_header(skb);
674
675 __skb_push(skb, skb->data - skb_transport_header(skb));
676
677 if (ipv6_hdr(skb)->hop_limit != 255)
678 return;
679 if (msg->icmph.icmp6_code != 0)
680 return;
681 if (msg->icmph.icmp6_type != NDISC_NEIGHBOUR_SOLICITATION)
682 return;
683
684 saddr = &ipv6_hdr(skb)->saddr;
685 daddr = &ipv6_hdr(skb)->daddr;
686
687 size = sizeof(struct icmp6hdr) + sizeof(struct in6_addr);
688
689 spin_lock_irqsave(&npinfo->rx_lock, flags);
690 list_for_each_entry_safe(np, tmp, &npinfo->rx_np, rx) {
Cong Wangfaeed822013-01-27 15:55:20 +0000691 if (!ipv6_addr_equal(daddr, &np->local_ip.in6))
Cong Wangb3d936f2013-01-07 20:52:41 +0000692 continue;
693
694 hlen = LL_RESERVED_SPACE(np->dev);
695 tlen = np->dev->needed_tailroom;
696 send_skb = find_skb(np, size + hlen + tlen, hlen);
697 if (!send_skb)
698 continue;
699
700 send_skb->protocol = htons(ETH_P_IPV6);
701 send_skb->dev = skb->dev;
702
703 skb_reset_network_header(send_skb);
Amerigo Wang00f97da2013-06-03 16:31:36 +0000704 hdr = (struct ipv6hdr *) skb_put(send_skb, sizeof(struct ipv6hdr));
Cong Wangb3d936f2013-01-07 20:52:41 +0000705 *(__be32*)hdr = htonl(0x60000000);
Cong Wangb3d936f2013-01-07 20:52:41 +0000706 hdr->payload_len = htons(size);
707 hdr->nexthdr = IPPROTO_ICMPV6;
708 hdr->hop_limit = 255;
709 hdr->saddr = *saddr;
710 hdr->daddr = *daddr;
711
Amerigo Wang00f97da2013-06-03 16:31:36 +0000712 icmp6h = (struct icmp6hdr *) skb_put(send_skb, sizeof(struct icmp6hdr));
Cong Wangb3d936f2013-01-07 20:52:41 +0000713 icmp6h->icmp6_type = NDISC_NEIGHBOUR_ADVERTISEMENT;
714 icmp6h->icmp6_router = 0;
715 icmp6h->icmp6_solicited = 1;
Amerigo Wang00f97da2013-06-03 16:31:36 +0000716
717 target = (struct in6_addr *) skb_put(send_skb, sizeof(struct in6_addr));
Cong Wangb3d936f2013-01-07 20:52:41 +0000718 *target = msg->target;
719 icmp6h->icmp6_cksum = csum_ipv6_magic(saddr, daddr, size,
720 IPPROTO_ICMPV6,
721 csum_partial(icmp6h,
722 size, 0));
723
724 if (dev_hard_header(send_skb, skb->dev, ETH_P_IPV6,
725 lladdr, np->dev->dev_addr,
726 send_skb->len) < 0) {
727 kfree_skb(send_skb);
728 continue;
729 }
730
731 netpoll_send_skb(np, send_skb);
732
Antonio Quartulli8fb479a2013-10-23 23:36:30 +0200733 /* If there are several rx_skb_hooks for the same
734 * address, we're fine by sending a single reply
735 */
Cong Wangb3d936f2013-01-07 20:52:41 +0000736 break;
737 }
738 spin_unlock_irqrestore(&npinfo->rx_lock, flags);
739#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741}
742
Cong Wangb3d936f2013-01-07 20:52:41 +0000743static bool pkt_is_ns(struct sk_buff *skb)
744{
745 struct nd_msg *msg;
746 struct ipv6hdr *hdr;
747
748 if (skb->protocol != htons(ETH_P_ARP))
749 return false;
750 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr) + sizeof(struct nd_msg)))
751 return false;
752
753 msg = (struct nd_msg *)skb_transport_header(skb);
754 __skb_push(skb, skb->data - skb_transport_header(skb));
755 hdr = ipv6_hdr(skb);
756
757 if (hdr->nexthdr != IPPROTO_ICMPV6)
758 return false;
759 if (hdr->hop_limit != 255)
760 return false;
761 if (msg->icmph.icmp6_code != 0)
762 return false;
763 if (msg->icmph.icmp6_type != NDISC_NEIGHBOUR_SOLICITATION)
764 return false;
765
766 return true;
767}
768
Amerigo Wang57c5d462012-08-10 01:24:40 +0000769int __netpoll_rx(struct sk_buff *skb, struct netpoll_info *npinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770{
Antonio Quartulli8fb479a2013-10-23 23:36:30 +0200771 int proto, len, ulen, data_len;
772 int hits = 0, offset;
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000773 const struct iphdr *iph;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 struct udphdr *uh;
Daniel Borkmann508e14b2010-01-12 14:27:30 +0000775 struct netpoll *np, *tmp;
Antonio Quartulli8fb479a2013-10-23 23:36:30 +0200776 uint16_t source;
Neil Horman068c6e92006-06-26 00:04:27 -0700777
Eric W. Biedermanff607632014-03-14 20:47:49 -0700778 if (!netpoll_rx_processing(npinfo))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 goto out;
Daniel Borkmann508e14b2010-01-12 14:27:30 +0000780
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 if (skb->dev->type != ARPHRD_ETHER)
782 goto out;
783
David S. Millerd9452e92008-03-04 12:28:49 -0800784 /* check if netpoll clients need ARP */
Eric W. Biedermanad8d4752014-03-14 20:49:43 -0700785 if (skb->protocol == htons(ETH_P_ARP) && netpoll_trap()) {
Cong Wangb3d936f2013-01-07 20:52:41 +0000786 skb_queue_tail(&npinfo->neigh_tx, skb);
787 return 1;
Eric W. Biedermanad8d4752014-03-14 20:49:43 -0700788 } else if (pkt_is_ns(skb) && netpoll_trap()) {
Cong Wangb7394d22013-01-07 20:52:39 +0000789 skb_queue_tail(&npinfo->neigh_tx, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 return 1;
791 }
792
Amerigo Wang689971b2012-08-10 01:24:49 +0000793 if (skb->protocol == cpu_to_be16(ETH_P_8021Q)) {
794 skb = vlan_untag(skb);
795 if (unlikely(!skb))
796 goto out;
797 }
798
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 proto = ntohs(eth_hdr(skb)->h_proto);
Cong Wangb7394d22013-01-07 20:52:39 +0000800 if (proto != ETH_P_IP && proto != ETH_P_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 goto out;
802 if (skb->pkt_type == PACKET_OTHERHOST)
803 goto out;
804 if (skb_shared(skb))
805 goto out;
806
Cong Wangb7394d22013-01-07 20:52:39 +0000807 if (proto == ETH_P_IP) {
808 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
809 goto out;
810 iph = (struct iphdr *)skb->data;
811 if (iph->ihl < 5 || iph->version != 4)
812 goto out;
813 if (!pskb_may_pull(skb, iph->ihl*4))
814 goto out;
815 iph = (struct iphdr *)skb->data;
816 if (ip_fast_csum((u8 *)iph, iph->ihl) != 0)
817 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818
Cong Wangb7394d22013-01-07 20:52:39 +0000819 len = ntohs(iph->tot_len);
820 if (skb->len < len || len < iph->ihl*4)
821 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822
Cong Wangb7394d22013-01-07 20:52:39 +0000823 /*
824 * Our transport medium may have padded the buffer out.
825 * Now We trim to the true length of the frame.
826 */
827 if (pskb_trim_rcsum(skb, len))
828 goto out;
Aubrey.Li5e7d7fa2007-04-17 12:40:20 -0700829
Cong Wangb7394d22013-01-07 20:52:39 +0000830 iph = (struct iphdr *)skb->data;
831 if (iph->protocol != IPPROTO_UDP)
832 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833
Cong Wangb7394d22013-01-07 20:52:39 +0000834 len -= iph->ihl*4;
835 uh = (struct udphdr *)(((char *)iph) + iph->ihl*4);
Antonio Quartulli8fb479a2013-10-23 23:36:30 +0200836 offset = (unsigned char *)(uh + 1) - skb->data;
Cong Wangb7394d22013-01-07 20:52:39 +0000837 ulen = ntohs(uh->len);
Antonio Quartulli8fb479a2013-10-23 23:36:30 +0200838 data_len = skb->len - offset;
839 source = ntohs(uh->source);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840
Cong Wangb7394d22013-01-07 20:52:39 +0000841 if (ulen != len)
842 goto out;
843 if (checksum_udp(skb, uh, ulen, iph->saddr, iph->daddr))
844 goto out;
845 list_for_each_entry_safe(np, tmp, &npinfo->rx_np, rx) {
846 if (np->local_ip.ip && np->local_ip.ip != iph->daddr)
847 continue;
848 if (np->remote_ip.ip && np->remote_ip.ip != iph->saddr)
849 continue;
850 if (np->local_port && np->local_port != ntohs(uh->dest))
851 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852
Antonio Quartulli8fb479a2013-10-23 23:36:30 +0200853 np->rx_skb_hook(np, source, skb, offset, data_len);
Cong Wangb7394d22013-01-07 20:52:39 +0000854 hits++;
855 }
Cong Wangb3d936f2013-01-07 20:52:41 +0000856 } else {
857#if IS_ENABLED(CONFIG_IPV6)
858 const struct ipv6hdr *ip6h;
859
860 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
861 goto out;
862 ip6h = (struct ipv6hdr *)skb->data;
863 if (ip6h->version != 6)
864 goto out;
865 len = ntohs(ip6h->payload_len);
866 if (!len)
867 goto out;
868 if (len + sizeof(struct ipv6hdr) > skb->len)
869 goto out;
870 if (pskb_trim_rcsum(skb, len + sizeof(struct ipv6hdr)))
871 goto out;
872 ip6h = ipv6_hdr(skb);
873 if (!pskb_may_pull(skb, sizeof(struct udphdr)))
874 goto out;
875 uh = udp_hdr(skb);
Antonio Quartulli8fb479a2013-10-23 23:36:30 +0200876 offset = (unsigned char *)(uh + 1) - skb->data;
Cong Wangb3d936f2013-01-07 20:52:41 +0000877 ulen = ntohs(uh->len);
Antonio Quartulli8fb479a2013-10-23 23:36:30 +0200878 data_len = skb->len - offset;
879 source = ntohs(uh->source);
Cong Wangb3d936f2013-01-07 20:52:41 +0000880 if (ulen != skb->len)
881 goto out;
882 if (udp6_csum_init(skb, uh, IPPROTO_UDP))
883 goto out;
884 list_for_each_entry_safe(np, tmp, &npinfo->rx_np, rx) {
Cong Wangfaeed822013-01-27 15:55:20 +0000885 if (!ipv6_addr_equal(&np->local_ip.in6, &ip6h->daddr))
Cong Wangb3d936f2013-01-07 20:52:41 +0000886 continue;
Cong Wangfaeed822013-01-27 15:55:20 +0000887 if (!ipv6_addr_equal(&np->remote_ip.in6, &ip6h->saddr))
Cong Wangb3d936f2013-01-07 20:52:41 +0000888 continue;
889 if (np->local_port && np->local_port != ntohs(uh->dest))
890 continue;
891
Antonio Quartulli8fb479a2013-10-23 23:36:30 +0200892 np->rx_skb_hook(np, source, skb, offset, data_len);
Cong Wangb3d936f2013-01-07 20:52:41 +0000893 hits++;
894 }
895#endif
Daniel Borkmann508e14b2010-01-12 14:27:30 +0000896 }
897
898 if (!hits)
899 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900
901 kfree_skb(skb);
902 return 1;
903
904out:
Eric W. Biedermanad8d4752014-03-14 20:49:43 -0700905 if (netpoll_trap()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 kfree_skb(skb);
907 return 1;
908 }
909
910 return 0;
911}
912
Eric W. Biedermane1bd4d32014-03-14 20:50:58 -0700913static void netpoll_trap_setup_info(struct netpoll_info *npinfo)
914{
915 INIT_LIST_HEAD(&npinfo->rx_np);
916 spin_lock_init(&npinfo->rx_lock);
917 skb_queue_head_init(&npinfo->neigh_tx);
918}
919
920static void netpoll_trap_cleanup_info(struct netpoll_info *npinfo)
921{
922 skb_queue_purge(&npinfo->neigh_tx);
923}
924
925static void netpoll_trap_setup(struct netpoll *np, struct netpoll_info *npinfo)
926{
927 unsigned long flags;
928 if (np->rx_skb_hook) {
929 spin_lock_irqsave(&npinfo->rx_lock, flags);
930 list_add_tail(&np->rx, &npinfo->rx_np);
931 spin_unlock_irqrestore(&npinfo->rx_lock, flags);
932 }
933}
934
935static void netpoll_trap_cleanup(struct netpoll *np, struct netpoll_info *npinfo)
936{
937 unsigned long flags;
938 if (!list_empty(&npinfo->rx_np)) {
939 spin_lock_irqsave(&npinfo->rx_lock, flags);
940 list_del(&np->rx);
941 spin_unlock_irqrestore(&npinfo->rx_lock, flags);
942 }
943}
944
945#else /* !CONFIG_NETPOLL_TRAP */
946static inline void netpoll_trap_setup_info(struct netpoll_info *npinfo)
947{
948}
949static inline void netpoll_trap_cleanup_info(struct netpoll_info *npinfo)
950{
951}
952static inline
953void netpoll_trap_setup(struct netpoll *np, struct netpoll_info *npinfo)
954{
955}
956static inline
957void netpoll_trap_cleanup(struct netpoll *np, struct netpoll_info *npinfo)
958{
959}
960#endif /* CONFIG_NETPOLL_TRAP */
961
Satyam Sharma0bcc1812007-08-10 15:35:05 -0700962void netpoll_print_options(struct netpoll *np)
963{
Joe Perchese6ec269352012-01-29 15:50:43 +0000964 np_info(np, "local port %d\n", np->local_port);
Cong Wangb3d936f2013-01-07 20:52:41 +0000965 if (np->ipv6)
966 np_info(np, "local IPv6 address %pI6c\n", &np->local_ip.in6);
967 else
Cong Wangb7394d22013-01-07 20:52:39 +0000968 np_info(np, "local IPv4 address %pI4\n", &np->local_ip.ip);
Joe Perchese6ec269352012-01-29 15:50:43 +0000969 np_info(np, "interface '%s'\n", np->dev_name);
970 np_info(np, "remote port %d\n", np->remote_port);
Cong Wangb3d936f2013-01-07 20:52:41 +0000971 if (np->ipv6)
972 np_info(np, "remote IPv6 address %pI6c\n", &np->remote_ip.in6);
973 else
Cong Wangb7394d22013-01-07 20:52:39 +0000974 np_info(np, "remote IPv4 address %pI4\n", &np->remote_ip.ip);
Joe Perchese6ec269352012-01-29 15:50:43 +0000975 np_info(np, "remote ethernet address %pM\n", np->remote_mac);
Satyam Sharma0bcc1812007-08-10 15:35:05 -0700976}
Eric Dumazet9e34a5b2010-07-09 21:22:04 +0000977EXPORT_SYMBOL(netpoll_print_options);
Satyam Sharma0bcc1812007-08-10 15:35:05 -0700978
Cong Wangb7394d22013-01-07 20:52:39 +0000979static int netpoll_parse_ip_addr(const char *str, union inet_addr *addr)
980{
981 const char *end;
982
983 if (!strchr(str, ':') &&
984 in4_pton(str, -1, (void *)addr, -1, &end) > 0) {
985 if (!*end)
986 return 0;
987 }
988 if (in6_pton(str, -1, addr->in6.s6_addr, -1, &end) > 0) {
989#if IS_ENABLED(CONFIG_IPV6)
990 if (!*end)
991 return 1;
992#else
993 return -1;
994#endif
995 }
996 return -1;
997}
998
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999int netpoll_parse_options(struct netpoll *np, char *opt)
1000{
1001 char *cur=opt, *delim;
Cong Wangb7394d22013-01-07 20:52:39 +00001002 int ipv6;
Sabrina Dubroca00fe11b2014-02-06 18:34:12 +01001003 bool ipversion_set = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004
David S. Millerc68b9072006-11-14 20:40:49 -08001005 if (*cur != '@') {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 if ((delim = strchr(cur, '@')) == NULL)
1007 goto parse_failed;
David S. Millerc68b9072006-11-14 20:40:49 -08001008 *delim = 0;
Abhijit Pawar4b5511e2012-12-09 23:12:28 +00001009 if (kstrtou16(cur, 10, &np->local_port))
1010 goto parse_failed;
David S. Millerc68b9072006-11-14 20:40:49 -08001011 cur = delim;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 }
1013 cur++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014
David S. Millerc68b9072006-11-14 20:40:49 -08001015 if (*cur != '/') {
Sabrina Dubroca00fe11b2014-02-06 18:34:12 +01001016 ipversion_set = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 if ((delim = strchr(cur, '/')) == NULL)
1018 goto parse_failed;
David S. Millerc68b9072006-11-14 20:40:49 -08001019 *delim = 0;
Cong Wangb7394d22013-01-07 20:52:39 +00001020 ipv6 = netpoll_parse_ip_addr(cur, &np->local_ip);
1021 if (ipv6 < 0)
1022 goto parse_failed;
1023 else
1024 np->ipv6 = (bool)ipv6;
David S. Millerc68b9072006-11-14 20:40:49 -08001025 cur = delim;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 }
1027 cur++;
1028
David S. Millerc68b9072006-11-14 20:40:49 -08001029 if (*cur != ',') {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 /* parse out dev name */
1031 if ((delim = strchr(cur, ',')) == NULL)
1032 goto parse_failed;
David S. Millerc68b9072006-11-14 20:40:49 -08001033 *delim = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 strlcpy(np->dev_name, cur, sizeof(np->dev_name));
David S. Millerc68b9072006-11-14 20:40:49 -08001035 cur = delim;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 }
1037 cur++;
1038
David S. Millerc68b9072006-11-14 20:40:49 -08001039 if (*cur != '@') {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 /* dst port */
1041 if ((delim = strchr(cur, '@')) == NULL)
1042 goto parse_failed;
David S. Millerc68b9072006-11-14 20:40:49 -08001043 *delim = 0;
Amerigo Wang5fc05f82010-03-21 22:59:58 +00001044 if (*cur == ' ' || *cur == '\t')
Joe Perchese6ec269352012-01-29 15:50:43 +00001045 np_info(np, "warning: whitespace is not allowed\n");
Abhijit Pawar4b5511e2012-12-09 23:12:28 +00001046 if (kstrtou16(cur, 10, &np->remote_port))
1047 goto parse_failed;
David S. Millerc68b9072006-11-14 20:40:49 -08001048 cur = delim;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 }
1050 cur++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051
1052 /* dst ip */
1053 if ((delim = strchr(cur, '/')) == NULL)
1054 goto parse_failed;
David S. Millerc68b9072006-11-14 20:40:49 -08001055 *delim = 0;
Cong Wangb7394d22013-01-07 20:52:39 +00001056 ipv6 = netpoll_parse_ip_addr(cur, &np->remote_ip);
1057 if (ipv6 < 0)
1058 goto parse_failed;
Sabrina Dubroca00fe11b2014-02-06 18:34:12 +01001059 else if (ipversion_set && np->ipv6 != (bool)ipv6)
Cong Wangb7394d22013-01-07 20:52:39 +00001060 goto parse_failed;
1061 else
1062 np->ipv6 = (bool)ipv6;
David S. Millerc68b9072006-11-14 20:40:49 -08001063 cur = delim + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064
David S. Millerc68b9072006-11-14 20:40:49 -08001065 if (*cur != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 /* MAC address */
Alexey Dobriyan4940fc82011-05-07 23:00:07 +00001067 if (!mac_pton(cur, np->remote_mac))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 goto parse_failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 }
1070
Satyam Sharma0bcc1812007-08-10 15:35:05 -07001071 netpoll_print_options(np);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072
1073 return 0;
1074
1075 parse_failed:
Joe Perchese6ec269352012-01-29 15:50:43 +00001076 np_info(np, "couldn't parse config at '%s'!\n", cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 return -1;
1078}
Eric Dumazet9e34a5b2010-07-09 21:22:04 +00001079EXPORT_SYMBOL(netpoll_parse_options);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080
Amerigo Wang47be03a22012-08-10 01:24:37 +00001081int __netpoll_setup(struct netpoll *np, struct net_device *ndev, gfp_t gfp)
Herbert Xu8fdd95e2010-06-10 16:12:48 +00001082{
Herbert Xu8fdd95e2010-06-10 16:12:48 +00001083 struct netpoll_info *npinfo;
1084 const struct net_device_ops *ops;
Herbert Xu8fdd95e2010-06-10 16:12:48 +00001085 int err;
1086
Jiri Pirko30fdd8a02012-07-17 05:22:35 +00001087 np->dev = ndev;
1088 strlcpy(np->dev_name, ndev->name, IFNAMSIZ);
Neil Horman2cde6ac2013-02-11 10:25:30 +00001089 INIT_WORK(&np->cleanup_work, netpoll_async_cleanup);
Jiri Pirko30fdd8a02012-07-17 05:22:35 +00001090
Herbert Xu8fdd95e2010-06-10 16:12:48 +00001091 if ((ndev->priv_flags & IFF_DISABLE_NETPOLL) ||
1092 !ndev->netdev_ops->ndo_poll_controller) {
Joe Perchese6ec269352012-01-29 15:50:43 +00001093 np_err(np, "%s doesn't support polling, aborting\n",
1094 np->dev_name);
Herbert Xu8fdd95e2010-06-10 16:12:48 +00001095 err = -ENOTSUPP;
1096 goto out;
1097 }
1098
1099 if (!ndev->npinfo) {
Amerigo Wang47be03a22012-08-10 01:24:37 +00001100 npinfo = kmalloc(sizeof(*npinfo), gfp);
Herbert Xu8fdd95e2010-06-10 16:12:48 +00001101 if (!npinfo) {
1102 err = -ENOMEM;
1103 goto out;
1104 }
1105
Eric W. Biedermane1bd4d32014-03-14 20:50:58 -07001106 netpoll_trap_setup_info(npinfo);
Herbert Xu8fdd95e2010-06-10 16:12:48 +00001107
Neil Hormanbd7c4b62013-04-30 05:35:05 +00001108 sema_init(&npinfo->dev_lock, 1);
Herbert Xu8fdd95e2010-06-10 16:12:48 +00001109 skb_queue_head_init(&npinfo->txq);
1110 INIT_DELAYED_WORK(&npinfo->tx_work, queue_process);
1111
1112 atomic_set(&npinfo->refcnt, 1);
1113
1114 ops = np->dev->netdev_ops;
1115 if (ops->ndo_netpoll_setup) {
Amerigo Wang47be03a22012-08-10 01:24:37 +00001116 err = ops->ndo_netpoll_setup(ndev, npinfo, gfp);
Herbert Xu8fdd95e2010-06-10 16:12:48 +00001117 if (err)
1118 goto free_npinfo;
1119 }
1120 } else {
Neil Horman0790bbb2013-02-11 10:25:31 +00001121 npinfo = rtnl_dereference(ndev->npinfo);
Herbert Xu8fdd95e2010-06-10 16:12:48 +00001122 atomic_inc(&npinfo->refcnt);
1123 }
1124
1125 npinfo->netpoll = np;
1126
Eric W. Biedermane1bd4d32014-03-14 20:50:58 -07001127 netpoll_trap_setup(np, npinfo);
Herbert Xu8fdd95e2010-06-10 16:12:48 +00001128
1129 /* last thing to do is link it to the net device structure */
Eric Dumazetcf778b02012-01-12 04:41:32 +00001130 rcu_assign_pointer(ndev->npinfo, npinfo);
Herbert Xu8fdd95e2010-06-10 16:12:48 +00001131
1132 return 0;
1133
1134free_npinfo:
1135 kfree(npinfo);
1136out:
1137 return err;
1138}
1139EXPORT_SYMBOL_GPL(__netpoll_setup);
1140
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141int netpoll_setup(struct netpoll *np)
1142{
1143 struct net_device *ndev = NULL;
1144 struct in_device *in_dev;
Stephen Hemmingerb41848b2006-10-26 15:46:52 -07001145 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146
Cong Wangf92d3182013-01-14 23:34:06 +00001147 rtnl_lock();
Cong Wang556e6252013-01-27 15:55:21 +00001148 if (np->dev_name) {
1149 struct net *net = current->nsproxy->net_ns;
1150 ndev = __dev_get_by_name(net, np->dev_name);
1151 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 if (!ndev) {
Joe Perchese6ec269352012-01-29 15:50:43 +00001153 np_err(np, "%s doesn't exist, aborting\n", np->dev_name);
Cong Wangf92d3182013-01-14 23:34:06 +00001154 err = -ENODEV;
1155 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 }
Cong Wang5bd30d32013-01-17 12:21:08 +08001157 dev_hold(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158
Jiri Pirko49bd8fb2013-01-03 22:48:55 +00001159 if (netdev_master_upper_dev_get(ndev)) {
Joe Perchese6ec269352012-01-29 15:50:43 +00001160 np_err(np, "%s is a slave device, aborting\n", np->dev_name);
Dan Carpenter83fe32d2011-06-11 18:55:22 -07001161 err = -EBUSY;
1162 goto put;
WANG Cong0c1ad042011-06-09 00:28:13 -07001163 }
1164
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 if (!netif_running(ndev)) {
1166 unsigned long atmost, atleast;
1167
Joe Perchese6ec269352012-01-29 15:50:43 +00001168 np_info(np, "device %s not up yet, forcing it\n", np->dev_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169
Stephen Hemmingerb41848b2006-10-26 15:46:52 -07001170 err = dev_open(ndev);
Stephen Hemmingerb41848b2006-10-26 15:46:52 -07001171
1172 if (err) {
Joe Perchese6ec269352012-01-29 15:50:43 +00001173 np_err(np, "failed to open %s\n", ndev->name);
Herbert Xudbaa1542010-06-10 16:12:46 +00001174 goto put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176
Cong Wangf92d3182013-01-14 23:34:06 +00001177 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 atleast = jiffies + HZ/10;
Anton Vorontsovbff38772009-07-08 11:10:56 -07001179 atmost = jiffies + carrier_timeout * HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 while (!netif_carrier_ok(ndev)) {
1181 if (time_after(jiffies, atmost)) {
Joe Perchese6ec269352012-01-29 15:50:43 +00001182 np_notice(np, "timeout waiting for carrier\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 break;
1184 }
Anton Vorontsov1b614fb2009-07-08 20:09:44 -07001185 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 }
1187
1188 /* If carrier appears to come up instantly, we don't
1189 * trust it and pause so that we don't pump all our
1190 * queued console messages into the bitbucket.
1191 */
1192
1193 if (time_before(jiffies, atleast)) {
Joe Perchese6ec269352012-01-29 15:50:43 +00001194 np_notice(np, "carrier detect appears untrustworthy, waiting 4 seconds\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 msleep(4000);
1196 }
Cong Wangf92d3182013-01-14 23:34:06 +00001197 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 }
1199
Cong Wangb7394d22013-01-07 20:52:39 +00001200 if (!np->local_ip.ip) {
1201 if (!np->ipv6) {
Cong Wangf92d3182013-01-14 23:34:06 +00001202 in_dev = __in_dev_get_rtnl(ndev);
Cong Wangb7394d22013-01-07 20:52:39 +00001203
1204 if (!in_dev || !in_dev->ifa_list) {
Cong Wangb7394d22013-01-07 20:52:39 +00001205 np_err(np, "no IP address for %s, aborting\n",
1206 np->dev_name);
1207 err = -EDESTADDRREQ;
1208 goto put;
1209 }
1210
1211 np->local_ip.ip = in_dev->ifa_list->ifa_local;
Cong Wangb7394d22013-01-07 20:52:39 +00001212 np_info(np, "local IP %pI4\n", &np->local_ip.ip);
Cong Wangb3d936f2013-01-07 20:52:41 +00001213 } else {
1214#if IS_ENABLED(CONFIG_IPV6)
1215 struct inet6_dev *idev;
1216
1217 err = -EDESTADDRREQ;
Cong Wangb3d936f2013-01-07 20:52:41 +00001218 idev = __in6_dev_get(ndev);
1219 if (idev) {
1220 struct inet6_ifaddr *ifp;
1221
1222 read_lock_bh(&idev->lock);
1223 list_for_each_entry(ifp, &idev->addr_list, if_list) {
1224 if (ipv6_addr_type(&ifp->addr) & IPV6_ADDR_LINKLOCAL)
1225 continue;
1226 np->local_ip.in6 = ifp->addr;
1227 err = 0;
1228 break;
1229 }
1230 read_unlock_bh(&idev->lock);
1231 }
Cong Wangb3d936f2013-01-07 20:52:41 +00001232 if (err) {
1233 np_err(np, "no IPv6 address for %s, aborting\n",
1234 np->dev_name);
1235 goto put;
1236 } else
1237 np_info(np, "local IPv6 %pI6c\n", &np->local_ip.in6);
1238#else
1239 np_err(np, "IPv6 is not supported %s, aborting\n",
1240 np->dev_name);
Cong Wange39363a2013-01-22 17:39:11 +00001241 err = -EINVAL;
Cong Wangb3d936f2013-01-07 20:52:41 +00001242 goto put;
1243#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 }
1246
Herbert Xudbaa1542010-06-10 16:12:46 +00001247 /* fill up the skb queue */
1248 refill_skbs();
1249
Amerigo Wang47be03a22012-08-10 01:24:37 +00001250 err = __netpoll_setup(np, ndev, GFP_KERNEL);
Herbert Xu8fdd95e2010-06-10 16:12:48 +00001251 if (err)
1252 goto put;
1253
Cong Wangf92d3182013-01-14 23:34:06 +00001254 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 return 0;
1256
Jiri Slaby21edbb22010-03-16 05:29:54 +00001257put:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 dev_put(ndev);
Cong Wangf92d3182013-01-14 23:34:06 +00001259unlock:
1260 rtnl_unlock();
Stephen Hemmingerb41848b2006-10-26 15:46:52 -07001261 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262}
Eric Dumazet9e34a5b2010-07-09 21:22:04 +00001263EXPORT_SYMBOL(netpoll_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264
David S. Millerc68b9072006-11-14 20:40:49 -08001265static int __init netpoll_init(void)
1266{
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -08001267 skb_queue_head_init(&skb_pool);
1268 return 0;
1269}
1270core_initcall(netpoll_init);
1271
Amerigo Wang38e6bc12012-08-10 01:24:38 +00001272static void rcu_cleanup_netpoll_info(struct rcu_head *rcu_head)
1273{
1274 struct netpoll_info *npinfo =
1275 container_of(rcu_head, struct netpoll_info, rcu);
1276
Eric W. Biedermane1bd4d32014-03-14 20:50:58 -07001277 netpoll_trap_cleanup_info(npinfo);
Amerigo Wang38e6bc12012-08-10 01:24:38 +00001278 skb_queue_purge(&npinfo->txq);
1279
1280 /* we can't call cancel_delayed_work_sync here, as we are in softirq */
1281 cancel_delayed_work(&npinfo->tx_work);
1282
1283 /* clean after last, unfinished work */
1284 __skb_queue_purge(&npinfo->txq);
1285 /* now cancel it again */
1286 cancel_delayed_work(&npinfo->tx_work);
1287 kfree(npinfo);
1288}
1289
Herbert Xu8fdd95e2010-06-10 16:12:48 +00001290void __netpoll_cleanup(struct netpoll *np)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291{
Jeff Moyerfbeec2e2005-06-22 22:05:59 -07001292 struct netpoll_info *npinfo;
Jeff Moyerfbeec2e2005-06-22 22:05:59 -07001293
Neil Horman0790bbb2013-02-11 10:25:31 +00001294 /* rtnl_dereference would be preferable here but
1295 * rcu_cleanup_netpoll path can put us in here safely without
1296 * holding the rtnl, so plain rcu_dereference it is
1297 */
1298 npinfo = rtnl_dereference(np->dev->npinfo);
Herbert Xu8fdd95e2010-06-10 16:12:48 +00001299 if (!npinfo)
Herbert Xudbaa1542010-06-10 16:12:46 +00001300 return;
Stephen Hemminger93ec2c72006-10-26 15:46:50 -07001301
Eric W. Biedermane1bd4d32014-03-14 20:50:58 -07001302 netpoll_trap_cleanup(np, npinfo);
Herbert Xudbaa1542010-06-10 16:12:46 +00001303
Neil Hormanca99ca12013-02-05 08:05:43 +00001304 synchronize_srcu(&netpoll_srcu);
1305
Herbert Xu8fdd95e2010-06-10 16:12:48 +00001306 if (atomic_dec_and_test(&npinfo->refcnt)) {
1307 const struct net_device_ops *ops;
1308
1309 ops = np->dev->netdev_ops;
1310 if (ops->ndo_netpoll_cleanup)
1311 ops->ndo_netpoll_cleanup(np->dev);
1312
Neil Horman2cde6ac2013-02-11 10:25:30 +00001313 rcu_assign_pointer(np->dev->npinfo, NULL);
Amerigo Wang38e6bc12012-08-10 01:24:38 +00001314 call_rcu_bh(&npinfo->rcu, rcu_cleanup_netpoll_info);
Herbert Xudbaa1542010-06-10 16:12:46 +00001315 }
Herbert Xu8fdd95e2010-06-10 16:12:48 +00001316}
1317EXPORT_SYMBOL_GPL(__netpoll_cleanup);
1318
Neil Horman2cde6ac2013-02-11 10:25:30 +00001319static void netpoll_async_cleanup(struct work_struct *work)
Amerigo Wang38e6bc12012-08-10 01:24:38 +00001320{
Neil Horman2cde6ac2013-02-11 10:25:30 +00001321 struct netpoll *np = container_of(work, struct netpoll, cleanup_work);
Amerigo Wang38e6bc12012-08-10 01:24:38 +00001322
Neil Horman2cde6ac2013-02-11 10:25:30 +00001323 rtnl_lock();
Amerigo Wang38e6bc12012-08-10 01:24:38 +00001324 __netpoll_cleanup(np);
Neil Horman2cde6ac2013-02-11 10:25:30 +00001325 rtnl_unlock();
Amerigo Wang38e6bc12012-08-10 01:24:38 +00001326 kfree(np);
1327}
1328
Neil Horman2cde6ac2013-02-11 10:25:30 +00001329void __netpoll_free_async(struct netpoll *np)
Amerigo Wang38e6bc12012-08-10 01:24:38 +00001330{
Neil Horman2cde6ac2013-02-11 10:25:30 +00001331 schedule_work(&np->cleanup_work);
Amerigo Wang38e6bc12012-08-10 01:24:38 +00001332}
Neil Horman2cde6ac2013-02-11 10:25:30 +00001333EXPORT_SYMBOL_GPL(__netpoll_free_async);
Amerigo Wang38e6bc12012-08-10 01:24:38 +00001334
Herbert Xu8fdd95e2010-06-10 16:12:48 +00001335void netpoll_cleanup(struct netpoll *np)
1336{
Herbert Xu8fdd95e2010-06-10 16:12:48 +00001337 rtnl_lock();
Nikolay Aleksandrovd0fe8c882013-09-19 15:02:35 +02001338 if (!np->dev)
1339 goto out;
Herbert Xu8fdd95e2010-06-10 16:12:48 +00001340 __netpoll_cleanup(np);
Herbert Xudbaa1542010-06-10 16:12:46 +00001341 dev_put(np->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 np->dev = NULL;
Nikolay Aleksandrovd0fe8c882013-09-19 15:02:35 +02001343out:
1344 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345}
Eric Dumazet9e34a5b2010-07-09 21:22:04 +00001346EXPORT_SYMBOL(netpoll_cleanup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347
Eric W. Biedermanad8d4752014-03-14 20:49:43 -07001348#ifdef CONFIG_NETPOLL_TRAP
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349int netpoll_trap(void)
1350{
1351 return atomic_read(&trapped);
1352}
Eric Dumazet9e34a5b2010-07-09 21:22:04 +00001353EXPORT_SYMBOL(netpoll_trap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354
1355void netpoll_set_trap(int trap)
1356{
1357 if (trap)
1358 atomic_inc(&trapped);
1359 else
1360 atomic_dec(&trapped);
1361}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362EXPORT_SYMBOL(netpoll_set_trap);
Eric W. Biedermanad8d4752014-03-14 20:49:43 -07001363#endif /* CONFIG_NETPOLL_TRAP */