raw.c 25 KB

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