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