raw.c 26 KB

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