raw.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101
  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 sock *sk, struct msghdr *msg, size_t len)
  415. {
  416. struct inet_sock *inet = inet_sk(sk);
  417. struct ipcm_cookie ipc;
  418. struct rtable *rt = NULL;
  419. struct flowi4 fl4;
  420. int free = 0;
  421. __be32 daddr;
  422. __be32 saddr;
  423. u8 tos;
  424. int err;
  425. struct ip_options_data opt_copy;
  426. struct raw_frag_vec rfv;
  427. err = -EMSGSIZE;
  428. if (len > 0xFFFF)
  429. goto out;
  430. /*
  431. * Check the flags.
  432. */
  433. err = -EOPNOTSUPP;
  434. if (msg->msg_flags & MSG_OOB) /* Mirror BSD error message */
  435. goto out; /* compatibility */
  436. /*
  437. * Get and verify the address.
  438. */
  439. if (msg->msg_namelen) {
  440. DECLARE_SOCKADDR(struct sockaddr_in *, usin, msg->msg_name);
  441. err = -EINVAL;
  442. if (msg->msg_namelen < sizeof(*usin))
  443. goto out;
  444. if (usin->sin_family != AF_INET) {
  445. pr_info_once("%s: %s forgot to set AF_INET. Fix it!\n",
  446. __func__, current->comm);
  447. err = -EAFNOSUPPORT;
  448. if (usin->sin_family)
  449. goto out;
  450. }
  451. daddr = usin->sin_addr.s_addr;
  452. /* ANK: I did not forget to get protocol from port field.
  453. * I just do not know, who uses this weirdness.
  454. * IP_HDRINCL is much more convenient.
  455. */
  456. } else {
  457. err = -EDESTADDRREQ;
  458. if (sk->sk_state != TCP_ESTABLISHED)
  459. goto out;
  460. daddr = inet->inet_daddr;
  461. }
  462. ipc.addr = inet->inet_saddr;
  463. ipc.opt = NULL;
  464. ipc.tx_flags = 0;
  465. ipc.ttl = 0;
  466. ipc.tos = -1;
  467. ipc.oif = sk->sk_bound_dev_if;
  468. if (msg->msg_controllen) {
  469. err = ip_cmsg_send(sock_net(sk), msg, &ipc, false);
  470. if (err)
  471. goto out;
  472. if (ipc.opt)
  473. free = 1;
  474. }
  475. saddr = ipc.addr;
  476. ipc.addr = daddr;
  477. if (!ipc.opt) {
  478. struct ip_options_rcu *inet_opt;
  479. rcu_read_lock();
  480. inet_opt = rcu_dereference(inet->inet_opt);
  481. if (inet_opt) {
  482. memcpy(&opt_copy, inet_opt,
  483. sizeof(*inet_opt) + inet_opt->opt.optlen);
  484. ipc.opt = &opt_copy.opt;
  485. }
  486. rcu_read_unlock();
  487. }
  488. if (ipc.opt) {
  489. err = -EINVAL;
  490. /* Linux does not mangle headers on raw sockets,
  491. * so that IP options + IP_HDRINCL is non-sense.
  492. */
  493. if (inet->hdrincl)
  494. goto done;
  495. if (ipc.opt->opt.srr) {
  496. if (!daddr)
  497. goto done;
  498. daddr = ipc.opt->opt.faddr;
  499. }
  500. }
  501. tos = get_rtconn_flags(&ipc, sk);
  502. if (msg->msg_flags & MSG_DONTROUTE)
  503. tos |= RTO_ONLINK;
  504. if (ipv4_is_multicast(daddr)) {
  505. if (!ipc.oif)
  506. ipc.oif = inet->mc_index;
  507. if (!saddr)
  508. saddr = inet->mc_addr;
  509. } else if (!ipc.oif)
  510. ipc.oif = inet->uc_index;
  511. flowi4_init_output(&fl4, ipc.oif, sk->sk_mark, tos,
  512. RT_SCOPE_UNIVERSE,
  513. inet->hdrincl ? IPPROTO_RAW : sk->sk_protocol,
  514. inet_sk_flowi_flags(sk) |
  515. (inet->hdrincl ? FLOWI_FLAG_KNOWN_NH : 0),
  516. daddr, saddr, 0, 0);
  517. if (!inet->hdrincl) {
  518. rfv.msg = msg;
  519. rfv.hlen = 0;
  520. err = raw_probe_proto_opt(&rfv, &fl4);
  521. if (err)
  522. goto done;
  523. }
  524. security_sk_classify_flow(sk, flowi4_to_flowi(&fl4));
  525. rt = ip_route_output_flow(sock_net(sk), &fl4, sk);
  526. if (IS_ERR(rt)) {
  527. err = PTR_ERR(rt);
  528. rt = NULL;
  529. goto done;
  530. }
  531. err = -EACCES;
  532. if (rt->rt_flags & RTCF_BROADCAST && !sock_flag(sk, SOCK_BROADCAST))
  533. goto done;
  534. if (msg->msg_flags & MSG_CONFIRM)
  535. goto do_confirm;
  536. back_from_confirm:
  537. if (inet->hdrincl)
  538. err = raw_send_hdrinc(sk, &fl4, msg, len,
  539. &rt, msg->msg_flags);
  540. else {
  541. sock_tx_timestamp(sk, &ipc.tx_flags);
  542. if (!ipc.addr)
  543. ipc.addr = fl4.daddr;
  544. lock_sock(sk);
  545. err = ip_append_data(sk, &fl4, raw_getfrag,
  546. &rfv, len, 0,
  547. &ipc, &rt, msg->msg_flags);
  548. if (err)
  549. ip_flush_pending_frames(sk);
  550. else if (!(msg->msg_flags & MSG_MORE)) {
  551. err = ip_push_pending_frames(sk, &fl4);
  552. if (err == -ENOBUFS && !inet->recverr)
  553. err = 0;
  554. }
  555. release_sock(sk);
  556. }
  557. done:
  558. if (free)
  559. kfree(ipc.opt);
  560. ip_rt_put(rt);
  561. out:
  562. if (err < 0)
  563. return err;
  564. return len;
  565. do_confirm:
  566. dst_confirm(&rt->dst);
  567. if (!(msg->msg_flags & MSG_PROBE) || len)
  568. goto back_from_confirm;
  569. err = 0;
  570. goto done;
  571. }
  572. static void raw_close(struct sock *sk, long timeout)
  573. {
  574. /*
  575. * Raw sockets may have direct kernel references. Kill them.
  576. */
  577. ip_ra_control(sk, 0, NULL);
  578. sk_common_release(sk);
  579. }
  580. static void raw_destroy(struct sock *sk)
  581. {
  582. lock_sock(sk);
  583. ip_flush_pending_frames(sk);
  584. release_sock(sk);
  585. }
  586. /* This gets rid of all the nasties in af_inet. -DaveM */
  587. static int raw_bind(struct sock *sk, struct sockaddr *uaddr, int addr_len)
  588. {
  589. struct inet_sock *inet = inet_sk(sk);
  590. struct sockaddr_in *addr = (struct sockaddr_in *) uaddr;
  591. int ret = -EINVAL;
  592. int chk_addr_ret;
  593. if (sk->sk_state != TCP_CLOSE || addr_len < sizeof(struct sockaddr_in))
  594. goto out;
  595. chk_addr_ret = inet_addr_type(sock_net(sk), addr->sin_addr.s_addr);
  596. ret = -EADDRNOTAVAIL;
  597. if (addr->sin_addr.s_addr && chk_addr_ret != RTN_LOCAL &&
  598. chk_addr_ret != RTN_MULTICAST && chk_addr_ret != RTN_BROADCAST)
  599. goto out;
  600. inet->inet_rcv_saddr = inet->inet_saddr = addr->sin_addr.s_addr;
  601. if (chk_addr_ret == RTN_MULTICAST || chk_addr_ret == RTN_BROADCAST)
  602. inet->inet_saddr = 0; /* Use device */
  603. sk_dst_reset(sk);
  604. ret = 0;
  605. out: return ret;
  606. }
  607. /*
  608. * This should be easy, if there is something there
  609. * we return it, otherwise we block.
  610. */
  611. static int raw_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
  612. int noblock, int flags, int *addr_len)
  613. {
  614. struct inet_sock *inet = inet_sk(sk);
  615. size_t copied = 0;
  616. int err = -EOPNOTSUPP;
  617. DECLARE_SOCKADDR(struct sockaddr_in *, sin, msg->msg_name);
  618. struct sk_buff *skb;
  619. if (flags & MSG_OOB)
  620. goto out;
  621. if (flags & MSG_ERRQUEUE) {
  622. err = ip_recv_error(sk, msg, len, addr_len);
  623. goto out;
  624. }
  625. skb = skb_recv_datagram(sk, flags, noblock, &err);
  626. if (!skb)
  627. goto out;
  628. copied = skb->len;
  629. if (len < copied) {
  630. msg->msg_flags |= MSG_TRUNC;
  631. copied = len;
  632. }
  633. err = skb_copy_datagram_msg(skb, 0, msg, copied);
  634. if (err)
  635. goto done;
  636. sock_recv_ts_and_drops(msg, sk, skb);
  637. /* Copy the address. */
  638. if (sin) {
  639. sin->sin_family = AF_INET;
  640. sin->sin_addr.s_addr = ip_hdr(skb)->saddr;
  641. sin->sin_port = 0;
  642. memset(&sin->sin_zero, 0, sizeof(sin->sin_zero));
  643. *addr_len = sizeof(*sin);
  644. }
  645. if (inet->cmsg_flags)
  646. ip_cmsg_recv(msg, skb);
  647. if (flags & MSG_TRUNC)
  648. copied = skb->len;
  649. done:
  650. skb_free_datagram(sk, skb);
  651. out:
  652. if (err)
  653. return err;
  654. return copied;
  655. }
  656. static int raw_init(struct sock *sk)
  657. {
  658. struct raw_sock *rp = raw_sk(sk);
  659. if (inet_sk(sk)->inet_num == IPPROTO_ICMP)
  660. memset(&rp->filter, 0, sizeof(rp->filter));
  661. return 0;
  662. }
  663. static int raw_seticmpfilter(struct sock *sk, char __user *optval, int optlen)
  664. {
  665. if (optlen > sizeof(struct icmp_filter))
  666. optlen = sizeof(struct icmp_filter);
  667. if (copy_from_user(&raw_sk(sk)->filter, optval, optlen))
  668. return -EFAULT;
  669. return 0;
  670. }
  671. static int raw_geticmpfilter(struct sock *sk, char __user *optval, int __user *optlen)
  672. {
  673. int len, ret = -EFAULT;
  674. if (get_user(len, optlen))
  675. goto out;
  676. ret = -EINVAL;
  677. if (len < 0)
  678. goto out;
  679. if (len > sizeof(struct icmp_filter))
  680. len = sizeof(struct icmp_filter);
  681. ret = -EFAULT;
  682. if (put_user(len, optlen) ||
  683. copy_to_user(optval, &raw_sk(sk)->filter, len))
  684. goto out;
  685. ret = 0;
  686. out: return ret;
  687. }
  688. static int do_raw_setsockopt(struct sock *sk, int level, int optname,
  689. char __user *optval, unsigned int optlen)
  690. {
  691. if (optname == ICMP_FILTER) {
  692. if (inet_sk(sk)->inet_num != IPPROTO_ICMP)
  693. return -EOPNOTSUPP;
  694. else
  695. return raw_seticmpfilter(sk, optval, optlen);
  696. }
  697. return -ENOPROTOOPT;
  698. }
  699. static int raw_setsockopt(struct sock *sk, int level, int optname,
  700. char __user *optval, unsigned int optlen)
  701. {
  702. if (level != SOL_RAW)
  703. return ip_setsockopt(sk, level, optname, optval, optlen);
  704. return do_raw_setsockopt(sk, level, optname, optval, optlen);
  705. }
  706. #ifdef CONFIG_COMPAT
  707. static int compat_raw_setsockopt(struct sock *sk, int level, int optname,
  708. char __user *optval, unsigned int optlen)
  709. {
  710. if (level != SOL_RAW)
  711. return compat_ip_setsockopt(sk, level, optname, optval, optlen);
  712. return do_raw_setsockopt(sk, level, optname, optval, optlen);
  713. }
  714. #endif
  715. static int do_raw_getsockopt(struct sock *sk, int level, int optname,
  716. char __user *optval, int __user *optlen)
  717. {
  718. if (optname == ICMP_FILTER) {
  719. if (inet_sk(sk)->inet_num != IPPROTO_ICMP)
  720. return -EOPNOTSUPP;
  721. else
  722. return raw_geticmpfilter(sk, optval, optlen);
  723. }
  724. return -ENOPROTOOPT;
  725. }
  726. static int raw_getsockopt(struct sock *sk, int level, int optname,
  727. char __user *optval, int __user *optlen)
  728. {
  729. if (level != SOL_RAW)
  730. return ip_getsockopt(sk, level, optname, optval, optlen);
  731. return do_raw_getsockopt(sk, level, optname, optval, optlen);
  732. }
  733. #ifdef CONFIG_COMPAT
  734. static int compat_raw_getsockopt(struct sock *sk, int level, int optname,
  735. char __user *optval, int __user *optlen)
  736. {
  737. if (level != SOL_RAW)
  738. return compat_ip_getsockopt(sk, level, optname, optval, optlen);
  739. return do_raw_getsockopt(sk, level, optname, optval, optlen);
  740. }
  741. #endif
  742. static int raw_ioctl(struct sock *sk, int cmd, unsigned long arg)
  743. {
  744. switch (cmd) {
  745. case SIOCOUTQ: {
  746. int amount = sk_wmem_alloc_get(sk);
  747. return put_user(amount, (int __user *)arg);
  748. }
  749. case SIOCINQ: {
  750. struct sk_buff *skb;
  751. int amount = 0;
  752. spin_lock_bh(&sk->sk_receive_queue.lock);
  753. skb = skb_peek(&sk->sk_receive_queue);
  754. if (skb != NULL)
  755. amount = skb->len;
  756. spin_unlock_bh(&sk->sk_receive_queue.lock);
  757. return put_user(amount, (int __user *)arg);
  758. }
  759. default:
  760. #ifdef CONFIG_IP_MROUTE
  761. return ipmr_ioctl(sk, cmd, (void __user *)arg);
  762. #else
  763. return -ENOIOCTLCMD;
  764. #endif
  765. }
  766. }
  767. #ifdef CONFIG_COMPAT
  768. static int compat_raw_ioctl(struct sock *sk, unsigned int cmd, unsigned long arg)
  769. {
  770. switch (cmd) {
  771. case SIOCOUTQ:
  772. case SIOCINQ:
  773. return -ENOIOCTLCMD;
  774. default:
  775. #ifdef CONFIG_IP_MROUTE
  776. return ipmr_compat_ioctl(sk, cmd, compat_ptr(arg));
  777. #else
  778. return -ENOIOCTLCMD;
  779. #endif
  780. }
  781. }
  782. #endif
  783. struct proto raw_prot = {
  784. .name = "RAW",
  785. .owner = THIS_MODULE,
  786. .close = raw_close,
  787. .destroy = raw_destroy,
  788. .connect = ip4_datagram_connect,
  789. .disconnect = udp_disconnect,
  790. .ioctl = raw_ioctl,
  791. .init = raw_init,
  792. .setsockopt = raw_setsockopt,
  793. .getsockopt = raw_getsockopt,
  794. .sendmsg = raw_sendmsg,
  795. .recvmsg = raw_recvmsg,
  796. .bind = raw_bind,
  797. .backlog_rcv = raw_rcv_skb,
  798. .release_cb = ip4_datagram_release_cb,
  799. .hash = raw_hash_sk,
  800. .unhash = raw_unhash_sk,
  801. .obj_size = sizeof(struct raw_sock),
  802. .h.raw_hash = &raw_v4_hashinfo,
  803. #ifdef CONFIG_COMPAT
  804. .compat_setsockopt = compat_raw_setsockopt,
  805. .compat_getsockopt = compat_raw_getsockopt,
  806. .compat_ioctl = compat_raw_ioctl,
  807. #endif
  808. };
  809. #ifdef CONFIG_PROC_FS
  810. static struct sock *raw_get_first(struct seq_file *seq)
  811. {
  812. struct sock *sk;
  813. struct raw_iter_state *state = raw_seq_private(seq);
  814. for (state->bucket = 0; state->bucket < RAW_HTABLE_SIZE;
  815. ++state->bucket) {
  816. sk_for_each(sk, &state->h->ht[state->bucket])
  817. if (sock_net(sk) == seq_file_net(seq))
  818. goto found;
  819. }
  820. sk = NULL;
  821. found:
  822. return sk;
  823. }
  824. static struct sock *raw_get_next(struct seq_file *seq, struct sock *sk)
  825. {
  826. struct raw_iter_state *state = raw_seq_private(seq);
  827. do {
  828. sk = sk_next(sk);
  829. try_again:
  830. ;
  831. } while (sk && sock_net(sk) != seq_file_net(seq));
  832. if (!sk && ++state->bucket < RAW_HTABLE_SIZE) {
  833. sk = sk_head(&state->h->ht[state->bucket]);
  834. goto try_again;
  835. }
  836. return sk;
  837. }
  838. static struct sock *raw_get_idx(struct seq_file *seq, loff_t pos)
  839. {
  840. struct sock *sk = raw_get_first(seq);
  841. if (sk)
  842. while (pos && (sk = raw_get_next(seq, sk)) != NULL)
  843. --pos;
  844. return pos ? NULL : sk;
  845. }
  846. void *raw_seq_start(struct seq_file *seq, loff_t *pos)
  847. {
  848. struct raw_iter_state *state = raw_seq_private(seq);
  849. read_lock(&state->h->lock);
  850. return *pos ? raw_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
  851. }
  852. EXPORT_SYMBOL_GPL(raw_seq_start);
  853. void *raw_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  854. {
  855. struct sock *sk;
  856. if (v == SEQ_START_TOKEN)
  857. sk = raw_get_first(seq);
  858. else
  859. sk = raw_get_next(seq, v);
  860. ++*pos;
  861. return sk;
  862. }
  863. EXPORT_SYMBOL_GPL(raw_seq_next);
  864. void raw_seq_stop(struct seq_file *seq, void *v)
  865. {
  866. struct raw_iter_state *state = raw_seq_private(seq);
  867. read_unlock(&state->h->lock);
  868. }
  869. EXPORT_SYMBOL_GPL(raw_seq_stop);
  870. static void raw_sock_seq_show(struct seq_file *seq, struct sock *sp, int i)
  871. {
  872. struct inet_sock *inet = inet_sk(sp);
  873. __be32 dest = inet->inet_daddr,
  874. src = inet->inet_rcv_saddr;
  875. __u16 destp = 0,
  876. srcp = inet->inet_num;
  877. seq_printf(seq, "%4d: %08X:%04X %08X:%04X"
  878. " %02X %08X:%08X %02X:%08lX %08X %5u %8d %lu %d %pK %d\n",
  879. i, src, srcp, dest, destp, sp->sk_state,
  880. sk_wmem_alloc_get(sp),
  881. sk_rmem_alloc_get(sp),
  882. 0, 0L, 0,
  883. from_kuid_munged(seq_user_ns(seq), sock_i_uid(sp)),
  884. 0, sock_i_ino(sp),
  885. atomic_read(&sp->sk_refcnt), sp, atomic_read(&sp->sk_drops));
  886. }
  887. static int raw_seq_show(struct seq_file *seq, void *v)
  888. {
  889. if (v == SEQ_START_TOKEN)
  890. seq_printf(seq, " sl local_address rem_address st tx_queue "
  891. "rx_queue tr tm->when retrnsmt uid timeout "
  892. "inode ref pointer drops\n");
  893. else
  894. raw_sock_seq_show(seq, v, raw_seq_private(seq)->bucket);
  895. return 0;
  896. }
  897. static const struct seq_operations raw_seq_ops = {
  898. .start = raw_seq_start,
  899. .next = raw_seq_next,
  900. .stop = raw_seq_stop,
  901. .show = raw_seq_show,
  902. };
  903. int raw_seq_open(struct inode *ino, struct file *file,
  904. struct raw_hashinfo *h, const struct seq_operations *ops)
  905. {
  906. int err;
  907. struct raw_iter_state *i;
  908. err = seq_open_net(ino, file, ops, sizeof(struct raw_iter_state));
  909. if (err < 0)
  910. return err;
  911. i = raw_seq_private((struct seq_file *)file->private_data);
  912. i->h = h;
  913. return 0;
  914. }
  915. EXPORT_SYMBOL_GPL(raw_seq_open);
  916. static int raw_v4_seq_open(struct inode *inode, struct file *file)
  917. {
  918. return raw_seq_open(inode, file, &raw_v4_hashinfo, &raw_seq_ops);
  919. }
  920. static const struct file_operations raw_seq_fops = {
  921. .owner = THIS_MODULE,
  922. .open = raw_v4_seq_open,
  923. .read = seq_read,
  924. .llseek = seq_lseek,
  925. .release = seq_release_net,
  926. };
  927. static __net_init int raw_init_net(struct net *net)
  928. {
  929. if (!proc_create("raw", S_IRUGO, net->proc_net, &raw_seq_fops))
  930. return -ENOMEM;
  931. return 0;
  932. }
  933. static __net_exit void raw_exit_net(struct net *net)
  934. {
  935. remove_proc_entry("raw", net->proc_net);
  936. }
  937. static __net_initdata struct pernet_operations raw_net_ops = {
  938. .init = raw_init_net,
  939. .exit = raw_exit_net,
  940. };
  941. int __init raw_proc_init(void)
  942. {
  943. return register_pernet_subsys(&raw_net_ops);
  944. }
  945. void __init raw_proc_exit(void)
  946. {
  947. unregister_pernet_subsys(&raw_net_ops);
  948. }
  949. #endif /* CONFIG_PROC_FS */