From owner-freebsd-net@FreeBSD.ORG Thu Feb 19 15:24:20 2004 Return-Path: Delivered-To: freebsd-net@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 8605916A4CE for ; Thu, 19 Feb 2004 15:24:20 -0800 (PST) Received: from venus.vincentjardin.net (lns-vlq-12-62-147-172-226.adsl.proxad.net [62.147.172.226]) by mx1.FreeBSD.org (Postfix) with ESMTP id 6FF0E43D1F for ; Thu, 19 Feb 2004 15:24:19 -0800 (PST) (envelope-from jardin@venus.vincentjardin.net) Received: from venus.vincentjardin.net (localhost [127.0.0.1]) i1JNS9QR009296; Fri, 20 Feb 2004 00:28:13 +0100 (CET) (envelope-from jardin@venus.vincentjardin.net) Received: from localhost (localhost [[UNIX: localhost]]) by venus.vincentjardin.net (8.12.9/8.12.9/Submit) id i1JNS4nA009295; Fri, 20 Feb 2004 00:28:04 +0100 (CET) From: Vincent Jardin To: Andreas T , freebsd-net@freebsd.org Date: Fri, 20 Feb 2004 00:28:00 +0100 User-Agent: KMail/1.5.2 References: <403487B8.901@olsr.org> In-Reply-To: <403487B8.901@olsr.org> MIME-Version: 1.0 Content-Type: Text/Plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable Content-Description: clearsigned data Content-Disposition: inline Message-Id: <200402200028.04226.vjardin@free.fr> Subject: Re: Binding sockets to devices in FreeBSD X-BeenThere: freebsd-net@freebsd.org X-Mailman-Version: 2.1.1 Precedence: list List-Id: Networking and TCP/IP with FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 19 Feb 2004 23:24:20 -0000 Hi, You need to use sendto or sendmsg. =46or example, with IPv6, Quagga is sending IPv6 multicast packet on a per= =20 interface basis. The interface's ifindex is provided into the pktinfo=20 structure. int ripng_send_packet (caddr_t buf, int bufsize, struct sockaddr_in6 *to, struct interface *ifp) { int ret; struct msghdr msg; struct iovec iov; struct cmsghdr *cmsgptr; char adata [256]; struct in6_pktinfo *pkt; struct sockaddr_in6 addr; [...] memset (&addr, 0, sizeof (struct sockaddr_in6)); addr.sin6_family =3D AF_INET6; #ifdef SIN6_LEN addr.sin6_len =3D sizeof (struct sockaddr_in6); #endif /* SIN6_LEN */ addr.sin6_flowinfo =3D htonl (RIPNG_PRIORITY_DEFAULT); [...] inet_pton(AF_INET6, RIPNG_GROUP, &addr.sin6_addr); addr.sin6_port =3D htons (RIPNG_PORT_DEFAULT); [...] msg.msg_name =3D (void *) &addr; msg.msg_namelen =3D sizeof (struct sockaddr_in6); msg.msg_iov =3D &iov; msg.msg_iovlen =3D 1; msg.msg_control =3D (void *) adata; msg.msg_controllen =3D CMSG_SPACE(sizeof(struct in6_pktinfo)); iov.iov_base =3D buf; iov.iov_len =3D bufsize; cmsgptr =3D (struct cmsghdr *)adata; cmsgptr->cmsg_len =3D CMSG_LEN(sizeof (struct in6_pktinfo)); cmsgptr->cmsg_level =3D IPPROTO_IPV6; cmsgptr->cmsg_type =3D IPV6_PKTINFO; pkt =3D (struct in6_pktinfo *) CMSG_DATA (cmsgptr); memset (&pkt->ipi6_addr, 0, sizeof (struct in6_addr)); pkt->ipi6_ifindex =3D ifp->ifindex; ret =3D sendmsg (ripng->sock, &msg, 0); [...] return ret; } About IPv4, see the sendto() use case of ripd/ripd.c. Moreover these solutions, which are used by Quagga, are portable on most of= =20 the OSes (Linux, FreeBSD, OpenBSD, NetBSD, Solaris, ...). Regards, Vincent On Thursday 19 February 2004 10:54, Andreas T wrote: > Hi all, > > I am developing an implementation of the Optimized LinkState Routing > protocol(RFC3626) for Linux(www.olsr.org). OLSR is a routing protocol > for mobile, multihop, wireless ad-hoc networks. > > I would really like to have my code compile on FreeBSD - but there is > one major issue. > > OLSR sends control traffic broadcasted(IPv4) or multicasted(IPv6) on a > pr.interface basis. That means that a node running OLSR on two > interfaces a and b, would not always broadcast the same content in > messages sent on a and b. This works fine using IPv4 and broadcast as > long as the interfaces uses different broadcast addresses - but as soon > as two interfaces is set up with the same broadcastaddress(or as soon as > one uses IPv6 with the same multicastgroup) any message (sent on the > broadcastsocket) will be transmitted on both interfaces. > I hope this made some sense :) > > Bottom line is that one has to be able to control on which interface > packets are sent. To do this in Linux i use the SO_BINDTODEVICE > flag(with setsockopt(2)). That way I can have one socket for each > interface OLSR is to use regradless of destinationaddress. > > In FreeBSD there is no SO_BINDTODEVICE or equalivant as far as I can > see. To me it seems like BPF could provide the functioning I need. Is > this so? Can BPF in some way help me control on which interface packets > are transmitted? > If so I would really appreciate some (pseudo or real)code examples to > get me started or some links to such material - as I am totally new to > the BPF interface(and the concept). > > If BPF is not the way to go I would appreciate any help in finding a way > of doing this in FreeBSD. > > I could ofcause try to figure this out by myself - but as FreeBSD > porting is not too high prioritized for now I figured I'd rather be lazy > and ask you experts :) > > > Thanks! > > regards, > Andreas T