raw.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078
  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. * RAW - implementation of IP "raw" sockets.
  7. *
  8. * Authors: Ross Biro
  9. * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
  10. *
  11. * Fixes:
  12. * Alan Cox : verify_area() fixed up
  13. * Alan Cox : ICMP error handling
  14. * Alan Cox : EMSGSIZE if you send too big a packet
  15. * Alan Cox : Now uses generic datagrams and shared
  16. * skbuff library. No more peek crashes,
  17. * no more backlogs
  18. * Alan Cox : Checks sk->broadcast.
  19. * Alan Cox : Uses skb_free_datagram/skb_copy_datagram
  20. * Alan Cox : Raw passes ip options too
  21. * Alan Cox : Setsocketopt added
  22. * Alan Cox : Fixed error return for broadcasts
  23. * Alan Cox : Removed wake_up calls
  24. * Alan Cox : Use ttl/tos
  25. * Alan Cox : Cleaned up old debugging
  26. * Alan Cox : Use new kernel side addresses
  27. * Arnt Gulbrandsen : Fixed MSG_DONTROUTE in raw sockets.
  28. * Alan Cox : BSD style RAW socket demultiplexing.
  29. * Alan Cox : Beginnings of mrouted support.
  30. * Alan Cox : Added IP_HDRINCL option.
  31. * Alan Cox : Skip broadcast check if BSDism set.
  32. * David S. Miller : New socket lookup architecture.
  33. *
  34. * This program is free software; you can redistribute it and/or
  35. * modify it under the terms of the GNU General Public License
  36. * as published by the Free Software Foundation; either version
  37. * 2 of the License, or (at your option) any later version.
  38. */
  39. #include <linux/types.h>
  40. #include <linux/atomic.h>
  41. #include <asm/byteorder.h>
  42. #include <asm/current.h>
  43. #include <asm/uaccess.h>
  44. #include <asm/ioctls.h>
  45. #include <linux/stddef.h>
  46. #include <linux/slab.h>
  47. #include <linux/errno.h>
  48. #include <linux/aio.h>
  49. #include <linux/kernel.h>
  50. #include <linux/export.h>
  51. #include <linux/spinlock.h>
  52. #include <linux/sockios.h>
  53. #include <linux/socket.h>
  54. #include <linux/in.h>
  55. #include <linux/mroute.h>
  56. #include <linux/netdevice.h>
  57. #include <linux/in_route.h>
  58. #include <linux/route.h>
  59. #include <linux/skbuff.h>
  60. #include <net/net_namespace.h>
  61. #include <net/dst.h>
  62. #include <net/sock.h>
  63. #include <linux/ip.h>
  64. #include <linux/net.h>
  65. #include <net/ip.h>
  66. #include <net/icmp.h>
  67. #include <net/udp.h>
  68. #include <net/raw.h>
  69. #include <net/snmp.h>
  70. #include <net/tcp_states.h>
  71. #include <net/inet_common.h>
  72. #include <net/checksum.h>
  73. #include <net/xfrm.h>
  74. #include <linux/rtnetlink.h>
  75. #include <linux/proc_fs.h>
  76. #include <linux/seq_file.h>
  77. #include <linux/netfilter.h>
  78. #include <linux/netfilter_ipv4.h>
  79. #include <linux/compat.h>
  80. static struct raw_hashinfo raw_v4_hashinfo = {
  81. .lock = __RW_LOCK_UNLOCKED(raw_v4_hashinfo.lock),
  82. };
  83. void raw_hash_sk(struct sock *sk)
  84. {
  85. struct raw_hashinfo *h = sk->sk_prot->h.raw_hash;
  86. struct hlist_head *head;
  87. head = &h->ht[inet_sk(sk)->inet_num & (RAW_HTABLE_SIZE - 1)];
  88. write_lock_bh(&h->lock);
  89. sk_add_node(sk, head);
  90. sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
  91. write_unlock_bh(&h->lock);
  92. }
  93. EXPORT_SYMBOL_GPL(raw_hash_sk);
  94. void raw_unhash_sk(struct sock *sk)
  95. {
  96. struct raw_hashinfo *h = sk->sk_prot->h.raw_hash;
  97. write_lock_bh(&h->lock);
  98. if (sk_del_node_init(sk))
  99. sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
  100. write_unlock_bh(&h->lock);
  101. }
  102. EXPORT_SYMBOL_GPL(raw_unhash_sk);
  103. static struct sock *__raw_v4_lookup(struct net *net, struct sock *sk,
  104. unsigned short num, __be32 raddr, __be32 laddr, int dif)
  105. {
  106. struct hlist_node *node;
  107. sk_for_each_from(sk, node) {
  108. struct inet_sock *inet = inet_sk(sk);
  109. if (net_eq(sock_net(sk), net) && inet->inet_num == num &&
  110. !(inet->inet_daddr && inet->inet_daddr != raddr) &&
  111. !(inet->inet_rcv_saddr && inet->inet_rcv_saddr != laddr) &&
  112. !(sk->sk_bound_dev_if && sk->sk_bound_dev_if != dif))
  113. goto found; /* gotcha */
  114. }
  115. sk = NULL;
  116. found:
  117. return sk;
  118. }
  119. /*
  120. * 0 - deliver
  121. * 1 - block
  122. */
  123. static int icmp_filter(const struct sock *sk, const struct sk_buff *skb)
  124. {
  125. struct icmphdr _hdr;
  126. const struct icmphdr *hdr;
  127. hdr = skb_header_pointer(skb, skb_transport_offset(skb),
  128. sizeof(_hdr), &_hdr);
  129. if (!hdr)
  130. return 1;
  131. if (hdr->type < 32) {
  132. __u32 data = raw_sk(sk)->filter.data;
  133. return ((1U << hdr->type) & data) != 0;
  134. }
  135. /* Do not block unknown ICMP types */
  136. return 0;
  137. }
  138. /* IP input processing comes here for RAW socket delivery.
  139. * Caller owns SKB, so we must make clones.
  140. *
  141. * RFC 1122: SHOULD pass TOS value up to the transport layer.
  142. * -> It does. And not only TOS, but all IP header.
  143. */
  144. static int raw_v4_input(struct sk_buff *skb, const struct iphdr *iph, int hash)
  145. {
  146. struct sock *sk;
  147. struct hlist_head *head;
  148. int delivered = 0;
  149. struct net *net;
  150. read_lock(&raw_v4_hashinfo.lock);
  151. head = &raw_v4_hashinfo.ht[hash];
  152. if (hlist_empty(head))
  153. goto out;
  154. net = dev_net(skb->dev);
  155. sk = __raw_v4_lookup(net, __sk_head(head), iph->protocol,
  156. iph->saddr, iph->daddr,
  157. skb->dev->ifindex);
  158. while (sk) {
  159. delivered = 1;
  160. if (iph->protocol != IPPROTO_ICMP || !icmp_filter(sk, skb)) {
  161. struct sk_buff *clone = skb_clone(skb, GFP_ATOMIC);
  162. /* Not releasing hash table! */
  163. if (clone)
  164. raw_rcv(sk, clone);
  165. }
  166. sk = __raw_v4_lookup(net, sk_next(sk), iph->protocol,
  167. iph->saddr, iph->daddr,
  168. skb->dev->ifindex);
  169. }
  170. out:
  171. read_unlock(&raw_v4_hashinfo.lock);
  172. return delivered;
  173. }
  174. int raw_local_deliver(struct sk_buff *skb, int protocol)
  175. {
  176. int hash;
  177. struct sock *raw_sk;
  178. hash = protocol & (RAW_HTABLE_SIZE - 1);
  179. raw_sk = sk_head(&raw_v4_hashinfo.ht[hash]);
  180. /* If there maybe a raw socket we must check - if not we
  181. * don't care less
  182. */
  183. if (raw_sk && !raw_v4_input(skb, ip_hdr(skb), hash))
  184. raw_sk = NULL;
  185. return raw_sk != NULL;
  186. }
  187. static void raw_err(struct sock *sk, struct sk_buff *skb, u32 info)
  188. {
  189. struct inet_sock *inet = inet_sk(sk);
  190. const int type = icmp_hdr(skb)->type;
  191. const int code = icmp_hdr(skb)->code;
  192. int err = 0;
  193. int harderr = 0;
  194. if (type == ICMP_DEST_UNREACH && code == ICMP_FRAG_NEEDED)
  195. ipv4_sk_update_pmtu(skb, sk, info);
  196. else if (type == ICMP_REDIRECT)
  197. ipv4_sk_redirect(skb, sk);
  198. /* Report error on raw socket, if:
  199. 1. User requested ip_recverr.
  200. 2. Socket is connected (otherwise the error indication
  201. is useless without ip_recverr and error is hard.
  202. */
  203. if (!inet->recverr && sk->sk_state != TCP_ESTABLISHED)
  204. return;
  205. switch (type) {
  206. default:
  207. case ICMP_TIME_EXCEEDED:
  208. err = EHOSTUNREACH;
  209. break;
  210. case ICMP_SOURCE_QUENCH:
  211. return;
  212. case ICMP_PARAMETERPROB:
  213. err = EPROTO;
  214. harderr = 1;
  215. break;
  216. case ICMP_DEST_UNREACH:
  217. err = EHOSTUNREACH;
  218. if (code > NR_ICMP_UNREACH)
  219. break;
  220. err = icmp_err_convert[code].errno;
  221. harderr = icmp_err_convert[code].fatal;
  222. if (code == ICMP_FRAG_NEEDED) {
  223. harderr = inet->pmtudisc != IP_PMTUDISC_DONT;
  224. err = EMSGSIZE;
  225. }
  226. }
  227. if (inet->recverr) {
  228. const struct iphdr *iph = (const struct iphdr *)skb->data;
  229. u8 *payload = skb->data + (iph->ihl << 2);
  230. if (inet->hdrincl)
  231. payload = skb->data;
  232. ip_icmp_error(sk, skb, err, 0, info, payload);
  233. }
  234. if (inet->recverr || harderr) {
  235. sk->sk_err = err;
  236. sk->sk_error_report(sk);
  237. }
  238. }
  239. void raw_icmp_error(struct sk_buff *skb, int protocol, u32 info)
  240. {
  241. int hash;
  242. struct sock *raw_sk;
  243. const struct iphdr *iph;
  244. struct net *net;
  245. hash = protocol & (RAW_HTABLE_SIZE - 1);
  246. read_lock(&raw_v4_hashinfo.lock);
  247. raw_sk = sk_head(&raw_v4_hashinfo.ht[hash]);
  248. if (raw_sk != NULL) {
  249. iph = (const struct iphdr *)skb->data;
  250. net = dev_net(skb->dev);
  251. while ((raw_sk = __raw_v4_lookup(net, raw_sk, protocol,
  252. iph->daddr, iph->saddr,
  253. skb->dev->ifindex)) != NULL) {
  254. raw_err(raw_sk, skb, info);
  255. raw_sk = sk_next(raw_sk);
  256. iph = (const struct iphdr *)skb->data;
  257. }
  258. }
  259. read_unlock(&raw_v4_hashinfo.lock);
  260. }
  261. static int raw_rcv_skb(struct sock *sk, struct sk_buff *skb)
  262. {
  263. /* Charge it to the socket. */
  264. ipv4_pktinfo_prepare(skb);
  265. if (sock_queue_rcv_skb(sk, skb) < 0) {
  266. kfree_skb(skb);
  267. return NET_RX_DROP;
  268. }
  269. return NET_RX_SUCCESS;
  270. }
  271. int raw_rcv(struct sock *sk, struct sk_buff *skb)
  272. {
  273. if (!xfrm4_policy_check(sk, XFRM_POLICY_IN, skb)) {
  274. atomic_inc(&sk->sk_drops);
  275. kfree_skb(skb);
  276. return NET_RX_DROP;
  277. }
  278. nf_reset(skb);
  279. skb_push(skb, skb->data - skb_network_header(skb));
  280. raw_rcv_skb(sk, skb);
  281. return 0;
  282. }
  283. static int raw_send_hdrinc(struct sock *sk, struct flowi4 *fl4,
  284. void *from, size_t length,
  285. struct rtable **rtp,
  286. unsigned int flags)
  287. {
  288. struct inet_sock *inet = inet_sk(sk);
  289. struct net *net = sock_net(sk);
  290. struct iphdr *iph;
  291. struct sk_buff *skb;
  292. unsigned int iphlen;
  293. int err;
  294. struct rtable *rt = *rtp;
  295. int hlen, tlen;
  296. if (length > rt->dst.dev->mtu) {
  297. ip_local_error(sk, EMSGSIZE, fl4->daddr, inet->inet_dport,
  298. rt->dst.dev->mtu);
  299. return -EMSGSIZE;
  300. }
  301. if (flags&MSG_PROBE)
  302. goto out;
  303. hlen = LL_RESERVED_SPACE(rt->dst.dev);
  304. tlen = rt->dst.dev->needed_tailroom;
  305. skb = sock_alloc_send_skb(sk,
  306. length + hlen + tlen + 15,
  307. flags & MSG_DONTWAIT, &err);
  308. if (skb == NULL)
  309. goto error;
  310. skb_reserve(skb, hlen);
  311. skb->priority = sk->sk_priority;
  312. skb->mark = sk->sk_mark;
  313. skb_dst_set(skb, &rt->dst);
  314. *rtp = NULL;
  315. skb_reset_network_header(skb);
  316. iph = ip_hdr(skb);
  317. skb_put(skb, length);
  318. skb->ip_summed = CHECKSUM_NONE;
  319. skb->transport_header = skb->network_header;
  320. err = -EFAULT;
  321. if (memcpy_fromiovecend((void *)iph, from, 0, length))
  322. goto error_free;
  323. iphlen = iph->ihl * 4;
  324. /*
  325. * We don't want to modify the ip header, but we do need to
  326. * be sure that it won't cause problems later along the network
  327. * stack. Specifically we want to make sure that iph->ihl is a
  328. * sane value. If ihl points beyond the length of the buffer passed
  329. * in, reject the frame as invalid
  330. */
  331. err = -EINVAL;
  332. if (iphlen > length)
  333. goto error_free;
  334. if (iphlen >= sizeof(*iph)) {
  335. if (!iph->saddr)
  336. iph->saddr = fl4->saddr;
  337. iph->check = 0;
  338. iph->tot_len = htons(length);
  339. if (!iph->id)
  340. ip_select_ident(iph, &rt->dst, NULL);
  341. iph->check = ip_fast_csum((unsigned char *)iph, iph->ihl);
  342. }
  343. if (iph->protocol == IPPROTO_ICMP)
  344. icmp_out_count(net, ((struct icmphdr *)
  345. skb_transport_header(skb))->type);
  346. err = NF_HOOK(NFPROTO_IPV4, NF_INET_LOCAL_OUT, skb, NULL,
  347. rt->dst.dev, dst_output);
  348. if (err > 0)
  349. err = net_xmit_errno(err);
  350. if (err)
  351. goto error;
  352. out:
  353. return 0;
  354. error_free:
  355. kfree_skb(skb);
  356. error:
  357. IP_INC_STATS(net, IPSTATS_MIB_OUTDISCARDS);
  358. if (err == -ENOBUFS && !inet->recverr)
  359. err = 0;
  360. return err;
  361. }
  362. static int raw_probe_proto_opt(struct flowi4 *fl4, struct msghdr *msg)
  363. {
  364. struct iovec *iov;
  365. u8 __user *type = NULL;
  366. u8 __user *code = NULL;
  367. int probed = 0;
  368. unsigned int i;
  369. if (!msg->msg_iov)
  370. return 0;
  371. for (i = 0; i < msg->msg_iovlen; i++) {
  372. iov = &msg->msg_iov[i];
  373. if (!iov)
  374. continue;
  375. switch (fl4->flowi4_proto) {
  376. case IPPROTO_ICMP:
  377. /* check if one-byte field is readable or not. */
  378. if (iov->iov_base && iov->iov_len < 1)
  379. break;
  380. if (!type) {
  381. type = iov->iov_base;
  382. /* check if code field is readable or not. */
  383. if (iov->iov_len > 1)
  384. code = type + 1;
  385. } else if (!code)
  386. code = iov->iov_base;
  387. if (type && code) {
  388. if (get_user(fl4->fl4_icmp_type, type) ||
  389. get_user(fl4->fl4_icmp_code, code))
  390. return -EFAULT;
  391. probed = 1;
  392. }
  393. break;
  394. default:
  395. probed = 1;
  396. break;
  397. }
  398. if (probed)
  399. break;
  400. }
  401. return 0;
  402. }
  403. static int raw_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
  404. size_t len)
  405. {
  406. struct inet_sock *inet = inet_sk(sk);
  407. struct ipcm_cookie ipc;
  408. struct rtable *rt = NULL;
  409. struct flowi4 fl4;
  410. int free = 0;
  411. __be32 daddr;
  412. __be32 saddr;
  413. u8 tos;
  414. int err;
  415. struct ip_options_data opt_copy;
  416. err = -EMSGSIZE;
  417. if (len > 0xFFFF)
  418. goto out;
  419. /*
  420. * Check the flags.
  421. */
  422. err = -EOPNOTSUPP;
  423. if (msg->msg_flags & MSG_OOB) /* Mirror BSD error message */
  424. goto out; /* compatibility */
  425. /*
  426. * Get and verify the address.
  427. */
  428. if (msg->msg_namelen) {
  429. struct sockaddr_in *usin = (struct sockaddr_in *)msg->msg_name;
  430. err = -EINVAL;
  431. if (msg->msg_namelen < sizeof(*usin))
  432. goto out;
  433. if (usin->sin_family != AF_INET) {
  434. pr_info_once("%s: %s forgot to set AF_INET. Fix it!\n",
  435. __func__, current->comm);
  436. err = -EAFNOSUPPORT;
  437. if (usin->sin_family)
  438. goto out;
  439. }
  440. daddr = usin->sin_addr.s_addr;
  441. /* ANK: I did not forget to get protocol from port field.
  442. * I just do not know, who uses this weirdness.
  443. * IP_HDRINCL is much more convenient.
  444. */
  445. } else {
  446. err = -EDESTADDRREQ;
  447. if (sk->sk_state != TCP_ESTABLISHED)
  448. goto out;
  449. daddr = inet->inet_daddr;
  450. }
  451. ipc.addr = inet->inet_saddr;
  452. ipc.opt = NULL;
  453. ipc.tx_flags = 0;
  454. ipc.oif = sk->sk_bound_dev_if;
  455. if (msg->msg_controllen) {
  456. err = ip_cmsg_send(sock_net(sk), msg, &ipc);
  457. if (err)
  458. goto out;
  459. if (ipc.opt)
  460. free = 1;
  461. }
  462. saddr = ipc.addr;
  463. ipc.addr = daddr;
  464. if (!ipc.opt) {
  465. struct ip_options_rcu *inet_opt;
  466. rcu_read_lock();
  467. inet_opt = rcu_dereference(inet->inet_opt);
  468. if (inet_opt) {
  469. memcpy(&opt_copy, inet_opt,
  470. sizeof(*inet_opt) + inet_opt->opt.optlen);
  471. ipc.opt = &opt_copy.opt;
  472. }
  473. rcu_read_unlock();
  474. }
  475. if (ipc.opt) {
  476. err = -EINVAL;
  477. /* Linux does not mangle headers on raw sockets,
  478. * so that IP options + IP_HDRINCL is non-sense.
  479. */
  480. if (inet->hdrincl)
  481. goto done;
  482. if (ipc.opt->opt.srr) {
  483. if (!daddr)
  484. goto done;
  485. daddr = ipc.opt->opt.faddr;
  486. }
  487. }
  488. tos = RT_CONN_FLAGS(sk);
  489. if (msg->msg_flags & MSG_DONTROUTE)
  490. tos |= RTO_ONLINK;
  491. if (ipv4_is_multicast(daddr)) {
  492. if (!ipc.oif)
  493. ipc.oif = inet->mc_index;
  494. if (!saddr)
  495. saddr = inet->mc_addr;
  496. } else if (!ipc.oif)
  497. ipc.oif = inet->uc_index;
  498. flowi4_init_output(&fl4, ipc.oif, sk->sk_mark, tos,
  499. RT_SCOPE_UNIVERSE,
  500. inet->hdrincl ? IPPROTO_RAW : sk->sk_protocol,
  501. inet_sk_flowi_flags(sk) | FLOWI_FLAG_CAN_SLEEP,
  502. daddr, saddr, 0, 0);
  503. if (!inet->hdrincl) {
  504. err = raw_probe_proto_opt(&fl4, msg);
  505. if (err)
  506. goto done;
  507. }
  508. security_sk_classify_flow(sk, flowi4_to_flowi(&fl4));
  509. rt = ip_route_output_flow(sock_net(sk), &fl4, sk);
  510. if (IS_ERR(rt)) {
  511. err = PTR_ERR(rt);
  512. rt = NULL;
  513. goto done;
  514. }
  515. err = -EACCES;
  516. if (rt->rt_flags & RTCF_BROADCAST && !sock_flag(sk, SOCK_BROADCAST))
  517. goto done;
  518. if (msg->msg_flags & MSG_CONFIRM)
  519. goto do_confirm;
  520. back_from_confirm:
  521. if (inet->hdrincl)
  522. err = raw_send_hdrinc(sk, &fl4, msg->msg_iov, len,
  523. &rt, msg->msg_flags);
  524. else {
  525. if (!ipc.addr)
  526. ipc.addr = fl4.daddr;
  527. lock_sock(sk);
  528. err = ip_append_data(sk, &fl4, ip_generic_getfrag,
  529. msg->msg_iov, len, 0,
  530. &ipc, &rt, msg->msg_flags);
  531. if (err)
  532. ip_flush_pending_frames(sk);
  533. else if (!(msg->msg_flags & MSG_MORE)) {
  534. err = ip_push_pending_frames(sk, &fl4);
  535. if (err == -ENOBUFS && !inet->recverr)
  536. err = 0;
  537. }
  538. release_sock(sk);
  539. }
  540. done:
  541. if (free)
  542. kfree(ipc.opt);
  543. ip_rt_put(rt);
  544. out:
  545. if (err < 0)
  546. return err;
  547. return len;
  548. do_confirm:
  549. dst_confirm(&rt->dst);
  550. if (!(msg->msg_flags & MSG_PROBE) || len)
  551. goto back_from_confirm;
  552. err = 0;
  553. goto done;
  554. }
  555. static void raw_close(struct sock *sk, long timeout)
  556. {
  557. /*
  558. * Raw sockets may have direct kernel references. Kill them.
  559. */
  560. ip_ra_control(sk, 0, NULL);
  561. sk_common_release(sk);
  562. }
  563. static void raw_destroy(struct sock *sk)
  564. {
  565. lock_sock(sk);
  566. ip_flush_pending_frames(sk);
  567. release_sock(sk);
  568. }
  569. /* This gets rid of all the nasties in af_inet. -DaveM */
  570. static int raw_bind(struct sock *sk, struct sockaddr *uaddr, int addr_len)
  571. {
  572. struct inet_sock *inet = inet_sk(sk);
  573. struct sockaddr_in *addr = (struct sockaddr_in *) uaddr;
  574. int ret = -EINVAL;
  575. int chk_addr_ret;
  576. if (sk->sk_state != TCP_CLOSE || addr_len < sizeof(struct sockaddr_in))
  577. goto out;
  578. chk_addr_ret = inet_addr_type(sock_net(sk), addr->sin_addr.s_addr);
  579. ret = -EADDRNOTAVAIL;
  580. if (addr->sin_addr.s_addr && chk_addr_ret != RTN_LOCAL &&
  581. chk_addr_ret != RTN_MULTICAST && chk_addr_ret != RTN_BROADCAST)
  582. goto out;
  583. inet->inet_rcv_saddr = inet->inet_saddr = addr->sin_addr.s_addr;
  584. if (chk_addr_ret == RTN_MULTICAST || chk_addr_ret == RTN_BROADCAST)
  585. inet->inet_saddr = 0; /* Use device */
  586. sk_dst_reset(sk);
  587. ret = 0;
  588. out: return ret;
  589. }
  590. /*
  591. * This should be easy, if there is something there
  592. * we return it, otherwise we block.
  593. */
  594. static int raw_recvmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
  595. size_t len, int noblock, int flags, int *addr_len)
  596. {
  597. struct inet_sock *inet = inet_sk(sk);
  598. size_t copied = 0;
  599. int err = -EOPNOTSUPP;
  600. struct sockaddr_in *sin = (struct sockaddr_in *)msg->msg_name;
  601. struct sk_buff *skb;
  602. if (flags & MSG_OOB)
  603. goto out;
  604. if (addr_len)
  605. *addr_len = sizeof(*sin);
  606. if (flags & MSG_ERRQUEUE) {
  607. err = ip_recv_error(sk, msg, len);
  608. goto out;
  609. }
  610. skb = skb_recv_datagram(sk, flags, noblock, &err);
  611. if (!skb)
  612. goto out;
  613. copied = skb->len;
  614. if (len < copied) {
  615. msg->msg_flags |= MSG_TRUNC;
  616. copied = len;
  617. }
  618. err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
  619. if (err)
  620. goto done;
  621. sock_recv_ts_and_drops(msg, sk, skb);
  622. /* Copy the address. */
  623. if (sin) {
  624. sin->sin_family = AF_INET;
  625. sin->sin_addr.s_addr = ip_hdr(skb)->saddr;
  626. sin->sin_port = 0;
  627. memset(&sin->sin_zero, 0, sizeof(sin->sin_zero));
  628. }
  629. if (inet->cmsg_flags)
  630. ip_cmsg_recv(msg, skb);
  631. if (flags & MSG_TRUNC)
  632. copied = skb->len;
  633. done:
  634. skb_free_datagram(sk, skb);
  635. out:
  636. if (err)
  637. return err;
  638. return copied;
  639. }
  640. static int raw_init(struct sock *sk)
  641. {
  642. struct raw_sock *rp = raw_sk(sk);
  643. if (inet_sk(sk)->inet_num == IPPROTO_ICMP)
  644. memset(&rp->filter, 0, sizeof(rp->filter));
  645. return 0;
  646. }
  647. static int raw_seticmpfilter(struct sock *sk, char __user *optval, int optlen)
  648. {
  649. if (optlen > sizeof(struct icmp_filter))
  650. optlen = sizeof(struct icmp_filter);
  651. if (copy_from_user(&raw_sk(sk)->filter, optval, optlen))
  652. return -EFAULT;
  653. return 0;
  654. }
  655. static int raw_geticmpfilter(struct sock *sk, char __user *optval, int __user *optlen)
  656. {
  657. int len, ret = -EFAULT;
  658. if (get_user(len, optlen))
  659. goto out;
  660. ret = -EINVAL;
  661. if (len < 0)
  662. goto out;
  663. if (len > sizeof(struct icmp_filter))
  664. len = sizeof(struct icmp_filter);
  665. ret = -EFAULT;
  666. if (put_user(len, optlen) ||
  667. copy_to_user(optval, &raw_sk(sk)->filter, len))
  668. goto out;
  669. ret = 0;
  670. out: return ret;
  671. }
  672. static int do_raw_setsockopt(struct sock *sk, int level, int optname,
  673. char __user *optval, unsigned int optlen)
  674. {
  675. if (optname == ICMP_FILTER) {
  676. if (inet_sk(sk)->inet_num != IPPROTO_ICMP)
  677. return -EOPNOTSUPP;
  678. else
  679. return raw_seticmpfilter(sk, optval, optlen);
  680. }
  681. return -ENOPROTOOPT;
  682. }
  683. static int raw_setsockopt(struct sock *sk, int level, int optname,
  684. char __user *optval, unsigned int optlen)
  685. {
  686. if (level != SOL_RAW)
  687. return ip_setsockopt(sk, level, optname, optval, optlen);
  688. return do_raw_setsockopt(sk, level, optname, optval, optlen);
  689. }
  690. #ifdef CONFIG_COMPAT
  691. static int compat_raw_setsockopt(struct sock *sk, int level, int optname,
  692. char __user *optval, unsigned int optlen)
  693. {
  694. if (level != SOL_RAW)
  695. return compat_ip_setsockopt(sk, level, optname, optval, optlen);
  696. return do_raw_setsockopt(sk, level, optname, optval, optlen);
  697. }
  698. #endif
  699. static int do_raw_getsockopt(struct sock *sk, int level, int optname,
  700. char __user *optval, int __user *optlen)
  701. {
  702. if (optname == ICMP_FILTER) {
  703. if (inet_sk(sk)->inet_num != IPPROTO_ICMP)
  704. return -EOPNOTSUPP;
  705. else
  706. return raw_geticmpfilter(sk, optval, optlen);
  707. }
  708. return -ENOPROTOOPT;
  709. }
  710. static int raw_getsockopt(struct sock *sk, int level, int optname,
  711. char __user *optval, int __user *optlen)
  712. {
  713. if (level != SOL_RAW)
  714. return ip_getsockopt(sk, level, optname, optval, optlen);
  715. return do_raw_getsockopt(sk, level, optname, optval, optlen);
  716. }
  717. #ifdef CONFIG_COMPAT
  718. static int compat_raw_getsockopt(struct sock *sk, int level, int optname,
  719. char __user *optval, int __user *optlen)
  720. {
  721. if (level != SOL_RAW)
  722. return compat_ip_getsockopt(sk, level, optname, optval, optlen);
  723. return do_raw_getsockopt(sk, level, optname, optval, optlen);
  724. }
  725. #endif
  726. static int raw_ioctl(struct sock *sk, int cmd, unsigned long arg)
  727. {
  728. switch (cmd) {
  729. case SIOCOUTQ: {
  730. int amount = sk_wmem_alloc_get(sk);
  731. return put_user(amount, (int __user *)arg);
  732. }
  733. case SIOCINQ: {
  734. struct sk_buff *skb;
  735. int amount = 0;
  736. spin_lock_bh(&sk->sk_receive_queue.lock);
  737. skb = skb_peek(&sk->sk_receive_queue);
  738. if (skb != NULL)
  739. amount = skb->len;
  740. spin_unlock_bh(&sk->sk_receive_queue.lock);
  741. return put_user(amount, (int __user *)arg);
  742. }
  743. default:
  744. #ifdef CONFIG_IP_MROUTE
  745. return ipmr_ioctl(sk, cmd, (void __user *)arg);
  746. #else
  747. return -ENOIOCTLCMD;
  748. #endif
  749. }
  750. }
  751. #ifdef CONFIG_COMPAT
  752. static int compat_raw_ioctl(struct sock *sk, unsigned int cmd, unsigned long arg)
  753. {
  754. switch (cmd) {
  755. case SIOCOUTQ:
  756. case SIOCINQ:
  757. return -ENOIOCTLCMD;
  758. default:
  759. #ifdef CONFIG_IP_MROUTE
  760. return ipmr_compat_ioctl(sk, cmd, compat_ptr(arg));
  761. #else
  762. return -ENOIOCTLCMD;
  763. #endif
  764. }
  765. }
  766. #endif
  767. struct proto raw_prot = {
  768. .name = "RAW",
  769. .owner = THIS_MODULE,
  770. .close = raw_close,
  771. .destroy = raw_destroy,
  772. .connect = ip4_datagram_connect,
  773. .disconnect = udp_disconnect,
  774. .ioctl = raw_ioctl,
  775. .init = raw_init,
  776. .setsockopt = raw_setsockopt,
  777. .getsockopt = raw_getsockopt,
  778. .sendmsg = raw_sendmsg,
  779. .recvmsg = raw_recvmsg,
  780. .bind = raw_bind,
  781. .backlog_rcv = raw_rcv_skb,
  782. .release_cb = ip4_datagram_release_cb,
  783. .hash = raw_hash_sk,
  784. .unhash = raw_unhash_sk,
  785. .obj_size = sizeof(struct raw_sock),
  786. .h.raw_hash = &raw_v4_hashinfo,
  787. #ifdef CONFIG_COMPAT
  788. .compat_setsockopt = compat_raw_setsockopt,
  789. .compat_getsockopt = compat_raw_getsockopt,
  790. .compat_ioctl = compat_raw_ioctl,
  791. #endif
  792. };
  793. #ifdef CONFIG_PROC_FS
  794. static struct sock *raw_get_first(struct seq_file *seq)
  795. {
  796. struct sock *sk;
  797. struct raw_iter_state *state = raw_seq_private(seq);
  798. for (state->bucket = 0; state->bucket < RAW_HTABLE_SIZE;
  799. ++state->bucket) {
  800. struct hlist_node *node;
  801. sk_for_each(sk, node, &state->h->ht[state->bucket])
  802. if (sock_net(sk) == seq_file_net(seq))
  803. goto found;
  804. }
  805. sk = NULL;
  806. found:
  807. return sk;
  808. }
  809. static struct sock *raw_get_next(struct seq_file *seq, struct sock *sk)
  810. {
  811. struct raw_iter_state *state = raw_seq_private(seq);
  812. do {
  813. sk = sk_next(sk);
  814. try_again:
  815. ;
  816. } while (sk && sock_net(sk) != seq_file_net(seq));
  817. if (!sk && ++state->bucket < RAW_HTABLE_SIZE) {
  818. sk = sk_head(&state->h->ht[state->bucket]);
  819. goto try_again;
  820. }
  821. return sk;
  822. }
  823. static struct sock *raw_get_idx(struct seq_file *seq, loff_t pos)
  824. {
  825. struct sock *sk = raw_get_first(seq);
  826. if (sk)
  827. while (pos && (sk = raw_get_next(seq, sk)) != NULL)
  828. --pos;
  829. return pos ? NULL : sk;
  830. }
  831. void *raw_seq_start(struct seq_file *seq, loff_t *pos)
  832. {
  833. struct raw_iter_state *state = raw_seq_private(seq);
  834. read_lock(&state->h->lock);
  835. return *pos ? raw_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
  836. }
  837. EXPORT_SYMBOL_GPL(raw_seq_start);
  838. void *raw_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  839. {
  840. struct sock *sk;
  841. if (v == SEQ_START_TOKEN)
  842. sk = raw_get_first(seq);
  843. else
  844. sk = raw_get_next(seq, v);
  845. ++*pos;
  846. return sk;
  847. }
  848. EXPORT_SYMBOL_GPL(raw_seq_next);
  849. void raw_seq_stop(struct seq_file *seq, void *v)
  850. {
  851. struct raw_iter_state *state = raw_seq_private(seq);
  852. read_unlock(&state->h->lock);
  853. }
  854. EXPORT_SYMBOL_GPL(raw_seq_stop);
  855. static void raw_sock_seq_show(struct seq_file *seq, struct sock *sp, int i)
  856. {
  857. struct inet_sock *inet = inet_sk(sp);
  858. __be32 dest = inet->inet_daddr,
  859. src = inet->inet_rcv_saddr;
  860. __u16 destp = 0,
  861. srcp = inet->inet_num;
  862. seq_printf(seq, "%4d: %08X:%04X %08X:%04X"
  863. " %02X %08X:%08X %02X:%08lX %08X %5d %8d %lu %d %pK %d\n",
  864. i, src, srcp, dest, destp, sp->sk_state,
  865. sk_wmem_alloc_get(sp),
  866. sk_rmem_alloc_get(sp),
  867. 0, 0L, 0,
  868. from_kuid_munged(seq_user_ns(seq), sock_i_uid(sp)),
  869. 0, sock_i_ino(sp),
  870. atomic_read(&sp->sk_refcnt), sp, atomic_read(&sp->sk_drops));
  871. }
  872. static int raw_seq_show(struct seq_file *seq, void *v)
  873. {
  874. if (v == SEQ_START_TOKEN)
  875. seq_printf(seq, " sl local_address rem_address st tx_queue "
  876. "rx_queue tr tm->when retrnsmt uid timeout "
  877. "inode ref pointer drops\n");
  878. else
  879. raw_sock_seq_show(seq, v, raw_seq_private(seq)->bucket);
  880. return 0;
  881. }
  882. static const struct seq_operations raw_seq_ops = {
  883. .start = raw_seq_start,
  884. .next = raw_seq_next,
  885. .stop = raw_seq_stop,
  886. .show = raw_seq_show,
  887. };
  888. int raw_seq_open(struct inode *ino, struct file *file,
  889. struct raw_hashinfo *h, const struct seq_operations *ops)
  890. {
  891. int err;
  892. struct raw_iter_state *i;
  893. err = seq_open_net(ino, file, ops, sizeof(struct raw_iter_state));
  894. if (err < 0)
  895. return err;
  896. i = raw_seq_private((struct seq_file *)file->private_data);
  897. i->h = h;
  898. return 0;
  899. }
  900. EXPORT_SYMBOL_GPL(raw_seq_open);
  901. static int raw_v4_seq_open(struct inode *inode, struct file *file)
  902. {
  903. return raw_seq_open(inode, file, &raw_v4_hashinfo, &raw_seq_ops);
  904. }
  905. static const struct file_operations raw_seq_fops = {
  906. .owner = THIS_MODULE,
  907. .open = raw_v4_seq_open,
  908. .read = seq_read,
  909. .llseek = seq_lseek,
  910. .release = seq_release_net,
  911. };
  912. static __net_init int raw_init_net(struct net *net)
  913. {
  914. if (!proc_create("raw", S_IRUGO, net->proc_net, &raw_seq_fops))
  915. return -ENOMEM;
  916. return 0;
  917. }
  918. static __net_exit void raw_exit_net(struct net *net)
  919. {
  920. remove_proc_entry("raw", net->proc_net);
  921. }
  922. static __net_initdata struct pernet_operations raw_net_ops = {
  923. .init = raw_init_net,
  924. .exit = raw_exit_net,
  925. };
  926. int __init raw_proc_init(void)
  927. {
  928. return register_pernet_subsys(&raw_net_ops);
  929. }
  930. void __init raw_proc_exit(void)
  931. {
  932. unregister_pernet_subsys(&raw_net_ops);
  933. }
  934. #endif /* CONFIG_PROC_FS */