- Check for packets that are looping back.
[oweals/tinc.git] / src / net.c
index 049f28e3dc9fb930cccafcdca9fcab5e62afe649..6e4fa6632219038614d8d9f04ccd271fdf2eb5fc 100644 (file)
--- a/src/net.c
+++ b/src/net.c
@@ -17,7 +17,7 @@
     along with this program; if not, write to the Free Software
     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
-    $Id: net.c,v 1.35.4.58 2000/11/03 22:33:16 zarq Exp $
+    $Id: net.c,v 1.35.4.65 2000/11/04 17:09:10 guus Exp $
 */
 
 #include "config.h"
@@ -78,32 +78,6 @@ char *interface_name = NULL;  /* Contains the name of the interface */
 
 subnet_t mymac;
 
-/*
-  strip off the MAC adresses of an ethernet frame
-*/
-void strip_mac_addresses(vpn_packet_t *p)
-{
-cp
-  memmove(p->data, p->data + 12, p->len -= 12);
-cp
-}
-
-/*
-  reassemble MAC addresses
-*/
-void add_mac_addresses(vpn_packet_t *p)
-{
-cp
-  memcpy(p->data + 12, p->data, p->len);
-  p->len += 12;
-  p->data[0] = p->data[6] = 0xfe;
-  p->data[1] = p->data[7] = 0xfd;
-  /* Really evil pointer stuff just below! */
-  *((ip_t*)(&p->data[2])) = (ip_t)(htonl(myself->address));
-  *((ip_t*)(&p->data[8])) = *((ip_t*)(&p->data[26]));
-cp
-}
-
 /*
   Execute the given script.
   This function doesn't really belong here.
@@ -112,10 +86,7 @@ int execute_script(const char* name)
 {
   char *scriptname;
   pid_t pid;
-  char **env;
-  extern char **environment;  /* From tincd.c; contains our env */
-
-  asprintf(&scriptname, "%s/%s", confbase, name);
+  char *s;
 
   if((pid = fork()) < 0)
     {
@@ -126,16 +97,34 @@ int execute_script(const char* name)
 
   if(pid)
     {
-      free(scriptname);
       return 0;
     }
 
   /* Child here */
 
-  env = xmalloc(sizeof(environment) + 1 * sizeof(char*));
-  memcpy(&(env[1]), environment, sizeof(environment));
-  asprintf(&(env[0]), "IFNAME=%s", interface_name);
-  execle(scriptname, NULL, env);
+  asprintf(&scriptname, "%s/%s", confbase, name);
+  asprintf(&s, "IFNAME=%s", interface_name);
+  putenv(s);
+  free(s);
+
+  if(netname)
+    {
+      asprintf(&s, "NETNAME=%s", netname);
+      putenv(s);
+      free(s);
+    }
+  else
+    {
+      unsetenv("NETNAME");
+    }
+
+  if(chdir(confbase) < 0)
+    {
+      syslog(LOG_ERR, _("Couldn't chdir to `%s': %m"),
+            confbase);
+    }
+  
+  execl(scriptname, NULL);
   /* No return on success */
   
   if(errno != ENOENT)  /* Ignore if the file does not exist */
@@ -369,10 +358,21 @@ cp
         }
 
       return -1;
-   }
+    }
 
   cl = subnet->owner;
     
+  if(cl == myself)
+    {
+      if(debug_lvl >= DEBUG_TRAFFIC)
+        {
+          syslog(LOG_NOTICE, _("Packet with destination %d.%d.%d.%d is looping back to us!"),
+                IP_ADDR_V(to));
+        }
+
+      return -1;
+    }
+
   /* If we ourselves have indirectdata flag set, we should send only to our uplink! */
 
   /* FIXME - check for indirection and reprogram it The Right Way(tm) this time. */
@@ -929,7 +929,6 @@ cp
 void close_network_connections(void)
 {
   conn_list_t *p;
-  char *scriptname;
 cp
   for(p = conn_list; p != NULL; p = p->next)
     {
@@ -946,10 +945,11 @@ cp
         myself = NULL;
       }
 
-  /* Execute tinc-down script right before shutting down the interface */
+  close(tap_fd);
+
+  /* Execute tinc-down script right after shutting down the interface */
   execute_script("tinc-down");
 
-  close(tap_fd);
   destroy_conn_list();
 
   syslog(LOG_NOTICE, _("Terminating"));