xref: /freebsd/usr.sbin/ppp/nat_cmd.c (revision d429ea332342fcb98d27a350d0c4944bf9aec3f9)
1 /*-
2  * Copyright (c) 2001 Charles Mott <cm@linktel.net>
3  *                    Brian Somers <brian@Awfulhak.org>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  *
27  * $FreeBSD$
28  */
29 
30 #include <sys/param.h>
31 #include <netinet/in.h>
32 #include <arpa/inet.h>
33 #include <netdb.h>
34 #include <netinet/in_systm.h>
35 #include <netinet/in.h>
36 #include <netinet/ip.h>
37 #include <sys/socket.h>
38 #include <sys/un.h>
39 
40 #include <stdarg.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <termios.h>
45 
46 #ifdef LOCALNAT
47 #include "alias.h"
48 #else
49 #include <alias.h>
50 #endif
51 
52 #include "layer.h"
53 #include "proto.h"
54 #include "defs.h"
55 #include "command.h"
56 #include "log.h"
57 #include "nat_cmd.h"
58 #include "descriptor.h"
59 #include "prompt.h"
60 #include "timer.h"
61 #include "fsm.h"
62 #include "slcompress.h"
63 #include "throughput.h"
64 #include "iplist.h"
65 #include "mbuf.h"
66 #include "lqr.h"
67 #include "hdlc.h"
68 #include "ncpaddr.h"
69 #include "ip.h"
70 #include "ipcp.h"
71 #include "ipv6cp.h"
72 #include "lcp.h"
73 #include "ccp.h"
74 #include "link.h"
75 #include "mp.h"
76 #include "filter.h"
77 #ifndef NORADIUS
78 #include "radius.h"
79 #endif
80 #include "ncp.h"
81 #include "bundle.h"
82 
83 
84 #define NAT_EXTRABUF (13)
85 
86 static int StrToAddr(const char *, struct in_addr *);
87 static int StrToPortRange(const char *, u_short *, u_short *, const char *);
88 static int StrToAddrAndPort(const char *, struct in_addr *, u_short *,
89                             u_short *, const char *);
90 
91 static void
92 lowhigh(u_short *a, u_short *b)
93 {
94   if (a > b) {
95     u_short c;
96 
97     c = *b;
98     *b = *a;
99     *a = c;
100   }
101 }
102 
103 int
104 nat_RedirectPort(struct cmdargs const *arg)
105 {
106   if (!arg->bundle->NatEnabled) {
107     prompt_Printf(arg->prompt, "Alias not enabled\n");
108     return 1;
109   } else if (arg->argc == arg->argn + 3 || arg->argc == arg->argn + 4) {
110     char proto_constant;
111     const char *proto;
112     struct in_addr localaddr;
113     u_short hlocalport, llocalport;
114     struct in_addr aliasaddr;
115     u_short haliasport, laliasport;
116     struct in_addr remoteaddr;
117     u_short hremoteport, lremoteport;
118     struct alias_link *link;
119     int error;
120 
121     proto = arg->argv[arg->argn];
122     if (strcmp(proto, "tcp") == 0) {
123       proto_constant = IPPROTO_TCP;
124     } else if (strcmp(proto, "udp") == 0) {
125       proto_constant = IPPROTO_UDP;
126     } else {
127       prompt_Printf(arg->prompt, "port redirect: protocol must be"
128                     " tcp or udp\n");
129       return -1;
130     }
131 
132     error = StrToAddrAndPort(arg->argv[arg->argn+1], &localaddr, &llocalport,
133                              &hlocalport, proto);
134     if (error) {
135       prompt_Printf(arg->prompt, "nat port: error reading localaddr:port\n");
136       return -1;
137     }
138 
139     error = StrToPortRange(arg->argv[arg->argn+2], &laliasport, &haliasport,
140                            proto);
141     if (error) {
142       prompt_Printf(arg->prompt, "nat port: error reading alias port\n");
143       return -1;
144     }
145     aliasaddr.s_addr = INADDR_ANY;
146 
147     if (arg->argc == arg->argn + 4) {
148       error = StrToAddrAndPort(arg->argv[arg->argn+3], &remoteaddr,
149                                &lremoteport, &hremoteport, proto);
150       if (error) {
151         prompt_Printf(arg->prompt, "nat port: error reading "
152                       "remoteaddr:port\n");
153         return -1;
154       }
155     } else {
156       remoteaddr.s_addr = INADDR_ANY;
157       lremoteport = hremoteport = 0;
158     }
159 
160     lowhigh(&llocalport, &hlocalport);
161     lowhigh(&laliasport, &haliasport);
162     lowhigh(&lremoteport, &hremoteport);
163 
164     if (haliasport - laliasport != hlocalport - llocalport) {
165       prompt_Printf(arg->prompt, "nat port: local & alias port ranges "
166                     "are not equal\n");
167       return -1;
168     }
169 
170     if (hremoteport && hremoteport - lremoteport != hlocalport - llocalport) {
171       prompt_Printf(arg->prompt, "nat port: local & remote port ranges "
172                     "are not equal\n");
173       return -1;
174     }
175 
176     while (laliasport <= haliasport) {
177       link = PacketAliasRedirectPort(localaddr, htons(llocalport),
178 				     remoteaddr, htons(lremoteport),
179                                      aliasaddr, htons(laliasport),
180 				     proto_constant);
181 
182       if (link == NULL) {
183         prompt_Printf(arg->prompt, "nat port: %d: error %d\n", laliasport,
184                       error);
185         return 1;
186       }
187       llocalport++;
188       laliasport++;
189       if (hremoteport)
190         lremoteport++;
191     }
192 
193     return 0;
194   }
195 
196   return -1;
197 }
198 
199 
200 int
201 nat_RedirectAddr(struct cmdargs const *arg)
202 {
203   if (!arg->bundle->NatEnabled) {
204     prompt_Printf(arg->prompt, "nat not enabled\n");
205     return 1;
206   } else if (arg->argc == arg->argn+2) {
207     int error;
208     struct in_addr localaddr, aliasaddr;
209     struct alias_link *link;
210 
211     error = StrToAddr(arg->argv[arg->argn], &localaddr);
212     if (error) {
213       prompt_Printf(arg->prompt, "address redirect: invalid local address\n");
214       return 1;
215     }
216     error = StrToAddr(arg->argv[arg->argn+1], &aliasaddr);
217     if (error) {
218       prompt_Printf(arg->prompt, "address redirect: invalid alias address\n");
219       prompt_Printf(arg->prompt, "usage: nat %s %s\n", arg->cmd->name,
220                     arg->cmd->syntax);
221       return 1;
222     }
223     link = PacketAliasRedirectAddr(localaddr, aliasaddr);
224     if (link == NULL) {
225       prompt_Printf(arg->prompt, "address redirect: packet aliasing"
226                     " engine error\n");
227       prompt_Printf(arg->prompt, "usage: nat %s %s\n", arg->cmd->name,
228                     arg->cmd->syntax);
229     }
230   } else
231     return -1;
232 
233   return 0;
234 }
235 
236 
237 int
238 nat_RedirectProto(struct cmdargs const *arg)
239 {
240   if (!arg->bundle->NatEnabled) {
241     prompt_Printf(arg->prompt, "nat not enabled\n");
242     return 1;
243   } else if (arg->argc >= arg->argn + 2 && arg->argc <= arg->argn + 4) {
244     struct in_addr localIP, publicIP, remoteIP;
245     struct alias_link *link;
246     struct protoent *pe;
247     int error;
248     unsigned len;
249 
250     len = strlen(arg->argv[arg->argn]);
251     if (len == 0) {
252       prompt_Printf(arg->prompt, "proto redirect: invalid protocol\n");
253       return 1;
254     }
255     if (strspn(arg->argv[arg->argn], "01234567") == len)
256       pe = getprotobynumber(atoi(arg->argv[arg->argn]));
257     else
258       pe = getprotobyname(arg->argv[arg->argn]);
259     if (pe == NULL) {
260       prompt_Printf(arg->prompt, "proto redirect: invalid protocol\n");
261       return 1;
262     }
263 
264     error = StrToAddr(arg->argv[arg->argn + 1], &localIP);
265     if (error) {
266       prompt_Printf(arg->prompt, "proto redirect: invalid src address\n");
267       return 1;
268     }
269 
270     if (arg->argc >= arg->argn + 3) {
271       error = StrToAddr(arg->argv[arg->argn + 2], &publicIP);
272       if (error) {
273         prompt_Printf(arg->prompt, "proto redirect: invalid alias address\n");
274         prompt_Printf(arg->prompt, "usage: nat %s %s\n", arg->cmd->name,
275                       arg->cmd->syntax);
276         return 1;
277       }
278     } else
279       publicIP.s_addr = INADDR_ANY;
280 
281     if (arg->argc == arg->argn + 4) {
282       error = StrToAddr(arg->argv[arg->argn + 2], &remoteIP);
283       if (error) {
284         prompt_Printf(arg->prompt, "proto redirect: invalid dst address\n");
285         prompt_Printf(arg->prompt, "usage: nat %s %s\n", arg->cmd->name,
286                       arg->cmd->syntax);
287         return 1;
288       }
289     } else
290       remoteIP.s_addr = INADDR_ANY;
291 
292     link = PacketAliasRedirectProto(localIP, remoteIP, publicIP, pe->p_proto);
293     if (link == NULL) {
294       prompt_Printf(arg->prompt, "proto redirect: packet aliasing"
295                     " engine error\n");
296       prompt_Printf(arg->prompt, "usage: nat %s %s\n", arg->cmd->name,
297                     arg->cmd->syntax);
298     }
299   } else
300     return -1;
301 
302   return 0;
303 }
304 
305 
306 static int
307 StrToAddr(const char *str, struct in_addr *addr)
308 {
309   struct hostent *hp;
310 
311   if (inet_aton(str, addr))
312     return 0;
313 
314   hp = gethostbyname(str);
315   if (!hp) {
316     log_Printf(LogWARN, "StrToAddr: Unknown host %s.\n", str);
317     return -1;
318   }
319   *addr = *((struct in_addr *) hp->h_addr);
320   return 0;
321 }
322 
323 
324 static int
325 StrToPort(const char *str, u_short *port, const char *proto)
326 {
327   struct servent *sp;
328   char *end;
329 
330   *port = strtol(str, &end, 10);
331   if (*end != '\0') {
332     sp = getservbyname(str, proto);
333     if (sp == NULL) {
334       log_Printf(LogWARN, "StrToAddr: Unknown port or service %s/%s.\n",
335 	        str, proto);
336       return -1;
337     }
338     *port = ntohs(sp->s_port);
339   }
340 
341   return 0;
342 }
343 
344 static int
345 StrToPortRange(const char *str, u_short *low, u_short *high, const char *proto)
346 {
347   char *minus;
348   int res;
349 
350   minus = strchr(str, '-');
351   if (minus)
352     *minus = '\0';		/* Cheat the const-ness ! */
353 
354   res = StrToPort(str, low, proto);
355 
356   if (minus)
357     *minus = '-';		/* Cheat the const-ness ! */
358 
359   if (res == 0) {
360     if (minus)
361       res = StrToPort(minus + 1, high, proto);
362     else
363       *high = *low;
364   }
365 
366   return res;
367 }
368 
369 static int
370 StrToAddrAndPort(const char *str, struct in_addr *addr, u_short *low,
371                  u_short *high, const char *proto)
372 {
373   char *colon;
374   int res;
375 
376   colon = strchr(str, ':');
377   if (!colon) {
378     log_Printf(LogWARN, "StrToAddrAndPort: %s is missing port number.\n", str);
379     return -1;
380   }
381 
382   *colon = '\0';		/* Cheat the const-ness ! */
383   res = StrToAddr(str, addr);
384   *colon = ':';			/* Cheat the const-ness ! */
385   if (res != 0)
386     return -1;
387 
388   return StrToPortRange(colon + 1, low, high, proto);
389 }
390 
391 int
392 nat_ProxyRule(struct cmdargs const *arg)
393 {
394   char cmd[LINE_LEN];
395   int f, pos;
396   size_t len;
397 
398   if (arg->argn >= arg->argc)
399     return -1;
400 
401   for (f = arg->argn, pos = 0; f < arg->argc; f++) {
402     len = strlen(arg->argv[f]);
403     if (sizeof cmd - pos < len + (len ? 1 : 0))
404       break;
405     if (len)
406       cmd[pos++] = ' ';
407     strcpy(cmd + pos, arg->argv[f]);
408     pos += len;
409   }
410 
411   return PacketAliasProxyRule(cmd);
412 }
413 
414 int
415 nat_SetTarget(struct cmdargs const *arg)
416 {
417   struct in_addr addr;
418 
419   if (arg->argc == arg->argn) {
420     addr.s_addr = INADDR_ANY;
421     PacketAliasSetTarget(addr);
422     return 0;
423   }
424 
425   if (arg->argc != arg->argn + 1)
426     return -1;
427 
428   if (!strcasecmp(arg->argv[arg->argn], "MYADDR")) {
429     addr.s_addr = INADDR_ANY;
430     PacketAliasSetTarget(addr);
431     return 0;
432   }
433 
434   addr = GetIpAddr(arg->argv[arg->argn]);
435   if (addr.s_addr == INADDR_NONE) {
436     log_Printf(LogWARN, "%s: invalid address\n", arg->argv[arg->argn]);
437     return 1;
438   }
439 
440   PacketAliasSetTarget(addr);
441   return 0;
442 }
443 
444 #ifndef NO_FW_PUNCH
445 int
446 nat_PunchFW(struct cmdargs const *arg)
447 {
448   char *end;
449   long base, count;
450 
451   if (arg->argc == arg->argn) {
452     PacketAliasSetMode(0, PKT_ALIAS_PUNCH_FW);
453     return 0;
454   }
455 
456   if (arg->argc != arg->argn + 2)
457     return -1;
458 
459   base = strtol(arg->argv[arg->argn], &end, 10);
460   if (*end != '\0' || base < 0)
461     return -1;
462 
463   count = strtol(arg->argv[arg->argn + 1], &end, 10);
464   if (*end != '\0' || count < 0)
465     return -1;
466 
467   PacketAliasSetFWBase(base, count);
468   PacketAliasSetMode(PKT_ALIAS_PUNCH_FW, PKT_ALIAS_PUNCH_FW);
469 
470   return 0;
471 }
472 #endif
473 
474 int
475 nat_SkinnyPort(struct cmdargs const *arg)
476 {
477   char *end;
478   long port;
479 
480   if (arg->argc == arg->argn) {
481     PacketAliasSetSkinnyPort(0);
482     return 0;
483   }
484 
485   if (arg->argc != arg->argn + 1)
486     return -1;
487 
488   port = strtol(arg->argv[arg->argn], &end, 10);
489   if (*end != '\0' || port < 0)
490     return -1;
491 
492   PacketAliasSetSkinnyPort(port);
493 
494   return 0;
495 }
496 
497 static struct mbuf *
498 nat_LayerPush(struct bundle *bundle, struct link *l __unused, struct mbuf *bp,
499                 int pri __unused, u_short *proto)
500 {
501   if (!bundle->NatEnabled || *proto != PROTO_IP)
502     return bp;
503 
504   log_Printf(LogDEBUG, "nat_LayerPush: PROTO_IP -> PROTO_IP\n");
505   m_settype(bp, MB_NATOUT);
506   /* Ensure there's a bit of extra buffer for the NAT code... */
507   bp = m_pullup(m_append(bp, NULL, NAT_EXTRABUF));
508   PacketAliasOut(MBUF_CTOP(bp), bp->m_len);
509   bp->m_len = ntohs(((struct ip *)MBUF_CTOP(bp))->ip_len);
510 
511   return bp;
512 }
513 
514 static struct mbuf *
515 nat_LayerPull(struct bundle *bundle, struct link *l __unused, struct mbuf *bp,
516                 u_short *proto)
517 {
518   static int gfrags;
519   int ret, len, nfrags;
520   struct mbuf **last;
521   char *fptr;
522 
523   if (!bundle->NatEnabled || *proto != PROTO_IP)
524     return bp;
525 
526   log_Printf(LogDEBUG, "nat_LayerPull: PROTO_IP -> PROTO_IP\n");
527   m_settype(bp, MB_NATIN);
528   /* Ensure there's a bit of extra buffer for the NAT code... */
529   bp = m_pullup(m_append(bp, NULL, NAT_EXTRABUF));
530   ret = PacketAliasIn(MBUF_CTOP(bp), bp->m_len);
531 
532   bp->m_len = ntohs(((struct ip *)MBUF_CTOP(bp))->ip_len);
533   if (bp->m_len > MAX_MRU) {
534     log_Printf(LogWARN, "nat_LayerPull: Problem with IP header length (%lu)\n",
535                (unsigned long)bp->m_len);
536     m_freem(bp);
537     return NULL;
538   }
539 
540   switch (ret) {
541     case PKT_ALIAS_OK:
542       break;
543 
544     case PKT_ALIAS_UNRESOLVED_FRAGMENT:
545       /* Save the data for later */
546       if ((fptr = malloc(bp->m_len)) == NULL) {
547 	log_Printf(LogWARN, "nat_LayerPull: Dropped unresolved fragment -"
548 		   " out of memory!\n");
549 	m_freem(bp);
550 	bp = NULL;
551       } else {
552 	bp = mbuf_Read(bp, fptr, bp->m_len);
553 	PacketAliasSaveFragment(fptr);
554 	log_Printf(LogDEBUG, "Store another frag (%lu) - now %d\n",
555 		   (unsigned long)((struct ip *)fptr)->ip_id, ++gfrags);
556       }
557       break;
558 
559     case PKT_ALIAS_FOUND_HEADER_FRAGMENT:
560       /* Fetch all the saved fragments and chain them on the end of `bp' */
561       last = &bp->m_nextpkt;
562       nfrags = 0;
563       while ((fptr = PacketAliasGetFragment(MBUF_CTOP(bp))) != NULL) {
564         nfrags++;
565         PacketAliasFragmentIn(MBUF_CTOP(bp), fptr);
566         len = ntohs(((struct ip *)fptr)->ip_len);
567         *last = m_get(len, MB_NATIN);
568         memcpy(MBUF_CTOP(*last), fptr, len);
569         free(fptr);
570         last = &(*last)->m_nextpkt;
571       }
572       gfrags -= nfrags;
573       log_Printf(LogDEBUG, "Found a frag header (%lu) - plus %d more frags (no"
574                  "w %d)\n", (unsigned long)((struct ip *)MBUF_CTOP(bp))->ip_id,
575                  nfrags, gfrags);
576       break;
577 
578     case PKT_ALIAS_IGNORED:
579       if (PacketAliasSetMode(0, 0) & PKT_ALIAS_DENY_INCOMING) {
580         log_Printf(LogTCPIP, "NAT engine denied data:\n");
581         m_freem(bp);
582         bp = NULL;
583       } else if (log_IsKept(LogTCPIP)) {
584         log_Printf(LogTCPIP, "NAT engine ignored data:\n");
585         PacketCheck(bundle, AF_INET, MBUF_CTOP(bp), bp->m_len, NULL,
586                     NULL, NULL);
587       }
588       break;
589 
590     default:
591       log_Printf(LogWARN, "nat_LayerPull: Dropped a packet (%d)....\n", ret);
592       m_freem(bp);
593       bp = NULL;
594       break;
595   }
596 
597   return bp;
598 }
599 
600 struct layer natlayer =
601   { LAYER_NAT, "nat", nat_LayerPush, nat_LayerPull };
602