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