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