2 * user-mode-linux networking multicast transport
3 * Copyright (C) 2001 by Harald Welte <laforge@gnumonks.org>
5 * based on the existing uml-networking code, which is
6 * Copyright (C) 2001 Lennert Buytenhek (buytenh@gnu.org) and
7 * James Leu (jleu@mindspring.net).
8 * Copyright (C) 2001 by various other people who didn't put their name here.
10 * Licensed under the GPL.
16 #include <linux/inet.h>
17 #include <sys/socket.h>
20 #include <netinet/in.h>
23 #include "kern_util.h"
24 #include "user_util.h"
28 #define MAX_PACKET (ETH_MAX_PACKET + ETH_HEADER_OTHER)
30 static struct sockaddr_in
*new_addr(char *addr
, unsigned short port
)
32 struct sockaddr_in
*sin
;
34 sin
= um_kmalloc(sizeof(struct sockaddr_in
));
36 printk("new_addr: allocation of sockaddr_in failed\n");
39 sin
->sin_family
= AF_INET
;
40 sin
->sin_addr
.s_addr
= in_aton(addr
);
41 sin
->sin_port
= htons(port
);
45 static void mcast_user_init(void *data
, void *dev
)
47 struct mcast_data
*pri
= data
;
49 pri
->mcast_addr
= new_addr(pri
->addr
, pri
->port
);
53 static int mcast_open(void *data
)
55 struct mcast_data
*pri
= data
;
56 struct sockaddr_in
*sin
= pri
->mcast_addr
;
58 int fd
= -EINVAL
, yes
= 1, err
= -EINVAL
;;
61 if ((sin
->sin_addr
.s_addr
== 0) || (sin
->sin_port
== 0))
64 fd
= socket(AF_INET
, SOCK_DGRAM
, 0);
67 printk("mcast_open : data socket failed, errno = %d\n",
73 if (setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, &yes
, sizeof(yes
)) < 0) {
74 printk("mcast_open: SO_REUSEADDR failed, errno = %d\n",
79 /* set ttl according to config */
80 if (setsockopt(fd
, SOL_IP
, IP_MULTICAST_TTL
, &pri
->ttl
,
81 sizeof(pri
->ttl
)) < 0) {
82 printk("mcast_open: IP_MULTICAST_TTL failed, error = %d\n",
87 /* set LOOP, so data does get fed back to local sockets */
88 if (setsockopt(fd
, SOL_IP
, IP_MULTICAST_LOOP
, &yes
, sizeof(yes
)) < 0) {
89 printk("mcast_open: IP_MULTICAST_LOOP failed, error = %d\n",
94 /* bind socket to mcast address */
95 if (bind(fd
, (struct sockaddr
*) sin
, sizeof(*sin
)) < 0) {
96 printk("mcast_open : data bind failed, errno = %d\n", errno
);
100 /* subscribe to the multicast group */
101 mreq
.imr_multiaddr
.s_addr
= sin
->sin_addr
.s_addr
;
102 mreq
.imr_interface
.s_addr
= 0;
103 if (setsockopt(fd
, SOL_IP
, IP_ADD_MEMBERSHIP
,
104 &mreq
, sizeof(mreq
)) < 0) {
105 printk("mcast_open: IP_ADD_MEMBERSHIP failed, error = %d\n",
107 printk("There appears not to be a multicast-capable network "
108 "interface on the host.\n");
109 printk("eth0 should be configured in order to use the "
110 "multicast transport.\n");
122 static void mcast_close(int fd
, void *data
)
125 struct mcast_data
*pri
= data
;
126 struct sockaddr_in
*sin
= pri
->mcast_addr
;
128 mreq
.imr_multiaddr
.s_addr
= sin
->sin_addr
.s_addr
;
129 mreq
.imr_interface
.s_addr
= 0;
130 if (setsockopt(fd
, SOL_IP
, IP_DROP_MEMBERSHIP
,
131 &mreq
, sizeof(mreq
)) < 0) {
132 printk("mcast_open: IP_DROP_MEMBERSHIP failed, error = %d\n",
139 int mcast_user_write(int fd
, void *buf
, int len
, struct mcast_data
*pri
)
141 struct sockaddr_in
*data_addr
= pri
->mcast_addr
;
143 return(net_sendto(fd
, buf
, len
, data_addr
, sizeof(*data_addr
)));
146 static int mcast_set_mtu(int mtu
, void *data
)
151 struct net_user_info mcast_user_info
= {
152 .init
= mcast_user_init
,
154 .close
= mcast_close
,
156 .set_mtu
= mcast_set_mtu
,
158 .delete_address
= NULL
,
159 .max_packet
= MAX_PACKET
- ETH_HEADER_OTHER