]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
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 | ||
e6ec2693 JP |
12 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
13 | ||
bff38771 | 14 | #include <linux/moduleparam.h> |
1da177e4 LT |
15 | #include <linux/netdevice.h> |
16 | #include <linux/etherdevice.h> | |
17 | #include <linux/string.h> | |
14c85021 | 18 | #include <linux/if_arp.h> |
1da177e4 LT |
19 | #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> | |
5a0e3ad6 | 27 | #include <linux/slab.h> |
bc3b2d7f | 28 | #include <linux/export.h> |
689971b4 | 29 | #include <linux/if_vlan.h> |
1da177e4 LT |
30 | #include <net/tcp.h> |
31 | #include <net/udp.h> | |
b3d936f3 CW |
32 | #include <net/addrconf.h> |
33 | #include <net/ndisc.h> | |
34 | #include <net/ip6_checksum.h> | |
1da177e4 | 35 | #include <asm/unaligned.h> |
9cbc1cb8 | 36 | #include <trace/events/napi.h> |
1da177e4 LT |
37 | |
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 | |
1da177e4 | 45 | |
a1bcfacd | 46 | static struct sk_buff_head skb_pool; |
1da177e4 LT |
47 | |
48 | static atomic_t trapped; | |
49 | ||
ca99ca14 NH |
50 | static struct srcu_struct netpoll_srcu; |
51 | ||
2bdfe0ba | 52 | #define USEC_PER_POLL 50 |
d9452e9f DM |
53 | #define NETPOLL_RX_ENABLED 1 |
54 | #define NETPOLL_RX_DROP 2 | |
1da177e4 | 55 | |
6f706245 JP |
56 | #define MAX_SKB_SIZE \ |
57 | (sizeof(struct ethhdr) + \ | |
58 | sizeof(struct iphdr) + \ | |
59 | sizeof(struct udphdr) + \ | |
60 | MAX_UDP_CHUNK) | |
1da177e4 | 61 | |
3578b0c8 | 62 | static void zap_completion_queue(void); |
b7394d24 | 63 | static void netpoll_neigh_reply(struct sk_buff *skb, struct netpoll_info *npinfo); |
1da177e4 | 64 | |
bff38771 AV |
65 | static unsigned int carrier_timeout = 4; |
66 | module_param(carrier_timeout, uint, 0644); | |
67 | ||
e6ec2693 JP |
68 | #define np_info(np, fmt, ...) \ |
69 | pr_info("%s: " fmt, np->name, ##__VA_ARGS__) | |
70 | #define np_err(np, fmt, ...) \ | |
71 | pr_err("%s: " fmt, np->name, ##__VA_ARGS__) | |
72 | #define np_notice(np, fmt, ...) \ | |
73 | pr_notice("%s: " fmt, np->name, ##__VA_ARGS__) | |
74 | ||
c4028958 | 75 | static void queue_process(struct work_struct *work) |
1da177e4 | 76 | { |
4c1ac1b4 DH |
77 | struct netpoll_info *npinfo = |
78 | container_of(work, struct netpoll_info, tx_work.work); | |
1da177e4 | 79 | struct sk_buff *skb; |
3640543d | 80 | unsigned long flags; |
1da177e4 | 81 | |
6c43ff18 SH |
82 | while ((skb = skb_dequeue(&npinfo->txq))) { |
83 | struct net_device *dev = skb->dev; | |
00829823 | 84 | const struct net_device_ops *ops = dev->netdev_ops; |
fd2ea0a7 | 85 | struct netdev_queue *txq; |
1da177e4 | 86 | |
6c43ff18 SH |
87 | if (!netif_device_present(dev) || !netif_running(dev)) { |
88 | __kfree_skb(skb); | |
89 | continue; | |
90 | } | |
1da177e4 | 91 | |
fd2ea0a7 DM |
92 | txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb)); |
93 | ||
3640543d | 94 | local_irq_save(flags); |
fd2ea0a7 | 95 | __netif_tx_lock(txq, smp_processor_id()); |
73466498 | 96 | if (netif_xmit_frozen_or_stopped(txq) || |
00829823 | 97 | ops->ndo_start_xmit(skb, dev) != NETDEV_TX_OK) { |
6c43ff18 | 98 | skb_queue_head(&npinfo->txq, skb); |
fd2ea0a7 | 99 | __netif_tx_unlock(txq); |
3640543d | 100 | local_irq_restore(flags); |
1da177e4 | 101 | |
25442caf | 102 | schedule_delayed_work(&npinfo->tx_work, HZ/10); |
6c43ff18 SH |
103 | return; |
104 | } | |
fd2ea0a7 | 105 | __netif_tx_unlock(txq); |
3640543d | 106 | local_irq_restore(flags); |
1da177e4 | 107 | } |
1da177e4 LT |
108 | } |
109 | ||
b51655b9 AV |
110 | static __sum16 checksum_udp(struct sk_buff *skb, struct udphdr *uh, |
111 | unsigned short ulen, __be32 saddr, __be32 daddr) | |
1da177e4 | 112 | { |
d6f5493c | 113 | __wsum psum; |
fb286bb2 | 114 | |
60476372 | 115 | if (uh->check == 0 || skb_csum_unnecessary(skb)) |
1da177e4 LT |
116 | return 0; |
117 | ||
fb286bb2 HX |
118 | psum = csum_tcpudp_nofold(saddr, daddr, ulen, IPPROTO_UDP, 0); |
119 | ||
84fa7933 | 120 | if (skb->ip_summed == CHECKSUM_COMPLETE && |
d3bc23e7 | 121 | !csum_fold(csum_add(psum, skb->csum))) |
fb286bb2 | 122 | return 0; |
1da177e4 | 123 | |
fb286bb2 | 124 | skb->csum = psum; |
1da177e4 | 125 | |
fb286bb2 | 126 | return __skb_checksum_complete(skb); |
1da177e4 LT |
127 | } |
128 | ||
129 | /* | |
130 | * Check whether delayed processing was scheduled for our NIC. If so, | |
131 | * we attempt to grab the poll lock and use ->poll() to pump the card. | |
132 | * If this fails, either we've recursed in ->poll() or it's already | |
133 | * running on another CPU. | |
134 | * | |
135 | * Note: we don't mask interrupts with this lock because we're using | |
136 | * trylock here and interrupts are already disabled in the softirq | |
137 | * case. Further, we test the poll_owner to avoid recursion on UP | |
138 | * systems where the lock doesn't exist. | |
139 | * | |
140 | * In cases where there is bi-directional communications, reading only | |
141 | * one message at a time can lead to packets being dropped by the | |
142 | * network adapter, forcing superfluous retries and possibly timeouts. | |
143 | * Thus, we set our budget to greater than 1. | |
144 | */ | |
0a7606c1 DM |
145 | static int poll_one_napi(struct netpoll_info *npinfo, |
146 | struct napi_struct *napi, int budget) | |
147 | { | |
148 | int work; | |
149 | ||
150 | /* net_rx_action's ->poll() invocations and our's are | |
151 | * synchronized by this test which is only made while | |
152 | * holding the napi->poll_lock. | |
153 | */ | |
154 | if (!test_bit(NAPI_STATE_SCHED, &napi->state)) | |
155 | return budget; | |
156 | ||
d9452e9f | 157 | npinfo->rx_flags |= NETPOLL_RX_DROP; |
0a7606c1 | 158 | atomic_inc(&trapped); |
7b363e44 | 159 | set_bit(NAPI_STATE_NPSVC, &napi->state); |
0a7606c1 DM |
160 | |
161 | work = napi->poll(napi, budget); | |
7d18f114 | 162 | trace_napi_poll(napi); |
0a7606c1 | 163 | |
7b363e44 | 164 | clear_bit(NAPI_STATE_NPSVC, &napi->state); |
0a7606c1 | 165 | atomic_dec(&trapped); |
d9452e9f | 166 | npinfo->rx_flags &= ~NETPOLL_RX_DROP; |
0a7606c1 DM |
167 | |
168 | return budget - work; | |
169 | } | |
170 | ||
5106930b | 171 | static void poll_napi(struct net_device *dev) |
1da177e4 | 172 | { |
bea3348e | 173 | struct napi_struct *napi; |
1da177e4 LT |
174 | int budget = 16; |
175 | ||
f13d493d | 176 | list_for_each_entry(napi, &dev->napi_list, dev_list) { |
0a7606c1 | 177 | if (napi->poll_owner != smp_processor_id() && |
bea3348e | 178 | spin_trylock(&napi->poll_lock)) { |
2899656b AW |
179 | budget = poll_one_napi(rcu_dereference_bh(dev->npinfo), |
180 | napi, budget); | |
bea3348e | 181 | spin_unlock(&napi->poll_lock); |
0a7606c1 | 182 | |
072a9c48 | 183 | if (!budget) |
0a7606c1 | 184 | break; |
bea3348e | 185 | } |
1da177e4 LT |
186 | } |
187 | } | |
188 | ||
b7394d24 | 189 | static void service_neigh_queue(struct netpoll_info *npi) |
068c6e98 | 190 | { |
5106930b SH |
191 | if (npi) { |
192 | struct sk_buff *skb; | |
068c6e98 | 193 | |
b7394d24 CW |
194 | while ((skb = skb_dequeue(&npi->neigh_tx))) |
195 | netpoll_neigh_reply(skb, npi); | |
068c6e98 | 196 | } |
068c6e98 NH |
197 | } |
198 | ||
234b921d | 199 | static void netpoll_poll_dev(struct net_device *dev) |
1da177e4 | 200 | { |
5e392739 | 201 | const struct net_device_ops *ops; |
2899656b | 202 | struct netpoll_info *ni = rcu_dereference_bh(dev->npinfo); |
5106930b | 203 | |
ca99ca14 NH |
204 | /* Don't do any rx activity if the dev_lock mutex is held |
205 | * the dev_open/close paths use this to block netpoll activity | |
206 | * while changing device state | |
207 | */ | |
208 | if (!mutex_trylock(&dev->npinfo->dev_lock)) | |
209 | return; | |
210 | ||
5e392739 PE |
211 | if (!dev || !netif_running(dev)) |
212 | return; | |
213 | ||
214 | ops = dev->netdev_ops; | |
215 | if (!ops->ndo_poll_controller) | |
1da177e4 LT |
216 | return; |
217 | ||
218 | /* Process pending work on NIC */ | |
d314774c | 219 | ops->ndo_poll_controller(dev); |
5106930b SH |
220 | |
221 | poll_napi(dev); | |
1da177e4 | 222 | |
ca99ca14 NH |
223 | mutex_unlock(&dev->npinfo->dev_lock); |
224 | ||
58e05f35 | 225 | if (dev->flags & IFF_SLAVE) { |
2899656b | 226 | if (ni) { |
49bd8fb0 | 227 | struct net_device *bond_dev; |
5a698af5 | 228 | struct sk_buff *skb; |
49bd8fb0 JP |
229 | struct netpoll_info *bond_ni; |
230 | ||
231 | bond_dev = netdev_master_upper_dev_get_rcu(dev); | |
232 | bond_ni = rcu_dereference_bh(bond_dev->npinfo); | |
b7394d24 | 233 | while ((skb = skb_dequeue(&ni->neigh_tx))) { |
5a698af5 | 234 | skb->dev = bond_dev; |
b7394d24 | 235 | skb_queue_tail(&bond_ni->neigh_tx, skb); |
5a698af5 AW |
236 | } |
237 | } | |
238 | } | |
239 | ||
b7394d24 | 240 | service_neigh_queue(ni); |
068c6e98 | 241 | |
3578b0c8 | 242 | zap_completion_queue(); |
1da177e4 LT |
243 | } |
244 | ||
ca99ca14 NH |
245 | int netpoll_rx_disable(struct net_device *dev) |
246 | { | |
247 | struct netpoll_info *ni; | |
248 | int idx; | |
249 | might_sleep(); | |
250 | idx = srcu_read_lock(&netpoll_srcu); | |
251 | ni = srcu_dereference(dev->npinfo, &netpoll_srcu); | |
252 | if (ni) | |
253 | mutex_lock(&ni->dev_lock); | |
254 | srcu_read_unlock(&netpoll_srcu, idx); | |
255 | return 0; | |
256 | } | |
257 | EXPORT_SYMBOL(netpoll_rx_disable); | |
258 | ||
259 | void netpoll_rx_enable(struct net_device *dev) | |
260 | { | |
261 | struct netpoll_info *ni; | |
262 | rcu_read_lock(); | |
263 | ni = rcu_dereference(dev->npinfo); | |
264 | if (ni) | |
265 | mutex_unlock(&ni->dev_lock); | |
266 | rcu_read_unlock(); | |
267 | } | |
268 | EXPORT_SYMBOL(netpoll_rx_enable); | |
269 | ||
1da177e4 LT |
270 | static void refill_skbs(void) |
271 | { | |
272 | struct sk_buff *skb; | |
273 | unsigned long flags; | |
274 | ||
a1bcfacd SH |
275 | spin_lock_irqsave(&skb_pool.lock, flags); |
276 | while (skb_pool.qlen < MAX_SKBS) { | |
1da177e4 LT |
277 | skb = alloc_skb(MAX_SKB_SIZE, GFP_ATOMIC); |
278 | if (!skb) | |
279 | break; | |
280 | ||
a1bcfacd | 281 | __skb_queue_tail(&skb_pool, skb); |
1da177e4 | 282 | } |
a1bcfacd | 283 | spin_unlock_irqrestore(&skb_pool.lock, flags); |
1da177e4 LT |
284 | } |
285 | ||
3578b0c8 DM |
286 | static void zap_completion_queue(void) |
287 | { | |
288 | unsigned long flags; | |
289 | struct softnet_data *sd = &get_cpu_var(softnet_data); | |
290 | ||
291 | if (sd->completion_queue) { | |
292 | struct sk_buff *clist; | |
293 | ||
294 | local_irq_save(flags); | |
295 | clist = sd->completion_queue; | |
296 | sd->completion_queue = NULL; | |
297 | local_irq_restore(flags); | |
298 | ||
299 | while (clist != NULL) { | |
300 | struct sk_buff *skb = clist; | |
301 | clist = clist->next; | |
302 | if (skb->destructor) { | |
303 | atomic_inc(&skb->users); | |
304 | dev_kfree_skb_any(skb); /* put this one back */ | |
305 | } else { | |
306 | __kfree_skb(skb); | |
307 | } | |
308 | } | |
309 | } | |
310 | ||
311 | put_cpu_var(softnet_data); | |
312 | } | |
313 | ||
a1bcfacd | 314 | static struct sk_buff *find_skb(struct netpoll *np, int len, int reserve) |
1da177e4 | 315 | { |
a1bcfacd SH |
316 | int count = 0; |
317 | struct sk_buff *skb; | |
1da177e4 | 318 | |
3578b0c8 | 319 | zap_completion_queue(); |
a1bcfacd | 320 | refill_skbs(); |
1da177e4 | 321 | repeat: |
1da177e4 LT |
322 | |
323 | skb = alloc_skb(len, GFP_ATOMIC); | |
a1bcfacd SH |
324 | if (!skb) |
325 | skb = skb_dequeue(&skb_pool); | |
1da177e4 LT |
326 | |
327 | if (!skb) { | |
a1bcfacd | 328 | if (++count < 10) { |
2a49e001 | 329 | netpoll_poll_dev(np->dev); |
a1bcfacd | 330 | goto repeat; |
1da177e4 | 331 | } |
a1bcfacd | 332 | return NULL; |
1da177e4 LT |
333 | } |
334 | ||
335 | atomic_set(&skb->users, 1); | |
336 | skb_reserve(skb, reserve); | |
337 | return skb; | |
338 | } | |
339 | ||
bea3348e SH |
340 | static int netpoll_owner_active(struct net_device *dev) |
341 | { | |
342 | struct napi_struct *napi; | |
343 | ||
344 | list_for_each_entry(napi, &dev->napi_list, dev_list) { | |
345 | if (napi->poll_owner == smp_processor_id()) | |
346 | return 1; | |
347 | } | |
348 | return 0; | |
349 | } | |
350 | ||
2899656b | 351 | /* call with IRQ disabled */ |
c2355e1a NH |
352 | void netpoll_send_skb_on_dev(struct netpoll *np, struct sk_buff *skb, |
353 | struct net_device *dev) | |
1da177e4 | 354 | { |
2bdfe0ba SH |
355 | int status = NETDEV_TX_BUSY; |
356 | unsigned long tries; | |
00829823 | 357 | const struct net_device_ops *ops = dev->netdev_ops; |
de85d99e | 358 | /* It is up to the caller to keep npinfo alive. */ |
2899656b | 359 | struct netpoll_info *npinfo; |
2bdfe0ba | 360 | |
2899656b AW |
361 | WARN_ON_ONCE(!irqs_disabled()); |
362 | ||
363 | npinfo = rcu_dereference_bh(np->dev->npinfo); | |
4ec93edb YH |
364 | if (!npinfo || !netif_running(dev) || !netif_device_present(dev)) { |
365 | __kfree_skb(skb); | |
366 | return; | |
367 | } | |
2bdfe0ba SH |
368 | |
369 | /* don't get messages out of order, and no recursion */ | |
bea3348e | 370 | if (skb_queue_len(&npinfo->txq) == 0 && !netpoll_owner_active(dev)) { |
fd2ea0a7 | 371 | struct netdev_queue *txq; |
a49f99ff | 372 | |
8c4c49df | 373 | txq = netdev_pick_tx(dev, skb); |
fd2ea0a7 | 374 | |
0db3dc73 SH |
375 | /* try until next clock tick */ |
376 | for (tries = jiffies_to_usecs(1)/USEC_PER_POLL; | |
377 | tries > 0; --tries) { | |
fd2ea0a7 | 378 | if (__netif_tx_trylock(txq)) { |
73466498 | 379 | if (!netif_xmit_stopped(txq)) { |
689971b4 AW |
380 | if (vlan_tx_tag_present(skb) && |
381 | !(netif_skb_features(skb) & NETIF_F_HW_VLAN_TX)) { | |
382 | skb = __vlan_put_tag(skb, vlan_tx_tag_get(skb)); | |
383 | if (unlikely(!skb)) | |
384 | break; | |
385 | skb->vlan_tci = 0; | |
386 | } | |
387 | ||
00829823 | 388 | status = ops->ndo_start_xmit(skb, dev); |
08baf561 ED |
389 | if (status == NETDEV_TX_OK) |
390 | txq_trans_update(txq); | |
391 | } | |
fd2ea0a7 | 392 | __netif_tx_unlock(txq); |
e37b8d93 AM |
393 | |
394 | if (status == NETDEV_TX_OK) | |
395 | break; | |
396 | ||
e37b8d93 | 397 | } |
0db3dc73 SH |
398 | |
399 | /* tickle device maybe there is some cleanup */ | |
2a49e001 | 400 | netpoll_poll_dev(np->dev); |
0db3dc73 SH |
401 | |
402 | udelay(USEC_PER_POLL); | |
0db1d6fc | 403 | } |
79b1bee8 DD |
404 | |
405 | WARN_ONCE(!irqs_disabled(), | |
2899656b | 406 | "netpoll_send_skb_on_dev(): %s enabled interrupts in poll (%pF)\n", |
79b1bee8 DD |
407 | dev->name, ops->ndo_start_xmit); |
408 | ||
1da177e4 | 409 | } |
1da177e4 | 410 | |
2bdfe0ba | 411 | if (status != NETDEV_TX_OK) { |
5de4a473 | 412 | skb_queue_tail(&npinfo->txq, skb); |
4c1ac1b4 | 413 | schedule_delayed_work(&npinfo->tx_work,0); |
1da177e4 | 414 | } |
1da177e4 | 415 | } |
c2355e1a | 416 | EXPORT_SYMBOL(netpoll_send_skb_on_dev); |
1da177e4 LT |
417 | |
418 | void netpoll_send_udp(struct netpoll *np, const char *msg, int len) | |
419 | { | |
954fba02 | 420 | int total_len, ip_len, udp_len; |
1da177e4 LT |
421 | struct sk_buff *skb; |
422 | struct udphdr *udph; | |
423 | struct iphdr *iph; | |
424 | struct ethhdr *eth; | |
ee130409 | 425 | static atomic_t ip_ident; |
b3d936f3 | 426 | struct ipv6hdr *ip6h; |
1da177e4 LT |
427 | |
428 | udp_len = len + sizeof(*udph); | |
b3d936f3 CW |
429 | if (np->ipv6) |
430 | ip_len = udp_len + sizeof(*ip6h); | |
431 | else | |
b7394d24 CW |
432 | ip_len = udp_len + sizeof(*iph); |
433 | ||
954fba02 | 434 | total_len = ip_len + LL_RESERVED_SPACE(np->dev); |
1da177e4 | 435 | |
954fba02 ED |
436 | skb = find_skb(np, total_len + np->dev->needed_tailroom, |
437 | total_len - len); | |
1da177e4 LT |
438 | if (!skb) |
439 | return; | |
440 | ||
27d7ff46 | 441 | skb_copy_to_linear_data(skb, msg, len); |
954fba02 | 442 | skb_put(skb, len); |
1da177e4 | 443 | |
4bedb452 ACM |
444 | skb_push(skb, sizeof(*udph)); |
445 | skb_reset_transport_header(skb); | |
446 | udph = udp_hdr(skb); | |
1da177e4 LT |
447 | udph->source = htons(np->local_port); |
448 | udph->dest = htons(np->remote_port); | |
449 | udph->len = htons(udp_len); | |
b7394d24 | 450 | |
b3d936f3 CW |
451 | if (np->ipv6) { |
452 | udph->check = 0; | |
453 | udph->check = csum_ipv6_magic(&np->local_ip.in6, | |
454 | &np->remote_ip.in6, | |
455 | udp_len, IPPROTO_UDP, | |
456 | csum_partial(udph, udp_len, 0)); | |
457 | if (udph->check == 0) | |
458 | udph->check = CSUM_MANGLED_0; | |
459 | ||
460 | skb_push(skb, sizeof(*ip6h)); | |
461 | skb_reset_network_header(skb); | |
462 | ip6h = ipv6_hdr(skb); | |
463 | ||
464 | /* ip6h->version = 6; ip6h->priority = 0; */ | |
465 | put_unaligned(0x60, (unsigned char *)ip6h); | |
466 | ip6h->flow_lbl[0] = 0; | |
467 | ip6h->flow_lbl[1] = 0; | |
468 | ip6h->flow_lbl[2] = 0; | |
469 | ||
470 | ip6h->payload_len = htons(sizeof(struct udphdr) + len); | |
471 | ip6h->nexthdr = IPPROTO_UDP; | |
472 | ip6h->hop_limit = 32; | |
473 | ip6h->saddr = np->local_ip.in6; | |
474 | ip6h->daddr = np->remote_ip.in6; | |
475 | ||
476 | eth = (struct ethhdr *) skb_push(skb, ETH_HLEN); | |
477 | skb_reset_mac_header(skb); | |
478 | skb->protocol = eth->h_proto = htons(ETH_P_IPV6); | |
479 | } else { | |
b7394d24 CW |
480 | udph->check = 0; |
481 | udph->check = csum_tcpudp_magic(np->local_ip.ip, | |
482 | np->remote_ip.ip, | |
483 | udp_len, IPPROTO_UDP, | |
484 | csum_partial(udph, udp_len, 0)); | |
485 | if (udph->check == 0) | |
486 | udph->check = CSUM_MANGLED_0; | |
487 | ||
488 | skb_push(skb, sizeof(*iph)); | |
489 | skb_reset_network_header(skb); | |
490 | iph = ip_hdr(skb); | |
491 | ||
492 | /* iph->version = 4; iph->ihl = 5; */ | |
493 | put_unaligned(0x45, (unsigned char *)iph); | |
494 | iph->tos = 0; | |
495 | put_unaligned(htons(ip_len), &(iph->tot_len)); | |
496 | iph->id = htons(atomic_inc_return(&ip_ident)); | |
497 | iph->frag_off = 0; | |
498 | iph->ttl = 64; | |
499 | iph->protocol = IPPROTO_UDP; | |
500 | iph->check = 0; | |
501 | put_unaligned(np->local_ip.ip, &(iph->saddr)); | |
502 | put_unaligned(np->remote_ip.ip, &(iph->daddr)); | |
503 | iph->check = ip_fast_csum((unsigned char *)iph, iph->ihl); | |
504 | ||
505 | eth = (struct ethhdr *) skb_push(skb, ETH_HLEN); | |
506 | skb_reset_mac_header(skb); | |
507 | skb->protocol = eth->h_proto = htons(ETH_P_IP); | |
508 | } | |
509 | ||
09538641 SH |
510 | memcpy(eth->h_source, np->dev->dev_addr, ETH_ALEN); |
511 | memcpy(eth->h_dest, np->remote_mac, ETH_ALEN); | |
1da177e4 LT |
512 | |
513 | skb->dev = np->dev; | |
514 | ||
515 | netpoll_send_skb(np, skb); | |
516 | } | |
9e34a5b5 | 517 | EXPORT_SYMBOL(netpoll_send_udp); |
1da177e4 | 518 | |
b7394d24 | 519 | static void netpoll_neigh_reply(struct sk_buff *skb, struct netpoll_info *npinfo) |
1da177e4 | 520 | { |
b3d936f3 | 521 | int size, type = ARPOP_REPLY; |
252e3346 | 522 | __be32 sip, tip; |
47bbec02 | 523 | unsigned char *sha; |
1da177e4 | 524 | struct sk_buff *send_skb; |
508e14b4 DB |
525 | struct netpoll *np, *tmp; |
526 | unsigned long flags; | |
ae641949 | 527 | int hlen, tlen; |
b7394d24 | 528 | int hits = 0, proto; |
508e14b4 DB |
529 | |
530 | if (list_empty(&npinfo->rx_np)) | |
531 | return; | |
532 | ||
533 | /* Before checking the packet, we do some early | |
534 | inspection whether this is interesting at all */ | |
535 | spin_lock_irqsave(&npinfo->rx_lock, flags); | |
536 | list_for_each_entry_safe(np, tmp, &npinfo->rx_np, rx) { | |
537 | if (np->dev == skb->dev) | |
538 | hits++; | |
539 | } | |
540 | spin_unlock_irqrestore(&npinfo->rx_lock, flags); | |
1da177e4 | 541 | |
508e14b4 DB |
542 | /* No netpoll struct is using this dev */ |
543 | if (!hits) | |
115c1d6e | 544 | return; |
1da177e4 | 545 | |
b7394d24 CW |
546 | proto = ntohs(eth_hdr(skb)->h_proto); |
547 | if (proto == ETH_P_IP) { | |
b3d936f3 CW |
548 | struct arphdr *arp; |
549 | unsigned char *arp_ptr; | |
b7394d24 CW |
550 | /* No arp on this interface */ |
551 | if (skb->dev->flags & IFF_NOARP) | |
552 | return; | |
1da177e4 | 553 | |
b7394d24 CW |
554 | if (!pskb_may_pull(skb, arp_hdr_len(skb->dev))) |
555 | return; | |
1da177e4 | 556 | |
b7394d24 CW |
557 | skb_reset_network_header(skb); |
558 | skb_reset_transport_header(skb); | |
559 | arp = arp_hdr(skb); | |
1da177e4 | 560 | |
b7394d24 CW |
561 | if ((arp->ar_hrd != htons(ARPHRD_ETHER) && |
562 | arp->ar_hrd != htons(ARPHRD_IEEE802)) || | |
563 | arp->ar_pro != htons(ETH_P_IP) || | |
564 | arp->ar_op != htons(ARPOP_REQUEST)) | |
565 | return; | |
1da177e4 | 566 | |
b7394d24 CW |
567 | arp_ptr = (unsigned char *)(arp+1); |
568 | /* save the location of the src hw addr */ | |
569 | sha = arp_ptr; | |
570 | arp_ptr += skb->dev->addr_len; | |
571 | memcpy(&sip, arp_ptr, 4); | |
572 | arp_ptr += 4; | |
573 | /* If we actually cared about dst hw addr, | |
574 | it would get copied here */ | |
575 | arp_ptr += skb->dev->addr_len; | |
576 | memcpy(&tip, arp_ptr, 4); | |
1da177e4 | 577 | |
b7394d24 CW |
578 | /* Should we ignore arp? */ |
579 | if (ipv4_is_loopback(tip) || ipv4_is_multicast(tip)) | |
580 | return; | |
1da177e4 | 581 | |
b7394d24 | 582 | size = arp_hdr_len(skb->dev); |
1da177e4 | 583 | |
b7394d24 CW |
584 | spin_lock_irqsave(&npinfo->rx_lock, flags); |
585 | list_for_each_entry_safe(np, tmp, &npinfo->rx_np, rx) { | |
586 | if (tip != np->local_ip.ip) | |
587 | continue; | |
588 | ||
589 | hlen = LL_RESERVED_SPACE(np->dev); | |
590 | tlen = np->dev->needed_tailroom; | |
591 | send_skb = find_skb(np, size + hlen + tlen, hlen); | |
592 | if (!send_skb) | |
593 | continue; | |
594 | ||
595 | skb_reset_network_header(send_skb); | |
596 | arp = (struct arphdr *) skb_put(send_skb, size); | |
597 | send_skb->dev = skb->dev; | |
598 | send_skb->protocol = htons(ETH_P_ARP); | |
599 | ||
600 | /* Fill the device header for the ARP frame */ | |
b3d936f3 | 601 | if (dev_hard_header(send_skb, skb->dev, ETH_P_ARP, |
b7394d24 CW |
602 | sha, np->dev->dev_addr, |
603 | send_skb->len) < 0) { | |
604 | kfree_skb(send_skb); | |
605 | continue; | |
606 | } | |
1da177e4 | 607 | |
b7394d24 CW |
608 | /* |
609 | * Fill out the arp protocol part. | |
610 | * | |
611 | * we only support ethernet device type, | |
612 | * which (according to RFC 1390) should | |
613 | * always equal 1 (Ethernet). | |
614 | */ | |
615 | ||
616 | arp->ar_hrd = htons(np->dev->type); | |
617 | arp->ar_pro = htons(ETH_P_IP); | |
618 | arp->ar_hln = np->dev->addr_len; | |
619 | arp->ar_pln = 4; | |
620 | arp->ar_op = htons(type); | |
621 | ||
622 | arp_ptr = (unsigned char *)(arp + 1); | |
623 | memcpy(arp_ptr, np->dev->dev_addr, np->dev->addr_len); | |
624 | arp_ptr += np->dev->addr_len; | |
625 | memcpy(arp_ptr, &tip, 4); | |
626 | arp_ptr += 4; | |
627 | memcpy(arp_ptr, sha, np->dev->addr_len); | |
628 | arp_ptr += np->dev->addr_len; | |
629 | memcpy(arp_ptr, &sip, 4); | |
630 | ||
631 | netpoll_send_skb(np, send_skb); | |
632 | ||
633 | /* If there are several rx_hooks for the same address, | |
634 | we're fine by sending a single reply */ | |
635 | break; | |
508e14b4 | 636 | } |
b7394d24 | 637 | spin_unlock_irqrestore(&npinfo->rx_lock, flags); |
b3d936f3 CW |
638 | } else if( proto == ETH_P_IPV6) { |
639 | #if IS_ENABLED(CONFIG_IPV6) | |
640 | struct nd_msg *msg; | |
641 | u8 *lladdr = NULL; | |
642 | struct ipv6hdr *hdr; | |
643 | struct icmp6hdr *icmp6h; | |
644 | const struct in6_addr *saddr; | |
645 | const struct in6_addr *daddr; | |
646 | struct inet6_dev *in6_dev = NULL; | |
647 | struct in6_addr *target; | |
648 | ||
649 | in6_dev = in6_dev_get(skb->dev); | |
650 | if (!in6_dev || !in6_dev->cnf.accept_ra) | |
651 | return; | |
652 | ||
653 | if (!pskb_may_pull(skb, skb->len)) | |
654 | return; | |
655 | ||
656 | msg = (struct nd_msg *)skb_transport_header(skb); | |
657 | ||
658 | __skb_push(skb, skb->data - skb_transport_header(skb)); | |
659 | ||
660 | if (ipv6_hdr(skb)->hop_limit != 255) | |
661 | return; | |
662 | if (msg->icmph.icmp6_code != 0) | |
663 | return; | |
664 | if (msg->icmph.icmp6_type != NDISC_NEIGHBOUR_SOLICITATION) | |
665 | return; | |
666 | ||
667 | saddr = &ipv6_hdr(skb)->saddr; | |
668 | daddr = &ipv6_hdr(skb)->daddr; | |
669 | ||
670 | size = sizeof(struct icmp6hdr) + sizeof(struct in6_addr); | |
671 | ||
672 | spin_lock_irqsave(&npinfo->rx_lock, flags); | |
673 | list_for_each_entry_safe(np, tmp, &npinfo->rx_np, rx) { | |
faeed828 | 674 | if (!ipv6_addr_equal(daddr, &np->local_ip.in6)) |
b3d936f3 CW |
675 | continue; |
676 | ||
677 | hlen = LL_RESERVED_SPACE(np->dev); | |
678 | tlen = np->dev->needed_tailroom; | |
679 | send_skb = find_skb(np, size + hlen + tlen, hlen); | |
680 | if (!send_skb) | |
681 | continue; | |
682 | ||
683 | send_skb->protocol = htons(ETH_P_IPV6); | |
684 | send_skb->dev = skb->dev; | |
685 | ||
686 | skb_reset_network_header(send_skb); | |
687 | skb_put(send_skb, sizeof(struct ipv6hdr)); | |
688 | hdr = ipv6_hdr(send_skb); | |
689 | ||
690 | *(__be32*)hdr = htonl(0x60000000); | |
691 | ||
692 | hdr->payload_len = htons(size); | |
693 | hdr->nexthdr = IPPROTO_ICMPV6; | |
694 | hdr->hop_limit = 255; | |
695 | hdr->saddr = *saddr; | |
696 | hdr->daddr = *daddr; | |
697 | ||
698 | send_skb->transport_header = send_skb->tail; | |
699 | skb_put(send_skb, size); | |
700 | ||
701 | icmp6h = (struct icmp6hdr *)skb_transport_header(skb); | |
702 | icmp6h->icmp6_type = NDISC_NEIGHBOUR_ADVERTISEMENT; | |
703 | icmp6h->icmp6_router = 0; | |
704 | icmp6h->icmp6_solicited = 1; | |
705 | target = (struct in6_addr *)skb_transport_header(send_skb) + sizeof(struct icmp6hdr); | |
706 | *target = msg->target; | |
707 | icmp6h->icmp6_cksum = csum_ipv6_magic(saddr, daddr, size, | |
708 | IPPROTO_ICMPV6, | |
709 | csum_partial(icmp6h, | |
710 | size, 0)); | |
711 | ||
712 | if (dev_hard_header(send_skb, skb->dev, ETH_P_IPV6, | |
713 | lladdr, np->dev->dev_addr, | |
714 | send_skb->len) < 0) { | |
715 | kfree_skb(send_skb); | |
716 | continue; | |
717 | } | |
718 | ||
719 | netpoll_send_skb(np, send_skb); | |
720 | ||
721 | /* If there are several rx_hooks for the same address, | |
722 | we're fine by sending a single reply */ | |
723 | break; | |
724 | } | |
725 | spin_unlock_irqrestore(&npinfo->rx_lock, flags); | |
726 | #endif | |
508e14b4 | 727 | } |
1da177e4 LT |
728 | } |
729 | ||
b3d936f3 CW |
730 | static bool pkt_is_ns(struct sk_buff *skb) |
731 | { | |
732 | struct nd_msg *msg; | |
733 | struct ipv6hdr *hdr; | |
734 | ||
735 | if (skb->protocol != htons(ETH_P_ARP)) | |
736 | return false; | |
737 | if (!pskb_may_pull(skb, sizeof(struct ipv6hdr) + sizeof(struct nd_msg))) | |
738 | return false; | |
739 | ||
740 | msg = (struct nd_msg *)skb_transport_header(skb); | |
741 | __skb_push(skb, skb->data - skb_transport_header(skb)); | |
742 | hdr = ipv6_hdr(skb); | |
743 | ||
744 | if (hdr->nexthdr != IPPROTO_ICMPV6) | |
745 | return false; | |
746 | if (hdr->hop_limit != 255) | |
747 | return false; | |
748 | if (msg->icmph.icmp6_code != 0) | |
749 | return false; | |
750 | if (msg->icmph.icmp6_type != NDISC_NEIGHBOUR_SOLICITATION) | |
751 | return false; | |
752 | ||
753 | return true; | |
754 | } | |
755 | ||
57c5d461 | 756 | int __netpoll_rx(struct sk_buff *skb, struct netpoll_info *npinfo) |
1da177e4 LT |
757 | { |
758 | int proto, len, ulen; | |
508e14b4 | 759 | int hits = 0; |
b71d1d42 | 760 | const struct iphdr *iph; |
1da177e4 | 761 | struct udphdr *uh; |
508e14b4 | 762 | struct netpoll *np, *tmp; |
068c6e98 | 763 | |
508e14b4 | 764 | if (list_empty(&npinfo->rx_np)) |
1da177e4 | 765 | goto out; |
508e14b4 | 766 | |
1da177e4 LT |
767 | if (skb->dev->type != ARPHRD_ETHER) |
768 | goto out; | |
769 | ||
d9452e9f | 770 | /* check if netpoll clients need ARP */ |
b3d936f3 CW |
771 | if (skb->protocol == htons(ETH_P_ARP) && atomic_read(&trapped)) { |
772 | skb_queue_tail(&npinfo->neigh_tx, skb); | |
773 | return 1; | |
774 | } else if (pkt_is_ns(skb) && atomic_read(&trapped)) { | |
b7394d24 | 775 | skb_queue_tail(&npinfo->neigh_tx, skb); |
1da177e4 LT |
776 | return 1; |
777 | } | |
778 | ||
689971b4 AW |
779 | if (skb->protocol == cpu_to_be16(ETH_P_8021Q)) { |
780 | skb = vlan_untag(skb); | |
781 | if (unlikely(!skb)) | |
782 | goto out; | |
783 | } | |
784 | ||
1da177e4 | 785 | proto = ntohs(eth_hdr(skb)->h_proto); |
b7394d24 | 786 | if (proto != ETH_P_IP && proto != ETH_P_IPV6) |
1da177e4 LT |
787 | goto out; |
788 | if (skb->pkt_type == PACKET_OTHERHOST) | |
789 | goto out; | |
790 | if (skb_shared(skb)) | |
791 | goto out; | |
792 | ||
b7394d24 CW |
793 | if (proto == ETH_P_IP) { |
794 | if (!pskb_may_pull(skb, sizeof(struct iphdr))) | |
795 | goto out; | |
796 | iph = (struct iphdr *)skb->data; | |
797 | if (iph->ihl < 5 || iph->version != 4) | |
798 | goto out; | |
799 | if (!pskb_may_pull(skb, iph->ihl*4)) | |
800 | goto out; | |
801 | iph = (struct iphdr *)skb->data; | |
802 | if (ip_fast_csum((u8 *)iph, iph->ihl) != 0) | |
803 | goto out; | |
5e7d7fa5 | 804 | |
b7394d24 CW |
805 | len = ntohs(iph->tot_len); |
806 | if (skb->len < len || len < iph->ihl*4) | |
807 | goto out; | |
1da177e4 | 808 | |
b7394d24 CW |
809 | /* |
810 | * Our transport medium may have padded the buffer out. | |
811 | * Now We trim to the true length of the frame. | |
812 | */ | |
813 | if (pskb_trim_rcsum(skb, len)) | |
814 | goto out; | |
1da177e4 | 815 | |
b7394d24 CW |
816 | iph = (struct iphdr *)skb->data; |
817 | if (iph->protocol != IPPROTO_UDP) | |
818 | goto out; | |
1da177e4 | 819 | |
b7394d24 CW |
820 | len -= iph->ihl*4; |
821 | uh = (struct udphdr *)(((char *)iph) + iph->ihl*4); | |
822 | ulen = ntohs(uh->len); | |
508e14b4 | 823 | |
b7394d24 CW |
824 | if (ulen != len) |
825 | goto out; | |
826 | if (checksum_udp(skb, uh, ulen, iph->saddr, iph->daddr)) | |
827 | goto out; | |
828 | list_for_each_entry_safe(np, tmp, &npinfo->rx_np, rx) { | |
829 | if (np->local_ip.ip && np->local_ip.ip != iph->daddr) | |
830 | continue; | |
831 | if (np->remote_ip.ip && np->remote_ip.ip != iph->saddr) | |
832 | continue; | |
833 | if (np->local_port && np->local_port != ntohs(uh->dest)) | |
834 | continue; | |
835 | ||
836 | np->rx_hook(np, ntohs(uh->source), | |
837 | (char *)(uh+1), | |
838 | ulen - sizeof(struct udphdr)); | |
839 | hits++; | |
840 | } | |
b3d936f3 CW |
841 | } else { |
842 | #if IS_ENABLED(CONFIG_IPV6) | |
843 | const struct ipv6hdr *ip6h; | |
844 | ||
845 | if (!pskb_may_pull(skb, sizeof(struct ipv6hdr))) | |
846 | goto out; | |
847 | ip6h = (struct ipv6hdr *)skb->data; | |
848 | if (ip6h->version != 6) | |
849 | goto out; | |
850 | len = ntohs(ip6h->payload_len); | |
851 | if (!len) | |
852 | goto out; | |
853 | if (len + sizeof(struct ipv6hdr) > skb->len) | |
854 | goto out; | |
855 | if (pskb_trim_rcsum(skb, len + sizeof(struct ipv6hdr))) | |
856 | goto out; | |
857 | ip6h = ipv6_hdr(skb); | |
858 | if (!pskb_may_pull(skb, sizeof(struct udphdr))) | |
859 | goto out; | |
860 | uh = udp_hdr(skb); | |
861 | ulen = ntohs(uh->len); | |
862 | if (ulen != skb->len) | |
863 | goto out; | |
864 | if (udp6_csum_init(skb, uh, IPPROTO_UDP)) | |
865 | goto out; | |
866 | list_for_each_entry_safe(np, tmp, &npinfo->rx_np, rx) { | |
faeed828 | 867 | if (!ipv6_addr_equal(&np->local_ip.in6, &ip6h->daddr)) |
b3d936f3 | 868 | continue; |
faeed828 | 869 | if (!ipv6_addr_equal(&np->remote_ip.in6, &ip6h->saddr)) |
b3d936f3 CW |
870 | continue; |
871 | if (np->local_port && np->local_port != ntohs(uh->dest)) | |
872 | continue; | |
873 | ||
874 | np->rx_hook(np, ntohs(uh->source), | |
875 | (char *)(uh+1), | |
876 | ulen - sizeof(struct udphdr)); | |
877 | hits++; | |
878 | } | |
879 | #endif | |
508e14b4 DB |
880 | } |
881 | ||
882 | if (!hits) | |
883 | goto out; | |
1da177e4 LT |
884 | |
885 | kfree_skb(skb); | |
886 | return 1; | |
887 | ||
888 | out: | |
889 | if (atomic_read(&trapped)) { | |
890 | kfree_skb(skb); | |
891 | return 1; | |
892 | } | |
893 | ||
894 | return 0; | |
895 | } | |
896 | ||
0bcc1816 SS |
897 | void netpoll_print_options(struct netpoll *np) |
898 | { | |
e6ec2693 | 899 | np_info(np, "local port %d\n", np->local_port); |
b3d936f3 CW |
900 | if (np->ipv6) |
901 | np_info(np, "local IPv6 address %pI6c\n", &np->local_ip.in6); | |
902 | else | |
b7394d24 | 903 | np_info(np, "local IPv4 address %pI4\n", &np->local_ip.ip); |
e6ec2693 JP |
904 | np_info(np, "interface '%s'\n", np->dev_name); |
905 | np_info(np, "remote port %d\n", np->remote_port); | |
b3d936f3 CW |
906 | if (np->ipv6) |
907 | np_info(np, "remote IPv6 address %pI6c\n", &np->remote_ip.in6); | |
908 | else | |
b7394d24 | 909 | np_info(np, "remote IPv4 address %pI4\n", &np->remote_ip.ip); |
e6ec2693 | 910 | np_info(np, "remote ethernet address %pM\n", np->remote_mac); |
0bcc1816 | 911 | } |
9e34a5b5 | 912 | EXPORT_SYMBOL(netpoll_print_options); |
0bcc1816 | 913 | |
b7394d24 CW |
914 | static int netpoll_parse_ip_addr(const char *str, union inet_addr *addr) |
915 | { | |
916 | const char *end; | |
917 | ||
918 | if (!strchr(str, ':') && | |
919 | in4_pton(str, -1, (void *)addr, -1, &end) > 0) { | |
920 | if (!*end) | |
921 | return 0; | |
922 | } | |
923 | if (in6_pton(str, -1, addr->in6.s6_addr, -1, &end) > 0) { | |
924 | #if IS_ENABLED(CONFIG_IPV6) | |
925 | if (!*end) | |
926 | return 1; | |
927 | #else | |
928 | return -1; | |
929 | #endif | |
930 | } | |
931 | return -1; | |
932 | } | |
933 | ||
1da177e4 LT |
934 | int netpoll_parse_options(struct netpoll *np, char *opt) |
935 | { | |
936 | char *cur=opt, *delim; | |
b7394d24 | 937 | int ipv6; |
1da177e4 | 938 | |
c68b9070 | 939 | if (*cur != '@') { |
1da177e4 LT |
940 | if ((delim = strchr(cur, '@')) == NULL) |
941 | goto parse_failed; | |
c68b9070 | 942 | *delim = 0; |
4b5511eb AP |
943 | if (kstrtou16(cur, 10, &np->local_port)) |
944 | goto parse_failed; | |
c68b9070 | 945 | cur = delim; |
1da177e4 LT |
946 | } |
947 | cur++; | |
1da177e4 | 948 | |
c68b9070 | 949 | if (*cur != '/') { |
1da177e4 LT |
950 | if ((delim = strchr(cur, '/')) == NULL) |
951 | goto parse_failed; | |
c68b9070 | 952 | *delim = 0; |
b7394d24 CW |
953 | ipv6 = netpoll_parse_ip_addr(cur, &np->local_ip); |
954 | if (ipv6 < 0) | |
955 | goto parse_failed; | |
956 | else | |
957 | np->ipv6 = (bool)ipv6; | |
c68b9070 | 958 | cur = delim; |
1da177e4 LT |
959 | } |
960 | cur++; | |
961 | ||
c68b9070 | 962 | if (*cur != ',') { |
1da177e4 LT |
963 | /* parse out dev name */ |
964 | if ((delim = strchr(cur, ',')) == NULL) | |
965 | goto parse_failed; | |
c68b9070 | 966 | *delim = 0; |
1da177e4 | 967 | strlcpy(np->dev_name, cur, sizeof(np->dev_name)); |
c68b9070 | 968 | cur = delim; |
1da177e4 LT |
969 | } |
970 | cur++; | |
971 | ||
c68b9070 | 972 | if (*cur != '@') { |
1da177e4 LT |
973 | /* dst port */ |
974 | if ((delim = strchr(cur, '@')) == NULL) | |
975 | goto parse_failed; | |
c68b9070 | 976 | *delim = 0; |
5fc05f87 | 977 | if (*cur == ' ' || *cur == '\t') |
e6ec2693 | 978 | np_info(np, "warning: whitespace is not allowed\n"); |
4b5511eb AP |
979 | if (kstrtou16(cur, 10, &np->remote_port)) |
980 | goto parse_failed; | |
c68b9070 | 981 | cur = delim; |
1da177e4 LT |
982 | } |
983 | cur++; | |
1da177e4 LT |
984 | |
985 | /* dst ip */ | |
986 | if ((delim = strchr(cur, '/')) == NULL) | |
987 | goto parse_failed; | |
c68b9070 | 988 | *delim = 0; |
b7394d24 CW |
989 | ipv6 = netpoll_parse_ip_addr(cur, &np->remote_ip); |
990 | if (ipv6 < 0) | |
991 | goto parse_failed; | |
992 | else if (np->ipv6 != (bool)ipv6) | |
993 | goto parse_failed; | |
994 | else | |
995 | np->ipv6 = (bool)ipv6; | |
c68b9070 | 996 | cur = delim + 1; |
1da177e4 | 997 | |
c68b9070 | 998 | if (*cur != 0) { |
1da177e4 | 999 | /* MAC address */ |
4940fc88 | 1000 | if (!mac_pton(cur, np->remote_mac)) |
1da177e4 | 1001 | goto parse_failed; |
1da177e4 LT |
1002 | } |
1003 | ||
0bcc1816 | 1004 | netpoll_print_options(np); |
1da177e4 LT |
1005 | |
1006 | return 0; | |
1007 | ||
1008 | parse_failed: | |
e6ec2693 | 1009 | np_info(np, "couldn't parse config at '%s'!\n", cur); |
1da177e4 LT |
1010 | return -1; |
1011 | } | |
9e34a5b5 | 1012 | EXPORT_SYMBOL(netpoll_parse_options); |
1da177e4 | 1013 | |
47be03a2 | 1014 | int __netpoll_setup(struct netpoll *np, struct net_device *ndev, gfp_t gfp) |
1da177e4 | 1015 | { |
115c1d6e | 1016 | struct netpoll_info *npinfo; |
4247e161 | 1017 | const struct net_device_ops *ops; |
fbeec2e1 | 1018 | unsigned long flags; |
b41848b6 | 1019 | int err; |
1da177e4 | 1020 | |
30fdd8a0 JP |
1021 | np->dev = ndev; |
1022 | strlcpy(np->dev_name, ndev->name, IFNAMSIZ); | |
1023 | ||
8fdd95ec HX |
1024 | if ((ndev->priv_flags & IFF_DISABLE_NETPOLL) || |
1025 | !ndev->netdev_ops->ndo_poll_controller) { | |
e6ec2693 JP |
1026 | np_err(np, "%s doesn't support polling, aborting\n", |
1027 | np->dev_name); | |
8fdd95ec HX |
1028 | err = -ENOTSUPP; |
1029 | goto out; | |
1030 | } | |
1031 | ||
1032 | if (!ndev->npinfo) { | |
47be03a2 | 1033 | npinfo = kmalloc(sizeof(*npinfo), gfp); |
8fdd95ec HX |
1034 | if (!npinfo) { |
1035 | err = -ENOMEM; | |
1036 | goto out; | |
1037 | } | |
1038 | ||
1039 | npinfo->rx_flags = 0; | |
1040 | INIT_LIST_HEAD(&npinfo->rx_np); | |
1041 | ||
1042 | spin_lock_init(&npinfo->rx_lock); | |
ca99ca14 | 1043 | mutex_init(&npinfo->dev_lock); |
b7394d24 | 1044 | skb_queue_head_init(&npinfo->neigh_tx); |
8fdd95ec HX |
1045 | skb_queue_head_init(&npinfo->txq); |
1046 | INIT_DELAYED_WORK(&npinfo->tx_work, queue_process); | |
1047 | ||
1048 | atomic_set(&npinfo->refcnt, 1); | |
1049 | ||
1050 | ops = np->dev->netdev_ops; | |
1051 | if (ops->ndo_netpoll_setup) { | |
47be03a2 | 1052 | err = ops->ndo_netpoll_setup(ndev, npinfo, gfp); |
8fdd95ec HX |
1053 | if (err) |
1054 | goto free_npinfo; | |
1055 | } | |
1056 | } else { | |
1057 | npinfo = ndev->npinfo; | |
1058 | atomic_inc(&npinfo->refcnt); | |
1059 | } | |
1060 | ||
1061 | npinfo->netpoll = np; | |
1062 | ||
1063 | if (np->rx_hook) { | |
1064 | spin_lock_irqsave(&npinfo->rx_lock, flags); | |
1065 | npinfo->rx_flags |= NETPOLL_RX_ENABLED; | |
1066 | list_add_tail(&np->rx, &npinfo->rx_np); | |
1067 | spin_unlock_irqrestore(&npinfo->rx_lock, flags); | |
1068 | } | |
1069 | ||
1070 | /* last thing to do is link it to the net device structure */ | |
cf778b00 | 1071 | rcu_assign_pointer(ndev->npinfo, npinfo); |
8fdd95ec HX |
1072 | |
1073 | return 0; | |
1074 | ||
1075 | free_npinfo: | |
1076 | kfree(npinfo); | |
1077 | out: | |
1078 | return err; | |
1079 | } | |
1080 | EXPORT_SYMBOL_GPL(__netpoll_setup); | |
1081 | ||
1082 | int netpoll_setup(struct netpoll *np) | |
1083 | { | |
1084 | struct net_device *ndev = NULL; | |
1085 | struct in_device *in_dev; | |
1086 | int err; | |
1087 | ||
f92d3180 | 1088 | rtnl_lock(); |
556e6256 CW |
1089 | if (np->dev_name) { |
1090 | struct net *net = current->nsproxy->net_ns; | |
1091 | ndev = __dev_get_by_name(net, np->dev_name); | |
1092 | } | |
1da177e4 | 1093 | if (!ndev) { |
e6ec2693 | 1094 | np_err(np, "%s doesn't exist, aborting\n", np->dev_name); |
f92d3180 CW |
1095 | err = -ENODEV; |
1096 | goto unlock; | |
1da177e4 | 1097 | } |
5bd30d39 | 1098 | dev_hold(ndev); |
1da177e4 | 1099 | |
49bd8fb0 | 1100 | if (netdev_master_upper_dev_get(ndev)) { |
e6ec2693 | 1101 | np_err(np, "%s is a slave device, aborting\n", np->dev_name); |
83fe32de DC |
1102 | err = -EBUSY; |
1103 | goto put; | |
0c1ad04a WC |
1104 | } |
1105 | ||
1da177e4 LT |
1106 | if (!netif_running(ndev)) { |
1107 | unsigned long atmost, atleast; | |
1108 | ||
e6ec2693 | 1109 | np_info(np, "device %s not up yet, forcing it\n", np->dev_name); |
1da177e4 | 1110 | |
b41848b6 | 1111 | err = dev_open(ndev); |
b41848b6 SH |
1112 | |
1113 | if (err) { | |
e6ec2693 | 1114 | np_err(np, "failed to open %s\n", ndev->name); |
dbaa1541 | 1115 | goto put; |
1da177e4 | 1116 | } |
1da177e4 | 1117 | |
f92d3180 | 1118 | rtnl_unlock(); |
1da177e4 | 1119 | atleast = jiffies + HZ/10; |
bff38771 | 1120 | atmost = jiffies + carrier_timeout * HZ; |
1da177e4 LT |
1121 | while (!netif_carrier_ok(ndev)) { |
1122 | if (time_after(jiffies, atmost)) { | |
e6ec2693 | 1123 | np_notice(np, "timeout waiting for carrier\n"); |
1da177e4 LT |
1124 | break; |
1125 | } | |
1b614fb9 | 1126 | msleep(1); |
1da177e4 LT |
1127 | } |
1128 | ||
1129 | /* If carrier appears to come up instantly, we don't | |
1130 | * trust it and pause so that we don't pump all our | |
1131 | * queued console messages into the bitbucket. | |
1132 | */ | |
1133 | ||
1134 | if (time_before(jiffies, atleast)) { | |
e6ec2693 | 1135 | np_notice(np, "carrier detect appears untrustworthy, waiting 4 seconds\n"); |
1da177e4 LT |
1136 | msleep(4000); |
1137 | } | |
f92d3180 | 1138 | rtnl_lock(); |
1da177e4 LT |
1139 | } |
1140 | ||
b7394d24 CW |
1141 | if (!np->local_ip.ip) { |
1142 | if (!np->ipv6) { | |
f92d3180 | 1143 | in_dev = __in_dev_get_rtnl(ndev); |
b7394d24 CW |
1144 | |
1145 | if (!in_dev || !in_dev->ifa_list) { | |
b7394d24 CW |
1146 | np_err(np, "no IP address for %s, aborting\n", |
1147 | np->dev_name); | |
1148 | err = -EDESTADDRREQ; | |
1149 | goto put; | |
1150 | } | |
1151 | ||
1152 | np->local_ip.ip = in_dev->ifa_list->ifa_local; | |
b7394d24 | 1153 | np_info(np, "local IP %pI4\n", &np->local_ip.ip); |
b3d936f3 CW |
1154 | } else { |
1155 | #if IS_ENABLED(CONFIG_IPV6) | |
1156 | struct inet6_dev *idev; | |
1157 | ||
1158 | err = -EDESTADDRREQ; | |
b3d936f3 CW |
1159 | idev = __in6_dev_get(ndev); |
1160 | if (idev) { | |
1161 | struct inet6_ifaddr *ifp; | |
1162 | ||
1163 | read_lock_bh(&idev->lock); | |
1164 | list_for_each_entry(ifp, &idev->addr_list, if_list) { | |
1165 | if (ipv6_addr_type(&ifp->addr) & IPV6_ADDR_LINKLOCAL) | |
1166 | continue; | |
1167 | np->local_ip.in6 = ifp->addr; | |
1168 | err = 0; | |
1169 | break; | |
1170 | } | |
1171 | read_unlock_bh(&idev->lock); | |
1172 | } | |
b3d936f3 CW |
1173 | if (err) { |
1174 | np_err(np, "no IPv6 address for %s, aborting\n", | |
1175 | np->dev_name); | |
1176 | goto put; | |
1177 | } else | |
1178 | np_info(np, "local IPv6 %pI6c\n", &np->local_ip.in6); | |
1179 | #else | |
1180 | np_err(np, "IPv6 is not supported %s, aborting\n", | |
1181 | np->dev_name); | |
e39363a9 | 1182 | err = -EINVAL; |
b3d936f3 CW |
1183 | goto put; |
1184 | #endif | |
1da177e4 | 1185 | } |
1da177e4 LT |
1186 | } |
1187 | ||
dbaa1541 HX |
1188 | /* fill up the skb queue */ |
1189 | refill_skbs(); | |
1190 | ||
47be03a2 | 1191 | err = __netpoll_setup(np, ndev, GFP_KERNEL); |
8fdd95ec HX |
1192 | if (err) |
1193 | goto put; | |
1194 | ||
f92d3180 | 1195 | rtnl_unlock(); |
1da177e4 LT |
1196 | return 0; |
1197 | ||
21edbb22 | 1198 | put: |
1da177e4 | 1199 | dev_put(ndev); |
f92d3180 CW |
1200 | unlock: |
1201 | rtnl_unlock(); | |
b41848b6 | 1202 | return err; |
1da177e4 | 1203 | } |
9e34a5b5 | 1204 | EXPORT_SYMBOL(netpoll_setup); |
1da177e4 | 1205 | |
c68b9070 DM |
1206 | static int __init netpoll_init(void) |
1207 | { | |
a1bcfacd | 1208 | skb_queue_head_init(&skb_pool); |
ca99ca14 | 1209 | init_srcu_struct(&netpoll_srcu); |
a1bcfacd SH |
1210 | return 0; |
1211 | } | |
1212 | core_initcall(netpoll_init); | |
1213 | ||
38e6bc18 AW |
1214 | static void rcu_cleanup_netpoll_info(struct rcu_head *rcu_head) |
1215 | { | |
1216 | struct netpoll_info *npinfo = | |
1217 | container_of(rcu_head, struct netpoll_info, rcu); | |
1218 | ||
b7394d24 | 1219 | skb_queue_purge(&npinfo->neigh_tx); |
38e6bc18 AW |
1220 | skb_queue_purge(&npinfo->txq); |
1221 | ||
1222 | /* we can't call cancel_delayed_work_sync here, as we are in softirq */ | |
1223 | cancel_delayed_work(&npinfo->tx_work); | |
1224 | ||
1225 | /* clean after last, unfinished work */ | |
1226 | __skb_queue_purge(&npinfo->txq); | |
1227 | /* now cancel it again */ | |
1228 | cancel_delayed_work(&npinfo->tx_work); | |
1229 | kfree(npinfo); | |
1230 | } | |
1231 | ||
8fdd95ec | 1232 | void __netpoll_cleanup(struct netpoll *np) |
1da177e4 | 1233 | { |
fbeec2e1 JM |
1234 | struct netpoll_info *npinfo; |
1235 | unsigned long flags; | |
1236 | ||
8fdd95ec HX |
1237 | npinfo = np->dev->npinfo; |
1238 | if (!npinfo) | |
dbaa1541 | 1239 | return; |
93ec2c72 | 1240 | |
8fdd95ec HX |
1241 | if (!list_empty(&npinfo->rx_np)) { |
1242 | spin_lock_irqsave(&npinfo->rx_lock, flags); | |
1243 | list_del(&np->rx); | |
1244 | if (list_empty(&npinfo->rx_np)) | |
1245 | npinfo->rx_flags &= ~NETPOLL_RX_ENABLED; | |
1246 | spin_unlock_irqrestore(&npinfo->rx_lock, flags); | |
1247 | } | |
de85d99e | 1248 | |
ca99ca14 NH |
1249 | synchronize_srcu(&netpoll_srcu); |
1250 | ||
8fdd95ec HX |
1251 | if (atomic_dec_and_test(&npinfo->refcnt)) { |
1252 | const struct net_device_ops *ops; | |
de85d99e | 1253 | |
8fdd95ec HX |
1254 | ops = np->dev->netdev_ops; |
1255 | if (ops->ndo_netpoll_cleanup) | |
1256 | ops->ndo_netpoll_cleanup(np->dev); | |
de85d99e | 1257 | |
a9b3cd7f | 1258 | RCU_INIT_POINTER(np->dev->npinfo, NULL); |
38e6bc18 AW |
1259 | call_rcu_bh(&npinfo->rcu, rcu_cleanup_netpoll_info); |
1260 | } | |
1261 | } | |
1262 | EXPORT_SYMBOL_GPL(__netpoll_cleanup); | |
de85d99e | 1263 | |
38e6bc18 AW |
1264 | static void rcu_cleanup_netpoll(struct rcu_head *rcu_head) |
1265 | { | |
1266 | struct netpoll *np = container_of(rcu_head, struct netpoll, rcu); | |
93ec2c72 | 1267 | |
38e6bc18 AW |
1268 | __netpoll_cleanup(np); |
1269 | kfree(np); | |
1270 | } | |
93ec2c72 | 1271 | |
38e6bc18 AW |
1272 | void __netpoll_free_rcu(struct netpoll *np) |
1273 | { | |
1274 | call_rcu_bh(&np->rcu, rcu_cleanup_netpoll); | |
8fdd95ec | 1275 | } |
38e6bc18 | 1276 | EXPORT_SYMBOL_GPL(__netpoll_free_rcu); |
fbeec2e1 | 1277 | |
8fdd95ec HX |
1278 | void netpoll_cleanup(struct netpoll *np) |
1279 | { | |
1280 | if (!np->dev) | |
1281 | return; | |
dbaa1541 | 1282 | |
8fdd95ec HX |
1283 | rtnl_lock(); |
1284 | __netpoll_cleanup(np); | |
1285 | rtnl_unlock(); | |
1286 | ||
1287 | dev_put(np->dev); | |
1da177e4 LT |
1288 | np->dev = NULL; |
1289 | } | |
9e34a5b5 | 1290 | EXPORT_SYMBOL(netpoll_cleanup); |
1da177e4 LT |
1291 | |
1292 | int netpoll_trap(void) | |
1293 | { | |
1294 | return atomic_read(&trapped); | |
1295 | } | |
9e34a5b5 | 1296 | EXPORT_SYMBOL(netpoll_trap); |
1da177e4 LT |
1297 | |
1298 | void netpoll_set_trap(int trap) | |
1299 | { | |
1300 | if (trap) | |
1301 | atomic_inc(&trapped); | |
1302 | else | |
1303 | atomic_dec(&trapped); | |
1304 | } | |
1da177e4 | 1305 | EXPORT_SYMBOL(netpoll_set_trap); |