initial version, corresponding to ipfw3-2012
[ipfw-google.git] / sys / netinet / ipfw / ip_fw_nat.c
1 /*-
2  * Copyright (c) 2008 Paolo Pisati
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD: user/luigi/ipfw3-head/sys/netinet/ipfw/ip_fw_nat.c 200975 2009-12-25 01:15:39Z luigi $");
29
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/eventhandler.h>
33 #include <sys/malloc.h>
34 #include <sys/kernel.h>
35 #include <sys/lock.h>
36 #include <sys/module.h>
37 #include <sys/rwlock.h>
38
39 #define        IPFW_INTERNAL   /* Access to protected data structures in ip_fw.h. */
40
41 #include <netinet/libalias/alias.h>
42 #include <netinet/libalias/alias_local.h>
43
44 #include <net/if.h>
45 #include <netinet/in.h>
46 #include <netinet/ip.h>
47 #include <netinet/ip_var.h>
48 #include <netinet/ip_fw.h>
49 #include <netinet/ipfw/ip_fw_private.h>
50 #include <netinet/tcp.h>
51 #include <netinet/udp.h>
52
53 #include <machine/in_cksum.h>   /* XXX for in_cksum */
54
55 static VNET_DEFINE(eventhandler_tag, ifaddr_event_tag);
56 #define V_ifaddr_event_tag      VNET(ifaddr_event_tag)
57
58 static void
59 ifaddr_change(void *arg, struct ifnet *ifp)
60 {
61         struct cfg_nat *ptr;
62         struct ifaddr *ifa;
63         struct ip_fw_chain *chain;
64
65         (void)arg;
66         chain = &V_layer3_chain;
67         IPFW_WLOCK(chain);
68         /* Check every nat entry... */
69         LIST_FOREACH(ptr, &chain->nat, _next) {
70                 /* ...using nic 'ifp->if_xname' as dynamic alias address. */
71                 if (strncmp(ptr->if_name, ifp->if_xname, IF_NAMESIZE) != 0)
72                         continue;
73                 if_addr_rlock(ifp);
74                 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
75                         if (ifa->ifa_addr == NULL)
76                                 continue;
77                         if (ifa->ifa_addr->sa_family != AF_INET)
78                                 continue;
79                         ptr->ip = ((struct sockaddr_in *)
80                             (ifa->ifa_addr))->sin_addr;
81                         LibAliasSetAddress(ptr->lib, ptr->ip);
82                 }
83                 if_addr_runlock(ifp);
84         }
85         IPFW_WUNLOCK(chain);
86 }
87
88 /*
89  * delete the pointers for nat entry ix, or all of them if ix < 0
90  */
91 static void
92 flush_nat_ptrs(struct ip_fw_chain *chain, const int ix)
93 {
94         int i;
95         ipfw_insn_nat *cmd;
96
97         IPFW_WLOCK_ASSERT(chain);
98         for (i = 0; i < chain->n_rules; i++) {
99                 cmd = (ipfw_insn_nat *)ACTION_PTR(chain->map[i]);
100                 /* XXX skip log and the like ? */
101                 if (cmd->o.opcode == O_NAT && cmd->nat != NULL &&
102                             (ix < 0 || cmd->nat->id == ix))
103                         cmd->nat = NULL;
104         }
105 }
106
107 static void
108 del_redir_spool_cfg(struct cfg_nat *n, struct redir_chain *head)
109 {
110         struct cfg_redir *r, *tmp_r;
111         struct cfg_spool *s, *tmp_s;
112         int i, num;
113
114         LIST_FOREACH_SAFE(r, head, _next, tmp_r) {
115                 num = 1; /* Number of alias_link to delete. */
116                 switch (r->mode) {
117                 case REDIR_PORT:
118                         num = r->pport_cnt;
119                         /* FALLTHROUGH */
120                 case REDIR_ADDR:
121                 case REDIR_PROTO:
122                         /* Delete all libalias redirect entry. */
123                         for (i = 0; i < num; i++)
124                                 LibAliasRedirectDelete(n->lib, r->alink[i]);
125                         /* Del spool cfg if any. */
126                         LIST_FOREACH_SAFE(s, &r->spool_chain, _next, tmp_s) {
127                                 LIST_REMOVE(s, _next);
128                                 free(s, M_IPFW);
129                         }
130                         free(r->alink, M_IPFW);
131                         LIST_REMOVE(r, _next);
132                         free(r, M_IPFW);
133                         break;
134                 default:
135                         printf("unknown redirect mode: %u\n", r->mode);
136                         /* XXX - panic?!?!? */
137                         break;
138                 }
139         }
140 }
141
142 static int
143 add_redir_spool_cfg(char *buf, struct cfg_nat *ptr)
144 {
145         struct cfg_redir *r, *ser_r;
146         struct cfg_spool *s, *ser_s;
147         int cnt, off, i;
148
149         for (cnt = 0, off = 0; cnt < ptr->redir_cnt; cnt++) {
150                 ser_r = (struct cfg_redir *)&buf[off];
151                 r = malloc(SOF_REDIR, M_IPFW, M_WAITOK | M_ZERO);
152                 memcpy(r, ser_r, SOF_REDIR);
153                 LIST_INIT(&r->spool_chain);
154                 off += SOF_REDIR;
155                 r->alink = malloc(sizeof(struct alias_link *) * r->pport_cnt,
156                     M_IPFW, M_WAITOK | M_ZERO);
157                 switch (r->mode) {
158                 case REDIR_ADDR:
159                         r->alink[0] = LibAliasRedirectAddr(ptr->lib, r->laddr,
160                             r->paddr);
161                         break;
162                 case REDIR_PORT:
163                         for (i = 0 ; i < r->pport_cnt; i++) {
164                                 /* If remotePort is all ports, set it to 0. */
165                                 u_short remotePortCopy = r->rport + i;
166                                 if (r->rport_cnt == 1 && r->rport == 0)
167                                         remotePortCopy = 0;
168                                 r->alink[i] = LibAliasRedirectPort(ptr->lib,
169                                     r->laddr, htons(r->lport + i), r->raddr,
170                                     htons(remotePortCopy), r->paddr,
171                                     htons(r->pport + i), r->proto);
172                                 if (r->alink[i] == NULL) {
173                                         r->alink[0] = NULL;
174                                         break;
175                                 }
176                         }
177                         break;
178                 case REDIR_PROTO:
179                         r->alink[0] = LibAliasRedirectProto(ptr->lib ,r->laddr,
180                             r->raddr, r->paddr, r->proto);
181                         break;
182                 default:
183                         printf("unknown redirect mode: %u\n", r->mode);
184                         break;
185                 }
186                 /* XXX perhaps return an error instead of panic ? */
187                 if (r->alink[0] == NULL)
188                         panic("LibAliasRedirect* returned NULL");
189                 /* LSNAT handling. */
190                 for (i = 0; i < r->spool_cnt; i++) {
191                         ser_s = (struct cfg_spool *)&buf[off];
192                         s = malloc(SOF_REDIR, M_IPFW, M_WAITOK | M_ZERO);
193                         memcpy(s, ser_s, SOF_SPOOL);
194                         LibAliasAddServer(ptr->lib, r->alink[0],
195                             s->addr, htons(s->port));
196                         off += SOF_SPOOL;
197                         /* Hook spool entry. */
198                         LIST_INSERT_HEAD(&r->spool_chain, s, _next);
199                 }
200                 /* And finally hook this redir entry. */
201                 LIST_INSERT_HEAD(&ptr->redir_chain, r, _next);
202         }
203         return (1);
204 }
205
206 static int
207 ipfw_nat(struct ip_fw_args *args, struct cfg_nat *t, struct mbuf *m)
208 {
209         struct mbuf *mcl;
210         struct ip *ip;
211         /* XXX - libalias duct tape */
212         int ldt, retval;
213         char *c;
214
215         ldt = 0;
216         retval = 0;
217         mcl = m_megapullup(m, m->m_pkthdr.len);
218         if (mcl == NULL) {
219                 args->m = NULL;
220                 return (IP_FW_DENY);
221         }
222         ip = mtod(mcl, struct ip *);
223
224         /*
225          * XXX - Libalias checksum offload 'duct tape':
226          *
227          * locally generated packets have only pseudo-header checksum
228          * calculated and libalias will break it[1], so mark them for
229          * later fix.  Moreover there are cases when libalias modifies
230          * tcp packet data[2], mark them for later fix too.
231          *
232          * [1] libalias was never meant to run in kernel, so it does
233          * not have any knowledge about checksum offloading, and
234          * expects a packet with a full internet checksum.
235          * Unfortunately, packets generated locally will have just the
236          * pseudo header calculated, and when libalias tries to adjust
237          * the checksum it will actually compute a wrong value.
238          *
239          * [2] when libalias modifies tcp's data content, full TCP
240          * checksum has to be recomputed: the problem is that
241          * libalias does not have any idea about checksum offloading.
242          * To work around this, we do not do checksumming in LibAlias,
243          * but only mark the packets in th_x2 field. If we receive a
244          * marked packet, we calculate correct checksum for it
245          * aware of offloading.  Why such a terrible hack instead of
246          * recalculating checksum for each packet?
247          * Because the previous checksum was not checked!
248          * Recalculating checksums for EVERY packet will hide ALL
249          * transmission errors. Yes, marked packets still suffer from
250          * this problem. But, sigh, natd(8) has this problem, too.
251          *
252          * TODO: -make libalias mbuf aware (so
253          * it can handle delayed checksum and tso)
254          */
255
256         if (mcl->m_pkthdr.rcvif == NULL &&
257             mcl->m_pkthdr.csum_flags & CSUM_DELAY_DATA)
258                 ldt = 1;
259
260         c = mtod(mcl, char *);
261         if (args->oif == NULL)
262                 retval = LibAliasIn(t->lib, c,
263                         mcl->m_len + M_TRAILINGSPACE(mcl));
264         else
265                 retval = LibAliasOut(t->lib, c,
266                         mcl->m_len + M_TRAILINGSPACE(mcl));
267         if (retval == PKT_ALIAS_RESPOND) {
268                 m->m_flags |= M_SKIP_FIREWALL;
269                 retval = PKT_ALIAS_OK;
270         }
271         if (retval != PKT_ALIAS_OK &&
272             retval != PKT_ALIAS_FOUND_HEADER_FRAGMENT) {
273                 /* XXX - should i add some logging? */
274                 m_free(mcl);
275                 args->m = NULL;
276                 return (IP_FW_DENY);
277         }
278         mcl->m_pkthdr.len = mcl->m_len = ntohs(ip->ip_len);
279
280         /*
281          * XXX - libalias checksum offload
282          * 'duct tape' (see above)
283          */
284
285         if ((ip->ip_off & htons(IP_OFFMASK)) == 0 &&
286             ip->ip_p == IPPROTO_TCP) {
287                 struct tcphdr   *th;
288
289                 th = (struct tcphdr *)(ip + 1);
290                 if (th->th_x2)
291                         ldt = 1;
292         }
293
294         if (ldt) {
295                 struct tcphdr   *th;
296                 struct udphdr   *uh;
297                 u_short cksum;
298
299                 ip->ip_len = ntohs(ip->ip_len);
300                 cksum = in_pseudo(ip->ip_src.s_addr, ip->ip_dst.s_addr,
301                     htons(ip->ip_p + ip->ip_len - (ip->ip_hl << 2)));
302
303                 switch (ip->ip_p) {
304                 case IPPROTO_TCP:
305                         th = (struct tcphdr *)(ip + 1);
306                         /*
307                          * Maybe it was set in
308                          * libalias...
309                          */
310                         th->th_x2 = 0;
311                         th->th_sum = cksum;
312                         mcl->m_pkthdr.csum_data =
313                             offsetof(struct tcphdr, th_sum);
314                         break;
315                 case IPPROTO_UDP:
316                         uh = (struct udphdr *)(ip + 1);
317                         uh->uh_sum = cksum;
318                         mcl->m_pkthdr.csum_data =
319                             offsetof(struct udphdr, uh_sum);
320                         break;
321                 }
322                 /* No hw checksum offloading: do it ourselves */
323                 if ((mcl->m_pkthdr.csum_flags & CSUM_DELAY_DATA) == 0) {
324                         in_delayed_cksum(mcl);
325                         mcl->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
326                 }
327                 ip->ip_len = htons(ip->ip_len);
328         }
329         args->m = mcl;
330         return (IP_FW_NAT);
331 }
332
333 static struct cfg_nat *
334 lookup_nat(struct nat_list *l, int nat_id)
335 {
336         struct cfg_nat *res;
337
338         LIST_FOREACH(res, l, _next) {
339                 if (res->id == nat_id)
340                         break;
341         }
342         return res;
343 }
344
345 static int
346 ipfw_nat_cfg(struct sockopt *sopt)
347 {
348         struct cfg_nat *ptr, *ser_n;
349         char *buf;
350         struct ip_fw_chain *chain = &V_layer3_chain;
351
352         buf = malloc(NAT_BUF_LEN, M_IPFW, M_WAITOK | M_ZERO);
353         sooptcopyin(sopt, buf, NAT_BUF_LEN, sizeof(struct cfg_nat));
354         ser_n = (struct cfg_nat *)buf;
355
356         /* check valid parameter ser_n->id > 0 ? */
357         /*
358          * Find/create nat rule.
359          */
360         IPFW_WLOCK(chain);
361         ptr = lookup_nat(&chain->nat, ser_n->id);
362         if (ptr == NULL) {
363                 /* New rule: allocate and init new instance. */
364                 ptr = malloc(sizeof(struct cfg_nat),
365                     M_IPFW, M_NOWAIT | M_ZERO);
366                 if (ptr == NULL) {
367                         IPFW_WUNLOCK(chain);
368                         free(buf, M_IPFW);
369                         return (ENOSPC);
370                 }
371                 ptr->lib = LibAliasInit(NULL);
372                 if (ptr->lib == NULL) {
373                         IPFW_WUNLOCK(chain);
374                         free(ptr, M_IPFW);
375                         free(buf, M_IPFW);
376                         return (EINVAL);
377                 }
378                 LIST_INIT(&ptr->redir_chain);
379         } else {
380                 /* Entry already present: temporarly unhook it. */
381                 LIST_REMOVE(ptr, _next);
382                 flush_nat_ptrs(chain, ser_n->id);
383         }
384         IPFW_WUNLOCK(chain);
385
386         /*
387          * Basic nat configuration.
388          */
389         ptr->id = ser_n->id;
390         /*
391          * XXX - what if this rule doesn't nat any ip and just
392          * redirect?
393          * do we set aliasaddress to 0.0.0.0?
394          */
395         ptr->ip = ser_n->ip;
396         ptr->redir_cnt = ser_n->redir_cnt;
397         ptr->mode = ser_n->mode;
398         LibAliasSetMode(ptr->lib, ser_n->mode, ser_n->mode);
399         LibAliasSetAddress(ptr->lib, ptr->ip);
400         memcpy(ptr->if_name, ser_n->if_name, IF_NAMESIZE);
401
402         /*
403          * Redir and LSNAT configuration.
404          */
405         /* Delete old cfgs. */
406         del_redir_spool_cfg(ptr, &ptr->redir_chain);
407         /* Add new entries. */
408         add_redir_spool_cfg(&buf[(sizeof(struct cfg_nat))], ptr);
409         free(buf, M_IPFW);
410         IPFW_WLOCK(chain);
411         LIST_INSERT_HEAD(&chain->nat, ptr, _next);
412         IPFW_WUNLOCK(chain);
413         return (0);
414 }
415
416 static int
417 ipfw_nat_del(struct sockopt *sopt)
418 {
419         struct cfg_nat *ptr;
420         struct ip_fw_chain *chain = &V_layer3_chain;
421         int i;
422
423         sooptcopyin(sopt, &i, sizeof i, sizeof i);
424         /* XXX validate i */
425         IPFW_WLOCK(chain);
426         ptr = lookup_nat(&chain->nat, i);
427         if (ptr == NULL) {
428                 IPFW_WUNLOCK(chain);
429                 return (EINVAL);
430         }
431         LIST_REMOVE(ptr, _next);
432         flush_nat_ptrs(chain, i);
433         IPFW_WUNLOCK(chain);
434         del_redir_spool_cfg(ptr, &ptr->redir_chain);
435         LibAliasUninit(ptr->lib);
436         free(ptr, M_IPFW);
437         return (0);
438 }
439
440 static int
441 ipfw_nat_get_cfg(struct sockopt *sopt)
442 {
443         uint8_t *data;
444         struct cfg_nat *n;
445         struct cfg_redir *r;
446         struct cfg_spool *s;
447         int nat_cnt, off;
448         struct ip_fw_chain *chain;
449         int err = ENOSPC;
450
451         chain = &V_layer3_chain;
452         nat_cnt = 0;
453         off = sizeof(nat_cnt);
454
455         data = malloc(NAT_BUF_LEN, M_IPFW, M_WAITOK | M_ZERO);
456         IPFW_RLOCK(chain);
457         /* Serialize all the data. */
458         LIST_FOREACH(n, &chain->nat, _next) {
459                 nat_cnt++;
460                 if (off + SOF_NAT >= NAT_BUF_LEN)
461                         goto nospace;
462                 bcopy(n, &data[off], SOF_NAT);
463                 off += SOF_NAT;
464                 LIST_FOREACH(r, &n->redir_chain, _next) {
465                         if (off + SOF_REDIR >= NAT_BUF_LEN)
466                                 goto nospace;
467                         bcopy(r, &data[off], SOF_REDIR);
468                         off += SOF_REDIR;
469                         LIST_FOREACH(s, &r->spool_chain, _next) {
470                                 if (off + SOF_SPOOL >= NAT_BUF_LEN)
471                                         goto nospace;
472                                 bcopy(s, &data[off], SOF_SPOOL);
473                                 off += SOF_SPOOL;
474                         }
475                 }
476         }
477         err = 0; /* all good */
478 nospace:
479         IPFW_RUNLOCK(chain);
480         if (err == 0) {
481                 bcopy(&nat_cnt, data, sizeof(nat_cnt));
482                 sooptcopyout(sopt, data, NAT_BUF_LEN);
483         } else {
484                 printf("serialized data buffer not big enough:"
485                     "please increase NAT_BUF_LEN\n");
486         }
487         free(data, M_IPFW);
488         return (err);
489 }
490
491 static int
492 ipfw_nat_get_log(struct sockopt *sopt)
493 {
494         uint8_t *data;
495         struct cfg_nat *ptr;
496         int i, size;
497         struct ip_fw_chain *chain;
498
499         chain = &V_layer3_chain;
500
501         IPFW_RLOCK(chain);
502         /* one pass to count, one to copy the data */
503         i = 0;
504         LIST_FOREACH(ptr, &chain->nat, _next) {
505                 if (ptr->lib->logDesc == NULL)
506                         continue;
507                 i++;
508         }
509         size = i * (LIBALIAS_BUF_SIZE + sizeof(int));
510         data = malloc(size, M_IPFW, M_NOWAIT | M_ZERO);
511         if (data == NULL) {
512                 IPFW_RUNLOCK(chain);
513                 return (ENOSPC);
514         }
515         i = 0;
516         LIST_FOREACH(ptr, &chain->nat, _next) {
517                 if (ptr->lib->logDesc == NULL)
518                         continue;
519                 bcopy(&ptr->id, &data[i], sizeof(int));
520                 i += sizeof(int);
521                 bcopy(ptr->lib->logDesc, &data[i], LIBALIAS_BUF_SIZE);
522                 i += LIBALIAS_BUF_SIZE;
523         }
524         IPFW_RUNLOCK(chain);
525         sooptcopyout(sopt, data, size);
526         free(data, M_IPFW);
527         return(0);
528 }
529
530 static void
531 ipfw_nat_init(void)
532 {
533
534         IPFW_WLOCK(&V_layer3_chain);
535         /* init ipfw hooks */
536         ipfw_nat_ptr = ipfw_nat;
537         lookup_nat_ptr = lookup_nat;
538         ipfw_nat_cfg_ptr = ipfw_nat_cfg;
539         ipfw_nat_del_ptr = ipfw_nat_del;
540         ipfw_nat_get_cfg_ptr = ipfw_nat_get_cfg;
541         ipfw_nat_get_log_ptr = ipfw_nat_get_log;
542         IPFW_WUNLOCK(&V_layer3_chain);
543         V_ifaddr_event_tag = EVENTHANDLER_REGISTER(
544             ifaddr_event, ifaddr_change,
545             NULL, EVENTHANDLER_PRI_ANY);
546 }
547
548 static void
549 ipfw_nat_destroy(void)
550 {
551         struct cfg_nat *ptr, *ptr_temp;
552         struct ip_fw_chain *chain;
553
554         chain = &V_layer3_chain;
555         IPFW_WLOCK(chain);
556         LIST_FOREACH_SAFE(ptr, &chain->nat, _next, ptr_temp) {
557                 LIST_REMOVE(ptr, _next);
558                 del_redir_spool_cfg(ptr, &ptr->redir_chain);
559                 LibAliasUninit(ptr->lib);
560                 free(ptr, M_IPFW);
561         }
562         EVENTHANDLER_DEREGISTER(ifaddr_event, V_ifaddr_event_tag);
563         flush_nat_ptrs(chain, -1 /* flush all */);
564         /* deregister ipfw_nat */
565         ipfw_nat_ptr = NULL;
566         lookup_nat_ptr = NULL;
567         ipfw_nat_cfg_ptr = NULL;
568         ipfw_nat_del_ptr = NULL;
569         ipfw_nat_get_cfg_ptr = NULL;
570         ipfw_nat_get_log_ptr = NULL;
571         IPFW_WUNLOCK(chain);
572 }
573
574 static int
575 ipfw_nat_modevent(module_t mod, int type, void *unused)
576 {
577         int err = 0;
578
579         switch (type) {
580         case MOD_LOAD:
581                 ipfw_nat_init();
582                 break;
583
584         case MOD_UNLOAD:
585                 ipfw_nat_destroy();
586                 break;
587
588         default:
589                 return EOPNOTSUPP;
590                 break;
591         }
592         return err;
593 }
594
595 static moduledata_t ipfw_nat_mod = {
596         "ipfw_nat",
597         ipfw_nat_modevent,
598         0
599 };
600
601 DECLARE_MODULE(ipfw_nat, ipfw_nat_mod, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY);
602 MODULE_DEPEND(ipfw_nat, libalias, 1, 1, 1);
603 MODULE_DEPEND(ipfw_nat, ipfw, 2, 2, 2);
604 MODULE_VERSION(ipfw_nat, 1);
605 /* end of file */