ipv6.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157
  1. /* SCTP kernel implementation
  2. * (C) Copyright IBM Corp. 2002, 2004
  3. * Copyright (c) 2001 Nokia, Inc.
  4. * Copyright (c) 2001 La Monte H.P. Yarroll
  5. * Copyright (c) 2002-2003 Intel Corp.
  6. *
  7. * This file is part of the SCTP kernel implementation
  8. *
  9. * SCTP over IPv6.
  10. *
  11. * This SCTP implementation is free software;
  12. * you can redistribute it and/or modify it under the terms of
  13. * the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2, or (at your option)
  15. * any later version.
  16. *
  17. * This SCTP implementation is distributed in the hope that it
  18. * will be useful, but WITHOUT ANY WARRANTY; without even the implied
  19. * ************************
  20. * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  21. * See the GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with GNU CC; see the file COPYING. If not, see
  25. * <http://www.gnu.org/licenses/>.
  26. *
  27. * Please send any bug reports or fixes you make to the
  28. * email address(es):
  29. * lksctp developers <linux-sctp@vger.kernel.org>
  30. *
  31. * Written or modified by:
  32. * Le Yanqun <yanqun.le@nokia.com>
  33. * Hui Huang <hui.huang@nokia.com>
  34. * La Monte H.P. Yarroll <piggy@acm.org>
  35. * Sridhar Samudrala <sri@us.ibm.com>
  36. * Jon Grimm <jgrimm@us.ibm.com>
  37. * Ardelle Fan <ardelle.fan@intel.com>
  38. *
  39. * Based on:
  40. * linux/net/ipv6/tcp_ipv6.c
  41. */
  42. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  43. #include <linux/module.h>
  44. #include <linux/errno.h>
  45. #include <linux/types.h>
  46. #include <linux/socket.h>
  47. #include <linux/sockios.h>
  48. #include <linux/net.h>
  49. #include <linux/in.h>
  50. #include <linux/in6.h>
  51. #include <linux/netdevice.h>
  52. #include <linux/init.h>
  53. #include <linux/ipsec.h>
  54. #include <linux/slab.h>
  55. #include <linux/ipv6.h>
  56. #include <linux/icmpv6.h>
  57. #include <linux/random.h>
  58. #include <linux/seq_file.h>
  59. #include <net/protocol.h>
  60. #include <net/ndisc.h>
  61. #include <net/ip.h>
  62. #include <net/ipv6.h>
  63. #include <net/transp_v6.h>
  64. #include <net/addrconf.h>
  65. #include <net/ip6_route.h>
  66. #include <net/inet_common.h>
  67. #include <net/inet_ecn.h>
  68. #include <net/sctp/sctp.h>
  69. #include <linux/uaccess.h>
  70. static inline int sctp_v6_addr_match_len(union sctp_addr *s1,
  71. union sctp_addr *s2);
  72. static void sctp_v6_to_addr(union sctp_addr *addr, struct in6_addr *saddr,
  73. __be16 port);
  74. static int sctp_v6_cmp_addr(const union sctp_addr *addr1,
  75. const union sctp_addr *addr2);
  76. /* Event handler for inet6 address addition/deletion events.
  77. * The sctp_local_addr_list needs to be protocted by a spin lock since
  78. * multiple notifiers (say IPv4 and IPv6) may be running at the same
  79. * time and thus corrupt the list.
  80. * The reader side is protected with RCU.
  81. */
  82. static int sctp_inet6addr_event(struct notifier_block *this, unsigned long ev,
  83. void *ptr)
  84. {
  85. struct inet6_ifaddr *ifa = (struct inet6_ifaddr *)ptr;
  86. struct sctp_sockaddr_entry *addr = NULL;
  87. struct sctp_sockaddr_entry *temp;
  88. struct net *net = dev_net(ifa->idev->dev);
  89. int found = 0;
  90. switch (ev) {
  91. case NETDEV_UP:
  92. addr = kmalloc(sizeof(struct sctp_sockaddr_entry), GFP_ATOMIC);
  93. if (addr) {
  94. addr->a.v6.sin6_family = AF_INET6;
  95. addr->a.v6.sin6_port = 0;
  96. addr->a.v6.sin6_addr = ifa->addr;
  97. addr->a.v6.sin6_scope_id = ifa->idev->dev->ifindex;
  98. addr->valid = 1;
  99. spin_lock_bh(&net->sctp.local_addr_lock);
  100. list_add_tail_rcu(&addr->list, &net->sctp.local_addr_list);
  101. sctp_addr_wq_mgmt(net, addr, SCTP_ADDR_NEW);
  102. spin_unlock_bh(&net->sctp.local_addr_lock);
  103. }
  104. break;
  105. case NETDEV_DOWN:
  106. spin_lock_bh(&net->sctp.local_addr_lock);
  107. list_for_each_entry_safe(addr, temp,
  108. &net->sctp.local_addr_list, list) {
  109. if (addr->a.sa.sa_family == AF_INET6 &&
  110. ipv6_addr_equal(&addr->a.v6.sin6_addr,
  111. &ifa->addr)) {
  112. sctp_addr_wq_mgmt(net, addr, SCTP_ADDR_DEL);
  113. found = 1;
  114. addr->valid = 0;
  115. list_del_rcu(&addr->list);
  116. break;
  117. }
  118. }
  119. spin_unlock_bh(&net->sctp.local_addr_lock);
  120. if (found)
  121. kfree_rcu(addr, rcu);
  122. break;
  123. }
  124. return NOTIFY_DONE;
  125. }
  126. static struct notifier_block sctp_inet6addr_notifier = {
  127. .notifier_call = sctp_inet6addr_event,
  128. };
  129. /* ICMP error handler. */
  130. static void sctp_v6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
  131. u8 type, u8 code, int offset, __be32 info)
  132. {
  133. struct inet6_dev *idev;
  134. struct sock *sk;
  135. struct sctp_association *asoc;
  136. struct sctp_transport *transport;
  137. struct ipv6_pinfo *np;
  138. __u16 saveip, savesctp;
  139. int err;
  140. struct net *net = dev_net(skb->dev);
  141. idev = in6_dev_get(skb->dev);
  142. /* Fix up skb to look at the embedded net header. */
  143. saveip = skb->network_header;
  144. savesctp = skb->transport_header;
  145. skb_reset_network_header(skb);
  146. skb_set_transport_header(skb, offset);
  147. sk = sctp_err_lookup(net, AF_INET6, skb, sctp_hdr(skb), &asoc, &transport);
  148. /* Put back, the original pointers. */
  149. skb->network_header = saveip;
  150. skb->transport_header = savesctp;
  151. if (!sk) {
  152. __ICMP6_INC_STATS(net, idev, ICMP6_MIB_INERRORS);
  153. goto out;
  154. }
  155. /* Warning: The sock lock is held. Remember to call
  156. * sctp_err_finish!
  157. */
  158. switch (type) {
  159. case ICMPV6_PKT_TOOBIG:
  160. if (ip6_sk_accept_pmtu(sk))
  161. sctp_icmp_frag_needed(sk, asoc, transport, ntohl(info));
  162. goto out_unlock;
  163. case ICMPV6_PARAMPROB:
  164. if (ICMPV6_UNK_NEXTHDR == code) {
  165. sctp_icmp_proto_unreachable(sk, asoc, transport);
  166. goto out_unlock;
  167. }
  168. break;
  169. case NDISC_REDIRECT:
  170. sctp_icmp_redirect(sk, transport, skb);
  171. goto out_unlock;
  172. default:
  173. break;
  174. }
  175. np = inet6_sk(sk);
  176. icmpv6_err_convert(type, code, &err);
  177. if (!sock_owned_by_user(sk) && np->recverr) {
  178. sk->sk_err = err;
  179. sk->sk_error_report(sk);
  180. } else { /* Only an error on timeout */
  181. sk->sk_err_soft = err;
  182. }
  183. out_unlock:
  184. sctp_err_finish(sk, transport);
  185. out:
  186. if (likely(idev != NULL))
  187. in6_dev_put(idev);
  188. }
  189. static int sctp_v6_xmit(struct sk_buff *skb, struct sctp_transport *transport)
  190. {
  191. struct sock *sk = skb->sk;
  192. struct ipv6_pinfo *np = inet6_sk(sk);
  193. struct flowi6 *fl6 = &transport->fl.u.ip6;
  194. int res;
  195. pr_debug("%s: skb:%p, len:%d, src:%pI6 dst:%pI6\n", __func__, skb,
  196. skb->len, &fl6->saddr, &fl6->daddr);
  197. IP6_ECN_flow_xmit(sk, fl6->flowlabel);
  198. if (!(transport->param_flags & SPP_PMTUD_ENABLE))
  199. skb->ignore_df = 1;
  200. SCTP_INC_STATS(sock_net(sk), SCTP_MIB_OUTSCTPPACKS);
  201. rcu_read_lock();
  202. res = ip6_xmit(sk, skb, fl6, sk->sk_mark, rcu_dereference(np->opt),
  203. np->tclass);
  204. rcu_read_unlock();
  205. return res;
  206. }
  207. /* Returns the dst cache entry for the given source and destination ip
  208. * addresses.
  209. */
  210. static void sctp_v6_get_dst(struct sctp_transport *t, union sctp_addr *saddr,
  211. struct flowi *fl, struct sock *sk)
  212. {
  213. struct sctp_association *asoc = t->asoc;
  214. struct dst_entry *dst = NULL;
  215. struct flowi6 *fl6 = &fl->u.ip6;
  216. struct sctp_bind_addr *bp;
  217. struct ipv6_pinfo *np = inet6_sk(sk);
  218. struct sctp_sockaddr_entry *laddr;
  219. union sctp_addr *daddr = &t->ipaddr;
  220. union sctp_addr dst_saddr;
  221. struct in6_addr *final_p, final;
  222. enum sctp_scope scope;
  223. __u8 matchlen = 0;
  224. memset(fl6, 0, sizeof(struct flowi6));
  225. fl6->daddr = daddr->v6.sin6_addr;
  226. fl6->fl6_dport = daddr->v6.sin6_port;
  227. fl6->flowi6_proto = IPPROTO_SCTP;
  228. if (ipv6_addr_type(&daddr->v6.sin6_addr) & IPV6_ADDR_LINKLOCAL)
  229. fl6->flowi6_oif = daddr->v6.sin6_scope_id;
  230. else if (asoc)
  231. fl6->flowi6_oif = asoc->base.sk->sk_bound_dev_if;
  232. pr_debug("%s: dst=%pI6 ", __func__, &fl6->daddr);
  233. if (asoc)
  234. fl6->fl6_sport = htons(asoc->base.bind_addr.port);
  235. if (saddr) {
  236. fl6->saddr = saddr->v6.sin6_addr;
  237. fl6->fl6_sport = saddr->v6.sin6_port;
  238. pr_debug("src=%pI6 - ", &fl6->saddr);
  239. }
  240. rcu_read_lock();
  241. final_p = fl6_update_dst(fl6, rcu_dereference(np->opt), &final);
  242. rcu_read_unlock();
  243. dst = ip6_dst_lookup_flow(sk, fl6, final_p);
  244. if (!asoc || saddr)
  245. goto out;
  246. bp = &asoc->base.bind_addr;
  247. scope = sctp_scope(daddr);
  248. /* ip6_dst_lookup has filled in the fl6->saddr for us. Check
  249. * to see if we can use it.
  250. */
  251. if (!IS_ERR(dst)) {
  252. /* Walk through the bind address list and look for a bind
  253. * address that matches the source address of the returned dst.
  254. */
  255. sctp_v6_to_addr(&dst_saddr, &fl6->saddr, htons(bp->port));
  256. rcu_read_lock();
  257. list_for_each_entry_rcu(laddr, &bp->address_list, list) {
  258. if (!laddr->valid || laddr->state == SCTP_ADDR_DEL ||
  259. (laddr->state != SCTP_ADDR_SRC &&
  260. !asoc->src_out_of_asoc_ok))
  261. continue;
  262. /* Do not compare against v4 addrs */
  263. if ((laddr->a.sa.sa_family == AF_INET6) &&
  264. (sctp_v6_cmp_addr(&dst_saddr, &laddr->a))) {
  265. rcu_read_unlock();
  266. goto out;
  267. }
  268. }
  269. rcu_read_unlock();
  270. /* None of the bound addresses match the source address of the
  271. * dst. So release it.
  272. */
  273. dst_release(dst);
  274. dst = NULL;
  275. }
  276. /* Walk through the bind address list and try to get the
  277. * best source address for a given destination.
  278. */
  279. rcu_read_lock();
  280. list_for_each_entry_rcu(laddr, &bp->address_list, list) {
  281. struct dst_entry *bdst;
  282. __u8 bmatchlen;
  283. if (!laddr->valid ||
  284. laddr->state != SCTP_ADDR_SRC ||
  285. laddr->a.sa.sa_family != AF_INET6 ||
  286. scope > sctp_scope(&laddr->a))
  287. continue;
  288. fl6->saddr = laddr->a.v6.sin6_addr;
  289. fl6->fl6_sport = laddr->a.v6.sin6_port;
  290. final_p = fl6_update_dst(fl6, rcu_dereference(np->opt), &final);
  291. bdst = ip6_dst_lookup_flow(sk, fl6, final_p);
  292. if (IS_ERR(bdst))
  293. continue;
  294. if (ipv6_chk_addr(dev_net(bdst->dev),
  295. &laddr->a.v6.sin6_addr, bdst->dev, 1)) {
  296. if (!IS_ERR_OR_NULL(dst))
  297. dst_release(dst);
  298. dst = bdst;
  299. break;
  300. }
  301. bmatchlen = sctp_v6_addr_match_len(daddr, &laddr->a);
  302. if (matchlen > bmatchlen) {
  303. dst_release(bdst);
  304. continue;
  305. }
  306. if (!IS_ERR_OR_NULL(dst))
  307. dst_release(dst);
  308. dst = bdst;
  309. matchlen = bmatchlen;
  310. }
  311. rcu_read_unlock();
  312. out:
  313. if (!IS_ERR_OR_NULL(dst)) {
  314. struct rt6_info *rt;
  315. rt = (struct rt6_info *)dst;
  316. t->dst = dst;
  317. t->dst_cookie = rt6_get_cookie(rt);
  318. pr_debug("rt6_dst:%pI6/%d rt6_src:%pI6\n",
  319. &rt->rt6i_dst.addr, rt->rt6i_dst.plen,
  320. &fl6->saddr);
  321. } else {
  322. t->dst = NULL;
  323. pr_debug("no route\n");
  324. }
  325. }
  326. /* Returns the number of consecutive initial bits that match in the 2 ipv6
  327. * addresses.
  328. */
  329. static inline int sctp_v6_addr_match_len(union sctp_addr *s1,
  330. union sctp_addr *s2)
  331. {
  332. return ipv6_addr_diff(&s1->v6.sin6_addr, &s2->v6.sin6_addr);
  333. }
  334. /* Fills in the source address(saddr) based on the destination address(daddr)
  335. * and asoc's bind address list.
  336. */
  337. static void sctp_v6_get_saddr(struct sctp_sock *sk,
  338. struct sctp_transport *t,
  339. struct flowi *fl)
  340. {
  341. struct flowi6 *fl6 = &fl->u.ip6;
  342. union sctp_addr *saddr = &t->saddr;
  343. pr_debug("%s: asoc:%p dst:%p\n", __func__, t->asoc, t->dst);
  344. if (t->dst) {
  345. saddr->v6.sin6_family = AF_INET6;
  346. saddr->v6.sin6_addr = fl6->saddr;
  347. }
  348. }
  349. /* Make a copy of all potential local addresses. */
  350. static void sctp_v6_copy_addrlist(struct list_head *addrlist,
  351. struct net_device *dev)
  352. {
  353. struct inet6_dev *in6_dev;
  354. struct inet6_ifaddr *ifp;
  355. struct sctp_sockaddr_entry *addr;
  356. rcu_read_lock();
  357. if ((in6_dev = __in6_dev_get(dev)) == NULL) {
  358. rcu_read_unlock();
  359. return;
  360. }
  361. read_lock_bh(&in6_dev->lock);
  362. list_for_each_entry(ifp, &in6_dev->addr_list, if_list) {
  363. /* Add the address to the local list. */
  364. addr = kzalloc(sizeof(*addr), GFP_ATOMIC);
  365. if (addr) {
  366. addr->a.v6.sin6_family = AF_INET6;
  367. addr->a.v6.sin6_port = 0;
  368. addr->a.v6.sin6_addr = ifp->addr;
  369. addr->a.v6.sin6_scope_id = dev->ifindex;
  370. addr->valid = 1;
  371. INIT_LIST_HEAD(&addr->list);
  372. list_add_tail(&addr->list, addrlist);
  373. }
  374. }
  375. read_unlock_bh(&in6_dev->lock);
  376. rcu_read_unlock();
  377. }
  378. /* Copy over any ip options */
  379. static void sctp_v6_copy_ip_options(struct sock *sk, struct sock *newsk)
  380. {
  381. struct ipv6_pinfo *newnp, *np = inet6_sk(sk);
  382. struct ipv6_txoptions *opt;
  383. newnp = inet6_sk(newsk);
  384. rcu_read_lock();
  385. opt = rcu_dereference(np->opt);
  386. if (opt) {
  387. opt = ipv6_dup_options(newsk, opt);
  388. if (!opt)
  389. pr_err("%s: Failed to copy ip options\n", __func__);
  390. }
  391. RCU_INIT_POINTER(newnp->opt, opt);
  392. rcu_read_unlock();
  393. }
  394. /* Account for the IP options */
  395. static int sctp_v6_ip_options_len(struct sock *sk)
  396. {
  397. struct ipv6_pinfo *np = inet6_sk(sk);
  398. struct ipv6_txoptions *opt;
  399. int len = 0;
  400. rcu_read_lock();
  401. opt = rcu_dereference(np->opt);
  402. if (opt)
  403. len = opt->opt_flen + opt->opt_nflen;
  404. rcu_read_unlock();
  405. return len;
  406. }
  407. /* Initialize a sockaddr_storage from in incoming skb. */
  408. static void sctp_v6_from_skb(union sctp_addr *addr, struct sk_buff *skb,
  409. int is_saddr)
  410. {
  411. /* Always called on head skb, so this is safe */
  412. struct sctphdr *sh = sctp_hdr(skb);
  413. struct sockaddr_in6 *sa = &addr->v6;
  414. addr->v6.sin6_family = AF_INET6;
  415. addr->v6.sin6_flowinfo = 0; /* FIXME */
  416. addr->v6.sin6_scope_id = ((struct inet6_skb_parm *)skb->cb)->iif;
  417. if (is_saddr) {
  418. sa->sin6_port = sh->source;
  419. sa->sin6_addr = ipv6_hdr(skb)->saddr;
  420. } else {
  421. sa->sin6_port = sh->dest;
  422. sa->sin6_addr = ipv6_hdr(skb)->daddr;
  423. }
  424. }
  425. /* Initialize an sctp_addr from a socket. */
  426. static void sctp_v6_from_sk(union sctp_addr *addr, struct sock *sk)
  427. {
  428. addr->v6.sin6_family = AF_INET6;
  429. addr->v6.sin6_port = 0;
  430. addr->v6.sin6_addr = sk->sk_v6_rcv_saddr;
  431. }
  432. /* Initialize sk->sk_rcv_saddr from sctp_addr. */
  433. static void sctp_v6_to_sk_saddr(union sctp_addr *addr, struct sock *sk)
  434. {
  435. if (addr->sa.sa_family == AF_INET) {
  436. sk->sk_v6_rcv_saddr.s6_addr32[0] = 0;
  437. sk->sk_v6_rcv_saddr.s6_addr32[1] = 0;
  438. sk->sk_v6_rcv_saddr.s6_addr32[2] = htonl(0x0000ffff);
  439. sk->sk_v6_rcv_saddr.s6_addr32[3] =
  440. addr->v4.sin_addr.s_addr;
  441. } else {
  442. sk->sk_v6_rcv_saddr = addr->v6.sin6_addr;
  443. }
  444. }
  445. /* Initialize sk->sk_daddr from sctp_addr. */
  446. static void sctp_v6_to_sk_daddr(union sctp_addr *addr, struct sock *sk)
  447. {
  448. if (addr->sa.sa_family == AF_INET) {
  449. sk->sk_v6_daddr.s6_addr32[0] = 0;
  450. sk->sk_v6_daddr.s6_addr32[1] = 0;
  451. sk->sk_v6_daddr.s6_addr32[2] = htonl(0x0000ffff);
  452. sk->sk_v6_daddr.s6_addr32[3] = addr->v4.sin_addr.s_addr;
  453. } else {
  454. sk->sk_v6_daddr = addr->v6.sin6_addr;
  455. }
  456. }
  457. /* Initialize a sctp_addr from an address parameter. */
  458. static void sctp_v6_from_addr_param(union sctp_addr *addr,
  459. union sctp_addr_param *param,
  460. __be16 port, int iif)
  461. {
  462. addr->v6.sin6_family = AF_INET6;
  463. addr->v6.sin6_port = port;
  464. addr->v6.sin6_flowinfo = 0; /* BUG */
  465. addr->v6.sin6_addr = param->v6.addr;
  466. addr->v6.sin6_scope_id = iif;
  467. }
  468. /* Initialize an address parameter from a sctp_addr and return the length
  469. * of the address parameter.
  470. */
  471. static int sctp_v6_to_addr_param(const union sctp_addr *addr,
  472. union sctp_addr_param *param)
  473. {
  474. int length = sizeof(struct sctp_ipv6addr_param);
  475. param->v6.param_hdr.type = SCTP_PARAM_IPV6_ADDRESS;
  476. param->v6.param_hdr.length = htons(length);
  477. param->v6.addr = addr->v6.sin6_addr;
  478. return length;
  479. }
  480. /* Initialize a sctp_addr from struct in6_addr. */
  481. static void sctp_v6_to_addr(union sctp_addr *addr, struct in6_addr *saddr,
  482. __be16 port)
  483. {
  484. addr->sa.sa_family = AF_INET6;
  485. addr->v6.sin6_port = port;
  486. addr->v6.sin6_flowinfo = 0;
  487. addr->v6.sin6_addr = *saddr;
  488. addr->v6.sin6_scope_id = 0;
  489. }
  490. static int __sctp_v6_cmp_addr(const union sctp_addr *addr1,
  491. const union sctp_addr *addr2)
  492. {
  493. if (addr1->sa.sa_family != addr2->sa.sa_family) {
  494. if (addr1->sa.sa_family == AF_INET &&
  495. addr2->sa.sa_family == AF_INET6 &&
  496. ipv6_addr_v4mapped(&addr2->v6.sin6_addr) &&
  497. addr2->v6.sin6_addr.s6_addr32[3] ==
  498. addr1->v4.sin_addr.s_addr)
  499. return 1;
  500. if (addr2->sa.sa_family == AF_INET &&
  501. addr1->sa.sa_family == AF_INET6 &&
  502. ipv6_addr_v4mapped(&addr1->v6.sin6_addr) &&
  503. addr1->v6.sin6_addr.s6_addr32[3] ==
  504. addr2->v4.sin_addr.s_addr)
  505. return 1;
  506. return 0;
  507. }
  508. if (!ipv6_addr_equal(&addr1->v6.sin6_addr, &addr2->v6.sin6_addr))
  509. return 0;
  510. /* If this is a linklocal address, compare the scope_id. */
  511. if ((ipv6_addr_type(&addr1->v6.sin6_addr) & IPV6_ADDR_LINKLOCAL) &&
  512. addr1->v6.sin6_scope_id && addr2->v6.sin6_scope_id &&
  513. addr1->v6.sin6_scope_id != addr2->v6.sin6_scope_id)
  514. return 0;
  515. return 1;
  516. }
  517. /* Compare addresses exactly.
  518. * v4-mapped-v6 is also in consideration.
  519. */
  520. static int sctp_v6_cmp_addr(const union sctp_addr *addr1,
  521. const union sctp_addr *addr2)
  522. {
  523. return __sctp_v6_cmp_addr(addr1, addr2) &&
  524. addr1->v6.sin6_port == addr2->v6.sin6_port;
  525. }
  526. /* Initialize addr struct to INADDR_ANY. */
  527. static void sctp_v6_inaddr_any(union sctp_addr *addr, __be16 port)
  528. {
  529. memset(addr, 0x00, sizeof(union sctp_addr));
  530. addr->v6.sin6_family = AF_INET6;
  531. addr->v6.sin6_port = port;
  532. }
  533. /* Is this a wildcard address? */
  534. static int sctp_v6_is_any(const union sctp_addr *addr)
  535. {
  536. return ipv6_addr_any(&addr->v6.sin6_addr);
  537. }
  538. /* Should this be available for binding? */
  539. static int sctp_v6_available(union sctp_addr *addr, struct sctp_sock *sp)
  540. {
  541. int type;
  542. struct net *net = sock_net(&sp->inet.sk);
  543. const struct in6_addr *in6 = (const struct in6_addr *)&addr->v6.sin6_addr;
  544. type = ipv6_addr_type(in6);
  545. if (IPV6_ADDR_ANY == type)
  546. return 1;
  547. if (type == IPV6_ADDR_MAPPED) {
  548. if (sp && ipv6_only_sock(sctp_opt2sk(sp)))
  549. return 0;
  550. sctp_v6_map_v4(addr);
  551. return sctp_get_af_specific(AF_INET)->available(addr, sp);
  552. }
  553. if (!(type & IPV6_ADDR_UNICAST))
  554. return 0;
  555. return sp->inet.freebind || net->ipv6.sysctl.ip_nonlocal_bind ||
  556. ipv6_chk_addr(net, in6, NULL, 0);
  557. }
  558. /* This function checks if the address is a valid address to be used for
  559. * SCTP.
  560. *
  561. * Output:
  562. * Return 0 - If the address is a non-unicast or an illegal address.
  563. * Return 1 - If the address is a unicast.
  564. */
  565. static int sctp_v6_addr_valid(union sctp_addr *addr,
  566. struct sctp_sock *sp,
  567. const struct sk_buff *skb)
  568. {
  569. int ret = ipv6_addr_type(&addr->v6.sin6_addr);
  570. /* Support v4-mapped-v6 address. */
  571. if (ret == IPV6_ADDR_MAPPED) {
  572. /* Note: This routine is used in input, so v4-mapped-v6
  573. * are disallowed here when there is no sctp_sock.
  574. */
  575. if (sp && ipv6_only_sock(sctp_opt2sk(sp)))
  576. return 0;
  577. sctp_v6_map_v4(addr);
  578. return sctp_get_af_specific(AF_INET)->addr_valid(addr, sp, skb);
  579. }
  580. /* Is this a non-unicast address */
  581. if (!(ret & IPV6_ADDR_UNICAST))
  582. return 0;
  583. return 1;
  584. }
  585. /* What is the scope of 'addr'? */
  586. static enum sctp_scope sctp_v6_scope(union sctp_addr *addr)
  587. {
  588. enum sctp_scope retval;
  589. int v6scope;
  590. /* The IPv6 scope is really a set of bit fields.
  591. * See IFA_* in <net/if_inet6.h>. Map to a generic SCTP scope.
  592. */
  593. v6scope = ipv6_addr_scope(&addr->v6.sin6_addr);
  594. switch (v6scope) {
  595. case IFA_HOST:
  596. retval = SCTP_SCOPE_LOOPBACK;
  597. break;
  598. case IFA_LINK:
  599. retval = SCTP_SCOPE_LINK;
  600. break;
  601. case IFA_SITE:
  602. retval = SCTP_SCOPE_PRIVATE;
  603. break;
  604. default:
  605. retval = SCTP_SCOPE_GLOBAL;
  606. break;
  607. }
  608. return retval;
  609. }
  610. /* Create and initialize a new sk for the socket to be returned by accept(). */
  611. static struct sock *sctp_v6_create_accept_sk(struct sock *sk,
  612. struct sctp_association *asoc,
  613. bool kern)
  614. {
  615. struct sock *newsk;
  616. struct ipv6_pinfo *newnp, *np = inet6_sk(sk);
  617. struct sctp6_sock *newsctp6sk;
  618. newsk = sk_alloc(sock_net(sk), PF_INET6, GFP_KERNEL, sk->sk_prot, kern);
  619. if (!newsk)
  620. goto out;
  621. sock_init_data(NULL, newsk);
  622. sctp_copy_sock(newsk, sk, asoc);
  623. sock_reset_flag(sk, SOCK_ZAPPED);
  624. newsctp6sk = (struct sctp6_sock *)newsk;
  625. inet_sk(newsk)->pinet6 = &newsctp6sk->inet6;
  626. sctp_sk(newsk)->v4mapped = sctp_sk(sk)->v4mapped;
  627. newnp = inet6_sk(newsk);
  628. memcpy(newnp, np, sizeof(struct ipv6_pinfo));
  629. newnp->ipv6_mc_list = NULL;
  630. newnp->ipv6_ac_list = NULL;
  631. newnp->ipv6_fl_list = NULL;
  632. sctp_v6_copy_ip_options(sk, newsk);
  633. /* Initialize sk's sport, dport, rcv_saddr and daddr for getsockname()
  634. * and getpeername().
  635. */
  636. sctp_v6_to_sk_daddr(&asoc->peer.primary_addr, newsk);
  637. newsk->sk_v6_rcv_saddr = sk->sk_v6_rcv_saddr;
  638. sk_refcnt_debug_inc(newsk);
  639. if (newsk->sk_prot->init(newsk)) {
  640. sk_common_release(newsk);
  641. newsk = NULL;
  642. }
  643. out:
  644. return newsk;
  645. }
  646. /* Format a sockaddr for return to user space. This makes sure the return is
  647. * AF_INET or AF_INET6 depending on the SCTP_I_WANT_MAPPED_V4_ADDR option.
  648. */
  649. static int sctp_v6_addr_to_user(struct sctp_sock *sp, union sctp_addr *addr)
  650. {
  651. if (sp->v4mapped) {
  652. if (addr->sa.sa_family == AF_INET)
  653. sctp_v4_map_v6(addr);
  654. } else {
  655. if (addr->sa.sa_family == AF_INET6 &&
  656. ipv6_addr_v4mapped(&addr->v6.sin6_addr))
  657. sctp_v6_map_v4(addr);
  658. }
  659. if (addr->sa.sa_family == AF_INET) {
  660. memset(addr->v4.sin_zero, 0, sizeof(addr->v4.sin_zero));
  661. return sizeof(struct sockaddr_in);
  662. }
  663. return sizeof(struct sockaddr_in6);
  664. }
  665. /* Where did this skb come from? */
  666. static int sctp_v6_skb_iif(const struct sk_buff *skb)
  667. {
  668. return IP6CB(skb)->iif;
  669. }
  670. /* Was this packet marked by Explicit Congestion Notification? */
  671. static int sctp_v6_is_ce(const struct sk_buff *skb)
  672. {
  673. return *((__u32 *)(ipv6_hdr(skb))) & (__force __u32)htonl(1 << 20);
  674. }
  675. /* Dump the v6 addr to the seq file. */
  676. static void sctp_v6_seq_dump_addr(struct seq_file *seq, union sctp_addr *addr)
  677. {
  678. seq_printf(seq, "%pI6 ", &addr->v6.sin6_addr);
  679. }
  680. static void sctp_v6_ecn_capable(struct sock *sk)
  681. {
  682. inet6_sk(sk)->tclass |= INET_ECN_ECT_0;
  683. }
  684. /* Initialize a PF_INET msgname from a ulpevent. */
  685. static void sctp_inet6_event_msgname(struct sctp_ulpevent *event,
  686. char *msgname, int *addrlen)
  687. {
  688. union sctp_addr *addr;
  689. struct sctp_association *asoc;
  690. union sctp_addr *paddr;
  691. if (!msgname)
  692. return;
  693. addr = (union sctp_addr *)msgname;
  694. asoc = event->asoc;
  695. paddr = &asoc->peer.primary_addr;
  696. if (paddr->sa.sa_family == AF_INET) {
  697. addr->v4.sin_family = AF_INET;
  698. addr->v4.sin_port = htons(asoc->peer.port);
  699. addr->v4.sin_addr = paddr->v4.sin_addr;
  700. } else {
  701. addr->v6.sin6_family = AF_INET6;
  702. addr->v6.sin6_flowinfo = 0;
  703. if (ipv6_addr_type(&paddr->v6.sin6_addr) & IPV6_ADDR_LINKLOCAL)
  704. addr->v6.sin6_scope_id = paddr->v6.sin6_scope_id;
  705. else
  706. addr->v6.sin6_scope_id = 0;
  707. addr->v6.sin6_port = htons(asoc->peer.port);
  708. addr->v6.sin6_addr = paddr->v6.sin6_addr;
  709. }
  710. *addrlen = sctp_v6_addr_to_user(sctp_sk(asoc->base.sk), addr);
  711. }
  712. /* Initialize a msg_name from an inbound skb. */
  713. static void sctp_inet6_skb_msgname(struct sk_buff *skb, char *msgname,
  714. int *addr_len)
  715. {
  716. union sctp_addr *addr;
  717. struct sctphdr *sh;
  718. if (!msgname)
  719. return;
  720. addr = (union sctp_addr *)msgname;
  721. sh = sctp_hdr(skb);
  722. if (ip_hdr(skb)->version == 4) {
  723. addr->v4.sin_family = AF_INET;
  724. addr->v4.sin_port = sh->source;
  725. addr->v4.sin_addr.s_addr = ip_hdr(skb)->saddr;
  726. } else {
  727. addr->v6.sin6_family = AF_INET6;
  728. addr->v6.sin6_flowinfo = 0;
  729. addr->v6.sin6_port = sh->source;
  730. addr->v6.sin6_addr = ipv6_hdr(skb)->saddr;
  731. if (ipv6_addr_type(&addr->v6.sin6_addr) & IPV6_ADDR_LINKLOCAL)
  732. addr->v6.sin6_scope_id = sctp_v6_skb_iif(skb);
  733. else
  734. addr->v6.sin6_scope_id = 0;
  735. }
  736. *addr_len = sctp_v6_addr_to_user(sctp_sk(skb->sk), addr);
  737. }
  738. /* Do we support this AF? */
  739. static int sctp_inet6_af_supported(sa_family_t family, struct sctp_sock *sp)
  740. {
  741. switch (family) {
  742. case AF_INET6:
  743. return 1;
  744. /* v4-mapped-v6 addresses */
  745. case AF_INET:
  746. if (!__ipv6_only_sock(sctp_opt2sk(sp)))
  747. return 1;
  748. /* fallthru */
  749. default:
  750. return 0;
  751. }
  752. }
  753. /* Address matching with wildcards allowed. This extra level
  754. * of indirection lets us choose whether a PF_INET6 should
  755. * disallow any v4 addresses if we so choose.
  756. */
  757. static int sctp_inet6_cmp_addr(const union sctp_addr *addr1,
  758. const union sctp_addr *addr2,
  759. struct sctp_sock *opt)
  760. {
  761. struct sock *sk = sctp_opt2sk(opt);
  762. struct sctp_af *af1, *af2;
  763. af1 = sctp_get_af_specific(addr1->sa.sa_family);
  764. af2 = sctp_get_af_specific(addr2->sa.sa_family);
  765. if (!af1 || !af2)
  766. return 0;
  767. /* If the socket is IPv6 only, v4 addrs will not match */
  768. if (__ipv6_only_sock(sk) && af1 != af2)
  769. return 0;
  770. /* Today, wildcard AF_INET/AF_INET6. */
  771. if (sctp_is_any(sk, addr1) || sctp_is_any(sk, addr2))
  772. return 1;
  773. if (addr1->sa.sa_family == AF_INET && addr2->sa.sa_family == AF_INET)
  774. return addr1->v4.sin_addr.s_addr == addr2->v4.sin_addr.s_addr;
  775. return __sctp_v6_cmp_addr(addr1, addr2);
  776. }
  777. /* Verify that the provided sockaddr looks bindable. Common verification,
  778. * has already been taken care of.
  779. */
  780. static int sctp_inet6_bind_verify(struct sctp_sock *opt, union sctp_addr *addr)
  781. {
  782. struct sctp_af *af;
  783. /* ASSERT: address family has already been verified. */
  784. if (addr->sa.sa_family != AF_INET6)
  785. af = sctp_get_af_specific(addr->sa.sa_family);
  786. else {
  787. int type = ipv6_addr_type(&addr->v6.sin6_addr);
  788. struct net_device *dev;
  789. if (type & IPV6_ADDR_LINKLOCAL) {
  790. struct net *net;
  791. if (!addr->v6.sin6_scope_id)
  792. return 0;
  793. net = sock_net(&opt->inet.sk);
  794. rcu_read_lock();
  795. dev = dev_get_by_index_rcu(net, addr->v6.sin6_scope_id);
  796. if (!dev || !(opt->inet.freebind ||
  797. net->ipv6.sysctl.ip_nonlocal_bind ||
  798. ipv6_chk_addr(net, &addr->v6.sin6_addr,
  799. dev, 0))) {
  800. rcu_read_unlock();
  801. return 0;
  802. }
  803. rcu_read_unlock();
  804. }
  805. af = opt->pf->af;
  806. }
  807. return af->available(addr, opt);
  808. }
  809. /* Verify that the provided sockaddr looks sendable. Common verification,
  810. * has already been taken care of.
  811. */
  812. static int sctp_inet6_send_verify(struct sctp_sock *opt, union sctp_addr *addr)
  813. {
  814. struct sctp_af *af = NULL;
  815. /* ASSERT: address family has already been verified. */
  816. if (addr->sa.sa_family != AF_INET6)
  817. af = sctp_get_af_specific(addr->sa.sa_family);
  818. else {
  819. int type = ipv6_addr_type(&addr->v6.sin6_addr);
  820. struct net_device *dev;
  821. if (type & IPV6_ADDR_LINKLOCAL) {
  822. if (!addr->v6.sin6_scope_id)
  823. return 0;
  824. rcu_read_lock();
  825. dev = dev_get_by_index_rcu(sock_net(&opt->inet.sk),
  826. addr->v6.sin6_scope_id);
  827. rcu_read_unlock();
  828. if (!dev)
  829. return 0;
  830. }
  831. af = opt->pf->af;
  832. }
  833. return af != NULL;
  834. }
  835. /* Fill in Supported Address Type information for INIT and INIT-ACK
  836. * chunks. Note: In the future, we may want to look at sock options
  837. * to determine whether a PF_INET6 socket really wants to have IPV4
  838. * addresses.
  839. * Returns number of addresses supported.
  840. */
  841. static int sctp_inet6_supported_addrs(const struct sctp_sock *opt,
  842. __be16 *types)
  843. {
  844. types[0] = SCTP_PARAM_IPV6_ADDRESS;
  845. if (!opt || !ipv6_only_sock(sctp_opt2sk(opt))) {
  846. types[1] = SCTP_PARAM_IPV4_ADDRESS;
  847. return 2;
  848. }
  849. return 1;
  850. }
  851. /* Handle SCTP_I_WANT_MAPPED_V4_ADDR for getpeername() and getsockname() */
  852. static int sctp_getname(struct socket *sock, struct sockaddr *uaddr,
  853. int peer)
  854. {
  855. int rc;
  856. rc = inet6_getname(sock, uaddr, peer);
  857. if (rc < 0)
  858. return rc;
  859. rc = sctp_v6_addr_to_user(sctp_sk(sock->sk),
  860. (union sctp_addr *)uaddr);
  861. return rc;
  862. }
  863. static const struct proto_ops inet6_seqpacket_ops = {
  864. .family = PF_INET6,
  865. .owner = THIS_MODULE,
  866. .release = inet6_release,
  867. .bind = inet6_bind,
  868. .connect = sctp_inet_connect,
  869. .socketpair = sock_no_socketpair,
  870. .accept = inet_accept,
  871. .getname = sctp_getname,
  872. .poll_mask = sctp_poll_mask,
  873. .ioctl = inet6_ioctl,
  874. .listen = sctp_inet_listen,
  875. .shutdown = inet_shutdown,
  876. .setsockopt = sock_common_setsockopt,
  877. .getsockopt = sock_common_getsockopt,
  878. .sendmsg = inet_sendmsg,
  879. .recvmsg = inet_recvmsg,
  880. .mmap = sock_no_mmap,
  881. #ifdef CONFIG_COMPAT
  882. .compat_setsockopt = compat_sock_common_setsockopt,
  883. .compat_getsockopt = compat_sock_common_getsockopt,
  884. #endif
  885. };
  886. static struct inet_protosw sctpv6_seqpacket_protosw = {
  887. .type = SOCK_SEQPACKET,
  888. .protocol = IPPROTO_SCTP,
  889. .prot = &sctpv6_prot,
  890. .ops = &inet6_seqpacket_ops,
  891. .flags = SCTP_PROTOSW_FLAG
  892. };
  893. static struct inet_protosw sctpv6_stream_protosw = {
  894. .type = SOCK_STREAM,
  895. .protocol = IPPROTO_SCTP,
  896. .prot = &sctpv6_prot,
  897. .ops = &inet6_seqpacket_ops,
  898. .flags = SCTP_PROTOSW_FLAG,
  899. };
  900. static int sctp6_rcv(struct sk_buff *skb)
  901. {
  902. return sctp_rcv(skb) ? -1 : 0;
  903. }
  904. static const struct inet6_protocol sctpv6_protocol = {
  905. .handler = sctp6_rcv,
  906. .err_handler = sctp_v6_err,
  907. .flags = INET6_PROTO_NOPOLICY | INET6_PROTO_FINAL,
  908. };
  909. static struct sctp_af sctp_af_inet6 = {
  910. .sa_family = AF_INET6,
  911. .sctp_xmit = sctp_v6_xmit,
  912. .setsockopt = ipv6_setsockopt,
  913. .getsockopt = ipv6_getsockopt,
  914. .get_dst = sctp_v6_get_dst,
  915. .get_saddr = sctp_v6_get_saddr,
  916. .copy_addrlist = sctp_v6_copy_addrlist,
  917. .from_skb = sctp_v6_from_skb,
  918. .from_sk = sctp_v6_from_sk,
  919. .from_addr_param = sctp_v6_from_addr_param,
  920. .to_addr_param = sctp_v6_to_addr_param,
  921. .cmp_addr = sctp_v6_cmp_addr,
  922. .scope = sctp_v6_scope,
  923. .addr_valid = sctp_v6_addr_valid,
  924. .inaddr_any = sctp_v6_inaddr_any,
  925. .is_any = sctp_v6_is_any,
  926. .available = sctp_v6_available,
  927. .skb_iif = sctp_v6_skb_iif,
  928. .is_ce = sctp_v6_is_ce,
  929. .seq_dump_addr = sctp_v6_seq_dump_addr,
  930. .ecn_capable = sctp_v6_ecn_capable,
  931. .net_header_len = sizeof(struct ipv6hdr),
  932. .sockaddr_len = sizeof(struct sockaddr_in6),
  933. .ip_options_len = sctp_v6_ip_options_len,
  934. #ifdef CONFIG_COMPAT
  935. .compat_setsockopt = compat_ipv6_setsockopt,
  936. .compat_getsockopt = compat_ipv6_getsockopt,
  937. #endif
  938. };
  939. static struct sctp_pf sctp_pf_inet6 = {
  940. .event_msgname = sctp_inet6_event_msgname,
  941. .skb_msgname = sctp_inet6_skb_msgname,
  942. .af_supported = sctp_inet6_af_supported,
  943. .cmp_addr = sctp_inet6_cmp_addr,
  944. .bind_verify = sctp_inet6_bind_verify,
  945. .send_verify = sctp_inet6_send_verify,
  946. .supported_addrs = sctp_inet6_supported_addrs,
  947. .create_accept_sk = sctp_v6_create_accept_sk,
  948. .addr_to_user = sctp_v6_addr_to_user,
  949. .to_sk_saddr = sctp_v6_to_sk_saddr,
  950. .to_sk_daddr = sctp_v6_to_sk_daddr,
  951. .copy_ip_options = sctp_v6_copy_ip_options,
  952. .af = &sctp_af_inet6,
  953. };
  954. /* Initialize IPv6 support and register with socket layer. */
  955. void sctp_v6_pf_init(void)
  956. {
  957. /* Register the SCTP specific PF_INET6 functions. */
  958. sctp_register_pf(&sctp_pf_inet6, PF_INET6);
  959. /* Register the SCTP specific AF_INET6 functions. */
  960. sctp_register_af(&sctp_af_inet6);
  961. }
  962. void sctp_v6_pf_exit(void)
  963. {
  964. list_del(&sctp_af_inet6.list);
  965. }
  966. /* Initialize IPv6 support and register with socket layer. */
  967. int sctp_v6_protosw_init(void)
  968. {
  969. int rc;
  970. rc = proto_register(&sctpv6_prot, 1);
  971. if (rc)
  972. return rc;
  973. /* Add SCTPv6(UDP and TCP style) to inetsw6 linked list. */
  974. inet6_register_protosw(&sctpv6_seqpacket_protosw);
  975. inet6_register_protosw(&sctpv6_stream_protosw);
  976. return 0;
  977. }
  978. void sctp_v6_protosw_exit(void)
  979. {
  980. inet6_unregister_protosw(&sctpv6_seqpacket_protosw);
  981. inet6_unregister_protosw(&sctpv6_stream_protosw);
  982. proto_unregister(&sctpv6_prot);
  983. }
  984. /* Register with inet6 layer. */
  985. int sctp_v6_add_protocol(void)
  986. {
  987. /* Register notifier for inet6 address additions/deletions. */
  988. register_inet6addr_notifier(&sctp_inet6addr_notifier);
  989. if (inet6_add_protocol(&sctpv6_protocol, IPPROTO_SCTP) < 0)
  990. return -EAGAIN;
  991. return 0;
  992. }
  993. /* Unregister with inet6 layer. */
  994. void sctp_v6_del_protocol(void)
  995. {
  996. inet6_del_protocol(&sctpv6_protocol, IPPROTO_SCTP);
  997. unregister_inet6addr_notifier(&sctp_inet6addr_notifier);
  998. }