fedcca83d07bd88f46cf5ec29047f21025d09df6
[sliver-openvswitch.git] / datapath / forward.c
1 /*
2  * Distributed under the terms of the GNU GPL version 2.
3  * Copyright (c) 2007, 2008 The Board of Trustees of The Leland 
4  * Stanford Junior University
5  */
6
7 #include <linux/if_ether.h>
8 #include <linux/if_vlan.h>
9 #include <linux/in.h>
10 #include <linux/ip.h>
11 #include <linux/tcp.h>
12 #include <linux/udp.h>
13 #include <linux/in6.h>
14 #include <asm/uaccess.h>
15 #include <linux/types.h>
16 #include <net/checksum.h>
17 #include "forward.h"
18 #include "datapath.h"
19 #include "chain.h"
20 #include "flow.h"
21
22 /* FIXME: do we need to use GFP_ATOMIC everywhere here? */
23
24 static int make_writable(struct sk_buff **);
25
26 static struct sk_buff *retrieve_skb(uint32_t id);
27 static void discard_skb(uint32_t id);
28
29 /* 'skb' was received on 'in_port', a physical switch port between 0 and
30  * OFPP_MAX.  Process it according to 'chain'.  Returns 0 if successful, in
31  * which case 'skb' is destroyed, or -ESRCH if there is no matching flow, in
32  * which case 'skb' still belongs to the caller. */
33 int run_flow_through_tables(struct sw_chain *chain, struct sk_buff *skb,
34                             int in_port)
35 {
36         struct sw_flow_key key;
37         struct sw_flow *flow;
38
39         if (flow_extract(skb, in_port, &key)
40             && (chain->dp->flags & OFPC_FRAG_MASK) == OFPC_FRAG_DROP) {
41                 /* Drop fragment. */
42                 kfree_skb(skb);
43                 return 0;
44         }
45
46         flow = chain_lookup(chain, &key);
47         if (likely(flow != NULL)) {
48                 flow_used(flow, skb);
49                 execute_actions(chain->dp, skb, &key,
50                                 flow->actions, flow->n_actions);
51                 return 0;
52         } else {
53                 return -ESRCH;
54         }
55 }
56
57 /* 'skb' was received on 'in_port', a physical switch port between 0 and
58  * OFPP_MAX.  Process it according to 'chain', sending it up to the controller
59  * if no flow matches.  Takes ownership of 'skb'. */
60 void fwd_port_input(struct sw_chain *chain, struct sk_buff *skb, int in_port)
61 {
62         if (run_flow_through_tables(chain, skb, in_port))
63                 dp_output_control(chain->dp, skb, fwd_save_skb(skb), 
64                                   chain->dp->miss_send_len,
65                                   OFPR_NO_MATCH);
66 }
67
68 static int do_output(struct datapath *dp, struct sk_buff *skb, size_t max_len,
69                         int out_port)
70 {
71         if (!skb)
72                 return -ENOMEM;
73         return (likely(out_port != OFPP_CONTROLLER)
74                 ? dp_output_port(dp, skb, out_port)
75                 : dp_output_control(dp, skb, fwd_save_skb(skb),
76                                          max_len, OFPR_ACTION));
77 }
78
79 void execute_actions(struct datapath *dp, struct sk_buff *skb,
80                                 const struct sw_flow_key *key,
81                                 const struct ofp_action *actions, int n_actions)
82 {
83         /* Every output action needs a separate clone of 'skb', but the common
84          * case is just a single output action, so that doing a clone and
85          * then freeing the original skbuff is wasteful.  So the following code
86          * is slightly obscure just to avoid that. */
87         int prev_port;
88         size_t max_len=0;        /* Initialze to make compiler happy */
89         uint16_t eth_proto;
90         int i;
91
92         prev_port = -1;
93         eth_proto = ntohs(key->dl_type);
94
95         for (i = 0; i < n_actions; i++) {
96                 const struct ofp_action *a = &actions[i];
97
98                 if (prev_port != -1) {
99                         do_output(dp, skb_clone(skb, GFP_ATOMIC),
100                                   max_len, prev_port);
101                         prev_port = -1;
102                 }
103
104                 if (likely(a->type == htons(OFPAT_OUTPUT))) {
105                         prev_port = ntohs(a->arg.output.port);
106                         max_len = ntohs(a->arg.output.max_len);
107                 } else {
108                         if (!make_writable(&skb)) {
109                                 if (net_ratelimit())
110                                     printk("make_writable failed\n");
111                                 break;
112                         }
113                         skb = execute_setter(skb, eth_proto, key, a);
114                         if (!skb) {
115                                 if (net_ratelimit())
116                                         printk("execute_setter lost skb\n");
117                                 return;
118                         }
119                 }
120         }
121         if (prev_port != -1)
122                 do_output(dp, skb, max_len, prev_port);
123         else
124                 kfree_skb(skb);
125 }
126
127 /* Updates 'sum', which is a field in 'skb''s data, given that a 4-byte field
128  * covered by the sum has been changed from 'from' to 'to'.  If set,
129  * 'pseudohdr' indicates that the field is in the TCP or UDP pseudo-header.
130  * Based on nf_proto_csum_replace4. */
131 static void update_csum(__sum16 *sum, struct sk_buff *skb,
132                         __be32 from, __be32 to, int pseudohdr)
133 {
134         __be32 diff[] = { ~from, to };
135         if (skb->ip_summed != CHECKSUM_PARTIAL) {
136                 *sum = csum_fold(csum_partial((char *)diff, sizeof(diff),
137                                 ~csum_unfold(*sum)));
138                 if (skb->ip_summed == CHECKSUM_COMPLETE && pseudohdr)
139                         skb->csum = ~csum_partial((char *)diff, sizeof(diff),
140                                                 ~skb->csum);
141         } else if (pseudohdr)
142                 *sum = ~csum_fold(csum_partial((char *)diff, sizeof(diff),
143                                 csum_unfold(*sum)));
144 }
145
146 static void modify_nh(struct sk_buff *skb, uint16_t eth_proto,
147                         uint8_t nw_proto, const struct ofp_action *a)
148 {
149         if (eth_proto == ETH_P_IP) {
150                 struct iphdr *nh = ip_hdr(skb);
151                 uint32_t new, *field;
152
153                 new = a->arg.nw_addr;
154
155                 if (a->type == htons(OFPAT_SET_NW_SRC))
156                         field = &nh->saddr;
157                 else
158                         field = &nh->daddr;
159
160                 if (nw_proto == IPPROTO_TCP) {
161                         struct tcphdr *th = tcp_hdr(skb);
162                         update_csum(&th->check, skb, *field, new, 1);
163                 } else if (nw_proto == IPPROTO_UDP) {
164                         struct udphdr *th = udp_hdr(skb);
165                         update_csum(&th->check, skb, *field, new, 1);
166                 }
167                 update_csum(&nh->check, skb, *field, new, 0);
168                 *field = new;
169         }
170 }
171
172 static void modify_th(struct sk_buff *skb, uint16_t eth_proto,
173                         uint8_t nw_proto, const struct ofp_action *a)
174 {
175         if (eth_proto == ETH_P_IP) {
176                 uint16_t new, *field;
177
178                 new = a->arg.tp;
179
180                 if (nw_proto == IPPROTO_TCP) {
181                         struct tcphdr *th = tcp_hdr(skb);
182
183                         if (a->type == htons(OFPAT_SET_TP_SRC))
184                                 field = &th->source;
185                         else
186                                 field = &th->dest;
187
188                         update_csum(&th->check, skb, *field, new, 1);
189                         *field = new;
190                 } else if (nw_proto == IPPROTO_UDP) {
191                         struct udphdr *th = udp_hdr(skb);
192
193                         if (a->type == htons(OFPAT_SET_TP_SRC))
194                                 field = &th->source;
195                         else
196                                 field = &th->dest;
197
198                         update_csum(&th->check, skb, *field, new, 1);
199                         *field = new;
200                 }
201         }
202 }
203
204 static struct sk_buff *vlan_pull_tag(struct sk_buff *skb)
205 {
206         struct vlan_ethhdr *vh = vlan_eth_hdr(skb);
207         struct ethhdr *eh;
208
209
210         /* Verify we were given a vlan packet */
211         if (vh->h_vlan_proto != htons(ETH_P_8021Q))
212                 return skb;
213
214         memmove(skb->data + VLAN_HLEN, skb->data, 2 * VLAN_ETH_ALEN);
215
216         eh = (struct ethhdr *)skb_pull(skb, VLAN_HLEN);
217
218         skb->protocol = eh->h_proto;
219         skb->mac_header += VLAN_HLEN;
220
221         return skb;
222 }
223
224 static struct sk_buff *modify_vlan(struct sk_buff *skb, 
225                 const struct sw_flow_key *key, const struct ofp_action *a)
226 {
227         uint16_t new_id = ntohs(a->arg.vlan_id);
228
229         if (new_id != OFP_VLAN_NONE) {
230                 if (key->dl_vlan != htons(OFP_VLAN_NONE)) {
231                         /* Modify vlan id, but maintain other TCI values */
232                         struct vlan_ethhdr *vh = vlan_eth_hdr(skb);
233                         vh->h_vlan_TCI = (vh->h_vlan_TCI 
234                                         & ~(htons(VLAN_VID_MASK))) | a->arg.vlan_id;
235                 } else  {
236                         /* Add vlan header */
237
238                         /* xxx The vlan_put_tag function, doesn't seem to work
239                          * xxx reliably when it attempts to use the hardware-accelerated
240                          * xxx version.  We'll directly use the software version
241                          * xxx until the problem can be diagnosed.
242                          */
243                         skb = __vlan_put_tag(skb, new_id);
244                 }
245         } else  {
246                 /* Remove an existing vlan header if it exists */
247                 vlan_pull_tag(skb);
248         }
249
250         return skb;
251 }
252
253 struct sk_buff *execute_setter(struct sk_buff *skb, uint16_t eth_proto,
254                         const struct sw_flow_key *key, const struct ofp_action *a)
255 {
256         switch (ntohs(a->type)) {
257         case OFPAT_SET_DL_VLAN:
258                 skb = modify_vlan(skb, key, a);
259                 break;
260
261         case OFPAT_SET_DL_SRC: {
262                 struct ethhdr *eh = eth_hdr(skb);
263                 memcpy(eh->h_source, a->arg.dl_addr, sizeof eh->h_source);
264                 break;
265         }
266         case OFPAT_SET_DL_DST: {
267                 struct ethhdr *eh = eth_hdr(skb);
268                 memcpy(eh->h_dest, a->arg.dl_addr, sizeof eh->h_dest);
269                 break;
270         }
271
272         case OFPAT_SET_NW_SRC:
273         case OFPAT_SET_NW_DST:
274                 modify_nh(skb, eth_proto, key->nw_proto, a);
275                 break;
276
277         case OFPAT_SET_TP_SRC:
278         case OFPAT_SET_TP_DST:
279                 modify_th(skb, eth_proto, key->nw_proto, a);
280                 break;
281         
282         default:
283                 if (net_ratelimit())
284                         printk("execute_setter: unknown action: %d\n", ntohs(a->type));
285         }
286
287         return skb;
288 }
289
290 static int
291 recv_features_request(struct sw_chain *chain, const struct sender *sender,
292                       const void *msg) 
293 {
294         return dp_send_features_reply(chain->dp, sender);
295 }
296
297 static int
298 recv_get_config_request(struct sw_chain *chain, const struct sender *sender,
299                         const void *msg)
300 {
301         return dp_send_config_reply(chain->dp, sender);
302 }
303
304 static int
305 recv_set_config(struct sw_chain *chain, const struct sender *sender,
306                 const void *msg)
307 {
308         const struct ofp_switch_config *osc = msg;
309         int flags;
310
311         flags = ntohs(osc->flags) & (OFPC_SEND_FLOW_EXP | OFPC_FRAG_MASK);
312         if ((flags & OFPC_FRAG_MASK) != OFPC_FRAG_NORMAL
313             && (flags & OFPC_FRAG_MASK) != OFPC_FRAG_DROP) {
314                 flags = (flags & ~OFPC_FRAG_MASK) | OFPC_FRAG_DROP;
315         }
316         chain->dp->flags = flags;
317
318         chain->dp->miss_send_len = ntohs(osc->miss_send_len);
319
320         return 0;
321 }
322
323 static int
324 recv_packet_out(struct sw_chain *chain, const struct sender *sender,
325                 const void *msg)
326 {
327         const struct ofp_packet_out *opo = msg;
328         struct sk_buff *skb;
329         struct vlan_ethhdr *mac;
330         int nh_ofs;
331
332         if (ntohl(opo->buffer_id) == (uint32_t) -1) {
333                 int data_len = ntohs(opo->header.length) - sizeof *opo;
334
335                 /* FIXME: there is likely a way to reuse the data in msg. */
336                 skb = alloc_skb(data_len, GFP_ATOMIC);
337                 if (!skb)
338                         return -ENOMEM;
339
340                 /* FIXME?  We don't reserve NET_IP_ALIGN or NET_SKB_PAD since
341                  * we're just transmitting this raw without examining anything
342                  * at those layers. */
343                 memcpy(skb_put(skb, data_len), opo->u.data, data_len);
344                 dp_set_origin(chain->dp, ntohs(opo->in_port), skb);
345
346                 skb_set_mac_header(skb, 0);
347                 mac = vlan_eth_hdr(skb);
348                 if (likely(mac->h_vlan_proto != htons(ETH_P_8021Q)))
349                         nh_ofs = sizeof(struct ethhdr);
350                 else
351                         nh_ofs = sizeof(struct vlan_ethhdr);
352                 skb_set_network_header(skb, nh_ofs);
353
354                 dp_output_port(chain->dp, skb, ntohs(opo->out_port));
355         } else {
356                 struct sw_flow_key key;
357                 int n_acts;
358
359                 skb = retrieve_skb(ntohl(opo->buffer_id));
360                 if (!skb)
361                         return -ESRCH;
362                 dp_set_origin(chain->dp, ntohs(opo->in_port), skb);
363
364                 n_acts = (ntohs(opo->header.length) - sizeof *opo) 
365                                 / sizeof *opo->u.actions;
366                 flow_extract(skb, ntohs(opo->in_port), &key);
367                 execute_actions(chain->dp, skb, &key, opo->u.actions, n_acts);
368         }
369         return 0;
370 }
371
372 static int
373 recv_port_mod(struct sw_chain *chain, const struct sender *sender,
374               const void *msg)
375 {
376         const struct ofp_port_mod *opm = msg;
377
378         dp_update_port_flags(chain->dp, &opm->desc);
379
380         return 0;
381 }
382
383 static int
384 recv_echo_request(struct sw_chain *chain, const struct sender *sender,
385                   const void *msg) 
386 {
387         return dp_send_echo_reply(chain->dp, sender, msg);
388 }
389
390 static int
391 recv_echo_reply(struct sw_chain *chain, const struct sender *sender,
392                   const void *msg) 
393 {
394         return 0;
395 }
396
397 static int
398 add_flow(struct sw_chain *chain, const struct ofp_flow_mod *ofm)
399 {
400         int error = -ENOMEM;
401         int i;
402         int n_acts;
403         struct sw_flow *flow;
404
405
406         /* To prevent loops, make sure there's no action to send to the
407          * OFP_TABLE virtual port.
408          */
409         n_acts = (ntohs(ofm->header.length) - sizeof *ofm) / sizeof *ofm->actions;
410         for (i=0; i<n_acts; i++) {
411                 const struct ofp_action *a = &ofm->actions[i];
412
413                 if (a->type == htons(OFPAT_OUTPUT) 
414                                         && (a->arg.output.port == htons(OFPP_TABLE) 
415                                                 || a->arg.output.port == htons(OFPP_NONE))) {
416                         /* xxx Send fancy new error message? */
417                         goto error;
418                 }
419         }
420
421         /* Allocate memory. */
422         flow = flow_alloc(n_acts, GFP_ATOMIC);
423         if (flow == NULL)
424                 goto error;
425
426         /* Fill out flow. */
427         flow_extract_match(&flow->key, &ofm->match);
428         flow->priority = flow->key.wildcards ? ntohs(ofm->priority) : -1;
429         flow->idle_timeout = ntohs(ofm->idle_timeout);
430         flow->hard_timeout = ntohs(ofm->hard_timeout);
431         flow->used = jiffies;
432         flow->n_actions = n_acts;
433         flow->init_time = jiffies;
434         flow->byte_count = 0;
435         flow->packet_count = 0;
436         spin_lock_init(&flow->lock);
437         memcpy(flow->actions, ofm->actions, n_acts * sizeof *flow->actions);
438
439         /* Act. */
440         error = chain_insert(chain, flow);
441         if (error)
442                 goto error_free_flow;
443         error = 0;
444         if (ntohl(ofm->buffer_id) != (uint32_t) -1) {
445                 struct sk_buff *skb = retrieve_skb(ntohl(ofm->buffer_id));
446                 if (skb) {
447                         struct sw_flow_key key;
448                         flow_used(flow, skb);
449                         flow_extract(skb, ntohs(ofm->match.in_port), &key);
450                         execute_actions(chain->dp, skb, &key,
451                                         ofm->actions, n_acts);
452                 }
453                 else
454                         error = -ESRCH;
455         }
456         return error;
457
458 error_free_flow:
459         flow_free(flow);
460 error:
461         if (ntohl(ofm->buffer_id) != (uint32_t) -1)
462                 discard_skb(ntohl(ofm->buffer_id));
463         return error;
464 }
465
466 static int
467 recv_flow(struct sw_chain *chain, const struct sender *sender, const void *msg)
468 {
469         const struct ofp_flow_mod *ofm = msg;
470         uint16_t command = ntohs(ofm->command);
471
472         if (command == OFPFC_ADD) {
473                 return add_flow(chain, ofm);
474         }  else if (command == OFPFC_DELETE) {
475                 struct sw_flow_key key;
476                 flow_extract_match(&key, &ofm->match);
477                 return chain_delete(chain, &key, 0, 0) ? 0 : -ESRCH;
478         } else if (command == OFPFC_DELETE_STRICT) {
479                 struct sw_flow_key key;
480                 uint16_t priority;
481                 flow_extract_match(&key, &ofm->match);
482                 priority = key.wildcards ? ntohs(ofm->priority) : -1;
483                 return chain_delete(chain, &key, priority, 1) ? 0 : -ESRCH;
484         } else {
485                 return -ENOTSUPP;
486         }
487 }
488
489 /* 'msg', which is 'length' bytes long, was received across Netlink from
490  * 'sender'.  Apply it to 'chain'. */
491 int
492 fwd_control_input(struct sw_chain *chain, const struct sender *sender,
493                   const void *msg, size_t length)
494 {
495
496         struct openflow_packet {
497                 size_t min_size;
498                 int (*handler)(struct sw_chain *, const struct sender *,
499                                const void *);
500         };
501
502         static const struct openflow_packet packets[] = {
503                 [OFPT_FEATURES_REQUEST] = {
504                         sizeof (struct ofp_header),
505                         recv_features_request,
506                 },
507                 [OFPT_GET_CONFIG_REQUEST] = {
508                         sizeof (struct ofp_header),
509                         recv_get_config_request,
510                 },
511                 [OFPT_SET_CONFIG] = {
512                         sizeof (struct ofp_switch_config),
513                         recv_set_config,
514                 },
515                 [OFPT_PACKET_OUT] = {
516                         sizeof (struct ofp_packet_out),
517                         recv_packet_out,
518                 },
519                 [OFPT_FLOW_MOD] = {
520                         sizeof (struct ofp_flow_mod),
521                         recv_flow,
522                 },
523                 [OFPT_PORT_MOD] = {
524                         sizeof (struct ofp_port_mod),
525                         recv_port_mod,
526                 },
527                 [OFPT_ECHO_REQUEST] = {
528                         sizeof (struct ofp_header),
529                         recv_echo_request,
530                 },
531                 [OFPT_ECHO_REPLY] = {
532                         sizeof (struct ofp_header),
533                         recv_echo_reply,
534                 },
535         };
536
537         const struct openflow_packet *pkt;
538         struct ofp_header *oh;
539
540         oh = (struct ofp_header *) msg;
541         if (oh->version != OFP_VERSION || oh->type >= ARRAY_SIZE(packets)
542                 || ntohs(oh->length) > length)
543                 return -EINVAL;
544
545         pkt = &packets[oh->type];
546         if (!pkt->handler)
547                 return -ENOSYS;
548         if (length < pkt->min_size)
549                 return -EFAULT;
550
551         return pkt->handler(chain, sender, msg);
552 }
553
554 /* Packet buffering. */
555
556 #define OVERWRITE_SECS  1
557 #define OVERWRITE_JIFFIES (OVERWRITE_SECS * HZ)
558
559 struct packet_buffer {
560         struct sk_buff *skb;
561         uint32_t cookie;
562         unsigned long exp_jiffies;
563 };
564
565 static struct packet_buffer buffers[N_PKT_BUFFERS];
566 static unsigned int buffer_idx;
567 static DEFINE_SPINLOCK(buffer_lock);
568
569 uint32_t fwd_save_skb(struct sk_buff *skb)
570 {
571         struct sk_buff *old_skb = NULL;
572         struct packet_buffer *p;
573         unsigned long int flags;
574         uint32_t id;
575
576         spin_lock_irqsave(&buffer_lock, flags);
577         buffer_idx = (buffer_idx + 1) & PKT_BUFFER_MASK;
578         p = &buffers[buffer_idx];
579         if (p->skb) {
580                 /* Don't buffer packet if existing entry is less than
581                  * OVERWRITE_SECS old. */
582                 if (time_before(jiffies, p->exp_jiffies)) {
583                         spin_unlock_irqrestore(&buffer_lock, flags);
584                         return -1;
585                 } else {
586                         /* Defer kfree_skb() until interrupts re-enabled. */
587                         old_skb = p->skb;
588                 }
589         }
590         /* Don't use maximum cookie value since the all-bits-1 id is
591          * special. */
592         if (++p->cookie >= (1u << PKT_COOKIE_BITS) - 1)
593                 p->cookie = 0;
594         skb_get(skb);
595         p->skb = skb;
596         p->exp_jiffies = jiffies + OVERWRITE_JIFFIES;
597         id = buffer_idx | (p->cookie << PKT_BUFFER_BITS);
598         spin_unlock_irqrestore(&buffer_lock, flags);
599
600         if (old_skb)
601                 kfree_skb(old_skb);
602
603         return id;
604 }
605
606 static struct sk_buff *retrieve_skb(uint32_t id)
607 {
608         unsigned long int flags;
609         struct sk_buff *skb = NULL;
610         struct packet_buffer *p;
611
612         spin_lock_irqsave(&buffer_lock, flags);
613         p = &buffers[id & PKT_BUFFER_MASK];
614         if (p->cookie == id >> PKT_BUFFER_BITS) {
615                 skb = p->skb;
616                 p->skb = NULL;
617         } else {
618                 printk("cookie mismatch: %x != %x\n",
619                                 id >> PKT_BUFFER_BITS, p->cookie);
620         }
621         spin_unlock_irqrestore(&buffer_lock, flags);
622
623         return skb;
624 }
625
626 void fwd_discard_all(void) 
627 {
628         int i;
629
630         for (i = 0; i < N_PKT_BUFFERS; i++) {
631                 struct sk_buff *skb;
632                 unsigned long int flags;
633
634                 /* Defer kfree_skb() until interrupts re-enabled. */
635                 spin_lock_irqsave(&buffer_lock, flags);
636                 skb = buffers[i].skb;
637                 buffers[i].skb = NULL;
638                 spin_unlock_irqrestore(&buffer_lock, flags);
639
640                 kfree_skb(skb);
641         }
642 }
643
644 static void discard_skb(uint32_t id)
645 {
646         struct sk_buff *old_skb = NULL;
647         unsigned long int flags;
648         struct packet_buffer *p;
649
650         spin_lock_irqsave(&buffer_lock, flags);
651         p = &buffers[id & PKT_BUFFER_MASK];
652         if (p->cookie == id >> PKT_BUFFER_BITS) {
653                 /* Defer kfree_skb() until interrupts re-enabled. */
654                 old_skb = p->skb;
655                 p->skb = NULL;
656         }
657         spin_unlock_irqrestore(&buffer_lock, flags);
658
659         if (old_skb)
660                 kfree_skb(old_skb);
661 }
662
663 void fwd_exit(void)
664 {
665         fwd_discard_all();
666 }
667
668 /* Utility functions. */
669
670 /* Makes '*pskb' writable, possibly copying it and setting '*pskb' to point to
671  * the copy.
672  * Returns 1 if successful, 0 on failure. */
673 static int
674 make_writable(struct sk_buff **pskb)
675 {
676         /* Based on skb_make_writable() in net/netfilter/core.c. */
677         struct sk_buff *nskb;
678
679         /* Not exclusive use of packet?  Must copy. */
680         if (skb_shared(*pskb) || skb_cloned(*pskb))
681                 goto copy_skb;
682
683         return pskb_may_pull(*pskb, 40); /* FIXME? */
684
685 copy_skb:
686         nskb = skb_copy(*pskb, GFP_ATOMIC);
687         if (!nskb)
688                 return 0;
689         BUG_ON(skb_is_nonlinear(nskb));
690
691         /* Rest of kernel will get very unhappy if we pass it a
692            suddenly-orphaned skbuff */
693         if ((*pskb)->sk)
694                 skb_set_owner_w(nskb, (*pskb)->sk);
695         kfree_skb(*pskb);
696         *pskb = nskb;
697         return 1;
698 }