Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
| 12 | #include <linux/smp_lock.h> |
| 13 | #include <linux/netdevice.h> |
| 14 | #include <linux/etherdevice.h> |
| 15 | #include <linux/string.h> |
| 16 | #include <linux/inetdevice.h> |
| 17 | #include <linux/inet.h> |
| 18 | #include <linux/interrupt.h> |
| 19 | #include <linux/netpoll.h> |
| 20 | #include <linux/sched.h> |
| 21 | #include <linux/delay.h> |
| 22 | #include <linux/rcupdate.h> |
| 23 | #include <linux/workqueue.h> |
| 24 | #include <net/tcp.h> |
| 25 | #include <net/udp.h> |
| 26 | #include <asm/unaligned.h> |
| 27 | |
| 28 | /* |
| 29 | * We maintain a small pool of fully-sized skbs, to make sure the |
| 30 | * message gets out even in extreme OOM situations. |
| 31 | */ |
| 32 | |
| 33 | #define MAX_UDP_CHUNK 1460 |
| 34 | #define MAX_SKBS 32 |
| 35 | #define MAX_QUEUE_DEPTH (MAX_SKBS / 2) |
Matt Mackall | 0db1d6f | 2005-08-11 19:25:54 -0700 | [diff] [blame] | 36 | #define MAX_RETRIES 20000 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | |
| 38 | static DEFINE_SPINLOCK(skb_list_lock); |
| 39 | static int nr_skbs; |
| 40 | static struct sk_buff *skbs; |
| 41 | |
| 42 | static DEFINE_SPINLOCK(queue_lock); |
| 43 | static int queue_depth; |
| 44 | static struct sk_buff *queue_head, *queue_tail; |
| 45 | |
| 46 | static atomic_t trapped; |
| 47 | |
| 48 | #define NETPOLL_RX_ENABLED 1 |
| 49 | #define NETPOLL_RX_DROP 2 |
| 50 | |
| 51 | #define MAX_SKB_SIZE \ |
| 52 | (MAX_UDP_CHUNK + sizeof(struct udphdr) + \ |
| 53 | sizeof(struct iphdr) + sizeof(struct ethhdr)) |
| 54 | |
| 55 | static void zap_completion_queue(void); |
| 56 | |
| 57 | static void queue_process(void *p) |
| 58 | { |
| 59 | unsigned long flags; |
| 60 | struct sk_buff *skb; |
| 61 | |
| 62 | while (queue_head) { |
| 63 | spin_lock_irqsave(&queue_lock, flags); |
| 64 | |
| 65 | skb = queue_head; |
| 66 | queue_head = skb->next; |
| 67 | if (skb == queue_tail) |
| 68 | queue_head = NULL; |
| 69 | |
| 70 | queue_depth--; |
| 71 | |
| 72 | spin_unlock_irqrestore(&queue_lock, flags); |
| 73 | |
| 74 | dev_queue_xmit(skb); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | static DECLARE_WORK(send_queue, queue_process, NULL); |
| 79 | |
| 80 | void netpoll_queue(struct sk_buff *skb) |
| 81 | { |
| 82 | unsigned long flags; |
| 83 | |
| 84 | if (queue_depth == MAX_QUEUE_DEPTH) { |
| 85 | __kfree_skb(skb); |
| 86 | return; |
| 87 | } |
| 88 | |
| 89 | spin_lock_irqsave(&queue_lock, flags); |
| 90 | if (!queue_head) |
| 91 | queue_head = skb; |
| 92 | else |
| 93 | queue_tail->next = skb; |
| 94 | queue_tail = skb; |
| 95 | queue_depth++; |
| 96 | spin_unlock_irqrestore(&queue_lock, flags); |
| 97 | |
| 98 | schedule_work(&send_queue); |
| 99 | } |
| 100 | |
| 101 | static int checksum_udp(struct sk_buff *skb, struct udphdr *uh, |
| 102 | unsigned short ulen, u32 saddr, u32 daddr) |
| 103 | { |
| 104 | if (uh->check == 0) |
| 105 | return 0; |
| 106 | |
| 107 | if (skb->ip_summed == CHECKSUM_HW) |
| 108 | return csum_tcpudp_magic( |
| 109 | saddr, daddr, ulen, IPPROTO_UDP, skb->csum); |
| 110 | |
| 111 | skb->csum = csum_tcpudp_nofold(saddr, daddr, ulen, IPPROTO_UDP, 0); |
| 112 | |
| 113 | return csum_fold(skb_checksum(skb, 0, skb->len, skb->csum)); |
| 114 | } |
| 115 | |
| 116 | /* |
| 117 | * Check whether delayed processing was scheduled for our NIC. If so, |
| 118 | * we attempt to grab the poll lock and use ->poll() to pump the card. |
| 119 | * If this fails, either we've recursed in ->poll() or it's already |
| 120 | * running on another CPU. |
| 121 | * |
| 122 | * Note: we don't mask interrupts with this lock because we're using |
| 123 | * trylock here and interrupts are already disabled in the softirq |
| 124 | * case. Further, we test the poll_owner to avoid recursion on UP |
| 125 | * systems where the lock doesn't exist. |
| 126 | * |
| 127 | * In cases where there is bi-directional communications, reading only |
| 128 | * one message at a time can lead to packets being dropped by the |
| 129 | * network adapter, forcing superfluous retries and possibly timeouts. |
| 130 | * Thus, we set our budget to greater than 1. |
| 131 | */ |
| 132 | static void poll_napi(struct netpoll *np) |
| 133 | { |
Jeff Moyer | 115c1d6 | 2005-06-22 22:05:31 -0700 | [diff] [blame] | 134 | struct netpoll_info *npinfo = np->dev->npinfo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | int budget = 16; |
| 136 | |
| 137 | if (test_bit(__LINK_STATE_RX_SCHED, &np->dev->state) && |
Jeff Moyer | 115c1d6 | 2005-06-22 22:05:31 -0700 | [diff] [blame] | 138 | npinfo->poll_owner != smp_processor_id() && |
| 139 | spin_trylock(&npinfo->poll_lock)) { |
| 140 | npinfo->rx_flags |= NETPOLL_RX_DROP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | atomic_inc(&trapped); |
| 142 | |
| 143 | np->dev->poll(np->dev, &budget); |
| 144 | |
| 145 | atomic_dec(&trapped); |
Jeff Moyer | 115c1d6 | 2005-06-22 22:05:31 -0700 | [diff] [blame] | 146 | npinfo->rx_flags &= ~NETPOLL_RX_DROP; |
| 147 | spin_unlock(&npinfo->poll_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | } |
| 149 | } |
| 150 | |
| 151 | void netpoll_poll(struct netpoll *np) |
| 152 | { |
| 153 | if(!np->dev || !netif_running(np->dev) || !np->dev->poll_controller) |
| 154 | return; |
| 155 | |
| 156 | /* Process pending work on NIC */ |
| 157 | np->dev->poll_controller(np->dev); |
| 158 | if (np->dev->poll) |
| 159 | poll_napi(np); |
| 160 | |
| 161 | zap_completion_queue(); |
| 162 | } |
| 163 | |
| 164 | static void refill_skbs(void) |
| 165 | { |
| 166 | struct sk_buff *skb; |
| 167 | unsigned long flags; |
| 168 | |
| 169 | spin_lock_irqsave(&skb_list_lock, flags); |
| 170 | while (nr_skbs < MAX_SKBS) { |
| 171 | skb = alloc_skb(MAX_SKB_SIZE, GFP_ATOMIC); |
| 172 | if (!skb) |
| 173 | break; |
| 174 | |
| 175 | skb->next = skbs; |
| 176 | skbs = skb; |
| 177 | nr_skbs++; |
| 178 | } |
| 179 | spin_unlock_irqrestore(&skb_list_lock, flags); |
| 180 | } |
| 181 | |
| 182 | static void zap_completion_queue(void) |
| 183 | { |
| 184 | unsigned long flags; |
| 185 | struct softnet_data *sd = &get_cpu_var(softnet_data); |
| 186 | |
| 187 | if (sd->completion_queue) { |
| 188 | struct sk_buff *clist; |
| 189 | |
| 190 | local_irq_save(flags); |
| 191 | clist = sd->completion_queue; |
| 192 | sd->completion_queue = NULL; |
| 193 | local_irq_restore(flags); |
| 194 | |
| 195 | while (clist != NULL) { |
| 196 | struct sk_buff *skb = clist; |
| 197 | clist = clist->next; |
| 198 | if(skb->destructor) |
| 199 | dev_kfree_skb_any(skb); /* put this one back */ |
| 200 | else |
| 201 | __kfree_skb(skb); |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | put_cpu_var(softnet_data); |
| 206 | } |
| 207 | |
| 208 | static struct sk_buff * find_skb(struct netpoll *np, int len, int reserve) |
| 209 | { |
| 210 | int once = 1, count = 0; |
| 211 | unsigned long flags; |
| 212 | struct sk_buff *skb = NULL; |
| 213 | |
| 214 | zap_completion_queue(); |
| 215 | repeat: |
| 216 | if (nr_skbs < MAX_SKBS) |
| 217 | refill_skbs(); |
| 218 | |
| 219 | skb = alloc_skb(len, GFP_ATOMIC); |
| 220 | |
| 221 | if (!skb) { |
| 222 | spin_lock_irqsave(&skb_list_lock, flags); |
| 223 | skb = skbs; |
| 224 | if (skb) { |
| 225 | skbs = skb->next; |
| 226 | skb->next = NULL; |
| 227 | nr_skbs--; |
| 228 | } |
| 229 | spin_unlock_irqrestore(&skb_list_lock, flags); |
| 230 | } |
| 231 | |
| 232 | if(!skb) { |
| 233 | count++; |
| 234 | if (once && (count == 1000000)) { |
| 235 | printk("out of netpoll skbs!\n"); |
| 236 | once = 0; |
| 237 | } |
| 238 | netpoll_poll(np); |
| 239 | goto repeat; |
| 240 | } |
| 241 | |
| 242 | atomic_set(&skb->users, 1); |
| 243 | skb_reserve(skb, reserve); |
| 244 | return skb; |
| 245 | } |
| 246 | |
| 247 | static void netpoll_send_skb(struct netpoll *np, struct sk_buff *skb) |
| 248 | { |
| 249 | int status; |
Jeff Moyer | 115c1d6 | 2005-06-22 22:05:31 -0700 | [diff] [blame] | 250 | struct netpoll_info *npinfo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | |
Matt Mackall | f0d3459 | 2005-08-11 19:25:11 -0700 | [diff] [blame] | 252 | if (!np || !np->dev || !netif_running(np->dev)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | __kfree_skb(skb); |
| 254 | return; |
| 255 | } |
| 256 | |
Jeff Moyer | 115c1d6 | 2005-06-22 22:05:31 -0700 | [diff] [blame] | 257 | npinfo = np->dev->npinfo; |
Matt Mackall | f0d3459 | 2005-08-11 19:25:11 -0700 | [diff] [blame] | 258 | |
| 259 | /* avoid recursion */ |
Jeff Moyer | 115c1d6 | 2005-06-22 22:05:31 -0700 | [diff] [blame] | 260 | if (npinfo->poll_owner == smp_processor_id() || |
| 261 | np->dev->xmit_lock_owner == smp_processor_id()) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | if (np->drop) |
| 263 | np->drop(skb); |
| 264 | else |
| 265 | __kfree_skb(skb); |
| 266 | return; |
| 267 | } |
| 268 | |
Matt Mackall | 0db1d6f | 2005-08-11 19:25:54 -0700 | [diff] [blame] | 269 | do { |
| 270 | npinfo->tries--; |
Matt Mackall | f0d3459 | 2005-08-11 19:25:11 -0700 | [diff] [blame] | 271 | spin_lock(&np->dev->xmit_lock); |
| 272 | np->dev->xmit_lock_owner = smp_processor_id(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | |
Matt Mackall | f0d3459 | 2005-08-11 19:25:11 -0700 | [diff] [blame] | 274 | /* |
| 275 | * network drivers do not expect to be called if the queue is |
| 276 | * stopped. |
| 277 | */ |
| 278 | if (netif_queue_stopped(np->dev)) { |
| 279 | np->dev->xmit_lock_owner = -1; |
| 280 | spin_unlock(&np->dev->xmit_lock); |
| 281 | netpoll_poll(np); |
Matt Mackall | 0db1d6f | 2005-08-11 19:25:54 -0700 | [diff] [blame] | 282 | udelay(50); |
Matt Mackall | f0d3459 | 2005-08-11 19:25:11 -0700 | [diff] [blame] | 283 | continue; |
| 284 | } |
| 285 | |
| 286 | status = np->dev->hard_start_xmit(skb, np->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 287 | np->dev->xmit_lock_owner = -1; |
| 288 | spin_unlock(&np->dev->xmit_lock); |
| 289 | |
Matt Mackall | f0d3459 | 2005-08-11 19:25:11 -0700 | [diff] [blame] | 290 | /* success */ |
Matt Mackall | 0db1d6f | 2005-08-11 19:25:54 -0700 | [diff] [blame] | 291 | if(!status) { |
| 292 | npinfo->tries = MAX_RETRIES; /* reset */ |
Matt Mackall | f0d3459 | 2005-08-11 19:25:11 -0700 | [diff] [blame] | 293 | return; |
Matt Mackall | 0db1d6f | 2005-08-11 19:25:54 -0700 | [diff] [blame] | 294 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | |
Matt Mackall | f0d3459 | 2005-08-11 19:25:11 -0700 | [diff] [blame] | 296 | /* transmit busy */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | netpoll_poll(np); |
Matt Mackall | 0db1d6f | 2005-08-11 19:25:54 -0700 | [diff] [blame] | 298 | udelay(50); |
| 299 | } while (npinfo->tries > 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | void netpoll_send_udp(struct netpoll *np, const char *msg, int len) |
| 303 | { |
| 304 | int total_len, eth_len, ip_len, udp_len; |
| 305 | struct sk_buff *skb; |
| 306 | struct udphdr *udph; |
| 307 | struct iphdr *iph; |
| 308 | struct ethhdr *eth; |
| 309 | |
| 310 | udp_len = len + sizeof(*udph); |
| 311 | ip_len = eth_len = udp_len + sizeof(*iph); |
| 312 | total_len = eth_len + ETH_HLEN + NET_IP_ALIGN; |
| 313 | |
| 314 | skb = find_skb(np, total_len, total_len - len); |
| 315 | if (!skb) |
| 316 | return; |
| 317 | |
| 318 | memcpy(skb->data, msg, len); |
| 319 | skb->len += len; |
| 320 | |
| 321 | udph = (struct udphdr *) skb_push(skb, sizeof(*udph)); |
| 322 | udph->source = htons(np->local_port); |
| 323 | udph->dest = htons(np->remote_port); |
| 324 | udph->len = htons(udp_len); |
| 325 | udph->check = 0; |
| 326 | |
| 327 | iph = (struct iphdr *)skb_push(skb, sizeof(*iph)); |
| 328 | |
| 329 | /* iph->version = 4; iph->ihl = 5; */ |
| 330 | put_unaligned(0x45, (unsigned char *)iph); |
| 331 | iph->tos = 0; |
| 332 | put_unaligned(htons(ip_len), &(iph->tot_len)); |
| 333 | iph->id = 0; |
| 334 | iph->frag_off = 0; |
| 335 | iph->ttl = 64; |
| 336 | iph->protocol = IPPROTO_UDP; |
| 337 | iph->check = 0; |
| 338 | put_unaligned(htonl(np->local_ip), &(iph->saddr)); |
| 339 | put_unaligned(htonl(np->remote_ip), &(iph->daddr)); |
| 340 | iph->check = ip_fast_csum((unsigned char *)iph, iph->ihl); |
| 341 | |
| 342 | eth = (struct ethhdr *) skb_push(skb, ETH_HLEN); |
| 343 | |
| 344 | eth->h_proto = htons(ETH_P_IP); |
| 345 | memcpy(eth->h_source, np->local_mac, 6); |
| 346 | memcpy(eth->h_dest, np->remote_mac, 6); |
| 347 | |
| 348 | skb->dev = np->dev; |
| 349 | |
| 350 | netpoll_send_skb(np, skb); |
| 351 | } |
| 352 | |
| 353 | static void arp_reply(struct sk_buff *skb) |
| 354 | { |
Jeff Moyer | 115c1d6 | 2005-06-22 22:05:31 -0700 | [diff] [blame] | 355 | struct netpoll_info *npinfo = skb->dev->npinfo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 356 | struct arphdr *arp; |
| 357 | unsigned char *arp_ptr; |
| 358 | int size, type = ARPOP_REPLY, ptype = ETH_P_ARP; |
| 359 | u32 sip, tip; |
Jeff Moyer | fbeec2e | 2005-06-22 22:05:59 -0700 | [diff] [blame] | 360 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 361 | struct sk_buff *send_skb; |
Jeff Moyer | 115c1d6 | 2005-06-22 22:05:31 -0700 | [diff] [blame] | 362 | struct netpoll *np = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 363 | |
Jeff Moyer | fbeec2e | 2005-06-22 22:05:59 -0700 | [diff] [blame] | 364 | if (npinfo->rx_np && npinfo->rx_np->dev == skb->dev) |
| 365 | np = npinfo->rx_np; |
Jeff Moyer | 115c1d6 | 2005-06-22 22:05:31 -0700 | [diff] [blame] | 366 | if (!np) |
| 367 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 368 | |
| 369 | /* No arp on this interface */ |
| 370 | if (skb->dev->flags & IFF_NOARP) |
| 371 | return; |
| 372 | |
| 373 | if (!pskb_may_pull(skb, (sizeof(struct arphdr) + |
| 374 | (2 * skb->dev->addr_len) + |
| 375 | (2 * sizeof(u32))))) |
| 376 | return; |
| 377 | |
| 378 | skb->h.raw = skb->nh.raw = skb->data; |
| 379 | arp = skb->nh.arph; |
| 380 | |
| 381 | if ((arp->ar_hrd != htons(ARPHRD_ETHER) && |
| 382 | arp->ar_hrd != htons(ARPHRD_IEEE802)) || |
| 383 | arp->ar_pro != htons(ETH_P_IP) || |
| 384 | arp->ar_op != htons(ARPOP_REQUEST)) |
| 385 | return; |
| 386 | |
| 387 | arp_ptr = (unsigned char *)(arp+1) + skb->dev->addr_len; |
| 388 | memcpy(&sip, arp_ptr, 4); |
| 389 | arp_ptr += 4 + skb->dev->addr_len; |
| 390 | memcpy(&tip, arp_ptr, 4); |
| 391 | |
| 392 | /* Should we ignore arp? */ |
| 393 | if (tip != htonl(np->local_ip) || LOOPBACK(tip) || MULTICAST(tip)) |
| 394 | return; |
| 395 | |
| 396 | size = sizeof(struct arphdr) + 2 * (skb->dev->addr_len + 4); |
| 397 | send_skb = find_skb(np, size + LL_RESERVED_SPACE(np->dev), |
| 398 | LL_RESERVED_SPACE(np->dev)); |
| 399 | |
| 400 | if (!send_skb) |
| 401 | return; |
| 402 | |
| 403 | send_skb->nh.raw = send_skb->data; |
| 404 | arp = (struct arphdr *) skb_put(send_skb, size); |
| 405 | send_skb->dev = skb->dev; |
| 406 | send_skb->protocol = htons(ETH_P_ARP); |
| 407 | |
| 408 | /* Fill the device header for the ARP frame */ |
| 409 | |
| 410 | if (np->dev->hard_header && |
| 411 | np->dev->hard_header(send_skb, skb->dev, ptype, |
| 412 | np->remote_mac, np->local_mac, |
| 413 | send_skb->len) < 0) { |
| 414 | kfree_skb(send_skb); |
| 415 | return; |
| 416 | } |
| 417 | |
| 418 | /* |
| 419 | * Fill out the arp protocol part. |
| 420 | * |
| 421 | * we only support ethernet device type, |
| 422 | * which (according to RFC 1390) should always equal 1 (Ethernet). |
| 423 | */ |
| 424 | |
| 425 | arp->ar_hrd = htons(np->dev->type); |
| 426 | arp->ar_pro = htons(ETH_P_IP); |
| 427 | arp->ar_hln = np->dev->addr_len; |
| 428 | arp->ar_pln = 4; |
| 429 | arp->ar_op = htons(type); |
| 430 | |
| 431 | arp_ptr=(unsigned char *)(arp + 1); |
| 432 | memcpy(arp_ptr, np->dev->dev_addr, np->dev->addr_len); |
| 433 | arp_ptr += np->dev->addr_len; |
| 434 | memcpy(arp_ptr, &tip, 4); |
| 435 | arp_ptr += 4; |
| 436 | memcpy(arp_ptr, np->remote_mac, np->dev->addr_len); |
| 437 | arp_ptr += np->dev->addr_len; |
| 438 | memcpy(arp_ptr, &sip, 4); |
| 439 | |
| 440 | netpoll_send_skb(np, send_skb); |
| 441 | } |
| 442 | |
| 443 | int __netpoll_rx(struct sk_buff *skb) |
| 444 | { |
| 445 | int proto, len, ulen; |
| 446 | struct iphdr *iph; |
| 447 | struct udphdr *uh; |
Jeff Moyer | fbeec2e | 2005-06-22 22:05:59 -0700 | [diff] [blame] | 448 | struct netpoll *np = skb->dev->npinfo->rx_np; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 449 | |
Jeff Moyer | fbeec2e | 2005-06-22 22:05:59 -0700 | [diff] [blame] | 450 | if (!np) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 451 | goto out; |
| 452 | if (skb->dev->type != ARPHRD_ETHER) |
| 453 | goto out; |
| 454 | |
| 455 | /* check if netpoll clients need ARP */ |
| 456 | if (skb->protocol == __constant_htons(ETH_P_ARP) && |
| 457 | atomic_read(&trapped)) { |
| 458 | arp_reply(skb); |
| 459 | return 1; |
| 460 | } |
| 461 | |
| 462 | proto = ntohs(eth_hdr(skb)->h_proto); |
| 463 | if (proto != ETH_P_IP) |
| 464 | goto out; |
| 465 | if (skb->pkt_type == PACKET_OTHERHOST) |
| 466 | goto out; |
| 467 | if (skb_shared(skb)) |
| 468 | goto out; |
| 469 | |
| 470 | iph = (struct iphdr *)skb->data; |
| 471 | if (!pskb_may_pull(skb, sizeof(struct iphdr))) |
| 472 | goto out; |
| 473 | if (iph->ihl < 5 || iph->version != 4) |
| 474 | goto out; |
| 475 | if (!pskb_may_pull(skb, iph->ihl*4)) |
| 476 | goto out; |
| 477 | if (ip_fast_csum((u8 *)iph, iph->ihl) != 0) |
| 478 | goto out; |
| 479 | |
| 480 | len = ntohs(iph->tot_len); |
| 481 | if (skb->len < len || len < iph->ihl*4) |
| 482 | goto out; |
| 483 | |
| 484 | if (iph->protocol != IPPROTO_UDP) |
| 485 | goto out; |
| 486 | |
| 487 | len -= iph->ihl*4; |
| 488 | uh = (struct udphdr *)(((char *)iph) + iph->ihl*4); |
| 489 | ulen = ntohs(uh->len); |
| 490 | |
| 491 | if (ulen != len) |
| 492 | goto out; |
| 493 | if (checksum_udp(skb, uh, ulen, iph->saddr, iph->daddr) < 0) |
| 494 | goto out; |
| 495 | if (np->local_ip && np->local_ip != ntohl(iph->daddr)) |
| 496 | goto out; |
| 497 | if (np->remote_ip && np->remote_ip != ntohl(iph->saddr)) |
| 498 | goto out; |
| 499 | if (np->local_port && np->local_port != ntohs(uh->dest)) |
| 500 | goto out; |
| 501 | |
| 502 | np->rx_hook(np, ntohs(uh->source), |
| 503 | (char *)(uh+1), |
| 504 | ulen - sizeof(struct udphdr)); |
| 505 | |
| 506 | kfree_skb(skb); |
| 507 | return 1; |
| 508 | |
| 509 | out: |
| 510 | if (atomic_read(&trapped)) { |
| 511 | kfree_skb(skb); |
| 512 | return 1; |
| 513 | } |
| 514 | |
| 515 | return 0; |
| 516 | } |
| 517 | |
| 518 | int netpoll_parse_options(struct netpoll *np, char *opt) |
| 519 | { |
| 520 | char *cur=opt, *delim; |
| 521 | |
| 522 | if(*cur != '@') { |
| 523 | if ((delim = strchr(cur, '@')) == NULL) |
| 524 | goto parse_failed; |
| 525 | *delim=0; |
| 526 | np->local_port=simple_strtol(cur, NULL, 10); |
| 527 | cur=delim; |
| 528 | } |
| 529 | cur++; |
| 530 | printk(KERN_INFO "%s: local port %d\n", np->name, np->local_port); |
| 531 | |
| 532 | if(*cur != '/') { |
| 533 | if ((delim = strchr(cur, '/')) == NULL) |
| 534 | goto parse_failed; |
| 535 | *delim=0; |
| 536 | np->local_ip=ntohl(in_aton(cur)); |
| 537 | cur=delim; |
| 538 | |
| 539 | printk(KERN_INFO "%s: local IP %d.%d.%d.%d\n", |
| 540 | np->name, HIPQUAD(np->local_ip)); |
| 541 | } |
| 542 | cur++; |
| 543 | |
| 544 | if ( *cur != ',') { |
| 545 | /* parse out dev name */ |
| 546 | if ((delim = strchr(cur, ',')) == NULL) |
| 547 | goto parse_failed; |
| 548 | *delim=0; |
| 549 | strlcpy(np->dev_name, cur, sizeof(np->dev_name)); |
| 550 | cur=delim; |
| 551 | } |
| 552 | cur++; |
| 553 | |
| 554 | printk(KERN_INFO "%s: interface %s\n", np->name, np->dev_name); |
| 555 | |
| 556 | if ( *cur != '@' ) { |
| 557 | /* dst port */ |
| 558 | if ((delim = strchr(cur, '@')) == NULL) |
| 559 | goto parse_failed; |
| 560 | *delim=0; |
| 561 | np->remote_port=simple_strtol(cur, NULL, 10); |
| 562 | cur=delim; |
| 563 | } |
| 564 | cur++; |
| 565 | printk(KERN_INFO "%s: remote port %d\n", np->name, np->remote_port); |
| 566 | |
| 567 | /* dst ip */ |
| 568 | if ((delim = strchr(cur, '/')) == NULL) |
| 569 | goto parse_failed; |
| 570 | *delim=0; |
| 571 | np->remote_ip=ntohl(in_aton(cur)); |
| 572 | cur=delim+1; |
| 573 | |
| 574 | printk(KERN_INFO "%s: remote IP %d.%d.%d.%d\n", |
| 575 | np->name, HIPQUAD(np->remote_ip)); |
| 576 | |
| 577 | if( *cur != 0 ) |
| 578 | { |
| 579 | /* MAC address */ |
| 580 | if ((delim = strchr(cur, ':')) == NULL) |
| 581 | goto parse_failed; |
| 582 | *delim=0; |
| 583 | np->remote_mac[0]=simple_strtol(cur, NULL, 16); |
| 584 | cur=delim+1; |
| 585 | if ((delim = strchr(cur, ':')) == NULL) |
| 586 | goto parse_failed; |
| 587 | *delim=0; |
| 588 | np->remote_mac[1]=simple_strtol(cur, NULL, 16); |
| 589 | cur=delim+1; |
| 590 | if ((delim = strchr(cur, ':')) == NULL) |
| 591 | goto parse_failed; |
| 592 | *delim=0; |
| 593 | np->remote_mac[2]=simple_strtol(cur, NULL, 16); |
| 594 | cur=delim+1; |
| 595 | if ((delim = strchr(cur, ':')) == NULL) |
| 596 | goto parse_failed; |
| 597 | *delim=0; |
| 598 | np->remote_mac[3]=simple_strtol(cur, NULL, 16); |
| 599 | cur=delim+1; |
| 600 | if ((delim = strchr(cur, ':')) == NULL) |
| 601 | goto parse_failed; |
| 602 | *delim=0; |
| 603 | np->remote_mac[4]=simple_strtol(cur, NULL, 16); |
| 604 | cur=delim+1; |
| 605 | np->remote_mac[5]=simple_strtol(cur, NULL, 16); |
| 606 | } |
| 607 | |
| 608 | printk(KERN_INFO "%s: remote ethernet address " |
| 609 | "%02x:%02x:%02x:%02x:%02x:%02x\n", |
| 610 | np->name, |
| 611 | np->remote_mac[0], |
| 612 | np->remote_mac[1], |
| 613 | np->remote_mac[2], |
| 614 | np->remote_mac[3], |
| 615 | np->remote_mac[4], |
| 616 | np->remote_mac[5]); |
| 617 | |
| 618 | return 0; |
| 619 | |
| 620 | parse_failed: |
| 621 | printk(KERN_INFO "%s: couldn't parse config at %s!\n", |
| 622 | np->name, cur); |
| 623 | return -1; |
| 624 | } |
| 625 | |
| 626 | int netpoll_setup(struct netpoll *np) |
| 627 | { |
| 628 | struct net_device *ndev = NULL; |
| 629 | struct in_device *in_dev; |
Jeff Moyer | 115c1d6 | 2005-06-22 22:05:31 -0700 | [diff] [blame] | 630 | struct netpoll_info *npinfo; |
Jeff Moyer | fbeec2e | 2005-06-22 22:05:59 -0700 | [diff] [blame] | 631 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 632 | |
| 633 | if (np->dev_name) |
| 634 | ndev = dev_get_by_name(np->dev_name); |
| 635 | if (!ndev) { |
| 636 | printk(KERN_ERR "%s: %s doesn't exist, aborting.\n", |
| 637 | np->name, np->dev_name); |
| 638 | return -1; |
| 639 | } |
| 640 | |
| 641 | np->dev = ndev; |
Jeff Moyer | 115c1d6 | 2005-06-22 22:05:31 -0700 | [diff] [blame] | 642 | if (!ndev->npinfo) { |
| 643 | npinfo = kmalloc(sizeof(*npinfo), GFP_KERNEL); |
| 644 | if (!npinfo) |
| 645 | goto release; |
| 646 | |
Jeff Moyer | 1151312 | 2005-08-11 19:23:04 -0700 | [diff] [blame] | 647 | npinfo->rx_flags = 0; |
Jeff Moyer | fbeec2e | 2005-06-22 22:05:59 -0700 | [diff] [blame] | 648 | npinfo->rx_np = NULL; |
Jeff Moyer | 115c1d6 | 2005-06-22 22:05:31 -0700 | [diff] [blame] | 649 | npinfo->poll_lock = SPIN_LOCK_UNLOCKED; |
| 650 | npinfo->poll_owner = -1; |
Matt Mackall | 0db1d6f | 2005-08-11 19:25:54 -0700 | [diff] [blame] | 651 | npinfo->tries = MAX_RETRIES; |
Jeff Moyer | fbeec2e | 2005-06-22 22:05:59 -0700 | [diff] [blame] | 652 | npinfo->rx_lock = SPIN_LOCK_UNLOCKED; |
Jeff Moyer | 115c1d6 | 2005-06-22 22:05:31 -0700 | [diff] [blame] | 653 | } else |
| 654 | npinfo = ndev->npinfo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 655 | |
| 656 | if (!ndev->poll_controller) { |
| 657 | printk(KERN_ERR "%s: %s doesn't support polling, aborting.\n", |
| 658 | np->name, np->dev_name); |
| 659 | goto release; |
| 660 | } |
| 661 | |
| 662 | if (!netif_running(ndev)) { |
| 663 | unsigned long atmost, atleast; |
| 664 | |
| 665 | printk(KERN_INFO "%s: device %s not up yet, forcing it\n", |
| 666 | np->name, np->dev_name); |
| 667 | |
| 668 | rtnl_shlock(); |
| 669 | if (dev_change_flags(ndev, ndev->flags | IFF_UP) < 0) { |
| 670 | printk(KERN_ERR "%s: failed to open %s\n", |
| 671 | np->name, np->dev_name); |
| 672 | rtnl_shunlock(); |
| 673 | goto release; |
| 674 | } |
| 675 | rtnl_shunlock(); |
| 676 | |
| 677 | atleast = jiffies + HZ/10; |
| 678 | atmost = jiffies + 4*HZ; |
| 679 | while (!netif_carrier_ok(ndev)) { |
| 680 | if (time_after(jiffies, atmost)) { |
| 681 | printk(KERN_NOTICE |
| 682 | "%s: timeout waiting for carrier\n", |
| 683 | np->name); |
| 684 | break; |
| 685 | } |
| 686 | cond_resched(); |
| 687 | } |
| 688 | |
| 689 | /* If carrier appears to come up instantly, we don't |
| 690 | * trust it and pause so that we don't pump all our |
| 691 | * queued console messages into the bitbucket. |
| 692 | */ |
| 693 | |
| 694 | if (time_before(jiffies, atleast)) { |
| 695 | printk(KERN_NOTICE "%s: carrier detect appears" |
| 696 | " untrustworthy, waiting 4 seconds\n", |
| 697 | np->name); |
| 698 | msleep(4000); |
| 699 | } |
| 700 | } |
| 701 | |
| 702 | if (!memcmp(np->local_mac, "\0\0\0\0\0\0", 6) && ndev->dev_addr) |
| 703 | memcpy(np->local_mac, ndev->dev_addr, 6); |
| 704 | |
| 705 | if (!np->local_ip) { |
| 706 | rcu_read_lock(); |
| 707 | in_dev = __in_dev_get(ndev); |
| 708 | |
| 709 | if (!in_dev || !in_dev->ifa_list) { |
| 710 | rcu_read_unlock(); |
| 711 | printk(KERN_ERR "%s: no IP address for %s, aborting\n", |
| 712 | np->name, np->dev_name); |
| 713 | goto release; |
| 714 | } |
| 715 | |
| 716 | np->local_ip = ntohl(in_dev->ifa_list->ifa_local); |
| 717 | rcu_read_unlock(); |
| 718 | printk(KERN_INFO "%s: local IP %d.%d.%d.%d\n", |
| 719 | np->name, HIPQUAD(np->local_ip)); |
| 720 | } |
| 721 | |
Jeff Moyer | fbeec2e | 2005-06-22 22:05:59 -0700 | [diff] [blame] | 722 | if (np->rx_hook) { |
| 723 | spin_lock_irqsave(&npinfo->rx_lock, flags); |
| 724 | npinfo->rx_flags |= NETPOLL_RX_ENABLED; |
| 725 | npinfo->rx_np = np; |
| 726 | spin_unlock_irqrestore(&npinfo->rx_lock, flags); |
| 727 | } |
Ingo Molnar | 2652076 | 2005-08-11 19:26:42 -0700 | [diff] [blame^] | 728 | |
| 729 | /* fill up the skb queue */ |
| 730 | refill_skbs(); |
| 731 | |
Jeff Moyer | fbeec2e | 2005-06-22 22:05:59 -0700 | [diff] [blame] | 732 | /* last thing to do is link it to the net device structure */ |
Jeff Moyer | 115c1d6 | 2005-06-22 22:05:31 -0700 | [diff] [blame] | 733 | ndev->npinfo = npinfo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 734 | |
| 735 | return 0; |
| 736 | |
| 737 | release: |
Jeff Moyer | 115c1d6 | 2005-06-22 22:05:31 -0700 | [diff] [blame] | 738 | if (!ndev->npinfo) |
| 739 | kfree(npinfo); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 740 | np->dev = NULL; |
| 741 | dev_put(ndev); |
| 742 | return -1; |
| 743 | } |
| 744 | |
| 745 | void netpoll_cleanup(struct netpoll *np) |
| 746 | { |
Jeff Moyer | fbeec2e | 2005-06-22 22:05:59 -0700 | [diff] [blame] | 747 | struct netpoll_info *npinfo; |
| 748 | unsigned long flags; |
| 749 | |
Jeff Moyer | 115c1d6 | 2005-06-22 22:05:31 -0700 | [diff] [blame] | 750 | if (np->dev) { |
Jeff Moyer | fbeec2e | 2005-06-22 22:05:59 -0700 | [diff] [blame] | 751 | npinfo = np->dev->npinfo; |
| 752 | if (npinfo && npinfo->rx_np == np) { |
| 753 | spin_lock_irqsave(&npinfo->rx_lock, flags); |
| 754 | npinfo->rx_np = NULL; |
| 755 | npinfo->rx_flags &= ~NETPOLL_RX_ENABLED; |
| 756 | spin_unlock_irqrestore(&npinfo->rx_lock, flags); |
| 757 | } |
Jeff Moyer | 115c1d6 | 2005-06-22 22:05:31 -0700 | [diff] [blame] | 758 | dev_put(np->dev); |
| 759 | } |
Jeff Moyer | fbeec2e | 2005-06-22 22:05:59 -0700 | [diff] [blame] | 760 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 761 | np->dev = NULL; |
| 762 | } |
| 763 | |
| 764 | int netpoll_trap(void) |
| 765 | { |
| 766 | return atomic_read(&trapped); |
| 767 | } |
| 768 | |
| 769 | void netpoll_set_trap(int trap) |
| 770 | { |
| 771 | if (trap) |
| 772 | atomic_inc(&trapped); |
| 773 | else |
| 774 | atomic_dec(&trapped); |
| 775 | } |
| 776 | |
| 777 | EXPORT_SYMBOL(netpoll_set_trap); |
| 778 | EXPORT_SYMBOL(netpoll_trap); |
| 779 | EXPORT_SYMBOL(netpoll_parse_options); |
| 780 | EXPORT_SYMBOL(netpoll_setup); |
| 781 | EXPORT_SYMBOL(netpoll_cleanup); |
| 782 | EXPORT_SYMBOL(netpoll_send_udp); |
| 783 | EXPORT_SYMBOL(netpoll_poll); |
| 784 | EXPORT_SYMBOL(netpoll_queue); |