Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * user-mode-linux networking multicast transport | |
cd1ae0e4 | 3 | * Copyright (C) 2001 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com) |
1da177e4 LT |
4 | * Copyright (C) 2001 by Harald Welte <laforge@gnumonks.org> |
5 | * | |
6 | * based on the existing uml-networking code, which is | |
cd1ae0e4 | 7 | * Copyright (C) 2001 Lennert Buytenhek (buytenh@gnu.org) and |
1da177e4 LT |
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 | ||
1da177e4 | 15 | #include <unistd.h> |
cd1ae0e4 | 16 | #include <errno.h> |
1da177e4 | 17 | #include <netinet/in.h> |
43f5b308 | 18 | #include "kern_constants.h" |
1da177e4 | 19 | #include "mcast.h" |
cd1ae0e4 | 20 | #include "net_user.h" |
c13e5690 | 21 | #include "um_malloc.h" |
cd1ae0e4 | 22 | #include "user.h" |
1da177e4 | 23 | |
1da177e4 LT |
24 | static struct sockaddr_in *new_addr(char *addr, unsigned short port) |
25 | { | |
26 | struct sockaddr_in *sin; | |
27 | ||
43f5b308 | 28 | sin = uml_kmalloc(sizeof(struct sockaddr_in), UM_GFP_KERNEL); |
cd1ae0e4 JD |
29 | if (sin == NULL) { |
30 | printk(UM_KERN_ERR "new_addr: allocation of sockaddr_in " | |
31 | "failed\n"); | |
56bd194b | 32 | return NULL; |
1da177e4 LT |
33 | } |
34 | sin->sin_family = AF_INET; | |
35 | sin->sin_addr.s_addr = in_aton(addr); | |
7c00c31f | 36 | sin->sin_port = htons(port); |
56bd194b | 37 | return sin; |
1da177e4 LT |
38 | } |
39 | ||
f34d9d2d | 40 | static int mcast_user_init(void *data, void *dev) |
1da177e4 LT |
41 | { |
42 | struct mcast_data *pri = data; | |
43 | ||
44 | pri->mcast_addr = new_addr(pri->addr, pri->port); | |
45 | pri->dev = dev; | |
f34d9d2d | 46 | return 0; |
1da177e4 LT |
47 | } |
48 | ||
83f4e8af PBG |
49 | static void mcast_remove(void *data) |
50 | { | |
51 | struct mcast_data *pri = data; | |
52 | ||
53 | kfree(pri->mcast_addr); | |
54 | pri->mcast_addr = NULL; | |
55 | } | |
56 | ||
1da177e4 LT |
57 | static int mcast_open(void *data) |
58 | { | |
59 | struct mcast_data *pri = data; | |
60 | struct sockaddr_in *sin = pri->mcast_addr; | |
61 | struct ip_mreq mreq; | |
c50d2c4d | 62 | int fd, yes = 1, err = -EINVAL; |
1da177e4 LT |
63 | |
64 | ||
7c00c31f | 65 | if ((sin->sin_addr.s_addr == 0) || (sin->sin_port == 0)) |
1da177e4 | 66 | goto out; |
1da177e4 LT |
67 | |
68 | fd = socket(AF_INET, SOCK_DGRAM, 0); | |
7c00c31f | 69 | |
cd1ae0e4 | 70 | if (fd < 0) { |
c50d2c4d | 71 | err = -errno; |
cd1ae0e4 JD |
72 | printk(UM_KERN_ERR "mcast_open : data socket failed, " |
73 | "errno = %d\n", errno); | |
1da177e4 LT |
74 | goto out; |
75 | } | |
76 | ||
77 | if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) < 0) { | |
c50d2c4d | 78 | err = -errno; |
cd1ae0e4 JD |
79 | printk(UM_KERN_ERR "mcast_open: SO_REUSEADDR failed, " |
80 | "errno = %d\n", errno); | |
7c00c31f | 81 | goto out_close; |
1da177e4 LT |
82 | } |
83 | ||
84 | /* set ttl according to config */ | |
85 | if (setsockopt(fd, SOL_IP, IP_MULTICAST_TTL, &pri->ttl, | |
86 | sizeof(pri->ttl)) < 0) { | |
c50d2c4d | 87 | err = -errno; |
cd1ae0e4 JD |
88 | printk(UM_KERN_ERR "mcast_open: IP_MULTICAST_TTL failed, " |
89 | "error = %d\n", errno); | |
7c00c31f | 90 | goto out_close; |
1da177e4 LT |
91 | } |
92 | ||
93 | /* set LOOP, so data does get fed back to local sockets */ | |
94 | if (setsockopt(fd, SOL_IP, IP_MULTICAST_LOOP, &yes, sizeof(yes)) < 0) { | |
c50d2c4d | 95 | err = -errno; |
cd1ae0e4 JD |
96 | printk(UM_KERN_ERR "mcast_open: IP_MULTICAST_LOOP failed, " |
97 | "error = %d\n", errno); | |
7c00c31f | 98 | goto out_close; |
1da177e4 LT |
99 | } |
100 | ||
101 | /* bind socket to mcast address */ | |
102 | if (bind(fd, (struct sockaddr *) sin, sizeof(*sin)) < 0) { | |
b4fd310e | 103 | err = -errno; |
cd1ae0e4 JD |
104 | printk(UM_KERN_ERR "mcast_open : data bind failed, " |
105 | "errno = %d\n", errno); | |
7c00c31f | 106 | goto out_close; |
56bd194b JD |
107 | } |
108 | ||
1da177e4 LT |
109 | /* subscribe to the multicast group */ |
110 | mreq.imr_multiaddr.s_addr = sin->sin_addr.s_addr; | |
111 | mreq.imr_interface.s_addr = 0; | |
cd1ae0e4 | 112 | if (setsockopt(fd, SOL_IP, IP_ADD_MEMBERSHIP, |
1da177e4 | 113 | &mreq, sizeof(mreq)) < 0) { |
c50d2c4d | 114 | err = -errno; |
cd1ae0e4 JD |
115 | printk(UM_KERN_ERR "mcast_open: IP_ADD_MEMBERSHIP failed, " |
116 | "error = %d\n", errno); | |
117 | printk(UM_KERN_ERR "There appears not to be a multicast-" | |
118 | "capable network interface on the host.\n"); | |
119 | printk(UM_KERN_ERR "eth0 should be configured in order to use " | |
120 | "the multicast transport.\n"); | |
c50d2c4d | 121 | goto out_close; |
1da177e4 LT |
122 | } |
123 | ||
7c00c31f JD |
124 | return fd; |
125 | ||
126 | out_close: | |
cd1ae0e4 | 127 | close(fd); |
b4fd310e | 128 | out: |
c50d2c4d | 129 | return err; |
1da177e4 LT |
130 | } |
131 | ||
132 | static void mcast_close(int fd, void *data) | |
133 | { | |
134 | struct ip_mreq mreq; | |
135 | struct mcast_data *pri = data; | |
136 | struct sockaddr_in *sin = pri->mcast_addr; | |
137 | ||
138 | mreq.imr_multiaddr.s_addr = sin->sin_addr.s_addr; | |
139 | mreq.imr_interface.s_addr = 0; | |
140 | if (setsockopt(fd, SOL_IP, IP_DROP_MEMBERSHIP, | |
141 | &mreq, sizeof(mreq)) < 0) { | |
cd1ae0e4 JD |
142 | printk(UM_KERN_ERR "mcast_open: IP_DROP_MEMBERSHIP failed, " |
143 | "error = %d\n", errno); | |
1da177e4 LT |
144 | } |
145 | ||
cd1ae0e4 | 146 | close(fd); |
1da177e4 LT |
147 | } |
148 | ||
149 | int mcast_user_write(int fd, void *buf, int len, struct mcast_data *pri) | |
150 | { | |
151 | struct sockaddr_in *data_addr = pri->mcast_addr; | |
152 | ||
56bd194b | 153 | return net_sendto(fd, buf, len, data_addr, sizeof(*data_addr)); |
1da177e4 LT |
154 | } |
155 | ||
5e7672ec | 156 | const struct net_user_info mcast_user_info = { |
1da177e4 LT |
157 | .init = mcast_user_init, |
158 | .open = mcast_open, | |
159 | .close = mcast_close, | |
83f4e8af | 160 | .remove = mcast_remove, |
1da177e4 LT |
161 | .add_address = NULL, |
162 | .delete_address = NULL, | |
b53f35a8 JD |
163 | .mtu = ETH_MAX_PACKET, |
164 | .max_packet = ETH_MAX_PACKET + ETH_HEADER_OTHER, | |
1da177e4 | 165 | }; |