raw.c 25 KB

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