Allow SNAT to build on older (2.6.15) and new (2.6.26) kernels.
[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 /* Check whether destination IP's address is in the IP->MAC mappings.
101  * If it is, then overwrite the destination MAC with the value from the
102  * cache.
103  *
104  * Returns -1 if there is a problem, otherwise 0. */
105 static int
106 dnat_mac(struct net_bridge_port *p, struct sk_buff *skb)
107 {
108         struct snat_conf *sc;
109         struct iphdr *iph = ip_hdr(skb);
110         struct ethhdr *eh = eth_hdr(skb);
111         struct snat_mapping *m;
112         unsigned long flags;
113
114         spin_lock_irqsave(&p->lock, flags);
115         sc = p->snat;
116         if (!sc) {
117                 spin_unlock_irqrestore(&p->lock, flags);
118                 return -EINVAL;
119         }
120
121         if (skb->protocol != htons(ETH_P_IP)) {
122                 spin_unlock_irqrestore(&p->lock, flags);
123                 return 0;
124         }
125
126         list_for_each_entry (m, &sc->mappings, node) {
127                 if (m->ip_addr == iph->daddr){
128                         /* Found it! */
129                         if (!make_writable(&skb)) {
130                                 if (net_ratelimit())
131                                         printk("make_writable failed\n");
132                                 spin_unlock_irqrestore(&p->lock, flags);
133                                 return -EINVAL;
134                         }
135                         m->used = jiffies;
136                         memcpy(eh->h_dest, m->hw_addr, ETH_ALEN);
137                         break;
138                 }
139         }
140
141         spin_unlock_irqrestore(&p->lock, flags);
142         return 0;
143 }
144
145 static int
146 snat_pre_route_finish(struct sk_buff *skb)
147 {
148         struct net_bridge_port *p = skb->dev->br_port;
149
150         /* If SNAT is configured for this input device, check the IP->MAC
151          * mappings to see if we should update the destination MAC. */
152         if (p->snat)
153                 dnat_mac(skb->dev->br_port, skb);
154
155         return 0;
156 }
157
158 /* Checks whether 'skb' is an ARP request for an SNAT'd interface.  If
159  * so, it will generate a response.  
160  *
161  * Returns 0 if the packet was not handled.  Otherwise, -1 is returned
162  * and the caller is responsible for freeing 'skb'. */
163 static int 
164 handle_arp_snat(struct sk_buff *skb)
165 {
166         struct net_bridge_port *p = skb->dev->br_port;
167         struct ip_arphdr *ah = (struct ip_arphdr *)arp_hdr(skb);
168         uint32_t ip_addr;
169         unsigned long flags;
170         struct snat_conf *sc;
171
172         if ((ah->ar_op != htons(ARPOP_REQUEST)) 
173                         || ah->ar_hln != ETH_ALEN
174                         || ah->ar_pro != htons(ETH_P_IP)
175                         || ah->ar_pln != 4)
176                 return 0;
177
178         ip_addr = ntohl(ah->ar_tip);
179         spin_lock_irqsave(&p->lock, flags);
180         sc = p->snat;
181
182         /* We're only interested in addresses we rewrite. */
183         if (!sc || (sc && ((ip_addr < sc->ip_addr_start) 
184                         || (ip_addr > sc->ip_addr_end)))) {
185                 spin_unlock_irqrestore(&p->lock, flags);
186                 return 0;
187         }
188         spin_unlock_irqrestore(&p->lock, flags);
189
190         arp_send(ARPOP_REPLY, ETH_P_ARP, ah->ar_sip, skb->dev, ah->ar_tip, 
191                          ah->ar_sha, p->dp->netdev->dev_addr, ah->ar_sha);
192
193         return -1;
194 }
195
196 /* Checks whether 'skb' is a ping request for an SNAT'd interface.  If
197  * so, it will generate a response.  
198  *
199  * Returns 0 if the packet was not handled.  Otherwise, -1 is returned
200  * and the caller is responsible for freeing 'skb'. */
201 static int 
202 handle_icmp_snat(struct sk_buff *skb)
203 {
204         struct net_bridge_port *p = skb->dev->br_port;
205         struct snat_conf *sc;
206         struct ethhdr *eh;
207         struct iphdr *iph = ip_hdr(skb);
208         uint32_t ip_addr;
209         struct icmphdr *icmph;
210         unsigned int datalen;
211         uint8_t tmp_eth[ETH_ALEN];
212         uint32_t tmp_ip;
213         struct sk_buff *nskb;
214         unsigned long flags;
215
216
217         ip_addr = ntohl(iph->daddr);
218         spin_lock_irqsave(&p->lock, flags);
219         sc = p->snat;
220
221         /* We're only interested in addresses we rewrite. */
222         if (!sc || (sc && ((ip_addr < sc->ip_addr_start) 
223                         || (ip_addr > sc->ip_addr_end)))) {
224                 spin_unlock_irqrestore(&p->lock, flags);
225                 return 0;
226         }
227         spin_unlock_irqrestore(&p->lock, flags);
228
229         icmph = (struct icmphdr *) ((u_int32_t *)iph + iph->ihl);
230         datalen = skb->len - iph->ihl * 4;
231
232         /* Drop fragments and packets not long enough to hold the ICMP
233          * header. */
234         if (((ntohs(iph->frag_off) & IP_OFFSET) != 0) || datalen < 4) 
235                 return 0;
236
237         /* We only respond to echo requests to our address.  Continue 
238          * processing replies and other ICMP messages since they may be 
239          * intended for NAT'd hosts. */
240         if (icmph->type != ICMP_ECHO)
241                 return 0;
242
243         /* Send an echo reply in response */
244         nskb = skb_copy(skb, GFP_ATOMIC);
245         if (!nskb) {
246                 if (net_ratelimit())
247                         printk("skb copy failed for icmp reply\n");
248                 return -1;
249         }
250
251         eh = eth_hdr(nskb);
252         iph = ip_hdr(nskb);
253         icmph = (struct icmphdr *) ((u_int32_t *)iph + iph->ihl);
254
255         tmp_ip = iph->daddr;
256         iph->daddr = iph->saddr;
257         iph->saddr = tmp_ip;
258
259         memcpy(tmp_eth, eh->h_dest, ETH_ALEN);
260         memcpy(eh->h_dest, eh->h_source, ETH_ALEN);
261         memcpy(eh->h_source, tmp_eth, ETH_ALEN);
262         
263         icmph->type = ICMP_ECHOREPLY;
264
265         dp_xmit_skb_push(nskb);
266
267         return -1;
268 }
269
270 /* Check if any SNAT maintenance needs to be done on 'skb' before it's 
271  * checked against the datapath's tables.  This includes DNAT
272  * modification based on prior SNAT action and responding to ARP and
273  * echo requests for the SNAT interface. 
274  *
275  * Returns 0 if 'skb' should continue to be processed by the caller.
276  * Returns -1 if the packet was handled, and the caller should free
277  * 'skb'.
278  */
279 int
280 snat_pre_route(struct sk_buff *skb)
281 {
282         struct iphdr *iph;
283         int len;
284
285         if (skb->protocol == htons(ETH_P_ARP)) 
286                 return handle_arp_snat(skb);
287         else if (skb->protocol != htons(ETH_P_IP)) 
288                 return 0;
289
290         iph = ip_hdr(skb);
291         if (iph->ihl < 5 || iph->version != 4)
292                 goto ipv4_error;
293
294         if (!pskb_may_pull(skb, iph->ihl*4))
295                 goto ipv4_error;
296
297         /* Check if we need to echo reply for this address */
298         if ((iph->protocol == IPPROTO_ICMP) && (handle_icmp_snat(skb))) 
299                 return -1;
300
301         if (unlikely(ip_fast_csum((u8 *)iph, iph->ihl)))
302                 goto ipv4_error;
303
304         len = ntohs(iph->tot_len);
305         if ((skb->len < len) || len < (iph->ihl*4))
306                 goto ipv4_error;
307
308         if (pskb_trim_rcsum(skb, len))
309                 goto ipv4_error;
310
311         skb->dst = (struct dst_entry *)&__fake_rtable;
312         dst_hold(skb->dst);
313
314         return NF_HOOK(PF_INET, NF_INET_PRE_ROUTING, skb, skb->dev, NULL,
315                         snat_pre_route_finish);
316
317 ipv4_error:
318         return -1;
319 }
320
321
322 static int 
323 snat_skb_finish(struct sk_buff *skb)
324 {
325         NF_HOOK(PF_INET, NF_INET_POST_ROUTING, skb, NULL, skb->dev, 
326                         dp_xmit_skb_push);
327
328         return 0;
329 }
330
331 /* Update the MAC->IP mappings for the private side of the SNAT'd
332  * interface. */
333 static void
334 update_mapping(struct net_bridge_port *p, struct sk_buff *skb)
335 {
336         unsigned long flags;
337         struct snat_conf *sc;
338         struct iphdr *iph = ip_hdr(skb);
339         struct ethhdr *eh = eth_hdr(skb);
340         struct snat_mapping *m;
341
342         spin_lock_irqsave(&p->lock, flags);
343         sc = p->snat;
344         if (!sc) 
345                 goto done;
346         
347         list_for_each_entry (m, &sc->mappings, node) {
348                 if (m->ip_addr == iph->saddr){
349                         if (memcmp(m->hw_addr, eh->h_source, ETH_ALEN)) {
350                                 memcpy(m->hw_addr, eh->h_source, ETH_ALEN);
351                         }
352                         m->used = jiffies;
353                         goto done;
354                 }
355         }
356
357         m = kmalloc(sizeof *m, GFP_ATOMIC);
358         m->ip_addr = iph->saddr;
359         memcpy(m->hw_addr, eh->h_source, ETH_ALEN);
360         m->used = jiffies;
361
362         list_add(&m->node, &sc->mappings);
363
364 done:
365         spin_unlock_irqrestore(&p->lock, flags);
366 }
367
368 /* Perform SNAT modification on 'skb' and send out 'out_port'.  If the 
369  * port was not configured for SNAT, it will be sent through the interface 
370  * unmodified.  'skb' is not consumed, so caller will need to free it.
371  */
372 void 
373 snat_skb(struct datapath *dp, struct sk_buff *skb, int out_port)
374 {
375         struct net_bridge_port *p = dp->ports[out_port];
376         struct sk_buff *nskb;
377
378         if (!p)
379                 return;
380
381         nskb = skb_copy(skb, GFP_ATOMIC);
382         if (!nskb)
383                 return;
384
385         nskb->dev = p->dev;
386
387         /* We only SNAT IP, so just send it on its way if not */
388         if (skb->protocol != htons(ETH_P_IP)) {
389                 dp_xmit_skb(nskb);
390                 return;
391         }
392
393         /* Set the source MAC to the OF interface */
394         memcpy(eth_hdr(nskb)->h_source, dp->netdev->dev_addr, ETH_ALEN);
395
396         update_mapping(p, skb);
397
398         /* Take the Ethernet header back off for netfilter hooks. */
399         skb_pull(nskb, ETH_HLEN);
400
401         NF_HOOK(PF_INET, NF_INET_FORWARD, nskb, skb->dev, nskb->dev, 
402                         snat_skb_finish);
403 }
404
405 /* Remove SNAT configuration on port 'p'.  
406  *
407  * NB: The caller must hold the port's spinlock. */
408 int
409 snat_free_conf(struct net_bridge_port *p)
410 {
411         struct snat_conf *sc = p->snat;
412
413         if (!sc) 
414                 return -EINVAL;
415
416         /* Free existing mapping entries */
417         while (!list_empty(&sc->mappings)) {
418                 struct snat_mapping *m = list_entry(sc->mappings.next, 
419                                 struct snat_mapping, node);
420                 list_del(&m->node);
421                 kfree(m);
422         }
423
424         kfree(p->snat);
425         p->snat = NULL;
426
427         return 0;
428 }
429
430 /* Remove SNAT configuration from an interface. */
431 static int 
432 snat_del_port(struct datapath *dp, uint16_t port)
433 {
434         unsigned long flags;
435         struct net_bridge_port *p = dp->ports[port];
436
437         if (!p) {
438                 if (net_ratelimit()) 
439                         printk("Attempt to remove snat on non-existent port: %d\n", port);
440                 return -EINVAL;
441         }
442
443         spin_lock_irqsave(&p->lock, flags);
444         if (snat_free_conf(p)) {
445                 /* SNAT not configured on this port */
446                 spin_unlock_irqrestore(&p->lock, flags);
447                 if (net_ratelimit()) 
448                         printk("Attempt to remove snat on non-snat port: %d\n", port);
449                 return -EINVAL;
450         }
451
452         spin_unlock_irqrestore(&p->lock, flags);
453
454         return 0;
455 }
456
457 /* Add SNAT configuration to an interface.  */
458 static int 
459 snat_add_port(struct datapath *dp, uint16_t port, 
460                 uint32_t ip_addr_start, uint32_t ip_addr_end,
461                 uint16_t mac_timeout)
462 {
463         unsigned long flags;
464         struct net_bridge_port *p = dp->ports[port];
465         struct snat_conf *sc;
466         
467
468         if (mac_timeout == 0)
469                 mac_timeout = MAC_TIMEOUT_DEFAULT;
470
471         if (!p) {
472                 if (net_ratelimit()) 
473                         printk("Attempt to add snat on non-existent port: %d\n", port);
474                 return -EINVAL;
475         }
476         
477         /* If SNAT is already configured on the port, check whether the same
478          * IP addresses are used.  If so, just update the mac timeout
479          * configuration. Otherwise, drop all SNAT configuration and
480          * reconfigure it. */
481         spin_lock_irqsave(&p->lock, flags);
482         if (p->snat) {
483                 if ((p->snat->ip_addr_start == ip_addr_start) 
484                                 && (p->snat->ip_addr_end = ip_addr_end)) {
485                         p->snat->mac_timeout = mac_timeout;
486                         spin_unlock_irqrestore(&p->lock, flags);
487                         return 0;
488                 }
489
490                 /* Free the existing configuration and mappings. */
491                 snat_free_conf(p);
492         }
493
494         sc = kzalloc(sizeof *sc, GFP_ATOMIC);
495         if (!sc) {
496                 spin_unlock_irqrestore(&p->lock, flags);
497                 return -ENOMEM;
498         }
499
500         sc->ip_addr_start = ip_addr_start;
501         sc->ip_addr_end = ip_addr_end;
502         sc->mac_timeout = mac_timeout;
503         INIT_LIST_HEAD(&sc->mappings);
504
505         p->snat = sc;
506         spin_unlock_irqrestore(&p->lock, flags);
507
508         return 0;
509 }
510
511 /* Handle a SNAT configuration message. 
512  *
513  * Returns 0 if no problems are found.  Otherwise, a negative errno. */
514 int 
515 snat_mod_config(struct datapath *dp, const struct nx_act_config *nac)
516 {
517         int n_entries = (ntohs(nac->header.header.length) - sizeof *nac)
518                         / sizeof (struct nx_snat_config);
519         int ret = 0;
520         int i;
521
522         for (i=0; i<n_entries; i++) {
523                 const struct nx_snat_config *sc = &nac->snat[i];
524                 uint16_t port = ntohs(sc->port);
525                 int r = 0;
526
527                 if (sc->command == NXSC_ADD)
528                         r = snat_add_port(dp, port, 
529                                         ntohl(sc->ip_addr_start), ntohl(sc->ip_addr_end), 
530                                         ntohs(sc->mac_timeout));
531                 else 
532                         r = snat_del_port(dp, port);
533
534                 if (r)
535                         ret = r;
536         }
537
538         return ret;
539 }
540 #endif