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 <sys/socket.h>
19 #include <netinet/in.h>
22 #include "kern_util.h"
25 #include "um_malloc.h"
27 #define MAX_PACKET (ETH_MAX_PACKET + ETH_HEADER_OTHER)
29 static struct sockaddr_in *new_addr(char *addr, unsigned short port)
31 struct sockaddr_in *sin;
33 sin = um_kmalloc(sizeof(struct sockaddr_in));
35 printk("new_addr: allocation of sockaddr_in failed\n");
38 sin->sin_family = AF_INET;
39 sin->sin_addr.s_addr = in_aton(addr);
40 sin->sin_port = htons(port);
44 static int mcast_user_init(void *data, void *dev)
46 struct mcast_data *pri = data;
48 pri->mcast_addr = new_addr(pri->addr, pri->port);
53 static void mcast_remove(void *data)
55 struct mcast_data *pri = data;
57 kfree(pri->mcast_addr);
58 pri->mcast_addr = NULL;
61 static int mcast_open(void *data)
63 struct mcast_data *pri = data;
64 struct sockaddr_in *sin = pri->mcast_addr;
66 int fd, yes = 1, err = -EINVAL;
69 if ((sin->sin_addr.s_addr == 0) || (sin->sin_port == 0))
72 fd = socket(AF_INET, SOCK_DGRAM, 0);
76 printk("mcast_open : data socket failed, errno = %d\n",
81 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) < 0) {
83 printk("mcast_open: SO_REUSEADDR failed, errno = %d\n",
88 /* set ttl according to config */
89 if (setsockopt(fd, SOL_IP, IP_MULTICAST_TTL, &pri->ttl,
90 sizeof(pri->ttl)) < 0) {
92 printk("mcast_open: IP_MULTICAST_TTL failed, error = %d\n",
97 /* set LOOP, so data does get fed back to local sockets */
98 if (setsockopt(fd, SOL_IP, IP_MULTICAST_LOOP, &yes, sizeof(yes)) < 0) {
100 printk("mcast_open: IP_MULTICAST_LOOP failed, error = %d\n",
105 /* bind socket to mcast address */
106 if (bind(fd, (struct sockaddr *) sin, sizeof(*sin)) < 0) {
108 printk("mcast_open : data bind failed, errno = %d\n", errno);
112 /* subscribe to the multicast group */
113 mreq.imr_multiaddr.s_addr = sin->sin_addr.s_addr;
114 mreq.imr_interface.s_addr = 0;
115 if (setsockopt(fd, SOL_IP, IP_ADD_MEMBERSHIP,
116 &mreq, sizeof(mreq)) < 0) {
118 printk("mcast_open: IP_ADD_MEMBERSHIP failed, error = %d\n",
120 printk("There appears not to be a multicast-capable network "
121 "interface on the host.\n");
122 printk("eth0 should be configured in order to use the "
123 "multicast transport.\n");
135 static void mcast_close(int fd, void *data)
138 struct mcast_data *pri = data;
139 struct sockaddr_in *sin = pri->mcast_addr;
141 mreq.imr_multiaddr.s_addr = sin->sin_addr.s_addr;
142 mreq.imr_interface.s_addr = 0;
143 if (setsockopt(fd, SOL_IP, IP_DROP_MEMBERSHIP,
144 &mreq, sizeof(mreq)) < 0) {
145 printk("mcast_open: IP_DROP_MEMBERSHIP failed, error = %d\n",
152 int mcast_user_write(int fd, void *buf, int len, struct mcast_data *pri)
154 struct sockaddr_in *data_addr = pri->mcast_addr;
156 return net_sendto(fd, buf, len, data_addr, sizeof(*data_addr));
159 static int mcast_set_mtu(int mtu, void *data)
164 const struct net_user_info mcast_user_info = {
165 .init = mcast_user_init,
167 .close = mcast_close,
168 .remove = mcast_remove,
169 .set_mtu = mcast_set_mtu,
171 .delete_address = NULL,
172 .max_packet = MAX_PACKET - ETH_HEADER_OTHER