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