af_mpls.c 36 KB

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