raw.c 25 KB

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