Re: Forcing packets through an interface in a multihomed machine

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Howdie,

On Monday 26 January 2004 13:49, C Jagadish wrote:
> Hi,
>
>         How do I route packets to destination "A" through an interface
>         differnet from the entry in the routing table?     ( In RedHat
> Linux 7.2)

I don't think you can do this exactly.

However, you can limit a packet to go through a specific interface assuming 
you have a route through it even though normally this will not be the route 
chosen, by using the SO_BINDTODEVICE socket options.

Attached is small example that creates a shared library that if preloaded 
(using LD_PRELOAD) will hijack all the socket() system calls and set this 
option on all sockets created, thus enabling to limit any executable to use a 
specific interface in a similar fashion to way the ping program -r option 
does for ping.

It's a rather crude example, if you plan to make real use of it make sure to 
handle error conditions in saner fashion. A better way to choose the 
interface to route packets to is also a good idea :-)


Cheers,
Gilad


-- 
Gilad Ben-Yossef <gilad@codefidence.com>
Codefidence. A name you can trust (tm)
http://www.codefidence.com

"The future is here,  it's just not evenly distributed yet." 
	- William Gibson
/************************************************************
* Copyright (C) 2004 Codefidence ltd.
* Author: Gilad Ben-Yossef <gilad@codefidence.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  
* 02111-1307, USA.
*/

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <net/if.h>
#define __USE_GNU // This is needed for the RTLD_NEXT definition
#include <dlfcn.h>

/* Change this to route the packet via a different interface. */

#define IFNAME "eth3"

int socket(int domain, int type, int protocol) {

	int (*origsock)(int domain, int type, int protocol);
	char * error;
	int sock;
	
	origsock = dlsym(RTLD_NEXT, "socket");
	if ((error = dlerror()) != NULL) {
		fprintf (stderr, "%s\n", error);
		exit(1);
	}
	
	sock = origsock(domain, type, protocol);
	if(sock != -1) {
		struct ifreq interface;

		strncpy(interface.ifr_ifrn.ifrn_name, IFNAME, IFNAMSIZ);
		if (setsockopt(sock, SOL_SOCKET, SO_BINDTODEVICE, (char *)&interface, sizeof(interface)) < 0) {
			perror("sendpacket: setting SO_BINDTODEVICE");
			exit(1);
		}
	}
	return sock;
}

/* 
	Compile with:
	gcc sendto.c -o sendto.so -ldl -shared

	Use with:
	LD_PRELOAD=sendto.so ./program
*/

[Index of Archives]     [Netdev]     [Ethernet Bridging]     [Linux 802.1Q VLAN]     [Linux Wireless]     [Kernel Newbies]     [Security]     [Linux for Hams]     [Netfilter]     [Git]     [Bugtraq]     [Yosemite News and Information]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux PCI]     [Linux Admin]     [Samba]

  Powered by Linux