]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * INET An implementation of the TCP/IP protocol suite for the LINUX | |
3 | * operating system. INET is implemented using the BSD Socket | |
4 | * interface as the means of communication with the user level. | |
5 | * | |
6 | * The IP to API glue. | |
e905a9ed | 7 | * |
1da177e4 LT |
8 | * Authors: see ip.c |
9 | * | |
10 | * Fixes: | |
11 | * Many : Split from ip.c , see ip.c for history. | |
12 | * Martin Mares : TOS setting fixed. | |
e905a9ed | 13 | * Alan Cox : Fixed a couple of oopses in Martin's |
1da177e4 LT |
14 | * TOS tweaks. |
15 | * Mike McLagan : Routing by source | |
16 | */ | |
17 | ||
1da177e4 LT |
18 | #include <linux/module.h> |
19 | #include <linux/types.h> | |
20 | #include <linux/mm.h> | |
1da177e4 LT |
21 | #include <linux/skbuff.h> |
22 | #include <linux/ip.h> | |
23 | #include <linux/icmp.h> | |
14c85021 | 24 | #include <linux/inetdevice.h> |
1da177e4 LT |
25 | #include <linux/netdevice.h> |
26 | #include <net/sock.h> | |
27 | #include <net/ip.h> | |
28 | #include <net/icmp.h> | |
d83d8461 | 29 | #include <net/tcp_states.h> |
1da177e4 LT |
30 | #include <linux/udp.h> |
31 | #include <linux/igmp.h> | |
32 | #include <linux/netfilter.h> | |
33 | #include <linux/route.h> | |
34 | #include <linux/mroute.h> | |
35 | #include <net/route.h> | |
36 | #include <net/xfrm.h> | |
dae50295 | 37 | #include <net/compat.h> |
1da177e4 LT |
38 | #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) |
39 | #include <net/transp_v6.h> | |
40 | #endif | |
41 | ||
42 | #include <linux/errqueue.h> | |
43 | #include <asm/uaccess.h> | |
44 | ||
45 | #define IP_CMSG_PKTINFO 1 | |
46 | #define IP_CMSG_TTL 2 | |
47 | #define IP_CMSG_TOS 4 | |
48 | #define IP_CMSG_RECVOPTS 8 | |
49 | #define IP_CMSG_RETOPTS 16 | |
2c7946a7 | 50 | #define IP_CMSG_PASSSEC 32 |
e8b2dfe9 | 51 | #define IP_CMSG_ORIGDSTADDR 64 |
1da177e4 LT |
52 | |
53 | /* | |
54 | * SOL_IP control messages. | |
55 | */ | |
56 | ||
57 | static void ip_cmsg_recv_pktinfo(struct msghdr *msg, struct sk_buff *skb) | |
58 | { | |
59 | struct in_pktinfo info; | |
ee6b9673 | 60 | struct rtable *rt = skb->rtable; |
1da177e4 | 61 | |
eddc9ec5 | 62 | info.ipi_addr.s_addr = ip_hdr(skb)->daddr; |
1da177e4 LT |
63 | if (rt) { |
64 | info.ipi_ifindex = rt->rt_iif; | |
65 | info.ipi_spec_dst.s_addr = rt->rt_spec_dst; | |
66 | } else { | |
67 | info.ipi_ifindex = 0; | |
68 | info.ipi_spec_dst.s_addr = 0; | |
69 | } | |
70 | ||
71 | put_cmsg(msg, SOL_IP, IP_PKTINFO, sizeof(info), &info); | |
72 | } | |
73 | ||
74 | static void ip_cmsg_recv_ttl(struct msghdr *msg, struct sk_buff *skb) | |
75 | { | |
eddc9ec5 | 76 | int ttl = ip_hdr(skb)->ttl; |
1da177e4 LT |
77 | put_cmsg(msg, SOL_IP, IP_TTL, sizeof(int), &ttl); |
78 | } | |
79 | ||
80 | static void ip_cmsg_recv_tos(struct msghdr *msg, struct sk_buff *skb) | |
81 | { | |
eddc9ec5 | 82 | put_cmsg(msg, SOL_IP, IP_TOS, 1, &ip_hdr(skb)->tos); |
1da177e4 LT |
83 | } |
84 | ||
85 | static void ip_cmsg_recv_opts(struct msghdr *msg, struct sk_buff *skb) | |
86 | { | |
87 | if (IPCB(skb)->opt.optlen == 0) | |
88 | return; | |
89 | ||
eddc9ec5 ACM |
90 | put_cmsg(msg, SOL_IP, IP_RECVOPTS, IPCB(skb)->opt.optlen, |
91 | ip_hdr(skb) + 1); | |
1da177e4 LT |
92 | } |
93 | ||
94 | ||
95 | static void ip_cmsg_recv_retopts(struct msghdr *msg, struct sk_buff *skb) | |
96 | { | |
97 | unsigned char optbuf[sizeof(struct ip_options) + 40]; | |
09cb105e | 98 | struct ip_options * opt = (struct ip_options *)optbuf; |
1da177e4 LT |
99 | |
100 | if (IPCB(skb)->opt.optlen == 0) | |
101 | return; | |
102 | ||
103 | if (ip_options_echo(opt, skb)) { | |
104 | msg->msg_flags |= MSG_CTRUNC; | |
105 | return; | |
106 | } | |
107 | ip_options_undo(opt); | |
108 | ||
109 | put_cmsg(msg, SOL_IP, IP_RETOPTS, opt->optlen, opt->__data); | |
110 | } | |
111 | ||
2c7946a7 CZ |
112 | static void ip_cmsg_recv_security(struct msghdr *msg, struct sk_buff *skb) |
113 | { | |
114 | char *secdata; | |
dc49c1f9 | 115 | u32 seclen, secid; |
2c7946a7 CZ |
116 | int err; |
117 | ||
dc49c1f9 CZ |
118 | err = security_socket_getpeersec_dgram(NULL, skb, &secid); |
119 | if (err) | |
120 | return; | |
121 | ||
122 | err = security_secid_to_secctx(secid, &secdata, &seclen); | |
2c7946a7 CZ |
123 | if (err) |
124 | return; | |
125 | ||
126 | put_cmsg(msg, SOL_IP, SCM_SECURITY, seclen, secdata); | |
dc49c1f9 | 127 | security_release_secctx(secdata, seclen); |
2c7946a7 CZ |
128 | } |
129 | ||
21d1a161 | 130 | static void ip_cmsg_recv_dstaddr(struct msghdr *msg, struct sk_buff *skb) |
e8b2dfe9 BS |
131 | { |
132 | struct sockaddr_in sin; | |
133 | struct iphdr *iph = ip_hdr(skb); | |
21d1a161 | 134 | __be16 *ports = (__be16 *)skb_transport_header(skb); |
e8b2dfe9 BS |
135 | |
136 | if (skb_transport_offset(skb) + 4 > skb->len) | |
137 | return; | |
138 | ||
139 | /* All current transport protocols have the port numbers in the | |
140 | * first four bytes of the transport header and this function is | |
141 | * written with this assumption in mind. | |
142 | */ | |
143 | ||
144 | sin.sin_family = AF_INET; | |
145 | sin.sin_addr.s_addr = iph->daddr; | |
146 | sin.sin_port = ports[1]; | |
147 | memset(sin.sin_zero, 0, sizeof(sin.sin_zero)); | |
148 | ||
149 | put_cmsg(msg, SOL_IP, IP_ORIGDSTADDR, sizeof(sin), &sin); | |
150 | } | |
1da177e4 LT |
151 | |
152 | void ip_cmsg_recv(struct msghdr *msg, struct sk_buff *skb) | |
153 | { | |
154 | struct inet_sock *inet = inet_sk(skb->sk); | |
155 | unsigned flags = inet->cmsg_flags; | |
156 | ||
157 | /* Ordered by supposed usage frequency */ | |
158 | if (flags & 1) | |
159 | ip_cmsg_recv_pktinfo(msg, skb); | |
4d52cfbe | 160 | if ((flags >>= 1) == 0) |
1da177e4 LT |
161 | return; |
162 | ||
163 | if (flags & 1) | |
164 | ip_cmsg_recv_ttl(msg, skb); | |
4d52cfbe | 165 | if ((flags >>= 1) == 0) |
1da177e4 LT |
166 | return; |
167 | ||
168 | if (flags & 1) | |
169 | ip_cmsg_recv_tos(msg, skb); | |
4d52cfbe | 170 | if ((flags >>= 1) == 0) |
1da177e4 LT |
171 | return; |
172 | ||
173 | if (flags & 1) | |
174 | ip_cmsg_recv_opts(msg, skb); | |
4d52cfbe | 175 | if ((flags >>= 1) == 0) |
1da177e4 LT |
176 | return; |
177 | ||
178 | if (flags & 1) | |
179 | ip_cmsg_recv_retopts(msg, skb); | |
4d52cfbe | 180 | if ((flags >>= 1) == 0) |
2c7946a7 CZ |
181 | return; |
182 | ||
183 | if (flags & 1) | |
184 | ip_cmsg_recv_security(msg, skb); | |
e8b2dfe9 | 185 | |
4d52cfbe | 186 | if ((flags >>= 1) == 0) |
e8b2dfe9 BS |
187 | return; |
188 | if (flags & 1) | |
189 | ip_cmsg_recv_dstaddr(msg, skb); | |
190 | ||
1da177e4 | 191 | } |
4d52cfbe | 192 | EXPORT_SYMBOL(ip_cmsg_recv); |
1da177e4 | 193 | |
7a6adb92 | 194 | int ip_cmsg_send(struct net *net, struct msghdr *msg, struct ipcm_cookie *ipc) |
1da177e4 LT |
195 | { |
196 | int err; | |
197 | struct cmsghdr *cmsg; | |
198 | ||
199 | for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) { | |
200 | if (!CMSG_OK(msg, cmsg)) | |
201 | return -EINVAL; | |
202 | if (cmsg->cmsg_level != SOL_IP) | |
203 | continue; | |
204 | switch (cmsg->cmsg_type) { | |
205 | case IP_RETOPTS: | |
206 | err = cmsg->cmsg_len - CMSG_ALIGN(sizeof(struct cmsghdr)); | |
4d52cfbe ED |
207 | err = ip_options_get(net, &ipc->opt, CMSG_DATA(cmsg), |
208 | err < 40 ? err : 40); | |
1da177e4 LT |
209 | if (err) |
210 | return err; | |
211 | break; | |
212 | case IP_PKTINFO: | |
213 | { | |
214 | struct in_pktinfo *info; | |
215 | if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct in_pktinfo))) | |
216 | return -EINVAL; | |
217 | info = (struct in_pktinfo *)CMSG_DATA(cmsg); | |
218 | ipc->oif = info->ipi_ifindex; | |
219 | ipc->addr = info->ipi_spec_dst.s_addr; | |
220 | break; | |
221 | } | |
222 | default: | |
223 | return -EINVAL; | |
224 | } | |
225 | } | |
226 | return 0; | |
227 | } | |
228 | ||
229 | ||
230 | /* Special input handler for packets caught by router alert option. | |
231 | They are selected only by protocol field, and then processed likely | |
232 | local ones; but only if someone wants them! Otherwise, router | |
233 | not running rsvpd will kill RSVP. | |
234 | ||
235 | It is user level problem, what it will make with them. | |
236 | I have no idea, how it will masquearde or NAT them (it is joke, joke :-)), | |
237 | but receiver should be enough clever f.e. to forward mtrace requests, | |
238 | sent to multicast group to reach destination designated router. | |
239 | */ | |
240 | struct ip_ra_chain *ip_ra_chain; | |
241 | DEFINE_RWLOCK(ip_ra_lock); | |
242 | ||
4d52cfbe ED |
243 | int ip_ra_control(struct sock *sk, unsigned char on, |
244 | void (*destructor)(struct sock *)) | |
1da177e4 LT |
245 | { |
246 | struct ip_ra_chain *ra, *new_ra, **rap; | |
247 | ||
248 | if (sk->sk_type != SOCK_RAW || inet_sk(sk)->num == IPPROTO_RAW) | |
249 | return -EINVAL; | |
250 | ||
251 | new_ra = on ? kmalloc(sizeof(*new_ra), GFP_KERNEL) : NULL; | |
252 | ||
253 | write_lock_bh(&ip_ra_lock); | |
4d52cfbe | 254 | for (rap = &ip_ra_chain; (ra = *rap) != NULL; rap = &ra->next) { |
1da177e4 LT |
255 | if (ra->sk == sk) { |
256 | if (on) { | |
257 | write_unlock_bh(&ip_ra_lock); | |
a51482bd | 258 | kfree(new_ra); |
1da177e4 LT |
259 | return -EADDRINUSE; |
260 | } | |
261 | *rap = ra->next; | |
262 | write_unlock_bh(&ip_ra_lock); | |
263 | ||
264 | if (ra->destructor) | |
265 | ra->destructor(sk); | |
266 | sock_put(sk); | |
267 | kfree(ra); | |
268 | return 0; | |
269 | } | |
270 | } | |
271 | if (new_ra == NULL) { | |
272 | write_unlock_bh(&ip_ra_lock); | |
273 | return -ENOBUFS; | |
274 | } | |
275 | new_ra->sk = sk; | |
276 | new_ra->destructor = destructor; | |
277 | ||
278 | new_ra->next = ra; | |
279 | *rap = new_ra; | |
280 | sock_hold(sk); | |
281 | write_unlock_bh(&ip_ra_lock); | |
282 | ||
283 | return 0; | |
284 | } | |
285 | ||
e905a9ed | 286 | void ip_icmp_error(struct sock *sk, struct sk_buff *skb, int err, |
35986b32 | 287 | __be16 port, u32 info, u8 *payload) |
1da177e4 LT |
288 | { |
289 | struct inet_sock *inet = inet_sk(sk); | |
290 | struct sock_exterr_skb *serr; | |
291 | ||
292 | if (!inet->recverr) | |
293 | return; | |
294 | ||
295 | skb = skb_clone(skb, GFP_ATOMIC); | |
296 | if (!skb) | |
297 | return; | |
298 | ||
e905a9ed | 299 | serr = SKB_EXT_ERR(skb); |
1da177e4 LT |
300 | serr->ee.ee_errno = err; |
301 | serr->ee.ee_origin = SO_EE_ORIGIN_ICMP; | |
88c7664f ACM |
302 | serr->ee.ee_type = icmp_hdr(skb)->type; |
303 | serr->ee.ee_code = icmp_hdr(skb)->code; | |
1da177e4 LT |
304 | serr->ee.ee_pad = 0; |
305 | serr->ee.ee_info = info; | |
306 | serr->ee.ee_data = 0; | |
88c7664f | 307 | serr->addr_offset = (u8 *)&(((struct iphdr *)(icmp_hdr(skb) + 1))->daddr) - |
d56f90a7 | 308 | skb_network_header(skb); |
1da177e4 LT |
309 | serr->port = port; |
310 | ||
bd82393c ACM |
311 | if (skb_pull(skb, payload - skb->data) != NULL) { |
312 | skb_reset_transport_header(skb); | |
313 | if (sock_queue_err_skb(sk, skb) == 0) | |
314 | return; | |
315 | } | |
316 | kfree_skb(skb); | |
1da177e4 LT |
317 | } |
318 | ||
0579016e | 319 | void ip_local_error(struct sock *sk, int err, __be32 daddr, __be16 port, u32 info) |
1da177e4 LT |
320 | { |
321 | struct inet_sock *inet = inet_sk(sk); | |
322 | struct sock_exterr_skb *serr; | |
323 | struct iphdr *iph; | |
324 | struct sk_buff *skb; | |
325 | ||
326 | if (!inet->recverr) | |
327 | return; | |
328 | ||
329 | skb = alloc_skb(sizeof(struct iphdr), GFP_ATOMIC); | |
330 | if (!skb) | |
331 | return; | |
332 | ||
2ca9e6f2 ACM |
333 | skb_put(skb, sizeof(struct iphdr)); |
334 | skb_reset_network_header(skb); | |
eddc9ec5 | 335 | iph = ip_hdr(skb); |
1da177e4 LT |
336 | iph->daddr = daddr; |
337 | ||
e905a9ed | 338 | serr = SKB_EXT_ERR(skb); |
1da177e4 LT |
339 | serr->ee.ee_errno = err; |
340 | serr->ee.ee_origin = SO_EE_ORIGIN_LOCAL; | |
e905a9ed | 341 | serr->ee.ee_type = 0; |
1da177e4 LT |
342 | serr->ee.ee_code = 0; |
343 | serr->ee.ee_pad = 0; | |
344 | serr->ee.ee_info = info; | |
345 | serr->ee.ee_data = 0; | |
d56f90a7 | 346 | serr->addr_offset = (u8 *)&iph->daddr - skb_network_header(skb); |
1da177e4 LT |
347 | serr->port = port; |
348 | ||
27a884dc | 349 | __skb_pull(skb, skb_tail_pointer(skb) - skb->data); |
bd82393c | 350 | skb_reset_transport_header(skb); |
1da177e4 LT |
351 | |
352 | if (sock_queue_err_skb(sk, skb)) | |
353 | kfree_skb(skb); | |
354 | } | |
355 | ||
e905a9ed | 356 | /* |
1da177e4 LT |
357 | * Handle MSG_ERRQUEUE |
358 | */ | |
359 | int ip_recv_error(struct sock *sk, struct msghdr *msg, int len) | |
360 | { | |
361 | struct sock_exterr_skb *serr; | |
362 | struct sk_buff *skb, *skb2; | |
363 | struct sockaddr_in *sin; | |
364 | struct { | |
365 | struct sock_extended_err ee; | |
366 | struct sockaddr_in offender; | |
367 | } errhdr; | |
368 | int err; | |
369 | int copied; | |
370 | ||
371 | err = -EAGAIN; | |
372 | skb = skb_dequeue(&sk->sk_error_queue); | |
373 | if (skb == NULL) | |
374 | goto out; | |
375 | ||
376 | copied = skb->len; | |
377 | if (copied > len) { | |
378 | msg->msg_flags |= MSG_TRUNC; | |
379 | copied = len; | |
380 | } | |
381 | err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied); | |
382 | if (err) | |
383 | goto out_free_skb; | |
384 | ||
385 | sock_recv_timestamp(msg, sk, skb); | |
386 | ||
387 | serr = SKB_EXT_ERR(skb); | |
388 | ||
389 | sin = (struct sockaddr_in *)msg->msg_name; | |
390 | if (sin) { | |
391 | sin->sin_family = AF_INET; | |
d56f90a7 ACM |
392 | sin->sin_addr.s_addr = *(__be32 *)(skb_network_header(skb) + |
393 | serr->addr_offset); | |
1da177e4 LT |
394 | sin->sin_port = serr->port; |
395 | memset(&sin->sin_zero, 0, sizeof(sin->sin_zero)); | |
396 | } | |
397 | ||
398 | memcpy(&errhdr.ee, &serr->ee, sizeof(struct sock_extended_err)); | |
399 | sin = &errhdr.offender; | |
400 | sin->sin_family = AF_UNSPEC; | |
401 | if (serr->ee.ee_origin == SO_EE_ORIGIN_ICMP) { | |
402 | struct inet_sock *inet = inet_sk(sk); | |
403 | ||
404 | sin->sin_family = AF_INET; | |
eddc9ec5 | 405 | sin->sin_addr.s_addr = ip_hdr(skb)->saddr; |
1da177e4 LT |
406 | sin->sin_port = 0; |
407 | memset(&sin->sin_zero, 0, sizeof(sin->sin_zero)); | |
408 | if (inet->cmsg_flags) | |
409 | ip_cmsg_recv(msg, skb); | |
410 | } | |
411 | ||
412 | put_cmsg(msg, SOL_IP, IP_RECVERR, sizeof(errhdr), &errhdr); | |
413 | ||
414 | /* Now we could try to dump offended packet options */ | |
415 | ||
416 | msg->msg_flags |= MSG_ERRQUEUE; | |
417 | err = copied; | |
418 | ||
419 | /* Reset and regenerate socket error */ | |
e0f9f858 | 420 | spin_lock_bh(&sk->sk_error_queue.lock); |
1da177e4 | 421 | sk->sk_err = 0; |
4d52cfbe ED |
422 | skb2 = skb_peek(&sk->sk_error_queue); |
423 | if (skb2 != NULL) { | |
1da177e4 | 424 | sk->sk_err = SKB_EXT_ERR(skb2)->ee.ee_errno; |
e0f9f858 | 425 | spin_unlock_bh(&sk->sk_error_queue.lock); |
1da177e4 LT |
426 | sk->sk_error_report(sk); |
427 | } else | |
e0f9f858 | 428 | spin_unlock_bh(&sk->sk_error_queue.lock); |
1da177e4 | 429 | |
e905a9ed | 430 | out_free_skb: |
1da177e4 LT |
431 | kfree_skb(skb); |
432 | out: | |
433 | return err; | |
434 | } | |
435 | ||
436 | ||
437 | /* | |
4d52cfbe ED |
438 | * Socket option code for IP. This is the end of the line after any |
439 | * TCP,UDP etc options on an IP socket. | |
1da177e4 LT |
440 | */ |
441 | ||
3fdadf7d | 442 | static int do_ip_setsockopt(struct sock *sk, int level, |
132adf54 | 443 | int optname, char __user *optval, int optlen) |
1da177e4 LT |
444 | { |
445 | struct inet_sock *inet = inet_sk(sk); | |
09cb105e | 446 | int val = 0, err; |
1da177e4 | 447 | |
e905a9ed | 448 | if (((1<<optname) & ((1<<IP_PKTINFO) | (1<<IP_RECVTTL) | |
132adf54 SH |
449 | (1<<IP_RECVOPTS) | (1<<IP_RECVTOS) | |
450 | (1<<IP_RETOPTS) | (1<<IP_TOS) | | |
451 | (1<<IP_TTL) | (1<<IP_HDRINCL) | | |
452 | (1<<IP_MTU_DISCOVER) | (1<<IP_RECVERR) | | |
453 | (1<<IP_ROUTER_ALERT) | (1<<IP_FREEBIND) | | |
f5715aea | 454 | (1<<IP_PASSSEC) | (1<<IP_TRANSPARENT))) || |
132adf54 | 455 | optname == IP_MULTICAST_TTL || |
e8b2dfe9 BS |
456 | optname == IP_MULTICAST_LOOP || |
457 | optname == IP_RECVORIGDSTADDR) { | |
1da177e4 LT |
458 | if (optlen >= sizeof(int)) { |
459 | if (get_user(val, (int __user *) optval)) | |
460 | return -EFAULT; | |
461 | } else if (optlen >= sizeof(char)) { | |
462 | unsigned char ucval; | |
463 | ||
464 | if (get_user(ucval, (unsigned char __user *) optval)) | |
465 | return -EFAULT; | |
466 | val = (int) ucval; | |
467 | } | |
468 | } | |
469 | ||
470 | /* If optlen==0, it is equivalent to val == 0 */ | |
471 | ||
6a9fb947 | 472 | if (ip_mroute_opt(optname)) |
09cb105e | 473 | return ip_mroute_setsockopt(sk, optname, optval, optlen); |
1da177e4 LT |
474 | |
475 | err = 0; | |
476 | lock_sock(sk); | |
477 | ||
478 | switch (optname) { | |
132adf54 SH |
479 | case IP_OPTIONS: |
480 | { | |
4d52cfbe | 481 | struct ip_options *opt = NULL; |
132adf54 SH |
482 | if (optlen > 40 || optlen < 0) |
483 | goto e_inval; | |
3b1e0a65 | 484 | err = ip_options_get_from_user(sock_net(sk), &opt, |
cb84663e | 485 | optval, optlen); |
132adf54 SH |
486 | if (err) |
487 | break; | |
488 | if (inet->is_icsk) { | |
489 | struct inet_connection_sock *icsk = inet_csk(sk); | |
1da177e4 | 490 | #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) |
132adf54 SH |
491 | if (sk->sk_family == PF_INET || |
492 | (!((1 << sk->sk_state) & | |
493 | (TCPF_LISTEN | TCPF_CLOSE)) && | |
494 | inet->daddr != LOOPBACK4_IPV6)) { | |
1da177e4 | 495 | #endif |
132adf54 SH |
496 | if (inet->opt) |
497 | icsk->icsk_ext_hdr_len -= inet->opt->optlen; | |
498 | if (opt) | |
499 | icsk->icsk_ext_hdr_len += opt->optlen; | |
500 | icsk->icsk_sync_mss(sk, icsk->icsk_pmtu_cookie); | |
1da177e4 | 501 | #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) |
1da177e4 | 502 | } |
132adf54 | 503 | #endif |
1da177e4 | 504 | } |
132adf54 SH |
505 | opt = xchg(&inet->opt, opt); |
506 | kfree(opt); | |
507 | break; | |
508 | } | |
509 | case IP_PKTINFO: | |
510 | if (val) | |
511 | inet->cmsg_flags |= IP_CMSG_PKTINFO; | |
512 | else | |
513 | inet->cmsg_flags &= ~IP_CMSG_PKTINFO; | |
514 | break; | |
515 | case IP_RECVTTL: | |
516 | if (val) | |
517 | inet->cmsg_flags |= IP_CMSG_TTL; | |
518 | else | |
519 | inet->cmsg_flags &= ~IP_CMSG_TTL; | |
520 | break; | |
521 | case IP_RECVTOS: | |
522 | if (val) | |
523 | inet->cmsg_flags |= IP_CMSG_TOS; | |
524 | else | |
525 | inet->cmsg_flags &= ~IP_CMSG_TOS; | |
526 | break; | |
527 | case IP_RECVOPTS: | |
528 | if (val) | |
529 | inet->cmsg_flags |= IP_CMSG_RECVOPTS; | |
530 | else | |
531 | inet->cmsg_flags &= ~IP_CMSG_RECVOPTS; | |
532 | break; | |
533 | case IP_RETOPTS: | |
534 | if (val) | |
535 | inet->cmsg_flags |= IP_CMSG_RETOPTS; | |
536 | else | |
537 | inet->cmsg_flags &= ~IP_CMSG_RETOPTS; | |
538 | break; | |
539 | case IP_PASSSEC: | |
540 | if (val) | |
541 | inet->cmsg_flags |= IP_CMSG_PASSSEC; | |
542 | else | |
543 | inet->cmsg_flags &= ~IP_CMSG_PASSSEC; | |
544 | break; | |
e8b2dfe9 BS |
545 | case IP_RECVORIGDSTADDR: |
546 | if (val) | |
547 | inet->cmsg_flags |= IP_CMSG_ORIGDSTADDR; | |
548 | else | |
549 | inet->cmsg_flags &= ~IP_CMSG_ORIGDSTADDR; | |
550 | break; | |
132adf54 SH |
551 | case IP_TOS: /* This sets both TOS and Precedence */ |
552 | if (sk->sk_type == SOCK_STREAM) { | |
553 | val &= ~3; | |
554 | val |= inet->tos & 3; | |
555 | } | |
132adf54 SH |
556 | if (inet->tos != val) { |
557 | inet->tos = val; | |
558 | sk->sk_priority = rt_tos2priority(val); | |
559 | sk_dst_reset(sk); | |
560 | } | |
561 | break; | |
562 | case IP_TTL: | |
4d52cfbe | 563 | if (optlen < 1) |
132adf54 | 564 | goto e_inval; |
4d52cfbe | 565 | if (val != -1 && (val < 0 || val > 255)) |
132adf54 SH |
566 | goto e_inval; |
567 | inet->uc_ttl = val; | |
568 | break; | |
569 | case IP_HDRINCL: | |
570 | if (sk->sk_type != SOCK_RAW) { | |
571 | err = -ENOPROTOOPT; | |
2c7946a7 | 572 | break; |
132adf54 SH |
573 | } |
574 | inet->hdrincl = val ? 1 : 0; | |
575 | break; | |
576 | case IP_MTU_DISCOVER: | |
4d52cfbe | 577 | if (val < 0 || val > 3) |
132adf54 SH |
578 | goto e_inval; |
579 | inet->pmtudisc = val; | |
580 | break; | |
581 | case IP_RECVERR: | |
582 | inet->recverr = !!val; | |
583 | if (!val) | |
584 | skb_queue_purge(&sk->sk_error_queue); | |
585 | break; | |
586 | case IP_MULTICAST_TTL: | |
587 | if (sk->sk_type == SOCK_STREAM) | |
588 | goto e_inval; | |
4d52cfbe | 589 | if (optlen < 1) |
132adf54 | 590 | goto e_inval; |
09cb105e | 591 | if (val == -1) |
132adf54 SH |
592 | val = 1; |
593 | if (val < 0 || val > 255) | |
594 | goto e_inval; | |
595 | inet->mc_ttl = val; | |
596 | break; | |
597 | case IP_MULTICAST_LOOP: | |
4d52cfbe | 598 | if (optlen < 1) |
132adf54 SH |
599 | goto e_inval; |
600 | inet->mc_loop = !!val; | |
601 | break; | |
602 | case IP_MULTICAST_IF: | |
603 | { | |
604 | struct ip_mreqn mreq; | |
605 | struct net_device *dev = NULL; | |
606 | ||
607 | if (sk->sk_type == SOCK_STREAM) | |
608 | goto e_inval; | |
609 | /* | |
610 | * Check the arguments are allowable | |
611 | */ | |
612 | ||
613 | err = -EFAULT; | |
614 | if (optlen >= sizeof(struct ip_mreqn)) { | |
09cb105e | 615 | if (copy_from_user(&mreq, optval, sizeof(mreq))) |
1da177e4 | 616 | break; |
132adf54 SH |
617 | } else { |
618 | memset(&mreq, 0, sizeof(mreq)); | |
619 | if (optlen >= sizeof(struct in_addr) && | |
4d52cfbe ED |
620 | copy_from_user(&mreq.imr_address, optval, |
621 | sizeof(struct in_addr))) | |
132adf54 SH |
622 | break; |
623 | } | |
624 | ||
625 | if (!mreq.imr_ifindex) { | |
e6f1cebf | 626 | if (mreq.imr_address.s_addr == htonl(INADDR_ANY)) { |
132adf54 SH |
627 | inet->mc_index = 0; |
628 | inet->mc_addr = 0; | |
629 | err = 0; | |
1da177e4 LT |
630 | break; |
631 | } | |
3b1e0a65 | 632 | dev = ip_dev_find(sock_net(sk), mreq.imr_address.s_addr); |
132adf54 SH |
633 | if (dev) { |
634 | mreq.imr_ifindex = dev->ifindex; | |
635 | dev_put(dev); | |
636 | } | |
637 | } else | |
3b1e0a65 | 638 | dev = __dev_get_by_index(sock_net(sk), mreq.imr_ifindex); |
1da177e4 | 639 | |
1da177e4 | 640 | |
132adf54 SH |
641 | err = -EADDRNOTAVAIL; |
642 | if (!dev) | |
643 | break; | |
644 | ||
645 | err = -EINVAL; | |
646 | if (sk->sk_bound_dev_if && | |
647 | mreq.imr_ifindex != sk->sk_bound_dev_if) | |
648 | break; | |
1da177e4 | 649 | |
132adf54 SH |
650 | inet->mc_index = mreq.imr_ifindex; |
651 | inet->mc_addr = mreq.imr_address.s_addr; | |
652 | err = 0; | |
653 | break; | |
654 | } | |
1da177e4 | 655 | |
132adf54 SH |
656 | case IP_ADD_MEMBERSHIP: |
657 | case IP_DROP_MEMBERSHIP: | |
658 | { | |
659 | struct ip_mreqn mreq; | |
1da177e4 | 660 | |
a96fb49b FL |
661 | err = -EPROTO; |
662 | if (inet_sk(sk)->is_icsk) | |
663 | break; | |
664 | ||
132adf54 SH |
665 | if (optlen < sizeof(struct ip_mreq)) |
666 | goto e_inval; | |
667 | err = -EFAULT; | |
668 | if (optlen >= sizeof(struct ip_mreqn)) { | |
09cb105e | 669 | if (copy_from_user(&mreq, optval, sizeof(mreq))) |
1da177e4 | 670 | break; |
132adf54 SH |
671 | } else { |
672 | memset(&mreq, 0, sizeof(mreq)); | |
09cb105e | 673 | if (copy_from_user(&mreq, optval, sizeof(struct ip_mreq))) |
1da177e4 | 674 | break; |
132adf54 | 675 | } |
1da177e4 | 676 | |
132adf54 SH |
677 | if (optname == IP_ADD_MEMBERSHIP) |
678 | err = ip_mc_join_group(sk, &mreq); | |
679 | else | |
680 | err = ip_mc_leave_group(sk, &mreq); | |
681 | break; | |
682 | } | |
683 | case IP_MSFILTER: | |
684 | { | |
132adf54 SH |
685 | struct ip_msfilter *msf; |
686 | ||
687 | if (optlen < IP_MSFILTER_SIZE(0)) | |
688 | goto e_inval; | |
689 | if (optlen > sysctl_optmem_max) { | |
690 | err = -ENOBUFS; | |
1da177e4 LT |
691 | break; |
692 | } | |
132adf54 | 693 | msf = kmalloc(optlen, GFP_KERNEL); |
cfcabdcc | 694 | if (!msf) { |
132adf54 SH |
695 | err = -ENOBUFS; |
696 | break; | |
697 | } | |
698 | err = -EFAULT; | |
699 | if (copy_from_user(msf, optval, optlen)) { | |
700 | kfree(msf); | |
701 | break; | |
702 | } | |
703 | /* numsrc >= (1G-4) overflow in 32 bits */ | |
704 | if (msf->imsf_numsrc >= 0x3ffffffcU || | |
705 | msf->imsf_numsrc > sysctl_igmp_max_msf) { | |
706 | kfree(msf); | |
707 | err = -ENOBUFS; | |
708 | break; | |
709 | } | |
710 | if (IP_MSFILTER_SIZE(msf->imsf_numsrc) > optlen) { | |
711 | kfree(msf); | |
712 | err = -EINVAL; | |
713 | break; | |
714 | } | |
715 | err = ip_mc_msfilter(sk, msf, 0); | |
716 | kfree(msf); | |
717 | break; | |
718 | } | |
719 | case IP_BLOCK_SOURCE: | |
720 | case IP_UNBLOCK_SOURCE: | |
721 | case IP_ADD_SOURCE_MEMBERSHIP: | |
722 | case IP_DROP_SOURCE_MEMBERSHIP: | |
723 | { | |
724 | struct ip_mreq_source mreqs; | |
725 | int omode, add; | |
1da177e4 | 726 | |
132adf54 SH |
727 | if (optlen != sizeof(struct ip_mreq_source)) |
728 | goto e_inval; | |
729 | if (copy_from_user(&mreqs, optval, sizeof(mreqs))) { | |
1da177e4 | 730 | err = -EFAULT; |
1da177e4 LT |
731 | break; |
732 | } | |
132adf54 SH |
733 | if (optname == IP_BLOCK_SOURCE) { |
734 | omode = MCAST_EXCLUDE; | |
735 | add = 1; | |
736 | } else if (optname == IP_UNBLOCK_SOURCE) { | |
737 | omode = MCAST_EXCLUDE; | |
738 | add = 0; | |
739 | } else if (optname == IP_ADD_SOURCE_MEMBERSHIP) { | |
740 | struct ip_mreqn mreq; | |
1da177e4 | 741 | |
132adf54 SH |
742 | mreq.imr_multiaddr.s_addr = mreqs.imr_multiaddr; |
743 | mreq.imr_address.s_addr = mreqs.imr_interface; | |
744 | mreq.imr_ifindex = 0; | |
745 | err = ip_mc_join_group(sk, &mreq); | |
746 | if (err && err != -EADDRINUSE) | |
1da177e4 | 747 | break; |
132adf54 SH |
748 | omode = MCAST_INCLUDE; |
749 | add = 1; | |
750 | } else /* IP_DROP_SOURCE_MEMBERSHIP */ { | |
751 | omode = MCAST_INCLUDE; | |
752 | add = 0; | |
753 | } | |
754 | err = ip_mc_source(add, omode, sk, &mreqs, 0); | |
755 | break; | |
756 | } | |
757 | case MCAST_JOIN_GROUP: | |
758 | case MCAST_LEAVE_GROUP: | |
759 | { | |
760 | struct group_req greq; | |
761 | struct sockaddr_in *psin; | |
762 | struct ip_mreqn mreq; | |
763 | ||
764 | if (optlen < sizeof(struct group_req)) | |
765 | goto e_inval; | |
766 | err = -EFAULT; | |
767 | if (copy_from_user(&greq, optval, sizeof(greq))) | |
768 | break; | |
769 | psin = (struct sockaddr_in *)&greq.gr_group; | |
770 | if (psin->sin_family != AF_INET) | |
771 | goto e_inval; | |
772 | memset(&mreq, 0, sizeof(mreq)); | |
773 | mreq.imr_multiaddr = psin->sin_addr; | |
774 | mreq.imr_ifindex = greq.gr_interface; | |
775 | ||
776 | if (optname == MCAST_JOIN_GROUP) | |
777 | err = ip_mc_join_group(sk, &mreq); | |
778 | else | |
779 | err = ip_mc_leave_group(sk, &mreq); | |
780 | break; | |
781 | } | |
782 | case MCAST_JOIN_SOURCE_GROUP: | |
783 | case MCAST_LEAVE_SOURCE_GROUP: | |
784 | case MCAST_BLOCK_SOURCE: | |
785 | case MCAST_UNBLOCK_SOURCE: | |
786 | { | |
787 | struct group_source_req greqs; | |
788 | struct ip_mreq_source mreqs; | |
789 | struct sockaddr_in *psin; | |
790 | int omode, add; | |
791 | ||
792 | if (optlen != sizeof(struct group_source_req)) | |
793 | goto e_inval; | |
794 | if (copy_from_user(&greqs, optval, sizeof(greqs))) { | |
1da177e4 | 795 | err = -EFAULT; |
1da177e4 LT |
796 | break; |
797 | } | |
132adf54 SH |
798 | if (greqs.gsr_group.ss_family != AF_INET || |
799 | greqs.gsr_source.ss_family != AF_INET) { | |
800 | err = -EADDRNOTAVAIL; | |
1da177e4 LT |
801 | break; |
802 | } | |
132adf54 SH |
803 | psin = (struct sockaddr_in *)&greqs.gsr_group; |
804 | mreqs.imr_multiaddr = psin->sin_addr.s_addr; | |
805 | psin = (struct sockaddr_in *)&greqs.gsr_source; | |
806 | mreqs.imr_sourceaddr = psin->sin_addr.s_addr; | |
807 | mreqs.imr_interface = 0; /* use index for mc_source */ | |
808 | ||
809 | if (optname == MCAST_BLOCK_SOURCE) { | |
810 | omode = MCAST_EXCLUDE; | |
811 | add = 1; | |
812 | } else if (optname == MCAST_UNBLOCK_SOURCE) { | |
813 | omode = MCAST_EXCLUDE; | |
814 | add = 0; | |
815 | } else if (optname == MCAST_JOIN_SOURCE_GROUP) { | |
1da177e4 LT |
816 | struct ip_mreqn mreq; |
817 | ||
132adf54 | 818 | psin = (struct sockaddr_in *)&greqs.gsr_group; |
1da177e4 | 819 | mreq.imr_multiaddr = psin->sin_addr; |
132adf54 SH |
820 | mreq.imr_address.s_addr = 0; |
821 | mreq.imr_ifindex = greqs.gsr_interface; | |
822 | err = ip_mc_join_group(sk, &mreq); | |
823 | if (err && err != -EADDRINUSE) | |
824 | break; | |
825 | greqs.gsr_interface = mreq.imr_ifindex; | |
826 | omode = MCAST_INCLUDE; | |
827 | add = 1; | |
828 | } else /* MCAST_LEAVE_SOURCE_GROUP */ { | |
829 | omode = MCAST_INCLUDE; | |
830 | add = 0; | |
831 | } | |
832 | err = ip_mc_source(add, omode, sk, &mreqs, | |
833 | greqs.gsr_interface); | |
834 | break; | |
835 | } | |
836 | case MCAST_MSFILTER: | |
837 | { | |
132adf54 SH |
838 | struct sockaddr_in *psin; |
839 | struct ip_msfilter *msf = NULL; | |
840 | struct group_filter *gsf = NULL; | |
841 | int msize, i, ifindex; | |
842 | ||
843 | if (optlen < GROUP_FILTER_SIZE(0)) | |
844 | goto e_inval; | |
845 | if (optlen > sysctl_optmem_max) { | |
846 | err = -ENOBUFS; | |
1da177e4 LT |
847 | break; |
848 | } | |
09cb105e | 849 | gsf = kmalloc(optlen, GFP_KERNEL); |
cfcabdcc | 850 | if (!gsf) { |
132adf54 | 851 | err = -ENOBUFS; |
1da177e4 LT |
852 | break; |
853 | } | |
132adf54 | 854 | err = -EFAULT; |
4d52cfbe | 855 | if (copy_from_user(gsf, optval, optlen)) |
132adf54 | 856 | goto mc_msf_out; |
4d52cfbe | 857 | |
132adf54 SH |
858 | /* numsrc >= (4G-140)/128 overflow in 32 bits */ |
859 | if (gsf->gf_numsrc >= 0x1ffffff || | |
860 | gsf->gf_numsrc > sysctl_igmp_max_msf) { | |
861 | err = -ENOBUFS; | |
862 | goto mc_msf_out; | |
863 | } | |
864 | if (GROUP_FILTER_SIZE(gsf->gf_numsrc) > optlen) { | |
865 | err = -EINVAL; | |
866 | goto mc_msf_out; | |
867 | } | |
868 | msize = IP_MSFILTER_SIZE(gsf->gf_numsrc); | |
09cb105e | 869 | msf = kmalloc(msize, GFP_KERNEL); |
cfcabdcc | 870 | if (!msf) { |
132adf54 SH |
871 | err = -ENOBUFS; |
872 | goto mc_msf_out; | |
873 | } | |
874 | ifindex = gsf->gf_interface; | |
875 | psin = (struct sockaddr_in *)&gsf->gf_group; | |
876 | if (psin->sin_family != AF_INET) { | |
1da177e4 | 877 | err = -EADDRNOTAVAIL; |
132adf54 | 878 | goto mc_msf_out; |
1da177e4 | 879 | } |
132adf54 SH |
880 | msf->imsf_multiaddr = psin->sin_addr.s_addr; |
881 | msf->imsf_interface = 0; | |
882 | msf->imsf_fmode = gsf->gf_fmode; | |
883 | msf->imsf_numsrc = gsf->gf_numsrc; | |
884 | err = -EADDRNOTAVAIL; | |
4d52cfbe | 885 | for (i = 0; i < gsf->gf_numsrc; ++i) { |
132adf54 | 886 | psin = (struct sockaddr_in *)&gsf->gf_slist[i]; |
e905a9ed | 887 | |
132adf54 SH |
888 | if (psin->sin_family != AF_INET) |
889 | goto mc_msf_out; | |
890 | msf->imsf_slist[i] = psin->sin_addr.s_addr; | |
891 | } | |
892 | kfree(gsf); | |
893 | gsf = NULL; | |
894 | ||
895 | err = ip_mc_msfilter(sk, msf, ifindex); | |
4d52cfbe | 896 | mc_msf_out: |
132adf54 SH |
897 | kfree(msf); |
898 | kfree(gsf); | |
899 | break; | |
900 | } | |
901 | case IP_ROUTER_ALERT: | |
902 | err = ip_ra_control(sk, val ? 1 : 0, NULL); | |
903 | break; | |
904 | ||
905 | case IP_FREEBIND: | |
4d52cfbe | 906 | if (optlen < 1) |
132adf54 SH |
907 | goto e_inval; |
908 | inet->freebind = !!val; | |
909 | break; | |
910 | ||
911 | case IP_IPSEC_POLICY: | |
912 | case IP_XFRM_POLICY: | |
913 | err = -EPERM; | |
914 | if (!capable(CAP_NET_ADMIN)) | |
1da177e4 | 915 | break; |
132adf54 SH |
916 | err = xfrm_user_policy(sk, optname, optval, optlen); |
917 | break; | |
1da177e4 | 918 | |
f5715aea KK |
919 | case IP_TRANSPARENT: |
920 | if (!capable(CAP_NET_ADMIN)) { | |
921 | err = -EPERM; | |
922 | break; | |
923 | } | |
924 | if (optlen < 1) | |
925 | goto e_inval; | |
926 | inet->transparent = !!val; | |
927 | break; | |
928 | ||
132adf54 SH |
929 | default: |
930 | err = -ENOPROTOOPT; | |
931 | break; | |
1da177e4 LT |
932 | } |
933 | release_sock(sk); | |
934 | return err; | |
935 | ||
936 | e_inval: | |
937 | release_sock(sk); | |
938 | return -EINVAL; | |
939 | } | |
940 | ||
3fdadf7d DM |
941 | int ip_setsockopt(struct sock *sk, int level, |
942 | int optname, char __user *optval, int optlen) | |
943 | { | |
944 | int err; | |
945 | ||
946 | if (level != SOL_IP) | |
947 | return -ENOPROTOOPT; | |
948 | ||
949 | err = do_ip_setsockopt(sk, level, optname, optval, optlen); | |
950 | #ifdef CONFIG_NETFILTER | |
951 | /* we need to exclude all possible ENOPROTOOPTs except default case */ | |
952 | if (err == -ENOPROTOOPT && optname != IP_HDRINCL && | |
6a9fb947 PE |
953 | optname != IP_IPSEC_POLICY && |
954 | optname != IP_XFRM_POLICY && | |
955 | !ip_mroute_opt(optname)) { | |
3fdadf7d DM |
956 | lock_sock(sk); |
957 | err = nf_setsockopt(sk, PF_INET, optname, optval, optlen); | |
958 | release_sock(sk); | |
959 | } | |
960 | #endif | |
961 | return err; | |
962 | } | |
4d52cfbe | 963 | EXPORT_SYMBOL(ip_setsockopt); |
3fdadf7d DM |
964 | |
965 | #ifdef CONFIG_COMPAT | |
543d9cfe ACM |
966 | int compat_ip_setsockopt(struct sock *sk, int level, int optname, |
967 | char __user *optval, int optlen) | |
3fdadf7d DM |
968 | { |
969 | int err; | |
970 | ||
971 | if (level != SOL_IP) | |
972 | return -ENOPROTOOPT; | |
973 | ||
dae50295 DS |
974 | if (optname >= MCAST_JOIN_GROUP && optname <= MCAST_MSFILTER) |
975 | return compat_mc_setsockopt(sk, level, optname, optval, optlen, | |
976 | ip_setsockopt); | |
977 | ||
3fdadf7d DM |
978 | err = do_ip_setsockopt(sk, level, optname, optval, optlen); |
979 | #ifdef CONFIG_NETFILTER | |
980 | /* we need to exclude all possible ENOPROTOOPTs except default case */ | |
981 | if (err == -ENOPROTOOPT && optname != IP_HDRINCL && | |
6a9fb947 PE |
982 | optname != IP_IPSEC_POLICY && |
983 | optname != IP_XFRM_POLICY && | |
984 | !ip_mroute_opt(optname)) { | |
3fdadf7d | 985 | lock_sock(sk); |
543d9cfe ACM |
986 | err = compat_nf_setsockopt(sk, PF_INET, optname, |
987 | optval, optlen); | |
3fdadf7d DM |
988 | release_sock(sk); |
989 | } | |
990 | #endif | |
991 | return err; | |
992 | } | |
543d9cfe | 993 | EXPORT_SYMBOL(compat_ip_setsockopt); |
3fdadf7d DM |
994 | #endif |
995 | ||
1da177e4 | 996 | /* |
4d52cfbe ED |
997 | * Get the options. Note for future reference. The GET of IP options gets |
998 | * the _received_ ones. The set sets the _sent_ ones. | |
1da177e4 LT |
999 | */ |
1000 | ||
3fdadf7d | 1001 | static int do_ip_getsockopt(struct sock *sk, int level, int optname, |
132adf54 | 1002 | char __user *optval, int __user *optlen) |
1da177e4 LT |
1003 | { |
1004 | struct inet_sock *inet = inet_sk(sk); | |
1005 | int val; | |
1006 | int len; | |
e905a9ed | 1007 | |
132adf54 | 1008 | if (level != SOL_IP) |
1da177e4 LT |
1009 | return -EOPNOTSUPP; |
1010 | ||
6a9fb947 | 1011 | if (ip_mroute_opt(optname)) |
09cb105e | 1012 | return ip_mroute_getsockopt(sk, optname, optval, optlen); |
1da177e4 | 1013 | |
09cb105e | 1014 | if (get_user(len, optlen)) |
1da177e4 | 1015 | return -EFAULT; |
132adf54 | 1016 | if (len < 0) |
1da177e4 | 1017 | return -EINVAL; |
e905a9ed | 1018 | |
1da177e4 LT |
1019 | lock_sock(sk); |
1020 | ||
132adf54 SH |
1021 | switch (optname) { |
1022 | case IP_OPTIONS: | |
1023 | { | |
1024 | unsigned char optbuf[sizeof(struct ip_options)+40]; | |
09cb105e | 1025 | struct ip_options * opt = (struct ip_options *)optbuf; |
132adf54 SH |
1026 | opt->optlen = 0; |
1027 | if (inet->opt) | |
1028 | memcpy(optbuf, inet->opt, | |
1029 | sizeof(struct ip_options)+ | |
1030 | inet->opt->optlen); | |
1031 | release_sock(sk); | |
1032 | ||
1033 | if (opt->optlen == 0) | |
1034 | return put_user(0, optlen); | |
1035 | ||
1036 | ip_options_undo(opt); | |
1037 | ||
1038 | len = min_t(unsigned int, len, opt->optlen); | |
1039 | if (put_user(len, optlen)) | |
1040 | return -EFAULT; | |
1041 | if (copy_to_user(optval, opt->__data, len)) | |
1042 | return -EFAULT; | |
1043 | return 0; | |
1044 | } | |
1045 | case IP_PKTINFO: | |
1046 | val = (inet->cmsg_flags & IP_CMSG_PKTINFO) != 0; | |
1047 | break; | |
1048 | case IP_RECVTTL: | |
1049 | val = (inet->cmsg_flags & IP_CMSG_TTL) != 0; | |
1050 | break; | |
1051 | case IP_RECVTOS: | |
1052 | val = (inet->cmsg_flags & IP_CMSG_TOS) != 0; | |
1053 | break; | |
1054 | case IP_RECVOPTS: | |
1055 | val = (inet->cmsg_flags & IP_CMSG_RECVOPTS) != 0; | |
1056 | break; | |
1057 | case IP_RETOPTS: | |
1058 | val = (inet->cmsg_flags & IP_CMSG_RETOPTS) != 0; | |
1059 | break; | |
1060 | case IP_PASSSEC: | |
1061 | val = (inet->cmsg_flags & IP_CMSG_PASSSEC) != 0; | |
1062 | break; | |
e8b2dfe9 BS |
1063 | case IP_RECVORIGDSTADDR: |
1064 | val = (inet->cmsg_flags & IP_CMSG_ORIGDSTADDR) != 0; | |
1065 | break; | |
132adf54 SH |
1066 | case IP_TOS: |
1067 | val = inet->tos; | |
1068 | break; | |
1069 | case IP_TTL: | |
1070 | val = (inet->uc_ttl == -1 ? | |
1071 | sysctl_ip_default_ttl : | |
1072 | inet->uc_ttl); | |
1073 | break; | |
1074 | case IP_HDRINCL: | |
1075 | val = inet->hdrincl; | |
1076 | break; | |
1077 | case IP_MTU_DISCOVER: | |
1078 | val = inet->pmtudisc; | |
1079 | break; | |
1080 | case IP_MTU: | |
1081 | { | |
1082 | struct dst_entry *dst; | |
1083 | val = 0; | |
1084 | dst = sk_dst_get(sk); | |
1085 | if (dst) { | |
1086 | val = dst_mtu(dst); | |
1087 | dst_release(dst); | |
1da177e4 | 1088 | } |
132adf54 | 1089 | if (!val) { |
1da177e4 | 1090 | release_sock(sk); |
132adf54 | 1091 | return -ENOTCONN; |
1da177e4 | 1092 | } |
132adf54 SH |
1093 | break; |
1094 | } | |
1095 | case IP_RECVERR: | |
1096 | val = inet->recverr; | |
1097 | break; | |
1098 | case IP_MULTICAST_TTL: | |
1099 | val = inet->mc_ttl; | |
1100 | break; | |
1101 | case IP_MULTICAST_LOOP: | |
1102 | val = inet->mc_loop; | |
1103 | break; | |
1104 | case IP_MULTICAST_IF: | |
1105 | { | |
1106 | struct in_addr addr; | |
1107 | len = min_t(unsigned int, len, sizeof(struct in_addr)); | |
1108 | addr.s_addr = inet->mc_addr; | |
1109 | release_sock(sk); | |
1da177e4 | 1110 | |
132adf54 SH |
1111 | if (put_user(len, optlen)) |
1112 | return -EFAULT; | |
1113 | if (copy_to_user(optval, &addr, len)) | |
1114 | return -EFAULT; | |
1115 | return 0; | |
1116 | } | |
1117 | case IP_MSFILTER: | |
1118 | { | |
1119 | struct ip_msfilter msf; | |
1120 | int err; | |
1121 | ||
1122 | if (len < IP_MSFILTER_SIZE(0)) { | |
1da177e4 | 1123 | release_sock(sk); |
132adf54 | 1124 | return -EINVAL; |
1da177e4 | 1125 | } |
132adf54 | 1126 | if (copy_from_user(&msf, optval, IP_MSFILTER_SIZE(0))) { |
1da177e4 | 1127 | release_sock(sk); |
132adf54 | 1128 | return -EFAULT; |
1da177e4 | 1129 | } |
132adf54 SH |
1130 | err = ip_mc_msfget(sk, &msf, |
1131 | (struct ip_msfilter __user *)optval, optlen); | |
1132 | release_sock(sk); | |
1133 | return err; | |
1134 | } | |
1135 | case MCAST_MSFILTER: | |
1136 | { | |
1137 | struct group_filter gsf; | |
1138 | int err; | |
1da177e4 | 1139 | |
132adf54 | 1140 | if (len < GROUP_FILTER_SIZE(0)) { |
1da177e4 | 1141 | release_sock(sk); |
132adf54 SH |
1142 | return -EINVAL; |
1143 | } | |
1144 | if (copy_from_user(&gsf, optval, GROUP_FILTER_SIZE(0))) { | |
1145 | release_sock(sk); | |
1146 | return -EFAULT; | |
1147 | } | |
1148 | err = ip_mc_gsfget(sk, &gsf, | |
4d52cfbe ED |
1149 | (struct group_filter __user *)optval, |
1150 | optlen); | |
132adf54 SH |
1151 | release_sock(sk); |
1152 | return err; | |
1153 | } | |
1154 | case IP_PKTOPTIONS: | |
1155 | { | |
1156 | struct msghdr msg; | |
1da177e4 | 1157 | |
132adf54 | 1158 | release_sock(sk); |
1da177e4 | 1159 | |
132adf54 SH |
1160 | if (sk->sk_type != SOCK_STREAM) |
1161 | return -ENOPROTOOPT; | |
1da177e4 | 1162 | |
132adf54 SH |
1163 | msg.msg_control = optval; |
1164 | msg.msg_controllen = len; | |
1165 | msg.msg_flags = 0; | |
1da177e4 | 1166 | |
132adf54 SH |
1167 | if (inet->cmsg_flags & IP_CMSG_PKTINFO) { |
1168 | struct in_pktinfo info; | |
1169 | ||
1170 | info.ipi_addr.s_addr = inet->rcv_saddr; | |
1171 | info.ipi_spec_dst.s_addr = inet->rcv_saddr; | |
1172 | info.ipi_ifindex = inet->mc_index; | |
1173 | put_cmsg(&msg, SOL_IP, IP_PKTINFO, sizeof(info), &info); | |
1da177e4 | 1174 | } |
132adf54 SH |
1175 | if (inet->cmsg_flags & IP_CMSG_TTL) { |
1176 | int hlim = inet->mc_ttl; | |
1177 | put_cmsg(&msg, SOL_IP, IP_TTL, sizeof(hlim), &hlim); | |
1178 | } | |
1179 | len -= msg.msg_controllen; | |
1180 | return put_user(len, optlen); | |
1181 | } | |
1182 | case IP_FREEBIND: | |
1183 | val = inet->freebind; | |
1184 | break; | |
f5715aea KK |
1185 | case IP_TRANSPARENT: |
1186 | val = inet->transparent; | |
1187 | break; | |
132adf54 SH |
1188 | default: |
1189 | release_sock(sk); | |
1190 | return -ENOPROTOOPT; | |
1da177e4 LT |
1191 | } |
1192 | release_sock(sk); | |
e905a9ed | 1193 | |
4d52cfbe | 1194 | if (len < sizeof(int) && len > 0 && val >= 0 && val <= 255) { |
1da177e4 LT |
1195 | unsigned char ucval = (unsigned char)val; |
1196 | len = 1; | |
132adf54 | 1197 | if (put_user(len, optlen)) |
1da177e4 | 1198 | return -EFAULT; |
09cb105e | 1199 | if (copy_to_user(optval, &ucval, 1)) |
1da177e4 LT |
1200 | return -EFAULT; |
1201 | } else { | |
1202 | len = min_t(unsigned int, sizeof(int), len); | |
132adf54 | 1203 | if (put_user(len, optlen)) |
1da177e4 | 1204 | return -EFAULT; |
09cb105e | 1205 | if (copy_to_user(optval, &val, len)) |
1da177e4 LT |
1206 | return -EFAULT; |
1207 | } | |
1208 | return 0; | |
1209 | } | |
1210 | ||
3fdadf7d | 1211 | int ip_getsockopt(struct sock *sk, int level, |
132adf54 | 1212 | int optname, char __user *optval, int __user *optlen) |
3fdadf7d DM |
1213 | { |
1214 | int err; | |
1215 | ||
1216 | err = do_ip_getsockopt(sk, level, optname, optval, optlen); | |
1217 | #ifdef CONFIG_NETFILTER | |
1218 | /* we need to exclude all possible ENOPROTOOPTs except default case */ | |
6a9fb947 PE |
1219 | if (err == -ENOPROTOOPT && optname != IP_PKTOPTIONS && |
1220 | !ip_mroute_opt(optname)) { | |
e905a9ed | 1221 | int len; |
3fdadf7d | 1222 | |
09cb105e | 1223 | if (get_user(len, optlen)) |
3fdadf7d DM |
1224 | return -EFAULT; |
1225 | ||
1226 | lock_sock(sk); | |
1227 | err = nf_getsockopt(sk, PF_INET, optname, optval, | |
1228 | &len); | |
1229 | release_sock(sk); | |
1230 | if (err >= 0) | |
1231 | err = put_user(len, optlen); | |
1232 | return err; | |
1233 | } | |
1234 | #endif | |
1235 | return err; | |
1236 | } | |
4d52cfbe | 1237 | EXPORT_SYMBOL(ip_getsockopt); |
3fdadf7d DM |
1238 | |
1239 | #ifdef CONFIG_COMPAT | |
543d9cfe ACM |
1240 | int compat_ip_getsockopt(struct sock *sk, int level, int optname, |
1241 | char __user *optval, int __user *optlen) | |
3fdadf7d | 1242 | { |
42908c69 DS |
1243 | int err; |
1244 | ||
1245 | if (optname == MCAST_MSFILTER) | |
1246 | return compat_mc_getsockopt(sk, level, optname, optval, optlen, | |
1247 | ip_getsockopt); | |
1248 | ||
1249 | err = do_ip_getsockopt(sk, level, optname, optval, optlen); | |
1250 | ||
3fdadf7d DM |
1251 | #ifdef CONFIG_NETFILTER |
1252 | /* we need to exclude all possible ENOPROTOOPTs except default case */ | |
6a9fb947 PE |
1253 | if (err == -ENOPROTOOPT && optname != IP_PKTOPTIONS && |
1254 | !ip_mroute_opt(optname)) { | |
e905a9ed | 1255 | int len; |
3fdadf7d | 1256 | |
543d9cfe | 1257 | if (get_user(len, optlen)) |
3fdadf7d DM |
1258 | return -EFAULT; |
1259 | ||
1260 | lock_sock(sk); | |
543d9cfe | 1261 | err = compat_nf_getsockopt(sk, PF_INET, optname, optval, &len); |
3fdadf7d DM |
1262 | release_sock(sk); |
1263 | if (err >= 0) | |
1264 | err = put_user(len, optlen); | |
1265 | return err; | |
1266 | } | |
1267 | #endif | |
1268 | return err; | |
1269 | } | |
543d9cfe | 1270 | EXPORT_SYMBOL(compat_ip_getsockopt); |
3fdadf7d | 1271 | #endif |