af_mpls.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619
  1. #include <linux/types.h>
  2. #include <linux/skbuff.h>
  3. #include <linux/socket.h>
  4. #include <linux/sysctl.h>
  5. #include <linux/net.h>
  6. #include <linux/module.h>
  7. #include <linux/if_arp.h>
  8. #include <linux/ipv6.h>
  9. #include <linux/mpls.h>
  10. #include <linux/vmalloc.h>
  11. #include <net/ip.h>
  12. #include <net/dst.h>
  13. #include <net/sock.h>
  14. #include <net/arp.h>
  15. #include <net/ip_fib.h>
  16. #include <net/netevent.h>
  17. #include <net/netns/generic.h>
  18. #if IS_ENABLED(CONFIG_IPV6)
  19. #include <net/ipv6.h>
  20. #include <net/addrconf.h>
  21. #endif
  22. #include <net/nexthop.h>
  23. #include "internal.h"
  24. /* Maximum number of labels to look ahead at when selecting a path of
  25. * a multipath route
  26. */
  27. #define MAX_MP_SELECT_LABELS 4
  28. #define MPLS_NEIGH_TABLE_UNSPEC (NEIGH_LINK_TABLE + 1)
  29. static int zero = 0;
  30. static int label_limit = (1 << 20) - 1;
  31. static void rtmsg_lfib(int event, u32 label, struct mpls_route *rt,
  32. struct nlmsghdr *nlh, struct net *net, u32 portid,
  33. unsigned int nlm_flags);
  34. static struct mpls_route *mpls_route_input_rcu(struct net *net, unsigned index)
  35. {
  36. struct mpls_route *rt = NULL;
  37. if (index < net->mpls.platform_labels) {
  38. struct mpls_route __rcu **platform_label =
  39. rcu_dereference(net->mpls.platform_label);
  40. rt = rcu_dereference(platform_label[index]);
  41. }
  42. return rt;
  43. }
  44. static inline struct mpls_dev *mpls_dev_get(const struct net_device *dev)
  45. {
  46. return rcu_dereference_rtnl(dev->mpls_ptr);
  47. }
  48. bool mpls_output_possible(const struct net_device *dev)
  49. {
  50. return dev && (dev->flags & IFF_UP) && netif_carrier_ok(dev);
  51. }
  52. EXPORT_SYMBOL_GPL(mpls_output_possible);
  53. static u8 *__mpls_nh_via(struct mpls_route *rt, struct mpls_nh *nh)
  54. {
  55. u8 *nh0_via = PTR_ALIGN((u8 *)&rt->rt_nh[rt->rt_nhn], VIA_ALEN_ALIGN);
  56. int nh_index = nh - rt->rt_nh;
  57. return nh0_via + rt->rt_max_alen * nh_index;
  58. }
  59. static const u8 *mpls_nh_via(const struct mpls_route *rt,
  60. const struct mpls_nh *nh)
  61. {
  62. return __mpls_nh_via((struct mpls_route *)rt, (struct mpls_nh *)nh);
  63. }
  64. static unsigned int mpls_nh_header_size(const struct mpls_nh *nh)
  65. {
  66. /* The size of the layer 2.5 labels to be added for this route */
  67. return nh->nh_labels * sizeof(struct mpls_shim_hdr);
  68. }
  69. unsigned int mpls_dev_mtu(const struct net_device *dev)
  70. {
  71. /* The amount of data the layer 2 frame can hold */
  72. return dev->mtu;
  73. }
  74. EXPORT_SYMBOL_GPL(mpls_dev_mtu);
  75. bool mpls_pkt_too_big(const struct sk_buff *skb, unsigned int mtu)
  76. {
  77. if (skb->len <= mtu)
  78. return false;
  79. if (skb_is_gso(skb) && skb_gso_network_seglen(skb) <= mtu)
  80. return false;
  81. return true;
  82. }
  83. EXPORT_SYMBOL_GPL(mpls_pkt_too_big);
  84. static struct mpls_nh *mpls_select_multipath(struct mpls_route *rt,
  85. struct sk_buff *skb, bool bos)
  86. {
  87. struct mpls_entry_decoded dec;
  88. struct mpls_shim_hdr *hdr;
  89. bool eli_seen = false;
  90. int label_index;
  91. int nh_index = 0;
  92. u32 hash = 0;
  93. /* No need to look further into packet if there's only
  94. * one path
  95. */
  96. if (rt->rt_nhn == 1)
  97. goto out;
  98. for (label_index = 0; label_index < MAX_MP_SELECT_LABELS && !bos;
  99. label_index++) {
  100. if (!pskb_may_pull(skb, sizeof(*hdr) * label_index))
  101. break;
  102. /* Read and decode the current label */
  103. hdr = mpls_hdr(skb) + label_index;
  104. dec = mpls_entry_decode(hdr);
  105. /* RFC6790 - reserved labels MUST NOT be used as keys
  106. * for the load-balancing function
  107. */
  108. if (likely(dec.label >= MPLS_LABEL_FIRST_UNRESERVED)) {
  109. hash = jhash_1word(dec.label, hash);
  110. /* The entropy label follows the entropy label
  111. * indicator, so this means that the entropy
  112. * label was just added to the hash - no need to
  113. * go any deeper either in the label stack or in the
  114. * payload
  115. */
  116. if (eli_seen)
  117. break;
  118. } else if (dec.label == MPLS_LABEL_ENTROPY) {
  119. eli_seen = true;
  120. }
  121. bos = dec.bos;
  122. if (bos && pskb_may_pull(skb, sizeof(*hdr) * label_index +
  123. sizeof(struct iphdr))) {
  124. const struct iphdr *v4hdr;
  125. v4hdr = (const struct iphdr *)(mpls_hdr(skb) +
  126. label_index);
  127. if (v4hdr->version == 4) {
  128. hash = jhash_3words(ntohl(v4hdr->saddr),
  129. ntohl(v4hdr->daddr),
  130. v4hdr->protocol, hash);
  131. } else if (v4hdr->version == 6 &&
  132. pskb_may_pull(skb, sizeof(*hdr) * label_index +
  133. sizeof(struct ipv6hdr))) {
  134. const struct ipv6hdr *v6hdr;
  135. v6hdr = (const struct ipv6hdr *)(mpls_hdr(skb) +
  136. label_index);
  137. hash = __ipv6_addr_jhash(&v6hdr->saddr, hash);
  138. hash = __ipv6_addr_jhash(&v6hdr->daddr, hash);
  139. hash = jhash_1word(v6hdr->nexthdr, hash);
  140. }
  141. }
  142. }
  143. nh_index = hash % rt->rt_nhn;
  144. out:
  145. return &rt->rt_nh[nh_index];
  146. }
  147. static bool mpls_egress(struct mpls_route *rt, struct sk_buff *skb,
  148. struct mpls_entry_decoded dec)
  149. {
  150. enum mpls_payload_type payload_type;
  151. bool success = false;
  152. /* The IPv4 code below accesses through the IPv4 header
  153. * checksum, which is 12 bytes into the packet.
  154. * The IPv6 code below accesses through the IPv6 hop limit
  155. * which is 8 bytes into the packet.
  156. *
  157. * For all supported cases there should always be at least 12
  158. * bytes of packet data present. The IPv4 header is 20 bytes
  159. * without options and the IPv6 header is always 40 bytes
  160. * long.
  161. */
  162. if (!pskb_may_pull(skb, 12))
  163. return false;
  164. payload_type = rt->rt_payload_type;
  165. if (payload_type == MPT_UNSPEC)
  166. payload_type = ip_hdr(skb)->version;
  167. switch (payload_type) {
  168. case MPT_IPV4: {
  169. struct iphdr *hdr4 = ip_hdr(skb);
  170. skb->protocol = htons(ETH_P_IP);
  171. csum_replace2(&hdr4->check,
  172. htons(hdr4->ttl << 8),
  173. htons(dec.ttl << 8));
  174. hdr4->ttl = dec.ttl;
  175. success = true;
  176. break;
  177. }
  178. case MPT_IPV6: {
  179. struct ipv6hdr *hdr6 = ipv6_hdr(skb);
  180. skb->protocol = htons(ETH_P_IPV6);
  181. hdr6->hop_limit = dec.ttl;
  182. success = true;
  183. break;
  184. }
  185. case MPT_UNSPEC:
  186. break;
  187. }
  188. return success;
  189. }
  190. static int mpls_forward(struct sk_buff *skb, struct net_device *dev,
  191. struct packet_type *pt, struct net_device *orig_dev)
  192. {
  193. struct net *net = dev_net(dev);
  194. struct mpls_shim_hdr *hdr;
  195. struct mpls_route *rt;
  196. struct mpls_nh *nh;
  197. struct mpls_entry_decoded dec;
  198. struct net_device *out_dev;
  199. struct mpls_dev *mdev;
  200. unsigned int hh_len;
  201. unsigned int new_header_size;
  202. unsigned int mtu;
  203. int err;
  204. /* Careful this entire function runs inside of an rcu critical section */
  205. mdev = mpls_dev_get(dev);
  206. if (!mdev || !mdev->input_enabled)
  207. goto drop;
  208. if (skb->pkt_type != PACKET_HOST)
  209. goto drop;
  210. if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL)
  211. goto drop;
  212. if (!pskb_may_pull(skb, sizeof(*hdr)))
  213. goto drop;
  214. /* Read and decode the label */
  215. hdr = mpls_hdr(skb);
  216. dec = mpls_entry_decode(hdr);
  217. /* Pop the label */
  218. skb_pull(skb, sizeof(*hdr));
  219. skb_reset_network_header(skb);
  220. skb_orphan(skb);
  221. rt = mpls_route_input_rcu(net, dec.label);
  222. if (!rt)
  223. goto drop;
  224. nh = mpls_select_multipath(rt, skb, dec.bos);
  225. if (!nh)
  226. goto drop;
  227. /* Find the output device */
  228. out_dev = rcu_dereference(nh->nh_dev);
  229. if (!mpls_output_possible(out_dev))
  230. goto drop;
  231. if (skb_warn_if_lro(skb))
  232. goto drop;
  233. skb_forward_csum(skb);
  234. /* Verify ttl is valid */
  235. if (dec.ttl <= 1)
  236. goto drop;
  237. dec.ttl -= 1;
  238. /* Verify the destination can hold the packet */
  239. new_header_size = mpls_nh_header_size(nh);
  240. mtu = mpls_dev_mtu(out_dev);
  241. if (mpls_pkt_too_big(skb, mtu - new_header_size))
  242. goto drop;
  243. hh_len = LL_RESERVED_SPACE(out_dev);
  244. if (!out_dev->header_ops)
  245. hh_len = 0;
  246. /* Ensure there is enough space for the headers in the skb */
  247. if (skb_cow(skb, hh_len + new_header_size))
  248. goto drop;
  249. skb->dev = out_dev;
  250. skb->protocol = htons(ETH_P_MPLS_UC);
  251. if (unlikely(!new_header_size && dec.bos)) {
  252. /* Penultimate hop popping */
  253. if (!mpls_egress(rt, skb, dec))
  254. goto drop;
  255. } else {
  256. bool bos;
  257. int i;
  258. skb_push(skb, new_header_size);
  259. skb_reset_network_header(skb);
  260. /* Push the new labels */
  261. hdr = mpls_hdr(skb);
  262. bos = dec.bos;
  263. for (i = nh->nh_labels - 1; i >= 0; i--) {
  264. hdr[i] = mpls_entry_encode(nh->nh_label[i],
  265. dec.ttl, 0, bos);
  266. bos = false;
  267. }
  268. }
  269. /* If via wasn't specified then send out using device address */
  270. if (nh->nh_via_table == MPLS_NEIGH_TABLE_UNSPEC)
  271. err = neigh_xmit(NEIGH_LINK_TABLE, out_dev,
  272. out_dev->dev_addr, skb);
  273. else
  274. err = neigh_xmit(nh->nh_via_table, out_dev,
  275. mpls_nh_via(rt, nh), skb);
  276. if (err)
  277. net_dbg_ratelimited("%s: packet transmission failed: %d\n",
  278. __func__, err);
  279. return 0;
  280. drop:
  281. kfree_skb(skb);
  282. return NET_RX_DROP;
  283. }
  284. static struct packet_type mpls_packet_type __read_mostly = {
  285. .type = cpu_to_be16(ETH_P_MPLS_UC),
  286. .func = mpls_forward,
  287. };
  288. static const struct nla_policy rtm_mpls_policy[RTA_MAX+1] = {
  289. [RTA_DST] = { .type = NLA_U32 },
  290. [RTA_OIF] = { .type = NLA_U32 },
  291. };
  292. struct mpls_route_config {
  293. u32 rc_protocol;
  294. u32 rc_ifindex;
  295. u8 rc_via_table;
  296. u8 rc_via_alen;
  297. u8 rc_via[MAX_VIA_ALEN];
  298. u32 rc_label;
  299. u8 rc_output_labels;
  300. u32 rc_output_label[MAX_NEW_LABELS];
  301. u32 rc_nlflags;
  302. enum mpls_payload_type rc_payload_type;
  303. struct nl_info rc_nlinfo;
  304. struct rtnexthop *rc_mp;
  305. int rc_mp_len;
  306. };
  307. static struct mpls_route *mpls_rt_alloc(int num_nh, u8 max_alen)
  308. {
  309. u8 max_alen_aligned = ALIGN(max_alen, VIA_ALEN_ALIGN);
  310. struct mpls_route *rt;
  311. rt = kzalloc(ALIGN(sizeof(*rt) + num_nh * sizeof(*rt->rt_nh),
  312. VIA_ALEN_ALIGN) +
  313. num_nh * max_alen_aligned,
  314. GFP_KERNEL);
  315. if (rt) {
  316. rt->rt_nhn = num_nh;
  317. rt->rt_max_alen = max_alen_aligned;
  318. }
  319. return rt;
  320. }
  321. static void mpls_rt_free(struct mpls_route *rt)
  322. {
  323. if (rt)
  324. kfree_rcu(rt, rt_rcu);
  325. }
  326. static void mpls_notify_route(struct net *net, unsigned index,
  327. struct mpls_route *old, struct mpls_route *new,
  328. const struct nl_info *info)
  329. {
  330. struct nlmsghdr *nlh = info ? info->nlh : NULL;
  331. unsigned portid = info ? info->portid : 0;
  332. int event = new ? RTM_NEWROUTE : RTM_DELROUTE;
  333. struct mpls_route *rt = new ? new : old;
  334. unsigned nlm_flags = (old && new) ? NLM_F_REPLACE : 0;
  335. /* Ignore reserved labels for now */
  336. if (rt && (index >= MPLS_LABEL_FIRST_UNRESERVED))
  337. rtmsg_lfib(event, index, rt, nlh, net, portid, nlm_flags);
  338. }
  339. static void mpls_route_update(struct net *net, unsigned index,
  340. struct mpls_route *new,
  341. const struct nl_info *info)
  342. {
  343. struct mpls_route __rcu **platform_label;
  344. struct mpls_route *rt;
  345. ASSERT_RTNL();
  346. platform_label = rtnl_dereference(net->mpls.platform_label);
  347. rt = rtnl_dereference(platform_label[index]);
  348. rcu_assign_pointer(platform_label[index], new);
  349. mpls_notify_route(net, index, rt, new, info);
  350. /* If we removed a route free it now */
  351. mpls_rt_free(rt);
  352. }
  353. static unsigned find_free_label(struct net *net)
  354. {
  355. struct mpls_route __rcu **platform_label;
  356. size_t platform_labels;
  357. unsigned index;
  358. platform_label = rtnl_dereference(net->mpls.platform_label);
  359. platform_labels = net->mpls.platform_labels;
  360. for (index = MPLS_LABEL_FIRST_UNRESERVED; index < platform_labels;
  361. index++) {
  362. if (!rtnl_dereference(platform_label[index]))
  363. return index;
  364. }
  365. return LABEL_NOT_SPECIFIED;
  366. }
  367. #if IS_ENABLED(CONFIG_INET)
  368. static struct net_device *inet_fib_lookup_dev(struct net *net,
  369. const void *addr)
  370. {
  371. struct net_device *dev;
  372. struct rtable *rt;
  373. struct in_addr daddr;
  374. memcpy(&daddr, addr, sizeof(struct in_addr));
  375. rt = ip_route_output(net, daddr.s_addr, 0, 0, 0);
  376. if (IS_ERR(rt))
  377. return ERR_CAST(rt);
  378. dev = rt->dst.dev;
  379. dev_hold(dev);
  380. ip_rt_put(rt);
  381. return dev;
  382. }
  383. #else
  384. static struct net_device *inet_fib_lookup_dev(struct net *net,
  385. const void *addr)
  386. {
  387. return ERR_PTR(-EAFNOSUPPORT);
  388. }
  389. #endif
  390. #if IS_ENABLED(CONFIG_IPV6)
  391. static struct net_device *inet6_fib_lookup_dev(struct net *net,
  392. const void *addr)
  393. {
  394. struct net_device *dev;
  395. struct dst_entry *dst;
  396. struct flowi6 fl6;
  397. int err;
  398. if (!ipv6_stub)
  399. return ERR_PTR(-EAFNOSUPPORT);
  400. memset(&fl6, 0, sizeof(fl6));
  401. memcpy(&fl6.daddr, addr, sizeof(struct in6_addr));
  402. err = ipv6_stub->ipv6_dst_lookup(net, NULL, &dst, &fl6);
  403. if (err)
  404. return ERR_PTR(err);
  405. dev = dst->dev;
  406. dev_hold(dev);
  407. dst_release(dst);
  408. return dev;
  409. }
  410. #else
  411. static struct net_device *inet6_fib_lookup_dev(struct net *net,
  412. const void *addr)
  413. {
  414. return ERR_PTR(-EAFNOSUPPORT);
  415. }
  416. #endif
  417. static struct net_device *find_outdev(struct net *net,
  418. struct mpls_route *rt,
  419. struct mpls_nh *nh, int oif)
  420. {
  421. struct net_device *dev = NULL;
  422. if (!oif) {
  423. switch (nh->nh_via_table) {
  424. case NEIGH_ARP_TABLE:
  425. dev = inet_fib_lookup_dev(net, mpls_nh_via(rt, nh));
  426. break;
  427. case NEIGH_ND_TABLE:
  428. dev = inet6_fib_lookup_dev(net, mpls_nh_via(rt, nh));
  429. break;
  430. case NEIGH_LINK_TABLE:
  431. break;
  432. }
  433. } else {
  434. dev = dev_get_by_index(net, oif);
  435. }
  436. if (!dev)
  437. return ERR_PTR(-ENODEV);
  438. /* The caller is holding rtnl anyways, so release the dev reference */
  439. dev_put(dev);
  440. return dev;
  441. }
  442. static int mpls_nh_assign_dev(struct net *net, struct mpls_route *rt,
  443. struct mpls_nh *nh, int oif)
  444. {
  445. struct net_device *dev = NULL;
  446. int err = -ENODEV;
  447. dev = find_outdev(net, rt, nh, oif);
  448. if (IS_ERR(dev)) {
  449. err = PTR_ERR(dev);
  450. dev = NULL;
  451. goto errout;
  452. }
  453. /* Ensure this is a supported device */
  454. err = -EINVAL;
  455. if (!mpls_dev_get(dev))
  456. goto errout;
  457. if ((nh->nh_via_table == NEIGH_LINK_TABLE) &&
  458. (dev->addr_len != nh->nh_via_alen))
  459. goto errout;
  460. RCU_INIT_POINTER(nh->nh_dev, dev);
  461. return 0;
  462. errout:
  463. return err;
  464. }
  465. static int mpls_nh_build_from_cfg(struct mpls_route_config *cfg,
  466. struct mpls_route *rt)
  467. {
  468. struct net *net = cfg->rc_nlinfo.nl_net;
  469. struct mpls_nh *nh = rt->rt_nh;
  470. int err;
  471. int i;
  472. if (!nh)
  473. return -ENOMEM;
  474. err = -EINVAL;
  475. /* Ensure only a supported number of labels are present */
  476. if (cfg->rc_output_labels > MAX_NEW_LABELS)
  477. goto errout;
  478. nh->nh_labels = cfg->rc_output_labels;
  479. for (i = 0; i < nh->nh_labels; i++)
  480. nh->nh_label[i] = cfg->rc_output_label[i];
  481. nh->nh_via_table = cfg->rc_via_table;
  482. memcpy(__mpls_nh_via(rt, nh), cfg->rc_via, cfg->rc_via_alen);
  483. nh->nh_via_alen = cfg->rc_via_alen;
  484. err = mpls_nh_assign_dev(net, rt, nh, cfg->rc_ifindex);
  485. if (err)
  486. goto errout;
  487. return 0;
  488. errout:
  489. return err;
  490. }
  491. static int mpls_nh_build(struct net *net, struct mpls_route *rt,
  492. struct mpls_nh *nh, int oif,
  493. struct nlattr *via, struct nlattr *newdst)
  494. {
  495. int err = -ENOMEM;
  496. if (!nh)
  497. goto errout;
  498. if (newdst) {
  499. err = nla_get_labels(newdst, MAX_NEW_LABELS,
  500. &nh->nh_labels, nh->nh_label);
  501. if (err)
  502. goto errout;
  503. }
  504. if (via) {
  505. err = nla_get_via(via, &nh->nh_via_alen, &nh->nh_via_table,
  506. __mpls_nh_via(rt, nh));
  507. if (err)
  508. goto errout;
  509. } else {
  510. nh->nh_via_table = MPLS_NEIGH_TABLE_UNSPEC;
  511. }
  512. err = mpls_nh_assign_dev(net, rt, nh, oif);
  513. if (err)
  514. goto errout;
  515. return 0;
  516. errout:
  517. return err;
  518. }
  519. static int mpls_count_nexthops(struct rtnexthop *rtnh, int len,
  520. u8 cfg_via_alen, u8 *max_via_alen)
  521. {
  522. int nhs = 0;
  523. int remaining = len;
  524. if (!rtnh) {
  525. *max_via_alen = cfg_via_alen;
  526. return 1;
  527. }
  528. *max_via_alen = 0;
  529. while (rtnh_ok(rtnh, remaining)) {
  530. struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
  531. int attrlen;
  532. attrlen = rtnh_attrlen(rtnh);
  533. nla = nla_find(attrs, attrlen, RTA_VIA);
  534. if (nla && nla_len(nla) >=
  535. offsetof(struct rtvia, rtvia_addr)) {
  536. int via_alen = nla_len(nla) -
  537. offsetof(struct rtvia, rtvia_addr);
  538. if (via_alen <= MAX_VIA_ALEN)
  539. *max_via_alen = max_t(u16, *max_via_alen,
  540. via_alen);
  541. }
  542. nhs++;
  543. rtnh = rtnh_next(rtnh, &remaining);
  544. }
  545. /* leftover implies invalid nexthop configuration, discard it */
  546. return remaining > 0 ? 0 : nhs;
  547. }
  548. static int mpls_nh_build_multi(struct mpls_route_config *cfg,
  549. struct mpls_route *rt)
  550. {
  551. struct rtnexthop *rtnh = cfg->rc_mp;
  552. struct nlattr *nla_via, *nla_newdst;
  553. int remaining = cfg->rc_mp_len;
  554. int nhs = 0;
  555. int err = 0;
  556. change_nexthops(rt) {
  557. int attrlen;
  558. nla_via = NULL;
  559. nla_newdst = NULL;
  560. err = -EINVAL;
  561. if (!rtnh_ok(rtnh, remaining))
  562. goto errout;
  563. /* neither weighted multipath nor any flags
  564. * are supported
  565. */
  566. if (rtnh->rtnh_hops || rtnh->rtnh_flags)
  567. goto errout;
  568. attrlen = rtnh_attrlen(rtnh);
  569. if (attrlen > 0) {
  570. struct nlattr *attrs = rtnh_attrs(rtnh);
  571. nla_via = nla_find(attrs, attrlen, RTA_VIA);
  572. nla_newdst = nla_find(attrs, attrlen, RTA_NEWDST);
  573. }
  574. err = mpls_nh_build(cfg->rc_nlinfo.nl_net, rt, nh,
  575. rtnh->rtnh_ifindex, nla_via,
  576. nla_newdst);
  577. if (err)
  578. goto errout;
  579. rtnh = rtnh_next(rtnh, &remaining);
  580. nhs++;
  581. } endfor_nexthops(rt);
  582. rt->rt_nhn = nhs;
  583. return 0;
  584. errout:
  585. return err;
  586. }
  587. static int mpls_route_add(struct mpls_route_config *cfg)
  588. {
  589. struct mpls_route __rcu **platform_label;
  590. struct net *net = cfg->rc_nlinfo.nl_net;
  591. struct mpls_route *rt, *old;
  592. int err = -EINVAL;
  593. u8 max_via_alen;
  594. unsigned index;
  595. int nhs;
  596. index = cfg->rc_label;
  597. /* If a label was not specified during insert pick one */
  598. if ((index == LABEL_NOT_SPECIFIED) &&
  599. (cfg->rc_nlflags & NLM_F_CREATE)) {
  600. index = find_free_label(net);
  601. }
  602. /* Reserved labels may not be set */
  603. if (index < MPLS_LABEL_FIRST_UNRESERVED)
  604. goto errout;
  605. /* The full 20 bit range may not be supported. */
  606. if (index >= net->mpls.platform_labels)
  607. goto errout;
  608. /* Append makes no sense with mpls */
  609. err = -EOPNOTSUPP;
  610. if (cfg->rc_nlflags & NLM_F_APPEND)
  611. goto errout;
  612. err = -EEXIST;
  613. platform_label = rtnl_dereference(net->mpls.platform_label);
  614. old = rtnl_dereference(platform_label[index]);
  615. if ((cfg->rc_nlflags & NLM_F_EXCL) && old)
  616. goto errout;
  617. err = -EEXIST;
  618. if (!(cfg->rc_nlflags & NLM_F_REPLACE) && old)
  619. goto errout;
  620. err = -ENOENT;
  621. if (!(cfg->rc_nlflags & NLM_F_CREATE) && !old)
  622. goto errout;
  623. err = -EINVAL;
  624. nhs = mpls_count_nexthops(cfg->rc_mp, cfg->rc_mp_len,
  625. cfg->rc_via_alen, &max_via_alen);
  626. if (nhs == 0)
  627. goto errout;
  628. err = -ENOMEM;
  629. rt = mpls_rt_alloc(nhs, max_via_alen);
  630. if (!rt)
  631. goto errout;
  632. rt->rt_protocol = cfg->rc_protocol;
  633. rt->rt_payload_type = cfg->rc_payload_type;
  634. if (cfg->rc_mp)
  635. err = mpls_nh_build_multi(cfg, rt);
  636. else
  637. err = mpls_nh_build_from_cfg(cfg, rt);
  638. if (err)
  639. goto freert;
  640. mpls_route_update(net, index, rt, &cfg->rc_nlinfo);
  641. return 0;
  642. freert:
  643. mpls_rt_free(rt);
  644. errout:
  645. return err;
  646. }
  647. static int mpls_route_del(struct mpls_route_config *cfg)
  648. {
  649. struct net *net = cfg->rc_nlinfo.nl_net;
  650. unsigned index;
  651. int err = -EINVAL;
  652. index = cfg->rc_label;
  653. /* Reserved labels may not be removed */
  654. if (index < MPLS_LABEL_FIRST_UNRESERVED)
  655. goto errout;
  656. /* The full 20 bit range may not be supported */
  657. if (index >= net->mpls.platform_labels)
  658. goto errout;
  659. mpls_route_update(net, index, NULL, &cfg->rc_nlinfo);
  660. err = 0;
  661. errout:
  662. return err;
  663. }
  664. #define MPLS_PERDEV_SYSCTL_OFFSET(field) \
  665. (&((struct mpls_dev *)0)->field)
  666. static const struct ctl_table mpls_dev_table[] = {
  667. {
  668. .procname = "input",
  669. .maxlen = sizeof(int),
  670. .mode = 0644,
  671. .proc_handler = proc_dointvec,
  672. .data = MPLS_PERDEV_SYSCTL_OFFSET(input_enabled),
  673. },
  674. { }
  675. };
  676. static int mpls_dev_sysctl_register(struct net_device *dev,
  677. struct mpls_dev *mdev)
  678. {
  679. char path[sizeof("net/mpls/conf/") + IFNAMSIZ];
  680. struct ctl_table *table;
  681. int i;
  682. table = kmemdup(&mpls_dev_table, sizeof(mpls_dev_table), GFP_KERNEL);
  683. if (!table)
  684. goto out;
  685. /* Table data contains only offsets relative to the base of
  686. * the mdev at this point, so make them absolute.
  687. */
  688. for (i = 0; i < ARRAY_SIZE(mpls_dev_table); i++)
  689. table[i].data = (char *)mdev + (uintptr_t)table[i].data;
  690. snprintf(path, sizeof(path), "net/mpls/conf/%s", dev->name);
  691. mdev->sysctl = register_net_sysctl(dev_net(dev), path, table);
  692. if (!mdev->sysctl)
  693. goto free;
  694. return 0;
  695. free:
  696. kfree(table);
  697. out:
  698. return -ENOBUFS;
  699. }
  700. static void mpls_dev_sysctl_unregister(struct mpls_dev *mdev)
  701. {
  702. struct ctl_table *table;
  703. table = mdev->sysctl->ctl_table_arg;
  704. unregister_net_sysctl_table(mdev->sysctl);
  705. kfree(table);
  706. }
  707. static struct mpls_dev *mpls_add_dev(struct net_device *dev)
  708. {
  709. struct mpls_dev *mdev;
  710. int err = -ENOMEM;
  711. ASSERT_RTNL();
  712. mdev = kzalloc(sizeof(*mdev), GFP_KERNEL);
  713. if (!mdev)
  714. return ERR_PTR(err);
  715. err = mpls_dev_sysctl_register(dev, mdev);
  716. if (err)
  717. goto free;
  718. rcu_assign_pointer(dev->mpls_ptr, mdev);
  719. return mdev;
  720. free:
  721. kfree(mdev);
  722. return ERR_PTR(err);
  723. }
  724. static void mpls_ifdown(struct net_device *dev)
  725. {
  726. struct mpls_route __rcu **platform_label;
  727. struct net *net = dev_net(dev);
  728. struct mpls_dev *mdev;
  729. unsigned index;
  730. platform_label = rtnl_dereference(net->mpls.platform_label);
  731. for (index = 0; index < net->mpls.platform_labels; index++) {
  732. struct mpls_route *rt = rtnl_dereference(platform_label[index]);
  733. if (!rt)
  734. continue;
  735. for_nexthops(rt) {
  736. if (rtnl_dereference(nh->nh_dev) != dev)
  737. continue;
  738. nh->nh_dev = NULL;
  739. } endfor_nexthops(rt);
  740. }
  741. mdev = mpls_dev_get(dev);
  742. if (!mdev)
  743. return;
  744. mpls_dev_sysctl_unregister(mdev);
  745. RCU_INIT_POINTER(dev->mpls_ptr, NULL);
  746. kfree_rcu(mdev, rcu);
  747. }
  748. static int mpls_dev_notify(struct notifier_block *this, unsigned long event,
  749. void *ptr)
  750. {
  751. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  752. struct mpls_dev *mdev;
  753. switch(event) {
  754. case NETDEV_REGISTER:
  755. /* For now just support ethernet devices */
  756. if ((dev->type == ARPHRD_ETHER) ||
  757. (dev->type == ARPHRD_LOOPBACK)) {
  758. mdev = mpls_add_dev(dev);
  759. if (IS_ERR(mdev))
  760. return notifier_from_errno(PTR_ERR(mdev));
  761. }
  762. break;
  763. case NETDEV_UNREGISTER:
  764. mpls_ifdown(dev);
  765. break;
  766. case NETDEV_CHANGENAME:
  767. mdev = mpls_dev_get(dev);
  768. if (mdev) {
  769. int err;
  770. mpls_dev_sysctl_unregister(mdev);
  771. err = mpls_dev_sysctl_register(dev, mdev);
  772. if (err)
  773. return notifier_from_errno(err);
  774. }
  775. break;
  776. }
  777. return NOTIFY_OK;
  778. }
  779. static struct notifier_block mpls_dev_notifier = {
  780. .notifier_call = mpls_dev_notify,
  781. };
  782. static int nla_put_via(struct sk_buff *skb,
  783. u8 table, const void *addr, int alen)
  784. {
  785. static const int table_to_family[NEIGH_NR_TABLES + 1] = {
  786. AF_INET, AF_INET6, AF_DECnet, AF_PACKET,
  787. };
  788. struct nlattr *nla;
  789. struct rtvia *via;
  790. int family = AF_UNSPEC;
  791. nla = nla_reserve(skb, RTA_VIA, alen + 2);
  792. if (!nla)
  793. return -EMSGSIZE;
  794. if (table <= NEIGH_NR_TABLES)
  795. family = table_to_family[table];
  796. via = nla_data(nla);
  797. via->rtvia_family = family;
  798. memcpy(via->rtvia_addr, addr, alen);
  799. return 0;
  800. }
  801. int nla_put_labels(struct sk_buff *skb, int attrtype,
  802. u8 labels, const u32 label[])
  803. {
  804. struct nlattr *nla;
  805. struct mpls_shim_hdr *nla_label;
  806. bool bos;
  807. int i;
  808. nla = nla_reserve(skb, attrtype, labels*4);
  809. if (!nla)
  810. return -EMSGSIZE;
  811. nla_label = nla_data(nla);
  812. bos = true;
  813. for (i = labels - 1; i >= 0; i--) {
  814. nla_label[i] = mpls_entry_encode(label[i], 0, 0, bos);
  815. bos = false;
  816. }
  817. return 0;
  818. }
  819. EXPORT_SYMBOL_GPL(nla_put_labels);
  820. int nla_get_labels(const struct nlattr *nla,
  821. u32 max_labels, u8 *labels, u32 label[])
  822. {
  823. unsigned len = nla_len(nla);
  824. unsigned nla_labels;
  825. struct mpls_shim_hdr *nla_label;
  826. bool bos;
  827. int i;
  828. /* len needs to be an even multiple of 4 (the label size) */
  829. if (len & 3)
  830. return -EINVAL;
  831. /* Limit the number of new labels allowed */
  832. nla_labels = len/4;
  833. if (nla_labels > max_labels)
  834. return -EINVAL;
  835. nla_label = nla_data(nla);
  836. bos = true;
  837. for (i = nla_labels - 1; i >= 0; i--, bos = false) {
  838. struct mpls_entry_decoded dec;
  839. dec = mpls_entry_decode(nla_label + i);
  840. /* Ensure the bottom of stack flag is properly set
  841. * and ttl and tc are both clear.
  842. */
  843. if ((dec.bos != bos) || dec.ttl || dec.tc)
  844. return -EINVAL;
  845. switch (dec.label) {
  846. case MPLS_LABEL_IMPLNULL:
  847. /* RFC3032: This is a label that an LSR may
  848. * assign and distribute, but which never
  849. * actually appears in the encapsulation.
  850. */
  851. return -EINVAL;
  852. }
  853. label[i] = dec.label;
  854. }
  855. *labels = nla_labels;
  856. return 0;
  857. }
  858. EXPORT_SYMBOL_GPL(nla_get_labels);
  859. int nla_get_via(const struct nlattr *nla, u8 *via_alen,
  860. u8 *via_table, u8 via_addr[])
  861. {
  862. struct rtvia *via = nla_data(nla);
  863. int err = -EINVAL;
  864. int alen;
  865. if (nla_len(nla) < offsetof(struct rtvia, rtvia_addr))
  866. goto errout;
  867. alen = nla_len(nla) -
  868. offsetof(struct rtvia, rtvia_addr);
  869. if (alen > MAX_VIA_ALEN)
  870. goto errout;
  871. /* Validate the address family */
  872. switch (via->rtvia_family) {
  873. case AF_PACKET:
  874. *via_table = NEIGH_LINK_TABLE;
  875. break;
  876. case AF_INET:
  877. *via_table = NEIGH_ARP_TABLE;
  878. if (alen != 4)
  879. goto errout;
  880. break;
  881. case AF_INET6:
  882. *via_table = NEIGH_ND_TABLE;
  883. if (alen != 16)
  884. goto errout;
  885. break;
  886. default:
  887. /* Unsupported address family */
  888. goto errout;
  889. }
  890. memcpy(via_addr, via->rtvia_addr, alen);
  891. *via_alen = alen;
  892. err = 0;
  893. errout:
  894. return err;
  895. }
  896. static int rtm_to_route_config(struct sk_buff *skb, struct nlmsghdr *nlh,
  897. struct mpls_route_config *cfg)
  898. {
  899. struct rtmsg *rtm;
  900. struct nlattr *tb[RTA_MAX+1];
  901. int index;
  902. int err;
  903. err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_mpls_policy);
  904. if (err < 0)
  905. goto errout;
  906. err = -EINVAL;
  907. rtm = nlmsg_data(nlh);
  908. memset(cfg, 0, sizeof(*cfg));
  909. if (rtm->rtm_family != AF_MPLS)
  910. goto errout;
  911. if (rtm->rtm_dst_len != 20)
  912. goto errout;
  913. if (rtm->rtm_src_len != 0)
  914. goto errout;
  915. if (rtm->rtm_tos != 0)
  916. goto errout;
  917. if (rtm->rtm_table != RT_TABLE_MAIN)
  918. goto errout;
  919. /* Any value is acceptable for rtm_protocol */
  920. /* As mpls uses destination specific addresses
  921. * (or source specific address in the case of multicast)
  922. * all addresses have universal scope.
  923. */
  924. if (rtm->rtm_scope != RT_SCOPE_UNIVERSE)
  925. goto errout;
  926. if (rtm->rtm_type != RTN_UNICAST)
  927. goto errout;
  928. if (rtm->rtm_flags != 0)
  929. goto errout;
  930. cfg->rc_label = LABEL_NOT_SPECIFIED;
  931. cfg->rc_protocol = rtm->rtm_protocol;
  932. cfg->rc_via_table = MPLS_NEIGH_TABLE_UNSPEC;
  933. cfg->rc_nlflags = nlh->nlmsg_flags;
  934. cfg->rc_nlinfo.portid = NETLINK_CB(skb).portid;
  935. cfg->rc_nlinfo.nlh = nlh;
  936. cfg->rc_nlinfo.nl_net = sock_net(skb->sk);
  937. for (index = 0; index <= RTA_MAX; index++) {
  938. struct nlattr *nla = tb[index];
  939. if (!nla)
  940. continue;
  941. switch(index) {
  942. case RTA_OIF:
  943. cfg->rc_ifindex = nla_get_u32(nla);
  944. break;
  945. case RTA_NEWDST:
  946. if (nla_get_labels(nla, MAX_NEW_LABELS,
  947. &cfg->rc_output_labels,
  948. cfg->rc_output_label))
  949. goto errout;
  950. break;
  951. case RTA_DST:
  952. {
  953. u8 label_count;
  954. if (nla_get_labels(nla, 1, &label_count,
  955. &cfg->rc_label))
  956. goto errout;
  957. /* Reserved labels may not be set */
  958. if (cfg->rc_label < MPLS_LABEL_FIRST_UNRESERVED)
  959. goto errout;
  960. break;
  961. }
  962. case RTA_VIA:
  963. {
  964. if (nla_get_via(nla, &cfg->rc_via_alen,
  965. &cfg->rc_via_table, cfg->rc_via))
  966. goto errout;
  967. break;
  968. }
  969. case RTA_MULTIPATH:
  970. {
  971. cfg->rc_mp = nla_data(nla);
  972. cfg->rc_mp_len = nla_len(nla);
  973. break;
  974. }
  975. default:
  976. /* Unsupported attribute */
  977. goto errout;
  978. }
  979. }
  980. err = 0;
  981. errout:
  982. return err;
  983. }
  984. static int mpls_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh)
  985. {
  986. struct mpls_route_config cfg;
  987. int err;
  988. err = rtm_to_route_config(skb, nlh, &cfg);
  989. if (err < 0)
  990. return err;
  991. return mpls_route_del(&cfg);
  992. }
  993. static int mpls_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh)
  994. {
  995. struct mpls_route_config cfg;
  996. int err;
  997. err = rtm_to_route_config(skb, nlh, &cfg);
  998. if (err < 0)
  999. return err;
  1000. return mpls_route_add(&cfg);
  1001. }
  1002. static int mpls_dump_route(struct sk_buff *skb, u32 portid, u32 seq, int event,
  1003. u32 label, struct mpls_route *rt, int flags)
  1004. {
  1005. struct net_device *dev;
  1006. struct nlmsghdr *nlh;
  1007. struct rtmsg *rtm;
  1008. nlh = nlmsg_put(skb, portid, seq, event, sizeof(*rtm), flags);
  1009. if (nlh == NULL)
  1010. return -EMSGSIZE;
  1011. rtm = nlmsg_data(nlh);
  1012. rtm->rtm_family = AF_MPLS;
  1013. rtm->rtm_dst_len = 20;
  1014. rtm->rtm_src_len = 0;
  1015. rtm->rtm_tos = 0;
  1016. rtm->rtm_table = RT_TABLE_MAIN;
  1017. rtm->rtm_protocol = rt->rt_protocol;
  1018. rtm->rtm_scope = RT_SCOPE_UNIVERSE;
  1019. rtm->rtm_type = RTN_UNICAST;
  1020. rtm->rtm_flags = 0;
  1021. if (nla_put_labels(skb, RTA_DST, 1, &label))
  1022. goto nla_put_failure;
  1023. if (rt->rt_nhn == 1) {
  1024. const struct mpls_nh *nh = rt->rt_nh;
  1025. if (nh->nh_labels &&
  1026. nla_put_labels(skb, RTA_NEWDST, nh->nh_labels,
  1027. nh->nh_label))
  1028. goto nla_put_failure;
  1029. if (nh->nh_via_table != MPLS_NEIGH_TABLE_UNSPEC &&
  1030. nla_put_via(skb, nh->nh_via_table, mpls_nh_via(rt, nh),
  1031. nh->nh_via_alen))
  1032. goto nla_put_failure;
  1033. dev = rtnl_dereference(nh->nh_dev);
  1034. if (dev && nla_put_u32(skb, RTA_OIF, dev->ifindex))
  1035. goto nla_put_failure;
  1036. } else {
  1037. struct rtnexthop *rtnh;
  1038. struct nlattr *mp;
  1039. mp = nla_nest_start(skb, RTA_MULTIPATH);
  1040. if (!mp)
  1041. goto nla_put_failure;
  1042. for_nexthops(rt) {
  1043. rtnh = nla_reserve_nohdr(skb, sizeof(*rtnh));
  1044. if (!rtnh)
  1045. goto nla_put_failure;
  1046. dev = rtnl_dereference(nh->nh_dev);
  1047. if (dev)
  1048. rtnh->rtnh_ifindex = dev->ifindex;
  1049. if (nh->nh_labels && nla_put_labels(skb, RTA_NEWDST,
  1050. nh->nh_labels,
  1051. nh->nh_label))
  1052. goto nla_put_failure;
  1053. if (nh->nh_via_table != MPLS_NEIGH_TABLE_UNSPEC &&
  1054. nla_put_via(skb, nh->nh_via_table,
  1055. mpls_nh_via(rt, nh),
  1056. nh->nh_via_alen))
  1057. goto nla_put_failure;
  1058. /* length of rtnetlink header + attributes */
  1059. rtnh->rtnh_len = nlmsg_get_pos(skb) - (void *)rtnh;
  1060. } endfor_nexthops(rt);
  1061. nla_nest_end(skb, mp);
  1062. }
  1063. nlmsg_end(skb, nlh);
  1064. return 0;
  1065. nla_put_failure:
  1066. nlmsg_cancel(skb, nlh);
  1067. return -EMSGSIZE;
  1068. }
  1069. static int mpls_dump_routes(struct sk_buff *skb, struct netlink_callback *cb)
  1070. {
  1071. struct net *net = sock_net(skb->sk);
  1072. struct mpls_route __rcu **platform_label;
  1073. size_t platform_labels;
  1074. unsigned int index;
  1075. ASSERT_RTNL();
  1076. index = cb->args[0];
  1077. if (index < MPLS_LABEL_FIRST_UNRESERVED)
  1078. index = MPLS_LABEL_FIRST_UNRESERVED;
  1079. platform_label = rtnl_dereference(net->mpls.platform_label);
  1080. platform_labels = net->mpls.platform_labels;
  1081. for (; index < platform_labels; index++) {
  1082. struct mpls_route *rt;
  1083. rt = rtnl_dereference(platform_label[index]);
  1084. if (!rt)
  1085. continue;
  1086. if (mpls_dump_route(skb, NETLINK_CB(cb->skb).portid,
  1087. cb->nlh->nlmsg_seq, RTM_NEWROUTE,
  1088. index, rt, NLM_F_MULTI) < 0)
  1089. break;
  1090. }
  1091. cb->args[0] = index;
  1092. return skb->len;
  1093. }
  1094. static inline size_t lfib_nlmsg_size(struct mpls_route *rt)
  1095. {
  1096. size_t payload =
  1097. NLMSG_ALIGN(sizeof(struct rtmsg))
  1098. + nla_total_size(4); /* RTA_DST */
  1099. if (rt->rt_nhn == 1) {
  1100. struct mpls_nh *nh = rt->rt_nh;
  1101. if (nh->nh_dev)
  1102. payload += nla_total_size(4); /* RTA_OIF */
  1103. if (nh->nh_via_table != MPLS_NEIGH_TABLE_UNSPEC) /* RTA_VIA */
  1104. payload += nla_total_size(2 + nh->nh_via_alen);
  1105. if (nh->nh_labels) /* RTA_NEWDST */
  1106. payload += nla_total_size(nh->nh_labels * 4);
  1107. } else {
  1108. /* each nexthop is packed in an attribute */
  1109. size_t nhsize = 0;
  1110. for_nexthops(rt) {
  1111. nhsize += nla_total_size(sizeof(struct rtnexthop));
  1112. /* RTA_VIA */
  1113. if (nh->nh_via_table != MPLS_NEIGH_TABLE_UNSPEC)
  1114. nhsize += nla_total_size(2 + nh->nh_via_alen);
  1115. if (nh->nh_labels)
  1116. nhsize += nla_total_size(nh->nh_labels * 4);
  1117. } endfor_nexthops(rt);
  1118. /* nested attribute */
  1119. payload += nla_total_size(nhsize);
  1120. }
  1121. return payload;
  1122. }
  1123. static void rtmsg_lfib(int event, u32 label, struct mpls_route *rt,
  1124. struct nlmsghdr *nlh, struct net *net, u32 portid,
  1125. unsigned int nlm_flags)
  1126. {
  1127. struct sk_buff *skb;
  1128. u32 seq = nlh ? nlh->nlmsg_seq : 0;
  1129. int err = -ENOBUFS;
  1130. skb = nlmsg_new(lfib_nlmsg_size(rt), GFP_KERNEL);
  1131. if (skb == NULL)
  1132. goto errout;
  1133. err = mpls_dump_route(skb, portid, seq, event, label, rt, nlm_flags);
  1134. if (err < 0) {
  1135. /* -EMSGSIZE implies BUG in lfib_nlmsg_size */
  1136. WARN_ON(err == -EMSGSIZE);
  1137. kfree_skb(skb);
  1138. goto errout;
  1139. }
  1140. rtnl_notify(skb, net, portid, RTNLGRP_MPLS_ROUTE, nlh, GFP_KERNEL);
  1141. return;
  1142. errout:
  1143. if (err < 0)
  1144. rtnl_set_sk_err(net, RTNLGRP_MPLS_ROUTE, err);
  1145. }
  1146. static int resize_platform_label_table(struct net *net, size_t limit)
  1147. {
  1148. size_t size = sizeof(struct mpls_route *) * limit;
  1149. size_t old_limit;
  1150. size_t cp_size;
  1151. struct mpls_route __rcu **labels = NULL, **old;
  1152. struct mpls_route *rt0 = NULL, *rt2 = NULL;
  1153. unsigned index;
  1154. if (size) {
  1155. labels = kzalloc(size, GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY);
  1156. if (!labels)
  1157. labels = vzalloc(size);
  1158. if (!labels)
  1159. goto nolabels;
  1160. }
  1161. /* In case the predefined labels need to be populated */
  1162. if (limit > MPLS_LABEL_IPV4NULL) {
  1163. struct net_device *lo = net->loopback_dev;
  1164. rt0 = mpls_rt_alloc(1, lo->addr_len);
  1165. if (!rt0)
  1166. goto nort0;
  1167. RCU_INIT_POINTER(rt0->rt_nh->nh_dev, lo);
  1168. rt0->rt_protocol = RTPROT_KERNEL;
  1169. rt0->rt_payload_type = MPT_IPV4;
  1170. rt0->rt_nh->nh_via_table = NEIGH_LINK_TABLE;
  1171. rt0->rt_nh->nh_via_alen = lo->addr_len;
  1172. memcpy(__mpls_nh_via(rt0, rt0->rt_nh), lo->dev_addr,
  1173. lo->addr_len);
  1174. }
  1175. if (limit > MPLS_LABEL_IPV6NULL) {
  1176. struct net_device *lo = net->loopback_dev;
  1177. rt2 = mpls_rt_alloc(1, lo->addr_len);
  1178. if (!rt2)
  1179. goto nort2;
  1180. RCU_INIT_POINTER(rt2->rt_nh->nh_dev, lo);
  1181. rt2->rt_protocol = RTPROT_KERNEL;
  1182. rt2->rt_payload_type = MPT_IPV6;
  1183. rt2->rt_nh->nh_via_table = NEIGH_LINK_TABLE;
  1184. rt2->rt_nh->nh_via_alen = lo->addr_len;
  1185. memcpy(__mpls_nh_via(rt2, rt2->rt_nh), lo->dev_addr,
  1186. lo->addr_len);
  1187. }
  1188. rtnl_lock();
  1189. /* Remember the original table */
  1190. old = rtnl_dereference(net->mpls.platform_label);
  1191. old_limit = net->mpls.platform_labels;
  1192. /* Free any labels beyond the new table */
  1193. for (index = limit; index < old_limit; index++)
  1194. mpls_route_update(net, index, NULL, NULL);
  1195. /* Copy over the old labels */
  1196. cp_size = size;
  1197. if (old_limit < limit)
  1198. cp_size = old_limit * sizeof(struct mpls_route *);
  1199. memcpy(labels, old, cp_size);
  1200. /* If needed set the predefined labels */
  1201. if ((old_limit <= MPLS_LABEL_IPV6NULL) &&
  1202. (limit > MPLS_LABEL_IPV6NULL)) {
  1203. RCU_INIT_POINTER(labels[MPLS_LABEL_IPV6NULL], rt2);
  1204. rt2 = NULL;
  1205. }
  1206. if ((old_limit <= MPLS_LABEL_IPV4NULL) &&
  1207. (limit > MPLS_LABEL_IPV4NULL)) {
  1208. RCU_INIT_POINTER(labels[MPLS_LABEL_IPV4NULL], rt0);
  1209. rt0 = NULL;
  1210. }
  1211. /* Update the global pointers */
  1212. net->mpls.platform_labels = limit;
  1213. rcu_assign_pointer(net->mpls.platform_label, labels);
  1214. rtnl_unlock();
  1215. mpls_rt_free(rt2);
  1216. mpls_rt_free(rt0);
  1217. if (old) {
  1218. synchronize_rcu();
  1219. kvfree(old);
  1220. }
  1221. return 0;
  1222. nort2:
  1223. mpls_rt_free(rt0);
  1224. nort0:
  1225. kvfree(labels);
  1226. nolabels:
  1227. return -ENOMEM;
  1228. }
  1229. static int mpls_platform_labels(struct ctl_table *table, int write,
  1230. void __user *buffer, size_t *lenp, loff_t *ppos)
  1231. {
  1232. struct net *net = table->data;
  1233. int platform_labels = net->mpls.platform_labels;
  1234. int ret;
  1235. struct ctl_table tmp = {
  1236. .procname = table->procname,
  1237. .data = &platform_labels,
  1238. .maxlen = sizeof(int),
  1239. .mode = table->mode,
  1240. .extra1 = &zero,
  1241. .extra2 = &label_limit,
  1242. };
  1243. ret = proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos);
  1244. if (write && ret == 0)
  1245. ret = resize_platform_label_table(net, platform_labels);
  1246. return ret;
  1247. }
  1248. static const struct ctl_table mpls_table[] = {
  1249. {
  1250. .procname = "platform_labels",
  1251. .data = NULL,
  1252. .maxlen = sizeof(int),
  1253. .mode = 0644,
  1254. .proc_handler = mpls_platform_labels,
  1255. },
  1256. { }
  1257. };
  1258. static int mpls_net_init(struct net *net)
  1259. {
  1260. struct ctl_table *table;
  1261. net->mpls.platform_labels = 0;
  1262. net->mpls.platform_label = NULL;
  1263. table = kmemdup(mpls_table, sizeof(mpls_table), GFP_KERNEL);
  1264. if (table == NULL)
  1265. return -ENOMEM;
  1266. table[0].data = net;
  1267. net->mpls.ctl = register_net_sysctl(net, "net/mpls", table);
  1268. if (net->mpls.ctl == NULL) {
  1269. kfree(table);
  1270. return -ENOMEM;
  1271. }
  1272. return 0;
  1273. }
  1274. static void mpls_net_exit(struct net *net)
  1275. {
  1276. struct mpls_route __rcu **platform_label;
  1277. size_t platform_labels;
  1278. struct ctl_table *table;
  1279. unsigned int index;
  1280. table = net->mpls.ctl->ctl_table_arg;
  1281. unregister_net_sysctl_table(net->mpls.ctl);
  1282. kfree(table);
  1283. /* An rcu grace period has passed since there was a device in
  1284. * the network namespace (and thus the last in flight packet)
  1285. * left this network namespace. This is because
  1286. * unregister_netdevice_many and netdev_run_todo has completed
  1287. * for each network device that was in this network namespace.
  1288. *
  1289. * As such no additional rcu synchronization is necessary when
  1290. * freeing the platform_label table.
  1291. */
  1292. rtnl_lock();
  1293. platform_label = rtnl_dereference(net->mpls.platform_label);
  1294. platform_labels = net->mpls.platform_labels;
  1295. for (index = 0; index < platform_labels; index++) {
  1296. struct mpls_route *rt = rtnl_dereference(platform_label[index]);
  1297. RCU_INIT_POINTER(platform_label[index], NULL);
  1298. mpls_rt_free(rt);
  1299. }
  1300. rtnl_unlock();
  1301. kvfree(platform_label);
  1302. }
  1303. static struct pernet_operations mpls_net_ops = {
  1304. .init = mpls_net_init,
  1305. .exit = mpls_net_exit,
  1306. };
  1307. static int __init mpls_init(void)
  1308. {
  1309. int err;
  1310. BUILD_BUG_ON(sizeof(struct mpls_shim_hdr) != 4);
  1311. err = register_pernet_subsys(&mpls_net_ops);
  1312. if (err)
  1313. goto out;
  1314. err = register_netdevice_notifier(&mpls_dev_notifier);
  1315. if (err)
  1316. goto out_unregister_pernet;
  1317. dev_add_pack(&mpls_packet_type);
  1318. rtnl_register(PF_MPLS, RTM_NEWROUTE, mpls_rtm_newroute, NULL, NULL);
  1319. rtnl_register(PF_MPLS, RTM_DELROUTE, mpls_rtm_delroute, NULL, NULL);
  1320. rtnl_register(PF_MPLS, RTM_GETROUTE, NULL, mpls_dump_routes, NULL);
  1321. err = 0;
  1322. out:
  1323. return err;
  1324. out_unregister_pernet:
  1325. unregister_pernet_subsys(&mpls_net_ops);
  1326. goto out;
  1327. }
  1328. module_init(mpls_init);
  1329. static void __exit mpls_exit(void)
  1330. {
  1331. rtnl_unregister_all(PF_MPLS);
  1332. dev_remove_pack(&mpls_packet_type);
  1333. unregister_netdevice_notifier(&mpls_dev_notifier);
  1334. unregister_pernet_subsys(&mpls_net_ops);
  1335. }
  1336. module_exit(mpls_exit);
  1337. MODULE_DESCRIPTION("MultiProtocol Label Switching");
  1338. MODULE_LICENSE("GPL v2");
  1339. MODULE_ALIAS_NETPROTO(PF_MPLS);