raw.c 25 KB

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