For SNAT, don't store the pre-fragment L2 header before actions are applied.
[sliver-openvswitch.git] / datapath / nx_act_snat.c
1 #ifdef SUPPORT_SNAT
2 /*
3  * Distributed under the terms of the GNU GPL version 2.
4  * Copyright (c) 2008 Nicira Networks
5  */
6
7 #include <linux/etherdevice.h>
8 #include <linux/netdevice.h>
9 #include <linux/netfilter.h>
10 #include <linux/netfilter_bridge.h>
11 #include <linux/netfilter_ipv4.h>
12 #include <linux/in.h>
13 #include <net/ip.h>
14 #include <linux/icmp.h>
15 #include <linux/if_ether.h>
16 #include <net/arp.h>
17 #include <net/route.h>
18
19 #include "forward.h"
20 #include "dp_act.h"
21 #include "nx_act_snat.h"
22
23
24 /* We need these fake structures to make netfilter happy --
25  * lots of places assume that skb->dst != NULL, which isn't
26  * all that unreasonable.
27  *
28  * Currently, we fill in the PMTU entry because netfilter
29  * refragmentation needs it, and the rt_flags entry because
30  * ipt_REJECT needs it.  Future netfilter modules might
31  * require us to fill additional fields. */
32 static struct net_device __fake_net_device = {
33         .hard_header_len        = ETH_HLEN
34 };
35
36 static struct rtable __fake_rtable = {
37         .u = {
38                 .dst = {
39                         .__refcnt          = ATOMIC_INIT(1),
40                         .dev                    = &__fake_net_device,
41                         .path              = &__fake_rtable.u.dst,
42                         .metrics                = {[RTAX_MTU - 1] = 1500},
43                         .flags            = DST_NOXFRM,
44                 }
45         },
46         .rt_flags   = 0,
47 };
48
49 /* Define ARP for IP since the Linux headers don't do it cleanly. */
50 struct ip_arphdr {
51         uint16_t ar_hrd;
52         uint16_t ar_pro;
53         uint8_t ar_hln;
54         uint8_t ar_pln;
55         uint16_t ar_op;
56         uint8_t ar_sha[ETH_ALEN];
57         uint32_t ar_sip;
58         uint8_t ar_tha[ETH_ALEN];
59         uint32_t ar_tip;
60 } __attribute__((packed));
61 OFP_ASSERT(sizeof(struct ip_arphdr) == 28);
62
63 static inline struct nf_bridge_info *nf_bridge_alloc(struct sk_buff *skb)
64 {
65         skb->nf_bridge = kzalloc(sizeof(struct nf_bridge_info), GFP_ATOMIC);
66         if (likely(skb->nf_bridge))
67                 atomic_set(&(skb->nf_bridge->use), 1);
68
69         return skb->nf_bridge;
70 }
71
72 /* Save a copy of the original Ethernet header. */
73 void snat_save_header(struct sk_buff *skb)
74 {
75         int header_size = ETH_HLEN + nf_bridge_encap_header_len(skb);
76
77         if (!skb->nf_bridge)
78                 return;
79
80         skb_copy_from_linear_data_offset(skb, -header_size, 
81                         skb->nf_bridge->data, header_size);
82 }
83
84 /* Restore a saved Ethernet header. */
85 int snat_copy_header(struct sk_buff *skb)
86 {
87         int err;
88         int header_size = ETH_HLEN + nf_bridge_encap_header_len(skb);
89
90         if (!skb->nf_bridge)
91                 return 0;
92
93         err = skb_cow_head(skb, header_size);
94         if (err)
95                 return err;
96
97         skb_copy_to_linear_data_offset(skb, -header_size, 
98                         skb->nf_bridge->data, header_size);
99         __skb_push(skb, nf_bridge_encap_header_len(skb));
100         return 0;
101 }
102
103 /* Push the Ethernet header back on and tranmit the packet. */
104 static int
105 dp_xmit_skb_push(struct sk_buff *skb)
106 {
107         skb_push(skb, ETH_HLEN);
108         return dp_xmit_skb(skb);
109 }
110
111 /* Perform maintainence related to a SNAT'd interface.  Currently, this only 
112  * checks whether MAC->IP bindings have expired.
113  *
114  * Called with the RCU read lock */
115 void
116 snat_maint(struct net_bridge_port *p)
117 {
118         struct snat_conf *sc;
119         struct snat_mapping *m, *n;
120         unsigned long flags;
121         unsigned long timeout;
122
123         spin_lock_irqsave(&p->lock, flags);
124         sc = p->snat;
125         if (!sc)
126                 goto done;
127
128         timeout = sc->mac_timeout * HZ;
129
130         list_for_each_entry_safe (m, n, &sc->mappings, node) {
131                 if (time_after(jiffies, m->used + timeout)) {
132                         list_del(&m->node);
133                         kfree(m);
134                 }
135         }
136
137 done:
138         spin_unlock_irqrestore(&p->lock, flags);
139 }
140
141 /* When the packet is bound for a local interface, strip off the fake
142  * routing table.
143  */
144 void snat_local_in(struct sk_buff *skb)
145 {
146         if (skb->dst == (struct dst_entry *)&__fake_rtable) {
147                 dst_release(skb->dst);
148                 skb->dst = NULL;
149         }
150 }
151
152 /* Check whether destination IP's address is in the IP->MAC mappings.
153  * If it is, then overwrite the destination MAC with the value from the
154  * cache.
155  *
156  * Returns -1 if there is a problem, otherwise 0. */
157 static int
158 dnat_mac(struct net_bridge_port *p, struct sk_buff *skb)
159 {
160         struct snat_conf *sc = p->snat;
161         struct iphdr *iph = ip_hdr(skb);
162         struct ethhdr *eh = eth_hdr(skb);
163         struct snat_mapping *m;
164
165         if (skb->protocol != htons(ETH_P_IP)) 
166                 return 0;
167
168         list_for_each_entry (m, &sc->mappings, node) {
169                 if (m->ip_addr == iph->daddr){
170                         /* Found it! */
171                         if (!make_writable(&skb)) {
172                                 if (net_ratelimit())
173                                         printk("make_writable failed\n");
174                                 return -EINVAL;
175                         }
176                         m->used = jiffies;
177                         memcpy(eh->h_dest, m->hw_addr, ETH_ALEN);
178                         break;
179                 }
180         }
181
182         return 0;
183 }
184
185 static int
186 __snat_this_address(struct snat_conf *sc, u32 ip_addr)
187 {
188         if (sc) {
189                 u32 h_ip_addr = ntohl(ip_addr);
190                 return (h_ip_addr >= sc->ip_addr_start &&
191                         h_ip_addr <= sc->ip_addr_end);
192         }
193         return 0;
194 }
195
196 static int
197 snat_this_address(struct net_bridge_port *p, u32 ip_addr)
198 {
199         unsigned long int flags;
200         int retval;
201
202         spin_lock_irqsave(&p->lock, flags);
203         retval = __snat_this_address(p->snat, ip_addr);
204         spin_unlock_irqrestore(&p->lock, flags);
205
206         return retval;
207 }
208
209 /* Must hold RCU lock. */
210 static struct net_bridge_port *
211 get_nbp_by_ip_addr(struct datapath *dp, u32 ip_addr)
212 {
213         struct net_bridge_port *p;
214
215         list_for_each_entry_rcu (p, &dp->port_list, node)
216                 if (snat_this_address(p, ip_addr))
217                         return p;
218
219         return NULL;
220 }
221
222 static int
223 snat_pre_route_finish(struct sk_buff *skb)
224 {
225         struct net_bridge_port *p = skb->dev->br_port;
226         struct snat_conf *sc;
227         struct iphdr *iph = ip_hdr(skb);
228         unsigned long flags;
229
230         skb->dst = (struct dst_entry *)&__fake_rtable;
231         dst_hold(skb->dst);
232
233         /* Don't process packets that were not translated due to NAT */
234         spin_lock_irqsave(&p->lock, flags);
235         sc = p->snat;
236         if (!__snat_this_address(sc, iph->daddr)) {
237                 /* If SNAT is configured for this input device, check the
238                  * IP->MAC mappings to see if we should update the destination
239                  * MAC. */
240                 if (sc)
241                         dnat_mac(skb->dev->br_port, skb);
242
243         }
244         spin_unlock_irqrestore(&p->lock, flags);
245
246         /* Pass the translated packet as input to the OpenFlow stack, which
247          * consumes it. */
248         skb_push(skb, ETH_HLEN);
249         skb_reset_mac_header(skb);
250         fwd_port_input(p->dp->chain, skb, p);
251
252         return 0;
253 }
254
255 /* Checks whether 'skb' is an ARP request for an SNAT'd interface.  If
256  * so, it will generate a response.  
257  *
258  * Returns 0 if the packet was not handled.  Otherwise, -1 is returned
259  * and the caller is responsible for freeing 'skb'. */
260 static int 
261 handle_arp_snat(struct sk_buff *skb)
262 {
263         struct net_bridge_port *s_nbp = skb->dev->br_port;
264         struct net_bridge_port *nat_nbp;
265         struct ip_arphdr *ah;
266         uint8_t mac_addr[ETH_ALEN];
267
268         if (!pskb_may_pull(skb, sizeof *ah))
269                 return 0;
270
271         ah = (struct ip_arphdr *)arp_hdr(skb);
272         if ((ah->ar_op != htons(ARPOP_REQUEST)) 
273                         || ah->ar_hln != ETH_ALEN
274                         || ah->ar_pro != htons(ETH_P_IP)
275                         || ah->ar_pln != 4)
276                 return 0;
277
278         rcu_read_lock();
279         nat_nbp = get_nbp_by_ip_addr(s_nbp->dp, ah->ar_tip);
280         if (!nat_nbp) {
281                 rcu_read_unlock();
282                 return 0;
283         }
284         if (s_nbp == nat_nbp) 
285                 memcpy(mac_addr, s_nbp->dp->netdev->dev_addr, sizeof(mac_addr));
286         else if (!is_zero_ether_addr(nat_nbp->snat->mac_addr)) 
287                 memcpy(mac_addr, nat_nbp->snat->mac_addr, sizeof(mac_addr));
288         else {
289                 rcu_read_unlock();
290                 return 0;
291         }
292         rcu_read_unlock();
293
294         arp_send(ARPOP_REPLY, ETH_P_ARP, ah->ar_sip, skb->dev, ah->ar_tip, 
295                          ah->ar_sha, mac_addr, ah->ar_sha);
296
297         return -1;
298 }
299
300 /* Checks whether 'skb' is a ping request for an SNAT'd interface.  If
301  * so, it will generate a response.  
302  *
303  * Returns 0 if the packet was not handled.  Otherwise, -1 is returned
304  * and the caller is responsible for freeing 'skb'. */
305 static int 
306 handle_icmp_snat(struct sk_buff *skb)
307 {
308         struct net_bridge_port *p = skb->dev->br_port;
309         struct ethhdr *eh;
310         struct iphdr *iph;
311         struct icmphdr *icmph;
312         uint8_t tmp_eth[ETH_ALEN];
313         uint32_t tmp_ip;
314         struct sk_buff *nskb;
315
316         /* We're only interested in addresses we rewrite. */
317         iph = ip_hdr(skb);
318         if (!snat_this_address(p, iph->daddr)) {
319                 return 0;
320         }
321
322         /* Drop fragments and packets not long enough to hold the ICMP
323          * header. */
324         if ((ntohs(iph->frag_off) & IP_OFFSET) != 0 ||
325             !pskb_may_pull(skb, skb_transport_offset(skb) + 4))
326                 return 0;
327
328         /* We only respond to echo requests to our address.  Continue 
329          * processing replies and other ICMP messages since they may be 
330          * intended for NAT'd hosts. */
331         icmph = icmp_hdr(skb);
332         if (icmph->type != ICMP_ECHO)
333                 return 0;
334
335         /* Send an echo reply in response */
336         nskb = skb_copy(skb, GFP_ATOMIC);
337         if (!nskb) {
338                 if (net_ratelimit())
339                         printk("skb copy failed for icmp reply\n");
340                 return -1;
341         }
342
343         /* Update Ethernet header. */
344         eh = eth_hdr(nskb);
345         memcpy(tmp_eth, eh->h_dest, ETH_ALEN);
346         memcpy(eh->h_dest, eh->h_source, ETH_ALEN);
347         memcpy(eh->h_source, tmp_eth, ETH_ALEN);
348
349         /* Update IP header.
350          * This is kind of busted, at least in that it doesn't check that the
351          * echoed IP options make sense. */
352         iph = ip_hdr(nskb);
353         iph->id = 0;
354         iph->frag_off = 0;
355         iph->ttl = IPDEFTTL;
356         iph->check = 0;
357         tmp_ip = iph->daddr;
358         iph->daddr = iph->saddr;
359         iph->saddr = tmp_ip;
360         iph->check = ip_fast_csum(iph, iph->ihl);
361
362         /* Update ICMP header. */
363         icmph = icmp_hdr(nskb);
364         icmph->type = ICMP_ECHOREPLY;
365         icmph->checksum = 0;
366         icmph->checksum = ip_compute_csum(icmph,
367                                           nskb->tail - nskb->transport_header);
368
369         dp_xmit_skb_push(nskb);
370
371         return -1;
372 }
373
374 /* Check if any SNAT maintenance needs to be done on 'skb' before it's 
375  * checked against the datapath's tables.  This includes DNAT
376  * modification based on prior SNAT action and responding to ARP and
377  * echo requests for the SNAT interface. 
378  *
379  * Returns -1 if the packet was handled and consumed, 0 if the caller
380  * should continue to process 'skb'.
381  */
382 int
383 snat_pre_route(struct sk_buff *skb)
384 {
385         struct iphdr *iph;
386         int len;
387
388         WARN_ON_ONCE(skb_network_offset(skb));
389         if (skb->protocol == htons(ETH_P_ARP)) {
390                 if (handle_arp_snat(skb))
391                         goto consume;
392                 return 0;
393         }
394         else if (skb->protocol != htons(ETH_P_IP)) 
395                 return 0;
396
397         if (!pskb_may_pull(skb, sizeof *iph))
398                 goto consume;
399
400         iph = ip_hdr(skb);
401         if (iph->ihl < 5 || iph->version != 4)
402                 goto consume;
403
404         if (!pskb_may_pull(skb, ip_hdrlen(skb)))
405                 goto consume;
406         skb_set_transport_header(skb, ip_hdrlen(skb));
407
408         /* Check if we need to echo reply for this address */
409         iph = ip_hdr(skb);
410         if ((iph->protocol == IPPROTO_ICMP) && (handle_icmp_snat(skb))) 
411                 goto consume;
412
413         iph = ip_hdr(skb);
414         if (unlikely(ip_fast_csum(iph, iph->ihl)))
415                 goto consume;
416
417         len = ntohs(iph->tot_len);
418         if ((skb->len < len) || len < (iph->ihl*4))
419                 goto consume;
420
421         if (pskb_trim_rcsum(skb, len))
422                 goto consume;
423
424         nf_bridge_put(skb->nf_bridge);
425         if (!nf_bridge_alloc(skb))
426                 return 0;
427
428         NF_HOOK(PF_INET, NF_INET_PRE_ROUTING, skb, skb->dev, NULL,
429                 snat_pre_route_finish);
430         return -1;
431
432 consume:
433         kfree_skb(skb);
434         return -1;
435 }
436
437
438 static int 
439 snat_skb_finish(struct sk_buff *skb)
440 {
441         NF_HOOK(PF_INET, NF_INET_POST_ROUTING, skb, NULL, skb->dev, 
442                         dp_xmit_skb_push);
443
444         return 0;
445 }
446
447 /* Update the MAC->IP mappings for the private side of the SNAT'd
448  * interface. */
449 static void
450 update_mapping(struct net_bridge_port *p, const struct sk_buff *skb)
451 {
452         unsigned long flags;
453         struct snat_conf *sc;
454         const struct iphdr *iph = ip_hdr(skb);
455         const struct ethhdr *eh = eth_hdr(skb);
456         struct snat_mapping *m;
457
458         spin_lock_irqsave(&p->lock, flags);
459         sc = p->snat;
460         if (!sc) 
461                 goto done;
462         
463         list_for_each_entry (m, &sc->mappings, node) {
464                 if (m->ip_addr == iph->saddr){
465                         memcpy(m->hw_addr, eh->h_source, ETH_ALEN);
466                         m->used = jiffies;
467                         goto done;
468                 }
469         }
470
471         m = kmalloc(sizeof *m, GFP_ATOMIC);
472         if (!m)
473                 goto done;
474         m->ip_addr = iph->saddr;
475         memcpy(m->hw_addr, eh->h_source, ETH_ALEN);
476         m->used = jiffies;
477
478         list_add(&m->node, &sc->mappings);
479
480 done:
481         spin_unlock_irqrestore(&p->lock, flags);
482 }
483
484 /* Perform SNAT modification on 'skb' and send out 'out_port'.  If the 
485  * port was not configured for SNAT, it will be sent through the interface 
486  * unmodified.  'skb' is not consumed, so caller will need to free it.
487  */
488 void 
489 snat_skb(struct datapath *dp, const struct sk_buff *skb, int out_port)
490 {
491         struct net_bridge_port *p = dp->ports[out_port];
492         struct sk_buff *nskb;
493
494         if (!p)
495                 return;
496
497         /* FIXME: Expensive.  Just need to skb_clone() here?
498          * (However, the skb_copy() does linearize and ensure that the headers
499          * are accessible.) */
500         nskb = skb_copy(skb, GFP_ATOMIC);
501         if (!nskb)
502                 return;
503
504         nskb->dev = p->dev;
505
506         /* We only SNAT IP, so just send it on its way if not */
507         if (skb->protocol != htons(ETH_P_IP)) {
508                 dp_xmit_skb(nskb);
509                 return;
510         }
511
512         /* Set the source MAC to the OF interface */
513         memcpy(eth_hdr(nskb)->h_source, dp->netdev->dev_addr, ETH_ALEN);
514
515         update_mapping(p, skb);
516
517         /* Take the Ethernet header back off for netfilter hooks. */
518         skb_pull(nskb, ETH_HLEN);
519
520         NF_HOOK(PF_INET, NF_INET_FORWARD, nskb, skb->dev, nskb->dev, 
521                         snat_skb_finish);
522 }
523
524 /* Remove SNAT configuration on port 'p'.  
525  *
526  * NB: The caller must hold the port's spinlock. */
527 int
528 snat_free_conf(struct net_bridge_port *p)
529 {
530         struct snat_conf *sc = p->snat;
531
532         if (!sc) 
533                 return -EINVAL;
534
535         /* Free existing mapping entries */
536         while (!list_empty(&sc->mappings)) {
537                 struct snat_mapping *m = list_entry(sc->mappings.next, 
538                                 struct snat_mapping, node);
539                 list_del(&m->node);
540                 kfree(m);
541         }
542
543         kfree(p->snat);
544         p->snat = NULL;
545
546         return 0;
547 }
548
549 /* Remove SNAT configuration from an interface. */
550 static int 
551 snat_del_port(struct datapath *dp, const struct nx_snat_config *nsc)
552 {
553         unsigned long flags;
554         uint16_t port = ntohs(nsc->port);
555         struct net_bridge_port *p = dp->ports[port];
556
557         if (!p) {
558                 if (net_ratelimit()) 
559                         printk("Attempt to remove snat on non-existent port: %d\n", port);
560                 return -EINVAL;
561         }
562
563         spin_lock_irqsave(&p->lock, flags);
564         if (snat_free_conf(p)) {
565                 /* SNAT not configured on this port */
566                 spin_unlock_irqrestore(&p->lock, flags);
567                 if (net_ratelimit()) 
568                         printk("Attempt to remove snat on non-snat port: %d\n", port);
569                 return -EINVAL;
570         }
571
572         spin_unlock_irqrestore(&p->lock, flags);
573
574         return 0;
575 }
576
577 /* Add SNAT configuration to an interface.  */
578 static int 
579 snat_add_port(struct datapath *dp, const struct nx_snat_config *nsc)
580 {
581         unsigned long flags;
582         uint16_t port = ntohs(nsc->port);
583         struct net_bridge_port *p = dp->ports[port];
584         uint16_t mac_timeout = ntohs(nsc->mac_timeout);
585         struct snat_conf *sc;
586         
587         if (mac_timeout == 0)
588                 mac_timeout = MAC_TIMEOUT_DEFAULT;
589
590         if (!p) {
591                 if (net_ratelimit()) 
592                         printk("Attempt to add snat on non-existent port: %d\n", port);
593                 return -EINVAL;
594         }
595         
596         /* If SNAT is already configured on the port, check whether the same
597          * IP addresses are used.  If so, just update the mac timeout
598          * configuration. Otherwise, drop all SNAT configuration and
599          * reconfigure it. */
600         spin_lock_irqsave(&p->lock, flags);
601         if (p->snat) {
602                 if ((p->snat->ip_addr_start == ntohl(nsc->ip_addr_start)) 
603                                 && (p->snat->ip_addr_end == ntohl(nsc->ip_addr_end))) {
604                         p->snat->mac_timeout = mac_timeout;
605                         spin_unlock_irqrestore(&p->lock, flags);
606                         return 0;
607                 }
608
609                 /* Free the existing configuration and mappings. */
610                 snat_free_conf(p);
611         }
612
613         sc = kzalloc(sizeof *sc, GFP_ATOMIC);
614         if (!sc) {
615                 spin_unlock_irqrestore(&p->lock, flags);
616                 return -ENOMEM;
617         }
618
619         sc->ip_addr_start = ntohl(nsc->ip_addr_start);
620         sc->ip_addr_end = ntohl(nsc->ip_addr_end);
621         sc->mac_timeout = mac_timeout;
622         memcpy(sc->mac_addr, nsc->mac_addr, sizeof(sc->mac_addr));
623         INIT_LIST_HEAD(&sc->mappings);
624
625         p->snat = sc;
626         spin_unlock_irqrestore(&p->lock, flags);
627
628         return 0;
629 }
630
631 /* Handle a SNAT configuration message. 
632  *
633  * Returns 0 if no problems are found.  Otherwise, a negative errno. */
634 int 
635 snat_mod_config(struct datapath *dp, const struct nx_act_config *nac)
636 {
637         int n_entries = (ntohs(nac->header.header.length) - sizeof *nac)
638                         / sizeof (struct nx_snat_config);
639         int ret = 0;
640         int i;
641
642         for (i=0; i<n_entries; i++) {
643                 const struct nx_snat_config *nsc = &nac->snat[i];
644                 int r = 0;
645
646                 if (nsc->command == NXSC_ADD)
647                         r = snat_add_port(dp, nsc);
648                 else 
649                         r = snat_del_port(dp, nsc);
650
651                 if (r)
652                         ret = r;
653         }
654
655         return ret;
656 }
657 #endif