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