datagram.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875
  1. /*
  2. * common UDP/RAW code
  3. * Linux INET6 implementation
  4. *
  5. * Authors:
  6. * Pedro Roque <roque@di.fc.ul.pt>
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version
  11. * 2 of the License, or (at your option) any later version.
  12. */
  13. #include <linux/capability.h>
  14. #include <linux/errno.h>
  15. #include <linux/types.h>
  16. #include <linux/kernel.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/socket.h>
  19. #include <linux/sockios.h>
  20. #include <linux/in6.h>
  21. #include <linux/ipv6.h>
  22. #include <linux/route.h>
  23. #include <linux/slab.h>
  24. #include <net/ipv6.h>
  25. #include <net/ndisc.h>
  26. #include <net/addrconf.h>
  27. #include <net/transp_v6.h>
  28. #include <net/ip6_route.h>
  29. #include <net/tcp_states.h>
  30. #include <linux/errqueue.h>
  31. #include <asm/uaccess.h>
  32. static inline int ipv6_mapped_addr_any(const struct in6_addr *a)
  33. {
  34. return (ipv6_addr_v4mapped(a) && (a->s6_addr32[3] == 0));
  35. }
  36. int ip6_datagram_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len)
  37. {
  38. struct sockaddr_in6 *usin = (struct sockaddr_in6 *) uaddr;
  39. struct inet_sock *inet = inet_sk(sk);
  40. struct ipv6_pinfo *np = inet6_sk(sk);
  41. struct in6_addr *daddr, *final_p, final;
  42. struct dst_entry *dst;
  43. struct flowi6 fl6;
  44. struct ip6_flowlabel *flowlabel = NULL;
  45. struct ipv6_txoptions *opt;
  46. int addr_type;
  47. int err;
  48. if (usin->sin6_family == AF_INET) {
  49. if (__ipv6_only_sock(sk))
  50. return -EAFNOSUPPORT;
  51. err = ip4_datagram_connect(sk, uaddr, addr_len);
  52. goto ipv4_connected;
  53. }
  54. if (addr_len < SIN6_LEN_RFC2133)
  55. return -EINVAL;
  56. if (usin->sin6_family != AF_INET6)
  57. return -EAFNOSUPPORT;
  58. memset(&fl6, 0, sizeof(fl6));
  59. if (np->sndflow) {
  60. fl6.flowlabel = usin->sin6_flowinfo&IPV6_FLOWINFO_MASK;
  61. if (fl6.flowlabel&IPV6_FLOWLABEL_MASK) {
  62. flowlabel = fl6_sock_lookup(sk, fl6.flowlabel);
  63. if (flowlabel == NULL)
  64. return -EINVAL;
  65. ipv6_addr_copy(&usin->sin6_addr, &flowlabel->dst);
  66. }
  67. }
  68. addr_type = ipv6_addr_type(&usin->sin6_addr);
  69. if (addr_type == IPV6_ADDR_ANY) {
  70. /*
  71. * connect to self
  72. */
  73. usin->sin6_addr.s6_addr[15] = 0x01;
  74. }
  75. daddr = &usin->sin6_addr;
  76. if (addr_type == IPV6_ADDR_MAPPED) {
  77. struct sockaddr_in sin;
  78. if (__ipv6_only_sock(sk)) {
  79. err = -ENETUNREACH;
  80. goto out;
  81. }
  82. sin.sin_family = AF_INET;
  83. sin.sin_addr.s_addr = daddr->s6_addr32[3];
  84. sin.sin_port = usin->sin6_port;
  85. err = ip4_datagram_connect(sk,
  86. (struct sockaddr*) &sin,
  87. sizeof(sin));
  88. ipv4_connected:
  89. if (err)
  90. goto out;
  91. ipv6_addr_set_v4mapped(inet->inet_daddr, &np->daddr);
  92. if (ipv6_addr_any(&np->saddr) ||
  93. ipv6_mapped_addr_any(&np->saddr))
  94. ipv6_addr_set_v4mapped(inet->inet_saddr, &np->saddr);
  95. if (ipv6_addr_any(&np->rcv_saddr) ||
  96. ipv6_mapped_addr_any(&np->rcv_saddr)) {
  97. ipv6_addr_set_v4mapped(inet->inet_rcv_saddr,
  98. &np->rcv_saddr);
  99. if (sk->sk_prot->rehash)
  100. sk->sk_prot->rehash(sk);
  101. }
  102. goto out;
  103. }
  104. if (addr_type&IPV6_ADDR_LINKLOCAL) {
  105. if (addr_len >= sizeof(struct sockaddr_in6) &&
  106. usin->sin6_scope_id) {
  107. if (sk->sk_bound_dev_if &&
  108. sk->sk_bound_dev_if != usin->sin6_scope_id) {
  109. err = -EINVAL;
  110. goto out;
  111. }
  112. sk->sk_bound_dev_if = usin->sin6_scope_id;
  113. }
  114. if (!sk->sk_bound_dev_if && (addr_type & IPV6_ADDR_MULTICAST))
  115. sk->sk_bound_dev_if = np->mcast_oif;
  116. /* Connect to link-local address requires an interface */
  117. if (!sk->sk_bound_dev_if) {
  118. err = -EINVAL;
  119. goto out;
  120. }
  121. }
  122. ipv6_addr_copy(&np->daddr, daddr);
  123. np->flow_label = fl6.flowlabel;
  124. inet->inet_dport = usin->sin6_port;
  125. /*
  126. * Check for a route to destination an obtain the
  127. * destination cache for it.
  128. */
  129. fl6.flowi6_proto = sk->sk_protocol;
  130. ipv6_addr_copy(&fl6.daddr, &np->daddr);
  131. ipv6_addr_copy(&fl6.saddr, &np->saddr);
  132. fl6.flowi6_oif = sk->sk_bound_dev_if;
  133. fl6.flowi6_mark = sk->sk_mark;
  134. fl6.fl6_dport = inet->inet_dport;
  135. fl6.fl6_sport = inet->inet_sport;
  136. if (!fl6.flowi6_oif && (addr_type&IPV6_ADDR_MULTICAST))
  137. fl6.flowi6_oif = np->mcast_oif;
  138. security_sk_classify_flow(sk, flowi6_to_flowi(&fl6));
  139. opt = flowlabel ? flowlabel->opt : np->opt;
  140. final_p = fl6_update_dst(&fl6, opt, &final);
  141. dst = ip6_dst_lookup_flow(sk, &fl6, final_p, true);
  142. err = 0;
  143. if (IS_ERR(dst)) {
  144. err = PTR_ERR(dst);
  145. goto out;
  146. }
  147. /* source address lookup done in ip6_dst_lookup */
  148. if (ipv6_addr_any(&np->saddr))
  149. ipv6_addr_copy(&np->saddr, &fl6.saddr);
  150. if (ipv6_addr_any(&np->rcv_saddr)) {
  151. ipv6_addr_copy(&np->rcv_saddr, &fl6.saddr);
  152. inet->inet_rcv_saddr = LOOPBACK4_IPV6;
  153. if (sk->sk_prot->rehash)
  154. sk->sk_prot->rehash(sk);
  155. }
  156. ip6_dst_store(sk, dst,
  157. ipv6_addr_equal(&fl6.daddr, &np->daddr) ?
  158. &np->daddr : NULL,
  159. #ifdef CONFIG_IPV6_SUBTREES
  160. ipv6_addr_equal(&fl6.saddr, &np->saddr) ?
  161. &np->saddr :
  162. #endif
  163. NULL);
  164. sk->sk_state = TCP_ESTABLISHED;
  165. out:
  166. fl6_sock_release(flowlabel);
  167. return err;
  168. }
  169. void ipv6_icmp_error(struct sock *sk, struct sk_buff *skb, int err,
  170. __be16 port, u32 info, u8 *payload)
  171. {
  172. struct ipv6_pinfo *np = inet6_sk(sk);
  173. struct icmp6hdr *icmph = icmp6_hdr(skb);
  174. struct sock_exterr_skb *serr;
  175. if (!np->recverr)
  176. return;
  177. skb = skb_clone(skb, GFP_ATOMIC);
  178. if (!skb)
  179. return;
  180. skb->protocol = htons(ETH_P_IPV6);
  181. serr = SKB_EXT_ERR(skb);
  182. serr->ee.ee_errno = err;
  183. serr->ee.ee_origin = SO_EE_ORIGIN_ICMP6;
  184. serr->ee.ee_type = icmph->icmp6_type;
  185. serr->ee.ee_code = icmph->icmp6_code;
  186. serr->ee.ee_pad = 0;
  187. serr->ee.ee_info = info;
  188. serr->ee.ee_data = 0;
  189. serr->addr_offset = (u8 *)&(((struct ipv6hdr *)(icmph + 1))->daddr) -
  190. skb_network_header(skb);
  191. serr->port = port;
  192. __skb_pull(skb, payload - skb->data);
  193. skb_reset_transport_header(skb);
  194. if (sock_queue_err_skb(sk, skb))
  195. kfree_skb(skb);
  196. }
  197. void ipv6_local_error(struct sock *sk, int err, struct flowi6 *fl6, u32 info)
  198. {
  199. struct ipv6_pinfo *np = inet6_sk(sk);
  200. struct sock_exterr_skb *serr;
  201. struct ipv6hdr *iph;
  202. struct sk_buff *skb;
  203. if (!np->recverr)
  204. return;
  205. skb = alloc_skb(sizeof(struct ipv6hdr), GFP_ATOMIC);
  206. if (!skb)
  207. return;
  208. skb->protocol = htons(ETH_P_IPV6);
  209. skb_put(skb, sizeof(struct ipv6hdr));
  210. skb_reset_network_header(skb);
  211. iph = ipv6_hdr(skb);
  212. ipv6_addr_copy(&iph->daddr, &fl6->daddr);
  213. serr = SKB_EXT_ERR(skb);
  214. serr->ee.ee_errno = err;
  215. serr->ee.ee_origin = SO_EE_ORIGIN_LOCAL;
  216. serr->ee.ee_type = 0;
  217. serr->ee.ee_code = 0;
  218. serr->ee.ee_pad = 0;
  219. serr->ee.ee_info = info;
  220. serr->ee.ee_data = 0;
  221. serr->addr_offset = (u8 *)&iph->daddr - skb_network_header(skb);
  222. serr->port = fl6->fl6_dport;
  223. __skb_pull(skb, skb_tail_pointer(skb) - skb->data);
  224. skb_reset_transport_header(skb);
  225. if (sock_queue_err_skb(sk, skb))
  226. kfree_skb(skb);
  227. }
  228. void ipv6_local_rxpmtu(struct sock *sk, struct flowi6 *fl6, u32 mtu)
  229. {
  230. struct ipv6_pinfo *np = inet6_sk(sk);
  231. struct ipv6hdr *iph;
  232. struct sk_buff *skb;
  233. struct ip6_mtuinfo *mtu_info;
  234. if (!np->rxopt.bits.rxpmtu)
  235. return;
  236. skb = alloc_skb(sizeof(struct ipv6hdr), GFP_ATOMIC);
  237. if (!skb)
  238. return;
  239. skb_put(skb, sizeof(struct ipv6hdr));
  240. skb_reset_network_header(skb);
  241. iph = ipv6_hdr(skb);
  242. ipv6_addr_copy(&iph->daddr, &fl6->daddr);
  243. mtu_info = IP6CBMTU(skb);
  244. if (!mtu_info) {
  245. kfree_skb(skb);
  246. return;
  247. }
  248. mtu_info->ip6m_mtu = mtu;
  249. mtu_info->ip6m_addr.sin6_family = AF_INET6;
  250. mtu_info->ip6m_addr.sin6_port = 0;
  251. mtu_info->ip6m_addr.sin6_flowinfo = 0;
  252. mtu_info->ip6m_addr.sin6_scope_id = fl6->flowi6_oif;
  253. ipv6_addr_copy(&mtu_info->ip6m_addr.sin6_addr, &ipv6_hdr(skb)->daddr);
  254. __skb_pull(skb, skb_tail_pointer(skb) - skb->data);
  255. skb_reset_transport_header(skb);
  256. skb = xchg(&np->rxpmtu, skb);
  257. kfree_skb(skb);
  258. }
  259. /*
  260. * Handle MSG_ERRQUEUE
  261. */
  262. int ipv6_recv_error(struct sock *sk, struct msghdr *msg, int len)
  263. {
  264. struct ipv6_pinfo *np = inet6_sk(sk);
  265. struct sock_exterr_skb *serr;
  266. struct sk_buff *skb, *skb2;
  267. struct sockaddr_in6 *sin;
  268. struct {
  269. struct sock_extended_err ee;
  270. struct sockaddr_in6 offender;
  271. } errhdr;
  272. int err;
  273. int copied;
  274. err = -EAGAIN;
  275. skb = skb_dequeue(&sk->sk_error_queue);
  276. if (skb == NULL)
  277. goto out;
  278. copied = skb->len;
  279. if (copied > len) {
  280. msg->msg_flags |= MSG_TRUNC;
  281. copied = len;
  282. }
  283. err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
  284. if (err)
  285. goto out_free_skb;
  286. sock_recv_timestamp(msg, sk, skb);
  287. serr = SKB_EXT_ERR(skb);
  288. sin = (struct sockaddr_in6 *)msg->msg_name;
  289. if (sin) {
  290. const unsigned char *nh = skb_network_header(skb);
  291. sin->sin6_family = AF_INET6;
  292. sin->sin6_flowinfo = 0;
  293. sin->sin6_port = serr->port;
  294. sin->sin6_scope_id = 0;
  295. if (skb->protocol == htons(ETH_P_IPV6)) {
  296. ipv6_addr_copy(&sin->sin6_addr,
  297. (struct in6_addr *)(nh + serr->addr_offset));
  298. if (np->sndflow)
  299. sin->sin6_flowinfo =
  300. (*(__be32 *)(nh + serr->addr_offset - 24) &
  301. IPV6_FLOWINFO_MASK);
  302. if (ipv6_addr_type(&sin->sin6_addr) & IPV6_ADDR_LINKLOCAL)
  303. sin->sin6_scope_id = IP6CB(skb)->iif;
  304. } else {
  305. ipv6_addr_set_v4mapped(*(__be32 *)(nh + serr->addr_offset),
  306. &sin->sin6_addr);
  307. }
  308. }
  309. memcpy(&errhdr.ee, &serr->ee, sizeof(struct sock_extended_err));
  310. sin = &errhdr.offender;
  311. sin->sin6_family = AF_UNSPEC;
  312. if (serr->ee.ee_origin != SO_EE_ORIGIN_LOCAL) {
  313. sin->sin6_family = AF_INET6;
  314. sin->sin6_flowinfo = 0;
  315. sin->sin6_scope_id = 0;
  316. if (skb->protocol == htons(ETH_P_IPV6)) {
  317. ipv6_addr_copy(&sin->sin6_addr, &ipv6_hdr(skb)->saddr);
  318. if (np->rxopt.all)
  319. datagram_recv_ctl(sk, msg, skb);
  320. if (ipv6_addr_type(&sin->sin6_addr) & IPV6_ADDR_LINKLOCAL)
  321. sin->sin6_scope_id = IP6CB(skb)->iif;
  322. } else {
  323. struct inet_sock *inet = inet_sk(sk);
  324. ipv6_addr_set_v4mapped(ip_hdr(skb)->saddr,
  325. &sin->sin6_addr);
  326. if (inet->cmsg_flags)
  327. ip_cmsg_recv(msg, skb);
  328. }
  329. }
  330. put_cmsg(msg, SOL_IPV6, IPV6_RECVERR, sizeof(errhdr), &errhdr);
  331. /* Now we could try to dump offended packet options */
  332. msg->msg_flags |= MSG_ERRQUEUE;
  333. err = copied;
  334. /* Reset and regenerate socket error */
  335. spin_lock_bh(&sk->sk_error_queue.lock);
  336. sk->sk_err = 0;
  337. if ((skb2 = skb_peek(&sk->sk_error_queue)) != NULL) {
  338. sk->sk_err = SKB_EXT_ERR(skb2)->ee.ee_errno;
  339. spin_unlock_bh(&sk->sk_error_queue.lock);
  340. sk->sk_error_report(sk);
  341. } else {
  342. spin_unlock_bh(&sk->sk_error_queue.lock);
  343. }
  344. out_free_skb:
  345. kfree_skb(skb);
  346. out:
  347. return err;
  348. }
  349. /*
  350. * Handle IPV6_RECVPATHMTU
  351. */
  352. int ipv6_recv_rxpmtu(struct sock *sk, struct msghdr *msg, int len)
  353. {
  354. struct ipv6_pinfo *np = inet6_sk(sk);
  355. struct sk_buff *skb;
  356. struct sockaddr_in6 *sin;
  357. struct ip6_mtuinfo mtu_info;
  358. int err;
  359. int copied;
  360. err = -EAGAIN;
  361. skb = xchg(&np->rxpmtu, NULL);
  362. if (skb == NULL)
  363. goto out;
  364. copied = skb->len;
  365. if (copied > len) {
  366. msg->msg_flags |= MSG_TRUNC;
  367. copied = len;
  368. }
  369. err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
  370. if (err)
  371. goto out_free_skb;
  372. sock_recv_timestamp(msg, sk, skb);
  373. memcpy(&mtu_info, IP6CBMTU(skb), sizeof(mtu_info));
  374. sin = (struct sockaddr_in6 *)msg->msg_name;
  375. if (sin) {
  376. sin->sin6_family = AF_INET6;
  377. sin->sin6_flowinfo = 0;
  378. sin->sin6_port = 0;
  379. sin->sin6_scope_id = mtu_info.ip6m_addr.sin6_scope_id;
  380. ipv6_addr_copy(&sin->sin6_addr, &mtu_info.ip6m_addr.sin6_addr);
  381. }
  382. put_cmsg(msg, SOL_IPV6, IPV6_PATHMTU, sizeof(mtu_info), &mtu_info);
  383. err = copied;
  384. out_free_skb:
  385. kfree_skb(skb);
  386. out:
  387. return err;
  388. }
  389. int datagram_recv_ctl(struct sock *sk, struct msghdr *msg, struct sk_buff *skb)
  390. {
  391. struct ipv6_pinfo *np = inet6_sk(sk);
  392. struct inet6_skb_parm *opt = IP6CB(skb);
  393. unsigned char *nh = skb_network_header(skb);
  394. if (np->rxopt.bits.rxinfo) {
  395. struct in6_pktinfo src_info;
  396. src_info.ipi6_ifindex = opt->iif;
  397. ipv6_addr_copy(&src_info.ipi6_addr, &ipv6_hdr(skb)->daddr);
  398. put_cmsg(msg, SOL_IPV6, IPV6_PKTINFO, sizeof(src_info), &src_info);
  399. }
  400. if (np->rxopt.bits.rxhlim) {
  401. int hlim = ipv6_hdr(skb)->hop_limit;
  402. put_cmsg(msg, SOL_IPV6, IPV6_HOPLIMIT, sizeof(hlim), &hlim);
  403. }
  404. if (np->rxopt.bits.rxtclass) {
  405. int tclass = (ntohl(*(__be32 *)ipv6_hdr(skb)) >> 20) & 0xff;
  406. put_cmsg(msg, SOL_IPV6, IPV6_TCLASS, sizeof(tclass), &tclass);
  407. }
  408. if (np->rxopt.bits.rxflow && (*(__be32 *)nh & IPV6_FLOWINFO_MASK)) {
  409. __be32 flowinfo = *(__be32 *)nh & IPV6_FLOWINFO_MASK;
  410. put_cmsg(msg, SOL_IPV6, IPV6_FLOWINFO, sizeof(flowinfo), &flowinfo);
  411. }
  412. /* HbH is allowed only once */
  413. if (np->rxopt.bits.hopopts && opt->hop) {
  414. u8 *ptr = nh + opt->hop;
  415. put_cmsg(msg, SOL_IPV6, IPV6_HOPOPTS, (ptr[1]+1)<<3, ptr);
  416. }
  417. if (opt->lastopt &&
  418. (np->rxopt.bits.dstopts || np->rxopt.bits.srcrt)) {
  419. /*
  420. * Silly enough, but we need to reparse in order to
  421. * report extension headers (except for HbH)
  422. * in order.
  423. *
  424. * Also note that IPV6_RECVRTHDRDSTOPTS is NOT
  425. * (and WILL NOT be) defined because
  426. * IPV6_RECVDSTOPTS is more generic. --yoshfuji
  427. */
  428. unsigned int off = sizeof(struct ipv6hdr);
  429. u8 nexthdr = ipv6_hdr(skb)->nexthdr;
  430. while (off <= opt->lastopt) {
  431. unsigned len;
  432. u8 *ptr = nh + off;
  433. switch(nexthdr) {
  434. case IPPROTO_DSTOPTS:
  435. nexthdr = ptr[0];
  436. len = (ptr[1] + 1) << 3;
  437. if (np->rxopt.bits.dstopts)
  438. put_cmsg(msg, SOL_IPV6, IPV6_DSTOPTS, len, ptr);
  439. break;
  440. case IPPROTO_ROUTING:
  441. nexthdr = ptr[0];
  442. len = (ptr[1] + 1) << 3;
  443. if (np->rxopt.bits.srcrt)
  444. put_cmsg(msg, SOL_IPV6, IPV6_RTHDR, len, ptr);
  445. break;
  446. case IPPROTO_AH:
  447. nexthdr = ptr[0];
  448. len = (ptr[1] + 2) << 2;
  449. break;
  450. default:
  451. nexthdr = ptr[0];
  452. len = (ptr[1] + 1) << 3;
  453. break;
  454. }
  455. off += len;
  456. }
  457. }
  458. /* socket options in old style */
  459. if (np->rxopt.bits.rxoinfo) {
  460. struct in6_pktinfo src_info;
  461. src_info.ipi6_ifindex = opt->iif;
  462. ipv6_addr_copy(&src_info.ipi6_addr, &ipv6_hdr(skb)->daddr);
  463. put_cmsg(msg, SOL_IPV6, IPV6_2292PKTINFO, sizeof(src_info), &src_info);
  464. }
  465. if (np->rxopt.bits.rxohlim) {
  466. int hlim = ipv6_hdr(skb)->hop_limit;
  467. put_cmsg(msg, SOL_IPV6, IPV6_2292HOPLIMIT, sizeof(hlim), &hlim);
  468. }
  469. if (np->rxopt.bits.ohopopts && opt->hop) {
  470. u8 *ptr = nh + opt->hop;
  471. put_cmsg(msg, SOL_IPV6, IPV6_2292HOPOPTS, (ptr[1]+1)<<3, ptr);
  472. }
  473. if (np->rxopt.bits.odstopts && opt->dst0) {
  474. u8 *ptr = nh + opt->dst0;
  475. put_cmsg(msg, SOL_IPV6, IPV6_2292DSTOPTS, (ptr[1]+1)<<3, ptr);
  476. }
  477. if (np->rxopt.bits.osrcrt && opt->srcrt) {
  478. struct ipv6_rt_hdr *rthdr = (struct ipv6_rt_hdr *)(nh + opt->srcrt);
  479. put_cmsg(msg, SOL_IPV6, IPV6_2292RTHDR, (rthdr->hdrlen+1) << 3, rthdr);
  480. }
  481. if (np->rxopt.bits.odstopts && opt->dst1) {
  482. u8 *ptr = nh + opt->dst1;
  483. put_cmsg(msg, SOL_IPV6, IPV6_2292DSTOPTS, (ptr[1]+1)<<3, ptr);
  484. }
  485. if (np->rxopt.bits.rxorigdstaddr) {
  486. struct sockaddr_in6 sin6;
  487. u16 *ports = (u16 *) skb_transport_header(skb);
  488. if (skb_transport_offset(skb) + 4 <= skb->len) {
  489. /* All current transport protocols have the port numbers in the
  490. * first four bytes of the transport header and this function is
  491. * written with this assumption in mind.
  492. */
  493. sin6.sin6_family = AF_INET6;
  494. ipv6_addr_copy(&sin6.sin6_addr, &ipv6_hdr(skb)->daddr);
  495. sin6.sin6_port = ports[1];
  496. sin6.sin6_flowinfo = 0;
  497. sin6.sin6_scope_id = 0;
  498. put_cmsg(msg, SOL_IPV6, IPV6_ORIGDSTADDR, sizeof(sin6), &sin6);
  499. }
  500. }
  501. return 0;
  502. }
  503. int datagram_send_ctl(struct net *net,
  504. struct msghdr *msg, struct flowi6 *fl6,
  505. struct ipv6_txoptions *opt,
  506. int *hlimit, int *tclass, int *dontfrag)
  507. {
  508. struct in6_pktinfo *src_info;
  509. struct cmsghdr *cmsg;
  510. struct ipv6_rt_hdr *rthdr;
  511. struct ipv6_opt_hdr *hdr;
  512. int len;
  513. int err = 0;
  514. for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) {
  515. int addr_type;
  516. if (!CMSG_OK(msg, cmsg)) {
  517. err = -EINVAL;
  518. goto exit_f;
  519. }
  520. if (cmsg->cmsg_level != SOL_IPV6)
  521. continue;
  522. switch (cmsg->cmsg_type) {
  523. case IPV6_PKTINFO:
  524. case IPV6_2292PKTINFO:
  525. {
  526. struct net_device *dev = NULL;
  527. if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct in6_pktinfo))) {
  528. err = -EINVAL;
  529. goto exit_f;
  530. }
  531. src_info = (struct in6_pktinfo *)CMSG_DATA(cmsg);
  532. if (src_info->ipi6_ifindex) {
  533. if (fl6->flowi6_oif &&
  534. src_info->ipi6_ifindex != fl6->flowi6_oif)
  535. return -EINVAL;
  536. fl6->flowi6_oif = src_info->ipi6_ifindex;
  537. }
  538. addr_type = __ipv6_addr_type(&src_info->ipi6_addr);
  539. rcu_read_lock();
  540. if (fl6->flowi6_oif) {
  541. dev = dev_get_by_index_rcu(net, fl6->flowi6_oif);
  542. if (!dev) {
  543. rcu_read_unlock();
  544. return -ENODEV;
  545. }
  546. } else if (addr_type & IPV6_ADDR_LINKLOCAL) {
  547. rcu_read_unlock();
  548. return -EINVAL;
  549. }
  550. if (addr_type != IPV6_ADDR_ANY) {
  551. int strict = __ipv6_addr_src_scope(addr_type) <= IPV6_ADDR_SCOPE_LINKLOCAL;
  552. if (!ipv6_chk_addr(net, &src_info->ipi6_addr,
  553. strict ? dev : NULL, 0))
  554. err = -EINVAL;
  555. else
  556. ipv6_addr_copy(&fl6->saddr, &src_info->ipi6_addr);
  557. }
  558. rcu_read_unlock();
  559. if (err)
  560. goto exit_f;
  561. break;
  562. }
  563. case IPV6_FLOWINFO:
  564. if (cmsg->cmsg_len < CMSG_LEN(4)) {
  565. err = -EINVAL;
  566. goto exit_f;
  567. }
  568. if (fl6->flowlabel&IPV6_FLOWINFO_MASK) {
  569. if ((fl6->flowlabel^*(__be32 *)CMSG_DATA(cmsg))&~IPV6_FLOWINFO_MASK) {
  570. err = -EINVAL;
  571. goto exit_f;
  572. }
  573. }
  574. fl6->flowlabel = IPV6_FLOWINFO_MASK & *(__be32 *)CMSG_DATA(cmsg);
  575. break;
  576. case IPV6_2292HOPOPTS:
  577. case IPV6_HOPOPTS:
  578. if (opt->hopopt || cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_opt_hdr))) {
  579. err = -EINVAL;
  580. goto exit_f;
  581. }
  582. hdr = (struct ipv6_opt_hdr *)CMSG_DATA(cmsg);
  583. len = ((hdr->hdrlen + 1) << 3);
  584. if (cmsg->cmsg_len < CMSG_LEN(len)) {
  585. err = -EINVAL;
  586. goto exit_f;
  587. }
  588. if (!capable(CAP_NET_RAW)) {
  589. err = -EPERM;
  590. goto exit_f;
  591. }
  592. opt->opt_nflen += len;
  593. opt->hopopt = hdr;
  594. break;
  595. case IPV6_2292DSTOPTS:
  596. if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_opt_hdr))) {
  597. err = -EINVAL;
  598. goto exit_f;
  599. }
  600. hdr = (struct ipv6_opt_hdr *)CMSG_DATA(cmsg);
  601. len = ((hdr->hdrlen + 1) << 3);
  602. if (cmsg->cmsg_len < CMSG_LEN(len)) {
  603. err = -EINVAL;
  604. goto exit_f;
  605. }
  606. if (!capable(CAP_NET_RAW)) {
  607. err = -EPERM;
  608. goto exit_f;
  609. }
  610. if (opt->dst1opt) {
  611. err = -EINVAL;
  612. goto exit_f;
  613. }
  614. opt->opt_flen += len;
  615. opt->dst1opt = hdr;
  616. break;
  617. case IPV6_DSTOPTS:
  618. case IPV6_RTHDRDSTOPTS:
  619. if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_opt_hdr))) {
  620. err = -EINVAL;
  621. goto exit_f;
  622. }
  623. hdr = (struct ipv6_opt_hdr *)CMSG_DATA(cmsg);
  624. len = ((hdr->hdrlen + 1) << 3);
  625. if (cmsg->cmsg_len < CMSG_LEN(len)) {
  626. err = -EINVAL;
  627. goto exit_f;
  628. }
  629. if (!capable(CAP_NET_RAW)) {
  630. err = -EPERM;
  631. goto exit_f;
  632. }
  633. if (cmsg->cmsg_type == IPV6_DSTOPTS) {
  634. opt->opt_flen += len;
  635. opt->dst1opt = hdr;
  636. } else {
  637. opt->opt_nflen += len;
  638. opt->dst0opt = hdr;
  639. }
  640. break;
  641. case IPV6_2292RTHDR:
  642. case IPV6_RTHDR:
  643. if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_rt_hdr))) {
  644. err = -EINVAL;
  645. goto exit_f;
  646. }
  647. rthdr = (struct ipv6_rt_hdr *)CMSG_DATA(cmsg);
  648. switch (rthdr->type) {
  649. #if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
  650. case IPV6_SRCRT_TYPE_2:
  651. if (rthdr->hdrlen != 2 ||
  652. rthdr->segments_left != 1) {
  653. err = -EINVAL;
  654. goto exit_f;
  655. }
  656. break;
  657. #endif
  658. default:
  659. err = -EINVAL;
  660. goto exit_f;
  661. }
  662. len = ((rthdr->hdrlen + 1) << 3);
  663. if (cmsg->cmsg_len < CMSG_LEN(len)) {
  664. err = -EINVAL;
  665. goto exit_f;
  666. }
  667. /* segments left must also match */
  668. if ((rthdr->hdrlen >> 1) != rthdr->segments_left) {
  669. err = -EINVAL;
  670. goto exit_f;
  671. }
  672. opt->opt_nflen += len;
  673. opt->srcrt = rthdr;
  674. if (cmsg->cmsg_type == IPV6_2292RTHDR && opt->dst1opt) {
  675. int dsthdrlen = ((opt->dst1opt->hdrlen+1)<<3);
  676. opt->opt_nflen += dsthdrlen;
  677. opt->dst0opt = opt->dst1opt;
  678. opt->dst1opt = NULL;
  679. opt->opt_flen -= dsthdrlen;
  680. }
  681. break;
  682. case IPV6_2292HOPLIMIT:
  683. case IPV6_HOPLIMIT:
  684. if (cmsg->cmsg_len != CMSG_LEN(sizeof(int))) {
  685. err = -EINVAL;
  686. goto exit_f;
  687. }
  688. *hlimit = *(int *)CMSG_DATA(cmsg);
  689. if (*hlimit < -1 || *hlimit > 0xff) {
  690. err = -EINVAL;
  691. goto exit_f;
  692. }
  693. break;
  694. case IPV6_TCLASS:
  695. {
  696. int tc;
  697. err = -EINVAL;
  698. if (cmsg->cmsg_len != CMSG_LEN(sizeof(int))) {
  699. goto exit_f;
  700. }
  701. tc = *(int *)CMSG_DATA(cmsg);
  702. if (tc < -1 || tc > 0xff)
  703. goto exit_f;
  704. err = 0;
  705. *tclass = tc;
  706. break;
  707. }
  708. case IPV6_DONTFRAG:
  709. {
  710. int df;
  711. err = -EINVAL;
  712. if (cmsg->cmsg_len != CMSG_LEN(sizeof(int))) {
  713. goto exit_f;
  714. }
  715. df = *(int *)CMSG_DATA(cmsg);
  716. if (df < 0 || df > 1)
  717. goto exit_f;
  718. err = 0;
  719. *dontfrag = df;
  720. break;
  721. }
  722. default:
  723. LIMIT_NETDEBUG(KERN_DEBUG "invalid cmsg type: %d\n",
  724. cmsg->cmsg_type);
  725. err = -EINVAL;
  726. goto exit_f;
  727. }
  728. }
  729. exit_f:
  730. return err;
  731. }