Beispiel #1
0
int
arp_remove_ip(char ip_addr[4]) {
  unsigned int ip;
  int status;
  unsigned int arp_envid;

  arp_envid = get_arp_envid();
  if (arp_envid == 0) {
    printf("Arp daemon not running\n");
    return -1;
  }
  
  /* make sure is word aligned */
  memcpy(&ip ,ip_addr, 4);
  printf("ARP_ENVID: %d. SIPCOUT ip: %s\n",arp_envid,pripaddr((char *)&ip));
  status = sipcout(arp_envid,IPC_ARP_DELETE,ip,0,0);
  switch(status) {
  case -1: printf("SIPCOUT ERROR: %d, arp_envid: %d\n",status,arp_envid); return -1;
  case ARP_SUCCESS: return 0;
  case ARP_NOTFOUND: printf("Entry %s not found\n",pripaddr(ip_addr)); return -1;
  default:
    assert(0);			/* bad return code */
    return 0;
  }
}
Beispiel #2
0
void
print_route(route_entry_p routep) {
  if (routep == NULL) {
    printf("%-15s %-19s %-15s %s\n","Network","Netmask","Destination","Flags");
  } else {
    printf("%-15s ",pripaddr(routep->net));
    printf("%-15s/%3d ",pripaddr(routep->netmask),routep->bitcount);
    printf("%-15s ",pripaddr(routep->dst));
    if (routep->flags & ROUTE_INET) printf("I");
    if (routep->flags & ROUTE_REJECT) printf("R");
    if (routep->flags & ROUTE_BLACKHOLE) printf("B");
    printf("\n");
  }
}
Beispiel #3
0
/* IMPORTANT CALLS */
int
get_dsteth(ipaddr_t dstaddr,ethaddr_t ethaddr,int *ifnum) {
  /* first search interfaces */
  ipaddr_t gateway;

  switch(find_if(dstaddr,ifnum)) {
  case IFRET_LOOPBACK:
    //printf("Directly on interface (loopback)\n");
    bzero(ethaddr,6); 
    return 0;
  case IFRET_RESOLVE:
    //printf("Directly on interface\n");
    return arp_resolve_ip(dstaddr,ethaddr,*ifnum);
  case IFRET_BROADCAST:
    //printf("Broadcast\n");
    memset(ethaddr,0xff,6);
    return 0;
  case IFRET_NOTFOUND:
    /* fall through */
  }
  if (find_route(dstaddr,gateway) == 0) {
    //printf("Via gateway: %s\n",pripaddr(gateway));
    switch(find_if(gateway,ifnum)) {
    case IFRET_LOOPBACK:
      bzero(ethaddr,6); 
      return 0;
    case IFRET_RESOLVE:
      //printf("Directly on interface\n");
      return arp_resolve_ip(gateway,ethaddr,*ifnum);
    case IFRET_BROADCAST:
      //printf("Broadcast\n");
      memset(ethaddr,0xff,6);
      return 0;
    case IFRET_NOTFOUND:
      /* fall through */
    }
  }
    
  printf("get_dsteth: could not find route to destination: %s\n",pripaddr(dstaddr));
  return -1;
}

#define ASSERTIFNUM(ifnum)				\
  EnterCritical(); 					\
  assert(ifnum < GETNUMIFENTRIES && ifnum >= 0);	\
  ExitCritical();

