]> git.karo-electronics.de Git - karo-tx-linux.git/blob - arch/um/drivers/mcast_user.c
uml: network formatting
[karo-tx-linux.git] / arch / um / drivers / mcast_user.c
1 /*
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>
5  *
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.
10  *
11  * Licensed under the GPL.
12  *
13  */
14
15 #include <unistd.h>
16 #include <errno.h>
17 #include <netinet/in.h>
18 #include "mcast.h"
19 #include "net_user.h"
20 #include "um_malloc.h"
21 #include "user.h"
22
23 #define MAX_PACKET (ETH_MAX_PACKET + ETH_HEADER_OTHER)
24
25 static struct sockaddr_in *new_addr(char *addr, unsigned short port)
26 {
27         struct sockaddr_in *sin;
28
29         sin = kmalloc(sizeof(struct sockaddr_in), UM_GFP_KERNEL);
30         if (sin == NULL) {
31                 printk(UM_KERN_ERR "new_addr: allocation of sockaddr_in "
32                        "failed\n");
33                 return NULL;
34         }
35         sin->sin_family = AF_INET;
36         sin->sin_addr.s_addr = in_aton(addr);
37         sin->sin_port = htons(port);
38         return sin;
39 }
40
41 static int mcast_user_init(void *data, void *dev)
42 {
43         struct mcast_data *pri = data;
44
45         pri->mcast_addr = new_addr(pri->addr, pri->port);
46         pri->dev = dev;
47         return 0;
48 }
49
50 static void mcast_remove(void *data)
51 {
52         struct mcast_data *pri = data;
53
54         kfree(pri->mcast_addr);
55         pri->mcast_addr = NULL;
56 }
57
58 static int mcast_open(void *data)
59 {
60         struct mcast_data *pri = data;
61         struct sockaddr_in *sin = pri->mcast_addr;
62         struct ip_mreq mreq;
63         int fd, yes = 1, err = -EINVAL;
64
65
66         if ((sin->sin_addr.s_addr == 0) || (sin->sin_port == 0))
67                 goto out;
68
69         fd = socket(AF_INET, SOCK_DGRAM, 0);
70
71         if (fd < 0) {
72                 err = -errno;
73                 printk(UM_KERN_ERR "mcast_open : data socket failed, "
74                        "errno = %d\n", errno);
75                 goto out;
76         }
77
78         if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) < 0) {
79                 err = -errno;
80                 printk(UM_KERN_ERR "mcast_open: SO_REUSEADDR failed, "
81                        "errno = %d\n", errno);
82                 goto out_close;
83         }
84
85         /* set ttl according to config */
86         if (setsockopt(fd, SOL_IP, IP_MULTICAST_TTL, &pri->ttl,
87                        sizeof(pri->ttl)) < 0) {
88                 err = -errno;
89                 printk(UM_KERN_ERR "mcast_open: IP_MULTICAST_TTL failed, "
90                        "error = %d\n", errno);
91                 goto out_close;
92         }
93
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) {
96                 err = -errno;
97                 printk(UM_KERN_ERR "mcast_open: IP_MULTICAST_LOOP failed, "
98                        "error = %d\n", errno);
99                 goto out_close;
100         }
101
102         /* bind socket to mcast address */
103         if (bind(fd, (struct sockaddr *) sin, sizeof(*sin)) < 0) {
104                 err = -errno;
105                 printk(UM_KERN_ERR "mcast_open : data bind failed, "
106                        "errno = %d\n", errno);
107                 goto out_close;
108         }
109
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) {
115                 err = -errno;
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");
122                 goto out_close;
123         }
124
125         return fd;
126
127  out_close:
128         close(fd);
129  out:
130         return err;
131 }
132
133 static void mcast_close(int fd, void *data)
134 {
135         struct ip_mreq mreq;
136         struct mcast_data *pri = data;
137         struct sockaddr_in *sin = pri->mcast_addr;
138
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);
145         }
146
147         close(fd);
148 }
149
150 int mcast_user_write(int fd, void *buf, int len, struct mcast_data *pri)
151 {
152         struct sockaddr_in *data_addr = pri->mcast_addr;
153
154         return net_sendto(fd, buf, len, data_addr, sizeof(*data_addr));
155 }
156
157 static int mcast_set_mtu(int mtu, void *data)
158 {
159         return mtu;
160 }
161
162 const struct net_user_info mcast_user_info = {
163         .init           = mcast_user_init,
164         .open           = mcast_open,
165         .close          = mcast_close,
166         .remove         = mcast_remove,
167         .set_mtu        = mcast_set_mtu,
168         .add_address    = NULL,
169         .delete_address = NULL,
170         .max_packet     = MAX_PACKET - ETH_HEADER_OTHER
171 };