raw.c 25 KB

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