2 * user-mode-linux networking multicast transport
3 * Copyright (C) 2001 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
4 * Copyright (C) 2001 by Harald Welte <laforge@gnumonks.org>
6 * based on the existing uml-networking code, which is
7 * Copyright (C) 2001 Lennert Buytenhek (buytenh@gnu.org) and
8 * James Leu (jleu@mindspring.net).
9 * Copyright (C) 2001 by various other people who didn't put their name here.
11 * Licensed under the GPL.
17 #include <netinet/in.h>
20 #include "um_malloc.h"
23 #define MAX_PACKET (ETH_MAX_PACKET + ETH_HEADER_OTHER)
25 static struct sockaddr_in *new_addr(char *addr, unsigned short port)
27 struct sockaddr_in *sin;
29 sin = kmalloc(sizeof(struct sockaddr_in), UM_GFP_KERNEL);
31 printk(UM_KERN_ERR "new_addr: allocation of sockaddr_in "
35 sin->sin_family = AF_INET;
36 sin->sin_addr.s_addr = in_aton(addr);
37 sin->sin_port = htons(port);
41 static int mcast_user_init(void *data, void *dev)
43 struct mcast_data *pri = data;
45 pri->mcast_addr = new_addr(pri->addr, pri->port);
50 static void mcast_remove(void *data)
52 struct mcast_data *pri = data;
54 kfree(pri->mcast_addr);
55 pri->mcast_addr = NULL;
58 static int mcast_open(void *data)
60 struct mcast_data *pri = data;
61 struct sockaddr_in *sin = pri->mcast_addr;
63 int fd, yes = 1, err = -EINVAL;
66 if ((sin->sin_addr.s_addr == 0) || (sin->sin_port == 0))
69 fd = socket(AF_INET, SOCK_DGRAM, 0);
73 printk(UM_KERN_ERR "mcast_open : data socket failed, "
74 "errno = %d\n", errno);
78 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) < 0) {
80 printk(UM_KERN_ERR "mcast_open: SO_REUSEADDR failed, "
81 "errno = %d\n", errno);
85 /* set ttl according to config */
86 if (setsockopt(fd, SOL_IP, IP_MULTICAST_TTL, &pri->ttl,
87 sizeof(pri->ttl)) < 0) {
89 printk(UM_KERN_ERR "mcast_open: IP_MULTICAST_TTL failed, "
90 "error = %d\n", errno);
94 /* set LOOP, so data does get fed back to local sockets */
95 if (setsockopt(fd, SOL_IP, IP_MULTICAST_LOOP, &yes, sizeof(yes)) < 0) {
97 printk(UM_KERN_ERR "mcast_open: IP_MULTICAST_LOOP failed, "
98 "error = %d\n", errno);
102 /* bind socket to mcast address */
103 if (bind(fd, (struct sockaddr *) sin, sizeof(*sin)) < 0) {
105 printk(UM_KERN_ERR "mcast_open : data bind failed, "
106 "errno = %d\n", errno);
110 /* subscribe to the multicast group */
111 mreq.imr_multiaddr.s_addr = sin->sin_addr.s_addr;
112 mreq.imr_interface.s_addr = 0;
113 if (setsockopt(fd, SOL_IP, IP_ADD_MEMBERSHIP,
114 &mreq, sizeof(mreq)) < 0) {
116 printk(UM_KERN_ERR "mcast_open: IP_ADD_MEMBERSHIP failed, "
117 "error = %d\n", errno);
118 printk(UM_KERN_ERR "There appears not to be a multicast-"
119 "capable network interface on the host.\n");
120 printk(UM_KERN_ERR "eth0 should be configured in order to use "
121 "the multicast transport.\n");
133 static void mcast_close(int fd, void *data)
136 struct mcast_data *pri = data;
137 struct sockaddr_in *sin = pri->mcast_addr;
139 mreq.imr_multiaddr.s_addr = sin->sin_addr.s_addr;
140 mreq.imr_interface.s_addr = 0;
141 if (setsockopt(fd, SOL_IP, IP_DROP_MEMBERSHIP,
142 &mreq, sizeof(mreq)) < 0) {
143 printk(UM_KERN_ERR "mcast_open: IP_DROP_MEMBERSHIP failed, "
144 "error = %d\n", errno);
150 int mcast_user_write(int fd, void *buf, int len, struct mcast_data *pri)
152 struct sockaddr_in *data_addr = pri->mcast_addr;
154 return net_sendto(fd, buf, len, data_addr, sizeof(*data_addr));
157 static int mcast_set_mtu(int mtu, void *data)
162 const struct net_user_info mcast_user_info = {
163 .init = mcast_user_init,
165 .close = mcast_close,
166 .remove = mcast_remove,
167 .set_mtu = mcast_set_mtu,
169 .delete_address = NULL,
170 .max_packet = MAX_PACKET - ETH_HEADER_OTHER