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