blob: 3fd69067a5f5caa136f3e68e95640b22ca123193 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * user-mode-linux networking multicast transport
3 * Copyright (C) 2001 by Harald Welte <laforge@gnumonks.org>
4 *
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.
9 *
10 * Licensed under the GPL.
11 *
12 */
13
14#include <errno.h>
15#include <unistd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <sys/socket.h>
17#include <sys/un.h>
18#include <sys/time.h>
19#include <netinet/in.h>
20#include "net_user.h"
21#include "mcast.h"
22#include "kern_util.h"
23#include "user_util.h"
24#include "user.h"
25#include "os.h"
26
27#define MAX_PACKET (ETH_MAX_PACKET + ETH_HEADER_OTHER)
28
29static struct sockaddr_in *new_addr(char *addr, unsigned short port)
30{
31 struct sockaddr_in *sin;
32
33 sin = um_kmalloc(sizeof(struct sockaddr_in));
34 if(sin == NULL){
35 printk("new_addr: allocation of sockaddr_in failed\n");
36 return(NULL);
37 }
38 sin->sin_family = AF_INET;
39 sin->sin_addr.s_addr = in_aton(addr);
Jeff Dike7c00c312005-05-20 13:59:09 -070040 sin->sin_port = htons(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 return(sin);
42}
43
44static void mcast_user_init(void *data, void *dev)
45{
46 struct mcast_data *pri = data;
47
48 pri->mcast_addr = new_addr(pri->addr, pri->port);
49 pri->dev = dev;
50}
51
52static int mcast_open(void *data)
53{
54 struct mcast_data *pri = data;
55 struct sockaddr_in *sin = pri->mcast_addr;
56 struct ip_mreq mreq;
Jeff Dike7c00c312005-05-20 13:59:09 -070057 int fd = -EINVAL, yes = 1, err = -EINVAL;;
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
59
Jeff Dike7c00c312005-05-20 13:59:09 -070060 if ((sin->sin_addr.s_addr == 0) || (sin->sin_port == 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
63 fd = socket(AF_INET, SOCK_DGRAM, 0);
Jeff Dike7c00c312005-05-20 13:59:09 -070064
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 if (fd < 0){
66 printk("mcast_open : data socket failed, errno = %d\n",
67 errno);
Jeff Dike7c00c312005-05-20 13:59:09 -070068 fd = -errno;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 goto out;
70 }
71
72 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) < 0) {
73 printk("mcast_open: SO_REUSEADDR failed, errno = %d\n",
74 errno);
Jeff Dike7c00c312005-05-20 13:59:09 -070075 goto out_close;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 }
77
78 /* set ttl according to config */
79 if (setsockopt(fd, SOL_IP, IP_MULTICAST_TTL, &pri->ttl,
80 sizeof(pri->ttl)) < 0) {
81 printk("mcast_open: IP_MULTICAST_TTL failed, error = %d\n",
82 errno);
Jeff Dike7c00c312005-05-20 13:59:09 -070083 goto out_close;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 }
85
86 /* set LOOP, so data does get fed back to local sockets */
87 if (setsockopt(fd, SOL_IP, IP_MULTICAST_LOOP, &yes, sizeof(yes)) < 0) {
88 printk("mcast_open: IP_MULTICAST_LOOP failed, error = %d\n",
89 errno);
Jeff Dike7c00c312005-05-20 13:59:09 -070090 goto out_close;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 }
92
93 /* bind socket to mcast address */
94 if (bind(fd, (struct sockaddr *) sin, sizeof(*sin)) < 0) {
95 printk("mcast_open : data bind failed, errno = %d\n", errno);
Jeff Dike7c00c312005-05-20 13:59:09 -070096 goto out_close;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 }
98
99 /* subscribe to the multicast group */
100 mreq.imr_multiaddr.s_addr = sin->sin_addr.s_addr;
101 mreq.imr_interface.s_addr = 0;
102 if (setsockopt(fd, SOL_IP, IP_ADD_MEMBERSHIP,
103 &mreq, sizeof(mreq)) < 0) {
104 printk("mcast_open: IP_ADD_MEMBERSHIP failed, error = %d\n",
105 errno);
106 printk("There appears not to be a multicast-capable network "
107 "interface on the host.\n");
108 printk("eth0 should be configured in order to use the "
109 "multicast transport.\n");
Jeff Dike7c00c312005-05-20 13:59:09 -0700110 goto out_close;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 }
112
113 out:
Jeff Dike7c00c312005-05-20 13:59:09 -0700114 return fd;
115
116 out_close:
117 os_close_file(fd);
118 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119}
120
121static void mcast_close(int fd, void *data)
122{
123 struct ip_mreq mreq;
124 struct mcast_data *pri = data;
125 struct sockaddr_in *sin = pri->mcast_addr;
126
127 mreq.imr_multiaddr.s_addr = sin->sin_addr.s_addr;
128 mreq.imr_interface.s_addr = 0;
129 if (setsockopt(fd, SOL_IP, IP_DROP_MEMBERSHIP,
130 &mreq, sizeof(mreq)) < 0) {
131 printk("mcast_open: IP_DROP_MEMBERSHIP failed, error = %d\n",
132 errno);
133 }
134
135 os_close_file(fd);
136}
137
138int mcast_user_write(int fd, void *buf, int len, struct mcast_data *pri)
139{
140 struct sockaddr_in *data_addr = pri->mcast_addr;
141
142 return(net_sendto(fd, buf, len, data_addr, sizeof(*data_addr)));
143}
144
145static int mcast_set_mtu(int mtu, void *data)
146{
147 return(mtu);
148}
149
150struct net_user_info mcast_user_info = {
151 .init = mcast_user_init,
152 .open = mcast_open,
153 .close = mcast_close,
154 .remove = NULL,
155 .set_mtu = mcast_set_mtu,
156 .add_address = NULL,
157 .delete_address = NULL,
158 .max_packet = MAX_PACKET - ETH_HEADER_OTHER
159};