ipv6.c 28 KB

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