ipv6.c 30 KB

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