ipv6.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115
  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. ipv6_chk_addr(dev_net(bdst->dev),
  294. &laddr->a.v6.sin6_addr, bdst->dev, 1)) {
  295. if (!IS_ERR_OR_NULL(dst))
  296. dst_release(dst);
  297. dst = bdst;
  298. break;
  299. }
  300. bmatchlen = sctp_v6_addr_match_len(daddr, &laddr->a);
  301. if (matchlen > bmatchlen)
  302. continue;
  303. if (!IS_ERR_OR_NULL(dst))
  304. dst_release(dst);
  305. dst = bdst;
  306. matchlen = bmatchlen;
  307. }
  308. rcu_read_unlock();
  309. out:
  310. if (!IS_ERR_OR_NULL(dst)) {
  311. struct rt6_info *rt;
  312. rt = (struct rt6_info *)dst;
  313. t->dst = dst;
  314. t->dst_cookie = rt6_get_cookie(rt);
  315. pr_debug("rt6_dst:%pI6/%d rt6_src:%pI6\n",
  316. &rt->rt6i_dst.addr, rt->rt6i_dst.plen,
  317. &fl6->saddr);
  318. } else {
  319. t->dst = NULL;
  320. pr_debug("no route\n");
  321. }
  322. }
  323. /* Returns the number of consecutive initial bits that match in the 2 ipv6
  324. * addresses.
  325. */
  326. static inline int sctp_v6_addr_match_len(union sctp_addr *s1,
  327. union sctp_addr *s2)
  328. {
  329. return ipv6_addr_diff(&s1->v6.sin6_addr, &s2->v6.sin6_addr);
  330. }
  331. /* Fills in the source address(saddr) based on the destination address(daddr)
  332. * and asoc's bind address list.
  333. */
  334. static void sctp_v6_get_saddr(struct sctp_sock *sk,
  335. struct sctp_transport *t,
  336. struct flowi *fl)
  337. {
  338. struct flowi6 *fl6 = &fl->u.ip6;
  339. union sctp_addr *saddr = &t->saddr;
  340. pr_debug("%s: asoc:%p dst:%p\n", __func__, t->asoc, t->dst);
  341. if (t->dst) {
  342. saddr->v6.sin6_family = AF_INET6;
  343. saddr->v6.sin6_addr = fl6->saddr;
  344. }
  345. }
  346. /* Make a copy of all potential local addresses. */
  347. static void sctp_v6_copy_addrlist(struct list_head *addrlist,
  348. struct net_device *dev)
  349. {
  350. struct inet6_dev *in6_dev;
  351. struct inet6_ifaddr *ifp;
  352. struct sctp_sockaddr_entry *addr;
  353. rcu_read_lock();
  354. if ((in6_dev = __in6_dev_get(dev)) == NULL) {
  355. rcu_read_unlock();
  356. return;
  357. }
  358. read_lock_bh(&in6_dev->lock);
  359. list_for_each_entry(ifp, &in6_dev->addr_list, if_list) {
  360. /* Add the address to the local list. */
  361. addr = kzalloc(sizeof(*addr), GFP_ATOMIC);
  362. if (addr) {
  363. addr->a.v6.sin6_family = AF_INET6;
  364. addr->a.v6.sin6_port = 0;
  365. addr->a.v6.sin6_addr = ifp->addr;
  366. addr->a.v6.sin6_scope_id = dev->ifindex;
  367. addr->valid = 1;
  368. INIT_LIST_HEAD(&addr->list);
  369. list_add_tail(&addr->list, addrlist);
  370. }
  371. }
  372. read_unlock_bh(&in6_dev->lock);
  373. rcu_read_unlock();
  374. }
  375. /* Initialize a sockaddr_storage from in incoming skb. */
  376. static void sctp_v6_from_skb(union sctp_addr *addr, struct sk_buff *skb,
  377. int is_saddr)
  378. {
  379. /* Always called on head skb, so this is safe */
  380. struct sctphdr *sh = sctp_hdr(skb);
  381. struct sockaddr_in6 *sa = &addr->v6;
  382. addr->v6.sin6_family = AF_INET6;
  383. addr->v6.sin6_flowinfo = 0; /* FIXME */
  384. addr->v6.sin6_scope_id = ((struct inet6_skb_parm *)skb->cb)->iif;
  385. if (is_saddr) {
  386. sa->sin6_port = sh->source;
  387. sa->sin6_addr = ipv6_hdr(skb)->saddr;
  388. } else {
  389. sa->sin6_port = sh->dest;
  390. sa->sin6_addr = ipv6_hdr(skb)->daddr;
  391. }
  392. }
  393. /* Initialize an sctp_addr from a socket. */
  394. static void sctp_v6_from_sk(union sctp_addr *addr, struct sock *sk)
  395. {
  396. addr->v6.sin6_family = AF_INET6;
  397. addr->v6.sin6_port = 0;
  398. addr->v6.sin6_addr = sk->sk_v6_rcv_saddr;
  399. }
  400. /* Initialize sk->sk_rcv_saddr from sctp_addr. */
  401. static void sctp_v6_to_sk_saddr(union sctp_addr *addr, struct sock *sk)
  402. {
  403. if (addr->sa.sa_family == AF_INET) {
  404. sk->sk_v6_rcv_saddr.s6_addr32[0] = 0;
  405. sk->sk_v6_rcv_saddr.s6_addr32[1] = 0;
  406. sk->sk_v6_rcv_saddr.s6_addr32[2] = htonl(0x0000ffff);
  407. sk->sk_v6_rcv_saddr.s6_addr32[3] =
  408. addr->v4.sin_addr.s_addr;
  409. } else {
  410. sk->sk_v6_rcv_saddr = addr->v6.sin6_addr;
  411. }
  412. }
  413. /* Initialize sk->sk_daddr from sctp_addr. */
  414. static void sctp_v6_to_sk_daddr(union sctp_addr *addr, struct sock *sk)
  415. {
  416. if (addr->sa.sa_family == AF_INET) {
  417. sk->sk_v6_daddr.s6_addr32[0] = 0;
  418. sk->sk_v6_daddr.s6_addr32[1] = 0;
  419. sk->sk_v6_daddr.s6_addr32[2] = htonl(0x0000ffff);
  420. sk->sk_v6_daddr.s6_addr32[3] = addr->v4.sin_addr.s_addr;
  421. } else {
  422. sk->sk_v6_daddr = addr->v6.sin6_addr;
  423. }
  424. }
  425. /* Initialize a sctp_addr from an address parameter. */
  426. static void sctp_v6_from_addr_param(union sctp_addr *addr,
  427. union sctp_addr_param *param,
  428. __be16 port, int iif)
  429. {
  430. addr->v6.sin6_family = AF_INET6;
  431. addr->v6.sin6_port = port;
  432. addr->v6.sin6_flowinfo = 0; /* BUG */
  433. addr->v6.sin6_addr = param->v6.addr;
  434. addr->v6.sin6_scope_id = iif;
  435. }
  436. /* Initialize an address parameter from a sctp_addr and return the length
  437. * of the address parameter.
  438. */
  439. static int sctp_v6_to_addr_param(const union sctp_addr *addr,
  440. union sctp_addr_param *param)
  441. {
  442. int length = sizeof(struct sctp_ipv6addr_param);
  443. param->v6.param_hdr.type = SCTP_PARAM_IPV6_ADDRESS;
  444. param->v6.param_hdr.length = htons(length);
  445. param->v6.addr = addr->v6.sin6_addr;
  446. return length;
  447. }
  448. /* Initialize a sctp_addr from struct in6_addr. */
  449. static void sctp_v6_to_addr(union sctp_addr *addr, struct in6_addr *saddr,
  450. __be16 port)
  451. {
  452. addr->sa.sa_family = AF_INET6;
  453. addr->v6.sin6_port = port;
  454. addr->v6.sin6_flowinfo = 0;
  455. addr->v6.sin6_addr = *saddr;
  456. addr->v6.sin6_scope_id = 0;
  457. }
  458. /* Compare addresses exactly.
  459. * v4-mapped-v6 is also in consideration.
  460. */
  461. static int sctp_v6_cmp_addr(const union sctp_addr *addr1,
  462. const union sctp_addr *addr2)
  463. {
  464. if (addr1->sa.sa_family != addr2->sa.sa_family) {
  465. if (addr1->sa.sa_family == AF_INET &&
  466. addr2->sa.sa_family == AF_INET6 &&
  467. ipv6_addr_v4mapped(&addr2->v6.sin6_addr)) {
  468. if (addr2->v6.sin6_port == addr1->v4.sin_port &&
  469. addr2->v6.sin6_addr.s6_addr32[3] ==
  470. addr1->v4.sin_addr.s_addr)
  471. return 1;
  472. }
  473. if (addr2->sa.sa_family == AF_INET &&
  474. addr1->sa.sa_family == AF_INET6 &&
  475. ipv6_addr_v4mapped(&addr1->v6.sin6_addr)) {
  476. if (addr1->v6.sin6_port == addr2->v4.sin_port &&
  477. addr1->v6.sin6_addr.s6_addr32[3] ==
  478. addr2->v4.sin_addr.s_addr)
  479. return 1;
  480. }
  481. return 0;
  482. }
  483. if (addr1->v6.sin6_port != addr2->v6.sin6_port)
  484. return 0;
  485. if (!ipv6_addr_equal(&addr1->v6.sin6_addr, &addr2->v6.sin6_addr))
  486. return 0;
  487. /* If this is a linklocal address, compare the scope_id. */
  488. if (ipv6_addr_type(&addr1->v6.sin6_addr) & IPV6_ADDR_LINKLOCAL) {
  489. if (addr1->v6.sin6_scope_id && addr2->v6.sin6_scope_id &&
  490. (addr1->v6.sin6_scope_id != addr2->v6.sin6_scope_id)) {
  491. return 0;
  492. }
  493. }
  494. return 1;
  495. }
  496. /* Initialize addr struct to INADDR_ANY. */
  497. static void sctp_v6_inaddr_any(union sctp_addr *addr, __be16 port)
  498. {
  499. memset(addr, 0x00, sizeof(union sctp_addr));
  500. addr->v6.sin6_family = AF_INET6;
  501. addr->v6.sin6_port = port;
  502. }
  503. /* Is this a wildcard address? */
  504. static int sctp_v6_is_any(const union sctp_addr *addr)
  505. {
  506. return ipv6_addr_any(&addr->v6.sin6_addr);
  507. }
  508. /* Should this be available for binding? */
  509. static int sctp_v6_available(union sctp_addr *addr, struct sctp_sock *sp)
  510. {
  511. int type;
  512. struct net *net = sock_net(&sp->inet.sk);
  513. const struct in6_addr *in6 = (const struct in6_addr *)&addr->v6.sin6_addr;
  514. type = ipv6_addr_type(in6);
  515. if (IPV6_ADDR_ANY == type)
  516. return 1;
  517. if (type == IPV6_ADDR_MAPPED) {
  518. if (sp && ipv6_only_sock(sctp_opt2sk(sp)))
  519. return 0;
  520. sctp_v6_map_v4(addr);
  521. return sctp_get_af_specific(AF_INET)->available(addr, sp);
  522. }
  523. if (!(type & IPV6_ADDR_UNICAST))
  524. return 0;
  525. return sp->inet.freebind || net->ipv6.sysctl.ip_nonlocal_bind ||
  526. ipv6_chk_addr(net, in6, NULL, 0);
  527. }
  528. /* This function checks if the address is a valid address to be used for
  529. * SCTP.
  530. *
  531. * Output:
  532. * Return 0 - If the address is a non-unicast or an illegal address.
  533. * Return 1 - If the address is a unicast.
  534. */
  535. static int sctp_v6_addr_valid(union sctp_addr *addr,
  536. struct sctp_sock *sp,
  537. const struct sk_buff *skb)
  538. {
  539. int ret = ipv6_addr_type(&addr->v6.sin6_addr);
  540. /* Support v4-mapped-v6 address. */
  541. if (ret == IPV6_ADDR_MAPPED) {
  542. /* Note: This routine is used in input, so v4-mapped-v6
  543. * are disallowed here when there is no sctp_sock.
  544. */
  545. if (sp && ipv6_only_sock(sctp_opt2sk(sp)))
  546. return 0;
  547. sctp_v6_map_v4(addr);
  548. return sctp_get_af_specific(AF_INET)->addr_valid(addr, sp, skb);
  549. }
  550. /* Is this a non-unicast address */
  551. if (!(ret & IPV6_ADDR_UNICAST))
  552. return 0;
  553. return 1;
  554. }
  555. /* What is the scope of 'addr'? */
  556. static enum sctp_scope sctp_v6_scope(union sctp_addr *addr)
  557. {
  558. enum sctp_scope retval;
  559. int v6scope;
  560. /* The IPv6 scope is really a set of bit fields.
  561. * See IFA_* in <net/if_inet6.h>. Map to a generic SCTP scope.
  562. */
  563. v6scope = ipv6_addr_scope(&addr->v6.sin6_addr);
  564. switch (v6scope) {
  565. case IFA_HOST:
  566. retval = SCTP_SCOPE_LOOPBACK;
  567. break;
  568. case IFA_LINK:
  569. retval = SCTP_SCOPE_LINK;
  570. break;
  571. case IFA_SITE:
  572. retval = SCTP_SCOPE_PRIVATE;
  573. break;
  574. default:
  575. retval = SCTP_SCOPE_GLOBAL;
  576. break;
  577. }
  578. return retval;
  579. }
  580. /* Create and initialize a new sk for the socket to be returned by accept(). */
  581. static struct sock *sctp_v6_create_accept_sk(struct sock *sk,
  582. struct sctp_association *asoc,
  583. bool kern)
  584. {
  585. struct sock *newsk;
  586. struct ipv6_pinfo *newnp, *np = inet6_sk(sk);
  587. struct sctp6_sock *newsctp6sk;
  588. struct ipv6_txoptions *opt;
  589. newsk = sk_alloc(sock_net(sk), PF_INET6, GFP_KERNEL, sk->sk_prot, kern);
  590. if (!newsk)
  591. goto out;
  592. sock_init_data(NULL, newsk);
  593. sctp_copy_sock(newsk, sk, asoc);
  594. sock_reset_flag(sk, SOCK_ZAPPED);
  595. newsctp6sk = (struct sctp6_sock *)newsk;
  596. inet_sk(newsk)->pinet6 = &newsctp6sk->inet6;
  597. sctp_sk(newsk)->v4mapped = sctp_sk(sk)->v4mapped;
  598. newnp = inet6_sk(newsk);
  599. memcpy(newnp, np, sizeof(struct ipv6_pinfo));
  600. newnp->ipv6_mc_list = NULL;
  601. newnp->ipv6_ac_list = NULL;
  602. newnp->ipv6_fl_list = NULL;
  603. rcu_read_lock();
  604. opt = rcu_dereference(np->opt);
  605. if (opt)
  606. opt = ipv6_dup_options(newsk, opt);
  607. RCU_INIT_POINTER(newnp->opt, opt);
  608. rcu_read_unlock();
  609. /* Initialize sk's sport, dport, rcv_saddr and daddr for getsockname()
  610. * and getpeername().
  611. */
  612. sctp_v6_to_sk_daddr(&asoc->peer.primary_addr, newsk);
  613. newsk->sk_v6_rcv_saddr = sk->sk_v6_rcv_saddr;
  614. sk_refcnt_debug_inc(newsk);
  615. if (newsk->sk_prot->init(newsk)) {
  616. sk_common_release(newsk);
  617. newsk = NULL;
  618. }
  619. out:
  620. return newsk;
  621. }
  622. /* Format a sockaddr for return to user space. This makes sure the return is
  623. * AF_INET or AF_INET6 depending on the SCTP_I_WANT_MAPPED_V4_ADDR option.
  624. */
  625. static int sctp_v6_addr_to_user(struct sctp_sock *sp, union sctp_addr *addr)
  626. {
  627. if (sp->v4mapped) {
  628. if (addr->sa.sa_family == AF_INET)
  629. sctp_v4_map_v6(addr);
  630. } else {
  631. if (addr->sa.sa_family == AF_INET6 &&
  632. ipv6_addr_v4mapped(&addr->v6.sin6_addr))
  633. sctp_v6_map_v4(addr);
  634. }
  635. if (addr->sa.sa_family == AF_INET)
  636. return sizeof(struct sockaddr_in);
  637. return sizeof(struct sockaddr_in6);
  638. }
  639. /* Where did this skb come from? */
  640. static int sctp_v6_skb_iif(const struct sk_buff *skb)
  641. {
  642. return IP6CB(skb)->iif;
  643. }
  644. /* Was this packet marked by Explicit Congestion Notification? */
  645. static int sctp_v6_is_ce(const struct sk_buff *skb)
  646. {
  647. return *((__u32 *)(ipv6_hdr(skb))) & (__force __u32)htonl(1 << 20);
  648. }
  649. /* Dump the v6 addr to the seq file. */
  650. static void sctp_v6_seq_dump_addr(struct seq_file *seq, union sctp_addr *addr)
  651. {
  652. seq_printf(seq, "%pI6 ", &addr->v6.sin6_addr);
  653. }
  654. static void sctp_v6_ecn_capable(struct sock *sk)
  655. {
  656. inet6_sk(sk)->tclass |= INET_ECN_ECT_0;
  657. }
  658. /* Initialize a PF_INET msgname from a ulpevent. */
  659. static void sctp_inet6_event_msgname(struct sctp_ulpevent *event,
  660. char *msgname, int *addrlen)
  661. {
  662. union sctp_addr *addr;
  663. struct sctp_association *asoc;
  664. union sctp_addr *paddr;
  665. if (!msgname)
  666. return;
  667. addr = (union sctp_addr *)msgname;
  668. asoc = event->asoc;
  669. paddr = &asoc->peer.primary_addr;
  670. if (paddr->sa.sa_family == AF_INET) {
  671. addr->v4.sin_family = AF_INET;
  672. addr->v4.sin_port = htons(asoc->peer.port);
  673. addr->v4.sin_addr = paddr->v4.sin_addr;
  674. } else {
  675. addr->v6.sin6_family = AF_INET6;
  676. addr->v6.sin6_flowinfo = 0;
  677. if (ipv6_addr_type(&paddr->v6.sin6_addr) & IPV6_ADDR_LINKLOCAL)
  678. addr->v6.sin6_scope_id = paddr->v6.sin6_scope_id;
  679. else
  680. addr->v6.sin6_scope_id = 0;
  681. addr->v6.sin6_port = htons(asoc->peer.port);
  682. addr->v6.sin6_addr = paddr->v6.sin6_addr;
  683. }
  684. *addrlen = sctp_v6_addr_to_user(sctp_sk(asoc->base.sk), addr);
  685. }
  686. /* Initialize a msg_name from an inbound skb. */
  687. static void sctp_inet6_skb_msgname(struct sk_buff *skb, char *msgname,
  688. int *addr_len)
  689. {
  690. union sctp_addr *addr;
  691. struct sctphdr *sh;
  692. if (!msgname)
  693. return;
  694. addr = (union sctp_addr *)msgname;
  695. sh = sctp_hdr(skb);
  696. if (ip_hdr(skb)->version == 4) {
  697. addr->v4.sin_family = AF_INET;
  698. addr->v4.sin_port = sh->source;
  699. addr->v4.sin_addr.s_addr = ip_hdr(skb)->saddr;
  700. } else {
  701. addr->v6.sin6_family = AF_INET6;
  702. addr->v6.sin6_flowinfo = 0;
  703. addr->v6.sin6_port = sh->source;
  704. addr->v6.sin6_addr = ipv6_hdr(skb)->saddr;
  705. if (ipv6_addr_type(&addr->v6.sin6_addr) & IPV6_ADDR_LINKLOCAL) {
  706. addr->v6.sin6_scope_id = sctp_v6_skb_iif(skb);
  707. }
  708. }
  709. *addr_len = sctp_v6_addr_to_user(sctp_sk(skb->sk), addr);
  710. }
  711. /* Do we support this AF? */
  712. static int sctp_inet6_af_supported(sa_family_t family, struct sctp_sock *sp)
  713. {
  714. switch (family) {
  715. case AF_INET6:
  716. return 1;
  717. /* v4-mapped-v6 addresses */
  718. case AF_INET:
  719. if (!__ipv6_only_sock(sctp_opt2sk(sp)))
  720. return 1;
  721. default:
  722. return 0;
  723. }
  724. }
  725. /* Address matching with wildcards allowed. This extra level
  726. * of indirection lets us choose whether a PF_INET6 should
  727. * disallow any v4 addresses if we so choose.
  728. */
  729. static int sctp_inet6_cmp_addr(const union sctp_addr *addr1,
  730. const union sctp_addr *addr2,
  731. struct sctp_sock *opt)
  732. {
  733. struct sctp_af *af1, *af2;
  734. struct sock *sk = sctp_opt2sk(opt);
  735. af1 = sctp_get_af_specific(addr1->sa.sa_family);
  736. af2 = sctp_get_af_specific(addr2->sa.sa_family);
  737. if (!af1 || !af2)
  738. return 0;
  739. /* If the socket is IPv6 only, v4 addrs will not match */
  740. if (__ipv6_only_sock(sk) && af1 != af2)
  741. return 0;
  742. /* Today, wildcard AF_INET/AF_INET6. */
  743. if (sctp_is_any(sk, addr1) || sctp_is_any(sk, addr2))
  744. return 1;
  745. if (addr1->sa.sa_family != addr2->sa.sa_family)
  746. return 0;
  747. return af1->cmp_addr(addr1, addr2);
  748. }
  749. /* Verify that the provided sockaddr looks bindable. Common verification,
  750. * has already been taken care of.
  751. */
  752. static int sctp_inet6_bind_verify(struct sctp_sock *opt, union sctp_addr *addr)
  753. {
  754. struct sctp_af *af;
  755. /* ASSERT: address family has already been verified. */
  756. if (addr->sa.sa_family != AF_INET6)
  757. af = sctp_get_af_specific(addr->sa.sa_family);
  758. else {
  759. int type = ipv6_addr_type(&addr->v6.sin6_addr);
  760. struct net_device *dev;
  761. if (type & IPV6_ADDR_LINKLOCAL) {
  762. struct net *net;
  763. if (!addr->v6.sin6_scope_id)
  764. return 0;
  765. net = sock_net(&opt->inet.sk);
  766. rcu_read_lock();
  767. dev = dev_get_by_index_rcu(net, addr->v6.sin6_scope_id);
  768. if (!dev || !(opt->inet.freebind ||
  769. net->ipv6.sysctl.ip_nonlocal_bind ||
  770. ipv6_chk_addr(net, &addr->v6.sin6_addr,
  771. dev, 0))) {
  772. rcu_read_unlock();
  773. return 0;
  774. }
  775. rcu_read_unlock();
  776. }
  777. af = opt->pf->af;
  778. }
  779. return af->available(addr, opt);
  780. }
  781. /* Verify that the provided sockaddr looks sendable. Common verification,
  782. * has already been taken care of.
  783. */
  784. static int sctp_inet6_send_verify(struct sctp_sock *opt, union sctp_addr *addr)
  785. {
  786. struct sctp_af *af = NULL;
  787. /* ASSERT: address family has already been verified. */
  788. if (addr->sa.sa_family != AF_INET6)
  789. af = sctp_get_af_specific(addr->sa.sa_family);
  790. else {
  791. int type = ipv6_addr_type(&addr->v6.sin6_addr);
  792. struct net_device *dev;
  793. if (type & IPV6_ADDR_LINKLOCAL) {
  794. if (!addr->v6.sin6_scope_id)
  795. return 0;
  796. rcu_read_lock();
  797. dev = dev_get_by_index_rcu(sock_net(&opt->inet.sk),
  798. addr->v6.sin6_scope_id);
  799. rcu_read_unlock();
  800. if (!dev)
  801. return 0;
  802. }
  803. af = opt->pf->af;
  804. }
  805. return af != NULL;
  806. }
  807. /* Fill in Supported Address Type information for INIT and INIT-ACK
  808. * chunks. Note: In the future, we may want to look at sock options
  809. * to determine whether a PF_INET6 socket really wants to have IPV4
  810. * addresses.
  811. * Returns number of addresses supported.
  812. */
  813. static int sctp_inet6_supported_addrs(const struct sctp_sock *opt,
  814. __be16 *types)
  815. {
  816. types[0] = SCTP_PARAM_IPV6_ADDRESS;
  817. if (!opt || !ipv6_only_sock(sctp_opt2sk(opt))) {
  818. types[1] = SCTP_PARAM_IPV4_ADDRESS;
  819. return 2;
  820. }
  821. return 1;
  822. }
  823. /* Handle SCTP_I_WANT_MAPPED_V4_ADDR for getpeername() and getsockname() */
  824. static int sctp_getname(struct socket *sock, struct sockaddr *uaddr,
  825. int *uaddr_len, int peer)
  826. {
  827. int rc;
  828. rc = inet6_getname(sock, uaddr, uaddr_len, peer);
  829. if (rc != 0)
  830. return rc;
  831. *uaddr_len = sctp_v6_addr_to_user(sctp_sk(sock->sk),
  832. (union sctp_addr *)uaddr);
  833. return rc;
  834. }
  835. static const struct proto_ops inet6_seqpacket_ops = {
  836. .family = PF_INET6,
  837. .owner = THIS_MODULE,
  838. .release = inet6_release,
  839. .bind = inet6_bind,
  840. .connect = inet_dgram_connect,
  841. .socketpair = sock_no_socketpair,
  842. .accept = inet_accept,
  843. .getname = sctp_getname,
  844. .poll = sctp_poll,
  845. .ioctl = inet6_ioctl,
  846. .listen = sctp_inet_listen,
  847. .shutdown = inet_shutdown,
  848. .setsockopt = sock_common_setsockopt,
  849. .getsockopt = sock_common_getsockopt,
  850. .sendmsg = inet_sendmsg,
  851. .recvmsg = inet_recvmsg,
  852. .mmap = sock_no_mmap,
  853. #ifdef CONFIG_COMPAT
  854. .compat_setsockopt = compat_sock_common_setsockopt,
  855. .compat_getsockopt = compat_sock_common_getsockopt,
  856. #endif
  857. };
  858. static struct inet_protosw sctpv6_seqpacket_protosw = {
  859. .type = SOCK_SEQPACKET,
  860. .protocol = IPPROTO_SCTP,
  861. .prot = &sctpv6_prot,
  862. .ops = &inet6_seqpacket_ops,
  863. .flags = SCTP_PROTOSW_FLAG
  864. };
  865. static struct inet_protosw sctpv6_stream_protosw = {
  866. .type = SOCK_STREAM,
  867. .protocol = IPPROTO_SCTP,
  868. .prot = &sctpv6_prot,
  869. .ops = &inet6_seqpacket_ops,
  870. .flags = SCTP_PROTOSW_FLAG,
  871. };
  872. static int sctp6_rcv(struct sk_buff *skb)
  873. {
  874. return sctp_rcv(skb) ? -1 : 0;
  875. }
  876. static const struct inet6_protocol sctpv6_protocol = {
  877. .handler = sctp6_rcv,
  878. .err_handler = sctp_v6_err,
  879. .flags = INET6_PROTO_NOPOLICY | INET6_PROTO_FINAL,
  880. };
  881. static struct sctp_af sctp_af_inet6 = {
  882. .sa_family = AF_INET6,
  883. .sctp_xmit = sctp_v6_xmit,
  884. .setsockopt = ipv6_setsockopt,
  885. .getsockopt = ipv6_getsockopt,
  886. .get_dst = sctp_v6_get_dst,
  887. .get_saddr = sctp_v6_get_saddr,
  888. .copy_addrlist = sctp_v6_copy_addrlist,
  889. .from_skb = sctp_v6_from_skb,
  890. .from_sk = sctp_v6_from_sk,
  891. .from_addr_param = sctp_v6_from_addr_param,
  892. .to_addr_param = sctp_v6_to_addr_param,
  893. .cmp_addr = sctp_v6_cmp_addr,
  894. .scope = sctp_v6_scope,
  895. .addr_valid = sctp_v6_addr_valid,
  896. .inaddr_any = sctp_v6_inaddr_any,
  897. .is_any = sctp_v6_is_any,
  898. .available = sctp_v6_available,
  899. .skb_iif = sctp_v6_skb_iif,
  900. .is_ce = sctp_v6_is_ce,
  901. .seq_dump_addr = sctp_v6_seq_dump_addr,
  902. .ecn_capable = sctp_v6_ecn_capable,
  903. .net_header_len = sizeof(struct ipv6hdr),
  904. .sockaddr_len = sizeof(struct sockaddr_in6),
  905. #ifdef CONFIG_COMPAT
  906. .compat_setsockopt = compat_ipv6_setsockopt,
  907. .compat_getsockopt = compat_ipv6_getsockopt,
  908. #endif
  909. };
  910. static struct sctp_pf sctp_pf_inet6 = {
  911. .event_msgname = sctp_inet6_event_msgname,
  912. .skb_msgname = sctp_inet6_skb_msgname,
  913. .af_supported = sctp_inet6_af_supported,
  914. .cmp_addr = sctp_inet6_cmp_addr,
  915. .bind_verify = sctp_inet6_bind_verify,
  916. .send_verify = sctp_inet6_send_verify,
  917. .supported_addrs = sctp_inet6_supported_addrs,
  918. .create_accept_sk = sctp_v6_create_accept_sk,
  919. .addr_to_user = sctp_v6_addr_to_user,
  920. .to_sk_saddr = sctp_v6_to_sk_saddr,
  921. .to_sk_daddr = sctp_v6_to_sk_daddr,
  922. .af = &sctp_af_inet6,
  923. };
  924. /* Initialize IPv6 support and register with socket layer. */
  925. void sctp_v6_pf_init(void)
  926. {
  927. /* Register the SCTP specific PF_INET6 functions. */
  928. sctp_register_pf(&sctp_pf_inet6, PF_INET6);
  929. /* Register the SCTP specific AF_INET6 functions. */
  930. sctp_register_af(&sctp_af_inet6);
  931. }
  932. void sctp_v6_pf_exit(void)
  933. {
  934. list_del(&sctp_af_inet6.list);
  935. }
  936. /* Initialize IPv6 support and register with socket layer. */
  937. int sctp_v6_protosw_init(void)
  938. {
  939. int rc;
  940. rc = proto_register(&sctpv6_prot, 1);
  941. if (rc)
  942. return rc;
  943. /* Add SCTPv6(UDP and TCP style) to inetsw6 linked list. */
  944. inet6_register_protosw(&sctpv6_seqpacket_protosw);
  945. inet6_register_protosw(&sctpv6_stream_protosw);
  946. return 0;
  947. }
  948. void sctp_v6_protosw_exit(void)
  949. {
  950. inet6_unregister_protosw(&sctpv6_seqpacket_protosw);
  951. inet6_unregister_protosw(&sctpv6_stream_protosw);
  952. proto_unregister(&sctpv6_prot);
  953. }
  954. /* Register with inet6 layer. */
  955. int sctp_v6_add_protocol(void)
  956. {
  957. /* Register notifier for inet6 address additions/deletions. */
  958. register_inet6addr_notifier(&sctp_inet6addr_notifier);
  959. if (inet6_add_protocol(&sctpv6_protocol, IPPROTO_SCTP) < 0)
  960. return -EAGAIN;
  961. return 0;
  962. }
  963. /* Unregister with inet6 layer. */
  964. void sctp_v6_del_protocol(void)
  965. {
  966. inet6_del_protocol(&sctpv6_protocol, IPPROTO_SCTP);
  967. unregister_inet6addr_notifier(&sctp_inet6addr_notifier);
  968. }