void
get_ifnum_ethernet(int ifnum, ethaddr_t ethaddr) {
  if_entry_p ife;
  ASSERTIFNUM(ifnum);
  EnterCritical();
  ife = GETIFP(ifnum);
  bcopy(ife->ethaddr,ethaddr, 6);
  ExitCritical();
}
Beispiel #4
0
static void
print_if_entry(if_entry_p ife) {
  int notfirst = 0;
  printf("%s: flags=%x<",
	 ife->name, 
	 ife->flags);
  if (ife->flags & IF_UP) printf("%sUP",(notfirst) ? "," : (notfirst++,""));
  if (ife->flags & IF_DOWN) printf("%sDOWN",(notfirst) ? "," : (notfirst++,""));
  if (ife->flags & IF_LOOPBACK) printf("%sLOOPBACK",(notfirst) ? "," : (notfirst++,""));
  if (ife->flags & IF_BROADCAST) printf("%sBROADCAST",(notfirst) ? "," : (notfirst++,""));
  printf("> cardno: %d\n",ife->cardno);
  if (ife->flags & IF_UP) {
    printf("     inet %s",pripaddr(ife->ipaddr));
    printf(" netmask %s",pripaddr(ife->netmask));
    if (ife->flags & IF_BROADCAST)
      printf(" bcast %s",pripaddr(ife->broadcast));
    printf("\n");
  } 
}
Beispiel #5
0
/* make a udp packet-filter */
void *mk_udpc(int *sz, 
	      unsigned short src_port, unsigned char *src_ip,
	      unsigned short dst_port, unsigned char *dst_ip) {
#ifdef PRINTME
  printf("mk_udpc: src ip: %s port: %d\n",pripaddr(src_ip),htons(src_port));
  printf("       : dst ip: %s port: %d\n",pripaddr(dst_ip),htons(dst_port));
#endif
  /* if the source port is any address, don't restrict this field. */
  if (bcmp(src_ip, IPADDR_ANY, 4) == 0) {

    udpc_unbound[6].imm  = dst_ip[2];  
    udpc_unbound[8].imm  = dst_ip[3];

    udpc_unbound[10].imm = dst_ip[0]; 
    udpc_unbound[12].imm = dst_ip[1];


    udpc_unbound[14].imm = dst_port;
    udpc_unbound[16].imm = src_port;

    *sz = sizeof udpc_unbound / sizeof udpc_unbound[0];
    return (void *)udpc_unbound;
  } else {
     udpc[6].imm = dst_ip[0]; 
     udpc[8].imm = dst_ip[1];
    udpc[10].imm = dst_ip[2]; 
    udpc[12].imm = dst_ip[3];

    udpc[14].imm = src_ip[0]; 
    udpc[16].imm = src_ip[1];
    udpc[18].imm = src_ip[2]; 
    udpc[20].imm = src_ip[3];
    
    udpc[22].imm = dst_port;
    udpc[24].imm = src_port;
    *sz = sizeof udpc / sizeof udpc[0];
    return (void *)udpc;
  }
}
Beispiel #6
0
/* Print Functions */
void
arp_print_entry(arp_entry_p e) {
  char *status ="?";
  switch(e->status) {
  case FREE: status = "F"; break;
  case PENDING: status = "P"; break;
  case RESOLVED: status = "R"; break;
  case TIMEOUT: status = "T"; break;
  }
  printf("pid %d: %s (%s) at %s, timeout: %qd:%d ifnum: %d\n",
	 getpid(),status,
	 pripaddr(e->ip),
	 prethaddr(e->eth),
	 e->ticks,
	 e->count,e->ifnum);
}
Beispiel #7
0
int
main(int argc, char **argv) {
  ipaddr_t ipaddr, netmask, broadcast;
  char *name;
  int flags;
  int status;
  int ifnum;
  /* skip progname */
  argv++;
  argc--;

  if (argc >= 1 && !strcmp(argv[0],"-a")) {
    if_show();
    return 0;
  }

  if (argc < 2) return usage(NULL);

  name = argv[0];
  ifnum = get_ifnum_by_name(name);
  if (ifnum == -1) {
    fprintf(stderr,"%s: No such interface\n",name);
    return -1;
  }
#if 0
  {
    ipaddr_t b,i,n;
    int flags;
    get_ifnum_broadcast(ifnum, b);
    get_ifnum_netmask(ifnum,n);
    get_ifnum_ipaddr(ifnum,i);
    get_ifnum_flags(ifnum,&flags);
    printf("BEFORE flags %x:\n",flags);
    printf("BEFORE using bcast:     %s\n",pripaddr(b));
    printf("BEFORE using host:      %s\n",pripaddr(i));
    printf("BEFORE using mask:      %s\n",pripaddr(n));
  }
#endif
  if (!strcmp(argv[1],"up")) {
    flags = IF_UP;
  } else if (!strcmp(argv[1],"down")) {
    flags = IF_DOWN;
  } else {
    return usage(argv[1]);
  }

  argv += 2;
  argc -= 2;

  if (argc && !strcmp(argv[0],"loopback")) {
    flags |= IF_LOOPBACK;
    argv++;
    argc--;
  }
  if (argc && !strcmp(argv[0],"broadcast")) {
    flags |= IF_BROADCAST;
    argv++;
    argc--;
  }

  
  switch(argc) {
  default:
    return usage(NULL);
  case 3:
    if (!inet_aton(argv[2], (struct in_addr *)broadcast)) {
      printf("We currently only accept addresses/mask/nets in the X.X.X.X form\n");
      return usage(NULL);
    }
  case 2:
    if (!inet_aton(argv[1], (struct in_addr *)netmask)) {
      printf("We currently only accept addresses/mask/nets in the X.X.X.X form\n");
      return usage(NULL);
    }
  case 1:
    if (!inet_aton(argv[0], (struct in_addr *)ipaddr)) {
      printf("We currently only accept addresses/mask/nets in the X.X.X.X form\n");
      return usage(NULL);
    }
  case 0:
    switch(argc) {
    case 0: get_ifnum_ipaddr(ifnum,ipaddr);
    case 1: get_ifnum_netmask(ifnum,netmask);
    case 2: get_ifnum_broadcast(ifnum,broadcast);
    default:
    }
  }

  if (flags == IF_DOWN) {
    get_ifnum_flags(ifnum,&flags);
    flags &= ~IF_UP;
    flags |= IF_DOWN;
  }

#if 0
  printf("AFTER flags %x:\n",flags);
  printf("AFTER using bcast:     %s\n",pripaddr(broadcast));
  printf("AFTER using host:      %s\n",pripaddr(ipaddr));
  printf("AFTER using mask:      %s\n",pripaddr(netmask));
#endif

  status= ifconfig(name,flags, ipaddr,netmask, broadcast);
  printf("%s\n",status ? "BAD" : "OK");
  return status;
}
Beispiel #8
0
int
main (int argc, char **argv) {
  int status;
  int i;
  /* configure interface */

  for (i = 0 ; i < (sizeof(ifinfo)/sizeof(struct ifinfo)) ; i++) {
    struct ifinfo *ifn = &ifinfo[i];
    ipaddr_t broadcast;
    
    apply_netmask_broadcast(broadcast,(char *)&ifn->ip, (char *)&ifn->netmask);
    printf(" Name: %s ",ifn->interface);
    printf("Ip Address: %s ",pripaddr((char *)&ifn->ip));
    printf("Netmask: %s ",pripaddr((char *)&ifn->netmask));
    printf("Broadcast: %s\n",pripaddr(broadcast));
    status = ifconfig(ifn->interface,ifn->flags, 
	     (char *)&ifn->ip, (char *)&ifn->netmask,broadcast);
    printf("ifconfig %s => %d\n",ifn->interface,status);
    if (status != 0) {
      printf("ifconfig failed\n");
    }
  }
  /* add default route */
  status = route_add(ROUTE_INET,
		     ((ipaddr_t){0,0,0,0}),
		     ((ipaddr_t){0,0,0,0}),
		     mygateway);
  printf("route add default %s ==> %d\n",pripaddr(mygateway),status);

  /* start arp daemon, necessary to be started before mounting nfs server
   * so that the server can resolve our ip address, and we can resolve his.
   */
  {
    extern void arpd_main (void);
    if (fork () == 0) {
      arpd_main ();
    }
  }

  /* mount the nfs root */
  {
    extern int nfs_mount_root();
    printf("MOUNTING ROOT: ");
    status = nfs_mount_root(nfs_server_ip,nfs_server_path);
    printf("DONE\n");
    if (status != 0) {
      printf("failed (errno: %d)\n",errno);
      printf("check machine \"%s\" /etc/exports file, and make sure that you\n"
	     "either have -alldirs or the exact directory there\n"
	     "you have restarted mountd after making changes\n"
	     "and that you have the proper permission\n",nfs_server_ip);
      printf("fix the problem and try again\n");
    }
  }

  /* configuring lo0: configure loopback interface and route
   * ip packets from our machineip to our machineip via loopback
   * interface
   */
  {
    ipaddr_t lo0mask = {255,255,255,255};
	if_show();
    status = ifconfig("lo0",IF_UP|IF_LOOPBACK,
		      IPADDR_LOOPBACK, lo0mask, IPADDR_BROADCAST);
    printf("ifconfig lo0 => %d\n",status);

    for (i = 0 ; i < (sizeof(ifinfo)/sizeof(struct ifinfo)) ; i++) {
      struct ifinfo *ifn = &ifinfo[i];

      status = route_add(ROUTE_INET,
			 ifn->ip,IPADDR_BROADCAST, IPADDR_LOOPBACK);
      printf("route %s localhost ==> %d\n",pripaddr(ifn->ip),status);
    }
  }
  

  /* set the hostname */
  {
    struct hostent *h;
    if ((h = gethostbyaddr(ifinfo[0].ip,4,AF_INET)) != NULL) {
      printf("My hostname is: %s\n",h->h_name);
      sethostname(h->h_name,strlen(h->h_name));
      setdomainname(DOMAIN,strlen(DOMAIN));
    } else {
      printf("gethostbyaddr %s %d %d failed\n",pripaddr(ifinfo[0].ip),4,AF_INET);
    }
  }

  /* if there is an rc.local files execute it.  this is crucial because 
   * otherwise the machine will simple have mounted a file system and stopped
   */
#ifdef RCLOCAL
  {
    struct stat sb;
    if (stat(RCLOCAL,&sb) == 0) {
      int status;
      sys_cputs("Spawning ");
      sys_cputs(RCLOCAL);
      sys_cputs("\n");
      status = system(RCLOCAL);
      /* child */
      kprintf("%d system(%s) returned %d %d\n",getpid(),RCLOCAL,status,errno);
    }
  }
#endif /* RCLOCAL */

  /* setup reaper */
#if 1
  signal(SIGCHLD,reapchild);
  for(;;) sleep(100);
#endif

  /* the first process now sleeps forever.  It can't die, since it holds
   * references to important resources (like some memory pages)
   */
#if 0
  reapchildren();
#else
  UAREA.u_status = U_SLEEP;
  yield(-1);
#endif
  kprintf("FIRST PROCESS SHOULD NEVER PRINT THIS ==> TROUBLE\n");
  assert(0);
  return 0;
}