blob: a5ad1c1c4b18d3d379010ee683d2cb4392b396fd [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/netdevice.h>
16#include <linux/etherdevice.h>
17#include <linux/string.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020018#include <linux/if_arp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/inetdevice.h>
20#include <linux/inet.h>
21#include <linux/interrupt.h>
22#include <linux/netpoll.h>
23#include <linux/sched.h>
24#include <linux/delay.h>
25#include <linux/rcupdate.h>
26#include <linux/workqueue.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090027#include <linux/slab.h>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040028#include <linux/export.h>
Amerigo Wang689971b2012-08-10 01:24:49 +000029#include <linux/if_vlan.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <net/tcp.h>
31#include <net/udp.h>
Cong Wangb3d936f2013-01-07 20:52:41 +000032#include <net/addrconf.h>
33#include <net/ndisc.h>
34#include <net/ip6_checksum.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <asm/unaligned.h>
David S. Miller9cbc1cb2009-06-15 03:02:23 -070036#include <trace/events/napi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
38/*
39 * We maintain a small pool of fully-sized skbs, to make sure the
40 * message gets out even in extreme OOM situations.
41 */
42
43#define MAX_UDP_CHUNK 1460
44#define MAX_SKBS 32
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -080046static struct sk_buff_head skb_pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
48static atomic_t trapped;
49
Stephen Hemminger2bdfe0b2006-10-26 15:46:54 -070050#define USEC_PER_POLL 50
David S. Millerd9452e92008-03-04 12:28:49 -080051#define NETPOLL_RX_ENABLED 1
52#define NETPOLL_RX_DROP 2
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Joe Perches6f706242012-01-29 15:50:44 +000054#define MAX_SKB_SIZE \
55 (sizeof(struct ethhdr) + \
56 sizeof(struct iphdr) + \
57 sizeof(struct udphdr) + \
58 MAX_UDP_CHUNK)
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
David S. Miller3578b0c2010-08-03 00:24:04 -070060static void zap_completion_queue(void);
Cong Wangb7394d22013-01-07 20:52:39 +000061static void netpoll_neigh_reply(struct sk_buff *skb, struct netpoll_info *npinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Anton Vorontsovbff38772009-07-08 11:10:56 -070063static unsigned int carrier_timeout = 4;
64module_param(carrier_timeout, uint, 0644);
65
Joe Perchese6ec269352012-01-29 15:50:43 +000066#define np_info(np, fmt, ...) \
67 pr_info("%s: " fmt, np->name, ##__VA_ARGS__)
68#define np_err(np, fmt, ...) \
69 pr_err("%s: " fmt, np->name, ##__VA_ARGS__)
70#define np_notice(np, fmt, ...) \
71 pr_notice("%s: " fmt, np->name, ##__VA_ARGS__)
72
David Howellsc4028952006-11-22 14:57:56 +000073static void queue_process(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -070074{
David Howells4c1ac1b2006-12-05 14:37:56 +000075 struct netpoll_info *npinfo =
76 container_of(work, struct netpoll_info, tx_work.work);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 struct sk_buff *skb;
Ingo Molnar36405432006-12-12 17:20:42 +010078 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
Stephen Hemminger6c43ff12006-10-26 15:46:53 -070080 while ((skb = skb_dequeue(&npinfo->txq))) {
81 struct net_device *dev = skb->dev;
Stephen Hemminger00829822008-11-20 20:14:53 -080082 const struct net_device_ops *ops = dev->netdev_ops;
David S. Millerfd2ea0a2008-07-17 01:56:23 -070083 struct netdev_queue *txq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
Stephen Hemminger6c43ff12006-10-26 15:46:53 -070085 if (!netif_device_present(dev) || !netif_running(dev)) {
86 __kfree_skb(skb);
87 continue;
88 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
David S. Millerfd2ea0a2008-07-17 01:56:23 -070090 txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb));
91
Ingo Molnar36405432006-12-12 17:20:42 +010092 local_irq_save(flags);
David S. Millerfd2ea0a2008-07-17 01:56:23 -070093 __netif_tx_lock(txq, smp_processor_id());
Tom Herbert734664982011-11-28 16:32:44 +000094 if (netif_xmit_frozen_or_stopped(txq) ||
Stephen Hemminger00829822008-11-20 20:14:53 -080095 ops->ndo_start_xmit(skb, dev) != NETDEV_TX_OK) {
Stephen Hemminger6c43ff12006-10-26 15:46:53 -070096 skb_queue_head(&npinfo->txq, skb);
David S. Millerfd2ea0a2008-07-17 01:56:23 -070097 __netif_tx_unlock(txq);
Ingo Molnar36405432006-12-12 17:20:42 +010098 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
Jarek Poplawski25442ca2007-07-05 17:42:44 -0700100 schedule_delayed_work(&npinfo->tx_work, HZ/10);
Stephen Hemminger6c43ff12006-10-26 15:46:53 -0700101 return;
102 }
David S. Millerfd2ea0a2008-07-17 01:56:23 -0700103 __netif_tx_unlock(txq);
Ingo Molnar36405432006-12-12 17:20:42 +0100104 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 }
106}
107
Al Virob51655b2006-11-14 21:40:42 -0800108static __sum16 checksum_udp(struct sk_buff *skb, struct udphdr *uh,
109 unsigned short ulen, __be32 saddr, __be32 daddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110{
Al Virod6f5493c2006-11-14 21:26:08 -0800111 __wsum psum;
Herbert Xufb286bb2005-11-10 13:01:24 -0800112
Herbert Xu60476372007-04-09 11:59:39 -0700113 if (uh->check == 0 || skb_csum_unnecessary(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 return 0;
115
Herbert Xufb286bb2005-11-10 13:01:24 -0800116 psum = csum_tcpudp_nofold(saddr, daddr, ulen, IPPROTO_UDP, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
Patrick McHardy84fa7932006-08-29 16:44:56 -0700118 if (skb->ip_summed == CHECKSUM_COMPLETE &&
Al Virod3bc23e2006-11-14 21:24:49 -0800119 !csum_fold(csum_add(psum, skb->csum)))
Herbert Xufb286bb2005-11-10 13:01:24 -0800120 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
Herbert Xufb286bb2005-11-10 13:01:24 -0800122 skb->csum = psum;
123
124 return __skb_checksum_complete(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125}
126
127/*
128 * Check whether delayed processing was scheduled for our NIC. If so,
129 * we attempt to grab the poll lock and use ->poll() to pump the card.
130 * If this fails, either we've recursed in ->poll() or it's already
131 * running on another CPU.
132 *
133 * Note: we don't mask interrupts with this lock because we're using
134 * trylock here and interrupts are already disabled in the softirq
135 * case. Further, we test the poll_owner to avoid recursion on UP
136 * systems where the lock doesn't exist.
137 *
138 * In cases where there is bi-directional communications, reading only
139 * one message at a time can lead to packets being dropped by the
140 * network adapter, forcing superfluous retries and possibly timeouts.
141 * Thus, we set our budget to greater than 1.
142 */
David S. Miller0a7606c2007-10-29 21:28:47 -0700143static int poll_one_napi(struct netpoll_info *npinfo,
144 struct napi_struct *napi, int budget)
145{
146 int work;
147
148 /* net_rx_action's ->poll() invocations and our's are
149 * synchronized by this test which is only made while
150 * holding the napi->poll_lock.
151 */
152 if (!test_bit(NAPI_STATE_SCHED, &napi->state))
153 return budget;
154
David S. Millerd9452e92008-03-04 12:28:49 -0800155 npinfo->rx_flags |= NETPOLL_RX_DROP;
David S. Miller0a7606c2007-10-29 21:28:47 -0700156 atomic_inc(&trapped);
Neil Horman7b363e42008-12-09 23:22:26 -0800157 set_bit(NAPI_STATE_NPSVC, &napi->state);
David S. Miller0a7606c2007-10-29 21:28:47 -0700158
159 work = napi->poll(napi, budget);
David S. Miller7d18f112009-05-21 23:30:09 -0700160 trace_napi_poll(napi);
David S. Miller0a7606c2007-10-29 21:28:47 -0700161
Neil Horman7b363e42008-12-09 23:22:26 -0800162 clear_bit(NAPI_STATE_NPSVC, &napi->state);
David S. Miller0a7606c2007-10-29 21:28:47 -0700163 atomic_dec(&trapped);
David S. Millerd9452e92008-03-04 12:28:49 -0800164 npinfo->rx_flags &= ~NETPOLL_RX_DROP;
David S. Miller0a7606c2007-10-29 21:28:47 -0700165
166 return budget - work;
167}
168
Stephen Hemminger51069302007-11-19 19:18:11 -0800169static void poll_napi(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170{
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700171 struct napi_struct *napi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 int budget = 16;
173
Neil Hormanf13d4932010-10-19 07:04:26 +0000174 list_for_each_entry(napi, &dev->napi_list, dev_list) {
David S. Miller0a7606c2007-10-29 21:28:47 -0700175 if (napi->poll_owner != smp_processor_id() &&
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700176 spin_trylock(&napi->poll_lock)) {
Amerigo Wang28996562012-08-10 01:24:42 +0000177 budget = poll_one_napi(rcu_dereference_bh(dev->npinfo),
178 napi, budget);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700179 spin_unlock(&napi->poll_lock);
David S. Miller0a7606c2007-10-29 21:28:47 -0700180
Amerigo Wang072a9c42012-08-24 21:41:11 +0000181 if (!budget)
David S. Miller0a7606c2007-10-29 21:28:47 -0700182 break;
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700183 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 }
185}
186
Cong Wangb7394d22013-01-07 20:52:39 +0000187static void service_neigh_queue(struct netpoll_info *npi)
Neil Horman068c6e92006-06-26 00:04:27 -0700188{
Stephen Hemminger51069302007-11-19 19:18:11 -0800189 if (npi) {
190 struct sk_buff *skb;
Neil Horman068c6e92006-06-26 00:04:27 -0700191
Cong Wangb7394d22013-01-07 20:52:39 +0000192 while ((skb = skb_dequeue(&npi->neigh_tx)))
193 netpoll_neigh_reply(skb, npi);
Neil Horman068c6e92006-06-26 00:04:27 -0700194 }
Neil Horman068c6e92006-06-26 00:04:27 -0700195}
196
Joe Perches234b9212011-06-30 15:08:57 +0000197static void netpoll_poll_dev(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198{
Pavel Emelyanov5e392732009-05-11 00:36:35 +0000199 const struct net_device_ops *ops;
Amerigo Wang28996562012-08-10 01:24:42 +0000200 struct netpoll_info *ni = rcu_dereference_bh(dev->npinfo);
Stephen Hemminger51069302007-11-19 19:18:11 -0800201
Pavel Emelyanov5e392732009-05-11 00:36:35 +0000202 if (!dev || !netif_running(dev))
203 return;
204
205 ops = dev->netdev_ops;
206 if (!ops->ndo_poll_controller)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 return;
208
209 /* Process pending work on NIC */
Stephen Hemmingerd3147742008-11-19 21:32:24 -0800210 ops->ndo_poll_controller(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
Stephen Hemminger51069302007-11-19 19:18:11 -0800212 poll_napi(dev);
213
Eric Dumazet58e05f32012-02-14 10:11:59 +0000214 if (dev->flags & IFF_SLAVE) {
Amerigo Wang28996562012-08-10 01:24:42 +0000215 if (ni) {
Jiri Pirko49bd8fb2013-01-03 22:48:55 +0000216 struct net_device *bond_dev;
Amerigo Wang5a698af2011-02-17 23:43:34 +0000217 struct sk_buff *skb;
Jiri Pirko49bd8fb2013-01-03 22:48:55 +0000218 struct netpoll_info *bond_ni;
219
220 bond_dev = netdev_master_upper_dev_get_rcu(dev);
221 bond_ni = rcu_dereference_bh(bond_dev->npinfo);
Cong Wangb7394d22013-01-07 20:52:39 +0000222 while ((skb = skb_dequeue(&ni->neigh_tx))) {
Amerigo Wang5a698af2011-02-17 23:43:34 +0000223 skb->dev = bond_dev;
Cong Wangb7394d22013-01-07 20:52:39 +0000224 skb_queue_tail(&bond_ni->neigh_tx, skb);
Amerigo Wang5a698af2011-02-17 23:43:34 +0000225 }
226 }
227 }
228
Cong Wangb7394d22013-01-07 20:52:39 +0000229 service_neigh_queue(ni);
Neil Horman068c6e92006-06-26 00:04:27 -0700230
David S. Miller3578b0c2010-08-03 00:24:04 -0700231 zap_completion_queue();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232}
233
234static void refill_skbs(void)
235{
236 struct sk_buff *skb;
237 unsigned long flags;
238
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -0800239 spin_lock_irqsave(&skb_pool.lock, flags);
240 while (skb_pool.qlen < MAX_SKBS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 skb = alloc_skb(MAX_SKB_SIZE, GFP_ATOMIC);
242 if (!skb)
243 break;
244
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -0800245 __skb_queue_tail(&skb_pool, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 }
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -0800247 spin_unlock_irqrestore(&skb_pool.lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248}
249
David S. Miller3578b0c2010-08-03 00:24:04 -0700250static void zap_completion_queue(void)
251{
252 unsigned long flags;
253 struct softnet_data *sd = &get_cpu_var(softnet_data);
254
255 if (sd->completion_queue) {
256 struct sk_buff *clist;
257
258 local_irq_save(flags);
259 clist = sd->completion_queue;
260 sd->completion_queue = NULL;
261 local_irq_restore(flags);
262
263 while (clist != NULL) {
264 struct sk_buff *skb = clist;
265 clist = clist->next;
266 if (skb->destructor) {
267 atomic_inc(&skb->users);
268 dev_kfree_skb_any(skb); /* put this one back */
269 } else {
270 __kfree_skb(skb);
271 }
272 }
273 }
274
275 put_cpu_var(softnet_data);
276}
277
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -0800278static struct sk_buff *find_skb(struct netpoll *np, int len, int reserve)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279{
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -0800280 int count = 0;
281 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
David S. Miller3578b0c2010-08-03 00:24:04 -0700283 zap_completion_queue();
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -0800284 refill_skbs();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285repeat:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
287 skb = alloc_skb(len, GFP_ATOMIC);
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -0800288 if (!skb)
289 skb = skb_dequeue(&skb_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
291 if (!skb) {
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -0800292 if (++count < 10) {
Joe Perches2a49e002011-06-30 15:08:58 +0000293 netpoll_poll_dev(np->dev);
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -0800294 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 }
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -0800296 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 }
298
299 atomic_set(&skb->users, 1);
300 skb_reserve(skb, reserve);
301 return skb;
302}
303
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700304static int netpoll_owner_active(struct net_device *dev)
305{
306 struct napi_struct *napi;
307
308 list_for_each_entry(napi, &dev->napi_list, dev_list) {
309 if (napi->poll_owner == smp_processor_id())
310 return 1;
311 }
312 return 0;
313}
314
Amerigo Wang28996562012-08-10 01:24:42 +0000315/* call with IRQ disabled */
Neil Hormanc2355e12010-10-13 16:01:49 +0000316void netpoll_send_skb_on_dev(struct netpoll *np, struct sk_buff *skb,
317 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318{
Stephen Hemminger2bdfe0b2006-10-26 15:46:54 -0700319 int status = NETDEV_TX_BUSY;
320 unsigned long tries;
Stephen Hemminger00829822008-11-20 20:14:53 -0800321 const struct net_device_ops *ops = dev->netdev_ops;
Herbert Xude85d992010-06-10 16:12:44 +0000322 /* It is up to the caller to keep npinfo alive. */
Amerigo Wang28996562012-08-10 01:24:42 +0000323 struct netpoll_info *npinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
Amerigo Wang28996562012-08-10 01:24:42 +0000325 WARN_ON_ONCE(!irqs_disabled());
326
327 npinfo = rcu_dereference_bh(np->dev->npinfo);
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900328 if (!npinfo || !netif_running(dev) || !netif_device_present(dev)) {
329 __kfree_skb(skb);
330 return;
331 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
Stephen Hemminger2bdfe0b2006-10-26 15:46:54 -0700333 /* don't get messages out of order, and no recursion */
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700334 if (skb_queue_len(&npinfo->txq) == 0 && !netpoll_owner_active(dev)) {
David S. Millerfd2ea0a2008-07-17 01:56:23 -0700335 struct netdev_queue *txq;
Andrew Mortona49f99f2006-12-11 17:24:46 -0800336
Amerigo Wang8c4c49d2012-09-17 20:16:31 +0000337 txq = netdev_pick_tx(dev, skb);
David S. Millerfd2ea0a2008-07-17 01:56:23 -0700338
Stephen Hemminger0db3dc72007-06-27 00:39:42 -0700339 /* try until next clock tick */
340 for (tries = jiffies_to_usecs(1)/USEC_PER_POLL;
341 tries > 0; --tries) {
David S. Millerfd2ea0a2008-07-17 01:56:23 -0700342 if (__netif_tx_trylock(txq)) {
Tom Herbert734664982011-11-28 16:32:44 +0000343 if (!netif_xmit_stopped(txq)) {
Amerigo Wang689971b2012-08-10 01:24:49 +0000344 if (vlan_tx_tag_present(skb) &&
345 !(netif_skb_features(skb) & NETIF_F_HW_VLAN_TX)) {
346 skb = __vlan_put_tag(skb, vlan_tx_tag_get(skb));
347 if (unlikely(!skb))
348 break;
349 skb->vlan_tci = 0;
350 }
351
Stephen Hemminger00829822008-11-20 20:14:53 -0800352 status = ops->ndo_start_xmit(skb, dev);
Eric Dumazet08baf562009-05-25 22:58:01 -0700353 if (status == NETDEV_TX_OK)
354 txq_trans_update(txq);
355 }
David S. Millerfd2ea0a2008-07-17 01:56:23 -0700356 __netif_tx_unlock(txq);
Matt Mackallf0d34592005-08-11 19:25:11 -0700357
Andrew Mortone37b8d92006-12-09 14:01:49 -0800358 if (status == NETDEV_TX_OK)
359 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
Andrew Mortone37b8d92006-12-09 14:01:49 -0800361 }
Stephen Hemminger0db3dc72007-06-27 00:39:42 -0700362
363 /* tickle device maybe there is some cleanup */
Joe Perches2a49e002011-06-30 15:08:58 +0000364 netpoll_poll_dev(np->dev);
Stephen Hemminger0db3dc72007-06-27 00:39:42 -0700365
366 udelay(USEC_PER_POLL);
Matt Mackall0db1d6f2005-08-11 19:25:54 -0700367 }
Dongdong Deng79b1bee2009-08-21 03:33:36 +0000368
369 WARN_ONCE(!irqs_disabled(),
Amerigo Wang28996562012-08-10 01:24:42 +0000370 "netpoll_send_skb_on_dev(): %s enabled interrupts in poll (%pF)\n",
Dongdong Deng79b1bee2009-08-21 03:33:36 +0000371 dev->name, ops->ndo_start_xmit);
372
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
Stephen Hemminger2bdfe0b2006-10-26 15:46:54 -0700375 if (status != NETDEV_TX_OK) {
Stephen Hemminger5de4a472006-10-26 15:46:55 -0700376 skb_queue_tail(&npinfo->txq, skb);
David Howells4c1ac1b2006-12-05 14:37:56 +0000377 schedule_delayed_work(&npinfo->tx_work,0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379}
Neil Hormanc2355e12010-10-13 16:01:49 +0000380EXPORT_SYMBOL(netpoll_send_skb_on_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
382void netpoll_send_udp(struct netpoll *np, const char *msg, int len)
383{
Eric Dumazet954fba02012-06-12 19:30:21 +0000384 int total_len, ip_len, udp_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 struct sk_buff *skb;
386 struct udphdr *udph;
387 struct iphdr *iph;
388 struct ethhdr *eth;
Eric Dumazetee130402012-08-24 01:47:26 +0000389 static atomic_t ip_ident;
Cong Wangb3d936f2013-01-07 20:52:41 +0000390 struct ipv6hdr *ip6h;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
392 udp_len = len + sizeof(*udph);
Cong Wangb3d936f2013-01-07 20:52:41 +0000393 if (np->ipv6)
394 ip_len = udp_len + sizeof(*ip6h);
395 else
Cong Wangb7394d22013-01-07 20:52:39 +0000396 ip_len = udp_len + sizeof(*iph);
397
Eric Dumazet954fba02012-06-12 19:30:21 +0000398 total_len = ip_len + LL_RESERVED_SPACE(np->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
Eric Dumazet954fba02012-06-12 19:30:21 +0000400 skb = find_skb(np, total_len + np->dev->needed_tailroom,
401 total_len - len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 if (!skb)
403 return;
404
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -0300405 skb_copy_to_linear_data(skb, msg, len);
Eric Dumazet954fba02012-06-12 19:30:21 +0000406 skb_put(skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
Arnaldo Carvalho de Melo4bedb452007-03-13 14:28:48 -0300408 skb_push(skb, sizeof(*udph));
409 skb_reset_transport_header(skb);
410 udph = udp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 udph->source = htons(np->local_port);
412 udph->dest = htons(np->remote_port);
413 udph->len = htons(udp_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
Cong Wangb3d936f2013-01-07 20:52:41 +0000415 if (np->ipv6) {
416 udph->check = 0;
417 udph->check = csum_ipv6_magic(&np->local_ip.in6,
418 &np->remote_ip.in6,
419 udp_len, IPPROTO_UDP,
420 csum_partial(udph, udp_len, 0));
421 if (udph->check == 0)
422 udph->check = CSUM_MANGLED_0;
423
424 skb_push(skb, sizeof(*ip6h));
425 skb_reset_network_header(skb);
426 ip6h = ipv6_hdr(skb);
427
428 /* ip6h->version = 6; ip6h->priority = 0; */
429 put_unaligned(0x60, (unsigned char *)ip6h);
430 ip6h->flow_lbl[0] = 0;
431 ip6h->flow_lbl[1] = 0;
432 ip6h->flow_lbl[2] = 0;
433
434 ip6h->payload_len = htons(sizeof(struct udphdr) + len);
435 ip6h->nexthdr = IPPROTO_UDP;
436 ip6h->hop_limit = 32;
437 ip6h->saddr = np->local_ip.in6;
438 ip6h->daddr = np->remote_ip.in6;
439
440 eth = (struct ethhdr *) skb_push(skb, ETH_HLEN);
441 skb_reset_mac_header(skb);
442 skb->protocol = eth->h_proto = htons(ETH_P_IPV6);
443 } else {
Cong Wangb7394d22013-01-07 20:52:39 +0000444 udph->check = 0;
445 udph->check = csum_tcpudp_magic(np->local_ip.ip,
446 np->remote_ip.ip,
447 udp_len, IPPROTO_UDP,
448 csum_partial(udph, udp_len, 0));
449 if (udph->check == 0)
450 udph->check = CSUM_MANGLED_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
Cong Wangb7394d22013-01-07 20:52:39 +0000452 skb_push(skb, sizeof(*iph));
453 skb_reset_network_header(skb);
454 iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
Cong Wangb7394d22013-01-07 20:52:39 +0000456 /* iph->version = 4; iph->ihl = 5; */
457 put_unaligned(0x45, (unsigned char *)iph);
458 iph->tos = 0;
459 put_unaligned(htons(ip_len), &(iph->tot_len));
460 iph->id = htons(atomic_inc_return(&ip_ident));
461 iph->frag_off = 0;
462 iph->ttl = 64;
463 iph->protocol = IPPROTO_UDP;
464 iph->check = 0;
465 put_unaligned(np->local_ip.ip, &(iph->saddr));
466 put_unaligned(np->remote_ip.ip, &(iph->daddr));
467 iph->check = ip_fast_csum((unsigned char *)iph, iph->ihl);
468
469 eth = (struct ethhdr *) skb_push(skb, ETH_HLEN);
470 skb_reset_mac_header(skb);
471 skb->protocol = eth->h_proto = htons(ETH_P_IP);
472 }
473
Stephen Hemminger09538642007-11-19 19:23:29 -0800474 memcpy(eth->h_source, np->dev->dev_addr, ETH_ALEN);
475 memcpy(eth->h_dest, np->remote_mac, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
477 skb->dev = np->dev;
478
479 netpoll_send_skb(np, skb);
480}
Eric Dumazet9e34a5b2010-07-09 21:22:04 +0000481EXPORT_SYMBOL(netpoll_send_udp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
Cong Wangb7394d22013-01-07 20:52:39 +0000483static void netpoll_neigh_reply(struct sk_buff *skb, struct netpoll_info *npinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484{
Cong Wangb3d936f2013-01-07 20:52:41 +0000485 int size, type = ARPOP_REPLY;
Al Viro252e33462006-11-14 20:48:11 -0800486 __be32 sip, tip;
Neil Horman47bbec02006-12-08 00:05:55 -0800487 unsigned char *sha;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 struct sk_buff *send_skb;
Daniel Borkmann508e14b2010-01-12 14:27:30 +0000489 struct netpoll *np, *tmp;
490 unsigned long flags;
Herbert Xuae641942011-11-18 02:20:04 +0000491 int hlen, tlen;
Cong Wangb7394d22013-01-07 20:52:39 +0000492 int hits = 0, proto;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
Daniel Borkmann508e14b2010-01-12 14:27:30 +0000494 if (list_empty(&npinfo->rx_np))
495 return;
496
497 /* Before checking the packet, we do some early
498 inspection whether this is interesting at all */
499 spin_lock_irqsave(&npinfo->rx_lock, flags);
500 list_for_each_entry_safe(np, tmp, &npinfo->rx_np, rx) {
501 if (np->dev == skb->dev)
502 hits++;
503 }
504 spin_unlock_irqrestore(&npinfo->rx_lock, flags);
505
506 /* No netpoll struct is using this dev */
507 if (!hits)
Jeff Moyer115c1d62005-06-22 22:05:31 -0700508 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
Cong Wangb7394d22013-01-07 20:52:39 +0000510 proto = ntohs(eth_hdr(skb)->h_proto);
511 if (proto == ETH_P_IP) {
Cong Wangb3d936f2013-01-07 20:52:41 +0000512 struct arphdr *arp;
513 unsigned char *arp_ptr;
Cong Wangb7394d22013-01-07 20:52:39 +0000514 /* No arp on this interface */
515 if (skb->dev->flags & IFF_NOARP)
516 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
Cong Wangb7394d22013-01-07 20:52:39 +0000518 if (!pskb_may_pull(skb, arp_hdr_len(skb->dev)))
519 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
Cong Wangb7394d22013-01-07 20:52:39 +0000521 skb_reset_network_header(skb);
522 skb_reset_transport_header(skb);
523 arp = arp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
Cong Wangb7394d22013-01-07 20:52:39 +0000525 if ((arp->ar_hrd != htons(ARPHRD_ETHER) &&
526 arp->ar_hrd != htons(ARPHRD_IEEE802)) ||
527 arp->ar_pro != htons(ETH_P_IP) ||
528 arp->ar_op != htons(ARPOP_REQUEST))
529 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530
Cong Wangb7394d22013-01-07 20:52:39 +0000531 arp_ptr = (unsigned char *)(arp+1);
532 /* save the location of the src hw addr */
533 sha = arp_ptr;
534 arp_ptr += skb->dev->addr_len;
535 memcpy(&sip, arp_ptr, 4);
Daniel Borkmann508e14b2010-01-12 14:27:30 +0000536 arp_ptr += 4;
Cong Wangb7394d22013-01-07 20:52:39 +0000537 /* If we actually cared about dst hw addr,
538 it would get copied here */
539 arp_ptr += skb->dev->addr_len;
540 memcpy(&tip, arp_ptr, 4);
Daniel Borkmann508e14b2010-01-12 14:27:30 +0000541
Cong Wangb7394d22013-01-07 20:52:39 +0000542 /* Should we ignore arp? */
543 if (ipv4_is_loopback(tip) || ipv4_is_multicast(tip))
544 return;
Daniel Borkmann508e14b2010-01-12 14:27:30 +0000545
Cong Wangb7394d22013-01-07 20:52:39 +0000546 size = arp_hdr_len(skb->dev);
547
548 spin_lock_irqsave(&npinfo->rx_lock, flags);
549 list_for_each_entry_safe(np, tmp, &npinfo->rx_np, rx) {
550 if (tip != np->local_ip.ip)
551 continue;
552
553 hlen = LL_RESERVED_SPACE(np->dev);
554 tlen = np->dev->needed_tailroom;
555 send_skb = find_skb(np, size + hlen + tlen, hlen);
556 if (!send_skb)
557 continue;
558
559 skb_reset_network_header(send_skb);
560 arp = (struct arphdr *) skb_put(send_skb, size);
561 send_skb->dev = skb->dev;
562 send_skb->protocol = htons(ETH_P_ARP);
563
564 /* Fill the device header for the ARP frame */
Cong Wangb3d936f2013-01-07 20:52:41 +0000565 if (dev_hard_header(send_skb, skb->dev, ETH_P_ARP,
Cong Wangb7394d22013-01-07 20:52:39 +0000566 sha, np->dev->dev_addr,
567 send_skb->len) < 0) {
568 kfree_skb(send_skb);
569 continue;
570 }
571
572 /*
573 * Fill out the arp protocol part.
574 *
575 * we only support ethernet device type,
576 * which (according to RFC 1390) should
577 * always equal 1 (Ethernet).
578 */
579
580 arp->ar_hrd = htons(np->dev->type);
581 arp->ar_pro = htons(ETH_P_IP);
582 arp->ar_hln = np->dev->addr_len;
583 arp->ar_pln = 4;
584 arp->ar_op = htons(type);
585
586 arp_ptr = (unsigned char *)(arp + 1);
587 memcpy(arp_ptr, np->dev->dev_addr, np->dev->addr_len);
588 arp_ptr += np->dev->addr_len;
589 memcpy(arp_ptr, &tip, 4);
590 arp_ptr += 4;
591 memcpy(arp_ptr, sha, np->dev->addr_len);
592 arp_ptr += np->dev->addr_len;
593 memcpy(arp_ptr, &sip, 4);
594
595 netpoll_send_skb(np, send_skb);
596
597 /* If there are several rx_hooks for the same address,
598 we're fine by sending a single reply */
599 break;
600 }
601 spin_unlock_irqrestore(&npinfo->rx_lock, flags);
Cong Wangb3d936f2013-01-07 20:52:41 +0000602 } else if( proto == ETH_P_IPV6) {
603#if IS_ENABLED(CONFIG_IPV6)
604 struct nd_msg *msg;
605 u8 *lladdr = NULL;
606 struct ipv6hdr *hdr;
607 struct icmp6hdr *icmp6h;
608 const struct in6_addr *saddr;
609 const struct in6_addr *daddr;
610 struct inet6_dev *in6_dev = NULL;
611 struct in6_addr *target;
612
613 in6_dev = in6_dev_get(skb->dev);
614 if (!in6_dev || !in6_dev->cnf.accept_ra)
615 return;
616
617 if (!pskb_may_pull(skb, skb->len))
618 return;
619
620 msg = (struct nd_msg *)skb_transport_header(skb);
621
622 __skb_push(skb, skb->data - skb_transport_header(skb));
623
624 if (ipv6_hdr(skb)->hop_limit != 255)
625 return;
626 if (msg->icmph.icmp6_code != 0)
627 return;
628 if (msg->icmph.icmp6_type != NDISC_NEIGHBOUR_SOLICITATION)
629 return;
630
631 saddr = &ipv6_hdr(skb)->saddr;
632 daddr = &ipv6_hdr(skb)->daddr;
633
634 size = sizeof(struct icmp6hdr) + sizeof(struct in6_addr);
635
636 spin_lock_irqsave(&npinfo->rx_lock, flags);
637 list_for_each_entry_safe(np, tmp, &npinfo->rx_np, rx) {
638 if (memcmp(daddr, &np->local_ip, sizeof(*daddr)))
639 continue;
640
641 hlen = LL_RESERVED_SPACE(np->dev);
642 tlen = np->dev->needed_tailroom;
643 send_skb = find_skb(np, size + hlen + tlen, hlen);
644 if (!send_skb)
645 continue;
646
647 send_skb->protocol = htons(ETH_P_IPV6);
648 send_skb->dev = skb->dev;
649
650 skb_reset_network_header(send_skb);
651 skb_put(send_skb, sizeof(struct ipv6hdr));
652 hdr = ipv6_hdr(send_skb);
653
654 *(__be32*)hdr = htonl(0x60000000);
655
656 hdr->payload_len = htons(size);
657 hdr->nexthdr = IPPROTO_ICMPV6;
658 hdr->hop_limit = 255;
659 hdr->saddr = *saddr;
660 hdr->daddr = *daddr;
661
662 send_skb->transport_header = send_skb->tail;
663 skb_put(send_skb, size);
664
665 icmp6h = (struct icmp6hdr *)skb_transport_header(skb);
666 icmp6h->icmp6_type = NDISC_NEIGHBOUR_ADVERTISEMENT;
667 icmp6h->icmp6_router = 0;
668 icmp6h->icmp6_solicited = 1;
669 target = (struct in6_addr *)skb_transport_header(send_skb) + sizeof(struct icmp6hdr);
670 *target = msg->target;
671 icmp6h->icmp6_cksum = csum_ipv6_magic(saddr, daddr, size,
672 IPPROTO_ICMPV6,
673 csum_partial(icmp6h,
674 size, 0));
675
676 if (dev_hard_header(send_skb, skb->dev, ETH_P_IPV6,
677 lladdr, np->dev->dev_addr,
678 send_skb->len) < 0) {
679 kfree_skb(send_skb);
680 continue;
681 }
682
683 netpoll_send_skb(np, send_skb);
684
685 /* If there are several rx_hooks for the same address,
686 we're fine by sending a single reply */
687 break;
688 }
689 spin_unlock_irqrestore(&npinfo->rx_lock, flags);
690#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692}
693
Cong Wangb3d936f2013-01-07 20:52:41 +0000694static bool pkt_is_ns(struct sk_buff *skb)
695{
696 struct nd_msg *msg;
697 struct ipv6hdr *hdr;
698
699 if (skb->protocol != htons(ETH_P_ARP))
700 return false;
701 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr) + sizeof(struct nd_msg)))
702 return false;
703
704 msg = (struct nd_msg *)skb_transport_header(skb);
705 __skb_push(skb, skb->data - skb_transport_header(skb));
706 hdr = ipv6_hdr(skb);
707
708 if (hdr->nexthdr != IPPROTO_ICMPV6)
709 return false;
710 if (hdr->hop_limit != 255)
711 return false;
712 if (msg->icmph.icmp6_code != 0)
713 return false;
714 if (msg->icmph.icmp6_type != NDISC_NEIGHBOUR_SOLICITATION)
715 return false;
716
717 return true;
718}
719
Amerigo Wang57c5d462012-08-10 01:24:40 +0000720int __netpoll_rx(struct sk_buff *skb, struct netpoll_info *npinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721{
722 int proto, len, ulen;
Daniel Borkmann508e14b2010-01-12 14:27:30 +0000723 int hits = 0;
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000724 const struct iphdr *iph;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 struct udphdr *uh;
Daniel Borkmann508e14b2010-01-12 14:27:30 +0000726 struct netpoll *np, *tmp;
Neil Horman068c6e92006-06-26 00:04:27 -0700727
Daniel Borkmann508e14b2010-01-12 14:27:30 +0000728 if (list_empty(&npinfo->rx_np))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 goto out;
Daniel Borkmann508e14b2010-01-12 14:27:30 +0000730
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 if (skb->dev->type != ARPHRD_ETHER)
732 goto out;
733
David S. Millerd9452e92008-03-04 12:28:49 -0800734 /* check if netpoll clients need ARP */
Cong Wangb3d936f2013-01-07 20:52:41 +0000735 if (skb->protocol == htons(ETH_P_ARP) && atomic_read(&trapped)) {
736 skb_queue_tail(&npinfo->neigh_tx, skb);
737 return 1;
738 } else if (pkt_is_ns(skb) && atomic_read(&trapped)) {
Cong Wangb7394d22013-01-07 20:52:39 +0000739 skb_queue_tail(&npinfo->neigh_tx, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 return 1;
741 }
742
Amerigo Wang689971b2012-08-10 01:24:49 +0000743 if (skb->protocol == cpu_to_be16(ETH_P_8021Q)) {
744 skb = vlan_untag(skb);
745 if (unlikely(!skb))
746 goto out;
747 }
748
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 proto = ntohs(eth_hdr(skb)->h_proto);
Cong Wangb7394d22013-01-07 20:52:39 +0000750 if (proto != ETH_P_IP && proto != ETH_P_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 goto out;
752 if (skb->pkt_type == PACKET_OTHERHOST)
753 goto out;
754 if (skb_shared(skb))
755 goto out;
756
Cong Wangb7394d22013-01-07 20:52:39 +0000757 if (proto == ETH_P_IP) {
758 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
759 goto out;
760 iph = (struct iphdr *)skb->data;
761 if (iph->ihl < 5 || iph->version != 4)
762 goto out;
763 if (!pskb_may_pull(skb, iph->ihl*4))
764 goto out;
765 iph = (struct iphdr *)skb->data;
766 if (ip_fast_csum((u8 *)iph, iph->ihl) != 0)
767 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768
Cong Wangb7394d22013-01-07 20:52:39 +0000769 len = ntohs(iph->tot_len);
770 if (skb->len < len || len < iph->ihl*4)
771 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772
Cong Wangb7394d22013-01-07 20:52:39 +0000773 /*
774 * Our transport medium may have padded the buffer out.
775 * Now We trim to the true length of the frame.
776 */
777 if (pskb_trim_rcsum(skb, len))
778 goto out;
Aubrey.Li5e7d7fa2007-04-17 12:40:20 -0700779
Cong Wangb7394d22013-01-07 20:52:39 +0000780 iph = (struct iphdr *)skb->data;
781 if (iph->protocol != IPPROTO_UDP)
782 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783
Cong Wangb7394d22013-01-07 20:52:39 +0000784 len -= iph->ihl*4;
785 uh = (struct udphdr *)(((char *)iph) + iph->ihl*4);
786 ulen = ntohs(uh->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787
Cong Wangb7394d22013-01-07 20:52:39 +0000788 if (ulen != len)
789 goto out;
790 if (checksum_udp(skb, uh, ulen, iph->saddr, iph->daddr))
791 goto out;
792 list_for_each_entry_safe(np, tmp, &npinfo->rx_np, rx) {
793 if (np->local_ip.ip && np->local_ip.ip != iph->daddr)
794 continue;
795 if (np->remote_ip.ip && np->remote_ip.ip != iph->saddr)
796 continue;
797 if (np->local_port && np->local_port != ntohs(uh->dest))
798 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799
Cong Wangb7394d22013-01-07 20:52:39 +0000800 np->rx_hook(np, ntohs(uh->source),
801 (char *)(uh+1),
802 ulen - sizeof(struct udphdr));
803 hits++;
804 }
Cong Wangb3d936f2013-01-07 20:52:41 +0000805 } else {
806#if IS_ENABLED(CONFIG_IPV6)
807 const struct ipv6hdr *ip6h;
808
809 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
810 goto out;
811 ip6h = (struct ipv6hdr *)skb->data;
812 if (ip6h->version != 6)
813 goto out;
814 len = ntohs(ip6h->payload_len);
815 if (!len)
816 goto out;
817 if (len + sizeof(struct ipv6hdr) > skb->len)
818 goto out;
819 if (pskb_trim_rcsum(skb, len + sizeof(struct ipv6hdr)))
820 goto out;
821 ip6h = ipv6_hdr(skb);
822 if (!pskb_may_pull(skb, sizeof(struct udphdr)))
823 goto out;
824 uh = udp_hdr(skb);
825 ulen = ntohs(uh->len);
826 if (ulen != skb->len)
827 goto out;
828 if (udp6_csum_init(skb, uh, IPPROTO_UDP))
829 goto out;
830 list_for_each_entry_safe(np, tmp, &npinfo->rx_np, rx) {
831 if (memcmp(&np->local_ip.in6, &ip6h->daddr, sizeof(struct in6_addr)) != 0)
832 continue;
833 if (memcmp(&np->remote_ip.in6, &ip6h->saddr, sizeof(struct in6_addr)) != 0)
834 continue;
835 if (np->local_port && np->local_port != ntohs(uh->dest))
836 continue;
837
838 np->rx_hook(np, ntohs(uh->source),
839 (char *)(uh+1),
840 ulen - sizeof(struct udphdr));
841 hits++;
842 }
843#endif
Daniel Borkmann508e14b2010-01-12 14:27:30 +0000844 }
845
846 if (!hits)
847 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
849 kfree_skb(skb);
850 return 1;
851
852out:
853 if (atomic_read(&trapped)) {
854 kfree_skb(skb);
855 return 1;
856 }
857
858 return 0;
859}
860
Satyam Sharma0bcc1812007-08-10 15:35:05 -0700861void netpoll_print_options(struct netpoll *np)
862{
Joe Perchese6ec269352012-01-29 15:50:43 +0000863 np_info(np, "local port %d\n", np->local_port);
Cong Wangb3d936f2013-01-07 20:52:41 +0000864 if (np->ipv6)
865 np_info(np, "local IPv6 address %pI6c\n", &np->local_ip.in6);
866 else
Cong Wangb7394d22013-01-07 20:52:39 +0000867 np_info(np, "local IPv4 address %pI4\n", &np->local_ip.ip);
Joe Perchese6ec269352012-01-29 15:50:43 +0000868 np_info(np, "interface '%s'\n", np->dev_name);
869 np_info(np, "remote port %d\n", np->remote_port);
Cong Wangb3d936f2013-01-07 20:52:41 +0000870 if (np->ipv6)
871 np_info(np, "remote IPv6 address %pI6c\n", &np->remote_ip.in6);
872 else
Cong Wangb7394d22013-01-07 20:52:39 +0000873 np_info(np, "remote IPv4 address %pI4\n", &np->remote_ip.ip);
Joe Perchese6ec269352012-01-29 15:50:43 +0000874 np_info(np, "remote ethernet address %pM\n", np->remote_mac);
Satyam Sharma0bcc1812007-08-10 15:35:05 -0700875}
Eric Dumazet9e34a5b2010-07-09 21:22:04 +0000876EXPORT_SYMBOL(netpoll_print_options);
Satyam Sharma0bcc1812007-08-10 15:35:05 -0700877
Cong Wangb7394d22013-01-07 20:52:39 +0000878static int netpoll_parse_ip_addr(const char *str, union inet_addr *addr)
879{
880 const char *end;
881
882 if (!strchr(str, ':') &&
883 in4_pton(str, -1, (void *)addr, -1, &end) > 0) {
884 if (!*end)
885 return 0;
886 }
887 if (in6_pton(str, -1, addr->in6.s6_addr, -1, &end) > 0) {
888#if IS_ENABLED(CONFIG_IPV6)
889 if (!*end)
890 return 1;
891#else
892 return -1;
893#endif
894 }
895 return -1;
896}
897
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898int netpoll_parse_options(struct netpoll *np, char *opt)
899{
900 char *cur=opt, *delim;
Cong Wangb7394d22013-01-07 20:52:39 +0000901 int ipv6;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902
David S. Millerc68b9072006-11-14 20:40:49 -0800903 if (*cur != '@') {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 if ((delim = strchr(cur, '@')) == NULL)
905 goto parse_failed;
David S. Millerc68b9072006-11-14 20:40:49 -0800906 *delim = 0;
Abhijit Pawar4b5511e2012-12-09 23:12:28 +0000907 if (kstrtou16(cur, 10, &np->local_port))
908 goto parse_failed;
David S. Millerc68b9072006-11-14 20:40:49 -0800909 cur = delim;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 }
911 cur++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912
David S. Millerc68b9072006-11-14 20:40:49 -0800913 if (*cur != '/') {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 if ((delim = strchr(cur, '/')) == NULL)
915 goto parse_failed;
David S. Millerc68b9072006-11-14 20:40:49 -0800916 *delim = 0;
Cong Wangb7394d22013-01-07 20:52:39 +0000917 ipv6 = netpoll_parse_ip_addr(cur, &np->local_ip);
918 if (ipv6 < 0)
919 goto parse_failed;
920 else
921 np->ipv6 = (bool)ipv6;
David S. Millerc68b9072006-11-14 20:40:49 -0800922 cur = delim;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 }
924 cur++;
925
David S. Millerc68b9072006-11-14 20:40:49 -0800926 if (*cur != ',') {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 /* parse out dev name */
928 if ((delim = strchr(cur, ',')) == NULL)
929 goto parse_failed;
David S. Millerc68b9072006-11-14 20:40:49 -0800930 *delim = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 strlcpy(np->dev_name, cur, sizeof(np->dev_name));
David S. Millerc68b9072006-11-14 20:40:49 -0800932 cur = delim;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 }
934 cur++;
935
David S. Millerc68b9072006-11-14 20:40:49 -0800936 if (*cur != '@') {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 /* dst port */
938 if ((delim = strchr(cur, '@')) == NULL)
939 goto parse_failed;
David S. Millerc68b9072006-11-14 20:40:49 -0800940 *delim = 0;
Amerigo Wang5fc05f82010-03-21 22:59:58 +0000941 if (*cur == ' ' || *cur == '\t')
Joe Perchese6ec269352012-01-29 15:50:43 +0000942 np_info(np, "warning: whitespace is not allowed\n");
Abhijit Pawar4b5511e2012-12-09 23:12:28 +0000943 if (kstrtou16(cur, 10, &np->remote_port))
944 goto parse_failed;
David S. Millerc68b9072006-11-14 20:40:49 -0800945 cur = delim;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 }
947 cur++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948
949 /* dst ip */
950 if ((delim = strchr(cur, '/')) == NULL)
951 goto parse_failed;
David S. Millerc68b9072006-11-14 20:40:49 -0800952 *delim = 0;
Cong Wangb7394d22013-01-07 20:52:39 +0000953 ipv6 = netpoll_parse_ip_addr(cur, &np->remote_ip);
954 if (ipv6 < 0)
955 goto parse_failed;
956 else if (np->ipv6 != (bool)ipv6)
957 goto parse_failed;
958 else
959 np->ipv6 = (bool)ipv6;
David S. Millerc68b9072006-11-14 20:40:49 -0800960 cur = delim + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961
David S. Millerc68b9072006-11-14 20:40:49 -0800962 if (*cur != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 /* MAC address */
Alexey Dobriyan4940fc82011-05-07 23:00:07 +0000964 if (!mac_pton(cur, np->remote_mac))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 goto parse_failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 }
967
Satyam Sharma0bcc1812007-08-10 15:35:05 -0700968 netpoll_print_options(np);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969
970 return 0;
971
972 parse_failed:
Joe Perchese6ec269352012-01-29 15:50:43 +0000973 np_info(np, "couldn't parse config at '%s'!\n", cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 return -1;
975}
Eric Dumazet9e34a5b2010-07-09 21:22:04 +0000976EXPORT_SYMBOL(netpoll_parse_options);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977
Amerigo Wang47be03a22012-08-10 01:24:37 +0000978int __netpoll_setup(struct netpoll *np, struct net_device *ndev, gfp_t gfp)
Herbert Xu8fdd95e2010-06-10 16:12:48 +0000979{
Herbert Xu8fdd95e2010-06-10 16:12:48 +0000980 struct netpoll_info *npinfo;
981 const struct net_device_ops *ops;
982 unsigned long flags;
983 int err;
984
Jiri Pirko30fdd8a02012-07-17 05:22:35 +0000985 np->dev = ndev;
986 strlcpy(np->dev_name, ndev->name, IFNAMSIZ);
987
Herbert Xu8fdd95e2010-06-10 16:12:48 +0000988 if ((ndev->priv_flags & IFF_DISABLE_NETPOLL) ||
989 !ndev->netdev_ops->ndo_poll_controller) {
Joe Perchese6ec269352012-01-29 15:50:43 +0000990 np_err(np, "%s doesn't support polling, aborting\n",
991 np->dev_name);
Herbert Xu8fdd95e2010-06-10 16:12:48 +0000992 err = -ENOTSUPP;
993 goto out;
994 }
995
996 if (!ndev->npinfo) {
Amerigo Wang47be03a22012-08-10 01:24:37 +0000997 npinfo = kmalloc(sizeof(*npinfo), gfp);
Herbert Xu8fdd95e2010-06-10 16:12:48 +0000998 if (!npinfo) {
999 err = -ENOMEM;
1000 goto out;
1001 }
1002
1003 npinfo->rx_flags = 0;
1004 INIT_LIST_HEAD(&npinfo->rx_np);
1005
1006 spin_lock_init(&npinfo->rx_lock);
Cong Wangb7394d22013-01-07 20:52:39 +00001007 skb_queue_head_init(&npinfo->neigh_tx);
Herbert Xu8fdd95e2010-06-10 16:12:48 +00001008 skb_queue_head_init(&npinfo->txq);
1009 INIT_DELAYED_WORK(&npinfo->tx_work, queue_process);
1010
1011 atomic_set(&npinfo->refcnt, 1);
1012
1013 ops = np->dev->netdev_ops;
1014 if (ops->ndo_netpoll_setup) {
Amerigo Wang47be03a22012-08-10 01:24:37 +00001015 err = ops->ndo_netpoll_setup(ndev, npinfo, gfp);
Herbert Xu8fdd95e2010-06-10 16:12:48 +00001016 if (err)
1017 goto free_npinfo;
1018 }
1019 } else {
1020 npinfo = ndev->npinfo;
1021 atomic_inc(&npinfo->refcnt);
1022 }
1023
1024 npinfo->netpoll = np;
1025
1026 if (np->rx_hook) {
1027 spin_lock_irqsave(&npinfo->rx_lock, flags);
1028 npinfo->rx_flags |= NETPOLL_RX_ENABLED;
1029 list_add_tail(&np->rx, &npinfo->rx_np);
1030 spin_unlock_irqrestore(&npinfo->rx_lock, flags);
1031 }
1032
1033 /* last thing to do is link it to the net device structure */
Eric Dumazetcf778b02012-01-12 04:41:32 +00001034 rcu_assign_pointer(ndev->npinfo, npinfo);
Herbert Xu8fdd95e2010-06-10 16:12:48 +00001035
1036 return 0;
1037
1038free_npinfo:
1039 kfree(npinfo);
1040out:
1041 return err;
1042}
1043EXPORT_SYMBOL_GPL(__netpoll_setup);
1044
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045int netpoll_setup(struct netpoll *np)
1046{
1047 struct net_device *ndev = NULL;
1048 struct in_device *in_dev;
Stephen Hemmingerb41848b2006-10-26 15:46:52 -07001049 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050
Cong Wangf92d3182013-01-14 23:34:06 +00001051 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 if (np->dev_name)
Cong Wangf92d3182013-01-14 23:34:06 +00001053 ndev = __dev_get_by_name(&init_net, np->dev_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 if (!ndev) {
Joe Perchese6ec269352012-01-29 15:50:43 +00001055 np_err(np, "%s doesn't exist, aborting\n", np->dev_name);
Cong Wangf92d3182013-01-14 23:34:06 +00001056 err = -ENODEV;
1057 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 }
1059
Jiri Pirko49bd8fb2013-01-03 22:48:55 +00001060 if (netdev_master_upper_dev_get(ndev)) {
Joe Perchese6ec269352012-01-29 15:50:43 +00001061 np_err(np, "%s is a slave device, aborting\n", np->dev_name);
Dan Carpenter83fe32d2011-06-11 18:55:22 -07001062 err = -EBUSY;
1063 goto put;
WANG Cong0c1ad042011-06-09 00:28:13 -07001064 }
1065
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 if (!netif_running(ndev)) {
1067 unsigned long atmost, atleast;
1068
Joe Perchese6ec269352012-01-29 15:50:43 +00001069 np_info(np, "device %s not up yet, forcing it\n", np->dev_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070
Stephen Hemmingerb41848b2006-10-26 15:46:52 -07001071 err = dev_open(ndev);
Stephen Hemmingerb41848b2006-10-26 15:46:52 -07001072
1073 if (err) {
Joe Perchese6ec269352012-01-29 15:50:43 +00001074 np_err(np, "failed to open %s\n", ndev->name);
Herbert Xudbaa1542010-06-10 16:12:46 +00001075 goto put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077
Cong Wangf92d3182013-01-14 23:34:06 +00001078 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 atleast = jiffies + HZ/10;
Anton Vorontsovbff38772009-07-08 11:10:56 -07001080 atmost = jiffies + carrier_timeout * HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 while (!netif_carrier_ok(ndev)) {
1082 if (time_after(jiffies, atmost)) {
Joe Perchese6ec269352012-01-29 15:50:43 +00001083 np_notice(np, "timeout waiting for carrier\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 break;
1085 }
Anton Vorontsov1b614fb2009-07-08 20:09:44 -07001086 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 }
1088
1089 /* If carrier appears to come up instantly, we don't
1090 * trust it and pause so that we don't pump all our
1091 * queued console messages into the bitbucket.
1092 */
1093
1094 if (time_before(jiffies, atleast)) {
Joe Perchese6ec269352012-01-29 15:50:43 +00001095 np_notice(np, "carrier detect appears untrustworthy, waiting 4 seconds\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 msleep(4000);
1097 }
Cong Wangf92d3182013-01-14 23:34:06 +00001098 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 }
1100
Cong Wangb7394d22013-01-07 20:52:39 +00001101 if (!np->local_ip.ip) {
1102 if (!np->ipv6) {
Cong Wangf92d3182013-01-14 23:34:06 +00001103 in_dev = __in_dev_get_rtnl(ndev);
Cong Wangb7394d22013-01-07 20:52:39 +00001104
1105 if (!in_dev || !in_dev->ifa_list) {
Cong Wangb7394d22013-01-07 20:52:39 +00001106 np_err(np, "no IP address for %s, aborting\n",
1107 np->dev_name);
1108 err = -EDESTADDRREQ;
1109 goto put;
1110 }
1111
1112 np->local_ip.ip = in_dev->ifa_list->ifa_local;
Cong Wangb7394d22013-01-07 20:52:39 +00001113 np_info(np, "local IP %pI4\n", &np->local_ip.ip);
Cong Wangb3d936f2013-01-07 20:52:41 +00001114 } else {
1115#if IS_ENABLED(CONFIG_IPV6)
1116 struct inet6_dev *idev;
1117
1118 err = -EDESTADDRREQ;
Cong Wangb3d936f2013-01-07 20:52:41 +00001119 idev = __in6_dev_get(ndev);
1120 if (idev) {
1121 struct inet6_ifaddr *ifp;
1122
1123 read_lock_bh(&idev->lock);
1124 list_for_each_entry(ifp, &idev->addr_list, if_list) {
1125 if (ipv6_addr_type(&ifp->addr) & IPV6_ADDR_LINKLOCAL)
1126 continue;
1127 np->local_ip.in6 = ifp->addr;
1128 err = 0;
1129 break;
1130 }
1131 read_unlock_bh(&idev->lock);
1132 }
Cong Wangb3d936f2013-01-07 20:52:41 +00001133 if (err) {
1134 np_err(np, "no IPv6 address for %s, aborting\n",
1135 np->dev_name);
1136 goto put;
1137 } else
1138 np_info(np, "local IPv6 %pI6c\n", &np->local_ip.in6);
1139#else
1140 np_err(np, "IPv6 is not supported %s, aborting\n",
1141 np->dev_name);
1142 goto put;
1143#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 }
1146
Herbert Xudbaa1542010-06-10 16:12:46 +00001147 /* fill up the skb queue */
1148 refill_skbs();
1149
Amerigo Wang47be03a22012-08-10 01:24:37 +00001150 err = __netpoll_setup(np, ndev, GFP_KERNEL);
Herbert Xu8fdd95e2010-06-10 16:12:48 +00001151 if (err)
1152 goto put;
1153
Cong Wangf92d3182013-01-14 23:34:06 +00001154 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 return 0;
1156
Jiri Slaby21edbb22010-03-16 05:29:54 +00001157put:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 dev_put(ndev);
Cong Wangf92d3182013-01-14 23:34:06 +00001159unlock:
1160 rtnl_unlock();
Stephen Hemmingerb41848b2006-10-26 15:46:52 -07001161 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162}
Eric Dumazet9e34a5b2010-07-09 21:22:04 +00001163EXPORT_SYMBOL(netpoll_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164
David S. Millerc68b9072006-11-14 20:40:49 -08001165static int __init netpoll_init(void)
1166{
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -08001167 skb_queue_head_init(&skb_pool);
1168 return 0;
1169}
1170core_initcall(netpoll_init);
1171
Amerigo Wang38e6bc12012-08-10 01:24:38 +00001172static void rcu_cleanup_netpoll_info(struct rcu_head *rcu_head)
1173{
1174 struct netpoll_info *npinfo =
1175 container_of(rcu_head, struct netpoll_info, rcu);
1176
Cong Wangb7394d22013-01-07 20:52:39 +00001177 skb_queue_purge(&npinfo->neigh_tx);
Amerigo Wang38e6bc12012-08-10 01:24:38 +00001178 skb_queue_purge(&npinfo->txq);
1179
1180 /* we can't call cancel_delayed_work_sync here, as we are in softirq */
1181 cancel_delayed_work(&npinfo->tx_work);
1182
1183 /* clean after last, unfinished work */
1184 __skb_queue_purge(&npinfo->txq);
1185 /* now cancel it again */
1186 cancel_delayed_work(&npinfo->tx_work);
1187 kfree(npinfo);
1188}
1189
Herbert Xu8fdd95e2010-06-10 16:12:48 +00001190void __netpoll_cleanup(struct netpoll *np)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191{
Jeff Moyerfbeec2e2005-06-22 22:05:59 -07001192 struct netpoll_info *npinfo;
1193 unsigned long flags;
1194
Herbert Xu8fdd95e2010-06-10 16:12:48 +00001195 npinfo = np->dev->npinfo;
1196 if (!npinfo)
Herbert Xudbaa1542010-06-10 16:12:46 +00001197 return;
Stephen Hemminger93ec2c72006-10-26 15:46:50 -07001198
Herbert Xu8fdd95e2010-06-10 16:12:48 +00001199 if (!list_empty(&npinfo->rx_np)) {
1200 spin_lock_irqsave(&npinfo->rx_lock, flags);
1201 list_del(&np->rx);
1202 if (list_empty(&npinfo->rx_np))
1203 npinfo->rx_flags &= ~NETPOLL_RX_ENABLED;
1204 spin_unlock_irqrestore(&npinfo->rx_lock, flags);
Jeff Moyer115c1d62005-06-22 22:05:31 -07001205 }
Herbert Xudbaa1542010-06-10 16:12:46 +00001206
Herbert Xu8fdd95e2010-06-10 16:12:48 +00001207 if (atomic_dec_and_test(&npinfo->refcnt)) {
1208 const struct net_device_ops *ops;
1209
1210 ops = np->dev->netdev_ops;
1211 if (ops->ndo_netpoll_cleanup)
1212 ops->ndo_netpoll_cleanup(np->dev);
1213
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00001214 RCU_INIT_POINTER(np->dev->npinfo, NULL);
Amerigo Wang38e6bc12012-08-10 01:24:38 +00001215 call_rcu_bh(&npinfo->rcu, rcu_cleanup_netpoll_info);
Herbert Xudbaa1542010-06-10 16:12:46 +00001216 }
Herbert Xu8fdd95e2010-06-10 16:12:48 +00001217}
1218EXPORT_SYMBOL_GPL(__netpoll_cleanup);
1219
Amerigo Wang38e6bc12012-08-10 01:24:38 +00001220static void rcu_cleanup_netpoll(struct rcu_head *rcu_head)
1221{
1222 struct netpoll *np = container_of(rcu_head, struct netpoll, rcu);
1223
1224 __netpoll_cleanup(np);
1225 kfree(np);
1226}
1227
1228void __netpoll_free_rcu(struct netpoll *np)
1229{
1230 call_rcu_bh(&np->rcu, rcu_cleanup_netpoll);
1231}
1232EXPORT_SYMBOL_GPL(__netpoll_free_rcu);
1233
Herbert Xu8fdd95e2010-06-10 16:12:48 +00001234void netpoll_cleanup(struct netpoll *np)
1235{
1236 if (!np->dev)
1237 return;
1238
1239 rtnl_lock();
1240 __netpoll_cleanup(np);
1241 rtnl_unlock();
Herbert Xudbaa1542010-06-10 16:12:46 +00001242
1243 dev_put(np->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 np->dev = NULL;
1245}
Eric Dumazet9e34a5b2010-07-09 21:22:04 +00001246EXPORT_SYMBOL(netpoll_cleanup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247
1248int netpoll_trap(void)
1249{
1250 return atomic_read(&trapped);
1251}
Eric Dumazet9e34a5b2010-07-09 21:22:04 +00001252EXPORT_SYMBOL(netpoll_trap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253
1254void netpoll_set_trap(int trap)
1255{
1256 if (trap)
1257 atomic_inc(&trapped);
1258 else
1259 atomic_dec(&trapped);
1260}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261EXPORT_SYMBOL(netpoll_set_trap);